当前位置:网站首页>Unity3d Learning Notes 6 - GPU instantiation (1)
Unity3d Learning Notes 6 - GPU instantiation (1)
2022-07-07 23:21:00 【charlee44】
List of articles
1. summary
In the previous article , A material corresponds to a drawing call instruction . Even in this case , Two 3D objects use the same material , But they use different material parameters , Then it will still cause two drawing instructions . The reason lies in , Graphics work is a state machine , The state has changed , You must perform a drawing call instruction .
GPU Instantiation is used to solve such problems : For things like grass 、 Objects like trees , They often have a large amount of data , But at the same time, there are only minor differences, such as location 、 Posture 、 Color, etc. . If you render like a regular object , There must be many drawing instructions used , Resource occupation is bound to be large . A reasonable strategy is , We specify an object that needs to be drawn , And a large number of different parameters of the object , Then draw it in a drawing call according to the parameters —— That's what's called GPU Instantiation .
2. Detailed discussion
First , We create an empty GameObject object , And attach the following script :
using UnityEngine;
// Instantiate parameters
public struct InstanceParam
{
public Color color;
public Matrix4x4 instanceToObjectMatrix; // Instantiate to the matter matrix
}
[ExecuteInEditMode]
public class Note6Main : MonoBehaviour
{
public Mesh mesh;
public Material material;
int instanceCount = 200;
Bounds instanceBounds;
ComputeBuffer bufferWithArgs = null;
ComputeBuffer instanceParamBufferData = null;
// Start is called before the first frame update
void Start()
{
instanceBounds = new Bounds(new Vector3(0, 0, 0), new Vector3(100, 100, 100));
uint[] args = new uint[5] {
0, 0, 0, 0, 0 };
bufferWithArgs = new ComputeBuffer(1, args.Length * sizeof(uint), ComputeBufferType.IndirectArguments);
int subMeshIndex = 0;
args[0] = mesh.GetIndexCount(subMeshIndex);
args[1] = (uint)instanceCount;
args[2] = mesh.GetIndexStart(subMeshIndex);
args[3] = mesh.GetBaseVertex(subMeshIndex);
bufferWithArgs.SetData(args);
InstanceParam[] instanceParam = new InstanceParam[instanceCount];
for (int i = 0; i < instanceCount; i++)
{
Vector3 position = Random.insideUnitSphere * 5;
Quaternion q = Quaternion.Euler(Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f));
float s = Random.value;
Vector3 scale = new Vector3(s, s, s);
instanceParam[i].instanceToObjectMatrix = Matrix4x4.TRS(position, q, scale);
instanceParam[i].color = Random.ColorHSV();
}
int stride = System.Runtime.InteropServices.Marshal.SizeOf(typeof(InstanceParam));
instanceParamBufferData = new ComputeBuffer(instanceCount, stride);
instanceParamBufferData.SetData(instanceParam);
material.SetBuffer("dataBuffer", instanceParamBufferData);
material.SetMatrix("ObjectToWorld", Matrix4x4.identity);
}
// Update is called once per frame
void Update()
{
if(bufferWithArgs != null)
{
Graphics.DrawMeshInstancedIndirect(mesh, 0, material, instanceBounds, bufferWithArgs, 0);
}
}
private void OnDestroy()
{
if (bufferWithArgs != null)
{
bufferWithArgs.Release();
}
if(instanceParamBufferData != null)
{
instanceParamBufferData.Release();
}
}
}
This script means , Set a mesh and a material , Through randomly obtained instantiation parameters , Render multiple instances of this mesh :
GPU The key interface of instantiation is Graphics.DrawMeshInstancedIndirect().Graphics A series of interfaces of an object are Unity The bottom of the API, It needs to be called every frame .Graphics.DrawMeshInstanced() You can also draw instances , But at most, you can only draw 1023 An example . So still Graphics.DrawMeshInstancedIndirect() better .
Instantiate parameters InstanceParam and GPU Buffer parameters bufferWithArgs Are stored in one ComputeBuffer In the object .ComputeBuffe Defined a GPU Data buffer object , Ability to map to Unity Shader Medium StructuredBuffer in . Instantiate parameters InstanceParam Stores the location of each instantiated object , Posture 、 Zoom and color information , adopt Material.SetBuffer(), Pass to shader :
Shader "Custom/SimpleInstanceShader"
{
Properties
{
}
SubShader
{
Tags{
"Queue" = "Geometry"}
Pass
{
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
#pragma target 4.5
sampler2D _MainTex;
float4x4 ObjectToWorld;
struct InstanceParam
{
float4 color;
float4x4 instanceToObjectMatrix;
};
#if SHADER_TARGET >= 45
StructuredBuffer<InstanceParam> dataBuffer;
#endif
// Vertex shader input
struct a2v
{
float4 position : POSITION;
float3 normal: NORMAL;
float2 texcoord : TEXCOORD0;
};
// Vertex shader output
struct v2f
{
float4 position: SV_POSITION;
float2 texcoord: TEXCOORD0;
float4 color: COLOR;
};
v2f vert(a2v v, uint instanceID : SV_InstanceID)
{
#if SHADER_TARGET >= 45
float4x4 instanceToObjectMatrix = dataBuffer[instanceID].instanceToObjectMatrix;
float4 color = dataBuffer[instanceID].color;
#else
float4x4 instanceToObjectMatrix = float4x4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
float4 color = float4(1.0f, 1.0f, 1.0f, 1.0f);
#endif
float4 localPosition = mul(instanceToObjectMatrix, v.position);
//float4 localPosition = v.position;
float4 worldPosition = mul(ObjectToWorld, localPosition);
v2f o;
//o.position = UnityObjectToClipPos(v.position);
o.position = mul(UNITY_MATRIX_VP, worldPosition);
o.texcoord = v.texcoord;
o.color = color;
return o;
}
fixed4 frag(v2f i) : SV_Target
{
return i.color;
}
ENDCG
}
}
Fallback "Diffuse"
}
This is an improvement from 《Unity3D Learning notes 3——Unity Shader Preliminary use of 》 Simple instantiation shader . The position of instantiated drawing is often not fixed , It means Shader Model matrix obtained in UNITY_MATRIX_M It is generally incorrect . Therefore, the key of instantiation rendering is to recalculate the model matrix , Otherwise, the drawing position is incorrect . The instantiated data is often located close , So you can first pass in a reference position ( matrix ObjectToWorld), Then the instantiated data can only be passed into the relative matrix of this position (instanceToObjectMatrix).
The final running results are as follows , Lots of different positions are drawn 、 Different posture 、 Capsules of different sizes and colors , And the performance is basically unaffected .

