ambient.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // Ambient represents an ambient light
  11. type Ambient 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. }
  17. // NewAmbient returns a pointer to a new ambient color with the specified
  18. // color and intensity
  19. func NewAmbient(color *math32.Color, intensity float32) *Ambient {
  20. la := new(Ambient)
  21. la.Node.Init(la)
  22. la.color = *color
  23. la.intensity = intensity
  24. la.uni.Init("AmbientLightColor")
  25. return la
  26. }
  27. // SetColor sets the color of this light
  28. func (la *Ambient) SetColor(color *math32.Color) {
  29. la.color = *color
  30. }
  31. // Color returns the current color of this light
  32. func (la *Ambient) Color() math32.Color {
  33. return la.color
  34. }
  35. // SetIntensity sets the intensity of this light
  36. func (la *Ambient) SetIntensity(intensity float32) {
  37. la.intensity = intensity
  38. }
  39. // Intensity returns the current intensity of this light
  40. func (la *Ambient) Intensity() float32 {
  41. return la.intensity
  42. }
  43. // RenderSetup is called by the engine before rendering the scene
  44. func (la *Ambient) RenderSetup(gs *gls.GLS, rinfo *core.RenderInfo, idx int) {
  45. color := la.color
  46. color.MultiplyScalar(la.intensity)
  47. location := la.uni.LocationIdx(gs, int32(idx))
  48. gs.Uniform3f(location, color.R, color.G, color.B)
  49. }