Unity中Batching优化的GPU实例化整理总结


前言

我们在之前的文章中,实现了GPU实例化的支持,我们在这篇文章中来整理总结一下


一、GPU Instancing的支持

1、硬件支持

2、Shader支持

3、脚本支持


二、我们来顺着理一下GPU实例化的使用步骤

1、GPU实例化前的C#代码准备

2、在 appdata 和 v2f 中定义GPU实例化ID

3、在顶点着色 和 片元着色器 设置GPU Instance ID,使实例化对象顶点位置正确(设置后坐标变换矩阵就生效)

4、如果GPU实例化后,需要不同对象使用不同属性,Shader中GPU实例化怎么定义和使用属性。以及在C#脚本中使用材质属性块来修改材质属性


三、最终代码

请添加图片描述

Shader代码:

Shader "MyShader/P2_6_5"
{
    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;
                float2 uv : TEXCOORD0;
                //1、定义GPU实例化 ID
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                float3 worldPos : TEXCOORD4;
                //1、定义GPU实例化 ID
                UNITY_VERTEX_INPUT_INSTANCE_ID
            };
            
	        UNITY_INSTANCING_BUFFER_START(prop)
	        UNITY_DEFINE_INSTANCED_PROP(fixed4,_Color)
	        UNITY_INSTANCING_BUFFER_END(prop)
            
            v2f vert (appdata v)
            {
	            //2、设置GPU实例化ID,使实例化对象顶点位置正确
                UNITY_SETUP_INSTANCE_ID(v);
                v2f o;
	            UNITY_TRANSFER_INSTANCE_ID(v, o);
                o.pos = UnityObjectToClipPos(v.vertex);
                o.worldPos = mul(unity_ObjectToWorld,v.vertex);
                o.uv = v.uv;
               
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                //2、设置GPU实例化ID,使实例化对象顶点位置正确
                UNITY_SETUP_INSTANCE_ID(i);
                return i.worldPos.y * 0.15 + UNITY_ACCESS_INSTANCED_PROP(prop,_Color);
            }
            ENDCG
        }
    }
}

C#代码:

using UnityEngine;

