model.monkey2 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. Namespace mojo3d
  2. #rem monkeydoc The Model class.
  3. #end
  4. Class Model Extends Renderable
  5. #rem monkeydoc @hidden
  6. #end
  7. Struct Bone
  8. Field entity:Entity
  9. Field offset:AffineMat4f
  10. End
  11. #rem monkeydoc Creates a new model.
  12. #end
  13. Method New( parent:Entity=Null )
  14. Super.New( parent )
  15. Name="Model"
  16. AddInstance()
  17. Visible=True
  18. End
  19. Method New( mesh:Mesh,material:Material,parent:Entity=Null )
  20. Super.New( parent )
  21. Name="Model"
  22. AddInstance( New Variant[]( mesh,material,parent ) )
  23. Mesh=mesh
  24. Materials=New Material[]( material )
  25. Material=material
  26. Visible=True
  27. End
  28. #rem monkeydoc Copies the model.
  29. #end
  30. Method Copy:Model( parent:Entity=Null ) Override
  31. Local copy:=OnCopy( parent )
  32. CopyTo( copy )
  33. copy._bones=_bones.Slice( 0 )
  34. For Local i:=0 Until _bones.Length
  35. copy._bones[i].entity=_bones[i].entity.LastCopy
  36. Next
  37. Return copy
  38. End
  39. #rem monkeydoc The mesh rendered by the model.
  40. #end
  41. [jsonify=1]
  42. Property Mesh:Mesh()
  43. Return _mesh
  44. Setter( mesh:Mesh )
  45. _mesh=mesh
  46. End
  47. #rem monkeydoc The materials to use for rendering.
  48. #end
  49. [jsonify=1]
  50. Property Materials:Material[]()
  51. Return _materials
  52. Setter( materials:Material[] )
  53. _materials=materials
  54. End
  55. #rem monkeydoc The default material to use for rendering.
  56. #end
  57. Property Material:Material()
  58. Return _material
  59. Setter( material:Material )
  60. _material=material
  61. End
  62. #rem monkeydoc @hidden
  63. #end
  64. Property Bones:Bone[]()
  65. Return _bones
  66. Setter( bones:Bone[] )
  67. _bones=bones
  68. End
  69. #rem monkeydoc Loads a model from a file path.
  70. On its own, mojo3d can only load gltf2 format mesh and model files.
  71. To add more formats, #import the mojo3d-assimp module into your app, eg:
  72. ```
  73. #Import "<mojo3d>"
  74. #Import "<mojo3d-assimp>"
  75. ```
  76. This will allow you to load any format supported by the assimp module.
  77. However, importing the assimp module into your app will also increase its size.
  78. #end
  79. Function Load:Model( path:String )
  80. Local scene:=mojo3d.Scene.GetCurrent()
  81. Local editing:=scene.Editing
  82. If editing scene.Jsonifier.BeginLoading()
  83. Local model:Model
  84. For Local loader:=Eachin Mojo3dLoader.Instances
  85. model=loader.LoadModel( path )
  86. If model Exit
  87. Next
  88. If editing scene.Jsonifier.EndLoading()
  89. If editing scene.Jsonifier.AddInstance( model,"mojo3d.Model.Load",New Variant[]( path ) )
  90. Return model
  91. End
  92. #rem monkeydoc Loads a boned model from a file path.
  93. On its own, mojo3d can only load gltf2 format mesh and model files.
  94. To add more formats, #import the mojo3d-assimp module into your app, eg:
  95. ```
  96. #Import "<mojo3d>"
  97. #Import "<mojo3d-assimp>"
  98. ```
  99. This will allow you to load any format supported by the assimp module.
  100. However, importing the assimp module into your app will also increase its size.
  101. #end
  102. Function LoadBoned:Model( path:String )
  103. For Local loader:=Eachin Mojo3dLoader.Instances
  104. Local model:=loader.LoadBonedModel( path )
  105. If model Return model
  106. Next
  107. Return Null
  108. End
  109. Protected
  110. Method New( model:Model,parent:Entity )
  111. Super.New( model,parent )
  112. Mesh=model.Mesh
  113. Materials=model.Materials
  114. Material=model.Material
  115. AddInstance( model )
  116. End
  117. Method OnCopy:Model( parent:Entity ) Override
  118. Return New Model( Self,parent )
  119. End
  120. Internal
  121. Method OnRender( rq:RenderQueue ) Override
  122. If Not _mesh Return
  123. If _bones
  124. If _boneMatrices.Length<>_bones.Length _boneMatrices=New Mat4f[ _bones.Length ]
  125. For Local i:=0 Until _bones.Length
  126. Local bone:=_bones[i]
  127. _boneMatrices[i]=New Mat4f( bone.entity.Matrix * bone.offset )
  128. Next
  129. End
  130. Local vbuffer:=_mesh.GetVertexBuffer()
  131. For Local i:=0 Until _mesh.NumMaterials
  132. Local ibuffer:=_mesh.GetIndexBuffer( i )
  133. Local material:=i<_materials.Length And _materials[i] ? _materials[i] Else _material
  134. If _bones
  135. rq.AddRenderOp( material,_boneMatrices,vbuffer,ibuffer,3,ibuffer.Length/3,0 )
  136. Else
  137. rq.AddRenderOp( material,Self,vbuffer,ibuffer,3,ibuffer.Length/3,0 )
  138. Endif
  139. Next
  140. End
  141. Private
  142. Field _mesh:Mesh
  143. Field _material:Material
  144. Field _materials:Material[]
  145. Field _bones:Bone[]
  146. Field _boneMatrices:Mat4f[]
  147. End