1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495 |
- import {
- BufferGeometryLoader,
- FileLoader,
- Loader,
- Object3D,
- MeshStandardMaterial,
- Mesh,
- Color,
- Points,
- PointsMaterial,
- Line,
- LineBasicMaterial,
- Matrix4,
- DirectionalLight,
- PointLight,
- SpotLight,
- RectAreaLight,
- Vector3,
- Sprite,
- SpriteMaterial,
- CanvasTexture,
- LinearFilter,
- ClampToEdgeWrapping,
- RepeatWrapping,
- TextureLoader
- } from 'three';
- const _taskCache = new WeakMap();
- class Rhino3dmLoader extends Loader {
- constructor( manager ) {
- super( manager );
- this.libraryPath = '';
- this.libraryPending = null;
- this.libraryBinary = null;
- this.libraryConfig = {};
- this.url = '';
- this.workerLimit = 4;
- this.workerPool = [];
- this.workerNextTaskID = 1;
- this.workerSourceURL = '';
- this.workerConfig = {};
- this.materials = [];
- this.warnings = [];
- }
- setLibraryPath( path ) {
- this.libraryPath = path;
- return this;
- }
- setWorkerLimit( workerLimit ) {
- this.workerLimit = workerLimit;
- return this;
- }
- load( url, onLoad, onProgress, onError ) {
- const loader = new FileLoader( this.manager );
- loader.setPath( this.path );
- loader.setResponseType( 'arraybuffer' );
- loader.setRequestHeader( this.requestHeader );
- this.url = url;
- loader.load( url, ( buffer ) => {
- // Check for an existing task using this buffer. A transferred buffer cannot be transferred
- // again from this thread.
- if ( _taskCache.has( buffer ) ) {
- const cachedTask = _taskCache.get( buffer );
- return cachedTask.promise.then( onLoad ).catch( onError );
- }
- this.decodeObjects( buffer, url )
- .then( result => {
- result.userData.warnings = this.warnings;
- onLoad( result );
- } )
- .catch( e => onError( e ) );
- }, onProgress, onError );
- }
- debug() {
- console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) );
- }
- decodeObjects( buffer, url ) {
- let worker;
- let taskID;
- const taskCost = buffer.byteLength;
- const objectPending = this._getWorker( taskCost )
- .then( ( _worker ) => {
- worker = _worker;
- taskID = this.workerNextTaskID ++;
- return new Promise( ( resolve, reject ) => {
- worker._callbacks[ taskID ] = { resolve, reject };
- worker.postMessage( { type: 'decode', id: taskID, buffer }, [ buffer ] );
- // this.debug();
- } );
- } )
- .then( ( message ) => this._createGeometry( message.data ) )
- .catch( e => {
- throw e;
- } );
- // Remove task from the task list.
- // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416)
- objectPending
- .catch( () => true )
- .then( () => {
- if ( worker && taskID ) {
- this._releaseTask( worker, taskID );
- //this.debug();
- }
- } );
- // Cache the task result.
- _taskCache.set( buffer, {
- url: url,
- promise: objectPending
- } );
- return objectPending;
- }
- parse( data, onLoad, onError ) {
- this.decodeObjects( data, '' )
- .then( result => {
- result.userData.warnings = this.warnings;
- onLoad( result );
- } )
- .catch( e => onError( e ) );
- }
- _compareMaterials( material ) {
- const mat = {};
- mat.name = material.name;
- mat.color = {};
- mat.color.r = material.color.r;
- mat.color.g = material.color.g;
- mat.color.b = material.color.b;
- mat.type = material.type;
- for ( let i = 0; i < this.materials.length; i ++ ) {
- const m = this.materials[ i ];
- const _mat = {};
- _mat.name = m.name;
- _mat.color = {};
- _mat.color.r = m.color.r;
- _mat.color.g = m.color.g;
- _mat.color.b = m.color.b;
- _mat.type = m.type;
- if ( JSON.stringify( mat ) === JSON.stringify( _mat ) ) {
- return m;
- }
- }
- this.materials.push( material );
- return material;
- }
- _createMaterial( material ) {
- if ( material === undefined ) {
- return new MeshStandardMaterial( {
- color: new Color( 1, 1, 1 ),
- metalness: 0.8,
- name: 'default',
- side: 2
- } );
- }
- const _diffuseColor = material.diffuseColor;
- const diffusecolor = new Color( _diffuseColor.r / 255.0, _diffuseColor.g / 255.0, _diffuseColor.b / 255.0 );
- if ( _diffuseColor.r === 0 && _diffuseColor.g === 0 && _diffuseColor.b === 0 ) {
- diffusecolor.r = 1;
- diffusecolor.g = 1;
- diffusecolor.b = 1;
- }
- // console.log( material );
- const mat = new MeshStandardMaterial( {
- color: diffusecolor,
- name: material.name,
- side: 2,
- transparent: material.transparency > 0 ? true : false,
- opacity: 1.0 - material.transparency
- } );
- const textureLoader = new TextureLoader();
- for ( let i = 0; i < material.textures.length; i ++ ) {
- const texture = material.textures[ i ];
- if ( texture.image !== null ) {
- const map = textureLoader.load( texture.image );
- switch ( texture.type ) {
- case 'Diffuse':
- mat.map = map;
- break;
- case 'Bump':
- mat.bumpMap = map;
- break;
- case 'Transparency':
- mat.alphaMap = map;
- mat.transparent = true;
- break;
- case 'Emap':
- mat.envMap = map;
- break;
- }
- map.wrapS = texture.wrapU === 0 ? RepeatWrapping : ClampToEdgeWrapping;
- map.wrapT = texture.wrapV === 0 ? RepeatWrapping : ClampToEdgeWrapping;
- map.repeat.set( texture.repeat[ 0 ], texture.repeat[ 1 ] );
- }
- }
- return mat;
- }
- _createGeometry( data ) {
- // console.log(data);
- const object = new Object3D();
- const instanceDefinitionObjects = [];
- const instanceDefinitions = [];
- const instanceReferences = [];
- object.userData[ 'layers' ] = data.layers;
- object.userData[ 'groups' ] = data.groups;
- object.userData[ 'settings' ] = data.settings;
- object.userData[ 'objectType' ] = 'File3dm';
- object.userData[ 'materials' ] = null;
- object.name = this.url;
- let objects = data.objects;
- const materials = data.materials;
- for ( let i = 0; i < objects.length; i ++ ) {
- const obj = objects[ i ];
- const attributes = obj.attributes;
- switch ( obj.objectType ) {
- case 'InstanceDefinition':
- instanceDefinitions.push( obj );
- break;
- case 'InstanceReference':
- instanceReferences.push( obj );
- break;
- default:
- let _object;
- if ( attributes.materialIndex >= 0 ) {
- const rMaterial = materials[ attributes.materialIndex ];
- let material = this._createMaterial( rMaterial );
- material = this._compareMaterials( material );
- _object = this._createObject( obj, material );
- } else {
- const material = this._createMaterial();
- _object = this._createObject( obj, material );
- }
- if ( _object === undefined ) {
- continue;
- }
- const layer = data.layers[ attributes.layerIndex ];
- _object.visible = layer ? data.layers[ attributes.layerIndex ].visible : true;
- if ( attributes.isInstanceDefinitionObject ) {
- instanceDefinitionObjects.push( _object );
- } else {
- object.add( _object );
- }
- break;
- }
- }
- for ( let i = 0; i < instanceDefinitions.length; i ++ ) {
- const iDef = instanceDefinitions[ i ];
- objects = [];
- for ( let j = 0; j < iDef.attributes.objectIds.length; j ++ ) {
- const objId = iDef.attributes.objectIds[ j ];
- for ( let p = 0; p < instanceDefinitionObjects.length; p ++ ) {
- const idoId = instanceDefinitionObjects[ p ].userData.attributes.id;
- if ( objId === idoId ) {
- objects.push( instanceDefinitionObjects[ p ] );
- }
- }
- }
- // Currently clones geometry and does not take advantage of instancing
- for ( let j = 0; j < instanceReferences.length; j ++ ) {
- const iRef = instanceReferences[ j ];
- if ( iRef.geometry.parentIdefId === iDef.attributes.id ) {
- const iRefObject = new Object3D();
- const xf = iRef.geometry.xform.array;
- const matrix = new Matrix4();
- 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 ] );
- iRefObject.applyMatrix4( matrix );
- for ( let p = 0; p < objects.length; p ++ ) {
- iRefObject.add( objects[ p ].clone( true ) );
- }
- object.add( iRefObject );
- }
- }
- }
- object.userData[ 'materials' ] = this.materials;
- return object;
- }
- _createObject( obj, mat ) {
- const loader = new BufferGeometryLoader();
- const attributes = obj.attributes;
- let geometry, material, _color, color;
- switch ( obj.objectType ) {
- case 'Point':
- case 'PointSet':
- geometry = loader.parse( obj.geometry );
- if ( geometry.attributes.hasOwnProperty( 'color' ) ) {
- material = new PointsMaterial( { vertexColors: true, sizeAttenuation: false, size: 2 } );
- } else {
- _color = attributes.drawColor;
- color = new Color( _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 );
- material = new PointsMaterial( { color: color, sizeAttenuation: false, size: 2 } );
- }
- material = this._compareMaterials( material );
- const points = new Points( geometry, material );
- points.userData[ 'attributes' ] = attributes;
- points.userData[ 'objectType' ] = obj.objectType;
- if ( attributes.name ) {
- points.name = attributes.name;
- }
- return points;
- case 'Mesh':
- case 'Extrusion':
- case 'SubD':
- case 'Brep':
- if ( obj.geometry === null ) return;
- geometry = loader.parse( obj.geometry );
- if ( geometry.attributes.hasOwnProperty( 'color' ) ) {
- mat.vertexColors = true;
- }
- if ( mat === null ) {
- mat = this._createMaterial();
- mat = this._compareMaterials( mat );
- }
- const mesh = new Mesh( geometry, mat );
- mesh.castShadow = attributes.castsShadows;
- mesh.receiveShadow = attributes.receivesShadows;
- mesh.userData[ 'attributes' ] = attributes;
- mesh.userData[ 'objectType' ] = obj.objectType;
- if ( attributes.name ) {
- mesh.name = attributes.name;
- }
- return mesh;
- case 'Curve':
- geometry = loader.parse( obj.geometry );
- _color = attributes.drawColor;
- color = new Color( _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 );
- material = new LineBasicMaterial( { color: color } );
- material = this._compareMaterials( material );
- const lines = new Line( geometry, material );
- lines.userData[ 'attributes' ] = attributes;
- lines.userData[ 'objectType' ] = obj.objectType;
- if ( attributes.name ) {
- lines.name = attributes.name;
- }
- return lines;
- case 'TextDot':
- geometry = obj.geometry;
- const ctx = document.createElement( 'canvas' ).getContext( '2d' );
- const font = `${geometry.fontHeight}px ${geometry.fontFace}`;
- ctx.font = font;
- const width = ctx.measureText( geometry.text ).width + 10;
- const height = geometry.fontHeight + 10;
- const r = window.devicePixelRatio;
- ctx.canvas.width = width * r;
- ctx.canvas.height = height * r;
- ctx.canvas.style.width = width + 'px';
- ctx.canvas.style.height = height + 'px';
- ctx.setTransform( r, 0, 0, r, 0, 0 );
- ctx.font = font;
- ctx.textBaseline = 'middle';
- ctx.textAlign = 'center';
- color = attributes.drawColor;
- ctx.fillStyle = `rgba(${color.r},${color.g},${color.b},${color.a})`;
- ctx.fillRect( 0, 0, width, height );
- ctx.fillStyle = 'white';
- ctx.fillText( geometry.text, width / 2, height / 2 );
- const texture = new CanvasTexture( ctx.canvas );
- texture.minFilter = LinearFilter;
- texture.wrapS = ClampToEdgeWrapping;
- texture.wrapT = ClampToEdgeWrapping;
- material = new SpriteMaterial( { map: texture, depthTest: false } );
- const sprite = new Sprite( material );
- sprite.position.set( geometry.point[ 0 ], geometry.point[ 1 ], geometry.point[ 2 ] );
- sprite.scale.set( width / 10, height / 10, 1.0 );
- sprite.userData[ 'attributes' ] = attributes;
- sprite.userData[ 'objectType' ] = obj.objectType;
- if ( attributes.name ) {
- sprite.name = attributes.name;
- }
- return sprite;
- case 'Light':
- geometry = obj.geometry;
- let light;
- switch ( geometry.lightStyle.name ) {
- case 'LightStyle_WorldPoint':
- light = new PointLight();
- light.castShadow = attributes.castsShadows;
- light.position.set( geometry.location[ 0 ], geometry.location[ 1 ], geometry.location[ 2 ] );
- light.shadow.normalBias = 0.1;
- break;
- case 'LightStyle_WorldSpot':
- light = new SpotLight();
- light.castShadow = attributes.castsShadows;
- light.position.set( geometry.location[ 0 ], geometry.location[ 1 ], geometry.location[ 2 ] );
- light.target.position.set( geometry.direction[ 0 ], geometry.direction[ 1 ], geometry.direction[ 2 ] );
- light.angle = geometry.spotAngleRadians;
- light.shadow.normalBias = 0.1;
- break;
- case 'LightStyle_WorldRectangular':
- light = new RectAreaLight();
- const width = Math.abs( geometry.width[ 2 ] );
- const height = Math.abs( geometry.length[ 0 ] );
- light.position.set( geometry.location[ 0 ] - ( height / 2 ), geometry.location[ 1 ], geometry.location[ 2 ] - ( width / 2 ) );
- light.height = height;
- light.width = width;
- light.lookAt( new Vector3( geometry.direction[ 0 ], geometry.direction[ 1 ], geometry.direction[ 2 ] ) );
- break;
- case 'LightStyle_WorldDirectional':
- light = new DirectionalLight();
- light.castShadow = attributes.castsShadows;
- light.position.set( geometry.location[ 0 ], geometry.location[ 1 ], geometry.location[ 2 ] );
- light.target.position.set( geometry.direction[ 0 ], geometry.direction[ 1 ], geometry.direction[ 2 ] );
- light.shadow.normalBias = 0.1;
- break;
- case 'LightStyle_WorldLinear':
- // not conversion exists, warning has already been printed to the console
- break;
- default:
- break;
- }
- if ( light ) {
- light.intensity = geometry.intensity;
- _color = geometry.diffuse;
- color = new Color( _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 );
- light.color = color;
- light.userData[ 'attributes' ] = attributes;
- light.userData[ 'objectType' ] = obj.objectType;
- }
- return light;
- }
- }
- _initLibrary() {
- if ( ! this.libraryPending ) {
- // Load rhino3dm wrapper.
- const jsLoader = new FileLoader( this.manager );
- jsLoader.setPath( this.libraryPath );
- const jsContent = new Promise( ( resolve, reject ) => {
- jsLoader.load( 'rhino3dm.js', resolve, undefined, reject );
- } );
- // Load rhino3dm WASM binary.
- const binaryLoader = new FileLoader( this.manager );
- binaryLoader.setPath( this.libraryPath );
- binaryLoader.setResponseType( 'arraybuffer' );
- const binaryContent = new Promise( ( resolve, reject ) => {
- binaryLoader.load( 'rhino3dm.wasm', resolve, undefined, reject );
- } );
- this.libraryPending = Promise.all( [ jsContent, binaryContent ] )
- .then( ( [ jsContent, binaryContent ] ) => {
- //this.libraryBinary = binaryContent;
- this.libraryConfig.wasmBinary = binaryContent;
- const fn = Rhino3dmWorker.toString();
- const body = [
- '/* rhino3dm.js */',
- jsContent,
- '/* worker */',
- fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) )
- ].join( '\n' );
- this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) );
- } );
- }
- return this.libraryPending;
- }
- _getWorker( taskCost ) {
- return this._initLibrary().then( () => {
- if ( this.workerPool.length < this.workerLimit ) {
- const worker = new Worker( this.workerSourceURL );
- worker._callbacks = {};
- worker._taskCosts = {};
- worker._taskLoad = 0;
- worker.postMessage( {
- type: 'init',
- libraryConfig: this.libraryConfig
- } );
- worker.onmessage = e => {
- const message = e.data;
- switch ( message.type ) {
- case 'warning':
- this.warnings.push( message.data );
- console.warn( message.data );
- break;
- case 'decode':
- worker._callbacks[ message.id ].resolve( message );
- break;
- case 'error':
- worker._callbacks[ message.id ].reject( message );
- break;
- default:
- console.error( 'THREE.Rhino3dmLoader: Unexpected message, "' + message.type + '"' );
- }
- };
- this.workerPool.push( worker );
- } else {
- this.workerPool.sort( function ( a, b ) {
- return a._taskLoad > b._taskLoad ? - 1 : 1;
- } );
- }
- const worker = this.workerPool[ this.workerPool.length - 1 ];
- worker._taskLoad += taskCost;
- return worker;
- } );
- }
- _releaseTask( worker, taskID ) {
- worker._taskLoad -= worker._taskCosts[ taskID ];
- delete worker._callbacks[ taskID ];
- delete worker._taskCosts[ taskID ];
- }
- dispose() {
- for ( let i = 0; i < this.workerPool.length; ++ i ) {
- this.workerPool[ i ].terminate();
- }
- this.workerPool.length = 0;
- return this;
- }
- }
- /* WEB WORKER */
- function Rhino3dmWorker() {
- let libraryPending;
- let libraryConfig;
- let rhino;
- let taskID;
- onmessage = function ( e ) {
- const message = e.data;
- switch ( message.type ) {
- case 'init':
- // console.log(message)
- libraryConfig = message.libraryConfig;
- const wasmBinary = libraryConfig.wasmBinary;
- let RhinoModule;
- libraryPending = new Promise( function ( resolve ) {
- /* Like Basis Loader */
- RhinoModule = { wasmBinary, onRuntimeInitialized: resolve };
- rhino3dm( RhinoModule ); // eslint-disable-line no-undef
- } ).then( () => {
- rhino = RhinoModule;
- } );
- break;
- case 'decode':
- taskID = message.id;
- const buffer = message.buffer;
- libraryPending.then( () => {
- try {
- const data = decodeObjects( rhino, buffer );
- self.postMessage( { type: 'decode', id: message.id, data } );
- } catch ( error ) {
- self.postMessage( { type: 'error', id: message.id, error } );
- }
- } );
- break;
- }
- };
- function decodeObjects( rhino, buffer ) {
- const arr = new Uint8Array( buffer );
- const doc = rhino.File3dm.fromByteArray( arr );
- const objects = [];
- const materials = [];
- const layers = [];
- const views = [];
- const namedViews = [];
- const groups = [];
- const strings = [];
- //Handle objects
- const objs = doc.objects();
- const cnt = objs.count;
- for ( let i = 0; i < cnt; i ++ ) {
- const _object = objs.get( i );
- const object = extractObjectData( _object, doc );
- _object.delete();
- if ( object ) {
- objects.push( object );
- }
- }
- // Handle instance definitions
- // console.log( `Instance Definitions Count: ${doc.instanceDefinitions().count()}` );
- for ( let i = 0; i < doc.instanceDefinitions().count(); i ++ ) {
- const idef = doc.instanceDefinitions().get( i );
- const idefAttributes = extractProperties( idef );
- idefAttributes.objectIds = idef.getObjectIds();
- objects.push( { geometry: null, attributes: idefAttributes, objectType: 'InstanceDefinition' } );
- }
- // Handle materials
- const textureTypes = [
- // rhino.TextureType.Bitmap,
- rhino.TextureType.Diffuse,
- rhino.TextureType.Bump,
- rhino.TextureType.Transparency,
- rhino.TextureType.Opacity,
- rhino.TextureType.Emap
- ];
- const pbrTextureTypes = [
- rhino.TextureType.PBR_BaseColor,
- rhino.TextureType.PBR_Subsurface,
- rhino.TextureType.PBR_SubsurfaceScattering,
- rhino.TextureType.PBR_SubsurfaceScatteringRadius,
- rhino.TextureType.PBR_Metallic,
- rhino.TextureType.PBR_Specular,
- rhino.TextureType.PBR_SpecularTint,
- rhino.TextureType.PBR_Roughness,
- rhino.TextureType.PBR_Anisotropic,
- rhino.TextureType.PBR_Anisotropic_Rotation,
- rhino.TextureType.PBR_Sheen,
- rhino.TextureType.PBR_SheenTint,
- rhino.TextureType.PBR_Clearcoat,
- rhino.TextureType.PBR_ClearcoatBump,
- rhino.TextureType.PBR_ClearcoatRoughness,
- rhino.TextureType.PBR_OpacityIor,
- rhino.TextureType.PBR_OpacityRoughness,
- rhino.TextureType.PBR_Emission,
- rhino.TextureType.PBR_AmbientOcclusion,
- rhino.TextureType.PBR_Displacement
- ];
- for ( let i = 0; i < doc.materials().count(); i ++ ) {
- const _material = doc.materials().get( i );
- const _pbrMaterial = _material.physicallyBased();
- let material = extractProperties( _material );
- const textures = [];
- for ( let j = 0; j < textureTypes.length; j ++ ) {
- const _texture = _material.getTexture( textureTypes[ j ] );
- if ( _texture ) {
- let textureType = textureTypes[ j ].constructor.name;
- textureType = textureType.substring( 12, textureType.length );
- const texture = { type: textureType };
- const image = doc.getEmbeddedFileAsBase64( _texture.fileName );
- texture.wrapU = _texture.wrapU;
- texture.wrapV = _texture.wrapV;
- texture.wrapW = _texture.wrapW;
- const uvw = _texture.uvwTransform.toFloatArray( true );
- texture.repeat = [ uvw[ 0 ], uvw[ 5 ] ];
- if ( image ) {
- texture.image = 'data:image/png;base64,' + image;
- } else {
- self.postMessage( { type: 'warning', id: taskID, data: {
- message: `THREE.3DMLoader: Image for ${textureType} texture not embedded in file.`,
- type: 'missing resource'
- }
- } );
- texture.image = null;
- }
- textures.push( texture );
- _texture.delete();
- }
- }
- material.textures = textures;
- if ( _pbrMaterial.supported ) {
- for ( let j = 0; j < pbrTextureTypes.length; j ++ ) {
- const _texture = _material.getTexture( pbrTextureTypes[ j ] );
- if ( _texture ) {
- const image = doc.getEmbeddedFileAsBase64( _texture.fileName );
- let textureType = pbrTextureTypes[ j ].constructor.name;
- textureType = textureType.substring( 12, textureType.length );
- const texture = { type: textureType, image: 'data:image/png;base64,' + image };
- textures.push( texture );
- _texture.delete();
- }
- }
- const pbMaterialProperties = extractProperties( _material.physicallyBased() );
- material = Object.assign( pbMaterialProperties, material );
- }
- materials.push( material );
- _material.delete();
- _pbrMaterial.delete();
- }
- // Handle layers
- for ( let i = 0; i < doc.layers().count(); i ++ ) {
- const _layer = doc.layers().get( i );
- const layer = extractProperties( _layer );
- layers.push( layer );
- _layer.delete();
- }
- // Handle views
- for ( let i = 0; i < doc.views().count(); i ++ ) {
- const _view = doc.views().get( i );
- const view = extractProperties( _view );
- views.push( view );
- _view.delete();
- }
- // Handle named views
- for ( let i = 0; i < doc.namedViews().count(); i ++ ) {
- const _namedView = doc.namedViews().get( i );
- const namedView = extractProperties( _namedView );
- namedViews.push( namedView );
- _namedView.delete();
- }
- // Handle groups
- for ( let i = 0; i < doc.groups().count(); i ++ ) {
- const _group = doc.groups().get( i );
- const group = extractProperties( _group );
- groups.push( group );
- _group.delete();
- }
- // Handle settings
- const settings = extractProperties( doc.settings() );
- //TODO: Handle other document stuff like dimstyles, instance definitions, bitmaps etc.
- // Handle dimstyles
- // console.log( `Dimstyle Count: ${doc.dimstyles().count()}` );
- // Handle bitmaps
- // console.log( `Bitmap Count: ${doc.bitmaps().count()}` );
- // Handle strings
- // console.log( `Document Strings Count: ${doc.strings().count()}` );
- // Note: doc.strings().documentUserTextCount() counts any doc.strings defined in a section
- //console.log( `Document User Text Count: ${doc.strings().documentUserTextCount()}` );
- const strings_count = doc.strings().count();
- for ( let i = 0; i < strings_count; i ++ ) {
- strings.push( doc.strings().get( i ) );
- }
- doc.delete();
- return { objects, materials, layers, views, namedViews, groups, strings, settings };
- }
- function extractObjectData( object, doc ) {
- const _geometry = object.geometry();
- const _attributes = object.attributes();
- let objectType = _geometry.objectType;
- let geometry, attributes, position, data, mesh;
- // skip instance definition objects
- //if( _attributes.isInstanceDefinitionObject ) { continue; }
- // TODO: handle other geometry types
- switch ( objectType ) {
- case rhino.ObjectType.Curve:
- const pts = curveToPoints( _geometry, 100 );
- position = {};
- attributes = {};
- data = {};
- position.itemSize = 3;
- position.type = 'Float32Array';
- position.array = [];
- for ( let j = 0; j < pts.length; j ++ ) {
- position.array.push( pts[ j ][ 0 ] );
- position.array.push( pts[ j ][ 1 ] );
- position.array.push( pts[ j ][ 2 ] );
- }
- attributes.position = position;
- data.attributes = attributes;
- geometry = { data };
- break;
- case rhino.ObjectType.Point:
- const pt = _geometry.location;
- position = {};
- const color = {};
- attributes = {};
- data = {};
- position.itemSize = 3;
- position.type = 'Float32Array';
- position.array = [ pt[ 0 ], pt[ 1 ], pt[ 2 ] ];
- const _color = _attributes.drawColor( doc );
- color.itemSize = 3;
- color.type = 'Float32Array';
- color.array = [ _color.r / 255.0, _color.g / 255.0, _color.b / 255.0 ];
- attributes.position = position;
- attributes.color = color;
- data.attributes = attributes;
- geometry = { data };
- break;
- case rhino.ObjectType.PointSet:
- case rhino.ObjectType.Mesh:
- geometry = _geometry.toThreejsJSON();
- break;
- case rhino.ObjectType.Brep:
- const faces = _geometry.faces();
- mesh = new rhino.Mesh();
- for ( let faceIndex = 0; faceIndex < faces.count; faceIndex ++ ) {
- const face = faces.get( faceIndex );
- const _mesh = face.getMesh( rhino.MeshType.Any );
- if ( _mesh ) {
- mesh.append( _mesh );
- _mesh.delete();
- }
- face.delete();
- }
- if ( mesh.faces().count > 0 ) {
- mesh.compact();
- geometry = mesh.toThreejsJSON();
- faces.delete();
- }
- mesh.delete();
- break;
- case rhino.ObjectType.Extrusion:
- mesh = _geometry.getMesh( rhino.MeshType.Any );
- if ( mesh ) {
- geometry = mesh.toThreejsJSON();
- mesh.delete();
- }
- break;
- case rhino.ObjectType.TextDot:
- geometry = extractProperties( _geometry );
- break;
- case rhino.ObjectType.Light:
- geometry = extractProperties( _geometry );
- if ( geometry.lightStyle.name === 'LightStyle_WorldLinear' ) {
- self.postMessage( { type: 'warning', id: taskID, data: {
- message: `THREE.3DMLoader: No conversion exists for ${objectType.constructor.name} ${geometry.lightStyle.name}`,
- type: 'no conversion',
- guid: _attributes.id
- }
- } );
- }
- break;
- case rhino.ObjectType.InstanceReference:
- geometry = extractProperties( _geometry );
- geometry.xform = extractProperties( _geometry.xform );
- geometry.xform.array = _geometry.xform.toFloatArray( true );
- break;
- case rhino.ObjectType.SubD:
- // TODO: precalculate resulting vertices and faces and warn on excessive results
- _geometry.subdivide( 3 );
- mesh = rhino.Mesh.createFromSubDControlNet( _geometry );
- if ( mesh ) {
- geometry = mesh.toThreejsJSON();
- mesh.delete();
- }
- break;
- /*
- case rhino.ObjectType.Annotation:
- case rhino.ObjectType.Hatch:
- case rhino.ObjectType.ClipPlane:
- */
- default:
- self.postMessage( { type: 'warning', id: taskID, data: {
- message: `THREE.3DMLoader: Conversion not implemented for ${objectType.constructor.name}`,
- type: 'not implemented',
- guid: _attributes.id
- }
- } );
- break;
- }
- if ( geometry ) {
- attributes = extractProperties( _attributes );
- attributes.geometry = extractProperties( _geometry );
- if ( _attributes.groupCount > 0 ) {
- attributes.groupIds = _attributes.getGroupList();
- }
- if ( _attributes.userStringCount > 0 ) {
- attributes.userStrings = _attributes.getUserStrings();
- }
- if ( _geometry.userStringCount > 0 ) {
- attributes.geometry.userStrings = _geometry.getUserStrings();
- }
- attributes.drawColor = _attributes.drawColor( doc );
- objectType = objectType.constructor.name;
- objectType = objectType.substring( 11, objectType.length );
- return { geometry, attributes, objectType };
- } else {
- self.postMessage( { type: 'warning', id: taskID, data: {
- message: `THREE.3DMLoader: ${objectType.constructor.name} has no associated mesh geometry.`,
- type: 'missing mesh',
- guid: _attributes.id
- }
- } );
- }
- }
- function extractProperties( object ) {
- const result = {};
- for ( const property in object ) {
- const value = object[ property ];
- if ( typeof value !== 'function' ) {
- if ( typeof value === 'object' && value !== null && value.hasOwnProperty( 'constructor' ) ) {
- result[ property ] = { name: value.constructor.name, value: value.value };
- } else {
- result[ property ] = value;
- }
- } else {
- // these are functions that could be called to extract more data.
- //console.log( `${property}: ${object[ property ].constructor.name}` );
- }
- }
- return result;
- }
- function curveToPoints( curve, pointLimit ) {
- let pointCount = pointLimit;
- let rc = [];
- const ts = [];
- if ( curve instanceof rhino.LineCurve ) {
- return [ curve.pointAtStart, curve.pointAtEnd ];
- }
- if ( curve instanceof rhino.PolylineCurve ) {
- pointCount = curve.pointCount;
- for ( let i = 0; i < pointCount; i ++ ) {
- rc.push( curve.point( i ) );
- }
- return rc;
- }
- if ( curve instanceof rhino.PolyCurve ) {
- const segmentCount = curve.segmentCount;
- for ( let i = 0; i < segmentCount; i ++ ) {
- const segment = curve.segmentCurve( i );
- const segmentArray = curveToPoints( segment, pointCount );
- rc = rc.concat( segmentArray );
- segment.delete();
- }
- return rc;
- }
- if ( curve instanceof rhino.ArcCurve ) {
- pointCount = Math.floor( curve.angleDegrees / 5 );
- pointCount = pointCount < 2 ? 2 : pointCount;
- // alternative to this hardcoded version: https://stackoverflow.com/a/18499923/2179399
- }
- if ( curve instanceof rhino.NurbsCurve && curve.degree === 1 ) {
- const pLine = curve.tryGetPolyline();
- for ( let i = 0; i < pLine.count; i ++ ) {
- rc.push( pLine.get( i ) );
- }
- pLine.delete();
- return rc;
- }
- const domain = curve.domain;
- const divisions = pointCount - 1.0;
- for ( let j = 0; j < pointCount; j ++ ) {
- const t = domain[ 0 ] + ( j / divisions ) * ( domain[ 1 ] - domain[ 0 ] );
- if ( t === domain[ 0 ] || t === domain[ 1 ] ) {
- ts.push( t );
- continue;
- }
- const tan = curve.tangentAt( t );
- const prevTan = curve.tangentAt( ts.slice( - 1 )[ 0 ] );
- // Duplicated from THREE.Vector3
- // How to pass imports to worker?
- const tS = tan[ 0 ] * tan[ 0 ] + tan[ 1 ] * tan[ 1 ] + tan[ 2 ] * tan[ 2 ];
- const ptS = prevTan[ 0 ] * prevTan[ 0 ] + prevTan[ 1 ] * prevTan[ 1 ] + prevTan[ 2 ] * prevTan[ 2 ];
- const denominator = Math.sqrt( tS * ptS );
- let angle;
- if ( denominator === 0 ) {
- angle = Math.PI / 2;
- } else {
- const theta = ( tan.x * prevTan.x + tan.y * prevTan.y + tan.z * prevTan.z ) / denominator;
- angle = Math.acos( Math.max( - 1, Math.min( 1, theta ) ) );
- }
- if ( angle < 0.1 ) continue;
- ts.push( t );
- }
- rc = ts.map( t => curve.pointAt( t ) );
- return rc;
- }
- }
- export { Rhino3dmLoader };
|