spot.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. type Spot struct {
  11. core.Node // Embedded node
  12. color math32.Color // Light color
  13. intensity float32 // Light intensity
  14. direction math32.Vector3 // Direction in world coordinates
  15. uColor gls.Uniform3f // Uniform for light color
  16. uPosition gls.Uniform3f // Uniform for position in camera coordinates
  17. uDirection gls.Uniform3f // Uniform for direction in camera coordinates
  18. uAngularDecay gls.Uniform1f // Uniform for angular attenuation exponent
  19. uCutoffAngle gls.Uniform1f // Uniform for cutoff angle from 0 to 90 degrees
  20. uLinearDecay gls.Uniform1f // Uniform for linear distance decay
  21. uQuadraticDecay gls.Uniform1f // Uniform for quadratic distance decay
  22. }
  23. // NewSpot creates and returns a spot light with the specified color and intensity
  24. func NewSpot(color *math32.Color, intensity float32) *Spot {
  25. sp := new(Spot)
  26. sp.Node.Init()
  27. sp.color = *color
  28. sp.intensity = intensity
  29. // Creates uniforms
  30. sp.uColor.Init("SpotLightColor")
  31. sp.uPosition.Init("SpotLightPosition")
  32. sp.uDirection.Init("SpotLightDirection")
  33. sp.uAngularDecay.Init("SpotLightAngularDecay")
  34. sp.uCutoffAngle.Init("SpotLightCutoffAngle")
  35. sp.uLinearDecay.Init("SpotLightLinearDecay")
  36. sp.uQuadraticDecay.Init("SpotQuadraticDecay")
  37. // Set initial values
  38. sp.intensity = intensity
  39. sp.SetColor(color)
  40. sp.uAngularDecay.Set(15.0)
  41. sp.uCutoffAngle.Set(45.0)
  42. sp.uLinearDecay.Set(1.0)
  43. sp.uQuadraticDecay.Set(1.0)
  44. return sp
  45. }
  46. // SetColor sets the color of this light
  47. func (sl *Spot) SetColor(color *math32.Color) {
  48. sl.color = *color
  49. tmpColor := sl.color
  50. tmpColor.MultiplyScalar(sl.intensity)
  51. sl.uColor.SetColor(&tmpColor)
  52. }
  53. // Color returns the current color of this light
  54. func (sl *Spot) Color() math32.Color {
  55. return sl.color
  56. }
  57. // SetIntensity sets the intensity of this light
  58. func (sl *Spot) SetIntensity(intensity float32) {
  59. sl.intensity = intensity
  60. tmpColor := sl.color
  61. tmpColor.MultiplyScalar(sl.intensity)
  62. sl.uColor.SetColor(&tmpColor)
  63. }
  64. // Intensity returns the current intensity of this light
  65. func (sl *Spot) Intensity() float32 {
  66. return sl.intensity
  67. }
  68. // SetDirection sets the direction of the spot light in world coordinates
  69. func (sp *Spot) SetDirection(direction *math32.Vector3) {
  70. sp.direction = *direction
  71. }
  72. // Direction returns the current direction of this spot light in world coordinates
  73. func (sp *Spot) Direction(direction *math32.Vector3) math32.Vector3 {
  74. return sp.direction
  75. }
  76. // SetCutoffAngle sets the cutoff angle in degrees from 0 to 90
  77. func (sl *Spot) SetCutoffAngle(angle float32) {
  78. sl.uCutoffAngle.Set(angle)
  79. }
  80. // CutoffAngle returns the current cutoff angle in degrees from 0 to 90
  81. func (sl *Spot) CutoffAngle() float32 {
  82. return sl.uCutoffAngle.Get()
  83. }
  84. // SetAngularDecay sets the angular decay exponent
  85. func (sl *Spot) SetAngularDecay(decay float32) {
  86. sl.uAngularDecay.Set(decay)
  87. }
  88. // AngularDecay returns the current angular decay exponent
  89. func (sl *Spot) AngularDecay() float32 {
  90. return sl.uAngularDecay.Get()
  91. }
  92. // SetLinearDecay sets the linear decay factor as a function of the distance
  93. func (sl *Spot) SetLinearDecay(decay float32) {
  94. sl.uLinearDecay.Set(decay)
  95. }
  96. // LinearDecay returns the current linear decay factor
  97. func (sl *Spot) LinearDecay() float32 {
  98. return sl.uLinearDecay.Get()
  99. }
  100. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  101. func (sl *Spot) SetQuadraticDecay(decay float32) {
  102. sl.uQuadraticDecay.Set(decay)
  103. }
  104. // QuadraticDecay returns the current quadratic decay factor
  105. func (sl *Spot) QuadraticDecay() float32 {
  106. return sl.uQuadraticDecay.Get()
  107. }
  108. // RenderSetup is called by the engine before rendering the scene
  109. func (sl *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  110. sl.uColor.TransferIdx(gs, idx)
  111. sl.uAngularDecay.TransferIdx(gs, idx)
  112. sl.uCutoffAngle.TransferIdx(gs, idx)
  113. sl.uLinearDecay.TransferIdx(gs, idx)
  114. sl.uQuadraticDecay.TransferIdx(gs, idx)
  115. // Calculates and updates light position uniform in camera coordinates
  116. var pos math32.Vector3
  117. sl.WorldPosition(&pos)
  118. var pos4 math32.Vector4
  119. pos4.SetVector3(&pos, 1.0)
  120. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  121. sl.uPosition.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  122. sl.uPosition.TransferIdx(gs, idx)
  123. // Calculates and updates light direction uniform in camera coordinates
  124. pos4.SetVector3(&sl.direction, 0.0)
  125. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  126. // Normalize here ??
  127. sl.uDirection.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  128. sl.uDirection.TransferIdx(gs, idx)
  129. }