Search Unity

How to set Material's Texture in Unity Render Pipeline (LWRP in my case)

Discussion in 'Scripting' started by arsnyan2002, May 2, 2019.

  1. arsnyan2002

    arsnyan2002

    Joined:
    Apr 30, 2019
    Posts:
    30
    So, my main problem is that I watched some tutorial on procedural mesh generation, which was made in 2016. There was the next solution to apply texture to model:

    Code (CSharp):
    1. // MapDisplay class
    2. using UnityEngine;
    3.  
    4. public class MapDisplay : MonoBehaviour
    5. {
    6.     public Renderer textureRender;
    7.  
    8.     public void DrawTexture(Texture2D texture)
    9.     {
    10.         textureRender.sharedMaterial.mainTexture = texture;
    11.         textureRender.transform.localScale = new Vector3(texture.width, 1, texture.height);
    12.     }
    13. }
    14.  
    15.  
    16. // MapGenerator initalization
    17. MapDisplay display = FindObjectOfType<MapDisplay>();
    18.         if (drawMode == DrawMode.NoiseMap)
    19.             display.DrawTexture(TextureGenerator.TextureFromHeightMap(noiseMap));
    20.         else if (drawMode == DrawMode.ColorMap)
    21.             display.DrawTexture(TextureGenerator.TextureFromColorMap(colorMap, mapWidth, mapHeight));
    22.  
    23.  
    24. // TextureGenerator class
    25. using UnityEngine;
    26.  
    27. public static class TextureGenerator
    28. {
    29.     public static Texture2D TextureFromColorMap(Color[] colorMap, int width, int height)
    30.     {
    31.         Texture2D texture = new Texture2D(width, height);
    32.         texture.filterMode = FilterMode.Point;
    33.         texture.wrapMode = TextureWrapMode.Clamp;
    34.         texture.SetPixels(colorMap);
    35.         texture.Apply();
    36.  
    37.         return texture;
    38.     }
    39.  
    40.     public static Texture2D TextureFromHeightMap(float[,] heightMap)
    41.     {
    42.         int width = heightMap.GetLength(0);
    43.         int height = heightMap.GetLength(1);
    44.  
    45.         Texture2D texture = new Texture2D(width, height);
    46.  
    47.         Color[] colorMap = new Color[width * height];
    48.         for (int y = 0; y < height; y++)
    49.         {
    50.             for (int x = 0; x < width; x++)
    51.             {
    52.                 colorMap[y * width + x] = Color.Lerp(Color.black, Color.white, heightMap[x, y]);
    53.             }
    54.         }
    55.  
    56.         return TextureFromColorMap(colorMap, width, height);
    57.     }
    58. }
    This tutorial is about creating a terrain with Perlin Noise. (I'm actually at E05)

    I've tried to find some solutions and I've found this. The main problem is that it doesn't work and gives an error "Instantiating material due to calling renderer.material during edit mode. This will leak materials into the scene.". That solution just doesn't work, so I can't get any type of displaying my material since the whole beggining.

    The intresting fact is that the material by itself (Unlit/Texture) has it's texture just like it should have. But not the plane which I use to create a Terrain. You can see it here


    So I've tried to get back to using sharedMaterial and I'm still stuck in this.
    The full code

    Specs:

    - Unity 2019.1.0f2 with Lightweight Render Pipeline
    - Me, stupid idiot

    Please help me! :( I've been trying to find answers for some hours, but I haven't get any.
     
  2. arsnyan2002

    arsnyan2002

    Joined:
    Apr 30, 2019
    Posts:
    30
    I'm gonna up this, cause someone may not even know that this question exists. Still not found a solution.
     
  3. arsnyan2002

    arsnyan2002

    Joined:
    Apr 30, 2019
    Posts:
    30
    Okay, what I figured out, is that every material is not showing up while I'm trying to use it. It's just gray ALWAYS. What's wrong can be?
     
  4. arsnyan2002

    arsnyan2002

    Joined:
    Apr 30, 2019
    Posts:
    30
    Now that's a total mess. Got fixed by just one click. And it took me more than 5 hours...

    So, to fix that, I should have simply remove mark from fog parameter in Lightning Settings. Now I need some psychological help > ._.>
     
    nobluff67 likes this.