summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/Bakery/BakerySkyLight.cs
blob: fddd6cbb59e5264c633ac6fdb983b06222c4d01f (plain)
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
110
111
112
113
114
115
116
117
118
119
120
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

using System;
using System.Collections;
using System.Collections.Generic;

[ExecuteInEditMode]
[DisallowMultipleComponent]
public class BakerySkyLight : MonoBehaviour
{
    public string texName = "sky.dds";
    public Color color = Color.white;
    public float intensity = 1.0f;
    public int samples = 32;
    public bool hemispherical = false;
    public int bitmask = 1;
    public bool bakeToIndirect = true;
    public float indirectIntensity = 1.0f;
    public bool tangentSH = false;
    public bool correctRotation = false;

    public Cubemap cubemap;

    public int UID;

    public static int lightsChanged = 0; // 1 = const, 2 = full

#if UNITY_EDITOR
    void OnValidate()
    {
        if (lightsChanged == 0) lightsChanged = 1;
    }
    void OnEnable()
    {
        lightsChanged = 2;
    }
    void OnDisable()
    {
        lightsChanged = 2;
    }

    public void Start()
    {
        if (gameObject.GetComponent<BakeryDirectLight>() != null ||
            gameObject.GetComponent<BakeryPointLight>() != null ||
            gameObject.GetComponent<BakeryLightMesh>() != null)
        {
            EditorUtility.DisplayDialog("Bakery", "Can't have more than one Bakery light on one object", "OK");
            DestroyImmediate(this);
            return;
        }

        if (EditorApplication.isPlayingOrWillChangePlaymode) return;
        if (UID == 0) UID = Guid.NewGuid().GetHashCode();
        ftUniqueIDRegistry.Register(UID, gameObject.GetInstanceID());
    }

    void OnDestroy()
    {
        if (UID == 0) return;
        if (EditorApplication.isPlayingOrWillChangePlaymode) return;
        ftUniqueIDRegistry.Deregister(UID);
    }

    void Update()
    {
        if (EditorApplication.isPlayingOrWillChangePlaymode) return;
        if (!ftUniqueIDRegistry.Mapping.ContainsKey(UID)) ftUniqueIDRegistry.Register(UID, gameObject.GetInstanceID());
        if (gameObject.GetInstanceID() != ftUniqueIDRegistry.GetInstanceId(UID))
        {
            UID = Guid.NewGuid().GetHashCode();
            ftUniqueIDRegistry.Register(UID, gameObject.GetInstanceID());
        }
    }

    void OnDrawGizmos()
    {
      Gizmos.color = new Color(49/255.0f, 91/255.0f, 191/255.0f);
      Gizmos.DrawSphere(transform.position, 0.1f);
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = new Color(49/255.0f, 91/255.0f, 191/255.0f);
        Vector3 origin = transform.position;
        const int segments = 16;
        for(int i=0; i<segments; i++)
        {
            float p1 = i / (float)segments;
            float p2 = (i+1) / (float)segments;

            float x1 = Mathf.Cos(p1 * Mathf.PI*2);
            float y1 = Mathf.Sin(p1 * Mathf.PI*2);

            float x2 = Mathf.Cos(p2 * Mathf.PI*2);
            float y2 = Mathf.Sin(p2 * Mathf.PI*2);

            Gizmos.DrawLine(origin + new Vector3(x1,0,y1), origin + new Vector3(x2,0,y2));

            if (hemispherical)
            {
                x1 = Mathf.Cos(p1 * Mathf.PI);
                y1 = Mathf.Sin(p1 * Mathf.PI);

                x2 = Mathf.Cos(p2 * Mathf.PI);
                y2 = Mathf.Sin(p2 * Mathf.PI);
            }

            Gizmos.DrawLine(origin + new Vector3(x1,y1,0), origin + new Vector3(x2,y2,0));
            Gizmos.DrawLine(origin + new Vector3(0,y1,x1), origin + new Vector3(0,y2,x2));
        }
    }

#endif
}