Created
September 28, 2016 08:06
-
-
Save hecomi/b1aa21976d7c64569a76d9ba13c45864 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 "Custom/DeferredSurface" | |
{ | |
Properties | |
{ | |
_Color ("Color", Color) = (1,1,1,1) | |
_MainTex ("Albedo (RGB)", 2D) = "white" {} | |
_Glossiness ("Smoothness", Range(0,1)) = 0.5 | |
_Metallic ("Metallic", Range(0,1)) = 0.0 | |
} | |
SubShader | |
{ | |
Tags { "RenderType"="Opaque" } | |
CGINCLUDE | |
#include "HLSLSupport.cginc" | |
#include "UnityShaderVariables.cginc" | |
#include "UnityCG.cginc" | |
#include "Lighting.cginc" | |
#include "UnityPBSLighting.cginc" | |
sampler2D _MainTex; | |
float4 _MainTex_ST; | |
half _Glossiness; | |
half _Metallic; | |
fixed4 _Color; | |
struct Input | |
{ | |
float2 uv_MainTex; | |
}; | |
void surf(Input IN, inout SurfaceOutputStandard o) | |
{ | |
fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color; | |
o.Albedo = c.rgb; | |
o.Metallic = _Metallic; | |
o.Smoothness = _Glossiness; | |
o.Alpha = c.a; | |
} | |
struct v2f_surf | |
{ | |
float4 pos : SV_POSITION; | |
float2 pack0 : TEXCOORD0; | |
half3 worldNormal : TEXCOORD1; | |
float3 worldPos : TEXCOORD2; | |
half3 viewDir : TEXCOORD3; | |
float4 lmap : TEXCOORD4; | |
#ifdef LIGHTMAP_OFF | |
half3 sh : TEXCOORD5; | |
#endif | |
}; | |
v2f_surf vert_surf (appdata_full v) | |
{ | |
v2f_surf o; | |
UNITY_INITIALIZE_OUTPUT(v2f_surf, o); | |
o.pos = UnityObjectToClipPos(v.vertex); | |
o.pack0.xy = TRANSFORM_TEX(v.texcoord, _MainTex); | |
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz; | |
o.worldNormal = UnityObjectToWorldNormal(v.normal); | |
o.viewDir = UnityWorldSpaceViewDir(o.worldPos); | |
#ifndef DYNAMICLIGHTMAP_OFF | |
o.lmap.zw = v.texcoord2.xy * unity_DynamicLightmapST.xy + unity_DynamicLightmapST.zw; | |
#else | |
o.lmap.zw = 0; | |
#endif | |
#ifndef LIGHTMAP_OFF | |
o.lmap.xy = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw; | |
#else | |
o.lmap.xy = 0; | |
o.sh = ShadeSHPerVertex(o.worldNormal, 0.0); | |
#endif | |
return o; | |
} | |
void frag_surf( | |
v2f_surf IN, | |
out half4 outDiffuse : SV_Target0, | |
out half4 outSpecSmoothness : SV_Target1, | |
out half4 outNormal : SV_Target2, | |
out half4 outEmission : SV_Target3) | |
{ | |
Input surfIN; | |
UNITY_INITIALIZE_OUTPUT(Input, surfIN); | |
surfIN.uv_MainTex = IN.pack0.xy; | |
float3 worldPos = IN.worldPos; | |
fixed3 lightDir = normalize(UnityWorldSpaceLightDir(worldPos)); | |
fixed3 worldViewDir = normalize(UnityWorldSpaceViewDir(worldPos)); | |
SurfaceOutputStandard o; | |
UNITY_INITIALIZE_OUTPUT(SurfaceOutputStandard, o); | |
o.Albedo = 0.0; | |
o.Emission = 0.0; | |
o.Alpha = 0.0; | |
o.Occlusion = 1.0; | |
fixed3 normalWorldVertex = fixed3(0, 0, 1); | |
o.Normal = IN.worldNormal; | |
normalWorldVertex = IN.worldNormal; | |
// call surface function | |
surf(surfIN, o); | |
// Setup lighting environment | |
UnityGI gi; | |
UNITY_INITIALIZE_OUTPUT(UnityGI, gi); | |
gi.indirect.diffuse = 0; | |
gi.indirect.specular = 0; | |
gi.light.color = 0; | |
gi.light.dir = half3(0, 1, 0); | |
gi.light.ndotl = LambertTerm(o.Normal, gi.light.dir); | |
// Call GI (lightmaps/SH/reflections) lighting function | |
UnityGIInput giInput; | |
UNITY_INITIALIZE_OUTPUT(UnityGIInput, giInput); | |
giInput.light = gi.light; | |
giInput.worldPos = worldPos; | |
giInput.worldViewDir = worldViewDir; | |
giInput.atten = 1; | |
giInput.lightmapUV = IN.lmap; | |
#ifdef LIGHTMAP_OFF | |
giInput.ambient = IN.sh; | |
#endif | |
giInput.probeHDR[0] = unity_SpecCube0_HDR; | |
giInput.probeHDR[1] = unity_SpecCube1_HDR; | |
giInput.boxMin[0] = unity_SpecCube0_BoxMin; // .w holds lerp value for blending | |
giInput.boxMax[0] = unity_SpecCube0_BoxMax; | |
giInput.probePosition[0] = unity_SpecCube0_ProbePosition; | |
giInput.boxMax[1] = unity_SpecCube1_BoxMax; | |
giInput.boxMin[1] = unity_SpecCube1_BoxMin; | |
giInput.probePosition[1] = unity_SpecCube1_ProbePosition; | |
LightingStandard_GI(o, giInput, gi); | |
// call lighting function to output g-buffer | |
outEmission = LightingStandard_Deferred(o, worldViewDir, gi, outDiffuse, outSpecSmoothness, outNormal); | |
#ifndef UNITY_HDR_ON | |
outEmission.rgb = exp2(-outEmission.rgb); | |
#endif | |
UNITY_OPAQUE_ALPHA(outDiffuse.a); | |
} | |
ENDCG | |
Pass | |
{ | |
Tags { "LightMode" = "Deferred" } | |
CGPROGRAM | |
#pragma vertex vert_surf | |
#pragma fragment frag_surf | |
#pragma target 3.0 | |
#pragma exclude_renderers nomrt | |
#pragma multi_compile_prepassfinal | |
#pragma skip_variants FOG_LINEAR FOG_EXP FOG_EXP2 | |
ENDCG | |
} | |
} | |
FallBack Off | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment