-
Notifications
You must be signed in to change notification settings - Fork 18
/
light.go
989 lines (751 loc) · 28 KB
/
light.go
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
package tetra3d
import "math"
// ILight represents an interface that is fulfilled by an object that emits light, returning the color a vertex should be given that Vertex and its model matrix.
type ILight interface {
INode
// beginRender is used to call any set-up code or to prepare math structures that are used when lighting the scene.
// It gets called once when first rendering a set of Nodes.
beginRender()
// beginModel is, similarly to beginRender(), used to prepare or precompute any math necessary when lighting the scene.
// It gets called once before lighting all visible triangles of a given Model.
beginModel(model *Model)
Light(meshPart *MeshPart, model *Model, targetColors []Color, onlyVisible bool) // Light lights the triangles in the MeshPart, storing the result in the targetColors
// color buffer. If onlyVisible is true, only the visible vertices will be lit; if it's false, they will all be lit.
IsOn() bool // isOn is simply used tfo tell if a "generic" Light is on or not.
SetOn(on bool) // SetOn sets whether the light is on or not
Color() Color
SetColor(c Color)
Energy() float32
SetEnergy(energy float32)
}
//---------------//
// AmbientLight represents an ambient light that colors the entire Scene.
type AmbientLight struct {
*Node
color Color // Color is the color of the PointLight.
// energy is the overall energy of the Light. Internally, technically there's no difference between a brighter color and a
// higher energy, but this is here for convenience / adherance to GLTF / 3D modelers.
energy float32
on bool // If the light is on and contributing to the scene.
result [3]float32
}
// NewAmbientLight returns a new AmbientLight.
func NewAmbientLight(name string, r, g, b, energy float32) *AmbientLight {
return &AmbientLight{
Node: NewNode(name),
color: NewColor(r, g, b, 1),
energy: energy,
on: true,
}
}
func (amb *AmbientLight) Clone() INode {
clone := NewAmbientLight(amb.name, amb.color.R, amb.color.G, amb.color.B, amb.energy)
clone.on = amb.on
clone.Node = amb.Node.Clone().(*Node)
for _, child := range amb.children {
child.setParent(clone)
}
return clone
}
func (amb *AmbientLight) beginRender() {
amb.result = [3]float32{amb.color.R * amb.energy, amb.color.G * amb.energy, amb.color.B * amb.energy}
}
func (amb *AmbientLight) beginModel(model *Model) {}
// Light returns the light level for the ambient light. It doesn't use the provided Triangle; it takes it as an argument to simply adhere to the Light interface.
func (amb *AmbientLight) Light(meshPart *MeshPart, model *Model, targetColors []Color, onlyVisible bool) {
meshPart.ForEachVertexIndex(func(vertIndex int) {
targetColors[vertIndex] = targetColors[vertIndex].AddRGBA(amb.result[0], amb.result[1], amb.result[2], 0)
}, onlyVisible)
}
// AddChildren parents the provided children Nodes to the passed parent Node, inheriting its transformations and being under it in the scenegraph
// hierarchy. If the children are already parented to other Nodes, they are unparented before doing so.
func (amb *AmbientLight) AddChildren(children ...INode) {
amb.addChildren(amb, children...)
}
// Unparent unparents the AmbientLight from its parent, removing it from the scenegraph.
func (amb *AmbientLight) Unparent() {
if amb.parent != nil {
amb.parent.RemoveChildren(amb)
}
}
func (amb *AmbientLight) IsOn() bool {
return amb.on && amb.energy > 0
}
func (amb *AmbientLight) SetOn(on bool) {
amb.on = on
}
func (amb *AmbientLight) Color() Color {
return amb.color
}
func (amb *AmbientLight) SetColor(color Color) {
amb.color = color
}
func (amb *AmbientLight) Energy() float32 {
return amb.energy
}
func (amb *AmbientLight) SetEnergy(energy float32) {
amb.energy = energy
}
// Type returns the NodeType for this object.
func (amb *AmbientLight) Type() NodeType {
return NodeTypeAmbientLight
}
// Index returns the index of the Node in its parent's children list.
// If the node doesn't have a parent, its index will be -1.
func (amb *AmbientLight) Index() int {
if amb.parent != nil {
for i, c := range amb.parent.Children() {
if c == amb {
return i
}
}
}
return -1
}
//---------------//
// PointLight represents a point light (naturally).
type PointLight struct {
*Node
// Range represents the distance after which the light fully attenuates. If this is 0 (the default),
// it falls off using something akin to the inverse square law.
Range float64
// color is the color of the PointLight.
color Color
// energy is the overall energy of the Light, with 1.0 being full brightness. Internally, technically there's no
// difference between a brighter color and a higher energy, but this is here for convenience / adherance to the
// GLTF spec and 3D modelers.
energy float32
// If the light is on and contributing to the scene.
On bool
rangeSquared float64
workingPosition Vector
}
// NewPointLight creates a new Point light.
func NewPointLight(name string, r, g, b, energy float32) *PointLight {
return &PointLight{
Node: NewNode(name),
energy: energy,
color: NewColor(r, g, b, 1),
On: true,
}
}
// Clone returns a new clone of the given point light.
func (p *PointLight) Clone() INode {
clone := NewPointLight(p.name, p.color.R, p.color.G, p.color.B, p.energy)
clone.On = p.On
clone.Range = p.Range
clone.Node = p.Node.Clone().(*Node)
for _, child := range p.children {
child.setParent(p)
}
return clone
}
func (p *PointLight) beginRender() {
p.rangeSquared = p.Range * p.Range
}
func (p *PointLight) beginModel(model *Model) {
pos, sca, rot := model.Transform().Inverted().Decompose()
// Rather than transforming all vertices of all triangles of a mesh, we can just transform the
// point light's position by the inversion of the model's transform to get the same effect and save processing time.
// The same technique is used for Sphere - Triangle collision in bounds.go.
if model.skinned {
p.workingPosition = p.WorldPosition()
} else {
p.workingPosition = rot.MultVec(p.WorldPosition()).Add(pos.Mult(Vector{1 / sca.X, 1 / sca.Y, 1 / sca.Z, sca.W}))
}
}
// Light returns the R, G, and B values for the PointLight for all vertices of a given Triangle.
func (p *PointLight) Light(meshPart *MeshPart, model *Model, targetColors []Color, onlyVisible bool) {
// We calculate both the eye vector as well as the light vector so that if the camera passes behind the
// lit face and backface culling is off, the triangle can still be lit or unlit from the other side. Otherwise,
// if the triangle were lit by a light, it would appear lit regardless of the positioning of the camera.
meshPart.ForEachVertexIndex(func(index int) {
// TODO: Make lighting faster by returning early if the triangle is too far from the point light position
var vertPos, vertNormal Vector
if model.skinned {
vertPos = model.Mesh.vertexSkinnedPositions[index]
vertNormal = model.Mesh.vertexSkinnedNormals[index]
} else {
vertPos = model.Mesh.VertexPositions[index]
vertNormal = model.Mesh.VertexNormals[index]
}
distance := p.workingPosition.DistanceSquared(vertPos)
if p.Range > 0 {
if distance > p.rangeSquared {
return
}
// var triCenter Vector
// if model.skinned {
// v0 := model.Mesh.vertexSkinnedPositions[tri.VertexIndices[0]].Clone()
// v1 := model.Mesh.vertexSkinnedPositions[tri.VertexIndices[1]]
// v2 := model.Mesh.vertexSkinnedPositions[tri.VertexIndices[2]]
// triCenter = Vector(vector.In(v0).Add(v1).Add(v2).Scale(1.0 / 3.0))
// } else {
// triCenter = tri.Center
// }
// dist := fastVectorDistanceSquared(point.workingPosition, triCenter)
// if dist > (point.distanceSquared + (tri.MaxSpan * tri.MaxSpan)) {
// return
// }
} else {
if 1/distance*float64(p.energy) < 0.001 {
return
}
}
// If you're on the other side of the plane, just assume it's not visible.
// if dot(model.Mesh.Triangles[triIndex].Normal, fastVectorSub(triCenter, point.cameraPosition).Unit()) < 0 {
// return light
// }
lightVec := p.workingPosition.Sub(vertPos).Unit()
if mat := meshPart.Material; mat != nil && mat.LightingMode == LightingModeFixedNormals {
vertNormal = lightVec
}
diffuse := vertNormal.Dot(lightVec)
if mat := meshPart.Material; mat != nil && mat.LightingMode == LightingModeDoubleSided {
diffuse = math.Abs(diffuse)
}
if diffuse > 0 {
diffuseFactor := diffuse * (1.0 / (1.0 + (0.1 * distance))) * 2
if p.Range > 0 {
distClamp := clamp((p.rangeSquared-distance)/distance, 0, 1)
diffuseFactor *= distClamp
}
targetColors[index] = targetColors[index].AddRGBA(
p.color.R*float32(diffuseFactor)*p.energy,
p.color.G*float32(diffuseFactor)*p.energy,
p.color.B*float32(diffuseFactor)*p.energy,
0,
)
}
}, onlyVisible)
}
// AddChildren parents the provided children Nodes to the passed parent Node, inheriting its transformations and being under it in the scenegraph
// hierarchy. If the children are already parented to other Nodes, they are unparented before doing so.
func (p *PointLight) AddChildren(children ...INode) {
p.addChildren(p, children...)
}
// Unparent unparents the PointLight from its parent, removing it from the scenegraph.
func (p *PointLight) Unparent() {
if p.parent != nil {
p.parent.RemoveChildren(p)
}
}
func (p *PointLight) IsOn() bool {
return p.On && p.energy > 0
}
func (p *PointLight) SetOn(on bool) {
p.On = on
}
func (p *PointLight) Color() Color {
return p.color
}
func (p *PointLight) SetColor(color Color) {
p.color = color
}
func (p *PointLight) Energy() float32 {
return p.energy
}
func (p *PointLight) SetEnergy(energy float32) {
p.energy = energy
}
// Index returns the index of the Node in its parent's children list.
// If the node doesn't have a parent, its index will be -1.
func (p *PointLight) Index() int {
if p.parent != nil {
for i, c := range p.parent.Children() {
if c == p {
return i
}
}
}
return -1
}
// Type returns the NodeType for this object.
func (p *PointLight) Type() NodeType {
return NodeTypePointLight
}
//---------------//
// DirectionalLight represents a directional light of infinite distance.
type DirectionalLight struct {
*Node
color Color // Color is the color of the light.
// energy is the overall energy of the light. Internally, technically there's no difference between a brighter color and a
// higher energy, but this is here for convenience / adherance to GLTF / 3D modelers.
energy float32
On bool // If the light is on and contributing to the scene.
workingForward Vector // Internal forward vector so we don't have to calculate it for every triangle for every model using this light.
workingModelRotation Matrix4 // Similarly, this is an internal rotational transform (without the transformation row) for the Model being lit.
}
// NewDirectionalLight creates a new Directional Light with the specified RGB color and energy (assuming 1.0 energy is standard / "100%" lighting).
func NewDirectionalLight(name string, r, g, b, energy float32) *DirectionalLight {
return &DirectionalLight{
Node: NewNode(name),
color: NewColor(r, g, b, 1),
energy: energy,
On: true,
}
}
// Clone returns a new DirectionalLight clone from the given DirectionalLight.
func (sun *DirectionalLight) Clone() INode {
clone := NewDirectionalLight(sun.name, sun.color.R, sun.color.G, sun.color.B, sun.energy)
clone.On = sun.On
clone.Node = sun.Node.Clone().(*Node)
for _, child := range sun.children {
child.setParent(clone)
}
return clone
}
func (sun *DirectionalLight) beginRender() {
sun.workingForward = sun.WorldRotation().Forward() // Already reversed
}
func (sun *DirectionalLight) beginModel(model *Model) {
if !model.skinned {
sun.workingModelRotation = model.WorldRotation().Inverted().Transposed()
}
}
// Light returns the R, G, and B values for the DirectionalLight for each vertex of the provided Triangle.
func (sun *DirectionalLight) Light(meshPart *MeshPart, model *Model, targetColors []Color, onlyVisible bool) {
meshPart.ForEachVertexIndex(func(index int) {
var normal Vector
if model.skinned {
// If it's skinned, we don't have to calculate the normal, as that's been pre-calc'd for us
normal = model.Mesh.vertexSkinnedNormals[index]
} else {
normal = sun.workingModelRotation.MultVec(model.Mesh.VertexNormals[index])
}
if mat := meshPart.Material; mat != nil && mat.LightingMode == LightingModeFixedNormals {
normal = sun.workingForward
}
diffuseFactor := normal.Dot(sun.workingForward)
if mat := meshPart.Material; mat != nil && mat.LightingMode == LightingModeDoubleSided {
diffuseFactor = math.Abs(diffuseFactor)
}
if diffuseFactor <= 0 {
return
}
targetColors[index] = targetColors[index].AddRGBA(
sun.color.R*float32(diffuseFactor)*sun.energy,
sun.color.G*float32(diffuseFactor)*sun.energy,
sun.color.B*float32(diffuseFactor)*sun.energy,
0,
)
}, onlyVisible)
}
// AddChildren parents the provided children Nodes to the passed parent Node, inheriting its transformations and being under it in the scenegraph
// hierarchy. If the children are already parented to other Nodes, they are unparented before doing so.
func (sun *DirectionalLight) AddChildren(children ...INode) {
sun.addChildren(sun, children...)
}
// Unparent unparents the DirectionalLight from its parent, removing it from the scenegraph.
func (sun *DirectionalLight) Unparent() {
if sun.parent != nil {
sun.parent.RemoveChildren(sun)
}
}
func (sun *DirectionalLight) IsOn() bool {
return sun.On && sun.energy > 0
}
func (sun *DirectionalLight) SetOn(on bool) {
sun.On = on
}
func (d *DirectionalLight) Color() Color {
return d.color
}
func (d *DirectionalLight) SetColor(color Color) {
d.color = color
}
func (d *DirectionalLight) Energy() float32 {
return d.energy
}
func (d *DirectionalLight) SetEnergy(energy float32) {
d.energy = energy
}
// Index returns the index of the Node in its parent's children list.
// If the node doesn't have a parent, its index will be -1.
func (sun *DirectionalLight) Index() int {
if sun.parent != nil {
for i, c := range sun.parent.Children() {
if c == sun {
return i
}
}
}
return -1
}
// Type returns the NodeType for this object.
func (sun *DirectionalLight) Type() NodeType {
return NodeTypeDirectionalLight
}
// CubeLight represents an AABB volume that lights triangles.
type CubeLight struct {
*Node
Dimensions Dimensions // The overall dimensions of the CubeLight.
energy float32 // The overall energy of the CubeLight
color Color // The color of the CubeLight
On bool // If the CubeLight is on or not
// A value between 0 and 1 indicating how much opposite faces are still lit within the volume (i.e. at LightBleed = 0.0,
// faces away from the light are dark; at 1.0, faces away from the light are fully illuminated)
Bleed float64
LightingAngle Vector // The direction in which light is shining. Defaults to local Y down (0, -1, 0).
workingDimensions Dimensions
workingPosition Vector
workingAngle Vector
}
// NewCubeLight creates a new CubeLight with the given dimensions.
func NewCubeLight(name string, dimensions Dimensions) *CubeLight {
cube := &CubeLight{
Node: NewNode(name),
// Dimensions: Dimensions{{-w / 2, -h / 2, -d / 2}, {w / 2, h / 2, d / 2}},
Dimensions: dimensions,
energy: 1,
color: NewColor(1, 1, 1, 1),
On: true,
LightingAngle: Vector{0, -1, 0, 0},
}
return cube
}
// NewCubeLightFromModel creates a new CubeLight from the Model's dimensions and world transform.
// Note that this does not add it to the Model's hierarchy in any way - the newly created CubeLight
// is still its own Node.
func NewCubeLightFromModel(name string, model *Model) *CubeLight {
cube := NewCubeLight(name, model.Mesh.Dimensions)
cube.SetWorldTransform(model.Transform())
cube.LightingAngle = model.WorldRotation().Up().Invert()
return cube
}
// Clone clones the CubeLight, returning a deep copy.
func (cube *CubeLight) Clone() INode {
newCube := NewCubeLight(cube.name, cube.Dimensions)
newCube.energy = cube.energy
newCube.color = cube.color
newCube.On = cube.On
newCube.Bleed = cube.Bleed
newCube.LightingAngle = cube.LightingAngle
newCube.SetWorldTransform(cube.Transform())
newCube.Node = cube.Node.Clone().(*Node)
for _, child := range newCube.children {
child.setParent(newCube)
}
return newCube
}
// TransformedDimensions returns the AABB volume dimensions of the CubeLight as they have been scaled, rotated, and positioned to follow the
// CubeLight's node.
func (cube *CubeLight) TransformedDimensions() Dimensions {
p, s, r := cube.Transform().Decompose()
corners := [][]float64{
{1, 1, 1},
{1, -1, 1},
{-1, 1, 1},
{-1, -1, 1},
{1, 1, -1},
{1, -1, -1},
{-1, 1, -1},
{-1, -1, -1},
}
dimensions := NewEmptyDimensions()
for _, c := range corners {
position := r.MultVec(Vector{
(cube.Dimensions.Width() * c[0] * s.X / 2),
(cube.Dimensions.Height() * c[1] * s.Y / 2),
(cube.Dimensions.Depth() * c[2] * s.Z / 2),
0,
})
if dimensions.Min.X > position.X {
dimensions.Min.X = position.X
}
if dimensions.Min.Y > position.Y {
dimensions.Min.Y = position.Y
}
if dimensions.Min.Z > position.Z {
dimensions.Min.Z = position.Z
}
if dimensions.Max.X < position.X {
dimensions.Max.X = position.X
}
if dimensions.Max.Y < position.Y {
dimensions.Max.Y = position.Y
}
if dimensions.Max.Z < position.Z {
dimensions.Max.Z = position.Z
}
}
dimensions.Min = dimensions.Min.Add(p)
dimensions.Max = dimensions.Max.Add(p)
return dimensions
}
func (cube *CubeLight) beginRender() {
cube.workingAngle = cube.WorldRotation().MultVec(cube.LightingAngle.Invert())
}
func (cube *CubeLight) beginModel(model *Model) {
p, s, r := model.Transform().Inverted().Decompose()
_, _, _ = p, s, r
// Rather than transforming all vertices of all triangles of a mesh, we can just transform the
// point light's position by the inversion of the model's transform to get the same effect and save processing time.
// The same technique is used for Sphere - Triangle collision in bounds.go.
lightStartPos := cube.WorldPosition().Add(cube.LightingAngle.Invert().Scale(cube.Dimensions.Height() / 2))
cube.workingDimensions = cube.TransformedDimensions()
if model.skinned {
cube.workingPosition = lightStartPos
} else {
sDen := Vector{1 / s.X, 1 / s.Y, 1 / s.Z, s.W}
_ = sDen
cube.workingPosition = r.MultVec(lightStartPos).Add(p.Mult(sDen))
// point.workingPosition = r.MultVec(point.WorldPosition()).Add(p.Mult(Vector{1 / s.X, 1 / s.Y, 1 / s.Z, s.W}))
cube.workingDimensions.Min = r.MultVec(cube.workingDimensions.Min.Mult(s))
cube.workingDimensions.Max = r.MultVec(cube.workingDimensions.Max.Mult(s))
cube.workingDimensions.Min = cube.workingDimensions.Min.Add(p)
cube.workingDimensions.Max = cube.workingDimensions.Max.Add(p)
}
}
// Light returns the R, G, and B values for the PointLight for all vertices of a given Triangle.
func (cube *CubeLight) Light(meshPart *MeshPart, model *Model, targetColors []Color, onlyVisible bool) {
meshPart.ForEachVertexIndex(func(index int) {
// TODO: Make lighting faster by returning early if the triangle is too far from the point light position
// We calculate both the eye vector as well as the light vector so that if the camera passes behind the
// lit face and backface culling is off, the triangle can still be lit or unlit from the other side. Otherwise,
// if the triangle were lit by a light, it would appear lit regardless of the positioning of the camera.
// var triCenter Vector
// if model.skinned {
// v0 := model.Mesh.vertexSkinnedPositions[triIndex*3].Clone()
// v1 := model.Mesh.vertexSkinnedPositions[triIndex*3+1]
// v2 := model.Mesh.vertexSkinnedPositions[triIndex*3+2]
// vector.In(v0).Add(v1).Add(v2).Scale(1.0 / 3.0)
// triCenter = v0
// } else {
// triCenter = model.Mesh.Triangles[triIndex].Center
// }
// dist := fastVectorDistanceSquared(cube.workingPosition, triCenter)
// if cube.Distance > 0 && dist > distanceSquared {
// return light
// }
// If you're on the other side of the plane, just assume it's not visible.
// if dot(model.Mesh.Triangles[triIndex].Normal, fastVectorSub(triCenter, point.cameraPosition).Unit()) < 0 {
// return light
// }
var vertPos, vertNormal Vector
if model.skinned {
vertPos = model.Mesh.vertexSkinnedPositions[index]
vertNormal = model.Mesh.vertexSkinnedNormals[index]
} else {
vertPos = model.Mesh.VertexPositions[index]
vertNormal = model.Mesh.VertexNormals[index]
}
var diffuse, diffuseFactor float64
if mat := meshPart.Material; mat != nil && mat.LightingMode == LightingModeFixedNormals {
vertNormal = cube.workingAngle
}
diffuse = vertNormal.Dot(cube.workingAngle)
if mat := meshPart.Material; mat != nil && mat.LightingMode == LightingModeDoubleSided {
diffuse = math.Abs(diffuse)
}
if cube.Bleed > 0 {
if diffuse < 0 {
diffuse = 0
}
// diffuse += cube.Bleed
diffuse = cube.Bleed + ((1 - cube.Bleed) * diffuse)
if diffuse > 1 {
diffuse = 1
}
}
if diffuse < 0 {
return
} else {
if !cube.workingDimensions.Inside(vertPos) {
diffuseFactor = 0
} else {
diffuseFactor = diffuse
}
}
if diffuseFactor <= 0 {
return
}
targetColors[index] = targetColors[index].AddRGBA(
cube.color.R*float32(diffuseFactor)*cube.energy,
cube.color.G*float32(diffuseFactor)*cube.energy,
cube.color.B*float32(diffuseFactor)*cube.energy,
0,
)
}, onlyVisible)
}
// IsOn returns if the CubeLight is on or not.
func (cube *CubeLight) IsOn() bool {
return cube.On
}
// SetOn sets the CubeLight to be on or off.
func (cube *CubeLight) SetOn(on bool) {
cube.On = on
}
func (cube *CubeLight) Color() Color {
return cube.color
}
func (cube *CubeLight) SetColor(color Color) {
cube.color = color
}
func (cube *CubeLight) Energy() float32 {
return cube.energy
}
func (cube *CubeLight) SetEnergy(energy float32) {
cube.energy = energy
}
// AddChildren parents the provided children Nodes to the passed parent Node, inheriting its transformations and being under it in the scenegraph
// hierarchy. If the children are already parented to other Nodes, they are unparented before doing so.
func (cube *CubeLight) AddChildren(children ...INode) {
cube.addChildren(cube, children...)
}
// Unparent unparents the CubeLight from its parent, removing it from the scenegraph.
func (cube *CubeLight) Unparent() {
if cube.parent != nil {
cube.parent.RemoveChildren(cube)
}
}
// Index returns the index of the Node in its parent's children list.
// If the node doesn't have a parent, its index will be -1.
func (cube *CubeLight) Index() int {
if cube.parent != nil {
for i, c := range cube.parent.Children() {
if c == cube {
return i
}
}
}
return -1
}
// Type returns the type of INode this is (NodeTypeCubeLight).
func (cube *CubeLight) Type() NodeType {
return NodeTypeCubeLight
}
// type polygonLightCell struct {
// Color *Color
// Distance float64
// }
// type PolygonLight struct {
// *Node
// Distance float64
// Energy float32
// Model *Model
// On bool
// gridSize float64
// lightCells [][][]*polygonLightCell
// }
// func NewPolygonLight(model *Model, energy float32, gridSize float64) *PolygonLight {
// poly := &PolygonLight{
// Model: model,
// Distance: 10,
// Energy: energy,
// On: true,
// lightCells: [][][]*polygonLightCell{},
// }
// poly.Node = model.Node
// poly.ResizeGrid(gridSize)
// return poly
// }
// func (poly *PolygonLight) ResizeGrid(gridSize float64) {
// poly.lightCells = [][][]*polygonLightCell{}
// dim := poly.Model.Mesh.Dimensions
// transform := poly.Model.Transform()
// zd := int(math.Ceil(dim.Depth() / gridSize))
// yd := int(math.Ceil(dim.Height() / gridSize))
// xd := int(math.Ceil(dim.Width() / gridSize))
// poly.lightCells = make([][][]*polygonLightCell, zd)
// for z := 0; z < zd; z++ {
// poly.lightCells[z] = make([][]*polygonLightCell, yd)
// for y := 0; y < yd; y++ {
// poly.lightCells[z][y] = make([]*polygonLightCell, xd)
// for x := 0; x < xd; x++ {
// gridPos := Vector{
// (float64(x) - float64(xd/2)) * gridSize,
// (float64(y) - float64(yd/2)) * gridSize,
// (float64(z) - float64(zd/2)) * gridSize,
// }
// gridPos = transform.MultVec(gridPos)
// // closest := poly.Model.Mesh.Triangles[0]
// closestDistance := math.MaxFloat64
// for _, tri := range poly.Model.Mesh.Triangles {
// tc := transform.MultVec(tri.Center)
// dist := fastVectorDistanceSquared(tc, gridPos)
// if dist < closestDistance {
// // closest = tri
// closestDistance = dist
// }
// }
// poly.lightCells[z][y][x] = &polygonLightCell{
// Color: NewColor(1, 1, 1, 1),
// Distance: closestDistance,
// }
// // if closestDistance <= gridSize*gridSize {
// // poly.lightPoints[z][y][x] = NewColor(1, 1, 1, 1)
// // } else {
// // poly.lightPoints[z][y][x] = nil
// // }
// }
// }
// }
// poly.gridSize = gridSize
// }
// func (poly *PolygonLight) Clone() INode {
// clone := NewPolygonLight(poly.Model.Clone().(*Model),
// poly.Energy,
// poly.gridSize,
// )
// clone.Node = clone.Model.Node
// clone.Distance = poly.Distance
// return clone
// }
// func (poly *PolygonLight) beginRender() {}
// func (poly *PolygonLight) beginModel(model *Model) {}
// func (poly *PolygonLight) Light(triIndex uint16, model *Model) [9]float32 {
// light := [9]float32{}
// var vertPos, vertNormal Vector
// color := poly.Model.Color
// // dist := tc.Sub(poly.centerPoints[0]).Magnitude()
// distanceSquared := poly.Distance * poly.Distance
// lightPos := Vector{0, 0, 0}
// for i := 0; i < 3; i++ {
// if model.skinned {
// vertPos = model.Mesh.vertexSkinnedPositions[triIndex*3+i]
// vertNormal = model.Mesh.vertexSkinnedNormals[triIndex*3+i]
// } else {
// vertPos = model.Mesh.VertexPositions[triIndex*3+i]
// vertNormal = model.Mesh.VertexNormals[triIndex*3+i]
// }
// lightVec := vector.In(fastVectorSub(lightPos, vertPos)).Unit()
// diffuse := dot(vertNormal, Vector(lightVec))
// if diffuse < 0 {
// diffuse = 0
// }
// var diffuseFactor float64
// distance := fastVectorDistanceSquared(lightPos, vertPos)
// diffuseFactor = diffuse * math.Max(math.Min(1.0-(math.Pow((distance/distanceSquared), 4)), 1), 0)
// light[i] = color.R * float32(diffuseFactor) * poly.Energy
// light[i+1] = color.G * float32(diffuseFactor) * poly.Energy
// light[i+2] = color.B * float32(diffuseFactor) * poly.Energy
// }
// return light
// }
// func (poly *PolygonLight) IsOn() bool {
// return poly.On && poly.Energy > 0
// }
// func (poly *PolygonLight) SetOn(on bool) {
// poly.On = on
// }
// // Type returns the NodeType for this object.
// func (poly *PolygonLight) Type() NodeType {
// return NodeTypePolygonLight
// }
// func (poly *PolygonLight) ToString() string {
// str := ""
// for z := range poly.lightCells {
// for y := range poly.lightCells[z] {
// for _, cell := range poly.lightCells[z][y] {
// if cell.Distance < 2 {
// str += "x"
// } else {
// str += " "
// }
// }
// }
// str += "\n"
// }
// return str
// }