spot.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // Copyright 2016 The G3N Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package light
  5. import (
  6. "unsafe"
  7. "github.com/g3n/engine/core"
  8. "github.com/g3n/engine/gls"
  9. "github.com/g3n/engine/math32"
  10. )
  11. // Spot represents a spotlight
  12. type Spot struct {
  13. core.Node // Embedded node
  14. color math32.Color // Light color
  15. intensity float32 // Light intensity
  16. uni gls.Uniform // Uniform location cache
  17. udata struct { // Combined uniform data in 5 vec3:
  18. color math32.Color // Light color
  19. position math32.Vector3 // Light position
  20. direction math32.Vector3 // Light direction
  21. angularDecay float32 // Angular decay factor
  22. cutoffAngle float32 // Cut off angle
  23. linearDecay float32 // Distance linear decay
  24. quadraticDecay float32 // Distance quadratic decay
  25. dummy1 float32 // Completes 5*vec3
  26. dummy2 float32 // Completes 5*vec3
  27. }
  28. }
  29. // NewSpot creates and returns a spot light with the specified color and intensity
  30. func NewSpot(color *math32.Color, intensity float32) *Spot {
  31. l := new(Spot)
  32. l.Node.Init()
  33. l.color = *color
  34. l.intensity = intensity
  35. l.uni.Init("SpotLight")
  36. l.SetColor(color)
  37. l.SetAngularDecay(15.0)
  38. l.SetCutoffAngle(45.0)
  39. l.SetLinearDecay(1.0)
  40. l.SetQuadraticDecay(1.0)
  41. return l
  42. }
  43. // SetColor sets the color of this light
  44. func (l *Spot) SetColor(color *math32.Color) {
  45. l.color = *color
  46. l.udata.color = l.color
  47. l.udata.color.MultiplyScalar(l.intensity)
  48. }
  49. // Color returns the current color of this light
  50. func (l *Spot) Color() math32.Color {
  51. return l.color
  52. }
  53. // SetIntensity sets the intensity of this light
  54. func (l *Spot) SetIntensity(intensity float32) {
  55. l.intensity = intensity
  56. l.udata.color = l.color
  57. l.udata.color.MultiplyScalar(l.intensity)
  58. }
  59. // Intensity returns the current intensity of this light
  60. func (l *Spot) Intensity() float32 {
  61. return l.intensity
  62. }
  63. // SetCutoffAngle sets the cutoff angle in degrees from 0 to 90
  64. func (l *Spot) SetCutoffAngle(angle float32) {
  65. l.udata.cutoffAngle = angle
  66. }
  67. // CutoffAngle returns the current cutoff angle in degrees from 0 to 90
  68. func (l *Spot) CutoffAngle() float32 {
  69. return l.udata.cutoffAngle
  70. }
  71. // SetAngularDecay sets the angular decay exponent
  72. func (l *Spot) SetAngularDecay(decay float32) {
  73. l.udata.angularDecay = decay
  74. }
  75. // AngularDecay returns the current angular decay exponent
  76. func (l *Spot) AngularDecay() float32 {
  77. return l.udata.angularDecay
  78. }
  79. // SetLinearDecay sets the linear decay factor as a function of the distance
  80. func (l *Spot) SetLinearDecay(decay float32) {
  81. l.udata.linearDecay = decay
  82. }
  83. // LinearDecay returns the current linear decay factor
  84. func (l *Spot) LinearDecay() float32 {
  85. return l.udata.linearDecay
  86. }
  87. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  88. func (l *Spot) SetQuadraticDecay(decay float32) {
  89. l.udata.quadraticDecay = decay
  90. }
  91. // QuadraticDecay returns the current quadratic decay factor
  92. func (l *Spot) QuadraticDecay() float32 {
  93. return l.udata.quadraticDecay
  94. }
  95. // RenderSetup is called by the engine before rendering the scene
  96. func (l *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  97. // Calculates and updates light position uniform in camera coordinates
  98. var pos math32.Vector3
  99. l.WorldPosition(&pos)
  100. var pos4 math32.Vector4
  101. pos4.SetVector3(&pos, 1.0)
  102. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  103. l.udata.position.X = pos4.X
  104. l.udata.position.Y = pos4.Y
  105. l.udata.position.Z = pos4.Z
  106. // Calculates and updates light direction uniform in camera coordinates
  107. var dir math32.Vector3
  108. l.WorldDirection(&dir)
  109. pos4.SetVector3(&dir, 0.0)
  110. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  111. l.udata.direction.X = pos4.X
  112. l.udata.direction.Y = pos4.Y
  113. l.udata.direction.Z = pos4.Z
  114. // Transfer uniform data
  115. const vec3count = 5
  116. location := l.uni.LocationIdx(gs, vec3count*int32(idx))
  117. gs.Uniform3fvUP(location, vec3count, unsafe.Pointer(&l.udata))
  118. }