Beispiel #1
0
        private static void NormalCheck(ref Mesh_Maker.Triangle triangle)
        {
            Vector3 crossProduct  = Vector3.Cross(triangle.vertices[1] - triangle.vertices[0], triangle.vertices[2] - triangle.vertices[0]);
            Vector3 averageNormal = (triangle.normals[0] + triangle.normals[1] + triangle.normals[2]) / 3.0f;
            float   dotProduct    = Vector3.Dot(averageNormal, crossProduct);

            if (dotProduct < 0)
            {
                Vector3 temp = triangle.vertices[2];
                triangle.vertices[2] = triangle.vertices[0];
                triangle.vertices[0] = temp;

                temp = triangle.normals[2];
                triangle.normals[2] = triangle.normals[0];
                triangle.normals[0] = temp;

                Vector2 temp2 = triangle.uvs[2];
                triangle.uvs[2] = triangle.uvs[0];
                triangle.uvs[0] = temp2;

                Vector4 temp3 = triangle.tangents[2];
                triangle.tangents[2] = triangle.tangents[0];
                triangle.tangents[0] = temp3;
            }
        }
Beispiel #2
0
        // Functions
        private static void Cut_this_Face(ref Mesh_Maker.Triangle triangle, int submesh)
        {
            _isLeftSideCache[0] = _blade.GetSide(triangle.vertices[0]); // true = left
            _isLeftSideCache[1] = _blade.GetSide(triangle.vertices[1]);
            _isLeftSideCache[2] = _blade.GetSide(triangle.vertices[2]);


            int leftCount  = 0;
            int rightCount = 0;

            for (int i = 0; i < 3; i++)
            {
                if (_isLeftSideCache[i])
                { // left
                    _leftTriangleCache.vertices[leftCount] = triangle.vertices[i];
                    _leftTriangleCache.uvs[leftCount]      = triangle.uvs[i];
                    _leftTriangleCache.normals[leftCount]  = triangle.normals[i];
                    _leftTriangleCache.tangents[leftCount] = triangle.tangents[i];

                    leftCount++;
                }
                else
                { // right
                    _rightTriangleCache.vertices[rightCount] = triangle.vertices[i];
                    _rightTriangleCache.uvs[rightCount]      = triangle.uvs[i];
                    _rightTriangleCache.normals[rightCount]  = triangle.normals[i];
                    _rightTriangleCache.tangents[rightCount] = triangle.tangents[i];

                    rightCount++;
                }
            }

            // find the new triangles X 3
            // first the new vertices

            // this will give me a triangle with the solo point as first
            if (leftCount == 1)
            {
                _triangleCache.vertices[0] = _leftTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _leftTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _leftTriangleCache.normals[0];
                _triangleCache.tangents[0] = _leftTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _rightTriangleCache.vertices[0];
                _triangleCache.uvs[1]      = _rightTriangleCache.uvs[0];
                _triangleCache.normals[1]  = _rightTriangleCache.normals[0];
                _triangleCache.tangents[1] = _rightTriangleCache.tangents[0];

                _triangleCache.vertices[2] = _rightTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _rightTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _rightTriangleCache.normals[1];
                _triangleCache.tangents[2] = _rightTriangleCache.tangents[1];
            }
            else // rightCount == 1
            {
                _triangleCache.vertices[0] = _rightTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _rightTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _rightTriangleCache.normals[0];
                _triangleCache.tangents[0] = _rightTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _leftTriangleCache.vertices[0];
                _triangleCache.uvs[1]      = _leftTriangleCache.uvs[0];
                _triangleCache.normals[1]  = _leftTriangleCache.normals[0];
                _triangleCache.tangents[1] = _leftTriangleCache.tangents[0];

                _triangleCache.vertices[2] = _leftTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _leftTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _leftTriangleCache.normals[1];
                _triangleCache.tangents[2] = _leftTriangleCache.tangents[1];
            }

            // now to find the intersection points between the solo point and the others
            float   distance           = 0;
            float   normalizedDistance = 0.0f;
            Vector3 edgeVector         = Vector3.zero; // contains edge length and direction

            edgeVector = _triangleCache.vertices[1] - _triangleCache.vertices[0];
            _blade.Raycast(new Ray(_triangleCache.vertices[0], edgeVector.normalized), out distance);

            normalizedDistance            = distance / edgeVector.magnitude;
            _newTriangleCache.vertices[0] = Vector3.Lerp(_triangleCache.vertices[0], _triangleCache.vertices[1], normalizedDistance);
            _newTriangleCache.uvs[0]      = Vector2.Lerp(_triangleCache.uvs[0], _triangleCache.uvs[1], normalizedDistance);
            _newTriangleCache.normals[0]  = Vector3.Lerp(_triangleCache.normals[0], _triangleCache.normals[1], normalizedDistance);
            _newTriangleCache.tangents[0] = Vector4.Lerp(_triangleCache.tangents[0], _triangleCache.tangents[1], normalizedDistance);

            edgeVector = _triangleCache.vertices[2] - _triangleCache.vertices[0];
            _blade.Raycast(new Ray(_triangleCache.vertices[0], edgeVector.normalized), out distance);

            normalizedDistance            = distance / edgeVector.magnitude;
            _newTriangleCache.vertices[1] = Vector3.Lerp(_triangleCache.vertices[0], _triangleCache.vertices[2], normalizedDistance);
            _newTriangleCache.uvs[1]      = Vector2.Lerp(_triangleCache.uvs[0], _triangleCache.uvs[2], normalizedDistance);
            _newTriangleCache.normals[1]  = Vector3.Lerp(_triangleCache.normals[0], _triangleCache.normals[2], normalizedDistance);
            _newTriangleCache.tangents[1] = Vector4.Lerp(_triangleCache.tangents[0], _triangleCache.tangents[2], normalizedDistance);

            if (_newTriangleCache.vertices[0] != _newTriangleCache.vertices[1])
            {
                //tracking newly created points
                _newVerticesCache.Add(_newTriangleCache.vertices[0]);
                _newVerticesCache.Add(_newTriangleCache.vertices[1]);
            }
            // make the new triangles
            // one side will get 1 the other will get 2

            if (leftCount == 1)
            {
                // first one on the left
                _triangleCache.vertices[0] = _leftTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _leftTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _leftTriangleCache.normals[0];
                _triangleCache.tangents[0] = _leftTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _newTriangleCache.vertices[0];
                _triangleCache.uvs[1]      = _newTriangleCache.uvs[0];
                _triangleCache.normals[1]  = _newTriangleCache.normals[0];
                _triangleCache.tangents[1] = _newTriangleCache.tangents[0];

                _triangleCache.vertices[2] = _newTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _newTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _newTriangleCache.normals[1];
                _triangleCache.tangents[2] = _newTriangleCache.tangents[1];

                // check if it is facing the right way
                NormalCheck(ref _triangleCache);

                // add it
                _leftSide.AddTriangle(_triangleCache, submesh);


                // other two on the right
                _triangleCache.vertices[0] = _rightTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _rightTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _rightTriangleCache.normals[0];
                _triangleCache.tangents[0] = _rightTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _newTriangleCache.vertices[0];
                _triangleCache.uvs[1]      = _newTriangleCache.uvs[0];
                _triangleCache.normals[1]  = _newTriangleCache.normals[0];
                _triangleCache.tangents[1] = _newTriangleCache.tangents[0];

                _triangleCache.vertices[2] = _newTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _newTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _newTriangleCache.normals[1];
                _triangleCache.tangents[2] = _newTriangleCache.tangents[1];

                // check if it is facing the right way
                NormalCheck(ref _triangleCache);

                // add it
                _rightSide.AddTriangle(_triangleCache, submesh);

                // third
                _triangleCache.vertices[0] = _rightTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _rightTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _rightTriangleCache.normals[0];
                _triangleCache.tangents[0] = _rightTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _rightTriangleCache.vertices[1];
                _triangleCache.uvs[1]      = _rightTriangleCache.uvs[1];
                _triangleCache.normals[1]  = _rightTriangleCache.normals[1];
                _triangleCache.tangents[1] = _rightTriangleCache.tangents[1];

                _triangleCache.vertices[2] = _newTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _newTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _newTriangleCache.normals[1];
                _triangleCache.tangents[2] = _newTriangleCache.tangents[1];

                // check if it is facing the right way
                NormalCheck(ref _triangleCache);

                // add it
                _rightSide.AddTriangle(_triangleCache, submesh);
            }
            else
            {
                // first one on the right
                _triangleCache.vertices[0] = _rightTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _rightTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _rightTriangleCache.normals[0];
                _triangleCache.tangents[0] = _rightTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _newTriangleCache.vertices[0];
                _triangleCache.uvs[1]      = _newTriangleCache.uvs[0];
                _triangleCache.normals[1]  = _newTriangleCache.normals[0];
                _triangleCache.tangents[1] = _newTriangleCache.tangents[0];

                _triangleCache.vertices[2] = _newTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _newTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _newTriangleCache.normals[1];
                _triangleCache.tangents[2] = _newTriangleCache.tangents[1];

                // check if it is facing the right way
                NormalCheck(ref _triangleCache);

                // add it
                _rightSide.AddTriangle(_triangleCache, submesh);


                // other two on the left
                _triangleCache.vertices[0] = _leftTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _leftTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _leftTriangleCache.normals[0];
                _triangleCache.tangents[0] = _leftTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _newTriangleCache.vertices[0];
                _triangleCache.uvs[1]      = _newTriangleCache.uvs[0];
                _triangleCache.normals[1]  = _newTriangleCache.normals[0];
                _triangleCache.tangents[1] = _newTriangleCache.tangents[0];

                _triangleCache.vertices[2] = _newTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _newTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _newTriangleCache.normals[1];
                _triangleCache.tangents[2] = _newTriangleCache.tangents[1];

                // check if it is facing the right way
                NormalCheck(ref _triangleCache);

                // add it
                _leftSide.AddTriangle(_triangleCache, submesh);

                // third
                _triangleCache.vertices[0] = _leftTriangleCache.vertices[0];
                _triangleCache.uvs[0]      = _leftTriangleCache.uvs[0];
                _triangleCache.normals[0]  = _leftTriangleCache.normals[0];
                _triangleCache.tangents[0] = _leftTriangleCache.tangents[0];

                _triangleCache.vertices[1] = _leftTriangleCache.vertices[1];
                _triangleCache.uvs[1]      = _leftTriangleCache.uvs[1];
                _triangleCache.normals[1]  = _leftTriangleCache.normals[1];
                _triangleCache.tangents[1] = _leftTriangleCache.tangents[1];

                _triangleCache.vertices[2] = _newTriangleCache.vertices[1];
                _triangleCache.uvs[2]      = _newTriangleCache.uvs[1];
                _triangleCache.normals[2]  = _newTriangleCache.normals[1];
                _triangleCache.tangents[2] = _newTriangleCache.tangents[1];

                // check if it is facing the right way
                NormalCheck(ref _triangleCache);

                // add it
                _leftSide.AddTriangle(_triangleCache, submesh);
            }
        }