Esempio n. 1
0
 private CameraHelper(Camera camera, Geometry geometry, LineBasicMaterial material)
     : base(geometry, material, LineType.Pieces)
 {
     this.camera = camera;
     this.matrixWorld = camera.matrixWorld;
     this.matrixAutoUpdate = false;
 }
Esempio n. 2
0
 public static BoxHelper Create(Renderer renderer, Object3D o)
 {
     var mat = new LineBasicMaterial(renderer)
     {
         Diffuse = new Color(0xffff00)
     };
     var geo = new Geometry();
     var boxHelper = new BoxHelper(geo, mat);
     boxHelper.o = o;
     boxHelper.Update();
     return boxHelper;
 }
Esempio n. 3
0
        public Sprite(Renderer renderer, SpriteMaterial material)
        {
            if(geo == null)
            {
                geo = new Geometry();
                geo.vertices.Add(new Vector3(-0.5f, -0.5f, 0));
                geo.vertices.Add(new Vector3(0.5f, -0.5f, 0));
                geo.vertices.Add(new Vector3(0.5f, 0.5f, 0));
            }

            geometry = geo;
            Material = material ?? new SpriteMaterial(renderer);
        }
Esempio n. 4
0
        public static ArrowHelper Create(Renderer renderer, Vector3 dir, Vector3 origin, float length = 1, Color? color = null, float? headLength = null, float? headWidth = null)
        {
            var c = color.HasValue ? color.Value : new Color(0xffff00);

            if (lineGeometry == null)
            {
                lineGeometry = new Geometry();
                lineGeometry.vertices.Add(Vector3.Zero);
                lineGeometry.vertices.Add(Vector3.UnitY);
                lineGeometry.vertexColors = new List<Color>()
                {
                    c,
                    c,
                };
            }

            if (coneGeometry == null)
            {
                coneGeometry = new CylinderGeometry(0, 0.5f, 1, 5, 1);
                var m = Matrix4.MakeTranslation(0, -0.5f, 0);
                coneGeometry.Apply(m);
            }

            var arrowHelper = new ArrowHelper();

            // dir is assumed to be normalized
            
            var head = headLength.HasValue ? headLength.Value : 0.2f * length;
            var width = headWidth.HasValue ? headWidth.Value : 0.2f * head;

            arrowHelper.Position = origin;
            arrowHelper.line = new Line(lineGeometry, new LineBasicMaterial(renderer)
            {
                Diffuse = c,
                VertexColors = Net.Renderers.VertexColorMode.Vertex
            });
            arrowHelper.line.matrixAutoUpdate = false;
            arrowHelper.Add(arrowHelper.line);

            arrowHelper.cone = new Mesh(coneGeometry, new MeshBasicMaterial(renderer)
            {
                Diffuse = c
            });
            arrowHelper.cone.matrixAutoUpdate = false;
            arrowHelper.Add(arrowHelper.cone);

            arrowHelper.SetDirection(dir.Normalized());
            arrowHelper.SetLength(length, head, width);

            return arrowHelper;
        }
Esempio n. 5
0
        public override LoadedInfo Parse(Renderer renderer, JObject json, string texturePath)
        {
            var geometry = new Geometry();
            throw new NotImplementedException(); //geometry.faces = json["indices"];
            throw new NotImplementedException(); //geometry.vertices = json.vertices;
            throw new NotImplementedException(); //geometry.normals = json.normals;
            throw new NotImplementedException(); //geometry.uvs = json.uvs;

            throw new NotImplementedException();
            //Sphere boundingSphere = null;
            //var boundingSphere = json.boundingSphere;
            //if ( boundingSphere !== undefined ) {
            //geometry.boundingSphere = new Sphere(
            //	new THREE.Vector3().fromArray( boundingSphere.center !== undefined ? boundingSphere.center : [ 0, 0, 0 ] ),
            //	boundingSphere.radius
            //);
        }
