图形学入门---反射向量与Cg reflect函数

1、添加点光源后,右侧的系统内嵌的小球被点光源照亮了,左侧的自定义的shader小球没有被照亮




2、辅助光照函数ShadeVertexLightsFull只能使用在pass通道的Vertext中,tags标签必须修改成下面的值


3、添加代码o.color.rgb = ShadeVertexLights(v.vertex,v.normal);

Shader "Custom/s_diffuse" {


	SubShader{
		pass {

		tags{"LightMode" = "Vertex"}

		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"
		#include "lighting.cginc"

		struct v2f {
			float4 pos:POSITION;
			fixed4 color : COLOR;
		};

		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			float3 N = normalize(v.normal);
			float3 L = normalize(_WorldSpaceLightPos0);
			
			N = mul(float4(N,0), _World2Object).xyz;
			N = normalize(N);
			float ndotl = saturate(dot(N, L));
			o.color = _LightColor0*ndotl;

			o.color.rgb = ShadeVertexLights(v.vertex,v.normal);
			return o;
		}


		fixed4 frag(v2f IN) :COLOR{
			//环境光照
			return IN.color+UNITY_LIGHTMODEL_AMBIENT;
		}
			ENDCG
	}
	}

}



点光源对物体起了作用


4、但是着色与左侧系统默认的不一样,使用Shade4PointLights函数


Shader "Custom/s_diffuse" {


	SubShader{
		pass {

		tags{"LightMode" = "ForwardBase"}

		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"
		#include "lighting.cginc"

		struct v2f {
			float4 pos:POSITION;
			fixed4 color : COLOR;
		};

		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			float3 N = normalize(v.normal);
			float3 L = normalize(_WorldSpaceLightPos0);
			
			N = mul(float4(N,0), _World2Object).xyz;
			N = normalize(N);
			float ndotl = saturate(dot(N, L));
			o.color = _LightColor0*ndotl;

			float3 wpos = mul(_Object2World,v.vertex).xyz;

			o.color.rgb = 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);
			return o;
		}


		fixed4 frag(v2f IN) :COLOR{
			//环境光照
			return IN.color+UNITY_LIGHTMODEL_AMBIENT;
		}
			ENDCG
	}
	}

}


球体已经被点光源照亮



5、但是平行光没有添加上去,因为只赋了rgb的颜色



改为+=就可以与平行光进行合并



Shader "Custom/s_diffuse" {


	SubShader{
		pass {

		tags{"LightMode" = "ForwardBase"}

		CGPROGRAM
		#pragma vertex vert
		#pragma fragment frag
		#include "unitycg.cginc"
		#include "lighting.cginc"

		struct v2f {
			float4 pos:POSITION;
			fixed4 color : COLOR;
		};

		v2f vert(appdata_base v) {
			v2f o;
			o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
			float3 N = normalize(v.normal);
			float3 L = normalize(_WorldSpaceLightPos0);
			
			N = mul(float4(N,0), _World2Object).xyz;
			N = normalize(N);
			float ndotl = saturate(dot(N, L));
			o.color = _LightColor0*ndotl;

			float3 wpos = mul(_Object2World,v.vertex).xyz;

			o.color.rgb += 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);
			return o;
		}


		fixed4 frag(v2f IN) :COLOR{
			//环境光照
			return IN.color+UNITY_LIGHTMODEL_AMBIENT;
		}
			ENDCG
	}
	}

}




  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值