- Home /
I need help with my NPR shader. URP
It can receive and cast shadows, However it cant render it properly as seen in the picture, the lighting becomes pixelated and wavy like this. I am using URP for my project. Im still quite new to all this and i need help fast.
Shader " PracticeShadersByHaron/Final_ToonShadeWithUnityChanOutlineRecShadows" {
Properties{
[Header(BaseColour)]
_MainTex("Main Texture", 2D) = "white" {}
_color("Colour" , color) = (0.5,0.5,0.5,1.0)
[Header(EmissionMap)]
_EmissionMap("Emission Map", 2D) = "black" {}
[HDR]
_EmissionColor("Emission Color", Color) = (0,0,0)
[Header(LightRamp)]
_RampTex("Ramp", 2D) = "white"{}
[Header(Celshading)]
_feathering("Feathering", Range(0,1)) = 0.1
[Header(Specularity)]
_specFeathering("Specular Feathering", Range(0.01,5)) = 0.1
[HDR]
_SpecularColor("Specular Color", Color) = (0.9,0.9,0.9,1)
_Glossiness("Glossiness", Float) = 15
[Header(AmbientColour)]
[HDR]
_AmbientColor("Ambient Color", Color) = (0.4,0.4,0.4,1)
[Header(RimLighting)]
_RimColor("Rim Colour", color) = (1.0,1.0,1.0,1.0)
_RimPower("RimPower", range(0.1 , 10.0)) = 0.7
_RimThreshold("Rim Threshold", Range(0, 1)) = 0.23
[Header(Shadow mapping)]
_ReceiveShadowMappingAmount("_ReceiveShadowMappingAmount", Range(0,1)) = 0.5
//borrowing
[Header(Outline)]
//Outline
[KeywordEnum(NML,POS)] _OUTLINE("OUTLINE MODE", Float) = 0
_Outline_Width("Outline_Width", Float) = 0
_Farthest_Distance("Farthest_Distance", Float) = 100
_Nearest_Distance("Nearest_Distance", Float) = 0.5
_Outline_Sampler("Outline_Sampler", 2D) = "white" {}
_Outline_Color("Outline_Color", Color) = (0.5,0.5,0.5,1)
[Toggle(_)] _Is_BlendBaseColor("Is_BlendBaseColor", Float) = 0
[Toggle(_)] _Is_LightColor_Outline("Is_LightColor_Outline", Float) = 1
//v.2.0.4
[Toggle(_)] _Is_OutlineTex("Is_OutlineTex", Float) = 0
_OutlineTex("OutlineTex", 2D) = "white" {}
//Offset parameter
_Offset_Z("Offset_Camera_Z", Float) = 0
//v.2.0.4.3 Baked Nrmal Texture for Outline
[Toggle(_)] _Is_BakedNormal("Is_BakedNormal", Float) = 0
_BakedNormal("Baked Normal for Outline", 2D) = "white" {}
}
Subshader{
Pass {
Name "SurfaceColor"
Tags
{
// "Lightmode" tag must be "UniversalForward" or not be defined, in order to render objects in URP.
"LightMode" = "UniversalForward"
}
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// Universal Render Pipeline keywords
// When doing custom shaders you most often want to copy and paste these #pragmas
// These multi_compile variants are stripped from the build depending on:
// 1) Settings in the URP Asset assigned in the GraphicsSettings at build time
// e.g If you disabled AdditionalLights in the asset then all _ADDITIONA_LIGHTS variants
// will be stripped from build
// 2) Invalid combinations are stripped. e.g variants with _MAIN_LIGHT_SHADOWS_CASCADE
// but not _MAIN_LIGHT_SHADOWS are invalid and therefore stripped.
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS
#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
#pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
#pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
#pragma multi_compile _ _SHADOWS_SOFT
//Handle 2 lighting situations, when main directional light does not cast shadows and does. UNITY will handle these
//confirgurations by compiled variants of this shader. Use this to compile our variants
//#pragma multi_compile_fwdbase
#pragma multi_compile_instancing
//Always include this package for cg
//#include "UnityCG.cginc"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
// lighting packages
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl"
CBUFFER_START(UnityPerMaterial)
//user variables
//uniform sampler2D _MainTex;
sampler2D _EmissionMap;
sampler2D _MainTex;
uniform float4 _color;
uniform float4 _MainTex_ST;
uniform float _feathering;
uniform float _specFeathering;
uniform float4 _AmbientColor;
uniform float _Glossiness;
uniform float4 _SpecularColor;
//uniform float _feathering;
//rim light
uniform float4 _RimColor;
uniform float _RimPower;
uniform float _RimThreshold;
//emission
float4 _EmissionColor;
//RampTexture
uniform sampler2D _RampTex;
// shadow mapping
half _ReceiveShadowMappingAmount;
CBUFFER_END
//base input structs
struct Attributes {
float3 normal : NORMAL;
//float4 tangentOS : TANGENT;
float4 vertex : POSITION;
float4 texCoord : TEXCOORD0;
};
struct Varyings {
float3 worldNormal : NORMAL;
float4 pos : SV_POSITION;
float3 viewDir : TEXCOORD1;
float2 tex : TEXCOORD0;
//SHADOW_COORDS(2)
float4 shadowCoord : TEXCOORD6;
//half3 tangentWS : TEXCOORD4;
};
inline float3 WorldSpaceViewDir(in float4 v)
{
return _WorldSpaceCameraPos.xyz - mul(unity_ObjectToWorld, v).xyz;
}
inline float3 UnityObjectToWorldNormal(in float3 norm)
{
#ifdef UNITY_ASSUME_UNIFORM_SCALING
return UnityObjectToWorldDir(norm);
#else
// mul(IT_M, norm) => mul(norm, I_M) => {dot(norm, I_M.col0), dot(norm, I_M.col1), dot(norm, I_M.col2)}
return normalize(mul(norm, (float3x3)unity_WorldToObject));
#endif
}
//vertex function
Varyings vert(Attributes v) {
Varyings o;
VertexPositionInputs vertexInput = GetVertexPositionInputs(v.vertex.xyz);
o.worldNormal = UnityObjectToWorldNormal(v.normal);
//o.worldNormal = TransformObjectToWorldNormal(v.normal);
o.pos = TransformObjectToHClip(v.vertex);
o.viewDir = WorldSpaceViewDir(v.vertex);
o.tex = v.texCoord;
//TRANSFER_SHADOW(o)
o.shadowCoord = GetShadowCoord(vertexInput);
return o;
}
//frag function
float4 frag(Varyings i) : COLOR{
float3 viewDirection = normalize(i.viewDir);
float3 normal = normalize(i.worldNormal);
// Diffuselighting AKA ur Lambert shading
//float NdotL = dot(_WorldSpaceLightPos0, normal);
//Shadows
//float shadow = SHADOW_ATTENUATION(i);
#ifdef _MAIN_LIGHT_SHADOWS
// Main light is the brightest directional light.
// It is shaded outside the light loop and it has a specific set of variables and shading path
// so we can be as fast as possible in the case when there's only a single directional light
// You can pass optionally a shadowCoord (computed per-vertex). If so, shadowAttenuation will be
// computed.
Light mainLight = GetMainLight(i.shadowCoord);
#else
Light mainLight = GetMainLight();
#endif
//Smoothstep to create the celshading technique and _feathering for how soft the celshade is but in this shader
//im using a ramptexture for shadows
float3 lightDirection;
float atten = 1.0;
//lighting
lightDirection = normalize(_MainLightPosition.xyz);
float ndotl = dot(normal, lightDirection) * 0.5 + 0.5;
float lightIntensity = smoothstep( 0.005 , _feathering, ndotl );
//float shadow = mainLight.shadowAttenuation
float4 diffuseReflection;
diffuseReflection.xyz = tex2D(_RampTex, ndotl.xx).rgb * _MainLightColor.rgb * _color.rgb * mainLight.shadowAttenuation;
//diffuseReflection.xyz = lightIntensity * _MainLightColor.rgb * _color.rgb;
diffuseReflection.w = 1.0;
//specularity
float3 halfVector = normalize(_MainLightPosition + viewDirection);
float NdotH = dot(normal, halfVector);
float specularIntensity = pow(NdotH * lightIntensity, _Glossiness * _Glossiness);
float specularIntensitySmooth = smoothstep(0.05, _specFeathering, specularIntensity);
float4 specular = specularIntensitySmooth * _SpecularColor;
//Rim Lighting
float4 rimDot = 1 - dot(viewDirection, normal);
float rimIntensity = rimDot * pow(ndotl, _RimThreshold);
rimIntensity = smoothstep(_RimPower - 0.01, _RimPower + 0.01, rimIntensity);
float4 rim = rimIntensity * _RimColor;
// light final
float4 light = diffuseReflection + _MainLightColor + specular + rim;
//emission
half4 emission = tex2D(_EmissionMap, i.tex) * _EmissionColor;
light += emission;
// texture
float4 tex = tex2D(_MainTex, _MainTex_ST * i.tex.xy + _MainTex_ST.zw);
return tex * _color * (light + _AmbientColor);
}
ENDHLSL
}
UsePass "Legacy Shaders/VertexLit/SHADOWCASTER" // well its purpose is alr in the name lol
//UsePass " Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl "
Pass{
Name "Outline"
Tags
{
//"LightMode" = "UniversalForward" // IMPORTANT: don't write this line for any custom +pass! else this outline pass will not be rendered in URP!
}
Cull Front
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile _IS_OUTLINE_CLIPPING_NO
#pragma multi_compile _OUTLINE_NML _OUTLINE_POS
// user defined variables
//uniform float _OutlineWidth;
//uniform float4 _OutlineColor;
#pragma target 3.0
#include "UCTS_Outline.cginc"
ENDCG
}
}
}
Im quite new to coding shaders in general, but i think i have a good idea of how it works and how to code shaders in general. $$anonymous$$y issue here is i have no idea why my shader turned out the way it did. When i add in mainLight.shadowAttenuation, the shadows become pixelated and wavy. i have already edited the shadow cascades
Your answer

Follow this Question
Related Questions
Unity Advanced Shader Help 0 Answers
Find position of pixel that's currently being calculated in shadergraph custom function 0 Answers
Only render stencil mask when behind maskable object 0 Answers
Silhouette overlay shader 0 Answers
Shader forge & Spine 1 Answer