123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742 |
- import * as THREE from "../libs/three.js/build/three.module.js";
-
- import {ExtendPointCloudMaterial} from "./materials/ExtendPointCloudMaterial.js";
-
- import {PointCloudOctree} from './PointCloudOctree.js'
- import {PointSizeType } from "./defines.js";
- import math from './custom/utils/math.js'
- export class ExtendPointCloudOctree extends PointCloudOctree{
- constructor(geometry, material){
- material = material || new ExtendPointCloudMaterial();
- super(geometry, material)
-
- //xzw move from material 。 adaptive_point_size才使用
- /* this.visibleNodesTexture = Utils.generateDataTexture(2048, 1, new THREE.Color(0xffffff));
- this.visibleNodesTexture.minFilter = THREE.NearestFilter;
- this.visibleNodesTexture.magFilter = THREE.NearestFilter; */
-
-
-
- this.boundingBox = this.pcoGeometry.tightBoundingBox//this.pcoGeometry.boundingBox; //boundingBox是正方体,所以换掉
- this.boundingSphere = this.boundingBox.getBoundingSphere(new THREE.Sphere());
- this.nodeMaxLevel = 0;//add
- this.maxLevel = Infinity;
- this.temp = { sizeFitToLevel:{}, opacity:{}}//add
- //add
-
- this.panos = []
- this.matrixAutoUpdate = false //最好禁止updateMatrix 直接使用matrixWorld
- this.orientationUser = 0
- this.translateUser = new THREE.Vector3;
-
- this.rotateMatrix = new THREE.Matrix4;
- this.transformMatrix = new THREE.Matrix4;// 数据集的变化矩阵
- this.transformInvMatrix = new THREE.Matrix4;
- /* this.transformMatrix2 = new THREE.Matrix4;// 数据集的变化矩阵
- this.transformInvMatrix2 = new THREE.Matrix4; */
- this.rotateInvMatrix = new THREE.Matrix4;
-
- this.material.spacing = this.pcoGeometry.spacing;//初始化一下 以便于设置pointsize
- this.nodeMaxLevelPredict = this.predictNodeMaxLevel()//预测maxNodeLevel
- this.testMaxNodeCount = this.testMaxNodeCount2 = 0
-
-
- this._visible = true;
- this.pcoGeometry.addEventListener('updateNodeMaxLevel', this.updateNodeMaxLevel.bind(this))
- this.isPointcloud = true //add
- }
-
- /*
-
- 注释:node的level从最大的box 0开始。
- 且加载任意一个node必定也会加载它的所有祖先。(如visibleNodes中有一个level为4,则一定有3,2,1,0)
- visibleNodes就是所有可见的node,比如:
- 如果相机在0这个位置朝下,这时候的visibleNodes中只有一个level为0的node;
- 而如果朝上看,上方的几个node如果在视野中占据足够大的位置的话,就会加载。
- 如果相机在2这个位置朝上,这时候的visibleNodes中所包含的level为: 0,1,2
-
- ________________
- | | | |
- |__2| | |
- | 1 | 1 |
- |_______|_______|
- | |
- | |
- | 0 |
- |_______________|
-
- 查看box可在potree中开启
- */
-
- updateNodeMaxLevel(e){//目前点云包含node的最高level
- var level = Math.max(e.level, this.nodeMaxLevel)
- if(level != this.nodeMaxLevel){
- this.nodeMaxLevel = level
- //viewer.dispatchEvent({type:'updateNodeMaxLevel', pointcloud: this, nodeMaxLevel:level})
-
- //console.log('updateNodeMaxLevel ' + this.dataset_id + " : "+ this.nodeMaxLevel)
-
- this.setPointLevel()//重新计算
-
- if(!Potree.settings.sizeFitToLevel){
- this.changePointSize()
- }
- }
- }//注:在没有加载到真实的 nodeMaxLevel之前,点云会显示得偏大
-
-
-
- //panoEdit时比预测值小很多?
-
- testMaxNodeLevel(camera){//手动使maxLevel达到最高,从而迫使updateNodeMaxLevel。 因为Potree.settings.pointDensity 不为 'high'时,maxLevel不是所加载的最高,就很容易加载不出下一个层级,导致无法知道nodeMaxLevel
- if(this.testMaxNodeLevelDone ) return
- //if(this.nodeMaxLevel > this.nodeMaxLevelPredict.min )return
-
-
- if( this.nodeMaxLevel==0 )return true
- if(camera.type == "OrthographicCamera" || this.testMaxNodeCount < 50){
- if(!Potree.Utils.isInsideFrustum(this.bound, camera)){
- return true
- }
- }else if( !viewer.atDatasets.includes(this))return true //否则老远就count++
-
-
-
-
- let levels = this.visibleNodes.map(e=>e.getLevel())
- let actMaxLevel = Math.max.apply(null, levels) //实际加载到的最高的node level
- if(actMaxLevel < this.maxLevel)return true// 还没加载到能加载到的最高。 但在细节设置较低时,排除作用微弱。
-
-
- //尝试加载出更高级的level
- let old = this.maxLevel
- this.maxLevel = 12;
- //var visibleNodes1 = this.visibleNodes.map(e=>e.getLevel())
- //console.log('visibleNodes1',visibleNodes1)
- Potree.updatePointClouds([this], viewer.scene.getActiveCamera(), viewer.mainViewport.resolution );
- //不在camera可视范围内还是加载不出来。即使临时修改位置
-
-
- var visibleNodes2 = this.visibleNodes.map(e=>e.getLevel())
- //console.log('visibleNodes2',visibleNodes2)
- this.maxLevel = old;
-
-
-
- this.testMaxNodeCount ++
- viewer.testMaxNodeCount ++
-
- //console.log('testMaxNodeCount', this.dataset_id, this.testMaxNodeCount, 'nodeMaxLevel', this.nodeMaxLevel )
-
-
- if( this.testMaxNodeCount == Potree.config.testNodeCount1 ){//差不多等当前所在数据集nodeMaxLevel加载出来
- this.changePointSize() //重新更新一下大小。因之前用的是nodeMaxLevelPredict (防止刚开始因nodeMaxLevel没涨完,导致过大的点云突然出现
- }
-
-
- if(this.testMaxNodeCount > 100){
- console.log('testMaxNodeLevel次数超出,强制结束:',this.dataset_id, this.nodeMaxLevel, this.nodeMaxLevelPredict.min)
- this.testMaxNodeLevelDone = 'moreThanMaxCount'
- return; //在可以看见点云的情况下,超时,有可能是预测的max是错的
- }
- if(this.nodeMaxLevel < this.nodeMaxLevelPredict.min) return true //仍需要继续testMaxNodeLevel
-
- this.testMaxNodeCount2 ++; // 已经> this.nodeMaxLevelPredict.min 后,开始计数。因为min可能低于真实nodeMaxLevel所以要再试几次
-
-
-
- if(this.testMaxNodeCount2 < 50) return true //再试几次 ( 主要是细节调得低时需要多测几次才加载到
- this.testMaxNodeLevelDone = true
-
-
-
-
-
- }
-
-
-
- updateMatrixWorld(force){//add
- super.updateMatrixWorld(force);
- this.matrixWorldInverse = this.matrixWorld.clone().invert();
- }
-
-
- setPointLevel(){
-
- var pointDensity = Potree.settings.pointDensity
- var config = Potree.config.pointDensity[pointDensity];
- if(!config)return
-
-
- /* if(this.testingMaxLevel){
- this.maxLevel = 12;//先加载到最大的直到测试完毕。由于5个level为一组来加载,所以如果写4最高能加载到5,如果写5最高能加载到下一个级别的最高也就是10
- //console.log('maxLevel: '+e.maxLevel + ' testingMaxLevel中 ' )
- }else{ */
- let percent = config.percentByUser && Potree.settings.UserDensityPercent != void 0 ? Potree.settings.UserDensityPercent : config.maxLevelPercent
- this.maxLevel = Math.round( percent * this.nodeMaxLevel);
- //console.log('maxLevel: '+e.maxLevel + ', density : '+Potree.settings.pointDensity, ", percent :"+percent);
-
- if(Potree.settings.sizeFitToLevel){
- this.changePointSize()
- }
- this.changePointOpacity()
- //}
- viewer.dealBeforeRender || viewer.dispatchEvent('content_changed')
- }
-
- //预测可能的nodeMaxLevel:
-
- predictNodeMaxLevel(){//预测maxNodeLevel。 可能只适用于我们相机拍的点云
- let spacing = {min:0.005, max:0.014};//最小节的两点间的距离 ,获得方法:spacing / Math.pow(2, nodeMaxLevel)。 目前观测的我们自己拍的这个数值的范围大概是这样
- let min = Math.log2(this.material.spacing / spacing.max); //有见过最大是0.01368
- let max = Math.log2(this.material.spacing / spacing.min); //大部分是 0.006
- //console.log('predictNodeMaxLevel:', this.name , min, max )
-
-
- return {min, max}
- }
-
- getHighestNodeSpacing(){
- return this.material.spacing / Math.pow(2, this.nodeMaxLevel) //前提是这个nodeMaxLevel是准确的
- }
-
- updateMaterial (material, visibleNodes, camera, renderer, resolution) {//改
- material.fov = camera.fov * (Math.PI / 180);
- /* material.screenWidth = renderer.domElement.clientWidth;
- material.screenHeight = renderer.domElement.clientHeight; */
- material.resolution = resolution
- material.spacing = this.pcoGeometry.spacing; // * Math.max(this.scale.x, this.scale.y, this.scale.z);
- material.near = camera.near;
- material.far = camera.far;
- material.uniforms.octreeSize.value = this.pcoGeometry.boundingBox.getSize(new THREE.Vector3()).x;
- }
-
- pick(viewer, viewport, camera, ray, params = {}){//改
-
- let renderer = viewer.renderer;
- let pRenderer = viewer.pRenderer;
- viewer.addTimeMark('pick','start')
- let getVal = (a, b) => a != void 0 ? a : b;
-
-
- let r0 = this.nodeMaxLevel > 0 ? this.maxLevel/this.nodeMaxLevel : 0.5
- let pickWindowSize_ = THREE.Math.clamp( Math.round((1.1-r0)*80), 5, 100)
- let pickWindowSize = getVal(params.pickWindowSize, pickWindowSize_ ); /* 65 */ //拾取像素边长,越小越精准,但点云稀疏的话可能容易出现识别不到的情况。 另外左下侧会有缝隙无法识别到,缝隙大小和这个值有关//突然发现pickWindowSize在一百以内的变化对pick费时影响甚微,1和100差1毫秒不到,但400时会多4毫秒
-
- if(camera.type == 'OrthographicCamera'){
- var cameraDir = new THREE.Vector3(0,0,-1).applyQuaternion(camera.quaternion)
- pickWindowSize *= 4 //pointsize比较大时截取太小会没多少点可以选
- }
-
-
- let pickOutsideClipRegion = getVal(params.pickOutsideClipRegion, false);
- let size = viewport ? viewport.resolution : renderer.getSize(new THREE.Vector2());
- let width = Math.ceil(getVal(params.width, size.width)); //renderTarget大小。影响识别精度
- let height = Math.ceil(getVal(params.height, size.height));
-
- let screenshot = ()=>{
- if(window.testScreen){
- let dataUrl = Potree.Utils.renderTargetToDataUrl(pickState.renderTarget, width, height, renderer)
-
- Common.downloadFile(dataUrl, 'screenshot.jpg') //为什么图片上不是只有pickWindowSize区域有颜色??
- window.testScreen = 0
- }
- }
-
-
- let pointSizeType = getVal(params.pointSizeType, this.material.pointSizeType);
- let pointSize = getVal(params.pointSize, this.material.size);
-
- let nodes = this.nodesOnRay(this.visibleNodes, ray);
-
- if (nodes.length === 0) {
-
- return null;
- }
- //console.log('nodes.length != 0', this.name)
- if (!this.pickState) {
- let scene = new THREE.Scene();
- let material = new ExtendPointCloudMaterial();
- material.activeAttributeName = "indices";
- let renderTarget = new THREE.WebGLRenderTarget(
- 1, 1,
- { minFilter: THREE.LinearFilter,
- magFilter: THREE.NearestFilter,
- format: THREE.RGBAFormat }
- );
- this.pickState = {
- renderTarget: renderTarget,
- material: material,
- scene: scene
- };
- };
- let pickState = this.pickState;
- let pickMaterial = pickState.material;
- { // update pick material
- pickMaterial.pointSizeType = pointSizeType;
- //pickMaterial.shape = this.material.shape;
- pickMaterial.shape = Potree.PointShape.PARABOLOID;
- pickMaterial.uniforms.uFilterReturnNumberRange.value = this.material.uniforms.uFilterReturnNumberRange.value;
- pickMaterial.uniforms.uFilterNumberOfReturnsRange.value = this.material.uniforms.uFilterNumberOfReturnsRange.value;
- pickMaterial.uniforms.uFilterGPSTimeClipRange.value = this.material.uniforms.uFilterGPSTimeClipRange.value;
- pickMaterial.uniforms.uFilterPointSourceIDClipRange.value = this.material.uniforms.uFilterPointSourceIDClipRange.value;
- pickMaterial.activeAttributeName = "indices";
- pickMaterial.size = pointSize;
- pickMaterial.uniforms.minSize.value = this.material.uniforms.minSize.value;
- pickMaterial.uniforms.maxSize.value = this.material.uniforms.maxSize.value;
- pickMaterial.classification = this.material.classification;
- pickMaterial.recomputeClassification();
-
- //pickClipped判断转移到上一层函数
- let {bigClipInBox,clipBoxes_in,clipBoxes_out} = this.material
- pickMaterial.setClipBoxes(bigClipInBox, clipBoxes_in, clipBoxes_out, [])
-
- this.updateMaterial(pickMaterial, nodes, camera, renderer, new THREE.Vector2(width, height));
- }
- pickState.renderTarget.setSize(width, height); //仅绘制屏幕大小的,而不乘以devicePixelRatio
- let pixelPos = new THREE.Vector2(params.x, params.y);
- let gl = renderer.getContext();
- gl.enable(gl.SCISSOR_TEST);
- gl.scissor( //规定渲染范围,只渲染一小块
- parseInt(pixelPos.x - (pickWindowSize - 1) / 2),
- parseInt(pixelPos.y - (pickWindowSize - 1) / 2),
- parseInt(pickWindowSize), parseInt(pickWindowSize));
- renderer.state.buffers.depth.setTest(pickMaterial.depthTest);
- renderer.state.buffers.depth.setMask(pickMaterial.depthWrite);
- renderer.state.setBlending(THREE.NoBlending);
-
- { // RENDER
- renderer.setRenderTarget(pickState.renderTarget);
- gl.clearColor(0, 0, 0, 0);
- renderer.clear(true, true, true);
- let tmp = this.material;
- this.material = pickMaterial;
-
- pRenderer.renderOctree(this, nodes, camera, pickState.renderTarget);
- screenshot();
- this.material = tmp;
- }
- let clamp = (number, min, max) => Math.min(Math.max(min, number), max);
- let x = parseInt(clamp(pixelPos.x - (pickWindowSize - 1) / 2, 0, width));
- let y = parseInt(clamp(pixelPos.y - (pickWindowSize - 1) / 2, 0, height));
- /* let w = parseInt(Math.min(x + pickWindowSize, width) - x);
- let h = parseInt(Math.min(y + pickWindowSize, height) - y); */
-
- let pixelCount = pickWindowSize * pickWindowSize//w * h;
- let buffer = new Uint8Array(4 * pixelCount);
- //w<pickWindowSize会报错
-
- gl.readPixels(x, y, pickWindowSize, pickWindowSize, gl.RGBA, gl.UNSIGNED_BYTE, buffer); //这句花费最多时间 pc:2-4, 即使只有1*1像素
-
- renderer.setRenderTarget(null);
- renderer.state.reset();
- renderer.setScissorTest(false);
- gl.disable(gl.SCISSOR_TEST);
- let pixels = buffer;
- let ibuffer = new Uint32Array(buffer.buffer); //四个数整合成一个
- // find closest hit inside pixelWindow boundaries
- let min = Number.MAX_VALUE;
- let hits = [], hits2 = [], rSquare;
-
- if(!params.all){
- let r = THREE.Math.clamp(Math.floor(pickWindowSize / 2),1, 40);
- rSquare = r * r
- }
-
- for (let u = 0; u < pickWindowSize; u++) {
- for (let v = 0; v < pickWindowSize; v++) {
- let offset = (u + v * pickWindowSize);
- let distance = Math.pow(u - (pickWindowSize - 1) / 2, 2) + Math.pow(v - (pickWindowSize - 1) / 2, 2);
- let pcIndex = pixels[4 * offset + 3];//nodes index(第四位)
- pixels[4 * offset + 3] = 0; //去除nodes index后剩下的是index(前三位)
- let pIndex = ibuffer[offset]; //index
- if(!(pcIndex === 0 && pIndex === 0) && (pcIndex !== undefined) && (pIndex !== undefined)){
- let hit = {
- pIndex: pIndex,
- pcIndex: pcIndex,
- distanceToCenter: distance
- };
- if(params.all){
- hits.push(hit);
- }else{
- if(hits.length > 0){//最后选取的是最靠近鼠标的那个pick
- if(distance < hits[0].distanceToCenter){
- hits[0] = hit;
- }
- }else{
- hits.push(hit);
- }
-
- if(distance < rSquare) hits2.push(hit); //add
-
- }
- }
- }
- }
-
- if(!params.all){
- if(hits2.length){//add
- hits = hits2
- }
- }
-
- // { // DEBUG: show panel with pick image
- // let img = Utils.pixelsArrayToImage(buffer, w, h);
- // let screenshot = img.src;
-
- // if(!this.debugDIV){
- // this.debugDIV = $(`
- // <div id="pickDebug"
- // style="position: absolute;
- // right: 400px; width: 300px;
- // bottom: 44px; width: 300px;
- // z-index: 1000;
- // "></div>`);
- // $(document.body).append(this.debugDIV);
- // }
-
- // this.debugDIV.empty();
- // this.debugDIV.append($(`<img src="${screenshot}"
- // style="transform: scaleY(-1); width: 300px"/>`));
- // //$(this.debugWindow.document).append($(`<img src="${screenshot}"/>`));
- // //this.debugWindow.document.write('<img src="'+screenshot+'"/>');
- // }
- for(let hit of hits){
- let point = {};
- if (!nodes[hit.pcIndex]) {
- return null;
- }
- let node = nodes[hit.pcIndex];
- let pc = node.sceneNode;
- let geometry = node.geometryNode.geometry;
- for(let attributeName in geometry.attributes){
- let attribute = geometry.attributes[attributeName];
- if (attributeName === 'position') {
- let x = attribute.array[3 * hit.pIndex + 0];
- let y = attribute.array[3 * hit.pIndex + 1];
- let z = attribute.array[3 * hit.pIndex + 2];
- let position = new THREE.Vector3(x, y, z);
-
- position.applyMatrix4( pc.matrixWorld );
-
- point[attributeName] = position;
-
- //add
- if(!params.all){
- if(camera.type == 'OrthographicCamera'){
- let vec = new THREE.Vector3().subVectors(position, camera.position)
- hit.disSquare = vec.projectOnVector( cameraDir ).length(); //只考虑到相机的垂直距离
- }else{
- hit.disSquare = camera.position.distanceTo(position)
- }
- }
- } else if (attributeName === 'indices') {
- } else {
- let values = attribute.array.slice(attribute.itemSize * hit.pIndex, attribute.itemSize * (hit.pIndex + 1)) ;
- if(attribute.potree){
- const {scale, offset} = attribute.potree;
- values = values.map(v => v / scale + offset);
- }
- point[attributeName] = values;
- //debugger;
- //if (values.itemSize === 1) {
- // point[attribute.name] = values.array[hit.pIndex];
- //} else {
- // let value = [];
- // for (let j = 0; j < values.itemSize; j++) {
- // value.push(values.array[values.itemSize * hit.pIndex + j]);
- // }
- // point[attribute.name] = value;
- //}
- }
-
- }
- hit.point = point;
- }
-
-
- viewer.addTimeMark('pick','end')
-
- if(params.all){
- return hits.map(hit => hit.point);
- }else{
- if(hits.length === 0){
- return null;
- }else{
- //为了防止透过点云缝隙,选到后排的点云,将选取的位置离相机的距离考虑进去。倾向选择离相机近、且离鼠标位置近的点。(否则按照原方案只选离鼠标位置最近的,可能从高楼不小心走到下层,导航选点也是)
- let sorted1 = hits.sort( (a, b) => a.disSquare - b.disSquare ).slice();
-
- let nearest = sorted1[0] //return nearest.point; //直接用最近点 在点云稀疏时不太跟手,如地面上,最近点往往在鼠标下方
- let r = 10
-
- hits.forEach( hit=>{
- let disDiff = hit.disSquare - nearest.disSquare //和最近点的偏差
- hit.disDiff = disDiff
- hit.score = -hit.distanceToCenter - disDiff * r
- })
-
- let sorted2 = hits.sort( (a, b) => b.score - a.score );
-
- //console.log(sorted2[0].point.position.z )
- return sorted2[0].point;
- }
- }
- }
- // 设置点大小
- changePointSize(num, sizeFitToLevel) {
- let size, nodeMaxLevel
-
- let dontRender = viewer.dealBeforeRender
-
- if(this.material.pointSizeType != PointSizeType.ATTENUATED){
- num && (size = num / Potree.config.material.realPointSize / 1.3)
- }else{
- let num_ = num
- if (num_ == void 0) {
- num_ = this.temp.pointSize
- } else {
- this.temp.pointSize = num_
- }
- num_ = num_ / (Potree.config.material.realPointSize / Potree.config.material.pointSize) //兼容
-
- num_ = Math.pow(num_, 1.05) * 6
-
-
- nodeMaxLevel = this.testMaxNodeCount >= Potree.config.testNodeCount1 ? this.nodeMaxLevel : this.nodeMaxLevelPredict.max //防止刚开始因nodeMaxLevel没涨完,导致过大的点云突然出现
-
- if(sizeFitToLevel || Potree.settings.sizeFitToLevel){//按照点云质量来调整的版本: 近似将pointSizeType换成ADAPTIVE
- let str = this.temp.pointSize+':'+this.maxLevel+':'+ nodeMaxLevel
- let value = this.temp.sizeFitToLevel[str] //储存。防止每次渲染(反复切换density)都要算。
- if(value){
- size = value
- }else{
-
- let base = this.material.spacing / Math.pow(2, this.maxLevel) //点云大小在level为0时设置为spacing,每长一级,大小就除以2. (不同场景还是会有偏差)
- let r = nodeMaxLevel > 0 ? this.maxLevel / nodeMaxLevel : 0.5 //越大,越精细,需要越缩小
- base *= nodeMaxLevel > 0 ? Math.max(0.1, Math.pow(r, 3*r+0.3 )) : 0.1 //低质量的缩小点,因为视觉上看太大了。navvis是不铺满的,我们也留一点缝隙(但是ortho是不用缩小的,如果能分开判断就好了)
-
- //base *= nodeMaxLevel > 0 ? Math.max(0.1, Math.pow(this.maxLevel / nodeMaxLevel, 1.1)) : 0.1 //低质量的缩小点,因为视觉上看太大了。navvis是不铺满的,我们也留一点缝隙(但是ortho是不用缩小的,如果能分开判断就好了)
- size = base * 5 * num_/* * window.devicePixelRatio */
- //在t-8BCqxQAr93 会议室 和 t-e2Kb2iU 隧道 两个场景里调节,因为它们的spacing相差较大,观察会议室墙壁的龟裂程度
- this.temp.sizeFitToLevel[str] = size
- }
- }else{
-
- /* let base = 0.007; */ let base = this.material.spacing / Math.pow(2, nodeMaxLevel) //点云大小在level为0时设置为spacing,每长一级,大小就除以2
- //base的数值理论上应该是右侧算出来的,但发现有的场景nodeMaxLevel和nodeMaxLevelPredict差别较大的点云显示也过大,而直接换成固定值反而可以适应所有场景。该固定值来源于 getHighestNodeSpacing 最小值,修改了下。(会不会是我们的相机其实该值是固定的,根据该值算出的spacing才是有误差的? 如果换了相机是否要改值?)
- //2022-12-21又换回非固定值。因为有的场景如SS-t-t01myDqnfE的两个数据集密集程度差别很大,应该将稀疏点云的大小设置的大些。 但是这样的缺点是两个数据集因相接处有大有小无法融合。
- size = base * 5 * num_ /* * window.devicePixelRatio */
- }
-
- }
- //console.log('changePointSize:' + this.dataset_id + ' , num: ' + (num && num.toPrecision(3)) + ' , size: ' + size.toPrecision(3), 'nodeMaxLevel', nodeMaxLevel.toPrecision(3), 'testMaxNodeCount',this.testMaxNodeCount /* this.material.spacing */)
- if(size){
- if(Potree.settings.sortCloudMat){//被废弃,不给material分组了
- this.size = size;this.material.size = size
- }else{
- this.material.size = size
- }
- }
- dontRender || viewer.dispatchEvent('content_changed')
-
- }
-
-
-
- // 设置点透明度
- changePointOpacity(num, canMoreThanOne ) {
- //num:0-1 navvis用的是亮度
- if (num == void 0) {
- num = this.temp.pointOpacity
- } else {
- this.temp.pointOpacity = num
- }
- let dontRender = viewer.dealBeforeRender //在执行beforeRender时更改的话不要发送content_changed 尤其分屏
-
-
- if(Potree.settings.notAdditiveBlending){
- return this.material.opacity = num
- }
- let opacity
- if (num == 1) {
- opacity = 1
- } else {
- let str = (Potree.settings.sizeFitToLevel?'sizeFit:':'')+ (canMoreThanOne ? 'canMoreThanOne:':'') +this.temp.pointOpacity+':'+this.maxLevel+':'+this.nodeMaxLevel
- let value = this.temp.opacity[str] //储存。防止每次渲染(反复切换density)都要算。
- if(value){
- opacity = value
- }else{
- if(Potree.settings.sizeFitToLevel){//按照点云质量来调整的版本:
- let base = this.material.spacing / Math.pow(1.7, this.maxLevel) //随着level提高,点云重叠几率增多
- let minBase = this.material.spacing / Math.pow(1.7, this.nodeMaxLevel)
- let ratio = Math.min(1 / base, 1 / minBase / 3) //ratio为一个能使opacity不大于1 的 乘量,minBase要除以一个数,该数调越大减弱效果越强。level越低opacity和面板越接,level越高效果越弱,以减免过度重叠后的亮度。
- opacity = base * ratio * num
- if(!canMoreThanOne){
- opacity = THREE.Math.clamp(opacity, 0, 0.999) //到1就不透明了(可能出现一段一样)
- }
- }else{
- let base = this.material.spacing / Math.pow(2, this.maxLevel)
- let minBase = this.material.spacing / Math.pow(2, this.nodeMaxLevel)
- //console.log(1 / base, 1 / minBase / 6)
- let ratio = Math.min(1 / base, 1 / minBase / 3) //ratio为一个能使opacity不大于1 的 乘量,minBase要除以一个数,该数调越大减弱效果越强。level越低opacity和面板越接,level越高效果越弱,以减免过度重叠后的亮度。
- opacity = base * ratio * num
- if(!canMoreThanOne){
- opacity = THREE.Math.clamp(opacity, 0, 0.999) //到1就不透明了(可能出现一段一样)
- }
- }
- this.temp.opacity[str] = opacity
- }
-
- //缺点:防止颜色过亮主要是相机离远时,当在漫游点处由于离点云太近,可能会导致高质量点云看起来很暗。
- }
- //console.log('changePointOpacity ' + this.dataset_id + ', num : ' + num + ' , opacity : ' + this.material.opacity) //检查是否做到了低质量时num==opacity,中质量opacity稍小于num,高质量更小
-
-
- if(Potree.settings.sortCloudMat){
- this.opacity = opacity; this.material.opacity = opacity
- }else{
- this.material.opacity = opacity
- }
-
- dontRender || viewer.dispatchEvent('content_changed')
- }
-
-
- updateBound(){
- var boundingBox_ = this.pcoGeometry.tightBoundingBox.clone().applyMatrix4(this.matrixWorld)//tightBoundingBox是点云原始的bound,但经过(绕z)旋转后bound有所改变,比之前体积更大。
- this.bound = boundingBox_
- this.bound2 = this.getBoundWithPanos()
- }
- getBoundWithPanos(){//确保panoBound在内的bound
- let bound = this.bound.clone()
- this.panos.forEach(pano=>{
- let panoBound = new THREE.Box3
- panoBound.expandByPoint(pano.position)
- panoBound.expandByVector(new THREE.Vector3(1,1,1));//give pano a margin
- bound.union(panoBound)
- })
- return bound
- }
-
- getPanosBound(){//仅由所有pano构成的bound
- if(this.panos.length > 0){
- let minSize = new THREE.Vector3(1,1,1)
- this.panosBound = math.getBoundByPoints(this.panos.map(e=>e.position), minSize)
- }else{
- this.panosBound = null
- }
- }
-
- getUnrotBoundPoint(type){//获取没有旋转的tightBounding的水平四个点
- //如果alighment支持任意轴旋转,水平面上看到的可能就是六边形了,失去意义,到时候不能用这个。也可以若只绕z旋转, 使用tightBoundingBox,否则bound
- let bound = this.pcoGeometry.tightBoundingBox
- if(type == 'all'){
- return [new THREE.Vector3(bound.min.x, bound.min.y, bound.min.z),
- new THREE.Vector3(bound.max.x, bound.min.y, bound.min.z),
- new THREE.Vector3(bound.max.x, bound.max.y,bound.min.z),
- new THREE.Vector3(bound.min.x, bound.max.y,bound.min.z),
- new THREE.Vector3(bound.min.x, bound.min.y, bound.max.z),
- new THREE.Vector3(bound.max.x, bound.min.y, bound.max.z),
- new THREE.Vector3(bound.max.x, bound.max.y,bound.max.z),
- new THREE.Vector3(bound.min.x, bound.max.y,bound.max.z),
- ].map(e=>e.applyMatrix4(this.matrixWorld))
- }else
- return [new THREE.Vector3(bound.min.x, bound.min.y,0),
- new THREE.Vector3(bound.max.x, bound.min.y,0),
- new THREE.Vector3(bound.max.x, bound.max.y,0),
- new THREE.Vector3(bound.min.x, bound.max.y,0),
- ].map(e=>e.applyMatrix4(this.matrixWorld))
- }
-
- getVolume(){
- /* var points = this.getUnrotBoundPoint() -----在只绕z轴旋转时这么写也行
- var area = Math.abs(math.getArea(points))
- return area * (this.bound.max.z - this.bound.min.z) */
- let bound = this.pcoGeometry.tightBoundingBox.clone()
- let size = bound.getSize(new THREE.Vector3)
- return size.x * size.y * size.z
- }
-
- ifContainsPoint(pos){//pos是否坐落于tightBound内
- /* if(!this.bound || !this.bound.containsPoint(pos))return ---这样写也行
- var points = this.getUnrotBoundPoint()
- return math.isPointInArea(points, null, pos) */
- //要把tightBoundingBox想象成一个volumeBox
- let box = this.pcoGeometry.tightBoundingBox
- let center = box.getCenter(new THREE.Vector3)
- let size = box.getSize(new THREE.Vector3)
- let boxMatrix = new THREE.Matrix4().setPosition(center.x,center.y,center.z);
- boxMatrix.scale(size);
- boxMatrix.premultiply(this.matrixWorld)
-
- return Potree.Utils.isIntersectBox(pos, boxMatrix)
- }
-
-
- intersectBox(boxWorldMatrix){
-
- let boxM = boxWorldMatrix.clone().premultiply(this.matrixWorld.clone().invert()); //box乘上点云逆矩阵 (因为点云的忽略掉其matrixWorld, 为了保持相对位置不变,box要左乘matrixWorld的逆)(因第一个参数bound不好变形,第二个参数box可以)
-
- return Potree.Utils.isIntersectBox(this.pcoGeometry.tightBoundingBox, boxM)
- }
-
-
-
-
-
- }
|