Пример #1
0
        private GameObject CreateModel(ModelProperties modelProperties)
        {
            int skew = modelProperties.Skew;
            // skew is in Wurm units that are 4 Unity units long and 0.1 units high
            float skewPerUnit = skew * 0.1f * 0.25f;

            GameObject clone = Object.Instantiate(originalModel);

            MeshFilter[] filters = clone.GetComponentsInChildren <MeshFilter>();
            foreach (MeshFilter filter in filters)
            {
                Mesh mesh    = filter.mesh;
                Mesh newMesh = new Mesh();
                newMesh.name = mesh.name;
                Vector3[] originalVertices = mesh.vertices;
                Vector3[] newVertices      = new Vector3[originalVertices.Length];
                for (int i = 0; i < originalVertices.Length; i++)
                {
                    Vector3 vec = originalVertices[i];
                    newVertices[i] = new Vector3(vec.x, vec.y + skewPerUnit * vec.x, vec.z);
                }
                newMesh.vertices  = newVertices;
                newMesh.uv        = mesh.uv;
                newMesh.triangles = mesh.triangles;
                newMesh.normals   = mesh.normals;
                newMesh.tangents  = mesh.tangents;
                newMesh.RecalculateBounds();
                if (!modelProperties.IsOriginalModel())
                {
                    newMesh.UploadMeshData(true);
                }
                filter.sharedMesh = newMesh;
            }

            if (modelProperties.CustomMaterial)
            {
                MeshRenderer[] renderers = clone.GetComponentsInChildren <MeshRenderer>();
                foreach (MeshRenderer renderer in renderers)
                {
                    Material oldMaterial    = renderer.sharedMaterial;
                    Material customMaterial = new Material(modelProperties.CustomMaterial);
                    if (!customMaterial.mainTexture)
                    {
                        customMaterial.mainTexture = oldMaterial.mainTexture;
                    }
                    renderer.sharedMaterial = customMaterial;
                }
            }

            return(clone);
        }