pbrmaterial.monkey2 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. Namespace mojo3d
  2. #rem monkeydoc The PbrMaterial class.
  3. #end
  4. Class PbrMaterial Extends Material
  5. #rem monkeydoc Creates a new pbr material.
  6. All properties default to white or '1' except for emissive factor which defaults to black.
  7. If you set an emissive texture, you will also need to set emissive factor to white to 'enable' it.
  8. The metalness value should be stored in the 'blue' channel of the metalness texture if the texture has multiple color channels.
  9. The roughness value should be stored in the 'green' channel of the metalness texture if the texture has multiple color channels.
  10. The occlusion value should be stored in the 'red' channel of the occlusion texture if the texture has multiple color channels.
  11. The above last 3 rules allow you to pack metalness, roughness and occlusion into a single texture.
  12. #end
  13. Method New()
  14. Init()
  15. AddInstance()
  16. End
  17. Method New( color:Color,metalness:Float=1.0,roughness:Float=1.0 )
  18. Init()
  19. ColorFactor=color
  20. MetalnessFactor=metalness
  21. RoughnessFactor=roughness
  22. AddInstance( New Variant[]( color,metalness,roughness ) )
  23. End
  24. Method New( material:PbrMaterial )
  25. Super.New( material )
  26. AddInstance( material )
  27. End
  28. #rem monkeydoc Creates a copy of the pbr material.
  29. #end
  30. Method Copy:PbrMaterial() Override
  31. Return New PbrMaterial( Self )
  32. End
  33. '***** textures *****
  34. Property Boned:Bool()
  35. Return (AttribMask & 192)=192
  36. Setter( boned:Bool )
  37. If boned AttribMask|=192 Else AttribMask&=~192
  38. End
  39. Property ColorTexture:Texture()
  40. Return Uniforms.GetTexture( "ColorTexture" )
  41. Setter( texture:Texture )
  42. Uniforms.SetTexture( "ColorTexture",texture )
  43. UpdateAttribMask()
  44. End
  45. Property AmbientTexture:Texture()
  46. Return Uniforms.GetTexture( "AmbientTexture" )
  47. Setter( texture:Texture )
  48. Uniforms.SetTexture( "AmbientTexture",texture )
  49. UpdateAttribMask()
  50. End
  51. Property EmissiveTexture:Texture()
  52. Return Uniforms.GetTexture( "EmissiveTexture" )
  53. Setter( texture:Texture )
  54. Uniforms.SetTexture( "EmissiveTexture",texture )
  55. UpdateAttribMask()
  56. End
  57. Property MetalnessTexture:Texture()
  58. Return Uniforms.GetTexture( "MetalnessTexture" )
  59. Setter( texture:Texture )
  60. Uniforms.SetTexture( "MetalnessTexture",texture )
  61. UpdateAttribMask()
  62. End
  63. Property RoughnessTexture:Texture()
  64. Return Uniforms.GetTexture( "RoughnessTexture" )
  65. Setter( texture:Texture )
  66. Uniforms.SetTexture( "RoughnessTexture",texture )
  67. UpdateAttribMask()
  68. End
  69. Property OcclusionTexture:Texture()
  70. Return Uniforms.GetTexture( "OcclusionTexture" )
  71. Setter( texture:Texture )
  72. Uniforms.SetTexture( "OcclusionTexture",texture )
  73. UpdateAttribMask()
  74. End
  75. Property NormalTexture:Texture()
  76. Return Uniforms.GetTexture( "NormalTexture" )
  77. Setter( texture:Texture )
  78. Uniforms.SetTexture( "NormalTexture",texture )
  79. If texture AttribMask|=32 Else AttribMask&=~32
  80. UpdateAttribMask()
  81. End
  82. '***** factors *****
  83. [jsonify=1]
  84. Property ColorFactor:Color()
  85. Return Uniforms.GetColor( "ColorFactor" )
  86. Setter( color:Color )
  87. Uniforms.SetColor( "ColorFactor",color )
  88. End
  89. [jsonify=1]
  90. Property AmbientFactor:Color()
  91. Return Uniforms.GetColor( "AmbientFactor" )
  92. Setter( color:Color )
  93. Uniforms.SetColor( "AmbientFactor",color )
  94. End
  95. [jsonify=1]
  96. Property EmissiveFactor:Color()
  97. Return Uniforms.GetColor( "EmissiveFactor" )
  98. Setter( color:Color )
  99. Uniforms.SetColor( "EmissiveFactor",color )
  100. End
  101. [jsonify=1]
  102. Property MetalnessFactor:Float()
  103. Return Uniforms.GetFloat( "MetalnessFactor" )
  104. Setter( factor:Float )
  105. Uniforms.SetFloat( "MetalnessFactor",factor )
  106. End
  107. [jsonify=1]
  108. Property RoughnessFactor:Float()
  109. Return Uniforms.GetFloat( "RoughnessFactor" )
  110. Setter( factor:Float )
  111. Uniforms.SetFloat( "RoughnessFactor",factor )
  112. End
  113. #rem monkeydoc Loads a PbrMaterial from a 'file'.
  114. A .pbr file is actually a directory containing a number of textures in png format. These textures are:
  115. color.png (required)
  116. emissive.png
  117. metalness.png
  118. roughness.png
  119. occlusion.png
  120. normal.png
  121. #end
  122. Function Load:PbrMaterial( path:String,textureFlags:TextureFlags=TextureFlags.WrapST|TextureFlags.FilterMipmap )
  123. Local scene:=Scene.GetCurrent(),editing:=scene.Editing
  124. If editing
  125. scene.Jsonifier.BeginLoading()
  126. Endif
  127. Local material:=New PbrMaterial
  128. Local texture:=scene.LoadTexture( path,textureFlags )
  129. If texture
  130. material.ColorTexture=texture
  131. Return material
  132. Endif
  133. texture=LoadTexture( path,"color",textureFlags )
  134. If texture
  135. material.ColorTexture=texture
  136. Endif
  137. texture=LoadTexture( path,"emissive",textureFlags )
  138. If texture
  139. material.EmissiveTexture=texture
  140. material.EmissiveFactor=Color.White
  141. Endif
  142. texture=LoadTexture( path,"metalness",textureFlags )
  143. If texture
  144. material.MetalnessTexture=texture
  145. Endif
  146. texture=LoadTexture( path,"roughness",textureFlags )
  147. If texture
  148. material.RoughnessTexture=texture
  149. Endif
  150. texture=LoadTexture( path,"occlusion",textureFlags )
  151. If texture
  152. material.OcclusionTexture=texture
  153. Endif
  154. texture=LoadTexture( path,"normal",textureFlags )
  155. If Not texture texture=LoadTexture( path,"unormal",textureFlags,True )
  156. If texture
  157. material.NormalTexture=texture
  158. Endif
  159. Local jobj:=JsonObject.Load( path+"/material.json" )
  160. If jobj
  161. If jobj.Contains( "colorFactor" ) material.ColorFactor=jobj.GetColor( "colorFactor" )
  162. If jobj.Contains( "emissiveFactor" ) material.EmissiveFactor=jobj.GetColor( "emissiveFactor" )
  163. If jobj.Contains( "metalnessFactor" ) material.MetalnessFactor=jobj.GetNumber( "metalnessFactor" )
  164. If jobj.Contains( "roughnessFactor" ) material.RoughnessFactor=jobj.GetNumber( "roughnessFactor" )
  165. Endif
  166. If editing
  167. scene.Jsonifier.EndLoading()
  168. scene.Jsonifier.AddInstance( material,"mojo3d.PbrMaterial.Load",New Variant[]( path,textureFlags ) )
  169. Endif
  170. Return material
  171. End
  172. Private
  173. Field _boned:Bool
  174. Method Init()
  175. Uniforms.DefaultTexture=Texture.ColorTexture( Color.White )
  176. ShaderName="materials/pbr-default"
  177. AttribMask=1|2|4
  178. ColorTexture=Null
  179. AmbientTexture=Null
  180. EmissiveTexture=Null
  181. MetalnessTexture=Null
  182. RoughnessTexture=Null
  183. OcclusionTexture=Null
  184. NormalTexture=Null
  185. ColorFactor=Color.White
  186. AmbientFactor=Color.Black
  187. EmissiveFactor=Color.Black
  188. MetalnessFactor=1.0
  189. RoughnessFactor=1.0
  190. End
  191. Method UpdateAttribMask()
  192. If Uniforms.NumTextures<>0 AttribMask|=24 Else AttribMask&=~24
  193. End
  194. End
  195. Private
  196. Function MakeColor:Color( jobj:JsonObject )
  197. Local r:=jobj.Contains( "r" ) ? jobj.GetNumber( "r" ) Else 1.0
  198. Local g:=jobj.Contains( "g" ) ? jobj.GetNumber( "g" ) Else 1.0
  199. Local b:=jobj.Contains( "b" ) ? jobj.GetNumber( "b" ) Else 1.0
  200. Local a:=jobj.Contains( "a" ) ? jobj.GetNumber( "a" ) Else 1.0
  201. Return New Color( r,g,b,a )
  202. End
  203. Class JsonObject Extension
  204. Method GetColor:Color( key:String )
  205. Local jobj:=GetObject( key )
  206. If Not jobj Return Color.White
  207. Return MakeColor( jobj )
  208. End
  209. End