private void CreateRenderData(Mesh meshToBuildListFor, Func <Vector3Float, Color> getColorFunc)
        {
            subMeshs = new List <SubTriangleMesh>();
            SubTriangleMesh currentSubMesh              = null;
            VectorPOD <VertexTextureData>  textureData  = null;
            VectorPOD <VertexColorData>    colorData    = null;
            VectorPOD <VertexNormalData>   normalData   = null;
            VectorPOD <VertexPositionData> positionData = null;

            // first make sure all the textures are created
            for (int faceIndex = 0; faceIndex < meshToBuildListFor.Faces.Count; faceIndex++)
            {
                FaceTextureData faceTexture;
                meshToBuildListFor.FaceTextures.TryGetValue(faceIndex, out faceTexture);
                if (faceTexture != null)
                {
                    ImageGlPlugin.GetImageGlPlugin(faceTexture.image, true);
                }

                // don't compare the data of the texture but rather if they are just the same object
                if (subMeshs.Count == 0 ||
                    (faceTexture != null &&
                     (object)subMeshs[subMeshs.Count - 1].texture != (object)faceTexture.image))
                {
                    SubTriangleMesh newSubMesh = new SubTriangleMesh();
                    newSubMesh.texture = faceTexture == null ? null : faceTexture.image;
                    subMeshs.Add(newSubMesh);
                    if (getColorFunc != null)
                    {
                        newSubMesh.UseVertexColors = true;
                    }

                    currentSubMesh = subMeshs[subMeshs.Count - 1];
                    textureData    = currentSubMesh.textureData;
                    colorData      = currentSubMesh.colorData;
                    normalData     = currentSubMesh.normalData;
                    positionData   = currentSubMesh.positionData;
                }

                VertexColorData color = new VertexColorData();

                if (getColorFunc != null)
                {
                    var faceColor = getColorFunc(meshToBuildListFor.Faces[faceIndex].normal);
                    color = new VertexColorData
                    {
                        red   = faceColor.red,
                        green = faceColor.green,
                        blue  = faceColor.blue
                    };
                }

                VertexTextureData  tempTexture;
                VertexNormalData   tempNormal;
                VertexPositionData tempPosition;
                tempTexture.textureU = faceTexture == null ? 0 : (float)faceTexture.uv0.X;
                tempTexture.textureV = faceTexture == null ? 0 : (float)faceTexture.uv0.Y;
                var normal = meshToBuildListFor.Faces[faceIndex].normal;
                tempNormal.normalX = normal.X;
                tempNormal.normalY = normal.Y;
                tempNormal.normalZ = normal.Z;
                int vertexIndex = meshToBuildListFor.Faces[faceIndex].v0;
                tempPosition.positionX = (float)meshToBuildListFor.Vertices[vertexIndex].X;
                tempPosition.positionY = (float)meshToBuildListFor.Vertices[vertexIndex].Y;
                tempPosition.positionZ = (float)meshToBuildListFor.Vertices[vertexIndex].Z;
                textureData.Add(tempTexture);
                normalData.Add(tempNormal);
                positionData.Add(tempPosition);
                colorData.add(color);

                tempTexture.textureU   = faceTexture == null ? 0 : (float)faceTexture.uv1.X;
                tempTexture.textureV   = faceTexture == null ? 0 : (float)faceTexture.uv1.Y;
                vertexIndex            = meshToBuildListFor.Faces[faceIndex].v1;
                tempPosition.positionX = (float)meshToBuildListFor.Vertices[vertexIndex].X;
                tempPosition.positionY = (float)meshToBuildListFor.Vertices[vertexIndex].Y;
                tempPosition.positionZ = (float)meshToBuildListFor.Vertices[vertexIndex].Z;
                textureData.Add(tempTexture);
                normalData.Add(tempNormal);
                positionData.Add(tempPosition);
                colorData.add(color);

                tempTexture.textureU   = faceTexture == null ? 0 : (float)faceTexture.uv2.X;
                tempTexture.textureV   = faceTexture == null ? 0 : (float)faceTexture.uv2.Y;
                vertexIndex            = meshToBuildListFor.Faces[faceIndex].v2;
                tempPosition.positionX = (float)meshToBuildListFor.Vertices[vertexIndex].X;
                tempPosition.positionY = (float)meshToBuildListFor.Vertices[vertexIndex].Y;
                tempPosition.positionZ = (float)meshToBuildListFor.Vertices[vertexIndex].Z;
                textureData.Add(tempTexture);
                normalData.Add(tempNormal);
                positionData.Add(tempPosition);
                colorData.add(color);
            }
        }
