[Unity]用同一份material在不同mesh上绘制不同图案

假设游戏中有256个相同材质的物体上要分别绘制数字1~256,需要占256个drawcall吗?可以batch到1个drawcall吗?

简化一下问题:假如有4个Plane,我们要用下面这张材质分别在它们上面画1,2,3,4四个数字,可以做到只消耗一个drawcall吗?当然可以,关键就是使用顶点色保存uv信息.因为我们不能把uv信息保存在material中或者运行时给它赋值(会破坏batching),(关于划掉的部分,运行时也可以赋值而不破坏batching,只不过需要一些步骤,参考文末的gpu instancing的文章)所以我们要把信息存到模型本身上.

给每个模型挂一个脚本TestMeshColor.cs来设置它的顶点色(让美术自己出256个不同模型怕是美术要弄死你,包容量也不允许)代码如下:

using UnityEngine;
public class TestMeshColor : MonoBehaviour
{
	private Mesh mesh;
	public Color color;
	void Start ()
	{
		mesh = GetComponent<MeshFilter>().mesh;
		Color[] colors = new Color[mesh.vertexCount];
		for (int i = 0; i < colors.Length; i++)
			colors[i] = color;
		mesh.colors = colors;
	}
}

 

然后在Inspector里输入public color的值(当然也可以用代码赋值).顶点色rgba四个通道,正好可以存Tiling x,y和Offset x,y,这里我们只用r和g通道存Offset,分别取(0,0),(0,1),(1,0),(1,1).

Tiling直接写在Material里(因为4个Plane的Tiling是一样的).

 

然后在Plane的shader中读取顶点色并转换为相应的uv:

Shader "Custom/TestMeshColor"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		Cull Off
		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			
			#include "UnityCG.cginc"

			struct v2f
			{
				float4 vertex : SV_POSITION;
				float2 uv : TEXCOORD0;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;

			v2f vert (appdata_full v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
				o.uv += float2(v.color.x * 256 / 2, v.color.y * 256 / 2);//这里v.color要乘以256还原回来
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				fixed4 col = tex2D(_MainTex, i.uv);
				return col;
			}
			ENDCG
		}
	}
}

结果如下(要运行起来哈不然没刷顶点色你会发现全是3),注意背景占掉了一个drawcall所以drawcall是2.有人问为什么顺序不是1,2,3,4啊,因为Offset的y是下方0上方1.这个我就不细调了反正知道意思就行了.

如果模型本身已经有顶点色了,可以尝试用MeshRenderer.additionalVertexStream再加一套UV来存信息,不过我没有试过,是老外一个帖子中提到的:https://forum.unity.com/threads/how-to-use-different-property-in-one-materials-shader.438588/

补充:

其实还有更简单的办法,用gpu instancing,传送门:

https://catlikecoding.com/unity/tutorials/rendering/part-19/

 

其他参考链接:

1.https://answers.unity.com/questions/1258951/changing-vertex-colors-break-batching.html

2.https://forum.unity.com/threads/tricks-for-dynamically-batching-materials-that-need-to-change-color.448821/

 

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值