Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Seamless texture shader unlit query

Discussion in 'Shaders' started by hamstar, Feb 21, 2014.

  1. hamstar

    hamstar

    Joined:
    Sep 25, 2013
    Posts:
    84
    I have no experience with shaders, but it looks like I might have to learn! I just wondered if someone can confirm whether this is possible before I dive in?

    I need a shader that will modify uvs in order to "line up" a texture across adjacent objects (2D), so that it looks seamless.

    The shader posted in an answer here does what I want, except I don't need/want lighting. My question is whether this is possible without using a Surface shader? The docs state not to use surface shaders if the surface isn't using light, I guess for performance.
     
  2. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Code (csharp):
    1. CGPROGRAM
    2. #pragma vertex vert
    3. #pragma fragment frag
    4.  
    5. sampler2D _MainTex;
    6. float4 _Color;
    7. float _Scale;
    8.  
    9. struct vOut {
    10. float4 texCoords;
    11. };
    12.  
    13. vOut vert ( appdata_base i )
    14. {
    15. vOut o;
    16. float3 worldNormal = normalize( mul( float4( i.normal, 0.0 ), _World2Object ).xyz );
    17. float3 worldPos = mul( UNITY_MATRIX_MVP, i.vertex );
    18. o.texCoords = i.texcoord;
    19.  
    20. if(worldNormal.y>0.5) UV = o.texCoords.xy = worldPos.xz;
    21. else if(abs(worldNormal.x)>0.5) o.texCoords.xy = worldPos.yz; // side
    22. else o.texCoords.xy = worldPos.xy;
    23.  
    24. return o;
    25. }
    26.  
    27. fixed4 frag ( vOut f )
    28. {
    29. fixed4 mainTex = tex2D( _MainTex, f.texCoords.xy * _Scale) * _Color;
    30. return mainTex;
    31. }
    32.  
    33. ENDCG
    34.  
    That's a vertex/fragment shader that I think may do something similar to what the subshader does. There may be some errors in places but it's the basic idea.
     
  3. hamstar

    hamstar

    Joined:
    Sep 25, 2013
    Posts:
    84
    Thank you that's very helpful! I'm working through the errors, but stuck on one at the moment.
     
  4. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Try

    Code (csharp):
    1. fixed4 frag ( vOut f ) : COLOR
     
  5. hamstar

    hamstar

    Joined:
    Sep 25, 2013
    Posts:
    84
    Thanks. The texture is now showing and is "seamless", but the texture "moves" with the camera. I thought using another built in matrix might fix it but no luck.
    Thanks for the help and sorry for being a bit useless :)

    Code (csharp):
    1. Shader "Custom/SimpleUnlitWorldAligned" {
    2.     Properties {
    3.         _MainTex ("Texture", 2D) = ""
    4.         _Scale ("Texture Scale Multiplier", Float) = 0.1
    5.     }
    6.  
    7.     SubShader {
    8.         Pass {
    9.             CGPROGRAM
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.             #include "UnityCG.cginc"
    13.  
    14.             sampler2D _MainTex;
    15.             float _Scale;
    16.  
    17.             struct vOut {
    18.                 float4 texCoords;
    19.                 float4 position : POSITION;
    20.             };
    21.  
    22.             vOut vert ( appdata_base i )
    23.             {
    24.                 vOut o;
    25.                 o.position = mul (UNITY_MATRIX_MVP, i.vertex);
    26.                
    27.                 float3 worldNormal = normalize( mul( float4( i.normal, 0.0 ), _World2Object ).xyz );
    28.                 float3 worldPos =  (float3) mul( UNITY_MATRIX_MV, i.vertex );
    29.                 o.texCoords = i.texcoord;
    30.  
    31.                 if(worldNormal.y>0.5)
    32.                     o.texCoords.xy = worldPos.xz; // top
    33.                 else if(abs(worldNormal.x)>0.5)
    34.                     o.texCoords.xy = worldPos.yz; // side
    35.                 else
    36.                     o.texCoords.xy = worldPos.xy; // front         
    37.  
    38.                 return o;
    39.             }
    40.  
    41.             fixed4 frag ( vOut f ) : COLOR
    42.             {
    43.                 fixed4 mainTex = tex2D( _MainTex, f.texCoords.xy * _Scale);
    44.                 return mainTex;
    45.             }
    46.  
    47.             ENDCG
    48.         }
    49.     }
    50. }
     
  6. hamstar

    hamstar

    Joined:
    Sep 25, 2013
    Posts:
    84
    OK this seems to be working. Just one warning now!
    Code (csharp):
    1. vOut vert ( appdata_base i )
    2. {
    3.     vOut o;
    4.     o.position = mul (UNITY_MATRIX_MVP, i.vertex);
    5.    
    6.     float3 worldNormal = normalize( mul( _Object2World, float4( i.normal, 0.0 ) ).xyz );
    7.     float3 worldPos = mul (_Object2World, i.vertex).xyz;
    8.     o.texCoords = i.texcoord;
    9.  
    10.     if(worldNormal.y>0.5)
    11.         o.texCoords.xy = worldPos.xz; // top
    12.     else if(abs(worldNormal.x)>0.5)
    13.         o.texCoords.xy = worldPos.yz; // side
    14.     else
    15.         o.texCoords.xy = worldPos.xy; // front         
    16.  
    17.     return o;
    18. }
    Warning:
    Will have another look on Monday.
     
  7. hamstar

    hamstar

    Joined:
    Sep 25, 2013
    Posts:
    84
    Seems OK now. Here's the final shader. Don't ask me if it's perfect. :p
    Code (csharp):
    1. Shader "Custom/UnlitWorldAligned2D" {
    2.     Properties {
    3.         _MainTex ("Texture", 2D) = ""
    4.         _Scale ("Texture Scale Multiplier", Float) = 0.1
    5.     }
    6.  
    7.     SubShader {
    8.         Pass {
    9.             CGPROGRAM
    10.             #pragma vertex vert
    11.             #pragma fragment frag
    12.             #include "UnityCG.cginc"
    13.  
    14.             sampler2D _MainTex;
    15.             float _Scale;
    16.  
    17.             struct vOut {
    18.                 fixed4 texCoords : TEXCOORD0;
    19.                 fixed4 position : POSITION;
    20.             };
    21.  
    22.             vOut vert (appdata_base i) {
    23.                 vOut o;            
    24.                 o.position = mul(UNITY_MATRIX_MVP, i.vertex);
    25.                 o.texCoords = i.texcoord;
    26.                
    27.                 float3 worldPos = mul(_Object2World, i.vertex).xyz;    
    28.        
    29.                 // remove comments on following lines for a 3D shader                          
    30.         //      float3 worldNormal = normalize(mul(float4(i.normal, 0.0 ), _World2Object).xyz);
    31.         //      if (worldNormal.y > 0.5)
    32.         //          o.texCoords.xy = worldPos.xz; // top
    33.         //      else if (abs(worldNormal.x) > 0.5)
    34.         //          o.texCoords.xy = worldPos.yz; // side
    35.         //      else
    36.                     o.texCoords.xy = worldPos.xy; // front         
    37.  
    38.                 return o;
    39.             }
    40.  
    41.             fixed4 frag (vOut f) : COLOR {
    42.                 fixed4 mainTex = tex2D(_MainTex, f.texCoords.xy * _Scale);
    43.                 return mainTex;
    44.             }
    45.  
    46.             ENDCG
    47.         }
    48.     }
    49. }
     
    Last edited: Feb 22, 2014
  8. varshit

    varshit

    Joined:
    Dec 6, 2016
    Posts:
    4
    thank you sooooooooooooo much, thank you sooooo sooo sooo much. i was about to ask a doubt but while typing itself i found the answer. for those who cant get the texture repeated, change the texture from clamped to repeat mode in inspector. thank you soo much guys. love you all
     
  9. Burje

    Burje

    Joined:
    Dec 18, 2016
    Posts:
    1
    Hey looks it's 2018 and I just had to tip my hat to Hamstar, Shader works like a dream
    <3