Exemplo n.º 1
0
        private void generateVertices(float initialSize)
        {
            this.Meshes    = new Mesh[1];
            this.Materials = new Material[1];

            List <TexturedVertex> verticesList = new List <TexturedVertex>();

            // Top
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, initialSize, -initialSize), Color4.Green, new Vector3(0, 1, 0)));  // top right
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, initialSize, -initialSize), Color4.Green, new Vector3(0, 1, 0))); // top left
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, initialSize, initialSize), Color4.Green, new Vector3(0, 1, 0)));  // bottom left
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, initialSize, initialSize), Color4.Green, new Vector3(0, 1, 0)));   // bottom right

            // Bottom
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, -initialSize, -initialSize), Color4.Orange, new Vector3(0, -1, 0))); // top left
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, -initialSize, initialSize), Color4.Orange, new Vector3(0, -1, 0)));  // bottom left
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, -initialSize, initialSize), Color4.Orange, new Vector3(0, -1, 0)));   // bottom right
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, -initialSize, -initialSize), Color4.Orange, new Vector3(0, -1, 0)));  // top right

            // Front
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, initialSize, initialSize), Color4.Red, new Vector3(0, 0, 1)));   // top right
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, initialSize, initialSize), Color4.Red, new Vector3(0, 0, 1)));  // top left
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, -initialSize, initialSize), Color4.Red, new Vector3(0, 0, 1))); // bottom left
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, -initialSize, initialSize), Color4.Red, new Vector3(0, 0, 1)));  // bottom right

            // Back
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, initialSize, -initialSize), Color4.Yellow, new Vector3(0, 0, -1)));   // top right
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, initialSize, -initialSize), Color4.Yellow, new Vector3(0, 0, -1)));  // top left
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, -initialSize, -initialSize), Color4.Yellow, new Vector3(0, 0, -1))); // bottom left
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, -initialSize, -initialSize), Color4.Yellow, new Vector3(0, 0, -1)));  // bottom right

            // Left
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, initialSize, initialSize), Color4.Blue, new Vector3(-1, 0, 0)));   // top right
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, initialSize, -initialSize), Color4.Blue, new Vector3(-1, 0, 0)));  // top left
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, -initialSize, -initialSize), Color4.Blue, new Vector3(-1, 0, 0))); // bottom left
            verticesList.Add(new TexturedVertex(new Vector3(-initialSize, -initialSize, initialSize), Color4.Blue, new Vector3(-1, 0, 0)));  // bottom right

            // Right
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, initialSize, initialSize), Color4.Magenta, new Vector3(1, 0, 0)));   // top right
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, initialSize, -initialSize), Color4.Magenta, new Vector3(1, 0, 0)));  // top left
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, -initialSize, -initialSize), Color4.Magenta, new Vector3(1, 0, 0))); // bottom left
            verticesList.Add(new TexturedVertex(new Vector3(initialSize, -initialSize, initialSize), Color4.Magenta, new Vector3(1, 0, 0)));  // bottom right

            List <ushort> indices = new List <ushort>()
            {
                0, 1, 2, 0, 2, 3,       // top
                4, 5, 6, 4, 6, 7,       // bottom
                8, 9, 10, 8, 10, 11,    // front
                12, 13, 14, 12, 14, 15, // back
                16, 17, 18, 16, 18, 19, // left
                20, 21, 22, 20, 22, 23  // right
            };

            Mesh.Primitive shapePrimitive = new Mesh.Primitive(verticesList, indices);
            this.Meshes[0]    = new Mesh(shapePrimitive);
            this.Materials[0] = new Material(0, Color4.Red, new Color4(255, 100, 100, 255), 2);
        }
Exemplo n.º 2
0
        private void generateVertices(float width, float height)
        {
            this.Meshes    = new Mesh[1];
            this.Materials = new Material[1];

            List <TexturedVertex> verticesList = new List <TexturedVertex>();

            verticesList.Add(new TexturedVertex(new Vector3(width, 0, -height), Color4.White, new Vector3(0, 1, 0)));  // top right
            verticesList.Add(new TexturedVertex(new Vector3(-width, 0, -height), Color4.White, new Vector3(0, 1, 0))); // top left
            verticesList.Add(new TexturedVertex(new Vector3(-width, 0, height), Color4.White, new Vector3(0, 1, 0)));  // bottom left
            verticesList.Add(new TexturedVertex(new Vector3(width, 0, height), Color4.White, new Vector3(0, 1, 0)));   // bottom right

            List <ushort> indices = new List <ushort>()
            {
                0, 1, 2, 0, 2, 3
            };

            Mesh.Primitive shapePrimitive = new Mesh.Primitive(verticesList, indices);
            this.Meshes[0]    = new Mesh(shapePrimitive);
            this.Materials[0] = new Material(0, Color4.White, Color4.White, 16);
        }
