[unity]GPU Instance学习

前言
我们之前研究过为什么Unity的UI可以合批,是因为使用了相同的材质进行渲染,UI上不同图片渲染是通过把图片打成一张图集后,使用Image组件对顶点填充了不同的UV值实现的。
那么有没有什么办法可以让3D的物体也像UI一样,使用相同材质,但是可以表现出不一样的样子呢(比如颜色/位置等)?
我们知道unity有两种传统的批处理的方式:静态批处理,动态批处理。其中动态批处理可以实现让物体使用相同的材质,拥有不同的位置信息。但是动态批处理的局限性很高(顶点数限制,PASS数限制等)。
Unity在5.4及以后的版本支持了一种新的批处理方式:GPU Instancing。通过这种方式,我们可以给同一材质传递一些不同的信息,进而渲染出不同的效果。
GPU Instancing官方文档


1.使用Unity的Standard材质
首先,为了避免动态批处理影响我们观察GPU Instance的结果,我们要先把动态批处理关掉(在Build Setting中的Player Setting中):
关闭动态批处理
然后我们在unity中新建一个材质球,把面板上的GPU Instancing选项勾上,新建几个cube得到的结果是这样的:
16个cube的GPUInstancing



WTF?16个cube居然有67个Batches?这哪里优化了,分明是负优化好吧。。。
这里我们读一下文档,文档中介绍说我们需要修改一下我们的shader以支持GPUInstancing


#pragma multi_compile_instancing
struct appdata
{
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
因为我使用的是旧版本的unity,看了一下stanard材质中并没有相关的宏定义,以下是我使用的standard材质的shader:


#pragma shader_feature _NORMALMAP
#pragma shader_feature _ _ALPHATEST_ON _ALPHABLEND_ON _ALPHAPREMULTIPLY_ON
#pragma shader_feature _EMISSION
#pragma shader_feature _METALLICGLOSSMAP 
#pragma shader_feature ___ _DETAIL_MULX2
#pragma shader_feature _PARALLAXMAP

#pragma multi_compile_f***ase
#pragma multi_compile_fog

#pragma vertex vertForwardBase
#pragma fragment fragForwardBase

#include "UnityStandardCore.cginc"
ENDCG
2.使用文档提供的demo进行实验
文档中提供了一个shader和一个脚本作为例子,我们就用我们之前的cube们进行实验。






使用了demo中的shader后


在使用了文档中提供的例子后,cube们真的可以通过一个DC绘制出来了。






再通过脚本中对颜色的控制,实现了一个DC中绘制不同颜色的相同材质





shader代码:


Shader "SimplestInstancedShader"
{
Properties
{
_Color("Color", Color) = (1, 1, 1, 1)
}

SubShader
{
Tags{ "RenderType" = "Opaque" }
LOD 100

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_instancing
#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct v2f
{
float4 vertex : SV_POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID // necessary only if you want to access instanced properties in fragment Shader.
};

UNITY_INSTANCING_BUFFER_START(Props)
UNITY_DEFINE_INSTANCED_PROP(float4, _Color)
UNITY_INSTANCING_BUFFER_END(Props)

v2f vert(appdata v)
{
v2f o;

UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o); // necessary only if you want to access instanced properties in the fragment Shader.

o.vertex = UnityObjectToClipPos(v.vertex);
return o;
}

fixed4 frag(v2f i) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(i); // necessary only if any instanced properties are going to be accessed in the fragment Shader.
return UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
}
ENDCG
}
}
}

C#中,使用同一个Block进行存储不同的颜色值,给相同的材质赋予同一个Block,才能进行批处理,其中除了可以存color类型,还可以存float,texture,Matrix等类型,以实现不同的需求。代码:


MaterialPropertyBlock props = new MaterialPropertyBlock();
MeshRenderer renderer;

foreach (GameObject obj in objects)
{
float r = Random.Range(0.0f, 1.0f);
float g = Random.Range(0.0f, 1.0f);
float b = Random.Range(0.0f, 1.0f);
props.SetColor("_Color", new Color(r, g, b));

renderer = obj.GetComponent<MeshRenderer>();
renderer.SetPropertyBlock(props);
}
更多unity2018的功能介绍请到paws3d学习中心查找。

转载于:https://www.cnblogs.com/jieke666/p/10815284.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BETA RELEASE Download Demo Build (Windows PC)​ Crowd Animations is an extension for GPU Instancer, and requires it to work. Support Forum | Documentation | F.A.Q. Crowd Animations is an out of the box solution for using massive amounts of animated characters in your scenes with high performance. CA uses the GPU Instancer core and adds GPU skinning techniques on top of GPUI’s indirect instancing solution and GPU culling techniques. This results in a combination which will allow you to get the most out of GPU Instancing in Unity for your animated characters. ——————————— FEATURES ——————————— – Indirect GPU instancing with skinned meshes. – GPU frustum, occlusion and distance culling. – VR compatible. Works with both single pass and multipass rendering modes. – Supports Standard, LW, HD and Universal Render Pipelines. – Custom shader support (requires manual set-up). – Animation Blending (up to 4 animations). – Multiple skinned mesh renderers and submeshes support. – LOD Groups support (all LOD meshes must use the same rig). – Bone attachments. – Root Motion support. – Ability to use custom shadow distance per prototype and to choose the LOD to render shadows with. – Automatic detection of added and removed instances without any additional code. – Rigidbody and physics support. – Custom animation event system. – Easy to use interface. Crowd Animations supports two different animator workflows: Mecanim Animator: You can use the Unity Mecanim Animator to handle the state machine for animations as usual, and GPUI will read the animator states from the Mecanim Animator. This will let you use your existing Animators and scripts with Crowd Animations, and make it possible to use indirect GPU Instancing out of the box with all the GPUI features such as GPU frustum and occlusion culling. This workflow increases GPU performance while rendering skinned meshes, but it won’t be an ideal solution for projects that are CPU bound since the Mecanim Animator will still create an o

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值