pbrmaterial.monkey2 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. [jsonify=1]
  35. Property Boned:Bool()
  36. Return (AttribMask & 192)=192
  37. Setter( boned:Bool )
  38. If boned AttribMask|=192 Else AttribMask&=~192
  39. End
  40. [jsonify=1]
  41. Property ColorTexture:Texture()
  42. Return Uniforms.GetTexture( "ColorTexture" )
  43. Setter( texture:Texture )
  44. Uniforms.SetTexture( "ColorTexture",texture )
  45. UpdateAttribMask()
  46. End
  47. [jsonify=1]
  48. Property AmbientTexture:Texture()
  49. Return Uniforms.GetTexture( "AmbientTexture" )
  50. Setter( texture:Texture )
  51. Uniforms.SetTexture( "AmbientTexture",texture )
  52. UpdateAttribMask()
  53. End
  54. [jsonify=1]
  55. Property EmissiveTexture:Texture()
  56. Return Uniforms.GetTexture( "EmissiveTexture" )
  57. Setter( texture:Texture )
  58. Uniforms.SetTexture( "EmissiveTexture",texture )
  59. UpdateAttribMask()
  60. End
  61. [jsonify=1]
  62. Property MetalnessTexture:Texture()
  63. Return Uniforms.GetTexture( "MetalnessTexture" )
  64. Setter( texture:Texture )
  65. Uniforms.SetTexture( "MetalnessTexture",texture )
  66. UpdateAttribMask()
  67. End
  68. [jsonify=1]
  69. Property RoughnessTexture:Texture()
  70. Return Uniforms.GetTexture( "RoughnessTexture" )
  71. Setter( texture:Texture )
  72. Uniforms.SetTexture( "RoughnessTexture",texture )
  73. UpdateAttribMask()
  74. End
  75. [jsonify=1]
  76. Property OcclusionTexture:Texture()
  77. Return Uniforms.GetTexture( "OcclusionTexture" )
  78. Setter( texture:Texture )
  79. Uniforms.SetTexture( "OcclusionTexture",texture )
  80. UpdateAttribMask()
  81. End
  82. [jsonify=1]
  83. Property NormalTexture:Texture()
  84. Return Uniforms.GetTexture( "NormalTexture" )
  85. Setter( texture:Texture )
  86. Uniforms.SetTexture( "NormalTexture",texture )
  87. If texture AttribMask|=32 Else AttribMask&=~32
  88. UpdateAttribMask()
  89. End
  90. '***** factors *****
  91. [jsonify=1]
  92. Property ColorFactor:Color()
  93. Return Uniforms.GetColor( "ColorFactor" )
  94. Setter( color:Color )
  95. Uniforms.SetColor( "ColorFactor",color )
  96. End
  97. [jsonify=1]
  98. Property AmbientFactor:Color()
  99. Return Uniforms.GetColor( "AmbientFactor" )
  100. Setter( color:Color )
  101. Uniforms.SetColor( "AmbientFactor",color )
  102. End
  103. [jsonify=1]
  104. Property EmissiveFactor:Color()
  105. Return Uniforms.GetColor( "EmissiveFactor" )
  106. Setter( color:Color )
  107. Uniforms.SetColor( "EmissiveFactor",color )
  108. End
  109. [jsonify=1]
  110. Property MetalnessFactor:Float()
  111. Return Uniforms.GetFloat( "MetalnessFactor" )
  112. Setter( factor:Float )
  113. Uniforms.SetFloat( "MetalnessFactor",factor )
  114. End
  115. [jsonify=1]
  116. Property RoughnessFactor:Float()
  117. Return Uniforms.GetFloat( "RoughnessFactor" )
  118. Setter( factor:Float )
  119. Uniforms.SetFloat( "RoughnessFactor",factor )
  120. End
  121. #rem monkeydoc Loads a PbrMaterial from a 'file'.
  122. A .pbr file is actually a directory containing a number of textures in png format. These textures are:
  123. color.png (required)
  124. emissive.png
  125. metalness.png
  126. roughness.png
  127. occlusion.png
  128. normal.png
  129. #end
  130. Function Load:PbrMaterial( path:String,textureFlags:TextureFlags=TextureFlags.WrapST|TextureFlags.FilterMipmap )
  131. Local scene:=Scene.GetCurrent(),editing:=scene.Editing
  132. If editing
  133. scene.Jsonifier.BeginLoading()
  134. Endif
  135. Local material:=New PbrMaterial
  136. Local texture:=scene.LoadTexture( path,textureFlags )
  137. If texture
  138. material.ColorTexture=texture
  139. Return material
  140. Endif
  141. texture=LoadTexture( path,"color",textureFlags )
  142. If texture
  143. material.ColorTexture=texture
  144. Endif
  145. texture=LoadTexture( path,"emissive",textureFlags )
  146. If texture
  147. material.EmissiveTexture=texture
  148. material.EmissiveFactor=Color.White
  149. Endif
  150. texture=LoadTexture( path,"metalness",textureFlags )
  151. If texture
  152. material.MetalnessTexture=texture
  153. Endif
  154. texture=LoadTexture( path,"roughness",textureFlags )
  155. If texture
  156. material.RoughnessTexture=texture
  157. Endif
  158. texture=LoadTexture( path,"occlusion",textureFlags )
  159. If texture
  160. material.OcclusionTexture=texture
  161. Endif
  162. texture=LoadTexture( path,"normal",textureFlags )
  163. If Not texture texture=LoadTexture( path,"unormal",textureFlags,True )
  164. If texture
  165. material.NormalTexture=texture
  166. Endif
  167. Local jobj:=JsonObject.Load( path+"/material.json" )
  168. If jobj
  169. If jobj.Contains( "colorFactor" ) material.ColorFactor=jobj.GetColor( "colorFactor" )
  170. If jobj.Contains( "emissiveFactor" ) material.EmissiveFactor=jobj.GetColor( "emissiveFactor" )
  171. If jobj.Contains( "metalnessFactor" ) material.MetalnessFactor=jobj.GetNumber( "metalnessFactor" )
  172. If jobj.Contains( "roughnessFactor" ) material.RoughnessFactor=jobj.GetNumber( "roughnessFactor" )
  173. Endif
  174. If editing
  175. scene.Jsonifier.EndLoading()
  176. scene.Jsonifier.AddInstance( material,"mojo3d.PbrMaterial.Load",New Variant[]( path,textureFlags ) )
  177. Endif
  178. Return material
  179. End
  180. Private
  181. Field _boned:Bool
  182. Method Init()
  183. Uniforms.DefaultTexture=Texture.ColorTexture( Color.White )
  184. ShaderName="materials/pbr-default"
  185. AttribMask=1|2|4
  186. ColorTexture=Null
  187. AmbientTexture=Null
  188. EmissiveTexture=Null
  189. MetalnessTexture=Null
  190. RoughnessTexture=Null
  191. OcclusionTexture=Null
  192. NormalTexture=Null
  193. ColorFactor=Color.White
  194. AmbientFactor=Color.Black
  195. EmissiveFactor=Color.Black
  196. MetalnessFactor=1.0
  197. RoughnessFactor=1.0
  198. End
  199. Method UpdateAttribMask()
  200. If Uniforms.NumTextures<>0 AttribMask|=24 Else AttribMask&=~24
  201. End
  202. End
  203. Private
  204. Function MakeColor:Color( jobj:JsonObject )
  205. Local r:=jobj.Contains( "r" ) ? jobj.GetNumber( "r" ) Else 1.0
  206. Local g:=jobj.Contains( "g" ) ? jobj.GetNumber( "g" ) Else 1.0
  207. Local b:=jobj.Contains( "b" ) ? jobj.GetNumber( "b" ) Else 1.0
  208. Local a:=jobj.Contains( "a" ) ? jobj.GetNumber( "a" ) Else 1.0
  209. Return New Color( r,g,b,a )
  210. End
  211. Class JsonObject Extension
  212. Method GetColor:Color( key:String )
  213. Local jobj:=GetObject( key )
  214. If Not jobj Return Color.White
  215. Return MakeColor( jobj )
  216. End
  217. End