Exemplo n.º 1
0
        Mesh NiTriShapeDataToMesh(NiTriShapeData data)
        {
            // vertex positions
            var vertices = new Vector3[data.Vertices.Length];

            for (var i = 0; i < vertices.Length; i++)
            {
                vertices[i] = NifUtils.NifPointToUnityPoint(data.Vertices[i]);
            }
            // vertex normals
            Vector3[] normals = null;
            if (data.HasNormals)
            {
                normals = new Vector3[vertices.Length];
                for (var i = 0; i < normals.Length; i++)
                {
                    normals[i] = NifUtils.NifVectorToUnityVector(data.Normals[i]);
                }
            }
            // vertex UV coordinates
            Vector2[] UVs = null;
            if (data.HasUV)
            {
                UVs = new Vector2[vertices.Length];
                for (var i = 0; i < UVs.Length; i++)
                {
                    var NiTexCoord = data.UVSets[0, i];
                    UVs[i] = new Vector2(NiTexCoord.u, NiTexCoord.v);
                }
            }
            // triangle vertex indices
            var triangles = new int[data.NumTrianglePoints];

            for (var i = 0; i < data.Triangles.Length; i++)
            {
                var baseI = 3 * i;
                // Reverse triangle winding order.
                triangles[baseI]     = data.Triangles[i].v1;
                triangles[baseI + 1] = data.Triangles[i].v3;
                triangles[baseI + 2] = data.Triangles[i].v2;
            }

            // Create the mesh.
            var mesh = new Mesh
            {
                vertices  = vertices,
                normals   = normals,
                uv        = UVs,
                triangles = triangles
            };

            if (!data.HasNormals)
            {
                mesh.RecalculateNormals();
            }
            mesh.RecalculateBounds();
            return(mesh);
        }
Exemplo n.º 2
0
 public static TriangleCollection GetTrianglesFromGeometryShape(this NiTriShapeData shapeData, Matrix transformationMatrix)
 {
     return(new TriangleCollection
     {
         Vertices = shapeData.Vertices.Select(vert => { Vector3 trans; Vector3.Transform(ref vert, ref transformationMatrix, out trans); return trans; }).ToArray(),
         Indices = shapeData.Triangles.Select(tri => new TriangleIndex {
             A = tri.X, B = tri.Y, C = tri.Z,
         }).ToArray()
     });
 }
Exemplo n.º 3
0
 public ShapeDesc()
 {
     this.shape       = (NiTriShape)null;
     this.data        = (NiTriShapeData)null;
     this.textures    = new string[2];
     this.boundingBox = new BBox();
     // relative x, y for segment
     this.x        = new float();
     this.y        = new float();
     this.segments = new List <SegmentDesc>();
 }
Exemplo n.º 4
0
        private void ParseShape(NiTriShape shape)
        {
            List <string> export = new List <string>();

            NiTriShapeData geometry = (NiTriShapeData)shape.Data.Object;

            // Verticles (v)
            if (geometry.HasVertices && geometry.NumVertices >= 3)
            {
                Matrix transformationMatrix = ComputeWorldMatrix(shape);
                computePolys(geometry.Triangles, geometry.Vertices, transformationMatrix);
            }
        }
