Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Texture applied to plane is blurred when the camera is moved away from it.

Discussion in 'Editor & General Support' started by FuturePilot1701, Jun 13, 2020.

  1. FuturePilot1701

    FuturePilot1701

    Joined:
    Jul 31, 2019
    Posts:
    8
    As I move the camera away from the plane, the texture on it is blurred to white. I have fog turned off. This happens in both the scene and game view, and the distance at which it blurs scales with the scale of the plane object.



    This is the code I use to generate the texture using perlin noise:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor;
    5. using UnityEngine;
    6.  
    7. public class generateMap : MonoBehaviour
    8. {
    9.     //public int mapWidth;
    10.     //public int mapHeight;
    11.     public int textureWidth;
    12.     public int textureHeight;
    13.     public Texture2D mapTexture;
    14.     float[,] terrainHeights;
    15.     Color32[] terrainColors;
    16.     float maxHeight;
    17.     float minHeight;
    18.     public float xSeed;
    19.     public float ySeed;
    20.     public float terrainDifference;
    21.     public float terrainZoom = 10;
    22.     float e;
    23.     public float redistribution = 1;
    24.  
    25.     // Start is called before the first frame update
    26.     void Start()
    27.     {
    28.  
    29.         mapTexture = generateMapTexture(textureWidth, textureHeight, xSeed, ySeed, terrainZoom);
    30.         mapTexture.filterMode = FilterMode.Trilinear;
    31.         mapTexture.anisoLevel = 9;
    32.  
    33.         GetComponent<MeshRenderer>().material.SetTexture("_MainTex", mapTexture);
    34.  
    35.         //Rect rec = new Rect(0, 0, mapWidth, mapHeight);
    36.         //last number changes pixels per unit, which would allow the resolution of the texture to be increased
    37.         //GetComponent<SpriteRenderer>().sprite = Sprite.Create(mapTexture, rec, new Vector2(0.5f, 0.5f), 1);
    38.     }
    39.  
    40.     Texture2D generateMapTexture(int width, int height, float xSeed, float ySeed, float zoom)
    41.     {
    42.         mapTexture = new Texture2D(width, height);
    43.  
    44.         terrainHeights = new float[width, height];
    45.  
    46.         //loop through each pixel in the texture and apply noise filters to it to generate a world map
    47.         for (int y = 0; y < height; y++)
    48.         {
    49.             for (int x = 0; x < width; x++)
    50.             {
    51.  
    52.                 e = Mathf.PerlinNoise(
    53.                     ((float)x + xSeed) / terrainZoom,
    54.                     ((float)y + ySeed) / terrainZoom)
    55.                     +
    56.                     //second octave of noise
    57.                     0.5f * Mathf.PerlinNoise(
    58.                     (2f * (float)x + xSeed) / terrainZoom,
    59.                     (2f * (float)y + ySeed) / terrainZoom)
    60.                     +
    61.                     //third octave of noise
    62.                     0.25f * Mathf.PerlinNoise(
    63.                     (4f * (float)x + xSeed) / terrainZoom,
    64.                     (4f * (float)y + ySeed) / terrainZoom);
    65.  
    66.                 terrainHeights[x, y] = Mathf.Pow(e, redistribution);
    67.  
    68.                 if (terrainHeights[x, y] < minHeight)
    69.                 {
    70.                     minHeight = terrainHeights[x, y];
    71.                 }
    72.                 else if (terrainHeights[x, y] > maxHeight)
    73.                 {
    74.                     maxHeight = terrainHeights[x, y];
    75.                 }
    76.             }
    77.         }
    78.  
    79.         terrainDifference = maxHeight - minHeight;
    80.  
    81.         //TO GENERATE A MORE REALISTIC COLOR, FOR EACH HEIGHT RANGE, A COLOR WILL BE CHOSEN RANDOMLY FROM A RANGE OF THAT SPECIFIC COLOR
    82.         //EX: FOR ALL POINTS IN THE GREEN HEIGHT RANGE, A RANDOM COLOR OF GREEN WILL BE CHOSEN, SO ITS NOT JUST A FLAT COLOR
    83.         //ALTHOUGH THIS FLAT COLOR COULD BE PART OF THE SIMPLE AESTHETIC, WHICH WOULD SAVE ALOT OF TIME IF ALL MODELS WERE SIMILARLY SIMPLE
    84.  
    85.         terrainColors = new Color32[width * height];
    86.         int colorArrayIndex = 0;
    87.         Color32 pixelColor = new Color32();
    88.  
    89.         //loop through each terrain pixel again, now that min and max terrain height is known
    90.         for (int y = 0; y < height; y++)
    91.         {
    92.             for (int x = 0; x < width; x++)
    93.             {
    94.                 //set terrain color based on "height" calculated by perlin noise
    95.                 //mountain top
    96.                 if (terrainHeights[x, y] / terrainDifference < 1f)
    97.                 {
    98.                     pixelColor = new Color32(255, 255, 255, 255);
    99.                     terrainColors[colorArrayIndex] = pixelColor;
    100.                 }
    101.  
    102.                 //mountain slope
    103.                 if (terrainHeights[x, y] / terrainDifference < 0.9f)
    104.                 {
    105.                     pixelColor = new Color32(96, 103, 107, 255);
    106.                     terrainColors[colorArrayIndex] = pixelColor;
    107.                 }
    108.  
    109.                 //grass
    110.                 if (terrainHeights[x, y] / terrainDifference < 0.75f)
    111.                 {
    112.                     pixelColor = new Color32(79, 121, 66, 255);
    113.                     terrainColors[colorArrayIndex] = pixelColor;
    114.                 }
    115.  
    116.                 //sand
    117.                 if (terrainHeights[x, y] / terrainDifference < 0.5f)
    118.                 {
    119.                     pixelColor = new Color32(255, 217, 199, 255);
    120.                     terrainColors[colorArrayIndex] = pixelColor;
    121.                 }
    122.  
    123.                 //water
    124.                 if (terrainHeights[x, y] / terrainDifference < 0.45f)
    125.                 {
    126.                     pixelColor = new Color32(0, 122, 199, 255);
    127.                     terrainColors[colorArrayIndex] = pixelColor;
    128.                 }
    129.  
    130.                 //deep water
    131.                 if (terrainHeights[x, y] / terrainDifference < 0.35f)
    132.                 {
    133.                     pixelColor = new Color32(0, 105, 148, 255);
    134.                     terrainColors[colorArrayIndex] = pixelColor;
    135.                 }
    136.  
    137.                 colorArrayIndex++;
    138.  
    139.             }
    140.  
    141.         }
    142.  
    143.         mapTexture.SetPixels32(terrainColors, 0);
    144.  
    145.         mapTexture.Apply(false);
    146.  
    147.         return mapTexture;
    148.     }
    149.  
    150. }
     

    Attached Files:

  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    This could be due to texture filtering.

    Try setting the filter mode of your texture to Point:
    Code (CSharp):
    1.         mapTexture = new Texture2D(width, height);
    2.         mapTexture.filterMode = FilterMode.Point;
     
  3. FuturePilot1701

    FuturePilot1701

    Joined:
    Jul 31, 2019
    Posts:
    8
    That just resulted in the blur having a harsher cutoff
     

    Attached Files: