spot.go 3.9 KB

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