- Home /
Can someone explain what I'm doing wrong in these shaders?
So, I'm trying to implement a toon shader technique using the sobel filter to get the edges on the final images. For that I investigated the support of deferred shading on unity. As far as I can tell I can replace the internal deferred shader in by going into "project settings -> Graphics", select custom deferred shader and then use my own deferred shader. The shader I wrote is based on the file "Internal-DeferredShading.shader" of Unity 5's builtin shaders. Ok in this shader I have access to all the expected information, so I guess no problem here. But to implement the techniqu would be desirable I think that the first shader pass could be replaced as well, so I wrote a shader based on the stardard one for the deferred lightMode. Here's the catch, everything is white now, and I can't figure out what I'm doing wrong. I think the problem is in the first shader as it uses MRT, I guess I'm not writing the information in the correct way. But I have no idea of what is wrong. I'm going to post the shaders here so you guys can check it out. Thanks in advance.
Intenal-deferredShading based shader
Shader "Rinha/Cel-DeferredShading" {
Properties {
_LightTexture0 ("", any) = "" {}
_LightTextureB0 ("", 2D) = "" {}
_ShadowMapTexture ("", any) = "" {}
_SrcBlend ("", Float) = 1
_DstBlend ("", Float) = 1
}
SubShader {
// Pass 1: Lighting pass
// LDR case - Lighting encoded into a subtractive ARGB8 buffer
// HDR case - Lighting additively blended into floating point buffer
Pass {
ZWrite Off
Blend [_SrcBlend] [_DstBlend]
CGPROGRAM
#pragma target 3.0
#pragma vertex vert_deferred
#pragma fragment frag
#pragma multi_compile_lightpass
#pragma multi_compile ___ UNITY_HDR_ON
#pragma exclude_renderers nomrt
#include "UnityCG.cginc"
#include "UnityDeferredLibrary.cginc"
#include "UnityPBSLighting.cginc"
#include "UnityStandardUtils.cginc"
#include "UnityStandardBRDF.cginc"
sampler2D _CameraGBufferTexture0;
sampler2D _CameraGBufferTexture1;
sampler2D _CameraGBufferTexture2;
half4 CalculateLight (unity_v2f_deferred i)
{
float3 wpos;
float2 uv;
float atten, fadeDist;
UnityLight light;
UNITY_INITIALIZE_OUTPUT(UnityLight, light);
UnityDeferredCalculateLightParams (i, wpos, uv, light.dir, atten, fadeDist);
half4 gbuffer0 = tex2D (_CameraGBufferTexture0, uv);
half4 gbuffer1 = tex2D (_CameraGBufferTexture1, uv);
half4 gbuffer2 = tex2D (_CameraGBufferTexture2, uv);
light.color = _LightColor.rgb * atten;
half3 baseColor = gbuffer0.rgb;
half3 specColor = gbuffer1.rgb;
half oneMinusRoughness = gbuffer1.a;
half3 normalWorld = gbuffer2.rgb * 2 - 1;
normalWorld = normalize(normalWorld);
float3 eyeVec = normalize(wpos-_WorldSpaceCameraPos);
half oneMinusReflectivity = 1 - SpecularStrength(specColor.rgb);
light.ndotl = LambertTerm (normalWorld, light.dir);
//UnityIndirect ind;
//UNITY_INITIALIZE_OUTPUT(UnityIndirect, ind);
//ind.diffuse = 0;
//ind.specular = 0;
half4 res = half4(baseColor, 1.0);
return res;
}
#ifdef UNITY_HDR_ON
half4
#else
fixed4
#endif
frag (unity_v2f_deferred i) : SV_Target
{
half4 c = CalculateLight(i);
#ifdef UNITY_HDR_ON
return c;
#else
return exp2(-c);
#endif
}
ENDCG
}
// Pass 2: Final decode pass.
// Used only with HDR off, to decode the logarithmic buffer into the main RT
Pass {
ZTest Always Cull Off ZWrite Off
Stencil {
ref [_StencilNonBackground]
readmask [_StencilNonBackground]
// Normally just comp would be sufficient, but there's a bug and only front face stencil state is set (case 583207)
compback equal
compfront equal
}
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma exclude_renderers nomrt
sampler2D _LightBuffer;
struct v2f {
float4 vertex : SV_POSITION;
float2 texcoord : TEXCOORD0;
};
v2f vert (float4 vertex : POSITION, float2 texcoord : TEXCOORD0)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, vertex);
o.texcoord = texcoord.xy;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return -log2(tex2D(_LightBuffer, i.texcoord));
}
ENDCG
}
}
Fallback Off
}
Shader based on the Standard one
Shader "Rinha/Cel-DiffusePass" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Weight ("Color force", Range(0.0, 1.0)) = 0.5
}
SubShader {
Tags { "RenderType" = "Opaque" }
Pass {
Tags {"LightMode" = "Deferred"}
CGPROGRAM
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
uniform sampler2D _MainTex;
uniform float4 _Color;
uniform float _Weight;
struct VertexInput {
float2 uv: TEXCOORD0;
float4 pos: POSITION0;
float3 normal: NORMAL0;
};
struct PixelInput {
float4 pos: POSITION0;
float2 uv: TEXCOORD0;
float3 normal: NORMAL0;
};
PixelInput vert(VertexInput vi)
{
PixelInput pi;
pi.pos = mul(UNITY_MATRIX_MVP, vi.pos);
pi.uv = vi.uv;
pi.normal = vi.normal;
return pi;
}
void frag(PixelInput pi,
out half4 diffuse: SV_Target0,
out half4 spec: SV_Target1,
out half4 normal: SV_Target2,
out half4 emission: SV_Target3)
{
diffuse = half4(0.0, 0.0, 0.0, 0.0);
//diffuse = lerp(_Color, tex2D(_MainTex, pi.uv), _Weight);
#ifndef UNITY_HDR_ON
diffuse.rgb = exp2(-diffuse.rgb);
#endif
normal = half4(pi.normal*0.5+0.5, 1.0);
spec = half4(1.0, 1.0, 1.0, 1.0);
emission = half4(0.0, 0.0, 0.0, 1.0);
}
ENDCG
}
}
FallBack "Off"
}
Your answer
Follow this Question
Related Questions
exclude_path:deferred causes see through? 0 Answers
toonLighted shader to work with deferred rendered shadows 2 Answers
Shader not working properly in Deferred render mode 0 Answers
Reading from Gbuffers 4-7 in a shader 1 Answer
Unity Surface Shader - Deferred lighting viewDir and lightDir 0 Answers