public void EvaluateNormals(OutputMesh mesh, CPNGuide guide)
        {
            int N = guide.GetN();

            if (N <= 0)
            {
                return;
            }

            bool doTangents = mesh.DoUseTangents();
            bool doNormals  = mesh.DoNormals();

            if (doNormals)
            {
                float step = 1.0f / N;
                for (int j = 1; j < N; j++)
                {
                    EvalAt(j * step, guide);
                    Vector3 dev;
                    Vector3 normal = EvalNormal(guide, out dev);
                    int     index  = guide.GetIndex(j);
                    mesh.SetNormal(index, normal.normalized);
                    if (doTangents)
                    {
                        mesh.SetTangent(index, GetTangent(guide, dev, normal).normalized);
                    }
                }
                if (doTangents)
                {
                    for (int j = 0; j <= N; j += N)
                    {
                        EvalAt(j * step, guide);
                        Vector3 dev    = EvalDev(guide);
                        int     index  = guide.GetIndex(j);
                        Vector3 normal = mesh.GetNormal(index);
                        mesh.SetTangent(index, GetTangent(guide, dev, normal).normalized);
                    }
                }
            }
        }
Beispiel #2
0
        int WriteVertices(OutputMesh mesh)
        {
            int numberOfVertices = subSet == null?curvedPolygonsNet.GetNumberOfVertices() :
                                       subSet.vertices.Length;

            Vector3[]   vertices        = curvedPolygonsNet.GetVertices();
            Vector3[]   normals         = curvedPolygonsNet.GetNormals();
            Vector3[]   uvs             = curvedPolygonsNet.GetUv();
            Vector3[][] properties      = curvedPolygonsNet.GetProperties3();
            int         countProperties = mesh.CountProperties();
            //Vector3[] tangents = curvedPolygonsNet.GetTangents();
            //bool doTangents = tangents != null;
            bool doUvs     = uvs != null && mesh.DoUseUVs();
            bool doNormals = normals != null && mesh.DoNormals();

            for (int i = 0; i < numberOfVertices; i++)
            {
                int index = subSet == null ? i : subSet.vertices[i];
                mesh.SetVertex(index, vertices[index]);
                if (doUvs)
                {
                    mesh.SetUV(index, uvs[index]);
                }
                if (doNormals)
                {
                    mesh.SetNormal(index, normals[index]);
                }
                for (int k = 0; k < countProperties; k++)
                {
                    mesh.SetProperty3(index, k, properties[k][index]);
                }
            }


            return(numberOfVertices);
        }
        public void UdpdateContent(OutputMesh mesh, CPNPolygon polygon, int internalsIndex,
                                   int facesIndex, bool doUpdateStructure = true)
        {
            triangleStructure.RetrieveInfos(polygon);

            bool useUV           = mesh.DoUseUVs();
            bool useNormals      = mesh.DoNormals();
            bool useTangents     = mesh.DoUseUVs();
            int  countProperties = mesh.CountProperties();

            buffer0.requestProperties(countProperties);
            buffer1.requestProperties(countProperties);
            buffer2.requestProperties(countProperties);

            int   M    = triangleStructure.GetM();
            float step = 1.0f / M;

            CPNSideEdge[] polylines = polygon.sideEdges;
            buffer0.writeWithGuide(polylines[0], M, mesh, evaluator);
            buffer1.writeWithGuide(polylines[1], M, mesh, evaluator);
            buffer2.writeWithGuide(polylines[2], M, mesh, evaluator);

            edgeSurface0.Set(buffer0, buffer2, buffer1);
            edgeSurface1.Set(buffer1, buffer0, buffer2);
            edgeSurface2.Set(buffer2, buffer1, buffer0);

            computeCorners();

            float l1 = ks[0] * ks[1] * ks[0] * ks[1];
            float l2 = ks[1] * ks[2] * ks[1] * ks[2];
            float l3 = ks[2] * ks[0] * ks[2] * ks[0];

            prepareMemory(M, countProperties);

            int position = internalsIndex;

            for (int i = 1; i < M - 1; i++)
            {
                for (int j = 1; j < M - 1 - (i - 1); j++)
                {
                    int k = M - i - j;

                    float U = j * step;
                    float V = i * step;
                    float W = 1 - U - V;

                    float a1 = U * U * W * W * l1 /** buffer0.thickness*/;
                    float a2 = V * V * U * U * l2 /** buffer1.thickness*/;
                    float a3 = W * W * V * V * l3 /** buffer2.thickness*/;

                    //float a1 = W * W;
                    //float a2 = U * U;
                    //float a3 = V * V;

                    if (TriangleInterpolator4.interpolationCorner != 0)
                    {
                        switch (TriangleInterpolator4.interpolationCorner)
                        {
                        case 1: a1 = 1; a2 = 0; a3 = 0; break;

                        case 2: a1 = 0; a2 = 1; a3 = 0; break;

                        case 3: a1 = 0; a2 = 0; a3 = 1; break;
                        }
                    }

                    float rec = 1.0f / (a1 + a2 + a3);
                    a1 *= rec;
                    a2 *= rec;
                    a3 *= rec;


                    Vector3 V1     = edgeSurface0.evalVertex(/*j,*/ i, polylines[0], U / (1 - V));
                    Vector3 V2     = edgeSurface1.evalVertex(/*i,*/ k, polylines[1], V / (1 - W));
                    Vector3 V3     = edgeSurface2.evalVertex(/*k,*/ j, polylines[2], W / (1 - U));
                    Vector3 vertex = V1 * a1 + V2 * a2 + V3 * a3;

                    int memoryIndex = j + i * (M + 1) - (((i) * (i - 1)) >> 1);
                    memory.vertices[memoryIndex] = vertex;

                    if (useUV)
                    {
                        Vector3 V1uv = edgeSurface0.evalUV(/*j,*/ i, polylines[0], U / (1 - V));
                        Vector3 V2uv = edgeSurface1.evalUV(/*i,*/ k, polylines[1], V / (1 - W));
                        Vector3 V3uv = edgeSurface2.evalUV(/*k,*/ j, polylines[2], W / (1 - U));
                        Vector3 uv   = V1uv * a1 + V2uv * a2 + V3uv * a3;
                        //Vector3 vertex = V3;
                        //Vector3 uv = V3uv;

                        memory.uv[memoryIndex] = uv;
                    }

                    for (int pIndex = 0; pIndex < countProperties; pIndex++)
                    {
                        Vector3 V1prop = edgeSurface0.evalProperty(pIndex, i, polylines[0], U / (1 - V));
                        Vector3 V2prop = edgeSurface1.evalProperty(pIndex, k, polylines[1], V / (1 - W));
                        Vector3 V3prop = edgeSurface2.evalProperty(pIndex, j, polylines[2], W / (1 - U));
                        Vector3 prop   = V1prop * a1 + V2prop * a2 + V3prop * a3;
                        mesh.SetProperty3(position, pIndex, prop);
                    }

                    position++;
                }
            }

            position = internalsIndex;

            for (int i = 1; i < M - 1; i++)
            {
                for (int j = 1; j < M - 1 - (i - 1); j++)
                {
                    int rowIndex     = i * (M + 1) - (((i) * (i - 1)) >> 1);
                    int rowIndexPrev = (i - 1) * (M + 1) - (((i - 1) * (i - 2)) >> 1);
                    int rowIndexNext = (i + 1) * (M + 1) - (((i + 1) * (i)) >> 1);

                    int     memoryIndex = j + rowIndex;
                    Vector3 vertex      = memory.vertices[memoryIndex];
                    Vector3 uv          = memory.uv[memoryIndex];

                    Vector3 normal  = Vector3.zero;
                    Vector3 tangent = Vector3.zero;

                    if (useNormals)
                    {
                        //Normal (S is the vertices, the surface)
                        Vector3 dSdu = memory.vertices[memoryIndex + 1] - memory.vertices[memoryIndex - 1];
                        Vector3 dSdv = memory.vertices[rowIndexNext + j] - memory.vertices[rowIndexPrev + j];
                        normal = Vector3.Cross(dSdu, dSdv).normalized;

                        if (useTangents)
                        {
                            //Tangent
                            Vector3 dTxdu = memory.uv[memoryIndex + 1] - memory.uv[memoryIndex - 1];
                            Vector3 dTxdv = memory.uv[rowIndexNext + j] - memory.uv[rowIndexPrev + j];
                            tangent = getTangent(dSdu, dSdv, dTxdu, dTxdv);
                        }
                    }

                    mesh.SetPNUV(position, vertex, normal, uv, tangent);

                    position++;
                }
            }

            if (doUpdateStructure)
            {
                triangleStructure.CreateTriangleTessellation(mesh, internalsIndex, facesIndex, polygon);
            }
        }
        public void UdpdateContent(OutputMesh mesh, CPNPolygon polygon, int internalsIndex,
                                   int facesIndex, bool doUpdateStructure = true)
        {
            triangleStructure.RetrieveInfos(polygon);
            bool useUV           = mesh.DoUseUVs();
            bool useNormals      = mesh.DoNormals();
            bool useTangents     = mesh.DoUseUVs();
            int  countProperties = mesh.CountProperties();

            buffer0.requestProperties(countProperties);
            buffer1.requestProperties(countProperties);
            buffer2.requestProperties(countProperties);

            int   M    = triangleStructure.GetM();
            float step = 1.0f / M;

            CPNSideEdge[] polylines = polygon.sideEdges;
            buffer0.writeWithGuide(polylines[0], M, mesh, evaluator);
            buffer1.writeWithGuide(polylines[1], M, mesh, evaluator);
            buffer2.writeWithGuide(polylines[2], M, mesh, evaluator);

            corner0.Set(buffer0, buffer2);
            corner1.Set(buffer1, buffer0);
            corner2.Set(buffer2, buffer1);

            int position = internalsIndex;

            for (int i = 1; i < M - 1; i++)
            {
                for (int j = 1; j < M - 1 - (i - 1); j++)
                {
                    int wIndex = M - i - j;

                    float U = j * step;
                    float V = i * step;
                    float W = 1 - U - V;

                    float a1 = W * W;
                    float a2 = U * U;
                    float a3 = V * V;

                    float rec = 1.0f / (a1 + a2 + a3);
                    a1 *= rec;
                    a2 *= rec;
                    a3 *= rec;


                    Vector3 V1     = corner0.evalVertex(j, i);
                    Vector3 V2     = corner1.evalVertex(i, wIndex);
                    Vector3 V3     = corner2.evalVertex(wIndex, j);
                    Vector3 vertex = V1 * a1 + V2 * a2 + V3 * a3;

                    Vector3 normal  = Vector3.zero;
                    Vector3 uv      = Vector3.zero;
                    Vector3 tangent = Vector3.zero;

                    if (useNormals)
                    {
                        Vector3 V1N = corner0.evalNormal(j, i);
                        Vector3 V2N = corner1.evalNormal(i, wIndex);
                        Vector3 V3N = corner2.evalNormal(wIndex, j);
                        normal = V1N * a1 + V2N * a2 + V3N * a3;
                    }

                    if (useUV)
                    {
                        Vector3 V1uv = corner0.evalUV(j, i);
                        Vector3 V2uv = corner1.evalUV(i, wIndex);
                        Vector3 V3uv = corner2.evalUV(wIndex, j);
                        uv = V1uv * a1 + V2uv * a2 + V3uv * a3;

                        if (useTangents)
                        {
                            float Uu  = U + 0.001f;
                            float Wu  = 1 - Uu - V;
                            float a1u = Wu * Wu;
                            float a2u = Uu * Uu;
                            float a3u = V * V;
                            a1u *= rec; a2u *= rec; a3u *= rec;
                            float Vv  = V + 0.001f;
                            float Wv  = 1 - U - Vv;
                            float a1v = Wv * Wv;
                            float a2v = U * U;
                            float a3v = Vv * Vv;
                            a1v *= rec; a2v *= rec; a3v *= rec;
                            Vector3 DPu  = (a1u * V1 + a2u * V2 + a3u * V3) - vertex;
                            Vector3 DPv  = (a1v * V1 + a2v * V2 + a3v * V3) - vertex;
                            Vector3 DUVu = (a1u * V1uv + a2u * V2uv + a3u * V3uv) - vertex;
                            Vector3 DUVv = (a1v * V1uv + a2v * V2uv + a3v * V3uv) - vertex;
                            tangent = getTangent(DPu, DPv, DUVu, DUVv);
                        }
                    }

                    for (int k = 0; k < countProperties; k++)
                    {
                        Vector3 V1propK = corner0.evalUV(j, i);
                        Vector3 V2propK = corner1.evalUV(i, wIndex);
                        Vector3 V3propK = corner2.evalUV(wIndex, j);
                        Vector3 propK   = a1 * V1propK + a2 * V2propK + a3 * V3propK;
                        mesh.SetProperty3(position, k, propK);
                    }

                    mesh.SetPNUV(position, vertex, normal, uv, tangent);
                    position++;
                }
            }


            if (doUpdateStructure)
            {
                triangleStructure.CreateTriangleTessellation(mesh, internalsIndex, facesIndex, polygon);
            }
        }
