- Home /
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!!
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!
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.
This was fixed in a patch to 5.0 and verified functioning in 5.2.x
Your answer
Follow this Question
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