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 Taorcb · May 11, 2016 at 03:39 AM · renderingshadersshader programmingdeferred renderingdecals

[Deferred Decals] Add simple specular channel to deffered decals

I'm trying to use Unity's deferred decals (the ones that Unity announced with the release of Unity 5). I want to add a simple specular channel to the shader. With surface shaders, that's fairly easy to do, but the decal shader is more complicated and I don't fully understand it. Here's what I've got (not much):

 Shader "Decal/DecalShader"
 {
     Properties
     {
         _MainTex ("Diffuse", 2D) = "white" {}
     }
     SubShader
     {
         Pass
         {
             Fog { Mode Off } // no fog in g-buffers pass
             ZWrite Off
             Blend SrcAlpha OneMinusSrcAlpha
 
             CGPROGRAM
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             #pragma exclude_renderers nomrt
             
             #include "UnityCG.cginc"
 
             struct v2f
             {
                 float4 pos : SV_POSITION;
                 half2 uv : TEXCOORD0;
                 float4 screenUV : TEXCOORD1;
                 float3 ray : TEXCOORD2;
                 half3 orientation : TEXCOORD3;
             };
 
             v2f vert (float3 v : POSITION)
             {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, float4(v,1));
                 o.uv = v.xz+0.5;
                 o.screenUV = ComputeScreenPos (o.pos);
                 o.ray = mul (UNITY_MATRIX_MV, float4(v,1)).xyz * float3(-1,-1,1);
                 o.orientation = mul ((float3x3)_Object2World, float3(0,1,0));
                 return o;
             }
 
             CBUFFER_START(UnityPerCamera2)
             float4x4 _CameraToWorld;
             CBUFFER_END
 
             sampler2D _MainTex;
             sampler2D_float _CameraDepthTexture;
             sampler2D _NormalsCopy;
     
             fixed4 frag(v2f i) : SV_Target
             {
                 i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
                 float2 uv = i.screenUV.xy / i.screenUV.w;
                 // read depth and reconstruct world position
                 float depth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, uv);
                 depth = Linear01Depth (depth);
                 float4 vpos = float4(i.ray * depth,1);
                 float3 wpos = mul (_CameraToWorld, vpos).xyz;
                 float3 opos = mul (_World2Object, float4(wpos,1)).xyz;
 
                 clip (float3(0.5,0.5,0.5) - abs(opos.xyz));
 
 
                 i.uv = opos.xz+0.5;
 
                 half3 normal = tex2D(_NormalsCopy, uv).rgb;
                 fixed3 wnormal = normal.rgb * 2.0 - 1.0;
                 clip (dot(wnormal, i.orientation) - 0.3);
 
                 fixed4 col = tex2D (_MainTex, i.uv);
                 return col;
             }
             ENDCG
         }        
 
     }
 
     Fallback Off
 }
 
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

44 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

Related Questions

Problems with Depth Normals and variable CullMode 0 Answers

Is it possible to avoid writing to certain g-buffers? 1 Answer

Rendering volumetric objects and maintaining correct depth 1 Answer

Scene Color Node in Shader Graph not working with Unity's 2D Renderer and URP 5 Answers

Converting c# function to 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