Esempio n. 6
0
        public static AxisHelper Create(Renderer renderer, float size = 1)
        {
            var geometry = new Geometry()
            {
                vertices = new List<Vector3>()
                {
                    Vector3.Zero,  new Vector3(size, 0, 0),
		     Vector3.Zero,  new Vector3(0, size, 0),
		     Vector3.Zero,  new Vector3(0, 0, size)
                },
                vertexColors = new List<Color>()
                {
                    new Color(1, 0, 0),  new Color(1, 0.5f, 0.5f),
		            new Color(0, 1, 0),  new Color(0.5f, 1, 0.5f),
		            new Color(0, 0, 1),  new Color(0.5f, 0.5f, 1),
                }
            };

            var axis = new AxisHelper(geometry, new LineBasicMaterial(renderer)
            {
                VertexColors = Net.Renderers.VertexColorMode.Vertex
            });
            return axis;
        }
Esempio n. 7
0
        public override LoadedInfo Parse(Renderer renderer, JObject json, String texturePath)
        {
            var geometry = new Geometry();

            var scaleData = json["scale"];
            var scale = scaleData != null ? 1 / scaleData.Value<float>() : 1;

            ParseModel(json, geometry, scale);
            ParseSkin(json, geometry);
            ParseMorphing(json, geometry, scale);

            geometry.ComputeNormals();
            geometry.ComputeBoundingSphere();

            var info = new LoadedInfo()
            {
                Geometry = geometry
            };

            var materialsJson = json["materials"];
            if (materialsJson != null)
            {
                var materialsData = materialsJson.ToArray();

                if (materialsData.Length != 0)
                {
                    var materials = InitMaterials(renderer, materialsData, texturePath);

                    if (NeedsTangents(materials)) geometry.ComputeTangents();

                    info.Materials.AddRange(materials);
                }
            }

            return info;
        }
Esempio n. 8
0
        private BoxHelper(Geometry geometry, LineBasicMaterial material)
            : base(geometry, material, LineType.Pieces)
        {

        }
Esempio n. 9
0
 public Mesh(Geometry geometry, Material material)
 {
     Debug.Assert(material is CustomShaderMaterial || material is MeshBasicMaterial);
     this.geometry = geometry;
     this.Material = material;
 }
Esempio n. 10
0
        private static Line CreateHilbertCube(Renderer renderer, Scene scene)
        {
            var points = Mathf.Hilbert3D(Vector3.Zero, 0.25f, 2);
            var spline = new SplineCurve3(points);
            var geometrySpline = new Geometry();
            geometrySpline.vertexColors = new List<Color>();
            var subdivisions = 6;
            var count = points.Count * subdivisions;
            for (var i = 0; i < count; i++)
            {
                var index = i / (float)(points.Count * subdivisions);
                var position = spline.InterpolatedPoint(index);
                geometrySpline.vertices.Add(position);

                var hsl = Color.FromHSV(index, 1, 1);
                geometrySpline.vertexColors.Add(hsl);
            }

            var lines = new Line(geometrySpline, new LineDashedMaterial(renderer)
            {
                //DiffuseColor = Color.Cornflowerblue,
                VertexColors = VertexColorMode.Vertex,
                LineWidth = 0.01f,
                DashSize = 0.006f,
                GapSize = 0.002f,
            });
            return lines;
        }
Esempio n. 11
0
 public Line(Geometry geometry, LineBasicMaterial material, LineType type = LineType.Strip)
 {
     this.geometry = geometry;
     Material = material;
     this.Type = type;
 }
