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 hubi037 · Dec 06, 2015 at 02:53 PM · unity 5shadersurface shader

Multipass Surface Shader with _CameraDepthNormalsTexture

Hello,

I'm trying to write a shader that does some depth testing in the first pass, outputs a calculated value into the color and then does a grab pass to fetch it. In a third pass a second surface shader should render a plane with the values from the grabbed Texture.

Somehow the second surface shader only outputs the color red, disregarding whatever I calculated in the first pass.

However if I use

Pass { SetTexture [_GrabTexture] }

instead of the second surface shader it outputs the correct color. In essence I'm trying to get both versions of the shader to do the same thing for now. I'm not sure what I did wrong with my third pass, but maybe one of you knows. Searching this site for answers didn't bring up anything that solved this issue. Any help is much appreciated.

EDIT: After some more debugging I found out that I can receive the right texture in the second surface shader pass if I set the render Queue to Transparent. However, this prevents me from receiving shadows and is therefore not a good solution. I also don't quite understand how the Queue setting affects this since the shader doesn't use transparency at all.

Two pictures that show the different outcomes (first picture is the desired outcome):

alt text alt text

Here the complete shader:

 Shader "Custom/WaterShader" 
 {
     Properties 
     {
         _Color ("Color", Color) = (1,1,1,1)
         _MainTex ("Albedo (RGB)", 2D) = "white" {}
         _Glossiness ("Smoothness", Range(0,1)) = 0.5
         _Metallic ("Metallic", Range(0,1)) = 0.0
         _InvFade ("Fade Factor", Range(0,10.0)) = 1.0
         _Threshold ("_Threshold", Range(0,10.0)) = 1.0
     }
     SubShader 
     {
         Tags { "RenderType"="Opaque" "Queue"="Geometry+20"}
 
         ZWrite Off
         Cull Off
 
         CGPROGRAM
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard vertex:surfVert
 
         struct Input 
         {
             float4 vertex;
             float4 projPos;
         };
 
         void surfVert (inout appdata_full v, out Input o)
         {
             UNITY_INITIALIZE_OUTPUT(Input,o);
 
             o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
             o.projPos = ComputeScreenPos (o.vertex);
             COMPUTE_EYEDEPTH(o.projPos.z);
         }
         
         sampler2D _CameraDepthTexture;
         float _InvFade;
         void surf (Input IN, inout SurfaceOutputStandard o) 
         {
             float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(IN.projPos)));
 
             float partZ = IN.projPos.z;
             float fade = 0;
             fade = saturate (_InvFade * (sceneZ-partZ));
                   
             o.Albedo = float3(fade,fade,fade);
             o.Alpha = 1;
         }
         ENDCG
         
         GrabPass 
         {
             "_GrabTexture"
             Tags { "LightMode" = "Always" } // no lighting
         }        
 
         
         Pass
         {
             SetTexture [_GrabTexture]
         }
         
         ZWrite On
         CGPROGRAM
 
         // Physically based Standard lighting model, and enable shadows on all light types
         #pragma surface surf Standard fullforwardshadows vertex:surfVert
 
         // Use shader model 3.0 target, to get nicer looking lighting
         #pragma target 3.0
 
         sampler2D _MainTex;
         sampler2D _GrabTexture;
         half _Glossiness;
         half _Metallic;
         fixed4 _Color;
 
         struct Input {
             float4 grabUV;
             };
 
         void surfVert (inout appdata_full v, out Input o)
         {
             UNITY_INITIALIZE_OUTPUT(Input,o);
             float4 hpos = mul (UNITY_MATRIX_MVP, v.vertex);
                     o.grabUV = ComputeGrabScreenPos(hpos);
         }
 
         void surf (Input IN, inout SurfaceOutputStandard o) 
         {
             float4 grabVector =  float4(tex2Dproj(_GrabTexture, UNITY_PROJ_COORD(IN.grabUV)));
 
             o.Albedo = grabVector.rgb;
             o.Alpha = 1;
         }
         ENDCG           
     } 
     Fallback "VertexLit"
 }
 


thirdpasssurfshader.png (68.9 kB)
settexturepass.png (111.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

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

Control cubemap reflection amount from metallic and smoothness properties 0 Answers

Unlit terrain shader 0 Answers

Splat Map shader no longer working in Unity 5 1 Answer

Simple cartoon water shader shoreline 1 Answer

Transparent shadow in surface 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