Unity Shaders: Uber Phong
I've been so happy with the results I've obtained with my Uber Phong shader for Maya that I've immediately ported it to Unity.
Of course, you can't just simply plug your Maya Cg shader into Unity and expect it to work out of the box.
I had to rewrite it but the logic is the same, what changes is just how I get the required data from Unity instead of Maya.
Instead of writing the shader from scratch, I've started from Unity built-in Standard Surface Shader, that way I get shadow casting for free and I only have to rewrite the lighting model part that is in fact my Uber Phong shader.
The only thing I still have to add is the shadow caster pass, so the models also receive shadows.
Here's the code if you want to test it out:
Of course, you can't just simply plug your Maya Cg shader into Unity and expect it to work out of the box.
I had to rewrite it but the logic is the same, what changes is just how I get the required data from Unity instead of Maya.
Instead of writing the shader from scratch, I've started from Unity built-in Standard Surface Shader, that way I get shadow casting for free and I only have to rewrite the lighting model part that is in fact my Uber Phong shader.
The only thing I still have to add is the shadow caster pass, so the models also receive shadows.
Here's the code if you want to test it out:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | Shader "Mistwork/Uber Phong" { Properties { _DiffuseColor("Diffuse Color", Color) = (1, 1, 1, 1) _DiffuseTex ("Diffuse Texture", 2D) = "white" {} _TopColor("Top Color", Color) = (0.15, 0.15, 0.15, 1) _BottomColor("Bottom Color", Color) = (0, 0, 0, 0) _BumpMap ("Bumpmap", 2D) = "bump" {} _SpecularColor("Specular Color", Color) = (1, 1, 1, 1) _SpecularTex("Specular Texture", 2D) = "white" {} _SpecularPower("Specular Power", Range(0, 100)) = 30 _SubsurfaceColor("Subsurface Color", Color) = (0.1, 0, 0, 1) _SubsurfaceSpread("Subsurface Spread", Range(0, 1)) = 0 _FresnelPower("Fresnel Power", Range(0, 10)) = 3 _ReflectionTex("Reflection Texture", Cube) = "" {} _ReflectionIntensity("Reflection Intensity", Range(0, 10)) = 1.0 _ReflectionContrast("Reflection Contrast", Range(0, 10)) = 1.0 } SubShader { Tags { "RenderType" = "Opaque" } CGPROGRAM #pragma surface surf UberPhong fullforwardshadows sampler2D _DiffuseTex; sampler2D _BumpMap; sampler2D _SpecularTex; samplerCUBE _ReflectionTex; float4 _DiffuseColor; float _SpecularPower; float4 _SpecularColor; float4 _SubsurfaceColor; float _SubsurfaceSpread; float _FresnelPower; float4 _TopColor; float4 _BottomColor; float _ReflectionIntensity; float _ReflectionContrast; float4 LightingUberPhong (SurfaceOutput s, float3 lightDir, float3 viewDir, float atten) { float4 outColor; float3 reflectionVector = -reflect(viewDir, s.Normal); //Lamber shading + super basic fake subsurface scattering float3 lambert = saturate((dot(lightDir, s.Normal) * (1 - _SubsurfaceSpread) + _SubsurfaceSpread) + _SubsurfaceColor) * _LightColor0; //Phong specularity float3 specular = saturate(dot(reflectionVector, lightDir)); specular = pow(specular, _SpecularPower) * _SpecularColor * s.Specular.r * _LightColor0; //Basic Fresnel float3 fresnel = pow(1 - saturate(dot(viewDir, s.Normal)), _FresnelPower) * lambert * _LightColor0; //Fake sky ambient float3 ambientColor = lerp(_BottomColor, _TopColor, saturate(dot(float3(0, 1, 0), s.Normal))); float3 cubeTex = texCUBE(_ReflectionTex, reflectionVector); float3 col = _DiffuseColor * s.Albedo.rgb; float3 diffuseLighting = ambientColor + _LightColor0 * lambert; float3 specularity = (fresnel + specular) + (pow(cubeTex, _ReflectionContrast) * _ReflectionIntensity * s.Specular.r); outColor.rgb = diffuseLighting * col + specularity; outColor.a = s.Alpha; return outColor; } struct Input { float2 uv_DiffuseTex; float2 uv_BumpMap; float2 uv_SpecularTex; }; void surf (Input IN, inout SurfaceOutput o) { float3 diffuseTex = tex2D(_DiffuseTex, IN.uv_DiffuseTex).rgb; float3 normalTex = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap)); float3 specularTex = tex2D(_SpecularTex, IN.uv_SpecularTex).rgb; o.Albedo = diffuseTex; o.Specular = specularTex; o.Normal = normalTex; } ENDCG } Fallback "Diffuse" } |