Esempio n. 1
0
        private bool Raycast(Ray ray, ref RaycastHit hit)
        {
            hit.distance = float.PositiveInfinity;

            if (scene != null)
            {
                foreach (var entity in scene.Objs)
                {
                    var tempHit = default(RaycastHit);

                    if (P3dWindowIntersect.IntersectRayMesh(ray, entity.Mesh, entity.Matrix, out tempHit) == true)
                    {
                        if (tempHit.distance < hit.distance)
                        {
                            hit = tempHit;
                        }
                    }
                }
            }

            return(hit.distance < float.PositiveInfinity);
        }
        public bool Raycast(Ray ray, ref RaycastHit hit)
        {
            var skinnedMeshRenderer = Root.GetComponent <SkinnedMeshRenderer>();

            if (skinnedMeshRenderer != null)
            {
                if (skinnedMeshRenderer.sharedMesh != null)
                {
                    if (bakedMesh == null)
                    {
                        bakedMesh = new Mesh();
                    }

                    var scaling    = P3dHelper.Reciprocal3(Root.transform.lossyScale);
                    var localScale = Root.transform.localScale;

                    Root.transform.localScale = Vector3.one;

                    skinnedMeshRenderer.BakeMesh(bakedMesh);

                    Root.transform.localScale = localScale;

                    lastMesh   = bakedMesh;
                    lastMatrix = Root.transform.localToWorldMatrix;
                    //lastBounds = skinnedMeshRenderer.bounds;

                    var matrix = lastMatrix * Matrix4x4.Scale(scaling);

                    if (P3dWindowIntersect.IntersectRayMesh(ray, bakedMesh, matrix, out hit) == true)
                    {
                        return(true);
                    }
                }
            }
            else
            {
                var meshRenderer = Root.GetComponent <MeshRenderer>();

                if (meshRenderer != null)
                {
                    var meshFilter = Root.GetComponent <MeshFilter>();

                    if (meshFilter != null)
                    {
                        var mesh = meshFilter.sharedMesh;

                        if (mesh != null)
                        {
                            lastMesh   = mesh;
                            lastMatrix = Root.transform.localToWorldMatrix;
                            //lastBounds = meshRenderer.bounds;

                            if (P3dWindowIntersect.IntersectRayMesh(ray, mesh, lastMatrix, out hit) == true)
                            {
                                return(true);
                            }
                        }
                    }
                }
            }

            return(false);
        }