standard.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. uni *gls.Uniform3fv // Uniform array of 3 floats with material properties
  12. //ambient *gls.Uniform3f // Ambient color uniform
  13. //diffuse *gls.Uniform3f // Diffuse color uniform
  14. //specular *gls.Uniform3f // Specular color uniform
  15. //emissive *gls.Uniform3f // Emissive color uniform
  16. //shininess *gls.Uniform1f // Shininess exponent uniform
  17. //opacity *gls.Uniform1f // Opacity (alpha)uniform
  18. }
  19. const (
  20. vAmbient = 0 // index for Ambient color in uniform array
  21. vDiffuse = 1 // index for Diffuse color in uniform array
  22. vSpecular = 2 // index for Specular color in uniform array
  23. vEmissive = 3 // index for Emissive color in uniform array
  24. pShininess = vEmissive * 4 // position for material shininess in uniform array
  25. pOpacity = pShininess + 1 // position for material opacitiy in uniform array
  26. uniSize = 5 // total count of groups 3 floats in uniform
  27. )
  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("shaderStandard", color)
  32. return ms
  33. }
  34. func (ms *Standard) Init(shader string, color *math32.Color) {
  35. ms.Material.Init()
  36. ms.SetShader(shader)
  37. // Creates uniforms and adds to material
  38. ms.uni = gls.NewUniform3fv("Material", uniSize)
  39. //ms.emissive = gls.NewUniform3f("MatEmissiveColor")
  40. //ms.ambient = gls.NewUniform3f("MatAmbientColor")
  41. //ms.diffuse = gls.NewUniform3f("MatDiffuseColor")
  42. //ms.specular = gls.NewUniform3f("MatSpecularColor")
  43. //ms.shininess = gls.NewUniform1f("MatShininess")
  44. //ms.opacity = gls.NewUniform1f("MatOpacity")
  45. // Set initial values
  46. ms.uni.SetColor(vAmbient, color)
  47. ms.uni.SetColor(vDiffuse, color)
  48. ms.uni.Set(vSpecular, 0.5, 0.5, 0.5)
  49. ms.uni.Set(vEmissive, 0, 0, 0)
  50. ms.uni.SetPos(pShininess, 30.0)
  51. ms.uni.SetPos(pOpacity, 1.0)
  52. //ms.ambient.SetColor(color)
  53. //ms.diffuse.SetColor(color)
  54. //ms.emissive.Set(0, 0, 0)
  55. //ms.specular.Set(0.5, 0.5, 0.5)
  56. //ms.shininess.Set(30.0)
  57. //ms.opacity.Set(1.0)
  58. }
  59. // AmbientColor returns the material ambient color reflectivity.
  60. func (ms *Standard) AmbientColor() math32.Color {
  61. return ms.uni.GetColor(vAmbient)
  62. }
  63. // SetAmbientColor sets the material ambient color reflectivity.
  64. // The default is the same as the diffuse color
  65. func (ms *Standard) SetAmbientColor(color *math32.Color) {
  66. ms.uni.SetColor(vAmbient, color)
  67. }
  68. // SetColor sets the material diffuse color and also the
  69. // material ambient color reflectivity
  70. func (ms *Standard) SetColor(color *math32.Color) {
  71. ms.uni.SetColor(vDiffuse, color)
  72. ms.uni.SetColor(vAmbient, color)
  73. // ms.diffuse.SetColor(color)
  74. // ms.ambient.SetColor(color)
  75. }
  76. // SetEmissiveColor sets the material emissive color
  77. // The default is {0,0,0}
  78. func (ms *Standard) SetEmissiveColor(color *math32.Color) {
  79. ms.uni.SetColor(vEmissive, color)
  80. // ms.emissive.SetColor(color)
  81. }
  82. // EmissiveColor returns the material current emissive color
  83. func (ms *Standard) EmissiveColor() math32.Color {
  84. return ms.uni.GetColor(vEmissive)
  85. //return ms.emissive.GetColor()
  86. }
  87. // SetSpecularColor sets the material specular color reflectivity.
  88. // The default is {0.5, 0.5, 0.5}
  89. func (ms *Standard) SetSpecularColor(color *math32.Color) {
  90. ms.uni.SetColor(vSpecular, color)
  91. //ms.specular.SetColor(color)
  92. }
  93. // SetShininess sets the specular highlight factor. Default is 30.
  94. func (ms *Standard) SetShininess(shininess float32) {
  95. //ms.shininess.Set(shininess)
  96. ms.uni.SetPos(pShininess, shininess)
  97. }
  98. // SetOpacity sets the material opacity (alpha). Default is 1.0.
  99. func (ms *Standard) SetOpacity(opacity float32) {
  100. ms.uni.SetPos(pOpacity, opacity)
  101. //ms.opacity.Set(opacity)
  102. }
  103. func (ms *Standard) RenderSetup(gs *gls.GLS) {
  104. ms.Material.RenderSetup(gs)
  105. ms.uni.Transfer(gs)
  106. //ms.emissive.Transfer(gs)
  107. //ms.ambient.Transfer(gs)
  108. //ms.diffuse.Transfer(gs)
  109. //ms.specular.Transfer(gs)
  110. //ms.shininess.Transfer(gs)
  111. //ms.opacity.Transfer(gs)
  112. }