Last active
June 19, 2020 07:50
-
-
Save mazelines/ad0f23058acd6689c639f3478418f4d8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Shader "JP-PBR-BASE-EXPERIMENTAL" | |
| { | |
| Properties | |
| { | |
| _Color("Main Color", Color) = (1,1,1,1) | |
| _MainTex("Base Color", 2D) = "white" {} | |
| _Roughness("Roughness",Range(0,1)) = 1 | |
| _NormalTex("Normal", 2D) = "bump" {} | |
| _Anisotropic("Anisotropic", Range(-20,1)) = 0 | |
| _Metallic("Metalness",Range(0,1)) = 0 | |
| _Emissive("Emissive", Float) = 1 | |
| [Header(Artist Lighting)] | |
| [PerRendererData]_DeviationRange("Devitation Direction XYZ", Vector) = (1,1,1) | |
| [NoScaleOffset]_EmissiveTex("Emissive Tex", 2D) = "black" {} | |
| [ToggleOff] _SpecularHighlights("Specular Highlights(遙섇뀎)", Float) = 0 | |
| [ToggleOff] _GlossyReflections("Glossy Reflections(?띶컙)", Float) = 0 | |
| [Toggle] _NonMetal("NonMetal", Float) = 1 | |
| } | |
| SubShader{ | |
| Tags{"RenderType" = "Opaque" "Queue" = "Geometry" "PerformanceChecks"="False"} | |
| Pass{ | |
| Name "FORWARD" | |
| Tags{ | |
| "LightMode" = "ForwardBase" | |
| } | |
| CGPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| #include "UnityCG.cginc" | |
| #include "AutoLight.cginc" | |
| #include "Lighting.cginc" | |
| #include "UnityPBSLighting.cginc" | |
| #include "UnityStandardBRDF.cginc" | |
| #include "UnityStandardCore.cginc" | |
| #include "X2mIncludes/X2mCommon.cginc" | |
| #pragma multi_compile_fwdbase_fullshadows | |
| #pragma shader_feature _SPECULARHIGHLIGHTS_OFF | |
| #pragma shader_feature _GLOSSYREFLECTIONS_OFF | |
| #pragma shader_feature _NONMETAL | |
| #pragma multi_compile LIGHTMAP_OFF LIGHTMAP_ON | |
| #pragma multi_compile DIRLIGHTMAP_OFF DIRLIGHTMAP_COMBINED DIRLIGHTMAP_SEPARATE | |
| #pragma multi_compile DYNAMICLIGHTMAP_OFF DYNAMICLIGHTMAP_ON | |
| #pragma multi_compile_fog | |
| #pragma only_renderers d3d9 d3d11 glcore gles gles3 metal d3d11_9x xboxone ps4 psp2 n3ds wiiu vulkan | |
| #pragma target 3.5 | |
| #define SHOULD_SAMPLE_SH ( defined (LIGHTMAP_OFF) && defined(DYNAMICLIGHTMAP_OFF) ) | |
| #define UNITY_PASS_FORWARDBASE | |
| #define _NORMALMAP 1 | |
| #define _CAMERA_LIGHT_ON | |
| uniform half _Anisotropic; | |
| uniform half _Roughness; | |
| uniform half _Emissive; | |
| uniform sampler2D _EmissiveTex; | |
| uniform sampler2D _NormalTex; uniform half4 _NormalTex_ST; | |
| uniform half _NonMetal; | |
| uniform float3 _DeviationRange; | |
| struct VertexBaseInput | |
| { | |
| float4 vertex : POSITION; | |
| float4 color : COLOR; | |
| half3 normal : NORMAL; | |
| float4 texcoord0 : TEXCOORD0; | |
| float4 texcoord1 : TEXCOORD1; | |
| float4 texcoord2 : TEXCOORD2; | |
| half4 tangent : TANGENT; | |
| UNITY_VERTEX_INPUT_INSTANCE_ID | |
| }; | |
| struct VertexOutput | |
| { | |
| float4 pos : SV_POSITION; | |
| float4 uv0 : TEXCOORD0; | |
| float4 uv1 : TEXCOORD1; | |
| float4 uv2 : TEXCOORD2; | |
| float3 viewDir : TEXCOORD3; | |
| half3 normalDir : TEXCOORD4; | |
| half3 tangentDir : TEXCOORD5; | |
| half3 bitangentDir : TEXCOORD6; | |
| half4 ambientOrLightmapUV : TEXCOORD7; // SH or Lightmap UV | |
| float4 posWorld : TEXCOORD8; | |
| UNITY_SHADOW_COORDS(9) | |
| UNITY_VERTEX_INPUT_INSTANCE_ID | |
| UNITY_VERTEX_OUTPUT_STEREO | |
| }; | |
| VertexOutput vert(VertexBaseInput v) | |
| { | |
| UNITY_SETUP_INSTANCE_ID(v); | |
| VertexOutput o = (VertexOutput)0; //초기화 | |
| UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o); | |
| o.normalDir = UnityObjectToWorldNormal(v.normal); | |
| o.tangentDir = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz); | |
| o.bitangentDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w * unity_WorldTransformParams.w); | |
| o.pos = UnityObjectToClipPos(v.vertex); | |
| o.uv0.xy = TRANSFORM_TEX(v.texcoord0.xy , _MainTex); | |
| o.uv0.zw = v.texcoord0.zw; | |
| o.uv1 = v.texcoord1; | |
| o.uv2 = v.texcoord2; | |
| float3 lightColor = _LightColor0.rgb; | |
| o.posWorld = mul(unity_ObjectToWorld, v.vertex); | |
| UNITY_TRANSFER_FOG(o,o.pos); | |
| TRANSFER_VERTEX_TO_FRAGMENT(o) | |
| return o; | |
| } | |
| //Since many other light model test function here. | |
| float sqr(float x){ return x*x; } | |
| //Blinn-Phong NDF function here | |
| float BlinnPhongNormalDistribution(float NdotH, float specularpower, float speculargloss){ | |
| float Distribution = pow(NdotH,speculargloss) * specularpower; | |
| Distribution *= (2+specularpower) / (2*3.1415926535); | |
| return Distribution; | |
| } | |
| //end of the Blinn-Phong NDF function | |
| //Phong NDF function here | |
| float PhongNormalDistribution(float RdotV, float specularpower, float speculargloss){ | |
| float Distribution = pow(RdotV,speculargloss) * specularpower; | |
| Distribution *= (2+specularpower) / (2*3.1415926535); | |
| return Distribution; | |
| } | |
| //end of the Phong NDF function | |
| //BackMann NDF function here | |
| float BeckmannNormalDistribution(float roughness, float NdotH) | |
| { | |
| float roughnessSqr = roughness*roughness; | |
| float NdotHSqr = NdotH*NdotH; | |
| return max(0.000001,(1.0 / (3.1415926535*roughnessSqr*NdotHSqr*NdotHSqr)) * exp((NdotHSqr-1)/(roughnessSqr*NdotHSqr))); | |
| } | |
| //end of the BackMann NDF function | |
| //GaussianNormalNDF function here | |
| float GaussianNormalDistribution(float roughness, float NdotH) | |
| { | |
| float roughnessSqr = roughness*roughness; | |
| float thetaH = acos(NdotH); | |
| return exp(-thetaH*thetaH/roughnessSqr); | |
| } | |
| //end of GaussianNormalNDF function | |
| //GGX NDF function here | |
| float GGXNormalDistribution(float roughness, float NdotH) | |
| { | |
| float roughnessSqr = roughness * roughness; | |
| float NdotHSqr = NdotH * NdotH; | |
| float TanNdotHSqr = (1-NdotHSqr)/NdotHSqr; | |
| return (1.0/3.1415926535) * sqrt(roughness/(NdotHSqr * (roughnessSqr + TanNdotHSqr))); | |
| } | |
| //end of GGX function | |
| //TrowbridgeReitzNormal NDF here | |
| float TrowbridgeReitzNormalDistribution(float roughness, float NdotH) | |
| { | |
| float roughnessSqr = roughness * roughness; | |
| float Distribution = NdotH * NdotH * (roughnessSqr-1.0) + 1.0; | |
| return roughnessSqr / (3.1415926535 * Distribution*Distribution); | |
| } | |
| //end of TrowbridgeReitzNormal NDF | |
| //WardAnisotropicNormal NDF here | |
| float WardAnisotropicNormalDistribution(float anisotropic, float NdotL, float NdotV, float NdotH, float HdotX, float HdotY){ | |
| float aspect = sqrt(1.0h-anisotropic * 0.9h); | |
| float X = max(.001, sqr(1.0-_Glossiness)/aspect) * 5; | |
| float Y = max(.001, sqr(1.0-_Glossiness)*aspect) * 5; | |
| float exponent = -(sqr(HdotX/X) + sqr(HdotY/Y)) / sqr(NdotH); | |
| float Distribution = 1.0 / ( 3.14159265 * X * Y * sqrt(NdotL * NdotV)); | |
| Distribution *= exp(exponent); | |
| return Distribution; | |
| } | |
| //end of WardAnisotropicNormal NDF | |
| //Geometric Shadowing function Here. | |
| //Implicit GSF here | |
| float ImplicitGeometricShadowingFunction (float NdotL, float NdotV) | |
| { | |
| float Gs = (NdotL * NdotV); | |
| return Gs; | |
| } | |
| //end of Implicit GSF | |
| float4 frag(VertexOutput i) : COLOR{ | |
| UNITY_SETUP_INSTANCE_ID(i); | |
| i.normalDir = normalize(i.normalDir); | |
| float3x3 tangentW = float3x3(i.tangentDir, i.bitangentDir, i.normalDir); | |
| half3 Normal_var = UnpackNormal(tex2D(_NormalTex , i.uv0)); | |
| half3 tangentSpaceNormal = Normal_var.xyz; | |
| float3 normalWorld = normalize(mul(tangentSpaceNormal, tangentW)); | |
| i.viewDir = normalize(i.posWorld.xyz - _WorldSpaceCameraPos); | |
| float3 viewReflectDirection = normalize(reflect(-i.viewDir, normalWorld)); | |
| //float3 lightDir = normalize(_WorldSpaceLightPos0.xyz); | |
| float3 lightDir = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w)); | |
| float3 lightReflectDirection = reflect(-lightDir, normalWorld); | |
| float3 halfDirection = normalize(i.viewDir + lightDir); | |
| //Extention data sets | |
| float NdotL = max(0.0, dot(normalWorld, lightDir)); | |
| float NdotH = max(0.0, dot(normalWorld, halfDirection)); | |
| float NdotV = max(0.0, dot(normalWorld, i.viewDir)); | |
| float VdotH = max(0.0, dot(i.viewDir, halfDirection)); | |
| float LdotH = max(0.0, dot(lightDir, halfDirection)); | |
| float LdotV = max(0.0, dot(lightDir, i.viewDir)); | |
| float RdotV = max(0.0, dot(lightReflectDirection, i.viewDir)); | |
| float HdotX = dot(halfDirection,i.tangentDir); | |
| float HdotY = dot(halfDirection, i.bitangentDir); | |
| UNITY_LIGHT_ATTENUATION(attenuation, i, i.posWorld.xyz); | |
| float3 attenColor = attenuation * _LightColor0.rgb; | |
| //End of Extention data | |
| half oneMinusReflectivity; | |
| half outputAlpha; | |
| half occlusion=1; | |
| FragmentCommonData s = (FragmentCommonData)0; | |
| s.alpha; | |
| s.diffColor = tex2D(_MainTex, TRANSFORM_TEX(i.uv0, _MainTex)); | |
| s.diffColor = DiffuseAndSpecularFromMetallic(s.diffColor, _Metallic, /*out*/ _SpecColor.rgb, /*out*/ oneMinusReflectivity);; | |
| s.diffColor = PreMultiplyAlpha (s.diffColor, s.alpha, oneMinusReflectivity, /*out*/ outputAlpha); | |
| s.specColor = _SpecColor; | |
| s.oneMinusReflectivity = oneMinusReflectivity; | |
| s.eyeVec = i.viewDir; | |
| s.smoothness = 1-_Roughness; | |
| s.normalWorld = normalWorld; | |
| s.posWorld = i.posWorld.xyz; | |
| UnityLight light = MainLight(); | |
| light.color = _LightColor0; | |
| light.dir = lightDir; | |
| UnityGI gi = FragmentGI(s, occlusion, i.ambientOrLightmapUV, attenuation, light , 1); | |
| half4 c = BRDF1_Unity_PBS(s.diffColor, s.specColor, s.oneMinusReflectivity, s.smoothness , s.normalWorld, s.eyeVec, gi.light, gi.indirect); | |
| c.a = 1; | |
| return OutputForward( c, c.a ); | |
| } | |
| ENDCG | |
| } | |
| } | |
| FallBack "Legacy Shaders/Diffuse" | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment