u3d的u3dshaderr有tex2D的个数限制吗

3766人阅读
Unity(112)
这里写的这个方法并不会提高效率。看看过程就好了,千万别用到项目中。在上一篇中,我通过一个例子,将三个带有不同颜色 RGB的立方体,合并Mesh和材质到Character这一个GameObject中。这样原本对3个GameObject的操作只需要对Character这一个GameObject进行操作就好了。但是我们的任务还没有完成。合并之前的游戏:合并之后的游戏:大家注意看合并之前和合并之后,虽然GameObject数量减少了,但是DrawCall一个都没有减少哦!之前是4个,合并之后仍然是4个。简单的来说呢,就是一个材质球,一个DrawCall。也就是说呢,一个Shader,一个DrawCall。既然知道了一个Shader一个DrawCall,那我们就开始着手去处理,把红、绿、蓝这三张图片,在一个Shader中进行处理,只使用一个材质球,这样就只有1个DrawCall了。我们来创建一个Shader,就叫CombineShader吧,在默认的Shader代码基础上,删掉MainTex这个纹理,添加我们自己的三个纹理:_Red 、_Green 、_Blue .Shader &Custom/CombineShader& {
Properties {
_Red (&Base (RGB)&, 2D) = &white& {}
_Green (&Base (RGB)&, 2D) = &white& {}
_Blue (&Base (RGB)&, 2D) = &white& {}
SubShader {
Tags { &RenderType&=&Opaque& }
#pragma surface surf Lambert
sampler2D _R
sampler2D _G
sampler2D _B
struct Input {
float2 uv_RedT
float2 uv_GreenT
float2 uv_BlueT
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_Red, IN.uv_RedTex);
o.Albedo = c.
o.Alpha = c.a;
FallBack &Diffuse&
在上面的未完成的Shader中,我取了_Red 的纹理来做取样。我们接着修改脚本代码,使合并之后的GameObject Character使用CombineShader创建的材质。using UnityE
using System.C
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
//获取纹理;
Texture redTex=transform.Find(&CubeRed&).GetComponent&MeshRenderer&().sharedMaterial.mainT
Texture greenTex=transform.Find(&CubeGreen&).GetComponent&MeshRenderer&().sharedMaterial.mainT
Texture blueTex=transform.Find(&CubeBlue&).GetComponent&MeshRenderer&().sharedMaterial.mainT
//合并材质;
Shader combineShader = Shader.Find(&Custom/CombineShader&);
Material combineMaterial = new Material(combineShader);
combineMaterial.SetTexture(&_Red&, redTex);
combineMaterial.SetTexture(&_Green&, greenTex);
combineMaterial.SetTexture(&_Blue&, blueTex);
MeshFilter[] meshFilters = GetComponentsInChildren&MeshFilter&();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
for (int i = 0; i & meshFilters.Li++ )
combine[i].mesh = meshFilters[i].sharedM
combine[i].transform = meshFilters[i].transform.localToWorldM
meshFilters[i].gameObject.SetActive(false);
transform.gameObject.AddComponent&MeshRenderer&();
transform.gameObject.AddComponent&MeshFilter&();
transform.GetComponent&MeshFilter&().mesh = new Mesh();
transform.GetComponent&MeshFilter&().bineMeshes(combine, false);
transform.gameObject.SetActive(true);
//设置材质;
transform.GetComponent&MeshRenderer&().sharedMaterial = combineM
// Update is called once per frame
void Update () {
运行之后能看到,在Character这个GameObject使用的材质球中,需要输入三张纹理图片。现在再看,DrawCall数量已经降到2了,也就是说,合并之后 由原来的3个DrawCall 降到了 1个DrawCall。但是还是有问题呢,为什么只显示一个立方体,哈哈,是我们代码写错了。transform.GetComponent&MeshFilter&().bineMeshes(combine, false);应该改为transform.GetComponent&MeshFilter&().bineMeshes(combine, true);好了,这次可以显示了,但是为什么没有贴图?为什么没有贴图?因为我们只是在Unity中设置了贴图,但是在Shader中还没有去使用它们。将Shader修改如下:Shader &Custom/CombineShader& {
Properties {
_Red (&Base (RGB)&, 2D) = &white& {}
_Green (&Base (RGB)&, 2D) = &white& {}
_Blue (&Base (RGB)&, 2D) = &white& {}
SubShader {
Tags { &RenderType&=&Opaque& }
#pragma surface surf Lambert
sampler2D _R
sampler2D _G
sampler2D _B
struct Input {
float2 uv_RedT
float2 uv_GreenT
float2 uv_BlueT
float4 color:COLOR;
void surf (Input IN, inout SurfaceOutput o)
half4 colorIn;
if(IN.color.a&0.33)
colorIn=tex2D(_Red,IN.uv_RedTex);
else if(IN.color.a&0.6)
colorIn=tex2D(_Green,IN.uv_GreenTex);
colorIn=tex2D(_Blue,IN.uv_BlueTex);
o.Albedo=colorIn.
o.Alpha=colorIn.a;
FallBack &Diffuse&
我们看到在surf 中对顶点颜色的Alpha值进行了判断处理,这是利用顶点色Color的属性,在代码中进行赋值,来区分当前顶点原来是属于哪一个立方体的。比如说color.a是0,那么原来就属于红色立方体,就给它从红色纹理来取样。using UnityE
using System.C
using System.Collections.G
public class NewBehaviourScript : MonoBehaviour {
// Use this for initialization
void Start () {
//获取纹理;
Texture redTex=transform.Find(&CubeRed&).GetComponent&MeshRenderer&().sharedMaterial.mainT
Texture greenTex=transform.Find(&CubeGreen&).GetComponent&MeshRenderer&().sharedMaterial.mainT
Texture blueTex=transform.Find(&CubeBlue&).GetComponent&MeshRenderer&().sharedMaterial.mainT
//合并材质;
Shader combineShader = Shader.Find(&Custom/CombineShader&);
Material combineMaterial = new Material(combineShader);
combineMaterial.SetTexture(&_Red&, redTex);
combineMaterial.SetTexture(&_Green&, greenTex);
combineMaterial.SetTexture(&_Blue&, blueTex);
MeshFilter[] meshFilters = GetComponentsInChildren&MeshFilter&();
CombineInstance[] combine = new CombineInstance[meshFilters.Length];
List&Color& combineMeshColor=new List&Color&(); //Combine之后Mesh的C
for (int i = 0; i & meshFilters.Li++ )
combine[i].mesh = meshFilters[i].sharedM
//处理顶点位置;
combine[i].transform = meshFilters[i].transform.localToWorldM
//处理顶点颜色;
Vector2[] UVArray = meshFilters[i].sharedMesh.
float bodyPart = i / (float)meshFilters.L
for (int uvindex = 0; uvindex & UVArray.L uvindex++)
combineMeshColor.Add(new Color(bodyPart, bodyPart, bodyPart, bodyPart));
meshFilters[i].gameObject.SetActive(false);
Mesh combineMesh = new Mesh();
bineMeshes(combine, true);
combineMesh.colors = combineMeshColor.ToArray();
combineMesh.name = gameObject.
transform.gameObject.AddComponent&MeshRenderer&();
transform.gameObject.AddComponent&MeshFilter&();
transform.gameObject.GetComponent&MeshFilter&().sharedMesh = combineM
//设置材质;
transform.GetComponent&MeshRenderer&().material = combineM
transform.gameObject.SetActive(true);
// Update is called once per frame
void Update () {
最后看运行结果,DrawCall减少到2 ,Character也完整的显示出来了。工程示例下载:/s/1o6ytCoU
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1235862次
积分:15832
积分:15832
排名:第759名
原创:331篇
转载:108篇
评论:282条
文章:32篇
阅读:114012
(4)(1)(1)(2)(1)(1)(5)(1)(3)(3)(3)(5)(6)(4)(7)(1)(18)(18)(17)(13)(9)(24)(25)(2)(7)(5)(9)(17)(11)(7)(25)(8)(9)(1)(4)(12)(6)(2)(3)(16)(21)(10)(14)(1)(7)(25)(11)(6)(13)(4)(8)(7)(2)Unity3D(82)
U3d Shader
unity: 5.6~2017
要比ugui中的mask组件的效果好很多
Shader "Custom/CircleMask" {
Properties {
_MainTex ("MainTex", 2D) = "white" {}
_MaskTex ("MaskTex", 2D) = "white" {}
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
SubShader {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
ColorMask [_ColorMask]
Name "FORWARD"
"LightMode"="ForwardBase"
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _MainT uniform float4 _MainTex_ST;
uniform sampler2D _MaskT uniform float4 _MaskTex_ST;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.pos = UnityObjectToClipPos( v.vertex );
float4 frag(VertexOutput i) : COLOR {
float4 _MainTex_var = tex2D(_MainTex,TRANSFORM_TEX(i.uv0, _MainTex));
float3 finalColor = _MainTex_var.
float4 _MaskTex_var = tex2D(_MaskTex,TRANSFORM_TEX(i.uv0, _MaskTex));
return fixed4(finalColor,_MaskTex_var.a);
FallBack "Diffuse"
&&相关文章推荐
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:371306次
积分:3487
积分:3487
排名:第10635名
原创:69篇
转载:125篇
评论:91条
(2)(1)(4)(2)(1)(7)(2)(1)(4)(8)(3)(2)(1)(1)(4)(3)(1)(2)(1)(4)(5)(19)(3)(7)(12)(3)(6)(14)(3)(6)(3)(6)(29)(25)(1)(2)
(window.slotbydup = window.slotbydup || []).push({
id: '4740887',
container: s,
size: '250,250',
display: 'inlay-fix'Here are some examples of . The examples below focus on using built- examples on how to implement custom lighting models are in .
We’ll start with a very simple Shader and build up on that. Here’s a Shader that sets the surface color to “white”. It uses the built-in Lambert (diffuse) lighting model.
Shader &Example/Diffuse Simple& {
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float4 color : COLOR;
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = 1;
Fallback &Diffuse&
Here’s how it looks like on a model with two
An all-white object is quite boring, so let’s add a Texture. We’ll add a
block to the Shader, so we get a Texture selector in our Material. Other changes are in bold below.
Shader &Example/Diffuse Texture& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
Fallback &Diffuse&
Normal mapping
Let’s add some normal mapping:
Shader &Example/Diffuse Bump& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_BumpMap (&Bumpmap&, 2D) = &bump& {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
sampler2D _MainT
sampler2D _BumpM
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
Fallback &Diffuse&
Rim Lighting
Now, try to add some Rim Lighting to highlight the edges of a GameObject. We’ll add some emissive light based on the angle between surface normal and view direction. For that, we’ll use the built-in viewDir Surface Shader variable.
Shader &Example/Rim& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_BumpMap (&Bumpmap&, 2D) = &bump& {}
_RimColor (&Rim Color&, Color) = (0.26,0.19,0.16,0.0)
_RimPower (&Rim Power&, Range(0.5,8.0)) = 3.0
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
float3 viewD
sampler2D _MainT
sampler2D _BumpM
float4 _RimC
float _RimP
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
half rim = 1.0 - saturate(dot (normalize(IN.viewDir), o.Normal));
o.Emission = _RimColor.rgb * pow (rim, _RimPower);
Fallback &Diffuse&
Detail Texture
For a different effect, let’s add a Detail Texture that is combined with the base Texture. A Detail Texture usually uses the same UVs but different Tiling in the Material, so we need to use different input UV coordinates.
Shader &Example/Detail& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_BumpMap (&Bumpmap&, 2D) = &bump& {}
_Detail (&Detail&, 2D) = &gray& {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
float2 uv_D
sampler2D _MainT
sampler2D _BumpM
sampler2D _D
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Albedo *= tex2D (_Detail, IN.uv_Detail).rgb * 2;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
Fallback &Diffuse&
Using a Texture checker does not always make much practical sense, but in this example it is used to illustrate what happens:
Detail Texture in Screen Space
A Detail Texture in screen space does not make practical sense for a soldier head model, but here it is used to illustrate how a built-in screenPos input might be used:
Shader &Example/ScreenPos& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_Detail (&Detail&, 2D) = &gray& {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float4 screenP
sampler2D _MainT
sampler2D _D
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
float2 screenUV = IN.screenPos.xy / IN.screenPos.w;
screenUV *= float2(8,6);
o.Albedo *= tex2D (_Detail, screenUV).rgb * 2;
Fallback &Diffuse&
The normal mapping has been removed from the Shader above, just to make it shorter:
Cubemap Reflection
Here’s a Shader that does cubemapped reflection using built-in worldRefl input. It’s very similar to built-in Reflective/Diffuse Shader:
Shader &Example/WorldRefl& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_Cube (&Cubemap&, CUBE) = && {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float3 worldR
sampler2D _MainT
samplerCUBE _C
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;
o.Emission = texCUBE (_Cube, IN.worldRefl).
Fallback &Diffuse&
Because it assigns the reflection color as Emission, we get a very shiny soldier:
If you want to do reflections that are affected by normal maps, it needs to be slightly more involved: INTERNAL_DATA needs to be added to the Input structure, and WorldReflectionVector function used to compute per-pixel reflection vector after you’ve written the Normal output.
Shader &Example/WorldRefl Normalmap& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_BumpMap (&Bumpmap&, 2D) = &bump& {}
_Cube (&Cubemap&, CUBE) = && {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
float3 worldR
INTERNAL_DATA
sampler2D _MainT
sampler2D _BumpM
samplerCUBE _C
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb * 0.5;
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
o.Emission = texCUBE (_Cube, WorldReflectionVector (IN, o.Normal)).
Fallback &Diffuse&
Here’s a normal-mapped shiny soldier:
Slices via World Space Position
Here’s a Shader that “slices” the GameObject by discarding pixels in nearly horizontal rings. It does this by using the clip() Cg/HLSL function based on the world position of a pixel. We’ll use the built-in worldPos Surface Shader variable.
Shader &Example/Slices& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_BumpMap (&Bumpmap&, 2D) = &bump& {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert
struct Input {
float2 uv_MainT
float2 uv_BumpM
float3 worldP
sampler2D _MainT
sampler2D _BumpM
void surf (Input IN, inout SurfaceOutput o) {
clip (frac((IN.worldPos.y+IN.worldPos.z*0.1) * 5) - 0.5);
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Normal = UnpackNormal (tex2D (_BumpMap, IN.uv_BumpMap));
Fallback &Diffuse&
Normal Extrusion with Vertex Modifier
It is possible to use a “vertex modifier” function that will modify the incoming vertex data in the vertex Shader. This can be used for things like procedural animation and extrusion along normals. Surface Shader compilation directive vertex:functionName is used for that, with a function that takes inout appdata_full parameter.
Here’s a Shader that moves vertices along their normals by the amount specified in the Material:
Shader &Example/Normal Extrusion& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_Amount (&Extrusion Amount&, Range(-1,1)) = 0.5
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainT
void vert (inout appdata_full v) {
v.vertex.xyz += v.normal * _A
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
Fallback &Diffuse&
Moving vertices along their normals makes a fat soldier:
Custom data computed per-vertex
Using a vertex modifier function, it is also possible to compute custom data in a vertex Shader, which then will be passed to the Surface Shader function per-pixel. The same compilation directive vertex:functionName is used, but the function should take two parameters: inout appdata_full and out Input. You can fill in any Input member that is not a built-in value there.
Note: Custom Input members used in this way must not have names beginning with ‘uv’ or they won’t work properly.
The example below defines a custom float3 customColor member, which is computed in a vertex function:
Shader &Example/Custom Vertex Data& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert vertex:vert
struct Input {
float2 uv_MainT
float3 customC
void vert (inout appdata_full v, out Input o) {
UNITY_INITIALIZE_OUTPUT(Input,o);
o.customColor = abs(v.normal);
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
o.Albedo *= IN.customC
Fallback &Diffuse&
In this example customColor is set to the absolute value of the normal:
More practical uses could be computing any per-vertex data that is not provided by built-in I or optimizing Shader computations. For example, it’s possible to compute Rim lighting at the GameObject’s vertices, instead of doing that in the Surface Shader per-pixel.
Final Color Modifier
It is possible to use a “final color modifier” function that will modify the final color computed by the Shader.The Surface Shader compilation directive finalcolor:functionName is used for this, with a function that takes Input IN, SurfaceOutput o, inout fixed4 color parameters.
Here’s a simple Shader that applies tint to the final color. This is different from just applying tint to the surface Albedo color: this tint will also affect any color that comes from Lightmaps, Light Probes and similar extra sources.
Shader &Example/Tint Final Color& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_ColorTint (&Tint&, Color) = (1.0, 0.6, 0.6, 1.0)
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert finalcolor:mycolor
struct Input {
float2 uv_MainT
fixed4 _ColorT
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
color *= _ColorT
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
Fallback &Diffuse&
Custom Fog with Final Color Modifier
A common case for using final color modifier (see above) would be implementing completely custom Fog in forward rendering. Fog needs to affect the final computed pixel Shader color, which is exactly what the finalcolor modifier does.
Here’s a Shader that applies fog tint based on the distance from screen center. This combines the vertex modifier with the custom vertex data (fog) and the final color modifier. When used in the forward rendering additive pass, the Fog needs to fade to black. This example handles that and performs a check for UNITY_PASS_FORWARDADD.
Shader &Example/Fog via Final Color& {
Properties {
_MainTex (&Texture&, 2D) = &white& {}
_FogColor (&Fog Color&, Color) = (0.3, 0.4, 0.7, 1.0)
SubShader {
Tags { &RenderType& = &Opaque& }
#pragma surface surf Lambert finalcolor:mycolor vertex:myvert
struct Input {
float2 uv_MainT
void myvert (inout appdata_full v, out Input data)
UNITY_INITIALIZE_OUTPUT(Input,data);
float4 hpos = UnityObjectToClipPos(v.vertex);
hpos.xy/=hpos.w;
data.fog = min (1, dot (hpos.xy, hpos.xy)*0.5);
fixed4 _FogC
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
fixed3 fogColor = _FogColor.
#ifdef UNITY_PASS_FORWARDADD
fogColor = 0;
color.rgb = lerp (color.rgb, fogColor, IN.fog);
sampler2D _MainT
void surf (Input IN, inout SurfaceOutput o) {
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).
Fallback &Diffuse&
Linear Fog
Shader &Example/Linear Fog& {
Properties {
_MainTex (&Base (RGB)&, 2D) = &white& {}
SubShader {
Tags { &RenderType&=&Opaque& }
#pragma surface surf Lambert finalcolor:mycolor vertex:myvert
#pragma multi_compile_fog
sampler2D _MainT
uniform half4 unity_FogS
uniform half4 unity_FogE
struct Input {
float2 uv_MainT
void myvert (inout appdata_full v, out Input data) {
UNITY_INITIALIZE_OUTPUT(Input,data);
float pos = length(UnityObjectToViewPos(v.vertex).xyz);
float diff = unity_FogEnd.x - unity_FogStart.x;
float invDiff = 1.0f /
data.fog = clamp ((unity_FogEnd.x - pos) * invDiff, 0.0, 1.0);
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color) {
#ifdef UNITY_PASS_FORWARDADD
UNITY_APPLY_FOG_COLOR(IN.fog, color, float4(0,0,0,0));
UNITY_APPLY_FOG_COLOR(IN.fog, color, unity_FogColor);
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.
o.Alpha = c.a;
FallBack &Diffuse&
Decals are commonly used to add details to Materials at run time (for example, bullet impacts). They are especially useful in deferred rendering, because they alter the GBuffer before it is lit, therefore saving on performance.
In a typical scenario, Decals should be rendered after the opaque objects and should not be shadow casters, as seen in the ShaderLab “Tags” in the example below.
Shader &Example/Decal& {
Properties {
_MainTex (&Base (RGB)&, 2D) = &white& {}
SubShader {
Tags { &RenderType&=&Opaque& &Queue&=&Geometry+1& &ForceNoShadowCasting&=&True& }
Offset -1, -1
#pragma surface surf Lambert decal:blend
sampler2D _MainT
struct Input {
float2 uv_MainT
void surf (Input IN, inout SurfaceOutput o) {
half4 c = tex2D (_MainTex, IN.uv_MainTex);
o.Albedo = c.
o.Alpha = c.a;
Did you find this page useful? Please give it a rating:
Writing Surface Shaders
Custom lighting models in Surface Shaders}

我要回帖

更多关于 u3d 带clip的shader 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信