BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Spatializer / editor / ONSPPropagationMaterialEditor.cs

ONSPPropagationMaterialEditor.cs

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
ONSPPropagationMaterialEditor.cs 13.8 KiB Download Raw
1/************************************************************************************
2Filename : ONSPPropagationMaterialEditor.cs
3Content : Propagation material editor class
4 Attach to geometry to define material properties
5Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.
6
7Licensed under the Oculus SDK Version 3.5 (the "License");
8you may not use the Oculus SDK except in compliance with the License,
9which is provided at the time of installation or download, or which
10otherwise accompanies this software in either electronic or hard copy form.
11
12You may obtain a copy of the License at
13
14https://developer.oculus.com/licenses/sdk-3.5/
15
16Unless required by applicable law or agreed to in writing, the Oculus SDK
17distributed under the License is distributed on an "AS IS" BASIS,
18WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19See the License for the specific language governing permissions and
20limitations under the License.
21************************************************************************************/
22using UnityEditor;
23using UnityEngine;
24
25using Spectrum = ONSPPropagationMaterial.Spectrum;
26using Point = ONSPPropagationMaterial.Point;
27
28[CustomEditor(typeof(ONSPPropagationMaterial))]
29internal sealed class ONSPPropagationMaterialEditor : Editor{
30
31 private enum AxisScale{ Lin, Log, Sqr }
32
33 private sealed class SpectrumDrawer{
34
35 private const float cutoff = 20000f;
36
37 private static readonly Texture2D texture = EditorGUIUtility.whiteTexture;
38
39 private static readonly GUIStyle textStyle = new GUIStyle{
40
41 alignment = TextAnchor.MiddleLeft,
42 clipping = TextClipping.Overflow,
43 fontSize = 8,
44 fontStyle = FontStyle.Bold,
45 wordWrap = false,
46 normal = new GUIStyleState{ textColor = Color.grey },
47 focused = new GUIStyleState{ textColor = Color.grey }
48
49 };
50
51 private static int focus;
52
53 private readonly string label;
54
55 private readonly AxisScale scale;
56 private readonly Spectrum spectrum;
57
58 private bool dragInitiated;
59 private bool isDragging;
60
61 private bool displaySpectrum;
62 private bool displayPoints;
63
64 internal bool IsFocus{
65
66 get{
67
68 return focus == spectrum.GetHashCode();
69
70 }
71
72 }
73
74 internal SpectrumDrawer(string label, Spectrum spectrum, AxisScale scale){
75
76 this.label = label;
77 this.spectrum = spectrum;
78 this.scale = scale;
79
80 }
81
82 internal void Draw(Event e){
83
84 displaySpectrum = EditorGUILayout.Foldout(displaySpectrum, label);
85
86 if(displaySpectrum){
87
88 EditorGUI.indentLevel++;
89 DrawSpectrum(e);
90
91 displayPoints = EditorGUILayout.Foldout(displayPoints, "Points");
92
93 if(displayPoints){
94
95 EditorGUI.indentLevel++;
96 DrawPoints();
97 EditorGUI.indentLevel--;
98
99 }
100
101 EditorGUI.indentLevel--;
102
103 }
104
105 }
106
107 internal void LoadFoldoutState(){
108
109 displaySpectrum = EditorPrefs.GetBool(label + "Spectrum", true);
110 displayPoints = EditorPrefs.GetBool(label + "Points", false);
111
112 }
113
114 internal void SaveFoldoutState(){
115
116 EditorPrefs.SetBool(label + "Spectrum", displaySpectrum);
117 EditorPrefs.SetBool(label + "Points", displayPoints);
118
119 }
120
121 private void DrawSpectrum(Event e){
122
123 float height = 10 * EditorGUIUtility.singleLineHeight;
124 Rect r = EditorGUILayout.GetControlRect(true, height);
125
126 r.width -= rightMargin;
127 DrawDataTicks(r);
128 r = AudioCurveRendering.BeginCurveFrame(r);
129
130 AudioCurveRendering.DrawFilledCurve(r, EvaluateCurve, AudioCurveRendering.kAudioOrange);
131
132 DrawFrequencyTicks(r);
133
134 HandleEvent(r, e);
135 if(IsFocus) DrawSelected(r);
136
137 AudioCurveRendering.EndCurveFrame();
138
139 }
140
141 private void DrawPoints(){
142
143 var points = spectrum.points;
144 int lines = points.Count > 0 ? points.Count + 2 : 1;
145 float height = EditorGUIUtility.singleLineHeight * lines;
146 Rect r1 = EditorGUILayout.GetControlRect(true, height);
147 r1.width -= rightMargin;
148 r1.height = EditorGUIUtility.singleLineHeight;
149
150 {
151
152 int oldCount = points.Count;
153 int newCount = EditorGUI.DelayedIntField(r1, "Size", oldCount);
154 r1.y += r1.height;
155
156 if(newCount < points.Count){
157
158 points.RemoveRange(newCount, oldCount - newCount);
159 Undo.SetCurrentGroupName("Points Removed");
160 GUI.changed = true;
161
162 } else if(newCount > oldCount){
163
164 if(newCount > points.Capacity)
165 points.Capacity = newCount;
166
167 for(int i = oldCount; i < newCount; i++)
168 points.Add(new Point(125 * (1 << i)));
169
170 Undo.SetCurrentGroupName("Points Added");
171 GUI.changed = true;
172
173 }
174
175 }
176
177 if(points.Count > 0){
178
179 Rect r2 = new Rect(r1.xMax + 9, r1.y + r1.height * 1.125f, 24, r1.height * .75f);
180
181 r1.width /= 2;
182 EditorGUI.LabelField(r1, "Frequency");
183 r1.x += r1.width;
184 EditorGUI.LabelField(r1, "Data");
185 r1.x -= r1.width;
186 r1.y += r1.height;
187
188 for(int i = 0; i < points.Count; i++){
189
190 points[i].frequency = EditorGUI.FloatField(r1, points[i].frequency);
191 points[i].frequency = Mathf.Clamp(points[i].frequency, 0f, cutoff);
192 r1.x += r1.width;
193 points[i].data = EditorGUI.FloatField(r1, points[i].data);
194 points[i].data = Mathf.Clamp01(points[i].data);
195 r1.x -= r1.width;
196 r1.y += r1.height;
197
198 if(GUI.Button(r2, "–")){
199
200 RemovePointAt(i);
201 break;
202
203 }
204
205 r2.y += r1.height;
206
207 }
208
209 }
210
211 }
212
213 private void DrawDataTicks(Rect r){
214
215 const int ticks = 10;
216 Rect label = new Rect(r.xMax + 9, r.y - r.height / (2 * ticks), 24, r.height / ticks);
217 Rect tick = new Rect(r.xMax + 2, r.y - 1, 4.5f, 2);
218
219 for(int i = 0; i <= ticks; i++){
220
221 float value = MapData(1 - (float)i / ticks, false);
222
223 EditorGUI.DrawRect(tick, textStyle.normal.textColor);
224 GUI.Label(label, value.ToString("0.000"), textStyle);
225 tick.y += label.height;
226 label.y += label.height;
227
228 }
229
230 }
231
232 private void DrawFrequencyTicks(Rect r){
233
234 Rect tick = new Rect(r.x, r.y, 1, r.height);
235 Rect label = new Rect(r.x, r.yMax - 1.5f * EditorGUIUtility.singleLineHeight, 32, EditorGUIUtility.singleLineHeight);
236
237 for(int i = 1; i < 30; i++){
238
239 float frequency;
240
241 if(MapFrequencyTick(i, out frequency)){
242
243 tick.x = MapFrequency(frequency) * r.width;
244 tick.height = label.y - r.y;
245 tick.width = 2;
246 EditorGUI.DrawRect(tick, textStyle.normal.textColor);
247
248 tick.y = label.yMax;
249 tick.height = r.yMax - label.yMax;
250 EditorGUI.DrawRect(tick, textStyle.normal.textColor);
251
252 label.x = tick.x - 2;
253 GUI.Label(label, FrequencyToString(frequency), textStyle);
254
255 tick.y = r.y;
256 tick.height = r.height;
257 tick.width = 1;
258
259 } else{
260
261 tick.x = MapFrequency(frequency) * r.width;
262 EditorGUI.DrawRect(tick, textStyle.normal.textColor);
263
264 }
265
266 }
267
268 }
269
270 private void DrawSelected(Rect r){
271
272 if(spectrum.points.Count > spectrum.selection){
273
274 const float radius = 12;
275 Vector2 position = MapPointPosition(r, spectrum.points[spectrum.selection]);
276 Vector2 size = new Vector2(radius, radius);
277 r = new Rect(position - size / 2, size);
278
279#if UNITY_5
280 GUI.DrawTexture(r, texture, ScaleMode.StretchToFill, false, 0);
281 GUI.DrawTexture(r, texture, ScaleMode.StretchToFill, false, 0);
282#else
283 GUI.DrawTexture(r, texture, ScaleMode.StretchToFill, false, 0, Color.white, 0, radius);
284 GUI.DrawTexture(r, texture, ScaleMode.StretchToFill, false, 0, Color.black, 2, radius);
285#endif
286
287 }
288 }
289
290 private void HandleEvent(Rect r, Event e){
291
292 Vector2 position = e.mousePosition;
293
294 switch(e.type){
295
296 case EventType.MouseDown:
297
298 if(r.Contains(position)){
299
300 if(e.clickCount == 2){
301
302 spectrum.selection = spectrum.points.Count;
303 spectrum.points.Add(MapMouseEvent(r, position));
304 Undo.SetCurrentGroupName("Point Added");
305 GUI.changed = true;
306
307 } else{
308
309 int selection = spectrum.selection;
310 float minDistance = float.MaxValue;
311
312 for(int i = 0; i < spectrum.points.Count; i++){
313
314 float distance = Vector2.Distance(MapPointPosition(r, spectrum.points[i]), position);
315
316 if(distance < minDistance){
317
318 selection = i;
319 minDistance = distance;
320
321 }
322
323 }
324
325 if(selection != spectrum.selection){
326
327 spectrum.selection = selection;
328 Undo.SetCurrentGroupName("Point Selected");
329 GUI.changed = true;
330
331 }
332
333 }
334
335 focus = spectrum.GetHashCode();
336 dragInitiated = true;
337
338 } else{
339
340 isDragging = false;
341 focus = 0;
342
343 }
344
345 e.Use();
346
347 break;
348
349 case EventType.MouseDrag:
350
351 if(dragInitiated){
352
353 dragInitiated = false;
354 isDragging = true;
355
356 }
357
358 if(isDragging && spectrum.selection < spectrum.points.Count){
359
360 spectrum.points[spectrum.selection] = MapMouseEvent(r, position);
361 e.Use();
362
363 }
364
365 break;
366
367 case EventType.Ignore:
368 case EventType.MouseUp:
369
370 dragInitiated = false;
371
372 if(isDragging){
373
374 isDragging = false;
375 Undo.SetCurrentGroupName("Point Moved");
376 GUI.changed = true;
377 e.Use();
378
379 }
380
381 break;
382
383 case EventType.KeyDown:
384
385 switch(e.keyCode){
386
387 case KeyCode.Delete:
388 case KeyCode.Backspace:
389
390 if(spectrum.selection < spectrum.points.Count){
391
392 RemovePointAt(spectrum.selection);
393 e.Use();
394
395 }
396
397 break;
398
399 }
400
401 break;
402
403 }
404
405 }
406
407 private void RemovePointAt(int index){
408
409 spectrum.points.RemoveAt(index);
410
411 if(spectrum.selection == index)
412 spectrum.selection = spectrum.points.Count;
413
414 Undo.SetCurrentGroupName("Point Removed");
415 GUI.changed = true;
416
417 }
418
419 private float EvaluateCurve(float f){
420
421 return 2 * MapData(spectrum[MapFrequency(f, false)]) - 1;
422
423 }
424
425 private Vector2 MapPointPosition(Rect r, Point point){
426
427 return new Vector2{
428
429 x = r.x + r.width * MapFrequency(point.frequency),
430 y = r.y + r.height * (1 - MapData(point.data))
431
432 };
433
434 }
435
436 private Point MapMouseEvent(Rect r, Vector2 v){
437
438 return new Point{
439
440 frequency = v.x < r.xMin ? 0 : v.x > r.xMax ? cutoff : MapFrequency((v.x - r.x) / r.width, false),
441 data = v.y < r.yMin ? 1 : v.y > r.yMax ? 0 : MapData(1 - (v.y - r.y) / r.height, false)
442
443 };
444
445 }
446
447 private float MapData(float f, bool forward = true){
448
449 switch(scale){
450
451 case AxisScale.Log:
452 return forward ? f < 1e-3f ? 0 : 1 + Mathf.Log10(f) / 3 : Mathf.Pow(10, -3 * (1 - f));
453
454 case AxisScale.Sqr:
455 return forward ? Mathf.Sqrt(f) : f * f;
456
457 default:
458 case AxisScale.Lin:
459 return f;
460
461 }
462
463 }
464
465 public static bool MapFrequencyTick(int i, out float frequency){
466
467 int power = i / 9 + 1;
468 int multiplier = i % 9 + 1;
469
470 frequency = multiplier * Mathf.Pow(10, power);
471
472 return multiplier == 1;
473
474 }
475
476 public static float MapFrequency(float f, bool forward = true){
477
478 return forward ? f < 10 ? 0 : Mathf.Log(f / 10, cutoff / 10) : 10 * Mathf.Pow(cutoff / 10, f);
479
480 }
481
482 private static string FrequencyToString(float frequency){
483
484 if(frequency < 1000)
485 return string.Format("{0:F0} Hz", frequency);
486 else
487 return string.Format("{0:F0} kHz", frequency * .001f);
488
489 }
490
491 }
492
493 private const float rightMargin = 36;
494
495 private SpectrumDrawer absorption, scattering, transmission;
496
497 private void OnEnable(){
498
499 GetSpectra(out absorption, out scattering, out transmission);
500
501 absorption.LoadFoldoutState();
502 scattering.LoadFoldoutState();
503 transmission.LoadFoldoutState();
504
505 }
506
507 private void OnDisable(){
508
509 absorption.SaveFoldoutState();
510 scattering.SaveFoldoutState();
511 transmission.SaveFoldoutState();
512
513 }
514
515 public override void OnInspectorGUI(){
516
517 ONSPPropagationMaterial material = target as ONSPPropagationMaterial;
518
519 EditorGUI.BeginChangeCheck();
520
521 Rect r = EditorGUILayout.GetControlRect();
522 r.width -= rightMargin;
523
524 ONSPPropagationMaterial.Preset newPreset =
525 (ONSPPropagationMaterial.Preset)EditorGUI.EnumPopup(r, "Preset", material.preset);
526
527 Event e = Event.current;
528 EventType type = e.type;
529 absorption.Draw(e);
530 scattering.Draw(e);
531 transmission.Draw(e);
532
533 if(EditorGUI.EndChangeCheck()){
534
535 string groupName = Undo.GetCurrentGroupName();
536
537 Undo.RegisterCompleteObjectUndo(material, groupName);
538
539 if(groupName == "Point Added")
540 Undo.CollapseUndoOperations(Undo.GetCurrentGroup() - 1);
541
542 if(material.preset != newPreset)
543 material.preset = newPreset;
544 else
545 material.preset = ONSPPropagationMaterial.Preset.Custom;
546
547 if(Application.isPlaying)
548 material.UploadMaterial();
549
550 }
551
552 }
553
554 private void GetSpectra(out SpectrumDrawer absorption,
555 out SpectrumDrawer scattering,
556 out SpectrumDrawer transmission){
557
558 ONSPPropagationMaterial material = target as ONSPPropagationMaterial;
559
560 absorption = new SpectrumDrawer("Absorption", material.absorption, AxisScale.Sqr);
561 scattering = new SpectrumDrawer("Scattering", material.scattering, AxisScale.Lin);
562 transmission = new SpectrumDrawer("Transmission", material.transmission, AxisScale.Sqr);
563
564 }
565
566}