standard.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.Uniform3fv // Uniform array of 3 floats with material properties
  15. }
  16. const (
  17. vAmbient = 0 // index for Ambient color in uniform array
  18. vDiffuse = 1 // index for Diffuse color in uniform array
  19. vSpecular = 2 // index for Specular color in uniform array
  20. vEmissive = 3 // index for Emissive color in uniform array
  21. pShininess = vEmissive * 4 // position for material shininess in uniform array
  22. pOpacity = pShininess + 1 // position for material opacity in uniform array
  23. pSize = pOpacity + 1 // position for material point size
  24. pRotationZ = pSize + 1 // position for material point rotation
  25. uniSize = 6 // total count of groups 3 floats in uniform
  26. )
  27. // NewStandard creates and returns a pointer to a new standard material
  28. func NewStandard(color *math32.Color) *Standard {
  29. ms := new(Standard)
  30. ms.Init("shaderStandard", color)
  31. return ms
  32. }
  33. // Init initializes the material setting the specified shader and color
  34. // It is used mainly when the material is embedded in another type
  35. func (ms *Standard) Init(shader string, color *math32.Color) {
  36. ms.Material.Init()
  37. ms.SetShader(shader)
  38. // Creates uniforms and set initial values
  39. ms.uni = gls.NewUniform3fv("Material", uniSize)
  40. ms.uni.SetColor(vAmbient, color)
  41. ms.uni.SetColor(vDiffuse, color)
  42. ms.uni.Set(vSpecular, 0.5, 0.5, 0.5)
  43. ms.uni.Set(vEmissive, 0, 0, 0)
  44. ms.uni.SetPos(pShininess, 30.0)
  45. ms.uni.SetPos(pOpacity, 1.0)
  46. }
  47. // AmbientColor returns the material ambient color reflectivity.
  48. func (ms *Standard) AmbientColor() math32.Color {
  49. return ms.uni.GetColor(vAmbient)
  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.uni.SetColor(vAmbient, 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.uni.SetColor(vDiffuse, color)
  60. ms.uni.SetColor(vAmbient, 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.uni.SetColor(vEmissive, color)
  66. }
  67. // EmissiveColor returns the material current emissive color
  68. func (ms *Standard) EmissiveColor() math32.Color {
  69. return ms.uni.GetColor(vEmissive)
  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.uni.SetColor(vSpecular, color)
  75. }
  76. // SetShininess sets the specular highlight factor. Default is 30.
  77. func (ms *Standard) SetShininess(shininess float32) {
  78. ms.uni.SetPos(pShininess, shininess)
  79. }
  80. // SetOpacity sets the material opacity (alpha). Default is 1.0.
  81. func (ms *Standard) SetOpacity(opacity float32) {
  82. ms.uni.SetPos(pOpacity, 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. ms.uni.Transfer(gs)
  89. }