3DMLoader.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /**
  2. * @author Luis Fraguada / https://github.com/fraguada
  3. */
  4. import {
  5. BufferGeometryLoader,
  6. FileLoader,
  7. Loader,
  8. Object3D,
  9. MeshStandardMaterial,
  10. Mesh,
  11. Color,
  12. Points,
  13. PointsMaterial,
  14. Line,
  15. LineBasicMaterial,
  16. Matrix4
  17. } from "../../../build/three.module.js";
  18. import { CSS2DObject } from '../renderers/CSS2DRenderer.js';
  19. var Rhino3dmLoader = function ( manager ) {
  20. Loader.call( this, manager );
  21. this.libraryPath = '';
  22. this.libraryPending = null;
  23. this.libraryBinary = null;
  24. this.libraryConfig = {};
  25. this.workerLimit = 4;
  26. this.workerPool = [];
  27. this.workerNextTaskID = 1;
  28. this.workerSourceURL = '';
  29. this.workerConfig = {};
  30. };
  31. Rhino3dmLoader.taskCache = new WeakMap();
  32. Rhino3dmLoader.prototype = Object.assign( Object.create( Loader.prototype ), {
  33. constructor: Rhino3dmLoader,
  34. setLibraryPath: function ( path ) {
  35. this.libraryPath = path;
  36. return this;
  37. },
  38. setWorkerLimit: function ( workerLimit ) {
  39. this.workerLimit = workerLimit;
  40. return this;
  41. },
  42. load: function ( url, onLoad, onProgress, onError ) {
  43. var loader = new FileLoader( this.manager );
  44. loader.setPath( this.path );
  45. loader.setResponseType( 'arraybuffer' );
  46. loader.setRequestHeader( this.requestHeader );
  47. loader.load( url, ( buffer ) => {
  48. // Check for an existing task using this buffer. A transferred buffer cannot be transferred
  49. // again from this thread.
  50. if ( Rhino3dmLoader.taskCache.has( buffer ) ) {
  51. var cachedTask = Rhino3dmLoader.taskCache.get( buffer );
  52. return cachedTask.promise.then( onLoad ).catch( onError );
  53. }
  54. this.decodeObjects( buffer, url )
  55. .then( onLoad )
  56. .catch( onError );
  57. }, onProgress, onError );
  58. },
  59. debug: function () {
  60. console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) );
  61. },
  62. decodeObjects: function ( buffer, url ) {
  63. var worker;
  64. var taskID;
  65. var taskCost = buffer.byteLength;
  66. var objectPending = this._getWorker( taskCost )
  67. .then( ( _worker ) => {
  68. worker = _worker;
  69. taskID = this.workerNextTaskID ++; //hmmm
  70. return new Promise( ( resolve, reject ) => {
  71. worker._callbacks[ taskID ] = { resolve, reject };
  72. worker.postMessage( { type: 'decode', id: taskID, buffer }, [ buffer ] );
  73. //this.debug();
  74. } );
  75. } )
  76. .then( ( message ) => this._createGeometry( message.data ) );
  77. // Remove task from the task list.
  78. // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
  79. objectPending
  80. .catch( () => true )
  81. .then( () => {
  82. if ( worker && taskID ) {
  83. this._releaseTask( worker, taskID );
  84. //this.debug();
  85. }
  86. } );
  87. // Cache the task result.
  88. Rhino3dmLoader.taskCache.set( buffer, {
  89. url: url,
  90. promise: objectPending
  91. } );
  92. return objectPending;
  93. },
  94. parse: function () {
  95. // parsing logic goes here
  96. console.warn( 'THREE.3DMLoader: TODO: Implement parse function' );
  97. },
  98. _createMaterial: function ( material ) {
  99. if ( material === undefined ) {
  100. return new MeshStandardMaterial( {
  101. color: new Color( 1, 1, 1 ),
  102. metalness: 0.8,
  103. name: 'default',
  104. side: 2
  105. } );
  106. }
  107. var _diffuseColor = material.diffuseColor;
  108. var diffusecolor = new Color( _diffuseColor.r / 255.0, _diffuseColor.g / 255.0, _diffuseColor.b / 255.0 );
  109. if ( _diffuseColor.r === 0 && _diffuseColor.g === 0 && _diffuseColor.b === 0 ) {
  110. diffusecolor.r = 1;
  111. diffusecolor.g = 1;
  112. diffusecolor.b = 1;
  113. }
  114. return new MeshStandardMaterial( {
  115. color: diffusecolor,
  116. metalness: 0.8,
  117. name: material.name,
  118. side: 2
  119. } );
  120. },
  121. _createGeometry: function ( data ) {
  122. // console.log(data);
  123. var object = new Object3D();
  124. var instanceDefinitionObjects = [];
  125. var instanceDefinitions = [];
  126. var instanceReferences = [];
  127. object.userData[ 'layers' ] = data.layers;
  128. object.userData[ 'groups' ] = data.groups;
  129. var objects = data.objects;
  130. var materials = data.materials;
  131. for ( var i = 0; i < objects.length; i ++ ) {
  132. var obj = objects[ i ];
  133. var attributes = obj.attributes;
  134. switch ( obj.objectType ) {
  135. case 'InstanceDefinition':
  136. instanceDefinitions.push( obj );
  137. break;
  138. case 'InstanceReference':
  139. instanceReferences.push( obj );
  140. break;
  141. default:
  142. var material = this._createMaterial( materials[ attributes.materialIndex ] );
  143. var _object = this._createObject( obj, material );
  144. _object.visible = data.layers[ attributes.layerIndex ].visible;
  145. if ( attributes.isInstanceDefinitionObject ) {
  146. instanceDefinitionObjects.push( _object );
  147. } else {
  148. object.add( _object );
  149. }
  150. break;
  151. }
  152. }
  153. for ( var i = 0; i < instanceDefinitions.length; i ++ ) {
  154. var iDef = instanceDefinitions[ i ];
  155. var objects = [];
  156. for ( var j = 0; j < iDef.attributes.objectIds.length; j ++ ) {
  157. var objId = iDef.attributes.objectIds[ j ];
  158. for ( var p = 0; p < instanceDefinitionObjects.length; p ++ ) {
  159. var idoId = instanceDefinitionObjects[ p ].userData.attributes.id;
  160. if ( objId === idoId ) {
  161. objects.push( instanceDefinitionObjects[ p ] );
  162. }
  163. }
  164. }
  165. // Currently clones geometry and does not take advantage of instancing
  166. for ( var j = 0; j < instanceReferences.length; j ++ ) {
  167. var iRef = instanceReferences[ j ];
  168. if ( iRef.geometry.parentIdefId === iDef.attributes.id ) {
  169. var iRefObject = new Object3D();
  170. var xf = iRef.geometry.xform.array;
  171. var matrix = new Matrix4();
  172. matrix.set( xf[ 0 ], xf[ 1 ], xf[ 2 ], xf[ 3 ], xf[ 4 ], xf[ 5 ], xf[ 6 ], xf[ 7 ], xf[ 8 ], xf[ 9 ], xf[ 10 ], xf[ 11 ], xf[ 12 ], xf[ 13 ], xf[ 14 ], xf[ 15 ] );
  173. iRefObject.applyMatrix4( matrix );
  174. for ( var p = 0; p < objects.length; p ++ ) {
  175. iRefObject.add( objects[ p ].clone( true ) );
  176. }
  177. object.add( iRefObject );
  178. }
  179. }
  180. }
  181. return object;
  182. },
  183. _createObject: function ( obj, mat ) {
  184. var loader = new BufferGeometryLoader();
  185. var attributes = obj.attributes;
  186. switch ( obj.objectType ) {
  187. case 'Point':
  188. case 'PointSet':
  189. var geometry = loader.parse( obj.geometry );
  190. var material = new PointsMaterial( { sizeAttenuation: true, vertexColors: true } );
  191. var points = new Points( geometry, material );
  192. points.userData[ 'attributes' ] = attributes;
  193. points.userData[ 'objectType' ] = obj.objectType;
  194. return points;
  195. case 'Mesh':
  196. case 'Extrusion':
  197. var geometry = loader.parse( obj.geometry );
  198. var mesh = new Mesh( geometry, mat );
  199. mesh.castShadow = attributes.castsShadows;
  200. mesh.receiveShadow = attributes.receivesShadows;
  201. mesh.userData[ 'attributes' ] = attributes;
  202. mesh.userData[ 'objectType' ] = obj.objectType;
  203. return mesh;
  204. case 'Brep':
  205. var brepObject = new Object3D();
  206. for ( var j = 0; j < obj.geometry.length; j ++ ) {
  207. geometry = loader.parse( obj.geometry[ j ] );
  208. var mesh = new Mesh( geometry, mat );
  209. mesh.castShadow = attributes.castsShadows;
  210. mesh.receiveShadow = attributes.receivesShadows;
  211. brepObject.add( mesh );
  212. }
  213. brepObject.userData[ 'attributes' ] = attributes;
  214. brepObject.userData[ 'objectType' ] = obj.objectType;
  215. return brepObject;
  216. case 'Curve':
  217. geometry = loader.parse( obj.geometry );
  218. var _color = attributes.drawColor;
  219. var color = new Color( _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 );
  220. var lines = new Line( geometry, new LineBasicMaterial( { color: color } ) );
  221. lines.userData[ 'attributes' ] = attributes;
  222. lines.userData[ 'objectType' ] = obj.objectType;
  223. return lines;
  224. case 'TextDot':
  225. geometry = obj.geometry;
  226. var dotDiv = document.createElement( 'div' );
  227. dotDiv.style.fontFamily = geometry.fontFace;
  228. dotDiv.style.fontSize = `${geometry.fontHeight}px`;
  229. dotDiv.style.marginTop = '-1em';
  230. dotDiv.style.color = '#FFF';
  231. dotDiv.style.padding = '2px';
  232. dotDiv.style.paddingRight = '5px';
  233. dotDiv.style.paddingLeft = '5px';
  234. dotDiv.style.borderRadius = '5px';
  235. var color = attributes.drawColor;
  236. dotDiv.style.background = `rgba(${color.r},${color.g},${color.b},${color.a})`;
  237. dotDiv.textContent = geometry.text;
  238. var dot = new CSS2DObject( dotDiv );
  239. var location = geometry.point;
  240. dot.position.set( location[ 0 ], location[ 1 ], location[ 2 ] );
  241. dot.userData[ 'attributes' ] = attributes;
  242. dot.userData[ 'objectType' ] = obj.objectType;
  243. return dot;
  244. }
  245. },
  246. _initLibrary: function () {
  247. if ( ! this.libraryPending ) {
  248. // Load rhino3dm wrapper.
  249. var jsLoader = new FileLoader( this.manager );
  250. jsLoader.setPath( this.libraryPath );
  251. var jsContent = new Promise( ( resolve, reject ) => {
  252. jsLoader.load( 'rhino3dm.js', resolve, undefined, reject );
  253. } );
  254. // Load rhino3dm WASM binary.
  255. var binaryLoader = new FileLoader( this.manager );
  256. binaryLoader.setPath( this.libraryPath );
  257. binaryLoader.setResponseType( 'arraybuffer' );
  258. var binaryContent = new Promise( ( resolve, reject ) => {
  259. binaryLoader.load( 'rhino3dm.wasm', resolve, undefined, reject );
  260. } );
  261. this.libraryPending = Promise.all( [ jsContent, binaryContent ] )
  262. .then( ( [ jsContent, binaryContent ] ) => {
  263. //this.libraryBinary = binaryContent;
  264. this.libraryConfig.wasmBinary = binaryContent;
  265. var fn = Rhino3dmLoader.Rhino3dmWorker.toString();
  266. var body = [
  267. '/* rhino3dm.js */',
  268. jsContent,
  269. '/* worker */',
  270. fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
  271. ].join( '\n' );
  272. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  273. } );
  274. }
  275. return this.libraryPending;
  276. },
  277. _getWorker: function ( taskCost ) {
  278. return this._initLibrary().then( () => {
  279. if ( this.workerPool.length < this.workerLimit ) {
  280. var worker = new Worker( this.workerSourceURL );
  281. worker._callbacks = {};
  282. worker._taskCosts = {};
  283. worker._taskLoad = 0;
  284. worker.postMessage( {
  285. type: 'init',
  286. libraryConfig: this.libraryConfig
  287. } );
  288. worker.onmessage = function ( e ) {
  289. var message = e.data;
  290. switch ( message.type ) {
  291. case 'decode':
  292. worker._callbacks[ message.id ].resolve( message );
  293. break;
  294. case 'error':
  295. worker._callbacks[ message.id ].reject( message );
  296. break;
  297. default:
  298. console.error( 'THREE.Rhino3dmLoader: Unexpected message, "' + message.type + '"' );
  299. }
  300. };
  301. this.workerPool.push( worker );
  302. } else {
  303. this.workerPool.sort( function ( a, b ) {
  304. return a._taskLoad > b._taskLoad ? - 1 : 1;
  305. } );
  306. }
  307. var worker = this.workerPool[ this.workerPool.length - 1 ];
  308. worker._taskLoad += taskCost;
  309. return worker;
  310. } );
  311. },
  312. _releaseTask: function ( worker, taskID ) {
  313. worker._taskLoad -= worker._taskCosts[ taskID ];
  314. delete worker._callbacks[ taskID ];
  315. delete worker._taskCosts[ taskID ];
  316. },
  317. dispose: function () {
  318. for ( var i = 0; i < this.workerPool.length; ++ i ) {
  319. this.workerPool[ i ].terminate();
  320. }
  321. this.workerPool.length = 0;
  322. return this;
  323. }
  324. } );
  325. /* WEB WORKER */
  326. Rhino3dmLoader.Rhino3dmWorker = function () {
  327. var libraryPending;
  328. var libraryConfig;
  329. var rhino;
  330. onmessage = function ( e ) {
  331. var message = e.data;
  332. switch ( message.type ) {
  333. case 'init':
  334. libraryConfig = message.libraryConfig;
  335. var wasmBinary = libraryConfig.wasmBinary;
  336. var RhinoModule;
  337. libraryPending = new Promise( function ( resolve ) {
  338. /* Like Basis Loader */
  339. RhinoModule = { wasmBinary, onRuntimeInitialized: resolve };
  340. rhino3dm( RhinoModule );
  341. } ).then( () => {
  342. rhino = RhinoModule;
  343. } );
  344. break;
  345. case 'decode':
  346. var buffer = message.buffer;
  347. libraryPending.then( () => {
  348. var data = decodeObjects( rhino, buffer );
  349. self.postMessage( { type: 'decode', id: message.id, data } );
  350. } );
  351. break;
  352. }
  353. };
  354. function decodeObjects( rhino, buffer ) {
  355. var arr = new Uint8Array( buffer );
  356. var doc = rhino.File3dm.fromByteArray( arr );
  357. var objects = [];
  358. var materials = [];
  359. var layers = [];
  360. var views = [];
  361. var namedViews = [];
  362. var groups = [];
  363. //Handle objects
  364. for ( var i = 0; i < doc.objects().count; i ++ ) {
  365. var _object = doc.objects().get( i );
  366. var object = extractObjectData( _object, doc );
  367. if ( object !== undefined ) {
  368. objects.push( object );
  369. }
  370. _object.delete();
  371. }
  372. // Handle instance definitions
  373. for ( var i = 0; i < doc.instanceDefinitions().count(); i ++ ) {
  374. var idef = doc.instanceDefinitions().get( i );
  375. var idefAttributes = extractProperties( idef );
  376. idefAttributes.objectIds = idef.getObjectIds();
  377. objects.push( { geometry: null, attributes: idefAttributes, objectType: 'InstanceDefinition' } );
  378. }
  379. // Handle materials
  380. for ( var i = 0; i < doc.materials().count(); i ++ ) {
  381. var _material = doc.materials().get( i );
  382. var materialProperties = extractProperties( _material );
  383. var pbMaterialProperties = extractProperties( _material.physicallyBased() );
  384. var material = Object.assign( materialProperties, pbMaterialProperties );
  385. materials.push( material );
  386. _material.delete();
  387. }
  388. // Handle layers
  389. for ( var i = 0; i < doc.layers().count(); i ++ ) {
  390. var _layer = doc.layers().get( i );
  391. var layer = extractProperties( _layer );
  392. layers.push( layer );
  393. _layer.delete();
  394. }
  395. // Handle views
  396. for ( var i = 0; i < doc.views().count(); i ++ ) {
  397. var _view = doc.views().get( i );
  398. var view = extractProperties( _view );
  399. views.push( view );
  400. _view.delete();
  401. }
  402. // Handle named views
  403. for ( var i = 0; i < doc.namedViews().count(); i ++ ) {
  404. var _namedView = doc.namedViews().get( i );
  405. var namedView = extractProperties( _namedView );
  406. namedViews.push( namedView );
  407. _namedView.delete();
  408. }
  409. // Handle groups
  410. for ( var i = 0; i < doc.groups().count(); i ++ ) {
  411. var _group = doc.groups().get( i );
  412. var group = extractProperties( _group );
  413. groups.push( group );
  414. _group.delete();
  415. }
  416. // Handle settings
  417. var settings = extractProperties( doc.settings() );
  418. //TODO: Handle other document stuff like dimstyles, instance definitions, bitmaps etc.
  419. // Handle dimstyles
  420. // console.log(`Dimstyle Count: ${doc.dimstyles().count()}`);
  421. // Handle bitmaps
  422. // console.log(`Bitmap Count: ${doc.bitmaps().count()}`);
  423. // Handle instance definitions
  424. // console.log(`Instance Definitions Count: ${doc.instanceDefinitions().count()}`);
  425. // Handle strings -- this seems to be broken at the moment in rhino3dm
  426. // console.log(`Strings Count: ${doc.strings().count()}`);
  427. /*
  428. for( var i = 0; i < doc.strings().count(); i++ ){
  429. var _string= doc.strings().get( i );
  430. console.log(_string);
  431. var string = extractProperties( _group );
  432. strings.push( string );
  433. _string.delete();
  434. }
  435. */
  436. doc.delete();
  437. return { objects, materials, layers, views, namedViews, groups, settings };
  438. }
  439. function extractObjectData( object, doc ) {
  440. var _geometry = object.geometry();
  441. var _attributes = object.attributes();
  442. var objectType = _geometry.objectType;
  443. var geometry = null;
  444. var attributes = null;
  445. // skip instance definition objects
  446. //if( _attributes.isInstanceDefinitionObject ) { continue; }
  447. // TODO: handle other geometry types
  448. switch ( objectType ) {
  449. case rhino.ObjectType.Curve:
  450. var pts = curveToPoints( _geometry, 100 );
  451. var position = {};
  452. var color = {};
  453. var attributes = {};
  454. var data = {};
  455. position.itemSize = 3;
  456. position.type = 'Float32Array';
  457. position.array = [];
  458. for ( var j = 0; j < pts.length; j ++ ) {
  459. position.array.push( pts[ j ][ 0 ] );
  460. position.array.push( pts[ j ][ 1 ] );
  461. position.array.push( pts[ j ][ 2 ] );
  462. }
  463. attributes.position = position;
  464. data.attributes = attributes;
  465. geometry = { data };
  466. break;
  467. case rhino.ObjectType.Point:
  468. var pt = _geometry.location;
  469. var position = {};
  470. var color = {};
  471. var attributes = {};
  472. var data = {};
  473. position.itemSize = 3;
  474. position.type = 'Float32Array';
  475. position.array = [ pt[ 0 ], pt[ 1 ], pt[ 2 ] ];
  476. var _color = _attributes.drawColor( doc );
  477. color.itemSize = 3;
  478. color.type = 'Float32Array';
  479. color.array = [ _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 ];
  480. attributes.position = position;
  481. attributes.color = color;
  482. data.attributes = attributes;
  483. geometry = { data };
  484. break;
  485. case rhino.ObjectType.PointSet:
  486. case rhino.ObjectType.Mesh:
  487. geometry = _geometry.toThreejsJSON();
  488. break;
  489. case rhino.ObjectType.Brep:
  490. var faces = _geometry.faces();
  491. geometry = [];
  492. for ( var faceIndex = 0; faceIndex < faces.count; faceIndex ++ ) {
  493. var face = faces.get( faceIndex );
  494. var mesh = face.getMesh( rhino.MeshType.Any );
  495. if ( mesh ) {
  496. geometry.push( mesh.toThreejsJSON() );
  497. mesh.delete();
  498. }
  499. face.delete();
  500. }
  501. faces.delete();
  502. break;
  503. case rhino.ObjectType.Extrusion:
  504. var mesh = _geometry.getMesh( rhino.MeshType.Any );
  505. if ( mesh ) {
  506. geometry = mesh.toThreejsJSON();
  507. mesh.delete();
  508. }
  509. break;
  510. case rhino.ObjectType.TextDot:
  511. geometry = extractProperties( _geometry );
  512. break;
  513. case rhino.ObjectType.InstanceReference:
  514. geometry = extractProperties( _geometry );
  515. geometry.xform = extractProperties( _geometry.xform );
  516. geometry.xform.array = _geometry.xform.toFloatArray( true );
  517. break;
  518. /*
  519. case rhino.ObjectType.Light:
  520. case rhino.ObjectType.Annotation:
  521. case rhino.ObjectType.Hatch:
  522. case rhino.ObjectType.SubD:
  523. case rhino.ObjectType.ClipPlane:
  524. */
  525. default:
  526. console.warn( `THREE.3DMLoader: TODO: Implement ${objectType.constructor.name}` );
  527. break;
  528. }
  529. if ( geometry ) {
  530. var attributes = extractProperties( _attributes );
  531. if ( _attributes.groupCount > 0 ) {
  532. attributes.groupIds = _attributes.getGroupList();
  533. }
  534. attributes.drawColor = _attributes.drawColor( doc );
  535. objectType = objectType.constructor.name;
  536. objectType = objectType.substring( 11, objectType.length );
  537. return { geometry, attributes, objectType };
  538. }
  539. }
  540. function extractProperties( object ) {
  541. var result = {};
  542. for ( var property in object ) {
  543. if ( typeof object[ property ] !== 'function' ) {
  544. result[ property ] = object[ property ];
  545. } else {
  546. // console.log(`${property}: ${object[property]}`);
  547. }
  548. }
  549. return result;
  550. }
  551. function curveToPoints( curve, pointLimit ) {
  552. var pointCount = pointLimit;
  553. var rc = [];
  554. var ts = [];
  555. if ( curve instanceof rhino.LineCurve ) {
  556. return [ curve.pointAtStart, curve.pointAtEnd ];
  557. }
  558. if ( curve instanceof rhino.PolylineCurve ) {
  559. pointCount = curve.pointCount;
  560. for ( var i = 0; i < pointCount; i ++ ) {
  561. rc.push( curve.point( i ) );
  562. }
  563. return rc;
  564. }
  565. if ( curve instanceof rhino.PolyCurve ) {
  566. var segmentCount = curve.segmentCount;
  567. for ( var i = 0; i < segmentCount; i ++ ) {
  568. var segment = curve.segmentCurve( i );
  569. var segmentArray = curveToPoints( segment );
  570. rc = rc.concat( segmentArray );
  571. segment.delete();
  572. }
  573. return rc;
  574. }
  575. if ( curve instanceof rhino.NurbsCurve && curve.degree === 1 ) {
  576. // console.info( 'degree 1 curve' );
  577. }
  578. var domain = curve.domain;
  579. var divisions = pointCount - 1.0;
  580. for ( var j = 0; j < pointCount; j ++ ) {
  581. var t = domain[ 0 ] + ( j / divisions ) * ( domain[ 1 ] - domain[ 0 ] );
  582. if ( t === domain[ 0 ] || t === domain[ 1 ] ) {
  583. ts.push( t );
  584. continue;
  585. }
  586. var tan = curve.tangentAt( t );
  587. var prevTan = curve.tangentAt( ts.slice( - 1 )[ 0 ] );
  588. // Duplicaated from THREE.Vector3
  589. // How to pass imports to worker?
  590. var tS = tan[ 0 ] * tan[ 0 ] + tan[ 1 ] * tan[ 1 ] + tan[ 2 ] * tan[ 2 ];
  591. var ptS = prevTan[ 0 ] * prevTan[ 0 ] + prevTan[ 1 ] * prevTan[ 1 ] + prevTan[ 2 ] * prevTan[ 2 ];
  592. var denominator = Math.sqrt( tS * ptS );
  593. var angle;
  594. if ( denominator === 0 ) {
  595. angle = Math.PI / 2;
  596. } else {
  597. var theta = ( tan.x * prevTan.x + tan.y * prevTan.y + tan.z * prevTan.z ) / denominator;
  598. angle = Math.acos( Math.max( - 1, Math.min( 1, theta ) ) );
  599. }
  600. if ( angle < 0.1 ) continue;
  601. ts.push( t );
  602. }
  603. rc = ts.map( t => curve.pointAt( t ) );
  604. return rc;
  605. }
  606. };
  607. export { Rhino3dmLoader };