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

Use Custom Shader to Determine Texture Coordinates

Discussion in 'Shaders' started by sys12, Jul 7, 2015.

  1. sys12

    sys12

    Joined:
    Apr 27, 2015
    Posts:
    9
    I'm currently writing a custom shader to determine the texture coordinates by calculating the final coordinates in vert method and use the results to fetch colors from the main texture in frag methods.

    But the thing is that I can't fetch the color by calling tex2D method which uses '_MainTex' and a float4 variable. The error says that 'Undeclared identifier '_MainTex''. My shader code is as follows:

    Code (CSharp):
    1.  
    2. Shader "Custom/Point" {
    3.     Properties {
    4.         _MainTex ("Base (RGB)", 2D) = "white" { }
    5.     }
    6.     SubShader {
    7.         Pass{
    8.             //ZTest Always Cull Off ZWrite Off
    9.             //Fog { Mode off }
    10.             Tags { "RenderType"="Opaque" }
    11.             CGPROGRAM
    12.             #include "UnityCG.cginc"
    13.             #pragma target 5.0
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #pragma debug
    17.             struct Vert
    18.             {
    19.                 float3 position : POSITION;
    20.                 float3 normal : NORMAL;
    21.                 float4 texCoord : TEXCOORD0;
    22.             };
    23.  
    24.             uniform StructuredBuffer<Vert> m_buffer;
    25.             uniform float4 m_yawPitch;
    26.             uniform float4 m_worldPosition;
    27.             uniform float4 m_index;
    28.             uniform float4 m_color1;
    29.             uniform float4 m_color2;
    30.  
    31.             struct v2f
    32.             {
    33.                 float4 position : SV_POSITION;
    34.                 float3 normal : TEXCOORD0;
    35.                 float4 texCoord : TEXCOORD1;
    36.                 float3 color : COLOR;
    37.             };
    38.  
    39.             v2f vert(uint id : SV_VertexID)
    40.             {
    41.                 Vert vert = m_buffer[id];
    42.                 v2f OUT;
    43.                 float4x4 world = {
    44.                 m_yawPitch.x, -m_yawPitch.y, 0, 0,
    45.                 m_yawPitch.z * m_yawPitch.y, m_yawPitch.z * m_yawPitch.x, m_yawPitch.w, 0,
    46.                 -m_yawPitch.w * m_yawPitch.y, -m_yawPitch.w * m_yawPitch.x, m_yawPitch.z, 0,
    47.                 m_worldPosition };
    48.                 OUT.position = mul(vert.position, world);
    49.                 OUT.position = mul(OUT.position, UNITY_MATRIX_V);
    50.                 OUT.position = mul(OUT.position, UNITY_MATRIX_P);
    51.                 OUT.normal = mul(vert.normal, world);
    52.                 OUT.color = lerp(m_color1, m_color2, vert.texCoord.w);
    53.                 float texIndex = lerp(lerp(m_index.x, m_index.y, vert.texCoord.w), 15, vert.texCoord.z);
    54.                 OUT.texCoord = float4((texIndex % 4 + vert.texCoord.x) / 4,(((int)texIndex / 4) + vert.texCoord.y) / 4,0,0);
    55.                 return OUT;
    56.             }
    57.  
    58.             float4 frag(v2f IN) : COLOR
    59.             {
    60.                 return float4((.5f * (1 - dot(normalize(_WorldSpaceLightPos0.xyz),float3(IN.normal.x,-IN.normal.y,IN.normal.z)) * (1 - tex2D(_MainTex, IN.texCoord).a) * IN.color.rgb), 1);
    61.             }
    62.             ENDCG
    63.         }
    64.     }
    65.     Fallback "Diffuse"
    66. }
    67.  
    68.  
    Thanks for reading, any help would be appreciated.
     
    Last edited: Jul 7, 2015
  2. CPXTom

    CPXTom

    Joined:
    Apr 24, 2010
    Posts:
    113
    you need to declare:

    Code (csharp):
    1.  
    2. uniform sampler2D _MainTex;
    3.  
    with the rest of your uniforms.

    Also as an optimization you can just do one matrix multiplication in your vertex shader:
    Code (csharp):
    1.  
    2. OUT.position = mul(OUT.position,UNITY_MATRIX_VP);
    3.  
     
  3. sys12

    sys12

    Joined:
    Apr 27, 2015
    Posts:
    9
    Thanks for your answer and suggestions. Actually I got it compiled yesterday but it is not working properly as the material transparent now. Do you have any ideas?
     
  4. sys12

    sys12

    Joined:
    Apr 27, 2015
    Posts:
    9
    And there are a lot of errors saying that 'DestroyBuffer can only be called from the main thread.'.
     
  5. CPXTom

    CPXTom

    Joined:
    Apr 24, 2010
    Posts:
    113
    Your shader is set to Opaque, you want to change this line:

    Code (csharp):
    1. Tags {"RenderType"="Opaque"}
    to

    Code (csharp):
    1. Tags {"RenderType"="Transparent" Queue ="Transparent"}
    Also add:

    Code (csharp):
    1. Blend SrcAlpha OneMinusSrcAlpha
    Also your fragment shader is always writing 1 for alpha. you'll want to adjust that value to the correct alpha.
    I'm unsure what would be causing those DestroyBuffer errors you're receiving
     
  6. sys12

    sys12

    Joined:
    Apr 27, 2015
    Posts:
    9

    Thanks a lot. Actually I want my shader to be opaque not transparent, so the render type should be fine.