Exemple #1
0
 public void SetColor(Vector4 color)
 {
     for (int i = 0; i < mesh.vertices.Length; ++i)
     {
         MeshAttr.SetColor(ref mesh.vertices[i], ref color);
     }
 }
Exemple #2
0
        /// <summary>
        /// Adds a triangle based on the current mesh with the vertex format Position, Normal, UV and Color.
        /// It sets the normal for each vertex to the original position vector,
        /// UV is set to top center, bottom left, bottom right, with original vert being top center
        /// It sets color of all verts to the color of the original vertex.
        /// </summary>
        /// <typeparam name="AltVertex"></typeparam>
        /// <param name="newVerts">The new set of vertices</param>
        /// <param name="newIndices">The new set of indices</param>
        /// <param name="v1index">The index of the vertex in the original mesh</param>
        /// <param name="v2">the position of the first new vertex</param>
        /// <param name="v3">the position of the second new vertex</param>
        /// <param name="color">The new vertex color</param>"
        public void AddTriangle2 <AltVertex>(ref List <AltVertex> newVerts, ref List <uint> newIndices, int v1index, ref Vector3 v2, ref Vector3 v3, Vector4 color)
            where AltVertex : struct, IVertex
        {
            AltVertex[] triVerts = new AltVertex[3];
            triVerts.Initialize();

            Vector3 v1Pos = MeshAttr.GetPosition(ref mesh.vertices[v1index]);

            MeshAttr.SetPosition(ref triVerts[0], ref v1Pos);
            MeshAttr.SetPosition(ref triVerts[1], ref v2);
            MeshAttr.SetPosition(ref triVerts[2], ref v3);

            v1Pos.Normalize();
            for (int i = 0; i < 3; i++)
            {
                MeshAttr.SetNormal(ref triVerts[i], ref v1Pos);
            }

            Vector2 uv0 = new Vector2(0.5f, 1);
            Vector2 uv1 = new Vector2(0, 0);
            Vector2 uv2 = new Vector2(1, 0);

            MeshAttr.SetUV(ref triVerts[0], ref uv0);
            MeshAttr.SetUV(ref triVerts[1], ref uv1);
            MeshAttr.SetUV(ref triVerts[2], ref uv2);

            MeshAttr.SetColor(ref triVerts[0], ref color);
            MeshAttr.SetColor(ref triVerts[1], ref color);
            MeshAttr.SetColor(ref triVerts[2], ref color);

            newVerts[v1index] = triVerts[0];
            int index = newVerts.Count;

            newVerts.Add(triVerts[1]);
            newVerts.Add(triVerts[2]);

            newIndices.Add((uint)v1index);
            newIndices.Add((uint)index++);
            newIndices.Add((uint)index++);
        }
Exemple #3
0
        public Geometry <AltVertex> GenerateBorderGeometry <AltVertex>()
            where AltVertex : struct, IVertex
        {
            if (borders == null || borders.Count == 0)
            {
                return(null);
            }
            else
            {
                List <AltVertex> vertices   = new List <AltVertex>();
                List <uint>      newIndices = new List <uint>();

                const float h = 0.1f;

                foreach (var iter in borders)
                {
                    Int64  borderKey = iter.Key;
                    int    v1index   = (int)(borderKey & 0xffffffff);
                    int    v2index   = (int)((borderKey >> 32) & 0xffffffff);
                    Border border    = iter.Value;

                    // The border lies along an edge in the dual geometry.
                    int     plateIndex1 = border.plate0;
                    int     plateIndex2 = border.plate1;
                    Vector3 v1Pos       = geometry.Mesh.GetPosition(v1index);
                    Vector3 v2Pos       = geometry.Mesh.GetPosition(v2index);
                    Vector3 centroid1   = geometry.Topology.Centroids[border.c1Index].position;
                    Vector3 centroid2   = geometry.Topology.Centroids[border.c2Index].position;

                    Vector3 v1g1prime = Math2.BaseProjection(v1Pos, centroid1, centroid2, h);
                    Vector3 v1g2prime = Math2.BaseProjection(v1Pos, centroid2, centroid1, h);
                    Vector3 v2g1prime = Math2.BaseProjection(v2Pos, centroid1, centroid2, h);
                    Vector3 v2g2prime = Math2.BaseProjection(v2Pos, centroid2, centroid1, h);

                    centroid1 *= 1.01f; // Project the centroids out of the sphere slightly
                    centroid2 *= 1.01f;

                    bool edgeOrderIsAnticlockwise = false;
                    for (int i = 0; i < 3; i++)
                    {
                        if (geometry.Indices[border.c1Index * 3 + i] == v1index)
                        {
                            if (geometry.Indices[border.c1Index * 3 + (i + 1) % 3] == v2index)
                            {
                                edgeOrderIsAnticlockwise = true;
                            }
                            break;
                        }
                    }

                    int index = vertices.Count;
                    geometry.AddTriangle(ref vertices, ref newIndices, ref v1g2prime, ref centroid2, ref centroid1, edgeOrderIsAnticlockwise);
                    geometry.AddTriangle(ref vertices, ref newIndices, ref v1g2prime, ref centroid1, ref v1g1prime, edgeOrderIsAnticlockwise);
                    geometry.AddTriangle(ref vertices, ref newIndices, ref v2g1prime, ref centroid1, ref centroid2, edgeOrderIsAnticlockwise);
                    geometry.AddTriangle(ref vertices, ref newIndices, ref v2g1prime, ref centroid2, ref v2g2prime, edgeOrderIsAnticlockwise);

                    float     p1      = Math2.Clamp(Math.Abs(borderCorners[border.c1Index].stress.pressure) * 1000.0f, 0, 1.0f);
                    float     p2      = Math2.Clamp(Math.Abs(borderCorners[border.c2Index].stress.pressure) * 1000.0f, 0, 1.0f);
                    float     hue1    = borderCorners[border.c1Index].stress.pressure > 0 ? 0 : 0.5f;
                    float     hue2    = borderCorners[border.c2Index].stress.pressure > 0 ? 0 : 0.5f;
                    Vector3   rgb1    = Math2.HSV2RGB(new Vector3(hue1, p1, 1.0f - p1));
                    Vector3   rgb2    = Math2.HSV2RGB(new Vector3(hue2, p2, 1.0f - p2));
                    Vector4   c1Color = new Vector4(rgb1.X, rgb1.Y, rgb1.Z, 1);
                    Vector4   c2Color = new Vector4(rgb2.X, rgb2.Y, rgb2.Z, 1);
                    AltVertex v;
                    for (int i = 0; i < 12; ++i)
                    {
                        Vector4 color = new Vector4(1.0f, 1.0f, 1.0f, 1.0f);
                        if (i == 0 || i == 1 || i == 3 || i == 8 || i == 10 || i == 11)
                        {
                            color = c2Color;
                        }
                        else if (i == 2 || i == 4 || i == 5 || i == 6 || i == 7 || i == 9)
                        {
                            color = c1Color;
                        }
                        v = vertices[index + i]; MeshAttr.SetColor(ref v, ref color); vertices[index + i] = v;
                    }
                }
                Mesh <AltVertex> newMesh = new Mesh <AltVertex>(vertices.ToArray());
                return(new Geometry <AltVertex>(newMesh, newIndices.ToArray()));
            }
        }
Exemple #4
0
 public void SetColor(int index, ref Vector4 color)
 {
     MeshAttr.SetColor(ref vertices[index], ref color);
 }