standard.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // Standard material supports the classic lighting model with
  10. // ambient, diffuse, specular and emissive lights.
  11. // The lighting calculation is implemented in the vertex shader.
  12. type Standard struct {
  13. Material // Embedded material
  14. uni gls.Uniform // Uniform location cache
  15. udata struct { // Combined uniform data in 6 vec3:
  16. ambient math32.Color // Ambient color reflectivity
  17. diffuse math32.Color // Diffuse color reflectivity
  18. specular math32.Color // Specular color reflectivity
  19. emissive math32.Color // Emissive color
  20. shininess float32 // Specular shininess factor
  21. opacity float32 // Opacity
  22. psize float32 // Point size
  23. protationZ float32 // Point rotation around Z axis
  24. }
  25. }
  26. // Number of glsl shader vec3 elements used by uniform data
  27. const standardVec3Count = 6
  28. // NewStandard creates and returns a pointer to a new standard material
  29. func NewStandard(color *math32.Color) *Standard {
  30. ms := new(Standard)
  31. ms.Init("standard", color)
  32. return ms
  33. }
  34. // Init initializes the material setting the specified shader and color
  35. // It is used mainly when the material is embedded in another type
  36. func (ms *Standard) Init(shader string, color *math32.Color) {
  37. ms.Material.Init()
  38. ms.SetShader(shader)
  39. // Creates uniforms and set initial values
  40. ms.uni.Init("Material")
  41. ms.SetColor(color)
  42. ms.SetSpecularColor(&math32.Color{0.5, 0.5, 0.5})
  43. ms.SetEmissiveColor(&math32.Color{0, 0, 0})
  44. ms.SetShininess(30.0)
  45. ms.SetOpacity(1.0)
  46. }
  47. // AmbientColor returns the material ambient color reflectivity.
  48. func (ms *Standard) AmbientColor() math32.Color {
  49. return ms.udata.ambient
  50. }
  51. // SetAmbientColor sets the material ambient color reflectivity.
  52. // The default is the same as the diffuse color
  53. func (ms *Standard) SetAmbientColor(color *math32.Color) {
  54. ms.udata.ambient = *color
  55. }
  56. // SetColor sets the material diffuse color and also the
  57. // material ambient color reflectivity
  58. func (ms *Standard) SetColor(color *math32.Color) {
  59. ms.udata.diffuse = *color
  60. ms.udata.ambient = *color
  61. }
  62. // SetEmissiveColor sets the material emissive color
  63. // The default is {0,0,0}
  64. func (ms *Standard) SetEmissiveColor(color *math32.Color) {
  65. ms.udata.emissive = *color
  66. }
  67. // EmissiveColor returns the material current emissive color
  68. func (ms *Standard) EmissiveColor() math32.Color {
  69. return ms.udata.emissive
  70. }
  71. // SetSpecularColor sets the material specular color reflectivity.
  72. // The default is {0.5, 0.5, 0.5}
  73. func (ms *Standard) SetSpecularColor(color *math32.Color) {
  74. ms.udata.specular = *color
  75. }
  76. // SetShininess sets the specular highlight factor. Default is 30.
  77. func (ms *Standard) SetShininess(shininess float32) {
  78. ms.udata.shininess = shininess
  79. }
  80. // SetOpacity sets the material opacity (alpha). Default is 1.0.
  81. func (ms *Standard) SetOpacity(opacity float32) {
  82. ms.udata.opacity = opacity
  83. }
  84. // RenderSetup is called by the engine before drawing the object
  85. // which uses this material
  86. func (ms *Standard) RenderSetup(gs *gls.GLS) {
  87. ms.Material.RenderSetup(gs)
  88. location := ms.uni.Location(gs)
  89. gs.Uniform3fv(location, standardVec3Count, &ms.udata.ambient.R)
  90. }