Exemple #1
0
        internal void RemoveObject(Object3D o)
        {
            var light = o as Light;
            if (light != null)
            {
                lights.Remove(light);

                var directional = light as DirectionalLight;
                if (directional != null) throw new NotImplementedException();
            }
            else
            {
                var camera = o as Camera;

                if (camera == null)
                {
                    objectsRemoved.Add(o);

                    // check if previously added
                    objectsAdded.Remove(o);
                }
            }

            foreach (var c in o.Children) RemoveObject(c);
        }
Exemple #2
0
        public void AddObject(Object3D o)
        {
            var light = o as Light;

            if (light != null)
            {
                if (!lights.Contains(light)) lights.Add(light);

                var targetLight = light as HasTarget;
            }
            else
            {
                var camera = o as Camera;
                var bone = o as Bone;

                if (!(camera != null || bone != null))
                {
                    objectsAdded.Add(o);

                    // check if previously removed
                    objectsRemoved.Remove(o);
                }
            }

            //dispatchEvent( { type: 'objectAdded', object: object } );
            //object.dispatchEvent( { type: 'addedToScene', scene: this } );

            foreach (var c in o.Children) AddObject(c);
        }
Exemple #3
0
 public bool IntersectsObject(Object3D o)
 {
     var geometry = o.geometry;
     if (geometry.BoundingSphere == Sphere.Empty) geometry.ComputeBoundingSphere();
     var sphere = geometry.BoundingSphere;
     sphere.Apply(o.matrixWorld);
     return IntersectsSphere(sphere);
 }
Exemple #4
0
 public IntersectionInfo(float distance, Vector3 intersectionPoint, Face3 face, int faceIndex, Object3D o)
 {
     Distance = distance;
     IntersectionPoint = intersectionPoint;
     Face = face;
     FaceIndex = faceIndex;
     Object = o;
 }
Exemple #5
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;
 }
 // a helper to show the world-axis-aligned bounding box for an object
 public static BoundingBoxHelper Create(Renderer renderer, Object3D o, Color? color = null)
 {
     var c = color.HasValue ? color.Value : new Color(0x888888);
     var geo = new BoxGeometry(1, 1, 1);
     var mat = new MeshBasicMaterial(renderer)
     {
         Diffuse = c,
         UseWireframe = true
     };
     var boundingBoxHelper = new BoundingBoxHelper(geo, mat);
     boundingBoxHelper.o = o;
     boundingBoxHelper.box = Box3.Empty;
     return boundingBoxHelper;
 }
Exemple #7
0
        public IEnumerable<IntersectionInfo> IntersectObject(Object3D o, bool recursive)
        {
            List<IntersectionInfo> intersects = new List<IntersectionInfo>();

            o.Raycast(this, intersects);

            if (recursive)
            {
                foreach (var c in o.Children)
                {
                    var subresults = IntersectObject(c, true);

                    if (subresults != null)
                    {
                        intersects.AddRange(subresults);
                    }
                }
            }

            intersects.Sort(descendingDistanceSort);
            return intersects;
        }
Exemple #8
0
        private void ProjectObject(Scene scene, Object3D o, Camera shadowCamera)
        {
            if (o.IsVisible)
            {
                List<Scene.BufferInfo> glObjects;
                if (scene.glObjects.TryGetValue(o.Id, out glObjects))
                {
                    if (o.DoesCastShadow && (!o.frustumCulled || frustum.IntersectsObject(o)))
                    {
                        foreach(var glObject in glObjects)
                        { 
                            o.modelViewMatrix.MultiplyMatrices(shadowCamera.matrixWorldInverse, o.matrixWorld);
                            renderList.Add(glObject);
                        }
                    }

                    foreach (var c in o.Children) ProjectObject(scene, c, shadowCamera);
                }
            }
        }
Exemple #9
0
        private static void Init()
        {
            mediaPath = Path.GetFullPath("../../../../../js/r68/examples/");
            texturesPath = Path.Combine(mediaPath, "textures");
            renderer = new Renderer();

            scene = new Scene()
            {
                Fog = new FogLinear(Color.Black, 1500,2100)
            };

            camera = new PerspectiveCamera(renderer, 60, 1, 2100)
            {
                Position = new Vector3(0, 0, 1500)
            };


            cameraOrtho = new OrthographicCamera(renderer, 1, 10000)
        {
            Position = new Vector3(0, 0, 150)
        };
            sceneOrtho = new Scene();

            //var amount = 200;
            //var radius = 500;


            sceneOrtho.Add(new Mesh(new SphereGeometry(100, 50, 50), new MeshBasicMaterial(renderer) { Diffuse = Color.Red}));
            

            var group = new Object3D();

            var materialA = new SpriteMaterial(renderer)
            {
                DiffuseMap = new Texture(Path.Combine(texturesPath, "sprite0.png")),
                Diffuse = Color.White,
                UseFog = true,
            };

            spriteTL = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width,materialA.DiffuseMap.Resolution.Height,1),                
            };
            sceneOrtho.Add(spriteTL);

            spriteTR = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteTR);

            spriteBL = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteBL);

            spriteBR = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteBR);

            spriteC = new Sprite(renderer, materialA)
            {
                Scale = new Vector3(materialA.DiffuseMap.Resolution.Width, materialA.DiffuseMap.Resolution.Height, 1),
            };
            sceneOrtho.Add(spriteC);

            UpdateHUDSprites();

            var materialB = new SpriteMaterial(renderer)
            {
                DiffuseMap = new Texture(Path.Combine(texturesPath, "sprite1.png")),
                Diffuse = Color.White,
                UseFog = true,
            };

            var materialC = new SpriteMaterial(renderer)
            {
                DiffuseMap = new Texture(Path.Combine(texturesPath, "sprite2.png")),
                Diffuse = Color.White,
                UseFog = true,
            };

            mediaPath = Path.Combine(mediaPath, "../../tests/");

        }
Exemple #10
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;
        }