<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Unity3D學習筆記8——GPU實例化(3)

      1. 概述

      在前兩篇文章《Unity3D學習筆記6——GPU實例化(1)》《Unity3D學習筆記6——GPU實例化(2)》分別介紹了通過簡單的頂點著色器+片元著色器,以及通過表面著色器實現GPU實例化的過程。而在Unity的官方文檔Creating shaders that support GPU instancing里,也提供了一個GPU實例化的案例,這里就詳細論述一下。

      2. 詳論

      2.1. 自動實例化

      一個有意思的地方在于,Unity提供的標準材質支持自動實例化,而不用像《Unity3D學習筆記6——GPU實例化(1)》《Unity3D學習筆記6——GPU實例化(2)》那樣額外編寫腳本和Shader。并且,會自動將transform,也就是模型矩陣作為每個實例的屬性。

      照例,還是編寫一個腳本掛到一個空的GameObject對象上:

      using UnityEngine;
      
      public class Note8Main : MonoBehaviour
      {
          public Mesh mesh;
          public Material material;
          public int instanceCount = 5000;
      
          // Start is called before the first frame update
          void Start()
          {
              MaterialPropertyBlock props = new MaterialPropertyBlock();
            
              for (int i = 0; i < instanceCount; i++)
              {
                  GameObject go = new GameObject();
                  go.name = i.ToString();
      
                  MeshFilter mf = go.AddComponent<MeshFilter>();
                  mf.mesh = mesh;
      
                  MeshRenderer mr = go.AddComponent<MeshRenderer>();
                  mr.material = material;
                  
                  go.transform.position = Random.insideUnitSphere * 5;
                  go.transform.eulerAngles = new Vector3(Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f));
                  float s = Random.value;
                  go.transform.localScale = new Vector3(s, s, s);
             
                  go.transform.parent = gameObject.transform;
              }
          }
      
          // Update is called once per frame
          void Update()
          {
              
          }
      }
      

      這個腳本的意思是,給掛接的GameObject下新建很多GameObject,它們使用我們傳入的Mesh和Material,但是位置、姿態和大小是隨機的。傳入的Mesh使用Unity自帶的膠囊體,Material使用Unity的標準材質。運行結果如下:

      imglink1

      這個時候Unity還沒有自動實例化,打開Frame Debug就可以看到:
      imglink2

      這個時候我們可以在使用的材質上勾選打開實例化的選項:
      imglink3

      再次運行,就會在Frame Debug看到Unity實現了自動實例化,繪制的批次明顯減少,并且性能會有所提升:
      imglink4

      可以看到確實是自動進行實例化繪制了,但是這種方式卻似乎存在實例化個數的上限,所有的實例化數據還是分成了好幾個批次進行繪制。與《Unity3D學習筆記6——GPU實例化(1)》《Unity3D學習筆記6——GPU實例化(2)》提到的通過底層接口Graphic進行實例化繪制相比,效率還是要低一些。

      2.2. MaterialPropertyBlock

      自動實例化只能將transform,也就是模型矩陣作為每個實例的屬性。如果需要增加自己的實例屬性,就需要使用MaterialPropertyBlock,也就是材質屬性塊。

      修改上面的腳本:

      using UnityEngine;
      
      public class Note8Main : MonoBehaviour
      {
          public Mesh mesh;
          public Material material;
          public int instanceCount = 5000;
      
          // Start is called before the first frame update
          void Start()
          {
              MaterialPropertyBlock props = new MaterialPropertyBlock();
            
              for (int i = 0; i < instanceCount; i++)
              {
                  GameObject go = new GameObject();
                  go.name = i.ToString();
      
                  MeshFilter mf = go.AddComponent<MeshFilter>();
                  mf.mesh = mesh;
      
                  MeshRenderer mr = go.AddComponent<MeshRenderer>();
                  mr.material = material;
      
                  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));
                  mr.SetPropertyBlock(props);
      
                  go.transform.position = Random.insideUnitSphere * 5;
                  go.transform.eulerAngles = new Vector3(Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f), Random.Range(0.0f, 90.0f));
                  float s = Random.value;
                  go.transform.localScale = new Vector3(s, s, s);
             
                  go.transform.parent = gameObject.transform;
              }
          }
      
          // Update is called once per frame
          void Update()
          {
              
          }
      }
      

      腳本使用的材質,其使用的Shader如下,可以直接在Standard Surface Shader的基礎上改:

      Shader "Custom/HiddenSurfaceIntanceShader"
      {
          Properties
          {
              _Color ("Color", Color) = (1,1,1,1)
              _MainTex ("Albedo (RGB)", 2D) = "white" {}
              _Glossiness ("Smoothness", Range(0,1)) = 0.5
              _Metallic ("Metallic", Range(0,1)) = 0.0
          }
          SubShader
          {
              Tags { "RenderType"="Opaque" }
              LOD 200
      
              CGPROGRAM
              // Physically based Standard lighting model, and enable shadows on all light types
              #pragma surface surf Standard fullforwardshadows
      
              // Use shader model 3.0 target, to get nicer looking lighting
              #pragma target 3.0
      
              sampler2D _MainTex;
      
              struct Input
              {
                  float2 uv_MainTex;
              };
      
              half _Glossiness;
              half _Metallic;
              //fixed4 _Color;
      
              // Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader.
              // See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing.
              // #pragma instancing_options assumeuniformscaling
              UNITY_INSTANCING_BUFFER_START(Props)
                  // put more per-instance properties here
      			UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
              UNITY_INSTANCING_BUFFER_END(Props)
      
              void surf (Input IN, inout SurfaceOutputStandard o)
              {
                  // Albedo comes from a texture tinted by color
                  //fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
      			fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
                  o.Albedo = c.rgb;
                  // Metallic and smoothness come from slider variables
                  o.Metallic = _Metallic;
                  o.Smoothness = _Glossiness;
                  o.Alpha = c.a;
              }
              ENDCG
          }
          FallBack "Diffuse"
      }
      

      關鍵的代碼在于Unity內置宏UNITY_INSTANCING_BUFFER_START和UNITY_INSTANCING_BUFFER_END、UNITY_DEFINE_INSTANCED_PROP定義了實例化屬性,在著色器中,通過內置宏UNITY_ACCESS_INSTANCED_PROP來獲取這個屬性值。這個實例化屬性也就是腳本代碼中MaterialPropertyBlock傳入的顏色值。

      查看Unity Shader源代碼,這四個用于實例化的宏封裝的是一個cbuffer數組,cbuffer就是hlsl的常量緩沖區:

      #define UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(name)  cbuffer name {
      #define UNITY_INSTANCING_CBUFFER_SCOPE_END          }
      
      #define UNITY_INSTANCING_BUFFER_START(buf)      UNITY_INSTANCING_CBUFFER_SCOPE_BEGIN(UnityInstancing_##buf) struct {
      #define UNITY_INSTANCING_BUFFER_END(arr)        } arr##Array[UNITY_INSTANCED_ARRAY_SIZE]; UNITY_INSTANCING_CBUFFER_SCOPE_END
      #define UNITY_DEFINE_INSTANCED_PROP(type, var)  type var;
      #define UNITY_ACCESS_INSTANCED_PROP(arr, var)   arr##Array[unity_InstanceID].var
      

      運行的結果如下:
      imglink5

      可以看到除了紋理,每一個膠囊體還獲取了隨機賦予給材質的顏色,也就是我們設置的顏色成為了實例化屬性數據。MaterialPropertyBlock主要由Graphics.DrawMesh和Renderer.SetPropertyBlock使用,在希望繪制具有相同材質,但屬性略有不同的多個對象時可使用它。

      個人認為使用MaterialPropertyBlock自動實例化性能比不上使用Graphics.DrawMeshInstancedIndirect(),但是它有個優點是實例化的要求沒那么高,Graphics.DrawMeshInstancedIndirect()要求使用同一mesh,同一貼圖;但是MaterialPropertyBlock沒這個要求,只要是同一材質,任何屬性不一樣都可以用,在減少繪制批次的同時還能減少材質的個數。

      3. 參考

      1. 《Unity3D學習筆記6——GPU實例化(1)》
      2. 《Unity3D學習筆記6——GPU實例化(2)》
      3. Creating shaders that support GPU instancing
      4. MaterialPropertyBlock

      具體實現代碼

      posted @ 2022-07-10 19:17  charlee44  閱讀(853)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 遵义市| 色www视频永久免费| 亚洲精品无码久久千人斩| 又爽又黄又无遮掩的免费视频| 乌什县| 亚洲美免无码中文字幕在线| 色偷偷久久一区二区三区| 亚洲精品中文字幕尤物综合| 国产一区二区av天堂热| 成人亚洲国产精品一区不卡| 亚洲中文字幕无码爆乳| 丰满岳乱妇久久久| 精品无码久久久久久尤物| 欧美大香线蕉线伊人久久| 精品一区二区三区日韩版| 国产精品一区二区久久毛片| 无码成人一区二区三区| 伊人久久大香线蕉网av| 日韩人妻av一区二区三区| 在线视频不卡在线亚洲| 亚洲午夜精品久久久久久抢 | 综合成人亚洲网友偷自拍| 天堂…中文在线最新版在线| 午夜av高清在线观看| 大冶市| 一本无码在线观看| 国产成人综合欧美精品久久| 久久精品国产亚洲av麻豆不卡| 日韩区中文字幕在线观看| 亚洲 制服 丝袜 无码| 99国产精品白浆在线观看免费 | 亚洲AVAV天堂AV在线网阿V| 久热这里有精品免费视频| 中文日韩在线一区二区| 中文字幕在线视频不卡一区二区| 97亚洲熟妇自偷自拍另类图片| 日韩熟妇中文色在线视频| 欧美丰满熟妇xxxx性| 国产性色av高清在线观看| 国产精品成人一区二区三区| 我国产码在线观看av哈哈哈网站|