Example #1
0
        public void Generate(TextureParam _output)
        {
            int seed = (int)m_Seed;

            Random.InitState(seed);
            RenderTexture destination = CreateRenderDestination(null, _output);

            TriangleDraw.GPUStart(destination);
            float angle = 0;;

            Color prevCol = Color.white * (m_OuterBrightness + m_RandomizeOuterBrightness * Random.value);

            prevCol.a = 1;

            Vector3 prev = new Vector3(m_X + Mathf.Cos(angle + m_Angle * Mathf.Deg2Rad) * Radius, m_Y + Mathf.Sin(angle + m_Angle * Mathf.Deg2Rad) * Radius, 0);

            prev += new Vector3((Random.value - 0.5f) * m_RandomizeVertPos, (Random.value - 0.5f) * m_RandomizeVertPos, 0);

            Color midCol = Color.white * (m_InnerBrightness + m_RandomizeInnerBrightness * Random.value);

            midCol.a = 1;

            Vector3 mid = new Vector3(m_X, m_Y, 0);

            mid += new Vector3(Random.value * m_RandomizeVertPos, Random.value * m_RandomizeVertPos, 0);


            Vector3 firstPos = prev;
            Color   firstCol = prevCol;

            int sides = (int)m_Sides;

            float step  = Mathf.PI * 2 / sides;
            float count = 1;

            for (angle = step; angle < Mathf.PI * 2 - step * 0.5f; angle += step, count += 1.0f)
            {
                Vector3 pos = new Vector3(m_X + Mathf.Cos(angle + m_Angle * Mathf.Deg2Rad) * Radius, m_Y + Mathf.Sin(angle + m_Angle * Mathf.Deg2Rad) * Radius, 0);
                pos += new Vector3((Random.value - 0.5f) * m_RandomizeVertPos, (Random.value - 0.5f) * m_RandomizeVertPos, 0);

                Color col = Color.white * (m_OuterBrightness + m_RandomizeOuterBrightness * Random.value + m_IncrementBrightnessPerVert * count);
                col.a = 1;

                TriangleDraw.AddVertsForTri(
                    prev,
                    mid,
                    pos,
                    prevCol, midCol, col);
                prev    = pos;
                prevCol = col;
            }

            TriangleDraw.AddVertsForTri(
                prev,
                mid,
                firstPos,
                prevCol, midCol, firstCol);

            TriangleDraw.GPUEnd();
        }
