在 Unity3D C# 中使用曲线分割纹理

Split Texture using a Curved Line in Unity3D C#

我有一个纹理,我想使用 Vector2 数组将其分成两部分。 我有曲线的所有 Vector2 点。

问题

如何使用曲线点将纹理分成两部分。

备选Solutions/Questions

如何'pixel' 填充 Vector2[] 形状来创建纹理?


我的尝试

1)生成Vector2点创建一个正方形,顶部为曲线边缘。看起来很有希望,但是当我尝试生成网格时,点排序不正确。

2) 动态创建一个 Polygon2D Collider - 模仿切片纹理的底部 - 这与尝试 1 存在相同的问题,即点排序。所以当将 Collider 转换为 Mesh 时,它显然与尝试具有相同的结果


下图中:

  1. 红线模拟我的Vector2数组
  2. 灰色+绿色方块是贴图1024 x 1024像素
  3. 绿色区域就是我要的目标区域

这将生成您想要的形状的网格(但顶部有锯齿状边缘),希望这是朝着正确方向迈出的一步。 Vector2 points[] 数组包含您的红线。它应该按 x 坐标排序,所有数字都应该在 0 到 1 之间。需要一个网格过滤器和一个带有纹理的网格渲染器。

using UnityEngine;

[RequireComponent(typeof(MeshFilter))]
[RequireComponent(typeof(MeshRenderer))]
public class createMesh : MonoBehaviour {

    void Start () {
        Vector2[] points = new Vector2[4];
        points [0] = new Vector2 (0, .5f);
        points [1] = new Vector2 (.33f, 1f);
        points [2] = new Vector2 (.66f, .5f);
        points [3] = new Vector2 (1, 1f);

        MeshFilter mf = GetComponent<MeshFilter> ();
        Mesh mesh = new Mesh();


        Vector3[] verticies = new Vector3[points.Length * 2];
        int[] triangles = new int[(points.Length - 1)*6];
        Vector3[] normals = new Vector3[points.Length * 2];
        Vector2[] uv = new Vector2[points.Length * 2];

        int vIndex = 0;
        int tIndex = 0;
        int nIndex = 0;
        int uvIndex = 0;

        for (int i = 0; i< points.Length; i++) {
            Vector3 topVert = points[i];
            Vector3 bottomVert = topVert;
            bottomVert.y = 0;
            verticies[vIndex++]= bottomVert;
            verticies[vIndex++]=topVert;

            //uv
            uv[uvIndex++] = bottomVert;
            uv[uvIndex++] = topVert;

            //normals
            normals[nIndex++] = -Vector3.forward;
            normals[nIndex++] = -Vector3.forward;

            if (i<points.Length - 1) {
                //triangles
                triangles[tIndex++] = (i)*2;
                triangles[tIndex++] = (i)*2+1;
                triangles[tIndex++] = (i)*2+2;
                triangles[tIndex++] = (i)*2+2;
                triangles[tIndex++] = (i)*2+1;
                triangles[tIndex++] = (i)*2+3;

            }
        }

        mesh.vertices = verticies;
        mesh.triangles = triangles;
        mesh.normals = normals;
        mesh.uv = uv;

        mf.mesh = mesh;
    }
}

奖励:这是一种只使用纹理的方法。要使用此位图,必须将位图设置为高级,并在导入设置中启用 read/write。此方法使用 0 到 1023(或者无论你的纹理有多大)作为坐标,并且应该也适用于超出该范围的数字。

using UnityEngine;
using System.Collections;


public class tex2d : MonoBehaviour {

    public Vector2[] points;

    void Start () {
        MeshRenderer mr;
        Texture2D t2d;
        Texture2D newTex = new Texture2D (1024, 1024);

        mr = GetComponent<MeshRenderer> ();
        t2d = mr.material.GetTexture (0) as Texture2D;
        MakeTex (points, t2d, ref newTex, 1024);
        mr.material.SetTexture (0, newTex);
    }


    void MakeTex(Vector2[] pnts, Texture2D inputTex, ref Texture2D outputTex, int size){

        Color bgcolor = new Color (1, 0, 1, 1);
        for (int i=0; i<(pnts.Length-1); i++) {
            Vector2 p1=pnts[i];
            Vector2 p2=pnts[i+1];

            //skip points that are out of range
            if ((p1.x <0 && p2.x <0) || (p1.x > size && p2.x>size)) continue;

            for (int x =(int)p1.x; x<(int)p2.x; x++) {
                if (x<0) continue;
                if (x>=size) break;

                float interpX = (x-p1.x)/(p2.x-p1.x);
                int interpY = (int) ((p2.y-p1.y)*interpX + p1.y);

                for (int y=0; y<interpY; y++) {
                    outputTex.SetPixel(x,y,inputTex.GetPixel(x,y));
                }
                for (int y= interpY; y<size; y++) {
                    outputTex.SetPixel(x,y,bgcolor);
                }

            }

        }
        outputTex.Apply ();
    }
}