Exemple #1
0
        public static Texture2D CreateTextureFromNormalArray(NormalArray normalArray)
        {
            var inputTexture = new Texture2D(normalArray.Width, normalArray.Height, TextureFormat.RGB24, false);

            byte[] rawTextureArray = new byte[normalArray.Width * normalArray.Height * 3];
            for (int y = 0; y < normalArray.Height; y++)
            {
                for (int x = 0; x < normalArray.Width; x++)
                {
                    Vector3 normal = EncodeNormal(normalArray.NormalsAsArray[x, y]);
                    var     idx    = 3 * (y * normalArray.Height + x);
                    rawTextureArray[idx]     = (byte)(normal.x * 255);
                    rawTextureArray[idx + 1] = (byte)(normal.y * 255);
                    rawTextureArray[idx + 2] = (byte)(normal.z * 255);
                }
            }

            inputTexture.LoadRawTextureData(rawTextureArray);
            inputTexture.Apply();
            return(inputTexture);
        }
Exemple #2
0
        public void UpdateGeometry()
        {
            if (VertexArray == null)
            {
                return;
            }
            int updateThreadID = GlobalEnvironment.UpdateThreadID;
            int curThreadID    = System.Threading.Thread.CurrentThread.ManagedThreadId;

            if (updateThreadID != curThreadID)
            {
                return;
            }
            VdsVec3d[] vArray = VertexArray.ToArray();
            VdsVec3d[] nArray = null;
            if (NormalArray != null)
            {
                nArray = NormalArray.ToArray();
            }
            else
            {
                nArray = new VdsVec3d[0];
            }
            VdsVec4d[] cArray = null;
            if (ColorArray != null)
            {
                cArray = ColorArray.ToArray();
            }
            else
            {
                cArray = new VdsVec4d[0];
            }
            VdsVec2d[] tArray = null;
            if (TexCoordArray != null)
            {
                tArray = TexCoordArray.ToArray();
            }
            else
            {
                tArray = new VdsVec2d[0];
            }

            VdsVec4d[] maturalArray = null;
            if (AmbientMatural == null && DiffuseMatural == null && SpecularMatural == null && EmissionMatural == null)
            {
                maturalArray = new VdsVec4d[0];
            }
            else
            {
                maturalArray = new VdsVec4d[4];
                if (AmbientMatural == null)
                {
                    maturalArray[0] = new VdsVec4d(0.8, 0.8, 0.8, 1.0);
                }
                else
                {
                    maturalArray[0] = AmbientMatural;
                }
                if (DiffuseMatural == null)
                {
                    maturalArray[1] = new VdsVec4d(0.9, 0.9, 0.9, 1.0);
                }
                else
                {
                    maturalArray[1] = DiffuseMatural;
                }
                if (SpecularMatural == null)
                {
                    maturalArray[2] = new VdsVec4d(0.95, 0.95, 0.95, 1.0);
                }
                else
                {
                    maturalArray[2] = SpecularMatural;
                }
                if (EmissionMatural == null)
                {
                    maturalArray[3] = new VdsVec4d(0.1, 0.1, 0.1, 1.0);
                }
                else
                {
                    maturalArray[3] = EmissionMatural;
                }
            }
            string tFileName = "";

            if (TextureFileName != null)
            {
                tFileName = TextureFileName;
            }
            int drawMode = 0;

            switch (DrawMode)
            {
            case GeometryDrawModel.POINTS:
                drawMode = 0;
                break;

            case GeometryDrawModel.LINES:
                drawMode = 1;
                break;

            case GeometryDrawModel.LINE_STRIP:
                drawMode = 3;
                break;

            case GeometryDrawModel.LINE_LOOP:
                drawMode = 2;
                break;

            case GeometryDrawModel.TRIANGLES:
                drawMode = 4;
                break;

            case GeometryDrawModel.TRIANGLE_STRIP:
                drawMode = 5;
                break;

            case GeometryDrawModel.TRIANGLE_FAN:
                drawMode = 6;
                break;

            case GeometryDrawModel.QUADS:
                drawMode = 7;
                break;

            case GeometryDrawModel.QUAD_STRIP:
                drawMode = 8;
                break;
            }
            IUpdateGeometry(this.NativeHandle, vArray, nArray, cArray, tArray, drawMode, (int)CullFaceMode, tFileName, maturalArray);
        }
Exemple #3
0
        public bool BuildIndexedPrimitives(List <NullMergeIndex> indexMapping)
        {
            if ((GetMeshObjectType() != NullPrimitiveType.MOT_TRIANGLES) || (GetTriangleCount() < 2))
            {
                return(false);
            }
            List <NullVertexStruct> floatData = PrepareFloatDataForVertexMerging();

            if (floatData == null)
            {
                return(false);
            }
            MergeVertices(floatData, indexMapping);

            //update mesh data
            List <Vector3> newVertices = ReCreateCompactData(VertexPosArray, indexMapping);

            VertexPosArray.Clear();
            VertexPosArray = newVertices;

            List <uint> newColors = ReCreateCompactData(VertexColorArray, indexMapping);

            if (VertexColorArray != null && VertexColorArray.Count > 0)
            {
                VertexColorArray.Clear();
                VertexColorArray = newColors;
            }

            List <Vector3> newNormals = ReCreateCompactData(NormalArray, indexMapping);

            if (NormalArray != null && NormalArray.Count > 0)
            {
                NormalArray.Clear();
                NormalArray = newNormals;
            }

            List <Vector3> newTangents = ReCreateCompactData(TangentArray, indexMapping);

            if (TangentArray != null && TangentArray.Count > 0)
            {
                TangentArray.Clear();
                TangentArray = newTangents;
            }

            List <Vector3> newBinormals = ReCreateCompactData(BinormalArray, indexMapping);

            if (BinormalArray != null && BinormalArray.Count > 0)
            {
                BinormalArray.Clear();
                BinormalArray = newBinormals;
            }
            //update uv groups
            if (UVGroups != null)
            {
                UVGroups.BuildIndexedPrimitives(indexMapping);
            }

            List <int> faceIndexes = new List <int>();

            for (int i = 0; i < GetTriangleCount(); i++)
            {
                faceIndexes.Add(i * 3 + 0);
                faceIndexes.Add(i * 3 + 1);
                faceIndexes.Add(i * 3 + 2);
            }
            for (int i = 0; i < indexMapping.Count; i++)
            {
                NullMergeIndex index = indexMapping[i];
                faceIndexes[index.index] = i;
                for (int j = 0; j < index.equalOnes.Count; j++)
                {
                    faceIndexes[index.equalOnes[j]] = i;
                }
            }
            FaceArray.Clear();
            for (int i = 0; i < GetTriangleCount(); i++)
            {
                FaceArray.Add(new Vector3Int(faceIndexes[i * 3 + 0], faceIndexes[i * 3 + 1], faceIndexes[i * 3 + 2]));
            }
            SetMeshObjectType(NullPrimitiveType.MOT_INDEXED_PRIMITIVES);
            return(true);
        }