forked from moonryul/TreeOfVoiceV2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LEDColorGenControllerCoord.cs
1153 lines (734 loc) · 50.4 KB
/
LEDColorGenControllerCoord.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.Runtime.InteropServices;
using System;
using System.IO;
using Random = UnityEngine.Random;
public class LEDColorGenControllerCoord : MonoBehaviour
{
//ComputeBuffer m_BoidLEDRenderDebugBuffer;
//ComputeBuffer m_BoidLEDRenderDebugBuffer0;
//// // ComputeBuffer(int count, int stride, ComputeBufferType type);
//BoidLEDRenderDebugData[] m_ComputeBufferArray;
//Vector4[] m_ComputeBufferArray0;
public struct BoidLEDRenderDebugData
{
// public Vector3 WallOrigin; // the reference position of the wall (the boid reference frame) on which the boid is
//public Vector3 EulerAngles; // the rotation of the boid reference frame
public int BoidLEDID; // the position of the boid in the boid reference frame
public int NearestBoidID; // the scale factors
public int NeighborCount; // heading direction of the boid on the local plane
public float NeighborRadius; // the radius of the circle boid
public Vector4 NearestBoidColor; // RGBA color
public Vector4 AvgColor; // RGBA color
}
public struct BoidLEDData
{
public List<int> LEDID; // (chainNo, ledNo)
public List<int> boxCoord; // (i,j)
public Vector2 LEDCoord; // (x,y)
public Matrix4x4 BoidFrame;
public Vector3 Position; //
public Vector3 HeadDir; // heading direction of the boid on the local plane
public Vector4 Color; // RGBA color
public Vector3 Scale;
public int WallNo; // the number of the wall whose boids defined the light sources of the branch cylinder
// 0=> the inner circular wall.
// 1 => the outer circular wall;
public int NearestBoidID;
public int NeighborCount;
}
//public struct BoidData
//{
// // public Vector3 WallOrigin; // the reference position of the wall (the boid reference frame) on which the boid is
// //public Vector3 EulerAngles; // the rotation of the boid reference frame
// public Vector3 Position; // the position of the boid in the boid reference frame
// public Vector3 Scale; // the scale factors
// public Vector3 HeadDir; // heading direction of the boid on the local plane
// public float Speed; // the speed of a boid
// public float Radius; // the radius of the circle boid
// public Vector3 ColorHSV;
// public Vector4 ColorRGB; // RGBA color
// public Vector2 SoundGrain; // soundGrain = (freq, amp)
// public float Duration; // duration of a boid each frame
// public int WallNo; // the number of the wall on which the boid lie. 0=> the ground
// // 1 => the ceiling, 2 => left wall, 3=> right wall. 4=> front wall
//}
public SimpleBoidsTreeOfVoice m_boids;
public ActionPlanController m_actionPlanController;
public int m_colorSamplingMethod = 0; // 0 = get the nearest neighbor color
// 1 = get the average color of the neighbors
public int m_totalNumOfLEDs; // computed within script
public float m_samplingRadius = 0.05f; //10cm
public float m_LEDChainHeight = 9; // the height of the LED chain
public float m_Hemisphere = 1;
// m_HemisphereGroundPosition is the reference position. If it is positive or zero,
// the hemisphere above this position will be used to sample LED colors
// It ranges from - m_MaxDomainRadius to m_DomainRadius; from the bottom of the sphere to
// the top of the sphere.
//https://www.reddit.com/r/Unity3D/comments/7ppldz/physics_simulation_on_gpu_with_compute_shader_in/
protected int m_kernelIDLED;
[SerializeField] protected ComputeShader m_BoidLEDComputeShader;
// m_BoidLEDComputeShader is set to SampleLEDColors.compute in the inspector
public ComputeBuffer m_BoidLEDBuffer { get; protected set; }
BoidLEDData[] m_BoidLEDArray;
byte[] m_LEDArray;
public float m_LEDInterval = 0.2f; // 20cm
//public int m_SphericalMotion = 0;
public float m_startingRadiusOfInnerChain = 0.1f; // m
// the startingRadiusOfInnerChain should be equal to the CeilingInnerRadius of SimpleBoidsForTreeOfVoice
public float m_endingRadiusOfInnerChainThreeTurns = 0.8f;
public float m_startingRadiusOfOuterChain = 1.4f; // m
public float m_endingRadiusOfOuterChainThreeTurns = 2f;
// public float m_MaxDomainRadius = 10; // 10
//public float m_MinDomainRadius = 0.7f; // 0.7
//public float m_MaxChainRadius = 2; // 2 m
//public float m_MinChainRadius = 0.7f; // 0.7 m
//cf. public float CeilingInnerRadius = 0.7f;
public int m_firstChain = 40;
public int m_secondChain = 44;
public int m_thirdChain = 50;
public int m_fourthChain = 52;
public float m_beginFromInChain1 = -135f;
public float m_beginFromInChain2 = -70f;
float m_startAngleOfChain1; // this is the angle where r0, that is, a0 is defined, that is, on the local x axis
float m_startAngleOfChain2;
// delegate signature (interface definition)
public delegate void LEDSenderHandler(byte[] m_LEDArray);
public event LEDSenderHandler m_LEDSenderHandler;
protected const int BLOCK_SIZE = 256; // The number of threads in a single thread group
//protected const int MAX_SIZE_OF_BUFFER = 1000;
int m_threadGroupSize;
const float epsilon = 1e-2f;
const float M_PI = 3.1415926535897932384626433832795f;
//m_neuroHeadSetController.onAverageSignalReceived += m_ledColorGenController.UpdateLEDResponseParameter;
//m_irSensorMasterController.onAverageSignalReceived += m_ledColorGenController.UpdateColorBrightnessParameter;
StreamWriter m_writer;
FileStream m_oStream;
string m_pathToCoord;
private void Awake()
{// initialize me
//string path = @"c:\temp\MyTest.txt";
//장열 '\' (백슬래쉬) 를 문자열 내에서 두번 써야 하지만 @ 를 써서 한번만 쓰도록 합니다.
//// DEBUG code
//string fileName = "LEDBoidGen";
////"yyyy.MM.dd.HH.mm.ss"
//string fileIndex = System.DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss");
m_pathToCoord = "Assets/Resources/LEDCoordFile.txt";
//File.CreateText(path).Dispose();
////FileStream fileStream = new FileStream(@"file_no.txt",
//// FileMode.OpenOrCreate,
//// FileAccess.ReadWrite,
//// FileShare.None);
////Write some text to the test.txt file
// m_writer = new StreamWriter(path, false); // do not append
//m_ioStream = new FileStream(path,
// FileMode.OpenOrCreate,
// FileAccess.ReadWrite,
// FileShare.None);
//m_oStream = new FileStream(path, FileMode.Append, FileAccess.Write, FileShare.None);
//m_writer = new System.IO.StreamWriter(m_oStream);
m_totalNumOfLEDs = m_firstChain + m_secondChain
+ m_thirdChain + m_fourthChain;
m_startAngleOfChain1 = m_beginFromInChain1 * M_PI / 180; // degree
m_startAngleOfChain2 = m_beginFromInChain2 * M_PI / 180; // degree
//m_threadGroupSize = Mathf.CeilToInt(m_BoidsNum / (float)BLOCK_SIZE);
m_threadGroupSize = Mathf.CeilToInt(m_totalNumOfLEDs / (float)BLOCK_SIZE);
m_LEDArray = new byte[m_totalNumOfLEDs * 3 * 2];
//m_boidArray = new BoidData[ (int) m_BoidsNum ]; // for debugging
if (m_BoidLEDComputeShader == null)
{
Debug.LogError("BoidLEDComputeShader should be set in the inspector");
#if UNITY_EDITOR
// Application.Quit() does not work in the editor so
// UnityEditor.EditorApplication.isPlaying = false;
UnityEditor.EditorApplication.Exit(0);
#else
Application.Quit();
#endif
}
m_kernelIDLED = m_BoidLEDComputeShader.FindKernel("SampleLEDColors");
// m_BoidLEDRenderDebugBuffer = new ComputeBuffer(m_totalNumOfLEDs,
// 4 * sizeof(float), ComputeBufferType.Default);
// Type of the buffer, default is ComputeBufferType.Default (structured buffer)
//m_BoidLEDRenderDebugBuffer = new ComputeBuffer(m_totalNumOfLEDs, Marshal.SizeOf(typeof(BoidLEDRenderDebugData)));
//m_BoidLEDRenderDebugBuffer0 = new ComputeBuffer(m_totalNumOfLEDs, Marshal.SizeOf(typeof(Vector4)));
//// Set the ComputeBuffer for shader debugging
//// But a RWStructuredBuffer, requires SetRandomWriteTarget to work at all in a non-compute-shader.
////This is all Unity API magic which in some ways is convenient
//Graphics.SetRandomWriteTarget(1, m_BoidLEDRenderDebugBuffer);
// m_ComputeBufferArray = new BoidLEDRenderDebugData[m_totalNumOfLEDs];
//m_ComputeBufferArray0 = new Vector4[m_totalNumOfLEDs];
//m_BoidLEDRenderDebugBuffer.SetData(m_ComputeBufferArray);
//m_BoidLEDRenderDebugBuffer0.SetData(m_ComputeBufferArray0);
Debug.Log("In Awake() in LEDColorGenController:");
//for (int i = 0; i < m_totalNumOfLEDs; i++)
//{
// Debug.Log(i + "th LED Position" + m_BoidLEDArray[i].Position);
// Debug.Log(i + "th LED HeadDir" + m_BoidLEDArray[i].HeadDir);
// Debug.Log(i + "th LED Color" + m_BoidLEDArray[i].Color);
// Debug.Log(i + "th LED Color: NeighborCount" + m_BoidLEDArray[i].NeighborCount);
//}
// m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidLEDRenderDebugBuffer", m_BoidLEDRenderDebugBuffer);
//m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidLEDRenderDebugBuffer0", m_BoidLEDRenderDebugBuffer0);
} // Awake()
void Start()
{
//initialize others
m_boids = this.gameObject.GetComponent<SimpleBoidsTreeOfVoice>();
//m_BoidBuffer = m_boids.m_BoidBuffer;
if (m_boids == null)
{
Debug.LogError("SimpleBoidsTreeOfVoice component should be added to CommHub");
// Application.Quit();
#if UNITY_EDITOR
// Application.Quit() does not work in the editor so
// UnityEditor.EditorApplication.isPlaying = false;
UnityEditor.EditorApplication.Exit(0);
#else
Application.Quit();
#endif
}
m_BoidLEDComputeShader.SetFloat("_CeilingInnerRadius", m_startingRadiusOfInnerChain);
m_BoidLEDComputeShader.SetFloat("_MaxChainRadius", m_endingRadiusOfOuterChainThreeTurns);
m_BoidLEDComputeShader.SetFloat("_Hemisphere", m_Hemisphere);
m_BoidLEDComputeShader.SetFloat("_MaxDomainRadius", m_boids.m_MaxDomainRadius);
// m_BoidLEDComputeShader.SetFloat("_MinDomainRadius", m_boids.m_MinDomainRadius);
m_BoidLEDComputeShader.SetFloat("_CeilingInnerRadius", m_boids.m_CeilingInnerRadius);
//m_BoidsNum = (int)m_boids.m_BoidsNum;
m_BoidLEDComputeShader.SetInt("_BoidsNum", (int)m_boids.m_BoidsNum);
m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidBuffer", m_boids.m_BoidBuffer);
m_BoidLEDComputeShader.SetInt("_ColorSamplingMethod", m_colorSamplingMethod);
m_BoidLEDComputeShader.SetFloat("_SamplingRadius", m_samplingRadius);
//define BoidLED Buffer
m_BoidLEDBuffer = new ComputeBuffer(m_totalNumOfLEDs, Marshal.SizeOf(typeof(BoidLEDData)));
m_BoidLEDArray = new BoidLEDData[m_totalNumOfLEDs]; // read info from LEDCoordFile
//For each kernel we are setting the buffers that are used by the kernel, so it would read and write to those buffers
// For the part of boidArray that is set by data are filled by null.
// When the array boidArray is created each element is set by null.
// Read the coordinates of each LED from the text file.
string[] lines = File.ReadAllLines( m_pathToCoord );
Debug.Assert(lines.Length == m_totalNumOfLEDs,
"the lines of the CoordFile should be equal to the number of LEDs");
int LEDindex = 0;
//public struct BoidLEDData
//{
// public Vector2 LEDID; // (chainNo, ledNo)
// public Vector2 boxCoord; // (i,j)
// public Vector2 LEDCoord; // (x,y)
// public Matrix4x4 BoidFrame;
// public Vector3 Position; //
// public Vector3 HeadDir; // heading direction of the boid on the local plane
// public Vector4 Color; // RGBA color
// public Vector3 Scale;
// public int WallNo; // the number of the wall whose boids defined the light sources of the branch cylinder
// // 0=> the inner circular wall.
// // 1 => the outer circular wall;
// public int NearestBoidID;
// public int NeighborCount;
//}
foreach (string line in lines) // line = (3,43):((0,2),(7,5.8))
{
string[] col = line.Split(':'); // col[0] = (3,43); col[1] = ( (0,2), (7,5.8) )
string[] LEDID = col[0].Split(','); // ledID = [3, 43];
string[] globalCoord = col[1].Split(','); // globalCoord = [ (0,2), (7,5.8) ]
string[] boxID = globalCoord[0].Split(','); // frame = [0,2]
string[] localCoord = globalCoord[1].Split(','); // localCoord = [7, 5.8]
m_BoidLEDArray[LEDindex].LEDID = new List<int> {System.Convert.ToInt32(LEDID[0]),
System.Convert.ToInt32(LEDID[1]) };
// LEDID starts from (1,1)
//
m_BoidLEDArray[LEDindex].boxCoord = new List<int> { System.Convert.ToInt32(boxID[0]),
System.Convert.ToInt32(boxID[1]) };
// boxCoord starts from 0
m_BoidLEDArray[LEDindex].LEDCoord = new Vector2(System.Convert.ToInt32(localCoord[0]),
System.Convert.ToInt32(localCoord[1]));
// LEDCoord starts from 0
LEDindex++;
}// foreach (string line in lines)
// boxNumx = Convert.ToInt32( frame[0]);
// boxNumy = Convert.ToInt32( frame[1]);
// x= Convert.ToSingle( localCoord[0] );
// y = Convert.ToSingle( localCoord[1] );
// // process col[0], col[1], col[2]
//}
// System.Convert.ToInt32(string)
// System.Convert.ToSingle(string)
// create a m_BoidLEDArray to link to m_BoidLEDBuffer:
SetBoidLEDArray(m_BoidLEDArray); // THe Boid LEDs array is defined without their colors
m_BoidLEDBuffer.SetData(m_BoidLEDArray); // buffer is R or RW
m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidLEDBuffer", m_BoidLEDBuffer);
}// void Start()
public void OnValidate()
{
}
protected void SetBoidLEDArray(BoidLEDData[] m_BoidLEDArray)
{
float radius;
float theta, phi;
// Arange a chain of 40 LEDs (with interval of 50cm) along the the logarithmic spiral
// r = a * exp( b * theta), where 0 <= theta <= 3 * 2pi:
// band with radii 1m and 0.85m three rounds. Arrange another chain along the circle band
// with radii 0.85m and 0.7m three rounds, each round with differnt radii.
//Two chains are arranged so that their LEDs are placed in a zigzag manner.
//x = r*cos(th); y = r *sin(th)
// x = a*exp(b*th)cos(th), y = a*exp(b*th)sin(th)
// dr/dth = b*r;
//conditions: r0 = a exp( b 0) = 0.85; r1 = a exp( b* 3 * 2pi)
// r0 = a exp(0) = a; a = r0; r1 = 0.85 * exp( b* 4pi) ==> b = the radius growth rate.
// exp( b * 4pi) = r1/ a; b * 6pi = ln( r1/a). b = ln( r1/a) / 6pi;
// L( r(th), th0, th) = a( root( 1 + b^2) /b ) [ exp( b * th) - exp( b * th0) ]
// = root(1 + b^2) / b * [ a*exp(b *th) - a * exp(th0) ]
// L( r(th), th0, th_i) = root(1 + b^2)/b * [ r(th_i) - r(th0)] = i * 0.5, 0.5= led Interval
// => the value of th_i can be determined.
// The ith LED will be placed at location (r_i, th_i) such that L(r(th_i), th0, th_i) = 0.5 * i, r_i = a*exp(b*th_i),
// i =0 ~ 39
//Define the parameters a and b of the logarithmic spiral curve r = a * exp(b * th).
float r0 = m_startingRadiusOfInnerChain; // a1 in r = a1 * exp(b1 * th) is set so that the radius r0 is 0.7 when th =0;
float r1 = m_endingRadiusOfInnerChainThreeTurns; // r1 = a1 exp (b1* 3 * 2pi)
float r2 = m_startingRadiusOfOuterChain; ; // a2 in r = a2 * exp( b2 * th); b2 is set so that r is r2 when th =0;
float r3 = m_endingRadiusOfOuterChainThreeTurns; // r3 = a2* exp(b2* 3 * 2pi)
float a1 = r0;
float b1 = Mathf.Log(r1 / a1) / (6 * M_PI);
float a2 = r2;
float b2 = Mathf.Log(r3 / a2) / (6 * M_PI);
Debug.Log("Inner Chain:");
float LEDChainLength = 0;
for (int i = 0; i < m_firstChain; i++)
{
// set the head direction of the boid: direction angle on xz plane
// thi_i: the angle on the local coordinate system:
float th_i = GetAngularPositionOfLED(a1, b1, 0.0f, ref LEDChainLength,
m_LEDInterval, i);
float r_i = a1 * Mathf.Exp(b1 * th_i);
float th_i_g = th_i + (M_PI / 180) * m_beginFromInChain1;
//Debug.Log(i + "th LED Ploar POS (th,r) [global coord]:" + new Vector2(th_i_g * 180 / M_PI, r_i).ToString("F4"));
m_BoidLEDArray[i].Position = new Vector3(r_i * Mathf.Cos(th_i_g), m_LEDChainHeight, r_i * Mathf.Sin(th_i_g));
// Set the rotation frame of LED boid at m_boidLEDArray[i].Position:
// Each boid is located on on the tangent of the sphere moving foward on the tagent plane;
// The boid frame follows the Unity convention where the x is to the right on the tangent plane,
// the z is forward direction on the tangent plane, and the y axis is the up direction, which is
// perpendicular to the tangent plane;
// The up (y, back side) direction of the boid is negative to the normal of the 2D circle mesh
// of the boid; The normal direction points to the center of the sphere; The light is within
// the sphere.
// The "forward" in OpenGL is "-z".In Unity forward is "+z".Most hand - rules you might know from math are inverted in Unity
// .For example the cross product usually uses the right hand rule c = a x b where a is thumb, b is index finger and c is the middle
// finger.In Unity you would use the same logic, but with the left hand.
// However this does not affect the projection matrix as Unity uses the OpenGL convention for the projection matrix.
// The required z - flipping is done by the cameras worldToCameraMatrix.
// So the projection matrix should look the same as in OpenGL.
// Compute the Unity affine frame for each boid on the sphere (uses the left hand rule).
// The direction of the Position is used as the up direction [Y axis) (points to the dorsal part)
// of the boid. The Z axis is set to the perpendicular to the plane formed by the Y axis
// and the world UP vector Vector3.up(0,1,0), and points the local forward of the boid.
// The z axis is the head (moving) direction of the boid. The X axis is the local right
// of the 2D circle boid.
Vector3 position = m_BoidLEDArray[i].Position;
Vector3 YAxis = position.normalized; // The direction vector of the boid
// is considered as the up vector of the boid frame.
Vector3 XAxis = Vector3.Cross(YAxis, Vector3.up); // XAxis = perpendicular to the
// plane formed by the boid up and the global up. It is the rightward basis
// left hand rule
//
Vector3 ZAxis = Vector3.Cross(XAxis, YAxis); // the forward direction
Matrix4x4 boidFrame = new Matrix4x4();
// XAxis, YAxis, ZAxis become the first, second, third columns of the boidFrame matrix
boidFrame.SetColumn(0, new Vector4(XAxis[0], XAxis[1], XAxis[2], 0.0f));
boidFrame.SetColumn(1, new Vector4(YAxis[0], YAxis[1], YAxis[2], 0.0f));
boidFrame.SetColumn(2, new Vector4(ZAxis[0], ZAxis[1], ZAxis[2], 0.0f));
boidFrame.SetColumn(3, new Vector4(position[0], position[1], position[2], 1.0f));
m_BoidLEDArray[i].BoidFrame = boidFrame; // affine frame
m_BoidLEDArray[i].HeadDir = ZAxis;
float initRadiusX = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius); // 0.1 ~ 0.3
float initRadiusY = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
float initRadiusZ = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
// m_BoidLEDArray[i].Scale = new Vector3(initRadiusX, initRadiusY, initRadiusZ);
m_BoidLEDArray[i].Scale = new Vector3(initRadiusX, initRadiusX, initRadiusX);
//m_writer.WriteLine( i+ "th LED POS:" + m_BoidLEDArray[i].Position);
//m_writer.WriteLine(i + "th LED frame:\n" + m_BoidLEDArray[i].BoidFrame);
} // for (int i )
//Debug.Log("Second Chain:");
//for (int i = 0; i < m_numOfChain2; i++)
//{
// // set the head direction of the boid: direction angle on xz plane
// float th_i = GetAngularPositionOfLED(a1, b1, m_startAngleOfChain2, ledInterval,i);
// float r_i = a1 * Mathf.Exp(b1 * th_i);
// Debug.Log(i + "th LED Ploar POS (th,r):" + (new Vector2(th_i * 180 / M_PI, r_i) ).ToString("F4") );
// m_BoidLEDArray[m_numOfChain1 + i].HeadDir = new Vector3(Mathf.Cos(th_i), 0.0f, Mathf.Sin(th_i));
// Debug.Log(i + "th LED HeadDir:" + m_BoidLEDArray[m_numOfChain1 + i].HeadDir.ToString("F4"));
// Vector3 ledPos = r_i * m_BoidLEDArray[m_numOfChain1 + i].HeadDir;
// m_BoidLEDArray[m_numOfChain1 + i].Position = ledPos;
// float initScaleX = Random.Range(MinCylinderRadius, MaxCylinderRadius); // 0.5 ~ 1.0
// //float initScaleY = Random.Range(MinCylinderRadius, MaxCylinderRadius);
// //float initScaleZ = Random.Range(MinCylinderRadius, MaxCylinderRadius);
// m_BoidLEDArray[m_numOfChain1 + i].Scale = new Vector3(initScaleX, initScaleX, initScaleX);
// Debug.Log(i + "th LED POS:" + ledPos.ToString("F4") );
//} // for (int i )
Debug.Log("Outer Chain:");
LEDChainLength = 0;
for (int i = 0; i < m_secondChain + m_thirdChain + m_fourthChain; i++)
{
// set the head direction of the boid: direction angle on xz plane
float th_i = GetAngularPositionOfLED(a2, b2, 0.0f, ref LEDChainLength,
m_LEDInterval, i);
float r_i = a2 * Mathf.Exp(b2 * th_i);
float th_i_g = th_i + (M_PI / 180) * m_beginFromInChain2;
// Debug.Log(i + "th LED Ploar POS (th,r):" + new Vector2(th_i_g * 180 / M_PI, r_i).ToString("F4"));
m_BoidLEDArray[m_firstChain + i].Position = new Vector3(r_i * Mathf.Cos(th_i_g), m_LEDChainHeight, r_i * Mathf.Sin(th_i_g));
// Set the rotation frame of LED boid at m_boidLEDArray[i].Position:
// Each boid is located on on the tangent of the sphere moving foward on the tagent plane;
// The boid frame follows the Unity convention where the x is to the right on the tangent plane,
// the z is forward direction on the tangent plane, and the y axis is the up direction, which is
// perpendicular to the tangent plane;
// The up (y, back side) direction of the boid is negative to the normal of the 2D circle mesh
// of the boid; The normal direction points to the center of the sphere; The light is within
// the sphere.
// The "forward" in OpenGL is "-z".In Unity forward is "+z".Most hand - rules you might know from math are inverted in Unity
// .For example the cross product usually uses the right hand rule c = a x b where a is thumb, b is index finger and c is the middle
// finger.In Unity you would use the same logic, but with the left hand.
// However this does not affect the projection matrix as Unity uses the OpenGL convention for the projection matrix.
// The required z - flipping is done by the cameras worldToCameraMatrix.
// So the projection matrix should look the same as in OpenGL.
// Compute the Unity affine frame for each boid on the sphere (uses the left hand rule).
// The direction of the Position is used as the up direction [Y axis) (points to the dorsal part)
// of the boid. The Z axis is set to the perpendicular to the plane formed by the Y axis
// and the world UP vector Vector3.up(0,1,0), and points the local forward of the boid.
// The z axis is the head (moving) direction of the boid. The X axis is the local right
// of the 2D circle boid.
Vector3 position = m_BoidLEDArray[i].Position;
Vector3 YAxis = position.normalized; // The direction vector of the boid
// is considered as the local up vector of the boid frame.
Vector3 ZAxis = Vector3.Cross(YAxis, Vector3.up); // XAxis = perpendicular to the
// plane formed by the boid up and the global up.
// It is tangent to the surface of sphere, and used as the forward head direction
// of the boid.
//
Vector3 XAxis = Vector3.Cross(YAxis, ZAxis); // the side (rightward) direction of the
// boid
Vector4 col0 = new Vector4(XAxis[0], XAxis[1], XAxis[2], 0.0f);
Vector4 col1 = new Vector4(YAxis[0], YAxis[1], YAxis[2], 0.0f);
Vector4 col2 = new Vector4(ZAxis[0], ZAxis[1], ZAxis[2], 0.0f);
Vector4 col3 = new Vector4(position[0], position[1], position[2], 1.0f);
Matrix4x4 boidFrame = new Matrix4x4(col0, col1, col2, col3);
m_BoidLEDArray[m_firstChain + i].BoidFrame = boidFrame;
m_BoidLEDArray[m_firstChain + i].HeadDir = ZAxis;
float initRadiusX = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius); // 0.1 ~ 0.3
float initRadiusY = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
float initRadiusZ = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
m_BoidLEDArray[m_firstChain + i].Scale = new Vector3(initRadiusX, initRadiusX, initRadiusX);
//m_writer.WriteLine( (m_firstChain + i) + "th LED POS:" + m_BoidLEDArray[i].Position);
//m_writer.WriteLine( (m_firstChain + i) + "th LED frame:" + m_BoidLEDArray[i].BoidFrame);
// Debug.Log(i + "th LED POS:" + ledPos.ToString("F4"));
float radius;
float theta, phi;
Arange a chain of 40 LEDs(with interval of 50cm) along the the logarithmic spiral
r = a * exp(b * theta), where 0 <= theta <= 3 * 2pi:
band with radii 1m and 0.85m three rounds. Arrange another chain along the circle band
with radii 0.85m and 0.7m three rounds, each round with differnt radii.
Two chains are arranged so that their LEDs are placed in a zigzag manner.
x = r * cos(th); y = r * sin(th)
x = a * exp(b * th)cos(th), y = a * exp(b * th)sin(th)
dr / dth = b * r;
conditions: r0 = a exp(b 0) = 0.85; r1 = a exp(b * 3 * 2pi)
r0 = a exp(0) = a; a = r0; r1 = 0.85 * exp(b * 4pi) ==> b = the radius growth rate.
exp(b * 4pi) = r1 / a; b * 6pi = ln(r1 / a).b = ln(r1 / a) / 6pi;
L(r(th), th0, th) = a(root(1 + b ^ 2) / b)[exp(b * th) - exp(b * th0)]
= root(1 + b ^ 2) / b * [a * exp(b * th) - a * exp(th0)]
L(r(th), th0, th_i) = root(1 + b ^ 2) / b * [r(th_i) - r(th0)] = i * 0.5, 0.5 = led Interval
=> the value of th_i can be determined.
The ith LED will be placed at location(r_i, th_i) such that L(r(th_i), th0, th_i) = 0.5 * i, r_i = a * exp(b * th_i),
i = 0 ~39
Define the parameters a and b of the logarithmic spiral curve r = a * exp(b * th).
float r0 = m_startingRadiusOfInnerChain; // a1 in r = a1 * exp(b1 * th) is set so that the radius r0 is 0.7 when th =0;
float r1 = m_endingRadiusOfInnerChainThreeTurns; // r1 = a1 exp (b1* 3 * 2pi)
float r2 = m_startingRadiusOfOuterChain; ; // a2 in r = a2 * exp( b2 * th); b2 is set so that r is r2 when th =0;
float r3 = m_endingRadiusOfOuterChainThreeTurns; // r3 = a2* exp(b2* 3 * 2pi)
float a1 = r0;
float b1 = Mathf.Log(r1 / a1) / (6 * M_PI);
float a2 = r2;
float b2 = Mathf.Log(r3 / a2) / (6 * M_PI);
//Debug.Log("Inner Chain:");
//float LEDChainLength = 0;
//for (int i = 0; i < m_firstChain; i++)
//{
// // set the head direction of the boid: direction angle on xz plane
// // thi_i: the angle on the local coordinate system:
// float th_i = GetAngularPositionOfLED(a1, b1, 0.0f, ref LEDChainLength,
// m_LEDInterval, i);
// float r_i = a1 * Mathf.Exp(b1 * th_i);
// float th_i_g = th_i + (M_PI / 180) * m_beginFromInChain1;
// //Debug.Log(i + "th LED Ploar POS (th,r) [global coord]:" + new Vector2(th_i_g * 180 / M_PI, r_i).ToString("F4"));
// m_BoidLEDArray[i].Position = new Vector3(r_i * Mathf.Cos(th_i_g), m_LEDChainHeight, r_i * Mathf.Sin(th_i_g));
// // Set the rotation frame of LED boid at m_boidLEDArray[i].Position:
// // Each boid is located on on the tangent of the sphere moving foward on the tagent plane;
// // The boid frame follows the Unity convention where the x is to the right on the tangent plane,
// // the z is forward direction on the tangent plane, and the y axis is the up direction, which is
// // perpendicular to the tangent plane;
// // The up (y, back side) direction of the boid is negative to the normal of the 2D circle mesh
// // of the boid; The normal direction points to the center of the sphere; The light is within
// // the sphere.
// // The "forward" in OpenGL is "-z".In Unity forward is "+z".Most hand - rules you might know from math are inverted in Unity
// // .For example the cross product usually uses the right hand rule c = a x b where a is thumb, b is index finger and c is the middle
// // finger.In Unity you would use the same logic, but with the left hand.
// // However this does not affect the projection matrix as Unity uses the OpenGL convention for the projection matrix.
// // The required z - flipping is done by the cameras worldToCameraMatrix.
// // So the projection matrix should look the same as in OpenGL.
// // Compute the Unity affine frame for each boid on the sphere (uses the left hand rule).
// // The direction of the Position is used as the up direction [Y axis) (points to the dorsal part)
// // of the boid. The Z axis is set to the perpendicular to the plane formed by the Y axis
// // and the world UP vector Vector3.up(0,1,0), and points the local forward of the boid.
// // The z axis is the head (moving) direction of the boid. The X axis is the local right
// // of the 2D circle boid.
// Vector3 position = m_BoidLEDArray[i].Position;
// Vector3 YAxis = position.normalized; // The direction vector of the boid
// // is considered as the up vector of the boid frame.
// Vector3 XAxis = Vector3.Cross(YAxis, Vector3.up); // XAxis = perpendicular to the
// // plane formed by the boid up and the global up. It is the rightward basis
// // left hand rule
// //
// Vector3 ZAxis = Vector3.Cross(XAxis, YAxis); // the forward direction
// Matrix4x4 boidFrame = new Matrix4x4();
// // XAxis, YAxis, ZAxis become the first, second, third columns of the boidFrame matrix
// boidFrame.SetColumn(0, new Vector4(XAxis[0], XAxis[1], XAxis[2], 0.0f));
// boidFrame.SetColumn(1, new Vector4(YAxis[0], YAxis[1], YAxis[2], 0.0f));
// boidFrame.SetColumn(2, new Vector4(ZAxis[0], ZAxis[1], ZAxis[2], 0.0f));
// boidFrame.SetColumn(3, new Vector4(position[0], position[1], position[2], 1.0f));
// m_BoidLEDArray[i].BoidFrame = boidFrame; // affine frame
// m_BoidLEDArray[i].HeadDir = ZAxis;
// float initRadiusX = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius); // 0.1 ~ 0.3
// float initRadiusY = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
// float initRadiusZ = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
// // m_BoidLEDArray[i].Scale = new Vector3(initRadiusX, initRadiusY, initRadiusZ);
// m_BoidLEDArray[i].Scale = new Vector3(initRadiusX, initRadiusX, initRadiusX);
// //m_writer.WriteLine( i+ "th LED POS:" + m_BoidLEDArray[i].Position);
// //m_writer.WriteLine(i + "th LED frame:\n" + m_BoidLEDArray[i].BoidFrame);
//} // for (int i )
////Debug.Log("Second Chain:");
////for (int i = 0; i < m_numOfChain2; i++)
////{
//// // set the head direction of the boid: direction angle on xz plane
//// float th_i = GetAngularPositionOfLED(a1, b1, m_startAngleOfChain2, ledInterval,i);
//// float r_i = a1 * Mathf.Exp(b1 * th_i);
//// Debug.Log(i + "th LED Ploar POS (th,r):" + (new Vector2(th_i * 180 / M_PI, r_i) ).ToString("F4") );
//// m_BoidLEDArray[m_numOfChain1 + i].HeadDir = new Vector3(Mathf.Cos(th_i), 0.0f, Mathf.Sin(th_i));
//// Debug.Log(i + "th LED HeadDir:" + m_BoidLEDArray[m_numOfChain1 + i].HeadDir.ToString("F4"));
//// Vector3 ledPos = r_i * m_BoidLEDArray[m_numOfChain1 + i].HeadDir;
//// m_BoidLEDArray[m_numOfChain1 + i].Position = ledPos;
//// float initScaleX = Random.Range(MinCylinderRadius, MaxCylinderRadius); // 0.5 ~ 1.0
//// //float initScaleY = Random.Range(MinCylinderRadius, MaxCylinderRadius);
//// //float initScaleZ = Random.Range(MinCylinderRadius, MaxCylinderRadius);
//// m_BoidLEDArray[m_numOfChain1 + i].Scale = new Vector3(initScaleX, initScaleX, initScaleX);
//// Debug.Log(i + "th LED POS:" + ledPos.ToString("F4") );
////} // for (int i )
//Debug.Log("Outer Chain:");
//LEDChainLength = 0;
//for (int i = 0; i < m_secondChain + m_thirdChain + m_fourthChain; i++)
//{
// // set the head direction of the boid: direction angle on xz plane
// float th_i = GetAngularPositionOfLED(a2, b2, 0.0f, ref LEDChainLength,
// m_LEDInterval, i);
// float r_i = a2 * Mathf.Exp(b2 * th_i);
// float th_i_g = th_i + (M_PI / 180) * m_beginFromInChain2;
// // Debug.Log(i + "th LED Ploar POS (th,r):" + new Vector2(th_i_g * 180 / M_PI, r_i).ToString("F4"));
// m_BoidLEDArray[m_firstChain + i].Position = new Vector3(r_i * Mathf.Cos(th_i_g), m_LEDChainHeight, r_i * Mathf.Sin(th_i_g));
// // Set the rotation frame of LED boid at m_boidLEDArray[i].Position:
// // Each boid is located on on the tangent of the sphere moving foward on the tagent plane;
// // The boid frame follows the Unity convention where the x is to the right on the tangent plane,
// // the z is forward direction on the tangent plane, and the y axis is the up direction, which is
// // perpendicular to the tangent plane;
// // The up (y, back side) direction of the boid is negative to the normal of the 2D circle mesh
// // of the boid; The normal direction points to the center of the sphere; The light is within
// // the sphere.
// // The "forward" in OpenGL is "-z".In Unity forward is "+z".Most hand - rules you might know from math are inverted in Unity
// // .For example the cross product usually uses the right hand rule c = a x b where a is thumb, b is index finger and c is the middle
// // finger.In Unity you would use the same logic, but with the left hand.
// // However this does not affect the projection matrix as Unity uses the OpenGL convention for the projection matrix.
// // The required z - flipping is done by the cameras worldToCameraMatrix.
// // So the projection matrix should look the same as in OpenGL.
// // Compute the Unity affine frame for each boid on the sphere (uses the left hand rule).
// // The direction of the Position is used as the up direction [Y axis) (points to the dorsal part)
// // of the boid. The Z axis is set to the perpendicular to the plane formed by the Y axis
// // and the world UP vector Vector3.up(0,1,0), and points the local forward of the boid.
// // The z axis is the head (moving) direction of the boid. The X axis is the local right
// // of the 2D circle boid.
// Vector3 position = m_BoidLEDArray[i].Position;
// Vector3 YAxis = position.normalized; // The direction vector of the boid
// // is considered as the local up vector of the boid frame.
// Vector3 ZAxis = Vector3.Cross(YAxis, Vector3.up); // XAxis = perpendicular to the
// // plane formed by the boid up and the global up.
// // It is tangent to the surface of sphere, and used as the forward head direction
// // of the boid.
// //
// Vector3 XAxis = Vector3.Cross(YAxis, ZAxis); // the side (rightward) direction of the
// // boid
// Vector4 col0 = new Vector4(XAxis[0], XAxis[1], XAxis[2], 0.0f);
// Vector4 col1 = new Vector4(YAxis[0], YAxis[1], YAxis[2], 0.0f);
// Vector4 col2 = new Vector4(ZAxis[0], ZAxis[1], ZAxis[2], 0.0f);
// Vector4 col3 = new Vector4(position[0], position[1], position[2], 1.0f);
// Matrix4x4 boidFrame = new Matrix4x4(col0, col1, col2, col3);
// m_BoidLEDArray[m_firstChain + i].BoidFrame = boidFrame;
// m_BoidLEDArray[m_firstChain + i].HeadDir = ZAxis;
// float initRadiusX = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius); // 0.1 ~ 0.3
// float initRadiusY = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
// float initRadiusZ = Random.Range(m_boids.MinBoidRadius, m_boids.MaxBoidRadius);
// m_BoidLEDArray[m_firstChain + i].Scale = new Vector3(initRadiusX, initRadiusX, initRadiusX);
// //m_writer.WriteLine( (m_firstChain + i) + "th LED POS:" + m_BoidLEDArray[i].Position);
// //m_writer.WriteLine( (m_firstChain + i) + "th LED frame:" + m_BoidLEDArray[i].BoidFrame);
// // Debug.Log(i + "th LED POS:" + ledPos.ToString("F4"));
//} // for (int i )
//} // for (int i )
// m_writer.Close();
//Debug.Log("Fourth Chain:");
//for (int i = 0; i < m_numOfChain4; i++)
//{
// // set the head direction of the boid: direction angle on xz plane
// float th_i = GetAngularPositionOfLED(a2, b2, m_startAngleOfChain4, ledInterval,i);
// float r_i = a2 * Mathf.Exp(b2 * th_i);
// Debug.Log(i + "th LED Ploar POS (th,r):" + new Vector2( th_i * 180 / M_PI, r_i).ToString("F4"));
// m_BoidLEDArray[m_numOfChain1 + m_numOfChain2 + m_numOfChain3 + i].HeadDir = new Vector3(Mathf.Cos(th_i), 0.0f, Mathf.Sin(th_i));
// Debug.Log(i + "th LED HeadDir:" + m_BoidLEDArray[m_numOfChain1 + m_numOfChain2 + m_numOfChain3 + i].HeadDir.ToString("F4"));
// Vector3 ledPos = r_i * m_BoidLEDArray[m_numOfChain1 + +m_numOfChain2 + m_numOfChain3 + i].HeadDir;
// m_BoidLEDArray[m_numOfChain1 + +m_numOfChain2 + m_numOfChain3 + i].Position = ledPos;
// float initScaleX = Random.Range(MinCylinderRadius, MaxCylinderRadius); // 0.5 ~ 1.0
// //float initScaleY = Random.Range(MinCylinderRadius, MaxCylinderRadius);
// //float initScaleZ = Random.Range(MinCylinderRadius, MaxCylinderRadius);
// m_BoidLEDArray[m_numOfChain1 + m_numOfChain2 + m_numOfChain3 + i].Scale = new Vector3(initScaleX, initScaleX, initScaleX);
// Debug.Log(i + "th LED POS:" + ledPos.ToString("F4"));
//} // for (int i )
} // SetBoidLEDArray()
// Get th_i for the ith LED along the sprial curve r = a * exp(b*th_i)
float GetAngularPositionOfLED(float a, float b, float th0, ref float LEDChainLength, float ledInterval, int ledNo)
{// // The ith LED will be placed at location (r_i, th_i)
// such that L(r(th), th0, th_i) = root(1 + b^2)/b * [ r(th_i) - r(th0)] =ledInterval * i,
float r_th_0 = a * Mathf.Exp(b * th0);
float r_th_i = (LEDChainLength) / (Mathf.Sqrt(1 + b * b) / b) + r_th_0;
float th_i = Mathf.Log((r_th_i / a)) / b;
LEDChainLength += ledInterval;
return th_i;
} // r(th_i) = a*exp(b*th_i),
public void UpdateLEDResponseParameter(double[] electrodeData) // eight EEG amplitudes
{
}
public void UpdateColorBrightnessParameter(int[] approachVectors) // four approach vectors; for testing use only one
{
}
void Update()
{
// Debug.Log("I am updating the LED colors ih LEDColorGenController");
//cf. m_kernelIDLED = m_BoidComputeShader.FindKernel("SampleLEDColors");
// Call a particular kernel "SampleLEDColors" in the m_BoidLEDComputeShader;
// m_BoidBuffer is set by the dispatching BoidComputeShader in SimpleBoidsTreeOfVoice;
// Now set m_BoidLEDBuffer by dispatching BoidLEDCOmputeShader.
// float currTime = Time.time; // seconds
// m_boids.DetermineParamValue("_SamplingRadius", out m_samplingRadius);
// m_BoidLEDComputeShader.SetFloat("_SamplingRadius", m_samplingRadius);
m_boids.DetermineParamValue("_Hemisphere", out m_Hemisphere);
m_BoidLEDComputeShader.SetFloat("_Hemisphere", m_Hemisphere);
m_BoidLEDComputeShader.SetFloat("_SamplingRadius", m_samplingRadius); // you can change inspector variable' value at runtime
m_BoidLEDComputeShader.Dispatch(m_kernelIDLED, m_threadGroupSize, 1, 1);
//note: m_BoidLEDComputeShader.SetBuffer(m_kernelIDLED, "_BoidLEDBuffer", m_BoidLEDBuffer);
// note: m_BoidLEDBuffer will be used in:
// m_Boid m_boidLEDInstanceMaterial.SetBuffer("_BoidLEDBuffer", m_LEDColorGenController.m_BoidLEDBuffer);
// Update is called once per frame
//m_BoidLEDRenderDebugBuffer.GetData(m_ComputeBufferArray);
//m_BoidLEDRenderDebugBuffer0.GetData(m_ComputeBufferArray0);
m_BoidLEDBuffer.GetData(m_BoidLEDArray); // Get the boidLED data to send to the arduino
// Debug.Log("BoidLEDRender Debug");