1
\$\begingroup\$

Good day, I seem to have run into a problem which I can't find an answer to. I'm making a World Editor for my project, and generating a grid plane which the user can modify. Whenever I rotate the camera as seen below, to a more side view angle, the grid texture fades out.

I have changed Aniso levels, tried at different quality settings, also different shaders for the texture.

At the moment, I'm using the Unlit/Transparent Cutout shader which gives a crisp look on the grid. The texture resolution is for width and height mapSize(64 > 254)*gridDetail(4>32). The same results is given no matter the detail size on the texture.

It does appear that the center of the plane gets the most faded as opposed to the sides.

EDIT: Okay, so I found out what's the problem, now here's my question, how can I generate mipmaps for the texture? (The texture is created with the following code(unoptimized I know):

using UnityEngine;
using System.Collections;

public class MyScript: MonoBehaviour
{
    public int sizeX,sizeZ;
    public int gridDetail;
    public Color smallGridColor = Color.white;
    public Color largeGridColor = Color.yellow;
    public bool drawSmallGrid,drawLargeGrid;

    Texture2D gridTexture()
    {
        Texture2D gridTex = new Texture2D(sizeZ * gridDetail + 1, sizeX * gridDetail + 1);
        Color[] gridColor = new Color[gridTex.width * gridTex.height];
        Color[,] gridColor2 = new Color[gridTex.width, gridTex.height];
        for (int i = 0; i < gridTex.width; i++)
        {
            for (int x = 0; x < gridTex.height; x++)
            {
                gridColor2[i, x] = Color.clear;
            }
        }

        if (drawSmallGrid)
        {
            for (int i = 0; i < gridTex.height - 1; i += gridDetail)
            {
                for (int j = 0; j < gridTex.width - 1; j++)
                {
                    gridColor2[i, j] = smallGridColor;
                }
            }
            for (int i = 0; i < gridTex.width; i++)
            {
                for (int j = 0; j < gridTex.height; j += gridDetail)
                {
                    gridColor2[i, j] = smallGridColor;
                }
            }
        }

        if (drawLargeGrid)
        {
            for (int i = 0; i < gridTex.height - 1; i += gridDetail * 4)
            {
                for (int j = 0; j < gridTex.width - 1; j++)
                {
                    gridColor2[i, j] = largeGridColor;
                }
            }
            for (int i = 0; i < gridTex.width; i++)
            {
                for (int j = 0; j < gridTex.height; j += gridDetail * 4)
                {
                    gridColor2[i, j] = largeGridColor;
                }
            }
        }

        for (int i = 0; i < gridTex.height; i++)
        {
            for (int j = 0; j < gridTex.width; j++)
            {
                gridColor[i * gridTex.width + j] = gridColor2[i, j];
            }
        }

        gridTex.SetPixels(gridColor);
        gridTex.filterMode = FilterMode.Point;
        gridTex.wrapMode = TextureWrapMode.Clamp;
        gridTex.alphaIsTransparency = true;
        gridTex.Apply();

        return gridTex;
    }
}

Any ideas?

Grid texture fading away.

\$\endgroup\$
1
\$\begingroup\$

So, upon further research, I found this: http://answers.unity3d.com/questions/10292/how-do-i-generate-mipmaps-at-runtime.html

Which pretty much answers my question. In order to generate mipmaps for the newly created texture. One should make a second texture, and then just set the pixels for the new texture to the originally created one. (Same goes for loading textures in through code from various places) Here's what I changed:

    ...
    // Modified at the bottom of my script just before the return.   
    gridTex.SetPixels(gridColor);
    gridTex.filterMode = FilterMode.Point;
    gridTex.wrapMode = TextureWrapMode.Clamp;
    gridTex.alphaIsTransparency = true;
    gridTex.Apply();

    Texture2D gridTex2 = new Texture2D(gridTex.width, gridTex.height);
    gridTex2.SetPixels(gridTex.GetPixels(0, 0, gridTex.width, gridTex.height));
    gridTex2.Apply();
    return gridTex2;
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.