point.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. uni *gls.Uniform3fv // Uniform with light properties
  15. }
  16. const (
  17. pColor = 0 // index of color vector in uniform (0,1,2)
  18. pPosition = 1 // index of position vector in uniform (3,4,5)
  19. pLinearDecay = 6 // position of scalar linear decay in uniform array
  20. pQuadraticDecay = pLinearDecay + 1 // position of scalar linear decay in uniform array
  21. pointUniSize = 3 // uniform count of 3 float32
  22. )
  23. // NewPoint creates and returns a point light with the specified color and intensity
  24. func NewPoint(color *math32.Color, intensity float32) *Point {
  25. lp := new(Point)
  26. lp.Node.Init()
  27. lp.color = *color
  28. lp.intensity = intensity
  29. // Creates uniform and sets initial values
  30. lp.uni = gls.NewUniform3fv("PointLight", pointUniSize)
  31. lp.SetColor(color)
  32. lp.SetIntensity(intensity)
  33. lp.SetLinearDecay(1.0)
  34. lp.SetQuadraticDecay(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.uni.SetColor(pColor, &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.uni.SetColor(pColor, &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.uni.SetPos(pLinearDecay, decay)
  62. }
  63. // LinearDecay returns the current linear decay factor
  64. func (lp *Point) LinearDecay() float32 {
  65. return lp.uni.GetPos(pLinearDecay)
  66. }
  67. // SetQuadraticDecay sets the quadratic decay factor as a function of the distance
  68. func (lp *Point) SetQuadraticDecay(decay float32) {
  69. lp.uni.SetPos(pQuadraticDecay, decay)
  70. }
  71. // QuadraticDecay returns the current quadratic decay factor
  72. func (lp *Point) QuadraticDecay() float32 {
  73. return lp.uni.GetPos(pQuadraticDecay)
  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. // Calculates and updates light position uniform in camera coordinates
  78. var pos math32.Vector3
  79. lp.WorldPosition(&pos)
  80. pos4 := math32.Vector4{pos.X, pos.Y, pos.Z, 1.0}
  81. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  82. lp.uni.SetVector3(pPosition, &math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  83. // Transfer uniform
  84. lp.uni.TransferIdx(gs, idx*pointUniSize)
  85. }