Example #1
0
    void DeformMesh(Vector3 position, float power)
    {
        Vector3[] vertices  = meshScan.mesh.vertices;
        float     sqrRadius = radius * radius;

        Vector3 averageNormal = lerpNearestOnMesh.lerpMove(position, meshGoal, percentTowards);

        // Deform vertices along averaged normal
        for (int i = 0; i < vertices.Length; i++)
        {
            float sqrMagnitude = (vertices[i] - position).sqrMagnitude;
            // Early out if vertex too far away form position
            if (sqrMagnitude > sqrRadius)
            {
                continue;
            }

            float distance = Mathf.Sqrt(sqrMagnitude);
            float falloff  = 0.0f;
            switch (fallOff)
            {
            case Falloff.type.Gauss:
                falloff = Falloff.Gauss(distance, radius);
                break;

            case Falloff.type.Needle:
                falloff = Falloff.Needle(distance, radius);
                break;

            case Falloff.type.Linear:
                falloff = Falloff.Linear(distance, radius);
                break;

            default:
                Debug.Log("unrecognized falloff type");
                break;
            }

            vertices[i] += averageNormal * falloff * power;
        }

        meshScan.mesh.vertices = vertices;
        meshScan.mesh.RecalculateNormals();
        meshScan.mesh.RecalculateBounds();
    }
Example #2
0
    void BulgeMesh(MeshFilter mf, Vector3 pos, float power, float inRadius)
    {
        Vector3 position = mf.transform.InverseTransformPoint(pos);
        Mesh    mesh     = mf.mesh;

        Vector3[] vertices  = mesh.vertices;
        Vector3[] normals   = mesh.normals;
        float     sqrRadius = inRadius * inRadius;

        // Calculate averaged normal of all surrounding vertices
        Vector3 averageNormal = Vector3.zero;

        for (int i = 0; i < vertices.Length; i++)
        {
            float sqrMagnitude = (vertices[i] - position).sqrMagnitude;
            // Early out if too far away
            if (sqrMagnitude > sqrRadius)
            {
                continue;
            }

            float distance = Mathf.Sqrt(sqrMagnitude);
            float falloff  = Falloff.Linear(distance, inRadius);
            averageNormal += falloff * normals[i];
        }
        averageNormal = averageNormal.normalized;

        // Deform vertices along averaged normal
        for (int i = 0; i < vertices.Length; i++)
        {
            float sqrMagnitude = (vertices[i] - position).sqrMagnitude;
            // Early out if too far away
            if (sqrMagnitude > sqrRadius)
            {
                continue;
            }

            float distance = Mathf.Sqrt(sqrMagnitude);
            float falloff  = 0f;
            switch (fallOff)
            {
            case Falloff.type.Gauss:
                falloff = Falloff.Gauss(distance, inRadius);
                break;

            case Falloff.type.Needle:
                falloff = Falloff.Needle(distance, inRadius);
                break;

            default:
                falloff = Falloff.Linear(distance, inRadius);
                break;
            }

            vertices[i] += averageNormal * falloff * power;
        }

        mesh.vertices = vertices;
        mesh.RecalculateNormals();
        mesh.RecalculateBounds();
    }