- Fixed shadows not being cast if the lightsource is outside the sub and the convexhull is not

- Option to change the texture of the "light sphere"
- Added "headlights" to the scooters
This commit is contained in:
Regalis
2016-05-03 00:27:48 +03:00
parent 26608d3f82
commit 03626c997b
9 changed files with 122 additions and 47 deletions

View File

@@ -64,6 +64,12 @@ namespace Barotrauma.Lights
private Rectangle boundingBox;
public Entity ParentEntity
{
get { return parentEntity; }
}
public bool Enabled
{
get;
@@ -302,9 +308,13 @@ namespace Barotrauma.Lights
{
if (!Enabled) return;
Vector2 lightSourcePos = light.Position;
if (light.Submarine==null && parentEntity != null && parentEntity.Submarine != null) lightSourcePos -= parentEntity.Submarine.Position;
CachedShadow cachedShadow = null;
if (cachedShadows.TryGetValue(light, out cachedShadow) &&
(light.Position == cachedShadow.LightPos || Vector2.DistanceSquared(light.Position, cachedShadow.LightPos) < 1.0f))
if (cachedShadows.TryGetValue(light, out cachedShadow) &&
(lightSourcePos == cachedShadow.LightPos || Vector2.DistanceSquared(lightSourcePos, cachedShadow.LightPos) < 1.0f))
{
}
@@ -315,17 +325,17 @@ namespace Barotrauma.Lights
// lightPos = light.Position;
//}
CalculateShadowVertices(light.Position, los);
CalculateShadowVertices(lightSourcePos, los);
if (cachedShadow != null)
{
cachedShadow.LightPos = light.Position;
cachedShadow.LightPos = lightSourcePos;
cachedShadow.ShadowBuffer.SetData(shadowVertices, 0, shadowVertices.Length);
cachedShadow.ShadowVertexCount = shadowVertexCount;
}
else
{
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position, shadowVertexCount, 0);
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, lightSourcePos, shadowVertexCount, 0);
RemoveCachedShadow(light);
cachedShadows.Add(light, cachedShadow);
}

View File

@@ -2,6 +2,7 @@
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Barotrauma.Lights
{
@@ -122,7 +123,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights)
{
if (light.hullsInRange.Count == 0 || light.Color.A < 0.01f || light.Range < 1.0f) continue;
if (!light.hullsInRange.Any() || light.Color.A < 0.01f || light.Range < 1.0f) continue;
if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
//clear alpha to 1
@@ -135,7 +136,7 @@ namespace Barotrauma.Lights
foreach (ConvexHull ch in light.hullsInRange)
{
if (!MathUtils.CircleIntersectsRectangle(light.Position, light.Range, ch.BoundingBox)) continue;
//if (!MathUtils.CircleIntersectsRectangle(light.Position, light.Range, ch.BoundingBox)) continue;
//draw shadow
ch.DrawShadows(graphics, cam, light, shadowTransform, false);
}
@@ -152,7 +153,7 @@ namespace Barotrauma.Lights
foreach (LightSource light in lights)
{
if (light.hullsInRange.Count > 0 || light.Color.A < 0.01f) continue;
if (light.hullsInRange.Any() || light.Color.A < 0.01f) continue;
//if (!MathUtils.CircleIntersectsRectangle(light.WorldPosition, light.Range, viewRect)) continue;
light.Draw(spriteBatch);
@@ -163,10 +164,6 @@ namespace Barotrauma.Lights
//clear alpha, to avoid messing stuff up later
ClearAlphaToOne(graphics, spriteBatch);
graphics.SetRenderTarget(null);
if (!ObstructVision) return;
}
public void UpdateObstructVision(GraphicsDevice graphics, SpriteBatch spriteBatch, Camera cam, Vector2 lookAtPosition)

View File

@@ -22,8 +22,12 @@ namespace Barotrauma.Lights
public Sprite LightSprite;
private Sprite overrideLightTexture;
public Entity Submarine;
public bool CastShadows;
//what was the range of the light when HullsInRange were last updated
private float prevHullUpdateRange;
@@ -45,6 +49,12 @@ namespace Barotrauma.Lights
}
}
public float Rotation
{
get;
set;
}
public Vector2 WorldPosition
{
get { return (Submarine == null) ? position : position + Submarine.Position; }
@@ -84,17 +94,24 @@ namespace Barotrauma.Lights
}
public LightSource (XElement element)
:this(Vector2.Zero, 100.0f, Color.White, null)
: this(Vector2.Zero, 100.0f, Color.White, null)
{
float range = ToolBox.GetAttributeFloat(element, "range", 100.0f);
Color color = new Color(ToolBox.GetAttributeVector4(element, "color", Vector4.One));
CastShadows = ToolBox.GetAttributeBool(element, "castshadows", true);
foreach (XElement subElement in element.Elements())
{
if (subElement.Name.ToString().ToLowerInvariant() != "sprite") continue;
LightSprite = new Sprite(subElement);
LightSprite.Origin = LightSprite.size / 2.0f;
switch (subElement.Name.ToString().ToLowerInvariant())
{
case "sprite":
LightSprite = new Sprite(subElement);
break;
case "lighttexture":
overrideLightTexture = new Sprite(subElement);
break;
}
}
}
@@ -108,6 +125,8 @@ namespace Barotrauma.Lights
this.range = range;
this.color = color;
CastShadows = true;
texture = LightTexture;
GameMain.LightManager.AddLight(this);
@@ -115,12 +134,25 @@ namespace Barotrauma.Lights
public void UpdateHullsInRange()
{
if (!CastShadows) return;
hullsInRange.Clear();
if (range < 1.0f || color.A < 0.01f) return;
foreach (ConvexHull ch in ConvexHull.list)
{
if (MathUtils.CircleIntersectsRectangle(position, range, ch.BoundingBox)) hullsInRange.Add(ch);
if (Submarine == null && ch.ParentEntity.Submarine != null)
{
if (MathUtils.CircleIntersectsRectangle(position - ch.ParentEntity.Submarine.Position, range, ch.BoundingBox))
{
hullsInRange.Add(ch);
}
}
else if (MathUtils.CircleIntersectsRectangle(position, range, ch.BoundingBox))
{
hullsInRange.Add(ch);
}
}
}
@@ -128,15 +160,25 @@ namespace Barotrauma.Lights
{
if (range > 1.0f)
{
Vector2 center = new Vector2(LightTexture.Width / 2, LightTexture.Height / 2);
float scale = range / (lightTexture.Width / 2.0f);
if (overrideLightTexture == null)
{
Vector2 center = new Vector2(LightTexture.Width / 2, LightTexture.Height / 2);
float scale = range / (lightTexture.Width / 2.0f);
spriteBatch.Draw(lightTexture, new Vector2(WorldPosition.X, -WorldPosition.Y), null, color, 0, center, scale, SpriteEffects.None, 1);
spriteBatch.Draw(lightTexture, new Vector2(WorldPosition.X, -WorldPosition.Y), null, color, 0, center, scale, SpriteEffects.None, 1);
}
else
{
overrideLightTexture.Draw(spriteBatch,
new Vector2(WorldPosition.X, -WorldPosition.Y), Color,
overrideLightTexture.Origin, -Rotation,
new Vector2(overrideLightTexture.size.X/overrideLightTexture.SourceRect.Width, overrideLightTexture.size.Y/overrideLightTexture.SourceRect.Height));
}
}
if (LightSprite != null)
{
LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color);
LightSprite.Draw(spriteBatch, new Vector2(WorldPosition.X, -WorldPosition.Y), Color, LightSprite.Origin);
}
}