Пример #1
0
    public void Initialize()
    {
        if (ResetOnRandomize)
        {
            ResetVertNorms();
        }
        //lets generate the data if its not loaded
        bool newData = false;

        if (mf == null)
        {
            mf = GetComponent <MeshFilter>();
        }
        if (m == null || m != mf.sharedMesh)
        {
            if (m != null)
            {
                DestroyImmediate(m);
            }
            Mesh m_pre = Instantiate(mf.sharedMesh);
            if (m == null)
            {
                baseMesh      = Instantiate(m_pre);
                baseMesh.name = baseMesh.name.Replace("(Clone)", "");
            }
            m       = m_pre;
            m.name  = m.name.Replace("(Clone)", "");
            mf.mesh = m;
            newData = true;
        }
        if (data == null || newData)
        {
            data = ScriptableObject.CreateInstance <PolyXData>();
            data.Initialize(m, m.vertices, m.GetIndices(0), m.normals);
            packets = data.GetDataBlocks();
            //loaded = true;
        }
    }
Пример #2
0
    public void Randomize()
    {
        //for each data block, randomize its position
        if (ResetOnRandomize)
        {
            ResetVertNorms();
        }
        //lets generate the data if its not loaded
        bool newData = false;

        if (mf == null)
        {
            mf = GetComponent <MeshFilter>();
        }
        if (m == null || m != mf.sharedMesh)
        {
            if (m != null)
            {
                DestroyImmediate(m);
            }
            Mesh m_pre = Instantiate(mf.sharedMesh);
            if (m == null)
            {
                baseMesh      = Instantiate(m_pre);
                baseMesh.name = baseMesh.name.Replace("(Clone)", "");
            }
            m       = m_pre;
            m.name  = m.name.Replace("(Clone)", "");
            mf.mesh = m;
            newData = true;
        }
        if (data == null || newData)
        {
            data = ScriptableObject.CreateInstance <PolyXData>();
            data.Initialize(m, m.vertices, m.GetIndices(0), m.normals);
            packets = data.GetDataBlocks();
            //loaded = true;
        }

        ///if the registered mesh is not the same, regenerate it
        //lets also use the texture

        Vector3[] newVertices = new Vector3[m.vertices.Length];
        float     scalar      = 1;
        Vector2   uvs;

        for (int i = 0; i < newVertices.Length; ++i)
        {
            newVertices[i] = m.vertices[i];
        }
        //Get Uvs
        Vector3 rDir;

        for (int i = 0; i < packets.Length; ++i)
        {
            int[] indices = packets[i].segIndices;

            switch (RandomType)
            {
            case RandomizationType.Random:
                rDir = Random.insideUnitSphere;
                break;

            case RandomizationType.RandomAlongNormal:
                rDir = packets[i].avgSplitNormal;
                break;

            default:
                rDir = Random.insideUnitSphere;
                break;
            }
            Vector3 r = rDir * Random.Range(randomizationScale.x, randomizationScale.y);
            if (DisplacementMap != null)
            {
                //to generate an average per vertex cluster
                scalar = 1;
                for (int index = 0; index < indices.Length; ++index)
                {
                    uvs = Vector2.zero;
                    switch (uvChannel)
                    {
                    case 0:
                        if (m.uv.Length != 0)
                        {
                            uvs = m.uv[indices[index]];
                        }
                        break;

                    case 1:
                        if (m.uv2.Length != 0)
                        {
                            uvs = m.uv2[indices[index]];
                        }
                        break;

                    case 2:
                        if (m.uv3.Length != 0)
                        {
                            uvs = m.uv3[indices[index]];
                        }
                        break;

                    case 3:
                        if (m.uv4.Length != 0)
                        {
                            uvs = m.uv4[indices[index]];
                        }
                        break;

                    default:
                        if (m.uv.Length != 0)
                        {
                            uvs = m.uv[indices[index]];
                        }
                        break;
                    }
                    //uvs = UVsArray[indices[index]];
                    scalar *= (UseAlpha) ? DisplacementMap.GetPixelBilinear(uvs.x, uvs.y).a : DisplacementMap.GetPixelBilinear(uvs.x, uvs.y).grayscale;
                }
            }
            float vScale = 1;
            switch (RandomScale)
            {
            case RandomizationScale.EdgeLength:
                vScale = (packets[i].minConnectionDistance / 2);
                break;

            case RandomizationScale.Uniform:
                vScale = transform.lossyScale.magnitude;
                break;

            default:
                vScale = transform.lossyScale.magnitude;
                break;
            }

            for (int index = 0; index < indices.Length; ++index)
            {
                newVertices[indices[index]] += r * scalar * vScale;
            }
        }
        m.vertices = newVertices;

        m.RecalculateBounds();
    }