UnityShader案例(二)——Phong高光反射
来源:互联网 发布:linux delay 头文件 编辑:程序博客网 时间:2024/05/17 02:16
一、Phong 高光反射原理
这里的高光反射是一种经验模型,也就是说他并不完全符合真实世界的高光反射现象。它可用于计算那些沿着完全镜面反射方向被反射的光线,这样可以看起来更有光泽。例如金属
计算高光反射需要知道的信息比较多,如表面法线、视角方向、光源方向、反射反向等
1、计算公式:
:最终的高光颜色
:光源颜色
:材质的高光反射颜色,它用于控制该材质对于高光反射的强度和颜色。
:材质的光泽度,它用于控制高光区域的 “亮点”有多宽,其值越大。亮点越小。反之,则亮点越小。
:视觉方向
:反射方向,反射方向r可以由表面法线方向n和光源方向l计算获得:
2、Phong高光原理图
二、Phong高光的Shader实现
1、表面着色器实现Phong高光模型
Shader "Custom/Phong" { Properties { _Color ("Diffuse Color", Color) = (1,1,1,1) _MainTex ("Base(RGB)", 2D) = "white" {} _SpecularColor ("Specular Color", Color) = (1,1,1,1) _SpecularPower ("Specular Power", Range(0,1)) = 1 } SubShader { Tags { "RenderType"="Opaque" } LOD 200 CGPROGRAM #pragma surface surf Phong #pragma target 3.0 sampler2D _MainTex; fixed4 _Color; float4 _SpecularColor; float _SpecularPower; struct Input { float2 uv_MainTex; }; inline float4 LightingPhong(SurfaceOutput s,fixed3 lightDir,half3 viewDir,fixed atten ){ float diff=dot(s.Normal,lightDir); float3 reflectionVector=normalize(2.0*s.Normal*diff-lightDir); float spec=pow(max(0,dot(reflectionVector,viewDir)),_SpecularPower); float3 finalSpec=_SpecularColor*spec; fixed4 c; c.rgb=(s.Albedo*_LightColor0.rgb*diff)+(_LightColor0.rgb*finalSpec); c.a=1.0; return c; } void surf (Input IN, inout SurfaceOutput o) { fixed4 c = tex2D (_MainTex, IN.uv_MainTex)*_Color; o.Albedo = c.rgb; o.Alpha = c.a; } ENDCG } FallBack "Specular"}
2、逐顶点光照实现Phong高光模型
Shader "Custom/Unlit/VertexPhong"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Diffuse ("Diffuse",Color)=(1,1,1,1) _Specular("Specular",Color)=(1,1,1,1) _Gloss("Gloss",Range(8.0,256))=20 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { Tags{"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #include "UnityCG.cginc" #include "Lighting.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal:NORMAL; }; struct v2f { float2 uv : TEXCOORD0; float4 vertex : SV_POSITION; fixed3 diffusecolor:COLOR; fixed3 ambientcolor:COLOR2; fixed3 specularcolor:COLOR1; }; sampler2D _MainTex; fixed4 _Diffuse; fixed4 _Specular; float _Gloss; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT.xyz; fixed3 worldNormal=UnityObjectToWorldNormal(v.normal); fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz); fixed3 diffuse=_LightColor0.rgb * _Diffuse.rgb*saturate(dot(worldNormal,worldLightDir)); fixed3 reflectDir=normalize(reflect(-worldLightDir,worldNormal)); fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-mul(unity_ObjectToWorld,v.vertex).xyz); fixed specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(reflectDir,viewDir)),_Gloss); o.diffusecolor=diffuse; o.specularcolor=specular; o.ambientcolor=ambient; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); fixed3 c=col.rgb*(i.ambientcolor+i.diffusecolor)+i.specularcolor; return fixed4(c,1.0); } ENDCG } }}
3、逐像素光照实现Phong高光模型
Shader "Custom/Unlit/FragmentPhong"{ Properties { _MainTex ("Texture", 2D) = "white" {} _Diffuse ("Diffuse",Color)=(1,1,1,1) _Specular("Specular",Color)=(1,1,1,1) _Gloss("Gloss",Range(8.0,256))=20 } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { Tags{"LightMode"="ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fog #include "UnityCG.cginc" #include "Lighting.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal:NORMAL; }; struct v2f { float2 uv : TEXCOORD0; float3 worldNormal:TEXCOORD1; float3 worldPos:TEXCOORD2; float4 vertex : SV_POSITION; }; sampler2D _MainTex; fixed4 _Diffuse; fixed4 _Specular; float _Gloss; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.vertex = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); o.worldNormal=mul(v.normal,(float3x3)unity_WorldToObject); o.worldPos=mul(unity_ObjectToWorld,v.vertex).xyz; return o; } fixed4 frag (v2f i) : SV_Target { fixed4 col = tex2D(_MainTex, i.uv); fixed3 ambient=UNITY_LIGHTMODEL_AMBIENT*col.rgb; fixed3 worldNormal=normalize(i.worldNormal); fixed3 worldLightDir=normalize(_WorldSpaceLightPos0.xyz); fixed3 diffuse=col.rgb*_LightColor0.rgb *_Diffuse.rgb*saturate(dot(worldNormal,worldLightDir)); fixed3 reflectDir=normalize(reflect(-worldLightDir,worldNormal)); fixed3 viewDir=normalize(_WorldSpaceCameraPos.xyz-i.worldPos.xyz); fixed3 specular=_LightColor0.rgb*_Specular.rgb*pow(saturate(dot(reflectDir,viewDir)),_Gloss); fixed3 color=ambient+diffuse+specular; return fixed4(color,1.0); } ENDCG } } FallBack "Diffuse"}
阅读全文
1 0
- UnityShader案例(二)——Phong高光反射
- UnityShader案例(三)——BlinnPhong高光反射
- 高光反射-逐像素光照(Blinn-Phong)
- UnityShader初级篇——实现逐顶点高光反射光照模型
- UnityShader初级篇——实现逐像素高光反射光照模型
- UnityShader案例(一)——漫反射
- UnityShader——挺进体积光
- 《unityshader 入门精要》漫反射光照与高光反射光照模型学习笔记
- UnityShader入门精要学习笔记(六):Unity中实现高光反射模型
- unityshader中高光反射--像素光照
- UnityShader案例(八)——透明效果(1)
- UnityShader案例(九)——透明效果(2)
- UnityShader案例(四)——单张纹理
- UnityShader案例(五)——法线纹理
- UnityShader案例(六)——渐变纹理
- UnityShader案例(七)——遮罩纹理
- unity shader学习笔记(三)——在Unity中计算高光反射光照
- shader之——单光源,法线,高光,+天空盒环境反射(HDR)
- MD5加密
- epoll
- 动态网页
- Android 之 USB Host 串口编程
- viewpager+fragment+slidingmenu滑动卡顿解决方法
- UnityShader案例(二)——Phong高光反射
- shiro与spring的整合
- XL1常用开发命令详细记录
- Android开发中selector状态选择器常用属性解析
- $.ajax()方法详解
- idea下gradle构建的spring项目dao层xml不识别问题
- Python学习笔记(14)-安装第三方模块
- angular中的ng-container标签
- Codeforces 544C Writing Code【二维完全背包】