Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by markv12 · Jan 18, 2017 at 08:12 AM · camerashadershadersgraphicsshaderlab

Custom vertex shader doesn't write to camera depth normals texture.

I'm attempting to write a custom vertex shader, but I'm finding that unlike the standard shader, my shader does not write to the camera depth normals texture. (UpdateDepthNormalsTexture step in the frame debugger)

It is my understanding that the UpdateDepthNormalsTexture works using a built-in replacement shader called "Hidden/Internal-DepthNormalsTexture" Looking at that built-in shader I see that it has a subshader with Tags { "RenderType"="Opaque" } My subshader has the same RenderType, so I would expect it to work with the replacement shader.

The DepthNormalsTexture is used by the SSAO script so the SSAO ends up ignoring objects in front of the camera using my custom shader.

My question is, how can I get my custom shader to render to the depth normals texture?

Shader:

 Shader "Custom/WrappedDiffuse" {
 
     Properties {
         _Tint ("Tint", Color) = (1, 1, 1, 1)
         _MainTex ("Texture", 2D) = "white" {}
         _WrapTex("Wrap Texture", 2D) = "white" {}
         _WrapShift("Wrap Shift", Range(-1, 1)) = 0
         _BumpMap("Normals", 2D) = "bump" {}
         _BumpScale("Bump Scale", Float) = 1
     }
 
     SubShader {
 
         Pass {
             Tags {
                 "RenderType" = "Opaque"
                 "Queue" = "Geometry"
                 "LightMode" = "ForwardBase"
             }
 
             CGPROGRAM
 
             #pragma vertex MyVertexProgram
             #pragma fragment MyFragmentProgram
 
             #include "UnityPBSLighting.cginc"
 
             float4 _Tint;
             float _BumpScale;
             sampler2D _MainTex;
             sampler2D _WrapTex;
             float _WrapShift;
             sampler2D _BumpMap;
             float4 _MainTex_ST;
 
             struct VertexData {
                 float4 position : POSITION;
                 float3 normal : NORMAL;
                 float2 uv : TEXCOORD0;
             };
 
             struct Interpolators {
                 float4 position : SV_POSITION;
                 float2 uv : TEXCOORD0;
                 float3 normal : TEXCOORD1;
             };
 
             Interpolators MyVertexProgram (VertexData v) {
                 Interpolators i;
                 i.position = mul(UNITY_MATRIX_MVP, v.position);
                 i.normal = UnityObjectToWorldNormal(v.normal);
                 i.uv = TRANSFORM_TEX(v.uv, _MainTex);
                 return i;
             }
 
             float3 InitializeFragmentNormal(inout Interpolators i) {
                 float3 texNormal;
                 texNormal.xy = tex2D(_BumpMap, i.uv).wy * 2 - 1;
                 texNormal.xy *= _BumpScale;
                 texNormal.z = sqrt(1 - saturate(dot(texNormal.xy, texNormal.xy)));
                 texNormal = UnpackScaleNormal(tex2D(_BumpMap, i.uv), _BumpScale);
                 texNormal = texNormal.xzy;
                 texNormal = normalize(texNormal);
                 return texNormal;
             }
 
             float4 MyFragmentProgram (Interpolators i) : SV_TARGET {
                 i.normal = i.normal + InitializeFragmentNormal(i);
                 float3 lightDir = _WorldSpaceLightPos0.xyz;
                 float3 lightColor = _LightColor0.rgb;
                 float3 albedo = tex2D(_MainTex, i.uv).rgb * _Tint.rgb;
                 float dotProd = dot(lightDir, i.normal);
                 dotProd = (dotProd + 1) / 2;
                     dotProd += _WrapShift;
                     dotProd = max(0.05, dotProd);
                     dotProd = min(0.95, dotProd);
                 float2 wrapPosition = float2(dotProd, 0);
                 float3 wrap = tex2D(_WrapTex, wrapPosition).rgb;
                 float3 diffuse =
                     albedo * lightColor * wrap;
                 
                 return float4(diffuse, 1);
             }
             ENDCG
         }
     }
     Fallback "Diffuse"
 }

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
3
Best Answer

Answer by markv12 · Feb 13, 2017 at 08:21 AM

It seems that as of Unity 5.5, when using a replacement shader you not only need a subshader with a matching RenderType, the material the shader is applied to also has to have a RenderType tag set to the proper value. To do this, go to a material in the inspector and change the inspector to "Debug" mode. Each material will have a "String Tag Map" list that allows you to specify a set of key value pairs for the material. Add a tag with the key "RenderType" and the value "Opaque" or whatever RebderType your shader is meant for. The material should now work properly with replacement shaders like the one used to render the depth-normals texture.

If you don't want to have to do this for every material that uses your shader, you can write a custom view for your shader that sets the RenderType automatically. If you look at the editor for the standard shader you'll notice at the top a "Rendering Mode" drop-down menu. This drop-down adds and removes tags from the String Tag Map for you and has a default value. Your custom view could work like that.

Comment
Add comment · Show 2 · 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
avatar image LocalsOnlyCoop · Apr 10, 2019 at 02:04 AM 2
Share

That was the fastest I've ever solved a problem in Unity. First post on the forums I came to had the right answer. Thank you so much!

avatar image DanielSchmidt · Mar 03 at 12:06 PM 1
Share

I've been working with Unity professionally for the better part of 10 years, and this is the first I learn about this whole thing.

I can't believe this wasn't like one of the first things that's mentioned when using certain post processing effects like SSAO.

Would never have figured this out, as we were stuck in our shader scratching our heads.

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

6 People are following this question.

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

Related Questions

How to achive the same look? Which shaders to use? 2 Answers

The Best Way To Make Stylised Grass in Unity? 0 Answers

Shader - What is float3.xy? 1 Answer

How do I keep an object lit when a light moves past it? 0 Answers

Implementing Custom Vertex Lighting Shader 1 Answer


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