spot.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. uni *gls.Uniform3fv // Uniform with spot light properties
  16. //uColor gls.Uniform3f // Uniform for light color
  17. //uPosition gls.Uniform3f // Uniform for position in camera coordinates
  18. //uDirection gls.Uniform3f // Uniform for direction in camera coordinates
  19. //uAngularDecay gls.Uniform1f // Uniform for angular attenuation exponent
  20. //uCutoffAngle gls.Uniform1f // Uniform for cutoff angle from 0 to 90 degrees
  21. //uLinearDecay gls.Uniform1f // Uniform for linear distance decay
  22. //uQuadraticDecay gls.Uniform1f // Uniform for quadratic distance decay
  23. }
  24. const (
  25. sColor = 0 // index of color vector in uniform (0,1,2)
  26. sPosition = 1 // index of position vector in uniform (3,4,5)
  27. sDirection = 2 // index of position vector in uniform (6,7,8)
  28. sAngularDecay = 9 // position of scalar angular decay in uniform array
  29. sCutoffAngle = 10 // position of cutoff angle in uniform array
  30. sLinearDecay = 11 // position of scalar linear decay in uniform array
  31. sQuadraticDecay = 12 // position of scalar quadratic decay in uniform array
  32. spotUniSize = 5 // uniform count of 5 float32
  33. )
  34. // NewSpot creates and returns a spot light with the specified color and intensity
  35. func NewSpot(color *math32.Color, intensity float32) *Spot {
  36. sl := new(Spot)
  37. sl.Node.Init()
  38. sl.color = *color
  39. sl.intensity = intensity
  40. // Creates uniforms and set initial values
  41. sl.uni = gls.NewUniform3fv("SpotLight", spotUniSize)
  42. sl.SetColor(color)
  43. sl.SetAngularDecay(15.0)
  44. sl.SetCutoffAngle(45.0)
  45. sl.SetLinearDecay(1.0)
  46. sl.SetQuadraticDecay(1.0)
  47. // // Creates uniforms
  48. // sp.uColor.Init("SpotLightColor")
  49. // sp.uPosition.Init("SpotLightPosition")
  50. // sp.uDirection.Init("SpotLightDirection")
  51. // sp.uAngularDecay.Init("SpotLightAngularDecay")
  52. // sp.uCutoffAngle.Init("SpotLightCutoffAngle")
  53. // sp.uLinearDecay.Init("SpotLightLinearDecay")
  54. // sp.uQuadraticDecay.Init("SpotQuadraticDecay")
  55. //
  56. // // Set initial values
  57. // sp.intensity = intensity
  58. // sp.SetColor(color)
  59. // sp.uAngularDecay.Set(15.0)
  60. // sp.uCutoffAngle.Set(45.0)
  61. // sp.uLinearDecay.Set(1.0)
  62. // sp.uQuadraticDecay.Set(1.0)
  63. return sl
  64. }
  65. // SetColor sets the color of this light
  66. func (sl *Spot) SetColor(color *math32.Color) {
  67. sl.color = *color
  68. tmpColor := sl.color
  69. tmpColor.MultiplyScalar(sl.intensity)
  70. sl.uni.SetColor(sColor, &tmpColor)
  71. }
  72. // Color returns the current color of this light
  73. func (sl *Spot) Color() math32.Color {
  74. return sl.color
  75. }
  76. // SetIntensity sets the intensity of this light
  77. func (sl *Spot) SetIntensity(intensity float32) {
  78. sl.intensity = intensity
  79. tmpColor := sl.color
  80. tmpColor.MultiplyScalar(sl.intensity)
  81. sl.uni.SetColor(sColor, &tmpColor)
  82. }
  83. // Intensity returns the current intensity of this light
  84. func (sl *Spot) Intensity() float32 {
  85. return sl.intensity
  86. }
  87. // SetDirection sets the direction of the spot light in world coordinates
  88. func (sp *Spot) SetDirection(direction *math32.Vector3) {
  89. sp.direction = *direction
  90. }
  91. // Direction returns the current direction of this spot light in world coordinates
  92. func (sp *Spot) Direction(direction *math32.Vector3) math32.Vector3 {
  93. return sp.direction
  94. }
  95. // SetCutoffAngle sets the cutoff angle in degrees from 0 to 90
  96. func (sl *Spot) SetCutoffAngle(angle float32) {
  97. sl.uni.SetPos(sCutoffAngle, angle)
  98. }
  99. // CutoffAngle returns the current cutoff angle in degrees from 0 to 90
  100. func (sl *Spot) CutoffAngle() float32 {
  101. return sl.uni.GetPos(sCutoffAngle)
  102. }
  103. // SetAngularDecay sets the angular decay exponent
  104. func (sl *Spot) SetAngularDecay(decay float32) {
  105. sl.uni.SetPos(sAngularDecay, decay)
  106. }
  107. // AngularDecay returns the current angular decay exponent
  108. func (sl *Spot) AngularDecay() float32 {
  109. return sl.uni.GetPos(sAngularDecay)
  110. }
  111. // SetLinearDecay sets the linear decay factor as a function of the distance
  112. func (sl *Spot) SetLinearDecay(decay float32) {
  113. sl.uni.SetPos(sLinearDecay, decay)
  114. }
  115. // LinearDecay returns the current linear decay factor
  116. func (sl *Spot) LinearDecay() float32 {
  117. return sl.uni.GetPos(sLinearDecay)
  118. }
  119. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  120. func (sl *Spot) SetQuadraticDecay(decay float32) {
  121. sl.uni.SetPos(sQuadraticDecay, decay)
  122. }
  123. // QuadraticDecay returns the current quadratic decay factor
  124. func (sl *Spot) QuadraticDecay() float32 {
  125. return sl.uni.GetPos(sQuadraticDecay)
  126. }
  127. // RenderSetup is called by the engine before rendering the scene
  128. func (sl *Spot) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  129. //sl.uColor.TransferIdx(gs, idx)
  130. //sl.uAngularDecay.TransferIdx(gs, idx)
  131. //sl.uCutoffAngle.TransferIdx(gs, idx)
  132. //sl.uLinearDecay.TransferIdx(gs, idx)
  133. //sl.uQuadraticDecay.TransferIdx(gs, idx)
  134. // Calculates and updates light position uniform in camera coordinates
  135. var pos math32.Vector3
  136. sl.WorldPosition(&pos)
  137. var pos4 math32.Vector4
  138. pos4.SetVector3(&pos, 1.0)
  139. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  140. //sl.uPosition.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  141. //sl.uPosition.TransferIdx(gs, idx)
  142. sl.uni.SetVector3(sPosition, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  143. // Calculates and updates light direction uniform in camera coordinates
  144. pos4.SetVector3(&sl.direction, 0.0)
  145. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  146. // Normalize here ??
  147. //sl.uDirection.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  148. //sl.uDirection.TransferIdx(gs, idx)
  149. sl.uni.SetVector3(sDirection, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  150. // Transfer uniform
  151. sl.uni.TransferIdx(gs, idx*spotUniSize)
  152. }