Esempio n. 12
0
        public static Object3D Parse(Renderer renderer, string path, Action<string> mtllibCallback = null)
        {
            ObjMtlLoader.renderer = renderer;

            var text = File.ReadAllText(path);

            var vector = new Func<string, string, string, Vector3>((x, y, z) =>
            {
                var vx = Convert.ToSingle(x);
                var vy = Convert.ToSingle(y);
                var vz = Convert.ToSingle(z);
                return new Vector3(vx, vy, vz);
            });

            var uv = new Func<string, string, Vector2>((u, v) =>
            {
                var vu = Convert.ToSingle(u);
                var vv = Convert.ToSingle(v);
                return new Vector2(vu, vv);
            });

            var parseVertexIndex = new Func<string, int>((indexString) =>
            {
                var index = Convert.ToInt32(indexString);
                return index >= 0 ? index - 1 : index + vertexIndicies.Count;
            });

            var parseNormalIndex = new Func<string, int>((indexString) =>
            {
                var index = Convert.ToInt32(indexString);
                return index >= 0 ? index - 1 : index + normals.Count;
            });

            var parseUVIndex = new Func<string, int>((indexString) =>
            {
                var index = Convert.ToInt32(indexString);
                return index >= 0 ? index - 1 : index + uvs2.Count;
            });

            var add_face = new Action<string, string, string, List<string>>((a, b, c, normals_inds) =>
            {

                if (normals_inds == null)
                {

                    geometry.faces.Add(new Face3(
                        vertexIndicies[parseVertexIndex(a)] - 1,
                        vertexIndicies[parseVertexIndex(b)] - 1,
                        vertexIndicies[parseVertexIndex(c)] - 1
                    ));
                }
                else
                {
                    geometry.faces.Add(new Face3(
                        vertexIndicies[parseVertexIndex(a)] - 1,
                        vertexIndicies[parseVertexIndex(b)] - 1,
                        vertexIndicies[parseVertexIndex(c)] - 1,
                        normals[parseNormalIndex(normals_inds[0])],
                        normals[parseNormalIndex(normals_inds[1])],
                        normals[parseNormalIndex(normals_inds[2])]));
                }
            });

            var add_uvs = new Action<string, string, string>((a, b, c) =>
            {

                geometry.faceVertexUvs[0].Add(new UVFaceSet(uvs2[parseUVIndex(a)], uvs2[parseUVIndex(b)], uvs2[parseUVIndex(c)]));
            });

            var handle_face_line = new Action<List<string>, List<string>, List<string>>((faces, uvs, normals_inds) =>
            {

                if (faces.Count == 3)
                {

                    add_face(faces[0], faces[1], faces[2], normals_inds);

                    if (uvs != null && uvs.Count > 0)
                    {
                        add_uvs(uvs[0], uvs[1], uvs[2]);
                    }

                }
                else
                {

                    if (normals_inds != null && normals_inds.Count > 0)
                    {
                        add_face(faces[0], faces[1], faces[3], new List<string>() { normals_inds[0], normals_inds[1], normals_inds[3] });
                        add_face(faces[1], faces[2], faces[3], new List<string>() { normals_inds[1], normals_inds[2], normals_inds[3] });

                    }
                    else
                    {
                        add_face(faces[0], faces[1], faces[3], null);
                        add_face(faces[1], faces[2], faces[3], null);

                    }

                    if (uvs != null && uvs.Count > 0)
                    {
                        add_uvs(uvs[0], uvs[1], uvs[3]);
                        add_uvs(uvs[1], uvs[2], uvs[3]);

                    }

                }

            });

            var o = new Object3D();

            var lines = text.Split('\n');

            // create mesh if no objects in text
            if (lines.Where(l => l.StartsWith("o")).Count() == 0)
            {
                geometry = new Geometry();
                material = new MeshBasicMaterial(renderer);
                mesh = new Mesh(geometry, material);
                o.Add(mesh);
            }

            vertexIndicies = new List<int>();
            normals = new List<Vector3>();
            uvs2 = new List<Vector2>();

            // fixes

            //text = text.Replace("\r\n",string.Empty); // handles line continuations \
            foreach (var l in lines)
            {
                if (l.Length == 0 || l[0] == '#') continue;

                var line = l.Trim();
                var result = line.Split(' ','/');

                if (vertex_pattern.IsMatch(line))
                {
                    // ["v 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
                    geometry.vertices.Add(
                        vector(
                            result[1], result[2], result[3]
                        )
                    );
                    vertexIndicies.Add(geometry.vertices.Count);
                }

                else if (normal_pattern.IsMatch(line))
                {
                    // ["vn 1.0 2.0 3.0", "1.0", "2.0", "3.0"]
                    normals.Add(vector(result[1], result[2], result[3]));
                }
                else if (uv_pattern.IsMatch(line))
                {
                    // ["vt 0.1 0.2", "0.1", "0.2"]
                    uvs2.Add(
                        uv(
                            result[1], result[2]
                        )
                    );
                }
                else if (face_pattern1.IsMatch(line))
                {
                    // ["f 1 2 3", "1", "2", "3", undefined]WDEAADEAWAAAADD
                    if (result.Length == 4)
                    {
                        handle_face_line(new List<string>() { result[1], result[2], result[3] }, null, null);
                    }
                    else
                    {
                        handle_face_line(new List<string>() { result[1], result[2], result[3], result[4] }, null, null);
                    }
                }
                else if (face_pattern2.IsMatch(line))
                {
                    // ["f 1/1 2/2 3/3", " 1/1", "1", "1", " 2/2", "2", "2", " 3/3", "3", "3", undefined, undefined, undefined]
                    if (result.Length == 7)
                    {
                        handle_face_line(
                            new List<string>() { result[1], result[3], result[5] }, //faces
                            new List<string>() { result[2], result[4], result[6] }, //uv
                            null
                        );
                    }
                    else
                    {
                        handle_face_line(
                            new List<string>() { result[1], result[3], result[5], result[7] }, //faces
                            new List<string>() { result[2], result[4], result[6], result[8] }, //uv
                            null
                        );
                    }
                }
                else if (face_pattern3.IsMatch(line))
                {
                    if (result.Length == 10)
                    {
                        handle_face_line(
                           new List<string>() { result[1], result[4], result[7], }, //faces
                           new List<string>() { result[2], result[5], result[8], }, //uv
                           new List<string>() { result[3], result[6], result[9], } //normal
                       );
                    }
                    else
                    {
                        // ["f 1/1/1 2/2/2 3/3/3", " 1/1/1", "1", "1", "1", " 2/2/2", "2", "2", "2", " 3/3/3", "3", "3", "3", undefined, undefined, undefined, undefined]
                        handle_face_line(
                            new List<string>() { result[1], result[4], result[7], result[10] }, //faces
                            new List<string>() { result[2], result[5], result[8], result[11] }, //uv
                            new List<string>() { result[3], result[6], result[9], result[12] } //normal
                        );
                    }
                }
                else if (face_pattern4.IsMatch(line))
                {
                    if (result.Length == 10)
                    {
                        // ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
                        handle_face_line(
                            new List<string>() { result[1], result[4], result[7] }, //faces
                            null, //uv
                            new List<string>() { result[3], result[6], result[9] } //normal
                        );
                    }
                    else
                    {
                        // ["f 1//1 2//2 3//3", " 1//1", "1", "1", " 2//2", "2", "2", " 3//3", "3", "3", undefined, undefined, undefined]
                        handle_face_line(
                            new List<string>() { result[1], result[4], result[7], result[10] }, //faces
                            null, //uv
                            new List<string>() { result[3], result[6], result[9], result[12] } //normal
                        );
                    }
                }
                else if (objectTest.IsMatch(line))
                {
                    geometry = new Geometry();
                    material = new MeshBasicMaterial(renderer);

                    mesh = new Mesh(geometry, material);
                    mesh.Name = line.Substring(2).Trim();
                    o.Add(mesh);
                }
                else if (groupTest.IsMatch(line))
                {
                    // group
                }
                else if (useMTLTest.IsMatch(line))
                {
                    // material
                    material = materials[result[1].Trim()];
                    mesh.Material = material;
                }
                else if (mtlTest.IsMatch(line))
                {
                    // mtl file
                    var objPath = Path.GetDirectoryName(path);
                    var fullPath = Path.Combine(objPath, result[1].Trim());
                    ParseMaterials(fullPath);
                }
                else if (smoothShadingTest.IsMatch(line))
                {
                    // smooth shading
                }
                else
                {
                    throw new NotSupportedException(line);
                }
            }

            var children = o.Children;
            foreach (var c in o.Children)
            {
                var geometry = c.geometry;
                geometry.ComputeNormals();
                geometry.ComputeBoundingSphere();
            }

            return o;
        }