Beispiel #5
0
        public void UdpdateContent(OutputMesh mesh, CPNPolygon polygon, int internalsIndex,
                                   int facesIndex, bool doUpdateStructure = true)
        {
            quadStructure.RetrieveInfos(polygon);

            bool useUV           = mesh.DoUseUVs();
            bool useNormals      = mesh.DoNormals();
            bool useTangents     = mesh.DoUseUVs();
            int  countProperties = mesh.CountProperties();

            buffer0.requestProperties(countProperties);
            buffer1.requestProperties(countProperties);
            buffer2.requestProperties(countProperties);

            int   MV    = quadStructure.GetMV();
            int   MH    = quadStructure.GetMH();
            float stepV = 1.0f / MV;
            float stepH = 1.0f / MH;

            CPNSideEdge[] polylines = polygon.sideEdges;
            buffer0.writeWithGuide(polylines[0], MH, mesh, evaluator);
            buffer1.writeWithGuide(polylines[1], MV, mesh, evaluator);
            buffer2.writeWithGuide(polylines[2], MH, mesh, evaluator);
            buffer3.writeWithGuide(polylines[3], MV, mesh, evaluator);

            corner0.Set(buffer0, buffer3);
            corner1.Set(buffer1, buffer0);
            corner2.Set(buffer2, buffer1);
            corner3.Set(buffer3, buffer2);

            prepareMemory(MH, MV);

            int index = internalsIndex;

            for (int i = 1; i < MV; i++)
            {
                for (int j = 1; j < MH; j++)
                {
                    float U = (j) * stepH;
                    float V = (i) * stepV;

                    float UM = 1 - U;
                    float VM = 1 - V;

                    float a1 = (UM * VM * UM * VM);
                    float a2 = (U * VM * U * VM);
                    float a3 = (U * V * U * V);
                    float a4 = (UM * V * UM * V);

                    float rec = 1.0f / (a1 + a2 + a3 + a4);
                    a1 *= rec;
                    a2 *= rec;
                    a3 *= rec;
                    a4 *= rec;

                    Vector3 V1 = corner0.evalVertex(j, i);
                    Vector3 V2 = corner1.evalVertex(i, MH - j);
                    Vector3 V3 = corner2.evalVertex(MH - j, MV - i);
                    Vector3 V4 = corner3.evalVertex(MV - i, j);


                    Vector3 vertex = a1 * V1 + a2 * V2 + a3 * V3 + a4 * V4;

                    int memoryIndex = j + i * (MH + 1);
                    memory.vertices[memoryIndex] = vertex;

                    if (useUV)
                    {
                        //Vector3 vertex = V3;
                        Vector3 V1uv = corner0.evalUV(j, i);
                        Vector3 V2uv = corner1.evalUV(i, MH - j);
                        Vector3 V3uv = corner2.evalUV(MH - j, MV - i);
                        Vector3 V4uv = corner3.evalUV(MV - i, j);
                        Vector3 uv   = a1 * V1uv + a2 * V2uv + a3 * V3uv + a4 * V4uv;
                        //Vector3 uv = V3uv;

                        memory.uv[memoryIndex] = uv;
                    }

                    for (int k = 0; k < countProperties; k++)
                    {
                        Vector3 V1propK = corner0.evalProperty(k, j, i);
                        Vector3 V2propK = corner1.evalProperty(k, i, MH - j);
                        Vector3 V3propK = corner2.evalProperty(k, MH - j, MV - i);
                        Vector3 V4propK = corner3.evalProperty(k, MV - i, j);
                        //Vector3 vertex = V3;
                        Vector3 propK = a1 * V1propK + a2 * V2propK + a3 * V3propK + a4 * V4propK;
                        //Vector3 uv = V3uv;
                        mesh.SetProperty3(index, k, propK);
                    }

                    index++;
                }
            }

            index = internalsIndex;
            for (int i = 1; i < MV; i++)
            {
                for (int j = 1; j < MH; j++)
                {
                    int rowIndex     = i * (MH + 1);
                    int rowIndexPrev = (i - 1) * (MH + 1);
                    int rowIndexNext = (i + 1) * (MH + 1);

                    int     memoryIndex = j + rowIndex;
                    Vector3 vertex      = memory.vertices[memoryIndex];
                    Vector3 uv          = memory.uv[memoryIndex];

                    Vector3 normal  = Vector3.zero;
                    Vector3 tangent = Vector3.zero;

                    if (useNormals)
                    {
                        //Normal (S is the vertices, the surface)
                        Vector3 dSdu = memory.vertices[memoryIndex + 1] - memory.vertices[memoryIndex - 1];
                        Vector3 dSdv = memory.vertices[rowIndexNext + j] - memory.vertices[rowIndexPrev + j];
                        dSdu   = dSdu.normalized;
                        dSdv   = dSdv.normalized;
                        normal = Vector3.Cross(dSdu, dSdv).normalized;

                        if (useTangents)
                        {
                            //Debug.Log("dSdu: (" + dSdu.x + "," + dSdu.y + "," + dSdu.z + ") dSdv:"
                            //    + dSdv.x + "," + dSdv.y + "," + dSdv.z + ") Vector3.Cross(dSdu, dSdv).magnitude:" + Vector3.Cross(dSdu, dSdv).magnitude);
                            //Debug.Log("Normal (" + normal.x + "," + normal.y + "," + normal.z + ")");

                            //Tangent
                            Vector3 dTxdu = memory.uv[memoryIndex + 1] - memory.uv[memoryIndex - 1];
                            Vector3 dTxdv = memory.uv[rowIndexNext + j] - memory.uv[rowIndexPrev + j];
                            tangent = getTangent(dSdu, dSdv, dTxdu, dTxdv);
                        }
                    }
                    mesh.SetPNUV(index, vertex, normal, uv, tangent);

                    index++;
                }
            }

            if (doUpdateStructure)
            {
                quadStructure.CreateQuadTessellation(mesh, internalsIndex, facesIndex, polygon);
            }
        }
        public void UdpdateContent(OutputMesh mesh, CPNPolygon polygon, int internalsIndex,
                                   int facesIndex, bool doUpdateStructure = true)
        {
            triangleStructure.RetrieveInfos(polygon);

            bool useUV           = mesh.DoUseUVs();
            bool useNormals      = mesh.DoNormals();
            bool useTangents     = mesh.DoUseUVs();
            int  countProperties = mesh.CountProperties();

            buffer0.requestProperties(countProperties);
            buffer1.requestProperties(countProperties);
            buffer2.requestProperties(countProperties);

            int   M    = triangleStructure.GetM();
            float step = 1.0f / M;

            CPNSideEdge[] polylines = polygon.sideEdges;
            buffer0.writeWithGuide(polylines[0], M, mesh, evaluator);
            buffer1.writeWithGuide(polylines[1], M, mesh, evaluator);
            buffer2.writeWithGuide(polylines[2], M, mesh, evaluator);

            corner0.Set(buffer0, buffer2);
            corner1.Set(buffer1, buffer0);
            corner2.Set(buffer2, buffer1);

            prepareMemory(M);

            int position = internalsIndex;

            for (int i = 1; i < M - 1; i++)
            {
                for (int j = 1; j < M - 1 - (i - 1); j++)
                {
                    int wIndex = M - i - j;

                    float U = j * step;
                    float V = i * step;
                    float W = 1 - U - V;

                    float a1 = W * W;
                    float a2 = U * U;
                    float a3 = V * V;

                    float rec = 1.0f / (a1 + a2 + a3);
                    a1 *= rec;
                    a2 *= rec;
                    a3 *= rec;

                    Vector3 V1          = corner0.evalVertex(j, i);
                    Vector3 V2          = corner1.evalVertex(i, wIndex);
                    Vector3 V3          = corner2.evalVertex(wIndex, j);
                    Vector3 vertex      = V1 * a1 + V2 * a2 + V3 * a3;
                    int     memoryIndex = j + i * (M + 1) - (((i) * (i - 1)) >> 1);
                    memory.vertices[memoryIndex] = vertex;

                    if (useUV)
                    {
                        Vector3 V1uv = corner0.evalUV(j, i);
                        Vector3 V2uv = corner1.evalUV(i, wIndex);
                        Vector3 V3uv = corner2.evalUV(wIndex, j);
                        Vector3 uv   = V1uv * a1 + V2uv * a2 + V3uv * a3;
                        memory.uv[memoryIndex] = uv;
                    }

                    for (int k = 0; k < countProperties; k++)
                    {
                        Vector3 V1prop = corner0.evalProperty(k, j, i);
                        Vector3 V2prop = corner1.evalProperty(k, i, wIndex);
                        Vector3 V3prop = corner2.evalProperty(k, wIndex, j);
                        Vector3 prop   = V1prop * a1 + V2prop * a2 + V3prop * a3;
                        mesh.SetProperty3(position, k, prop);
                    }

                    position++;
                }
            }

            position = internalsIndex;

            for (int i = 1; i < M - 1; i++)
            {
                for (int j = 1; j < M - 1 - (i - 1); j++)
                {
                    int rowIndex     = i * (M + 1) - (((i) * (i - 1)) >> 1);
                    int rowIndexPrev = (i - 1) * (M + 1) - (((i - 1) * (i - 2)) >> 1);
                    int rowIndexNext = (i + 1) * (M + 1) - (((i + 1) * (i)) >> 1);

                    int     memoryIndex = j + rowIndex;
                    Vector3 vertex      = memory.vertices[memoryIndex];
                    Vector3 uv          = memory.uv[memoryIndex];

                    //Normal (S is the vertices, the surface)
                    Vector3 dSdu   = memory.vertices[memoryIndex + 1] - memory.vertices[memoryIndex - 1];
                    Vector3 dSdv   = memory.vertices[rowIndexNext + j] - memory.vertices[rowIndexPrev + j];
                    Vector3 normal = Vector3.Cross(dSdu, dSdv).normalized;

                    //Tangent
                    Vector3 dTxdu   = memory.uv[memoryIndex + 1] - memory.uv[memoryIndex - 1];
                    Vector3 dTxdv   = memory.uv[rowIndexNext + j] - memory.uv[rowIndexPrev + j];
                    Vector3 tangent = getTangent(dSdu, dSdv, dTxdu, dTxdv);

                    mesh.SetPNUV(position, vertex, normal, uv, tangent);

                    position++;
                }
            }

            if (doUpdateStructure)
            {
                triangleStructure.CreateTriangleTessellation(mesh, internalsIndex, facesIndex, polygon);
            }
        }
