pbr_mr.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. "unsafe"
  7. "github.com/g3n/engine/gls"
  8. "github.com/g3n/engine/math32"
  9. "github.com/g3n/engine/texture"
  10. )
  11. // PbrMr is a physically based rendered material which uses the metallic-roughness model.
  12. type PbrMr struct {
  13. Material // Embedded material
  14. baseColorTex *texture.Texture2D // Optional base color texture
  15. metallicRoughnessTex *texture.Texture2D // Optional metallic-roughness
  16. normalTex *texture.Texture2D // Optional normal texture
  17. occlusionTex *texture.Texture2D // Optional occlusion texture
  18. emissiveTex *texture.Texture2D // Optional emissive texture
  19. uni gls.Uniform // Uniform location cache
  20. udata struct { // Combined uniform data
  21. baseColorFactor math32.Color4
  22. emissiveFactor math32.Color4
  23. metallicFactor float32
  24. roughnessFactor float32
  25. }
  26. }
  27. // Number of glsl shader vec4 elements used by uniform data
  28. const pbrMrVec4Count = 3
  29. // NewPbrMr creates and returns a pointer to a new PbrMr material.
  30. func NewPbrMr() *PbrMr {
  31. m := new(PbrMr)
  32. m.Material.Init()
  33. m.SetShader("pbr_mr")
  34. // Creates uniform and set defaulf values
  35. m.uni.Init("Material")
  36. m.udata.baseColorFactor = math32.Color4{1, 1, 1, 1}
  37. m.udata.emissiveFactor = math32.Color4{0, 0, 0, 0}
  38. m.udata.metallicFactor = 1.0
  39. m.udata.roughnessFactor = 1.0
  40. return m
  41. }
  42. // SetBaseColorFactor sets this material base color.
  43. // Its default value is {1,1,1,1}.
  44. // Returns pointer to this updated material.
  45. func (m *PbrMr) SetBaseColorFactor(c *math32.Color4) *PbrMr {
  46. m.udata.baseColorFactor = *c
  47. return m
  48. }
  49. // SetBaseColorTexture sets this material optional texture base color.
  50. // Returns pointer to this updated material.
  51. func (m *PbrMr) SetBaseColorTexture(tex *texture.Texture2D) *PbrMr {
  52. m.baseColorTex = tex
  53. if m.baseColorTex != nil {
  54. m.baseColorTex.SetUniformNames("uBaseColorSampler", "uBaseColorTexParams")
  55. m.SetShaderDefine("HAS_BASECOLORMAP", "")
  56. }
  57. return m
  58. }
  59. // SetEmissiveFactor sets the emissive color of the material.
  60. // Its default is {0, 0, 0}.
  61. // Returns pointer to this updated material.
  62. func (m *PbrMr) SetEmissiveFactor(c *math32.Color) *PbrMr {
  63. m.udata.emissiveFactor.R = c.R
  64. m.udata.emissiveFactor.G = c.G
  65. m.udata.emissiveFactor.B = c.B
  66. return m
  67. }
  68. // SetMetallicFactor sets this material metallic factor.
  69. // Its default value is 1.0
  70. // Returns pointer to this updated material.
  71. func (m *PbrMr) SetMetallicFactor(v float32) *PbrMr {
  72. m.udata.metallicFactor = v
  73. return m
  74. }
  75. // SetMetallicRoughnessTexture sets this material optional metallic-roughness texture.
  76. // Returns pointer to this updated material.
  77. func (m *PbrMr) SetMetallicRoughnessTexture(tex *texture.Texture2D) *PbrMr {
  78. m.metallicRoughnessTex = tex
  79. if m.metallicRoughnessTex != nil {
  80. m.metallicRoughnessTex.SetUniformNames("uMetallicRoughnessSampler", "uMetallicRoughnessTexParams")
  81. m.SetShaderDefine("HAS_METALROUGHNESSMAP", "")
  82. }
  83. return m
  84. }
  85. // SetNormalTexture sets this material optional normal texture.
  86. // Returns pointer to this updated material.
  87. func (m *PbrMr) SetNormalTexture(tex *texture.Texture2D) *PbrMr {
  88. m.normalTex = tex
  89. if m.normalTex != nil {
  90. m.normalTex.SetUniformNames("uNormalSampler", "uNormalSamplerTexParams")
  91. m.SetShaderDefine("HAS_NORMALMAP", "")
  92. }
  93. return m
  94. }
  95. // SetOcclusionTexture sets this material optional occlusion texture.
  96. // Returns pointer to this updated material.
  97. func (m *PbrMr) SetOcclusionTexture(tex *texture.Texture2D) *PbrMr {
  98. m.occlusionTex = tex
  99. if m.occlusionTex != nil {
  100. m.occlusionTex.SetUniformNames("uOcclusionSampler", "uOcclusionTexParams")
  101. m.SetShaderDefine("HAS_OCCLUSIONMAP", "")
  102. }
  103. return m
  104. }
  105. // SetEmissiveTexture sets this material optional emissive texture.
  106. // Returns pointer to this updated material.
  107. func (m *PbrMr) SetEmissiveTexture(tex *texture.Texture2D) *PbrMr {
  108. m.emissiveTex = tex
  109. if m.emissiveTex != nil {
  110. m.emissiveTex.SetUniformNames("uEmissiveSampler", "uEmissiveSamplerTexParams")
  111. m.SetShaderDefine("HAS_EMISSIVEMAP", "")
  112. }
  113. return m
  114. }
  115. // SetRoughnessFactor sets this material roughness factor.
  116. // Its default value is 1.0
  117. // Returns pointer to this updated material.
  118. func (m *PbrMr) SetRoughnessFactor(v float32) *PbrMr {
  119. m.udata.roughnessFactor = v
  120. return m
  121. }
  122. // RenderSetup transfer this material uniforms and textures to the shader
  123. func (m *PbrMr) RenderSetup(gl *gls.GLS) {
  124. m.Material.RenderSetup(gl)
  125. location := m.uni.Location(gl)
  126. gl.Uniform4fvUP(location, pbrMrVec4Count, unsafe.Pointer(&m.udata))
  127. // Transfer optional textures
  128. if m.baseColorTex != nil {
  129. m.baseColorTex.RenderSetup(gl, 0)
  130. }
  131. if m.metallicRoughnessTex != nil {
  132. m.metallicRoughnessTex.RenderSetup(gl, 0)
  133. }
  134. if m.normalTex != nil {
  135. m.normalTex.RenderSetup(gl, 0)
  136. }
  137. if m.occlusionTex != nil {
  138. m.occlusionTex.RenderSetup(gl, 0)
  139. }
  140. if m.emissiveTex != nil {
  141. m.emissiveTex.RenderSetup(gl, 0)
  142. }
  143. }