spot.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. type Spot struct {
  12. core.Node // Embedded node
  13. color math32.Color // Light color
  14. intensity float32 // Light intensity
  15. direction math32.Vector3 // Direction in world coordinates
  16. uni gls.Uniform2 // 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 position
  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. // SetDirection sets the direction of the spot light in world coordinates
  64. func (l *Spot) SetDirection(direction *math32.Vector3) {
  65. l.direction = *direction
  66. }
  67. // Direction returns the current direction of this spot light in world coordinates
  68. func (l *Spot) Direction(direction *math32.Vector3) math32.Vector3 {
  69. return l.direction
  70. }
  71. // SetCutoffAngle sets the cutoff angle in degrees from 0 to 90
  72. func (l *Spot) SetCutoffAngle(angle float32) {
  73. l.udata.cutoffAngle = angle
  74. }
  75. // CutoffAngle returns the current cutoff angle in degrees from 0 to 90
  76. func (l *Spot) CutoffAngle() float32 {
  77. return l.udata.cutoffAngle
  78. }
  79. // SetAngularDecay sets the angular decay exponent
  80. func (l *Spot) SetAngularDecay(decay float32) {
  81. l.udata.angularDecay = decay
  82. }
  83. // AngularDecay returns the current angular decay exponent
  84. func (l *Spot) AngularDecay() float32 {
  85. return l.udata.angularDecay
  86. }
  87. // SetLinearDecay sets the linear decay factor as a function of the distance
  88. func (l *Spot) SetLinearDecay(decay float32) {
  89. l.udata.linearDecay = decay
  90. }
  91. // LinearDecay returns the current linear decay factor
  92. func (l *Spot) LinearDecay() float32 {
  93. return l.udata.linearDecay
  94. }
  95. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  96. func (l *Spot) SetQuadraticDecay(decay float32) {
  97. l.udata.quadraticDecay = decay
  98. }
  99. // QuadraticDecay returns the current quadratic decay factor
  100. func (l *Spot) QuadraticDecay() float32 {
  101. return l.udata.quadraticDecay
  102. }
  103. // RenderSetup is called by the engine before rendering the scene
  104. func (l *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  105. // Calculates and updates light position uniform in camera coordinates
  106. var pos math32.Vector3
  107. l.WorldPosition(&pos)
  108. var pos4 math32.Vector4
  109. pos4.SetVector3(&pos, 1.0)
  110. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  111. l.udata.position.X = pos4.X
  112. l.udata.position.Y = pos4.Y
  113. l.udata.position.Z = pos4.Z
  114. // Calculates and updates light direction uniform in camera coordinates
  115. pos4.SetVector3(&l.direction, 0.0)
  116. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  117. l.udata.direction.X = pos4.X
  118. l.udata.direction.Y = pos4.Y
  119. l.udata.direction.Z = pos4.Z
  120. // Transfer uniform data
  121. const vec3count = 5
  122. location := l.uni.LocationIdx(gs, vec3count*int32(idx))
  123. gs.Uniform3fvUP(location, vec3count, unsafe.Pointer(&l.udata))
  124. }