standard.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // NewBlinnPhong creates and returns a pointer to a new Standard material using Blinn-Phong model
  35. // It is very close to Standard (Phong) model so we need only pass a parameter
  36. func NewBlinnPhong(color *math32.Color) *Standard {
  37. ms := new(Standard)
  38. ms.Init("standard", color)
  39. ms.ShaderDefines.Set("BLINN", "true")
  40. return ms
  41. }
  42. // Init initializes the material setting the specified shader and color
  43. // It is used mainly when the material is embedded in another type
  44. func (ms *Standard) Init(shader string, color *math32.Color) {
  45. ms.Material.Init()
  46. ms.SetShader(shader)
  47. // Creates uniforms and set initial values
  48. ms.uni.Init("Material")
  49. ms.SetColor(color)
  50. ms.SetSpecularColor(&math32.Color{0.5, 0.5, 0.5})
  51. ms.SetEmissiveColor(&math32.Color{0, 0, 0})
  52. ms.SetShininess(30.0)
  53. ms.SetOpacity(1.0)
  54. }
  55. // AmbientColor returns the material ambient color reflectivity.
  56. func (ms *Standard) AmbientColor() math32.Color {
  57. return ms.udata.ambient
  58. }
  59. // SetAmbientColor sets the material ambient color reflectivity.
  60. // The default is the same as the diffuse color
  61. func (ms *Standard) SetAmbientColor(color *math32.Color) {
  62. ms.udata.ambient = *color
  63. }
  64. // SetColor sets the material diffuse color and also the
  65. // material ambient color reflectivity
  66. func (ms *Standard) SetColor(color *math32.Color) {
  67. ms.udata.diffuse = *color
  68. ms.udata.ambient = *color
  69. }
  70. // SetEmissiveColor sets the material emissive color
  71. // The default is {0,0,0}
  72. func (ms *Standard) SetEmissiveColor(color *math32.Color) {
  73. ms.udata.emissive = *color
  74. }
  75. // EmissiveColor returns the material current emissive color
  76. func (ms *Standard) EmissiveColor() math32.Color {
  77. return ms.udata.emissive
  78. }
  79. // SetSpecularColor sets the material specular color reflectivity.
  80. // The default is {0.5, 0.5, 0.5}
  81. func (ms *Standard) SetSpecularColor(color *math32.Color) {
  82. ms.udata.specular = *color
  83. }
  84. // SetShininess sets the specular highlight factor. Default is 30.
  85. func (ms *Standard) SetShininess(shininess float32) {
  86. ms.udata.shininess = shininess
  87. }
  88. // SetOpacity sets the material opacity (alpha). Default is 1.0.
  89. func (ms *Standard) SetOpacity(opacity float32) {
  90. ms.udata.opacity = opacity
  91. }
  92. // RenderSetup is called by the engine before drawing the object
  93. // which uses this material
  94. func (ms *Standard) RenderSetup(gs *gls.GLS) {
  95. ms.Material.RenderSetup(gs)
  96. location := ms.uni.Location(gs)
  97. gs.Uniform3fv(location, standardVec3Count, &ms.udata.ambient.R)
  98. }