Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by supastrikerz619 · Dec 07, 2020 at 08:32 PM · shadershader programming

I need help with my NPR shader. URP

alt textalt text

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
             }
 
         }
 
 }


helpme.jpg (44.4 kB)
capture.jpg (148.5 kB)
Comment
Add comment · Show 1
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 supastrikerz619 · Dec 07, 2020 at 11:11 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

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

214 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 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 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 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 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 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 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 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 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 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 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

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


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