Guest User

WorldPosEffectsSphere.shader

a guest
Feb 6th, 2023
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Toon/Lit WorldPos Effects Sphere" {
  2.     Properties {    
  3.         _MainTex ("Primary (RGB)", 2D) = "white" {}      
  4.         _SecondTex ("Secondary (RGB)", 2D) = "white" {}
  5.         [Header(Noise)]      
  6.         _NoiseTex("Noise", 2D) = "white"{}
  7.         _NScale ("Noise Scale", Range(0, 10)) = 1
  8.         _NoiseCutoff("Noise Radius Cutoff", Range(0.01, 1)) =0.01
  9.         _NoiseStrength("Noise Strength", Range(0, 1)) = 0
  10.         [Header(Line)]  
  11.         _LineWidth("Line Width", Range(0, 2)) = 0
  12.         [HDR]_LineColor("Line Color", Color) = (1,1,1,1)
  13.        
  14.         [KeywordEnum(SwapTextures, Appear, Disappear)]_STYLE("Style", Float) = 0
  15.     }
  16.    
  17.     SubShader{
  18.         Tags { "RenderType" = "Transparent" }
  19.         LOD 200            
  20.        
  21.         CGPROGRAM
  22.        
  23.         #pragma surface surf ToonRamp addshadow
  24.         #pragma shader_feature_local _STYLE_SWAPTEXTURES _STYLE_APPEAR _STYLE_DISAPPEAR
  25.        
  26.        
  27.         // custom lighting function that uses a texture ramp based
  28.         // on angle between light direction and normal
  29.         #pragma lighting ToonRamp exclude_path:prepass
  30.        
  31.        
  32.         inline half4 LightingToonRamp (SurfaceOutput s, half3 lightDir, half atten)
  33.         {
  34.             #ifndef USING_DIRECTIONAL_LIGHT
  35.                 lightDir = normalize(lightDir);
  36.             #endif
  37.            
  38.             float d = dot(s.Normal, lightDir) ;
  39.             float dChange = fwidth(d);
  40.             float3 lightIntensity = smoothstep(0 , 0 + 0.1, d);
  41.            
  42.             half4 c;
  43.             c.rgb = s.Albedo * _LightColor0.rgb * lightIntensity * (atten * 2);
  44.             c.a = 0;
  45.             return c;
  46.         }
  47.  
  48.         float3 _Position; // from script
  49.         float _Radius;// from script
  50.  
  51.         sampler2D _MainTex, _SecondTex;
  52.         sampler2D _NoiseTex;
  53.         float _NoiseCutoff, _NScale;
  54.         float _LineWidth;
  55.         float4 _LineColor;
  56.         float _NoiseStrength;
  57.        
  58.         struct Input {
  59.             float2 uv_MainTex : TEXCOORD0;
  60.             float3 worldPos;// built in value to use the world space position
  61.             float3 worldNormal; // built in value for world normal
  62.            
  63.         };
  64.        
  65.         void surf (Input IN, inout SurfaceOutput o) {
  66.             // sphere
  67.             float3 dis =  distance(_Position.xyz, IN.worldPos);
  68.             float sphereR = 1- saturate(dis / _Radius).r;
  69.            
  70.             // triplanar noise
  71.             float3 blendNormal = saturate(pow(IN.worldNormal * 1.4,4));
  72.             half4 nSide1 = tex2D(_NoiseTex, (IN.worldPos.xy + _Time.x) * _NScale);
  73.             half4 nSide2 = tex2D(_NoiseTex, (IN.worldPos.xz + _Time.x) * _NScale);
  74.             half4 nTop = tex2D(_NoiseTex, (IN.worldPos.yz + _Time.x) * _NScale);
  75.  
  76.             float3 noisetexture = nSide1;
  77.             noisetexture = lerp(noisetexture, nTop, blendNormal.x);
  78.             noisetexture = lerp(noisetexture, nSide2, blendNormal.y);
  79.            
  80.             float3 sphereNoise = lerp(noisetexture.r * sphereR , sphereR, _NoiseStrength);
  81.  
  82.             // cutoff
  83.             float radiusCutoff = step(_NoiseCutoff,sphereNoise);
  84.  
  85.             // glowing line
  86.             float Line = step(sphereNoise - _LineWidth, _NoiseCutoff) * radiusCutoff ; // line between two textures
  87.             float3 colouredLine= Line * _LineColor; // color the line
  88.            
  89.             // textures
  90.             half4 c = tex2D(_MainTex, IN.uv_MainTex);
  91.             half4 c2 = tex2D(_SecondTex, IN.uv_MainTex);
  92.             float3 combinedTex = lerp(c, c2, radiusCutoff);
  93.            
  94.             // final
  95.             float3 resultTex = combinedTex + colouredLine;
  96.             o.Albedo = resultTex;
  97.  
  98.             o.Emission = colouredLine;
  99.  
  100.             // alpha clipping
  101.             #if defined(_STYLE_APPEAR)
  102.                 clip(radiusCutoff - 0.01);
  103.             #elif defined(_STYLE_DISAPPEAR)
  104.                 clip(1-(radiusCutoff - Line) - 0.01);              
  105.             #endif
  106.             o.Alpha = c.a;
  107.            
  108.         }
  109.         ENDCG
  110.        
  111.     }
  112.    
  113.     Fallback "Diffuse"
  114. }
Advertisement
Add Comment
Please, Sign In to add comment