123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- //======= Copyright (c) Stereolabs Corporation, All rights reserved. ===============
- // Computes lighting and shadows and apply them to the real
- Shader "ZED/ZED Forward Lighting"
- {
- Properties{
- [MaterialToggle] directionalLightEffect("Directional light affects image", Int) = 0
- _MaxDepth("Max Depth Range", Range(1,40)) = 40
- _DepthXYZTex("Depth texture", 2D) = "" {}
- _MainTex("Main texture", 2D) = "" {}
- }
- SubShader
- {
- ZWrite On
- Pass
- {
- Name "FORWARD"
- Tags{ "LightMode" = "Always" }
- Cull Off
- CGPROGRAM
- #define ZEDStandard
- // compile directives
- #pragma target 4.0
- #pragma vertex vert_surf
- #pragma fragment frag_surf
- #pragma multi_compile_fwdbase
- #pragma multi_compile_fwdadd_fullshadows
- #pragma multi_compile __ NO_DEPTH
- #include "HLSLSupport.cginc"
- #include "UnityShaderVariables.cginc"
- #define UNITY_PASS_FORWARDBASE
- #include "UnityCG.cginc"
- #include "AutoLight.cginc"
- #include "../ZED_Utils.cginc"
- #define ZED_SPOT_LIGHT_DECLARATION
- #define ZED_POINT_LIGHT_DECLARATION
- #include "ZED_Lighting.cginc"
- sampler2D _MainTex;
- struct Input {
- float2 uv_MainTex;
- };
- float4x4 _CameraMatrix;
- sampler2D _DirectionalShadowMap;
- struct v2f_surf {
- float4 pos : SV_POSITION;
- float4 pack0 : TEXCOORD0;
- SHADOW_COORDS(4)
- ZED_WORLD_DIR(1)
- };
- float4 _MainTex_ST;
- sampler2D _DepthXYZTex;
- float4 _DepthXYZTex_ST;
- sampler2D _MaskTex;
- int _HasShadows;
- float4 ZED_directionalLight[2];
- int directionalLightEffect;
- float _ZEDFactorAffectReal;
- float _MaxDepth;
- bool Unity_IsNan_float3(float3 In)
- {
- bool Out = (In < 0.0 || In > 0.0 || In == 0.0) ? 0 : 1;
- return Out;
- }
- // vertex shader
- v2f_surf vert_surf(appdata_full v)
- {
- v2f_surf o;
- UNITY_INITIALIZE_OUTPUT(v2f_surf,o);
- o.pos = UnityObjectToClipPos(v.vertex);
- ZED_TRANSFER_WORLD_DIR(o)
- o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex);
- o.pack0.zw = TRANSFORM_TEX(v.texcoord, _DepthXYZTex);
- o.pack0.y = 1 - o.pack0.y;
- o.pack0.w = 1 - o.pack0.w;
- TRANSFER_SHADOW(o);
- return o;
- }
- // fragment shader
- void frag_surf(v2f_surf IN, out fixed4 outColor : SV_Target, out float outDepth : SV_Depth)
- {
- UNITY_INITIALIZE_OUTPUT(fixed4,outColor);
- float4 uv = IN.pack0;
- float3 zed_xyz = tex2D(_DepthXYZTex, uv.zw).xxx;
- //Filter out depth values beyond the max value.
- if (_MaxDepth < 40.0) //Avoid clipping out FAR values when not using feature.
- {
- if (zed_xyz.z > _MaxDepth || Unity_IsNan_float3(zed_xyz.z)) discard;
- }
- #ifdef NO_DEPTH
- #if SHADER_API_D3D11
- outDepth = 0;
- #elif SHADER_API_GLCORE
- outDepth = 1000;//fake infinite depth
- #endif
- #else
- outDepth = computeDepthXYZ(zed_xyz.z);
- #endif
- fixed4 c = 0;
- float4 color = tex2D(_MainTex, uv.xy).bgra;
- float3 normals = tex2D(_NormalsTex, uv.zw).rgb;
- //Apply directional light
- color *= _ZEDFactorAffectReal;
- //Compute world space
- float3 worldspace;
- GET_XYZ(IN, zed_xyz.x, worldspace)
- //Apply shadows
- // e(1) == 2.71828182846
- if (_HasShadows == 1) {
- //Depends on the ambient lighting
- float atten = saturate(tex2D(_DirectionalShadowMap, fixed2(uv.z, 1 - uv.w)) + log(1 + 1.72*length(UNITY_LIGHTMODEL_AMBIENT.rgb)/4.0));
- c = half4(color*atten);
- }
- else {
- c = half4(color);
- }
- //Add light
- c += saturate(computeLighting(color.rgb, normals, worldspace, 1));
- c.a = 0;
- outColor.rgb = c;
- }
- ENDCG
- }
- }
- Fallback Off
- }
|