Exemplo n.º 3
0
        public virtual void GenerateVertices(int recursionLevel, bool perFaceLighting, float size, Texture diffuseTexture = null, Texture specularTexture = null, IShapeDecorator decorator = null)
        {
            this.Meshes    = new Mesh[1];
            this.Materials = new Material[1];
            List <TexturedVertex> verticesList = new List <TexturedVertex>();

            this.middlePointIndexCache = new Dictionary <long, int>();
            this.points = new List <Vector3>();
            this.index  = 0;
            float       t     = (float)((1.0 + Math.Sqrt(5.0)) / 2.0);
            float       s     = 1;
            List <Face> faces = new List <Face>();

            this.addVertex(new Vector3(-s, t, 0));
            this.addVertex(new Vector3(s, t, 0));
            this.addVertex(new Vector3(-s, -t, 0));
            this.addVertex(new Vector3(s, -t, 0));

            this.addVertex(new Vector3(0, -s, t));
            this.addVertex(new Vector3(0, s, t));
            this.addVertex(new Vector3(0, -s, -t));
            this.addVertex(new Vector3(0, s, -t));

            this.addVertex(new Vector3(t, 0, -s));
            this.addVertex(new Vector3(t, 0, s));
            this.addVertex(new Vector3(-t, 0, -s));
            this.addVertex(new Vector3(-t, 0, s));

            // 5 faces around point 0
            faces.Add(new Face(this.points[0], this.points[11], this.points[5]));
            faces.Add(new Face(this.points[0], this.points[5], this.points[1]));
            faces.Add(new Face(this.points[0], this.points[1], this.points[7]));
            faces.Add(new Face(this.points[0], this.points[7], this.points[10]));
            faces.Add(new Face(this.points[0], this.points[10], this.points[11]));

            // 5 adjacent faces
            faces.Add(new Face(this.points[1], this.points[5], this.points[9]));
            faces.Add(new Face(this.points[5], this.points[11], this.points[4]));
            faces.Add(new Face(this.points[11], this.points[10], this.points[2]));
            faces.Add(new Face(this.points[10], this.points[7], this.points[6]));
            faces.Add(new Face(this.points[7], this.points[1], this.points[8]));

            // 5 faces around point 3
            faces.Add(new Face(this.points[3], this.points[9], this.points[4]));
            faces.Add(new Face(this.points[3], this.points[4], this.points[2]));
            faces.Add(new Face(this.points[3], this.points[2], this.points[6]));
            faces.Add(new Face(this.points[3], this.points[6], this.points[8]));
            faces.Add(new Face(this.points[3], this.points[8], this.points[9]));

            // 5 adjacent faces
            faces.Add(new Face(this.points[4], this.points[9], this.points[5]));
            faces.Add(new Face(this.points[2], this.points[4], this.points[11]));
            faces.Add(new Face(this.points[6], this.points[2], this.points[10]));
            faces.Add(new Face(this.points[8], this.points[6], this.points[7]));
            faces.Add(new Face(this.points[9], this.points[8], this.points[1]));

            // refine triangles
            for (int i = 0; i < recursionLevel; i++)
            {
                var faces2 = new List <Face>();
                foreach (Face tri in faces)
                {
                    // replace triangle by 4 triangles
                    int a = this.getMiddlePoint(tri.V1, tri.V2);
                    int b = this.getMiddlePoint(tri.V2, tri.V3);
                    int c = this.getMiddlePoint(tri.V3, tri.V1);

                    faces2.Add(new Face(tri.V1, this.points[a], this.points[c]));
                    faces2.Add(new Face(tri.V2, this.points[b], this.points[a]));
                    faces2.Add(new Face(tri.V3, this.points[c], this.points[b]));
                    faces2.Add(new Face(this.points[a], this.points[b], this.points[c]));
                }
                faces = faces2;
            }

            foreach (Face tri in faces)
            {
                Vector3 V1 = tri.V1;
                Vector3 V2 = tri.V2;
                Vector3 V3 = tri.V3;

                V1 = Vector3.Multiply(V1, size);
                V2 = Vector3.Multiply(V2, size);
                V3 = Vector3.Multiply(V3, size);

                if (decorator != null)
                {
                    decorator.ApplyFace(ref V1, ref V2, ref V3);
                }

                Vector2 uv1 = V1.GetSphereCoord();
                Vector2 uv2 = V2.GetSphereCoord();
                Vector2 uv3 = V3.GetSphereCoord();
                this.fixColorStrip(ref uv1, ref uv2, ref uv3);

                if (perFaceLighting)
                {
                    Vector3 dir    = Vector3.Cross(V2 - V1, V3 - V1);
                    Vector3 normal = Vector3.Normalize(dir);

                    verticesList.Add(new TexturedVertex(V1, uv1, Color4.White, normal));
                    verticesList.Add(new TexturedVertex(V2, uv2, Color4.White, normal));
                    verticesList.Add(new TexturedVertex(V3, uv3, Color4.White, normal));
                }
                else
                {
                    Vector3 normalV1 = V1.Normalized();
                    normalV1 = Vector3.Divide(normalV1, normalV1.Length);
                    Vector3 normalV2 = V2.Normalized();
                    normalV2 = Vector3.Divide(normalV2, normalV2.Length);
                    Vector3 normalV3 = V3.Normalized();
                    normalV3 = Vector3.Divide(normalV3, normalV3.Length);

                    verticesList.Add(new TexturedVertex(V1, uv1, Color4.White, normalV1));
                    verticesList.Add(new TexturedVertex(V2, uv2, Color4.White, normalV2));
                    verticesList.Add(new TexturedVertex(V3, uv3, Color4.White, normalV3));
                }
            }

            Mesh.Primitive shapePrimitive = new Mesh.Primitive(verticesList);
            this.Meshes[0] = new Mesh(shapePrimitive);
            if (diffuseTexture != null)
            {
                if (specularTexture != null)
                {
                    this.Materials[0] = new Material(0, diffuseTexture, specularTexture, 2);
                }
                else
                {
                    this.Materials[0] = new Material(0, diffuseTexture, Color4.LightGoldenrodYellow, 2);
                }
                this.Materials[0].AmbientColor = Color4.DarkBlue;
            }
            else
            {
                this.Materials[0] = new Material(0, Color4.Blue, Color4.White, 2);
                this.Materials[0].AmbientColor = Color4.Blue;
            }
            Logger.Info("Completed sphere generation! Generated vertices: " + verticesList.Count);
        }
