threepipe
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

index.ts 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import {
  2. AnyOptions,
  3. BaseImporterPlugin,
  4. BoxGeometry,
  5. BufferGeometry,
  6. Color,
  7. Group,
  8. ILoader,
  9. Importer,
  10. Mesh,
  11. Object3D,
  12. PhysicalMaterial,
  13. Points,
  14. PointsMaterial,
  15. Scene,
  16. SkeletonHelper,
  17. } from 'threepipe'
  18. import {TDSLoader} from 'three/examples/jsm/loaders/TDSLoader.js'
  19. import {ThreeMFLoader} from 'three/examples/jsm/loaders/3MFLoader.js'
  20. import {Collada, ColladaLoader} from 'three/examples/jsm/loaders/ColladaLoader.js'
  21. import {AMFLoader} from 'three/examples/jsm/loaders/AMFLoader.js'
  22. import {GCodeLoader} from 'three/examples/jsm/loaders/GCodeLoader.js'
  23. import {BVH, BVHLoader} from 'three/examples/jsm/loaders/BVHLoader.js'
  24. import {Chunk, VOXLoader, VOXMesh} from 'three/examples/jsm/loaders/VOXLoader.js'
  25. import {MDD, MDDLoader} from 'three/examples/jsm/loaders/MDDLoader.js'
  26. import {PCDLoader} from 'three/examples/jsm/loaders/PCDLoader.js'
  27. import {TiltLoader} from 'three/examples/jsm/loaders/TiltLoader.js'
  28. import {VRMLLoader} from 'three/examples/jsm/loaders/VRMLLoader.js'
  29. import {LDrawLoader} from 'three/examples/jsm/loaders/LDrawLoader.js'
  30. import {VTKLoader} from 'three/examples/jsm/loaders/VTKLoader.js'
  31. import {XYZLoader} from 'three/examples/jsm/loaders/XYZLoader.js'
  32. // 3ds
  33. /**
  34. * Adds support for loading Autodesk 3ds `.3ds`, `application/x-3ds` files and data uris
  35. */
  36. export class TDSLoadPlugin extends BaseImporterPlugin {
  37. public static readonly PluginType = 'TDSLoadPlugin'
  38. protected _importer = new Importer(TDSLoader, ['3ds'], ['image/x-3ds', 'application/x-3ds'], false)
  39. }
  40. // 3mf
  41. /**
  42. * Adds support for loading `.3mf`, `model/3mf` files and data uris
  43. */
  44. export class ThreeMFLoadPlugin extends BaseImporterPlugin {
  45. public static readonly PluginType = 'ThreeMFLoadPlugin'
  46. protected _importer = new Importer(ThreeMFLoader, ['3mf'], ['model/3mf'], false)
  47. }
  48. // collada
  49. /**
  50. * Adds support for loading Collada `.dae`, `model/vnd.collada+xml` files and data uris
  51. */
  52. export class ColladaLoadPlugin extends BaseImporterPlugin {
  53. public static readonly PluginType = 'ColladaLoadPlugin'
  54. protected _importer = new Importer(class extends ColladaLoader implements ILoader {
  55. transform(res: Collada, _: AnyOptions): Scene {
  56. res.scene.userData.kinematics = res.kinematics
  57. res.scene.userData.library = res.library
  58. return res.scene
  59. }
  60. }, ['dae'], ['model/vnd.collada+xml'], false)
  61. }
  62. // amf
  63. /**
  64. * Adds support for loading Additive Manufacturing files `.amf`, `application/amf` files and data uris
  65. */
  66. export class AMFLoadPlugin extends BaseImporterPlugin {
  67. public static readonly PluginType = 'AMFLoadPlugin'
  68. protected _importer = new Importer(AMFLoader, ['amf'], ['application/amf'], false)
  69. }
  70. // gcode
  71. /**
  72. * Adds support for loading `.gcode`, `application/gcode` files and data uris
  73. */
  74. export class GCodeLoadPlugin extends BaseImporterPlugin {
  75. public static readonly PluginType = 'GCodeLoadPlugin'
  76. protected _importer = new Importer(GCodeLoader, ['gcode'], ['application/gcode'], false)
  77. }
  78. // bvh
  79. /**
  80. * Adds support for loading `.bvh`, `application/bvh` files and data uris
  81. */
  82. export class BVHLoadPlugin extends BaseImporterPlugin {
  83. public static readonly PluginType = 'BVHLoadPlugin'
  84. protected _importer = new Importer(class extends BVHLoader implements ILoader {
  85. transform(res: BVH, _: AnyOptions): Object3D {
  86. const obj = new Object3D()
  87. const helper = new SkeletonHelper(res.skeleton.bones[0])
  88. obj.add(res.skeleton.bones[0])
  89. obj.add(helper)
  90. obj.animations = [res.clip]
  91. obj.scale.set(0.1, 0.1, 0.1) // todo: autoScale and autoCenter not working
  92. return obj
  93. }
  94. }, ['bvh'], ['application/bvh'], false)
  95. }
  96. // vox
  97. /**
  98. * Adds support for loading Magica Voxel `.vox` files and data uris
  99. */
  100. export class VOXLoadPlugin extends BaseImporterPlugin {
  101. public static readonly PluginType = 'VOXLoadPlugin'
  102. protected _importer = new Importer(class extends VOXLoader implements ILoader {
  103. transform(chunks: Chunk[], _: AnyOptions): Object3D {
  104. const obj = new Object3D()
  105. for (const chunk of chunks) {
  106. // displayPalette( chunk.palette );
  107. const mesh = new VOXMesh(chunk)
  108. mesh.scale.setScalar(0.0015)
  109. obj.add(mesh)
  110. }
  111. return obj
  112. }
  113. }, ['vox'], [''], false)
  114. }
  115. // mdd
  116. /**
  117. * Adds support for loading animation `.mdd`, `application/mdd` files and data uris
  118. */
  119. export class MDDLoadPlugin extends BaseImporterPlugin {
  120. public static readonly PluginType = 'MDDLoadPlugin'
  121. protected _importer = new Importer(class extends MDDLoader implements ILoader {
  122. transform(res: MDD, _: AnyOptions): Object3D {
  123. const morphTargets = res.morphTargets
  124. const geometry = new BoxGeometry()
  125. geometry.morphAttributes.position = morphTargets // apply morph targets
  126. const mesh = new Mesh(geometry, new PhysicalMaterial())
  127. const obj = new Object3D()
  128. obj.add(mesh)
  129. res.clip.tracks.forEach(track=> track.name = mesh.uuid + track.name)
  130. obj.animations = [res.clip]
  131. return obj
  132. }
  133. }, ['mdd'], ['application/mdd'], false)
  134. }
  135. // pcd
  136. /**
  137. * Adds support for loading Point cloud data `.pcd`, `application/pcd` files and data uris
  138. */
  139. export class PCDLoadPlugin extends BaseImporterPlugin {
  140. public static readonly PluginType = 'PCDLoadPlugin'
  141. protected _importer = new Importer(class extends PCDLoader implements ILoader {
  142. transform(points: Points, options: AnyOptions): any {
  143. if (options.autoCenter) points.geometry.center()
  144. points.geometry.rotateX(Math.PI)
  145. return points
  146. }
  147. }, ['pcd'], ['application/pcd'], false)
  148. }
  149. // tilt
  150. /**
  151. * Adds support for loading Tilt brush `.tilt`, `application/tilt` files and data uris
  152. */
  153. export class TiltLoadPlugin extends BaseImporterPlugin {
  154. public static readonly PluginType = 'TiltLoadPlugin'
  155. protected _importer = new Importer(TiltLoader, ['tilt'], ['application/tilt'], false)
  156. }
  157. // vrml
  158. /**
  159. * Adds support for loading VRML `.wrl`, `model/vrml` files and data uris
  160. */
  161. export class VRMLLoadPlugin extends BaseImporterPlugin {
  162. public static readonly PluginType = 'VRMLLoadPlugin'
  163. protected _importer = new Importer(VRMLLoader, ['wrl'], ['model/vrml'], false)
  164. }
  165. // ldraw
  166. /**
  167. * Adds support for loading LDraw `.mpd`, `application/mpd` files and data uris. see https://ldraw.org
  168. */
  169. export class LDrawLoadPlugin extends BaseImporterPlugin {
  170. public static readonly PluginType = 'LDrawLoadPlugin'
  171. protected _importer = new Importer(class extends LDrawLoader implements ILoader {
  172. transform(res: Group, _: AnyOptions): any {
  173. // Convert from LDraw coordinates: rotate 180 degrees around OX
  174. res.rotation.x = Math.PI
  175. return res
  176. }
  177. }, ['mpd'], ['application/ldraw'], false)
  178. }
  179. // vtk
  180. /**
  181. * Adds support for loading VTK `.vtk`, '.vtp', `application/vtk` files and data uris
  182. **/
  183. export class VTKLoadPlugin extends BaseImporterPlugin {
  184. public static readonly PluginType = 'VTKLoadPlugin'
  185. protected _importer = new Importer(class extends VTKLoader implements ILoader {
  186. transform(res: BufferGeometry, _: AnyOptions): Mesh|undefined {
  187. if (!res.attributes?.normal) res.computeVertexNormals()
  188. // todo set mesh name from options/path
  189. return res ? new Mesh(res, new PhysicalMaterial({
  190. color: new Color(1, 1, 1),
  191. vertexColors: res.hasAttribute('color'),
  192. })) : undefined
  193. }
  194. }, ['vtk', 'vtp'], ['application/vtk'], false)
  195. }
  196. // xyz
  197. /**
  198. * Adds support for loading XYZ `.xyz`, `text/plain+xyz` files and data uris
  199. */
  200. export class XYZLoadPlugin extends BaseImporterPlugin {
  201. public static readonly PluginType = 'XYZLoadPlugin'
  202. protected _importer = new Importer(class extends XYZLoader implements ILoader {
  203. transform(res: BufferGeometry, options: AnyOptions): Points|undefined {
  204. if (!res.attributes?.normal) res.computeVertexNormals()
  205. if (options.autoCenter) res.center()
  206. return res ? new Points(res, new PointsMaterial({
  207. size: 0.1,
  208. vertexColors: res.hasAttribute('color'),
  209. })) : undefined
  210. }
  211. }, ['xyz'], ['text/plain+xyz'], false)
  212. }
  213. export const extraImportPlugins = [
  214. TDSLoadPlugin,
  215. ThreeMFLoadPlugin,
  216. ColladaLoadPlugin,
  217. AMFLoadPlugin,
  218. GCodeLoadPlugin,
  219. BVHLoadPlugin,
  220. VOXLoadPlugin,
  221. MDDLoadPlugin,
  222. PCDLoadPlugin,
  223. TiltLoadPlugin,
  224. VRMLLoadPlugin,
  225. LDrawLoadPlugin,
  226. VTKLoadPlugin,
  227. XYZLoadPlugin,
  228. ] as const