Beispiel #7
0
        public void UdpdateContent(OutputMesh mesh, CPNPolygon polygon, int internalsIndex,
                                   int facesIndex, bool doUpdateStructure = true)
        {
            quadStructure.RetrieveInfos(polygon);

            bool useUV           = mesh.DoUseUVs();
            bool useNormals      = mesh.DoNormals();
            bool useTangents     = mesh.DoUseUVs();
            int  countProperties = mesh.CountProperties();

            buffer0.requestProperties(countProperties);
            buffer1.requestProperties(countProperties);
            buffer2.requestProperties(countProperties);
            buffer3.requestProperties(countProperties);

            int   MV    = quadStructure.GetMV();
            int   MH    = quadStructure.GetMH();
            float stepV = 1.0f / MV;
            float stepH = 1.0f / MH;

            CPNSideEdge[] polylines = polygon.sideEdges;
            buffer0.writeWithGuide(polylines[0], MH, mesh, evaluator);
            buffer1.writeWithGuide(polylines[1], MV, mesh, evaluator);
            buffer2.writeWithGuide(polylines[2], MH, mesh, evaluator);
            buffer3.writeWithGuide(polylines[3], MV, mesh, evaluator);

            edgeSurface0.Set(buffer0, buffer3, buffer1);
            edgeSurface1.Set(buffer1, buffer0, buffer2);
            edgeSurface2.Set(buffer2, buffer1, buffer3);
            edgeSurface3.Set(buffer3, buffer2, buffer0);

            //float l1 = ks[0] * ks[1] * ks[0] * ks[1];
            //float l2 = ks[1] * ks[2] * ks[1] * ks[2];
            //float l3 = ks[2] * ks[3] * ks[2] * ks[3];
            //float l4 = ks[3] * ks[0] * ks[3] * ks[0];
            float l1 = GetBufferLength(buffer0);
            float l2 = GetBufferLength(buffer1);
            float l3 = GetBufferLength(buffer2);
            float l4 = GetBufferLength(buffer3);

            prepareMemory(MH, MV);

            int index = internalsIndex;

            for (int i = 1; i < MV; i++)
            {
                for (int j = 1; j < MH; j++)
                {
                    float U = (j) * stepH;
                    float V = (i) * stepV;

                    float UM = 1 - U;
                    float VM = 1 - V;

                    float a1 = U * U * VM * VM * UM * UM * l1 /** buffer0.thickness*/;
                    float a2 = VM * VM * U * U * V * V * l2 /** buffer1.thickness*/;
                    float a3 = UM * UM * V * V * U * U * l3 /** buffer2.thickness*/;
                    float a4 = V * V * UM * UM * VM * VM * l4 /** buffer3.thickness*/;

#if DEBUG
                    if (TriangleInterpolator4.interpolationCorner != 0)
                    {
                        switch (TriangleInterpolator4.interpolationCorner)
                        {
                        case 1: a1 = 1; a2 = 0; a3 = 0; a4 = 0; break;

                        case 2: a1 = 0; a2 = 1; a3 = 0; a4 = 0; break;

                        case 3: a1 = 0; a2 = 0; a3 = 1; a4 = 0; break;

                        case 4: a1 = 0; a2 = 0; a3 = 0; a4 = 1; break;
                        }
                    }
#endif

                    float rec = 1.0f / (a1 + a2 + a3 + a4);
                    a1 *= rec;
                    a2 *= rec;
                    a3 *= rec;
                    a4 *= rec;

                    Vector3 V1          = edgeSurface0.evalVertex(j, i);
                    Vector3 V2          = edgeSurface1.evalVertex(i, MH - j);
                    Vector3 V3          = edgeSurface2.evalVertex(MH - j, MV - i);
                    Vector3 V4          = edgeSurface3.evalVertex(MV - i, j);
                    Vector3 vertex      = a1 * V1 + a2 * V2 + a3 * V3 + a4 * V4;
                    int     memoryIndex = j + i * (MH + 1);
                    memory.vertices[memoryIndex] = vertex;

                    if (useUV)
                    {
                        Vector3 V1uv = edgeSurface0.evalUV(j, i);
                        Vector3 V2uv = edgeSurface1.evalUV(i, MH - j);
                        Vector3 V3uv = edgeSurface2.evalUV(MH - j, MV - i);
                        Vector3 V4uv = edgeSurface3.evalUV(MV - i, j);
                        //Vector3 vertex = V3;
                        Vector3 uv = a1 * V1uv + a2 * V2uv + a3 * V3uv + a4 * V4uv;
                        //Vector3 uv = V3uv;
                        memory.uv[memoryIndex] = uv;
                    }

                    for (int k = 0; k < countProperties; k++)
                    {
                        Vector3 V1propK = edgeSurface0.evalProperty(k, j, i);
                        Vector3 V2propK = edgeSurface1.evalProperty(k, i, MH - j);
                        Vector3 V3propK = edgeSurface2.evalProperty(k, MH - j, MV - i);
                        Vector3 V4propK = edgeSurface3.evalProperty(k, MV - i, j);
                        Vector3 propK   = a1 * V1propK + a2 * V2propK + a3 * V3propK + a4 * V4propK;
                        mesh.SetProperty3(index, k, propK);
                    }

                    index++;
                }
            }

            index = internalsIndex;
            for (int i = 1; i < MV; i++)
            {
                for (int j = 1; j < MH; j++)
                {
                    int rowIndex     = i * (MH + 1);
                    int rowIndexPrev = (i - 1) * (MH + 1);
                    int rowIndexNext = (i + 1) * (MH + 1);

                    int     memoryIndex = j + rowIndex;
                    Vector3 vertex      = memory.vertices[memoryIndex];
                    Vector3 uv          = memory.uv[memoryIndex];

                    Vector3 normal  = Vector3.zero;
                    Vector3 tangent = Vector3.zero;

                    if (useNormals)
                    {
                        Vector3 dSdu = memory.vertices[memoryIndex + 1] - memory.vertices[memoryIndex - 1];
                        Vector3 dSdv = memory.vertices[rowIndexNext + j] - memory.vertices[rowIndexPrev + j];
                        dSdu   = dSdu.normalized;
                        dSdv   = dSdv.normalized;
                        normal = Vector3.Cross(dSdu, dSdv).normalized;

                        if (useTangents)
                        {
                            //Tangent
                            Vector3 dTxdu = memory.uv[memoryIndex + 1] - memory.uv[memoryIndex - 1];
                            Vector3 dTxdv = memory.uv[rowIndexNext + j] - memory.uv[rowIndexPrev + j];
                            tangent = getTangent(dSdu, dSdv, dTxdu, dTxdv);
                        }
                    }

                    mesh.SetPNUV(index, vertex, normal, uv, tangent);

                    index++;
                }
            }

            if (doUpdateStructure)
            {
                quadStructure.CreateQuadTessellation(mesh, internalsIndex, facesIndex, polygon);
            }
        }