Exemplo n.º 5
0
        public void Load(Stream source_stream)
        {
            NiHeader header = GetHeader(source_stream);

            header.Dump();

            int bt_NiTriShapeData           = header.GetBlockTypeIdxByName("NiTriShapeData");
            int bt_BSLightingShaderProperty = header.GetBlockTypeIdxByName("BSLightingShaderProperty");
            int bt_BSShaderTextureSet       = header.GetBlockTypeIdxByName("BSShaderTextureSet");
            int bt_NiSkinInstance           = header.GetBlockTypeIdxByName("NiSkinInstance");
            int bt_NiSkinPartition          = header.GetBlockTypeIdxByName("NiSkinPartition");

            int num_blocks = header.blocks.Length;

            for (int i = 0; i < num_blocks; i++)
            {
                if (header.blocks[i].type == bt_NiTriShapeData)
                {
                    NiTriShapeData triShapeData = GetObject <NiTriShapeData>(header, i);
                    triShapeData.Dump();
                }
                if (header.blocks[i].type == bt_BSLightingShaderProperty)
                {
                    BSLightingShaderProperty lightingShaderProperty = GetObject <BSLightingShaderProperty>(header, i);
                    lightingShaderProperty.Dump();
                }
                if (header.blocks[i].type == bt_BSShaderTextureSet)
                {
                    BSShaderTextureSet shaderTextureSet = GetObject <BSShaderTextureSet>(header, i);
                    shaderTextureSet.Dump();
                }
                if (header.blocks[i].type == bt_NiSkinInstance)
                {
                    NiSkinInstance skinInstance = GetObject <NiSkinInstance>(header, i);
                    skinInstance.Dump();
                    foreach (ObjectRef boneref in skinInstance.bones)
                    {
                        NiNode node = GetObject <NiNode>(header, boneref);
                        System.Console.WriteLine(header.strings[node.name]);
                    }
                }
                if (header.blocks[i].type == bt_NiSkinPartition)
                {
                    NiSkinPartition skinPartition = GetObject <NiSkinPartition>(header, i);
                    skinPartition.Dump();
                }
            }
        }
        public QuadTree(NiTriShapeData data, BBox boundingBox)
        {
            this.nodes    = new QuadTreeNode[4];
            this.vertices = data.GetVertices();
            BBox  boundingBox1 = new BBox();
            BBox  boundingBox2 = new BBox();
            BBox  boundingBox3 = new BBox();
            BBox  boundingBox4 = new BBox();
            float num1         = (float)(((double)boundingBox.px2 - (double)boundingBox.px1) / 2.0);
            float num2         = (float)(((double)boundingBox.py2 - (double)boundingBox.py1) / 2.0);

            boundingBox1.Set(boundingBox.px1, boundingBox.px1 + num1, boundingBox.py1, boundingBox.py1 + num2, 0.0f, 0.0f);
            boundingBox2.Set(boundingBox.px1 + num1, boundingBox.px2, boundingBox.py1, boundingBox.py1 + num2, 0.0f, 0.0f);
            boundingBox3.Set(boundingBox.px1, boundingBox.px1 + num1, boundingBox.py1 + num2, boundingBox.py2, 0.0f, 0.0f);
            boundingBox4.Set(boundingBox.px1 + num1, boundingBox.px2, boundingBox.py1 + num2, boundingBox.py2, 0.0f, 0.0f);
            this.nodes[0] = this.CreateQuadNode(data.GetTriangles(), data.GetVertices(), boundingBox1);
            this.nodes[1] = this.CreateQuadNode(data.GetTriangles(), data.GetVertices(), boundingBox2);
            this.nodes[2] = this.CreateQuadNode(data.GetTriangles(), data.GetVertices(), boundingBox3);
            this.nodes[3] = this.CreateQuadNode(data.GetTriangles(), data.GetVertices(), boundingBox4);
        }
Exemplo n.º 7
0
        private string ParseShape(NiTriShape shape)
        {
            if (!shape.Data.IsValid())
            {
                return("");
            }

            NiTriShapeData geometry = (NiTriShapeData)shape.Data.Object;

            // The final text
            List <string> export = new List <string>();

            Matrix transformationMatrix = ComputeWorldMatrix(shape);

            // Set Object name
            export.Add("g Shape " + shape.Name + Environment.NewLine);

            NiMaterialProperty  material = null;
            NiTexturingProperty texture  = null;

            foreach (NiRef <NiProperty> property in shape.Properties)
            {
                if (property.Object is NiMaterialProperty)
                {
                    material = property.Object as NiMaterialProperty;
                }
                if (property.Object is NiTexturingProperty)
                {
                    texture = property.Object as NiTexturingProperty;
                }
            }

            if (material != null && texture != null)
            {
                export.Add(printMaterial(material, texture));
            }

            // Verticles (v)
            if (geometry.HasVertices && geometry.NumVertices >= 3)
            {
                export.Add(printVertices(geometry.Vertices, transformationMatrix));
            }

            // Texture coordinates (vt)
            if (geometry.UVSets.Length > 0)
            {
                export.Add(printUvSets(geometry.UVSets));
            }

            // Normals (vn)
            if (geometry.HasNormals)
            {
                export.Add(printNormals(geometry.Normals, transformationMatrix));
            }

            // Parameter space vertices (vp)

            // Face Definitions (f)
            export.Add(printTriangles(geometry.Triangles, (geometry.UVSets.Length > 0)));

            return(string.Join(Environment.NewLine, export));
        }