Esempio n. 13
0
        public static CameraHelper Create(Renderer renderer, Camera camera)
        {
            var pointMap = new Dictionary<string, List<int>>();

            var geometry = new Geometry();
            var material = new LineBasicMaterial(renderer)
            {
                Diffuse = Color.White,
                VertexColors = VertexColorMode.Vertex
            };
            var cameraHelper = new CameraHelper(camera, geometry, material);

            //Colors
            var hexFrustum = new Color(0xffaa00);
            var hexCone = new Color(0xff0000);
            var hexUp = new Color(0x00aaff);
            var hexTarget = new Color(0xffffff);
            var hexCross = new Color(0x333333);

            // near
            cameraHelper.AddLine("n1", "n2", hexFrustum);
            cameraHelper.AddLine("n2", "n4", hexFrustum);
            cameraHelper.AddLine("n4", "n3", hexFrustum);
            cameraHelper.AddLine("n3", "n1", hexFrustum);
            
            // far
            cameraHelper.AddLine("f1", "f2", hexFrustum);
            cameraHelper.AddLine("f2", "f4", hexFrustum);
            cameraHelper.AddLine("f4", "f3", hexFrustum);
            cameraHelper.AddLine("f3", "f1", hexFrustum);
            
            // sides
            cameraHelper.AddLine("n1", "f1", hexFrustum);
            cameraHelper.AddLine("n2", "f2", hexFrustum);
            cameraHelper.AddLine("n3", "f3", hexFrustum);
            cameraHelper.AddLine("n4", "f4", hexFrustum);
            
            // cone
            cameraHelper.AddLine("p", "n1", hexCone);
            cameraHelper.AddLine("p", "n2", hexCone);
            cameraHelper.AddLine("p", "n3", hexCone);
            cameraHelper.AddLine("p", "n4", hexCone);
            
            // up
            cameraHelper.AddLine("u1", "u2", hexUp);
            cameraHelper.AddLine("u2", "u3", hexUp);
            cameraHelper.AddLine("u3", "u1", hexUp);
            
            // target
            cameraHelper.AddLine("c", "t", hexTarget);
            cameraHelper.AddLine("p", "c", hexCross);
            
            // cross
            cameraHelper.AddLine("cn1", "cn2", hexCross);
            cameraHelper.AddLine("cn3", "cn4", hexCross);
            cameraHelper.AddLine("cf1", "cf2", hexCross);
            cameraHelper.AddLine("cf3", "cf4", hexCross);

            cameraHelper.Update();
            return cameraHelper;
        }