Example #2
0
        public void Generate(TextureParam _output)
        {
            int seed = (int)m_MaterialIndex;

            Random.InitState(seed);
            RenderTexture destination = CreateRenderDestination(null, _output);

            TriangleDraw.GPUStart(destination, GL.LINES);
            List <Vector2> uvs   = new List <Vector2>();
            List <Vector3> norms = new List <Vector3>();
            List <Vector3> pos   = new List <Vector3>();

            if ((int)m_MaterialIndex.m_Value >= m_Mesh.subMeshCount)
            {
                m_MaterialIndex.Set(m_Mesh.subMeshCount - 1);
            }
            int[] indicies = m_Mesh.GetIndices((int)m_MaterialIndex.m_Value);

            m_Mesh.GetUVs(0, uvs);
            m_Mesh.GetNormals(norms);
            m_Mesh.GetVertices(pos);
            Vector3[] averageTri = new Vector3[indicies.Length / 3];
            Dictionary <double, Int64> edgeToTri    = new Dictionary <double, long>();
            Dictionary <Int64, int>    TriTriToEdge = new Dictionary <Int64, int>();

            for (int i = 0; i < indicies.Length; i += 3)
            {
                int     i0 = indicies[i];
                int     i1 = indicies[i + 1];
                int     i2 = indicies[i + 2];
                Vector3 n0 = norms[i0];
                Vector3 n1 = norms[i1];
                Vector3 n2 = norms[i2];

                Vector3 p0 = pos[i0];
                Vector3 p1 = pos[i1];
                Vector3 p2 = pos[i2];

                Vector3 average = (n0 + n1 + n2) * .3333333333f;

                averageTri[i / 3] = average;

                double key         = GetEdgeKey(i0, i1);
                long   triIndex    = (i / 3) + 1;
                long   existingTri = 0;
                edgeToTri.TryGetValue(key, out existingTri);
                Debug.Log(" key " + key + " ia " + i0 + " ib " + i1 + " tri: " + (i / 3) + "existing from dict " + existingTri);

                if (edgeToTri.ContainsKey(key))
                {
                    Int64 tris = edgeToTri[key];
                    if (tris < (1 << 30))
                    {//allready done
                        tris |= triIndex << 32;

                        edgeToTri[key]     = tris;
                        TriTriToEdge[tris] = 0;
                    }
                    else
                    {
                        Debug.LogError("e0 third attempt at tri " + (i / 3));
                    }
                }
                else
                {
                    edgeToTri[key]         = triIndex;
                    TriTriToEdge[triIndex] = 0;
                }

                key = GetEdgeKey(i1, i2);
                edgeToTri.TryGetValue(key, out existingTri);

                Debug.Log(" key " + key + " ia " + i1 + " ib " + i2 + " tri: " + (i / 3) + "existing from dict " + existingTri);
                if (edgeToTri.ContainsKey(key))
                {
                    Int64 tris = edgeToTri[key];
                    if (tris < (1 << 30))
                    {//allready done
                        tris |= triIndex << 32;

                        edgeToTri[key]     = tris;
                        TriTriToEdge[tris] = 1;
                    }
                    else
                    {
                        Debug.LogError("e1 third attempt at tri " + (i / 3));
                    }
                }
                else
                {
                    edgeToTri[key]         = triIndex;
                    TriTriToEdge[triIndex] = 1;
                }
                key = GetEdgeKey(i2, i0);
                edgeToTri.TryGetValue(key, out existingTri);

                Debug.Log(" key " + key + " ia " + i2 + " ib " + i0 + " tri: " + (i / 3) + "existing from dict " + existingTri);
                if (edgeToTri.ContainsKey(key))
                {
                    Int64 tris = edgeToTri[key];
                    if (tris < (1 << 30))
                    {//allready done
                        tris |= triIndex << 32;

                        edgeToTri[key]     = tris;
                        TriTriToEdge[tris] = 2;
                    }
                    else
                    {
                        Debug.LogError("e2 third attempt at tri " + (i / 3));
                    }
                }
                else
                {
                    edgeToTri[key]         = triIndex;
                    TriTriToEdge[triIndex] = 2;
                }
            }


            TriangleDraw.AddCol(Color.white);
            foreach (double key in edgeToTri.Keys)
            {
                long index    = edgeToTri[key];
                int  left     = (int)(index & 0xfffffff) - 1;
                int  right    = (int)((index >> 32) & 0xfffffffff) - 1;
                bool drawEdge = false;
                if (right < 0)
                {
                    drawEdge = false;
                    int     offset = TriTriToEdge[index];
                    int     v0     = left * 3 + offset;
                    int     v1     = left * 3 + ((offset + 1) % 3);
                    Vector3 uv0    = uvs[indicies[v0]];
                    Vector3 uv1    = uvs[indicies[v1]];
                    TriangleDraw.AddVert(new Vector3(uv0.x, uv0.y, 0));
                    TriangleDraw.AddVert(new Vector3(uv1.x, uv1.y, 0));
                }
                else
                {
                    float dot = Vector3.Dot(averageTri[left], averageTri[right]);
                    if (dot < m_EdgeAngleDif && right >= 0)
                    {
                        drawEdge = true;
                    }
                }
                if (drawEdge)
                {
                    int     offset = TriTriToEdge[index];
                    int     v0     = right * 3 + offset;
                    int     v1     = right * 3 + (offset + 1) % 3;
                    Vector3 uv0    = uvs[indicies[v0]];
                    Vector3 uv1    = uvs[indicies[v1]];
                    TriangleDraw.AddVert(new Vector3(uv0.x, uv0.y, 0));
                    TriangleDraw.AddVert(new Vector3(uv1.x, uv1.y, 0));
                }
            }

/*
 *          for (int i = 0; i < indicies.Length; i +=3)
 *          {
 *              Vector3 uv0 = uvs[indicies[i]];
 *              Vector3 uv1 = uvs[indicies[i+1]];
 *              Vector3 uv2 = uvs[indicies[i+2]];
 *              TriangleDraw.AddVert(new Vector3(uv0.x,uv0.y,0));
 *              TriangleDraw.AddVert(new Vector3(uv1.x, uv1.y, 0));
 *
 *              TriangleDraw.AddVert(new Vector3(uv1.x, uv1.y, 0));
 *              TriangleDraw.AddVert(new Vector3(uv2.x, uv2.y, 0));
 *
 *              TriangleDraw.AddVert(new Vector3(uv2.x, uv2.y, 0));
 *              TriangleDraw.AddVert(new Vector3(uv0.x, uv0.y, 0));
 *          }
 */
            TriangleDraw.GPUEnd();
        }