Unity3D Shader官方教程翻译(十)----Shader语法:Pass的Alpha测试

ShaderLab syntax: Alpha testing

The alpha test is a last chance to reject a pixel from being written to the screen.

alpha测试是最后一个机会,拒绝将一个像素写到屏幕上。(可以理解为在将画面显示到屏幕上的最后1次修改,这次修改只能让该像素显示或不显示)

点击浏览下一页

After the final output color has been calculated, the color can optionally have its alpha value compared to a fixed value. If the test fails, the pixel is not written to the display.

在最后的输出颜色被计算之后,这个颜色可以选择是否让自己的alpha值和一个固定值做比较。如果测试失败,这个像素将不在屏幕上显示。

指导:当每个像素即将绘制时,如果启动了Alpha测试,OpenGL会检查像素的Alpha值,只有Alpha值满足条件的像素才会进行绘制(严格的说,满足条件的像素会通过本项测试,进行下一测试,只有所有测试都通过,才能进行绘制),不满足条件的则不进行绘制。这个“条件”可以是:始终通过(默认情况)、始终不通过、大于设定值则通过、小于设定值则通过、等于设定值则通过、大于等于设定值则通过、小于等于设定值则通过、不等于设定值则通过。

如果我们需要绘制一幅图片,而这幅图片的某些部分又是透明的(想象一下,你先绘制一幅相片,然后绘制一个相框,则相框这幅图片有很多地方都是透明的,这样就可以透过相框看到下面的照片),这时可以使用Alpha测试。将图片中所有需要透明的地方的Alpha值设置为0.0,不需要透明的地方Alpha值设置为1.0,然后设置Alpha测试的通过条件为:“大于0.5则通过”,这样便能达到目的。当然也可以设置需要透明的地方Alpha值为1.0,不需要透明的地方Alpha值设置为0.0,然后设置条件为“小于0.5则通过”。Alpha测试的设置方式往往不只一,可以根据个人喜好和实际情况需要进行选择。

Syntax 语法

AlphaTest Off
Render all pixels (default).
关闭alpha测试,渲染所有像素
 
AlphaTest  comparison AlphaValue
Set up the alpha test to only render pixels whose alpha value is within a certain range.
设置alpha测试:仅当像素的alpha值处在一定范围之内才渲染它

Comparison 比较命令

Comparison is one of the following words:

比较命令式下面的1个

GreaterOnly render pixels whose alpha is greater than AlphaValue.  只渲染alpha值比AlphaValue大的像素
GEqualOnly render pixels whose alpha is greater than or equal to AlphaValue. 只渲染alpha值>=AlphaValue的像素
LessOnly render pixels whose alpha value is less than AlphaValue. 只渲染alpha值<AlphaValue的像素
LEqualOnly render pixels whose alpha value is less than or equal to from AlphaValue. 只渲染alpha值<= AlphaValue的像素
EqualOnly render pixels whose alpha value equals AlphaValue. 只渲染alpha值=AlphaValue的像素
NotEqualOnly render pixels whose alpha value differs from AlphaValue. 只渲染alpha值不等于AlphaValue的像素
AlwaysRender all pixels. This is functionally equivalent to AlphaTest Off. 渲染所有像素相当于关闭alpha测试
NeverDon't render any pixels. 不渲染任何像素

AlphaValue  alpha值

A floating-point number between 0 and 1. This can also be a variable reference to a float or range property, in which case it should be written using the standard square bracket notation ([VariableName]).

alpha值是0~1之间的浮点数。这也可以是一个可以被引用的float变量或范围属性,在这情况下,它应该使用标准的方括号来表示([变量名])。

Details 详情

The alpha test is important when rendering concave objects with transparent parts. The graphics card maintains a record of the depth of every pixel written to the screen. If a new pixel is further away than one already rendered, the new pixel is not written to the display. This means that even with Blending, objects will not show through.

当渲染1个凹面物体的半透明部分的时候,alpha测试是很重要的。显卡会持续记录写入到屏幕上的每1个像素的深度值。如果1个新像素比1个已经渲染的像素在同一个屏幕上的位置更远时候,这个新的像素将不会被渲染。这意味着就算使用混合效果,这个物体仍然不会被显示。

点击浏览下一页

In this figure, the tree on the left is rendered using AlphaTest. Note how the pixels in it are either completely transparent or opaque. The center tree is rendered using only Alpha Blending - notice how transparent parts of nearby branches cover the distant leaves because of the depth buffer. The tree on the right is rendered using the last example shader - which implements a combination of blending and alpha testing to hide any artifacts.

在此图中,左边的树使用了alpha测试,请注意它的像素或者是全透明或者是不透明。中间的树仅仅使用了alpha混合,请注意临近树枝的透明部分由于深度缓冲的原因覆盖了远处的树叶。右边的树使用了最后1个例子的着色器,这个着色器实现了混合效果和alpha测试,隐藏了假象。

Examples 例子

The simplest possible example, assign a texture with an alpha channel to it. The object will only be visible where alpha is greater than 0.5

这是1个尽可能简单的例子,分配给它一个带有alpha通道的纹理。这个物体中仅有alpha值大于0.5的部分可见。

