shader 前向渲染(Splot光渲染)

1 主要用于渲染筒灯和点光源

Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0, unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,unity_4LightAtten0 , wpos, N);

2 代码,未开启Shade4PointLights

Shader "Unlit/TestPlot"
{
   Properties {
		_Spec ("Spec", Color) = (1,1,1,1)  //高光颜色
		_Shin ("Shin", range(1,32)) = 2      //高光强度系数
	}
    
	SubShader {
		pass {
			tags{ "lightmode" = "ForwardBase" }
            Blend one one
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "unitycg.cginc"
			#include "lighting.cginc"
			fixed4 _Spec;
			float _Shin;
			struct v2f{
				float4 pos:POSITION;
				float3 normal:NORMAL;
				float4 vertex:TEXCOORD2;
			};
			v2f vert(appdata_base v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.normal = normalize(v.normal);
				o.vertex = v.vertex;
				return o;
			}
			fixed4 frag(v2f IN):COLOR
			{
				float3 wpos = mul(unity_ObjectToWorld, IN.vertex).xyz;  //计算世界坐标系空间中的物体坐标(三维向量)
				//diffuse 漫反射
				float3 N = UnityObjectToWorldNormal(IN.normal);     //计算世界坐标空间中的法线向量
				float3 L = normalize(_WorldSpaceLightPos0).xyz;    //计算世界坐标空间中平行光向量
				float ndotl = saturate(dot(N, L)) * 0.5 + 0.5;                                    //点积得平行光颜色系数
				fixed4 col = _LightColor0*ndotl;                                   //平行光颜色*系数得颜色
				//specular  高光
				float3 V = normalize(WorldSpaceViewDir(IN.vertex));    //计算世界坐标空间中的视向量
				float3 R = 2 * dot(N, L)*N - L;	//phong                                //反射向量
				float3 H = normalize(V + L);	//blinnphong                         //半角向量:点到光源+点到摄像的单位向量,平均值
				float specScale = pow(saturate(dot(R, V)), _Shin);	//phong
				specScale = pow(saturate(dot(H, N)), _Shin);		//blinnphong
				col += _Spec*specScale;                                       //颜色+高光*高光系数
				
				//pointlight  接收点光源
				//Shade4PointLights来自unitycg.cginc
				//其中用的参数前七个unity_4LightPosX0~unity4LightAtten0来自UnityShaderVariables.cginc,内建不需引用
				// float3 pL = Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
				// 	unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
				// 	unity_4LightAtten0,
				// 	wpos, N);
				//col.rgb += pL ;     //颜色+点光源反光
				return col;
			}
			ENDCG
		}
    }
}

效果:没有接受到Splot Light光照

2  代码,开启Shade4PointLights

Shader "Unlit/TestPlot"
{
   Properties {
		_Spec ("Spec", Color) = (1,1,1,1)  //高光颜色
		_Shin ("Shin", range(1,32)) = 2      //高光强度系数
	}
    
	SubShader {
		pass {
			tags{ "lightmode" = "ForwardBase" }
            Blend one one
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			#include "unitycg.cginc"
			#include "lighting.cginc"
			fixed4 _Spec;
			float _Shin;
			struct v2f{
				float4 pos:POSITION;
				float3 normal:NORMAL;
				float4 vertex:TEXCOORD2;
			};
			v2f vert(appdata_base v)
			{
				v2f o;
				o.pos = UnityObjectToClipPos(v.vertex);
				o.normal = normalize(v.normal);
				o.vertex = v.vertex;
				return o;
			}
			fixed4 frag(v2f IN):COLOR
			{
				float3 wpos = mul(unity_ObjectToWorld, IN.vertex).xyz;  //计算世界坐标系空间中的物体坐标(三维向量)
				//diffuse 漫反射
				float3 N = UnityObjectToWorldNormal(IN.normal);     //计算世界坐标空间中的法线向量
				float3 L = normalize(_WorldSpaceLightPos0).xyz;    //计算世界坐标空间中平行光向量
				float ndotl = saturate(dot(N, L)) * 0.5 + 0.5;                                    //点积得平行光颜色系数
				fixed4 col = _LightColor0*ndotl;                                   //平行光颜色*系数得颜色
				//specular  高光
				float3 V = normalize(WorldSpaceViewDir(IN.vertex));    //计算世界坐标空间中的视向量
				float3 R = 2 * dot(N, L)*N - L;	//phong                                //反射向量
				float3 H = normalize(V + L);	//blinnphong                         //半角向量:点到光源+点到摄像的单位向量,平均值
				float specScale = pow(saturate(dot(R, V)), _Shin);	//phong
				specScale = pow(saturate(dot(H, N)), _Shin);		//blinnphong
				col += _Spec*specScale;                                       //颜色+高光*高光系数
				
				//pointlight  接收点光源
				//Shade4PointLights来自unitycg.cginc
				//其中用的参数前七个unity_4LightPosX0~unity4LightAtten0来自UnityShaderVariables.cginc,内建不需引用
				float3 pL = Shade4PointLights(unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
					unity_LightColor[0].rgb, unity_LightColor[1].rgb, unity_LightColor[2].rgb, unity_LightColor[3].rgb,
					unity_4LightAtten0,
					wpos, N);
				col.rgb += pL ;     //颜色+点光源反光
				return col;
			}
			ENDCG
		}
    }
}

效果:接收到Splot Light光照

 

export default function canvasBg(){ window.requestAnimFrame = (function() { return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { window.setTimeout(callback, 1000 / 60); }; })(); var c = document.getElementById('canvas'); var $ = c.getContext('2d'); var w = c.width = window.innerWidth; var h = c.height = window.innerHeight; var _w = w * 0.5; var _h = h * 0.5; var arr = []; var cnt = 0; window.addEventListener('load', resize); window.addEventListener('resize', resize, false); function resize() { c.width = w = window.innerWidth; c.height = h = window.innerHeight; c.style.position = 'absolute'; c.style.left = (window.innerWidth - w) * .01 + 'px'; c.style.top = (window.innerHeight - h) * .01 + 'px'; } function anim() { cnt++; if (cnt % 6) draw(); window.requestAnimFrame(anim); } anim(); function draw() { var splot = { x: rng(_w - 900, _w + 900), y: rng(_h - 900, _h + 900), r: rng(20, 80), spX: rng(-1, 1), spY: rng(-1, 1) }; arr.push(splot); while (arr.length > 100) { arr.shift(); } $.clearRect(0, 0, w, h); for (var i = 0; i < arr.length; i++) { splot = arr[i];; $.fillStyle = rndCol(); $.beginPath(); $.arc(splot.x, splot.y, splot.r, 0, Math.PI * 2, true); $.shadowBlur = 80; $.shadowOffsetX = 2; $.shadowOffsetY = 2; $.shadowColor = rndCol(); $.globalCompositeOperation = 'lighter'; $.fill(); splot.x = splot.x + splot.spX; splot.y = splot.y + splot.spY; splot.r = splot.r * 0.96; } } function rndCol() { var r = Math.floor(Math.random() * 180); var g = Math.floor(Math.random() * 60); var b = Math.floor(Math.random() * 100); return "rgb(" + r + "," + g + "," + b + ")"; } function rng(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } }
最新发布
06-12
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值