/// <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; } }
// 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 ((MyRender.RenderTimeInMS - 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 triangle = m_trianglesQueue.Dequeue(); triangle.Close(); m_freeTriangles.Push(triangle); } 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 = (int)MyRender.RenderTimeInMS; } } }
// 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) { RenderObject = null; } }
// 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) { VoxelCell = null; } }
public void Add(MyDecalTriangle_Data triangle, int remainingTrianglesOfThisDecal, Vector3D position, float lightSize, 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 = (float)MyRender.RenderTimeInMS; decalTriangle.Position = position; // We must repack vertex positions before copying to new triange, to avoid Z-fight. decalTriangle.Position0 = triangle.Vertexes.Vertex0; decalTriangle.Position1 = triangle.Vertexes.Vertex1; decalTriangle.Position2 = triangle.Vertexes.Vertex2; // Texture coords decalTriangle.TexCoord0 = triangle.TexCoords.TexCoord0; decalTriangle.TexCoord1 = triangle.TexCoords.TexCoord1; decalTriangle.TexCoord2 = triangle.TexCoords.TexCoord2; // Alpha decalTriangle.Color0 = triangle.Colors.Color0; decalTriangle.Color1 = triangle.Colors.Color1; decalTriangle.Color2 = triangle.Colors.Color2; //AdjustAlphaByDistance(normal, decalSize, position, decalTriangle); // Bump mapping decalTriangle.Normal0 = triangle.Normals.Normal0; decalTriangle.Normal1 = triangle.Normals.Normal1; decalTriangle.Normal2 = triangle.Normals.Normal2; /* * decalTriangle.Tangent0 = triangle.Tangents.Normal0; * decalTriangle.Tangent1 = triangle.Tangents.Normal1; * decalTriangle.Tangent2 = triangle.Tangents.Normal2; */ decalTriangle.RemainingTrianglesOfThisDecal = remainingTrianglesOfThisDecal; m_trianglesQueue.Enqueue(decalTriangle); }
public void Add(MyDecalTriangle_Data triangle, int remainingTrianglesOfThisDecal, Vector3D position, float lightSize, float emissivity) { MyDecalTriangle decalTriangle = m_freeTriangles.Pop(); decalTriangle.Start(lightSize); decalTriangle.Emissivity = emissivity; //decalTriangle.RandomOffset = MyMwcUtils.GetRandomFloat(0.0f, MathHelper.Pi); decalTriangle.RandomOffset = MyRender.RenderTimeInMS; decalTriangle.Position = position; // We must repack vertex positions before copying to new triange, to avoid Z-fight. decalTriangle.Position0 = triangle.Vertexes.Vertex0; decalTriangle.Position1 = triangle.Vertexes.Vertex1; decalTriangle.Position2 = triangle.Vertexes.Vertex2; // Texture coords decalTriangle.TexCoord0 = triangle.TexCoords.TexCoord0; decalTriangle.TexCoord1 = triangle.TexCoords.TexCoord1; decalTriangle.TexCoord2 = triangle.TexCoords.TexCoord2; // Alpha decalTriangle.Color0 = triangle.Colors.Color0; decalTriangle.Color1 = triangle.Colors.Color1; decalTriangle.Color2 = triangle.Colors.Color2; // Bump mapping decalTriangle.Normal0 = triangle.Normals.Normal0; decalTriangle.Normal1 = triangle.Normals.Normal1; decalTriangle.Normal2 = triangle.Normals.Normal2; /* * decalTriangle.Tangent0 = triangle.Tangents.Normal0; * decalTriangle.Tangent1 = triangle.Tangents.Normal1; * decalTriangle.Tangent2 = triangle.Tangents.Normal2; */ decalTriangle.Draw = true; decalTriangle.RemainingTrianglesOfThisDecal = remainingTrianglesOfThisDecal; m_trianglesQueue.Enqueue(decalTriangle); }
public static float UpdateDecalEmissivity(MyDecalTriangle decalTriangle, float alpha, MyRenderObject renderObject) { float emisivity = 0; if (decalTriangle.Emissivity > 0) { //emisivity = (float)(Math.Sin(decalTriangle.RandomOffset + decalTriangle.RandomOffset * MySandboxGame.TotalGamePlayTimeInMilliseconds / 1000.0f)) * 0.4f + 0.7f; // 2 seconds default, more emissive lit longer float stableLength = 2000 * decalTriangle.Emissivity; if ((MyRender.RenderTimeInMS - decalTriangle.RandomOffset) < stableLength) emisivity = 1; else { emisivity = (float)(500 - (MyRender.RenderTimeInMS - stableLength - decalTriangle.RandomOffset)) / 500.0f; if (emisivity < 0) emisivity = 0; } emisivity *= decalTriangle.Emissivity; if (emisivity > 0) { Color color = MyDecalsConstants.PROJECTILE_DECAL_COLOR; Vector3D position; MyRenderTransformObject transformObject = renderObject as MyRenderTransformObject; if (transformObject != null) { position = Vector3D.Transform(decalTriangle.Position, transformObject.WorldMatrix); } else if (renderObject is MyManualCullableRenderObject) { position = Vector3D.Transform(decalTriangle.Position, (renderObject as MyManualCullableRenderObject).WorldMatrix); } else { position = decalTriangle.Position; } MyTransparentGeometry.AddPointBillboard( "DecalGlare", color * alpha, (Vector3)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 static float UpdateDecalEmissivity(MyDecalTriangle decalTriangle, float alpha, MyRenderObject renderObject) { float emisivity = 0; if (decalTriangle.Emissivity > 0) { //emisivity = (float)(Math.Sin(decalTriangle.RandomOffset + decalTriangle.RandomOffset * MySandboxGame.TotalGamePlayTimeInMilliseconds / 1000.0f)) * 0.4f + 0.7f; // 2 seconds default, more emissive lit longer float stableLength = 2000 * decalTriangle.Emissivity; if ((MyRender.RenderTimeInMS - decalTriangle.RandomOffset) < stableLength) { emisivity = 1; } else { emisivity = (float)(500 - (MyRender.RenderTimeInMS - stableLength - decalTriangle.RandomOffset)) / 500.0f; if (emisivity < 0) { emisivity = 0; } } emisivity *= decalTriangle.Emissivity; if (emisivity > 0) { Color color = MyDecalsConstants.PROJECTILE_DECAL_COLOR; Vector3D position; MyRenderTransformObject transformObject = renderObject as MyRenderTransformObject; if (transformObject != null) { position = Vector3D.Transform(decalTriangle.Position, transformObject.WorldMatrix); } else if (renderObject is MyManualCullableRenderObject) { position = Vector3D.Transform(decalTriangle.Position, (renderObject as MyManualCullableRenderObject).WorldMatrix); } else { position = decalTriangle.Position; } MyTransparentGeometry.AddPointBillboard( "DecalGlare", color * alpha, (Vector3)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); }