From 8bbe9199379a0cb568890aa24c5701cd876651dd Mon Sep 17 00:00:00 2001 From: Firtina Ozbalikci Date: Wed, 10 Feb 2016 22:00:39 +0000 Subject: [PATCH] Make 5.3.2p1 compatible --- .gitignore | 1 + Shaders/Standard.mat | 138 +++++++++ Shaders/StandardClippable.mat | 170 ++++++++++++ Shaders/StandardClippable.shader | 35 +-- .../UnityStandardCoreForwardClippable.cginc | 24 ++ Shaders/plane_clipping.cginc | 6 +- Shaders/standard_clipped.cginc | 262 +++++++++++++----- 7 files changed, 537 insertions(+), 99 deletions(-) create mode 100644 .gitignore create mode 100644 Shaders/Standard.mat create mode 100644 Shaders/StandardClippable.mat create mode 100644 Shaders/UnityStandardCoreForwardClippable.cginc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c6bb293 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.meta diff --git a/Shaders/Standard.mat b/Shaders/Standard.mat new file mode 100644 index 0000000..589201e --- /dev/null +++ b/Shaders/Standard.mat @@ -0,0 +1,138 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_Name: Standard + m_Shader: {fileID: 46, guid: 0000000000000000f000000000000000, type: 0} + m_ShaderKeywords: CLIP_ONE + m_LightmapFlags: 5 + m_CustomRenderQueue: -1 + stringTagMap: {} + m_SavedProperties: + serializedVersion: 2 + m_TexEnvs: + data: + first: + name: _MainTex + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _BumpMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _DetailNormalMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _ParallaxMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _OcclusionMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _EmissionMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _DetailMask + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _DetailAlbedoMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _MetallicGlossMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + data: + first: + name: _SrcBlend + second: 1 + data: + first: + name: _DstBlend + second: 0 + data: + first: + name: _Cutoff + second: 0.5 + data: + first: + name: _Parallax + second: 0.02 + data: + first: + name: _ZWrite + second: 1 + data: + first: + name: _Glossiness + second: 0.5 + data: + first: + name: _BumpScale + second: 1 + data: + first: + name: _OcclusionStrength + second: 1 + data: + first: + name: _DetailNormalMapScale + second: 1 + data: + first: + name: _UVSec + second: 0 + data: + first: + name: _Mode + second: 0 + data: + first: + name: _Metallic + second: 0 + m_Colors: + data: + first: + name: _EmissionColor + second: {r: 0, g: 0, b: 0, a: 1} + data: + first: + name: _Color + second: {r: 1, g: 1, b: 1, a: 1} diff --git a/Shaders/StandardClippable.mat b/Shaders/StandardClippable.mat new file mode 100644 index 0000000..fd45094 --- /dev/null +++ b/Shaders/StandardClippable.mat @@ -0,0 +1,170 @@ +%YAML 1.1 +%TAG !u! tag:unity3d.com,2011: +--- !u!21 &2100000 +Material: + serializedVersion: 6 + m_ObjectHideFlags: 0 + m_PrefabParentObject: {fileID: 0} + m_PrefabInternal: {fileID: 0} + m_Name: StandardClippable + m_Shader: {fileID: 4800000, guid: 5ff29455d94cbf144931df4d8a4e5375, type: 3} + m_ShaderKeywords: CLIP_ONE _EMISSION + m_LightmapFlags: 1 + m_CustomRenderQueue: -1 + stringTagMap: {} + m_SavedProperties: + serializedVersion: 2 + m_TexEnvs: + data: + first: + name: _MainTex + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _BumpMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _DetailNormalMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _ParallaxMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _OcclusionMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _EmissionMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _DetailMask + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _DetailAlbedoMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + data: + first: + name: _MetallicGlossMap + second: + m_Texture: {fileID: 0} + m_Scale: {x: 1, y: 1} + m_Offset: {x: 0, y: 0} + m_Floats: + data: + first: + name: _SrcBlend + second: 1 + data: + first: + name: _DstBlend + second: 0 + data: + first: + name: _Cutoff + second: 0.5 + data: + first: + name: _Parallax + second: 0.02 + data: + first: + name: _ZWrite + second: 1 + data: + first: + name: _Glossiness + second: 0.5 + data: + first: + name: _BumpScale + second: 1 + data: + first: + name: _OcclusionStrength + second: 1 + data: + first: + name: _DetailNormalMapScale + second: 1 + data: + first: + name: _UVSec + second: 0 + data: + first: + name: _Mode + second: 0 + data: + first: + name: _Metallic + second: 0 + data: + first: + name: _EmissionScaleUI + second: 0 + m_Colors: + data: + first: + name: _EmissionColor + second: {r: 1, g: 1, b: 1, a: 1} + data: + first: + name: _Color + second: {r: 1, g: 1, b: 1, a: 1} + data: + first: + name: _EmissionColorUI + second: {r: 1, g: 1, b: 1, a: 1} + data: + first: + name: _planePos + second: {r: 0, g: 0, b: 0, a: 0} + data: + first: + name: _planePos2 + second: {r: 0, g: 0, b: 0, a: 1} + data: + first: + name: _planePos3 + second: {r: 0, g: 0, b: 0, a: 1} + data: + first: + name: _planeNorm + second: {r: 0, g: 1, b: 0, a: 0} + data: + first: + name: _planeNorm2 + second: {r: 0, g: 1, b: 0, a: 1} + data: + first: + name: _planeNorm3 + second: {r: 0, g: 1, b: 0, a: 1} diff --git a/Shaders/StandardClippable.shader b/Shaders/StandardClippable.shader index 4b3b265..8c8376b 100644 --- a/Shaders/StandardClippable.shader +++ b/Shaders/StandardClippable.shader @@ -30,9 +30,6 @@ Shader "Custom/StandardClippable" { [Enum(UV0,0,UV1,1)] _UVSec ("UV Set for secondary textures", Float) = 0 - // UI-only data - [HideInInspector] _EmissionScaleUI("Scale", Float) = 0.0 - [HideInInspector] _EmissionColorUI("Color", Color) = (1,1,1) // Blending state [HideInInspector] _Mode ("__mode", Float) = 0.0 @@ -85,13 +82,11 @@ Shader "Custom/StandardClippable" { #pragma multi_compile_fwdbase #pragma multi_compile_fog - - #pragma vertex vertForwardBase - #pragma fragment fragForwardBase #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE - - #include "standard_clipped.cginc" + #pragma vertex vertBase + #pragma fragment fragBase + #include "UnityStandardCoreForwardClippable.cginc" ENDCG } @@ -122,13 +117,12 @@ Shader "Custom/StandardClippable" { #pragma multi_compile_fwdadd_fullshadows #pragma multi_compile_fog - - #pragma vertex vertForwardAdd - #pragma fragment fragForwardAdd #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE + #pragma vertex vertAdd + #pragma fragment fragAdd - #include "standard_clipped.cginc" + #include "UnityStandardCoreForwardClippable.cginc" ENDCG } @@ -249,13 +243,11 @@ Shader "Custom/StandardClippable" { #pragma multi_compile_fwdbase #pragma multi_compile_fog - - #pragma vertex vertForwardBase - #pragma fragment fragForwardBase - #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE - #include "standard_clipped.cginc" + #pragma vertex vertBase + #pragma fragment fragBase + #include "UnityStandardCoreForwardClippable.cginc" ENDCG } @@ -283,12 +275,10 @@ Shader "Custom/StandardClippable" { #pragma multi_compile_fwdadd_fullshadows #pragma multi_compile_fog - #pragma vertex vertForwardAdd - #pragma fragment fragForwardAdd - #pragma multi_compile __ CLIP_ONE CLIP_TWO CLIP_THREE - - #include "standard_clipped.cginc" + #pragma vertex vertAdd + #pragma fragment fragAdd + #include "UnityStandardCoreForwardClippable.cginc" ENDCG } @@ -340,6 +330,7 @@ Shader "Custom/StandardClippable" { } } + FallBack "VertexLit" CustomEditor "StandardShaderGUI" } diff --git a/Shaders/UnityStandardCoreForwardClippable.cginc b/Shaders/UnityStandardCoreForwardClippable.cginc new file mode 100644 index 0000000..44ad19a --- /dev/null +++ b/Shaders/UnityStandardCoreForwardClippable.cginc @@ -0,0 +1,24 @@ +#ifndef UNITY_STANDARD_CORE_FORWARD_INCLUDED +#define UNITY_STANDARD_CORE_FORWARD_INCLUDED + +#if defined(UNITY_NO_FULL_STANDARD_SHADER) +# define UNITY_STANDARD_SIMPLE 1 +#endif + +#include "UnityStandardConfig.cginc" + +#if UNITY_STANDARD_SIMPLE + #include "UnityStandardCoreForwardSimple.cginc" + VertexOutputBaseSimple vertBase (VertexInput v) { return vertForwardBaseSimple(v); } + VertexOutputForwardAddSimple vertAdd (VertexInput v) { return vertForwardAddSimple(v); } + half4 fragBase (VertexOutputBaseSimple i) : SV_Target { return fragForwardBaseSimpleInternal(i); } + half4 fragAdd (VertexOutputForwardAddSimple i) : SV_Target { return fragForwardAddSimpleInternal(i); } +#else + #include "standard_clipped.cginc" + VertexOutputForwardBase vertBase (VertexInput v) { return vertForwardBase(v); } + VertexOutputForwardAdd vertAdd (VertexInput v) { return vertForwardAdd(v); } + half4 fragBase (VertexOutputForwardBase i) : SV_Target { return fragForwardBaseInternal(i); } + half4 fragAdd (VertexOutputForwardAdd i) : SV_Target { return fragForwardAddInternal(i); } +#endif + +#endif // UNITY_STANDARD_CORE_FORWARD_INCLUDED diff --git a/Shaders/plane_clipping.cginc b/Shaders/plane_clipping.cginc index 3a11aa3..b381294 100644 --- a/Shaders/plane_clipping.cginc +++ b/Shaders/plane_clipping.cginc @@ -9,11 +9,11 @@ #define PLANE_CLIPPING_ENABLED 1 //http://mathworld.wolfram.com/Point-PlaneDistance.html - float distanceToPlane(float3 planePosition, float3 planeNormal, float3 pointInWorld) + float distanceToPlane(half3 planePosition, half3 planeNormal, half3 pointInWorld) { //w = vector from plane to point - float3 w = - ( planePosition - pointInWorld ); - float res = ( planeNormal.x * w.x + + half3 w = - ( planePosition - pointInWorld ); + half res = ( planeNormal.x * w.x + planeNormal.y * w.y + planeNormal.z * w.z ) / sqrt( planeNormal.x * planeNormal.x + diff --git a/Shaders/standard_clipped.cginc b/Shaders/standard_clipped.cginc index ed88980..39e74bb 100644 --- a/Shaders/standard_clipped.cginc +++ b/Shaders/standard_clipped.cginc @@ -17,7 +17,7 @@ // skips normalization per-vertex and expects normalization to happen per-pixel half3 NormalizePerVertexNormal (half3 n) { - #if (SHADER_TARGET < 30) + #if (SHADER_TARGET < 30) || UNITY_STANDARD_SIMPLE return normalize(n); #else return n; // will normalize per-pixel instead @@ -26,7 +26,7 @@ half3 NormalizePerVertexNormal (half3 n) half3 NormalizePerPixelNormal (half3 n) { - #if (SHADER_TARGET < 30) + #if (SHADER_TARGET < 30) || UNITY_STANDARD_SIMPLE return n; #else return normalize(n); @@ -88,11 +88,14 @@ UnityIndirect ZeroIndirect () //------------------------------------------------------------------------------------- // Common fragment setup + +// deprecated half3 WorldNormal(half4 tan2world[3]) { return normalize(tan2world[2].xyz); } +// deprecated #ifdef _TANGENT_TO_WORLD half3x3 ExtractTangentToWorldPerPixel(half4 tan2world[3]) { @@ -120,6 +123,32 @@ half3 WorldNormal(half4 tan2world[3]) } #endif +half3 PerPixelWorldNormal(float4 i_tex, half4 tangentToWorld[3]) +{ +#ifdef _NORMALMAP + half3 tangent = tangentToWorld[0].xyz; + half3 binormal = tangentToWorld[1].xyz; + half3 normal = tangentToWorld[2].xyz; + + #if UNITY_TANGENT_ORTHONORMALIZE + normal = NormalizePerPixelNormal(normal); + + // ortho-normalize Tangent + tangent = normalize (tangent - normal * dot(tangent, normal)); + + // recalculate Binormal + half3 newB = cross(normal, tangent); + binormal = newB * sign (dot (newB, binormal)); + #endif + + half3 normalTangent = NormalInTangentSpace(i_tex); + half3 normalWorld = NormalizePerPixelNormal(tangent * normalTangent.x + binormal * normalTangent.y + normal * normalTangent.z); // @TODO: see if we can squeeze this normalize on SM2.0 as well +#else + half3 normalWorld = normalize(tangentToWorld[2].xyz); +#endif + return normalWorld; +} + #ifdef _PARALLAXMAP #define IN_VIEWDIR4PARALLAX(i) NormalizePerPixelNormal(half3(i.tangentToWorldAndParallax[0].w,i.tangentToWorldAndParallax[1].w,i.tangentToWorldAndParallax[2].w)) #define IN_VIEWDIR4PARALLAX_FWDADD(i) NormalizePerPixelNormal(i.viewDirForParallax.xyz) @@ -138,7 +167,7 @@ half3 WorldNormal(half4 tan2world[3]) #define IN_LIGHTDIR_FWDADD(i) half3(i.tangentToWorldAndLightDir[0].w, i.tangentToWorldAndLightDir[1].w, i.tangentToWorldAndLightDir[2].w) #define FRAGMENT_SETUP(x) FragmentCommonData x = \ - FragmentSetup(i.tex, i.eyeVec, WorldNormal(i.tangentToWorldAndParallax), IN_VIEWDIR4PARALLAX(i), ExtractTangentToWorldPerPixel(i.tangentToWorldAndParallax), IN_WORLDPOS(i)); + FragmentSetup(i.tex, i.eyeVec, IN_VIEWDIR4PARALLAX(i), i.tangentToWorldAndParallax, IN_WORLDPOS(i)); #if PLANE_CLIPPING_ENABLED #define IN_WORLDPOS_FWDADD(i) i.posWorld @@ -147,7 +176,7 @@ half3 WorldNormal(half4 tan2world[3]) #endif #define FRAGMENT_SETUP_FWDADD(x) FragmentCommonData x = \ - FragmentSetup(i.tex, i.eyeVec, WorldNormal(i.tangentToWorldAndLightDir), IN_VIEWDIR4PARALLAX_FWDADD(i), ExtractTangentToWorldPerPixel(i.tangentToWorldAndLightDir), IN_WORLDPOS_FWDADD(i)); + FragmentSetup(i.tex, i.eyeVec, IN_VIEWDIR4PARALLAX_FWDADD(i), i.tangentToWorldAndLightDir, IN_WORLDPOS_FWDADD(i)); struct FragmentCommonData { @@ -157,6 +186,14 @@ struct FragmentCommonData half oneMinusReflectivity, oneMinusRoughness; half3 normalWorld, eyeVec, posWorld; half alpha; + +#if UNITY_OPTIMIZE_TEXCUBELOD || UNITY_STANDARD_SIMPLE + half3 reflUVW; +#endif + +#if UNITY_STANDARD_SIMPLE + half3 tangentSpaceNormal; +#endif }; #ifndef UNITY_SETUP_BRDF_INPUT @@ -184,7 +221,7 @@ inline FragmentCommonData MetallicSetup (float4 i_tex) { half2 metallicGloss = MetallicGloss(i_tex.xy); half metallic = metallicGloss.x; - half oneMinusRoughness = metallicGloss.y; + half oneMinusRoughness = metallicGloss.y; // this is 1 minus the square root of real roughness m. half oneMinusReflectivity; half3 specColor; @@ -198,7 +235,7 @@ inline FragmentCommonData MetallicSetup (float4 i_tex) return o; } -inline FragmentCommonData FragmentSetup (float4 i_tex, half3 i_eyeVec, half3 i_normalWorld, half3 i_viewDirForParallax, half3x3 i_tanToWorld, half3 i_posWorld) +inline FragmentCommonData FragmentSetup (float4 i_tex, half3 i_eyeVec, half3 i_viewDirForParallax, half4 tangentToWorld[3], half3 i_posWorld) { i_tex = Parallax(i_tex, i_viewDirForParallax); @@ -207,20 +244,9 @@ inline FragmentCommonData FragmentSetup (float4 i_tex, half3 i_eyeVec, half3 i_n clip (alpha - _Cutoff); #endif - #ifdef _NORMALMAP - half3 normalWorld = NormalizePerPixelNormal(mul(NormalInTangentSpace(i_tex), i_tanToWorld)); // @TODO: see if we can squeeze this normalize on SM2.0 as well - #else - // Should get compiled out, isn't being used in the end. - half3 normalWorld = i_normalWorld; - #endif - - half3 eyeVec = i_eyeVec; - eyeVec = NormalizePerPixelNormal(eyeVec); - - FragmentCommonData o = UNITY_SETUP_BRDF_INPUT (i_tex); - o.normalWorld = normalWorld; - o.eyeVec = eyeVec; + o.normalWorld = PerPixelWorldNormal(i_tex, tangentToWorld); + o.eyeVec = NormalizePerPixelNormal(i_eyeVec); o.posWorld = i_posWorld; // NOTE: shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha) @@ -228,15 +254,12 @@ inline FragmentCommonData FragmentSetup (float4 i_tex, half3 i_eyeVec, half3 i_n return o; } -inline UnityGI FragmentGI ( - float3 posWorld, - half occlusion, half4 i_ambientOrLightmapUV, half atten, half oneMinusRoughness, half3 normalWorld, half3 eyeVec, - UnityLight light) +inline UnityGI FragmentGI (FragmentCommonData s, half occlusion, half4 i_ambientOrLightmapUV, half atten, UnityLight light, bool reflections) { UnityGIInput d; d.light = light; - d.worldPos = posWorld; - d.worldViewDir = -eyeVec; + d.worldPos = s.posWorld; + d.worldViewDir = -s.eyeVec; d.atten = atten; #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON) d.ambient = 0; @@ -255,10 +278,30 @@ inline UnityGI FragmentGI ( d.probePosition[1] = unity_SpecCube1_ProbePosition; d.probeHDR[1] = unity_SpecCube1_HDR; - return UnityGlobalIllumination ( - d, occlusion, oneMinusRoughness, normalWorld); + if(reflections) + { + Unity_GlossyEnvironmentData g; + g.roughness = 1 - s.oneMinusRoughness; + #if UNITY_OPTIMIZE_TEXCUBELOD || UNITY_STANDARD_SIMPLE + g.reflUVW = s.reflUVW; + #else + g.reflUVW = reflect(s.eyeVec, s.normalWorld); + #endif + + return UnityGlobalIllumination (d, occlusion, s.normalWorld, g); + } + else + { + return UnityGlobalIllumination (d, occlusion, s.normalWorld); + } +} + +inline UnityGI FragmentGI (FragmentCommonData s, half occlusion, half4 i_ambientOrLightmapUV, half atten, UnityLight light) +{ + return FragmentGI(s, occlusion, i_ambientOrLightmapUV, atten, light, true); } + //------------------------------------------------------------------------------------- half4 OutputForward (half4 output, half alphaFromSurface) { @@ -270,6 +313,33 @@ half4 OutputForward (half4 output, half alphaFromSurface) return output; } +inline half4 VertexGIForward(VertexInput v, float3 posWorld, half3 normalWorld) +{ + half4 ambientOrLightmapUV = 0; + // Static lightmaps + #ifndef LIGHTMAP_OFF + ambientOrLightmapUV.xy = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; + ambientOrLightmapUV.zw = 0; + // Sample light probe for Dynamic objects only (no static or dynamic lightmaps) + #elif UNITY_SHOULD_SAMPLE_SH + #ifdef VERTEXLIGHT_ON + // Approximated illumination from non-important point lights + ambientOrLightmapUV.rgb = Shade4PointLights ( + unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, + unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb, + unity_4LightAtten0, posWorld, normalWorld); + #endif + + ambientOrLightmapUV.rgb = ShadeSHPerVertex (normalWorld, ambientOrLightmapUV.rgb); + #endif + + #ifdef DYNAMICLIGHTMAP_ON + ambientOrLightmapUV.zw = v.uv2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw; + #endif + + return ambientOrLightmapUV; +} + // ------------------------------------------------------------------ // Base forward pass (directional light, emission, lightmaps, ...) @@ -287,6 +357,14 @@ struct VertexOutputForwardBase #if UNITY_SPECCUBE_BOX_PROJECTION || PLANE_CLIPPING_ENABLED float3 posWorld : TEXCOORD8; #endif + + #if UNITY_OPTIMIZE_TEXCUBELOD + #if UNITY_SPECCUBE_BOX_PROJECTION + half3 reflUVW : TEXCOORD9; + #else + half3 reflUVW : TEXCOORD8; + #endif + #endif }; VertexOutputForwardBase vertForwardBase (VertexInput v) @@ -317,32 +395,7 @@ VertexOutputForwardBase vertForwardBase (VertexInput v) //We need this for shadow receving TRANSFER_SHADOW(o); - // Static lightmaps - #ifndef LIGHTMAP_OFF - o.ambientOrLightmapUV.xy = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; - o.ambientOrLightmapUV.zw = 0; - // Sample light probe for Dynamic objects only (no static or dynamic lightmaps) - #elif UNITY_SHOULD_SAMPLE_SH - #if UNITY_SAMPLE_FULL_SH_PER_PIXEL - o.ambientOrLightmapUV.rgb = 0; - #elif (SHADER_TARGET < 30) - o.ambientOrLightmapUV.rgb = ShadeSH9(half4(normalWorld, 1.0)); - #else - // Optimization: L2 per-vertex, L0..L1 per-pixel - o.ambientOrLightmapUV.rgb = ShadeSH3Order(half4(normalWorld, 1.0)); - #endif - // Add approximated illumination from non-important point lights - #ifdef VERTEXLIGHT_ON - o.ambientOrLightmapUV.rgb += Shade4PointLights ( - unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, - unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb, - unity_4LightAtten0, posWorld, normalWorld); - #endif - #endif - - #ifdef DYNAMICLIGHTMAP_ON - o.ambientOrLightmapUV.zw = v.uv2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw; - #endif + o.ambientOrLightmapUV = VertexGIForward(v, posWorld, normalWorld); #ifdef _PARALLAXMAP TANGENT_SPACE_ROTATION; @@ -351,23 +404,29 @@ VertexOutputForwardBase vertForwardBase (VertexInput v) o.tangentToWorldAndParallax[1].w = viewDirForParallax.y; o.tangentToWorldAndParallax[2].w = viewDirForParallax.z; #endif - + + #if UNITY_OPTIMIZE_TEXCUBELOD + o.reflUVW = reflect(o.eyeVec, normalWorld); + #endif + UNITY_TRANSFER_FOG(o,o.pos); return o; } - -half4 fragForwardBase (VertexOutputForwardBase i) : SV_Target +half4 fragForwardBaseInternal (VertexOutputForwardBase i) { FRAGMENT_SETUP(s) PLANE_CLIP(s.posWorld) +#if UNITY_OPTIMIZE_TEXCUBELOD + s.reflUVW = i.reflUVW; +#endif UnityLight mainLight = MainLight (s.normalWorld); half atten = SHADOW_ATTENUATION(i); - + + half occlusion = Occlusion(i.tex.xy); - UnityGI gi = FragmentGI ( - s.posWorld, occlusion, i.ambientOrLightmapUV, atten, s.oneMinusRoughness, s.normalWorld, s.eyeVec, mainLight); + UnityGI gi = FragmentGI (s, occlusion, i.ambientOrLightmapUV, atten, mainLight); half4 c = UNITY_BRDF_PBS (s.diffColor, s.specColor, s.oneMinusReflectivity, s.oneMinusRoughness, s.normalWorld, -s.eyeVec, gi.light, gi.indirect); c.rgb += UNITY_BRDF_GI (s.diffColor, s.specColor, s.oneMinusReflectivity, s.oneMinusRoughness, s.normalWorld, -s.eyeVec, occlusion, gi); @@ -377,8 +436,14 @@ half4 fragForwardBase (VertexOutputForwardBase i) : SV_Target return OutputForward (c, s.alpha); } +half4 fragForwardBase (VertexOutputForwardBase i) : SV_Target // backward compatibility (this used to be the fragment entry function) +{ + return fragForwardBaseInternal(i); +} + // ------------------------------------------------------------------ // Additive forward pass (one light per pass) + struct VertexOutputForwardAdd { float4 pos : SV_POSITION; @@ -423,7 +488,7 @@ VertexOutputForwardAdd vertForwardAdd (VertexInput v) o.tangentToWorldAndLightDir[1].xyz = 0; o.tangentToWorldAndLightDir[2].xyz = normalWorld; #endif - //We need this for shadow receving + //We need this for shadow receiving TRANSFER_VERTEX_TO_FRAGMENT(o); float3 lightDir = _WorldSpaceLightPos0.xyz - posWorld.xyz * _WorldSpaceLightPos0.w; @@ -443,7 +508,7 @@ VertexOutputForwardAdd vertForwardAdd (VertexInput v) return o; } -half4 fragForwardAdd (VertexOutputForwardAdd i) : SV_Target +half4 fragForwardAddInternal (VertexOutputForwardAdd i) { FRAGMENT_SETUP_FWDADD(s) PLANE_CLIP(s.posWorld) @@ -457,6 +522,11 @@ half4 fragForwardAdd (VertexOutputForwardAdd i) : SV_Target return OutputForward (c, s.alpha); } +half4 fragForwardAdd (VertexOutputForwardAdd i) : SV_Target // backward compatibility (this used to be the fragment entry function) +{ + return fragForwardAddInternal(i); +} + // ------------------------------------------------------------------ // Deferred pass @@ -470,6 +540,14 @@ struct VertexOutputDeferred #if UNITY_SPECCUBE_BOX_PROJECTION || PLANE_CLIPPING_ENABLED float3 posWorld : TEXCOORD6; #endif + #if UNITY_OPTIMIZE_TEXCUBELOD + #if UNITY_SPECCUBE_BOX_PROJECTION + half3 reflUVW : TEXCOORD7; + #else + half3 reflUVW : TEXCOORD6; + #endif + #endif + }; @@ -499,18 +577,12 @@ VertexOutputDeferred vertDeferred (VertexInput v) o.tangentToWorldAndParallax[2].xyz = normalWorld; #endif + o.ambientOrLightmapUV = 0; #ifndef LIGHTMAP_OFF o.ambientOrLightmapUV.xy = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; - o.ambientOrLightmapUV.zw = 0; #elif UNITY_SHOULD_SAMPLE_SH - #if (SHADER_TARGET < 30) - o.ambientOrLightmapUV.rgb = ShadeSH9(half4(normalWorld, 1.0)); - #else - // Optimization: L2 per-vertex, L0..L1 per-pixel - o.ambientOrLightmapUV.rgb = ShadeSH3Order(half4(normalWorld, 1.0)); - #endif + o.ambientOrLightmapUV.rgb = ShadeSHPerVertex (normalWorld, o.ambientOrLightmapUV.rgb); #endif - #ifdef DYNAMICLIGHTMAP_ON o.ambientOrLightmapUV.zw = v.uv2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw; #endif @@ -522,7 +594,11 @@ VertexOutputDeferred vertDeferred (VertexInput v) o.tangentToWorldAndParallax[1].w = viewDirForParallax.y; o.tangentToWorldAndParallax[2].w = viewDirForParallax.z; #endif - + + #if UNITY_OPTIMIZE_TEXCUBELOD + o.reflUVW = reflect(o.eyeVec, normalWorld); + #endif + return o; } @@ -544,6 +620,9 @@ void fragDeferred ( FRAGMENT_SETUP(s) PLANE_CLIP(s.posWorld) +#if UNITY_OPTIMIZE_TEXCUBELOD + s.reflUVW = i.reflUVW; +#endif // no analytic lights in this pass UnityLight dummyLight = DummyLight (s.normalWorld); @@ -551,8 +630,13 @@ void fragDeferred ( // only GI half occlusion = Occlusion(i.tex.xy); - UnityGI gi = FragmentGI ( - s.posWorld, occlusion, i.ambientOrLightmapUV, atten, s.oneMinusRoughness, s.normalWorld, s.eyeVec, dummyLight); +#if UNITY_ENABLE_REFLECTION_BUFFERS + bool sampleReflectionsInDeferred = false; +#else + bool sampleReflectionsInDeferred = true; +#endif + + UnityGI gi = FragmentGI (s, occlusion, i.ambientOrLightmapUV, atten, dummyLight, sampleReflectionsInDeferred); half3 color = UNITY_BRDF_PBS (s.diffColor, s.specColor, s.oneMinusReflectivity, s.oneMinusRoughness, s.normalWorld, -s.eyeVec, gi.light, gi.indirect).rgb; color += UNITY_BRDF_GI (s.diffColor, s.specColor, s.oneMinusReflectivity, s.oneMinusRoughness, s.normalWorld, -s.eyeVec, occlusion, gi); @@ -569,6 +653,36 @@ void fragDeferred ( outSpecSmoothness = half4(s.specColor, s.oneMinusRoughness); outNormal = half4(s.normalWorld*0.5+0.5,1); outEmission = half4(color, 1); -} - +} + + +// +// Old FragmentGI signature. Kept only for backward compatibility and will be removed soon +// + +inline UnityGI FragmentGI( + float3 posWorld, + half occlusion, half4 i_ambientOrLightmapUV, half atten, half oneMinusRoughness, half3 normalWorld, half3 eyeVec, + UnityLight light, + bool reflections) +{ + // we init only fields actually used + FragmentCommonData s = (FragmentCommonData)0; + s.oneMinusRoughness = oneMinusRoughness; + s.normalWorld = normalWorld; + s.eyeVec = eyeVec; + s.posWorld = posWorld; +#if UNITY_OPTIMIZE_TEXCUBELOD + s.reflUVW = reflect(eyeVec, normalWorld); +#endif + return FragmentGI(s, occlusion, i_ambientOrLightmapUV, atten, light, reflections); +} +inline UnityGI FragmentGI ( + float3 posWorld, + half occlusion, half4 i_ambientOrLightmapUV, half atten, half oneMinusRoughness, half3 normalWorld, half3 eyeVec, + UnityLight light) +{ + return FragmentGI (posWorld, occlusion, i_ambientOrLightmapUV, atten, oneMinusRoughness, normalWorld, eyeVec, light, true); +} + #endif // UNITY_STANDARD_CORE_INCLUDED