vsync 체크 : cpu gpu 동기화 사용. 필요 없는 경우가 많음. 

dynamic batching 체크하기 : 유니티에서 자동으로 최적화 도와줌. 


GPU Instancing을 위한 쉐이더 기본 구조

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
Shader "Custom/GPUinstancing"
{
    //shader에서 사용할 속성 (인터페이스)
    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
        _Emission ("Emission", Color) = (000)
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 200
 
        CGPROGRAM
        //전처리(스니핏) - 조명 설정 
        #pragma surface surf Standard fullforwardshadows
        #pragma target 3.0
 
        sampler2D _MainTex;
        
        //구조체 - 엔진으로부터 받아와야할 데이터 
        struct Input
        {
            float2 uv_MainTex;
        };
        
        //변수 선언 
        half _Glossiness;
        half _Metallic;
        fixed4 _Emission;
        
        //GPU Instancing을 사용하기 위한 기능
        UNITY_INSTANCING_BUFFER_START(Props)
        UNITY_DEFINE_INSTANCED_PROP(fixed4, _Color)
        UNITY_INSTANCING_BUFFER_END(Props)
        
        //함수 - 색상 이미지 출력  
        void surf (Input IN, inout SurfaceOutputStandard o)
        {
            fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * UNITY_ACCESS_INSTANCED_PROP(Props, _Color);
            o.Albedo = c.rgb;
            o.Metallic = _Metallic;
            o.Smoothness = _Glossiness;
            o.Alpha = c.a;
            o.Emission = c.rgb + _Emission;
        }
        ENDCG
    }
    FallBack "Diffuse"
}
 


cs


생성 오브젝트에 적용하는 스크립트 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class GPUInstancingTest_ : MonoBehaviour
{
    public Transform prefab;
    public int instances = 5000;
    public float radius = 50f;
 
    private MaterialPropertyBlock properties;
    private Transform t;
 
    private List<Renderer> rendlist;
 
    private void Start()
    {
        rendlist = new List<Renderer>(); //render list생성 
        properties = new MaterialPropertyBlock(); //property 블록 생성 
 
        for (int i=0; i<instances; i++)
        {
             //prefab 생성 
            t = Instantiate(prefab);
            //render 리스트 추가 
            Renderer rend = t.GetComponent<Renderer>();
            rendlist.Add(rend);
 
            t.localPosition = Random.insideUnitSphere * radius; //random 위치 
            t.SetParent(transform); //생성된 오브젝트 하위로 
        }
    }
 
    private void Update()
    {
        for(int i=0; i<instances; i++)
        {
            //색상 지정하기 
            properties.SetColor(
               "_Color"new Color(Random.value, Random.value, Random.value)
           );
            //색상 적용하기 
            rendlist[i].SetPropertyBlock(properties);
        }
    }
}
 
cs


Posted by 도이(doi)
,