unity wireframe 物体矩形网格

今天把矩形网格做出来了 判断是不是直角,只保留边的颜色形成新的texture 和material

物体原先模样
矩形网格

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public struct MeshStruct
{
    //public object vertices { get; private set; }

    public int[] triangles;       // 网格的三角形 
    public Vector2[] uvs;         // 网格的UV坐标 
    public MeshStruct(int[] triangles, Vector2[] uvs)
    {
       // this.vertices = vertices;
        this.triangles = triangles;
        this.uvs = uvs;
    }
}
public class TextureUtility
{


    public static Texture2D GetLineTexture(MeshStruct meshData, Texture2D texture)
    {
        int width = texture.width;
        int height = texture.height;
        Texture2D newTexture = new Texture2D(width, height, TextureFormat.ARGB32, false);
        newTexture.alphaIsTransparency = true;
        Color[] colors = new Color[width * height];
        for (int i = 0; i < colors.Length; i++)
        {
            colors[i] = Color.clear;
        }
        newTexture.SetPixels(colors);

        for (int k = 0; k < meshData.triangles.Length - 2; k += 3)
        {
            Vector2[] _uvs = new Vector2[3];
            int tID1 = meshData.triangles[k];
            int tID2 = meshData.triangles[k + 1];
            int tID3 = meshData.triangles[k + 2];

            _uvs[0] = meshData.uvs[tID1];
            _uvs[1] = meshData.uvs[tID2];
            _uvs[2] = meshData.uvs[tID3];

            for (int i = 0; i < _uvs.Length; ++i)
            {
                for (int j = i + 1; j < _uvs.Length; ++j)
                {
                    if (_uvs[i].x == _uvs[j].x || _uvs[i].y == _uvs[j].y)//轴向 
                    {
                        float uvx1 = _uvs[i].x;
                        float uvy1 = _uvs[i].y;
                        float uvx2 = _uvs[j].x;
                        float uvy2 = _uvs[j].y;

                        int px1 = (int)(uvx1 * width);
                        int py1 = (int)(uvy1 * height);
                        int px2 = (int)(uvx2 * width);
                        int py2 = (int)(uvy2 * height);

                        int minpx = px1 < px2 ? px1 : px2;
                        int minpy = py1 < py2 ? py1 : py2;

                        int pw = Mathf.FloorToInt(Mathf.Abs((uvx1 - uvx2) * width));
                        int ph = Mathf.FloorToInt(Mathf.Abs((uvy1 - uvy2) * height));

                        for (int m = 0; m <= ph; m++)
                        {
                            for (int n = 0; n <= pw; n++)
                            {
                                Color colorline = texture.GetPixel(minpx + n, minpy + m);
                                newTexture.SetPixel(minpx + n, minpy + m, Color.black);
                            }
                        }
                    }
                }
            }
        }
        newTexture.Apply();
        return newTexture;
    }

    public static Material GetParticalMaterial(Texture texture)
    {
        // Unity has a built-in shader that is useful for drawing 
        // simple colored things. 
        Shader shader = Shader.Find("Particles/Alpha Blended");
        Material particMat = new Material(shader);
        particMat.mainTexture = texture;
        return particMat;
    }
}
public class TestC:MonoBehaviour
{

    private void Start()
    {
        Texture  objtex= GetComponent<MeshRenderer>().material.mainTexture;
        Debug.Log(objtex);
        MeshStruct meshStruct = new MeshStruct(GetComponent<MeshFilter>().mesh.triangles, GetComponent<MeshFilter>().mesh.uv);
        Texture outtex = TextureUtility.GetLineTexture(meshStruct, (Texture2D)objtex);

      GetComponent<MeshRenderer>().material= TextureUtility.GetParticalMaterial(outtex);
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值