Search Unity

Applying a texture to a plane, through script

Discussion in 'General Graphics' started by boorb, Dec 6, 2018.

  1. boorb

    boorb

    Joined:
    Mar 18, 2016
    Posts:
    22
    I use this to create the mesh:
    Code (CSharp):
    1.     public static Mesh Create(float width, float height)
    2.     {
    3.         Mesh m = new Mesh();
    4.         m.name = "PlaneMesh";
    5.         m.vertices = new Vector3[] {
    6.          new Vector3(0, 0, 0.01f),
    7.          new Vector3(width, 0, 0.01f),
    8.          new Vector3(0, height, 0.01f),
    9.          new Vector3(width, height, 0.01f)
    10.      };
    11.         m.uv = new Vector2[] {
    12.          new Vector2(0, 0),
    13.          new Vector2(1, 0),
    14.          new Vector2(0, 1),
    15.          new Vector2(1, 1)
    16.      };
    17.         m.triangles = new int[] { 0, 2, 1, 0, 3, 2 };
    18.         m.RecalculateNormals();
    19.  
    20.         Color[] colors = new Color[m.vertices.Length];
    21.         for (int i = 0; i < m.vertices.Length; i++)
    22.             colors[i] = new Color(0f, 1f, 0f);
    23.         m.colors = colors;
    24.  
    25.         return m;
    26.     }

    Then I use this to apply the texture:
    Code (CSharp):
    1. quadObject.GetComponent<Renderer>().material.SetTexture("_MainTex", EchoView.Update(nextZ, texture3d, quadMesh));
    Where EchoView returns a 2D texture of the same width and height as the mesh of the gameObject.

    However, when I apply this texture, I don't see it. I can see in unity that the shader/texture/material sphere changes to that of the texture, but I can't see it on the gameObject itself.

    I am using a shader and as I don't understand those (yet) it may have to do with that:
    Code (CSharp):
    1.     Shader "Custom/PreZ Standard Fade" {
    2.         Properties {
    3.             _Color ("Color", Color) = (1,1,1,1)
    4.             _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.             _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.             _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         }
    8.         SubShader {
    9.             Tags { "Queue"="Transparent" "RenderType"="Transparent" "IgnoreProjectors"="True"}
    10.             LOD 200
    11.    
    12.             // ZWrite pre-pass
    13.             Pass {
    14.                 ZWrite On
    15.                 ColorMask 0
    16.    
    17.                 CGPROGRAM
    18.                 #pragma vertex vert
    19.                 #pragma fragment frag
    20.                 #include "UnityCG.cginc"
    21.    
    22.                 float4 vert(float4 vertex : POSITION) : SV_POSITION { return UnityObjectToClipPos(vertex); }
    23.                 fixed4 frag() : SV_Target { return 0; }
    24.                 ENDCG
    25.             }
    26.        
    27.             CGPROGRAM
    28.             // Physically based Standard lighting model, and enable shadows on all light types
    29.             #pragma surface surf Standard fullforwardshadows alpha:fade
    30.    
    31.             // Use shader model 3.0 target, to get nicer looking lighting
    32.             #pragma target 3.0
    33.    
    34.             sampler2D _MainTex;
    35.    
    36.             struct Input {
    37.                 float2 uv_MainTex;
    38.             };
    39.    
    40.             half _Glossiness;
    41.             half _Metallic;
    42.             fixed4 _Color;
    43.    
    44.             void surf (Input IN, inout SurfaceOutputStandard o) {
    45.                 // Albedo comes from a texture tinted by color
    46.                 fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    47.                 o.Albedo = c.rgb;
    48.                 // Metallic and smoothness come from slider variables
    49.                 o.Metallic = _Metallic;
    50.                 o.Smoothness = _Glossiness;
    51.                 o.Alpha = c.a;
    52.             }
    53.             ENDCG
    54.         }
    55.         FallBack "Diffuse"
    56.     }
    57.  

    This code displays a bright fuschia pink plane. Whereas the texture is a black and white image.

    Does anyone know what I have to add/edit in order for the texture to show?

    Thanks in advance
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Does the material work before you set a texture on it? I'm also concerned by the fact the EchoView function is being passed a variable named "texture3d", is that a typo or is the texture you're trying set on the material actually a Texture3D asset instead of a Texture2D?
     
  3. boorb

    boorb

    Joined:
    Mar 18, 2016
    Posts:
    22
    It is a Texture3D and I take a Texture2D out of that. I ended up giving up on planes and I'm using a RawImage instead now.