UNPKG

qwc2

Version:
14 lines 9.5 kB
function _slicedToArray(r,e){return _arrayWithHoles(r)||_iterableToArrayLimit(r,e)||_unsupportedIterableToArray(r,e)||_nonIterableRest()}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _iterableToArrayLimit(r,l){var t=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=t){var e,n,i,u,a=[],f=!0,o=!1;try{if(i=(t=t.call(r)).next,0===l){if(Object(t)!==t)return;f=!1}else for(;!(f=(e=i.call(t)).done)&&(a.push(e.value),a.length!==l);f=!0);}catch(r){o=!0,n=r}finally{try{if(!f&&null!=t["return"]&&(u=t["return"](),Object(u)!==u))return}finally{if(o)throw n}}return a}}function _arrayWithHoles(r){if(Array.isArray(r))return r}function _toConsumableArray(r){return _arrayWithoutHoles(r)||_iterableToArray(r)||_unsupportedIterableToArray(r)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _unsupportedIterableToArray(r,a){if(r){if("string"==typeof r)return _arrayLikeToArray(r,a);var t={}.toString.call(r).slice(8,-1);return"Object"===t&&r.constructor&&(t=r.constructor.name),"Map"===t||"Set"===t?Array.from(r):"Arguments"===t||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t)?_arrayLikeToArray(r,a):void 0}}function _iterableToArray(r){if("undefined"!=typeof Symbol&&null!=r[Symbol.iterator]||null!=r["@@iterator"])return Array.from(r)}function _arrayWithoutHoles(r){if(Array.isArray(r))return _arrayLikeToArray(r)}function _arrayLikeToArray(r,a){(null==a||a>r.length)&&(a=r.length);for(var e=0,n=Array(a);e<a;e++)n[e]=r[e];return n}/** * Copyright 2025 Sourcepole AG * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. */import{Parser as ExprParser}from"expr-eval";import isEmpty from"lodash.isempty";import parseCssColor from"parse-css-color";import{Float32BufferAttribute,Group,Vector3}from"three";import MiscUtils from"../../../utils/MiscUtils";import{createLabelObject}from"./MiscUtils3D";var styleExpressionParser=new ExprParser;styleExpressionParser.functions.color=function(name){var alpha=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1;var color=parseCssColor(name);return[].concat(_toConsumableArray(color.values.map(function(c){return c/255})),[alpha])};styleExpressionParser.functions.rgb=function(r,g,b){return[r/255,g/255,g/255,1]};styleExpressionParser.functions.rgba=function(r,g,b,a){return[r/255,g/255,g/255,a]};styleExpressionParser.functions.hsl=function(h,s,l){return[].concat(_toConsumableArray(MiscUtils.hslToRgb(h,s,l)),[1])};styleExpressionParser.functions.hsla=function(h,s,l,a){return[].concat(_toConsumableArray(MiscUtils.hslToRgb(h,s,l)),[a])};function batchColor(batchId,batchAttr,context){var _context$batchStyles$,_context$batchStyles;if(((_context$batchStyles$=(_context$batchStyles=context.batchStyles)===null||_context$batchStyles===void 0||(_context$batchStyles=_context$batchStyles[batchId])===null||_context$batchStyles===void 0?void 0:_context$batchStyles.color)!==null&&_context$batchStyles$!==void 0?_context$batchStyles$:null)!==null){var color=parseCssColor(context.batchStyles[batchId].color);return[].concat(_toConsumableArray(color.values.map(function(c){return c/255})),[color.alpha])}else if(context.colorExpressions.length){try{for(var i=0;i<context.colorExpressions.length;++i){var condition=context.colorExpressions[i][0].evaluate(batchAttr);if(condition){return context.colorExpressions[i][1].evaluate(batchAttr)}}}catch(e){/* eslint-disable-next-line */console.warn("Failed to parse color expression: "+String(e))}return null}else if(batchAttr[context.colorAttr]){var _batchAttr$context$al;var _color=batchAttr[context.colorAttr];var alpha=context.alphaAttr?(_batchAttr$context$al=batchAttr[context.alphaAttr])!==null&&_batchAttr$context$al!==void 0?_batchAttr$context$al:255:255;var r=(_color>>16&255)/255;var g=(_color>>8&255)/255;var b=(_color&255)/255;return[r,g,b,alpha/255]}else{return null}}function batchLabel(batchId,batchAttr,context){var _context$batchStyles$2,_context$batchStyles2;if(((_context$batchStyles$2=(_context$batchStyles2=context.batchStyles)===null||_context$batchStyles2===void 0||(_context$batchStyles2=_context$batchStyles2[batchId])===null||_context$batchStyles2===void 0?void 0:_context$batchStyles2.label)!==null&&_context$batchStyles$2!==void 0?_context$batchStyles$2:null)!==null){var _context$batchStyles$3;return{text:context.batchStyles[batchId].label,offset:(_context$batchStyles$3=context.batchStyles[batchId].labelOffset)!==null&&_context$batchStyles$3!==void 0?_context$batchStyles$3:80}}else if(context.labelAttr){return batchAttr[context.labelAttr]}else{return null}}var Tiles3DStyle={applyTileStyle:function applyTileStyle(group,config,sceneContext){var _config$idAttr,_config$tilesetStyle,_config$tilesetStyle2;var batchColorCache={};var batchLabelCache={};var labels={};var idAttr=(_config$idAttr=config.idAttr)!==null&&_config$idAttr!==void 0?_config$idAttr:"id";var context={colorExpressions:[],batchStyles:(_config$tilesetStyle=config.tilesetStyle)===null||_config$tilesetStyle===void 0?void 0:_config$tilesetStyle.batchstyles,colorAttr:config.colorAttr,alphaAttr:config.alphaAttr,labelAttr:config.labelAttr};var baseColor=[1,1,1,1];var customBaseColor=false;if(config.baseColor){var color=parseCssColor(config.baseColor);baseColor=[].concat(_toConsumableArray(color.values.map(function(x){return x/255})),[color.alpha]);customBaseColor=true}var colorRules=(_config$tilesetStyle2=config.tilesetStyle)===null||_config$tilesetStyle2===void 0?void 0:_config$tilesetStyle2.color;var parseExpr=function parseExpr(expr){var cleanExpr=expr.replace(/\$\{(\w+)\}/g,"$1").replaceAll("===","==").replaceAll("!==","==");return styleExpressionParser.parse(cleanExpr)};if(colorRules!==null&&colorRules!==void 0&&colorRules.conditions){colorRules===null||colorRules===void 0||colorRules.conditions.map(function(cond){context.colorExpressions.push([parseExpr(cond[0]),parseExpr(cond[1])])})}else if(typeof colorRules==="string"){context.colorExpressions.push([parseExpr("true"),parseExpr(colorRules)])}group.traverse(function(c){if(c.geometry){var batchidxAttr=c.geometry.getAttribute("_batchid");if(!batchidxAttr){return}var batchPosAttr=c.geometry.getAttribute("position");var rgbaColors=[];var rgbColors=[];var haveColor=customBaseColor;var haveAlpha=baseColor[3]<1;batchidxAttr.array.forEach(function(batchIdx,idx){var batchAttr=group.batchTable.getDataFromId(batchIdx);var batchId=String(batchAttr[idAttr]);// Handle color var color=batchColorCache[batchIdx];if(color===undefined){color=batchColorCache[batchIdx]=batchColor(batchId,batchAttr,context)}if(color){haveColor=true;haveAlpha|=color[3]<1;rgbaColors.push.apply(rgbaColors,_toConsumableArray(color));rgbColors.push.apply(rgbColors,_toConsumableArray(color.slice(0,3)))}else{rgbaColors.push.apply(rgbaColors,_toConsumableArray(baseColor));rgbColors.push.apply(rgbColors,_toConsumableArray(baseColor.slice(0,3)))}// Handle label var label=batchLabelCache[batchIdx];if(label===undefined){label=batchLabelCache[batchIdx]=batchLabel(batchId,batchAttr,context)}if(label){var pos=batchPosAttr.array.slice(3*idx,3*idx+3);var entry=labels[batchIdx];if(!entry){entry=labels[batchIdx]={label:label.text,labelOffset:label.offset,pos:pos,ymax:pos[1],count:1,matrix:c.matrixWorld}}else{entry.pos[0]+=pos[0];entry.pos[1]+=pos[1];entry.pos[2]+=pos[2];entry.ymax=Math.max(entry.ymax,pos[1]);++entry.count}}});// NOTE: Also update color buffers if they were previously colored if(haveColor||group.userData.haveColor){if(haveAlpha){c.geometry.setAttribute("color",new Float32BufferAttribute(rgbaColors,4))}else{// Discard alpha var count=rgbaColors.length/4;var bufAttr=new Float32BufferAttribute(count*3,3);for(var i=0,j=0;i<count;++i){bufAttr.array[j++]=rgbaColors[4*i];bufAttr.array[j++]=rgbaColors[4*i+1];bufAttr.array[j++]=rgbaColors[4*i+2]}c.geometry.setAttribute("color",bufAttr)}c.material.vertexColors=true;c.material.transparent=haveAlpha;group.userData.haveColor=haveColor}}});// Clear previous labels if(group.userData.labelGroup){group.remove(group.children.find(function(child){return child.uuid===group.userData.labelGroup}));// Explicitly remove label DOM elements Object.values(group.userData.tileLabels).forEach(function(entry){entry.labelObject.element.parentNode.removeChild(entry.labelObject.element)});delete group.userData.tileLabels;delete group.userData.labelGroup}// Add new labels if(!isEmpty(labels)){var tileLabels={};var labelObjects=new Group;Object.entries(labels).forEach(function(_ref){var _ref2=_slicedToArray(_ref,2),batchId=_ref2[0],entry=_ref2[1];var pos=new Vector3(entry.pos[0]/entry.count,entry.pos[1]/entry.count,entry.pos[2]/entry.count).applyMatrix4(entry.matrix);var maxpos=new Vector3(entry.pos[0]/entry.count,entry.ymax,entry.pos[2]/entry.count).applyMatrix4(entry.matrix);tileLabels[batchId]={pos:pos,label:entry.label,labelOffset:entry.labelOffset};tileLabels[batchId].labelObject=createLabelObject(entry.label,pos,sceneContext,0,entry.labelOffset+(maxpos.y-pos.y));labelObjects.add(tileLabels[batchId].labelObject)});group.userData.tileLabels=tileLabels;group.userData.labelGroup=labelObjects.uuid;group.add(labelObjects)}}};export default Tiles3DStyle;