point.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. // Point is an omnidirectional light source
  11. type Point 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 3 vec3:
  17. color math32.Color // Light color
  18. position math32.Vector3 // Light position
  19. linearDecay float32 // Distance linear decay factor
  20. quadraticDecay float32 // Distance quadratic decay factor
  21. dummy float32 // Completes 3*vec3
  22. }
  23. }
  24. // NewPoint creates and returns a point light with the specified color and intensity
  25. func NewPoint(color *math32.Color, intensity float32) *Point {
  26. lp := new(Point)
  27. lp.Node.Init()
  28. lp.color = *color
  29. lp.intensity = intensity
  30. // Creates uniform and sets initial values
  31. lp.uni.Init("PointLight")
  32. lp.SetColor(color)
  33. lp.SetIntensity(intensity)
  34. lp.SetLinearDecay(1.0)
  35. lp.SetQuadraticDecay(1.0)
  36. return lp
  37. }
  38. // SetColor sets the color of this light
  39. func (lp *Point) SetColor(color *math32.Color) {
  40. lp.color = *color
  41. lp.udata.color = lp.color
  42. lp.udata.color.MultiplyScalar(lp.intensity)
  43. }
  44. // Color returns the current color of this light
  45. func (lp *Point) Color() math32.Color {
  46. return lp.color
  47. }
  48. // SetIntensity sets the intensity of this light
  49. func (lp *Point) SetIntensity(intensity float32) {
  50. lp.intensity = intensity
  51. lp.udata.color = lp.color
  52. lp.udata.color.MultiplyScalar(lp.intensity)
  53. }
  54. // Intensity returns the current intensity of this light
  55. func (lp *Point) Intensity() float32 {
  56. return lp.intensity
  57. }
  58. // SetLinearDecay sets the linear decay factor as a function of the distance
  59. func (lp *Point) SetLinearDecay(decay float32) {
  60. lp.udata.linearDecay = decay
  61. }
  62. // LinearDecay returns the current linear decay factor
  63. func (lp *Point) LinearDecay() float32 {
  64. return lp.udata.linearDecay
  65. }
  66. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  67. func (lp *Point) SetQuadraticDecay(decay float32) {
  68. lp.udata.quadraticDecay = decay
  69. }
  70. // QuadraticDecay returns the current quadratic decay factor
  71. func (lp *Point) QuadraticDecay() float32 {
  72. return lp.udata.quadraticDecay
  73. }
  74. // RenderSetup is called by the engine before rendering the scene
  75. func (lp *Point) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  76. // Calculates light position in camera coordinates and updates uniform
  77. var pos math32.Vector3
  78. lp.WorldPosition(&pos)
  79. pos4 := math32.Vector4{pos.X, pos.Y, pos.Z, 1.0}
  80. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  81. lp.udata.position.X = pos4.X
  82. lp.udata.position.Y = pos4.Y
  83. lp.udata.position.Z = pos4.Z
  84. // Transfer uniform data
  85. const vec3count = 3
  86. location := lp.uni.LocationIdx(gs, vec3count*int32(idx))
  87. gs.Uniform3fv(location, vec3count, &lp.udata.color.R)
  88. }