Lighting fixes & teleporting characters to sub
This commit is contained in:
@@ -30,6 +30,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
private Dictionary<LightSource, CachedShadow> cachedShadows;
|
||||
|
||||
private Vector2[] worldVertices;
|
||||
private Vector2[] vertices;
|
||||
private int primitiveCount;
|
||||
|
||||
@@ -38,6 +39,8 @@ namespace Barotrauma.Lights
|
||||
private VertexPositionColor[] shadowVertices;
|
||||
private VertexPositionTexture[] penumbraVertices;
|
||||
|
||||
private Entity parentEntity;
|
||||
|
||||
private Rectangle boundingBox;
|
||||
|
||||
public bool Enabled
|
||||
@@ -51,7 +54,7 @@ namespace Barotrauma.Lights
|
||||
get { return boundingBox; }
|
||||
}
|
||||
|
||||
public ConvexHull(Vector2[] points, Color color)
|
||||
public ConvexHull(Vector2[] points, Color color, Entity parent)
|
||||
{
|
||||
if (shadowEffect == null)
|
||||
{
|
||||
@@ -67,9 +70,12 @@ namespace Barotrauma.Lights
|
||||
penumbraEffect.Texture = TextureLoader.FromFile("Content/Lights/penumbra.png");
|
||||
}
|
||||
|
||||
parentEntity = parent;
|
||||
|
||||
cachedShadows = new Dictionary<LightSource, CachedShadow>();
|
||||
|
||||
vertices = points;
|
||||
worldVertices = new Vector2[vertices.Length];
|
||||
primitiveCount = vertices.Length;
|
||||
|
||||
CalculateDimensions();
|
||||
@@ -109,6 +115,7 @@ namespace Barotrauma.Lights
|
||||
for (int i = 0; i < vertices.Count(); i++)
|
||||
{
|
||||
vertices[i] += amount;
|
||||
worldVertices[i] += amount;
|
||||
}
|
||||
|
||||
CalculateDimensions();
|
||||
@@ -118,17 +125,26 @@ namespace Barotrauma.Lights
|
||||
{
|
||||
cachedShadows.Clear();
|
||||
|
||||
worldVertices = points;
|
||||
vertices = points;
|
||||
}
|
||||
|
||||
private void CalculateShadowVertices(Vector2 lightSourcePos, bool los = true)
|
||||
{
|
||||
for (int i = 0; i < vertices.Length; i++)
|
||||
{
|
||||
worldVertices[i] = vertices[i];
|
||||
if (parentEntity != null && parentEntity.Submarine != null)
|
||||
{
|
||||
worldVertices[i] += parentEntity.Submarine.Position;
|
||||
}
|
||||
}
|
||||
//compute facing of each edge, using N*L
|
||||
for (int i = 0; i < primitiveCount; i++)
|
||||
{
|
||||
Vector2 firstVertex = new Vector2(vertices[i].X, vertices[i].Y);
|
||||
Vector2 firstVertex = new Vector2(worldVertices[i].X, worldVertices[i].Y);
|
||||
int secondIndex = (i + 1) % primitiveCount;
|
||||
Vector2 secondVertex = new Vector2(vertices[secondIndex].X, vertices[secondIndex].Y);
|
||||
Vector2 secondVertex = new Vector2(worldVertices[secondIndex].X, worldVertices[secondIndex].Y);
|
||||
Vector2 middle = (firstVertex + secondVertex) / 2;
|
||||
|
||||
Vector2 L = lightSourcePos - middle;
|
||||
@@ -171,7 +187,7 @@ namespace Barotrauma.Lights
|
||||
int svCount = 0;
|
||||
while (svCount != shadowVertexCount * 2)
|
||||
{
|
||||
Vector3 vertexPos = new Vector3(vertices[currentIndex], 0.0f);
|
||||
Vector3 vertexPos = new Vector3(worldVertices[currentIndex], 0.0f);
|
||||
|
||||
//one vertex on the hull
|
||||
shadowVertices[svCount] = new VertexPositionColor();
|
||||
@@ -201,7 +217,7 @@ namespace Barotrauma.Lights
|
||||
|
||||
for (int n = 0; n < 4; n += 3)
|
||||
{
|
||||
Vector3 penumbraStart = new Vector3((n == 0) ? vertices[startingIndex] : vertices[endingIndex], 0.0f);
|
||||
Vector3 penumbraStart = new Vector3((n == 0) ? worldVertices[startingIndex] : worldVertices[endingIndex], 0.0f);
|
||||
|
||||
penumbraVertices[n] = new VertexPositionTexture();
|
||||
penumbraVertices[n].Position = penumbraStart;
|
||||
@@ -248,7 +264,7 @@ namespace Barotrauma.Lights
|
||||
if (cachedShadows.TryGetValue(light, out cachedShadow))
|
||||
{
|
||||
if (light.Position == cachedShadow.LightPos ||
|
||||
Vector2.DistanceSquared(light.Position, cachedShadow.LightPos) < 1.0f)
|
||||
Vector2.DistanceSquared(light.WorldPosition, cachedShadow.LightPos) < 1.0f)
|
||||
{
|
||||
shadowVertices = cachedShadow.ShadowVertices;
|
||||
penumbraVertices = cachedShadow.PenumbraVertices;
|
||||
@@ -256,8 +272,8 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
else
|
||||
{
|
||||
CalculateShadowVertices(light.Position, los);
|
||||
cachedShadow.LightPos = light.Position;
|
||||
CalculateShadowVertices(light.WorldPosition, los);
|
||||
cachedShadow.LightPos = light.WorldPosition;
|
||||
cachedShadow.ShadowVertices = shadowVertices;
|
||||
cachedShadow.PenumbraVertices = penumbraVertices;
|
||||
|
||||
@@ -266,7 +282,7 @@ namespace Barotrauma.Lights
|
||||
else
|
||||
{
|
||||
CalculateShadowVertices(light.Position, los);
|
||||
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.Position);
|
||||
cachedShadow = new CachedShadow(shadowVertices, penumbraVertices, light.WorldPosition);
|
||||
cachedShadows.Add(light, cachedShadow);
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,8 @@ namespace Barotrauma.Lights
|
||||
|
||||
private Texture2D texture;
|
||||
|
||||
public Entity Submarine;
|
||||
|
||||
private Vector2 position;
|
||||
public Vector2 Position
|
||||
{
|
||||
@@ -32,6 +34,11 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
}
|
||||
|
||||
public Vector2 WorldPosition
|
||||
{
|
||||
get { return (Submarine == null) ? position : position + Submarine.Position; }
|
||||
}
|
||||
|
||||
public static Texture2D LightTexture
|
||||
{
|
||||
get
|
||||
@@ -64,10 +71,12 @@ namespace Barotrauma.Lights
|
||||
}
|
||||
}
|
||||
|
||||
public LightSource(Vector2 position, float range, Color color)
|
||||
public LightSource(Vector2 position, float range, Color color, Submarine submarine)
|
||||
{
|
||||
hullsInRange = new List<ConvexHull>();
|
||||
|
||||
this.Submarine = submarine;
|
||||
|
||||
this.position = position;
|
||||
this.range = range;
|
||||
this.color = color;
|
||||
|
||||
Reference in New Issue
Block a user