physical.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // Physical is a physically based rendered material which uses the metallic-roughness model.
  12. type Physical 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 physicalVec4Count = 3
  29. // NewPhysical creates and returns a pointer to a new Physical material.
  30. func NewPhysical() *Physical {
  31. m := new(Physical)
  32. m.Material.Init()
  33. m.SetShader("physical")
  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 *Physical) SetBaseColorFactor(c *math32.Color4) *Physical {
  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 *Physical) SetBaseColorTexture(tex *texture.Texture2D) *Physical {
  52. m.baseColorTex = tex
  53. if m.baseColorTex != nil {
  54. m.baseColorTex.SetUniformNames("uBaseColorSampler", "uBaseColorTexParams")
  55. m.SetShaderDefine("HAS_BASECOLORMAP", "")
  56. } else {
  57. m.UnsetShaderDefine("HAS_BASECOLORMAP")
  58. }
  59. return m
  60. }
  61. // SetEmissiveFactor sets the emissive color of the material.
  62. // Its default is {0, 0, 0}.
  63. // Returns pointer to this updated material.
  64. func (m *Physical) SetEmissiveFactor(c *math32.Color) *Physical {
  65. m.udata.emissiveFactor.R = c.R
  66. m.udata.emissiveFactor.G = c.G
  67. m.udata.emissiveFactor.B = c.B
  68. return m
  69. }
  70. // SetMetallicFactor sets this material metallic factor.
  71. // Its default value is 1.0
  72. // Returns pointer to this updated material.
  73. func (m *Physical) SetMetallicFactor(v float32) *Physical {
  74. m.udata.metallicFactor = v
  75. return m
  76. }
  77. // SetMetallicRoughnessTexture sets this material optional metallic-roughness texture.
  78. // Returns pointer to this updated material.
  79. func (m *Physical) SetMetallicRoughnessTexture(tex *texture.Texture2D) *Physical {
  80. m.metallicRoughnessTex = tex
  81. if m.metallicRoughnessTex != nil {
  82. m.metallicRoughnessTex.SetUniformNames("uMetallicRoughnessSampler", "uMetallicRoughnessTexParams")
  83. m.SetShaderDefine("HAS_METALROUGHNESSMAP", "")
  84. } else {
  85. m.UnsetShaderDefine("HAS_METALROUGHNESSMAP")
  86. }
  87. return m
  88. }
  89. // SetNormalTexture sets this material optional normal texture.
  90. // Returns pointer to this updated material.
  91. func (m *Physical) SetNormalTexture(tex *texture.Texture2D) *Physical {
  92. m.normalTex = tex
  93. if m.normalTex != nil {
  94. m.normalTex.SetUniformNames("uNormalSampler", "uNormalSamplerTexParams")
  95. m.SetShaderDefine("HAS_NORMALMAP", "")
  96. } else {
  97. m.UnsetShaderDefine("HAS_NORMALMAP")
  98. }
  99. return m
  100. }
  101. // SetOcclusionTexture sets this material optional occlusion texture.
  102. // Returns pointer to this updated material.
  103. func (m *Physical) SetOcclusionTexture(tex *texture.Texture2D) *Physical {
  104. m.occlusionTex = tex
  105. if m.occlusionTex != nil {
  106. m.occlusionTex.SetUniformNames("uOcclusionSampler", "uOcclusionTexParams")
  107. m.SetShaderDefine("HAS_OCCLUSIONMAP", "")
  108. } else {
  109. m.UnsetShaderDefine("HAS_OCCLUSIONMAP")
  110. }
  111. return m
  112. }
  113. // SetEmissiveTexture sets this material optional emissive texture.
  114. // Returns pointer to this updated material.
  115. func (m *Physical) SetEmissiveTexture(tex *texture.Texture2D) *Physical {
  116. m.emissiveTex = tex
  117. if m.emissiveTex != nil {
  118. m.emissiveTex.SetUniformNames("uEmissiveSampler", "uEmissiveSamplerTexParams")
  119. m.SetShaderDefine("HAS_EMISSIVEMAP", "")
  120. } else {
  121. m.UnsetShaderDefine("HAS_EMISSIVEMAP")
  122. }
  123. return m
  124. }
  125. // SetRoughnessFactor sets this material roughness factor.
  126. // Its default value is 1.0
  127. // Returns pointer to this updated material.
  128. func (m *Physical) SetRoughnessFactor(v float32) *Physical {
  129. m.udata.roughnessFactor = v
  130. return m
  131. }
  132. // RenderSetup transfer this material uniforms and textures to the shader
  133. func (m *Physical) RenderSetup(gl *gls.GLS) {
  134. m.Material.RenderSetup(gl)
  135. location := m.uni.Location(gl)
  136. gl.Uniform4fvUP(location, physicalVec4Count, unsafe.Pointer(&m.udata))
  137. // Transfer optional textures
  138. if m.baseColorTex != nil {
  139. m.baseColorTex.RenderSetup(gl, 0)
  140. }
  141. if m.metallicRoughnessTex != nil {
  142. m.metallicRoughnessTex.RenderSetup(gl, 0)
  143. }
  144. if m.normalTex != nil {
  145. m.normalTex.RenderSetup(gl, 0)
  146. }
  147. if m.occlusionTex != nil {
  148. m.occlusionTex.RenderSetup(gl, 0)
  149. }
  150. if m.emissiveTex != nil {
  151. m.emissiveTex.RenderSetup(gl, 0)
  152. }
  153. }