summaryrefslogtreecommitdiff
path: root/VRCSDK3Worlds/Assets/Bakery/BakeryPointLight.cs
blob: 170b14664cc3f4e06dee47f5405c58d4a8a5e8bb (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using UnityEngine;

#if UNITY_EDITOR
using UnityEditor;
#endif

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

[ExecuteInEditMode]
[DisallowMultipleComponent]
public class BakeryPointLight : MonoBehaviour
{
    public enum ftLightProjectionMode
    {
        Omni = 0,
        Cookie = 1,
        Cubemap = 2,
        IES = 3,
        Cone = 4
    };

    public enum Direction
    {
        NegativeY = 0,
        PositiveZ = 1
    };

    public int UID;
    public Color color = Color.white;
    public float intensity = 1.0f;
    public float shadowSpread = 0.05f;
    public float cutoff = 10.0f;
    public bool realisticFalloff = false;
    public int samples = 8;
    public ftLightProjectionMode projMode;
    public Texture2D cookie;
    public float angle = 30.0f;
    public float innerAngle = 0;
    public Cubemap cubemap;
    public UnityEngine.Object iesFile;
    public int bitmask = 1;
    public bool bakeToIndirect = false;
    public bool shadowmask = false;
    public float indirectIntensity = 1.0f;
    public float falloffMinRadius = 1.0f;
    public int shadowmaskGroupID = 0;
    public Direction directionMode = Direction.NegativeY;

    const float GIZMO_MAXSIZE = 0.1f;
    const float GIZMO_SCALE = 0.01f;
    float screenRadius = GIZMO_MAXSIZE;

    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<BakerySkyLight>() != 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 = color;
        var curCam = Camera.current;
        if (curCam != null)
        {
            screenRadius = Mathf.Min((transform.position - curCam.transform.position).magnitude * GIZMO_SCALE, GIZMO_MAXSIZE);
        }
        Gizmos.DrawSphere(transform.position, screenRadius);
    }

    void DrawArrow(Vector3 a, Vector3 b)
    {
        //const float len = 0.125f;

        b = a + b * (shadowSpread + 0.05f);
        Gizmos.DrawLine(a, b);
    }

    void OnDrawGizmosSelected()
    {
      Gizmos.color = color;//Color.yellow;
      Gizmos.DrawWireSphere(transform.position, shadowSpread);

      Gizmos.color = new Color(color.r, color.g, color.b, 0.25f);//Color.gray;
      if (projMode != ftLightProjectionMode.Cookie && projMode != ftLightProjectionMode.Cone) Gizmos.DrawWireSphere(transform.position, cutoff);

      if (projMode != 0)
      {
          Gizmos.color = color;//Color.yellow;
          Vector3 endPoint;
          if (projMode == ftLightProjectionMode.Cookie || projMode == ftLightProjectionMode.Cone)
          {
            endPoint = transform.forward * 2;
            Gizmos.DrawRay(transform.position, endPoint);

            float angle2 = (180 - angle) * Mathf.Deg2Rad * 0.5f;
            //float x = Mathf.Cos(angle2);
            //float radius = x * cutoff;

            float x = 1 / Mathf.Sin(angle2);
            x = Mathf.Sqrt(x * x - 1);
            float radius = x * cutoff;

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

                Vector3 A = transform.position + transform.forward * cutoff + transform.right * x1 * radius + transform.up * y1 * radius;
                Vector3 B = transform.position + transform.forward * cutoff + transform.right * x2 * radius + transform.up * y2 * radius;
                Gizmos.DrawLine(A, B);

                if (i % 4 == 0) Gizmos.DrawLine(transform.position, A);
            }
          }
          else
          {
            if (projMode == ftLightProjectionMode.IES && directionMode == Direction.PositiveZ)
            {
                endPoint = transform.forward * 2;
            }
            else
            {
                endPoint = -transform.up * 2;
            }
            Gizmos.DrawRay(transform.position, endPoint);
          }
          endPoint += transform.position;
          Gizmos.DrawLine(endPoint, endPoint + (transform.position + transform.right - endPoint).normalized * 0.5f);
          Gizmos.DrawLine(endPoint, endPoint + (transform.position - transform.right - endPoint).normalized * 0.5f);
          Gizmos.DrawLine(endPoint, endPoint + (transform.position + transform.up - endPoint).normalized * 0.5f);
          Gizmos.DrawLine(endPoint, endPoint + (transform.position - transform.up - endPoint).normalized * 0.5f);
      }
    }
#endif
}