Esempio n. 14
0
        private void ParseMorphing(JObject json, Geometry geometry, float scale)
        {
            var morphTargetsData = json["morphTargets"];
            
            if (morphTargetsData != null)
            {
                var morphtargets = morphTargetsData.ToArray();
                if (morphtargets.Length > 0)
                {
                    for (var i = 0; i < morphtargets.Length; i++)
                    {
                        var morphTargetData = morphtargets[i];


                        var morphTarget = new MorphTargetInfo()
                            {
                                Name = morphTargetData[i]["name"].Value<string>()
                            };
                        geometry.MorphTargets.Add(morphTarget);

                        var dstVertices = morphTarget.Vertices;
                        var srcVertices = morphTargetData["vertices"].Values<float>().ToArray();

                        for (var v = 0; v < srcVertices.Length; v += 3)
                        {
                            var vertex = new Vector3();
                            vertex.x = srcVertices[v] * scale;
                            vertex.y = srcVertices[v + 1] * scale;
                            vertex.z = srcVertices[v + 2] * scale;
                            dstVertices.Add(vertex);
                        }
                    }
                }
            }

            var morphColorsData = json["morphColors"];
            if (morphColorsData != null)
            {
                geometry.MorphColors = new List<MorphColorInfo>();
                var morphColors = morphColorsData.ToArray();
                for (var i = 0; i < morphColors.Length; i++)
                {
                    var colorData = morphColors[i];
                    var morphColor = new MorphColorInfo()
                    {
                        Name = colorData["name"].Value<string>()
                    };
                    geometry.MorphColors.Add(morphColor);

                    var dstColors = morphColor.Colors;
                    var srcColors = colorData["colors"].Values<float>().ToArray();

                    for (var c = 0; c < srcColors.Length; c += 3)
                    {
                        var r = srcColors[c];
                        var g = srcColors[c + 1];
                        var b = srcColors[c + 2];
                        var color = new Color(r, g, b);
                        dstColors.Add(color);
                    }
                }
            }
        }
