3DMLoader.js 20 KB

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