标签:游戏开发 图形学 shader unity3d physically-based
根据前文的例子http://blog.csdn.net/wolf96/article/details/44172243(不弄超链接了审核太慢)弄一下真正的基于物理的渲染inline fixed4 LightingOps(SurfaceOps s, fixed3 lightDir, fixed3 viewDir, fixed atten) { viewDir = normalize(viewDir); lightDir = normalize(lightDir); float3 H = normalize(lightDir + viewDir); s.Normal = normalize(s.Normal); float3 N = s.Normal; float _SP = pow(8192, _GL); float d = (_SP + 2) / (8 * PIE) * pow(dot(N, H), _SP); // float f = _SC + (1 - _SC)*pow((1 - dot(H, lightDir)), 5); float f = EnvironmentBRDF(_GL, dot(N, viewDir), _R0F); float k = 2 / sqrt(PIE * (_SP + 2)); float v = 1 / ((dot(N, lightDir)*(1 - k) + k)*(dot(N, viewDir)*(1 - k) + k)); float spec = d*f*v; float4 c = float4(s.Albedo, 1); c.rgb += (_SC + (1.0 - _SC) * s.DeferredFresnel) * spec;//* light.rgb; c += spec*_SC; c.a = s.Alpha; return c; }
void surf(Input IN, inout SurfaceOps o) { half4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainTint; o.Emission = texCUBElod(_Cubemap, float4(IN.worldRefl, _nMips - _GL*_nMips)).rgb * _ReflAmount; float cosT = dot(IN.viewDir, IN.worldNormal); // c = (1 - spec)* c; float F = EnvironmentBRDF(_GL, dot(IN.worldNormal, IN.viewDir), _R0F); o.DeferredFresnel = F; o.Albedo = c.rgb; o.Alpha = c.a; }
Shader "Custom/surface_cube_new ops" { Properties{ _MainTex("Base (RGB)", 2D) = "white" {} _MainTint("Diffuse Color", Color) = (1, 1, 1, 1) _Cubemap("CubeMap", CUBE) = ""{} _ReflAmount("Reflection Amount", Range(0.01, 1)) = 0.5 _SC("Specular Color", Color) = (1, 1, 1, 1) _GL("gloss", Range(0, 1)) = 0.5 _R0F("R0F", Range(0, 1)) = 0.5 _nMips("nMipsF", Range(0, 5)) = 0.5 } SubShader{ Tags{ "RenderType" = "Opaque" } LOD 400 CGPROGRAM #pragma surface surf Ops noambient #pragma glsl #pragma target 3.0 sampler2D _MainTex; samplerCUBE _Cubemap; float4 _MainTint; float _ReflAmount; float4 _SC; float _nMips; float _GL; float _R0F; struct SurfaceOps { fixed3 Albedo; fixed3 Normal; fixed3 Emission; fixed3 Specular; fixed Gloss; fixed Alpha; half DeferredFresnel; }; struct Input { float2 uv_MainTex; float3 worldPos; float3 viewDir; float3 worldNormal; float3 worldRefl;////worldRefl:即为世界空间的反射向量///内置的worldRefl 来做立方图反射(cubemap reflection) }; #define PIE 3.1415926535 #define E 2.71828 float3 EnvironmentBRDF(float g, float NoV, float3 rf0) { float4 t = float4(1 / 0.96, 0.475, (0.0275 - 0.25 * 0.04) / 0.96, 0.25); t *= float4(g, g, g, g); t += float4(0, 0, (0.015 - 0.75 * 0.04) / 0.96, 0.75); float a0 = t.x * min(t.y, exp2(-9.28 * NoV)) + t.z; float a1 = t.w; return saturate(a0 + rf0 * (a1 - a0)); } inline fixed4 LightingOps(SurfaceOps s, fixed3 lightDir, fixed3 viewDir, fixed atten) { viewDir = normalize(viewDir); lightDir = normalize(lightDir); float3 H = normalize(lightDir + viewDir); s.Normal = normalize(s.Normal); float3 N = s.Normal; float _SP = pow(8192, _GL); float d = (_SP + 2) / (8 * PIE) * pow(dot(N, H), _SP); // float f = _SC + (1 - _SC)*pow((1 - dot(H, lightDir)), 5); float f = EnvironmentBRDF(_GL, dot(N, viewDir), _R0F); float k = 2 / sqrt(PIE * (_SP + 2)); float v = 1 / ((dot(N, lightDir)*(1 - k) + k)*(dot(N, viewDir)*(1 - k) + k)); float spec = d*f*v; float4 c = float4(s.Albedo, 1); c.rgb += (_SC + (1.0 - _SC) * s.DeferredFresnel) * spec;//* light.rgb; c += spec*_SC; c.a = s.Alpha; return c; } void surf(Input IN, inout SurfaceOps o) { half4 c = tex2D(_MainTex, IN.uv_MainTex) * _MainTint; o.Emission = texCUBElod(_Cubemap, float4(IN.worldRefl, _nMips - _GL*_nMips)).rgb * _ReflAmount; float cosT = dot(IN.viewDir, IN.worldNormal); // c = (1 - spec)* c; float F = EnvironmentBRDF(_GL, dot(IN.worldNormal, IN.viewDir), _R0F); o.DeferredFresnel = F; o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Diffuse" }
Unity3d 基于物理渲染Physically-Based Rendering之实现
标签:游戏开发 图形学 shader unity3d physically-based
原文地址:http://blog.csdn.net/wolf96/article/details/44193883