directional.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 Directional struct {
  11. core.Node // Embedded node
  12. color math32.Color // Light color
  13. intensity float32 // Light intensity
  14. uColor gls.Uniform3f // Light color uniform (color * intensity)
  15. uDirection gls.Uniform3f // Light direction uniform
  16. }
  17. func NewDirectional(color *math32.Color, intensity float32) *Directional {
  18. ld := new(Directional)
  19. ld.Node.Init()
  20. ld.color = *color
  21. ld.intensity = intensity
  22. ld.uColor.Init("DirLightColor")
  23. ld.uDirection.Init("DirLightPosition")
  24. ld.SetColor(color)
  25. return ld
  26. }
  27. // SetColor sets the color of this light
  28. func (ld *Directional) SetColor(color *math32.Color) {
  29. ld.color = *color
  30. tmpColor := ld.color
  31. tmpColor.MultiplyScalar(ld.intensity)
  32. ld.uColor.SetColor(&tmpColor)
  33. }
  34. // Color returns the current color of this light
  35. func (ld *Directional) Color() math32.Color {
  36. return ld.color
  37. }
  38. // SetIntensity sets the intensity of this light
  39. func (ld *Directional) SetIntensity(intensity float32) {
  40. ld.intensity = intensity
  41. tmpColor := ld.color
  42. tmpColor.MultiplyScalar(ld.intensity)
  43. ld.uColor.SetColor(&tmpColor)
  44. }
  45. // Intensity returns the current intensity of this light
  46. func (ld *Directional) Intensity() float32 {
  47. return ld.intensity
  48. }
  49. // RenderSetup is called by the engine before rendering the scene
  50. func (ld *Directional) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  51. // Sets color
  52. ld.uColor.TransferIdx(gs, idx)
  53. // Calculates and updates light direction uniform in camera coordinates
  54. var pos math32.Vector3
  55. ld.WorldPosition(&pos)
  56. pos4 := math32.Vector4{pos.X, pos.Y, pos.Z, 0.0}
  57. pos4.ApplyMatrix4(&rinfo.ViewMatrix)
  58. ld.uDirection.SetVector3(&math32.Vector3{pos4.X, pos4.Y, pos4.Z})
  59. ld.uDirection.TransferIdx(gs, idx)
  60. }