Esempio n. 15
0
        private void ParseModel(JObject json, Geometry geometry,  float scale)
        {
            var verticesData = json["vertices"].Values<float>().ToArray();
            var facesData = json["faces"].Values<int>().ToArray();
            var normalsData = json["normals"].Values<float>().ToArray();
            var colorsData = json["colors"].Values<uint>().ToArray();
            
            var uvsData = new List<List<float>>();
            foreach (var array in json["uvs"])
            {
                var uvData = array.Values<float>().ToArray();
                uvsData.Add(new List<float>(uvData));
            }

            var nUvLayers = 0;
            
            if(uvsData.Count > 0)
            {
                geometry.faceVertexUvs.Clear();
                // disregard empty arrays
                foreach (var row in uvsData)
                {
                    nUvLayers++;
                    geometry.faceVertexUvs.Add(new List<UVFaceSet>());
                }
            }

           
            var offset = 0;
            while (offset < verticesData.Length)
            {
                var x = verticesData[offset++] * scale;
                var y = verticesData[offset++] * scale;
                var z = verticesData[offset++] * scale;
                var v = new Vector3(x, y, z);
                geometry.vertices.Add(v);
            }

            offset = 0;
            while (offset < facesData.Length)
            {
                var type = facesData[offset++];
                var isQuad = IsBitSet(type, 0);
                var hasMaterial = IsBitSet(type, 1);
                var hasFaceVertexUv = IsBitSet(type, 3);
                var hasFaceNormal = IsBitSet(type, 4);
                var hasFaceVertexNormal = IsBitSet(type, 5);
                var hasFaceColor = IsBitSet(type, 6);
                var hasFaceVertexColor = IsBitSet(type, 7);

                //Debug.WriteLine("type:{0} bits( IsQuad:{1} Material:{2} FaceVertexUv:{3} FaceNormal:{4} FaceVertexNormal:{5} FaceColor:{6} FaceVertexColor:{7}", type, isQuad, hasMaterial, hasFaceVertexUv, hasFaceNormal, hasFaceVertexNormal, hasFaceColor, hasFaceVertexColor);

                if (isQuad != 0)
                {
                    var a = facesData[offset++];
                    var b = facesData[offset++];
                    var c = facesData[offset++];
                    var d = facesData[offset++];
                    var faceA = new Face3(a, b, d);
                    var faceB = new Face3(b, c, d);

                    if (hasMaterial != 0)
                    {
                        var materialIndex = facesData[offset++];
                    }

                    if (hasFaceVertexUv != 0)
                    {
                        for (var i = 0; i < nUvLayers; i++)
                        {
                            var uvLayer = uvsData[ i ];

                            var uvIndexA = facesData[offset++];
                            var uvIndexB = facesData[offset++];
                            var uvIndexC = facesData[offset++];
                            var uvIndexD = facesData[offset++];

                            var uA = uvLayer[uvIndexA * 2];
                            var vA = uvLayer[uvIndexA * 2 + 1];
                            var uvA = new Vector2(uA, vA);

                            var uB = uvLayer[uvIndexB * 2];
                            var vB = uvLayer[uvIndexB * 2 + 1];
                            var uvB = new Vector2(uB, vB);

                            var uC = uvLayer[uvIndexC * 2];
                            var vC = uvLayer[uvIndexC * 2 + 1];
                            var uvC = new Vector2(uC, vC);

                            var uD = uvLayer[uvIndexD * 2];
                            var vD = uvLayer[uvIndexD * 2 + 1];
                            var uvD = new Vector2(uD, vD);


                            geometry.faceVertexUvs[i].Add(new UVFaceSet(uvA,uvB,uvD));
                            geometry.faceVertexUvs[i].Add(new UVFaceSet(uvB,uvC,uvD));
                        }
                    }

                    if (hasFaceNormal != 0)
                    {
                        var normalIndex = facesData[offset++] * 3;

                        var x = normalsData[normalIndex++];
                        var y = normalsData[normalIndex++];
                        var z = normalsData[normalIndex];
                        var n = new Vector3(x, y, z); ;
                        faceA.NormalA = n;
                        faceA.NormalB = n;
                        faceA.NormalC = n;
                        faceB.NormalA = n;
                        faceB.NormalB = n;
                        faceB.NormalC = n;
                    }

                    if (hasFaceVertexNormal != 0)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            var normalIndex = facesData[offset++] * 3;
                            var x = normalsData[normalIndex++];
                            var y = normalsData[normalIndex++];
                            var z = normalsData[normalIndex];
                            var n = new Vector3(x, y, z);

                            switch(i)
                            {
                                case 0: faceA.NormalA = n; break;
                                case 1: faceA.NormalB = n; faceB.NormalA = n; break;
                                case 2: faceB.NormalB = n; break;
                                case 3: faceA.NormalC = n; faceB.NormalC = n; break;
                            }
                        }
                    }

                    if (hasFaceColor != 0)
                    {
                        var colorIndex = facesData[offset++];
                        var hex = colorsData[colorIndex];
                        var color = new Color(hex);
                        faceA.ColorA = color;
                        faceA.ColorB = color;
                        faceA.ColorC = color;
                        faceB.ColorA = color;
                        faceB.ColorB = color;
                        faceB.ColorC = color;
                    }

                    if (hasFaceVertexColor != 0)
                    {
                        for (var i = 0; i < 4; i++)
                        {
                            var colorIndex = facesData[offset++];
                            var hex = colorsData[colorIndex];
                            var color = new Color(hex);

                            switch (i)
                            {
                                case 0: faceA.ColorA = color; break;
                                case 1: faceA.ColorB = color; faceB.ColorA = color; break;
                                case 2: faceB.ColorB = color; break;
                                case 3: faceA.ColorC = color; faceB.ColorC = color; break;
                            }
                        }
                    }

                    geometry.faces.Add(faceA);
                    geometry.faces.Add(faceB);
                }
                else
                {
                    var a = facesData[offset++];
                    var b = facesData[offset++];
                    var c = facesData[offset++];
                    var face = new Face3(a, b, c);

                    if (hasFaceVertexUv != 0)
                    {
                        for (var i = 0; i < nUvLayers; i++)
                        {
                            var uvLayer = uvsData[i];

                            var uvIndexA = facesData[offset++];
                            var uvIndexB = facesData[offset++];
                            var uvIndexC = facesData[offset++];

                            var uA = uvLayer[uvIndexA * 2];
                            var vA = uvLayer[uvIndexA * 2 + 1];
                            var uvA = new Vector2(uA, vA);

                            var uB = uvLayer[uvIndexB * 2];
                            var vB = uvLayer[uvIndexB * 2 + 1];
                            var uvB = new Vector2(uB, vB);

                            var uC = uvLayer[uvIndexC * 2];
                            var vC = uvLayer[uvIndexC * 2 + 1];
                            var uvC = new Vector2(uC, vC);


                            geometry.faceVertexUvs[i].Add(new UVFaceSet(uvA, uvB, uvC));
                        }
                    }

                    if (hasFaceNormal != 0)
                    {
                        var normalIndex = facesData[offset++] * 3;
                        var x = normalsData[normalIndex++];
                        var y = normalsData[normalIndex++];
                        var z = normalsData[normalIndex];
                        var n = new Vector3(x, y, z);
                        face.NormalA = n;
                        face.NormalB = n;
                        face.NormalC = n;
                    }

                    if (hasFaceVertexNormal != 0)
                    {
                        for (var i = 0; i < 3; i++)
                        {
                            var normalIndex = facesData[offset++] * 3;
                            var x = normalsData[normalIndex++];
                            var y = normalsData[normalIndex++];
                            var z = normalsData[normalIndex];
                            var n = new Vector3(x, y, z);
                            switch (i)
                            {
                                case 0: face.NormalA = n; break;
                                case 1: face.NormalB = n; break;
                                case 2: face.NormalC = n; break;
                            }
                        }
                    }

                    if (hasFaceColor != 0)
                    {
                        var colorIndex = facesData[offset++];
                        var hex = colorsData[colorIndex];
                        var color = new Color(hex);
                        face.ColorA = color;
                        face.ColorB = color;
                        face.ColorC = color;
                    }

                    if (hasFaceVertexColor != 0)
                    {
                        for (var i = 0; i < 3; i++)
                        {
                            var colorIndex = facesData[offset++];
                            var hex = colorsData[colorIndex];
                            var color = new Color(hex);
                            switch (i)
                            {
                                case 0: face.ColorA = color; break;
                                case 1: face.ColorB = color; break;
                                case 2: face.ColorC = color; break;
                            }
                        }
                    }

                    geometry.faces.Add(face);
                }
            }
        }
