spot.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. direction math32.Vector3 // Direction in world coordinates
  17. uni gls.Uniform // Uniform location cache
  18. udata struct { // Combined uniform data in 5 vec3:
  19. color math32.Color // Light color
  20. position math32.Vector3 // Light position
  21. direction math32.Vector3 // Light position
  22. angularDecay float32 // Angular decay factor
  23. cutoffAngle float32 // Cut off angle
  24. linearDecay float32 // Distance linear decay
  25. quadraticDecay float32 // Distance quadratic decay
  26. dummy1 float32 // Completes 5*vec3
  27. dummy2 float32 // Completes 5*vec3
  28. }
  29. }
  30. // NewSpot creates and returns a spot light with the specified color and intensity
  31. func NewSpot(color *math32.Color, intensity float32) *Spot {
  32. l := new(Spot)
  33. l.Node.Init()
  34. l.color = *color
  35. l.intensity = intensity
  36. l.uni.Init("SpotLight")
  37. l.SetColor(color)
  38. l.SetAngularDecay(15.0)
  39. l.SetCutoffAngle(45.0)
  40. l.SetLinearDecay(1.0)
  41. l.SetQuadraticDecay(1.0)
  42. return l
  43. }
  44. // SetColor sets the color of this light
  45. func (l *Spot) SetColor(color *math32.Color) {
  46. l.color = *color
  47. l.udata.color = l.color
  48. l.udata.color.MultiplyScalar(l.intensity)
  49. }
  50. // Color returns the current color of this light
  51. func (l *Spot) Color() math32.Color {
  52. return l.color
  53. }
  54. // SetIntensity sets the intensity of this light
  55. func (l *Spot) SetIntensity(intensity float32) {
  56. l.intensity = intensity
  57. l.udata.color = l.color
  58. l.udata.color.MultiplyScalar(l.intensity)
  59. }
  60. // Intensity returns the current intensity of this light
  61. func (l *Spot) Intensity() float32 {
  62. return l.intensity
  63. }
  64. // SetDirection sets the direction of the spot light in world coordinates
  65. func (l *Spot) SetDirection(direction *math32.Vector3) {
  66. l.direction = *direction
  67. }
  68. // Direction returns the current direction of this spot light in world coordinates
  69. func (l *Spot) Direction(direction *math32.Vector3) math32.Vector3 {
  70. return l.direction
  71. }
  72. // SetCutoffAngle sets the cutoff angle in degrees from 0 to 90
  73. func (l *Spot) SetCutoffAngle(angle float32) {
  74. l.udata.cutoffAngle = angle
  75. }
  76. // CutoffAngle returns the current cutoff angle in degrees from 0 to 90
  77. func (l *Spot) CutoffAngle() float32 {
  78. return l.udata.cutoffAngle
  79. }
  80. // SetAngularDecay sets the angular decay exponent
  81. func (l *Spot) SetAngularDecay(decay float32) {
  82. l.udata.angularDecay = decay
  83. }
  84. // AngularDecay returns the current angular decay exponent
  85. func (l *Spot) AngularDecay() float32 {
  86. return l.udata.angularDecay
  87. }
  88. // SetLinearDecay sets the linear decay factor as a function of the distance
  89. func (l *Spot) SetLinearDecay(decay float32) {
  90. l.udata.linearDecay = decay
  91. }
  92. // LinearDecay returns the current linear decay factor
  93. func (l *Spot) LinearDecay() float32 {
  94. return l.udata.linearDecay
  95. }
  96. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  97. func (l *Spot) SetQuadraticDecay(decay float32) {
  98. l.udata.quadraticDecay = decay
  99. }
  100. // QuadraticDecay returns the current quadratic decay factor
  101. func (l *Spot) QuadraticDecay() float32 {
  102. return l.udata.quadraticDecay
  103. }
  104. // RenderSetup is called by the engine before rendering the scene
  105. func (l *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  106. // Calculates and updates light position uniform in camera coordinates
  107. var pos math32.Vector3
  108. l.WorldPosition(&pos)
  109. var pos4 math32.Vector4
  110. pos4.SetVector3(&pos, 1.0)
  111. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  112. l.udata.position.X = pos4.X
  113. l.udata.position.Y = pos4.Y
  114. l.udata.position.Z = pos4.Z
  115. // Calculates and updates light direction uniform in camera coordinates
  116. pos4.SetVector3(&l.direction, 0.0)
  117. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  118. l.udata.direction.X = pos4.X
  119. l.udata.direction.Y = pos4.Y
  120. l.udata.direction.Z = pos4.Z
  121. // Transfer uniform data
  122. const vec3count = 5
  123. location := l.uni.LocationIdx(gs, vec3count*int32(idx))
  124. gs.Uniform3fvUP(location, vec3count, unsafe.Pointer(&l.udata))
  125. }