3DMLoader.js 20 KB

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