Пример #1
0
    public TriangularModelMesh(ParticleModel model, ClothSimulation settings)
    {
        this.model = model;

        GameObject meshGameObject = new GameObject("TriangularModelMesh");

        MeshFilter meshFilter = meshGameObject.AddComponent <MeshFilter>();

        this.mesh = meshFilter.mesh;

        MeshRenderer meshRenderer    = meshGameObject.AddComponent <MeshRenderer>();
        Material     defaultMaterial = new Material(Shader.Find("VR/SpatialMapping/Wireframe"));

        meshRenderer.sharedMaterial = defaultMaterial;

        this.mesh.Clear();

        int subdivisions = 6;
        int modelWH      = this.model.getWidthHeight();

        points = new Vector3[(int)Math.Pow((modelWH + (modelWH - 1)) * subdivisions, 2)];
        Array.Copy(model.positions, points, model.positions.Length);
        int index = model.positions.Length;

        List <int> triangles = new List <int>();

        List <SubLine> SubLines = new List <SubLine>();

        // horizontal
        for (int j = 0; j < modelWH; j++)
        {
            for (int i = 0; i < modelWH - 1; i++)
            {
                int left  = j * modelWH + i;
                int right = left + 1;

                SubLine subLine = bezier > 0 ? new BezierSubline(left, right, index, index + subdivisions) : new SubLine(left, right, index, index + subdivisions);
                index = index + subdivisions;

                if (i > 0)
                {
                    SubLine prev = SubLines[(modelWH - 1) * j + (i - 1)];
                    subLine.link(ref prev);
                }

                SubLines.Add(subLine);
            }
        }
        // vertical
        for (int j = 0; j < modelWH - 1; j++)
        {
            for (int i = 0; i < modelWH; i++)
            {
                int bottom = j * modelWH + i;
                int top    = bottom + modelWH;

                SubLine subLine = bezier > 0 ? new BezierSubline(bottom, top, index, index + subdivisions) : new SubLine(bottom, top, index, index + subdivisions);
                index = index + subdivisions;

                if (j > 0)
                {
                    SubLine prev = SubLines[modelWH * (modelWH - 1) + modelWH * (j - 1) + i];
                    subLine.link(ref prev);
                }

                SubLines.Add(subLine);
            }
        }
        List <SubGrid> SubGrids = new List <SubGrid>();

        for (int j = 0; j < modelWH - 1; j++)
        {
            for (int i = 0; i < modelWH - 1; i++)
            {
                int a = modelWH * j + i;
                int b = a + 1;
                int c = a + modelWH;
                int d = c + 1;

                SubLine bottom = SubLines[(modelWH - 1) * j + i];
                SubLine top    = SubLines[(modelWH - 1) * (j + 1) + i];
                SubLine left   = SubLines[(modelWH - 1) * modelWH + modelWH * j + i];
                SubLine right  = SubLines[(modelWH - 1) * modelWH + modelWH * j + i + 1];

                SubGrid grid = new SubGrid(ref bottom, ref right, ref left, ref top, index, subdivisions, subdivisions);
                index += subdivisions * subdivisions;

                if (i > 0)
                {
                    SubGrid prev = SubGrids[(modelWH - 1) * j + i - 1];
                    grid.linkHorizontal(ref prev);
                }
                if (j > 0)
                {
                    SubGrid prev = SubGrids[(modelWH - 1) * (j - 1) + i];
                    grid.linkVertical(ref prev);
                }

                SubGrids.Add(grid);

                // Add triangles for the grid
                triangles.AddRange(grid.GenTriangles());
            }
        }

        List <int> included = new List <int>(new HashSet <int>(triangles));

        // Calculate the uvs
        Vector2[] uvs = new Vector2[points.Length]; // uv maps texture to points
        float     du  = 1.0f / modelWH;
        float     dv  = 1.0f / modelWH;

        for (int u = 0; u < modelWH; u++)
        {
            for (int v = 0; v < modelWH; v++)
            {
                uvs[u * modelWH + v] = new Vector2(u * du, v * dv);
            }
        }

        if (showGridPoints)
        {
            gridPointsObjectManager = new MonoManager <SceneObject>();
            GameObject unitPrefab = new GameObject();
            unitPrefab.AddComponent <SceneObject>();
            unitPrefab.AddComponent <MeshFilter>();
            unitPrefab.AddComponent <MeshRenderer>();
            gridPointsObjectManager.OverrideGameObject(unitPrefab);

            Mesh     mesh     = Assets.Scripts.Tools.Geometry.PrimitiveHelper.GetPrimitiveMesh(PrimitiveType.Sphere);
            Material material = new Material(Shader.Find("Transparent/Diffuse"));
            material.color = Color.yellow;


            for (int i = model.positions.Length; i < points.Length; i++)
            {
                var newObj = gridPointsObjectManager.New();
                newObj.Init(mesh, material);
                newObj.transform.position = points[i];
            }
        }

        SubLines.ForEach(sl => {
            sl.setUV(ref uvs);
        });
        this.subLines = SubLines.ToArray();
        SubGrids.ForEach(sl => {
            sl.setUV(ref uvs);
        });
        this.subGrids = SubGrids.ToArray();

        SubLines.ForEach(sl => {
            sl.setPoints(ref points);
        });
        SubGrids.ForEach(sl => {
            sl.setPoints(ref points);
        });

        // Set unit mesh
        this.mesh.vertices  = points;
        this.mesh.uv        = uvs;
        this.mesh.triangles = addBothSide(triangles.ToArray());
    }