Beispiel #8
0
        public void UdpdateContent(OutputMesh mesh, CPNPolygon polygon, int internalsIndex,
                                   int facesIndex, bool doUpdateStructure = true)
        {
            quadStructure.RetrieveInfos(polygon);

            bool useUV           = mesh.DoUseUVs();
            bool useNormals      = mesh.DoNormals();
            bool useTangents     = mesh.DoUseUVs();
            int  countProperties = mesh.CountProperties();

            buffer0.requestProperties(countProperties);
            buffer1.requestProperties(countProperties);
            buffer2.requestProperties(countProperties);
            buffer3.requestProperties(countProperties);

            int   MV    = quadStructure.GetMV();
            int   MH    = quadStructure.GetMH();
            float stepV = 1.0f / MV;
            float stepH = 1.0f / MH;

            CPNSideEdge[] polylines = polygon.sideEdges;
            buffer0.writeWithGuide(polylines[0], MH, mesh, evaluator);
            buffer1.writeWithGuide(polylines[1], MV, mesh, evaluator);
            buffer2.writeWithGuide(polylines[2], MH, mesh, evaluator);
            buffer3.writeWithGuide(polylines[3], MV, mesh, evaluator);

            corner0.Set(buffer0, buffer3);
            corner1.Set(buffer1, buffer0);
            corner2.Set(buffer2, buffer1);
            corner3.Set(buffer3, buffer2);

            int position = internalsIndex;


            for (int i = 1; i < MV; i++)
            {
                for (int j = 1; j < MH; j++)
                {
                    float U = (j) * stepH;
                    float V = (i) * stepV;

                    Vector3 V1 = corner0.evalVertex(j, i);
                    Vector3 V2 = corner1.evalVertex(i, MH - j);
                    Vector3 V3 = corner2.evalVertex(MH - j, MV - i);
                    Vector3 V4 = corner3.evalVertex(MV - i, j);

                    float UM = 1 - U;
                    float VM = 1 - V;

                    float a1 = (UM * VM * UM * VM);
                    float a2 = (U * VM * U * VM);
                    float a3 = (U * V * U * V);
                    float a4 = (UM * V * UM * V);

                    float rec = 1.0f / (a1 + a2 + a3 + a4);
                    a1 *= rec;
                    a2 *= rec;
                    a3 *= rec;
                    a4 *= rec;

                    Vector3 vertex = a1 * V1 + a2 * V2 + a3 * V3 + a4 * V4;

                    Vector3 normal  = Vector3.zero;
                    Vector3 uv      = Vector3.zero;
                    Vector3 tangent = Vector3.zero;

                    if (useNormals)
                    {
                        Vector3 V1N = corner0.evalNormal(j, i);
                        Vector3 V2N = corner1.evalNormal(i, MH - j);
                        Vector3 V3N = corner2.evalNormal(MH - j, MV - i);
                        Vector3 V4N = corner3.evalNormal(MV - i, j);
                        normal = a1 * V1N + a2 * V2N + a3 * V3N + a4 * V4N;
                    }

                    if (useUV)
                    {
                        Vector3 V1uv = corner0.evalUV(j, i);
                        Vector3 V2uv = corner1.evalUV(i, MH - j);
                        Vector3 V3uv = corner2.evalUV(MH - j, MV - i);
                        Vector3 V4uv = corner3.evalUV(MV - i, j);
                        uv = a1 * V1uv + a2 * V2uv + a3 * V3uv + a4 * V4uv;

                        if (useTangents)
                        {
                            float Uu  = U + 0.001f;
                            float UMu = 1 - Uu;
                            float a1u = (UMu * VM * UMu * VM);
                            float a2u = (Uu * VM * Uu * VM);
                            float a3u = (Uu * V * Uu * V);
                            float a4u = (UMu * V * UMu * V);
                            a1u *= rec; a2u *= rec; a3u *= rec; a4u *= rec;
                            float Vv  = V + 0.001f;
                            float VMv = 1 - Vv;
                            float a1v = (UM * VMv * UM * VMv);
                            float a2v = (U * VMv * U * VMv);
                            float a3v = (U * Vv * U * Vv);
                            float a4v = (UM * Vv * UM * Vv);
                            a1v *= rec; a2v *= rec; a3v *= rec; a4v *= rec;
                            Vector3 DPu  = (a1u * V1 + a2u * V2 + a3u * V3 + a4u * V4) - vertex;
                            Vector3 DPv  = (a1v * V1 + a2v * V2 + a3v * V3 + a4v * V4) - vertex;
                            Vector3 DUVu = (a1u * V1uv + a2u * V2uv + a3u * V3uv + a4u * V4uv) - vertex;
                            Vector3 DUVv = (a1v * V1uv + a2v * V2uv + a3v * V3uv + a4v * V4uv) - vertex;
                            tangent = getTangent(DPu, DPv, DUVu, DUVv);
                        }
                    }

                    for (int k = 0; k < countProperties; k++)
                    {
                        Vector3 V1propK = corner0.evalProperty(k, j, i);
                        Vector3 V2propK = corner1.evalProperty(k, i, MH - j);
                        Vector3 V3propK = corner2.evalProperty(k, MH - j, MV - i);
                        Vector3 V4propK = corner3.evalProperty(k, MV - i, j);
                        Vector3 propK   = a1 * V1propK + a2 * V2propK + a3 * V3propK + a4 * V4propK;
                        mesh.SetProperty3(position, k, propK);
                    }

                    mesh.SetPNUV(position, vertex, normal, uv, tangent);
                    position++;
                }
            }


            if (doUpdateStructure)
            {
                quadStructure.CreateQuadTessellation(mesh, internalsIndex, facesIndex, polygon);
            }
        }