|
| 1 | +//Example: https://github.com/tomvandig/web-ifc-three/tree/main/examples/jsm |
| 2 | + |
| 3 | +import { IfcAPI } from './ifc/web-ifc-api.js'; |
| 4 | +import { |
| 5 | + FileLoader, |
| 6 | + Loader, |
| 7 | + Object3D, |
| 8 | + Mesh, |
| 9 | + Color, |
| 10 | + MeshPhongMaterial, |
| 11 | + DoubleSide, |
| 12 | + Matrix4, |
| 13 | + BufferGeometry, |
| 14 | + InterleavedBuffer, |
| 15 | + InterleavedBufferAttribute, |
| 16 | + BufferAttribute, |
| 17 | +} from '../../../../build/three.module.js'; |
| 18 | + |
| 19 | +var ifcAPI = new IfcAPI(); |
| 20 | +ifcAPI.Init(); |
| 21 | + |
| 22 | +function IFCLoader( manager ) { |
| 23 | + |
| 24 | + Loader.call( this, manager ); |
| 25 | + |
| 26 | +} |
| 27 | + |
| 28 | +IFCLoader.prototype = Object.assign( Object.create( Loader.prototype ), { |
| 29 | + |
| 30 | + constructor: IFCLoader, |
| 31 | + |
| 32 | + load: function ( url, onLoad, onProgress, onError ) { |
| 33 | + |
| 34 | + var scope = this; |
| 35 | + |
| 36 | + var loader = new FileLoader( scope.manager ); |
| 37 | + loader.setPath( scope.path ); |
| 38 | + loader.setResponseType( 'arraybuffer' ); |
| 39 | + loader.setRequestHeader( scope.requestHeader ); |
| 40 | + loader.setWithCredentials( scope.withCredentials ); |
| 41 | + loader.load( |
| 42 | + url, |
| 43 | + function ( buffer ) { |
| 44 | + |
| 45 | + try { |
| 46 | + |
| 47 | + onLoad( scope.parse( buffer ) ); |
| 48 | + |
| 49 | + } catch ( e ) { |
| 50 | + |
| 51 | + if ( onError ) { |
| 52 | + |
| 53 | + onError( e ); |
| 54 | + |
| 55 | + } else { |
| 56 | + |
| 57 | + console.error( e ); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + scope.manager.itemError( url ); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + }, |
| 66 | + onProgress, |
| 67 | + onError |
| 68 | + ); |
| 69 | + |
| 70 | + }, |
| 71 | + |
| 72 | + parse: ( function () { |
| 73 | + |
| 74 | + return function ( buffer ) { |
| 75 | + |
| 76 | + var data = new Uint8Array( buffer ); |
| 77 | + var modelID = ifcAPI.OpenModel( 'example.ifc', data ); |
| 78 | + return loadAllGeometry( modelID ); |
| 79 | + |
| 80 | + function loadAllGeometry( modelID ) { |
| 81 | + |
| 82 | + var flatMeshes = getFlatMeshes( modelID ); |
| 83 | + var mainObject = new Object3D(); |
| 84 | + for ( var i = 0; i < flatMeshes.size(); i ++ ) { |
| 85 | + |
| 86 | + var placedGeometries = flatMeshes.get( i ).geometries; |
| 87 | + for ( var j = 0; j < placedGeometries.size(); j ++ ) |
| 88 | + mainObject.add( getPlacedGeometry( modelID, placedGeometries.get( j ) ) ); |
| 89 | + |
| 90 | + } |
| 91 | + |
| 92 | + return mainObject; |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | + function getFlatMeshes( modelID ) { |
| 97 | + |
| 98 | + var flatMeshes = ifcAPI.LoadAllGeometry( modelID ); |
| 99 | + return flatMeshes; |
| 100 | + |
| 101 | + } |
| 102 | + |
| 103 | + function getPlacedGeometry( modelID, placedGeometry ) { |
| 104 | + |
| 105 | + var geometry = getBufferGeometry( modelID, placedGeometry ); |
| 106 | + var material = getMeshMaterial( placedGeometry.color ); |
| 107 | + var mesh = new Mesh( geometry, material ); |
| 108 | + mesh.matrix = getMeshMatrix( placedGeometry.flatTransformation ); |
| 109 | + mesh.matrixAutoUpdate = false; |
| 110 | + return mesh; |
| 111 | + |
| 112 | + } |
| 113 | + |
| 114 | + function getBufferGeometry( modelID, placedGeometry ) { |
| 115 | + |
| 116 | + var geometry = ifcAPI.GetGeometry( |
| 117 | + modelID, |
| 118 | + placedGeometry.geometryExpressID |
| 119 | + ); |
| 120 | + var verts = ifcAPI.GetVertexArray( |
| 121 | + geometry.GetVertexData(), |
| 122 | + geometry.GetVertexDataSize() |
| 123 | + ); |
| 124 | + var indices = ifcAPI.GetIndexArray( |
| 125 | + geometry.GetIndexData(), |
| 126 | + geometry.GetIndexDataSize() |
| 127 | + ); |
| 128 | + var bufferGeometry = ifcGeometryToBuffer( verts, indices ); |
| 129 | + return bufferGeometry; |
| 130 | + |
| 131 | + } |
| 132 | + |
| 133 | + function getMeshMaterial( color ) { |
| 134 | + |
| 135 | + var col = new Color( color.x, color.y, color.z ); |
| 136 | + var material = new MeshPhongMaterial( { color: col, side: DoubleSide } ); |
| 137 | + material.transparent = color.w !== 1; |
| 138 | + if ( material.transparent ) material.opacity = color.w; |
| 139 | + return material; |
| 140 | + |
| 141 | + } |
| 142 | + |
| 143 | + function getMeshMatrix( matrix ) { |
| 144 | + |
| 145 | + var mat = new Matrix4(); |
| 146 | + mat.fromArray( matrix ); |
| 147 | + // mat.elements[15 - 3] *= 0.001; |
| 148 | + // mat.elements[15 - 2] *= 0.001; |
| 149 | + // mat.elements[15 - 1] *= 0.001; |
| 150 | + return mat; |
| 151 | + |
| 152 | + } |
| 153 | + |
| 154 | + function ifcGeometryToBuffer( vertexData, indexData ) { |
| 155 | + |
| 156 | + var geometry = new BufferGeometry(); |
| 157 | + var buffer32 = new InterleavedBuffer( vertexData, 6 ); |
| 158 | + geometry.setAttribute( |
| 159 | + 'position', |
| 160 | + new InterleavedBufferAttribute( buffer32, 3, 0 ) |
| 161 | + ); |
| 162 | + geometry.setAttribute( |
| 163 | + 'normal', |
| 164 | + new InterleavedBufferAttribute( buffer32, 3, 3 ) |
| 165 | + ); |
| 166 | + geometry.setIndex( new BufferAttribute( indexData, 1 ) ); |
| 167 | + return geometry; |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | + }; |
| 172 | + |
| 173 | + } )(), |
| 174 | +} ); |
| 175 | + |
| 176 | +export { IFCLoader }; |
0 commit comments