public class P2_6_5 : MonoBehaviour
{
    public GameObject Prefab;
    public int Count = 1;
    public int Range = 10;
    // Start is called before the first frame update
    void Start()
    {
        
        
        for (int i = 0;i < Count;i++)
        {
            Vector3 pos = Random.insideUnitCircle * Range;
            GameObject chair = Instantiate(Prefab,new Vector3(pos.x,0,pos.y),Quaternion.identity);
            Color color = new Color(Random.value,Random.value,Random.value);
            
            MaterialPropertyBlock prop = new MaterialPropertyBlock();
            prop.SetColor("_Color",color);
            chair.GetComponentInChildren<MeshRenderer>().SetPropertyBlock(prop);
        }
    }
}

  • 10
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
GPU Instancer is an out of the box solution to display extreme numbers of objects on screen with high performance. With a few mouse clicks, you can instance your prefabs, Unity Terrain details and trees. GPU Instancer provides user friendly tools to allow everyone to use Indirect GPU Instancing without having to go through the deep learning curve of Compute Shaders and GPU infrastructure. Also, an API with extensive documentation is provided to manage runtime changes. ——————————— FEATURES ——————————— – Out of the box solution for complex GPU Instancing. – VR compatible. Works with both single pass and multi pass rendering modes. – Mobile compatible. Works with both iOS and Android. – Easy to use interface. – Tens of thousands of objects rendered lightning fast in a single draw call. – GPU frustum culling. – GPU occlusion culling (also supports VR platforms with both single pass and multi pass rendering modes). – Automatically configured custom shader support. – Supports Standard, Universal and HD Render Pipelines. – Complex hierarchies of prefabs instanced with a single click. – Multiple sub-meshes support. – LOD Groups and cross-fading support. (Cross-fading is supported on Standard Render Pipeline only) – Automatic 2D Billboard generation system (Standard RP only). – Shadows casting and receiving support for instances (frustum culled instances still can cast shadows). – Ability to use custom shadow distance per prototype and to choose the LOD to render shadows with. – Support for Floating Origin handling. – Multiple camera support. – Well documented API for procedural scenes and runtime modifications (examples included). – Ability to Remove instances inside bounds or colliders at runtime. – Ability to extend with custom Compute Shaders. – Example scenes that showcase GPU Instancer capabilities. Prefab Instancing Features: – Ability to automatically instance prefabs at your scene that you distribute with your favorite prefab painting tool. – Automatically Add-Re
GPU Instancer 是一款开箱即用的工具,可显示屏幕上对象的精确数量,性能表现强劲。只需鼠标点击几下,你就可以实例化你的预设、Unity 地形细节和树木。 GPU Instancer 可提供方便操作的工具,让你省去费力学习计算着色器和 GPU 基础架构的麻烦,轻松使用间接 GPU 实例化。同时,我们还提供带有详细文档的 API,用来管理运行时期更改。 --------------------------------- 功能特色 --------------------------------- - 适用于复杂 GPU 实例化的开箱即用型解决方案。 - 兼容 VR。适用于单通道和多通道渲染模式。 - 兼容移动端。适用于 iOS 和 Android。 - 易于使用的界面。 - 数以万计的对象仅需绘制调用一次,即可快速渲染。 - GPU 视锥体剔除。 - GPU 遮挡剔除(还支持具有单通道和多通道渲染模式的 VR 平台)。 - 支持自动配置的自定义着色器。 - 支持标准、通用和高清渲染管线。 - 一键即可将层次复杂的预制件进行实例化。 - 支持多个子网格。 - 支持 LOD 组和交叉渐变。(交叉渐变仅在标准渲染管线支持) - 自动 2D 广告牌生成系统(仅限标准 RP)。 - 阴影投射和获得实例的支持(视锥剔除实例仍然可以投射阴影)。 - 能够按原型使用自定义阴影距离,以及选择用于渲染阴影的 LOD。 - 支持浮动原点处理。 - 支持多个视角。 - 记录良好的 API,可用于过程场景和运行时修改(包括示例)。 - 能够在运行时移除边界或碰撞体内的实例。 - 能够使用自定义计算着色器进行扩展。 - 展示 GPU 实例化器功能的示例场景。 预制件实例化功能: - 能够用你喜欢的预制件涂装工具在分配的场景自动实例化预制件。 - 自动添加-移除预制件实例,无需任何附加代码。 - 自动检测和更新变换位置、旋转和缩放变化。 - 支持全部或区域定位的刚体和物理。 - 支持嵌套式预制件(Unity 2018.3 及更高版本)。 - 带有或不带实例化游戏对象的添加-移除-更新预制件实例(包括示例)。 - 经由 API 基于实例的材质变化(由于缓冲区限制,Android 不支持) - 在运行时基于实例启用和禁用实例化。 - 在运行时管理实例化预制件的 API。 - 包括带有自定义控制器的移动演示场景。 详细的实例化功能: - 高帧频的茂密草地和植被。 - 包括具有风、阴影、AO、广告牌和各种其他属性的植被着色器。 - 支持自定义着色器和材质。 - 支持交叉四边形:自动将草纹理转变成交叉四边形。 - 能够使用 Unity 地形上的自定义材质对预制件进行涂刷(使用 Unity 地形工具)。 - 能够在 Unity 地形上将预制件与 LOD 组一起使用。 - 通过自动空间分区进一步提高性能。 - 在运行时管理实例化地形详细信息原型的 API(包括示例)。 - 编辑器 GPU 实例化模拟。 树木实例化功能: - 高帧率的茂密森林。 - SpeedTree 支持风动画。 - SpeedTree 8 支持风动画。 - 树木创建器支持风动画。 - 支持软遮挡树。 - 包括广告牌烘焙师和渲染器。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

楠溪泽岸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值