unity shader實(shí)現(xiàn)較完整光照效果
更新時(shí)間:2019年11月25日 11:41:04 作者:周者
這篇文章主要為大家詳細(xì)介紹了unity shader實(shí)現(xiàn)較完整光照效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了unity shader實(shí)現(xiàn)光照效果的具體代碼,供大家參考,具體內(nèi)容如下
效果圖:
shader被附給了球。
燈光需要在屬性面板開啟陰影。
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)' Shader "Unlit/lightFull" { Properties { _MainTex ("Texture", 2D) = "white" {} } SubShader { Tags { "RenderType"="Opaque" } LOD 100 Pass { Tags{"LightMode" = "ForwardBase"} CGPROGRAM #pragma vertex vert #pragma fragment frag // make fog work #pragma multi_compile_fwdbase #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" struct appdata { float4 vertex : POSITION; float2 uv : TEXCOORD0; float3 normal : NORMAL; }; struct v2f { float2 uv : TEXCOORD0; float4 pos : POSITION; float4 pos_world : TEXCOORD1; float3 normal:TEXCOORD2; SHADOW_COORDS(3) }; sampler2D _MainTex; float4 _MainTex_ST; v2f vert (appdata v) { v2f o; o.pos_world = mul(UNITY_MATRIX_M, v.vertex); o.normal = v.normal; o.pos = UnityObjectToClipPos(v.vertex); o.uv = TRANSFORM_TEX(v.uv, _MainTex); TRANSFER_SHADOW(o); return o; } fixed4 frag (v2f i) : SV_Target { // sample the texture fixed4 col = tex2D(_MainTex, i.uv); float4 lightColor = _LightColor0; float3 lightDir = WorldSpaceLightDir(i.pos_world); UNITY_LIGHT_ATTENUATION(atten, i, i.pos_world.xyz); return col * lightColor * saturate(dot(lightDir, i.normal)) * atten; } ENDCG } pass { Tags{"LightMode" = "ForwardAdd"} Blend One One CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdadd_fullshadows #include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc" struct v2f { float4 pos : POSITION; float4 vertex : TEXCOORD0; float3 normal : NORMAL; SHADOW_COORDS(2) }; v2f vert(appdata_full data) { v2f v; v.pos = UnityObjectToClipPos(data.vertex); v.vertex = mul(UNITY_MATRIX_M, data.vertex); v.normal = data.normal; TRANSFER_SHADOW(v); return v; } float4 frag(v2f v) :SV_Target { float3 lightColor = _LightColor0; #ifdef USING_DIRECTIONAL_LIGHT float3 lightDir = _WorldSpaceLightPos0; #else float3 lightDir = _WorldSpaceLightPos0 - v.vertex; #endif UNITY_LIGHT_ATTENUATION(atten, v, v.vertex.xyz); float3 color = lightColor * saturate(dot(lightDir, v.normal) * atten); return float4(color, 1); } ENDCG } } Fallback "Specular" }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
基于C#實(shí)現(xiàn)獲取Windows所有窗口句柄
在做錄屏或截屏操作時(shí),需要獲取當(dāng)前正在運(yùn)行中的桌面程序句柄,所以這篇文章主要為大家詳細(xì)介紹了如何使用C#實(shí)現(xiàn)獲取Windows所有窗口句柄,需要的可以參考下2023-12-12C#數(shù)值轉(zhuǎn)換-顯式數(shù)值轉(zhuǎn)換表(參考)
就是在將一種類型轉(zhuǎn)換成另外一種類型時(shí),需要額外的代碼來完成這種轉(zhuǎn)換。2013-04-04C#實(shí)現(xiàn)多線程的幾種方式小結(jié)
多線程是C#中一個(gè)重要的概念,多線程指的是在同一進(jìn)程中同時(shí)運(yùn)行多個(gè)線程的機(jī)制,多線程適用于需要提高系統(tǒng)并發(fā)性、吞吐量和響應(yīng)速度的場景,可以充分利用多核處理器和系統(tǒng)資源,提高應(yīng)用程序的性能和效率,本文介紹了C#實(shí)現(xiàn)多線程的幾種方式,需要的朋友可以參考下2024-07-07