// Checks if buffer isn't full at 80% (or something like that). If is, we start fadeing-out first 20% triangles (or something like that). But always all triangles of a decal. void CheckIfBufferIsFull() { if (m_status == MyDecalsBufferState.FADING_OUT_ALL) { return; } else if (m_status == MyDecalsBufferState.FADING_OUT_ONLY_BEGINNING) { if ((MyMinerGame.TotalGamePlayTimeInMilliseconds - m_fadingOutStartTime) > MyDecalsConstants.DECALS_FADE_OUT_INTERVAL_MILISECONDS) { // If fading-out phase finished, we change state and remove faded-out triangles for (int i = 0; i < m_fadingOutRealTriangleCount; i++) { MyDecalTriangle fadedoutTriangle = m_trianglesQueue.Dequeue(); fadedoutTriangle.Close(); m_freeTriangles.Push(fadedoutTriangle); } m_status = MyDecalsBufferState.READY; } } else { if (m_trianglesQueue.Count >= m_fadingOutStartLimit) { // If we get here, buffer is close to be full, so we start fade-out phase m_fadingOutRealTriangleCount = GetFadingOutRealTriangleCount(); m_status = MyDecalsBufferState.FADING_OUT_ONLY_BEGINNING; m_fadingOutStartTime = MyMinerGame.TotalGamePlayTimeInMilliseconds; } } }
/// <summary> /// Reduces the alpha of the decalTriangle if it's in front of the decal position. /// The further it is, the lower alpha it gets. /// </summary> private static void AdjustAlphaByDistance(Vector3 normal, float decalSize, Vector3 position, MyDecalTriangle decalTriangle) { Vector3 displacement0 = decalTriangle.Position0 - position; var dot0 = Vector3.Dot(displacement0, normal) / decalSize; if (dot0 > 0) { float alignment0 = 1 - dot0; decalTriangle.Color0.W *= alignment0; } Vector3 displacement1 = decalTriangle.Position1 - position; var dot1 = Vector3.Dot(displacement1, normal) / decalSize; if (dot1 > 0) { float alignment1 = 1 - dot1; decalTriangle.Color1.W *= alignment1; } Vector3 displacement2 = decalTriangle.Position2 - position; var dot2 = Vector3.Dot(displacement2, normal) / decalSize; if (dot2 > 0) { float alignment2 = 1 - dot2; decalTriangle.Color2.W *= alignment2; } }
public static float UpdateDecalEmissivity(MyDecalTriangle decalTriangle, float alpha, MyEntity entity) { float emisivity = 0; if (decalTriangle.Emissivity > 0) { //emisivity = (float)(Math.Sin(decalTriangle.RandomOffset + decalTriangle.RandomOffset * MyMinerGame.TotalGamePlayTimeInMilliseconds / 1000.0f)) * 0.4f + 0.7f; // 2 seconds default, more emissive lit longer float stableLength = 2000 * decalTriangle.Emissivity; if ((MyMinerGame.TotalGamePlayTimeInMilliseconds - decalTriangle.RandomOffset) < stableLength) { emisivity = 1; } else { emisivity = (float)(500 - (MyMinerGame.TotalGamePlayTimeInMilliseconds - stableLength - decalTriangle.RandomOffset)) / 500.0f; if (emisivity < 0) { emisivity = 0; } } emisivity *= decalTriangle.Emissivity; if (emisivity > 0) { Vector4 color = MyDecalsConstants.PROJECTILE_DECAL_COLOR; Vector3 position; if (entity != null) { position = Vector3.Transform(decalTriangle.Position, entity.WorldMatrix); } else { position = decalTriangle.Position; } MinerWars.AppCode.Game.TransparentGeometry.MyTransparentGeometry.AddPointBillboard( TransparentGeometry.MyTransparentMaterialEnum.DecalGlare, color * alpha, position, 1.5f * emisivity, 0); if (decalTriangle.Light != null) { decalTriangle.Light.Color = color; decalTriangle.Light.SetPosition(position); float range = Math.Max(3 * emisivity * alpha, 0.1f); decalTriangle.Light.Range = range; } } } return(emisivity); }
public void Add(MyTriangle_Vertex_Normals triangle, Vector3 normal, ref MyPlane rightPlane, ref MyPlane upPlane, float decalScale, float decalSize, int remainingTrianglesOfThisDecal, Vector4 color, bool alphaBlendByAngle, float lightSize, Vector3 position, float emissivity) { // We can't add triangles while fading out //if (m_status == MyDecalsBufferState.FADING_OUT) return; MyDecalTriangle decalTriangle = m_freeTriangles.Pop(); decalTriangle.Start(lightSize); decalTriangle.Emissivity = emissivity; //decalTriangle.RandomOffset = MyMwcUtils.GetRandomFloat(0.0f, MathHelper.Pi); decalTriangle.RandomOffset = MyMinerGame.TotalGamePlayTimeInMilliseconds; decalTriangle.Position = position; // We must repack vertex positions before copying to new triange, to avoid Z-fight. decalTriangle.Position0 = VF_Packer.RepackModelPosition(ref triangle.Vertexes.Vertex0); decalTriangle.Position1 = VF_Packer.RepackModelPosition(ref triangle.Vertexes.Vertex1); decalTriangle.Position2 = VF_Packer.RepackModelPosition(ref triangle.Vertexes.Vertex2); // Texture coords decalTriangle.TexCoord0 = new Vector2( 0.5f + decalScale * MyUtils.GetDistanceFromPointToPlane(ref decalTriangle.Position0, ref rightPlane), 0.5f + decalScale * MyUtils.GetDistanceFromPointToPlane(ref decalTriangle.Position0, ref upPlane)); decalTriangle.TexCoord1 = new Vector2( 0.5f + decalScale * MyUtils.GetDistanceFromPointToPlane(ref decalTriangle.Position1, ref rightPlane), 0.5f + decalScale * MyUtils.GetDistanceFromPointToPlane(ref decalTriangle.Position1, ref upPlane)); decalTriangle.TexCoord2 = new Vector2( 0.5f + decalScale * MyUtils.GetDistanceFromPointToPlane(ref decalTriangle.Position2, ref rightPlane), 0.5f + decalScale * MyUtils.GetDistanceFromPointToPlane(ref decalTriangle.Position2, ref upPlane)); // Alpha decalTriangle.Color0 = color; decalTriangle.Color1 = color; decalTriangle.Color2 = color; if (alphaBlendByAngle) { decalTriangle.Color0.W = GetAlphaByAngleDiff(ref normal, ref triangle.Normals.Normal0); decalTriangle.Color1.W = GetAlphaByAngleDiff(ref normal, ref triangle.Normals.Normal1); decalTriangle.Color2.W = GetAlphaByAngleDiff(ref normal, ref triangle.Normals.Normal2); } AdjustAlphaByDistance(normal, decalSize, position, decalTriangle); // Bump mapping decalTriangle.Normal0 = triangle.Normals.Normal0; decalTriangle.Normal1 = triangle.Normals.Normal1; decalTriangle.Normal2 = triangle.Normals.Normal2; decalTriangle.Binormal0 = triangle.Binormals.Normal0; decalTriangle.Binormal1 = triangle.Binormals.Normal1; decalTriangle.Binormal2 = triangle.Binormals.Normal2; decalTriangle.Tangent0 = triangle.Tangents.Normal0; decalTriangle.Tangent1 = triangle.Tangents.Normal1; decalTriangle.Tangent2 = triangle.Tangents.Normal2; decalTriangle.RemainingTrianglesOfThisDecal = remainingTrianglesOfThisDecal; m_trianglesQueue.Enqueue(decalTriangle); }
// We can't just erase decal triangles. We need to push them back into 'free triangles stack'. So we do it here. public void Clear(bool destroy = false) { while (m_trianglesQueue.Count > 0) { MyDecalTriangle fadedoutTriangle = m_trianglesQueue.Dequeue(); fadedoutTriangle.Close(); m_freeTriangles.Push(fadedoutTriangle); } if (destroy) { VoxelMap = null; } }
// We can just erase decal triangles, because here we don't have 'free triangles stack' as voxel decals do. public void Clear(bool destroy = false) { while (m_trianglesQueue.Count > 0) { MyDecalTriangle triangle = m_trianglesQueue.Dequeue(); triangle.Close(); m_freeTriangles.Push(triangle); } if (destroy) { Entity = null; } }
public static float UpdateDecalEmissivity(MyDecalTriangle decalTriangle, float alpha, MyEntity entity) { float emisivity = 0; if (decalTriangle.Emissivity > 0) { //emisivity = (float)(Math.Sin(decalTriangle.RandomOffset + decalTriangle.RandomOffset * MyMinerGame.TotalGamePlayTimeInMilliseconds / 1000.0f)) * 0.4f + 0.7f; // 2 seconds default, more emissive lit longer float stableLength = 2000 * decalTriangle.Emissivity; if ((MyMinerGame.TotalGamePlayTimeInMilliseconds - decalTriangle.RandomOffset) < stableLength) emisivity = 1; else { emisivity = (float)(500 - (MyMinerGame.TotalGamePlayTimeInMilliseconds - stableLength - decalTriangle.RandomOffset)) / 500.0f; if (emisivity < 0) emisivity = 0; } emisivity *= decalTriangle.Emissivity; if (emisivity > 0) { Vector4 color = MyDecalsConstants.PROJECTILE_DECAL_COLOR; Vector3 position; if (entity != null) { position = Vector3.Transform(decalTriangle.Position, entity.WorldMatrix); } else { position = decalTriangle.Position; } MinerWars.AppCode.Game.TransparentGeometry.MyTransparentGeometry.AddPointBillboard( TransparentGeometry.MyTransparentMaterialEnum.DecalGlare, color * alpha, position, 1.5f * emisivity, 0); if (decalTriangle.Light != null) { decalTriangle.Light.Color = color; decalTriangle.Light.SetPosition(position); float range = Math.Max(3 * emisivity * alpha, 0.1f); decalTriangle.Light.Range = range; } } } return emisivity; }