123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- Shader "Custom/ParallaxCloud"
- {
- Properties
- {
- _MainTex ("BaseMap:基础贴图", 2D) = "white" {}
- _Color ("Color:颜色", Color) = (0.0, 0.0, 0.0, 1.0)
- _ParallaxSize ("ParallaxSize:视差映射强度", Range(0.0, 0.1)) = 0.0
- _ParallaxLayers ("ParallaxLayers:视差映射层数", Range(1.0, 50.0)) = 20
- _MoveSpeed ("MoveSpeed:移动速度", Range(-0.5, 0.5)) = 0.1
- _Alpha ("Alpha:透明度", Range(0, 1)) = 0.5
- _AlphaExtent ("AlphaExtent:透明对比度", Range(0.0, 5.0)) = 5.0
- }
-
- SubShader
- {
- Tags
- {
- "RenderPipeline"="UniversalPipeline"
- "Queue"="Transparent"
- "RenderType"="Transparent"
- }
- Pass
- {
- Blend SrcAlpha OneMinusSrcAlpha
- Cull off
-
- HLSLPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
-
- struct Attributes
- {
- float4 positionOS : POSITION;
- float3 normalOS : NORMAL;
- float2 uv : TEXCOORD0;
- float4 tangent : TANGENT;
- };
- struct Varyings
- {
- float4 positionCS : SV_POSITION;
- float3 positionWS : TEXCOORD0;
- float4 uv : TEXCOORD1;
- float3 nDirWS : TEXCOORD2;
- float3 tDirWS : TEXCOORD3;
- float3 bDirWS : TEXCOORD4;
- };
- CBUFFER_START(UnityPerMaterial)
- half4 _MainTex_ST;
- half4 _Color;
- half _ParallaxSize;
- half _ParallaxLayers;
- half _MoveSpeed;
- half _Alpha;
- half _AlphaExtent;
- CBUFFER_END
- TEXTURE2D(_MainTex); SAMPLER(sampler_MainTex);
- TEXTURE2D(_NormalTex); SAMPLER(sampler_NormalTex);
- TEXTURE2D(_ParallaxTex); SAMPLER(sampler_ParallaxTex);
-
- Varyings vert (Attributes v)
- {
- Varyings o;
- o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
- o.positionWS = TransformObjectToWorld(v.positionOS.xyz);
- o.nDirWS = TransformObjectToWorldNormal(v.normalOS);
- o.tDirWS = normalize(TransformObjectToWorld(v.tangent.xyz));
- o.bDirWS = normalize(cross(o.nDirWS, o.tDirWS) * v.tangent.w);
- o.uv.xy = TRANSFORM_TEX(v.uv, _MainTex) + float2(frac(_Time.y * _MoveSpeed), 0.0);
- o.uv.zw = v.uv;
- return o;
- }
- half4 frag (Varyings i) : SV_Target
- {
- //Light mylight = GetMainLight();
- //half3 light = mylight.color;
- float3x3 TBN = float3x3(i.tDirWS, i.bDirWS, i.nDirWS);
- float3 vDir = normalize(i.positionWS - _WorldSpaceCameraPos.xyz);
- float3 vDirTS = mul(TBN, vDir);
-
- float3 var_MainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv.zw).rgb;
-
- half2 offset = 0.0;
- half currentDepth = 0.0;
- half parallaxDepth = 0.0;
- half preParallaxDepth = 0.0;
- half heightStep = 1.0 / _ParallaxLayers;
- half2 offsetTemp = vDirTS.xy / -vDirTS.z * _ParallaxSize;
- for (int j = 0; j < _ParallaxLayers; j++)
- {
- parallaxDepth = 1.0 - SAMPLE_TEXTURE2D_LOD(_MainTex, sampler_MainTex, i.uv.xy + offset, 0).r * var_MainTex.r;
- if (currentDepth > parallaxDepth) break;
- preParallaxDepth = parallaxDepth;
- currentDepth += heightStep;
- offset = offsetTemp * currentDepth;
- }
- half preDepth = currentDepth - heightStep;
- half A_C = preDepth - preParallaxDepth;
- half D_B = parallaxDepth - currentDepth;
- half t = A_C / (D_B + A_C);
- half height = lerp(preDepth, currentDepth, t);
- offset += offsetTemp * height;
- i.uv.xy += offset;
-
- half3 finalColor = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv.xy).rgb * var_MainTex.rgb;
- float alphaTex = max(var_MainTex.r * finalColor.r,0.0);
- half alpha = max(pow(abs(lerp(alphaTex, 1.0, _Alpha)), _AlphaExtent), 0.0) * _Alpha;
- return half4(finalColor * _Color.rgb, alpha);
- }
- ENDHLSL
- }
- }
- }
|