3. Reference resources
边栏推荐
- Adults have only one main job, but they have to pay a price. I was persuaded to step back by personnel, and I cried all night
- 微信论坛交流小程序系统毕业设计毕设(8)毕业设计论文模板
- Puce à tension stabilisée LDO - schéma de bloc interne et paramètres de sélection du modèle
- 系统设计概述
- Coreseek: the second step is index building and testing
- 2021ICPC上海 H.Life is a Game Kruskal重构树
- Count the top 10 films at the box office and save them in another file
- Experience sharing of system architecture designers in preparing for the exam: the direction of paper writing
- leetcode-520. 检测大写字母-js
- Install a new version of idea. Double click it to open it
猜你喜欢

Wechat forum exchange applet system graduation design completion (1) development outline

Install a new version of idea. Double click it to open it

微信论坛交流小程序系统毕业设计毕设(4)开题报告

高级程序员必知必会,一文详解MySQL主从同步原理,推荐收藏

Wechat forum exchange applet system graduation design completion (7) Interim inspection report

Specific method example of V20 frequency converter manual automatic switching (local remote switching)

三问TDM

Binary tree

ROS2专题(03):ROS1和ROS2的区别【02】
![给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」](/img/21/2e99dd6173ab4925ec22290cd4a357.png)
给出一个数组,如 [7864, 284, 347, 7732, 8498],现在需要将数组中的数字拼接起来,返回「最大的可能拼出的数字」
随机推荐
LeeCode -- 6. Z 字形变换
FPGA基础篇目录
Specific method example of V20 frequency converter manual automatic switching (local remote switching)
网络安全-永恒之蓝
opencv scalar传入三个参数只能显示黑白灰问题解决
648. 单词替换
智慧社區和智慧城市之間有什麼异同
When copying something from the USB flash disk, an error volume error is reported. Please run CHKDSK
Ros2 topic (03): the difference between ros1 and ros2 [02]
Install Fedora under RedHat
十三、系统优化
Wechat forum exchange applet system graduation design (5) assignment
Cloud native is devouring everything. How should developers deal with it?
Dynamic agent explanation (July 16, 2020)
ROS2专题(03):ROS1和ROS2的区别【01】
【编译原理】词法分析设计实现
POJ2392 SpaceElevator [DP]
Inftnews | web5 vs Web3: the future is a process, not a destination
Matlab 信号处理【问答随笔·2】
MATLAB signal processing [Q & A essays · 2]