팀프로젝트/R&D

2020.06.14. 낮,밤 / 조명 (버튼) - Emission 이 쉐이더 눌러야 적용되던 것 해결

dev_sr 2020. 6. 14. 14:36

 

emission color에 잘 접근했는데 쉐이더 버튼을 일일히 눌러줘야 적용되던 걸 해결했다

 

this.arrMaterials[index1].EnableKeyword("_EMISSION");

this.arrMaterials[index1].globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;

 

 

저 두줄을 start에 넣어주면 되는 거였음

emission color는 시작할 때 비어있어서 사용하려면 초기화를 한번 해줘야한다고 한다. 

 

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
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
    public GameObject[] arrObjects; //조명을 적용할 대상들
    private Material[] arrMaterials;    //그 대상들의 메테리얼을 갖고 있을 배열
    public Button btn;  //누를때마다 낮밤이 바뀜
    private bool isNight;   //밤인가?
 
    void Start()
    {
        this.isNight = false;
        this.arrMaterials = new Material[this.arrObjects.Length];
        
        int index1 = 0;
        for (int i = 0; i < this.arrObjects.Length; i++)
        {
            index1 = i;
            this.arrMaterials[index1] = this.arrObjects[index1].GetComponent<Renderer>().material;
            this.arrMaterials[index1].EnableKeyword("_EMISSION");
            this.arrMaterials[index1].globalIlluminationFlags = MaterialGlobalIlluminationFlags.RealtimeEmissive;
 
        }
 
        this.btn.onClick.AddListener(() =>
        {
            int index2 = 0;
 
            if (!this.isNight)
            {
                for (int i = 0; i < this.arrMaterials.Length; i++)
                {
                    index2 = i;
                    this.arrMaterials[index2].SetColor("_EmissionColor", Color.white);  //건물 안 조명키기
                    RenderSettings.ambientLight = Color.black;  //씬 전체 라이트렌더링 어둡게 하기
 
                }
                this.isNight = true;
            }
            else
            {
                for (int i = 0; i < this.arrMaterials.Length; i++)
                {
                    index2 = i;
                    this.arrMaterials[index2].SetColor("_EmissionColor", Color.black);  //건물 안 조명 끄기
                    RenderSettings.ambientLight = Color.white;  //씬 전체 라이트렌더링 밝게 하기
                }
                this.isNight = false;
 
            }
 
        });
    }
 
}
 
 

 

 

 

Getting your emission maps to work in Unity

Published on 27 Jan 2020, 02:20 AM (+0800) Emission maps (also known as emissive maps) are textures that can be applied to materials to make certain parts of an object emit light (like the cube in the image above). In Unity, lights from emission maps don��

blog.terresquall.com

 

 

----------------------------------------------------------------------------------------------------------------------------------------------------

polycity 패키지에서 낮밤을 구현해보았다.

 

일단 씬 전체를 밝고 어둡게 조절하려면 ligting을 켜서 Source를 Color로 바꿔주고

Ambient Color 를 제어해야함

 

버튼 하나로 낮 밤 바뀌게 해보았는데

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
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
 
public class Test : MonoBehaviour
{
    public GameObject[] arrObjects; //조명을 적용할 대상들
    private Material[] arrMaterials;    //그 대상들의 메테리얼을 갖고 있을 배열
    public Button btn;  //누를때마다 낮밤이 바뀜
    private bool isNight;   //밤인가?
 
    void Start()
    {
        this.isNight = false;
        this.arrMaterials = new Material[this.arrObjects.Length];
        
        int index1 = 0;
        for (int i = 0; i < this.arrObjects.Length; i++)
        {
            index1 = i;
            this.arrMaterials[index1] = this.arrObjects[index1].GetComponent<Renderer>().material;
 
        }
 
        this.btn.onClick.AddListener(() =>
        {
            int index2 = 0;
 
            if (!this.isNight)
            {
                for (int i = 0; i < this.arrMaterials.Length; i++)
                {
                    index2 = i;
                    this.arrMaterials[index2].SetColor("_EmissionColor", Color.white);  //건물 안 조명키기
                    RenderSettings.ambientLight = Color.black;  //씬 전체 라이트렌더링 어둡게 하기
 
                }
                this.isNight = true;
            }
            else
            {
                for (int i = 0; i < this.arrMaterials.Length; i++)
                {
                    index2 = i;
                    this.arrMaterials[index2].SetColor("_EmissionColor", Color.black);  //건물 안 조명 끄기
                    RenderSettings.ambientLight = Color.white;  //씬 전체 라이트렌더링 밝게 하기
                }
                this.isNight = false;
 
            }
 
        });
    }
 
}
 
 

 

먼저 조명을 켤 오브젝트들을 배열에 넣고

그 오브젝트들의 메테리얼을 찾아서 배열에 넣고

 

this.arrMaterials[index2].SetColor("_EmissionColor", Color.black);

 

를 통해서 하나씩 EmissionColor에 접근해서 색을 밝게 해주면 건물안이나 가로등, 차의 불이 켜진다.

(이 패키지 메테리얼이 그렇게 설정이 되어있음)

 

 

그리고

 

RenderSettings.ambientLight = Color.white; 

 

를 통해 씬 전체의 라이트 색깔에 접근해서 어둡거나 밝게 설정해준다.

 

이거까진 코드도 틀린 것 없고 괜찮은데

문제는 실행하고 내가 일일히 하이어라키에서 오브젝트를 눌러주고 인스펙터에서 쉐이더 창을 열어줘야 적용된다

왜그런지 모르겠음..

 

 

1. 처음 플레이 화면 - 여기서 아무리 버튼 클릭해도 뒤에 배경만 바뀌고 불 안켜짐 

 

 

2. 어두운 상태에서 인스펙터에서 쉐이더 창을 클릭해서 열어주면 불이 켜진다 

 

 

그렇게 스크립트에 적용된 오브젝트들을 다 하나씩 만져주면

 

내가 키고 싶었던 오브젝트들은 다 켜지고 그 다음부터는 버튼에 잘 작동한다

그래서 빌드되면 당연히 버튼에 반응 안한다 ..

 

 

 

 

 

참고 - 여기서 emission에 접근하면 된다고 알려줌

 

lighting 의 ambient color 에 접근하는 방법

 

Unity - Scripting API: RenderSettings.ambientLight

Success! Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable. Close

docs.unity3d.com

 

emission color에 접근하는 방법

 

unity3d - Emission | unity3d Tutorial

unity3d documentation: Emission

riptutorial.com