/// <summary> /// /// </summary> /// <param name="raycaster"></param> /// <param name="intersects"></param> public override void Raycast(Raycaster raycaster, ref List <Intersect> intersects) { var geometry = this.Geometry; // Checking boundingSphere distance to ray if (geometry.BoundingSphere == null) { geometry.ComputeBoundingSphere(); } var sphere = geometry.BoundingSphere; sphere.ApplyMatrix4(this.MatrixWorld); if (raycaster.Ray.IsIntersectionSphere(sphere) == false) { return; } // Check boundingBox before continuing var inverseMatrix = this.MatrixWorld.GetInverse(); var ray = new Ray(); ray.Copy(raycaster.Ray).ApplyMatrix4(inverseMatrix); if (geometry.BoundingBox != null) { if (ray.IsIntersectionBox(geometry.BoundingBox) == false) { return; } } // We are within the boundingbox or sphere var bufferGeometry = geometry as BufferGeometry; if (bufferGeometry != null) { this.Raycast(raycaster, ray, bufferGeometry, ref intersects); } else if (geometry is Geometry) { this.Raycast(raycaster, ray, geometry as Geometry, ref intersects); } }
/// <summary> /// /// </summary> /// <param name="raycaster"></param> /// <param name="intersects"></param> public virtual void Raycast(Raycaster raycaster, ref List <Intersect> intersects) { var inverseMatrix = new Matrix4(); var ray = new Ray(); var sphere = new Sphere(); var precision = raycaster.LinePrecision; var precisionSq = precision * precision; var geometry = this.Geometry as Geometry; Debug.Assert(null != geometry, "this.Geometry as Geometry cast failed"); if (geometry.BoundingSphere == null) { geometry.ComputeBoundingSphere(); } // Checking boundingSphere distance to ray sphere.Copy(geometry.BoundingSphere); sphere.ApplyMatrix4(this.MatrixWorld); if (raycaster.Ray.IsIntersectionSphere(sphere) == false) { return; } inverseMatrix = this.MatrixWorld.GetInverse(); ray.Copy(raycaster.Ray).ApplyMatrix4(inverseMatrix); /* if ( geometry instanceof THREE.BufferGeometry ) { * * } else */ if (geometry is Geometry) { var vertices = geometry.Vertices; var nbVertices = vertices.Count; var interSegment = new Vector3(); var interRay = new Vector3(); var step = (this is Line && ((Line)this).Mode == Three.LineStrip ? 1 : 2); for (var i = 0; i < nbVertices - 1; i = i + step) { var distSq = ray.DistanceSqToSegment(vertices[i], vertices[i + 1], interRay, interSegment); if (distSq > precisionSq) { continue; } var distance = ray.Origin.DistanceTo(interRay); if (distance < raycaster.Near || distance > raycaster.Far) { continue; } intersects.Add(new Intersect() { Distance = distance, // What do we want? intersection point on the ray or on the segment?? // point: raycaster.ray.at( distance ), Point = ((Vector3)interSegment.Clone()).ApplyMatrix4(this.MatrixWorld), Face = null, FaceIndex = -1, Object3D = this }); } } }