3DMLoader.js 20 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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.load( url, ( buffer ) => {
  47. // Check for an existing task using this buffer. A transferred buffer cannot be transferred
  48. // again from this thread.
  49. if ( Rhino3dmLoader.taskCache.has( buffer ) ) {
  50. var cachedTask = Rhino3dmLoader.taskCache.get( buffer );
  51. return cachedTask.promise.then( onLoad ).catch( onError );
  52. }
  53. this.decodeObjects( buffer, url )
  54. .then( onLoad )
  55. .catch( onError );
  56. }, onProgress, onError );
  57. },
  58. debug: function () {
  59. console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) );
  60. },
  61. decodeObjects: function ( buffer, url ) {
  62. var worker;
  63. var taskID;
  64. var taskCost = buffer.byteLength;
  65. var objectPending = this._getWorker( taskCost )
  66. .then( ( _worker ) => {
  67. worker = _worker;
  68. taskID = this.workerNextTaskID ++; //hmmm
  69. return new Promise( ( resolve, reject ) => {
  70. worker._callbacks[ taskID ] = { resolve, reject };
  71. worker.postMessage( { type: 'decode', id: taskID, buffer }, [ buffer ] );
  72. //this.debug();
  73. } );
  74. } )
  75. .then( ( message ) => this._createGeometry( message.data ) );
  76. // Remove task from the task list.
  77. // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
  78. objectPending
  79. .catch( () => true )
  80. .then( () => {
  81. if ( worker && taskID ) {
  82. this._releaseTask( worker, taskID );
  83. //this.debug();
  84. }
  85. } );
  86. // Cache the task result.
  87. Rhino3dmLoader.taskCache.set( buffer, {
  88. url: url,
  89. promise: objectPending
  90. } );
  91. return objectPending;
  92. },
  93. parse: function () {
  94. // parsing logic goes here
  95. console.warn( 'THREE.3DMLoader: TODO: Implement parse function' );
  96. },
  97. _createMaterial: function ( material ) {
  98. if ( material === undefined ) {
  99. return new MeshStandardMaterial( {
  100. color: new Color( 1, 1, 1 ),
  101. metalness: 0.8,
  102. name: 'default',
  103. side: 2
  104. } );
  105. }
  106. var _diffuseColor = material.diffuseColor;
  107. var diffusecolor = new Color( _diffuseColor.r / 255.0, _diffuseColor.g / 255.0, _diffuseColor.b / 255.0 );
  108. if ( _diffuseColor.r === 0 && _diffuseColor.g === 0 && _diffuseColor.b === 0 ) {
  109. diffusecolor.r = 1;
  110. diffusecolor.g = 1;
  111. diffusecolor.b = 1;
  112. }
  113. return new MeshStandardMaterial( {
  114. color: diffusecolor,
  115. metalness: 0.8,
  116. name: material.name,
  117. side: 2
  118. } );
  119. },
  120. _createGeometry: function ( data ) {
  121. // console.log(data);
  122. var object = new Object3D();
  123. var instanceDefinitionObjects = [];
  124. var instanceDefinitions = [];
  125. var instanceReferences = [];
  126. object.userData[ 'layers' ] = data.layers;
  127. object.userData[ 'groups' ] = data.groups;
  128. var objects = data.objects;
  129. var materials = data.materials;
  130. for ( var i = 0; i < objects.length; i ++ ) {
  131. var obj = objects[ i ];
  132. var attributes = obj.attributes;
  133. switch ( obj.objectType ) {
  134. case 'InstanceDefinition':
  135. instanceDefinitions.push( obj );
  136. break;
  137. case 'InstanceReference':
  138. instanceReferences.push( obj );
  139. break;
  140. default:
  141. var material = this._createMaterial( materials[ attributes.materialIndex ] );
  142. var _object = this._createObject( obj, material );
  143. _object.visible = data.layers[ attributes.layerIndex ].visible;
  144. if ( attributes.isInstanceDefinitionObject ) {
  145. instanceDefinitionObjects.push( _object );
  146. } else {
  147. object.add( _object );
  148. }
  149. break;
  150. }
  151. }
  152. for ( var i = 0; i < instanceDefinitions.length; i ++ ) {
  153. var iDef = instanceDefinitions[ i ];
  154. var objects = [];
  155. for ( var j = 0; j < iDef.attributes.objectIds.length; j ++ ) {
  156. var objId = iDef.attributes.objectIds[ j ];
  157. for ( var p = 0; p < instanceDefinitionObjects.length; p ++ ) {
  158. var idoId = instanceDefinitionObjects[ p ].userData.attributes.id;
  159. if ( objId === idoId ) {
  160. objects.push( instanceDefinitionObjects[ p ] );
  161. }
  162. }
  163. }
  164. // Currently clones geometry and does not take advantage of instancing
  165. for ( var j = 0; j < instanceReferences.length; j ++ ) {
  166. var iRef = instanceReferences[ j ];
  167. if ( iRef.geometry.parentIdefId === iDef.attributes.id ) {
  168. var iRefObject = new Object3D();
  169. var xf = iRef.geometry.xform.array;
  170. var matrix = new Matrix4();
  171. 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 ] );
  172. iRefObject.applyMatrix4( matrix );
  173. for ( var p = 0; p < objects.length; p ++ ) {
  174. iRefObject.add( objects[ p ].clone( true ) );
  175. }
  176. object.add( iRefObject );
  177. }
  178. }
  179. }
  180. return object;
  181. },
  182. _createObject: function ( obj, mat ) {
  183. var loader = new BufferGeometryLoader();
  184. var attributes = obj.attributes;
  185. switch ( obj.objectType ) {
  186. case 'Point':
  187. case 'PointSet':
  188. var geometry = loader.parse( obj.geometry );
  189. var material = new PointsMaterial( { sizeAttenuation: true, vertexColors: true } );
  190. var points = new Points( geometry, material );
  191. points.userData[ 'attributes' ] = attributes;
  192. points.userData[ 'objectType' ] = obj.objectType;
  193. return points;
  194. case 'Mesh':
  195. case 'Extrusion':
  196. var geometry = loader.parse( obj.geometry );
  197. var mesh = new Mesh( geometry, mat );
  198. mesh.castShadow = attributes.castsShadows;
  199. mesh.receiveShadow = attributes.receivesShadows;
  200. mesh.userData[ 'attributes' ] = attributes;
  201. mesh.userData[ 'objectType' ] = obj.objectType;
  202. return mesh;
  203. case 'Brep':
  204. var brepObject = new Object3D();
  205. for ( var j = 0; j < obj.geometry.length; j ++ ) {
  206. geometry = loader.parse( obj.geometry[ j ] );
  207. var mesh = new Mesh( geometry, mat );
  208. mesh.castShadow = attributes.castsShadows;
  209. mesh.receiveShadow = attributes.receivesShadows;
  210. brepObject.add( mesh );
  211. }
  212. brepObject.userData[ 'attributes' ] = attributes;
  213. brepObject.userData[ 'objectType' ] = obj.objectType;
  214. return brepObject;
  215. case 'Curve':
  216. geometry = loader.parse( obj.geometry );
  217. var _color = attributes.drawColor;
  218. var color = new Color( _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 );
  219. var lines = new Line( geometry, new LineBasicMaterial( { color: color } ) );
  220. lines.userData[ 'attributes' ] = attributes;
  221. lines.userData[ 'objectType' ] = obj.objectType;
  222. return lines;
  223. case 'TextDot':
  224. geometry = obj.geometry;
  225. var dotDiv = document.createElement( 'div' );
  226. dotDiv.style.fontFamily = geometry.fontFace;
  227. dotDiv.style.fontSize = `${geometry.fontHeight}px`;
  228. dotDiv.style.marginTop = '-1em';
  229. dotDiv.style.color = '#FFF';
  230. dotDiv.style.padding = '2px';
  231. dotDiv.style.paddingRight = '5px';
  232. dotDiv.style.paddingLeft = '5px';
  233. dotDiv.style.borderRadius = '5px';
  234. var color = attributes.drawColor;
  235. dotDiv.style.background = `rgba(${color.r},${color.g},${color.b},${color.a})`;
  236. dotDiv.textContent = geometry.text;
  237. var dot = new CSS2DObject( dotDiv );
  238. var location = geometry.point;
  239. dot.position.set( location[ 0 ], location[ 1 ], location[ 2 ] );
  240. dot.userData[ 'attributes' ] = attributes;
  241. dot.userData[ 'objectType' ] = obj.objectType;
  242. return dot;
  243. }
  244. },
  245. _initLibrary: function () {
  246. if ( ! this.libraryPending ) {
  247. // Load rhino3dm wrapper.
  248. var jsLoader = new FileLoader( this.manager );
  249. jsLoader.setPath( this.libraryPath );
  250. var jsContent = new Promise( ( resolve, reject ) => {
  251. jsLoader.load( 'rhino3dm.js', resolve, undefined, reject );
  252. } );
  253. // Load rhino3dm WASM binary.
  254. var binaryLoader = new FileLoader( this.manager );
  255. binaryLoader.setPath( this.libraryPath );
  256. binaryLoader.setResponseType( 'arraybuffer' );
  257. var binaryContent = new Promise( ( resolve, reject ) => {
  258. binaryLoader.load( 'rhino3dm.wasm', resolve, undefined, reject );
  259. } );
  260. this.libraryPending = Promise.all( [ jsContent, binaryContent ] )
  261. .then( ( [ jsContent, binaryContent ] ) => {
  262. //this.libraryBinary = binaryContent;
  263. this.libraryConfig.wasmBinary = binaryContent;
  264. var fn = Rhino3dmLoader.Rhino3dmWorker.toString();
  265. var body = [
  266. '/* rhino3dm.js */',
  267. jsContent,
  268. '/* worker */',
  269. fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
  270. ].join( '\n' );
  271. this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
  272. } );
  273. }
  274. return this.libraryPending;
  275. },
  276. _getWorker: function ( taskCost ) {
  277. return this._initLibrary().then( () => {
  278. if ( this.workerPool.length < this.workerLimit ) {
  279. var worker = new Worker( this.workerSourceURL );
  280. worker._callbacks = {};
  281. worker._taskCosts = {};
  282. worker._taskLoad = 0;
  283. worker.postMessage( {
  284. type: 'init',
  285. libraryConfig: this.libraryConfig
  286. } );
  287. worker.onmessage = function ( e ) {
  288. var message = e.data;
  289. switch ( message.type ) {
  290. case 'decode':
  291. worker._callbacks[ message.id ].resolve( message );
  292. break;
  293. case 'error':
  294. worker._callbacks[ message.id ].reject( message );
  295. break;
  296. default:
  297. console.error( 'THREE.Rhino3dmLoader: Unexpected message, "' + message.type + '"' );
  298. }
  299. };
  300. this.workerPool.push( worker );
  301. } else {
  302. this.workerPool.sort( function ( a, b ) {
  303. return a._taskLoad > b._taskLoad ? - 1 : 1;
  304. } );
  305. }
  306. var worker = this.workerPool[ this.workerPool.length - 1 ];
  307. worker._taskLoad += taskCost;
  308. return worker;
  309. } );
  310. },
  311. _releaseTask: function ( worker, taskID ) {
  312. worker._taskLoad -= worker._taskCosts[ taskID ];
  313. delete worker._callbacks[ taskID ];
  314. delete worker._taskCosts[ taskID ];
  315. },
  316. dispose: function () {
  317. for ( var i = 0; i < this.workerPool.length; ++ i ) {
  318. this.workerPool[ i ].terminate();
  319. }
  320. this.workerPool.length = 0;
  321. return this;
  322. }
  323. } );
  324. /* WEB WORKER */
  325. Rhino3dmLoader.Rhino3dmWorker = function () {
  326. var libraryPending;
  327. var libraryConfig;
  328. var rhino;
  329. onmessage = function ( e ) {
  330. var message = e.data;
  331. switch ( message.type ) {
  332. case 'init':
  333. libraryConfig = message.libraryConfig;
  334. var wasmBinary = libraryConfig.wasmBinary;
  335. var RhinoModule;
  336. libraryPending = new Promise( function ( resolve ) {
  337. /* Like Basis Loader */
  338. RhinoModule = { wasmBinary, onRuntimeInitialized: resolve };
  339. rhino3dm( RhinoModule );
  340. } ).then( () => {
  341. rhino = RhinoModule;
  342. } );
  343. break;
  344. case 'decode':
  345. var buffer = message.buffer;
  346. libraryPending.then( () => {
  347. var data = decodeObjects( rhino, buffer );
  348. self.postMessage( { type: 'decode', id: message.id, data } );
  349. } );
  350. break;
  351. }
  352. };
  353. function decodeObjects( rhino, buffer ) {
  354. var arr = new Uint8Array( buffer );
  355. var doc = rhino.File3dm.fromByteArray( arr );
  356. var objects = [];
  357. var materials = [];
  358. var layers = [];
  359. var views = [];
  360. var namedViews = [];
  361. var groups = [];
  362. var strings = [];
  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. _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. tS = tan[ 0 ] * tan[ 0 ] + tan[ 1 ] * tan[ 1 ] + tan[ 2 ] * tan[ 2 ];
  591. 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 };