physical.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 default values
  35. m.uni.Init("Material")
  36. m.udata.baseColorFactor = math32.Color4{1, 1, 1, 1}
  37. m.udata.emissiveFactor = math32.Color4{1, 1, 1, 1}
  38. m.udata.metallicFactor = 1
  39. m.udata.roughnessFactor = 1
  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. // SetMetallicFactor sets this material metallic factor.
  50. // Its default value is 1.0
  51. // Returns pointer to this updated material.
  52. func (m *Physical) SetMetallicFactor(v float32) *Physical {
  53. m.udata.metallicFactor = v
  54. return m
  55. }
  56. // SetRoughnessFactor sets this material roughness factor.
  57. // Its default value is 1.0
  58. // Returns pointer to this updated material.
  59. func (m *Physical) SetRoughnessFactor(v float32) *Physical {
  60. m.udata.roughnessFactor = v
  61. return m
  62. }
  63. // SetEmissiveFactor sets the emissive color of the material.
  64. // Its default is {0, 0, 0}.
  65. // Returns pointer to this updated material.
  66. func (m *Physical) SetEmissiveFactor(c *math32.Color) *Physical {
  67. m.udata.emissiveFactor.R = c.R
  68. m.udata.emissiveFactor.G = c.G
  69. m.udata.emissiveFactor.B = c.B
  70. return m
  71. }
  72. // SetBaseColorMap sets this material optional texture base color.
  73. // Returns pointer to this updated material.
  74. func (m *Physical) SetBaseColorMap(tex *texture.Texture2D) *Physical {
  75. m.baseColorTex = tex
  76. if m.baseColorTex != nil {
  77. m.baseColorTex.SetUniformNames("uBaseColorSampler", "uBaseColorTexParams")
  78. m.SetShaderDefine("HAS_BASECOLORMAP", "")
  79. m.AddTexture(m.baseColorTex)
  80. } else {
  81. m.UnsetShaderDefine("HAS_BASECOLORMAP")
  82. m.RemoveTexture(m.baseColorTex)
  83. }
  84. return m
  85. }
  86. // SetMetallicRoughnessMap sets this material optional metallic-roughness texture.
  87. // Returns pointer to this updated material.
  88. func (m *Physical) SetMetallicRoughnessMap(tex *texture.Texture2D) *Physical {
  89. m.metallicRoughnessTex = tex
  90. if m.metallicRoughnessTex != nil {
  91. m.metallicRoughnessTex.SetUniformNames("uMetallicRoughnessSampler", "uMetallicRoughnessTexParams")
  92. m.SetShaderDefine("HAS_METALROUGHNESSMAP", "")
  93. m.AddTexture(m.metallicRoughnessTex)
  94. } else {
  95. m.UnsetShaderDefine("HAS_METALROUGHNESSMAP")
  96. m.RemoveTexture(m.metallicRoughnessTex)
  97. }
  98. return m
  99. }
  100. // TODO add SetNormalMap (and SetSpecularMap) to StandardMaterial.
  101. // SetNormalMap sets this material optional normal texture.
  102. // Returns pointer to this updated material.
  103. func (m *Physical) SetNormalMap(tex *texture.Texture2D) *Physical {
  104. m.normalTex = tex
  105. if m.normalTex != nil {
  106. m.normalTex.SetUniformNames("uNormalSampler", "uNormalTexParams")
  107. m.SetShaderDefine("HAS_NORMALMAP", "")
  108. m.AddTexture(m.normalTex)
  109. } else {
  110. m.UnsetShaderDefine("HAS_NORMALMAP")
  111. m.RemoveTexture(m.normalTex)
  112. }
  113. return m
  114. }
  115. // SetOcclusionMap sets this material optional occlusion texture.
  116. // Returns pointer to this updated material.
  117. func (m *Physical) SetOcclusionMap(tex *texture.Texture2D) *Physical {
  118. m.occlusionTex = tex
  119. if m.occlusionTex != nil {
  120. m.occlusionTex.SetUniformNames("uOcclusionSampler", "uOcclusionTexParams")
  121. m.SetShaderDefine("HAS_OCCLUSIONMAP", "")
  122. m.AddTexture(m.occlusionTex)
  123. } else {
  124. m.UnsetShaderDefine("HAS_OCCLUSIONMAP")
  125. m.RemoveTexture(m.occlusionTex)
  126. }
  127. return m
  128. }
  129. // SetEmissiveMap sets this material optional emissive texture.
  130. // Returns pointer to this updated material.
  131. func (m *Physical) SetEmissiveMap(tex *texture.Texture2D) *Physical {
  132. m.emissiveTex = tex
  133. if m.emissiveTex != nil {
  134. m.emissiveTex.SetUniformNames("uEmissiveSampler", "uEmissiveTexParams")
  135. m.SetShaderDefine("HAS_EMISSIVEMAP", "")
  136. m.AddTexture(m.emissiveTex)
  137. } else {
  138. m.UnsetShaderDefine("HAS_EMISSIVEMAP")
  139. m.RemoveTexture(m.emissiveTex)
  140. }
  141. return m
  142. }
  143. // RenderSetup transfer this material uniforms and textures to the shader
  144. func (m *Physical) RenderSetup(gl *gls.GLS) {
  145. m.Material.RenderSetup(gl)
  146. location := m.uni.Location(gl)
  147. log.Error("Physical RenderSetup location:%v udata:%+v", location, m.udata)
  148. gl.Uniform4fvUP(location, physicalVec4Count, unsafe.Pointer(&m.udata))
  149. }