point.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 Point struct {
  11. core.Node // Embedded node
  12. color math32.Color // Light color
  13. intensity float32 // Light intensity
  14. uColor gls.Uniform3f // PointLightColor uniform
  15. uPosition gls.Uniform3f // PointLightPosition uniform
  16. uLinearDecay gls.Uniform1f // PointLightLinearDecay uniform
  17. uQuadraticDecay gls.Uniform1f // PointLightQuadraticDecay uniform
  18. }
  19. // NewPoint creates and returns a point light with the specified color and intensity
  20. func NewPoint(color *math32.Color, intensity float32) *Point {
  21. lp := new(Point)
  22. lp.Node.Init()
  23. lp.color = *color
  24. lp.intensity = intensity
  25. // Creates uniforms
  26. lp.uColor.Init("PointLightColor")
  27. lp.uPosition.Init("PointLightPosition")
  28. lp.uLinearDecay.Init("PointLightLinearDecay")
  29. lp.uQuadraticDecay.Init("PointLightQuadraticDecay")
  30. // Set initial values
  31. lp.SetColor(color)
  32. lp.uPosition.Set(0, 0, 0)
  33. lp.uLinearDecay.Set(1.0)
  34. lp.uQuadraticDecay.Set(1.0)
  35. return lp
  36. }
  37. // SetColor sets the color of this light
  38. func (lp *Point) SetColor(color *math32.Color) {
  39. lp.color = *color
  40. tmpColor := lp.color
  41. tmpColor.MultiplyScalar(lp.intensity)
  42. lp.uColor.SetColor(&tmpColor)
  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. tmpColor := lp.color
  52. tmpColor.MultiplyScalar(lp.intensity)
  53. lp.uColor.SetColor(&tmpColor)
  54. }
  55. // Intensity returns the current intensity of this light
  56. func (lp *Point) Intensity() float32 {
  57. return lp.intensity
  58. }
  59. // SetLinearDecay sets the linear decay factor as a function of the distance
  60. func (lp *Point) SetLinearDecay(decay float32) {
  61. lp.uLinearDecay.Set(decay)
  62. }
  63. // LinearDecay returns the current linear decay factor
  64. func (lp *Point) LinearDecay() float32 {
  65. return lp.uLinearDecay.Get()
  66. }
  67. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  68. func (lp *Point) SetQuadraticDecay(decay float32) {
  69. lp.uQuadraticDecay.Set(decay)
  70. }
  71. // QuadraticDecay returns the current quadratic decay factor
  72. func (lp *Point) QuadraticDecay() float32 {
  73. return lp.uQuadraticDecay.Get()
  74. }
  75. // RenderSetup is called by the engine before rendering the scene
  76. func (lp *Point) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  77. // Transfer uniforms
  78. lp.uColor.TransferIdx(gs, idx)
  79. lp.uLinearDecay.TransferIdx(gs, idx)
  80. lp.uQuadraticDecay.TransferIdx(gs, idx)
  81. // Calculates and updates light position uniform in camera coordinates
  82. var pos math32.Vector3
  83. lp.WorldPosition(&pos)
  84. pos4 := math32.Vector4{pos.X, pos.Y, pos.Z, 1.0}
  85. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  86. lp.uPosition.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  87. lp.uPosition.TransferIdx(gs, idx)
  88. }