Exemple #1
0
        public override void BeginApplyStroke(Ray ray)
        {
            RaycastHit hit;
            Mesh       msh = target.GetComponent <MeshFilter>().sharedMesh;

            didHit = (RXLookingGlass.IntersectRayMesh(ray, msh, target.transform.localToWorldMatrix, out hit));
            if (didHit)
            {
                var triangles = msh.triangles;
                int triangle  = hit.triangleIndex;
                int i0        = triangles[triangle];
                int i1        = triangles[triangle + 1];
                int i2        = triangles[triangle + 2];
                var bary      = hit.barycentricCoordinate;

                Matrix4x4 mtx = target.transform.worldToLocalMatrix;

                normal = target.GetSafeNormal(i0) * bary.x +
                         target.GetSafeNormal(i1) * bary.y +
                         target.GetSafeNormal(i2) * bary.z;

                tangent = target.GetSafeTangent(i0) * bary.x +
                          target.GetSafeTangent(i1) * bary.y +
                          target.GetSafeTangent(i2) * bary.z;

                normal = mtx.MultiplyVector(mtx.MultiplyVector(normal));
                Vector3 tng = new Vector3(tangent.x, tangent.y, tangent.z);
                tng       = mtx.MultiplyVector(mtx.MultiplyVector(tng));
                tangent.x = tng.x;
                tangent.y = tng.y;
                tangent.z = tng.z;
            }
        }
Exemple #2
0
        public override void BeginApplyStroke(Ray ray)
        {
            Vector3 bary = Vector3.zero;
            VertexInstanceStream stream = null;

            didHit = false;
            Mesh  best     = null;
            int   triangle = 0;
            float distance = float.MaxValue;

            if (streams != null)
            {
                for (int i = 0; i < streams.Length; ++i)
                {
                    Matrix4x4 mtx = streams[i].transform.localToWorldMatrix;
                    Mesh      msh = streams[i].GetComponent <MeshFilter>().sharedMesh;

                    RaycastHit hit;
                    RXLookingGlass.IntersectRayMesh(ray, msh, mtx, out hit);
                    if (hit.distance < distance)
                    {
                        distance = hit.distance;
                        bary     = hit.barycentricCoordinate;
                        best     = msh;
                        triangle = hit.triangleIndex;
                        stream   = streams[i];
                        didHit   = true;
                    }
                }
            }
            if (didHit && best != null)
            {
                var triangles = best.triangles;
                int i0        = triangles[triangle];
                int i1        = triangles[triangle + 1];
                int i2        = triangles[triangle + 2];

                normal = stream.GetSafeNormal(i0) * bary.x +
                         stream.GetSafeNormal(i1) * bary.y +
                         stream.GetSafeNormal(i2) * bary.z;

                tangent = stream.GetSafeTangent(i0) * bary.x +
                          stream.GetSafeTangent(i1) * bary.y +
                          stream.GetSafeTangent(i2) * bary.z;
            }
        }