standard.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 material
  5. import (
  6. "github.com/g3n/engine/gls"
  7. "github.com/g3n/engine/math32"
  8. )
  9. type Standard struct {
  10. Material // Embedded material
  11. emissive *gls.Uniform3f // Emissive color uniform
  12. ambient *gls.Uniform3f // Ambient color uniform
  13. diffuse *gls.Uniform3f // Diffuse color uniform
  14. specular *gls.Uniform3f // Specular color uniform
  15. shininess *gls.Uniform1f // Shininess exponent uniform
  16. opacity *gls.Uniform1f // Opacity (alpha)uniform
  17. }
  18. // NewStandard creates and returns a pointer to a new standard material
  19. func NewStandard(color *math32.Color) *Standard {
  20. ms := new(Standard)
  21. ms.Init("shaderStandard", color)
  22. return ms
  23. }
  24. func (ms *Standard) Init(shader string, color *math32.Color) {
  25. ms.Material.Init()
  26. ms.SetShader(shader)
  27. // Creates uniforms and adds to material
  28. ms.emissive = gls.NewUniform3f("MatEmissiveColor")
  29. ms.ambient = gls.NewUniform3f("MatAmbientColor")
  30. ms.diffuse = gls.NewUniform3f("MatDiffuseColor")
  31. ms.specular = gls.NewUniform3f("MatSpecularColor")
  32. ms.shininess = gls.NewUniform1f("MatShininess")
  33. ms.opacity = gls.NewUniform1f("MatOpacity")
  34. // Set initial values
  35. ms.emissive.Set(0, 0, 0)
  36. ms.ambient.SetColor(color)
  37. ms.diffuse.SetColor(color)
  38. ms.specular.Set(0.5, 0.5, 0.5)
  39. ms.shininess.Set(30.0)
  40. ms.opacity.Set(1.0)
  41. }
  42. // AmbientColor returns the material ambient color reflectivity.
  43. func (ms *Standard) AmbientColor() math32.Color {
  44. return ms.ambient.GetColor()
  45. }
  46. // SetAmbientColor sets the material ambient color reflectivity.
  47. // The default is the same as the diffuse color
  48. func (ms *Standard) SetAmbientColor(color *math32.Color) {
  49. ms.ambient.SetColor(color)
  50. }
  51. // SetColor sets the material diffuse color and also the
  52. // material ambient color reflectivity
  53. func (ms *Standard) SetColor(color *math32.Color) {
  54. ms.diffuse.SetColor(color)
  55. ms.ambient.SetColor(color)
  56. }
  57. // SetEmissiveColor sets the material emissive color
  58. // The default is {0,0,0}
  59. func (ms *Standard) SetEmissiveColor(color *math32.Color) {
  60. ms.emissive.SetColor(color)
  61. }
  62. // EmissiveColor returns the material current emissive color
  63. func (ms *Standard) EmissiveColor() math32.Color {
  64. return ms.emissive.GetColor()
  65. }
  66. // SetSpecularColor sets the material specular color reflectivity.
  67. // The default is {0.5, 0.5, 0.5}
  68. func (ms *Standard) SetSpecularColor(color *math32.Color) {
  69. ms.specular.SetColor(color)
  70. }
  71. // SetShininess sets the specular highlight factor. Default is 30.
  72. func (ms *Standard) SetShininess(shininess float32) {
  73. ms.shininess.Set(shininess)
  74. }
  75. // SetOpacity sets the material opacity (alpha). Default is 1.0.
  76. func (ms *Standard) SetOpacity(opacity float32) {
  77. ms.opacity.Set(opacity)
  78. }
  79. func (ms *Standard) RenderSetup(gs *gls.GLS) {
  80. ms.Material.RenderSetup(gs)
  81. ms.emissive.Transfer(gs)
  82. ms.ambient.Transfer(gs)
  83. ms.diffuse.Transfer(gs)
  84. ms.specular.Transfer(gs)
  85. ms.shininess.Transfer(gs)
  86. ms.opacity.Transfer(gs)
  87. }