Hello.
I’m new into shaders and I’m trying to achieve this view following a guide. The thing is the result is a black screen, and no errors displaying. My scene is composed of unity cubes so they have meshes. Can anyone help me? The previous step in the guide give good results, but get the said black screen when trying to show normals.
Shader "PostProcessing/SobelOutline"
{
HLSLINCLUDE
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
TEXTURE2D_SAMPLER2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2);
float _OutlineThickness;
float _OutlineDepthMultiplier;
float _OutlineDepthBias;
float _OutlineNormalMultiplier;
float _OutlineNormalBias;
float4 _OutlineColor;
float SobelDepth(float ldc, float ldl, float ldr, float ldu, float ldd) {
return abs(ldl - ldc) +
abs(ldr - ldc) +
abs(ldu - ldc) +
abs(ldd - ldc);
}
float SobelSampleDepth(Texture2D t, SamplerState s, float2 uv, float3 offset) {
float pixelCenter = LinearEyeDepth(t.Sample(s, uv).r);
float pixelLeft = LinearEyeDepth(t.Sample(s, uv - offset.xz).r);
float pixelRight = LinearEyeDepth(t.Sample(s, uv + offset.xz).r);
float pixelUp = LinearEyeDepth(t.Sample(s, uv + offset.zy).r);
float pixelDown = LinearEyeDepth(t.Sample(s, uv - offset.zy).r);
return SobelDepth(pixelCenter, pixelLeft, pixelRight, pixelUp, pixelDown);
}
float4 FragMain(VaryingsDefault i) : SV_Target {
// Sample the scene and our depth buffer
float3 offset = float3((1.0 / _ScreenParams.x), (1.0 / _ScreenParams.y), 0.0) * _OutlineThickness;
float3 sceneColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).rgb;
float sobelDepth = SobelSampleDepth(_CameraDepthTexture, sampler_CameraDepthTexture, i.texcoord.xy, offset);
// Modulate the outline color based on it's transparency
float3 outlineColor = lerp(sceneColor, _OutlineColor.rgb, _OutlineColor.a);
// Calculate the final scene color
float3 color = lerp(sceneColor, outlineColor, sobelDepth);
return float4(color, 1.0);
}
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragMain
ENDHLSL
}
}
}
I added normal maps to the object because I think they will need it. Still black screen with no results when using the shader.
The shader you posted does absolutely nothing with normals, so I’m not sure what you mean with “but get the said black screen when trying to show normals.”
1 Like
Thanks for the response! Sorry, seems I posted the wrong code. With this one, the result should be something like the first images in the first post.
Shader "PostProcessing/NormalOutline"
{
HLSLINCLUDE
#include "Packages/com.unity.postprocessing/PostProcessing/Shaders/StdLib.hlsl"
TEXTURE2D_SAMPLER2D(_MainTex, sampler_MainTex);
TEXTURE2D_SAMPLER2D(_CameraDepthTexture, sampler_CameraDepthTexture);
TEXTURE2D_SAMPLER2D(_CameraGBufferTexture2, sampler_CameraGBufferTexture2);
float _OutlineThickness;
float _OutlineDepthMultiplier;
float _OutlineDepthBias;
float _OutlineNormalMultiplier;
float _OutlineNormalBias;
float4 _OutlineColor;
float4 SobelSample(Texture2D t, SamplerState s, float2 uv, float3 offset) {
float4 pixelCenter = t.Sample(s, uv);
float4 pixelLeft = t.Sample(s, uv - offset.xz);
float4 pixelRight = t.Sample(s, uv + offset.xz);
float4 pixelUp = t.Sample(s, uv + offset.zy);
float4 pixelDown = t.Sample(s, uv - offset.zy);
return abs(pixelLeft - pixelCenter) +
abs(pixelRight - pixelCenter) +
abs(pixelUp - pixelCenter) +
abs(pixelDown - pixelCenter);
}
float4 FragMain(VaryingsDefault i) : SV_Target {
float3 offset = float3((1.0 / _ScreenParams.x), (1.0 / _ScreenParams.y), 0.0) * _OutlineThickness;
float3 sceneColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.texcoord).rgb;
float3 sobelNormalVec = SobelSample(_CameraGBufferTexture2, sampler_CameraGBufferTexture2, i.texcoord.xy, offset).rgb;
return float4(sobelNormalVec, 1.0);
}
ENDHLSL
SubShader
{
Cull Off ZWrite Off ZTest Always
Pass
{
HLSLPROGRAM
#pragma vertex VertDefault
#pragma fragment FragMain
ENDHLSL
}
}
}
What render pipeline are you using? I think only deferred Built-in used _CameraGBufferTexture2.
1 Like
That I have no idea, how do I check? Created a core 3D, I think thats built-in
If you’re using built-in then make sure you are using deferred shading. Look in Quality settings (and graphics?) and on your camera.
1 Like
Thank you so much Acid! OMG you are my saviour, was the camera thing. How do I learn this shader, normals, render stuff anyway, I see a lot of tutorials but im looking more of a deep course or documentation. Thanks again <3
1 Like
tsunamigue:
How do I learn this shader, normals, render stuff anyway, I see a lot of tutorials but im looking more of a deep course or documentation.
I think @bgolus said it best
https://x.com/bgolus/status/1697332952177529341?s=46&t=Bv3_c-pn9Z25o1EVJSlQPg
1 Like
Then I guess I’m doing well.
But more seriously, like you said before, you notice that there wasn’t anithing with normals in the first script. How do I learn to read that and understand shaders outputs and stuff?
Experience? I really don’t know what to tell you. Looking at the source code for a bunch of shaders, tweaking stuff and seeing what happens? The old vert frag stuff is probably better for learning because there is less “magic” in them and things are a bit more straightforward.
1 Like
Thanks, I’ll look for some s h a d e r c o d e