Awake 함수는 생성자이다.

component 생성할 awake함수에서 실행하면 된다.

무엇인가를 생성(set)해줄 사용. 


Start함수는 

업데이트 직전에 호출되며 스크립트가 활성화 되어야 작동.

기능을 부여할 사용. 


Physics 조절하는 방법(움직임 jitter줄이기)

rigidbody 

물리 연산 가능하게하는 component 일반적으로 collider와 함께 사용./ 

이동시 rigidbody의 velocity사용해서 이동하기(물리적용 이동)

collision detection(방법 선택 가능

interpolate(이전 씬과 다음 씬의 이동을 보간해줘서 움직임을 자연스럽게 만듦.) 

extrapolate - 속도에 따른 예상 범위로의 보간. 일정한 속도를 가지고 있는 경우에 사용함. 

physics Material 

마찰력, 탄성 조절 가능


물리엔진은 fixed time step 사용한다. 
따라서, fixedUpdate에서 움직인다. 


*‘|=‘ 연산자는 명시적으로 false값을 만들어 주기 전까지 
활성화 상태를 유지한다. 


Posted by 도이(doi)
,

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)
,

GetPixels

programming/c# 2020. 1. 1. 14:06
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class GetPixel : MonoBehaviour
{
    public RenderTexture rendTex;
    private Texture2D sourceTex;
 
    public GameObject prefab;
    public float offset = 1.2f;
    public float prefabScale = 5.0f;
 
    private int _width;
    private int _height;
 
    private Color[] pix;
    private List<GameObject> list;
    private List<Renderer> rendlist;
    private List<Transform> transformlist;
 
    private MaterialPropertyBlock properties; 
 
    void Start()
    { 
        sourceTex = GetRTPixels(rendTex);
        //소스 텍스트 크기 
        _width = sourceTex.width;
        _height = sourceTex.height;
        //픽셀 색깔 저장하기 
        pix = sourceTex.GetPixels(00, _width, _height);
        //리스트 생성  
        list = new List<GameObject>();
        rendlist = new List<Renderer>();
        transformlist = new List<Transform>();
        properties = new MaterialPropertyBlock();
 
        //픽셀 큐브 생성 
        for (int w = 0; w < _width; w++)
        {
            for (int h = 0; h < _height; h++)
            {
                GameObject obj = Instantiate(prefab, new Vector3(w * offset, 0, h * offset), Quaternion.identity) as GameObject;
                list.Add(obj);
                transformlist.Add(obj.transform);
                Renderer rend = obj.GetComponent<Renderer>();
                rendlist.Add(rend);
 
                obj.transform.SetParent(transform);
            }
        }
 
        transform.rotation = Quaternion.Euler(180-90-90); //보드 회전하기 
    }
 
    private void Update()
    {
        sourceTex = GetRTPixels(rendTex); //실시간으로 texture받아오기 
        pix = sourceTex.GetPixels(00, _width, _height); //pixel 받아오기 
 
        //픽셀 색상 적용 
        for (int i = 0; i < _width * _height; i++)
        {
            properties.SetColor("_Color", pix[i]);//gpuInstancing pixel Color값 받아오기 
            rendlist[i].SetPropertyBlock(properties); //gpuInstancing 사용 Color값 적용  
        }
 
        //pixel 애니메이션
        for (int i = 0; i < _width * _height; i++)
        {
            //화이트 제외 -> 사용하려면 아래 else로 묶어주기 
            //if(pix[i].r > .7f && pix[i].g > .7f && pix[i].b > .7f)
            //{
            //    transformlist[i].localScale = 
            //    new Vector3(1, Mathf.Lerp(list[i].transform.localScale.y, 0.1f, Time.deltaTime * 5.0f), 1);
            //}
 
            float color = pix[i].r + pix[i].g + pix[i].b;
 
            //색상 값 pingpong (균일)
            transformlist[i].localScale = 
            new Vector3(1, Mathf.PingPong(color * Time.time + .01f, color * prefabScale + 0.01f), 1);
 
            //색상 값 pingpong (비균일)
            //transformlist[i].localScale =
            //new Vector3(1, Mathf.PingPong(Time.time + .01f, color * prefabScale + 0.01f), 1);
 
            //속도 값 pingpong
            //transformlist[i].localScale = 
            //new Vector3(1, Mathf.PingPong(Time.time + color + 0.01f, prefabScale), 1);
 
            //색상 값 lerp - 색상 변형 영향 x
            //transformlist[i].localScale = 
            //new Vector3(1, Mathf.Lerp(list[i].transform.localScale.y, (color + 0.01f) * prefabScale, Time.deltaTime * 5.0f), 1);
        }
    }
 
    //renderTexture -> Texture2D로 전환 함수 
    static public Texture2D GetRTPixels(RenderTexture rt)
    {
        RenderTexture currentActiveRT = RenderTexture.active;
        RenderTexture.active = rt;
        Texture2D tex = new Texture2D(rt.width, rt.height);
        tex.ReadPixels(new Rect(00, tex.width, tex.height), 00);
        RenderTexture.active = currentActiveRT;
        return tex;
    }
 
}
cs


Posted by 도이(doi)
,

Enumerator란?

배열의 모든 요소를 반복하는 것처럼.
한 번에 collection에 있는 한 개의 아이템을 거치는 개념입니다. 
Enumerator를 사용하는 이유는 Coroutine이 이를 필요로 하기 때문입니다.


yield란?

yield라는 것은 반복자의 IEnumerator 객체에 값을 전달(yield)하거나,
반복의 종료를 알리기 위해 사용한다.

Coroutine은 어떻게 작동하는가?

coroutine을 사용하는 것은 iterator를 사용하는 것이다. 

https://m.blog.naver.com/PostView.nhn?blogId=dlwhdgur20&logNo=221016139917&proxyReferer=https%3A%2F%2Fwww.google.com%2F


Twist 

시작 축을 바꿔서 재미있게 돌아가도록 유도함. 
ex) y축을 기준으로 도는 도형에 start 함수에서 x축 변환을 줌 


Posted by 도이(doi)
,