Exemplo n.º 2
0
        private void CreateRenderData(Mesh meshToBuildListFor, Func <Vector3, Color> getColorFunc)
        {
            subMeshs = new List <SubTriangleMesh>();
            SubTriangleMesh currentSubMesh              = null;
            VectorPOD <VertexTextureData>  textureData  = null;
            VectorPOD <VertexColorData>    colorData    = null;
            VectorPOD <VertexNormalData>   normalData   = null;
            VectorPOD <VertexPositionData> positionData = null;

            // first make sure all the textures are created
            foreach (Face face in meshToBuildListFor.Faces)
            {
                ImageBuffer faceTexture = face.GetTexture(0);
                if (faceTexture != null)
                {
                    ImageGlPlugin.GetImageGlPlugin(faceTexture, true);
                }

                // don't compare the data of the texture but rather if they are just the same object
                if (subMeshs.Count == 0 || (object)subMeshs[subMeshs.Count - 1].texture != (object)faceTexture)
                {
                    SubTriangleMesh newSubMesh = new SubTriangleMesh();
                    newSubMesh.texture = faceTexture;
                    subMeshs.Add(newSubMesh);
                    if (getColorFunc != null)
                    {
                        newSubMesh.UseVertexColors = true;
                    }

                    currentSubMesh = subMeshs[subMeshs.Count - 1];
                    textureData    = currentSubMesh.textureData;
                    colorData      = currentSubMesh.colorData;
                    normalData     = currentSubMesh.normalData;
                    positionData   = currentSubMesh.positionData;
                }

                Vector2[]       textureUV = new Vector2[2];
                Vector3[]       position  = new Vector3[2];
                VertexColorData color     = new VertexColorData();

                if (getColorFunc != null)
                {
                    var faceColor = getColorFunc(face.Normal);
                    color = new VertexColorData
                    {
                        red   = faceColor.red,
                        green = faceColor.green,
                        blue  = faceColor.blue
                    };
                }

                int vertexIndex = 0;
                foreach (FaceEdge faceEdge in face.FaceEdges())
                {
                    if (vertexIndex < 2)
                    {
                        textureUV[vertexIndex] = faceEdge.GetUv(0);
                        position[vertexIndex]  = faceEdge.FirstVertex.Position;
                    }
                    else
                    {
                        VertexTextureData  tempTexture;
                        VertexNormalData   tempNormal;
                        VertexPositionData tempPosition;
                        tempTexture.textureU   = (float)textureUV[0].X; tempTexture.textureV = (float)textureUV[0].Y;
                        tempNormal.normalX     = (float)face.Normal.X; tempNormal.normalY = (float)face.Normal.Y; tempNormal.normalZ = (float)face.Normal.Z;
                        tempPosition.positionX = (float)position[0].X; tempPosition.positionY = (float)position[0].Y; tempPosition.positionZ = (float)position[0].Z;
                        textureData.Add(tempTexture);
                        normalData.Add(tempNormal);
                        positionData.Add(tempPosition);
                        colorData.add(color);

                        tempTexture.textureU   = (float)textureUV[1].X; tempTexture.textureV = (float)textureUV[1].Y;
                        tempNormal.normalX     = (float)face.Normal.X; tempNormal.normalY = (float)face.Normal.Y; tempNormal.normalZ = (float)face.Normal.Z;
                        tempPosition.positionX = (float)position[1].X; tempPosition.positionY = (float)position[1].Y; tempPosition.positionZ = (float)position[1].Z;
                        textureData.Add(tempTexture);
                        normalData.Add(tempNormal);
                        positionData.Add(tempPosition);
                        colorData.add(color);

                        Vector2 textureUV2 = faceEdge.GetUv(0);
                        Vector3 position2  = faceEdge.FirstVertex.Position;
                        tempTexture.textureU   = (float)textureUV2.X; tempTexture.textureV = (float)textureUV2.Y;
                        tempNormal.normalX     = (float)face.Normal.X; tempNormal.normalY = (float)face.Normal.Y; tempNormal.normalZ = (float)face.Normal.Z;
                        tempPosition.positionX = (float)position2.X; tempPosition.positionY = (float)position2.Y; tempPosition.positionZ = (float)position2.Z;
                        textureData.Add(tempTexture);
                        normalData.Add(tempNormal);
                        positionData.Add(tempPosition);
                        colorData.add(color);

                        textureUV[1] = faceEdge.GetUv(0);
                        position[1]  = faceEdge.FirstVertex.Position;
                    }

                    vertexIndex++;
                }
            }
        }