Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by The_ZeeT · Dec 29, 2018 at 10:40 PM · shadertextureshadows

How to use UV3 to send coordinates to the Custom Sheder (blending textures and having shadows)

Hi! I have a procedurally generated objects with two textures: first for the base color, second for the shading.

Both textures have separate UV coordinates for each triangle, and use UV and UV2 for sending them to this custom shader:

 Shader "Blended Textures" {
     Properties {
         _MainTex ("Base (RGB)", 2D) = "white" {}
         _BlendTex ("Alpha Blended (RGBA) ", 2D) = "white" {}
     }
     SubShader {
         Pass {
             BindChannels {
                Bind "Vertex", vertex
                Bind "texcoord", texcoord0
                Bind "texcoord1", texcoord1
             }
             SetTexture [_MainTex] {
                 combine texture
             }
             SetTexture [_BlendTex] {
                 combine texture lerp (texture) previous
             }
         }
     }
 }



This works, but objects do not cast or receive shadows, and i tried to fix it by re-writing my shader and combining it with Unity's standard shader.

Because Unity uses UV2 for lightprobes, i have to move second texture coordinates onto UV3, but how do i do that?

No matter how i change my shader, second texture keeps using the same coordinates as primary texture

alt text

I even send second UV coordintates to diferent UV's:

 mesh.uv = uvone; // works
 mesh.uv2 = uvtwo; // do not work
 mesh.uv3 = uvtwo; // do not work
 mesh.uv4 = uvtwo; // do not work
 
 mesh.SetUVs(2, UVs2); // do not work
 mesh.SetUVs(3, UVs2); // do not work


Now i have this mess of code, that only partially works, how do i fix it?

 Shader "Custom/TestBlending 2"
      {
          Properties
          {
              _MainTex("Base (RGB)", 2D) = "white" {}
              _Texture2("Alpha Blended (RGBA) ", 2D) = "white" {}
              _Color ("Color", Color) = (1,1,1,1)
          }
          
          SubShader
          {
              Tags
              {
                  "RenderType"="Opaque" 
              }
              
                  
              BindChannels
              {
                  Bind "Color", color
                  Bind "Vertex", vertex
                  Bind "TexCoord", texcoord
                  Bind "Texcoord2", texcoord2
                  Bind "Texcoord3", texcoord3
              }
              
              Pass
              {
                  SetTexture[_MainTex]
                  
                  SetTexture[_Texture2]
                  {
                     combine texture lerp (texture) previous
                  }     
              }
             
             CGPROGRAM
             // Physically based Standard lighting model, and enable shadows on all light types
             #pragma surface surf Standard fullforwardshadows
     
             // Use shader model 3.0 target, to get nicer looking lighting
             #pragma target 3.0
     
             sampler2D _MainTex;
             sampler2D _Texture2;
             sampler2D _Blended;
             
             struct appdata
             {
                 float4 vertex    : POSITION;  // The vertex position in model space.
                 float3 normal    : NORMAL;    // The vertex normal in model space.
                 float4 texcoord  : TEXCOORD0; // The first UV coordinate.
                 float4 texcoord1 : TEXCOORD1; // The second UV coordinate.
                 float4 texcoord2 : TEXCOORD2; // The third UV coordinate.
                 float4 texcoord3 : TEXCOORD3; // The fourfth UV coordinate.
             };
             
             struct Input {
                 float2 uv_MainTex: TEXCOORD0;
                 float2 uv_Texture2 : TEXCOORD2;
                 float2 uv_Blended;
             };
             
             void vert(inout appdata v, out Input o) {
                 UNITY_INITIALIZE_OUTPUT(Input, o);
                
                 o.uv_MainTex = v.texcoord.xy;
      
                 o.uv_Texture2 = v.texcoord3.xy;
                 
             }
     
             fixed4 _Color;
     
             UNITY_INSTANCING_BUFFER_START(Props)
                 
             UNITY_INSTANCING_BUFFER_END(Props)
     
             void surf (Input IN, inout SurfaceOutputStandard o) {
             
                 fixed4 t1  = tex2D( _MainTex, IN.uv_MainTex );
                 fixed4 t2  = tex2D( _Texture2, IN.uv_Texture2 ) * 1;
                 o.Albedo  = lerp( t1, t2, 0.5 ) ;
                 
             }
             ENDCG
          }
          FallBack "Diffuse"
      }
 
 
 


unity-help-04.png (59.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by The_ZeeT · Dec 30, 2018 at 02:46 AM

Fixed the issue!

Turns out, to use uv's you need to name them correctly, like this:

UV%Number%_%TextureName%

uv3_TestTexture

uv4_AnotherTexture

alt text


Full shader:

 Shader "Custom/BlendedTexturesWithShadows"
 {
     Properties
     {
         _MainTex("Base (RGB)", 2D) = "white" {}
         _Texture2("AO (RGB)", 2D) = "white" {}
     }
      
     SubShader
     {
      
         Tags
         {
             "RenderType"="Opaque"           
 
         }
 
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
         
         #include "AutoLight.cginc"
         
         sampler2D _MainTex;
         sampler2D _Texture2;
                 
         
         
         struct Input 
         {
             float2 uv_MainTex: TEXCOORD0;
             float2 uv4_Texture2: TEXCOORD3;
         };
         
         
         #pragma instancing_options assumeuniformscaling
         UNITY_INSTANCING_BUFFER_START(Props)
             
         UNITY_INSTANCING_BUFFER_END(Props)
 
         void surf (Input IN, inout SurfaceOutputStandard o) {
         
             fixed4 t1  = tex2D( _MainTex, IN.uv_MainTex.xy );
             fixed4 t2  = tex2D( _Texture2, IN.uv4_Texture2.xy ) * 1;
             fixed4 c = lerp( t1, t2, 0.5 ) ;
             o.Albedo = c.rgb;
             o.Alpha = c.a;
         }
         ENDCG
      }
      FallBack "Diffuse"
  }



unity-help-05.png (53.8 kB)
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

245 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Something wrong with the cut out feature of the standard shader? 0 Answers

Small bug? on ShaderGraph 0 Answers

What is the best way to handle transparency? 0 Answers

How can I make this custom shader support shadows? 1 Answer

How can I apply texture through shader on sprite without streching? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges