Exemple #1
0
        internal override void Raycast(Raycaster raycaster, IList<IntersectionInfo> intersectionsList)
        {
            // Checking boundingSphere distance to ray
            if (geometry.BoundingSphere == null) geometry.ComputeBoundingSphere();

            var sphere = geometry.BoundingSphere;
            sphere.Apply(matrixWorld);

            if (!raycaster.ray.IsIntersectionSphere(sphere)) return;

            // Check boundingBox before continuing
            var inverseMatrix = Matrix4.GetInverse(matrixWorld);
            var ray = raycaster.ray;
            ray.Apply(inverseMatrix);

            if (geometry.BoundingBox == Box3.Empty) geometry.ComputeBoundingBox();
            
            if (!ray.IsIntersectionBox(geometry.BoundingBox)) return;
            
            var material = Material as MeshBasicMaterial;
            for (var f = 0; f < geometry.faces.Count; f++)
            {
                var face = geometry.faces[f];
                var a = geometry.vertices[face.A];
                var b = geometry.vertices[face.B];
                var c = geometry.vertices[face.C];

                if (material.UseMorphTargets)
                {
                    var vA = Vector3.Zero;
                    var vB = Vector3.Zero;
                    var vC = Vector3.Zero;

                    for (var t = 0; t < geometry.MorphTargets.Count; t++)
                    {
                        var influence = morphTargetInfluences[t];
                        if (influence == 0) continue;

                        var targets = geometry.MorphTargets[t].Vertices;

                        vA.x += (targets[face.A].x - a.x) * influence;
                        vA.y += (targets[face.A].y - a.y) * influence;
                        vA.z += (targets[face.A].z - a.z) * influence;

                        vB.x += (targets[face.B].x - b.x) * influence;
                        vB.y += (targets[face.B].y - b.y) * influence;
                        vB.z += (targets[face.B].z - b.z) * influence;

                        vC.x += (targets[face.C].x - c.x) * influence;
                        vC.y += (targets[face.C].y - c.y) * influence;
                        vC.z += (targets[face.C].z - c.z) * influence;
                    }

                    vA.Add(a);
                    vB.Add(b);
                    vC.Add(c);

                    a = vA;
                    b = vB;
                    c = vC;

                }

                Vector3? intersectionPoint = null;
                if (material.Side == Renderers.SideMode.Back) intersectionPoint = ray.IntersectTriangle(c, b, a, true);
                else intersectionPoint = ray.IntersectTriangle(a, b, c, material.Side != Renderers.SideMode.Double);

                if (!intersectionPoint.HasValue) continue;

                intersectionPoint.Value.Apply(matrixWorld);

                var distance = raycaster.ray.origin.DistanceTo(intersectionPoint.Value);

                if (distance < raycaster.Near || distance > raycaster.Far) continue;

                intersectionsList.Add(new IntersectionInfo(distance, intersectionPoint.Value, face, f, this));
            }
        }
        static void Main(string[] args)
        {
            var mediaPath = Path.GetFullPath("../../../../../js/r68/examples/");
            var texturesPath = Path.Combine(mediaPath, "textures");

            var renderer = new Renderer();

            var scene = new Scene()
            {
                //Fog = new FogExp2(Color.Blue, 0.24f)
            };

            var camera = new OrthographicCamera(renderer, -1000, 1000)
            {
                Position = new Vector3(0, 0, 2)
            };

            //// create a point light
            scene.Add(new DirectionalLight(Color.White)
            {
                Target = Vector3.UnitX
            });


            var geometry = new BoxGeometry(20, 20, 20);

            for (var i = 0; i < 2000; i++)
            {
                var o = new Mesh(geometry, new MeshLambertMaterial(renderer) { Diffuse = Color.Random() });
                o.Position = new Vector3(Mathf.RandomF(-400, 400), Mathf.RandomF(-400, 400), Mathf.RandomF(-400, 400));
                o.Rotation = new Euler(Mathf.Tau * Mathf.RandomF(), Mathf.Tau * Mathf.RandomF(), Mathf.Tau * Mathf.RandomF());
                o.Scale = new Vector3(Mathf.RandomF(0.5f, 1.5f), Mathf.RandomF(0.5f, 1.5f), Mathf.RandomF(0.5f, 1.5f));
                scene.Add(o);
            }

            var raycaster = new Raycaster();
            Object3D INTERSECTED = null;
            Color previousColor = Color.White;

            var radius = 100;
            var previousTime = 0f;
            var stopwatch = Stopwatch.StartNew();
            while (!renderer.Done)
            {
                var now = (float)stopwatch.Elapsed.TotalSeconds;
                var deltaTime = now - previousTime;
                previousTime = now;

                var offset = now / 4;
                var sin = Mathf.Sin(offset) * radius;
                var cos = Mathf.Cos(offset) * radius;
                camera.Position = new Vector3(sin, sin, cos);
                camera.LookAt(Vector3.Zero);

                #region FindIntersections
                var vector = Projector.UnprojectVector(new Vector3(renderer.MousePositionNormalized,-1),camera.projectionMatrix, camera.matrixWorld);
                var direction = new Vector3(0, 0, -1);
                direction.TransformDirection(camera.matrixWorld);

                raycaster.Set(vector, direction);

                var intersects = raycaster.IntersectObjects(scene.Children);

                if (intersects != null &&  intersects.Count > 0)
                {
                    var first = intersects[0];

                    if (INTERSECTED != first.Object)
                    {
                        if (INTERSECTED != null)
                        {
                            var basic = INTERSECTED.Material as MeshLambertMaterial;
                            basic.Emissive = previousColor;
                        }

                        var firstMat = first.Object.Material as MeshLambertMaterial;

                        INTERSECTED = first.Object;
                        previousColor = firstMat.Emissive;
                        firstMat.Emissive = Color.Red;
                    }
                }
                else
                {
                    if (INTERSECTED != null)
                    {
                        (INTERSECTED.Material as MeshLambertMaterial).Emissive = previousColor;
                    }

                    INTERSECTED = null;

                }
                #endregion

                renderer.RenderFrame(scene, camera);
            }
        }
Exemple #3
0
 virtual internal void Raycast(Raycaster raycaster, IList<IntersectionInfo> intersectionList) { }