在四边形上应用8x8纹理的问题(非常模糊)

时间:2019-02-09 02:41:47

标签: c# unity3d

问题

我在Unity3d中遇到问题,使用uvs坐标在纹理图集(32x32)的简单四边形上渲染8x8px纹理。 当我使用代码在四边形上应用纹理时,它非常模糊。可能是将8x8图像缩放到更大的尺寸。如何在四边形上适当渲染8x8纹理?

我一直在寻找解决方案,但取得了积极的成果。我是初学者,我不完全知道如何统一渲染纹理。

我的代码

这是我设置材料的方式:

    private void LoadData() {
        if (blockType == BlockType.AIR) {
            this.isSolid = false;
            return;
        }
        this.material = new Material(Shader.Find("Standard"));
        Texture2D texture2D =  Resources.Load("Textures/" + blockType) as Texture2D;
        this.material.mainTexture = texture2D;
    }

这是我的BuildQuad函数(定义uvs):

    public void BuildQuad(CubeSide cubeSide) {
        Mesh mesh = new Mesh();
        mesh.name = "QuadMesh";
        Vector3[] vertices = new Vector3[4];
        Vector3[] normals = new Vector3[4];
        Vector2[] uvs = new Vector2[4];
        int[] triangles = new int[6];
        if (cubeSide == CubeSide.FRONT) {
            vertices = new Vector3[] {p4, p5, p1, p0};
            normals = new Vector3[] {
                Vector3.forward, 
                Vector3.forward, 
                Vector3.forward, 
                Vector3.forward
            };
            uvs = new Vector2[] {
                new Vector2(0.25f, 0.25f),
                new Vector2(0.0f, 0.25f),
                new Vector2(0.0f, 0.0f),
                new Vector2(0.25f, 0),
            };
        } else if (cubeSide == CubeSide.BACK) {
            vertices = new Vector3[] {p6, p7, p3, p2};
            normals = new Vector3[] {
                Vector3.back, 
                Vector3.back, 
                Vector3.back, 
                Vector3.back
            };
            uvs = new Vector2[] {
                new Vector2(0.5f, 0.25f), 
                new Vector2(0.251f, 0.25f), 
                new Vector2(0.251f, 0.0f), 
                new Vector2(0.5f, 0.0f),  
            };
        } else if (cubeSide == CubeSide.LEFT) {
            vertices = new Vector3[] {p7, p4, p0, p3};
            normals = new Vector3[] {
                Vector3.left, 
                Vector3.left, 
                Vector3.left, 
                Vector3.left
            };
            uvs = new Vector2[] {
                new Vector2(0.75f, 0.25f),
                new Vector2(0.501f, 0.25f),
                new Vector2(0.501f, 0.0f), 
                new Vector2(0.75f, 0.0f),
            };
        } else if (cubeSide == CubeSide.RIGHT) {
            vertices = new Vector3[] {p5, p6, p2, p1};
            normals = new Vector3[] {
                Vector3.right, 
                Vector3.right, 
                Vector3.right, 
                Vector3.right
            };
            uvs = new Vector2[] {
                new Vector2(1.0f, 0.25f),
                new Vector2(0.751f, 0.25f),
                new Vector2(0.751f, 0.0f), 
                new Vector2(1.00f, 0.0f),
            };
        } else if (cubeSide == CubeSide.BOTTOM) {
            vertices = new Vector3[] {p0, p1, p2, p3};
            normals = new Vector3[] {
                Vector3.down, 
                Vector3.down, 
                Vector3.down, 
                Vector3.down
            };
            uvs = new Vector2[] {
                new Vector2(0.25f, 0.50f),
                new Vector2(0.0f, 0.50f),
                new Vector2(0.0f, 0.251f), 
                new Vector2(0.25f, 0.251f),
            };
        } else if (cubeSide == CubeSide.TOP) {
            vertices = new Vector3[] {p7, p6, p5, p4};
            normals = new Vector3[] {
                Vector3.up, 
                Vector3.up, 
                Vector3.up, 
                Vector3.up
            };
            uvs = new Vector2[] {
                new Vector2(0.50f, 0.50f),
                new Vector2(0.251f, 0.5f),
                new Vector2(0.251f, 0.251f), 
                new Vector2(0.5f, 0.251f),
            };
        }
        triangles = new int[] {3, 1, 0, 3, 2, 1};
        mesh.vertices = vertices;
        mesh.normals = normals;
        mesh.uv = uvs;
        mesh.triangles = triangles;
        mesh.RecalculateBounds();
        GameObject quad = new GameObject("quad");
        quad.transform.position = location.position;
        quad.transform.parent = parent.transform;
        MeshFilter meshFilter = (MeshFilter) quad.AddComponent(typeof(MeshFilter));
        meshFilter.mesh = mesh;
    }

这是我将材质设置为渲染器的方式

    void CombineQuads() {

        //1. Combine all children meshes
        MeshFilter[] meshFilters = chunkGameObject.GetComponentsInChildren<MeshFilter>();
        CombineInstance[] combine = new CombineInstance[meshFilters.Length];
        int i = 0;
        while (i < meshFilters.Length) {
            combine[i].mesh = meshFilters[i].sharedMesh;
            combine[i].transform = meshFilters[i].transform.localToWorldMatrix;
            i++;
        }

        //2. Create a new mesh on the parent object
        MeshFilter meshFilter = (MeshFilter) chunkGameObject.AddComponent(typeof(MeshFilter));
        meshFilter.mesh = new Mesh();

        //3. Add combined meshes on children as the parent's mesh
        meshFilter.mesh.CombineMeshes(combine);

        //4. Create a renderer for the parent
        MeshRenderer renderer = chunkGameObject.AddComponent(typeof(MeshRenderer)) as MeshRenderer;
        renderer.material = BlockData.GetBlockData(BlockType.DIRT).material;

        //5. Delete all uncombined children
        foreach (Transform quad in chunkGameObject.transform) {
            GameObject.Destroy(quad.gameObject);
        }
    }

结果

我使用此图像通过在所有面上渲染四边形来渲染污垢块:
32x32 dirt quads atlas

这不是想要的结果(带有模糊):
blur

我完全不知道如何避免这种情况。问题出在哪里?
如何使其看起来像这样:
wanted result

0 个答案:

没有答案