Esempio n. 16
0
        private void ParseSkin(JObject json, Geometry geometry)
        {
            var influencesPerVertexData = json["influencesPerVertex"];
            var influencesPerVertex = influencesPerVertexData != null ? influencesPerVertexData.Value<int>() : 2;

            var skinWeightsData = json["skinWeights"];
            if (skinWeightsData != null)
            {
                var skinWeights = skinWeightsData.Values<float>().ToArray();
                if (skinWeights.Length > 0)
                {
                    geometry.SkinWeights = new List<Vector4>();
                    for (var i = 0; i < skinWeights.Length; i += influencesPerVertex)
                    {
                        var x = skinWeights[i];
                        var y = (influencesPerVertex > 1) ? skinWeights[i + 1] : 0;
                        var z = (influencesPerVertex > 2) ? skinWeights[i + 2] : 0;
                        var w = (influencesPerVertex > 3) ? skinWeights[i + 3] : 0;
                        geometry.SkinWeights.Add(new Vector4(x, y, z, w));
                    }
                }
            }

            var skinIndicesData = json["skinIndices"];
            if (skinIndicesData != null)
            {
                var skinIndices = skinIndicesData.Values<int>().ToArray();
                if (skinIndices.Length > 0)
                {
                    geometry.SkinIndices = new List<Vector4>();
                    for (var i = 0; i < skinIndices.Length; i += influencesPerVertex)
                    {
                        var a = skinIndices[i];
                        var b = (influencesPerVertex > 1) ? skinIndices[i + 1] : 0;
                        var c = (influencesPerVertex > 2) ? skinIndices[i + 2] : 0;
                        var d = (influencesPerVertex > 3) ? skinIndices[i + 3] : 0;
                        geometry.SkinIndices.Add(new Vector4(a, b, c, d));
                    }
                }
            }

            var bonesData = json["bones"];
            if (bonesData != null)
            {
                var bones = bonesData.ToArray();

                if (bones.Length > 0)
                {
                    if ((geometry.SkinWeights.Count != geometry.SkinIndices.Count || geometry.SkinIndices.Count != geometry.vertices.Count))
                    {
                        var format = "When skinning, number of vertices ({0}), skinIndices ({1}), and skinWeights ({2}) should match.";
                        var msg = string.Format(format, geometry.vertices.Count, geometry.SkinIndices.Count, geometry.SkinWeights.Count);
                        throw new Exception(msg);
                    }

                    // could change this to json.animations[0] or remove completely
                    var animationData = json["animation"];
                    throw new NotImplementedException();

                    //var animationsData = json["animations"];
                    //if (animationsData != null) throw new NotImplementedException();
                }
            }
        }
Esempio n. 17
0
        private BoundingBoxHelper(Geometry geometry, MeshBasicMaterial material)
            : base(geometry, material)
        {

        }
Esempio n. 18
0
        public SkinnedMesh(Geometry geometry, MeshBasicMaterial material, bool useVertexTexture):base(geometry, material)
        {

        }