Shader "Simple Alpha Test" {
	Properties {
		_MainTex ("Base (RGB) Transparency (A)", 2D) = "" {}
	}
	SubShader {
		Pass {
			// Only render pixels with an alpha larger than 50%
			AlphaTest Greater 0.5
			SetTexture [_MainTex] { combine texture }
		}
	}
}

This is not much good by itself. Let us add some lighting and make the cutoff value tweakable:

这个效果不是很好,我们添加一些光照和一个变量给它,使它可以调整AlphaTest的值
Shader "Cutoff Alpha" {
	Properties {
		_MainTex ("Base (RGB) Transparency (A)", 2D) = "" {}
		_Cutoff ("Alpha cutoff", Range (0,1)) = 0.5
	}
	SubShader {
		Pass {
			// Use the Cutoff parameter defined above to determine
			// what to render.
			AlphaTest Greater [_Cutoff]
			Material {
				Diffuse (1,1,1,1)
				Ambient (1,1,1,1)
			}
			Lighting On
			SetTexture [_MainTex] { combine texture * primary }
		}
	}
} 

When rendering plants and trees, many games have the hard edges typical of alpha testing. A way around that is to render the object twice. In the first pass, we use alpha testing to only render pixels that are more than 50% opaque. In the second pass, we alpha-blend the graphic in the parts that were cut away, without recording the depth of the pixel. We might get a bit of confusion as further away branches overwrite the nearby ones, but in practice, that is hard to see as leaves have a lot of visual detail in them.

我们渲染了植物和树木,许多游戏具有典型alpha测试的硬边界。解决这个问题的办法是渲染这个物体2次。在第1个pass中,我们使用alpha测试仅仅去渲染那些半透明值大于50%的像素。在第2个pass中,我们使用alpha混合那些我们切掉的图形,不再记录这些像素的深度。远处的叶子覆盖掉了近处的叶子,在这里我们也许有些混乱,但在现实中,我们很难看到叶子的许多细节。

Shader "Vegetation" {
	Properties {
		_Color ("Main Color", Color) = (.5, .5, .5, .5)
		_MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
		_Cutoff ("Base Alpha cutoff", Range (0,.9)) = .5
	}
	SubShader {
		// Set up basic lighting
		Material {
			Diffuse [_Color]
			Ambient [_Color]
		}
		Lighting On
		// Render both front and back facing polygons.
		Cull Off
		// first pass:
		//   render any pixels that are more than [_Cutoff] opaque
		Pass {
			AlphaTest Greater [_Cutoff]
			SetTexture [_MainTex] {
				combine texture * primary, texture
			}
		}
		// Second pass:
		//   render in the semitransparent details.
		Pass {
			// Dont write to the depth buffer
			ZWrite off
			// Don't write pixels we have already written.
			ZTest Less
			// Only render pixels less or equal to the value
			AlphaTest LEqual [_Cutoff]
			// Set up alpha blending
			Blend SrcAlpha OneMinusSrcAlpha
			SetTexture [_MainTex] {
				combine texture * primary, texture
			}
		}
	}
} 

Note that we have some setup inside the SubShader, rather than in the individual passes. Any state set in the SubShader is inherited as defaults in passes inside it.

 注意我们在SubShader中做了一些设置,而不是在独立的pass中。所有在SubShader中做的设置将被当做其中pass的默认值。

www.J2meGame.com原创,转载请说明。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity Shader是一种用于在Unity引擎中创建和控制图形渲染效果的编程语言。通过使用Unity Shader,开发人员可以自定义游戏中各种物体的外观和行为,从而实现更加逼真和出色的视觉效果。 而热图(Heatmap)是一种用于显示某个区域内物体热度分布的视觉化工具。在游戏开发中,热图通常用于统计和分析玩家在游戏中的行为和偏好,以便开发人员可以根据这些数据进行游戏优化和改进。 为了创建一个热图效果,我们可以使用Unity Shader来实现。首先,我们需要将游戏中各个物体按照玩家与其的互动情况和频率进行区分,不同的行为和频率可以对应不同的颜色或者纹理。接着,我们可以在Shader中根据这些信息来着色和渲染物体,以展示物体的热度分布。 在Shader中,我们可以通过为物体添加一张热图纹理,并使用该纹理来表示物体的热度值。热图纹理可以是一张灰度图,不同的灰度值对应不同的热度。然后,我们可以使用纹理坐标和采样操作来获取每个像素对应的热度值,并根据这些值来着色和渲染物体。 除了使用纹理来表示热度分布,我们还可以使用其他的技术和效果来增强热图的可视化效果。例如,我们可以使用颜色渐变和透明度来形成平滑的过渡效果,以更好地显示物体的热度变化。我们还可以添加动画效果,使热图效果更加生动和有趣。 总结而言,Unity Shader可以用于创建热图效果,通过着色和渲染来展示物体的热度分布。这样的热图可以帮助开发人员分析游戏中玩家的行为和偏好,从而优化和改进游戏的设计和内容。这些热图效果能够增强游戏的可视化效果,并提供有价值的数据供开发人员参考。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值