Exemplo n.º 4
0
        private Mesh getMesh(JObject nodeJson)
        {
            int     meshId   = (int)nodeJson["mesh"];
            JObject meshJson = (JObject)_json["meshes"][meshId];

            List <Mesh.Primitive> primitives = new List <Mesh.Primitive>();

            foreach (JObject primitiveJson in (JArray)meshJson["primitives"])
            {
                // Material
                int materialId = (int)primitiveJson["material"];

                // Geometry
                if (primitiveJson["mode"] == null || (GLTFPrimitiveMode)(int)primitiveJson["mode"] != GLTFPrimitiveMode.TRIANGLES)
                {
                    throw new Exception($"Found unsupported primitive mode {(int)primitiveJson["mode"]} for {meshId}, only TRIANGLES (4) is supported.");
                }

                int positionAttributeAccessorId = (int)primitiveJson["attributes"]["POSITION"];
                var positionAccessor            = _json["accessors"][positionAttributeAccessorId];

                int verticesCount = (int)positionAccessor["count"];

                TexturedVertex[] staticVertices = new TexturedVertex[verticesCount];

                JToken       accessor, bufferView;
                int          sourceOffset, stride;
                BinaryReader buffer;

                // Position
                accessor = positionAccessor;
                if ((GLTFConst)(int)accessor["componentType"] != GLTFConst.FLOAT)
                {
                    throw new Exception($"Found unexpected component type {(int)accessor["componentType"]} for POSITION attribute of {meshId}, FLOAT was expected.");
                }

                bufferView = _json["bufferViews"][(int)accessor["bufferView"]];
                int accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                int bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                sourceOffset = bufferViewByteOffset + accessorByteOffset;
                stride       = (bufferView["byteStride"] != null) ? (int)bufferView["byteStride"] : sizeof(float) * 3;

                buffer = _buffers[(int)bufferView["buffer"]];

                for (var i = 0; i < verticesCount; i++)
                {
                    buffer.BaseStream.Position = sourceOffset + stride * i;

                    var position = new Vector3(buffer.ReadSingle(), buffer.ReadSingle(), buffer.ReadSingle());
                    staticVertices[i].Position = position;
                    staticVertices[i].Color    = Color4.White;
                }

                // Normals
                int normalAttributeAccessorId = (int)primitiveJson["attributes"]["NORMAL"];
                var normalAccessor            = _json["accessors"][normalAttributeAccessorId];
                accessor = normalAccessor;
                if ((GLTFConst)(int)accessor["componentType"] != GLTFConst.FLOAT)
                {
                    throw new Exception($"Found unexpected component type {(int)accessor["componentType"]} for NORMAL attribute of {meshId}, FLOAT was expected.");
                }

                bufferView           = _json["bufferViews"][(int)accessor["bufferView"]];
                accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                sourceOffset         = bufferViewByteOffset + accessorByteOffset;
                stride = (bufferView["byteStride"] != null) ? (int)bufferView["byteStride"] : sizeof(float) * 3;

                buffer = _buffers[(int)bufferView["buffer"]];

                for (var i = 0; i < verticesCount; i++)
                {
                    buffer.BaseStream.Position = sourceOffset + stride * i;

                    staticVertices[i].Normal = new Vector3(buffer.ReadSingle(), buffer.ReadSingle(), buffer.ReadSingle());
                }

                // Texture coordinates
                if (primitiveJson["attributes"]["TEXCOORD_0"] != null)
                {
                    int attributeAccessorId = (int)primitiveJson["attributes"]["TEXCOORD_0"];

                    if (primitiveJson["attributes"]["TEXCOORD_1"] != null)
                    {
                        throw new Exception("Dual textures primitives are not supported");
                    }

                    accessor = _json["accessors"][attributeAccessorId];
                    if ((GLTFConst)(int)accessor["componentType"] != GLTFConst.FLOAT)
                    {
                        throw new Exception($"Found unexpected component type {(int)accessor["componentType"]} for TEXCOORD_0 attribute of {meshId}, FLOAT was expected.");
                    }

                    bufferView           = _json["bufferViews"][(int)accessor["bufferView"]];
                    accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                    bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                    sourceOffset         = bufferViewByteOffset + accessorByteOffset;
                    stride = (bufferView["byteStride"] != null) ? (int)bufferView["byteStride"] : sizeof(float) * 2;

                    buffer = _buffers[(int)bufferView["buffer"]];

                    for (var i = 0; i < verticesCount; i++)
                    {
                        buffer.BaseStream.Position = sourceOffset + stride * i;

                        staticVertices[i].TextureCoord = new Vector2(buffer.ReadSingle(), buffer.ReadSingle());
                    }
                }

                // Indices
                ushort[] indices = new ushort[0];
                if (primitiveJson["indices"] != null)
                {
                    int indicesAccessorId = (int)primitiveJson["indices"];
                    var indicesAccessor   = _json["accessors"][indicesAccessorId];

                    indices  = new ushort[(int)indicesAccessor["count"]];
                    accessor = indicesAccessor;

                    bufferView           = _json["bufferViews"][(int)accessor["bufferView"]];
                    accessorByteOffset   = (accessor["byteOffset"] != null) ? (int)accessor["byteOffset"] : 0;
                    bufferViewByteOffset = (bufferView["byteOffset"] != null) ? (int)bufferView["byteOffset"] : 0;
                    sourceOffset         = bufferViewByteOffset + accessorByteOffset;

                    buffer = _buffers[(int)bufferView["buffer"]];

                    for (var i = 0; i < indices.Length; i++)
                    {
                        buffer.BaseStream.Position = sourceOffset + sizeof(ushort) * i;
                        indices[i] = buffer.ReadUInt16();
                    }
                }

                Mesh.Primitive primitive = new Mesh.Primitive(staticVertices.ToList(), indices.ToList());
                primitive.MaterialModelId = materialId;
                primitives.Add(primitive);
            }

            Mesh mesh = new Mesh(primitives.ToArray());

            if (nodeJson["matrix"] != null)
            {
                JArray  m             = (JArray)nodeJson["matrix"];
                Matrix4 initialMatrix = new Matrix4((float)m[0], (float)m[1], (float)m[2], (float)m[3], (float)m[4], (float)m[5], (float)m[6], (float)m[7], (float)m[8], (float)m[9], (float)m[10], (float)m[11], (float)m[12], (float)m[13], (float)m[14], (float)m[15]);
                mesh.LocalMatrix = initialMatrix;
            }

            if (nodeJson["name"] != null)
            {
                mesh.Name = (string)nodeJson["name"];
            }

            return(mesh);
        }