123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522 |
- // Please note: This shader will never be batched as unity uses Materialpropertyblocks to write per instance properties.
- Shader "Lux URP/Nature/Tree Creator Bark Optimized"
- {
- Properties
- {
- [Header(Surface Options)]
- [Space(5)]
- [ToggleOff(_RECEIVE_SHADOWS_OFF)]
- _ReceiveShadows ("Receive Shadows", Float) = 1.0
- [Toggle(_ENABLEDITHERING)]
- _EnableDither ("Enable Dithering for VR", Float) = 0.0
- [Header(Surface Inputs)]
- [Space(5)]
- [MainColor]
- _Color ("Main Color", Color) = (1,1,1,1)
- [MainTexture]
- _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
- _Cutoff ("Alpha cutoff", Range(0.0, 1.0)) = 0.3
- [NoScaleOffset]
- _BumpSpecMap ("Normalmap (GA) Spec (R)", 2D) = "bump" {}
- [NoScaleOffset]
- _TranslucencyMap ("Trans (B) Gloss(A)", 2D) = "white" {}
- [Space(5)]
- _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 1)
- [HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
- [HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
- [HideInInspector] _SquashAmount ("Squash", Float) = 1
- // Lightmapper and outline selection shader need _MainTex, _Color and _Cutoff
-
- }
- SubShader
- {
- Tags
- {
- "RenderPipeline" = "UniversalPipeline"
- "RenderType" = "Opaque"
- "Queue" = "Geometry"
- }
- LOD 100
- Pass
- {
- Name "ForwardLit"
- Tags{"LightMode" = "UniversalForward"}
- ZWrite On
- Cull Back
- HLSLPROGRAM
- // Required to compile gles 2.0 with standard SRP library
- #pragma prefer_hlslcc gles
- #pragma exclude_renderers d3d11_9x
- #pragma target 2.0
- // -------------------------------------
- // Material Keywords
- // #define _SPECULAR_SETUP 1
- //#pragma shader_feature _NORMALMAP
- #pragma shader_feature _ENABLEDITHERING
- #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
- // #pragma shader_feature _SPECULARHIGHLIGHTS_OFF
- // #pragma shader_feature _ENVIRONMENTREFLECTIONS_OFF
- #pragma shader_feature _RECEIVE_SHADOWS_OFF
- // -------------------------------------
- // Universal Pipeline keywords
- #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
- #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
- #pragma multi_compile _ _ADDITIONAL_LIGHTS_VERTEX _ADDITIONAL_LIGHTS
- #pragma multi_compile_fragment _ _ADDITIONAL_LIGHT_SHADOWS
- #pragma multi_compile_fragment _ _SHADOWS_SOFT
- #pragma multi_compile_fragment _ _SCREEN_SPACE_OCCLUSION
- #pragma multi_compile _ LIGHTMAP_SHADOW_MIXING
- #pragma multi_compile _ SHADOWS_SHADOWMASK
- // -------------------------------------
- // Unity defined keywords
- // #pragma multi_compile _ DIRLIGHTMAP_COMBINED
- // #pragma multi_compile _ LIGHTMAP_ON
- #pragma multi_compile_fog
- //--------------------------------------
- // GPU Instancing
- #pragma multi_compile_instancing
- // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
- // Include base inputs and all other needed "base" includes
- #include "Includes/Lux URP Tree Creator Inputs.hlsl"
- #include "Includes/Lux URP Tree Creator Library.hlsl"
- #pragma vertex LitPassVertex
- #pragma fragment LitPassFragment
- //--------------------------------------
- // Vertex shader
- VertexOutput LitPassVertex(VertexInput input)
- {
- VertexOutput output = (VertexOutput)0;
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_TRANSFER_INSTANCE_ID(input, output);
- UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
- TreeVertLeaf(input);
- VertexPositionInputs vertexInput; //
- vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
- VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
- half3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
- half3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
- half fogFactor = ComputeFogFactor(vertexInput.positionCS.z);
- output.uv = TRANSFORM_TEX(input.texcoord, _MainTex);
-
- output.normalWS = half4(normalInput.normalWS, viewDirWS.x);
- output.tangentWS = half4(normalInput.tangentWS, viewDirWS.y);
- output.bitangentWS = half4(normalInput.bitangentWS, viewDirWS.z);
- OUTPUT_LIGHTMAP_UV(input.lightmapUV, unity_LightmapST, output.lightmapUV);
- OUTPUT_SH(output.normalWS.xyz, output.vertexSH);
-
- output.fogFactorAndVertexLight = half4(fogFactor, vertexLight);
- #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
- output.positionWS = vertexInput.positionWS;
- #endif
- #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
- output.shadowCoord = GetShadowCoord(vertexInput);
- #endif
- output.positionCS = vertexInput.positionCS;
- output.color = input.color;
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- output.screenPos = ComputeScreenPos(output.positionCS);
- #endif
- return output;
- }
- //--------------------------------------
- // Fragment shader and functions
- inline void InitializeSurfaceData(
- float2 uv,
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- float4 screenPos,
- half dither,
- #endif
- out SurfaceDescription outSurfaceData)
- {
- half4 albedoAlpha = SampleAlbedoAlpha(uv.xy, TEXTURE2D_ARGS(_MainTex, sampler_MainTex));
- //outSurfaceData.alpha = Alpha(albedoAlpha.a, half4(1,1,1,1), _Cutoff);
- // Dither
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- half coverage = 1.0h;
- [branch]
- if (dither < 1.0h) {
- coverage = LuxComputeAlphaCoverage(screenPos, dither);
- }
- clip(coverage - 0.01h);
- //albedoAlpha.rgb = coverage;
- #endif
- outSurfaceData.alpha = 1;
- outSurfaceData.albedo = albedoAlpha.rgb; // * _Color.rgb;
- // Normal
- half4 normalSample = SAMPLE_TEXTURE2D(_BumpSpecMap, sampler_BumpSpecMap, uv);
- half3 normal;
- normal.xy = normalSample.ag * 2 - 1;
- normal.z = sqrt(1.0 - saturate(dot(normal.xy, normal.xy)));
- outSurfaceData.normalTS = normal;
- outSurfaceData.specular = _SpecColor.rgb;
- outSurfaceData.occlusion = 1;
- outSurfaceData.translucency = 0;
- // Transmission
- half4 maskSample = SAMPLE_TEXTURE2D(_TranslucencyMap, sampler_TranslucencyMap, uv);
- outSurfaceData.translucency *= maskSample.b;
- //outSurfaceData.occlusion = lerp(1.0h, maskSample.b, _Occlusion);
- //outSurfaceData.mask = maskSample.r;
- outSurfaceData.gloss = maskSample.a;
- }
- void InitializeInputData(VertexOutput input, half3 normalTS, out InputData inputData)
- {
- inputData = (InputData)0;
- #if defined(REQUIRES_WORLD_SPACE_POS_INTERPOLATOR)
- inputData.positionWS = input.positionWS;
- #endif
-
- half3 viewDirWS = half3(input.normalWS.w, input.tangentWS.w, input.bitangentWS.w);
- inputData.normalWS = TransformTangentToWorld(normalTS, half3x3(input.tangentWS.xyz, input.bitangentWS.xyz, input.normalWS.xyz));
- inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS);
- viewDirWS = SafeNormalize(viewDirWS);
- inputData.viewDirectionWS = viewDirWS;
- #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR)
- inputData.shadowCoord = input.shadowCoord;
- #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS)
- inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS);
- #else
- inputData.shadowCoord = float4(0, 0, 0, 0);
- #endif
-
- inputData.fogCoord = input.fogFactorAndVertexLight.x;
- inputData.vertexLighting = input.fogFactorAndVertexLight.yzw;
- inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS);
- inputData.normalizedScreenSpaceUV = GetNormalizedScreenSpaceUV(input.positionCS);
- inputData.shadowMask = SAMPLE_SHADOWMASK(input.lightmapUV);
- }
- half4 LitPassFragment(VertexOutput input) : SV_Target
- {
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
- // Get the surface description
- SurfaceDescription surfaceData;
- InitializeSurfaceData(input.uv,
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- input.screenPos, UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a,
- #endif
- surfaceData);
- // Apply tree color
- surfaceData.albedo *= input.color.rgb;
- surfaceData.occlusion = input.color.a;
- // Prepare surface data (like bring normal into world space and get missing inputs like gi)
- InputData inputData;
- InitializeInputData(input, surfaceData.normalTS, inputData);
- // Apply lighting
- half4 color = LuxURPTreeBarkFragment (
- inputData,
- surfaceData.albedo,
- surfaceData.specular,
- surfaceData.gloss,
- surfaceData.occlusion,
- surfaceData.alpha,
- UNITY_ACCESS_INSTANCED_PROP(Props, _SquashAmount)
- );
-
- // Add fog
- color.rgb = MixFog(color.rgb, inputData.fogCoord);
- return color;
- }
- ENDHLSL
- }
- // Shadows -----------------------------------------------------
-
- Pass
- {
- Name "ShadowCaster"
- Tags{"LightMode" = "ShadowCaster"}
- ZWrite On
- ZTest LEqual
- ColorMask 0
- Cull Back
- HLSLPROGRAM
- // Required to compile gles 2.0 with standard srp library
- #pragma prefer_hlslcc gles
- #pragma exclude_renderers d3d11_9x
- #pragma target 2.0
- // -------------------------------------
- // Material Keywords
- // Usually no shadows during the transition...
- #pragma shader_feature _ENABLEDITHERING
- #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
- //--------------------------------------
- // GPU Instancing
- #pragma multi_compile_instancing
- #pragma vertex ShadowPassVertex
- #pragma fragment ShadowPassFragment
- // Include base inputs and all other needed "base" includes
- #include "Includes/Lux URP Tree Creator Inputs.hlsl"
- #include "Includes/Lux URP Tree Creator Library.hlsl"
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
-
- // Shadow caster specific input
- float3 _LightDirection;
- VertexOutput ShadowPassVertex(VertexInput input)
- {
- VertexOutput output = (VertexOutput)0;
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_TRANSFER_INSTANCE_ID(input, output);
- TreeVertLeaf(input);
- float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
- float3 normalWS = TransformObjectToWorldDir(input.normalOS);
- output.positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
-
- // Dither coords - not perfect for shadows but ok.
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- output.screenPos = ComputeScreenPos(output.positionCS);
- #endif
- #if UNITY_REVERSED_Z
- output.positionCS.z = min(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
- #else
- output.positionCS.z = max(output.positionCS.z, output.positionCS.w * UNITY_NEAR_CLIP_VALUE);
- #endif
- return output;
- }
- half4 ShadowPassFragment(VertexOutput input) : SV_TARGET
- {
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
- // Dither
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- half coverage = 1.0h;
- half dither = UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a;
- [branch]
- if ( dither < 1.0h) {
- coverage = ComputeAlphaCoverage(input.screenPos, dither );
- }
- clip (coverage);
- #endif
- return 0;
- }
- ENDHLSL
- }
- // Depth -----------------------------------------------------
- Pass
- {
- Tags{"LightMode" = "DepthOnly"}
- ZWrite On
- ColorMask 0
- Cull Back
- HLSLPROGRAM
- // Required to compile gles 2.0 with standard srp library
- #pragma prefer_hlslcc gles
- #pragma exclude_renderers d3d11_9x
- #pragma target 2.0
- #pragma vertex DepthOnlyVertex
- #pragma fragment DepthOnlyFragment
- // -------------------------------------
- // Material Keywords
- #pragma shader_feature _ENABLEDITHERING
- #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
- //--------------------------------------
- // GPU Instancing
- #pragma multi_compile_instancing
-
- #define DEPTHONLYPASS
- #include "Includes/Lux URP Tree Creator Inputs.hlsl"
- #include "Includes/Lux URP Tree Creator Library.hlsl"
- VertexOutput DepthOnlyVertex(VertexInput input)
- {
- VertexOutput output = (VertexOutput)0;
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_TRANSFER_INSTANCE_ID(input, output);
- UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
- TreeVertLeaf(input);
- output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- output.screenPos = ComputeScreenPos(output.positionCS);
- #endif
- return output;
- }
- half4 DepthOnlyFragment(VertexOutput input) : SV_TARGET
- {
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
- // Dither
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- half coverage = 1.0h;
- half dither = UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a;
- [branch]
- if ( dither < 1.0h) {
- coverage = ComputeAlphaCoverage(input.screenPos, dither );
- }
- clip (coverage);
- #endif
- return 0;
- }
- ENDHLSL
- }
- // DepthNormal -----------------------------------------------------
- Pass
- {
- Name "DepthNormals"
- Tags{"LightMode" = "DepthNormals"}
- ZWrite On
- Cull[_Cull]
- HLSLPROGRAM
- // Required to compile gles 2.0 with standard srp library
- #pragma prefer_hlslcc gles
- #pragma exclude_renderers d3d11_9x
- #pragma target 2.0
- #pragma vertex DepthNormalsVertex
- #pragma fragment DepthNormalsFragment
- // -------------------------------------
- // Material Keywords
- #pragma shader_feature _ENABLEDITHERING
- #pragma multi_compile __ BILLBOARD_FACE_CAMERA_POS
- //--------------------------------------
- // GPU Instancing
- #pragma multi_compile_instancing
- // #pragma multi_compile _ DOTS_INSTANCING_ON // needs shader target 4.5
-
- #define DEPTHNORMALONLYPASS
- #include "Includes/Lux URP Tree Creator Inputs.hlsl"
- #include "Includes/Lux URP Tree Creator Library.hlsl"
- VertexOutput DepthNormalsVertex(VertexInput input)
- {
- VertexOutput output = (VertexOutput)0;
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_TRANSFER_INSTANCE_ID(input, output);
- UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
- TreeVertLeaf(input);
- output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- output.screenPos = ComputeScreenPos(output.positionCS);
- #endif
- VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, float4(1,1,1,1)); //input.tangentOS);
- output.normalWS.xyz = NormalizeNormalPerVertex(normalInput.normalWS).xyz;
- return output;
- }
- half4 DepthNormalsFragment(VertexOutput input) : SV_TARGET
- {
- UNITY_SETUP_INSTANCE_ID(input);
- UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
- // Dither
- #if defined(BILLBOARD_FACE_CAMERA_POS) && defined(_ENABLEDITHERING)
- half coverage = 1.0h;
- half dither = UNITY_ACCESS_INSTANCED_PROP(Props, _TreeInstanceColor).a;
- [branch]
- if ( dither < 1.0h) {
- coverage = ComputeAlphaCoverage(input.screenPos, dither );
- }
- clip (coverage);
- #endif
- float3 normal = input.normalWS.xyz;
- return float4(PackNormalOctRectEncode(TransformWorldToViewDir(normal, true)), 0.0, 0.0);
- }
- ENDHLSL
- }
- // End Passes -----------------------------------------------------
-
- }
- FallBack "Hidden/InternalErrorShader"
- Dependency "BillboardShader" = "Hidden/Nature/Lux Tree Creator Bark Rendertex"
- }
|