Unity Shaders and Effects Cookbook (D-2) Cull Back背面剔除 -- 模型半边不可见

这两天从其它游戏里面拔一些资源来给美术做参考,碰到很多模型都是只有一半可见,另一半要转动视角才可见。



用的是最简单的Diffuse Shader:

Shader "Custom/MyDiffuse" 
{
	Properties 
	{
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200

		CGPROGRAM
		
		#pragma surface surf Standard

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input 
		{
			float2 uv_MainTex;
		};


		fixed4 _Color;

		void surf (Input IN, inout SurfaceOutputStandard o) 
		{
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

那这个问题,其实是背面剔除的导致的。

我们知道物体都有两面,一个立方体盒子,有外面和里面之分,对于我们游戏来说,玩家一般只能看到外面,里面其实是不需要的。

所以默认Unity会开启背面剔除,把背对摄像机的面都剔除掉。

转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn

三角面的正面朝向有两种定义方式:

1、顶点的绘制顺序

2、面法线的朝向


我不知道Unity是哪种方式。

但是我可以关闭背面剔除。


首先。

Unity 中的背面剔除有几种模式:

1、Cull Back 剔除背面,这是默认的方式

2、Cull Front 剔除正面

3、Cull Off 关闭剔除


默认是 Cull Back


使用 Cull Front 的话,修改Shader如下

Shader "Custom/MyDiffuse" 
{
	Properties 
	{
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		Cull Front  
		
		CGPROGRAM
		
		#pragma surface surf Standard

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input 
		{
			float2 uv_MainTex;
		};


		fixed4 _Color;

		void surf (Input IN, inout SurfaceOutputStandard o) 
		{
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

效果 转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn


剔除了正面,留下了背面,背面是不受光照的,所以会更暗。


关闭背面剔除,修改Shader 如下

Shader "Custom/MyDiffuse" 
{
	Properties 
	{
		_Color ("Color", Color) = (1,1,1,1)
		_MainTex ("Albedo (RGB)", 2D) = "white" {}
	}
	SubShader {
		Tags { "RenderType"="Opaque" }
		LOD 200
		Cull Off  
		
		CGPROGRAM
		
		#pragma surface surf Standard

		// Use shader model 3.0 target, to get nicer looking lighting
		#pragma target 3.0

		sampler2D _MainTex;

		struct Input 
		{
			float2 uv_MainTex;
		};


		fixed4 _Color;

		void surf (Input IN, inout SurfaceOutputStandard o) 
		{
			// Albedo comes from a texture tinted by color
			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
			o.Albedo = c.rgb;
			o.Alpha = c.a;
		}
		ENDCG
	}
	FallBack "Diffuse"
}

效果 转自http://blog.csdn.net/huutu http://www.thisisgame.com.cn



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值