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 /
avatar image
0
Question by space928 · Jul 12, 2016 at 11:47 AM · shaderterrainblending

Why wont the normals of my addpass terrain shader blend with the first pass?

So I've been deving a custom shader for Unity for quite some time now and have recently discovered a major bug with the blending between the Add pass and the First pass but it only applies in foreward rendering. I think the attached images will make the problem clear.

Deffered rendering (blending is fine) Deffered rendering (blending is fine)

Forward rendering (blending is buggy) Forward rendering (blending is buggy)

I am already certain that the problem is in the normals and not the albedo as I have experimented with disabling the normals of the shader and the problem disappears.

I will provide a truncated version of the shader and a demo scene which still exibits the same bug in forward rendering.

 Shader "Custom/Terrain Shader Test First" {
     Properties {
         [HideInInspector] _Control ("Control (RGB)", 2D) = "red" {}
         [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
         [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
         [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
         [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
         [HideInInspector] _Normal3 ("Normal 3 (A)", 2D) = "bump" {}
         [HideInInspector] _Normal2 ("Normal 2 (B)", 2D) = "bump" {}
         [HideInInspector] _Normal1 ("Normal 1 (G)", 2D) = "bump" {}
         [HideInInspector] _Normal0 ("Normal 0 (R)", 2D) = "bump" {}
         [HideInInspector] [Gamma] _Metallic0 ("Metallic 0", Range(0.0, 1.0)) = 0.0    
         [HideInInspector] [Gamma] _Metallic1 ("Metallic 1", Range(0.0, 1.0)) = 0.0    
         [HideInInspector] [Gamma] _Metallic2 ("Metallic 2", Range(0.0, 1.0)) = 0.0    
         [HideInInspector] [Gamma] _Metallic3 ("Metallic 3", Range(0.0, 1.0)) = 0.0
         [HideInInspector] _Smoothness0 ("Smoothness 0", Range(0.0, 1.0)) = 1.0    
         [HideInInspector] _Smoothness1 ("Smoothness 1", Range(0.0, 1.0)) = 1.0    
         [HideInInspector] _Smoothness2 ("Smoothness 2", Range(0.0, 1.0)) = 1.0    
         [HideInInspector] _Smoothness3 ("Smoothness 3", Range(0.0, 1.0)) = 1.0
     }
     SubShader {
         Tags {
             "SplatCount" = "4"
             "Queue" = "Geometry-100"
             "RenderType" = "Opaque"
         }
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Standard vertex:SplatmapVert fullforwardshadows //finalgbuffer:SplatmapFinalGBuffer
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         uniform sampler2D _Control;
 
         sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
         sampler2D _Normal0,_Normal1,_Normal2,_Normal3;
 
         half _Metallic0;
         half _Metallic1;
         half _Metallic2;
         half _Metallic3;
 
         half _Smoothness0;
         half _Smoothness1;
         half _Smoothness2;
         half _Smoothness3;
 
         struct Input {
             float3 worldPos;
             float2 uv_Control;
         };
 
         void SplatmapVert(inout appdata_full v, out Input data)
         {
             UNITY_INITIALIZE_OUTPUT(Input, data);
             float4 pos = mul (UNITY_MATRIX_MVP, v.vertex);
             //UNITY_TRANSFER_FOG(data, pos);
 
             v.tangent.xyz = cross(v.normal, float3(0,0,1));
             v.tangent.w = -1;
 
             //data.projPos = ComputeScreenPos(pos);
 
             //data.localPos = v.vertex.xyz;
         }
 
         void surf (Input IN, inout SurfaceOutputStandard o) {
             fixed4 splatControl = tex2D(_Control, IN.uv_Control);
 
             fixed4 splat0;
             fixed4 splat1;
             fixed4 splat2;
             fixed4 splat3;
 
             fixed4 normal0;
             fixed4 normal1;
             fixed4 normal2;
             fixed4 normal3;
 
             float2 _ScaledUV = IN.worldPos.xz / 50;
 
             normal0 = tex2D(_Normal0, _ScaledUV);
             normal1 = tex2D(_Normal1, _ScaledUV);
             normal2 = tex2D(_Normal2, _ScaledUV);
             normal3 = tex2D(_Normal3, _ScaledUV);
 
             splat0 = tex2D(_Splat0, _ScaledUV);
                           splat1 = tex2D(_Splat1, _ScaledUV);
             splat2 = tex2D(_Splat2, _ScaledUV);
              splat3 = tex2D(_Splat3, _ScaledUV);
 
             fixed4 blendSplat = splatControl;
             int b1 = 1;
             int b2 = 0;
             int b3 = 0;
             int b4 = 0;
 
             fixed4 finalCol;
             finalCol  = blendSplat.r * splat0;
             finalCol += blendSplat.g * splat1;
             finalCol += blendSplat.b * splat2;
             finalCol += blendSplat.a * splat3;
             finalCol /= b1 + b2 + b3 + b4;
 
             fixed4 nrm;
             nrm  = blendSplat.r * normal0;
             nrm += blendSplat.g * normal1;
             nrm += blendSplat.b * normal2;
             nrm += blendSplat.a * normal3;
             nrm /= b1 + b2 + b3 + b4;
 
             half finalGloss = dot(blendSplat, half4(_Smoothness0, _Smoothness1, _Smoothness2, _Smoothness3)) / (b1 + b2 + b3 + b4);
 
             fixed Alpha = dot(splatControl, 1);
             nrm = (nrm*2)-1;
 
             o.Normal = float3(nrm.rg, 1);
             o.Albedo = finalCol;
             o.Alpha = Alpha;
             o.Smoothness = finalGloss;
             o.Metallic = dot(blendSplat, half4(_Metallic0, _Metallic1, _Metallic2, _Metallic3)) / (b1 + b2 + b3 + b4);
         }
         ENDCG
     }
     Dependency "AddPassShader" = "Custom/Terrain Shader Test Add"
     Dependency "BaseMapShader" = "Hidden/TerrainEngine/Splatmap/Standard-Base"
 
     Fallback "Nature/Terrain/Diffuse"
 }

I have also attached a scene which clearly illustrates the problem at the following link: https://drive.google.com/file/d/0B7XHf-0bgF2-NjFfRmpLUzR6UzQ/view?usp=sharing

Note: if you require anymore information please feel free to ask.

Thank you in advance.

2016-07-11-1.png (322.9 kB)
2016-07-11.png (395.3 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

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can I get this type of terrain texture? 1 Answer

How do you turn off terrain blending? 1 Answer

Blending Reflection 0 Answers

shader for light replacement blending, instead of additive blending? 0 Answers

Silhouette overlay shader 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