// MAP CALLBACKS refresh() { const frustum = new THREE.Frustum(); frustum.setFromMatrix( new THREE.Matrix4().multiplyMatrices( this.camera.projectionMatrix, this.camera.matrixWorldInverse ) ); this.tileset.root.checkLoad(frustum, this._getCameraPosition()); }
// HELPERS _getCameraPosition() { if (!this.viewProjectionMatrix) { return new THREE.Vector3(); } const cam = new THREE.Camera(); const rootInverse = new THREE.Matrix4().getInverse(this.rootTransform); cam.projectionMatrix.elements = this.viewProjectionMatrix; cam.projectionMatrixInverse = new THREE.Matrix4().getInverse(cam.projectionMatrix); // add since three@0.103.0 const campos = new THREE.Vector3(0, 0, 0).unproject(cam).applyMatrix4(rootInverse); return campos; }
function createTHREEOutlineFromOBB(box) { const geom = new THREE.BoxGeometry(box[3] * 2, box[7] * 2, box[11] * 2); const edges = new THREE.EdgesGeometry(geom); const line = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({color: 0x800000})); const trans = new THREE.Matrix4().makeTranslation(box[0], box[1], box[2]); line.applyMatrix(trans); return line; }
export function transform2mapbox(matrix) { const min = -WEBMERCATOR_EXTENT; const max = WEBMERCATOR_EXTENT; const scale = 1 / (2 * WEBMERCATOR_EXTENT); const result = matrix.slice(); // copy array result[12] = (matrix[12] - min) * scale; // x translation result[13] = (matrix[13] - max) * -scale; // y translation result[14] = matrix[14] * scale; // z translation return new THREE.Matrix4().fromArray(result).scale(new THREE.Vector3(scale, -scale, scale)); }
_createPointNodes(d, tileContent) { const geometry = new THREE.BufferGeometry(); geometry.addAttribute('position', new THREE.Float32BufferAttribute(d.points, 3)); const material = new THREE.PointsMaterial(); material.size = this.styleParams.pointsize !== null ? this.styleParams.pointsize : 1.0; if (this.styleParams.color) { material.vertexColors = THREE.NoColors; material.color = new THREE.Color(this.styleParams.color); material.opacity = this.styleParams.opacity !== null ? this.styleParams.opacity : 1.0; } else if (d.rgba) { geometry.addAttribute('color', new THREE.Float32BufferAttribute(d.rgba, 4)); material.vertexColors = THREE.VertexColors; } else if (d.rgb) { geometry.addAttribute('color', new THREE.Float32BufferAttribute(d.rgb, 3)); material.vertexColors = THREE.VertexColors; } tileContent.add(new THREE.Points(geometry, material)); if (d.rtc_center) { const c = d.rtc_center; tileContent.applyMatrix(new THREE.Matrix4().makeTranslation(c[0], c[1], c[2])); } tileContent.add(new THREE.Points(geometry, material)); return tileContent; }
render(gl, viewProjectionMatrix) { this.viewProjectionMatrix = viewProjectionMatrix; const l = new THREE.Matrix4().fromArray(viewProjectionMatrix); this.renderer.state.reset(); // The root tile transform is applied to the camera while rendering // instead of to the root tile. This avoids precision errors. this.camera.projectionMatrix = l.multiply(this.rootTransform); this.renderer.render(this.scene, this.camera); if (this.loadStatus === 1) { // first render after root tile is loaded this.loadStatus = 2; const frustum = new THREE.Frustum(); frustum.setFromMatrix( new THREE.Matrix4().multiplyMatrices( this.camera.projectionMatrix, this.camera.matrixWorldInverse ) ); if (this.tileset) { this.tileset.update(frustum, this._getCameraPosition()); } } }
_initTraversal(json, parentRefine, isRoot) { this.refine = json.refine ? json.refine.toUpperCase() : parentRefine; this.geometricError = json.geometricError; this.transform = json.transform; if (this.transform && !isRoot) { // if not the root tile: apply the transform to the THREE js Group // the root tile transform is applied to the camera while rendering this.totalContent.applyMatrix(new THREE.Matrix4().fromArray(this.transform)); } this.content = json.content; this.children = []; if (json.children) { for (let i = 0; i < json.children.length; i++) { const child = new TileHeader( json.children[i], this.resourcePath, this.styleParams, this.refine, false ); this.childContent.add(child.totalContent); this.children.push(child); } } }
_createGLTFNodes(d, tileContent) { const loader = new GLTFLoader(); const rotateX = new THREE.Matrix4().makeRotationAxis(new THREE.Vector3(1, 0, 0), Math.PI / 2);
new THREE.Matrix4().fromArray(tileset.root.transform) );