Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 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 GeraldOBBI · Jun 01, 2015 at 06:01 PM · shaderrenderingalphadecaldeferred

COLORMASK & writing to multiple render targets In Deferred doesn't work as expected in DX9

Hello Unity Community!

I'm running into an issue building off of the Unity 5 deferred decals example (http://blogs.unity3d.com/2015/02/06/extending-unity-5-rendering-pipeline-command-buffers/). What I'm looking to do, is to add specularity modulation to our decals by writing out to the spec/roughness render target as well as the albedo channel. We are using command buffers in the same way, with the spec/roughness render target bound to our output in the command buffer.

The desired behaviour for this shader is that the decals are rendered with alpha blending in both the diffuse and spec/roughness RTs however, I only want to attenuate/blend the specular component and not the roughness of the spec/roughness RT (i.e. the alpha I want to write controls the blend of RGB without affecting the output).

In Unity 4, I was able to use COLORMASK to only write RGB out when alpha blending in the pre-pass base or pre-pass final so I'm not sure why when using MRT shaders, this fails to happen. Take a look at the GIF I've prepared below. I would hate to have to perform multiple passes!!

alt text

On the left of the GIF, you can see the output of the specular buffer, and the smooothness on the right. As you can see, the smoothness value inherits the alpha I'm using to blend the specular value. Here's the shader, it's not far from the example:

 // http://www.popekim.com/2012/10/siggraph-2012-screen-space-decals-in.html
 
 Shader "Decals-Alpha-Diffuse"
 {
     Properties
     {
         _Color ("Main Color (RGB)", Color) = (1,1,1,1)
         _MainTex ("Diffuse", 2D) = "white" {}
     }
     SubShader
     {
         Pass
         {
             Fog { Mode Off } // no fog in g-buffers pass
             ZWrite Off
             ZTest GEqual
             Cull Front
             ColorMask RGB
             Blend SrcAlpha OneMinusSrcAlpha
 
             CGPROGRAM
             #pragma target 3.0
             #pragma vertex vert
             #pragma fragment frag
             
             #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;
             float4 _Color;
 
             //void frag(
             //    v2f i,
             //    out half4 outDiffuse : COLOR0,            // RT0: diffuse color (rgb), --unused-- (a)
             //    out half4 outSpecRoughness : COLOR1,    // RT1: spec color (rgb), roughness (a)
             //)
             void frag(v2f i, out half4 outDiffuse : COLOR0, out half4 outSpecRoughness : COLOR1)
             {
                 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));
 
                 outDiffuse = tex2D (_MainTex, i.uv) * _Color;
                 outSpecRoughness = half4(0, 0, 0, outDiffuse.a);
             }
             ENDCG
         }        
 
     }
 
     Fallback Off
 }
 

Does anyone have any insight as to why color mask wouldn't work in this case? Thanks!

spec-roughness-test.gif (495.7 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
Best Answer

Answer by GeraldOBBI · Jun 01, 2015 at 09:57 PM

NOTE that after a little bit of analysis, this functions correctly in DX11 mode however, we are using DX9 where it doesn't function as expected.

Comment
Add comment · Show 1 · 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 GeraldOBBI · Nov 03, 2015 at 06:21 PM 0
Share

This was fixed in a patch to 5.0 and verified functioning in 5.2.x

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

20 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

Related Questions

a shader or decal that only renders the normal map 0 Answers

Model Head not Rendering correctly. 0 Answers

custom shader error. copy of "Nature/Tree Soft Occlusion Leaves" 0 Answers

Shader alpha setting being ignored? 1 Answer

How to: vertex colored mesh shader that doesn't break Sprite Manager. 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