(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = global || self, factory(global.Potree = {})); }(this, (function (exports) { 'use strict'; // threejs.org/license const REVISION = '124'; const MOUSE = { LEFT: 0, MIDDLE: 1, RIGHT: 2, ROTATE: 0, DOLLY: 1, PAN: 2 }; const TOUCH = { ROTATE: 0, PAN: 1, DOLLY_PAN: 2, DOLLY_ROTATE: 3 }; const CullFaceNone = 0; const CullFaceBack = 1; const CullFaceFront = 2; const CullFaceFrontBack = 3; const BasicShadowMap = 0; const PCFShadowMap = 1; const PCFSoftShadowMap = 2; const VSMShadowMap = 3; const FrontSide = 0; const BackSide = 1; const DoubleSide = 2; const FlatShading$1 = 1; const SmoothShading = 2; const NoBlending = 0; const NormalBlending = 1; const AdditiveBlending = 2; const SubtractiveBlending = 3; const MultiplyBlending = 4; const CustomBlending = 5; const AddEquation = 100; const SubtractEquation = 101; const ReverseSubtractEquation = 102; const MinEquation = 103; const MaxEquation = 104; const ZeroFactor = 200; const OneFactor = 201; const SrcColorFactor = 202; const OneMinusSrcColorFactor = 203; const SrcAlphaFactor = 204; const OneMinusSrcAlphaFactor = 205; const DstAlphaFactor = 206; const OneMinusDstAlphaFactor = 207; const DstColorFactor = 208; const OneMinusDstColorFactor = 209; const SrcAlphaSaturateFactor = 210; const NeverDepth = 0; const AlwaysDepth = 1; const LessDepth = 2; const LessEqualDepth = 3; const EqualDepth = 4; const GreaterEqualDepth = 5; const GreaterDepth = 6; const NotEqualDepth = 7; const MultiplyOperation = 0; const MixOperation = 1; const AddOperation = 2; const NoToneMapping = 0; const LinearToneMapping = 1; const ReinhardToneMapping = 2; const CineonToneMapping = 3; const ACESFilmicToneMapping = 4; const CustomToneMapping = 5; const UVMapping = 300; const CubeReflectionMapping = 301; const CubeRefractionMapping = 302; const EquirectangularReflectionMapping = 303; const EquirectangularRefractionMapping = 304; const CubeUVReflectionMapping = 306; const CubeUVRefractionMapping = 307; const RepeatWrapping = 1000; const ClampToEdgeWrapping = 1001; const MirroredRepeatWrapping = 1002; const NearestFilter = 1003; const NearestMipmapNearestFilter = 1004; const NearestMipMapNearestFilter = 1004; const NearestMipmapLinearFilter = 1005; const NearestMipMapLinearFilter = 1005; const LinearFilter = 1006; const LinearMipmapNearestFilter = 1007; const LinearMipMapNearestFilter = 1007; const LinearMipmapLinearFilter = 1008; const LinearMipMapLinearFilter = 1008; const UnsignedByteType = 1009; const ByteType = 1010; const ShortType = 1011; const UnsignedShortType = 1012; const IntType = 1013; const UnsignedIntType = 1014; const FloatType = 1015; const HalfFloatType = 1016; const UnsignedShort4444Type = 1017; const UnsignedShort5551Type = 1018; const UnsignedShort565Type = 1019; const UnsignedInt248Type$1 = 1020; const AlphaFormat = 1021; const RGBFormat = 1022; const RGBAFormat = 1023; const LuminanceFormat = 1024; const LuminanceAlphaFormat = 1025; const RGBEFormat = RGBAFormat; const DepthFormat = 1026; const DepthStencilFormat = 1027; const RedFormat = 1028; const RedIntegerFormat = 1029; const RGFormat = 1030; const RGIntegerFormat = 1031; const RGBIntegerFormat = 1032; const RGBAIntegerFormat = 1033; const RGB_S3TC_DXT1_Format = 33776; const RGBA_S3TC_DXT1_Format$1 = 33777; const RGBA_S3TC_DXT3_Format = 33778; const RGBA_S3TC_DXT5_Format$1 = 33779; const RGB_PVRTC_4BPPV1_Format = 35840; const RGB_PVRTC_2BPPV1_Format = 35841; const RGBA_PVRTC_4BPPV1_Format = 35842; const RGBA_PVRTC_2BPPV1_Format = 35843; const RGB_ETC1_Format = 36196; const RGB_ETC2_Format = 37492; const RGBA_ETC2_EAC_Format = 37496; const RGBA_ASTC_4x4_Format = 37808; const RGBA_ASTC_5x4_Format = 37809; const RGBA_ASTC_5x5_Format = 37810; const RGBA_ASTC_6x5_Format = 37811; const RGBA_ASTC_6x6_Format = 37812; const RGBA_ASTC_8x5_Format = 37813; const RGBA_ASTC_8x6_Format = 37814; const RGBA_ASTC_8x8_Format = 37815; const RGBA_ASTC_10x5_Format = 37816; const RGBA_ASTC_10x6_Format = 37817; const RGBA_ASTC_10x8_Format = 37818; const RGBA_ASTC_10x10_Format = 37819; const RGBA_ASTC_12x10_Format = 37820; const RGBA_ASTC_12x12_Format = 37821; const RGBA_BPTC_Format = 36492; const SRGB8_ALPHA8_ASTC_4x4_Format = 37840; const SRGB8_ALPHA8_ASTC_5x4_Format = 37841; const SRGB8_ALPHA8_ASTC_5x5_Format = 37842; const SRGB8_ALPHA8_ASTC_6x5_Format = 37843; const SRGB8_ALPHA8_ASTC_6x6_Format = 37844; const SRGB8_ALPHA8_ASTC_8x5_Format = 37845; const SRGB8_ALPHA8_ASTC_8x6_Format = 37846; const SRGB8_ALPHA8_ASTC_8x8_Format = 37847; const SRGB8_ALPHA8_ASTC_10x5_Format = 37848; const SRGB8_ALPHA8_ASTC_10x6_Format = 37849; const SRGB8_ALPHA8_ASTC_10x8_Format = 37850; const SRGB8_ALPHA8_ASTC_10x10_Format = 37851; const SRGB8_ALPHA8_ASTC_12x10_Format = 37852; const SRGB8_ALPHA8_ASTC_12x12_Format = 37853; const LoopOnce = 2200; const LoopRepeat = 2201; const LoopPingPong = 2202; const InterpolateDiscrete = 2300; const InterpolateLinear = 2301; const InterpolateSmooth = 2302; const ZeroCurvatureEnding = 2400; const ZeroSlopeEnding = 2401; const WrapAroundEnding = 2402; const NormalAnimationBlendMode = 2500; const AdditiveAnimationBlendMode = 2501; const TrianglesDrawMode = 0; const TriangleStripDrawMode = 1; const TriangleFanDrawMode = 2; const LinearEncoding = 3000; const sRGBEncoding = 3001; const GammaEncoding = 3007; const RGBEEncoding = 3002; const LogLuvEncoding = 3003; const RGBM7Encoding = 3004; const RGBM16Encoding = 3005; const RGBDEncoding = 3006; const BasicDepthPacking = 3200; const RGBADepthPacking = 3201; const TangentSpaceNormalMap = 0; const ObjectSpaceNormalMap = 1; const ZeroStencilOp = 0; const KeepStencilOp = 7680; const ReplaceStencilOp = 7681; const IncrementStencilOp = 7682; const DecrementStencilOp = 7683; const IncrementWrapStencilOp = 34055; const DecrementWrapStencilOp = 34056; const InvertStencilOp = 5386; const NeverStencilFunc = 512; const LessStencilFunc = 513; const EqualStencilFunc = 514; const LessEqualStencilFunc = 515; const GreaterStencilFunc = 516; const NotEqualStencilFunc = 517; const GreaterEqualStencilFunc = 518; const AlwaysStencilFunc = 519; const StaticDrawUsage = 35044; const DynamicDrawUsage = 35048; const StreamDrawUsage = 35040; const StaticReadUsage = 35045; const DynamicReadUsage = 35049; const StreamReadUsage = 35041; const StaticCopyUsage = 35046; const DynamicCopyUsage = 35050; const StreamCopyUsage = 35042; const GLSL1 = '100'; const GLSL3 = '300 es'; /** * https://github.com/mrdoob/eventdispatcher.js/ */ function EventDispatcher() {} Object.assign( EventDispatcher.prototype, { addEventListener: function ( type, listener ) { if ( this._listeners === undefined ) this._listeners = {}; const listeners = this._listeners; if ( listeners[ type ] === undefined ) { listeners[ type ] = []; } if ( listeners[ type ].indexOf( listener ) === - 1 ) { listeners[ type ].push( listener ); } }, hasEventListener: function ( type, listener ) { if ( this._listeners === undefined ) return false; const listeners = this._listeners; return listeners[ type ] !== undefined && listeners[ type ].indexOf( listener ) !== - 1; }, dispatchEvent: function ( event ) { if ( this._listeners === undefined ) return; const listeners = this._listeners; const listenerArray = listeners[ event.type ]; if ( listenerArray !== undefined ) { event.target = this; // Make a copy, in case listeners are removed while iterating. const array = listenerArray.slice( 0 ); for ( let i = 0, l = array.length; i < l; i ++ ) { array[ i ].call( this, event ); } } } } ); const _lut = []; for ( let i = 0; i < 256; i ++ ) { _lut[ i ] = ( i < 16 ? '0' : '' ) + ( i ).toString( 16 ); } let _seed = 1234567; const MathUtils = { DEG2RAD: Math.PI / 180, RAD2DEG: 180 / Math.PI, generateUUID: function () { // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/21963136#21963136 const d0 = Math.random() * 0xffffffff | 0; const d1 = Math.random() * 0xffffffff | 0; const d2 = Math.random() * 0xffffffff | 0; const d3 = Math.random() * 0xffffffff | 0; const uuid = _lut[ d0 & 0xff ] + _lut[ d0 >> 8 & 0xff ] + _lut[ d0 >> 16 & 0xff ] + _lut[ d0 >> 24 & 0xff ] + '-' + _lut[ d1 & 0xff ] + _lut[ d1 >> 8 & 0xff ] + '-' + _lut[ d1 >> 16 & 0x0f | 0x40 ] + _lut[ d1 >> 24 & 0xff ] + '-' + _lut[ d2 & 0x3f | 0x80 ] + _lut[ d2 >> 8 & 0xff ] + '-' + _lut[ d2 >> 16 & 0xff ] + _lut[ d2 >> 24 & 0xff ] + _lut[ d3 & 0xff ] + _lut[ d3 >> 8 & 0xff ] + _lut[ d3 >> 16 & 0xff ] + _lut[ d3 >> 24 & 0xff ]; // .toUpperCase() here flattens concatenated strings to save heap memory space. return uuid.toUpperCase(); }, clamp: function ( value, min, max ) { return Math.max( min, Math.min( max, value ) ); }, // compute euclidian modulo of m % n // https://en.wikipedia.org/wiki/Modulo_operation euclideanModulo: function ( n, m ) { return ( ( n % m ) + m ) % m; }, // Linear mapping from range to range mapLinear: function ( x, a1, a2, b1, b2 ) { return b1 + ( x - a1 ) * ( b2 - b1 ) / ( a2 - a1 ); }, // https://en.wikipedia.org/wiki/Linear_interpolation lerp: function ( x, y, t ) { return ( 1 - t ) * x + t * y; }, // http://en.wikipedia.org/wiki/Smoothstep smoothstep: function ( x, min, max ) { if ( x <= min ) return 0; if ( x >= max ) return 1; x = ( x - min ) / ( max - min ); return x * x * ( 3 - 2 * x ); }, smootherstep: function ( x, min, max ) { if ( x <= min ) return 0; if ( x >= max ) return 1; x = ( x - min ) / ( max - min ); return x * x * x * ( x * ( x * 6 - 15 ) + 10 ); }, // Random integer from interval randInt: function ( low, high ) { return low + Math.floor( Math.random() * ( high - low + 1 ) ); }, // Random float from interval randFloat: function ( low, high ) { return low + Math.random() * ( high - low ); }, // Random float from <-range/2, range/2> interval randFloatSpread: function ( range ) { return range * ( 0.5 - Math.random() ); }, // Deterministic pseudo-random float in the interval [ 0, 1 ] seededRandom: function ( s ) { if ( s !== undefined ) _seed = s % 2147483647; // Park-Miller algorithm _seed = _seed * 16807 % 2147483647; return ( _seed - 1 ) / 2147483646; }, degToRad: function ( degrees ) { return degrees * MathUtils.DEG2RAD; }, radToDeg: function ( radians ) { return radians * MathUtils.RAD2DEG; }, isPowerOfTwo: function ( value ) { return ( value & ( value - 1 ) ) === 0 && value !== 0; }, ceilPowerOfTwo: function ( value ) { return Math.pow( 2, Math.ceil( Math.log( value ) / Math.LN2 ) ); }, floorPowerOfTwo: function ( value ) { return Math.pow( 2, Math.floor( Math.log( value ) / Math.LN2 ) ); }, setQuaternionFromProperEuler: function ( q, a, b, c, order ) { // Intrinsic Proper Euler Angles - see https://en.wikipedia.org/wiki/Euler_angles // rotations are applied to the axes in the order specified by 'order' // rotation by angle 'a' is applied first, then by angle 'b', then by angle 'c' // angles are in radians const cos = Math.cos; const sin = Math.sin; const c2 = cos( b / 2 ); const s2 = sin( b / 2 ); const c13 = cos( ( a + c ) / 2 ); const s13 = sin( ( a + c ) / 2 ); const c1_3 = cos( ( a - c ) / 2 ); const s1_3 = sin( ( a - c ) / 2 ); const c3_1 = cos( ( c - a ) / 2 ); const s3_1 = sin( ( c - a ) / 2 ); switch ( order ) { case 'XYX': q.set( c2 * s13, s2 * c1_3, s2 * s1_3, c2 * c13 ); break; case 'YZY': q.set( s2 * s1_3, c2 * s13, s2 * c1_3, c2 * c13 ); break; case 'ZXZ': q.set( s2 * c1_3, s2 * s1_3, c2 * s13, c2 * c13 ); break; case 'XZX': q.set( c2 * s13, s2 * s3_1, s2 * c3_1, c2 * c13 ); break; case 'YXY': q.set( s2 * c3_1, c2 * s13, s2 * s3_1, c2 * c13 ); break; case 'ZYZ': q.set( s2 * s3_1, s2 * c3_1, c2 * s13, c2 * c13 ); break; default: console.warn( 'THREE.MathUtils: .setQuaternionFromProperEuler() encountered an unknown order: ' + order ); } } }; class Vector2 { constructor( x = 0, y = 0 ) { Object.defineProperty( this, 'isVector2', { value: true } ); this.x = x; this.y = y; } get width() { return this.x; } set width( value ) { this.x = value; } get height() { return this.y; } set height( value ) { this.y = value; } set( x, y ) { this.x = x; this.y = y; return this; } setScalar( scalar ) { this.x = scalar; this.y = scalar; return this; } setX( x ) { this.x = x; return this; } setY( y ) { this.y = y; return this; } setComponent( index, value ) { switch ( index ) { case 0: this.x = value; break; case 1: this.y = value; break; default: throw new Error( 'index is out of range: ' + index ); } return this; } getComponent( index ) { switch ( index ) { case 0: return this.x; case 1: return this.y; default: throw new Error( 'index is out of range: ' + index ); } } clone() { return new this.constructor( this.x, this.y ); } copy( v ) { this.x = v.x; this.y = v.y; return this; } add( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector2: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' ); return this.addVectors( v, w ); } this.x += v.x; this.y += v.y; return this; } addScalar( s ) { this.x += s; this.y += s; return this; } addVectors( a, b ) { this.x = a.x + b.x; this.y = a.y + b.y; return this; } addScaledVector( v, s ) { this.x += v.x * s; this.y += v.y * s; return this; } sub( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector2: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' ); return this.subVectors( v, w ); } this.x -= v.x; this.y -= v.y; return this; } subScalar( s ) { this.x -= s; this.y -= s; return this; } subVectors( a, b ) { this.x = a.x - b.x; this.y = a.y - b.y; return this; } multiply( v ) { this.x *= v.x; this.y *= v.y; return this; } multiplyScalar( scalar ) { this.x *= scalar; this.y *= scalar; return this; } divide( v ) { this.x /= v.x; this.y /= v.y; return this; } divideScalar( scalar ) { return this.multiplyScalar( 1 / scalar ); } applyMatrix3( m ) { const x = this.x, y = this.y; const e = m.elements; this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ]; this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ]; return this; } min( v ) { this.x = Math.min( this.x, v.x ); this.y = Math.min( this.y, v.y ); return this; } max( v ) { this.x = Math.max( this.x, v.x ); this.y = Math.max( this.y, v.y ); return this; } clamp( min, max ) { // assumes min < max, componentwise this.x = Math.max( min.x, Math.min( max.x, this.x ) ); this.y = Math.max( min.y, Math.min( max.y, this.y ) ); return this; } clampScalar( minVal, maxVal ) { this.x = Math.max( minVal, Math.min( maxVal, this.x ) ); this.y = Math.max( minVal, Math.min( maxVal, this.y ) ); return this; } clampLength( min, max ) { const length = this.length(); return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) ); } floor() { this.x = Math.floor( this.x ); this.y = Math.floor( this.y ); return this; } ceil() { this.x = Math.ceil( this.x ); this.y = Math.ceil( this.y ); return this; } round() { this.x = Math.round( this.x ); this.y = Math.round( this.y ); return this; } roundToZero() { this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x ); this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y ); return this; } negate() { this.x = - this.x; this.y = - this.y; return this; } dot( v ) { return this.x * v.x + this.y * v.y; } cross( v ) { return this.x * v.y - this.y * v.x; } lengthSq() { return this.x * this.x + this.y * this.y; } length() { return Math.sqrt( this.x * this.x + this.y * this.y ); } manhattanLength() { return Math.abs( this.x ) + Math.abs( this.y ); } normalize() { return this.divideScalar( this.length() || 1 ); } angle() { // computes the angle in radians with respect to the positive x-axis const angle = Math.atan2( - this.y, - this.x ) + Math.PI; return angle; } distanceTo( v ) { return Math.sqrt( this.distanceToSquared( v ) ); } distanceToSquared( v ) { const dx = this.x - v.x, dy = this.y - v.y; return dx * dx + dy * dy; } manhattanDistanceTo( v ) { return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ); } setLength( length ) { return this.normalize().multiplyScalar( length ); } lerp( v, alpha ) { this.x += ( v.x - this.x ) * alpha; this.y += ( v.y - this.y ) * alpha; return this; } lerpVectors( v1, v2, alpha ) { this.x = v1.x + ( v2.x - v1.x ) * alpha; this.y = v1.y + ( v2.y - v1.y ) * alpha; return this; } equals( v ) { return ( ( v.x === this.x ) && ( v.y === this.y ) ); } fromArray( array, offset = 0 ) { this.x = array[ offset ]; this.y = array[ offset + 1 ]; return this; } toArray( array = [], offset = 0 ) { array[ offset ] = this.x; array[ offset + 1 ] = this.y; return array; } fromBufferAttribute( attribute, index, offset ) { if ( offset !== undefined ) { console.warn( 'THREE.Vector2: offset has been removed from .fromBufferAttribute().' ); } this.x = attribute.getX( index ); this.y = attribute.getY( index ); return this; } rotateAround( center, angle ) { const c = Math.cos( angle ), s = Math.sin( angle ); const x = this.x - center.x; const y = this.y - center.y; this.x = x * c - y * s + center.x; this.y = x * s + y * c + center.y; return this; } random() { this.x = Math.random(); this.y = Math.random(); return this; } } class Matrix3 { constructor() { Object.defineProperty( this, 'isMatrix3', { value: true } ); this.elements = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ]; if ( arguments.length > 0 ) { console.error( 'THREE.Matrix3: the constructor no longer reads arguments. use .set() instead.' ); } } set( n11, n12, n13, n21, n22, n23, n31, n32, n33 ) { const te = this.elements; te[ 0 ] = n11; te[ 1 ] = n21; te[ 2 ] = n31; te[ 3 ] = n12; te[ 4 ] = n22; te[ 5 ] = n32; te[ 6 ] = n13; te[ 7 ] = n23; te[ 8 ] = n33; return this; } identity() { this.set( 1, 0, 0, 0, 1, 0, 0, 0, 1 ); return this; } clone() { return new this.constructor().fromArray( this.elements ); } copy( m ) { const te = this.elements; const me = m.elements; te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ]; te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; te[ 8 ] = me[ 8 ]; return this; } extractBasis( xAxis, yAxis, zAxis ) { xAxis.setFromMatrix3Column( this, 0 ); yAxis.setFromMatrix3Column( this, 1 ); zAxis.setFromMatrix3Column( this, 2 ); return this; } setFromMatrix4( m ) { const me = m.elements; this.set( me[ 0 ], me[ 4 ], me[ 8 ], me[ 1 ], me[ 5 ], me[ 9 ], me[ 2 ], me[ 6 ], me[ 10 ] ); return this; } multiply( m ) { return this.multiplyMatrices( this, m ); } premultiply( m ) { return this.multiplyMatrices( m, this ); } multiplyMatrices( a, b ) { const ae = a.elements; const be = b.elements; const te = this.elements; const a11 = ae[ 0 ], a12 = ae[ 3 ], a13 = ae[ 6 ]; const a21 = ae[ 1 ], a22 = ae[ 4 ], a23 = ae[ 7 ]; const a31 = ae[ 2 ], a32 = ae[ 5 ], a33 = ae[ 8 ]; const b11 = be[ 0 ], b12 = be[ 3 ], b13 = be[ 6 ]; const b21 = be[ 1 ], b22 = be[ 4 ], b23 = be[ 7 ]; const b31 = be[ 2 ], b32 = be[ 5 ], b33 = be[ 8 ]; te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31; te[ 3 ] = a11 * b12 + a12 * b22 + a13 * b32; te[ 6 ] = a11 * b13 + a12 * b23 + a13 * b33; te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31; te[ 4 ] = a21 * b12 + a22 * b22 + a23 * b32; te[ 7 ] = a21 * b13 + a22 * b23 + a23 * b33; te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31; te[ 5 ] = a31 * b12 + a32 * b22 + a33 * b32; te[ 8 ] = a31 * b13 + a32 * b23 + a33 * b33; return this; } multiplyScalar( s ) { const te = this.elements; te[ 0 ] *= s; te[ 3 ] *= s; te[ 6 ] *= s; te[ 1 ] *= s; te[ 4 ] *= s; te[ 7 ] *= s; te[ 2 ] *= s; te[ 5 ] *= s; te[ 8 ] *= s; return this; } determinant() { const te = this.elements; const a = te[ 0 ], b = te[ 1 ], c = te[ 2 ], d = te[ 3 ], e = te[ 4 ], f = te[ 5 ], g = te[ 6 ], h = te[ 7 ], i = te[ 8 ]; return a * e * i - a * f * h - b * d * i + b * f * g + c * d * h - c * e * g; } invert() { const te = this.elements, n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ], n12 = te[ 3 ], n22 = te[ 4 ], n32 = te[ 5 ], n13 = te[ 6 ], n23 = te[ 7 ], n33 = te[ 8 ], t11 = n33 * n22 - n32 * n23, t12 = n32 * n13 - n33 * n12, t13 = n23 * n12 - n22 * n13, det = n11 * t11 + n21 * t12 + n31 * t13; if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0 ); const detInv = 1 / det; te[ 0 ] = t11 * detInv; te[ 1 ] = ( n31 * n23 - n33 * n21 ) * detInv; te[ 2 ] = ( n32 * n21 - n31 * n22 ) * detInv; te[ 3 ] = t12 * detInv; te[ 4 ] = ( n33 * n11 - n31 * n13 ) * detInv; te[ 5 ] = ( n31 * n12 - n32 * n11 ) * detInv; te[ 6 ] = t13 * detInv; te[ 7 ] = ( n21 * n13 - n23 * n11 ) * detInv; te[ 8 ] = ( n22 * n11 - n21 * n12 ) * detInv; return this; } transpose() { let tmp; const m = this.elements; tmp = m[ 1 ]; m[ 1 ] = m[ 3 ]; m[ 3 ] = tmp; tmp = m[ 2 ]; m[ 2 ] = m[ 6 ]; m[ 6 ] = tmp; tmp = m[ 5 ]; m[ 5 ] = m[ 7 ]; m[ 7 ] = tmp; return this; } getNormalMatrix( matrix4 ) { return this.setFromMatrix4( matrix4 ).copy( this ).invert().transpose(); } transposeIntoArray( r ) { const m = this.elements; r[ 0 ] = m[ 0 ]; r[ 1 ] = m[ 3 ]; r[ 2 ] = m[ 6 ]; r[ 3 ] = m[ 1 ]; r[ 4 ] = m[ 4 ]; r[ 5 ] = m[ 7 ]; r[ 6 ] = m[ 2 ]; r[ 7 ] = m[ 5 ]; r[ 8 ] = m[ 8 ]; return this; } setUvTransform( tx, ty, sx, sy, rotation, cx, cy ) { const c = Math.cos( rotation ); const s = Math.sin( rotation ); this.set( sx * c, sx * s, - sx * ( c * cx + s * cy ) + cx + tx, - sy * s, sy * c, - sy * ( - s * cx + c * cy ) + cy + ty, 0, 0, 1 ); return this; } scale( sx, sy ) { const te = this.elements; te[ 0 ] *= sx; te[ 3 ] *= sx; te[ 6 ] *= sx; te[ 1 ] *= sy; te[ 4 ] *= sy; te[ 7 ] *= sy; return this; } rotate( theta ) { const c = Math.cos( theta ); const s = Math.sin( theta ); const te = this.elements; const a11 = te[ 0 ], a12 = te[ 3 ], a13 = te[ 6 ]; const a21 = te[ 1 ], a22 = te[ 4 ], a23 = te[ 7 ]; te[ 0 ] = c * a11 + s * a21; te[ 3 ] = c * a12 + s * a22; te[ 6 ] = c * a13 + s * a23; te[ 1 ] = - s * a11 + c * a21; te[ 4 ] = - s * a12 + c * a22; te[ 7 ] = - s * a13 + c * a23; return this; } translate( tx, ty ) { const te = this.elements; te[ 0 ] += tx * te[ 2 ]; te[ 3 ] += tx * te[ 5 ]; te[ 6 ] += tx * te[ 8 ]; te[ 1 ] += ty * te[ 2 ]; te[ 4 ] += ty * te[ 5 ]; te[ 7 ] += ty * te[ 8 ]; return this; } equals( matrix ) { const te = this.elements; const me = matrix.elements; for ( let i = 0; i < 9; i ++ ) { if ( te[ i ] !== me[ i ] ) return false; } return true; } fromArray( array, offset = 0 ) { for ( let i = 0; i < 9; i ++ ) { this.elements[ i ] = array[ i + offset ]; } return this; } toArray( array = [], offset = 0 ) { const te = this.elements; array[ offset ] = te[ 0 ]; array[ offset + 1 ] = te[ 1 ]; array[ offset + 2 ] = te[ 2 ]; array[ offset + 3 ] = te[ 3 ]; array[ offset + 4 ] = te[ 4 ]; array[ offset + 5 ] = te[ 5 ]; array[ offset + 6 ] = te[ 6 ]; array[ offset + 7 ] = te[ 7 ]; array[ offset + 8 ] = te[ 8 ]; return array; } } let _canvas; const ImageUtils = { getDataURL: function ( image ) { if ( /^data:/i.test( image.src ) ) { return image.src; } if ( typeof HTMLCanvasElement == 'undefined' ) { return image.src; } let canvas; if ( image instanceof HTMLCanvasElement ) { canvas = image; } else { if ( _canvas === undefined ) _canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ); _canvas.width = image.width; _canvas.height = image.height; const context = _canvas.getContext( '2d' ); if ( image instanceof ImageData ) { context.putImageData( image, 0, 0 ); } else { context.drawImage( image, 0, 0, image.width, image.height ); } canvas = _canvas; } if ( canvas.width > 2048 || canvas.height > 2048 ) { return canvas.toDataURL( 'image/jpeg', 0.6 ); } else { return canvas.toDataURL( 'image/png' ); } } }; let textureId = 0; function Texture( image = Texture.DEFAULT_IMAGE, mapping = Texture.DEFAULT_MAPPING, wrapS = ClampToEdgeWrapping, wrapT = ClampToEdgeWrapping, magFilter = LinearFilter, minFilter = LinearMipmapLinearFilter, format = RGBAFormat, type = UnsignedByteType, anisotropy = 1, encoding = LinearEncoding ) { Object.defineProperty( this, 'id', { value: textureId ++ } ); this.uuid = MathUtils.generateUUID(); this.name = ''; this.image = image; this.mipmaps = []; this.mapping = mapping; this.wrapS = wrapS; this.wrapT = wrapT; this.magFilter = magFilter; this.minFilter = minFilter; this.anisotropy = anisotropy; this.format = format; this.internalFormat = null; this.type = type; this.offset = new Vector2( 0, 0 ); this.repeat = new Vector2( 1, 1 ); this.center = new Vector2( 0, 0 ); this.rotation = 0; this.matrixAutoUpdate = true; this.matrix = new Matrix3(); this.generateMipmaps = true; this.premultiplyAlpha = false; this.flipY = true; this.unpackAlignment = 4; // valid values: 1, 2, 4, 8 (see http://www.khronos.org/opengles/sdk/docs/man/xhtml/glPixelStorei.xml) // Values of encoding !== THREE.LinearEncoding only supported on map, envMap and emissiveMap. // // Also changing the encoding after already used by a Material will not automatically make the Material // update. You need to explicitly call Material.needsUpdate to trigger it to recompile. this.encoding = encoding; this.version = 0; this.onUpdate = null; } Texture.DEFAULT_IMAGE = undefined; Texture.DEFAULT_MAPPING = UVMapping; Texture.prototype = Object.assign( Object.create( EventDispatcher.prototype ), { constructor: Texture, isTexture: true, updateMatrix: function () { this.matrix.setUvTransform( this.offset.x, this.offset.y, this.repeat.x, this.repeat.y, this.rotation, this.center.x, this.center.y ); }, clone: function () { return new this.constructor().copy( this ); }, copy: function ( source ) { this.name = source.name; this.image = source.image; this.mipmaps = source.mipmaps.slice( 0 ); this.mapping = source.mapping; this.wrapS = source.wrapS; this.wrapT = source.wrapT; this.magFilter = source.magFilter; this.minFilter = source.minFilter; this.anisotropy = source.anisotropy; this.format = source.format; this.internalFormat = source.internalFormat; this.type = source.type; this.offset.copy( source.offset ); this.repeat.copy( source.repeat ); this.center.copy( source.center ); this.rotation = source.rotation; this.matrixAutoUpdate = source.matrixAutoUpdate; this.matrix.copy( source.matrix ); this.generateMipmaps = source.generateMipmaps; this.premultiplyAlpha = source.premultiplyAlpha; this.flipY = source.flipY; this.unpackAlignment = source.unpackAlignment; this.encoding = source.encoding; return this; }, toJSON: function ( meta ) { const isRootObject = ( meta === undefined || typeof meta === 'string' ); if ( ! isRootObject && meta.textures[ this.uuid ] !== undefined ) { return meta.textures[ this.uuid ]; } const output = { metadata: { version: 4.5, type: 'Texture', generator: 'Texture.toJSON' }, uuid: this.uuid, name: this.name, mapping: this.mapping, repeat: [ this.repeat.x, this.repeat.y ], offset: [ this.offset.x, this.offset.y ], center: [ this.center.x, this.center.y ], rotation: this.rotation, wrap: [ this.wrapS, this.wrapT ], format: this.format, type: this.type, encoding: this.encoding, minFilter: this.minFilter, magFilter: this.magFilter, anisotropy: this.anisotropy, flipY: this.flipY, premultiplyAlpha: this.premultiplyAlpha, unpackAlignment: this.unpackAlignment }; if ( this.image !== undefined ) { // TODO: Move to THREE.Image const image = this.image; if ( image.uuid === undefined ) { image.uuid = MathUtils.generateUUID(); // UGH } if ( ! isRootObject && meta.images[ image.uuid ] === undefined ) { let url; if ( Array.isArray( image ) ) { // process array of images e.g. CubeTexture url = []; for ( let i = 0, l = image.length; i < l; i ++ ) { // check cube texture with data textures if ( image[ i ].isDataTexture ) { url.push( serializeImage( image[ i ].image ) ); } else { url.push( serializeImage( image[ i ] ) ); } } } else { // process single image url = serializeImage( image ); } meta.images[ image.uuid ] = { uuid: image.uuid, url: url }; } output.image = image.uuid; } if ( ! isRootObject ) { meta.textures[ this.uuid ] = output; } return output; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); }, transformUv: function ( uv ) { if ( this.mapping !== UVMapping ) return uv; uv.applyMatrix3( this.matrix ); if ( uv.x < 0 || uv.x > 1 ) { switch ( this.wrapS ) { case RepeatWrapping: uv.x = uv.x - Math.floor( uv.x ); break; case ClampToEdgeWrapping: uv.x = uv.x < 0 ? 0 : 1; break; case MirroredRepeatWrapping: if ( Math.abs( Math.floor( uv.x ) % 2 ) === 1 ) { uv.x = Math.ceil( uv.x ) - uv.x; } else { uv.x = uv.x - Math.floor( uv.x ); } break; } } if ( uv.y < 0 || uv.y > 1 ) { switch ( this.wrapT ) { case RepeatWrapping: uv.y = uv.y - Math.floor( uv.y ); break; case ClampToEdgeWrapping: uv.y = uv.y < 0 ? 0 : 1; break; case MirroredRepeatWrapping: if ( Math.abs( Math.floor( uv.y ) % 2 ) === 1 ) { uv.y = Math.ceil( uv.y ) - uv.y; } else { uv.y = uv.y - Math.floor( uv.y ); } break; } } if ( this.flipY ) { uv.y = 1 - uv.y; } return uv; } } ); Object.defineProperty( Texture.prototype, 'needsUpdate', { set: function ( value ) { if ( value === true ) this.version ++; } } ); function serializeImage( image ) { if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) || ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) { // default images return ImageUtils.getDataURL( image ); } else { if ( image.data ) { // images of DataTexture return { data: Array.prototype.slice.call( image.data ), width: image.width, height: image.height, type: image.data.constructor.name }; } else { console.warn( 'THREE.Texture: Unable to serialize Texture.' ); return {}; } } } class Vector4 { constructor( x = 0, y = 0, z = 0, w = 1 ) { Object.defineProperty( this, 'isVector4', { value: true } ); this.x = x; this.y = y; this.z = z; this.w = w; } get width() { return this.z; } set width( value ) { this.z = value; } get height() { return this.w; } set height( value ) { this.w = value; } set( x, y, z, w ) { this.x = x; this.y = y; this.z = z; this.w = w; return this; } setScalar( scalar ) { this.x = scalar; this.y = scalar; this.z = scalar; this.w = scalar; return this; } setX( x ) { this.x = x; return this; } setY( y ) { this.y = y; return this; } setZ( z ) { this.z = z; return this; } setW( w ) { this.w = w; return this; } setComponent( index, value ) { switch ( index ) { case 0: this.x = value; break; case 1: this.y = value; break; case 2: this.z = value; break; case 3: this.w = value; break; default: throw new Error( 'index is out of range: ' + index ); } return this; } getComponent( index ) { switch ( index ) { case 0: return this.x; case 1: return this.y; case 2: return this.z; case 3: return this.w; default: throw new Error( 'index is out of range: ' + index ); } } clone() { return new this.constructor( this.x, this.y, this.z, this.w ); } copy( v ) { this.x = v.x; this.y = v.y; this.z = v.z; this.w = ( v.w !== undefined ) ? v.w : 1; return this; } add( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector4: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' ); return this.addVectors( v, w ); } this.x += v.x; this.y += v.y; this.z += v.z; this.w += v.w; return this; } addScalar( s ) { this.x += s; this.y += s; this.z += s; this.w += s; return this; } addVectors( a, b ) { this.x = a.x + b.x; this.y = a.y + b.y; this.z = a.z + b.z; this.w = a.w + b.w; return this; } addScaledVector( v, s ) { this.x += v.x * s; this.y += v.y * s; this.z += v.z * s; this.w += v.w * s; return this; } sub( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector4: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' ); return this.subVectors( v, w ); } this.x -= v.x; this.y -= v.y; this.z -= v.z; this.w -= v.w; return this; } subScalar( s ) { this.x -= s; this.y -= s; this.z -= s; this.w -= s; return this; } subVectors( a, b ) { this.x = a.x - b.x; this.y = a.y - b.y; this.z = a.z - b.z; this.w = a.w - b.w; return this; } multiplyScalar( scalar ) { this.x *= scalar; this.y *= scalar; this.z *= scalar; this.w *= scalar; return this; } applyMatrix4( m ) { const x = this.x, y = this.y, z = this.z, w = this.w; const e = m.elements; this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] * w; this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] * w; this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] * w; this.w = e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] * w; return this; } divideScalar( scalar ) { return this.multiplyScalar( 1 / scalar ); } setAxisAngleFromQuaternion( q ) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToAngle/index.htm // q is assumed to be normalized this.w = 2 * Math.acos( q.w ); const s = Math.sqrt( 1 - q.w * q.w ); if ( s < 0.0001 ) { this.x = 1; this.y = 0; this.z = 0; } else { this.x = q.x / s; this.y = q.y / s; this.z = q.z / s; } return this; } setAxisAngleFromRotationMatrix( m ) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToAngle/index.htm // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) let angle, x, y, z; // variables for result const epsilon = 0.01, // margin to allow for rounding errors epsilon2 = 0.1, // margin to distinguish between 0 and 180 degrees te = m.elements, m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ], m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ], m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ]; if ( ( Math.abs( m12 - m21 ) < epsilon ) && ( Math.abs( m13 - m31 ) < epsilon ) && ( Math.abs( m23 - m32 ) < epsilon ) ) { // singularity found // first check for identity matrix which must have +1 for all terms // in leading diagonal and zero in other terms if ( ( Math.abs( m12 + m21 ) < epsilon2 ) && ( Math.abs( m13 + m31 ) < epsilon2 ) && ( Math.abs( m23 + m32 ) < epsilon2 ) && ( Math.abs( m11 + m22 + m33 - 3 ) < epsilon2 ) ) { // this singularity is identity matrix so angle = 0 this.set( 1, 0, 0, 0 ); return this; // zero angle, arbitrary axis } // otherwise this singularity is angle = 180 angle = Math.PI; const xx = ( m11 + 1 ) / 2; const yy = ( m22 + 1 ) / 2; const zz = ( m33 + 1 ) / 2; const xy = ( m12 + m21 ) / 4; const xz = ( m13 + m31 ) / 4; const yz = ( m23 + m32 ) / 4; if ( ( xx > yy ) && ( xx > zz ) ) { // m11 is the largest diagonal term if ( xx < epsilon ) { x = 0; y = 0.707106781; z = 0.707106781; } else { x = Math.sqrt( xx ); y = xy / x; z = xz / x; } } else if ( yy > zz ) { // m22 is the largest diagonal term if ( yy < epsilon ) { x = 0.707106781; y = 0; z = 0.707106781; } else { y = Math.sqrt( yy ); x = xy / y; z = yz / y; } } else { // m33 is the largest diagonal term so base result on this if ( zz < epsilon ) { x = 0.707106781; y = 0.707106781; z = 0; } else { z = Math.sqrt( zz ); x = xz / z; y = yz / z; } } this.set( x, y, z, angle ); return this; // return 180 deg rotation } // as we have reached here there are no singularities so we can handle normally let s = Math.sqrt( ( m32 - m23 ) * ( m32 - m23 ) + ( m13 - m31 ) * ( m13 - m31 ) + ( m21 - m12 ) * ( m21 - m12 ) ); // used to normalize if ( Math.abs( s ) < 0.001 ) s = 1; // prevent divide by zero, should not happen if matrix is orthogonal and should be // caught by singularity test above, but I've left it in just in case this.x = ( m32 - m23 ) / s; this.y = ( m13 - m31 ) / s; this.z = ( m21 - m12 ) / s; this.w = Math.acos( ( m11 + m22 + m33 - 1 ) / 2 ); return this; } min( v ) { this.x = Math.min( this.x, v.x ); this.y = Math.min( this.y, v.y ); this.z = Math.min( this.z, v.z ); this.w = Math.min( this.w, v.w ); return this; } max( v ) { this.x = Math.max( this.x, v.x ); this.y = Math.max( this.y, v.y ); this.z = Math.max( this.z, v.z ); this.w = Math.max( this.w, v.w ); return this; } clamp( min, max ) { // assumes min < max, componentwise this.x = Math.max( min.x, Math.min( max.x, this.x ) ); this.y = Math.max( min.y, Math.min( max.y, this.y ) ); this.z = Math.max( min.z, Math.min( max.z, this.z ) ); this.w = Math.max( min.w, Math.min( max.w, this.w ) ); return this; } clampScalar( minVal, maxVal ) { this.x = Math.max( minVal, Math.min( maxVal, this.x ) ); this.y = Math.max( minVal, Math.min( maxVal, this.y ) ); this.z = Math.max( minVal, Math.min( maxVal, this.z ) ); this.w = Math.max( minVal, Math.min( maxVal, this.w ) ); return this; } clampLength( min, max ) { const length = this.length(); return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) ); } floor() { this.x = Math.floor( this.x ); this.y = Math.floor( this.y ); this.z = Math.floor( this.z ); this.w = Math.floor( this.w ); return this; } ceil() { this.x = Math.ceil( this.x ); this.y = Math.ceil( this.y ); this.z = Math.ceil( this.z ); this.w = Math.ceil( this.w ); return this; } round() { this.x = Math.round( this.x ); this.y = Math.round( this.y ); this.z = Math.round( this.z ); this.w = Math.round( this.w ); return this; } roundToZero() { this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x ); this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y ); this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z ); this.w = ( this.w < 0 ) ? Math.ceil( this.w ) : Math.floor( this.w ); return this; } negate() { this.x = - this.x; this.y = - this.y; this.z = - this.z; this.w = - this.w; return this; } dot( v ) { return this.x * v.x + this.y * v.y + this.z * v.z + this.w * v.w; } lengthSq() { return this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w; } length() { return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w ); } manhattanLength() { return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ) + Math.abs( this.w ); } normalize() { return this.divideScalar( this.length() || 1 ); } setLength( length ) { return this.normalize().multiplyScalar( length ); } lerp( v, alpha ) { this.x += ( v.x - this.x ) * alpha; this.y += ( v.y - this.y ) * alpha; this.z += ( v.z - this.z ) * alpha; this.w += ( v.w - this.w ) * alpha; return this; } lerpVectors( v1, v2, alpha ) { this.x = v1.x + ( v2.x - v1.x ) * alpha; this.y = v1.y + ( v2.y - v1.y ) * alpha; this.z = v1.z + ( v2.z - v1.z ) * alpha; this.w = v1.w + ( v2.w - v1.w ) * alpha; return this; } equals( v ) { return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) && ( v.w === this.w ) ); } fromArray( array, offset = 0 ) { this.x = array[ offset ]; this.y = array[ offset + 1 ]; this.z = array[ offset + 2 ]; this.w = array[ offset + 3 ]; return this; } toArray( array = [], offset = 0 ) { array[ offset ] = this.x; array[ offset + 1 ] = this.y; array[ offset + 2 ] = this.z; array[ offset + 3 ] = this.w; return array; } fromBufferAttribute( attribute, index, offset ) { if ( offset !== undefined ) { console.warn( 'THREE.Vector4: offset has been removed from .fromBufferAttribute().' ); } this.x = attribute.getX( index ); this.y = attribute.getY( index ); this.z = attribute.getZ( index ); this.w = attribute.getW( index ); return this; } random() { this.x = Math.random(); this.y = Math.random(); this.z = Math.random(); this.w = Math.random(); return this; } } /* In options, we can specify: * Texture parameters for an auto-generated target texture * depthBuffer/stencilBuffer: Booleans to indicate if we should generate these buffers */ function WebGLRenderTarget( width, height, options ) { this.width = width; this.height = height; this.scissor = new Vector4( 0, 0, width, height ); this.scissorTest = false; this.viewport = new Vector4( 0, 0, width, height ); options = options || {}; this.texture = new Texture( undefined, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.encoding ); this.texture.image = {}; this.texture.image.width = width; this.texture.image.height = height; this.texture.generateMipmaps = options.generateMipmaps !== undefined ? options.generateMipmaps : false; this.texture.minFilter = options.minFilter !== undefined ? options.minFilter : LinearFilter; this.depthBuffer = options.depthBuffer !== undefined ? options.depthBuffer : true; this.stencilBuffer = options.stencilBuffer !== undefined ? options.stencilBuffer : false; this.depthTexture = options.depthTexture !== undefined ? options.depthTexture : null; } WebGLRenderTarget.prototype = Object.assign( Object.create( EventDispatcher.prototype ), { constructor: WebGLRenderTarget, isWebGLRenderTarget: true, setSize: function ( width, height ) { if ( this.width !== width || this.height !== height ) { this.width = width; this.height = height; this.texture.image.width = width; this.texture.image.height = height; this.dispose(); } this.viewport.set( 0, 0, width, height ); this.scissor.set( 0, 0, width, height ); }, clone: function () { return new this.constructor().copy( this ); }, copy: function ( source ) { this.width = source.width; this.height = source.height; this.viewport.copy( source.viewport ); this.texture = source.texture.clone(); this.depthBuffer = source.depthBuffer; this.stencilBuffer = source.stencilBuffer; this.depthTexture = source.depthTexture; return this; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } } ); function WebGLMultisampleRenderTarget( width, height, options ) { WebGLRenderTarget.call( this, width, height, options ); this.samples = 4; } WebGLMultisampleRenderTarget.prototype = Object.assign( Object.create( WebGLRenderTarget.prototype ), { constructor: WebGLMultisampleRenderTarget, isWebGLMultisampleRenderTarget: true, copy: function ( source ) { WebGLRenderTarget.prototype.copy.call( this, source ); this.samples = source.samples; return this; } } ); class Quaternion { constructor( x = 0, y = 0, z = 0, w = 1 ) { Object.defineProperty( this, 'isQuaternion', { value: true } ); this._x = x; this._y = y; this._z = z; this._w = w; } static slerp( qa, qb, qm, t ) { return qm.copy( qa ).slerp( qb, t ); } static slerpFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1, t ) { // fuzz-free, array-based Quaternion SLERP operation let x0 = src0[ srcOffset0 + 0 ], y0 = src0[ srcOffset0 + 1 ], z0 = src0[ srcOffset0 + 2 ], w0 = src0[ srcOffset0 + 3 ]; const x1 = src1[ srcOffset1 + 0 ], y1 = src1[ srcOffset1 + 1 ], z1 = src1[ srcOffset1 + 2 ], w1 = src1[ srcOffset1 + 3 ]; if ( w0 !== w1 || x0 !== x1 || y0 !== y1 || z0 !== z1 ) { let s = 1 - t; const cos = x0 * x1 + y0 * y1 + z0 * z1 + w0 * w1, dir = ( cos >= 0 ? 1 : - 1 ), sqrSin = 1 - cos * cos; // Skip the Slerp for tiny steps to avoid numeric problems: if ( sqrSin > Number.EPSILON ) { const sin = Math.sqrt( sqrSin ), len = Math.atan2( sin, cos * dir ); s = Math.sin( s * len ) / sin; t = Math.sin( t * len ) / sin; } const tDir = t * dir; x0 = x0 * s + x1 * tDir; y0 = y0 * s + y1 * tDir; z0 = z0 * s + z1 * tDir; w0 = w0 * s + w1 * tDir; // Normalize in case we just did a lerp: if ( s === 1 - t ) { const f = 1 / Math.sqrt( x0 * x0 + y0 * y0 + z0 * z0 + w0 * w0 ); x0 *= f; y0 *= f; z0 *= f; w0 *= f; } } dst[ dstOffset ] = x0; dst[ dstOffset + 1 ] = y0; dst[ dstOffset + 2 ] = z0; dst[ dstOffset + 3 ] = w0; } static multiplyQuaternionsFlat( dst, dstOffset, src0, srcOffset0, src1, srcOffset1 ) { const x0 = src0[ srcOffset0 ]; const y0 = src0[ srcOffset0 + 1 ]; const z0 = src0[ srcOffset0 + 2 ]; const w0 = src0[ srcOffset0 + 3 ]; const x1 = src1[ srcOffset1 ]; const y1 = src1[ srcOffset1 + 1 ]; const z1 = src1[ srcOffset1 + 2 ]; const w1 = src1[ srcOffset1 + 3 ]; dst[ dstOffset ] = x0 * w1 + w0 * x1 + y0 * z1 - z0 * y1; dst[ dstOffset + 1 ] = y0 * w1 + w0 * y1 + z0 * x1 - x0 * z1; dst[ dstOffset + 2 ] = z0 * w1 + w0 * z1 + x0 * y1 - y0 * x1; dst[ dstOffset + 3 ] = w0 * w1 - x0 * x1 - y0 * y1 - z0 * z1; return dst; } get x() { return this._x; } set x( value ) { this._x = value; this._onChangeCallback(); } get y() { return this._y; } set y( value ) { this._y = value; this._onChangeCallback(); } get z() { return this._z; } set z( value ) { this._z = value; this._onChangeCallback(); } get w() { return this._w; } set w( value ) { this._w = value; this._onChangeCallback(); } set( x, y, z, w ) { this._x = x; this._y = y; this._z = z; this._w = w; this._onChangeCallback(); return this; } clone() { return new this.constructor( this._x, this._y, this._z, this._w ); } copy( quaternion ) { this._x = quaternion.x; this._y = quaternion.y; this._z = quaternion.z; this._w = quaternion.w; this._onChangeCallback(); return this; } setFromEuler( euler, update ) { if ( ! ( euler && euler.isEuler ) ) { throw new Error( 'THREE.Quaternion: .setFromEuler() now expects an Euler rotation rather than a Vector3 and order.' ); } const x = euler._x, y = euler._y, z = euler._z, order = euler._order; // http://www.mathworks.com/matlabcentral/fileexchange/ // 20696-function-to-convert-between-dcm-euler-angles-quaternions-and-euler-vectors/ // content/SpinCalc.m const cos = Math.cos; const sin = Math.sin; const c1 = cos( x / 2 ); const c2 = cos( y / 2 ); const c3 = cos( z / 2 ); const s1 = sin( x / 2 ); const s2 = sin( y / 2 ); const s3 = sin( z / 2 ); switch ( order ) { case 'XYZ': this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; break; case 'YXZ': this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; break; case 'ZXY': this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; break; case 'ZYX': this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; break; case 'YZX': this._x = s1 * c2 * c3 + c1 * s2 * s3; this._y = c1 * s2 * c3 + s1 * c2 * s3; this._z = c1 * c2 * s3 - s1 * s2 * c3; this._w = c1 * c2 * c3 - s1 * s2 * s3; break; case 'XZY': this._x = s1 * c2 * c3 - c1 * s2 * s3; this._y = c1 * s2 * c3 - s1 * c2 * s3; this._z = c1 * c2 * s3 + s1 * s2 * c3; this._w = c1 * c2 * c3 + s1 * s2 * s3; break; default: console.warn( 'THREE.Quaternion: .setFromEuler() encountered an unknown order: ' + order ); } if ( update !== false ) this._onChangeCallback(); return this; } setFromAxisAngle( axis, angle ) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/angleToQuaternion/index.htm // assumes axis is normalized const halfAngle = angle / 2, s = Math.sin( halfAngle ); this._x = axis.x * s; this._y = axis.y * s; this._z = axis.z * s; this._w = Math.cos( halfAngle ); this._onChangeCallback(); return this; } setFromRotationMatrix( m ) { // http://www.euclideanspace.com/maths/geometry/rotations/conversions/matrixToQuaternion/index.htm // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) const te = m.elements, m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ], m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ], m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ], trace = m11 + m22 + m33; if ( trace > 0 ) { const s = 0.5 / Math.sqrt( trace + 1.0 ); this._w = 0.25 / s; this._x = ( m32 - m23 ) * s; this._y = ( m13 - m31 ) * s; this._z = ( m21 - m12 ) * s; } else if ( m11 > m22 && m11 > m33 ) { const s = 2.0 * Math.sqrt( 1.0 + m11 - m22 - m33 ); this._w = ( m32 - m23 ) / s; this._x = 0.25 * s; this._y = ( m12 + m21 ) / s; this._z = ( m13 + m31 ) / s; } else if ( m22 > m33 ) { const s = 2.0 * Math.sqrt( 1.0 + m22 - m11 - m33 ); this._w = ( m13 - m31 ) / s; this._x = ( m12 + m21 ) / s; this._y = 0.25 * s; this._z = ( m23 + m32 ) / s; } else { const s = 2.0 * Math.sqrt( 1.0 + m33 - m11 - m22 ); this._w = ( m21 - m12 ) / s; this._x = ( m13 + m31 ) / s; this._y = ( m23 + m32 ) / s; this._z = 0.25 * s; } this._onChangeCallback(); return this; } setFromUnitVectors( vFrom, vTo ) { // assumes direction vectors vFrom and vTo are normalized const EPS = 0.000001; let r = vFrom.dot( vTo ) + 1; if ( r < EPS ) { r = 0; if ( Math.abs( vFrom.x ) > Math.abs( vFrom.z ) ) { this._x = - vFrom.y; this._y = vFrom.x; this._z = 0; this._w = r; } else { this._x = 0; this._y = - vFrom.z; this._z = vFrom.y; this._w = r; } } else { // crossVectors( vFrom, vTo ); // inlined to avoid cyclic dependency on Vector3 this._x = vFrom.y * vTo.z - vFrom.z * vTo.y; this._y = vFrom.z * vTo.x - vFrom.x * vTo.z; this._z = vFrom.x * vTo.y - vFrom.y * vTo.x; this._w = r; } return this.normalize(); } angleTo( q ) { return 2 * Math.acos( Math.abs( MathUtils.clamp( this.dot( q ), - 1, 1 ) ) ); } rotateTowards( q, step ) { const angle = this.angleTo( q ); if ( angle === 0 ) return this; const t = Math.min( 1, step / angle ); this.slerp( q, t ); return this; } identity() { return this.set( 0, 0, 0, 1 ); } invert() { // quaternion is assumed to have unit length return this.conjugate(); } conjugate() { this._x *= - 1; this._y *= - 1; this._z *= - 1; this._onChangeCallback(); return this; } dot( v ) { return this._x * v._x + this._y * v._y + this._z * v._z + this._w * v._w; } lengthSq() { return this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w; } length() { return Math.sqrt( this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w ); } normalize() { let l = this.length(); if ( l === 0 ) { this._x = 0; this._y = 0; this._z = 0; this._w = 1; } else { l = 1 / l; this._x = this._x * l; this._y = this._y * l; this._z = this._z * l; this._w = this._w * l; } this._onChangeCallback(); return this; } multiply( q, p ) { if ( p !== undefined ) { console.warn( 'THREE.Quaternion: .multiply() now only accepts one argument. Use .multiplyQuaternions( a, b ) instead.' ); return this.multiplyQuaternions( q, p ); } return this.multiplyQuaternions( this, q ); } premultiply( q ) { return this.multiplyQuaternions( q, this ); } multiplyQuaternions( a, b ) { // from http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/code/index.htm const qax = a._x, qay = a._y, qaz = a._z, qaw = a._w; const qbx = b._x, qby = b._y, qbz = b._z, qbw = b._w; this._x = qax * qbw + qaw * qbx + qay * qbz - qaz * qby; this._y = qay * qbw + qaw * qby + qaz * qbx - qax * qbz; this._z = qaz * qbw + qaw * qbz + qax * qby - qay * qbx; this._w = qaw * qbw - qax * qbx - qay * qby - qaz * qbz; this._onChangeCallback(); return this; } slerp( qb, t ) { if ( t === 0 ) return this; if ( t === 1 ) return this.copy( qb ); const x = this._x, y = this._y, z = this._z, w = this._w; // http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/ let cosHalfTheta = w * qb._w + x * qb._x + y * qb._y + z * qb._z; if ( cosHalfTheta < 0 ) { this._w = - qb._w; this._x = - qb._x; this._y = - qb._y; this._z = - qb._z; cosHalfTheta = - cosHalfTheta; } else { this.copy( qb ); } if ( cosHalfTheta >= 1.0 ) { this._w = w; this._x = x; this._y = y; this._z = z; return this; } const sqrSinHalfTheta = 1.0 - cosHalfTheta * cosHalfTheta; if ( sqrSinHalfTheta <= Number.EPSILON ) { const s = 1 - t; this._w = s * w + t * this._w; this._x = s * x + t * this._x; this._y = s * y + t * this._y; this._z = s * z + t * this._z; this.normalize(); this._onChangeCallback(); return this; } const sinHalfTheta = Math.sqrt( sqrSinHalfTheta ); const halfTheta = Math.atan2( sinHalfTheta, cosHalfTheta ); const ratioA = Math.sin( ( 1 - t ) * halfTheta ) / sinHalfTheta, ratioB = Math.sin( t * halfTheta ) / sinHalfTheta; this._w = ( w * ratioA + this._w * ratioB ); this._x = ( x * ratioA + this._x * ratioB ); this._y = ( y * ratioA + this._y * ratioB ); this._z = ( z * ratioA + this._z * ratioB ); this._onChangeCallback(); return this; } equals( quaternion ) { return ( quaternion._x === this._x ) && ( quaternion._y === this._y ) && ( quaternion._z === this._z ) && ( quaternion._w === this._w ); } fromArray( array, offset = 0 ) { this._x = array[ offset ]; this._y = array[ offset + 1 ]; this._z = array[ offset + 2 ]; this._w = array[ offset + 3 ]; this._onChangeCallback(); return this; } toArray( array = [], offset = 0 ) { array[ offset ] = this._x; array[ offset + 1 ] = this._y; array[ offset + 2 ] = this._z; array[ offset + 3 ] = this._w; return array; } fromBufferAttribute( attribute, index ) { this._x = attribute.getX( index ); this._y = attribute.getY( index ); this._z = attribute.getZ( index ); this._w = attribute.getW( index ); return this; } _onChange( callback ) { this._onChangeCallback = callback; return this; } _onChangeCallback() {} } class Vector3 { constructor( x = 0, y = 0, z = 0 ) { Object.defineProperty( this, 'isVector3', { value: true } ); this.x = x; this.y = y; this.z = z; } set( x, y, z ) { if ( z === undefined ) z = this.z; // sprite.scale.set(x,y) this.x = x; this.y = y; this.z = z; return this; } setScalar( scalar ) { this.x = scalar; this.y = scalar; this.z = scalar; return this; } setX( x ) { this.x = x; return this; } setY( y ) { this.y = y; return this; } setZ( z ) { this.z = z; return this; } setComponent( index, value ) { switch ( index ) { case 0: this.x = value; break; case 1: this.y = value; break; case 2: this.z = value; break; default: throw new Error( 'index is out of range: ' + index ); } return this; } getComponent( index ) { switch ( index ) { case 0: return this.x; case 1: return this.y; case 2: return this.z; default: throw new Error( 'index is out of range: ' + index ); } } clone() { return new this.constructor( this.x, this.y, this.z ); } copy( v ) { this.x = v.x; this.y = v.y; this.z = v.z; return this; } add( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector3: .add() now only accepts one argument. Use .addVectors( a, b ) instead.' ); return this.addVectors( v, w ); } this.x += v.x; this.y += v.y; this.z += v.z; return this; } addScalar( s ) { this.x += s; this.y += s; this.z += s; return this; } addVectors( a, b ) { this.x = a.x + b.x; this.y = a.y + b.y; this.z = a.z + b.z; return this; } addScaledVector( v, s ) { this.x += v.x * s; this.y += v.y * s; this.z += v.z * s; return this; } sub( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector3: .sub() now only accepts one argument. Use .subVectors( a, b ) instead.' ); return this.subVectors( v, w ); } this.x -= v.x; this.y -= v.y; this.z -= v.z; return this; } subScalar( s ) { this.x -= s; this.y -= s; this.z -= s; return this; } subVectors( a, b ) { this.x = a.x - b.x; this.y = a.y - b.y; this.z = a.z - b.z; return this; } multiply( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector3: .multiply() now only accepts one argument. Use .multiplyVectors( a, b ) instead.' ); return this.multiplyVectors( v, w ); } this.x *= v.x; this.y *= v.y; this.z *= v.z; return this; } multiplyScalar( scalar ) { this.x *= scalar; this.y *= scalar; this.z *= scalar; return this; } multiplyVectors( a, b ) { this.x = a.x * b.x; this.y = a.y * b.y; this.z = a.z * b.z; return this; } applyEuler( euler ) { if ( ! ( euler && euler.isEuler ) ) { console.error( 'THREE.Vector3: .applyEuler() now expects an Euler rotation rather than a Vector3 and order.' ); } return this.applyQuaternion( _quaternion.setFromEuler( euler ) ); } applyAxisAngle( axis, angle ) { return this.applyQuaternion( _quaternion.setFromAxisAngle( axis, angle ) ); } applyMatrix3( m ) { const x = this.x, y = this.y, z = this.z; const e = m.elements; this.x = e[ 0 ] * x + e[ 3 ] * y + e[ 6 ] * z; this.y = e[ 1 ] * x + e[ 4 ] * y + e[ 7 ] * z; this.z = e[ 2 ] * x + e[ 5 ] * y + e[ 8 ] * z; return this; } applyNormalMatrix( m ) { return this.applyMatrix3( m ).normalize(); } applyMatrix4( m ) { const x = this.x, y = this.y, z = this.z; const e = m.elements; const w = 1 / ( e[ 3 ] * x + e[ 7 ] * y + e[ 11 ] * z + e[ 15 ] ); this.x = ( e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z + e[ 12 ] ) * w; this.y = ( e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z + e[ 13 ] ) * w; this.z = ( e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z + e[ 14 ] ) * w; return this; } applyQuaternion( q ) { const x = this.x, y = this.y, z = this.z; const qx = q.x, qy = q.y, qz = q.z, qw = q.w; // calculate quat * vector const ix = qw * x + qy * z - qz * y; const iy = qw * y + qz * x - qx * z; const iz = qw * z + qx * y - qy * x; const iw = - qx * x - qy * y - qz * z; // calculate result * inverse quat this.x = ix * qw + iw * - qx + iy * - qz - iz * - qy; this.y = iy * qw + iw * - qy + iz * - qx - ix * - qz; this.z = iz * qw + iw * - qz + ix * - qy - iy * - qx; return this; } project( camera ) { return this.applyMatrix4( camera.matrixWorldInverse ).applyMatrix4( camera.projectionMatrix ); } unproject( camera ) { return this.applyMatrix4( camera.projectionMatrixInverse ).applyMatrix4( camera.matrixWorld ); } transformDirection( m ) { // input: THREE.Matrix4 affine matrix // vector interpreted as a direction const x = this.x, y = this.y, z = this.z; const e = m.elements; this.x = e[ 0 ] * x + e[ 4 ] * y + e[ 8 ] * z; this.y = e[ 1 ] * x + e[ 5 ] * y + e[ 9 ] * z; this.z = e[ 2 ] * x + e[ 6 ] * y + e[ 10 ] * z; return this.normalize(); } divide( v ) { this.x /= v.x; this.y /= v.y; this.z /= v.z; return this; } divideScalar( scalar ) { return this.multiplyScalar( 1 / scalar ); } min( v ) { this.x = Math.min( this.x, v.x ); this.y = Math.min( this.y, v.y ); this.z = Math.min( this.z, v.z ); return this; } max( v ) { this.x = Math.max( this.x, v.x ); this.y = Math.max( this.y, v.y ); this.z = Math.max( this.z, v.z ); return this; } clamp( min, max ) { // assumes min < max, componentwise this.x = Math.max( min.x, Math.min( max.x, this.x ) ); this.y = Math.max( min.y, Math.min( max.y, this.y ) ); this.z = Math.max( min.z, Math.min( max.z, this.z ) ); return this; } clampScalar( minVal, maxVal ) { this.x = Math.max( minVal, Math.min( maxVal, this.x ) ); this.y = Math.max( minVal, Math.min( maxVal, this.y ) ); this.z = Math.max( minVal, Math.min( maxVal, this.z ) ); return this; } clampLength( min, max ) { const length = this.length(); return this.divideScalar( length || 1 ).multiplyScalar( Math.max( min, Math.min( max, length ) ) ); } floor() { this.x = Math.floor( this.x ); this.y = Math.floor( this.y ); this.z = Math.floor( this.z ); return this; } ceil() { this.x = Math.ceil( this.x ); this.y = Math.ceil( this.y ); this.z = Math.ceil( this.z ); return this; } round() { this.x = Math.round( this.x ); this.y = Math.round( this.y ); this.z = Math.round( this.z ); return this; } roundToZero() { this.x = ( this.x < 0 ) ? Math.ceil( this.x ) : Math.floor( this.x ); this.y = ( this.y < 0 ) ? Math.ceil( this.y ) : Math.floor( this.y ); this.z = ( this.z < 0 ) ? Math.ceil( this.z ) : Math.floor( this.z ); return this; } negate() { this.x = - this.x; this.y = - this.y; this.z = - this.z; return this; } dot( v ) { return this.x * v.x + this.y * v.y + this.z * v.z; } // TODO lengthSquared? lengthSq() { return this.x * this.x + this.y * this.y + this.z * this.z; } length() { return Math.sqrt( this.x * this.x + this.y * this.y + this.z * this.z ); } manhattanLength() { return Math.abs( this.x ) + Math.abs( this.y ) + Math.abs( this.z ); } normalize() { return this.divideScalar( this.length() || 1 ); } setLength( length ) { return this.normalize().multiplyScalar( length ); } lerp( v, alpha ) { this.x += ( v.x - this.x ) * alpha; this.y += ( v.y - this.y ) * alpha; this.z += ( v.z - this.z ) * alpha; return this; } lerpVectors( v1, v2, alpha ) { this.x = v1.x + ( v2.x - v1.x ) * alpha; this.y = v1.y + ( v2.y - v1.y ) * alpha; this.z = v1.z + ( v2.z - v1.z ) * alpha; return this; } cross( v, w ) { if ( w !== undefined ) { console.warn( 'THREE.Vector3: .cross() now only accepts one argument. Use .crossVectors( a, b ) instead.' ); return this.crossVectors( v, w ); } return this.crossVectors( this, v ); } crossVectors( a, b ) { const ax = a.x, ay = a.y, az = a.z; const bx = b.x, by = b.y, bz = b.z; this.x = ay * bz - az * by; this.y = az * bx - ax * bz; this.z = ax * by - ay * bx; return this; } projectOnVector( v ) { const denominator = v.lengthSq(); if ( denominator === 0 ) return this.set( 0, 0, 0 ); const scalar = v.dot( this ) / denominator; return this.copy( v ).multiplyScalar( scalar ); } projectOnPlane( planeNormal ) { _vector.copy( this ).projectOnVector( planeNormal ); return this.sub( _vector ); } reflect( normal ) { // reflect incident vector off plane orthogonal to normal // normal is assumed to have unit length return this.sub( _vector.copy( normal ).multiplyScalar( 2 * this.dot( normal ) ) ); } angleTo( v ) { const denominator = Math.sqrt( this.lengthSq() * v.lengthSq() ); if ( denominator === 0 ) return Math.PI / 2; const theta = this.dot( v ) / denominator; // clamp, to handle numerical problems return Math.acos( MathUtils.clamp( theta, - 1, 1 ) ); } distanceTo( v ) { return Math.sqrt( this.distanceToSquared( v ) ); } distanceToSquared( v ) { const dx = this.x - v.x, dy = this.y - v.y, dz = this.z - v.z; return dx * dx + dy * dy + dz * dz; } manhattanDistanceTo( v ) { return Math.abs( this.x - v.x ) + Math.abs( this.y - v.y ) + Math.abs( this.z - v.z ); } setFromSpherical( s ) { return this.setFromSphericalCoords( s.radius, s.phi, s.theta ); } setFromSphericalCoords( radius, phi, theta ) { const sinPhiRadius = Math.sin( phi ) * radius; this.x = sinPhiRadius * Math.sin( theta ); this.y = Math.cos( phi ) * radius; this.z = sinPhiRadius * Math.cos( theta ); return this; } setFromCylindrical( c ) { return this.setFromCylindricalCoords( c.radius, c.theta, c.y ); } setFromCylindricalCoords( radius, theta, y ) { this.x = radius * Math.sin( theta ); this.y = y; this.z = radius * Math.cos( theta ); return this; } setFromMatrixPosition( m ) { const e = m.elements; this.x = e[ 12 ]; this.y = e[ 13 ]; this.z = e[ 14 ]; return this; } setFromMatrixScale( m ) { const sx = this.setFromMatrixColumn( m, 0 ).length(); const sy = this.setFromMatrixColumn( m, 1 ).length(); const sz = this.setFromMatrixColumn( m, 2 ).length(); this.x = sx; this.y = sy; this.z = sz; return this; } setFromMatrixColumn( m, index ) { return this.fromArray( m.elements, index * 4 ); } setFromMatrix3Column( m, index ) { return this.fromArray( m.elements, index * 3 ); } equals( v ) { return ( ( v.x === this.x ) && ( v.y === this.y ) && ( v.z === this.z ) ); } fromArray( array, offset = 0 ) { this.x = array[ offset ]; this.y = array[ offset + 1 ]; this.z = array[ offset + 2 ]; return this; } toArray( array = [], offset = 0 ) { array[ offset ] = this.x; array[ offset + 1 ] = this.y; array[ offset + 2 ] = this.z; return array; } fromBufferAttribute( attribute, index, offset ) { if ( offset !== undefined ) { console.warn( 'THREE.Vector3: offset has been removed from .fromBufferAttribute().' ); } this.x = attribute.getX( index ); this.y = attribute.getY( index ); this.z = attribute.getZ( index ); return this; } random() { this.x = Math.random(); this.y = Math.random(); this.z = Math.random(); return this; } } const _vector = /*@__PURE__*/ new Vector3(); const _quaternion = /*@__PURE__*/ new Quaternion(); class Box3 { constructor( min, max ) { Object.defineProperty( this, 'isBox3', { value: true } ); this.min = ( min !== undefined ) ? min : new Vector3( + Infinity, + Infinity, + Infinity ); this.max = ( max !== undefined ) ? max : new Vector3( - Infinity, - Infinity, - Infinity ); } set( min, max ) { this.min.copy( min ); this.max.copy( max ); return this; } setFromArray( array ) { let minX = + Infinity; let minY = + Infinity; let minZ = + Infinity; let maxX = - Infinity; let maxY = - Infinity; let maxZ = - Infinity; for ( let i = 0, l = array.length; i < l; i += 3 ) { const x = array[ i ]; const y = array[ i + 1 ]; const z = array[ i + 2 ]; if ( x < minX ) minX = x; if ( y < minY ) minY = y; if ( z < minZ ) minZ = z; if ( x > maxX ) maxX = x; if ( y > maxY ) maxY = y; if ( z > maxZ ) maxZ = z; } this.min.set( minX, minY, minZ ); this.max.set( maxX, maxY, maxZ ); return this; } setFromBufferAttribute( attribute ) { let minX = + Infinity; let minY = + Infinity; let minZ = + Infinity; let maxX = - Infinity; let maxY = - Infinity; let maxZ = - Infinity; for ( let i = 0, l = attribute.count; i < l; i ++ ) { const x = attribute.getX( i ); const y = attribute.getY( i ); const z = attribute.getZ( i ); if ( x < minX ) minX = x; if ( y < minY ) minY = y; if ( z < minZ ) minZ = z; if ( x > maxX ) maxX = x; if ( y > maxY ) maxY = y; if ( z > maxZ ) maxZ = z; } this.min.set( minX, minY, minZ ); this.max.set( maxX, maxY, maxZ ); return this; } setFromPoints( points ) { this.makeEmpty(); for ( let i = 0, il = points.length; i < il; i ++ ) { this.expandByPoint( points[ i ] ); } return this; } setFromCenterAndSize( center, size ) { const halfSize = _vector$1.copy( size ).multiplyScalar( 0.5 ); this.min.copy( center ).sub( halfSize ); this.max.copy( center ).add( halfSize ); return this; } setFromObject( object ) { this.makeEmpty(); return this.expandByObject( object ); } clone() { return new this.constructor().copy( this ); } copy( box ) { this.min.copy( box.min ); this.max.copy( box.max ); return this; } makeEmpty() { this.min.x = this.min.y = this.min.z = + Infinity; this.max.x = this.max.y = this.max.z = - Infinity; return this; } isEmpty() { // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ) || ( this.max.z < this.min.z ); } getCenter( target ) { if ( target === undefined ) { console.warn( 'THREE.Box3: .getCenter() target is now required' ); target = new Vector3(); } return this.isEmpty() ? target.set( 0, 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 ); } getSize( target ) { if ( target === undefined ) { console.warn( 'THREE.Box3: .getSize() target is now required' ); target = new Vector3(); } return this.isEmpty() ? target.set( 0, 0, 0 ) : target.subVectors( this.max, this.min ); } expandByPoint( point ) { this.min.min( point ); this.max.max( point ); return this; } expandByVector( vector ) { this.min.sub( vector ); this.max.add( vector ); return this; } expandByScalar( scalar ) { this.min.addScalar( - scalar ); this.max.addScalar( scalar ); return this; } expandByObject( object ) { // Computes the world-axis-aligned bounding box of an object (including its children), // accounting for both the object's, and children's, world transforms object.updateWorldMatrix( false, false ); const geometry = object.geometry; if ( geometry !== undefined ) { if ( geometry.boundingBox === null ) { geometry.computeBoundingBox(); } _box.copy( geometry.boundingBox ); _box.applyMatrix4( object.matrixWorld ); this.union( _box ); } const children = object.children; for ( let i = 0, l = children.length; i < l; i ++ ) { this.expandByObject( children[ i ] ); } return this; } containsPoint( point ) { return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y || point.z < this.min.z || point.z > this.max.z ? false : true; } containsBox( box ) { return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y && this.min.z <= box.min.z && box.max.z <= this.max.z; } getParameter( point, target ) { // This can potentially have a divide by zero if the box // has a size dimension of 0. if ( target === undefined ) { console.warn( 'THREE.Box3: .getParameter() target is now required' ); target = new Vector3(); } return target.set( ( point.x - this.min.x ) / ( this.max.x - this.min.x ), ( point.y - this.min.y ) / ( this.max.y - this.min.y ), ( point.z - this.min.z ) / ( this.max.z - this.min.z ) ); } intersectsBox( box ) { // using 6 splitting planes to rule out intersections. return box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y || box.max.z < this.min.z || box.min.z > this.max.z ? false : true; } intersectsSphere( sphere ) { // Find the point on the AABB closest to the sphere center. this.clampPoint( sphere.center, _vector$1 ); // If that point is inside the sphere, the AABB and sphere intersect. return _vector$1.distanceToSquared( sphere.center ) <= ( sphere.radius * sphere.radius ); } intersectsPlane( plane ) { // We compute the minimum and maximum dot product values. If those values // are on the same side (back or front) of the plane, then there is no intersection. let min, max; if ( plane.normal.x > 0 ) { min = plane.normal.x * this.min.x; max = plane.normal.x * this.max.x; } else { min = plane.normal.x * this.max.x; max = plane.normal.x * this.min.x; } if ( plane.normal.y > 0 ) { min += plane.normal.y * this.min.y; max += plane.normal.y * this.max.y; } else { min += plane.normal.y * this.max.y; max += plane.normal.y * this.min.y; } if ( plane.normal.z > 0 ) { min += plane.normal.z * this.min.z; max += plane.normal.z * this.max.z; } else { min += plane.normal.z * this.max.z; max += plane.normal.z * this.min.z; } return ( min <= - plane.constant && max >= - plane.constant ); } intersectsTriangle( triangle ) { if ( this.isEmpty() ) { return false; } // compute box center and extents this.getCenter( _center ); _extents.subVectors( this.max, _center ); // translate triangle to aabb origin _v0.subVectors( triangle.a, _center ); _v1.subVectors( triangle.b, _center ); _v2.subVectors( triangle.c, _center ); // compute edge vectors for triangle _f0.subVectors( _v1, _v0 ); _f1.subVectors( _v2, _v1 ); _f2.subVectors( _v0, _v2 ); // test against axes that are given by cross product combinations of the edges of the triangle and the edges of the aabb // make an axis testing of each of the 3 sides of the aabb against each of the 3 sides of the triangle = 9 axis of separation // axis_ij = u_i x f_j (u0, u1, u2 = face normals of aabb = x,y,z axes vectors since aabb is axis aligned) let axes = [ 0, - _f0.z, _f0.y, 0, - _f1.z, _f1.y, 0, - _f2.z, _f2.y, _f0.z, 0, - _f0.x, _f1.z, 0, - _f1.x, _f2.z, 0, - _f2.x, - _f0.y, _f0.x, 0, - _f1.y, _f1.x, 0, - _f2.y, _f2.x, 0 ]; if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) { return false; } // test 3 face normals from the aabb axes = [ 1, 0, 0, 0, 1, 0, 0, 0, 1 ]; if ( ! satForAxes( axes, _v0, _v1, _v2, _extents ) ) { return false; } // finally testing the face normal of the triangle // use already existing triangle edge vectors here _triangleNormal.crossVectors( _f0, _f1 ); axes = [ _triangleNormal.x, _triangleNormal.y, _triangleNormal.z ]; return satForAxes( axes, _v0, _v1, _v2, _extents ); } clampPoint( point, target ) { if ( target === undefined ) { console.warn( 'THREE.Box3: .clampPoint() target is now required' ); target = new Vector3(); } return target.copy( point ).clamp( this.min, this.max ); } distanceToPoint( point ) { const clampedPoint = _vector$1.copy( point ).clamp( this.min, this.max ); return clampedPoint.sub( point ).length(); } getBoundingSphere( target ) { if ( target === undefined ) { console.error( 'THREE.Box3: .getBoundingSphere() target is now required' ); //target = new Sphere(); // removed to avoid cyclic dependency } this.getCenter( target.center ); target.radius = this.getSize( _vector$1 ).length() * 0.5; return target; } intersect( box ) { this.min.max( box.min ); this.max.min( box.max ); // ensure that if there is no overlap, the result is fully empty, not slightly empty with non-inf/+inf values that will cause subsequence intersects to erroneously return valid values. if ( this.isEmpty() ) this.makeEmpty(); return this; } union( box ) { this.min.min( box.min ); this.max.max( box.max ); return this; } applyMatrix4( matrix ) { // transform of empty box is an empty box. if ( this.isEmpty() ) return this; // NOTE: I am using a binary pattern to specify all 2^3 combinations below _points[ 0 ].set( this.min.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 000 _points[ 1 ].set( this.min.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 001 _points[ 2 ].set( this.min.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 010 _points[ 3 ].set( this.min.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 011 _points[ 4 ].set( this.max.x, this.min.y, this.min.z ).applyMatrix4( matrix ); // 100 _points[ 5 ].set( this.max.x, this.min.y, this.max.z ).applyMatrix4( matrix ); // 101 _points[ 6 ].set( this.max.x, this.max.y, this.min.z ).applyMatrix4( matrix ); // 110 _points[ 7 ].set( this.max.x, this.max.y, this.max.z ).applyMatrix4( matrix ); // 111 this.setFromPoints( _points ); return this; } translate( offset ) { this.min.add( offset ); this.max.add( offset ); return this; } equals( box ) { return box.min.equals( this.min ) && box.max.equals( this.max ); } } function satForAxes( axes, v0, v1, v2, extents ) { for ( let i = 0, j = axes.length - 3; i <= j; i += 3 ) { _testAxis.fromArray( axes, i ); // project the aabb onto the seperating axis const r = extents.x * Math.abs( _testAxis.x ) + extents.y * Math.abs( _testAxis.y ) + extents.z * Math.abs( _testAxis.z ); // project all 3 vertices of the triangle onto the seperating axis const p0 = v0.dot( _testAxis ); const p1 = v1.dot( _testAxis ); const p2 = v2.dot( _testAxis ); // actual test, basically see if either of the most extreme of the triangle points intersects r if ( Math.max( - Math.max( p0, p1, p2 ), Math.min( p0, p1, p2 ) ) > r ) { // points of the projected triangle are outside the projected half-length of the aabb // the axis is seperating and we can exit return false; } } return true; } const _points = [ /*@__PURE__*/ new Vector3(), /*@__PURE__*/ new Vector3(), /*@__PURE__*/ new Vector3(), /*@__PURE__*/ new Vector3(), /*@__PURE__*/ new Vector3(), /*@__PURE__*/ new Vector3(), /*@__PURE__*/ new Vector3(), /*@__PURE__*/ new Vector3() ]; const _vector$1 = /*@__PURE__*/ new Vector3(); const _box = /*@__PURE__*/ new Box3(); // triangle centered vertices const _v0 = /*@__PURE__*/ new Vector3(); const _v1 = /*@__PURE__*/ new Vector3(); const _v2 = /*@__PURE__*/ new Vector3(); // triangle edge vectors const _f0 = /*@__PURE__*/ new Vector3(); const _f1 = /*@__PURE__*/ new Vector3(); const _f2 = /*@__PURE__*/ new Vector3(); const _center = /*@__PURE__*/ new Vector3(); const _extents = /*@__PURE__*/ new Vector3(); const _triangleNormal = /*@__PURE__*/ new Vector3(); const _testAxis = /*@__PURE__*/ new Vector3(); const _box$1 = /*@__PURE__*/ new Box3(); class Sphere { constructor( center, radius ) { this.center = ( center !== undefined ) ? center : new Vector3(); this.radius = ( radius !== undefined ) ? radius : - 1; } set( center, radius ) { this.center.copy( center ); this.radius = radius; return this; } setFromPoints( points, optionalCenter ) { const center = this.center; if ( optionalCenter !== undefined ) { center.copy( optionalCenter ); } else { _box$1.setFromPoints( points ).getCenter( center ); } let maxRadiusSq = 0; for ( let i = 0, il = points.length; i < il; i ++ ) { maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( points[ i ] ) ); } this.radius = Math.sqrt( maxRadiusSq ); return this; } clone() { return new this.constructor().copy( this ); } copy( sphere ) { this.center.copy( sphere.center ); this.radius = sphere.radius; return this; } isEmpty() { return ( this.radius < 0 ); } makeEmpty() { this.center.set( 0, 0, 0 ); this.radius = - 1; return this; } containsPoint( point ) { return ( point.distanceToSquared( this.center ) <= ( this.radius * this.radius ) ); } distanceToPoint( point ) { return ( point.distanceTo( this.center ) - this.radius ); } intersectsSphere( sphere ) { const radiusSum = this.radius + sphere.radius; return sphere.center.distanceToSquared( this.center ) <= ( radiusSum * radiusSum ); } intersectsBox( box ) { return box.intersectsSphere( this ); } intersectsPlane( plane ) { return Math.abs( plane.distanceToPoint( this.center ) ) <= this.radius; } clampPoint( point, target ) { const deltaLengthSq = this.center.distanceToSquared( point ); if ( target === undefined ) { console.warn( 'THREE.Sphere: .clampPoint() target is now required' ); target = new Vector3(); } target.copy( point ); if ( deltaLengthSq > ( this.radius * this.radius ) ) { target.sub( this.center ).normalize(); target.multiplyScalar( this.radius ).add( this.center ); } return target; } getBoundingBox( target ) { if ( target === undefined ) { console.warn( 'THREE.Sphere: .getBoundingBox() target is now required' ); target = new Box3(); } if ( this.isEmpty() ) { // Empty sphere produces empty bounding box target.makeEmpty(); return target; } target.set( this.center, this.center ); target.expandByScalar( this.radius ); return target; } applyMatrix4( matrix ) { this.center.applyMatrix4( matrix ); this.radius = this.radius * matrix.getMaxScaleOnAxis(); return this; } translate( offset ) { this.center.add( offset ); return this; } equals( sphere ) { return sphere.center.equals( this.center ) && ( sphere.radius === this.radius ); } } const _vector$2 = /*@__PURE__*/ new Vector3(); const _segCenter = /*@__PURE__*/ new Vector3(); const _segDir = /*@__PURE__*/ new Vector3(); const _diff = /*@__PURE__*/ new Vector3(); const _edge1 = /*@__PURE__*/ new Vector3(); const _edge2 = /*@__PURE__*/ new Vector3(); const _normal = /*@__PURE__*/ new Vector3(); class Ray { constructor( origin, direction ) { this.origin = ( origin !== undefined ) ? origin : new Vector3(); this.direction = ( direction !== undefined ) ? direction : new Vector3( 0, 0, - 1 ); } set( origin, direction ) { this.origin.copy( origin ); this.direction.copy( direction ); return this; } clone() { return new this.constructor().copy( this ); } copy( ray ) { this.origin.copy( ray.origin ); this.direction.copy( ray.direction ); return this; } at( t, target ) { if ( target === undefined ) { console.warn( 'THREE.Ray: .at() target is now required' ); target = new Vector3(); } return target.copy( this.direction ).multiplyScalar( t ).add( this.origin ); } lookAt( v ) { this.direction.copy( v ).sub( this.origin ).normalize(); return this; } recast( t ) { this.origin.copy( this.at( t, _vector$2 ) ); return this; } closestPointToPoint( point, target ) { if ( target === undefined ) { console.warn( 'THREE.Ray: .closestPointToPoint() target is now required' ); target = new Vector3(); } target.subVectors( point, this.origin ); const directionDistance = target.dot( this.direction ); if ( directionDistance < 0 ) { return target.copy( this.origin ); } return target.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin ); } distanceToPoint( point ) { return Math.sqrt( this.distanceSqToPoint( point ) ); } distanceSqToPoint( point ) { const directionDistance = _vector$2.subVectors( point, this.origin ).dot( this.direction ); // point behind the ray if ( directionDistance < 0 ) { return this.origin.distanceToSquared( point ); } _vector$2.copy( this.direction ).multiplyScalar( directionDistance ).add( this.origin ); return _vector$2.distanceToSquared( point ); } distanceSqToSegment( v0, v1, optionalPointOnRay, optionalPointOnSegment ) { // from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteDistRaySegment.h // It returns the min distance between the ray and the segment // defined by v0 and v1 // It can also set two optional targets : // - The closest point on the ray // - The closest point on the segment _segCenter.copy( v0 ).add( v1 ).multiplyScalar( 0.5 ); _segDir.copy( v1 ).sub( v0 ).normalize(); _diff.copy( this.origin ).sub( _segCenter ); const segExtent = v0.distanceTo( v1 ) * 0.5; const a01 = - this.direction.dot( _segDir ); const b0 = _diff.dot( this.direction ); const b1 = - _diff.dot( _segDir ); const c = _diff.lengthSq(); const det = Math.abs( 1 - a01 * a01 ); let s0, s1, sqrDist, extDet; if ( det > 0 ) { // The ray and segment are not parallel. s0 = a01 * b1 - b0; s1 = a01 * b0 - b1; extDet = segExtent * det; if ( s0 >= 0 ) { if ( s1 >= - extDet ) { if ( s1 <= extDet ) { // region 0 // Minimum at interior points of ray and segment. const invDet = 1 / det; s0 *= invDet; s1 *= invDet; sqrDist = s0 * ( s0 + a01 * s1 + 2 * b0 ) + s1 * ( a01 * s0 + s1 + 2 * b1 ) + c; } else { // region 1 s1 = segExtent; s0 = Math.max( 0, - ( a01 * s1 + b0 ) ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } } else { // region 5 s1 = - segExtent; s0 = Math.max( 0, - ( a01 * s1 + b0 ) ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } } else { if ( s1 <= - extDet ) { // region 4 s0 = Math.max( 0, - ( - a01 * segExtent + b0 ) ); s1 = ( s0 > 0 ) ? - segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } else if ( s1 <= extDet ) { // region 3 s0 = 0; s1 = Math.min( Math.max( - segExtent, - b1 ), segExtent ); sqrDist = s1 * ( s1 + 2 * b1 ) + c; } else { // region 2 s0 = Math.max( 0, - ( a01 * segExtent + b0 ) ); s1 = ( s0 > 0 ) ? segExtent : Math.min( Math.max( - segExtent, - b1 ), segExtent ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } } } else { // Ray and segment are parallel. s1 = ( a01 > 0 ) ? - segExtent : segExtent; s0 = Math.max( 0, - ( a01 * s1 + b0 ) ); sqrDist = - s0 * s0 + s1 * ( s1 + 2 * b1 ) + c; } if ( optionalPointOnRay ) { optionalPointOnRay.copy( this.direction ).multiplyScalar( s0 ).add( this.origin ); } if ( optionalPointOnSegment ) { optionalPointOnSegment.copy( _segDir ).multiplyScalar( s1 ).add( _segCenter ); } return sqrDist; } intersectSphere( sphere, target ) { _vector$2.subVectors( sphere.center, this.origin ); const tca = _vector$2.dot( this.direction ); const d2 = _vector$2.dot( _vector$2 ) - tca * tca; const radius2 = sphere.radius * sphere.radius; if ( d2 > radius2 ) return null; const thc = Math.sqrt( radius2 - d2 ); // t0 = first intersect point - entrance on front of sphere const t0 = tca - thc; // t1 = second intersect point - exit point on back of sphere const t1 = tca + thc; // test to see if both t0 and t1 are behind the ray - if so, return null if ( t0 < 0 && t1 < 0 ) return null; // test to see if t0 is behind the ray: // if it is, the ray is inside the sphere, so return the second exit point scaled by t1, // in order to always return an intersect point that is in front of the ray. if ( t0 < 0 ) return this.at( t1, target ); // else t0 is in front of the ray, so return the first collision point scaled by t0 return this.at( t0, target ); } intersectsSphere( sphere ) { return this.distanceSqToPoint( sphere.center ) <= ( sphere.radius * sphere.radius ); } distanceToPlane( plane ) { const denominator = plane.normal.dot( this.direction ); if ( denominator === 0 ) { // line is coplanar, return origin if ( plane.distanceToPoint( this.origin ) === 0 ) { return 0; } // Null is preferable to undefined since undefined means.... it is undefined return null; } const t = - ( this.origin.dot( plane.normal ) + plane.constant ) / denominator; // Return if the ray never intersects the plane return t >= 0 ? t : null; } intersectPlane( plane, target ) { const t = this.distanceToPlane( plane ); if ( t === null ) { return null; } return this.at( t, target ); } intersectsPlane( plane ) { // check if the ray lies on the plane first const distToPoint = plane.distanceToPoint( this.origin ); if ( distToPoint === 0 ) { return true; } const denominator = plane.normal.dot( this.direction ); if ( denominator * distToPoint < 0 ) { return true; } // ray origin is behind the plane (and is pointing behind it) return false; } intersectBox( box, target ) { let tmin, tmax, tymin, tymax, tzmin, tzmax; const invdirx = 1 / this.direction.x, invdiry = 1 / this.direction.y, invdirz = 1 / this.direction.z; const origin = this.origin; if ( invdirx >= 0 ) { tmin = ( box.min.x - origin.x ) * invdirx; tmax = ( box.max.x - origin.x ) * invdirx; } else { tmin = ( box.max.x - origin.x ) * invdirx; tmax = ( box.min.x - origin.x ) * invdirx; } if ( invdiry >= 0 ) { tymin = ( box.min.y - origin.y ) * invdiry; tymax = ( box.max.y - origin.y ) * invdiry; } else { tymin = ( box.max.y - origin.y ) * invdiry; tymax = ( box.min.y - origin.y ) * invdiry; } if ( ( tmin > tymax ) || ( tymin > tmax ) ) return null; // These lines also handle the case where tmin or tmax is NaN // (result of 0 * Infinity). x !== x returns true if x is NaN if ( tymin > tmin || tmin !== tmin ) tmin = tymin; if ( tymax < tmax || tmax !== tmax ) tmax = tymax; if ( invdirz >= 0 ) { tzmin = ( box.min.z - origin.z ) * invdirz; tzmax = ( box.max.z - origin.z ) * invdirz; } else { tzmin = ( box.max.z - origin.z ) * invdirz; tzmax = ( box.min.z - origin.z ) * invdirz; } if ( ( tmin > tzmax ) || ( tzmin > tmax ) ) return null; if ( tzmin > tmin || tmin !== tmin ) tmin = tzmin; if ( tzmax < tmax || tmax !== tmax ) tmax = tzmax; //return point closest to the ray (positive side) if ( tmax < 0 ) return null; return this.at( tmin >= 0 ? tmin : tmax, target ); } intersectsBox( box ) { return this.intersectBox( box, _vector$2 ) !== null; } intersectTriangle( a, b, c, backfaceCulling, target ) { // Compute the offset origin, edges, and normal. // from http://www.geometrictools.com/GTEngine/Include/Mathematics/GteIntrRay3Triangle3.h _edge1.subVectors( b, a ); _edge2.subVectors( c, a ); _normal.crossVectors( _edge1, _edge2 ); // Solve Q + t*D = b1*E1 + b2*E2 (Q = kDiff, D = ray direction, // E1 = kEdge1, E2 = kEdge2, N = Cross(E1,E2)) by // |Dot(D,N)|*b1 = sign(Dot(D,N))*Dot(D,Cross(Q,E2)) // |Dot(D,N)|*b2 = sign(Dot(D,N))*Dot(D,Cross(E1,Q)) // |Dot(D,N)|*t = -sign(Dot(D,N))*Dot(Q,N) let DdN = this.direction.dot( _normal ); let sign; if ( DdN > 0 ) { if ( backfaceCulling ) return null; sign = 1; } else if ( DdN < 0 ) { sign = - 1; DdN = - DdN; } else { return null; } _diff.subVectors( this.origin, a ); const DdQxE2 = sign * this.direction.dot( _edge2.crossVectors( _diff, _edge2 ) ); // b1 < 0, no intersection if ( DdQxE2 < 0 ) { return null; } const DdE1xQ = sign * this.direction.dot( _edge1.cross( _diff ) ); // b2 < 0, no intersection if ( DdE1xQ < 0 ) { return null; } // b1+b2 > 1, no intersection if ( DdQxE2 + DdE1xQ > DdN ) { return null; } // Line intersects triangle, check if ray does. const QdN = - sign * _diff.dot( _normal ); // t < 0, no intersection if ( QdN < 0 ) { return null; } // Ray intersects triangle. return this.at( QdN / DdN, target ); } applyMatrix4( matrix4 ) { this.origin.applyMatrix4( matrix4 ); this.direction.transformDirection( matrix4 ); return this; } equals( ray ) { return ray.origin.equals( this.origin ) && ray.direction.equals( this.direction ); } } class Matrix4 { constructor() { Object.defineProperty( this, 'isMatrix4', { value: true } ); this.elements = [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ]; if ( arguments.length > 0 ) { console.error( 'THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.' ); } } set( n11, n12, n13, n14, n21, n22, n23, n24, n31, n32, n33, n34, n41, n42, n43, n44 ) { const te = this.elements; te[ 0 ] = n11; te[ 4 ] = n12; te[ 8 ] = n13; te[ 12 ] = n14; te[ 1 ] = n21; te[ 5 ] = n22; te[ 9 ] = n23; te[ 13 ] = n24; te[ 2 ] = n31; te[ 6 ] = n32; te[ 10 ] = n33; te[ 14 ] = n34; te[ 3 ] = n41; te[ 7 ] = n42; te[ 11 ] = n43; te[ 15 ] = n44; return this; } identity() { this.set( 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); return this; } clone() { return new Matrix4().fromArray( this.elements ); } copy( m ) { const te = this.elements; const me = m.elements; te[ 0 ] = me[ 0 ]; te[ 1 ] = me[ 1 ]; te[ 2 ] = me[ 2 ]; te[ 3 ] = me[ 3 ]; te[ 4 ] = me[ 4 ]; te[ 5 ] = me[ 5 ]; te[ 6 ] = me[ 6 ]; te[ 7 ] = me[ 7 ]; te[ 8 ] = me[ 8 ]; te[ 9 ] = me[ 9 ]; te[ 10 ] = me[ 10 ]; te[ 11 ] = me[ 11 ]; te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; te[ 15 ] = me[ 15 ]; return this; } copyPosition( m ) { const te = this.elements, me = m.elements; te[ 12 ] = me[ 12 ]; te[ 13 ] = me[ 13 ]; te[ 14 ] = me[ 14 ]; return this; } extractBasis( xAxis, yAxis, zAxis ) { xAxis.setFromMatrixColumn( this, 0 ); yAxis.setFromMatrixColumn( this, 1 ); zAxis.setFromMatrixColumn( this, 2 ); return this; } makeBasis( xAxis, yAxis, zAxis ) { this.set( xAxis.x, yAxis.x, zAxis.x, 0, xAxis.y, yAxis.y, zAxis.y, 0, xAxis.z, yAxis.z, zAxis.z, 0, 0, 0, 0, 1 ); return this; } extractRotation( m ) { // this method does not support reflection matrices const te = this.elements; const me = m.elements; const scaleX = 1 / _v1$1.setFromMatrixColumn( m, 0 ).length(); const scaleY = 1 / _v1$1.setFromMatrixColumn( m, 1 ).length(); const scaleZ = 1 / _v1$1.setFromMatrixColumn( m, 2 ).length(); te[ 0 ] = me[ 0 ] * scaleX; te[ 1 ] = me[ 1 ] * scaleX; te[ 2 ] = me[ 2 ] * scaleX; te[ 3 ] = 0; te[ 4 ] = me[ 4 ] * scaleY; te[ 5 ] = me[ 5 ] * scaleY; te[ 6 ] = me[ 6 ] * scaleY; te[ 7 ] = 0; te[ 8 ] = me[ 8 ] * scaleZ; te[ 9 ] = me[ 9 ] * scaleZ; te[ 10 ] = me[ 10 ] * scaleZ; te[ 11 ] = 0; te[ 12 ] = 0; te[ 13 ] = 0; te[ 14 ] = 0; te[ 15 ] = 1; return this; } makeRotationFromEuler( euler ) { if ( ! ( euler && euler.isEuler ) ) { console.error( 'THREE.Matrix4: .makeRotationFromEuler() now expects a Euler rotation rather than a Vector3 and order.' ); } const te = this.elements; const x = euler.x, y = euler.y, z = euler.z; const a = Math.cos( x ), b = Math.sin( x ); const c = Math.cos( y ), d = Math.sin( y ); const e = Math.cos( z ), f = Math.sin( z ); if ( euler.order === 'XYZ' ) { const ae = a * e, af = a * f, be = b * e, bf = b * f; te[ 0 ] = c * e; te[ 4 ] = - c * f; te[ 8 ] = d; te[ 1 ] = af + be * d; te[ 5 ] = ae - bf * d; te[ 9 ] = - b * c; te[ 2 ] = bf - ae * d; te[ 6 ] = be + af * d; te[ 10 ] = a * c; } else if ( euler.order === 'YXZ' ) { const ce = c * e, cf = c * f, de = d * e, df = d * f; te[ 0 ] = ce + df * b; te[ 4 ] = de * b - cf; te[ 8 ] = a * d; te[ 1 ] = a * f; te[ 5 ] = a * e; te[ 9 ] = - b; te[ 2 ] = cf * b - de; te[ 6 ] = df + ce * b; te[ 10 ] = a * c; } else if ( euler.order === 'ZXY' ) { const ce = c * e, cf = c * f, de = d * e, df = d * f; te[ 0 ] = ce - df * b; te[ 4 ] = - a * f; te[ 8 ] = de + cf * b; te[ 1 ] = cf + de * b; te[ 5 ] = a * e; te[ 9 ] = df - ce * b; te[ 2 ] = - a * d; te[ 6 ] = b; te[ 10 ] = a * c; } else if ( euler.order === 'ZYX' ) { const ae = a * e, af = a * f, be = b * e, bf = b * f; te[ 0 ] = c * e; te[ 4 ] = be * d - af; te[ 8 ] = ae * d + bf; te[ 1 ] = c * f; te[ 5 ] = bf * d + ae; te[ 9 ] = af * d - be; te[ 2 ] = - d; te[ 6 ] = b * c; te[ 10 ] = a * c; } else if ( euler.order === 'YZX' ) { const ac = a * c, ad = a * d, bc = b * c, bd = b * d; te[ 0 ] = c * e; te[ 4 ] = bd - ac * f; te[ 8 ] = bc * f + ad; te[ 1 ] = f; te[ 5 ] = a * e; te[ 9 ] = - b * e; te[ 2 ] = - d * e; te[ 6 ] = ad * f + bc; te[ 10 ] = ac - bd * f; } else if ( euler.order === 'XZY' ) { const ac = a * c, ad = a * d, bc = b * c, bd = b * d; te[ 0 ] = c * e; te[ 4 ] = - f; te[ 8 ] = d * e; te[ 1 ] = ac * f + bd; te[ 5 ] = a * e; te[ 9 ] = ad * f - bc; te[ 2 ] = bc * f - ad; te[ 6 ] = b * e; te[ 10 ] = bd * f + ac; } // bottom row te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; // last column te[ 12 ] = 0; te[ 13 ] = 0; te[ 14 ] = 0; te[ 15 ] = 1; return this; } makeRotationFromQuaternion( q ) { return this.compose( _zero, q, _one ); } lookAt( eye, target, up ) { const te = this.elements; _z.subVectors( eye, target ); if ( _z.lengthSq() === 0 ) { // eye and target are in the same position _z.z = 1; } _z.normalize(); _x.crossVectors( up, _z ); if ( _x.lengthSq() === 0 ) { // up and z are parallel if ( Math.abs( up.z ) === 1 ) { _z.x += 0.0001; } else { _z.z += 0.0001; } _z.normalize(); _x.crossVectors( up, _z ); } _x.normalize(); _y.crossVectors( _z, _x ); te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x; te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y; te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z; return this; } multiply( m, n ) { if ( n !== undefined ) { console.warn( 'THREE.Matrix4: .multiply() now only accepts one argument. Use .multiplyMatrices( a, b ) instead.' ); return this.multiplyMatrices( m, n ); } return this.multiplyMatrices( this, m ); } premultiply( m ) { return this.multiplyMatrices( m, this ); } multiplyMatrices( a, b ) { const ae = a.elements; const be = b.elements; const te = this.elements; const a11 = ae[ 0 ], a12 = ae[ 4 ], a13 = ae[ 8 ], a14 = ae[ 12 ]; const a21 = ae[ 1 ], a22 = ae[ 5 ], a23 = ae[ 9 ], a24 = ae[ 13 ]; const a31 = ae[ 2 ], a32 = ae[ 6 ], a33 = ae[ 10 ], a34 = ae[ 14 ]; const a41 = ae[ 3 ], a42 = ae[ 7 ], a43 = ae[ 11 ], a44 = ae[ 15 ]; const b11 = be[ 0 ], b12 = be[ 4 ], b13 = be[ 8 ], b14 = be[ 12 ]; const b21 = be[ 1 ], b22 = be[ 5 ], b23 = be[ 9 ], b24 = be[ 13 ]; const b31 = be[ 2 ], b32 = be[ 6 ], b33 = be[ 10 ], b34 = be[ 14 ]; const b41 = be[ 3 ], b42 = be[ 7 ], b43 = be[ 11 ], b44 = be[ 15 ]; te[ 0 ] = a11 * b11 + a12 * b21 + a13 * b31 + a14 * b41; te[ 4 ] = a11 * b12 + a12 * b22 + a13 * b32 + a14 * b42; te[ 8 ] = a11 * b13 + a12 * b23 + a13 * b33 + a14 * b43; te[ 12 ] = a11 * b14 + a12 * b24 + a13 * b34 + a14 * b44; te[ 1 ] = a21 * b11 + a22 * b21 + a23 * b31 + a24 * b41; te[ 5 ] = a21 * b12 + a22 * b22 + a23 * b32 + a24 * b42; te[ 9 ] = a21 * b13 + a22 * b23 + a23 * b33 + a24 * b43; te[ 13 ] = a21 * b14 + a22 * b24 + a23 * b34 + a24 * b44; te[ 2 ] = a31 * b11 + a32 * b21 + a33 * b31 + a34 * b41; te[ 6 ] = a31 * b12 + a32 * b22 + a33 * b32 + a34 * b42; te[ 10 ] = a31 * b13 + a32 * b23 + a33 * b33 + a34 * b43; te[ 14 ] = a31 * b14 + a32 * b24 + a33 * b34 + a34 * b44; te[ 3 ] = a41 * b11 + a42 * b21 + a43 * b31 + a44 * b41; te[ 7 ] = a41 * b12 + a42 * b22 + a43 * b32 + a44 * b42; te[ 11 ] = a41 * b13 + a42 * b23 + a43 * b33 + a44 * b43; te[ 15 ] = a41 * b14 + a42 * b24 + a43 * b34 + a44 * b44; return this; } multiplyScalar( s ) { const te = this.elements; te[ 0 ] *= s; te[ 4 ] *= s; te[ 8 ] *= s; te[ 12 ] *= s; te[ 1 ] *= s; te[ 5 ] *= s; te[ 9 ] *= s; te[ 13 ] *= s; te[ 2 ] *= s; te[ 6 ] *= s; te[ 10 ] *= s; te[ 14 ] *= s; te[ 3 ] *= s; te[ 7 ] *= s; te[ 11 ] *= s; te[ 15 ] *= s; return this; } determinant() { const te = this.elements; const n11 = te[ 0 ], n12 = te[ 4 ], n13 = te[ 8 ], n14 = te[ 12 ]; const n21 = te[ 1 ], n22 = te[ 5 ], n23 = te[ 9 ], n24 = te[ 13 ]; const n31 = te[ 2 ], n32 = te[ 6 ], n33 = te[ 10 ], n34 = te[ 14 ]; const n41 = te[ 3 ], n42 = te[ 7 ], n43 = te[ 11 ], n44 = te[ 15 ]; //TODO: make this more efficient //( based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm ) return ( n41 * ( + n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34 ) + n42 * ( + n11 * n23 * n34 - n11 * n24 * n33 + n14 * n21 * n33 - n13 * n21 * n34 + n13 * n24 * n31 - n14 * n23 * n31 ) + n43 * ( + n11 * n24 * n32 - n11 * n22 * n34 - n14 * n21 * n32 + n12 * n21 * n34 + n14 * n22 * n31 - n12 * n24 * n31 ) + n44 * ( - n13 * n22 * n31 - n11 * n23 * n32 + n11 * n22 * n33 + n13 * n21 * n32 - n12 * n21 * n33 + n12 * n23 * n31 ) ); } transpose() { const te = this.elements; let tmp; tmp = te[ 1 ]; te[ 1 ] = te[ 4 ]; te[ 4 ] = tmp; tmp = te[ 2 ]; te[ 2 ] = te[ 8 ]; te[ 8 ] = tmp; tmp = te[ 6 ]; te[ 6 ] = te[ 9 ]; te[ 9 ] = tmp; tmp = te[ 3 ]; te[ 3 ] = te[ 12 ]; te[ 12 ] = tmp; tmp = te[ 7 ]; te[ 7 ] = te[ 13 ]; te[ 13 ] = tmp; tmp = te[ 11 ]; te[ 11 ] = te[ 14 ]; te[ 14 ] = tmp; return this; } setPosition( x, y, z ) { const te = this.elements; if ( x.isVector3 ) { te[ 12 ] = x.x; te[ 13 ] = x.y; te[ 14 ] = x.z; } else { te[ 12 ] = x; te[ 13 ] = y; te[ 14 ] = z; } return this; } invert() { // based on http://www.euclideanspace.com/maths/algebra/matrix/functions/inverse/fourD/index.htm const te = this.elements, n11 = te[ 0 ], n21 = te[ 1 ], n31 = te[ 2 ], n41 = te[ 3 ], n12 = te[ 4 ], n22 = te[ 5 ], n32 = te[ 6 ], n42 = te[ 7 ], n13 = te[ 8 ], n23 = te[ 9 ], n33 = te[ 10 ], n43 = te[ 11 ], n14 = te[ 12 ], n24 = te[ 13 ], n34 = te[ 14 ], n44 = te[ 15 ], t11 = n23 * n34 * n42 - n24 * n33 * n42 + n24 * n32 * n43 - n22 * n34 * n43 - n23 * n32 * n44 + n22 * n33 * n44, t12 = n14 * n33 * n42 - n13 * n34 * n42 - n14 * n32 * n43 + n12 * n34 * n43 + n13 * n32 * n44 - n12 * n33 * n44, t13 = n13 * n24 * n42 - n14 * n23 * n42 + n14 * n22 * n43 - n12 * n24 * n43 - n13 * n22 * n44 + n12 * n23 * n44, t14 = n14 * n23 * n32 - n13 * n24 * n32 - n14 * n22 * n33 + n12 * n24 * n33 + n13 * n22 * n34 - n12 * n23 * n34; const det = n11 * t11 + n21 * t12 + n31 * t13 + n41 * t14; if ( det === 0 ) return this.set( 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ); const detInv = 1 / det; te[ 0 ] = t11 * detInv; te[ 1 ] = ( n24 * n33 * n41 - n23 * n34 * n41 - n24 * n31 * n43 + n21 * n34 * n43 + n23 * n31 * n44 - n21 * n33 * n44 ) * detInv; te[ 2 ] = ( n22 * n34 * n41 - n24 * n32 * n41 + n24 * n31 * n42 - n21 * n34 * n42 - n22 * n31 * n44 + n21 * n32 * n44 ) * detInv; te[ 3 ] = ( n23 * n32 * n41 - n22 * n33 * n41 - n23 * n31 * n42 + n21 * n33 * n42 + n22 * n31 * n43 - n21 * n32 * n43 ) * detInv; te[ 4 ] = t12 * detInv; te[ 5 ] = ( n13 * n34 * n41 - n14 * n33 * n41 + n14 * n31 * n43 - n11 * n34 * n43 - n13 * n31 * n44 + n11 * n33 * n44 ) * detInv; te[ 6 ] = ( n14 * n32 * n41 - n12 * n34 * n41 - n14 * n31 * n42 + n11 * n34 * n42 + n12 * n31 * n44 - n11 * n32 * n44 ) * detInv; te[ 7 ] = ( n12 * n33 * n41 - n13 * n32 * n41 + n13 * n31 * n42 - n11 * n33 * n42 - n12 * n31 * n43 + n11 * n32 * n43 ) * detInv; te[ 8 ] = t13 * detInv; te[ 9 ] = ( n14 * n23 * n41 - n13 * n24 * n41 - n14 * n21 * n43 + n11 * n24 * n43 + n13 * n21 * n44 - n11 * n23 * n44 ) * detInv; te[ 10 ] = ( n12 * n24 * n41 - n14 * n22 * n41 + n14 * n21 * n42 - n11 * n24 * n42 - n12 * n21 * n44 + n11 * n22 * n44 ) * detInv; te[ 11 ] = ( n13 * n22 * n41 - n12 * n23 * n41 - n13 * n21 * n42 + n11 * n23 * n42 + n12 * n21 * n43 - n11 * n22 * n43 ) * detInv; te[ 12 ] = t14 * detInv; te[ 13 ] = ( n13 * n24 * n31 - n14 * n23 * n31 + n14 * n21 * n33 - n11 * n24 * n33 - n13 * n21 * n34 + n11 * n23 * n34 ) * detInv; te[ 14 ] = ( n14 * n22 * n31 - n12 * n24 * n31 - n14 * n21 * n32 + n11 * n24 * n32 + n12 * n21 * n34 - n11 * n22 * n34 ) * detInv; te[ 15 ] = ( n12 * n23 * n31 - n13 * n22 * n31 + n13 * n21 * n32 - n11 * n23 * n32 - n12 * n21 * n33 + n11 * n22 * n33 ) * detInv; return this; } scale( v ) { const te = this.elements; const x = v.x, y = v.y, z = v.z; te[ 0 ] *= x; te[ 4 ] *= y; te[ 8 ] *= z; te[ 1 ] *= x; te[ 5 ] *= y; te[ 9 ] *= z; te[ 2 ] *= x; te[ 6 ] *= y; te[ 10 ] *= z; te[ 3 ] *= x; te[ 7 ] *= y; te[ 11 ] *= z; return this; } getMaxScaleOnAxis() { const te = this.elements; const scaleXSq = te[ 0 ] * te[ 0 ] + te[ 1 ] * te[ 1 ] + te[ 2 ] * te[ 2 ]; const scaleYSq = te[ 4 ] * te[ 4 ] + te[ 5 ] * te[ 5 ] + te[ 6 ] * te[ 6 ]; const scaleZSq = te[ 8 ] * te[ 8 ] + te[ 9 ] * te[ 9 ] + te[ 10 ] * te[ 10 ]; return Math.sqrt( Math.max( scaleXSq, scaleYSq, scaleZSq ) ); } makeTranslation( x, y, z ) { this.set( 1, 0, 0, x, 0, 1, 0, y, 0, 0, 1, z, 0, 0, 0, 1 ); return this; } makeRotationX( theta ) { const c = Math.cos( theta ), s = Math.sin( theta ); this.set( 1, 0, 0, 0, 0, c, - s, 0, 0, s, c, 0, 0, 0, 0, 1 ); return this; } makeRotationY( theta ) { const c = Math.cos( theta ), s = Math.sin( theta ); this.set( c, 0, s, 0, 0, 1, 0, 0, - s, 0, c, 0, 0, 0, 0, 1 ); return this; } makeRotationZ( theta ) { const c = Math.cos( theta ), s = Math.sin( theta ); this.set( c, - s, 0, 0, s, c, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ); return this; } makeRotationAxis( axis, angle ) { // Based on http://www.gamedev.net/reference/articles/article1199.asp const c = Math.cos( angle ); const s = Math.sin( angle ); const t = 1 - c; const x = axis.x, y = axis.y, z = axis.z; const tx = t * x, ty = t * y; this.set( tx * x + c, tx * y - s * z, tx * z + s * y, 0, tx * y + s * z, ty * y + c, ty * z - s * x, 0, tx * z - s * y, ty * z + s * x, t * z * z + c, 0, 0, 0, 0, 1 ); return this; } makeScale( x, y, z ) { this.set( x, 0, 0, 0, 0, y, 0, 0, 0, 0, z, 0, 0, 0, 0, 1 ); return this; } makeShear( x, y, z ) { this.set( 1, y, z, 0, x, 1, z, 0, x, y, 1, 0, 0, 0, 0, 1 ); return this; } compose( position, quaternion, scale ) { const te = this.elements; const x = quaternion._x, y = quaternion._y, z = quaternion._z, w = quaternion._w; const x2 = x + x, y2 = y + y, z2 = z + z; const xx = x * x2, xy = x * y2, xz = x * z2; const yy = y * y2, yz = y * z2, zz = z * z2; const wx = w * x2, wy = w * y2, wz = w * z2; const sx = scale.x, sy = scale.y, sz = scale.z; te[ 0 ] = ( 1 - ( yy + zz ) ) * sx; te[ 1 ] = ( xy + wz ) * sx; te[ 2 ] = ( xz - wy ) * sx; te[ 3 ] = 0; te[ 4 ] = ( xy - wz ) * sy; te[ 5 ] = ( 1 - ( xx + zz ) ) * sy; te[ 6 ] = ( yz + wx ) * sy; te[ 7 ] = 0; te[ 8 ] = ( xz + wy ) * sz; te[ 9 ] = ( yz - wx ) * sz; te[ 10 ] = ( 1 - ( xx + yy ) ) * sz; te[ 11 ] = 0; te[ 12 ] = position.x; te[ 13 ] = position.y; te[ 14 ] = position.z; te[ 15 ] = 1; return this; } decompose( position, quaternion, scale ) { const te = this.elements; let sx = _v1$1.set( te[ 0 ], te[ 1 ], te[ 2 ] ).length(); const sy = _v1$1.set( te[ 4 ], te[ 5 ], te[ 6 ] ).length(); const sz = _v1$1.set( te[ 8 ], te[ 9 ], te[ 10 ] ).length(); // if determine is negative, we need to invert one scale const det = this.determinant(); if ( det < 0 ) sx = - sx; position.x = te[ 12 ]; position.y = te[ 13 ]; position.z = te[ 14 ]; // scale the rotation part _m1.copy( this ); const invSX = 1 / sx; const invSY = 1 / sy; const invSZ = 1 / sz; _m1.elements[ 0 ] *= invSX; _m1.elements[ 1 ] *= invSX; _m1.elements[ 2 ] *= invSX; _m1.elements[ 4 ] *= invSY; _m1.elements[ 5 ] *= invSY; _m1.elements[ 6 ] *= invSY; _m1.elements[ 8 ] *= invSZ; _m1.elements[ 9 ] *= invSZ; _m1.elements[ 10 ] *= invSZ; quaternion.setFromRotationMatrix( _m1 ); scale.x = sx; scale.y = sy; scale.z = sz; return this; } makePerspective( left, right, top, bottom, near, far ) { if ( far === undefined ) { console.warn( 'THREE.Matrix4: .makePerspective() has been redefined and has a new signature. Please check the docs.' ); } const te = this.elements; const x = 2 * near / ( right - left ); const y = 2 * near / ( top - bottom ); const a = ( right + left ) / ( right - left ); const b = ( top + bottom ) / ( top - bottom ); const c = - ( far + near ) / ( far - near ); const d = - 2 * far * near / ( far - near ); te[ 0 ] = x; te[ 4 ] = 0; te[ 8 ] = a; te[ 12 ] = 0; te[ 1 ] = 0; te[ 5 ] = y; te[ 9 ] = b; te[ 13 ] = 0; te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = c; te[ 14 ] = d; te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = - 1; te[ 15 ] = 0; return this; } makeOrthographic( left, right, top, bottom, near, far ) { const te = this.elements; const w = 1.0 / ( right - left ); const h = 1.0 / ( top - bottom ); const p = 1.0 / ( far - near ); const x = ( right + left ) * w; const y = ( top + bottom ) * h; const z = ( far + near ) * p; te[ 0 ] = 2 * w; te[ 4 ] = 0; te[ 8 ] = 0; te[ 12 ] = - x; te[ 1 ] = 0; te[ 5 ] = 2 * h; te[ 9 ] = 0; te[ 13 ] = - y; te[ 2 ] = 0; te[ 6 ] = 0; te[ 10 ] = - 2 * p; te[ 14 ] = - z; te[ 3 ] = 0; te[ 7 ] = 0; te[ 11 ] = 0; te[ 15 ] = 1; return this; } equals( matrix ) { const te = this.elements; const me = matrix.elements; for ( let i = 0; i < 16; i ++ ) { if ( te[ i ] !== me[ i ] ) return false; } return true; } fromArray( array, offset = 0 ) { for ( let i = 0; i < 16; i ++ ) { this.elements[ i ] = array[ i + offset ]; } return this; } toArray( array = [], offset = 0 ) { const te = this.elements; array[ offset ] = te[ 0 ]; array[ offset + 1 ] = te[ 1 ]; array[ offset + 2 ] = te[ 2 ]; array[ offset + 3 ] = te[ 3 ]; array[ offset + 4 ] = te[ 4 ]; array[ offset + 5 ] = te[ 5 ]; array[ offset + 6 ] = te[ 6 ]; array[ offset + 7 ] = te[ 7 ]; array[ offset + 8 ] = te[ 8 ]; array[ offset + 9 ] = te[ 9 ]; array[ offset + 10 ] = te[ 10 ]; array[ offset + 11 ] = te[ 11 ]; array[ offset + 12 ] = te[ 12 ]; array[ offset + 13 ] = te[ 13 ]; array[ offset + 14 ] = te[ 14 ]; array[ offset + 15 ] = te[ 15 ]; return array; } } const _v1$1 = /*@__PURE__*/ new Vector3(); const _m1 = /*@__PURE__*/ new Matrix4(); const _zero = /*@__PURE__*/ new Vector3( 0, 0, 0 ); const _one = /*@__PURE__*/ new Vector3( 1, 1, 1 ); const _x = /*@__PURE__*/ new Vector3(); const _y = /*@__PURE__*/ new Vector3(); const _z = /*@__PURE__*/ new Vector3(); class Euler { constructor( x = 0, y = 0, z = 0, order = Euler.DefaultOrder ) { Object.defineProperty( this, 'isEuler', { value: true } ); this._x = x; this._y = y; this._z = z; this._order = order; } get x() { return this._x; } set x( value ) { this._x = value; this._onChangeCallback(); } get y() { return this._y; } set y( value ) { this._y = value; this._onChangeCallback(); } get z() { return this._z; } set z( value ) { this._z = value; this._onChangeCallback(); } get order() { return this._order; } set order( value ) { this._order = value; this._onChangeCallback(); } set( x, y, z, order ) { this._x = x; this._y = y; this._z = z; this._order = order || this._order; this._onChangeCallback(); return this; } clone() { return new this.constructor( this._x, this._y, this._z, this._order ); } copy( euler ) { this._x = euler._x; this._y = euler._y; this._z = euler._z; this._order = euler._order; this._onChangeCallback(); return this; } setFromRotationMatrix( m, order, update ) { const clamp = MathUtils.clamp; // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) const te = m.elements; const m11 = te[ 0 ], m12 = te[ 4 ], m13 = te[ 8 ]; const m21 = te[ 1 ], m22 = te[ 5 ], m23 = te[ 9 ]; const m31 = te[ 2 ], m32 = te[ 6 ], m33 = te[ 10 ]; order = order || this._order; switch ( order ) { case 'XYZ': this._y = Math.asin( clamp( m13, - 1, 1 ) ); if ( Math.abs( m13 ) < 0.9999999 ) { this._x = Math.atan2( - m23, m33 ); this._z = Math.atan2( - m12, m11 ); } else { this._x = Math.atan2( m32, m22 ); this._z = 0; } break; case 'YXZ': this._x = Math.asin( - clamp( m23, - 1, 1 ) ); if ( Math.abs( m23 ) < 0.9999999 ) { this._y = Math.atan2( m13, m33 ); this._z = Math.atan2( m21, m22 ); } else { this._y = Math.atan2( - m31, m11 ); this._z = 0; } break; case 'ZXY': this._x = Math.asin( clamp( m32, - 1, 1 ) ); if ( Math.abs( m32 ) < 0.9999999 ) { this._y = Math.atan2( - m31, m33 ); this._z = Math.atan2( - m12, m22 ); } else { this._y = 0; this._z = Math.atan2( m21, m11 ); } break; case 'ZYX': this._y = Math.asin( - clamp( m31, - 1, 1 ) ); if ( Math.abs( m31 ) < 0.9999999 ) { this._x = Math.atan2( m32, m33 ); this._z = Math.atan2( m21, m11 ); } else { this._x = 0; this._z = Math.atan2( - m12, m22 ); } break; case 'YZX': this._z = Math.asin( clamp( m21, - 1, 1 ) ); if ( Math.abs( m21 ) < 0.9999999 ) { this._x = Math.atan2( - m23, m22 ); this._y = Math.atan2( - m31, m11 ); } else { this._x = 0; this._y = Math.atan2( m13, m33 ); } break; case 'XZY': this._z = Math.asin( - clamp( m12, - 1, 1 ) ); if ( Math.abs( m12 ) < 0.9999999 ) { this._x = Math.atan2( m32, m22 ); this._y = Math.atan2( m13, m11 ); } else { this._x = Math.atan2( - m23, m33 ); this._y = 0; } break; default: console.warn( 'THREE.Euler: .setFromRotationMatrix() encountered an unknown order: ' + order ); } this._order = order; if ( update !== false ) this._onChangeCallback(); return this; } setFromQuaternion( q, order, update ) { _matrix.makeRotationFromQuaternion( q ); return this.setFromRotationMatrix( _matrix, order, update ); } setFromVector3( v, order ) { return this.set( v.x, v.y, v.z, order || this._order ); } reorder( newOrder ) { // WARNING: this discards revolution information -bhouston _quaternion$1.setFromEuler( this ); return this.setFromQuaternion( _quaternion$1, newOrder ); } equals( euler ) { return ( euler._x === this._x ) && ( euler._y === this._y ) && ( euler._z === this._z ) && ( euler._order === this._order ); } fromArray( array ) { this._x = array[ 0 ]; this._y = array[ 1 ]; this._z = array[ 2 ]; if ( array[ 3 ] !== undefined ) this._order = array[ 3 ]; this._onChangeCallback(); return this; } toArray( array = [], offset = 0 ) { array[ offset ] = this._x; array[ offset + 1 ] = this._y; array[ offset + 2 ] = this._z; array[ offset + 3 ] = this._order; return array; } toVector3( optionalResult ) { if ( optionalResult ) { return optionalResult.set( this._x, this._y, this._z ); } else { return new Vector3( this._x, this._y, this._z ); } } _onChange( callback ) { this._onChangeCallback = callback; return this; } _onChangeCallback() {} } Euler.DefaultOrder = 'XYZ'; Euler.RotationOrders = [ 'XYZ', 'YZX', 'ZXY', 'XZY', 'YXZ', 'ZYX' ]; const _matrix = /*@__PURE__*/ new Matrix4(); const _quaternion$1 = /*@__PURE__*/ new Quaternion(); class Layers { constructor() { this.mask = 1 | 0; } set( channel ) { this.mask = 1 << channel | 0; } enable( channel ) { this.mask |= 1 << channel | 0; } enableAll() { this.mask = 0xffffffff | 0; } toggle( channel ) { this.mask ^= 1 << channel | 0; } disable( channel ) { this.mask &= ~ ( 1 << channel | 0 ); } disableAll() { this.mask = 0; } test( layers ) { return ( this.mask & layers.mask ) !== 0; } } let _object3DId = 0; const _v1$2 = new Vector3(); const _q1 = new Quaternion(); const _m1$1 = new Matrix4(); const _target = new Vector3(); const _position = new Vector3(); const _scale = new Vector3(); const _quaternion$2 = new Quaternion(); const _xAxis = new Vector3( 1, 0, 0 ); const _yAxis = new Vector3( 0, 1, 0 ); const _zAxis = new Vector3( 0, 0, 1 ); const _addedEvent = { type: 'added' }; const _removedEvent = { type: 'removed' }; function Object3D() { Object.defineProperty( this, 'id', { value: _object3DId ++ } ); this.uuid = MathUtils.generateUUID(); this.name = ''; this.type = 'Object3D'; this.parent = null; this.children = []; this.up = Object3D.DefaultUp.clone(); const position = new Vector3(); const rotation = new Euler(); const quaternion = new Quaternion(); const scale = new Vector3( 1, 1, 1 ); function onRotationChange() { quaternion.setFromEuler( rotation, false ); } function onQuaternionChange() { rotation.setFromQuaternion( quaternion, undefined, false ); } rotation._onChange( onRotationChange ); quaternion._onChange( onQuaternionChange ); Object.defineProperties( this, { position: { configurable: true, enumerable: true, value: position }, rotation: { configurable: true, enumerable: true, value: rotation }, quaternion: { configurable: true, enumerable: true, value: quaternion }, scale: { configurable: true, enumerable: true, value: scale }, modelViewMatrix: { value: new Matrix4() }, normalMatrix: { value: new Matrix3() } } ); this.matrix = new Matrix4(); this.matrixWorld = new Matrix4(); this.matrixAutoUpdate = Object3D.DefaultMatrixAutoUpdate; this.matrixWorldNeedsUpdate = false; this.layers = new Layers(); this.visible = true; this.castShadow = false; this.receiveShadow = false; this.frustumCulled = true; this.renderOrder = 0; this.animations = []; this.userData = {}; } Object3D.DefaultUp = new Vector3( 0, 1, 0 ); Object3D.DefaultMatrixAutoUpdate = true; Object3D.prototype = Object.assign( Object.create( EventDispatcher.prototype ), { constructor: Object3D, isObject3D: true, onBeforeRender: function () {}, onAfterRender: function () {}, applyMatrix4: function ( matrix ) { if ( this.matrixAutoUpdate ) this.updateMatrix(); this.matrix.premultiply( matrix ); this.matrix.decompose( this.position, this.quaternion, this.scale ); }, applyQuaternion: function ( q ) { this.quaternion.premultiply( q ); return this; }, setRotationFromAxisAngle: function ( axis, angle ) { // assumes axis is normalized this.quaternion.setFromAxisAngle( axis, angle ); }, setRotationFromEuler: function ( euler ) { this.quaternion.setFromEuler( euler, true ); }, setRotationFromMatrix: function ( m ) { // assumes the upper 3x3 of m is a pure rotation matrix (i.e, unscaled) this.quaternion.setFromRotationMatrix( m ); }, setRotationFromQuaternion: function ( q ) { // assumes q is normalized this.quaternion.copy( q ); }, rotateOnAxis: function ( axis, angle ) { // rotate object on axis in object space // axis is assumed to be normalized _q1.setFromAxisAngle( axis, angle ); this.quaternion.multiply( _q1 ); return this; }, rotateOnWorldAxis: function ( axis, angle ) { // rotate object on axis in world space // axis is assumed to be normalized // method assumes no rotated parent _q1.setFromAxisAngle( axis, angle ); this.quaternion.premultiply( _q1 ); return this; }, rotateX: function ( angle ) { return this.rotateOnAxis( _xAxis, angle ); }, rotateY: function ( angle ) { return this.rotateOnAxis( _yAxis, angle ); }, rotateZ: function ( angle ) { return this.rotateOnAxis( _zAxis, angle ); }, translateOnAxis: function ( axis, distance ) { // translate object by distance along axis in object space // axis is assumed to be normalized _v1$2.copy( axis ).applyQuaternion( this.quaternion ); this.position.add( _v1$2.multiplyScalar( distance ) ); return this; }, translateX: function ( distance ) { return this.translateOnAxis( _xAxis, distance ); }, translateY: function ( distance ) { return this.translateOnAxis( _yAxis, distance ); }, translateZ: function ( distance ) { return this.translateOnAxis( _zAxis, distance ); }, localToWorld: function ( vector ) { return vector.applyMatrix4( this.matrixWorld ); }, worldToLocal: function ( vector ) { return vector.applyMatrix4( _m1$1.copy( this.matrixWorld ).invert() ); }, lookAt: function ( x, y, z ) { // This method does not support objects having non-uniformly-scaled parent(s) if ( x.isVector3 ) { _target.copy( x ); } else { _target.set( x, y, z ); } const parent = this.parent; this.updateWorldMatrix( true, false ); _position.setFromMatrixPosition( this.matrixWorld ); if ( this.isCamera || this.isLight ) { _m1$1.lookAt( _position, _target, this.up ); } else { _m1$1.lookAt( _target, _position, this.up ); } this.quaternion.setFromRotationMatrix( _m1$1 ); if ( parent ) { _m1$1.extractRotation( parent.matrixWorld ); _q1.setFromRotationMatrix( _m1$1 ); this.quaternion.premultiply( _q1.invert() ); } }, add: function ( object ) { if ( arguments.length > 1 ) { for ( let i = 0; i < arguments.length; i ++ ) { this.add( arguments[ i ] ); } return this; } if ( object === this ) { console.error( 'THREE.Object3D.add: object can\'t be added as a child of itself.', object ); return this; } if ( object && object.isObject3D ) { if ( object.parent !== null ) { object.parent.remove( object ); } object.parent = this; this.children.push( object ); object.dispatchEvent( _addedEvent ); } else { console.error( 'THREE.Object3D.add: object not an instance of THREE.Object3D.', object ); } return this; }, remove: function ( object ) { if ( arguments.length > 1 ) { for ( let i = 0; i < arguments.length; i ++ ) { this.remove( arguments[ i ] ); } return this; } const index = this.children.indexOf( object ); if ( index !== - 1 ) { object.parent = null; this.children.splice( index, 1 ); object.dispatchEvent( _removedEvent ); } return this; }, clear: function () { for ( let i = 0; i < this.children.length; i ++ ) { const object = this.children[ i ]; object.parent = null; object.dispatchEvent( _removedEvent ); } this.children.length = 0; return this; }, attach: function ( object ) { // adds object as a child of this, while maintaining the object's world transform this.updateWorldMatrix( true, false ); _m1$1.copy( this.matrixWorld ).invert(); if ( object.parent !== null ) { object.parent.updateWorldMatrix( true, false ); _m1$1.multiply( object.parent.matrixWorld ); } object.applyMatrix4( _m1$1 ); object.updateWorldMatrix( false, false ); this.add( object ); return this; }, getObjectById: function ( id ) { return this.getObjectByProperty( 'id', id ); }, getObjectByName: function ( name ) { return this.getObjectByProperty( 'name', name ); }, getObjectByProperty: function ( name, value ) { if ( this[ name ] === value ) return this; for ( let i = 0, l = this.children.length; i < l; i ++ ) { const child = this.children[ i ]; const object = child.getObjectByProperty( name, value ); if ( object !== undefined ) { return object; } } return undefined; }, getWorldPosition: function ( target ) { if ( target === undefined ) { console.warn( 'THREE.Object3D: .getWorldPosition() target is now required' ); target = new Vector3(); } this.updateWorldMatrix( true, false ); return target.setFromMatrixPosition( this.matrixWorld ); }, getWorldQuaternion: function ( target ) { if ( target === undefined ) { console.warn( 'THREE.Object3D: .getWorldQuaternion() target is now required' ); target = new Quaternion(); } this.updateWorldMatrix( true, false ); this.matrixWorld.decompose( _position, target, _scale ); return target; }, getWorldScale: function ( target ) { if ( target === undefined ) { console.warn( 'THREE.Object3D: .getWorldScale() target is now required' ); target = new Vector3(); } this.updateWorldMatrix( true, false ); this.matrixWorld.decompose( _position, _quaternion$2, target ); return target; }, getWorldDirection: function ( target ) { if ( target === undefined ) { console.warn( 'THREE.Object3D: .getWorldDirection() target is now required' ); target = new Vector3(); } this.updateWorldMatrix( true, false ); const e = this.matrixWorld.elements; return target.set( e[ 8 ], e[ 9 ], e[ 10 ] ).normalize(); }, raycast: function () {}, traverse: function ( callback ) { callback( this ); const children = this.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].traverse( callback ); } }, traverseVisible: function ( callback ) { if ( this.visible === false ) return; callback( this ); const children = this.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].traverseVisible( callback ); } }, traverseAncestors: function ( callback ) { const parent = this.parent; if ( parent !== null ) { callback( parent ); parent.traverseAncestors( callback ); } }, updateMatrix: function () { this.matrix.compose( this.position, this.quaternion, this.scale ); this.matrixWorldNeedsUpdate = true; }, updateMatrixWorld: function ( force ) { if ( this.matrixAutoUpdate ) this.updateMatrix(); if ( this.matrixWorldNeedsUpdate || force ) { if ( this.parent === null ) { this.matrixWorld.copy( this.matrix ); } else { this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix ); } this.matrixWorldNeedsUpdate = false; force = true; } // update children const children = this.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].updateMatrixWorld( force ); } }, updateWorldMatrix: function ( updateParents, updateChildren ) { const parent = this.parent; if ( updateParents === true && parent !== null ) { parent.updateWorldMatrix( true, false ); } if ( this.matrixAutoUpdate ) this.updateMatrix(); if ( this.parent === null ) { this.matrixWorld.copy( this.matrix ); } else { this.matrixWorld.multiplyMatrices( this.parent.matrixWorld, this.matrix ); } // update children if ( updateChildren === true ) { const children = this.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].updateWorldMatrix( false, true ); } } }, toJSON: function ( meta ) { // meta is a string when called from JSON.stringify const isRootObject = ( meta === undefined || typeof meta === 'string' ); const output = {}; // meta is a hash used to collect geometries, materials. // not providing it implies that this is the root object // being serialized. if ( isRootObject ) { // initialize meta obj meta = { geometries: {}, materials: {}, textures: {}, images: {}, shapes: {}, skeletons: {}, animations: {} }; output.metadata = { version: 4.5, type: 'Object', generator: 'Object3D.toJSON' }; } // standard Object3D serialization const object = {}; object.uuid = this.uuid; object.type = this.type; if ( this.name !== '' ) object.name = this.name; if ( this.castShadow === true ) object.castShadow = true; if ( this.receiveShadow === true ) object.receiveShadow = true; if ( this.visible === false ) object.visible = false; if ( this.frustumCulled === false ) object.frustumCulled = false; if ( this.renderOrder !== 0 ) object.renderOrder = this.renderOrder; if ( JSON.stringify( this.userData ) !== '{}' ) object.userData = this.userData; object.layers = this.layers.mask; object.matrix = this.matrix.toArray(); if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false; // object specific properties if ( this.isInstancedMesh ) { object.type = 'InstancedMesh'; object.count = this.count; object.instanceMatrix = this.instanceMatrix.toJSON(); } // function serialize( library, element ) { if ( library[ element.uuid ] === undefined ) { library[ element.uuid ] = element.toJSON( meta ); } return element.uuid; } if ( this.isMesh || this.isLine || this.isPoints ) { object.geometry = serialize( meta.geometries, this.geometry ); const parameters = this.geometry.parameters; if ( parameters !== undefined && parameters.shapes !== undefined ) { const shapes = parameters.shapes; if ( Array.isArray( shapes ) ) { for ( let i = 0, l = shapes.length; i < l; i ++ ) { const shape = shapes[ i ]; serialize( meta.shapes, shape ); } } else { serialize( meta.shapes, shapes ); } } } if ( this.isSkinnedMesh ) { object.bindMode = this.bindMode; object.bindMatrix = this.bindMatrix.toArray(); if ( this.skeleton !== undefined ) { serialize( meta.skeletons, this.skeleton ); object.skeleton = this.skeleton.uuid; } } if ( this.material !== undefined ) { if ( Array.isArray( this.material ) ) { const uuids = []; for ( let i = 0, l = this.material.length; i < l; i ++ ) { uuids.push( serialize( meta.materials, this.material[ i ] ) ); } object.material = uuids; } else { object.material = serialize( meta.materials, this.material ); } } // if ( this.children.length > 0 ) { object.children = []; for ( let i = 0; i < this.children.length; i ++ ) { object.children.push( this.children[ i ].toJSON( meta ).object ); } } // if ( this.animations.length > 0 ) { object.animations = []; for ( let i = 0; i < this.animations.length; i ++ ) { const animation = this.animations[ i ]; object.animations.push( serialize( meta.animations, animation ) ); } } if ( isRootObject ) { const geometries = extractFromCache( meta.geometries ); const materials = extractFromCache( meta.materials ); const textures = extractFromCache( meta.textures ); const images = extractFromCache( meta.images ); const shapes = extractFromCache( meta.shapes ); const skeletons = extractFromCache( meta.skeletons ); const animations = extractFromCache( meta.animations ); if ( geometries.length > 0 ) output.geometries = geometries; if ( materials.length > 0 ) output.materials = materials; if ( textures.length > 0 ) output.textures = textures; if ( images.length > 0 ) output.images = images; if ( shapes.length > 0 ) output.shapes = shapes; if ( skeletons.length > 0 ) output.skeletons = skeletons; if ( animations.length > 0 ) output.animations = animations; } output.object = object; return output; // extract data from the cache hash // remove metadata on each item // and return as array function extractFromCache( cache ) { const values = []; for ( const key in cache ) { const data = cache[ key ]; delete data.metadata; values.push( data ); } return values; } }, clone: function ( recursive ) { return new this.constructor().copy( this, recursive ); }, copy: function ( source, recursive = true ) { this.name = source.name; this.up.copy( source.up ); this.position.copy( source.position ); this.rotation.order = source.rotation.order; this.quaternion.copy( source.quaternion ); this.scale.copy( source.scale ); this.matrix.copy( source.matrix ); this.matrixWorld.copy( source.matrixWorld ); this.matrixAutoUpdate = source.matrixAutoUpdate; this.matrixWorldNeedsUpdate = source.matrixWorldNeedsUpdate; this.layers.mask = source.layers.mask; this.visible = source.visible; this.castShadow = source.castShadow; this.receiveShadow = source.receiveShadow; this.frustumCulled = source.frustumCulled; this.renderOrder = source.renderOrder; this.userData = JSON.parse( JSON.stringify( source.userData ) ); if ( recursive === true ) { for ( let i = 0; i < source.children.length; i ++ ) { const child = source.children[ i ]; this.add( child.clone() ); } } return this; } } ); const _vector1 = /*@__PURE__*/ new Vector3(); const _vector2 = /*@__PURE__*/ new Vector3(); const _normalMatrix = /*@__PURE__*/ new Matrix3(); class Plane { constructor( normal, constant ) { Object.defineProperty( this, 'isPlane', { value: true } ); // normal is assumed to be normalized this.normal = ( normal !== undefined ) ? normal : new Vector3( 1, 0, 0 ); this.constant = ( constant !== undefined ) ? constant : 0; } set( normal, constant ) { this.normal.copy( normal ); this.constant = constant; return this; } setComponents( x, y, z, w ) { this.normal.set( x, y, z ); this.constant = w; return this; } setFromNormalAndCoplanarPoint( normal, point ) { this.normal.copy( normal ); this.constant = - point.dot( this.normal ); return this; } setFromCoplanarPoints( a, b, c ) { const normal = _vector1.subVectors( c, b ).cross( _vector2.subVectors( a, b ) ).normalize(); // Q: should an error be thrown if normal is zero (e.g. degenerate plane)? this.setFromNormalAndCoplanarPoint( normal, a ); return this; } clone() { return new this.constructor().copy( this ); } copy( plane ) { this.normal.copy( plane.normal ); this.constant = plane.constant; return this; } normalize() { // Note: will lead to a divide by zero if the plane is invalid. const inverseNormalLength = 1.0 / this.normal.length(); this.normal.multiplyScalar( inverseNormalLength ); this.constant *= inverseNormalLength; return this; } negate() { this.constant *= - 1; this.normal.negate(); return this; } distanceToPoint( point ) { return this.normal.dot( point ) + this.constant; } distanceToSphere( sphere ) { return this.distanceToPoint( sphere.center ) - sphere.radius; } projectPoint( point, target ) { if ( target === undefined ) { console.warn( 'THREE.Plane: .projectPoint() target is now required' ); target = new Vector3(); } return target.copy( this.normal ).multiplyScalar( - this.distanceToPoint( point ) ).add( point ); } intersectLine( line, target ) { if ( target === undefined ) { console.warn( 'THREE.Plane: .intersectLine() target is now required' ); target = new Vector3(); } const direction = line.delta( _vector1 ); const denominator = this.normal.dot( direction ); if ( denominator === 0 ) { // line is coplanar, return origin if ( this.distanceToPoint( line.start ) === 0 ) { return target.copy( line.start ); } // Unsure if this is the correct method to handle this case. return undefined; } const t = - ( line.start.dot( this.normal ) + this.constant ) / denominator; if ( t < 0 || t > 1 ) { return undefined; } return target.copy( direction ).multiplyScalar( t ).add( line.start ); } intersectsLine( line ) { // Note: this tests if a line intersects the plane, not whether it (or its end-points) are coplanar with it. const startSign = this.distanceToPoint( line.start ); const endSign = this.distanceToPoint( line.end ); return ( startSign < 0 && endSign > 0 ) || ( endSign < 0 && startSign > 0 ); } intersectsBox( box ) { return box.intersectsPlane( this ); } intersectsSphere( sphere ) { return sphere.intersectsPlane( this ); } coplanarPoint( target ) { if ( target === undefined ) { console.warn( 'THREE.Plane: .coplanarPoint() target is now required' ); target = new Vector3(); } return target.copy( this.normal ).multiplyScalar( - this.constant ); } applyMatrix4( matrix, optionalNormalMatrix ) { const normalMatrix = optionalNormalMatrix || _normalMatrix.getNormalMatrix( matrix ); const referencePoint = this.coplanarPoint( _vector1 ).applyMatrix4( matrix ); const normal = this.normal.applyMatrix3( normalMatrix ).normalize(); this.constant = - referencePoint.dot( normal ); return this; } translate( offset ) { this.constant -= offset.dot( this.normal ); return this; } equals( plane ) { return plane.normal.equals( this.normal ) && ( plane.constant === this.constant ); } } const _v0$1 = /*@__PURE__*/ new Vector3(); const _v1$3 = /*@__PURE__*/ new Vector3(); const _v2$1 = /*@__PURE__*/ new Vector3(); const _v3 = /*@__PURE__*/ new Vector3(); const _vab = /*@__PURE__*/ new Vector3(); const _vac = /*@__PURE__*/ new Vector3(); const _vbc = /*@__PURE__*/ new Vector3(); const _vap = /*@__PURE__*/ new Vector3(); const _vbp = /*@__PURE__*/ new Vector3(); const _vcp = /*@__PURE__*/ new Vector3(); class Triangle { constructor( a, b, c ) { this.a = ( a !== undefined ) ? a : new Vector3(); this.b = ( b !== undefined ) ? b : new Vector3(); this.c = ( c !== undefined ) ? c : new Vector3(); } static getNormal( a, b, c, target ) { if ( target === undefined ) { console.warn( 'THREE.Triangle: .getNormal() target is now required' ); target = new Vector3(); } target.subVectors( c, b ); _v0$1.subVectors( a, b ); target.cross( _v0$1 ); const targetLengthSq = target.lengthSq(); if ( targetLengthSq > 0 ) { return target.multiplyScalar( 1 / Math.sqrt( targetLengthSq ) ); } return target.set( 0, 0, 0 ); } // static/instance method to calculate barycentric coordinates // based on: http://www.blackpawn.com/texts/pointinpoly/default.html static getBarycoord( point, a, b, c, target ) { _v0$1.subVectors( c, a ); _v1$3.subVectors( b, a ); _v2$1.subVectors( point, a ); const dot00 = _v0$1.dot( _v0$1 ); const dot01 = _v0$1.dot( _v1$3 ); const dot02 = _v0$1.dot( _v2$1 ); const dot11 = _v1$3.dot( _v1$3 ); const dot12 = _v1$3.dot( _v2$1 ); const denom = ( dot00 * dot11 - dot01 * dot01 ); if ( target === undefined ) { console.warn( 'THREE.Triangle: .getBarycoord() target is now required' ); target = new Vector3(); } // collinear or singular triangle if ( denom === 0 ) { // arbitrary location outside of triangle? // not sure if this is the best idea, maybe should be returning undefined return target.set( - 2, - 1, - 1 ); } const invDenom = 1 / denom; const u = ( dot11 * dot02 - dot01 * dot12 ) * invDenom; const v = ( dot00 * dot12 - dot01 * dot02 ) * invDenom; // barycentric coordinates must always sum to 1 return target.set( 1 - u - v, v, u ); } static containsPoint( point, a, b, c ) { this.getBarycoord( point, a, b, c, _v3 ); return ( _v3.x >= 0 ) && ( _v3.y >= 0 ) && ( ( _v3.x + _v3.y ) <= 1 ); } static getUV( point, p1, p2, p3, uv1, uv2, uv3, target ) { this.getBarycoord( point, p1, p2, p3, _v3 ); target.set( 0, 0 ); target.addScaledVector( uv1, _v3.x ); target.addScaledVector( uv2, _v3.y ); target.addScaledVector( uv3, _v3.z ); return target; } static isFrontFacing( a, b, c, direction ) { _v0$1.subVectors( c, b ); _v1$3.subVectors( a, b ); // strictly front facing return ( _v0$1.cross( _v1$3 ).dot( direction ) < 0 ) ? true : false; } set( a, b, c ) { this.a.copy( a ); this.b.copy( b ); this.c.copy( c ); return this; } setFromPointsAndIndices( points, i0, i1, i2 ) { this.a.copy( points[ i0 ] ); this.b.copy( points[ i1 ] ); this.c.copy( points[ i2 ] ); return this; } clone() { return new this.constructor().copy( this ); } copy( triangle ) { this.a.copy( triangle.a ); this.b.copy( triangle.b ); this.c.copy( triangle.c ); return this; } getArea() { _v0$1.subVectors( this.c, this.b ); _v1$3.subVectors( this.a, this.b ); return _v0$1.cross( _v1$3 ).length() * 0.5; } getMidpoint( target ) { if ( target === undefined ) { console.warn( 'THREE.Triangle: .getMidpoint() target is now required' ); target = new Vector3(); } return target.addVectors( this.a, this.b ).add( this.c ).multiplyScalar( 1 / 3 ); } getNormal( target ) { return Triangle.getNormal( this.a, this.b, this.c, target ); } getPlane( target ) { if ( target === undefined ) { console.warn( 'THREE.Triangle: .getPlane() target is now required' ); target = new Plane(); } return target.setFromCoplanarPoints( this.a, this.b, this.c ); } getBarycoord( point, target ) { return Triangle.getBarycoord( point, this.a, this.b, this.c, target ); } getUV( point, uv1, uv2, uv3, target ) { return Triangle.getUV( point, this.a, this.b, this.c, uv1, uv2, uv3, target ); } containsPoint( point ) { return Triangle.containsPoint( point, this.a, this.b, this.c ); } isFrontFacing( direction ) { return Triangle.isFrontFacing( this.a, this.b, this.c, direction ); } intersectsBox( box ) { return box.intersectsTriangle( this ); } closestPointToPoint( p, target ) { if ( target === undefined ) { console.warn( 'THREE.Triangle: .closestPointToPoint() target is now required' ); target = new Vector3(); } const a = this.a, b = this.b, c = this.c; let v, w; // algorithm thanks to Real-Time Collision Detection by Christer Ericson, // published by Morgan Kaufmann Publishers, (c) 2005 Elsevier Inc., // under the accompanying license; see chapter 5.1.5 for detailed explanation. // basically, we're distinguishing which of the voronoi regions of the triangle // the point lies in with the minimum amount of redundant computation. _vab.subVectors( b, a ); _vac.subVectors( c, a ); _vap.subVectors( p, a ); const d1 = _vab.dot( _vap ); const d2 = _vac.dot( _vap ); if ( d1 <= 0 && d2 <= 0 ) { // vertex region of A; barycentric coords (1, 0, 0) return target.copy( a ); } _vbp.subVectors( p, b ); const d3 = _vab.dot( _vbp ); const d4 = _vac.dot( _vbp ); if ( d3 >= 0 && d4 <= d3 ) { // vertex region of B; barycentric coords (0, 1, 0) return target.copy( b ); } const vc = d1 * d4 - d3 * d2; if ( vc <= 0 && d1 >= 0 && d3 <= 0 ) { v = d1 / ( d1 - d3 ); // edge region of AB; barycentric coords (1-v, v, 0) return target.copy( a ).addScaledVector( _vab, v ); } _vcp.subVectors( p, c ); const d5 = _vab.dot( _vcp ); const d6 = _vac.dot( _vcp ); if ( d6 >= 0 && d5 <= d6 ) { // vertex region of C; barycentric coords (0, 0, 1) return target.copy( c ); } const vb = d5 * d2 - d1 * d6; if ( vb <= 0 && d2 >= 0 && d6 <= 0 ) { w = d2 / ( d2 - d6 ); // edge region of AC; barycentric coords (1-w, 0, w) return target.copy( a ).addScaledVector( _vac, w ); } const va = d3 * d6 - d5 * d4; if ( va <= 0 && ( d4 - d3 ) >= 0 && ( d5 - d6 ) >= 0 ) { _vbc.subVectors( c, b ); w = ( d4 - d3 ) / ( ( d4 - d3 ) + ( d5 - d6 ) ); // edge region of BC; barycentric coords (0, 1-w, w) return target.copy( b ).addScaledVector( _vbc, w ); // edge region of BC } // face region const denom = 1 / ( va + vb + vc ); // u = va * denom v = vb * denom; w = vc * denom; return target.copy( a ).addScaledVector( _vab, v ).addScaledVector( _vac, w ); } equals( triangle ) { return triangle.a.equals( this.a ) && triangle.b.equals( this.b ) && triangle.c.equals( this.c ); } } const _colorKeywords = { 'aliceblue': 0xF0F8FF, 'antiquewhite': 0xFAEBD7, 'aqua': 0x00FFFF, 'aquamarine': 0x7FFFD4, 'azure': 0xF0FFFF, 'beige': 0xF5F5DC, 'bisque': 0xFFE4C4, 'black': 0x000000, 'blanchedalmond': 0xFFEBCD, 'blue': 0x0000FF, 'blueviolet': 0x8A2BE2, 'brown': 0xA52A2A, 'burlywood': 0xDEB887, 'cadetblue': 0x5F9EA0, 'chartreuse': 0x7FFF00, 'chocolate': 0xD2691E, 'coral': 0xFF7F50, 'cornflowerblue': 0x6495ED, 'cornsilk': 0xFFF8DC, 'crimson': 0xDC143C, 'cyan': 0x00FFFF, 'darkblue': 0x00008B, 'darkcyan': 0x008B8B, 'darkgoldenrod': 0xB8860B, 'darkgray': 0xA9A9A9, 'darkgreen': 0x006400, 'darkgrey': 0xA9A9A9, 'darkkhaki': 0xBDB76B, 'darkmagenta': 0x8B008B, 'darkolivegreen': 0x556B2F, 'darkorange': 0xFF8C00, 'darkorchid': 0x9932CC, 'darkred': 0x8B0000, 'darksalmon': 0xE9967A, 'darkseagreen': 0x8FBC8F, 'darkslateblue': 0x483D8B, 'darkslategray': 0x2F4F4F, 'darkslategrey': 0x2F4F4F, 'darkturquoise': 0x00CED1, 'darkviolet': 0x9400D3, 'deeppink': 0xFF1493, 'deepskyblue': 0x00BFFF, 'dimgray': 0x696969, 'dimgrey': 0x696969, 'dodgerblue': 0x1E90FF, 'firebrick': 0xB22222, 'floralwhite': 0xFFFAF0, 'forestgreen': 0x228B22, 'fuchsia': 0xFF00FF, 'gainsboro': 0xDCDCDC, 'ghostwhite': 0xF8F8FF, 'gold': 0xFFD700, 'goldenrod': 0xDAA520, 'gray': 0x808080, 'green': 0x008000, 'greenyellow': 0xADFF2F, 'grey': 0x808080, 'honeydew': 0xF0FFF0, 'hotpink': 0xFF69B4, 'indianred': 0xCD5C5C, 'indigo': 0x4B0082, 'ivory': 0xFFFFF0, 'khaki': 0xF0E68C, 'lavender': 0xE6E6FA, 'lavenderblush': 0xFFF0F5, 'lawngreen': 0x7CFC00, 'lemonchiffon': 0xFFFACD, 'lightblue': 0xADD8E6, 'lightcoral': 0xF08080, 'lightcyan': 0xE0FFFF, 'lightgoldenrodyellow': 0xFAFAD2, 'lightgray': 0xD3D3D3, 'lightgreen': 0x90EE90, 'lightgrey': 0xD3D3D3, 'lightpink': 0xFFB6C1, 'lightsalmon': 0xFFA07A, 'lightseagreen': 0x20B2AA, 'lightskyblue': 0x87CEFA, 'lightslategray': 0x778899, 'lightslategrey': 0x778899, 'lightsteelblue': 0xB0C4DE, 'lightyellow': 0xFFFFE0, 'lime': 0x00FF00, 'limegreen': 0x32CD32, 'linen': 0xFAF0E6, 'magenta': 0xFF00FF, 'maroon': 0x800000, 'mediumaquamarine': 0x66CDAA, 'mediumblue': 0x0000CD, 'mediumorchid': 0xBA55D3, 'mediumpurple': 0x9370DB, 'mediumseagreen': 0x3CB371, 'mediumslateblue': 0x7B68EE, 'mediumspringgreen': 0x00FA9A, 'mediumturquoise': 0x48D1CC, 'mediumvioletred': 0xC71585, 'midnightblue': 0x191970, 'mintcream': 0xF5FFFA, 'mistyrose': 0xFFE4E1, 'moccasin': 0xFFE4B5, 'navajowhite': 0xFFDEAD, 'navy': 0x000080, 'oldlace': 0xFDF5E6, 'olive': 0x808000, 'olivedrab': 0x6B8E23, 'orange': 0xFFA500, 'orangered': 0xFF4500, 'orchid': 0xDA70D6, 'palegoldenrod': 0xEEE8AA, 'palegreen': 0x98FB98, 'paleturquoise': 0xAFEEEE, 'palevioletred': 0xDB7093, 'papayawhip': 0xFFEFD5, 'peachpuff': 0xFFDAB9, 'peru': 0xCD853F, 'pink': 0xFFC0CB, 'plum': 0xDDA0DD, 'powderblue': 0xB0E0E6, 'purple': 0x800080, 'rebeccapurple': 0x663399, 'red': 0xFF0000, 'rosybrown': 0xBC8F8F, 'royalblue': 0x4169E1, 'saddlebrown': 0x8B4513, 'salmon': 0xFA8072, 'sandybrown': 0xF4A460, 'seagreen': 0x2E8B57, 'seashell': 0xFFF5EE, 'sienna': 0xA0522D, 'silver': 0xC0C0C0, 'skyblue': 0x87CEEB, 'slateblue': 0x6A5ACD, 'slategray': 0x708090, 'slategrey': 0x708090, 'snow': 0xFFFAFA, 'springgreen': 0x00FF7F, 'steelblue': 0x4682B4, 'tan': 0xD2B48C, 'teal': 0x008080, 'thistle': 0xD8BFD8, 'tomato': 0xFF6347, 'turquoise': 0x40E0D0, 'violet': 0xEE82EE, 'wheat': 0xF5DEB3, 'white': 0xFFFFFF, 'whitesmoke': 0xF5F5F5, 'yellow': 0xFFFF00, 'yellowgreen': 0x9ACD32 }; const _hslA = { h: 0, s: 0, l: 0 }; const _hslB = { h: 0, s: 0, l: 0 }; function hue2rgb( p, q, t ) { if ( t < 0 ) t += 1; if ( t > 1 ) t -= 1; if ( t < 1 / 6 ) return p + ( q - p ) * 6 * t; if ( t < 1 / 2 ) return q; if ( t < 2 / 3 ) return p + ( q - p ) * 6 * ( 2 / 3 - t ); return p; } function SRGBToLinear( c ) { return ( c < 0.04045 ) ? c * 0.0773993808 : Math.pow( c * 0.9478672986 + 0.0521327014, 2.4 ); } function LinearToSRGB( c ) { return ( c < 0.0031308 ) ? c * 12.92 : 1.055 * ( Math.pow( c, 0.41666 ) ) - 0.055; } class Color { constructor( r, g, b ) { Object.defineProperty( this, 'isColor', { value: true } ); if ( g === undefined && b === undefined ) { // r is THREE.Color, hex or string return this.set( r ); } return this.setRGB( r, g, b ); } set( value ) { if ( value && value.isColor ) { this.copy( value ); } else if ( typeof value === 'number' ) { this.setHex( value ); } else if ( typeof value === 'string' ) { this.setStyle( value ); } return this; } setScalar( scalar ) { this.r = scalar; this.g = scalar; this.b = scalar; return this; } setHex( hex ) { hex = Math.floor( hex ); this.r = ( hex >> 16 & 255 ) / 255; this.g = ( hex >> 8 & 255 ) / 255; this.b = ( hex & 255 ) / 255; return this; } setRGB( r, g, b ) { this.r = r; this.g = g; this.b = b; return this; } setHSL( h, s, l ) { // h,s,l ranges are in 0.0 - 1.0 h = MathUtils.euclideanModulo( h, 1 ); s = MathUtils.clamp( s, 0, 1 ); l = MathUtils.clamp( l, 0, 1 ); if ( s === 0 ) { this.r = this.g = this.b = l; } else { const p = l <= 0.5 ? l * ( 1 + s ) : l + s - ( l * s ); const q = ( 2 * l ) - p; this.r = hue2rgb( q, p, h + 1 / 3 ); this.g = hue2rgb( q, p, h ); this.b = hue2rgb( q, p, h - 1 / 3 ); } return this; } setStyle( style ) { function handleAlpha( string ) { if ( string === undefined ) return; if ( parseFloat( string ) < 1 ) { console.warn( 'THREE.Color: Alpha component of ' + style + ' will be ignored.' ); } } let m; if ( m = /^((?:rgb|hsl)a?)\(\s*([^\)]*)\)/.exec( style ) ) { // rgb / hsl let color; const name = m[ 1 ]; const components = m[ 2 ]; switch ( name ) { case 'rgb': case 'rgba': if ( color = /^(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) { // rgb(255,0,0) rgba(255,0,0,0.5) this.r = Math.min( 255, parseInt( color[ 1 ], 10 ) ) / 255; this.g = Math.min( 255, parseInt( color[ 2 ], 10 ) ) / 255; this.b = Math.min( 255, parseInt( color[ 3 ], 10 ) ) / 255; handleAlpha( color[ 4 ] ); return this; } if ( color = /^(\d+)\%\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) { // rgb(100%,0%,0%) rgba(100%,0%,0%,0.5) this.r = Math.min( 100, parseInt( color[ 1 ], 10 ) ) / 100; this.g = Math.min( 100, parseInt( color[ 2 ], 10 ) ) / 100; this.b = Math.min( 100, parseInt( color[ 3 ], 10 ) ) / 100; handleAlpha( color[ 4 ] ); return this; } break; case 'hsl': case 'hsla': if ( color = /^(\d*\.?\d+)\s*,\s*(\d+)\%\s*,\s*(\d+)\%\s*(?:,\s*(\d*\.?\d+)\s*)?$/.exec( components ) ) { // hsl(120,50%,50%) hsla(120,50%,50%,0.5) const h = parseFloat( color[ 1 ] ) / 360; const s = parseInt( color[ 2 ], 10 ) / 100; const l = parseInt( color[ 3 ], 10 ) / 100; handleAlpha( color[ 4 ] ); return this.setHSL( h, s, l ); } break; } } else if ( m = /^\#([A-Fa-f\d]+)$/.exec( style ) ) { // hex color const hex = m[ 1 ]; const size = hex.length; if ( size === 3 ) { // #ff0 this.r = parseInt( hex.charAt( 0 ) + hex.charAt( 0 ), 16 ) / 255; this.g = parseInt( hex.charAt( 1 ) + hex.charAt( 1 ), 16 ) / 255; this.b = parseInt( hex.charAt( 2 ) + hex.charAt( 2 ), 16 ) / 255; return this; } else if ( size === 6 ) { // #ff0000 this.r = parseInt( hex.charAt( 0 ) + hex.charAt( 1 ), 16 ) / 255; this.g = parseInt( hex.charAt( 2 ) + hex.charAt( 3 ), 16 ) / 255; this.b = parseInt( hex.charAt( 4 ) + hex.charAt( 5 ), 16 ) / 255; return this; } } if ( style && style.length > 0 ) { return this.setColorName( style ); } return this; } setColorName( style ) { // color keywords const hex = _colorKeywords[ style ]; if ( hex !== undefined ) { // red this.setHex( hex ); } else { // unknown color console.warn( 'THREE.Color: Unknown color ' + style ); } return this; } clone() { return new this.constructor( this.r, this.g, this.b ); } copy( color ) { this.r = color.r; this.g = color.g; this.b = color.b; return this; } copyGammaToLinear( color, gammaFactor = 2.0 ) { this.r = Math.pow( color.r, gammaFactor ); this.g = Math.pow( color.g, gammaFactor ); this.b = Math.pow( color.b, gammaFactor ); return this; } copyLinearToGamma( color, gammaFactor = 2.0 ) { const safeInverse = ( gammaFactor > 0 ) ? ( 1.0 / gammaFactor ) : 1.0; this.r = Math.pow( color.r, safeInverse ); this.g = Math.pow( color.g, safeInverse ); this.b = Math.pow( color.b, safeInverse ); return this; } convertGammaToLinear( gammaFactor ) { this.copyGammaToLinear( this, gammaFactor ); return this; } convertLinearToGamma( gammaFactor ) { this.copyLinearToGamma( this, gammaFactor ); return this; } copySRGBToLinear( color ) { this.r = SRGBToLinear( color.r ); this.g = SRGBToLinear( color.g ); this.b = SRGBToLinear( color.b ); return this; } copyLinearToSRGB( color ) { this.r = LinearToSRGB( color.r ); this.g = LinearToSRGB( color.g ); this.b = LinearToSRGB( color.b ); return this; } convertSRGBToLinear() { this.copySRGBToLinear( this ); return this; } convertLinearToSRGB() { this.copyLinearToSRGB( this ); return this; } getHex() { return ( this.r * 255 ) << 16 ^ ( this.g * 255 ) << 8 ^ ( this.b * 255 ) << 0; } getHexString() { return ( '000000' + this.getHex().toString( 16 ) ).slice( - 6 ); } getHSL( target ) { // h,s,l ranges are in 0.0 - 1.0 if ( target === undefined ) { console.warn( 'THREE.Color: .getHSL() target is now required' ); target = { h: 0, s: 0, l: 0 }; } const r = this.r, g = this.g, b = this.b; const max = Math.max( r, g, b ); const min = Math.min( r, g, b ); let hue, saturation; const lightness = ( min + max ) / 2.0; if ( min === max ) { hue = 0; saturation = 0; } else { const delta = max - min; saturation = lightness <= 0.5 ? delta / ( max + min ) : delta / ( 2 - max - min ); switch ( max ) { case r: hue = ( g - b ) / delta + ( g < b ? 6 : 0 ); break; case g: hue = ( b - r ) / delta + 2; break; case b: hue = ( r - g ) / delta + 4; break; } hue /= 6; } target.h = hue; target.s = saturation; target.l = lightness; return target; } getStyle() { return 'rgb(' + ( ( this.r * 255 ) | 0 ) + ',' + ( ( this.g * 255 ) | 0 ) + ',' + ( ( this.b * 255 ) | 0 ) + ')'; } offsetHSL( h, s, l ) { this.getHSL( _hslA ); _hslA.h += h; _hslA.s += s; _hslA.l += l; this.setHSL( _hslA.h, _hslA.s, _hslA.l ); return this; } add( color ) { this.r += color.r; this.g += color.g; this.b += color.b; return this; } addColors( color1, color2 ) { this.r = color1.r + color2.r; this.g = color1.g + color2.g; this.b = color1.b + color2.b; return this; } addScalar( s ) { this.r += s; this.g += s; this.b += s; return this; } sub( color ) { this.r = Math.max( 0, this.r - color.r ); this.g = Math.max( 0, this.g - color.g ); this.b = Math.max( 0, this.b - color.b ); return this; } multiply( color ) { this.r *= color.r; this.g *= color.g; this.b *= color.b; return this; } multiplyScalar( s ) { this.r *= s; this.g *= s; this.b *= s; return this; } lerp( color, alpha ) { this.r += ( color.r - this.r ) * alpha; this.g += ( color.g - this.g ) * alpha; this.b += ( color.b - this.b ) * alpha; return this; } lerpHSL( color, alpha ) { this.getHSL( _hslA ); color.getHSL( _hslB ); const h = MathUtils.lerp( _hslA.h, _hslB.h, alpha ); const s = MathUtils.lerp( _hslA.s, _hslB.s, alpha ); const l = MathUtils.lerp( _hslA.l, _hslB.l, alpha ); this.setHSL( h, s, l ); return this; } equals( c ) { return ( c.r === this.r ) && ( c.g === this.g ) && ( c.b === this.b ); } fromArray( array, offset = 0 ) { this.r = array[ offset ]; this.g = array[ offset + 1 ]; this.b = array[ offset + 2 ]; return this; } toArray( array = [], offset = 0 ) { array[ offset ] = this.r; array[ offset + 1 ] = this.g; array[ offset + 2 ] = this.b; return array; } fromBufferAttribute( attribute, index ) { this.r = attribute.getX( index ); this.g = attribute.getY( index ); this.b = attribute.getZ( index ); if ( attribute.normalized === true ) { // assuming Uint8Array this.r /= 255; this.g /= 255; this.b /= 255; } return this; } toJSON() { return this.getHex(); } } Color.NAMES = _colorKeywords; Color.prototype.r = 1; Color.prototype.g = 1; Color.prototype.b = 1; class Face3 { constructor( a, b, c, normal, color, materialIndex = 0 ) { this.a = a; this.b = b; this.c = c; this.normal = ( normal && normal.isVector3 ) ? normal : new Vector3(); this.vertexNormals = Array.isArray( normal ) ? normal : []; this.color = ( color && color.isColor ) ? color : new Color(); this.vertexColors = Array.isArray( color ) ? color : []; this.materialIndex = materialIndex; } clone() { return new this.constructor().copy( this ); } copy( source ) { this.a = source.a; this.b = source.b; this.c = source.c; this.normal.copy( source.normal ); this.color.copy( source.color ); this.materialIndex = source.materialIndex; for ( let i = 0, il = source.vertexNormals.length; i < il; i ++ ) { this.vertexNormals[ i ] = source.vertexNormals[ i ].clone(); } for ( let i = 0, il = source.vertexColors.length; i < il; i ++ ) { this.vertexColors[ i ] = source.vertexColors[ i ].clone(); } return this; } } let materialId = 0; function Material() { Object.defineProperty( this, 'id', { value: materialId ++ } ); this.uuid = MathUtils.generateUUID(); this.name = ''; this.type = 'Material'; this.fog = true; this.blending = NormalBlending; this.side = FrontSide; this.flatShading = false; this.vertexColors = false; this.opacity = 1; this.transparent = false; this.blendSrc = SrcAlphaFactor; this.blendDst = OneMinusSrcAlphaFactor; this.blendEquation = AddEquation; this.blendSrcAlpha = null; this.blendDstAlpha = null; this.blendEquationAlpha = null; this.depthFunc = LessEqualDepth; this.depthTest = true; this.depthWrite = true; this.stencilWriteMask = 0xff; this.stencilFunc = AlwaysStencilFunc; this.stencilRef = 0; this.stencilFuncMask = 0xff; this.stencilFail = KeepStencilOp; this.stencilZFail = KeepStencilOp; this.stencilZPass = KeepStencilOp; this.stencilWrite = false; this.clippingPlanes = null; this.clipIntersection = false; this.clipShadows = false; this.shadowSide = null; this.colorWrite = true; this.precision = null; // override the renderer's default precision for this material this.polygonOffset = false; this.polygonOffsetFactor = 0; this.polygonOffsetUnits = 0; this.dithering = false; this.alphaTest = 0; this.premultipliedAlpha = false; this.visible = true; this.toneMapped = true; this.userData = {}; this.version = 0; } Material.prototype = Object.assign( Object.create( EventDispatcher.prototype ), { constructor: Material, isMaterial: true, onBeforeCompile: function ( /* shaderobject, renderer */ ) {}, customProgramCacheKey: function () { return this.onBeforeCompile.toString(); }, setValues: function ( values ) { if ( values === undefined ) return; for ( const key in values ) { const newValue = values[ key ]; if ( newValue === undefined ) { console.warn( 'THREE.Material: \'' + key + '\' parameter is undefined.' ); continue; } // for backward compatability if shading is set in the constructor if ( key === 'shading' ) { console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' ); this.flatShading = ( newValue === FlatShading$1 ) ? true : false; continue; } const currentValue = this[ key ]; if ( currentValue === undefined ) { //console.warn( 'THREE.' + this.type + ': \'' + key + '\' is not a property of this material.' ); continue; } if ( currentValue && currentValue.isColor ) { currentValue.set( newValue ); } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) { currentValue.copy( newValue ); } else { this[ key ] = newValue; } } }, toJSON: function ( meta ) { const isRoot = ( meta === undefined || typeof meta === 'string' ); if ( isRoot ) { meta = { textures: {}, images: {} }; } const data = { metadata: { version: 4.5, type: 'Material', generator: 'Material.toJSON' } }; // standard Material serialization data.uuid = this.uuid; data.type = this.type; if ( this.name !== '' ) data.name = this.name; if ( this.color && this.color.isColor ) data.color = this.color.getHex(); if ( this.roughness !== undefined ) data.roughness = this.roughness; if ( this.metalness !== undefined ) data.metalness = this.metalness; if ( this.sheen && this.sheen.isColor ) data.sheen = this.sheen.getHex(); if ( this.emissive && this.emissive.isColor ) data.emissive = this.emissive.getHex(); if ( this.emissiveIntensity && this.emissiveIntensity !== 1 ) data.emissiveIntensity = this.emissiveIntensity; if ( this.specular && this.specular.isColor ) data.specular = this.specular.getHex(); if ( this.shininess !== undefined ) data.shininess = this.shininess; if ( this.clearcoat !== undefined ) data.clearcoat = this.clearcoat; if ( this.clearcoatRoughness !== undefined ) data.clearcoatRoughness = this.clearcoatRoughness; if ( this.clearcoatMap && this.clearcoatMap.isTexture ) { data.clearcoatMap = this.clearcoatMap.toJSON( meta ).uuid; } if ( this.clearcoatRoughnessMap && this.clearcoatRoughnessMap.isTexture ) { data.clearcoatRoughnessMap = this.clearcoatRoughnessMap.toJSON( meta ).uuid; } if ( this.clearcoatNormalMap && this.clearcoatNormalMap.isTexture ) { data.clearcoatNormalMap = this.clearcoatNormalMap.toJSON( meta ).uuid; data.clearcoatNormalScale = this.clearcoatNormalScale.toArray(); } if ( this.map && this.map.isTexture ) data.map = this.map.toJSON( meta ).uuid; if ( this.matcap && this.matcap.isTexture ) data.matcap = this.matcap.toJSON( meta ).uuid; if ( this.alphaMap && this.alphaMap.isTexture ) data.alphaMap = this.alphaMap.toJSON( meta ).uuid; if ( this.lightMap && this.lightMap.isTexture ) data.lightMap = this.lightMap.toJSON( meta ).uuid; if ( this.aoMap && this.aoMap.isTexture ) { data.aoMap = this.aoMap.toJSON( meta ).uuid; data.aoMapIntensity = this.aoMapIntensity; } if ( this.bumpMap && this.bumpMap.isTexture ) { data.bumpMap = this.bumpMap.toJSON( meta ).uuid; data.bumpScale = this.bumpScale; } if ( this.normalMap && this.normalMap.isTexture ) { data.normalMap = this.normalMap.toJSON( meta ).uuid; data.normalMapType = this.normalMapType; data.normalScale = this.normalScale.toArray(); } if ( this.displacementMap && this.displacementMap.isTexture ) { data.displacementMap = this.displacementMap.toJSON( meta ).uuid; data.displacementScale = this.displacementScale; data.displacementBias = this.displacementBias; } if ( this.roughnessMap && this.roughnessMap.isTexture ) data.roughnessMap = this.roughnessMap.toJSON( meta ).uuid; if ( this.metalnessMap && this.metalnessMap.isTexture ) data.metalnessMap = this.metalnessMap.toJSON( meta ).uuid; if ( this.emissiveMap && this.emissiveMap.isTexture ) data.emissiveMap = this.emissiveMap.toJSON( meta ).uuid; if ( this.specularMap && this.specularMap.isTexture ) data.specularMap = this.specularMap.toJSON( meta ).uuid; if ( this.envMap && this.envMap.isTexture ) { data.envMap = this.envMap.toJSON( meta ).uuid; data.reflectivity = this.reflectivity; // Scale behind envMap data.refractionRatio = this.refractionRatio; if ( this.combine !== undefined ) data.combine = this.combine; if ( this.envMapIntensity !== undefined ) data.envMapIntensity = this.envMapIntensity; } if ( this.gradientMap && this.gradientMap.isTexture ) { data.gradientMap = this.gradientMap.toJSON( meta ).uuid; } if ( this.size !== undefined ) data.size = this.size; if ( this.sizeAttenuation !== undefined ) data.sizeAttenuation = this.sizeAttenuation; if ( this.blending !== NormalBlending ) data.blending = this.blending; if ( this.flatShading === true ) data.flatShading = this.flatShading; if ( this.side !== FrontSide ) data.side = this.side; if ( this.vertexColors ) data.vertexColors = true; if ( this.opacity < 1 ) data.opacity = this.opacity; if ( this.transparent === true ) data.transparent = this.transparent; data.depthFunc = this.depthFunc; data.depthTest = this.depthTest; data.depthWrite = this.depthWrite; data.stencilWrite = this.stencilWrite; data.stencilWriteMask = this.stencilWriteMask; data.stencilFunc = this.stencilFunc; data.stencilRef = this.stencilRef; data.stencilFuncMask = this.stencilFuncMask; data.stencilFail = this.stencilFail; data.stencilZFail = this.stencilZFail; data.stencilZPass = this.stencilZPass; // rotation (SpriteMaterial) if ( this.rotation && this.rotation !== 0 ) data.rotation = this.rotation; if ( this.polygonOffset === true ) data.polygonOffset = true; if ( this.polygonOffsetFactor !== 0 ) data.polygonOffsetFactor = this.polygonOffsetFactor; if ( this.polygonOffsetUnits !== 0 ) data.polygonOffsetUnits = this.polygonOffsetUnits; if ( this.linewidth && this.linewidth !== 1 ) data.linewidth = this.linewidth; if ( this.dashSize !== undefined ) data.dashSize = this.dashSize; if ( this.gapSize !== undefined ) data.gapSize = this.gapSize; if ( this.scale !== undefined ) data.scale = this.scale; if ( this.dithering === true ) data.dithering = true; if ( this.alphaTest > 0 ) data.alphaTest = this.alphaTest; if ( this.premultipliedAlpha === true ) data.premultipliedAlpha = this.premultipliedAlpha; if ( this.wireframe === true ) data.wireframe = this.wireframe; if ( this.wireframeLinewidth > 1 ) data.wireframeLinewidth = this.wireframeLinewidth; if ( this.wireframeLinecap !== 'round' ) data.wireframeLinecap = this.wireframeLinecap; if ( this.wireframeLinejoin !== 'round' ) data.wireframeLinejoin = this.wireframeLinejoin; if ( this.morphTargets === true ) data.morphTargets = true; if ( this.morphNormals === true ) data.morphNormals = true; if ( this.skinning === true ) data.skinning = true; if ( this.visible === false ) data.visible = false; if ( this.toneMapped === false ) data.toneMapped = false; if ( JSON.stringify( this.userData ) !== '{}' ) data.userData = this.userData; // TODO: Copied from Object3D.toJSON function extractFromCache( cache ) { const values = []; for ( const key in cache ) { const data = cache[ key ]; delete data.metadata; values.push( data ); } return values; } if ( isRoot ) { const textures = extractFromCache( meta.textures ); const images = extractFromCache( meta.images ); if ( textures.length > 0 ) data.textures = textures; if ( images.length > 0 ) data.images = images; } return data; }, clone: function () { return new this.constructor().copy( this ); }, copy: function ( source ) { this.name = source.name; this.fog = source.fog; this.blending = source.blending; this.side = source.side; this.flatShading = source.flatShading; this.vertexColors = source.vertexColors; this.opacity = source.opacity; this.transparent = source.transparent; this.blendSrc = source.blendSrc; this.blendDst = source.blendDst; this.blendEquation = source.blendEquation; this.blendSrcAlpha = source.blendSrcAlpha; this.blendDstAlpha = source.blendDstAlpha; this.blendEquationAlpha = source.blendEquationAlpha; this.depthFunc = source.depthFunc; this.depthTest = source.depthTest; this.depthWrite = source.depthWrite; this.stencilWriteMask = source.stencilWriteMask; this.stencilFunc = source.stencilFunc; this.stencilRef = source.stencilRef; this.stencilFuncMask = source.stencilFuncMask; this.stencilFail = source.stencilFail; this.stencilZFail = source.stencilZFail; this.stencilZPass = source.stencilZPass; this.stencilWrite = source.stencilWrite; const srcPlanes = source.clippingPlanes; let dstPlanes = null; if ( srcPlanes !== null ) { const n = srcPlanes.length; dstPlanes = new Array( n ); for ( let i = 0; i !== n; ++ i ) { dstPlanes[ i ] = srcPlanes[ i ].clone(); } } this.clippingPlanes = dstPlanes; this.clipIntersection = source.clipIntersection; this.clipShadows = source.clipShadows; this.shadowSide = source.shadowSide; this.colorWrite = source.colorWrite; this.precision = source.precision; this.polygonOffset = source.polygonOffset; this.polygonOffsetFactor = source.polygonOffsetFactor; this.polygonOffsetUnits = source.polygonOffsetUnits; this.dithering = source.dithering; this.alphaTest = source.alphaTest; this.premultipliedAlpha = source.premultipliedAlpha; this.visible = source.visible; this.toneMapped = source.toneMapped; this.userData = JSON.parse( JSON.stringify( source.userData ) ); return this; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } } ); Object.defineProperty( Material.prototype, 'needsUpdate', { set: function ( value ) { if ( value === true ) this.version ++; } } ); /** * parameters = { * color: , * opacity: , * map: new THREE.Texture( ), * * lightMap: new THREE.Texture( ), * lightMapIntensity: * * aoMap: new THREE.Texture( ), * aoMapIntensity: * * specularMap: new THREE.Texture( ), * * alphaMap: new THREE.Texture( ), * * envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ), * combine: THREE.Multiply, * reflectivity: , * refractionRatio: , * * depthTest: , * depthWrite: , * * wireframe: , * wireframeLinewidth: , * * skinning: , * morphTargets: * } */ function MeshBasicMaterial( parameters ) { Material.call( this ); this.type = 'MeshBasicMaterial'; this.color = new Color( 0xffffff ); // emissive this.map = null; this.lightMap = null; this.lightMapIntensity = 1.0; this.aoMap = null; this.aoMapIntensity = 1.0; this.specularMap = null; this.alphaMap = null; this.envMap = null; this.combine = MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.skinning = false; this.morphTargets = false; this.setValues( parameters ); } MeshBasicMaterial.prototype = Object.create( Material.prototype ); MeshBasicMaterial.prototype.constructor = MeshBasicMaterial; MeshBasicMaterial.prototype.isMeshBasicMaterial = true; MeshBasicMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); this.map = source.map; this.lightMap = source.lightMap; this.lightMapIntensity = source.lightMapIntensity; this.aoMap = source.aoMap; this.aoMapIntensity = source.aoMapIntensity; this.specularMap = source.specularMap; this.alphaMap = source.alphaMap; this.envMap = source.envMap; this.combine = source.combine; this.reflectivity = source.reflectivity; this.refractionRatio = source.refractionRatio; this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; this.wireframeLinecap = source.wireframeLinecap; this.wireframeLinejoin = source.wireframeLinejoin; this.skinning = source.skinning; this.morphTargets = source.morphTargets; return this; }; const _vector$3 = new Vector3(); const _vector2$1 = new Vector2(); function BufferAttribute( array, itemSize, normalized ) { if ( Array.isArray( array ) ) { throw new TypeError( 'THREE.BufferAttribute: array should be a Typed Array.' ); } this.name = ''; this.array = array; this.itemSize = itemSize; this.count = array !== undefined ? array.length / itemSize : 0; this.normalized = normalized === true; this.usage = StaticDrawUsage; this.updateRange = { offset: 0, count: - 1 }; this.version = 0; } Object.defineProperty( BufferAttribute.prototype, 'needsUpdate', { set: function ( value ) { if ( value === true ) this.version ++; } } ); Object.assign( BufferAttribute.prototype, { isBufferAttribute: true, onUploadCallback: function () {}, setUsage: function ( value ) { this.usage = value; return this; }, copy: function ( source ) { this.name = source.name; this.array = new source.array.constructor( source.array ); this.itemSize = source.itemSize; this.count = source.count; this.normalized = source.normalized; this.usage = source.usage; return this; }, copyAt: function ( index1, attribute, index2 ) { index1 *= this.itemSize; index2 *= attribute.itemSize; for ( let i = 0, l = this.itemSize; i < l; i ++ ) { this.array[ index1 + i ] = attribute.array[ index2 + i ]; } return this; }, copyArray: function ( array ) { this.array.set( array ); return this; }, copyColorsArray: function ( colors ) { const array = this.array; let offset = 0; for ( let i = 0, l = colors.length; i < l; i ++ ) { let color = colors[ i ]; if ( color === undefined ) { console.warn( 'THREE.BufferAttribute.copyColorsArray(): color is undefined', i ); color = new Color(); } array[ offset ++ ] = color.r; array[ offset ++ ] = color.g; array[ offset ++ ] = color.b; } return this; }, copyVector2sArray: function ( vectors ) { const array = this.array; let offset = 0; for ( let i = 0, l = vectors.length; i < l; i ++ ) { let vector = vectors[ i ]; if ( vector === undefined ) { console.warn( 'THREE.BufferAttribute.copyVector2sArray(): vector is undefined', i ); vector = new Vector2(); } array[ offset ++ ] = vector.x; array[ offset ++ ] = vector.y; } return this; }, copyVector3sArray: function ( vectors ) { const array = this.array; let offset = 0; for ( let i = 0, l = vectors.length; i < l; i ++ ) { let vector = vectors[ i ]; if ( vector === undefined ) { console.warn( 'THREE.BufferAttribute.copyVector3sArray(): vector is undefined', i ); vector = new Vector3(); } array[ offset ++ ] = vector.x; array[ offset ++ ] = vector.y; array[ offset ++ ] = vector.z; } return this; }, copyVector4sArray: function ( vectors ) { const array = this.array; let offset = 0; for ( let i = 0, l = vectors.length; i < l; i ++ ) { let vector = vectors[ i ]; if ( vector === undefined ) { console.warn( 'THREE.BufferAttribute.copyVector4sArray(): vector is undefined', i ); vector = new Vector4(); } array[ offset ++ ] = vector.x; array[ offset ++ ] = vector.y; array[ offset ++ ] = vector.z; array[ offset ++ ] = vector.w; } return this; }, applyMatrix3: function ( m ) { if ( this.itemSize === 2 ) { for ( let i = 0, l = this.count; i < l; i ++ ) { _vector2$1.fromBufferAttribute( this, i ); _vector2$1.applyMatrix3( m ); this.setXY( i, _vector2$1.x, _vector2$1.y ); } } else if ( this.itemSize === 3 ) { for ( let i = 0, l = this.count; i < l; i ++ ) { _vector$3.fromBufferAttribute( this, i ); _vector$3.applyMatrix3( m ); this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z ); } } return this; }, applyMatrix4: function ( m ) { for ( let i = 0, l = this.count; i < l; i ++ ) { _vector$3.x = this.getX( i ); _vector$3.y = this.getY( i ); _vector$3.z = this.getZ( i ); _vector$3.applyMatrix4( m ); this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z ); } return this; }, applyNormalMatrix: function ( m ) { for ( let i = 0, l = this.count; i < l; i ++ ) { _vector$3.x = this.getX( i ); _vector$3.y = this.getY( i ); _vector$3.z = this.getZ( i ); _vector$3.applyNormalMatrix( m ); this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z ); } return this; }, transformDirection: function ( m ) { for ( let i = 0, l = this.count; i < l; i ++ ) { _vector$3.x = this.getX( i ); _vector$3.y = this.getY( i ); _vector$3.z = this.getZ( i ); _vector$3.transformDirection( m ); this.setXYZ( i, _vector$3.x, _vector$3.y, _vector$3.z ); } return this; }, set: function ( value, offset = 0 ) { this.array.set( value, offset ); return this; }, getX: function ( index ) { return this.array[ index * this.itemSize ]; }, setX: function ( index, x ) { this.array[ index * this.itemSize ] = x; return this; }, getY: function ( index ) { return this.array[ index * this.itemSize + 1 ]; }, setY: function ( index, y ) { this.array[ index * this.itemSize + 1 ] = y; return this; }, getZ: function ( index ) { return this.array[ index * this.itemSize + 2 ]; }, setZ: function ( index, z ) { this.array[ index * this.itemSize + 2 ] = z; return this; }, getW: function ( index ) { return this.array[ index * this.itemSize + 3 ]; }, setW: function ( index, w ) { this.array[ index * this.itemSize + 3 ] = w; return this; }, setXY: function ( index, x, y ) { index *= this.itemSize; this.array[ index + 0 ] = x; this.array[ index + 1 ] = y; return this; }, setXYZ: function ( index, x, y, z ) { index *= this.itemSize; this.array[ index + 0 ] = x; this.array[ index + 1 ] = y; this.array[ index + 2 ] = z; return this; }, setXYZW: function ( index, x, y, z, w ) { index *= this.itemSize; this.array[ index + 0 ] = x; this.array[ index + 1 ] = y; this.array[ index + 2 ] = z; this.array[ index + 3 ] = w; return this; }, onUpload: function ( callback ) { this.onUploadCallback = callback; return this; }, clone: function () { return new this.constructor( this.array, this.itemSize ).copy( this ); }, toJSON: function () { return { itemSize: this.itemSize, type: this.array.constructor.name, array: Array.prototype.slice.call( this.array ), normalized: this.normalized }; } } ); // function Int8BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Int8Array( array ), itemSize, normalized ); } Int8BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Int8BufferAttribute.prototype.constructor = Int8BufferAttribute; function Uint8BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Uint8Array( array ), itemSize, normalized ); } Uint8BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Uint8BufferAttribute.prototype.constructor = Uint8BufferAttribute; function Uint8ClampedBufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Uint8ClampedArray( array ), itemSize, normalized ); } Uint8ClampedBufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Uint8ClampedBufferAttribute.prototype.constructor = Uint8ClampedBufferAttribute; function Int16BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Int16Array( array ), itemSize, normalized ); } Int16BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Int16BufferAttribute.prototype.constructor = Int16BufferAttribute; function Uint16BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized ); } Uint16BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Uint16BufferAttribute.prototype.constructor = Uint16BufferAttribute; function Int32BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Int32Array( array ), itemSize, normalized ); } Int32BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Int32BufferAttribute.prototype.constructor = Int32BufferAttribute; function Uint32BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Uint32Array( array ), itemSize, normalized ); } Uint32BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Uint32BufferAttribute.prototype.constructor = Uint32BufferAttribute; function Float16BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Uint16Array( array ), itemSize, normalized ); } Float16BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Float16BufferAttribute.prototype.constructor = Float16BufferAttribute; Float16BufferAttribute.prototype.isFloat16BufferAttribute = true; function Float32BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Float32Array( array ), itemSize, normalized ); } Float32BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Float32BufferAttribute.prototype.constructor = Float32BufferAttribute; function Float64BufferAttribute( array, itemSize, normalized ) { BufferAttribute.call( this, new Float64Array( array ), itemSize, normalized ); } Float64BufferAttribute.prototype = Object.create( BufferAttribute.prototype ); Float64BufferAttribute.prototype.constructor = Float64BufferAttribute; class DirectGeometry { constructor() { this.vertices = []; this.normals = []; this.colors = []; this.uvs = []; this.uvs2 = []; this.groups = []; this.morphTargets = {}; this.skinWeights = []; this.skinIndices = []; // this.lineDistances = []; this.boundingBox = null; this.boundingSphere = null; // update flags this.verticesNeedUpdate = false; this.normalsNeedUpdate = false; this.colorsNeedUpdate = false; this.uvsNeedUpdate = false; this.groupsNeedUpdate = false; } computeGroups( geometry ) { const groups = []; let group, i; let materialIndex = undefined; const faces = geometry.faces; for ( i = 0; i < faces.length; i ++ ) { const face = faces[ i ]; // materials if ( face.materialIndex !== materialIndex ) { materialIndex = face.materialIndex; if ( group !== undefined ) { group.count = ( i * 3 ) - group.start; groups.push( group ); } group = { start: i * 3, materialIndex: materialIndex }; } } if ( group !== undefined ) { group.count = ( i * 3 ) - group.start; groups.push( group ); } this.groups = groups; } fromGeometry( geometry ) { const faces = geometry.faces; const vertices = geometry.vertices; const faceVertexUvs = geometry.faceVertexUvs; const hasFaceVertexUv = faceVertexUvs[ 0 ] && faceVertexUvs[ 0 ].length > 0; const hasFaceVertexUv2 = faceVertexUvs[ 1 ] && faceVertexUvs[ 1 ].length > 0; // morphs const morphTargets = geometry.morphTargets; const morphTargetsLength = morphTargets.length; let morphTargetsPosition; if ( morphTargetsLength > 0 ) { morphTargetsPosition = []; for ( let i = 0; i < morphTargetsLength; i ++ ) { morphTargetsPosition[ i ] = { name: morphTargets[ i ].name, data: [] }; } this.morphTargets.position = morphTargetsPosition; } const morphNormals = geometry.morphNormals; const morphNormalsLength = morphNormals.length; let morphTargetsNormal; if ( morphNormalsLength > 0 ) { morphTargetsNormal = []; for ( let i = 0; i < morphNormalsLength; i ++ ) { morphTargetsNormal[ i ] = { name: morphNormals[ i ].name, data: [] }; } this.morphTargets.normal = morphTargetsNormal; } // skins const skinIndices = geometry.skinIndices; const skinWeights = geometry.skinWeights; const hasSkinIndices = skinIndices.length === vertices.length; const hasSkinWeights = skinWeights.length === vertices.length; // if ( vertices.length > 0 && faces.length === 0 ) { console.error( 'THREE.DirectGeometry: Faceless geometries are not supported.' ); } for ( let i = 0; i < faces.length; i ++ ) { const face = faces[ i ]; this.vertices.push( vertices[ face.a ], vertices[ face.b ], vertices[ face.c ] ); const vertexNormals = face.vertexNormals; if ( vertexNormals.length === 3 ) { this.normals.push( vertexNormals[ 0 ], vertexNormals[ 1 ], vertexNormals[ 2 ] ); } else { const normal = face.normal; this.normals.push( normal, normal, normal ); } const vertexColors = face.vertexColors; if ( vertexColors.length === 3 ) { this.colors.push( vertexColors[ 0 ], vertexColors[ 1 ], vertexColors[ 2 ] ); } else { const color = face.color; this.colors.push( color, color, color ); } if ( hasFaceVertexUv === true ) { const vertexUvs = faceVertexUvs[ 0 ][ i ]; if ( vertexUvs !== undefined ) { this.uvs.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] ); } else { console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv ', i ); this.uvs.push( new Vector2(), new Vector2(), new Vector2() ); } } if ( hasFaceVertexUv2 === true ) { const vertexUvs = faceVertexUvs[ 1 ][ i ]; if ( vertexUvs !== undefined ) { this.uvs2.push( vertexUvs[ 0 ], vertexUvs[ 1 ], vertexUvs[ 2 ] ); } else { console.warn( 'THREE.DirectGeometry.fromGeometry(): Undefined vertexUv2 ', i ); this.uvs2.push( new Vector2(), new Vector2(), new Vector2() ); } } // morphs for ( let j = 0; j < morphTargetsLength; j ++ ) { const morphTarget = morphTargets[ j ].vertices; morphTargetsPosition[ j ].data.push( morphTarget[ face.a ], morphTarget[ face.b ], morphTarget[ face.c ] ); } for ( let j = 0; j < morphNormalsLength; j ++ ) { const morphNormal = morphNormals[ j ].vertexNormals[ i ]; morphTargetsNormal[ j ].data.push( morphNormal.a, morphNormal.b, morphNormal.c ); } // skins if ( hasSkinIndices ) { this.skinIndices.push( skinIndices[ face.a ], skinIndices[ face.b ], skinIndices[ face.c ] ); } if ( hasSkinWeights ) { this.skinWeights.push( skinWeights[ face.a ], skinWeights[ face.b ], skinWeights[ face.c ] ); } } this.computeGroups( geometry ); this.verticesNeedUpdate = geometry.verticesNeedUpdate; this.normalsNeedUpdate = geometry.normalsNeedUpdate; this.colorsNeedUpdate = geometry.colorsNeedUpdate; this.uvsNeedUpdate = geometry.uvsNeedUpdate; this.groupsNeedUpdate = geometry.groupsNeedUpdate; if ( geometry.boundingSphere !== null ) { this.boundingSphere = geometry.boundingSphere.clone(); } if ( geometry.boundingBox !== null ) { this.boundingBox = geometry.boundingBox.clone(); } return this; } } function arrayMax( array ) { if ( array.length === 0 ) return - Infinity; let max = array[ 0 ]; for ( let i = 1, l = array.length; i < l; ++ i ) { if ( array[ i ] > max ) max = array[ i ]; } return max; } const TYPED_ARRAYS = { Int8Array: Int8Array, Uint8Array: Uint8Array, // Workaround for IE11 pre KB2929437. See #11440 Uint8ClampedArray: typeof Uint8ClampedArray !== 'undefined' ? Uint8ClampedArray : Uint8Array, Int16Array: Int16Array, Uint16Array: Uint16Array, Int32Array: Int32Array, Uint32Array: Uint32Array, Float32Array: Float32Array, Float64Array: Float64Array }; function getTypedArray( type, buffer ) { return new TYPED_ARRAYS[ type ]( buffer ); } let _bufferGeometryId = 1; // BufferGeometry uses odd numbers as Id const _m1$2 = new Matrix4(); const _obj = new Object3D(); const _offset = new Vector3(); const _box$2 = new Box3(); const _boxMorphTargets = new Box3(); const _vector$4 = new Vector3(); function BufferGeometry() { Object.defineProperty( this, 'id', { value: _bufferGeometryId += 2 } ); this.uuid = MathUtils.generateUUID(); this.name = ''; this.type = 'BufferGeometry'; this.index = null; this.attributes = {}; this.morphAttributes = {}; this.morphTargetsRelative = false; this.groups = []; this.boundingBox = null; this.boundingSphere = null; this.drawRange = { start: 0, count: Infinity }; this.userData = {}; } BufferGeometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), { constructor: BufferGeometry, isBufferGeometry: true, getIndex: function () { return this.index; }, setIndex: function ( index ) { if ( Array.isArray( index ) ) { this.index = new ( arrayMax( index ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( index, 1 ); } else { this.index = index; } return this; }, getAttribute: function ( name ) { return this.attributes[ name ]; }, setAttribute: function ( name, attribute ) { this.attributes[ name ] = attribute; return this; }, deleteAttribute: function ( name ) { delete this.attributes[ name ]; return this; }, hasAttribute: function ( name ) { return this.attributes[ name ] !== undefined; }, addGroup: function ( start, count, materialIndex = 0 ) { this.groups.push( { start: start, count: count, materialIndex: materialIndex } ); }, clearGroups: function () { this.groups = []; }, setDrawRange: function ( start, count ) { this.drawRange.start = start; this.drawRange.count = count; }, applyMatrix4: function ( matrix ) { const position = this.attributes.position; if ( position !== undefined ) { position.applyMatrix4( matrix ); position.needsUpdate = true; } const normal = this.attributes.normal; if ( normal !== undefined ) { const normalMatrix = new Matrix3().getNormalMatrix( matrix ); normal.applyNormalMatrix( normalMatrix ); normal.needsUpdate = true; } const tangent = this.attributes.tangent; if ( tangent !== undefined ) { tangent.transformDirection( matrix ); tangent.needsUpdate = true; } if ( this.boundingBox !== null ) { this.computeBoundingBox(); } if ( this.boundingSphere !== null ) { this.computeBoundingSphere(); } return this; }, rotateX: function ( angle ) { // rotate geometry around world x-axis _m1$2.makeRotationX( angle ); this.applyMatrix4( _m1$2 ); return this; }, rotateY: function ( angle ) { // rotate geometry around world y-axis _m1$2.makeRotationY( angle ); this.applyMatrix4( _m1$2 ); return this; }, rotateZ: function ( angle ) { // rotate geometry around world z-axis _m1$2.makeRotationZ( angle ); this.applyMatrix4( _m1$2 ); return this; }, translate: function ( x, y, z ) { // translate geometry _m1$2.makeTranslation( x, y, z ); this.applyMatrix4( _m1$2 ); return this; }, scale: function ( x, y, z ) { // scale geometry _m1$2.makeScale( x, y, z ); this.applyMatrix4( _m1$2 ); return this; }, lookAt: function ( vector ) { _obj.lookAt( vector ); _obj.updateMatrix(); this.applyMatrix4( _obj.matrix ); return this; }, center: function () { this.computeBoundingBox(); this.boundingBox.getCenter( _offset ).negate(); this.translate( _offset.x, _offset.y, _offset.z ); return this; }, setFromObject: function ( object ) { // console.log( 'THREE.BufferGeometry.setFromObject(). Converting', object, this ); const geometry = object.geometry; if ( object.isPoints || object.isLine ) { const positions = new Float32BufferAttribute( geometry.vertices.length * 3, 3 ); const colors = new Float32BufferAttribute( geometry.colors.length * 3, 3 ); this.setAttribute( 'position', positions.copyVector3sArray( geometry.vertices ) ); this.setAttribute( 'color', colors.copyColorsArray( geometry.colors ) ); if ( geometry.lineDistances && geometry.lineDistances.length === geometry.vertices.length ) { const lineDistances = new Float32BufferAttribute( geometry.lineDistances.length, 1 ); this.setAttribute( 'lineDistance', lineDistances.copyArray( geometry.lineDistances ) ); } if ( geometry.boundingSphere !== null ) { this.boundingSphere = geometry.boundingSphere.clone(); } if ( geometry.boundingBox !== null ) { this.boundingBox = geometry.boundingBox.clone(); } } else if ( object.isMesh ) { if ( geometry && geometry.isGeometry ) { this.fromGeometry( geometry ); } } return this; }, setFromPoints: function ( points ) { const position = []; for ( let i = 0, l = points.length; i < l; i ++ ) { const point = points[ i ]; position.push( point.x, point.y, point.z || 0 ); } this.setAttribute( 'position', new Float32BufferAttribute( position, 3 ) ); return this; }, updateFromObject: function ( object ) { let geometry = object.geometry; if ( object.isMesh ) { let direct = geometry.__directGeometry; if ( geometry.elementsNeedUpdate === true ) { direct = undefined; geometry.elementsNeedUpdate = false; } if ( direct === undefined ) { return this.fromGeometry( geometry ); } direct.verticesNeedUpdate = geometry.verticesNeedUpdate; direct.normalsNeedUpdate = geometry.normalsNeedUpdate; direct.colorsNeedUpdate = geometry.colorsNeedUpdate; direct.uvsNeedUpdate = geometry.uvsNeedUpdate; direct.groupsNeedUpdate = geometry.groupsNeedUpdate; geometry.verticesNeedUpdate = false; geometry.normalsNeedUpdate = false; geometry.colorsNeedUpdate = false; geometry.uvsNeedUpdate = false; geometry.groupsNeedUpdate = false; geometry = direct; } if ( geometry.verticesNeedUpdate === true ) { const attribute = this.attributes.position; if ( attribute !== undefined ) { attribute.copyVector3sArray( geometry.vertices ); attribute.needsUpdate = true; } geometry.verticesNeedUpdate = false; } if ( geometry.normalsNeedUpdate === true ) { const attribute = this.attributes.normal; if ( attribute !== undefined ) { attribute.copyVector3sArray( geometry.normals ); attribute.needsUpdate = true; } geometry.normalsNeedUpdate = false; } if ( geometry.colorsNeedUpdate === true ) { const attribute = this.attributes.color; if ( attribute !== undefined ) { attribute.copyColorsArray( geometry.colors ); attribute.needsUpdate = true; } geometry.colorsNeedUpdate = false; } if ( geometry.uvsNeedUpdate ) { const attribute = this.attributes.uv; if ( attribute !== undefined ) { attribute.copyVector2sArray( geometry.uvs ); attribute.needsUpdate = true; } geometry.uvsNeedUpdate = false; } if ( geometry.lineDistancesNeedUpdate ) { const attribute = this.attributes.lineDistance; if ( attribute !== undefined ) { attribute.copyArray( geometry.lineDistances ); attribute.needsUpdate = true; } geometry.lineDistancesNeedUpdate = false; } if ( geometry.groupsNeedUpdate ) { geometry.computeGroups( object.geometry ); this.groups = geometry.groups; geometry.groupsNeedUpdate = false; } return this; }, fromGeometry: function ( geometry ) { geometry.__directGeometry = new DirectGeometry().fromGeometry( geometry ); return this.fromDirectGeometry( geometry.__directGeometry ); }, fromDirectGeometry: function ( geometry ) { const positions = new Float32Array( geometry.vertices.length * 3 ); this.setAttribute( 'position', new BufferAttribute( positions, 3 ).copyVector3sArray( geometry.vertices ) ); if ( geometry.normals.length > 0 ) { const normals = new Float32Array( geometry.normals.length * 3 ); this.setAttribute( 'normal', new BufferAttribute( normals, 3 ).copyVector3sArray( geometry.normals ) ); } if ( geometry.colors.length > 0 ) { const colors = new Float32Array( geometry.colors.length * 3 ); this.setAttribute( 'color', new BufferAttribute( colors, 3 ).copyColorsArray( geometry.colors ) ); } if ( geometry.uvs.length > 0 ) { const uvs = new Float32Array( geometry.uvs.length * 2 ); this.setAttribute( 'uv', new BufferAttribute( uvs, 2 ).copyVector2sArray( geometry.uvs ) ); } if ( geometry.uvs2.length > 0 ) { const uvs2 = new Float32Array( geometry.uvs2.length * 2 ); this.setAttribute( 'uv2', new BufferAttribute( uvs2, 2 ).copyVector2sArray( geometry.uvs2 ) ); } // groups this.groups = geometry.groups; // morphs for ( const name in geometry.morphTargets ) { const array = []; const morphTargets = geometry.morphTargets[ name ]; for ( let i = 0, l = morphTargets.length; i < l; i ++ ) { const morphTarget = morphTargets[ i ]; const attribute = new Float32BufferAttribute( morphTarget.data.length * 3, 3 ); attribute.name = morphTarget.name; array.push( attribute.copyVector3sArray( morphTarget.data ) ); } this.morphAttributes[ name ] = array; } // skinning if ( geometry.skinIndices.length > 0 ) { const skinIndices = new Float32BufferAttribute( geometry.skinIndices.length * 4, 4 ); this.setAttribute( 'skinIndex', skinIndices.copyVector4sArray( geometry.skinIndices ) ); } if ( geometry.skinWeights.length > 0 ) { const skinWeights = new Float32BufferAttribute( geometry.skinWeights.length * 4, 4 ); this.setAttribute( 'skinWeight', skinWeights.copyVector4sArray( geometry.skinWeights ) ); } // if ( geometry.boundingSphere !== null ) { this.boundingSphere = geometry.boundingSphere.clone(); } if ( geometry.boundingBox !== null ) { this.boundingBox = geometry.boundingBox.clone(); } return this; }, computeBoundingBox: function () { if ( this.boundingBox === null ) { this.boundingBox = new Box3(); } const position = this.attributes.position; const morphAttributesPosition = this.morphAttributes.position; if ( position && position.isGLBufferAttribute ) { console.error( 'THREE.BufferGeometry.computeBoundingBox(): GLBufferAttribute requires a manual bounding box. Alternatively set "mesh.frustumCulled" to "false".', this ); this.boundingBox.set( new Vector3( - Infinity, - Infinity, - Infinity ), new Vector3( + Infinity, + Infinity, + Infinity ) ); return; } if ( position !== undefined ) { this.boundingBox.setFromBufferAttribute( position ); // process morph attributes if present if ( morphAttributesPosition ) { for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) { const morphAttribute = morphAttributesPosition[ i ]; _box$2.setFromBufferAttribute( morphAttribute ); if ( this.morphTargetsRelative ) { _vector$4.addVectors( this.boundingBox.min, _box$2.min ); this.boundingBox.expandByPoint( _vector$4 ); _vector$4.addVectors( this.boundingBox.max, _box$2.max ); this.boundingBox.expandByPoint( _vector$4 ); } else { this.boundingBox.expandByPoint( _box$2.min ); this.boundingBox.expandByPoint( _box$2.max ); } } } } else { this.boundingBox.makeEmpty(); } if ( isNaN( this.boundingBox.min.x ) || isNaN( this.boundingBox.min.y ) || isNaN( this.boundingBox.min.z ) ) { console.error( 'THREE.BufferGeometry.computeBoundingBox(): Computed min/max have NaN values. The "position" attribute is likely to have NaN values.', this ); } }, computeBoundingSphere: function () { if ( this.boundingSphere === null ) { this.boundingSphere = new Sphere(); } const position = this.attributes.position; const morphAttributesPosition = this.morphAttributes.position; if ( position && position.isGLBufferAttribute ) { console.error( 'THREE.BufferGeometry.computeBoundingSphere(): GLBufferAttribute requires a manual bounding sphere. Alternatively set "mesh.frustumCulled" to "false".', this ); this.boundingSphere.set( new Vector3(), Infinity ); return; } if ( position ) { // first, find the center of the bounding sphere const center = this.boundingSphere.center; _box$2.setFromBufferAttribute( position ); // process morph attributes if present if ( morphAttributesPosition ) { for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) { const morphAttribute = morphAttributesPosition[ i ]; _boxMorphTargets.setFromBufferAttribute( morphAttribute ); if ( this.morphTargetsRelative ) { _vector$4.addVectors( _box$2.min, _boxMorphTargets.min ); _box$2.expandByPoint( _vector$4 ); _vector$4.addVectors( _box$2.max, _boxMorphTargets.max ); _box$2.expandByPoint( _vector$4 ); } else { _box$2.expandByPoint( _boxMorphTargets.min ); _box$2.expandByPoint( _boxMorphTargets.max ); } } } _box$2.getCenter( center ); // second, try to find a boundingSphere with a radius smaller than the // boundingSphere of the boundingBox: sqrt(3) smaller in the best case let maxRadiusSq = 0; for ( let i = 0, il = position.count; i < il; i ++ ) { _vector$4.fromBufferAttribute( position, i ); maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$4 ) ); } // process morph attributes if present if ( morphAttributesPosition ) { for ( let i = 0, il = morphAttributesPosition.length; i < il; i ++ ) { const morphAttribute = morphAttributesPosition[ i ]; const morphTargetsRelative = this.morphTargetsRelative; for ( let j = 0, jl = morphAttribute.count; j < jl; j ++ ) { _vector$4.fromBufferAttribute( morphAttribute, j ); if ( morphTargetsRelative ) { _offset.fromBufferAttribute( position, j ); _vector$4.add( _offset ); } maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$4 ) ); } } } this.boundingSphere.radius = Math.sqrt( maxRadiusSq ); if ( isNaN( this.boundingSphere.radius ) ) { console.error( 'THREE.BufferGeometry.computeBoundingSphere(): Computed radius is NaN. The "position" attribute is likely to have NaN values.', this ); } } }, computeFaceNormals: function () { // backwards compatibility }, computeVertexNormals: function () { const index = this.index; const positionAttribute = this.getAttribute( 'position' ); if ( positionAttribute !== undefined ) { let normalAttribute = this.getAttribute( 'normal' ); if ( normalAttribute === undefined ) { normalAttribute = new BufferAttribute( new Float32Array( positionAttribute.count * 3 ), 3 ); this.setAttribute( 'normal', normalAttribute ); } else { // reset existing normals to zero for ( let i = 0, il = normalAttribute.count; i < il; i ++ ) { normalAttribute.setXYZ( i, 0, 0, 0 ); } } const pA = new Vector3(), pB = new Vector3(), pC = new Vector3(); const nA = new Vector3(), nB = new Vector3(), nC = new Vector3(); const cb = new Vector3(), ab = new Vector3(); // indexed elements if ( index ) { for ( let i = 0, il = index.count; i < il; i += 3 ) { const vA = index.getX( i + 0 ); const vB = index.getX( i + 1 ); const vC = index.getX( i + 2 ); pA.fromBufferAttribute( positionAttribute, vA ); pB.fromBufferAttribute( positionAttribute, vB ); pC.fromBufferAttribute( positionAttribute, vC ); cb.subVectors( pC, pB ); ab.subVectors( pA, pB ); cb.cross( ab ); nA.fromBufferAttribute( normalAttribute, vA ); nB.fromBufferAttribute( normalAttribute, vB ); nC.fromBufferAttribute( normalAttribute, vC ); nA.add( cb ); nB.add( cb ); nC.add( cb ); normalAttribute.setXYZ( vA, nA.x, nA.y, nA.z ); normalAttribute.setXYZ( vB, nB.x, nB.y, nB.z ); normalAttribute.setXYZ( vC, nC.x, nC.y, nC.z ); } } else { // non-indexed elements (unconnected triangle soup) for ( let i = 0, il = positionAttribute.count; i < il; i += 3 ) { pA.fromBufferAttribute( positionAttribute, i + 0 ); pB.fromBufferAttribute( positionAttribute, i + 1 ); pC.fromBufferAttribute( positionAttribute, i + 2 ); cb.subVectors( pC, pB ); ab.subVectors( pA, pB ); cb.cross( ab ); normalAttribute.setXYZ( i + 0, cb.x, cb.y, cb.z ); normalAttribute.setXYZ( i + 1, cb.x, cb.y, cb.z ); normalAttribute.setXYZ( i + 2, cb.x, cb.y, cb.z ); } } this.normalizeNormals(); normalAttribute.needsUpdate = true; } }, merge: function ( geometry, offset ) { if ( ! ( geometry && geometry.isBufferGeometry ) ) { console.error( 'THREE.BufferGeometry.merge(): geometry not an instance of THREE.BufferGeometry.', geometry ); return; } if ( offset === undefined ) { offset = 0; console.warn( 'THREE.BufferGeometry.merge(): Overwriting original geometry, starting at offset=0. ' + 'Use BufferGeometryUtils.mergeBufferGeometries() for lossless merge.' ); } const attributes = this.attributes; for ( const key in attributes ) { if ( geometry.attributes[ key ] === undefined ) continue; const attribute1 = attributes[ key ]; const attributeArray1 = attribute1.array; const attribute2 = geometry.attributes[ key ]; const attributeArray2 = attribute2.array; const attributeOffset = attribute2.itemSize * offset; const length = Math.min( attributeArray2.length, attributeArray1.length - attributeOffset ); for ( let i = 0, j = attributeOffset; i < length; i ++, j ++ ) { attributeArray1[ j ] = attributeArray2[ i ]; } } return this; }, normalizeNormals: function () { const normals = this.attributes.normal; for ( let i = 0, il = normals.count; i < il; i ++ ) { _vector$4.fromBufferAttribute( normals, i ); _vector$4.normalize(); normals.setXYZ( i, _vector$4.x, _vector$4.y, _vector$4.z ); } }, toNonIndexed: function () { function convertBufferAttribute( attribute, indices ) { const array = attribute.array; const itemSize = attribute.itemSize; const normalized = attribute.normalized; const array2 = new array.constructor( indices.length * itemSize ); let index = 0, index2 = 0; for ( let i = 0, l = indices.length; i < l; i ++ ) { index = indices[ i ] * itemSize; for ( let j = 0; j < itemSize; j ++ ) { array2[ index2 ++ ] = array[ index ++ ]; } } return new BufferAttribute( array2, itemSize, normalized ); } // if ( this.index === null ) { console.warn( 'THREE.BufferGeometry.toNonIndexed(): Geometry is already non-indexed.' ); return this; } const geometry2 = new BufferGeometry(); const indices = this.index.array; const attributes = this.attributes; // attributes for ( const name in attributes ) { const attribute = attributes[ name ]; const newAttribute = convertBufferAttribute( attribute, indices ); geometry2.setAttribute( name, newAttribute ); } // morph attributes const morphAttributes = this.morphAttributes; for ( const name in morphAttributes ) { const morphArray = []; const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes for ( let i = 0, il = morphAttribute.length; i < il; i ++ ) { const attribute = morphAttribute[ i ]; const newAttribute = convertBufferAttribute( attribute, indices ); morphArray.push( newAttribute ); } geometry2.morphAttributes[ name ] = morphArray; } geometry2.morphTargetsRelative = this.morphTargetsRelative; // groups const groups = this.groups; for ( let i = 0, l = groups.length; i < l; i ++ ) { const group = groups[ i ]; geometry2.addGroup( group.start, group.count, group.materialIndex ); } return geometry2; }, toJSON: function () { const data = { metadata: { version: 4.5, type: 'BufferGeometry', generator: 'BufferGeometry.toJSON' } }; // standard BufferGeometry serialization data.uuid = this.uuid; data.type = this.type; if ( this.name !== '' ) data.name = this.name; if ( Object.keys( this.userData ).length > 0 ) data.userData = this.userData; if ( this.parameters !== undefined ) { const parameters = this.parameters; for ( const key in parameters ) { if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ]; } return data; } data.data = { attributes: {} }; const index = this.index; if ( index !== null ) { data.data.index = { type: index.array.constructor.name, array: Array.prototype.slice.call( index.array ) }; } const attributes = this.attributes; for ( const key in attributes ) { const attribute = attributes[ key ]; const attributeData = attribute.toJSON( data.data ); if ( attribute.name !== '' ) attributeData.name = attribute.name; data.data.attributes[ key ] = attributeData; } const morphAttributes = {}; let hasMorphAttributes = false; for ( const key in this.morphAttributes ) { const attributeArray = this.morphAttributes[ key ]; const array = []; for ( let i = 0, il = attributeArray.length; i < il; i ++ ) { const attribute = attributeArray[ i ]; const attributeData = attribute.toJSON( data.data ); if ( attribute.name !== '' ) attributeData.name = attribute.name; array.push( attributeData ); } if ( array.length > 0 ) { morphAttributes[ key ] = array; hasMorphAttributes = true; } } if ( hasMorphAttributes ) { data.data.morphAttributes = morphAttributes; data.data.morphTargetsRelative = this.morphTargetsRelative; } const groups = this.groups; if ( groups.length > 0 ) { data.data.groups = JSON.parse( JSON.stringify( groups ) ); } const boundingSphere = this.boundingSphere; if ( boundingSphere !== null ) { data.data.boundingSphere = { center: boundingSphere.center.toArray(), radius: boundingSphere.radius }; } return data; }, clone: function () { /* // Handle primitives const parameters = this.parameters; if ( parameters !== undefined ) { const values = []; for ( const key in parameters ) { values.push( parameters[ key ] ); } const geometry = Object.create( this.constructor.prototype ); this.constructor.apply( geometry, values ); return geometry; } return new this.constructor().copy( this ); */ return new BufferGeometry().copy( this ); }, copy: function ( source ) { // reset this.index = null; this.attributes = {}; this.morphAttributes = {}; this.groups = []; this.boundingBox = null; this.boundingSphere = null; // used for storing cloned, shared data const data = {}; // name this.name = source.name; // index const index = source.index; if ( index !== null ) { this.setIndex( index.clone( data ) ); } // attributes const attributes = source.attributes; for ( const name in attributes ) { const attribute = attributes[ name ]; this.setAttribute( name, attribute.clone( data ) ); } // morph attributes const morphAttributes = source.morphAttributes; for ( const name in morphAttributes ) { const array = []; const morphAttribute = morphAttributes[ name ]; // morphAttribute: array of Float32BufferAttributes for ( let i = 0, l = morphAttribute.length; i < l; i ++ ) { array.push( morphAttribute[ i ].clone( data ) ); } this.morphAttributes[ name ] = array; } this.morphTargetsRelative = source.morphTargetsRelative; // groups const groups = source.groups; for ( let i = 0, l = groups.length; i < l; i ++ ) { const group = groups[ i ]; this.addGroup( group.start, group.count, group.materialIndex ); } // bounding box const boundingBox = source.boundingBox; if ( boundingBox !== null ) { this.boundingBox = boundingBox.clone(); } // bounding sphere const boundingSphere = source.boundingSphere; if ( boundingSphere !== null ) { this.boundingSphere = boundingSphere.clone(); } // draw range this.drawRange.start = source.drawRange.start; this.drawRange.count = source.drawRange.count; // user data this.userData = source.userData; return this; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } } ); const _inverseMatrix = new Matrix4(); const _ray = new Ray(); const _sphere = new Sphere(); const _vA = new Vector3(); const _vB = new Vector3(); const _vC = new Vector3(); const _tempA = new Vector3(); const _tempB = new Vector3(); const _tempC = new Vector3(); const _morphA = new Vector3(); const _morphB = new Vector3(); const _morphC = new Vector3(); const _uvA = new Vector2(); const _uvB = new Vector2(); const _uvC = new Vector2(); const _intersectionPoint = new Vector3(); const _intersectionPointWorld = new Vector3(); function Mesh( geometry = new BufferGeometry(), material = new MeshBasicMaterial() ) { Object3D.call( this ); this.type = 'Mesh'; this.geometry = geometry; this.material = material; this.updateMorphTargets(); } Mesh.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Mesh, isMesh: true, copy: function ( source ) { Object3D.prototype.copy.call( this, source ); if ( source.morphTargetInfluences !== undefined ) { this.morphTargetInfluences = source.morphTargetInfluences.slice(); } if ( source.morphTargetDictionary !== undefined ) { this.morphTargetDictionary = Object.assign( {}, source.morphTargetDictionary ); } this.material = source.material; this.geometry = source.geometry; return this; }, updateMorphTargets: function () { const geometry = this.geometry; if ( geometry.isBufferGeometry ) { const morphAttributes = geometry.morphAttributes; const keys = Object.keys( morphAttributes ); if ( keys.length > 0 ) { const morphAttribute = morphAttributes[ keys[ 0 ] ]; if ( morphAttribute !== undefined ) { this.morphTargetInfluences = []; this.morphTargetDictionary = {}; for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) { const name = morphAttribute[ m ].name || String( m ); this.morphTargetInfluences.push( 0 ); this.morphTargetDictionary[ name ] = m; } } } } else { const morphTargets = geometry.morphTargets; if ( morphTargets !== undefined && morphTargets.length > 0 ) { console.error( 'THREE.Mesh.updateMorphTargets() no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' ); } } }, raycast: function ( raycaster, intersects ) { const geometry = this.geometry; const material = this.material; const matrixWorld = this.matrixWorld; if ( material === undefined ) return; // Checking boundingSphere distance to ray if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); _sphere.copy( geometry.boundingSphere ); _sphere.applyMatrix4( matrixWorld ); if ( raycaster.ray.intersectsSphere( _sphere ) === false ) return; // _inverseMatrix.copy( matrixWorld ).invert(); _ray.copy( raycaster.ray ).applyMatrix4( _inverseMatrix ); // Check boundingBox before continuing if ( geometry.boundingBox !== null ) { if ( _ray.intersectsBox( geometry.boundingBox ) === false ) return; } let intersection; if ( geometry.isBufferGeometry ) { const index = geometry.index; const position = geometry.attributes.position; const morphPosition = geometry.morphAttributes.position; const morphTargetsRelative = geometry.morphTargetsRelative; const uv = geometry.attributes.uv; const uv2 = geometry.attributes.uv2; const groups = geometry.groups; const drawRange = geometry.drawRange; if ( index !== null ) { // indexed buffer geometry if ( Array.isArray( material ) ) { for ( let i = 0, il = groups.length; i < il; i ++ ) { const group = groups[ i ]; const groupMaterial = material[ group.materialIndex ]; const start = Math.max( group.start, drawRange.start ); const end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ); for ( let j = start, jl = end; j < jl; j += 3 ) { const a = index.getX( j ); const b = index.getX( j + 1 ); const c = index.getX( j + 2 ); intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ); if ( intersection ) { intersection.faceIndex = Math.floor( j / 3 ); // triangle number in indexed buffer semantics intersection.face.materialIndex = group.materialIndex; intersects.push( intersection ); } } } } else { const start = Math.max( 0, drawRange.start ); const end = Math.min( index.count, ( drawRange.start + drawRange.count ) ); for ( let i = start, il = end; i < il; i += 3 ) { const a = index.getX( i ); const b = index.getX( i + 1 ); const c = index.getX( i + 2 ); intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ); if ( intersection ) { intersection.faceIndex = Math.floor( i / 3 ); // triangle number in indexed buffer semantics intersects.push( intersection ); } } } } else if ( position !== undefined ) { // non-indexed buffer geometry if ( Array.isArray( material ) ) { for ( let i = 0, il = groups.length; i < il; i ++ ) { const group = groups[ i ]; const groupMaterial = material[ group.materialIndex ]; const start = Math.max( group.start, drawRange.start ); const end = Math.min( ( group.start + group.count ), ( drawRange.start + drawRange.count ) ); for ( let j = start, jl = end; j < jl; j += 3 ) { const a = j; const b = j + 1; const c = j + 2; intersection = checkBufferGeometryIntersection( this, groupMaterial, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ); if ( intersection ) { intersection.faceIndex = Math.floor( j / 3 ); // triangle number in non-indexed buffer semantics intersection.face.materialIndex = group.materialIndex; intersects.push( intersection ); } } } } else { const start = Math.max( 0, drawRange.start ); const end = Math.min( position.count, ( drawRange.start + drawRange.count ) ); for ( let i = start, il = end; i < il; i += 3 ) { const a = i; const b = i + 1; const c = i + 2; intersection = checkBufferGeometryIntersection( this, material, raycaster, _ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ); if ( intersection ) { intersection.faceIndex = Math.floor( i / 3 ); // triangle number in non-indexed buffer semantics intersects.push( intersection ); } } } } } else if ( geometry.isGeometry ) { const isMultiMaterial = Array.isArray( material ); const vertices = geometry.vertices; const faces = geometry.faces; let uvs; const faceVertexUvs = geometry.faceVertexUvs[ 0 ]; if ( faceVertexUvs.length > 0 ) uvs = faceVertexUvs; for ( let f = 0, fl = faces.length; f < fl; f ++ ) { const face = faces[ f ]; const faceMaterial = isMultiMaterial ? material[ face.materialIndex ] : material; if ( faceMaterial === undefined ) continue; const fvA = vertices[ face.a ]; const fvB = vertices[ face.b ]; const fvC = vertices[ face.c ]; intersection = checkIntersection( this, faceMaterial, raycaster, _ray, fvA, fvB, fvC, _intersectionPoint ); if ( intersection ) { if ( uvs && uvs[ f ] ) { const uvs_f = uvs[ f ]; _uvA.copy( uvs_f[ 0 ] ); _uvB.copy( uvs_f[ 1 ] ); _uvC.copy( uvs_f[ 2 ] ); intersection.uv = Triangle.getUV( _intersectionPoint, fvA, fvB, fvC, _uvA, _uvB, _uvC, new Vector2() ); } intersection.face = face; intersection.faceIndex = f; intersects.push( intersection ); } } } } } ); function checkIntersection( object, material, raycaster, ray, pA, pB, pC, point ) { let intersect; if ( material.side === BackSide ) { intersect = ray.intersectTriangle( pC, pB, pA, true, point ); } else { intersect = ray.intersectTriangle( pA, pB, pC, material.side !== DoubleSide, point ); } if ( intersect === null ) return null; _intersectionPointWorld.copy( point ); _intersectionPointWorld.applyMatrix4( object.matrixWorld ); const distance = raycaster.ray.origin.distanceTo( _intersectionPointWorld ); if ( distance < raycaster.near || distance > raycaster.far ) return null; return { distance: distance, point: _intersectionPointWorld.clone(), object: object }; } function checkBufferGeometryIntersection( object, material, raycaster, ray, position, morphPosition, morphTargetsRelative, uv, uv2, a, b, c ) { _vA.fromBufferAttribute( position, a ); _vB.fromBufferAttribute( position, b ); _vC.fromBufferAttribute( position, c ); const morphInfluences = object.morphTargetInfluences; if ( material.morphTargets && morphPosition && morphInfluences ) { _morphA.set( 0, 0, 0 ); _morphB.set( 0, 0, 0 ); _morphC.set( 0, 0, 0 ); for ( let i = 0, il = morphPosition.length; i < il; i ++ ) { const influence = morphInfluences[ i ]; const morphAttribute = morphPosition[ i ]; if ( influence === 0 ) continue; _tempA.fromBufferAttribute( morphAttribute, a ); _tempB.fromBufferAttribute( morphAttribute, b ); _tempC.fromBufferAttribute( morphAttribute, c ); if ( morphTargetsRelative ) { _morphA.addScaledVector( _tempA, influence ); _morphB.addScaledVector( _tempB, influence ); _morphC.addScaledVector( _tempC, influence ); } else { _morphA.addScaledVector( _tempA.sub( _vA ), influence ); _morphB.addScaledVector( _tempB.sub( _vB ), influence ); _morphC.addScaledVector( _tempC.sub( _vC ), influence ); } } _vA.add( _morphA ); _vB.add( _morphB ); _vC.add( _morphC ); } if ( object.isSkinnedMesh ) { object.boneTransform( a, _vA ); object.boneTransform( b, _vB ); object.boneTransform( c, _vC ); } const intersection = checkIntersection( object, material, raycaster, ray, _vA, _vB, _vC, _intersectionPoint ); if ( intersection ) { if ( uv ) { _uvA.fromBufferAttribute( uv, a ); _uvB.fromBufferAttribute( uv, b ); _uvC.fromBufferAttribute( uv, c ); intersection.uv = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() ); } if ( uv2 ) { _uvA.fromBufferAttribute( uv2, a ); _uvB.fromBufferAttribute( uv2, b ); _uvC.fromBufferAttribute( uv2, c ); intersection.uv2 = Triangle.getUV( _intersectionPoint, _vA, _vB, _vC, _uvA, _uvB, _uvC, new Vector2() ); } const face = new Face3( a, b, c ); Triangle.getNormal( _vA, _vB, _vC, face.normal ); intersection.face = face; } return intersection; } class BoxBufferGeometry extends BufferGeometry { constructor( width = 1, height = 1, depth = 1, widthSegments = 1, heightSegments = 1, depthSegments = 1 ) { super(); this.type = 'BoxBufferGeometry'; this.parameters = { width: width, height: height, depth: depth, widthSegments: widthSegments, heightSegments: heightSegments, depthSegments: depthSegments }; const scope = this; // segments widthSegments = Math.floor( widthSegments ); heightSegments = Math.floor( heightSegments ); depthSegments = Math.floor( depthSegments ); // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // helper variables let numberOfVertices = 0; let groupStart = 0; // build each side of the box geometry buildPlane( 'z', 'y', 'x', - 1, - 1, depth, height, width, depthSegments, heightSegments, 0 ); // px buildPlane( 'z', 'y', 'x', 1, - 1, depth, height, - width, depthSegments, heightSegments, 1 ); // nx buildPlane( 'x', 'z', 'y', 1, 1, width, depth, height, widthSegments, depthSegments, 2 ); // py buildPlane( 'x', 'z', 'y', 1, - 1, width, depth, - height, widthSegments, depthSegments, 3 ); // ny buildPlane( 'x', 'y', 'z', 1, - 1, width, height, depth, widthSegments, heightSegments, 4 ); // pz buildPlane( 'x', 'y', 'z', - 1, - 1, width, height, - depth, widthSegments, heightSegments, 5 ); // nz // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); function buildPlane( u, v, w, udir, vdir, width, height, depth, gridX, gridY, materialIndex ) { const segmentWidth = width / gridX; const segmentHeight = height / gridY; const widthHalf = width / 2; const heightHalf = height / 2; const depthHalf = depth / 2; const gridX1 = gridX + 1; const gridY1 = gridY + 1; let vertexCounter = 0; let groupCount = 0; const vector = new Vector3(); // generate vertices, normals and uvs for ( let iy = 0; iy < gridY1; iy ++ ) { const y = iy * segmentHeight - heightHalf; for ( let ix = 0; ix < gridX1; ix ++ ) { const x = ix * segmentWidth - widthHalf; // set values to correct vector component vector[ u ] = x * udir; vector[ v ] = y * vdir; vector[ w ] = depthHalf; // now apply vector to vertex buffer vertices.push( vector.x, vector.y, vector.z ); // set values to correct vector component vector[ u ] = 0; vector[ v ] = 0; vector[ w ] = depth > 0 ? 1 : - 1; // now apply vector to normal buffer normals.push( vector.x, vector.y, vector.z ); // uvs uvs.push( ix / gridX ); uvs.push( 1 - ( iy / gridY ) ); // counters vertexCounter += 1; } } // indices // 1. you need three indices to draw a single face // 2. a single segment consists of two faces // 3. so we need to generate six (2*3) indices per segment for ( let iy = 0; iy < gridY; iy ++ ) { for ( let ix = 0; ix < gridX; ix ++ ) { const a = numberOfVertices + ix + gridX1 * iy; const b = numberOfVertices + ix + gridX1 * ( iy + 1 ); const c = numberOfVertices + ( ix + 1 ) + gridX1 * ( iy + 1 ); const d = numberOfVertices + ( ix + 1 ) + gridX1 * iy; // faces indices.push( a, b, d ); indices.push( b, c, d ); // increase counter groupCount += 6; } } // add a group to the geometry. this will ensure multi material support scope.addGroup( groupStart, groupCount, materialIndex ); // calculate new start value for groups groupStart += groupCount; // update total number of vertices numberOfVertices += vertexCounter; } } } /** * Uniform Utilities */ function cloneUniforms( src ) { const dst = {}; for ( const u in src ) { dst[ u ] = {}; for ( const p in src[ u ] ) { const property = src[ u ][ p ]; if ( property && ( property.isColor || property.isMatrix3 || property.isMatrix4 || property.isVector2 || property.isVector3 || property.isVector4 || property.isTexture ) ) { dst[ u ][ p ] = property.clone(); } else if ( Array.isArray( property ) ) { dst[ u ][ p ] = property.slice(); } else { dst[ u ][ p ] = property; } } } return dst; } function mergeUniforms( uniforms ) { const merged = {}; for ( let u = 0; u < uniforms.length; u ++ ) { const tmp = cloneUniforms( uniforms[ u ] ); for ( const p in tmp ) { merged[ p ] = tmp[ p ]; } } return merged; } // Legacy const UniformsUtils = { clone: cloneUniforms, merge: mergeUniforms }; var default_vertex = "void main() {\n\tgl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n}"; var default_fragment = "void main() {\n\tgl_FragColor = vec4( 1.0, 0.0, 0.0, 1.0 );\n}"; /** * parameters = { * defines: { "label" : "value" }, * uniforms: { "parameter1": { value: 1.0 }, "parameter2": { value2: 2 } }, * * fragmentShader: , * vertexShader: , * * wireframe: , * wireframeLinewidth: , * * lights: , * * skinning: , * morphTargets: , * morphNormals: * } */ function ShaderMaterial( parameters ) { Material.call( this ); this.type = 'ShaderMaterial'; this.defines = {}; this.uniforms = {}; this.vertexShader = default_vertex; this.fragmentShader = default_fragment; this.linewidth = 1; this.wireframe = false; this.wireframeLinewidth = 1; this.fog = false; // set to use scene fog this.lights = false; // set to use scene lights this.clipping = false; // set to use user-defined clipping planes this.skinning = false; // set to use skinning attribute streams this.morphTargets = false; // set to use morph targets this.morphNormals = false; // set to use morph normals this.extensions = { derivatives: false, // set to use derivatives fragDepth: false, // set to use fragment depth values drawBuffers: false, // set to use draw buffers shaderTextureLOD: false // set to use shader texture LOD }; // When rendered geometry doesn't include these attributes but the material does, // use these default values in WebGL. This avoids errors when buffer data is missing. this.defaultAttributeValues = { 'color': [ 1, 1, 1 ], 'uv': [ 0, 0 ], 'uv2': [ 0, 0 ] }; this.index0AttributeName = undefined; this.uniformsNeedUpdate = false; this.glslVersion = null; if ( parameters !== undefined ) { if ( parameters.attributes !== undefined ) { console.error( 'THREE.ShaderMaterial: attributes should now be defined in THREE.BufferGeometry instead.' ); } this.setValues( parameters ); } } ShaderMaterial.prototype = Object.create( Material.prototype ); ShaderMaterial.prototype.constructor = ShaderMaterial; ShaderMaterial.prototype.isShaderMaterial = true; ShaderMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.fragmentShader = source.fragmentShader; this.vertexShader = source.vertexShader; this.uniforms = cloneUniforms( source.uniforms ); this.defines = Object.assign( {}, source.defines ); this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; this.lights = source.lights; this.clipping = source.clipping; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.morphNormals = source.morphNormals; this.extensions = Object.assign( {}, source.extensions ); this.glslVersion = source.glslVersion; return this; }; ShaderMaterial.prototype.toJSON = function ( meta ) { const data = Material.prototype.toJSON.call( this, meta ); data.glslVersion = this.glslVersion; data.uniforms = {}; for ( const name in this.uniforms ) { const uniform = this.uniforms[ name ]; const value = uniform.value; if ( value && value.isTexture ) { data.uniforms[ name ] = { type: 't', value: value.toJSON( meta ).uuid }; } else if ( value && value.isColor ) { data.uniforms[ name ] = { type: 'c', value: value.getHex() }; } else if ( value && value.isVector2 ) { data.uniforms[ name ] = { type: 'v2', value: value.toArray() }; } else if ( value && value.isVector3 ) { data.uniforms[ name ] = { type: 'v3', value: value.toArray() }; } else if ( value && value.isVector4 ) { data.uniforms[ name ] = { type: 'v4', value: value.toArray() }; } else if ( value && value.isMatrix3 ) { data.uniforms[ name ] = { type: 'm3', value: value.toArray() }; } else if ( value && value.isMatrix4 ) { data.uniforms[ name ] = { type: 'm4', value: value.toArray() }; } else { data.uniforms[ name ] = { value: value }; // note: the array variants v2v, v3v, v4v, m4v and tv are not supported so far } } if ( Object.keys( this.defines ).length > 0 ) data.defines = this.defines; data.vertexShader = this.vertexShader; data.fragmentShader = this.fragmentShader; const extensions = {}; for ( const key in this.extensions ) { if ( this.extensions[ key ] === true ) extensions[ key ] = true; } if ( Object.keys( extensions ).length > 0 ) data.extensions = extensions; return data; }; function Camera() { Object3D.call( this ); this.type = 'Camera'; this.matrixWorldInverse = new Matrix4(); this.projectionMatrix = new Matrix4(); this.projectionMatrixInverse = new Matrix4(); } Camera.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Camera, isCamera: true, copy: function ( source, recursive ) { Object3D.prototype.copy.call( this, source, recursive ); this.matrixWorldInverse.copy( source.matrixWorldInverse ); this.projectionMatrix.copy( source.projectionMatrix ); this.projectionMatrixInverse.copy( source.projectionMatrixInverse ); return this; }, getWorldDirection: function ( target ) { if ( target === undefined ) { console.warn( 'THREE.Camera: .getWorldDirection() target is now required' ); target = new Vector3(); } this.updateWorldMatrix( true, false ); const e = this.matrixWorld.elements; return target.set( - e[ 8 ], - e[ 9 ], - e[ 10 ] ).normalize(); }, updateMatrixWorld: function ( force ) { Object3D.prototype.updateMatrixWorld.call( this, force ); this.matrixWorldInverse.copy( this.matrixWorld ).invert(); }, updateWorldMatrix: function ( updateParents, updateChildren ) { Object3D.prototype.updateWorldMatrix.call( this, updateParents, updateChildren ); this.matrixWorldInverse.copy( this.matrixWorld ).invert(); }, clone: function () { return new this.constructor().copy( this ); } } ); function PerspectiveCamera( fov = 50, aspect = 1, near = 0.1, far = 2000 ) { Camera.call( this ); this.type = 'PerspectiveCamera'; this.fov = fov; this.zoom = 1; this.near = near; this.far = far; this.focus = 10; this.aspect = aspect; this.view = null; this.filmGauge = 35; // width of the film (default in millimeters) this.filmOffset = 0; // horizontal film offset (same unit as gauge) this.updateProjectionMatrix(); } PerspectiveCamera.prototype = Object.assign( Object.create( Camera.prototype ), { constructor: PerspectiveCamera, isPerspectiveCamera: true, copy: function ( source, recursive ) { Camera.prototype.copy.call( this, source, recursive ); this.fov = source.fov; this.zoom = source.zoom; this.near = source.near; this.far = source.far; this.focus = source.focus; this.aspect = source.aspect; this.view = source.view === null ? null : Object.assign( {}, source.view ); this.filmGauge = source.filmGauge; this.filmOffset = source.filmOffset; return this; }, /** * Sets the FOV by focal length in respect to the current .filmGauge. * * The default film gauge is 35, so that the focal length can be specified for * a 35mm (full frame) camera. * * Values for focal length and film gauge must have the same unit. */ setFocalLength: function ( focalLength ) { // see http://www.bobatkins.com/photography/technical/field_of_view.html const vExtentSlope = 0.5 * this.getFilmHeight() / focalLength; this.fov = MathUtils.RAD2DEG * 2 * Math.atan( vExtentSlope ); this.updateProjectionMatrix(); }, /** * Calculates the focal length from the current .fov and .filmGauge. */ getFocalLength: function () { const vExtentSlope = Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ); return 0.5 * this.getFilmHeight() / vExtentSlope; }, getEffectiveFOV: function () { return MathUtils.RAD2DEG * 2 * Math.atan( Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom ); }, getFilmWidth: function () { // film not completely covered in portrait format (aspect < 1) return this.filmGauge * Math.min( this.aspect, 1 ); }, getFilmHeight: function () { // film not completely covered in landscape format (aspect > 1) return this.filmGauge / Math.max( this.aspect, 1 ); }, /** * Sets an offset in a larger frustum. This is useful for multi-window or * multi-monitor/multi-machine setups. * * For example, if you have 3x2 monitors and each monitor is 1920x1080 and * the monitors are in grid like this * * +---+---+---+ * | A | B | C | * +---+---+---+ * | D | E | F | * +---+---+---+ * * then for each monitor you would call it like this * * const w = 1920; * const h = 1080; * const fullWidth = w * 3; * const fullHeight = h * 2; * * --A-- * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 0, w, h ); * --B-- * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 0, w, h ); * --C-- * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 0, w, h ); * --D-- * camera.setViewOffset( fullWidth, fullHeight, w * 0, h * 1, w, h ); * --E-- * camera.setViewOffset( fullWidth, fullHeight, w * 1, h * 1, w, h ); * --F-- * camera.setViewOffset( fullWidth, fullHeight, w * 2, h * 1, w, h ); * * Note there is no reason monitors have to be the same size or in a grid. */ setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) { this.aspect = fullWidth / fullHeight; if ( this.view === null ) { this.view = { enabled: true, fullWidth: 1, fullHeight: 1, offsetX: 0, offsetY: 0, width: 1, height: 1 }; } this.view.enabled = true; this.view.fullWidth = fullWidth; this.view.fullHeight = fullHeight; this.view.offsetX = x; this.view.offsetY = y; this.view.width = width; this.view.height = height; this.updateProjectionMatrix(); }, clearViewOffset: function () { if ( this.view !== null ) { this.view.enabled = false; } this.updateProjectionMatrix(); }, updateProjectionMatrix: function () { const near = this.near; let top = near * Math.tan( MathUtils.DEG2RAD * 0.5 * this.fov ) / this.zoom; let height = 2 * top; let width = this.aspect * height; let left = - 0.5 * width; const view = this.view; if ( this.view !== null && this.view.enabled ) { const fullWidth = view.fullWidth, fullHeight = view.fullHeight; left += view.offsetX * width / fullWidth; top -= view.offsetY * height / fullHeight; width *= view.width / fullWidth; height *= view.height / fullHeight; } const skew = this.filmOffset; if ( skew !== 0 ) left += near * skew / this.getFilmWidth(); this.projectionMatrix.makePerspective( left, left + width, top, top - height, near, this.far ); this.projectionMatrixInverse.copy( this.projectionMatrix ).invert(); }, toJSON: function ( meta ) { const data = Object3D.prototype.toJSON.call( this, meta ); data.object.fov = this.fov; data.object.zoom = this.zoom; data.object.near = this.near; data.object.far = this.far; data.object.focus = this.focus; data.object.aspect = this.aspect; if ( this.view !== null ) data.object.view = Object.assign( {}, this.view ); data.object.filmGauge = this.filmGauge; data.object.filmOffset = this.filmOffset; return data; } } ); const fov = 90, aspect = 1; function CubeCamera( near, far, renderTarget ) { Object3D.call( this ); this.type = 'CubeCamera'; if ( renderTarget.isWebGLCubeRenderTarget !== true ) { console.error( 'THREE.CubeCamera: The constructor now expects an instance of WebGLCubeRenderTarget as third parameter.' ); return; } this.renderTarget = renderTarget; const cameraPX = new PerspectiveCamera( fov, aspect, near, far ); cameraPX.layers = this.layers; cameraPX.up.set( 0, - 1, 0 ); cameraPX.lookAt( new Vector3( 1, 0, 0 ) ); this.add( cameraPX ); const cameraNX = new PerspectiveCamera( fov, aspect, near, far ); cameraNX.layers = this.layers; cameraNX.up.set( 0, - 1, 0 ); cameraNX.lookAt( new Vector3( - 1, 0, 0 ) ); this.add( cameraNX ); const cameraPY = new PerspectiveCamera( fov, aspect, near, far ); cameraPY.layers = this.layers; cameraPY.up.set( 0, 0, 1 ); cameraPY.lookAt( new Vector3( 0, 1, 0 ) ); this.add( cameraPY ); const cameraNY = new PerspectiveCamera( fov, aspect, near, far ); cameraNY.layers = this.layers; cameraNY.up.set( 0, 0, - 1 ); cameraNY.lookAt( new Vector3( 0, - 1, 0 ) ); this.add( cameraNY ); const cameraPZ = new PerspectiveCamera( fov, aspect, near, far ); cameraPZ.layers = this.layers; cameraPZ.up.set( 0, - 1, 0 ); cameraPZ.lookAt( new Vector3( 0, 0, 1 ) ); this.add( cameraPZ ); const cameraNZ = new PerspectiveCamera( fov, aspect, near, far ); cameraNZ.layers = this.layers; cameraNZ.up.set( 0, - 1, 0 ); cameraNZ.lookAt( new Vector3( 0, 0, - 1 ) ); this.add( cameraNZ ); this.update = function ( renderer, scene ) { if ( this.parent === null ) this.updateMatrixWorld(); const currentXrEnabled = renderer.xr.enabled; const currentRenderTarget = renderer.getRenderTarget(); renderer.xr.enabled = false; const generateMipmaps = renderTarget.texture.generateMipmaps; renderTarget.texture.generateMipmaps = false; renderer.setRenderTarget( renderTarget, 0 ); renderer.render( scene, cameraPX ); renderer.setRenderTarget( renderTarget, 1 ); renderer.render( scene, cameraNX ); renderer.setRenderTarget( renderTarget, 2 ); renderer.render( scene, cameraPY ); renderer.setRenderTarget( renderTarget, 3 ); renderer.render( scene, cameraNY ); renderer.setRenderTarget( renderTarget, 4 ); renderer.render( scene, cameraPZ ); renderTarget.texture.generateMipmaps = generateMipmaps; renderer.setRenderTarget( renderTarget, 5 ); renderer.render( scene, cameraNZ ); renderer.setRenderTarget( currentRenderTarget ); renderer.xr.enabled = currentXrEnabled; }; } CubeCamera.prototype = Object.create( Object3D.prototype ); CubeCamera.prototype.constructor = CubeCamera; function CubeTexture( images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ) { images = images !== undefined ? images : []; mapping = mapping !== undefined ? mapping : CubeReflectionMapping; format = format !== undefined ? format : RGBFormat; Texture.call( this, images, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ); this.flipY = false; // Why CubeTexture._needsFlipEnvMap is necessary: // // By convention -- likely based on the RenderMan spec from the 1990's -- cube maps are specified by WebGL (and three.js) // in a coordinate system in which positive-x is to the right when looking up the positive-z axis -- in other words, // in a left-handed coordinate system. By continuing this convention, preexisting cube maps continued to render correctly. // three.js uses a right-handed coordinate system. So environment maps used in three.js appear to have px and nx swapped // and the flag _needsFlipEnvMap controls this conversion. The flip is not required (and thus _needsFlipEnvMap is set to false) // when using WebGLCubeRenderTarget.texture as a cube texture. this._needsFlipEnvMap = true; } CubeTexture.prototype = Object.create( Texture.prototype ); CubeTexture.prototype.constructor = CubeTexture; CubeTexture.prototype.isCubeTexture = true; Object.defineProperty( CubeTexture.prototype, 'images', { get: function () { return this.image; }, set: function ( value ) { this.image = value; } } ); function WebGLCubeRenderTarget( size, options, dummy ) { if ( Number.isInteger( options ) ) { console.warn( 'THREE.WebGLCubeRenderTarget: constructor signature is now WebGLCubeRenderTarget( size, options )' ); options = dummy; } WebGLRenderTarget.call( this, size, size, options ); options = options || {}; this.texture = new CubeTexture( undefined, options.mapping, options.wrapS, options.wrapT, options.magFilter, options.minFilter, options.format, options.type, options.anisotropy, options.encoding ); this.texture._needsFlipEnvMap = false; } WebGLCubeRenderTarget.prototype = Object.create( WebGLRenderTarget.prototype ); WebGLCubeRenderTarget.prototype.constructor = WebGLCubeRenderTarget; WebGLCubeRenderTarget.prototype.isWebGLCubeRenderTarget = true; WebGLCubeRenderTarget.prototype.fromEquirectangularTexture = function ( renderer, texture ) { this.texture.type = texture.type; this.texture.format = RGBAFormat; // see #18859 this.texture.encoding = texture.encoding; this.texture.generateMipmaps = texture.generateMipmaps; this.texture.minFilter = texture.minFilter; this.texture.magFilter = texture.magFilter; const shader = { uniforms: { tEquirect: { value: null }, }, vertexShader: /* glsl */` varying vec3 vWorldDirection; vec3 transformDirection( in vec3 dir, in mat4 matrix ) { return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz ); } void main() { vWorldDirection = transformDirection( position, modelMatrix ); #include #include } `, fragmentShader: /* glsl */` uniform sampler2D tEquirect; varying vec3 vWorldDirection; #include void main() { vec3 direction = normalize( vWorldDirection ); vec2 sampleUV = equirectUv( direction ); gl_FragColor = texture2D( tEquirect, sampleUV ); } ` }; const geometry = new BoxBufferGeometry( 5, 5, 5 ); const material = new ShaderMaterial( { name: 'CubemapFromEquirect', uniforms: cloneUniforms( shader.uniforms ), vertexShader: shader.vertexShader, fragmentShader: shader.fragmentShader, side: BackSide, blending: NoBlending } ); material.uniforms.tEquirect.value = texture; const mesh = new Mesh( geometry, material ); const currentMinFilter = texture.minFilter; // Avoid blurred poles if ( texture.minFilter === LinearMipmapLinearFilter ) texture.minFilter = LinearFilter; const camera = new CubeCamera( 1, 10, this ); camera.update( renderer, mesh ); texture.minFilter = currentMinFilter; mesh.geometry.dispose(); mesh.material.dispose(); return this; }; WebGLCubeRenderTarget.prototype.clear = function ( renderer, color, depth, stencil ) { const currentRenderTarget = renderer.getRenderTarget(); for ( let i = 0; i < 6; i ++ ) { renderer.setRenderTarget( this, i ); renderer.clear( color, depth, stencil ); } renderer.setRenderTarget( currentRenderTarget ); }; function DataTexture( data, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding ) { Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ); this.image = { data: data || null, width: width || 1, height: height || 1 }; this.magFilter = magFilter !== undefined ? magFilter : NearestFilter; this.minFilter = minFilter !== undefined ? minFilter : NearestFilter; this.generateMipmaps = false; this.flipY = false; this.unpackAlignment = 1; this.needsUpdate = true; } DataTexture.prototype = Object.create( Texture.prototype ); DataTexture.prototype.constructor = DataTexture; DataTexture.prototype.isDataTexture = true; const _sphere$1 = /*@__PURE__*/ new Sphere(); const _vector$5 = /*@__PURE__*/ new Vector3(); class Frustum { constructor( p0, p1, p2, p3, p4, p5 ) { this.planes = [ ( p0 !== undefined ) ? p0 : new Plane(), ( p1 !== undefined ) ? p1 : new Plane(), ( p2 !== undefined ) ? p2 : new Plane(), ( p3 !== undefined ) ? p3 : new Plane(), ( p4 !== undefined ) ? p4 : new Plane(), ( p5 !== undefined ) ? p5 : new Plane() ]; } set( p0, p1, p2, p3, p4, p5 ) { const planes = this.planes; planes[ 0 ].copy( p0 ); planes[ 1 ].copy( p1 ); planes[ 2 ].copy( p2 ); planes[ 3 ].copy( p3 ); planes[ 4 ].copy( p4 ); planes[ 5 ].copy( p5 ); return this; } clone() { return new this.constructor().copy( this ); } copy( frustum ) { const planes = this.planes; for ( let i = 0; i < 6; i ++ ) { planes[ i ].copy( frustum.planes[ i ] ); } return this; } setFromProjectionMatrix( m ) { const planes = this.planes; const me = m.elements; const me0 = me[ 0 ], me1 = me[ 1 ], me2 = me[ 2 ], me3 = me[ 3 ]; const me4 = me[ 4 ], me5 = me[ 5 ], me6 = me[ 6 ], me7 = me[ 7 ]; const me8 = me[ 8 ], me9 = me[ 9 ], me10 = me[ 10 ], me11 = me[ 11 ]; const me12 = me[ 12 ], me13 = me[ 13 ], me14 = me[ 14 ], me15 = me[ 15 ]; planes[ 0 ].setComponents( me3 - me0, me7 - me4, me11 - me8, me15 - me12 ).normalize(); planes[ 1 ].setComponents( me3 + me0, me7 + me4, me11 + me8, me15 + me12 ).normalize(); planes[ 2 ].setComponents( me3 + me1, me7 + me5, me11 + me9, me15 + me13 ).normalize(); planes[ 3 ].setComponents( me3 - me1, me7 - me5, me11 - me9, me15 - me13 ).normalize(); planes[ 4 ].setComponents( me3 - me2, me7 - me6, me11 - me10, me15 - me14 ).normalize(); planes[ 5 ].setComponents( me3 + me2, me7 + me6, me11 + me10, me15 + me14 ).normalize(); return this; } intersectsObject( object ) { const geometry = object.geometry; if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); _sphere$1.copy( geometry.boundingSphere ).applyMatrix4( object.matrixWorld ); return this.intersectsSphere( _sphere$1 ); } intersectsSprite( sprite ) { _sphere$1.center.set( 0, 0, 0 ); _sphere$1.radius = 0.7071067811865476; _sphere$1.applyMatrix4( sprite.matrixWorld ); return this.intersectsSphere( _sphere$1 ); } intersectsSphere( sphere ) { const planes = this.planes; const center = sphere.center; const negRadius = - sphere.radius; for ( let i = 0; i < 6; i ++ ) { const distance = planes[ i ].distanceToPoint( center ); if ( distance < negRadius ) { return false; } } return true; } intersectsBox( box ) { const planes = this.planes; for ( let i = 0; i < 6; i ++ ) { const plane = planes[ i ]; // corner at max distance _vector$5.x = plane.normal.x > 0 ? box.max.x : box.min.x; _vector$5.y = plane.normal.y > 0 ? box.max.y : box.min.y; _vector$5.z = plane.normal.z > 0 ? box.max.z : box.min.z; if ( plane.distanceToPoint( _vector$5 ) < 0 ) { return false; } } return true; } containsPoint( point ) { const planes = this.planes; for ( let i = 0; i < 6; i ++ ) { if ( planes[ i ].distanceToPoint( point ) < 0 ) { return false; } } return true; } } function WebGLAnimation() { let context = null; let isAnimating = false; let animationLoop = null; let requestId = null; function onAnimationFrame( time, frame ) { animationLoop( time, frame ); requestId = context.requestAnimationFrame( onAnimationFrame ); } return { start: function () { if ( isAnimating === true ) return; if ( animationLoop === null ) return; requestId = context.requestAnimationFrame( onAnimationFrame ); isAnimating = true; }, stop: function () { context.cancelAnimationFrame( requestId ); isAnimating = false; }, setAnimationLoop: function ( callback ) { animationLoop = callback; }, setContext: function ( value ) { context = value; } }; } function WebGLAttributes( gl, capabilities ) { const isWebGL2 = capabilities.isWebGL2; const buffers = new WeakMap(); function createBuffer( attribute, bufferType ) { const array = attribute.array; const usage = attribute.usage; const buffer = gl.createBuffer(); gl.bindBuffer( bufferType, buffer ); gl.bufferData( bufferType, array, usage ); attribute.onUploadCallback(); let type = 5126; if ( array instanceof Float32Array ) { type = 5126; } else if ( array instanceof Float64Array ) { console.warn( 'THREE.WebGLAttributes: Unsupported data buffer format: Float64Array.' ); } else if ( array instanceof Uint16Array ) { if ( attribute.isFloat16BufferAttribute ) { if ( isWebGL2 ) { type = 5131; } else { console.warn( 'THREE.WebGLAttributes: Usage of Float16BufferAttribute requires WebGL2.' ); } } else { type = 5123; } } else if ( array instanceof Int16Array ) { type = 5122; } else if ( array instanceof Uint32Array ) { type = 5125; } else if ( array instanceof Int32Array ) { type = 5124; } else if ( array instanceof Int8Array ) { type = 5120; } else if ( array instanceof Uint8Array ) { type = 5121; } return { buffer: buffer, type: type, bytesPerElement: array.BYTES_PER_ELEMENT, version: attribute.version }; } function updateBuffer( buffer, attribute, bufferType ) { const array = attribute.array; const updateRange = attribute.updateRange; gl.bindBuffer( bufferType, buffer ); if ( updateRange.count === - 1 ) { // Not using update ranges gl.bufferSubData( bufferType, 0, array ); } else { if ( isWebGL2 ) { gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT, array, updateRange.offset, updateRange.count ); } else { gl.bufferSubData( bufferType, updateRange.offset * array.BYTES_PER_ELEMENT, array.subarray( updateRange.offset, updateRange.offset + updateRange.count ) ); } updateRange.count = - 1; // reset range } } // function get( attribute ) { if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data; return buffers.get( attribute ); } function remove( attribute ) { if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data; const data = buffers.get( attribute ); if ( data ) { gl.deleteBuffer( data.buffer ); buffers.delete( attribute ); } } function update( attribute, bufferType ) { if ( attribute.isGLBufferAttribute ) { const cached = buffers.get( attribute ); if ( ! cached || cached.version < attribute.version ) { buffers.set( attribute, { buffer: attribute.buffer, type: attribute.type, bytesPerElement: attribute.elementSize, version: attribute.version } ); } return; } if ( attribute.isInterleavedBufferAttribute ) attribute = attribute.data; const data = buffers.get( attribute ); if ( data === undefined ) { buffers.set( attribute, createBuffer( attribute, bufferType ) ); } else if ( data.version < attribute.version ) { updateBuffer( data.buffer, attribute, bufferType ); data.version = attribute.version; } } return { get: get, remove: remove, update: update }; } class PlaneBufferGeometry extends BufferGeometry { constructor( width = 1, height = 1, widthSegments = 1, heightSegments = 1 ) { super(); this.type = 'PlaneBufferGeometry'; this.parameters = { width: width, height: height, widthSegments: widthSegments, heightSegments: heightSegments }; const width_half = width / 2; const height_half = height / 2; const gridX = Math.floor( widthSegments ); const gridY = Math.floor( heightSegments ); const gridX1 = gridX + 1; const gridY1 = gridY + 1; const segment_width = width / gridX; const segment_height = height / gridY; // const indices = []; const vertices = []; const normals = []; const uvs = []; for ( let iy = 0; iy < gridY1; iy ++ ) { const y = iy * segment_height - height_half; for ( let ix = 0; ix < gridX1; ix ++ ) { const x = ix * segment_width - width_half; vertices.push( x, - y, 0 ); normals.push( 0, 0, 1 ); uvs.push( ix / gridX ); uvs.push( 1 - ( iy / gridY ) ); } } for ( let iy = 0; iy < gridY; iy ++ ) { for ( let ix = 0; ix < gridX; ix ++ ) { const a = ix + gridX1 * iy; const b = ix + gridX1 * ( iy + 1 ); const c = ( ix + 1 ) + gridX1 * ( iy + 1 ); const d = ( ix + 1 ) + gridX1 * iy; indices.push( a, b, d ); indices.push( b, c, d ); } } this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); } } var alphamap_fragment = "#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, vUv ).g;\n#endif"; var alphamap_pars_fragment = "#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif"; var alphatest_fragment = "#ifdef ALPHATEST\n\tif ( diffuseColor.a < ALPHATEST ) discard;\n#endif"; var aomap_fragment = "#ifdef USE_AOMAP\n\tfloat ambientOcclusion = ( texture2D( aoMap, vUv2 ).r - 1.0 ) * aoMapIntensity + 1.0;\n\treflectedLight.indirectDiffuse *= ambientOcclusion;\n\t#if defined( USE_ENVMAP ) && defined( STANDARD )\n\t\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\t\treflectedLight.indirectSpecular *= computeSpecularOcclusion( dotNV, ambientOcclusion, material.specularRoughness );\n\t#endif\n#endif"; var aomap_pars_fragment = "#ifdef USE_AOMAP\n\tuniform sampler2D aoMap;\n\tuniform float aoMapIntensity;\n#endif"; var begin_vertex = "vec3 transformed = vec3( position );"; var beginnormal_vertex = "vec3 objectNormal = vec3( normal );\n#ifdef USE_TANGENT\n\tvec3 objectTangent = vec3( tangent.xyz );\n#endif"; var bsdfs = "vec2 integrateSpecularBRDF( const in float dotNV, const in float roughness ) {\n\tconst vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );\n\tconst vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );\n\tvec4 r = roughness * c0 + c1;\n\tfloat a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;\n\treturn vec2( -1.04, 1.04 ) * a004 + r.zw;\n}\nfloat punctualLightIntensityToIrradianceFactor( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {\n#if defined ( PHYSICALLY_CORRECT_LIGHTS )\n\tfloat distanceFalloff = 1.0 / max( pow( lightDistance, decayExponent ), 0.01 );\n\tif( cutoffDistance > 0.0 ) {\n\t\tdistanceFalloff *= pow2( saturate( 1.0 - pow4( lightDistance / cutoffDistance ) ) );\n\t}\n\treturn distanceFalloff;\n#else\n\tif( cutoffDistance > 0.0 && decayExponent > 0.0 ) {\n\t\treturn pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );\n\t}\n\treturn 1.0;\n#endif\n}\nvec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {\n\treturn RECIPROCAL_PI * diffuseColor;\n}\nvec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {\n\tfloat fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );\n\treturn ( 1.0 - specularColor ) * fresnel + specularColor;\n}\nvec3 F_Schlick_RoughnessDependent( const in vec3 F0, const in float dotNV, const in float roughness ) {\n\tfloat fresnel = exp2( ( -5.55473 * dotNV - 6.98316 ) * dotNV );\n\tvec3 Fr = max( vec3( 1.0 - roughness ), F0 ) - F0;\n\treturn Fr * fresnel + F0;\n}\nfloat G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\tfloat gl = dotNL + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\tfloat gv = dotNV + sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\treturn 1.0 / ( gl * gv );\n}\nfloat G_GGX_SmithCorrelated( const in float alpha, const in float dotNL, const in float dotNV ) {\n\tfloat a2 = pow2( alpha );\n\tfloat gv = dotNL * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNV ) );\n\tfloat gl = dotNV * sqrt( a2 + ( 1.0 - a2 ) * pow2( dotNL ) );\n\treturn 0.5 / max( gv + gl, EPSILON );\n}\nfloat D_GGX( const in float alpha, const in float dotNH ) {\n\tfloat a2 = pow2( alpha );\n\tfloat denom = pow2( dotNH ) * ( a2 - 1.0 ) + 1.0;\n\treturn RECIPROCAL_PI * a2 / pow2( denom );\n}\nvec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat alpha = pow2( roughness );\n\tvec3 halfDir = normalize( incidentLight.direction + viewDir );\n\tfloat dotNL = saturate( dot( normal, incidentLight.direction ) );\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tfloat dotNH = saturate( dot( normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_GGX_SmithCorrelated( alpha, dotNL, dotNV );\n\tfloat D = D_GGX( alpha, dotNH );\n\treturn F * ( G * D );\n}\nvec2 LTC_Uv( const in vec3 N, const in vec3 V, const in float roughness ) {\n\tconst float LUT_SIZE = 64.0;\n\tconst float LUT_SCALE = ( LUT_SIZE - 1.0 ) / LUT_SIZE;\n\tconst float LUT_BIAS = 0.5 / LUT_SIZE;\n\tfloat dotNV = saturate( dot( N, V ) );\n\tvec2 uv = vec2( roughness, sqrt( 1.0 - dotNV ) );\n\tuv = uv * LUT_SCALE + LUT_BIAS;\n\treturn uv;\n}\nfloat LTC_ClippedSphereFormFactor( const in vec3 f ) {\n\tfloat l = length( f );\n\treturn max( ( l * l + f.z ) / ( l + 1.0 ), 0.0 );\n}\nvec3 LTC_EdgeVectorFormFactor( const in vec3 v1, const in vec3 v2 ) {\n\tfloat x = dot( v1, v2 );\n\tfloat y = abs( x );\n\tfloat a = 0.8543985 + ( 0.4965155 + 0.0145206 * y ) * y;\n\tfloat b = 3.4175940 + ( 4.1616724 + y ) * y;\n\tfloat v = a / b;\n\tfloat theta_sintheta = ( x > 0.0 ) ? v : 0.5 * inversesqrt( max( 1.0 - x * x, 1e-7 ) ) - v;\n\treturn cross( v1, v2 ) * theta_sintheta;\n}\nvec3 LTC_Evaluate( const in vec3 N, const in vec3 V, const in vec3 P, const in mat3 mInv, const in vec3 rectCoords[ 4 ] ) {\n\tvec3 v1 = rectCoords[ 1 ] - rectCoords[ 0 ];\n\tvec3 v2 = rectCoords[ 3 ] - rectCoords[ 0 ];\n\tvec3 lightNormal = cross( v1, v2 );\n\tif( dot( lightNormal, P - rectCoords[ 0 ] ) < 0.0 ) return vec3( 0.0 );\n\tvec3 T1, T2;\n\tT1 = normalize( V - N * dot( V, N ) );\n\tT2 = - cross( N, T1 );\n\tmat3 mat = mInv * transposeMat3( mat3( T1, T2, N ) );\n\tvec3 coords[ 4 ];\n\tcoords[ 0 ] = mat * ( rectCoords[ 0 ] - P );\n\tcoords[ 1 ] = mat * ( rectCoords[ 1 ] - P );\n\tcoords[ 2 ] = mat * ( rectCoords[ 2 ] - P );\n\tcoords[ 3 ] = mat * ( rectCoords[ 3 ] - P );\n\tcoords[ 0 ] = normalize( coords[ 0 ] );\n\tcoords[ 1 ] = normalize( coords[ 1 ] );\n\tcoords[ 2 ] = normalize( coords[ 2 ] );\n\tcoords[ 3 ] = normalize( coords[ 3 ] );\n\tvec3 vectorFormFactor = vec3( 0.0 );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 0 ], coords[ 1 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 1 ], coords[ 2 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 2 ], coords[ 3 ] );\n\tvectorFormFactor += LTC_EdgeVectorFormFactor( coords[ 3 ], coords[ 0 ] );\n\tfloat result = LTC_ClippedSphereFormFactor( vectorFormFactor );\n\treturn vec3( result );\n}\nvec3 BRDF_Specular_GGX_Environment( const in vec3 viewDir, const in vec3 normal, const in vec3 specularColor, const in float roughness ) {\n\tfloat dotNV = saturate( dot( normal, viewDir ) );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\treturn specularColor * brdf.x + brdf.y;\n}\nvoid BRDF_Specular_Multiscattering_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness, inout vec3 singleScatter, inout vec3 multiScatter ) {\n\tfloat dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );\n\tvec3 F = F_Schlick_RoughnessDependent( specularColor, dotNV, roughness );\n\tvec2 brdf = integrateSpecularBRDF( dotNV, roughness );\n\tvec3 FssEss = F * brdf.x + brdf.y;\n\tfloat Ess = brdf.x + brdf.y;\n\tfloat Ems = 1.0 - Ess;\n\tvec3 Favg = specularColor + ( 1.0 - specularColor ) * 0.047619;\tvec3 Fms = FssEss * Favg / ( 1.0 - Ems * Favg );\n\tsingleScatter += FssEss;\n\tmultiScatter += Fms * Ems;\n}\nfloat G_BlinnPhong_Implicit( ) {\n\treturn 0.25;\n}\nfloat D_BlinnPhong( const in float shininess, const in float dotNH ) {\n\treturn RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );\n}\nvec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {\n\tvec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );\n\tfloat dotNH = saturate( dot( geometry.normal, halfDir ) );\n\tfloat dotLH = saturate( dot( incidentLight.direction, halfDir ) );\n\tvec3 F = F_Schlick( specularColor, dotLH );\n\tfloat G = G_BlinnPhong_Implicit( );\n\tfloat D = D_BlinnPhong( shininess, dotNH );\n\treturn F * ( G * D );\n}\nfloat GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {\n\treturn ( 2.0 / pow2( ggxRoughness + 0.0001 ) - 2.0 );\n}\nfloat BlinnExponentToGGXRoughness( const in float blinnExponent ) {\n\treturn sqrt( 2.0 / ( blinnExponent + 2.0 ) );\n}\n#if defined( USE_SHEEN )\nfloat D_Charlie(float roughness, float NoH) {\n\tfloat invAlpha = 1.0 / roughness;\n\tfloat cos2h = NoH * NoH;\n\tfloat sin2h = max(1.0 - cos2h, 0.0078125);\treturn (2.0 + invAlpha) * pow(sin2h, invAlpha * 0.5) / (2.0 * PI);\n}\nfloat V_Neubelt(float NoV, float NoL) {\n\treturn saturate(1.0 / (4.0 * (NoL + NoV - NoL * NoV)));\n}\nvec3 BRDF_Specular_Sheen( const in float roughness, const in vec3 L, const in GeometricContext geometry, vec3 specularColor ) {\n\tvec3 N = geometry.normal;\n\tvec3 V = geometry.viewDir;\n\tvec3 H = normalize( V + L );\n\tfloat dotNH = saturate( dot( N, H ) );\n\treturn specularColor * D_Charlie( roughness, dotNH ) * V_Neubelt( dot(N, V), dot(N, L) );\n}\n#endif"; var bumpmap_pars_fragment = "#ifdef USE_BUMPMAP\n\tuniform sampler2D bumpMap;\n\tuniform float bumpScale;\n\tvec2 dHdxy_fwd() {\n\t\tvec2 dSTdx = dFdx( vUv );\n\t\tvec2 dSTdy = dFdy( vUv );\n\t\tfloat Hll = bumpScale * texture2D( bumpMap, vUv ).x;\n\t\tfloat dBx = bumpScale * texture2D( bumpMap, vUv + dSTdx ).x - Hll;\n\t\tfloat dBy = bumpScale * texture2D( bumpMap, vUv + dSTdy ).x - Hll;\n\t\treturn vec2( dBx, dBy );\n\t}\n\tvec3 perturbNormalArb( vec3 surf_pos, vec3 surf_norm, vec2 dHdxy ) {\n\t\tvec3 vSigmaX = vec3( dFdx( surf_pos.x ), dFdx( surf_pos.y ), dFdx( surf_pos.z ) );\n\t\tvec3 vSigmaY = vec3( dFdy( surf_pos.x ), dFdy( surf_pos.y ), dFdy( surf_pos.z ) );\n\t\tvec3 vN = surf_norm;\n\t\tvec3 R1 = cross( vSigmaY, vN );\n\t\tvec3 R2 = cross( vN, vSigmaX );\n\t\tfloat fDet = dot( vSigmaX, R1 );\n\t\tfDet *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\tvec3 vGrad = sign( fDet ) * ( dHdxy.x * R1 + dHdxy.y * R2 );\n\t\treturn normalize( abs( fDet ) * surf_norm - vGrad );\n\t}\n#endif"; var clipping_planes_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvec4 plane;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < UNION_CLIPPING_PLANES; i ++ ) {\n\t\tplane = clippingPlanes[ i ];\n\t\tif ( dot( vClipPosition, plane.xyz ) > plane.w ) discard;\n\t}\n\t#pragma unroll_loop_end\n\t#if UNION_CLIPPING_PLANES < NUM_CLIPPING_PLANES\n\t\tbool clipped = true;\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = UNION_CLIPPING_PLANES; i < NUM_CLIPPING_PLANES; i ++ ) {\n\t\t\tplane = clippingPlanes[ i ];\n\t\t\tclipped = ( dot( vClipPosition, plane.xyz ) > plane.w ) && clipped;\n\t\t}\n\t\t#pragma unroll_loop_end\n\t\tif ( clipped ) discard;\n\t#endif\n#endif"; var clipping_planes_pars_fragment = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n\tuniform vec4 clippingPlanes[ NUM_CLIPPING_PLANES ];\n#endif"; var clipping_planes_pars_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvarying vec3 vClipPosition;\n#endif"; var clipping_planes_vertex = "#if NUM_CLIPPING_PLANES > 0\n\tvClipPosition = - mvPosition.xyz;\n#endif"; var color_fragment = "#ifdef USE_COLOR\n\tdiffuseColor.rgb *= vColor;\n#endif"; var color_pars_fragment = "#ifdef USE_COLOR\n\tvarying vec3 vColor;\n#endif"; var color_pars_vertex = "#if defined( USE_COLOR ) || defined( USE_INSTANCING_COLOR )\n\tvarying vec3 vColor;\n#endif"; var color_vertex = "#if defined( USE_COLOR ) || defined( USE_INSTANCING_COLOR )\n\tvColor = vec3( 1.0 );\n#endif\n#ifdef USE_COLOR\n\tvColor.xyz *= color.xyz;\n#endif\n#ifdef USE_INSTANCING_COLOR\n\tvColor.xyz *= instanceColor.xyz;\n#endif"; var common = "#define PI 3.141592653589793\n#define PI2 6.283185307179586\n#define PI_HALF 1.5707963267948966\n#define RECIPROCAL_PI 0.3183098861837907\n#define RECIPROCAL_PI2 0.15915494309189535\n#define EPSILON 1e-6\n#ifndef saturate\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#endif\n#define whiteComplement(a) ( 1.0 - saturate( a ) )\nfloat pow2( const in float x ) { return x*x; }\nfloat pow3( const in float x ) { return x*x*x; }\nfloat pow4( const in float x ) { float x2 = x*x; return x2*x2; }\nfloat average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }\nhighp float rand( const in vec2 uv ) {\n\tconst highp float a = 12.9898, b = 78.233, c = 43758.5453;\n\thighp float dt = dot( uv.xy, vec2( a,b ) ), sn = mod( dt, PI );\n\treturn fract(sin(sn) * c);\n}\n#ifdef HIGH_PRECISION\n\tfloat precisionSafeLength( vec3 v ) { return length( v ); }\n#else\n\tfloat max3( vec3 v ) { return max( max( v.x, v.y ), v.z ); }\n\tfloat precisionSafeLength( vec3 v ) {\n\t\tfloat maxComponent = max3( abs( v ) );\n\t\treturn length( v / maxComponent ) * maxComponent;\n\t}\n#endif\nstruct IncidentLight {\n\tvec3 color;\n\tvec3 direction;\n\tbool visible;\n};\nstruct ReflectedLight {\n\tvec3 directDiffuse;\n\tvec3 directSpecular;\n\tvec3 indirectDiffuse;\n\tvec3 indirectSpecular;\n};\nstruct GeometricContext {\n\tvec3 position;\n\tvec3 normal;\n\tvec3 viewDir;\n#ifdef CLEARCOAT\n\tvec3 clearcoatNormal;\n#endif\n};\nvec3 transformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );\n}\nvec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {\n\treturn normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );\n}\nvec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\tfloat distance = dot( planeNormal, point - pointOnPlane );\n\treturn - distance * planeNormal + point;\n}\nfloat sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\treturn sign( dot( point - pointOnPlane, planeNormal ) );\n}\nvec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {\n\treturn lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;\n}\nmat3 transposeMat3( const in mat3 m ) {\n\tmat3 tmp;\n\ttmp[ 0 ] = vec3( m[ 0 ].x, m[ 1 ].x, m[ 2 ].x );\n\ttmp[ 1 ] = vec3( m[ 0 ].y, m[ 1 ].y, m[ 2 ].y );\n\ttmp[ 2 ] = vec3( m[ 0 ].z, m[ 1 ].z, m[ 2 ].z );\n\treturn tmp;\n}\nfloat linearToRelativeLuminance( const in vec3 color ) {\n\tvec3 weights = vec3( 0.2126, 0.7152, 0.0722 );\n\treturn dot( weights, color.rgb );\n}\nbool isPerspectiveMatrix( mat4 m ) {\n\treturn m[ 2 ][ 3 ] == - 1.0;\n}\nvec2 equirectUv( in vec3 dir ) {\n\tfloat u = atan( dir.z, dir.x ) * RECIPROCAL_PI2 + 0.5;\n\tfloat v = asin( clamp( dir.y, - 1.0, 1.0 ) ) * RECIPROCAL_PI + 0.5;\n\treturn vec2( u, v );\n}"; var cube_uv_reflection_fragment = "#ifdef ENVMAP_TYPE_CUBE_UV\n\t#define cubeUV_maxMipLevel 8.0\n\t#define cubeUV_minMipLevel 4.0\n\t#define cubeUV_maxTileSize 256.0\n\t#define cubeUV_minTileSize 16.0\n\tfloat getFace( vec3 direction ) {\n\t\tvec3 absDirection = abs( direction );\n\t\tfloat face = - 1.0;\n\t\tif ( absDirection.x > absDirection.z ) {\n\t\t\tif ( absDirection.x > absDirection.y )\n\t\t\t\tface = direction.x > 0.0 ? 0.0 : 3.0;\n\t\t\telse\n\t\t\t\tface = direction.y > 0.0 ? 1.0 : 4.0;\n\t\t} else {\n\t\t\tif ( absDirection.z > absDirection.y )\n\t\t\t\tface = direction.z > 0.0 ? 2.0 : 5.0;\n\t\t\telse\n\t\t\t\tface = direction.y > 0.0 ? 1.0 : 4.0;\n\t\t}\n\t\treturn face;\n\t}\n\tvec2 getUV( vec3 direction, float face ) {\n\t\tvec2 uv;\n\t\tif ( face == 0.0 ) {\n\t\t\tuv = vec2( direction.z, direction.y ) / abs( direction.x );\n\t\t} else if ( face == 1.0 ) {\n\t\t\tuv = vec2( - direction.x, - direction.z ) / abs( direction.y );\n\t\t} else if ( face == 2.0 ) {\n\t\t\tuv = vec2( - direction.x, direction.y ) / abs( direction.z );\n\t\t} else if ( face == 3.0 ) {\n\t\t\tuv = vec2( - direction.z, direction.y ) / abs( direction.x );\n\t\t} else if ( face == 4.0 ) {\n\t\t\tuv = vec2( - direction.x, direction.z ) / abs( direction.y );\n\t\t} else {\n\t\t\tuv = vec2( direction.x, direction.y ) / abs( direction.z );\n\t\t}\n\t\treturn 0.5 * ( uv + 1.0 );\n\t}\n\tvec3 bilinearCubeUV( sampler2D envMap, vec3 direction, float mipInt ) {\n\t\tfloat face = getFace( direction );\n\t\tfloat filterInt = max( cubeUV_minMipLevel - mipInt, 0.0 );\n\t\tmipInt = max( mipInt, cubeUV_minMipLevel );\n\t\tfloat faceSize = exp2( mipInt );\n\t\tfloat texelSize = 1.0 / ( 3.0 * cubeUV_maxTileSize );\n\t\tvec2 uv = getUV( direction, face ) * ( faceSize - 1.0 );\n\t\tvec2 f = fract( uv );\n\t\tuv += 0.5 - f;\n\t\tif ( face > 2.0 ) {\n\t\t\tuv.y += faceSize;\n\t\t\tface -= 3.0;\n\t\t}\n\t\tuv.x += face * faceSize;\n\t\tif ( mipInt < cubeUV_maxMipLevel ) {\n\t\t\tuv.y += 2.0 * cubeUV_maxTileSize;\n\t\t}\n\t\tuv.y += filterInt * 2.0 * cubeUV_minTileSize;\n\t\tuv.x += 3.0 * max( 0.0, cubeUV_maxTileSize - 2.0 * faceSize );\n\t\tuv *= texelSize;\n\t\tvec3 tl = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;\n\t\tuv.x += texelSize;\n\t\tvec3 tr = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;\n\t\tuv.y += texelSize;\n\t\tvec3 br = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;\n\t\tuv.x -= texelSize;\n\t\tvec3 bl = envMapTexelToLinear( texture2D( envMap, uv ) ).rgb;\n\t\tvec3 tm = mix( tl, tr, f.x );\n\t\tvec3 bm = mix( bl, br, f.x );\n\t\treturn mix( tm, bm, f.y );\n\t}\n\t#define r0 1.0\n\t#define v0 0.339\n\t#define m0 - 2.0\n\t#define r1 0.8\n\t#define v1 0.276\n\t#define m1 - 1.0\n\t#define r4 0.4\n\t#define v4 0.046\n\t#define m4 2.0\n\t#define r5 0.305\n\t#define v5 0.016\n\t#define m5 3.0\n\t#define r6 0.21\n\t#define v6 0.0038\n\t#define m6 4.0\n\tfloat roughnessToMip( float roughness ) {\n\t\tfloat mip = 0.0;\n\t\tif ( roughness >= r1 ) {\n\t\t\tmip = ( r0 - roughness ) * ( m1 - m0 ) / ( r0 - r1 ) + m0;\n\t\t} else if ( roughness >= r4 ) {\n\t\t\tmip = ( r1 - roughness ) * ( m4 - m1 ) / ( r1 - r4 ) + m1;\n\t\t} else if ( roughness >= r5 ) {\n\t\t\tmip = ( r4 - roughness ) * ( m5 - m4 ) / ( r4 - r5 ) + m4;\n\t\t} else if ( roughness >= r6 ) {\n\t\t\tmip = ( r5 - roughness ) * ( m6 - m5 ) / ( r5 - r6 ) + m5;\n\t\t} else {\n\t\t\tmip = - 2.0 * log2( 1.16 * roughness );\t\t}\n\t\treturn mip;\n\t}\n\tvec4 textureCubeUV( sampler2D envMap, vec3 sampleDir, float roughness ) {\n\t\tfloat mip = clamp( roughnessToMip( roughness ), m0, cubeUV_maxMipLevel );\n\t\tfloat mipF = fract( mip );\n\t\tfloat mipInt = floor( mip );\n\t\tvec3 color0 = bilinearCubeUV( envMap, sampleDir, mipInt );\n\t\tif ( mipF == 0.0 ) {\n\t\t\treturn vec4( color0, 1.0 );\n\t\t} else {\n\t\t\tvec3 color1 = bilinearCubeUV( envMap, sampleDir, mipInt + 1.0 );\n\t\t\treturn vec4( mix( color0, color1, mipF ), 1.0 );\n\t\t}\n\t}\n#endif"; var defaultnormal_vertex = "vec3 transformedNormal = objectNormal;\n#ifdef USE_INSTANCING\n\tmat3 m = mat3( instanceMatrix );\n\ttransformedNormal /= vec3( dot( m[ 0 ], m[ 0 ] ), dot( m[ 1 ], m[ 1 ] ), dot( m[ 2 ], m[ 2 ] ) );\n\ttransformedNormal = m * transformedNormal;\n#endif\ntransformedNormal = normalMatrix * transformedNormal;\n#ifdef FLIP_SIDED\n\ttransformedNormal = - transformedNormal;\n#endif\n#ifdef USE_TANGENT\n\tvec3 transformedTangent = ( modelViewMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#ifdef FLIP_SIDED\n\t\ttransformedTangent = - transformedTangent;\n\t#endif\n#endif"; var displacementmap_pars_vertex = "#ifdef USE_DISPLACEMENTMAP\n\tuniform sampler2D displacementMap;\n\tuniform float displacementScale;\n\tuniform float displacementBias;\n#endif"; var displacementmap_vertex = "#ifdef USE_DISPLACEMENTMAP\n\ttransformed += normalize( objectNormal ) * ( texture2D( displacementMap, vUv ).x * displacementScale + displacementBias );\n#endif"; var emissivemap_fragment = "#ifdef USE_EMISSIVEMAP\n\tvec4 emissiveColor = texture2D( emissiveMap, vUv );\n\temissiveColor.rgb = emissiveMapTexelToLinear( emissiveColor ).rgb;\n\ttotalEmissiveRadiance *= emissiveColor.rgb;\n#endif"; var emissivemap_pars_fragment = "#ifdef USE_EMISSIVEMAP\n\tuniform sampler2D emissiveMap;\n#endif"; var encodings_fragment = "gl_FragColor = linearToOutputTexel( gl_FragColor );"; var encodings_pars_fragment = "\nvec4 LinearToLinear( in vec4 value ) {\n\treturn value;\n}\nvec4 GammaToLinear( in vec4 value, in float gammaFactor ) {\n\treturn vec4( pow( value.rgb, vec3( gammaFactor ) ), value.a );\n}\nvec4 LinearToGamma( in vec4 value, in float gammaFactor ) {\n\treturn vec4( pow( value.rgb, vec3( 1.0 / gammaFactor ) ), value.a );\n}\nvec4 sRGBToLinear( in vec4 value ) {\n\treturn vec4( mix( pow( value.rgb * 0.9478672986 + vec3( 0.0521327014 ), vec3( 2.4 ) ), value.rgb * 0.0773993808, vec3( lessThanEqual( value.rgb, vec3( 0.04045 ) ) ) ), value.a );\n}\nvec4 LinearTosRGB( in vec4 value ) {\n\treturn vec4( mix( pow( value.rgb, vec3( 0.41666 ) ) * 1.055 - vec3( 0.055 ), value.rgb * 12.92, vec3( lessThanEqual( value.rgb, vec3( 0.0031308 ) ) ) ), value.a );\n}\nvec4 RGBEToLinear( in vec4 value ) {\n\treturn vec4( value.rgb * exp2( value.a * 255.0 - 128.0 ), 1.0 );\n}\nvec4 LinearToRGBE( in vec4 value ) {\n\tfloat maxComponent = max( max( value.r, value.g ), value.b );\n\tfloat fExp = clamp( ceil( log2( maxComponent ) ), -128.0, 127.0 );\n\treturn vec4( value.rgb / exp2( fExp ), ( fExp + 128.0 ) / 255.0 );\n}\nvec4 RGBMToLinear( in vec4 value, in float maxRange ) {\n\treturn vec4( value.rgb * value.a * maxRange, 1.0 );\n}\nvec4 LinearToRGBM( in vec4 value, in float maxRange ) {\n\tfloat maxRGB = max( value.r, max( value.g, value.b ) );\n\tfloat M = clamp( maxRGB / maxRange, 0.0, 1.0 );\n\tM = ceil( M * 255.0 ) / 255.0;\n\treturn vec4( value.rgb / ( M * maxRange ), M );\n}\nvec4 RGBDToLinear( in vec4 value, in float maxRange ) {\n\treturn vec4( value.rgb * ( ( maxRange / 255.0 ) / value.a ), 1.0 );\n}\nvec4 LinearToRGBD( in vec4 value, in float maxRange ) {\n\tfloat maxRGB = max( value.r, max( value.g, value.b ) );\n\tfloat D = max( maxRange / maxRGB, 1.0 );\n\tD = clamp( floor( D ) / 255.0, 0.0, 1.0 );\n\treturn vec4( value.rgb * ( D * ( 255.0 / maxRange ) ), D );\n}\nconst mat3 cLogLuvM = mat3( 0.2209, 0.3390, 0.4184, 0.1138, 0.6780, 0.7319, 0.0102, 0.1130, 0.2969 );\nvec4 LinearToLogLuv( in vec4 value ) {\n\tvec3 Xp_Y_XYZp = cLogLuvM * value.rgb;\n\tXp_Y_XYZp = max( Xp_Y_XYZp, vec3( 1e-6, 1e-6, 1e-6 ) );\n\tvec4 vResult;\n\tvResult.xy = Xp_Y_XYZp.xy / Xp_Y_XYZp.z;\n\tfloat Le = 2.0 * log2(Xp_Y_XYZp.y) + 127.0;\n\tvResult.w = fract( Le );\n\tvResult.z = ( Le - ( floor( vResult.w * 255.0 ) ) / 255.0 ) / 255.0;\n\treturn vResult;\n}\nconst mat3 cLogLuvInverseM = mat3( 6.0014, -2.7008, -1.7996, -1.3320, 3.1029, -5.7721, 0.3008, -1.0882, 5.6268 );\nvec4 LogLuvToLinear( in vec4 value ) {\n\tfloat Le = value.z * 255.0 + value.w;\n\tvec3 Xp_Y_XYZp;\n\tXp_Y_XYZp.y = exp2( ( Le - 127.0 ) / 2.0 );\n\tXp_Y_XYZp.z = Xp_Y_XYZp.y / value.y;\n\tXp_Y_XYZp.x = value.x * Xp_Y_XYZp.z;\n\tvec3 vRGB = cLogLuvInverseM * Xp_Y_XYZp.rgb;\n\treturn vec4( max( vRGB, 0.0 ), 1.0 );\n}"; var envmap_fragment = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvec3 cameraToFrag;\n\t\tif ( isOrthographic ) {\n\t\t\tcameraToFrag = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t} else {\n\t\t\tcameraToFrag = normalize( vWorldPosition - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( normal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvec3 reflectVec = reflect( cameraToFrag, worldNormal );\n\t\t#else\n\t\t\tvec3 reflectVec = refract( cameraToFrag, worldNormal, refractionRatio );\n\t\t#endif\n\t#else\n\t\tvec3 reflectVec = vReflect;\n\t#endif\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tvec4 envColor = textureCube( envMap, vec3( flipEnvMap * reflectVec.x, reflectVec.yz ) );\n\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\tvec4 envColor = textureCubeUV( envMap, reflectVec, 0.0 );\n\t#else\n\t\tvec4 envColor = vec4( 0.0 );\n\t#endif\n\t#ifndef ENVMAP_TYPE_CUBE_UV\n\t\tenvColor = envMapTexelToLinear( envColor );\n\t#endif\n\t#ifdef ENVMAP_BLENDING_MULTIPLY\n\t\toutgoingLight = mix( outgoingLight, outgoingLight * envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_MIX )\n\t\toutgoingLight = mix( outgoingLight, envColor.xyz, specularStrength * reflectivity );\n\t#elif defined( ENVMAP_BLENDING_ADD )\n\t\toutgoingLight += envColor.xyz * specularStrength * reflectivity;\n\t#endif\n#endif"; var envmap_common_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float envMapIntensity;\n\tuniform float flipEnvMap;\n\tuniform int maxMipLevel;\n\t#ifdef ENVMAP_TYPE_CUBE\n\t\tuniform samplerCube envMap;\n\t#else\n\t\tuniform sampler2D envMap;\n\t#endif\n\t\n#endif"; var envmap_pars_fragment = "#ifdef USE_ENVMAP\n\tuniform float reflectivity;\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( PHONG )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\tvarying vec3 vWorldPosition;\n\t\tuniform float refractionRatio;\n\t#else\n\t\tvarying vec3 vReflect;\n\t#endif\n#endif"; var envmap_pars_vertex = "#ifdef USE_ENVMAP\n\t#if defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) ||defined( PHONG )\n\t\t#define ENV_WORLDPOS\n\t#endif\n\t#ifdef ENV_WORLDPOS\n\t\t\n\t\tvarying vec3 vWorldPosition;\n\t#else\n\t\tvarying vec3 vReflect;\n\t\tuniform float refractionRatio;\n\t#endif\n#endif"; var envmap_vertex = "#ifdef USE_ENVMAP\n\t#ifdef ENV_WORLDPOS\n\t\tvWorldPosition = worldPosition.xyz;\n\t#else\n\t\tvec3 cameraToVertex;\n\t\tif ( isOrthographic ) {\n\t\t\tcameraToVertex = normalize( vec3( - viewMatrix[ 0 ][ 2 ], - viewMatrix[ 1 ][ 2 ], - viewMatrix[ 2 ][ 2 ] ) );\n\t\t} else {\n\t\t\tcameraToVertex = normalize( worldPosition.xyz - cameraPosition );\n\t\t}\n\t\tvec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvReflect = reflect( cameraToVertex, worldNormal );\n\t\t#else\n\t\t\tvReflect = refract( cameraToVertex, worldNormal, refractionRatio );\n\t\t#endif\n\t#endif\n#endif"; var fog_vertex = "#ifdef USE_FOG\n\tfogDepth = - mvPosition.z;\n#endif"; var fog_pars_vertex = "#ifdef USE_FOG\n\tvarying float fogDepth;\n#endif"; var fog_fragment = "#ifdef USE_FOG\n\t#ifdef FOG_EXP2\n\t\tfloat fogFactor = 1.0 - exp( - fogDensity * fogDensity * fogDepth * fogDepth );\n\t#else\n\t\tfloat fogFactor = smoothstep( fogNear, fogFar, fogDepth );\n\t#endif\n\tgl_FragColor.rgb = mix( gl_FragColor.rgb, fogColor, fogFactor );\n#endif"; var fog_pars_fragment = "#ifdef USE_FOG\n\tuniform vec3 fogColor;\n\tvarying float fogDepth;\n\t#ifdef FOG_EXP2\n\t\tuniform float fogDensity;\n\t#else\n\t\tuniform float fogNear;\n\t\tuniform float fogFar;\n\t#endif\n#endif"; var gradientmap_pars_fragment = "#ifdef USE_GRADIENTMAP\n\tuniform sampler2D gradientMap;\n#endif\nvec3 getGradientIrradiance( vec3 normal, vec3 lightDirection ) {\n\tfloat dotNL = dot( normal, lightDirection );\n\tvec2 coord = vec2( dotNL * 0.5 + 0.5, 0.0 );\n\t#ifdef USE_GRADIENTMAP\n\t\treturn texture2D( gradientMap, coord ).rgb;\n\t#else\n\t\treturn ( coord.x < 0.7 ) ? vec3( 0.7 ) : vec3( 1.0 );\n\t#endif\n}"; var lightmap_fragment = "#ifdef USE_LIGHTMAP\n\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\treflectedLight.indirectDiffuse += PI * lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n#endif"; var lightmap_pars_fragment = "#ifdef USE_LIGHTMAP\n\tuniform sampler2D lightMap;\n\tuniform float lightMapIntensity;\n#endif"; var lights_lambert_vertex = "vec3 diffuse = vec3( 1.0 );\nGeometricContext geometry;\ngeometry.position = mvPosition.xyz;\ngeometry.normal = normalize( transformedNormal );\ngeometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( -mvPosition.xyz );\nGeometricContext backGeometry;\nbackGeometry.position = geometry.position;\nbackGeometry.normal = -geometry.normal;\nbackGeometry.viewDir = geometry.viewDir;\nvLightFront = vec3( 0.0 );\nvIndirectFront = vec3( 0.0 );\n#ifdef DOUBLE_SIDED\n\tvLightBack = vec3( 0.0 );\n\tvIndirectBack = vec3( 0.0 );\n#endif\nIncidentLight directLight;\nfloat dotNL;\nvec3 directLightColor_Diffuse;\nvIndirectFront += getAmbientLightIrradiance( ambientLightColor );\nvIndirectFront += getLightProbeIrradiance( lightProbe, geometry );\n#ifdef DOUBLE_SIDED\n\tvIndirectBack += getAmbientLightIrradiance( ambientLightColor );\n\tvIndirectBack += getLightProbeIrradiance( lightProbe, backGeometry );\n#endif\n#if NUM_POINT_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {\n\t\tgetPointDirectLightIrradiance( pointLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_SPOT_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {\n\t\tgetSpotDirectLightIrradiance( spotLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_DIR_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {\n\t\tgetDirectionalDirectLightIrradiance( directionalLights[ i ], geometry, directLight );\n\t\tdotNL = dot( geometry.normal, directLight.direction );\n\t\tdirectLightColor_Diffuse = PI * directLight.color;\n\t\tvLightFront += saturate( dotNL ) * directLightColor_Diffuse;\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvLightBack += saturate( -dotNL ) * directLightColor_Diffuse;\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if NUM_HEMI_LIGHTS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {\n\t\tvIndirectFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );\n\t\t#ifdef DOUBLE_SIDED\n\t\t\tvIndirectBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometry );\n\t\t#endif\n\t}\n\t#pragma unroll_loop_end\n#endif"; var lights_pars_begin = "uniform bool receiveShadow;\nuniform vec3 ambientLightColor;\nuniform vec3 lightProbe[ 9 ];\nvec3 shGetIrradianceAt( in vec3 normal, in vec3 shCoefficients[ 9 ] ) {\n\tfloat x = normal.x, y = normal.y, z = normal.z;\n\tvec3 result = shCoefficients[ 0 ] * 0.886227;\n\tresult += shCoefficients[ 1 ] * 2.0 * 0.511664 * y;\n\tresult += shCoefficients[ 2 ] * 2.0 * 0.511664 * z;\n\tresult += shCoefficients[ 3 ] * 2.0 * 0.511664 * x;\n\tresult += shCoefficients[ 4 ] * 2.0 * 0.429043 * x * y;\n\tresult += shCoefficients[ 5 ] * 2.0 * 0.429043 * y * z;\n\tresult += shCoefficients[ 6 ] * ( 0.743125 * z * z - 0.247708 );\n\tresult += shCoefficients[ 7 ] * 2.0 * 0.429043 * x * z;\n\tresult += shCoefficients[ 8 ] * 0.429043 * ( x * x - y * y );\n\treturn result;\n}\nvec3 getLightProbeIrradiance( const in vec3 lightProbe[ 9 ], const in GeometricContext geometry ) {\n\tvec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );\n\tvec3 irradiance = shGetIrradianceAt( worldNormal, lightProbe );\n\treturn irradiance;\n}\nvec3 getAmbientLightIrradiance( const in vec3 ambientLightColor ) {\n\tvec3 irradiance = ambientLightColor;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treturn irradiance;\n}\n#if NUM_DIR_LIGHTS > 0\n\tstruct DirectionalLight {\n\t\tvec3 direction;\n\t\tvec3 color;\n\t};\n\tuniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];\n\tvoid getDirectionalDirectLightIrradiance( const in DirectionalLight directionalLight, const in GeometricContext geometry, out IncidentLight directLight ) {\n\t\tdirectLight.color = directionalLight.color;\n\t\tdirectLight.direction = directionalLight.direction;\n\t\tdirectLight.visible = true;\n\t}\n#endif\n#if NUM_POINT_LIGHTS > 0\n\tstruct PointLight {\n\t\tvec3 position;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t};\n\tuniform PointLight pointLights[ NUM_POINT_LIGHTS ];\n\tvoid getPointDirectLightIrradiance( const in PointLight pointLight, const in GeometricContext geometry, out IncidentLight directLight ) {\n\t\tvec3 lVector = pointLight.position - geometry.position;\n\t\tdirectLight.direction = normalize( lVector );\n\t\tfloat lightDistance = length( lVector );\n\t\tdirectLight.color = pointLight.color;\n\t\tdirectLight.color *= punctualLightIntensityToIrradianceFactor( lightDistance, pointLight.distance, pointLight.decay );\n\t\tdirectLight.visible = ( directLight.color != vec3( 0.0 ) );\n\t}\n#endif\n#if NUM_SPOT_LIGHTS > 0\n\tstruct SpotLight {\n\t\tvec3 position;\n\t\tvec3 direction;\n\t\tvec3 color;\n\t\tfloat distance;\n\t\tfloat decay;\n\t\tfloat coneCos;\n\t\tfloat penumbraCos;\n\t};\n\tuniform SpotLight spotLights[ NUM_SPOT_LIGHTS ];\n\tvoid getSpotDirectLightIrradiance( const in SpotLight spotLight, const in GeometricContext geometry, out IncidentLight directLight ) {\n\t\tvec3 lVector = spotLight.position - geometry.position;\n\t\tdirectLight.direction = normalize( lVector );\n\t\tfloat lightDistance = length( lVector );\n\t\tfloat angleCos = dot( directLight.direction, spotLight.direction );\n\t\tif ( angleCos > spotLight.coneCos ) {\n\t\t\tfloat spotEffect = smoothstep( spotLight.coneCos, spotLight.penumbraCos, angleCos );\n\t\t\tdirectLight.color = spotLight.color;\n\t\t\tdirectLight.color *= spotEffect * punctualLightIntensityToIrradianceFactor( lightDistance, spotLight.distance, spotLight.decay );\n\t\t\tdirectLight.visible = true;\n\t\t} else {\n\t\t\tdirectLight.color = vec3( 0.0 );\n\t\t\tdirectLight.visible = false;\n\t\t}\n\t}\n#endif\n#if NUM_RECT_AREA_LIGHTS > 0\n\tstruct RectAreaLight {\n\t\tvec3 color;\n\t\tvec3 position;\n\t\tvec3 halfWidth;\n\t\tvec3 halfHeight;\n\t};\n\tuniform sampler2D ltc_1;\tuniform sampler2D ltc_2;\n\tuniform RectAreaLight rectAreaLights[ NUM_RECT_AREA_LIGHTS ];\n#endif\n#if NUM_HEMI_LIGHTS > 0\n\tstruct HemisphereLight {\n\t\tvec3 direction;\n\t\tvec3 skyColor;\n\t\tvec3 groundColor;\n\t};\n\tuniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];\n\tvec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {\n\t\tfloat dotNL = dot( geometry.normal, hemiLight.direction );\n\t\tfloat hemiDiffuseWeight = 0.5 * dotNL + 0.5;\n\t\tvec3 irradiance = mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tirradiance *= PI;\n\t\t#endif\n\t\treturn irradiance;\n\t}\n#endif"; var envmap_physical_pars_fragment = "#if defined( USE_ENVMAP )\n\t#ifdef ENVMAP_MODE_REFRACTION\n\t\tuniform float refractionRatio;\n\t#endif\n\tvec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {\n\t\tvec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );\n\t\t#ifdef ENVMAP_TYPE_CUBE\n\t\t\tvec3 queryVec = vec3( flipEnvMap * worldNormal.x, worldNormal.yz );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, worldNormal, 1.0 );\n\t\t#else\n\t\t\tvec4 envMapColor = vec4( 0.0 );\n\t\t#endif\n\t\treturn PI * envMapColor.rgb * envMapIntensity;\n\t}\n\tfloat getSpecularMIPLevel( const in float roughness, const in int maxMIPLevel ) {\n\t\tfloat maxMIPLevelScalar = float( maxMIPLevel );\n\t\tfloat sigma = PI * roughness * roughness / ( 1.0 + roughness );\n\t\tfloat desiredMIPLevel = maxMIPLevelScalar + log2( sigma );\n\t\treturn clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );\n\t}\n\tvec3 getLightProbeIndirectRadiance( const in vec3 viewDir, const in vec3 normal, const in float roughness, const in int maxMIPLevel ) {\n\t\t#ifdef ENVMAP_MODE_REFLECTION\n\t\t\tvec3 reflectVec = reflect( -viewDir, normal );\n\t\t\treflectVec = normalize( mix( reflectVec, normal, roughness * roughness) );\n\t\t#else\n\t\t\tvec3 reflectVec = refract( -viewDir, normal, refractionRatio );\n\t\t#endif\n\t\treflectVec = inverseTransformDirection( reflectVec, viewMatrix );\n\t\tfloat specularMIPLevel = getSpecularMIPLevel( roughness, maxMIPLevel );\n\t\t#ifdef ENVMAP_TYPE_CUBE\n\t\t\tvec3 queryReflectVec = vec3( flipEnvMap * reflectVec.x, reflectVec.yz );\n\t\t\t#ifdef TEXTURE_LOD_EXT\n\t\t\t\tvec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );\n\t\t\t#else\n\t\t\t\tvec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );\n\t\t\t#endif\n\t\t\tenvMapColor.rgb = envMapTexelToLinear( envMapColor ).rgb;\n\t\t#elif defined( ENVMAP_TYPE_CUBE_UV )\n\t\t\tvec4 envMapColor = textureCubeUV( envMap, reflectVec, roughness );\n\t\t#endif\n\t\treturn envMapColor.rgb * envMapIntensity;\n\t}\n#endif"; var lights_toon_fragment = "ToonMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;"; var lights_toon_pars_fragment = "varying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\nstruct ToonMaterial {\n\tvec3 diffuseColor;\n};\nvoid RE_Direct_Toon( const in IncidentLight directLight, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\tvec3 irradiance = getGradientIrradiance( geometry.normal, directLight.direction ) * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectDiffuse_Toon( const in vec3 irradiance, const in GeometricContext geometry, const in ToonMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_Toon\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Toon\n#define Material_LightProbeLOD( material )\t(0)"; var lights_phong_fragment = "BlinnPhongMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb;\nmaterial.specularColor = specular;\nmaterial.specularShininess = shininess;\nmaterial.specularStrength = specularStrength;"; var lights_phong_pars_fragment = "varying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\nstruct BlinnPhongMaterial {\n\tvec3 diffuseColor;\n\tvec3 specularColor;\n\tfloat specularShininess;\n\tfloat specularStrength;\n};\nvoid RE_Direct_BlinnPhong( const in IncidentLight directLight, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometry.normal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\treflectedLight.directDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n\treflectedLight.directSpecular += irradiance * BRDF_Specular_BlinnPhong( directLight, geometry, material.specularColor, material.specularShininess ) * material.specularStrength;\n}\nvoid RE_IndirectDiffuse_BlinnPhong( const in vec3 irradiance, const in GeometricContext geometry, const in BlinnPhongMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\n#define RE_Direct\t\t\t\tRE_Direct_BlinnPhong\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_BlinnPhong\n#define Material_LightProbeLOD( material )\t(0)"; var lights_physical_fragment = "PhysicalMaterial material;\nmaterial.diffuseColor = diffuseColor.rgb * ( 1.0 - metalnessFactor );\nvec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );\nfloat geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );\nmaterial.specularRoughness = max( roughnessFactor, 0.0525 );material.specularRoughness += geometryRoughness;\nmaterial.specularRoughness = min( material.specularRoughness, 1.0 );\n#ifdef REFLECTIVITY\n\tmaterial.specularColor = mix( vec3( MAXIMUM_SPECULAR_COEFFICIENT * pow2( reflectivity ) ), diffuseColor.rgb, metalnessFactor );\n#else\n\tmaterial.specularColor = mix( vec3( DEFAULT_SPECULAR_COEFFICIENT ), diffuseColor.rgb, metalnessFactor );\n#endif\n#ifdef CLEARCOAT\n\tmaterial.clearcoat = clearcoat;\n\tmaterial.clearcoatRoughness = clearcoatRoughness;\n\t#ifdef USE_CLEARCOATMAP\n\t\tmaterial.clearcoat *= texture2D( clearcoatMap, vUv ).x;\n\t#endif\n\t#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\t\tmaterial.clearcoatRoughness *= texture2D( clearcoatRoughnessMap, vUv ).y;\n\t#endif\n\tmaterial.clearcoat = saturate( material.clearcoat );\tmaterial.clearcoatRoughness = max( material.clearcoatRoughness, 0.0525 );\n\tmaterial.clearcoatRoughness += geometryRoughness;\n\tmaterial.clearcoatRoughness = min( material.clearcoatRoughness, 1.0 );\n#endif\n#ifdef USE_SHEEN\n\tmaterial.sheenColor = sheen;\n#endif"; var lights_physical_pars_fragment = "struct PhysicalMaterial {\n\tvec3 diffuseColor;\n\tfloat specularRoughness;\n\tvec3 specularColor;\n#ifdef CLEARCOAT\n\tfloat clearcoat;\n\tfloat clearcoatRoughness;\n#endif\n#ifdef USE_SHEEN\n\tvec3 sheenColor;\n#endif\n};\n#define MAXIMUM_SPECULAR_COEFFICIENT 0.16\n#define DEFAULT_SPECULAR_COEFFICIENT 0.04\nfloat clearcoatDHRApprox( const in float roughness, const in float dotNL ) {\n\treturn DEFAULT_SPECULAR_COEFFICIENT + ( 1.0 - DEFAULT_SPECULAR_COEFFICIENT ) * ( pow( 1.0 - dotNL, 5.0 ) * pow( 1.0 - roughness, 2.0 ) );\n}\n#if NUM_RECT_AREA_LIGHTS > 0\n\tvoid RE_Direct_RectArea_Physical( const in RectAreaLight rectAreaLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\t\tvec3 normal = geometry.normal;\n\t\tvec3 viewDir = geometry.viewDir;\n\t\tvec3 position = geometry.position;\n\t\tvec3 lightPos = rectAreaLight.position;\n\t\tvec3 halfWidth = rectAreaLight.halfWidth;\n\t\tvec3 halfHeight = rectAreaLight.halfHeight;\n\t\tvec3 lightColor = rectAreaLight.color;\n\t\tfloat roughness = material.specularRoughness;\n\t\tvec3 rectCoords[ 4 ];\n\t\trectCoords[ 0 ] = lightPos + halfWidth - halfHeight;\t\trectCoords[ 1 ] = lightPos - halfWidth - halfHeight;\n\t\trectCoords[ 2 ] = lightPos - halfWidth + halfHeight;\n\t\trectCoords[ 3 ] = lightPos + halfWidth + halfHeight;\n\t\tvec2 uv = LTC_Uv( normal, viewDir, roughness );\n\t\tvec4 t1 = texture2D( ltc_1, uv );\n\t\tvec4 t2 = texture2D( ltc_2, uv );\n\t\tmat3 mInv = mat3(\n\t\t\tvec3( t1.x, 0, t1.y ),\n\t\t\tvec3( 0, 1, 0 ),\n\t\t\tvec3( t1.z, 0, t1.w )\n\t\t);\n\t\tvec3 fresnel = ( material.specularColor * t2.x + ( vec3( 1.0 ) - material.specularColor ) * t2.y );\n\t\treflectedLight.directSpecular += lightColor * fresnel * LTC_Evaluate( normal, viewDir, position, mInv, rectCoords );\n\t\treflectedLight.directDiffuse += lightColor * material.diffuseColor * LTC_Evaluate( normal, viewDir, position, mat3( 1.0 ), rectCoords );\n\t}\n#endif\nvoid RE_Direct_Physical( const in IncidentLight directLight, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\tfloat dotNL = saturate( dot( geometry.normal, directLight.direction ) );\n\tvec3 irradiance = dotNL * directLight.color;\n\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\tirradiance *= PI;\n\t#endif\n\t#ifdef CLEARCOAT\n\t\tfloat ccDotNL = saturate( dot( geometry.clearcoatNormal, directLight.direction ) );\n\t\tvec3 ccIrradiance = ccDotNL * directLight.color;\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tccIrradiance *= PI;\n\t\t#endif\n\t\tfloat clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );\n\t\treflectedLight.directSpecular += ccIrradiance * material.clearcoat * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );\n\t#else\n\t\tfloat clearcoatDHR = 0.0;\n\t#endif\n\t#ifdef USE_SHEEN\n\t\treflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_Sheen(\n\t\t\tmaterial.specularRoughness,\n\t\t\tdirectLight.direction,\n\t\t\tgeometry,\n\t\t\tmaterial.sheenColor\n\t\t);\n\t#else\n\t\treflectedLight.directSpecular += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Specular_GGX( directLight, geometry.viewDir, geometry.normal, material.specularColor, material.specularRoughness);\n\t#endif\n\treflectedLight.directDiffuse += ( 1.0 - clearcoatDHR ) * irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectDiffuse_Physical( const in vec3 irradiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight ) {\n\treflectedLight.indirectDiffuse += irradiance * BRDF_Diffuse_Lambert( material.diffuseColor );\n}\nvoid RE_IndirectSpecular_Physical( const in vec3 radiance, const in vec3 irradiance, const in vec3 clearcoatRadiance, const in GeometricContext geometry, const in PhysicalMaterial material, inout ReflectedLight reflectedLight) {\n\t#ifdef CLEARCOAT\n\t\tfloat ccDotNV = saturate( dot( geometry.clearcoatNormal, geometry.viewDir ) );\n\t\treflectedLight.indirectSpecular += clearcoatRadiance * material.clearcoat * BRDF_Specular_GGX_Environment( geometry.viewDir, geometry.clearcoatNormal, vec3( DEFAULT_SPECULAR_COEFFICIENT ), material.clearcoatRoughness );\n\t\tfloat ccDotNL = ccDotNV;\n\t\tfloat clearcoatDHR = material.clearcoat * clearcoatDHRApprox( material.clearcoatRoughness, ccDotNL );\n\t#else\n\t\tfloat clearcoatDHR = 0.0;\n\t#endif\n\tfloat clearcoatInv = 1.0 - clearcoatDHR;\n\tvec3 singleScattering = vec3( 0.0 );\n\tvec3 multiScattering = vec3( 0.0 );\n\tvec3 cosineWeightedIrradiance = irradiance * RECIPROCAL_PI;\n\tBRDF_Specular_Multiscattering_Environment( geometry, material.specularColor, material.specularRoughness, singleScattering, multiScattering );\n\tvec3 diffuse = material.diffuseColor * ( 1.0 - ( singleScattering + multiScattering ) );\n\treflectedLight.indirectSpecular += clearcoatInv * radiance * singleScattering;\n\treflectedLight.indirectSpecular += multiScattering * cosineWeightedIrradiance;\n\treflectedLight.indirectDiffuse += diffuse * cosineWeightedIrradiance;\n}\n#define RE_Direct\t\t\t\tRE_Direct_Physical\n#define RE_Direct_RectArea\t\tRE_Direct_RectArea_Physical\n#define RE_IndirectDiffuse\t\tRE_IndirectDiffuse_Physical\n#define RE_IndirectSpecular\t\tRE_IndirectSpecular_Physical\nfloat computeSpecularOcclusion( const in float dotNV, const in float ambientOcclusion, const in float roughness ) {\n\treturn saturate( pow( dotNV + ambientOcclusion, exp2( - 16.0 * roughness - 1.0 ) ) - 1.0 + ambientOcclusion );\n}"; var lights_fragment_begin = "\nGeometricContext geometry;\ngeometry.position = - vViewPosition;\ngeometry.normal = normal;\ngeometry.viewDir = ( isOrthographic ) ? vec3( 0, 0, 1 ) : normalize( vViewPosition );\n#ifdef CLEARCOAT\n\tgeometry.clearcoatNormal = clearcoatNormal;\n#endif\nIncidentLight directLight;\n#if ( NUM_POINT_LIGHTS > 0 ) && defined( RE_Direct )\n\tPointLight pointLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {\n\t\tpointLight = pointLights[ i ];\n\t\tgetPointDirectLightIrradiance( pointLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_POINT_LIGHT_SHADOWS )\n\t\tpointLightShadow = pointLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getPointShadow( pointShadowMap[ i ], pointLightShadow.shadowMapSize, pointLightShadow.shadowBias, pointLightShadow.shadowRadius, vPointShadowCoord[ i ], pointLightShadow.shadowCameraNear, pointLightShadow.shadowCameraFar ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_SPOT_LIGHTS > 0 ) && defined( RE_Direct )\n\tSpotLight spotLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {\n\t\tspotLight = spotLights[ i ];\n\t\tgetSpotDirectLightIrradiance( spotLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_SPOT_LIGHT_SHADOWS )\n\t\tspotLightShadow = spotLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( spotShadowMap[ i ], spotLightShadow.shadowMapSize, spotLightShadow.shadowBias, spotLightShadow.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_DIR_LIGHTS > 0 ) && defined( RE_Direct )\n\tDirectionalLight directionalLight;\n\t#if defined( USE_SHADOWMAP ) && NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLightShadow;\n\t#endif\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {\n\t\tdirectionalLight = directionalLights[ i ];\n\t\tgetDirectionalDirectLightIrradiance( directionalLight, geometry, directLight );\n\t\t#if defined( USE_SHADOWMAP ) && ( UNROLLED_LOOP_INDEX < NUM_DIR_LIGHT_SHADOWS )\n\t\tdirectionalLightShadow = directionalLightShadows[ i ];\n\t\tdirectLight.color *= all( bvec2( directLight.visible, receiveShadow ) ) ? getShadow( directionalShadowMap[ i ], directionalLightShadow.shadowMapSize, directionalLightShadow.shadowBias, directionalLightShadow.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t\t#endif\n\t\tRE_Direct( directLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if ( NUM_RECT_AREA_LIGHTS > 0 ) && defined( RE_Direct_RectArea )\n\tRectAreaLight rectAreaLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_RECT_AREA_LIGHTS; i ++ ) {\n\t\trectAreaLight = rectAreaLights[ i ];\n\t\tRE_Direct_RectArea( rectAreaLight, geometry, material, reflectedLight );\n\t}\n\t#pragma unroll_loop_end\n#endif\n#if defined( RE_IndirectDiffuse )\n\tvec3 iblIrradiance = vec3( 0.0 );\n\tvec3 irradiance = getAmbientLightIrradiance( ambientLightColor );\n\tirradiance += getLightProbeIrradiance( lightProbe, geometry );\n\t#if ( NUM_HEMI_LIGHTS > 0 )\n\t\t#pragma unroll_loop_start\n\t\tfor ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {\n\t\t\tirradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );\n\t\t}\n\t\t#pragma unroll_loop_end\n\t#endif\n#endif\n#if defined( RE_IndirectSpecular )\n\tvec3 radiance = vec3( 0.0 );\n\tvec3 clearcoatRadiance = vec3( 0.0 );\n#endif"; var lights_fragment_maps = "#if defined( RE_IndirectDiffuse )\n\t#ifdef USE_LIGHTMAP\n\t\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\t\tvec3 lightMapIrradiance = lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n\t\t#ifndef PHYSICALLY_CORRECT_LIGHTS\n\t\t\tlightMapIrradiance *= PI;\n\t\t#endif\n\t\tirradiance += lightMapIrradiance;\n\t#endif\n\t#if defined( USE_ENVMAP ) && defined( STANDARD ) && defined( ENVMAP_TYPE_CUBE_UV )\n\t\tiblIrradiance += getLightProbeIndirectIrradiance( geometry, maxMipLevel );\n\t#endif\n#endif\n#if defined( USE_ENVMAP ) && defined( RE_IndirectSpecular )\n\tradiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.normal, material.specularRoughness, maxMipLevel );\n\t#ifdef CLEARCOAT\n\t\tclearcoatRadiance += getLightProbeIndirectRadiance( geometry.viewDir, geometry.clearcoatNormal, material.clearcoatRoughness, maxMipLevel );\n\t#endif\n#endif"; var lights_fragment_end = "#if defined( RE_IndirectDiffuse )\n\tRE_IndirectDiffuse( irradiance, geometry, material, reflectedLight );\n#endif\n#if defined( RE_IndirectSpecular )\n\tRE_IndirectSpecular( radiance, iblIrradiance, clearcoatRadiance, geometry, material, reflectedLight );\n#endif"; var logdepthbuf_fragment = "#if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )\n\tgl_FragDepthEXT = vIsPerspective == 0.0 ? gl_FragCoord.z : log2( vFragDepth ) * logDepthBufFC * 0.5;\n#endif"; var logdepthbuf_pars_fragment = "#if defined( USE_LOGDEPTHBUF ) && defined( USE_LOGDEPTHBUF_EXT )\n\tuniform float logDepthBufFC;\n\tvarying float vFragDepth;\n\tvarying float vIsPerspective;\n#endif"; var logdepthbuf_pars_vertex = "#ifdef USE_LOGDEPTHBUF\n\t#ifdef USE_LOGDEPTHBUF_EXT\n\t\tvarying float vFragDepth;\n\t\tvarying float vIsPerspective;\n\t#else\n\t\tuniform float logDepthBufFC;\n\t#endif\n#endif"; var logdepthbuf_vertex = "#ifdef USE_LOGDEPTHBUF\n\t#ifdef USE_LOGDEPTHBUF_EXT\n\t\tvFragDepth = 1.0 + gl_Position.w;\n\t\tvIsPerspective = float( isPerspectiveMatrix( projectionMatrix ) );\n\t#else\n\t\tif ( isPerspectiveMatrix( projectionMatrix ) ) {\n\t\t\tgl_Position.z = log2( max( EPSILON, gl_Position.w + 1.0 ) ) * logDepthBufFC - 1.0;\n\t\t\tgl_Position.z *= gl_Position.w;\n\t\t}\n\t#endif\n#endif"; var map_fragment = "#ifdef USE_MAP\n\tvec4 texelColor = texture2D( map, vUv );\n\ttexelColor = mapTexelToLinear( texelColor );\n\tdiffuseColor *= texelColor;\n#endif"; var map_pars_fragment = "#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif"; var map_particle_fragment = "#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\tvec2 uv = ( uvTransform * vec3( gl_PointCoord.x, 1.0 - gl_PointCoord.y, 1 ) ).xy;\n#endif\n#ifdef USE_MAP\n\tvec4 mapTexel = texture2D( map, uv );\n\tdiffuseColor *= mapTexelToLinear( mapTexel );\n#endif\n#ifdef USE_ALPHAMAP\n\tdiffuseColor.a *= texture2D( alphaMap, uv ).g;\n#endif"; var map_particle_pars_fragment = "#if defined( USE_MAP ) || defined( USE_ALPHAMAP )\n\tuniform mat3 uvTransform;\n#endif\n#ifdef USE_MAP\n\tuniform sampler2D map;\n#endif\n#ifdef USE_ALPHAMAP\n\tuniform sampler2D alphaMap;\n#endif"; var metalnessmap_fragment = "float metalnessFactor = metalness;\n#ifdef USE_METALNESSMAP\n\tvec4 texelMetalness = texture2D( metalnessMap, vUv );\n\tmetalnessFactor *= texelMetalness.b;\n#endif"; var metalnessmap_pars_fragment = "#ifdef USE_METALNESSMAP\n\tuniform sampler2D metalnessMap;\n#endif"; var morphnormal_vertex = "#ifdef USE_MORPHNORMALS\n\tobjectNormal *= morphTargetBaseInfluence;\n\tobjectNormal += morphNormal0 * morphTargetInfluences[ 0 ];\n\tobjectNormal += morphNormal1 * morphTargetInfluences[ 1 ];\n\tobjectNormal += morphNormal2 * morphTargetInfluences[ 2 ];\n\tobjectNormal += morphNormal3 * morphTargetInfluences[ 3 ];\n#endif"; var morphtarget_pars_vertex = "#ifdef USE_MORPHTARGETS\n\tuniform float morphTargetBaseInfluence;\n\t#ifndef USE_MORPHNORMALS\n\t\tuniform float morphTargetInfluences[ 8 ];\n\t#else\n\t\tuniform float morphTargetInfluences[ 4 ];\n\t#endif\n#endif"; var morphtarget_vertex = "#ifdef USE_MORPHTARGETS\n\ttransformed *= morphTargetBaseInfluence;\n\ttransformed += morphTarget0 * morphTargetInfluences[ 0 ];\n\ttransformed += morphTarget1 * morphTargetInfluences[ 1 ];\n\ttransformed += morphTarget2 * morphTargetInfluences[ 2 ];\n\ttransformed += morphTarget3 * morphTargetInfluences[ 3 ];\n\t#ifndef USE_MORPHNORMALS\n\t\ttransformed += morphTarget4 * morphTargetInfluences[ 4 ];\n\t\ttransformed += morphTarget5 * morphTargetInfluences[ 5 ];\n\t\ttransformed += morphTarget6 * morphTargetInfluences[ 6 ];\n\t\ttransformed += morphTarget7 * morphTargetInfluences[ 7 ];\n\t#endif\n#endif"; var normal_fragment_begin = "#ifdef FLAT_SHADED\n\tvec3 fdx = vec3( dFdx( vViewPosition.x ), dFdx( vViewPosition.y ), dFdx( vViewPosition.z ) );\n\tvec3 fdy = vec3( dFdy( vViewPosition.x ), dFdy( vViewPosition.y ), dFdy( vViewPosition.z ) );\n\tvec3 normal = normalize( cross( fdx, fdy ) );\n#else\n\tvec3 normal = normalize( vNormal );\n\t#ifdef DOUBLE_SIDED\n\t\tnormal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t#endif\n\t#ifdef USE_TANGENT\n\t\tvec3 tangent = normalize( vTangent );\n\t\tvec3 bitangent = normalize( vBitangent );\n\t\t#ifdef DOUBLE_SIDED\n\t\t\ttangent = tangent * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\t\tbitangent = bitangent * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\t#endif\n\t\t#if defined( TANGENTSPACE_NORMALMAP ) || defined( USE_CLEARCOAT_NORMALMAP )\n\t\t\tmat3 vTBN = mat3( tangent, bitangent, normal );\n\t\t#endif\n\t#endif\n#endif\nvec3 geometryNormal = normal;"; var normal_fragment_maps = "#ifdef OBJECTSPACE_NORMALMAP\n\tnormal = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;\n\t#ifdef FLIP_SIDED\n\t\tnormal = - normal;\n\t#endif\n\t#ifdef DOUBLE_SIDED\n\t\tnormal = normal * ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t#endif\n\tnormal = normalize( normalMatrix * normal );\n#elif defined( TANGENTSPACE_NORMALMAP )\n\tvec3 mapN = texture2D( normalMap, vUv ).xyz * 2.0 - 1.0;\n\tmapN.xy *= normalScale;\n\t#ifdef USE_TANGENT\n\t\tnormal = normalize( vTBN * mapN );\n\t#else\n\t\tnormal = perturbNormal2Arb( -vViewPosition, normal, mapN );\n\t#endif\n#elif defined( USE_BUMPMAP )\n\tnormal = perturbNormalArb( -vViewPosition, normal, dHdxy_fwd() );\n#endif"; var normalmap_pars_fragment = "#ifdef USE_NORMALMAP\n\tuniform sampler2D normalMap;\n\tuniform vec2 normalScale;\n#endif\n#ifdef OBJECTSPACE_NORMALMAP\n\tuniform mat3 normalMatrix;\n#endif\n#if ! defined ( USE_TANGENT ) && ( defined ( TANGENTSPACE_NORMALMAP ) || defined ( USE_CLEARCOAT_NORMALMAP ) )\n\tvec3 perturbNormal2Arb( vec3 eye_pos, vec3 surf_norm, vec3 mapN ) {\n\t\tvec3 q0 = vec3( dFdx( eye_pos.x ), dFdx( eye_pos.y ), dFdx( eye_pos.z ) );\n\t\tvec3 q1 = vec3( dFdy( eye_pos.x ), dFdy( eye_pos.y ), dFdy( eye_pos.z ) );\n\t\tvec2 st0 = dFdx( vUv.st );\n\t\tvec2 st1 = dFdy( vUv.st );\n\t\tfloat scale = sign( st1.t * st0.s - st0.t * st1.s );\n\t\tvec3 S = normalize( ( q0 * st1.t - q1 * st0.t ) * scale );\n\t\tvec3 T = normalize( ( - q0 * st1.s + q1 * st0.s ) * scale );\n\t\tvec3 N = normalize( surf_norm );\n\t\tmat3 tsn = mat3( S, T, N );\n\t\tmapN.xy *= ( float( gl_FrontFacing ) * 2.0 - 1.0 );\n\t\treturn normalize( tsn * mapN );\n\t}\n#endif"; var clearcoat_normal_fragment_begin = "#ifdef CLEARCOAT\n\tvec3 clearcoatNormal = geometryNormal;\n#endif"; var clearcoat_normal_fragment_maps = "#ifdef USE_CLEARCOAT_NORMALMAP\n\tvec3 clearcoatMapN = texture2D( clearcoatNormalMap, vUv ).xyz * 2.0 - 1.0;\n\tclearcoatMapN.xy *= clearcoatNormalScale;\n\t#ifdef USE_TANGENT\n\t\tclearcoatNormal = normalize( vTBN * clearcoatMapN );\n\t#else\n\t\tclearcoatNormal = perturbNormal2Arb( - vViewPosition, clearcoatNormal, clearcoatMapN );\n\t#endif\n#endif"; var clearcoat_pars_fragment = "#ifdef USE_CLEARCOATMAP\n\tuniform sampler2D clearcoatMap;\n#endif\n#ifdef USE_CLEARCOAT_ROUGHNESSMAP\n\tuniform sampler2D clearcoatRoughnessMap;\n#endif\n#ifdef USE_CLEARCOAT_NORMALMAP\n\tuniform sampler2D clearcoatNormalMap;\n\tuniform vec2 clearcoatNormalScale;\n#endif"; var packing = "vec3 packNormalToRGB( const in vec3 normal ) {\n\treturn normalize( normal ) * 0.5 + 0.5;\n}\nvec3 unpackRGBToNormal( const in vec3 rgb ) {\n\treturn 2.0 * rgb.xyz - 1.0;\n}\nconst float PackUpscale = 256. / 255.;const float UnpackDownscale = 255. / 256.;\nconst vec3 PackFactors = vec3( 256. * 256. * 256., 256. * 256., 256. );\nconst vec4 UnpackFactors = UnpackDownscale / vec4( PackFactors, 1. );\nconst float ShiftRight8 = 1. / 256.;\nvec4 packDepthToRGBA( const in float v ) {\n\tvec4 r = vec4( fract( v * PackFactors ), v );\n\tr.yzw -= r.xyz * ShiftRight8;\treturn r * PackUpscale;\n}\nfloat unpackRGBAToDepth( const in vec4 v ) {\n\treturn dot( v, UnpackFactors );\n}\nvec4 pack2HalfToRGBA( vec2 v ) {\n\tvec4 r = vec4( v.x, fract( v.x * 255.0 ), v.y, fract( v.y * 255.0 ));\n\treturn vec4( r.x - r.y / 255.0, r.y, r.z - r.w / 255.0, r.w);\n}\nvec2 unpackRGBATo2Half( vec4 v ) {\n\treturn vec2( v.x + ( v.y / 255.0 ), v.z + ( v.w / 255.0 ) );\n}\nfloat viewZToOrthographicDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn ( viewZ + near ) / ( near - far );\n}\nfloat orthographicDepthToViewZ( const in float linearClipZ, const in float near, const in float far ) {\n\treturn linearClipZ * ( near - far ) - near;\n}\nfloat viewZToPerspectiveDepth( const in float viewZ, const in float near, const in float far ) {\n\treturn (( near + viewZ ) * far ) / (( far - near ) * viewZ );\n}\nfloat perspectiveDepthToViewZ( const in float invClipZ, const in float near, const in float far ) {\n\treturn ( near * far ) / ( ( far - near ) * invClipZ - far );\n}"; var premultiplied_alpha_fragment = "#ifdef PREMULTIPLIED_ALPHA\n\tgl_FragColor.rgb *= gl_FragColor.a;\n#endif"; var project_vertex = "vec4 mvPosition = vec4( transformed, 1.0 );\n#ifdef USE_INSTANCING\n\tmvPosition = instanceMatrix * mvPosition;\n#endif\nmvPosition = modelViewMatrix * mvPosition;\ngl_Position = projectionMatrix * mvPosition;"; var dithering_fragment = "#ifdef DITHERING\n\tgl_FragColor.rgb = dithering( gl_FragColor.rgb );\n#endif"; var dithering_pars_fragment = "#ifdef DITHERING\n\tvec3 dithering( vec3 color ) {\n\t\tfloat grid_position = rand( gl_FragCoord.xy );\n\t\tvec3 dither_shift_RGB = vec3( 0.25 / 255.0, -0.25 / 255.0, 0.25 / 255.0 );\n\t\tdither_shift_RGB = mix( 2.0 * dither_shift_RGB, -2.0 * dither_shift_RGB, grid_position );\n\t\treturn color + dither_shift_RGB;\n\t}\n#endif"; var roughnessmap_fragment = "float roughnessFactor = roughness;\n#ifdef USE_ROUGHNESSMAP\n\tvec4 texelRoughness = texture2D( roughnessMap, vUv );\n\troughnessFactor *= texelRoughness.g;\n#endif"; var roughnessmap_pars_fragment = "#ifdef USE_ROUGHNESSMAP\n\tuniform sampler2D roughnessMap;\n#endif"; var shadowmap_pars_fragment = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D directionalShadowMap[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tstruct DirectionalLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D spotShadowMap[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tstruct SpotLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform sampler2D pointShadowMap[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tstruct PointLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t\tfloat shadowCameraNear;\n\t\t\tfloat shadowCameraFar;\n\t\t};\n\t\tuniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n\tfloat texture2DCompare( sampler2D depths, vec2 uv, float compare ) {\n\t\treturn step( compare, unpackRGBAToDepth( texture2D( depths, uv ) ) );\n\t}\n\tvec2 texture2DDistribution( sampler2D shadow, vec2 uv ) {\n\t\treturn unpackRGBATo2Half( texture2D( shadow, uv ) );\n\t}\n\tfloat VSMShadow (sampler2D shadow, vec2 uv, float compare ){\n\t\tfloat occlusion = 1.0;\n\t\tvec2 distribution = texture2DDistribution( shadow, uv );\n\t\tfloat hard_shadow = step( compare , distribution.x );\n\t\tif (hard_shadow != 1.0 ) {\n\t\t\tfloat distance = compare - distribution.x ;\n\t\t\tfloat variance = max( 0.00000, distribution.y * distribution.y );\n\t\t\tfloat softness_probability = variance / (variance + distance * distance );\t\t\tsoftness_probability = clamp( ( softness_probability - 0.3 ) / ( 0.95 - 0.3 ), 0.0, 1.0 );\t\t\tocclusion = clamp( max( hard_shadow, softness_probability ), 0.0, 1.0 );\n\t\t}\n\t\treturn occlusion;\n\t}\n\tfloat getShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord ) {\n\t\tfloat shadow = 1.0;\n\t\tshadowCoord.xyz /= shadowCoord.w;\n\t\tshadowCoord.z += shadowBias;\n\t\tbvec4 inFrustumVec = bvec4 ( shadowCoord.x >= 0.0, shadowCoord.x <= 1.0, shadowCoord.y >= 0.0, shadowCoord.y <= 1.0 );\n\t\tbool inFrustum = all( inFrustumVec );\n\t\tbvec2 frustumTestVec = bvec2( inFrustum, shadowCoord.z <= 1.0 );\n\t\tbool frustumTest = all( frustumTestVec );\n\t\tif ( frustumTest ) {\n\t\t#if defined( SHADOWMAP_TYPE_PCF )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx0 = - texelSize.x * shadowRadius;\n\t\t\tfloat dy0 = - texelSize.y * shadowRadius;\n\t\t\tfloat dx1 = + texelSize.x * shadowRadius;\n\t\t\tfloat dy1 = + texelSize.y * shadowRadius;\n\t\t\tfloat dx2 = dx0 / 2.0;\n\t\t\tfloat dy2 = dy0 / 2.0;\n\t\t\tfloat dx3 = dx1 / 2.0;\n\t\t\tfloat dy3 = dy1 / 2.0;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy2 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx2, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx3, dy3 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( 0.0, dy1 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, shadowCoord.xy + vec2( dx1, dy1 ), shadowCoord.z )\n\t\t\t) * ( 1.0 / 17.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_PCF_SOFT )\n\t\t\tvec2 texelSize = vec2( 1.0 ) / shadowMapSize;\n\t\t\tfloat dx = texelSize.x;\n\t\t\tfloat dy = texelSize.y;\n\t\t\tvec2 uv = shadowCoord.xy;\n\t\t\tvec2 f = fract( uv * shadowMapSize + 0.5 );\n\t\t\tuv -= f * texelSize;\n\t\t\tshadow = (\n\t\t\t\ttexture2DCompare( shadowMap, uv, shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( dx, 0.0 ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + vec2( 0.0, dy ), shadowCoord.z ) +\n\t\t\t\ttexture2DCompare( shadowMap, uv + texelSize, shadowCoord.z ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, 0.0 ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 0.0 ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( -dx, dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, dy ), shadowCoord.z ),\n\t\t\t\t\t f.x ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( 0.0, -dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 0.0, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( texture2DCompare( shadowMap, uv + vec2( dx, -dy ), shadowCoord.z ), \n\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t f.y ) +\n\t\t\t\tmix( mix( texture2DCompare( shadowMap, uv + vec2( -dx, -dy ), shadowCoord.z ), \n\t\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, -dy ), shadowCoord.z ),\n\t\t\t\t\t\t f.x ),\n\t\t\t\t\t mix( texture2DCompare( shadowMap, uv + vec2( -dx, 2.0 * dy ), shadowCoord.z ), \n\t\t\t\t\t\t texture2DCompare( shadowMap, uv + vec2( 2.0 * dx, 2.0 * dy ), shadowCoord.z ),\n\t\t\t\t\t\t f.x ),\n\t\t\t\t\t f.y )\n\t\t\t) * ( 1.0 / 9.0 );\n\t\t#elif defined( SHADOWMAP_TYPE_VSM )\n\t\t\tshadow = VSMShadow( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#else\n\t\t\tshadow = texture2DCompare( shadowMap, shadowCoord.xy, shadowCoord.z );\n\t\t#endif\n\t\t}\n\t\treturn shadow;\n\t}\n\tvec2 cubeToUV( vec3 v, float texelSizeY ) {\n\t\tvec3 absV = abs( v );\n\t\tfloat scaleToCube = 1.0 / max( absV.x, max( absV.y, absV.z ) );\n\t\tabsV *= scaleToCube;\n\t\tv *= scaleToCube * ( 1.0 - 2.0 * texelSizeY );\n\t\tvec2 planar = v.xy;\n\t\tfloat almostATexel = 1.5 * texelSizeY;\n\t\tfloat almostOne = 1.0 - almostATexel;\n\t\tif ( absV.z >= almostOne ) {\n\t\t\tif ( v.z > 0.0 )\n\t\t\t\tplanar.x = 4.0 - v.x;\n\t\t} else if ( absV.x >= almostOne ) {\n\t\t\tfloat signX = sign( v.x );\n\t\t\tplanar.x = v.z * signX + 2.0 * signX;\n\t\t} else if ( absV.y >= almostOne ) {\n\t\t\tfloat signY = sign( v.y );\n\t\t\tplanar.x = v.x + 2.0 * signY + 2.0;\n\t\t\tplanar.y = v.z * signY - 2.0;\n\t\t}\n\t\treturn vec2( 0.125, 0.25 ) * planar + vec2( 0.375, 0.75 );\n\t}\n\tfloat getPointShadow( sampler2D shadowMap, vec2 shadowMapSize, float shadowBias, float shadowRadius, vec4 shadowCoord, float shadowCameraNear, float shadowCameraFar ) {\n\t\tvec2 texelSize = vec2( 1.0 ) / ( shadowMapSize * vec2( 4.0, 2.0 ) );\n\t\tvec3 lightToPosition = shadowCoord.xyz;\n\t\tfloat dp = ( length( lightToPosition ) - shadowCameraNear ) / ( shadowCameraFar - shadowCameraNear );\t\tdp += shadowBias;\n\t\tvec3 bd3D = normalize( lightToPosition );\n\t\t#if defined( SHADOWMAP_TYPE_PCF ) || defined( SHADOWMAP_TYPE_PCF_SOFT ) || defined( SHADOWMAP_TYPE_VSM )\n\t\t\tvec2 offset = vec2( - 1, 1 ) * shadowRadius * texelSize.y;\n\t\t\treturn (\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xyx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yyx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxy, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.xxx, texelSize.y ), dp ) +\n\t\t\t\ttexture2DCompare( shadowMap, cubeToUV( bd3D + offset.yxx, texelSize.y ), dp )\n\t\t\t) * ( 1.0 / 9.0 );\n\t\t#else\n\t\t\treturn texture2DCompare( shadowMap, cubeToUV( bd3D, texelSize.y ), dp );\n\t\t#endif\n\t}\n#endif"; var shadowmap_pars_vertex = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t\tuniform mat4 directionalShadowMatrix[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tvarying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHT_SHADOWS ];\n\t\tstruct DirectionalLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform DirectionalLightShadow directionalLightShadows[ NUM_DIR_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t\tuniform mat4 spotShadowMatrix[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHT_SHADOWS ];\n\t\tstruct SpotLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t};\n\t\tuniform SpotLightShadow spotLightShadows[ NUM_SPOT_LIGHT_SHADOWS ];\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t\tuniform mat4 pointShadowMatrix[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tvarying vec4 vPointShadowCoord[ NUM_POINT_LIGHT_SHADOWS ];\n\t\tstruct PointLightShadow {\n\t\t\tfloat shadowBias;\n\t\t\tfloat shadowNormalBias;\n\t\t\tfloat shadowRadius;\n\t\t\tvec2 shadowMapSize;\n\t\t\tfloat shadowCameraNear;\n\t\t\tfloat shadowCameraFar;\n\t\t};\n\t\tuniform PointLightShadow pointLightShadows[ NUM_POINT_LIGHT_SHADOWS ];\n\t#endif\n#endif"; var shadowmap_vertex = "#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0 || NUM_SPOT_LIGHT_SHADOWS > 0 || NUM_POINT_LIGHT_SHADOWS > 0\n\t\tvec3 shadowWorldNormal = inverseTransformDirection( transformedNormal, viewMatrix );\n\t\tvec4 shadowWorldPosition;\n\t#endif\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\tshadowWorldPosition = worldPosition + vec4( shadowWorldNormal * directionalLightShadows[ i ].shadowNormalBias, 0 );\n\t\tvDirectionalShadowCoord[ i ] = directionalShadowMatrix[ i ] * shadowWorldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) {\n\t\tshadowWorldPosition = worldPosition + vec4( shadowWorldNormal * spotLightShadows[ i ].shadowNormalBias, 0 );\n\t\tvSpotShadowCoord[ i ] = spotShadowMatrix[ i ] * shadowWorldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\tshadowWorldPosition = worldPosition + vec4( shadowWorldNormal * pointLightShadows[ i ].shadowNormalBias, 0 );\n\t\tvPointShadowCoord[ i ] = pointShadowMatrix[ i ] * shadowWorldPosition;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n#endif"; var shadowmask_pars_fragment = "float getShadowMask() {\n\tfloat shadow = 1.0;\n\t#ifdef USE_SHADOWMAP\n\t#if NUM_DIR_LIGHT_SHADOWS > 0\n\tDirectionalLightShadow directionalLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_DIR_LIGHT_SHADOWS; i ++ ) {\n\t\tdirectionalLight = directionalLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( directionalShadowMap[ i ], directionalLight.shadowMapSize, directionalLight.shadowBias, directionalLight.shadowRadius, vDirectionalShadowCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_SPOT_LIGHT_SHADOWS > 0\n\tSpotLightShadow spotLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_SPOT_LIGHT_SHADOWS; i ++ ) {\n\t\tspotLight = spotLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getShadow( spotShadowMap[ i ], spotLight.shadowMapSize, spotLight.shadowBias, spotLight.shadowRadius, vSpotShadowCoord[ i ] ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#if NUM_POINT_LIGHT_SHADOWS > 0\n\tPointLightShadow pointLight;\n\t#pragma unroll_loop_start\n\tfor ( int i = 0; i < NUM_POINT_LIGHT_SHADOWS; i ++ ) {\n\t\tpointLight = pointLightShadows[ i ];\n\t\tshadow *= receiveShadow ? getPointShadow( pointShadowMap[ i ], pointLight.shadowMapSize, pointLight.shadowBias, pointLight.shadowRadius, vPointShadowCoord[ i ], pointLight.shadowCameraNear, pointLight.shadowCameraFar ) : 1.0;\n\t}\n\t#pragma unroll_loop_end\n\t#endif\n\t#endif\n\treturn shadow;\n}"; var skinbase_vertex = "#ifdef USE_SKINNING\n\tmat4 boneMatX = getBoneMatrix( skinIndex.x );\n\tmat4 boneMatY = getBoneMatrix( skinIndex.y );\n\tmat4 boneMatZ = getBoneMatrix( skinIndex.z );\n\tmat4 boneMatW = getBoneMatrix( skinIndex.w );\n#endif"; var skinning_pars_vertex = "#ifdef USE_SKINNING\n\tuniform mat4 bindMatrix;\n\tuniform mat4 bindMatrixInverse;\n\t#ifdef BONE_TEXTURE\n\t\tuniform highp sampler2D boneTexture;\n\t\tuniform int boneTextureSize;\n\t\tmat4 getBoneMatrix( const in float i ) {\n\t\t\tfloat j = i * 4.0;\n\t\t\tfloat x = mod( j, float( boneTextureSize ) );\n\t\t\tfloat y = floor( j / float( boneTextureSize ) );\n\t\t\tfloat dx = 1.0 / float( boneTextureSize );\n\t\t\tfloat dy = 1.0 / float( boneTextureSize );\n\t\t\ty = dy * ( y + 0.5 );\n\t\t\tvec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );\n\t\t\tvec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );\n\t\t\tvec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );\n\t\t\tvec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );\n\t\t\tmat4 bone = mat4( v1, v2, v3, v4 );\n\t\t\treturn bone;\n\t\t}\n\t#else\n\t\tuniform mat4 boneMatrices[ MAX_BONES ];\n\t\tmat4 getBoneMatrix( const in float i ) {\n\t\t\tmat4 bone = boneMatrices[ int(i) ];\n\t\t\treturn bone;\n\t\t}\n\t#endif\n#endif"; var skinning_vertex = "#ifdef USE_SKINNING\n\tvec4 skinVertex = bindMatrix * vec4( transformed, 1.0 );\n\tvec4 skinned = vec4( 0.0 );\n\tskinned += boneMatX * skinVertex * skinWeight.x;\n\tskinned += boneMatY * skinVertex * skinWeight.y;\n\tskinned += boneMatZ * skinVertex * skinWeight.z;\n\tskinned += boneMatW * skinVertex * skinWeight.w;\n\ttransformed = ( bindMatrixInverse * skinned ).xyz;\n#endif"; var skinnormal_vertex = "#ifdef USE_SKINNING\n\tmat4 skinMatrix = mat4( 0.0 );\n\tskinMatrix += skinWeight.x * boneMatX;\n\tskinMatrix += skinWeight.y * boneMatY;\n\tskinMatrix += skinWeight.z * boneMatZ;\n\tskinMatrix += skinWeight.w * boneMatW;\n\tskinMatrix = bindMatrixInverse * skinMatrix * bindMatrix;\n\tobjectNormal = vec4( skinMatrix * vec4( objectNormal, 0.0 ) ).xyz;\n\t#ifdef USE_TANGENT\n\t\tobjectTangent = vec4( skinMatrix * vec4( objectTangent, 0.0 ) ).xyz;\n\t#endif\n#endif"; var specularmap_fragment = "float specularStrength;\n#ifdef USE_SPECULARMAP\n\tvec4 texelSpecular = texture2D( specularMap, vUv );\n\tspecularStrength = texelSpecular.r;\n#else\n\tspecularStrength = 1.0;\n#endif"; var specularmap_pars_fragment = "#ifdef USE_SPECULARMAP\n\tuniform sampler2D specularMap;\n#endif"; var tonemapping_fragment = "#if defined( TONE_MAPPING )\n\tgl_FragColor.rgb = toneMapping( gl_FragColor.rgb );\n#endif"; var tonemapping_pars_fragment = "#ifndef saturate\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#endif\nuniform float toneMappingExposure;\nvec3 LinearToneMapping( vec3 color ) {\n\treturn toneMappingExposure * color;\n}\nvec3 ReinhardToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\treturn saturate( color / ( vec3( 1.0 ) + color ) );\n}\nvec3 OptimizedCineonToneMapping( vec3 color ) {\n\tcolor *= toneMappingExposure;\n\tcolor = max( vec3( 0.0 ), color - 0.004 );\n\treturn pow( ( color * ( 6.2 * color + 0.5 ) ) / ( color * ( 6.2 * color + 1.7 ) + 0.06 ), vec3( 2.2 ) );\n}\nvec3 RRTAndODTFit( vec3 v ) {\n\tvec3 a = v * ( v + 0.0245786 ) - 0.000090537;\n\tvec3 b = v * ( 0.983729 * v + 0.4329510 ) + 0.238081;\n\treturn a / b;\n}\nvec3 ACESFilmicToneMapping( vec3 color ) {\n\tconst mat3 ACESInputMat = mat3(\n\t\tvec3( 0.59719, 0.07600, 0.02840 ),\t\tvec3( 0.35458, 0.90834, 0.13383 ),\n\t\tvec3( 0.04823, 0.01566, 0.83777 )\n\t);\n\tconst mat3 ACESOutputMat = mat3(\n\t\tvec3( 1.60475, -0.10208, -0.00327 ),\t\tvec3( -0.53108, 1.10813, -0.07276 ),\n\t\tvec3( -0.07367, -0.00605, 1.07602 )\n\t);\n\tcolor *= toneMappingExposure / 0.6;\n\tcolor = ACESInputMat * color;\n\tcolor = RRTAndODTFit( color );\n\tcolor = ACESOutputMat * color;\n\treturn saturate( color );\n}\nvec3 CustomToneMapping( vec3 color ) { return color; }"; var transmissionmap_fragment = "#ifdef USE_TRANSMISSIONMAP\n\ttotalTransmission *= texture2D( transmissionMap, vUv ).r;\n#endif"; var transmissionmap_pars_fragment = "#ifdef USE_TRANSMISSIONMAP\n\tuniform sampler2D transmissionMap;\n#endif"; var uv_pars_fragment = "#if ( defined( USE_UV ) && ! defined( UVS_VERTEX_ONLY ) )\n\tvarying vec2 vUv;\n#endif"; var uv_pars_vertex = "#ifdef USE_UV\n\t#ifdef UVS_VERTEX_ONLY\n\t\tvec2 vUv;\n\t#else\n\t\tvarying vec2 vUv;\n\t#endif\n\tuniform mat3 uvTransform;\n#endif"; var uv_vertex = "#ifdef USE_UV\n\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n#endif"; var uv2_pars_fragment = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tvarying vec2 vUv2;\n#endif"; var uv2_pars_vertex = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tattribute vec2 uv2;\n\tvarying vec2 vUv2;\n\tuniform mat3 uv2Transform;\n#endif"; var uv2_vertex = "#if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )\n\tvUv2 = ( uv2Transform * vec3( uv2, 1 ) ).xy;\n#endif"; var worldpos_vertex = "#if defined( USE_ENVMAP ) || defined( DISTANCE ) || defined ( USE_SHADOWMAP )\n\tvec4 worldPosition = vec4( transformed, 1.0 );\n\t#ifdef USE_INSTANCING\n\t\tworldPosition = instanceMatrix * worldPosition;\n\t#endif\n\tworldPosition = modelMatrix * worldPosition;\n#endif"; var background_frag = "uniform sampler2D t2D;\nvarying vec2 vUv;\nvoid main() {\n\tvec4 texColor = texture2D( t2D, vUv );\n\tgl_FragColor = mapTexelToLinear( texColor );\n\t#include \n\t#include \n}"; var background_vert = "varying vec2 vUv;\nuniform mat3 uvTransform;\nvoid main() {\n\tvUv = ( uvTransform * vec3( uv, 1 ) ).xy;\n\tgl_Position = vec4( position.xy, 1.0, 1.0 );\n}"; var cube_frag = "#include \nuniform float opacity;\nvarying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvec3 vReflect = vWorldDirection;\n\t#include \n\tgl_FragColor = envColor;\n\tgl_FragColor.a *= opacity;\n\t#include \n\t#include \n}"; var cube_vert = "varying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include \n\t#include \n\tgl_Position.z = gl_Position.w;\n}"; var depth_frag = "#if DEPTH_PACKING == 3200\n\tuniform float opacity;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\t#include \n\tvec4 diffuseColor = vec4( 1.0 );\n\t#if DEPTH_PACKING == 3200\n\t\tdiffuseColor.a = opacity;\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\tfloat fragCoordZ = 0.5 * vHighPrecisionZW[0] / vHighPrecisionZW[1] + 0.5;\n\t#if DEPTH_PACKING == 3200\n\t\tgl_FragColor = vec4( vec3( 1.0 - fragCoordZ ), opacity );\n\t#elif DEPTH_PACKING == 3201\n\t\tgl_FragColor = packDepthToRGBA( fragCoordZ );\n\t#endif\n}"; var depth_vert = "#include \n#include \n#include \n#include \n#include \n#include \n#include \nvarying vec2 vHighPrecisionZW;\nvoid main() {\n\t#include \n\t#include \n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include \n\t\t#include \n\t\t#include \n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvHighPrecisionZW = gl_Position.zw;\n}"; var distanceRGBA_frag = "#define DISTANCE\nuniform vec3 referencePosition;\nuniform float nearDistance;\nuniform float farDistance;\nvarying vec3 vWorldPosition;\n#include \n#include \n#include \n#include \n#include \n#include \nvoid main () {\n\t#include \n\tvec4 diffuseColor = vec4( 1.0 );\n\t#include \n\t#include \n\t#include \n\tfloat dist = length( vWorldPosition - referencePosition );\n\tdist = ( dist - nearDistance ) / ( farDistance - nearDistance );\n\tdist = saturate( dist );\n\tgl_FragColor = packDepthToRGBA( dist );\n}"; var distanceRGBA_vert = "#define DISTANCE\nvarying vec3 vWorldPosition;\n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#ifdef USE_DISPLACEMENTMAP\n\t\t#include \n\t\t#include \n\t\t#include \n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvWorldPosition = worldPosition.xyz;\n}"; var equirect_frag = "uniform sampler2D tEquirect;\nvarying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvec3 direction = normalize( vWorldDirection );\n\tvec2 sampleUV = equirectUv( direction );\n\tvec4 texColor = texture2D( tEquirect, sampleUV );\n\tgl_FragColor = mapTexelToLinear( texColor );\n\t#include \n\t#include \n}"; var equirect_vert = "varying vec3 vWorldDirection;\n#include \nvoid main() {\n\tvWorldDirection = transformDirection( position, modelMatrix );\n\t#include \n\t#include \n}"; var linedashed_frag = "uniform vec3 diffuse;\nuniform float opacity;\nuniform float dashSize;\nuniform float totalSize;\nvarying float vLineDistance;\n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tif ( mod( vLineDistance, totalSize ) > dashSize ) {\n\t\tdiscard;\n\t}\n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\t#include \n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n}"; var linedashed_vert = "uniform float scale;\nattribute float lineDistance;\nvarying float vLineDistance;\n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tvLineDistance = scale * lineDistance;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshbasic_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\t#ifdef USE_LIGHTMAP\n\t\n\t\tvec4 lightMapTexel= texture2D( lightMap, vUv2 );\n\t\treflectedLight.indirectDiffuse += lightMapTexelToLinear( lightMapTexel ).rgb * lightMapIntensity;\n\t#else\n\t\treflectedLight.indirectDiffuse += vec3( 1.0 );\n\t#endif\n\t#include \n\treflectedLight.indirectDiffuse *= diffuseColor.rgb;\n\tvec3 outgoingLight = reflectedLight.indirectDiffuse;\n\t#include \n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshbasic_vert = "#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#ifdef USE_ENVMAP\n\t#include \n\t#include \n\t#include \n\t#include \n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshlambert_frag = "uniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float opacity;\nvarying vec3 vLightFront;\nvarying vec3 vIndirectFront;\n#ifdef DOUBLE_SIDED\n\tvarying vec3 vLightBack;\n\tvarying vec3 vIndirectBack;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#ifdef DOUBLE_SIDED\n\t\treflectedLight.indirectDiffuse += ( gl_FrontFacing ) ? vIndirectFront : vIndirectBack;\n\t#else\n\t\treflectedLight.indirectDiffuse += vIndirectFront;\n\t#endif\n\t#include \n\treflectedLight.indirectDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb );\n\t#ifdef DOUBLE_SIDED\n\t\treflectedLight.directDiffuse = ( gl_FrontFacing ) ? vLightFront : vLightBack;\n\t#else\n\t\treflectedLight.directDiffuse = vLightFront;\n\t#endif\n\treflectedLight.directDiffuse *= BRDF_Diffuse_Lambert( diffuseColor.rgb ) * getShadowMask();\n\t#include \n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;\n\t#include \n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshlambert_vert = "#define LAMBERT\nvarying vec3 vLightFront;\nvarying vec3 vIndirectFront;\n#ifdef DOUBLE_SIDED\n\tvarying vec3 vLightBack;\n\tvarying vec3 vIndirectBack;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshmatcap_frag = "#define MATCAP\nuniform vec3 diffuse;\nuniform float opacity;\nuniform sampler2D matcap;\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 viewDir = normalize( vViewPosition );\n\tvec3 x = normalize( vec3( viewDir.z, 0.0, - viewDir.x ) );\n\tvec3 y = cross( viewDir, x );\n\tvec2 uv = vec2( dot( x, normal ), dot( y, normal ) ) * 0.495 + 0.5;\n\t#ifdef USE_MATCAP\n\t\tvec4 matcapColor = texture2D( matcap, uv );\n\t\tmatcapColor = matcapTexelToLinear( matcapColor );\n\t#else\n\t\tvec4 matcapColor = vec4( 1.0 );\n\t#endif\n\tvec3 outgoingLight = diffuseColor.rgb * matcapColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshmatcap_vert = "#define MATCAP\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#ifndef FLAT_SHADED\n\t\tvNormal = normalize( transformedNormal );\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n}"; var meshtoon_frag = "#define TOON\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + totalEmissiveRadiance;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshtoon_vert = "#define TOON\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n\t#include \n\t#include \n\t#include \n}"; var meshphong_frag = "#define PHONG\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform vec3 specular;\nuniform float shininess;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\t#include \n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshphong_vert = "#define PHONG\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshphysical_frag = "#define STANDARD\n#ifdef PHYSICAL\n\t#define REFLECTIVITY\n\t#define CLEARCOAT\n\t#define TRANSMISSION\n#endif\nuniform vec3 diffuse;\nuniform vec3 emissive;\nuniform float roughness;\nuniform float metalness;\nuniform float opacity;\n#ifdef TRANSMISSION\n\tuniform float transmission;\n#endif\n#ifdef REFLECTIVITY\n\tuniform float reflectivity;\n#endif\n#ifdef CLEARCOAT\n\tuniform float clearcoat;\n\tuniform float clearcoatRoughness;\n#endif\n#ifdef USE_SHEEN\n\tuniform vec3 sheen;\n#endif\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\tReflectedLight reflectedLight = ReflectedLight( vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ), vec3( 0.0 ) );\n\tvec3 totalEmissiveRadiance = emissive;\n\t#ifdef TRANSMISSION\n\t\tfloat totalTransmission = transmission;\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvec3 outgoingLight = reflectedLight.directDiffuse + reflectedLight.indirectDiffuse + reflectedLight.directSpecular + reflectedLight.indirectSpecular + totalEmissiveRadiance;\n\t#ifdef TRANSMISSION\n\t\tdiffuseColor.a *= mix( saturate( 1. - totalTransmission + linearToRelativeLuminance( reflectedLight.directSpecular + reflectedLight.indirectSpecular ) ), 1.0, metalness );\n\t#endif\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var meshphysical_vert = "#define STANDARD\nvarying vec3 vViewPosition;\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n\t#ifdef USE_TANGENT\n\t\tvTangent = normalize( transformedTangent );\n\t\tvBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );\n\t#endif\n#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\tvViewPosition = - mvPosition.xyz;\n\t#include \n\t#include \n\t#include \n}"; var normal_frag = "#define NORMAL\nuniform float opacity;\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvarying vec3 vViewPosition;\n#endif\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\tgl_FragColor = vec4( packNormalToRGB( normal ), opacity );\n}"; var normal_vert = "#define NORMAL\n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvarying vec3 vViewPosition;\n#endif\n#ifndef FLAT_SHADED\n\tvarying vec3 vNormal;\n\t#ifdef USE_TANGENT\n\t\tvarying vec3 vTangent;\n\t\tvarying vec3 vBitangent;\n\t#endif\n#endif\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n#ifndef FLAT_SHADED\n\tvNormal = normalize( transformedNormal );\n\t#ifdef USE_TANGENT\n\t\tvTangent = normalize( transformedTangent );\n\t\tvBitangent = normalize( cross( vNormal, vTangent ) * tangent.w );\n\t#endif\n#endif\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n#if defined( FLAT_SHADED ) || defined( USE_BUMPMAP ) || defined( TANGENTSPACE_NORMALMAP )\n\tvViewPosition = - mvPosition.xyz;\n#endif\n}"; var points_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\t#include \n\t#include \n\t#include \n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n\t#include \n}"; var points_vert = "uniform float size;\nuniform float scale;\n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\tgl_PointSize = size;\n\t#ifdef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) gl_PointSize *= ( scale / - mvPosition.z );\n\t#endif\n\t#include \n\t#include \n\t#include \n\t#include \n}"; var shadow_frag = "uniform vec3 color;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\tgl_FragColor = vec4( color, opacity * ( 1.0 - getShadowMask() ) );\n\t#include \n\t#include \n\t#include \n}"; var shadow_vert = "#include \n#include \n#include \nvoid main() {\n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n\t#include \n}"; var sprite_frag = "uniform vec3 diffuse;\nuniform float opacity;\n#include \n#include \n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec3 outgoingLight = vec3( 0.0 );\n\tvec4 diffuseColor = vec4( diffuse, opacity );\n\t#include \n\t#include \n\t#include \n\t#include \n\toutgoingLight = diffuseColor.rgb;\n\tgl_FragColor = vec4( outgoingLight, diffuseColor.a );\n\t#include \n\t#include \n\t#include \n}"; var sprite_vert = "uniform float rotation;\nuniform vec2 center;\n#include \n#include \n#include \n#include \n#include \nvoid main() {\n\t#include \n\tvec4 mvPosition = modelViewMatrix * vec4( 0.0, 0.0, 0.0, 1.0 );\n\tvec2 scale;\n\tscale.x = length( vec3( modelMatrix[ 0 ].x, modelMatrix[ 0 ].y, modelMatrix[ 0 ].z ) );\n\tscale.y = length( vec3( modelMatrix[ 1 ].x, modelMatrix[ 1 ].y, modelMatrix[ 1 ].z ) );\n\t#ifndef USE_SIZEATTENUATION\n\t\tbool isPerspective = isPerspectiveMatrix( projectionMatrix );\n\t\tif ( isPerspective ) scale *= - mvPosition.z;\n\t#endif\n\tvec2 alignedPosition = ( position.xy - ( center - vec2( 0.5 ) ) ) * scale;\n\tvec2 rotatedPosition;\n\trotatedPosition.x = cos( rotation ) * alignedPosition.x - sin( rotation ) * alignedPosition.y;\n\trotatedPosition.y = sin( rotation ) * alignedPosition.x + cos( rotation ) * alignedPosition.y;\n\tmvPosition.xy += rotatedPosition;\n\tgl_Position = projectionMatrix * mvPosition;\n\t#include \n\t#include \n\t#include \n}"; const ShaderChunk = { alphamap_fragment: alphamap_fragment, alphamap_pars_fragment: alphamap_pars_fragment, alphatest_fragment: alphatest_fragment, aomap_fragment: aomap_fragment, aomap_pars_fragment: aomap_pars_fragment, begin_vertex: begin_vertex, beginnormal_vertex: beginnormal_vertex, bsdfs: bsdfs, bumpmap_pars_fragment: bumpmap_pars_fragment, clipping_planes_fragment: clipping_planes_fragment, clipping_planes_pars_fragment: clipping_planes_pars_fragment, clipping_planes_pars_vertex: clipping_planes_pars_vertex, clipping_planes_vertex: clipping_planes_vertex, color_fragment: color_fragment, color_pars_fragment: color_pars_fragment, color_pars_vertex: color_pars_vertex, color_vertex: color_vertex, common: common, cube_uv_reflection_fragment: cube_uv_reflection_fragment, defaultnormal_vertex: defaultnormal_vertex, displacementmap_pars_vertex: displacementmap_pars_vertex, displacementmap_vertex: displacementmap_vertex, emissivemap_fragment: emissivemap_fragment, emissivemap_pars_fragment: emissivemap_pars_fragment, encodings_fragment: encodings_fragment, encodings_pars_fragment: encodings_pars_fragment, envmap_fragment: envmap_fragment, envmap_common_pars_fragment: envmap_common_pars_fragment, envmap_pars_fragment: envmap_pars_fragment, envmap_pars_vertex: envmap_pars_vertex, envmap_physical_pars_fragment: envmap_physical_pars_fragment, envmap_vertex: envmap_vertex, fog_vertex: fog_vertex, fog_pars_vertex: fog_pars_vertex, fog_fragment: fog_fragment, fog_pars_fragment: fog_pars_fragment, gradientmap_pars_fragment: gradientmap_pars_fragment, lightmap_fragment: lightmap_fragment, lightmap_pars_fragment: lightmap_pars_fragment, lights_lambert_vertex: lights_lambert_vertex, lights_pars_begin: lights_pars_begin, lights_toon_fragment: lights_toon_fragment, lights_toon_pars_fragment: lights_toon_pars_fragment, lights_phong_fragment: lights_phong_fragment, lights_phong_pars_fragment: lights_phong_pars_fragment, lights_physical_fragment: lights_physical_fragment, lights_physical_pars_fragment: lights_physical_pars_fragment, lights_fragment_begin: lights_fragment_begin, lights_fragment_maps: lights_fragment_maps, lights_fragment_end: lights_fragment_end, logdepthbuf_fragment: logdepthbuf_fragment, logdepthbuf_pars_fragment: logdepthbuf_pars_fragment, logdepthbuf_pars_vertex: logdepthbuf_pars_vertex, logdepthbuf_vertex: logdepthbuf_vertex, map_fragment: map_fragment, map_pars_fragment: map_pars_fragment, map_particle_fragment: map_particle_fragment, map_particle_pars_fragment: map_particle_pars_fragment, metalnessmap_fragment: metalnessmap_fragment, metalnessmap_pars_fragment: metalnessmap_pars_fragment, morphnormal_vertex: morphnormal_vertex, morphtarget_pars_vertex: morphtarget_pars_vertex, morphtarget_vertex: morphtarget_vertex, normal_fragment_begin: normal_fragment_begin, normal_fragment_maps: normal_fragment_maps, normalmap_pars_fragment: normalmap_pars_fragment, clearcoat_normal_fragment_begin: clearcoat_normal_fragment_begin, clearcoat_normal_fragment_maps: clearcoat_normal_fragment_maps, clearcoat_pars_fragment: clearcoat_pars_fragment, packing: packing, premultiplied_alpha_fragment: premultiplied_alpha_fragment, project_vertex: project_vertex, dithering_fragment: dithering_fragment, dithering_pars_fragment: dithering_pars_fragment, roughnessmap_fragment: roughnessmap_fragment, roughnessmap_pars_fragment: roughnessmap_pars_fragment, shadowmap_pars_fragment: shadowmap_pars_fragment, shadowmap_pars_vertex: shadowmap_pars_vertex, shadowmap_vertex: shadowmap_vertex, shadowmask_pars_fragment: shadowmask_pars_fragment, skinbase_vertex: skinbase_vertex, skinning_pars_vertex: skinning_pars_vertex, skinning_vertex: skinning_vertex, skinnormal_vertex: skinnormal_vertex, specularmap_fragment: specularmap_fragment, specularmap_pars_fragment: specularmap_pars_fragment, tonemapping_fragment: tonemapping_fragment, tonemapping_pars_fragment: tonemapping_pars_fragment, transmissionmap_fragment: transmissionmap_fragment, transmissionmap_pars_fragment: transmissionmap_pars_fragment, uv_pars_fragment: uv_pars_fragment, uv_pars_vertex: uv_pars_vertex, uv_vertex: uv_vertex, uv2_pars_fragment: uv2_pars_fragment, uv2_pars_vertex: uv2_pars_vertex, uv2_vertex: uv2_vertex, worldpos_vertex: worldpos_vertex, background_frag: background_frag, background_vert: background_vert, cube_frag: cube_frag, cube_vert: cube_vert, depth_frag: depth_frag, depth_vert: depth_vert, distanceRGBA_frag: distanceRGBA_frag, distanceRGBA_vert: distanceRGBA_vert, equirect_frag: equirect_frag, equirect_vert: equirect_vert, linedashed_frag: linedashed_frag, linedashed_vert: linedashed_vert, meshbasic_frag: meshbasic_frag, meshbasic_vert: meshbasic_vert, meshlambert_frag: meshlambert_frag, meshlambert_vert: meshlambert_vert, meshmatcap_frag: meshmatcap_frag, meshmatcap_vert: meshmatcap_vert, meshtoon_frag: meshtoon_frag, meshtoon_vert: meshtoon_vert, meshphong_frag: meshphong_frag, meshphong_vert: meshphong_vert, meshphysical_frag: meshphysical_frag, meshphysical_vert: meshphysical_vert, normal_frag: normal_frag, normal_vert: normal_vert, points_frag: points_frag, points_vert: points_vert, shadow_frag: shadow_frag, shadow_vert: shadow_vert, sprite_frag: sprite_frag, sprite_vert: sprite_vert }; /** * Uniforms library for shared webgl shaders */ const UniformsLib = { common: { diffuse: { value: new Color( 0xeeeeee ) }, opacity: { value: 1.0 }, map: { value: null }, uvTransform: { value: new Matrix3() }, uv2Transform: { value: new Matrix3() }, alphaMap: { value: null }, }, specularmap: { specularMap: { value: null }, }, envmap: { envMap: { value: null }, flipEnvMap: { value: - 1 }, reflectivity: { value: 1.0 }, refractionRatio: { value: 0.98 }, maxMipLevel: { value: 0 } }, aomap: { aoMap: { value: null }, aoMapIntensity: { value: 1 } }, lightmap: { lightMap: { value: null }, lightMapIntensity: { value: 1 } }, emissivemap: { emissiveMap: { value: null } }, bumpmap: { bumpMap: { value: null }, bumpScale: { value: 1 } }, normalmap: { normalMap: { value: null }, normalScale: { value: new Vector2( 1, 1 ) } }, displacementmap: { displacementMap: { value: null }, displacementScale: { value: 1 }, displacementBias: { value: 0 } }, roughnessmap: { roughnessMap: { value: null } }, metalnessmap: { metalnessMap: { value: null } }, gradientmap: { gradientMap: { value: null } }, fog: { fogDensity: { value: 0.00025 }, fogNear: { value: 1 }, fogFar: { value: 2000 }, fogColor: { value: new Color( 0xffffff ) } }, lights: { ambientLightColor: { value: [] }, lightProbe: { value: [] }, directionalLights: { value: [], properties: { direction: {}, color: {} } }, directionalLightShadows: { value: [], properties: { shadowBias: {}, shadowNormalBias: {}, shadowRadius: {}, shadowMapSize: {} } }, directionalShadowMap: { value: [] }, directionalShadowMatrix: { value: [] }, spotLights: { value: [], properties: { color: {}, position: {}, direction: {}, distance: {}, coneCos: {}, penumbraCos: {}, decay: {} } }, spotLightShadows: { value: [], properties: { shadowBias: {}, shadowNormalBias: {}, shadowRadius: {}, shadowMapSize: {} } }, spotShadowMap: { value: [] }, spotShadowMatrix: { value: [] }, pointLights: { value: [], properties: { color: {}, position: {}, decay: {}, distance: {} } }, pointLightShadows: { value: [], properties: { shadowBias: {}, shadowNormalBias: {}, shadowRadius: {}, shadowMapSize: {}, shadowCameraNear: {}, shadowCameraFar: {} } }, pointShadowMap: { value: [] }, pointShadowMatrix: { value: [] }, hemisphereLights: { value: [], properties: { direction: {}, skyColor: {}, groundColor: {} } }, // TODO (abelnation): RectAreaLight BRDF data needs to be moved from example to main src rectAreaLights: { value: [], properties: { color: {}, position: {}, width: {}, height: {} } }, ltc_1: { value: null }, ltc_2: { value: null } }, points: { diffuse: { value: new Color( 0xeeeeee ) }, opacity: { value: 1.0 }, size: { value: 1.0 }, scale: { value: 1.0 }, map: { value: null }, alphaMap: { value: null }, uvTransform: { value: new Matrix3() } }, sprite: { diffuse: { value: new Color( 0xeeeeee ) }, opacity: { value: 1.0 }, center: { value: new Vector2( 0.5, 0.5 ) }, rotation: { value: 0.0 }, map: { value: null }, alphaMap: { value: null }, uvTransform: { value: new Matrix3() } } }; const ShaderLib = { basic: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.specularmap, UniformsLib.envmap, UniformsLib.aomap, UniformsLib.lightmap, UniformsLib.fog ] ), vertexShader: ShaderChunk.meshbasic_vert, fragmentShader: ShaderChunk.meshbasic_frag }, lambert: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.specularmap, UniformsLib.envmap, UniformsLib.aomap, UniformsLib.lightmap, UniformsLib.emissivemap, UniformsLib.fog, UniformsLib.lights, { emissive: { value: new Color( 0x000000 ) } } ] ), vertexShader: ShaderChunk.meshlambert_vert, fragmentShader: ShaderChunk.meshlambert_frag }, phong: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.specularmap, UniformsLib.envmap, UniformsLib.aomap, UniformsLib.lightmap, UniformsLib.emissivemap, UniformsLib.bumpmap, UniformsLib.normalmap, UniformsLib.displacementmap, UniformsLib.fog, UniformsLib.lights, { emissive: { value: new Color( 0x000000 ) }, specular: { value: new Color( 0x111111 ) }, shininess: { value: 30 } } ] ), vertexShader: ShaderChunk.meshphong_vert, fragmentShader: ShaderChunk.meshphong_frag }, standard: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.envmap, UniformsLib.aomap, UniformsLib.lightmap, UniformsLib.emissivemap, UniformsLib.bumpmap, UniformsLib.normalmap, UniformsLib.displacementmap, UniformsLib.roughnessmap, UniformsLib.metalnessmap, UniformsLib.fog, UniformsLib.lights, { emissive: { value: new Color( 0x000000 ) }, roughness: { value: 1.0 }, metalness: { value: 0.0 }, envMapIntensity: { value: 1 } // temporary } ] ), vertexShader: ShaderChunk.meshphysical_vert, fragmentShader: ShaderChunk.meshphysical_frag }, toon: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.aomap, UniformsLib.lightmap, UniformsLib.emissivemap, UniformsLib.bumpmap, UniformsLib.normalmap, UniformsLib.displacementmap, UniformsLib.gradientmap, UniformsLib.fog, UniformsLib.lights, { emissive: { value: new Color( 0x000000 ) } } ] ), vertexShader: ShaderChunk.meshtoon_vert, fragmentShader: ShaderChunk.meshtoon_frag }, matcap: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.bumpmap, UniformsLib.normalmap, UniformsLib.displacementmap, UniformsLib.fog, { matcap: { value: null } } ] ), vertexShader: ShaderChunk.meshmatcap_vert, fragmentShader: ShaderChunk.meshmatcap_frag }, points: { uniforms: mergeUniforms( [ UniformsLib.points, UniformsLib.fog ] ), vertexShader: ShaderChunk.points_vert, fragmentShader: ShaderChunk.points_frag }, dashed: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.fog, { scale: { value: 1 }, dashSize: { value: 1 }, totalSize: { value: 2 } } ] ), vertexShader: ShaderChunk.linedashed_vert, fragmentShader: ShaderChunk.linedashed_frag }, depth: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.displacementmap ] ), vertexShader: ShaderChunk.depth_vert, fragmentShader: ShaderChunk.depth_frag }, normal: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.bumpmap, UniformsLib.normalmap, UniformsLib.displacementmap, { opacity: { value: 1.0 } } ] ), vertexShader: ShaderChunk.normal_vert, fragmentShader: ShaderChunk.normal_frag }, sprite: { uniforms: mergeUniforms( [ UniformsLib.sprite, UniformsLib.fog ] ), vertexShader: ShaderChunk.sprite_vert, fragmentShader: ShaderChunk.sprite_frag }, background: { uniforms: { uvTransform: { value: new Matrix3() }, t2D: { value: null }, }, vertexShader: ShaderChunk.background_vert, fragmentShader: ShaderChunk.background_frag }, /* ------------------------------------------------------------------------- // Cube map shader ------------------------------------------------------------------------- */ cube: { uniforms: mergeUniforms( [ UniformsLib.envmap, { opacity: { value: 1.0 } } ] ), vertexShader: ShaderChunk.cube_vert, fragmentShader: ShaderChunk.cube_frag }, equirect: { uniforms: { tEquirect: { value: null }, }, vertexShader: ShaderChunk.equirect_vert, fragmentShader: ShaderChunk.equirect_frag }, distanceRGBA: { uniforms: mergeUniforms( [ UniformsLib.common, UniformsLib.displacementmap, { referencePosition: { value: new Vector3() }, nearDistance: { value: 1 }, farDistance: { value: 1000 } } ] ), vertexShader: ShaderChunk.distanceRGBA_vert, fragmentShader: ShaderChunk.distanceRGBA_frag }, shadow: { uniforms: mergeUniforms( [ UniformsLib.lights, UniformsLib.fog, { color: { value: new Color( 0x00000 ) }, opacity: { value: 1.0 } }, ] ), vertexShader: ShaderChunk.shadow_vert, fragmentShader: ShaderChunk.shadow_frag } }; ShaderLib.physical = { uniforms: mergeUniforms( [ ShaderLib.standard.uniforms, { clearcoat: { value: 0 }, clearcoatMap: { value: null }, clearcoatRoughness: { value: 0 }, clearcoatRoughnessMap: { value: null }, clearcoatNormalScale: { value: new Vector2( 1, 1 ) }, clearcoatNormalMap: { value: null }, sheen: { value: new Color( 0x000000 ) }, transmission: { value: 0 }, transmissionMap: { value: null }, } ] ), vertexShader: ShaderChunk.meshphysical_vert, fragmentShader: ShaderChunk.meshphysical_frag }; function WebGLBackground( renderer, cubemaps, state, objects, premultipliedAlpha ) { const clearColor = new Color( 0x000000 ); let clearAlpha = 0; let planeMesh; let boxMesh; let currentBackground = null; let currentBackgroundVersion = 0; let currentTonemapping = null; function render( renderList, scene, camera, forceClear ) { let background = scene.isScene === true ? scene.background : null; if ( background && background.isTexture ) { background = cubemaps.get( background ); } // Ignore background in AR // TODO: Reconsider this. const xr = renderer.xr; const session = xr.getSession && xr.getSession(); if ( session && session.environmentBlendMode === 'additive' ) { background = null; } if ( background === null ) { setClear( clearColor, clearAlpha ); } else if ( background && background.isColor ) { setClear( background, 1 ); forceClear = true; } if ( renderer.autoClear || forceClear ) { renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil ); } if ( background && ( background.isCubeTexture || background.isWebGLCubeRenderTarget || background.mapping === CubeUVReflectionMapping ) ) { if ( boxMesh === undefined ) { boxMesh = new Mesh( new BoxBufferGeometry( 1, 1, 1 ), new ShaderMaterial( { name: 'BackgroundCubeMaterial', uniforms: cloneUniforms( ShaderLib.cube.uniforms ), vertexShader: ShaderLib.cube.vertexShader, fragmentShader: ShaderLib.cube.fragmentShader, side: BackSide, depthTest: false, depthWrite: false, fog: false } ) ); boxMesh.geometry.deleteAttribute( 'normal' ); boxMesh.geometry.deleteAttribute( 'uv' ); boxMesh.onBeforeRender = function ( renderer, scene, camera ) { this.matrixWorld.copyPosition( camera.matrixWorld ); }; // enable code injection for non-built-in material Object.defineProperty( boxMesh.material, 'envMap', { get: function () { return this.uniforms.envMap.value; } } ); objects.update( boxMesh ); } if ( background.isWebGLCubeRenderTarget ) { // TODO Deprecate background = background.texture; } boxMesh.material.uniforms.envMap.value = background; boxMesh.material.uniforms.flipEnvMap.value = ( background.isCubeTexture && background._needsFlipEnvMap ) ? - 1 : 1; if ( currentBackground !== background || currentBackgroundVersion !== background.version || currentTonemapping !== renderer.toneMapping ) { boxMesh.material.needsUpdate = true; currentBackground = background; currentBackgroundVersion = background.version; currentTonemapping = renderer.toneMapping; } // push to the pre-sorted opaque render list renderList.unshift( boxMesh, boxMesh.geometry, boxMesh.material, 0, 0, null ); } else if ( background && background.isTexture ) { if ( planeMesh === undefined ) { planeMesh = new Mesh( new PlaneBufferGeometry( 2, 2 ), new ShaderMaterial( { name: 'BackgroundMaterial', uniforms: cloneUniforms( ShaderLib.background.uniforms ), vertexShader: ShaderLib.background.vertexShader, fragmentShader: ShaderLib.background.fragmentShader, side: FrontSide, depthTest: false, depthWrite: false, fog: false } ) ); planeMesh.geometry.deleteAttribute( 'normal' ); // enable code injection for non-built-in material Object.defineProperty( planeMesh.material, 'map', { get: function () { return this.uniforms.t2D.value; } } ); objects.update( planeMesh ); } planeMesh.material.uniforms.t2D.value = background; if ( background.matrixAutoUpdate === true ) { background.updateMatrix(); } planeMesh.material.uniforms.uvTransform.value.copy( background.matrix ); if ( currentBackground !== background || currentBackgroundVersion !== background.version || currentTonemapping !== renderer.toneMapping ) { planeMesh.material.needsUpdate = true; currentBackground = background; currentBackgroundVersion = background.version; currentTonemapping = renderer.toneMapping; } // push to the pre-sorted opaque render list renderList.unshift( planeMesh, planeMesh.geometry, planeMesh.material, 0, 0, null ); } } function setClear( color, alpha ) { state.buffers.color.setClear( color.r, color.g, color.b, alpha, premultipliedAlpha ); } return { getClearColor: function () { return clearColor; }, setClearColor: function ( color, alpha = 1 ) { clearColor.set( color ); clearAlpha = alpha; setClear( clearColor, clearAlpha ); }, getClearAlpha: function () { return clearAlpha; }, setClearAlpha: function ( alpha ) { clearAlpha = alpha; setClear( clearColor, clearAlpha ); }, render: render }; } function WebGLBindingStates( gl, extensions, attributes, capabilities ) { const maxVertexAttributes = gl.getParameter( 34921 ); const extension = capabilities.isWebGL2 ? null : extensions.get( 'OES_vertex_array_object' ); const vaoAvailable = capabilities.isWebGL2 || extension !== null; const bindingStates = {}; const defaultState = createBindingState( null ); let currentState = defaultState; function setup( object, material, program, geometry, index ) { let updateBuffers = false; if ( vaoAvailable ) { const state = getBindingState( geometry, program, material ); if ( currentState !== state ) { currentState = state; bindVertexArrayObject( currentState.object ); } updateBuffers = needsUpdate( geometry, index ); if ( updateBuffers ) saveCache( geometry, index ); } else { const wireframe = ( material.wireframe === true ); if ( currentState.geometry !== geometry.id || currentState.program !== program.id || currentState.wireframe !== wireframe ) { currentState.geometry = geometry.id; currentState.program = program.id; currentState.wireframe = wireframe; updateBuffers = true; } } if ( object.isInstancedMesh === true ) { updateBuffers = true; } if ( index !== null ) { attributes.update( index, 34963 ); } if ( updateBuffers ) { setupVertexAttributes( object, material, program, geometry ); if ( index !== null ) { gl.bindBuffer( 34963, attributes.get( index ).buffer ); } } } function createVertexArrayObject() { if ( capabilities.isWebGL2 ) return gl.createVertexArray(); return extension.createVertexArrayOES(); } function bindVertexArrayObject( vao ) { if ( capabilities.isWebGL2 ) return gl.bindVertexArray( vao ); return extension.bindVertexArrayOES( vao ); } function deleteVertexArrayObject( vao ) { if ( capabilities.isWebGL2 ) return gl.deleteVertexArray( vao ); return extension.deleteVertexArrayOES( vao ); } function getBindingState( geometry, program, material ) { const wireframe = ( material.wireframe === true ); let programMap = bindingStates[ geometry.id ]; if ( programMap === undefined ) { programMap = {}; bindingStates[ geometry.id ] = programMap; } let stateMap = programMap[ program.id ]; if ( stateMap === undefined ) { stateMap = {}; programMap[ program.id ] = stateMap; } let state = stateMap[ wireframe ]; if ( state === undefined ) { state = createBindingState( createVertexArrayObject() ); stateMap[ wireframe ] = state; } return state; } function createBindingState( vao ) { const newAttributes = []; const enabledAttributes = []; const attributeDivisors = []; for ( let i = 0; i < maxVertexAttributes; i ++ ) { newAttributes[ i ] = 0; enabledAttributes[ i ] = 0; attributeDivisors[ i ] = 0; } return { // for backward compatibility on non-VAO support browser geometry: null, program: null, wireframe: false, newAttributes: newAttributes, enabledAttributes: enabledAttributes, attributeDivisors: attributeDivisors, object: vao, attributes: {}, index: null }; } function needsUpdate( geometry, index ) { const cachedAttributes = currentState.attributes; const geometryAttributes = geometry.attributes; let attributesNum = 0; for ( const key in geometryAttributes ) { const cachedAttribute = cachedAttributes[ key ]; const geometryAttribute = geometryAttributes[ key ]; if ( cachedAttribute === undefined ) return true; if ( cachedAttribute.attribute !== geometryAttribute ) return true; if ( cachedAttribute.data !== geometryAttribute.data ) return true; attributesNum ++; } if ( currentState.attributesNum !== attributesNum ) return true; if ( currentState.index !== index ) return true; return false; } function saveCache( geometry, index ) { const cache = {}; const attributes = geometry.attributes; let attributesNum = 0; for ( const key in attributes ) { const attribute = attributes[ key ]; const data = {}; data.attribute = attribute; if ( attribute.data ) { data.data = attribute.data; } cache[ key ] = data; attributesNum ++; } currentState.attributes = cache; currentState.attributesNum = attributesNum; currentState.index = index; } function initAttributes() { const newAttributes = currentState.newAttributes; for ( let i = 0, il = newAttributes.length; i < il; i ++ ) { newAttributes[ i ] = 0; } } function enableAttribute( attribute ) { enableAttributeAndDivisor( attribute, 0 ); } function enableAttributeAndDivisor( attribute, meshPerAttribute ) { const newAttributes = currentState.newAttributes; const enabledAttributes = currentState.enabledAttributes; const attributeDivisors = currentState.attributeDivisors; newAttributes[ attribute ] = 1; if ( enabledAttributes[ attribute ] === 0 ) { gl.enableVertexAttribArray( attribute ); enabledAttributes[ attribute ] = 1; } if ( attributeDivisors[ attribute ] !== meshPerAttribute ) { const extension = capabilities.isWebGL2 ? gl : extensions.get( 'ANGLE_instanced_arrays' ); extension[ capabilities.isWebGL2 ? 'vertexAttribDivisor' : 'vertexAttribDivisorANGLE' ]( attribute, meshPerAttribute ); attributeDivisors[ attribute ] = meshPerAttribute; } } function disableUnusedAttributes() { const newAttributes = currentState.newAttributes; const enabledAttributes = currentState.enabledAttributes; for ( let i = 0, il = enabledAttributes.length; i < il; i ++ ) { if ( enabledAttributes[ i ] !== newAttributes[ i ] ) { gl.disableVertexAttribArray( i ); enabledAttributes[ i ] = 0; } } } function vertexAttribPointer( index, size, type, normalized, stride, offset ) { if ( capabilities.isWebGL2 === true && ( type === 5124 || type === 5125 ) ) { gl.vertexAttribIPointer( index, size, type, stride, offset ); } else { gl.vertexAttribPointer( index, size, type, normalized, stride, offset ); } } function setupVertexAttributes( object, material, program, geometry ) { if ( capabilities.isWebGL2 === false && ( object.isInstancedMesh || geometry.isInstancedBufferGeometry ) ) { if ( extensions.get( 'ANGLE_instanced_arrays' ) === null ) return; } initAttributes(); const geometryAttributes = geometry.attributes; const programAttributes = program.getAttributes(); const materialDefaultAttributeValues = material.defaultAttributeValues; for ( const name in programAttributes ) { const programAttribute = programAttributes[ name ]; if ( programAttribute >= 0 ) { const geometryAttribute = geometryAttributes[ name ]; if ( geometryAttribute !== undefined ) { const normalized = geometryAttribute.normalized; const size = geometryAttribute.itemSize; const attribute = attributes.get( geometryAttribute ); // TODO Attribute may not be available on context restore if ( attribute === undefined ) continue; const buffer = attribute.buffer; const type = attribute.type; const bytesPerElement = attribute.bytesPerElement; if ( geometryAttribute.isInterleavedBufferAttribute ) { const data = geometryAttribute.data; const stride = data.stride; const offset = geometryAttribute.offset; if ( data && data.isInstancedInterleavedBuffer ) { enableAttributeAndDivisor( programAttribute, data.meshPerAttribute ); if ( geometry._maxInstanceCount === undefined ) { geometry._maxInstanceCount = data.meshPerAttribute * data.count; } } else { enableAttribute( programAttribute ); } gl.bindBuffer( 34962, buffer ); vertexAttribPointer( programAttribute, size, type, normalized, stride * bytesPerElement, offset * bytesPerElement ); } else { if ( geometryAttribute.isInstancedBufferAttribute ) { enableAttributeAndDivisor( programAttribute, geometryAttribute.meshPerAttribute ); if ( geometry._maxInstanceCount === undefined ) { geometry._maxInstanceCount = geometryAttribute.meshPerAttribute * geometryAttribute.count; } } else { enableAttribute( programAttribute ); } gl.bindBuffer( 34962, buffer ); vertexAttribPointer( programAttribute, size, type, normalized, 0, 0 ); } } else if ( name === 'instanceMatrix' ) { const attribute = attributes.get( object.instanceMatrix ); // TODO Attribute may not be available on context restore if ( attribute === undefined ) continue; const buffer = attribute.buffer; const type = attribute.type; enableAttributeAndDivisor( programAttribute + 0, 1 ); enableAttributeAndDivisor( programAttribute + 1, 1 ); enableAttributeAndDivisor( programAttribute + 2, 1 ); enableAttributeAndDivisor( programAttribute + 3, 1 ); gl.bindBuffer( 34962, buffer ); gl.vertexAttribPointer( programAttribute + 0, 4, type, false, 64, 0 ); gl.vertexAttribPointer( programAttribute + 1, 4, type, false, 64, 16 ); gl.vertexAttribPointer( programAttribute + 2, 4, type, false, 64, 32 ); gl.vertexAttribPointer( programAttribute + 3, 4, type, false, 64, 48 ); } else if ( name === 'instanceColor' ) { const attribute = attributes.get( object.instanceColor ); // TODO Attribute may not be available on context restore if ( attribute === undefined ) continue; const buffer = attribute.buffer; const type = attribute.type; enableAttributeAndDivisor( programAttribute, 1 ); gl.bindBuffer( 34962, buffer ); gl.vertexAttribPointer( programAttribute, 3, type, false, 12, 0 ); } else if ( materialDefaultAttributeValues !== undefined ) { const value = materialDefaultAttributeValues[ name ]; if ( value !== undefined ) { switch ( value.length ) { case 2: gl.vertexAttrib2fv( programAttribute, value ); break; case 3: gl.vertexAttrib3fv( programAttribute, value ); break; case 4: gl.vertexAttrib4fv( programAttribute, value ); break; default: gl.vertexAttrib1fv( programAttribute, value ); } } } } } disableUnusedAttributes(); } function dispose() { reset(); for ( const geometryId in bindingStates ) { const programMap = bindingStates[ geometryId ]; for ( const programId in programMap ) { const stateMap = programMap[ programId ]; for ( const wireframe in stateMap ) { deleteVertexArrayObject( stateMap[ wireframe ].object ); delete stateMap[ wireframe ]; } delete programMap[ programId ]; } delete bindingStates[ geometryId ]; } } function releaseStatesOfGeometry( geometry ) { if ( bindingStates[ geometry.id ] === undefined ) return; const programMap = bindingStates[ geometry.id ]; for ( const programId in programMap ) { const stateMap = programMap[ programId ]; for ( const wireframe in stateMap ) { deleteVertexArrayObject( stateMap[ wireframe ].object ); delete stateMap[ wireframe ]; } delete programMap[ programId ]; } delete bindingStates[ geometry.id ]; } function releaseStatesOfProgram( program ) { for ( const geometryId in bindingStates ) { const programMap = bindingStates[ geometryId ]; if ( programMap[ program.id ] === undefined ) continue; const stateMap = programMap[ program.id ]; for ( const wireframe in stateMap ) { deleteVertexArrayObject( stateMap[ wireframe ].object ); delete stateMap[ wireframe ]; } delete programMap[ program.id ]; } } function reset() { resetDefaultState(); if ( currentState === defaultState ) return; currentState = defaultState; bindVertexArrayObject( currentState.object ); } // for backward-compatilibity function resetDefaultState() { defaultState.geometry = null; defaultState.program = null; defaultState.wireframe = false; } return { setup: setup, reset: reset, resetDefaultState: resetDefaultState, dispose: dispose, releaseStatesOfGeometry: releaseStatesOfGeometry, releaseStatesOfProgram: releaseStatesOfProgram, initAttributes: initAttributes, enableAttribute: enableAttribute, disableUnusedAttributes: disableUnusedAttributes }; } function WebGLBufferRenderer( gl, extensions, info, capabilities ) { const isWebGL2 = capabilities.isWebGL2; let mode; function setMode( value ) { mode = value; } function render( start, count ) { gl.drawArrays( mode, start, count ); info.update( count, mode, 1 ); } function renderInstances( start, count, primcount ) { if ( primcount === 0 ) return; let extension, methodName; if ( isWebGL2 ) { extension = gl; methodName = 'drawArraysInstanced'; } else { extension = extensions.get( 'ANGLE_instanced_arrays' ); methodName = 'drawArraysInstancedANGLE'; if ( extension === null ) { console.error( 'THREE.WebGLBufferRenderer: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' ); return; } } extension[ methodName ]( mode, start, count, primcount ); info.update( count, mode, primcount ); } // this.setMode = setMode; this.render = render; this.renderInstances = renderInstances; } function WebGLCapabilities( gl, extensions, parameters ) { let maxAnisotropy; function getMaxAnisotropy() { if ( maxAnisotropy !== undefined ) return maxAnisotropy; const extension = extensions.get( 'EXT_texture_filter_anisotropic' ); if ( extension !== null ) { maxAnisotropy = gl.getParameter( extension.MAX_TEXTURE_MAX_ANISOTROPY_EXT ); } else { maxAnisotropy = 0; } return maxAnisotropy; } function getMaxPrecision( precision ) { if ( precision === 'highp' ) { if ( gl.getShaderPrecisionFormat( 35633, 36338 ).precision > 0 && gl.getShaderPrecisionFormat( 35632, 36338 ).precision > 0 ) { return 'highp'; } precision = 'mediump'; } if ( precision === 'mediump' ) { if ( gl.getShaderPrecisionFormat( 35633, 36337 ).precision > 0 && gl.getShaderPrecisionFormat( 35632, 36337 ).precision > 0 ) { return 'mediump'; } } return 'lowp'; } /* eslint-disable no-undef */ const isWebGL2 = ( typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext ) || ( typeof WebGL2ComputeRenderingContext !== 'undefined' && gl instanceof WebGL2ComputeRenderingContext ); /* eslint-enable no-undef */ let precision = parameters.precision !== undefined ? parameters.precision : 'highp'; const maxPrecision = getMaxPrecision( precision ); if ( maxPrecision !== precision ) { console.warn( 'THREE.WebGLRenderer:', precision, 'not supported, using', maxPrecision, 'instead.' ); precision = maxPrecision; } const logarithmicDepthBuffer = parameters.logarithmicDepthBuffer === true; const maxTextures = gl.getParameter( 34930 ); const maxVertexTextures = gl.getParameter( 35660 ); const maxTextureSize = gl.getParameter( 3379 ); const maxCubemapSize = gl.getParameter( 34076 ); const maxAttributes = gl.getParameter( 34921 ); const maxVertexUniforms = gl.getParameter( 36347 ); const maxVaryings = gl.getParameter( 36348 ); const maxFragmentUniforms = gl.getParameter( 36349 ); const vertexTextures = maxVertexTextures > 0; const floatFragmentTextures = isWebGL2 || !! extensions.get( 'OES_texture_float' ); const floatVertexTextures = vertexTextures && floatFragmentTextures; const maxSamples = isWebGL2 ? gl.getParameter( 36183 ) : 0; return { isWebGL2: isWebGL2, getMaxAnisotropy: getMaxAnisotropy, getMaxPrecision: getMaxPrecision, precision: precision, logarithmicDepthBuffer: logarithmicDepthBuffer, maxTextures: maxTextures, maxVertexTextures: maxVertexTextures, maxTextureSize: maxTextureSize, maxCubemapSize: maxCubemapSize, maxAttributes: maxAttributes, maxVertexUniforms: maxVertexUniforms, maxVaryings: maxVaryings, maxFragmentUniforms: maxFragmentUniforms, vertexTextures: vertexTextures, floatFragmentTextures: floatFragmentTextures, floatVertexTextures: floatVertexTextures, maxSamples: maxSamples }; } function WebGLClipping( properties ) { const scope = this; let globalState = null, numGlobalPlanes = 0, localClippingEnabled = false, renderingShadows = false; const plane = new Plane(), viewNormalMatrix = new Matrix3(), uniform = { value: null, needsUpdate: false }; this.uniform = uniform; this.numPlanes = 0; this.numIntersection = 0; this.init = function ( planes, enableLocalClipping, camera ) { const enabled = planes.length !== 0 || enableLocalClipping || // enable state of previous frame - the clipping code has to // run another frame in order to reset the state: numGlobalPlanes !== 0 || localClippingEnabled; localClippingEnabled = enableLocalClipping; globalState = projectPlanes( planes, camera, 0 ); numGlobalPlanes = planes.length; return enabled; }; this.beginShadows = function () { renderingShadows = true; projectPlanes( null ); }; this.endShadows = function () { renderingShadows = false; resetGlobalState(); }; this.setState = function ( material, camera, useCache ) { const planes = material.clippingPlanes, clipIntersection = material.clipIntersection, clipShadows = material.clipShadows; const materialProperties = properties.get( material ); if ( ! localClippingEnabled || planes === null || planes.length === 0 || renderingShadows && ! clipShadows ) { // there's no local clipping if ( renderingShadows ) { // there's no global clipping projectPlanes( null ); } else { resetGlobalState(); } } else { const nGlobal = renderingShadows ? 0 : numGlobalPlanes, lGlobal = nGlobal * 4; let dstArray = materialProperties.clippingState || null; uniform.value = dstArray; // ensure unique state dstArray = projectPlanes( planes, camera, lGlobal, useCache ); for ( let i = 0; i !== lGlobal; ++ i ) { dstArray[ i ] = globalState[ i ]; } materialProperties.clippingState = dstArray; this.numIntersection = clipIntersection ? this.numPlanes : 0; this.numPlanes += nGlobal; } }; function resetGlobalState() { if ( uniform.value !== globalState ) { uniform.value = globalState; uniform.needsUpdate = numGlobalPlanes > 0; } scope.numPlanes = numGlobalPlanes; scope.numIntersection = 0; } function projectPlanes( planes, camera, dstOffset, skipTransform ) { const nPlanes = planes !== null ? planes.length : 0; let dstArray = null; if ( nPlanes !== 0 ) { dstArray = uniform.value; if ( skipTransform !== true || dstArray === null ) { const flatSize = dstOffset + nPlanes * 4, viewMatrix = camera.matrixWorldInverse; viewNormalMatrix.getNormalMatrix( viewMatrix ); if ( dstArray === null || dstArray.length < flatSize ) { dstArray = new Float32Array( flatSize ); } for ( let i = 0, i4 = dstOffset; i !== nPlanes; ++ i, i4 += 4 ) { plane.copy( planes[ i ] ).applyMatrix4( viewMatrix, viewNormalMatrix ); plane.normal.toArray( dstArray, i4 ); dstArray[ i4 + 3 ] = plane.constant; } } uniform.value = dstArray; uniform.needsUpdate = true; } scope.numPlanes = nPlanes; scope.numIntersection = 0; return dstArray; } } function WebGLCubeMaps( renderer ) { let cubemaps = new WeakMap(); function mapTextureMapping( texture, mapping ) { if ( mapping === EquirectangularReflectionMapping ) { texture.mapping = CubeReflectionMapping; } else if ( mapping === EquirectangularRefractionMapping ) { texture.mapping = CubeRefractionMapping; } return texture; } function get( texture ) { if ( texture && texture.isTexture ) { const mapping = texture.mapping; if ( mapping === EquirectangularReflectionMapping || mapping === EquirectangularRefractionMapping ) { if ( cubemaps.has( texture ) ) { const cubemap = cubemaps.get( texture ).texture; return mapTextureMapping( cubemap, texture.mapping ); } else { const image = texture.image; if ( image && image.height > 0 ) { const currentRenderList = renderer.getRenderList(); const currentRenderTarget = renderer.getRenderTarget(); const renderTarget = new WebGLCubeRenderTarget( image.height / 2 ); renderTarget.fromEquirectangularTexture( renderer, texture ); cubemaps.set( texture, renderTarget ); renderer.setRenderTarget( currentRenderTarget ); renderer.setRenderList( currentRenderList ); texture.addEventListener( 'dispose', onTextureDispose ); return mapTextureMapping( renderTarget.texture, texture.mapping ); } else { // image not yet ready. try the conversion next frame return null; } } } } return texture; } function onTextureDispose( event ) { const texture = event.target; texture.removeEventListener( 'dispose', onTextureDispose ); const cubemap = cubemaps.get( texture ); if ( cubemap !== undefined ) { cubemaps.delete( texture ); cubemap.dispose(); } } function dispose() { cubemaps = new WeakMap(); } return { get: get, dispose: dispose }; } function WebGLExtensions( gl ) { const extensions = {}; return { has: function ( name ) { if ( extensions[ name ] !== undefined ) { return extensions[ name ] !== null; } let extension; switch ( name ) { case 'WEBGL_depth_texture': extension = gl.getExtension( 'WEBGL_depth_texture' ) || gl.getExtension( 'MOZ_WEBGL_depth_texture' ) || gl.getExtension( 'WEBKIT_WEBGL_depth_texture' ); break; case 'EXT_texture_filter_anisotropic': extension = gl.getExtension( 'EXT_texture_filter_anisotropic' ) || gl.getExtension( 'MOZ_EXT_texture_filter_anisotropic' ) || gl.getExtension( 'WEBKIT_EXT_texture_filter_anisotropic' ); break; case 'WEBGL_compressed_texture_s3tc': extension = gl.getExtension( 'WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'MOZ_WEBGL_compressed_texture_s3tc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_s3tc' ); break; case 'WEBGL_compressed_texture_pvrtc': extension = gl.getExtension( 'WEBGL_compressed_texture_pvrtc' ) || gl.getExtension( 'WEBKIT_WEBGL_compressed_texture_pvrtc' ); break; default: extension = gl.getExtension( name ); } extensions[ name ] = extension; return extension !== null; }, get: function ( name ) { if ( ! this.has( name ) ) { console.warn( 'THREE.WebGLRenderer: ' + name + ' extension not supported.' ); } return extensions[ name ]; } }; } function WebGLGeometries( gl, attributes, info, bindingStates ) { const geometries = new WeakMap(); const wireframeAttributes = new WeakMap(); function onGeometryDispose( event ) { const geometry = event.target; const buffergeometry = geometries.get( geometry ); if ( buffergeometry.index !== null ) { attributes.remove( buffergeometry.index ); } for ( const name in buffergeometry.attributes ) { attributes.remove( buffergeometry.attributes[ name ] ); } geometry.removeEventListener( 'dispose', onGeometryDispose ); geometries.delete( geometry ); const attribute = wireframeAttributes.get( buffergeometry ); if ( attribute ) { attributes.remove( attribute ); wireframeAttributes.delete( buffergeometry ); } bindingStates.releaseStatesOfGeometry( buffergeometry ); if ( geometry.isInstancedBufferGeometry === true ) { delete geometry._maxInstanceCount; } // info.memory.geometries --; } function get( object, geometry ) { let buffergeometry = geometries.get( geometry ); if ( buffergeometry ) return buffergeometry; geometry.addEventListener( 'dispose', onGeometryDispose ); if ( geometry.isBufferGeometry ) { buffergeometry = geometry; } else if ( geometry.isGeometry ) { if ( geometry._bufferGeometry === undefined ) { geometry._bufferGeometry = new BufferGeometry().setFromObject( object ); } buffergeometry = geometry._bufferGeometry; } geometries.set( geometry, buffergeometry ); info.memory.geometries ++; return buffergeometry; } function update( geometry ) { const geometryAttributes = geometry.attributes; // Updating index buffer in VAO now. See WebGLBindingStates. for ( const name in geometryAttributes ) { attributes.update( geometryAttributes[ name ], 34962 ); } // morph targets const morphAttributes = geometry.morphAttributes; for ( const name in morphAttributes ) { const array = morphAttributes[ name ]; for ( let i = 0, l = array.length; i < l; i ++ ) { attributes.update( array[ i ], 34962 ); } } } function updateWireframeAttribute( geometry ) { const indices = []; const geometryIndex = geometry.index; const geometryPosition = geometry.attributes.position; let version = 0; if ( geometryIndex !== null ) { const array = geometryIndex.array; version = geometryIndex.version; for ( let i = 0, l = array.length; i < l; i += 3 ) { const a = array[ i + 0 ]; const b = array[ i + 1 ]; const c = array[ i + 2 ]; indices.push( a, b, b, c, c, a ); } } else { const array = geometryPosition.array; version = geometryPosition.version; for ( let i = 0, l = ( array.length / 3 ) - 1; i < l; i += 3 ) { const a = i + 0; const b = i + 1; const c = i + 2; indices.push( a, b, b, c, c, a ); } } const attribute = new ( arrayMax( indices ) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute )( indices, 1 ); attribute.version = version; // Updating index buffer in VAO now. See WebGLBindingStates // const previousAttribute = wireframeAttributes.get( geometry ); if ( previousAttribute ) attributes.remove( previousAttribute ); // wireframeAttributes.set( geometry, attribute ); } function getWireframeAttribute( geometry ) { const currentAttribute = wireframeAttributes.get( geometry ); if ( currentAttribute ) { const geometryIndex = geometry.index; if ( geometryIndex !== null ) { // if the attribute is obsolete, create a new one if ( currentAttribute.version < geometryIndex.version ) { updateWireframeAttribute( geometry ); } } } else { updateWireframeAttribute( geometry ); } return wireframeAttributes.get( geometry ); } return { get: get, update: update, getWireframeAttribute: getWireframeAttribute }; } function WebGLIndexedBufferRenderer( gl, extensions, info, capabilities ) { const isWebGL2 = capabilities.isWebGL2; let mode; function setMode( value ) { mode = value; } let type, bytesPerElement; function setIndex( value ) { type = value.type; bytesPerElement = value.bytesPerElement; } function render( start, count ) { gl.drawElements( mode, count, type, start * bytesPerElement ); info.update( count, mode, 1 ); } function renderInstances( start, count, primcount ) { if ( primcount === 0 ) return; let extension, methodName; if ( isWebGL2 ) { extension = gl; methodName = 'drawElementsInstanced'; } else { extension = extensions.get( 'ANGLE_instanced_arrays' ); methodName = 'drawElementsInstancedANGLE'; if ( extension === null ) { console.error( 'THREE.WebGLIndexedBufferRenderer: using THREE.InstancedBufferGeometry but hardware does not support extension ANGLE_instanced_arrays.' ); return; } } extension[ methodName ]( mode, count, type, start * bytesPerElement, primcount ); info.update( count, mode, primcount ); } // this.setMode = setMode; this.setIndex = setIndex; this.render = render; this.renderInstances = renderInstances; } function WebGLInfo( gl ) { const memory = { geometries: 0, textures: 0 }; const render = { frame: 0, calls: 0, triangles: 0, points: 0, lines: 0 }; function update( count, mode, instanceCount ) { render.calls ++; switch ( mode ) { case 4: render.triangles += instanceCount * ( count / 3 ); break; case 1: render.lines += instanceCount * ( count / 2 ); break; case 3: render.lines += instanceCount * ( count - 1 ); break; case 2: render.lines += instanceCount * count; break; case 0: render.points += instanceCount * count; break; default: console.error( 'THREE.WebGLInfo: Unknown draw mode:', mode ); break; } } function reset() { render.frame ++; render.calls = 0; render.triangles = 0; render.points = 0; render.lines = 0; } return { memory: memory, render: render, programs: null, autoReset: true, reset: reset, update: update }; } function numericalSort( a, b ) { return a[ 0 ] - b[ 0 ]; } function absNumericalSort( a, b ) { return Math.abs( b[ 1 ] ) - Math.abs( a[ 1 ] ); } function WebGLMorphtargets( gl ) { const influencesList = {}; const morphInfluences = new Float32Array( 8 ); const workInfluences = []; for ( let i = 0; i < 8; i ++ ) { workInfluences[ i ] = [ i, 0 ]; } function update( object, geometry, material, program ) { const objectInfluences = object.morphTargetInfluences; // When object doesn't have morph target influences defined, we treat it as a 0-length array // This is important to make sure we set up morphTargetBaseInfluence / morphTargetInfluences const length = objectInfluences === undefined ? 0 : objectInfluences.length; let influences = influencesList[ geometry.id ]; if ( influences === undefined ) { // initialise list influences = []; for ( let i = 0; i < length; i ++ ) { influences[ i ] = [ i, 0 ]; } influencesList[ geometry.id ] = influences; } // Collect influences for ( let i = 0; i < length; i ++ ) { const influence = influences[ i ]; influence[ 0 ] = i; influence[ 1 ] = objectInfluences[ i ]; } influences.sort( absNumericalSort ); for ( let i = 0; i < 8; i ++ ) { if ( i < length && influences[ i ][ 1 ] ) { workInfluences[ i ][ 0 ] = influences[ i ][ 0 ]; workInfluences[ i ][ 1 ] = influences[ i ][ 1 ]; } else { workInfluences[ i ][ 0 ] = Number.MAX_SAFE_INTEGER; workInfluences[ i ][ 1 ] = 0; } } workInfluences.sort( numericalSort ); const morphTargets = material.morphTargets && geometry.morphAttributes.position; const morphNormals = material.morphNormals && geometry.morphAttributes.normal; let morphInfluencesSum = 0; for ( let i = 0; i < 8; i ++ ) { const influence = workInfluences[ i ]; const index = influence[ 0 ]; const value = influence[ 1 ]; if ( index !== Number.MAX_SAFE_INTEGER && value ) { if ( morphTargets && geometry.getAttribute( 'morphTarget' + i ) !== morphTargets[ index ] ) { geometry.setAttribute( 'morphTarget' + i, morphTargets[ index ] ); } if ( morphNormals && geometry.getAttribute( 'morphNormal' + i ) !== morphNormals[ index ] ) { geometry.setAttribute( 'morphNormal' + i, morphNormals[ index ] ); } morphInfluences[ i ] = value; morphInfluencesSum += value; } else { if ( morphTargets && geometry.hasAttribute( 'morphTarget' + i ) === true ) { geometry.deleteAttribute( 'morphTarget' + i ); } if ( morphNormals && geometry.hasAttribute( 'morphNormal' + i ) === true ) { geometry.deleteAttribute( 'morphNormal' + i ); } morphInfluences[ i ] = 0; } } // GLSL shader uses formula baseinfluence * base + sum(target * influence) // This allows us to switch between absolute morphs and relative morphs without changing shader code // When baseinfluence = 1 - sum(influence), the above is equivalent to sum((target - base) * influence) const morphBaseInfluence = geometry.morphTargetsRelative ? 1 : 1 - morphInfluencesSum; program.getUniforms().setValue( gl, 'morphTargetBaseInfluence', morphBaseInfluence ); program.getUniforms().setValue( gl, 'morphTargetInfluences', morphInfluences ); } return { update: update }; } function WebGLObjects( gl, geometries, attributes, info ) { let updateMap = new WeakMap(); function update( object ) { const frame = info.render.frame; const geometry = object.geometry; const buffergeometry = geometries.get( object, geometry ); // Update once per frame if ( updateMap.get( buffergeometry ) !== frame ) { if ( geometry.isGeometry ) { buffergeometry.updateFromObject( object ); } geometries.update( buffergeometry ); updateMap.set( buffergeometry, frame ); } if ( object.isInstancedMesh ) { if ( object.hasEventListener( 'dispose', onInstancedMeshDispose ) === false ) { object.addEventListener( 'dispose', onInstancedMeshDispose ); } attributes.update( object.instanceMatrix, 34962 ); if ( object.instanceColor !== null ) { attributes.update( object.instanceColor, 34962 ); } } return buffergeometry; } function dispose() { updateMap = new WeakMap(); } function onInstancedMeshDispose( event ) { const instancedMesh = event.target; instancedMesh.removeEventListener( 'dispose', onInstancedMeshDispose ); attributes.remove( instancedMesh.instanceMatrix ); if ( instancedMesh.instanceColor !== null ) attributes.remove( instancedMesh.instanceColor ); } return { update: update, dispose: dispose }; } function DataTexture2DArray( data = null, width = 1, height = 1, depth = 1 ) { Texture.call( this, null ); this.image = { data, width, height, depth }; this.magFilter = NearestFilter; this.minFilter = NearestFilter; this.wrapR = ClampToEdgeWrapping; this.generateMipmaps = false; this.flipY = false; this.needsUpdate = true; } DataTexture2DArray.prototype = Object.create( Texture.prototype ); DataTexture2DArray.prototype.constructor = DataTexture2DArray; DataTexture2DArray.prototype.isDataTexture2DArray = true; function DataTexture3D( data = null, width = 1, height = 1, depth = 1 ) { // We're going to add .setXXX() methods for setting properties later. // Users can still set in DataTexture3D directly. // // const texture = new THREE.DataTexture3D( data, width, height, depth ); // texture.anisotropy = 16; // // See #14839 Texture.call( this, null ); this.image = { data, width, height, depth }; this.magFilter = NearestFilter; this.minFilter = NearestFilter; this.wrapR = ClampToEdgeWrapping; this.generateMipmaps = false; this.flipY = false; this.needsUpdate = true; } DataTexture3D.prototype = Object.create( Texture.prototype ); DataTexture3D.prototype.constructor = DataTexture3D; DataTexture3D.prototype.isDataTexture3D = true; /** * Uniforms of a program. * Those form a tree structure with a special top-level container for the root, * which you get by calling 'new WebGLUniforms( gl, program )'. * * * Properties of inner nodes including the top-level container: * * .seq - array of nested uniforms * .map - nested uniforms by name * * * Methods of all nodes except the top-level container: * * .setValue( gl, value, [textures] ) * * uploads a uniform value(s) * the 'textures' parameter is needed for sampler uniforms * * * Static methods of the top-level container (textures factorizations): * * .upload( gl, seq, values, textures ) * * sets uniforms in 'seq' to 'values[id].value' * * .seqWithValue( seq, values ) : filteredSeq * * filters 'seq' entries with corresponding entry in values * * * Methods of the top-level container (textures factorizations): * * .setValue( gl, name, value, textures ) * * sets uniform with name 'name' to 'value' * * .setOptional( gl, obj, prop ) * * like .set for an optional property of the object * */ const emptyTexture = new Texture(); const emptyTexture2dArray = new DataTexture2DArray(); const emptyTexture3d = new DataTexture3D(); const emptyCubeTexture = new CubeTexture(); // --- Utilities --- // Array Caches (provide typed arrays for temporary by size) const arrayCacheF32 = []; const arrayCacheI32 = []; // Float32Array caches used for uploading Matrix uniforms const mat4array = new Float32Array( 16 ); const mat3array = new Float32Array( 9 ); const mat2array = new Float32Array( 4 ); // Flattening for arrays of vectors and matrices function flatten( array, nBlocks, blockSize ) { const firstElem = array[ 0 ]; if ( firstElem <= 0 || firstElem > 0 ) return array; // unoptimized: ! isNaN( firstElem ) // see http://jacksondunstan.com/articles/983 const n = nBlocks * blockSize; let r = arrayCacheF32[ n ]; if ( r === undefined ) { r = new Float32Array( n ); arrayCacheF32[ n ] = r; } if ( nBlocks !== 0 ) { firstElem.toArray( r, 0 ); for ( let i = 1, offset = 0; i !== nBlocks; ++ i ) { offset += blockSize; array[ i ].toArray( r, offset ); } } return r; } function arraysEqual( a, b ) { if ( a.length !== b.length ) return false; for ( let i = 0, l = a.length; i < l; i ++ ) { if ( a[ i ] !== b[ i ] ) return false; } return true; } function copyArray( a, b ) { for ( let i = 0, l = b.length; i < l; i ++ ) { a[ i ] = b[ i ]; } } // Texture unit allocation function allocTexUnits( textures, n ) { let r = arrayCacheI32[ n ]; if ( r === undefined ) { r = new Int32Array( n ); arrayCacheI32[ n ] = r; } for ( let i = 0; i !== n; ++ i ) { r[ i ] = textures.allocateTextureUnit(); } return r; } // --- Setters --- // Note: Defining these methods externally, because they come in a bunch // and this way their names minify. // Single scalar function setValueV1f( gl, v ) { const cache = this.cache; if ( cache[ 0 ] === v ) return; gl.uniform1f( this.addr, v ); cache[ 0 ] = v; } // Single float vector (from flat array or THREE.VectorN) function setValueV2f( gl, v ) { const cache = this.cache; if ( v.x !== undefined ) { if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y ) { gl.uniform2f( this.addr, v.x, v.y ); cache[ 0 ] = v.x; cache[ 1 ] = v.y; } } else { if ( arraysEqual( cache, v ) ) return; gl.uniform2fv( this.addr, v ); copyArray( cache, v ); } } function setValueV3f( gl, v ) { const cache = this.cache; if ( v.x !== undefined ) { if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z ) { gl.uniform3f( this.addr, v.x, v.y, v.z ); cache[ 0 ] = v.x; cache[ 1 ] = v.y; cache[ 2 ] = v.z; } } else if ( v.r !== undefined ) { if ( cache[ 0 ] !== v.r || cache[ 1 ] !== v.g || cache[ 2 ] !== v.b ) { gl.uniform3f( this.addr, v.r, v.g, v.b ); cache[ 0 ] = v.r; cache[ 1 ] = v.g; cache[ 2 ] = v.b; } } else { if ( arraysEqual( cache, v ) ) return; gl.uniform3fv( this.addr, v ); copyArray( cache, v ); } } function setValueV4f( gl, v ) { const cache = this.cache; if ( v.x !== undefined ) { if ( cache[ 0 ] !== v.x || cache[ 1 ] !== v.y || cache[ 2 ] !== v.z || cache[ 3 ] !== v.w ) { gl.uniform4f( this.addr, v.x, v.y, v.z, v.w ); cache[ 0 ] = v.x; cache[ 1 ] = v.y; cache[ 2 ] = v.z; cache[ 3 ] = v.w; } } else { if ( arraysEqual( cache, v ) ) return; gl.uniform4fv( this.addr, v ); copyArray( cache, v ); } } // Single matrix (from flat array or MatrixN) function setValueM2( gl, v ) { const cache = this.cache; const elements = v.elements; if ( elements === undefined ) { if ( arraysEqual( cache, v ) ) return; gl.uniformMatrix2fv( this.addr, false, v ); copyArray( cache, v ); } else { if ( arraysEqual( cache, elements ) ) return; mat2array.set( elements ); gl.uniformMatrix2fv( this.addr, false, mat2array ); copyArray( cache, elements ); } } function setValueM3( gl, v ) { const cache = this.cache; const elements = v.elements; if ( elements === undefined ) { if ( arraysEqual( cache, v ) ) return; gl.uniformMatrix3fv( this.addr, false, v ); copyArray( cache, v ); } else { if ( arraysEqual( cache, elements ) ) return; mat3array.set( elements ); gl.uniformMatrix3fv( this.addr, false, mat3array ); copyArray( cache, elements ); } } function setValueM4( gl, v ) { const cache = this.cache; const elements = v.elements; if ( elements === undefined ) { if ( arraysEqual( cache, v ) ) return; gl.uniformMatrix4fv( this.addr, false, v ); copyArray( cache, v ); } else { if ( arraysEqual( cache, elements ) ) return; mat4array.set( elements ); gl.uniformMatrix4fv( this.addr, false, mat4array ); copyArray( cache, elements ); } } // Single texture (2D / Cube) function setValueT1( gl, v, textures ) { const cache = this.cache; const unit = textures.allocateTextureUnit(); if ( cache[ 0 ] !== unit ) { gl.uniform1i( this.addr, unit ); cache[ 0 ] = unit; } textures.safeSetTexture2D( v || emptyTexture, unit ); } function setValueT2DArray1( gl, v, textures ) { const cache = this.cache; const unit = textures.allocateTextureUnit(); if ( cache[ 0 ] !== unit ) { gl.uniform1i( this.addr, unit ); cache[ 0 ] = unit; } textures.setTexture2DArray( v || emptyTexture2dArray, unit ); } function setValueT3D1( gl, v, textures ) { const cache = this.cache; const unit = textures.allocateTextureUnit(); if ( cache[ 0 ] !== unit ) { gl.uniform1i( this.addr, unit ); cache[ 0 ] = unit; } textures.setTexture3D( v || emptyTexture3d, unit ); } function setValueT6( gl, v, textures ) { const cache = this.cache; const unit = textures.allocateTextureUnit(); if ( cache[ 0 ] !== unit ) { gl.uniform1i( this.addr, unit ); cache[ 0 ] = unit; } textures.safeSetTextureCube( v || emptyCubeTexture, unit ); } // Integer / Boolean vectors or arrays thereof (always flat arrays) function setValueV1i( gl, v ) { const cache = this.cache; if ( cache[ 0 ] === v ) return; gl.uniform1i( this.addr, v ); cache[ 0 ] = v; } function setValueV2i( gl, v ) { const cache = this.cache; if ( arraysEqual( cache, v ) ) return; gl.uniform2iv( this.addr, v ); copyArray( cache, v ); } function setValueV3i( gl, v ) { const cache = this.cache; if ( arraysEqual( cache, v ) ) return; gl.uniform3iv( this.addr, v ); copyArray( cache, v ); } function setValueV4i( gl, v ) { const cache = this.cache; if ( arraysEqual( cache, v ) ) return; gl.uniform4iv( this.addr, v ); copyArray( cache, v ); } // uint function setValueV1ui( gl, v ) { const cache = this.cache; if ( cache[ 0 ] === v ) return; gl.uniform1ui( this.addr, v ); cache[ 0 ] = v; } // Helper to pick the right setter for the singular case function getSingularSetter( type ) { switch ( type ) { case 0x1406: return setValueV1f; // FLOAT case 0x8b50: return setValueV2f; // _VEC2 case 0x8b51: return setValueV3f; // _VEC3 case 0x8b52: return setValueV4f; // _VEC4 case 0x8b5a: return setValueM2; // _MAT2 case 0x8b5b: return setValueM3; // _MAT3 case 0x8b5c: return setValueM4; // _MAT4 case 0x1404: case 0x8b56: return setValueV1i; // INT, BOOL case 0x8b53: case 0x8b57: return setValueV2i; // _VEC2 case 0x8b54: case 0x8b58: return setValueV3i; // _VEC3 case 0x8b55: case 0x8b59: return setValueV4i; // _VEC4 case 0x1405: return setValueV1ui; // UINT case 0x8b5e: // SAMPLER_2D case 0x8d66: // SAMPLER_EXTERNAL_OES case 0x8dca: // INT_SAMPLER_2D case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D case 0x8b62: // SAMPLER_2D_SHADOW return setValueT1; case 0x8b5f: // SAMPLER_3D case 0x8dcb: // INT_SAMPLER_3D case 0x8dd3: // UNSIGNED_INT_SAMPLER_3D return setValueT3D1; case 0x8b60: // SAMPLER_CUBE case 0x8dcc: // INT_SAMPLER_CUBE case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE case 0x8dc5: // SAMPLER_CUBE_SHADOW return setValueT6; case 0x8dc1: // SAMPLER_2D_ARRAY case 0x8dcf: // INT_SAMPLER_2D_ARRAY case 0x8dd7: // UNSIGNED_INT_SAMPLER_2D_ARRAY case 0x8dc4: // SAMPLER_2D_ARRAY_SHADOW return setValueT2DArray1; } } // Array of scalars function setValueV1fArray( gl, v ) { gl.uniform1fv( this.addr, v ); } // Integer / Boolean vectors or arrays thereof (always flat arrays) function setValueV1iArray( gl, v ) { gl.uniform1iv( this.addr, v ); } function setValueV2iArray( gl, v ) { gl.uniform2iv( this.addr, v ); } function setValueV3iArray( gl, v ) { gl.uniform3iv( this.addr, v ); } function setValueV4iArray( gl, v ) { gl.uniform4iv( this.addr, v ); } // Array of vectors (flat or from THREE classes) function setValueV2fArray( gl, v ) { const data = flatten( v, this.size, 2 ); gl.uniform2fv( this.addr, data ); } function setValueV3fArray( gl, v ) { const data = flatten( v, this.size, 3 ); gl.uniform3fv( this.addr, data ); } function setValueV4fArray( gl, v ) { const data = flatten( v, this.size, 4 ); gl.uniform4fv( this.addr, data ); } // Array of matrices (flat or from THREE clases) function setValueM2Array( gl, v ) { const data = flatten( v, this.size, 4 ); gl.uniformMatrix2fv( this.addr, false, data ); } function setValueM3Array( gl, v ) { const data = flatten( v, this.size, 9 ); gl.uniformMatrix3fv( this.addr, false, data ); } function setValueM4Array( gl, v ) { const data = flatten( v, this.size, 16 ); gl.uniformMatrix4fv( this.addr, false, data ); } // Array of textures (2D / Cube) function setValueT1Array( gl, v, textures ) { const n = v.length; const units = allocTexUnits( textures, n ); gl.uniform1iv( this.addr, units ); for ( let i = 0; i !== n; ++ i ) { textures.safeSetTexture2D( v[ i ] || emptyTexture, units[ i ] ); } } function setValueT6Array( gl, v, textures ) { const n = v.length; const units = allocTexUnits( textures, n ); gl.uniform1iv( this.addr, units ); for ( let i = 0; i !== n; ++ i ) { textures.safeSetTextureCube( v[ i ] || emptyCubeTexture, units[ i ] ); } } // Helper to pick the right setter for a pure (bottom-level) array function getPureArraySetter( type ) { switch ( type ) { case 0x1406: return setValueV1fArray; // FLOAT case 0x8b50: return setValueV2fArray; // _VEC2 case 0x8b51: return setValueV3fArray; // _VEC3 case 0x8b52: return setValueV4fArray; // _VEC4 case 0x8b5a: return setValueM2Array; // _MAT2 case 0x8b5b: return setValueM3Array; // _MAT3 case 0x8b5c: return setValueM4Array; // _MAT4 case 0x1404: case 0x8b56: return setValueV1iArray; // INT, BOOL case 0x8b53: case 0x8b57: return setValueV2iArray; // _VEC2 case 0x8b54: case 0x8b58: return setValueV3iArray; // _VEC3 case 0x8b55: case 0x8b59: return setValueV4iArray; // _VEC4 case 0x8b5e: // SAMPLER_2D case 0x8d66: // SAMPLER_EXTERNAL_OES case 0x8dca: // INT_SAMPLER_2D case 0x8dd2: // UNSIGNED_INT_SAMPLER_2D case 0x8b62: // SAMPLER_2D_SHADOW return setValueT1Array; case 0x8b60: // SAMPLER_CUBE case 0x8dcc: // INT_SAMPLER_CUBE case 0x8dd4: // UNSIGNED_INT_SAMPLER_CUBE case 0x8dc5: // SAMPLER_CUBE_SHADOW return setValueT6Array; } } // --- Uniform Classes --- function SingleUniform( id, activeInfo, addr ) { this.id = id; this.addr = addr; this.cache = []; this.setValue = getSingularSetter( activeInfo.type ); // this.path = activeInfo.name; // DEBUG } function PureArrayUniform( id, activeInfo, addr ) { this.id = id; this.addr = addr; this.cache = []; this.size = activeInfo.size; this.setValue = getPureArraySetter( activeInfo.type ); // this.path = activeInfo.name; // DEBUG } PureArrayUniform.prototype.updateCache = function ( data ) { const cache = this.cache; if ( data instanceof Float32Array && cache.length !== data.length ) { this.cache = new Float32Array( data.length ); } copyArray( cache, data ); }; function StructuredUniform( id ) { this.id = id; this.seq = []; this.map = {}; } StructuredUniform.prototype.setValue = function ( gl, value, textures ) { const seq = this.seq; for ( let i = 0, n = seq.length; i !== n; ++ i ) { const u = seq[ i ]; u.setValue( gl, value[ u.id ], textures ); } }; // --- Top-level --- // Parser - builds up the property tree from the path strings const RePathPart = /(\w+)(\])?(\[|\.)?/g; // extracts // - the identifier (member name or array index) // - followed by an optional right bracket (found when array index) // - followed by an optional left bracket or dot (type of subscript) // // Note: These portions can be read in a non-overlapping fashion and // allow straightforward parsing of the hierarchy that WebGL encodes // in the uniform names. function addUniform( container, uniformObject ) { container.seq.push( uniformObject ); container.map[ uniformObject.id ] = uniformObject; } function parseUniform( activeInfo, addr, container ) { const path = activeInfo.name, pathLength = path.length; // reset RegExp object, because of the early exit of a previous run RePathPart.lastIndex = 0; while ( true ) { const match = RePathPart.exec( path ), matchEnd = RePathPart.lastIndex; let id = match[ 1 ]; const idIsIndex = match[ 2 ] === ']', subscript = match[ 3 ]; if ( idIsIndex ) id = id | 0; // convert to integer if ( subscript === undefined || subscript === '[' && matchEnd + 2 === pathLength ) { // bare name or "pure" bottom-level array "[0]" suffix addUniform( container, subscript === undefined ? new SingleUniform( id, activeInfo, addr ) : new PureArrayUniform( id, activeInfo, addr ) ); break; } else { // step into inner node / create it in case it doesn't exist const map = container.map; let next = map[ id ]; if ( next === undefined ) { next = new StructuredUniform( id ); addUniform( container, next ); } container = next; } } } // Root Container function WebGLUniforms( gl, program ) { this.seq = []; this.map = {}; const n = gl.getProgramParameter( program, 35718 ); for ( let i = 0; i < n; ++ i ) { const info = gl.getActiveUniform( program, i ), addr = gl.getUniformLocation( program, info.name ); parseUniform( info, addr, this ); } } WebGLUniforms.prototype.setValue = function ( gl, name, value, textures ) { const u = this.map[ name ]; if ( u !== undefined ) u.setValue( gl, value, textures ); }; WebGLUniforms.prototype.setOptional = function ( gl, object, name ) { const v = object[ name ]; if ( v !== undefined ) this.setValue( gl, name, v ); }; // Static interface WebGLUniforms.upload = function ( gl, seq, values, textures ) { for ( let i = 0, n = seq.length; i !== n; ++ i ) { const u = seq[ i ], v = values[ u.id ]; if ( v.needsUpdate !== false ) { // note: always updating when .needsUpdate is undefined u.setValue( gl, v.value, textures ); } } }; WebGLUniforms.seqWithValue = function ( seq, values ) { const r = []; for ( let i = 0, n = seq.length; i !== n; ++ i ) { const u = seq[ i ]; if ( u.id in values ) r.push( u ); } return r; }; function WebGLShader( gl, type, string ) { const shader = gl.createShader( type ); gl.shaderSource( shader, string ); gl.compileShader( shader ); return shader; } let programIdCount = 0; function addLineNumbers( string ) { const lines = string.split( '\n' ); for ( let i = 0; i < lines.length; i ++ ) { lines[ i ] = ( i + 1 ) + ': ' + lines[ i ]; } return lines.join( '\n' ); } function getEncodingComponents( encoding ) { switch ( encoding ) { case LinearEncoding: return [ 'Linear', '( value )' ]; case sRGBEncoding: return [ 'sRGB', '( value )' ]; case RGBEEncoding: return [ 'RGBE', '( value )' ]; case RGBM7Encoding: return [ 'RGBM', '( value, 7.0 )' ]; case RGBM16Encoding: return [ 'RGBM', '( value, 16.0 )' ]; case RGBDEncoding: return [ 'RGBD', '( value, 256.0 )' ]; case GammaEncoding: return [ 'Gamma', '( value, float( GAMMA_FACTOR ) )' ]; case LogLuvEncoding: return [ 'LogLuv', '( value )' ]; default: console.warn( 'THREE.WebGLProgram: Unsupported encoding:', encoding ); return [ 'Linear', '( value )' ]; } } function getShaderErrors( gl, shader, type ) { const status = gl.getShaderParameter( shader, 35713 ); const log = gl.getShaderInfoLog( shader ).trim(); if ( status && log === '' ) return ''; // --enable-privileged-webgl-extension // console.log( '**' + type + '**', gl.getExtension( 'WEBGL_debug_shaders' ).getTranslatedShaderSource( shader ) ); const source = gl.getShaderSource( shader ); return 'THREE.WebGLShader: gl.getShaderInfoLog() ' + type + '\n' + log + addLineNumbers( source ); } function getTexelDecodingFunction( functionName, encoding ) { const components = getEncodingComponents( encoding ); return 'vec4 ' + functionName + '( vec4 value ) { return ' + components[ 0 ] + 'ToLinear' + components[ 1 ] + '; }'; } function getTexelEncodingFunction( functionName, encoding ) { const components = getEncodingComponents( encoding ); return 'vec4 ' + functionName + '( vec4 value ) { return LinearTo' + components[ 0 ] + components[ 1 ] + '; }'; } function getToneMappingFunction( functionName, toneMapping ) { let toneMappingName; switch ( toneMapping ) { case LinearToneMapping: toneMappingName = 'Linear'; break; case ReinhardToneMapping: toneMappingName = 'Reinhard'; break; case CineonToneMapping: toneMappingName = 'OptimizedCineon'; break; case ACESFilmicToneMapping: toneMappingName = 'ACESFilmic'; break; case CustomToneMapping: toneMappingName = 'Custom'; break; default: console.warn( 'THREE.WebGLProgram: Unsupported toneMapping:', toneMapping ); toneMappingName = 'Linear'; } return 'vec3 ' + functionName + '( vec3 color ) { return ' + toneMappingName + 'ToneMapping( color ); }'; } function generateExtensions( parameters ) { const chunks = [ ( parameters.extensionDerivatives || parameters.envMapCubeUV || parameters.bumpMap || parameters.tangentSpaceNormalMap || parameters.clearcoatNormalMap || parameters.flatShading || parameters.shaderID === 'physical' ) ? '#extension GL_OES_standard_derivatives : enable' : '', ( parameters.extensionFragDepth || parameters.logarithmicDepthBuffer ) && parameters.rendererExtensionFragDepth ? '#extension GL_EXT_frag_depth : enable' : '', ( parameters.extensionDrawBuffers && parameters.rendererExtensionDrawBuffers ) ? '#extension GL_EXT_draw_buffers : require' : '', ( parameters.extensionShaderTextureLOD || parameters.envMap ) && parameters.rendererExtensionShaderTextureLod ? '#extension GL_EXT_shader_texture_lod : enable' : '' ]; return chunks.filter( filterEmptyLine ).join( '\n' ); } function generateDefines( defines ) { const chunks = []; for ( const name in defines ) { const value = defines[ name ]; if ( value === false ) continue; chunks.push( '#define ' + name + ' ' + value ); } return chunks.join( '\n' ); } function fetchAttributeLocations( gl, program ) { const attributes = {}; const n = gl.getProgramParameter( program, 35721 ); for ( let i = 0; i < n; i ++ ) { const info = gl.getActiveAttrib( program, i ); const name = info.name; // console.log( 'THREE.WebGLProgram: ACTIVE VERTEX ATTRIBUTE:', name, i ); attributes[ name ] = gl.getAttribLocation( program, name ); } return attributes; } function filterEmptyLine( string ) { return string !== ''; } function replaceLightNums( string, parameters ) { return string .replace( /NUM_DIR_LIGHTS/g, parameters.numDirLights ) .replace( /NUM_SPOT_LIGHTS/g, parameters.numSpotLights ) .replace( /NUM_RECT_AREA_LIGHTS/g, parameters.numRectAreaLights ) .replace( /NUM_POINT_LIGHTS/g, parameters.numPointLights ) .replace( /NUM_HEMI_LIGHTS/g, parameters.numHemiLights ) .replace( /NUM_DIR_LIGHT_SHADOWS/g, parameters.numDirLightShadows ) .replace( /NUM_SPOT_LIGHT_SHADOWS/g, parameters.numSpotLightShadows ) .replace( /NUM_POINT_LIGHT_SHADOWS/g, parameters.numPointLightShadows ); } function replaceClippingPlaneNums( string, parameters ) { return string .replace( /NUM_CLIPPING_PLANES/g, parameters.numClippingPlanes ) .replace( /UNION_CLIPPING_PLANES/g, ( parameters.numClippingPlanes - parameters.numClipIntersection ) ); } // Resolve Includes const includePattern = /^[ \t]*#include +<([\w\d./]+)>/gm; function resolveIncludes( string ) { return string.replace( includePattern, includeReplacer ); } function includeReplacer( match, include ) { const string = ShaderChunk[ include ]; if ( string === undefined ) { throw new Error( 'Can not resolve #include <' + include + '>' ); } return resolveIncludes( string ); } // Unroll Loops const deprecatedUnrollLoopPattern = /#pragma unroll_loop[\s]+?for \( int i \= (\d+)\; i < (\d+)\; i \+\+ \) \{([\s\S]+?)(?=\})\}/g; const unrollLoopPattern = /#pragma unroll_loop_start\s+for\s*\(\s*int\s+i\s*=\s*(\d+)\s*;\s*i\s*<\s*(\d+)\s*;\s*i\s*\+\+\s*\)\s*{([\s\S]+?)}\s+#pragma unroll_loop_end/g; function unrollLoops( string ) { return string .replace( unrollLoopPattern, loopReplacer ) .replace( deprecatedUnrollLoopPattern, deprecatedLoopReplacer ); } function deprecatedLoopReplacer( match, start, end, snippet ) { console.warn( 'WebGLProgram: #pragma unroll_loop shader syntax is deprecated. Please use #pragma unroll_loop_start syntax instead.' ); return loopReplacer( match, start, end, snippet ); } function loopReplacer( match, start, end, snippet ) { let string = ''; for ( let i = parseInt( start ); i < parseInt( end ); i ++ ) { string += snippet .replace( /\[\s*i\s*\]/g, '[ ' + i + ' ]' ) .replace( /UNROLLED_LOOP_INDEX/g, i ); } return string; } // function generatePrecision( parameters ) { let precisionstring = 'precision ' + parameters.precision + ' float;\nprecision ' + parameters.precision + ' int;'; if ( parameters.precision === 'highp' ) { precisionstring += '\n#define HIGH_PRECISION'; } else if ( parameters.precision === 'mediump' ) { precisionstring += '\n#define MEDIUM_PRECISION'; } else if ( parameters.precision === 'lowp' ) { precisionstring += '\n#define LOW_PRECISION'; } return precisionstring; } function generateShadowMapTypeDefine( parameters ) { let shadowMapTypeDefine = 'SHADOWMAP_TYPE_BASIC'; if ( parameters.shadowMapType === PCFShadowMap ) { shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF'; } else if ( parameters.shadowMapType === PCFSoftShadowMap ) { shadowMapTypeDefine = 'SHADOWMAP_TYPE_PCF_SOFT'; } else if ( parameters.shadowMapType === VSMShadowMap ) { shadowMapTypeDefine = 'SHADOWMAP_TYPE_VSM'; } return shadowMapTypeDefine; } function generateEnvMapTypeDefine( parameters ) { let envMapTypeDefine = 'ENVMAP_TYPE_CUBE'; if ( parameters.envMap ) { switch ( parameters.envMapMode ) { case CubeReflectionMapping: case CubeRefractionMapping: envMapTypeDefine = 'ENVMAP_TYPE_CUBE'; break; case CubeUVReflectionMapping: case CubeUVRefractionMapping: envMapTypeDefine = 'ENVMAP_TYPE_CUBE_UV'; break; } } return envMapTypeDefine; } function generateEnvMapModeDefine( parameters ) { let envMapModeDefine = 'ENVMAP_MODE_REFLECTION'; if ( parameters.envMap ) { switch ( parameters.envMapMode ) { case CubeRefractionMapping: case CubeUVRefractionMapping: envMapModeDefine = 'ENVMAP_MODE_REFRACTION'; break; } } return envMapModeDefine; } function generateEnvMapBlendingDefine( parameters ) { let envMapBlendingDefine = 'ENVMAP_BLENDING_NONE'; if ( parameters.envMap ) { switch ( parameters.combine ) { case MultiplyOperation: envMapBlendingDefine = 'ENVMAP_BLENDING_MULTIPLY'; break; case MixOperation: envMapBlendingDefine = 'ENVMAP_BLENDING_MIX'; break; case AddOperation: envMapBlendingDefine = 'ENVMAP_BLENDING_ADD'; break; } } return envMapBlendingDefine; } function WebGLProgram( renderer, cacheKey, parameters, bindingStates ) { const gl = renderer.getContext(); const defines = parameters.defines; let vertexShader = parameters.vertexShader; let fragmentShader = parameters.fragmentShader; const shadowMapTypeDefine = generateShadowMapTypeDefine( parameters ); const envMapTypeDefine = generateEnvMapTypeDefine( parameters ); const envMapModeDefine = generateEnvMapModeDefine( parameters ); const envMapBlendingDefine = generateEnvMapBlendingDefine( parameters ); const gammaFactorDefine = ( renderer.gammaFactor > 0 ) ? renderer.gammaFactor : 1.0; const customExtensions = parameters.isWebGL2 ? '' : generateExtensions( parameters ); const customDefines = generateDefines( defines ); const program = gl.createProgram(); let prefixVertex, prefixFragment; let versionString = parameters.glslVersion ? '#version ' + parameters.glslVersion + '\n' : ''; if ( parameters.isRawShaderMaterial ) { prefixVertex = [ customDefines ].filter( filterEmptyLine ).join( '\n' ); if ( prefixVertex.length > 0 ) { prefixVertex += '\n'; } prefixFragment = [ customExtensions, customDefines ].filter( filterEmptyLine ).join( '\n' ); if ( prefixFragment.length > 0 ) { prefixFragment += '\n'; } } else { prefixVertex = [ generatePrecision( parameters ), '#define SHADER_NAME ' + parameters.shaderName, customDefines, parameters.instancing ? '#define USE_INSTANCING' : '', parameters.instancingColor ? '#define USE_INSTANCING_COLOR' : '', parameters.supportsVertexTextures ? '#define VERTEX_TEXTURES' : '', '#define GAMMA_FACTOR ' + gammaFactorDefine, '#define MAX_BONES ' + parameters.maxBones, ( parameters.useFog && parameters.fog ) ? '#define USE_FOG' : '', ( parameters.useFog && parameters.fogExp2 ) ? '#define FOG_EXP2' : '', parameters.map ? '#define USE_MAP' : '', parameters.envMap ? '#define USE_ENVMAP' : '', parameters.envMap ? '#define ' + envMapModeDefine : '', parameters.lightMap ? '#define USE_LIGHTMAP' : '', parameters.aoMap ? '#define USE_AOMAP' : '', parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '', parameters.bumpMap ? '#define USE_BUMPMAP' : '', parameters.normalMap ? '#define USE_NORMALMAP' : '', ( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '', ( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '', parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '', parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '', parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '', parameters.displacementMap && parameters.supportsVertexTextures ? '#define USE_DISPLACEMENTMAP' : '', parameters.specularMap ? '#define USE_SPECULARMAP' : '', parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '', parameters.metalnessMap ? '#define USE_METALNESSMAP' : '', parameters.alphaMap ? '#define USE_ALPHAMAP' : '', parameters.transmissionMap ? '#define USE_TRANSMISSIONMAP' : '', parameters.vertexTangents ? '#define USE_TANGENT' : '', parameters.vertexColors ? '#define USE_COLOR' : '', parameters.vertexUvs ? '#define USE_UV' : '', parameters.uvsVertexOnly ? '#define UVS_VERTEX_ONLY' : '', parameters.flatShading ? '#define FLAT_SHADED' : '', parameters.skinning ? '#define USE_SKINNING' : '', parameters.useVertexTexture ? '#define BONE_TEXTURE' : '', parameters.morphTargets ? '#define USE_MORPHTARGETS' : '', parameters.morphNormals && parameters.flatShading === false ? '#define USE_MORPHNORMALS' : '', parameters.doubleSided ? '#define DOUBLE_SIDED' : '', parameters.flipSided ? '#define FLIP_SIDED' : '', parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '', parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '', parameters.sizeAttenuation ? '#define USE_SIZEATTENUATION' : '', parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '', ( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '', 'uniform mat4 modelMatrix;', 'uniform mat4 modelViewMatrix;', 'uniform mat4 projectionMatrix;', 'uniform mat4 viewMatrix;', 'uniform mat3 normalMatrix;', 'uniform vec3 cameraPosition;', 'uniform bool isOrthographic;', '#ifdef USE_INSTANCING', ' attribute mat4 instanceMatrix;', '#endif', '#ifdef USE_INSTANCING_COLOR', ' attribute vec3 instanceColor;', '#endif', 'attribute vec3 position;', 'attribute vec3 normal;', 'attribute vec2 uv;', '#ifdef USE_TANGENT', ' attribute vec4 tangent;', '#endif', '#ifdef USE_COLOR', ' attribute vec3 color;', '#endif', '#ifdef USE_MORPHTARGETS', ' attribute vec3 morphTarget0;', ' attribute vec3 morphTarget1;', ' attribute vec3 morphTarget2;', ' attribute vec3 morphTarget3;', ' #ifdef USE_MORPHNORMALS', ' attribute vec3 morphNormal0;', ' attribute vec3 morphNormal1;', ' attribute vec3 morphNormal2;', ' attribute vec3 morphNormal3;', ' #else', ' attribute vec3 morphTarget4;', ' attribute vec3 morphTarget5;', ' attribute vec3 morphTarget6;', ' attribute vec3 morphTarget7;', ' #endif', '#endif', '#ifdef USE_SKINNING', ' attribute vec4 skinIndex;', ' attribute vec4 skinWeight;', '#endif', '\n' ].filter( filterEmptyLine ).join( '\n' ); prefixFragment = [ customExtensions, generatePrecision( parameters ), '#define SHADER_NAME ' + parameters.shaderName, customDefines, parameters.alphaTest ? '#define ALPHATEST ' + parameters.alphaTest + ( parameters.alphaTest % 1 ? '' : '.0' ) : '', // add '.0' if integer '#define GAMMA_FACTOR ' + gammaFactorDefine, ( parameters.useFog && parameters.fog ) ? '#define USE_FOG' : '', ( parameters.useFog && parameters.fogExp2 ) ? '#define FOG_EXP2' : '', parameters.map ? '#define USE_MAP' : '', parameters.matcap ? '#define USE_MATCAP' : '', parameters.envMap ? '#define USE_ENVMAP' : '', parameters.envMap ? '#define ' + envMapTypeDefine : '', parameters.envMap ? '#define ' + envMapModeDefine : '', parameters.envMap ? '#define ' + envMapBlendingDefine : '', parameters.lightMap ? '#define USE_LIGHTMAP' : '', parameters.aoMap ? '#define USE_AOMAP' : '', parameters.emissiveMap ? '#define USE_EMISSIVEMAP' : '', parameters.bumpMap ? '#define USE_BUMPMAP' : '', parameters.normalMap ? '#define USE_NORMALMAP' : '', ( parameters.normalMap && parameters.objectSpaceNormalMap ) ? '#define OBJECTSPACE_NORMALMAP' : '', ( parameters.normalMap && parameters.tangentSpaceNormalMap ) ? '#define TANGENTSPACE_NORMALMAP' : '', parameters.clearcoatMap ? '#define USE_CLEARCOATMAP' : '', parameters.clearcoatRoughnessMap ? '#define USE_CLEARCOAT_ROUGHNESSMAP' : '', parameters.clearcoatNormalMap ? '#define USE_CLEARCOAT_NORMALMAP' : '', parameters.specularMap ? '#define USE_SPECULARMAP' : '', parameters.roughnessMap ? '#define USE_ROUGHNESSMAP' : '', parameters.metalnessMap ? '#define USE_METALNESSMAP' : '', parameters.alphaMap ? '#define USE_ALPHAMAP' : '', parameters.sheen ? '#define USE_SHEEN' : '', parameters.transmissionMap ? '#define USE_TRANSMISSIONMAP' : '', parameters.vertexTangents ? '#define USE_TANGENT' : '', parameters.vertexColors || parameters.instancingColor ? '#define USE_COLOR' : '', parameters.vertexUvs ? '#define USE_UV' : '', parameters.uvsVertexOnly ? '#define UVS_VERTEX_ONLY' : '', parameters.gradientMap ? '#define USE_GRADIENTMAP' : '', parameters.flatShading ? '#define FLAT_SHADED' : '', parameters.doubleSided ? '#define DOUBLE_SIDED' : '', parameters.flipSided ? '#define FLIP_SIDED' : '', parameters.shadowMapEnabled ? '#define USE_SHADOWMAP' : '', parameters.shadowMapEnabled ? '#define ' + shadowMapTypeDefine : '', parameters.premultipliedAlpha ? '#define PREMULTIPLIED_ALPHA' : '', parameters.physicallyCorrectLights ? '#define PHYSICALLY_CORRECT_LIGHTS' : '', parameters.logarithmicDepthBuffer ? '#define USE_LOGDEPTHBUF' : '', ( parameters.logarithmicDepthBuffer && parameters.rendererExtensionFragDepth ) ? '#define USE_LOGDEPTHBUF_EXT' : '', ( ( parameters.extensionShaderTextureLOD || parameters.envMap ) && parameters.rendererExtensionShaderTextureLod ) ? '#define TEXTURE_LOD_EXT' : '', 'uniform mat4 viewMatrix;', 'uniform vec3 cameraPosition;', 'uniform bool isOrthographic;', ( parameters.toneMapping !== NoToneMapping ) ? '#define TONE_MAPPING' : '', ( parameters.toneMapping !== NoToneMapping ) ? ShaderChunk[ 'tonemapping_pars_fragment' ] : '', // this code is required here because it is used by the toneMapping() function defined below ( parameters.toneMapping !== NoToneMapping ) ? getToneMappingFunction( 'toneMapping', parameters.toneMapping ) : '', parameters.dithering ? '#define DITHERING' : '', ShaderChunk[ 'encodings_pars_fragment' ], // this code is required here because it is used by the various encoding/decoding function defined below parameters.map ? getTexelDecodingFunction( 'mapTexelToLinear', parameters.mapEncoding ) : '', parameters.matcap ? getTexelDecodingFunction( 'matcapTexelToLinear', parameters.matcapEncoding ) : '', parameters.envMap ? getTexelDecodingFunction( 'envMapTexelToLinear', parameters.envMapEncoding ) : '', parameters.emissiveMap ? getTexelDecodingFunction( 'emissiveMapTexelToLinear', parameters.emissiveMapEncoding ) : '', parameters.lightMap ? getTexelDecodingFunction( 'lightMapTexelToLinear', parameters.lightMapEncoding ) : '', getTexelEncodingFunction( 'linearToOutputTexel', parameters.outputEncoding ), parameters.depthPacking ? '#define DEPTH_PACKING ' + parameters.depthPacking : '', '\n' ].filter( filterEmptyLine ).join( '\n' ); } vertexShader = resolveIncludes( vertexShader ); vertexShader = replaceLightNums( vertexShader, parameters ); vertexShader = replaceClippingPlaneNums( vertexShader, parameters ); fragmentShader = resolveIncludes( fragmentShader ); fragmentShader = replaceLightNums( fragmentShader, parameters ); fragmentShader = replaceClippingPlaneNums( fragmentShader, parameters ); vertexShader = unrollLoops( vertexShader ); fragmentShader = unrollLoops( fragmentShader ); if ( parameters.isWebGL2 && parameters.isRawShaderMaterial !== true ) { // GLSL 3.0 conversion for built-in materials and ShaderMaterial versionString = '#version 300 es\n'; prefixVertex = [ '#define attribute in', '#define varying out', '#define texture2D texture' ].join( '\n' ) + '\n' + prefixVertex; prefixFragment = [ '#define varying in', ( parameters.glslVersion === GLSL3 ) ? '' : 'out highp vec4 pc_fragColor;', ( parameters.glslVersion === GLSL3 ) ? '' : '#define gl_FragColor pc_fragColor', '#define gl_FragDepthEXT gl_FragDepth', '#define texture2D texture', '#define textureCube texture', '#define texture2DProj textureProj', '#define texture2DLodEXT textureLod', '#define texture2DProjLodEXT textureProjLod', '#define textureCubeLodEXT textureLod', '#define texture2DGradEXT textureGrad', '#define texture2DProjGradEXT textureProjGrad', '#define textureCubeGradEXT textureGrad' ].join( '\n' ) + '\n' + prefixFragment; } const vertexGlsl = versionString + prefixVertex + vertexShader; const fragmentGlsl = versionString + prefixFragment + fragmentShader; // console.log( '*VERTEX*', vertexGlsl ); // console.log( '*FRAGMENT*', fragmentGlsl ); const glVertexShader = WebGLShader( gl, 35633, vertexGlsl ); const glFragmentShader = WebGLShader( gl, 35632, fragmentGlsl ); gl.attachShader( program, glVertexShader ); gl.attachShader( program, glFragmentShader ); // Force a particular attribute to index 0. if ( parameters.index0AttributeName !== undefined ) { gl.bindAttribLocation( program, 0, parameters.index0AttributeName ); } else if ( parameters.morphTargets === true ) { // programs with morphTargets displace position out of attribute 0 gl.bindAttribLocation( program, 0, 'position' ); } gl.linkProgram( program ); // check for link errors if ( renderer.debug.checkShaderErrors ) { const programLog = gl.getProgramInfoLog( program ).trim(); const vertexLog = gl.getShaderInfoLog( glVertexShader ).trim(); const fragmentLog = gl.getShaderInfoLog( glFragmentShader ).trim(); let runnable = true; let haveDiagnostics = true; if ( gl.getProgramParameter( program, 35714 ) === false ) { runnable = false; const vertexErrors = getShaderErrors( gl, glVertexShader, 'vertex' ); const fragmentErrors = getShaderErrors( gl, glFragmentShader, 'fragment' ); console.error( 'THREE.WebGLProgram: shader error: ', gl.getError(), '35715', gl.getProgramParameter( program, 35715 ), 'gl.getProgramInfoLog', programLog, vertexErrors, fragmentErrors ); //add: if(fragmentErrors){ console.log(fragmentGlsl.split("\n").map((a, i) => `${i + 1}`.padEnd(5) + a).join("\n") ); }else { console.log(vertexGlsl.split("\n").map((a, i) => `${i + 1}`.padEnd(5) + a).join("\n") ); } } else if ( programLog !== '' ) { console.warn( 'THREE.WebGLProgram: gl.getProgramInfoLog()', programLog ); } else if ( vertexLog === '' || fragmentLog === '' ) { haveDiagnostics = false; } if ( haveDiagnostics ) { this.diagnostics = { runnable: runnable, programLog: programLog, vertexShader: { log: vertexLog, prefix: prefixVertex }, fragmentShader: { log: fragmentLog, prefix: prefixFragment } }; } } // Clean up // Crashes in iOS9 and iOS10. #18402 // gl.detachShader( program, glVertexShader ); // gl.detachShader( program, glFragmentShader ); gl.deleteShader( glVertexShader ); gl.deleteShader( glFragmentShader ); // set up caching for uniform locations let cachedUniforms; this.getUniforms = function () { if ( cachedUniforms === undefined ) { cachedUniforms = new WebGLUniforms( gl, program ); } return cachedUniforms; }; // set up caching for attribute locations let cachedAttributes; this.getAttributes = function () { if ( cachedAttributes === undefined ) { cachedAttributes = fetchAttributeLocations( gl, program ); } return cachedAttributes; }; // free resource this.destroy = function () { bindingStates.releaseStatesOfProgram( this ); gl.deleteProgram( program ); this.program = undefined; }; // this.name = parameters.shaderName; this.id = programIdCount ++; this.cacheKey = cacheKey; this.usedTimes = 1; this.program = program; this.vertexShader = glVertexShader; this.fragmentShader = glFragmentShader; return this; } function WebGLPrograms( renderer, cubemaps, extensions, capabilities, bindingStates, clipping ) { const programs = []; const isWebGL2 = capabilities.isWebGL2; const logarithmicDepthBuffer = capabilities.logarithmicDepthBuffer; const floatVertexTextures = capabilities.floatVertexTextures; const maxVertexUniforms = capabilities.maxVertexUniforms; const vertexTextures = capabilities.vertexTextures; let precision = capabilities.precision; const shaderIDs = { MeshDepthMaterial: 'depth', MeshDistanceMaterial: 'distanceRGBA', MeshNormalMaterial: 'normal', MeshBasicMaterial: 'basic', MeshLambertMaterial: 'lambert', MeshPhongMaterial: 'phong', MeshToonMaterial: 'toon', MeshStandardMaterial: 'physical', MeshPhysicalMaterial: 'physical', MeshMatcapMaterial: 'matcap', LineBasicMaterial: 'basic', LineDashedMaterial: 'dashed', PointsMaterial: 'points', ShadowMaterial: 'shadow', SpriteMaterial: 'sprite' }; const parameterNames = [ 'precision', 'isWebGL2', 'supportsVertexTextures', 'outputEncoding', 'instancing', 'instancingColor', 'map', 'mapEncoding', 'matcap', 'matcapEncoding', 'envMap', 'envMapMode', 'envMapEncoding', 'envMapCubeUV', 'lightMap', 'lightMapEncoding', 'aoMap', 'emissiveMap', 'emissiveMapEncoding', 'bumpMap', 'normalMap', 'objectSpaceNormalMap', 'tangentSpaceNormalMap', 'clearcoatMap', 'clearcoatRoughnessMap', 'clearcoatNormalMap', 'displacementMap', 'specularMap', 'roughnessMap', 'metalnessMap', 'gradientMap', 'alphaMap', 'combine', 'vertexColors', 'vertexTangents', 'vertexUvs', 'uvsVertexOnly', 'fog', 'useFog', 'fogExp2', 'flatShading', 'sizeAttenuation', 'logarithmicDepthBuffer', 'skinning', 'maxBones', 'useVertexTexture', 'morphTargets', 'morphNormals', 'maxMorphTargets', 'maxMorphNormals', 'premultipliedAlpha', 'numDirLights', 'numPointLights', 'numSpotLights', 'numHemiLights', 'numRectAreaLights', 'numDirLightShadows', 'numPointLightShadows', 'numSpotLightShadows', 'shadowMapEnabled', 'shadowMapType', 'toneMapping', 'physicallyCorrectLights', 'alphaTest', 'doubleSided', 'flipSided', 'numClippingPlanes', 'numClipIntersection', 'depthPacking', 'dithering', 'sheen', 'transmissionMap' ]; function getMaxBones( object ) { const skeleton = object.skeleton; const bones = skeleton.bones; if ( floatVertexTextures ) { return 1024; } else { // default for when object is not specified // ( for example when prebuilding shader to be used with multiple objects ) // // - leave some extra space for other uniforms // - limit here is ANGLE's 254 max uniform vectors // (up to 54 should be safe) const nVertexUniforms = maxVertexUniforms; const nVertexMatrices = Math.floor( ( nVertexUniforms - 20 ) / 4 ); const maxBones = Math.min( nVertexMatrices, bones.length ); if ( maxBones < bones.length ) { console.warn( 'THREE.WebGLRenderer: Skeleton has ' + bones.length + ' bones. This GPU supports ' + maxBones + '.' ); return 0; } return maxBones; } } function getTextureEncodingFromMap( map ) { let encoding; if ( map && map.isTexture ) { encoding = map.encoding; } else if ( map && map.isWebGLRenderTarget ) { console.warn( 'THREE.WebGLPrograms.getTextureEncodingFromMap: don\'t use render targets as textures. Use their .texture property instead.' ); encoding = map.texture.encoding; } else { encoding = LinearEncoding; } return encoding; } function getParameters( material, lights, shadows, scene, object ) { const fog = scene.fog; const environment = material.isMeshStandardMaterial ? scene.environment : null; const envMap = cubemaps.get( material.envMap || environment ); const shaderID = shaderIDs[ material.type ]; // heuristics to create shader parameters according to lights in the scene // (not to blow over maxLights budget) const maxBones = object.isSkinnedMesh ? getMaxBones( object ) : 0; if ( material.precision !== null ) { precision = capabilities.getMaxPrecision( material.precision ); if ( precision !== material.precision ) { console.warn( 'THREE.WebGLProgram.getParameters:', material.precision, 'not supported, using', precision, 'instead.' ); } } let vertexShader, fragmentShader; if ( shaderID ) { const shader = ShaderLib[ shaderID ]; vertexShader = shader.vertexShader; fragmentShader = shader.fragmentShader; } else { vertexShader = material.vertexShader; fragmentShader = material.fragmentShader; } const currentRenderTarget = renderer.getRenderTarget(); const parameters = { isWebGL2: isWebGL2, shaderID: shaderID, shaderName: material.type, vertexShader: vertexShader, fragmentShader: fragmentShader, defines: material.defines, isRawShaderMaterial: material.isRawShaderMaterial === true, glslVersion: material.glslVersion, precision: precision, instancing: object.isInstancedMesh === true, instancingColor: object.isInstancedMesh === true && object.instanceColor !== null, supportsVertexTextures: vertexTextures, outputEncoding: ( currentRenderTarget !== null ) ? getTextureEncodingFromMap( currentRenderTarget.texture ) : renderer.outputEncoding, map: !! material.map, mapEncoding: getTextureEncodingFromMap( material.map ), matcap: !! material.matcap, matcapEncoding: getTextureEncodingFromMap( material.matcap ), envMap: !! envMap, envMapMode: envMap && envMap.mapping, envMapEncoding: getTextureEncodingFromMap( envMap ), envMapCubeUV: ( !! envMap ) && ( ( envMap.mapping === CubeUVReflectionMapping ) || ( envMap.mapping === CubeUVRefractionMapping ) ), lightMap: !! material.lightMap, lightMapEncoding: getTextureEncodingFromMap( material.lightMap ), aoMap: !! material.aoMap, emissiveMap: !! material.emissiveMap, emissiveMapEncoding: getTextureEncodingFromMap( material.emissiveMap ), bumpMap: !! material.bumpMap, normalMap: !! material.normalMap, objectSpaceNormalMap: material.normalMapType === ObjectSpaceNormalMap, tangentSpaceNormalMap: material.normalMapType === TangentSpaceNormalMap, clearcoatMap: !! material.clearcoatMap, clearcoatRoughnessMap: !! material.clearcoatRoughnessMap, clearcoatNormalMap: !! material.clearcoatNormalMap, displacementMap: !! material.displacementMap, roughnessMap: !! material.roughnessMap, metalnessMap: !! material.metalnessMap, specularMap: !! material.specularMap, alphaMap: !! material.alphaMap, gradientMap: !! material.gradientMap, sheen: !! material.sheen, transmissionMap: !! material.transmissionMap, combine: material.combine, vertexTangents: ( material.normalMap && material.vertexTangents ), vertexColors: material.vertexColors, vertexUvs: !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatMap || !! material.clearcoatRoughnessMap || !! material.clearcoatNormalMap || !! material.displacementMap || !! material.transmissionMap, uvsVertexOnly: ! ( !! material.map || !! material.bumpMap || !! material.normalMap || !! material.specularMap || !! material.alphaMap || !! material.emissiveMap || !! material.roughnessMap || !! material.metalnessMap || !! material.clearcoatNormalMap || !! material.transmissionMap ) && !! material.displacementMap, fog: !! fog, useFog: material.fog, fogExp2: ( fog && fog.isFogExp2 ), flatShading: material.flatShading, sizeAttenuation: material.sizeAttenuation, logarithmicDepthBuffer: logarithmicDepthBuffer, skinning: material.skinning && maxBones > 0, maxBones: maxBones, useVertexTexture: floatVertexTextures, morphTargets: material.morphTargets, morphNormals: material.morphNormals, maxMorphTargets: renderer.maxMorphTargets, maxMorphNormals: renderer.maxMorphNormals, numDirLights: lights.directional.length, numPointLights: lights.point.length, numSpotLights: lights.spot.length, numRectAreaLights: lights.rectArea.length, numHemiLights: lights.hemi.length, numDirLightShadows: lights.directionalShadowMap.length, numPointLightShadows: lights.pointShadowMap.length, numSpotLightShadows: lights.spotShadowMap.length, numClippingPlanes: clipping.numPlanes, numClipIntersection: clipping.numIntersection, dithering: material.dithering, shadowMapEnabled: renderer.shadowMap.enabled && shadows.length > 0, shadowMapType: renderer.shadowMap.type, toneMapping: material.toneMapped ? renderer.toneMapping : NoToneMapping, physicallyCorrectLights: renderer.physicallyCorrectLights, premultipliedAlpha: material.premultipliedAlpha, alphaTest: material.alphaTest, doubleSided: material.side === DoubleSide, flipSided: material.side === BackSide, depthPacking: ( material.depthPacking !== undefined ) ? material.depthPacking : false, index0AttributeName: material.index0AttributeName, extensionDerivatives: material.extensions && material.extensions.derivatives, extensionFragDepth: material.extensions && material.extensions.fragDepth, extensionDrawBuffers: material.extensions && material.extensions.drawBuffers, extensionShaderTextureLOD: material.extensions && material.extensions.shaderTextureLOD, rendererExtensionFragDepth: isWebGL2 || extensions.has( 'EXT_frag_depth' ), rendererExtensionDrawBuffers: isWebGL2 || extensions.has( 'WEBGL_draw_buffers' ), rendererExtensionShaderTextureLod: isWebGL2 || extensions.has( 'EXT_shader_texture_lod' ), customProgramCacheKey: material.customProgramCacheKey() }; return parameters; } function getProgramCacheKey( parameters ) { const array = []; if ( parameters.shaderID ) { array.push( parameters.shaderID ); } else { array.push( parameters.fragmentShader ); array.push( parameters.vertexShader ); } if ( parameters.defines !== undefined ) { for ( const name in parameters.defines ) { array.push( name ); array.push( parameters.defines[ name ] ); } } if ( parameters.isRawShaderMaterial === false ) { for ( let i = 0; i < parameterNames.length; i ++ ) { array.push( parameters[ parameterNames[ i ] ] ); } array.push( renderer.outputEncoding ); array.push( renderer.gammaFactor ); } array.push( parameters.customProgramCacheKey ); return array.join(); } function getUniforms( material ) { const shaderID = shaderIDs[ material.type ]; let uniforms; if ( shaderID ) { const shader = ShaderLib[ shaderID ]; uniforms = UniformsUtils.clone( shader.uniforms ); } else { uniforms = material.uniforms; } return uniforms; } function acquireProgram( parameters, cacheKey ) { let program; // Check if code has been already compiled for ( let p = 0, pl = programs.length; p < pl; p ++ ) { const preexistingProgram = programs[ p ]; if ( preexistingProgram.cacheKey === cacheKey ) { program = preexistingProgram; ++ program.usedTimes; break; } } if ( program === undefined ) { program = new WebGLProgram( renderer, cacheKey, parameters, bindingStates ); programs.push( program ); } return program; } function releaseProgram( program ) { if ( -- program.usedTimes === 0 ) { // Remove from unordered set const i = programs.indexOf( program ); programs[ i ] = programs[ programs.length - 1 ]; programs.pop(); // Free WebGL resources program.destroy(); } } return { getParameters: getParameters, getProgramCacheKey: getProgramCacheKey, getUniforms: getUniforms, acquireProgram: acquireProgram, releaseProgram: releaseProgram, // Exposed for resource monitoring & error feedback via renderer.info: programs: programs }; } function WebGLProperties() { let properties = new WeakMap(); function get( object ) { let map = properties.get( object ); if ( map === undefined ) { map = {}; properties.set( object, map ); } return map; } function remove( object ) { properties.delete( object ); } function update( object, key, value ) { properties.get( object )[ key ] = value; } function dispose() { properties = new WeakMap(); } return { get: get, remove: remove, update: update, dispose: dispose }; } function painterSortStable( a, b ) { if ( a.groupOrder !== b.groupOrder ) { return a.groupOrder - b.groupOrder; } else if ( a.renderOrder !== b.renderOrder ) { return a.renderOrder - b.renderOrder; } else if ( a.program !== b.program ) { return a.program.id - b.program.id; } else if ( a.material.id !== b.material.id ) { return a.material.id - b.material.id; } else if ( a.z !== b.z ) { return a.z - b.z; } else { return a.id - b.id; } } function reversePainterSortStable( a, b ) { if ( a.groupOrder !== b.groupOrder ) { return a.groupOrder - b.groupOrder; } else if ( a.renderOrder !== b.renderOrder ) { return a.renderOrder - b.renderOrder; } else if ( a.z !== b.z ) { return b.z - a.z; } else { return a.id - b.id; } } function WebGLRenderList( properties ) { const renderItems = []; let renderItemsIndex = 0; const opaque = []; const transparent = []; const defaultProgram = { id: - 1 }; function init() { renderItemsIndex = 0; opaque.length = 0; transparent.length = 0; } function getNextRenderItem( object, geometry, material, groupOrder, z, group ) { let renderItem = renderItems[ renderItemsIndex ]; const materialProperties = properties.get( material ); if ( renderItem === undefined ) { renderItem = { id: object.id, object: object, geometry: geometry, material: material, program: materialProperties.program || defaultProgram, groupOrder: groupOrder, renderOrder: object.renderOrder, z: z, group: group }; renderItems[ renderItemsIndex ] = renderItem; } else { renderItem.id = object.id; renderItem.object = object; renderItem.geometry = geometry; renderItem.material = material; renderItem.program = materialProperties.program || defaultProgram; renderItem.groupOrder = groupOrder; renderItem.renderOrder = object.renderOrder; renderItem.z = z; renderItem.group = group; } renderItemsIndex ++; return renderItem; } function push( object, geometry, material, groupOrder, z, group ) { const renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group ); ( material.transparent === true ? transparent : opaque ).push( renderItem ); } function unshift( object, geometry, material, groupOrder, z, group ) { const renderItem = getNextRenderItem( object, geometry, material, groupOrder, z, group ); ( material.transparent === true ? transparent : opaque ).unshift( renderItem ); } function sort( customOpaqueSort, customTransparentSort ) { if ( opaque.length > 1 ) opaque.sort( customOpaqueSort || painterSortStable ); if ( transparent.length > 1 ) transparent.sort( customTransparentSort || reversePainterSortStable ); } function finish() { // Clear references from inactive renderItems in the list for ( let i = renderItemsIndex, il = renderItems.length; i < il; i ++ ) { const renderItem = renderItems[ i ]; if ( renderItem.id === null ) break; renderItem.id = null; renderItem.object = null; renderItem.geometry = null; renderItem.material = null; renderItem.program = null; renderItem.group = null; } } return { opaque: opaque, transparent: transparent, init: init, push: push, unshift: unshift, finish: finish, sort: sort }; } function WebGLRenderLists( properties ) { let lists = new WeakMap(); function get( scene, camera ) { const cameras = lists.get( scene ); let list; if ( cameras === undefined ) { list = new WebGLRenderList( properties ); lists.set( scene, new WeakMap() ); lists.get( scene ).set( camera, list ); } else { list = cameras.get( camera ); if ( list === undefined ) { list = new WebGLRenderList( properties ); cameras.set( camera, list ); } } return list; } function dispose() { lists = new WeakMap(); } return { get: get, dispose: dispose }; } function UniformsCache() { const lights = {}; return { get: function ( light ) { if ( lights[ light.id ] !== undefined ) { return lights[ light.id ]; } let uniforms; switch ( light.type ) { case 'DirectionalLight': uniforms = { direction: new Vector3(), color: new Color() }; break; case 'SpotLight': uniforms = { position: new Vector3(), direction: new Vector3(), color: new Color(), distance: 0, coneCos: 0, penumbraCos: 0, decay: 0 }; break; case 'PointLight': uniforms = { position: new Vector3(), color: new Color(), distance: 0, decay: 0 }; break; case 'HemisphereLight': uniforms = { direction: new Vector3(), skyColor: new Color(), groundColor: new Color() }; break; case 'RectAreaLight': uniforms = { color: new Color(), position: new Vector3(), halfWidth: new Vector3(), halfHeight: new Vector3() }; break; } lights[ light.id ] = uniforms; return uniforms; } }; } function ShadowUniformsCache() { const lights = {}; return { get: function ( light ) { if ( lights[ light.id ] !== undefined ) { return lights[ light.id ]; } let uniforms; switch ( light.type ) { case 'DirectionalLight': uniforms = { shadowBias: 0, shadowNormalBias: 0, shadowRadius: 1, shadowMapSize: new Vector2() }; break; case 'SpotLight': uniforms = { shadowBias: 0, shadowNormalBias: 0, shadowRadius: 1, shadowMapSize: new Vector2() }; break; case 'PointLight': uniforms = { shadowBias: 0, shadowNormalBias: 0, shadowRadius: 1, shadowMapSize: new Vector2(), shadowCameraNear: 1, shadowCameraFar: 1000 }; break; // TODO (abelnation): set RectAreaLight shadow uniforms } lights[ light.id ] = uniforms; return uniforms; } }; } let nextVersion = 0; function shadowCastingLightsFirst( lightA, lightB ) { return ( lightB.castShadow ? 1 : 0 ) - ( lightA.castShadow ? 1 : 0 ); } function WebGLLights( extensions, capabilities ) { const cache = new UniformsCache(); const shadowCache = ShadowUniformsCache(); const state = { version: 0, hash: { directionalLength: - 1, pointLength: - 1, spotLength: - 1, rectAreaLength: - 1, hemiLength: - 1, numDirectionalShadows: - 1, numPointShadows: - 1, numSpotShadows: - 1 }, ambient: [ 0, 0, 0 ], probe: [], directional: [], directionalShadow: [], directionalShadowMap: [], directionalShadowMatrix: [], spot: [], spotShadow: [], spotShadowMap: [], spotShadowMatrix: [], rectArea: [], rectAreaLTC1: null, rectAreaLTC2: null, point: [], pointShadow: [], pointShadowMap: [], pointShadowMatrix: [], hemi: [] }; for ( let i = 0; i < 9; i ++ ) state.probe.push( new Vector3() ); const vector3 = new Vector3(); const matrix4 = new Matrix4(); const matrix42 = new Matrix4(); function setup( lights ) { let r = 0, g = 0, b = 0; for ( let i = 0; i < 9; i ++ ) state.probe[ i ].set( 0, 0, 0 ); let directionalLength = 0; let pointLength = 0; let spotLength = 0; let rectAreaLength = 0; let hemiLength = 0; let numDirectionalShadows = 0; let numPointShadows = 0; let numSpotShadows = 0; lights.sort( shadowCastingLightsFirst ); for ( let i = 0, l = lights.length; i < l; i ++ ) { const light = lights[ i ]; const color = light.color; const intensity = light.intensity; const distance = light.distance; const shadowMap = ( light.shadow && light.shadow.map ) ? light.shadow.map.texture : null; if ( light.isAmbientLight ) { r += color.r * intensity; g += color.g * intensity; b += color.b * intensity; } else if ( light.isLightProbe ) { for ( let j = 0; j < 9; j ++ ) { state.probe[ j ].addScaledVector( light.sh.coefficients[ j ], intensity ); } } else if ( light.isDirectionalLight ) { const uniforms = cache.get( light ); uniforms.color.copy( light.color ).multiplyScalar( light.intensity ); if ( light.castShadow ) { const shadow = light.shadow; const shadowUniforms = shadowCache.get( light ); shadowUniforms.shadowBias = shadow.bias; shadowUniforms.shadowNormalBias = shadow.normalBias; shadowUniforms.shadowRadius = shadow.radius; shadowUniforms.shadowMapSize = shadow.mapSize; state.directionalShadow[ directionalLength ] = shadowUniforms; state.directionalShadowMap[ directionalLength ] = shadowMap; state.directionalShadowMatrix[ directionalLength ] = light.shadow.matrix; numDirectionalShadows ++; } state.directional[ directionalLength ] = uniforms; directionalLength ++; } else if ( light.isSpotLight ) { const uniforms = cache.get( light ); uniforms.position.setFromMatrixPosition( light.matrixWorld ); uniforms.color.copy( color ).multiplyScalar( intensity ); uniforms.distance = distance; uniforms.coneCos = Math.cos( light.angle ); uniforms.penumbraCos = Math.cos( light.angle * ( 1 - light.penumbra ) ); uniforms.decay = light.decay; if ( light.castShadow ) { const shadow = light.shadow; const shadowUniforms = shadowCache.get( light ); shadowUniforms.shadowBias = shadow.bias; shadowUniforms.shadowNormalBias = shadow.normalBias; shadowUniforms.shadowRadius = shadow.radius; shadowUniforms.shadowMapSize = shadow.mapSize; state.spotShadow[ spotLength ] = shadowUniforms; state.spotShadowMap[ spotLength ] = shadowMap; state.spotShadowMatrix[ spotLength ] = light.shadow.matrix; numSpotShadows ++; } state.spot[ spotLength ] = uniforms; spotLength ++; } else if ( light.isRectAreaLight ) { const uniforms = cache.get( light ); // (a) intensity is the total visible light emitted //uniforms.color.copy( color ).multiplyScalar( intensity / ( light.width * light.height * Math.PI ) ); // (b) intensity is the brightness of the light uniforms.color.copy( color ).multiplyScalar( intensity ); uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 ); uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 ); state.rectArea[ rectAreaLength ] = uniforms; rectAreaLength ++; } else if ( light.isPointLight ) { const uniforms = cache.get( light ); uniforms.color.copy( light.color ).multiplyScalar( light.intensity ); uniforms.distance = light.distance; uniforms.decay = light.decay; if ( light.castShadow ) { const shadow = light.shadow; const shadowUniforms = shadowCache.get( light ); shadowUniforms.shadowBias = shadow.bias; shadowUniforms.shadowNormalBias = shadow.normalBias; shadowUniforms.shadowRadius = shadow.radius; shadowUniforms.shadowMapSize = shadow.mapSize; shadowUniforms.shadowCameraNear = shadow.camera.near; shadowUniforms.shadowCameraFar = shadow.camera.far; state.pointShadow[ pointLength ] = shadowUniforms; state.pointShadowMap[ pointLength ] = shadowMap; state.pointShadowMatrix[ pointLength ] = light.shadow.matrix; numPointShadows ++; } state.point[ pointLength ] = uniforms; pointLength ++; } else if ( light.isHemisphereLight ) { const uniforms = cache.get( light ); uniforms.skyColor.copy( light.color ).multiplyScalar( intensity ); uniforms.groundColor.copy( light.groundColor ).multiplyScalar( intensity ); state.hemi[ hemiLength ] = uniforms; hemiLength ++; } } if ( rectAreaLength > 0 ) { if ( capabilities.isWebGL2 ) { // WebGL 2 state.rectAreaLTC1 = UniformsLib.LTC_FLOAT_1; state.rectAreaLTC2 = UniformsLib.LTC_FLOAT_2; } else { // WebGL 1 if ( extensions.has( 'OES_texture_float_linear' ) === true ) { state.rectAreaLTC1 = UniformsLib.LTC_FLOAT_1; state.rectAreaLTC2 = UniformsLib.LTC_FLOAT_2; } else if ( extensions.has( 'OES_texture_half_float_linear' ) === true ) { state.rectAreaLTC1 = UniformsLib.LTC_HALF_1; state.rectAreaLTC2 = UniformsLib.LTC_HALF_2; } else { console.error( 'THREE.WebGLRenderer: Unable to use RectAreaLight. Missing WebGL extensions.' ); } } } state.ambient[ 0 ] = r; state.ambient[ 1 ] = g; state.ambient[ 2 ] = b; const hash = state.hash; if ( hash.directionalLength !== directionalLength || hash.pointLength !== pointLength || hash.spotLength !== spotLength || hash.rectAreaLength !== rectAreaLength || hash.hemiLength !== hemiLength || hash.numDirectionalShadows !== numDirectionalShadows || hash.numPointShadows !== numPointShadows || hash.numSpotShadows !== numSpotShadows ) { state.directional.length = directionalLength; state.spot.length = spotLength; state.rectArea.length = rectAreaLength; state.point.length = pointLength; state.hemi.length = hemiLength; state.directionalShadow.length = numDirectionalShadows; state.directionalShadowMap.length = numDirectionalShadows; state.pointShadow.length = numPointShadows; state.pointShadowMap.length = numPointShadows; state.spotShadow.length = numSpotShadows; state.spotShadowMap.length = numSpotShadows; state.directionalShadowMatrix.length = numDirectionalShadows; state.pointShadowMatrix.length = numPointShadows; state.spotShadowMatrix.length = numSpotShadows; hash.directionalLength = directionalLength; hash.pointLength = pointLength; hash.spotLength = spotLength; hash.rectAreaLength = rectAreaLength; hash.hemiLength = hemiLength; hash.numDirectionalShadows = numDirectionalShadows; hash.numPointShadows = numPointShadows; hash.numSpotShadows = numSpotShadows; state.version = nextVersion ++; } } function setupView( lights, camera ) { let directionalLength = 0; let pointLength = 0; let spotLength = 0; let rectAreaLength = 0; let hemiLength = 0; const viewMatrix = camera.matrixWorldInverse; for ( let i = 0, l = lights.length; i < l; i ++ ) { const light = lights[ i ]; if ( light.isDirectionalLight ) { const uniforms = state.directional[ directionalLength ]; uniforms.direction.setFromMatrixPosition( light.matrixWorld ); vector3.setFromMatrixPosition( light.target.matrixWorld ); uniforms.direction.sub( vector3 ); uniforms.direction.transformDirection( viewMatrix ); directionalLength ++; } else if ( light.isSpotLight ) { const uniforms = state.spot[ spotLength ]; uniforms.position.setFromMatrixPosition( light.matrixWorld ); uniforms.position.applyMatrix4( viewMatrix ); uniforms.direction.setFromMatrixPosition( light.matrixWorld ); vector3.setFromMatrixPosition( light.target.matrixWorld ); uniforms.direction.sub( vector3 ); uniforms.direction.transformDirection( viewMatrix ); spotLength ++; } else if ( light.isRectAreaLight ) { const uniforms = state.rectArea[ rectAreaLength ]; uniforms.position.setFromMatrixPosition( light.matrixWorld ); uniforms.position.applyMatrix4( viewMatrix ); // extract local rotation of light to derive width/height half vectors matrix42.identity(); matrix4.copy( light.matrixWorld ); matrix4.premultiply( viewMatrix ); matrix42.extractRotation( matrix4 ); uniforms.halfWidth.set( light.width * 0.5, 0.0, 0.0 ); uniforms.halfHeight.set( 0.0, light.height * 0.5, 0.0 ); uniforms.halfWidth.applyMatrix4( matrix42 ); uniforms.halfHeight.applyMatrix4( matrix42 ); rectAreaLength ++; } else if ( light.isPointLight ) { const uniforms = state.point[ pointLength ]; uniforms.position.setFromMatrixPosition( light.matrixWorld ); uniforms.position.applyMatrix4( viewMatrix ); pointLength ++; } else if ( light.isHemisphereLight ) { const uniforms = state.hemi[ hemiLength ]; uniforms.direction.setFromMatrixPosition( light.matrixWorld ); uniforms.direction.transformDirection( viewMatrix ); uniforms.direction.normalize(); hemiLength ++; } } } return { setup: setup, setupView: setupView, state: state }; } function WebGLRenderState( extensions, capabilities ) { const lights = new WebGLLights( extensions, capabilities ); const lightsArray = []; const shadowsArray = []; function init() { lightsArray.length = 0; shadowsArray.length = 0; } function pushLight( light ) { lightsArray.push( light ); } function pushShadow( shadowLight ) { shadowsArray.push( shadowLight ); } function setupLights() { lights.setup( lightsArray ); } function setupLightsView( camera ) { lights.setupView( lightsArray, camera ); } const state = { lightsArray: lightsArray, shadowsArray: shadowsArray, lights: lights }; return { init: init, state: state, setupLights: setupLights, setupLightsView: setupLightsView, pushLight: pushLight, pushShadow: pushShadow }; } function WebGLRenderStates( extensions, capabilities ) { let renderStates = new WeakMap(); function get( scene, renderCallDepth = 0 ) { let renderState; if ( renderStates.has( scene ) === false ) { renderState = new WebGLRenderState( extensions, capabilities ); renderStates.set( scene, [] ); renderStates.get( scene ).push( renderState ); } else { if ( renderCallDepth >= renderStates.get( scene ).length ) { renderState = new WebGLRenderState( extensions, capabilities ); renderStates.get( scene ).push( renderState ); } else { renderState = renderStates.get( scene )[ renderCallDepth ]; } } return renderState; } function dispose() { renderStates = new WeakMap(); } return { get: get, dispose: dispose }; } /** * parameters = { * * opacity: , * * map: new THREE.Texture( ), * * alphaMap: new THREE.Texture( ), * * displacementMap: new THREE.Texture( ), * displacementScale: , * displacementBias: , * * wireframe: , * wireframeLinewidth: * } */ function MeshDepthMaterial( parameters ) { Material.call( this ); this.type = 'MeshDepthMaterial'; this.depthPacking = BasicDepthPacking; this.skinning = false; this.morphTargets = false; this.map = null; this.alphaMap = null; this.displacementMap = null; this.displacementScale = 1; this.displacementBias = 0; this.wireframe = false; this.wireframeLinewidth = 1; this.fog = false; this.setValues( parameters ); } MeshDepthMaterial.prototype = Object.create( Material.prototype ); MeshDepthMaterial.prototype.constructor = MeshDepthMaterial; MeshDepthMaterial.prototype.isMeshDepthMaterial = true; MeshDepthMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.depthPacking = source.depthPacking; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.map = source.map; this.alphaMap = source.alphaMap; this.displacementMap = source.displacementMap; this.displacementScale = source.displacementScale; this.displacementBias = source.displacementBias; this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; return this; }; /** * parameters = { * * referencePosition: , * nearDistance: , * farDistance: , * * skinning: , * morphTargets: , * * map: new THREE.Texture( ), * * alphaMap: new THREE.Texture( ), * * displacementMap: new THREE.Texture( ), * displacementScale: , * displacementBias: * * } */ function MeshDistanceMaterial( parameters ) { Material.call( this ); this.type = 'MeshDistanceMaterial'; this.referencePosition = new Vector3(); this.nearDistance = 1; this.farDistance = 1000; this.skinning = false; this.morphTargets = false; this.map = null; this.alphaMap = null; this.displacementMap = null; this.displacementScale = 1; this.displacementBias = 0; this.fog = false; this.setValues( parameters ); } MeshDistanceMaterial.prototype = Object.create( Material.prototype ); MeshDistanceMaterial.prototype.constructor = MeshDistanceMaterial; MeshDistanceMaterial.prototype.isMeshDistanceMaterial = true; MeshDistanceMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.referencePosition.copy( source.referencePosition ); this.nearDistance = source.nearDistance; this.farDistance = source.farDistance; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.map = source.map; this.alphaMap = source.alphaMap; this.displacementMap = source.displacementMap; this.displacementScale = source.displacementScale; this.displacementBias = source.displacementBias; return this; }; var vsm_frag = "uniform sampler2D shadow_pass;\nuniform vec2 resolution;\nuniform float radius;\n#include \nvoid main() {\n\tfloat mean = 0.0;\n\tfloat squared_mean = 0.0;\n\tfloat depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy ) / resolution ) );\n\tfor ( float i = -1.0; i < 1.0 ; i += SAMPLE_RATE) {\n\t\t#ifdef HORIZONTAL_PASS\n\t\t\tvec2 distribution = unpackRGBATo2Half( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( i, 0.0 ) * radius ) / resolution ) );\n\t\t\tmean += distribution.x;\n\t\t\tsquared_mean += distribution.y * distribution.y + distribution.x * distribution.x;\n\t\t#else\n\t\t\tfloat depth = unpackRGBAToDepth( texture2D( shadow_pass, ( gl_FragCoord.xy + vec2( 0.0, i ) * radius ) / resolution ) );\n\t\t\tmean += depth;\n\t\t\tsquared_mean += depth * depth;\n\t\t#endif\n\t}\n\tmean = mean * HALF_SAMPLE_RATE;\n\tsquared_mean = squared_mean * HALF_SAMPLE_RATE;\n\tfloat std_dev = sqrt( squared_mean - mean * mean );\n\tgl_FragColor = pack2HalfToRGBA( vec2( mean, std_dev ) );\n}"; var vsm_vert = "void main() {\n\tgl_Position = vec4( position, 1.0 );\n}"; function WebGLShadowMap( _renderer, _objects, maxTextureSize ) { let _frustum = new Frustum(); const _shadowMapSize = new Vector2(), _viewportSize = new Vector2(), _viewport = new Vector4(), _depthMaterials = [], _distanceMaterials = [], _materialCache = {}; const shadowSide = { 0: BackSide, 1: FrontSide, 2: DoubleSide }; const shadowMaterialVertical = new ShaderMaterial( { defines: { SAMPLE_RATE: 2.0 / 8.0, HALF_SAMPLE_RATE: 1.0 / 8.0 }, uniforms: { shadow_pass: { value: null }, resolution: { value: new Vector2() }, radius: { value: 4.0 } }, vertexShader: vsm_vert, fragmentShader: vsm_frag } ); const shadowMaterialHorizontal = shadowMaterialVertical.clone(); shadowMaterialHorizontal.defines.HORIZONTAL_PASS = 1; const fullScreenTri = new BufferGeometry(); fullScreenTri.setAttribute( 'position', new BufferAttribute( new Float32Array( [ - 1, - 1, 0.5, 3, - 1, 0.5, - 1, 3, 0.5 ] ), 3 ) ); const fullScreenMesh = new Mesh( fullScreenTri, shadowMaterialVertical ); const scope = this; this.enabled = false; this.autoUpdate = true; this.needsUpdate = false; this.type = PCFShadowMap; this.render = function ( lights, scene, camera ) { if ( scope.enabled === false ) return; if ( scope.autoUpdate === false && scope.needsUpdate === false ) return; if ( lights.length === 0 ) return; const currentRenderTarget = _renderer.getRenderTarget(); const activeCubeFace = _renderer.getActiveCubeFace(); const activeMipmapLevel = _renderer.getActiveMipmapLevel(); const _state = _renderer.state; // Set GL state for depth map. _state.setBlending( NoBlending ); _state.buffers.color.setClear( 1, 1, 1, 1 ); _state.buffers.depth.setTest( true ); _state.setScissorTest( false ); // render depth map for ( let i = 0, il = lights.length; i < il; i ++ ) { const light = lights[ i ]; const shadow = light.shadow; if ( shadow === undefined ) { console.warn( 'THREE.WebGLShadowMap:', light, 'has no shadow.' ); continue; } if ( shadow.autoUpdate === false && shadow.needsUpdate === false ) continue; _shadowMapSize.copy( shadow.mapSize ); const shadowFrameExtents = shadow.getFrameExtents(); _shadowMapSize.multiply( shadowFrameExtents ); _viewportSize.copy( shadow.mapSize ); if ( _shadowMapSize.x > maxTextureSize || _shadowMapSize.y > maxTextureSize ) { if ( _shadowMapSize.x > maxTextureSize ) { _viewportSize.x = Math.floor( maxTextureSize / shadowFrameExtents.x ); _shadowMapSize.x = _viewportSize.x * shadowFrameExtents.x; shadow.mapSize.x = _viewportSize.x; } if ( _shadowMapSize.y > maxTextureSize ) { _viewportSize.y = Math.floor( maxTextureSize / shadowFrameExtents.y ); _shadowMapSize.y = _viewportSize.y * shadowFrameExtents.y; shadow.mapSize.y = _viewportSize.y; } } if ( shadow.map === null && ! shadow.isPointLightShadow && this.type === VSMShadowMap ) { const pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat }; shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars ); shadow.map.texture.name = light.name + '.shadowMap'; shadow.mapPass = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars ); shadow.camera.updateProjectionMatrix(); } if ( shadow.map === null ) { const pars = { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat }; shadow.map = new WebGLRenderTarget( _shadowMapSize.x, _shadowMapSize.y, pars ); shadow.map.texture.name = light.name + '.shadowMap'; shadow.camera.updateProjectionMatrix(); } _renderer.setRenderTarget( shadow.map ); _renderer.clear(); const viewportCount = shadow.getViewportCount(); for ( let vp = 0; vp < viewportCount; vp ++ ) { const viewport = shadow.getViewport( vp ); _viewport.set( _viewportSize.x * viewport.x, _viewportSize.y * viewport.y, _viewportSize.x * viewport.z, _viewportSize.y * viewport.w ); _state.viewport( _viewport ); shadow.updateMatrices( light, vp ); _frustum = shadow.getFrustum(); renderObject( scene, camera, shadow.camera, light, this.type ); } // do blur pass for VSM if ( ! shadow.isPointLightShadow && this.type === VSMShadowMap ) { VSMPass( shadow, camera ); } shadow.needsUpdate = false; } scope.needsUpdate = false; _renderer.setRenderTarget( currentRenderTarget, activeCubeFace, activeMipmapLevel ); }; function VSMPass( shadow, camera ) { const geometry = _objects.update( fullScreenMesh ); // vertical pass shadowMaterialVertical.uniforms.shadow_pass.value = shadow.map.texture; shadowMaterialVertical.uniforms.resolution.value = shadow.mapSize; shadowMaterialVertical.uniforms.radius.value = shadow.radius; _renderer.setRenderTarget( shadow.mapPass ); _renderer.clear(); _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialVertical, fullScreenMesh, null ); // horizontal pass shadowMaterialHorizontal.uniforms.shadow_pass.value = shadow.mapPass.texture; shadowMaterialHorizontal.uniforms.resolution.value = shadow.mapSize; shadowMaterialHorizontal.uniforms.radius.value = shadow.radius; _renderer.setRenderTarget( shadow.map ); _renderer.clear(); _renderer.renderBufferDirect( camera, null, geometry, shadowMaterialHorizontal, fullScreenMesh, null ); } function getDepthMaterialVariant( useMorphing, useSkinning, useInstancing ) { const index = useMorphing << 0 | useSkinning << 1 | useInstancing << 2; let material = _depthMaterials[ index ]; if ( material === undefined ) { material = new MeshDepthMaterial( { depthPacking: RGBADepthPacking, morphTargets: useMorphing, skinning: useSkinning } ); _depthMaterials[ index ] = material; } return material; } function getDistanceMaterialVariant( useMorphing, useSkinning, useInstancing ) { const index = useMorphing << 0 | useSkinning << 1 | useInstancing << 2; let material = _distanceMaterials[ index ]; if ( material === undefined ) { material = new MeshDistanceMaterial( { morphTargets: useMorphing, skinning: useSkinning } ); _distanceMaterials[ index ] = material; } return material; } function getDepthMaterial( object, geometry, material, light, shadowCameraNear, shadowCameraFar, type ) { let result = null; let getMaterialVariant = getDepthMaterialVariant; let customMaterial = object.customDepthMaterial; if ( light.isPointLight === true ) { getMaterialVariant = getDistanceMaterialVariant; customMaterial = object.customDistanceMaterial; } if ( customMaterial === undefined ) { let useMorphing = false; if ( material.morphTargets === true ) { useMorphing = geometry.morphAttributes && geometry.morphAttributes.position && geometry.morphAttributes.position.length > 0; } let useSkinning = false; if ( object.isSkinnedMesh === true ) { if ( material.skinning === true ) { useSkinning = true; } else { console.warn( 'THREE.WebGLShadowMap: THREE.SkinnedMesh with material.skinning set to false:', object ); } } const useInstancing = object.isInstancedMesh === true; result = getMaterialVariant( useMorphing, useSkinning, useInstancing ); } else { result = customMaterial; } if ( _renderer.localClippingEnabled && material.clipShadows === true && material.clippingPlanes.length !== 0 ) { // in this case we need a unique material instance reflecting the // appropriate state const keyA = result.uuid, keyB = material.uuid; let materialsForVariant = _materialCache[ keyA ]; if ( materialsForVariant === undefined ) { materialsForVariant = {}; _materialCache[ keyA ] = materialsForVariant; } let cachedMaterial = materialsForVariant[ keyB ]; if ( cachedMaterial === undefined ) { cachedMaterial = result.clone(); materialsForVariant[ keyB ] = cachedMaterial; } result = cachedMaterial; } result.visible = material.visible; result.wireframe = material.wireframe; if ( type === VSMShadowMap ) { result.side = ( material.shadowSide !== null ) ? material.shadowSide : material.side; } else { result.side = ( material.shadowSide !== null ) ? material.shadowSide : shadowSide[ material.side ]; } result.clipShadows = material.clipShadows; result.clippingPlanes = material.clippingPlanes; result.clipIntersection = material.clipIntersection; result.wireframeLinewidth = material.wireframeLinewidth; result.linewidth = material.linewidth; if ( light.isPointLight === true && result.isMeshDistanceMaterial === true ) { result.referencePosition.setFromMatrixPosition( light.matrixWorld ); result.nearDistance = shadowCameraNear; result.farDistance = shadowCameraFar; } return result; } function renderObject( object, camera, shadowCamera, light, type ) { if ( object.visible === false ) return; const visible = object.layers.test( camera.layers ); if ( visible && ( object.isMesh || object.isLine || object.isPoints ) ) { if ( ( object.castShadow || ( object.receiveShadow && type === VSMShadowMap ) ) && ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) ) { object.modelViewMatrix.multiplyMatrices( shadowCamera.matrixWorldInverse, object.matrixWorld ); const geometry = _objects.update( object ); const material = object.material; if ( Array.isArray( material ) ) { const groups = geometry.groups; for ( let k = 0, kl = groups.length; k < kl; k ++ ) { const group = groups[ k ]; const groupMaterial = material[ group.materialIndex ]; if ( groupMaterial && groupMaterial.visible ) { const depthMaterial = getDepthMaterial( object, geometry, groupMaterial, light, shadowCamera.near, shadowCamera.far, type ); _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, group ); } } } else if ( material.visible ) { const depthMaterial = getDepthMaterial( object, geometry, material, light, shadowCamera.near, shadowCamera.far, type ); _renderer.renderBufferDirect( shadowCamera, null, geometry, depthMaterial, object, null ); } } } const children = object.children; for ( let i = 0, l = children.length; i < l; i ++ ) { renderObject( children[ i ], camera, shadowCamera, light, type ); } } } function WebGLState( gl, extensions, capabilities ) { const isWebGL2 = capabilities.isWebGL2; function ColorBuffer() { let locked = false; const color = new Vector4(); let currentColorMask = null; const currentColorClear = new Vector4( 0, 0, 0, 0 ); return { setMask: function ( colorMask ) { if ( currentColorMask !== colorMask && ! locked ) { gl.colorMask( colorMask, colorMask, colorMask, colorMask ); currentColorMask = colorMask; } }, setLocked: function ( lock ) { locked = lock; }, setClear: function ( r, g, b, a, premultipliedAlpha ) { if ( premultipliedAlpha === true ) { r *= a; g *= a; b *= a; } color.set( r, g, b, a ); if ( currentColorClear.equals( color ) === false ) { gl.clearColor( r, g, b, a ); currentColorClear.copy( color ); } }, reset: function () { locked = false; currentColorMask = null; currentColorClear.set( - 1, 0, 0, 0 ); // set to invalid state } }; } function DepthBuffer() { let locked = false; let currentDepthMask = null; let currentDepthFunc = null; let currentDepthClear = null; return { setTest: function ( depthTest ) { if ( depthTest ) { enable( 2929 ); } else { disable( 2929 ); } }, setMask: function ( depthMask ) { if ( currentDepthMask !== depthMask && ! locked ) { gl.depthMask( depthMask ); currentDepthMask = depthMask; } }, setFunc: function ( depthFunc ) { if ( currentDepthFunc !== depthFunc ) { if ( depthFunc ) { switch ( depthFunc ) { case NeverDepth: gl.depthFunc( 512 ); break; case AlwaysDepth: gl.depthFunc( 519 ); break; case LessDepth: gl.depthFunc( 513 ); break; case LessEqualDepth: gl.depthFunc( 515 ); break; case EqualDepth: gl.depthFunc( 514 ); break; case GreaterEqualDepth: gl.depthFunc( 518 ); break; case GreaterDepth: gl.depthFunc( 516 ); break; case NotEqualDepth: gl.depthFunc( 517 ); break; default: gl.depthFunc( 515 ); } } else { gl.depthFunc( 515 ); } currentDepthFunc = depthFunc; } }, setLocked: function ( lock ) { locked = lock; }, setClear: function ( depth ) { if ( currentDepthClear !== depth ) { gl.clearDepth( depth ); currentDepthClear = depth; } }, reset: function () { locked = false; currentDepthMask = null; currentDepthFunc = null; currentDepthClear = null; } }; } function StencilBuffer() { let locked = false; let currentStencilMask = null; let currentStencilFunc = null; let currentStencilRef = null; let currentStencilFuncMask = null; let currentStencilFail = null; let currentStencilZFail = null; let currentStencilZPass = null; let currentStencilClear = null; return { setTest: function ( stencilTest ) { if ( ! locked ) { if ( stencilTest ) { enable( 2960 ); } else { disable( 2960 ); } } }, setMask: function ( stencilMask ) { if ( currentStencilMask !== stencilMask && ! locked ) { gl.stencilMask( stencilMask ); currentStencilMask = stencilMask; } }, setFunc: function ( stencilFunc, stencilRef, stencilMask ) { if ( currentStencilFunc !== stencilFunc || currentStencilRef !== stencilRef || currentStencilFuncMask !== stencilMask ) { gl.stencilFunc( stencilFunc, stencilRef, stencilMask ); currentStencilFunc = stencilFunc; currentStencilRef = stencilRef; currentStencilFuncMask = stencilMask; } }, setOp: function ( stencilFail, stencilZFail, stencilZPass ) { if ( currentStencilFail !== stencilFail || currentStencilZFail !== stencilZFail || currentStencilZPass !== stencilZPass ) { gl.stencilOp( stencilFail, stencilZFail, stencilZPass ); currentStencilFail = stencilFail; currentStencilZFail = stencilZFail; currentStencilZPass = stencilZPass; } }, setLocked: function ( lock ) { locked = lock; }, setClear: function ( stencil ) { if ( currentStencilClear !== stencil ) { gl.clearStencil( stencil ); currentStencilClear = stencil; } }, reset: function () { locked = false; currentStencilMask = null; currentStencilFunc = null; currentStencilRef = null; currentStencilFuncMask = null; currentStencilFail = null; currentStencilZFail = null; currentStencilZPass = null; currentStencilClear = null; } }; } // const colorBuffer = new ColorBuffer(); const depthBuffer = new DepthBuffer(); const stencilBuffer = new StencilBuffer(); let enabledCapabilities = {}; let currentProgram = null; let currentBlendingEnabled = null; let currentBlending = null; let currentBlendEquation = null; let currentBlendSrc = null; let currentBlendDst = null; let currentBlendEquationAlpha = null; let currentBlendSrcAlpha = null; let currentBlendDstAlpha = null; let currentPremultipledAlpha = false; let currentFlipSided = null; let currentCullFace = null; let currentLineWidth = null; let currentPolygonOffsetFactor = null; let currentPolygonOffsetUnits = null; const maxTextures = gl.getParameter( 35661 ); let lineWidthAvailable = false; let version = 0; const glVersion = gl.getParameter( 7938 ); if ( glVersion.indexOf( 'WebGL' ) !== - 1 ) { version = parseFloat( /^WebGL (\d)/.exec( glVersion )[ 1 ] ); lineWidthAvailable = ( version >= 1.0 ); } else if ( glVersion.indexOf( 'OpenGL ES' ) !== - 1 ) { version = parseFloat( /^OpenGL ES (\d)/.exec( glVersion )[ 1 ] ); lineWidthAvailable = ( version >= 2.0 ); } let currentTextureSlot = null; let currentBoundTextures = {}; const currentScissor = new Vector4(); const currentViewport = new Vector4(); function createTexture( type, target, count ) { const data = new Uint8Array( 4 ); // 4 is required to match default unpack alignment of 4. const texture = gl.createTexture(); gl.bindTexture( type, texture ); gl.texParameteri( type, 10241, 9728 ); gl.texParameteri( type, 10240, 9728 ); for ( let i = 0; i < count; i ++ ) { gl.texImage2D( target + i, 0, 6408, 1, 1, 0, 6408, 5121, data ); } return texture; } const emptyTextures = {}; emptyTextures[ 3553 ] = createTexture( 3553, 3553, 1 ); emptyTextures[ 34067 ] = createTexture( 34067, 34069, 6 ); // init colorBuffer.setClear( 0, 0, 0, 1 ); depthBuffer.setClear( 1 ); stencilBuffer.setClear( 0 ); enable( 2929 ); depthBuffer.setFunc( LessEqualDepth ); setFlipSided( false ); setCullFace( CullFaceBack ); enable( 2884 ); setBlending( NoBlending ); // function enable( id ) { if ( enabledCapabilities[ id ] !== true ) { gl.enable( id ); enabledCapabilities[ id ] = true; } } function disable( id ) { if ( enabledCapabilities[ id ] !== false ) { gl.disable( id ); enabledCapabilities[ id ] = false; } } function useProgram( program ) { if ( currentProgram !== program ) { gl.useProgram( program ); currentProgram = program; return true; } return false; } const equationToGL = { [ AddEquation ]: 32774, [ SubtractEquation ]: 32778, [ ReverseSubtractEquation ]: 32779 }; if ( isWebGL2 ) { equationToGL[ MinEquation ] = 32775; equationToGL[ MaxEquation ] = 32776; } else { const extension = extensions.get( 'EXT_blend_minmax' ); if ( extension !== null ) { equationToGL[ MinEquation ] = extension.MIN_EXT; equationToGL[ MaxEquation ] = extension.MAX_EXT; } } const factorToGL = { [ ZeroFactor ]: 0, [ OneFactor ]: 1, [ SrcColorFactor ]: 768, [ SrcAlphaFactor ]: 770, [ SrcAlphaSaturateFactor ]: 776, [ DstColorFactor ]: 774, [ DstAlphaFactor ]: 772, [ OneMinusSrcColorFactor ]: 769, [ OneMinusSrcAlphaFactor ]: 771, [ OneMinusDstColorFactor ]: 775, [ OneMinusDstAlphaFactor ]: 773 }; function setBlending( blending, blendEquation, blendSrc, blendDst, blendEquationAlpha, blendSrcAlpha, blendDstAlpha, premultipliedAlpha ) { if ( blending === NoBlending ) { if ( currentBlendingEnabled ) { disable( 3042 ); currentBlendingEnabled = false; } return; } if ( ! currentBlendingEnabled ) { enable( 3042 ); currentBlendingEnabled = true; } if ( blending !== CustomBlending ) { if ( blending !== currentBlending || premultipliedAlpha !== currentPremultipledAlpha ) { if ( currentBlendEquation !== AddEquation || currentBlendEquationAlpha !== AddEquation ) { gl.blendEquation( 32774 ); currentBlendEquation = AddEquation; currentBlendEquationAlpha = AddEquation; } if ( premultipliedAlpha ) { switch ( blending ) { case NormalBlending: gl.blendFuncSeparate( 1, 771, 1, 771 ); break; case AdditiveBlending: gl.blendFunc( 1, 1 ); break; case SubtractiveBlending: gl.blendFuncSeparate( 0, 0, 769, 771 ); break; case MultiplyBlending: gl.blendFuncSeparate( 0, 768, 0, 770 ); break; default: console.error( 'THREE.WebGLState: Invalid blending: ', blending ); break; } } else { switch ( blending ) { case NormalBlending: gl.blendFuncSeparate( 770, 771, 1, 771 ); break; case AdditiveBlending: gl.blendFunc( 770, 1 ); break; case SubtractiveBlending: gl.blendFunc( 0, 769 ); break; case MultiplyBlending: gl.blendFunc( 0, 768 ); break; default: console.error( 'THREE.WebGLState: Invalid blending: ', blending ); break; } } currentBlendSrc = null; currentBlendDst = null; currentBlendSrcAlpha = null; currentBlendDstAlpha = null; currentBlending = blending; currentPremultipledAlpha = premultipliedAlpha; } return; } // custom blending blendEquationAlpha = blendEquationAlpha || blendEquation; blendSrcAlpha = blendSrcAlpha || blendSrc; blendDstAlpha = blendDstAlpha || blendDst; if ( blendEquation !== currentBlendEquation || blendEquationAlpha !== currentBlendEquationAlpha ) { gl.blendEquationSeparate( equationToGL[ blendEquation ], equationToGL[ blendEquationAlpha ] ); currentBlendEquation = blendEquation; currentBlendEquationAlpha = blendEquationAlpha; } if ( blendSrc !== currentBlendSrc || blendDst !== currentBlendDst || blendSrcAlpha !== currentBlendSrcAlpha || blendDstAlpha !== currentBlendDstAlpha ) { gl.blendFuncSeparate( factorToGL[ blendSrc ], factorToGL[ blendDst ], factorToGL[ blendSrcAlpha ], factorToGL[ blendDstAlpha ] ); currentBlendSrc = blendSrc; currentBlendDst = blendDst; currentBlendSrcAlpha = blendSrcAlpha; currentBlendDstAlpha = blendDstAlpha; } currentBlending = blending; currentPremultipledAlpha = null; } function setMaterial( material, frontFaceCW ) { material.side === DoubleSide ? disable( 2884 ) : enable( 2884 ); let flipSided = ( material.side === BackSide ); if ( frontFaceCW ) flipSided = ! flipSided; setFlipSided( flipSided ); ( material.blending === NormalBlending && material.transparent === false ) ? setBlending( NoBlending ) : setBlending( material.blending, material.blendEquation, material.blendSrc, material.blendDst, material.blendEquationAlpha, material.blendSrcAlpha, material.blendDstAlpha, material.premultipliedAlpha ); depthBuffer.setFunc( material.depthFunc ); depthBuffer.setTest( material.depthTest ); depthBuffer.setMask( material.depthWrite ); colorBuffer.setMask( material.colorWrite ); const stencilWrite = material.stencilWrite; stencilBuffer.setTest( stencilWrite ); if ( stencilWrite ) { stencilBuffer.setMask( material.stencilWriteMask ); stencilBuffer.setFunc( material.stencilFunc, material.stencilRef, material.stencilFuncMask ); stencilBuffer.setOp( material.stencilFail, material.stencilZFail, material.stencilZPass ); } setPolygonOffset( material.polygonOffset, material.polygonOffsetFactor, material.polygonOffsetUnits ); } // function setFlipSided( flipSided ) { if ( currentFlipSided !== flipSided ) { if ( flipSided ) { gl.frontFace( 2304 ); } else { gl.frontFace( 2305 ); } currentFlipSided = flipSided; } } function setCullFace( cullFace ) { if ( cullFace !== CullFaceNone ) { enable( 2884 ); if ( cullFace !== currentCullFace ) { if ( cullFace === CullFaceBack ) { gl.cullFace( 1029 ); } else if ( cullFace === CullFaceFront ) { gl.cullFace( 1028 ); } else { gl.cullFace( 1032 ); } } } else { disable( 2884 ); } currentCullFace = cullFace; } function setLineWidth( width ) { if ( width !== currentLineWidth ) { if ( lineWidthAvailable ) gl.lineWidth( width ); currentLineWidth = width; } } function setPolygonOffset( polygonOffset, factor, units ) { if ( polygonOffset ) { enable( 32823 ); if ( currentPolygonOffsetFactor !== factor || currentPolygonOffsetUnits !== units ) { gl.polygonOffset( factor, units ); currentPolygonOffsetFactor = factor; currentPolygonOffsetUnits = units; } } else { disable( 32823 ); } } function setScissorTest( scissorTest ) { if ( scissorTest ) { enable( 3089 ); } else { disable( 3089 ); } } // texture function activeTexture( webglSlot ) { if ( webglSlot === undefined ) webglSlot = 33984 + maxTextures - 1; if ( currentTextureSlot !== webglSlot ) { gl.activeTexture( webglSlot ); currentTextureSlot = webglSlot; } } function bindTexture( webglType, webglTexture ) { if ( currentTextureSlot === null ) { activeTexture(); } let boundTexture = currentBoundTextures[ currentTextureSlot ]; if ( boundTexture === undefined ) { boundTexture = { type: undefined, texture: undefined }; currentBoundTextures[ currentTextureSlot ] = boundTexture; } if ( boundTexture.type !== webglType || boundTexture.texture !== webglTexture ) { gl.bindTexture( webglType, webglTexture || emptyTextures[ webglType ] ); boundTexture.type = webglType; boundTexture.texture = webglTexture; } } function unbindTexture() { const boundTexture = currentBoundTextures[ currentTextureSlot ]; if ( boundTexture !== undefined && boundTexture.type !== undefined ) { gl.bindTexture( boundTexture.type, null ); boundTexture.type = undefined; boundTexture.texture = undefined; } } function compressedTexImage2D() { try { gl.compressedTexImage2D.apply( gl, arguments ); } catch ( error ) { console.error( 'THREE.WebGLState:', error ); } } function texImage2D() { try { gl.texImage2D.apply( gl, arguments ); } catch ( error ) { console.error( 'THREE.WebGLState:', error ); } } function texImage3D() { try { gl.texImage3D.apply( gl, arguments ); } catch ( error ) { console.error( 'THREE.WebGLState:', error ); } } // function scissor( scissor ) { if ( currentScissor.equals( scissor ) === false ) { gl.scissor( scissor.x, scissor.y, scissor.z, scissor.w ); currentScissor.copy( scissor ); } } function viewport( viewport ) { if ( currentViewport.equals( viewport ) === false ) { gl.viewport( viewport.x, viewport.y, viewport.z, viewport.w ); currentViewport.copy( viewport ); } } // function reset() { enabledCapabilities = {}; currentTextureSlot = null; currentBoundTextures = {}; currentProgram = null; currentBlendingEnabled = null; currentBlending = null; currentBlendEquation = null; currentBlendSrc = null; currentBlendDst = null; currentBlendEquationAlpha = null; currentBlendSrcAlpha = null; currentBlendDstAlpha = null; currentPremultipledAlpha = false; currentFlipSided = null; currentCullFace = null; currentLineWidth = null; currentPolygonOffsetFactor = null; currentPolygonOffsetUnits = null; colorBuffer.reset(); depthBuffer.reset(); stencilBuffer.reset(); } return { buffers: { color: colorBuffer, depth: depthBuffer, stencil: stencilBuffer }, enable: enable, disable: disable, useProgram: useProgram, setBlending: setBlending, setMaterial: setMaterial, setFlipSided: setFlipSided, setCullFace: setCullFace, setLineWidth: setLineWidth, setPolygonOffset: setPolygonOffset, setScissorTest: setScissorTest, activeTexture: activeTexture, bindTexture: bindTexture, unbindTexture: unbindTexture, compressedTexImage2D: compressedTexImage2D, texImage2D: texImage2D, texImage3D: texImage3D, scissor: scissor, viewport: viewport, reset: reset }; } function WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ) { const isWebGL2 = capabilities.isWebGL2; const maxTextures = capabilities.maxTextures; const maxCubemapSize = capabilities.maxCubemapSize; const maxTextureSize = capabilities.maxTextureSize; const maxSamples = capabilities.maxSamples; const _videoTextures = new WeakMap(); let _canvas; // cordova iOS (as of 5.0) still uses UIWebView, which provides OffscreenCanvas, // also OffscreenCanvas.getContext("webgl"), but not OffscreenCanvas.getContext("2d")! // Some implementations may only implement OffscreenCanvas partially (e.g. lacking 2d). let useOffscreenCanvas = false; try { useOffscreenCanvas = typeof OffscreenCanvas !== 'undefined' && ( new OffscreenCanvas( 1, 1 ).getContext( '2d' ) ) !== null; } catch ( err ) { // Ignore any errors } function createCanvas( width, height ) { // Use OffscreenCanvas when available. Specially needed in web workers return useOffscreenCanvas ? new OffscreenCanvas( width, height ) : document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ); } function resizeImage( image, needsPowerOfTwo, needsNewCanvas, maxSize ) { let scale = 1; // handle case if texture exceeds max size if ( image.width > maxSize || image.height > maxSize ) { scale = maxSize / Math.max( image.width, image.height ); } // only perform resize if necessary if ( scale < 1 || needsPowerOfTwo === true ) { // only perform resize for certain image types if ( ( typeof HTMLImageElement !== 'undefined' && image instanceof HTMLImageElement ) || ( typeof HTMLCanvasElement !== 'undefined' && image instanceof HTMLCanvasElement ) || ( typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap ) ) { const floor = needsPowerOfTwo ? MathUtils.floorPowerOfTwo : Math.floor; const width = floor( scale * image.width ); const height = floor( scale * image.height ); if ( _canvas === undefined ) _canvas = createCanvas( width, height ); // cube textures can't reuse the same canvas const canvas = needsNewCanvas ? createCanvas( width, height ) : _canvas; canvas.width = width; canvas.height = height; const context = canvas.getContext( '2d' ); context.drawImage( image, 0, 0, width, height ); console.warn( 'THREE.WebGLRenderer: Texture has been resized from (' + image.width + 'x' + image.height + ') to (' + width + 'x' + height + ').' ); return canvas; } else { if ( 'data' in image ) { console.warn( 'THREE.WebGLRenderer: Image in DataTexture is too big (' + image.width + 'x' + image.height + ').' ); } return image; } } return image; } function isPowerOfTwo( image ) { return MathUtils.isPowerOfTwo( image.width ) && MathUtils.isPowerOfTwo( image.height ); } function textureNeedsPowerOfTwo( texture ) { if ( isWebGL2 ) return false; return ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) || ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ); } function textureNeedsGenerateMipmaps( texture, supportsMips ) { return texture.generateMipmaps && supportsMips && texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter; } function generateMipmap( target, texture, width, height ) { _gl.generateMipmap( target ); const textureProperties = properties.get( texture ); // Note: Math.log( x ) * Math.LOG2E used instead of Math.log2( x ) which is not supported by IE11 textureProperties.__maxMipLevel = Math.log( Math.max( width, height ) ) * Math.LOG2E; } function getInternalFormat( internalFormatName, glFormat, glType ) { if ( isWebGL2 === false ) return glFormat; if ( internalFormatName !== null ) { if ( _gl[ internalFormatName ] !== undefined ) return _gl[ internalFormatName ]; console.warn( 'THREE.WebGLRenderer: Attempt to use non-existing WebGL internal format \'' + internalFormatName + '\'' ); } let internalFormat = glFormat; if ( glFormat === 6403 ) { if ( glType === 5126 ) internalFormat = 33326; if ( glType === 5131 ) internalFormat = 33325; if ( glType === 5121 ) internalFormat = 33321; } if ( glFormat === 6407 ) { if ( glType === 5126 ) internalFormat = 34837; if ( glType === 5131 ) internalFormat = 34843; if ( glType === 5121 ) internalFormat = 32849; } if ( glFormat === 6408 ) { if ( glType === 5126 ) internalFormat = 34836; if ( glType === 5131 ) internalFormat = 34842; if ( glType === 5121 ) internalFormat = 32856; } if ( internalFormat === 33325 || internalFormat === 33326 || internalFormat === 34842 || internalFormat === 34836 ) { extensions.get( 'EXT_color_buffer_float' ); } return internalFormat; } // Fallback filters for non-power-of-2 textures function filterFallback( f ) { if ( f === NearestFilter || f === NearestMipmapNearestFilter || f === NearestMipmapLinearFilter ) { return 9728; } return 9729; } // function onTextureDispose( event ) { const texture = event.target; texture.removeEventListener( 'dispose', onTextureDispose ); deallocateTexture( texture ); if ( texture.isVideoTexture ) { _videoTextures.delete( texture ); } info.memory.textures --; } function onRenderTargetDispose( event ) { const renderTarget = event.target; renderTarget.removeEventListener( 'dispose', onRenderTargetDispose ); deallocateRenderTarget( renderTarget ); info.memory.textures --; } // function deallocateTexture( texture ) { const textureProperties = properties.get( texture ); if ( textureProperties.__webglInit === undefined ) return; _gl.deleteTexture( textureProperties.__webglTexture ); properties.remove( texture ); } function deallocateRenderTarget( renderTarget ) { const renderTargetProperties = properties.get( renderTarget ); const textureProperties = properties.get( renderTarget.texture ); if ( ! renderTarget ) return; if ( textureProperties.__webglTexture !== undefined ) { _gl.deleteTexture( textureProperties.__webglTexture ); } if ( renderTarget.depthTexture ) { renderTarget.depthTexture.dispose(); } if ( renderTarget.isWebGLCubeRenderTarget ) { for ( let i = 0; i < 6; i ++ ) { _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer[ i ] ); if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer[ i ] ); } } else { _gl.deleteFramebuffer( renderTargetProperties.__webglFramebuffer ); if ( renderTargetProperties.__webglDepthbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthbuffer ); if ( renderTargetProperties.__webglMultisampledFramebuffer ) _gl.deleteFramebuffer( renderTargetProperties.__webglMultisampledFramebuffer ); if ( renderTargetProperties.__webglColorRenderbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglColorRenderbuffer ); if ( renderTargetProperties.__webglDepthRenderbuffer ) _gl.deleteRenderbuffer( renderTargetProperties.__webglDepthRenderbuffer ); } properties.remove( renderTarget.texture ); properties.remove( renderTarget ); } // let textureUnits = 0; function resetTextureUnits() { textureUnits = 0; } function allocateTextureUnit() { const textureUnit = textureUnits; if ( textureUnit >= maxTextures ) { console.warn( 'THREE.WebGLTextures: Trying to use ' + textureUnit + ' texture units while this GPU supports only ' + maxTextures ); } textureUnits += 1; return textureUnit; } // function setTexture2D( texture, slot ) { const textureProperties = properties.get( texture ); if ( texture.isVideoTexture ) updateVideoTexture( texture ); if ( texture.version > 0 && textureProperties.__version !== texture.version ) { const image = texture.image; if ( image === undefined ) { console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is undefined' ); } else if ( image.complete === false ) { console.warn( 'THREE.WebGLRenderer: Texture marked for update but image is incomplete' ); } else { uploadTexture( textureProperties, texture, slot ); return; } } state.activeTexture( 33984 + slot ); state.bindTexture( 3553, textureProperties.__webglTexture ); } function setTexture2DArray( texture, slot ) { const textureProperties = properties.get( texture ); if ( texture.version > 0 && textureProperties.__version !== texture.version ) { uploadTexture( textureProperties, texture, slot ); return; } state.activeTexture( 33984 + slot ); state.bindTexture( 35866, textureProperties.__webglTexture ); } function setTexture3D( texture, slot ) { const textureProperties = properties.get( texture ); if ( texture.version > 0 && textureProperties.__version !== texture.version ) { uploadTexture( textureProperties, texture, slot ); return; } state.activeTexture( 33984 + slot ); state.bindTexture( 32879, textureProperties.__webglTexture ); } function setTextureCube( texture, slot ) { const textureProperties = properties.get( texture ); if ( texture.version > 0 && textureProperties.__version !== texture.version ) { uploadCubeTexture( textureProperties, texture, slot ); return; } state.activeTexture( 33984 + slot ); state.bindTexture( 34067, textureProperties.__webglTexture ); } const wrappingToGL = { [ RepeatWrapping ]: 10497, [ ClampToEdgeWrapping ]: 33071, [ MirroredRepeatWrapping ]: 33648 }; const filterToGL = { [ NearestFilter ]: 9728, [ NearestMipmapNearestFilter ]: 9984, [ NearestMipmapLinearFilter ]: 9986, [ LinearFilter ]: 9729, [ LinearMipmapNearestFilter ]: 9985, [ LinearMipmapLinearFilter ]: 9987 }; function setTextureParameters( textureType, texture, supportsMips ) { if ( supportsMips ) { _gl.texParameteri( textureType, 10242, wrappingToGL[ texture.wrapS ] ); _gl.texParameteri( textureType, 10243, wrappingToGL[ texture.wrapT ] ); if ( textureType === 32879 || textureType === 35866 ) { _gl.texParameteri( textureType, 32882, wrappingToGL[ texture.wrapR ] ); } _gl.texParameteri( textureType, 10240, filterToGL[ texture.magFilter ] ); _gl.texParameteri( textureType, 10241, filterToGL[ texture.minFilter ] ); } else { _gl.texParameteri( textureType, 10242, 33071 ); _gl.texParameteri( textureType, 10243, 33071 ); if ( textureType === 32879 || textureType === 35866 ) { _gl.texParameteri( textureType, 32882, 33071 ); } if ( texture.wrapS !== ClampToEdgeWrapping || texture.wrapT !== ClampToEdgeWrapping ) { console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.wrapS and Texture.wrapT should be set to THREE.ClampToEdgeWrapping.' ); } _gl.texParameteri( textureType, 10240, filterFallback( texture.magFilter ) ); _gl.texParameteri( textureType, 10241, filterFallback( texture.minFilter ) ); if ( texture.minFilter !== NearestFilter && texture.minFilter !== LinearFilter ) { console.warn( 'THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter should be set to THREE.NearestFilter or THREE.LinearFilter.' ); } } const extension = extensions.get( 'EXT_texture_filter_anisotropic' ); if ( extension ) { if ( texture.type === FloatType && extensions.get( 'OES_texture_float_linear' ) === null ) return; if ( texture.type === HalfFloatType && ( isWebGL2 || extensions.get( 'OES_texture_half_float_linear' ) ) === null ) return; if ( texture.anisotropy > 1 || properties.get( texture ).__currentAnisotropy ) { _gl.texParameterf( textureType, extension.TEXTURE_MAX_ANISOTROPY_EXT, Math.min( texture.anisotropy, capabilities.getMaxAnisotropy() ) ); properties.get( texture ).__currentAnisotropy = texture.anisotropy; } } } function initTexture( textureProperties, texture ) { if ( textureProperties.__webglInit === undefined ) { textureProperties.__webglInit = true; texture.addEventListener( 'dispose', onTextureDispose ); textureProperties.__webglTexture = _gl.createTexture(); info.memory.textures ++; } } function uploadTexture( textureProperties, texture, slot ) { let textureType = 3553; if ( texture.isDataTexture2DArray ) textureType = 35866; if ( texture.isDataTexture3D ) textureType = 32879; initTexture( textureProperties, texture ); state.activeTexture( 33984 + slot ); state.bindTexture( textureType, textureProperties.__webglTexture ); _gl.pixelStorei( 37440, texture.flipY ); _gl.pixelStorei( 37441, texture.premultiplyAlpha ); _gl.pixelStorei( 3317, texture.unpackAlignment ); const needsPowerOfTwo = textureNeedsPowerOfTwo( texture ) && isPowerOfTwo( texture.image ) === false; const image = resizeImage( texture.image, needsPowerOfTwo, false, maxTextureSize ); const supportsMips = isPowerOfTwo( image ) || isWebGL2, glFormat = utils.convert( texture.format ); let glType = utils.convert( texture.type ), glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType ); setTextureParameters( textureType, texture, supportsMips ); let mipmap; const mipmaps = texture.mipmaps; if ( texture.isDepthTexture ) { // populate depth texture with dummy data glInternalFormat = 6402; if ( isWebGL2 ) { if ( texture.type === FloatType ) { glInternalFormat = 36012; } else if ( texture.type === UnsignedIntType ) { glInternalFormat = 33190; } else if ( texture.type === UnsignedInt248Type$1 ) { glInternalFormat = 35056; } else { glInternalFormat = 33189; // WebGL2 requires sized internalformat for glTexImage2D } } else { if ( texture.type === FloatType ) { console.error( 'WebGLRenderer: Floating point depth texture requires WebGL2.' ); } } // validation checks for WebGL 1 if ( texture.format === DepthFormat && glInternalFormat === 6402 ) { // The error INVALID_OPERATION is generated by texImage2D if format and internalformat are // DEPTH_COMPONENT and type is not UNSIGNED_SHORT or UNSIGNED_INT // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/) if ( texture.type !== UnsignedShortType && texture.type !== UnsignedIntType ) { console.warn( 'THREE.WebGLRenderer: Use UnsignedShortType or UnsignedIntType for DepthFormat DepthTexture.' ); texture.type = UnsignedShortType; glType = utils.convert( texture.type ); } } if ( texture.format === DepthStencilFormat && glInternalFormat === 6402 ) { // Depth stencil textures need the DEPTH_STENCIL internal format // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/) glInternalFormat = 34041; // The error INVALID_OPERATION is generated by texImage2D if format and internalformat are // DEPTH_STENCIL and type is not UNSIGNED_INT_24_8_WEBGL. // (https://www.khronos.org/registry/webgl/extensions/WEBGL_depth_texture/) if ( texture.type !== UnsignedInt248Type$1 ) { console.warn( 'THREE.WebGLRenderer: Use UnsignedInt248Type for DepthStencilFormat DepthTexture.' ); texture.type = UnsignedInt248Type$1; glType = utils.convert( texture.type ); } } // state.texImage2D( 3553, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null ); } else if ( texture.isDataTexture ) { // use manually created mipmaps if available // if there are no manual mipmaps // set 0 level mipmap and then use GL to generate other mipmap levels if ( mipmaps.length > 0 && supportsMips ) { for ( let i = 0, il = mipmaps.length; i < il; i ++ ) { mipmap = mipmaps[ i ]; state.texImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); } texture.generateMipmaps = false; textureProperties.__maxMipLevel = mipmaps.length - 1; } else { state.texImage2D( 3553, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, image.data ); textureProperties.__maxMipLevel = 0; } } else if ( texture.isCompressedTexture ) { for ( let i = 0, il = mipmaps.length; i < il; i ++ ) { mipmap = mipmaps[ i ]; if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) { if ( glFormat !== null ) { state.compressedTexImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data ); } else { console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .uploadTexture()' ); } } else { state.texImage2D( 3553, i, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); } } textureProperties.__maxMipLevel = mipmaps.length - 1; } else if ( texture.isDataTexture2DArray ) { state.texImage3D( 35866, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data ); textureProperties.__maxMipLevel = 0; } else if ( texture.isDataTexture3D ) { state.texImage3D( 32879, 0, glInternalFormat, image.width, image.height, image.depth, 0, glFormat, glType, image.data ); textureProperties.__maxMipLevel = 0; } else { // regular Texture (image, video, canvas) // use manually created mipmaps if available // if there are no manual mipmaps // set 0 level mipmap and then use GL to generate other mipmap levels if ( mipmaps.length > 0 && supportsMips ) { for ( let i = 0, il = mipmaps.length; i < il; i ++ ) { mipmap = mipmaps[ i ]; state.texImage2D( 3553, i, glInternalFormat, glFormat, glType, mipmap ); } texture.generateMipmaps = false; textureProperties.__maxMipLevel = mipmaps.length - 1; } else { state.texImage2D( 3553, 0, glInternalFormat, glFormat, glType, image ); textureProperties.__maxMipLevel = 0; } } if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) { generateMipmap( textureType, texture, image.width, image.height ); } textureProperties.__version = texture.version; if ( texture.onUpdate ) texture.onUpdate( texture ); } function uploadCubeTexture( textureProperties, texture, slot ) { if ( texture.image.length !== 6 ) return; initTexture( textureProperties, texture ); state.activeTexture( 33984 + slot ); state.bindTexture( 34067, textureProperties.__webglTexture ); _gl.pixelStorei( 37440, texture.flipY ); const isCompressed = ( texture && ( texture.isCompressedTexture || texture.image[ 0 ].isCompressedTexture ) ); const isDataTexture = ( texture.image[ 0 ] && texture.image[ 0 ].isDataTexture ); const cubeImage = []; for ( let i = 0; i < 6; i ++ ) { if ( ! isCompressed && ! isDataTexture ) { cubeImage[ i ] = resizeImage( texture.image[ i ], false, true, maxCubemapSize ); } else { cubeImage[ i ] = isDataTexture ? texture.image[ i ].image : texture.image[ i ]; } } const image = cubeImage[ 0 ], supportsMips = isPowerOfTwo( image ) || isWebGL2, glFormat = utils.convert( texture.format ), glType = utils.convert( texture.type ), glInternalFormat = getInternalFormat( texture.internalFormat, glFormat, glType ); setTextureParameters( 34067, texture, supportsMips ); let mipmaps; if ( isCompressed ) { for ( let i = 0; i < 6; i ++ ) { mipmaps = cubeImage[ i ].mipmaps; for ( let j = 0; j < mipmaps.length; j ++ ) { const mipmap = mipmaps[ j ]; if ( texture.format !== RGBAFormat && texture.format !== RGBFormat ) { if ( glFormat !== null ) { state.compressedTexImage2D( 34069 + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, mipmap.data ); } else { console.warn( 'THREE.WebGLRenderer: Attempt to load unsupported compressed texture format in .setTextureCube()' ); } } else { state.texImage2D( 34069 + i, j, glInternalFormat, mipmap.width, mipmap.height, 0, glFormat, glType, mipmap.data ); } } } textureProperties.__maxMipLevel = mipmaps.length - 1; } else { mipmaps = texture.mipmaps; for ( let i = 0; i < 6; i ++ ) { if ( isDataTexture ) { state.texImage2D( 34069 + i, 0, glInternalFormat, cubeImage[ i ].width, cubeImage[ i ].height, 0, glFormat, glType, cubeImage[ i ].data ); for ( let j = 0; j < mipmaps.length; j ++ ) { const mipmap = mipmaps[ j ]; const mipmapImage = mipmap.image[ i ].image; state.texImage2D( 34069 + i, j + 1, glInternalFormat, mipmapImage.width, mipmapImage.height, 0, glFormat, glType, mipmapImage.data ); } } else { state.texImage2D( 34069 + i, 0, glInternalFormat, glFormat, glType, cubeImage[ i ] ); for ( let j = 0; j < mipmaps.length; j ++ ) { const mipmap = mipmaps[ j ]; state.texImage2D( 34069 + i, j + 1, glInternalFormat, glFormat, glType, mipmap.image[ i ] ); } } } textureProperties.__maxMipLevel = mipmaps.length; } if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) { // We assume images for cube map have the same size. generateMipmap( 34067, texture, image.width, image.height ); } textureProperties.__version = texture.version; if ( texture.onUpdate ) texture.onUpdate( texture ); } // Render targets // Setup storage for target texture and bind it to correct framebuffer function setupFrameBufferTexture( framebuffer, renderTarget, attachment, textureTarget ) { const glFormat = utils.convert( renderTarget.texture.format ); const glType = utils.convert( renderTarget.texture.type ); const glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType ); state.texImage2D( textureTarget, 0, glInternalFormat, renderTarget.width, renderTarget.height, 0, glFormat, glType, null ); _gl.bindFramebuffer( 36160, framebuffer ); _gl.framebufferTexture2D( 36160, attachment, textureTarget, properties.get( renderTarget.texture ).__webglTexture, 0 ); _gl.bindFramebuffer( 36160, null ); } // Setup storage for internal depth/stencil buffers and bind to correct framebuffer function setupRenderBufferStorage( renderbuffer, renderTarget, isMultisample ) { _gl.bindRenderbuffer( 36161, renderbuffer ); if ( renderTarget.depthBuffer && ! renderTarget.stencilBuffer ) { let glInternalFormat = 33189; if ( isMultisample ) { const depthTexture = renderTarget.depthTexture; if ( depthTexture && depthTexture.isDepthTexture ) { if ( depthTexture.type === FloatType ) { glInternalFormat = 36012; } else if ( depthTexture.type === UnsignedIntType ) { glInternalFormat = 33190; } } const samples = getRenderTargetSamples( renderTarget ); _gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height ); } else { _gl.renderbufferStorage( 36161, glInternalFormat, renderTarget.width, renderTarget.height ); } _gl.framebufferRenderbuffer( 36160, 36096, 36161, renderbuffer ); } else if ( renderTarget.depthBuffer && renderTarget.stencilBuffer ) { if ( isMultisample ) { const samples = getRenderTargetSamples( renderTarget ); _gl.renderbufferStorageMultisample( 36161, samples, 35056, renderTarget.width, renderTarget.height ); } else { _gl.renderbufferStorage( 36161, 34041, renderTarget.width, renderTarget.height ); } _gl.framebufferRenderbuffer( 36160, 33306, 36161, renderbuffer ); } else { const glFormat = utils.convert( renderTarget.texture.format ); const glType = utils.convert( renderTarget.texture.type ); const glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType ); if ( isMultisample ) { const samples = getRenderTargetSamples( renderTarget ); _gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height ); } else { _gl.renderbufferStorage( 36161, glInternalFormat, renderTarget.width, renderTarget.height ); } } _gl.bindRenderbuffer( 36161, null ); } // Setup resources for a Depth Texture for a FBO (needs an extension) function setupDepthTexture( framebuffer, renderTarget ) { const isCube = ( renderTarget && renderTarget.isWebGLCubeRenderTarget ); if ( isCube ) throw new Error( 'Depth Texture with cube render targets is not supported' ); _gl.bindFramebuffer( 36160, framebuffer ); if ( ! ( renderTarget.depthTexture && renderTarget.depthTexture.isDepthTexture ) ) { throw new Error( 'renderTarget.depthTexture must be an instance of THREE.DepthTexture' ); } // upload an empty depth texture with framebuffer size if ( ! properties.get( renderTarget.depthTexture ).__webglTexture || renderTarget.depthTexture.image.width !== renderTarget.width || renderTarget.depthTexture.image.height !== renderTarget.height ) { renderTarget.depthTexture.image.width = renderTarget.width; renderTarget.depthTexture.image.height = renderTarget.height; renderTarget.depthTexture.needsUpdate = true; } setTexture2D( renderTarget.depthTexture, 0 ); const webglDepthTexture = properties.get( renderTarget.depthTexture ).__webglTexture; if ( renderTarget.depthTexture.format === DepthFormat ) { _gl.framebufferTexture2D( 36160, 36096, 3553, webglDepthTexture, 0 ); } else if ( renderTarget.depthTexture.format === DepthStencilFormat ) { _gl.framebufferTexture2D( 36160, 33306, 3553, webglDepthTexture, 0 ); } else { throw new Error( 'Unknown depthTexture format' ); } } // Setup GL resources for a non-texture depth buffer function setupDepthRenderbuffer( renderTarget ) { const renderTargetProperties = properties.get( renderTarget ); const isCube = ( renderTarget.isWebGLCubeRenderTarget === true ); if ( renderTarget.depthTexture ) { if ( isCube ) throw new Error( 'target.depthTexture not supported in Cube render targets' ); setupDepthTexture( renderTargetProperties.__webglFramebuffer, renderTarget ); } else { if ( isCube ) { renderTargetProperties.__webglDepthbuffer = []; for ( let i = 0; i < 6; i ++ ) { _gl.bindFramebuffer( 36160, renderTargetProperties.__webglFramebuffer[ i ] ); renderTargetProperties.__webglDepthbuffer[ i ] = _gl.createRenderbuffer(); setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer[ i ], renderTarget, false ); } } else { _gl.bindFramebuffer( 36160, renderTargetProperties.__webglFramebuffer ); renderTargetProperties.__webglDepthbuffer = _gl.createRenderbuffer(); setupRenderBufferStorage( renderTargetProperties.__webglDepthbuffer, renderTarget, false ); } } _gl.bindFramebuffer( 36160, null ); } // Set up GL resources for the render target function setupRenderTarget( renderTarget ) { const renderTargetProperties = properties.get( renderTarget ); const textureProperties = properties.get( renderTarget.texture ); renderTarget.addEventListener( 'dispose', onRenderTargetDispose ); textureProperties.__webglTexture = _gl.createTexture(); info.memory.textures ++; const isCube = ( renderTarget.isWebGLCubeRenderTarget === true ); const isMultisample = ( renderTarget.isWebGLMultisampleRenderTarget === true ); const supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2; // Handles WebGL2 RGBFormat fallback - #18858 if ( isWebGL2 && renderTarget.texture.format === RGBFormat && ( renderTarget.texture.type === FloatType || renderTarget.texture.type === HalfFloatType ) ) { renderTarget.texture.format = RGBAFormat; console.warn( 'THREE.WebGLRenderer: Rendering to textures with RGB format is not supported. Using RGBA format instead.' ); } // Setup framebuffer if ( isCube ) { renderTargetProperties.__webglFramebuffer = []; for ( let i = 0; i < 6; i ++ ) { renderTargetProperties.__webglFramebuffer[ i ] = _gl.createFramebuffer(); } } else { renderTargetProperties.__webglFramebuffer = _gl.createFramebuffer(); if ( isMultisample ) { if ( isWebGL2 ) { renderTargetProperties.__webglMultisampledFramebuffer = _gl.createFramebuffer(); renderTargetProperties.__webglColorRenderbuffer = _gl.createRenderbuffer(); _gl.bindRenderbuffer( 36161, renderTargetProperties.__webglColorRenderbuffer ); const glFormat = utils.convert( renderTarget.texture.format ); const glType = utils.convert( renderTarget.texture.type ); const glInternalFormat = getInternalFormat( renderTarget.texture.internalFormat, glFormat, glType ); const samples = getRenderTargetSamples( renderTarget ); _gl.renderbufferStorageMultisample( 36161, samples, glInternalFormat, renderTarget.width, renderTarget.height ); _gl.bindFramebuffer( 36160, renderTargetProperties.__webglMultisampledFramebuffer ); _gl.framebufferRenderbuffer( 36160, 36064, 36161, renderTargetProperties.__webglColorRenderbuffer ); _gl.bindRenderbuffer( 36161, null ); if ( renderTarget.depthBuffer ) { renderTargetProperties.__webglDepthRenderbuffer = _gl.createRenderbuffer(); setupRenderBufferStorage( renderTargetProperties.__webglDepthRenderbuffer, renderTarget, true ); } _gl.bindFramebuffer( 36160, null ); } else { console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' ); } } } // Setup color buffer if ( isCube ) { state.bindTexture( 34067, textureProperties.__webglTexture ); setTextureParameters( 34067, renderTarget.texture, supportsMips ); for ( let i = 0; i < 6; i ++ ) { setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer[ i ], renderTarget, 36064, 34069 + i ); } if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) { generateMipmap( 34067, renderTarget.texture, renderTarget.width, renderTarget.height ); } state.bindTexture( 34067, null ); } else { state.bindTexture( 3553, textureProperties.__webglTexture ); setTextureParameters( 3553, renderTarget.texture, supportsMips ); setupFrameBufferTexture( renderTargetProperties.__webglFramebuffer, renderTarget, 36064, 3553 ); if ( textureNeedsGenerateMipmaps( renderTarget.texture, supportsMips ) ) { generateMipmap( 3553, renderTarget.texture, renderTarget.width, renderTarget.height ); } state.bindTexture( 3553, null ); } // Setup depth and stencil buffers if ( renderTarget.depthBuffer ) { setupDepthRenderbuffer( renderTarget ); } } function updateRenderTargetMipmap( renderTarget ) { const texture = renderTarget.texture; const supportsMips = isPowerOfTwo( renderTarget ) || isWebGL2; if ( textureNeedsGenerateMipmaps( texture, supportsMips ) ) { const target = renderTarget.isWebGLCubeRenderTarget ? 34067 : 3553; const webglTexture = properties.get( texture ).__webglTexture; state.bindTexture( target, webglTexture ); generateMipmap( target, texture, renderTarget.width, renderTarget.height ); state.bindTexture( target, null ); } } function updateMultisampleRenderTarget( renderTarget ) { if ( renderTarget.isWebGLMultisampleRenderTarget ) { if ( isWebGL2 ) { const renderTargetProperties = properties.get( renderTarget ); _gl.bindFramebuffer( 36008, renderTargetProperties.__webglMultisampledFramebuffer ); _gl.bindFramebuffer( 36009, renderTargetProperties.__webglFramebuffer ); const width = renderTarget.width; const height = renderTarget.height; let mask = 16384; if ( renderTarget.depthBuffer ) mask |= 256; if ( renderTarget.stencilBuffer ) mask |= 1024; _gl.blitFramebuffer( 0, 0, width, height, 0, 0, width, height, mask, 9728 ); _gl.bindFramebuffer( 36160, renderTargetProperties.__webglMultisampledFramebuffer ); // see #18905 } else { console.warn( 'THREE.WebGLRenderer: WebGLMultisampleRenderTarget can only be used with WebGL2.' ); } } } function getRenderTargetSamples( renderTarget ) { return ( isWebGL2 && renderTarget.isWebGLMultisampleRenderTarget ) ? Math.min( maxSamples, renderTarget.samples ) : 0; } function updateVideoTexture( texture ) { const frame = info.render.frame; // Check the last frame we updated the VideoTexture if ( _videoTextures.get( texture ) !== frame ) { _videoTextures.set( texture, frame ); texture.update(); } } // backwards compatibility let warnedTexture2D = false; let warnedTextureCube = false; function safeSetTexture2D( texture, slot ) { if ( texture && texture.isWebGLRenderTarget ) { if ( warnedTexture2D === false ) { console.warn( 'THREE.WebGLTextures.safeSetTexture2D: don\'t use render targets as textures. Use their .texture property instead.' ); warnedTexture2D = true; } texture = texture.texture; } setTexture2D( texture, slot ); } function safeSetTextureCube( texture, slot ) { if ( texture && texture.isWebGLCubeRenderTarget ) { if ( warnedTextureCube === false ) { console.warn( 'THREE.WebGLTextures.safeSetTextureCube: don\'t use cube render targets as textures. Use their .texture property instead.' ); warnedTextureCube = true; } texture = texture.texture; } setTextureCube( texture, slot ); } // this.allocateTextureUnit = allocateTextureUnit; this.resetTextureUnits = resetTextureUnits; this.setTexture2D = setTexture2D; this.setTexture2DArray = setTexture2DArray; this.setTexture3D = setTexture3D; this.setTextureCube = setTextureCube; this.setupRenderTarget = setupRenderTarget; this.updateRenderTargetMipmap = updateRenderTargetMipmap; this.updateMultisampleRenderTarget = updateMultisampleRenderTarget; this.safeSetTexture2D = safeSetTexture2D; this.safeSetTextureCube = safeSetTextureCube; } function WebGLUtils( gl, extensions, capabilities ) { const isWebGL2 = capabilities.isWebGL2; function convert( p ) { let extension; if ( p === UnsignedByteType ) return 5121; if ( p === UnsignedShort4444Type ) return 32819; if ( p === UnsignedShort5551Type ) return 32820; if ( p === UnsignedShort565Type ) return 33635; if ( p === ByteType ) return 5120; if ( p === ShortType ) return 5122; if ( p === UnsignedShortType ) return 5123; if ( p === IntType ) return 5124; if ( p === UnsignedIntType ) return 5125; if ( p === FloatType ) return 5126; if ( p === HalfFloatType ) { if ( isWebGL2 ) return 5131; extension = extensions.get( 'OES_texture_half_float' ); if ( extension !== null ) { return extension.HALF_FLOAT_OES; } else { return null; } } if ( p === AlphaFormat ) return 6406; if ( p === RGBFormat ) return 6407; if ( p === RGBAFormat ) return 6408; if ( p === LuminanceFormat ) return 6409; if ( p === LuminanceAlphaFormat ) return 6410; if ( p === DepthFormat ) return 6402; if ( p === DepthStencilFormat ) return 34041; if ( p === RedFormat ) return 6403; // WebGL2 formats. if ( p === RedIntegerFormat ) return 36244; if ( p === RGFormat ) return 33319; if ( p === RGIntegerFormat ) return 33320; if ( p === RGBIntegerFormat ) return 36248; if ( p === RGBAIntegerFormat ) return 36249; if ( p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format$1 || p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format$1 ) { extension = extensions.get( 'WEBGL_compressed_texture_s3tc' ); if ( extension !== null ) { if ( p === RGB_S3TC_DXT1_Format ) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT; if ( p === RGBA_S3TC_DXT1_Format$1 ) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT; if ( p === RGBA_S3TC_DXT3_Format ) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT; if ( p === RGBA_S3TC_DXT5_Format$1 ) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT; } else { return null; } } if ( p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format || p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format ) { extension = extensions.get( 'WEBGL_compressed_texture_pvrtc' ); if ( extension !== null ) { if ( p === RGB_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; if ( p === RGB_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; if ( p === RGBA_PVRTC_4BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; if ( p === RGBA_PVRTC_2BPPV1_Format ) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; } else { return null; } } if ( p === RGB_ETC1_Format ) { extension = extensions.get( 'WEBGL_compressed_texture_etc1' ); if ( extension !== null ) { return extension.COMPRESSED_RGB_ETC1_WEBGL; } else { return null; } } if ( p === RGB_ETC2_Format || p === RGBA_ETC2_EAC_Format ) { extension = extensions.get( 'WEBGL_compressed_texture_etc' ); if ( extension !== null ) { if ( p === RGB_ETC2_Format ) return extension.COMPRESSED_RGB8_ETC2; if ( p === RGBA_ETC2_EAC_Format ) return extension.COMPRESSED_RGBA8_ETC2_EAC; } } if ( p === RGBA_ASTC_4x4_Format || p === RGBA_ASTC_5x4_Format || p === RGBA_ASTC_5x5_Format || p === RGBA_ASTC_6x5_Format || p === RGBA_ASTC_6x6_Format || p === RGBA_ASTC_8x5_Format || p === RGBA_ASTC_8x6_Format || p === RGBA_ASTC_8x8_Format || p === RGBA_ASTC_10x5_Format || p === RGBA_ASTC_10x6_Format || p === RGBA_ASTC_10x8_Format || p === RGBA_ASTC_10x10_Format || p === RGBA_ASTC_12x10_Format || p === RGBA_ASTC_12x12_Format || p === SRGB8_ALPHA8_ASTC_4x4_Format || p === SRGB8_ALPHA8_ASTC_5x4_Format || p === SRGB8_ALPHA8_ASTC_5x5_Format || p === SRGB8_ALPHA8_ASTC_6x5_Format || p === SRGB8_ALPHA8_ASTC_6x6_Format || p === SRGB8_ALPHA8_ASTC_8x5_Format || p === SRGB8_ALPHA8_ASTC_8x6_Format || p === SRGB8_ALPHA8_ASTC_8x8_Format || p === SRGB8_ALPHA8_ASTC_10x5_Format || p === SRGB8_ALPHA8_ASTC_10x6_Format || p === SRGB8_ALPHA8_ASTC_10x8_Format || p === SRGB8_ALPHA8_ASTC_10x10_Format || p === SRGB8_ALPHA8_ASTC_12x10_Format || p === SRGB8_ALPHA8_ASTC_12x12_Format ) { extension = extensions.get( 'WEBGL_compressed_texture_astc' ); if ( extension !== null ) { // TODO Complete? return p; } else { return null; } } if ( p === RGBA_BPTC_Format ) { extension = extensions.get( 'EXT_texture_compression_bptc' ); if ( extension !== null ) { // TODO Complete? return p; } else { return null; } } if ( p === UnsignedInt248Type$1 ) { if ( isWebGL2 ) return 34042; extension = extensions.get( 'WEBGL_depth_texture' ); if ( extension !== null ) { return extension.UNSIGNED_INT_24_8_WEBGL; } else { return null; } } } return { convert: convert }; } function ArrayCamera( array = [] ) { PerspectiveCamera.call( this ); this.cameras = array; } ArrayCamera.prototype = Object.assign( Object.create( PerspectiveCamera.prototype ), { constructor: ArrayCamera, isArrayCamera: true } ); function Group() { Object3D.call( this ); this.type = 'Group'; } Group.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Group, isGroup: true } ); function WebXRController() { this._targetRay = null; this._grip = null; this._hand = null; } Object.assign( WebXRController.prototype, { constructor: WebXRController, getHandSpace: function () { if ( this._hand === null ) { this._hand = new Group(); this._hand.matrixAutoUpdate = false; this._hand.visible = false; this._hand.joints = []; this._hand.inputState = { pinching: false }; if ( window.XRHand ) { for ( let i = 0; i <= window.XRHand.LITTLE_PHALANX_TIP; i ++ ) { // The transform of this joint will be updated with the joint pose on each frame const joint = new Group(); joint.matrixAutoUpdate = false; joint.visible = false; this._hand.joints.push( joint ); // ?? this._hand.add( joint ); } } } return this._hand; }, getTargetRaySpace: function () { if ( this._targetRay === null ) { this._targetRay = new Group(); this._targetRay.matrixAutoUpdate = false; this._targetRay.visible = false; } return this._targetRay; }, getGripSpace: function () { if ( this._grip === null ) { this._grip = new Group(); this._grip.matrixAutoUpdate = false; this._grip.visible = false; } return this._grip; }, dispatchEvent: function ( event ) { if ( this._targetRay !== null ) { this._targetRay.dispatchEvent( event ); } if ( this._grip !== null ) { this._grip.dispatchEvent( event ); } if ( this._hand !== null ) { this._hand.dispatchEvent( event ); } return this; }, disconnect: function ( inputSource ) { this.dispatchEvent( { type: 'disconnected', data: inputSource } ); if ( this._targetRay !== null ) { this._targetRay.visible = false; } if ( this._grip !== null ) { this._grip.visible = false; } if ( this._hand !== null ) { this._hand.visible = false; } return this; }, update: function ( inputSource, frame, referenceSpace ) { let inputPose = null; let gripPose = null; let handPose = null; const targetRay = this._targetRay; const grip = this._grip; const hand = this._hand; if ( inputSource && frame.session.visibilityState !== 'visible-blurred' ) { if ( hand && inputSource.hand ) { handPose = true; for ( let i = 0; i <= window.XRHand.LITTLE_PHALANX_TIP; i ++ ) { if ( inputSource.hand[ i ] ) { // Update the joints groups with the XRJoint poses const jointPose = frame.getJointPose( inputSource.hand[ i ], referenceSpace ); const joint = hand.joints[ i ]; if ( jointPose !== null ) { joint.matrix.fromArray( jointPose.transform.matrix ); joint.matrix.decompose( joint.position, joint.rotation, joint.scale ); joint.jointRadius = jointPose.radius; } joint.visible = jointPose !== null; // Custom events // Check pinch const indexTip = hand.joints[ window.XRHand.INDEX_PHALANX_TIP ]; const thumbTip = hand.joints[ window.XRHand.THUMB_PHALANX_TIP ]; const distance = indexTip.position.distanceTo( thumbTip.position ); const distanceToPinch = 0.02; const threshold = 0.005; if ( hand.inputState.pinching && distance > distanceToPinch + threshold ) { hand.inputState.pinching = false; this.dispatchEvent( { type: 'pinchend', handedness: inputSource.handedness, target: this } ); } else if ( ! hand.inputState.pinching && distance <= distanceToPinch - threshold ) { hand.inputState.pinching = true; this.dispatchEvent( { type: 'pinchstart', handedness: inputSource.handedness, target: this } ); } } } } else { if ( targetRay !== null ) { inputPose = frame.getPose( inputSource.targetRaySpace, referenceSpace ); if ( inputPose !== null ) { targetRay.matrix.fromArray( inputPose.transform.matrix ); targetRay.matrix.decompose( targetRay.position, targetRay.rotation, targetRay.scale ); } } if ( grip !== null && inputSource.gripSpace ) { gripPose = frame.getPose( inputSource.gripSpace, referenceSpace ); if ( gripPose !== null ) { grip.matrix.fromArray( gripPose.transform.matrix ); grip.matrix.decompose( grip.position, grip.rotation, grip.scale ); } } } } if ( targetRay !== null ) { targetRay.visible = ( inputPose !== null ); } if ( grip !== null ) { grip.visible = ( gripPose !== null ); } if ( hand !== null ) { hand.visible = ( handPose !== null ); } return this; } } ); function WebXRManager( renderer, gl ) { const scope = this; let session = null; let framebufferScaleFactor = 1.0; let referenceSpace = null; let referenceSpaceType = 'local-floor'; let pose = null; const controllers = []; const inputSourcesMap = new Map(); // const cameraL = new PerspectiveCamera(); cameraL.layers.enable( 1 ); cameraL.viewport = new Vector4(); const cameraR = new PerspectiveCamera(); cameraR.layers.enable( 2 ); cameraR.viewport = new Vector4(); const cameras = [ cameraL, cameraR ]; const cameraVR = new ArrayCamera(); cameraVR.layers.enable( 1 ); cameraVR.layers.enable( 2 ); let _currentDepthNear = null; let _currentDepthFar = null; // this.enabled = false; this.isPresenting = false; this.getController = function ( index ) { let controller = controllers[ index ]; if ( controller === undefined ) { controller = new WebXRController(); controllers[ index ] = controller; } return controller.getTargetRaySpace(); }; this.getControllerGrip = function ( index ) { let controller = controllers[ index ]; if ( controller === undefined ) { controller = new WebXRController(); controllers[ index ] = controller; } return controller.getGripSpace(); }; this.getHand = function ( index ) { let controller = controllers[ index ]; if ( controller === undefined ) { controller = new WebXRController(); controllers[ index ] = controller; } return controller.getHandSpace(); }; // function onSessionEvent( event ) { const controller = inputSourcesMap.get( event.inputSource ); if ( controller ) { controller.dispatchEvent( { type: event.type, data: event.inputSource } ); } } function onSessionEnd() { inputSourcesMap.forEach( function ( controller, inputSource ) { controller.disconnect( inputSource ); } ); inputSourcesMap.clear(); // renderer.setFramebuffer( null ); renderer.setRenderTarget( renderer.getRenderTarget() ); // Hack #15830 animation.stop(); scope.isPresenting = false; scope.dispatchEvent( { type: 'sessionend' } ); } function onRequestReferenceSpace( value ) { referenceSpace = value; animation.setContext( session ); animation.start(); scope.isPresenting = true; scope.dispatchEvent( { type: 'sessionstart' } ); } this.setFramebufferScaleFactor = function ( value ) { framebufferScaleFactor = value; if ( scope.isPresenting === true ) { console.warn( 'THREE.WebXRManager: Cannot change framebuffer scale while presenting.' ); } }; this.setReferenceSpaceType = function ( value ) { referenceSpaceType = value; if ( scope.isPresenting === true ) { console.warn( 'THREE.WebXRManager: Cannot change reference space type while presenting.' ); } }; this.getReferenceSpace = function () { return referenceSpace; }; this.getSession = function () { return session; }; this.setSession = function ( value ) { session = value; if ( session !== null ) { session.addEventListener( 'select', onSessionEvent ); session.addEventListener( 'selectstart', onSessionEvent ); session.addEventListener( 'selectend', onSessionEvent ); session.addEventListener( 'squeeze', onSessionEvent ); session.addEventListener( 'squeezestart', onSessionEvent ); session.addEventListener( 'squeezeend', onSessionEvent ); session.addEventListener( 'end', onSessionEnd ); const attributes = gl.getContextAttributes(); if ( attributes.xrCompatible !== true ) { gl.makeXRCompatible(); } const layerInit = { antialias: attributes.antialias, alpha: attributes.alpha, depth: attributes.depth, stencil: attributes.stencil, framebufferScaleFactor: framebufferScaleFactor }; // eslint-disable-next-line no-undef const baseLayer = new XRWebGLLayer( session, gl, layerInit ); session.updateRenderState( { baseLayer: baseLayer } ); session.requestReferenceSpace( referenceSpaceType ).then( onRequestReferenceSpace ); // session.addEventListener( 'inputsourceschange', updateInputSources ); } }; function updateInputSources( event ) { const inputSources = session.inputSources; // Assign inputSources to available controllers for ( let i = 0; i < controllers.length; i ++ ) { inputSourcesMap.set( inputSources[ i ], controllers[ i ] ); } // Notify disconnected for ( let i = 0; i < event.removed.length; i ++ ) { const inputSource = event.removed[ i ]; const controller = inputSourcesMap.get( inputSource ); if ( controller ) { controller.dispatchEvent( { type: 'disconnected', data: inputSource } ); inputSourcesMap.delete( inputSource ); } } // Notify connected for ( let i = 0; i < event.added.length; i ++ ) { const inputSource = event.added[ i ]; const controller = inputSourcesMap.get( inputSource ); if ( controller ) { controller.dispatchEvent( { type: 'connected', data: inputSource } ); } } } // const cameraLPos = new Vector3(); const cameraRPos = new Vector3(); /** * Assumes 2 cameras that are parallel and share an X-axis, and that * the cameras' projection and world matrices have already been set. * And that near and far planes are identical for both cameras. * Visualization of this technique: https://computergraphics.stackexchange.com/a/4765 */ function setProjectionFromUnion( camera, cameraL, cameraR ) { cameraLPos.setFromMatrixPosition( cameraL.matrixWorld ); cameraRPos.setFromMatrixPosition( cameraR.matrixWorld ); const ipd = cameraLPos.distanceTo( cameraRPos ); const projL = cameraL.projectionMatrix.elements; const projR = cameraR.projectionMatrix.elements; // VR systems will have identical far and near planes, and // most likely identical top and bottom frustum extents. // Use the left camera for these values. const near = projL[ 14 ] / ( projL[ 10 ] - 1 ); const far = projL[ 14 ] / ( projL[ 10 ] + 1 ); const topFov = ( projL[ 9 ] + 1 ) / projL[ 5 ]; const bottomFov = ( projL[ 9 ] - 1 ) / projL[ 5 ]; const leftFov = ( projL[ 8 ] - 1 ) / projL[ 0 ]; const rightFov = ( projR[ 8 ] + 1 ) / projR[ 0 ]; const left = near * leftFov; const right = near * rightFov; // Calculate the new camera's position offset from the // left camera. xOffset should be roughly half `ipd`. const zOffset = ipd / ( - leftFov + rightFov ); const xOffset = zOffset * - leftFov; // TODO: Better way to apply this offset? cameraL.matrixWorld.decompose( camera.position, camera.quaternion, camera.scale ); camera.translateX( xOffset ); camera.translateZ( zOffset ); camera.matrixWorld.compose( camera.position, camera.quaternion, camera.scale ); camera.matrixWorldInverse.copy( camera.matrixWorld ).invert(); // Find the union of the frustum values of the cameras and scale // the values so that the near plane's position does not change in world space, // although must now be relative to the new union camera. const near2 = near + zOffset; const far2 = far + zOffset; const left2 = left - xOffset; const right2 = right + ( ipd - xOffset ); const top2 = topFov * far / far2 * near2; const bottom2 = bottomFov * far / far2 * near2; camera.projectionMatrix.makePerspective( left2, right2, top2, bottom2, near2, far2 ); } function updateCamera( camera, parent ) { if ( parent === null ) { camera.matrixWorld.copy( camera.matrix ); } else { camera.matrixWorld.multiplyMatrices( parent.matrixWorld, camera.matrix ); } camera.matrixWorldInverse.copy( camera.matrixWorld ).invert(); } this.getCamera = function ( camera ) { cameraVR.near = cameraR.near = cameraL.near = camera.near; cameraVR.far = cameraR.far = cameraL.far = camera.far; if ( _currentDepthNear !== cameraVR.near || _currentDepthFar !== cameraVR.far ) { // Note that the new renderState won't apply until the next frame. See #18320 session.updateRenderState( { depthNear: cameraVR.near, depthFar: cameraVR.far } ); _currentDepthNear = cameraVR.near; _currentDepthFar = cameraVR.far; } const parent = camera.parent; const cameras = cameraVR.cameras; updateCamera( cameraVR, parent ); for ( let i = 0; i < cameras.length; i ++ ) { updateCamera( cameras[ i ], parent ); } // update camera and its children camera.matrixWorld.copy( cameraVR.matrixWorld ); const children = camera.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].updateMatrixWorld( true ); } // update projection matrix for proper view frustum culling if ( cameras.length === 2 ) { setProjectionFromUnion( cameraVR, cameraL, cameraR ); } else { // assume single camera setup (AR) cameraVR.projectionMatrix.copy( cameraL.projectionMatrix ); } return cameraVR; }; // Animation Loop let onAnimationFrameCallback = null; function onAnimationFrame( time, frame ) { pose = frame.getViewerPose( referenceSpace ); if ( pose !== null ) { const views = pose.views; const baseLayer = session.renderState.baseLayer; renderer.setFramebuffer( baseLayer.framebuffer ); let cameraVRNeedsUpdate = false; // check if it's necessary to rebuild cameraVR's camera list if ( views.length !== cameraVR.cameras.length ) { cameraVR.cameras.length = 0; cameraVRNeedsUpdate = true; } for ( let i = 0; i < views.length; i ++ ) { const view = views[ i ]; const viewport = baseLayer.getViewport( view ); const camera = cameras[ i ]; camera.matrix.fromArray( view.transform.matrix ); camera.projectionMatrix.fromArray( view.projectionMatrix ); camera.viewport.set( viewport.x, viewport.y, viewport.width, viewport.height ); if ( i === 0 ) { cameraVR.matrix.copy( camera.matrix ); } if ( cameraVRNeedsUpdate === true ) { cameraVR.cameras.push( camera ); } } } // const inputSources = session.inputSources; for ( let i = 0; i < controllers.length; i ++ ) { const controller = controllers[ i ]; const inputSource = inputSources[ i ]; controller.update( inputSource, frame, referenceSpace ); } if ( onAnimationFrameCallback ) onAnimationFrameCallback( time, frame ); } const animation = new WebGLAnimation(); animation.setAnimationLoop( onAnimationFrame ); this.setAnimationLoop = function ( callback ) { onAnimationFrameCallback = callback; }; this.dispose = function () {}; } Object.assign( WebXRManager.prototype, EventDispatcher.prototype ); function WebGLMaterials( properties ) { function refreshFogUniforms( uniforms, fog ) { uniforms.fogColor.value.copy( fog.color ); if ( fog.isFog ) { uniforms.fogNear.value = fog.near; uniforms.fogFar.value = fog.far; } else if ( fog.isFogExp2 ) { uniforms.fogDensity.value = fog.density; } } function refreshMaterialUniforms( uniforms, material, pixelRatio, height ) { if ( material.isMeshBasicMaterial ) { refreshUniformsCommon( uniforms, material ); } else if ( material.isMeshLambertMaterial ) { refreshUniformsCommon( uniforms, material ); refreshUniformsLambert( uniforms, material ); } else if ( material.isMeshToonMaterial ) { refreshUniformsCommon( uniforms, material ); refreshUniformsToon( uniforms, material ); } else if ( material.isMeshPhongMaterial ) { refreshUniformsCommon( uniforms, material ); refreshUniformsPhong( uniforms, material ); } else if ( material.isMeshStandardMaterial ) { refreshUniformsCommon( uniforms, material ); if ( material.isMeshPhysicalMaterial ) { refreshUniformsPhysical( uniforms, material ); } else { refreshUniformsStandard( uniforms, material ); } } else if ( material.isMeshMatcapMaterial ) { refreshUniformsCommon( uniforms, material ); refreshUniformsMatcap( uniforms, material ); } else if ( material.isMeshDepthMaterial ) { refreshUniformsCommon( uniforms, material ); refreshUniformsDepth( uniforms, material ); } else if ( material.isMeshDistanceMaterial ) { refreshUniformsCommon( uniforms, material ); refreshUniformsDistance( uniforms, material ); } else if ( material.isMeshNormalMaterial ) { refreshUniformsCommon( uniforms, material ); refreshUniformsNormal( uniforms, material ); } else if ( material.isLineBasicMaterial ) { refreshUniformsLine( uniforms, material ); if ( material.isLineDashedMaterial ) { refreshUniformsDash( uniforms, material ); } } else if ( material.isPointsMaterial ) { refreshUniformsPoints( uniforms, material, pixelRatio, height ); } else if ( material.isSpriteMaterial ) { refreshUniformsSprites( uniforms, material ); } else if ( material.isShadowMaterial ) { uniforms.color.value.copy( material.color ); uniforms.opacity.value = material.opacity; } else if ( material.isShaderMaterial ) { material.uniformsNeedUpdate = false; // #15581 } } function refreshUniformsCommon( uniforms, material ) { uniforms.opacity.value = material.opacity; if ( material.color ) { uniforms.diffuse.value.copy( material.color ); } if ( material.emissive ) { uniforms.emissive.value.copy( material.emissive ).multiplyScalar( material.emissiveIntensity ); } if ( material.map ) { uniforms.map.value = material.map; } if ( material.alphaMap ) { uniforms.alphaMap.value = material.alphaMap; } if ( material.specularMap ) { uniforms.specularMap.value = material.specularMap; } const envMap = properties.get( material ).envMap; if ( envMap ) { uniforms.envMap.value = envMap; uniforms.flipEnvMap.value = ( envMap.isCubeTexture && envMap._needsFlipEnvMap ) ? - 1 : 1; uniforms.reflectivity.value = material.reflectivity; uniforms.refractionRatio.value = material.refractionRatio; const maxMipLevel = properties.get( envMap ).__maxMipLevel; if ( maxMipLevel !== undefined ) { uniforms.maxMipLevel.value = maxMipLevel; } } if ( material.lightMap ) { uniforms.lightMap.value = material.lightMap; uniforms.lightMapIntensity.value = material.lightMapIntensity; } if ( material.aoMap ) { uniforms.aoMap.value = material.aoMap; uniforms.aoMapIntensity.value = material.aoMapIntensity; } // uv repeat and offset setting priorities // 1. color map // 2. specular map // 3. displacementMap map // 4. normal map // 5. bump map // 6. roughnessMap map // 7. metalnessMap map // 8. alphaMap map // 9. emissiveMap map // 10. clearcoat map // 11. clearcoat normal map // 12. clearcoat roughnessMap map let uvScaleMap; if ( material.map ) { uvScaleMap = material.map; } else if ( material.specularMap ) { uvScaleMap = material.specularMap; } else if ( material.displacementMap ) { uvScaleMap = material.displacementMap; } else if ( material.normalMap ) { uvScaleMap = material.normalMap; } else if ( material.bumpMap ) { uvScaleMap = material.bumpMap; } else if ( material.roughnessMap ) { uvScaleMap = material.roughnessMap; } else if ( material.metalnessMap ) { uvScaleMap = material.metalnessMap; } else if ( material.alphaMap ) { uvScaleMap = material.alphaMap; } else if ( material.emissiveMap ) { uvScaleMap = material.emissiveMap; } else if ( material.clearcoatMap ) { uvScaleMap = material.clearcoatMap; } else if ( material.clearcoatNormalMap ) { uvScaleMap = material.clearcoatNormalMap; } else if ( material.clearcoatRoughnessMap ) { uvScaleMap = material.clearcoatRoughnessMap; } if ( uvScaleMap !== undefined ) { // backwards compatibility if ( uvScaleMap.isWebGLRenderTarget ) { uvScaleMap = uvScaleMap.texture; } if ( uvScaleMap.matrixAutoUpdate === true ) { uvScaleMap.updateMatrix(); } uniforms.uvTransform.value.copy( uvScaleMap.matrix ); } // uv repeat and offset setting priorities for uv2 // 1. ao map // 2. light map let uv2ScaleMap; if ( material.aoMap ) { uv2ScaleMap = material.aoMap; } else if ( material.lightMap ) { uv2ScaleMap = material.lightMap; } if ( uv2ScaleMap !== undefined ) { // backwards compatibility if ( uv2ScaleMap.isWebGLRenderTarget ) { uv2ScaleMap = uv2ScaleMap.texture; } if ( uv2ScaleMap.matrixAutoUpdate === true ) { uv2ScaleMap.updateMatrix(); } uniforms.uv2Transform.value.copy( uv2ScaleMap.matrix ); } } function refreshUniformsLine( uniforms, material ) { uniforms.diffuse.value.copy( material.color ); uniforms.opacity.value = material.opacity; } function refreshUniformsDash( uniforms, material ) { uniforms.dashSize.value = material.dashSize; uniforms.totalSize.value = material.dashSize + material.gapSize; uniforms.scale.value = material.scale; } function refreshUniformsPoints( uniforms, material, pixelRatio, height ) { uniforms.diffuse.value.copy( material.color ); uniforms.opacity.value = material.opacity; uniforms.size.value = material.size * pixelRatio; uniforms.scale.value = height * 0.5; if ( material.map ) { uniforms.map.value = material.map; } if ( material.alphaMap ) { uniforms.alphaMap.value = material.alphaMap; } // uv repeat and offset setting priorities // 1. color map // 2. alpha map let uvScaleMap; if ( material.map ) { uvScaleMap = material.map; } else if ( material.alphaMap ) { uvScaleMap = material.alphaMap; } if ( uvScaleMap !== undefined ) { if ( uvScaleMap.matrixAutoUpdate === true ) { uvScaleMap.updateMatrix(); } uniforms.uvTransform.value.copy( uvScaleMap.matrix ); } } function refreshUniformsSprites( uniforms, material ) { uniforms.diffuse.value.copy( material.color ); uniforms.opacity.value = material.opacity; uniforms.rotation.value = material.rotation; if ( material.map ) { uniforms.map.value = material.map; } if ( material.alphaMap ) { uniforms.alphaMap.value = material.alphaMap; } // uv repeat and offset setting priorities // 1. color map // 2. alpha map let uvScaleMap; if ( material.map ) { uvScaleMap = material.map; } else if ( material.alphaMap ) { uvScaleMap = material.alphaMap; } if ( uvScaleMap !== undefined ) { if ( uvScaleMap.matrixAutoUpdate === true ) { uvScaleMap.updateMatrix(); } uniforms.uvTransform.value.copy( uvScaleMap.matrix ); } } function refreshUniformsLambert( uniforms, material ) { if ( material.emissiveMap ) { uniforms.emissiveMap.value = material.emissiveMap; } } function refreshUniformsPhong( uniforms, material ) { uniforms.specular.value.copy( material.specular ); uniforms.shininess.value = Math.max( material.shininess, 1e-4 ); // to prevent pow( 0.0, 0.0 ) if ( material.emissiveMap ) { uniforms.emissiveMap.value = material.emissiveMap; } if ( material.bumpMap ) { uniforms.bumpMap.value = material.bumpMap; uniforms.bumpScale.value = material.bumpScale; if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1; } if ( material.normalMap ) { uniforms.normalMap.value = material.normalMap; uniforms.normalScale.value.copy( material.normalScale ); if ( material.side === BackSide ) uniforms.normalScale.value.negate(); } if ( material.displacementMap ) { uniforms.displacementMap.value = material.displacementMap; uniforms.displacementScale.value = material.displacementScale; uniforms.displacementBias.value = material.displacementBias; } } function refreshUniformsToon( uniforms, material ) { if ( material.gradientMap ) { uniforms.gradientMap.value = material.gradientMap; } if ( material.emissiveMap ) { uniforms.emissiveMap.value = material.emissiveMap; } if ( material.bumpMap ) { uniforms.bumpMap.value = material.bumpMap; uniforms.bumpScale.value = material.bumpScale; if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1; } if ( material.normalMap ) { uniforms.normalMap.value = material.normalMap; uniforms.normalScale.value.copy( material.normalScale ); if ( material.side === BackSide ) uniforms.normalScale.value.negate(); } if ( material.displacementMap ) { uniforms.displacementMap.value = material.displacementMap; uniforms.displacementScale.value = material.displacementScale; uniforms.displacementBias.value = material.displacementBias; } } function refreshUniformsStandard( uniforms, material ) { uniforms.roughness.value = material.roughness; uniforms.metalness.value = material.metalness; if ( material.roughnessMap ) { uniforms.roughnessMap.value = material.roughnessMap; } if ( material.metalnessMap ) { uniforms.metalnessMap.value = material.metalnessMap; } if ( material.emissiveMap ) { uniforms.emissiveMap.value = material.emissiveMap; } if ( material.bumpMap ) { uniforms.bumpMap.value = material.bumpMap; uniforms.bumpScale.value = material.bumpScale; if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1; } if ( material.normalMap ) { uniforms.normalMap.value = material.normalMap; uniforms.normalScale.value.copy( material.normalScale ); if ( material.side === BackSide ) uniforms.normalScale.value.negate(); } if ( material.displacementMap ) { uniforms.displacementMap.value = material.displacementMap; uniforms.displacementScale.value = material.displacementScale; uniforms.displacementBias.value = material.displacementBias; } const envMap = properties.get( material ).envMap; if ( envMap ) { //uniforms.envMap.value = material.envMap; // part of uniforms common uniforms.envMapIntensity.value = material.envMapIntensity; } } function refreshUniformsPhysical( uniforms, material ) { refreshUniformsStandard( uniforms, material ); uniforms.reflectivity.value = material.reflectivity; // also part of uniforms common uniforms.clearcoat.value = material.clearcoat; uniforms.clearcoatRoughness.value = material.clearcoatRoughness; if ( material.sheen ) uniforms.sheen.value.copy( material.sheen ); if ( material.clearcoatMap ) { uniforms.clearcoatMap.value = material.clearcoatMap; } if ( material.clearcoatRoughnessMap ) { uniforms.clearcoatRoughnessMap.value = material.clearcoatRoughnessMap; } if ( material.clearcoatNormalMap ) { uniforms.clearcoatNormalScale.value.copy( material.clearcoatNormalScale ); uniforms.clearcoatNormalMap.value = material.clearcoatNormalMap; if ( material.side === BackSide ) { uniforms.clearcoatNormalScale.value.negate(); } } uniforms.transmission.value = material.transmission; if ( material.transmissionMap ) { uniforms.transmissionMap.value = material.transmissionMap; } } function refreshUniformsMatcap( uniforms, material ) { if ( material.matcap ) { uniforms.matcap.value = material.matcap; } if ( material.bumpMap ) { uniforms.bumpMap.value = material.bumpMap; uniforms.bumpScale.value = material.bumpScale; if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1; } if ( material.normalMap ) { uniforms.normalMap.value = material.normalMap; uniforms.normalScale.value.copy( material.normalScale ); if ( material.side === BackSide ) uniforms.normalScale.value.negate(); } if ( material.displacementMap ) { uniforms.displacementMap.value = material.displacementMap; uniforms.displacementScale.value = material.displacementScale; uniforms.displacementBias.value = material.displacementBias; } } function refreshUniformsDepth( uniforms, material ) { if ( material.displacementMap ) { uniforms.displacementMap.value = material.displacementMap; uniforms.displacementScale.value = material.displacementScale; uniforms.displacementBias.value = material.displacementBias; } } function refreshUniformsDistance( uniforms, material ) { if ( material.displacementMap ) { uniforms.displacementMap.value = material.displacementMap; uniforms.displacementScale.value = material.displacementScale; uniforms.displacementBias.value = material.displacementBias; } uniforms.referencePosition.value.copy( material.referencePosition ); uniforms.nearDistance.value = material.nearDistance; uniforms.farDistance.value = material.farDistance; } function refreshUniformsNormal( uniforms, material ) { if ( material.bumpMap ) { uniforms.bumpMap.value = material.bumpMap; uniforms.bumpScale.value = material.bumpScale; if ( material.side === BackSide ) uniforms.bumpScale.value *= - 1; } if ( material.normalMap ) { uniforms.normalMap.value = material.normalMap; uniforms.normalScale.value.copy( material.normalScale ); if ( material.side === BackSide ) uniforms.normalScale.value.negate(); } if ( material.displacementMap ) { uniforms.displacementMap.value = material.displacementMap; uniforms.displacementScale.value = material.displacementScale; uniforms.displacementBias.value = material.displacementBias; } } return { refreshFogUniforms: refreshFogUniforms, refreshMaterialUniforms: refreshMaterialUniforms }; } function createCanvasElement() { const canvas = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'canvas' ); canvas.style.display = 'block'; return canvas; } function WebGLRenderer( parameters ) { parameters = parameters || {}; const _canvas = parameters.canvas !== undefined ? parameters.canvas : createCanvasElement(), _context = parameters.context !== undefined ? parameters.context : null, _alpha = parameters.alpha !== undefined ? parameters.alpha : false, _depth = parameters.depth !== undefined ? parameters.depth : true, _stencil = parameters.stencil !== undefined ? parameters.stencil : true, _antialias = parameters.antialias !== undefined ? parameters.antialias : false, _premultipliedAlpha = parameters.premultipliedAlpha !== undefined ? parameters.premultipliedAlpha : true, _preserveDrawingBuffer = parameters.preserveDrawingBuffer !== undefined ? parameters.preserveDrawingBuffer : false, _powerPreference = parameters.powerPreference !== undefined ? parameters.powerPreference : 'default', _failIfMajorPerformanceCaveat = parameters.failIfMajorPerformanceCaveat !== undefined ? parameters.failIfMajorPerformanceCaveat : false; let currentRenderList = null; let currentRenderState = null; // render() can be called from within a callback triggered by another render. // We track this so that the nested render call gets its state isolated from the parent render call. const renderStateStack = []; // public properties this.domElement = _canvas; // Debug configuration container this.debug = { /** * Enables error checking and reporting when shader programs are being compiled * @type {boolean} */ checkShaderErrors: true }; // clearing this.autoClear = true; this.autoClearColor = true; this.autoClearDepth = true; this.autoClearStencil = true; // scene graph this.sortObjects = true; // user-defined clipping this.clippingPlanes = []; this.localClippingEnabled = false; // physically based shading this.gammaFactor = 2.0; // for backwards compatibility this.outputEncoding = LinearEncoding; // physical lights this.physicallyCorrectLights = false; // tone mapping this.toneMapping = NoToneMapping; this.toneMappingExposure = 1.0; // morphs this.maxMorphTargets = 8; this.maxMorphNormals = 4; // internal properties const _this = this; let _isContextLost = false; // internal state cache let _framebuffer = null; let _currentActiveCubeFace = 0; let _currentActiveMipmapLevel = 0; let _currentRenderTarget = null; let _currentFramebuffer = null; let _currentMaterialId = - 1; let _currentCamera = null; const _currentViewport = new Vector4(); const _currentScissor = new Vector4(); let _currentScissorTest = null; // let _width = _canvas.width; let _height = _canvas.height; let _pixelRatio = 1; let _opaqueSort = null; let _transparentSort = null; const _viewport = new Vector4( 0, 0, _width, _height ); const _scissor = new Vector4( 0, 0, _width, _height ); let _scissorTest = false; // frustum const _frustum = new Frustum(); // clipping let _clippingEnabled = false; let _localClippingEnabled = false; // camera matrices cache const _projScreenMatrix = new Matrix4(); const _vector3 = new Vector3(); const _emptyScene = { background: null, fog: null, environment: null, overrideMaterial: null, isScene: true }; function getTargetPixelRatio() { return _currentRenderTarget === null ? _pixelRatio : 1; } // initialize let _gl = _context; function getContext( contextNames, contextAttributes ) { for ( let i = 0; i < contextNames.length; i ++ ) { const contextName = contextNames[ i ]; const context = _canvas.getContext( contextName, contextAttributes ); if ( context !== null ) return context; } return null; } try { const contextAttributes = { alpha: _alpha, depth: _depth, stencil: _stencil, antialias: _antialias, premultipliedAlpha: _premultipliedAlpha, preserveDrawingBuffer: _preserveDrawingBuffer, powerPreference: _powerPreference, failIfMajorPerformanceCaveat: _failIfMajorPerformanceCaveat }; // event listeners must be registered before WebGL context is created, see #12753 _canvas.addEventListener( 'webglcontextlost', onContextLost, false ); _canvas.addEventListener( 'webglcontextrestored', onContextRestore, false ); if ( _gl === null ) { const contextNames = [ 'webgl2', 'webgl', 'experimental-webgl' ]; if ( _this.isWebGL1Renderer === true ) { contextNames.shift(); } _gl = getContext( contextNames, contextAttributes ); if ( _gl === null ) { if ( getContext( contextNames ) ) { throw new Error( 'Error creating WebGL context with your selected attributes.' ); } else { throw new Error( 'Error creating WebGL context.' ); } } } // Some experimental-webgl implementations do not have getShaderPrecisionFormat if ( _gl.getShaderPrecisionFormat === undefined ) { _gl.getShaderPrecisionFormat = function () { return { 'rangeMin': 1, 'rangeMax': 1, 'precision': 1 }; }; } } catch ( error ) { console.error( 'THREE.WebGLRenderer: ' + error.message ); throw error; } let extensions, capabilities, state, info; let properties, textures, cubemaps, attributes, geometries, objects; let programCache, materials, renderLists, renderStates, clipping; let background, morphtargets, bufferRenderer, indexedBufferRenderer; let utils, bindingStates; function initGLContext() { extensions = new WebGLExtensions( _gl ); capabilities = new WebGLCapabilities( _gl, extensions, parameters ); if ( capabilities.isWebGL2 === false ) { extensions.get( 'WEBGL_depth_texture' ); extensions.get( 'OES_texture_float' ); extensions.get( 'OES_texture_half_float' ); extensions.get( 'OES_texture_half_float_linear' ); extensions.get( 'OES_standard_derivatives' ); extensions.get( 'OES_element_index_uint' ); extensions.get( 'OES_vertex_array_object' ); extensions.get( 'ANGLE_instanced_arrays' ); } extensions.get( 'OES_texture_float_linear' ); utils = new WebGLUtils( _gl, extensions, capabilities ); state = new WebGLState( _gl, extensions, capabilities ); state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() ); state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() ); info = new WebGLInfo( _gl ); properties = new WebGLProperties(); textures = new WebGLTextures( _gl, extensions, state, properties, capabilities, utils, info ); cubemaps = new WebGLCubeMaps( _this ); attributes = new WebGLAttributes( _gl, capabilities ); bindingStates = new WebGLBindingStates( _gl, extensions, attributes, capabilities ); geometries = new WebGLGeometries( _gl, attributes, info, bindingStates ); objects = new WebGLObjects( _gl, geometries, attributes, info ); morphtargets = new WebGLMorphtargets( _gl ); clipping = new WebGLClipping( properties ); programCache = new WebGLPrograms( _this, cubemaps, extensions, capabilities, bindingStates, clipping ); materials = new WebGLMaterials( properties ); renderLists = new WebGLRenderLists( properties ); renderStates = new WebGLRenderStates( extensions, capabilities ); background = new WebGLBackground( _this, cubemaps, state, objects, _premultipliedAlpha ); bufferRenderer = new WebGLBufferRenderer( _gl, extensions, info, capabilities ); indexedBufferRenderer = new WebGLIndexedBufferRenderer( _gl, extensions, info, capabilities ); info.programs = programCache.programs; _this.capabilities = capabilities; _this.extensions = extensions; _this.properties = properties; _this.renderLists = renderLists; _this.state = state; _this.info = info; _this._textures = textures;//add } initGLContext(); // xr const xr = new WebXRManager( _this, _gl ); this.xr = xr; // shadow map const shadowMap = new WebGLShadowMap( _this, objects, capabilities.maxTextureSize ); this.shadowMap = shadowMap; // API this.getContext = function () { return _gl; }; this.getContextAttributes = function () { return _gl.getContextAttributes(); }; this.forceContextLoss = function () { const extension = extensions.get( 'WEBGL_lose_context' ); if ( extension ) extension.loseContext(); }; this.forceContextRestore = function () { const extension = extensions.get( 'WEBGL_lose_context' ); if ( extension ) extension.restoreContext(); }; this.getPixelRatio = function () { return _pixelRatio; }; this.setPixelRatio = function ( value ) { if ( value === undefined ) return; _pixelRatio = value; this.setSize( _width, _height, false ); }; this.getSize = function ( target ) { if ( target === undefined ) { console.warn( 'WebGLRenderer: .getsize() now requires a Vector2 as an argument' ); target = new Vector2(); } return target.set( _width, _height ); }; this.setSize = function ( width, height, updateStyle ) { if ( xr.isPresenting ) { console.warn( 'THREE.WebGLRenderer: Can\'t change size while VR device is presenting.' ); return; } _width = width; _height = height; _canvas.width = Math.floor( width * _pixelRatio ); _canvas.height = Math.floor( height * _pixelRatio ); if ( updateStyle !== false ) { _canvas.style.width = width + 'px'; _canvas.style.height = height + 'px'; } this.setViewport( 0, 0, width, height ); }; this.getDrawingBufferSize = function ( target ) { if ( target === undefined ) { console.warn( 'WebGLRenderer: .getdrawingBufferSize() now requires a Vector2 as an argument' ); target = new Vector2(); } return target.set( _width * _pixelRatio, _height * _pixelRatio ).floor(); }; this.setDrawingBufferSize = function ( width, height, pixelRatio ) { _width = width; _height = height; _pixelRatio = pixelRatio; _canvas.width = Math.floor( width * pixelRatio ); _canvas.height = Math.floor( height * pixelRatio ); this.setViewport( 0, 0, width, height ); }; this.getCurrentViewport = function ( target ) { if ( target === undefined ) { console.warn( 'WebGLRenderer: .getCurrentViewport() now requires a Vector4 as an argument' ); target = new Vector4(); } return target.copy( _currentViewport ); }; this.getViewport = function ( target ) { return target.copy( _viewport ); }; this.setViewport = function ( x, y, width, height ) { if ( x.isVector4 ) { _viewport.set( x.x, x.y, x.z, x.w ); } else { _viewport.set( x, y, width, height ); } state.viewport( _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor() ); }; this.getScissor = function ( target ) { return target.copy( _scissor ); }; this.setScissor = function ( x, y, width, height ) { if ( x.isVector4 ) { _scissor.set( x.x, x.y, x.z, x.w ); } else { _scissor.set( x, y, width, height ); } state.scissor( _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor() ); }; this.getScissorTest = function () { return _scissorTest; }; this.setScissorTest = function ( boolean ) { state.setScissorTest( _scissorTest = boolean ); }; this.setOpaqueSort = function ( method ) { _opaqueSort = method; }; this.setTransparentSort = function ( method ) { _transparentSort = method; }; // Clearing this.getClearColor = function ( target ) { if ( target === undefined ) { console.warn( 'WebGLRenderer: .getClearColor() now requires a Color as an argument' ); target = new Color(); } return target.copy( background.getClearColor() ); }; this.setClearColor = function () { background.setClearColor.apply( background, arguments ); }; this.getClearAlpha = function () { return background.getClearAlpha(); }; this.setClearAlpha = function () { background.setClearAlpha.apply( background, arguments ); }; this.clear = function ( color, depth, stencil ) { let bits = 0; if ( color === undefined || color ) bits |= 16384; if ( depth === undefined || depth ) bits |= 256; if ( stencil === undefined || stencil ) bits |= 1024; _gl.clear( bits ); }; this.clearColor = function () { this.clear( true, false, false ); }; this.clearDepth = function () { this.clear( false, true, false ); }; this.clearStencil = function () { this.clear( false, false, true ); }; // this.dispose = function () { _canvas.removeEventListener( 'webglcontextlost', onContextLost, false ); _canvas.removeEventListener( 'webglcontextrestored', onContextRestore, false ); renderLists.dispose(); renderStates.dispose(); properties.dispose(); cubemaps.dispose(); objects.dispose(); bindingStates.dispose(); xr.dispose(); animation.stop(); }; // Events function onContextLost( event ) { event.preventDefault(); console.log( 'THREE.WebGLRenderer: Context Lost.' ); _isContextLost = true; } function onContextRestore( /* event */ ) { console.log( 'THREE.WebGLRenderer: Context Restored.' ); _isContextLost = false; initGLContext(); } function onMaterialDispose( event ) { const material = event.target; material.removeEventListener( 'dispose', onMaterialDispose ); deallocateMaterial( material ); } // Buffer deallocation function deallocateMaterial( material ) { releaseMaterialProgramReference( material ); properties.remove( material ); } function releaseMaterialProgramReference( material ) { const programInfo = properties.get( material ).program; if ( programInfo !== undefined ) { programCache.releaseProgram( programInfo ); } } // Buffer rendering function renderObjectImmediate( object, program ) { object.render( function ( object ) { _this.renderBufferImmediate( object, program ); } ); } this.renderBufferImmediate = function ( object, program ) { bindingStates.initAttributes(); const buffers = properties.get( object ); if ( object.hasPositions && ! buffers.position ) buffers.position = _gl.createBuffer(); if ( object.hasNormals && ! buffers.normal ) buffers.normal = _gl.createBuffer(); if ( object.hasUvs && ! buffers.uv ) buffers.uv = _gl.createBuffer(); if ( object.hasColors && ! buffers.color ) buffers.color = _gl.createBuffer(); const programAttributes = program.getAttributes(); if ( object.hasPositions ) { _gl.bindBuffer( 34962, buffers.position ); _gl.bufferData( 34962, object.positionArray, 35048 ); bindingStates.enableAttribute( programAttributes.position ); _gl.vertexAttribPointer( programAttributes.position, 3, 5126, false, 0, 0 ); } if ( object.hasNormals ) { _gl.bindBuffer( 34962, buffers.normal ); _gl.bufferData( 34962, object.normalArray, 35048 ); bindingStates.enableAttribute( programAttributes.normal ); _gl.vertexAttribPointer( programAttributes.normal, 3, 5126, false, 0, 0 ); } if ( object.hasUvs ) { _gl.bindBuffer( 34962, buffers.uv ); _gl.bufferData( 34962, object.uvArray, 35048 ); bindingStates.enableAttribute( programAttributes.uv ); _gl.vertexAttribPointer( programAttributes.uv, 2, 5126, false, 0, 0 ); } if ( object.hasColors ) { _gl.bindBuffer( 34962, buffers.color ); _gl.bufferData( 34962, object.colorArray, 35048 ); bindingStates.enableAttribute( programAttributes.color ); _gl.vertexAttribPointer( programAttributes.color, 3, 5126, false, 0, 0 ); } bindingStates.disableUnusedAttributes(); _gl.drawArrays( 4, 0, object.count ); object.count = 0; }; this.renderBufferDirect = function ( camera, scene, geometry, material, object, group ) { if ( scene === null ) scene = _emptyScene; // renderBufferDirect second parameter used to be fog (could be null) const frontFaceCW = ( object.isMesh && object.matrixWorld.determinant() < 0 ); const program = setProgram( camera, scene, material, object ); state.setMaterial( material, frontFaceCW ); // let index = geometry.index; const position = geometry.attributes.position; // if ( index === null ) { if ( position === undefined || position.count === 0 ) return; } else if ( index.count === 0 ) { return; } // let rangeFactor = 1; if ( material.wireframe === true ) { index = geometries.getWireframeAttribute( geometry ); rangeFactor = 2; } if ( material.morphTargets || material.morphNormals ) { morphtargets.update( object, geometry, material, program ); } bindingStates.setup( object, material, program, geometry, index ); let attribute; let renderer = bufferRenderer; if ( index !== null ) { attribute = attributes.get( index ); renderer = indexedBufferRenderer; renderer.setIndex( attribute ); } // const dataCount = ( index !== null ) ? index.count : position.count; const rangeStart = geometry.drawRange.start * rangeFactor; const rangeCount = geometry.drawRange.count * rangeFactor; const groupStart = group !== null ? group.start * rangeFactor : 0; const groupCount = group !== null ? group.count * rangeFactor : Infinity; const drawStart = Math.max( rangeStart, groupStart ); const drawEnd = Math.min( dataCount, rangeStart + rangeCount, groupStart + groupCount ) - 1; const drawCount = Math.max( 0, drawEnd - drawStart + 1 ); if ( drawCount === 0 ) return; // if ( object.isMesh ) { if ( material.wireframe === true ) { state.setLineWidth( material.wireframeLinewidth * getTargetPixelRatio() ); renderer.setMode( 1 ); } else { renderer.setMode( 4 ); } } else if ( object.isLine ) { let lineWidth = material.linewidth; if ( lineWidth === undefined ) lineWidth = 1; // Not using Line*Material state.setLineWidth( lineWidth * getTargetPixelRatio() ); if ( object.isLineSegments ) { renderer.setMode( 1 ); } else if ( object.isLineLoop ) { renderer.setMode( 2 ); } else { renderer.setMode( 3 ); } } else if ( object.isPoints ) { renderer.setMode( 0 ); } else if ( object.isSprite ) { renderer.setMode( 4 ); } if ( object.isInstancedMesh ) { renderer.renderInstances( drawStart, drawCount, object.count ); } else if ( geometry.isInstancedBufferGeometry ) { const instanceCount = Math.min( geometry.instanceCount, geometry._maxInstanceCount ); renderer.renderInstances( drawStart, drawCount, instanceCount ); } else { renderer.render( drawStart, drawCount ); } }; // Compile this.compile = function ( scene, camera ) { currentRenderState = renderStates.get( scene ); currentRenderState.init(); scene.traverseVisible( function ( object ) { if ( object.isLight && object.layers.test( camera.layers ) ) { currentRenderState.pushLight( object ); if ( object.castShadow ) { currentRenderState.pushShadow( object ); } } } ); currentRenderState.setupLights(); const compiled = new WeakMap(); scene.traverse( function ( object ) { const material = object.material; if ( material ) { if ( Array.isArray( material ) ) { for ( let i = 0; i < material.length; i ++ ) { const material2 = material[ i ]; if ( compiled.has( material2 ) === false ) { initMaterial( material2, scene, object ); compiled.set( material2 ); } } } else if ( compiled.has( material ) === false ) { initMaterial( material, scene, object ); compiled.set( material ); } } } ); }; // Animation Loop let onAnimationFrameCallback = null; function onAnimationFrame( time ) { if ( xr.isPresenting ) return; if ( onAnimationFrameCallback ) onAnimationFrameCallback( time ); } const animation = new WebGLAnimation(); animation.setAnimationLoop( onAnimationFrame ); if ( typeof window !== 'undefined' ) animation.setContext( window ); this.setAnimationLoop = function ( callback ) { onAnimationFrameCallback = callback; xr.setAnimationLoop( callback ); ( callback === null ) ? animation.stop() : animation.start(); }; // Rendering this.render = function ( scene, camera ) { let renderTarget, forceClear; if ( arguments[ 2 ] !== undefined ) { console.warn( 'THREE.WebGLRenderer.render(): the renderTarget argument has been removed. Use .setRenderTarget() instead.' ); renderTarget = arguments[ 2 ]; } if ( arguments[ 3 ] !== undefined ) { console.warn( 'THREE.WebGLRenderer.render(): the forceClear argument has been removed. Use .clear() instead.' ); forceClear = arguments[ 3 ]; } if ( camera !== undefined && camera.isCamera !== true ) { console.error( 'THREE.WebGLRenderer.render: camera is not an instance of THREE.Camera.' ); return; } if ( _isContextLost === true ) return; // reset caching for this frame bindingStates.resetDefaultState(); _currentMaterialId = - 1; _currentCamera = null; // update scene graph if ( scene.autoUpdate === true ) scene.updateMatrixWorld(); // update camera matrices and frustum if ( camera.parent === null ) camera.updateMatrixWorld(); if ( xr.enabled === true && xr.isPresenting === true ) { camera = xr.getCamera( camera ); } // if ( scene.isScene === true ) scene.onBeforeRender( _this, scene, camera, renderTarget || _currentRenderTarget ); currentRenderState = renderStates.get( scene, renderStateStack.length ); currentRenderState.init(); renderStateStack.push( currentRenderState ); _projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); _frustum.setFromProjectionMatrix( _projScreenMatrix ); _localClippingEnabled = this.localClippingEnabled; _clippingEnabled = clipping.init( this.clippingPlanes, _localClippingEnabled, camera ); currentRenderList = renderLists.get( scene, camera ); currentRenderList.init(); projectObject( scene, camera, 0, _this.sortObjects ); currentRenderList.finish(); if ( _this.sortObjects === true ) { currentRenderList.sort( _opaqueSort, _transparentSort ); } // if ( _clippingEnabled === true ) clipping.beginShadows(); const shadowsArray = currentRenderState.state.shadowsArray; shadowMap.render( shadowsArray, scene, camera ); currentRenderState.setupLights(); currentRenderState.setupLightsView( camera ); if ( _clippingEnabled === true ) clipping.endShadows(); // if ( this.info.autoReset === true ) this.info.reset(); if ( renderTarget !== undefined ) { this.setRenderTarget( renderTarget ); } // background.render( currentRenderList, scene, camera, forceClear ); // render scene const opaqueObjects = currentRenderList.opaque; const transparentObjects = currentRenderList.transparent; if ( opaqueObjects.length > 0 ) renderObjects( opaqueObjects, scene, camera ); if ( transparentObjects.length > 0 ) renderObjects( transparentObjects, scene, camera ); // if ( scene.isScene === true ) scene.onAfterRender( _this, scene, camera ); // if ( _currentRenderTarget !== null ) { // Generate mipmap if we're using any kind of mipmap filtering textures.updateRenderTargetMipmap( _currentRenderTarget ); // resolve multisample renderbuffers to a single-sample texture if necessary textures.updateMultisampleRenderTarget( _currentRenderTarget ); } // Ensure depth buffer writing is enabled so it can be cleared on next render state.buffers.depth.setTest( true ); state.buffers.depth.setMask( true ); state.buffers.color.setMask( true ); state.setPolygonOffset( false ); // _gl.finish(); renderStateStack.pop(); if ( renderStateStack.length > 0 ) { currentRenderState = renderStateStack[ renderStateStack.length - 1 ]; } else { currentRenderState = null; } currentRenderList = null; }; function projectObject( object, camera, groupOrder, sortObjects ) { if ( object.visible === false ) return; const visible = object.layers.test( camera.layers ); if ( visible ) { if ( object.isGroup ) { groupOrder = object.renderOrder; } else if ( object.isLOD ) { if ( object.autoUpdate === true ) object.update( camera ); } else if ( object.isLight ) { currentRenderState.pushLight( object ); if ( object.castShadow ) { currentRenderState.pushShadow( object ); } } else if ( object.isSprite ) { if ( ! object.frustumCulled || _frustum.intersectsSprite( object ) ) { if ( sortObjects ) { _vector3.setFromMatrixPosition( object.matrixWorld ) .applyMatrix4( _projScreenMatrix ); } const geometry = objects.update( object ); const material = object.material; if ( material.visible ) { currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null ); } } } else if ( object.isImmediateRenderObject ) { if ( sortObjects ) { _vector3.setFromMatrixPosition( object.matrixWorld ) .applyMatrix4( _projScreenMatrix ); } currentRenderList.push( object, null, object.material, groupOrder, _vector3.z, null ); } else if ( object.isMesh || object.isLine || object.isPoints ) { if ( object.isSkinnedMesh ) { // update skeleton only once in a frame if ( object.skeleton.frame !== info.render.frame ) { object.skeleton.update(); object.skeleton.frame = info.render.frame; } } if ( ! object.frustumCulled || _frustum.intersectsObject( object ) ) { if ( sortObjects ) { _vector3.setFromMatrixPosition( object.matrixWorld ) .applyMatrix4( _projScreenMatrix ); } const geometry = objects.update( object ); const material = object.material; if ( Array.isArray( material ) ) { const groups = geometry.groups; for ( let i = 0, l = groups.length; i < l; i ++ ) { const group = groups[ i ]; const groupMaterial = material[ group.materialIndex ]; if ( groupMaterial && groupMaterial.visible ) { currentRenderList.push( object, geometry, groupMaterial, groupOrder, _vector3.z, group ); } } } else if ( material.visible ) { currentRenderList.push( object, geometry, material, groupOrder, _vector3.z, null ); } } } } const children = object.children; for ( let i = 0, l = children.length; i < l; i ++ ) { projectObject( children[ i ], camera, groupOrder, sortObjects ); } } function renderObjects( renderList, scene, camera ) { const overrideMaterial = scene.isScene === true ? scene.overrideMaterial : null; for ( let i = 0, l = renderList.length; i < l; i ++ ) { const renderItem = renderList[ i ]; const object = renderItem.object; const geometry = renderItem.geometry; const material = overrideMaterial === null ? renderItem.material : overrideMaterial; const group = renderItem.group; if ( camera.isArrayCamera ) { const cameras = camera.cameras; for ( let j = 0, jl = cameras.length; j < jl; j ++ ) { const camera2 = cameras[ j ]; if ( object.layers.test( camera2.layers ) ) { state.viewport( _currentViewport.copy( camera2.viewport ) ); currentRenderState.setupLightsView( camera2 ); renderObject( object, scene, camera2, geometry, material, group ); } } } else { renderObject( object, scene, camera, geometry, material, group ); } } } function renderObject( object, scene, camera, geometry, material, group ) { object.onBeforeRender( _this, scene, camera, geometry, material, group ); object.modelViewMatrix.multiplyMatrices( camera.matrixWorldInverse, object.matrixWorld ); object.normalMatrix.getNormalMatrix( object.modelViewMatrix ); if ( object.isImmediateRenderObject ) { const program = setProgram( camera, scene, material, object ); state.setMaterial( material ); bindingStates.reset(); renderObjectImmediate( object, program ); } else { _this.renderBufferDirect( camera, scene, geometry, material, object, group ); } object.onAfterRender( _this, scene, camera, geometry, material, group ); } function initMaterial( material, scene, object ) { if ( scene.isScene !== true ) scene = _emptyScene; // scene could be a Mesh, Line, Points, ... const materialProperties = properties.get( material ); const lights = currentRenderState.state.lights; const shadowsArray = currentRenderState.state.shadowsArray; const lightsStateVersion = lights.state.version; const parameters = programCache.getParameters( material, lights.state, shadowsArray, scene, object ); const programCacheKey = programCache.getProgramCacheKey( parameters ); let program = materialProperties.program; let programChange = true; if ( program === undefined ) { // new material material.addEventListener( 'dispose', onMaterialDispose ); } else if ( program.cacheKey !== programCacheKey ) { // changed glsl or parameters releaseMaterialProgramReference( material ); } else if ( materialProperties.lightsStateVersion !== lightsStateVersion ) { programChange = false; } else if ( parameters.shaderID !== undefined ) { // same glsl and uniform list, envMap still needs the update here to avoid a frame-late effect const environment = material.isMeshStandardMaterial ? scene.environment : null; materialProperties.envMap = cubemaps.get( material.envMap || environment ); return; } else { // only rebuild uniform list programChange = false; } if ( programChange ) { parameters.uniforms = programCache.getUniforms( material ); material.onBeforeCompile( parameters, _this ); program = programCache.acquireProgram( parameters, programCacheKey ); materialProperties.program = program; materialProperties.uniforms = parameters.uniforms; materialProperties.outputEncoding = parameters.outputEncoding; } const uniforms = materialProperties.uniforms; if ( ! material.isShaderMaterial && ! material.isRawShaderMaterial || material.clipping === true ) { materialProperties.numClippingPlanes = clipping.numPlanes; materialProperties.numIntersection = clipping.numIntersection; uniforms.clippingPlanes = clipping.uniform; } materialProperties.environment = material.isMeshStandardMaterial ? scene.environment : null; materialProperties.fog = scene.fog; materialProperties.envMap = cubemaps.get( material.envMap || materialProperties.environment ); // store the light setup it was created for materialProperties.needsLights = materialNeedsLights( material ); materialProperties.lightsStateVersion = lightsStateVersion; if ( materialProperties.needsLights ) { // wire up the material to this renderer's lighting state uniforms.ambientLightColor.value = lights.state.ambient; uniforms.lightProbe.value = lights.state.probe; uniforms.directionalLights.value = lights.state.directional; uniforms.directionalLightShadows.value = lights.state.directionalShadow; uniforms.spotLights.value = lights.state.spot; uniforms.spotLightShadows.value = lights.state.spotShadow; uniforms.rectAreaLights.value = lights.state.rectArea; uniforms.ltc_1.value = lights.state.rectAreaLTC1; uniforms.ltc_2.value = lights.state.rectAreaLTC2; uniforms.pointLights.value = lights.state.point; uniforms.pointLightShadows.value = lights.state.pointShadow; uniforms.hemisphereLights.value = lights.state.hemi; uniforms.directionalShadowMap.value = lights.state.directionalShadowMap; uniforms.directionalShadowMatrix.value = lights.state.directionalShadowMatrix; uniforms.spotShadowMap.value = lights.state.spotShadowMap; uniforms.spotShadowMatrix.value = lights.state.spotShadowMatrix; uniforms.pointShadowMap.value = lights.state.pointShadowMap; uniforms.pointShadowMatrix.value = lights.state.pointShadowMatrix; // TODO (abelnation): add area lights shadow info to uniforms } const progUniforms = materialProperties.program.getUniforms(); const uniformsList = WebGLUniforms.seqWithValue( progUniforms.seq, uniforms ); materialProperties.uniformsList = uniformsList; } function setProgram( camera, scene, material, object ) { if ( scene.isScene !== true ) scene = _emptyScene; // scene could be a Mesh, Line, Points, ... textures.resetTextureUnits(); const fog = scene.fog; const environment = material.isMeshStandardMaterial ? scene.environment : null; const encoding = ( _currentRenderTarget === null ) ? _this.outputEncoding : _currentRenderTarget.texture.encoding; const envMap = cubemaps.get( material.envMap || environment ); const materialProperties = properties.get( material ); const lights = currentRenderState.state.lights; if ( _clippingEnabled === true ) { if ( _localClippingEnabled === true || camera !== _currentCamera ) { const useCache = camera === _currentCamera && material.id === _currentMaterialId; // we might want to call this function with some ClippingGroup // object instead of the material, once it becomes feasible // (#8465, #8379) clipping.setState( material, camera, useCache ); } } if ( material.version === materialProperties.__version ) { if ( material.fog && materialProperties.fog !== fog ) { initMaterial( material, scene, object ); } else if ( materialProperties.environment !== environment ) { initMaterial( material, scene, object ); } else if ( materialProperties.needsLights && ( materialProperties.lightsStateVersion !== lights.state.version ) ) { initMaterial( material, scene, object ); } else if ( materialProperties.numClippingPlanes !== undefined && ( materialProperties.numClippingPlanes !== clipping.numPlanes || materialProperties.numIntersection !== clipping.numIntersection ) ) { initMaterial( material, scene, object ); } else if ( materialProperties.outputEncoding !== encoding ) { initMaterial( material, scene, object ); } else if ( materialProperties.envMap !== envMap ) { initMaterial( material, scene, object ); } } else { initMaterial( material, scene, object ); materialProperties.__version = material.version; } let refreshProgram = false; let refreshMaterial = false; let refreshLights = false; const program = materialProperties.program, p_uniforms = program.getUniforms(), m_uniforms = materialProperties.uniforms; if ( state.useProgram( program.program ) ) { refreshProgram = true; refreshMaterial = true; refreshLights = true; } if ( material.id !== _currentMaterialId ) { _currentMaterialId = material.id; refreshMaterial = true; } if ( refreshProgram || _currentCamera !== camera ) { p_uniforms.setValue( _gl, 'projectionMatrix', camera.projectionMatrix ); if ( capabilities.logarithmicDepthBuffer ) { p_uniforms.setValue( _gl, 'logDepthBufFC', 2.0 / ( Math.log( camera.far + 1.0 ) / Math.LN2 ) ); } if ( _currentCamera !== camera ) { _currentCamera = camera; // lighting uniforms depend on the camera so enforce an update // now, in case this material supports lights - or later, when // the next material that does gets activated: refreshMaterial = true; // set to true on material change refreshLights = true; // remains set until update done } // load material specific uniforms // (shader material also gets them for the sake of genericity) if ( material.isShaderMaterial || material.isMeshPhongMaterial || material.isMeshToonMaterial || material.isMeshStandardMaterial || material.envMap ) { const uCamPos = p_uniforms.map.cameraPosition; if ( uCamPos !== undefined ) { uCamPos.setValue( _gl, _vector3.setFromMatrixPosition( camera.matrixWorld ) ); } } if ( material.isMeshPhongMaterial || material.isMeshToonMaterial || material.isMeshLambertMaterial || material.isMeshBasicMaterial || material.isMeshStandardMaterial || material.isShaderMaterial ) { p_uniforms.setValue( _gl, 'isOrthographic', camera.isOrthographicCamera === true ); } if ( material.isMeshPhongMaterial || material.isMeshToonMaterial || material.isMeshLambertMaterial || material.isMeshBasicMaterial || material.isMeshStandardMaterial || material.isShaderMaterial || material.isShadowMaterial || material.skinning ) { p_uniforms.setValue( _gl, 'viewMatrix', camera.matrixWorldInverse ); } } // skinning uniforms must be set even if material didn't change // auto-setting of texture unit for bone texture must go before other textures // otherwise textures used for skinning can take over texture units reserved for other material textures if ( material.skinning ) { p_uniforms.setOptional( _gl, object, 'bindMatrix' ); p_uniforms.setOptional( _gl, object, 'bindMatrixInverse' ); const skeleton = object.skeleton; if ( skeleton ) { const bones = skeleton.bones; if ( capabilities.floatVertexTextures ) { if ( skeleton.boneTexture === null ) { // layout (1 matrix = 4 pixels) // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4) // with 8x8 pixel texture max 16 bones * 4 pixels = (8 * 8) // 16x16 pixel texture max 64 bones * 4 pixels = (16 * 16) // 32x32 pixel texture max 256 bones * 4 pixels = (32 * 32) // 64x64 pixel texture max 1024 bones * 4 pixels = (64 * 64) let size = Math.sqrt( bones.length * 4 ); // 4 pixels needed for 1 matrix size = MathUtils.ceilPowerOfTwo( size ); size = Math.max( size, 4 ); const boneMatrices = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel boneMatrices.set( skeleton.boneMatrices ); // copy current values const boneTexture = new DataTexture( boneMatrices, size, size, RGBAFormat, FloatType ); skeleton.boneMatrices = boneMatrices; skeleton.boneTexture = boneTexture; skeleton.boneTextureSize = size; } p_uniforms.setValue( _gl, 'boneTexture', skeleton.boneTexture, textures ); p_uniforms.setValue( _gl, 'boneTextureSize', skeleton.boneTextureSize ); } else { p_uniforms.setOptional( _gl, skeleton, 'boneMatrices' ); } } } if ( refreshMaterial || materialProperties.receiveShadow !== object.receiveShadow ) { materialProperties.receiveShadow = object.receiveShadow; p_uniforms.setValue( _gl, 'receiveShadow', object.receiveShadow ); } if ( refreshMaterial ) { p_uniforms.setValue( _gl, 'toneMappingExposure', _this.toneMappingExposure ); if ( materialProperties.needsLights ) { // the current material requires lighting info // note: all lighting uniforms are always set correctly // they simply reference the renderer's state for their // values // // use the current material's .needsUpdate flags to set // the GL state when required markUniformsLightsNeedsUpdate( m_uniforms, refreshLights ); } // refresh uniforms common to several materials if ( fog && material.fog ) { materials.refreshFogUniforms( m_uniforms, fog ); } materials.refreshMaterialUniforms( m_uniforms, material, _pixelRatio, _height ); WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures ); } if ( material.isShaderMaterial && material.uniformsNeedUpdate === true ) { WebGLUniforms.upload( _gl, materialProperties.uniformsList, m_uniforms, textures ); material.uniformsNeedUpdate = false; } if ( material.isSpriteMaterial ) { p_uniforms.setValue( _gl, 'center', object.center ); } // common matrices p_uniforms.setValue( _gl, 'modelViewMatrix', object.modelViewMatrix ); p_uniforms.setValue( _gl, 'normalMatrix', object.normalMatrix ); p_uniforms.setValue( _gl, 'modelMatrix', object.matrixWorld ); return program; } // If uniforms are marked as clean, they don't need to be loaded to the GPU. function markUniformsLightsNeedsUpdate( uniforms, value ) { uniforms.ambientLightColor.needsUpdate = value; uniforms.lightProbe.needsUpdate = value; uniforms.directionalLights.needsUpdate = value; uniforms.directionalLightShadows.needsUpdate = value; uniforms.pointLights.needsUpdate = value; uniforms.pointLightShadows.needsUpdate = value; uniforms.spotLights.needsUpdate = value; uniforms.spotLightShadows.needsUpdate = value; uniforms.rectAreaLights.needsUpdate = value; uniforms.hemisphereLights.needsUpdate = value; } function materialNeedsLights( material ) { return material.isMeshLambertMaterial || material.isMeshToonMaterial || material.isMeshPhongMaterial || material.isMeshStandardMaterial || material.isShadowMaterial || ( material.isShaderMaterial && material.lights === true ); } // this.setFramebuffer = function ( value ) { if ( _framebuffer !== value && _currentRenderTarget === null ) _gl.bindFramebuffer( 36160, value ); _framebuffer = value; }; this.getActiveCubeFace = function () { return _currentActiveCubeFace; }; this.getActiveMipmapLevel = function () { return _currentActiveMipmapLevel; }; this.getRenderList = function () { return currentRenderList; }; this.setRenderList = function ( renderList ) { currentRenderList = renderList; }; this.getRenderTarget = function () { return _currentRenderTarget; }; this.setRenderTarget = function ( renderTarget, activeCubeFace = 0, activeMipmapLevel = 0 ) { _currentRenderTarget = renderTarget; _currentActiveCubeFace = activeCubeFace; _currentActiveMipmapLevel = activeMipmapLevel; if ( renderTarget && properties.get( renderTarget ).__webglFramebuffer === undefined ) { textures.setupRenderTarget( renderTarget ); } let framebuffer = _framebuffer; let isCube = false; if ( renderTarget ) { const __webglFramebuffer = properties.get( renderTarget ).__webglFramebuffer; if ( renderTarget.isWebGLCubeRenderTarget ) { framebuffer = __webglFramebuffer[ activeCubeFace ]; isCube = true; } else if ( renderTarget.isWebGLMultisampleRenderTarget ) { framebuffer = properties.get( renderTarget ).__webglMultisampledFramebuffer; } else { framebuffer = __webglFramebuffer; } _currentViewport.copy( renderTarget.viewport ); _currentScissor.copy( renderTarget.scissor ); _currentScissorTest = renderTarget.scissorTest; } else { _currentViewport.copy( _viewport ).multiplyScalar( _pixelRatio ).floor(); _currentScissor.copy( _scissor ).multiplyScalar( _pixelRatio ).floor(); _currentScissorTest = _scissorTest; } if ( _currentFramebuffer !== framebuffer ) { _gl.bindFramebuffer( 36160, framebuffer ); _currentFramebuffer = framebuffer; } state.viewport( _currentViewport ); state.scissor( _currentScissor ); state.setScissorTest( _currentScissorTest ); if ( isCube ) { const textureProperties = properties.get( renderTarget.texture ); _gl.framebufferTexture2D( 36160, 36064, 34069 + activeCubeFace, textureProperties.__webglTexture, activeMipmapLevel ); } }; this.readRenderTargetPixels = function ( renderTarget, x, y, width, height, buffer, activeCubeFaceIndex ) { if ( ! ( renderTarget && renderTarget.isWebGLRenderTarget ) ) { console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not THREE.WebGLRenderTarget.' ); return; } let framebuffer = properties.get( renderTarget ).__webglFramebuffer; if ( renderTarget.isWebGLCubeRenderTarget && activeCubeFaceIndex !== undefined ) { framebuffer = framebuffer[ activeCubeFaceIndex ]; } if ( framebuffer ) { let restore = false; if ( framebuffer !== _currentFramebuffer ) { _gl.bindFramebuffer( 36160, framebuffer ); restore = true; } try { const texture = renderTarget.texture; const textureFormat = texture.format; const textureType = texture.type; if ( textureFormat !== RGBAFormat && utils.convert( textureFormat ) !== _gl.getParameter( 35739 ) ) { console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in RGBA or implementation defined format.' ); return; } if ( textureType !== UnsignedByteType && utils.convert( textureType ) !== _gl.getParameter( 35738 ) && // IE11, Edge and Chrome Mac < 52 (#9513) ! ( textureType === FloatType && ( capabilities.isWebGL2 || extensions.get( 'OES_texture_float' ) || extensions.get( 'WEBGL_color_buffer_float' ) ) ) && // Chrome Mac >= 52 and Firefox ! ( textureType === HalfFloatType && ( capabilities.isWebGL2 ? extensions.get( 'EXT_color_buffer_float' ) : extensions.get( 'EXT_color_buffer_half_float' ) ) ) ) { console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: renderTarget is not in UnsignedByteType or implementation defined type.' ); return; } if ( _gl.checkFramebufferStatus( 36160 ) === 36053 ) { // the following if statement ensures valid read requests (no out-of-bounds pixels, see #8604) if ( ( x >= 0 && x <= ( renderTarget.width - width ) ) && ( y >= 0 && y <= ( renderTarget.height - height ) ) ) { _gl.readPixels( x, y, width, height, utils.convert( textureFormat ), utils.convert( textureType ), buffer ); } } else { console.error( 'THREE.WebGLRenderer.readRenderTargetPixels: readPixels from renderTarget failed. Framebuffer not complete.' ); } } finally { if ( restore ) { _gl.bindFramebuffer( 36160, _currentFramebuffer ); } } } }; this.copyFramebufferToTexture = function ( position, texture, level = 0 ) { const levelScale = Math.pow( 2, - level ); const width = Math.floor( texture.image.width * levelScale ); const height = Math.floor( texture.image.height * levelScale ); const glFormat = utils.convert( texture.format ); textures.setTexture2D( texture, 0 ); _gl.copyTexImage2D( 3553, level, glFormat, position.x, position.y, width, height, 0 ); state.unbindTexture(); }; this.copyTextureToTexture = function ( position, srcTexture, dstTexture, level = 0 ) { const width = srcTexture.image.width; const height = srcTexture.image.height; const glFormat = utils.convert( dstTexture.format ); const glType = utils.convert( dstTexture.type ); textures.setTexture2D( dstTexture, 0 ); // As another texture upload may have changed pixelStorei // parameters, make sure they are correct for the dstTexture _gl.pixelStorei( 37440, dstTexture.flipY ); _gl.pixelStorei( 37441, dstTexture.premultiplyAlpha ); _gl.pixelStorei( 3317, dstTexture.unpackAlignment ); if ( srcTexture.isDataTexture ) { _gl.texSubImage2D( 3553, level, position.x, position.y, width, height, glFormat, glType, srcTexture.image.data ); } else { if ( srcTexture.isCompressedTexture ) { _gl.compressedTexSubImage2D( 3553, level, position.x, position.y, srcTexture.mipmaps[ 0 ].width, srcTexture.mipmaps[ 0 ].height, glFormat, srcTexture.mipmaps[ 0 ].data ); } else { _gl.texSubImage2D( 3553, level, position.x, position.y, glFormat, glType, srcTexture.image ); } } // Generate mipmaps only when copying level 0 if ( level === 0 && dstTexture.generateMipmaps ) _gl.generateMipmap( 3553 ); state.unbindTexture(); }; this.initTexture = function ( texture ) { textures.setTexture2D( texture, 0 ); state.unbindTexture(); }; this.resetState = function () { state.reset(); bindingStates.reset(); }; if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) { __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); // eslint-disable-line no-undef } } function WebGL1Renderer( parameters ) { WebGLRenderer.call( this, parameters ); } WebGL1Renderer.prototype = Object.assign( Object.create( WebGLRenderer.prototype ), { constructor: WebGL1Renderer, isWebGL1Renderer: true } ); class FogExp2 { constructor( color, density ) { Object.defineProperty( this, 'isFogExp2', { value: true } ); this.name = ''; this.color = new Color( color ); this.density = ( density !== undefined ) ? density : 0.00025; } clone() { return new FogExp2( this.color, this.density ); } toJSON( /* meta */ ) { return { type: 'FogExp2', color: this.color.getHex(), density: this.density }; } } class Fog { constructor( color, near, far ) { Object.defineProperty( this, 'isFog', { value: true } ); this.name = ''; this.color = new Color( color ); this.near = ( near !== undefined ) ? near : 1; this.far = ( far !== undefined ) ? far : 1000; } clone() { return new Fog( this.color, this.near, this.far ); } toJSON( /* meta */ ) { return { type: 'Fog', color: this.color.getHex(), near: this.near, far: this.far }; } } class Scene extends Object3D { constructor() { super(); Object.defineProperty( this, 'isScene', { value: true } ); this.type = 'Scene'; this.background = null; this.environment = null; this.fog = null; this.overrideMaterial = null; this.autoUpdate = true; // checked by the renderer if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) { __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'observe', { detail: this } ) ); // eslint-disable-line no-undef } } copy( source, recursive ) { super.copy( source, recursive ); if ( source.background !== null ) this.background = source.background.clone(); if ( source.environment !== null ) this.environment = source.environment.clone(); if ( source.fog !== null ) this.fog = source.fog.clone(); if ( source.overrideMaterial !== null ) this.overrideMaterial = source.overrideMaterial.clone(); this.autoUpdate = source.autoUpdate; this.matrixAutoUpdate = source.matrixAutoUpdate; return this; } toJSON( meta ) { const data = super.toJSON( meta ); if ( this.background !== null ) data.object.background = this.background.toJSON( meta ); if ( this.environment !== null ) data.object.environment = this.environment.toJSON( meta ); if ( this.fog !== null ) data.object.fog = this.fog.toJSON(); return data; } } function InterleavedBuffer( array, stride ) { this.array = array; this.stride = stride; this.count = array !== undefined ? array.length / stride : 0; this.usage = StaticDrawUsage; this.updateRange = { offset: 0, count: - 1 }; this.version = 0; this.uuid = MathUtils.generateUUID(); } Object.defineProperty( InterleavedBuffer.prototype, 'needsUpdate', { set: function ( value ) { if ( value === true ) this.version ++; } } ); Object.assign( InterleavedBuffer.prototype, { isInterleavedBuffer: true, onUploadCallback: function () {}, setUsage: function ( value ) { this.usage = value; return this; }, copy: function ( source ) { this.array = new source.array.constructor( source.array ); this.count = source.count; this.stride = source.stride; this.usage = source.usage; return this; }, copyAt: function ( index1, attribute, index2 ) { index1 *= this.stride; index2 *= attribute.stride; for ( let i = 0, l = this.stride; i < l; i ++ ) { this.array[ index1 + i ] = attribute.array[ index2 + i ]; } return this; }, set: function ( value, offset = 0 ) { this.array.set( value, offset ); return this; }, clone: function ( data ) { if ( data.arrayBuffers === undefined ) { data.arrayBuffers = {}; } if ( this.array.buffer._uuid === undefined ) { this.array.buffer._uuid = MathUtils.generateUUID(); } if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) { data.arrayBuffers[ this.array.buffer._uuid ] = this.array.slice( 0 ).buffer; } const array = new this.array.constructor( data.arrayBuffers[ this.array.buffer._uuid ] ); const ib = new InterleavedBuffer( array, this.stride ); ib.setUsage( this.usage ); return ib; }, onUpload: function ( callback ) { this.onUploadCallback = callback; return this; }, toJSON: function ( data ) { if ( data.arrayBuffers === undefined ) { data.arrayBuffers = {}; } // generate UUID for array buffer if necessary if ( this.array.buffer._uuid === undefined ) { this.array.buffer._uuid = MathUtils.generateUUID(); } if ( data.arrayBuffers[ this.array.buffer._uuid ] === undefined ) { data.arrayBuffers[ this.array.buffer._uuid ] = Array.prototype.slice.call( new Uint32Array( this.array.buffer ) ); } // return { uuid: this.uuid, buffer: this.array.buffer._uuid, type: this.array.constructor.name, stride: this.stride }; } } ); const _vector$6 = new Vector3(); function InterleavedBufferAttribute( interleavedBuffer, itemSize, offset, normalized ) { this.name = ''; this.data = interleavedBuffer; this.itemSize = itemSize; this.offset = offset; this.normalized = normalized === true; } Object.defineProperties( InterleavedBufferAttribute.prototype, { count: { get: function () { return this.data.count; } }, array: { get: function () { return this.data.array; } }, needsUpdate: { set: function ( value ) { this.data.needsUpdate = value; } } } ); Object.assign( InterleavedBufferAttribute.prototype, { isInterleavedBufferAttribute: true, applyMatrix4: function ( m ) { for ( let i = 0, l = this.data.count; i < l; i ++ ) { _vector$6.x = this.getX( i ); _vector$6.y = this.getY( i ); _vector$6.z = this.getZ( i ); _vector$6.applyMatrix4( m ); this.setXYZ( i, _vector$6.x, _vector$6.y, _vector$6.z ); } return this; }, setX: function ( index, x ) { this.data.array[ index * this.data.stride + this.offset ] = x; return this; }, setY: function ( index, y ) { this.data.array[ index * this.data.stride + this.offset + 1 ] = y; return this; }, setZ: function ( index, z ) { this.data.array[ index * this.data.stride + this.offset + 2 ] = z; return this; }, setW: function ( index, w ) { this.data.array[ index * this.data.stride + this.offset + 3 ] = w; return this; }, getX: function ( index ) { return this.data.array[ index * this.data.stride + this.offset ]; }, getY: function ( index ) { return this.data.array[ index * this.data.stride + this.offset + 1 ]; }, getZ: function ( index ) { return this.data.array[ index * this.data.stride + this.offset + 2 ]; }, getW: function ( index ) { return this.data.array[ index * this.data.stride + this.offset + 3 ]; }, setXY: function ( index, x, y ) { index = index * this.data.stride + this.offset; this.data.array[ index + 0 ] = x; this.data.array[ index + 1 ] = y; return this; }, setXYZ: function ( index, x, y, z ) { index = index * this.data.stride + this.offset; this.data.array[ index + 0 ] = x; this.data.array[ index + 1 ] = y; this.data.array[ index + 2 ] = z; return this; }, setXYZW: function ( index, x, y, z, w ) { index = index * this.data.stride + this.offset; this.data.array[ index + 0 ] = x; this.data.array[ index + 1 ] = y; this.data.array[ index + 2 ] = z; this.data.array[ index + 3 ] = w; return this; }, clone: function ( data ) { if ( data === undefined ) { console.log( 'THREE.InterleavedBufferAttribute.clone(): Cloning an interlaved buffer attribute will deinterleave buffer data.' ); const array = []; for ( let i = 0; i < this.count; i ++ ) { const index = i * this.data.stride + this.offset; for ( let j = 0; j < this.itemSize; j ++ ) { array.push( this.data.array[ index + j ] ); } } return new BufferAttribute( new this.array.constructor( array ), this.itemSize, this.normalized ); } else { if ( data.interleavedBuffers === undefined ) { data.interleavedBuffers = {}; } if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) { data.interleavedBuffers[ this.data.uuid ] = this.data.clone( data ); } return new InterleavedBufferAttribute( data.interleavedBuffers[ this.data.uuid ], this.itemSize, this.offset, this.normalized ); } }, toJSON: function ( data ) { if ( data === undefined ) { console.log( 'THREE.InterleavedBufferAttribute.toJSON(): Serializing an interlaved buffer attribute will deinterleave buffer data.' ); const array = []; for ( let i = 0; i < this.count; i ++ ) { const index = i * this.data.stride + this.offset; for ( let j = 0; j < this.itemSize; j ++ ) { array.push( this.data.array[ index + j ] ); } } // deinterleave data and save it as an ordinary buffer attribute for now return { itemSize: this.itemSize, type: this.array.constructor.name, array: array, normalized: this.normalized }; } else { // save as true interlaved attribtue if ( data.interleavedBuffers === undefined ) { data.interleavedBuffers = {}; } if ( data.interleavedBuffers[ this.data.uuid ] === undefined ) { data.interleavedBuffers[ this.data.uuid ] = this.data.toJSON( data ); } return { isInterleavedBufferAttribute: true, itemSize: this.itemSize, data: this.data.uuid, offset: this.offset, normalized: this.normalized }; } } } ); /** * parameters = { * color: , * map: new THREE.Texture( ), * alphaMap: new THREE.Texture( ), * rotation: , * sizeAttenuation: * } */ function SpriteMaterial( parameters ) { Material.call( this ); this.type = 'SpriteMaterial'; this.color = new Color( 0xffffff ); this.map = null; this.alphaMap = null; this.rotation = 0; this.sizeAttenuation = true; this.transparent = true; this.setValues( parameters ); } SpriteMaterial.prototype = Object.create( Material.prototype ); SpriteMaterial.prototype.constructor = SpriteMaterial; SpriteMaterial.prototype.isSpriteMaterial = true; SpriteMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); this.map = source.map; this.alphaMap = source.alphaMap; this.rotation = source.rotation; this.sizeAttenuation = source.sizeAttenuation; return this; }; let _geometry; const _intersectPoint = new Vector3(); const _worldScale = new Vector3(); const _mvPosition = new Vector3(); const _alignedPosition = new Vector2(); const _rotatedPosition = new Vector2(); const _viewWorldMatrix = new Matrix4(); const _vA$1 = new Vector3(); const _vB$1 = new Vector3(); const _vC$1 = new Vector3(); const _uvA$1 = new Vector2(); const _uvB$1 = new Vector2(); const _uvC$1 = new Vector2(); function Sprite$1( material ) { Object3D.call( this ); this.type = 'Sprite'; if ( _geometry === undefined ) { _geometry = new BufferGeometry(); const float32Array = new Float32Array( [ - 0.5, - 0.5, 0, 0, 0, 0.5, - 0.5, 0, 1, 0, 0.5, 0.5, 0, 1, 1, - 0.5, 0.5, 0, 0, 1 ] ); const interleavedBuffer = new InterleavedBuffer( float32Array, 5 ); _geometry.setIndex( [ 0, 1, 2, 0, 2, 3 ] ); _geometry.setAttribute( 'position', new InterleavedBufferAttribute( interleavedBuffer, 3, 0, false ) ); _geometry.setAttribute( 'uv', new InterleavedBufferAttribute( interleavedBuffer, 2, 3, false ) ); } this.geometry = _geometry; this.material = ( material !== undefined ) ? material : new SpriteMaterial(); this.center = new Vector2( 0.5, 0.5 ); } Sprite$1.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Sprite$1, isSprite: true, raycast: function ( raycaster, intersects ) { if ( raycaster.camera === null ) { console.error( 'THREE.Sprite: "Raycaster.camera" needs to be set in order to raycast against sprites.' ); } _worldScale.setFromMatrixScale( this.matrixWorld ); _viewWorldMatrix.copy( raycaster.camera.matrixWorld ); this.modelViewMatrix.multiplyMatrices( raycaster.camera.matrixWorldInverse, this.matrixWorld ); _mvPosition.setFromMatrixPosition( this.modelViewMatrix ); if ( raycaster.camera.isPerspectiveCamera && this.material.sizeAttenuation === false ) { _worldScale.multiplyScalar( - _mvPosition.z ); } const rotation = this.material.rotation; let sin, cos; if ( rotation !== 0 ) { cos = Math.cos( rotation ); sin = Math.sin( rotation ); } const center = this.center; transformVertex( _vA$1.set( - 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); transformVertex( _vB$1.set( 0.5, - 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); transformVertex( _vC$1.set( 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); _uvA$1.set( 0, 0 ); _uvB$1.set( 1, 0 ); _uvC$1.set( 1, 1 ); // check first triangle let intersect = raycaster.ray.intersectTriangle( _vA$1, _vB$1, _vC$1, false, _intersectPoint ); if ( intersect === null ) { // check second triangle transformVertex( _vB$1.set( - 0.5, 0.5, 0 ), _mvPosition, center, _worldScale, sin, cos ); _uvB$1.set( 0, 1 ); intersect = raycaster.ray.intersectTriangle( _vA$1, _vC$1, _vB$1, false, _intersectPoint ); if ( intersect === null ) { return; } } const distance = raycaster.ray.origin.distanceTo( _intersectPoint ); if ( distance < raycaster.near || distance > raycaster.far ) return; intersects.push( { distance: distance, point: _intersectPoint.clone(), uv: Triangle.getUV( _intersectPoint, _vA$1, _vB$1, _vC$1, _uvA$1, _uvB$1, _uvC$1, new Vector2() ), face: null, object: this } ); }, copy: function ( source ) { Object3D.prototype.copy.call( this, source ); if ( source.center !== undefined ) this.center.copy( source.center ); this.material = source.material; return this; } } ); function transformVertex( vertexPosition, mvPosition, center, scale, sin, cos ) { // compute position in camera space _alignedPosition.subVectors( vertexPosition, center ).addScalar( 0.5 ).multiply( scale ); // to check if rotation is not zero if ( sin !== undefined ) { _rotatedPosition.x = ( cos * _alignedPosition.x ) - ( sin * _alignedPosition.y ); _rotatedPosition.y = ( sin * _alignedPosition.x ) + ( cos * _alignedPosition.y ); } else { _rotatedPosition.copy( _alignedPosition ); } vertexPosition.copy( mvPosition ); vertexPosition.x += _rotatedPosition.x; vertexPosition.y += _rotatedPosition.y; // transform to world space vertexPosition.applyMatrix4( _viewWorldMatrix ); } const _v1$4 = new Vector3(); const _v2$2 = new Vector3(); function LOD() { Object3D.call( this ); this._currentLevel = 0; this.type = 'LOD'; Object.defineProperties( this, { levels: { enumerable: true, value: [] } } ); this.autoUpdate = true; } LOD.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: LOD, isLOD: true, copy: function ( source ) { Object3D.prototype.copy.call( this, source, false ); const levels = source.levels; for ( let i = 0, l = levels.length; i < l; i ++ ) { const level = levels[ i ]; this.addLevel( level.object.clone(), level.distance ); } this.autoUpdate = source.autoUpdate; return this; }, addLevel: function ( object, distance = 0 ) { distance = Math.abs( distance ); const levels = this.levels; let l; for ( l = 0; l < levels.length; l ++ ) { if ( distance < levels[ l ].distance ) { break; } } levels.splice( l, 0, { distance: distance, object: object } ); this.add( object ); return this; }, getCurrentLevel: function () { return this._currentLevel; }, getObjectForDistance: function ( distance ) { const levels = this.levels; if ( levels.length > 0 ) { let i, l; for ( i = 1, l = levels.length; i < l; i ++ ) { if ( distance < levels[ i ].distance ) { break; } } return levels[ i - 1 ].object; } return null; }, raycast: function ( raycaster, intersects ) { const levels = this.levels; if ( levels.length > 0 ) { _v1$4.setFromMatrixPosition( this.matrixWorld ); const distance = raycaster.ray.origin.distanceTo( _v1$4 ); this.getObjectForDistance( distance ).raycast( raycaster, intersects ); } }, update: function ( camera ) { const levels = this.levels; if ( levels.length > 1 ) { _v1$4.setFromMatrixPosition( camera.matrixWorld ); _v2$2.setFromMatrixPosition( this.matrixWorld ); const distance = _v1$4.distanceTo( _v2$2 ) / camera.zoom; levels[ 0 ].object.visible = true; let i, l; for ( i = 1, l = levels.length; i < l; i ++ ) { if ( distance >= levels[ i ].distance ) { levels[ i - 1 ].object.visible = false; levels[ i ].object.visible = true; } else { break; } } this._currentLevel = i - 1; for ( ; i < l; i ++ ) { levels[ i ].object.visible = false; } } }, toJSON: function ( meta ) { const data = Object3D.prototype.toJSON.call( this, meta ); if ( this.autoUpdate === false ) data.object.autoUpdate = false; data.object.levels = []; const levels = this.levels; for ( let i = 0, l = levels.length; i < l; i ++ ) { const level = levels[ i ]; data.object.levels.push( { object: level.object.uuid, distance: level.distance } ); } return data; } } ); const _basePosition = new Vector3(); const _skinIndex = new Vector4(); const _skinWeight = new Vector4(); const _vector$7 = new Vector3(); const _matrix$1 = new Matrix4(); function SkinnedMesh( geometry, material ) { if ( geometry && geometry.isGeometry ) { console.error( 'THREE.SkinnedMesh no longer supports THREE.Geometry. Use THREE.BufferGeometry instead.' ); } Mesh.call( this, geometry, material ); this.type = 'SkinnedMesh'; this.bindMode = 'attached'; this.bindMatrix = new Matrix4(); this.bindMatrixInverse = new Matrix4(); } SkinnedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), { constructor: SkinnedMesh, isSkinnedMesh: true, copy: function ( source ) { Mesh.prototype.copy.call( this, source ); this.bindMode = source.bindMode; this.bindMatrix.copy( source.bindMatrix ); this.bindMatrixInverse.copy( source.bindMatrixInverse ); this.skeleton = source.skeleton; return this; }, bind: function ( skeleton, bindMatrix ) { this.skeleton = skeleton; if ( bindMatrix === undefined ) { this.updateMatrixWorld( true ); this.skeleton.calculateInverses(); bindMatrix = this.matrixWorld; } this.bindMatrix.copy( bindMatrix ); this.bindMatrixInverse.copy( bindMatrix ).invert(); }, pose: function () { this.skeleton.pose(); }, normalizeSkinWeights: function () { const vector = new Vector4(); const skinWeight = this.geometry.attributes.skinWeight; for ( let i = 0, l = skinWeight.count; i < l; i ++ ) { vector.x = skinWeight.getX( i ); vector.y = skinWeight.getY( i ); vector.z = skinWeight.getZ( i ); vector.w = skinWeight.getW( i ); const scale = 1.0 / vector.manhattanLength(); if ( scale !== Infinity ) { vector.multiplyScalar( scale ); } else { vector.set( 1, 0, 0, 0 ); // do something reasonable } skinWeight.setXYZW( i, vector.x, vector.y, vector.z, vector.w ); } }, updateMatrixWorld: function ( force ) { Mesh.prototype.updateMatrixWorld.call( this, force ); if ( this.bindMode === 'attached' ) { this.bindMatrixInverse.copy( this.matrixWorld ).invert(); } else if ( this.bindMode === 'detached' ) { this.bindMatrixInverse.copy( this.bindMatrix ).invert(); } else { console.warn( 'THREE.SkinnedMesh: Unrecognized bindMode: ' + this.bindMode ); } }, boneTransform: function ( index, target ) { const skeleton = this.skeleton; const geometry = this.geometry; _skinIndex.fromBufferAttribute( geometry.attributes.skinIndex, index ); _skinWeight.fromBufferAttribute( geometry.attributes.skinWeight, index ); _basePosition.fromBufferAttribute( geometry.attributes.position, index ).applyMatrix4( this.bindMatrix ); target.set( 0, 0, 0 ); for ( let i = 0; i < 4; i ++ ) { const weight = _skinWeight.getComponent( i ); if ( weight !== 0 ) { const boneIndex = _skinIndex.getComponent( i ); _matrix$1.multiplyMatrices( skeleton.bones[ boneIndex ].matrixWorld, skeleton.boneInverses[ boneIndex ] ); target.addScaledVector( _vector$7.copy( _basePosition ).applyMatrix4( _matrix$1 ), weight ); } } return target.applyMatrix4( this.bindMatrixInverse ); } } ); function Bone() { Object3D.call( this ); this.type = 'Bone'; } Bone.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Bone, isBone: true } ); const _offsetMatrix = new Matrix4(); const _identityMatrix = new Matrix4(); function Skeleton( bones = [], boneInverses = [] ) { this.uuid = MathUtils.generateUUID(); this.bones = bones.slice( 0 ); this.boneInverses = boneInverses; this.boneMatrices = null; this.boneTexture = null; this.boneTextureSize = 0; this.frame = - 1; this.init(); } Object.assign( Skeleton.prototype, { init: function () { const bones = this.bones; const boneInverses = this.boneInverses; this.boneMatrices = new Float32Array( bones.length * 16 ); // calculate inverse bone matrices if necessary if ( boneInverses.length === 0 ) { this.calculateInverses(); } else { // handle special case if ( bones.length !== boneInverses.length ) { console.warn( 'THREE.Skeleton: Number of inverse bone matrices does not match amount of bones.' ); this.boneInverses = []; for ( let i = 0, il = this.bones.length; i < il; i ++ ) { this.boneInverses.push( new Matrix4() ); } } } }, calculateInverses: function () { this.boneInverses.length = 0; for ( let i = 0, il = this.bones.length; i < il; i ++ ) { const inverse = new Matrix4(); if ( this.bones[ i ] ) { inverse.copy( this.bones[ i ].matrixWorld ).invert(); } this.boneInverses.push( inverse ); } }, pose: function () { // recover the bind-time world matrices for ( let i = 0, il = this.bones.length; i < il; i ++ ) { const bone = this.bones[ i ]; if ( bone ) { bone.matrixWorld.copy( this.boneInverses[ i ] ).invert(); } } // compute the local matrices, positions, rotations and scales for ( let i = 0, il = this.bones.length; i < il; i ++ ) { const bone = this.bones[ i ]; if ( bone ) { if ( bone.parent && bone.parent.isBone ) { bone.matrix.copy( bone.parent.matrixWorld ).invert(); bone.matrix.multiply( bone.matrixWorld ); } else { bone.matrix.copy( bone.matrixWorld ); } bone.matrix.decompose( bone.position, bone.quaternion, bone.scale ); } } }, update: function () { const bones = this.bones; const boneInverses = this.boneInverses; const boneMatrices = this.boneMatrices; const boneTexture = this.boneTexture; // flatten bone matrices to array for ( let i = 0, il = bones.length; i < il; i ++ ) { // compute the offset between the current and the original transform const matrix = bones[ i ] ? bones[ i ].matrixWorld : _identityMatrix; _offsetMatrix.multiplyMatrices( matrix, boneInverses[ i ] ); _offsetMatrix.toArray( boneMatrices, i * 16 ); } if ( boneTexture !== null ) { boneTexture.needsUpdate = true; } }, clone: function () { return new Skeleton( this.bones, this.boneInverses ); }, getBoneByName: function ( name ) { for ( let i = 0, il = this.bones.length; i < il; i ++ ) { const bone = this.bones[ i ]; if ( bone.name === name ) { return bone; } } return undefined; }, dispose: function ( ) { if ( this.boneTexture !== null ) { this.boneTexture.dispose(); this.boneTexture = null; } }, fromJSON: function ( json, bones ) { this.uuid = json.uuid; for ( let i = 0, l = json.bones.length; i < l; i ++ ) { const uuid = json.bones[ i ]; let bone = bones[ uuid ]; if ( bone === undefined ) { console.warn( 'THREE.Skeleton: No bone found with UUID:', uuid ); bone = new Bone(); } this.bones.push( bone ); this.boneInverses.push( new Matrix4().fromArray( json.boneInverses[ i ] ) ); } this.init(); return this; }, toJSON: function () { const data = { metadata: { version: 4.5, type: 'Skeleton', generator: 'Skeleton.toJSON' }, bones: [], boneInverses: [] }; data.uuid = this.uuid; const bones = this.bones; const boneInverses = this.boneInverses; for ( let i = 0, l = bones.length; i < l; i ++ ) { const bone = bones[ i ]; data.bones.push( bone.uuid ); const boneInverse = boneInverses[ i ]; data.boneInverses.push( boneInverse.toArray() ); } return data; } } ); const _instanceLocalMatrix = new Matrix4(); const _instanceWorldMatrix = new Matrix4(); const _instanceIntersects = []; const _mesh = new Mesh(); function InstancedMesh( geometry, material, count ) { Mesh.call( this, geometry, material ); this.instanceMatrix = new BufferAttribute( new Float32Array( count * 16 ), 16 ); this.instanceColor = null; this.count = count; this.frustumCulled = false; } InstancedMesh.prototype = Object.assign( Object.create( Mesh.prototype ), { constructor: InstancedMesh, isInstancedMesh: true, copy: function ( source ) { Mesh.prototype.copy.call( this, source ); this.instanceMatrix.copy( source.instanceMatrix ); this.count = source.count; return this; }, getColorAt: function ( index, color ) { color.fromArray( this.instanceColor.array, index * 3 ); }, getMatrixAt: function ( index, matrix ) { matrix.fromArray( this.instanceMatrix.array, index * 16 ); }, raycast: function ( raycaster, intersects ) { const matrixWorld = this.matrixWorld; const raycastTimes = this.count; _mesh.geometry = this.geometry; _mesh.material = this.material; if ( _mesh.material === undefined ) return; for ( let instanceId = 0; instanceId < raycastTimes; instanceId ++ ) { // calculate the world matrix for each instance this.getMatrixAt( instanceId, _instanceLocalMatrix ); _instanceWorldMatrix.multiplyMatrices( matrixWorld, _instanceLocalMatrix ); // the mesh represents this single instance _mesh.matrixWorld = _instanceWorldMatrix; _mesh.raycast( raycaster, _instanceIntersects ); // process the result of raycast for ( let i = 0, l = _instanceIntersects.length; i < l; i ++ ) { const intersect = _instanceIntersects[ i ]; intersect.instanceId = instanceId; intersect.object = this; intersects.push( intersect ); } _instanceIntersects.length = 0; } }, setColorAt: function ( index, color ) { if ( this.instanceColor === null ) { this.instanceColor = new BufferAttribute( new Float32Array( this.count * 3 ), 3 ); } color.toArray( this.instanceColor.array, index * 3 ); }, setMatrixAt: function ( index, matrix ) { matrix.toArray( this.instanceMatrix.array, index * 16 ); }, updateMorphTargets: function () { }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } } ); /** * parameters = { * color: , * opacity: , * * linewidth: , * linecap: "round", * linejoin: "round" * } */ function LineBasicMaterial( parameters ) { Material.call( this ); this.type = 'LineBasicMaterial'; this.color = new Color( 0xffffff ); this.linewidth = 1; this.linecap = 'round'; this.linejoin = 'round'; this.morphTargets = false; this.setValues( parameters ); } LineBasicMaterial.prototype = Object.create( Material.prototype ); LineBasicMaterial.prototype.constructor = LineBasicMaterial; LineBasicMaterial.prototype.isLineBasicMaterial = true; LineBasicMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); this.linewidth = source.linewidth; this.linecap = source.linecap; this.linejoin = source.linejoin; this.morphTargets = source.morphTargets; return this; }; const _start = new Vector3(); const _end = new Vector3(); const _inverseMatrix$1 = new Matrix4(); const _ray$1 = new Ray(); const _sphere$2 = new Sphere(); function Line( geometry = new BufferGeometry(), material = new LineBasicMaterial() ) { Object3D.call( this ); this.type = 'Line'; this.geometry = geometry; this.material = material; this.updateMorphTargets(); } Line.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Line, isLine: true, copy: function ( source ) { Object3D.prototype.copy.call( this, source ); this.material = source.material; this.geometry = source.geometry; return this; }, computeLineDistances: function () { const geometry = this.geometry; if ( geometry.isBufferGeometry ) { // we assume non-indexed geometry if ( geometry.index === null ) { const positionAttribute = geometry.attributes.position; const lineDistances = [ 0 ]; for ( let i = 1, l = positionAttribute.count; i < l; i ++ ) { _start.fromBufferAttribute( positionAttribute, i - 1 ); _end.fromBufferAttribute( positionAttribute, i ); lineDistances[ i ] = lineDistances[ i - 1 ]; lineDistances[ i ] += _start.distanceTo( _end ); } geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) ); } else { console.warn( 'THREE.Line.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' ); } } else if ( geometry.isGeometry ) { const vertices = geometry.vertices; const lineDistances = geometry.lineDistances; lineDistances[ 0 ] = 0; for ( let i = 1, l = vertices.length; i < l; i ++ ) { lineDistances[ i ] = lineDistances[ i - 1 ]; lineDistances[ i ] += vertices[ i - 1 ].distanceTo( vertices[ i ] ); } } return this; }, raycast: function ( raycaster, intersects ) { const geometry = this.geometry; const matrixWorld = this.matrixWorld; const threshold = raycaster.params.Line.threshold; // Checking boundingSphere distance to ray if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); _sphere$2.copy( geometry.boundingSphere ); _sphere$2.applyMatrix4( matrixWorld ); _sphere$2.radius += threshold; if ( raycaster.ray.intersectsSphere( _sphere$2 ) === false ) return; // _inverseMatrix$1.copy( matrixWorld ).invert(); _ray$1.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$1 ); const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 ); const localThresholdSq = localThreshold * localThreshold; const vStart = new Vector3(); const vEnd = new Vector3(); const interSegment = new Vector3(); const interRay = new Vector3(); const step = this.isLineSegments ? 2 : 1; if ( geometry.isBufferGeometry ) { const index = geometry.index; const attributes = geometry.attributes; const positionAttribute = attributes.position; if ( index !== null ) { const indices = index.array; for ( let i = 0, l = indices.length - 1; i < l; i += step ) { const a = indices[ i ]; const b = indices[ i + 1 ]; vStart.fromBufferAttribute( positionAttribute, a ); vEnd.fromBufferAttribute( positionAttribute, b ); const distSq = _ray$1.distanceSqToSegment( vStart, vEnd, interRay, interSegment ); if ( distSq > localThresholdSq ) continue; interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation const distance = raycaster.ray.origin.distanceTo( interRay ); if ( distance < raycaster.near || distance > raycaster.far ) continue; intersects.push( { distance: distance, // What do we want? intersection point on the ray or on the segment?? // point: raycaster.ray.at( distance ), point: interSegment.clone().applyMatrix4( this.matrixWorld ), index: i, face: null, faceIndex: null, object: this } ); } } else { for ( let i = 0, l = positionAttribute.count - 1; i < l; i += step ) { vStart.fromBufferAttribute( positionAttribute, i ); vEnd.fromBufferAttribute( positionAttribute, i + 1 ); const distSq = _ray$1.distanceSqToSegment( vStart, vEnd, interRay, interSegment ); if ( distSq > localThresholdSq ) continue; interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation const distance = raycaster.ray.origin.distanceTo( interRay ); if ( distance < raycaster.near || distance > raycaster.far ) continue; intersects.push( { distance: distance, // What do we want? intersection point on the ray or on the segment?? // point: raycaster.ray.at( distance ), point: interSegment.clone().applyMatrix4( this.matrixWorld ), index: i, face: null, faceIndex: null, object: this } ); } } } else if ( geometry.isGeometry ) { const vertices = geometry.vertices; const nbVertices = vertices.length; for ( let i = 0; i < nbVertices - 1; i += step ) { const distSq = _ray$1.distanceSqToSegment( vertices[ i ], vertices[ i + 1 ], interRay, interSegment ); if ( distSq > localThresholdSq ) continue; interRay.applyMatrix4( this.matrixWorld ); //Move back to world space for distance calculation const distance = raycaster.ray.origin.distanceTo( interRay ); if ( distance < raycaster.near || distance > raycaster.far ) continue; intersects.push( { distance: distance, // What do we want? intersection point on the ray or on the segment?? // point: raycaster.ray.at( distance ), point: interSegment.clone().applyMatrix4( this.matrixWorld ), index: i, face: null, faceIndex: null, object: this } ); } } }, updateMorphTargets: function () { const geometry = this.geometry; if ( geometry.isBufferGeometry ) { const morphAttributes = geometry.morphAttributes; const keys = Object.keys( morphAttributes ); if ( keys.length > 0 ) { const morphAttribute = morphAttributes[ keys[ 0 ] ]; if ( morphAttribute !== undefined ) { this.morphTargetInfluences = []; this.morphTargetDictionary = {}; for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) { const name = morphAttribute[ m ].name || String( m ); this.morphTargetInfluences.push( 0 ); this.morphTargetDictionary[ name ] = m; } } } } else { const morphTargets = geometry.morphTargets; if ( morphTargets !== undefined && morphTargets.length > 0 ) { console.error( 'THREE.Line.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' ); } } } } ); const _start$1 = new Vector3(); const _end$1 = new Vector3(); function LineSegments( geometry, material ) { Line.call( this, geometry, material ); this.type = 'LineSegments'; } LineSegments.prototype = Object.assign( Object.create( Line.prototype ), { constructor: LineSegments, isLineSegments: true, computeLineDistances: function () { const geometry = this.geometry; if ( geometry.isBufferGeometry ) { // we assume non-indexed geometry if ( geometry.index === null ) { const positionAttribute = geometry.attributes.position; const lineDistances = []; for ( let i = 0, l = positionAttribute.count; i < l; i += 2 ) { _start$1.fromBufferAttribute( positionAttribute, i ); _end$1.fromBufferAttribute( positionAttribute, i + 1 ); lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ]; lineDistances[ i + 1 ] = lineDistances[ i ] + _start$1.distanceTo( _end$1 ); } geometry.setAttribute( 'lineDistance', new Float32BufferAttribute( lineDistances, 1 ) ); } else { console.warn( 'THREE.LineSegments.computeLineDistances(): Computation only possible with non-indexed BufferGeometry.' ); } } else if ( geometry.isGeometry ) { const vertices = geometry.vertices; const lineDistances = geometry.lineDistances; for ( let i = 0, l = vertices.length; i < l; i += 2 ) { _start$1.copy( vertices[ i ] ); _end$1.copy( vertices[ i + 1 ] ); lineDistances[ i ] = ( i === 0 ) ? 0 : lineDistances[ i - 1 ]; lineDistances[ i + 1 ] = lineDistances[ i ] + _start$1.distanceTo( _end$1 ); } } return this; } } ); function LineLoop( geometry, material ) { Line.call( this, geometry, material ); this.type = 'LineLoop'; } LineLoop.prototype = Object.assign( Object.create( Line.prototype ), { constructor: LineLoop, isLineLoop: true, } ); /** * parameters = { * color: , * opacity: , * map: new THREE.Texture( ), * alphaMap: new THREE.Texture( ), * * size: , * sizeAttenuation: * * morphTargets: * } */ function PointsMaterial( parameters ) { Material.call( this ); this.type = 'PointsMaterial'; this.color = new Color( 0xffffff ); this.map = null; this.alphaMap = null; this.size = 1; this.sizeAttenuation = true; this.morphTargets = false; this.setValues( parameters ); } PointsMaterial.prototype = Object.create( Material.prototype ); PointsMaterial.prototype.constructor = PointsMaterial; PointsMaterial.prototype.isPointsMaterial = true; PointsMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); this.map = source.map; this.alphaMap = source.alphaMap; this.size = source.size; this.sizeAttenuation = source.sizeAttenuation; this.morphTargets = source.morphTargets; return this; }; const _inverseMatrix$2 = new Matrix4(); const _ray$2 = new Ray(); const _sphere$3 = new Sphere(); const _position$1 = new Vector3(); function Points( geometry = new BufferGeometry(), material = new PointsMaterial() ) { Object3D.call( this ); this.type = 'Points'; this.geometry = geometry; this.material = material; this.updateMorphTargets(); } Points.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Points, isPoints: true, copy: function ( source ) { Object3D.prototype.copy.call( this, source ); this.material = source.material; this.geometry = source.geometry; return this; }, raycast: function ( raycaster, intersects ) { const geometry = this.geometry; const matrixWorld = this.matrixWorld; const threshold = raycaster.params.Points.threshold; // Checking boundingSphere distance to ray if ( geometry.boundingSphere === null ) geometry.computeBoundingSphere(); _sphere$3.copy( geometry.boundingSphere ); _sphere$3.applyMatrix4( matrixWorld ); _sphere$3.radius += threshold; if ( raycaster.ray.intersectsSphere( _sphere$3 ) === false ) return; // _inverseMatrix$2.copy( matrixWorld ).invert(); _ray$2.copy( raycaster.ray ).applyMatrix4( _inverseMatrix$2 ); const localThreshold = threshold / ( ( this.scale.x + this.scale.y + this.scale.z ) / 3 ); const localThresholdSq = localThreshold * localThreshold; if ( geometry.isBufferGeometry ) { const index = geometry.index; const attributes = geometry.attributes; const positionAttribute = attributes.position; if ( index !== null ) { const indices = index.array; for ( let i = 0, il = indices.length; i < il; i ++ ) { const a = indices[ i ]; _position$1.fromBufferAttribute( positionAttribute, a ); testPoint( _position$1, a, localThresholdSq, matrixWorld, raycaster, intersects, this ); } } else { for ( let i = 0, l = positionAttribute.count; i < l; i ++ ) { _position$1.fromBufferAttribute( positionAttribute, i ); testPoint( _position$1, i, localThresholdSq, matrixWorld, raycaster, intersects, this ); } } } else { const vertices = geometry.vertices; for ( let i = 0, l = vertices.length; i < l; i ++ ) { testPoint( vertices[ i ], i, localThresholdSq, matrixWorld, raycaster, intersects, this ); } } }, updateMorphTargets: function () { const geometry = this.geometry; if ( geometry.isBufferGeometry ) { const morphAttributes = geometry.morphAttributes; const keys = Object.keys( morphAttributes ); if ( keys.length > 0 ) { const morphAttribute = morphAttributes[ keys[ 0 ] ]; if ( morphAttribute !== undefined ) { this.morphTargetInfluences = []; this.morphTargetDictionary = {}; for ( let m = 0, ml = morphAttribute.length; m < ml; m ++ ) { const name = morphAttribute[ m ].name || String( m ); this.morphTargetInfluences.push( 0 ); this.morphTargetDictionary[ name ] = m; } } } } else { const morphTargets = geometry.morphTargets; if ( morphTargets !== undefined && morphTargets.length > 0 ) { console.error( 'THREE.Points.updateMorphTargets() does not support THREE.Geometry. Use THREE.BufferGeometry instead.' ); } } } } ); function testPoint( point, index, localThresholdSq, matrixWorld, raycaster, intersects, object ) { const rayPointDistanceSq = _ray$2.distanceSqToPoint( point ); if ( rayPointDistanceSq < localThresholdSq ) { const intersectPoint = new Vector3(); _ray$2.closestPointToPoint( point, intersectPoint ); intersectPoint.applyMatrix4( matrixWorld ); const distance = raycaster.ray.origin.distanceTo( intersectPoint ); if ( distance < raycaster.near || distance > raycaster.far ) return; intersects.push( { distance: distance, distanceToRay: Math.sqrt( rayPointDistanceSq ), point: intersectPoint, index: index, face: null, object: object } ); } } function VideoTexture( video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) { Texture.call( this, video, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); this.format = format !== undefined ? format : RGBFormat; this.minFilter = minFilter !== undefined ? minFilter : LinearFilter; this.magFilter = magFilter !== undefined ? magFilter : LinearFilter; this.generateMipmaps = false; const scope = this; function updateVideo() { scope.needsUpdate = true; video.requestVideoFrameCallback( updateVideo ); } if ( 'requestVideoFrameCallback' in video ) { video.requestVideoFrameCallback( updateVideo ); } } VideoTexture.prototype = Object.assign( Object.create( Texture.prototype ), { constructor: VideoTexture, clone: function () { return new this.constructor( this.image ).copy( this ); }, isVideoTexture: true, update: function () { const video = this.image; const hasVideoFrameCallback = 'requestVideoFrameCallback' in video; if ( hasVideoFrameCallback === false && video.readyState >= video.HAVE_CURRENT_DATA ) { this.needsUpdate = true; } } } ); function CompressedTexture( mipmaps, width, height, format, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, encoding ) { Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy, encoding ); this.image = { width: width, height: height }; this.mipmaps = mipmaps; // no flipping for cube textures // (also flipping doesn't work for compressed textures ) this.flipY = false; // can't generate mipmaps for compressed textures // mips must be embedded in DDS files this.generateMipmaps = false; } CompressedTexture.prototype = Object.create( Texture.prototype ); CompressedTexture.prototype.constructor = CompressedTexture; CompressedTexture.prototype.isCompressedTexture = true; function CanvasTexture( canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ) { Texture.call( this, canvas, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); this.needsUpdate = true; } CanvasTexture.prototype = Object.create( Texture.prototype ); CanvasTexture.prototype.constructor = CanvasTexture; CanvasTexture.prototype.isCanvasTexture = true; function DepthTexture( width, height, type, mapping, wrapS, wrapT, magFilter, minFilter, anisotropy, format ) { format = format !== undefined ? format : DepthFormat; if ( format !== DepthFormat && format !== DepthStencilFormat ) { throw new Error( 'DepthTexture format must be either THREE.DepthFormat or THREE.DepthStencilFormat' ); } if ( type === undefined && format === DepthFormat ) type = UnsignedShortType; if ( type === undefined && format === DepthStencilFormat ) type = UnsignedInt248Type$1; Texture.call( this, null, mapping, wrapS, wrapT, magFilter, minFilter, format, type, anisotropy ); this.image = { width: width, height: height }; this.magFilter = magFilter !== undefined ? magFilter : NearestFilter; this.minFilter = minFilter !== undefined ? minFilter : NearestFilter; this.flipY = false; this.generateMipmaps = false; } DepthTexture.prototype = Object.create( Texture.prototype ); DepthTexture.prototype.constructor = DepthTexture; DepthTexture.prototype.isDepthTexture = true; let _geometryId = 0; // Geometry uses even numbers as Id const _m1$3 = new Matrix4(); const _obj$1 = new Object3D(); const _offset$1 = new Vector3(); function Geometry() { Object.defineProperty( this, 'id', { value: _geometryId += 2 } ); this.uuid = MathUtils.generateUUID(); this.name = ''; this.type = 'Geometry'; this.vertices = []; this.colors = []; this.faces = []; this.faceVertexUvs = [[]]; this.morphTargets = []; this.morphNormals = []; this.skinWeights = []; this.skinIndices = []; this.lineDistances = []; this.boundingBox = null; this.boundingSphere = null; // update flags this.elementsNeedUpdate = false; this.verticesNeedUpdate = false; this.uvsNeedUpdate = false; this.normalsNeedUpdate = false; this.colorsNeedUpdate = false; this.lineDistancesNeedUpdate = false; this.groupsNeedUpdate = false; } Geometry.prototype = Object.assign( Object.create( EventDispatcher.prototype ), { constructor: Geometry, isGeometry: true, applyMatrix4: function ( matrix ) { const normalMatrix = new Matrix3().getNormalMatrix( matrix ); for ( let i = 0, il = this.vertices.length; i < il; i ++ ) { const vertex = this.vertices[ i ]; vertex.applyMatrix4( matrix ); } for ( let i = 0, il = this.faces.length; i < il; i ++ ) { const face = this.faces[ i ]; face.normal.applyMatrix3( normalMatrix ).normalize(); for ( let j = 0, jl = face.vertexNormals.length; j < jl; j ++ ) { face.vertexNormals[ j ].applyMatrix3( normalMatrix ).normalize(); } } if ( this.boundingBox !== null ) { this.computeBoundingBox(); } if ( this.boundingSphere !== null ) { this.computeBoundingSphere(); } this.verticesNeedUpdate = true; this.normalsNeedUpdate = true; return this; }, rotateX: function ( angle ) { // rotate geometry around world x-axis _m1$3.makeRotationX( angle ); this.applyMatrix4( _m1$3 ); return this; }, rotateY: function ( angle ) { // rotate geometry around world y-axis _m1$3.makeRotationY( angle ); this.applyMatrix4( _m1$3 ); return this; }, rotateZ: function ( angle ) { // rotate geometry around world z-axis _m1$3.makeRotationZ( angle ); this.applyMatrix4( _m1$3 ); return this; }, translate: function ( x, y, z ) { // translate geometry _m1$3.makeTranslation( x, y, z ); this.applyMatrix4( _m1$3 ); return this; }, scale: function ( x, y, z ) { // scale geometry _m1$3.makeScale( x, y, z ); this.applyMatrix4( _m1$3 ); return this; }, lookAt: function ( vector ) { _obj$1.lookAt( vector ); _obj$1.updateMatrix(); this.applyMatrix4( _obj$1.matrix ); return this; }, fromBufferGeometry: function ( geometry ) { const scope = this; const index = geometry.index !== null ? geometry.index : undefined; const attributes = geometry.attributes; if ( attributes.position === undefined ) { console.error( 'THREE.Geometry.fromBufferGeometry(): Position attribute required for conversion.' ); return this; } const position = attributes.position; const normal = attributes.normal; const color = attributes.color; const uv = attributes.uv; const uv2 = attributes.uv2; if ( uv2 !== undefined ) this.faceVertexUvs[ 1 ] = []; for ( let i = 0; i < position.count; i ++ ) { scope.vertices.push( new Vector3().fromBufferAttribute( position, i ) ); if ( color !== undefined ) { scope.colors.push( new Color().fromBufferAttribute( color, i ) ); } } function addFace( a, b, c, materialIndex ) { const vertexColors = ( color === undefined ) ? [] : [ scope.colors[ a ].clone(), scope.colors[ b ].clone(), scope.colors[ c ].clone() ]; const vertexNormals = ( normal === undefined ) ? [] : [ new Vector3().fromBufferAttribute( normal, a ), new Vector3().fromBufferAttribute( normal, b ), new Vector3().fromBufferAttribute( normal, c ) ]; const face = new Face3( a, b, c, vertexNormals, vertexColors, materialIndex ); scope.faces.push( face ); if ( uv !== undefined ) { scope.faceVertexUvs[ 0 ].push( [ new Vector2().fromBufferAttribute( uv, a ), new Vector2().fromBufferAttribute( uv, b ), new Vector2().fromBufferAttribute( uv, c ) ] ); } if ( uv2 !== undefined ) { scope.faceVertexUvs[ 1 ].push( [ new Vector2().fromBufferAttribute( uv2, a ), new Vector2().fromBufferAttribute( uv2, b ), new Vector2().fromBufferAttribute( uv2, c ) ] ); } } const groups = geometry.groups; if ( groups.length > 0 ) { for ( let i = 0; i < groups.length; i ++ ) { const group = groups[ i ]; const start = group.start; const count = group.count; for ( let j = start, jl = start + count; j < jl; j += 3 ) { if ( index !== undefined ) { addFace( index.getX( j ), index.getX( j + 1 ), index.getX( j + 2 ), group.materialIndex ); } else { addFace( j, j + 1, j + 2, group.materialIndex ); } } } } else { if ( index !== undefined ) { for ( let i = 0; i < index.count; i += 3 ) { addFace( index.getX( i ), index.getX( i + 1 ), index.getX( i + 2 ) ); } } else { for ( let i = 0; i < position.count; i += 3 ) { addFace( i, i + 1, i + 2 ); } } } this.computeFaceNormals(); if ( geometry.boundingBox !== null ) { this.boundingBox = geometry.boundingBox.clone(); } if ( geometry.boundingSphere !== null ) { this.boundingSphere = geometry.boundingSphere.clone(); } return this; }, center: function () { this.computeBoundingBox(); this.boundingBox.getCenter( _offset$1 ).negate(); this.translate( _offset$1.x, _offset$1.y, _offset$1.z ); return this; }, normalize: function () { this.computeBoundingSphere(); const center = this.boundingSphere.center; const radius = this.boundingSphere.radius; const s = radius === 0 ? 1 : 1.0 / radius; const matrix = new Matrix4(); matrix.set( s, 0, 0, - s * center.x, 0, s, 0, - s * center.y, 0, 0, s, - s * center.z, 0, 0, 0, 1 ); this.applyMatrix4( matrix ); return this; }, computeFaceNormals: function () { const cb = new Vector3(), ab = new Vector3(); for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; const vA = this.vertices[ face.a ]; const vB = this.vertices[ face.b ]; const vC = this.vertices[ face.c ]; cb.subVectors( vC, vB ); ab.subVectors( vA, vB ); cb.cross( ab ); cb.normalize(); face.normal.copy( cb ); } }, computeVertexNormals: function ( areaWeighted = true ) { const vertices = new Array( this.vertices.length ); for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) { vertices[ v ] = new Vector3(); } if ( areaWeighted ) { // vertex normals weighted by triangle areas // http://www.iquilezles.org/www/articles/normals/normals.htm const cb = new Vector3(), ab = new Vector3(); for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; const vA = this.vertices[ face.a ]; const vB = this.vertices[ face.b ]; const vC = this.vertices[ face.c ]; cb.subVectors( vC, vB ); ab.subVectors( vA, vB ); cb.cross( ab ); vertices[ face.a ].add( cb ); vertices[ face.b ].add( cb ); vertices[ face.c ].add( cb ); } } else { this.computeFaceNormals(); for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; vertices[ face.a ].add( face.normal ); vertices[ face.b ].add( face.normal ); vertices[ face.c ].add( face.normal ); } } for ( let v = 0, vl = this.vertices.length; v < vl; v ++ ) { vertices[ v ].normalize(); } for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; const vertexNormals = face.vertexNormals; if ( vertexNormals.length === 3 ) { vertexNormals[ 0 ].copy( vertices[ face.a ] ); vertexNormals[ 1 ].copy( vertices[ face.b ] ); vertexNormals[ 2 ].copy( vertices[ face.c ] ); } else { vertexNormals[ 0 ] = vertices[ face.a ].clone(); vertexNormals[ 1 ] = vertices[ face.b ].clone(); vertexNormals[ 2 ] = vertices[ face.c ].clone(); } } if ( this.faces.length > 0 ) { this.normalsNeedUpdate = true; } }, computeFlatVertexNormals: function () { this.computeFaceNormals(); for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; const vertexNormals = face.vertexNormals; if ( vertexNormals.length === 3 ) { vertexNormals[ 0 ].copy( face.normal ); vertexNormals[ 1 ].copy( face.normal ); vertexNormals[ 2 ].copy( face.normal ); } else { vertexNormals[ 0 ] = face.normal.clone(); vertexNormals[ 1 ] = face.normal.clone(); vertexNormals[ 2 ] = face.normal.clone(); } } if ( this.faces.length > 0 ) { this.normalsNeedUpdate = true; } }, computeMorphNormals: function () { // save original normals // - create temp variables on first access // otherwise just copy (for faster repeated calls) for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; if ( ! face.__originalFaceNormal ) { face.__originalFaceNormal = face.normal.clone(); } else { face.__originalFaceNormal.copy( face.normal ); } if ( ! face.__originalVertexNormals ) face.__originalVertexNormals = []; for ( let i = 0, il = face.vertexNormals.length; i < il; i ++ ) { if ( ! face.__originalVertexNormals[ i ] ) { face.__originalVertexNormals[ i ] = face.vertexNormals[ i ].clone(); } else { face.__originalVertexNormals[ i ].copy( face.vertexNormals[ i ] ); } } } // use temp geometry to compute face and vertex normals for each morph const tmpGeo = new Geometry(); tmpGeo.faces = this.faces; for ( let i = 0, il = this.morphTargets.length; i < il; i ++ ) { // create on first access if ( ! this.morphNormals[ i ] ) { this.morphNormals[ i ] = {}; this.morphNormals[ i ].faceNormals = []; this.morphNormals[ i ].vertexNormals = []; const dstNormalsFace = this.morphNormals[ i ].faceNormals; const dstNormalsVertex = this.morphNormals[ i ].vertexNormals; for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const faceNormal = new Vector3(); const vertexNormals = { a: new Vector3(), b: new Vector3(), c: new Vector3() }; dstNormalsFace.push( faceNormal ); dstNormalsVertex.push( vertexNormals ); } } const morphNormals = this.morphNormals[ i ]; // set vertices to morph target tmpGeo.vertices = this.morphTargets[ i ].vertices; // compute morph normals tmpGeo.computeFaceNormals(); tmpGeo.computeVertexNormals(); // store morph normals for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; const faceNormal = morphNormals.faceNormals[ f ]; const vertexNormals = morphNormals.vertexNormals[ f ]; faceNormal.copy( face.normal ); vertexNormals.a.copy( face.vertexNormals[ 0 ] ); vertexNormals.b.copy( face.vertexNormals[ 1 ] ); vertexNormals.c.copy( face.vertexNormals[ 2 ] ); } } // restore original normals for ( let f = 0, fl = this.faces.length; f < fl; f ++ ) { const face = this.faces[ f ]; face.normal = face.__originalFaceNormal; face.vertexNormals = face.__originalVertexNormals; } }, computeBoundingBox: function () { if ( this.boundingBox === null ) { this.boundingBox = new Box3(); } this.boundingBox.setFromPoints( this.vertices ); }, computeBoundingSphere: function () { if ( this.boundingSphere === null ) { this.boundingSphere = new Sphere(); } this.boundingSphere.setFromPoints( this.vertices ); }, merge: function ( geometry, matrix, materialIndexOffset = 0 ) { if ( ! ( geometry && geometry.isGeometry ) ) { console.error( 'THREE.Geometry.merge(): geometry not an instance of THREE.Geometry.', geometry ); return; } let normalMatrix; const vertexOffset = this.vertices.length, vertices1 = this.vertices, vertices2 = geometry.vertices, faces1 = this.faces, faces2 = geometry.faces, colors1 = this.colors, colors2 = geometry.colors; if ( matrix !== undefined ) { normalMatrix = new Matrix3().getNormalMatrix( matrix ); } // vertices for ( let i = 0, il = vertices2.length; i < il; i ++ ) { const vertex = vertices2[ i ]; const vertexCopy = vertex.clone(); if ( matrix !== undefined ) vertexCopy.applyMatrix4( matrix ); vertices1.push( vertexCopy ); } // colors for ( let i = 0, il = colors2.length; i < il; i ++ ) { colors1.push( colors2[ i ].clone() ); } // faces for ( let i = 0, il = faces2.length; i < il; i ++ ) { const face = faces2[ i ]; let normal, color; const faceVertexNormals = face.vertexNormals, faceVertexColors = face.vertexColors; const faceCopy = new Face3( face.a + vertexOffset, face.b + vertexOffset, face.c + vertexOffset ); faceCopy.normal.copy( face.normal ); if ( normalMatrix !== undefined ) { faceCopy.normal.applyMatrix3( normalMatrix ).normalize(); } for ( let j = 0, jl = faceVertexNormals.length; j < jl; j ++ ) { normal = faceVertexNormals[ j ].clone(); if ( normalMatrix !== undefined ) { normal.applyMatrix3( normalMatrix ).normalize(); } faceCopy.vertexNormals.push( normal ); } faceCopy.color.copy( face.color ); for ( let j = 0, jl = faceVertexColors.length; j < jl; j ++ ) { color = faceVertexColors[ j ]; faceCopy.vertexColors.push( color.clone() ); } faceCopy.materialIndex = face.materialIndex + materialIndexOffset; faces1.push( faceCopy ); } // uvs for ( let i = 0, il = geometry.faceVertexUvs.length; i < il; i ++ ) { const faceVertexUvs2 = geometry.faceVertexUvs[ i ]; if ( this.faceVertexUvs[ i ] === undefined ) this.faceVertexUvs[ i ] = []; for ( let j = 0, jl = faceVertexUvs2.length; j < jl; j ++ ) { const uvs2 = faceVertexUvs2[ j ], uvsCopy = []; for ( let k = 0, kl = uvs2.length; k < kl; k ++ ) { uvsCopy.push( uvs2[ k ].clone() ); } this.faceVertexUvs[ i ].push( uvsCopy ); } } }, mergeMesh: function ( mesh ) { if ( ! ( mesh && mesh.isMesh ) ) { console.error( 'THREE.Geometry.mergeMesh(): mesh not an instance of THREE.Mesh.', mesh ); return; } if ( mesh.matrixAutoUpdate ) mesh.updateMatrix(); this.merge( mesh.geometry, mesh.matrix ); }, /* * Checks for duplicate vertices with hashmap. * Duplicated vertices are removed * and faces' vertices are updated. */ mergeVertices: function ( precisionPoints = 4 ) { const verticesMap = {}; // Hashmap for looking up vertices by position coordinates (and making sure they are unique) const unique = [], changes = []; const precision = Math.pow( 10, precisionPoints ); for ( let i = 0, il = this.vertices.length; i < il; i ++ ) { const v = this.vertices[ i ]; const key = Math.round( v.x * precision ) + '_' + Math.round( v.y * precision ) + '_' + Math.round( v.z * precision ); if ( verticesMap[ key ] === undefined ) { verticesMap[ key ] = i; unique.push( this.vertices[ i ] ); changes[ i ] = unique.length - 1; } else { //console.log('Duplicate vertex found. ', i, ' could be using ', verticesMap[key]); changes[ i ] = changes[ verticesMap[ key ] ]; } } // if faces are completely degenerate after merging vertices, we // have to remove them from the geometry. const faceIndicesToRemove = []; for ( let i = 0, il = this.faces.length; i < il; i ++ ) { const face = this.faces[ i ]; face.a = changes[ face.a ]; face.b = changes[ face.b ]; face.c = changes[ face.c ]; const indices = [ face.a, face.b, face.c ]; // if any duplicate vertices are found in a Face3 // we have to remove the face as nothing can be saved for ( let n = 0; n < 3; n ++ ) { if ( indices[ n ] === indices[ ( n + 1 ) % 3 ] ) { faceIndicesToRemove.push( i ); break; } } } for ( let i = faceIndicesToRemove.length - 1; i >= 0; i -- ) { const idx = faceIndicesToRemove[ i ]; this.faces.splice( idx, 1 ); for ( let j = 0, jl = this.faceVertexUvs.length; j < jl; j ++ ) { this.faceVertexUvs[ j ].splice( idx, 1 ); } } // Use unique set of vertices const diff = this.vertices.length - unique.length; this.vertices = unique; return diff; }, setFromPoints: function ( points ) { this.vertices = []; for ( let i = 0, l = points.length; i < l; i ++ ) { const point = points[ i ]; this.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) ); } return this; }, sortFacesByMaterialIndex: function () { const faces = this.faces; const length = faces.length; // tag faces for ( let i = 0; i < length; i ++ ) { faces[ i ]._id = i; } // sort faces function materialIndexSort( a, b ) { return a.materialIndex - b.materialIndex; } faces.sort( materialIndexSort ); // sort uvs const uvs1 = this.faceVertexUvs[ 0 ]; const uvs2 = this.faceVertexUvs[ 1 ]; let newUvs1, newUvs2; if ( uvs1 && uvs1.length === length ) newUvs1 = []; if ( uvs2 && uvs2.length === length ) newUvs2 = []; for ( let i = 0; i < length; i ++ ) { const id = faces[ i ]._id; if ( newUvs1 ) newUvs1.push( uvs1[ id ] ); if ( newUvs2 ) newUvs2.push( uvs2[ id ] ); } if ( newUvs1 ) this.faceVertexUvs[ 0 ] = newUvs1; if ( newUvs2 ) this.faceVertexUvs[ 1 ] = newUvs2; }, toJSON: function () { const data = { metadata: { version: 4.5, type: 'Geometry', generator: 'Geometry.toJSON' } }; // standard Geometry serialization data.uuid = this.uuid; data.type = this.type; if ( this.name !== '' ) data.name = this.name; if ( this.parameters !== undefined ) { const parameters = this.parameters; for ( const key in parameters ) { if ( parameters[ key ] !== undefined ) data[ key ] = parameters[ key ]; } return data; } const vertices = []; for ( let i = 0; i < this.vertices.length; i ++ ) { const vertex = this.vertices[ i ]; vertices.push( vertex.x, vertex.y, vertex.z ); } const faces = []; const normals = []; const normalsHash = {}; const colors = []; const colorsHash = {}; const uvs = []; const uvsHash = {}; for ( let i = 0; i < this.faces.length; i ++ ) { const face = this.faces[ i ]; const hasMaterial = true; const hasFaceUv = false; // deprecated const hasFaceVertexUv = this.faceVertexUvs[ 0 ][ i ] !== undefined; const hasFaceNormal = face.normal.length() > 0; const hasFaceVertexNormal = face.vertexNormals.length > 0; const hasFaceColor = face.color.r !== 1 || face.color.g !== 1 || face.color.b !== 1; const hasFaceVertexColor = face.vertexColors.length > 0; let faceType = 0; faceType = setBit( faceType, 0, 0 ); // isQuad faceType = setBit( faceType, 1, hasMaterial ); faceType = setBit( faceType, 2, hasFaceUv ); faceType = setBit( faceType, 3, hasFaceVertexUv ); faceType = setBit( faceType, 4, hasFaceNormal ); faceType = setBit( faceType, 5, hasFaceVertexNormal ); faceType = setBit( faceType, 6, hasFaceColor ); faceType = setBit( faceType, 7, hasFaceVertexColor ); faces.push( faceType ); faces.push( face.a, face.b, face.c ); faces.push( face.materialIndex ); if ( hasFaceVertexUv ) { const faceVertexUvs = this.faceVertexUvs[ 0 ][ i ]; faces.push( getUvIndex( faceVertexUvs[ 0 ] ), getUvIndex( faceVertexUvs[ 1 ] ), getUvIndex( faceVertexUvs[ 2 ] ) ); } if ( hasFaceNormal ) { faces.push( getNormalIndex( face.normal ) ); } if ( hasFaceVertexNormal ) { const vertexNormals = face.vertexNormals; faces.push( getNormalIndex( vertexNormals[ 0 ] ), getNormalIndex( vertexNormals[ 1 ] ), getNormalIndex( vertexNormals[ 2 ] ) ); } if ( hasFaceColor ) { faces.push( getColorIndex( face.color ) ); } if ( hasFaceVertexColor ) { const vertexColors = face.vertexColors; faces.push( getColorIndex( vertexColors[ 0 ] ), getColorIndex( vertexColors[ 1 ] ), getColorIndex( vertexColors[ 2 ] ) ); } } function setBit( value, position, enabled ) { return enabled ? value | ( 1 << position ) : value & ( ~ ( 1 << position ) ); } function getNormalIndex( normal ) { const hash = normal.x.toString() + normal.y.toString() + normal.z.toString(); if ( normalsHash[ hash ] !== undefined ) { return normalsHash[ hash ]; } normalsHash[ hash ] = normals.length / 3; normals.push( normal.x, normal.y, normal.z ); return normalsHash[ hash ]; } function getColorIndex( color ) { const hash = color.r.toString() + color.g.toString() + color.b.toString(); if ( colorsHash[ hash ] !== undefined ) { return colorsHash[ hash ]; } colorsHash[ hash ] = colors.length; colors.push( color.getHex() ); return colorsHash[ hash ]; } function getUvIndex( uv ) { const hash = uv.x.toString() + uv.y.toString(); if ( uvsHash[ hash ] !== undefined ) { return uvsHash[ hash ]; } uvsHash[ hash ] = uvs.length / 2; uvs.push( uv.x, uv.y ); return uvsHash[ hash ]; } data.data = {}; data.data.vertices = vertices; data.data.normals = normals; if ( colors.length > 0 ) data.data.colors = colors; if ( uvs.length > 0 ) data.data.uvs = [ uvs ]; // temporal backward compatibility data.data.faces = faces; return data; }, clone: function () { /* // Handle primitives const parameters = this.parameters; if ( parameters !== undefined ) { const values = []; for ( const key in parameters ) { values.push( parameters[ key ] ); } const geometry = Object.create( this.constructor.prototype ); this.constructor.apply( geometry, values ); return geometry; } return new this.constructor().copy( this ); */ return new Geometry().copy( this ); }, copy: function ( source ) { // reset this.vertices = []; this.colors = []; this.faces = []; this.faceVertexUvs = [[]]; this.morphTargets = []; this.morphNormals = []; this.skinWeights = []; this.skinIndices = []; this.lineDistances = []; this.boundingBox = null; this.boundingSphere = null; // name this.name = source.name; // vertices const vertices = source.vertices; for ( let i = 0, il = vertices.length; i < il; i ++ ) { this.vertices.push( vertices[ i ].clone() ); } // colors const colors = source.colors; for ( let i = 0, il = colors.length; i < il; i ++ ) { this.colors.push( colors[ i ].clone() ); } // faces const faces = source.faces; for ( let i = 0, il = faces.length; i < il; i ++ ) { this.faces.push( faces[ i ].clone() ); } // face vertex uvs for ( let i = 0, il = source.faceVertexUvs.length; i < il; i ++ ) { const faceVertexUvs = source.faceVertexUvs[ i ]; if ( this.faceVertexUvs[ i ] === undefined ) { this.faceVertexUvs[ i ] = []; } for ( let j = 0, jl = faceVertexUvs.length; j < jl; j ++ ) { const uvs = faceVertexUvs[ j ], uvsCopy = []; for ( let k = 0, kl = uvs.length; k < kl; k ++ ) { const uv = uvs[ k ]; uvsCopy.push( uv.clone() ); } this.faceVertexUvs[ i ].push( uvsCopy ); } } // morph targets const morphTargets = source.morphTargets; for ( let i = 0, il = morphTargets.length; i < il; i ++ ) { const morphTarget = {}; morphTarget.name = morphTargets[ i ].name; // vertices if ( morphTargets[ i ].vertices !== undefined ) { morphTarget.vertices = []; for ( let j = 0, jl = morphTargets[ i ].vertices.length; j < jl; j ++ ) { morphTarget.vertices.push( morphTargets[ i ].vertices[ j ].clone() ); } } // normals if ( morphTargets[ i ].normals !== undefined ) { morphTarget.normals = []; for ( let j = 0, jl = morphTargets[ i ].normals.length; j < jl; j ++ ) { morphTarget.normals.push( morphTargets[ i ].normals[ j ].clone() ); } } this.morphTargets.push( morphTarget ); } // morph normals const morphNormals = source.morphNormals; for ( let i = 0, il = morphNormals.length; i < il; i ++ ) { const morphNormal = {}; // vertex normals if ( morphNormals[ i ].vertexNormals !== undefined ) { morphNormal.vertexNormals = []; for ( let j = 0, jl = morphNormals[ i ].vertexNormals.length; j < jl; j ++ ) { const srcVertexNormal = morphNormals[ i ].vertexNormals[ j ]; const destVertexNormal = {}; destVertexNormal.a = srcVertexNormal.a.clone(); destVertexNormal.b = srcVertexNormal.b.clone(); destVertexNormal.c = srcVertexNormal.c.clone(); morphNormal.vertexNormals.push( destVertexNormal ); } } // face normals if ( morphNormals[ i ].faceNormals !== undefined ) { morphNormal.faceNormals = []; for ( let j = 0, jl = morphNormals[ i ].faceNormals.length; j < jl; j ++ ) { morphNormal.faceNormals.push( morphNormals[ i ].faceNormals[ j ].clone() ); } } this.morphNormals.push( morphNormal ); } // skin weights const skinWeights = source.skinWeights; for ( let i = 0, il = skinWeights.length; i < il; i ++ ) { this.skinWeights.push( skinWeights[ i ].clone() ); } // skin indices const skinIndices = source.skinIndices; for ( let i = 0, il = skinIndices.length; i < il; i ++ ) { this.skinIndices.push( skinIndices[ i ].clone() ); } // line distances const lineDistances = source.lineDistances; for ( let i = 0, il = lineDistances.length; i < il; i ++ ) { this.lineDistances.push( lineDistances[ i ] ); } // bounding box const boundingBox = source.boundingBox; if ( boundingBox !== null ) { this.boundingBox = boundingBox.clone(); } // bounding sphere const boundingSphere = source.boundingSphere; if ( boundingSphere !== null ) { this.boundingSphere = boundingSphere.clone(); } // update flags this.elementsNeedUpdate = source.elementsNeedUpdate; this.verticesNeedUpdate = source.verticesNeedUpdate; this.uvsNeedUpdate = source.uvsNeedUpdate; this.normalsNeedUpdate = source.normalsNeedUpdate; this.colorsNeedUpdate = source.colorsNeedUpdate; this.lineDistancesNeedUpdate = source.lineDistancesNeedUpdate; this.groupsNeedUpdate = source.groupsNeedUpdate; return this; }, dispose: function () { this.dispatchEvent( { type: 'dispose' } ); } } ); class BoxGeometry extends Geometry { constructor( width, height, depth, widthSegments, heightSegments, depthSegments ) { super(); this.type = 'BoxGeometry'; this.parameters = { width: width, height: height, depth: depth, widthSegments: widthSegments, heightSegments: heightSegments, depthSegments: depthSegments }; this.fromBufferGeometry( new BoxBufferGeometry( width, height, depth, widthSegments, heightSegments, depthSegments ) ); this.mergeVertices(); } } class CircleBufferGeometry extends BufferGeometry { constructor( radius = 1, segments = 8, thetaStart = 0, thetaLength = Math.PI * 2 ) { super(); this.type = 'CircleBufferGeometry'; this.parameters = { radius: radius, segments: segments, thetaStart: thetaStart, thetaLength: thetaLength }; segments = Math.max( 3, segments ); // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // helper variables const vertex = new Vector3(); const uv = new Vector2(); // center point vertices.push( 0, 0, 0 ); normals.push( 0, 0, 1 ); uvs.push( 0.5, 0.5 ); for ( let s = 0, i = 3; s <= segments; s ++, i += 3 ) { const segment = thetaStart + s / segments * thetaLength; // vertex vertex.x = radius * Math.cos( segment ); vertex.y = radius * Math.sin( segment ); vertices.push( vertex.x, vertex.y, vertex.z ); // normal normals.push( 0, 0, 1 ); // uvs uv.x = ( vertices[ i ] / radius + 1 ) / 2; uv.y = ( vertices[ i + 1 ] / radius + 1 ) / 2; uvs.push( uv.x, uv.y ); } // indices for ( let i = 1; i <= segments; i ++ ) { indices.push( i, i + 1, 0 ); } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); } } class CircleGeometry extends Geometry { constructor( radius, segments, thetaStart, thetaLength ) { super(); this.type = 'CircleGeometry'; this.parameters = { radius: radius, segments: segments, thetaStart: thetaStart, thetaLength: thetaLength }; this.fromBufferGeometry( new CircleBufferGeometry( radius, segments, thetaStart, thetaLength ) ); this.mergeVertices(); } } class CylinderBufferGeometry extends BufferGeometry { constructor( radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 8, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) { super(); this.type = 'CylinderBufferGeometry'; this.parameters = { radiusTop: radiusTop, radiusBottom: radiusBottom, height: height, radialSegments: radialSegments, heightSegments: heightSegments, openEnded: openEnded, thetaStart: thetaStart, thetaLength: thetaLength }; const scope = this; radialSegments = Math.floor( radialSegments ); heightSegments = Math.floor( heightSegments ); // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // helper variables let index = 0; const indexArray = []; const halfHeight = height / 2; let groupStart = 0; // generate geometry generateTorso(); if ( openEnded === false ) { if ( radiusTop > 0 ) generateCap( true ); if ( radiusBottom > 0 ) generateCap( false ); } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); function generateTorso() { const normal = new Vector3(); const vertex = new Vector3(); let groupCount = 0; // this will be used to calculate the normal const slope = ( radiusBottom - radiusTop ) / height; // generate vertices, normals and uvs for ( let y = 0; y <= heightSegments; y ++ ) { const indexRow = []; const v = y / heightSegments; // calculate the radius of the current row const radius = v * ( radiusBottom - radiusTop ) + radiusTop; for ( let x = 0; x <= radialSegments; x ++ ) { const u = x / radialSegments; const theta = u * thetaLength + thetaStart; const sinTheta = Math.sin( theta ); const cosTheta = Math.cos( theta ); // vertex vertex.x = radius * sinTheta; vertex.y = - v * height + halfHeight; vertex.z = radius * cosTheta; vertices.push( vertex.x, vertex.y, vertex.z ); // normal normal.set( sinTheta, slope, cosTheta ).normalize(); normals.push( normal.x, normal.y, normal.z ); // uv uvs.push( u, 1 - v ); // save index of vertex in respective row indexRow.push( index ++ ); } // now save vertices of the row in our index array indexArray.push( indexRow ); } // generate indices for ( let x = 0; x < radialSegments; x ++ ) { for ( let y = 0; y < heightSegments; y ++ ) { // we use the index array to access the correct indices const a = indexArray[ y ][ x ]; const b = indexArray[ y + 1 ][ x ]; const c = indexArray[ y + 1 ][ x + 1 ]; const d = indexArray[ y ][ x + 1 ]; // faces indices.push( a, b, d ); indices.push( b, c, d ); // update group counter groupCount += 6; } } // add a group to the geometry. this will ensure multi material support scope.addGroup( groupStart, groupCount, 0 ); // calculate new start value for groups groupStart += groupCount; } function generateCap( top ) { // save the index of the first center vertex const centerIndexStart = index; const uv = new Vector2(); const vertex = new Vector3(); let groupCount = 0; const radius = ( top === true ) ? radiusTop : radiusBottom; const sign = ( top === true ) ? 1 : - 1; // first we generate the center vertex data of the cap. // because the geometry needs one set of uvs per face, // we must generate a center vertex per face/segment for ( let x = 1; x <= radialSegments; x ++ ) { // vertex vertices.push( 0, halfHeight * sign, 0 ); // normal normals.push( 0, sign, 0 ); // uv uvs.push( 0.5, 0.5 ); // increase index index ++; } // save the index of the last center vertex const centerIndexEnd = index; // now we generate the surrounding vertices, normals and uvs for ( let x = 0; x <= radialSegments; x ++ ) { const u = x / radialSegments; const theta = u * thetaLength + thetaStart; const cosTheta = Math.cos( theta ); const sinTheta = Math.sin( theta ); // vertex vertex.x = radius * sinTheta; vertex.y = halfHeight * sign; vertex.z = radius * cosTheta; vertices.push( vertex.x, vertex.y, vertex.z ); // normal normals.push( 0, sign, 0 ); // uv uv.x = ( cosTheta * 0.5 ) + 0.5; uv.y = ( sinTheta * 0.5 * sign ) + 0.5; uvs.push( uv.x, uv.y ); // increase index index ++; } // generate indices for ( let x = 0; x < radialSegments; x ++ ) { const c = centerIndexStart + x; const i = centerIndexEnd + x; if ( top === true ) { // face top indices.push( i, i + 1, c ); } else { // face bottom indices.push( i + 1, i, c ); } groupCount += 3; } // add a group to the geometry. this will ensure multi material support scope.addGroup( groupStart, groupCount, top === true ? 1 : 2 ); // calculate new start value for groups groupStart += groupCount; } } } class CylinderGeometry extends Geometry { constructor( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) { super(); this.type = 'CylinderGeometry'; this.parameters = { radiusTop: radiusTop, radiusBottom: radiusBottom, height: height, radialSegments: radialSegments, heightSegments: heightSegments, openEnded: openEnded, thetaStart: thetaStart, thetaLength: thetaLength }; this.fromBufferGeometry( new CylinderBufferGeometry( radiusTop, radiusBottom, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) ); this.mergeVertices(); } } class ConeGeometry extends CylinderGeometry { constructor( radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ) { super( 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ); this.type = 'ConeGeometry'; this.parameters = { radius: radius, height: height, radialSegments: radialSegments, heightSegments: heightSegments, openEnded: openEnded, thetaStart: thetaStart, thetaLength: thetaLength }; } } class ConeBufferGeometry extends CylinderBufferGeometry { constructor( radius = 1, height = 1, radialSegments = 8, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 ) { super( 0, radius, height, radialSegments, heightSegments, openEnded, thetaStart, thetaLength ); this.type = 'ConeBufferGeometry'; this.parameters = { radius: radius, height: height, radialSegments: radialSegments, heightSegments: heightSegments, openEnded: openEnded, thetaStart: thetaStart, thetaLength: thetaLength }; } } class PolyhedronBufferGeometry extends BufferGeometry { constructor( vertices, indices, radius = 1, detail = 0 ) { super(); this.type = 'PolyhedronBufferGeometry'; this.parameters = { vertices: vertices, indices: indices, radius: radius, detail: detail }; // default buffer data const vertexBuffer = []; const uvBuffer = []; // the subdivision creates the vertex buffer data subdivide( detail ); // all vertices should lie on a conceptual sphere with a given radius applyRadius( radius ); // finally, create the uv data generateUVs(); // build non-indexed geometry this.setAttribute( 'position', new Float32BufferAttribute( vertexBuffer, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( vertexBuffer.slice(), 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvBuffer, 2 ) ); if ( detail === 0 ) { this.computeVertexNormals(); // flat normals } else { this.normalizeNormals(); // smooth normals } // helper functions function subdivide( detail ) { const a = new Vector3(); const b = new Vector3(); const c = new Vector3(); // iterate over all faces and apply a subdivison with the given detail value for ( let i = 0; i < indices.length; i += 3 ) { // get the vertices of the face getVertexByIndex( indices[ i + 0 ], a ); getVertexByIndex( indices[ i + 1 ], b ); getVertexByIndex( indices[ i + 2 ], c ); // perform subdivision subdivideFace( a, b, c, detail ); } } function subdivideFace( a, b, c, detail ) { const cols = detail + 1; // we use this multidimensional array as a data structure for creating the subdivision const v = []; // construct all of the vertices for this subdivision for ( let i = 0; i <= cols; i ++ ) { v[ i ] = []; const aj = a.clone().lerp( c, i / cols ); const bj = b.clone().lerp( c, i / cols ); const rows = cols - i; for ( let j = 0; j <= rows; j ++ ) { if ( j === 0 && i === cols ) { v[ i ][ j ] = aj; } else { v[ i ][ j ] = aj.clone().lerp( bj, j / rows ); } } } // construct all of the faces for ( let i = 0; i < cols; i ++ ) { for ( let j = 0; j < 2 * ( cols - i ) - 1; j ++ ) { const k = Math.floor( j / 2 ); if ( j % 2 === 0 ) { pushVertex( v[ i ][ k + 1 ] ); pushVertex( v[ i + 1 ][ k ] ); pushVertex( v[ i ][ k ] ); } else { pushVertex( v[ i ][ k + 1 ] ); pushVertex( v[ i + 1 ][ k + 1 ] ); pushVertex( v[ i + 1 ][ k ] ); } } } } function applyRadius( radius ) { const vertex = new Vector3(); // iterate over the entire buffer and apply the radius to each vertex for ( let i = 0; i < vertexBuffer.length; i += 3 ) { vertex.x = vertexBuffer[ i + 0 ]; vertex.y = vertexBuffer[ i + 1 ]; vertex.z = vertexBuffer[ i + 2 ]; vertex.normalize().multiplyScalar( radius ); vertexBuffer[ i + 0 ] = vertex.x; vertexBuffer[ i + 1 ] = vertex.y; vertexBuffer[ i + 2 ] = vertex.z; } } function generateUVs() { const vertex = new Vector3(); for ( let i = 0; i < vertexBuffer.length; i += 3 ) { vertex.x = vertexBuffer[ i + 0 ]; vertex.y = vertexBuffer[ i + 1 ]; vertex.z = vertexBuffer[ i + 2 ]; const u = azimuth( vertex ) / 2 / Math.PI + 0.5; const v = inclination( vertex ) / Math.PI + 0.5; uvBuffer.push( u, 1 - v ); } correctUVs(); correctSeam(); } function correctSeam() { // handle case when face straddles the seam, see #3269 for ( let i = 0; i < uvBuffer.length; i += 6 ) { // uv data of a single face const x0 = uvBuffer[ i + 0 ]; const x1 = uvBuffer[ i + 2 ]; const x2 = uvBuffer[ i + 4 ]; const max = Math.max( x0, x1, x2 ); const min = Math.min( x0, x1, x2 ); // 0.9 is somewhat arbitrary if ( max > 0.9 && min < 0.1 ) { if ( x0 < 0.2 ) uvBuffer[ i + 0 ] += 1; if ( x1 < 0.2 ) uvBuffer[ i + 2 ] += 1; if ( x2 < 0.2 ) uvBuffer[ i + 4 ] += 1; } } } function pushVertex( vertex ) { vertexBuffer.push( vertex.x, vertex.y, vertex.z ); } function getVertexByIndex( index, vertex ) { const stride = index * 3; vertex.x = vertices[ stride + 0 ]; vertex.y = vertices[ stride + 1 ]; vertex.z = vertices[ stride + 2 ]; } function correctUVs() { const a = new Vector3(); const b = new Vector3(); const c = new Vector3(); const centroid = new Vector3(); const uvA = new Vector2(); const uvB = new Vector2(); const uvC = new Vector2(); for ( let i = 0, j = 0; i < vertexBuffer.length; i += 9, j += 6 ) { a.set( vertexBuffer[ i + 0 ], vertexBuffer[ i + 1 ], vertexBuffer[ i + 2 ] ); b.set( vertexBuffer[ i + 3 ], vertexBuffer[ i + 4 ], vertexBuffer[ i + 5 ] ); c.set( vertexBuffer[ i + 6 ], vertexBuffer[ i + 7 ], vertexBuffer[ i + 8 ] ); uvA.set( uvBuffer[ j + 0 ], uvBuffer[ j + 1 ] ); uvB.set( uvBuffer[ j + 2 ], uvBuffer[ j + 3 ] ); uvC.set( uvBuffer[ j + 4 ], uvBuffer[ j + 5 ] ); centroid.copy( a ).add( b ).add( c ).divideScalar( 3 ); const azi = azimuth( centroid ); correctUV( uvA, j + 0, a, azi ); correctUV( uvB, j + 2, b, azi ); correctUV( uvC, j + 4, c, azi ); } } function correctUV( uv, stride, vector, azimuth ) { if ( ( azimuth < 0 ) && ( uv.x === 1 ) ) { uvBuffer[ stride ] = uv.x - 1; } if ( ( vector.x === 0 ) && ( vector.z === 0 ) ) { uvBuffer[ stride ] = azimuth / 2 / Math.PI + 0.5; } } // Angle around the Y axis, counter-clockwise when looking from above. function azimuth( vector ) { return Math.atan2( vector.z, - vector.x ); } // Angle above the XZ plane. function inclination( vector ) { return Math.atan2( - vector.y, Math.sqrt( ( vector.x * vector.x ) + ( vector.z * vector.z ) ) ); } } } class DodecahedronBufferGeometry extends PolyhedronBufferGeometry { constructor( radius = 1, detail = 0 ) { const t = ( 1 + Math.sqrt( 5 ) ) / 2; const r = 1 / t; const vertices = [ // (±1, ±1, ±1) - 1, - 1, - 1, - 1, - 1, 1, - 1, 1, - 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, 1, 1, 1, - 1, 1, 1, 1, // (0, ±1/φ, ±φ) 0, - r, - t, 0, - r, t, 0, r, - t, 0, r, t, // (±1/φ, ±φ, 0) - r, - t, 0, - r, t, 0, r, - t, 0, r, t, 0, // (±φ, 0, ±1/φ) - t, 0, - r, t, 0, - r, - t, 0, r, t, 0, r ]; const indices = [ 3, 11, 7, 3, 7, 15, 3, 15, 13, 7, 19, 17, 7, 17, 6, 7, 6, 15, 17, 4, 8, 17, 8, 10, 17, 10, 6, 8, 0, 16, 8, 16, 2, 8, 2, 10, 0, 12, 1, 0, 1, 18, 0, 18, 16, 6, 10, 2, 6, 2, 13, 6, 13, 15, 2, 16, 18, 2, 18, 3, 2, 3, 13, 18, 1, 9, 18, 9, 11, 18, 11, 3, 4, 14, 12, 4, 12, 0, 4, 0, 8, 11, 9, 5, 11, 5, 19, 11, 19, 7, 19, 5, 14, 19, 14, 4, 19, 4, 17, 1, 12, 14, 1, 14, 5, 1, 5, 9 ]; super( vertices, indices, radius, detail ); this.type = 'DodecahedronBufferGeometry'; this.parameters = { radius: radius, detail: detail }; } } class DodecahedronGeometry extends Geometry { constructor( radius, detail ) { super(); this.type = 'DodecahedronGeometry'; this.parameters = { radius: radius, detail: detail }; this.fromBufferGeometry( new DodecahedronBufferGeometry( radius, detail ) ); this.mergeVertices(); } } const _v0$2 = new Vector3(); const _v1$5 = new Vector3(); const _normal$1 = new Vector3(); const _triangle = new Triangle(); class EdgesGeometry extends BufferGeometry { constructor( geometry, thresholdAngle ) { super(); this.type = 'EdgesGeometry'; this.parameters = { thresholdAngle: thresholdAngle }; thresholdAngle = ( thresholdAngle !== undefined ) ? thresholdAngle : 1; if ( geometry.isGeometry ) { geometry = new BufferGeometry().fromGeometry( geometry ); } const precisionPoints = 4; const precision = Math.pow( 10, precisionPoints ); const thresholdDot = Math.cos( MathUtils.DEG2RAD * thresholdAngle ); const indexAttr = geometry.getIndex(); const positionAttr = geometry.getAttribute( 'position' ); const indexCount = indexAttr ? indexAttr.count : positionAttr.count; const indexArr = [ 0, 0, 0 ]; const vertKeys = [ 'a', 'b', 'c' ]; const hashes = new Array( 3 ); const edgeData = {}; const vertices = []; for ( let i = 0; i < indexCount; i += 3 ) { if ( indexAttr ) { indexArr[ 0 ] = indexAttr.getX( i ); indexArr[ 1 ] = indexAttr.getX( i + 1 ); indexArr[ 2 ] = indexAttr.getX( i + 2 ); } else { indexArr[ 0 ] = i; indexArr[ 1 ] = i + 1; indexArr[ 2 ] = i + 2; } const { a, b, c } = _triangle; a.fromBufferAttribute( positionAttr, indexArr[ 0 ] ); b.fromBufferAttribute( positionAttr, indexArr[ 1 ] ); c.fromBufferAttribute( positionAttr, indexArr[ 2 ] ); _triangle.getNormal( _normal$1 ); // create hashes for the edge from the vertices hashes[ 0 ] = `${ Math.round( a.x * precision ) },${ Math.round( a.y * precision ) },${ Math.round( a.z * precision ) }`; hashes[ 1 ] = `${ Math.round( b.x * precision ) },${ Math.round( b.y * precision ) },${ Math.round( b.z * precision ) }`; hashes[ 2 ] = `${ Math.round( c.x * precision ) },${ Math.round( c.y * precision ) },${ Math.round( c.z * precision ) }`; // skip degenerate triangles if ( hashes[ 0 ] === hashes[ 1 ] || hashes[ 1 ] === hashes[ 2 ] || hashes[ 2 ] === hashes[ 0 ] ) { continue; } // iterate over every edge for ( let j = 0; j < 3; j ++ ) { // get the first and next vertex making up the edge const jNext = ( j + 1 ) % 3; const vecHash0 = hashes[ j ]; const vecHash1 = hashes[ jNext ]; const v0 = _triangle[ vertKeys[ j ] ]; const v1 = _triangle[ vertKeys[ jNext ] ]; const hash = `${ vecHash0 }_${ vecHash1 }`; const reverseHash = `${ vecHash1 }_${ vecHash0 }`; if ( reverseHash in edgeData && edgeData[ reverseHash ] ) { // if we found a sibling edge add it into the vertex array if // it meets the angle threshold and delete the edge from the map. if ( _normal$1.dot( edgeData[ reverseHash ].normal ) <= thresholdDot ) { vertices.push( v0.x, v0.y, v0.z ); vertices.push( v1.x, v1.y, v1.z ); } edgeData[ reverseHash ] = null; } else if ( ! ( hash in edgeData ) ) { // if we've already got an edge here then skip adding a new one edgeData[ hash ] = { index0: indexArr[ j ], index1: indexArr[ jNext ], normal: _normal$1.clone(), }; } } } // iterate over all remaining, unmatched edges and add them to the vertex array for ( const key in edgeData ) { if ( edgeData[ key ] ) { const { index0, index1 } = edgeData[ key ]; _v0$2.fromBufferAttribute( positionAttr, index0 ); _v1$5.fromBufferAttribute( positionAttr, index1 ); vertices.push( _v0$2.x, _v0$2.y, _v0$2.z ); vertices.push( _v1$5.x, _v1$5.y, _v1$5.z ); } } this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); } } /** * Port from https://github.com/mapbox/earcut (v2.2.2) */ const Earcut = { triangulate: function ( data, holeIndices, dim ) { dim = dim || 2; const hasHoles = holeIndices && holeIndices.length; const outerLen = hasHoles ? holeIndices[ 0 ] * dim : data.length; let outerNode = linkedList( data, 0, outerLen, dim, true ); const triangles = []; if ( ! outerNode || outerNode.next === outerNode.prev ) return triangles; let minX, minY, maxX, maxY, x, y, invSize; if ( hasHoles ) outerNode = eliminateHoles( data, holeIndices, outerNode, dim ); // if the shape is not too simple, we'll use z-order curve hash later; calculate polygon bbox if ( data.length > 80 * dim ) { minX = maxX = data[ 0 ]; minY = maxY = data[ 1 ]; for ( let i = dim; i < outerLen; i += dim ) { x = data[ i ]; y = data[ i + 1 ]; if ( x < minX ) minX = x; if ( y < minY ) minY = y; if ( x > maxX ) maxX = x; if ( y > maxY ) maxY = y; } // minX, minY and invSize are later used to transform coords into integers for z-order calculation invSize = Math.max( maxX - minX, maxY - minY ); invSize = invSize !== 0 ? 1 / invSize : 0; } earcutLinked( outerNode, triangles, dim, minX, minY, invSize ); return triangles; } }; // create a circular doubly linked list from polygon points in the specified winding order function linkedList( data, start, end, dim, clockwise ) { let i, last; if ( clockwise === ( signedArea( data, start, end, dim ) > 0 ) ) { for ( i = start; i < end; i += dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last ); } else { for ( i = end - dim; i >= start; i -= dim ) last = insertNode( i, data[ i ], data[ i + 1 ], last ); } if ( last && equals( last, last.next ) ) { removeNode( last ); last = last.next; } return last; } // eliminate colinear or duplicate points function filterPoints( start, end ) { if ( ! start ) return start; if ( ! end ) end = start; let p = start, again; do { again = false; if ( ! p.steiner && ( equals( p, p.next ) || area( p.prev, p, p.next ) === 0 ) ) { removeNode( p ); p = end = p.prev; if ( p === p.next ) break; again = true; } else { p = p.next; } } while ( again || p !== end ); return end; } // main ear slicing loop which triangulates a polygon (given as a linked list) function earcutLinked( ear, triangles, dim, minX, minY, invSize, pass ) { if ( ! ear ) return; // interlink polygon nodes in z-order if ( ! pass && invSize ) indexCurve( ear, minX, minY, invSize ); let stop = ear, prev, next; // iterate through ears, slicing them one by one while ( ear.prev !== ear.next ) { prev = ear.prev; next = ear.next; if ( invSize ? isEarHashed( ear, minX, minY, invSize ) : isEar( ear ) ) { // cut off the triangle triangles.push( prev.i / dim ); triangles.push( ear.i / dim ); triangles.push( next.i / dim ); removeNode( ear ); // skipping the next vertex leads to less sliver triangles ear = next.next; stop = next.next; continue; } ear = next; // if we looped through the whole remaining polygon and can't find any more ears if ( ear === stop ) { // try filtering points and slicing again if ( ! pass ) { earcutLinked( filterPoints( ear ), triangles, dim, minX, minY, invSize, 1 ); // if this didn't work, try curing all small self-intersections locally } else if ( pass === 1 ) { ear = cureLocalIntersections( filterPoints( ear ), triangles, dim ); earcutLinked( ear, triangles, dim, minX, minY, invSize, 2 ); // as a last resort, try splitting the remaining polygon into two } else if ( pass === 2 ) { splitEarcut( ear, triangles, dim, minX, minY, invSize ); } break; } } } // check whether a polygon node forms a valid ear with adjacent nodes function isEar( ear ) { const a = ear.prev, b = ear, c = ear.next; if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear // now make sure we don't have other points inside the potential ear let p = ear.next.next; while ( p !== ear.prev ) { if ( pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false; p = p.next; } return true; } function isEarHashed( ear, minX, minY, invSize ) { const a = ear.prev, b = ear, c = ear.next; if ( area( a, b, c ) >= 0 ) return false; // reflex, can't be an ear // triangle bbox; min & max are calculated like this for speed const minTX = a.x < b.x ? ( a.x < c.x ? a.x : c.x ) : ( b.x < c.x ? b.x : c.x ), minTY = a.y < b.y ? ( a.y < c.y ? a.y : c.y ) : ( b.y < c.y ? b.y : c.y ), maxTX = a.x > b.x ? ( a.x > c.x ? a.x : c.x ) : ( b.x > c.x ? b.x : c.x ), maxTY = a.y > b.y ? ( a.y > c.y ? a.y : c.y ) : ( b.y > c.y ? b.y : c.y ); // z-order range for the current triangle bbox; const minZ = zOrder( minTX, minTY, minX, minY, invSize ), maxZ = zOrder( maxTX, maxTY, minX, minY, invSize ); let p = ear.prevZ, n = ear.nextZ; // look for points inside the triangle in both directions while ( p && p.z >= minZ && n && n.z <= maxZ ) { if ( p !== ear.prev && p !== ear.next && pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false; p = p.prevZ; if ( n !== ear.prev && n !== ear.next && pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false; n = n.nextZ; } // look for remaining points in decreasing z-order while ( p && p.z >= minZ ) { if ( p !== ear.prev && p !== ear.next && pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, p.x, p.y ) && area( p.prev, p, p.next ) >= 0 ) return false; p = p.prevZ; } // look for remaining points in increasing z-order while ( n && n.z <= maxZ ) { if ( n !== ear.prev && n !== ear.next && pointInTriangle( a.x, a.y, b.x, b.y, c.x, c.y, n.x, n.y ) && area( n.prev, n, n.next ) >= 0 ) return false; n = n.nextZ; } return true; } // go through all polygon nodes and cure small local self-intersections function cureLocalIntersections( start, triangles, dim ) { let p = start; do { const a = p.prev, b = p.next.next; if ( ! equals( a, b ) && intersects( a, p, p.next, b ) && locallyInside( a, b ) && locallyInside( b, a ) ) { triangles.push( a.i / dim ); triangles.push( p.i / dim ); triangles.push( b.i / dim ); // remove two nodes involved removeNode( p ); removeNode( p.next ); p = start = b; } p = p.next; } while ( p !== start ); return filterPoints( p ); } // try splitting polygon into two and triangulate them independently function splitEarcut( start, triangles, dim, minX, minY, invSize ) { // look for a valid diagonal that divides the polygon into two let a = start; do { let b = a.next.next; while ( b !== a.prev ) { if ( a.i !== b.i && isValidDiagonal( a, b ) ) { // split the polygon in two by the diagonal let c = splitPolygon( a, b ); // filter colinear points around the cuts a = filterPoints( a, a.next ); c = filterPoints( c, c.next ); // run earcut on each half earcutLinked( a, triangles, dim, minX, minY, invSize ); earcutLinked( c, triangles, dim, minX, minY, invSize ); return; } b = b.next; } a = a.next; } while ( a !== start ); } // link every hole into the outer loop, producing a single-ring polygon without holes function eliminateHoles( data, holeIndices, outerNode, dim ) { const queue = []; let i, len, start, end, list; for ( i = 0, len = holeIndices.length; i < len; i ++ ) { start = holeIndices[ i ] * dim; end = i < len - 1 ? holeIndices[ i + 1 ] * dim : data.length; list = linkedList( data, start, end, dim, false ); if ( list === list.next ) list.steiner = true; queue.push( getLeftmost( list ) ); } queue.sort( compareX ); // process holes from left to right for ( i = 0; i < queue.length; i ++ ) { eliminateHole( queue[ i ], outerNode ); outerNode = filterPoints( outerNode, outerNode.next ); } return outerNode; } function compareX( a, b ) { return a.x - b.x; } // find a bridge between vertices that connects hole with an outer ring and and link it function eliminateHole( hole, outerNode ) { outerNode = findHoleBridge( hole, outerNode ); if ( outerNode ) { const b = splitPolygon( outerNode, hole ); // filter collinear points around the cuts filterPoints( outerNode, outerNode.next ); filterPoints( b, b.next ); } } // David Eberly's algorithm for finding a bridge between hole and outer polygon function findHoleBridge( hole, outerNode ) { let p = outerNode; const hx = hole.x; const hy = hole.y; let qx = - Infinity, m; // find a segment intersected by a ray from the hole's leftmost point to the left; // segment's endpoint with lesser x will be potential connection point do { if ( hy <= p.y && hy >= p.next.y && p.next.y !== p.y ) { const x = p.x + ( hy - p.y ) * ( p.next.x - p.x ) / ( p.next.y - p.y ); if ( x <= hx && x > qx ) { qx = x; if ( x === hx ) { if ( hy === p.y ) return p; if ( hy === p.next.y ) return p.next; } m = p.x < p.next.x ? p : p.next; } } p = p.next; } while ( p !== outerNode ); if ( ! m ) return null; if ( hx === qx ) return m; // hole touches outer segment; pick leftmost endpoint // look for points inside the triangle of hole point, segment intersection and endpoint; // if there are no points found, we have a valid connection; // otherwise choose the point of the minimum angle with the ray as connection point const stop = m, mx = m.x, my = m.y; let tanMin = Infinity, tan; p = m; do { if ( hx >= p.x && p.x >= mx && hx !== p.x && pointInTriangle( hy < my ? hx : qx, hy, mx, my, hy < my ? qx : hx, hy, p.x, p.y ) ) { tan = Math.abs( hy - p.y ) / ( hx - p.x ); // tangential if ( locallyInside( p, hole ) && ( tan < tanMin || ( tan === tanMin && ( p.x > m.x || ( p.x === m.x && sectorContainsSector( m, p ) ) ) ) ) ) { m = p; tanMin = tan; } } p = p.next; } while ( p !== stop ); return m; } // whether sector in vertex m contains sector in vertex p in the same coordinates function sectorContainsSector( m, p ) { return area( m.prev, m, p.prev ) < 0 && area( p.next, m, m.next ) < 0; } // interlink polygon nodes in z-order function indexCurve( start, minX, minY, invSize ) { let p = start; do { if ( p.z === null ) p.z = zOrder( p.x, p.y, minX, minY, invSize ); p.prevZ = p.prev; p.nextZ = p.next; p = p.next; } while ( p !== start ); p.prevZ.nextZ = null; p.prevZ = null; sortLinked( p ); } // Simon Tatham's linked list merge sort algorithm // http://www.chiark.greenend.org.uk/~sgtatham/algorithms/listsort.html function sortLinked( list ) { let i, p, q, e, tail, numMerges, pSize, qSize, inSize = 1; do { p = list; list = null; tail = null; numMerges = 0; while ( p ) { numMerges ++; q = p; pSize = 0; for ( i = 0; i < inSize; i ++ ) { pSize ++; q = q.nextZ; if ( ! q ) break; } qSize = inSize; while ( pSize > 0 || ( qSize > 0 && q ) ) { if ( pSize !== 0 && ( qSize === 0 || ! q || p.z <= q.z ) ) { e = p; p = p.nextZ; pSize --; } else { e = q; q = q.nextZ; qSize --; } if ( tail ) tail.nextZ = e; else list = e; e.prevZ = tail; tail = e; } p = q; } tail.nextZ = null; inSize *= 2; } while ( numMerges > 1 ); return list; } // z-order of a point given coords and inverse of the longer side of data bbox function zOrder( x, y, minX, minY, invSize ) { // coords are transformed into non-negative 15-bit integer range x = 32767 * ( x - minX ) * invSize; y = 32767 * ( y - minY ) * invSize; x = ( x | ( x << 8 ) ) & 0x00FF00FF; x = ( x | ( x << 4 ) ) & 0x0F0F0F0F; x = ( x | ( x << 2 ) ) & 0x33333333; x = ( x | ( x << 1 ) ) & 0x55555555; y = ( y | ( y << 8 ) ) & 0x00FF00FF; y = ( y | ( y << 4 ) ) & 0x0F0F0F0F; y = ( y | ( y << 2 ) ) & 0x33333333; y = ( y | ( y << 1 ) ) & 0x55555555; return x | ( y << 1 ); } // find the leftmost node of a polygon ring function getLeftmost( start ) { let p = start, leftmost = start; do { if ( p.x < leftmost.x || ( p.x === leftmost.x && p.y < leftmost.y ) ) leftmost = p; p = p.next; } while ( p !== start ); return leftmost; } // check if a point lies within a convex triangle function pointInTriangle( ax, ay, bx, by, cx, cy, px, py ) { return ( cx - px ) * ( ay - py ) - ( ax - px ) * ( cy - py ) >= 0 && ( ax - px ) * ( by - py ) - ( bx - px ) * ( ay - py ) >= 0 && ( bx - px ) * ( cy - py ) - ( cx - px ) * ( by - py ) >= 0; } // check if a diagonal between two polygon nodes is valid (lies in polygon interior) function isValidDiagonal( a, b ) { return a.next.i !== b.i && a.prev.i !== b.i && ! intersectsPolygon( a, b ) && // dones't intersect other edges ( locallyInside( a, b ) && locallyInside( b, a ) && middleInside( a, b ) && // locally visible ( area( a.prev, a, b.prev ) || area( a, b.prev, b ) ) || // does not create opposite-facing sectors equals( a, b ) && area( a.prev, a, a.next ) > 0 && area( b.prev, b, b.next ) > 0 ); // special zero-length case } // signed area of a triangle function area( p, q, r ) { return ( q.y - p.y ) * ( r.x - q.x ) - ( q.x - p.x ) * ( r.y - q.y ); } // check if two points are equal function equals( p1, p2 ) { return p1.x === p2.x && p1.y === p2.y; } // check if two segments intersect function intersects( p1, q1, p2, q2 ) { const o1 = sign( area( p1, q1, p2 ) ); const o2 = sign( area( p1, q1, q2 ) ); const o3 = sign( area( p2, q2, p1 ) ); const o4 = sign( area( p2, q2, q1 ) ); if ( o1 !== o2 && o3 !== o4 ) return true; // general case if ( o1 === 0 && onSegment( p1, p2, q1 ) ) return true; // p1, q1 and p2 are collinear and p2 lies on p1q1 if ( o2 === 0 && onSegment( p1, q2, q1 ) ) return true; // p1, q1 and q2 are collinear and q2 lies on p1q1 if ( o3 === 0 && onSegment( p2, p1, q2 ) ) return true; // p2, q2 and p1 are collinear and p1 lies on p2q2 if ( o4 === 0 && onSegment( p2, q1, q2 ) ) return true; // p2, q2 and q1 are collinear and q1 lies on p2q2 return false; } // for collinear points p, q, r, check if point q lies on segment pr function onSegment( p, q, r ) { return q.x <= Math.max( p.x, r.x ) && q.x >= Math.min( p.x, r.x ) && q.y <= Math.max( p.y, r.y ) && q.y >= Math.min( p.y, r.y ); } function sign( num ) { return num > 0 ? 1 : num < 0 ? - 1 : 0; } // check if a polygon diagonal intersects any polygon segments function intersectsPolygon( a, b ) { let p = a; do { if ( p.i !== a.i && p.next.i !== a.i && p.i !== b.i && p.next.i !== b.i && intersects( p, p.next, a, b ) ) return true; p = p.next; } while ( p !== a ); return false; } // check if a polygon diagonal is locally inside the polygon function locallyInside( a, b ) { return area( a.prev, a, a.next ) < 0 ? area( a, b, a.next ) >= 0 && area( a, a.prev, b ) >= 0 : area( a, b, a.prev ) < 0 || area( a, a.next, b ) < 0; } // check if the middle point of a polygon diagonal is inside the polygon function middleInside( a, b ) { let p = a, inside = false; const px = ( a.x + b.x ) / 2, py = ( a.y + b.y ) / 2; do { if ( ( ( p.y > py ) !== ( p.next.y > py ) ) && p.next.y !== p.y && ( px < ( p.next.x - p.x ) * ( py - p.y ) / ( p.next.y - p.y ) + p.x ) ) inside = ! inside; p = p.next; } while ( p !== a ); return inside; } // link two polygon vertices with a bridge; if the vertices belong to the same ring, it splits polygon into two; // if one belongs to the outer ring and another to a hole, it merges it into a single ring function splitPolygon( a, b ) { const a2 = new Node( a.i, a.x, a.y ), b2 = new Node( b.i, b.x, b.y ), an = a.next, bp = b.prev; a.next = b; b.prev = a; a2.next = an; an.prev = a2; b2.next = a2; a2.prev = b2; bp.next = b2; b2.prev = bp; return b2; } // create a node and optionally link it with previous one (in a circular doubly linked list) function insertNode( i, x, y, last ) { const p = new Node( i, x, y ); if ( ! last ) { p.prev = p; p.next = p; } else { p.next = last.next; p.prev = last; last.next.prev = p; last.next = p; } return p; } function removeNode( p ) { p.next.prev = p.prev; p.prev.next = p.next; if ( p.prevZ ) p.prevZ.nextZ = p.nextZ; if ( p.nextZ ) p.nextZ.prevZ = p.prevZ; } function Node( i, x, y ) { // vertex index in coordinates array this.i = i; // vertex coordinates this.x = x; this.y = y; // previous and next vertex nodes in a polygon ring this.prev = null; this.next = null; // z-order curve value this.z = null; // previous and next nodes in z-order this.prevZ = null; this.nextZ = null; // indicates whether this is a steiner point this.steiner = false; } function signedArea( data, start, end, dim ) { let sum = 0; for ( let i = start, j = end - dim; i < end; i += dim ) { sum += ( data[ j ] - data[ i ] ) * ( data[ i + 1 ] + data[ j + 1 ] ); j = i; } return sum; } const ShapeUtils = { // calculate area of the contour polygon area: function ( contour ) { const n = contour.length; let a = 0.0; for ( let p = n - 1, q = 0; q < n; p = q ++ ) { a += contour[ p ].x * contour[ q ].y - contour[ q ].x * contour[ p ].y; } return a * 0.5; }, isClockWise: function ( pts ) { return ShapeUtils.area( pts ) < 0; }, triangulateShape: function ( contour, holes ) { const vertices = []; // flat array of vertices like [ x0,y0, x1,y1, x2,y2, ... ] const holeIndices = []; // array of hole indices const faces = []; // final array of vertex indices like [ [ a,b,d ], [ b,c,d ] ] removeDupEndPts( contour ); addContour( vertices, contour ); // let holeIndex = contour.length; holes.forEach( removeDupEndPts ); for ( let i = 0; i < holes.length; i ++ ) { holeIndices.push( holeIndex ); holeIndex += holes[ i ].length; addContour( vertices, holes[ i ] ); } // const triangles = Earcut.triangulate( vertices, holeIndices ); // for ( let i = 0; i < triangles.length; i += 3 ) { faces.push( triangles.slice( i, i + 3 ) ); } return faces; } }; function removeDupEndPts( points ) { const l = points.length; if ( l > 2 && points[ l - 1 ].equals( points[ 0 ] ) ) { points.pop(); } } function addContour( vertices, contour ) { for ( let i = 0; i < contour.length; i ++ ) { vertices.push( contour[ i ].x ); vertices.push( contour[ i ].y ); } } /** * Creates extruded geometry from a path shape. * * parameters = { * * curveSegments: , // number of points on the curves * steps: , // number of points for z-side extrusions / used for subdividing segments of extrude spline too * depth: , // Depth to extrude the shape * * bevelEnabled: , // turn on bevel * bevelThickness: , // how deep into the original shape bevel goes * bevelSize: , // how far from shape outline (including bevelOffset) is bevel * bevelOffset: , // how far from shape outline does bevel start * bevelSegments: , // number of bevel layers * * extrudePath: // curve to extrude shape along * * UVGenerator: // object that provides UV generator functions * * } */ class ExtrudeBufferGeometry extends BufferGeometry { constructor( shapes, options ) { super(); this.type = 'ExtrudeBufferGeometry'; this.parameters = { shapes: shapes, options: options }; shapes = Array.isArray( shapes ) ? shapes : [ shapes ]; const scope = this; const verticesArray = []; const uvArray = []; for ( let i = 0, l = shapes.length; i < l; i ++ ) { const shape = shapes[ i ]; addShape( shape ); } // build geometry this.setAttribute( 'position', new Float32BufferAttribute( verticesArray, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvArray, 2 ) ); this.computeVertexNormals(); // functions function addShape( shape ) { const placeholder = []; // options const curveSegments = options.curveSegments !== undefined ? options.curveSegments : 12; const steps = options.steps !== undefined ? options.steps : 1; let depth = options.depth !== undefined ? options.depth : 100; let bevelEnabled = options.bevelEnabled !== undefined ? options.bevelEnabled : true; let bevelThickness = options.bevelThickness !== undefined ? options.bevelThickness : 6; let bevelSize = options.bevelSize !== undefined ? options.bevelSize : bevelThickness - 2; let bevelOffset = options.bevelOffset !== undefined ? options.bevelOffset : 0; let bevelSegments = options.bevelSegments !== undefined ? options.bevelSegments : 3; const extrudePath = options.extrudePath; const uvgen = options.UVGenerator !== undefined ? options.UVGenerator : WorldUVGenerator; // deprecated options if ( options.amount !== undefined ) { console.warn( 'THREE.ExtrudeBufferGeometry: amount has been renamed to depth.' ); depth = options.amount; } // let extrudePts, extrudeByPath = false; let splineTube, binormal, normal, position2; if ( extrudePath ) { extrudePts = extrudePath.getSpacedPoints( steps ); extrudeByPath = true; bevelEnabled = false; // bevels not supported for path extrusion // SETUP TNB variables // TODO1 - have a .isClosed in spline? splineTube = extrudePath.computeFrenetFrames( steps, false ); // console.log(splineTube, 'splineTube', splineTube.normals.length, 'steps', steps, 'extrudePts', extrudePts.length); binormal = new Vector3(); normal = new Vector3(); position2 = new Vector3(); } // Safeguards if bevels are not enabled if ( ! bevelEnabled ) { bevelSegments = 0; bevelThickness = 0; bevelSize = 0; bevelOffset = 0; } // Variables initialization const shapePoints = shape.extractPoints( curveSegments ); let vertices = shapePoints.shape; const holes = shapePoints.holes; const reverse = ! ShapeUtils.isClockWise( vertices ); if ( reverse ) { vertices = vertices.reverse(); // Maybe we should also check if holes are in the opposite direction, just to be safe ... for ( let h = 0, hl = holes.length; h < hl; h ++ ) { const ahole = holes[ h ]; if ( ShapeUtils.isClockWise( ahole ) ) { holes[ h ] = ahole.reverse(); } } } const faces = ShapeUtils.triangulateShape( vertices, holes ); /* Vertices */ const contour = vertices; // vertices has all points but contour has only points of circumference for ( let h = 0, hl = holes.length; h < hl; h ++ ) { const ahole = holes[ h ]; vertices = vertices.concat( ahole ); } function scalePt2( pt, vec, size ) { if ( ! vec ) console.error( 'THREE.ExtrudeGeometry: vec does not exist' ); return vec.clone().multiplyScalar( size ).add( pt ); } const vlen = vertices.length, flen = faces.length; // Find directions for point movement function getBevelVec( inPt, inPrev, inNext ) { // computes for inPt the corresponding point inPt' on a new contour // shifted by 1 unit (length of normalized vector) to the left // if we walk along contour clockwise, this new contour is outside the old one // // inPt' is the intersection of the two lines parallel to the two // adjacent edges of inPt at a distance of 1 unit on the left side. let v_trans_x, v_trans_y, shrink_by; // resulting translation vector for inPt // good reading for geometry algorithms (here: line-line intersection) // http://geomalgorithms.com/a05-_intersect-1.html const v_prev_x = inPt.x - inPrev.x, v_prev_y = inPt.y - inPrev.y; const v_next_x = inNext.x - inPt.x, v_next_y = inNext.y - inPt.y; const v_prev_lensq = ( v_prev_x * v_prev_x + v_prev_y * v_prev_y ); // check for collinear edges const collinear0 = ( v_prev_x * v_next_y - v_prev_y * v_next_x ); if ( Math.abs( collinear0 ) > Number.EPSILON ) { // not collinear // length of vectors for normalizing const v_prev_len = Math.sqrt( v_prev_lensq ); const v_next_len = Math.sqrt( v_next_x * v_next_x + v_next_y * v_next_y ); // shift adjacent points by unit vectors to the left const ptPrevShift_x = ( inPrev.x - v_prev_y / v_prev_len ); const ptPrevShift_y = ( inPrev.y + v_prev_x / v_prev_len ); const ptNextShift_x = ( inNext.x - v_next_y / v_next_len ); const ptNextShift_y = ( inNext.y + v_next_x / v_next_len ); // scaling factor for v_prev to intersection point const sf = ( ( ptNextShift_x - ptPrevShift_x ) * v_next_y - ( ptNextShift_y - ptPrevShift_y ) * v_next_x ) / ( v_prev_x * v_next_y - v_prev_y * v_next_x ); // vector from inPt to intersection point v_trans_x = ( ptPrevShift_x + v_prev_x * sf - inPt.x ); v_trans_y = ( ptPrevShift_y + v_prev_y * sf - inPt.y ); // Don't normalize!, otherwise sharp corners become ugly // but prevent crazy spikes const v_trans_lensq = ( v_trans_x * v_trans_x + v_trans_y * v_trans_y ); if ( v_trans_lensq <= 2 ) { return new Vector2( v_trans_x, v_trans_y ); } else { shrink_by = Math.sqrt( v_trans_lensq / 2 ); } } else { // handle special case of collinear edges let direction_eq = false; // assumes: opposite if ( v_prev_x > Number.EPSILON ) { if ( v_next_x > Number.EPSILON ) { direction_eq = true; } } else { if ( v_prev_x < - Number.EPSILON ) { if ( v_next_x < - Number.EPSILON ) { direction_eq = true; } } else { if ( Math.sign( v_prev_y ) === Math.sign( v_next_y ) ) { direction_eq = true; } } } if ( direction_eq ) { // console.log("Warning: lines are a straight sequence"); v_trans_x = - v_prev_y; v_trans_y = v_prev_x; shrink_by = Math.sqrt( v_prev_lensq ); } else { // console.log("Warning: lines are a straight spike"); v_trans_x = v_prev_x; v_trans_y = v_prev_y; shrink_by = Math.sqrt( v_prev_lensq / 2 ); } } return new Vector2( v_trans_x / shrink_by, v_trans_y / shrink_by ); } const contourMovements = []; for ( let i = 0, il = contour.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) { if ( j === il ) j = 0; if ( k === il ) k = 0; // (j)---(i)---(k) // console.log('i,j,k', i, j , k) contourMovements[ i ] = getBevelVec( contour[ i ], contour[ j ], contour[ k ] ); } const holesMovements = []; let oneHoleMovements, verticesMovements = contourMovements.concat(); for ( let h = 0, hl = holes.length; h < hl; h ++ ) { const ahole = holes[ h ]; oneHoleMovements = []; for ( let i = 0, il = ahole.length, j = il - 1, k = i + 1; i < il; i ++, j ++, k ++ ) { if ( j === il ) j = 0; if ( k === il ) k = 0; // (j)---(i)---(k) oneHoleMovements[ i ] = getBevelVec( ahole[ i ], ahole[ j ], ahole[ k ] ); } holesMovements.push( oneHoleMovements ); verticesMovements = verticesMovements.concat( oneHoleMovements ); } // Loop bevelSegments, 1 for the front, 1 for the back for ( let b = 0; b < bevelSegments; b ++ ) { //for ( b = bevelSegments; b > 0; b -- ) { const t = b / bevelSegments; const z = bevelThickness * Math.cos( t * Math.PI / 2 ); const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset; // contract shape for ( let i = 0, il = contour.length; i < il; i ++ ) { const vert = scalePt2( contour[ i ], contourMovements[ i ], bs ); v( vert.x, vert.y, - z ); } // expand holes for ( let h = 0, hl = holes.length; h < hl; h ++ ) { const ahole = holes[ h ]; oneHoleMovements = holesMovements[ h ]; for ( let i = 0, il = ahole.length; i < il; i ++ ) { const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs ); v( vert.x, vert.y, - z ); } } } const bs = bevelSize + bevelOffset; // Back facing vertices for ( let i = 0; i < vlen; i ++ ) { const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ]; if ( ! extrudeByPath ) { v( vert.x, vert.y, 0 ); } else { // v( vert.x, vert.y + extrudePts[ 0 ].y, extrudePts[ 0 ].x ); normal.copy( splineTube.normals[ 0 ] ).multiplyScalar( vert.x ); binormal.copy( splineTube.binormals[ 0 ] ).multiplyScalar( vert.y ); position2.copy( extrudePts[ 0 ] ).add( normal ).add( binormal ); v( position2.x, position2.y, position2.z ); } } // Add stepped vertices... // Including front facing vertices for ( let s = 1; s <= steps; s ++ ) { for ( let i = 0; i < vlen; i ++ ) { const vert = bevelEnabled ? scalePt2( vertices[ i ], verticesMovements[ i ], bs ) : vertices[ i ]; if ( ! extrudeByPath ) { v( vert.x, vert.y, depth / steps * s ); } else { // v( vert.x, vert.y + extrudePts[ s - 1 ].y, extrudePts[ s - 1 ].x ); normal.copy( splineTube.normals[ s ] ).multiplyScalar( vert.x ); binormal.copy( splineTube.binormals[ s ] ).multiplyScalar( vert.y ); position2.copy( extrudePts[ s ] ).add( normal ).add( binormal ); v( position2.x, position2.y, position2.z ); } } } // Add bevel segments planes //for ( b = 1; b <= bevelSegments; b ++ ) { for ( let b = bevelSegments - 1; b >= 0; b -- ) { const t = b / bevelSegments; const z = bevelThickness * Math.cos( t * Math.PI / 2 ); const bs = bevelSize * Math.sin( t * Math.PI / 2 ) + bevelOffset; // contract shape for ( let i = 0, il = contour.length; i < il; i ++ ) { const vert = scalePt2( contour[ i ], contourMovements[ i ], bs ); v( vert.x, vert.y, depth + z ); } // expand holes for ( let h = 0, hl = holes.length; h < hl; h ++ ) { const ahole = holes[ h ]; oneHoleMovements = holesMovements[ h ]; for ( let i = 0, il = ahole.length; i < il; i ++ ) { const vert = scalePt2( ahole[ i ], oneHoleMovements[ i ], bs ); if ( ! extrudeByPath ) { v( vert.x, vert.y, depth + z ); } else { v( vert.x, vert.y + extrudePts[ steps - 1 ].y, extrudePts[ steps - 1 ].x + z ); } } } } /* Faces */ // Top and bottom faces if(!options.openEnded){//xzw add 可以选择开口,不创建Top and bottom faces buildLidFaces(); } // Sides faces buildSideFaces(); ///// Internal functions function buildLidFaces() { const start = verticesArray.length / 3; if ( bevelEnabled ) { let layer = 0; // steps + 1 let offset = vlen * layer; // Bottom faces for ( let i = 0; i < flen; i ++ ) { const face = faces[ i ]; f3( face[ 2 ] + offset, face[ 1 ] + offset, face[ 0 ] + offset ); } layer = steps + bevelSegments * 2; offset = vlen * layer; // Top faces for ( let i = 0; i < flen; i ++ ) { const face = faces[ i ]; f3( face[ 0 ] + offset, face[ 1 ] + offset, face[ 2 ] + offset ); } } else { // Bottom faces for ( let i = 0; i < flen; i ++ ) { const face = faces[ i ]; f3( face[ 2 ], face[ 1 ], face[ 0 ] ); } // Top faces for ( let i = 0; i < flen; i ++ ) { const face = faces[ i ]; f3( face[ 0 ] + vlen * steps, face[ 1 ] + vlen * steps, face[ 2 ] + vlen * steps ); } } scope.addGroup( start, verticesArray.length / 3 - start, 0 ); } // Create faces for the z-sides of the shape function buildSideFaces() { const start = verticesArray.length / 3; let layeroffset = 0; sidewalls( contour, layeroffset ); layeroffset += contour.length; for ( let h = 0, hl = holes.length; h < hl; h ++ ) { const ahole = holes[ h ]; sidewalls( ahole, layeroffset ); //, true layeroffset += ahole.length; } scope.addGroup( start, verticesArray.length / 3 - start, 1 ); } function sidewalls( contour, layeroffset) { let i = contour.length; let shapeDontClose = shape.dontClose; //xzw add不闭合 while ( -- i >= 0 ) { if(shapeDontClose && i==0)break; //xzw add const j = i; let k = i - 1; if ( k < 0 ) k = contour.length - 1; //console.log('b', i,j, i-1, k,vertices.length); for ( let s = 0, sl = ( steps + bevelSegments * 2 ); s < sl; s ++ ) { const slen1 = vlen * s; const slen2 = vlen * ( s + 1 ); const a = layeroffset + j + slen1, b = layeroffset + k + slen1, c = layeroffset + k + slen2, d = layeroffset + j + slen2; f4( a, b, c, d ); } } } function v( x, y, z ) { placeholder.push( x ); placeholder.push( y ); placeholder.push( z ); } function f3( a, b, c ) { addVertex( a ); addVertex( b ); addVertex( c ); const nextIndex = verticesArray.length / 3; const uvs = uvgen.generateTopUV( scope, verticesArray, nextIndex - 3, nextIndex - 2, nextIndex - 1 ); addUV( uvs[ 0 ] ); addUV( uvs[ 1 ] ); addUV( uvs[ 2 ] ); } function f4( a, b, c, d ) { addVertex( a ); addVertex( b ); addVertex( d ); addVertex( b ); addVertex( c ); addVertex( d ); const nextIndex = verticesArray.length / 3; const uvs = uvgen.generateSideWallUV( scope, verticesArray, nextIndex - 6, nextIndex - 3, nextIndex - 2, nextIndex - 1 ); addUV( uvs[ 0 ] ); addUV( uvs[ 1 ] ); addUV( uvs[ 3 ] ); addUV( uvs[ 1 ] ); addUV( uvs[ 2 ] ); addUV( uvs[ 3 ] ); } function addVertex( index ) { verticesArray.push( placeholder[ index * 3 + 0 ] ); verticesArray.push( placeholder[ index * 3 + 1 ] ); verticesArray.push( placeholder[ index * 3 + 2 ] ); } function addUV( vector2 ) { uvArray.push( vector2.x ); uvArray.push( vector2.y ); } } } toJSON() { const data = BufferGeometry.prototype.toJSON.call( this ); const shapes = this.parameters.shapes; const options = this.parameters.options; return toJSON( shapes, options, data ); } } const WorldUVGenerator = { generateTopUV: function ( geometry, vertices, indexA, indexB, indexC ) { const a_x = vertices[ indexA * 3 ]; const a_y = vertices[ indexA * 3 + 1 ]; const b_x = vertices[ indexB * 3 ]; const b_y = vertices[ indexB * 3 + 1 ]; const c_x = vertices[ indexC * 3 ]; const c_y = vertices[ indexC * 3 + 1 ]; return [ new Vector2( a_x, a_y ), new Vector2( b_x, b_y ), new Vector2( c_x, c_y ) ]; }, generateSideWallUV: function ( geometry, vertices, indexA, indexB, indexC, indexD ) { const a_x = vertices[ indexA * 3 ]; const a_y = vertices[ indexA * 3 + 1 ]; const a_z = vertices[ indexA * 3 + 2 ]; const b_x = vertices[ indexB * 3 ]; const b_y = vertices[ indexB * 3 + 1 ]; const b_z = vertices[ indexB * 3 + 2 ]; const c_x = vertices[ indexC * 3 ]; const c_y = vertices[ indexC * 3 + 1 ]; const c_z = vertices[ indexC * 3 + 2 ]; const d_x = vertices[ indexD * 3 ]; const d_y = vertices[ indexD * 3 + 1 ]; const d_z = vertices[ indexD * 3 + 2 ]; if ( Math.abs( a_y - b_y ) < 0.01 ) { return [ new Vector2( a_x, 1 - a_z ), new Vector2( b_x, 1 - b_z ), new Vector2( c_x, 1 - c_z ), new Vector2( d_x, 1 - d_z ) ]; } else { return [ new Vector2( a_y, 1 - a_z ), new Vector2( b_y, 1 - b_z ), new Vector2( c_y, 1 - c_z ), new Vector2( d_y, 1 - d_z ) ]; } } }; function toJSON( shapes, options, data ) { data.shapes = []; if ( Array.isArray( shapes ) ) { for ( let i = 0, l = shapes.length; i < l; i ++ ) { const shape = shapes[ i ]; data.shapes.push( shape.uuid ); } } else { data.shapes.push( shapes.uuid ); } if ( options.extrudePath !== undefined ) data.options.extrudePath = options.extrudePath.toJSON(); return data; } /** * Creates extruded geometry from a path shape. * * parameters = { * * curveSegments: , // number of points on the curves * steps: , // number of points for z-side extrusions / used for subdividing segments of extrude spline too * depth: , // Depth to extrude the shape * * bevelEnabled: , // turn on bevel * bevelThickness: , // how deep into the original shape bevel goes * bevelSize: , // how far from shape outline (including bevelOffset) is bevel * bevelOffset: , // how far from shape outline does bevel start * bevelSegments: , // number of bevel layers * * extrudePath: // curve to extrude shape along * * UVGenerator: // object that provides UV generator functions * * } */ class ExtrudeGeometry extends Geometry { constructor( shapes, options ) { super(); this.type = 'ExtrudeGeometry'; this.parameters = { shapes: shapes, options: options }; this.fromBufferGeometry( new ExtrudeBufferGeometry( shapes, options ) ); this.mergeVertices(); } toJSON() { const data = super.toJSON(); const shapes = this.parameters.shapes; const options = this.parameters.options; return toJSON$1( shapes, options, data ); } } function toJSON$1( shapes, options, data ) { data.shapes = []; if ( Array.isArray( shapes ) ) { for ( let i = 0, l = shapes.length; i < l; i ++ ) { const shape = shapes[ i ]; data.shapes.push( shape.uuid ); } } else { data.shapes.push( shapes.uuid ); } if ( options.extrudePath !== undefined ) data.options.extrudePath = options.extrudePath.toJSON(); return data; } class IcosahedronBufferGeometry extends PolyhedronBufferGeometry { constructor( radius = 1, detail = 0 ) { const t = ( 1 + Math.sqrt( 5 ) ) / 2; const vertices = [ - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, 0, 0, - 1, t, 0, 1, t, 0, - 1, - t, 0, 1, - t, t, 0, - 1, t, 0, 1, - t, 0, - 1, - t, 0, 1 ]; const indices = [ 0, 11, 5, 0, 5, 1, 0, 1, 7, 0, 7, 10, 0, 10, 11, 1, 5, 9, 5, 11, 4, 11, 10, 2, 10, 7, 6, 7, 1, 8, 3, 9, 4, 3, 4, 2, 3, 2, 6, 3, 6, 8, 3, 8, 9, 4, 9, 5, 2, 4, 11, 6, 2, 10, 8, 6, 7, 9, 8, 1 ]; super( vertices, indices, radius, detail ); this.type = 'IcosahedronBufferGeometry'; this.parameters = { radius: radius, detail: detail }; } } class IcosahedronGeometry extends Geometry { constructor( radius, detail ) { super(); this.type = 'IcosahedronGeometry'; this.parameters = { radius: radius, detail: detail }; this.fromBufferGeometry( new IcosahedronBufferGeometry( radius, detail ) ); this.mergeVertices(); } } class LatheBufferGeometry extends BufferGeometry { constructor( points, segments = 12, phiStart = 0, phiLength = Math.PI * 2 ) { super(); this.type = 'LatheBufferGeometry'; this.parameters = { points: points, segments: segments, phiStart: phiStart, phiLength: phiLength }; segments = Math.floor( segments ); // clamp phiLength so it's in range of [ 0, 2PI ] phiLength = MathUtils.clamp( phiLength, 0, Math.PI * 2 ); // buffers const indices = []; const vertices = []; const uvs = []; // helper variables const inverseSegments = 1.0 / segments; const vertex = new Vector3(); const uv = new Vector2(); // generate vertices and uvs for ( let i = 0; i <= segments; i ++ ) { const phi = phiStart + i * inverseSegments * phiLength; const sin = Math.sin( phi ); const cos = Math.cos( phi ); for ( let j = 0; j <= ( points.length - 1 ); j ++ ) { // vertex vertex.x = points[ j ].x * sin; vertex.y = points[ j ].y; vertex.z = points[ j ].x * cos; vertices.push( vertex.x, vertex.y, vertex.z ); // uv uv.x = i / segments; uv.y = j / ( points.length - 1 ); uvs.push( uv.x, uv.y ); } } // indices for ( let i = 0; i < segments; i ++ ) { for ( let j = 0; j < ( points.length - 1 ); j ++ ) { const base = j + i * points.length; const a = base; const b = base + points.length; const c = base + points.length + 1; const d = base + 1; // faces indices.push( a, b, d ); indices.push( b, c, d ); } } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); // generate normals this.computeVertexNormals(); // if the geometry is closed, we need to average the normals along the seam. // because the corresponding vertices are identical (but still have different UVs). if ( phiLength === Math.PI * 2 ) { const normals = this.attributes.normal.array; const n1 = new Vector3(); const n2 = new Vector3(); const n = new Vector3(); // this is the buffer offset for the last line of vertices const base = segments * points.length * 3; for ( let i = 0, j = 0; i < points.length; i ++, j += 3 ) { // select the normal of the vertex in the first line n1.x = normals[ j + 0 ]; n1.y = normals[ j + 1 ]; n1.z = normals[ j + 2 ]; // select the normal of the vertex in the last line n2.x = normals[ base + j + 0 ]; n2.y = normals[ base + j + 1 ]; n2.z = normals[ base + j + 2 ]; // average normals n.addVectors( n1, n2 ).normalize(); // assign the new values to both normals normals[ j + 0 ] = normals[ base + j + 0 ] = n.x; normals[ j + 1 ] = normals[ base + j + 1 ] = n.y; normals[ j + 2 ] = normals[ base + j + 2 ] = n.z; } } } } class LatheGeometry extends Geometry { constructor( points, segments, phiStart, phiLength ) { super(); this.type = 'LatheGeometry'; this.parameters = { points: points, segments: segments, phiStart: phiStart, phiLength: phiLength }; this.fromBufferGeometry( new LatheBufferGeometry( points, segments, phiStart, phiLength ) ); this.mergeVertices(); } } class OctahedronBufferGeometry extends PolyhedronBufferGeometry { constructor( radius = 1, detail = 0 ) { const vertices = [ 1, 0, 0, - 1, 0, 0, 0, 1, 0, 0, - 1, 0, 0, 0, 1, 0, 0, - 1 ]; const indices = [ 0, 2, 4, 0, 4, 3, 0, 3, 5, 0, 5, 2, 1, 2, 5, 1, 5, 3, 1, 3, 4, 1, 4, 2 ]; super( vertices, indices, radius, detail ); this.type = 'OctahedronBufferGeometry'; this.parameters = { radius: radius, detail: detail }; } } class OctahedronGeometry extends Geometry { constructor( radius, detail ) { super(); this.type = 'OctahedronGeometry'; this.parameters = { radius: radius, detail: detail }; this.fromBufferGeometry( new OctahedronBufferGeometry( radius, detail ) ); this.mergeVertices(); } } /** * Parametric Surfaces Geometry * based on the brilliant article by @prideout https://prideout.net/blog/old/blog/index.html@p=44.html */ function ParametricBufferGeometry( func, slices, stacks ) { BufferGeometry.call( this ); this.type = 'ParametricBufferGeometry'; this.parameters = { func: func, slices: slices, stacks: stacks }; // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; const EPS = 0.00001; const normal = new Vector3(); const p0 = new Vector3(), p1 = new Vector3(); const pu = new Vector3(), pv = new Vector3(); if ( func.length < 3 ) { console.error( 'THREE.ParametricGeometry: Function must now modify a Vector3 as third parameter.' ); } // generate vertices, normals and uvs const sliceCount = slices + 1; for ( let i = 0; i <= stacks; i ++ ) { const v = i / stacks; for ( let j = 0; j <= slices; j ++ ) { const u = j / slices; // vertex func( u, v, p0 ); vertices.push( p0.x, p0.y, p0.z ); // normal // approximate tangent vectors via finite differences if ( u - EPS >= 0 ) { func( u - EPS, v, p1 ); pu.subVectors( p0, p1 ); } else { func( u + EPS, v, p1 ); pu.subVectors( p1, p0 ); } if ( v - EPS >= 0 ) { func( u, v - EPS, p1 ); pv.subVectors( p0, p1 ); } else { func( u, v + EPS, p1 ); pv.subVectors( p1, p0 ); } // cross product of tangent vectors returns surface normal normal.crossVectors( pu, pv ).normalize(); normals.push( normal.x, normal.y, normal.z ); // uv uvs.push( u, v ); } } // generate indices for ( let i = 0; i < stacks; i ++ ) { for ( let j = 0; j < slices; j ++ ) { const a = i * sliceCount + j; const b = i * sliceCount + j + 1; const c = ( i + 1 ) * sliceCount + j + 1; const d = ( i + 1 ) * sliceCount + j; // faces one and two indices.push( a, b, d ); indices.push( b, c, d ); } } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); } ParametricBufferGeometry.prototype = Object.create( BufferGeometry.prototype ); ParametricBufferGeometry.prototype.constructor = ParametricBufferGeometry; /** * Parametric Surfaces Geometry * based on the brilliant article by @prideout https://prideout.net/blog/old/blog/index.html@p=44.html */ function ParametricGeometry( func, slices, stacks ) { Geometry.call( this ); this.type = 'ParametricGeometry'; this.parameters = { func: func, slices: slices, stacks: stacks }; this.fromBufferGeometry( new ParametricBufferGeometry( func, slices, stacks ) ); this.mergeVertices(); } ParametricGeometry.prototype = Object.create( Geometry.prototype ); ParametricGeometry.prototype.constructor = ParametricGeometry; class PlaneGeometry extends Geometry { constructor( width, height, widthSegments, heightSegments ) { super(); this.type = 'PlaneGeometry'; this.parameters = { width: width, height: height, widthSegments: widthSegments, heightSegments: heightSegments }; this.fromBufferGeometry( new PlaneBufferGeometry( width, height, widthSegments, heightSegments ) ); this.mergeVertices(); } } class PolyhedronGeometry extends Geometry { constructor( vertices, indices, radius, detail ) { super(); this.type = 'PolyhedronGeometry'; this.parameters = { vertices: vertices, indices: indices, radius: radius, detail: detail }; this.fromBufferGeometry( new PolyhedronBufferGeometry( vertices, indices, radius, detail ) ); this.mergeVertices(); } } class RingBufferGeometry extends BufferGeometry { constructor( innerRadius = 0.5, outerRadius = 1, thetaSegments = 8, phiSegments = 1, thetaStart = 0, thetaLength = Math.PI * 2 ) { super(); this.type = 'RingBufferGeometry'; this.parameters = { innerRadius: innerRadius, outerRadius: outerRadius, thetaSegments: thetaSegments, phiSegments: phiSegments, thetaStart: thetaStart, thetaLength: thetaLength }; thetaSegments = Math.max( 3, thetaSegments ); phiSegments = Math.max( 1, phiSegments ); // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // some helper variables let radius = innerRadius; const radiusStep = ( ( outerRadius - innerRadius ) / phiSegments ); const vertex = new Vector3(); const uv = new Vector2(); // generate vertices, normals and uvs for ( let j = 0; j <= phiSegments; j ++ ) { for ( let i = 0; i <= thetaSegments; i ++ ) { // values are generate from the inside of the ring to the outside const segment = thetaStart + i / thetaSegments * thetaLength; // vertex vertex.x = radius * Math.cos( segment ); vertex.y = radius * Math.sin( segment ); vertices.push( vertex.x, vertex.y, vertex.z ); // normal normals.push( 0, 0, 1 ); // uv uv.x = ( vertex.x / outerRadius + 1 ) / 2; uv.y = ( vertex.y / outerRadius + 1 ) / 2; uvs.push( uv.x, uv.y ); } // increase the radius for next row of vertices radius += radiusStep; } // indices for ( let j = 0; j < phiSegments; j ++ ) { const thetaSegmentLevel = j * ( thetaSegments + 1 ); for ( let i = 0; i < thetaSegments; i ++ ) { const segment = i + thetaSegmentLevel; const a = segment; const b = segment + thetaSegments + 1; const c = segment + thetaSegments + 2; const d = segment + 1; // faces indices.push( a, b, d ); indices.push( b, c, d ); } } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); } } class RingGeometry extends Geometry { constructor( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) { super(); this.type = 'RingGeometry'; this.parameters = { innerRadius: innerRadius, outerRadius: outerRadius, thetaSegments: thetaSegments, phiSegments: phiSegments, thetaStart: thetaStart, thetaLength: thetaLength }; this.fromBufferGeometry( new RingBufferGeometry( innerRadius, outerRadius, thetaSegments, phiSegments, thetaStart, thetaLength ) ); this.mergeVertices(); } } class ShapeBufferGeometry extends BufferGeometry { constructor( shapes, curveSegments = 12 ) { super(); this.type = 'ShapeBufferGeometry'; this.parameters = { shapes: shapes, curveSegments: curveSegments }; // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // helper variables let groupStart = 0; let groupCount = 0; // allow single and array values for "shapes" parameter if ( Array.isArray( shapes ) === false ) { addShape( shapes ); } else { for ( let i = 0; i < shapes.length; i ++ ) { addShape( shapes[ i ] ); this.addGroup( groupStart, groupCount, i ); // enables MultiMaterial support groupStart += groupCount; groupCount = 0; } } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); // helper functions function addShape( shape ) { const indexOffset = vertices.length / 3; const points = shape.extractPoints( curveSegments ); let shapeVertices = points.shape; const shapeHoles = points.holes; // check direction of vertices if ( ShapeUtils.isClockWise( shapeVertices ) === false ) { shapeVertices = shapeVertices.reverse(); } for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) { const shapeHole = shapeHoles[ i ]; if ( ShapeUtils.isClockWise( shapeHole ) === true ) { shapeHoles[ i ] = shapeHole.reverse(); } } const faces = ShapeUtils.triangulateShape( shapeVertices, shapeHoles ); // join vertices of inner and outer paths to a single array for ( let i = 0, l = shapeHoles.length; i < l; i ++ ) { const shapeHole = shapeHoles[ i ]; shapeVertices = shapeVertices.concat( shapeHole ); } // vertices, normals, uvs for ( let i = 0, l = shapeVertices.length; i < l; i ++ ) { const vertex = shapeVertices[ i ]; vertices.push( vertex.x, vertex.y, 0 ); normals.push( 0, 0, 1 ); uvs.push( vertex.x, vertex.y ); // world uvs } // incides for ( let i = 0, l = faces.length; i < l; i ++ ) { const face = faces[ i ]; const a = face[ 0 ] + indexOffset; const b = face[ 1 ] + indexOffset; const c = face[ 2 ] + indexOffset; indices.push( a, b, c ); groupCount += 3; } } } toJSON() { const data = BufferGeometry.prototype.toJSON.call( this ); const shapes = this.parameters.shapes; return toJSON$2( shapes, data ); } } function toJSON$2( shapes, data ) { data.shapes = []; if ( Array.isArray( shapes ) ) { for ( let i = 0, l = shapes.length; i < l; i ++ ) { const shape = shapes[ i ]; data.shapes.push( shape.uuid ); } } else { data.shapes.push( shapes.uuid ); } return data; } class ShapeGeometry extends Geometry { constructor( shapes, curveSegments ) { super(); this.type = 'ShapeGeometry'; if ( typeof curveSegments === 'object' ) { console.warn( 'THREE.ShapeGeometry: Options parameter has been removed.' ); curveSegments = curveSegments.curveSegments; } this.parameters = { shapes: shapes, curveSegments: curveSegments }; this.fromBufferGeometry( new ShapeBufferGeometry( shapes, curveSegments ) ); this.mergeVertices(); } toJSON() { const data = Geometry.prototype.toJSON.call( this ); const shapes = this.parameters.shapes; return toJSON$3( shapes, data ); } } function toJSON$3( shapes, data ) { data.shapes = []; if ( Array.isArray( shapes ) ) { for ( let i = 0, l = shapes.length; i < l; i ++ ) { const shape = shapes[ i ]; data.shapes.push( shape.uuid ); } } else { data.shapes.push( shapes.uuid ); } return data; } class SphereBufferGeometry extends BufferGeometry { constructor( radius = 1, widthSegments = 8, heightSegments = 6, phiStart = 0, phiLength = Math.PI * 2, thetaStart = 0, thetaLength = Math.PI ) { super(); this.type = 'SphereBufferGeometry'; this.parameters = { radius: radius, widthSegments: widthSegments, heightSegments: heightSegments, phiStart: phiStart, phiLength: phiLength, thetaStart: thetaStart, thetaLength: thetaLength }; widthSegments = Math.max( 3, Math.floor( widthSegments ) ); heightSegments = Math.max( 2, Math.floor( heightSegments ) ); const thetaEnd = Math.min( thetaStart + thetaLength, Math.PI ); let index = 0; const grid = []; const vertex = new Vector3(); const normal = new Vector3(); // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // generate vertices, normals and uvs for ( let iy = 0; iy <= heightSegments; iy ++ ) { const verticesRow = []; const v = iy / heightSegments; // special case for the poles let uOffset = 0; if ( iy == 0 && thetaStart == 0 ) { uOffset = 0.5 / widthSegments; } else if ( iy == heightSegments && thetaEnd == Math.PI ) { uOffset = - 0.5 / widthSegments; } for ( let ix = 0; ix <= widthSegments; ix ++ ) { const u = ix / widthSegments; // vertex vertex.x = - radius * Math.cos( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ); vertex.y = radius * Math.cos( thetaStart + v * thetaLength ); vertex.z = radius * Math.sin( phiStart + u * phiLength ) * Math.sin( thetaStart + v * thetaLength ); vertices.push( vertex.x, vertex.y, vertex.z ); // normal normal.copy( vertex ).normalize(); normals.push( normal.x, normal.y, normal.z ); // uv uvs.push( u + uOffset, 1 - v ); verticesRow.push( index ++ ); } grid.push( verticesRow ); } // indices for ( let iy = 0; iy < heightSegments; iy ++ ) { for ( let ix = 0; ix < widthSegments; ix ++ ) { const a = grid[ iy ][ ix + 1 ]; const b = grid[ iy ][ ix ]; const c = grid[ iy + 1 ][ ix ]; const d = grid[ iy + 1 ][ ix + 1 ]; if ( iy !== 0 || thetaStart > 0 ) indices.push( a, b, d ); if ( iy !== heightSegments - 1 || thetaEnd < Math.PI ) indices.push( b, c, d ); } } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); } } class SphereGeometry extends Geometry { constructor( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) { super(); this.type = 'SphereGeometry'; this.parameters = { radius: radius, widthSegments: widthSegments, heightSegments: heightSegments, phiStart: phiStart, phiLength: phiLength, thetaStart: thetaStart, thetaLength: thetaLength }; this.fromBufferGeometry( new SphereBufferGeometry( radius, widthSegments, heightSegments, phiStart, phiLength, thetaStart, thetaLength ) ); this.mergeVertices(); } } class TetrahedronBufferGeometry extends PolyhedronBufferGeometry { constructor( radius = 1, detail = 0 ) { const vertices = [ 1, 1, 1, - 1, - 1, 1, - 1, 1, - 1, 1, - 1, - 1 ]; const indices = [ 2, 1, 0, 0, 3, 2, 1, 3, 0, 2, 3, 1 ]; super( vertices, indices, radius, detail ); this.type = 'TetrahedronBufferGeometry'; this.parameters = { radius: radius, detail: detail }; } } class TetrahedronGeometry extends Geometry { constructor( radius, detail ) { super(); this.type = 'TetrahedronGeometry'; this.parameters = { radius: radius, detail: detail }; this.fromBufferGeometry( new TetrahedronBufferGeometry( radius, detail ) ); this.mergeVertices(); } } /** * Text = 3D Text * * parameters = { * font: , // font * * size: , // size of the text * height: , // thickness to extrude text * curveSegments: , // number of points on the curves * * bevelEnabled: , // turn on bevel * bevelThickness: , // how deep into text bevel goes * bevelSize: , // how far from text outline (including bevelOffset) is bevel * bevelOffset: // how far from text outline does bevel start * } */ class TextBufferGeometry extends ExtrudeBufferGeometry { constructor( text, parameters = {} ) { const font = parameters.font; if ( ! ( font && font.isFont ) ) { console.error( 'THREE.TextGeometry: font parameter is not an instance of THREE.Font.' ); return new BufferGeometry(); } const shapes = font.generateShapes( text, parameters.size ); // translate parameters to ExtrudeGeometry API parameters.depth = parameters.height !== undefined ? parameters.height : 50; // defaults if ( parameters.bevelThickness === undefined ) parameters.bevelThickness = 10; if ( parameters.bevelSize === undefined ) parameters.bevelSize = 8; if ( parameters.bevelEnabled === undefined ) parameters.bevelEnabled = false; super( shapes, parameters ); this.type = 'TextBufferGeometry'; } } /** * Text = 3D Text * * parameters = { * font: , // font * * size: , // size of the text * height: , // thickness to extrude text * curveSegments: , // number of points on the curves * * bevelEnabled: , // turn on bevel * bevelThickness: , // how deep into text bevel goes * bevelSize: , // how far from text outline (including bevelOffset) is bevel * bevelOffset: // how far from text outline does bevel start * } */ class TextGeometry extends Geometry { constructor( text, parameters ) { super(); this.type = 'TextGeometry'; this.parameters = { text: text, parameters: parameters }; this.fromBufferGeometry( new TextBufferGeometry( text, parameters ) ); this.mergeVertices(); } } class TorusBufferGeometry extends BufferGeometry { constructor( radius = 1, tube = 0.4, radialSegments = 8, tubularSegments = 6, arc = Math.PI * 2 ) { super(); this.type = 'TorusBufferGeometry'; this.parameters = { radius: radius, tube: tube, radialSegments: radialSegments, tubularSegments: tubularSegments, arc: arc }; radialSegments = Math.floor( radialSegments ); tubularSegments = Math.floor( tubularSegments ); // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // helper variables const center = new Vector3(); const vertex = new Vector3(); const normal = new Vector3(); // generate vertices, normals and uvs for ( let j = 0; j <= radialSegments; j ++ ) { for ( let i = 0; i <= tubularSegments; i ++ ) { const u = i / tubularSegments * arc; const v = j / radialSegments * Math.PI * 2; // vertex vertex.x = ( radius + tube * Math.cos( v ) ) * Math.cos( u ); vertex.y = ( radius + tube * Math.cos( v ) ) * Math.sin( u ); vertex.z = tube * Math.sin( v ); vertices.push( vertex.x, vertex.y, vertex.z ); // normal center.x = radius * Math.cos( u ); center.y = radius * Math.sin( u ); normal.subVectors( vertex, center ).normalize(); normals.push( normal.x, normal.y, normal.z ); // uv uvs.push( i / tubularSegments ); uvs.push( j / radialSegments ); } } // generate indices for ( let j = 1; j <= radialSegments; j ++ ) { for ( let i = 1; i <= tubularSegments; i ++ ) { // indices const a = ( tubularSegments + 1 ) * j + i - 1; const b = ( tubularSegments + 1 ) * ( j - 1 ) + i - 1; const c = ( tubularSegments + 1 ) * ( j - 1 ) + i; const d = ( tubularSegments + 1 ) * j + i; // faces indices.push( a, b, d ); indices.push( b, c, d ); } } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); } } class TorusGeometry extends Geometry { constructor( radius, tube, radialSegments, tubularSegments, arc ) { super(); this.type = 'TorusGeometry'; this.parameters = { radius: radius, tube: tube, radialSegments: radialSegments, tubularSegments: tubularSegments, arc: arc }; this.fromBufferGeometry( new TorusBufferGeometry( radius, tube, radialSegments, tubularSegments, arc ) ); this.mergeVertices(); } } class TorusKnotBufferGeometry extends BufferGeometry { constructor( radius = 1, tube = 0.4, tubularSegments = 64, radialSegments = 8, p = 2, q = 3 ) { super(); this.type = 'TorusKnotBufferGeometry'; this.parameters = { radius: radius, tube: tube, tubularSegments: tubularSegments, radialSegments: radialSegments, p: p, q: q }; tubularSegments = Math.floor( tubularSegments ); radialSegments = Math.floor( radialSegments ); // buffers const indices = []; const vertices = []; const normals = []; const uvs = []; // helper variables const vertex = new Vector3(); const normal = new Vector3(); const P1 = new Vector3(); const P2 = new Vector3(); const B = new Vector3(); const T = new Vector3(); const N = new Vector3(); // generate vertices, normals and uvs for ( let i = 0; i <= tubularSegments; ++ i ) { // the radian "u" is used to calculate the position on the torus curve of the current tubular segement const u = i / tubularSegments * p * Math.PI * 2; // now we calculate two points. P1 is our current position on the curve, P2 is a little farther ahead. // these points are used to create a special "coordinate space", which is necessary to calculate the correct vertex positions calculatePositionOnCurve( u, p, q, radius, P1 ); calculatePositionOnCurve( u + 0.01, p, q, radius, P2 ); // calculate orthonormal basis T.subVectors( P2, P1 ); N.addVectors( P2, P1 ); B.crossVectors( T, N ); N.crossVectors( B, T ); // normalize B, N. T can be ignored, we don't use it B.normalize(); N.normalize(); for ( let j = 0; j <= radialSegments; ++ j ) { // now calculate the vertices. they are nothing more than an extrusion of the torus curve. // because we extrude a shape in the xy-plane, there is no need to calculate a z-value. const v = j / radialSegments * Math.PI * 2; const cx = - tube * Math.cos( v ); const cy = tube * Math.sin( v ); // now calculate the final vertex position. // first we orient the extrusion with our basis vectos, then we add it to the current position on the curve vertex.x = P1.x + ( cx * N.x + cy * B.x ); vertex.y = P1.y + ( cx * N.y + cy * B.y ); vertex.z = P1.z + ( cx * N.z + cy * B.z ); vertices.push( vertex.x, vertex.y, vertex.z ); // normal (P1 is always the center/origin of the extrusion, thus we can use it to calculate the normal) normal.subVectors( vertex, P1 ).normalize(); normals.push( normal.x, normal.y, normal.z ); // uv uvs.push( i / tubularSegments ); uvs.push( j / radialSegments ); } } // generate indices for ( let j = 1; j <= tubularSegments; j ++ ) { for ( let i = 1; i <= radialSegments; i ++ ) { // indices const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 ); const b = ( radialSegments + 1 ) * j + ( i - 1 ); const c = ( radialSegments + 1 ) * j + i; const d = ( radialSegments + 1 ) * ( j - 1 ) + i; // faces indices.push( a, b, d ); indices.push( b, c, d ); } } // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); // this function calculates the current position on the torus curve function calculatePositionOnCurve( u, p, q, radius, position ) { const cu = Math.cos( u ); const su = Math.sin( u ); const quOverP = q / p * u; const cs = Math.cos( quOverP ); position.x = radius * ( 2 + cs ) * 0.5 * cu; position.y = radius * ( 2 + cs ) * su * 0.5; position.z = radius * Math.sin( quOverP ) * 0.5; } } } class TorusKnotGeometry extends Geometry { constructor( radius, tube, tubularSegments, radialSegments, p, q, heightScale ) { super(); this.type = 'TorusKnotGeometry'; this.parameters = { radius: radius, tube: tube, tubularSegments: tubularSegments, radialSegments: radialSegments, p: p, q: q }; if ( heightScale !== undefined ) console.warn( 'THREE.TorusKnotGeometry: heightScale has been deprecated. Use .scale( x, y, z ) instead.' ); this.fromBufferGeometry( new TorusKnotBufferGeometry( radius, tube, tubularSegments, radialSegments, p, q ) ); this.mergeVertices(); } } class TubeBufferGeometry extends BufferGeometry { constructor( path, tubularSegments = 64, radius = 1, radialSegments = 8, closed = false ) { super(); this.type = 'TubeBufferGeometry'; this.parameters = { path: path, tubularSegments: tubularSegments, radius: radius, radialSegments: radialSegments, closed: closed }; const frames = path.computeFrenetFrames( tubularSegments, closed ); // expose internals this.tangents = frames.tangents; this.normals = frames.normals; this.binormals = frames.binormals; // helper variables const vertex = new Vector3(); const normal = new Vector3(); const uv = new Vector2(); let P = new Vector3(); // buffer const vertices = []; const normals = []; const uvs = []; const indices = []; // create buffer data generateBufferData(); // build geometry this.setIndex( indices ); this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); this.setAttribute( 'normal', new Float32BufferAttribute( normals, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); // functions function generateBufferData() { for ( let i = 0; i < tubularSegments; i ++ ) { generateSegment( i ); } // if the geometry is not closed, generate the last row of vertices and normals // at the regular position on the given path // // if the geometry is closed, duplicate the first row of vertices and normals (uvs will differ) generateSegment( ( closed === false ) ? tubularSegments : 0 ); // uvs are generated in a separate function. // this makes it easy compute correct values for closed geometries generateUVs(); // finally create faces generateIndices(); } function generateSegment( i ) { // we use getPointAt to sample evenly distributed points from the given path P = path.getPointAt( i / tubularSegments, P ); // retrieve corresponding normal and binormal const N = frames.normals[ i ]; const B = frames.binormals[ i ]; // generate normals and vertices for the current segment for ( let j = 0; j <= radialSegments; j ++ ) { const v = j / radialSegments * Math.PI * 2; const sin = Math.sin( v ); const cos = - Math.cos( v ); // normal normal.x = ( cos * N.x + sin * B.x ); normal.y = ( cos * N.y + sin * B.y ); normal.z = ( cos * N.z + sin * B.z ); normal.normalize(); normals.push( normal.x, normal.y, normal.z ); // vertex vertex.x = P.x + radius * normal.x; vertex.y = P.y + radius * normal.y; vertex.z = P.z + radius * normal.z; vertices.push( vertex.x, vertex.y, vertex.z ); } } function generateIndices() { for ( let j = 1; j <= tubularSegments; j ++ ) { for ( let i = 1; i <= radialSegments; i ++ ) { const a = ( radialSegments + 1 ) * ( j - 1 ) + ( i - 1 ); const b = ( radialSegments + 1 ) * j + ( i - 1 ); const c = ( radialSegments + 1 ) * j + i; const d = ( radialSegments + 1 ) * ( j - 1 ) + i; // faces indices.push( a, b, d ); indices.push( b, c, d ); } } } function generateUVs() { for ( let i = 0; i <= tubularSegments; i ++ ) { for ( let j = 0; j <= radialSegments; j ++ ) { uv.x = i / tubularSegments; uv.y = j / radialSegments; uvs.push( uv.x, uv.y ); } } } } toJSON() { const data = BufferGeometry.prototype.toJSON.call( this ); data.path = this.parameters.path.toJSON(); return data; } } class TubeGeometry extends Geometry { constructor( path, tubularSegments, radius, radialSegments, closed, taper ) { super(); this.type = 'TubeGeometry'; this.parameters = { path: path, tubularSegments: tubularSegments, radius: radius, radialSegments: radialSegments, closed: closed }; if ( taper !== undefined ) console.warn( 'THREE.TubeGeometry: taper has been removed.' ); const bufferGeometry = new TubeBufferGeometry( path, tubularSegments, radius, radialSegments, closed ); // expose internals this.tangents = bufferGeometry.tangents; this.normals = bufferGeometry.normals; this.binormals = bufferGeometry.binormals; // create geometry this.fromBufferGeometry( bufferGeometry ); this.mergeVertices(); } } class WireframeGeometry extends BufferGeometry { constructor( geometry ) { super(); this.type = 'WireframeGeometry'; // buffer const vertices = []; // helper variables const edge = [ 0, 0 ], edges = {}; const keys = [ 'a', 'b', 'c' ]; // different logic for Geometry and BufferGeometry if ( geometry && geometry.isGeometry ) { // create a data structure that contains all edges without duplicates const faces = geometry.faces; for ( let i = 0, l = faces.length; i < l; i ++ ) { const face = faces[ i ]; for ( let j = 0; j < 3; j ++ ) { const edge1 = face[ keys[ j ] ]; const edge2 = face[ keys[ ( j + 1 ) % 3 ] ]; edge[ 0 ] = Math.min( edge1, edge2 ); // sorting prevents duplicates edge[ 1 ] = Math.max( edge1, edge2 ); const key = edge[ 0 ] + ',' + edge[ 1 ]; if ( edges[ key ] === undefined ) { edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ] }; } } } // generate vertices for ( const key in edges ) { const e = edges[ key ]; let vertex = geometry.vertices[ e.index1 ]; vertices.push( vertex.x, vertex.y, vertex.z ); vertex = geometry.vertices[ e.index2 ]; vertices.push( vertex.x, vertex.y, vertex.z ); } } else if ( geometry && geometry.isBufferGeometry ) { const vertex = new Vector3(); if ( geometry.index !== null ) { // indexed BufferGeometry const position = geometry.attributes.position; const indices = geometry.index; let groups = geometry.groups; if ( groups.length === 0 ) { groups = [ { start: 0, count: indices.count, materialIndex: 0 } ]; } // create a data structure that contains all eges without duplicates for ( let o = 0, ol = groups.length; o < ol; ++ o ) { const group = groups[ o ]; const start = group.start; const count = group.count; for ( let i = start, l = ( start + count ); i < l; i += 3 ) { for ( let j = 0; j < 3; j ++ ) { const edge1 = indices.getX( i + j ); const edge2 = indices.getX( i + ( j + 1 ) % 3 ); edge[ 0 ] = Math.min( edge1, edge2 ); // sorting prevents duplicates edge[ 1 ] = Math.max( edge1, edge2 ); const key = edge[ 0 ] + ',' + edge[ 1 ]; if ( edges[ key ] === undefined ) { edges[ key ] = { index1: edge[ 0 ], index2: edge[ 1 ] }; } } } } // generate vertices for ( const key in edges ) { const e = edges[ key ]; vertex.fromBufferAttribute( position, e.index1 ); vertices.push( vertex.x, vertex.y, vertex.z ); vertex.fromBufferAttribute( position, e.index2 ); vertices.push( vertex.x, vertex.y, vertex.z ); } } else { // non-indexed BufferGeometry const position = geometry.attributes.position; for ( let i = 0, l = ( position.count / 3 ); i < l; i ++ ) { for ( let j = 0; j < 3; j ++ ) { // three edges per triangle, an edge is represented as (index1, index2) // e.g. the first triangle has the following edges: (0,1),(1,2),(2,0) const index1 = 3 * i + j; vertex.fromBufferAttribute( position, index1 ); vertices.push( vertex.x, vertex.y, vertex.z ); const index2 = 3 * i + ( ( j + 1 ) % 3 ); vertex.fromBufferAttribute( position, index2 ); vertices.push( vertex.x, vertex.y, vertex.z ); } } } } // build geometry this.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); } } var Geometries = /*#__PURE__*/Object.freeze({ __proto__: null, BoxGeometry: BoxGeometry, BoxBufferGeometry: BoxBufferGeometry, CircleGeometry: CircleGeometry, CircleBufferGeometry: CircleBufferGeometry, ConeGeometry: ConeGeometry, ConeBufferGeometry: ConeBufferGeometry, CylinderGeometry: CylinderGeometry, CylinderBufferGeometry: CylinderBufferGeometry, DodecahedronGeometry: DodecahedronGeometry, DodecahedronBufferGeometry: DodecahedronBufferGeometry, EdgesGeometry: EdgesGeometry, ExtrudeGeometry: ExtrudeGeometry, ExtrudeBufferGeometry: ExtrudeBufferGeometry, IcosahedronGeometry: IcosahedronGeometry, IcosahedronBufferGeometry: IcosahedronBufferGeometry, LatheGeometry: LatheGeometry, LatheBufferGeometry: LatheBufferGeometry, OctahedronGeometry: OctahedronGeometry, OctahedronBufferGeometry: OctahedronBufferGeometry, ParametricGeometry: ParametricGeometry, ParametricBufferGeometry: ParametricBufferGeometry, PlaneGeometry: PlaneGeometry, PlaneBufferGeometry: PlaneBufferGeometry, PolyhedronGeometry: PolyhedronGeometry, PolyhedronBufferGeometry: PolyhedronBufferGeometry, RingGeometry: RingGeometry, RingBufferGeometry: RingBufferGeometry, ShapeGeometry: ShapeGeometry, ShapeBufferGeometry: ShapeBufferGeometry, SphereGeometry: SphereGeometry, SphereBufferGeometry: SphereBufferGeometry, TetrahedronGeometry: TetrahedronGeometry, TetrahedronBufferGeometry: TetrahedronBufferGeometry, TextGeometry: TextGeometry, TextBufferGeometry: TextBufferGeometry, TorusGeometry: TorusGeometry, TorusBufferGeometry: TorusBufferGeometry, TorusKnotGeometry: TorusKnotGeometry, TorusKnotBufferGeometry: TorusKnotBufferGeometry, TubeGeometry: TubeGeometry, TubeBufferGeometry: TubeBufferGeometry, WireframeGeometry: WireframeGeometry }); /** * parameters = { * color: * } */ function ShadowMaterial( parameters ) { Material.call( this ); this.type = 'ShadowMaterial'; this.color = new Color( 0x000000 ); this.transparent = true; this.setValues( parameters ); } ShadowMaterial.prototype = Object.create( Material.prototype ); ShadowMaterial.prototype.constructor = ShadowMaterial; ShadowMaterial.prototype.isShadowMaterial = true; ShadowMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); return this; }; function RawShaderMaterial( parameters ) { ShaderMaterial.call( this, parameters ); this.type = 'RawShaderMaterial'; } RawShaderMaterial.prototype = Object.create( ShaderMaterial.prototype ); RawShaderMaterial.prototype.constructor = RawShaderMaterial; RawShaderMaterial.prototype.isRawShaderMaterial = true; /** * parameters = { * color: , * roughness: , * metalness: , * opacity: , * * map: new THREE.Texture( ), * * lightMap: new THREE.Texture( ), * lightMapIntensity: * * aoMap: new THREE.Texture( ), * aoMapIntensity: * * emissive: , * emissiveIntensity: * emissiveMap: new THREE.Texture( ), * * bumpMap: new THREE.Texture( ), * bumpScale: , * * normalMap: new THREE.Texture( ), * normalMapType: THREE.TangentSpaceNormalMap, * normalScale: , * * displacementMap: new THREE.Texture( ), * displacementScale: , * displacementBias: , * * roughnessMap: new THREE.Texture( ), * * metalnessMap: new THREE.Texture( ), * * alphaMap: new THREE.Texture( ), * * envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ), * envMapIntensity: * * refractionRatio: , * * wireframe: , * wireframeLinewidth: , * * skinning: , * morphTargets: , * morphNormals: * } */ function MeshStandardMaterial( parameters ) { Material.call( this ); this.defines = { 'STANDARD': '' }; this.type = 'MeshStandardMaterial'; this.color = new Color( 0xffffff ); // diffuse this.roughness = 1.0; this.metalness = 0.0; this.map = null; this.lightMap = null; this.lightMapIntensity = 1.0; this.aoMap = null; this.aoMapIntensity = 1.0; this.emissive = new Color( 0x000000 ); this.emissiveIntensity = 1.0; this.emissiveMap = null; this.bumpMap = null; this.bumpScale = 1; this.normalMap = null; this.normalMapType = TangentSpaceNormalMap; this.normalScale = new Vector2( 1, 1 ); this.displacementMap = null; this.displacementScale = 1; this.displacementBias = 0; this.roughnessMap = null; this.metalnessMap = null; this.alphaMap = null; this.envMap = null; this.envMapIntensity = 1.0; this.refractionRatio = 0.98; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.vertexTangents = false; this.setValues( parameters ); } MeshStandardMaterial.prototype = Object.create( Material.prototype ); MeshStandardMaterial.prototype.constructor = MeshStandardMaterial; MeshStandardMaterial.prototype.isMeshStandardMaterial = true; MeshStandardMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.defines = { 'STANDARD': '' }; this.color.copy( source.color ); this.roughness = source.roughness; this.metalness = source.metalness; this.map = source.map; this.lightMap = source.lightMap; this.lightMapIntensity = source.lightMapIntensity; this.aoMap = source.aoMap; this.aoMapIntensity = source.aoMapIntensity; this.emissive.copy( source.emissive ); this.emissiveMap = source.emissiveMap; this.emissiveIntensity = source.emissiveIntensity; this.bumpMap = source.bumpMap; this.bumpScale = source.bumpScale; this.normalMap = source.normalMap; this.normalMapType = source.normalMapType; this.normalScale.copy( source.normalScale ); this.displacementMap = source.displacementMap; this.displacementScale = source.displacementScale; this.displacementBias = source.displacementBias; this.roughnessMap = source.roughnessMap; this.metalnessMap = source.metalnessMap; this.alphaMap = source.alphaMap; this.envMap = source.envMap; this.envMapIntensity = source.envMapIntensity; this.refractionRatio = source.refractionRatio; this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; this.wireframeLinecap = source.wireframeLinecap; this.wireframeLinejoin = source.wireframeLinejoin; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.morphNormals = source.morphNormals; this.vertexTangents = source.vertexTangents; return this; }; /** * parameters = { * clearcoat: , * clearcoatMap: new THREE.Texture( ), * clearcoatRoughness: , * clearcoatRoughnessMap: new THREE.Texture( ), * clearcoatNormalScale: , * clearcoatNormalMap: new THREE.Texture( ), * * reflectivity: , * ior: , * * sheen: , * * transmission: , * transmissionMap: new THREE.Texture( ) * } */ function MeshPhysicalMaterial( parameters ) { MeshStandardMaterial.call( this ); this.defines = { 'STANDARD': '', 'PHYSICAL': '' }; this.type = 'MeshPhysicalMaterial'; this.clearcoat = 0.0; this.clearcoatMap = null; this.clearcoatRoughness = 0.0; this.clearcoatRoughnessMap = null; this.clearcoatNormalScale = new Vector2( 1, 1 ); this.clearcoatNormalMap = null; this.reflectivity = 0.5; // maps to F0 = 0.04 Object.defineProperty( this, 'ior', { get: function () { return ( 1 + 0.4 * this.reflectivity ) / ( 1 - 0.4 * this.reflectivity ); }, set: function ( ior ) { this.reflectivity = MathUtils.clamp( 2.5 * ( ior - 1 ) / ( ior + 1 ), 0, 1 ); } } ); this.sheen = null; // null will disable sheen bsdf this.transmission = 0.0; this.transmissionMap = null; this.setValues( parameters ); } MeshPhysicalMaterial.prototype = Object.create( MeshStandardMaterial.prototype ); MeshPhysicalMaterial.prototype.constructor = MeshPhysicalMaterial; MeshPhysicalMaterial.prototype.isMeshPhysicalMaterial = true; MeshPhysicalMaterial.prototype.copy = function ( source ) { MeshStandardMaterial.prototype.copy.call( this, source ); this.defines = { 'STANDARD': '', 'PHYSICAL': '' }; this.clearcoat = source.clearcoat; this.clearcoatMap = source.clearcoatMap; this.clearcoatRoughness = source.clearcoatRoughness; this.clearcoatRoughnessMap = source.clearcoatRoughnessMap; this.clearcoatNormalMap = source.clearcoatNormalMap; this.clearcoatNormalScale.copy( source.clearcoatNormalScale ); this.reflectivity = source.reflectivity; if ( source.sheen ) { this.sheen = ( this.sheen || new Color() ).copy( source.sheen ); } else { this.sheen = null; } this.transmission = source.transmission; this.transmissionMap = source.transmissionMap; return this; }; /** * parameters = { * color: , * specular: , * shininess: , * opacity: , * * map: new THREE.Texture( ), * * lightMap: new THREE.Texture( ), * lightMapIntensity: * * aoMap: new THREE.Texture( ), * aoMapIntensity: * * emissive: , * emissiveIntensity: * emissiveMap: new THREE.Texture( ), * * bumpMap: new THREE.Texture( ), * bumpScale: , * * normalMap: new THREE.Texture( ), * normalMapType: THREE.TangentSpaceNormalMap, * normalScale: , * * displacementMap: new THREE.Texture( ), * displacementScale: , * displacementBias: , * * specularMap: new THREE.Texture( ), * * alphaMap: new THREE.Texture( ), * * envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ), * combine: THREE.MultiplyOperation, * reflectivity: , * refractionRatio: , * * wireframe: , * wireframeLinewidth: , * * skinning: , * morphTargets: , * morphNormals: * } */ function MeshPhongMaterial( parameters ) { Material.call( this ); this.type = 'MeshPhongMaterial'; this.color = new Color( 0xffffff ); // diffuse this.specular = new Color( 0x111111 ); this.shininess = 30; this.map = null; this.lightMap = null; this.lightMapIntensity = 1.0; this.aoMap = null; this.aoMapIntensity = 1.0; this.emissive = new Color( 0x000000 ); this.emissiveIntensity = 1.0; this.emissiveMap = null; this.bumpMap = null; this.bumpScale = 1; this.normalMap = null; this.normalMapType = TangentSpaceNormalMap; this.normalScale = new Vector2( 1, 1 ); this.displacementMap = null; this.displacementScale = 1; this.displacementBias = 0; this.specularMap = null; this.alphaMap = null; this.envMap = null; this.combine = MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.setValues( parameters ); } MeshPhongMaterial.prototype = Object.create( Material.prototype ); MeshPhongMaterial.prototype.constructor = MeshPhongMaterial; MeshPhongMaterial.prototype.isMeshPhongMaterial = true; MeshPhongMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); this.specular.copy( source.specular ); this.shininess = source.shininess; this.map = source.map; this.lightMap = source.lightMap; this.lightMapIntensity = source.lightMapIntensity; this.aoMap = source.aoMap; this.aoMapIntensity = source.aoMapIntensity; this.emissive.copy( source.emissive ); this.emissiveMap = source.emissiveMap; this.emissiveIntensity = source.emissiveIntensity; this.bumpMap = source.bumpMap; this.bumpScale = source.bumpScale; this.normalMap = source.normalMap; this.normalMapType = source.normalMapType; this.normalScale.copy( source.normalScale ); this.displacementMap = source.displacementMap; this.displacementScale = source.displacementScale; this.displacementBias = source.displacementBias; this.specularMap = source.specularMap; this.alphaMap = source.alphaMap; this.envMap = source.envMap; this.combine = source.combine; this.reflectivity = source.reflectivity; this.refractionRatio = source.refractionRatio; this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; this.wireframeLinecap = source.wireframeLinecap; this.wireframeLinejoin = source.wireframeLinejoin; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.morphNormals = source.morphNormals; return this; }; /** * parameters = { * color: , * * map: new THREE.Texture( ), * gradientMap: new THREE.Texture( ), * * lightMap: new THREE.Texture( ), * lightMapIntensity: * * aoMap: new THREE.Texture( ), * aoMapIntensity: * * emissive: , * emissiveIntensity: * emissiveMap: new THREE.Texture( ), * * bumpMap: new THREE.Texture( ), * bumpScale: , * * normalMap: new THREE.Texture( ), * normalMapType: THREE.TangentSpaceNormalMap, * normalScale: , * * displacementMap: new THREE.Texture( ), * displacementScale: , * displacementBias: , * * alphaMap: new THREE.Texture( ), * * wireframe: , * wireframeLinewidth: , * * skinning: , * morphTargets: , * morphNormals: * } */ function MeshToonMaterial( parameters ) { Material.call( this ); this.defines = { 'TOON': '' }; this.type = 'MeshToonMaterial'; this.color = new Color( 0xffffff ); this.map = null; this.gradientMap = null; this.lightMap = null; this.lightMapIntensity = 1.0; this.aoMap = null; this.aoMapIntensity = 1.0; this.emissive = new Color( 0x000000 ); this.emissiveIntensity = 1.0; this.emissiveMap = null; this.bumpMap = null; this.bumpScale = 1; this.normalMap = null; this.normalMapType = TangentSpaceNormalMap; this.normalScale = new Vector2( 1, 1 ); this.displacementMap = null; this.displacementScale = 1; this.displacementBias = 0; this.alphaMap = null; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.setValues( parameters ); } MeshToonMaterial.prototype = Object.create( Material.prototype ); MeshToonMaterial.prototype.constructor = MeshToonMaterial; MeshToonMaterial.prototype.isMeshToonMaterial = true; MeshToonMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); this.map = source.map; this.gradientMap = source.gradientMap; this.lightMap = source.lightMap; this.lightMapIntensity = source.lightMapIntensity; this.aoMap = source.aoMap; this.aoMapIntensity = source.aoMapIntensity; this.emissive.copy( source.emissive ); this.emissiveMap = source.emissiveMap; this.emissiveIntensity = source.emissiveIntensity; this.bumpMap = source.bumpMap; this.bumpScale = source.bumpScale; this.normalMap = source.normalMap; this.normalMapType = source.normalMapType; this.normalScale.copy( source.normalScale ); this.displacementMap = source.displacementMap; this.displacementScale = source.displacementScale; this.displacementBias = source.displacementBias; this.alphaMap = source.alphaMap; this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; this.wireframeLinecap = source.wireframeLinecap; this.wireframeLinejoin = source.wireframeLinejoin; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.morphNormals = source.morphNormals; return this; }; /** * parameters = { * opacity: , * * bumpMap: new THREE.Texture( ), * bumpScale: , * * normalMap: new THREE.Texture( ), * normalMapType: THREE.TangentSpaceNormalMap, * normalScale: , * * displacementMap: new THREE.Texture( ), * displacementScale: , * displacementBias: , * * wireframe: , * wireframeLinewidth: * * skinning: , * morphTargets: , * morphNormals: * } */ function MeshNormalMaterial( parameters ) { Material.call( this ); this.type = 'MeshNormalMaterial'; this.bumpMap = null; this.bumpScale = 1; this.normalMap = null; this.normalMapType = TangentSpaceNormalMap; this.normalScale = new Vector2( 1, 1 ); this.displacementMap = null; this.displacementScale = 1; this.displacementBias = 0; this.wireframe = false; this.wireframeLinewidth = 1; this.fog = false; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.setValues( parameters ); } MeshNormalMaterial.prototype = Object.create( Material.prototype ); MeshNormalMaterial.prototype.constructor = MeshNormalMaterial; MeshNormalMaterial.prototype.isMeshNormalMaterial = true; MeshNormalMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.bumpMap = source.bumpMap; this.bumpScale = source.bumpScale; this.normalMap = source.normalMap; this.normalMapType = source.normalMapType; this.normalScale.copy( source.normalScale ); this.displacementMap = source.displacementMap; this.displacementScale = source.displacementScale; this.displacementBias = source.displacementBias; this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.morphNormals = source.morphNormals; return this; }; /** * parameters = { * color: , * opacity: , * * map: new THREE.Texture( ), * * lightMap: new THREE.Texture( ), * lightMapIntensity: * * aoMap: new THREE.Texture( ), * aoMapIntensity: * * emissive: , * emissiveIntensity: * emissiveMap: new THREE.Texture( ), * * specularMap: new THREE.Texture( ), * * alphaMap: new THREE.Texture( ), * * envMap: new THREE.CubeTexture( [posx, negx, posy, negy, posz, negz] ), * combine: THREE.Multiply, * reflectivity: , * refractionRatio: , * * wireframe: , * wireframeLinewidth: , * * skinning: , * morphTargets: , * morphNormals: * } */ function MeshLambertMaterial( parameters ) { Material.call( this ); this.type = 'MeshLambertMaterial'; this.color = new Color( 0xffffff ); // diffuse this.map = null; this.lightMap = null; this.lightMapIntensity = 1.0; this.aoMap = null; this.aoMapIntensity = 1.0; this.emissive = new Color( 0x000000 ); this.emissiveIntensity = 1.0; this.emissiveMap = null; this.specularMap = null; this.alphaMap = null; this.envMap = null; this.combine = MultiplyOperation; this.reflectivity = 1; this.refractionRatio = 0.98; this.wireframe = false; this.wireframeLinewidth = 1; this.wireframeLinecap = 'round'; this.wireframeLinejoin = 'round'; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.setValues( parameters ); } MeshLambertMaterial.prototype = Object.create( Material.prototype ); MeshLambertMaterial.prototype.constructor = MeshLambertMaterial; MeshLambertMaterial.prototype.isMeshLambertMaterial = true; MeshLambertMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.color.copy( source.color ); this.map = source.map; this.lightMap = source.lightMap; this.lightMapIntensity = source.lightMapIntensity; this.aoMap = source.aoMap; this.aoMapIntensity = source.aoMapIntensity; this.emissive.copy( source.emissive ); this.emissiveMap = source.emissiveMap; this.emissiveIntensity = source.emissiveIntensity; this.specularMap = source.specularMap; this.alphaMap = source.alphaMap; this.envMap = source.envMap; this.combine = source.combine; this.reflectivity = source.reflectivity; this.refractionRatio = source.refractionRatio; this.wireframe = source.wireframe; this.wireframeLinewidth = source.wireframeLinewidth; this.wireframeLinecap = source.wireframeLinecap; this.wireframeLinejoin = source.wireframeLinejoin; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.morphNormals = source.morphNormals; return this; }; /** * parameters = { * color: , * opacity: , * * matcap: new THREE.Texture( ), * * map: new THREE.Texture( ), * * bumpMap: new THREE.Texture( ), * bumpScale: , * * normalMap: new THREE.Texture( ), * normalMapType: THREE.TangentSpaceNormalMap, * normalScale: , * * displacementMap: new THREE.Texture( ), * displacementScale: , * displacementBias: , * * alphaMap: new THREE.Texture( ), * * skinning: , * morphTargets: , * morphNormals: * } */ function MeshMatcapMaterial( parameters ) { Material.call( this ); this.defines = { 'MATCAP': '' }; this.type = 'MeshMatcapMaterial'; this.color = new Color( 0xffffff ); // diffuse this.matcap = null; this.map = null; this.bumpMap = null; this.bumpScale = 1; this.normalMap = null; this.normalMapType = TangentSpaceNormalMap; this.normalScale = new Vector2( 1, 1 ); this.displacementMap = null; this.displacementScale = 1; this.displacementBias = 0; this.alphaMap = null; this.skinning = false; this.morphTargets = false; this.morphNormals = false; this.setValues( parameters ); } MeshMatcapMaterial.prototype = Object.create( Material.prototype ); MeshMatcapMaterial.prototype.constructor = MeshMatcapMaterial; MeshMatcapMaterial.prototype.isMeshMatcapMaterial = true; MeshMatcapMaterial.prototype.copy = function ( source ) { Material.prototype.copy.call( this, source ); this.defines = { 'MATCAP': '' }; this.color.copy( source.color ); this.matcap = source.matcap; this.map = source.map; this.bumpMap = source.bumpMap; this.bumpScale = source.bumpScale; this.normalMap = source.normalMap; this.normalMapType = source.normalMapType; this.normalScale.copy( source.normalScale ); this.displacementMap = source.displacementMap; this.displacementScale = source.displacementScale; this.displacementBias = source.displacementBias; this.alphaMap = source.alphaMap; this.skinning = source.skinning; this.morphTargets = source.morphTargets; this.morphNormals = source.morphNormals; return this; }; /** * parameters = { * color: , * opacity: , * * linewidth: , * * scale: , * dashSize: , * gapSize: * } */ function LineDashedMaterial( parameters ) { LineBasicMaterial.call( this ); this.type = 'LineDashedMaterial'; this.scale = 1; this.dashSize = 3; this.gapSize = 1; this.setValues( parameters ); } LineDashedMaterial.prototype = Object.create( LineBasicMaterial.prototype ); LineDashedMaterial.prototype.constructor = LineDashedMaterial; LineDashedMaterial.prototype.isLineDashedMaterial = true; LineDashedMaterial.prototype.copy = function ( source ) { LineBasicMaterial.prototype.copy.call( this, source ); this.scale = source.scale; this.dashSize = source.dashSize; this.gapSize = source.gapSize; return this; }; var Materials = /*#__PURE__*/Object.freeze({ __proto__: null, ShadowMaterial: ShadowMaterial, SpriteMaterial: SpriteMaterial, RawShaderMaterial: RawShaderMaterial, ShaderMaterial: ShaderMaterial, PointsMaterial: PointsMaterial, MeshPhysicalMaterial: MeshPhysicalMaterial, MeshStandardMaterial: MeshStandardMaterial, MeshPhongMaterial: MeshPhongMaterial, MeshToonMaterial: MeshToonMaterial, MeshNormalMaterial: MeshNormalMaterial, MeshLambertMaterial: MeshLambertMaterial, MeshDepthMaterial: MeshDepthMaterial, MeshDistanceMaterial: MeshDistanceMaterial, MeshBasicMaterial: MeshBasicMaterial, MeshMatcapMaterial: MeshMatcapMaterial, LineDashedMaterial: LineDashedMaterial, LineBasicMaterial: LineBasicMaterial, Material: Material }); const AnimationUtils = { // same as Array.prototype.slice, but also works on typed arrays arraySlice: function ( array, from, to ) { if ( AnimationUtils.isTypedArray( array ) ) { // in ios9 array.subarray(from, undefined) will return empty array // but array.subarray(from) or array.subarray(from, len) is correct return new array.constructor( array.subarray( from, to !== undefined ? to : array.length ) ); } return array.slice( from, to ); }, // converts an array to a specific type convertArray: function ( array, type, forceClone ) { if ( ! array || // let 'undefined' and 'null' pass ! forceClone && array.constructor === type ) return array; if ( typeof type.BYTES_PER_ELEMENT === 'number' ) { return new type( array ); // create typed array } return Array.prototype.slice.call( array ); // create Array }, isTypedArray: function ( object ) { return ArrayBuffer.isView( object ) && ! ( object instanceof DataView ); }, // returns an array by which times and values can be sorted getKeyframeOrder: function ( times ) { function compareTime( i, j ) { return times[ i ] - times[ j ]; } const n = times.length; const result = new Array( n ); for ( let i = 0; i !== n; ++ i ) result[ i ] = i; result.sort( compareTime ); return result; }, // uses the array previously returned by 'getKeyframeOrder' to sort data sortedArray: function ( values, stride, order ) { const nValues = values.length; const result = new values.constructor( nValues ); for ( let i = 0, dstOffset = 0; dstOffset !== nValues; ++ i ) { const srcOffset = order[ i ] * stride; for ( let j = 0; j !== stride; ++ j ) { result[ dstOffset ++ ] = values[ srcOffset + j ]; } } return result; }, // function for parsing AOS keyframe formats flattenJSON: function ( jsonKeys, times, values, valuePropertyName ) { let i = 1, key = jsonKeys[ 0 ]; while ( key !== undefined && key[ valuePropertyName ] === undefined ) { key = jsonKeys[ i ++ ]; } if ( key === undefined ) return; // no data let value = key[ valuePropertyName ]; if ( value === undefined ) return; // no data if ( Array.isArray( value ) ) { do { value = key[ valuePropertyName ]; if ( value !== undefined ) { times.push( key.time ); values.push.apply( values, value ); // push all elements } key = jsonKeys[ i ++ ]; } while ( key !== undefined ); } else if ( value.toArray !== undefined ) { // ...assume THREE.Math-ish do { value = key[ valuePropertyName ]; if ( value !== undefined ) { times.push( key.time ); value.toArray( values, values.length ); } key = jsonKeys[ i ++ ]; } while ( key !== undefined ); } else { // otherwise push as-is do { value = key[ valuePropertyName ]; if ( value !== undefined ) { times.push( key.time ); values.push( value ); } key = jsonKeys[ i ++ ]; } while ( key !== undefined ); } }, subclip: function ( sourceClip, name, startFrame, endFrame, fps = 30 ) { const clip = sourceClip.clone(); clip.name = name; const tracks = []; for ( let i = 0; i < clip.tracks.length; ++ i ) { const track = clip.tracks[ i ]; const valueSize = track.getValueSize(); const times = []; const values = []; for ( let j = 0; j < track.times.length; ++ j ) { const frame = track.times[ j ] * fps; if ( frame < startFrame || frame >= endFrame ) continue; times.push( track.times[ j ] ); for ( let k = 0; k < valueSize; ++ k ) { values.push( track.values[ j * valueSize + k ] ); } } if ( times.length === 0 ) continue; track.times = AnimationUtils.convertArray( times, track.times.constructor ); track.values = AnimationUtils.convertArray( values, track.values.constructor ); tracks.push( track ); } clip.tracks = tracks; // find minimum .times value across all tracks in the trimmed clip let minStartTime = Infinity; for ( let i = 0; i < clip.tracks.length; ++ i ) { if ( minStartTime > clip.tracks[ i ].times[ 0 ] ) { minStartTime = clip.tracks[ i ].times[ 0 ]; } } // shift all tracks such that clip begins at t=0 for ( let i = 0; i < clip.tracks.length; ++ i ) { clip.tracks[ i ].shift( - 1 * minStartTime ); } clip.resetDuration(); return clip; }, makeClipAdditive: function ( targetClip, referenceFrame = 0, referenceClip = targetClip, fps = 30 ) { if ( fps <= 0 ) fps = 30; const numTracks = referenceClip.tracks.length; const referenceTime = referenceFrame / fps; // Make each track's values relative to the values at the reference frame for ( let i = 0; i < numTracks; ++ i ) { const referenceTrack = referenceClip.tracks[ i ]; const referenceTrackType = referenceTrack.ValueTypeName; // Skip this track if it's non-numeric if ( referenceTrackType === 'bool' || referenceTrackType === 'string' ) continue; // Find the track in the target clip whose name and type matches the reference track const targetTrack = targetClip.tracks.find( function ( track ) { return track.name === referenceTrack.name && track.ValueTypeName === referenceTrackType; } ); if ( targetTrack === undefined ) continue; let referenceOffset = 0; const referenceValueSize = referenceTrack.getValueSize(); if ( referenceTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) { referenceOffset = referenceValueSize / 3; } let targetOffset = 0; const targetValueSize = targetTrack.getValueSize(); if ( targetTrack.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline ) { targetOffset = targetValueSize / 3; } const lastIndex = referenceTrack.times.length - 1; let referenceValue; // Find the value to subtract out of the track if ( referenceTime <= referenceTrack.times[ 0 ] ) { // Reference frame is earlier than the first keyframe, so just use the first keyframe const startIndex = referenceOffset; const endIndex = referenceValueSize - referenceOffset; referenceValue = AnimationUtils.arraySlice( referenceTrack.values, startIndex, endIndex ); } else if ( referenceTime >= referenceTrack.times[ lastIndex ] ) { // Reference frame is after the last keyframe, so just use the last keyframe const startIndex = lastIndex * referenceValueSize + referenceOffset; const endIndex = startIndex + referenceValueSize - referenceOffset; referenceValue = AnimationUtils.arraySlice( referenceTrack.values, startIndex, endIndex ); } else { // Interpolate to the reference value const interpolant = referenceTrack.createInterpolant(); const startIndex = referenceOffset; const endIndex = referenceValueSize - referenceOffset; interpolant.evaluate( referenceTime ); referenceValue = AnimationUtils.arraySlice( interpolant.resultBuffer, startIndex, endIndex ); } // Conjugate the quaternion if ( referenceTrackType === 'quaternion' ) { const referenceQuat = new Quaternion().fromArray( referenceValue ).normalize().conjugate(); referenceQuat.toArray( referenceValue ); } // Subtract the reference value from all of the track values const numTimes = targetTrack.times.length; for ( let j = 0; j < numTimes; ++ j ) { const valueStart = j * targetValueSize + targetOffset; if ( referenceTrackType === 'quaternion' ) { // Multiply the conjugate for quaternion track types Quaternion.multiplyQuaternionsFlat( targetTrack.values, valueStart, referenceValue, 0, targetTrack.values, valueStart ); } else { const valueEnd = targetValueSize - targetOffset * 2; // Subtract each value for all other numeric track types for ( let k = 0; k < valueEnd; ++ k ) { targetTrack.values[ valueStart + k ] -= referenceValue[ k ]; } } } } targetClip.blendMode = AdditiveAnimationBlendMode; return targetClip; } }; /** * Abstract base class of interpolants over parametric samples. * * The parameter domain is one dimensional, typically the time or a path * along a curve defined by the data. * * The sample values can have any dimensionality and derived classes may * apply special interpretations to the data. * * This class provides the interval seek in a Template Method, deferring * the actual interpolation to derived classes. * * Time complexity is O(1) for linear access crossing at most two points * and O(log N) for random access, where N is the number of positions. * * References: * * http://www.oodesign.com/template-method-pattern.html * */ function Interpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) { this.parameterPositions = parameterPositions; this._cachedIndex = 0; this.resultBuffer = resultBuffer !== undefined ? resultBuffer : new sampleValues.constructor( sampleSize ); this.sampleValues = sampleValues; this.valueSize = sampleSize; } Object.assign( Interpolant.prototype, { evaluate: function ( t ) { const pp = this.parameterPositions; let i1 = this._cachedIndex, t1 = pp[ i1 ], t0 = pp[ i1 - 1 ]; validate_interval: { seek: { let right; linear_scan: { //- See http://jsperf.com/comparison-to-undefined/3 //- slower code: //- //- if ( t >= t1 || t1 === undefined ) { forward_scan: if ( ! ( t < t1 ) ) { for ( let giveUpAt = i1 + 2; ; ) { if ( t1 === undefined ) { if ( t < t0 ) break forward_scan; // after end i1 = pp.length; this._cachedIndex = i1; return this.afterEnd_( i1 - 1, t, t0 ); } if ( i1 === giveUpAt ) break; // this loop t0 = t1; t1 = pp[ ++ i1 ]; if ( t < t1 ) { // we have arrived at the sought interval break seek; } } // prepare binary search on the right side of the index right = pp.length; break linear_scan; } //- slower code: //- if ( t < t0 || t0 === undefined ) { if ( ! ( t >= t0 ) ) { // looping? const t1global = pp[ 1 ]; if ( t < t1global ) { i1 = 2; // + 1, using the scan for the details t0 = t1global; } // linear reverse scan for ( let giveUpAt = i1 - 2; ; ) { if ( t0 === undefined ) { // before start this._cachedIndex = 0; return this.beforeStart_( 0, t, t1 ); } if ( i1 === giveUpAt ) break; // this loop t1 = t0; t0 = pp[ -- i1 - 1 ]; if ( t >= t0 ) { // we have arrived at the sought interval break seek; } } // prepare binary search on the left side of the index right = i1; i1 = 0; break linear_scan; } // the interval is valid break validate_interval; } // linear scan // binary search while ( i1 < right ) { const mid = ( i1 + right ) >>> 1; if ( t < pp[ mid ] ) { right = mid; } else { i1 = mid + 1; } } t1 = pp[ i1 ]; t0 = pp[ i1 - 1 ]; // check boundary cases, again if ( t0 === undefined ) { this._cachedIndex = 0; return this.beforeStart_( 0, t, t1 ); } if ( t1 === undefined ) { i1 = pp.length; this._cachedIndex = i1; return this.afterEnd_( i1 - 1, t0, t ); } } // seek this._cachedIndex = i1; this.intervalChanged_( i1, t0, t1 ); } // validate_interval return this.interpolate_( i1, t0, t, t1 ); }, settings: null, // optional, subclass-specific settings structure // Note: The indirection allows central control of many interpolants. // --- Protected interface DefaultSettings_: {}, getSettings_: function () { return this.settings || this.DefaultSettings_; }, copySampleValue_: function ( index ) { // copies a sample value to the result buffer const result = this.resultBuffer, values = this.sampleValues, stride = this.valueSize, offset = index * stride; for ( let i = 0; i !== stride; ++ i ) { result[ i ] = values[ offset + i ]; } return result; }, // Template methods for derived classes: interpolate_: function ( /* i1, t0, t, t1 */ ) { throw new Error( 'call to abstract method' ); // implementations shall return this.resultBuffer }, intervalChanged_: function ( /* i1, t0, t1 */ ) { // empty } } ); // DECLARE ALIAS AFTER assign prototype Object.assign( Interpolant.prototype, { //( 0, t, t0 ), returns this.resultBuffer beforeStart_: Interpolant.prototype.copySampleValue_, //( N-1, tN-1, t ), returns this.resultBuffer afterEnd_: Interpolant.prototype.copySampleValue_, } ); /** * Fast and simple cubic spline interpolant. * * It was derived from a Hermitian construction setting the first derivative * at each sample position to the linear slope between neighboring positions * over their parameter interval. */ function CubicInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) { Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer ); this._weightPrev = - 0; this._offsetPrev = - 0; this._weightNext = - 0; this._offsetNext = - 0; } CubicInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), { constructor: CubicInterpolant, DefaultSettings_: { endingStart: ZeroCurvatureEnding, endingEnd: ZeroCurvatureEnding }, intervalChanged_: function ( i1, t0, t1 ) { const pp = this.parameterPositions; let iPrev = i1 - 2, iNext = i1 + 1, tPrev = pp[ iPrev ], tNext = pp[ iNext ]; if ( tPrev === undefined ) { switch ( this.getSettings_().endingStart ) { case ZeroSlopeEnding: // f'(t0) = 0 iPrev = i1; tPrev = 2 * t0 - t1; break; case WrapAroundEnding: // use the other end of the curve iPrev = pp.length - 2; tPrev = t0 + pp[ iPrev ] - pp[ iPrev + 1 ]; break; default: // ZeroCurvatureEnding // f''(t0) = 0 a.k.a. Natural Spline iPrev = i1; tPrev = t1; } } if ( tNext === undefined ) { switch ( this.getSettings_().endingEnd ) { case ZeroSlopeEnding: // f'(tN) = 0 iNext = i1; tNext = 2 * t1 - t0; break; case WrapAroundEnding: // use the other end of the curve iNext = 1; tNext = t1 + pp[ 1 ] - pp[ 0 ]; break; default: // ZeroCurvatureEnding // f''(tN) = 0, a.k.a. Natural Spline iNext = i1 - 1; tNext = t0; } } const halfDt = ( t1 - t0 ) * 0.5, stride = this.valueSize; this._weightPrev = halfDt / ( t0 - tPrev ); this._weightNext = halfDt / ( tNext - t1 ); this._offsetPrev = iPrev * stride; this._offsetNext = iNext * stride; }, interpolate_: function ( i1, t0, t, t1 ) { const result = this.resultBuffer, values = this.sampleValues, stride = this.valueSize, o1 = i1 * stride, o0 = o1 - stride, oP = this._offsetPrev, oN = this._offsetNext, wP = this._weightPrev, wN = this._weightNext, p = ( t - t0 ) / ( t1 - t0 ), pp = p * p, ppp = pp * p; // evaluate polynomials const sP = - wP * ppp + 2 * wP * pp - wP * p; const s0 = ( 1 + wP ) * ppp + ( - 1.5 - 2 * wP ) * pp + ( - 0.5 + wP ) * p + 1; const s1 = ( - 1 - wN ) * ppp + ( 1.5 + wN ) * pp + 0.5 * p; const sN = wN * ppp - wN * pp; // combine data linearly for ( let i = 0; i !== stride; ++ i ) { result[ i ] = sP * values[ oP + i ] + s0 * values[ o0 + i ] + s1 * values[ o1 + i ] + sN * values[ oN + i ]; } return result; } } ); function LinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) { Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer ); } LinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), { constructor: LinearInterpolant, interpolate_: function ( i1, t0, t, t1 ) { const result = this.resultBuffer, values = this.sampleValues, stride = this.valueSize, offset1 = i1 * stride, offset0 = offset1 - stride, weight1 = ( t - t0 ) / ( t1 - t0 ), weight0 = 1 - weight1; for ( let i = 0; i !== stride; ++ i ) { result[ i ] = values[ offset0 + i ] * weight0 + values[ offset1 + i ] * weight1; } return result; } } ); /** * * Interpolant that evaluates to the sample value at the position preceeding * the parameter. */ function DiscreteInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) { Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer ); } DiscreteInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), { constructor: DiscreteInterpolant, interpolate_: function ( i1 /*, t0, t, t1 */ ) { return this.copySampleValue_( i1 - 1 ); } } ); function KeyframeTrack( name, times, values, interpolation ) { if ( name === undefined ) throw new Error( 'THREE.KeyframeTrack: track name is undefined' ); if ( times === undefined || times.length === 0 ) throw new Error( 'THREE.KeyframeTrack: no keyframes in track named ' + name ); this.name = name; this.times = AnimationUtils.convertArray( times, this.TimeBufferType ); this.values = AnimationUtils.convertArray( values, this.ValueBufferType ); this.setInterpolation( interpolation || this.DefaultInterpolation ); } // Static methods Object.assign( KeyframeTrack, { // Serialization (in static context, because of constructor invocation // and automatic invocation of .toJSON): toJSON: function ( track ) { const trackType = track.constructor; let json; // derived classes can define a static toJSON method if ( trackType.toJSON !== undefined ) { json = trackType.toJSON( track ); } else { // by default, we assume the data can be serialized as-is json = { 'name': track.name, 'times': AnimationUtils.convertArray( track.times, Array ), 'values': AnimationUtils.convertArray( track.values, Array ) }; const interpolation = track.getInterpolation(); if ( interpolation !== track.DefaultInterpolation ) { json.interpolation = interpolation; } } json.type = track.ValueTypeName; // mandatory return json; } } ); Object.assign( KeyframeTrack.prototype, { constructor: KeyframeTrack, TimeBufferType: Float32Array, ValueBufferType: Float32Array, DefaultInterpolation: InterpolateLinear, InterpolantFactoryMethodDiscrete: function ( result ) { return new DiscreteInterpolant( this.times, this.values, this.getValueSize(), result ); }, InterpolantFactoryMethodLinear: function ( result ) { return new LinearInterpolant( this.times, this.values, this.getValueSize(), result ); }, InterpolantFactoryMethodSmooth: function ( result ) { return new CubicInterpolant( this.times, this.values, this.getValueSize(), result ); }, setInterpolation: function ( interpolation ) { let factoryMethod; switch ( interpolation ) { case InterpolateDiscrete: factoryMethod = this.InterpolantFactoryMethodDiscrete; break; case InterpolateLinear: factoryMethod = this.InterpolantFactoryMethodLinear; break; case InterpolateSmooth: factoryMethod = this.InterpolantFactoryMethodSmooth; break; } if ( factoryMethod === undefined ) { const message = 'unsupported interpolation for ' + this.ValueTypeName + ' keyframe track named ' + this.name; if ( this.createInterpolant === undefined ) { // fall back to default, unless the default itself is messed up if ( interpolation !== this.DefaultInterpolation ) { this.setInterpolation( this.DefaultInterpolation ); } else { throw new Error( message ); // fatal, in this case } } console.warn( 'THREE.KeyframeTrack:', message ); return this; } this.createInterpolant = factoryMethod; return this; }, getInterpolation: function () { switch ( this.createInterpolant ) { case this.InterpolantFactoryMethodDiscrete: return InterpolateDiscrete; case this.InterpolantFactoryMethodLinear: return InterpolateLinear; case this.InterpolantFactoryMethodSmooth: return InterpolateSmooth; } }, getValueSize: function () { return this.values.length / this.times.length; }, // move all keyframes either forwards or backwards in time shift: function ( timeOffset ) { if ( timeOffset !== 0.0 ) { const times = this.times; for ( let i = 0, n = times.length; i !== n; ++ i ) { times[ i ] += timeOffset; } } return this; }, // scale all keyframe times by a factor (useful for frame <-> seconds conversions) scale: function ( timeScale ) { if ( timeScale !== 1.0 ) { const times = this.times; for ( let i = 0, n = times.length; i !== n; ++ i ) { times[ i ] *= timeScale; } } return this; }, // removes keyframes before and after animation without changing any values within the range [startTime, endTime]. // IMPORTANT: We do not shift around keys to the start of the track time, because for interpolated keys this will change their values trim: function ( startTime, endTime ) { const times = this.times, nKeys = times.length; let from = 0, to = nKeys - 1; while ( from !== nKeys && times[ from ] < startTime ) { ++ from; } while ( to !== - 1 && times[ to ] > endTime ) { -- to; } ++ to; // inclusive -> exclusive bound if ( from !== 0 || to !== nKeys ) { // empty tracks are forbidden, so keep at least one keyframe if ( from >= to ) { to = Math.max( to, 1 ); from = to - 1; } const stride = this.getValueSize(); this.times = AnimationUtils.arraySlice( times, from, to ); this.values = AnimationUtils.arraySlice( this.values, from * stride, to * stride ); } return this; }, // ensure we do not get a GarbageInGarbageOut situation, make sure tracks are at least minimally viable validate: function () { let valid = true; const valueSize = this.getValueSize(); if ( valueSize - Math.floor( valueSize ) !== 0 ) { console.error( 'THREE.KeyframeTrack: Invalid value size in track.', this ); valid = false; } const times = this.times, values = this.values, nKeys = times.length; if ( nKeys === 0 ) { console.error( 'THREE.KeyframeTrack: Track is empty.', this ); valid = false; } let prevTime = null; for ( let i = 0; i !== nKeys; i ++ ) { const currTime = times[ i ]; if ( typeof currTime === 'number' && isNaN( currTime ) ) { console.error( 'THREE.KeyframeTrack: Time is not a valid number.', this, i, currTime ); valid = false; break; } if ( prevTime !== null && prevTime > currTime ) { console.error( 'THREE.KeyframeTrack: Out of order keys.', this, i, currTime, prevTime ); valid = false; break; } prevTime = currTime; } if ( values !== undefined ) { if ( AnimationUtils.isTypedArray( values ) ) { for ( let i = 0, n = values.length; i !== n; ++ i ) { const value = values[ i ]; if ( isNaN( value ) ) { console.error( 'THREE.KeyframeTrack: Value is not a valid number.', this, i, value ); valid = false; break; } } } } return valid; }, // removes equivalent sequential keys as common in morph target sequences // (0,0,0,0,1,1,1,0,0,0,0,0,0,0) --> (0,0,1,1,0,0) optimize: function () { // times or values may be shared with other tracks, so overwriting is unsafe const times = AnimationUtils.arraySlice( this.times ), values = AnimationUtils.arraySlice( this.values ), stride = this.getValueSize(), smoothInterpolation = this.getInterpolation() === InterpolateSmooth, lastIndex = times.length - 1; let writeIndex = 1; for ( let i = 1; i < lastIndex; ++ i ) { let keep = false; const time = times[ i ]; const timeNext = times[ i + 1 ]; // remove adjacent keyframes scheduled at the same time if ( time !== timeNext && ( i !== 1 || time !== time[ 0 ] ) ) { if ( ! smoothInterpolation ) { // remove unnecessary keyframes same as their neighbors const offset = i * stride, offsetP = offset - stride, offsetN = offset + stride; for ( let j = 0; j !== stride; ++ j ) { const value = values[ offset + j ]; if ( value !== values[ offsetP + j ] || value !== values[ offsetN + j ] ) { keep = true; break; } } } else { keep = true; } } // in-place compaction if ( keep ) { if ( i !== writeIndex ) { times[ writeIndex ] = times[ i ]; const readOffset = i * stride, writeOffset = writeIndex * stride; for ( let j = 0; j !== stride; ++ j ) { values[ writeOffset + j ] = values[ readOffset + j ]; } } ++ writeIndex; } } // flush last keyframe (compaction looks ahead) if ( lastIndex > 0 ) { times[ writeIndex ] = times[ lastIndex ]; for ( let readOffset = lastIndex * stride, writeOffset = writeIndex * stride, j = 0; j !== stride; ++ j ) { values[ writeOffset + j ] = values[ readOffset + j ]; } ++ writeIndex; } if ( writeIndex !== times.length ) { this.times = AnimationUtils.arraySlice( times, 0, writeIndex ); this.values = AnimationUtils.arraySlice( values, 0, writeIndex * stride ); } else { this.times = times; this.values = values; } return this; }, clone: function () { const times = AnimationUtils.arraySlice( this.times, 0 ); const values = AnimationUtils.arraySlice( this.values, 0 ); const TypedKeyframeTrack = this.constructor; const track = new TypedKeyframeTrack( this.name, times, values ); // Interpolant argument to constructor is not saved, so copy the factory method directly. track.createInterpolant = this.createInterpolant; return track; } } ); /** * A Track of Boolean keyframe values. */ function BooleanKeyframeTrack( name, times, values ) { KeyframeTrack.call( this, name, times, values ); } BooleanKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), { constructor: BooleanKeyframeTrack, ValueTypeName: 'bool', ValueBufferType: Array, DefaultInterpolation: InterpolateDiscrete, InterpolantFactoryMethodLinear: undefined, InterpolantFactoryMethodSmooth: undefined // Note: Actually this track could have a optimized / compressed // representation of a single value and a custom interpolant that // computes "firstValue ^ isOdd( index )". } ); /** * A Track of keyframe values that represent color. */ function ColorKeyframeTrack( name, times, values, interpolation ) { KeyframeTrack.call( this, name, times, values, interpolation ); } ColorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), { constructor: ColorKeyframeTrack, ValueTypeName: 'color' // ValueBufferType is inherited // DefaultInterpolation is inherited // Note: Very basic implementation and nothing special yet. // However, this is the place for color space parameterization. } ); /** * A Track of numeric keyframe values. */ function NumberKeyframeTrack( name, times, values, interpolation ) { KeyframeTrack.call( this, name, times, values, interpolation ); } NumberKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), { constructor: NumberKeyframeTrack, ValueTypeName: 'number' // ValueBufferType is inherited // DefaultInterpolation is inherited } ); /** * Spherical linear unit quaternion interpolant. */ function QuaternionLinearInterpolant( parameterPositions, sampleValues, sampleSize, resultBuffer ) { Interpolant.call( this, parameterPositions, sampleValues, sampleSize, resultBuffer ); } QuaternionLinearInterpolant.prototype = Object.assign( Object.create( Interpolant.prototype ), { constructor: QuaternionLinearInterpolant, interpolate_: function ( i1, t0, t, t1 ) { const result = this.resultBuffer, values = this.sampleValues, stride = this.valueSize, alpha = ( t - t0 ) / ( t1 - t0 ); let offset = i1 * stride; for ( let end = offset + stride; offset !== end; offset += 4 ) { Quaternion.slerpFlat( result, 0, values, offset - stride, values, offset, alpha ); } return result; } } ); /** * A Track of quaternion keyframe values. */ function QuaternionKeyframeTrack( name, times, values, interpolation ) { KeyframeTrack.call( this, name, times, values, interpolation ); } QuaternionKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), { constructor: QuaternionKeyframeTrack, ValueTypeName: 'quaternion', // ValueBufferType is inherited DefaultInterpolation: InterpolateLinear, InterpolantFactoryMethodLinear: function ( result ) { return new QuaternionLinearInterpolant( this.times, this.values, this.getValueSize(), result ); }, InterpolantFactoryMethodSmooth: undefined // not yet implemented } ); /** * A Track that interpolates Strings */ function StringKeyframeTrack( name, times, values, interpolation ) { KeyframeTrack.call( this, name, times, values, interpolation ); } StringKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), { constructor: StringKeyframeTrack, ValueTypeName: 'string', ValueBufferType: Array, DefaultInterpolation: InterpolateDiscrete, InterpolantFactoryMethodLinear: undefined, InterpolantFactoryMethodSmooth: undefined } ); /** * A Track of vectored keyframe values. */ function VectorKeyframeTrack( name, times, values, interpolation ) { KeyframeTrack.call( this, name, times, values, interpolation ); } VectorKeyframeTrack.prototype = Object.assign( Object.create( KeyframeTrack.prototype ), { constructor: VectorKeyframeTrack, ValueTypeName: 'vector' // ValueBufferType is inherited // DefaultInterpolation is inherited } ); function AnimationClip( name, duration = - 1, tracks, blendMode = NormalAnimationBlendMode ) { this.name = name; this.tracks = tracks; this.duration = duration; this.blendMode = blendMode; this.uuid = MathUtils.generateUUID(); // this means it should figure out its duration by scanning the tracks if ( this.duration < 0 ) { this.resetDuration(); } } function getTrackTypeForValueTypeName( typeName ) { switch ( typeName.toLowerCase() ) { case 'scalar': case 'double': case 'float': case 'number': case 'integer': return NumberKeyframeTrack; case 'vector': case 'vector2': case 'vector3': case 'vector4': return VectorKeyframeTrack; case 'color': return ColorKeyframeTrack; case 'quaternion': return QuaternionKeyframeTrack; case 'bool': case 'boolean': return BooleanKeyframeTrack; case 'string': return StringKeyframeTrack; } throw new Error( 'THREE.KeyframeTrack: Unsupported typeName: ' + typeName ); } function parseKeyframeTrack( json ) { if ( json.type === undefined ) { throw new Error( 'THREE.KeyframeTrack: track type undefined, can not parse' ); } const trackType = getTrackTypeForValueTypeName( json.type ); if ( json.times === undefined ) { const times = [], values = []; AnimationUtils.flattenJSON( json.keys, times, values, 'value' ); json.times = times; json.values = values; } // derived classes can define a static parse method if ( trackType.parse !== undefined ) { return trackType.parse( json ); } else { // by default, we assume a constructor compatible with the base return new trackType( json.name, json.times, json.values, json.interpolation ); } } Object.assign( AnimationClip, { parse: function ( json ) { const tracks = [], jsonTracks = json.tracks, frameTime = 1.0 / ( json.fps || 1.0 ); for ( let i = 0, n = jsonTracks.length; i !== n; ++ i ) { tracks.push( parseKeyframeTrack( jsonTracks[ i ] ).scale( frameTime ) ); } const clip = new AnimationClip( json.name, json.duration, tracks, json.blendMode ); clip.uuid = json.uuid; return clip; }, toJSON: function ( clip ) { const tracks = [], clipTracks = clip.tracks; const json = { 'name': clip.name, 'duration': clip.duration, 'tracks': tracks, 'uuid': clip.uuid, 'blendMode': clip.blendMode }; for ( let i = 0, n = clipTracks.length; i !== n; ++ i ) { tracks.push( KeyframeTrack.toJSON( clipTracks[ i ] ) ); } return json; }, CreateFromMorphTargetSequence: function ( name, morphTargetSequence, fps, noLoop ) { const numMorphTargets = morphTargetSequence.length; const tracks = []; for ( let i = 0; i < numMorphTargets; i ++ ) { let times = []; let values = []; times.push( ( i + numMorphTargets - 1 ) % numMorphTargets, i, ( i + 1 ) % numMorphTargets ); values.push( 0, 1, 0 ); const order = AnimationUtils.getKeyframeOrder( times ); times = AnimationUtils.sortedArray( times, 1, order ); values = AnimationUtils.sortedArray( values, 1, order ); // if there is a key at the first frame, duplicate it as the // last frame as well for perfect loop. if ( ! noLoop && times[ 0 ] === 0 ) { times.push( numMorphTargets ); values.push( values[ 0 ] ); } tracks.push( new NumberKeyframeTrack( '.morphTargetInfluences[' + morphTargetSequence[ i ].name + ']', times, values ).scale( 1.0 / fps ) ); } return new AnimationClip( name, - 1, tracks ); }, findByName: function ( objectOrClipArray, name ) { let clipArray = objectOrClipArray; if ( ! Array.isArray( objectOrClipArray ) ) { const o = objectOrClipArray; clipArray = o.geometry && o.geometry.animations || o.animations; } for ( let i = 0; i < clipArray.length; i ++ ) { if ( clipArray[ i ].name === name ) { return clipArray[ i ]; } } return null; }, CreateClipsFromMorphTargetSequences: function ( morphTargets, fps, noLoop ) { const animationToMorphTargets = {}; // tested with https://regex101.com/ on trick sequences // such flamingo_flyA_003, flamingo_run1_003, crdeath0059 const pattern = /^([\w-]*?)([\d]+)$/; // sort morph target names into animation groups based // patterns like Walk_001, Walk_002, Run_001, Run_002 for ( let i = 0, il = morphTargets.length; i < il; i ++ ) { const morphTarget = morphTargets[ i ]; const parts = morphTarget.name.match( pattern ); if ( parts && parts.length > 1 ) { const name = parts[ 1 ]; let animationMorphTargets = animationToMorphTargets[ name ]; if ( ! animationMorphTargets ) { animationToMorphTargets[ name ] = animationMorphTargets = []; } animationMorphTargets.push( morphTarget ); } } const clips = []; for ( const name in animationToMorphTargets ) { clips.push( AnimationClip.CreateFromMorphTargetSequence( name, animationToMorphTargets[ name ], fps, noLoop ) ); } return clips; }, // parse the animation.hierarchy format parseAnimation: function ( animation, bones ) { if ( ! animation ) { console.error( 'THREE.AnimationClip: No animation in JSONLoader data.' ); return null; } const addNonemptyTrack = function ( trackType, trackName, animationKeys, propertyName, destTracks ) { // only return track if there are actually keys. if ( animationKeys.length !== 0 ) { const times = []; const values = []; AnimationUtils.flattenJSON( animationKeys, times, values, propertyName ); // empty keys are filtered out, so check again if ( times.length !== 0 ) { destTracks.push( new trackType( trackName, times, values ) ); } } }; const tracks = []; const clipName = animation.name || 'default'; const fps = animation.fps || 30; const blendMode = animation.blendMode; // automatic length determination in AnimationClip. let duration = animation.length || - 1; const hierarchyTracks = animation.hierarchy || []; for ( let h = 0; h < hierarchyTracks.length; h ++ ) { const animationKeys = hierarchyTracks[ h ].keys; // skip empty tracks if ( ! animationKeys || animationKeys.length === 0 ) continue; // process morph targets if ( animationKeys[ 0 ].morphTargets ) { // figure out all morph targets used in this track const morphTargetNames = {}; let k; for ( k = 0; k < animationKeys.length; k ++ ) { if ( animationKeys[ k ].morphTargets ) { for ( let m = 0; m < animationKeys[ k ].morphTargets.length; m ++ ) { morphTargetNames[ animationKeys[ k ].morphTargets[ m ] ] = - 1; } } } // create a track for each morph target with all zero // morphTargetInfluences except for the keys in which // the morphTarget is named. for ( const morphTargetName in morphTargetNames ) { const times = []; const values = []; for ( let m = 0; m !== animationKeys[ k ].morphTargets.length; ++ m ) { const animationKey = animationKeys[ k ]; times.push( animationKey.time ); values.push( ( animationKey.morphTarget === morphTargetName ) ? 1 : 0 ); } tracks.push( new NumberKeyframeTrack( '.morphTargetInfluence[' + morphTargetName + ']', times, values ) ); } duration = morphTargetNames.length * ( fps || 1.0 ); } else { // ...assume skeletal animation const boneName = '.bones[' + bones[ h ].name + ']'; addNonemptyTrack( VectorKeyframeTrack, boneName + '.position', animationKeys, 'pos', tracks ); addNonemptyTrack( QuaternionKeyframeTrack, boneName + '.quaternion', animationKeys, 'rot', tracks ); addNonemptyTrack( VectorKeyframeTrack, boneName + '.scale', animationKeys, 'scl', tracks ); } } if ( tracks.length === 0 ) { return null; } const clip = new AnimationClip( clipName, duration, tracks, blendMode ); return clip; } } ); Object.assign( AnimationClip.prototype, { resetDuration: function () { const tracks = this.tracks; let duration = 0; for ( let i = 0, n = tracks.length; i !== n; ++ i ) { const track = this.tracks[ i ]; duration = Math.max( duration, track.times[ track.times.length - 1 ] ); } this.duration = duration; return this; }, trim: function () { for ( let i = 0; i < this.tracks.length; i ++ ) { this.tracks[ i ].trim( 0, this.duration ); } return this; }, validate: function () { let valid = true; for ( let i = 0; i < this.tracks.length; i ++ ) { valid = valid && this.tracks[ i ].validate(); } return valid; }, optimize: function () { for ( let i = 0; i < this.tracks.length; i ++ ) { this.tracks[ i ].optimize(); } return this; }, clone: function () { const tracks = []; for ( let i = 0; i < this.tracks.length; i ++ ) { tracks.push( this.tracks[ i ].clone() ); } return new AnimationClip( this.name, this.duration, tracks, this.blendMode ); }, toJSON: function () { return AnimationClip.toJSON( this ); } } ); const Cache = { enabled: false, files: {}, add: function ( key, file ) { if ( this.enabled === false ) return; // console.log( 'THREE.Cache', 'Adding key:', key ); this.files[ key ] = file; }, get: function ( key ) { if ( this.enabled === false ) return; // console.log( 'THREE.Cache', 'Checking key:', key ); return this.files[ key ]; }, remove: function ( key ) { delete this.files[ key ]; }, clear: function () { this.files = {}; } }; function LoadingManager( onLoad, onProgress, onError ) { const scope = this; let isLoading = false; let itemsLoaded = 0; let itemsTotal = 0; let urlModifier = undefined; const handlers = []; // Refer to #5689 for the reason why we don't set .onStart // in the constructor this.onStart = undefined; this.onLoad = onLoad; this.onProgress = onProgress; this.onError = onError; this.itemStart = function ( url ) { itemsTotal ++; if ( isLoading === false ) { if ( scope.onStart !== undefined ) { scope.onStart( url, itemsLoaded, itemsTotal ); } } isLoading = true; }; this.itemEnd = function ( url ) { itemsLoaded ++; if ( scope.onProgress !== undefined ) { scope.onProgress( url, itemsLoaded, itemsTotal ); } if ( itemsLoaded === itemsTotal ) { isLoading = false; if ( scope.onLoad !== undefined ) { scope.onLoad(); } } }; this.itemError = function ( url ) { if ( scope.onError !== undefined ) { scope.onError( url ); } }; this.resolveURL = function ( url ) { if ( urlModifier ) { return urlModifier( url ); } return url; }; this.setURLModifier = function ( transform ) { urlModifier = transform; return this; }; this.addHandler = function ( regex, loader ) { handlers.push( regex, loader ); return this; }; this.removeHandler = function ( regex ) { const index = handlers.indexOf( regex ); if ( index !== - 1 ) { handlers.splice( index, 2 ); } return this; }; this.getHandler = function ( file ) { for ( let i = 0, l = handlers.length; i < l; i += 2 ) { const regex = handlers[ i ]; const loader = handlers[ i + 1 ]; if ( regex.global ) regex.lastIndex = 0; // see #17920 if ( regex.test( file ) ) { return loader; } } return null; }; } const DefaultLoadingManager = new LoadingManager(); function Loader( manager ) { this.manager = ( manager !== undefined ) ? manager : DefaultLoadingManager; this.crossOrigin = 'anonymous'; this.withCredentials = false; this.path = ''; this.resourcePath = ''; this.requestHeader = {}; } Object.assign( Loader.prototype, { load: function ( /* url, onLoad, onProgress, onError */ ) {}, loadAsync: function ( url, onProgress ) { const scope = this; return new Promise( function ( resolve, reject ) { scope.load( url, resolve, onProgress, reject ); } ); }, parse: function ( /* data */ ) {}, setCrossOrigin: function ( crossOrigin ) { this.crossOrigin = crossOrigin; return this; }, setWithCredentials: function ( value ) { this.withCredentials = value; return this; }, setPath: function ( path ) { this.path = path; return this; }, setResourcePath: function ( resourcePath ) { this.resourcePath = resourcePath; return this; }, setRequestHeader: function ( requestHeader ) { this.requestHeader = requestHeader; return this; } } ); const loading = {}; function FileLoader( manager ) { Loader.call( this, manager ); } FileLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: FileLoader, load: function ( url, onLoad, onProgress, onError ) { if ( url === undefined ) url = ''; if ( this.path !== undefined ) url = this.path + url; url = this.manager.resolveURL( url ); const scope = this; const cached = Cache.get( url ); if ( cached !== undefined ) { scope.manager.itemStart( url ); setTimeout( function () { if ( onLoad ) onLoad( cached ); scope.manager.itemEnd( url ); }, 0 ); return cached; } // Check if request is duplicate if ( loading[ url ] !== undefined ) { loading[ url ].push( { onLoad: onLoad, onProgress: onProgress, onError: onError } ); return; } // Check for data: URI const dataUriRegex = /^data:(.*?)(;base64)?,(.*)$/; const dataUriRegexResult = url.match( dataUriRegex ); let request; // Safari can not handle Data URIs through XMLHttpRequest so process manually if ( dataUriRegexResult ) { const mimeType = dataUriRegexResult[ 1 ]; const isBase64 = !! dataUriRegexResult[ 2 ]; let data = dataUriRegexResult[ 3 ]; data = decodeURIComponent( data ); if ( isBase64 ) data = atob( data ); try { let response; const responseType = ( this.responseType || '' ).toLowerCase(); switch ( responseType ) { case 'arraybuffer': case 'blob': const view = new Uint8Array( data.length ); for ( let i = 0; i < data.length; i ++ ) { view[ i ] = data.charCodeAt( i ); } if ( responseType === 'blob' ) { response = new Blob( [ view.buffer ], { type: mimeType } ); } else { response = view.buffer; } break; case 'document': const parser = new DOMParser(); response = parser.parseFromString( data, mimeType ); break; case 'json': response = JSON.parse( data ); break; default: // 'text' or other response = data; break; } // Wait for next browser tick like standard XMLHttpRequest event dispatching does setTimeout( function () { if ( onLoad ) onLoad( response ); scope.manager.itemEnd( url ); }, 0 ); } catch ( error ) { // Wait for next browser tick like standard XMLHttpRequest event dispatching does setTimeout( function () { if ( onError ) onError( error ); scope.manager.itemError( url ); scope.manager.itemEnd( url ); }, 0 ); } } else { // Initialise array for duplicate requests loading[ url ] = []; loading[ url ].push( { onLoad: onLoad, onProgress: onProgress, onError: onError } ); request = new XMLHttpRequest(); request.open( 'GET', url, true ); request.addEventListener( 'load', function ( event ) { const response = this.response; const callbacks = loading[ url ]; delete loading[ url ]; if ( this.status === 200 || this.status === 0 ) { // Some browsers return HTTP Status 0 when using non-http protocol // e.g. 'file://' or 'data://'. Handle as success. if ( this.status === 0 ) console.warn( 'THREE.FileLoader: HTTP Status 0 received.' ); // Add to cache only on HTTP success, so that we do not cache // error response bodies as proper responses to requests. Cache.add( url, response ); for ( let i = 0, il = callbacks.length; i < il; i ++ ) { const callback = callbacks[ i ]; if ( callback.onLoad ) callback.onLoad( response ); } scope.manager.itemEnd( url ); } else { for ( let i = 0, il = callbacks.length; i < il; i ++ ) { const callback = callbacks[ i ]; if ( callback.onError ) callback.onError( event ); } scope.manager.itemError( url ); scope.manager.itemEnd( url ); } }, false ); request.addEventListener( 'progress', function ( event ) { const callbacks = loading[ url ]; for ( let i = 0, il = callbacks.length; i < il; i ++ ) { const callback = callbacks[ i ]; if ( callback.onProgress ) callback.onProgress( event ); } }, false ); request.addEventListener( 'error', function ( event ) { const callbacks = loading[ url ]; delete loading[ url ]; for ( let i = 0, il = callbacks.length; i < il; i ++ ) { const callback = callbacks[ i ]; if ( callback.onError ) callback.onError( event ); } scope.manager.itemError( url ); scope.manager.itemEnd( url ); }, false ); request.addEventListener( 'abort', function ( event ) { const callbacks = loading[ url ]; delete loading[ url ]; for ( let i = 0, il = callbacks.length; i < il; i ++ ) { const callback = callbacks[ i ]; if ( callback.onError ) callback.onError( event ); } scope.manager.itemError( url ); scope.manager.itemEnd( url ); }, false ); if ( this.responseType !== undefined ) request.responseType = this.responseType; if ( this.withCredentials !== undefined ) request.withCredentials = this.withCredentials; if ( request.overrideMimeType ) request.overrideMimeType( this.mimeType !== undefined ? this.mimeType : 'text/plain' ); for ( const header in this.requestHeader ) { request.setRequestHeader( header, this.requestHeader[ header ] ); } request.send( null ); } scope.manager.itemStart( url ); return request; }, setResponseType: function ( value ) { this.responseType = value; return this; }, setMimeType: function ( value ) { this.mimeType = value; return this; } } ); function AnimationLoader( manager ) { Loader.call( this, manager ); } AnimationLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: AnimationLoader, load: function ( url, onLoad, onProgress, onError ) { const scope = this; const loader = new FileLoader( scope.manager ); loader.setPath( scope.path ); loader.setRequestHeader( scope.requestHeader ); loader.setWithCredentials( scope.withCredentials ); loader.load( url, function ( text ) { try { onLoad( scope.parse( JSON.parse( text ) ) ); } catch ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); } }, onProgress, onError ); }, parse: function ( json ) { const animations = []; for ( let i = 0; i < json.length; i ++ ) { const clip = AnimationClip.parse( json[ i ] ); animations.push( clip ); } return animations; } } ); /** * Abstract Base class to block based textures loader (dds, pvr, ...) * * Sub classes have to implement the parse() method which will be used in load(). */ function CompressedTextureLoader( manager ) { Loader.call( this, manager ); } CompressedTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: CompressedTextureLoader, load: function ( url, onLoad, onProgress, onError ) { const scope = this; const images = []; const texture = new CompressedTexture(); const loader = new FileLoader( this.manager ); loader.setPath( this.path ); loader.setResponseType( 'arraybuffer' ); loader.setRequestHeader( this.requestHeader ); loader.setWithCredentials( scope.withCredentials ); let loaded = 0; function loadTexture( i ) { loader.load( url[ i ], function ( buffer ) { const texDatas = scope.parse( buffer, true ); images[ i ] = { width: texDatas.width, height: texDatas.height, format: texDatas.format, mipmaps: texDatas.mipmaps }; loaded += 1; if ( loaded === 6 ) { if ( texDatas.mipmapCount === 1 ) texture.minFilter = LinearFilter; texture.image = images; texture.format = texDatas.format; texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } }, onProgress, onError ); } if ( Array.isArray( url ) ) { for ( let i = 0, il = url.length; i < il; ++ i ) { loadTexture( i ); } } else { // compressed cubemap texture stored in a single DDS file loader.load( url, function ( buffer ) { const texDatas = scope.parse( buffer, true ); if ( texDatas.isCubemap ) { const faces = texDatas.mipmaps.length / texDatas.mipmapCount; for ( let f = 0; f < faces; f ++ ) { images[ f ] = { mipmaps: [] }; for ( let i = 0; i < texDatas.mipmapCount; i ++ ) { images[ f ].mipmaps.push( texDatas.mipmaps[ f * texDatas.mipmapCount + i ] ); images[ f ].format = texDatas.format; images[ f ].width = texDatas.width; images[ f ].height = texDatas.height; } } texture.image = images; } else { texture.image.width = texDatas.width; texture.image.height = texDatas.height; texture.mipmaps = texDatas.mipmaps; } if ( texDatas.mipmapCount === 1 ) { texture.minFilter = LinearFilter; } texture.format = texDatas.format; texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); }, onProgress, onError ); } return texture; } } ); function ImageLoader( manager ) { Loader.call( this, manager ); } ImageLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: ImageLoader, load: function ( url, onLoad, onProgress, onError ) { if ( this.path !== undefined ) url = this.path + url; url = this.manager.resolveURL( url ); const scope = this; const cached = Cache.get( url ); if ( cached !== undefined ) { scope.manager.itemStart( url ); setTimeout( function () { if ( onLoad ) onLoad( cached ); scope.manager.itemEnd( url ); }, 0 ); return cached; } const image = document.createElementNS( 'http://www.w3.org/1999/xhtml', 'img' ); function onImageLoad() { image.removeEventListener( 'load', onImageLoad, false ); image.removeEventListener( 'error', onImageError, false ); Cache.add( url, this ); if ( onLoad ) onLoad( this ); scope.manager.itemEnd( url ); } function onImageError( event ) { image.removeEventListener( 'load', onImageLoad, false ); image.removeEventListener( 'error', onImageError, false ); if ( onError ) onError( event ); scope.manager.itemError( url ); scope.manager.itemEnd( url ); } image.addEventListener( 'load', onImageLoad, false ); image.addEventListener( 'error', onImageError, false ); if ( url.substr( 0, 5 ) !== 'data:' ) { if ( this.crossOrigin !== undefined ) image.crossOrigin = this.crossOrigin; } scope.manager.itemStart( url ); image.src = url; return image; } } ); function CubeTextureLoader( manager ) { Loader.call( this, manager ); } CubeTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: CubeTextureLoader, load: function ( urls, onLoad, onProgress, onError ) { const texture = new CubeTexture(); const loader = new ImageLoader( this.manager ); loader.setCrossOrigin( this.crossOrigin ); loader.setPath( this.path ); let loaded = 0; function loadTexture( i ) { loader.load( urls[ i ], function ( image ) { texture.images[ i ] = image; loaded ++; if ( loaded === 6 ) { texture.needsUpdate = true; if ( onLoad ) onLoad( texture ); } }, undefined, onError ); } for ( let i = 0; i < urls.length; ++ i ) { loadTexture( i ); } return texture; } } ); /** * Abstract Base class to load generic binary textures formats (rgbe, hdr, ...) * * Sub classes have to implement the parse() method which will be used in load(). */ function DataTextureLoader( manager ) { Loader.call( this, manager ); } DataTextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: DataTextureLoader, load: function ( url, onLoad, onProgress, onError ) { const scope = this; const texture = new DataTexture(); const loader = new FileLoader( this.manager ); loader.setResponseType( 'arraybuffer' ); loader.setRequestHeader( this.requestHeader ); loader.setPath( this.path ); loader.setWithCredentials( scope.withCredentials ); loader.load( url, function ( buffer ) { const texData = scope.parse( buffer ); if ( ! texData ) return; if ( texData.image !== undefined ) { texture.image = texData.image; } else if ( texData.data !== undefined ) { texture.image.width = texData.width; texture.image.height = texData.height; texture.image.data = texData.data; } texture.wrapS = texData.wrapS !== undefined ? texData.wrapS : ClampToEdgeWrapping; texture.wrapT = texData.wrapT !== undefined ? texData.wrapT : ClampToEdgeWrapping; texture.magFilter = texData.magFilter !== undefined ? texData.magFilter : LinearFilter; texture.minFilter = texData.minFilter !== undefined ? texData.minFilter : LinearFilter; texture.anisotropy = texData.anisotropy !== undefined ? texData.anisotropy : 1; if ( texData.format !== undefined ) { texture.format = texData.format; } if ( texData.type !== undefined ) { texture.type = texData.type; } if ( texData.mipmaps !== undefined ) { texture.mipmaps = texData.mipmaps; texture.minFilter = LinearMipmapLinearFilter; // presumably... } if ( texData.mipmapCount === 1 ) { texture.minFilter = LinearFilter; } texture.needsUpdate = true; if ( onLoad ) onLoad( texture, texData ); }, onProgress, onError ); return texture; } } ); function TextureLoader( manager ) { Loader.call( this, manager ); } TextureLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: TextureLoader, load: function ( url, onLoad, onProgress, onError ) { const texture = new Texture(); const loader = new ImageLoader( this.manager ); loader.setCrossOrigin( this.crossOrigin ); loader.setPath( this.path ); loader.load( url, function ( image ) { texture.image = image; // JPEGs can't have an alpha channel, so memory can be saved by storing them as RGB. const isJPEG = url.search( /\.jpe?g($|\?)/i ) > 0 || url.search( /^data\:image\/jpeg/ ) === 0; texture.format = isJPEG ? RGBFormat : RGBAFormat; texture.needsUpdate = true; if ( onLoad !== undefined ) { onLoad( texture ); } }, onProgress, onError ); return texture; } } ); /** * Extensible curve object. * * Some common of curve methods: * .getPoint( t, optionalTarget ), .getTangent( t, optionalTarget ) * .getPointAt( u, optionalTarget ), .getTangentAt( u, optionalTarget ) * .getPoints(), .getSpacedPoints() * .getLength() * .updateArcLengths() * * This following curves inherit from THREE.Curve: * * -- 2D curves -- * THREE.ArcCurve * THREE.CubicBezierCurve * THREE.EllipseCurve * THREE.LineCurve * THREE.QuadraticBezierCurve * THREE.SplineCurve * * -- 3D curves -- * THREE.CatmullRomCurve3 * THREE.CubicBezierCurve3 * THREE.LineCurve3 * THREE.QuadraticBezierCurve3 * * A series of curves can be represented as a THREE.CurvePath. * **/ function Curve() { this.type = 'Curve'; this.arcLengthDivisions = 200; } Object.assign( Curve.prototype, { // Virtual base class method to overwrite and implement in subclasses // - t [0 .. 1] getPoint: function ( /* t, optionalTarget */ ) { console.warn( 'THREE.Curve: .getPoint() not implemented.' ); return null; }, // Get point at relative position in curve according to arc length // - u [0 .. 1] getPointAt: function ( u, optionalTarget ) { const t = this.getUtoTmapping( u ); return this.getPoint( t, optionalTarget ); }, // Get sequence of points using getPoint( t ) getPoints: function ( divisions = 5 ) { const points = []; for ( let d = 0; d <= divisions; d ++ ) { points.push( this.getPoint( d / divisions ) ); } return points; }, // Get sequence of points using getPointAt( u ) getSpacedPoints: function ( divisions = 5 ) { const points = []; for ( let d = 0; d <= divisions; d ++ ) { points.push( this.getPointAt( d / divisions ) ); } return points; }, // Get total curve arc length getLength: function () { const lengths = this.getLengths(); return lengths[ lengths.length - 1 ]; }, // Get list of cumulative segment lengths getLengths: function ( divisions ) { if ( divisions === undefined ) divisions = this.arcLengthDivisions; if ( this.cacheArcLengths && ( this.cacheArcLengths.length === divisions + 1 ) && ! this.needsUpdate ) { return this.cacheArcLengths; } this.needsUpdate = false; const cache = []; let current, last = this.getPoint( 0 ); let sum = 0; cache.push( 0 ); for ( let p = 1; p <= divisions; p ++ ) { current = this.getPoint( p / divisions ); sum += current.distanceTo( last ); cache.push( sum ); last = current; } this.cacheArcLengths = cache; return cache; // { sums: cache, sum: sum }; Sum is in the last element. }, updateArcLengths: function () { this.needsUpdate = true; this.getLengths(); }, // Given u ( 0 .. 1 ), get a t to find p. This gives you points which are equidistant getUtoTmapping: function ( u, distance ) { const arcLengths = this.getLengths(); let i = 0; const il = arcLengths.length; let targetArcLength; // The targeted u distance value to get if ( distance ) { targetArcLength = distance; } else { targetArcLength = u * arcLengths[ il - 1 ]; } // binary search for the index with largest value smaller than target u distance let low = 0, high = il - 1, comparison; while ( low <= high ) { i = Math.floor( low + ( high - low ) / 2 ); // less likely to overflow, though probably not issue here, JS doesn't really have integers, all numbers are floats comparison = arcLengths[ i ] - targetArcLength; if ( comparison < 0 ) { low = i + 1; } else if ( comparison > 0 ) { high = i - 1; } else { high = i; break; // DONE } } i = high; if ( arcLengths[ i ] === targetArcLength ) { return i / ( il - 1 ); } // we could get finer grain at lengths, or use simple interpolation between two points const lengthBefore = arcLengths[ i ]; const lengthAfter = arcLengths[ i + 1 ]; const segmentLength = lengthAfter - lengthBefore; // determine where we are between the 'before' and 'after' points const segmentFraction = ( targetArcLength - lengthBefore ) / segmentLength; // add that fractional amount to t const t = ( i + segmentFraction ) / ( il - 1 ); return t; }, // Returns a unit vector tangent at t // In case any sub curve does not implement its tangent derivation, // 2 points a small delta apart will be used to find its gradient // which seems to give a reasonable approximation getTangent: function ( t, optionalTarget ) { const delta = 0.0001; let t1 = t - delta; let t2 = t + delta; // Capping in case of danger if ( t1 < 0 ) t1 = 0; if ( t2 > 1 ) t2 = 1; const pt1 = this.getPoint( t1 ); const pt2 = this.getPoint( t2 ); const tangent = optionalTarget || ( ( pt1.isVector2 ) ? new Vector2() : new Vector3() ); tangent.copy( pt2 ).sub( pt1 ).normalize(); return tangent; }, getTangentAt: function ( u, optionalTarget ) { const t = this.getUtoTmapping( u ); return this.getTangent( t, optionalTarget ); }, computeFrenetFrames: function ( segments, closed ) { // see http://www.cs.indiana.edu/pub/techreports/TR425.pdf const normal = new Vector3(); const tangents = []; const normals = []; const binormals = []; const vec = new Vector3(); const mat = new Matrix4(); // compute the tangent vectors for each segment on the curve for ( let i = 0; i <= segments; i ++ ) { const u = i / segments; tangents[ i ] = this.getTangentAt( u, new Vector3() ); tangents[ i ].normalize(); } // select an initial normal vector perpendicular to the first tangent vector, // and in the direction of the minimum tangent xyz component normals[ 0 ] = new Vector3(); binormals[ 0 ] = new Vector3(); let min = Number.MAX_VALUE; const tx = Math.abs( tangents[ 0 ].x ); const ty = Math.abs( tangents[ 0 ].y ); const tz = Math.abs( tangents[ 0 ].z ); if ( tx <= min ) { min = tx; normal.set( 1, 0, 0 ); } if ( ty <= min ) { min = ty; normal.set( 0, 1, 0 ); } if ( tz <= min ) { normal.set( 0, 0, 1 ); } vec.crossVectors( tangents[ 0 ], normal ).normalize(); normals[ 0 ].crossVectors( tangents[ 0 ], vec ); binormals[ 0 ].crossVectors( tangents[ 0 ], normals[ 0 ] ); // compute the slowly-varying normal and binormal vectors for each segment on the curve for ( let i = 1; i <= segments; i ++ ) { normals[ i ] = normals[ i - 1 ].clone(); binormals[ i ] = binormals[ i - 1 ].clone(); vec.crossVectors( tangents[ i - 1 ], tangents[ i ] ); if ( vec.length() > Number.EPSILON ) { vec.normalize(); const theta = Math.acos( MathUtils.clamp( tangents[ i - 1 ].dot( tangents[ i ] ), - 1, 1 ) ); // clamp for floating pt errors normals[ i ].applyMatrix4( mat.makeRotationAxis( vec, theta ) ); } binormals[ i ].crossVectors( tangents[ i ], normals[ i ] ); } // if the curve is closed, postprocess the vectors so the first and last normal vectors are the same if ( closed === true ) { let theta = Math.acos( MathUtils.clamp( normals[ 0 ].dot( normals[ segments ] ), - 1, 1 ) ); theta /= segments; if ( tangents[ 0 ].dot( vec.crossVectors( normals[ 0 ], normals[ segments ] ) ) > 0 ) { theta = - theta; } for ( let i = 1; i <= segments; i ++ ) { // twist a little... normals[ i ].applyMatrix4( mat.makeRotationAxis( tangents[ i ], theta * i ) ); binormals[ i ].crossVectors( tangents[ i ], normals[ i ] ); } } return { tangents: tangents, normals: normals, binormals: binormals }; }, clone: function () { return new this.constructor().copy( this ); }, copy: function ( source ) { this.arcLengthDivisions = source.arcLengthDivisions; return this; }, toJSON: function () { const data = { metadata: { version: 4.5, type: 'Curve', generator: 'Curve.toJSON' } }; data.arcLengthDivisions = this.arcLengthDivisions; data.type = this.type; return data; }, fromJSON: function ( json ) { this.arcLengthDivisions = json.arcLengthDivisions; return this; } } ); function EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) { Curve.call( this ); this.type = 'EllipseCurve'; this.aX = aX || 0; this.aY = aY || 0; this.xRadius = xRadius || 1; this.yRadius = yRadius || 1; this.aStartAngle = aStartAngle || 0; this.aEndAngle = aEndAngle || 2 * Math.PI; this.aClockwise = aClockwise || false; this.aRotation = aRotation || 0; } EllipseCurve.prototype = Object.create( Curve.prototype ); EllipseCurve.prototype.constructor = EllipseCurve; EllipseCurve.prototype.isEllipseCurve = true; EllipseCurve.prototype.getPoint = function ( t, optionalTarget ) { const point = optionalTarget || new Vector2(); const twoPi = Math.PI * 2; let deltaAngle = this.aEndAngle - this.aStartAngle; const samePoints = Math.abs( deltaAngle ) < Number.EPSILON; // ensures that deltaAngle is 0 .. 2 PI while ( deltaAngle < 0 ) deltaAngle += twoPi; while ( deltaAngle > twoPi ) deltaAngle -= twoPi; if ( deltaAngle < Number.EPSILON ) { if ( samePoints ) { deltaAngle = 0; } else { deltaAngle = twoPi; } } if ( this.aClockwise === true && ! samePoints ) { if ( deltaAngle === twoPi ) { deltaAngle = - twoPi; } else { deltaAngle = deltaAngle - twoPi; } } const angle = this.aStartAngle + t * deltaAngle; let x = this.aX + this.xRadius * Math.cos( angle ); let y = this.aY + this.yRadius * Math.sin( angle ); if ( this.aRotation !== 0 ) { const cos = Math.cos( this.aRotation ); const sin = Math.sin( this.aRotation ); const tx = x - this.aX; const ty = y - this.aY; // Rotate the point about the center of the ellipse. x = tx * cos - ty * sin + this.aX; y = tx * sin + ty * cos + this.aY; } return point.set( x, y ); }; EllipseCurve.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.aX = source.aX; this.aY = source.aY; this.xRadius = source.xRadius; this.yRadius = source.yRadius; this.aStartAngle = source.aStartAngle; this.aEndAngle = source.aEndAngle; this.aClockwise = source.aClockwise; this.aRotation = source.aRotation; return this; }; EllipseCurve.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.aX = this.aX; data.aY = this.aY; data.xRadius = this.xRadius; data.yRadius = this.yRadius; data.aStartAngle = this.aStartAngle; data.aEndAngle = this.aEndAngle; data.aClockwise = this.aClockwise; data.aRotation = this.aRotation; return data; }; EllipseCurve.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.aX = json.aX; this.aY = json.aY; this.xRadius = json.xRadius; this.yRadius = json.yRadius; this.aStartAngle = json.aStartAngle; this.aEndAngle = json.aEndAngle; this.aClockwise = json.aClockwise; this.aRotation = json.aRotation; return this; }; function ArcCurve( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { EllipseCurve.call( this, aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise ); this.type = 'ArcCurve'; } ArcCurve.prototype = Object.create( EllipseCurve.prototype ); ArcCurve.prototype.constructor = ArcCurve; ArcCurve.prototype.isArcCurve = true; /** * Centripetal CatmullRom Curve - which is useful for avoiding * cusps and self-intersections in non-uniform catmull rom curves. * http://www.cemyuksel.com/research/catmullrom_param/catmullrom.pdf * * curve.type accepts centripetal(default), chordal and catmullrom * curve.tension is used for catmullrom which defaults to 0.5 */ /* Based on an optimized c++ solution in - http://stackoverflow.com/questions/9489736/catmull-rom-curve-with-no-cusps-and-no-self-intersections/ - http://ideone.com/NoEbVM This CubicPoly class could be used for reusing some variables and calculations, but for three.js curve use, it could be possible inlined and flatten into a single function call which can be placed in CurveUtils. */ function CubicPoly() { let c0 = 0, c1 = 0, c2 = 0, c3 = 0; /* * Compute coefficients for a cubic polynomial * p(s) = c0 + c1*s + c2*s^2 + c3*s^3 * such that * p(0) = x0, p(1) = x1 * and * p'(0) = t0, p'(1) = t1. */ function init( x0, x1, t0, t1 ) { c0 = x0; c1 = t0; c2 = - 3 * x0 + 3 * x1 - 2 * t0 - t1; c3 = 2 * x0 - 2 * x1 + t0 + t1; } return { initCatmullRom: function ( x0, x1, x2, x3, tension ) { init( x1, x2, tension * ( x2 - x0 ), tension * ( x3 - x1 ) ); }, initNonuniformCatmullRom: function ( x0, x1, x2, x3, dt0, dt1, dt2 ) { // compute tangents when parameterized in [t1,t2] let t1 = ( x1 - x0 ) / dt0 - ( x2 - x0 ) / ( dt0 + dt1 ) + ( x2 - x1 ) / dt1; let t2 = ( x2 - x1 ) / dt1 - ( x3 - x1 ) / ( dt1 + dt2 ) + ( x3 - x2 ) / dt2; // rescale tangents for parametrization in [0,1] t1 *= dt1; t2 *= dt1; init( x1, x2, t1, t2 ); }, calc: function ( t ) { const t2 = t * t; const t3 = t2 * t; return c0 + c1 * t + c2 * t2 + c3 * t3; } }; } // const tmp = new Vector3(); const px = new CubicPoly(), py = new CubicPoly(), pz = new CubicPoly(); function CatmullRomCurve3( points = [], closed = false, curveType = 'centripetal', tension = 0.5 ) { Curve.call( this ); this.type = 'CatmullRomCurve3'; this.points = points; this.closed = closed; this.curveType = curveType; this.tension = tension; } CatmullRomCurve3.prototype = Object.create( Curve.prototype ); CatmullRomCurve3.prototype.constructor = CatmullRomCurve3; CatmullRomCurve3.prototype.isCatmullRomCurve3 = true; CatmullRomCurve3.prototype.getPoint = function ( t, optionalTarget = new Vector3() ) { const point = optionalTarget; const points = this.points; const l = points.length; const p = ( l - ( this.closed ? 0 : 1 ) ) * t; let intPoint = Math.floor( p ); let weight = p - intPoint; if ( this.closed ) { intPoint += intPoint > 0 ? 0 : ( Math.floor( Math.abs( intPoint ) / l ) + 1 ) * l; } else if ( weight === 0 && intPoint === l - 1 ) { intPoint = l - 2; weight = 1; } let p0, p3; // 4 points (p1 & p2 defined below) if ( this.closed || intPoint > 0 ) { p0 = points[ ( intPoint - 1 ) % l ]; } else { // extrapolate first point tmp.subVectors( points[ 0 ], points[ 1 ] ).add( points[ 0 ] ); p0 = tmp; } const p1 = points[ intPoint % l ]; const p2 = points[ ( intPoint + 1 ) % l ]; if ( this.closed || intPoint + 2 < l ) { p3 = points[ ( intPoint + 2 ) % l ]; } else { // extrapolate last point tmp.subVectors( points[ l - 1 ], points[ l - 2 ] ).add( points[ l - 1 ] ); p3 = tmp; } if ( this.curveType === 'centripetal' || this.curveType === 'chordal' ) { // init Centripetal / Chordal Catmull-Rom const pow = this.curveType === 'chordal' ? 0.5 : 0.25; let dt0 = Math.pow( p0.distanceToSquared( p1 ), pow ); let dt1 = Math.pow( p1.distanceToSquared( p2 ), pow ); let dt2 = Math.pow( p2.distanceToSquared( p3 ), pow ); // safety check for repeated points if ( dt1 < 1e-4 ) dt1 = 1.0; if ( dt0 < 1e-4 ) dt0 = dt1; if ( dt2 < 1e-4 ) dt2 = dt1; px.initNonuniformCatmullRom( p0.x, p1.x, p2.x, p3.x, dt0, dt1, dt2 ); py.initNonuniformCatmullRom( p0.y, p1.y, p2.y, p3.y, dt0, dt1, dt2 ); pz.initNonuniformCatmullRom( p0.z, p1.z, p2.z, p3.z, dt0, dt1, dt2 ); } else if ( this.curveType === 'catmullrom' ) { px.initCatmullRom( p0.x, p1.x, p2.x, p3.x, this.tension ); py.initCatmullRom( p0.y, p1.y, p2.y, p3.y, this.tension ); pz.initCatmullRom( p0.z, p1.z, p2.z, p3.z, this.tension ); } point.set( px.calc( weight ), py.calc( weight ), pz.calc( weight ) ); return point; }; CatmullRomCurve3.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.points = []; for ( let i = 0, l = source.points.length; i < l; i ++ ) { const point = source.points[ i ]; this.points.push( point.clone() ); } this.closed = source.closed; this.curveType = source.curveType; this.tension = source.tension; return this; }; CatmullRomCurve3.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.points = []; for ( let i = 0, l = this.points.length; i < l; i ++ ) { const point = this.points[ i ]; data.points.push( point.toArray() ); } data.closed = this.closed; data.curveType = this.curveType; data.tension = this.tension; return data; }; CatmullRomCurve3.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.points = []; for ( let i = 0, l = json.points.length; i < l; i ++ ) { const point = json.points[ i ]; this.points.push( new Vector3().fromArray( point ) ); } this.closed = json.closed; this.curveType = json.curveType; this.tension = json.tension; return this; }; /** * Bezier Curves formulas obtained from * http://en.wikipedia.org/wiki/Bézier_curve */ function CatmullRom( t, p0, p1, p2, p3 ) { const v0 = ( p2 - p0 ) * 0.5; const v1 = ( p3 - p1 ) * 0.5; const t2 = t * t; const t3 = t * t2; return ( 2 * p1 - 2 * p2 + v0 + v1 ) * t3 + ( - 3 * p1 + 3 * p2 - 2 * v0 - v1 ) * t2 + v0 * t + p1; } // function QuadraticBezierP0( t, p ) { const k = 1 - t; return k * k * p; } function QuadraticBezierP1( t, p ) { return 2 * ( 1 - t ) * t * p; } function QuadraticBezierP2( t, p ) { return t * t * p; } function QuadraticBezier( t, p0, p1, p2 ) { return QuadraticBezierP0( t, p0 ) + QuadraticBezierP1( t, p1 ) + QuadraticBezierP2( t, p2 ); } // function CubicBezierP0( t, p ) { const k = 1 - t; return k * k * k * p; } function CubicBezierP1( t, p ) { const k = 1 - t; return 3 * k * k * t * p; } function CubicBezierP2( t, p ) { return 3 * ( 1 - t ) * t * t * p; } function CubicBezierP3( t, p ) { return t * t * t * p; } function CubicBezier( t, p0, p1, p2, p3 ) { return CubicBezierP0( t, p0 ) + CubicBezierP1( t, p1 ) + CubicBezierP2( t, p2 ) + CubicBezierP3( t, p3 ); } function CubicBezierCurve( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2(), v3 = new Vector2() ) { Curve.call( this ); this.type = 'CubicBezierCurve'; this.v0 = v0; this.v1 = v1; this.v2 = v2; this.v3 = v3; } CubicBezierCurve.prototype = Object.create( Curve.prototype ); CubicBezierCurve.prototype.constructor = CubicBezierCurve; CubicBezierCurve.prototype.isCubicBezierCurve = true; CubicBezierCurve.prototype.getPoint = function ( t, optionalTarget = new Vector2() ) { const point = optionalTarget; const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3; point.set( CubicBezier( t, v0.x, v1.x, v2.x, v3.x ), CubicBezier( t, v0.y, v1.y, v2.y, v3.y ) ); return point; }; CubicBezierCurve.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.v0.copy( source.v0 ); this.v1.copy( source.v1 ); this.v2.copy( source.v2 ); this.v3.copy( source.v3 ); return this; }; CubicBezierCurve.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.v0 = this.v0.toArray(); data.v1 = this.v1.toArray(); data.v2 = this.v2.toArray(); data.v3 = this.v3.toArray(); return data; }; CubicBezierCurve.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.v0.fromArray( json.v0 ); this.v1.fromArray( json.v1 ); this.v2.fromArray( json.v2 ); this.v3.fromArray( json.v3 ); return this; }; function CubicBezierCurve3( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3(), v3 = new Vector3() ) { Curve.call( this ); this.type = 'CubicBezierCurve3'; this.v0 = v0; this.v1 = v1; this.v2 = v2; this.v3 = v3; } CubicBezierCurve3.prototype = Object.create( Curve.prototype ); CubicBezierCurve3.prototype.constructor = CubicBezierCurve3; CubicBezierCurve3.prototype.isCubicBezierCurve3 = true; CubicBezierCurve3.prototype.getPoint = function ( t, optionalTarget = new Vector3() ) { const point = optionalTarget; const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3; point.set( CubicBezier( t, v0.x, v1.x, v2.x, v3.x ), CubicBezier( t, v0.y, v1.y, v2.y, v3.y ), CubicBezier( t, v0.z, v1.z, v2.z, v3.z ) ); return point; }; CubicBezierCurve3.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.v0.copy( source.v0 ); this.v1.copy( source.v1 ); this.v2.copy( source.v2 ); this.v3.copy( source.v3 ); return this; }; CubicBezierCurve3.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.v0 = this.v0.toArray(); data.v1 = this.v1.toArray(); data.v2 = this.v2.toArray(); data.v3 = this.v3.toArray(); return data; }; CubicBezierCurve3.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.v0.fromArray( json.v0 ); this.v1.fromArray( json.v1 ); this.v2.fromArray( json.v2 ); this.v3.fromArray( json.v3 ); return this; }; function LineCurve( v1 = new Vector2(), v2 = new Vector2() ) { Curve.call( this ); this.type = 'LineCurve'; this.v1 = v1; this.v2 = v2; } LineCurve.prototype = Object.create( Curve.prototype ); LineCurve.prototype.constructor = LineCurve; LineCurve.prototype.isLineCurve = true; LineCurve.prototype.getPoint = function ( t, optionalTarget = new Vector2() ) { const point = optionalTarget; if ( t === 1 ) { point.copy( this.v2 ); } else { point.copy( this.v2 ).sub( this.v1 ); point.multiplyScalar( t ).add( this.v1 ); } return point; }; // Line curve is linear, so we can overwrite default getPointAt LineCurve.prototype.getPointAt = function ( u, optionalTarget ) { return this.getPoint( u, optionalTarget ); }; LineCurve.prototype.getTangent = function ( t, optionalTarget ) { const tangent = optionalTarget || new Vector2(); tangent.copy( this.v2 ).sub( this.v1 ).normalize(); return tangent; }; LineCurve.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.v1.copy( source.v1 ); this.v2.copy( source.v2 ); return this; }; LineCurve.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.v1 = this.v1.toArray(); data.v2 = this.v2.toArray(); return data; }; LineCurve.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.v1.fromArray( json.v1 ); this.v2.fromArray( json.v2 ); return this; }; function LineCurve3( v1 = new Vector3(), v2 = new Vector3() ) { Curve.call( this ); this.type = 'LineCurve3'; this.v1 = v1; this.v2 = v2; } LineCurve3.prototype = Object.create( Curve.prototype ); LineCurve3.prototype.constructor = LineCurve3; LineCurve3.prototype.isLineCurve3 = true; LineCurve3.prototype.getPoint = function ( t, optionalTarget = new Vector3() ) { const point = optionalTarget; if ( t === 1 ) { point.copy( this.v2 ); } else { point.copy( this.v2 ).sub( this.v1 ); point.multiplyScalar( t ).add( this.v1 ); } return point; }; // Line curve is linear, so we can overwrite default getPointAt LineCurve3.prototype.getPointAt = function ( u, optionalTarget ) { return this.getPoint( u, optionalTarget ); }; LineCurve3.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.v1.copy( source.v1 ); this.v2.copy( source.v2 ); return this; }; LineCurve3.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.v1 = this.v1.toArray(); data.v2 = this.v2.toArray(); return data; }; LineCurve3.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.v1.fromArray( json.v1 ); this.v2.fromArray( json.v2 ); return this; }; function QuadraticBezierCurve( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2() ) { Curve.call( this ); this.type = 'QuadraticBezierCurve'; this.v0 = v0; this.v1 = v1; this.v2 = v2; } QuadraticBezierCurve.prototype = Object.create( Curve.prototype ); QuadraticBezierCurve.prototype.constructor = QuadraticBezierCurve; QuadraticBezierCurve.prototype.isQuadraticBezierCurve = true; QuadraticBezierCurve.prototype.getPoint = function ( t, optionalTarget = new Vector2() ) { const point = optionalTarget; const v0 = this.v0, v1 = this.v1, v2 = this.v2; point.set( QuadraticBezier( t, v0.x, v1.x, v2.x ), QuadraticBezier( t, v0.y, v1.y, v2.y ) ); return point; }; QuadraticBezierCurve.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.v0.copy( source.v0 ); this.v1.copy( source.v1 ); this.v2.copy( source.v2 ); return this; }; QuadraticBezierCurve.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.v0 = this.v0.toArray(); data.v1 = this.v1.toArray(); data.v2 = this.v2.toArray(); return data; }; QuadraticBezierCurve.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.v0.fromArray( json.v0 ); this.v1.fromArray( json.v1 ); this.v2.fromArray( json.v2 ); return this; }; function QuadraticBezierCurve3( v0 = new Vector3(), v1 = new Vector3(), v2 = new Vector3() ) { Curve.call( this ); this.type = 'QuadraticBezierCurve3'; this.v0 = v0; this.v1 = v1; this.v2 = v2; } QuadraticBezierCurve3.prototype = Object.create( Curve.prototype ); QuadraticBezierCurve3.prototype.constructor = QuadraticBezierCurve3; QuadraticBezierCurve3.prototype.isQuadraticBezierCurve3 = true; QuadraticBezierCurve3.prototype.getPoint = function ( t, optionalTarget = new Vector3() ) { const point = optionalTarget; const v0 = this.v0, v1 = this.v1, v2 = this.v2; point.set( QuadraticBezier( t, v0.x, v1.x, v2.x ), QuadraticBezier( t, v0.y, v1.y, v2.y ), QuadraticBezier( t, v0.z, v1.z, v2.z ) ); return point; }; QuadraticBezierCurve3.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.v0.copy( source.v0 ); this.v1.copy( source.v1 ); this.v2.copy( source.v2 ); return this; }; QuadraticBezierCurve3.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.v0 = this.v0.toArray(); data.v1 = this.v1.toArray(); data.v2 = this.v2.toArray(); return data; }; QuadraticBezierCurve3.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.v0.fromArray( json.v0 ); this.v1.fromArray( json.v1 ); this.v2.fromArray( json.v2 ); return this; }; function SplineCurve( points = [] ) { Curve.call( this ); this.type = 'SplineCurve'; this.points = points; } SplineCurve.prototype = Object.create( Curve.prototype ); SplineCurve.prototype.constructor = SplineCurve; SplineCurve.prototype.isSplineCurve = true; SplineCurve.prototype.getPoint = function ( t, optionalTarget = new Vector2() ) { const point = optionalTarget; const points = this.points; const p = ( points.length - 1 ) * t; const intPoint = Math.floor( p ); const weight = p - intPoint; const p0 = points[ intPoint === 0 ? intPoint : intPoint - 1 ]; const p1 = points[ intPoint ]; const p2 = points[ intPoint > points.length - 2 ? points.length - 1 : intPoint + 1 ]; const p3 = points[ intPoint > points.length - 3 ? points.length - 1 : intPoint + 2 ]; point.set( CatmullRom( weight, p0.x, p1.x, p2.x, p3.x ), CatmullRom( weight, p0.y, p1.y, p2.y, p3.y ) ); return point; }; SplineCurve.prototype.copy = function ( source ) { Curve.prototype.copy.call( this, source ); this.points = []; for ( let i = 0, l = source.points.length; i < l; i ++ ) { const point = source.points[ i ]; this.points.push( point.clone() ); } return this; }; SplineCurve.prototype.toJSON = function () { const data = Curve.prototype.toJSON.call( this ); data.points = []; for ( let i = 0, l = this.points.length; i < l; i ++ ) { const point = this.points[ i ]; data.points.push( point.toArray() ); } return data; }; SplineCurve.prototype.fromJSON = function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.points = []; for ( let i = 0, l = json.points.length; i < l; i ++ ) { const point = json.points[ i ]; this.points.push( new Vector2().fromArray( point ) ); } return this; }; var Curves = /*#__PURE__*/Object.freeze({ __proto__: null, ArcCurve: ArcCurve, CatmullRomCurve3: CatmullRomCurve3, CubicBezierCurve: CubicBezierCurve, CubicBezierCurve3: CubicBezierCurve3, EllipseCurve: EllipseCurve, LineCurve: LineCurve, LineCurve3: LineCurve3, QuadraticBezierCurve: QuadraticBezierCurve, QuadraticBezierCurve3: QuadraticBezierCurve3, SplineCurve: SplineCurve }); /************************************************************** * Curved Path - a curve path is simply a array of connected * curves, but retains the api of a curve **************************************************************/ function CurvePath() { Curve.call( this ); this.type = 'CurvePath'; this.curves = []; this.autoClose = false; // Automatically closes the path } CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), { constructor: CurvePath, add: function ( curve ) { this.curves.push( curve ); }, closePath: function () { // Add a line curve if start and end of lines are not connected const startPoint = this.curves[ 0 ].getPoint( 0 ); const endPoint = this.curves[ this.curves.length - 1 ].getPoint( 1 ); if ( ! startPoint.equals( endPoint ) ) { this.curves.push( new LineCurve( endPoint, startPoint ) ); } }, // To get accurate point with reference to // entire path distance at time t, // following has to be done: // 1. Length of each sub path have to be known // 2. Locate and identify type of curve // 3. Get t for the curve // 4. Return curve.getPointAt(t') getPoint: function ( t ) { const d = t * this.getLength(); const curveLengths = this.getCurveLengths(); let i = 0; // To think about boundaries points. while ( i < curveLengths.length ) { if ( curveLengths[ i ] >= d ) { const diff = curveLengths[ i ] - d; const curve = this.curves[ i ]; const segmentLength = curve.getLength(); const u = segmentLength === 0 ? 0 : 1 - diff / segmentLength; return curve.getPointAt( u ); } i ++; } return null; // loop where sum != 0, sum > d , sum+1 1 && ! points[ points.length - 1 ].equals( points[ 0 ] ) ) { points.push( points[ 0 ] ); } return points; }, copy: function ( source ) { Curve.prototype.copy.call( this, source ); this.curves = []; for ( let i = 0, l = source.curves.length; i < l; i ++ ) { const curve = source.curves[ i ]; this.curves.push( curve.clone() ); } this.autoClose = source.autoClose; return this; }, toJSON: function () { const data = Curve.prototype.toJSON.call( this ); data.autoClose = this.autoClose; data.curves = []; for ( let i = 0, l = this.curves.length; i < l; i ++ ) { const curve = this.curves[ i ]; data.curves.push( curve.toJSON() ); } return data; }, fromJSON: function ( json ) { Curve.prototype.fromJSON.call( this, json ); this.autoClose = json.autoClose; this.curves = []; for ( let i = 0, l = json.curves.length; i < l; i ++ ) { const curve = json.curves[ i ]; this.curves.push( new Curves[ curve.type ]().fromJSON( curve ) ); } return this; } } ); function Path( points ) { CurvePath.call( this ); this.type = 'Path'; this.currentPoint = new Vector2(); if ( points ) { this.setFromPoints( points ); } } Path.prototype = Object.assign( Object.create( CurvePath.prototype ), { constructor: Path, setFromPoints: function ( points ) { this.moveTo( points[ 0 ].x, points[ 0 ].y ); for ( let i = 1, l = points.length; i < l; i ++ ) { this.lineTo( points[ i ].x, points[ i ].y ); } return this; }, moveTo: function ( x, y ) { this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying? return this; }, lineTo: function ( x, y ) { const curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) ); this.curves.push( curve ); this.currentPoint.set( x, y ); return this; }, quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) { const curve = new QuadraticBezierCurve( this.currentPoint.clone(), new Vector2( aCPx, aCPy ), new Vector2( aX, aY ) ); this.curves.push( curve ); this.currentPoint.set( aX, aY ); return this; }, bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) { const curve = new CubicBezierCurve( this.currentPoint.clone(), new Vector2( aCP1x, aCP1y ), new Vector2( aCP2x, aCP2y ), new Vector2( aX, aY ) ); this.curves.push( curve ); this.currentPoint.set( aX, aY ); return this; }, splineThru: function ( pts /*Array of Vector*/ ) { const npts = [ this.currentPoint.clone() ].concat( pts ); const curve = new SplineCurve( npts ); this.curves.push( curve ); this.currentPoint.copy( pts[ pts.length - 1 ] ); return this; }, arc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { const x0 = this.currentPoint.x; const y0 = this.currentPoint.y; this.absarc( aX + x0, aY + y0, aRadius, aStartAngle, aEndAngle, aClockwise ); return this; }, absarc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) { this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise ); return this; }, ellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) { const x0 = this.currentPoint.x; const y0 = this.currentPoint.y; this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ); return this; }, absellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) { const curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ); if ( this.curves.length > 0 ) { // if a previous curve is present, attempt to join const firstPoint = curve.getPoint( 0 ); if ( ! firstPoint.equals( this.currentPoint ) ) { this.lineTo( firstPoint.x, firstPoint.y ); } } this.curves.push( curve ); const lastPoint = curve.getPoint( 1 ); this.currentPoint.copy( lastPoint ); return this; }, copy: function ( source ) { CurvePath.prototype.copy.call( this, source ); this.currentPoint.copy( source.currentPoint ); return this; }, toJSON: function () { const data = CurvePath.prototype.toJSON.call( this ); data.currentPoint = this.currentPoint.toArray(); return data; }, fromJSON: function ( json ) { CurvePath.prototype.fromJSON.call( this, json ); this.currentPoint.fromArray( json.currentPoint ); return this; } } ); function Shape( points ) { Path.call( this, points ); this.uuid = MathUtils.generateUUID(); this.type = 'Shape'; this.holes = []; } Shape.prototype = Object.assign( Object.create( Path.prototype ), { constructor: Shape, getPointsHoles: function ( divisions ) { const holesPts = []; for ( let i = 0, l = this.holes.length; i < l; i ++ ) { holesPts[ i ] = this.holes[ i ].getPoints( divisions ); } return holesPts; }, // get points of shape and holes (keypoints based on segments parameter) extractPoints: function ( divisions ) { return { shape: this.getPoints( divisions ), holes: this.getPointsHoles( divisions ) }; }, copy: function ( source ) { Path.prototype.copy.call( this, source ); this.holes = []; for ( let i = 0, l = source.holes.length; i < l; i ++ ) { const hole = source.holes[ i ]; this.holes.push( hole.clone() ); } return this; }, toJSON: function () { const data = Path.prototype.toJSON.call( this ); data.uuid = this.uuid; data.holes = []; for ( let i = 0, l = this.holes.length; i < l; i ++ ) { const hole = this.holes[ i ]; data.holes.push( hole.toJSON() ); } return data; }, fromJSON: function ( json ) { Path.prototype.fromJSON.call( this, json ); this.uuid = json.uuid; this.holes = []; for ( let i = 0, l = json.holes.length; i < l; i ++ ) { const hole = json.holes[ i ]; this.holes.push( new Path().fromJSON( hole ) ); } return this; } } ); function Light( color, intensity = 1 ) { Object3D.call( this ); this.type = 'Light'; this.color = new Color( color ); this.intensity = intensity; } Light.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: Light, isLight: true, copy: function ( source ) { Object3D.prototype.copy.call( this, source ); this.color.copy( source.color ); this.intensity = source.intensity; return this; }, toJSON: function ( meta ) { const data = Object3D.prototype.toJSON.call( this, meta ); data.object.color = this.color.getHex(); data.object.intensity = this.intensity; if ( this.groundColor !== undefined ) data.object.groundColor = this.groundColor.getHex(); if ( this.distance !== undefined ) data.object.distance = this.distance; if ( this.angle !== undefined ) data.object.angle = this.angle; if ( this.decay !== undefined ) data.object.decay = this.decay; if ( this.penumbra !== undefined ) data.object.penumbra = this.penumbra; if ( this.shadow !== undefined ) data.object.shadow = this.shadow.toJSON(); return data; } } ); function HemisphereLight( skyColor, groundColor, intensity ) { Light.call( this, skyColor, intensity ); this.type = 'HemisphereLight'; this.position.copy( Object3D.DefaultUp ); this.updateMatrix(); this.groundColor = new Color( groundColor ); } HemisphereLight.prototype = Object.assign( Object.create( Light.prototype ), { constructor: HemisphereLight, isHemisphereLight: true, copy: function ( source ) { Light.prototype.copy.call( this, source ); this.groundColor.copy( source.groundColor ); return this; } } ); function LightShadow( camera ) { this.camera = camera; this.bias = 0; this.normalBias = 0; this.radius = 1; this.mapSize = new Vector2( 512, 512 ); this.map = null; this.mapPass = null; this.matrix = new Matrix4(); this.autoUpdate = true; this.needsUpdate = false; this._frustum = new Frustum(); this._frameExtents = new Vector2( 1, 1 ); this._viewportCount = 1; this._viewports = [ new Vector4( 0, 0, 1, 1 ) ]; } Object.assign( LightShadow.prototype, { _projScreenMatrix: new Matrix4(), _lightPositionWorld: new Vector3(), _lookTarget: new Vector3(), getViewportCount: function () { return this._viewportCount; }, getFrustum: function () { return this._frustum; }, updateMatrices: function ( light ) { const shadowCamera = this.camera, shadowMatrix = this.matrix, projScreenMatrix = this._projScreenMatrix, lookTarget = this._lookTarget, lightPositionWorld = this._lightPositionWorld; lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); shadowCamera.position.copy( lightPositionWorld ); lookTarget.setFromMatrixPosition( light.target.matrixWorld ); shadowCamera.lookAt( lookTarget ); shadowCamera.updateMatrixWorld(); projScreenMatrix.multiplyMatrices( shadowCamera.projectionMatrix, shadowCamera.matrixWorldInverse ); this._frustum.setFromProjectionMatrix( projScreenMatrix ); shadowMatrix.set( 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0 ); shadowMatrix.multiply( shadowCamera.projectionMatrix ); shadowMatrix.multiply( shadowCamera.matrixWorldInverse ); }, getViewport: function ( viewportIndex ) { return this._viewports[ viewportIndex ]; }, getFrameExtents: function () { return this._frameExtents; }, copy: function ( source ) { this.camera = source.camera.clone(); this.bias = source.bias; this.radius = source.radius; this.mapSize.copy( source.mapSize ); return this; }, clone: function () { return new this.constructor().copy( this ); }, toJSON: function () { const object = {}; if ( this.bias !== 0 ) object.bias = this.bias; if ( this.normalBias !== 0 ) object.normalBias = this.normalBias; if ( this.radius !== 1 ) object.radius = this.radius; if ( this.mapSize.x !== 512 || this.mapSize.y !== 512 ) object.mapSize = this.mapSize.toArray(); object.camera = this.camera.toJSON( false ).object; delete object.camera.matrix; return object; } } ); function SpotLightShadow() { LightShadow.call( this, new PerspectiveCamera( 50, 1, 0.5, 500 ) ); this.focus = 1; } SpotLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), { constructor: SpotLightShadow, isSpotLightShadow: true, updateMatrices: function ( light ) { const camera = this.camera; const fov = MathUtils.RAD2DEG * 2 * light.angle * this.focus; const aspect = this.mapSize.width / this.mapSize.height; const far = light.distance || camera.far; if ( fov !== camera.fov || aspect !== camera.aspect || far !== camera.far ) { camera.fov = fov; camera.aspect = aspect; camera.far = far; camera.updateProjectionMatrix(); } LightShadow.prototype.updateMatrices.call( this, light ); } } ); function SpotLight( color, intensity, distance, angle, penumbra, decay ) { Light.call( this, color, intensity ); this.type = 'SpotLight'; this.position.copy( Object3D.DefaultUp ); this.updateMatrix(); this.target = new Object3D(); Object.defineProperty( this, 'power', { get: function () { // intensity = power per solid angle. // ref: equation (17) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf return this.intensity * Math.PI; }, set: function ( power ) { // intensity = power per solid angle. // ref: equation (17) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf this.intensity = power / Math.PI; } } ); this.distance = ( distance !== undefined ) ? distance : 0; this.angle = ( angle !== undefined ) ? angle : Math.PI / 3; this.penumbra = ( penumbra !== undefined ) ? penumbra : 0; this.decay = ( decay !== undefined ) ? decay : 1; // for physically correct lights, should be 2. this.shadow = new SpotLightShadow(); } SpotLight.prototype = Object.assign( Object.create( Light.prototype ), { constructor: SpotLight, isSpotLight: true, copy: function ( source ) { Light.prototype.copy.call( this, source ); this.distance = source.distance; this.angle = source.angle; this.penumbra = source.penumbra; this.decay = source.decay; this.target = source.target.clone(); this.shadow = source.shadow.clone(); return this; } } ); function PointLightShadow() { LightShadow.call( this, new PerspectiveCamera( 90, 1, 0.5, 500 ) ); this._frameExtents = new Vector2( 4, 2 ); this._viewportCount = 6; this._viewports = [ // These viewports map a cube-map onto a 2D texture with the // following orientation: // // xzXZ // y Y // // X - Positive x direction // x - Negative x direction // Y - Positive y direction // y - Negative y direction // Z - Positive z direction // z - Negative z direction // positive X new Vector4( 2, 1, 1, 1 ), // negative X new Vector4( 0, 1, 1, 1 ), // positive Z new Vector4( 3, 1, 1, 1 ), // negative Z new Vector4( 1, 1, 1, 1 ), // positive Y new Vector4( 3, 0, 1, 1 ), // negative Y new Vector4( 1, 0, 1, 1 ) ]; this._cubeDirections = [ new Vector3( 1, 0, 0 ), new Vector3( - 1, 0, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 ), new Vector3( 0, 1, 0 ), new Vector3( 0, - 1, 0 ) ]; this._cubeUps = [ new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 1, 0 ), new Vector3( 0, 0, 1 ), new Vector3( 0, 0, - 1 ) ]; } PointLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), { constructor: PointLightShadow, isPointLightShadow: true, updateMatrices: function ( light, viewportIndex = 0 ) { const camera = this.camera, shadowMatrix = this.matrix, lightPositionWorld = this._lightPositionWorld, lookTarget = this._lookTarget, projScreenMatrix = this._projScreenMatrix; lightPositionWorld.setFromMatrixPosition( light.matrixWorld ); camera.position.copy( lightPositionWorld ); lookTarget.copy( camera.position ); lookTarget.add( this._cubeDirections[ viewportIndex ] ); camera.up.copy( this._cubeUps[ viewportIndex ] ); camera.lookAt( lookTarget ); camera.updateMatrixWorld(); shadowMatrix.makeTranslation( - lightPositionWorld.x, - lightPositionWorld.y, - lightPositionWorld.z ); projScreenMatrix.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ); this._frustum.setFromProjectionMatrix( projScreenMatrix ); } } ); function PointLight( color, intensity, distance, decay ) { Light.call( this, color, intensity ); this.type = 'PointLight'; Object.defineProperty( this, 'power', { get: function () { // intensity = power per solid angle. // ref: equation (15) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf return this.intensity * 4 * Math.PI; }, set: function ( power ) { // intensity = power per solid angle. // ref: equation (15) from https://seblagarde.files.wordpress.com/2015/07/course_notes_moving_frostbite_to_pbr_v32.pdf this.intensity = power / ( 4 * Math.PI ); } } ); this.distance = ( distance !== undefined ) ? distance : 0; this.decay = ( decay !== undefined ) ? decay : 1; // for physically correct lights, should be 2. this.shadow = new PointLightShadow(); } PointLight.prototype = Object.assign( Object.create( Light.prototype ), { constructor: PointLight, isPointLight: true, copy: function ( source ) { Light.prototype.copy.call( this, source ); this.distance = source.distance; this.decay = source.decay; this.shadow = source.shadow.clone(); return this; } } ); function OrthographicCamera( left = - 1, right = 1, top = 1, bottom = - 1, near = 0.1, far = 2000 ) { Camera.call( this ); this.type = 'OrthographicCamera'; this.zoom = 1; this.view = null; this.left = left; this.right = right; this.top = top; this.bottom = bottom; this.near = near; this.far = far; this.updateProjectionMatrix(); } OrthographicCamera.prototype = Object.assign( Object.create( Camera.prototype ), { constructor: OrthographicCamera, isOrthographicCamera: true, copy: function ( source, recursive ) { Camera.prototype.copy.call( this, source, recursive ); this.left = source.left; this.right = source.right; this.top = source.top; this.bottom = source.bottom; this.near = source.near; this.far = source.far; this.zoom = source.zoom; this.view = source.view === null ? null : Object.assign( {}, source.view ); return this; }, setViewOffset: function ( fullWidth, fullHeight, x, y, width, height ) { if ( this.view === null ) { this.view = { enabled: true, fullWidth: 1, fullHeight: 1, offsetX: 0, offsetY: 0, width: 1, height: 1 }; } this.view.enabled = true; this.view.fullWidth = fullWidth; this.view.fullHeight = fullHeight; this.view.offsetX = x; this.view.offsetY = y; this.view.width = width; this.view.height = height; this.updateProjectionMatrix(); }, clearViewOffset: function () { if ( this.view !== null ) { this.view.enabled = false; } this.updateProjectionMatrix(); }, updateProjectionMatrix: function () { const dx = ( this.right - this.left ) / ( 2 * this.zoom ); const dy = ( this.top - this.bottom ) / ( 2 * this.zoom ); const cx = ( this.right + this.left ) / 2; const cy = ( this.top + this.bottom ) / 2; let left = cx - dx; let right = cx + dx; let top = cy + dy; let bottom = cy - dy; if ( this.view !== null && this.view.enabled ) { const scaleW = ( this.right - this.left ) / this.view.fullWidth / this.zoom; const scaleH = ( this.top - this.bottom ) / this.view.fullHeight / this.zoom; left += scaleW * this.view.offsetX; right = left + scaleW * this.view.width; top -= scaleH * this.view.offsetY; bottom = top - scaleH * this.view.height; } this.projectionMatrix.makeOrthographic( left, right, top, bottom, this.near, this.far ); this.projectionMatrixInverse.copy( this.projectionMatrix ).invert(); }, toJSON: function ( meta ) { const data = Object3D.prototype.toJSON.call( this, meta ); data.object.zoom = this.zoom; data.object.left = this.left; data.object.right = this.right; data.object.top = this.top; data.object.bottom = this.bottom; data.object.near = this.near; data.object.far = this.far; if ( this.view !== null ) data.object.view = Object.assign( {}, this.view ); return data; } } ); function DirectionalLightShadow() { LightShadow.call( this, new OrthographicCamera( - 5, 5, 5, - 5, 0.5, 500 ) ); } DirectionalLightShadow.prototype = Object.assign( Object.create( LightShadow.prototype ), { constructor: DirectionalLightShadow, isDirectionalLightShadow: true, updateMatrices: function ( light ) { LightShadow.prototype.updateMatrices.call( this, light ); } } ); function DirectionalLight( color, intensity ) { Light.call( this, color, intensity ); this.type = 'DirectionalLight'; this.position.copy( Object3D.DefaultUp ); this.updateMatrix(); this.target = new Object3D(); this.shadow = new DirectionalLightShadow(); } DirectionalLight.prototype = Object.assign( Object.create( Light.prototype ), { constructor: DirectionalLight, isDirectionalLight: true, copy: function ( source ) { Light.prototype.copy.call( this, source ); this.target = source.target.clone(); this.shadow = source.shadow.clone(); return this; } } ); function AmbientLight( color, intensity ) { Light.call( this, color, intensity ); this.type = 'AmbientLight'; } AmbientLight.prototype = Object.assign( Object.create( Light.prototype ), { constructor: AmbientLight, isAmbientLight: true } ); function RectAreaLight( color, intensity, width, height ) { Light.call( this, color, intensity ); this.type = 'RectAreaLight'; this.width = ( width !== undefined ) ? width : 10; this.height = ( height !== undefined ) ? height : 10; } RectAreaLight.prototype = Object.assign( Object.create( Light.prototype ), { constructor: RectAreaLight, isRectAreaLight: true, copy: function ( source ) { Light.prototype.copy.call( this, source ); this.width = source.width; this.height = source.height; return this; }, toJSON: function ( meta ) { const data = Light.prototype.toJSON.call( this, meta ); data.object.width = this.width; data.object.height = this.height; return data; } } ); /** * Primary reference: * https://graphics.stanford.edu/papers/envmap/envmap.pdf * * Secondary reference: * https://www.ppsloan.org/publications/StupidSH36.pdf */ // 3-band SH defined by 9 coefficients class SphericalHarmonics3 { constructor() { Object.defineProperty( this, 'isSphericalHarmonics3', { value: true } ); this.coefficients = []; for ( let i = 0; i < 9; i ++ ) { this.coefficients.push( new Vector3() ); } } set( coefficients ) { for ( let i = 0; i < 9; i ++ ) { this.coefficients[ i ].copy( coefficients[ i ] ); } return this; } zero() { for ( let i = 0; i < 9; i ++ ) { this.coefficients[ i ].set( 0, 0, 0 ); } return this; } // get the radiance in the direction of the normal // target is a Vector3 getAt( normal, target ) { // normal is assumed to be unit length const x = normal.x, y = normal.y, z = normal.z; const coeff = this.coefficients; // band 0 target.copy( coeff[ 0 ] ).multiplyScalar( 0.282095 ); // band 1 target.addScaledVector( coeff[ 1 ], 0.488603 * y ); target.addScaledVector( coeff[ 2 ], 0.488603 * z ); target.addScaledVector( coeff[ 3 ], 0.488603 * x ); // band 2 target.addScaledVector( coeff[ 4 ], 1.092548 * ( x * y ) ); target.addScaledVector( coeff[ 5 ], 1.092548 * ( y * z ) ); target.addScaledVector( coeff[ 6 ], 0.315392 * ( 3.0 * z * z - 1.0 ) ); target.addScaledVector( coeff[ 7 ], 1.092548 * ( x * z ) ); target.addScaledVector( coeff[ 8 ], 0.546274 * ( x * x - y * y ) ); return target; } // get the irradiance (radiance convolved with cosine lobe) in the direction of the normal // target is a Vector3 // https://graphics.stanford.edu/papers/envmap/envmap.pdf getIrradianceAt( normal, target ) { // normal is assumed to be unit length const x = normal.x, y = normal.y, z = normal.z; const coeff = this.coefficients; // band 0 target.copy( coeff[ 0 ] ).multiplyScalar( 0.886227 ); // π * 0.282095 // band 1 target.addScaledVector( coeff[ 1 ], 2.0 * 0.511664 * y ); // ( 2 * π / 3 ) * 0.488603 target.addScaledVector( coeff[ 2 ], 2.0 * 0.511664 * z ); target.addScaledVector( coeff[ 3 ], 2.0 * 0.511664 * x ); // band 2 target.addScaledVector( coeff[ 4 ], 2.0 * 0.429043 * x * y ); // ( π / 4 ) * 1.092548 target.addScaledVector( coeff[ 5 ], 2.0 * 0.429043 * y * z ); target.addScaledVector( coeff[ 6 ], 0.743125 * z * z - 0.247708 ); // ( π / 4 ) * 0.315392 * 3 target.addScaledVector( coeff[ 7 ], 2.0 * 0.429043 * x * z ); target.addScaledVector( coeff[ 8 ], 0.429043 * ( x * x - y * y ) ); // ( π / 4 ) * 0.546274 return target; } add( sh ) { for ( let i = 0; i < 9; i ++ ) { this.coefficients[ i ].add( sh.coefficients[ i ] ); } return this; } addScaledSH( sh, s ) { for ( let i = 0; i < 9; i ++ ) { this.coefficients[ i ].addScaledVector( sh.coefficients[ i ], s ); } return this; } scale( s ) { for ( let i = 0; i < 9; i ++ ) { this.coefficients[ i ].multiplyScalar( s ); } return this; } lerp( sh, alpha ) { for ( let i = 0; i < 9; i ++ ) { this.coefficients[ i ].lerp( sh.coefficients[ i ], alpha ); } return this; } equals( sh ) { for ( let i = 0; i < 9; i ++ ) { if ( ! this.coefficients[ i ].equals( sh.coefficients[ i ] ) ) { return false; } } return true; } copy( sh ) { return this.set( sh.coefficients ); } clone() { return new this.constructor().copy( this ); } fromArray( array, offset = 0 ) { const coefficients = this.coefficients; for ( let i = 0; i < 9; i ++ ) { coefficients[ i ].fromArray( array, offset + ( i * 3 ) ); } return this; } toArray( array = [], offset = 0 ) { const coefficients = this.coefficients; for ( let i = 0; i < 9; i ++ ) { coefficients[ i ].toArray( array, offset + ( i * 3 ) ); } return array; } // evaluate the basis functions // shBasis is an Array[ 9 ] static getBasisAt( normal, shBasis ) { // normal is assumed to be unit length const x = normal.x, y = normal.y, z = normal.z; // band 0 shBasis[ 0 ] = 0.282095; // band 1 shBasis[ 1 ] = 0.488603 * y; shBasis[ 2 ] = 0.488603 * z; shBasis[ 3 ] = 0.488603 * x; // band 2 shBasis[ 4 ] = 1.092548 * x * y; shBasis[ 5 ] = 1.092548 * y * z; shBasis[ 6 ] = 0.315392 * ( 3 * z * z - 1 ); shBasis[ 7 ] = 1.092548 * x * z; shBasis[ 8 ] = 0.546274 * ( x * x - y * y ); } } function LightProbe( sh, intensity ) { Light.call( this, undefined, intensity ); this.type = 'LightProbe'; this.sh = ( sh !== undefined ) ? sh : new SphericalHarmonics3(); } LightProbe.prototype = Object.assign( Object.create( Light.prototype ), { constructor: LightProbe, isLightProbe: true, copy: function ( source ) { Light.prototype.copy.call( this, source ); this.sh.copy( source.sh ); return this; }, fromJSON: function ( json ) { this.intensity = json.intensity; // TODO: Move this bit to Light.fromJSON(); this.sh.fromArray( json.sh ); return this; }, toJSON: function ( meta ) { const data = Light.prototype.toJSON.call( this, meta ); data.object.sh = this.sh.toArray(); return data; } } ); function MaterialLoader( manager ) { Loader.call( this, manager ); this.textures = {}; } MaterialLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: MaterialLoader, load: function ( url, onLoad, onProgress, onError ) { const scope = this; const loader = new FileLoader( scope.manager ); loader.setPath( scope.path ); loader.setRequestHeader( scope.requestHeader ); loader.setWithCredentials( scope.withCredentials ); loader.load( url, function ( text ) { try { onLoad( scope.parse( JSON.parse( text ) ) ); } catch ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); } }, onProgress, onError ); }, parse: function ( json ) { const textures = this.textures; function getTexture( name ) { if ( textures[ name ] === undefined ) { console.warn( 'THREE.MaterialLoader: Undefined texture', name ); } return textures[ name ]; } const material = new Materials[ json.type ](); if ( json.uuid !== undefined ) material.uuid = json.uuid; if ( json.name !== undefined ) material.name = json.name; if ( json.color !== undefined && material.color !== undefined ) material.color.setHex( json.color ); if ( json.roughness !== undefined ) material.roughness = json.roughness; if ( json.metalness !== undefined ) material.metalness = json.metalness; if ( json.sheen !== undefined ) material.sheen = new Color().setHex( json.sheen ); if ( json.emissive !== undefined && material.emissive !== undefined ) material.emissive.setHex( json.emissive ); if ( json.specular !== undefined && material.specular !== undefined ) material.specular.setHex( json.specular ); if ( json.shininess !== undefined ) material.shininess = json.shininess; if ( json.clearcoat !== undefined ) material.clearcoat = json.clearcoat; if ( json.clearcoatRoughness !== undefined ) material.clearcoatRoughness = json.clearcoatRoughness; if ( json.fog !== undefined ) material.fog = json.fog; if ( json.flatShading !== undefined ) material.flatShading = json.flatShading; if ( json.blending !== undefined ) material.blending = json.blending; if ( json.combine !== undefined ) material.combine = json.combine; if ( json.side !== undefined ) material.side = json.side; if ( json.opacity !== undefined ) material.opacity = json.opacity; if ( json.transparent !== undefined ) material.transparent = json.transparent; if ( json.alphaTest !== undefined ) material.alphaTest = json.alphaTest; if ( json.depthTest !== undefined ) material.depthTest = json.depthTest; if ( json.depthWrite !== undefined ) material.depthWrite = json.depthWrite; if ( json.colorWrite !== undefined ) material.colorWrite = json.colorWrite; if ( json.stencilWrite !== undefined ) material.stencilWrite = json.stencilWrite; if ( json.stencilWriteMask !== undefined ) material.stencilWriteMask = json.stencilWriteMask; if ( json.stencilFunc !== undefined ) material.stencilFunc = json.stencilFunc; if ( json.stencilRef !== undefined ) material.stencilRef = json.stencilRef; if ( json.stencilFuncMask !== undefined ) material.stencilFuncMask = json.stencilFuncMask; if ( json.stencilFail !== undefined ) material.stencilFail = json.stencilFail; if ( json.stencilZFail !== undefined ) material.stencilZFail = json.stencilZFail; if ( json.stencilZPass !== undefined ) material.stencilZPass = json.stencilZPass; if ( json.wireframe !== undefined ) material.wireframe = json.wireframe; if ( json.wireframeLinewidth !== undefined ) material.wireframeLinewidth = json.wireframeLinewidth; if ( json.wireframeLinecap !== undefined ) material.wireframeLinecap = json.wireframeLinecap; if ( json.wireframeLinejoin !== undefined ) material.wireframeLinejoin = json.wireframeLinejoin; if ( json.rotation !== undefined ) material.rotation = json.rotation; if ( json.linewidth !== 1 ) material.linewidth = json.linewidth; if ( json.dashSize !== undefined ) material.dashSize = json.dashSize; if ( json.gapSize !== undefined ) material.gapSize = json.gapSize; if ( json.scale !== undefined ) material.scale = json.scale; if ( json.polygonOffset !== undefined ) material.polygonOffset = json.polygonOffset; if ( json.polygonOffsetFactor !== undefined ) material.polygonOffsetFactor = json.polygonOffsetFactor; if ( json.polygonOffsetUnits !== undefined ) material.polygonOffsetUnits = json.polygonOffsetUnits; if ( json.skinning !== undefined ) material.skinning = json.skinning; if ( json.morphTargets !== undefined ) material.morphTargets = json.morphTargets; if ( json.morphNormals !== undefined ) material.morphNormals = json.morphNormals; if ( json.dithering !== undefined ) material.dithering = json.dithering; if ( json.vertexTangents !== undefined ) material.vertexTangents = json.vertexTangents; if ( json.visible !== undefined ) material.visible = json.visible; if ( json.toneMapped !== undefined ) material.toneMapped = json.toneMapped; if ( json.userData !== undefined ) material.userData = json.userData; if ( json.vertexColors !== undefined ) { if ( typeof json.vertexColors === 'number' ) { material.vertexColors = ( json.vertexColors > 0 ) ? true : false; } else { material.vertexColors = json.vertexColors; } } // Shader Material if ( json.uniforms !== undefined ) { for ( const name in json.uniforms ) { const uniform = json.uniforms[ name ]; material.uniforms[ name ] = {}; switch ( uniform.type ) { case 't': material.uniforms[ name ].value = getTexture( uniform.value ); break; case 'c': material.uniforms[ name ].value = new Color().setHex( uniform.value ); break; case 'v2': material.uniforms[ name ].value = new Vector2().fromArray( uniform.value ); break; case 'v3': material.uniforms[ name ].value = new Vector3().fromArray( uniform.value ); break; case 'v4': material.uniforms[ name ].value = new Vector4().fromArray( uniform.value ); break; case 'm3': material.uniforms[ name ].value = new Matrix3().fromArray( uniform.value ); break; case 'm4': material.uniforms[ name ].value = new Matrix4().fromArray( uniform.value ); break; default: material.uniforms[ name ].value = uniform.value; } } } if ( json.defines !== undefined ) material.defines = json.defines; if ( json.vertexShader !== undefined ) material.vertexShader = json.vertexShader; if ( json.fragmentShader !== undefined ) material.fragmentShader = json.fragmentShader; if ( json.extensions !== undefined ) { for ( const key in json.extensions ) { material.extensions[ key ] = json.extensions[ key ]; } } // Deprecated if ( json.shading !== undefined ) material.flatShading = json.shading === 1; // THREE.FlatShading // for PointsMaterial if ( json.size !== undefined ) material.size = json.size; if ( json.sizeAttenuation !== undefined ) material.sizeAttenuation = json.sizeAttenuation; // maps if ( json.map !== undefined ) material.map = getTexture( json.map ); if ( json.matcap !== undefined ) material.matcap = getTexture( json.matcap ); if ( json.alphaMap !== undefined ) material.alphaMap = getTexture( json.alphaMap ); if ( json.bumpMap !== undefined ) material.bumpMap = getTexture( json.bumpMap ); if ( json.bumpScale !== undefined ) material.bumpScale = json.bumpScale; if ( json.normalMap !== undefined ) material.normalMap = getTexture( json.normalMap ); if ( json.normalMapType !== undefined ) material.normalMapType = json.normalMapType; if ( json.normalScale !== undefined ) { let normalScale = json.normalScale; if ( Array.isArray( normalScale ) === false ) { // Blender exporter used to export a scalar. See #7459 normalScale = [ normalScale, normalScale ]; } material.normalScale = new Vector2().fromArray( normalScale ); } if ( json.displacementMap !== undefined ) material.displacementMap = getTexture( json.displacementMap ); if ( json.displacementScale !== undefined ) material.displacementScale = json.displacementScale; if ( json.displacementBias !== undefined ) material.displacementBias = json.displacementBias; if ( json.roughnessMap !== undefined ) material.roughnessMap = getTexture( json.roughnessMap ); if ( json.metalnessMap !== undefined ) material.metalnessMap = getTexture( json.metalnessMap ); if ( json.emissiveMap !== undefined ) material.emissiveMap = getTexture( json.emissiveMap ); if ( json.emissiveIntensity !== undefined ) material.emissiveIntensity = json.emissiveIntensity; if ( json.specularMap !== undefined ) material.specularMap = getTexture( json.specularMap ); if ( json.envMap !== undefined ) material.envMap = getTexture( json.envMap ); if ( json.envMapIntensity !== undefined ) material.envMapIntensity = json.envMapIntensity; if ( json.reflectivity !== undefined ) material.reflectivity = json.reflectivity; if ( json.refractionRatio !== undefined ) material.refractionRatio = json.refractionRatio; if ( json.lightMap !== undefined ) material.lightMap = getTexture( json.lightMap ); if ( json.lightMapIntensity !== undefined ) material.lightMapIntensity = json.lightMapIntensity; if ( json.aoMap !== undefined ) material.aoMap = getTexture( json.aoMap ); if ( json.aoMapIntensity !== undefined ) material.aoMapIntensity = json.aoMapIntensity; if ( json.gradientMap !== undefined ) material.gradientMap = getTexture( json.gradientMap ); if ( json.clearcoatMap !== undefined ) material.clearcoatMap = getTexture( json.clearcoatMap ); if ( json.clearcoatRoughnessMap !== undefined ) material.clearcoatRoughnessMap = getTexture( json.clearcoatRoughnessMap ); if ( json.clearcoatNormalMap !== undefined ) material.clearcoatNormalMap = getTexture( json.clearcoatNormalMap ); if ( json.clearcoatNormalScale !== undefined ) material.clearcoatNormalScale = new Vector2().fromArray( json.clearcoatNormalScale ); if ( json.transmission !== undefined ) material.transmission = json.transmission; if ( json.transmissionMap !== undefined ) material.transmissionMap = getTexture( json.transmissionMap ); return material; }, setTextures: function ( value ) { this.textures = value; return this; } } ); const LoaderUtils = { decodeText: function ( array ) { if ( typeof TextDecoder !== 'undefined' ) { return new TextDecoder().decode( array ); } // Avoid the String.fromCharCode.apply(null, array) shortcut, which // throws a "maximum call stack size exceeded" error for large arrays. let s = ''; for ( let i = 0, il = array.length; i < il; i ++ ) { // Implicitly assumes little-endian. s += String.fromCharCode( array[ i ] ); } try { // merges multi-byte utf-8 characters. return decodeURIComponent( escape( s ) ); } catch ( e ) { // see #16358 return s; } }, extractUrlBase: function ( url ) { const index = url.lastIndexOf( '/' ); if ( index === - 1 ) return './'; return url.substr( 0, index + 1 ); } }; function InstancedBufferGeometry() { BufferGeometry.call( this ); this.type = 'InstancedBufferGeometry'; this.instanceCount = Infinity; } InstancedBufferGeometry.prototype = Object.assign( Object.create( BufferGeometry.prototype ), { constructor: InstancedBufferGeometry, isInstancedBufferGeometry: true, copy: function ( source ) { BufferGeometry.prototype.copy.call( this, source ); this.instanceCount = source.instanceCount; return this; }, clone: function () { return new this.constructor().copy( this ); }, toJSON: function () { const data = BufferGeometry.prototype.toJSON.call( this ); data.instanceCount = this.instanceCount; data.isInstancedBufferGeometry = true; return data; } } ); function InstancedBufferAttribute( array, itemSize, normalized, meshPerAttribute ) { if ( typeof ( normalized ) === 'number' ) { meshPerAttribute = normalized; normalized = false; console.error( 'THREE.InstancedBufferAttribute: The constructor now expects normalized as the third argument.' ); } BufferAttribute.call( this, array, itemSize, normalized ); this.meshPerAttribute = meshPerAttribute || 1; } InstancedBufferAttribute.prototype = Object.assign( Object.create( BufferAttribute.prototype ), { constructor: InstancedBufferAttribute, isInstancedBufferAttribute: true, copy: function ( source ) { BufferAttribute.prototype.copy.call( this, source ); this.meshPerAttribute = source.meshPerAttribute; return this; }, toJSON: function () { const data = BufferAttribute.prototype.toJSON.call( this ); data.meshPerAttribute = this.meshPerAttribute; data.isInstancedBufferAttribute = true; return data; } } ); function BufferGeometryLoader( manager ) { Loader.call( this, manager ); } BufferGeometryLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: BufferGeometryLoader, load: function ( url, onLoad, onProgress, onError ) { const scope = this; const loader = new FileLoader( scope.manager ); loader.setPath( scope.path ); loader.setRequestHeader( scope.requestHeader ); loader.setWithCredentials( scope.withCredentials ); loader.load( url, function ( text ) { try { onLoad( scope.parse( JSON.parse( text ) ) ); } catch ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); } }, onProgress, onError ); }, parse: function ( json ) { const interleavedBufferMap = {}; const arrayBufferMap = {}; function getInterleavedBuffer( json, uuid ) { if ( interleavedBufferMap[ uuid ] !== undefined ) return interleavedBufferMap[ uuid ]; const interleavedBuffers = json.interleavedBuffers; const interleavedBuffer = interleavedBuffers[ uuid ]; const buffer = getArrayBuffer( json, interleavedBuffer.buffer ); const array = getTypedArray( interleavedBuffer.type, buffer ); const ib = new InterleavedBuffer( array, interleavedBuffer.stride ); ib.uuid = interleavedBuffer.uuid; interleavedBufferMap[ uuid ] = ib; return ib; } function getArrayBuffer( json, uuid ) { if ( arrayBufferMap[ uuid ] !== undefined ) return arrayBufferMap[ uuid ]; const arrayBuffers = json.arrayBuffers; const arrayBuffer = arrayBuffers[ uuid ]; const ab = new Uint32Array( arrayBuffer ).buffer; arrayBufferMap[ uuid ] = ab; return ab; } const geometry = json.isInstancedBufferGeometry ? new InstancedBufferGeometry() : new BufferGeometry(); const index = json.data.index; if ( index !== undefined ) { const typedArray = getTypedArray( index.type, index.array ); geometry.setIndex( new BufferAttribute( typedArray, 1 ) ); } const attributes = json.data.attributes; for ( const key in attributes ) { const attribute = attributes[ key ]; let bufferAttribute; if ( attribute.isInterleavedBufferAttribute ) { const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data ); bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized ); } else { const typedArray = getTypedArray( attribute.type, attribute.array ); const bufferAttributeConstr = attribute.isInstancedBufferAttribute ? InstancedBufferAttribute : BufferAttribute; bufferAttribute = new bufferAttributeConstr( typedArray, attribute.itemSize, attribute.normalized ); } if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name; geometry.setAttribute( key, bufferAttribute ); } const morphAttributes = json.data.morphAttributes; if ( morphAttributes ) { for ( const key in morphAttributes ) { const attributeArray = morphAttributes[ key ]; const array = []; for ( let i = 0, il = attributeArray.length; i < il; i ++ ) { const attribute = attributeArray[ i ]; let bufferAttribute; if ( attribute.isInterleavedBufferAttribute ) { const interleavedBuffer = getInterleavedBuffer( json.data, attribute.data ); bufferAttribute = new InterleavedBufferAttribute( interleavedBuffer, attribute.itemSize, attribute.offset, attribute.normalized ); } else { const typedArray = getTypedArray( attribute.type, attribute.array ); bufferAttribute = new BufferAttribute( typedArray, attribute.itemSize, attribute.normalized ); } if ( attribute.name !== undefined ) bufferAttribute.name = attribute.name; array.push( bufferAttribute ); } geometry.morphAttributes[ key ] = array; } } const morphTargetsRelative = json.data.morphTargetsRelative; if ( morphTargetsRelative ) { geometry.morphTargetsRelative = true; } const groups = json.data.groups || json.data.drawcalls || json.data.offsets; if ( groups !== undefined ) { for ( let i = 0, n = groups.length; i !== n; ++ i ) { const group = groups[ i ]; geometry.addGroup( group.start, group.count, group.materialIndex ); } } const boundingSphere = json.data.boundingSphere; if ( boundingSphere !== undefined ) { const center = new Vector3(); if ( boundingSphere.center !== undefined ) { center.fromArray( boundingSphere.center ); } geometry.boundingSphere = new Sphere( center, boundingSphere.radius ); } if ( json.name ) geometry.name = json.name; if ( json.userData ) geometry.userData = json.userData; return geometry; } } ); class ObjectLoader extends Loader { constructor( manager ) { super( manager ); } load( url, onLoad, onProgress, onError ) { const scope = this; const path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path; this.resourcePath = this.resourcePath || path; const loader = new FileLoader( this.manager ); loader.setPath( this.path ); loader.setRequestHeader( this.requestHeader ); loader.setWithCredentials( this.withCredentials ); loader.load( url, function ( text ) { let json = null; try { json = JSON.parse( text ); } catch ( error ) { if ( onError !== undefined ) onError( error ); console.error( 'THREE:ObjectLoader: Can\'t parse ' + url + '.', error.message ); return; } const metadata = json.metadata; if ( metadata === undefined || metadata.type === undefined || metadata.type.toLowerCase() === 'geometry' ) { console.error( 'THREE.ObjectLoader: Can\'t load ' + url ); return; } scope.parse( json, onLoad ); }, onProgress, onError ); } parse( json, onLoad ) { const animations = this.parseAnimations( json.animations ); const shapes = this.parseShapes( json.shapes ); const geometries = this.parseGeometries( json.geometries, shapes ); const images = this.parseImages( json.images, function () { if ( onLoad !== undefined ) onLoad( object ); } ); const textures = this.parseTextures( json.textures, images ); const materials = this.parseMaterials( json.materials, textures ); const object = this.parseObject( json.object, geometries, materials, animations ); const skeletons = this.parseSkeletons( json.skeletons, object ); this.bindSkeletons( object, skeletons ); // if ( onLoad !== undefined ) { let hasImages = false; for ( const uuid in images ) { if ( images[ uuid ] instanceof HTMLImageElement ) { hasImages = true; break; } } if ( hasImages === false ) onLoad( object ); } return object; } parseShapes( json ) { const shapes = {}; if ( json !== undefined ) { for ( let i = 0, l = json.length; i < l; i ++ ) { const shape = new Shape().fromJSON( json[ i ] ); shapes[ shape.uuid ] = shape; } } return shapes; } parseSkeletons( json, object ) { const skeletons = {}; const bones = {}; // generate bone lookup table object.traverse( function ( child ) { if ( child.isBone ) bones[ child.uuid ] = child; } ); // create skeletons if ( json !== undefined ) { for ( let i = 0, l = json.length; i < l; i ++ ) { const skeleton = new Skeleton().fromJSON( json[ i ], bones ); skeletons[ skeleton.uuid ] = skeleton; } } return skeletons; } parseGeometries( json, shapes ) { const geometries = {}; let geometryShapes; if ( json !== undefined ) { const bufferGeometryLoader = new BufferGeometryLoader(); for ( let i = 0, l = json.length; i < l; i ++ ) { let geometry; const data = json[ i ]; switch ( data.type ) { case 'PlaneGeometry': case 'PlaneBufferGeometry': geometry = new Geometries[ data.type ]( data.width, data.height, data.widthSegments, data.heightSegments ); break; case 'BoxGeometry': case 'BoxBufferGeometry': case 'CubeGeometry': // backwards compatible geometry = new Geometries[ data.type ]( data.width, data.height, data.depth, data.widthSegments, data.heightSegments, data.depthSegments ); break; case 'CircleGeometry': case 'CircleBufferGeometry': geometry = new Geometries[ data.type ]( data.radius, data.segments, data.thetaStart, data.thetaLength ); break; case 'CylinderGeometry': case 'CylinderBufferGeometry': geometry = new Geometries[ data.type ]( data.radiusTop, data.radiusBottom, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength ); break; case 'ConeGeometry': case 'ConeBufferGeometry': geometry = new Geometries[ data.type ]( data.radius, data.height, data.radialSegments, data.heightSegments, data.openEnded, data.thetaStart, data.thetaLength ); break; case 'SphereGeometry': case 'SphereBufferGeometry': geometry = new Geometries[ data.type ]( data.radius, data.widthSegments, data.heightSegments, data.phiStart, data.phiLength, data.thetaStart, data.thetaLength ); break; case 'DodecahedronGeometry': case 'DodecahedronBufferGeometry': case 'IcosahedronGeometry': case 'IcosahedronBufferGeometry': case 'OctahedronGeometry': case 'OctahedronBufferGeometry': case 'TetrahedronGeometry': case 'TetrahedronBufferGeometry': geometry = new Geometries[ data.type ]( data.radius, data.detail ); break; case 'RingGeometry': case 'RingBufferGeometry': geometry = new Geometries[ data.type ]( data.innerRadius, data.outerRadius, data.thetaSegments, data.phiSegments, data.thetaStart, data.thetaLength ); break; case 'TorusGeometry': case 'TorusBufferGeometry': geometry = new Geometries[ data.type ]( data.radius, data.tube, data.radialSegments, data.tubularSegments, data.arc ); break; case 'TorusKnotGeometry': case 'TorusKnotBufferGeometry': geometry = new Geometries[ data.type ]( data.radius, data.tube, data.tubularSegments, data.radialSegments, data.p, data.q ); break; case 'TubeGeometry': case 'TubeBufferGeometry': // This only works for built-in curves (e.g. CatmullRomCurve3). // User defined curves or instances of CurvePath will not be deserialized. geometry = new Geometries[ data.type ]( new Curves[ data.path.type ]().fromJSON( data.path ), data.tubularSegments, data.radius, data.radialSegments, data.closed ); break; case 'LatheGeometry': case 'LatheBufferGeometry': geometry = new Geometries[ data.type ]( data.points, data.segments, data.phiStart, data.phiLength ); break; case 'PolyhedronGeometry': case 'PolyhedronBufferGeometry': geometry = new Geometries[ data.type ]( data.vertices, data.indices, data.radius, data.details ); break; case 'ShapeGeometry': case 'ShapeBufferGeometry': geometryShapes = []; for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) { const shape = shapes[ data.shapes[ j ] ]; geometryShapes.push( shape ); } geometry = new Geometries[ data.type ]( geometryShapes, data.curveSegments ); break; case 'ExtrudeGeometry': case 'ExtrudeBufferGeometry': geometryShapes = []; for ( let j = 0, jl = data.shapes.length; j < jl; j ++ ) { const shape = shapes[ data.shapes[ j ] ]; geometryShapes.push( shape ); } const extrudePath = data.options.extrudePath; if ( extrudePath !== undefined ) { data.options.extrudePath = new Curves[ extrudePath.type ]().fromJSON( extrudePath ); } geometry = new Geometries[ data.type ]( geometryShapes, data.options ); break; case 'BufferGeometry': case 'InstancedBufferGeometry': geometry = bufferGeometryLoader.parse( data ); break; case 'Geometry': console.error( 'THREE.ObjectLoader: Loading "Geometry" is not supported anymore.' ); break; default: console.warn( 'THREE.ObjectLoader: Unsupported geometry type "' + data.type + '"' ); continue; } geometry.uuid = data.uuid; if ( data.name !== undefined ) geometry.name = data.name; if ( geometry.isBufferGeometry === true && data.userData !== undefined ) geometry.userData = data.userData; geometries[ data.uuid ] = geometry; } } return geometries; } parseMaterials( json, textures ) { const cache = {}; // MultiMaterial const materials = {}; if ( json !== undefined ) { const loader = new MaterialLoader(); loader.setTextures( textures ); for ( let i = 0, l = json.length; i < l; i ++ ) { const data = json[ i ]; if ( data.type === 'MultiMaterial' ) { // Deprecated const array = []; for ( let j = 0; j < data.materials.length; j ++ ) { const material = data.materials[ j ]; if ( cache[ material.uuid ] === undefined ) { cache[ material.uuid ] = loader.parse( material ); } array.push( cache[ material.uuid ] ); } materials[ data.uuid ] = array; } else { if ( cache[ data.uuid ] === undefined ) { cache[ data.uuid ] = loader.parse( data ); } materials[ data.uuid ] = cache[ data.uuid ]; } } } return materials; } parseAnimations( json ) { const animations = {}; if ( json !== undefined ) { for ( let i = 0; i < json.length; i ++ ) { const data = json[ i ]; const clip = AnimationClip.parse( data ); animations[ clip.uuid ] = clip; } } return animations; } parseImages( json, onLoad ) { const scope = this; const images = {}; let loader; function loadImage( url ) { scope.manager.itemStart( url ); return loader.load( url, function () { scope.manager.itemEnd( url ); }, undefined, function () { scope.manager.itemError( url ); scope.manager.itemEnd( url ); } ); } function deserializeImage( image ) { if ( typeof image === 'string' ) { const url = image; const path = /^(\/\/)|([a-z]+:(\/\/)?)/i.test( url ) ? url : scope.resourcePath + url; return loadImage( path ); } else { if ( image.data ) { return { data: getTypedArray( image.type, image.data ), width: image.width, height: image.height }; } else { return null; } } } if ( json !== undefined && json.length > 0 ) { const manager = new LoadingManager( onLoad ); loader = new ImageLoader( manager ); loader.setCrossOrigin( this.crossOrigin ); for ( let i = 0, il = json.length; i < il; i ++ ) { const image = json[ i ]; const url = image.url; if ( Array.isArray( url ) ) { // load array of images e.g CubeTexture images[ image.uuid ] = []; for ( let j = 0, jl = url.length; j < jl; j ++ ) { const currentUrl = url[ j ]; const deserializedImage = deserializeImage( currentUrl ); if ( deserializedImage !== null ) { if ( deserializedImage instanceof HTMLImageElement ) { images[ image.uuid ].push( deserializedImage ); } else { // special case: handle array of data textures for cube textures images[ image.uuid ].push( new DataTexture( deserializedImage.data, deserializedImage.width, deserializedImage.height ) ); } } } } else { // load single image const deserializedImage = deserializeImage( image.url ); if ( deserializedImage !== null ) { images[ image.uuid ] = deserializedImage; } } } } return images; } parseTextures( json, images ) { function parseConstant( value, type ) { if ( typeof value === 'number' ) return value; console.warn( 'THREE.ObjectLoader.parseTexture: Constant should be in numeric form.', value ); return type[ value ]; } const textures = {}; if ( json !== undefined ) { for ( let i = 0, l = json.length; i < l; i ++ ) { const data = json[ i ]; if ( data.image === undefined ) { console.warn( 'THREE.ObjectLoader: No "image" specified for', data.uuid ); } if ( images[ data.image ] === undefined ) { console.warn( 'THREE.ObjectLoader: Undefined image', data.image ); } let texture; const image = images[ data.image ]; if ( Array.isArray( image ) ) { texture = new CubeTexture( image ); if ( image.length === 6 ) texture.needsUpdate = true; } else { if ( image && image.data ) { texture = new DataTexture( image.data, image.width, image.height ); } else { texture = new Texture( image ); } if ( image ) texture.needsUpdate = true; // textures can have undefined image data } texture.uuid = data.uuid; if ( data.name !== undefined ) texture.name = data.name; if ( data.mapping !== undefined ) texture.mapping = parseConstant( data.mapping, TEXTURE_MAPPING ); if ( data.offset !== undefined ) texture.offset.fromArray( data.offset ); if ( data.repeat !== undefined ) texture.repeat.fromArray( data.repeat ); if ( data.center !== undefined ) texture.center.fromArray( data.center ); if ( data.rotation !== undefined ) texture.rotation = data.rotation; if ( data.wrap !== undefined ) { texture.wrapS = parseConstant( data.wrap[ 0 ], TEXTURE_WRAPPING ); texture.wrapT = parseConstant( data.wrap[ 1 ], TEXTURE_WRAPPING ); } if ( data.format !== undefined ) texture.format = data.format; if ( data.type !== undefined ) texture.type = data.type; if ( data.encoding !== undefined ) texture.encoding = data.encoding; if ( data.minFilter !== undefined ) texture.minFilter = parseConstant( data.minFilter, TEXTURE_FILTER ); if ( data.magFilter !== undefined ) texture.magFilter = parseConstant( data.magFilter, TEXTURE_FILTER ); if ( data.anisotropy !== undefined ) texture.anisotropy = data.anisotropy; if ( data.flipY !== undefined ) texture.flipY = data.flipY; if ( data.premultiplyAlpha !== undefined ) texture.premultiplyAlpha = data.premultiplyAlpha; if ( data.unpackAlignment !== undefined ) texture.unpackAlignment = data.unpackAlignment; textures[ data.uuid ] = texture; } } return textures; } parseObject( data, geometries, materials, animations ) { let object; function getGeometry( name ) { if ( geometries[ name ] === undefined ) { console.warn( 'THREE.ObjectLoader: Undefined geometry', name ); } return geometries[ name ]; } function getMaterial( name ) { if ( name === undefined ) return undefined; if ( Array.isArray( name ) ) { const array = []; for ( let i = 0, l = name.length; i < l; i ++ ) { const uuid = name[ i ]; if ( materials[ uuid ] === undefined ) { console.warn( 'THREE.ObjectLoader: Undefined material', uuid ); } array.push( materials[ uuid ] ); } return array; } if ( materials[ name ] === undefined ) { console.warn( 'THREE.ObjectLoader: Undefined material', name ); } return materials[ name ]; } let geometry, material; switch ( data.type ) { case 'Scene': object = new Scene(); if ( data.background !== undefined ) { if ( Number.isInteger( data.background ) ) { object.background = new Color( data.background ); } } if ( data.fog !== undefined ) { if ( data.fog.type === 'Fog' ) { object.fog = new Fog( data.fog.color, data.fog.near, data.fog.far ); } else if ( data.fog.type === 'FogExp2' ) { object.fog = new FogExp2( data.fog.color, data.fog.density ); } } break; case 'PerspectiveCamera': object = new PerspectiveCamera( data.fov, data.aspect, data.near, data.far ); if ( data.focus !== undefined ) object.focus = data.focus; if ( data.zoom !== undefined ) object.zoom = data.zoom; if ( data.filmGauge !== undefined ) object.filmGauge = data.filmGauge; if ( data.filmOffset !== undefined ) object.filmOffset = data.filmOffset; if ( data.view !== undefined ) object.view = Object.assign( {}, data.view ); break; case 'OrthographicCamera': object = new OrthographicCamera( data.left, data.right, data.top, data.bottom, data.near, data.far ); if ( data.zoom !== undefined ) object.zoom = data.zoom; if ( data.view !== undefined ) object.view = Object.assign( {}, data.view ); break; case 'AmbientLight': object = new AmbientLight( data.color, data.intensity ); break; case 'DirectionalLight': object = new DirectionalLight( data.color, data.intensity ); break; case 'PointLight': object = new PointLight( data.color, data.intensity, data.distance, data.decay ); break; case 'RectAreaLight': object = new RectAreaLight( data.color, data.intensity, data.width, data.height ); break; case 'SpotLight': object = new SpotLight( data.color, data.intensity, data.distance, data.angle, data.penumbra, data.decay ); break; case 'HemisphereLight': object = new HemisphereLight( data.color, data.groundColor, data.intensity ); break; case 'LightProbe': object = new LightProbe().fromJSON( data ); break; case 'SkinnedMesh': geometry = getGeometry( data.geometry ); material = getMaterial( data.material ); object = new SkinnedMesh( geometry, material ); if ( data.bindMode !== undefined ) object.bindMode = data.bindMode; if ( data.bindMatrix !== undefined ) object.bindMatrix.fromArray( data.bindMatrix ); if ( data.skeleton !== undefined ) object.skeleton = data.skeleton; break; case 'Mesh': geometry = getGeometry( data.geometry ); material = getMaterial( data.material ); object = new Mesh( geometry, material ); break; case 'InstancedMesh': geometry = getGeometry( data.geometry ); material = getMaterial( data.material ); const count = data.count; const instanceMatrix = data.instanceMatrix; object = new InstancedMesh( geometry, material, count ); object.instanceMatrix = new BufferAttribute( new Float32Array( instanceMatrix.array ), 16 ); break; case 'LOD': object = new LOD(); break; case 'Line': object = new Line( getGeometry( data.geometry ), getMaterial( data.material ) ); break; case 'LineLoop': object = new LineLoop( getGeometry( data.geometry ), getMaterial( data.material ) ); break; case 'LineSegments': object = new LineSegments( getGeometry( data.geometry ), getMaterial( data.material ) ); break; case 'PointCloud': case 'Points': object = new Points( getGeometry( data.geometry ), getMaterial( data.material ) ); break; case 'Sprite': object = new Sprite$1( getMaterial( data.material ) ); break; case 'Group': object = new Group(); break; case 'Bone': object = new Bone(); break; default: object = new Object3D(); } object.uuid = data.uuid; if ( data.name !== undefined ) object.name = data.name; if ( data.matrix !== undefined ) { object.matrix.fromArray( data.matrix ); if ( data.matrixAutoUpdate !== undefined ) object.matrixAutoUpdate = data.matrixAutoUpdate; if ( object.matrixAutoUpdate ) object.matrix.decompose( object.position, object.quaternion, object.scale ); } else { if ( data.position !== undefined ) object.position.fromArray( data.position ); if ( data.rotation !== undefined ) object.rotation.fromArray( data.rotation ); if ( data.quaternion !== undefined ) object.quaternion.fromArray( data.quaternion ); if ( data.scale !== undefined ) object.scale.fromArray( data.scale ); } if ( data.castShadow !== undefined ) object.castShadow = data.castShadow; if ( data.receiveShadow !== undefined ) object.receiveShadow = data.receiveShadow; if ( data.shadow ) { if ( data.shadow.bias !== undefined ) object.shadow.bias = data.shadow.bias; if ( data.shadow.normalBias !== undefined ) object.shadow.normalBias = data.shadow.normalBias; if ( data.shadow.radius !== undefined ) object.shadow.radius = data.shadow.radius; if ( data.shadow.mapSize !== undefined ) object.shadow.mapSize.fromArray( data.shadow.mapSize ); if ( data.shadow.camera !== undefined ) object.shadow.camera = this.parseObject( data.shadow.camera ); } if ( data.visible !== undefined ) object.visible = data.visible; if ( data.frustumCulled !== undefined ) object.frustumCulled = data.frustumCulled; if ( data.renderOrder !== undefined ) object.renderOrder = data.renderOrder; if ( data.userData !== undefined ) object.userData = data.userData; if ( data.layers !== undefined ) object.layers.mask = data.layers; if ( data.children !== undefined ) { const children = data.children; for ( let i = 0; i < children.length; i ++ ) { object.add( this.parseObject( children[ i ], geometries, materials, animations ) ); } } if ( data.animations !== undefined ) { const objectAnimations = data.animations; for ( let i = 0; i < objectAnimations.length; i ++ ) { const uuid = objectAnimations[ i ]; object.animations.push( animations[ uuid ] ); } } if ( data.type === 'LOD' ) { if ( data.autoUpdate !== undefined ) object.autoUpdate = data.autoUpdate; const levels = data.levels; for ( let l = 0; l < levels.length; l ++ ) { const level = levels[ l ]; const child = object.getObjectByProperty( 'uuid', level.object ); if ( child !== undefined ) { object.addLevel( child, level.distance ); } } } return object; } bindSkeletons( object, skeletons ) { if ( Object.keys( skeletons ).length === 0 ) return; object.traverse( function ( child ) { if ( child.isSkinnedMesh === true && child.skeleton !== undefined ) { const skeleton = skeletons[ child.skeleton ]; if ( skeleton === undefined ) { console.warn( 'THREE.ObjectLoader: No skeleton found with UUID:', child.skeleton ); } else { child.bind( skeleton, child.bindMatrix ); } } } ); } /* DEPRECATED */ setTexturePath( value ) { console.warn( 'THREE.ObjectLoader: .setTexturePath() has been renamed to .setResourcePath().' ); return this.setResourcePath( value ); } } const TEXTURE_MAPPING = { UVMapping: UVMapping, CubeReflectionMapping: CubeReflectionMapping, CubeRefractionMapping: CubeRefractionMapping, EquirectangularReflectionMapping: EquirectangularReflectionMapping, EquirectangularRefractionMapping: EquirectangularRefractionMapping, CubeUVReflectionMapping: CubeUVReflectionMapping, CubeUVRefractionMapping: CubeUVRefractionMapping }; const TEXTURE_WRAPPING = { RepeatWrapping: RepeatWrapping, ClampToEdgeWrapping: ClampToEdgeWrapping, MirroredRepeatWrapping: MirroredRepeatWrapping }; const TEXTURE_FILTER = { NearestFilter: NearestFilter, NearestMipmapNearestFilter: NearestMipmapNearestFilter, NearestMipmapLinearFilter: NearestMipmapLinearFilter, LinearFilter: LinearFilter, LinearMipmapNearestFilter: LinearMipmapNearestFilter, LinearMipmapLinearFilter: LinearMipmapLinearFilter }; function ImageBitmapLoader( manager ) { if ( typeof createImageBitmap === 'undefined' ) { console.warn( 'THREE.ImageBitmapLoader: createImageBitmap() not supported.' ); } if ( typeof fetch === 'undefined' ) { console.warn( 'THREE.ImageBitmapLoader: fetch() not supported.' ); } Loader.call( this, manager ); this.options = { premultiplyAlpha: 'none' }; } ImageBitmapLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: ImageBitmapLoader, isImageBitmapLoader: true, setOptions: function setOptions( options ) { this.options = options; return this; }, load: function ( url, onLoad, onProgress, onError ) { if ( url === undefined ) url = ''; if ( this.path !== undefined ) url = this.path + url; url = this.manager.resolveURL( url ); const scope = this; const cached = Cache.get( url ); if ( cached !== undefined ) { scope.manager.itemStart( url ); setTimeout( function () { if ( onLoad ) onLoad( cached ); scope.manager.itemEnd( url ); }, 0 ); return cached; } const fetchOptions = {}; fetchOptions.credentials = ( this.crossOrigin === 'anonymous' ) ? 'same-origin' : 'include'; fetch( url, fetchOptions ).then( function ( res ) { return res.blob(); } ).then( function ( blob ) { //console.log('getBlob', url ) return createImageBitmap( blob, scope.options ); } ).then( function ( imageBitmap ) { Cache.add( url, imageBitmap ); if ( onLoad ) onLoad( imageBitmap ); scope.manager.itemEnd( url ); } ).catch( function ( e ) { //console.log('error', url, e) if ( onError ) onError( e, url ); scope.manager.itemError( url ); scope.manager.itemEnd( url ); } ); scope.manager.itemStart( url ); } } ); function ShapePath() { this.type = 'ShapePath'; this.color = new Color(); this.subPaths = []; this.currentPath = null; } Object.assign( ShapePath.prototype, { moveTo: function ( x, y ) { this.currentPath = new Path(); this.subPaths.push( this.currentPath ); this.currentPath.moveTo( x, y ); return this; }, lineTo: function ( x, y ) { this.currentPath.lineTo( x, y ); return this; }, quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) { this.currentPath.quadraticCurveTo( aCPx, aCPy, aX, aY ); return this; }, bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) { this.currentPath.bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ); return this; }, splineThru: function ( pts ) { this.currentPath.splineThru( pts ); return this; }, toShapes: function ( isCCW, noHoles ) { function toShapesNoHoles( inSubpaths ) { const shapes = []; for ( let i = 0, l = inSubpaths.length; i < l; i ++ ) { const tmpPath = inSubpaths[ i ]; const tmpShape = new Shape(); tmpShape.curves = tmpPath.curves; shapes.push( tmpShape ); } return shapes; } function isPointInsidePolygon( inPt, inPolygon ) { const polyLen = inPolygon.length; // inPt on polygon contour => immediate success or // toggling of inside/outside at every single! intersection point of an edge // with the horizontal line through inPt, left of inPt // not counting lowerY endpoints of edges and whole edges on that line let inside = false; for ( let p = polyLen - 1, q = 0; q < polyLen; p = q ++ ) { let edgeLowPt = inPolygon[ p ]; let edgeHighPt = inPolygon[ q ]; let edgeDx = edgeHighPt.x - edgeLowPt.x; let edgeDy = edgeHighPt.y - edgeLowPt.y; if ( Math.abs( edgeDy ) > Number.EPSILON ) { // not parallel if ( edgeDy < 0 ) { edgeLowPt = inPolygon[ q ]; edgeDx = - edgeDx; edgeHighPt = inPolygon[ p ]; edgeDy = - edgeDy; } if ( ( inPt.y < edgeLowPt.y ) || ( inPt.y > edgeHighPt.y ) ) continue; if ( inPt.y === edgeLowPt.y ) { if ( inPt.x === edgeLowPt.x ) return true; // inPt is on contour ? // continue; // no intersection or edgeLowPt => doesn't count !!! } else { const perpEdge = edgeDy * ( inPt.x - edgeLowPt.x ) - edgeDx * ( inPt.y - edgeLowPt.y ); if ( perpEdge === 0 ) return true; // inPt is on contour ? if ( perpEdge < 0 ) continue; inside = ! inside; // true intersection left of inPt } } else { // parallel or collinear if ( inPt.y !== edgeLowPt.y ) continue; // parallel // edge lies on the same horizontal line as inPt if ( ( ( edgeHighPt.x <= inPt.x ) && ( inPt.x <= edgeLowPt.x ) ) || ( ( edgeLowPt.x <= inPt.x ) && ( inPt.x <= edgeHighPt.x ) ) ) return true; // inPt: Point on contour ! // continue; } } return inside; } const isClockWise = ShapeUtils.isClockWise; const subPaths = this.subPaths; if ( subPaths.length === 0 ) return []; if ( noHoles === true ) return toShapesNoHoles( subPaths ); let solid, tmpPath, tmpShape; const shapes = []; if ( subPaths.length === 1 ) { tmpPath = subPaths[ 0 ]; tmpShape = new Shape(); tmpShape.curves = tmpPath.curves; shapes.push( tmpShape ); return shapes; } let holesFirst = ! isClockWise( subPaths[ 0 ].getPoints() ); holesFirst = isCCW ? ! holesFirst : holesFirst; // console.log("Holes first", holesFirst); const betterShapeHoles = []; const newShapes = []; let newShapeHoles = []; let mainIdx = 0; let tmpPoints; newShapes[ mainIdx ] = undefined; newShapeHoles[ mainIdx ] = []; for ( let i = 0, l = subPaths.length; i < l; i ++ ) { tmpPath = subPaths[ i ]; tmpPoints = tmpPath.getPoints(); solid = isClockWise( tmpPoints ); solid = isCCW ? ! solid : solid; if ( solid ) { if ( ( ! holesFirst ) && ( newShapes[ mainIdx ] ) ) mainIdx ++; newShapes[ mainIdx ] = { s: new Shape(), p: tmpPoints }; newShapes[ mainIdx ].s.curves = tmpPath.curves; if ( holesFirst ) mainIdx ++; newShapeHoles[ mainIdx ] = []; //console.log('cw', i); } else { newShapeHoles[ mainIdx ].push( { h: tmpPath, p: tmpPoints[ 0 ] } ); //console.log('ccw', i); } } // only Holes? -> probably all Shapes with wrong orientation if ( ! newShapes[ 0 ] ) return toShapesNoHoles( subPaths ); if ( newShapes.length > 1 ) { let ambiguous = false; const toChange = []; for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) { betterShapeHoles[ sIdx ] = []; } for ( let sIdx = 0, sLen = newShapes.length; sIdx < sLen; sIdx ++ ) { const sho = newShapeHoles[ sIdx ]; for ( let hIdx = 0; hIdx < sho.length; hIdx ++ ) { const ho = sho[ hIdx ]; let hole_unassigned = true; for ( let s2Idx = 0; s2Idx < newShapes.length; s2Idx ++ ) { if ( isPointInsidePolygon( ho.p, newShapes[ s2Idx ].p ) ) { if ( sIdx !== s2Idx ) toChange.push( { froms: sIdx, tos: s2Idx, hole: hIdx } ); if ( hole_unassigned ) { hole_unassigned = false; betterShapeHoles[ s2Idx ].push( ho ); } else { ambiguous = true; } } } if ( hole_unassigned ) { betterShapeHoles[ sIdx ].push( ho ); } } } // console.log("ambiguous: ", ambiguous); if ( toChange.length > 0 ) { // console.log("to change: ", toChange); if ( ! ambiguous ) newShapeHoles = betterShapeHoles; } } let tmpHoles; for ( let i = 0, il = newShapes.length; i < il; i ++ ) { tmpShape = newShapes[ i ].s; shapes.push( tmpShape ); tmpHoles = newShapeHoles[ i ]; for ( let j = 0, jl = tmpHoles.length; j < jl; j ++ ) { tmpShape.holes.push( tmpHoles[ j ].h ); } } //console.log("shape", shapes); return shapes; } } ); function Font( data ) { this.type = 'Font'; this.data = data; } Object.assign( Font.prototype, { isFont: true, generateShapes: function ( text, size = 100 ) { const shapes = []; const paths = createPaths( text, size, this.data ); for ( let p = 0, pl = paths.length; p < pl; p ++ ) { Array.prototype.push.apply( shapes, paths[ p ].toShapes() ); } return shapes; } } ); function createPaths( text, size, data ) { const chars = Array.from ? Array.from( text ) : String( text ).split( '' ); // workaround for IE11, see #13988 const scale = size / data.resolution; const line_height = ( data.boundingBox.yMax - data.boundingBox.yMin + data.underlineThickness ) * scale; const paths = []; let offsetX = 0, offsetY = 0; for ( let i = 0; i < chars.length; i ++ ) { const char = chars[ i ]; if ( char === '\n' ) { offsetX = 0; offsetY -= line_height; } else { const ret = createPath( char, scale, offsetX, offsetY, data ); offsetX += ret.offsetX; paths.push( ret.path ); } } return paths; } function createPath( char, scale, offsetX, offsetY, data ) { const glyph = data.glyphs[ char ] || data.glyphs[ '?' ]; if ( ! glyph ) { console.error( 'THREE.Font: character "' + char + '" does not exists in font family ' + data.familyName + '.' ); return; } const path = new ShapePath(); let x, y, cpx, cpy, cpx1, cpy1, cpx2, cpy2; if ( glyph.o ) { const outline = glyph._cachedOutline || ( glyph._cachedOutline = glyph.o.split( ' ' ) ); for ( let i = 0, l = outline.length; i < l; ) { const action = outline[ i ++ ]; switch ( action ) { case 'm': // moveTo x = outline[ i ++ ] * scale + offsetX; y = outline[ i ++ ] * scale + offsetY; path.moveTo( x, y ); break; case 'l': // lineTo x = outline[ i ++ ] * scale + offsetX; y = outline[ i ++ ] * scale + offsetY; path.lineTo( x, y ); break; case 'q': // quadraticCurveTo cpx = outline[ i ++ ] * scale + offsetX; cpy = outline[ i ++ ] * scale + offsetY; cpx1 = outline[ i ++ ] * scale + offsetX; cpy1 = outline[ i ++ ] * scale + offsetY; path.quadraticCurveTo( cpx1, cpy1, cpx, cpy ); break; case 'b': // bezierCurveTo cpx = outline[ i ++ ] * scale + offsetX; cpy = outline[ i ++ ] * scale + offsetY; cpx1 = outline[ i ++ ] * scale + offsetX; cpy1 = outline[ i ++ ] * scale + offsetY; cpx2 = outline[ i ++ ] * scale + offsetX; cpy2 = outline[ i ++ ] * scale + offsetY; path.bezierCurveTo( cpx1, cpy1, cpx2, cpy2, cpx, cpy ); break; } } } return { offsetX: glyph.ha * scale, path: path }; } function FontLoader( manager ) { Loader.call( this, manager ); } FontLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: FontLoader, load: function ( url, onLoad, onProgress, onError ) { const scope = this; const loader = new FileLoader( this.manager ); loader.setPath( this.path ); loader.setRequestHeader( this.requestHeader ); loader.setWithCredentials( scope.withCredentials ); loader.load( url, function ( text ) { let json; try { json = JSON.parse( text ); } catch ( e ) { console.warn( 'THREE.FontLoader: typeface.js support is being deprecated. Use typeface.json instead.' ); json = JSON.parse( text.substring( 65, text.length - 2 ) ); } const font = scope.parse( json ); if ( onLoad ) onLoad( font ); }, onProgress, onError ); }, parse: function ( json ) { return new Font( json ); } } ); let _context; const AudioContext = { getContext: function () { if ( _context === undefined ) { _context = new ( window.AudioContext || window.webkitAudioContext )(); } return _context; }, setContext: function ( value ) { _context = value; } }; function AudioLoader( manager ) { Loader.call( this, manager ); } AudioLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: AudioLoader, load: function ( url, onLoad, onProgress, onError ) { const scope = this; const loader = new FileLoader( scope.manager ); loader.setResponseType( 'arraybuffer' ); loader.setPath( scope.path ); loader.setRequestHeader( scope.requestHeader ); loader.setWithCredentials( scope.withCredentials ); loader.load( url, function ( buffer ) { try { // Create a copy of the buffer. The `decodeAudioData` method // detaches the buffer when complete, preventing reuse. const bufferCopy = buffer.slice( 0 ); const context = AudioContext.getContext(); context.decodeAudioData( bufferCopy, function ( audioBuffer ) { onLoad( audioBuffer ); } ); } catch ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); } }, onProgress, onError ); } } ); function HemisphereLightProbe( skyColor, groundColor, intensity ) { LightProbe.call( this, undefined, intensity ); const color1 = new Color().set( skyColor ); const color2 = new Color().set( groundColor ); const sky = new Vector3( color1.r, color1.g, color1.b ); const ground = new Vector3( color2.r, color2.g, color2.b ); // without extra factor of PI in the shader, should = 1 / Math.sqrt( Math.PI ); const c0 = Math.sqrt( Math.PI ); const c1 = c0 * Math.sqrt( 0.75 ); this.sh.coefficients[ 0 ].copy( sky ).add( ground ).multiplyScalar( c0 ); this.sh.coefficients[ 1 ].copy( sky ).sub( ground ).multiplyScalar( c1 ); } HemisphereLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), { constructor: HemisphereLightProbe, isHemisphereLightProbe: true, copy: function ( source ) { // modifying colors not currently supported LightProbe.prototype.copy.call( this, source ); return this; }, toJSON: function ( meta ) { const data = LightProbe.prototype.toJSON.call( this, meta ); // data.sh = this.sh.toArray(); // todo return data; } } ); function AmbientLightProbe( color, intensity ) { LightProbe.call( this, undefined, intensity ); const color1 = new Color().set( color ); // without extra factor of PI in the shader, would be 2 / Math.sqrt( Math.PI ); this.sh.coefficients[ 0 ].set( color1.r, color1.g, color1.b ).multiplyScalar( 2 * Math.sqrt( Math.PI ) ); } AmbientLightProbe.prototype = Object.assign( Object.create( LightProbe.prototype ), { constructor: AmbientLightProbe, isAmbientLightProbe: true, copy: function ( source ) { // modifying color not currently supported LightProbe.prototype.copy.call( this, source ); return this; }, toJSON: function ( meta ) { const data = LightProbe.prototype.toJSON.call( this, meta ); // data.sh = this.sh.toArray(); // todo return data; } } ); const _eyeRight = new Matrix4(); const _eyeLeft = new Matrix4(); function StereoCamera() { this.type = 'StereoCamera'; this.aspect = 1; this.eyeSep = 0.064; this.cameraL = new PerspectiveCamera(); this.cameraL.layers.enable( 1 ); this.cameraL.matrixAutoUpdate = false; this.cameraR = new PerspectiveCamera(); this.cameraR.layers.enable( 2 ); this.cameraR.matrixAutoUpdate = false; this._cache = { focus: null, fov: null, aspect: null, near: null, far: null, zoom: null, eyeSep: null }; } Object.assign( StereoCamera.prototype, { update: function ( camera ) { const cache = this._cache; const needsUpdate = cache.focus !== camera.focus || cache.fov !== camera.fov || cache.aspect !== camera.aspect * this.aspect || cache.near !== camera.near || cache.far !== camera.far || cache.zoom !== camera.zoom || cache.eyeSep !== this.eyeSep; if ( needsUpdate ) { cache.focus = camera.focus; cache.fov = camera.fov; cache.aspect = camera.aspect * this.aspect; cache.near = camera.near; cache.far = camera.far; cache.zoom = camera.zoom; cache.eyeSep = this.eyeSep; // Off-axis stereoscopic effect based on // http://paulbourke.net/stereographics/stereorender/ const projectionMatrix = camera.projectionMatrix.clone(); const eyeSepHalf = cache.eyeSep / 2; const eyeSepOnProjection = eyeSepHalf * cache.near / cache.focus; const ymax = ( cache.near * Math.tan( MathUtils.DEG2RAD * cache.fov * 0.5 ) ) / cache.zoom; let xmin, xmax; // translate xOffset _eyeLeft.elements[ 12 ] = - eyeSepHalf; _eyeRight.elements[ 12 ] = eyeSepHalf; // for left eye xmin = - ymax * cache.aspect + eyeSepOnProjection; xmax = ymax * cache.aspect + eyeSepOnProjection; projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin ); projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin ); this.cameraL.projectionMatrix.copy( projectionMatrix ); // for right eye xmin = - ymax * cache.aspect - eyeSepOnProjection; xmax = ymax * cache.aspect - eyeSepOnProjection; projectionMatrix.elements[ 0 ] = 2 * cache.near / ( xmax - xmin ); projectionMatrix.elements[ 8 ] = ( xmax + xmin ) / ( xmax - xmin ); this.cameraR.projectionMatrix.copy( projectionMatrix ); } this.cameraL.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeLeft ); this.cameraR.matrixWorld.copy( camera.matrixWorld ).multiply( _eyeRight ); } } ); class Clock { constructor( autoStart ) { this.autoStart = ( autoStart !== undefined ) ? autoStart : true; this.startTime = 0; this.oldTime = 0; this.elapsedTime = 0; this.running = false; } start() { this.startTime = now(); this.oldTime = this.startTime; this.elapsedTime = 0; this.running = true; } stop() { this.getElapsedTime(); this.running = false; this.autoStart = false; } getElapsedTime() { this.getDelta(); return this.elapsedTime; } getDelta() { let diff = 0; if ( this.autoStart && ! this.running ) { this.start(); return 0; } if ( this.running ) { const newTime = now(); diff = ( newTime - this.oldTime ) / 1000; this.oldTime = newTime; this.elapsedTime += diff; } return diff; } } function now() { return ( typeof performance === 'undefined' ? Date : performance ).now(); // see #10732 } const _position$2 = /*@__PURE__*/ new Vector3(); const _quaternion$3 = /*@__PURE__*/ new Quaternion(); const _scale$1 = /*@__PURE__*/ new Vector3(); const _orientation = /*@__PURE__*/ new Vector3(); class AudioListener extends Object3D { constructor() { super(); this.type = 'AudioListener'; this.context = AudioContext.getContext(); this.gain = this.context.createGain(); this.gain.connect( this.context.destination ); this.filter = null; this.timeDelta = 0; // private this._clock = new Clock(); } getInput() { return this.gain; } removeFilter() { if ( this.filter !== null ) { this.gain.disconnect( this.filter ); this.filter.disconnect( this.context.destination ); this.gain.connect( this.context.destination ); this.filter = null; } return this; } getFilter() { return this.filter; } setFilter( value ) { if ( this.filter !== null ) { this.gain.disconnect( this.filter ); this.filter.disconnect( this.context.destination ); } else { this.gain.disconnect( this.context.destination ); } this.filter = value; this.gain.connect( this.filter ); this.filter.connect( this.context.destination ); return this; } getMasterVolume() { return this.gain.gain.value; } setMasterVolume( value ) { this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 ); return this; } updateMatrixWorld( force ) { super.updateMatrixWorld( force ); const listener = this.context.listener; const up = this.up; this.timeDelta = this._clock.getDelta(); this.matrixWorld.decompose( _position$2, _quaternion$3, _scale$1 ); _orientation.set( 0, 0, - 1 ).applyQuaternion( _quaternion$3 ); if ( listener.positionX ) { // code path for Chrome (see #14393) const endTime = this.context.currentTime + this.timeDelta; listener.positionX.linearRampToValueAtTime( _position$2.x, endTime ); listener.positionY.linearRampToValueAtTime( _position$2.y, endTime ); listener.positionZ.linearRampToValueAtTime( _position$2.z, endTime ); listener.forwardX.linearRampToValueAtTime( _orientation.x, endTime ); listener.forwardY.linearRampToValueAtTime( _orientation.y, endTime ); listener.forwardZ.linearRampToValueAtTime( _orientation.z, endTime ); listener.upX.linearRampToValueAtTime( up.x, endTime ); listener.upY.linearRampToValueAtTime( up.y, endTime ); listener.upZ.linearRampToValueAtTime( up.z, endTime ); } else { listener.setPosition( _position$2.x, _position$2.y, _position$2.z ); listener.setOrientation( _orientation.x, _orientation.y, _orientation.z, up.x, up.y, up.z ); } } } class Audio extends Object3D { constructor( listener ) { super(); this.type = 'Audio'; this.listener = listener; this.context = listener.context; this.gain = this.context.createGain(); this.gain.connect( listener.getInput() ); this.autoplay = false; this.buffer = null; this.detune = 0; this.loop = false; this.loopStart = 0; this.loopEnd = 0; this.offset = 0; this.duration = undefined; this.playbackRate = 1; this.isPlaying = false; this.hasPlaybackControl = true; this.source = null; this.sourceType = 'empty'; this._startedAt = 0; this._progress = 0; this._connected = false; this.filters = []; } getOutput() { return this.gain; } setNodeSource( audioNode ) { this.hasPlaybackControl = false; this.sourceType = 'audioNode'; this.source = audioNode; this.connect(); return this; } setMediaElementSource( mediaElement ) { this.hasPlaybackControl = false; this.sourceType = 'mediaNode'; this.source = this.context.createMediaElementSource( mediaElement ); this.connect(); return this; } setMediaStreamSource( mediaStream ) { this.hasPlaybackControl = false; this.sourceType = 'mediaStreamNode'; this.source = this.context.createMediaStreamSource( mediaStream ); this.connect(); return this; } setBuffer( audioBuffer ) { this.buffer = audioBuffer; this.sourceType = 'buffer'; if ( this.autoplay ) this.play(); return this; } play( delay = 0 ) { if ( this.isPlaying === true ) { console.warn( 'THREE.Audio: Audio is already playing.' ); return; } if ( this.hasPlaybackControl === false ) { console.warn( 'THREE.Audio: this Audio has no playback control.' ); return; } this._startedAt = this.context.currentTime + delay; const source = this.context.createBufferSource(); source.buffer = this.buffer; source.loop = this.loop; source.loopStart = this.loopStart; source.loopEnd = this.loopEnd; source.onended = this.onEnded.bind( this ); source.start( this._startedAt, this._progress + this.offset, this.duration ); this.isPlaying = true; this.source = source; this.setDetune( this.detune ); this.setPlaybackRate( this.playbackRate ); return this.connect(); } pause() { if ( this.hasPlaybackControl === false ) { console.warn( 'THREE.Audio: this Audio has no playback control.' ); return; } if ( this.isPlaying === true ) { // update current progress this._progress += Math.max( this.context.currentTime - this._startedAt, 0 ) * this.playbackRate; if ( this.loop === true ) { // ensure _progress does not exceed duration with looped audios this._progress = this._progress % ( this.duration || this.buffer.duration ); } this.source.stop(); this.source.onended = null; this.isPlaying = false; } return this; } stop() { if ( this.hasPlaybackControl === false ) { console.warn( 'THREE.Audio: this Audio has no playback control.' ); return; } this._progress = 0; this.source.stop(); this.source.onended = null; this.isPlaying = false; return this; } connect() { if ( this.filters.length > 0 ) { this.source.connect( this.filters[ 0 ] ); for ( let i = 1, l = this.filters.length; i < l; i ++ ) { this.filters[ i - 1 ].connect( this.filters[ i ] ); } this.filters[ this.filters.length - 1 ].connect( this.getOutput() ); } else { this.source.connect( this.getOutput() ); } this._connected = true; return this; } disconnect() { if ( this.filters.length > 0 ) { this.source.disconnect( this.filters[ 0 ] ); for ( let i = 1, l = this.filters.length; i < l; i ++ ) { this.filters[ i - 1 ].disconnect( this.filters[ i ] ); } this.filters[ this.filters.length - 1 ].disconnect( this.getOutput() ); } else { this.source.disconnect( this.getOutput() ); } this._connected = false; return this; } getFilters() { return this.filters; } setFilters( value ) { if ( ! value ) value = []; if ( this._connected === true ) { this.disconnect(); this.filters = value.slice(); this.connect(); } else { this.filters = value.slice(); } return this; } setDetune( value ) { this.detune = value; if ( this.source.detune === undefined ) return; // only set detune when available if ( this.isPlaying === true ) { this.source.detune.setTargetAtTime( this.detune, this.context.currentTime, 0.01 ); } return this; } getDetune() { return this.detune; } getFilter() { return this.getFilters()[ 0 ]; } setFilter( filter ) { return this.setFilters( filter ? [ filter ] : [] ); } setPlaybackRate( value ) { if ( this.hasPlaybackControl === false ) { console.warn( 'THREE.Audio: this Audio has no playback control.' ); return; } this.playbackRate = value; if ( this.isPlaying === true ) { this.source.playbackRate.setTargetAtTime( this.playbackRate, this.context.currentTime, 0.01 ); } return this; } getPlaybackRate() { return this.playbackRate; } onEnded() { this.isPlaying = false; } getLoop() { if ( this.hasPlaybackControl === false ) { console.warn( 'THREE.Audio: this Audio has no playback control.' ); return false; } return this.loop; } setLoop( value ) { if ( this.hasPlaybackControl === false ) { console.warn( 'THREE.Audio: this Audio has no playback control.' ); return; } this.loop = value; if ( this.isPlaying === true ) { this.source.loop = this.loop; } return this; } setLoopStart( value ) { this.loopStart = value; return this; } setLoopEnd( value ) { this.loopEnd = value; return this; } getVolume() { return this.gain.gain.value; } setVolume( value ) { this.gain.gain.setTargetAtTime( value, this.context.currentTime, 0.01 ); return this; } } const _position$3 = /*@__PURE__*/ new Vector3(); const _quaternion$4 = /*@__PURE__*/ new Quaternion(); const _scale$2 = /*@__PURE__*/ new Vector3(); const _orientation$1 = /*@__PURE__*/ new Vector3(); class PositionalAudio extends Audio { constructor( listener ) { super( listener ); this.panner = this.context.createPanner(); this.panner.panningModel = 'HRTF'; this.panner.connect( this.gain ); } getOutput() { return this.panner; } getRefDistance() { return this.panner.refDistance; } setRefDistance( value ) { this.panner.refDistance = value; return this; } getRolloffFactor() { return this.panner.rolloffFactor; } setRolloffFactor( value ) { this.panner.rolloffFactor = value; return this; } getDistanceModel() { return this.panner.distanceModel; } setDistanceModel( value ) { this.panner.distanceModel = value; return this; } getMaxDistance() { return this.panner.maxDistance; } setMaxDistance( value ) { this.panner.maxDistance = value; return this; } setDirectionalCone( coneInnerAngle, coneOuterAngle, coneOuterGain ) { this.panner.coneInnerAngle = coneInnerAngle; this.panner.coneOuterAngle = coneOuterAngle; this.panner.coneOuterGain = coneOuterGain; return this; } updateMatrixWorld( force ) { super.updateMatrixWorld( force ); if ( this.hasPlaybackControl === true && this.isPlaying === false ) return; this.matrixWorld.decompose( _position$3, _quaternion$4, _scale$2 ); _orientation$1.set( 0, 0, 1 ).applyQuaternion( _quaternion$4 ); const panner = this.panner; if ( panner.positionX ) { // code path for Chrome and Firefox (see #14393) const endTime = this.context.currentTime + this.listener.timeDelta; panner.positionX.linearRampToValueAtTime( _position$3.x, endTime ); panner.positionY.linearRampToValueAtTime( _position$3.y, endTime ); panner.positionZ.linearRampToValueAtTime( _position$3.z, endTime ); panner.orientationX.linearRampToValueAtTime( _orientation$1.x, endTime ); panner.orientationY.linearRampToValueAtTime( _orientation$1.y, endTime ); panner.orientationZ.linearRampToValueAtTime( _orientation$1.z, endTime ); } else { panner.setPosition( _position$3.x, _position$3.y, _position$3.z ); panner.setOrientation( _orientation$1.x, _orientation$1.y, _orientation$1.z ); } } } class AudioAnalyser { constructor( audio, fftSize = 2048 ) { this.analyser = audio.context.createAnalyser(); this.analyser.fftSize = fftSize; this.data = new Uint8Array( this.analyser.frequencyBinCount ); audio.getOutput().connect( this.analyser ); } getFrequencyData() { this.analyser.getByteFrequencyData( this.data ); return this.data; } getAverageFrequency() { let value = 0; const data = this.getFrequencyData(); for ( let i = 0; i < data.length; i ++ ) { value += data[ i ]; } return value / data.length; } } function PropertyMixer( binding, typeName, valueSize ) { this.binding = binding; this.valueSize = valueSize; let mixFunction, mixFunctionAdditive, setIdentity; // buffer layout: [ incoming | accu0 | accu1 | orig | addAccu | (optional work) ] // // interpolators can use .buffer as their .result // the data then goes to 'incoming' // // 'accu0' and 'accu1' are used frame-interleaved for // the cumulative result and are compared to detect // changes // // 'orig' stores the original state of the property // // 'add' is used for additive cumulative results // // 'work' is optional and is only present for quaternion types. It is used // to store intermediate quaternion multiplication results switch ( typeName ) { case 'quaternion': mixFunction = this._slerp; mixFunctionAdditive = this._slerpAdditive; setIdentity = this._setAdditiveIdentityQuaternion; this.buffer = new Float64Array( valueSize * 6 ); this._workIndex = 5; break; case 'string': case 'bool': mixFunction = this._select; // Use the regular mix function and for additive on these types, // additive is not relevant for non-numeric types mixFunctionAdditive = this._select; setIdentity = this._setAdditiveIdentityOther; this.buffer = new Array( valueSize * 5 ); break; default: mixFunction = this._lerp; mixFunctionAdditive = this._lerpAdditive; setIdentity = this._setAdditiveIdentityNumeric; this.buffer = new Float64Array( valueSize * 5 ); } this._mixBufferRegion = mixFunction; this._mixBufferRegionAdditive = mixFunctionAdditive; this._setIdentity = setIdentity; this._origIndex = 3; this._addIndex = 4; this.cumulativeWeight = 0; this.cumulativeWeightAdditive = 0; this.useCount = 0; this.referenceCount = 0; } Object.assign( PropertyMixer.prototype, { // accumulate data in the 'incoming' region into 'accu' accumulate: function ( accuIndex, weight ) { // note: happily accumulating nothing when weight = 0, the caller knows // the weight and shouldn't have made the call in the first place const buffer = this.buffer, stride = this.valueSize, offset = accuIndex * stride + stride; let currentWeight = this.cumulativeWeight; if ( currentWeight === 0 ) { // accuN := incoming * weight for ( let i = 0; i !== stride; ++ i ) { buffer[ offset + i ] = buffer[ i ]; } currentWeight = weight; } else { // accuN := accuN + incoming * weight currentWeight += weight; const mix = weight / currentWeight; this._mixBufferRegion( buffer, offset, 0, mix, stride ); } this.cumulativeWeight = currentWeight; }, // accumulate data in the 'incoming' region into 'add' accumulateAdditive: function ( weight ) { const buffer = this.buffer, stride = this.valueSize, offset = stride * this._addIndex; if ( this.cumulativeWeightAdditive === 0 ) { // add = identity this._setIdentity(); } // add := add + incoming * weight this._mixBufferRegionAdditive( buffer, offset, 0, weight, stride ); this.cumulativeWeightAdditive += weight; }, // apply the state of 'accu' to the binding when accus differ apply: function ( accuIndex ) { const stride = this.valueSize, buffer = this.buffer, offset = accuIndex * stride + stride, weight = this.cumulativeWeight, weightAdditive = this.cumulativeWeightAdditive, binding = this.binding; this.cumulativeWeight = 0; this.cumulativeWeightAdditive = 0; if ( weight < 1 ) { // accuN := accuN + original * ( 1 - cumulativeWeight ) const originalValueOffset = stride * this._origIndex; this._mixBufferRegion( buffer, offset, originalValueOffset, 1 - weight, stride ); } if ( weightAdditive > 0 ) { // accuN := accuN + additive accuN this._mixBufferRegionAdditive( buffer, offset, this._addIndex * stride, 1, stride ); } for ( let i = stride, e = stride + stride; i !== e; ++ i ) { if ( buffer[ i ] !== buffer[ i + stride ] ) { // value has changed -> update scene graph binding.setValue( buffer, offset ); break; } } }, // remember the state of the bound property and copy it to both accus saveOriginalState: function () { const binding = this.binding; const buffer = this.buffer, stride = this.valueSize, originalValueOffset = stride * this._origIndex; binding.getValue( buffer, originalValueOffset ); // accu[0..1] := orig -- initially detect changes against the original for ( let i = stride, e = originalValueOffset; i !== e; ++ i ) { buffer[ i ] = buffer[ originalValueOffset + ( i % stride ) ]; } // Add to identity for additive this._setIdentity(); this.cumulativeWeight = 0; this.cumulativeWeightAdditive = 0; }, // apply the state previously taken via 'saveOriginalState' to the binding restoreOriginalState: function () { const originalValueOffset = this.valueSize * 3; this.binding.setValue( this.buffer, originalValueOffset ); }, _setAdditiveIdentityNumeric: function () { const startIndex = this._addIndex * this.valueSize; const endIndex = startIndex + this.valueSize; for ( let i = startIndex; i < endIndex; i ++ ) { this.buffer[ i ] = 0; } }, _setAdditiveIdentityQuaternion: function () { this._setAdditiveIdentityNumeric(); this.buffer[ this._addIndex * this.valueSize + 3 ] = 1; }, _setAdditiveIdentityOther: function () { const startIndex = this._origIndex * this.valueSize; const targetIndex = this._addIndex * this.valueSize; for ( let i = 0; i < this.valueSize; i ++ ) { this.buffer[ targetIndex + i ] = this.buffer[ startIndex + i ]; } }, // mix functions _select: function ( buffer, dstOffset, srcOffset, t, stride ) { if ( t >= 0.5 ) { for ( let i = 0; i !== stride; ++ i ) { buffer[ dstOffset + i ] = buffer[ srcOffset + i ]; } } }, _slerp: function ( buffer, dstOffset, srcOffset, t ) { Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, srcOffset, t ); }, _slerpAdditive: function ( buffer, dstOffset, srcOffset, t, stride ) { const workOffset = this._workIndex * stride; // Store result in intermediate buffer offset Quaternion.multiplyQuaternionsFlat( buffer, workOffset, buffer, dstOffset, buffer, srcOffset ); // Slerp to the intermediate result Quaternion.slerpFlat( buffer, dstOffset, buffer, dstOffset, buffer, workOffset, t ); }, _lerp: function ( buffer, dstOffset, srcOffset, t, stride ) { const s = 1 - t; for ( let i = 0; i !== stride; ++ i ) { const j = dstOffset + i; buffer[ j ] = buffer[ j ] * s + buffer[ srcOffset + i ] * t; } }, _lerpAdditive: function ( buffer, dstOffset, srcOffset, t, stride ) { for ( let i = 0; i !== stride; ++ i ) { const j = dstOffset + i; buffer[ j ] = buffer[ j ] + buffer[ srcOffset + i ] * t; } } } ); // Characters [].:/ are reserved for track binding syntax. const _RESERVED_CHARS_RE = '\\[\\]\\.:\\/'; const _reservedRe = new RegExp( '[' + _RESERVED_CHARS_RE + ']', 'g' ); // Attempts to allow node names from any language. ES5's `\w` regexp matches // only latin characters, and the unicode \p{L} is not yet supported. So // instead, we exclude reserved characters and match everything else. const _wordChar = '[^' + _RESERVED_CHARS_RE + ']'; const _wordCharOrDot = '[^' + _RESERVED_CHARS_RE.replace( '\\.', '' ) + ']'; // Parent directories, delimited by '/' or ':'. Currently unused, but must // be matched to parse the rest of the track name. const _directoryRe = /((?:WC+[\/:])*)/.source.replace( 'WC', _wordChar ); // Target node. May contain word characters (a-zA-Z0-9_) and '.' or '-'. const _nodeRe = /(WCOD+)?/.source.replace( 'WCOD', _wordCharOrDot ); // Object on target node, and accessor. May not contain reserved // characters. Accessor may contain any character except closing bracket. const _objectRe = /(?:\.(WC+)(?:\[(.+)\])?)?/.source.replace( 'WC', _wordChar ); // Property and accessor. May not contain reserved characters. Accessor may // contain any non-bracket characters. const _propertyRe = /\.(WC+)(?:\[(.+)\])?/.source.replace( 'WC', _wordChar ); const _trackRe = new RegExp( '' + '^' + _directoryRe + _nodeRe + _objectRe + _propertyRe + '$' ); const _supportedObjectNames = [ 'material', 'materials', 'bones' ]; function Composite( targetGroup, path, optionalParsedPath ) { const parsedPath = optionalParsedPath || PropertyBinding.parseTrackName( path ); this._targetGroup = targetGroup; this._bindings = targetGroup.subscribe_( path, parsedPath ); } Object.assign( Composite.prototype, { getValue: function ( array, offset ) { this.bind(); // bind all binding const firstValidIndex = this._targetGroup.nCachedObjects_, binding = this._bindings[ firstValidIndex ]; // and only call .getValue on the first if ( binding !== undefined ) binding.getValue( array, offset ); }, setValue: function ( array, offset ) { const bindings = this._bindings; for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) { bindings[ i ].setValue( array, offset ); } }, bind: function () { const bindings = this._bindings; for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) { bindings[ i ].bind(); } }, unbind: function () { const bindings = this._bindings; for ( let i = this._targetGroup.nCachedObjects_, n = bindings.length; i !== n; ++ i ) { bindings[ i ].unbind(); } } } ); function PropertyBinding( rootNode, path, parsedPath ) { this.path = path; this.parsedPath = parsedPath || PropertyBinding.parseTrackName( path ); this.node = PropertyBinding.findNode( rootNode, this.parsedPath.nodeName ) || rootNode; this.rootNode = rootNode; } Object.assign( PropertyBinding, { Composite: Composite, create: function ( root, path, parsedPath ) { if ( ! ( root && root.isAnimationObjectGroup ) ) { return new PropertyBinding( root, path, parsedPath ); } else { return new PropertyBinding.Composite( root, path, parsedPath ); } }, /** * Replaces spaces with underscores and removes unsupported characters from * node names, to ensure compatibility with parseTrackName(). * * @param {string} name Node name to be sanitized. * @return {string} */ sanitizeNodeName: function ( name ) { return name.replace( /\s/g, '_' ).replace( _reservedRe, '' ); }, parseTrackName: function ( trackName ) { const matches = _trackRe.exec( trackName ); if ( ! matches ) { throw new Error( 'PropertyBinding: Cannot parse trackName: ' + trackName ); } const results = { // directoryName: matches[ 1 ], // (tschw) currently unused nodeName: matches[ 2 ], objectName: matches[ 3 ], objectIndex: matches[ 4 ], propertyName: matches[ 5 ], // required propertyIndex: matches[ 6 ] }; const lastDot = results.nodeName && results.nodeName.lastIndexOf( '.' ); if ( lastDot !== undefined && lastDot !== - 1 ) { const objectName = results.nodeName.substring( lastDot + 1 ); // Object names must be checked against an allowlist. Otherwise, there // is no way to parse 'foo.bar.baz': 'baz' must be a property, but // 'bar' could be the objectName, or part of a nodeName (which can // include '.' characters). if ( _supportedObjectNames.indexOf( objectName ) !== - 1 ) { results.nodeName = results.nodeName.substring( 0, lastDot ); results.objectName = objectName; } } if ( results.propertyName === null || results.propertyName.length === 0 ) { throw new Error( 'PropertyBinding: can not parse propertyName from trackName: ' + trackName ); } return results; }, findNode: function ( root, nodeName ) { if ( ! nodeName || nodeName === '' || nodeName === '.' || nodeName === - 1 || nodeName === root.name || nodeName === root.uuid ) { return root; } // search into skeleton bones. if ( root.skeleton ) { const bone = root.skeleton.getBoneByName( nodeName ); if ( bone !== undefined ) { return bone; } } // search into node subtree. if ( root.children ) { const searchNodeSubtree = function ( children ) { for ( let i = 0; i < children.length; i ++ ) { const childNode = children[ i ]; if ( childNode.name === nodeName || childNode.uuid === nodeName ) { return childNode; } const result = searchNodeSubtree( childNode.children ); if ( result ) return result; } return null; }; const subTreeNode = searchNodeSubtree( root.children ); if ( subTreeNode ) { return subTreeNode; } } return null; } } ); Object.assign( PropertyBinding.prototype, { // prototype, continued // these are used to "bind" a nonexistent property _getValue_unavailable: function () {}, _setValue_unavailable: function () {}, BindingType: { Direct: 0, EntireArray: 1, ArrayElement: 2, HasFromToArray: 3 }, Versioning: { None: 0, NeedsUpdate: 1, MatrixWorldNeedsUpdate: 2 }, GetterByBindingType: [ function getValue_direct( buffer, offset ) { buffer[ offset ] = this.node[ this.propertyName ]; }, function getValue_array( buffer, offset ) { const source = this.resolvedProperty; for ( let i = 0, n = source.length; i !== n; ++ i ) { buffer[ offset ++ ] = source[ i ]; } }, function getValue_arrayElement( buffer, offset ) { buffer[ offset ] = this.resolvedProperty[ this.propertyIndex ]; }, function getValue_toArray( buffer, offset ) { this.resolvedProperty.toArray( buffer, offset ); } ], SetterByBindingTypeAndVersioning: [ [ // Direct function setValue_direct( buffer, offset ) { this.targetObject[ this.propertyName ] = buffer[ offset ]; }, function setValue_direct_setNeedsUpdate( buffer, offset ) { this.targetObject[ this.propertyName ] = buffer[ offset ]; this.targetObject.needsUpdate = true; }, function setValue_direct_setMatrixWorldNeedsUpdate( buffer, offset ) { this.targetObject[ this.propertyName ] = buffer[ offset ]; this.targetObject.matrixWorldNeedsUpdate = true; } ], [ // EntireArray function setValue_array( buffer, offset ) { const dest = this.resolvedProperty; for ( let i = 0, n = dest.length; i !== n; ++ i ) { dest[ i ] = buffer[ offset ++ ]; } }, function setValue_array_setNeedsUpdate( buffer, offset ) { const dest = this.resolvedProperty; for ( let i = 0, n = dest.length; i !== n; ++ i ) { dest[ i ] = buffer[ offset ++ ]; } this.targetObject.needsUpdate = true; }, function setValue_array_setMatrixWorldNeedsUpdate( buffer, offset ) { const dest = this.resolvedProperty; for ( let i = 0, n = dest.length; i !== n; ++ i ) { dest[ i ] = buffer[ offset ++ ]; } this.targetObject.matrixWorldNeedsUpdate = true; } ], [ // ArrayElement function setValue_arrayElement( buffer, offset ) { this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ]; }, function setValue_arrayElement_setNeedsUpdate( buffer, offset ) { this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ]; this.targetObject.needsUpdate = true; }, function setValue_arrayElement_setMatrixWorldNeedsUpdate( buffer, offset ) { this.resolvedProperty[ this.propertyIndex ] = buffer[ offset ]; this.targetObject.matrixWorldNeedsUpdate = true; } ], [ // HasToFromArray function setValue_fromArray( buffer, offset ) { this.resolvedProperty.fromArray( buffer, offset ); }, function setValue_fromArray_setNeedsUpdate( buffer, offset ) { this.resolvedProperty.fromArray( buffer, offset ); this.targetObject.needsUpdate = true; }, function setValue_fromArray_setMatrixWorldNeedsUpdate( buffer, offset ) { this.resolvedProperty.fromArray( buffer, offset ); this.targetObject.matrixWorldNeedsUpdate = true; } ] ], getValue: function getValue_unbound( targetArray, offset ) { this.bind(); this.getValue( targetArray, offset ); // Note: This class uses a State pattern on a per-method basis: // 'bind' sets 'this.getValue' / 'setValue' and shadows the // prototype version of these methods with one that represents // the bound state. When the property is not found, the methods // become no-ops. }, setValue: function getValue_unbound( sourceArray, offset ) { this.bind(); this.setValue( sourceArray, offset ); }, // create getter / setter pair for a property in the scene graph bind: function () { let targetObject = this.node; const parsedPath = this.parsedPath; const objectName = parsedPath.objectName; const propertyName = parsedPath.propertyName; let propertyIndex = parsedPath.propertyIndex; if ( ! targetObject ) { targetObject = PropertyBinding.findNode( this.rootNode, parsedPath.nodeName ) || this.rootNode; this.node = targetObject; } // set fail state so we can just 'return' on error this.getValue = this._getValue_unavailable; this.setValue = this._setValue_unavailable; // ensure there is a value node if ( ! targetObject ) { console.error( 'THREE.PropertyBinding: Trying to update node for track: ' + this.path + ' but it wasn\'t found.' ); return; } if ( objectName ) { let objectIndex = parsedPath.objectIndex; // special cases were we need to reach deeper into the hierarchy to get the face materials.... switch ( objectName ) { case 'materials': if ( ! targetObject.material ) { console.error( 'THREE.PropertyBinding: Can not bind to material as node does not have a material.', this ); return; } if ( ! targetObject.material.materials ) { console.error( 'THREE.PropertyBinding: Can not bind to material.materials as node.material does not have a materials array.', this ); return; } targetObject = targetObject.material.materials; break; case 'bones': if ( ! targetObject.skeleton ) { console.error( 'THREE.PropertyBinding: Can not bind to bones as node does not have a skeleton.', this ); return; } // potential future optimization: skip this if propertyIndex is already an integer // and convert the integer string to a true integer. targetObject = targetObject.skeleton.bones; // support resolving morphTarget names into indices. for ( let i = 0; i < targetObject.length; i ++ ) { if ( targetObject[ i ].name === objectIndex ) { objectIndex = i; break; } } break; default: if ( targetObject[ objectName ] === undefined ) { console.error( 'THREE.PropertyBinding: Can not bind to objectName of node undefined.', this ); return; } targetObject = targetObject[ objectName ]; } if ( objectIndex !== undefined ) { if ( targetObject[ objectIndex ] === undefined ) { console.error( 'THREE.PropertyBinding: Trying to bind to objectIndex of objectName, but is undefined.', this, targetObject ); return; } targetObject = targetObject[ objectIndex ]; } } // resolve property const nodeProperty = targetObject[ propertyName ]; if ( nodeProperty === undefined ) { const nodeName = parsedPath.nodeName; console.error( 'THREE.PropertyBinding: Trying to update property for track: ' + nodeName + '.' + propertyName + ' but it wasn\'t found.', targetObject ); return; } // determine versioning scheme let versioning = this.Versioning.None; this.targetObject = targetObject; if ( targetObject.needsUpdate !== undefined ) { // material versioning = this.Versioning.NeedsUpdate; } else if ( targetObject.matrixWorldNeedsUpdate !== undefined ) { // node transform versioning = this.Versioning.MatrixWorldNeedsUpdate; } // determine how the property gets bound let bindingType = this.BindingType.Direct; if ( propertyIndex !== undefined ) { // access a sub element of the property array (only primitives are supported right now) if ( propertyName === 'morphTargetInfluences' ) { // potential optimization, skip this if propertyIndex is already an integer, and convert the integer string to a true integer. // support resolving morphTarget names into indices. if ( ! targetObject.geometry ) { console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.', this ); return; } if ( targetObject.geometry.isBufferGeometry ) { if ( ! targetObject.geometry.morphAttributes ) { console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences because node does not have a geometry.morphAttributes.', this ); return; } if ( targetObject.morphTargetDictionary[ propertyIndex ] !== undefined ) { propertyIndex = targetObject.morphTargetDictionary[ propertyIndex ]; } } else { console.error( 'THREE.PropertyBinding: Can not bind to morphTargetInfluences on THREE.Geometry. Use THREE.BufferGeometry instead.', this ); return; } } bindingType = this.BindingType.ArrayElement; this.resolvedProperty = nodeProperty; this.propertyIndex = propertyIndex; } else if ( nodeProperty.fromArray !== undefined && nodeProperty.toArray !== undefined ) { // must use copy for Object3D.Euler/Quaternion bindingType = this.BindingType.HasFromToArray; this.resolvedProperty = nodeProperty; } else if ( Array.isArray( nodeProperty ) ) { bindingType = this.BindingType.EntireArray; this.resolvedProperty = nodeProperty; } else { this.propertyName = propertyName; } // select getter / setter this.getValue = this.GetterByBindingType[ bindingType ]; this.setValue = this.SetterByBindingTypeAndVersioning[ bindingType ][ versioning ]; }, unbind: function () { this.node = null; // back to the prototype version of getValue / setValue // note: avoiding to mutate the shape of 'this' via 'delete' this.getValue = this._getValue_unbound; this.setValue = this._setValue_unbound; } } ); // DECLARE ALIAS AFTER assign prototype Object.assign( PropertyBinding.prototype, { // initial state of these methods that calls 'bind' _getValue_unbound: PropertyBinding.prototype.getValue, _setValue_unbound: PropertyBinding.prototype.setValue, } ); /** * * A group of objects that receives a shared animation state. * * Usage: * * - Add objects you would otherwise pass as 'root' to the * constructor or the .clipAction method of AnimationMixer. * * - Instead pass this object as 'root'. * * - You can also add and remove objects later when the mixer * is running. * * Note: * * Objects of this class appear as one object to the mixer, * so cache control of the individual objects must be done * on the group. * * Limitation: * * - The animated properties must be compatible among the * all objects in the group. * * - A single property can either be controlled through a * target group or directly, but not both. */ function AnimationObjectGroup() { this.uuid = MathUtils.generateUUID(); // cached objects followed by the active ones this._objects = Array.prototype.slice.call( arguments ); this.nCachedObjects_ = 0; // threshold // note: read by PropertyBinding.Composite const indices = {}; this._indicesByUUID = indices; // for bookkeeping for ( let i = 0, n = arguments.length; i !== n; ++ i ) { indices[ arguments[ i ].uuid ] = i; } this._paths = []; // inside: string this._parsedPaths = []; // inside: { we don't care, here } this._bindings = []; // inside: Array< PropertyBinding > this._bindingsIndicesByPath = {}; // inside: indices in these arrays const scope = this; this.stats = { objects: { get total() { return scope._objects.length; }, get inUse() { return this.total - scope.nCachedObjects_; } }, get bindingsPerObject() { return scope._bindings.length; } }; } Object.assign( AnimationObjectGroup.prototype, { isAnimationObjectGroup: true, add: function () { const objects = this._objects, indicesByUUID = this._indicesByUUID, paths = this._paths, parsedPaths = this._parsedPaths, bindings = this._bindings, nBindings = bindings.length; let knownObject = undefined, nObjects = objects.length, nCachedObjects = this.nCachedObjects_; for ( let i = 0, n = arguments.length; i !== n; ++ i ) { const object = arguments[ i ], uuid = object.uuid; let index = indicesByUUID[ uuid ]; if ( index === undefined ) { // unknown object -> add it to the ACTIVE region index = nObjects ++; indicesByUUID[ uuid ] = index; objects.push( object ); // accounting is done, now do the same for all bindings for ( let j = 0, m = nBindings; j !== m; ++ j ) { bindings[ j ].push( new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ) ); } } else if ( index < nCachedObjects ) { knownObject = objects[ index ]; // move existing object to the ACTIVE region const firstActiveIndex = -- nCachedObjects, lastCachedObject = objects[ firstActiveIndex ]; indicesByUUID[ lastCachedObject.uuid ] = index; objects[ index ] = lastCachedObject; indicesByUUID[ uuid ] = firstActiveIndex; objects[ firstActiveIndex ] = object; // accounting is done, now do the same for all bindings for ( let j = 0, m = nBindings; j !== m; ++ j ) { const bindingsForPath = bindings[ j ], lastCached = bindingsForPath[ firstActiveIndex ]; let binding = bindingsForPath[ index ]; bindingsForPath[ index ] = lastCached; if ( binding === undefined ) { // since we do not bother to create new bindings // for objects that are cached, the binding may // or may not exist binding = new PropertyBinding( object, paths[ j ], parsedPaths[ j ] ); } bindingsForPath[ firstActiveIndex ] = binding; } } else if ( objects[ index ] !== knownObject ) { console.error( 'THREE.AnimationObjectGroup: Different objects with the same UUID ' + 'detected. Clean the caches or recreate your infrastructure when reloading scenes.' ); } // else the object is already where we want it to be } // for arguments this.nCachedObjects_ = nCachedObjects; }, remove: function () { const objects = this._objects, indicesByUUID = this._indicesByUUID, bindings = this._bindings, nBindings = bindings.length; let nCachedObjects = this.nCachedObjects_; for ( let i = 0, n = arguments.length; i !== n; ++ i ) { const object = arguments[ i ], uuid = object.uuid, index = indicesByUUID[ uuid ]; if ( index !== undefined && index >= nCachedObjects ) { // move existing object into the CACHED region const lastCachedIndex = nCachedObjects ++, firstActiveObject = objects[ lastCachedIndex ]; indicesByUUID[ firstActiveObject.uuid ] = index; objects[ index ] = firstActiveObject; indicesByUUID[ uuid ] = lastCachedIndex; objects[ lastCachedIndex ] = object; // accounting is done, now do the same for all bindings for ( let j = 0, m = nBindings; j !== m; ++ j ) { const bindingsForPath = bindings[ j ], firstActive = bindingsForPath[ lastCachedIndex ], binding = bindingsForPath[ index ]; bindingsForPath[ index ] = firstActive; bindingsForPath[ lastCachedIndex ] = binding; } } } // for arguments this.nCachedObjects_ = nCachedObjects; }, // remove & forget uncache: function () { const objects = this._objects, indicesByUUID = this._indicesByUUID, bindings = this._bindings, nBindings = bindings.length; let nCachedObjects = this.nCachedObjects_, nObjects = objects.length; for ( let i = 0, n = arguments.length; i !== n; ++ i ) { const object = arguments[ i ], uuid = object.uuid, index = indicesByUUID[ uuid ]; if ( index !== undefined ) { delete indicesByUUID[ uuid ]; if ( index < nCachedObjects ) { // object is cached, shrink the CACHED region const firstActiveIndex = -- nCachedObjects, lastCachedObject = objects[ firstActiveIndex ], lastIndex = -- nObjects, lastObject = objects[ lastIndex ]; // last cached object takes this object's place indicesByUUID[ lastCachedObject.uuid ] = index; objects[ index ] = lastCachedObject; // last object goes to the activated slot and pop indicesByUUID[ lastObject.uuid ] = firstActiveIndex; objects[ firstActiveIndex ] = lastObject; objects.pop(); // accounting is done, now do the same for all bindings for ( let j = 0, m = nBindings; j !== m; ++ j ) { const bindingsForPath = bindings[ j ], lastCached = bindingsForPath[ firstActiveIndex ], last = bindingsForPath[ lastIndex ]; bindingsForPath[ index ] = lastCached; bindingsForPath[ firstActiveIndex ] = last; bindingsForPath.pop(); } } else { // object is active, just swap with the last and pop const lastIndex = -- nObjects, lastObject = objects[ lastIndex ]; if ( lastIndex > 0 ) { indicesByUUID[ lastObject.uuid ] = index; } objects[ index ] = lastObject; objects.pop(); // accounting is done, now do the same for all bindings for ( let j = 0, m = nBindings; j !== m; ++ j ) { const bindingsForPath = bindings[ j ]; bindingsForPath[ index ] = bindingsForPath[ lastIndex ]; bindingsForPath.pop(); } } // cached or active } // if object is known } // for arguments this.nCachedObjects_ = nCachedObjects; }, // Internal interface used by befriended PropertyBinding.Composite: subscribe_: function ( path, parsedPath ) { // returns an array of bindings for the given path that is changed // according to the contained objects in the group const indicesByPath = this._bindingsIndicesByPath; let index = indicesByPath[ path ]; const bindings = this._bindings; if ( index !== undefined ) return bindings[ index ]; const paths = this._paths, parsedPaths = this._parsedPaths, objects = this._objects, nObjects = objects.length, nCachedObjects = this.nCachedObjects_, bindingsForPath = new Array( nObjects ); index = bindings.length; indicesByPath[ path ] = index; paths.push( path ); parsedPaths.push( parsedPath ); bindings.push( bindingsForPath ); for ( let i = nCachedObjects, n = objects.length; i !== n; ++ i ) { const object = objects[ i ]; bindingsForPath[ i ] = new PropertyBinding( object, path, parsedPath ); } return bindingsForPath; }, unsubscribe_: function ( path ) { // tells the group to forget about a property path and no longer // update the array previously obtained with 'subscribe_' const indicesByPath = this._bindingsIndicesByPath, index = indicesByPath[ path ]; if ( index !== undefined ) { const paths = this._paths, parsedPaths = this._parsedPaths, bindings = this._bindings, lastBindingsIndex = bindings.length - 1, lastBindings = bindings[ lastBindingsIndex ], lastBindingsPath = path[ lastBindingsIndex ]; indicesByPath[ lastBindingsPath ] = index; bindings[ index ] = lastBindings; bindings.pop(); parsedPaths[ index ] = parsedPaths[ lastBindingsIndex ]; parsedPaths.pop(); paths[ index ] = paths[ lastBindingsIndex ]; paths.pop(); } } } ); class AnimationAction { constructor( mixer, clip, localRoot = null, blendMode = clip.blendMode ) { this._mixer = mixer; this._clip = clip; this._localRoot = localRoot; this.blendMode = blendMode; const tracks = clip.tracks, nTracks = tracks.length, interpolants = new Array( nTracks ); const interpolantSettings = { endingStart: ZeroCurvatureEnding, endingEnd: ZeroCurvatureEnding }; for ( let i = 0; i !== nTracks; ++ i ) { const interpolant = tracks[ i ].createInterpolant( null ); interpolants[ i ] = interpolant; interpolant.settings = interpolantSettings; } this._interpolantSettings = interpolantSettings; this._interpolants = interpolants; // bound by the mixer // inside: PropertyMixer (managed by the mixer) this._propertyBindings = new Array( nTracks ); this._cacheIndex = null; // for the memory manager this._byClipCacheIndex = null; // for the memory manager this._timeScaleInterpolant = null; this._weightInterpolant = null; this.loop = LoopRepeat; this._loopCount = - 1; // global mixer time when the action is to be started // it's set back to 'null' upon start of the action this._startTime = null; // scaled local time of the action // gets clamped or wrapped to 0..clip.duration according to loop this.time = 0; this.timeScale = 1; this._effectiveTimeScale = 1; this.weight = 1; this._effectiveWeight = 1; this.repetitions = Infinity; // no. of repetitions when looping this.paused = false; // true -> zero effective time scale this.enabled = true; // false -> zero effective weight this.clampWhenFinished = false;// keep feeding the last frame? this.zeroSlopeAtStart = true;// for smooth interpolation w/o separate this.zeroSlopeAtEnd = true;// clips for start, loop and end } // State & Scheduling play() { this._mixer._activateAction( this ); return this; } stop() { this._mixer._deactivateAction( this ); return this.reset(); } reset() { this.paused = false; this.enabled = true; this.time = 0; // restart clip this._loopCount = - 1;// forget previous loops this._startTime = null;// forget scheduling return this.stopFading().stopWarping(); } isRunning() { return this.enabled && ! this.paused && this.timeScale !== 0 && this._startTime === null && this._mixer._isActiveAction( this ); } // return true when play has been called isScheduled() { return this._mixer._isActiveAction( this ); } startAt( time ) { this._startTime = time; return this; } setLoop( mode, repetitions ) { this.loop = mode; this.repetitions = repetitions; return this; } // Weight // set the weight stopping any scheduled fading // although .enabled = false yields an effective weight of zero, this // method does *not* change .enabled, because it would be confusing setEffectiveWeight( weight ) { this.weight = weight; // note: same logic as when updated at runtime this._effectiveWeight = this.enabled ? weight : 0; return this.stopFading(); } // return the weight considering fading and .enabled getEffectiveWeight() { return this._effectiveWeight; } fadeIn( duration ) { return this._scheduleFading( duration, 0, 1 ); } fadeOut( duration ) { return this._scheduleFading( duration, 1, 0 ); } crossFadeFrom( fadeOutAction, duration, warp ) { fadeOutAction.fadeOut( duration ); this.fadeIn( duration ); if ( warp ) { const fadeInDuration = this._clip.duration, fadeOutDuration = fadeOutAction._clip.duration, startEndRatio = fadeOutDuration / fadeInDuration, endStartRatio = fadeInDuration / fadeOutDuration; fadeOutAction.warp( 1.0, startEndRatio, duration ); this.warp( endStartRatio, 1.0, duration ); } return this; } crossFadeTo( fadeInAction, duration, warp ) { return fadeInAction.crossFadeFrom( this, duration, warp ); } stopFading() { const weightInterpolant = this._weightInterpolant; if ( weightInterpolant !== null ) { this._weightInterpolant = null; this._mixer._takeBackControlInterpolant( weightInterpolant ); } return this; } // Time Scale Control // set the time scale stopping any scheduled warping // although .paused = true yields an effective time scale of zero, this // method does *not* change .paused, because it would be confusing setEffectiveTimeScale( timeScale ) { this.timeScale = timeScale; this._effectiveTimeScale = this.paused ? 0 : timeScale; return this.stopWarping(); } // return the time scale considering warping and .paused getEffectiveTimeScale() { return this._effectiveTimeScale; } setDuration( duration ) { this.timeScale = this._clip.duration / duration; return this.stopWarping(); } syncWith( action ) { this.time = action.time; this.timeScale = action.timeScale; return this.stopWarping(); } halt( duration ) { return this.warp( this._effectiveTimeScale, 0, duration ); } warp( startTimeScale, endTimeScale, duration ) { const mixer = this._mixer, now = mixer.time, timeScale = this.timeScale; let interpolant = this._timeScaleInterpolant; if ( interpolant === null ) { interpolant = mixer._lendControlInterpolant(); this._timeScaleInterpolant = interpolant; } const times = interpolant.parameterPositions, values = interpolant.sampleValues; times[ 0 ] = now; times[ 1 ] = now + duration; values[ 0 ] = startTimeScale / timeScale; values[ 1 ] = endTimeScale / timeScale; return this; } stopWarping() { const timeScaleInterpolant = this._timeScaleInterpolant; if ( timeScaleInterpolant !== null ) { this._timeScaleInterpolant = null; this._mixer._takeBackControlInterpolant( timeScaleInterpolant ); } return this; } // Object Accessors getMixer() { return this._mixer; } getClip() { return this._clip; } getRoot() { return this._localRoot || this._mixer._root; } // Interna _update( time, deltaTime, timeDirection, accuIndex ) { // called by the mixer if ( ! this.enabled ) { // call ._updateWeight() to update ._effectiveWeight this._updateWeight( time ); return; } const startTime = this._startTime; if ( startTime !== null ) { // check for scheduled start of action const timeRunning = ( time - startTime ) * timeDirection; if ( timeRunning < 0 || timeDirection === 0 ) { return; // yet to come / don't decide when delta = 0 } // start this._startTime = null; // unschedule deltaTime = timeDirection * timeRunning; } // apply time scale and advance time deltaTime *= this._updateTimeScale( time ); const clipTime = this._updateTime( deltaTime ); // note: _updateTime may disable the action resulting in // an effective weight of 0 const weight = this._updateWeight( time ); if ( weight > 0 ) { const interpolants = this._interpolants; const propertyMixers = this._propertyBindings; switch ( this.blendMode ) { case AdditiveAnimationBlendMode: for ( let j = 0, m = interpolants.length; j !== m; ++ j ) { interpolants[ j ].evaluate( clipTime ); propertyMixers[ j ].accumulateAdditive( weight ); } break; case NormalAnimationBlendMode: default: for ( let j = 0, m = interpolants.length; j !== m; ++ j ) { interpolants[ j ].evaluate( clipTime ); propertyMixers[ j ].accumulate( accuIndex, weight ); } } } } _updateWeight( time ) { let weight = 0; if ( this.enabled ) { weight = this.weight; const interpolant = this._weightInterpolant; if ( interpolant !== null ) { const interpolantValue = interpolant.evaluate( time )[ 0 ]; weight *= interpolantValue; if ( time > interpolant.parameterPositions[ 1 ] ) { this.stopFading(); if ( interpolantValue === 0 ) { // faded out, disable this.enabled = false; } } } } this._effectiveWeight = weight; return weight; } _updateTimeScale( time ) { let timeScale = 0; if ( ! this.paused ) { timeScale = this.timeScale; const interpolant = this._timeScaleInterpolant; if ( interpolant !== null ) { const interpolantValue = interpolant.evaluate( time )[ 0 ]; timeScale *= interpolantValue; if ( time > interpolant.parameterPositions[ 1 ] ) { this.stopWarping(); if ( timeScale === 0 ) { // motion has halted, pause this.paused = true; } else { // warp done - apply final time scale this.timeScale = timeScale; } } } } this._effectiveTimeScale = timeScale; return timeScale; } _updateTime( deltaTime ) { const duration = this._clip.duration; const loop = this.loop; let time = this.time + deltaTime; let loopCount = this._loopCount; const pingPong = ( loop === LoopPingPong ); if ( deltaTime === 0 ) { if ( loopCount === - 1 ) return time; return ( pingPong && ( loopCount & 1 ) === 1 ) ? duration - time : time; } if ( loop === LoopOnce ) { if ( loopCount === - 1 ) { // just started this._loopCount = 0; this._setEndings( true, true, false ); } handle_stop: { if ( time >= duration ) { time = duration; } else if ( time < 0 ) { time = 0; } else { this.time = time; break handle_stop; } if ( this.clampWhenFinished ) this.paused = true; else this.enabled = false; this.time = time; this._mixer.dispatchEvent( { type: 'finished', action: this, direction: deltaTime < 0 ? - 1 : 1 } ); } } else { // repetitive Repeat or PingPong if ( loopCount === - 1 ) { // just started if ( deltaTime >= 0 ) { loopCount = 0; this._setEndings( true, this.repetitions === 0, pingPong ); } else { // when looping in reverse direction, the initial // transition through zero counts as a repetition, // so leave loopCount at -1 this._setEndings( this.repetitions === 0, true, pingPong ); } } if ( time >= duration || time < 0 ) { // wrap around const loopDelta = Math.floor( time / duration ); // signed time -= duration * loopDelta; loopCount += Math.abs( loopDelta ); const pending = this.repetitions - loopCount; if ( pending <= 0 ) { // have to stop (switch state, clamp time, fire event) if ( this.clampWhenFinished ) this.paused = true; else this.enabled = false; time = deltaTime > 0 ? duration : 0; this.time = time; this._mixer.dispatchEvent( { type: 'finished', action: this, direction: deltaTime > 0 ? 1 : - 1 } ); } else { // keep running if ( pending === 1 ) { // entering the last round const atStart = deltaTime < 0; this._setEndings( atStart, ! atStart, pingPong ); } else { this._setEndings( false, false, pingPong ); } this._loopCount = loopCount; this.time = time; this._mixer.dispatchEvent( { type: 'loop', action: this, loopDelta: loopDelta } ); } } else { this.time = time; } if ( pingPong && ( loopCount & 1 ) === 1 ) { // invert time for the "pong round" return duration - time; } } return time; } _setEndings( atStart, atEnd, pingPong ) { const settings = this._interpolantSettings; if ( pingPong ) { settings.endingStart = ZeroSlopeEnding; settings.endingEnd = ZeroSlopeEnding; } else { // assuming for LoopOnce atStart == atEnd == true if ( atStart ) { settings.endingStart = this.zeroSlopeAtStart ? ZeroSlopeEnding : ZeroCurvatureEnding; } else { settings.endingStart = WrapAroundEnding; } if ( atEnd ) { settings.endingEnd = this.zeroSlopeAtEnd ? ZeroSlopeEnding : ZeroCurvatureEnding; } else { settings.endingEnd = WrapAroundEnding; } } } _scheduleFading( duration, weightNow, weightThen ) { const mixer = this._mixer, now = mixer.time; let interpolant = this._weightInterpolant; if ( interpolant === null ) { interpolant = mixer._lendControlInterpolant(); this._weightInterpolant = interpolant; } const times = interpolant.parameterPositions, values = interpolant.sampleValues; times[ 0 ] = now; values[ 0 ] = weightNow; times[ 1 ] = now + duration; values[ 1 ] = weightThen; return this; } } function AnimationMixer( root ) { this._root = root; this._initMemoryManager(); this._accuIndex = 0; this.time = 0; this.timeScale = 1.0; } AnimationMixer.prototype = Object.assign( Object.create( EventDispatcher.prototype ), { constructor: AnimationMixer, _bindAction: function ( action, prototypeAction ) { const root = action._localRoot || this._root, tracks = action._clip.tracks, nTracks = tracks.length, bindings = action._propertyBindings, interpolants = action._interpolants, rootUuid = root.uuid, bindingsByRoot = this._bindingsByRootAndName; let bindingsByName = bindingsByRoot[ rootUuid ]; if ( bindingsByName === undefined ) { bindingsByName = {}; bindingsByRoot[ rootUuid ] = bindingsByName; } for ( let i = 0; i !== nTracks; ++ i ) { const track = tracks[ i ], trackName = track.name; let binding = bindingsByName[ trackName ]; if ( binding !== undefined ) { bindings[ i ] = binding; } else { binding = bindings[ i ]; if ( binding !== undefined ) { // existing binding, make sure the cache knows if ( binding._cacheIndex === null ) { ++ binding.referenceCount; this._addInactiveBinding( binding, rootUuid, trackName ); } continue; } const path = prototypeAction && prototypeAction. _propertyBindings[ i ].binding.parsedPath; binding = new PropertyMixer( PropertyBinding.create( root, trackName, path ), track.ValueTypeName, track.getValueSize() ); ++ binding.referenceCount; this._addInactiveBinding( binding, rootUuid, trackName ); bindings[ i ] = binding; } interpolants[ i ].resultBuffer = binding.buffer; } }, _activateAction: function ( action ) { if ( ! this._isActiveAction( action ) ) { if ( action._cacheIndex === null ) { // this action has been forgotten by the cache, but the user // appears to be still using it -> rebind const rootUuid = ( action._localRoot || this._root ).uuid, clipUuid = action._clip.uuid, actionsForClip = this._actionsByClip[ clipUuid ]; this._bindAction( action, actionsForClip && actionsForClip.knownActions[ 0 ] ); this._addInactiveAction( action, clipUuid, rootUuid ); } const bindings = action._propertyBindings; // increment reference counts / sort out state for ( let i = 0, n = bindings.length; i !== n; ++ i ) { const binding = bindings[ i ]; if ( binding.useCount ++ === 0 ) { this._lendBinding( binding ); binding.saveOriginalState(); } } this._lendAction( action ); } }, _deactivateAction: function ( action ) { if ( this._isActiveAction( action ) ) { const bindings = action._propertyBindings; // decrement reference counts / sort out state for ( let i = 0, n = bindings.length; i !== n; ++ i ) { const binding = bindings[ i ]; if ( -- binding.useCount === 0 ) { binding.restoreOriginalState(); this._takeBackBinding( binding ); } } this._takeBackAction( action ); } }, // Memory manager _initMemoryManager: function () { this._actions = []; // 'nActiveActions' followed by inactive ones this._nActiveActions = 0; this._actionsByClip = {}; // inside: // { // knownActions: Array< AnimationAction > - used as prototypes // actionByRoot: AnimationAction - lookup // } this._bindings = []; // 'nActiveBindings' followed by inactive ones this._nActiveBindings = 0; this._bindingsByRootAndName = {}; // inside: Map< name, PropertyMixer > this._controlInterpolants = []; // same game as above this._nActiveControlInterpolants = 0; const scope = this; this.stats = { actions: { get total() { return scope._actions.length; }, get inUse() { return scope._nActiveActions; } }, bindings: { get total() { return scope._bindings.length; }, get inUse() { return scope._nActiveBindings; } }, controlInterpolants: { get total() { return scope._controlInterpolants.length; }, get inUse() { return scope._nActiveControlInterpolants; } } }; }, // Memory management for AnimationAction objects _isActiveAction: function ( action ) { const index = action._cacheIndex; return index !== null && index < this._nActiveActions; }, _addInactiveAction: function ( action, clipUuid, rootUuid ) { const actions = this._actions, actionsByClip = this._actionsByClip; let actionsForClip = actionsByClip[ clipUuid ]; if ( actionsForClip === undefined ) { actionsForClip = { knownActions: [ action ], actionByRoot: {} }; action._byClipCacheIndex = 0; actionsByClip[ clipUuid ] = actionsForClip; } else { const knownActions = actionsForClip.knownActions; action._byClipCacheIndex = knownActions.length; knownActions.push( action ); } action._cacheIndex = actions.length; actions.push( action ); actionsForClip.actionByRoot[ rootUuid ] = action; }, _removeInactiveAction: function ( action ) { const actions = this._actions, lastInactiveAction = actions[ actions.length - 1 ], cacheIndex = action._cacheIndex; lastInactiveAction._cacheIndex = cacheIndex; actions[ cacheIndex ] = lastInactiveAction; actions.pop(); action._cacheIndex = null; const clipUuid = action._clip.uuid, actionsByClip = this._actionsByClip, actionsForClip = actionsByClip[ clipUuid ], knownActionsForClip = actionsForClip.knownActions, lastKnownAction = knownActionsForClip[ knownActionsForClip.length - 1 ], byClipCacheIndex = action._byClipCacheIndex; lastKnownAction._byClipCacheIndex = byClipCacheIndex; knownActionsForClip[ byClipCacheIndex ] = lastKnownAction; knownActionsForClip.pop(); action._byClipCacheIndex = null; const actionByRoot = actionsForClip.actionByRoot, rootUuid = ( action._localRoot || this._root ).uuid; delete actionByRoot[ rootUuid ]; if ( knownActionsForClip.length === 0 ) { delete actionsByClip[ clipUuid ]; } this._removeInactiveBindingsForAction( action ); }, _removeInactiveBindingsForAction: function ( action ) { const bindings = action._propertyBindings; for ( let i = 0, n = bindings.length; i !== n; ++ i ) { const binding = bindings[ i ]; if ( -- binding.referenceCount === 0 ) { this._removeInactiveBinding( binding ); } } }, _lendAction: function ( action ) { // [ active actions | inactive actions ] // [ active actions >| inactive actions ] // s a // <-swap-> // a s const actions = this._actions, prevIndex = action._cacheIndex, lastActiveIndex = this._nActiveActions ++, firstInactiveAction = actions[ lastActiveIndex ]; action._cacheIndex = lastActiveIndex; actions[ lastActiveIndex ] = action; firstInactiveAction._cacheIndex = prevIndex; actions[ prevIndex ] = firstInactiveAction; }, _takeBackAction: function ( action ) { // [ active actions | inactive actions ] // [ active actions |< inactive actions ] // a s // <-swap-> // s a const actions = this._actions, prevIndex = action._cacheIndex, firstInactiveIndex = -- this._nActiveActions, lastActiveAction = actions[ firstInactiveIndex ]; action._cacheIndex = firstInactiveIndex; actions[ firstInactiveIndex ] = action; lastActiveAction._cacheIndex = prevIndex; actions[ prevIndex ] = lastActiveAction; }, // Memory management for PropertyMixer objects _addInactiveBinding: function ( binding, rootUuid, trackName ) { const bindingsByRoot = this._bindingsByRootAndName, bindings = this._bindings; let bindingByName = bindingsByRoot[ rootUuid ]; if ( bindingByName === undefined ) { bindingByName = {}; bindingsByRoot[ rootUuid ] = bindingByName; } bindingByName[ trackName ] = binding; binding._cacheIndex = bindings.length; bindings.push( binding ); }, _removeInactiveBinding: function ( binding ) { const bindings = this._bindings, propBinding = binding.binding, rootUuid = propBinding.rootNode.uuid, trackName = propBinding.path, bindingsByRoot = this._bindingsByRootAndName, bindingByName = bindingsByRoot[ rootUuid ], lastInactiveBinding = bindings[ bindings.length - 1 ], cacheIndex = binding._cacheIndex; lastInactiveBinding._cacheIndex = cacheIndex; bindings[ cacheIndex ] = lastInactiveBinding; bindings.pop(); delete bindingByName[ trackName ]; if ( Object.keys( bindingByName ).length === 0 ) { delete bindingsByRoot[ rootUuid ]; } }, _lendBinding: function ( binding ) { const bindings = this._bindings, prevIndex = binding._cacheIndex, lastActiveIndex = this._nActiveBindings ++, firstInactiveBinding = bindings[ lastActiveIndex ]; binding._cacheIndex = lastActiveIndex; bindings[ lastActiveIndex ] = binding; firstInactiveBinding._cacheIndex = prevIndex; bindings[ prevIndex ] = firstInactiveBinding; }, _takeBackBinding: function ( binding ) { const bindings = this._bindings, prevIndex = binding._cacheIndex, firstInactiveIndex = -- this._nActiveBindings, lastActiveBinding = bindings[ firstInactiveIndex ]; binding._cacheIndex = firstInactiveIndex; bindings[ firstInactiveIndex ] = binding; lastActiveBinding._cacheIndex = prevIndex; bindings[ prevIndex ] = lastActiveBinding; }, // Memory management of Interpolants for weight and time scale _lendControlInterpolant: function () { const interpolants = this._controlInterpolants, lastActiveIndex = this._nActiveControlInterpolants ++; let interpolant = interpolants[ lastActiveIndex ]; if ( interpolant === undefined ) { interpolant = new LinearInterpolant( new Float32Array( 2 ), new Float32Array( 2 ), 1, this._controlInterpolantsResultBuffer ); interpolant.__cacheIndex = lastActiveIndex; interpolants[ lastActiveIndex ] = interpolant; } return interpolant; }, _takeBackControlInterpolant: function ( interpolant ) { const interpolants = this._controlInterpolants, prevIndex = interpolant.__cacheIndex, firstInactiveIndex = -- this._nActiveControlInterpolants, lastActiveInterpolant = interpolants[ firstInactiveIndex ]; interpolant.__cacheIndex = firstInactiveIndex; interpolants[ firstInactiveIndex ] = interpolant; lastActiveInterpolant.__cacheIndex = prevIndex; interpolants[ prevIndex ] = lastActiveInterpolant; }, _controlInterpolantsResultBuffer: new Float32Array( 1 ), // return an action for a clip optionally using a custom root target // object (this method allocates a lot of dynamic memory in case a // previously unknown clip/root combination is specified) clipAction: function ( clip, optionalRoot, blendMode ) { const root = optionalRoot || this._root, rootUuid = root.uuid; let clipObject = typeof clip === 'string' ? AnimationClip.findByName( root, clip ) : clip; const clipUuid = clipObject !== null ? clipObject.uuid : clip; const actionsForClip = this._actionsByClip[ clipUuid ]; let prototypeAction = null; if ( blendMode === undefined ) { if ( clipObject !== null ) { blendMode = clipObject.blendMode; } else { blendMode = NormalAnimationBlendMode; } } if ( actionsForClip !== undefined ) { const existingAction = actionsForClip.actionByRoot[ rootUuid ]; if ( existingAction !== undefined && existingAction.blendMode === blendMode ) { return existingAction; } // we know the clip, so we don't have to parse all // the bindings again but can just copy prototypeAction = actionsForClip.knownActions[ 0 ]; // also, take the clip from the prototype action if ( clipObject === null ) clipObject = prototypeAction._clip; } // clip must be known when specified via string if ( clipObject === null ) return null; // allocate all resources required to run it const newAction = new AnimationAction( this, clipObject, optionalRoot, blendMode ); this._bindAction( newAction, prototypeAction ); // and make the action known to the memory manager this._addInactiveAction( newAction, clipUuid, rootUuid ); return newAction; }, // get an existing action existingAction: function ( clip, optionalRoot ) { const root = optionalRoot || this._root, rootUuid = root.uuid, clipObject = typeof clip === 'string' ? AnimationClip.findByName( root, clip ) : clip, clipUuid = clipObject ? clipObject.uuid : clip, actionsForClip = this._actionsByClip[ clipUuid ]; if ( actionsForClip !== undefined ) { return actionsForClip.actionByRoot[ rootUuid ] || null; } return null; }, // deactivates all previously scheduled actions stopAllAction: function () { const actions = this._actions, nActions = this._nActiveActions; for ( let i = nActions - 1; i >= 0; -- i ) { actions[ i ].stop(); } return this; }, // advance the time and update apply the animation update: function ( deltaTime ) { deltaTime *= this.timeScale; const actions = this._actions, nActions = this._nActiveActions, time = this.time += deltaTime, timeDirection = Math.sign( deltaTime ), accuIndex = this._accuIndex ^= 1; // run active actions for ( let i = 0; i !== nActions; ++ i ) { const action = actions[ i ]; action._update( time, deltaTime, timeDirection, accuIndex ); } // update scene graph const bindings = this._bindings, nBindings = this._nActiveBindings; for ( let i = 0; i !== nBindings; ++ i ) { bindings[ i ].apply( accuIndex ); } return this; }, // Allows you to seek to a specific time in an animation. setTime: function ( timeInSeconds ) { this.time = 0; // Zero out time attribute for AnimationMixer object; for ( let i = 0; i < this._actions.length; i ++ ) { this._actions[ i ].time = 0; // Zero out time attribute for all associated AnimationAction objects. } return this.update( timeInSeconds ); // Update used to set exact time. Returns "this" AnimationMixer object. }, // return this mixer's root target object getRoot: function () { return this._root; }, // free all resources specific to a particular clip uncacheClip: function ( clip ) { const actions = this._actions, clipUuid = clip.uuid, actionsByClip = this._actionsByClip, actionsForClip = actionsByClip[ clipUuid ]; if ( actionsForClip !== undefined ) { // note: just calling _removeInactiveAction would mess up the // iteration state and also require updating the state we can // just throw away const actionsToRemove = actionsForClip.knownActions; for ( let i = 0, n = actionsToRemove.length; i !== n; ++ i ) { const action = actionsToRemove[ i ]; this._deactivateAction( action ); const cacheIndex = action._cacheIndex, lastInactiveAction = actions[ actions.length - 1 ]; action._cacheIndex = null; action._byClipCacheIndex = null; lastInactiveAction._cacheIndex = cacheIndex; actions[ cacheIndex ] = lastInactiveAction; actions.pop(); this._removeInactiveBindingsForAction( action ); } delete actionsByClip[ clipUuid ]; } }, // free all resources specific to a particular root target object uncacheRoot: function ( root ) { const rootUuid = root.uuid, actionsByClip = this._actionsByClip; for ( const clipUuid in actionsByClip ) { const actionByRoot = actionsByClip[ clipUuid ].actionByRoot, action = actionByRoot[ rootUuid ]; if ( action !== undefined ) { this._deactivateAction( action ); this._removeInactiveAction( action ); } } const bindingsByRoot = this._bindingsByRootAndName, bindingByName = bindingsByRoot[ rootUuid ]; if ( bindingByName !== undefined ) { for ( const trackName in bindingByName ) { const binding = bindingByName[ trackName ]; binding.restoreOriginalState(); this._removeInactiveBinding( binding ); } } }, // remove a targeted clip from the cache uncacheAction: function ( clip, optionalRoot ) { const action = this.existingAction( clip, optionalRoot ); if ( action !== null ) { this._deactivateAction( action ); this._removeInactiveAction( action ); } } } ); class Uniform { constructor( value ) { if ( typeof value === 'string' ) { console.warn( 'THREE.Uniform: Type parameter is no longer needed.' ); value = arguments[ 1 ]; } this.value = value; } clone() { return new Uniform( this.value.clone === undefined ? this.value : this.value.clone() ); } } function InstancedInterleavedBuffer( array, stride, meshPerAttribute ) { InterleavedBuffer.call( this, array, stride ); this.meshPerAttribute = meshPerAttribute || 1; } InstancedInterleavedBuffer.prototype = Object.assign( Object.create( InterleavedBuffer.prototype ), { constructor: InstancedInterleavedBuffer, isInstancedInterleavedBuffer: true, copy: function ( source ) { InterleavedBuffer.prototype.copy.call( this, source ); this.meshPerAttribute = source.meshPerAttribute; return this; }, clone: function ( data ) { const ib = InterleavedBuffer.prototype.clone.call( this, data ); ib.meshPerAttribute = this.meshPerAttribute; return ib; }, toJSON: function ( data ) { const json = InterleavedBuffer.prototype.toJSON.call( this, data ); json.isInstancedInterleavedBuffer = true; json.meshPerAttribute = this.meshPerAttribute; return json; } } ); function GLBufferAttribute( buffer, type, itemSize, elementSize, count ) { this.buffer = buffer; this.type = type; this.itemSize = itemSize; this.elementSize = elementSize; this.count = count; this.version = 0; } Object.defineProperty( GLBufferAttribute.prototype, 'needsUpdate', { set: function ( value ) { if ( value === true ) this.version ++; } } ); Object.assign( GLBufferAttribute.prototype, { isGLBufferAttribute: true, setBuffer: function ( buffer ) { this.buffer = buffer; return this; }, setType: function ( type, elementSize ) { this.type = type; this.elementSize = elementSize; return this; }, setItemSize: function ( itemSize ) { this.itemSize = itemSize; return this; }, setCount: function ( count ) { this.count = count; return this; }, } ); function Raycaster( origin, direction, near, far ) { this.ray = new Ray( origin, direction ); // direction is assumed to be normalized (for accurate distance calculations) this.near = near || 0; this.far = far || Infinity; this.camera = null; this.layers = new Layers(); this.params = { Mesh: {}, Line: { threshold: 1 }, LOD: {}, Points: { threshold: 1 }, Sprite: {} }; Object.defineProperties( this.params, { PointCloud: { get: function () { console.warn( 'THREE.Raycaster: params.PointCloud has been renamed to params.Points.' ); return this.Points; } } } ); } function ascSort( a, b ) { return a.distance - b.distance; } function intersectObject( object, raycaster, intersects, recursive ) { if ( object.layers.test( raycaster.layers ) ) { object.raycast( raycaster, intersects ); } if ( recursive === true ) { const children = object.children; for ( let i = 0, l = children.length; i < l; i ++ ) { intersectObject( children[ i ], raycaster, intersects, true ); } } } Object.assign( Raycaster.prototype, { set: function ( origin, direction ) { // direction is assumed to be normalized (for accurate distance calculations) this.ray.set( origin, direction ); }, setFromCamera: function ( coords, camera ) { if ( camera && camera.isPerspectiveCamera ) { this.ray.origin.setFromMatrixPosition( camera.matrixWorld ); this.ray.direction.set( coords.x, coords.y, 0.5 ).unproject( camera ).sub( this.ray.origin ).normalize(); this.camera = camera; } else if ( camera && camera.isOrthographicCamera ) { this.ray.origin.set( coords.x, coords.y, ( camera.near + camera.far ) / ( camera.near - camera.far ) ).unproject( camera ); // set origin in plane of camera this.ray.direction.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld ); this.camera = camera; } else { console.error( 'THREE.Raycaster: Unsupported camera type: ' + camera.type ); } }, intersectObject: function ( object, recursive, optionalTarget ) { const intersects = optionalTarget || []; intersectObject( object, this, intersects, recursive ); intersects.sort( ascSort ); return intersects; }, intersectObjects: function ( objects, recursive, optionalTarget ) { const intersects = optionalTarget || []; if ( Array.isArray( objects ) === false ) { console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' ); return intersects; } for ( let i = 0, l = objects.length; i < l; i ++ ) { intersectObject( objects[ i ], this, intersects, recursive ); } intersects.sort( ascSort ); return intersects; } } ); /** * Ref: https://en.wikipedia.org/wiki/Spherical_coordinate_system * * The polar angle (phi) is measured from the positive y-axis. The positive y-axis is up. * The azimuthal angle (theta) is measured from the positive z-axis. */ class Spherical { constructor( radius = 1, phi = 0, theta = 0 ) { this.radius = radius; this.phi = phi; // polar angle this.theta = theta; // azimuthal angle return this; } set( radius, phi, theta ) { this.radius = radius; this.phi = phi; this.theta = theta; return this; } clone() { return new this.constructor().copy( this ); } copy( other ) { this.radius = other.radius; this.phi = other.phi; this.theta = other.theta; return this; } // restrict phi to be betwee EPS and PI-EPS makeSafe() { const EPS = 0.000001; this.phi = Math.max( EPS, Math.min( Math.PI - EPS, this.phi ) ); return this; } setFromVector3( v ) { return this.setFromCartesianCoords( v.x, v.y, v.z ); } setFromCartesianCoords( x, y, z ) { this.radius = Math.sqrt( x * x + y * y + z * z ); if ( this.radius === 0 ) { this.theta = 0; this.phi = 0; } else { this.theta = Math.atan2( x, z ); this.phi = Math.acos( MathUtils.clamp( y / this.radius, - 1, 1 ) ); } return this; } } /** * Ref: https://en.wikipedia.org/wiki/Cylindrical_coordinate_system */ class Cylindrical { constructor( radius, theta, y ) { this.radius = ( radius !== undefined ) ? radius : 1.0; // distance from the origin to a point in the x-z plane this.theta = ( theta !== undefined ) ? theta : 0; // counterclockwise angle in the x-z plane measured in radians from the positive z-axis this.y = ( y !== undefined ) ? y : 0; // height above the x-z plane return this; } set( radius, theta, y ) { this.radius = radius; this.theta = theta; this.y = y; return this; } clone() { return new this.constructor().copy( this ); } copy( other ) { this.radius = other.radius; this.theta = other.theta; this.y = other.y; return this; } setFromVector3( v ) { return this.setFromCartesianCoords( v.x, v.y, v.z ); } setFromCartesianCoords( x, y, z ) { this.radius = Math.sqrt( x * x + z * z ); this.theta = Math.atan2( x, z ); this.y = y; return this; } } const _vector$8 = /*@__PURE__*/ new Vector2(); class Box2 { constructor( min, max ) { Object.defineProperty( this, 'isBox2', { value: true } ); this.min = ( min !== undefined ) ? min : new Vector2( + Infinity, + Infinity ); this.max = ( max !== undefined ) ? max : new Vector2( - Infinity, - Infinity ); } set( min, max ) { this.min.copy( min ); this.max.copy( max ); return this; } setFromPoints( points ) { this.makeEmpty(); for ( let i = 0, il = points.length; i < il; i ++ ) { this.expandByPoint( points[ i ] ); } return this; } setFromCenterAndSize( center, size ) { const halfSize = _vector$8.copy( size ).multiplyScalar( 0.5 ); this.min.copy( center ).sub( halfSize ); this.max.copy( center ).add( halfSize ); return this; } clone() { return new this.constructor().copy( this ); } copy( box ) { this.min.copy( box.min ); this.max.copy( box.max ); return this; } makeEmpty() { this.min.x = this.min.y = + Infinity; this.max.x = this.max.y = - Infinity; return this; } isEmpty() { // this is a more robust check for empty than ( volume <= 0 ) because volume can get positive with two negative axes return ( this.max.x < this.min.x ) || ( this.max.y < this.min.y ); } getCenter( target ) { if ( target === undefined ) { console.warn( 'THREE.Box2: .getCenter() target is now required' ); target = new Vector2(); } return this.isEmpty() ? target.set( 0, 0 ) : target.addVectors( this.min, this.max ).multiplyScalar( 0.5 ); } getSize( target ) { if ( target === undefined ) { console.warn( 'THREE.Box2: .getSize() target is now required' ); target = new Vector2(); } return this.isEmpty() ? target.set( 0, 0 ) : target.subVectors( this.max, this.min ); } expandByPoint( point ) { this.min.min( point ); this.max.max( point ); return this; } expandByVector( vector ) { this.min.sub( vector ); this.max.add( vector ); return this; } expandByScalar( scalar ) { this.min.addScalar( - scalar ); this.max.addScalar( scalar ); return this; } containsPoint( point ) { return point.x < this.min.x || point.x > this.max.x || point.y < this.min.y || point.y > this.max.y ? false : true; } containsBox( box ) { return this.min.x <= box.min.x && box.max.x <= this.max.x && this.min.y <= box.min.y && box.max.y <= this.max.y; } getParameter( point, target ) { // This can potentially have a divide by zero if the box // has a size dimension of 0. if ( target === undefined ) { console.warn( 'THREE.Box2: .getParameter() target is now required' ); target = new Vector2(); } return target.set( ( point.x - this.min.x ) / ( this.max.x - this.min.x ), ( point.y - this.min.y ) / ( this.max.y - this.min.y ) ); } intersectsBox( box ) { // using 4 splitting planes to rule out intersections return box.max.x < this.min.x || box.min.x > this.max.x || box.max.y < this.min.y || box.min.y > this.max.y ? false : true; } clampPoint( point, target ) { if ( target === undefined ) { console.warn( 'THREE.Box2: .clampPoint() target is now required' ); target = new Vector2(); } return target.copy( point ).clamp( this.min, this.max ); } distanceToPoint( point ) { const clampedPoint = _vector$8.copy( point ).clamp( this.min, this.max ); return clampedPoint.sub( point ).length(); } intersect( box ) { this.min.max( box.min ); this.max.min( box.max ); return this; } union( box ) { this.min.min( box.min ); this.max.max( box.max ); return this; } translate( offset ) { this.min.add( offset ); this.max.add( offset ); return this; } equals( box ) { return box.min.equals( this.min ) && box.max.equals( this.max ); } } const _startP = /*@__PURE__*/ new Vector3(); const _startEnd = /*@__PURE__*/ new Vector3(); class Line3 { constructor( start, end ) { this.start = ( start !== undefined ) ? start : new Vector3(); this.end = ( end !== undefined ) ? end : new Vector3(); } set( start, end ) { this.start.copy( start ); this.end.copy( end ); return this; } clone() { return new this.constructor().copy( this ); } copy( line ) { this.start.copy( line.start ); this.end.copy( line.end ); return this; } getCenter( target ) { if ( target === undefined ) { console.warn( 'THREE.Line3: .getCenter() target is now required' ); target = new Vector3(); } return target.addVectors( this.start, this.end ).multiplyScalar( 0.5 ); } delta( target ) { if ( target === undefined ) { console.warn( 'THREE.Line3: .delta() target is now required' ); target = new Vector3(); } return target.subVectors( this.end, this.start ); } distanceSq() { return this.start.distanceToSquared( this.end ); } distance() { return this.start.distanceTo( this.end ); } at( t, target ) { if ( target === undefined ) { console.warn( 'THREE.Line3: .at() target is now required' ); target = new Vector3(); } return this.delta( target ).multiplyScalar( t ).add( this.start ); } closestPointToPointParameter( point, clampToLine ) { _startP.subVectors( point, this.start ); _startEnd.subVectors( this.end, this.start ); const startEnd2 = _startEnd.dot( _startEnd ); const startEnd_startP = _startEnd.dot( _startP ); let t = startEnd_startP / startEnd2; if ( clampToLine ) { t = MathUtils.clamp( t, 0, 1 ); } return t; } closestPointToPoint( point, clampToLine, target ) { const t = this.closestPointToPointParameter( point, clampToLine ); if ( target === undefined ) { console.warn( 'THREE.Line3: .closestPointToPoint() target is now required' ); target = new Vector3(); } return this.delta( target ).multiplyScalar( t ).add( this.start ); } applyMatrix4( matrix ) { this.start.applyMatrix4( matrix ); this.end.applyMatrix4( matrix ); return this; } equals( line ) { return line.start.equals( this.start ) && line.end.equals( this.end ); } } function ImmediateRenderObject( material ) { Object3D.call( this ); this.material = material; this.render = function ( /* renderCallback */ ) {}; this.hasPositions = false; this.hasNormals = false; this.hasColors = false; this.hasUvs = false; this.positionArray = null; this.normalArray = null; this.colorArray = null; this.uvArray = null; this.count = 0; } ImmediateRenderObject.prototype = Object.create( Object3D.prototype ); ImmediateRenderObject.prototype.constructor = ImmediateRenderObject; ImmediateRenderObject.prototype.isImmediateRenderObject = true; const _vector$9 = /*@__PURE__*/ new Vector3(); class SpotLightHelper extends Object3D { constructor( light, color ) { super(); this.light = light; this.light.updateMatrixWorld(); this.matrix = light.matrixWorld; this.matrixAutoUpdate = false; this.color = color; const geometry = new BufferGeometry(); const positions = [ 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, - 1, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, - 1, 1 ]; for ( let i = 0, j = 1, l = 32; i < l; i ++, j ++ ) { const p1 = ( i / l ) * Math.PI * 2; const p2 = ( j / l ) * Math.PI * 2; positions.push( Math.cos( p1 ), Math.sin( p1 ), 1, Math.cos( p2 ), Math.sin( p2 ), 1 ); } geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) ); const material = new LineBasicMaterial( { fog: false, toneMapped: false } ); this.cone = new LineSegments( geometry, material ); this.add( this.cone ); this.update(); } dispose() { this.cone.geometry.dispose(); this.cone.material.dispose(); } update() { this.light.updateMatrixWorld(); const coneLength = this.light.distance ? this.light.distance : 1000; const coneWidth = coneLength * Math.tan( this.light.angle ); this.cone.scale.set( coneWidth, coneWidth, coneLength ); _vector$9.setFromMatrixPosition( this.light.target.matrixWorld ); this.cone.lookAt( _vector$9 ); if ( this.color !== undefined ) { this.cone.material.color.set( this.color ); } else { this.cone.material.color.copy( this.light.color ); } } } const _vector$a = /*@__PURE__*/ new Vector3(); const _boneMatrix = /*@__PURE__*/ new Matrix4(); const _matrixWorldInv = /*@__PURE__*/ new Matrix4(); class SkeletonHelper extends LineSegments { constructor( object ) { const bones = getBoneList( object ); const geometry = new BufferGeometry(); const vertices = []; const colors = []; const color1 = new Color( 0, 0, 1 ); const color2 = new Color( 0, 1, 0 ); for ( let i = 0; i < bones.length; i ++ ) { const bone = bones[ i ]; if ( bone.parent && bone.parent.isBone ) { vertices.push( 0, 0, 0 ); vertices.push( 0, 0, 0 ); colors.push( color1.r, color1.g, color1.b ); colors.push( color2.r, color2.g, color2.b ); } } geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); const material = new LineBasicMaterial( { vertexColors: true, depthTest: false, depthWrite: false, toneMapped: false, transparent: true } ); super( geometry, material ); this.type = 'SkeletonHelper'; this.isSkeletonHelper = true; this.root = object; this.bones = bones; this.matrix = object.matrixWorld; this.matrixAutoUpdate = false; } updateMatrixWorld( force ) { const bones = this.bones; const geometry = this.geometry; const position = geometry.getAttribute( 'position' ); _matrixWorldInv.copy( this.root.matrixWorld ).invert(); for ( let i = 0, j = 0; i < bones.length; i ++ ) { const bone = bones[ i ]; if ( bone.parent && bone.parent.isBone ) { _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.matrixWorld ); _vector$a.setFromMatrixPosition( _boneMatrix ); position.setXYZ( j, _vector$a.x, _vector$a.y, _vector$a.z ); _boneMatrix.multiplyMatrices( _matrixWorldInv, bone.parent.matrixWorld ); _vector$a.setFromMatrixPosition( _boneMatrix ); position.setXYZ( j + 1, _vector$a.x, _vector$a.y, _vector$a.z ); j += 2; } } geometry.getAttribute( 'position' ).needsUpdate = true; super.updateMatrixWorld( force ); } } function getBoneList( object ) { const boneList = []; if ( object && object.isBone ) { boneList.push( object ); } for ( let i = 0; i < object.children.length; i ++ ) { boneList.push.apply( boneList, getBoneList( object.children[ i ] ) ); } return boneList; } class PointLightHelper extends Mesh { constructor( light, sphereSize, color ) { const geometry = new SphereBufferGeometry( sphereSize, 4, 2 ); const material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } ); super( geometry, material ); this.light = light; this.light.updateMatrixWorld(); this.color = color; this.type = 'PointLightHelper'; this.matrix = this.light.matrixWorld; this.matrixAutoUpdate = false; this.update(); /* // TODO: delete this comment? const distanceGeometry = new THREE.IcosahedronBufferGeometry( 1, 2 ); const distanceMaterial = new THREE.MeshBasicMaterial( { color: hexColor, fog: false, wireframe: true, opacity: 0.1, transparent: true } ); this.lightSphere = new THREE.Mesh( bulbGeometry, bulbMaterial ); this.lightDistance = new THREE.Mesh( distanceGeometry, distanceMaterial ); const d = light.distance; if ( d === 0.0 ) { this.lightDistance.visible = false; } else { this.lightDistance.scale.set( d, d, d ); } this.add( this.lightDistance ); */ } dispose() { this.geometry.dispose(); this.material.dispose(); } update() { if ( this.color !== undefined ) { this.material.color.set( this.color ); } else { this.material.color.copy( this.light.color ); } /* const d = this.light.distance; if ( d === 0.0 ) { this.lightDistance.visible = false; } else { this.lightDistance.visible = true; this.lightDistance.scale.set( d, d, d ); } */ } } const _vector$b = /*@__PURE__*/ new Vector3(); const _color1 = /*@__PURE__*/ new Color(); const _color2 = /*@__PURE__*/ new Color(); class HemisphereLightHelper extends Object3D { constructor( light, size, color ) { super(); this.light = light; this.light.updateMatrixWorld(); this.matrix = light.matrixWorld; this.matrixAutoUpdate = false; this.color = color; const geometry = new OctahedronBufferGeometry( size ); geometry.rotateY( Math.PI * 0.5 ); this.material = new MeshBasicMaterial( { wireframe: true, fog: false, toneMapped: false } ); if ( this.color === undefined ) this.material.vertexColors = true; const position = geometry.getAttribute( 'position' ); const colors = new Float32Array( position.count * 3 ); geometry.setAttribute( 'color', new BufferAttribute( colors, 3 ) ); this.add( new Mesh( geometry, this.material ) ); this.update(); } dispose() { this.children[ 0 ].geometry.dispose(); this.children[ 0 ].material.dispose(); } update() { const mesh = this.children[ 0 ]; if ( this.color !== undefined ) { this.material.color.set( this.color ); } else { const colors = mesh.geometry.getAttribute( 'color' ); _color1.copy( this.light.color ); _color2.copy( this.light.groundColor ); for ( let i = 0, l = colors.count; i < l; i ++ ) { const color = ( i < ( l / 2 ) ) ? _color1 : _color2; colors.setXYZ( i, color.r, color.g, color.b ); } colors.needsUpdate = true; } mesh.lookAt( _vector$b.setFromMatrixPosition( this.light.matrixWorld ).negate() ); } } class GridHelper extends LineSegments { constructor( size = 10, divisions = 10, color1 = 0x444444, color2 = 0x888888 ) { color1 = new Color( color1 ); color2 = new Color( color2 ); const center = divisions / 2; const step = size / divisions; const halfSize = size / 2; const vertices = [], colors = []; for ( let i = 0, j = 0, k = - halfSize; i <= divisions; i ++, k += step ) { vertices.push( - halfSize, 0, k, halfSize, 0, k ); vertices.push( k, 0, - halfSize, k, 0, halfSize ); const color = i === center ? color1 : color2; color.toArray( colors, j ); j += 3; color.toArray( colors, j ); j += 3; color.toArray( colors, j ); j += 3; color.toArray( colors, j ); j += 3; } const geometry = new BufferGeometry(); geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } ); super( geometry, material ); this.type = 'GridHelper'; } } class PolarGridHelper extends LineSegments { constructor( radius = 10, radials = 16, circles = 8, divisions = 64, color1 = 0x444444, color2 = 0x888888 ) { color1 = new Color( color1 ); color2 = new Color( color2 ); const vertices = []; const colors = []; // create the radials for ( let i = 0; i <= radials; i ++ ) { const v = ( i / radials ) * ( Math.PI * 2 ); const x = Math.sin( v ) * radius; const z = Math.cos( v ) * radius; vertices.push( 0, 0, 0 ); vertices.push( x, 0, z ); const color = ( i & 1 ) ? color1 : color2; colors.push( color.r, color.g, color.b ); colors.push( color.r, color.g, color.b ); } // create the circles for ( let i = 0; i <= circles; i ++ ) { const color = ( i & 1 ) ? color1 : color2; const r = radius - ( radius / circles * i ); for ( let j = 0; j < divisions; j ++ ) { // first vertex let v = ( j / divisions ) * ( Math.PI * 2 ); let x = Math.sin( v ) * r; let z = Math.cos( v ) * r; vertices.push( x, 0, z ); colors.push( color.r, color.g, color.b ); // second vertex v = ( ( j + 1 ) / divisions ) * ( Math.PI * 2 ); x = Math.sin( v ) * r; z = Math.cos( v ) * r; vertices.push( x, 0, z ); colors.push( color.r, color.g, color.b ); } } const geometry = new BufferGeometry(); geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } ); super( geometry, material ); this.type = 'PolarGridHelper'; } } const _v1$6 = /*@__PURE__*/ new Vector3(); const _v2$3 = /*@__PURE__*/ new Vector3(); const _v3$1 = /*@__PURE__*/ new Vector3(); class DirectionalLightHelper extends Object3D { constructor( light, size, color ) { super(); this.light = light; this.light.updateMatrixWorld(); this.matrix = light.matrixWorld; this.matrixAutoUpdate = false; this.color = color; if ( size === undefined ) size = 1; let geometry = new BufferGeometry(); geometry.setAttribute( 'position', new Float32BufferAttribute( [ - size, size, 0, size, size, 0, size, - size, 0, - size, - size, 0, - size, size, 0 ], 3 ) ); const material = new LineBasicMaterial( { fog: false, toneMapped: false } ); this.lightPlane = new Line( geometry, material ); this.add( this.lightPlane ); geometry = new BufferGeometry(); geometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 0, 1 ], 3 ) ); this.targetLine = new Line( geometry, material ); this.add( this.targetLine ); this.update(); } dispose() { this.lightPlane.geometry.dispose(); this.lightPlane.material.dispose(); this.targetLine.geometry.dispose(); this.targetLine.material.dispose(); } update() { _v1$6.setFromMatrixPosition( this.light.matrixWorld ); _v2$3.setFromMatrixPosition( this.light.target.matrixWorld ); _v3$1.subVectors( _v2$3, _v1$6 ); this.lightPlane.lookAt( _v2$3 ); if ( this.color !== undefined ) { this.lightPlane.material.color.set( this.color ); this.targetLine.material.color.set( this.color ); } else { this.lightPlane.material.color.copy( this.light.color ); this.targetLine.material.color.copy( this.light.color ); } this.targetLine.lookAt( _v2$3 ); this.targetLine.scale.z = _v3$1.length(); } } const _vector$c = /*@__PURE__*/ new Vector3(); const _camera = /*@__PURE__*/ new Camera(); /** * - shows frustum, line of sight and up of the camera * - suitable for fast updates * - based on frustum visualization in lightgl.js shadowmap example * http://evanw.github.com/lightgl.js/tests/shadowmap.html */ class CameraHelper extends LineSegments { constructor( camera ) { const geometry = new BufferGeometry(); const material = new LineBasicMaterial( { color: 0xffffff, vertexColors: true, toneMapped: false } ); const vertices = []; const colors = []; const pointMap = {}; // colors const colorFrustum = new Color( 0xffaa00 ); const colorCone = new Color( 0xff0000 ); const colorUp = new Color( 0x00aaff ); const colorTarget = new Color( 0xffffff ); const colorCross = new Color( 0x333333 ); // near addLine( 'n1', 'n2', colorFrustum ); addLine( 'n2', 'n4', colorFrustum ); addLine( 'n4', 'n3', colorFrustum ); addLine( 'n3', 'n1', colorFrustum ); // far addLine( 'f1', 'f2', colorFrustum ); addLine( 'f2', 'f4', colorFrustum ); addLine( 'f4', 'f3', colorFrustum ); addLine( 'f3', 'f1', colorFrustum ); // sides addLine( 'n1', 'f1', colorFrustum ); addLine( 'n2', 'f2', colorFrustum ); addLine( 'n3', 'f3', colorFrustum ); addLine( 'n4', 'f4', colorFrustum ); // cone addLine( 'p', 'n1', colorCone ); addLine( 'p', 'n2', colorCone ); addLine( 'p', 'n3', colorCone ); addLine( 'p', 'n4', colorCone ); // up addLine( 'u1', 'u2', colorUp ); addLine( 'u2', 'u3', colorUp ); addLine( 'u3', 'u1', colorUp ); // target addLine( 'c', 't', colorTarget ); addLine( 'p', 'c', colorCross ); // cross addLine( 'cn1', 'cn2', colorCross ); addLine( 'cn3', 'cn4', colorCross ); addLine( 'cf1', 'cf2', colorCross ); addLine( 'cf3', 'cf4', colorCross ); function addLine( a, b, color ) { addPoint( a, color ); addPoint( b, color ); } function addPoint( id, color ) { vertices.push( 0, 0, 0 ); colors.push( color.r, color.g, color.b ); if ( pointMap[ id ] === undefined ) { pointMap[ id ] = []; } pointMap[ id ].push( ( vertices.length / 3 ) - 1 ); } geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); super( geometry, material ); this.type = 'CameraHelper'; this.camera = camera; if ( this.camera.updateProjectionMatrix ) this.camera.updateProjectionMatrix(); this.matrix = camera.matrixWorld; this.matrixAutoUpdate = false; this.pointMap = pointMap; this.update(); } update() { const geometry = this.geometry; const pointMap = this.pointMap; const w = 1, h = 1; // we need just camera projection matrix inverse // world matrix must be identity _camera.projectionMatrixInverse.copy( this.camera.projectionMatrixInverse ); // center / target setPoint( 'c', pointMap, geometry, _camera, 0, 0, - 1 ); setPoint( 't', pointMap, geometry, _camera, 0, 0, 1 ); // near setPoint( 'n1', pointMap, geometry, _camera, - w, - h, - 1 ); setPoint( 'n2', pointMap, geometry, _camera, w, - h, - 1 ); setPoint( 'n3', pointMap, geometry, _camera, - w, h, - 1 ); setPoint( 'n4', pointMap, geometry, _camera, w, h, - 1 ); // far setPoint( 'f1', pointMap, geometry, _camera, - w, - h, 1 ); setPoint( 'f2', pointMap, geometry, _camera, w, - h, 1 ); setPoint( 'f3', pointMap, geometry, _camera, - w, h, 1 ); setPoint( 'f4', pointMap, geometry, _camera, w, h, 1 ); // up setPoint( 'u1', pointMap, geometry, _camera, w * 0.7, h * 1.1, - 1 ); setPoint( 'u2', pointMap, geometry, _camera, - w * 0.7, h * 1.1, - 1 ); setPoint( 'u3', pointMap, geometry, _camera, 0, h * 2, - 1 ); // cross setPoint( 'cf1', pointMap, geometry, _camera, - w, 0, 1 ); setPoint( 'cf2', pointMap, geometry, _camera, w, 0, 1 ); setPoint( 'cf3', pointMap, geometry, _camera, 0, - h, 1 ); setPoint( 'cf4', pointMap, geometry, _camera, 0, h, 1 ); setPoint( 'cn1', pointMap, geometry, _camera, - w, 0, - 1 ); setPoint( 'cn2', pointMap, geometry, _camera, w, 0, - 1 ); setPoint( 'cn3', pointMap, geometry, _camera, 0, - h, - 1 ); setPoint( 'cn4', pointMap, geometry, _camera, 0, h, - 1 ); geometry.getAttribute( 'position' ).needsUpdate = true; } } function setPoint( point, pointMap, geometry, camera, x, y, z ) { _vector$c.set( x, y, z ).unproject( camera ); const points = pointMap[ point ]; if ( points !== undefined ) { const position = geometry.getAttribute( 'position' ); for ( let i = 0, l = points.length; i < l; i ++ ) { position.setXYZ( points[ i ], _vector$c.x, _vector$c.y, _vector$c.z ); } } } const _box$3 = /*@__PURE__*/ new Box3(); class BoxHelper extends LineSegments { constructor( object, color = 0xffff00 ) { const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] ); const positions = new Float32Array( 8 * 3 ); const geometry = new BufferGeometry(); geometry.setIndex( new BufferAttribute( indices, 1 ) ); geometry.setAttribute( 'position', new BufferAttribute( positions, 3 ) ); super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); this.object = object; this.type = 'BoxHelper'; this.matrixAutoUpdate = false; this.update(); } update( object ) { if ( object !== undefined ) { console.warn( 'THREE.BoxHelper: .update() has no longer arguments.' ); } if ( this.object !== undefined ) { _box$3.setFromObject( this.object ); } if ( _box$3.isEmpty() ) return; const min = _box$3.min; const max = _box$3.max; /* 5____4 1/___0/| | 6__|_7 2/___3/ 0: max.x, max.y, max.z 1: min.x, max.y, max.z 2: min.x, min.y, max.z 3: max.x, min.y, max.z 4: max.x, max.y, min.z 5: min.x, max.y, min.z 6: min.x, min.y, min.z 7: max.x, min.y, min.z */ const position = this.geometry.attributes.position; const array = position.array; array[ 0 ] = max.x; array[ 1 ] = max.y; array[ 2 ] = max.z; array[ 3 ] = min.x; array[ 4 ] = max.y; array[ 5 ] = max.z; array[ 6 ] = min.x; array[ 7 ] = min.y; array[ 8 ] = max.z; array[ 9 ] = max.x; array[ 10 ] = min.y; array[ 11 ] = max.z; array[ 12 ] = max.x; array[ 13 ] = max.y; array[ 14 ] = min.z; array[ 15 ] = min.x; array[ 16 ] = max.y; array[ 17 ] = min.z; array[ 18 ] = min.x; array[ 19 ] = min.y; array[ 20 ] = min.z; array[ 21 ] = max.x; array[ 22 ] = min.y; array[ 23 ] = min.z; position.needsUpdate = true; this.geometry.computeBoundingSphere(); } setFromObject( object ) { this.object = object; this.update(); return this; } copy( source ) { LineSegments.prototype.copy.call( this, source ); this.object = source.object; return this; } } class Box3Helper extends LineSegments { constructor( box, color = 0xffff00 ) { const indices = new Uint16Array( [ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ] ); const positions = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 1, - 1, 1, - 1, - 1 ]; const geometry = new BufferGeometry(); geometry.setIndex( new BufferAttribute( indices, 1 ) ); geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) ); super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); this.box = box; this.type = 'Box3Helper'; this.geometry.computeBoundingSphere(); } updateMatrixWorld( force ) { const box = this.box; if ( box.isEmpty() ) return; box.getCenter( this.position ); box.getSize( this.scale ); this.scale.multiplyScalar( 0.5 ); super.updateMatrixWorld( force ); } } class PlaneHelper extends Line { constructor( plane, size = 1, hex = 0xffff00 ) { const color = hex; const positions = [ 1, - 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, - 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0 ]; const geometry = new BufferGeometry(); geometry.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) ); geometry.computeBoundingSphere(); super( geometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); this.type = 'PlaneHelper'; this.plane = plane; this.size = size; const positions2 = [ 1, 1, 1, - 1, 1, 1, - 1, - 1, 1, 1, 1, 1, - 1, - 1, 1, 1, - 1, 1 ]; const geometry2 = new BufferGeometry(); geometry2.setAttribute( 'position', new Float32BufferAttribute( positions2, 3 ) ); geometry2.computeBoundingSphere(); this.add( new Mesh( geometry2, new MeshBasicMaterial( { color: color, opacity: 0.2, transparent: true, depthWrite: false, toneMapped: false } ) ) ); } updateMatrixWorld( force ) { let scale = - this.plane.constant; if ( Math.abs( scale ) < 1e-8 ) scale = 1e-8; // sign does not matter this.scale.set( 0.5 * this.size, 0.5 * this.size, scale ); this.children[ 0 ].material.side = ( scale < 0 ) ? BackSide : FrontSide; // renderer flips side when determinant < 0; flipping not wanted here this.lookAt( this.plane.normal ); super.updateMatrixWorld( force ); } } const _axis = /*@__PURE__*/ new Vector3(); let _lineGeometry, _coneGeometry; class ArrowHelper extends Object3D { constructor( dir, origin, length, color, headLength, headWidth ) { super(); // dir is assumed to be normalized this.type = 'ArrowHelper'; if ( dir === undefined ) dir = new Vector3( 0, 0, 1 ); if ( origin === undefined ) origin = new Vector3( 0, 0, 0 ); if ( length === undefined ) length = 1; if ( color === undefined ) color = 0xffff00; if ( headLength === undefined ) headLength = 0.2 * length; if ( headWidth === undefined ) headWidth = 0.2 * headLength; if ( _lineGeometry === undefined ) { _lineGeometry = new BufferGeometry(); _lineGeometry.setAttribute( 'position', new Float32BufferAttribute( [ 0, 0, 0, 0, 1, 0 ], 3 ) ); _coneGeometry = new CylinderBufferGeometry( 0, 0.5, 1, 5, 1 ); _coneGeometry.translate( 0, - 0.5, 0 ); } this.position.copy( origin ); this.line = new Line( _lineGeometry, new LineBasicMaterial( { color: color, toneMapped: false } ) ); this.line.matrixAutoUpdate = false; this.add( this.line ); this.cone = new Mesh( _coneGeometry, new MeshBasicMaterial( { color: color, toneMapped: false } ) ); this.cone.matrixAutoUpdate = false; this.add( this.cone ); this.setDirection( dir ); this.setLength( length, headLength, headWidth ); } setDirection( dir ) { // dir is assumed to be normalized if ( dir.y > 0.99999 ) { this.quaternion.set( 0, 0, 0, 1 ); } else if ( dir.y < - 0.99999 ) { this.quaternion.set( 1, 0, 0, 0 ); } else { _axis.set( dir.z, 0, - dir.x ).normalize(); const radians = Math.acos( dir.y ); this.quaternion.setFromAxisAngle( _axis, radians ); } } setLength( length, headLength, headWidth ) { if ( headLength === undefined ) headLength = 0.2 * length; if ( headWidth === undefined ) headWidth = 0.2 * headLength; this.line.scale.set( 1, Math.max( 0.0001, length - headLength ), 1 ); // see #17458 this.line.updateMatrix(); this.cone.scale.set( headWidth, headLength, headWidth ); this.cone.position.y = length; this.cone.updateMatrix(); } setColor( color ) { this.line.material.color.set( color ); this.cone.material.color.set( color ); } copy( source ) { super.copy( source, false ); this.line.copy( source.line ); this.cone.copy( source.cone ); return this; } } class AxesHelper extends LineSegments { constructor( size = 1 ) { const vertices = [ 0, 0, 0, size, 0, 0, 0, 0, 0, 0, size, 0, 0, 0, 0, 0, 0, size ]; const colors = [ 1, 0, 0, 1, 0.6, 0, 0, 1, 0, 0.6, 1, 0, 0, 0, 1, 0, 0.6, 1 ]; const geometry = new BufferGeometry(); geometry.setAttribute( 'position', new Float32BufferAttribute( vertices, 3 ) ); geometry.setAttribute( 'color', new Float32BufferAttribute( colors, 3 ) ); const material = new LineBasicMaterial( { vertexColors: true, toneMapped: false } ); super( geometry, material ); this.type = 'AxesHelper'; } } const _floatView = new Float32Array( 1 ); const _int32View = new Int32Array( _floatView.buffer ); const DataUtils = { // Converts float32 to float16 (stored as uint16 value). toHalfFloat: function ( val ) { // Source: http://gamedev.stackexchange.com/questions/17326/conversion-of-a-number-from-single-precision-floating-point-representation-to-a/17410#17410 /* This method is faster than the OpenEXR implementation (very often * used, eg. in Ogre), with the additional benefit of rounding, inspired * by James Tursa?s half-precision code. */ _floatView[ 0 ] = val; const x = _int32View[ 0 ]; let bits = ( x >> 16 ) & 0x8000; /* Get the sign */ let m = ( x >> 12 ) & 0x07ff; /* Keep one extra bit for rounding */ const e = ( x >> 23 ) & 0xff; /* Using int is faster here */ /* If zero, or denormal, or exponent underflows too much for a denormal * half, return signed zero. */ if ( e < 103 ) return bits; /* If NaN, return NaN. If Inf or exponent overflow, return Inf. */ if ( e > 142 ) { bits |= 0x7c00; /* If exponent was 0xff and one mantissa bit was set, it means NaN, * not Inf, so make sure we set one mantissa bit too. */ bits |= ( ( e == 255 ) ? 0 : 1 ) && ( x & 0x007fffff ); return bits; } /* If exponent underflows but not too much, return a denormal */ if ( e < 113 ) { m |= 0x0800; /* Extra rounding may overflow and set mantissa to 0 and exponent * to 1, which is OK. */ bits |= ( m >> ( 114 - e ) ) + ( ( m >> ( 113 - e ) ) & 1 ); return bits; } bits |= ( ( e - 112 ) << 10 ) | ( m >> 1 ); /* Extra rounding. An overflow will set mantissa to 0 and increment * the exponent, which is OK. */ bits += m & 1; return bits; } }; const LOD_MIN = 4; const LOD_MAX = 8; const SIZE_MAX = Math.pow( 2, LOD_MAX ); // The standard deviations (radians) associated with the extra mips. These are // chosen to approximate a Trowbridge-Reitz distribution function times the // geometric shadowing function. These sigma values squared must match the // variance #defines in cube_uv_reflection_fragment.glsl.js. const EXTRA_LOD_SIGMA = [ 0.125, 0.215, 0.35, 0.446, 0.526, 0.582 ]; const TOTAL_LODS = LOD_MAX - LOD_MIN + 1 + EXTRA_LOD_SIGMA.length; // The maximum length of the blur for loop. Smaller sigmas will use fewer // samples and exit early, but not recompile the shader. const MAX_SAMPLES = 20; const ENCODINGS = { [ LinearEncoding ]: 0, [ sRGBEncoding ]: 1, [ RGBEEncoding ]: 2, [ RGBM7Encoding ]: 3, [ RGBM16Encoding ]: 4, [ RGBDEncoding ]: 5, [ GammaEncoding ]: 6 }; const _flatCamera = /*@__PURE__*/ new OrthographicCamera(); const { _lodPlanes, _sizeLods, _sigmas } = /*@__PURE__*/ _createPlanes(); const _clearColor = /*@__PURE__*/ new Color(); let _oldTarget = null; // Golden Ratio const PHI = ( 1 + Math.sqrt( 5 ) ) / 2; const INV_PHI = 1 / PHI; // Vertices of a dodecahedron (except the opposites, which represent the // same axis), used as axis directions evenly spread on a sphere. const _axisDirections = [ /*@__PURE__*/ new Vector3( 1, 1, 1 ), /*@__PURE__*/ new Vector3( - 1, 1, 1 ), /*@__PURE__*/ new Vector3( 1, 1, - 1 ), /*@__PURE__*/ new Vector3( - 1, 1, - 1 ), /*@__PURE__*/ new Vector3( 0, PHI, INV_PHI ), /*@__PURE__*/ new Vector3( 0, PHI, - INV_PHI ), /*@__PURE__*/ new Vector3( INV_PHI, 0, PHI ), /*@__PURE__*/ new Vector3( - INV_PHI, 0, PHI ), /*@__PURE__*/ new Vector3( PHI, INV_PHI, 0 ), /*@__PURE__*/ new Vector3( - PHI, INV_PHI, 0 ) ]; /** * This class generates a Prefiltered, Mipmapped Radiance Environment Map * (PMREM) from a cubeMap environment texture. This allows different levels of * blur to be quickly accessed based on material roughness. It is packed into a * special CubeUV format that allows us to perform custom interpolation so that * we can support nonlinear formats such as RGBE. Unlike a traditional mipmap * chain, it only goes down to the LOD_MIN level (above), and then creates extra * even more filtered 'mips' at the same LOD_MIN resolution, associated with * higher roughness levels. In this way we maintain resolution to smoothly * interpolate diffuse lighting while limiting sampling computation. */ class PMREMGenerator { constructor( renderer ) { this._renderer = renderer; this._pingPongRenderTarget = null; this._blurMaterial = _getBlurShader( MAX_SAMPLES ); this._equirectShader = null; this._cubemapShader = null; this._compileMaterial( this._blurMaterial ); } /** * Generates a PMREM from a supplied Scene, which can be faster than using an * image if networking bandwidth is low. Optional sigma specifies a blur radius * in radians to be applied to the scene before PMREM generation. Optional near * and far planes ensure the scene is rendered in its entirety (the cubeCamera * is placed at the origin). */ fromScene( scene, sigma = 0, near = 0.1, far = 100 ) { _oldTarget = this._renderer.getRenderTarget(); const cubeUVRenderTarget = this._allocateTargets(); this._sceneToCubeUV( scene, near, far, cubeUVRenderTarget ); if ( sigma > 0 ) { this._blur( cubeUVRenderTarget, 0, 0, sigma ); } this._applyPMREM( cubeUVRenderTarget ); this._cleanup( cubeUVRenderTarget ); return cubeUVRenderTarget; } /** * Generates a PMREM from an equirectangular texture, which can be either LDR * (RGBFormat) or HDR (RGBEFormat). The ideal input image size is 1k (1024 x 512), * as this matches best with the 256 x 256 cubemap output. */ fromEquirectangular( equirectangular ) { return this._fromTexture( equirectangular ); } /** * Generates a PMREM from an cubemap texture, which can be either LDR * (RGBFormat) or HDR (RGBEFormat). The ideal input cube size is 256 x 256, * as this matches best with the 256 x 256 cubemap output. */ fromCubemap( cubemap ) { return this._fromTexture( cubemap ); } /** * Pre-compiles the cubemap shader. You can get faster start-up by invoking this method during * your texture's network fetch for increased concurrency. */ compileCubemapShader() { if ( this._cubemapShader === null ) { this._cubemapShader = _getCubemapShader(); this._compileMaterial( this._cubemapShader ); } } /** * Pre-compiles the equirectangular shader. You can get faster start-up by invoking this method during * your texture's network fetch for increased concurrency. */ compileEquirectangularShader() { if ( this._equirectShader === null ) { this._equirectShader = _getEquirectShader(); this._compileMaterial( this._equirectShader ); } } /** * Disposes of the PMREMGenerator's internal memory. Note that PMREMGenerator is a static class, * so you should not need more than one PMREMGenerator object. If you do, calling dispose() on * one of them will cause any others to also become unusable. */ dispose() { this._blurMaterial.dispose(); if ( this._cubemapShader !== null ) this._cubemapShader.dispose(); if ( this._equirectShader !== null ) this._equirectShader.dispose(); for ( let i = 0; i < _lodPlanes.length; i ++ ) { _lodPlanes[ i ].dispose(); } } // private interface _cleanup( outputTarget ) { this._pingPongRenderTarget.dispose(); this._renderer.setRenderTarget( _oldTarget ); outputTarget.scissorTest = false; _setViewport( outputTarget, 0, 0, outputTarget.width, outputTarget.height ); } _fromTexture( texture ) { _oldTarget = this._renderer.getRenderTarget(); const cubeUVRenderTarget = this._allocateTargets( texture ); this._textureToCubeUV( texture, cubeUVRenderTarget ); this._applyPMREM( cubeUVRenderTarget ); this._cleanup( cubeUVRenderTarget ); return cubeUVRenderTarget; } _allocateTargets( texture ) { // warning: null texture is valid const params = { magFilter: NearestFilter, minFilter: NearestFilter, generateMipmaps: false, type: UnsignedByteType, format: RGBEFormat, encoding: _isLDR( texture ) ? texture.encoding : RGBEEncoding, depthBuffer: false }; const cubeUVRenderTarget = _createRenderTarget( params ); cubeUVRenderTarget.depthBuffer = texture ? false : true; this._pingPongRenderTarget = _createRenderTarget( params ); return cubeUVRenderTarget; } _compileMaterial( material ) { const tmpMesh = new Mesh( _lodPlanes[ 0 ], material ); this._renderer.compile( tmpMesh, _flatCamera ); } _sceneToCubeUV( scene, near, far, cubeUVRenderTarget ) { const fov = 90; const aspect = 1; const cubeCamera = new PerspectiveCamera( fov, aspect, near, far ); const upSign = [ 1, - 1, 1, 1, 1, 1 ]; const forwardSign = [ 1, 1, 1, - 1, - 1, - 1 ]; const renderer = this._renderer; const outputEncoding = renderer.outputEncoding; const toneMapping = renderer.toneMapping; renderer.getClearColor( _clearColor ); const clearAlpha = renderer.getClearAlpha(); renderer.toneMapping = NoToneMapping; renderer.outputEncoding = LinearEncoding; let background = scene.background; if ( background && background.isColor ) { background.convertSRGBToLinear(); // Convert linear to RGBE const maxComponent = Math.max( background.r, background.g, background.b ); const fExp = Math.min( Math.max( Math.ceil( Math.log2( maxComponent ) ), - 128.0 ), 127.0 ); background = background.multiplyScalar( Math.pow( 2.0, - fExp ) ); const alpha = ( fExp + 128.0 ) / 255.0; renderer.setClearColor( background, alpha ); scene.background = null; } for ( let i = 0; i < 6; i ++ ) { const col = i % 3; if ( col == 0 ) { cubeCamera.up.set( 0, upSign[ i ], 0 ); cubeCamera.lookAt( forwardSign[ i ], 0, 0 ); } else if ( col == 1 ) { cubeCamera.up.set( 0, 0, upSign[ i ] ); cubeCamera.lookAt( 0, forwardSign[ i ], 0 ); } else { cubeCamera.up.set( 0, upSign[ i ], 0 ); cubeCamera.lookAt( 0, 0, forwardSign[ i ] ); } _setViewport( cubeUVRenderTarget, col * SIZE_MAX, i > 2 ? SIZE_MAX : 0, SIZE_MAX, SIZE_MAX ); renderer.setRenderTarget( cubeUVRenderTarget ); renderer.render( scene, cubeCamera ); } renderer.toneMapping = toneMapping; renderer.outputEncoding = outputEncoding; renderer.setClearColor( _clearColor, clearAlpha ); } _textureToCubeUV( texture, cubeUVRenderTarget ) { const renderer = this._renderer; if ( texture.isCubeTexture ) { if ( this._cubemapShader == null ) { this._cubemapShader = _getCubemapShader(); } } else { if ( this._equirectShader == null ) { this._equirectShader = _getEquirectShader(); } } const material = texture.isCubeTexture ? this._cubemapShader : this._equirectShader; const mesh = new Mesh( _lodPlanes[ 0 ], material ); const uniforms = material.uniforms; uniforms[ 'envMap' ].value = texture; if ( ! texture.isCubeTexture ) { uniforms[ 'texelSize' ].value.set( 1.0 / texture.image.width, 1.0 / texture.image.height ); } uniforms[ 'inputEncoding' ].value = ENCODINGS[ texture.encoding ]; uniforms[ 'outputEncoding' ].value = ENCODINGS[ cubeUVRenderTarget.texture.encoding ]; _setViewport( cubeUVRenderTarget, 0, 0, 3 * SIZE_MAX, 2 * SIZE_MAX ); renderer.setRenderTarget( cubeUVRenderTarget ); renderer.render( mesh, _flatCamera ); } _applyPMREM( cubeUVRenderTarget ) { const renderer = this._renderer; const autoClear = renderer.autoClear; renderer.autoClear = false; for ( let i = 1; i < TOTAL_LODS; i ++ ) { const sigma = Math.sqrt( _sigmas[ i ] * _sigmas[ i ] - _sigmas[ i - 1 ] * _sigmas[ i - 1 ] ); const poleAxis = _axisDirections[ ( i - 1 ) % _axisDirections.length ]; this._blur( cubeUVRenderTarget, i - 1, i, sigma, poleAxis ); } renderer.autoClear = autoClear; } /** * This is a two-pass Gaussian blur for a cubemap. Normally this is done * vertically and horizontally, but this breaks down on a cube. Here we apply * the blur latitudinally (around the poles), and then longitudinally (towards * the poles) to approximate the orthogonally-separable blur. It is least * accurate at the poles, but still does a decent job. */ _blur( cubeUVRenderTarget, lodIn, lodOut, sigma, poleAxis ) { const pingPongRenderTarget = this._pingPongRenderTarget; this._halfBlur( cubeUVRenderTarget, pingPongRenderTarget, lodIn, lodOut, sigma, 'latitudinal', poleAxis ); this._halfBlur( pingPongRenderTarget, cubeUVRenderTarget, lodOut, lodOut, sigma, 'longitudinal', poleAxis ); } _halfBlur( targetIn, targetOut, lodIn, lodOut, sigmaRadians, direction, poleAxis ) { const renderer = this._renderer; const blurMaterial = this._blurMaterial; if ( direction !== 'latitudinal' && direction !== 'longitudinal' ) { console.error( 'blur direction must be either latitudinal or longitudinal!' ); } // Number of standard deviations at which to cut off the discrete approximation. const STANDARD_DEVIATIONS = 3; const blurMesh = new Mesh( _lodPlanes[ lodOut ], blurMaterial ); const blurUniforms = blurMaterial.uniforms; const pixels = _sizeLods[ lodIn ] - 1; const radiansPerPixel = isFinite( sigmaRadians ) ? Math.PI / ( 2 * pixels ) : 2 * Math.PI / ( 2 * MAX_SAMPLES - 1 ); const sigmaPixels = sigmaRadians / radiansPerPixel; const samples = isFinite( sigmaRadians ) ? 1 + Math.floor( STANDARD_DEVIATIONS * sigmaPixels ) : MAX_SAMPLES; if ( samples > MAX_SAMPLES ) { console.warn( `sigmaRadians, ${ sigmaRadians}, is too large and will clip, as it requested ${ samples} samples when the maximum is set to ${MAX_SAMPLES}` ); } const weights = []; let sum = 0; for ( let i = 0; i < MAX_SAMPLES; ++ i ) { const x = i / sigmaPixels; const weight = Math.exp( - x * x / 2 ); weights.push( weight ); if ( i == 0 ) { sum += weight; } else if ( i < samples ) { sum += 2 * weight; } } for ( let i = 0; i < weights.length; i ++ ) { weights[ i ] = weights[ i ] / sum; } blurUniforms[ 'envMap' ].value = targetIn.texture; blurUniforms[ 'samples' ].value = samples; blurUniforms[ 'weights' ].value = weights; blurUniforms[ 'latitudinal' ].value = direction === 'latitudinal'; if ( poleAxis ) { blurUniforms[ 'poleAxis' ].value = poleAxis; } blurUniforms[ 'dTheta' ].value = radiansPerPixel; blurUniforms[ 'mipInt' ].value = LOD_MAX - lodIn; blurUniforms[ 'inputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ]; blurUniforms[ 'outputEncoding' ].value = ENCODINGS[ targetIn.texture.encoding ]; const outputSize = _sizeLods[ lodOut ]; const x = 3 * Math.max( 0, SIZE_MAX - 2 * outputSize ); const y = ( lodOut === 0 ? 0 : 2 * SIZE_MAX ) + 2 * outputSize * ( lodOut > LOD_MAX - LOD_MIN ? lodOut - LOD_MAX + LOD_MIN : 0 ); _setViewport( targetOut, x, y, 3 * outputSize, 2 * outputSize ); renderer.setRenderTarget( targetOut ); renderer.render( blurMesh, _flatCamera ); } } function _isLDR( texture ) { if ( texture === undefined || texture.type !== UnsignedByteType ) return false; return texture.encoding === LinearEncoding || texture.encoding === sRGBEncoding || texture.encoding === GammaEncoding; } function _createPlanes() { const _lodPlanes = []; const _sizeLods = []; const _sigmas = []; let lod = LOD_MAX; for ( let i = 0; i < TOTAL_LODS; i ++ ) { const sizeLod = Math.pow( 2, lod ); _sizeLods.push( sizeLod ); let sigma = 1.0 / sizeLod; if ( i > LOD_MAX - LOD_MIN ) { sigma = EXTRA_LOD_SIGMA[ i - LOD_MAX + LOD_MIN - 1 ]; } else if ( i == 0 ) { sigma = 0; } _sigmas.push( sigma ); const texelSize = 1.0 / ( sizeLod - 1 ); const min = - texelSize / 2; const max = 1 + texelSize / 2; const uv1 = [ min, min, max, min, max, max, min, min, max, max, min, max ]; const cubeFaces = 6; const vertices = 6; const positionSize = 3; const uvSize = 2; const faceIndexSize = 1; const position = new Float32Array( positionSize * vertices * cubeFaces ); const uv = new Float32Array( uvSize * vertices * cubeFaces ); const faceIndex = new Float32Array( faceIndexSize * vertices * cubeFaces ); for ( let face = 0; face < cubeFaces; face ++ ) { const x = ( face % 3 ) * 2 / 3 - 1; const y = face > 2 ? 0 : - 1; const coordinates = [ x, y, 0, x + 2 / 3, y, 0, x + 2 / 3, y + 1, 0, x, y, 0, x + 2 / 3, y + 1, 0, x, y + 1, 0 ]; position.set( coordinates, positionSize * vertices * face ); uv.set( uv1, uvSize * vertices * face ); const fill = [ face, face, face, face, face, face ]; faceIndex.set( fill, faceIndexSize * vertices * face ); } const planes = new BufferGeometry(); planes.setAttribute( 'position', new BufferAttribute( position, positionSize ) ); planes.setAttribute( 'uv', new BufferAttribute( uv, uvSize ) ); planes.setAttribute( 'faceIndex', new BufferAttribute( faceIndex, faceIndexSize ) ); _lodPlanes.push( planes ); if ( lod > LOD_MIN ) { lod --; } } return { _lodPlanes, _sizeLods, _sigmas }; } function _createRenderTarget( params ) { const cubeUVRenderTarget = new WebGLRenderTarget( 3 * SIZE_MAX, 3 * SIZE_MAX, params ); cubeUVRenderTarget.texture.mapping = CubeUVReflectionMapping; cubeUVRenderTarget.texture.name = 'PMREM.cubeUv'; cubeUVRenderTarget.scissorTest = true; return cubeUVRenderTarget; } function _setViewport( target, x, y, width, height ) { target.viewport.set( x, y, width, height ); target.scissor.set( x, y, width, height ); } function _getBlurShader( maxSamples ) { const weights = new Float32Array( maxSamples ); const poleAxis = new Vector3( 0, 1, 0 ); const shaderMaterial = new RawShaderMaterial( { name: 'SphericalGaussianBlur', defines: { 'n': maxSamples }, uniforms: { 'envMap': { value: null }, 'samples': { value: 1 }, 'weights': { value: weights }, 'latitudinal': { value: false }, 'dTheta': { value: 0 }, 'mipInt': { value: 0 }, 'poleAxis': { value: poleAxis }, 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] }, 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] } }, vertexShader: _getCommonVertexShader(), fragmentShader: /* glsl */` precision mediump float; precision mediump int; varying vec3 vOutputDirection; uniform sampler2D envMap; uniform int samples; uniform float weights[ n ]; uniform bool latitudinal; uniform float dTheta; uniform float mipInt; uniform vec3 poleAxis; ${ _getEncodings() } #define ENVMAP_TYPE_CUBE_UV #include vec3 getSample( float theta, vec3 axis ) { float cosTheta = cos( theta ); // Rodrigues' axis-angle rotation vec3 sampleDirection = vOutputDirection * cosTheta + cross( axis, vOutputDirection ) * sin( theta ) + axis * dot( axis, vOutputDirection ) * ( 1.0 - cosTheta ); return bilinearCubeUV( envMap, sampleDirection, mipInt ); } void main() { vec3 axis = latitudinal ? poleAxis : cross( poleAxis, vOutputDirection ); if ( all( equal( axis, vec3( 0.0 ) ) ) ) { axis = vec3( vOutputDirection.z, 0.0, - vOutputDirection.x ); } axis = normalize( axis ); gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); gl_FragColor.rgb += weights[ 0 ] * getSample( 0.0, axis ); for ( int i = 1; i < n; i++ ) { if ( i >= samples ) { break; } float theta = dTheta * float( i ); gl_FragColor.rgb += weights[ i ] * getSample( -1.0 * theta, axis ); gl_FragColor.rgb += weights[ i ] * getSample( theta, axis ); } gl_FragColor = linearToOutputTexel( gl_FragColor ); } `, blending: NoBlending, depthTest: false, depthWrite: false } ); return shaderMaterial; } function _getEquirectShader() { const texelSize = new Vector2( 1, 1 ); const shaderMaterial = new RawShaderMaterial( { name: 'EquirectangularToCubeUV', uniforms: { 'envMap': { value: null }, 'texelSize': { value: texelSize }, 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] }, 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] } }, vertexShader: _getCommonVertexShader(), fragmentShader: /* glsl */` precision mediump float; precision mediump int; varying vec3 vOutputDirection; uniform sampler2D envMap; uniform vec2 texelSize; ${ _getEncodings() } #include void main() { gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); vec3 outputDirection = normalize( vOutputDirection ); vec2 uv = equirectUv( outputDirection ); vec2 f = fract( uv / texelSize - 0.5 ); uv -= f * texelSize; vec3 tl = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb; uv.x += texelSize.x; vec3 tr = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb; uv.y += texelSize.y; vec3 br = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb; uv.x -= texelSize.x; vec3 bl = envMapTexelToLinear( texture2D ( envMap, uv ) ).rgb; vec3 tm = mix( tl, tr, f.x ); vec3 bm = mix( bl, br, f.x ); gl_FragColor.rgb = mix( tm, bm, f.y ); gl_FragColor = linearToOutputTexel( gl_FragColor ); } `, blending: NoBlending, depthTest: false, depthWrite: false } ); return shaderMaterial; } function _getCubemapShader() { const shaderMaterial = new RawShaderMaterial( { name: 'CubemapToCubeUV', uniforms: { 'envMap': { value: null }, 'inputEncoding': { value: ENCODINGS[ LinearEncoding ] }, 'outputEncoding': { value: ENCODINGS[ LinearEncoding ] } }, vertexShader: _getCommonVertexShader(), fragmentShader: /* glsl */` precision mediump float; precision mediump int; varying vec3 vOutputDirection; uniform samplerCube envMap; ${ _getEncodings() } void main() { gl_FragColor = vec4( 0.0, 0.0, 0.0, 1.0 ); gl_FragColor.rgb = envMapTexelToLinear( textureCube( envMap, vec3( - vOutputDirection.x, vOutputDirection.yz ) ) ).rgb; gl_FragColor = linearToOutputTexel( gl_FragColor ); } `, blending: NoBlending, depthTest: false, depthWrite: false } ); return shaderMaterial; } function _getCommonVertexShader() { return /* glsl */` precision mediump float; precision mediump int; attribute vec3 position; attribute vec2 uv; attribute float faceIndex; varying vec3 vOutputDirection; // RH coordinate system; PMREM face-indexing convention vec3 getDirection( vec2 uv, float face ) { uv = 2.0 * uv - 1.0; vec3 direction = vec3( uv, 1.0 ); if ( face == 0.0 ) { direction = direction.zyx; // ( 1, v, u ) pos x } else if ( face == 1.0 ) { direction = direction.xzy; direction.xz *= -1.0; // ( -u, 1, -v ) pos y } else if ( face == 2.0 ) { direction.x *= -1.0; // ( -u, v, 1 ) pos z } else if ( face == 3.0 ) { direction = direction.zyx; direction.xz *= -1.0; // ( -1, v, -u ) neg x } else if ( face == 4.0 ) { direction = direction.xzy; direction.xy *= -1.0; // ( -u, -1, v ) neg y } else if ( face == 5.0 ) { direction.z *= -1.0; // ( u, v, -1 ) neg z } return direction; } void main() { vOutputDirection = getDirection( uv, faceIndex ); gl_Position = vec4( position, 1.0 ); } `; } function _getEncodings() { return /* glsl */` uniform int inputEncoding; uniform int outputEncoding; #include vec4 inputTexelToLinear( vec4 value ) { if ( inputEncoding == 0 ) { return value; } else if ( inputEncoding == 1 ) { return sRGBToLinear( value ); } else if ( inputEncoding == 2 ) { return RGBEToLinear( value ); } else if ( inputEncoding == 3 ) { return RGBMToLinear( value, 7.0 ); } else if ( inputEncoding == 4 ) { return RGBMToLinear( value, 16.0 ); } else if ( inputEncoding == 5 ) { return RGBDToLinear( value, 256.0 ); } else { return GammaToLinear( value, 2.2 ); } } vec4 linearToOutputTexel( vec4 value ) { if ( outputEncoding == 0 ) { return value; } else if ( outputEncoding == 1 ) { return LinearTosRGB( value ); } else if ( outputEncoding == 2 ) { return LinearToRGBE( value ); } else if ( outputEncoding == 3 ) { return LinearToRGBM( value, 7.0 ); } else if ( outputEncoding == 4 ) { return LinearToRGBM( value, 16.0 ); } else if ( outputEncoding == 5 ) { return LinearToRGBD( value, 256.0 ); } else { return LinearToGamma( value, 2.2 ); } } vec4 envMapTexelToLinear( vec4 color ) { return inputTexelToLinear( color ); } `; } function Face4( a, b, c, d, normal, color, materialIndex ) { console.warn( 'THREE.Face4 has been removed. A THREE.Face3 will be created instead.' ); return new Face3( a, b, c, normal, color, materialIndex ); } const LineStrip = 0; const LinePieces = 1; const NoColors = 0; const FaceColors = 1; const VertexColors = 2; function MeshFaceMaterial( materials ) { console.warn( 'THREE.MeshFaceMaterial has been removed. Use an Array instead.' ); return materials; } function MultiMaterial( materials = [] ) { console.warn( 'THREE.MultiMaterial has been removed. Use an Array instead.' ); materials.isMultiMaterial = true; materials.materials = materials; materials.clone = function () { return materials.slice(); }; return materials; } function PointCloud( geometry, material ) { console.warn( 'THREE.PointCloud has been renamed to THREE.Points.' ); return new Points( geometry, material ); } function Particle( material ) { console.warn( 'THREE.Particle has been renamed to THREE.Sprite.' ); return new Sprite$1( material ); } function ParticleSystem( geometry, material ) { console.warn( 'THREE.ParticleSystem has been renamed to THREE.Points.' ); return new Points( geometry, material ); } function PointCloudMaterial( parameters ) { console.warn( 'THREE.PointCloudMaterial has been renamed to THREE.PointsMaterial.' ); return new PointsMaterial( parameters ); } function ParticleBasicMaterial( parameters ) { console.warn( 'THREE.ParticleBasicMaterial has been renamed to THREE.PointsMaterial.' ); return new PointsMaterial( parameters ); } function ParticleSystemMaterial( parameters ) { console.warn( 'THREE.ParticleSystemMaterial has been renamed to THREE.PointsMaterial.' ); return new PointsMaterial( parameters ); } function Vertex( x, y, z ) { console.warn( 'THREE.Vertex has been removed. Use THREE.Vector3 instead.' ); return new Vector3( x, y, z ); } // function DynamicBufferAttribute( array, itemSize ) { console.warn( 'THREE.DynamicBufferAttribute has been removed. Use new THREE.BufferAttribute().setUsage( THREE.DynamicDrawUsage ) instead.' ); return new BufferAttribute( array, itemSize ).setUsage( DynamicDrawUsage ); } function Int8Attribute( array, itemSize ) { console.warn( 'THREE.Int8Attribute has been removed. Use new THREE.Int8BufferAttribute() instead.' ); return new Int8BufferAttribute( array, itemSize ); } function Uint8Attribute( array, itemSize ) { console.warn( 'THREE.Uint8Attribute has been removed. Use new THREE.Uint8BufferAttribute() instead.' ); return new Uint8BufferAttribute( array, itemSize ); } function Uint8ClampedAttribute( array, itemSize ) { console.warn( 'THREE.Uint8ClampedAttribute has been removed. Use new THREE.Uint8ClampedBufferAttribute() instead.' ); return new Uint8ClampedBufferAttribute( array, itemSize ); } function Int16Attribute( array, itemSize ) { console.warn( 'THREE.Int16Attribute has been removed. Use new THREE.Int16BufferAttribute() instead.' ); return new Int16BufferAttribute( array, itemSize ); } function Uint16Attribute( array, itemSize ) { console.warn( 'THREE.Uint16Attribute has been removed. Use new THREE.Uint16BufferAttribute() instead.' ); return new Uint16BufferAttribute( array, itemSize ); } function Int32Attribute( array, itemSize ) { console.warn( 'THREE.Int32Attribute has been removed. Use new THREE.Int32BufferAttribute() instead.' ); return new Int32BufferAttribute( array, itemSize ); } function Uint32Attribute( array, itemSize ) { console.warn( 'THREE.Uint32Attribute has been removed. Use new THREE.Uint32BufferAttribute() instead.' ); return new Uint32BufferAttribute( array, itemSize ); } function Float32Attribute( array, itemSize ) { console.warn( 'THREE.Float32Attribute has been removed. Use new THREE.Float32BufferAttribute() instead.' ); return new Float32BufferAttribute( array, itemSize ); } function Float64Attribute( array, itemSize ) { console.warn( 'THREE.Float64Attribute has been removed. Use new THREE.Float64BufferAttribute() instead.' ); return new Float64BufferAttribute( array, itemSize ); } // Curve.create = function ( construct, getPoint ) { console.log( 'THREE.Curve.create() has been deprecated' ); construct.prototype = Object.create( Curve.prototype ); construct.prototype.constructor = construct; construct.prototype.getPoint = getPoint; return construct; }; // Object.assign( CurvePath.prototype, { createPointsGeometry: function ( divisions ) { console.warn( 'THREE.CurvePath: .createPointsGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' ); // generate geometry from path points (for Line or Points objects) const pts = this.getPoints( divisions ); return this.createGeometry( pts ); }, createSpacedPointsGeometry: function ( divisions ) { console.warn( 'THREE.CurvePath: .createSpacedPointsGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' ); // generate geometry from equidistant sampling along the path const pts = this.getSpacedPoints( divisions ); return this.createGeometry( pts ); }, createGeometry: function ( points ) { console.warn( 'THREE.CurvePath: .createGeometry() has been removed. Use new THREE.Geometry().setFromPoints( points ) instead.' ); const geometry = new Geometry(); for ( let i = 0, l = points.length; i < l; i ++ ) { const point = points[ i ]; geometry.vertices.push( new Vector3( point.x, point.y, point.z || 0 ) ); } return geometry; } } ); // Object.assign( Path.prototype, { fromPoints: function ( points ) { console.warn( 'THREE.Path: .fromPoints() has been renamed to .setFromPoints().' ); return this.setFromPoints( points ); } } ); // function ClosedSplineCurve3( points ) { console.warn( 'THREE.ClosedSplineCurve3 has been deprecated. Use THREE.CatmullRomCurve3 instead.' ); CatmullRomCurve3.call( this, points ); this.type = 'catmullrom'; this.closed = true; } ClosedSplineCurve3.prototype = Object.create( CatmullRomCurve3.prototype ); // function SplineCurve3( points ) { console.warn( 'THREE.SplineCurve3 has been deprecated. Use THREE.CatmullRomCurve3 instead.' ); CatmullRomCurve3.call( this, points ); this.type = 'catmullrom'; } SplineCurve3.prototype = Object.create( CatmullRomCurve3.prototype ); // function Spline( points ) { console.warn( 'THREE.Spline has been removed. Use THREE.CatmullRomCurve3 instead.' ); CatmullRomCurve3.call( this, points ); this.type = 'catmullrom'; } Spline.prototype = Object.create( CatmullRomCurve3.prototype ); Object.assign( Spline.prototype, { initFromArray: function ( /* a */ ) { console.error( 'THREE.Spline: .initFromArray() has been removed.' ); }, getControlPointsArray: function ( /* optionalTarget */ ) { console.error( 'THREE.Spline: .getControlPointsArray() has been removed.' ); }, reparametrizeByArcLength: function ( /* samplingCoef */ ) { console.error( 'THREE.Spline: .reparametrizeByArcLength() has been removed.' ); } } ); // function AxisHelper( size ) { console.warn( 'THREE.AxisHelper has been renamed to THREE.AxesHelper.' ); return new AxesHelper( size ); } function BoundingBoxHelper( object, color ) { console.warn( 'THREE.BoundingBoxHelper has been deprecated. Creating a THREE.BoxHelper instead.' ); return new BoxHelper( object, color ); } function EdgesHelper( object, hex ) { console.warn( 'THREE.EdgesHelper has been removed. Use THREE.EdgesGeometry instead.' ); return new LineSegments( new EdgesGeometry( object.geometry ), new LineBasicMaterial( { color: hex !== undefined ? hex : 0xffffff } ) ); } GridHelper.prototype.setColors = function () { console.error( 'THREE.GridHelper: setColors() has been deprecated, pass them in the constructor instead.' ); }; SkeletonHelper.prototype.update = function () { console.error( 'THREE.SkeletonHelper: update() no longer needs to be called.' ); }; function WireframeHelper( object, hex ) { console.warn( 'THREE.WireframeHelper has been removed. Use THREE.WireframeGeometry instead.' ); return new LineSegments( new WireframeGeometry( object.geometry ), new LineBasicMaterial( { color: hex !== undefined ? hex : 0xffffff } ) ); } // Object.assign( Loader.prototype, { extractUrlBase: function ( url ) { console.warn( 'THREE.Loader: .extractUrlBase() has been deprecated. Use THREE.LoaderUtils.extractUrlBase() instead.' ); return LoaderUtils.extractUrlBase( url ); } } ); Loader.Handlers = { add: function ( /* regex, loader */ ) { console.error( 'THREE.Loader: Handlers.add() has been removed. Use LoadingManager.addHandler() instead.' ); }, get: function ( /* file */ ) { console.error( 'THREE.Loader: Handlers.get() has been removed. Use LoadingManager.getHandler() instead.' ); } }; function XHRLoader( manager ) { console.warn( 'THREE.XHRLoader has been renamed to THREE.FileLoader.' ); return new FileLoader( manager ); } function BinaryTextureLoader( manager ) { console.warn( 'THREE.BinaryTextureLoader has been renamed to THREE.DataTextureLoader.' ); return new DataTextureLoader( manager ); } // Object.assign( Box2.prototype, { center: function ( optionalTarget ) { console.warn( 'THREE.Box2: .center() has been renamed to .getCenter().' ); return this.getCenter( optionalTarget ); }, empty: function () { console.warn( 'THREE.Box2: .empty() has been renamed to .isEmpty().' ); return this.isEmpty(); }, isIntersectionBox: function ( box ) { console.warn( 'THREE.Box2: .isIntersectionBox() has been renamed to .intersectsBox().' ); return this.intersectsBox( box ); }, size: function ( optionalTarget ) { console.warn( 'THREE.Box2: .size() has been renamed to .getSize().' ); return this.getSize( optionalTarget ); } } ); Object.assign( Box3.prototype, { center: function ( optionalTarget ) { console.warn( 'THREE.Box3: .center() has been renamed to .getCenter().' ); return this.getCenter( optionalTarget ); }, empty: function () { console.warn( 'THREE.Box3: .empty() has been renamed to .isEmpty().' ); return this.isEmpty(); }, isIntersectionBox: function ( box ) { console.warn( 'THREE.Box3: .isIntersectionBox() has been renamed to .intersectsBox().' ); return this.intersectsBox( box ); }, isIntersectionSphere: function ( sphere ) { console.warn( 'THREE.Box3: .isIntersectionSphere() has been renamed to .intersectsSphere().' ); return this.intersectsSphere( sphere ); }, size: function ( optionalTarget ) { console.warn( 'THREE.Box3: .size() has been renamed to .getSize().' ); return this.getSize( optionalTarget ); } } ); Object.assign( Sphere.prototype, { empty: function () { console.warn( 'THREE.Sphere: .empty() has been renamed to .isEmpty().' ); return this.isEmpty(); }, } ); Frustum.prototype.setFromMatrix = function ( m ) { console.warn( 'THREE.Frustum: .setFromMatrix() has been renamed to .setFromProjectionMatrix().' ); return this.setFromProjectionMatrix( m ); }; Line3.prototype.center = function ( optionalTarget ) { console.warn( 'THREE.Line3: .center() has been renamed to .getCenter().' ); return this.getCenter( optionalTarget ); }; Object.assign( MathUtils, { random16: function () { console.warn( 'THREE.Math: .random16() has been deprecated. Use Math.random() instead.' ); return Math.random(); }, nearestPowerOfTwo: function ( value ) { console.warn( 'THREE.Math: .nearestPowerOfTwo() has been renamed to .floorPowerOfTwo().' ); return MathUtils.floorPowerOfTwo( value ); }, nextPowerOfTwo: function ( value ) { console.warn( 'THREE.Math: .nextPowerOfTwo() has been renamed to .ceilPowerOfTwo().' ); return MathUtils.ceilPowerOfTwo( value ); } } ); Object.assign( Matrix3.prototype, { flattenToArrayOffset: function ( array, offset ) { console.warn( 'THREE.Matrix3: .flattenToArrayOffset() has been deprecated. Use .toArray() instead.' ); return this.toArray( array, offset ); }, multiplyVector3: function ( vector ) { console.warn( 'THREE.Matrix3: .multiplyVector3() has been removed. Use vector.applyMatrix3( matrix ) instead.' ); return vector.applyMatrix3( this ); }, multiplyVector3Array: function ( /* a */ ) { console.error( 'THREE.Matrix3: .multiplyVector3Array() has been removed.' ); }, applyToBufferAttribute: function ( attribute ) { console.warn( 'THREE.Matrix3: .applyToBufferAttribute() has been removed. Use attribute.applyMatrix3( matrix ) instead.' ); return attribute.applyMatrix3( this ); }, applyToVector3Array: function ( /* array, offset, length */ ) { console.error( 'THREE.Matrix3: .applyToVector3Array() has been removed.' ); }, getInverse: function ( matrix ) { console.warn( 'THREE.Matrix3: .getInverse() has been removed. Use matrixInv.copy( matrix ).invert(); instead.' ); return this.copy( matrix ).invert(); } } ); Object.assign( Matrix4.prototype, { extractPosition: function ( m ) { console.warn( 'THREE.Matrix4: .extractPosition() has been renamed to .copyPosition().' ); return this.copyPosition( m ); }, flattenToArrayOffset: function ( array, offset ) { console.warn( 'THREE.Matrix4: .flattenToArrayOffset() has been deprecated. Use .toArray() instead.' ); return this.toArray( array, offset ); }, getPosition: function () { console.warn( 'THREE.Matrix4: .getPosition() has been removed. Use Vector3.setFromMatrixPosition( matrix ) instead.' ); return new Vector3().setFromMatrixColumn( this, 3 ); }, setRotationFromQuaternion: function ( q ) { console.warn( 'THREE.Matrix4: .setRotationFromQuaternion() has been renamed to .makeRotationFromQuaternion().' ); return this.makeRotationFromQuaternion( q ); }, multiplyToArray: function () { console.warn( 'THREE.Matrix4: .multiplyToArray() has been removed.' ); }, multiplyVector3: function ( vector ) { console.warn( 'THREE.Matrix4: .multiplyVector3() has been removed. Use vector.applyMatrix4( matrix ) instead.' ); return vector.applyMatrix4( this ); }, multiplyVector4: function ( vector ) { console.warn( 'THREE.Matrix4: .multiplyVector4() has been removed. Use vector.applyMatrix4( matrix ) instead.' ); return vector.applyMatrix4( this ); }, multiplyVector3Array: function ( /* a */ ) { console.error( 'THREE.Matrix4: .multiplyVector3Array() has been removed.' ); }, rotateAxis: function ( v ) { console.warn( 'THREE.Matrix4: .rotateAxis() has been removed. Use Vector3.transformDirection( matrix ) instead.' ); v.transformDirection( this ); }, crossVector: function ( vector ) { console.warn( 'THREE.Matrix4: .crossVector() has been removed. Use vector.applyMatrix4( matrix ) instead.' ); return vector.applyMatrix4( this ); }, translate: function () { console.error( 'THREE.Matrix4: .translate() has been removed.' ); }, rotateX: function () { console.error( 'THREE.Matrix4: .rotateX() has been removed.' ); }, rotateY: function () { console.error( 'THREE.Matrix4: .rotateY() has been removed.' ); }, rotateZ: function () { console.error( 'THREE.Matrix4: .rotateZ() has been removed.' ); }, rotateByAxis: function () { console.error( 'THREE.Matrix4: .rotateByAxis() has been removed.' ); }, applyToBufferAttribute: function ( attribute ) { console.warn( 'THREE.Matrix4: .applyToBufferAttribute() has been removed. Use attribute.applyMatrix4( matrix ) instead.' ); return attribute.applyMatrix4( this ); }, applyToVector3Array: function ( /* array, offset, length */ ) { console.error( 'THREE.Matrix4: .applyToVector3Array() has been removed.' ); }, makeFrustum: function ( left, right, bottom, top, near, far ) { console.warn( 'THREE.Matrix4: .makeFrustum() has been removed. Use .makePerspective( left, right, top, bottom, near, far ) instead.' ); return this.makePerspective( left, right, top, bottom, near, far ); }, getInverse: function ( matrix ) { console.warn( 'THREE.Matrix4: .getInverse() has been removed. Use matrixInv.copy( matrix ).invert(); instead.' ); return this.copy( matrix ).invert(); } } ); Plane.prototype.isIntersectionLine = function ( line ) { console.warn( 'THREE.Plane: .isIntersectionLine() has been renamed to .intersectsLine().' ); return this.intersectsLine( line ); }; Object.assign( Quaternion.prototype, { multiplyVector3: function ( vector ) { console.warn( 'THREE.Quaternion: .multiplyVector3() has been removed. Use is now vector.applyQuaternion( quaternion ) instead.' ); return vector.applyQuaternion( this ); }, inverse: function ( ) { console.warn( 'THREE.Quaternion: .inverse() has been renamed to invert().' ); return this.invert(); } } ); Object.assign( Ray.prototype, { isIntersectionBox: function ( box ) { console.warn( 'THREE.Ray: .isIntersectionBox() has been renamed to .intersectsBox().' ); return this.intersectsBox( box ); }, isIntersectionPlane: function ( plane ) { console.warn( 'THREE.Ray: .isIntersectionPlane() has been renamed to .intersectsPlane().' ); return this.intersectsPlane( plane ); }, isIntersectionSphere: function ( sphere ) { console.warn( 'THREE.Ray: .isIntersectionSphere() has been renamed to .intersectsSphere().' ); return this.intersectsSphere( sphere ); } } ); Object.assign( Triangle.prototype, { area: function () { console.warn( 'THREE.Triangle: .area() has been renamed to .getArea().' ); return this.getArea(); }, barycoordFromPoint: function ( point, target ) { console.warn( 'THREE.Triangle: .barycoordFromPoint() has been renamed to .getBarycoord().' ); return this.getBarycoord( point, target ); }, midpoint: function ( target ) { console.warn( 'THREE.Triangle: .midpoint() has been renamed to .getMidpoint().' ); return this.getMidpoint( target ); }, normal: function ( target ) { console.warn( 'THREE.Triangle: .normal() has been renamed to .getNormal().' ); return this.getNormal( target ); }, plane: function ( target ) { console.warn( 'THREE.Triangle: .plane() has been renamed to .getPlane().' ); return this.getPlane( target ); } } ); Object.assign( Triangle, { barycoordFromPoint: function ( point, a, b, c, target ) { console.warn( 'THREE.Triangle: .barycoordFromPoint() has been renamed to .getBarycoord().' ); return Triangle.getBarycoord( point, a, b, c, target ); }, normal: function ( a, b, c, target ) { console.warn( 'THREE.Triangle: .normal() has been renamed to .getNormal().' ); return Triangle.getNormal( a, b, c, target ); } } ); Object.assign( Shape.prototype, { extractAllPoints: function ( divisions ) { console.warn( 'THREE.Shape: .extractAllPoints() has been removed. Use .extractPoints() instead.' ); return this.extractPoints( divisions ); }, extrude: function ( options ) { console.warn( 'THREE.Shape: .extrude() has been removed. Use ExtrudeGeometry() instead.' ); return new ExtrudeGeometry( this, options ); }, makeGeometry: function ( options ) { console.warn( 'THREE.Shape: .makeGeometry() has been removed. Use ShapeGeometry() instead.' ); return new ShapeGeometry( this, options ); } } ); Object.assign( Vector2.prototype, { fromAttribute: function ( attribute, index, offset ) { console.warn( 'THREE.Vector2: .fromAttribute() has been renamed to .fromBufferAttribute().' ); return this.fromBufferAttribute( attribute, index, offset ); }, distanceToManhattan: function ( v ) { console.warn( 'THREE.Vector2: .distanceToManhattan() has been renamed to .manhattanDistanceTo().' ); return this.manhattanDistanceTo( v ); }, lengthManhattan: function () { console.warn( 'THREE.Vector2: .lengthManhattan() has been renamed to .manhattanLength().' ); return this.manhattanLength(); } } ); Object.assign( Vector3.prototype, { setEulerFromRotationMatrix: function () { console.error( 'THREE.Vector3: .setEulerFromRotationMatrix() has been removed. Use Euler.setFromRotationMatrix() instead.' ); }, setEulerFromQuaternion: function () { console.error( 'THREE.Vector3: .setEulerFromQuaternion() has been removed. Use Euler.setFromQuaternion() instead.' ); }, getPositionFromMatrix: function ( m ) { console.warn( 'THREE.Vector3: .getPositionFromMatrix() has been renamed to .setFromMatrixPosition().' ); return this.setFromMatrixPosition( m ); }, getScaleFromMatrix: function ( m ) { console.warn( 'THREE.Vector3: .getScaleFromMatrix() has been renamed to .setFromMatrixScale().' ); return this.setFromMatrixScale( m ); }, getColumnFromMatrix: function ( index, matrix ) { console.warn( 'THREE.Vector3: .getColumnFromMatrix() has been renamed to .setFromMatrixColumn().' ); return this.setFromMatrixColumn( matrix, index ); }, applyProjection: function ( m ) { console.warn( 'THREE.Vector3: .applyProjection() has been removed. Use .applyMatrix4( m ) instead.' ); return this.applyMatrix4( m ); }, fromAttribute: function ( attribute, index, offset ) { console.warn( 'THREE.Vector3: .fromAttribute() has been renamed to .fromBufferAttribute().' ); return this.fromBufferAttribute( attribute, index, offset ); }, distanceToManhattan: function ( v ) { console.warn( 'THREE.Vector3: .distanceToManhattan() has been renamed to .manhattanDistanceTo().' ); return this.manhattanDistanceTo( v ); }, lengthManhattan: function () { console.warn( 'THREE.Vector3: .lengthManhattan() has been renamed to .manhattanLength().' ); return this.manhattanLength(); } } ); Object.assign( Vector4.prototype, { fromAttribute: function ( attribute, index, offset ) { console.warn( 'THREE.Vector4: .fromAttribute() has been renamed to .fromBufferAttribute().' ); return this.fromBufferAttribute( attribute, index, offset ); }, lengthManhattan: function () { console.warn( 'THREE.Vector4: .lengthManhattan() has been renamed to .manhattanLength().' ); return this.manhattanLength(); } } ); // Object.assign( Geometry.prototype, { computeTangents: function () { console.error( 'THREE.Geometry: .computeTangents() has been removed.' ); }, computeLineDistances: function () { console.error( 'THREE.Geometry: .computeLineDistances() has been removed. Use THREE.Line.computeLineDistances() instead.' ); }, applyMatrix: function ( matrix ) { console.warn( 'THREE.Geometry: .applyMatrix() has been renamed to .applyMatrix4().' ); return this.applyMatrix4( matrix ); } } ); Object.assign( Object3D.prototype, { getChildByName: function ( name ) { console.warn( 'THREE.Object3D: .getChildByName() has been renamed to .getObjectByName().' ); return this.getObjectByName( name ); }, renderDepth: function () { console.warn( 'THREE.Object3D: .renderDepth has been removed. Use .renderOrder, instead.' ); }, translate: function ( distance, axis ) { console.warn( 'THREE.Object3D: .translate() has been removed. Use .translateOnAxis( axis, distance ) instead.' ); return this.translateOnAxis( axis, distance ); }, getWorldRotation: function () { console.error( 'THREE.Object3D: .getWorldRotation() has been removed. Use THREE.Object3D.getWorldQuaternion( target ) instead.' ); }, applyMatrix: function ( matrix ) { console.warn( 'THREE.Object3D: .applyMatrix() has been renamed to .applyMatrix4().' ); return this.applyMatrix4( matrix ); } } ); Object.defineProperties( Object3D.prototype, { eulerOrder: { get: function () { console.warn( 'THREE.Object3D: .eulerOrder is now .rotation.order.' ); return this.rotation.order; }, set: function ( value ) { console.warn( 'THREE.Object3D: .eulerOrder is now .rotation.order.' ); this.rotation.order = value; } }, useQuaternion: { get: function () { console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' ); }, set: function () { console.warn( 'THREE.Object3D: .useQuaternion has been removed. The library now uses quaternions by default.' ); } } } ); Object.assign( Mesh.prototype, { setDrawMode: function () { console.error( 'THREE.Mesh: .setDrawMode() has been removed. The renderer now always assumes THREE.TrianglesDrawMode. Transform your geometry via BufferGeometryUtils.toTrianglesDrawMode() if necessary.' ); }, } ); Object.defineProperties( Mesh.prototype, { drawMode: { get: function () { console.error( 'THREE.Mesh: .drawMode has been removed. The renderer now always assumes THREE.TrianglesDrawMode.' ); return TrianglesDrawMode; }, set: function () { console.error( 'THREE.Mesh: .drawMode has been removed. The renderer now always assumes THREE.TrianglesDrawMode. Transform your geometry via BufferGeometryUtils.toTrianglesDrawMode() if necessary.' ); } } } ); Object.defineProperties( LOD.prototype, { objects: { get: function () { console.warn( 'THREE.LOD: .objects has been renamed to .levels.' ); return this.levels; } } } ); Object.defineProperty( Skeleton.prototype, 'useVertexTexture', { get: function () { console.warn( 'THREE.Skeleton: useVertexTexture has been removed.' ); }, set: function () { console.warn( 'THREE.Skeleton: useVertexTexture has been removed.' ); } } ); SkinnedMesh.prototype.initBones = function () { console.error( 'THREE.SkinnedMesh: initBones() has been removed.' ); }; Object.defineProperty( Curve.prototype, '__arcLengthDivisions', { get: function () { console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' ); return this.arcLengthDivisions; }, set: function ( value ) { console.warn( 'THREE.Curve: .__arcLengthDivisions is now .arcLengthDivisions.' ); this.arcLengthDivisions = value; } } ); // PerspectiveCamera.prototype.setLens = function ( focalLength, filmGauge ) { console.warn( 'THREE.PerspectiveCamera.setLens is deprecated. ' + 'Use .setFocalLength and .filmGauge for a photographic setup.' ); if ( filmGauge !== undefined ) this.filmGauge = filmGauge; this.setFocalLength( focalLength ); }; // Object.defineProperties( Light.prototype, { onlyShadow: { set: function () { console.warn( 'THREE.Light: .onlyShadow has been removed.' ); } }, shadowCameraFov: { set: function ( value ) { console.warn( 'THREE.Light: .shadowCameraFov is now .shadow.camera.fov.' ); this.shadow.camera.fov = value; } }, shadowCameraLeft: { set: function ( value ) { console.warn( 'THREE.Light: .shadowCameraLeft is now .shadow.camera.left.' ); this.shadow.camera.left = value; } }, shadowCameraRight: { set: function ( value ) { console.warn( 'THREE.Light: .shadowCameraRight is now .shadow.camera.right.' ); this.shadow.camera.right = value; } }, shadowCameraTop: { set: function ( value ) { console.warn( 'THREE.Light: .shadowCameraTop is now .shadow.camera.top.' ); this.shadow.camera.top = value; } }, shadowCameraBottom: { set: function ( value ) { console.warn( 'THREE.Light: .shadowCameraBottom is now .shadow.camera.bottom.' ); this.shadow.camera.bottom = value; } }, shadowCameraNear: { set: function ( value ) { console.warn( 'THREE.Light: .shadowCameraNear is now .shadow.camera.near.' ); this.shadow.camera.near = value; } }, shadowCameraFar: { set: function ( value ) { console.warn( 'THREE.Light: .shadowCameraFar is now .shadow.camera.far.' ); this.shadow.camera.far = value; } }, shadowCameraVisible: { set: function () { console.warn( 'THREE.Light: .shadowCameraVisible has been removed. Use new THREE.CameraHelper( light.shadow.camera ) instead.' ); } }, shadowBias: { set: function ( value ) { console.warn( 'THREE.Light: .shadowBias is now .shadow.bias.' ); this.shadow.bias = value; } }, shadowDarkness: { set: function () { console.warn( 'THREE.Light: .shadowDarkness has been removed.' ); } }, shadowMapWidth: { set: function ( value ) { console.warn( 'THREE.Light: .shadowMapWidth is now .shadow.mapSize.width.' ); this.shadow.mapSize.width = value; } }, shadowMapHeight: { set: function ( value ) { console.warn( 'THREE.Light: .shadowMapHeight is now .shadow.mapSize.height.' ); this.shadow.mapSize.height = value; } } } ); // Object.defineProperties( BufferAttribute.prototype, { length: { get: function () { console.warn( 'THREE.BufferAttribute: .length has been deprecated. Use .count instead.' ); return this.array.length; } }, dynamic: { get: function () { console.warn( 'THREE.BufferAttribute: .dynamic has been deprecated. Use .usage instead.' ); return this.usage === DynamicDrawUsage; }, set: function ( /* value */ ) { console.warn( 'THREE.BufferAttribute: .dynamic has been deprecated. Use .usage instead.' ); this.setUsage( DynamicDrawUsage ); } } } ); Object.assign( BufferAttribute.prototype, { setDynamic: function ( value ) { //console.warn( 'THREE.BufferAttribute: .setDynamic() has been deprecated. Use .setUsage() instead.' ); this.setUsage( value === true ? DynamicDrawUsage : StaticDrawUsage ); return this; }, copyIndicesArray: function ( /* indices */ ) { console.error( 'THREE.BufferAttribute: .copyIndicesArray() has been removed.' ); }, setArray: function ( /* array */ ) { console.error( 'THREE.BufferAttribute: .setArray has been removed. Use BufferGeometry .setAttribute to replace/resize attribute buffers' ); } } ); Object.assign( BufferGeometry.prototype, { addIndex: function ( index ) { console.warn( 'THREE.BufferGeometry: .addIndex() has been renamed to .setIndex().' ); this.setIndex( index ); }, addAttribute: function ( name, attribute ) { console.warn( 'THREE.BufferGeometry: .addAttribute() has been renamed to .setAttribute().' ); if ( ! ( attribute && attribute.isBufferAttribute ) && ! ( attribute && attribute.isInterleavedBufferAttribute ) ) { console.warn( 'THREE.BufferGeometry: .addAttribute() now expects ( name, attribute ).' ); return this.setAttribute( name, new BufferAttribute( arguments[ 1 ], arguments[ 2 ] ) ); } if ( name === 'index' ) { console.warn( 'THREE.BufferGeometry.addAttribute: Use .setIndex() for index attribute.' ); this.setIndex( attribute ); return this; } return this.setAttribute( name, attribute ); }, addDrawCall: function ( start, count, indexOffset ) { if ( indexOffset !== undefined ) { console.warn( 'THREE.BufferGeometry: .addDrawCall() no longer supports indexOffset.' ); } console.warn( 'THREE.BufferGeometry: .addDrawCall() is now .addGroup().' ); this.addGroup( start, count ); }, clearDrawCalls: function () { console.warn( 'THREE.BufferGeometry: .clearDrawCalls() is now .clearGroups().' ); this.clearGroups(); }, computeTangents: function () { console.warn( 'THREE.BufferGeometry: .computeTangents() has been removed.' ); }, computeOffsets: function () { console.warn( 'THREE.BufferGeometry: .computeOffsets() has been removed.' ); }, removeAttribute: function ( name ) { console.warn( 'THREE.BufferGeometry: .removeAttribute() has been renamed to .deleteAttribute().' ); return this.deleteAttribute( name ); }, applyMatrix: function ( matrix ) { console.warn( 'THREE.BufferGeometry: .applyMatrix() has been renamed to .applyMatrix4().' ); return this.applyMatrix4( matrix ); } } ); Object.defineProperties( BufferGeometry.prototype, { drawcalls: { get: function () { console.error( 'THREE.BufferGeometry: .drawcalls has been renamed to .groups.' ); return this.groups; } }, offsets: { get: function () { console.warn( 'THREE.BufferGeometry: .offsets has been renamed to .groups.' ); return this.groups; } } } ); Object.defineProperties( InstancedBufferGeometry.prototype, { maxInstancedCount: { get: function () { console.warn( 'THREE.InstancedBufferGeometry: .maxInstancedCount has been renamed to .instanceCount.' ); return this.instanceCount; }, set: function ( value ) { console.warn( 'THREE.InstancedBufferGeometry: .maxInstancedCount has been renamed to .instanceCount.' ); this.instanceCount = value; } } } ); Object.defineProperties( Raycaster.prototype, { linePrecision: { get: function () { console.warn( 'THREE.Raycaster: .linePrecision has been deprecated. Use .params.Line.threshold instead.' ); return this.params.Line.threshold; }, set: function ( value ) { console.warn( 'THREE.Raycaster: .linePrecision has been deprecated. Use .params.Line.threshold instead.' ); this.params.Line.threshold = value; } } } ); Object.defineProperties( InterleavedBuffer.prototype, { dynamic: { get: function () { console.warn( 'THREE.InterleavedBuffer: .length has been deprecated. Use .usage instead.' ); return this.usage === DynamicDrawUsage; }, set: function ( value ) { console.warn( 'THREE.InterleavedBuffer: .length has been deprecated. Use .usage instead.' ); this.setUsage( value ); } } } ); Object.assign( InterleavedBuffer.prototype, { setDynamic: function ( value ) { console.warn( 'THREE.InterleavedBuffer: .setDynamic() has been deprecated. Use .setUsage() instead.' ); this.setUsage( value === true ? DynamicDrawUsage : StaticDrawUsage ); return this; }, setArray: function ( /* array */ ) { console.error( 'THREE.InterleavedBuffer: .setArray has been removed. Use BufferGeometry .setAttribute to replace/resize attribute buffers' ); } } ); // Object.assign( ExtrudeBufferGeometry.prototype, { getArrays: function () { console.error( 'THREE.ExtrudeBufferGeometry: .getArrays() has been removed.' ); }, addShapeList: function () { console.error( 'THREE.ExtrudeBufferGeometry: .addShapeList() has been removed.' ); }, addShape: function () { console.error( 'THREE.ExtrudeBufferGeometry: .addShape() has been removed.' ); } } ); // Object.assign( Scene.prototype, { dispose: function () { console.error( 'THREE.Scene: .dispose() has been removed.' ); } } ); // Object.defineProperties( Uniform.prototype, { dynamic: { set: function () { console.warn( 'THREE.Uniform: .dynamic has been removed. Use object.onBeforeRender() instead.' ); } }, onUpdate: { value: function () { console.warn( 'THREE.Uniform: .onUpdate() has been removed. Use object.onBeforeRender() instead.' ); return this; } } } ); // Object.defineProperties( Material.prototype, { wrapAround: { get: function () { console.warn( 'THREE.Material: .wrapAround has been removed.' ); }, set: function () { console.warn( 'THREE.Material: .wrapAround has been removed.' ); } }, overdraw: { get: function () { console.warn( 'THREE.Material: .overdraw has been removed.' ); }, set: function () { console.warn( 'THREE.Material: .overdraw has been removed.' ); } }, wrapRGB: { get: function () { console.warn( 'THREE.Material: .wrapRGB has been removed.' ); return new Color(); } }, shading: { get: function () { console.error( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' ); }, set: function ( value ) { console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' ); this.flatShading = ( value === FlatShading$1 ); } }, stencilMask: { get: function () { console.warn( 'THREE.' + this.type + ': .stencilMask has been removed. Use .stencilFuncMask instead.' ); return this.stencilFuncMask; }, set: function ( value ) { console.warn( 'THREE.' + this.type + ': .stencilMask has been removed. Use .stencilFuncMask instead.' ); this.stencilFuncMask = value; } } } ); Object.defineProperties( MeshPhongMaterial.prototype, { metal: { get: function () { console.warn( 'THREE.MeshPhongMaterial: .metal has been removed. Use THREE.MeshStandardMaterial instead.' ); return false; }, set: function () { console.warn( 'THREE.MeshPhongMaterial: .metal has been removed. Use THREE.MeshStandardMaterial instead' ); } } } ); Object.defineProperties( MeshPhysicalMaterial.prototype, { transparency: { get: function () { console.warn( 'THREE.MeshPhysicalMaterial: .transparency has been renamed to .transmission.' ); return this.transmission; }, set: function ( value ) { console.warn( 'THREE.MeshPhysicalMaterial: .transparency has been renamed to .transmission.' ); this.transmission = value; } } } ); Object.defineProperties( ShaderMaterial.prototype, { derivatives: { get: function () { console.warn( 'THREE.ShaderMaterial: .derivatives has been moved to .extensions.derivatives.' ); return this.extensions.derivatives; }, set: function ( value ) { console.warn( 'THREE. ShaderMaterial: .derivatives has been moved to .extensions.derivatives.' ); this.extensions.derivatives = value; } } } ); // Object.assign( WebGLRenderer.prototype, { clearTarget: function ( renderTarget, color, depth, stencil ) { console.warn( 'THREE.WebGLRenderer: .clearTarget() has been deprecated. Use .setRenderTarget() and .clear() instead.' ); this.setRenderTarget( renderTarget ); this.clear( color, depth, stencil ); }, animate: function ( callback ) { console.warn( 'THREE.WebGLRenderer: .animate() is now .setAnimationLoop().' ); this.setAnimationLoop( callback ); }, getCurrentRenderTarget: function () { console.warn( 'THREE.WebGLRenderer: .getCurrentRenderTarget() is now .getRenderTarget().' ); return this.getRenderTarget(); }, getMaxAnisotropy: function () { console.warn( 'THREE.WebGLRenderer: .getMaxAnisotropy() is now .capabilities.getMaxAnisotropy().' ); return this.capabilities.getMaxAnisotropy(); }, getPrecision: function () { console.warn( 'THREE.WebGLRenderer: .getPrecision() is now .capabilities.precision.' ); return this.capabilities.precision; }, resetGLState: function () { console.warn( 'THREE.WebGLRenderer: .resetGLState() is now .state.reset().' ); return this.state.reset(); }, supportsFloatTextures: function () { console.warn( 'THREE.WebGLRenderer: .supportsFloatTextures() is now .extensions.get( \'OES_texture_float\' ).' ); return this.extensions.get( 'OES_texture_float' ); }, supportsHalfFloatTextures: function () { console.warn( 'THREE.WebGLRenderer: .supportsHalfFloatTextures() is now .extensions.get( \'OES_texture_half_float\' ).' ); return this.extensions.get( 'OES_texture_half_float' ); }, supportsStandardDerivatives: function () { console.warn( 'THREE.WebGLRenderer: .supportsStandardDerivatives() is now .extensions.get( \'OES_standard_derivatives\' ).' ); return this.extensions.get( 'OES_standard_derivatives' ); }, supportsCompressedTextureS3TC: function () { console.warn( 'THREE.WebGLRenderer: .supportsCompressedTextureS3TC() is now .extensions.get( \'WEBGL_compressed_texture_s3tc\' ).' ); return this.extensions.get( 'WEBGL_compressed_texture_s3tc' ); }, supportsCompressedTexturePVRTC: function () { console.warn( 'THREE.WebGLRenderer: .supportsCompressedTexturePVRTC() is now .extensions.get( \'WEBGL_compressed_texture_pvrtc\' ).' ); return this.extensions.get( 'WEBGL_compressed_texture_pvrtc' ); }, supportsBlendMinMax: function () { console.warn( 'THREE.WebGLRenderer: .supportsBlendMinMax() is now .extensions.get( \'EXT_blend_minmax\' ).' ); return this.extensions.get( 'EXT_blend_minmax' ); }, supportsVertexTextures: function () { console.warn( 'THREE.WebGLRenderer: .supportsVertexTextures() is now .capabilities.vertexTextures.' ); return this.capabilities.vertexTextures; }, supportsInstancedArrays: function () { console.warn( 'THREE.WebGLRenderer: .supportsInstancedArrays() is now .extensions.get( \'ANGLE_instanced_arrays\' ).' ); return this.extensions.get( 'ANGLE_instanced_arrays' ); }, enableScissorTest: function ( boolean ) { console.warn( 'THREE.WebGLRenderer: .enableScissorTest() is now .setScissorTest().' ); this.setScissorTest( boolean ); }, initMaterial: function () { console.warn( 'THREE.WebGLRenderer: .initMaterial() has been removed.' ); }, addPrePlugin: function () { console.warn( 'THREE.WebGLRenderer: .addPrePlugin() has been removed.' ); }, addPostPlugin: function () { console.warn( 'THREE.WebGLRenderer: .addPostPlugin() has been removed.' ); }, updateShadowMap: function () { console.warn( 'THREE.WebGLRenderer: .updateShadowMap() has been removed.' ); }, setFaceCulling: function () { console.warn( 'THREE.WebGLRenderer: .setFaceCulling() has been removed.' ); }, allocTextureUnit: function () { console.warn( 'THREE.WebGLRenderer: .allocTextureUnit() has been removed.' ); }, setTexture: function () { console.warn( 'THREE.WebGLRenderer: .setTexture() has been removed.' ); }, setTexture2D: function () { console.warn( 'THREE.WebGLRenderer: .setTexture2D() has been removed.' ); }, setTextureCube: function () { console.warn( 'THREE.WebGLRenderer: .setTextureCube() has been removed.' ); }, getActiveMipMapLevel: function () { console.warn( 'THREE.WebGLRenderer: .getActiveMipMapLevel() is now .getActiveMipmapLevel().' ); return this.getActiveMipmapLevel(); } } ); Object.defineProperties( WebGLRenderer.prototype, { shadowMapEnabled: { get: function () { return this.shadowMap.enabled; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderer: .shadowMapEnabled is now .shadowMap.enabled.' ); this.shadowMap.enabled = value; } }, shadowMapType: { get: function () { return this.shadowMap.type; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderer: .shadowMapType is now .shadowMap.type.' ); this.shadowMap.type = value; } }, shadowMapCullFace: { get: function () { console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace has been removed. Set Material.shadowSide instead.' ); return undefined; }, set: function ( /* value */ ) { console.warn( 'THREE.WebGLRenderer: .shadowMapCullFace has been removed. Set Material.shadowSide instead.' ); } }, context: { get: function () { console.warn( 'THREE.WebGLRenderer: .context has been removed. Use .getContext() instead.' ); return this.getContext(); } }, vr: { get: function () { console.warn( 'THREE.WebGLRenderer: .vr has been renamed to .xr' ); return this.xr; } }, gammaInput: { get: function () { console.warn( 'THREE.WebGLRenderer: .gammaInput has been removed. Set the encoding for textures via Texture.encoding instead.' ); return false; }, set: function () { console.warn( 'THREE.WebGLRenderer: .gammaInput has been removed. Set the encoding for textures via Texture.encoding instead.' ); } }, gammaOutput: { get: function () { console.warn( 'THREE.WebGLRenderer: .gammaOutput has been removed. Set WebGLRenderer.outputEncoding instead.' ); return false; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderer: .gammaOutput has been removed. Set WebGLRenderer.outputEncoding instead.' ); this.outputEncoding = ( value === true ) ? sRGBEncoding : LinearEncoding; } }, toneMappingWhitePoint: { get: function () { console.warn( 'THREE.WebGLRenderer: .toneMappingWhitePoint has been removed.' ); return 1.0; }, set: function () { console.warn( 'THREE.WebGLRenderer: .toneMappingWhitePoint has been removed.' ); } }, } ); Object.defineProperties( WebGLShadowMap.prototype, { cullFace: { get: function () { console.warn( 'THREE.WebGLRenderer: .shadowMap.cullFace has been removed. Set Material.shadowSide instead.' ); return undefined; }, set: function ( /* cullFace */ ) { console.warn( 'THREE.WebGLRenderer: .shadowMap.cullFace has been removed. Set Material.shadowSide instead.' ); } }, renderReverseSided: { get: function () { console.warn( 'THREE.WebGLRenderer: .shadowMap.renderReverseSided has been removed. Set Material.shadowSide instead.' ); return undefined; }, set: function () { console.warn( 'THREE.WebGLRenderer: .shadowMap.renderReverseSided has been removed. Set Material.shadowSide instead.' ); } }, renderSingleSided: { get: function () { console.warn( 'THREE.WebGLRenderer: .shadowMap.renderSingleSided has been removed. Set Material.shadowSide instead.' ); return undefined; }, set: function () { console.warn( 'THREE.WebGLRenderer: .shadowMap.renderSingleSided has been removed. Set Material.shadowSide instead.' ); } } } ); function WebGLRenderTargetCube( width, height, options ) { console.warn( 'THREE.WebGLRenderTargetCube( width, height, options ) is now WebGLCubeRenderTarget( size, options ).' ); return new WebGLCubeRenderTarget( width, options ); } // Object.defineProperties( WebGLRenderTarget.prototype, { wrapS: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .wrapS is now .texture.wrapS.' ); return this.texture.wrapS; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .wrapS is now .texture.wrapS.' ); this.texture.wrapS = value; } }, wrapT: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .wrapT is now .texture.wrapT.' ); return this.texture.wrapT; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .wrapT is now .texture.wrapT.' ); this.texture.wrapT = value; } }, magFilter: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .magFilter is now .texture.magFilter.' ); return this.texture.magFilter; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .magFilter is now .texture.magFilter.' ); this.texture.magFilter = value; } }, minFilter: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .minFilter is now .texture.minFilter.' ); return this.texture.minFilter; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .minFilter is now .texture.minFilter.' ); this.texture.minFilter = value; } }, anisotropy: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .anisotropy is now .texture.anisotropy.' ); return this.texture.anisotropy; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .anisotropy is now .texture.anisotropy.' ); this.texture.anisotropy = value; } }, offset: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .offset is now .texture.offset.' ); return this.texture.offset; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .offset is now .texture.offset.' ); this.texture.offset = value; } }, repeat: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .repeat is now .texture.repeat.' ); return this.texture.repeat; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .repeat is now .texture.repeat.' ); this.texture.repeat = value; } }, format: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .format is now .texture.format.' ); return this.texture.format; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .format is now .texture.format.' ); this.texture.format = value; } }, type: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .type is now .texture.type.' ); return this.texture.type; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .type is now .texture.type.' ); this.texture.type = value; } }, generateMipmaps: { get: function () { console.warn( 'THREE.WebGLRenderTarget: .generateMipmaps is now .texture.generateMipmaps.' ); return this.texture.generateMipmaps; }, set: function ( value ) { console.warn( 'THREE.WebGLRenderTarget: .generateMipmaps is now .texture.generateMipmaps.' ); this.texture.generateMipmaps = value; } } } ); // Object.defineProperties( Audio.prototype, { load: { value: function ( file ) { console.warn( 'THREE.Audio: .load has been deprecated. Use THREE.AudioLoader instead.' ); const scope = this; const audioLoader = new AudioLoader(); audioLoader.load( file, function ( buffer ) { scope.setBuffer( buffer ); } ); return this; } }, startTime: { set: function () { console.warn( 'THREE.Audio: .startTime is now .play( delay ).' ); } } } ); AudioAnalyser.prototype.getData = function () { console.warn( 'THREE.AudioAnalyser: .getData() is now .getFrequencyData().' ); return this.getFrequencyData(); }; // CubeCamera.prototype.updateCubeMap = function ( renderer, scene ) { console.warn( 'THREE.CubeCamera: .updateCubeMap() is now .update().' ); return this.update( renderer, scene ); }; CubeCamera.prototype.clear = function ( renderer, color, depth, stencil ) { console.warn( 'THREE.CubeCamera: .clear() is now .renderTarget.clear().' ); return this.renderTarget.clear( renderer, color, depth, stencil ); }; // const GeometryUtils = { merge: function ( geometry1, geometry2, materialIndexOffset ) { console.warn( 'THREE.GeometryUtils: .merge() has been moved to Geometry. Use geometry.merge( geometry2, matrix, materialIndexOffset ) instead.' ); let matrix; if ( geometry2.isMesh ) { geometry2.matrixAutoUpdate && geometry2.updateMatrix(); matrix = geometry2.matrix; geometry2 = geometry2.geometry; } geometry1.merge( geometry2, matrix, materialIndexOffset ); }, center: function ( geometry ) { console.warn( 'THREE.GeometryUtils: .center() has been moved to Geometry. Use geometry.center() instead.' ); return geometry.center(); } }; ImageUtils.crossOrigin = undefined; ImageUtils.loadTexture = function ( url, mapping, onLoad, onError ) { console.warn( 'THREE.ImageUtils.loadTexture has been deprecated. Use THREE.TextureLoader() instead.' ); const loader = new TextureLoader(); loader.setCrossOrigin( this.crossOrigin ); const texture = loader.load( url, onLoad, undefined, onError ); if ( mapping ) texture.mapping = mapping; return texture; }; ImageUtils.loadTextureCube = function ( urls, mapping, onLoad, onError ) { console.warn( 'THREE.ImageUtils.loadTextureCube has been deprecated. Use THREE.CubeTextureLoader() instead.' ); const loader = new CubeTextureLoader(); loader.setCrossOrigin( this.crossOrigin ); const texture = loader.load( urls, onLoad, undefined, onError ); if ( mapping ) texture.mapping = mapping; return texture; }; ImageUtils.loadCompressedTexture = function () { console.error( 'THREE.ImageUtils.loadCompressedTexture has been removed. Use THREE.DDSLoader instead.' ); }; ImageUtils.loadCompressedTextureCube = function () { console.error( 'THREE.ImageUtils.loadCompressedTextureCube has been removed. Use THREE.DDSLoader instead.' ); }; // function CanvasRenderer() { console.error( 'THREE.CanvasRenderer has been removed' ); } // function JSONLoader() { console.error( 'THREE.JSONLoader has been removed.' ); } // const SceneUtils = { createMultiMaterialObject: function ( /* geometry, materials */ ) { console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' ); }, detach: function ( /* child, parent, scene */ ) { console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' ); }, attach: function ( /* child, scene, parent */ ) { console.error( 'THREE.SceneUtils has been moved to /examples/jsm/utils/SceneUtils.js' ); } }; // function LensFlare() { console.error( 'THREE.LensFlare has been moved to /examples/jsm/objects/Lensflare.js' ); } if ( typeof __THREE_DEVTOOLS__ !== 'undefined' ) { /* eslint-disable no-undef */ __THREE_DEVTOOLS__.dispatchEvent( new CustomEvent( 'register', { detail: { revision: REVISION, } } ) ); /* eslint-enable no-undef */ } var THREE$1 = /*#__PURE__*/Object.freeze({ __proto__: null, ACESFilmicToneMapping: ACESFilmicToneMapping, AddEquation: AddEquation, AddOperation: AddOperation, AdditiveAnimationBlendMode: AdditiveAnimationBlendMode, AdditiveBlending: AdditiveBlending, AlphaFormat: AlphaFormat, AlwaysDepth: AlwaysDepth, AlwaysStencilFunc: AlwaysStencilFunc, AmbientLight: AmbientLight, AmbientLightProbe: AmbientLightProbe, AnimationClip: AnimationClip, AnimationLoader: AnimationLoader, AnimationMixer: AnimationMixer, AnimationObjectGroup: AnimationObjectGroup, AnimationUtils: AnimationUtils, ArcCurve: ArcCurve, ArrayCamera: ArrayCamera, ArrowHelper: ArrowHelper, Audio: Audio, AudioAnalyser: AudioAnalyser, AudioContext: AudioContext, AudioListener: AudioListener, AudioLoader: AudioLoader, AxesHelper: AxesHelper, AxisHelper: AxisHelper, BackSide: BackSide, BasicDepthPacking: BasicDepthPacking, BasicShadowMap: BasicShadowMap, BinaryTextureLoader: BinaryTextureLoader, Bone: Bone, BooleanKeyframeTrack: BooleanKeyframeTrack, BoundingBoxHelper: BoundingBoxHelper, Box2: Box2, Box3: Box3, Box3Helper: Box3Helper, BoxBufferGeometry: BoxBufferGeometry, BoxGeometry: BoxGeometry, BoxHelper: BoxHelper, BufferAttribute: BufferAttribute, BufferGeometry: BufferGeometry, BufferGeometryLoader: BufferGeometryLoader, ByteType: ByteType, Cache: Cache, Camera: Camera, CameraHelper: CameraHelper, CanvasRenderer: CanvasRenderer, CanvasTexture: CanvasTexture, CatmullRomCurve3: CatmullRomCurve3, CineonToneMapping: CineonToneMapping, CircleBufferGeometry: CircleBufferGeometry, CircleGeometry: CircleGeometry, ClampToEdgeWrapping: ClampToEdgeWrapping, Clock: Clock, ClosedSplineCurve3: ClosedSplineCurve3, Color: Color, ColorKeyframeTrack: ColorKeyframeTrack, CompressedTexture: CompressedTexture, CompressedTextureLoader: CompressedTextureLoader, ConeBufferGeometry: ConeBufferGeometry, ConeGeometry: ConeGeometry, CubeCamera: CubeCamera, CubeGeometry: BoxGeometry, CubeReflectionMapping: CubeReflectionMapping, CubeRefractionMapping: CubeRefractionMapping, CubeTexture: CubeTexture, CubeTextureLoader: CubeTextureLoader, CubeUVReflectionMapping: CubeUVReflectionMapping, CubeUVRefractionMapping: CubeUVRefractionMapping, CubicBezierCurve: CubicBezierCurve, CubicBezierCurve3: CubicBezierCurve3, CubicInterpolant: CubicInterpolant, CullFaceBack: CullFaceBack, CullFaceFront: CullFaceFront, CullFaceFrontBack: CullFaceFrontBack, CullFaceNone: CullFaceNone, Curve: Curve, CurvePath: CurvePath, CustomBlending: CustomBlending, CustomToneMapping: CustomToneMapping, CylinderBufferGeometry: CylinderBufferGeometry, CylinderGeometry: CylinderGeometry, Cylindrical: Cylindrical, DataTexture: DataTexture, DataTexture2DArray: DataTexture2DArray, DataTexture3D: DataTexture3D, DataTextureLoader: DataTextureLoader, DataUtils: DataUtils, DecrementStencilOp: DecrementStencilOp, DecrementWrapStencilOp: DecrementWrapStencilOp, DefaultLoadingManager: DefaultLoadingManager, DepthFormat: DepthFormat, DepthStencilFormat: DepthStencilFormat, DepthTexture: DepthTexture, DirectionalLight: DirectionalLight, DirectionalLightHelper: DirectionalLightHelper, DiscreteInterpolant: DiscreteInterpolant, DodecahedronBufferGeometry: DodecahedronBufferGeometry, DodecahedronGeometry: DodecahedronGeometry, DoubleSide: DoubleSide, DstAlphaFactor: DstAlphaFactor, DstColorFactor: DstColorFactor, DynamicBufferAttribute: DynamicBufferAttribute, DynamicCopyUsage: DynamicCopyUsage, DynamicDrawUsage: DynamicDrawUsage, DynamicReadUsage: DynamicReadUsage, EdgesGeometry: EdgesGeometry, EdgesHelper: EdgesHelper, EllipseCurve: EllipseCurve, EqualDepth: EqualDepth, EqualStencilFunc: EqualStencilFunc, EquirectangularReflectionMapping: EquirectangularReflectionMapping, EquirectangularRefractionMapping: EquirectangularRefractionMapping, Euler: Euler, EventDispatcher: EventDispatcher, ExtrudeBufferGeometry: ExtrudeBufferGeometry, ExtrudeGeometry: ExtrudeGeometry, Face3: Face3, Face4: Face4, FaceColors: FaceColors, FileLoader: FileLoader, FlatShading: FlatShading$1, Float16BufferAttribute: Float16BufferAttribute, Float32Attribute: Float32Attribute, Float32BufferAttribute: Float32BufferAttribute, Float64Attribute: Float64Attribute, Float64BufferAttribute: Float64BufferAttribute, FloatType: FloatType, Fog: Fog, FogExp2: FogExp2, Font: Font, FontLoader: FontLoader, FrontSide: FrontSide, Frustum: Frustum, GLBufferAttribute: GLBufferAttribute, GLSL1: GLSL1, GLSL3: GLSL3, GammaEncoding: GammaEncoding, Geometry: Geometry, GeometryUtils: GeometryUtils, GreaterDepth: GreaterDepth, GreaterEqualDepth: GreaterEqualDepth, GreaterEqualStencilFunc: GreaterEqualStencilFunc, GreaterStencilFunc: GreaterStencilFunc, GridHelper: GridHelper, Group: Group, HalfFloatType: HalfFloatType, HemisphereLight: HemisphereLight, HemisphereLightHelper: HemisphereLightHelper, HemisphereLightProbe: HemisphereLightProbe, IcosahedronBufferGeometry: IcosahedronBufferGeometry, IcosahedronGeometry: IcosahedronGeometry, ImageBitmapLoader: ImageBitmapLoader, ImageLoader: ImageLoader, ImageUtils: ImageUtils, ImmediateRenderObject: ImmediateRenderObject, IncrementStencilOp: IncrementStencilOp, IncrementWrapStencilOp: IncrementWrapStencilOp, InstancedBufferAttribute: InstancedBufferAttribute, InstancedBufferGeometry: InstancedBufferGeometry, InstancedInterleavedBuffer: InstancedInterleavedBuffer, InstancedMesh: InstancedMesh, Int16Attribute: Int16Attribute, Int16BufferAttribute: Int16BufferAttribute, Int32Attribute: Int32Attribute, Int32BufferAttribute: Int32BufferAttribute, Int8Attribute: Int8Attribute, Int8BufferAttribute: Int8BufferAttribute, IntType: IntType, InterleavedBuffer: InterleavedBuffer, InterleavedBufferAttribute: InterleavedBufferAttribute, Interpolant: Interpolant, InterpolateDiscrete: InterpolateDiscrete, InterpolateLinear: InterpolateLinear, InterpolateSmooth: InterpolateSmooth, InvertStencilOp: InvertStencilOp, JSONLoader: JSONLoader, KeepStencilOp: KeepStencilOp, KeyframeTrack: KeyframeTrack, LOD: LOD, LatheBufferGeometry: LatheBufferGeometry, LatheGeometry: LatheGeometry, Layers: Layers, LensFlare: LensFlare, LessDepth: LessDepth, LessEqualDepth: LessEqualDepth, LessEqualStencilFunc: LessEqualStencilFunc, LessStencilFunc: LessStencilFunc, Light: Light, LightProbe: LightProbe, Line: Line, Line3: Line3, LineBasicMaterial: LineBasicMaterial, LineCurve: LineCurve, LineCurve3: LineCurve3, LineDashedMaterial: LineDashedMaterial, LineLoop: LineLoop, LinePieces: LinePieces, LineSegments: LineSegments, LineStrip: LineStrip, LinearEncoding: LinearEncoding, LinearFilter: LinearFilter, LinearInterpolant: LinearInterpolant, LinearMipMapLinearFilter: LinearMipMapLinearFilter, LinearMipMapNearestFilter: LinearMipMapNearestFilter, LinearMipmapLinearFilter: LinearMipmapLinearFilter, LinearMipmapNearestFilter: LinearMipmapNearestFilter, LinearToneMapping: LinearToneMapping, Loader: Loader, LoaderUtils: LoaderUtils, LoadingManager: LoadingManager, LogLuvEncoding: LogLuvEncoding, LoopOnce: LoopOnce, LoopPingPong: LoopPingPong, LoopRepeat: LoopRepeat, LuminanceAlphaFormat: LuminanceAlphaFormat, LuminanceFormat: LuminanceFormat, MOUSE: MOUSE, Material: Material, MaterialLoader: MaterialLoader, Math: MathUtils, MathUtils: MathUtils, Matrix3: Matrix3, Matrix4: Matrix4, MaxEquation: MaxEquation, Mesh: Mesh, MeshBasicMaterial: MeshBasicMaterial, MeshDepthMaterial: MeshDepthMaterial, MeshDistanceMaterial: MeshDistanceMaterial, MeshFaceMaterial: MeshFaceMaterial, MeshLambertMaterial: MeshLambertMaterial, MeshMatcapMaterial: MeshMatcapMaterial, MeshNormalMaterial: MeshNormalMaterial, MeshPhongMaterial: MeshPhongMaterial, MeshPhysicalMaterial: MeshPhysicalMaterial, MeshStandardMaterial: MeshStandardMaterial, MeshToonMaterial: MeshToonMaterial, MinEquation: MinEquation, MirroredRepeatWrapping: MirroredRepeatWrapping, MixOperation: MixOperation, MultiMaterial: MultiMaterial, MultiplyBlending: MultiplyBlending, MultiplyOperation: MultiplyOperation, NearestFilter: NearestFilter, NearestMipMapLinearFilter: NearestMipMapLinearFilter, NearestMipMapNearestFilter: NearestMipMapNearestFilter, NearestMipmapLinearFilter: NearestMipmapLinearFilter, NearestMipmapNearestFilter: NearestMipmapNearestFilter, NeverDepth: NeverDepth, NeverStencilFunc: NeverStencilFunc, NoBlending: NoBlending, NoColors: NoColors, NoToneMapping: NoToneMapping, NormalAnimationBlendMode: NormalAnimationBlendMode, NormalBlending: NormalBlending, NotEqualDepth: NotEqualDepth, NotEqualStencilFunc: NotEqualStencilFunc, NumberKeyframeTrack: NumberKeyframeTrack, Object3D: Object3D, ObjectLoader: ObjectLoader, ObjectSpaceNormalMap: ObjectSpaceNormalMap, OctahedronBufferGeometry: OctahedronBufferGeometry, OctahedronGeometry: OctahedronGeometry, OneFactor: OneFactor, OneMinusDstAlphaFactor: OneMinusDstAlphaFactor, OneMinusDstColorFactor: OneMinusDstColorFactor, OneMinusSrcAlphaFactor: OneMinusSrcAlphaFactor, OneMinusSrcColorFactor: OneMinusSrcColorFactor, OrthographicCamera: OrthographicCamera, PCFShadowMap: PCFShadowMap, PCFSoftShadowMap: PCFSoftShadowMap, PMREMGenerator: PMREMGenerator, ParametricBufferGeometry: ParametricBufferGeometry, ParametricGeometry: ParametricGeometry, Particle: Particle, ParticleBasicMaterial: ParticleBasicMaterial, ParticleSystem: ParticleSystem, ParticleSystemMaterial: ParticleSystemMaterial, Path: Path, PerspectiveCamera: PerspectiveCamera, Plane: Plane, PlaneBufferGeometry: PlaneBufferGeometry, PlaneGeometry: PlaneGeometry, PlaneHelper: PlaneHelper, PointCloud: PointCloud, PointCloudMaterial: PointCloudMaterial, PointLight: PointLight, PointLightHelper: PointLightHelper, Points: Points, PointsMaterial: PointsMaterial, PolarGridHelper: PolarGridHelper, PolyhedronBufferGeometry: PolyhedronBufferGeometry, PolyhedronGeometry: PolyhedronGeometry, PositionalAudio: PositionalAudio, PropertyBinding: PropertyBinding, PropertyMixer: PropertyMixer, QuadraticBezierCurve: QuadraticBezierCurve, QuadraticBezierCurve3: QuadraticBezierCurve3, Quaternion: Quaternion, QuaternionKeyframeTrack: QuaternionKeyframeTrack, QuaternionLinearInterpolant: QuaternionLinearInterpolant, REVISION: REVISION, RGBADepthPacking: RGBADepthPacking, RGBAFormat: RGBAFormat, RGBAIntegerFormat: RGBAIntegerFormat, RGBA_ASTC_10x10_Format: RGBA_ASTC_10x10_Format, RGBA_ASTC_10x5_Format: RGBA_ASTC_10x5_Format, RGBA_ASTC_10x6_Format: RGBA_ASTC_10x6_Format, RGBA_ASTC_10x8_Format: RGBA_ASTC_10x8_Format, RGBA_ASTC_12x10_Format: RGBA_ASTC_12x10_Format, RGBA_ASTC_12x12_Format: RGBA_ASTC_12x12_Format, RGBA_ASTC_4x4_Format: RGBA_ASTC_4x4_Format, RGBA_ASTC_5x4_Format: RGBA_ASTC_5x4_Format, RGBA_ASTC_5x5_Format: RGBA_ASTC_5x5_Format, RGBA_ASTC_6x5_Format: RGBA_ASTC_6x5_Format, RGBA_ASTC_6x6_Format: RGBA_ASTC_6x6_Format, RGBA_ASTC_8x5_Format: RGBA_ASTC_8x5_Format, RGBA_ASTC_8x6_Format: RGBA_ASTC_8x6_Format, RGBA_ASTC_8x8_Format: RGBA_ASTC_8x8_Format, RGBA_BPTC_Format: RGBA_BPTC_Format, RGBA_ETC2_EAC_Format: RGBA_ETC2_EAC_Format, RGBA_PVRTC_2BPPV1_Format: RGBA_PVRTC_2BPPV1_Format, RGBA_PVRTC_4BPPV1_Format: RGBA_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT1_Format: RGBA_S3TC_DXT1_Format$1, RGBA_S3TC_DXT3_Format: RGBA_S3TC_DXT3_Format, RGBA_S3TC_DXT5_Format: RGBA_S3TC_DXT5_Format$1, RGBDEncoding: RGBDEncoding, RGBEEncoding: RGBEEncoding, RGBEFormat: RGBEFormat, RGBFormat: RGBFormat, RGBIntegerFormat: RGBIntegerFormat, RGBM16Encoding: RGBM16Encoding, RGBM7Encoding: RGBM7Encoding, RGB_ETC1_Format: RGB_ETC1_Format, RGB_ETC2_Format: RGB_ETC2_Format, RGB_PVRTC_2BPPV1_Format: RGB_PVRTC_2BPPV1_Format, RGB_PVRTC_4BPPV1_Format: RGB_PVRTC_4BPPV1_Format, RGB_S3TC_DXT1_Format: RGB_S3TC_DXT1_Format, RGFormat: RGFormat, RGIntegerFormat: RGIntegerFormat, RawShaderMaterial: RawShaderMaterial, Ray: Ray, Raycaster: Raycaster, RectAreaLight: RectAreaLight, RedFormat: RedFormat, RedIntegerFormat: RedIntegerFormat, ReinhardToneMapping: ReinhardToneMapping, RepeatWrapping: RepeatWrapping, ReplaceStencilOp: ReplaceStencilOp, ReverseSubtractEquation: ReverseSubtractEquation, RingBufferGeometry: RingBufferGeometry, RingGeometry: RingGeometry, SRGB8_ALPHA8_ASTC_10x10_Format: SRGB8_ALPHA8_ASTC_10x10_Format, SRGB8_ALPHA8_ASTC_10x5_Format: SRGB8_ALPHA8_ASTC_10x5_Format, SRGB8_ALPHA8_ASTC_10x6_Format: SRGB8_ALPHA8_ASTC_10x6_Format, SRGB8_ALPHA8_ASTC_10x8_Format: SRGB8_ALPHA8_ASTC_10x8_Format, SRGB8_ALPHA8_ASTC_12x10_Format: SRGB8_ALPHA8_ASTC_12x10_Format, SRGB8_ALPHA8_ASTC_12x12_Format: SRGB8_ALPHA8_ASTC_12x12_Format, SRGB8_ALPHA8_ASTC_4x4_Format: SRGB8_ALPHA8_ASTC_4x4_Format, SRGB8_ALPHA8_ASTC_5x4_Format: SRGB8_ALPHA8_ASTC_5x4_Format, SRGB8_ALPHA8_ASTC_5x5_Format: SRGB8_ALPHA8_ASTC_5x5_Format, SRGB8_ALPHA8_ASTC_6x5_Format: SRGB8_ALPHA8_ASTC_6x5_Format, SRGB8_ALPHA8_ASTC_6x6_Format: SRGB8_ALPHA8_ASTC_6x6_Format, SRGB8_ALPHA8_ASTC_8x5_Format: SRGB8_ALPHA8_ASTC_8x5_Format, SRGB8_ALPHA8_ASTC_8x6_Format: SRGB8_ALPHA8_ASTC_8x6_Format, SRGB8_ALPHA8_ASTC_8x8_Format: SRGB8_ALPHA8_ASTC_8x8_Format, Scene: Scene, SceneUtils: SceneUtils, ShaderChunk: ShaderChunk, ShaderLib: ShaderLib, ShaderMaterial: ShaderMaterial, ShadowMaterial: ShadowMaterial, Shape: Shape, ShapeBufferGeometry: ShapeBufferGeometry, ShapeGeometry: ShapeGeometry, ShapePath: ShapePath, ShapeUtils: ShapeUtils, ShortType: ShortType, Skeleton: Skeleton, SkeletonHelper: SkeletonHelper, SkinnedMesh: SkinnedMesh, SmoothShading: SmoothShading, Sphere: Sphere, SphereBufferGeometry: SphereBufferGeometry, SphereGeometry: SphereGeometry, Spherical: Spherical, SphericalHarmonics3: SphericalHarmonics3, Spline: Spline, SplineCurve: SplineCurve, SplineCurve3: SplineCurve3, SpotLight: SpotLight, SpotLightHelper: SpotLightHelper, Sprite: Sprite$1, SpriteMaterial: SpriteMaterial, SrcAlphaFactor: SrcAlphaFactor, SrcAlphaSaturateFactor: SrcAlphaSaturateFactor, SrcColorFactor: SrcColorFactor, StaticCopyUsage: StaticCopyUsage, StaticDrawUsage: StaticDrawUsage, StaticReadUsage: StaticReadUsage, StereoCamera: StereoCamera, StreamCopyUsage: StreamCopyUsage, StreamDrawUsage: StreamDrawUsage, StreamReadUsage: StreamReadUsage, StringKeyframeTrack: StringKeyframeTrack, SubtractEquation: SubtractEquation, SubtractiveBlending: SubtractiveBlending, TOUCH: TOUCH, TangentSpaceNormalMap: TangentSpaceNormalMap, TetrahedronBufferGeometry: TetrahedronBufferGeometry, TetrahedronGeometry: TetrahedronGeometry, TextBufferGeometry: TextBufferGeometry, TextGeometry: TextGeometry, Texture: Texture, TextureLoader: TextureLoader, TorusBufferGeometry: TorusBufferGeometry, TorusGeometry: TorusGeometry, TorusKnotBufferGeometry: TorusKnotBufferGeometry, TorusKnotGeometry: TorusKnotGeometry, Triangle: Triangle, TriangleFanDrawMode: TriangleFanDrawMode, TriangleStripDrawMode: TriangleStripDrawMode, TrianglesDrawMode: TrianglesDrawMode, TubeBufferGeometry: TubeBufferGeometry, TubeGeometry: TubeGeometry, UVMapping: UVMapping, Uint16Attribute: Uint16Attribute, Uint16BufferAttribute: Uint16BufferAttribute, Uint32Attribute: Uint32Attribute, Uint32BufferAttribute: Uint32BufferAttribute, Uint8Attribute: Uint8Attribute, Uint8BufferAttribute: Uint8BufferAttribute, Uint8ClampedAttribute: Uint8ClampedAttribute, Uint8ClampedBufferAttribute: Uint8ClampedBufferAttribute, Uniform: Uniform, UniformsLib: UniformsLib, UniformsUtils: UniformsUtils, UnsignedByteType: UnsignedByteType, UnsignedInt248Type: UnsignedInt248Type$1, UnsignedIntType: UnsignedIntType, UnsignedShort4444Type: UnsignedShort4444Type, UnsignedShort5551Type: UnsignedShort5551Type, UnsignedShort565Type: UnsignedShort565Type, UnsignedShortType: UnsignedShortType, VSMShadowMap: VSMShadowMap, Vector2: Vector2, Vector3: Vector3, Vector4: Vector4, VectorKeyframeTrack: VectorKeyframeTrack, Vertex: Vertex, VertexColors: VertexColors, VideoTexture: VideoTexture, WebGL1Renderer: WebGL1Renderer, WebGLCubeRenderTarget: WebGLCubeRenderTarget, WebGLMultisampleRenderTarget: WebGLMultisampleRenderTarget, WebGLRenderTarget: WebGLRenderTarget, WebGLRenderTargetCube: WebGLRenderTargetCube, WebGLRenderer: WebGLRenderer, WebGLUtils: WebGLUtils, WireframeGeometry: WireframeGeometry, WireframeHelper: WireframeHelper, WrapAroundEnding: WrapAroundEnding, XHRLoader: XHRLoader, ZeroCurvatureEnding: ZeroCurvatureEnding, ZeroFactor: ZeroFactor, ZeroSlopeEnding: ZeroSlopeEnding, ZeroStencilOp: ZeroStencilOp, sRGBEncoding: sRGBEncoding }); var points = []; var lines = []; var rings = []; var precision = 0.1; //容错精度 //正常是0.01 但是在编辑时容易出现交错的线看不出来,导致需要getSliceLines 然后多出新增点 var getPoint = function(o, type){ var point; if(typeof o == "string" || typeof o == "number")point = points.find(p=> p.ids.includes(o)); else { point = points.find(p=> math.closeTo(p.x , o.x, precision) && math.closeTo(p.y , o.y, precision) ); if(!point) point = new Point(o.x, o.y,{record:true, id:o.id}, type); else { //console.log('addPoint', point, o) point.addPoint(o.id); } } if(!point){ console.log("no point!"); } return point }; var getLine = function(id){ return lines.find(line=> line.ids.includes(id)); }; var getAngleInfo = function(points){ var info = {}; info.angle = points[1].clone().sub(points[0]).angle(); if(math.closeTo(info.angle, Math.PI*2)){ //如360-0.01 info.angle -= Math.PI*2; //有可能得到负数-0.001 }else if(info.angle > Math.PI || math.closeTo(info.angle, Math.PI)){//如180+-0.01 info.angle -= Math.PI; info.reverse = true; } return info //结果大约是 0 - 3.14 }; class Point extends Vector2{ constructor(x, y, o={}){ super(x, y); if(o.record){ this.id = o.id; if(this.id == void 0) this.id = "add_"+points.length; this.ids = [this.id] ;//存储拥有该坐标的点原始数据的id points.push(this); } this.type = o.type || ""; this.lines = []; } addPoint(id){ this.ids.push(id); } searchLineByFactor(dir, type, comeLine){ var lines = this.lines.filter(line=>line.searchTime<2); if(lines.length==0)return; else if(lines.length==1)return lines[0]; else lines = lines.filter(line=>line!=comeLine); if(lines.length==1)return lines[0]; var result; lines.forEach(line=>{ var vec = line.getVector(); if(line.points[1] == this) vec.negate(); var factor = math.getVec2Angle(dir, vec); if(new Vector3(dir.x, dir.y, 0).cross(new Vector3(vec.x, vec.y, 0)).z<0) factor*= -1; ///// if(!result){ result = {line, factor}; } else { if(type == "min" && factorresult.factor) result = {line, factor}; } }); return result.line; } } var lineLen=0; class Line$1{ constructor(o){ if(o.points[0] == o.points[1])return; this.points = o.points; this.type = o.type || 'line'; if(this.type == 'line'){ var oldLine = lines.find(line=>line.points.includes(o.points[0]) && line.points.includes(o.points[1])); if(oldLine){ o.id != void 0 && oldLine.ids.push(o.id); return oldLine; } this.id = o.id == void 0 ? ("line"+lineLen ++) : o.id; this.ids = [this.id]; o.dontWriteToPoint || this.points.forEach((point)=>{point.lines.push(this);}); o.isChild || lines.push(this); this.searchTime = 0; // 最多两次 } this.children = [];//分割 this.parents = [];//分割 this.match = []; } getAngleInfo(){ var angleInfo = getAngleInfo(this.points); this.angle = angleInfo.angle; this.reverse = angleInfo.reverse; } getIntersectWithLine(line, precision){ var joint = line.points.find(point=>this.points.includes(point)); if(joint)return {point:joint, type:"joint"}; var intersect = math.isLineIntersect( line.points , this.points , false, precision ); if(intersect) return {point: intersect, type:"intersect"}; } writeToPoint(){ this.points.forEach((point)=>{point.lines.includes(this) || point.lines.push(this);}); } checkIfParent(line){ if(this == line){ return true;//原因就是slice的点和端点很近 误差导致 } else return this.parents.find(e=>e.checkIfParent(line)) } splitByPoint(point){ var line1 = new Line$1({points:[point, this.points[0]], dontWriteToPoint:true, hasntsure:true}); var line2 = new Line$1({points:[point, this.points[1]], dontWriteToPoint:true, hasntsure:true}); if(!line1.points || !line2.points){//有至少一个是点相同的,没写到group.lines里 console.warn('splitByPoint 线有点相同'); return; } if(this.checkIfParent(line1)||this.checkIfParent(line2) || line1.checkIfParent(this) || line2.checkIfParent(this)){ console.warn("splitByPoint 发现parent和children一样");//,请检查getSliceWalls,尤其 if(math.closeTo(line1.angle,line2.angle)){ 处 return; } var deal = (line)=>{ this.children.push(line); line.parents.push(this); if(!lines.includes(line))lines.push(line); line.writeToPoint(); }; deal(line1); deal(line2); var index = this.points[0].lines.indexOf(this); index > -1 && this.points[0].lines.splice(index,1); var index = this.points[1].lines.indexOf(this); index > -1 && this.points[1].lines.splice(index,1); return [line1,line2] } splitByPoints(points){ points = points.map(point=>{return {dis:point.distanceTo(this.points[0]), point:point}}); points.sort((point1, point2)=>{return point1.dis - point2.dis}); var children = []; points.forEach((point, index)=>{ var line1 = new Line$1({points:[point.point, index==0?this.points[0]:points[index-1].point ],group:this.group , dontWriteToPoint:true, hasntsure:true}); children.push(line1); }); var line2 = new Line$1({points:[points[points.length-1].point, this.points[1] ],group:this.group , dontWriteToPoint:true, hasntsure:true}); children.push(line2); var a = children.find(line=> !line.points || this.checkIfParent(line) || line.checkIfParent(this)); if(a){ console.error("splitByPoints return"); return; } children.forEach(line=>{ this.children.push(line); line.parents.push(this); if(!lines.includes(line))lines.push(line); line.writeToPoint(); line.writeToPoint(); }); var index = this.points[0].lines.indexOf(this); index > -1 && this.points[0].lines.splice(index,1); var index = this.points[1].lines.indexOf(this); index > -1 && this.points[1].lines.splice(index,1); } getAllSlices(){//如果有被分割的片段 就返回片段,否则返回自身 var children = []; var traverse = function(elem){ if(elem.children.length == 0) children.push(elem); else elem.children.forEach(traverse); }; traverse(this); return children } getVector(){ return this.points[1].clone().sub(this.points[0]); } getLength(){ return this.points[0].distanceTo(this.points[1]) } getCenter(){ return this.points[1].clone().add(this.points[0]).multiplyScalar(.5); } } var getMixedSet = function(arr1, arr2){//交集 return arr1.filter(item=>arr2.includes(item)); }; var getUnionSet = function(arr1, arr2){//并集 return arr1.concat(arr2.filter(item=>!arr1.includes(item))) }; var getDifferenceSet = function(arr1, arr2){//差集 var arr11 = arr1.filter(item=>!arr2.includes(item)); var arr22 = arr2.filter(item=>!arr1.includes(item)); return arr11.concat(arr22) }; var getDifferenceSetMuti = function(arr){//收集绝对没有重复的元素,也就是判断出现次数=1的 var set = []; arr.forEach(arr1=>{ arr1.forEach(item=>{ var index = set.indexOf(item); if(index>-1){ set.splice(index, 1); }else { set.push(item); } }); }); return set; }; function DoorAtWhichLine(points, lines){ var mid = points[0].clone().add(points[1]).multiplyScalar(0.5); lines = lines.filter(line=>math.ifPointAtLineBound(mid, line.points, precision)); if(lines.length == 0)return var result = {line:null, dis:Infinity}; lines.forEach(line=>{ var foot = math.getFootPoint(mid, line.points[0], line.points[1] ); var dis = foot.distanceTo(mid); if(disline.points.includes(p1) && line.points.includes(p2) ) }; var ifSamePart = function(checkPart , part){//checkPart中所包含的part片段是否和基准part的顺序一样(逆序也可以, 中间有其他数也可以,起始不同也行。比如 01234和204一样的) var axis, startIndex, newCheckPart=[]; for(var j=0,len1 = checkPart.length; j-1)newCheckPart.push(checkPart[j]); } for(var i=0,len = part.length; i0}; //如果一样的话返回正逆是否相同 }; //或者判断是否有相同边(但是相同点是可以组成不同环) var ifSameRing = function(ring1, ring2){//判断两个环是否相等。 除了可以逆向外顺序要对 if(ring1 instanceof Ring)ring1 = ring1.points; if(ring2 instanceof Ring)ring2 = ring2.points; if(ring1.length != ring2.length)return false; if(ring1.lines && ring2.lines){ if(getDifferenceSet(ring1.lines , ring2.lines).length == 0)return true;//差集个数为0 }else { if(ifSamePart(ring1, ring2))return true } }; var atWhichChildLine = function(point, line, precision){ if(line.children.length == 0){//这里可能要放低精度 保证能找到 if(math.ifPointAtLineBound(point, line.points, precision)) return line; }else { for(var i=0;ip == point) && line2_.points.find(p=>p == point)){//这个点是line1_、 line2_端点,不做处理 //console.log("joint型 "+point.id) }else if(line1_.points.find(p=>p == point)){//T型交叉 line2_.splitByPoint(point);//加入到母线中,之后还先用母线判断交点 //console.log("T型交叉1 "+point.id) }else if(line2_.points.find(p=>p == point)){//T型交叉 line1_.splitByPoint(point); //console.log("T型交叉2 "+point.id) }else {//十字交叉 line1_.splitByPoint(point); line2_.splitByPoint(point); } }else { point = intersect.point;//交点是端点 if(math.closeTo(line1.angle,line2.angle)){ //重合一部分 var children1 = line1.getAllSlices(); var children2 = line2.getAllSlices(); if(children1.length>1 || children2.length>1){ //使用最小分割片段来比较 children1.forEach(child1=>{ children2.forEach(child2=>{ deal(child1, child2); }); }); return; } var anotherPoint1 = line1.points.find(point_=>point_!=point); var anotherPoint2 = line2.points.find(point_=>point_!=point); if(math.ifPointAtLineBound(anotherPoint1, line2.points)){ line2.splitByPoint(anotherPoint1); }else if(math.ifPointAtLineBound(anotherPoint2, line1.points)){ line1.splitByPoint(anotherPoint2); } } } }else if(math.closeTo(line1.angle,line2.angle)){ var vec1 = line1.getVector(); var vec = line1.points[0].clone().sub(line2.points[0]); var cos = math.getVec2Cos(vec1, vec); if(math.closeTo(cos, -1, 1e-4) || math.closeTo(cos, 1, 1e-4)){ //共线 var children1 = line1.getAllSlices(); var children2 = line2.getAllSlices(); if(children1.length>1 || children2.length>1){ //使用最小分割片段来比较 children1.forEach(child1=>{ children2.forEach(child2=>{ deal(child1, child2); }); }); return; } //判断是否重叠 var A = line1.points[0]; var C = line1.reverse == line2.reverse ? line2.points[0] : line2.points[1]; var B = line1.points[1]; var D = line1.reverse == line2.reverse ? line2.points[1] : line2.points[0]; var BC = C.clone().sub(B); var AD = D.clone().sub(A); if(BC.length()= 1.57 )return;//没有重叠部分 }else { var AB = B.clone().sub(A); if(math.getVec2Angle(AD, AB) >= 1.57 )return; } var f = function(line1,line2){ var one = math.ifPointAtLineBound(line1.points[0], line2.points); var two = math.ifPointAtLineBound(line1.points[1], line2.points); if(one && two){//line1在line2上 line2.splitByPoints( line1.points ); return true }else if(one || two){//错开 var point1 = one ? line1.points[0] : line1.points[1]; var anotherPoint1 = one ? line1.points[1] : line1.points[0]; var dis1 = line2.points[0].distanceTo(anotherPoint1); var dis2 = line2.points[1].distanceTo(anotherPoint1); var point2 = dis1 < dis2 ? line2.points[0] : line2.points[1]; line1.splitByPoint(point2); line2.splitByPoint(point1); return true } }; f(line1, line2) || f(line2, line1); } } }; for(let i=0;i{return line.children.length == 0}) //console.log("现有线条个数:"+lines.length) } var bound = new Box2(); var build = function(o){ //融合了相近点 //根据bound 处理precision o.points.forEach(p=>{ bound.expandByPoint(new Vector2(p.x,p.y)); }); if(o.precision != void 0){ precision = o.precision; }else { var boundSize = bound.getSize(new Vector2); precision = MathUtils.clamp(Math.max(boundSize.x, boundSize.y) / 70, 0.2, 2); } o.points.forEach(point=>getPoint(point));//{x:..,y:..} o.lines.forEach(line=>{ //{p1:id1. p2:id2} new Line$1({points:[getPoint(line.p1), getPoint(line.p2)], id:line.id }); }); //注意:不能出现一条线的两个点坐标一致,否则寻路时方向出错。 所以手动融合下相近点。 }; var searchRings = function(o={}){ points = []; lines = []; rings = []; lineLen = ringLen = 0; o.points = o.points || []; o.lines = o.lines || []; build(o); if(!o.dontSliceLines){ getSliceLines(); } //查找最小回路: //参考: 引入方向因子的最小回路、最大回路搜索算法.pdf //方法: 逆时针寻找(标记)最外层大环 -->从走过的点开始逆时针寻找最小环(直到所有可走的路被走过两次)-->逆时针寻找最外层大环(直到所有可走的路被走过两次)-->.. //其中找大环时选择方向因子最小的路, 而小环则相反(但只有开始第一条路是一样的, 都是选择最左边的点的因子最小的路)。 //标记方法: 每条线需要被搜索两次才算完毕。搜索完毕的线退出搜索。(依据:搜索完全部最小回路后 , 在无向图中删除搜索过 2 次的边及孤立节点得到退化图 , 恰好构成最大回路。) var searchTime = 0; var addRingJudgeCount = 0; var addRingJudge = function(ring, lines, connectedLines, type){// 处理拣出的片段 addRingJudgeCount++; //console.log("addRingJudge points("+ type+"):"+ ring.map(point=>point.id) ) if(o.onlyGetOutRing && type == "small")return if(type == "small" || o.onlyGetOutRing){//挑出回路: var newRings = []; while(ring.length){ var road = []; var turnBack = false; for(let i=0;i2){// 如果只有两个数,代表原路返回, 如 1->2(->1) if( !rings.find(ring_=>ifSameRing(pointArr, ring_))) newRings.push( new Ring({points: pointArr, lines:linesArr}) ); } turnBack = true; break; }else { road.push(ring[i]); turnBack = false; } } if(!turnBack){//没有重复的点,那么就直接处理整条。 if(ring.length>2){// 如果只有两个数,代表原路返回, 如 1->2(->1) if( !rings.find(ring_=>ifSameRing(ring, ring_))) newRings.push( new Ring({points: ring, lines}) ); } break; } } if(type != 'small'){ newRings.forEach(e=>e.isOutRing = true); } //console.log(newRings) }else { return ring } }; var search = function(point2d, comeRoad, type, connectedLines){ searchTime++; var goLine; var direction; if(type.includes("big")){ if(!comeRoad){ if(type.includes("Left")){//逆时针 direction = new Vector2(1,0); }else { direction = new Vector2(-1,0); } goLine = point2d.searchLineByFactor(direction,"min"); }else { var lastPoint = comeRoad.points[comeRoad.points.length-1]; direction = point2d.clone().sub(lastPoint); goLine = point2d.searchLineByFactor(direction,"min", findLine(point2d, lastPoint)); } }else { if(!comeRoad){ //似乎找最小环时,第一条线也是找最小的因子,这样才能保证逆时针(除非只有顺时针一条路) direction = new Vector2(1,0); goLine = point2d.searchLineByFactor(direction,"min"); }else { var lastPoint = comeRoad.points[comeRoad.points.length-1]; direction = point2d.clone().sub(lastPoint); goLine = point2d.searchLineByFactor(direction,"max", findLine(point2d, lastPoint)); } } if(!goLine)return goLine.searchTime++; connectedLines.includes(goLine) || connectedLines.push(goLine); var nextPoint = goLine.points.find( point => point2d!=point ); //if( comeRoad && comeRoad.points[comeRoad.points.length - 1] == nextPoint ) return;//不能查找来时的方向(反方向) //走不通就原路返回 var roadPoints = comeRoad ? comeRoad.points.concat([point2d]) : [point2d];//每个分叉都能构成一条新的road var roadLines = comeRoad ? comeRoad.lines.concat([goLine]) : [goLine]; //走到第一个点就算停止,这时候可能得到一个环、或者一段走了两遍的线、或者一条线上带了些环。 if(nextPoint == roadPoints[0]) return addRingJudge(roadPoints, roadLines, connectedLines, type) //形成环 else { /* var len = roadPoints.indexOf(nextPoint); if( len > -1){ //走到走过的路的某一点 构成这段路的回路 var points = roadPoints.slice(len, roadPoints.length); var lines = roadLines.slice(len, roadPoints.length); addRingJudge(points, lines) }else{ */ return search(nextPoint, {lines:roadLines, points:roadPoints}, type, connectedLines);//继续寻路 //} } }; while(1){//搜寻一次大环 var connectedLines = [];//被搜寻过的且searchTime<2的线。一旦全部搜完就说明该连通区域搜寻完毕,继续查下一个连通区域。 var startPoint = null; points.forEach(point=>{//找出x最小的点 if(!point.lines.find(line=>line.searchTime<2))return; if(!startPoint)startPoint = point; else if(point.x < startPoint.x)startPoint = point; }); if(!startPoint)break; //说明全部找完 var ring = search(startPoint, null, "bigLeft", connectedLines);//逆时针 //search(startPoint, null, "bigRight", connectedLines);//顺时针(为了防止最外层不是回路之前写了顺时针,但如果是回路就会走重复。后来发现只要逆时针即可,因为走完后剩下的可以再次找大环) connectedLines = connectedLines.filter(line=>line.searchTime<2); while(connectedLines.length>0){//目标是顺着connectedLines把所有连通的小环都找到 let points_ = [];//connectedLines中所有的点 connectedLines.forEach(line=>line.points.forEach(point=>{if(!points_.includes(point))points_.push(point); })); var startPoint = null; points_.forEach(point=>{//找出x最小的点 if(!point.lines.find(line=>line.searchTime<2))return; if(!startPoint)startPoint = point; else if(point.x < startPoint.x)startPoint = point; }); if(!startPoint)break; search(startPoint, null, "small", connectedLines); connectedLines = connectedLines.filter(line=>line.searchTime<2); } } /* if(o.onlyGetOutRing){ rings = rings.filter(e=>e.isOutRing) } */ //console.log("searchTime "+searchTime + ", addRingJudgeCount " +addRingJudgeCount) //找出所有的相邻关系,包括公共边 var len = rings.length; for(let i=0; i{ for(let i=0; i!r.isClockwise);//一般都是逆时针得到的,如果是顺时针,可能是贪吃蛇的情况,可能不是最小环,需要去掉。 if(wiseRings.length > 0){ //console.log('%c存在非最小的ring! 进行处理:',"color:#00f"); wiseRings.forEach(ring=>{ //(此案例验证出smallNeibours就是它的最小构成,可以再看看别的案例) if(ring.smallNeibours.length>0){//另:如果内部只有一个,说明它是最小环,不需要处理 var is = false; var difference = getDifferenceSet(ring.lines , getDifferenceSetMuti(ring.smallNeibours.concat(ring.child).map(ring=>ring.lines)));//获取所有smallNeibours和child的边中没有重复过的边(就是outline) 和该ring的线比较 is = difference.every(line=> ring.child.find(r=>r.lines.includes(line)) ); //多出的线只能是child中的线 if(is){ console.log('%c删除非最小环 ring'+ring.id,"color:#00f"); console.log(ring); rings.splice(rings.indexOf(ring), 1); ring.child.forEach(c=>{var index = c.parent.indexOf(ring);index>-1 && c.parent.splice(index,1);}); ring.parent.forEach(c=>{var index = c.child.indexOf(ring);index>-1 && c.child.splice(index,1);}); ring.smallNeibours.forEach(c=>{var index = c.smallNeibours.indexOf(ring);index>-1 && c.smallNeibours.splice(index,1);}); } } }); } /* rings = rings.filter(ring=>{ rings = rings.filter(ring=>{ var enoughSize = ring.area > 0.5 if(!enoughSize){console.log('因面积过小去除ring '+ring.id + " , area: "+ring.area)} return enoughSize }) rings.forEach(ring=>{ if(ring.closetChilds){ ring.closetChilds = ring.closetChilds.filter(e=>rings.includes(e)) } }) return rings }) */ //在dealRings前不能随意删除rings,因为判断是否是最小环时需要全部的环 rings.forEach(ring=>{ //这里和cad中的不太一样, cad中双数个parent算外环,单数内环; 这里不分内外, 只看有无parent child if(ring.parent.length){ ring.closetParent = ring.parent.find(ring_ => ring_.parent.length == ring.parent.length - 1);//最近一层的大环就是比它的parent个数少一的 ring.closetParent.closetChilds || (ring.closetParent.closetChilds = []);//内环可能多个 ring.closetParent.closetChilds.push(ring); } }); //console.log(rings) var _ring = rings.map(ring=>{ var data = { id: ring.id, points: ring.points.map(point=>{return {id: point.ids[0], x:point.x, y:point.y}}), /* doors : o.doors.filter(door=>{ if(ring.closetChilds){ var childOutLines = getDifferenceSetMuti(ring.closetChilds.map(ring=>ring.lines)) //最近子环的外边 return ring.lines.concat(childOutLines).includes(door.atLine) }else{ return ring.lines.includes(door.atLine) } }), */ area:ring.area, closetParent : ring.closetParent && ring.closetParent.id, closetChilds : ring.closetChilds && ring.closetChilds.map(e=>e.id) }; return data }); //console.log(JSON.stringify(_ring)) return _ring }; var easing = {}; //渐变曲线函数,反应加速度的变化 //currentTime:x轴当前时间(从0-到duration), startY:起始点, duration:总时长, wholeY:路程 (即endY-startY) //参数基本是 x, 0, 1, 1 /* easeOut 基本是y= m * (x-dur)^k + n, 若k为偶数,m<0, 若k为奇数,m>0; (因为偶数的话必须开口向下才能获得斜率递减的递增的那段,而奇数是对称的,单调递增. ) 根据x=0时y=0, x=dur时y=S , 得 n = S,m = -S/(-dur)^k */ easing.getEaseOut = function(k){// k 是>=2的整数. 越大变化率越大, 相同初始速度所需要时间越久 let easeFun; k = Math.round(k); if(k<2){ k = Math.PI / 2; easeFun = easing.easeOutSine; }else { easeFun = function(currentTime, startY, wholeY, duration) { if(k>2){ console.log(k); } return -wholeY/Math.pow(-duration, k) * Math.pow(currentTime-duration, k) + wholeY }; } return { k, easeFun } }; easing.linearTween = function(currentTime, startY, wholeY, duration) { return wholeY * currentTime / duration + startY } , easing.easeInQuad = function(currentTime, startY, wholeY, duration) { return currentTime /= duration, wholeY * currentTime * currentTime + startY } , easing.easeOutQuad = function(currentTime, startY, wholeY, duration) { // 如套上实际的距离S和时长dur, y = - S / dur *(x^2-2x) 当s为1,dur为1时,是 y = -(x-1)^2 + 1 , 在0-1中是斜率递减的递增函数. 导数- S / dur *(2x-2 ) 可求出实时速度 故在0这一时刻,速度为 2S/dur return currentTime /= duration, -wholeY * currentTime * (currentTime - 2) + startY } , easing.easeInOutQuad = function(currentTime, startY, wholeY, duration) { return currentTime /= duration / 2, currentTime < 1 ? wholeY / 2 * currentTime * currentTime + startY : (currentTime--, -wholeY / 2 * (currentTime * (currentTime - 2) - 1) + startY) } , easing.easeInCubic = function(currentTime, startY, wholeY, duration) { return currentTime /= duration, wholeY * currentTime * currentTime * currentTime + startY } , easing.easeOutCubic = function(currentTime, startY, wholeY, duration) {// y = S / dur^3 *(x-dur)^3 + S,对称中心是(dur,S),从0-dur是 斜率递减的递增函数,导数为3S/dur^3 * (x-dur)^2, 0时速度为3S/dur return currentTime /= duration, currentTime--, wholeY * (currentTime * currentTime * currentTime + 1) + startY } , easing.easeInOutCubic = function(currentTime, startY, wholeY, duration) { return currentTime /= duration / 2, currentTime < 1 ? wholeY / 2 * currentTime * currentTime * currentTime + startY : (currentTime -= 2, wholeY / 2 * (currentTime * currentTime * currentTime + 2) + startY) } , easing.easeInQuart = function(currentTime, startY, wholeY, duration) { return currentTime /= duration, wholeY * currentTime * currentTime * currentTime * currentTime + startY } , easing.easeOutQuart = function(currentTime, startY, wholeY, duration) {//根据上面的计算,估计0时速度应该是4S/dur吧…… return currentTime /= duration, currentTime--, -wholeY * (currentTime * currentTime * currentTime * currentTime - 1) + startY } , easing.easeInOutQuart = function(currentTime, startY, wholeY, duration) { return currentTime /= duration / 2, currentTime < 1 ? wholeY / 2 * currentTime * currentTime * currentTime * currentTime + startY : (currentTime -= 2, -wholeY / 2 * (currentTime * currentTime * currentTime * currentTime - 2) + startY) } , easing.easeInQuint = function(currentTime, startY, wholeY, duration) { return currentTime /= duration, wholeY * currentTime * currentTime * currentTime * currentTime * currentTime + startY } , easing.easeOutQuint = function(currentTime, startY, wholeY, duration) { return currentTime /= duration, currentTime--, wholeY * (currentTime * currentTime * currentTime * currentTime * currentTime + 1) + startY } , easing.easeInOutQuint = function(currentTime, startY, wholeY, duration) { return currentTime /= duration / 2, currentTime < 1 ? wholeY / 2 * currentTime * currentTime * currentTime * currentTime * currentTime + startY : (currentTime -= 2, wholeY / 2 * (currentTime * currentTime * currentTime * currentTime * currentTime + 2) + startY) } , easing.easeInSine = function(currentTime, startY, wholeY, duration) { return -wholeY * Math.cos(currentTime / duration * (Math.PI / 2)) + wholeY + startY } , easing.easeOutSine = function(currentTime, startY, wholeY, duration) {// y' = S * PI / 2 / dur * cos(PI/2/dur * x) return wholeY * Math.sin(currentTime / duration * (Math.PI / 2)) + startY } , easing.easeInOutSine = function(currentTime, startY, wholeY, duration) { return -wholeY / 2 * (Math.cos(Math.PI * currentTime / duration) - 1) + startY } , easing.easeInExpo = function(currentTime, startY, wholeY, duration) { return wholeY * Math.pow(2, 10 * (currentTime / duration - 1)) + startY } , easing.easeOutExpo = function(currentTime, startY, wholeY, duration) { return wholeY * (-Math.pow(2, -10 * currentTime / duration) + 1) + startY } , easing.easeInOutExpo = function(currentTime, startY, wholeY, duration) { return currentTime /= duration / 2, currentTime < 1 ? wholeY / 2 * Math.pow(2, 10 * (currentTime - 1)) + startY : (currentTime--, wholeY / 2 * (-Math.pow(2, -10 * currentTime) + 2) + startY) } , easing.easeInCirc = function(currentTime, startY, wholeY, duration) { return currentTime /= duration, -wholeY * (Math.sqrt(1 - currentTime * currentTime) - 1) + startY } , easing.easeOutCirc = function(currentTime, startY, wholeY, duration) { return currentTime /= duration, currentTime--, wholeY * Math.sqrt(1 - currentTime * currentTime) + startY } , easing.easeInOutCirc = function(currentTime, startY, wholeY, duration) { return currentTime /= duration / 2, currentTime < 1 ? -wholeY / 2 * (Math.sqrt(1 - currentTime * currentTime) - 1) + startY : (currentTime -= 2, wholeY / 2 * (Math.sqrt(1 - currentTime * currentTime) + 1) + startY) } , easing.easeInElastic = function(currentTime, startY, wholeY, duration) { var r = 1.70158 , o = 0 , a = wholeY; return 0 === currentTime ? startY : 1 === (currentTime /= duration) ? startY + wholeY : (o || (o = .3 * duration), a < Math.abs(wholeY) ? (a = wholeY, r = o / 4) : r = o / (2 * Math.PI) * Math.asin(wholeY / a), -(a * Math.pow(2, 10 * (currentTime -= 1)) * Math.sin((currentTime * duration - r) * (2 * Math.PI) / o)) + startY) } , easing.easeOutElastic = function(currentTime, startY, wholeY, duration) { var r = 1.70158 , o = 0 , a = wholeY; return 0 === currentTime ? startY : 1 === (currentTime /= duration) ? startY + wholeY : (o || (o = .3 * duration), a < Math.abs(wholeY) ? (a = wholeY, r = o / 4) : r = o / (2 * Math.PI) * Math.asin(wholeY / a), a * Math.pow(2, -10 * currentTime) * Math.sin((currentTime * duration - r) * (2 * Math.PI) / o) + wholeY + startY) } , easing.easeInOutElastic = function(currentTime, startY, wholeY, duration) { var r = 1.70158 , o = 0 , a = wholeY; return 0 === currentTime ? startY : 2 === (currentTime /= duration / 2) ? startY + wholeY : (o || (o = duration * (.3 * 1.5)), a < Math.abs(wholeY) ? (a = wholeY, r = o / 4) : r = o / (2 * Math.PI) * Math.asin(wholeY / a), currentTime < 1 ? -.5 * (a * Math.pow(2, 10 * (currentTime -= 1)) * Math.sin((currentTime * duration - r) * (2 * Math.PI) / o)) + startY : a * Math.pow(2, -10 * (currentTime -= 1)) * Math.sin((currentTime * duration - r) * (2 * Math.PI) / o) * .5 + wholeY + startY) } , easing.easeInBack = function(currentTime, startY, wholeY, duration, r) { return void 0 === r && (r = 1.70158), wholeY * (currentTime /= duration) * currentTime * ((r + 1) * currentTime - r) + startY } , easing.easeOutBack = function(currentTime, startY, wholeY, duration, r) { return void 0 === r && (r = 1.70158), wholeY * ((currentTime = currentTime / duration - 1) * currentTime * ((r + 1) * currentTime + r) + 1) + startY } , easing.easeInOutBack = function(currentTime, startY, wholeY, duration, r) { return void 0 === r && (r = 1.70158), (currentTime /= duration / 2) < 1 ? wholeY / 2 * (currentTime * currentTime * (((r *= 1.525) + 1) * currentTime - r)) + startY : wholeY / 2 * ((currentTime -= 2) * currentTime * (((r *= 1.525) + 1) * currentTime + r) + 2) + startY } , easing.easeOutBounce = function(currentTime, startY, wholeY, duration) { return (currentTime /= duration) < 1 / 2.75 ? wholeY * (7.5625 * currentTime * currentTime) + startY : currentTime < 2 / 2.75 ? wholeY * (7.5625 * (currentTime -= 1.5 / 2.75) * currentTime + .75) + startY : currentTime < 2.5 / 2.75 ? wholeY * (7.5625 * (currentTime -= 2.25 / 2.75) * currentTime + .9375) + startY : wholeY * (7.5625 * (currentTime -= 2.625 / 2.75) * currentTime + .984375) + startY } , easing.easeInBounce = function(currentTime, startY, wholeY, r) { return wholeY - easing.easeOutBounce(r - currentTime, 0, wholeY, r) + startY } , easing.easeInOutBounce = function(currentTime, startY, wholeY, r) { return currentTime < r / 2 ? .5 * easing.easeInBounce(2 * currentTime, 0, wholeY, r) + startY : .5 * easing.easeOutBounce(x, 2 * currentTime - r, 0, wholeY, r) + .5 * wholeY + startY }; var lerp = { /* vector: function(currentTime, startY, f) {//xzw change, add f var wholeY = currentTime.clone(); return startY = startY.clone(), function(duration) { currentTime.set(wholeY.x * (1 - duration) + startY.x * duration, wholeY.y * (1 - duration) + startY.y * duration, wholeY.z * (1 - duration) + startY.z * duration) f && f(currentTime,duration); } }, quaternion: function(currentTime, startY, f) {//xzw change, add f var wholeY = currentTime.clone(); return function(duration) { currentTime.copy(wholeY).slerp(startY, duration); f && f(currentTime,duration); } }, property: function(currentTime, startY, wholeY, duration) { var r = currentTime[startY]; return function(o) { currentTime[startY] = r * (1 - o) + wholeY * o, duration && duration(currentTime[startY]) } }, uniform: function(currentTime, startY, wholeY) { var duration = currentTime.material.uniforms[startY].value; return function(r) { try{ currentTime.material.uniforms[startY] && (currentTime.material.uniforms[startY].value = duration * (1 - r) + wholeY * r) }catch(currentTime){ console.log(1) } } }, matrix4: function(currentTime, startY) { var wholeY = currentTime.clone(); return function(duration) { for (var r = currentTime.elements, o = wholeY.elements, a = startY.elements, s = 0; s < 16; s++) r[s] = o[s] * (1 - duration) + a[s] * duration } }, allUniforms: function(currentTime, startY, wholeY) { var duration = currentTime.map(function(currentTime) { return this.uniform(currentTime, startY, wholeY) } .bind(this)); return function(currentTime) { duration.forEach(function(startY) { startY(currentTime) }) } } */ vector: function(t, i, f) {//xzw change, add f var n = t.clone(); return i = i.clone(), function(e, delta) { t.set(n.x * (1 - e) + i.x * e, n.y * (1 - e) + i.y * e, n.z * (1 - e) + i.z * e); f && f(t,e, delta); } }, quaternion: function(t, i, f) {//xzw change, add f var n = t.clone(); return function(e) { t.copy(n).slerp(i, e); f && f(t,e); } }, property: function(t, i, n, r) { var o = t[i]; return function(e) { t[i] = o * (1 - e) + n * e, r && r(t[i]); } }, uniform: function(t, i, n) { var r = t.material.uniforms[i].value; return function(e) { t.material.uniforms[i] && (t.material.uniforms[i].value = r * (1 - e) + n * e); } }, matrix4: function(o, a) { var s = o.clone(); return function(e) { for (var t = o.elements, i = s.elements, n = a.elements, r = 0; r < 16; r++) t[r] = i[r] * (1 - e) + n[r] * e; } }, allUniforms: function(e, t, i) { var n = e.map(function(e) { return this.uniform(e, t, i) } .bind(this)); return function(t) { n.forEach(function(e) { e(t); }); } } }; /* 渐变 */ var transitions = { globalDone: null, funcs: [], counter: 0, uniqueID: 0, start: function(func, duration, done, delay, ease, name, id, cancelFun, ignoreFirstFrame=true) { return delay = delay || 0, this.funcs.push({ func: func, current: -delay * Math.abs(duration), //当前时间 duration: (1 - Math.max(delay, 0)) * Math.abs(duration), //总时长 done: done, easing: ease || easing.linearTween, //渐变曲线 cycling: duration < 0, running: !0, debug: delay < 0, name: name || "T" + this.counter, id: void 0 === id ? this.counter : id, paused: !1, cancelFun : cancelFun, //取消时执行的函数 updateCount:0, ignoreFirstFrame, }), func(0, 16), this.counter += 1, func }, trigger: function(e) { var t = void 0 === e.delayRatio ? 0 : e.delayRatio , u = e.func || function() {} , r = void 0 === e.duration ? 0 : e.duration; void 0 !== e.cycling && e.cycling && (r = -Math.abs(r)); var o = e.done || null , a = e.easing || easing.linearTween , s = e.name || "R" + this.counter , l = void 0 === e.id ? this.counter : e.id; return this.start(u, r, o, t, a, s, l) }, setTimeout: function(e, t, u) { var duration = void 0 === u ? this.counter : u; return this.trigger({ done: e, duration: void 0 === t ? 0 : t, name: "O" + this.counter, id: duration }) }, pause: function() { this.paused = !0; }, resume: function() { this.paused = !1; }, update: function(e) { this.funcs.forEach(function(t) { if(t.updateCount++ == 0 && t.ignoreFirstFrame) return //add start可能发生在一帧中任意时刻,而每次update的是在一帧中的固定时刻,所以从start到第一次update的时间并不是所传入的delta,该delta 是上一帧的update到这一帧的update的耗时。 故去掉了第一次的update,相当于延迟一帧再update. if (!(t.paused || (t.current += 1e3 * e, t.current < 0))){ if (t.current >= t.duration && !t.cycling) { var u = t.easing(1, 0, 1, 1); t.func(u, 1e3 * e), t.done && t.done(), t.running = !1; } else { var duration = t.easing(t.current % t.duration / t.duration, 0, 1, 1) , r = t.func(duration, 1e3 * e) || !1; r && (t.done && t.done(), t.running = !1); } } }); var t = this.funcs.length; this.funcs = this.funcs.filter(function(e) { return e.running }); var u = this.funcs.length; if (t > 0 && 0 === u && this.globalDone) { var duration = this.globalDone; this.globalDone = null, duration(); } }, adjustSpeed: function(e, t) { for (var u = this.getById(e), duration = 0; duration < u.length; duration++) { var r = u[duration]; r.duration /= t, r.current /= t; } }, getById: function(e) { return this.funcs.filter(function(t) { return e === t.id }) }, get: function(e) { for (var t = 0; t < this.funcs.length; t += 1) if (this.funcs[t].func === e) return this.funcs[t]; return null }, isRunning: function(e) { var t = this.get(e); return null !== t && t.running }, countActive: function() { for (var e = 0, t = 0; t < this.funcs.length; t += 1) e += this.funcs[t].running; return e }, listActive: function() { for (var e = [], t = 0; t < this.funcs.length; t += 1) this.funcs[t].running && e.push(this.funcs[t].name); return e }, done: function(e) { this.globalDone = e; }, cancelById: function(e, dealCancelFun) { //xzw add dealDone var t = void 0 === e ? 0 : e; let cancels = []; this.funcs = this.funcs.filter(function(e) { var is = e.id == t; if(is && dealCancelFun){ e.cancelFun && cancels.push(e.cancelFun); //e.cancelFun && e.cancelFun() } return !is }); cancels.forEach(e=>{e();}); //先从funcs中去除后再执行 }, cancel: function(e) { this.funcs = this.funcs.filter(function(t) { return t.func !== e }); }, getUniqueId: function() { return this.uniqueID -= 1, this.uniqueID } }; //THREE.Vector2.name2 = 'Common' var Common = { sortByScore: function(list, request, rank){ var i = request ? Common.filterAll(list, request) : list; return 0 === i.length ? [] : i = i.map(function(e) { let results = rank.map(function(f){return f(e)}); let scores = results.map(e=>e.score != void 0 ? e.score : e); let logs = results.map(e=>e.log); return { item: e, scores, logs, score: scores.reduce(function(t, i) {//总分 return t + i }, 0) } }).sort(function(e, t) { return t.score - e.score; }) } , filterAll: function(e, t) { return e.filter(function (e) { return t.every(function (t) { return t(e) }) }) }, //--------------- find : function(list, request, rank, sortByScore ) { if(sortByScore){ var r = this.sortByScore(list, request, rank); return r[0] && r[0].item }else { var i = request ? Common.filterAll(list, request) : list; return 0 === i.length ? null : (rank && rank.forEach(function(e) { i = Common.stableSort(i, e); }), i[0]) } } , stableSort: function(e, f) {//用到排序函数,涉及到两个item相减 return e.map(function(e, i) { return { value: e, index: i } }).sort(function(e, u) { var n = f(e.value, u.value); return 0 !== n ? n : e.index - u.index //似乎就是加多了这一步:若差距为0,按照原顺序 }).map(function(e) { return e.value }) }, average: function (e, t) { if (0 === e.length) return null; for (var i = 0, n = 0, r = 0; r < e.length; r++) { var o = t ? e[r][t] : e[r]; i += o, n++; } return i / n }, //--------------------------- getMixedSet : function(arr1, arr2){//交集 return arr1.filter(item=>arr2.includes(item)); }, getUnionSet : function(arr1, arr2){//并集 return arr1.concat(arr2.filter(item=>!arr1.includes(item))) }, getDifferenceSet : function(arr1, arr2){//差集 不能识别重复的,如getDifferenceSet([1,2,2],[1,1,2]) 为空 var arr11 = arr1.filter(item=>!arr2.includes(item)); var arr22 = arr2.filter(item=>!arr1.includes(item)); return arr11.concat(arr22) }, getDifferenceSetMuti : function(arr){//收集绝对没有重复的元素,也就是判断出现次数=1的 var set = []; arr.forEach(arr1=>{ arr1.forEach(item=>{ var index = set.indexOf(item); if(index>-1){ set.splice(index, 1); }else { set.push(item); } }); }); return set; } , CloneJson : function(data){ var str = JSON.stringify(data); return JSON.parse(str) } , CloneObject : function (copyObj, isSimpleCopy, simpleCopyList = [], judgeSimpleCopyFun) { //isSimpleCopy 只复制最外层 //复制json result的可能:普通数字或字符串、普通数组、复杂对象 judgeSimpleCopyFun || (judgeSimpleCopyFun=()=>{}); if (!copyObj || typeof copyObj == 'number' || typeof copyObj == 'string' ||copyObj.isObject3D || copyObj instanceof Function || simpleCopyList.some(className => copyObj instanceof className) || judgeSimpleCopyFun(copyObj)) { return copyObj } if (copyObj instanceof Array) { return copyObj.map(e => { return this.CloneObject(e, isSimpleCopy, simpleCopyList, judgeSimpleCopyFun) }) } else { if (copyObj.clone instanceof Function) { //解决一部分 return copyObj.clone() } } let result = {}; for (var key in copyObj) { if (copyObj[key] instanceof Object && !isSimpleCopy ) result[key] = this.CloneObject(copyObj[key], isSimpleCopy, simpleCopyList, judgeSimpleCopyFun); else result[key] = copyObj[key]; //如果是函数类同基本数据,即复制引用 } return result } , CloneClassObject :function(copyObj, {ignoreList=[],simpleCopyList=[]}={}){//复杂类对象 var newobj = new copyObj.constructor(); this.CopyClassObject(newobj, copyObj, {ignoreList,simpleCopyList}); return newobj } , CopyClassObject :function(targetObj, copyObj, {ignoreList=[],simpleCopyList=[]}={}){//复杂类对象 for(let i in copyObj){ if(i in copyObj.__proto__)break; //到函数了跳出 if(ignoreList.includes(i)){ continue; }else if(simpleCopyList.includes(i)){ targetObj[i] = copyObj[i]; }else { targetObj[i] = this.CloneObject(copyObj[i], false, simpleCopyList ); } /* else if(copyObj[i].clone instanceof Function ){ targetObj[i] = copyObj[i].clone() }else{ targetObj[i] = copyObj[i]; } */ } } , ifSame : function(object1, object2, simpleEqualClass=[]){ //对于复杂的类对象,若能简单判断就直接写进simpleEqualClass if(object1 == object2 )return true // 0 != undefined , 0 == '' else if(!object1 || !object2) return false else if(object1.constructor != object2.constructor){ return false }else if(simpleEqualClass.some(className => object1 instanceof className)){ return object1 == object2 }else if(object1 instanceof Array ) { if(object1.length != object2.length)return false; var _object2 = object2.slice(0); for(let i=0;iCommon.ifSame(object1[i], e, simpleEqualClass)); if(u == void 0 && !_object2.includes(u) && !object1.includes(u))return false; else { let index = _object2.indexOf(u); _object2.splice(index,1); } } return true }else if(object1.equals instanceof Function ){//复杂数据仅支持这种,其他的可能卡住? return object1.equals(object2) }else if(typeof object1 == 'number' || typeof object1 == 'string'){ if(isNaN(object1) && isNaN(object2))return true else return object1 == object2 }else if(typeof object1 == "object"){ var keys1 = Object.keys(object1); var keys2 = Object.keys(object2); if(!Common.ifSame(keys1,keys2,simpleEqualClass))return false; for(let i in object1){ var same = Common.ifSame(object1[i], object2[i],simpleEqualClass); if(!same)return false } return true }else { console.log('isSame出现例外'); } } , downloadFile : function(data, filename, cb) { var save_link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a'); save_link.href = data; save_link.download = filename; var event = document.createEvent('MouseEvents'); event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); save_link.dispatchEvent(event); cb && cb(); }, replaceAll : function (str, f, e) { //f全部替换成e var reg = new RegExp(f, "g"); //创建正则RegExp对象 return str.replace(reg, e); }, dealURL(url){ let urlNew = this.replaceAll(url, "\\+", "%2B");// 浏览器似乎不支持访问带+的地址 urlNew = this.replaceAll(urlNew, "/.//", "/"); //去除双斜杠(/.//) return urlNew }, getNameFromURL(url){ if(!url)return '' let get = (e)=>{ return e.split('/').pop() }; if(url instanceof Array){ return url.map(e=>get(e)) } return get(url) }, //--------------------------- intervalTool:{ //延时update,防止卡顿 list:[], /* isWaiting:function(name, func, delayTime){ if(!this.list.includes(name)){ //如果没有该项, 则开始判断 var needWait = func(); //触发了改变,则等待一段时间后再自动判断 if(needWait){ this.list.push(name); setTimeout(()=>{ var a = this.list.indexOf(name); this.list.splice(a,1); this.isWaiting(name, func, delayTime) //循环 },delayTime) } } }, */ isWaiting:function(name, func, delayTime/* , autoCycle */){ let item = this.list.find(e=>e.name == name); if(!item){ //如果没有该项, 则加入循环 let ifContinue = func(); item = {name, func, delayTime}; this.list.push(item); setTimeout(()=>{ var a = this.list.indexOf(item); this.list.splice(a,1); let {func, delayTime} = item; if(item.requestUpdate || ifContinue ) this.isWaiting(name, func, delayTime); //循环 },delayTime); }else {//如果有该项,说明现在请求下一次继续更新 //if(delayTime == 0){//想立刻更新一次 // func() //}else{ //更新属性 item.func = func; item.delayTime = delayTime; item.requestUpdate = true; //} } }, } , pushToGroupAuto : function(items, groups, recognizeFunction, recognizeGroup){//自动分组。 items是将分到一起的组合。items.length = 1 or 2. recognizeFunction = recognizeFunction || function(){}; if(recognizeGroup){ //有更复杂的识别处理,直接传递整个组 var atGroups = groups.filter(group=>recognizeGroup(group, items)); }else { var atGroups = groups.filter(group=>group.find( item => items[0] == item || recognizeFunction(item, items[0]) || items[1] == item || items[1] && recognizeFunction(item, items[1]) )); } if(atGroups.length){//在不同组 //因为items是一组的,所以先都放入组1 items.forEach(item=> {if(!atGroups[0].includes(item)) atGroups[0].push(item);}); if(atGroups.length>1){//如果在不同组,说明这两个组需要合并 var combineGroup = []; atGroups.forEach(group=>{ combineGroup = Common.getUnionSet(combineGroup, group); groups.splice(groups.indexOf(group),1); }); groups.push(combineGroup); } }else {//直接加入为一组 groups.push(items); } }, getBestCount : (function(){ let lastCount = {}; return function(name, minCount=1,maxCount=6, durBound1 = 1.2, durBound2 = 10, ifLog, maxHistory){ let timeStamp = performance.getEntriesByName("loop-start"); let count; if(timeStamp.length){ let dur = performance.now() - timeStamp[timeStamp.length-1].startTime; //dur在iphoneX中静止有7,pc是2 count = Math.round(math.linearClamp(dur, [durBound1,durBound2], [maxCount, minCount])); if (maxHistory) { if (!lastCount[name]) lastCount[name] = []; if (count == 0 && lastCount[name].length > maxHistory - 1 && !lastCount[name].some(e => e > 0)) { count = 1; } lastCount[name].push(count); if (lastCount[name].length > maxHistory) lastCount[name].splice(0, 1); } if(ifLog){//注意,console.log本身用时挺高, 降4倍时可能占用0.5毫秒 name && count && console.log(name, count , ' ,dur:', dur.toFixed(3)); } }else { count = maxCount; // ? } //主要在手机端有效果。 return count } })(), batchHandling : {//分批处理 lists:[], getSlice : function(name, items , {stopWhenAllUsed, min=5,max=100, durBound1 , durBound2, useEquals , maxUseCount}){ if(items.length == 0 || ((maxUseCount = maxUseCount == void 0 ? Common.getBestCount(name, min,max , durBound1, durBound2 /* , true */ ) : maxUseCount), !maxUseCount) //本次最多可以使用的个数 ){ return {list:[]} } if(!this.lists[name]) this.lists[name] = {list:[] }; //更新列表项目,但不变原来的顺序 let list = this.lists[name].list.filter(a=>items.some(item=> useEquals ? a.item.equals(item) : a.item == item));//去掉已经不在items里的项目 this.lists[name].list = list; items.forEach(item=>{//增加新的项目。 if(!list.some(a=>useEquals ? a.item.equals(item) : a.item == item )){ list.push({item, count:0}); } }); //至此,在后排的都是未使用的 let unUsed = list.filter(e=>e.count == 0);//未使用的项目(count为0)优先 let result = []; unUsed.slice(0,maxUseCount).forEach(e=>{ result.push(e.item); e.count ++; }); if(unUsed.length > maxUseCount){ //还是剩有未使用的项目,等待下一次 }else { //所有项目都能使用一次 if(!stopWhenAllUsed){ //若不是全部使用就停止 let wholeCount = Math.min(items.length, maxUseCount); let restCount = wholeCount - result.length; //补齐 list.slice(0,restCount).forEach(e=>{ result.push(e.item); e.count ++; }); } list.forEach(e=>e.count--); //复原,等待新的循环 } /* result.forEach((e,i)=>{//有重复的 if( result.slice(0,i).some(a=>a.equals(e)) || result.slice(i+1).some(a=>a.equals(e)) ) { console.log(e) } }) */ return {list:result } } }, getRootWindow(){//获取包含Potree的根window let win = window; try{ while(win.parent!=win && win.parent.Potree){ win = win.parent; } if(window != win)return win }catch(e){ //console.log(e) //可能跨域,从而win.parent.Potree报错 console.log('getRootWindow 跨域'); return } }, watch: function(object, propName, initialValue){ //监听某个属性的变化 let v = initialValue; Object.defineProperty(object, propName, { get: function() { return v }, set: function(e) { console.warn('watch:',propName, e); v = e; } }); }, imgAddLabel : function (img, labelImg, labelInfo = {}) { //图上加另一张小图,用于添加水印 let canvas; if(img instanceof Image){ canvas = document.createElement('canvas'); }else { canvas = img; } let context = canvas.getContext('2d'); let marginLeft = labelInfo.bgMargin && labelInfo.bgMargin.left || 0; let marginRight = labelInfo.bgMargin && labelInfo.bgMargin.right || 0; let marginTop = labelInfo.bgMargin && labelInfo.bgMargin.top || 0; let marginBottom = labelInfo.bgMargin && labelInfo.bgMargin.bottom || 0; if(img instanceof Image){//如果img是canvas,说明已绘制在canvas上了就不用绘制了 let width = img.width + marginLeft + marginRight; let height = img.height + marginTop + marginBottom; canvas.width = width; canvas.height = height; if(labelInfo.bgColor){ context.fillStyle = 'rgba(' + labelInfo.bgColor.r + ',' + labelInfo.bgColor.g + ',' + labelInfo.bgColor.b + ',' + labelInfo.bgColor.a + ')'; context.fillRect(0,0,width,height); } context.drawImage(img, marginLeft, marginTop, img.width, img.height); } let labelWidth = labelInfo.widthRatioToImg ? img.width * labelInfo.widthRatioToImg : labelImg.width; //widthRatioToImg:label的width占img的width的比例 let labelHeight = (labelWidth * labelImg.height) / labelImg.width; if (labelInfo.leftRatioToImg == void 0 && labelInfo.rightRatioToImg != void 0) { labelInfo.leftRatioToImg = 1 - labelInfo.rightRatioToImg - (labelInfo.widthRatioToImg || labelImg.width / img.width); } if (labelInfo.topRatioToImg == void 0 && labelInfo.bottomRatioToImg != void 0) { labelInfo.topRatioToImg = 1 - labelInfo.bottomRatioToImg - labelHeight / img.height; } let labelLeft = img.width * labelInfo.leftRatioToImg + marginLeft; //leftRatioToImg:label的left占img的width的比例 let labelTop = img.height * labelInfo.topRatioToImg + marginTop; //topRatioToImg:label的top占img的height的比例 context.globalAlpha = labelInfo.opacity != void 0 ? labelInfo.opacity : 1; context.drawImage(labelImg, labelLeft, labelTop, labelWidth, labelHeight); if(labelInfo.outputCanvas){ return canvas } var dataUrl = canvas.toDataURL('image/png', labelInfo.compressRatio); //Common.downloadFile(dataUrl, 'screenshot.png') context.clearRect(0,0,canvas.width,canvas.height); return dataUrl }, changeShaderToWebgl2(vs, fs, matType, otherReplaces=[]){//部分shader要根据webgl版本作更改 if(!Potree.settings.isWebgl2)return {vs, fs} let turnTo300 = matType != 'ShaderMaterial' && (vs.includes('gl_FragDepthEXT') || fs.includes('gl_FragDepthEXT') ); let addV300 = turnTo300 && matType != 'RawShaderMaterial'; // RawShaderMaterial直接material.glslVersion = '300 es' 以加在define之前 let change = (shader, shaderType)=>{ let newShader = shader; if(turnTo300){ //非shaderMaterial需要手动改为300 es的写法 addV300 && (newShader = '#version 300 es \n' + newShader); //需要加 #version 300 es。 three.js自带的渲染会自动加所以不用 newShader = newShader.replaceAll('varying ', shaderType == 'vs' ? 'out ' : 'in '); newShader = newShader.replaceAll('attribute ', 'in '); if(shaderType == 'fs'){ newShader = newShader.replaceAll('gl_FragColor', 'fragColor'); newShader = newShader.replace('void main', 'out vec4 fragColor;\n void main' );//在void main前加入这个声明 } newShader = newShader.replaceAll('gl_FragDepthEXT','gl_FragDepth'); newShader = newShader.replaceAll('texture2D','texture'); newShader = newShader.replaceAll('textureCube','texture'); } newShader = newShader.replace('#extension GL_EXT_frag_depth : enable',''); newShader = newShader.replaceAll('defined(GL_EXT_frag_depth) &&',''); otherReplaces.forEach(({oldStr,newStr})=>{ newShader = newShader.replaceAll(oldStr,newStr); }); return newShader }; vs = change(vs,'vs'); fs = change(fs,'fs'); //console.log('成功替换为webgl2' ) return {vs,fs} }//three.js的shaderMaterial也有替换功能,搜 '#define gl_FragDepthEXT gl_FragDepth', }; Potree.Common = Common; class View{//base constructor () { this.position = new Vector3(0, 0, 0); this.yaw = Math.PI / 4; this._pitch = -Math.PI / 4; this.radius = 1; this.maxPitch = Math.PI / 2; this.minPitch = -Math.PI / 2; } clone () { let c = new View(); c.yaw = this.yaw; c._pitch = this.pitch; c.radius = this.radius; c.maxPitch = this.maxPitch; c.minPitch = this.minPitch; return c; } get pitch () { return this._pitch; } set pitch (angle) { this._pitch = Math.max(Math.min(angle, this.maxPitch), this.minPitch); } get direction () { let dir = new Vector3(0, 1, 0); dir.applyAxisAngle(new Vector3(1, 0, 0), this.pitch); dir.applyAxisAngle(new Vector3(0, 0, 1), this.yaw); return dir; } set direction (dir) { dir = dir.clone().normalize();//add if(dir.x === 0 && dir.y === 0){ this.pitch = Math.PI / 2 * Math.sign(dir.z); //this.yaw = 0 //add:还是要指定一下, 否则不统一 }else { let yaw = Math.atan2(dir.y, dir.x) - Math.PI / 2; let pitch = Math.atan2(dir.z, Math.sqrt(dir.x * dir.x + dir.y * dir.y)); this.yaw = yaw; this.pitch = pitch; } } lookAt(t){//setPivot let V; if(arguments.length === 1){ V = new Vector3().subVectors(t, this.position); }else if(arguments.length === 3){ V = new Vector3().subVectors(new Vector3(...arguments), this.position); } let radius = V.length(); let dir = V.normalize(); this.radius = radius; this.direction = dir; } getPivot () { return new Vector3().addVectors(this.position, this.direction.multiplyScalar(this.radius)); } getSide () { let side = new Vector3(1, 0, 0); side.applyAxisAngle(new Vector3(0, 0, 1), this.yaw); return side; } pan (x, y) { let dir = new Vector3(0, 1, 0); dir.applyAxisAngle(new Vector3(1, 0, 0), this.pitch); dir.applyAxisAngle(new Vector3(0, 0, 1), this.yaw); // let side = new THREE.Vector3(1, 0, 0); // side.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw); let side = this.getSide(); let up = side.clone().cross(dir); let pan = side.multiplyScalar(x).add(up.multiplyScalar(y)); this.position = this.position.add(pan); // this.target = this.target.add(pan); } translate (x, y, z) { let dir = new Vector3(0, 1, 0); dir.applyAxisAngle(new Vector3(1, 0, 0), this.pitch); dir.applyAxisAngle(new Vector3(0, 0, 1), this.yaw); let side = new Vector3(1, 0, 0); side.applyAxisAngle(new Vector3(0, 0, 1), this.yaw); let up = side.clone().cross(dir); let t = side.multiplyScalar(x) .add(dir.multiplyScalar(y)) .add(up.multiplyScalar(z)); this.position = this.position.add(t); } translateWorld (x, y, z) { this.position.x += x; this.position.y += y; this.position.z += z; } setView(position, target, duration = 0, callback = null){ let endPosition = null; if(position instanceof Array){ endPosition = new Vector3(...position); }else if(position.x != null){ endPosition = position.clone(); } let endTarget = null; if(target instanceof Array){ endTarget = new Vector3(...target); }else if(target.x != null){ endTarget = target.clone(); } const startPosition = this.position.clone(); const startTarget = this.getPivot(); //const endPosition = position.clone(); //const endTarget = target.clone(); let easing = TWEEN.Easing.Quartic.Out; if(duration === 0){ this.position.copy(endPosition); this.lookAt(endTarget); }else { let value = {x: 0}; let tween = new TWEEN.Tween(value).to({x: 1}, duration); tween.easing(easing); //this.tweens.push(tween); tween.onUpdate(() => { let t = value.x; //console.log(t); const pos = new Vector3( (1 - t) * startPosition.x + t * endPosition.x, (1 - t) * startPosition.y + t * endPosition.y, (1 - t) * startPosition.z + t * endPosition.z, ); const target = new Vector3( (1 - t) * startTarget.x + t * endTarget.x, (1 - t) * startTarget.y + t * endTarget.y, (1 - t) * startTarget.z + t * endTarget.z, ); this.position.copy(pos); this.lookAt(target); }); tween.start(); tween.onComplete(() => { if(callback){ callback(); } }); } } }; let sid = 0; class ExtendView extends View { constructor () { super(); this.yaw = 0; //Math.PI / 4; // = 4dkk lon + 90 this._pitch = 0; //-Math.PI / 4; //上下旋转 = 4dkk lat this.sid = sid++; this.LookTransition = 'LookTransition'+this.sid; this.FlyTransition = 'FlyTransition'+this.sid; } //add------ applyToCamera(camera){ camera.position.copy(this.position); camera.rotation.copy(this.rotation); camera.updateMatrix(); camera.updateMatrixWorld(); //camera.matrixWorldInverse.copy(camera.matrixWorld).invert(); } get rotation(){ var rotation = new Euler; rotation.order = "ZXY"; rotation.x = Math.PI / 2 + this.pitch; rotation.z = this.yaw; return rotation } set rotation(rotation){ if(rotation.y != 0){//因为 rotation的y不一定是0 , 所以不能直接逆着get rotation写。 //console.error('set rotation y不为0!!!!?', rotation ) //过渡时因为quaternion lerp所以不为0。没办法了orz this.direction = new Vector3(0,0,-1).applyEuler(rotation); //转回direction有损耗,在俯视时的(dir.x==dir.y==0), 丢失yaw信息从而 yaw无法获取(希望不要遇到这种情况,如果有的话,考虑先计算yaw,似乎好像可以算) }else {//尽量不用direction this.pitch = rotation.x - Math.PI / 2; this.yaw = rotation.z; } } get quaternion(){ return new Quaternion().setFromEuler(this.rotation) } set quaternion(q){ this.rotation = new Euler().setFromQuaternion(q); } copy(a){ Common.CopyClassObject(this, a, {ignoreList: ['_listeners']}); } clone () { return Common.CloneClassObject(this, {ignoreList: ['_listeners']}) } //---------- setCubeView(dir) { switch(dir) { case "front": this.yaw = 0; this.pitch = 0; break; case "back": this.yaw = Math.PI; this.pitch = 0; break; case "left": this.yaw = -Math.PI / 2; this.pitch = 0; break; case "right": this.yaw = Math.PI / 2; this.pitch = 0; break; case "top": this.yaw = 0; this.pitch = -Math.PI / 2; break; case "bottom": this.yaw = -Math.PI; this.pitch = Math.PI / 2; break; } } /* pan (x, y) { let dir = new THREE.Vector3(0, 1, 0); dir.applyAxisAngle(new THREE.Vector3(1, 0, 0), this.pitch); dir.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw); // let side = new THREE.Vector3(1, 0, 0); // side.applyAxisAngle(new THREE.Vector3(0, 0, 1), this.yaw); let side = this.getSide(); let up = side.clone().cross(dir); let pan = side.multiplyScalar(x).add(up.multiplyScalar(y)); this.position = this.position.add(pan); // this.target = this.target.add(pan); } */ pan (x, y) { //发现pan其实就是translate this.translate(x, 0, y); } translate (x, y, z, forceHorizon ) { //相机方向 let dir = new Vector3(0, 1, 0); dir.applyAxisAngle(new Vector3(1, 0, 0), forceHorizon ? 0 : this.pitch); //上下角度 dir.applyAxisAngle(new Vector3(0, 0, 1), this.yaw);//水平角度 let side = new Vector3(1, 0, 0); side.applyAxisAngle(new Vector3(0, 0, 1), this.yaw); //垂直于相机当前水平朝向的 左右方向 let up = side.clone().cross(dir); //垂直于相机当前水平朝向的 向上方向 let t = side.multiplyScalar(x) //x影响 左右分量 .add(dir.multiplyScalar(y)) //y影响 前后分量 .add(up.multiplyScalar(z)); //z影响 上下分量 this.position = this.position.add(t); if((!math.closeTo(x, 0, 1e-2) || !math.closeTo(y, 0, 1e-2) || !math.closeTo(z, 0, 1e-2)) && Potree.settings.displayMode != 'showPanos'){ this.cancelFlying('pos'); } this.restrictPos(); } translateWorld (x, y, z) { super.translateWorld(x, y, z); if((!math.closeTo(x, 0, 1e-2) || !math.closeTo(y, 0, 1e-2) || !math.closeTo(z, 0, 1e-2)) && Potree.settings.displayMode != 'showPanos'){ this.cancelFlying('pos'); } this.restrictPos(); } restrictPos(position){//add if(this.limitBound){ (position || this.position).clamp(this.limitBound.min, this.limitBound.max); } } isFlying(type='all'){ let a = transitions.getById(this.FlyTransition).length > 0; let b = transitions.getById(this.LookTransition).length > 0; return type == 'pos' ? a : type == 'rotate' ? b : (a || b) } cancelFlying(type='all', dealCancel=true){//外界只能通过这个来cancel type == 'pos' ? transitions.cancelById(this.FlyTransition, dealCancel ) : type == 'rotate' ? transitions.cancelById(this.LookTransition, dealCancel ) : (transitions.cancelById(this.FlyTransition, dealCancel ), transitions.cancelById(this.LookTransition, dealCancel )); //console.log('cancelFlying ' , this.sid, type) } setView( info = {}){ // position, target, duration = 0, callback = null, onUpdate = null, Easing='', cancelFun this.cancelFlying(); let posWaitDone, rotWaitDone , dir; let posDone = ()=>{ rotWaitDone || done(); posWaitDone = false; }; let rotDone = ()=>{ if(endTarget){ this.lookAt(endTarget); //compute radius for orbitcontrol }else if(endQuaternion){ this.rotation = new Euler().setFromQuaternion(endQuaternion); }else if(endYaw != void 0){ this.yaw = endYaw, this.pitch = endPitch; } //if(dir.x == 0 && dir.y == 0)this.yaw = 0 //统一一下 朝上的话是正的。朝下的一般不是0,会保留一个接近0的小数所以不用管 posWaitDone || done(); rotWaitDone = false; }; let done = ()=>{ //一定要旋转和位移都结束了才能执行 let f = ()=>{ this.position.copy(endPosition); //因为延时1后control的update会导致位置改变 info.callback && info.callback(); this.dispatchEvent('flyingDone'); }; if(info.duration){ setTimeout(f,1);//延迟是为了使isFlying先为false }else { f(); //有的需要迅速执行回调 } }; let endPosition = new Vector3().copy(info.position); let startPosition = this.position.clone(); let startQuaternion, endQuaternion, endTarget = info.target && new Vector3().copy(info.target) , endYaw, startYaw, endPitch, startPitch ; this.restrictPos(endPosition); if(info.endYaw == void 0){ if(info.target ){ endQuaternion = math.getQuaFromPosAim(endPosition,endTarget); //若为垂直,会自动偏向x负的方向 }else if(info.quaternion){ endQuaternion = info.quaternion.clone(); } if(endQuaternion && math.closeTo(Math.abs(this.direction.z), 1, 1e-4)){ //在垂直的视角下的quaternion刚开始突变的厉害,这时候可能渐变yaw比较好(如俯视时点击测量线) let a = this.clone(); a.quaternion = endQuaternion; info.endYaw = a.yaw; info.endPitch = a.pitch; //console.log('turn to yaw') } } if(info.endYaw != void 0) { startPitch = this.pitch; endPitch = info.endPitch; startYaw = this.yaw; endYaw = info.endYaw; if(Math.abs(startYaw - endYaw)>Math.PI){//如果差距大于半个圆,就要反个方向转(把大的那个数字减去360度) startYaw > endYaw ? (startYaw -= Math.PI*2) : (endYaw -= Math.PI*2); } //console.log('startYaw', startYaw, 'endYaw', endYaw) } if(endQuaternion){ startQuaternion = this.quaternion; } if(!info.duration){ this.position.copy(endPosition); this.restrictPos(); posWaitDone = true, rotWaitDone = true; info.onUpdate && info.onUpdate(1); posDone(); rotDone(); }else { info.onUpdate && info.onUpdate(0); //初始化progress let posChange = !this.position.equals(endPosition); if(posChange){ posWaitDone = true; transitions.start(lerp.vector(this.position, endPosition, (pos, progress, delta)=>{ info.onUpdate && info.onUpdate(progress, delta); }), info.duration, posDone , 0, info.Easing ? easing[info.Easing] : easing.easeInOutSine ,null, this.FlyTransition, ()=>{ //中途取消 if(endTarget ){ /* endPosition = new THREE.Vector3().copy(this.position)//更改旋转的endQuaternion endQuaternion = math.getQuaFromPosAim(endPosition,endTarget) */ //直接改变endQuaternion会突变,所以还是cancel吧 this.cancelFlying('rotate'); } posWaitDone = false; info.cancelFun && info.cancelFun(); }, info.ignoreFirstFrame); } if(endQuaternion || endYaw != void 0){ rotWaitDone = true; transitions.start( (progress, delta )=>{ if(endYaw != void 0){ this.yaw = startYaw * (1-progress) + endYaw * progress; this.pitch = startPitch * (1-progress) + endPitch * progress; }else { let quaternion = (new Quaternion()).copy(startQuaternion); lerp.quaternion(quaternion, endQuaternion)(progress); //在垂直的视角下的角度突变的厉害,这时候可能渐变yaw比较好 this.quaternion = quaternion; //console.log(quaternion,this.yaw) } posChange || info.onUpdate && info.onUpdate(progress, delta); }, info.duration, rotDone , 0, info.Easing ? easing[info.Easing] : easing.easeInOutSine ,null, this.LookTransition, ()=>{ //中途取消 rotWaitDone = false; info.cancelFun && info.cancelFun(); }, info.ignoreFirstFrame); } /* transitions.start(lerp.vector(this.position, endPosition, (pos, progress)=>{ let t = progress if(endQuaternion){ let quaternion = (new THREE.Quaternion()).copy(startQuaternion) lerp.quaternion(quaternion, endQuaternion)(progress), this.rotation = new THREE.Euler().setFromQuaternion(quaternion) } this.restrictPos() //console.log('setView flying') info.onUpdate && info.onUpdate(t)//add }), info.duration, done, 0, info.Easing ? easing[info.Easing] : easing.easeInOutSine ,null, this.LookTransition, info.cancelFun); //easeInOutQuad */ } } //平移Ortho相机 moveOrthoCamera(viewport, info, duration, easeName){//boundSize优先于endZoom。 let camera = info.camera || viewport.camera; let startZoom = camera.zoom; let endPosition = info.endPosition; let boundSize = info.boundSize; let endZoom = info.endZoom; let margin = info.margin || {x:0,y:0};/* 200 */ //像素 let onUpdate = info.onUpdate; if(info.bound){//需要修改boundSize以适应相机的旋转,当相机不在xy水平面上朝向z时 endPosition = endPosition || info.bound.getCenter(new Vector3()); let matrixRot = new Matrix4().makeRotationFromEuler(this.rotation).invert(); let boundingBox = info.bound.clone().applyMatrix4(matrixRot); boundSize = boundingBox.getSize(new Vector3()); } if(boundSize && boundSize.x == 0 && boundSize.y == 0){ boundSize.set(1,1); //避免infinity } this.setView( Object.assign(info, { position:endPosition, duration, onUpdate:(progress, delta)=>{ if(boundSize || endZoom){ if(boundSize){ let aspect = boundSize.x / boundSize.y; let w, h; if(camera.aspect > aspect){//视野更宽则用bound的纵向来决定 h = boundSize.y; endZoom = (viewport.resolution.y - margin.y) / h; //注意,要在resolution不为0时执行 }else { w = boundSize.x; endZoom = (viewport.resolution.x - margin.x) / w; } //onUpdate时更新endzoom是因为画布大小可能更改 } this.zoom = camera.zoom = endZoom * progress + startZoom * (1 - progress); //view里也加一下,有些地方需要记录,如截图 camera.updateProjectionMatrix(); onUpdate && onUpdate(progress, delta); } }, Easing:easeName })); } zoomOrthoCamera(camera, endZoom, pointer, duration, onProgress){//定点缩放 let startZoom = camera.zoom; let pointerPos = new Vector3(pointer.x, pointer.y,0.5); transitions.start(( progress)=>{ let oldPos = pointerPos.clone().unproject(camera); this.zoom = camera.zoom = endZoom * progress + startZoom * (1 - progress); camera.updateProjectionMatrix(); let newPos = pointerPos.clone().unproject(camera); //定点缩放, 恢复一下鼠标所在位置的位置改变量 let moveVec = new Vector3().subVectors(newPos, oldPos); camera.position.sub(moveVec); this.position.copy(camera.position); onProgress && onProgress(); } , duration, null/* done */, 0, easing.easeInOutSine, null, "zoomInView"/* , info.cancelFun */); } tranCamera(viewport, info, duration, easeName){ viewport.camera = info.midCamera; //viewport.camera.matrixWorld = info.endCamera.matrixWorld //viewer.setCameraMode(CameraMode.ORTHOGRAPHIC) info.midCamera.projectionMatrix.copy(info.startCamera.projectionMatrix); let onUpdate = info.onUpdate; info.onUpdate = (progress, delta)=>{ lerp.matrix4(info.midCamera.projectionMatrix, info.endCamera.projectionMatrix)(progress); onUpdate && onUpdate(progress, delta); }; let callback = info.callback; info.callback = ()=>{ viewport.camera = info.endCamera; viewer.scene.measurements.forEach((e)=>{ Potree.Utils.updateVisible(e, 'tranCamera', true); }); this.applyToCamera(viewport.camera); viewer.dispatchEvent({type:'camera_changed', viewport:viewer.mainViewport, changeInfo:{}});//update sprite callback && callback(); }; //info.forbitCancel = true info.camera = info.endCamera; if(info.camera.type == "OrthographicCamera"){ this.moveOrthoCamera(viewport, info, duration, easeName); }else { this.setView( Object.assign(info, { duration}) ); } } }; var math = { getBaseLog(x, y) {//返回以 x 为底 y 的对数(即 logx y) . Math.log 返回一个数的自然对数 return Math.log(y) / Math.log(x); } , convertVector : { ZupToYup: function(e){//navvis -> 4dkk return new Vector3(e.x,e.z,-e.y) }, YupToZup: function(e){//4dkk -> navvis return new Vector3(e.x,-e.z,e.y) }, }, convertQuaternion: { ZupToYup: function(e){//navvis -> 4dkk //不同于convertVisionQuaternion let rotation = new Euler(-Math.PI/2,0,0); let quaternion = new Quaternion().setFromEuler(rotation); return e.clone().premultiply(quaternion) //return new THREE.Quaternion(e.x,e.z,-e.y,e.w).multiply((new THREE.Quaternion).setFromAxisAngle(new THREE.Vector3(1,0,0), THREE.Math.degToRad(90))) }, YupToZup: function(e){//4dkk -> navvis let rotation = new Euler(Math.PI/2,0,0); let quaternion = new Quaternion().setFromEuler(rotation); return e.clone().premultiply(quaternion) }, }, convertVisionQuaternion: function(e) { return new Quaternion(e.x,e.z,-e.y,e.w).multiply((new Quaternion).setFromAxisAngle(new Vector3(0,1,0), MathUtils.degToRad(90))) }, invertVisionQuaternion : function(e) {//反转给算法部 var a = e.clone().multiply((new Quaternion).setFromAxisAngle(new Vector3(0,1,0), MathUtils.degToRad(-90))); return new Quaternion(a.x,-a.z,a.y,a.w) }, //------------ getVec2Angle : function(dir1,dir2){ return Math.acos( MathUtils.clamp(this.getVec2Cos(dir1,dir2), -1,1) ) }, getVec2Cos : function(dir1,dir2){ return dir1.dot(dir2) / dir1.length() / dir2.length() }, getAngle:function(vec1, vec2, axis='z'){//带方向的角度 vector3 if(!vec1.isVector3){ vec1 = new Vector3(vec1.x, vec1.y, 0); vec2 = new Vector3(vec2.x, vec2.y, 0); } var angle = vec1.angleTo(vec2); var axis_ = vec1.clone().cross(vec2); if(typeof axis == 'string'){ if(axis_[axis] < 0){ angle *= -1; } }else {//vector3 if(axis_.dot(axis)< 0){ angle *= -1; } } return angle }, closeTo : function(a,b, precision=1e-6){ let f = (a,b)=>{ return Math.abs(a-b) < precision; }; if(typeof (a) == 'number'){ return f(a, b); }else { let judge = (name)=>{ if(a[name] == void 0)return true //有值就判断,没值就不判断 else return f(a[name],b[name]) }; return judge('x') && judge('y') && judge('z') && judge('w') } }, toPrecision: function (e, t) {//xzw change 保留小数 var f = function (e, t) { var i = Math.pow(10, t); return Math.round(e * i) / i }; if (e instanceof Array) { for (var s = 0; s < e.length; s++) { e[s] = f(e[s], t); } return e; } else if (e instanceof Object) { for (var s in e) { e[s] = f(e[s], t); } return e; } else if(typeof e == 'number'){ return f(e, t) }else { return e } }, isEmptyQuaternion: function(e) { return 0 === Math.abs(e.x) && 0 === Math.abs(e.y) && 0 === Math.abs(e.z) && 0 === Math.abs(e.w) }, projectPositionToCanvas: function(e, t, i) { i = i || new Vector3, i.copy(e); var r = .5 * $('#player').width() , o = .5 * $('#player').height(); return i.project(t), i.x = i.x * r + r, i.y = -(i.y * o) + o, i }, handelPadResize:false, /* handelPadding : function () { //去除player左边和上面的宽高,因为pc的player左上有其他element 许钟文 var pads = [];//记录下来避免反复计算 var index = []; var resetPad = function(){ pads = []; index = []; math.handelPadResize = false; //switchview时resized为true } if(config.isEdit && !config.isMobile){ window.addEventListener('resize',resetPad); } return function(x, y, domE){ if(!config.isEdit || config.isMobile) { return { x: x, y: y } } if(this.handelPadResize)resetPad(); domE = domE || $('#player')[0]; var pad; var i = index.indexOf(domE); if (i == -1){ index.push(domE); pad = { x: this.getOffset("left", domE), y: this.getOffset("top", domE) } pads.push(pad) } else pad = pads[i]; return { x: x - pad.x, y: y - pad.y } } }(), */ getOffset: function (type, element, parent) {//获取元素的边距 许钟文 var offset = (type == "left") ? element.offsetLeft : element.offsetTop; if (!parent) parent = $("body")[0]; while (element = element.offsetParent) { if (element == parent) break; offset += (type == "left") ? element.offsetLeft : element.offsetTop; } return offset; } , constrainedTurn: function(e) { var t = e % (2 * Math.PI); return t = t > Math.PI ? t -= 2 * Math.PI : t < -Math.PI ? t += 2 * Math.PI : t }, getFOVDotThreshold: function(e) { return Math.cos(MathUtils.degToRad(e / 2)) }, transform2DForwardVectorByCubeFace: function(e, t, i, n) { switch (e) { case GLCubeFaces.GL_TEXTURE_CUBE_MAP_POSITIVE_X: i.set(1, t.y, t.x); break; case GLCubeFaces.GL_TEXTURE_CUBE_MAP_NEGATIVE_X: i.set(-1, t.y, -t.x); break; case GLCubeFaces.GL_TEXTURE_CUBE_MAP_POSITIVE_Y: i.set(-t.x, 1, -t.y); break; case GLCubeFaces.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: i.set(-t.x, -1, t.y); break; case GLCubeFaces.GL_TEXTURE_CUBE_MAP_POSITIVE_Z: i.set(-t.x, t.y, 1); break; case GLCubeFaces.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: i.set(t.x, t.y, -1); } n && i.normalize(); }, getFootPoint : function(oldPos, p1, p2, restricInline){ //找oldPos在线段p1, p2上的垂足 /* if(isWorld){//输出全局坐标 需要考虑meshGroup.position p1 = p1.clone(); p2 = p2.clone(); p1.y += mainDesign.meshGroup.position.y; p2.y += mainDesign.meshGroup.position.y; } */ if(p1.equals(p2))return p1.clone() var op1 = oldPos.clone().sub(p1); var p1p2 = p1.clone().sub(p2); var p1p2Len = p1p2.length(); var leftLen = op1.dot(p1p2) / p1p2Len; var pos = p1.clone().add(p1p2.multiplyScalar( leftLen/p1p2Len )); if(restricInline && pos.clone().sub(p1).dot( pos.clone().sub(p2) ) > 0){//foot不在线段上 if(pos.distanceTo(p1) < pos.distanceTo(p2)) pos = p1.clone(); else pos = p2.clone(); } return pos; }, /** * 计算多边形的重心 * @param {*} points */ getCenterOfGravityPoint : function(mPoints){ var area = 0.0;//多边形面积 var Gx = 0.0, Gy = 0.0;// 重心的x、y for (var i = 1; i <= mPoints.length; i++) { var ix = mPoints[i % mPoints.length].x; var iy = mPoints[i % mPoints.length].y; var nx = mPoints[i - 1].x; var ny = mPoints[i - 1].y; var temp = (ix * ny - iy * nx) / 2.0; area += temp; Gx += temp * (ix + nx) / 3.0; Gy += temp * (iy + ny) / 3.0; } Gx = Gx / area; Gy = Gy / area; return { x: Gx, y: Gy }; }, getBound : function(ring){ var bound = new Box2(); for(var j=0,len = ring.length; j bound.max.x || point.y < bound.min.y || point.y > bound.max.y)return false; var inside = false; var x = point.x, y = point.y; for (var i = 0, j = ring.length - 1; i < ring.length; j = i++) { var xi = ring[i].x, yi = ring[i].y; var xj = ring[j].x, yj = ring[j].y; if((xi - x)*(yj - y) == (xi - x)*(yi - y) && x>=Math.min(xi,xj) && x<=Math.max(xi,xj)//xzw add && y>=Math.min(yi,yj) && y<=Math.max(yi,yj) ){ //return !!ifAtLine;//在线段上,则判断为…… (默认在外) return {atLine:true} } if (((yi > y) != (yj > y)) && (x < (xj - xi) * (y - yi) / (yj - yi) + xi) ) { inside = !inside; } } if(inside && holes){ return !holes.some(ring=>this.isPointInArea(ring, null, point, ifAtLine) ) //不能存在于任何一个二级内环内 }else { return inside; } }, getArea : function (ring) { //求面积 顺时针为正 来自three shape for (var t = ring.length, i = 0, n = t - 1, r = 0; r < t; n = r++) i += ring[n].x * ring[r].y - ring[r].x * ring[n].y; return -.5 * i }, getVolume:function(faceArr){//求三角多面体的体积。和求面积同理都用鞋带计算法 https://blog.csdn.net/weixin_43414513/article/details/123758897 //问题:怎么确定方向 //每个三角形的顺序必须是右手螺旋法则指向物体外(外向里看为逆时针),分别为ABC,则每个三角形和原点O构成的三棱锥体积为 (OA cross OB) dot(OC) / 6 . 结果一般朝向原点的为负,反之为正 }, isInBetween : function(a, b, c, precision) { // 如果b几乎等于a或c,返回false.为了避免浮点运行时两值几乎相等,但存在相差0.00000...0001的这种情况出现使用下面方式进行避免 /* if (Math.abs(a - b) < 0.000001 || Math.abs(b - c) < 0.000001) { return false; } return (a <= b && b <= c) || (c <= b && b <= a);*/ //更改:如果b和a或c中一个接近 就算在a和c之间 return (a <= b && b <= c) || (c <= b && b <= a) || this.closeTo(a,b,precision) || this.closeTo(b,c,precision); }, ifPointAtLineBound:function(point, linePoints, precision){ //待验证 横线和竖线比较特殊 return math.isInBetween(linePoints[0].x, point.x, linePoints[1].x, precision) && math.isInBetween(linePoints[0].y, point.y, linePoints[1].y, precision) } , isLineIntersect: function (line1, line2, notSegment, precision) {//线段和线段是否有交点. notSegment代表是直线而不是线段 var a1 = line1[1].y - line1[0].y; var b1 = line1[0].x - line1[1].x; var c1 = a1 * line1[0].x + b1 * line1[0].y; //转换成一般式: Ax+By = C var a2 = line2[1].y - line2[0].y; var b2 = line2[0].x - line2[1].x; var c2 = a2 * line2[0].x + b2 * line2[0].y; // 计算交点 var d = a1 * b2 - a2 * b1; // 当d==0时,两线平行 if (d == 0) { return false; } else { var x = (b2 * c1 - b1 * c2) / d; var y = (a1 * c2 - a2 * c1) / d; // 检测交点是否在两条线段上 /* if (notSegment || (isInBetween(line1[0].x, x, line1[1].x) || isInBetween(line1[0].y, y, line1[1].y)) && (isInBetween(line2[0].x, x, line2[1].x) || isInBetween(line2[0].y, y, line2[1].y))) { return {x,y}; } */ if (notSegment || math.ifPointAtLineBound({x,y}, line1, precision) && math.ifPointAtLineBound({x,y}, line2, precision)){ return {x,y}; } } }, getNormal2d : function(o={} ){//获取二维法向量 方向向内 var x,y, x1,y1; //line2d的向量 if(o.vec){ x1 = o.vec.x; y1 = o.vec.y; }else { x1 = o.p1.x - o.p2.x; y1 = o.p1.y - o.p2.y; } //假设法向量的x或y固定为1或-1 if(y1 != 0){ x = 1; y = - (x1 * x) / y1; }else if(x1 != 0){//y如果为0,正常情况x不会是0 y = 1; x = - (y1 * y) / x1; }else { console.log("两个点一样"); return null; } //判断方向里或者外: var vNormal = new Vector3(x, 0, y); var vLine = new Vector3(x1, 0, y1); var vDir = vNormal.cross(vLine); if(vDir.y>0){ x *= -1; y *= -1; } return new Vector2(x, y).normalize(); }, getQuaBetween2Vector:function(oriVec, newVec, upVec){ //获取从oriVec旋转到newVec可以应用的quaternion var angle = oriVec.angleTo(newVec); var axis = oriVec.clone().cross( newVec).normalize();//两个up之间 if(axis.length() == 0){//当夹角为180 或 0 度时,得到的axis为(0,0,0),故使用备用的指定upVec return new Quaternion().setFromAxisAngle( upVec, angle ); } return new Quaternion().setFromAxisAngle( axis, angle ); } , /* getQuaBetween2Vector2 : function(oriVec, newVec ){//not camera var _ = (new THREE.Matrix4).lookAt( oriVec, new THREE.Vector3, new THREE.Vector3(0,1,0)) var aimQua = (new THREE.Quaternion).setFromRotationMatrix(_) var _2 = (new THREE.Matrix4).lookAt( newVec, new THREE.Vector3, new THREE.Vector3(0,1,0)) var aimQua2 = (new THREE.Quaternion).setFromRotationMatrix(_2) return aimQua2.multiply(aimQua.clone().inverse()) } */ getQuaByAim: function (aim, center=new Vector3) { let forward = new Vector3(0, 1, 0); let qua1 = new Quaternion().setFromUnitVectors(forward, aim.clone().sub(center).normalize()); /* var _ = (new THREE.Matrix4).lookAt(center,aim, new THREE.Vector3(0,1,0)); let qua2 = (new THREE.Quaternion).setFromRotationMatrix(_); let rot1 = new THREE.Euler().setFromQuaternion(qua1) let rot2 = new THREE.Euler().setFromQuaternion(qua2) //奇怪,qua2怎么都不对 console.log(rot1,rot2) */ return qua1 }, getAimByQua: function (quaternion, center=new Vector3) { return new Vector3(0, 0, -1).applyQuaternion(quaternion).add(center) }, getScaleForConstantSize : function(){ //获得规定二维大小的mesh的scale值。可以避免因camera的projection造成的mesh视觉大小改变。 来源:tag.updateDisc var w; var i = new Vector3, o = new Vector3, l = new Vector3, c = new Vector3, h = new Vector3; return function(op={}){ if(op.width2d) w = op.width2d; //如果恒定二维宽度 else {//否则考虑上距离,加一丢丢近大远小的效果 var currentDis, nearBound, farBound; if(op.camera.type == "OrthographicCamera"){ currentDis = 200 / op.camera.zoom; //(op.camera.right - op.camera.left) / op.camera.zoom }else { currentDis = op.position.distanceTo(op.camera.position); } w = op.maxSize - ( op.maxSize - op.minSize) * MathUtils.smoothstep(currentDis, op.nearBound, op.farBound); //maxSize : mesh要表现的最大像素宽度; nearBound: 最近距离,若比nearBound近,则使用maxSize } i.copy(op.position).project(op.camera); //tag中心在屏幕上的二维坐标 o.set(op.resolution.x / 2, op.resolution.y / 2, 1).multiply(i); //转化成px -w/2 到 w/2的范围 l.set(w / 2, 0, 0).add(o); //加上tag宽度的一半 c.set(2 / op.resolution.x, 2 / op.resolution.y, 1).multiply(l); //再转回 -1 到 1的范围 h.copy(c).unproject(op.camera);//再转成三维坐标,求得tag边缘的位置 var g = h.distanceTo(op.position);//就能得到tag的三维半径 //这里使用的都是resolution2, 好处是手机端不会太小, 坏处是pc更改网页显示百分比时显示的大小会变(或许可以自己算出设备真实的deviceRatio, 因window.screen是不会改变的),但考虑到用户可以自行调节字大小也许是好的 return g //可能NAN 当相机和position重叠时 } }() , //W , H, left, top分别是rect的宽、高、左、上 getCrossPointAtRect : function(p1, aim, W , H, left, top){//求射线p1-aim在rect边界上的交点,其中aim在rect范围内,p1则不一定(交点在aim这边的延长线上) var x,y, borderX; var r = (aim.x - p1.x) / (aim.y - p1.y);//根据相似三角形原理先求出这个比值 var getX = function(y){ return r * (y-p1.y) + p1.x; }; var getY = function(x){ return 1/r * (x-p1.x) + p1.y; }; if(aim.x >= p1.x){ borderX = W+left; }else { borderX = left; } x = borderX; y = getY(x); if(y < top || y > top+H){ if(y < top){ y = top; }else { y = top+H; } x = getX(y); } return new Vector2(x, y); }, getDirFromUV : function(uv){ //获取dir 反向计算 - - 二维转三维比较麻烦 var dirB; //所求 单位向量 var y = Math.cos(uv.y * Math.PI); //uv中纵向可以直接确定y, 根据上面getUVfromDir的反向计算 // 故 uv.y * Math.PI 就是到垂直线(向上)的夹角 var angle = 2 * Math.PI * uv.x - Math.PI; //x/z代表的是角度 var axisX, axisZ; //axis为1代表是正,-1是负数 if (-Math.PI <= angle && angle < 0) { axisX = -1; //下半圆 } else { axisX = 1; //上半圆 } if (-Math.PI / 2 <= angle && angle < Math.PI / 2) { axisZ = 1; //右半圆 } else { axisZ = -1; //左半圆 } var XDivideZ = Math.tan(angle); var z = Math.sqrt((1 - y * y) / (1 + XDivideZ * XDivideZ)); var x = XDivideZ * z; if (z * axisZ < 0) { //异号 z *= -1; x *= -1; if (x * axisX < 0) { // console.log("wrong!!!!!??????????") } } x *= -1; //计算完成后这里不能漏掉 *= -1 dirB = this.convertVector.YupToZup(new Vector3(x, y, z)); //理想状态下x和z和anotherDir相同 return dirB }, getUVfromDir : function(dir) { //获取UV 同shader里的计算 var dir = this.convertVector.ZupToYup(dir); dir.x *= -1; //计算前这里不能漏掉 *= -1 见shader var tx = Math.atan2(dir.x, dir.z) / (Math.PI * 2.0) + 0.5; //atan2(y,x) 返回从 X 轴正向逆时针旋转到点 (x,y) 时经过的角度。区间是-PI 到 PI 之间的值 var ty = Math.acos(dir.y) / Math.PI; return new Vector2(tx, ty) //理想状态下tx相同 }, getDirByLonLat : function(lon,lat){ var dir = new Vector3; var phi = MathUtils.degToRad(90 - lat); var theta = MathUtils.degToRad(lon); dir.x = Math.sin(phi) * Math.cos(theta); dir.y = Math.cos(phi); dir.z = Math.sin(phi) * Math.sin(theta); return dir } //0,0 => (1,0,0) 270=>(0,0,-1) , projectPointAtPlane:function(o={}){//获取一个点在一个面上的投影 {facePoints:[a,b,c], point:} var plane = new Plane().setFromCoplanarPoints(...o.facePoints); return plane.projectPoint(o.point, new Vector3() ) } , getPolygonsMixedRings:function( polygons, onlyGetOutRing){//{points:[vector2,...],holes:[[],[]]} let points = []; let lines = []; let i = 0; polygons.forEach(e=> points.push(...e.map(a=>new Vector2().copy(a) )) ); polygons.forEach((ps,j)=>{ let length = ps.length; let index = 0; while(index{p.id = j;}); let rings = searchRings({ points, lines, onlyGetOutRing }); //console.log(rings) rings = rings.filter(e=>e.closetParent == void 0);// 子环不加,被外环包含了 return rings }, getQuaFromPosAim( position, target ){ /* let matrix = (new THREE.Matrix4).lookAt(position, target, new THREE.Vector3(0,0,1)) //这里垂直的话会默认给一个右向所以不这么写 return (new THREE.Quaternion).setFromRotationMatrix(matrix) */ let view = new ExtendView(); view.direction = new Vector3().subVectors(target,position); return view.quaternion }, getBoundByPoints(points, minSize){ var bound = new Box3; points.forEach(point=>{ bound.expandByPoint(point); }); let center = bound.getCenter(new Vector3); if(minSize){ let minBound = (new Box3()).setFromCenterAndSize(center, minSize); bound.union(minBound); } return { bounding:bound, size: bound.getSize(new Vector3), center, } }, /* linearClamp(value, x1,x2, y1, y2){//x为bound.min, bound.max value = THREE.Math.clamp(value, x1,x2) return y1 + ( y2 - y1) * (value - x1) / (x2 - x1) } */ linearClamp(value, xArr , yArr){ //xArr需要按顺序从小到大,yArr对应xArr中的值 let len = xArr.length; if(value <= xArr[0]) return yArr[0] if(value >= xArr[len - 1]) return yArr[len - 1] let i = 0; while(++i < len ){ if(value < xArr[i]){ let x1 = xArr[i-1], x2 = xArr[i], y1 = yArr[i-1], y2 = yArr[i]; value = y1 + ( y2 - y1) * (value - x1) / (x2 - x1); break } } return value } }; /* 如何将若干个点拟合出线段 // 假设你有一个点数组,每个点表示为一个包含x和y坐标的对象 const points = [ { x: 1, y: 2 }, { x: 2, y: 3 }, { x: 3, y: 4 }, { x: 4, y: 5 }, // ... 更多点 ]; // 定义用于计算最小二乘法参数的函数 function leastSquares( ) { let sumX = 0; let sumY = 0; let sumXY = 0; let sumX2 = 0; let n = points.length; for (const point of points) { sumX += point.x; sumY += point.y; sumXY += point.x * point.y; sumX2 += point.x * point.x; } const k = (n * sumXY - sumX * sumY) / (n * sumX2 - sumX * sumX); //不知道为何这样算,自己解不出来(使点到直线距离之和平方最小),但测了下似乎是准的 const b = (sumY - k * sumX) / n; return { k, b }; } // 使用上述函数计算拟合直线的参数 const { k, b } = leastSquares(points); // 现在你可以使用这些参数来表示拟合出的线段 console.log(`拟合线段的方程为: y = ${k}x + ${b}`); */ Potree.math = math; function mobileVersion(e, t) { //ios的版本 var i = window.navigator.userAgent, n = i.match(e); return ( (n = n ? n[1].split(t) : []), { major: parseInt(n[0]) || 0, minor: parseInt(n[1]) || 0, patch: parseInt(n[2]) || 0, } ) } var browser = { isFullscreen: function() { return ( document.fullscreenElement || document.mozFullscreenElement || document.mozFullScreenElement || document.webkitFullscreenElement || document.msFullscreenElement ) }, supportsFullscreen: function() { return ( document.fullscreenEnabled || document.mozFullscreenEnabled || document.mozFullScreenEnabled || document.webkitFullscreenEnabled || document.msFullscreenEnabled ) }, isPointerLocked: function() { return ( document.pointerLockElement || document.mozPointerLockElement || document.webkitPointerLockElement ) }, requestFullscreen: function(dom, t) { dom.requestFullscreen ? dom.requestFullscreen() : dom.mozRequestFullScreen ? dom.mozRequestFullScreen() : dom.webkitRequestFullscreen ? dom.webkitRequestFullscreen(Element.ALLOW_KEYBOARD_INPUT) : dom.msRequestFullscreen && dom.msRequestFullscreen(), t && $(document).on( "fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange", browser.requestPointerLock ); }, requestPointerLock: function() { var e; if (document.fullscreenElement) e = document.fullscreenElement(); else if (document.mozFullscreenElement) e = document.mozFullscreenElement(); else if (document.mozFullScreenElement) e = document.mozFullScreenElement(); else { if (!document.webkitFullscreenElement) return e = document.webkitFullscreenElement(); }; (e.requestPointerLock = e.requestPointerLock || e.mozRequestPointerLock || e.webkitRequestPointerLock), e.requestPointerLock(), $(document).off( "fullscreenchange webkitfullscreenchange mozfullscreenchange MSFullscreenChange", this ); }, exitPointerLock: function() { ; (document.exitPointerLock = document.exitPointerLock || document.mozExitPointerLock || document.webkitExitPointerLock), document.exitPointerLock(); }, exitFullscreen: function() { document.exitFullscreen ? document.exitFullscreen() : document.msExitFullscreen ? document.msExitFullscreen() : document.mozCancelFullScreen ? document.mozCancelFullScreen() : document.webkitExitFullscreen && document.webkitExitFullscreen(); }, details: function() { var e = navigator.userAgent.match("(Firefox|Chrome|Safari)/([\\d]+)"); return e ? { name: e[1], version: parseInt(e[2]), platform: navigator.platform, } : {} }, is: function(e) { return this.details() && this.details().name === e }, inIframe: function() { return window.parent !== window }, aspectRatio: function($elem) { $elem = $elem || $("#player"); var e = $elem.width() / $elem.height(); return isFinite(e) ? e : 0 }, userAgent: function() { return window.navigator.userAgent }, isMobile: function() { var e = navigator.userAgent || navigator.vendor || window.opera; return ( /(android|bb\d+|meego).+mobile|android|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od|ad)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test( e ) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test( e.substr(0, 4) ) ) }, isLandscape: function() { return this.isMobile && this.aspectRatio() > 1 }, isSmallScreen: function() { var e = screen.width / window.devicePixelRatio; return e < 240 }, detectIE: function() { var e = window.navigator.userAgent, t = e.indexOf("MSIE "); return t !== -1 || !!navigator.userAgent.match(/Trident.*rv\:11\./) }, detectSafari: function() { var e = window.navigator.userAgent, t = e.indexOf("Safari"); return t !== -1 && !this.detectOpera() && !this.detectChrome() //xzw add detectOpera }, detectFirefox: function() { var e = window.navigator.userAgent; return e.indexOf("Firefox") !== -1 }, detectChrome: function() { var e = window.navigator.userAgent; return e.indexOf("Chrome") !== -1 && !this.detectOpera() }, detectOpera: function() { var e = window.navigator.userAgent; return e.indexOf("OPR") !== -1 }, detectIOS: function() { return this.detectIPhone() || this.detectIPad() || this.detectIPod() }, detectIPad: function() { var e = window.navigator.userAgent, t = /iPad/; return t.test(e) }, detectIPod: function() { var e = window.navigator.userAgent, t = /iPod/; return t.test(e) }, detectIPhone: function() { var e = window.navigator.userAgent, t = /iPhone/; return t.test(e) }, detectAndroid: function() { var e = window.navigator.userAgent; return e.indexOf("Android") !== -1 }, detectAndroidMobile: function() { var e = window.navigator.userAgent; return this.detectAndroid() && e.indexOf("Mobile") !== -1 }, detectSamsungNative: function() { var e = window.navigator.userAgent; return ( e.indexOf("SM-G900H") !== -1 || e.indexOf("GT-I9500") !== -1 || e.indexOf("SM-N900") !== -1 ) }, detectSamsungS6: function() { var e = window.navigator.userAgent; return e.indexOf("SM-G92") !== -1 }, /************************************************************徐世廷*************************************************************/ detectHUAWEI5X: function() { return -1 !== window.navigator.userAgent.indexOf("KIW-TL00H") }, /*******************************************************************************************************************************/ detectWebVR: function() { return !(!window.navigator.getVRDisplays || !window.VRDisplay) }, getVRDisplay: function() { var e = $.Deferred(); return this.detectWebVR() ? (navigator.getVRDisplays().then(function(t) { t.length >= 1 && e.resolve(t[0]), e.reject(null); }), e) : e.reject(null) }, iosVersion: function() { if (!this.detectIOS()) throw new DeviceMismatchException("Did not detect an iDevice") var e = /((?:\d+\_?){1,3}) like Mac OS/, t = "_"; return mobileVersion(e, t) }, androidVersion: function() { if (!this.detectAndroid()) throw new DeviceMismatchException("Did not detect an Android based device") var e = /Android ((?:\d+\.?){1,3})/, t = "."; return mobileVersion(e, t) }, valueFromCookie: function(e, t) { var i = new RegExp(e + "=([0-9a-f]+)(; ?|$)").exec(document.cookie); if (!i) return t var n = i[1]; return "boolean" == typeof t ? "true" === n || "1" === n : "number" == typeof t ? parseFloat(n) : n }, valueFromHash: function(e, t) { var i = new RegExp("[#&?]" + e + "=([^#&?]*)"), n = i.exec(window.location.href); if (!n) return t var r = n[1]; return "boolean" == typeof t ? "true" === r || "1" === r : "number" == typeof t ? parseFloat(r) : window.decodeURIComponent(r) }, //-------许钟文:------------------------------------------------- getProjectNum: function() { //获取场景projectNum if (window.__ProjectNum && window.__ProjectNum != "__ProjectNum__") { return window.__ProjectNum } var number = window.location.href.substring(window.location.href.indexOf("=") + 1); if (number.indexOf("&") != -1) { number = number.substring(0, number.indexOf("&")); } if (number.indexOf("#") != -1) { number = number.substring(0, number.indexOf("#")); } return number }, urlHasValue: function(key, isGetValue) { let querys = window.location.search.substr(1).split("&"); if (isGetValue) { for (let i = 0; i < querys.length; i++) { let keypair = querys[i].split("="); if (keypair.length === 2 && keypair[0] === key) { return keypair[1] } } return "" } else { for (let i = 0; i < querys.length; i++) { let keypair = querys[i].split("="); if (keypair[0] == key) { return true } } return false } }, /** * 获取查询参数的值 * @param {String} key * @returns String */ urlQueryValue(key) { return this.urlHasValue(key, true) || "" }, /** * 判断是否存在hash * @param {String} key * @returns Boolean */ urlIsHasHash(key) { let querys = window.location.hash.substr(1).replace('/?', '').split("&"); return querys.includes(key) }, islongPhone: function() { //是否是刘海全面屏幕 仅仅根据比例判断 - - //screen.height == 812 && screen.width == 375) var r = screen.height / screen.width; //可能横屏 return this.isMobile() && (r > 1.99 || r < 0.502512) //18/9=2.165 //??? }, detectWeixin: function() { //微信 包括PC的微信 return window.navigator.userAgent.toLowerCase().match(/MicroMessenger/i) == "micromessenger" }, detectWeixinMiniProgram: function() { return window.navigator.userAgent.match("miniProgram") }, detectEdge: function() { return window.navigator.userAgent.indexOf("Edge") > -1 }, detectApp: function() { return this.urlHasValue("app") }, /** * 判断标签页是否切换状态 */ isTabHidden: function() { var prefixes = ["webkit", "moz", "ms", "o"]; if ("hidden" in document) return document.hidden for (var i = 0; i < prefixes.length; i++) { if (prefixes[i] + "Hidden" in document) return document[prefixes[i] + "Hidden"] } return false }, }; WebGLRenderer.prototype.paramThreeToGL = function(e) { var t, i = this.extensions, r = this.getContext();//context; if (e === RepeatWrapping) return r.REPEAT; if (e === ClampToEdgeWrapping) return r.CLAMP_TO_EDGE; if (e === MirroredRepeatWrapping) return r.MIRRORED_REPEAT; if (e === NearestFilter) return r.NEAREST; if (e === NearestMipMapNearestFilter) return r.NEAREST_MIPMAP_NEAREST; if (e === NearestMipMapLinearFilter) return r.NEAREST_MIPMAP_LINEAR; if (e === LinearFilter) return r.LINEAR; if (e === LinearMipMapNearestFilter) return r.LINEAR_MIPMAP_NEAREST; if (e === LinearMipMapLinearFilter) return r.LINEAR_MIPMAP_LINEAR; if (e === UnsignedByteType) return r.UNSIGNED_BYTE; if (e === UnsignedShort4444Type) return r.UNSIGNED_SHORT_4_4_4_4; if (e === UnsignedShort5551Type) return r.UNSIGNED_SHORT_5_5_5_1; if (e === UnsignedShort565Type) return r.UNSIGNED_SHORT_5_6_5; if (e === ByteType) return r.BYTE; if (e === ShortType) return r.SHORT; if (e === UnsignedShortType) return r.UNSIGNED_SHORT; if (e === IntType) return r.INT; if (e === UnsignedIntType) return r.UNSIGNED_INT; if (e === FloatType) return r.FLOAT; if (t = i.get("OES_texture_half_float"), null !== t && e === HalfFloatType) return t.HALF_FLOAT_OES; if (e === AlphaFormat) return r.ALPHA; if (e === RGBFormat) return r.RGB; if (e === RGBAFormat) return r.RGBA; if (e === LuminanceFormat) return r.LUMINANCE; if (e === LuminanceAlphaFormat) return r.LUMINANCE_ALPHA; if (e === AddEquation) return r.FUNC_ADD; if (e === SubtractEquation) return r.FUNC_SUBTRACT; if (e === ReverseSubtractEquation) return r.FUNC_REVERSE_SUBTRACT; if (e === ZeroFactor) return r.ZERO; if (e === OneFactor) return r.ONE; if (e === SrcColorFactor) return r.SRC_COLOR; if (e === OneMinusSrcColorFactor) return r.ONE_MINUS_SRC_COLOR; if (e === SrcAlphaFactor) return r.SRC_ALPHA; if (e === OneMinusSrcAlphaFactor) return r.ONE_MINUS_SRC_ALPHA; if (e === DstAlphaFactor) return r.DST_ALPHA; if (e === OneMinusDstAlphaFactor) return r.ONE_MINUS_DST_ALPHA; if (e === DstColorFactor) return r.DST_COLOR; if (e === OneMinusDstColorFactor) return r.ONE_MINUS_DST_COLOR; if (e === SrcAlphaSaturateFactor) return r.SRC_ALPHA_SATURATE; if (t = i.get("WEBGL_compressed_texture_s3tc"), null !== t) { if (e === RGB_S3TC_DXT1_Format) return t.COMPRESSED_RGB_S3TC_DXT1_EXT; if (e === RGBA_S3TC_DXT1_Format$1) return t.COMPRESSED_RGBA_S3TC_DXT1_EXT; if (e === RGBA_S3TC_DXT3_Format) return t.COMPRESSED_RGBA_S3TC_DXT3_EXT; if (e === RGBA_S3TC_DXT5_Format$1) return t.COMPRESSED_RGBA_S3TC_DXT5_EXT } if (t = i.get("WEBGL_compressed_texture_pvrtc"), null !== t) { if (e === RGB_PVRTC_4BPPV1_Format) return t.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; if (e === RGB_PVRTC_2BPPV1_Format) return t.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; if (e === RGBA_PVRTC_4BPPV1_Format) return t.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; if (e === RGBA_PVRTC_2BPPV1_Format) return t.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG } if (t = i.get("WEBGL_compressed_texture_etc1"), null !== t && e === RGB_ETC1_Format) return t.COMPRESSED_RGB_ETC1_WEBGL; if (t = i.get("EXT_blend_minmax"), null !== t) { if (e === MinEquation) return t.MIN_EXT; if (e === MaxEquation) return t.MAX_EXT } return 0 }; EventDispatcher.prototype.addEventListener = function(type, listener, {importance=0, once}={}){ //add importance if ( this._listeners === undefined ) this._listeners = {}; const listeners = this._listeners; if ( listeners[ type ] === undefined ) { listeners[ type ] = []; } /* if(type == 'flyingDone'){ console.log('addEventListener flyingDone') } */ if ( !listeners[ type ].some(e=>e.listener == listener ) ) { listeners[type].push({ listener, importance, once}); listeners[type] = listeners[type].sort((e,a)=> a.importance - e.importance);//add } }; EventDispatcher.prototype.hasEventListener = function(type, listener){ if ( this._listeners === undefined ) return false; const listeners = this._listeners; return listeners[ type ] !== undefined && listeners[ type ].some(e=>e.listener == listener ) }; EventDispatcher.prototype.removeEventListener = function(type, listener){ if ( this._listeners === undefined ) return; const listeners = this._listeners; const listenerArray = listeners[ type ]; if ( listenerArray !== undefined ) { /* const index = listenerArray.indexOf( listener ); if ( index !== - 1 ) { listenerArray.splice( index, 1 ); } */ let item = listenerArray.find(e=>e.listener == listener); item && listenerArray.splice(listenerArray.indexOf(item), 1); } }; EventDispatcher.prototype.removeEventListeners = function(type){ //add if(this._listeners && this._listeners[type] !== undefined){ delete this._listeners[type]; } }; EventDispatcher.prototype.removeAllListeners = function(){ //add this._listeners = {}; }; EventDispatcher.prototype.dispatchEvent = function(event){ if(typeof event == 'string'){//add event = {type:event}; } if ( this._listeners === undefined ) return; const listeners = this._listeners; const listenerArray = listeners[ event.type ]; if ( listenerArray !== undefined ) { event.target = this; // Make a copy, in case listeners are removed while iterating. for(let {listener, once} of listenerArray.slice(0)){ if(once){ this.removeEventListener(event.type,listener); } let result = listener.call(this, event); //add stopContinue if(result && result.stopContinue){ break } } } }; EventDispatcher.prototype.traverse = function(callback){ let result = callback( this ); if(result && result.stopContinue){//xzw add return } const children = this.children; if(children){ for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].traverse( callback ); } } }; Object3D.prototype.traverse = function ( callback ) { let result = callback( this ); if(result && result.stopContinue){//xzw add return } const children = this.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ] && children[ i ].traverse( callback ); } }; Material.prototype.setValues = function ( values ) { if ( values === undefined ) return; for ( const key in values ) { const newValue = values[ key ]; if ( newValue === undefined ) { console.warn( 'THREE.Material: \'' + key + '\' parameter is undefined.' ); continue; } // for backward compatability if shading is set in the constructor if ( key === 'shading' ) { console.warn( 'THREE.' + this.type + ': .shading has been removed. Use the boolean .flatShading instead.' ); this.flatShading = ( newValue === FlatShading ) ? true : false; continue; } const currentValue = this[ key ]; /* if ( currentValue === undefined ) { //-----主要删了这段,否则很难和 set 一起使用 //console.warn( 'THREE.' + this.type + ': \'' + key + '\' is not a property of this material.' ); continue; } */ if ( currentValue && currentValue.isColor ) { currentValue.set( newValue ); } else if ( ( currentValue && currentValue.isVector3 ) && ( newValue && newValue.isVector3 ) ) { currentValue.copy( newValue ); } else { this[ key ] = newValue; } } }; function ascSort$1( a, b ) { return a.distance - b.distance; } function intersectObject$1( object, raycaster, intersects, recursive, ignoreUnvisible ) { if(ignoreUnvisible && !object.visible)return //add if ( object.layers.test( raycaster.layers ) ) { object.raycast( raycaster, intersects ); } if ( recursive === true ) { const children = object.children; for ( let i = 0, l = children.length; i < l; i ++ ) { intersectObject$1( children[ i ], raycaster, intersects, true, ignoreUnvisible); } } } Raycaster.prototype.intersectObject = function ( object, recursive, optionalTarget, ignoreUnvisible ) { const intersects = optionalTarget || []; intersectObject$1( object, this, intersects, recursive, ignoreUnvisible ); intersects.sort( ascSort$1 ); return intersects; }; Raycaster.prototype.intersectObjects = function ( objects, recursive, optionalTarget, ignoreUnvisible ) {//add ignoreUnvisible 跳过不可见 const intersects = optionalTarget || []; if ( Array.isArray( objects ) === false ) { console.warn( 'THREE.Raycaster.intersectObjects: objects is not an Array.' ); return intersects; } for ( let i = 0, l = objects.length; i < l; i ++ ) { intersectObject$1( objects[ i ], this, intersects, recursive, ignoreUnvisible ); } intersects.sort( ascSort$1 ); return intersects; }; Object3D.prototype.realVisible = function(){ let v = true; let parent = this; let lastParent; while(parent){ if(parent.visible === false){ v = false; break; } lastParent = parent; parent = parent.parent; } if(v && !(lastParent instanceof Scene)){//已被删除 v = false; } return v }; var MathLight = {}; MathLight.RADIANS_PER_DEGREE = Math.PI / 180; MathLight.DEGREES_PER_RADIAN = 180 / Math.PI; MathLight.Vector3 = function(e, t, i) { this.x = e || 0, this.y = t || 0, this.z = i || 0; }; MathLight.Matrix4 = function() { this.elements = new Float32Array([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]), arguments.length > 0 && console.error("MathLight.Matrix4: the constructor no longer reads arguments. use .set() instead."); }; MathLight.Matrix4.prototype = { identity: function() { return this.set(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1), this }, copy: function(e) { return this.elements.set(e.elements), this }, applyToVector3: function(e) { var t = e.x , i = e.y , n = e.z , r = this.elements; return e.x = r[0] * t + r[4] * i + r[8] * n + r[12], e.y = r[1] * t + r[5] * i + r[9] * n + r[13], e.z = r[2] * t + r[6] * i + r[10] * n + r[14], this }, getInverse: function(e, t) { var i = this.elements , n = e.elements , r = n[0] , o = n[1] , a = n[2] , s = n[3] , l = n[4] , c = n[5] , h = n[6] , u = n[7] , d = n[8] , p = n[9] , f = n[10] , g = n[11] , m = n[12] , v = n[13] , A = n[14] , y = n[15] , C = p * A * u - v * f * u + v * h * g - c * A * g - p * h * y + c * f * y , I = m * f * u - d * A * u - m * h * g + l * A * g + d * h * y - l * f * y , E = d * v * u - m * p * u + m * c * g - l * v * g - d * c * y + l * p * y , b = m * p * h - d * v * h - m * c * f + l * v * f + d * c * A - l * p * A , w = r * C + o * I + a * E + s * b; if (0 === w) { var _ = "MathLight.Matrix4.getInverse(): can't invert matrix, determinant is 0"; if (t) throw new Error(_); return console.warn(_), this.identity() } var T = 1 / w; return i[0] = C * T, i[1] = (v * f * s - p * A * s - v * a * g + o * A * g + p * a * y - o * f * y) * T, i[2] = (c * A * s - v * h * s + v * a * u - o * A * u - c * a * y + o * h * y) * T, i[3] = (p * h * s - c * f * s - p * a * u + o * f * u + c * a * g - o * h * g) * T, i[4] = I * T, i[5] = (d * A * s - m * f * s + m * a * g - r * A * g - d * a * y + r * f * y) * T, i[6] = (m * h * s - l * A * s - m * a * u + r * A * u + l * a * y - r * h * y) * T, i[7] = (l * f * s - d * h * s + d * a * u - r * f * u - l * a * g + r * h * g) * T, i[8] = E * T, i[9] = (m * p * s - d * v * s - m * o * g + r * v * g + d * o * y - r * p * y) * T, i[10] = (l * v * s - m * c * s + m * o * u - r * v * u - l * o * y + r * c * y) * T, i[11] = (d * c * s - l * p * s - d * o * u + r * p * u + l * o * g - r * c * g) * T, i[12] = b * T, i[13] = (d * v * a - m * p * a + m * o * f - r * v * f - d * o * A + r * p * A) * T, i[14] = (m * c * a - l * v * a - m * o * h + r * v * h + l * o * A - r * c * A) * T, i[15] = (l * p * a - d * c * a + d * o * h - r * p * h - l * o * f + r * c * f) * T, this }, makeRotationFromQuaternion: function(e) { var t = this.elements , i = e.x , n = e.y , r = e.z , o = e.w , a = i + i , s = n + n , l = r + r , c = i * a , h = i * s , u = i * l , d = n * s , p = n * l , f = r * l , g = o * a , m = o * s , v = o * l; return t[0] = 1 - (d + f), t[4] = h - v, t[8] = u + m, t[1] = h + v, t[5] = 1 - (c + f), t[9] = p - g, t[2] = u - m, t[6] = p + g, t[10] = 1 - (c + d), t[3] = 0, t[7] = 0, t[11] = 0, t[12] = 0, t[13] = 0, t[14] = 0, t[15] = 1, this } }; MathLight.Quaternion = function(e, t, i, n) { this._x = e || 0, this._y = t || 0, this._z = i || 0, this._w = void 0 !== n ? n : 1; }; MathLight.Quaternion.prototype = { get x() { return this._x }, set x(e) { this._x = e; }, get y() { return this._y }, set y(e) { this._y = e; }, get z() { return this._z }, set z(e) { this._z = e; }, get w() { return this._w }, set w(e) { this._w = e; }, copy: function(e) { this._x = e.x, this._y = e.y, this._z = e.z, this._w = e.w; }, inverse: function() { return this.conjugate().normalize() }, conjugate: function() { return this._x *= -1, this._y *= -1, this._z *= -1, this }, length: function() { return Math.sqrt(this._x * this._x + this._y * this._y + this._z * this._z + this._w * this._w) }, normalize: function() { var e = this.length(); return 0 === e ? (this._x = 0, this._y = 0, this._z = 0, this._w = 1) : (e = 1 / e, this._x = this._x * e, this._y = this._y * e, this._z = this._z * e, this._w = this._w * e), this }, setFromAxisAngle: function(e, t) { var i = t / 2 , n = Math.sin(i); return this._x = e.x * n, this._y = e.y * n, this._z = e.z * n, this._w = Math.cos(i), this }, setFromUnitVectors: function() { var e, t, i = 1e-6; return function(n, o) { return void 0 === e && (e = new MathLight.Vector3), t = MathLight.dot(n, o) + 1, t < i ? (t = 0, Math.abs(n.x) > Math.abs(n.z) ? MathLight.setVector(e, -n.y, n.x, 0) : MathLight.setVector(e, 0, -n.z, n.y)) : MathLight.cross(n, o, e), this._x = e.x, this._y = e.y, this._z = e.z, this._w = t, this.normalize() } }(), multiply: function(e) { return this.multiplyQuaternions(this, e) }, premultiply: function(e) { return this.multiplyQuaternions(e, this) }, multiplyQuaternions: function(e, t) { var i = e._x , n = e._y , r = e._z , o = e._w , a = t._x , s = t._y , l = t._z , c = t._w; return this._x = i * c + o * a + n * l - r * s, this._y = n * c + o * s + r * a - i * l, this._z = r * c + o * l + i * s - n * a, this._w = o * c - i * a - n * s - r * l, this } }; MathLight.convertWorkshopVector = function(e) { return new MathLight.Vector3(-e.x,e.y,e.z) }; MathLight.convertWorkshopQuaternion = function(e) { return new MathLight.Quaternion(-e.x,e.y,e.z,-e.w).multiply(new MathLight.Quaternion(Math.sqrt(2) / 2,Math.sqrt(2) / 2,0,0)) }; MathLight.convertWorkshopOrthoZoom = function(e) { //return e === -1 ? -1 : e / 16 * ($('#player').width() / $('#player').height()) / n.workshopApsect return e === -1 ? -1 : e * ($("#player").width() / $("#player").height()) ; }; MathLight.convertWorkshopPanoramaQuaternion = function(e) { return new MathLight.Quaternion(e.x,-e.y,-e.z,e.w).normalize().multiply((new MathLight.Quaternion).setFromAxisAngle(new MathLight.Vector3(0,1,0), 270 * MathLight.RADIANS_PER_DEGREE)) }; MathLight.normalize = function(e) { var t = e.x * e.x + e.y * e.y + e.z * e.z , i = Math.sqrt(t); e.x /= i, e.y /= i, e.z /= i; }; MathLight.dot = function(e, t) { return e.x * t.x + e.y * t.y + e.z * t.z }; MathLight.cross = function(e, t, i) { var n = e.x , r = e.y , o = e.z; i.x = r * t.z - o * t.y, i.y = o * t.x - n * t.z, i.z = n * t.y - r * t.x; }; MathLight.setVector = function(e, t, i, n) { e.x = t, e.y = i, e.z = n; }; MathLight.copyVector = function(e, t) { t.x = e.x, t.y = e.y, t.z = e.z; }; MathLight.addVector = function(e, t) { e.x += t.x, e.y += t.y, e.z += t.z; }; MathLight.subVector = function(e, t) { e.x -= t.x, e.y -= t.y, e.z -= t.z; }; MathLight.applyQuaternionToVector = function(e, t) { var i = t.x , n = t.y , r = t.z , o = e.x , a = e.y , s = e.z , l = e.w , c = l * i + a * r - s * n , h = l * n + s * i - o * r , u = l * r + o * n - a * i , d = -o * i - a * n - s * r; t.x = c * l + d * -o + h * -s - u * -a, t.y = h * l + d * -a + u * -o - c * -s, t.z = u * l + d * -s + c * -a - h * -o; }; MathLight.angleBetweenVectors = function(e, t) { return Math.acos(MathLight.dot(e, t)) }; var cameraLight = { clampVFOV: function(currentFov, maxHFov, width, height) {//限制currentFov, 使之造成的横向fov不大于指定值maxHFov var r = cameraLight.getHFOVFromVFOV(currentFov, width, height); return r > maxHFov ? cameraLight.getVFOVFromHFOV(maxHFov, width, height) : currentFov }, getHFOVForCamera: function(camera, getRad) { return cameraLight.getHFOVByScreenPrecent(camera.fov, camera.aspect, getRad) }, //add getHFOVByScreenPrecent: function(fov, percent, getRad) { //当fov为占比百分百时,percent代表在屏幕上从中心到边缘的占比 let rad = 2 * Math.atan(percent * Math.tan(fov * MathLight.RADIANS_PER_DEGREE / 2)); if(getRad)return rad else return rad * MathLight.DEGREES_PER_RADIAN; } }; const XHRFactory = { config: { withCredentials: false, customHeaders: [ { header: null, value: null } ] }, createXMLHttpRequest: function () { let xhr = new XMLHttpRequest(); if (this.config.customHeaders && Array.isArray(this.config.customHeaders) && this.config.customHeaders.length > 0) { let baseOpen = xhr.open; let customHeaders = this.config.customHeaders; xhr.open = function () { baseOpen.apply(this, [].slice.call(arguments)); customHeaders.forEach(function (customHeader) { if (!!customHeader.header && !!customHeader.value) { xhr.setRequestHeader(customHeader.header, customHeader.value); } }); }; } return xhr; } }; class TextSprite$1 extends Object3D{//old constructor(text){ super(); let texture = new Texture(); texture.minFilter = LinearFilter; texture.magFilter = LinearFilter; let spriteMaterial = new SpriteMaterial({ map: texture, depthTest: false, depthWrite: false}); this.texture = texture; this.material = spriteMaterial; //this.material = getRawMaterial(texture); this.sprite = new Sprite$1(this.material); this.add(this.sprite); this.borderThickness = 4; this.fontface = 'Arial'; this.fontsize = 28; this.borderColor = { r: 0, g: 0, b: 0, a: 1.0 }; this.backgroundColor = { r: 255, g: 255, b: 255, a: 1.0 }; this.textColor = {r: 255, g: 255, b: 255, a: 1.0}; this.text = ''; this.setText(text); } setText(text){ if (this.text !== text){ this.text = text; this.update(); } } setTextColor(color){ this.textColor = color; this.update(); } setBorderColor(color){ this.borderColor = color; this.update(); } setBackgroundColor(color){ this.backgroundColor = color; this.update(); } update(){ let canvas = document.createElement('canvas'); let context = canvas.getContext('2d'); context.font = 'Bold ' + this.fontsize + 'px ' + this.fontface; // get size data (height depends only on font size) let metrics = context.measureText(this.text); let textWidth = metrics.width; let margin = 5; let spriteWidth = 2 * margin + textWidth + 2 * this.borderThickness; let spriteHeight = this.fontsize * 1.4 + 2 * this.borderThickness; context.canvas.width = spriteWidth; context.canvas.height = spriteHeight; context.font = 'Bold ' + this.fontsize + 'px ' + this.fontface; // background color context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' + this.backgroundColor.b + ',' + this.backgroundColor.a + ')'; // border color context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' + this.borderColor.b + ',' + this.borderColor.a + ')'; context.lineWidth = this.borderThickness; this.roundRect(context, this.borderThickness / 2, this.borderThickness / 2, textWidth + this.borderThickness + 2 * margin, this.fontsize * 1.4 + this.borderThickness, 6); // text color context.strokeStyle = 'rgba(0, 0, 0, 1.0)'; context.strokeText(this.text, this.borderThickness + margin, this.fontsize + this.borderThickness); context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' + this.textColor.b + ',' + this.textColor.a + ')'; context.fillText(this.text, this.borderThickness + margin, this.fontsize + this.borderThickness); let texture = new Texture(canvas); texture.minFilter = LinearFilter; texture.magFilter = LinearFilter; texture.needsUpdate = true; //this.material.needsUpdate = true; // { // screen-space sprite // let [screenWidth, screenHeight] = [1620, 937]; // let uniforms = this.sprite.material.uniforms; // let aspect = spriteHeight / spriteWidth; // let factor = 0.5; // let w = spriteWidth / screenWidth; // let h = spriteHeight / screenHeight; // uniforms.uScale.value = [2 * w, 2 * h]; // //uniforms.uScale.value = [factor * 1, factor * aspect]; // this.sprite.material.uniforms.map.value = texture; // } this.sprite.material.map = texture; this.texture = texture; this.sprite.scale.set(spriteWidth * 0.01, spriteHeight * 0.01, 1.0); } roundRect(ctx, x, y, w, h, r){ ctx.beginPath(); ctx.moveTo(x + r, y); ctx.lineTo(x + w - r, y); ctx.quadraticCurveTo(x + w, y, x + w, y + r); ctx.lineTo(x + w, y + h - r); ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h); ctx.lineTo(x + r, y + h); ctx.quadraticCurveTo(x, y + h, x, y + h - r); ctx.lineTo(x, y + r); ctx.quadraticCurveTo(x, y, x + r, y); ctx.closePath(); ctx.fill(); ctx.stroke(); } } class Volume extends Object3D { constructor (args = {}) { super(); if(this.constructor.name === "Volume"){ console.warn("Can't create object of class Volume directly. Use classes BoxVolume or SphereVolume instead."); } //console.log(this); //console.log(this.constructor); //console.log(this.constructor.name); this._clip = args.clip || false; this._visible = true; this.showVolumeLabel = true; this._modifiable = args.modifiable || true; this.label = new TextSprite$1('0'); this.label.setBorderColor({r: 0, g: 255, b: 0, a: 0.0}); this.label.setBackgroundColor({r: 0, g: 255, b: 0, a: 0.0}); this.label.material.depthTest = false; this.label.material.depthWrite = false; this.label.material.transparent = true; this.label.position.y -= 0.5; this.add(this.label); this.label.updateMatrixWorld = () => { let volumeWorldPos = new Vector3(); volumeWorldPos.setFromMatrixPosition(this.matrixWorld); this.label.position.copy(volumeWorldPos); this.label.updateMatrix(); this.label.matrixWorld.copy(this.label.matrix); this.label.matrixWorldNeedsUpdate = false; for (let i = 0, l = this.label.children.length; i < l; i++) { this.label.children[ i ].updateMatrixWorld(true); } }; { // event listeners this.addEventListener('select', e => {}); this.addEventListener('deselect', e => {}); } } get visible(){ return this._visible; } set visible(value){ if(this._visible !== value){ this._visible = value; this.dispatchEvent({type: "visibility_changed", object: this}); } } getVolume () { console.warn("override this in subclass"); } update () { }; raycast (raycaster, intersects) { } get clip () { return this._clip; } set clip (value) { if(this._clip !== value){ this._clip = value; this.update(); this.dispatchEvent({ type: "clip_changed", object: this }); } } get modifieable () { return this._modifiable; } set modifieable (value) { this._modifiable = value; this.update(); } }; class BoxVolume extends Volume{ constructor(args = {}){ super(args); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = 'box_' + this.constructor.counter; let boxGeometry = new BoxGeometry(1, 1, 1); boxGeometry.computeBoundingBox(); let boxFrameGeometry = new Geometry(); { let Vector3$1 = Vector3; boxFrameGeometry.vertices.push( // bottom new Vector3$1(-0.5, -0.5, 0.5), new Vector3$1(0.5, -0.5, 0.5), new Vector3$1(0.5, -0.5, 0.5), new Vector3$1(0.5, -0.5, -0.5), new Vector3$1(0.5, -0.5, -0.5), new Vector3$1(-0.5, -0.5, -0.5), new Vector3$1(-0.5, -0.5, -0.5), new Vector3$1(-0.5, -0.5, 0.5), // top new Vector3$1(-0.5, 0.5, 0.5), new Vector3$1(0.5, 0.5, 0.5), new Vector3$1(0.5, 0.5, 0.5), new Vector3$1(0.5, 0.5, -0.5), new Vector3$1(0.5, 0.5, -0.5), new Vector3$1(-0.5, 0.5, -0.5), new Vector3$1(-0.5, 0.5, -0.5), new Vector3$1(-0.5, 0.5, 0.5), // sides new Vector3$1(-0.5, -0.5, 0.5), new Vector3$1(-0.5, 0.5, 0.5), new Vector3$1(0.5, -0.5, 0.5), new Vector3$1(0.5, 0.5, 0.5), new Vector3$1(0.5, -0.5, -0.5), new Vector3$1(0.5, 0.5, -0.5), new Vector3$1(-0.5, -0.5, -0.5), new Vector3$1(-0.5, 0.5, -0.5), ); } this.material = new MeshBasicMaterial({ color: 0x00ff00, transparent: true, opacity: 0.3, depthTest: true, depthWrite: false}); this.box = new Mesh(boxGeometry, this.material); this.box.geometry.computeBoundingBox(); this.boundingBox = this.box.geometry.boundingBox; this.add(this.box); this.frame = new LineSegments(boxFrameGeometry, new LineBasicMaterial({color: 0x000000})); // this.frame.mode = THREE.Lines; this.add(this.frame); this.update(); } update(){ this.boundingBox = this.box.geometry.boundingBox; this.boundingSphere = this.boundingBox.getBoundingSphere(new Sphere()); if (this._clip) { this.box.visible = false; this.label.visible = false; } else { this.box.visible = true; this.label.visible = this.showVolumeLabel; } } raycast (raycaster, intersects) { let is = []; this.box.raycast(raycaster, is); if (is.length > 0) { let I = is[0]; intersects.push({ distance: I.distance, object: this, point: I.point.clone() }); } } getVolume(){ return Math.abs(this.scale.x * this.scale.y * this.scale.z); } }; class SphereVolume$1 extends Volume{ constructor(args = {}){ super(args); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = 'sphere_' + this.constructor.counter; let sphereGeometry = new SphereGeometry(1, 32, 32); sphereGeometry.computeBoundingBox(); this.material = new MeshBasicMaterial({ color: 0x00ff00, transparent: true, opacity: 0.3, depthTest: true, depthWrite: false}); this.sphere = new Mesh(sphereGeometry, this.material); this.sphere.visible = false; this.sphere.geometry.computeBoundingBox(); this.boundingBox = this.sphere.geometry.boundingBox; this.add(this.sphere); this.label.visible = false; let frameGeometry = new Geometry(); { let steps = 64; let uSegments = 8; let vSegments = 5; let r = 1; for(let uSegment = 0; uSegment < uSegments; uSegment++){ let alpha = (uSegment / uSegments) * Math.PI * 2; let dirx = Math.cos(alpha); let diry = Math.sin(alpha); for(let i = 0; i <= steps; i++){ let v = (i / steps) * Math.PI * 2; let vNext = v + 2 * Math.PI / steps; let height = Math.sin(v); let xyAmount = Math.cos(v); let heightNext = Math.sin(vNext); let xyAmountNext = Math.cos(vNext); let vertex = new Vector3(dirx * xyAmount, diry * xyAmount, height); frameGeometry.vertices.push(vertex); let vertexNext = new Vector3(dirx * xyAmountNext, diry * xyAmountNext, heightNext); frameGeometry.vertices.push(vertexNext); } } // creates rings at poles, just because it's easier to implement for(let vSegment = 0; vSegment <= vSegments + 1; vSegment++){ //let height = (vSegment / (vSegments + 1)) * 2 - 1; // -1 to 1 let uh = (vSegment / (vSegments + 1)); // -1 to 1 uh = (1 - uh) * (-Math.PI / 2) + uh *(Math.PI / 2); let height = Math.sin(uh); console.log(uh, height); for(let i = 0; i <= steps; i++){ let u = (i / steps) * Math.PI * 2; let uNext = u + 2 * Math.PI / steps; let dirx = Math.cos(u); let diry = Math.sin(u); let dirxNext = Math.cos(uNext); let diryNext = Math.sin(uNext); let xyAmount = Math.sqrt(1 - height * height); let vertex = new Vector3(dirx * xyAmount, diry * xyAmount, height); frameGeometry.vertices.push(vertex); let vertexNext = new Vector3(dirxNext * xyAmount, diryNext * xyAmount, height); frameGeometry.vertices.push(vertexNext); } } } this.frame = new LineSegments(frameGeometry, new LineBasicMaterial({color: 0x000000})); this.add(this.frame); let frameMaterial = new MeshBasicMaterial({wireframe: true, color: 0x000000}); this.frame = new Mesh(sphereGeometry, frameMaterial); //this.add(this.frame); //this.frame = new THREE.LineSegments(boxFrameGeometry, new THREE.LineBasicMaterial({color: 0x000000})); // this.frame.mode = THREE.Lines; //this.add(this.frame); this.update(); } update(){ this.boundingBox = this.sphere.geometry.boundingBox; this.boundingSphere = this.boundingBox.getBoundingSphere(new Sphere()); //if (this._clip) { // this.sphere.visible = false; // this.label.visible = false; //} else { // this.sphere.visible = true; // this.label.visible = this.showVolumeLabel; //} } raycast (raycaster, intersects) { let is = []; this.sphere.raycast(raycaster, is); if (is.length > 0) { let I = is[0]; intersects.push({ distance: I.distance, object: this, point: I.point.clone() }); } } // see https://en.wikipedia.org/wiki/Ellipsoid#Volume getVolume(){ return (4 / 3) * Math.PI * this.scale.x * this.scale.y * this.scale.z; } }; class Profile extends Object3D{ constructor () { super(); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = 'Profile_' + this.constructor.counter; this.points = []; this.spheres = []; this.edges = []; this.boxes = []; this.width = 1; this.height = 20; this._modifiable = true; this.sphereGeometry = new SphereGeometry(0.4, 10, 10); this.color = new Color(0xff0000); this.lineColor = new Color(0xff0000); } createSphereMaterial () { let sphereMaterial = new MeshLambertMaterial({ //shading: THREE.SmoothShading, color: 0xff0000, depthTest: false, depthWrite: false} ); return sphereMaterial; }; getSegments () { let segments = []; for (let i = 0; i < this.points.length - 1; i++) { let start = this.points[i].clone(); let end = this.points[i + 1].clone(); segments.push({start: start, end: end}); } return segments; } getSegmentMatrices () { let segments = this.getSegments(); let matrices = []; for (let segment of segments) { let {start, end} = segment; let box = new Object3D(); let length = start.clone().setZ(0).distanceTo(end.clone().setZ(0)); box.scale.set(length, 10000, this.width); box.up.set(0, 0, 1); let center = new Vector3().addVectors(start, end).multiplyScalar(0.5); let diff = new Vector3().subVectors(end, start); let target = new Vector3(diff.y, -diff.x, 0); box.position.set(0, 0, 0); box.lookAt(target); box.position.copy(center); box.updateMatrixWorld(); matrices.push(box.matrixWorld); } return matrices; } addMarker (point) { this.points.push(point); let sphere = new Mesh(this.sphereGeometry, this.createSphereMaterial()); this.add(sphere); this.spheres.push(sphere); // edges & boxes if (this.points.length > 1) { let lineGeometry = new Geometry(); lineGeometry.vertices.push(new Vector3(), new Vector3()); lineGeometry.colors.push(this.lineColor, this.lineColor, this.lineColor); let lineMaterial = new LineBasicMaterial({ vertexColors: VertexColors, linewidth: 2, transparent: true, opacity: 0.4 }); lineMaterial.depthTest = false; let edge = new Line(lineGeometry, lineMaterial); edge.visible = false; this.add(edge); this.edges.push(edge); let boxGeometry = new BoxGeometry(1, 1, 1); let boxMaterial = new MeshBasicMaterial({color: 0xff0000, transparent: true, opacity: 0.2}); let box = new Mesh(boxGeometry, boxMaterial); box.visible = false; this.add(box); this.boxes.push(box); } { // event listeners let drag = (e) => { let I = Utils.getMousePointCloudIntersection( e.drag.end, e.viewer.scene.getActiveCamera(), e.viewer, e.viewer.scene.pointclouds); if (I) { let i = this.spheres.indexOf(e.drag.object); if (i !== -1) { this.setPosition(i, I.location); //this.dispatchEvent({ // 'type': 'marker_moved', // 'profile': this, // 'index': i //}); } } }; let drop = e => { let i = this.spheres.indexOf(e.drag.object); if (i !== -1) { this.dispatchEvent({ 'type': 'marker_dropped', 'profile': this, 'index': i }); } }; let mouseover = (e) => e.object.material.emissive.setHex(0x888888); let mouseleave = (e) => e.object.material.emissive.setHex(0x000000); sphere.addEventListener('drag', drag); sphere.addEventListener('drop', drop); sphere.addEventListener('mouseover', mouseover); sphere.addEventListener('mouseleave', mouseleave); } let event = { type: 'marker_added', profile: this, sphere: sphere }; this.dispatchEvent(event); this.setPosition(this.points.length - 1, point); } removeMarker (index) { this.points.splice(index, 1); this.remove(this.spheres[index]); let edgeIndex = (index === 0) ? 0 : (index - 1); this.remove(this.edges[edgeIndex]); this.edges.splice(edgeIndex, 1); this.remove(this.boxes[edgeIndex]); this.boxes.splice(edgeIndex, 1); this.spheres.splice(index, 1); this.update(); this.dispatchEvent({ 'type': 'marker_removed', 'profile': this }); } setPosition (index, position) { let point = this.points[index]; point.copy(position); let event = { type: 'marker_moved', profile: this, index: index, position: point.clone() }; this.dispatchEvent(event); this.update(); } setWidth (width) { this.width = width; let event = { type: 'width_changed', profile: this, width: width }; this.dispatchEvent(event); this.update(); } getWidth () { return this.width; } update () { if (this.points.length === 0) { return; } else if (this.points.length === 1) { let point = this.points[0]; this.spheres[0].position.copy(point); return; } let min = this.points[0].clone(); let max = this.points[0].clone(); let centroid = new Vector3(); let lastIndex = this.points.length - 1; for (let i = 0; i <= lastIndex; i++) { let point = this.points[i]; let sphere = this.spheres[i]; let leftIndex = (i === 0) ? lastIndex : i - 1; // let rightIndex = (i === lastIndex) ? 0 : i + 1; let leftVertex = this.points[leftIndex]; // let rightVertex = this.points[rightIndex]; let leftEdge = this.edges[leftIndex]; let rightEdge = this.edges[i]; let leftBox = this.boxes[leftIndex]; // rightBox = this.boxes[i]; // let leftEdgeLength = point.distanceTo(leftVertex); // let rightEdgeLength = point.distanceTo(rightVertex); // let leftEdgeCenter = new THREE.Vector3().addVectors(leftVertex, point).multiplyScalar(0.5); // let rightEdgeCenter = new THREE.Vector3().addVectors(point, rightVertex).multiplyScalar(0.5); sphere.position.copy(point); if (this._modifiable) { sphere.visible = true; } else { sphere.visible = false; } if (leftEdge) { leftEdge.geometry.vertices[1].copy(point); leftEdge.geometry.verticesNeedUpdate = true; leftEdge.geometry.computeBoundingSphere(); } if (rightEdge) { rightEdge.geometry.vertices[0].copy(point); rightEdge.geometry.verticesNeedUpdate = true; rightEdge.geometry.computeBoundingSphere(); } if (leftBox) { let start = leftVertex; let end = point; let length = start.clone().setZ(0).distanceTo(end.clone().setZ(0)); leftBox.scale.set(length, 1000000, this.width); leftBox.up.set(0, 0, 1); let center = new Vector3().addVectors(start, end).multiplyScalar(0.5); let diff = new Vector3().subVectors(end, start); let target = new Vector3(diff.y, -diff.x, 0); leftBox.position.set(0, 0, 0); leftBox.lookAt(target); leftBox.position.copy(center); } centroid.add(point); min.min(point); max.max(point); } centroid.multiplyScalar(1 / this.points.length); for (let i = 0; i < this.boxes.length; i++) { let box = this.boxes[i]; box.position.z = min.z + (max.z - min.z) / 2; } } raycast (raycaster, intersects) { for (let i = 0; i < this.points.length; i++) { let sphere = this.spheres[i]; sphere.raycast(raycaster, intersects); } // recalculate distances because they are not necessarely correct // for scaled objects. // see https://github.com/mrdoob/three.js/issues/5827 // TODO: remove this once the bug has been fixed for (let i = 0; i < intersects.length; i++) { let I = intersects[i]; I.distance = raycaster.ray.origin.distanceTo(I.point); } intersects.sort(function (a, b) { return a.distance - b.distance; }); }; get modifiable () { return this._modifiable; } set modifiable (value) { this._modifiable = value; this.update(); } } const _box$4 = new Box3(); const _vector$d = new Vector3(); class LineSegmentsGeometry extends InstancedBufferGeometry { constructor() { super(); this.isLineSegmentsGeometry = true; this.type = 'LineSegmentsGeometry'; const positions = [ - 1, 2, 0, 1, 2, 0, - 1, 1, 0, 1, 1, 0, - 1, 0, 0, 1, 0, 0, - 1, - 1, 0, 1, - 1, 0 ]; const uvs = [ - 1, 2, 1, 2, - 1, 1, 1, 1, - 1, - 1, 1, - 1, - 1, - 2, 1, - 2 ]; const index = [ 0, 2, 1, 2, 3, 1, 2, 4, 3, 4, 5, 3, 4, 6, 5, 6, 7, 5 ]; this.setIndex( index ); this.setAttribute( 'position', new Float32BufferAttribute( positions, 3 ) ); this.setAttribute( 'uv', new Float32BufferAttribute( uvs, 2 ) ); } applyMatrix4( matrix ) { const start = this.attributes.instanceStart; const end = this.attributes.instanceEnd; if ( start !== undefined ) { start.applyMatrix4( matrix ); end.applyMatrix4( matrix ); start.needsUpdate = true; } if ( this.boundingBox !== null ) { this.computeBoundingBox(); } if ( this.boundingSphere !== null ) { this.computeBoundingSphere(); } return this; } setPositions( array ) { let lineSegments; if ( array instanceof Float32Array ) { lineSegments = array; } else if ( Array.isArray( array ) ) { lineSegments = new Float32Array( array ); } const instanceBuffer = new InstancedInterleavedBuffer( lineSegments, 6, 1 ); // xyz, xyz this.setAttribute( 'instanceStart', new InterleavedBufferAttribute( instanceBuffer, 3, 0 ) ); // xyz this.setAttribute( 'instanceEnd', new InterleavedBufferAttribute( instanceBuffer, 3, 3 ) ); // xyz // this.computeBoundingBox(); this.computeBoundingSphere(); return this; } setColors( array ) { let colors; if ( array instanceof Float32Array ) { colors = array; } else if ( Array.isArray( array ) ) { colors = new Float32Array( array ); } const instanceColorBuffer = new InstancedInterleavedBuffer( colors, 6, 1 ); // rgb, rgb this.setAttribute( 'instanceColorStart', new InterleavedBufferAttribute( instanceColorBuffer, 3, 0 ) ); // rgb this.setAttribute( 'instanceColorEnd', new InterleavedBufferAttribute( instanceColorBuffer, 3, 3 ) ); // rgb return this; } fromWireframeGeometry( geometry ) { this.setPositions( geometry.attributes.position.array ); return this; } fromEdgesGeometry( geometry ) { this.setPositions( geometry.attributes.position.array ); return this; } fromMesh( mesh ) { this.fromWireframeGeometry( new WireframeGeometry( mesh.geometry ) ); // set colors, maybe return this; } fromLineSegments( lineSegments ) { const geometry = lineSegments.geometry; this.setPositions( geometry.attributes.position.array ); // assumes non-indexed // set colors, maybe return this; } computeBoundingBox() { if ( this.boundingBox === null ) { this.boundingBox = new Box3(); } const start = this.attributes.instanceStart; const end = this.attributes.instanceEnd; if ( start !== undefined && end !== undefined ) { this.boundingBox.setFromBufferAttribute( start ); _box$4.setFromBufferAttribute( end ); this.boundingBox.union( _box$4 ); } } computeBoundingSphere() { if ( this.boundingSphere === null ) { this.boundingSphere = new Sphere(); } if ( this.boundingBox === null ) { this.computeBoundingBox(); } const start = this.attributes.instanceStart; const end = this.attributes.instanceEnd; if ( start !== undefined && end !== undefined ) { const center = this.boundingSphere.center; this.boundingBox.getCenter( center ); let maxRadiusSq = 0; for ( let i = 0, il = start.count; i < il; i ++ ) { _vector$d.fromBufferAttribute( start, i ); maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$d ) ); _vector$d.fromBufferAttribute( end, i ); maxRadiusSq = Math.max( maxRadiusSq, center.distanceToSquared( _vector$d ) ); } this.boundingSphere.radius = Math.sqrt( maxRadiusSq ); if ( isNaN( this.boundingSphere.radius ) ) { console.error( 'THREE.LineSegmentsGeometry.computeBoundingSphere(): Computed radius is NaN. The instanced position data is likely to have NaN values.', this ); } } } toJSON() { // todo } applyMatrix( matrix ) { console.warn( 'THREE.LineSegmentsGeometry: applyMatrix() has been renamed to applyMatrix4().' ); return this.applyMatrix4( matrix ); } } /** * parameters = { * color: , * lineWidth: , * dashed: , * dashScale: , * dashSize: , * dashOffset: , * gapSize: , * resolution: , // to be set by renderer * } */ UniformsLib.line = { /* worldUnits: { value: 1 }, lineWidth: { value: 1 }, resolution: { value: new Vector2( 1, 1 ) }, dashOffset: { value: 0 }, dashScale: { value: 1 }, dashSize: { value: 1 }, gapSize: { value: 1 } // todo FIX - maybe change to totalSize */ worldUnits: { value: 1 }, lineWidth: { value: 1 }, resolution: { value: new Vector2( 1, 1 ) }, viewportOffset: { value: new Vector2(0, 0 ) }, //left, top devicePixelRatio:{ value:window.devicePixelRatio}, dashScale: { value: 1 }, dashSize: { value: 1 }, dashOffset: { value: 0 }, gapSize: { value: 1 }, opacity: { value: 1 }, backColor: {type:'v3', value: new Color("#ddd")}, clipDistance : { type: 'f', value: 4}, //消失距离 occlusionDistance : { type: 'f', value: 1 }, //变为backColor距离 maxClipFactor : { type: 'f', value: 1 }, //0-1 maxOcclusionFactor : { type: 'f', value: 1 }, //0-1 depthTexture:{ value: null }, nearPlane:{value: 0.1}, farPlane:{value: 100000}, //uUseOrthographicCamera:{ type: "b", value: false }, }; ShaderLib[ 'line' ] = { uniforms: UniformsUtils.merge( [ UniformsLib.common, UniformsLib.fog, UniformsLib.line ] ), vertexShader: /* glsl */` #include #include #include #include #include uniform float lineWidth; uniform vec2 resolution; uniform float devicePixelRatio; //add attribute vec3 instanceStart; attribute vec3 instanceEnd; attribute vec3 instanceColorStart; attribute vec3 instanceColorEnd; #ifdef WORLD_UNITS varying vec4 worldPos; varying vec3 worldStart; varying vec3 worldEnd; #ifdef USE_DASH varying vec2 vUv; #endif #else varying vec2 vUv; #endif #ifdef USE_DASH uniform float dashScale; attribute float instanceDistanceStart; attribute float instanceDistanceEnd; varying float vLineDistance; #endif void trimSegment( const in vec4 start, inout vec4 end ) { // trim end segment so it terminates between the camera plane and the near plane // conservative estimate of the near plane float a = projectionMatrix[ 2 ][ 2 ]; // 3nd entry in 3th column float b = projectionMatrix[ 3 ][ 2 ]; // 3nd entry in 4th column float nearEstimate = - 0.5 * b / a; float alpha = ( nearEstimate - start.z ) / ( end.z - start.z ); end.xyz = mix( start.xyz, end.xyz, alpha ); } void main() { #ifdef USE_COLOR vColor.xyz = ( position.y < 0.5 ) ? instanceColorStart : instanceColorEnd; #endif #ifdef USE_DASH vLineDistance = ( position.y < 0.5 ) ? dashScale * instanceDistanceStart : dashScale * instanceDistanceEnd; vUv = uv; #endif float aspect = resolution.x / resolution.y; // camera space vec4 start = modelViewMatrix * vec4( instanceStart, 1.0 ); vec4 end = modelViewMatrix * vec4( instanceEnd, 1.0 ); #ifdef WORLD_UNITS worldStart = start.xyz; worldEnd = end.xyz; #else vUv = uv; #endif // special case for perspective projection, and segments that terminate either in, or behind, the camera plane // clearly the gpu firmware has a way of addressing this issue when projecting into ndc space // but we need to perform ndc-space calculations in the shader, so we must address this issue directly // perhaps there is a more elegant solution -- WestLangley bool perspective = ( projectionMatrix[ 2 ][ 3 ] == - 1.0 ); // 4th entry in the 3rd column if ( perspective ) { if ( start.z < 0.0 && end.z >= 0.0 ) { trimSegment( start, end ); } else if ( end.z < 0.0 && start.z >= 0.0 ) { trimSegment( end, start ); } } // clip space vec4 clipStart = projectionMatrix * start; vec4 clipEnd = projectionMatrix * end; // ndc space vec3 ndcStart = clipStart.xyz / clipStart.w; vec3 ndcEnd = clipEnd.xyz / clipEnd.w; // direction vec2 dir = ndcEnd.xy - ndcStart.xy; // account for clip-space aspect ratio dir.x *= aspect; dir = normalize( dir ); #ifdef WORLD_UNITS // get the offset direction as perpendicular to the view vector vec3 worldDir = normalize( end.xyz - start.xyz ); vec3 offset; if ( position.y < 0.5 ) { offset = normalize( cross( start.xyz, worldDir ) ); } else { offset = normalize( cross( end.xyz, worldDir ) ); } // sign flip if ( position.x < 0.0 ) offset *= - 1.0; float forwardOffset = dot( worldDir, vec3( 0.0, 0.0, 1.0 ) ); // don't extend the line if we're rendering dashes because we // won't be rendering the endcaps #ifndef USE_DASH // extend the line bounds to encompass endcaps start.xyz += - worldDir * lineWidth * 0.5; end.xyz += worldDir * lineWidth * 0.5; // shift the position of the quad so it hugs the forward edge of the line offset.xy -= dir * forwardOffset; offset.z += 0.5; #endif // endcaps if ( position.y > 1.0 || position.y < 0.0 ) { offset.xy += dir * 2.0 * forwardOffset; } // adjust for lineWidth offset *= lineWidth * 0.5; // set the world position worldPos = ( position.y < 0.5 ) ? start : end; worldPos.xyz += offset; // project the worldpos vec4 clip = projectionMatrix * worldPos; // shift the depth of the projected points so the line // segments overlap neatly vec3 clipPose = ( position.y < 0.5 ) ? ndcStart : ndcEnd; clip.z = clipPose.z * clip.w; #else vec2 offset = vec2( dir.y, - dir.x ); // undo aspect ratio adjustment dir.x /= aspect; offset.x /= aspect; // sign flip if ( position.x < 0.0 ) offset *= - 1.0; // endcaps if ( position.y < 0.0 ) { offset += - dir; } else if ( position.y > 1.0 ) { offset += dir; } // adjust for lineWidth offset *= lineWidth; // adjust for clip-space to screen-space conversion // maybe resolution should be based on viewport ... offset /= resolution.y; //* devicePixelRatio; // select end vec4 clip = ( position.y < 0.5 ) ? clipStart : clipEnd; // back to clip space offset *= clip.w; clip.xy += offset; #endif gl_Position = clip; vec4 mvPosition = ( position.y < 0.5 ) ? start : end; // this is an approximation #include #include #include } `, fragmentShader: /* glsl */` uniform vec3 diffuse; uniform float opacity; uniform float lineWidth; uniform bool uUseOrthographicCamera; #ifdef USE_DASH uniform float dashOffset; uniform float dashSize; uniform float gapSize; #endif //加 #if defined(GL_EXT_frag_depth) && defined(useDepth) uniform sampler2D depthTexture; uniform float nearPlane; uniform float farPlane; uniform vec2 resolution; uniform vec2 viewportOffset; uniform vec3 backColor; uniform float occlusionDistance; uniform float clipDistance; uniform float maxClipFactor; uniform float maxOcclusionFactor; #endif varying float vLineDistance; #ifdef WORLD_UNITS varying vec4 worldPos; varying vec3 worldStart; varying vec3 worldEnd; #ifdef USE_DASH varying vec2 vUv; #endif #else varying vec2 vUv; #endif #include #include #include #include #include #if defined(GL_EXT_frag_depth) && defined(useDepth) float convertToLinear(float zValue) { //if(uUseOrthographicCamera){ // return zValue*(farPlane-nearPlane)+nearPlane; //}else{ float z = zValue * 2.0 - 1.0; return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane)); //} } #endif vec2 closestLineToLine(vec3 p1, vec3 p2, vec3 p3, vec3 p4) { float mua; float mub; vec3 p13 = p1 - p3; vec3 p43 = p4 - p3; vec3 p21 = p2 - p1; float d1343 = dot( p13, p43 ); float d4321 = dot( p43, p21 ); float d1321 = dot( p13, p21 ); float d4343 = dot( p43, p43 ); float d2121 = dot( p21, p21 ); float denom = d2121 * d4343 - d4321 * d4321; float numer = d1343 * d4321 - d1321 * d4343; mua = numer / denom; mua = clamp( mua, 0.0, 1.0 ); mub = ( d1343 + d4321 * ( mua ) ) / d4343; mub = clamp( mub, 0.0, 1.0 ); return vec2( mua, mub ); } void main() { #include /*#ifdef USE_DASH if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps if ( mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize ) discard; // todo - FIX #endif*/ #ifdef USE_DASH if ( vUv.y < - 1.0 || vUv.y > 1.0 ) discard; // discard endcaps bool unvisible = mod( vLineDistance + dashOffset, dashSize + gapSize ) > dashSize; //加 #ifdef DASH_with_depth #else if (unvisible) discard; // todo - FIX #endif #endif float alpha = opacity; #ifdef WORLD_UNITS // Find the closest points on the view ray and the line segment vec3 rayEnd = normalize( worldPos.xyz ) * 1e5; vec3 lineDir = worldEnd - worldStart; vec2 params = closestLineToLine( worldStart, worldEnd, vec3( 0.0, 0.0, 0.0 ), rayEnd ); vec3 p1 = worldStart + lineDir * params.x; vec3 p2 = rayEnd * params.y; vec3 delta = p1 - p2; float len = length( delta ); float norm = len / lineWidth; #ifndef USE_DASH #ifdef USE_ALPHA_TO_COVERAGE float dnorm = fwidth( norm ); alpha = 1.0 - smoothstep( 0.5 - dnorm, 0.5 + dnorm, norm ); #else if ( norm > 0.5 ) { discard; } #endif #endif #else #ifdef USE_ALPHA_TO_COVERAGE // artifacts appear on some hardware if a derivative is taken within a conditional float a = vUv.x; float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0; float len2 = a * a + b * b; float dlen = fwidth( len2 ); if ( abs( vUv.y ) > 1.0 ) { alpha = 1.0 - smoothstep( 1.0 - dlen, 1.0 + dlen, len2 ); } #else if ( abs( vUv.y ) > 1.0 ) { float a = vUv.x; float b = ( vUv.y > 0.0 ) ? vUv.y - 1.0 : vUv.y + 1.0; float len2 = a * a + b * b; if ( len2 > 1.0 ) discard; } #endif #endif vec4 diffuseColor = vec4( diffuse, alpha ); //加 #if defined(GL_EXT_frag_depth) && defined(useDepth) float mixFactor = 0.0; float clipFactor = 0.0; float fragDepth = convertToLinear(gl_FragCoord.z); //gl_FragCoord大小为 viewport client大小 vec2 depthTxtCoords = vec2(gl_FragCoord.x - viewportOffset.x, gl_FragCoord.y - viewportOffset.y) / resolution; float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r); float delta = fragDepth - textureDepth; if (delta > 0.0) { mixFactor = clamp(delta / occlusionDistance, 0.0, maxOcclusionFactor); clipFactor = clamp(delta / clipDistance, 0.0, maxClipFactor); } if (clipFactor == 1.0) { discard; } vec4 backColor_ = vec4(backColor, opacity); //vec4(0.8,0.8,0.8, 0.8*opacity); #ifdef DASH_with_depth // 只在被遮住的部分显示虚线, 所以若同时是虚线不可见部分和被遮住时, a为0 if(unvisible) backColor_.a = 0.0; #endif //vec4 diffuseColor = vec4(mix(diffuse, backColor_, mixFactor), opacity*(1.0 - clipFactor)); diffuseColor = mix(diffuseColor, backColor_ , mixFactor); diffuseColor.a *= (1.0 - clipFactor); #endif #include #include //gl_FragColor = vec4( diffuseColor.rgb, alpha ); gl_FragColor = vec4( diffuseColor.rgb, diffuseColor.a ); #include #include #include #include } ` }; class LineMaterial extends ShaderMaterial { constructor( parameters ) { let {vs,fs} = Potree.Common.changeShaderToWebgl2(ShaderLib[ 'line' ].vertexShader, ShaderLib[ 'line' ].fragmentShader, 'ShaderMaterial'); super( { type: 'LineMaterial', uniforms: UniformsUtils.clone( ShaderLib[ 'line' ].uniforms ), vertexShader: vs, fragmentShader: fs, clipping: true // required for clipping support } ); this.isLineMaterial = true; this.lineWidth_ = 0; this.supportExtDepth = parameters.supportExtDepth; this.depthTestWhenPick = false; //pick时是否识别点云等 if(parameters.color){ this.color = new Color(parameters.color); } if(parameters.backColor){ this.uniforms.backColor.value = new Color(parameters.backColor); } if(parameters.clipDistance){ this.uniforms.clipDistance.value = parameters.clipDistance; } if(parameters.occlusionDistance){ this.uniforms.occlusionDistance.value = parameters.occlusionDistance; } if(parameters.maxClipFactor){ this.uniforms.maxClipFactor.value = parameters.maxClipFactor; } Object.defineProperties( this, { color: { enumerable: true, get: function () { return this.uniforms.diffuse.value; }, set: function ( value ) { this.uniforms.diffuse.value = value; } }, worldUnits: { enumerable: true, get: function () { return 'WORLD_UNITS' in this.defines; }, set: function ( value ) { if ( value === true ) { this.defines.WORLD_UNITS = ''; } else { delete this.defines.WORLD_UNITS; } } }, lineWidth: { enumerable: true, get: function () { return this.lineWidth_;//this.uniforms.lineWidth.value; }, set: function ( value ) { this.uniforms.lineWidth.value = value * window.devicePixelRatio; this.lineWidth_ = value; } }, dashed: { enumerable: true, get: function () { return Boolean( 'USE_DASH' in this.defines ); }, set( value ) { if ( Boolean( value ) !== Boolean( 'USE_DASH' in this.defines ) ) { this.needsUpdate = true; } if ( value === true ) { this.defines.USE_DASH = ''; } else { delete this.defines.USE_DASH; } } }, dashScale: { enumerable: true, get: function () { return this.uniforms.dashScale.value; }, set: function ( value ) { this.uniforms.dashScale.value = value; } }, dashSize: { enumerable: true, get: function () { return this.uniforms.dashSize.value; }, set: function ( value ) { this.uniforms.dashSize.value = value; } }, dashOffset: { enumerable: true, get: function () { return this.uniforms.dashOffset.value; }, set: function ( value ) { this.uniforms.dashOffset.value = value; } }, gapSize: { enumerable: true, get: function () { return this.uniforms.gapSize.value; }, set: function ( value ) { this.uniforms.gapSize.value = value; } }, opacity: { enumerable: true, get: function () { return this.uniforms.opacity.value; }, set: function ( value ) { this.uniforms.opacity.value = value; } }, resolution: { enumerable: true, get: function () { return this.uniforms.resolution.value; }, set: function ( value ) { this.uniforms.resolution.value.copy( value ); } }, alphaToCoverage: { enumerable: true, get: function () { return Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines ); }, set: function ( value ) { if ( Boolean( value ) !== Boolean( 'USE_ALPHA_TO_COVERAGE' in this.defines ) ) { this.needsUpdate = true; } if ( value === true ) { this.defines.USE_ALPHA_TO_COVERAGE = ''; this.extensions.derivatives = true; } else { delete this.defines.USE_ALPHA_TO_COVERAGE; this.extensions.derivatives = false; } } }, dashWithDepth:{//add enumerable: true, get: function () { return 'DASH_with_depth' in this.defines }, set: function ( value ) { value = value && !!this.supportExtDepth; if(value != this.dashWithDepth){ if(value){ this.defines.DASH_with_depth = ''; }else { delete this.defines.DASH_with_depth; } this.needsUpdate = true; } } }, } ); this.events = { setSize:(e)=>{//如果出现横条状的异常,往往是viewportOffset出错 //地图不需要 let viewport = e.viewport; //console.log(viewport.name, viewport.resolution2) this.uniforms.resolution.value.copy(viewport.resolution2); this.uniforms.devicePixelRatio.value = window.devicePixelRatio; this.lineWidth = this.lineWidth_; //update if(!this.realUseDepth || !e.viewport)return let viewportOffset = viewport.offset || new THREE.Vector2(); this.uniforms.viewportOffset.value.copy(viewportOffset); }, render:(e)=>{//before render 如果有大于两个viewport的话,不同viewport用不同的depthTex this.useDepth && this.updateDepthParams(e); var viewport = e.viewport || viewer.mainViewport; if(viewport != this.lastViewport){ //当mapViewer要渲染测量线后,就需要变viewport this.events.setSize({viewport}); } this.lastViewport = viewport; } }; this.setValues( parameters ); let viewport = viewer.mainViewport; this.events.setSize({viewport}); viewer.addEventListener('resize', this.events.setSize); viewer.addEventListener("render.begin", this.events.render); } get useDepth(){ return this.useDepth_ } set useDepth(value){ value = value && this.supportExtDepth; //如果不支持 EXT_DEPTH 的话会失效 if(this.useDepth_ != value){ this.setRealDepth(value); this.useDepth_ = value; } } setRealDepth(useDepth, viewport){//确实使用到depthTex if(this.realUseDepth != useDepth){ if(useDepth ){ this.defines.useDepth = ''; }else { delete this.defines.useDepth; } this.realUseDepth = useDepth; if(this.autoDepthTest)this.depthWrite = this.depthTest = !useDepth; //如果useDepth = false,使用原始的depthTest this.needsUpdate = true; if(!viewport)viewport = viewer.mainViewport; //暂时这么设置 useDepth && this.events.setSize({viewport}); } } updateDepthParams(e={}){ var viewport = e.viewport || viewer.mainViewport; var camera = viewport.camera; let hasDepth = this.useDepth && camera.isPerspectiveCamera && (Potree.settings.pointEnableRT || Potree.settings.displayMode == 'showPanos' || viewer.useEDL); this.setRealDepth(hasDepth, viewport); if(hasDepth){ this.uniforms.depthTexture.value = viewer.getPRenderer().getRtEDL(viewport).depthTexture; //其实只赋值一次就行 this.uniforms.nearPlane.value = camera.near; this.uniforms.farPlane.value = camera.far; } //this.uniforms.uUseOrthographicCamera.value = !camera.isPerspectiveCamera } } const _start$2 = new Vector3(); const _end$2 = new Vector3(); const _start4 = new Vector4(); const _end4 = new Vector4(); const _ssOrigin = new Vector4(); const _ssOrigin3 = new Vector3(); const _mvMatrix = new Matrix4(); const _line = new Line3(); const _closestPoint = new Vector3(); const _box$5 = new Box3(); const _sphere$4 = new Sphere(); const _clipToWorldVector = new Vector4(); let _ray$3, _instanceStart, _instanceEnd, _lineWidth; // Returns the margin required to expand by in world space given the distance from the camera, // line width, resolution, and camera projection function getWorldSpaceHalfWidth( camera, distance, resolution ) { // transform into clip space, adjust the x and y values by the pixel width offset, then // transform back into world space to get world offset. Note clip space is [-1, 1] so full // width does not need to be halved. _clipToWorldVector.set( 0, 0, - distance, 1.0 ).applyMatrix4( camera.projectionMatrix ); _clipToWorldVector.multiplyScalar( 1.0 / _clipToWorldVector.w ); _clipToWorldVector.x = _lineWidth / resolution.width; _clipToWorldVector.y = _lineWidth / resolution.height; _clipToWorldVector.applyMatrix4( camera.projectionMatrixInverse ); _clipToWorldVector.multiplyScalar( 1.0 / _clipToWorldVector.w ); return Math.abs( Math.max( _clipToWorldVector.x, _clipToWorldVector.y ) ); } function raycastWorldUnits( lineSegments, intersects ) { for ( let i = 0, l = _instanceStart.count; i < l; i ++ ) { _line.start.fromBufferAttribute( _instanceStart, i ); _line.end.fromBufferAttribute( _instanceEnd, i ); const pointOnLine = new Vector3(); const point = new Vector3(); _ray$3.distanceSqToSegment( _line.start, _line.end, point, pointOnLine ); const isInside = point.distanceTo( pointOnLine ) < _lineWidth * 0.5; if ( isInside ) { intersects.push( { point, pointOnLine, distance: _ray$3.origin.distanceTo( point ), object: lineSegments, face: null, faceIndex: i, uv: null, uv2: null, } ); } } } function raycastScreenSpace( lineSegments, camera, intersects ) { const projectionMatrix = camera.projectionMatrix; const material = lineSegments.material; const resolution = material.resolution; const matrixWorld = lineSegments.matrixWorld; const geometry = lineSegments.geometry; const instanceStart = geometry.attributes.instanceStart; const instanceEnd = geometry.attributes.instanceEnd; const near = - camera.near; // // pick a point 1 unit out along the ray to avoid the ray origin // sitting at the camera origin which will cause "w" to be 0 when // applying the projection matrix. _ray$3.at( 1, _ssOrigin ); // ndc space [ - 1.0, 1.0 ] _ssOrigin.w = 1; _ssOrigin.applyMatrix4( camera.matrixWorldInverse ); _ssOrigin.applyMatrix4( projectionMatrix ); _ssOrigin.multiplyScalar( 1 / _ssOrigin.w ); // screen space _ssOrigin.x *= resolution.x / 2; _ssOrigin.y *= resolution.y / 2; _ssOrigin.z = 0; _ssOrigin3.copy( _ssOrigin ); _mvMatrix.multiplyMatrices( camera.matrixWorldInverse, matrixWorld ); for ( let i = 0, l = instanceStart.count; i < l; i ++ ) { _start4.fromBufferAttribute( instanceStart, i ); _end4.fromBufferAttribute( instanceEnd, i ); _start4.w = 1; _end4.w = 1; // camera space _start4.applyMatrix4( _mvMatrix ); _end4.applyMatrix4( _mvMatrix ); // skip the segment if it's entirely behind the camera const isBehindCameraNear = _start4.z > near && _end4.z > near; if ( isBehindCameraNear ) { continue; } // trim the segment if it extends behind camera near if ( _start4.z > near ) { const deltaDist = _start4.z - _end4.z; const t = ( _start4.z - near ) / deltaDist; _start4.lerp( _end4, t ); } else if ( _end4.z > near ) { const deltaDist = _end4.z - _start4.z; const t = ( _end4.z - near ) / deltaDist; _end4.lerp( _start4, t ); } // clip space _start4.applyMatrix4( projectionMatrix ); _end4.applyMatrix4( projectionMatrix ); // ndc space [ - 1.0, 1.0 ] _start4.multiplyScalar( 1 / _start4.w ); _end4.multiplyScalar( 1 / _end4.w ); // screen space _start4.x *= resolution.x / 2; _start4.y *= resolution.y / 2; _end4.x *= resolution.x / 2; _end4.y *= resolution.y / 2; // create 2d segment _line.start.copy( _start4 ); _line.start.z = 0; _line.end.copy( _end4 ); _line.end.z = 0; // get closest point on ray to segment const param = _line.closestPointToPointParameter( _ssOrigin3, true ); _line.at( param, _closestPoint ); // check if the intersection point is within clip space const zPos = MathUtils.lerp( _start4.z, _end4.z, param ); const isInClipSpace = zPos >= - 1 && zPos <= 1; const isInside = _ssOrigin3.distanceTo( _closestPoint ) < _lineWidth * 0.5; if ( isInClipSpace && isInside ) { _line.start.fromBufferAttribute( instanceStart, i ); _line.end.fromBufferAttribute( instanceEnd, i ); _line.start.applyMatrix4( matrixWorld ); _line.end.applyMatrix4( matrixWorld ); const pointOnLine = new Vector3(); const point = new Vector3(); _ray$3.distanceSqToSegment( _line.start, _line.end, point, pointOnLine ); intersects.push( { point: point, pointOnLine: pointOnLine, distance: _ray$3.origin.distanceTo( point ), object: lineSegments, face: null, faceIndex: i, uv: null, uv2: null, } ); } } } class LineSegments2 extends Mesh { constructor( geometry = new LineSegmentsGeometry(), material = new LineMaterial( { color: Math.random() * 0xffffff } ) ) { super( geometry, material ); this.isLineSegments2 = true; this.type = 'LineSegments2'; } // for backwards-compatibility, but could be a method of LineSegmentsGeometry... computeLineDistances() { const geometry = this.geometry; const instanceStart = geometry.attributes.instanceStart; const instanceEnd = geometry.attributes.instanceEnd; const lineDistances = new Float32Array( 2 * instanceStart.count ); for ( let i = 0, j = 0, l = instanceStart.count; i < l; i ++, j += 2 ) { _start$2.fromBufferAttribute( instanceStart, i ); _end$2.fromBufferAttribute( instanceEnd, i ); lineDistances[ j ] = ( j === 0 ) ? 0 : lineDistances[ j - 1 ]; lineDistances[ j + 1 ] = lineDistances[ j ] + _start$2.distanceTo( _end$2 ); } const instanceDistanceBuffer = new InstancedInterleavedBuffer( lineDistances, 2, 1 ); // d0, d1 geometry.setAttribute( 'instanceDistanceStart', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 0 ) ); // d0 geometry.setAttribute( 'instanceDistanceEnd', new InterleavedBufferAttribute( instanceDistanceBuffer, 1, 1 ) ); // d1 return this; } raycast( raycaster, intersects ) { const worldUnits = this.material.worldUnits; const camera = raycaster.camera; if ( camera === null && ! worldUnits ) { console.error( 'LineSegments2: "Raycaster.camera" needs to be set in order to raycast against LineSegments2 while worldUnits is set to false.' ); } const threshold = ( raycaster.params.Line2 !== undefined ) ? raycaster.params.Line2.threshold || 0 : 0; _ray$3 = raycaster.ray; const matrixWorld = this.matrixWorld; const geometry = this.geometry; const material = this.material; _lineWidth = material.lineWidth + threshold; _instanceStart = geometry.attributes.instanceStart; _instanceEnd = geometry.attributes.instanceEnd; // check if we intersect the sphere bounds if ( geometry.boundingSphere === null ) { geometry.computeBoundingSphere(); } _sphere$4.copy( geometry.boundingSphere ).applyMatrix4( matrixWorld ); // increase the sphere bounds by the worst case line screen space width let sphereMargin; if ( worldUnits ) { sphereMargin = _lineWidth * 0.5; } else { const distanceToSphere = Math.max( camera.near, _sphere$4.distanceToPoint( _ray$3.origin ) ); sphereMargin = getWorldSpaceHalfWidth( camera, distanceToSphere, material.resolution ); } _sphere$4.radius += sphereMargin; if ( _ray$3.intersectsSphere( _sphere$4 ) === false ) { return; } // check if we intersect the box bounds if ( geometry.boundingBox === null ) { geometry.computeBoundingBox(); } _box$5.copy( geometry.boundingBox ).applyMatrix4( matrixWorld ); // increase the box bounds by the worst case line width let boxMargin; if ( worldUnits ) { boxMargin = _lineWidth * 0.5; } else { const distanceToBox = Math.max( camera.near, _box$5.distanceToPoint( _ray$3.origin ) ); boxMargin = getWorldSpaceHalfWidth( camera, distanceToBox, material.resolution ); } _box$5.expandByScalar( boxMargin ); if ( _ray$3.intersectsBox( _box$5 ) === false ) { return; } if ( worldUnits ) { raycastWorldUnits( this, intersects ); } else { raycastScreenSpace( this, camera, intersects ); } } } class LineGeometry extends LineSegmentsGeometry { constructor() { super(); this.isLineGeometry = true; this.type = 'LineGeometry'; } setPositions( array ) {//见potree.shim.js // converts [ x1, y1, z1, x2, y2, z2, ... ] to pairs format const length = array.length - 3; const points = new Float32Array( 2 * length ); for ( let i = 0; i < length; i += 3 ) { points[ 2 * i ] = array[ i ]; points[ 2 * i + 1 ] = array[ i + 1 ]; points[ 2 * i + 2 ] = array[ i + 2 ]; points[ 2 * i + 3 ] = array[ i + 3 ]; points[ 2 * i + 4 ] = array[ i + 4 ]; points[ 2 * i + 5 ] = array[ i + 5 ]; } super.setPositions( points ); return this; } setColors( array ) { // converts [ r1, g1, b1, r2, g2, b2, ... ] to pairs format const length = array.length - 3; const colors = new Float32Array( 2 * length ); for ( let i = 0; i < length; i += 3 ) { colors[ 2 * i ] = array[ i ]; colors[ 2 * i + 1 ] = array[ i + 1 ]; colors[ 2 * i + 2 ] = array[ i + 2 ]; colors[ 2 * i + 3 ] = array[ i + 3 ]; colors[ 2 * i + 4 ] = array[ i + 4 ]; colors[ 2 * i + 5 ] = array[ i + 5 ]; } super.setColors( colors ); return this; } fromLine( line ) { const geometry = line.geometry; this.setPositions( geometry.attributes.position.array ); // assumes non-indexed // set colors, maybe return this; } } class Line2 extends LineSegments2 { constructor( geometry = new LineGeometry(), material = new LineMaterial( { color: Math.random() * 0xffffff } ) ) { super( geometry, material ); this.isLine2 = true; this.type = 'Line2'; } } //old function createHeightLine(){ let lineGeometry = new LineGeometry(); lineGeometry.setPositions([ 0, 0, 0, 0, 0, 0, ]); let lineMaterial = new LineMaterial({ color: 0x00ff00, dashSize: 5, gapSize: 2, linewidth: 2, resolution: new Vector2(1000, 1000), }); lineMaterial.depthTest = false; const heightEdge = new Line2(lineGeometry, lineMaterial); heightEdge.visible = false; //this.add(this.heightEdge); return heightEdge; } function createHeightLabel(){ const heightLabel = new TextSprite$1(''); heightLabel.setTextColor({r: 140, g: 250, b: 140, a: 1.0}); heightLabel.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); heightLabel.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); heightLabel.fontsize = 16; heightLabel.material.depthTest = false; heightLabel.material.opacity = 1; heightLabel.visible = false; return heightLabel; } function createAreaLabel(){ const areaLabel = new TextSprite$1(''); areaLabel.setTextColor({r: 140, g: 250, b: 140, a: 1.0}); areaLabel.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); areaLabel.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); areaLabel.fontsize = 16; areaLabel.material.depthTest = false; areaLabel.material.opacity = 1; areaLabel.visible = false; return areaLabel; } function createCircleRadiusLabel(){ const circleRadiusLabel = new TextSprite$1(""); circleRadiusLabel.setTextColor({r: 140, g: 250, b: 140, a: 1.0}); circleRadiusLabel.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); circleRadiusLabel.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); circleRadiusLabel.fontsize = 16; circleRadiusLabel.material.depthTest = false; circleRadiusLabel.material.opacity = 1; circleRadiusLabel.visible = false; return circleRadiusLabel; } function createCircleRadiusLine(){ const lineGeometry = new LineGeometry(); lineGeometry.setPositions([ 0, 0, 0, 0, 0, 0, ]); const lineMaterial = new LineMaterial({ color: 0xff0000, linewidth: 2, resolution: new Vector2(1000, 1000), gapSize: 1, dashed: true, }); lineMaterial.depthTest = false; const circleRadiusLine = new Line2(lineGeometry, lineMaterial); circleRadiusLine.visible = false; return circleRadiusLine; } function createCircleLine(){ const coordinates = []; let n = 128; for(let i = 0; i <= n; i++){ let u0 = 2 * Math.PI * (i / n); let u1 = 2 * Math.PI * (i + 1) / n; let p0 = new Vector3( Math.cos(u0), Math.sin(u0), 0 ); let p1 = new Vector3( Math.cos(u1), Math.sin(u1), 0 ); coordinates.push( ...p0.toArray(), ...p1.toArray(), ); } const geometry = new LineGeometry(); geometry.setPositions(coordinates); const material = new LineMaterial({ color: 0xff0000, dashSize: 5, gapSize: 2, linewidth: 2, resolution: new Vector2(1000, 1000), }); material.depthTest = false; const circleLine = new Line2(geometry, material); circleLine.visible = false; circleLine.computeLineDistances(); return circleLine; } function createCircleCenter(){ const sg = new SphereGeometry(1, 32, 32); const sm = new MeshNormalMaterial(); const circleCenter = new Mesh(sg, sm); circleCenter.visible = false; return circleCenter; } function createLine(){ const geometry = new LineGeometry(); geometry.setPositions([ 0, 0, 0, 0, 0, 0, ]); const material = new LineMaterial({ color: 0xff0000, linewidth: 2, resolution: new Vector2(1000, 1000), gapSize: 1, dashed: true, }); material.depthTest = false; const line = new Line2(geometry, material); return line; } function createCircle(){ const coordinates = []; let n = 128; for(let i = 0; i <= n; i++){ let u0 = 2 * Math.PI * (i / n); let u1 = 2 * Math.PI * (i + 1) / n; let p0 = new Vector3( Math.cos(u0), Math.sin(u0), 0 ); let p1 = new Vector3( Math.cos(u1), Math.sin(u1), 0 ); coordinates.push( ...p0.toArray(), ...p1.toArray(), ); } const geometry = new LineGeometry(); geometry.setPositions(coordinates); const material = new LineMaterial({ color: 0xff0000, dashSize: 5, gapSize: 2, linewidth: 2, resolution: new Vector2(1000, 1000), }); material.depthTest = false; const line = new Line2(geometry, material); line.computeLineDistances(); return line; } function createAzimuth(){ const azimuth = { label: null, center: null, target: null, north: null, centerToNorth: null, centerToTarget: null, centerToTargetground: null, targetgroundToTarget: null, circle: null, node: null, }; const sg = new SphereGeometry(1, 32, 32); const sm = new MeshNormalMaterial(); { const label = new TextSprite$1(""); label.setTextColor({r: 140, g: 250, b: 140, a: 1.0}); label.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); label.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); label.fontsize = 16; label.material.depthTest = false; label.material.opacity = 1; azimuth.label = label; } azimuth.center = new Mesh(sg, sm); azimuth.target = new Mesh(sg, sm); azimuth.north = new Mesh(sg, sm); azimuth.centerToNorth = createLine(); azimuth.centerToTarget = createLine(); azimuth.centerToTargetground = createLine(); azimuth.targetgroundToTarget = createLine(); azimuth.circle = createCircle(); azimuth.node = new Object3D(); azimuth.node.add( azimuth.centerToNorth, azimuth.centerToTarget, azimuth.centerToTargetground, azimuth.targetgroundToTarget, azimuth.circle, azimuth.label, azimuth.center, azimuth.target, azimuth.north, ); return azimuth; } class Measure extends Object3D { constructor () { super(); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = 'Measure_' + this.constructor.counter; this.points = []; this._showDistances = true; this._showCoordinates = false; this._showArea = false; this._closed = true; this._showAngles = false; this._showCircle = false; this._showHeight = false; this._showEdges = true; this._showAzimuth = false; this.maxMarkers = Number.MAX_SAFE_INTEGER; this.sphereGeometry = new SphereGeometry(0.4, 10, 10); this.color = new Color(0xff0000); this.spheres = []; this.edges = []; this.sphereLabels = []; this.edgeLabels = []; this.angleLabels = []; this.coordinateLabels = []; this.heightEdge = createHeightLine(); this.heightLabel = createHeightLabel(); this.areaLabel = createAreaLabel(); this.circleRadiusLabel = createCircleRadiusLabel(); this.circleRadiusLine = createCircleRadiusLine(); this.circleLine = createCircleLine(); this.circleCenter = createCircleCenter(); this.azimuth = createAzimuth(); this.add(this.heightEdge); this.add(this.heightLabel); this.add(this.areaLabel); this.add(this.circleRadiusLabel); this.add(this.circleRadiusLine); this.add(this.circleLine); this.add(this.circleCenter); this.add(this.azimuth.node); } createSphereMaterial () { let sphereMaterial = new MeshLambertMaterial({ //shading: THREE.SmoothShading, color: this.color, depthTest: false, depthWrite: false} ); return sphereMaterial; }; addMarker (point) { if (point.x != null) { point = {position: point}; }else if(point instanceof Array){ point = {position: new Vector3(...point)}; } this.points.push(point); // sphere let sphere = new Mesh(this.sphereGeometry, this.createSphereMaterial()); this.add(sphere); this.spheres.push(sphere); { // edges let lineGeometry = new LineGeometry(); lineGeometry.setPositions( [ 0, 0, 0, 0, 0, 0, ]); let lineMaterial = new LineMaterial({ color: 0xff0000, linewidth: 2, resolution: new Vector2(1000, 1000), }); lineMaterial.depthTest = false; let edge = new Line2(lineGeometry, lineMaterial); edge.visible = true; this.add(edge); this.edges.push(edge); } { // edge labels let edgeLabel = new TextSprite$1(); edgeLabel.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); edgeLabel.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); edgeLabel.material.depthTest = false; edgeLabel.visible = false; edgeLabel.fontsize = 16; this.edgeLabels.push(edgeLabel); this.add(edgeLabel); } { // angle labels let angleLabel = new TextSprite$1(); angleLabel.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); angleLabel.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); angleLabel.fontsize = 16; angleLabel.material.depthTest = false; angleLabel.material.opacity = 1; angleLabel.visible = false; this.angleLabels.push(angleLabel); this.add(angleLabel); } { // coordinate labels let coordinateLabel = new TextSprite$1(); coordinateLabel.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); coordinateLabel.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); coordinateLabel.fontsize = 16; coordinateLabel.material.depthTest = false; coordinateLabel.material.opacity = 1; coordinateLabel.visible = false; this.coordinateLabels.push(coordinateLabel); this.add(coordinateLabel); } { // Event Listeners let drag = (e) => { let I = Utils.getMousePointCloudIntersection( e.drag.end, e.viewer.scene.getActiveCamera(), e.viewer, e.viewer.scene.pointclouds, {pickClipped: true}); if (I) { let i = this.spheres.indexOf(e.drag.object); if (i !== -1) { let point = this.points[i]; // loop through current keys and cleanup ones that will be orphaned for (let key of Object.keys(point)) { if (!I.point[key]) { delete point[key]; } } for (let key of Object.keys(I.point).filter(e => e !== 'position')) { point[key] = I.point[key]; } this.setPosition(i, I.location); } } }; let drop = e => { let i = this.spheres.indexOf(e.drag.object); if (i !== -1) { this.dispatchEvent({ 'type': 'marker_dropped', 'measurement': this, 'index': i }); } }; let mouseover = (e) => e.object.material.emissive.setHex(0x888888); let mouseleave = (e) => e.object.material.emissive.setHex(0x000000); sphere.addEventListener('drag', drag); sphere.addEventListener('drop', drop); sphere.addEventListener('mouseover', mouseover); sphere.addEventListener('mouseleave', mouseleave); } let event = { type: 'marker_added', measurement: this, sphere: sphere }; this.dispatchEvent(event); this.setMarker(this.points.length - 1, point); }; removeMarker (index) { this.points.splice(index, 1); this.remove(this.spheres[index]); let edgeIndex = (index === 0) ? 0 : (index - 1); this.remove(this.edges[edgeIndex]); this.edges.splice(edgeIndex, 1); this.remove(this.edgeLabels[edgeIndex]); this.edgeLabels.splice(edgeIndex, 1); this.coordinateLabels.splice(index, 1); this.remove(this.angleLabels[index]); this.angleLabels.splice(index, 1); this.spheres.splice(index, 1); this.update(); this.dispatchEvent({type: 'marker_removed', measurement: this}); }; setMarker (index, point) { this.points[index] = point; let event = { type: 'marker_moved', measure: this, index: index, position: point.position.clone() }; this.dispatchEvent(event); this.update(); } setPosition (index, position) { let point = this.points[index]; point.position.copy(position); let event = { type: 'marker_moved', measure: this, index: index, position: position.clone() }; this.dispatchEvent(event); this.update(); }; getArea () { let area = 0; let j = this.points.length - 1; for (let i = 0; i < this.points.length; i++) { let p1 = this.points[i].position; let p2 = this.points[j].position; area += (p2.x + p1.x) * (p1.y - p2.y); j = i; } return Math.abs(area / 2); }; getTotalDistance () { if (this.points.length === 0) { return 0; } let distance = 0; for (let i = 1; i < this.points.length; i++) { let prev = this.points[i - 1].position; let curr = this.points[i].position; let d = prev.distanceTo(curr); distance += d; } if (this.closed && this.points.length > 1) { let first = this.points[0].position; let last = this.points[this.points.length - 1].position; let d = last.distanceTo(first); distance += d; } return distance; } getAngleBetweenLines (cornerPoint, point1, point2) { let v1 = new Vector3().subVectors(point1.position, cornerPoint.position); let v2 = new Vector3().subVectors(point2.position, cornerPoint.position); // avoid the error printed by threejs if denominator is 0 const denominator = Math.sqrt( v1.lengthSq() * v2.lengthSq() ); if(denominator === 0){ return 0; }else { return v1.angleTo(v2); } }; getAngle (index) { if (this.points.length < 3 || index >= this.points.length) { return 0; } let previous = (index === 0) ? this.points[this.points.length - 1] : this.points[index - 1]; let point = this.points[index]; let next = this.points[(index + 1) % (this.points.length)]; return this.getAngleBetweenLines(point, previous, next); } // updateAzimuth(){ // // if(this.points.length !== 2){ // // return; // // } // // const azimuth = this.azimuth; // // const [p0, p1] = this.points; // // const r = p0.position.distanceTo(p1.position); // } update () { if (this.points.length === 0) { return; } else if (this.points.length === 1) { let point = this.points[0]; let position = point.position; this.spheres[0].position.copy(position); { // coordinate labels let coordinateLabel = this.coordinateLabels[0]; let msg = position.toArray().map(p => Utils.addCommas(p.toFixed(2))).join(" / "); coordinateLabel.setText(msg); coordinateLabel.visible = this.showCoordinates; } return; } let lastIndex = this.points.length - 1; let centroid = new Vector3(); for (let i = 0; i <= lastIndex; i++) { let point = this.points[i]; centroid.add(point.position); } centroid.divideScalar(this.points.length); for (let i = 0; i <= lastIndex; i++) { let index = i; let nextIndex = (i + 1 > lastIndex) ? 0 : i + 1; let previousIndex = (i === 0) ? lastIndex : i - 1; let point = this.points[index]; let nextPoint = this.points[nextIndex]; let previousPoint = this.points[previousIndex]; let sphere = this.spheres[index]; // spheres sphere.position.copy(point.position); sphere.material.color = this.color; { // edges let edge = this.edges[index]; edge.material.color = this.color; edge.position.copy(point.position); edge.geometry.setPositions([ 0, 0, 0, ...nextPoint.position.clone().sub(point.position).toArray(), ]); edge.geometry.verticesNeedUpdate = true; edge.geometry.computeBoundingSphere(); edge.computeLineDistances(); edge.visible = index < lastIndex || this.closed; if(!this.showEdges){ edge.visible = false; } } { // edge labels let edgeLabel = this.edgeLabels[i]; let center = new Vector3().add(point.position); center.add(nextPoint.position); center = center.multiplyScalar(0.5); let distance = point.position.distanceTo(nextPoint.position); edgeLabel.position.copy(center); let suffix = ""; if(this.lengthUnit != null && this.lengthUnitDisplay != null){ distance = distance / this.lengthUnit.unitspermeter * this.lengthUnitDisplay.unitspermeter; //convert to meters then to the display unit suffix = this.lengthUnitDisplay.code; } let txtLength = Utils.addCommas(distance.toFixed(2)); edgeLabel.setText(`${txtLength} ${suffix}`); edgeLabel.visible = this.showDistances && (index < lastIndex || this.closed) && this.points.length >= 2 && distance > 0; } { // angle labels let angleLabel = this.angleLabels[i]; let angle = this.getAngleBetweenLines(point, previousPoint, nextPoint); let dir = nextPoint.position.clone().sub(previousPoint.position); dir.multiplyScalar(0.5); dir = previousPoint.position.clone().add(dir).sub(point.position).normalize(); let dist = Math.min(point.position.distanceTo(previousPoint.position), point.position.distanceTo(nextPoint.position)); dist = dist / 9; let labelPos = point.position.clone().add(dir.multiplyScalar(dist)); angleLabel.position.copy(labelPos); let msg = Utils.addCommas((angle * (180.0 / Math.PI)).toFixed(1)) + '\u00B0'; angleLabel.setText(msg); angleLabel.visible = this.showAngles && (index < lastIndex || this.closed) && this.points.length >= 3 && angle > 0; } } { // update height stuff let heightEdge = this.heightEdge; heightEdge.visible = this.showHeight; this.heightLabel.visible = this.showHeight; if (this.showHeight) { let sorted = this.points.slice().sort((a, b) => a.position.z - b.position.z); let lowPoint = sorted[0].position.clone(); let highPoint = sorted[sorted.length - 1].position.clone(); let min = lowPoint.z; let max = highPoint.z; let height = max - min; let start = new Vector3(highPoint.x, highPoint.y, min); let end = new Vector3(highPoint.x, highPoint.y, max); heightEdge.position.copy(lowPoint); heightEdge.geometry.setPositions([ 0, 0, 0, ...start.clone().sub(lowPoint).toArray(), ...start.clone().sub(lowPoint).toArray(), ...end.clone().sub(lowPoint).toArray(), ]); heightEdge.geometry.verticesNeedUpdate = true; // heightEdge.geometry.computeLineDistances(); // heightEdge.geometry.lineDistancesNeedUpdate = true; heightEdge.geometry.computeBoundingSphere(); heightEdge.computeLineDistances(); // heightEdge.material.dashSize = height / 40; // heightEdge.material.gapSize = height / 40; let heightLabelPosition = start.clone().add(end).multiplyScalar(0.5); this.heightLabel.position.copy(heightLabelPosition); let suffix = ""; if(this.lengthUnit != null && this.lengthUnitDisplay != null){ height = height / this.lengthUnit.unitspermeter * this.lengthUnitDisplay.unitspermeter; //convert to meters then to the display unit suffix = this.lengthUnitDisplay.code; } let txtHeight = Utils.addCommas(height.toFixed(2)); let msg = `${txtHeight} ${suffix}`; this.heightLabel.setText(msg); } } { // update circle stuff const circleRadiusLabel = this.circleRadiusLabel; const circleRadiusLine = this.circleRadiusLine; const circleLine = this.circleLine; const circleCenter = this.circleCenter; const circleOkay = this.points.length === 3; circleRadiusLabel.visible = this.showCircle && circleOkay; circleRadiusLine.visible = this.showCircle && circleOkay; circleLine.visible = this.showCircle && circleOkay; circleCenter.visible = this.showCircle && circleOkay; if(this.showCircle && circleOkay){ const A = this.points[0].position; const B = this.points[1].position; const C = this.points[2].position; const AB = B.clone().sub(A); const AC = C.clone().sub(A); const N = AC.clone().cross(AB).normalize(); const center = Potree.Utils.computeCircleCenter(A, B, C); const radius = center.distanceTo(A); const scale = radius / 20; circleCenter.position.copy(center); circleCenter.scale.set(scale, scale, scale); //circleRadiusLine.geometry.vertices[0].set(0, 0, 0); //circleRadiusLine.geometry.vertices[1].copy(B.clone().sub(center)); circleRadiusLine.geometry.setPositions( [ 0, 0, 0, ...B.clone().sub(center).toArray() ] ); circleRadiusLine.geometry.verticesNeedUpdate = true; circleRadiusLine.geometry.computeBoundingSphere(); circleRadiusLine.position.copy(center); circleRadiusLine.computeLineDistances(); const target = center.clone().add(N); circleLine.position.copy(center); circleLine.scale.set(radius, radius, radius); circleLine.lookAt(target); circleRadiusLabel.visible = true; circleRadiusLabel.position.copy(center.clone().add(B).multiplyScalar(0.5)); circleRadiusLabel.setText(`${radius.toFixed(3)}`); } } { // update area label this.areaLabel.position.copy(centroid); this.areaLabel.visible = this.showArea && this.points.length >= 3; let area = this.getArea(); let suffix = ""; if(this.lengthUnit != null && this.lengthUnitDisplay != null){ area = area / Math.pow(this.lengthUnit.unitspermeter, 2) * Math.pow(this.lengthUnitDisplay.unitspermeter, 2); //convert to square meters then to the square display unit suffix = this.lengthUnitDisplay.code; } let txtArea = Utils.addCommas(area.toFixed(1)); let msg = `${txtArea} ${suffix}\u00B2`; this.areaLabel.setText(msg); } // this.updateAzimuth(); }; raycast (raycaster, intersects) { for (let i = 0; i < this.points.length; i++) { let sphere = this.spheres[i]; sphere.raycast(raycaster, intersects); } // recalculate distances because they are not necessarely correct // for scaled objects. // see https://github.com/mrdoob/three.js/issues/5827 // TODO: remove this once the bug has been fixed for (let i = 0; i < intersects.length; i++) { let I = intersects[i]; I.distance = raycaster.ray.origin.distanceTo(I.point); } intersects.sort(function (a, b) { return a.distance - b.distance; }); }; get showCoordinates () { return this._showCoordinates; } set showCoordinates (value) { this._showCoordinates = value; this.update(); } get showAngles () { return this._showAngles; } set showAngles (value) { this._showAngles = value; this.update(); } get showCircle () { return this._showCircle; } set showCircle (value) { this._showCircle = value; this.update(); } get showAzimuth(){ return this._showAzimuth; } set showAzimuth(value){ this._showAzimuth = value; this.update(); } get showEdges () { return this._showEdges; } set showEdges (value) { this._showEdges = value; this.update(); } get showHeight () { return this._showHeight; } set showHeight (value) { this._showHeight = value; this.update(); } get showArea () { return this._showArea; } set showArea (value) { this._showArea = value; this.update(); } get closed () { return this._closed; } set closed (value) { this._closed = value; this.update(); } get showDistances () { return this._showDistances; } set showDistances (value) { this._showDistances = value; this.update(); } } class PolygonClipVolume extends Object3D{ constructor(camera){ super(); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = "polygon_clip_volume_" + this.constructor.counter; this.camera = camera.clone(); this.camera.rotation.set(...camera.rotation.toArray()); // [r85] workaround because camera.clone() doesn't work on rotation this.camera.rotation.order = camera.rotation.order; this.camera.updateMatrixWorld(); this.camera.updateProjectionMatrix(); this.camera.matrixWorldInverse.copy(this.camera.matrixWorld).invert(); this.viewMatrix = this.camera.matrixWorldInverse.clone(); this.projMatrix = this.camera.projectionMatrix.clone(); // projected markers this.markers = []; this.initialized = false; } addMarker() { let marker = new Mesh(); let cancel; let drag = e => { let size = e.viewer.renderer.getSize(new Vector2()); let projectedPos = new Vector3( 2.0 * (e.drag.end.x / size.width) - 1.0, -2.0 * (e.drag.end.y / size.height) + 1.0, 0 ); marker.position.copy(projectedPos); }; let drop = e => { cancel(); }; cancel = e => { marker.removeEventListener("drag", drag); marker.removeEventListener("drop", drop); }; marker.addEventListener("drag", drag); marker.addEventListener("drop", drop); this.markers.push(marker); } removeLastMarker() { if(this.markers.length > 0) { this.markers.splice(this.markers.length - 1, 1); } } }; class Utils { static async loadShapefileFeatures (file, callback) { let features = []; let handleFinish = () => { callback(features); }; let source = await shapefile.open(file); while(true){ let result = await source.read(); if (result.done) { handleFinish(); break; } if (result.value && result.value.type === 'Feature' && result.value.geometry !== undefined) { features.push(result.value); } } } static toString (value) { if (value.x != null) { return value.x.toFixed(2) + ', ' + value.y.toFixed(2) + ', ' + value.z.toFixed(2); } else { return '' + value + ''; } } static normalizeURL (url) { let u = new URL(url); return u.protocol + '//' + u.hostname + u.pathname.replace(/\/+/g, '/'); }; static pathExists (url) { let req = XHRFactory.createXMLHttpRequest(); req.open('GET', url, false); req.send(null); if (req.status !== 200) { return false; } return true; }; static debugSphere(parent, position, scale, color){ let geometry = new SphereGeometry(1, 8, 8); let material; if(color !== undefined){ material = new MeshBasicMaterial({color: color}); }else { material = new MeshNormalMaterial(); } let sphere = new Mesh(geometry, material); sphere.position.copy(position); sphere.scale.set(scale, scale, scale); parent.add(sphere); return sphere; } static debugLine(parent, start, end, color){ let material = new LineBasicMaterial({ color: color }); let geometry = new Geometry(); const p1 = new Vector3(0, 0, 0); const p2 = end.clone().sub(start); geometry.vertices.push(p1, p2); let tl = new Line( geometry, material ); tl.position.copy(start); parent.add(tl); let line = { node: tl, set: (start, end) => { geometry.vertices[0].copy(start); geometry.vertices[1].copy(end); geometry.verticesNeedUpdate = true; }, }; return line; } static debugCircle(parent, center, radius, normal, color){ let material = new LineBasicMaterial({ color: color }); let geometry = new Geometry(); let n = 32; for(let i = 0; i <= n; i++){ let u0 = 2 * Math.PI * (i / n); let u1 = 2 * Math.PI * (i + 1) / n; let p0 = new Vector3( Math.cos(u0), Math.sin(u0), 0 ); let p1 = new Vector3( Math.cos(u1), Math.sin(u1), 0 ); geometry.vertices.push(p0, p1); } let tl = new Line( geometry, material ); tl.position.copy(center); tl.scale.set(radius, radius, radius); parent.add(tl); } static debugBox(parent, box, transform = new Matrix4(), color = 0xFFFF00){ let vertices = [ [box.min.x, box.min.y, box.min.z], [box.min.x, box.min.y, box.max.z], [box.min.x, box.max.y, box.min.z], [box.min.x, box.max.y, box.max.z], [box.max.x, box.min.y, box.min.z], [box.max.x, box.min.y, box.max.z], [box.max.x, box.max.y, box.min.z], [box.max.x, box.max.y, box.max.z], ].map(v => new Vector3(...v)); let edges = [ [0, 4], [4, 5], [5, 1], [1, 0], [2, 6], [6, 7], [7, 3], [3, 2], [0, 2], [4, 6], [5, 7], [1, 3] ]; let center = box.getCenter(new Vector3()); let centroids = [ {position: [box.min.x, center.y, center.z], color: 0xFF0000}, {position: [box.max.x, center.y, center.z], color: 0x880000}, {position: [center.x, box.min.y, center.z], color: 0x00FF00}, {position: [center.x, box.max.y, center.z], color: 0x008800}, {position: [center.x, center.y, box.min.z], color: 0x0000FF}, {position: [center.x, center.y, box.max.z], color: 0x000088}, ]; for(let vertex of vertices){ let pos = vertex.clone().applyMatrix4(transform); Utils.debugSphere(parent, pos, 0.1, 0xFF0000); } for(let edge of edges){ let start = vertices[edge[0]].clone().applyMatrix4(transform); let end = vertices[edge[1]].clone().applyMatrix4(transform); Utils.debugLine(parent, start, end, color); } for(let centroid of centroids){ let pos = new Vector3(...centroid.position).applyMatrix4(transform); Utils.debugSphere(parent, pos, 0.1, centroid.color); } } static debugPlane(parent, plane, size = 1, color = 0x0000FF){ let planehelper = new PlaneHelper(plane, size, color); parent.add(planehelper); } /** * adapted from mhluska at https://github.com/mrdoob/three.js/issues/1561 */ static computeTransformedBoundingBox (box, transform) { let vertices = [ new Vector3(box.min.x, box.min.y, box.min.z).applyMatrix4(transform), new Vector3(box.min.x, box.min.y, box.min.z).applyMatrix4(transform), new Vector3(box.max.x, box.min.y, box.min.z).applyMatrix4(transform), new Vector3(box.min.x, box.max.y, box.min.z).applyMatrix4(transform), new Vector3(box.min.x, box.min.y, box.max.z).applyMatrix4(transform), new Vector3(box.min.x, box.max.y, box.max.z).applyMatrix4(transform), new Vector3(box.max.x, box.max.y, box.min.z).applyMatrix4(transform), new Vector3(box.max.x, box.min.y, box.max.z).applyMatrix4(transform), new Vector3(box.max.x, box.max.y, box.max.z).applyMatrix4(transform) ]; let boundingBox = new Box3(); boundingBox.setFromPoints(vertices); return boundingBox; };//感觉就是bound.applyMatrix4(transform) /** * add separators to large numbers * * @param nStr * @returns */ static addCommas (nStr) { nStr += ''; let x = nStr.split('.'); let x1 = x[0]; let x2 = x.length > 1 ? '.' + x[1] : ''; let rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; }; static removeCommas (str) { return str.replace(/,/g, ''); } /** * create worker from a string * * code from http://stackoverflow.com/questions/10343913/how-to-create-a-web-worker-from-a-string */ static createWorker (code) { let blob = new Blob([code], {type: 'application/javascript'}); let worker = new Worker(URL.createObjectURL(blob)); return worker; }; static moveTo(scene, endPosition, endTarget){ let view = scene.view; let camera = scene.getActiveCamera(); let animationDuration = 500; let easing = TWEEN.Easing.Quartic.Out; { // animate camera position let tween = new TWEEN.Tween(view.position).to(endPosition, animationDuration); tween.easing(easing); tween.start(); } { // animate camera target let camTargetDistance = camera.position.distanceTo(endTarget); let target = new Vector3().addVectors( camera.position, camera.getWorldDirection(new Vector3()).clone().multiplyScalar(camTargetDistance) ); let tween = new TWEEN.Tween(target).to(endTarget, animationDuration); tween.easing(easing); tween.onUpdate(() => { view.lookAt(target); }); tween.onComplete(() => { view.lookAt(target); }); tween.start(); } } static loadSkybox (path) { let parent = new Object3D("skybox_root"); let camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 100000); camera.up.set(0, 0, 1); let scene = new Scene(); let format = '.jpg'; let urls = [ path + 'px' + format, path + 'nx' + format, path + 'py' + format, path + 'ny' + format, path + 'pz' + format, path + 'nz' + format ]; let materialArray = []; { for (let i = 0; i < 6; i++) { let material = new MeshBasicMaterial({ map: null, side: BackSide, depthTest: false, depthWrite: false, color: 0x424556 }); materialArray.push(material); let loader = new TextureLoader(); loader.load(urls[i], function loaded (texture) { material.map = texture; material.needsUpdate = true; material.color.setHex(0xffffff); }, function progress (xhr) { // console.log( (xhr.loaded / xhr.total * 100) + '% loaded' ); }, function error (xhr) { console.log('An error happened', xhr); } ); } } let skyGeometry = new BoxGeometry(700, 700, 700); let skybox = new Mesh(skyGeometry, materialArray); scene.add(skybox); scene.traverse(n => n.frustumCulled = false); // z up scene.rotation.x = Math.PI / 2; parent.children.push(camera); camera.parent = parent; return {camera, scene, parent}; }; static createGrid (width, length, spacing, color) { let material = new LineBasicMaterial({ color: color || 0x888888 }); let geometry = new Geometry(); for (let i = 0; i <= length; i++) { geometry.vertices.push(new Vector3(-(spacing * width) / 2, i * spacing - (spacing * length) / 2, 0)); geometry.vertices.push(new Vector3(+(spacing * width) / 2, i * spacing - (spacing * length) / 2, 0)); } for (let i = 0; i <= width; i++) { geometry.vertices.push(new Vector3(i * spacing - (spacing * width) / 2, -(spacing * length) / 2, 0)); geometry.vertices.push(new Vector3(i * spacing - (spacing * width) / 2, +(spacing * length) / 2, 0)); } let line = new LineSegments(geometry, material, LinePieces); line.receiveShadow = true; return line; } static createBackgroundTexture (width, height) { function gauss (x, y) { return (1 / (2 * Math.PI)) * Math.exp(-(x * x + y * y) / 2); }; // map.magFilter = THREE.NearestFilter; let size = width * height; let data = new Uint8Array(3 * size); let chroma = [1, 1.5, 1.7]; let max = gauss(0, 0); for (let x = 0; x < width; x++) { for (let y = 0; y < height; y++) { let u = 2 * (x / width) - 1; let v = 2 * (y / height) - 1; let i = x + width * y; let d = gauss(2 * u, 2 * v) / max; let r = (Math.random() + Math.random() + Math.random()) / 3; r = (d * 0.5 + 0.5) * r * 0.03; r = r * 0.4; // d = Math.pow(d, 0.6); data[3 * i + 0] = 255 * (d / 15 + 0.05 + r) * chroma[0]; data[3 * i + 1] = 255 * (d / 15 + 0.05 + r) * chroma[1]; data[3 * i + 2] = 255 * (d / 15 + 0.05 + r) * chroma[2]; } } let texture = new DataTexture(data, width, height, RGBFormat); texture.needsUpdate = true; return texture; } static getMousePointCloudIntersection (mouse, camera, viewer, pointclouds, params = {}) { let renderer = viewer.renderer; let nmouse = { x: (mouse.x / renderer.domElement.clientWidth) * 2 - 1, y: -(mouse.y / renderer.domElement.clientHeight) * 2 + 1 }; let pickParams = {}; if(params.pickClipped){ pickParams.pickClipped = params.pickClipped; } pickParams.x = mouse.x; pickParams.y = renderer.domElement.clientHeight - mouse.y; let raycaster = new Raycaster(); raycaster.setFromCamera(nmouse, camera); let ray = raycaster.ray; let selectedPointcloud = null; let closestDistance = Infinity; let closestIntersection = null; let closestPoint = null; for(let pointcloud of pointclouds){ let point = pointcloud.pick(viewer, camera, ray, pickParams); if(!point){ continue; } let distance = camera.position.distanceTo(point.position); if (distance < closestDistance) { closestDistance = distance; selectedPointcloud = pointcloud; closestIntersection = point.position; closestPoint = point; } } if (selectedPointcloud) { return { location: closestIntersection, distance: closestDistance, pointcloud: selectedPointcloud, point: closestPoint }; } else { return null; } } static pixelsArrayToImage (pixels, width, height) { let canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; let context = canvas.getContext('2d'); pixels = new pixels.constructor(pixels); for (let i = 0; i < pixels.length; i++) { pixels[i * 4 + 3] = 255; } let imageData = context.createImageData(width, height); imageData.data.set(pixels); context.putImageData(imageData, 0, 0); let img = new Image(); img.src = canvas.toDataURL(); // img.style.transform = "scaleY(-1)"; return img; } static pixelsArrayToDataUrl(pixels, width, height) { let canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; let context = canvas.getContext('2d'); pixels = new pixels.constructor(pixels); for (let i = 0; i < pixels.length; i++) { pixels[i * 4 + 3] = 255; } let imageData = context.createImageData(width, height); imageData.data.set(pixels); context.putImageData(imageData, 0, 0); let dataURL = canvas.toDataURL(); return dataURL; } static pixelsArrayToCanvas(pixels, width, height){ let canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; let context = canvas.getContext('2d'); pixels = new pixels.constructor(pixels); //for (let i = 0; i < pixels.length; i++) { // pixels[i * 4 + 3] = 255; //} // flip vertically let bytesPerLine = width * 4; for(let i = 0; i < parseInt(height / 2); i++){ let j = height - i - 1; let lineI = pixels.slice(i * bytesPerLine, i * bytesPerLine + bytesPerLine); let lineJ = pixels.slice(j * bytesPerLine, j * bytesPerLine + bytesPerLine); pixels.set(lineJ, i * bytesPerLine); pixels.set(lineI, j * bytesPerLine); } let imageData = context.createImageData(width, height); imageData.data.set(pixels); context.putImageData(imageData, 0, 0); return canvas; } static removeListeners(dispatcher, type){ if (dispatcher._listeners === undefined) { return; } if (dispatcher._listeners[ type ]) { delete dispatcher._listeners[ type ]; } } static mouseToRay(mouse, camera, width, height){ let normalizedMouse = { x: (mouse.x / width) * 2 - 1, y: -(mouse.y / height) * 2 + 1 }; let vector = new Vector3(normalizedMouse.x, normalizedMouse.y, 0.5); let origin = camera.position.clone(); vector.unproject(camera); let direction = new Vector3().subVectors(vector, origin).normalize(); let ray = new Ray(origin, direction); return ray; } static projectedRadius(radius, camera, distance, screenWidth, screenHeight){ if(camera instanceof OrthographicCamera){ return Utils.projectedRadiusOrtho(radius, camera.projectionMatrix, screenWidth, screenHeight); }else if(camera instanceof PerspectiveCamera){ return Utils.projectedRadiusPerspective(radius, camera.fov * Math.PI / 180, distance, screenHeight); }else { throw new Error("invalid parameters"); } } static projectedRadiusPerspective(radius, fov, distance, screenHeight) { let projFactor = (1 / Math.tan(fov / 2)) / distance; projFactor = projFactor * screenHeight / 2; return radius * projFactor; } static projectedRadiusOrtho(radius, proj, screenWidth, screenHeight) { let p1 = new Vector4(0); let p2 = new Vector4(radius); p1.applyMatrix4(proj); p2.applyMatrix4(proj); p1 = new Vector3(p1.x, p1.y, p1.z); p2 = new Vector3(p2.x, p2.y, p2.z); p1.x = (p1.x + 1.0) * 0.5 * screenWidth; p1.y = (p1.y + 1.0) * 0.5 * screenHeight; p2.x = (p2.x + 1.0) * 0.5 * screenWidth; p2.y = (p2.y + 1.0) * 0.5 * screenHeight; return p1.distanceTo(p2); } static topView(camera, node){ camera.position.set(0, 1, 0); camera.rotation.set(-Math.PI / 2, 0, 0); camera.zoomTo(node, 1); } static frontView (camera, node) { camera.position.set(0, 0, 1); camera.rotation.set(0, 0, 0); camera.zoomTo(node, 1); } static leftView (camera, node) { camera.position.set(-1, 0, 0); camera.rotation.set(0, -Math.PI / 2, 0); camera.zoomTo(node, 1); } static rightView (camera, node) { camera.position.set(1, 0, 0); camera.rotation.set(0, Math.PI / 2, 0); camera.zoomTo(node, 1); } static findClosestGpsTime(target, viewer){ const start = performance.now(); const nodes = []; for(const pc of viewer.scene.pointclouds){ nodes.push(pc.root); for(const child of pc.root.children){ if(child){ nodes.push(child); } } } let closestNode = null; let closestIndex = Infinity; let closestDistance = Infinity; let closestValue = 0; for(const node of nodes){ const isOkay = node.geometryNode != null && node.geometryNode.geometry != null && node.sceneNode != null; if(!isOkay){ continue; } let geometry = node.geometryNode.geometry; let gpsTime = geometry.attributes["gps-time"]; let range = gpsTime.potree.range; for(let i = 0; i < gpsTime.array.length; i++){ let value = gpsTime.array[i]; value = value * (range[1] - range[0]) + range[0]; const distance = Math.abs(target - value); if(distance < closestDistance){ closestIndex = i; closestDistance = distance; closestValue = value; closestNode = node; //console.log("found a closer one: " + value); } } } const geometry = closestNode.geometryNode.geometry; const position = new Vector3( geometry.attributes.position.array[3 * closestIndex + 0], geometry.attributes.position.array[3 * closestIndex + 1], geometry.attributes.position.array[3 * closestIndex + 2], ); position.applyMatrix4(closestNode.sceneNode.matrixWorld); const end = performance.now(); const duration = (end - start); console.log(`duration: ${duration.toFixed(3)}ms`); return { node: closestNode, index: closestIndex, position: position, }; } /** * * 0: no intersection * 1: intersection * 2: fully inside */ static frustumSphereIntersection (frustum, sphere) { let planes = frustum.planes; let center = sphere.center; let negRadius = -sphere.radius; let minDistance = Number.MAX_VALUE; for (let i = 0; i < 6; i++) { let distance = planes[ i ].distanceToPoint(center); if (distance < negRadius) { return 0; } minDistance = Math.min(minDistance, distance); } return (minDistance >= sphere.radius) ? 2 : 1; } // code taken from three.js // ImageUtils - generateDataTexture() static generateDataTexture (width, height, color) { let size = width * height; let data = new Uint8Array(4 * width * height); let r = Math.floor(color.r * 255); let g = Math.floor(color.g * 255); let b = Math.floor(color.b * 255); for (let i = 0; i < size; i++) { data[ i * 3 ] = r; data[ i * 3 + 1 ] = g; data[ i * 3 + 2 ] = b; } let texture = new DataTexture(data, width, height, RGBAFormat); texture.needsUpdate = true; texture.magFilter = NearestFilter; return texture; } // from http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values-in-javascript static getParameterByName (name) { name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]'); let regex = new RegExp('[\\?&]' + name + '=([^&#]*)'); let results = regex.exec(document.location.search); return results === null ? null : decodeURIComponent(results[1].replace(/\+/g, ' ')); } static setParameter (name, value) { // value = encodeURIComponent(value); name = name.replace(/[[]/, '\\[').replace(/[\]]/, '\\]'); let regex = new RegExp('([\\?&])(' + name + '=([^&#]*))'); let results = regex.exec(document.location.search); let url = window.location.href; if (results === null) { if (window.location.search.length === 0) { url = url + '?'; } else { url = url + '&'; } url = url + name + '=' + value; } else { let newValue = name + '=' + value; url = url.replace(results[2], newValue); } window.history.replaceState({}, '', url); } static createChildAABB(aabb, index){ let min = aabb.min.clone(); let max = aabb.max.clone(); let size = new Vector3().subVectors(max, min); if ((index & 0b0001) > 0) { min.z += size.z / 2; } else { max.z -= size.z / 2; } if ((index & 0b0010) > 0) { min.y += size.y / 2; } else { max.y -= size.y / 2; } if ((index & 0b0100) > 0) { min.x += size.x / 2; } else { max.x -= size.x / 2; } return new Box3(min, max); } // see https://stackoverflow.com/questions/400212/how-do-i-copy-to-the-clipboard-in-javascript static clipboardCopy(text){ let textArea = document.createElement("textarea"); textArea.style.position = 'fixed'; textArea.style.top = 0; textArea.style.left = 0; textArea.style.width = '2em'; textArea.style.height = '2em'; textArea.style.padding = 0; textArea.style.border = 'none'; textArea.style.outline = 'none'; textArea.style.boxShadow = 'none'; textArea.style.background = 'transparent'; textArea.value = text; document.body.appendChild(textArea); textArea.select(); try { let success = document.execCommand('copy'); if(success){ console.log("copied text to clipboard"); }else { console.log("copy to clipboard failed"); } } catch (err) { console.log("error while trying to copy to clipboard"); } document.body.removeChild(textArea); } static getMeasurementIcon(measurement){ if (measurement instanceof Measure) { if (measurement.showDistances && !measurement.showArea && !measurement.showAngles) { return `${Potree.resourcePath}/icons/distance.svg`; } else if (measurement.showDistances && measurement.showArea && !measurement.showAngles) { return `${Potree.resourcePath}/icons/area.svg`; } else if (measurement.maxMarkers === 1) { return `${Potree.resourcePath}/icons/point.svg`; } else if (!measurement.showDistances && !measurement.showArea && measurement.showAngles) { return `${Potree.resourcePath}/icons/angle.png`; } else if (measurement.showHeight) { return `${Potree.resourcePath}/icons/height.svg`; } else { return `${Potree.resourcePath}/icons/distance.svg`; } } else if (measurement instanceof Profile) { return `${Potree.resourcePath}/icons/profile.svg`; } else if (measurement instanceof Volume) { return `${Potree.resourcePath}/icons/volume.svg`; } else if (measurement instanceof PolygonClipVolume) { return `${Potree.resourcePath}/icons/clip-polygon.svg`; } } static lineToLineIntersection(P0, P1, P2, P3){ const P = [P0, P1, P2, P3]; const d = (m, n, o, p) => { let result = (P[m].x - P[n].x) * (P[o].x - P[p].x) + (P[m].y - P[n].y) * (P[o].y - P[p].y) + (P[m].z - P[n].z) * (P[o].z - P[p].z); return result; }; const mua = (d(0, 2, 3, 2) * d(3, 2, 1, 0) - d(0, 2, 1, 0) * d(3, 2, 3, 2)) /**-----------------------------------------------------------------**/ / (d(1, 0, 1, 0) * d(3, 2, 3, 2) - d(3, 2, 1, 0) * d(3, 2, 1, 0)); const mub = (d(0, 2, 3, 2) + mua * d(3, 2, 1, 0)) /**--------------------------------------**/ / d(3, 2, 3, 2); const P01 = P1.clone().sub(P0); const P23 = P3.clone().sub(P2); const Pa = P0.clone().add(P01.multiplyScalar(mua)); const Pb = P2.clone().add(P23.multiplyScalar(mub)); const center = Pa.clone().add(Pb).multiplyScalar(0.5); return center; } static computeCircleCenter(A, B, C){ const AB = B.clone().sub(A); const AC = C.clone().sub(A); const N = AC.clone().cross(AB).normalize(); const ab_dir = AB.clone().cross(N).normalize(); const ac_dir = AC.clone().cross(N).normalize(); const ab_origin = A.clone().add(B).multiplyScalar(0.5); const ac_origin = A.clone().add(C).multiplyScalar(0.5); const P0 = ab_origin; const P1 = ab_origin.clone().add(ab_dir); const P2 = ac_origin; const P3 = ac_origin.clone().add(ac_dir); const center = Utils.lineToLineIntersection(P0, P1, P2, P3); return center; // Potree.Utils.debugLine(viewer.scene.scene, P0, P1, 0x00ff00); // Potree.Utils.debugLine(viewer.scene.scene, P2, P3, 0x0000ff); // Potree.Utils.debugSphere(viewer.scene.scene, center, 0.03, 0xff00ff); // const radius = center.distanceTo(A); // Potree.Utils.debugCircle(viewer.scene.scene, center, radius, new THREE.Vector3(0, 0, 1), 0xff00ff); } static getNorthVec(p1, distance, projection){ if(projection){ // if there is a projection, transform coordinates to WGS84 // and compute angle to north there proj4.defs("pointcloud", projection); const transform = proj4("pointcloud", "WGS84"); const llP1 = transform.forward(p1.toArray()); let llP2 = transform.forward([p1.x, p1.y + distance]); const polarRadius = Math.sqrt((llP2[0] - llP1[0]) ** 2 + (llP2[1] - llP1[1]) ** 2); llP2 = [llP1[0], llP1[1] + polarRadius]; const northVec = transform.inverse(llP2); return new Vector3(...northVec, p1.z).sub(p1); }else { // if there is no projection, assume [0, 1, 0] as north direction const vec = new Vector3(0, 1, 0).multiplyScalar(distance); return vec; } } static computeAzimuth(p1, p2, projection){ let azimuth = 0; if(projection){ // if there is a projection, transform coordinates to WGS84 // and compute angle to north there let transform; if (projection.includes('EPSG')) { transform = proj4(projection, "WGS84"); } else { proj4.defs("pointcloud", projection); transform = proj4("pointcloud", "WGS84"); } const llP1 = transform.forward(p1.toArray()); const llP2 = transform.forward(p2.toArray()); const dir = [ llP2[0] - llP1[0], llP2[1] - llP1[1], ]; azimuth = Math.atan2(dir[1], dir[0]) - Math.PI / 2; }else { // if there is no projection, assume [0, 1, 0] as north direction const dir = [p2.x - p1.x, p2.y - p1.y]; azimuth = Math.atan2(dir[1], dir[0]) - Math.PI / 2; } // make clockwise azimuth = -azimuth; return azimuth; } static async loadScript(url){ return new Promise( resolve => { const element = document.getElementById(url); if(element){ resolve(); }else { const script = document.createElement("script"); script.id = url; script.onload = () => { resolve(); }; script.src = url; document.body.appendChild(script); } }); } static createSvgGradient(scheme){ // this is what we are creating: // // // // // // ... // // // // // // const gradientId = `${Math.random()}_${Date.now()}`; const svgn = "http://www.w3.org/2000/svg"; const svg = document.createElementNS(svgn, "svg"); svg.setAttributeNS(null, "width", "2em"); svg.setAttributeNS(null, "height", "3em"); { // const defs = document.createElementNS(svgn, "defs"); const linearGradient = document.createElementNS(svgn, "linearGradient"); linearGradient.setAttributeNS(null, "id", gradientId); linearGradient.setAttributeNS(null, "gradientTransform", "rotate(90)"); for(let i = scheme.length - 1; i >= 0; i--){ const stopVal = scheme[i]; const percent = parseInt(100 - stopVal[0] * 100); const [r, g, b] = stopVal[1].toArray().map(v => parseInt(v * 255)); const stop = document.createElementNS(svgn, "stop"); stop.setAttributeNS(null, "offset", `${percent}%`); stop.setAttributeNS(null, "stop-color", `rgb(${r}, ${g}, ${b})`); linearGradient.appendChild(stop); } defs.appendChild(linearGradient); svg.appendChild(defs); } const rect = document.createElementNS(svgn, "rect"); rect.setAttributeNS(null, "width", `100%`); rect.setAttributeNS(null, "height", `100%`); rect.setAttributeNS(null, "fill", `url("#${gradientId}")`); rect.setAttributeNS(null, "stroke", `black`); rect.setAttributeNS(null, "stroke-width", `0.1em`); svg.appendChild(rect); return svg; } static async waitAny(promises){ return new Promise( (resolve) => { promises.map( promise => { promise.then( () => { resolve(); }); }); }); } } Utils.screenPass = new function () { this.screenScene = new Scene(); this.screenQuad = new Mesh(new PlaneBufferGeometry(2, 2, 1)); this.screenQuad.material.depthTest = true; this.screenQuad.material.depthWrite = true; this.screenQuad.material.transparent = true; this.screenScene.add(this.screenQuad); this.camera = new Camera(); this.render = function (renderer, material, target) { this.screenQuad.material = material; if (typeof target === 'undefined') { renderer.render(this.screenScene, this.camera); } else { renderer.render(this.screenScene, this.camera, target); } }; }(); class Version{ constructor(version){ this.version = version; let vmLength = (version.indexOf('.') === -1) ? version.length : version.indexOf('.'); this.versionMajor = parseInt(version.substr(0, vmLength)); this.versionMinor = parseInt(version.substr(vmLength + 1)); if (this.versionMinor.length === 0) { this.versionMinor = 0; } } newerThan(version){ let v = new Version(version); if (this.versionMajor > v.versionMajor) { return true; } else if (this.versionMajor === v.versionMajor && this.versionMinor > v.versionMinor) { return true; } else { return false; } } equalOrHigher(version){ let v = new Version(version); if (this.versionMajor > v.versionMajor) { return true; } else if (this.versionMajor === v.versionMajor && this.versionMinor >= v.versionMinor) { return true; } else { return false; } } upTo(version){ return !this.newerThan(version); } } //加载 解析点云 class BinaryLoader{ constructor(version, boundingBox, scale){ if (typeof (version) === 'string') { this.version = new Version(version); } else { this.version = version; } this.boundingBox = boundingBox; this.scale = scale; } load(node){ if (node.loaded) { return; } let url = node.getURL(); if (this.version.equalOrHigher('1.4')) { url += '.bin'; } let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if((xhr.status === 200 || xhr.status === 0) && xhr.response !== null){ let buffer = xhr.response; this.parse(node, buffer); } else { //console.error(`Failed to load file! HTTP status: ${xhr.status}, file: ${url}`); throw new Error(`Failed to load file! HTTP status: ${xhr.status}, file: ${url}`); } } }; try { xhr.send(null); } catch (e) { console.log('fehler beim laden der punktwolke: ' + e); } }; parse(node, buffer, callback){ let pointAttributes = node.pcoGeometry.pointAttributes; let numPoints = buffer.byteLength / node.pcoGeometry.pointAttributes.byteSize; if (this.version.upTo('1.5')) { node.numPoints = numPoints; } let workerPath = Potree.scriptPath + '/workers/BinaryDecoderWorker.js'; let worker = Potree.workerPool.getWorker(workerPath); worker.onmessage = function (e) { let data = e.data; let buffers = data.attributeBuffers; let tightBoundingBox = new Box3( new Vector3().fromArray(data.tightBoundingBox.min), new Vector3().fromArray(data.tightBoundingBox.max) ); Potree.workerPool.returnWorker(workerPath, worker); let geometry = new BufferGeometry(); for(let property in buffers){ let buffer = buffers[property].buffer; let batchAttribute = buffers[property].attribute; if (property === "POSITION_CARTESIAN") { geometry.setAttribute('position', new BufferAttribute(new Float32Array(buffer), 3)); } else if (property === "rgba") { geometry.setAttribute("rgba", new BufferAttribute(new Uint8Array(buffer), 4, true)); } else if (property === "NORMAL_SPHEREMAPPED") { geometry.setAttribute('normal', new BufferAttribute(new Float32Array(buffer), 3)); } else if (property === "NORMAL_OCT16") { geometry.setAttribute('normal', new BufferAttribute(new Float32Array(buffer), 3)); } else if (property === "NORMAL") { geometry.setAttribute('normal', new BufferAttribute(new Float32Array(buffer), 3)); } else if (property === "INDICES") { let bufferAttribute = new BufferAttribute(new Uint8Array(buffer), 4); bufferAttribute.normalized = true; geometry.setAttribute('indices', bufferAttribute); } else if (property === "SPACING") { let bufferAttribute = new BufferAttribute(new Float32Array(buffer), 1); geometry.setAttribute('spacing', bufferAttribute); } else { const bufferAttribute = new BufferAttribute(new Float32Array(buffer), 1); bufferAttribute.potree = { offset: buffers[property].offset, scale: buffers[property].scale, preciseBuffer: buffers[property].preciseBuffer, range: batchAttribute.range, }; geometry.setAttribute(property, bufferAttribute); const attribute = pointAttributes.attributes.find(a => a.name === batchAttribute.name); attribute.range[0] = Math.min(attribute.range[0], batchAttribute.range[0]); attribute.range[1] = Math.max(attribute.range[1], batchAttribute.range[1]); if(node.getLevel() === 0){ attribute.initialRange = batchAttribute.range; } } } tightBoundingBox.max.sub(tightBoundingBox.min); tightBoundingBox.min.set(0, 0, 0); let numPoints = e.data.buffer.byteLength / pointAttributes.byteSize; node.numPoints = numPoints; node.geometry = geometry; node.mean = new Vector3(...data.mean); node.tightBoundingBox = tightBoundingBox; node.loaded = true; node.loading = false; node.estimatedSpacing = data.estimatedSpacing; Potree.numNodesLoading--; callback();//add }; let message = { buffer: buffer, pointAttributes: pointAttributes, version: this.version.version, min: [ node.boundingBox.min.x, node.boundingBox.min.y, node.boundingBox.min.z ], offset: [node.pcoGeometry.offset.x, node.pcoGeometry.offset.y, node.pcoGeometry.offset.z], scale: this.scale, spacing: node.spacing, hasChildren: node.hasChildren, name: node.name }; worker.postMessage(message, [message.buffer]); }; } let ftCanvas = document.createElement('canvas'); const Features = (function () { let gl = ftCanvas.getContext('webgl') || ftCanvas.getContext('experimental-webgl'); if (gl === null){ return {}; } // -- code taken from THREE.WebGLRenderer -- let _vertexShaderPrecisionHighpFloat = gl.getShaderPrecisionFormat(gl.VERTEX_SHADER, gl.HIGH_FLOAT); let _vertexShaderPrecisionMediumpFloat = gl.getShaderPrecisionFormat(gl.VERTEX_SHADER, gl.MEDIUM_FLOAT); // Unused: let _vertexShaderPrecisionLowpFloat = gl.getShaderPrecisionFormat(gl.VERTEX_SHADER, gl.LOW_FLOAT); let _fragmentShaderPrecisionHighpFloat = gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.HIGH_FLOAT); let _fragmentShaderPrecisionMediumpFloat = gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.MEDIUM_FLOAT); // Unused: let _fragmentShaderPrecisionLowpFloat = gl.getShaderPrecisionFormat(gl.FRAGMENT_SHADER, gl.LOW_FLOAT); let highpAvailable = _vertexShaderPrecisionHighpFloat.precision > 0 && _fragmentShaderPrecisionHighpFloat.precision > 0; let mediumpAvailable = _vertexShaderPrecisionMediumpFloat.precision > 0 && _fragmentShaderPrecisionMediumpFloat.precision > 0; // ----------------------------------------- let precision; if (highpAvailable) { precision = 'highp'; } else if (mediumpAvailable) { precision = 'mediump'; } else { precision = 'lowp'; } return { SHADER_INTERPOLATION: { isSupported: function () { let supported = true; supported = supported && gl.getExtension('EXT_frag_depth'); supported = supported && gl.getParameter(gl.MAX_VARYING_VECTORS) >= 8; return supported; } }, SHADER_SPLATS: { isSupported: function () { let supported = true; supported = supported && gl.getExtension('EXT_frag_depth'); supported = supported && gl.getExtension('OES_texture_float'); supported = supported && gl.getParameter(gl.MAX_VARYING_VECTORS) >= 8; return supported; } }, SHADER_EDL: { isSupported: function () { let supported = true; supported = supported && gl.getExtension('EXT_frag_depth'); supported = supported && gl.getExtension('OES_texture_float'); supported = supported && gl.getParameter(gl.MAX_VARYING_VECTORS) >= 8; //supported = supported || (gl instanceof WebGL2RenderingContext); return supported; } }, //WEBGL2: { // isSupported: function(){ // return typeof WebGL2RenderingContext != 'undefined' && gl instanceof WebGL2RenderingContext; // } //}, precision: precision }; }()); /** * Some types of possible point attribute data formats * * @class */ const PointAttributeTypes = { DATA_TYPE_DOUBLE: {ordinal: 0, name: "double", size: 8}, DATA_TYPE_FLOAT: {ordinal: 1, name: "float", size: 4}, DATA_TYPE_INT8: {ordinal: 2, name: "int8", size: 1}, DATA_TYPE_UINT8: {ordinal: 3, name: "uint8", size: 1}, DATA_TYPE_INT16: {ordinal: 4, name: "int16", size: 2}, DATA_TYPE_UINT16: {ordinal: 5, name: "uint16", size: 2}, DATA_TYPE_INT32: {ordinal: 6, name: "int32", size: 4}, DATA_TYPE_UINT32: {ordinal: 7, name: "uint32", size: 4}, DATA_TYPE_INT64: {ordinal: 8, name: "int64", size: 8}, DATA_TYPE_UINT64: {ordinal: 9, name: "uint64", size: 8} }; let i = 0; for (let obj in PointAttributeTypes) { PointAttributeTypes[i] = PointAttributeTypes[obj]; i++; } class PointAttribute{ constructor(name, type, numElements){ this.name = name; this.type = type; this.numElements = numElements; this.byteSize = this.numElements * this.type.size; this.description = ""; this.range = [Infinity, -Infinity]; } }; PointAttribute.POSITION_CARTESIAN = new PointAttribute( "POSITION_CARTESIAN", PointAttributeTypes.DATA_TYPE_FLOAT, 3); PointAttribute.RGBA_PACKED = new PointAttribute( "COLOR_PACKED", PointAttributeTypes.DATA_TYPE_INT8, 4); PointAttribute.COLOR_PACKED = PointAttribute.RGBA_PACKED; PointAttribute.RGB_PACKED = new PointAttribute( "COLOR_PACKED", PointAttributeTypes.DATA_TYPE_INT8, 3); PointAttribute.NORMAL_FLOATS = new PointAttribute( "NORMAL_FLOATS", PointAttributeTypes.DATA_TYPE_FLOAT, 3); PointAttribute.INTENSITY = new PointAttribute( "INTENSITY", PointAttributeTypes.DATA_TYPE_UINT16, 1); PointAttribute.CLASSIFICATION = new PointAttribute( "CLASSIFICATION", PointAttributeTypes.DATA_TYPE_UINT8, 1); PointAttribute.NORMAL_SPHEREMAPPED = new PointAttribute( "NORMAL_SPHEREMAPPED", PointAttributeTypes.DATA_TYPE_UINT8, 2); PointAttribute.NORMAL_OCT16 = new PointAttribute( "NORMAL_OCT16", PointAttributeTypes.DATA_TYPE_UINT8, 2); PointAttribute.NORMAL = new PointAttribute( "NORMAL", PointAttributeTypes.DATA_TYPE_FLOAT, 3); PointAttribute.RETURN_NUMBER = new PointAttribute( "RETURN_NUMBER", PointAttributeTypes.DATA_TYPE_UINT8, 1); PointAttribute.NUMBER_OF_RETURNS = new PointAttribute( "NUMBER_OF_RETURNS", PointAttributeTypes.DATA_TYPE_UINT8, 1); PointAttribute.SOURCE_ID = new PointAttribute( "SOURCE_ID", PointAttributeTypes.DATA_TYPE_UINT16, 1); PointAttribute.INDICES = new PointAttribute( "INDICES", PointAttributeTypes.DATA_TYPE_UINT32, 1); PointAttribute.SPACING = new PointAttribute( "SPACING", PointAttributeTypes.DATA_TYPE_FLOAT, 1); PointAttribute.GPS_TIME = new PointAttribute( "GPS_TIME", PointAttributeTypes.DATA_TYPE_DOUBLE, 1); class PointAttributes{ constructor(pointAttributes){ this.attributes = []; this.byteSize = 0; this.size = 0; this.vectors = []; if (pointAttributes != null) { for (let i = 0; i < pointAttributes.length; i++) { let pointAttributeName = pointAttributes[i]; let pointAttribute = PointAttribute[pointAttributeName]; this.attributes.push(pointAttribute); this.byteSize += pointAttribute.byteSize; this.size++; } } } add(pointAttribute){ this.attributes.push(pointAttribute); this.byteSize += pointAttribute.byteSize; this.size++; }; addVector(vector){ this.vectors.push(vector); } hasNormals(){ for (let name in this.attributes) { let pointAttribute = this.attributes[name]; if ( pointAttribute === PointAttribute.NORMAL_SPHEREMAPPED || pointAttribute === PointAttribute.NORMAL_FLOATS || pointAttribute === PointAttribute.NORMAL || pointAttribute === PointAttribute.NORMAL_OCT16) { return true; } } return false; }; } class Points$1 { constructor () { this.boundingBox = new Box3(); this.numPoints = 0; this.data = {}; } add (points) { let currentSize = this.numPoints; let additionalSize = points.numPoints; let newSize = currentSize + additionalSize; let thisAttributes = Object.keys(this.data); let otherAttributes = Object.keys(points.data); let attributes = new Set([...thisAttributes, ...otherAttributes]); for (let attribute of attributes) { if (thisAttributes.includes(attribute) && otherAttributes.includes(attribute)) { // attribute in both, merge let Type = this.data[attribute].constructor; let merged = new Type(this.data[attribute].length + points.data[attribute].length); merged.set(this.data[attribute], 0); merged.set(points.data[attribute], this.data[attribute].length); this.data[attribute] = merged; } else if (thisAttributes.includes(attribute) && !otherAttributes.includes(attribute)) { // attribute only in this; take over this and expand to new size let elementsPerPoint = this.data[attribute].length / this.numPoints; let Type = this.data[attribute].constructor; let expanded = new Type(elementsPerPoint * newSize); expanded.set(this.data[attribute], 0); this.data[attribute] = expanded; } else if (!thisAttributes.includes(attribute) && otherAttributes.includes(attribute)) { // attribute only in points to be added; take over new points and expand to new size let elementsPerPoint = points.data[attribute].length / points.numPoints; let Type = points.data[attribute].constructor; let expanded = new Type(elementsPerPoint * newSize); expanded.set(points.data[attribute], elementsPerPoint * currentSize); this.data[attribute] = expanded; } } this.numPoints = newSize; this.boundingBox.union(points.boundingBox); } } class CSVExporter { static toString (points) { let string = ''; let attributes = Object.keys(points.data) .filter(a => a !== 'normal') .sort((a, b) => { if (a === 'position') return -1; if (b === 'position') return 1; if (a === 'rgba') return -1; if (b === 'rgba') return 1; }); let headerValues = []; for (let attribute of attributes) { let itemSize = points.data[attribute].length / points.numPoints; if (attribute === 'position') { headerValues = headerValues.concat(['x', 'y', 'z']); } else if (attribute === 'rgba') { headerValues = headerValues.concat(['r', 'g', 'b', 'a']); } else if (itemSize > 1) { for (let i = 0; i < itemSize; i++) { headerValues.push(`${attribute}_${i}`); } } else { headerValues.push(attribute); } } string = headerValues.join(', ') + '\n'; for (let i = 0; i < points.numPoints; i++) { let values = []; for (let attribute of attributes) { let itemSize = points.data[attribute].length / points.numPoints; let value = points.data[attribute] .subarray(itemSize * i, itemSize * i + itemSize) .join(', '); values.push(value); } string += values.join(', ') + '\n'; } return string; } }; class LASExporter { static toLAS (points) { // TODO Unused: let string = ''; let boundingBox = points.boundingBox; let offset = boundingBox.min.clone(); let diagonal = boundingBox.min.distanceTo(boundingBox.max); let scale = new Vector3(0.001, 0.001, 0.001); if (diagonal > 1000 * 1000) { scale = new Vector3(0.01, 0.01, 0.01); } else { scale = new Vector3(0.001, 0.001, 0.001); } let setString = function (string, offset, buffer) { let view = new Uint8Array(buffer); for (let i = 0; i < string.length; i++) { let charCode = string.charCodeAt(i); view[offset + i] = charCode; } }; let buffer = new ArrayBuffer(227 + 28 * points.numPoints); let view = new DataView(buffer); let u8View = new Uint8Array(buffer); // let u16View = new Uint16Array(buffer); setString('LASF', 0, buffer); u8View[24] = 1; u8View[25] = 2; // system identifier o:26 l:32 // generating software o:58 l:32 setString('Potree 1.7', 58, buffer); // file creation day of year o:90 l:2 // file creation year o:92 l:2 // header size o:94 l:2 view.setUint16(94, 227, true); // offset to point data o:96 l:4 view.setUint32(96, 227, true); // number of letiable length records o:100 l:4 // point data record format 104 1 u8View[104] = 2; // point data record length 105 2 view.setUint16(105, 28, true); // number of point records 107 4 view.setUint32(107, points.numPoints, true); // number of points by return 111 20 // x scale factor 131 8 view.setFloat64(131, scale.x, true); // y scale factor 139 8 view.setFloat64(139, scale.y, true); // z scale factor 147 8 view.setFloat64(147, scale.z, true); // x offset 155 8 view.setFloat64(155, offset.x, true); // y offset 163 8 view.setFloat64(163, offset.y, true); // z offset 171 8 view.setFloat64(171, offset.z, true); // max x 179 8 view.setFloat64(179, boundingBox.max.x, true); // min x 187 8 view.setFloat64(187, boundingBox.min.x, true); // max y 195 8 view.setFloat64(195, boundingBox.max.y, true); // min y 203 8 view.setFloat64(203, boundingBox.min.y, true); // max z 211 8 view.setFloat64(211, boundingBox.max.z, true); // min z 219 8 view.setFloat64(219, boundingBox.min.z, true); let boffset = 227; for (let i = 0; i < points.numPoints; i++) { let px = points.data.position[3 * i + 0]; let py = points.data.position[3 * i + 1]; let pz = points.data.position[3 * i + 2]; let ux = parseInt((px - offset.x) / scale.x); let uy = parseInt((py - offset.y) / scale.y); let uz = parseInt((pz - offset.z) / scale.z); view.setUint32(boffset + 0, ux, true); view.setUint32(boffset + 4, uy, true); view.setUint32(boffset + 8, uz, true); if (points.data.intensity) { view.setUint16(boffset + 12, (points.data.intensity[i]), true); } let rt = 0; if (points.data.returnNumber) { rt += points.data.returnNumber[i]; } if (points.data.numberOfReturns) { rt += (points.data.numberOfReturns[i] << 3); } view.setUint8(boffset + 14, rt); if (points.data.classification) { view.setUint8(boffset + 15, points.data.classification[i]); } // scan angle rank // user data // point source id if (points.data.pointSourceID) { view.setUint16(boffset + 18, points.data.pointSourceID[i]); } if (points.data.rgba) { let rgba = points.data.rgba; view.setUint16(boffset + 20, (rgba[4 * i + 0] * 255), true); view.setUint16(boffset + 22, (rgba[4 * i + 1] * 255), true); view.setUint16(boffset + 24, (rgba[4 * i + 2] * 255), true); } boffset += 28; } return buffer; } } /** * @author mrdoob / http://mrdoob.com/ https://github.com/mrdoob/eventdispatcher.js * * with slight modifications by mschuetz, http://potree.org * */ // The MIT License // // Copyright (c) 2011 Mr.doob // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. class EventDispatcher$1{ constructor(){ this._listeners = {}; } addEventListener(type, listener){ const listeners = this._listeners; if(listeners[type] === undefined){ listeners[type] = []; } if(listeners[type].indexOf(listener) === - 1){ listeners[type].push( listener ); } } hasEventListener(type, listener){ const listeners = this._listeners; return listeners[type] !== undefined && listeners[type].indexOf(listener) !== - 1; } removeEventListener(type, listener){ let listeners = this._listeners; let listenerArray = listeners[type]; if (listenerArray !== undefined){ let index = listenerArray.indexOf(listener); if(index !== - 1){ listenerArray.splice(index, 1); } } } removeEventListeners(type){ if(this._listeners[type] !== undefined){ delete this._listeners[type]; } }; dispatchEvent(event){ let listeners = this._listeners; let listenerArray = listeners[event.type]; if ( listenerArray !== undefined ) { event.target = this; for(let listener of listenerArray.slice(0)){ listener.call(this, event); } } } } class PointCloudTreeNode extends EventDispatcher$1{ constructor(){ super(); this.needsTransformUpdate = true; } getChildren () { throw new Error('override function'); } getBoundingBox () { throw new Error('override function'); } isLoaded () { throw new Error('override function'); } isGeometryNode () { throw new Error('override function'); } isTreeNode () { throw new Error('override function'); } getLevel () { throw new Error('override function'); } getBoundingSphere () { throw new Error('override function'); } }; class PointCloudTree extends Object3D { constructor () { super(); //this.spriteGroup = new THREE.Object3D //add } initialized () { return this.root !== null; } }; class PointCloudOctreeGeometry{ constructor(){ this.url = null; this.octreeDir = null; this.spacing = 0; this.boundingBox = null; this.root = null; this.nodes = null; this.pointAttributes = null; this.hierarchyStepSize = -1; this.loader = null; } } class PointCloudOctreeGeometryNode extends PointCloudTreeNode{ constructor(name, pcoGeometry, boundingBox){ super(); this.id = PointCloudOctreeGeometryNode.IDCount++; this.name = name; this.index = parseInt(name.charAt(name.length - 1)); this.pcoGeometry = pcoGeometry; this.geometry = null; this.boundingBox = boundingBox; this.boundingSphere = boundingBox.getBoundingSphere(new Sphere()); this.children = {}; this.numPoints = 0; this.level = null; this.loaded = false; this.oneTimeDisposeHandlers = []; } isGeometryNode(){ return true; } getLevel(){ return this.level; } isTreeNode(){ return false; } isLoaded(){ return this.loaded; } getBoundingSphere(){ return this.boundingSphere; } getBoundingBox(){ return this.boundingBox; } getChildren(){ let children = []; for (let i = 0; i < 8; i++) { if (this.children[i]) { children.push(this.children[i]); } } return children; } getBoundingBox(){ return this.boundingBox; } getURL(){ let url = ''; let version = this.pcoGeometry.loader.version; if (version.equalOrHigher('1.5')) { url = this.pcoGeometry.octreeDir + '/' + this.getHierarchyPath() + '/' + this.name; } else if (version.equalOrHigher('1.4')) { url = this.pcoGeometry.octreeDir + '/' + this.name; } else if (version.upTo('1.3')) { url = this.pcoGeometry.octreeDir + '/' + this.name; } return url; } getHierarchyPath(){ let path = 'r/'; let hierarchyStepSize = this.pcoGeometry.hierarchyStepSize; let indices = this.name.substr(1); let numParts = Math.floor(indices.length / hierarchyStepSize); for (let i = 0; i < numParts; i++) { path += indices.substr(i * hierarchyStepSize, hierarchyStepSize) + '/'; } path = path.slice(0, -1); return path; } addChild(child) { this.children[child.index] = child; child.parent = this; } load(){ if (this.loading === true || this.loaded === true || Potree.numNodesLoading >= Potree.maxNodesLoading) { return; } this.loading = true; Potree.numNodesLoading++; if (this.pcoGeometry.loader.version.equalOrHigher('1.5')) { if ((this.level % this.pcoGeometry.hierarchyStepSize) === 0 && this.hasChildren) { this.loadHierachyThenPoints(); } else { this.loadPoints(); } } else { this.pcoGeometry.dispatchEvent({type:'updateNodeMaxLevel',level:this.level});//add this.loadPoints(); } } loadPoints(){ this.pcoGeometry.loader.load(this); } loadHierachyThenPoints(){ let node = this; // load hierarchy let callback = function (node, hbuffer) { let tStart = performance.now(); let view = new DataView(hbuffer); let stack = []; let children = view.getUint8(0); let numPoints = view.getUint32(1, true); node.numPoints = numPoints; stack.push({children: children, numPoints: numPoints, name: node.name}); let decoded = []; let offset = 5; while (stack.length > 0) { let snode = stack.shift(); let mask = 1; for (let i = 0; i < 8; i++) { if ((snode.children & mask) !== 0) { let childName = snode.name + i; let childChildren = view.getUint8(offset); let childNumPoints = view.getUint32(offset + 1, true); stack.push({children: childChildren, numPoints: childNumPoints, name: childName}); decoded.push({children: childChildren, numPoints: childNumPoints, name: childName}); offset += 5; } mask = mask * 2; } if (offset === hbuffer.byteLength) { break; } } // console.log(decoded); let nodes = {}; nodes[node.name] = node; let pco = node.pcoGeometry; for (let i = 0; i < decoded.length; i++) { let name = decoded[i].name; let decodedNumPoints = decoded[i].numPoints; let index = parseInt(name.charAt(name.length - 1)); let parentName = name.substring(0, name.length - 1); let parentNode = nodes[parentName]; let level = name.length - 1; let boundingBox = Utils.createChildAABB(parentNode.boundingBox, index); let currentNode = new PointCloudOctreeGeometryNode(name, pco, boundingBox); currentNode.level = level; currentNode.numPoints = decodedNumPoints; currentNode.hasChildren = decoded[i].children > 0; currentNode.spacing = pco.spacing / Math.pow(2, level); parentNode.addChild(currentNode); nodes[name] = currentNode; } let duration = performance.now() - tStart; if(duration > 5){ let msg = `duration: ${duration}ms, numNodes: ${decoded.length}`; console.log(msg); } node.loadPoints(); }; if ((node.level % node.pcoGeometry.hierarchyStepSize) === 0) { // let hurl = node.pcoGeometry.octreeDir + "/../hierarchy/" + node.name + ".hrc"; let hurl = node.pcoGeometry.octreeDir + '/' + node.getHierarchyPath() + '/' + node.name + '.hrc'; let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', hurl, true); xhr.responseType = 'arraybuffer'; xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200 || xhr.status === 0) { let hbuffer = xhr.response; callback(node, hbuffer); } else { console.log('Failed to load file! HTTP status: ' + xhr.status + ', file: ' + hurl); Potree.numNodesLoading--; } } }; try { xhr.send(null); } catch (e) { console.log('fehler beim laden der punktwolke: ' + e); } } } getNumPoints(){ return this.numPoints; } dispose(){ if (this.geometry && this.parent != null) { this.geometry.dispose(); this.geometry = null; this.loaded = false; this.dispatchEvent( { type: 'dispose' } ); for (let i = 0; i < this.oneTimeDisposeHandlers.length; i++) { let handler = this.oneTimeDisposeHandlers[i]; handler(); } this.oneTimeDisposeHandlers = []; } } } PointCloudOctreeGeometryNode.IDCount = 0; // ------------------------------------------- // to get a ready to use gradient array from a chroma.js gradient: // http://gka.github.io/chroma.js/ // ------------------------------------------- // // let stops = []; // for(let i = 0; i <= 10; i++){ // let range = chroma.scale(['yellow', 'navy']).mode('lch').domain([10,0])(i)._rgb // .slice(0, 3) // .map(v => (v / 255).toFixed(4)) // .join(", "); // // let line = `[${i / 10}, new THREE.Color(${range})],`; // // stops.push(line); // } // stops.join("\n"); // // // // ------------------------------------------- // to get a ready to use gradient array from matplotlib: // ------------------------------------------- // import matplotlib.pyplot as plt // import matplotlib.colors as colors // // norm = colors.Normalize(vmin=0,vmax=1) // cmap = plt.cm.viridis // // for i in range(0,11): // u = i / 10 // rgb = cmap(norm(u))[0:3] // rgb = ["{0:.3f}".format(v) for v in rgb] // rgb = "[" + str(u) + ", new THREE.Color(" + ", ".join(rgb) + ")]," // print(rgb) let Gradients = { // From chroma spectral http://gka.github.io/chroma.js/ SPECTRAL: [ [0, new Color(0.3686, 0.3098, 0.6353)], [0.1, new Color(0.1961, 0.5333, 0.7412)], [0.2, new Color(0.4000, 0.7608, 0.6471)], [0.3, new Color(0.6706, 0.8667, 0.6431)], [0.4, new Color(0.9020, 0.9608, 0.5961)], [0.5, new Color(1.0000, 1.0000, 0.7490)], [0.6, new Color(0.9961, 0.8784, 0.5451)], [0.7, new Color(0.9922, 0.6824, 0.3804)], [0.8, new Color(0.9569, 0.4275, 0.2627)], [0.9, new Color(0.8353, 0.2431, 0.3098)], [1, new Color(0.6196, 0.0039, 0.2588)] ], PLASMA: [ [0.0, new Color(0.241, 0.015, 0.610)], [0.1, new Color(0.387, 0.001, 0.654)], [0.2, new Color(0.524, 0.025, 0.653)], [0.3, new Color(0.651, 0.125, 0.596)], [0.4, new Color(0.752, 0.227, 0.513)], [0.5, new Color(0.837, 0.329, 0.431)], [0.6, new Color(0.907, 0.435, 0.353)], [0.7, new Color(0.963, 0.554, 0.272)], [0.8, new Color(0.992, 0.681, 0.195)], [0.9, new Color(0.987, 0.822, 0.144)], [1.0, new Color(0.940, 0.975, 0.131)] ], YELLOW_GREEN: [ [0, new Color(0.1647, 0.2824, 0.3451)], [0.1, new Color(0.1338, 0.3555, 0.4227)], [0.2, new Color(0.0610, 0.4319, 0.4864)], [0.3, new Color(0.0000, 0.5099, 0.5319)], [0.4, new Color(0.0000, 0.5881, 0.5569)], [0.5, new Color(0.1370, 0.6650, 0.5614)], [0.6, new Color(0.2906, 0.7395, 0.5477)], [0.7, new Color(0.4453, 0.8099, 0.5201)], [0.8, new Color(0.6102, 0.8748, 0.4850)], [0.9, new Color(0.7883, 0.9323, 0.4514)], [1, new Color(0.9804, 0.9804, 0.4314)] ], VIRIDIS: [ [0.0, new Color(0.267, 0.005, 0.329)], [0.1, new Color(0.283, 0.141, 0.458)], [0.2, new Color(0.254, 0.265, 0.530)], [0.3, new Color(0.207, 0.372, 0.553)], [0.4, new Color(0.164, 0.471, 0.558)], [0.5, new Color(0.128, 0.567, 0.551)], [0.6, new Color(0.135, 0.659, 0.518)], [0.7, new Color(0.267, 0.749, 0.441)], [0.8, new Color(0.478, 0.821, 0.318)], [0.9, new Color(0.741, 0.873, 0.150)], [1.0, new Color(0.993, 0.906, 0.144)] ], INFERNO: [ [0.0, new Color(0.077, 0.042, 0.206)], [0.1, new Color(0.225, 0.036, 0.388)], [0.2, new Color(0.373, 0.074, 0.432)], [0.3, new Color(0.522, 0.128, 0.420)], [0.4, new Color(0.665, 0.182, 0.370)], [0.5, new Color(0.797, 0.255, 0.287)], [0.6, new Color(0.902, 0.364, 0.184)], [0.7, new Color(0.969, 0.516, 0.063)], [0.8, new Color(0.988, 0.683, 0.072)], [0.9, new Color(0.961, 0.859, 0.298)], [1.0, new Color(0.988, 0.998, 0.645)] ], GRAYSCALE: [ [0, new Color(0, 0, 0)], [1, new Color(1, 1, 1)] ], // 16 samples of the TURBU color scheme // values taken from: https://gist.github.com/mikhailov-work/ee72ba4191942acecc03fe6da94fc73f // original file licensed under Apache-2.0 TURBO: [ [0.00, new Color(0.18995, 0.07176, 0.23217)], [0.07, new Color(0.25107, 0.25237, 0.63374)], [0.13, new Color(0.27628, 0.42118, 0.89123)], [0.20, new Color(0.25862, 0.57958, 0.99876)], [0.27, new Color(0.15844, 0.73551, 0.92305)], [0.33, new Color(0.09267, 0.86554, 0.7623)], [0.40, new Color(0.19659, 0.94901, 0.59466)], [0.47, new Color(0.42778, 0.99419, 0.38575)], [0.53, new Color(0.64362, 0.98999, 0.23356)], [0.60, new Color(0.80473, 0.92452, 0.20459)], [0.67, new Color(0.93301, 0.81236, 0.22667)], [0.73, new Color(0.99314, 0.67408, 0.20348)], [0.80, new Color(0.9836, 0.49291, 0.12849)], [0.87, new Color(0.92105, 0.31489, 0.05475)], [0.93, new Color(0.81608, 0.18462, 0.01809)], [1.00, new Color(0.66449, 0.08436, 0.00424)], ], RAINBOW: [ [0, new Color(0.278, 0, 0.714)], [1 / 6, new Color(0, 0, 1)], [2 / 6, new Color(0, 1, 1)], [3 / 6, new Color(0, 1, 0)], [4 / 6, new Color(1, 1, 0)], [5 / 6, new Color(1, 0.64, 0)], [1, new Color(1, 0, 0)] ], CONTOUR: [ [0.00, new Color(0, 0, 0)], [0.03, new Color(0, 0, 0)], [0.04, new Color(1, 1, 1)], [1.00, new Color(1, 1, 1)] ], }; let Shaders = {}; Shaders["pointcloud_new.vs"] = ` precision highp float; precision highp int; #define PI 3.141592653589793 #if defined(usePanoMap) uniform samplerCube pano0Map; //随便设置一个samplerCube去使用都会让点云消失 uniform samplerCube pano1Map; uniform float progress; uniform float easeInOutRatio; uniform vec3 pano0Position; uniform mat4 pano0Matrix; uniform vec3 pano1Position; uniform mat4 pano1Matrix; /* varying vec3 vWorldPosition0; varying vec3 vWorldPosition1; */ #endif //-------------- attribute vec3 position; attribute vec3 color; attribute float intensity; attribute float classification; attribute float returnNumber; attribute float numberOfReturns; attribute float pointSourceID; attribute vec4 indices; //每个点的index attribute float spacing; attribute float gpsTime; attribute vec3 normal; attribute float aExtra; uniform mat4 modelMatrix; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; uniform mat4 viewMatrix; uniform mat4 uViewInv; //uniform float uScreenWidth; //uniform float uScreenHeight; uniform vec2 resolution; uniform float fov; uniform float near; uniform float far; uniform bool uDebug; uniform bool uUseOrthographicCamera; uniform float uOrthoWidth; uniform float uOrthoHeight; #define CLIPTASK_NONE 0 #define CLIPTASK_HIGHLIGHT 1 #define CLIPTASK_SHOW_INSIDE 2 #define CLIPTASK_SHOW_OUTSIDE 3 #define CLIPMETHOD_INSIDE_ANY 0 #define CLIPMETHOD_INSIDE_ALL 1 //最外层裁剪(下载) #if defined(bigClipInBox) uniform mat4 clipBoxBig_in; #endif //内层裁剪 #if defined(num_in_clipboxes) && num_in_clipboxes > 0 uniform mat4 clipBoxes_in[num_in_clipboxes]; #endif #if defined(num_out_clipboxes) && num_out_clipboxes > 0 uniform mat4 clipBoxes_out[num_out_clipboxes]; #endif #if defined(num_clipspheres) && num_clipspheres > 0 uniform mat4 uClipSpheres[num_clipspheres]; #endif #if defined(num_highlightBox) && num_highlightBox > 0 uniform mat4 boxes_highlight[num_highlightBox]; #endif #if defined(num_prism) && num_prism > 0 uniform mat3 prismList[num_prism]; uniform vec2 prismPoints[prismPointCountSum]; #endif #if defined(showBaseHeight) uniform sampler2D baseHeightAreaMap ; uniform vec2 baseHeightBoundZ; uniform vec4 baseHeightBoundXY; #endif uniform float size; uniform float minSize; uniform float maxSize; uniform float orthoMaxSize;//add uniform float uPCIndex; uniform float uOctreeSpacing; uniform float uNodeSpacing; uniform float uOctreeSize; uniform vec3 uBBSize; uniform float uLevel; uniform float levelPercent;//add uniform float uVNStart; uniform bool uIsLeafNode; uniform vec3 uColor; uniform float uOpacity; varying float vOpacity; //add uniform vec2 elevationRange; uniform vec2 intensityRange; uniform vec2 uFilterReturnNumberRange; uniform vec2 uFilterNumberOfReturnsRange; uniform vec2 uFilterPointSourceIDClipRange; uniform vec2 uFilterGPSTimeClipRange; //uniform float ufilterByNormalThreshold; uniform float uGpsScale; uniform float uGpsOffset; uniform vec2 uNormalizedGpsBufferRange; uniform vec3 uIntensity_gbc; uniform vec3 uRGB_gbc; uniform vec3 uExtra_gbc; uniform float uTransition; uniform float wRGB; uniform float wIntensity; uniform float wElevation; uniform float wClassification; uniform float wReturnNumber; uniform float wSourceID; uniform vec2 uExtraNormalizedRange; uniform vec2 uExtraRange; uniform float uExtraScale; uniform float uExtraOffset; uniform vec3 uShadowColor; uniform sampler2D visibleNodes; uniform sampler2D gradient; uniform sampler2D classificationLUT; #if defined(color_type_matcap) uniform sampler2D matcapTextureUniform; #endif uniform bool backfaceCulling; #if defined(num_shadowmaps) && num_shadowmaps > 0 uniform sampler2D uShadowMap[num_shadowmaps]; uniform mat4 uShadowWorldView[num_shadowmaps]; uniform mat4 uShadowProj[num_shadowmaps]; #endif varying vec3 vColor; varying float vLogDepth; varying vec3 vViewPosition; varying float vRadius; varying float vPointSize; float Round(float number){ return floor(number + 0.5); } // // ### ######## ### ######## ######## #### ## ## ######## ###### #### ######## ######## ###### // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## // ## ## ## ## ## ## ######## ## ## ## ## ###### ###### ## ## ###### ###### // ######### ## ## ######### ## ## ## ## ## ## ## ## ## ## ## // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## // ## ## ######## ## ## ## ## #### ### ######## ###### #### ######## ######## ###### // // --------------------- // OCTREE // --------------------- #if (defined(adaptive_point_size) || defined(color_type_level_of_detail)) && defined(tree_type_octree) /** * number of 1-bits up to inclusive index position * number is treated as if it were an integer in the range 0-255 * */ int numberOfOnes(int number, int index){ int numOnes = 0; int tmp = 128; for(int i = 7; i >= 0; i--){ if(number >= tmp){ number = number - tmp; if(i <= index){ numOnes++; } } tmp = tmp / 2; } return numOnes; } /** * checks whether the bit at index is 1 * number is treated as if it were an integer in the range 0-255 * */ bool isBitSet(int number, int index){ // weird multi else if due to lack of proper array, int and bitwise support in WebGL 1.0 int powi = 1; if(index == 0){ powi = 1; }else if(index == 1){ powi = 2; }else if(index == 2){ powi = 4; }else if(index == 3){ powi = 8; }else if(index == 4){ powi = 16; }else if(index == 5){ powi = 32; }else if(index == 6){ powi = 64; }else if(index == 7){ powi = 128; }else{ return false; } int ndp = number / powi; return mod(float(ndp), 2.0) != 0.0; } /** * find the LOD at the point position */ float getLOD(){//////we use this vec3 offset = vec3(0.0, 0.0, 0.0); int iOffset = int(uVNStart); float depth = uLevel; for(float i = 0.0; i <= 30.0; i++){ float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0); vec3 index3d = (position-offset) / nodeSizeAtLevel; index3d = floor(index3d + 0.5); int index = int(Round(4.0 * index3d.x + 2.0 * index3d.y + index3d.z)); vec4 value = texture2D(visibleNodes, vec2(float(iOffset) / 2048.0, 0.0)); int mask = int(Round(value.r * 255.0)); if(isBitSet(mask, index)){ // there are more visible child nodes at this position int advanceG = int(Round(value.g * 255.0)) * 256; int advanceB = int(Round(value.b * 255.0)); int advanceChild = numberOfOnes(mask, index - 1); int advance = advanceG + advanceB + advanceChild; iOffset = iOffset + advance; depth++; }else{ // no more visible child nodes at this position //return value.a * 255.0; float lodOffset = (255.0 * value.a) / 10.0 - 10.0; return depth + lodOffset; } offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d; } return depth; } float getSpacing(){ vec3 offset = vec3(0.0, 0.0, 0.0); int iOffset = int(uVNStart); float depth = uLevel; float spacing = uNodeSpacing; for(float i = 0.0; i <= 30.0; i++){ float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0); vec3 index3d = (position-offset) / nodeSizeAtLevel; index3d = floor(index3d + 0.5); int index = int(Round(4.0 * index3d.x + 2.0 * index3d.y + index3d.z)); vec4 value = texture2D(visibleNodes, vec2(float(iOffset) / 2048.0, 0.0)); int mask = int(Round(value.r * 255.0)); float spacingFactor = value.a; if(i > 0.0){ spacing = spacing / (255.0 * spacingFactor); } if(isBitSet(mask, index)){ // there are more visible child nodes at this position int advanceG = int(Round(value.g * 255.0)) * 256; int advanceB = int(Round(value.b * 255.0)); int advanceChild = numberOfOnes(mask, index - 1); int advance = advanceG + advanceB + advanceChild; iOffset = iOffset + advance; //spacing = spacing / (255.0 * spacingFactor); //spacing = spacing / 3.0; depth++; }else{ // no more visible child nodes at this position return spacing; } offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d; } return spacing; } float getPointSizeAttenuation(){ return pow(2.0, getLOD()); } #endif // --------------------- // KD-TREE // --------------------- #if (defined(adaptive_point_size) || defined(color_type_level_of_detail)) && defined(tree_type_kdtree) float getLOD(){ vec3 offset = vec3(0.0, 0.0, 0.0); float iOffset = 0.0; float depth = 0.0; vec3 size = uBBSize; vec3 pos = position; for(float i = 0.0; i <= 1000.0; i++){ vec4 value = texture2D(visibleNodes, vec2(iOffset / 2048.0, 0.0)); int children = int(value.r * 255.0); float next = value.g * 255.0; int split = int(value.b * 255.0); if(next == 0.0){ return depth; } vec3 splitv = vec3(0.0, 0.0, 0.0); if(split == 1){ splitv.x = 1.0; }else if(split == 2){ splitv.y = 1.0; }else if(split == 4){ splitv.z = 1.0; } iOffset = iOffset + next; float factor = length(pos * splitv / size); if(factor < 0.5){ // left if(children == 0 || children == 2){ return depth; } }else{ // right pos = pos - size * splitv * 0.5; if(children == 0 || children == 1){ return depth; } if(children == 3){ iOffset = iOffset + 1.0; } } size = size * ((1.0 - (splitv + 1.0) / 2.0) + 0.5); depth++; } return depth; } float getPointSizeAttenuation(){ return 0.5 * pow(1.3, getLOD()); } #endif // // ### ######## ######## ######## #### ######## ## ## ######## ######## ###### // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## // ## ## ## ## ## ## ## ## ## ## ## ## ## ## // ## ## ## ## ######## ## ######## ## ## ## ###### ###### // ######### ## ## ## ## ## ## ## ## ## ## ## ## // ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## // ## ## ## ## ## ## #### ######## ####### ## ######## ###### // // formula adapted from: http://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-5-contrast-adjustment/ float getContrastFactor(float contrast){ return (1.0158730158730156 * (contrast + 1.0)) / (1.0158730158730156 - contrast); } vec3 getRGB(){ vec3 rgb = color; rgb = pow(rgb, vec3(uRGB_gbc.x)); rgb = rgb + uRGB_gbc.y; rgb = (rgb - 0.5) * getContrastFactor(uRGB_gbc.z) + 0.5; rgb = clamp(rgb, 0.0, 1.0); return rgb; } float getIntensity(){ float w = (intensity - intensityRange.x) / (intensityRange.y - intensityRange.x); w = pow(w, uIntensity_gbc.x); w = w + uIntensity_gbc.y; w = (w - 0.5) * getContrastFactor(uIntensity_gbc.z) + 0.5; w = clamp(w, 0.0, 1.0); return w; } vec3 getGpsTime(){ float w = (gpsTime + uGpsOffset) * uGpsScale; vec3 c = texture2D(gradient, vec2(w, 1.0 - w)).rgb; // vec2 r = uNormalizedGpsBufferRange; // float w = gpsTime * (r.y - r.x) + r.x; // w = clamp(w, 0.0, 1.0); // vec3 c = texture2D(gradient, vec2(w,1.0-w)).rgb; return c; } vec3 getElevation(vec4 world){ float w = (world.z - elevationRange.x) / (elevationRange.y - elevationRange.x); vec3 cElevation = texture2D(gradient, vec2(w,1.0-w)).rgb; return cElevation; } vec4 getClassification(){ vec2 uv = vec2(classification / 255.0, 0.5); vec4 classColor = texture2D(classificationLUT, uv); return classColor; } vec3 getReturns(){ // 0b 00_000_111 float rn = mod(returnNumber, 8.0); // 0b 00_111_000 float nr = mod(returnNumber / 8.0, 8.0); if(nr <= 1.0){ return vec3(1.0, 0.0, 0.0); }else{ return vec3(0.0, 1.0, 0.0); } // return vec3(nr / 4.0, 0.0, 0.0); // if(nr == 1.0){ // return vec3(1.0, 1.0, 0.0); // }else{ // if(rn == 1.0){ // return vec3(1.0, 0.0, 0.0); // }else if(rn == nr){ // return vec3(0.0, 0.0, 1.0); // }else{ // return vec3(0.0, 1.0, 0.0); // } // } // if(numberOfReturns == 1.0){ // return vec3(1.0, 1.0, 0.0); // }else{ // if(returnNumber == 1.0){ // return vec3(1.0, 0.0, 0.0); // }else if(returnNumber == numberOfReturns){ // return vec3(0.0, 0.0, 1.0); // }else{ // return vec3(0.0, 1.0, 0.0); // } // } } vec3 getReturnNumber(){ if(numberOfReturns == 1.0){ return vec3(1.0, 1.0, 0.0); }else{ if(returnNumber == 1.0){ return vec3(1.0, 0.0, 0.0); }else if(returnNumber == numberOfReturns){ return vec3(0.0, 0.0, 1.0); }else{ return vec3(0.0, 1.0, 0.0); } } } vec3 getNumberOfReturns(){ float value = numberOfReturns; float w = value / 6.0; vec3 color = texture2D(gradient, vec2(w, 1.0 - w)).rgb; return color; } vec3 getSourceID(){ float w = mod(pointSourceID, 10.0) / 10.0; return texture2D(gradient, vec2(w,1.0 - w)).rgb; } vec3 getCompositeColor(vec4 world){ vec3 c; float w; c += wRGB * getRGB(); w += wRGB; c += wIntensity * getIntensity() * vec3(1.0, 1.0, 1.0); w += wIntensity; c += wElevation * getElevation(world); w += wElevation; c += wReturnNumber * getReturnNumber(); w += wReturnNumber; c += wSourceID * getSourceID(); w += wSourceID; vec4 cl = wClassification * getClassification(); c += cl.a * cl.rgb; w += wClassification * cl.a; c = c / w; if(w == 0.0){ //c = color; gl_Position = vec4(100.0, 100.0, 100.0, 0.0); } return c; } vec3 getNormal(){ //vec3 n_hsv = vec3( modelMatrix * vec4( normal, 0.0 )) * 0.5 + 0.5; // (n_world.xyz + vec3(1.,1.,1.)) / 2.; vec3 n_view = normalize( vec3(modelViewMatrix * vec4( normal, 0.0 )) ); return n_view; } bool applyBackfaceCulling() { // Black not facing vertices / Backface culling vec3 e = normalize(vec3(modelViewMatrix * vec4( position, 1. ))); vec3 n = getNormal(); // normalize( vec3(modelViewMatrix * vec4( normal, 0.0 )) ); if((uUseOrthographicCamera && n.z <= 0.) || (!uUseOrthographicCamera && dot( n, e ) >= 0.)) { return true; } else { return false; } } #if defined(color_type_matcap) // Matcap Material vec3 getMatcap(){ vec3 eye = normalize( vec3( modelViewMatrix * vec4( position, 1. ) ) ); if(uUseOrthographicCamera) { eye = vec3(0., 0., -1.); } vec3 r_en = reflect( eye, getNormal() ); // or r_en = e - 2. * dot( n, e ) * n; float m = 2. * sqrt(pow( r_en.x, 2. ) + pow( r_en.y, 2. ) + pow( r_en.z + 1., 2. )); vec2 vN = r_en.xy / m + .5; return texture2D(matcapTextureUniform, vN).rgb; } #endif vec3 getExtra(){ float w = (aExtra + uExtraOffset) * uExtraScale; w = clamp(w, 0.0, 1.0); vec3 color = texture2D(gradient, vec2(w,1.0-w)).rgb; // vec2 r = uExtraNormalizedRange; // float w = aExtra * (r.y - r.x) + r.x; // w = (w - uExtraRange.x) / (uExtraRange.y - uExtraRange.x); // w = clamp(w, 0.0, 1.0); // vec3 color = texture2D(gradient, vec2(w,1.0-w)).rgb; return color; } vec3 getColor(vec4 world){ vec3 color; #ifdef color_type_rgba color = getRGB(); #elif defined color_type_height || defined color_type_elevation color = getElevation(world); #elif defined color_type_rgb_height vec3 cHeight = getElevation(); color = (1.0 - uTransition) * getRGB() + uTransition * cHeight; #elif defined color_type_depth float linearDepth = gl_Position.w; float expDepth = (gl_Position.z / gl_Position.w) * 0.5 + 0.5; color = vec3(linearDepth, expDepth, 0.0); //color = vec3(1.0, 0.5, 0.3); #elif defined color_type_intensity float w = getIntensity(); color = vec3(w, w, w); #elif defined color_type_gps_time color = getGpsTime(); #elif defined color_type_intensity_gradient float w = getIntensity(); color = texture2D(gradient, vec2(w,1.0-w)).rgb; #elif defined color_type_color color = uColor; #elif defined color_type_level_of_detail float depth = getLOD(); float w = depth / 10.0; color = texture2D(gradient, vec2(w,1.0-w)).rgb; #elif defined color_type_indices color = indices.rgb; #elif defined color_type_classification vec4 cl = getClassification(); color = cl.rgb; #elif defined color_type_return_number color = getReturnNumber(); #elif defined color_type_returns color = getReturns(); #elif defined color_type_number_of_returns color = getNumberOfReturns(); #elif defined color_type_source_id color = getSourceID(); #elif defined color_type_point_source_id color = getSourceID(); #elif defined color_type_normal color = (modelMatrix * vec4(normal, 0.0)).xyz; #elif defined color_type_phong color = color; #elif defined color_type_composite color = getCompositeColor(world); #elif defined color_type_matcap color = getMatcap(); #elif defined color_type_heightCpt //add color = vec3(0.0,0.0,0.0); #else color = getExtra(); #endif if (backfaceCulling && applyBackfaceCulling()){ //color = vec3(0.); } //applyBackfaceCulling直接返回false或者注释color = vec3(0.);都没问题 return color; } float getPointSize(){ float pointSize = 1.0; float maxSize_ = maxSize; float slope = tan(fov / 2.0); float projFactor = -0.5 * resolution.y / (slope * vViewPosition.z); /* float scale = length( modelViewMatrix * vec4(0, 0, 0, 1) - modelViewMatrix * vec4(uOctreeSpacing, 0, 0, 1) ) / uOctreeSpacing; projFactor = projFactor * scale; */ float r = uOctreeSpacing * 1.7; //vRadius = r; #if defined fixed_point_size pointSize = size; #elif defined attenuated_point_size if(uUseOrthographicCamera){ pointSize = size / uOrthoWidth * resolution.x; //改成近似adaptive_point_size根据窗口缩放。其实就是size * zoom (size单位转化为米,如size为1代表一个点云在场景中1米宽,zoom代表1px=1/zoom米) maxSize_ = orthoMaxSize; //for panoEditor, when zoom in, need more details, rather than always same size }else{ //近大远小,模拟真实mesh,边缘放大 //pointSize = size * spacing * projFactor; //spacing是attribute 为空 如果有这个值就能更自适应填补 //pointSize = size * uOctreeSpacing * projFactor / 18.0; //直接用cloud的spacing里,不过因为都一样所以可能没有什么意义 //pointSize = pointSize * projFactor; pointSize = size * projFactor ; } #elif defined adaptive_point_size if(uUseOrthographicCamera) { float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation(); pointSize = (worldSpaceSize / uOrthoWidth) * resolution.x; //uScreenWidth; maxSize_ = orthoMaxSize; } else { float worldSpaceSize = 1.0 * size * r / getPointSizeAttenuation(); pointSize = worldSpaceSize * projFactor; } #endif pointSize = max(minSize, pointSize); pointSize = min(maxSize_, pointSize); vRadius = pointSize / projFactor; return pointSize; } bool insideBox(mat4 clipBox, vec4 worldPos){//add vec4 clipPosition = clipBox * worldPos; bool inside = -0.5 <= clipPosition.x && clipPosition.x <= 0.5; inside = inside && -0.5 <= clipPosition.y && clipPosition.y <= 0.5; inside = inside && -0.5 <= clipPosition.z && clipPosition.z <= 0.5; return inside; } #if defined(color_type_heightCpt) vec3 percentToByte(float num){ //输出是0-1, shader精度不够,只够存3个数 float a = 1.0; float result[4]; for(int i=0;i<3;i++){ a = i == 2 ? a/255.0 : a / 256.0 ; //分成256份,其中255份给当前位置,最后一份给后一个位置,而到了最后一个位置时没有下一个位置了所以只分成255份 if(num > a){ float c = num / a; float r = floor(c); r = min(255.0, r); result[i] = r; num -= r * a; }else{ result[i] = 0.0; } } return vec3(result[0]/255.0, result[1]/255.0,result[2]/255.0); //除以多少255还是256? 参考uPCIndex / 255.0 应该是255 } #endif #if defined(num_prism) && num_prism > 0 int insidePrism(mat3 prismInfo, int pointIndexStart, vec4 worldPos){//是否在棱柱里 float zMin = prismInfo[0][0]; float zMid = prismInfo[0][1]; float zMax = prismInfo[0][2]; float xMin = prismInfo[1][0]; float xMax = prismInfo[1][1]; float yMin = prismInfo[1][2]; float yMax = prismInfo[2][0]; int pointCount = int(Round(prismInfo[2][1])); if( worldPos.x < xMin || worldPos.x > xMax || worldPos.y < yMin || worldPos.y > yMax)return 0; #ifndef showBaseHeight if( worldPos.z < zMin || worldPos.z > zMax) return 0; #endif bool inside = false; int j=pointCount-1; for(int i=0; i=pointCount)break; float xi = prismPoints[i+pointIndexStart].x, yi = prismPoints[i+pointIndexStart].y, xj = prismPoints[j+pointIndexStart].x, yj = prismPoints[j+pointIndexStart].y; if(((yi > worldPos.y) != (yj > worldPos.y)) && (worldPos.x < (xj - xi) * (worldPos.y - yi) / (yj - yi) + xi)){ inside = !inside; } j=i; } if(inside){ #ifdef showBaseHeight return 1; #else #ifdef color_type_heightCpt vColor = percentToByte((worldPos.z - zMin) / (zMax - zMin)); #endif #endif return worldPos.z < zMid ? 1 : 2; }else return 0; } #endif #ifdef showBaseHeight float byteToFloat(vec3 color){ float percent = 0.0; float a = 1.0; for(int i=0;i<3;i++){ a = i == 2 ? a/255.0 : a/256.0; percent += a * color[i] ; } return percent * 255.0 ; } #endif void doClipping(vec4 world){ { vec4 cl = getClassification(); if(cl.a == 0.0){ gl_Position = vec4(100.0, 100.0, 100.0, 0.0); return; } } #if defined(clip_return_number_enabled) { // return number filter vec2 range = uFilterReturnNumberRange; if(returnNumber < range.x || returnNumber > range.y){ gl_Position = vec4(100.0, 100.0, 100.0, 0.0); return; } } #endif #if defined(clip_number_of_returns_enabled) { // number of return filter vec2 range = uFilterNumberOfReturnsRange; if(numberOfReturns < range.x || numberOfReturns > range.y){ gl_Position = vec4(100.0, 100.0, 100.0, 0.0); return; } } #endif #if defined(clip_gps_enabled) { // GPS time filter float time = (gpsTime + uGpsOffset) * uGpsScale; vec2 range = uFilterGPSTimeClipRange; if(time < range.x || time > range.y){ gl_Position = vec4(100.0, 100.0, 100.0, 0.0); return; } } #endif #if defined(clip_point_source_id_enabled) { // point source id filter vec2 range = uFilterPointSourceIDClipRange; if(pointSourceID < range.x || pointSourceID > range.y){ gl_Position = vec4(100.0, 100.0, 100.0, 0.0); return; } } #endif //总共三种box : 最外层可见、内层可见和不可见(外层可见和内层可见是交集,内层可见和内层不可见是并集) #if defined(bigClipInBox) if(!insideBox(clipBoxBig_in, world)){ gl_Position = vec4(100.0, 100.0, 100.0, 1.0); return;; } #endif #if defined(num_in_clipboxes) && num_in_clipboxes > 0 //当有可见box时,需要在任一可见box内才可见 bool visi1 = false; for(int i = 0; i < num_in_clipboxes; i++){ if(insideBox(clipBoxes_in[i], world)){ visi1 = true; break; } } if(!visi1){ gl_Position = vec4(100.0, 100.0, 100.0, 1.0); return; } #endif #if defined(num_out_clipboxes) && num_out_clipboxes > 0 //当有不可见box时,不在所有不可见box内才可见 bool visi2 = true; for(int i = 0; i < num_out_clipboxes; i++){ if(insideBox(clipBoxes_out[i], world)){ visi2 = false; break; } } if(!visi2){ gl_Position = vec4(100.0, 100.0, 100.0, 1.0); return; } #endif #if defined(num_highlightBox) && num_highlightBox > 0 //当有高亮box时,需要在任一可见高亮内都高宽 bool highlight = false; for(int i = 0; i < num_highlightBox; i++){ if(insideBox(boxes_highlight[i], world)){ highlight = true; break; } } if(highlight){ vColor.r += 0.5; } #endif int highlight_ = 0; #if defined(num_prism) && num_prism > 0 //基于prism棱柱的土方量显示或计算 int pointIndexStart = 0; for(int i = 0; i < num_prism; i++){ highlight_ = insidePrism(prismList[i], pointIndexStart, world); if(highlight_>0){ #if !defined(showBaseHeight) && !defined(color_type_heightCpt) if(highlight_ == 1){ vColor.r += 0.5; }else if(highlight_ == 2){ vColor.g += 0.5; } #endif break; } pointIndexStart += int(Round(prismList[i][2][1])); } #ifdef color_type_heightCpt if(highlight_==0){ gl_Position = vec4(100.0, 100.0, 100.0, 1.0); //unvisible return; } #endif #endif #ifdef showBaseHeight //基于模型的土方量显示或计算 bool outsidePrism = false; #if defined(num_prism) && num_prism > 0//高亮交集 if( highlight_ == 0 ){ outsidePrism = true; } #endif if(!outsidePrism){ float zMin = baseHeightBoundZ.x; float zMax = baseHeightBoundZ.y; float xMin = baseHeightBoundXY.x; float xMax = baseHeightBoundXY.y; float yMin = baseHeightBoundXY.z; float yMax = baseHeightBoundXY.w; bool inside = false; if(world.z < zMin || world.z > zMax || world.x < xMin || world.x > xMax || world.y < yMin || world.y > yMax){ inside = false; }else{ vec2 uv = vec2((world.x - xMin) / (xMax - xMin), (world.y - yMin) / (yMax - yMin) ); vec4 color = texture2D(baseHeightAreaMap,uv); //抗锯齿待写 if(color.a==1.0){ float currentHeight = (world.z - zMin) / (zMax - zMin) ; #ifdef color_type_heightCpt /*float diff = currentHeight - baseHeight; if(diff<0.0){ diff = -diff; vOpacity = 0.5;//需要透明通道,容易出错 }else{ vOpacity = 1.0; } vColor = percentToByte(diff); */ vColor = percentToByte(currentHeight); //vColor = color.rgb; vOpacity = 1.0; #else //vColor = color.rgb; float baseHeight = byteToFloat(color.rgb); if(currentHeight > baseHeight){ vColor.g += 0.5; }else{ vColor.r += 0.5; } #endif inside = true; }else{ inside = false; } } if(inside == false){ #ifdef color_type_heightCpt gl_Position = vec4(100.0, 100.0, 100.0, 1.0); //unvisible return; #endif } } #endif } // // ## ## ### #### ## ## // ### ### ## ## ## ### ## // #### #### ## ## ## #### ## // ## ### ## ## ## ## ## ## ## // ## ## ######### ## ## #### // ## ## ## ## ## ## ### // ## ## ## ## #### ## ## // vec2 getSamplerCoord( vec3 direction ) { direction = normalize(direction); float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5; float ty=acos(direction.z)/PI; return vec2(tx,ty); } vec3 transformAxis( vec3 direction ) //navvis->4dkk { float y = direction.y; direction.y = direction.z; direction.z = -y; return direction; } void main() { //bool filtered_by_normal = false; float normalZ = 0.0; #ifdef use_filter_by_normal /*if(abs(getNormal().z) > 0.4) { //ufilterByNormalThreshold 暂定 3 // Move point outside clip space space to discard it. //gl_Position = vec4(0.0, 0.0, 2.0, 1.0); //gl_Position的可视区域是 x,y,z都是[-1,1] //return; //filtered_by_normal = true; //标记一下。不直接不绘制,因为有的法线都是垂直向上 }*/ normalZ = abs(getNormal().z); #endif vec4 worldPos = modelMatrix * vec4(position, 1.0); vec4 mvPosition = modelViewMatrix * vec4(position, 1.0 ); //vec4 mvPosition = viewMatrix * worldPos; vViewPosition = mvPosition.xyz; gl_Position = projectionMatrix * mvPosition; vLogDepth = log2(-mvPosition.z); // COLOR //加------------------- #if defined(usePanoMap) vec3 positionLocalToPanoCenter0 = worldPos.xyz - pano0Position; vec3 vWorldPosition0 = (vec4(positionLocalToPanoCenter0, 1.0) * pano0Matrix).xyz; vWorldPosition0.x *= -1.0; vWorldPosition0 = transformAxis(vWorldPosition0); vec3 positionLocalToPanoCenter1 = worldPos.xyz - pano1Position; vec3 vWorldPosition1 = (vec4(positionLocalToPanoCenter1, 1.0) * pano1Matrix).xyz; vWorldPosition1.x *= -1.0; vWorldPosition1 = transformAxis(vWorldPosition1); /* vec2 samplerCoord0 = getSamplerCoord(vWorldPosition0.xyz); vec2 samplerCoord1 = getSamplerCoord(vWorldPosition1.xyz); vec4 colorFromPano0 = texture2D(pano0Map,samplerCoord0); vec4 colorFromPano1 = texture2D(pano1Map,samplerCoord1); */ vec4 colorFromPano0=textureCube(pano0Map,vWorldPosition0.xyz); vec4 colorFromPano1=textureCube(pano1Map,vWorldPosition1.xyz); vColor = mix(colorFromPano0,colorFromPano1,progress).xyz; //float easeInOutRatio = 0.0; //缓冲,渐变点云到贴图的颜色 if(progress < easeInOutRatio){ float easeProgress = (easeInOutRatio - progress) / easeInOutRatio; vec3 vColor1 = getColor(worldPos); vColor = mix(vColor,vColor1,easeProgress); }else if(progress > 1.0 - easeInOutRatio){ float easeProgress = (progress - (1.0 - easeInOutRatio) ) / easeInOutRatio; vec3 vColor1 = getColor(worldPos); vColor = mix(vColor,vColor1,easeProgress); } #else vColor = getColor(worldPos); #endif //------------------- if(uOpacity>=1.0 || uUseOrthographicCamera){ vOpacity = uOpacity; if(uOpacity<1.0){ //uUseOrthographicCamera //防止缩小后点云几乎看不见 vOpacity *= 4.0-3.0 * levelPercent; } }else{ //#ifdef attenuated_opacity zoom不会改变z 所以这并不是用在分屏时候的 float v = -gl_Position.z-1.0 ; // 范围从-2到0, e的-2到0次方的范围是0.818到1 //vOpacity = uOpacity * exp(v/ (levelPercent * 20.0 ) ); //近处加深,远处变淡 gl_Position.z似乎朝向屏幕里为正 float r = clamp( pow(1.1, v/10.0 ), 0.1, 1.0 ); //除以的数字越大,近高远低的程度越小,范围越长。 程度太高远处单薄的区域看不见 pow的底数越大改变率越大 vOpacity = uOpacity * r ; } vOpacity *= max(0.3, pow((1.0 - normalZ),5.0));//垂直朝相机时降低透明度 //vOpacity = clamp(vOpacity, 0.0, 1.0); // POINT SIZE float pointSize = getPointSize(); gl_PointSize = pointSize; vPointSize = pointSize; // only for "replacing" approaches // if(getLOD() != uLevel){ // gl_Position = vec4(10.0, 10.0, 10.0, 1.0); // } #if defined hq_depth_pass float originalDepth = gl_Position.w; float adjustedDepth = originalDepth + 2.0 * vRadius; float adjust = adjustedDepth / originalDepth; mvPosition.xyz = mvPosition.xyz * adjust; gl_Position = projectionMatrix * mvPosition; #endif // CLIPPING doClipping(worldPos); #if defined(num_clipspheres) && num_clipspheres > 0 for(int i = 0; i < num_clipspheres; i++){ vec4 sphereLocal = uClipSpheres[i] * mvPosition; float distance = length(sphereLocal.xyz); if(distance < 1.0){ float w = distance; vec3 cGradient = texture2D(gradient, vec2(w, 1.0 - w)).rgb; vColor = cGradient; //vColor = cGradient * 0.7 + vColor * 0.3; } } #endif #if defined(num_shadowmaps) && num_shadowmaps > 0 const float sm_near = 0.1; const float sm_far = 10000.0; for(int i = 0; i < num_shadowmaps; i++){ vec3 viewPos = (uShadowWorldView[i] * vec4(position, 1.0)).xyz; float distanceToLight = abs(viewPos.z); vec4 projPos = uShadowProj[i] * uShadowWorldView[i] * vec4(position, 1); vec3 nc = projPos.xyz / projPos.w; float u = nc.x * 0.5 + 0.5; float v = nc.y * 0.5 + 0.5; vec2 sampleStep = vec2(1.0 / (2.0*1024.0), 1.0 / (2.0*1024.0)) * 1.5; vec2 sampleLocations[9]; sampleLocations[0] = vec2(0.0, 0.0); sampleLocations[1] = sampleStep; sampleLocations[2] = -sampleStep; sampleLocations[3] = vec2(sampleStep.x, -sampleStep.y); sampleLocations[4] = vec2(-sampleStep.x, sampleStep.y); sampleLocations[5] = vec2(0.0, sampleStep.y); sampleLocations[6] = vec2(0.0, -sampleStep.y); sampleLocations[7] = vec2(sampleStep.x, 0.0); sampleLocations[8] = vec2(-sampleStep.x, 0.0); float visibleSamples = 0.0; float numSamples = 0.0; float bias = vRadius * 2.0; for(int j = 0; j < 9; j++){ vec4 depthMapValue = texture2D(uShadowMap[i], vec2(u, v) + sampleLocations[j]); float linearDepthFromSM = depthMapValue.x + bias; float linearDepthFromViewer = distanceToLight; if(linearDepthFromSM > linearDepthFromViewer){ visibleSamples += 1.0; } numSamples += 1.0; } float visibility = visibleSamples / numSamples; if(u < 0.0 || u > 1.0 || v < 0.0 || v > 1.0 || nc.x < -1.0 || nc.x > 1.0 || nc.y < -1.0 || nc.y > 1.0 || nc.z < -1.0 || nc.z > 1.0){ //vColor = vec3(0.0, 0.0, 0.2); }else{ //vColor = vec3(1.0, 1.0, 1.0) * visibility + vec3(1.0, 1.0, 1.0) * vec3(0.5, 0.0, 0.0) * (1.0 - visibility); vColor = vColor * visibility + vColor * uShadowColor * (1.0 - visibility); } } #endif } `; Shaders["pointcloud_new.fs"] = ` #if defined paraboloid_point_shape #extension GL_EXT_frag_depth : enable #endif #define PI 3.141592653589793 precision highp float; precision highp int; /* #if defined(usePanoMap) uniform samplerCube pano0Map; //随便设置一个samplerCube去使用都会让点云消失 uniform samplerCube pano1Map; uniform float progress; uniform float easeInOutRatio; uniform vec3 pano0Position; uniform mat4 pano0Matrix; uniform vec3 pano1Position; uniform mat4 pano1Matrix; varying vec3 vWorldPosition0; varying vec3 vWorldPosition1; #endif */ //------------ uniform mat4 viewMatrix; uniform mat4 uViewInv; uniform mat4 uProjInv; uniform vec3 cameraPosition; uniform mat4 projectionMatrix; //uniform float uOpacity; varying float vOpacity; //add uniform float blendHardness; uniform float blendDepthSupplement; uniform float fov; uniform float uSpacing; uniform float near; uniform float far; uniform float uPCIndex; uniform float uScreenWidth; uniform float uScreenHeight; varying vec3 vColor; varying float vLogDepth; varying vec3 vViewPosition; varying float vRadius; varying float vPointSize; varying vec3 vPosition; float specularStrength = 1.0; vec2 getSamplerCoord( vec3 direction ) { direction = normalize(direction); float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5; float ty=acos(direction.z)/PI; return vec2(tx,ty); } void main() { vec3 color = vColor; /*#if defined(usePanoMap) //加 经测试,即使全部写在fragment里也是无论pointsize多大都是一个点一个颜色,所以干脆写在vectex里 vec4 colorFromPano0=textureCube(pano0Map,vWorldPosition0.xyz); vec4 colorFromPano1=textureCube(pano1Map,vWorldPosition1.xyz); color = mix(colorFromPano0,colorFromPano1,progress).xyz; //float easeInOutRatio = 0.0; //缓冲,渐变点云到贴图的颜色 if(progress < easeInOutRatio){ float easeProgress = (easeInOutRatio - progress) / easeInOutRatio; color = mix(color,vColor,easeProgress); }else if(progress > 1.0 - easeInOutRatio){ float easeProgress = (progress - (1.0 - easeInOutRatio) ) / easeInOutRatio; color = mix(color,vColor,easeProgress); } #else color = vColor; #endif*/ float depth = gl_FragCoord.z; #if defined(circle_point_shape) || defined(paraboloid_point_shape) float u = 2.0 * gl_PointCoord.x - 1.0; float v = 2.0 * gl_PointCoord.y - 1.0; #endif #if defined(circle_point_shape) float cc = u*u + v*v; if(cc > 1.0){ discard; } #endif #if defined color_type_indices //pick point recognize gl_FragColor = vec4(color, uPCIndex / 255.0); //uPCIndex : node Index #else gl_FragColor = vec4(color, vOpacity); #endif #if defined paraboloid_point_shape float wi = 0.0 - ( u*u + v*v); vec4 pos = vec4(vViewPosition, 1.0); pos.z += wi * vRadius; float linearDepth = -pos.z; pos = projectionMatrix * pos; pos = pos / pos.w; float expDepth = pos.z; depth = (pos.z + 1.0) / 2.0; gl_FragDepthEXT = depth; gl_FragDepthEXT = clamp(gl_FragDepthEXT, 0.0, 1.0); //add #if defined(color_type_depth) color.r = linearDepth; color.g = expDepth; #endif #if defined(use_edl) gl_FragColor.a = log2(linearDepth); #endif #else #if defined(use_edl) gl_FragColor.a = vLogDepth; #endif #endif #if defined(weighted_splats) float distance = 2.0 * length(gl_PointCoord.xy - 0.5); float weight = max(0.0, 1.0 - distance); weight = pow(weight, 1.5); gl_FragColor.a = weight; gl_FragColor.xyz = gl_FragColor.xyz * weight; #endif //gl_FragColor = vec4(0.0, 0.7, 0.0, 1.0); } `; Shaders["pointcloud_sm.vs"] = ` precision mediump float; precision mediump int; attribute vec3 position; attribute vec3 color; uniform mat4 modelMatrix; uniform mat4 modelViewMatrix; uniform mat4 projectionMatrix; uniform mat4 viewMatrix; uniform float uScreenWidth; uniform float uScreenHeight; uniform float near; uniform float far; uniform float uSpacing; uniform float uOctreeSize; uniform float uLevel; uniform float uVNStart; uniform sampler2D visibleNodes; varying float vLinearDepth; varying vec3 vColor; #define PI 3.141592653589793 // --------------------- // OCTREE // --------------------- #if defined(adaptive_point_size) /** * number of 1-bits up to inclusive index position * number is treated as if it were an integer in the range 0-255 * */ float numberOfOnes(float number, float index){ float tmp = mod(number, pow(2.0, index + 1.0)); float numOnes = 0.0; for(float i = 0.0; i < 8.0; i++){ if(mod(tmp, 2.0) != 0.0){ numOnes++; } tmp = floor(tmp / 2.0); } return numOnes; } /** * checks whether the bit at index is 1 * number is treated as if it were an integer in the range 0-255 * */ bool isBitSet(float number, float index){ return mod(floor(number / pow(2.0, index)), 2.0) != 0.0; } /** * find the LOD at the point position */ float getLOD(){ vec3 offset = vec3(0.0, 0.0, 0.0); float iOffset = uVNStart; float depth = uLevel; for(float i = 0.0; i <= 30.0; i++){ float nodeSizeAtLevel = uOctreeSize / pow(2.0, i + uLevel + 0.0); vec3 index3d = (position-offset) / nodeSizeAtLevel; index3d = floor(index3d + 0.5); float index = 4.0 * index3d.x + 2.0 * index3d.y + index3d.z; vec4 value = texture2D(visibleNodes, vec2(iOffset / 2048.0, 0.0)); float mask = value.r * 255.0; if(isBitSet(mask, index)){ // there are more visible child nodes at this position iOffset = iOffset + value.g * 255.0 * 256.0 + value.b * 255.0 + numberOfOnes(mask, index - 1.0); depth++; }else{ // no more visible child nodes at this position return depth; } offset = offset + (vec3(1.0, 1.0, 1.0) * nodeSizeAtLevel * 0.5) * index3d; } return depth; } #endif float getPointSize(){ float pointSize = 1.0; float slope = tan(fov / 2.0); float projFactor = -0.5 * uScreenHeight / (slope * vViewPosition.z); float r = uOctreeSpacing * 1.5; vRadius = r; #if defined fixed_point_size pointSize = size; #elif defined attenuated_point_size if(uUseOrthographicCamera){ pointSize = size; }else{ pointSize = pointSize * projFactor; } #elif defined adaptive_point_size if(uUseOrthographicCamera) { float worldSpaceSize = 1.5 * size * r / getPointSizeAttenuation(); pointSize = (worldSpaceSize / uOrthoWidth) * uScreenWidth; } else { float worldSpaceSize = 1.5 * size * r / getPointSizeAttenuation(); pointSize = worldSpaceSize * projFactor; } #endif pointSize = max(minSize, pointSize); pointSize = min(maxSize, pointSize); vRadius = pointSize / projFactor; return pointSize; } void main() { vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); vLinearDepth = gl_Position.w; float pointSize = getPointSize(); gl_PointSize = pointSize; } `; Shaders["pointcloud_sm.fs"] = ` precision mediump float; precision mediump int; varying vec3 vColor; varying float vLinearDepth; void main() { //gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); //gl_FragColor = vec4(vColor, 1.0); //gl_FragColor = vec4(vLinearDepth, pow(vLinearDepth, 2.0), 0.0, 1.0); gl_FragColor = vec4(vLinearDepth, vLinearDepth / 30.0, vLinearDepth / 30.0, 1.0); } `; Shaders["normalize.vs"] = ` precision mediump float; precision mediump int; attribute vec3 position; attribute vec2 uv; uniform mat4 projectionMatrix; uniform mat4 modelViewMatrix; varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }`; Shaders["normalize.fs"] = ` #extension GL_EXT_frag_depth : enable precision mediump float; precision mediump int; uniform sampler2D uWeightMap; uniform sampler2D uDepthMap; varying vec2 vUv; void main() { float depth = texture2D(uDepthMap, vUv).r; if(depth >= 1.0){ discard; } gl_FragColor = vec4(depth, 1.0, 0.0, 1.0); vec4 color = texture2D(uWeightMap, vUv); color = color / color.w; gl_FragColor = vec4(color.xyz, 1.0); gl_FragDepthEXT = depth; }`; Shaders["normalize_and_edl.fs"] = ` #extension GL_EXT_frag_depth : enable // // adapted from the EDL shader code from Christian Boucheny in cloud compare: // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL // precision mediump float; precision mediump int; uniform sampler2D uWeightMap; uniform sampler2D uEDLMap; uniform sampler2D uDepthMap; uniform float screenWidth; uniform float screenHeight; uniform vec2 neighbours[NEIGHBOUR_COUNT]; uniform float edlStrength; uniform float radius; varying vec2 vUv; float response(float depth){ vec2 uvRadius = radius / vec2(screenWidth, screenHeight); float sum = 0.0; for(int i = 0; i < NEIGHBOUR_COUNT; i++){ vec2 uvNeighbor = vUv + uvRadius * neighbours[i]; float neighbourDepth = texture2D(uEDLMap, uvNeighbor).a; if(neighbourDepth != 0.0){ if(depth == 0.0){ sum += 100.0; }else{ sum += max(0.0, depth - neighbourDepth); } } } return sum / float(NEIGHBOUR_COUNT); } void main() { float edlDepth = texture2D(uEDLMap, vUv).a; float res = response(edlDepth); float shade = exp(-res * 300.0 * edlStrength); float depth = texture2D(uDepthMap, vUv).r; if(depth >= 1.0 && res == 0.0){ discard; } vec4 color = texture2D(uWeightMap, vUv); color = color / color.w; color = color * shade; gl_FragColor = vec4(color.xyz, 1.0); gl_FragDepthEXT = depth; }`; Shaders["edl_new.vs"] = ` precision mediump float; precision mediump int; attribute vec3 position; attribute vec2 uv; uniform mat4 projectionMatrix; uniform mat4 modelViewMatrix; varying vec2 vUv; void main() { vUv = uv; vec4 mvPosition = modelViewMatrix * vec4(position,1.0); gl_Position = projectionMatrix * mvPosition; }`; Shaders["edl_new.fs"] = ` #extension GL_EXT_frag_depth : enable // // adapted from the EDL shader code from Christian Boucheny in cloud compare: // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL // precision mediump float; precision mediump int; //uniform float screenWidth; //uniform float screenHeight; uniform vec2 resolution; uniform vec2 neighbours[NEIGHBOUR_COUNT]; uniform float edlStrength; uniform float radius; uniform float opacity; //uniform float uNear; //uniform float uFar; uniform mat4 uProj; uniform sampler2D uEDLColor; uniform sampler2D uEDLDepth; varying vec2 vUv; uniform int useEDL; float response(float depth){ vec2 uvRadius = radius / resolution; //vec2(screenWidth, screenHeight); float sum = 0.0; for(int i = 0; i < NEIGHBOUR_COUNT; i++){ vec2 uvNeighbor = vUv + uvRadius * neighbours[i]; //获取周围八个格子的值 float neighbourDepth = texture2D(uEDLColor, uvNeighbor).a; neighbourDepth = (neighbourDepth == 1.0) ? 0.0 : neighbourDepth; if(neighbourDepth != 0.0){ //if(depth == 0.0){ // sum += 100.0; //}else{ sum += max(0.0, depth - neighbourDepth); //获取差值 //} } } return sum / float(NEIGHBOUR_COUNT); } void main(){ vec4 cEDL = texture2D(uEDLColor, vUv); float depth = cEDL.a; depth = (depth == 1.0) ? 0.0 : depth; if(depth == 0.0){ //去掉这句就能在无点云像素的地方渲染outline,但会遮住其他mesh discard; } if(useEDL == 1){ float res = response(depth); float shade = exp(-res * 300.0 * edlStrength); //自然常数e为底的指数函数 const float min = 0.2; //add 使边缘色变浅 范围从0-1 变为min-1 shade = shade * (1.0-min) + min; gl_FragColor = vec4(cEDL.rgb * shade, opacity); }else{//加 不改颜色的情况 gl_FragColor = vec4(cEDL.rgb, opacity); } //注意,edlStrength越强,在相同depth的叠放的点云间的轮廓线颜色越深 //作用:勾勒边缘,强化深度差异,模拟阴影,加强结构立体感 { // write regular hyperbolic depth values to depth buffer 修改深度 float dl = pow(2.0, depth); vec4 dp = uProj * vec4(0.0, 0.0, -dl, 1.0); float pz = dp.z / dp.w; float fragDepth = (pz + 1.0) / 2.0; gl_FragDepthEXT = fragDepth; } } `; Shaders["blur.vs"] = ` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position,1.0); }`; Shaders["blur.fs"] = ` uniform mat4 projectionMatrix; uniform float screenWidth; uniform float screenHeight; uniform float near; uniform float far; uniform sampler2D map; varying vec2 vUv; void main() { float dx = 1.0 / screenWidth; float dy = 1.0 / screenHeight; vec3 color = vec3(0.0, 0.0, 0.0); color += texture2D(map, vUv + vec2(-dx, -dy)).rgb; color += texture2D(map, vUv + vec2( 0, -dy)).rgb; color += texture2D(map, vUv + vec2(+dx, -dy)).rgb; color += texture2D(map, vUv + vec2(-dx, 0)).rgb; color += texture2D(map, vUv + vec2( 0, 0)).rgb; color += texture2D(map, vUv + vec2(+dx, 0)).rgb; color += texture2D(map, vUv + vec2(-dx, dy)).rgb; color += texture2D(map, vUv + vec2( 0, dy)).rgb; color += texture2D(map, vUv + vec2(+dx, dy)).rgb; color = color / 9.0; gl_FragColor = vec4(color, 1.0); }`; Shaders["depthBasic.vs"] = ` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } `; Shaders["depthBasic.fs"] = `varying vec2 vUv; uniform float opacity; uniform float mapScale; uniform vec3 baseColor; #if defined use_map uniform sampler2D map; vec4 getMapColor(vec4 color){ if(mapScale == 1.0){ color = texture2D(map, vUv) * color; return color; }else{ vec2 uv = (vUv - 0.5) / mapScale + 0.5; if(uv.x > 1.0 || uv.y > 1.0 || uv.x < 0.0 || uv.y < 0.0){ //color = vec4(0.0,0.0,0.0,0.0); discard; }else{ color = texture2D(map, uv) * color; } return color; } } #endif #if defined(GL_EXT_frag_depth) && defined(useDepth) //似乎通过gl.getExtension('EXT_frag_depth')得到的GL_EXT_frag_depth uniform sampler2D depthTexture; uniform float nearPlane; uniform float farPlane; uniform vec2 resolution; uniform vec2 viewportOffset; // viewportOffset 范围从0-整个画布的像素 uniform vec3 backColor; uniform float occlusionDistance; uniform float clipDistance; uniform float maxClipFactor; uniform float maxOcclusionFactor; //uniform bool uUseOrthographicCamera; float convertToLinear(float zValue) { //if(uUseOrthographicCamera){ // return zValue*(farPlane-nearPlane)+nearPlane; //}else{ float z = zValue * 2.0 - 1.0; return (2.0 * nearPlane * farPlane) / (farPlane + nearPlane - z * (farPlane - nearPlane)); //} } #endif void main() { vec4 color = vec4(baseColor, opacity); #if defined(GL_EXT_frag_depth) && defined(useDepth) // mixFactor and clipFactor define the color mixing proportion between the states of // full visibility and occluded visibility // and // full visibility and total invisibility float mixFactor = 0.0; float clipFactor = 0.0; // The linear depth value of the current fragment float fragDepth = convertToLinear(gl_FragCoord.z); // The coordinates of the current fragment in the depth texture vec2 depthTxtCoords = vec2(gl_FragCoord.x-viewportOffset.x, gl_FragCoord.y - viewportOffset.y) / resolution; //gl_FragCoord大小为 viewport client大小 // The linear depth value of the pixel occupied by this fragment in the depth buffer float textureDepth = convertToLinear(texture2D(depthTexture, depthTxtCoords).r); // The difference between the two depths float delta = fragDepth - textureDepth; if (delta > 0.0)//差距 { // occlusionDistance and clipDistance define the width of the respective zones and // mixFactor and clipFactor express the interpolation between the two colors depending on the position // of the current fragment withing those zones. mixFactor = clamp(delta / occlusionDistance, 0.0, maxOcclusionFactor); clipFactor = clamp(delta / clipDistance, 0.0, maxClipFactor); } // If the fragment is totally transparent, don't bother drawing it if (clipFactor == 1.0) { discard; }else{ #if defined use_map color = getMapColor(color); #endif color = vec4(mix(color.rgb, backColor, mixFactor), color.a * (1.0 - clipFactor)); } #else #if defined use_map color = getMapColor(color); #endif #endif gl_FragColor = color; } `; Shaders["copyCubeMap.vs"] = `varying vec3 vWorldPos; vec3 transformAxis( vec3 direction ) //navvis->4dkk { float y = direction.y; direction.y = direction.z; direction.z = -y; return direction; } void main() { vWorldPos = vec3(-position.x, -position.y, position.z); //vWorldPos = transformAxis(vWorldPos); gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } `; Shaders["copyCubeMap.fs"] = `varying vec3 vWorldPos; uniform float alpha; uniform samplerCube tDiffuse; void main() { vec4 texColor = textureCube(tDiffuse, vWorldPos); gl_FragColor = vec4(texColor.rgb, texColor.a * alpha); } `; Shaders["basicTextured.vs"] = `varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0); } `; Shaders["basicTextured.fs"] = `varying vec2 vUv; uniform float opacity; #ifdef HasMap uniform sampler2D map; #endif #ifdef HasColor uniform vec3 color; #endif void main() { vec4 color_; #ifdef HasColor color_ = vec4(color, opacity); #else color_ = vec4(1.0,1.0,1.0, opacity); #endif #ifdef HasMap vec4 texColor = texture2D(map, vUv); gl_FragColor = texColor * color_; /*#ifdef InverseColor bool inverse = false; //gl_FragColor.r < 0.3 || gl_FragColor.g < 0.3 || gl_FragColor.b < 0.3 || //深色字和白色地面要反转 if(gl_FragColor.r > 0.9 && gl_FragColor.g > 0.9 && gl_FragColor.b > 0.9){ //gl_FragColor.r = 1.0 - gl_FragColor.r; //gl_FragColor.g = 1.0 - gl_FragColor.g; //gl_FragColor.b = 1.0 - gl_FragColor.b; gl_FragColor = vec4(0.07,0.22, 0.5,1.0); }else{//海和草坪颜色加深 gl_FragColor = texColor * vec4(0.3,0.5,0.7,1.0); } #endif*/ #else gl_FragColor = color_; #endif } `; Shaders["skybox.vs"] = `uniform mat4 matrix; varying vec3 vWorldPosition; void main() { vWorldPosition = (modelMatrix * vec4(position, 1.0)).xyz ; // - cameraPosition; vWorldPosition = (vec4(vWorldPosition, 1.0) * matrix).xyz; vWorldPosition.x *= -1.0; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); }`; Shaders["skybox.fs"] = `varying vec3 vWorldPosition; uniform sampler2D tDiffuse; #define PI 3.141592653 /*vec2 getSamplerCoord( vec3 direction ) { direction = normalize(direction); float tx=atan(direction.x,direction.z)/(PI*2.0)+0.5; float ty=acos(direction.y)/PI; return vec2(tx,ty); }*/ vec2 getSamplerCoord( vec3 direction ) { direction = normalize(direction); float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5; float ty=acos(direction.z)/PI; return vec2(tx,ty); } void main() { vec2 samplerCoord = getSamplerCoord(vWorldPosition); vec4 color = texture2D(tDiffuse, samplerCoord); //gl_FragColor = RGBEToLinear( color ); //对于hdr必须加这一句,否则效果很奇怪。copy自meshBasicMaterial里的mat_fragment的mapTexelToLinear函数,只要有map就会使用该函数 gl_FragColor = color; //#include //////#include } `; const ClassificationScheme = { DEFAULT: { 0: { visible: true, name: 'never classified' , color: [0.5, 0.5, 0.5, 1.0] }, 1: { visible: true, name: 'unclassified' , color: [0.5, 0.5, 0.5, 1.0] }, 2: { visible: true, name: 'ground' , color: [0.63, 0.32, 0.18, 1.0] }, 3: { visible: true, name: 'low vegetation' , color: [0.0, 1.0, 0.0, 1.0] }, 4: { visible: true, name: 'medium vegetation' , color: [0.0, 0.8, 0.0, 1.0] }, 5: { visible: true, name: 'high vegetation' , color: [0.0, 0.6, 0.0, 1.0] }, 6: { visible: true, name: 'building' , color: [1.0, 0.66, 0.0, 1.0] }, 7: { visible: true, name: 'low point(noise)' , color: [1.0, 0.0, 1.0, 1.0] }, 8: { visible: true, name: 'key-point' , color: [1.0, 0.0, 0.0, 1.0] }, 9: { visible: true, name: 'water' , color: [0.0, 0.0, 1.0, 1.0] }, 12: { visible: true, name: 'overlap' , color: [1.0, 1.0, 0.0, 1.0] }, DEFAULT: { visible: true, name: 'default' , color: [0.3, 0.6, 0.6, 0.5] }, } }; Object.defineProperty(ClassificationScheme, 'RANDOM', { get: function() { let scheme = {}; for(let i = 0; i <= 255; i++){ scheme[i] = new Vector4(Math.random(), Math.random(), Math.random()); } scheme["DEFAULT"] = new Vector4(Math.random(), Math.random(), Math.random()); return scheme; } }); class EnumItem{ constructor(object){ for(let key of Object.keys(object)){ this[key] = object[key]; } } inspect(){ return `Enum(${this.name}: ${this.value})`; } }; class Enum{ constructor(object){ this.object = object; for(let key of Object.keys(object)){ let value = object[key]; if(typeof value === "object"){ value.name = key; }else { value = {name: key, value: value}; } this[key] = new EnumItem(value); } } fromValue(value){ for(let key of Object.keys(this.object)){ if(this[key].value === value){ return this[key]; } } throw new Error(`No enum for value: ${value}`); } }; const CameraMode = { ORTHOGRAPHIC: 0, PERSPECTIVE: 1, VR: 2, }; const ClipTask = { NONE: 0, HIGHLIGHT: 1, SHOW_INSIDE: 2, SHOW_OUTSIDE: 3 }; const ClipMethod = { INSIDE_ANY: 0, INSIDE_ALL: 1 }; const ElevationGradientRepeat = { CLAMP: 0, REPEAT: 1, MIRRORED_REPEAT: 2, }; const MOUSE$1 = { LEFT: 0b0001, RIGHT: 0b0010, MIDDLE: 0b0100 }; const PointSizeType = { FIXED: 0, ATTENUATED: 1, ADAPTIVE: 2 }; const PointShape$1 = { SQUARE: 0, CIRCLE: 1, PARABOLOID: 2 }; const TreeType = { OCTREE: 0, KDTREE: 1 }; const LengthUnits = { METER: {code: 'm', unitspermeter: 1.0}, FEET: {code: 'ft', unitspermeter: 3.28084}, INCH: {code: '\u2033', unitspermeter: 39.3701} }; // // how to calculate the radius of a projected sphere in screen space // http://stackoverflow.com/questions/21648630/radius-of-projected-sphere-in-screen-space // http://stackoverflow.com/questions/3717226/radius-of-projected-sphere // class PointCloudMaterial$1 extends RawShaderMaterial {//base constructor (parameters = {}) { super(); this.visibleNodesTexture = Utils.generateDataTexture(2048, 1, new Color(0xffffff)); this.visibleNodesTexture.minFilter = NearestFilter; this.visibleNodesTexture.magFilter = NearestFilter; let getValid = (a, b) => { if(a !== undefined){ return a; }else { return b; } }; let pointSize = getValid(parameters.size, 1.0); let minSize = getValid(parameters.minSize, 2.0); let maxSize = getValid(parameters.maxSize, 50.0); let treeType = getValid(parameters.treeType, TreeType.OCTREE); this._pointSizeType = PointSizeType.FIXED; this._shape = PointShape$1.SQUARE; this._useClipBox = false; this.clipBoxes = []; this.clipPolygons = []; this._weighted = false; this._gradient = Gradients.SPECTRAL; this.gradientTexture = PointCloudMaterial$1.generateGradientTexture(this._gradient); //this._matcap = "matcap.jpg"; //this.matcapTexture = PointCloudMaterial.generateMatcapTexture(this._matcap); this.lights = false; this.fog = false; this._treeType = treeType; this._useEDL = false; this.defines = new Map(); this.ranges = new Map(); this._activeAttributeName = null; this._defaultIntensityRangeChanged = false; this._defaultElevationRangeChanged = false; { const [width, height] = [256, 1]; let data = new Uint8Array(width * 4); let texture = new DataTexture(data, width, height, RGBAFormat); texture.magFilter = NearestFilter; texture.needsUpdate = true; this.classificationTexture = texture; } this.attributes = { position: { type: 'fv', value: [] }, color: { type: 'fv', value: [] }, normal: { type: 'fv', value: [] }, intensity: { type: 'f', value: [] }, classification: { type: 'f', value: [] }, returnNumber: { type: 'f', value: [] }, numberOfReturns: { type: 'f', value: [] }, pointSourceID: { type: 'f', value: [] }, indices: { type: 'fv', value: [] } }; this.uniforms = { level: { type: "f", value: 0.0 }, vnStart: { type: "f", value: 0.0 }, spacing: { type: "f", value: 1.0 }, blendHardness: { type: "f", value: 2.0 }, blendDepthSupplement: { type: "f", value: 0.0 }, fov: { type: "f", value: 1.0 }, screenWidth: { type: "f", value: 1.0 }, screenHeight: { type: "f", value: 1.0 }, near: { type: "f", value: 0.1 }, far: { type: "f", value: 1.0 }, uColor: { type: "c", value: new Color( 0xffffff ) }, uOpacity: { type: "f", value: 1.0 }, size: { type: "f", value: pointSize }, minSize: { type: "f", value: minSize }, maxSize: { type: "f", value: maxSize }, octreeSize: { type: "f", value: 0 }, bbSize: { type: "fv", value: [0, 0, 0] }, elevationRange: { type: "2fv", value: [0, 0] }, clipBoxCount: { type: "f", value: 0 }, //clipSphereCount: { type: "f", value: 0 }, clipPolygonCount: { type: "i", value: 0 }, clipBoxes: { type: "Matrix4fv", value: [] }, //clipSpheres: { type: "Matrix4fv", value: [] }, clipPolygons: { type: "3fv", value: [] }, clipPolygonVCount: { type: "iv", value: [] }, clipPolygonVP: { type: "Matrix4fv", value: [] }, visibleNodes: { type: "t", value: this.visibleNodesTexture }, pcIndex: { type: "f", value: 0 }, gradient: { type: "t", value: this.gradientTexture }, classificationLUT: { type: "t", value: this.classificationTexture }, uHQDepthMap: { type: "t", value: null }, toModel: { type: "Matrix4f", value: [] }, diffuse: { type: "fv", value: [1, 1, 1] }, transition: { type: "f", value: 0.5 }, intensityRange: { type: "fv", value: [Infinity, -Infinity] }, intensity_gbc: { type: "fv", value: [1, 0, 0]}, uRGB_gbc: { type: "fv", value: [1, 0, 0]}, // intensityGamma: { type: "f", value: 1 }, // intensityContrast: { type: "f", value: 0 }, // intensityBrightness:{ type: "f", value: 0 }, // rgbGamma: { type: "f", value: 1 }, // rgbContrast: { type: "f", value: 0 }, // rgbBrightness: { type: "f", value: 0 }, wRGB: { type: "f", value: 1 }, wIntensity: { type: "f", value: 0 }, wElevation: { type: "f", value: 0 }, wClassification: { type: "f", value: 0 }, wReturnNumber: { type: "f", value: 0 }, wSourceID: { type: "f", value: 0 }, useOrthographicCamera: { type: "b", value: false }, elevationGradientRepat: { type: "i", value: ElevationGradientRepeat.CLAMP }, clipTask: { type: "i", value: 1 }, clipMethod: { type: "i", value: 1 }, uShadowColor: { type: "3fv", value: [0, 0, 0] }, uExtraScale: { type: "f", value: 1}, uExtraOffset: { type: "f", value: 0}, uExtraRange: { type: "2fv", value: [0, 1] }, uExtraGammaBrightContr: { type: "3fv", value: [1, 0, 0] }, uFilterReturnNumberRange: { type: "fv", value: [0, 7]}, uFilterNumberOfReturnsRange: { type: "fv", value: [0, 7]}, uFilterGPSTimeClipRange: { type: "fv", value: [0, 7]}, uFilterPointSourceIDClipRange: { type: "fv", value: [0, 65535]}, //matcapTextureUniform: { type: "t", value: this.matcapTexture }, backfaceCulling: { type: "b", value: false }, }; this.classification = ClassificationScheme.DEFAULT; this.defaultAttributeValues.normal = [0, 0, 0]; this.defaultAttributeValues.classification = [0, 0, 0]; this.defaultAttributeValues.indices = [0, 0, 0, 0]; this.vertexShader = Shaders['pointcloud.vs']; this.fragmentShader = Shaders['pointcloud.fs']; this.vertexColors = VertexColors; this.updateShaderSource(); } setDefine(key, value){ if(value !== undefined && value !== null){ if(this.defines.get(key) !== value){ this.defines.set(key, value); this.updateShaderSource(); } }else { this.removeDefine(key); } } removeDefine(key){ this.defines.delete(key); } updateShaderSource () { let vs = Shaders['pointcloud.vs']; let fs = Shaders['pointcloud.fs']; let definesString = this.getDefines(); let vsVersionIndex = vs.indexOf("#version "); let fsVersionIndex = fs.indexOf("#version "); if(vsVersionIndex >= 0){ vs = vs.replace(/(#version .*)/, `$1\n${definesString}`); }else { vs = `${definesString}\n${vs}`; } if(fsVersionIndex >= 0){ fs = fs.replace(/(#version .*)/, `$1\n${definesString}`); }else { fs = `${definesString}\n${fs}`; } this.vertexShader = vs; this.fragmentShader = fs; if (this.opacity === 1.0) { this.blending = NoBlending; this.transparent = false; this.depthTest = true; this.depthWrite = true; this.depthFunc = LessEqualDepth; } else if (this.opacity < 1.0 && !this.useEDL) { this.blending = AdditiveBlending; this.transparent = true; this.depthTest = false; this.depthWrite = true; this.depthFunc = AlwaysDepth; } if (this.weighted) { this.blending = AdditiveBlending; this.transparent = true; this.depthTest = true; this.depthWrite = false; } this.needsUpdate = true; } getDefines () { let defines = []; if (this.pointSizeType === PointSizeType.FIXED) { defines.push('#define fixed_point_size'); } else if (this.pointSizeType === PointSizeType.ATTENUATED) { defines.push('#define attenuated_point_size'); } else if (this.pointSizeType === PointSizeType.ADAPTIVE) { defines.push('#define adaptive_point_size'); } if (this.shape === PointShape$1.SQUARE) { defines.push('#define square_point_shape'); } else if (this.shape === PointShape$1.CIRCLE) { defines.push('#define circle_point_shape'); } else if (this.shape === PointShape$1.PARABOLOID) { defines.push('#define paraboloid_point_shape'); } if (this._useEDL) { defines.push('#define use_edl'); } if(this.activeAttributeName){ let attributeName = this.activeAttributeName.replace(/[^a-zA-Z0-9]/g, '_'); defines.push(`#define color_type_${attributeName}`); } if(this._treeType === TreeType.OCTREE){ defines.push('#define tree_type_octree'); }else if(this._treeType === TreeType.KDTREE){ defines.push('#define tree_type_kdtree'); } if (this.weighted) { defines.push('#define weighted_splats'); } for(let [key, value] of this.defines){ defines.push(value); } return defines.join("\n"); } setClipBoxes (clipBoxes) { if (!clipBoxes) { return; } let doUpdate = (this.clipBoxes.length !== clipBoxes.length) && (clipBoxes.length === 0 || this.clipBoxes.length === 0); this.uniforms.clipBoxCount.value = this.clipBoxes.length; this.clipBoxes = clipBoxes; if (doUpdate) { this.updateShaderSource(); } this.uniforms.clipBoxes.value = new Float32Array(this.clipBoxes.length * 16); for (let i = 0; i < this.clipBoxes.length; i++) { let box = clipBoxes[i]; this.uniforms.clipBoxes.value.set(box.inverse.elements, 16 * i); } for (let i = 0; i < this.uniforms.clipBoxes.value.length; i++) { if (Number.isNaN(this.uniforms.clipBoxes.value[i])) { this.uniforms.clipBoxes.value[i] = Infinity; } } } setClipPolygons(clipPolygons, maxPolygonVertices) { if(!clipPolygons){ return; } this.clipPolygons = clipPolygons; let doUpdate = (this.clipPolygons.length !== clipPolygons.length); if(doUpdate){ this.updateShaderSource(); } } get gradient(){ return this._gradient; } set gradient (value) {//海拔贴图 if (this._gradient !== value) { this._gradient = value; this.gradientTexture = PointCloudMaterial$1.generateGradientTexture(this._gradient); this.uniforms.gradient.value = this.gradientTexture; } } /* get matcap(){ return this._matcap; } set matcap (value) { if (this._matcap !== value) { this._matcap = value; this.matcapTexture = Potree.PointCloudMaterial.generateMatcapTexture(this._matcap); this.uniforms.matcapTextureUniform.value = this.matcapTexture; } } */ get useOrthographicCamera() { return this.uniforms.useOrthographicCamera.value; } set useOrthographicCamera(value) { if(this.uniforms.useOrthographicCamera.value !== value){ this.uniforms.useOrthographicCamera.value = value; } } get backfaceCulling() { return this.uniforms.backfaceCulling.value; } set backfaceCulling(value) { if(this.uniforms.backfaceCulling.value !== value){ this.uniforms.backfaceCulling.value = value; this.dispatchEvent({type: 'backface_changed', target: this}); } } recomputeClassification () { const classification = this.classification; const data = this.classificationTexture.image.data; let width = 256; const black = [1, 1, 1, 1]; let valuesChanged = false; for (let i = 0; i < width; i++) { let color; let visible = true; if (classification[i]) { color = classification[i].color; visible = classification[i].visible; } else if (classification[i % 32]) { color = classification[i % 32].color; visible = classification[i % 32].visible; } else if(classification.DEFAULT) { color = classification.DEFAULT.color; visible = classification.DEFAULT.visible; }else { color = black; } const r = parseInt(255 * color[0]); const g = parseInt(255 * color[1]); const b = parseInt(255 * color[2]); const a = visible ? parseInt(255 * color[3]) : 0; if(data[4 * i + 0] !== r){ data[4 * i + 0] = r; valuesChanged = true; } if(data[4 * i + 1] !== g){ data[4 * i + 1] = g; valuesChanged = true; } if(data[4 * i + 2] !== b){ data[4 * i + 2] = b; valuesChanged = true; } if(data[4 * i + 3] !== a){ data[4 * i + 3] = a; valuesChanged = true; } } if(valuesChanged){ this.classificationTexture.needsUpdate = true; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get spacing () { return this.uniforms.spacing.value; } set spacing (value) { if (this.uniforms.spacing.value !== value) { // 即 uOctreeSpacing 来自cloud.js里 this.uniforms.spacing.value = value; } } get useClipBox () { return this._useClipBox; } set useClipBox (value) { if (this._useClipBox !== value) { this._useClipBox = value; this.updateShaderSource(); } } get clipTask(){ return this.uniforms.clipTask.value; } set clipTask(mode){ this.uniforms.clipTask.value = mode; } get elevationGradientRepat(){ return this.uniforms.elevationGradientRepat.value; } set elevationGradientRepat(mode){ this.uniforms.elevationGradientRepat.value = mode; } get clipMethod(){ return this.uniforms.clipMethod.value; } set clipMethod(mode){ this.uniforms.clipMethod.value = mode; } get weighted(){ return this._weighted; } set weighted (value) { if (this._weighted !== value) { this._weighted = value; this.updateShaderSource(); } } get fov () { return this.uniforms.fov.value; } set fov (value) { if (this.uniforms.fov.value !== value) { this.uniforms.fov.value = value; // this.updateShaderSource(); } } get screenWidth () { return this.uniforms.screenWidth.value; } set screenWidth (value) { if (this.uniforms.screenWidth.value !== value) { this.uniforms.screenWidth.value = value; // this.updateShaderSource(); } } get screenHeight () { return this.uniforms.screenHeight.value; } set screenHeight (value) { if (this.uniforms.screenHeight.value !== value) { this.uniforms.screenHeight.value = value; // this.updateShaderSource(); } } get near () { return this.uniforms.near.value; } set near (value) { if (this.uniforms.near.value !== value) { this.uniforms.near.value = value; } } get far () { return this.uniforms.far.value; } set far (value) { if (this.uniforms.far.value !== value) { this.uniforms.far.value = value; } } get opacity(){ return this.uniforms.uOpacity.value; } set opacity (value) { if (this.uniforms && this.uniforms.uOpacity) { if (this.uniforms.uOpacity.value !== value) { this.uniforms.uOpacity.value = value; this.updateShaderSource(); this.dispatchEvent({ type: 'opacity_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); } } } get activeAttributeName(){ return this._activeAttributeName; } set activeAttributeName(value){ if (this._activeAttributeName !== value) { this._activeAttributeName = value; this.updateShaderSource(); this.dispatchEvent({ type: 'active_attribute_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get pointSizeType () { return this._pointSizeType; } set pointSizeType (value) { if (this._pointSizeType !== value) { this._pointSizeType = value; this.updateShaderSource(); //这句表明这个属性频繁更改会卡顿 this.dispatchEvent({ type: 'point_size_type_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get useEDL(){ return this._useEDL; } set useEDL (value) { if (this._useEDL !== value) { this._useEDL = value; this.updateShaderSource(); } } get color () { return this.uniforms.uColor.value; } set color (value) { if (!this.uniforms.uColor.value.equals(value)) { this.uniforms.uColor.value.copy(value); this.dispatchEvent({ type: 'color_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get shape () { return this._shape; } set shape (value) { if (this._shape !== value) { this._shape = value; this.updateShaderSource(); this.dispatchEvent({type: 'point_shape_changed', target: this}); this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get treeType () { return this._treeType; } set treeType (value) { if (this._treeType !== value) { this._treeType = value; this.updateShaderSource(); } } get bbSize () { return this.uniforms.bbSize.value; } set bbSize (value) { this.uniforms.bbSize.value = value; } get size () { return this.uniforms.size.value; } set size (value) { if (this.uniforms.size.value !== value) { this.uniforms.size.value = value; this.dispatchEvent({ type: 'point_size_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get minSize(){ return this.uniforms.minSize.value; } set minSize(value){ if (this.uniforms.minSize.value !== value) { this.uniforms.minSize.value = value; this.dispatchEvent({ type: 'point_size_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get elevationRange () { return this.uniforms.elevationRange.value; } set elevationRange (value) { let changed = this.uniforms.elevationRange.value[0] !== value[0] || this.uniforms.elevationRange.value[1] !== value[1]; if(changed){ this.uniforms.elevationRange.value = value; this._defaultElevationRangeChanged = true; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get heightMin () { return this.uniforms.elevationRange.value[0]; } set heightMin (value) { this.elevationRange = [value, this.elevationRange[1]]; } get heightMax () { return this.uniforms.elevationRange.value[1]; } set heightMax (value) { this.elevationRange = [this.elevationRange[0], value]; } get transition () { return this.uniforms.transition.value; } set transition (value) { this.uniforms.transition.value = value; } get intensityRange () { return this.uniforms.intensityRange.value; } set intensityRange (value) { if (!(value instanceof Array && value.length === 2)) { return; } if (value[0] === this.uniforms.intensityRange.value[0] && value[1] === this.uniforms.intensityRange.value[1]) { return; } this.uniforms.intensityRange.value = value; this._defaultIntensityRangeChanged = true; this.dispatchEvent({ type: 'material_property_changed', target: this }); } get intensityGamma () { return this.uniforms.intensity_gbc.value[0]; } set intensityGamma (value) { if (this.uniforms.intensity_gbc.value[0] !== value) { this.uniforms.intensity_gbc.value[0] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get intensityContrast () { return this.uniforms.intensity_gbc.value[2]; } set intensityContrast (value) { if (this.uniforms.intensity_gbc.value[2] !== value) { this.uniforms.intensity_gbc.value[2] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get intensityBrightness () { return this.uniforms.intensity_gbc.value[1]; } set intensityBrightness (value) { if (this.uniforms.intensity_gbc.value[1] !== value) { this.uniforms.intensity_gbc.value[1] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get rgbGamma () { return this.uniforms.uRGB_gbc.value[0]; } set rgbGamma (value) { if (this.uniforms.uRGB_gbc.value[0] !== value) { this.uniforms.uRGB_gbc.value[0] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get rgbContrast () { return this.uniforms.uRGB_gbc.value[2]; } set rgbContrast (value) { if (this.uniforms.uRGB_gbc.value[2] !== value) { this.uniforms.uRGB_gbc.value[2] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get rgbBrightness () { return this.uniforms.uRGB_gbc.value[1]; } set rgbBrightness (value) { if (this.uniforms.uRGB_gbc.value[1] !== value) { this.uniforms.uRGB_gbc.value[1] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get extraGamma () { return this.uniforms.uExtraGammaBrightContr.value[0]; } set extraGamma (value) { if (this.uniforms.uExtraGammaBrightContr.value[0] !== value) { this.uniforms.uExtraGammaBrightContr.value[0] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get extraBrightness () { return this.uniforms.uExtraGammaBrightContr.value[1]; } set extraBrightness (value) { if (this.uniforms.uExtraGammaBrightContr.value[1] !== value) { this.uniforms.uExtraGammaBrightContr.value[1] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get extraContrast () { return this.uniforms.uExtraGammaBrightContr.value[2]; } set extraContrast (value) { if (this.uniforms.uExtraGammaBrightContr.value[2] !== value) { this.uniforms.uExtraGammaBrightContr.value[2] = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } getRange(attributeName){ return this.ranges.get(attributeName); } setRange(attributeName, newRange){ let rangeChanged = false; let oldRange = this.ranges.get(attributeName); if(oldRange != null && newRange != null){ rangeChanged = oldRange[0] !== newRange[0] || oldRange[1] !== newRange[1]; }else { rangeChanged = true; } this.ranges.set(attributeName, newRange); if(rangeChanged){ this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get extraRange () { return this.uniforms.uExtraRange.value; } set extraRange (value) { if (!(value instanceof Array && value.length === 2)) { return; } if (value[0] === this.uniforms.uExtraRange.value[0] && value[1] === this.uniforms.uExtraRange.value[1]) { return; } this.uniforms.uExtraRange.value = value; this._defaultExtraRangeChanged = true; this.dispatchEvent({ type: 'material_property_changed', target: this }); } get weightRGB () { return this.uniforms.wRGB.value; } set weightRGB (value) { if(this.uniforms.wRGB.value !== value){ this.uniforms.wRGB.value = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get weightIntensity () { return this.uniforms.wIntensity.value; } set weightIntensity (value) { if(this.uniforms.wIntensity.value !== value){ this.uniforms.wIntensity.value = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get weightElevation () { return this.uniforms.wElevation.value; } set weightElevation (value) { if(this.uniforms.wElevation.value !== value){ this.uniforms.wElevation.value = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get weightClassification () { return this.uniforms.wClassification.value; } set weightClassification (value) { if(this.uniforms.wClassification.value !== value){ this.uniforms.wClassification.value = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get weightReturnNumber () { return this.uniforms.wReturnNumber.value; } set weightReturnNumber (value) { if(this.uniforms.wReturnNumber.value !== value){ this.uniforms.wReturnNumber.value = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } get weightSourceID () { return this.uniforms.wSourceID.value; } set weightSourceID (value) { if(this.uniforms.wSourceID.value !== value){ this.uniforms.wSourceID.value = value; this.dispatchEvent({ type: 'material_property_changed', target: this }); } } static generateGradientTexture (gradient) { let size = 64; // create canvas let canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; // get context let context = canvas.getContext('2d'); // draw gradient context.rect(0, 0, size, size); let ctxGradient = context.createLinearGradient(0, 0, size, size); for (let i = 0; i < gradient.length; i++) { let step = gradient[i]; ctxGradient.addColorStop(step[0], '#' + step[1].getHexString()); } context.fillStyle = ctxGradient; context.fill(); //let texture = new THREE.Texture(canvas); let texture = new CanvasTexture(canvas); texture.needsUpdate = true; texture.minFilter = LinearFilter; texture.wrap = RepeatWrapping; texture.repeat = 2; // textureImage = texture.image; return texture; } static generateMatcapTexture (matcap) { var url = new URL(Potree.resourcePath + "/textures/matcap/" + matcap).href; let texture = new TextureLoader().load( url ); texture.magFilter = texture.minFilter = LinearFilter; texture.needsUpdate = true; // PotreeConverter_1.6_2018_07_29_windows_x64\PotreeConverter.exe autzen_xyzrgbXYZ_ascii.xyz -f xyzrgbXYZ -a RGB NORMAL -o autzen_xyzrgbXYZ_ascii_a -p index --overwrite // Switch matcap texture on the fly : viewer.scene.pointclouds[0].material.matcap = 'matcap1.jpg'; // For non power of 2, use LinearFilter and dont generate mipmaps, For power of 2, use NearestFilter and generate mipmaps : matcap2.jpg 1 2 8 11 12 13 return texture; } disableEvents(){ if(this._hiddenListeners === undefined){ this._hiddenListeners = this._listeners; this._listeners = {}; } }; enableEvents(){ this._listeners = this._hiddenListeners; this._hiddenListeners = undefined; }; // copyFrom(from){ // var a = 10; // for(let name of Object.keys(this.uniforms)){ // this.uniforms[name].value = from.uniforms[name].value; // } // } // copy(from){ // this.copyFrom(from); // } } //见ExtendPointCloudOctree class PointCloudOctreeNode extends PointCloudTreeNode { constructor () { super(); //this.children = {}; this.children = []; this.sceneNode = null; this.octree = null; } getNumPoints () { return this.geometryNode.numPoints; } isLoaded () { return true; } isTreeNode () { return true; } isGeometryNode () { return false; } getLevel () { return this.geometryNode.level; } getBoundingSphere () { return this.geometryNode.boundingSphere; } getBoundingBox () { return this.geometryNode.boundingBox; } getChildren () { let children = []; for (let i = 0; i < 8; i++) { if (this.children[i]) { children.push(this.children[i]); } } return children; } getPointsInBox(boxNode){ if(!this.sceneNode){ return null; } let buffer = this.geometryNode.buffer; let posOffset = buffer.offset("position"); let stride = buffer.stride; let view = new DataView(buffer.data); let worldToBox = boxNode.matrixWorld.clone().invert(); let objectToBox = new Matrix4().multiplyMatrices(worldToBox, this.sceneNode.matrixWorld); let inBox = []; let pos = new Vector4(); for(let i = 0; i < buffer.numElements; i++){ let x = view.getFloat32(i * stride + posOffset + 0, true); let y = view.getFloat32(i * stride + posOffset + 4, true); let z = view.getFloat32(i * stride + posOffset + 8, true); pos.set(x, y, z, 1); pos.applyMatrix4(objectToBox); if(-0.5 < pos.x && pos.x < 0.5){ if(-0.5 < pos.y && pos.y < 0.5){ if(-0.5 < pos.z && pos.z < 0.5){ pos.set(x, y, z, 1).applyMatrix4(this.sceneNode.matrixWorld); inBox.push(new Vector3(pos.x, pos.y, pos.z)); } } } } return inBox; } get name () { return this.geometryNode.name; } }; class PointCloudOctree extends PointCloudTree {//base constructor (geometry, material) { super(); this.pointBudget = Infinity; //一直是这个值 this.pcoGeometry = geometry; this.boundingBox = this.pcoGeometry.boundingBox; this.boundingSphere = this.boundingBox.getBoundingSphere(new Sphere()); this.material = material || new PointCloudMaterial$1(); this.visiblePointsTarget = 2 * 1000 * 1000; this.minimumNodePixelSize = 150; this.level = 0; this.position.copy(geometry.offset); this.updateMatrix(); { let priorityQueue = ["rgba", "rgb", "intensity", "classification"]; let selected = "rgba"; for(let attributeName of priorityQueue){ let attribute = this.pcoGeometry.pointAttributes.attributes.find(a => a.name === attributeName); if(!attribute){ continue; } let min = attribute.range[0].constructor.name === "Array" ? attribute.range[0] : [attribute.range[0]]; let max = attribute.range[1].constructor.name === "Array" ? attribute.range[1] : [attribute.range[1]]; let range_min = new Vector3(...min); let range_max = new Vector3(...max); let range = range_min.distanceTo(range_max); if(range === 0){ continue; } selected = attributeName; break; } this.material.activeAttributeName = selected; } this.showBoundingBox = false; this.boundingBoxNodes = []; this.loadQueue = []; this.visibleBounds = new Box3(); this.visibleNodes = []; this.visibleGeometry = []; this.generateDEM = false; this.profileRequests = []; this.name = ''; this._visible = true; { let box = [this.pcoGeometry.tightBoundingBox, this.getBoundingBoxWorld()] .find(v => v !== undefined); this.updateMatrixWorld(true); box = Utils.computeTransformedBoundingBox(box, this.matrixWorld); let bMin = box.min.z; let bMax = box.max.z; this.material.heightMin = bMin; this.material.heightMax = bMax; } // TODO read projection from file instead this.projection = geometry.projection; this.fallbackProjection = geometry.fallbackProjection; this.root = this.pcoGeometry.root; } setName (name) { if (this.name !== name) { this.name = name; this.dispatchEvent({type: 'name_changed', name: name, pointcloud: this}); } } getName () { return this.name; } getAttribute(name){ const attribute = this.pcoGeometry.pointAttributes.attributes.find(a => a.name === name); if(attribute){ return attribute; }else { return null; } } getAttributes(){ return this.pcoGeometry.pointAttributes; } toTreeNode (geometryNode, parent) { let node = new PointCloudOctreeNode(); // if(geometryNode.name === "r40206"){ // console.log("creating node for r40206"); // } let sceneNode = new Points(geometryNode.geometry, this.material); sceneNode.name = geometryNode.name; sceneNode.position.copy(geometryNode.boundingBox.min); sceneNode.frustumCulled = false; sceneNode.onBeforeRender = (_this, scene, camera, geometry, material, group) => { if (material.program) { _this.getContext().useProgram(material.program.program); if (material.program.getUniforms().map.level) { let level = geometryNode.getLevel(); material.uniforms.level.value = level; material.program.getUniforms().map.level.setValue(_this.getContext(), level); } if (this.visibleNodeTextureOffsets && material.program.getUniforms().map.vnStart) { let vnStart = this.visibleNodeTextureOffsets.get(node); material.uniforms.vnStart.value = vnStart; material.program.getUniforms().map.vnStart.setValue(_this.getContext(), vnStart); } if (material.program.getUniforms().map.pcIndex) { let i = node.pcIndex ? node.pcIndex : this.visibleNodes.indexOf(node); material.uniforms.pcIndex.value = i; material.program.getUniforms().map.pcIndex.setValue(_this.getContext(), i); } } }; // { // DEBUG // let sg = new THREE.SphereGeometry(1, 16, 16); // let sm = new THREE.MeshNormalMaterial(); // let s = new THREE.Mesh(sg, sm); // s.scale.set(5, 5, 5); // s.position.copy(geometryNode.mean) // .add(this.position) // .add(geometryNode.boundingBox.min); // // viewer.scene.scene.add(s); // } node.geometryNode = geometryNode; node.sceneNode = sceneNode; node.pointcloud = this; node.children = []; //for (let key in geometryNode.children) { // node.children[key] = geometryNode.children[key]; //} for(let i = 0; i < 8; i++){ node.children[i] = geometryNode.children[i]; } if (!parent) { this.root = node; this.add(sceneNode); } else { let childIndex = parseInt(geometryNode.name[geometryNode.name.length - 1]); parent.sceneNode.add(sceneNode); parent.children[childIndex] = node; } let disposeListener = function () { let childIndex = parseInt(geometryNode.name[geometryNode.name.length - 1]); parent.sceneNode.remove(node.sceneNode); parent.children[childIndex] = geometryNode; }; geometryNode.oneTimeDisposeHandlers.push(disposeListener); return node; } updateVisibleBounds () { let leafNodes = []; for (let i = 0; i < this.visibleNodes.length; i++) { let node = this.visibleNodes[i]; let isLeaf = true; for (let j = 0; j < node.children.length; j++) { let child = node.children[j]; if (child instanceof PointCloudOctreeNode) { isLeaf = isLeaf && !child.sceneNode.visible; } else if (child instanceof PointCloudOctreeGeometryNode) { isLeaf = true; } } if (isLeaf) { leafNodes.push(node); } } this.visibleBounds.min = new Vector3(Infinity, Infinity, Infinity); this.visibleBounds.max = new Vector3(-Infinity, -Infinity, -Infinity); for (let i = 0; i < leafNodes.length; i++) { let node = leafNodes[i]; this.visibleBounds.expandByPoint(node.getBoundingBox().min); this.visibleBounds.expandByPoint(node.getBoundingBox().max); } } updateMaterial (material, visibleNodes, camera, renderer) { material.fov = camera.fov * (Math.PI / 180); material.screenWidth = renderer.domElement.clientWidth; material.screenHeight = renderer.domElement.clientHeight; 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 Vector3()).x; } computeVisibilityTextureData(nodes, camera){ if(Potree.measureTimings) performance.mark("computeVisibilityTextureData-start"); let data = new Uint8Array(nodes.length * 4); let visibleNodeTextureOffsets = new Map(); // copy array nodes = nodes.slice(); // sort by level and index, e.g. r, r0, r3, r4, r01, r07, r30, ... let sort = function (a, b) { let na = a.geometryNode.name; let nb = b.geometryNode.name; if (na.length !== nb.length) return na.length - nb.length; if (na < nb) return -1; if (na > nb) return 1; return 0; }; nodes.sort(sort); let worldDir = new Vector3(); let nodeMap = new Map(); let offsetsToChild = new Array(nodes.length).fill(Infinity); for(let i = 0; i < nodes.length; i++){ let node = nodes[i]; nodeMap.set(node.name, node); visibleNodeTextureOffsets.set(node, i); if(i > 0){ let index = parseInt(node.name.slice(-1)); let parentName = node.name.slice(0, -1); let parent = nodeMap.get(parentName); let parentOffset = visibleNodeTextureOffsets.get(parent); let parentOffsetToChild = (i - parentOffset); offsetsToChild[parentOffset] = Math.min(offsetsToChild[parentOffset], parentOffsetToChild); data[parentOffset * 4 + 0] = data[parentOffset * 4 + 0] | (1 << index); data[parentOffset * 4 + 1] = (offsetsToChild[parentOffset] >> 8); data[parentOffset * 4 + 2] = (offsetsToChild[parentOffset] % 256); } let density = node.geometryNode.density; if(typeof density === "number"){ let lodOffset = Math.log2(density) / 2 - 1.5; let offsetUint8 = (lodOffset + 10) * 10; data[i * 4 + 3] = offsetUint8; }else { data[i * 4 + 3] = 100; } } if(Potree.measureTimings){ performance.mark("computeVisibilityTextureData-end"); performance.measure("render.computeVisibilityTextureData", "computeVisibilityTextureData-start", "computeVisibilityTextureData-end"); } return { data: data, offsets: visibleNodeTextureOffsets }; } nodeIntersectsProfile (node, profile) { let bbWorld = node.boundingBox.clone().applyMatrix4(this.matrixWorld); let bsWorld = bbWorld.getBoundingSphere(new Sphere()); let intersects = false; for (let i = 0; i < profile.points.length - 1; i++) { let start = new Vector3(profile.points[i + 0].x, profile.points[i + 0].y, bsWorld.center.z); let end = new Vector3(profile.points[i + 1].x, profile.points[i + 1].y, bsWorld.center.z); let closest = new Line3(start, end).closestPointToPoint(bsWorld.center, true, new Vector3()); let distance = closest.distanceTo(bsWorld.center); intersects = intersects || (distance < (bsWorld.radius + profile.width)); } //console.log(`${node.name}: ${intersects}`); return intersects; } deepestNodeAt(position){ const toObjectSpace = this.matrixWorld.clone().invert(); const objPos = position.clone().applyMatrix4(toObjectSpace); let current = this.root; while(true){ let containingChild = null; for(const child of current.children){ if(child !== undefined){ if(child.getBoundingBox().containsPoint(objPos)){ containingChild = child; } } } if(containingChild !== null && containingChild instanceof PointCloudOctreeNode){ current = containingChild; }else { break; } } const deepest = current; return deepest; } nodesOnRay (nodes, ray) { let nodesOnRay = []; let _ray = ray.clone(); for (let i = 0; i < nodes.length; i++) { let node = nodes[i]; let sphere = node.getBoundingSphere().clone().applyMatrix4(this.matrixWorld); if (_ray.intersectsSphere(sphere)) { nodesOnRay.push(node); } } return nodesOnRay; } updateMatrixWorld (force) { if (this.matrixAutoUpdate === true) this.updateMatrix(); if (this.matrixWorldNeedsUpdate === true || force === true) { if (!this.parent) { this.matrixWorld.copy(this.matrix); } else { this.matrixWorld.multiplyMatrices(this.parent.matrixWorld, this.matrix); } this.matrixWorldNeedsUpdate = false; force = true; } } hideDescendants (object) { let stack = []; for (let i = 0; i < object.children.length; i++) { let child = object.children[i]; if (child.visible) { stack.push(child); } } while (stack.length > 0) { let object = stack.shift(); object.visible = false; for (let i = 0; i < object.children.length; i++) { let child = object.children[i]; if (child.visible) { stack.push(child); } } } } moveToOrigin () { this.position.set(0, 0, 0); this.updateMatrixWorld(true); let box = this.boundingBox; let transform = this.matrixWorld; let tBox = Utils.computeTransformedBoundingBox(box, transform); this.position.set(0, 0, 0).sub(tBox.getCenter(new Vector3())); }; moveToGroundPlane () { this.updateMatrixWorld(true); let box = this.boundingBox; let transform = this.matrixWorld; let tBox = Utils.computeTransformedBoundingBox(box, transform); this.position.y += -tBox.min.y; }; getBoundingBoxWorld () { this.updateMatrixWorld(true); let box = this.boundingBox; let transform = this.matrixWorld; let tBox = Utils.computeTransformedBoundingBox(box, transform); return tBox; }; /** * returns points inside the profile points * * maxDepth: search points up to the given octree depth * * * The return value is an array with all segments of the profile path * let segment = { * start: THREE.Vector3, * end: THREE.Vector3, * points: {} * project: function() * }; * * The project() function inside each segment can be used to transform * that segments point coordinates to line up along the x-axis. * * */ getPointsInProfile (profile, maxDepth, callback) { if (callback) { let request = new Potree.ProfileRequest(this, profile, maxDepth, callback); this.profileRequests.push(request); return request; } let points = { segments: [], boundingBox: new Box3(), projectedBoundingBox: new Box2() }; // evaluate segments for (let i = 0; i < profile.points.length - 1; i++) { let start = profile.points[i]; let end = profile.points[i + 1]; let ps = this.getProfile(start, end, profile.width, maxDepth); let segment = { start: start, end: end, points: ps, project: null }; points.segments.push(segment); points.boundingBox.expandByPoint(ps.boundingBox.min); points.boundingBox.expandByPoint(ps.boundingBox.max); } // add projection functions to the segments let mileage = new Vector3(); for (let i = 0; i < points.segments.length; i++) { let segment = points.segments[i]; let start = segment.start; let end = segment.end; let project = (function (_start, _end, _mileage, _boundingBox) { let start = _start; let end = _end; let mileage = _mileage; let boundingBox = _boundingBox; let xAxis = new Vector3(1, 0, 0); let dir = new Vector3().subVectors(end, start); dir.y = 0; dir.normalize(); let alpha = Math.acos(xAxis.dot(dir)); if (dir.z > 0) { alpha = -alpha; } return function (position) { let toOrigin = new Matrix4().makeTranslation(-start.x, -boundingBox.min.y, -start.z); let alignWithX = new Matrix4().makeRotationY(-alpha); let applyMileage = new Matrix4().makeTranslation(mileage.x, 0, 0); let pos = position.clone(); pos.applyMatrix4(toOrigin); pos.applyMatrix4(alignWithX); pos.applyMatrix4(applyMileage); return pos; }; }(start, end, mileage.clone(), points.boundingBox.clone())); segment.project = project; mileage.x += new Vector3(start.x, 0, start.z).distanceTo(new Vector3(end.x, 0, end.z)); mileage.y += end.y - start.y; } points.projectedBoundingBox.min.x = 0; points.projectedBoundingBox.min.y = points.boundingBox.min.y; points.projectedBoundingBox.max.x = mileage.x; points.projectedBoundingBox.max.y = points.boundingBox.max.y; return points; } /** * returns points inside the given profile bounds. * * start: * end: * width: * depth: search points up to the given octree depth * callback: if specified, points are loaded before searching * * */ getProfile (start, end, width, depth, callback) { let request = new Potree.ProfileRequest(start, end, width, depth, callback); this.profileRequests.push(request); }; getVisibleExtent () { return this.visibleBounds.applyMatrix4(this.matrixWorld); }; intersectsPoint(position){ let rootAvailable = this.pcoGeometry.root && this.pcoGeometry.root.geometry; if(!rootAvailable){ return false; } if(typeof this.signedDistanceField === "undefined"){ const resolution = 32; const field = new Float32Array(resolution ** 3).fill(Infinity); const positions = this.pcoGeometry.root.geometry.attributes.position; const boundingBox = this.boundingBox; const n = positions.count; for(let i = 0; i < n; i = i + 3){ const x = positions.array[3 * i + 0]; const y = positions.array[3 * i + 1]; const z = positions.array[3 * i + 2]; const ix = parseInt(Math.min(resolution * (x / boundingBox.max.x), resolution - 1)); const iy = parseInt(Math.min(resolution * (y / boundingBox.max.y), resolution - 1)); const iz = parseInt(Math.min(resolution * (z / boundingBox.max.z), resolution - 1)); const index = ix + iy * resolution + iz * resolution * resolution; field[index] = 0; } const sdf = { resolution: resolution, field: field, }; this.signedDistanceField = sdf; } { const sdf = this.signedDistanceField; const boundingBox = this.boundingBox; const toObjectSpace = this.matrixWorld.clone().invert(); const objPos = position.clone().applyMatrix4(toObjectSpace); const resolution = sdf.resolution; const ix = parseInt(resolution * (objPos.x / boundingBox.max.x)); const iy = parseInt(resolution * (objPos.y / boundingBox.max.y)); const iz = parseInt(resolution * (objPos.z / boundingBox.max.z)); if(ix < 0 || iy < 0 || iz < 0){ return false; } if(ix >= resolution || iy >= resolution || iz >= resolution){ return false; } const index = ix + iy * resolution + iz * resolution * resolution; const value = sdf.field[index]; if(value === 0){ return true; } } return false; } /** * * * * params.pickWindowSize: Look for points inside a pixel window of this size. * Use odd values: 1, 3, 5, ... * * * TODO: only draw pixels that are actually read with readPixels(). * */ pick(viewer, camera, ray, params = {}){ let renderer = viewer.renderer; let pRenderer = viewer.pRenderer; performance.mark("pick-start"); let getVal = (a, b) => a !== undefined ? a : b; let pickWindowSize = getVal(params.pickWindowSize, 65); let pickOutsideClipRegion = getVal(params.pickOutsideClipRegion, false); let size = renderer.getSize(new Vector2()); let width = Math.ceil(getVal(params.width, size.width)); let height = Math.ceil(getVal(params.height, size.height)); 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; } if (!this.pickState) { let scene = new Scene(); let material = new Potree.PointCloudMaterial(); material.activeAttributeName = "indices"; let renderTarget = new WebGLRenderTarget( 1, 1, { minFilter: LinearFilter, magFilter: NearestFilter, format: 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(); if(params.pickClipped){ pickMaterial.clipBoxes = this.material.clipBoxes; pickMaterial.uniforms.clipBoxes = this.material.uniforms.clipBoxes; if(this.material.clipTask === Potree.ClipTask.HIGHLIGHT){ pickMaterial.clipTask = Potree.ClipTask.NONE; }else { pickMaterial.clipTask = this.material.clipTask; } pickMaterial.clipMethod = this.material.clipMethod; }else { pickMaterial.clipBoxes = []; } this.updateMaterial(pickMaterial, nodes, camera, renderer); } pickState.renderTarget.setSize(width, height); let pixelPos = new 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(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); 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 = w * h; let buffer = new Uint8Array(4 * pixelCount); gl.readPixels(x, y, pickWindowSize, pickWindowSize, gl.RGBA, gl.UNSIGNED_BYTE, buffer); 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 = []; 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]; pixels[4 * offset + 3] = 0; let pIndex = ibuffer[offset]; 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){ if(distance < hits[0].distanceToCenter){ hits[0] = hit; } }else { hits.push(hit); } } } } } // { // DEBUG: show panel with pick image // let img = Utils.pixelsArrayToImage(buffer, w, h); // let screenshot = img.src; // if(!this.debugDIV){ // this.debugDIV = $(` //
`); // $(document.body).append(this.debugDIV); // } // this.debugDIV.empty(); // this.debugDIV.append($(``)); // //$(this.debugWindow.document).append($(``)); // //this.debugWindow.document.write(''); // } 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 Vector3(x, y, z); position.applyMatrix4(pc.matrixWorld); point[attributeName] = 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; } performance.mark("pick-end"); performance.measure("pick", "pick-start", "pick-end"); if(params.all){ return hits.map(hit => hit.point); }else { if(hits.length === 0){ return null; }else { return hits[0].point; //let sorted = hits.sort( (a, b) => a.distanceToCenter - b.distanceToCenter); //return sorted[0].point; } } }; * getFittedBoxGen(boxNode){ let start = performance.now(); let shrinkedLocalBounds = new Box3(); let worldToBox = boxNode.matrixWorld.clone().invert(); for(let node of this.visibleNodes){ if(!node.sceneNode){ continue; } let buffer = node.geometryNode.buffer; let posOffset = buffer.offset("position"); let stride = buffer.stride; let view = new DataView(buffer.data); let objectToBox = new Matrix4().multiplyMatrices(worldToBox, node.sceneNode.matrixWorld); let pos = new Vector4(); for(let i = 0; i < buffer.numElements; i++){ let x = view.getFloat32(i * stride + posOffset + 0, true); let y = view.getFloat32(i * stride + posOffset + 4, true); let z = view.getFloat32(i * stride + posOffset + 8, true); pos.set(x, y, z, 1); pos.applyMatrix4(objectToBox); if(-0.5 < pos.x && pos.x < 0.5){ if(-0.5 < pos.y && pos.y < 0.5){ if(-0.5 < pos.z && pos.z < 0.5){ shrinkedLocalBounds.expandByPoint(pos); } } } } yield; } let fittedPosition = shrinkedLocalBounds.getCenter(new Vector3()).applyMatrix4(boxNode.matrixWorld); let fitted = new Object3D(); fitted.position.copy(fittedPosition); fitted.scale.copy(boxNode.scale); fitted.rotation.copy(boxNode.rotation); let ds = new Vector3().subVectors(shrinkedLocalBounds.max, shrinkedLocalBounds.min); fitted.scale.multiply(ds); let duration = performance.now() - start; console.log("duration: ", duration); yield fitted; } getFittedBox(boxNode, maxLevel = Infinity){ maxLevel = Infinity; let start = performance.now(); let shrinkedLocalBounds = new Box3(); let worldToBox = boxNode.matrixWorld.clone().invert(); for(let node of this.visibleNodes){ if(!node.sceneNode || node.getLevel() > maxLevel){ continue; } let buffer = node.geometryNode.buffer; let posOffset = buffer.offset("position"); let stride = buffer.stride; let view = new DataView(buffer.data); let objectToBox = new Matrix4().multiplyMatrices(worldToBox, node.sceneNode.matrixWorld); let pos = new Vector4(); for(let i = 0; i < buffer.numElements; i++){ let x = view.getFloat32(i * stride + posOffset + 0, true); let y = view.getFloat32(i * stride + posOffset + 4, true); let z = view.getFloat32(i * stride + posOffset + 8, true); pos.set(x, y, z, 1); pos.applyMatrix4(objectToBox); if(-0.5 < pos.x && pos.x < 0.5){ if(-0.5 < pos.y && pos.y < 0.5){ if(-0.5 < pos.z && pos.z < 0.5){ shrinkedLocalBounds.expandByPoint(pos); } } } } } let fittedPosition = shrinkedLocalBounds.getCenter(new Vector3()).applyMatrix4(boxNode.matrixWorld); let fitted = new Object3D(); fitted.position.copy(fittedPosition); fitted.scale.copy(boxNode.scale); fitted.rotation.copy(boxNode.rotation); let ds = new Vector3().subVectors(shrinkedLocalBounds.max, shrinkedLocalBounds.min); fitted.scale.multiply(ds); let duration = performance.now() - start; console.log("duration: ", duration); return fitted; } get progress () { return this.visibleNodes.length / this.visibleGeometry.length; } find(name){ let node = null; for(let char of name){ if(char === "r"){ node = this.root; }else { node = node.children[char]; } } return node; } get visible(){ return this._visible; } set visible(value){ if(value !== this._visible){ this._visible = value; this.dispatchEvent({type: 'visibility_changed', pointcloud: this}); } } } class PointCloudArena4DNode extends PointCloudTreeNode { constructor () { super(); this.left = null; this.right = null; this.sceneNode = null; this.kdtree = null; } getNumPoints () { return this.geometryNode.numPoints; } isLoaded () { return true; } isTreeNode () { return true; } isGeometryNode () { return false; } getLevel () { return this.geometryNode.level; } getBoundingSphere () { return this.geometryNode.boundingSphere; } getBoundingBox () { return this.geometryNode.boundingBox; } toTreeNode (child) { let geometryNode = null; if (this.left === child) { geometryNode = this.left; } else if (this.right === child) { geometryNode = this.right; } if (!geometryNode.loaded) { return; } let node = new PointCloudArena4DNode(); let sceneNode = PointCloud(geometryNode.geometry, this.kdtree.material); sceneNode.visible = false; node.kdtree = this.kdtree; node.geometryNode = geometryNode; node.sceneNode = sceneNode; node.parent = this; node.left = this.geometryNode.left; node.right = this.geometryNode.right; } getChildren () { let children = []; if (this.left) { children.push(this.left); } if (this.right) { children.push(this.right); } return children; } }; class PointCloudArena4D$1 extends PointCloudTree{ constructor (geometry) { super(); this.root = null; if (geometry.root) { this.root = geometry.root; } else { geometry.addEventListener('hierarchy_loaded', () => { this.root = geometry.root; }); } this.visiblePointsTarget = 2 * 1000 * 1000; this.minimumNodePixelSize = 150; this.position.sub(geometry.offset); this.updateMatrix(); this.numVisibleNodes = 0; this.numVisiblePoints = 0; this.boundingBoxNodes = []; this.loadQueue = []; this.visibleNodes = []; this.pcoGeometry = geometry; this.boundingBox = this.pcoGeometry.boundingBox; this.boundingSphere = this.pcoGeometry.boundingSphere; this.material = new PointCloudMaterial$1({vertexColors: VertexColors, size: 0.05, treeType: TreeType.KDTREE}); this.material.sizeType = PointSizeType.ATTENUATED; this.material.size = 0.05; this.profileRequests = []; this.name = ''; } getBoundingBoxWorld () { this.updateMatrixWorld(true); let box = this.boundingBox; let transform = this.matrixWorld; let tBox = Utils.computeTransformedBoundingBox(box, transform); return tBox; }; setName (name) { if (this.name !== name) { this.name = name; this.dispatchEvent({type: 'name_changed', name: name, pointcloud: this}); } } getName () { return this.name; } getLevel () { return this.level; } toTreeNode (geometryNode, parent) { let node = new PointCloudArena4DNode(); let sceneNode = new Points(geometryNode.geometry, this.material); sceneNode.frustumCulled = false; sceneNode.onBeforeRender = (_this, scene, camera, geometry, material, group) => { if (material.program) { _this.getContext().useProgram(material.program.program); if (material.program.getUniforms().map.level) { let level = geometryNode.getLevel(); material.uniforms.level.value = level; material.program.getUniforms().map.level.setValue(_this.getContext(), level); } if (this.visibleNodeTextureOffsets && material.program.getUniforms().map.vnStart) { let vnStart = this.visibleNodeTextureOffsets.get(node); material.uniforms.vnStart.value = vnStart; material.program.getUniforms().map.vnStart.setValue(_this.getContext(), vnStart); } if (material.program.getUniforms().map.pcIndex) { let i = node.pcIndex ? node.pcIndex : this.visibleNodes.indexOf(node); material.uniforms.pcIndex.value = i; material.program.getUniforms().map.pcIndex.setValue(_this.getContext(), i); } } }; node.geometryNode = geometryNode; node.sceneNode = sceneNode; node.pointcloud = this; node.left = geometryNode.left; node.right = geometryNode.right; if (!parent) { this.root = node; this.add(sceneNode); } else { parent.sceneNode.add(sceneNode); if (parent.left === geometryNode) { parent.left = node; } else if (parent.right === geometryNode) { parent.right = node; } } let disposeListener = function () { parent.sceneNode.remove(node.sceneNode); if (parent.left === node) { parent.left = geometryNode; } else if (parent.right === node) { parent.right = geometryNode; } }; geometryNode.oneTimeDisposeHandlers.push(disposeListener); return node; } updateMaterial (material, visibleNodes, camera, renderer) { material.fov = camera.fov * (Math.PI / 180); material.screenWidth = renderer.domElement.clientWidth; material.screenHeight = renderer.domElement.clientHeight; material.spacing = this.pcoGeometry.spacing; material.near = camera.near; material.far = camera.far; // reduce shader source updates by setting maxLevel slightly higher than actually necessary if (this.maxLevel > material.levels) { material.levels = this.maxLevel + 2; } // material.uniforms.octreeSize.value = this.boundingBox.size().x; let bbSize = this.boundingBox.getSize(new Vector3()); material.bbSize = [bbSize.x, bbSize.y, bbSize.z]; } updateVisibleBounds () { } hideDescendants (object) { let stack = []; for (let i = 0; i < object.children.length; i++) { let child = object.children[i]; if (child.visible) { stack.push(child); } } while (stack.length > 0) { let child = stack.shift(); child.visible = false; if (child.boundingBoxNode) { child.boundingBoxNode.visible = false; } for (let i = 0; i < child.children.length; i++) { let childOfChild = child.children[i]; if (childOfChild.visible) { stack.push(childOfChild); } } } } updateMatrixWorld (force) { // node.matrixWorld.multiplyMatrices( node.parent.matrixWorld, node.matrix ); if (this.matrixAutoUpdate === true) this.updateMatrix(); if (this.matrixWorldNeedsUpdate === true || force === true) { if (this.parent === undefined) { this.matrixWorld.copy(this.matrix); } else { this.matrixWorld.multiplyMatrices(this.parent.matrixWorld, this.matrix); } this.matrixWorldNeedsUpdate = false; force = true; } } nodesOnRay (nodes, ray) { let nodesOnRay = []; let _ray = ray.clone(); for (let i = 0; i < nodes.length; i++) { let node = nodes[i]; let sphere = node.getBoundingSphere().clone().applyMatrix4(node.sceneNode.matrixWorld); // TODO Unused: let box = node.getBoundingBox().clone().applyMatrix4(node.sceneNode.matrixWorld); if (_ray.intersectsSphere(sphere)) { nodesOnRay.push(node); } // if(_ray.isIntersectionBox(box)){ // nodesOnRay.push(node); // } } return nodesOnRay; } pick(viewer, camera, ray, params = {}){ let renderer = viewer.renderer; let pRenderer = viewer.pRenderer; performance.mark("pick-start"); let getVal = (a, b) => a !== undefined ? a : b; let pickWindowSize = getVal(params.pickWindowSize, 17); let pickOutsideClipRegion = getVal(params.pickOutsideClipRegion, false); let size = renderer.getSize(new Vector2()); let width = Math.ceil(getVal(params.width, size.width)); let height = Math.ceil(getVal(params.height, size.height)); 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; } if (!this.pickState) { let scene = new Scene(); let material = new PointCloudMaterial$1(); material.activeAttributeName = "indices"; let renderTarget = new WebGLRenderTarget( 1, 1, { minFilter: LinearFilter, magFilter: NearestFilter, format: 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.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; if(params.pickClipped){ pickMaterial.clipBoxes = this.material.clipBoxes; if(this.material.clipTask === ClipTask.HIGHLIGHT){ pickMaterial.clipTask = ClipTask.NONE; }else { pickMaterial.clipTask = this.material.clipTask; } }else { pickMaterial.clipBoxes = []; } this.updateMaterial(pickMaterial, nodes, camera, renderer); } pickState.renderTarget.setSize(width, height); let pixelPos = new 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(NoBlending); renderer.clearTarget(pickState.renderTarget, true, true, true); { // RENDER renderer.setRenderTarget(pickState.renderTarget); gl.clearColor(0, 0, 0, 0); renderer.clearTarget( pickState.renderTarget, true, true, true ); let tmp = this.material; this.material = pickMaterial; pRenderer.renderOctree(this, nodes, camera, pickState.renderTarget); 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 = w * h; let buffer = new Uint8Array(4 * pixelCount); gl.readPixels(x, y, pickWindowSize, pickWindowSize, gl.RGBA, gl.UNSIGNED_BYTE, buffer); 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 = []; 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]; pixels[4 * offset + 3] = 0; let pIndex = ibuffer[offset]; 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){ if(distance < hits[0].distanceToCenter){ hits[0] = hit; } }else { hits.push(hit); } } } } } 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 Vector3(x, y, z); position.applyMatrix4(pc.matrixWorld); point[attributeName] = position; } else if (attributeName === 'indices') { } else { //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; } performance.mark("pick-end"); performance.measure("pick", "pick-start", "pick-end"); if(params.all){ return hits.map(hit => hit.point); }else { if(hits.length === 0){ return null; }else { return hits[0].point; } } } computeVisibilityTextureData(nodes){ if(exports.measureTimings) performance.mark("computeVisibilityTextureData-start"); let data = new Uint8Array(nodes.length * 3); let visibleNodeTextureOffsets = new Map(); // copy array nodes = nodes.slice(); // sort by level and number let sort = function (a, b) { let la = a.geometryNode.level; let lb = b.geometryNode.level; let na = a.geometryNode.number; let nb = b.geometryNode.number; if (la !== lb) return la - lb; if (na < nb) return -1; if (na > nb) return 1; return 0; }; nodes.sort(sort); let visibleNodeNames = []; for (let i = 0; i < nodes.length; i++) { visibleNodeNames.push(nodes[i].geometryNode.number); } for (let i = 0; i < nodes.length; i++) { let node = nodes[i]; visibleNodeTextureOffsets.set(node, i); let b1 = 0; // children let b2 = 0; // offset to first child let b3 = 0; // split if (node.geometryNode.left && visibleNodeNames.indexOf(node.geometryNode.left.number) > 0) { b1 += 1; b2 = visibleNodeNames.indexOf(node.geometryNode.left.number) - i; } if (node.geometryNode.right && visibleNodeNames.indexOf(node.geometryNode.right.number) > 0) { b1 += 2; b2 = (b2 === 0) ? visibleNodeNames.indexOf(node.geometryNode.right.number) - i : b2; } if (node.geometryNode.split === 'X') { b3 = 1; } else if (node.geometryNode.split === 'Y') { b3 = 2; } else if (node.geometryNode.split === 'Z') { b3 = 4; } data[i * 3 + 0] = b1; data[i * 3 + 1] = b2; data[i * 3 + 2] = b3; } if(exports.measureTimings){ performance.mark("computeVisibilityTextureData-end"); performance.measure("render.computeVisibilityTextureData", "computeVisibilityTextureData-start", "computeVisibilityTextureData-end"); } return { data: data, offsets: visibleNodeTextureOffsets }; } get progress () { if (this.pcoGeometry.root) { return exports.numNodesLoading > 0 ? 0 : 1; } else { return 0; } } }; // Copied from three.js: WebGLRenderer.js function paramThreeToGL(_gl, p) { let extension; if (p === RepeatWrapping) return _gl.REPEAT; if (p === ClampToEdgeWrapping) return _gl.CLAMP_TO_EDGE; if (p === MirroredRepeatWrapping) return _gl.MIRRORED_REPEAT; if (p === NearestFilter) return _gl.NEAREST; if (p === NearestMipMapNearestFilter) return _gl.NEAREST_MIPMAP_NEAREST; if (p === NearestMipMapLinearFilter) return _gl.NEAREST_MIPMAP_LINEAR; if (p === LinearFilter) return _gl.LINEAR; if (p === LinearMipMapNearestFilter) return _gl.LINEAR_MIPMAP_NEAREST; if (p === LinearMipMapLinearFilter) return _gl.LINEAR_MIPMAP_LINEAR; if (p === UnsignedByteType) return _gl.UNSIGNED_BYTE; if (p === UnsignedShort4444Type) return _gl.UNSIGNED_SHORT_4_4_4_4; if (p === UnsignedShort5551Type) return _gl.UNSIGNED_SHORT_5_5_5_1; if (p === UnsignedShort565Type) return _gl.UNSIGNED_SHORT_5_6_5; if (p === ByteType) return _gl.BYTE; if (p === ShortType) return _gl.SHORT; if (p === UnsignedShortType) return _gl.UNSIGNED_SHORT; if (p === IntType) return _gl.INT; if (p === UnsignedIntType) return _gl.UNSIGNED_INT; if (p === FloatType) return _gl.FLOAT; if (p === HalfFloatType) { extension = extensions.get('OES_texture_half_float'); if (extension !== null) return extension.HALF_FLOAT_OES; } if (p === AlphaFormat) return _gl.ALPHA; if (p === RGBFormat) return _gl.RGB; if (p === RGBAFormat) return _gl.RGBA; if (p === LuminanceFormat) return _gl.LUMINANCE; if (p === LuminanceAlphaFormat) return _gl.LUMINANCE_ALPHA; if (p === DepthFormat) return _gl.DEPTH_COMPONENT; if (p === DepthStencilFormat) return _gl.DEPTH_STENCIL; if (p === AddEquation) return _gl.FUNC_ADD; if (p === SubtractEquation) return _gl.FUNC_SUBTRACT; if (p === ReverseSubtractEquation) return _gl.FUNC_REVERSE_SUBTRACT; if (p === ZeroFactor) return _gl.ZERO; if (p === OneFactor) return _gl.ONE; if (p === SrcColorFactor) return _gl.SRC_COLOR; if (p === OneMinusSrcColorFactor) return _gl.ONE_MINUS_SRC_COLOR; if (p === SrcAlphaFactor) return _gl.SRC_ALPHA; if (p === OneMinusSrcAlphaFactor) return _gl.ONE_MINUS_SRC_ALPHA; if (p === DstAlphaFactor) return _gl.DST_ALPHA; if (p === OneMinusDstAlphaFactor) return _gl.ONE_MINUS_DST_ALPHA; if (p === DstColorFactor) return _gl.DST_COLOR; if (p === OneMinusDstColorFactor) return _gl.ONE_MINUS_DST_COLOR; if (p === SrcAlphaSaturateFactor) return _gl.SRC_ALPHA_SATURATE; if (p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format || p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format) { extension = extensions.get('WEBGL_compressed_texture_s3tc'); if (extension !== null) { if (p === RGB_S3TC_DXT1_Format) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT; if (p === RGBA_S3TC_DXT1_Format$1) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT; if (p === RGBA_S3TC_DXT3_Format) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT; if (p === RGBA_S3TC_DXT5_Format$1) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT; } } if (p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format || p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format) { extension = extensions.get('WEBGL_compressed_texture_pvrtc'); if (extension !== null) { if (p === RGB_PVRTC_4BPPV1_Format) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; if (p === RGB_PVRTC_2BPPV1_Format) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; if (p === RGBA_PVRTC_4BPPV1_Format) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; if (p === RGBA_PVRTC_2BPPV1_Format) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; } } if (p === RGB_ETC1_Format) { extension = extensions.get('WEBGL_compressed_texture_etc1'); if (extension !== null) return extension.COMPRESSED_RGB_ETC1_WEBGL; } if (p === MinEquation || p === MaxEquation) { extension = extensions.get('EXT_blend_minmax'); if (extension !== null) { if (p === MinEquation) return extension.MIN_EXT; if (p === MaxEquation) return extension.MAX_EXT; } } if (p === UnsignedInt248Type) { extension = extensions.get('WEBGL_depth_texture'); if (extension !== null) return extension.UNSIGNED_INT_24_8_WEBGL; } return 0; }; let attributeLocations = { "position": {name: "position", location: 0}, "color": {name: "color", location: 1}, "rgba": {name: "color", location: 1}, "intensity": {name: "intensity", location: 2}, "classification": {name: "classification", location: 3}, "returnNumber": {name: "returnNumber", location: 4}, "return number": {name: "returnNumber", location: 4}, "returns": {name: "returnNumber", location: 4}, "numberOfReturns": {name: "numberOfReturns", location: 5}, "number of returns": {name: "numberOfReturns", location: 5}, "pointSourceID": {name: "pointSourceID", location: 6}, "source id": {name: "pointSourceID", location: 6}, "point source id": {name: "pointSourceID", location: 6}, "indices": {name: "indices", location: 7}, "normal": {name: "normal", location: 8}, "spacing": {name: "spacing", location: 9}, "gps-time": {name: "gpsTime", location: 10}, "aExtra": {name: "aExtra", location: 11}, }; class Shader { constructor(gl, name, vsSource, fsSource) { this.gl = gl; this.name = name; this.vsSource = vsSource; this.fsSource = fsSource; this.cache = new Map(); this.vs = null; this.fs = null; this.program = null; this.uniformLocations = {}; this.attributeLocations = {}; this.uniformBlockIndices = {}; this.uniformBlocks = {}; this.uniforms = {}; this.update(vsSource, fsSource); } update(vsSource, fsSource) { this.vsSource = vsSource; this.fsSource = fsSource; this.linkProgram(); } compileShader(shader, source){ let gl = this.gl; gl.shaderSource(shader, source); gl.compileShader(shader); let success = gl.getShaderParameter(shader, gl.COMPILE_STATUS); if (!success) { let info = gl.getShaderInfoLog(shader); let numberedSource = source.split("\n").map((a, i) => `${i + 1}`.padEnd(5) + a).join("\n"); throw `could not compile shader ${this.name}: ${info}, \n${numberedSource}`; } } linkProgram() { const tStart = performance.now(); let gl = this.gl; this.uniformLocations = {}; this.attributeLocations = {}; this.uniforms = {}; gl.useProgram(null); let cached = this.cache.get(`${this.vsSource}, ${this.fsSource}`); if (cached) { this.program = cached.program; this.vs = cached.vs; this.fs = cached.fs; this.attributeLocations = cached.attributeLocations; this.uniformLocations = cached.uniformLocations; this.uniformBlocks = cached.uniformBlocks; this.uniforms = cached.uniforms; return; } else { this.vs = gl.createShader(gl.VERTEX_SHADER); this.fs = gl.createShader(gl.FRAGMENT_SHADER); this.program = gl.createProgram(); for(let name of Object.keys(attributeLocations)){ let location = attributeLocations[name].location; let glslName = attributeLocations[name].name; gl.bindAttribLocation(this.program, location, glslName); } this.compileShader(this.vs, this.vsSource); this.compileShader(this.fs, this.fsSource); let program = this.program; gl.attachShader(program, this.vs); gl.attachShader(program, this.fs); gl.linkProgram(program); gl.detachShader(program, this.vs); gl.detachShader(program, this.fs); let success = gl.getProgramParameter(program, gl.LINK_STATUS); if (!success) { let info = gl.getProgramInfoLog(program); throw `could not link program ${this.name}: ${info}`; } { // attribute locations let numAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES); for (let i = 0; i < numAttributes; i++) { let attribute = gl.getActiveAttrib(program, i); let location = gl.getAttribLocation(program, attribute.name); this.attributeLocations[attribute.name] = location; } } { // uniform locations let numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS); for (let i = 0; i < numUniforms; i++) { let uniform = gl.getActiveUniform(program, i); let location = gl.getUniformLocation(program, uniform.name); this.uniformLocations[uniform.name] = location; this.uniforms[uniform.name] = { location: location, value: null, }; } } // uniform blocks if(typeof WebGL2RenderingContext != 'undefined' && gl instanceof WebGL2RenderingContext){ let numBlocks = gl.getProgramParameter(program, gl.ACTIVE_UNIFORM_BLOCKS); for (let i = 0; i < numBlocks; i++) { let blockName = gl.getActiveUniformBlockName(program, i); let blockIndex = gl.getUniformBlockIndex(program, blockName); this.uniformBlockIndices[blockName] = blockIndex; gl.uniformBlockBinding(program, blockIndex, blockIndex); let dataSize = gl.getActiveUniformBlockParameter(program, blockIndex, gl.UNIFORM_BLOCK_DATA_SIZE); let uBuffer = gl.createBuffer(); gl.bindBuffer(gl.UNIFORM_BUFFER, uBuffer); gl.bufferData(gl.UNIFORM_BUFFER, dataSize, gl.DYNAMIC_READ); gl.bindBufferBase(gl.UNIFORM_BUFFER, blockIndex, uBuffer); gl.bindBuffer(gl.UNIFORM_BUFFER, null); this.uniformBlocks[blockName] = { name: blockName, index: blockIndex, dataSize: dataSize, buffer: uBuffer }; } } let cached = { program: this.program, vs: this.vs, fs: this.fs, attributeLocations: this.attributeLocations, uniformLocations: this.uniformLocations, uniforms: this.uniforms, uniformBlocks: this.uniformBlocks, }; this.cache.set(`${this.vsSource}, ${this.fsSource}`, cached); } const tEnd = performance.now(); const duration = tEnd - tStart; console.log(`shader compile duration: ${duration.toFixed(3)}`); } setUniformMatrix4(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } let tmp = new Float32Array(value.elements); gl.uniformMatrix4fv(location, false, tmp); } setUniform1f(name, value) { const gl = this.gl; const uniform = this.uniforms[name]; if (uniform === undefined) { return; } if(uniform.value === value){ return; } uniform.value = value; gl.uniform1f(uniform.location, value); } setUniformBoolean(name, value) { const gl = this.gl; const uniform = this.uniforms[name]; if (uniform === undefined) { return; } if(uniform.value === value){ return; } uniform.value = value; gl.uniform1i(uniform.location, value); } setUniformTexture(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform1i(location, value); } setUniform2f(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform2f(location, value[0], value[1]); } setUniform3f(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform3f(location, value[0], value[1], value[2]); } setUniform(name, value) { if (value.constructor === Matrix4) { this.setUniformMatrix4(name, value); } else if (typeof value === "number") { this.setUniform1f(name, value); } else if (typeof value === "boolean") { this.setUniformBoolean(name, value); } else if (value instanceof WebGLTexture) { this.setUniformTexture(name, value); } else if (value instanceof Array) { if (value.length === 2) { this.setUniform2f(name, value); } else if (value.length === 3) { this.setUniform3f(name, value); } } else { console.error("unhandled uniform type: ", name, value); } } setUniform1i(name, value) { let gl = this.gl; let location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform1i(location, value); } }; class WebGLTexture { constructor(gl, texture) { this.gl = gl; this.texture = texture; this.id = gl.createTexture(); this.target = gl.TEXTURE_2D; this.version = -1; this.update(texture); } update() { if (!this.texture.image) { this.version = this.texture.version; return; } let gl = this.gl; let texture = this.texture; if (this.version === texture.version) { return; } this.target = gl.TEXTURE_2D; gl.bindTexture(this.target, this.id); let level = 0; let internalFormat = paramThreeToGL(gl, texture.format); let width = texture.image.width; let height = texture.image.height; let border = 0; let srcFormat = internalFormat; let srcType = paramThreeToGL(gl, texture.type); let data; gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, texture.flipY); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha); gl.pixelStorei(gl.UNPACK_ALIGNMENT, texture.unpackAlignment); if (texture instanceof DataTexture) { data = texture.image.data; gl.texParameteri(this.target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(this.target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.texParameteri(this.target, gl.TEXTURE_MAG_FILTER, paramThreeToGL(gl, texture.magFilter)); gl.texParameteri(this.target, gl.TEXTURE_MIN_FILTER, paramThreeToGL(gl, texture.minFilter)); gl.texImage2D(this.target, level, internalFormat, width, height, border, srcFormat, srcType, data); } else if ((texture instanceof CanvasTexture) || (texture instanceof Texture)) { data = texture.image; gl.texParameteri(this.target, gl.TEXTURE_WRAP_S, paramThreeToGL(gl, texture.wrapS)); gl.texParameteri(this.target, gl.TEXTURE_WRAP_T, paramThreeToGL(gl, texture.wrapT)); gl.texParameteri(this.target, gl.TEXTURE_MAG_FILTER, paramThreeToGL(gl, texture.magFilter)); gl.texParameteri(this.target, gl.TEXTURE_MIN_FILTER, paramThreeToGL(gl, texture.minFilter)); gl.texImage2D(this.target, level, internalFormat, internalFormat, srcType, data); if (texture instanceof Texture) {gl.generateMipmap(gl.TEXTURE_2D);} } gl.bindTexture(this.target, null); this.version = texture.version; } }; class WebGLBuffer { constructor() { this.numElements = 0; this.vao = null; this.vbos = new Map(); } }; class Renderer { constructor(threeRenderer) { this.threeRenderer = threeRenderer; this.gl = this.threeRenderer.getContext(); this.buffers = new Map(); this.shaders = new Map(); this.textures = new Map(); this.glTypeMapping = new Map(); this.glTypeMapping.set(Float32Array, this.gl.FLOAT); this.glTypeMapping.set(Uint8Array, this.gl.UNSIGNED_BYTE); this.glTypeMapping.set(Uint16Array, this.gl.UNSIGNED_SHORT); this.toggle = 0; } deleteBuffer(geometry) { let gl = this.gl; let webglBuffer = this.buffers.get(geometry); if (webglBuffer != null) { for (let attributeName in geometry.attributes) { gl.deleteBuffer(webglBuffer.vbos.get(attributeName).handle); } this.buffers.delete(geometry); } } createBuffer(geometry){ let gl = this.gl; let webglBuffer = new WebGLBuffer(); webglBuffer.vao = gl.createVertexArray(); webglBuffer.numElements = geometry.attributes.position.count; gl.bindVertexArray(webglBuffer.vao); for(let attributeName in geometry.attributes){ let bufferAttribute = geometry.attributes[attributeName]; let vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, bufferAttribute.array, gl.STATIC_DRAW); let normalized = bufferAttribute.normalized; let type = this.glTypeMapping.get(bufferAttribute.array.constructor); if(attributeLocations[attributeName] === undefined){ //attributeLocation = attributeLocations["aExtra"]; }else { let attributeLocation = attributeLocations[attributeName].location; gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } webglBuffer.vbos.set(attributeName, { handle: vbo, name: attributeName, count: bufferAttribute.count, itemSize: bufferAttribute.itemSize, type: geometry.attributes.position.array.constructor, version: 0 }); } gl.bindBuffer(gl.ARRAY_BUFFER, null); gl.bindVertexArray(null); let disposeHandler = (event) => { this.deleteBuffer(geometry); geometry.removeEventListener("dispose", disposeHandler); }; geometry.addEventListener("dispose", disposeHandler); return webglBuffer; } updateBuffer(geometry){ let gl = this.gl; let webglBuffer = this.buffers.get(geometry); gl.bindVertexArray(webglBuffer.vao); for(let attributeName in geometry.attributes){ let bufferAttribute = geometry.attributes[attributeName]; let normalized = bufferAttribute.normalized; let type = this.glTypeMapping.get(bufferAttribute.array.constructor); let vbo = null; if(!webglBuffer.vbos.has(attributeName)){ vbo = gl.createBuffer(); webglBuffer.vbos.set(attributeName, { handle: vbo, name: attributeName, count: bufferAttribute.count, itemSize: bufferAttribute.itemSize, type: geometry.attributes.position.array.constructor, version: bufferAttribute.version }); }else { vbo = webglBuffer.vbos.get(attributeName).handle; webglBuffer.vbos.get(attributeName).version = bufferAttribute.version; } gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, bufferAttribute.array, gl.STATIC_DRAW); if(attributeLocations[attributeName] === undefined){ //attributeLocation = attributeLocations["aExtra"]; }else { let attributeLocation = attributeLocations[attributeName].location; gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } } gl.bindBuffer(gl.ARRAY_BUFFER, null); gl.bindVertexArray(null); } traverse(scene) { let octrees = []; let stack = [scene]; while (stack.length > 0) { let node = stack.pop(); if (node instanceof PointCloudTree) { octrees.push(node); continue; } let visibleChildren = node.children.filter(c => c.visible); stack.push(...visibleChildren); } let result = { octrees: octrees }; return result; } renderNodes(octree, nodes, visibilityTextureData, camera, target, shader, params) { if (exports.measureTimings) performance.mark("renderNodes-start"); let gl = this.gl; let material = params.material ? params.material : octree.material; let shadowMaps = params.shadowMaps == null ? [] : params.shadowMaps; let view = camera.matrixWorldInverse; if(params.viewOverride){ view = params.viewOverride; } let worldView = new Matrix4(); let mat4holder = new Float32Array(16); let i = 0; for (let node of nodes) { if(exports.debug.allowedNodes !== undefined){ if(!exports.debug.allowedNodes.includes(node.name)){ continue; } } let world = node.sceneNode.matrixWorld; worldView.multiplyMatrices(view, world); if (visibilityTextureData) { let vnStart = visibilityTextureData.offsets.get(node); shader.setUniform1f("uVNStart", vnStart); } let level = node.getLevel(); if(node.debug){ shader.setUniform("uDebug", true); }else { shader.setUniform("uDebug", false); } // let isLeaf = false; // if(node instanceof PointCloudOctreeNode){ // isLeaf = Object.keys(node.children).length === 0; // }else if(node instanceof PointCloudArena4DNode){ // isLeaf = node.geometryNode.isLeaf; // } // shader.setUniform("uIsLeafNode", isLeaf); // let isLeaf = node.children.filter(n => n != null).length === 0; // if(!isLeaf){ // continue; // } // TODO consider passing matrices in an array to avoid uniformMatrix4fv overhead const lModel = shader.uniformLocations["modelMatrix"]; if (lModel) { mat4holder.set(world.elements); gl.uniformMatrix4fv(lModel, false, mat4holder); } const lModelView = shader.uniformLocations["modelViewMatrix"]; //mat4holder.set(worldView.elements); // faster then set in chrome 63 for(let j = 0; j < 16; j++){ mat4holder[j] = worldView.elements[j]; } gl.uniformMatrix4fv(lModelView, false, mat4holder); { // Clip Polygons if(material.clipPolygons && material.clipPolygons.length > 0){ let clipPolygonVCount = []; let worldViewProjMatrices = []; for(let clipPolygon of material.clipPolygons){ let view = clipPolygon.viewMatrix; let proj = clipPolygon.projMatrix; let worldViewProj = proj.clone().multiply(view).multiply(world); clipPolygonVCount.push(clipPolygon.markers.length); worldViewProjMatrices.push(worldViewProj); } let flattenedMatrices = [].concat(...worldViewProjMatrices.map(m => m.elements)); let flattenedVertices = new Array(8 * 3 * material.clipPolygons.length); for(let i = 0; i < material.clipPolygons.length; i++){ let clipPolygon = material.clipPolygons[i]; for(let j = 0; j < clipPolygon.markers.length; j++){ flattenedVertices[i * 24 + (j * 3 + 0)] = clipPolygon.markers[j].position.x; flattenedVertices[i * 24 + (j * 3 + 1)] = clipPolygon.markers[j].position.y; flattenedVertices[i * 24 + (j * 3 + 2)] = clipPolygon.markers[j].position.z; } } const lClipPolygonVCount = shader.uniformLocations["uClipPolygonVCount[0]"]; gl.uniform1iv(lClipPolygonVCount, clipPolygonVCount); const lClipPolygonVP = shader.uniformLocations["uClipPolygonWVP[0]"]; gl.uniformMatrix4fv(lClipPolygonVP, false, flattenedMatrices); const lClipPolygons = shader.uniformLocations["uClipPolygonVertices[0]"]; gl.uniform3fv(lClipPolygons, flattenedVertices); } } //shader.setUniformMatrix4("modelMatrix", world); //shader.setUniformMatrix4("modelViewMatrix", worldView); shader.setUniform1f("uLevel", level); shader.setUniform1f("uNodeSpacing", node.geometryNode.estimatedSpacing); shader.setUniform1f("uPCIndex", i); // uBBSize if (shadowMaps.length > 0) { const lShadowMap = shader.uniformLocations["uShadowMap[0]"]; shader.setUniform3f("uShadowColor", material.uniforms.uShadowColor.value); let bindingStart = 5; let bindingPoints = new Array(shadowMaps.length).fill(bindingStart).map((a, i) => (a + i)); gl.uniform1iv(lShadowMap, bindingPoints); for (let i = 0; i < shadowMaps.length; i++) { let shadowMap = shadowMaps[i]; let bindingPoint = bindingPoints[i]; let glTexture = this.threeRenderer.properties.get(shadowMap.target.texture).__webglTexture; gl.activeTexture(gl[`TEXTURE${bindingPoint}`]); gl.bindTexture(gl.TEXTURE_2D, glTexture); } { let worldViewMatrices = shadowMaps .map(sm => sm.camera.matrixWorldInverse) .map(view => new Matrix4().multiplyMatrices(view, world)); let flattenedMatrices = [].concat(...worldViewMatrices.map(c => c.elements)); const lWorldView = shader.uniformLocations["uShadowWorldView[0]"]; gl.uniformMatrix4fv(lWorldView, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...shadowMaps.map(sm => sm.camera.projectionMatrix.elements)); const lProj = shader.uniformLocations["uShadowProj[0]"]; gl.uniformMatrix4fv(lProj, false, flattenedMatrices); } } const geometry = node.geometryNode.geometry; if(geometry.attributes["gps-time"]){ const bufferAttribute = geometry.attributes["gps-time"]; const attGPS = octree.getAttribute("gps-time"); let initialRange = attGPS.initialRange; let initialRangeSize = initialRange[1] - initialRange[0]; let globalRange = attGPS.range; let globalRangeSize = globalRange[1] - globalRange[0]; let scale = initialRangeSize / globalRangeSize; let offset = -(globalRange[0] - initialRange[0]) / initialRangeSize; scale = Number.isNaN(scale) ? 1 : scale; offset = Number.isNaN(offset) ? 0 : offset; shader.setUniform1f("uGpsScale", scale); shader.setUniform1f("uGpsOffset", offset); //shader.setUniform2f("uFilterGPSTimeClipRange", [-Infinity, Infinity]); let uFilterGPSTimeClipRange = material.uniforms.uFilterGPSTimeClipRange.value; // let gpsCliPRangeMin = uFilterGPSTimeClipRange[0] // let gpsCliPRangeMax = uFilterGPSTimeClipRange[1] // shader.setUniform2f("uFilterGPSTimeClipRange", [gpsCliPRangeMin, gpsCliPRangeMax]); let normalizedClipRange = [ (uFilterGPSTimeClipRange[0] - globalRange[0]) / globalRangeSize, (uFilterGPSTimeClipRange[1] - globalRange[0]) / globalRangeSize, ]; shader.setUniform2f("uFilterGPSTimeClipRange", normalizedClipRange); // // ranges in full gps coordinate system // const globalRange = attGPS.range; // const bufferRange = bufferAttribute.potree.range; // // ranges in [0, 1] // // normalizedGlobalRange = [0, 1] // // normalizedBufferRange: norm buffer within norm global range e.g. [0.2, 0.8] // const globalWidth = globalRange[1] - globalRange[0]; // const normalizedBufferRange = [ // (bufferRange[0] - globalRange[0]) / globalWidth, // (bufferRange[1] - globalRange[0]) / globalWidth, // ]; // shader.setUniform2f("uNormalizedGpsBufferRange", normalizedBufferRange); // let uFilterGPSTimeClipRange = material.uniforms.uFilterGPSTimeClipRange.value; // let gpsCliPRangeMin = uFilterGPSTimeClipRange[0] // let gpsCliPRangeMax = uFilterGPSTimeClipRange[1] // shader.setUniform2f("uFilterGPSTimeClipRange", [gpsCliPRangeMin, gpsCliPRangeMax]); // shader.setUniform1f("uGpsScale", bufferAttribute.potree.scale); // shader.setUniform1f("uGpsOffset", bufferAttribute.potree.offset); } { let uFilterReturnNumberRange = material.uniforms.uFilterReturnNumberRange.value; let uFilterNumberOfReturnsRange = material.uniforms.uFilterNumberOfReturnsRange.value; let uFilterPointSourceIDClipRange = material.uniforms.uFilterPointSourceIDClipRange.value; shader.setUniform2f("uFilterReturnNumberRange", uFilterReturnNumberRange); shader.setUniform2f("uFilterNumberOfReturnsRange", uFilterNumberOfReturnsRange); shader.setUniform2f("uFilterPointSourceIDClipRange", uFilterPointSourceIDClipRange); } let webglBuffer = null; if(!this.buffers.has(geometry)){ webglBuffer = this.createBuffer(geometry); this.buffers.set(geometry, webglBuffer); }else { webglBuffer = this.buffers.get(geometry); for(let attributeName in geometry.attributes){ let attribute = geometry.attributes[attributeName]; if(attribute.version > webglBuffer.vbos.get(attributeName).version){ this.updateBuffer(geometry); } } } gl.bindVertexArray(webglBuffer.vao); let isExtraAttribute = attributeLocations[material.activeAttributeName] === undefined && Object.keys(geometry.attributes).includes(material.activeAttributeName); if(isExtraAttribute){ const attributeLocation = attributeLocations["aExtra"].location; for(const attributeName in geometry.attributes){ const bufferAttribute = geometry.attributes[attributeName]; const vbo = webglBuffer.vbos.get(attributeName); gl.bindBuffer(gl.ARRAY_BUFFER, vbo.handle); gl.disableVertexAttribArray(attributeLocation); } const attName = material.activeAttributeName; const bufferAttribute = geometry.attributes[attName]; const vbo = webglBuffer.vbos.get(attName); if(bufferAttribute !== undefined && vbo !== undefined){ let type = this.glTypeMapping.get(bufferAttribute.array.constructor); let normalized = bufferAttribute.normalized; gl.bindBuffer(gl.ARRAY_BUFFER, vbo.handle); gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } { const attExtra = octree.pcoGeometry.pointAttributes.attributes .find(a => a.name === attName); let range = material.getRange(attName); if(!range){ range = attExtra.range; } if(!range){ range = [0, 1]; } let initialRange = attExtra.initialRange; let initialRangeSize = initialRange[1] - initialRange[0]; let globalRange = range; let globalRangeSize = globalRange[1] - globalRange[0]; let scale = initialRangeSize / globalRangeSize; let offset = -(globalRange[0] - initialRange[0]) / initialRangeSize; scale = Number.isNaN(scale) ? 1 : scale; offset = Number.isNaN(offset) ? 0 : offset; shader.setUniform1f("uExtraScale", scale); shader.setUniform1f("uExtraOffset", offset); } }else { for(const attributeName in geometry.attributes){ const bufferAttribute = geometry.attributes[attributeName]; const vbo = webglBuffer.vbos.get(attributeName); if(attributeLocations[attributeName] !== undefined){ const attributeLocation = attributeLocations[attributeName].location; let type = this.glTypeMapping.get(bufferAttribute.array.constructor); let normalized = bufferAttribute.normalized; gl.bindBuffer(gl.ARRAY_BUFFER, vbo.handle); gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } } } let numPoints = webglBuffer.numElements; gl.drawArrays(gl.POINTS, 0, numPoints); i++; } gl.bindVertexArray(null); if (exports.measureTimings) { performance.mark("renderNodes-end"); performance.measure("render.renderNodes", "renderNodes-start", "renderNodes-end"); } } renderOctree(octree, nodes, camera, target, params = {}){ let gl = this.gl; let material = params.material ? params.material : octree.material; let shadowMaps = params.shadowMaps == null ? [] : params.shadowMaps; let view = camera.matrixWorldInverse; let viewInv = camera.matrixWorld; if(params.viewOverride){ view = params.viewOverride; viewInv = view.clone().invert(); } let proj = camera.projectionMatrix; let projInv = proj.clone().invert(); //let worldView = new THREE.Matrix4(); let shader = null; let visibilityTextureData = null; let currentTextureBindingPoint = 0; if (material.pointSizeType >= 0) { if (material.pointSizeType === PointSizeType.ADAPTIVE || material.activeAttributeName === "level of detail") { let vnNodes = (params.vnTextureNodes != null) ? params.vnTextureNodes : nodes; visibilityTextureData = octree.computeVisibilityTextureData(vnNodes, camera); const vnt = material.visibleNodesTexture; const data = vnt.image.data; data.set(visibilityTextureData.data); vnt.needsUpdate = true; } } { // UPDATE SHADER AND TEXTURES if (!this.shaders.has(material)) { let [vs, fs] = [material.vertexShader, material.fragmentShader]; let shader = new Shader(gl, "pointcloud", vs, fs); this.shaders.set(material, shader); } shader = this.shaders.get(material); //if(material.needsUpdate){ { let [vs, fs] = [material.vertexShader, material.fragmentShader]; let numSnapshots = material.snapEnabled ? material.numSnapshots : 0; let numClipBoxes = (material.clipBoxes && material.clipBoxes.length) ? material.clipBoxes.length : 0; let numClipSpheres = (params.clipSpheres && params.clipSpheres.length) ? params.clipSpheres.length : 0; let numClipPolygons = (material.clipPolygons && material.clipPolygons.length) ? material.clipPolygons.length : 0; let defines = [ `#define num_shadowmaps ${shadowMaps.length}`, `#define num_snapshots ${numSnapshots}`, `#define num_clipboxes ${numClipBoxes}`, `#define num_clipspheres ${numClipSpheres}`, `#define num_clippolygons ${numClipPolygons}`, ]; if(octree.pcoGeometry.root.isLoaded()){ let attributes = octree.pcoGeometry.root.geometry.attributes; if(attributes["gps-time"]){ defines.push("#define clip_gps_enabled"); } if(attributes["return number"]){ defines.push("#define clip_return_number_enabled"); } if(attributes["number of returns"]){ defines.push("#define clip_number_of_returns_enabled"); } if(attributes["source id"] || attributes["point source id"]){ defines.push("#define clip_point_source_id_enabled"); } } let definesString = defines.join("\n"); let vsVersionIndex = vs.indexOf("#version "); let fsVersionIndex = fs.indexOf("#version "); if(vsVersionIndex >= 0){ vs = vs.replace(/(#version .*)/, `$1\n${definesString}`); }else { vs = `${definesString}\n${vs}`; } if(fsVersionIndex >= 0){ fs = fs.replace(/(#version .*)/, `$1\n${definesString}`); }else { fs = `${definesString}\n${fs}`; } shader.update(vs, fs); material.needsUpdate = false; } for (let uniformName of Object.keys(material.uniforms)) { let uniform = material.uniforms[uniformName]; if (uniform.type == "t") { let texture = uniform.value; if (!texture) { continue; } if (!this.textures.has(texture)) { let webglTexture = new WebGLTexture(gl, texture); this.textures.set(texture, webglTexture); } let webGLTexture = this.textures.get(texture); webGLTexture.update(); } } } gl.useProgram(shader.program); let transparent = false; if(params.transparent !== undefined){ transparent = params.transparent && material.opacity < 1; }else { transparent = material.opacity < 1; } if (transparent){ gl.enable(gl.BLEND); gl.blendFunc(gl.SRC_ALPHA, gl.ONE); gl.depthMask(false); gl.disable(gl.DEPTH_TEST); } else { gl.disable(gl.BLEND); gl.depthMask(true); gl.enable(gl.DEPTH_TEST); } if(params.blendFunc !== undefined){ gl.enable(gl.BLEND); gl.blendFunc(...params.blendFunc); } if(params.depthTest !== undefined){ if(params.depthTest === true){ gl.enable(gl.DEPTH_TEST); }else { gl.disable(gl.DEPTH_TEST); } } if(params.depthWrite !== undefined){ if(params.depthWrite === true){ gl.depthMask(true); }else { gl.depthMask(false); } } { // UPDATE UNIFORMS shader.setUniformMatrix4("projectionMatrix", proj); shader.setUniformMatrix4("viewMatrix", view); shader.setUniformMatrix4("uViewInv", viewInv); shader.setUniformMatrix4("uProjInv", projInv); let screenWidth = target ? target.width : material.screenWidth; let screenHeight = target ? target.height : material.screenHeight; shader.setUniform1f("uScreenWidth", screenWidth); shader.setUniform1f("uScreenHeight", screenHeight); shader.setUniform1f("fov", Math.PI * camera.fov / 180); shader.setUniform1f("near", camera.near); shader.setUniform1f("far", camera.far); if(camera instanceof OrthographicCamera){ shader.setUniform("uUseOrthographicCamera", true); shader.setUniform("uOrthoWidth", camera.right - camera.left); shader.setUniform("uOrthoHeight", camera.top - camera.bottom); }else { shader.setUniform("uUseOrthographicCamera", false); } if(material.clipBoxes.length + material.clipPolygons.length === 0){ shader.setUniform1i("clipTask", ClipTask.NONE); }else { shader.setUniform1i("clipTask", material.clipTask); } shader.setUniform1i("clipMethod", material.clipMethod); if (material.clipBoxes && material.clipBoxes.length > 0) { //let flattenedMatrices = [].concat(...material.clipBoxes.map(c => c.inverse.elements)); //const lClipBoxes = shader.uniformLocations["clipBoxes[0]"]; //gl.uniformMatrix4fv(lClipBoxes, false, flattenedMatrices); const lClipBoxes = shader.uniformLocations["clipBoxes[0]"]; gl.uniformMatrix4fv(lClipBoxes, false, material.uniforms.clipBoxes.value); } // TODO CLIPSPHERES if(params.clipSpheres && params.clipSpheres.length > 0){ let clipSpheres = params.clipSpheres; let matrices = []; for(let clipSphere of clipSpheres){ //let mScale = new THREE.Matrix4().makeScale(...clipSphere.scale.toArray()); //let mTranslate = new THREE.Matrix4().makeTranslation(...clipSphere.position.toArray()); //let clipToWorld = new THREE.Matrix4().multiplyMatrices(mTranslate, mScale); let clipToWorld = clipSphere.matrixWorld; let viewToWorld = camera.matrixWorld; let worldToClip = clipToWorld.clone().invert(); let viewToClip = new Matrix4().multiplyMatrices(worldToClip, viewToWorld); matrices.push(viewToClip); } let flattenedMatrices = [].concat(...matrices.map(matrix => matrix.elements)); const lClipSpheres = shader.uniformLocations["uClipSpheres[0]"]; gl.uniformMatrix4fv(lClipSpheres, false, flattenedMatrices); //const lClipSpheres = shader.uniformLocations["uClipSpheres[0]"]; //gl.uniformMatrix4fv(lClipSpheres, false, material.uniforms.clipSpheres.value); } shader.setUniform1f("size", material.size); shader.setUniform1f("maxSize", material.uniforms.maxSize.value); shader.setUniform1f("minSize", material.uniforms.minSize.value); // uniform float uPCIndex shader.setUniform1f("uOctreeSpacing", material.spacing); shader.setUniform("uOctreeSize", material.uniforms.octreeSize.value); //uniform vec3 uColor; shader.setUniform3f("uColor", material.color.toArray()); //uniform float opacity; shader.setUniform1f("uOpacity", material.opacity); shader.setUniform2f("elevationRange", material.elevationRange); shader.setUniform2f("intensityRange", material.intensityRange); shader.setUniform3f("uIntensity_gbc", [ material.intensityGamma, material.intensityBrightness, material.intensityContrast ]); shader.setUniform3f("uRGB_gbc", [ material.rgbGamma, material.rgbBrightness, material.rgbContrast ]); shader.setUniform1f("uTransition", material.transition); shader.setUniform1f("wRGB", material.weightRGB); shader.setUniform1f("wIntensity", material.weightIntensity); shader.setUniform1f("wElevation", material.weightElevation); shader.setUniform1f("wClassification", material.weightClassification); shader.setUniform1f("wReturnNumber", material.weightReturnNumber); shader.setUniform1f("wSourceID", material.weightSourceID); shader.setUniform("backfaceCulling", material.uniforms.backfaceCulling.value); let vnWebGLTexture = this.textures.get(material.visibleNodesTexture); if(vnWebGLTexture){ shader.setUniform1i("visibleNodesTexture", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(vnWebGLTexture.target, vnWebGLTexture.id); currentTextureBindingPoint++; } let gradientTexture = this.textures.get(material.gradientTexture); shader.setUniform1i("gradient", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(gradientTexture.target, gradientTexture.id); const repeat = material.elevationGradientRepeat; if(repeat === ElevationGradientRepeat.REPEAT){ gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_T, gl.REPEAT); }else if(repeat === ElevationGradientRepeat.MIRRORED_REPEAT){ gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_S, gl.MIRRORED_REPEAT); gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_T, gl.MIRRORED_REPEAT); }else { gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } currentTextureBindingPoint++; let classificationTexture = this.textures.get(material.classificationTexture); shader.setUniform1i("classificationLUT", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(classificationTexture.target, classificationTexture.id); currentTextureBindingPoint++; let matcapTexture = this.textures.get(material.matcapTexture); shader.setUniform1i("matcapTextureUniform", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(matcapTexture.target, matcapTexture.id); currentTextureBindingPoint++; if (material.snapEnabled === true) { { const lSnapshot = shader.uniformLocations["uSnapshot[0]"]; const lSnapshotDepth = shader.uniformLocations["uSnapshotDepth[0]"]; let bindingStart = currentTextureBindingPoint; let lSnapshotBindingPoints = new Array(5).fill(bindingStart).map((a, i) => (a + i)); let lSnapshotDepthBindingPoints = new Array(5) .fill(1 + Math.max(...lSnapshotBindingPoints)) .map((a, i) => (a + i)); currentTextureBindingPoint = 1 + Math.max(...lSnapshotDepthBindingPoints); gl.uniform1iv(lSnapshot, lSnapshotBindingPoints); gl.uniform1iv(lSnapshotDepth, lSnapshotDepthBindingPoints); for (let i = 0; i < 5; i++) { let texture = material.uniforms[`uSnapshot`].value[i]; let textureDepth = material.uniforms[`uSnapshotDepth`].value[i]; if (!texture) { break; } let snapTexture = this.threeRenderer.properties.get(texture).__webglTexture; let snapTextureDepth = this.threeRenderer.properties.get(textureDepth).__webglTexture; let bindingPoint = lSnapshotBindingPoints[i]; let depthBindingPoint = lSnapshotDepthBindingPoints[i]; gl.activeTexture(gl[`TEXTURE${bindingPoint}`]); gl.bindTexture(gl.TEXTURE_2D, snapTexture); gl.activeTexture(gl[`TEXTURE${depthBindingPoint}`]); gl.bindTexture(gl.TEXTURE_2D, snapTextureDepth); } } { let flattenedMatrices = [].concat(...material.uniforms.uSnapView.value.map(c => c.elements)); const lSnapView = shader.uniformLocations["uSnapView[0]"]; gl.uniformMatrix4fv(lSnapView, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...material.uniforms.uSnapProj.value.map(c => c.elements)); const lSnapProj = shader.uniformLocations["uSnapProj[0]"]; gl.uniformMatrix4fv(lSnapProj, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...material.uniforms.uSnapProjInv.value.map(c => c.elements)); const lSnapProjInv = shader.uniformLocations["uSnapProjInv[0]"]; gl.uniformMatrix4fv(lSnapProjInv, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...material.uniforms.uSnapViewInv.value.map(c => c.elements)); const lSnapViewInv = shader.uniformLocations["uSnapViewInv[0]"]; gl.uniformMatrix4fv(lSnapViewInv, false, flattenedMatrices); } } } this.renderNodes(octree, nodes, visibilityTextureData, camera, target, shader, params); gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, null); gl.activeTexture(gl.TEXTURE0); } render(scene, camera, target = null, params = {}) { const gl = this.gl; // PREPARE if (target != null) { this.threeRenderer.setRenderTarget(target); } //camera.updateProjectionMatrix(); // camera.matrixWorldInverse.invert(camera.matrixWorld); const traversalResult = this.traverse(scene); // RENDER for (const octree of traversalResult.octrees) { let nodes = octree.visibleNodes; this.renderOctree(octree, nodes, camera, target, params); } // CLEANUP gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, null); gl.bindBuffer(gl.ARRAY_BUFFER, null); gl.bindVertexArray(null); this.threeRenderer.resetState(); } }; function copyMaterial(source, target){ for(let name of Object.keys(target.uniforms)){ target.uniforms[name].value = source.uniforms[name].value; } target.gradientTexture = source.gradientTexture; target.visibleNodesTexture = source.visibleNodesTexture; target.classificationTexture = source.classificationTexture; target.matcapTexture = source.matcapTexture; target.activeAttributeName = source.activeAttributeName; target.ranges = source.ranges; //target.updateShaderSource(); } class Batch{ constructor(geometry, material){ this.geometry = geometry; this.material = material; this.sceneNode = new Points(geometry, material); this.geometryNode = { estimatedSpacing: 1.0, geometry: geometry, }; } getLevel(){ return 0; } } class ProfileFakeOctree extends PointCloudTree{ constructor(octree){ super(); this.trueOctree = octree; this.pcoGeometry = octree.pcoGeometry; this.points = []; this.visibleNodes = []; //this.material = this.trueOctree.material; this.material = new PointCloudMaterial$1(); //this.material.copy(this.trueOctree.material); copyMaterial(this.trueOctree.material, this.material); this.material.pointSizeType = PointSizeType.FIXED; this.batchSize = 100 * 1000; this.currentBatch = null; } getAttribute(name){ return this.trueOctree.getAttribute(name); } dispose(){ for(let node of this.visibleNodes){ node.geometry.dispose(); } this.visibleNodes = []; this.currentBatch = null; this.points = []; } addPoints(data){ // since each call to addPoints can deliver very very few points, // we're going to batch them into larger buffers for efficiency. if(this.currentBatch === null){ this.currentBatch = this.createNewBatch(data); } this.points.push(data); let updateRange = { start: this.currentBatch.geometry.drawRange.count, count: 0 }; let projectedBox = new Box3(); let truePos = new Vector3(); for(let i = 0; i < data.numPoints; i++){ if(updateRange.start + updateRange.count >= this.batchSize){ // current batch full, start new batch for(let key of Object.keys(this.currentBatch.geometry.attributes)){ let attribute = this.currentBatch.geometry.attributes[key]; attribute.updateRange.offset = updateRange.start; attribute.updateRange.count = updateRange.count; attribute.needsUpdate = true; } this.currentBatch.geometry.computeBoundingBox(); this.currentBatch.geometry.computeBoundingSphere(); this.currentBatch = this.createNewBatch(data); updateRange = { start: 0, count: 0 }; } truePos.set( data.data.position[3 * i + 0] + this.trueOctree.position.x, data.data.position[3 * i + 1] + this.trueOctree.position.y, data.data.position[3 * i + 2] + this.trueOctree.position.z, ); let x = data.data.mileage[i]; let y = 0; let z = truePos.z; projectedBox.expandByPoint(new Vector3(x, y, z)); let index = updateRange.start + updateRange.count; let geometry = this.currentBatch.geometry; for(let attributeName of Object.keys(data.data)){ let source = data.data[attributeName]; let target = geometry.attributes[attributeName]; let numElements = target.itemSize; for(let item = 0; item < numElements; item++){ target.array[numElements * index + item] = source[numElements * i + item]; } } { let position = geometry.attributes.position; position.array[3 * index + 0] = x; position.array[3 * index + 1] = y; position.array[3 * index + 2] = z; } updateRange.count++; this.currentBatch.geometry.drawRange.count++; } for(let key of Object.keys(this.currentBatch.geometry.attributes)){ let attribute = this.currentBatch.geometry.attributes[key]; attribute.updateRange.offset = updateRange.start; attribute.updateRange.count = updateRange.count; attribute.needsUpdate = true; } data.projectedBox = projectedBox; this.projectedBox = this.points.reduce( (a, i) => a.union(i.projectedBox), new Box3()); } createNewBatch(data){ let geometry = new BufferGeometry(); // create new batches with batch_size elements of the same type as the attribute for(let attributeName of Object.keys(data.data)){ let buffer = data.data[attributeName]; let numElements = buffer.length / data.numPoints; // 3 for pos, 4 for col, 1 for scalars let constructor = buffer.constructor; let normalized = false; if(this.trueOctree.root.sceneNode){ if(this.trueOctree.root.sceneNode.geometry.attributes[attributeName]){ normalized = this.trueOctree.root.sceneNode.geometry.attributes[attributeName].normalized; } } let batchBuffer = new constructor(numElements * this.batchSize); let bufferAttribute = new BufferAttribute(batchBuffer, numElements, normalized); bufferAttribute.potree = { range: [0, 1], }; geometry.setAttribute(attributeName, bufferAttribute); } geometry.drawRange.start = 0; geometry.drawRange.count = 0; let batch = new Batch(geometry, this.material); this.visibleNodes.push(batch); return batch; } computeVisibilityTextureData(){ let data = new Uint8Array(this.visibleNodes.length * 4); let offsets = new Map(); for(let i = 0; i < this.visibleNodes.length; i++){ let node = this.visibleNodes[i]; offsets[node] = i; } return { data: data, offsets: offsets, }; } } class ProfileWindow extends EventDispatcher$1 { constructor (viewer) { super(); this.viewer = viewer; this.elRoot = $('#profile_window'); this.renderArea = this.elRoot.find('#profileCanvasContainer'); this.svg = d3.select('svg#profileSVG'); this.mouseIsDown = false; this.projectedBox = new Box3(); this.pointclouds = new Map(); this.numPoints = 0; this.lastAddPointsTimestamp = undefined; this.mouse = new Vector2(0, 0); this.scale = new Vector3(1, 1, 1); this.autoFitEnabled = true; // completely disable/enable this.autoFit = false; // internal let cwIcon = `${exports.resourcePath}/icons/arrow_cw.svg`; $('#potree_profile_rotate_cw').attr('src', cwIcon); let ccwIcon = `${exports.resourcePath}/icons/arrow_ccw.svg`; $('#potree_profile_rotate_ccw').attr('src', ccwIcon); let forwardIcon = `${exports.resourcePath}/icons/arrow_up.svg`; $('#potree_profile_move_forward').attr('src', forwardIcon); let backwardIcon = `${exports.resourcePath}/icons/arrow_down.svg`; $('#potree_profile_move_backward').attr('src', backwardIcon); let csvIcon = `${exports.resourcePath}/icons/file_csv_2d.svg`; $('#potree_download_csv_icon').attr('src', csvIcon); let lasIcon = `${exports.resourcePath}/icons/file_las_3d.svg`; $('#potree_download_las_icon').attr('src', lasIcon); let closeIcon = `${exports.resourcePath}/icons/close.svg`; $('#closeProfileContainer').attr("src", closeIcon); this.initTHREE(); this.initSVG(); this.initListeners(); this.pRenderer = new Renderer(this.renderer); this.elRoot.i18n(); } initListeners () { $(window).resize(() => { if (this.enabled) { this.render(); } }); this.renderArea.mousedown(e => { this.mouseIsDown = true; }); this.renderArea.mouseup(e => { this.mouseIsDown = false; }); let viewerPickSphereSizeHandler = () => { let camera = this.viewer.scene.getActiveCamera(); let domElement = this.viewer.renderer.domElement; let distance = this.viewerPickSphere.position.distanceTo(camera.position); let pr = Utils.projectedRadius(1, camera, distance, domElement.clientWidth, domElement.clientHeight); let scale = (10 / pr); this.viewerPickSphere.scale.set(scale, scale, scale); }; this.renderArea.mousemove(e => { if (this.pointclouds.size === 0) { return; } let rect = this.renderArea[0].getBoundingClientRect(); let x = e.clientX - rect.left; let y = e.clientY - rect.top; let newMouse = new Vector2(x, y); if (this.mouseIsDown) { // DRAG this.autoFit = false; this.lastDrag = new Date().getTime(); let cPos = [this.scaleX.invert(this.mouse.x), this.scaleY.invert(this.mouse.y)]; let ncPos = [this.scaleX.invert(newMouse.x), this.scaleY.invert(newMouse.y)]; this.camera.position.x -= ncPos[0] - cPos[0]; this.camera.position.z -= ncPos[1] - cPos[1]; this.render(); } else if (this.pointclouds.size > 0) { // FIND HOVERED POINT let radius = Math.abs(this.scaleX.invert(0) - this.scaleX.invert(40)); let mileage = this.scaleX.invert(newMouse.x); let elevation = this.scaleY.invert(newMouse.y); let closest = this.selectPoint(mileage, elevation, radius); if (closest) { let point = closest.point; let position = new Float64Array([ point.position[0] + closest.pointcloud.position.x, point.position[1] + closest.pointcloud.position.y, point.position[2] + closest.pointcloud.position.z ]); this.elRoot.find('#profileSelectionProperties').fadeIn(200); this.pickSphere.visible = true; this.pickSphere.scale.set(0.5 * radius, 0.5 * radius, 0.5 * radius); this.pickSphere.position.set(point.mileage, 0, position[2]); this.viewerPickSphere.position.set(...position); if(!this.viewer.scene.scene.children.includes(this.viewerPickSphere)){ this.viewer.scene.scene.add(this.viewerPickSphere); if(!this.viewer.hasEventListener("update", viewerPickSphereSizeHandler)){ this.viewer.addEventListener("update", viewerPickSphereSizeHandler); } } let info = this.elRoot.find('#profileSelectionProperties'); let html = ''; for (let attributeName of Object.keys(point)) { let value = point[attributeName]; let attribute = closest.pointcloud.getAttribute(attributeName); let transform = value => value; if(attribute && attribute.type.size > 4){ let range = attribute.initialRange; let scale = 1 / (range[1] - range[0]); let offset = range[0]; transform = value => value / scale + offset; } if (attributeName === 'position') { let values = [...position].map(v => Utils.addCommas(v.toFixed(3))); html += ` `; } else if (attributeName === 'rgba') { html += ` `; } else if (attributeName === 'normal') { continue; } else if (attributeName === 'mileage') { html += ` `; } else { html += ` `; } } html += '
x ${values[0]}
y ${values[1]}
z ${values[2]}
${attributeName} ${value.join(', ')}
${attributeName} ${value.toFixed(3)}
${attributeName} ${transform(value)}
'; info.html(html); this.selectedPoint = point; } else { // this.pickSphere.visible = false; // this.selectedPoint = null; this.viewer.scene.scene.add(this.viewerPickSphere); let index = this.viewer.scene.scene.children.indexOf(this.viewerPickSphere); if(index >= 0){ this.viewer.scene.scene.children.splice(index, 1); } this.viewer.removeEventListener("update", viewerPickSphereSizeHandler); } this.render(); } this.mouse.copy(newMouse); }); let onWheel = e => { this.autoFit = false; let delta = 0; if (e.wheelDelta !== undefined) { // WebKit / Opera / Explorer 9 delta = e.wheelDelta; } else if (e.detail !== undefined) { // Firefox delta = -e.detail; } let ndelta = Math.sign(delta); let cPos = [this.scaleX.invert(this.mouse.x), this.scaleY.invert(this.mouse.y)]; if (ndelta > 0) { // + 10% this.scale.multiplyScalar(1.1); } else { // - 10% this.scale.multiplyScalar(100 / 110); } this.updateScales(); let ncPos = [this.scaleX.invert(this.mouse.x), this.scaleY.invert(this.mouse.y)]; this.camera.position.x -= ncPos[0] - cPos[0]; this.camera.position.z -= ncPos[1] - cPos[1]; this.render(); this.updateScales(); }; $(this.renderArea)[0].addEventListener('mousewheel', onWheel, false); $(this.renderArea)[0].addEventListener('DOMMouseScroll', onWheel, false); // Firefox $('#closeProfileContainer').click(() => { this.hide(); }); let getProfilePoints = () => { let points = new Points$1(); for(let [pointcloud, entry] of this.pointclouds){ for(let pointSet of entry.points){ let originPos = pointSet.data.position; let trueElevationPosition = new Float32Array(originPos); for(let i = 0; i < pointSet.numPoints; i++){ trueElevationPosition[3 * i + 2] += pointcloud.position.z; } pointSet.data.position = trueElevationPosition; points.add(pointSet); pointSet.data.position = originPos; } } return points; }; $('#potree_download_csv_icon').click(() => { let points = getProfilePoints(); let string = CSVExporter.toString(points); let blob = new Blob([string], {type: "text/string"}); $('#potree_download_profile_ortho_link').attr('href', URL.createObjectURL(blob)); }); $('#potree_download_las_icon').click(() => { let points = getProfilePoints(); let buffer = LASExporter.toLAS(points); let blob = new Blob([buffer], {type: "application/octet-binary"}); $('#potree_download_profile_link').attr('href', URL.createObjectURL(blob)); }); } selectPoint (mileage, elevation, radius) { let closest = { distance: Infinity, pointcloud: null, points: null, index: null }; let pointBox = new Box2( new Vector2(mileage - radius, elevation - radius), new Vector2(mileage + radius, elevation + radius)); let numTested = 0; let numSkipped = 0; let numTestedPoints = 0; let numSkippedPoints = 0; for (let [pointcloud, entry] of this.pointclouds) { for(let points of entry.points){ let collisionBox = new Box2( new Vector2(points.projectedBox.min.x, points.projectedBox.min.z), new Vector2(points.projectedBox.max.x, points.projectedBox.max.z) ); let intersects = collisionBox.intersectsBox(pointBox); if(!intersects){ numSkipped++; numSkippedPoints += points.numPoints; continue; } numTested++; numTestedPoints += points.numPoints; for (let i = 0; i < points.numPoints; i++) { let m = points.data.mileage[i] - mileage; let e = points.data.position[3 * i + 2] - elevation + pointcloud.position.z; let r = Math.sqrt(m * m + e * e); const withinDistance = r < radius && r < closest.distance; let unfilteredClass = true; if(points.data.classification){ const classification = pointcloud.material.classification; const pointClassID = points.data.classification[i]; const pointClassValue = classification[pointClassID]; if(pointClassValue && (!pointClassValue.visible || pointClassValue.color.w === 0)){ unfilteredClass = false; } } if (withinDistance && unfilteredClass) { closest = { distance: r, pointcloud: pointcloud, points: points, index: i }; } } } } //console.log(`nodes: ${numTested}, ${numSkipped} || points: ${numTestedPoints}, ${numSkippedPoints}`); if (closest.distance < Infinity) { let points = closest.points; let point = {}; let attributes = Object.keys(points.data); for (let attribute of attributes) { let attributeData = points.data[attribute]; let itemSize = attributeData.length / points.numPoints; let value = attributeData.subarray(itemSize * closest.index, itemSize * closest.index + itemSize); if (value.length === 1) { point[attribute] = value[0]; } else { point[attribute] = value; } } closest.point = point; return closest; } else { return null; } } initTHREE () { this.renderer = new WebGLRenderer({alpha: true, premultipliedAlpha: false}); this.renderer.setClearColor(0x000000, 0); this.renderer.setSize(10, 10); this.renderer.autoClear = false; this.renderArea.append($(this.renderer.domElement)); this.renderer.domElement.tabIndex = '2222'; $(this.renderer.domElement).css('width', '100%'); $(this.renderer.domElement).css('height', '100%'); { let gl = this.renderer.getContext(); if(gl.createVertexArray == null){ let extVAO = gl.getExtension('OES_vertex_array_object'); if(!extVAO){ throw new Error("OES_vertex_array_object extension not supported"); } gl.createVertexArray = extVAO.createVertexArrayOES.bind(extVAO); gl.bindVertexArray = extVAO.bindVertexArrayOES.bind(extVAO); } } this.camera = new OrthographicCamera(-1000, 1000, 1000, -1000, -1000, 1000); this.camera.up.set(0, 0, 1); this.camera.rotation.order = "ZXY"; this.camera.rotation.x = Math.PI / 2.0; this.scene = new Scene(); this.profileScene = new Scene(); let sg = new SphereGeometry(1, 16, 16); let sm = new MeshNormalMaterial(); this.pickSphere = new Mesh(sg, sm); this.scene.add(this.pickSphere); this.viewerPickSphere = new Mesh(sg, sm); } initSVG () { let width = this.renderArea[0].clientWidth; let height = this.renderArea[0].clientHeight; let marginLeft = this.renderArea[0].offsetLeft; this.svg.selectAll('*').remove(); this.scaleX = d3.scale.linear() .domain([this.camera.left + this.camera.position.x, this.camera.right + this.camera.position.x]) .range([0, width]); this.scaleY = d3.scale.linear() .domain([this.camera.bottom + this.camera.position.z, this.camera.top + this.camera.position.z]) .range([height, 0]); this.xAxis = d3.svg.axis() .scale(this.scaleX) .orient('bottom') .innerTickSize(-height) .outerTickSize(1) .tickPadding(10) .ticks(width / 50); this.yAxis = d3.svg.axis() .scale(this.scaleY) .orient('left') .innerTickSize(-width) .outerTickSize(1) .tickPadding(10) .ticks(height / 20); this.elXAxis = this.svg.append('g') .attr('class', 'x axis') .attr('transform', `translate(${marginLeft}, ${height})`) .call(this.xAxis); this.elYAxis = this.svg.append('g') .attr('class', 'y axis') .attr('transform', `translate(${marginLeft}, 0)`) .call(this.yAxis); } addPoints (pointcloud, points) { if(points.numPoints === 0){ return; } let entry = this.pointclouds.get(pointcloud); if(!entry){ entry = new ProfileFakeOctree(pointcloud); this.pointclouds.set(pointcloud, entry); this.profileScene.add(entry); let materialChanged = () => { this.render(); }; materialChanged(); pointcloud.material.addEventListener('material_property_changed', materialChanged); this.addEventListener("on_reset_once", () => { pointcloud.material.removeEventListener('material_property_changed', materialChanged); }); } entry.addPoints(points); this.projectedBox.union(entry.projectedBox); if (this.autoFit && this.autoFitEnabled) { let width = this.renderArea[0].clientWidth; let height = this.renderArea[0].clientHeight; let size = this.projectedBox.getSize(new Vector3()); let sx = width / size.x; let sy = height / size.z; let scale = Math.min(sx, sy); let center = this.projectedBox.getCenter(new Vector3()); this.scale.set(scale, scale, 1); this.camera.position.copy(center); //console.log("camera: ", this.camera.position.toArray().join(", ")); } //console.log(entry); this.render(); let numPoints = 0; for (let [key, value] of this.pointclouds.entries()) { numPoints += value.points.reduce( (a, i) => a + i.numPoints, 0); } $(`#profile_num_points`).html(Utils.addCommas(numPoints)); } reset () { this.lastReset = new Date().getTime(); this.dispatchEvent({type: "on_reset_once"}); this.removeEventListeners("on_reset_once"); this.autoFit = true; this.projectedBox = new Box3(); for(let [key, entry] of this.pointclouds){ entry.dispose(); } this.pointclouds.clear(); this.mouseIsDown = false; this.mouse.set(0, 0); if(this.autoFitEnabled){ this.scale.set(1, 1, 1); } this.pickSphere.visible = false; this.elRoot.find('#profileSelectionProperties').hide(); this.render(); } show () { this.elRoot.fadeIn(); this.enabled = true; } hide () { this.elRoot.fadeOut(); this.enabled = false; } updateScales () { let width = this.renderArea[0].clientWidth; let height = this.renderArea[0].clientHeight; let left = (-width / 2) / this.scale.x; let right = (+width / 2) / this.scale.x; let top = (+height / 2) / this.scale.y; let bottom = (-height / 2) / this.scale.y; this.camera.left = left; this.camera.right = right; this.camera.top = top; this.camera.bottom = bottom; this.camera.updateProjectionMatrix(); this.scaleX.domain([this.camera.left + this.camera.position.x, this.camera.right + this.camera.position.x]) .range([0, width]); this.scaleY.domain([this.camera.bottom + this.camera.position.z, this.camera.top + this.camera.position.z]) .range([height, 0]); let marginLeft = this.renderArea[0].offsetLeft; this.xAxis.scale(this.scaleX) .orient('bottom') .innerTickSize(-height) .outerTickSize(1) .tickPadding(10) .ticks(width / 50); this.yAxis.scale(this.scaleY) .orient('left') .innerTickSize(-width) .outerTickSize(1) .tickPadding(10) .ticks(height / 20); this.elXAxis .attr('transform', `translate(${marginLeft}, ${height})`) .call(this.xAxis); this.elYAxis .attr('transform', `translate(${marginLeft}, 0)`) .call(this.yAxis); } requestScaleUpdate(){ let threshold = 100; let allowUpdate = ((this.lastReset === undefined) || (this.lastScaleUpdate === undefined)) || ((new Date().getTime() - this.lastReset) > threshold && (new Date().getTime() - this.lastScaleUpdate) > threshold); if(allowUpdate){ this.updateScales(); this.lastScaleUpdate = new Date().getTime(); this.scaleUpdatePending = false; }else if(!this.scaleUpdatePending) { setTimeout(this.requestScaleUpdate.bind(this), 100); this.scaleUpdatePending = true; } } render () { let width = this.renderArea[0].clientWidth; let height = this.renderArea[0].clientHeight; let {renderer, pRenderer, camera, profileScene, scene} = this; let {scaleX, pickSphere} = this; renderer.setSize(width, height); renderer.setClearColor(0x000000, 0); renderer.clear(true, true, false); for(let pointcloud of this.pointclouds.keys()){ let source = pointcloud.material; let target = this.pointclouds.get(pointcloud).material; copyMaterial(source, target); target.size = 2; } pRenderer.render(profileScene, camera, null); let radius = Math.abs(scaleX.invert(0) - scaleX.invert(5)); if (radius === 0) { pickSphere.visible = false; } else { pickSphere.scale.set(radius, radius, radius); pickSphere.visible = true; } renderer.render(scene, camera); this.requestScaleUpdate(); } }; class ProfileWindowController { constructor (viewer) { this.viewer = viewer; this.profileWindow = viewer.profileWindow; this.profile = null; this.numPoints = 0; this.threshold = 60 * 1000; this.rotateAmount = 10; this.scheduledRecomputeTime = null; this.enabled = true; this.requests = []; this._recompute = () => { this.recompute(); }; this.viewer.addEventListener("scene_changed", e => { e.oldScene.removeEventListener("pointcloud_added", this._recompute); e.scene.addEventListener("pointcloud_added", this._recompute); }); this.viewer.scene.addEventListener("pointcloud_added", this._recompute); $("#potree_profile_rotate_amount").val(parseInt(this.rotateAmount)); $("#potree_profile_rotate_amount").on("input", (e) => { const str = $("#potree_profile_rotate_amount").val(); if(!isNaN(str)){ const value = parseFloat(str); this.rotateAmount = value; $("#potree_profile_rotate_amount").css("background-color", ""); }else { $("#potree_profile_rotate_amount").css("background-color", "#ff9999"); } }); const rotate = (radians) => { const profile = this.profile; const points = profile.points; const start = points[0]; const end = points[points.length - 1]; const center = start.clone().add(end).multiplyScalar(0.5); const mMoveOrigin = new Matrix4().makeTranslation(-center.x, -center.y, -center.z); const mRotate = new Matrix4().makeRotationZ(radians); const mMoveBack = new Matrix4().makeTranslation(center.x, center.y, center.z); //const transform = mMoveOrigin.multiply(mRotate).multiply(mMoveBack); const transform = mMoveBack.multiply(mRotate).multiply(mMoveOrigin); const rotatedPoints = points.map( point => point.clone().applyMatrix4(transform) ); this.profileWindow.autoFitEnabled = false; for(let i = 0; i < points.length; i++){ profile.setPosition(i, rotatedPoints[i]); } }; $("#potree_profile_rotate_cw").click( () => { const radians = MathUtils.degToRad(this.rotateAmount); rotate(-radians); }); $("#potree_profile_rotate_ccw").click( () => { const radians = MathUtils.degToRad(this.rotateAmount); rotate(radians); }); $("#potree_profile_move_forward").click( () => { const profile = this.profile; const points = profile.points; const start = points[0]; const end = points[points.length - 1]; const dir = end.clone().sub(start).normalize(); const up = new Vector3(0, 0, 1); const forward = up.cross(dir); const move = forward.clone().multiplyScalar(profile.width / 2); this.profileWindow.autoFitEnabled = false; for(let i = 0; i < points.length; i++){ profile.setPosition(i, points[i].clone().add(move)); } }); $("#potree_profile_move_backward").click( () => { const profile = this.profile; const points = profile.points; const start = points[0]; const end = points[points.length - 1]; const dir = end.clone().sub(start).normalize(); const up = new Vector3(0, 0, 1); const forward = up.cross(dir); const move = forward.clone().multiplyScalar(-profile.width / 2); this.profileWindow.autoFitEnabled = false; for(let i = 0; i < points.length; i++){ profile.setPosition(i, points[i].clone().add(move)); } }); } setProfile (profile) { if (this.profile !== null && this.profile !== profile) { this.profile.removeEventListener('marker_moved', this._recompute); this.profile.removeEventListener('marker_added', this._recompute); this.profile.removeEventListener('marker_removed', this._recompute); this.profile.removeEventListener('width_changed', this._recompute); } this.profile = profile; { this.profile.addEventListener('marker_moved', this._recompute); this.profile.addEventListener('marker_added', this._recompute); this.profile.addEventListener('marker_removed', this._recompute); this.profile.addEventListener('width_changed', this._recompute); } this.recompute(); } reset () { this.profileWindow.reset(); this.numPoints = 0; if (this.profile) { for (let request of this.requests) { request.cancel(); } } } progressHandler (pointcloud, progress) { for (let segment of progress.segments) { this.profileWindow.addPoints(pointcloud, segment.points); this.numPoints += segment.points.numPoints; } } cancel () { for (let request of this.requests) { request.cancel(); // request.finishLevelThenCancel(); } this.requests = []; }; finishLevelThenCancel(){ for (let request of this.requests) { request.finishLevelThenCancel(); } this.requests = []; } recompute () { if (!this.profile) { return; } if (this.scheduledRecomputeTime !== null && this.scheduledRecomputeTime > new Date().getTime()) { return; } else { this.scheduledRecomputeTime = new Date().getTime() + 100; } this.scheduledRecomputeTime = null; this.reset(); for (let pointcloud of this.viewer.scene.pointclouds.filter(p => p.visible)) { let request = pointcloud.getPointsInProfile(this.profile, null, { 'onProgress': (event) => { if (!this.enabled) { return; } this.progressHandler(pointcloud, event.points); if (this.numPoints > this.threshold) { this.finishLevelThenCancel(); } }, 'onFinish': (event) => { if (!this.enabled) { } }, 'onCancel': () => { if (!this.enabled) { } } }); this.requests.push(request); } } }; //import { EventDispatcher } from "../EventDispatcher.js"; class VolumeTool extends EventDispatcher{ constructor (viewer) { super(); this.viewer = viewer; this.renderer = viewer.renderer; this.addEventListener('start_inserting_volume', e => { this.viewer.dispatchEvent({ type: 'cancel_insertions' }); }); this.scene = new Scene(); this.scene.name = 'scene_volume'; this.viewer.inputHandler.registerInteractiveScene(this.scene); this.onRemove = e => { this.scene.remove(e.volume); }; this.onAdd = e => { this.scene.add(e.volume); }; for(let volume of viewer.scene.volumes){ this.onAdd({volume: volume}); } this.viewer.inputHandler.addEventListener('delete', e => { let volumes = e.selection.filter(e => (e instanceof Volume)); volumes.forEach(e => this.viewer.scene.removeVolume(e)); }); viewer.addEventListener("update", this.update.bind(this)); viewer.addEventListener("render.pass.scene", e => this.render(e)); viewer.addEventListener("scene_changed", this.onSceneChange.bind(this)); viewer.scene.addEventListener('volume_added', this.onAdd); viewer.scene.addEventListener('volume_removed', this.onRemove); } onSceneChange(e){ if(e.oldScene){ e.oldScene.removeEventListeners('volume_added', this.onAdd); e.oldScene.removeEventListeners('volume_removed', this.onRemove); } e.scene.addEventListener('volume_added', this.onAdd); e.scene.addEventListener('volume_removed', this.onRemove); } startInsertion (args = {}) { let volume; if(args.type){ volume = new args.type(); }else { volume = new BoxVolume(); } volume.clip = args.clip || false; volume.name = args.name || 'Volume'; this.dispatchEvent({ type: 'start_inserting_volume', volume: volume }); this.viewer.scene.addVolume(volume); this.scene.add(volume); let cancel = { callback: null }; let drag = e => { let camera = this.viewer.scene.getActiveCamera(); let I = Utils.getMousePointCloudIntersection( e.drag.end, this.viewer.scene.getActiveCamera(), this.viewer, this.viewer.scene.pointclouds, {pickClipped: false}); if (I) { volume.position.copy(I.location); let wp = volume.getWorldPosition(new Vector3()).applyMatrix4(camera.matrixWorldInverse); // let pp = new THREE.Vector4(wp.x, wp.y, wp.z).applyMatrix4(camera.projectionMatrix); let w = Math.abs((wp.z / 5)); volume.scale.set(w, w, w); } }; let drop = e => { volume.removeEventListener('drag', drag); volume.removeEventListener('drop', drop); cancel.callback(); }; cancel.callback = e => { volume.removeEventListener('drag', drag); volume.removeEventListener('drop', drop); this.viewer.removeEventListener('cancel_insertions', cancel.callback); }; volume.addEventListener('drag', drag); volume.addEventListener('drop', drop); this.viewer.addEventListener('cancel_insertions', cancel.callback); this.viewer.inputHandler.startDragging(volume); return volume; } update(){ if (!this.viewer.scene) { return; } let camera = this.viewer.scene.getActiveCamera(); let renderAreaSize = this.viewer.renderer.getSize(new Vector2()); let clientWidth = renderAreaSize.width; let clientHeight = renderAreaSize.height; let volumes = this.viewer.scene.volumes; for (let volume of volumes) { let label = volume.label; { let distance = label.position.distanceTo(camera.position); let pr = Utils.projectedRadius(1, camera, distance, clientWidth, clientHeight); let scale = (70 / pr); label.scale.set(scale, scale, scale); } let calculatedVolume = volume.getVolume(); calculatedVolume = calculatedVolume / Math.pow(this.viewer.lengthUnit.unitspermeter, 3) * Math.pow(this.viewer.lengthUnitDisplay.unitspermeter, 3); //convert to cubic meters then to the cubic display unit let text = Utils.addCommas(calculatedVolume.toFixed(3)) + ' ' + this.viewer.lengthUnitDisplay.code + '\u00B3'; label.setText(text); } } render(params){ const renderer = this.viewer.renderer; const oldTarget = renderer.getRenderTarget(); if(params.renderTarget){ renderer.setRenderTarget(params.renderTarget); } renderer.render(this.scene, this.viewer.scene.getActiveCamera()); renderer.setRenderTarget(oldTarget); } } /** * * code adapted from three.js BoxHelper.js * https://github.com/mrdoob/three.js/blob/dev/src/helpers/BoxHelper.js * * @author mrdoob / http://mrdoob.com/ * @author Mugen87 / http://github.com/Mugen87 * @author mschuetz / http://potree.org */ class Box3Helper$1 extends LineSegments { constructor (box, color, depthTest = true) { if (color === undefined) color = 0xffff00; let indices = new Uint16Array([ 0, 1, 1, 2, 2, 3, 3, 0, 4, 5, 5, 6, 6, 7, 7, 4, 0, 4, 1, 5, 2, 6, 3, 7 ]); let positions = new Float32Array([ box.min.x, box.min.y, box.min.z, box.max.x, box.min.y, box.min.z, box.max.x, box.min.y, box.max.z, box.min.x, box.min.y, box.max.z, box.min.x, box.max.y, box.min.z, box.max.x, box.max.y, box.min.z, box.max.x, box.max.y, box.max.z, box.min.x, box.max.y, box.max.z ]); let geometry = new BufferGeometry(); geometry.setIndex(new BufferAttribute(indices, 1)); geometry.setAttribute('position', new BufferAttribute(positions, 3)); let material = new LineBasicMaterial({ color: color , depthTest}); super(geometry, material); } } const KeyCodes = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40, DELETE: 46, A: 'A'.charCodeAt(0), S: 'S'.charCodeAt(0), D: 'D'.charCodeAt(0), W: 'W'.charCodeAt(0), Q: 'Q'.charCodeAt(0), E: 'E'.charCodeAt(0), R: 'R'.charCodeAt(0), F: 'F'.charCodeAt(0) }; class NormalizationMaterial extends RawShaderMaterial{ constructor(parameters = {}){ super(); let uniforms = { uDepthMap: { type: 't', value: null }, uWeightMap: { type: 't', value: null }, }; this.setValues({ uniforms: uniforms, vertexShader: this.getDefines() + Shaders['normalize.vs'], fragmentShader: this.getDefines() + Shaders['normalize.fs'], }); } getDefines() { let defines = ''; return defines; } updateShaderSource() { let vs = this.getDefines() + Shaders['normalize.vs']; let fs = this.getDefines() + Shaders['normalize.fs']; this.setValues({ vertexShader: vs, fragmentShader: fs }); this.needsUpdate = true; } } class NormalizationEDLMaterial extends RawShaderMaterial{ constructor(parameters = {}){ super(); let uniforms = { screenWidth: { type: 'f', value: 0 }, screenHeight: { type: 'f', value: 0 }, edlStrength: { type: 'f', value: 1.0 }, radius: { type: 'f', value: 1.0 }, neighbours: { type: '2fv', value: [] }, uEDLMap: { type: 't', value: null }, uDepthMap: { type: 't', value: null }, uWeightMap: { type: 't', value: null }, }; this.setValues({ uniforms: uniforms, vertexShader: this.getDefines() + Shaders['normalize.vs'], fragmentShader: this.getDefines() + Shaders['normalize_and_edl.fs'], }); this.neighbourCount = 8; } getDefines() { let defines = ''; defines += '#define NEIGHBOUR_COUNT ' + this.neighbourCount + '\n'; return defines; } updateShaderSource() { let vs = this.getDefines() + Shaders['normalize.vs']; let fs = this.getDefines() + Shaders['normalize_and_edl.fs']; this.setValues({ vertexShader: vs, fragmentShader: fs }); this.uniforms.neighbours.value = this.neighbours; this.needsUpdate = true; } get neighbourCount(){ return this._neighbourCount; } set neighbourCount(value){ if (this._neighbourCount !== value) { this._neighbourCount = value; this.neighbours = new Float32Array(this._neighbourCount * 2); for (let c = 0; c < this._neighbourCount; c++) { this.neighbours[2 * c + 0] = Math.cos(2 * c * Math.PI / this._neighbourCount); this.neighbours[2 * c + 1] = Math.sin(2 * c * Math.PI / this._neighbourCount); } this.updateShaderSource(); } } } //在potree.shim中修改。 。official中用不到且有bug class HQSplatRenderer{ constructor(viewer){ this.viewer = viewer; this.depthMaterials = new Map(); this.attributeMaterials = new Map(); this.normalizationMaterial = null; this.rtDepth = null; this.rtAttribute = null; this.gl = viewer.renderer.getContext(); this.initialized = false; } init(){ if (this.initialized) { return; } this.normalizationMaterial = new NormalizationMaterial(); this.normalizationMaterial.depthTest = true; this.normalizationMaterial.depthWrite = true; this.normalizationMaterial.transparent = true; this.normalizationEDLMaterial = new NormalizationEDLMaterial(); this.normalizationEDLMaterial.depthTest = true; this.normalizationEDLMaterial.depthWrite = true; this.normalizationEDLMaterial.transparent = true; this.rtDepth = new WebGLRenderTarget(1024, 1024, { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat, type: FloatType, depthTexture: new DepthTexture(undefined, undefined, UnsignedIntType) }); this.rtAttribute = new WebGLRenderTarget(1024, 1024, { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat, type: FloatType, depthTexture: this.rtDepth.depthTexture, }); this.initialized = true; }; resize(width, height){ this.rtDepth.setSize(width, height); this.rtAttribute.setSize(width, height); } clearTargets(){ const viewer = this.viewer; const {renderer} = viewer; const oldTarget = renderer.getRenderTarget(); renderer.setClearColor(0x000000, 0); renderer.setRenderTarget( this.rtDepth ); renderer.clear( true, true, true ); renderer.setRenderTarget( this.rtAttribute ); renderer.clear( true, true, true ); renderer.setRenderTarget(oldTarget); } clear(){ this.init(); const {renderer, background} = this.viewer; if(background === "skybox"){ renderer.setClearColor(0x000000, 0); } else if (background === 'gradient') { renderer.setClearColor(0x000000, 0); } else if (background === 'black') { renderer.setClearColor(0x000000, 1); } else if (background === 'white') { renderer.setClearColor(0xFFFFFF, 1); } else { renderer.setClearColor(0x000000, 0); } renderer.clear(); this.clearTargets(); } render (params) { this.init(); const viewer = this.viewer; const camera = params.camera ? params.camera : viewer.scene.getActiveCamera(); const {width, height} = this.viewer.renderer.getSize(new Vector2()); viewer.dispatchEvent({type: "render.pass.begin",viewer: viewer}); this.resize(width, height); const visiblePointClouds = viewer.scene.pointclouds.filter(pc => pc.visible); const originalMaterials = new Map(); for(let pointcloud of visiblePointClouds){ originalMaterials.set(pointcloud, pointcloud.material); if(!this.attributeMaterials.has(pointcloud)){ let attributeMaterial = new PointCloudMaterial$1(); this.attributeMaterials.set(pointcloud, attributeMaterial); } if(!this.depthMaterials.has(pointcloud)){ let depthMaterial = new PointCloudMaterial$1(); depthMaterial.setDefine("depth_pass", "#define hq_depth_pass"); depthMaterial.setDefine("use_edl", "#define use_edl"); this.depthMaterials.set(pointcloud, depthMaterial); } } { // DEPTH PASS for (let pointcloud of visiblePointClouds) { let octreeSize = pointcloud.pcoGeometry.boundingBox.getSize(new Vector3()).x; let material = originalMaterials.get(pointcloud); let depthMaterial = this.depthMaterials.get(pointcloud); depthMaterial.size = material.size; depthMaterial.minSize = material.minSize; depthMaterial.maxSize = material.maxSize; depthMaterial.pointSizeType = material.pointSizeType; depthMaterial.visibleNodesTexture = material.visibleNodesTexture; depthMaterial.weighted = false; depthMaterial.screenWidth = width; depthMaterial.shape = PointShape$1.CIRCLE; depthMaterial.screenHeight = height; depthMaterial.uniforms.visibleNodes.value = material.visibleNodesTexture; depthMaterial.uniforms.octreeSize.value = octreeSize; depthMaterial.spacing = pointcloud.pcoGeometry.spacing; // * Math.max(...pointcloud.scale.toArray()); depthMaterial.classification = material.classification; depthMaterial.uniforms.classificationLUT.value.image.data = material.uniforms.classificationLUT.value.image.data; depthMaterial.classificationTexture.needsUpdate = true; depthMaterial.uniforms.uFilterReturnNumberRange.value = material.uniforms.uFilterReturnNumberRange.value; depthMaterial.uniforms.uFilterNumberOfReturnsRange.value = material.uniforms.uFilterNumberOfReturnsRange.value; depthMaterial.uniforms.uFilterGPSTimeClipRange.value = material.uniforms.uFilterGPSTimeClipRange.value; depthMaterial.uniforms.uFilterPointSourceIDClipRange.value = material.uniforms.uFilterPointSourceIDClipRange.value; depthMaterial.clipTask = material.clipTask; depthMaterial.clipMethod = material.clipMethod; depthMaterial.setClipBoxes(material.clipBoxes); depthMaterial.setClipPolygons(material.clipPolygons); pointcloud.material = depthMaterial; } viewer.pRenderer.render(viewer.scene.scenePointCloud, camera, this.rtDepth, { clipSpheres: viewer.scene.volumes.filter(v => (v instanceof SphereVolume$1)), }); } { // ATTRIBUTE PASS for (let pointcloud of visiblePointClouds) { let octreeSize = pointcloud.pcoGeometry.boundingBox.getSize(new Vector3()).x; let material = originalMaterials.get(pointcloud); let attributeMaterial = this.attributeMaterials.get(pointcloud); attributeMaterial.size = material.size; attributeMaterial.minSize = material.minSize; attributeMaterial.maxSize = material.maxSize; attributeMaterial.pointSizeType = material.pointSizeType; attributeMaterial.activeAttributeName = material.activeAttributeName; attributeMaterial.visibleNodesTexture = material.visibleNodesTexture; attributeMaterial.weighted = true; attributeMaterial.screenWidth = width; attributeMaterial.screenHeight = height; attributeMaterial.shape = PointShape$1.CIRCLE; attributeMaterial.uniforms.visibleNodes.value = material.visibleNodesTexture; attributeMaterial.uniforms.octreeSize.value = octreeSize; attributeMaterial.spacing = pointcloud.pcoGeometry.spacing; // * Math.max(...pointcloud.scale.toArray()); attributeMaterial.classification = material.classification; attributeMaterial.uniforms.classificationLUT.value.image.data = material.uniforms.classificationLUT.value.image.data; attributeMaterial.classificationTexture.needsUpdate = true; attributeMaterial.uniforms.uFilterReturnNumberRange.value = material.uniforms.uFilterReturnNumberRange.value; attributeMaterial.uniforms.uFilterNumberOfReturnsRange.value = material.uniforms.uFilterNumberOfReturnsRange.value; attributeMaterial.uniforms.uFilterGPSTimeClipRange.value = material.uniforms.uFilterGPSTimeClipRange.value; attributeMaterial.uniforms.uFilterPointSourceIDClipRange.value = material.uniforms.uFilterPointSourceIDClipRange.value; attributeMaterial.elevationGradientRepeat = material.elevationGradientRepeat; attributeMaterial.elevationRange = material.elevationRange; attributeMaterial.gradient = material.gradient; attributeMaterial.matcap = material.matcap; attributeMaterial.intensityRange = material.intensityRange; attributeMaterial.intensityGamma = material.intensityGamma; attributeMaterial.intensityContrast = material.intensityContrast; attributeMaterial.intensityBrightness = material.intensityBrightness; attributeMaterial.rgbGamma = material.rgbGamma; attributeMaterial.rgbContrast = material.rgbContrast; attributeMaterial.rgbBrightness = material.rgbBrightness; attributeMaterial.weightRGB = material.weightRGB; attributeMaterial.weightIntensity = material.weightIntensity; attributeMaterial.weightElevation = material.weightElevation; attributeMaterial.weightRGB = material.weightRGB; attributeMaterial.weightClassification = material.weightClassification; attributeMaterial.weightReturnNumber = material.weightReturnNumber; attributeMaterial.weightSourceID = material.weightSourceID; attributeMaterial.color = material.color; attributeMaterial.clipTask = material.clipTask; attributeMaterial.clipMethod = material.clipMethod; attributeMaterial.setClipBoxes(material.clipBoxes); attributeMaterial.setClipPolygons(material.clipPolygons); pointcloud.material = attributeMaterial; } let gl = this.gl; viewer.renderer.setRenderTarget(null); viewer.pRenderer.render(viewer.scene.scenePointCloud, camera, this.rtAttribute, { clipSpheres: viewer.scene.volumes.filter(v => (v instanceof SphereVolume$1)), //material: this.attributeMaterial, blendFunc: [gl.SRC_ALPHA, gl.ONE], //depthTest: false, depthWrite: false }); } for(let [pointcloud, material] of originalMaterials){ pointcloud.material = material; } viewer.renderer.setRenderTarget(null); if(viewer.background === "skybox"){ viewer.renderer.setClearColor(0x000000, 0); viewer.renderer.clear(); viewer.skybox.camera.rotation.copy(viewer.scene.cameraP.rotation); viewer.skybox.camera.fov = viewer.scene.cameraP.fov; viewer.skybox.camera.aspect = viewer.scene.cameraP.aspect; viewer.skybox.parent.rotation.x = 0; viewer.skybox.parent.updateMatrixWorld(); viewer.skybox.camera.updateProjectionMatrix(); viewer.renderer.render(viewer.skybox.scene, viewer.skybox.camera); } else if (viewer.background === 'gradient') { viewer.renderer.setClearColor(0x000000, 0); viewer.renderer.clear(); viewer.renderer.render(viewer.scene.sceneBG, viewer.scene.cameraBG); } else if (viewer.background === 'black') { viewer.renderer.setClearColor(0x000000, 1); viewer.renderer.clear(); } else if (viewer.background === 'white') { viewer.renderer.setClearColor(0xFFFFFF, 1); viewer.renderer.clear(); } else { viewer.renderer.setClearColor(0x000000, 0); viewer.renderer.clear(); } { // NORMALIZATION PASS let normalizationMaterial = this.useEDL ? this.normalizationEDLMaterial : this.normalizationMaterial; if(this.useEDL){ normalizationMaterial.uniforms.edlStrength.value = viewer.edlStrength; normalizationMaterial.uniforms.radius.value = viewer.edlRadius; normalizationMaterial.uniforms.screenWidth.value = width; normalizationMaterial.uniforms.screenHeight.value = height; normalizationMaterial.uniforms.uEDLMap.value = this.rtDepth.texture; } normalizationMaterial.uniforms.uWeightMap.value = this.rtAttribute.texture; normalizationMaterial.uniforms.uDepthMap.value = this.rtAttribute.depthTexture; Utils.screenPass.render(viewer.renderer, normalizationMaterial); } viewer.renderer.render(viewer.scene.scene, camera); viewer.dispatchEvent({type: "render.pass.scene", viewer: viewer}); viewer.renderer.clearDepth(); viewer.transformationTool.update(); viewer.dispatchEvent({type: "render.pass.perspective_overlay",viewer: viewer}); viewer.renderer.render(viewer.controls.sceneControls, camera); viewer.renderer.render(viewer.clippingTool.sceneVolume, camera); viewer.renderer.render(viewer.transformationTool.scene, camera); viewer.renderer.setViewport(width - viewer.navigationCube.width, height - viewer.navigationCube.width, viewer.navigationCube.width, viewer.navigationCube.width); viewer.renderer.render(viewer.navigationCube, viewer.navigationCube.camera); viewer.renderer.setViewport(0, 0, width, height); viewer.dispatchEvent({type: "render.pass.end",viewer: viewer}); } } class LRUItem{ constructor(node){ this.previous = null; this.next = null; this.node = node; } } /** * * @class A doubly-linked-list of the least recently used elements. */ class LRU{ constructor(){ //类似链表存储 // the least recently used item this.first = null; // the most recently used item this.last = null; // a list of all items in the lru list this.items = {}; //按node的id存储。(id为0的就是root,name='r') this.elements = 0; this.numPoints = 0; } size(){ return this.elements; } contains(node){ return this.items[node.id] == null; } touch(node){//链接node,并且永远放在最后. (每次updatePointClouds都要刷新一次链表) if (!node.loaded) { return; } let item; if (this.items[node.id] == null) { // add to list item = new LRUItem(node); item.previous = this.last; this.last = item; if (item.previous !== null) { item.previous.next = item; } this.items[node.id] = item; this.elements++; if (this.first === null) { this.first = item; } this.numPoints += node.numPoints; } else { // update in list item = this.items[node.id]; if (item.previous === null) { // handle touch on first element if (item.next !== null) { this.first = item.next; this.first.previous = null; item.previous = this.last; item.next = null; this.last = item; item.previous.next = item; } } else if (item.next === null) { // handle touch on last element } else {//从原来的位置挑出放最后 // handle touch on any other element item.previous.next = item.next; item.next.previous = item.previous; item.previous = this.last; item.next = null; this.last = item; item.previous.next = item; } } } //因为需要显示的都放末尾,所以不显示的部分都在前面,删除时从头删除。(但并不代表开头的一定是不显示的,所以如果第一个仍是显示的,它很可能是root,也就是name='r'的nodeGeo,删除它也就会删除全部) remove(node){ let lruItem = this.items[node.id]; if (lruItem) { if (this.elements === 1) { this.first = null; this.last = null; } else { if (!lruItem.previous) { this.first = lruItem.next; this.first.previous = null; } if (!lruItem.next) { this.last = lruItem.previous; this.last.next = null; } if (lruItem.previous && lruItem.next) { lruItem.previous.next = lruItem.next; lruItem.next.previous = lruItem.previous; } } delete this.items[node.id]; this.elements--; this.numPoints -= node.numPoints; } } getLRUItem(){ if (this.first === null) { return null; } let lru = this.first; return lru.node; } toString(){ let string = '{ '; let curr = this.first; while (curr !== null) { string += curr.node.id; if (curr.next !== null) { string += ', '; } curr = curr.next; } string += '}'; string += '(' + this.size() + ')'; return string; } freeMemory(){ if (this.elements <= 1) { return; } while (this.numPoints > Potree.pointLoadLimit) { let element = this.first; let node = element.node; this.disposeDescendants(node); } } disposeDescendants(node){ let stack = []; stack.push(node); while (stack.length > 0) { let current = stack.pop(); // console.log(current); current.dispose(); //真正删除geometry等 this.remove(current); for (let key in current.children) { if (current.children.hasOwnProperty(key)) { let child = current.children[key]; if (child.loaded) { stack.push(current.children[key]); } } } } } } // // how to calculate the radius of a projected sphere in screen space // http://stackoverflow.com/questions/21648630/radius-of-projected-sphere-in-screen-space // http://stackoverflow.com/questions/3717226/radius-of-projected-sphere // class ExtendPointCloudMaterial extends PointCloudMaterial$1 { constructor (parameters = {}) { super(parameters); let getValid = (a, b) => { if(a !== undefined){ return a; }else { return b; } }; let maxSize = getValid(parameters.maxSize, 200.0); let orthoMaxSize = getValid(parameters.orthoMaxSize, 3.0); this._gradient = Gradients.RAINBOW;//Gradients.SPECTRAL;//海拔贴图种类 this.gradientTexture = ExtendPointCloudMaterial.generateGradientTexture(this._gradient); //this.matcapTexture = ExtendPointCloudMaterial.generateMatcapTexture(this._matcap); delete this.uniforms.screenWidth; delete this.uniforms.screenHeight; delete this.uniforms.clipBoxes; delete this.uniforms.clipPolygons; delete this.uniforms.clipPolygonVCount; delete this.uniforms.clipPolygonVP; delete this.uniforms.clipBoxCount; //注意:这里修改了uniforms后,还需要在PotreeRender中手动传递到shader, like: gl.uniformMatrix4fv(.... Object.assign(this.uniforms,{ resolution: { type: 'v2', value: new Vector2() }, maxSize: { type: "f", value: maxSize }, orthoMaxSize: { type: "f", value: orthoMaxSize }, gradient: { type: "t", value: this.gradientTexture }, clipBoxes_in: { type: "Matrix4fv", value: [] }, clipBoxes_out: { type: "Matrix4fv", value: [] }, clipBoxBig_in: { type: "Matrix4fv", value: [] }, boxes_highlight: { type: "Matrix4fv", value: [] }, progress: { type: "f", value: 0 }, easeInOutRatio:{ type: "f", value: 0.3 }, pano0Map: { type: "t", value: null }, pano0Position: { type: "v3", value: new Vector3 }, pano0Matrix: { type: "m4", value: new Matrix4 }, pano1Map: { type: "t", value: null }, pano1Position: { type: "v3", value: new Vector3 }, pano1Matrix: { type: "m4", value: new Matrix4 }, prismList: { type: "Matrix3fv", value: null }, prismPoints: { type: "vec2fv", value: null }, baseHeightAreaMap:{ type:'t', value:null }, baseHeightBoundZ:{ type:'vec2', value:new Vector2 }, baseHeightBoundXY:{ type:'vec4', value:new Vector4 }, }); delete this.clipBoxes; this.clipBoxes_in = []; this.clipBoxes_out = []; this.highlightBoxes = []; this.prisms = []; let {vs, fs} = Common.changeShaderToWebgl2(Shaders['pointcloud_new.vs'],Shaders['pointcloud_new.fs'], 'selfBuild' ); Shaders['pointcloud_new.vs'] = vs; Shaders['pointcloud_new.fs'] = fs; this.updateShaderSource(); } updateShaderSource () { let vs = Shaders['pointcloud_new.vs']; //改 let fs = Shaders['pointcloud_new.fs']; //改 let definesString = this.getDefines(); let vsVersionIndex = vs.indexOf("#version "); let fsVersionIndex = fs.indexOf("#version "); if(vsVersionIndex >= 0){ vs = vs.replace(/(#version .*)/, `$1\n${definesString}`); }else { vs = `${definesString}\n${vs}`; } if(fsVersionIndex >= 0){ fs = fs.replace(/(#version .*)/, `$1\n${definesString}`); }else { fs = `${definesString}\n${fs}`; } this.vertexShader = vs; this.fragmentShader = fs; if (this.opacity === 1.0 && !this.useFilterByNormal) {//add useFilterByNormal this.blending = NoBlending; this.transparent = false; this.depthTest = true; this.depthWrite = true; this.depthFunc = LessEqualDepth; } else if ( (this.opacity < 1.0 ||this.useFilterByNormal) && !this.useEDL) {//add useFilterByNormal this.blending = AdditiveBlending; this.transparent = true; this.depthTest = false; this.depthWrite = true; this.depthFunc = AlwaysDepth; } if (this.weighted) { this.blending = AdditiveBlending; this.transparent = true; this.depthTest = true; this.depthWrite = false; } this.shaderNeedsUpdate = true; } getDefines () { let defines = []; if (this.pointSizeType === PointSizeType.FIXED) { defines.push('#define fixed_point_size'); } else if (this.pointSizeType === PointSizeType.ATTENUATED) { defines.push('#define attenuated_point_size'); } else if (this.pointSizeType === PointSizeType.ADAPTIVE) { defines.push('#define adaptive_point_size'); } if(!Features.EXT_DEPTH.isSupported(viewer.renderer.getContext()) && this.shape === PointShape$1.PARABOLOID){ this.shape = PointShape$1.SQUARE ;//强行替换 } if (this.shape === PointShape$1.SQUARE) { defines.push('#define square_point_shape'); } else if (this.shape === PointShape$1.CIRCLE) { defines.push('#define circle_point_shape'); } else if (this.shape === PointShape$1.PARABOLOID) { defines.push('#define paraboloid_point_shape'); } //console.log('this.shape PARABOLOID', this.shape, this.shape === PointShape.PARABOLOID) if (this._useEDL || this.fakeEDL) { defines.push('#define use_edl'); } if(this.activeAttributeName){ let attributeName = this.activeAttributeName.replace(/[^a-zA-Z0-9]/g, '_'); defines.push(`#define color_type_${attributeName}`); } if(this._treeType === TreeType.OCTREE){ defines.push('#define tree_type_octree'); }else if(this._treeType === TreeType.KDTREE){ defines.push('#define tree_type_kdtree'); } if (this.weighted) { defines.push('#define weighted_splats'); } for(let [key, value] of this.defines){ defines.push(value); } return defines.join("\n"); } get pointSizeType () { return this._pointSizeType; } set pointSizeType (value) { if(typeof value == 'string' )value = PointSizeType[value]; super.pointSizeType = value; /* if (this._pointSizeType !== value) { this._pointSizeType = value; this.updateShaderSource(); //这句表明这个属性频繁更改会卡顿 this.dispatchEvent({ type: 'point_size_type_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); } */ } get color () { return this.uniforms.uColor.value; } set color (value) {//改 if(value == this.color_)return let color = value; //if (!this.uniforms.uColor.value.equals(value)) { if(typeof value == 'string') { var colorArr = Potree.config.colors[value]; if(!colorArr){ //console.warn('没找到该颜色值'+ value) }else { color = new Color().fromArray(colorArr).multiplyScalar(1/255); } } this.uniforms.uColor.value.set(color); //this.uniforms.uColor.value.copy(value); this.dispatchEvent({ type: 'color_changed', target: this }); this.dispatchEvent({ type: 'material_property_changed', target: this }); //} this.color_ = value; //记录下str } ////////////////////////add setProjectedPanos(pano0, pano1, progressValue, easeInOutRatio){ //this.uniforms.usePanoMap.value = 1 this.usePanoMap = true; progressValue!=void 0 && (this.uniforms.progress.value = progressValue); //pano0.ensureSkyboxReadyForRender(); this.uniforms.pano0Map.value = pano0.getSkyboxTexture(); this.uniforms.pano0Position.value.copy(pano0.position); this.uniforms.pano0Matrix.value.copy(pano0.panoMatrix ); //pano1.ensureSkyboxReadyForRender(); this.uniforms.easeInOutRatio.value = easeInOutRatio || 0; //之前做点云和全景混合时加的,为了让点云颜色柔和切换到全景颜色。如不混合就0 this.uniforms.pano1Map.value = pano1.getSkyboxTexture(); this.uniforms.pano1Position.value.copy(pano1.position); this.uniforms.pano1Matrix.value.copy(pano1.panoMatrix ); //this.updateShaderSource() //this.needsUpdate = true; } stopProjectedPanos(){ //this.uniforms.usePanoMap.value = 0 this.usePanoMap = false; } setClipBoxes (bigClipInBox, clipBoxes_in, clipBoxes_out, highlightBoxes, prismPolygons=[]) { if (!clipBoxes_in || !clipBoxes_out) { return; } this.uniforms.clipBoxBig_in.value = bigClipInBox && bigClipInBox.inverse; this.uniforms.clipBoxes_in.value = new Float32Array(clipBoxes_in.length * 16); this.uniforms.clipBoxes_out.value = new Float32Array(clipBoxes_out.length * 16); this.uniforms.boxes_highlight.value = new Float32Array(highlightBoxes.length * 16); for (let i = 0; i < clipBoxes_in.length; i++) { let box = clipBoxes_in[i]; this.uniforms.clipBoxes_in.value.set(box.inverse.elements, 16 * i); } for (let i = 0; i < clipBoxes_out.length; i++) { let box = clipBoxes_out[i]; this.uniforms.clipBoxes_out.value.set(box.inverse.elements, 16 * i); } for (let i = 0; i < highlightBoxes.length; i++) { let box = highlightBoxes[i]; this.uniforms.boxes_highlight.value.set(box.inverse.elements, 16 * i); } /* for (let i = 0; i < this.uniforms.clipBoxes.value.length; i++) {?? if (Number.isNaN(this.uniforms.clipBoxes.value[i])) { this.uniforms.clipBoxes.value[i] = Infinity; } } */ if(prismPolygons.length){ let pointsCount = prismPolygons.pointsCount = prismPolygons.reduce((w,c)=>{return w+c.points.length},0); this.uniforms.prismList.value = new Float32Array(9 * prismPolygons.length); this.uniforms.prismPoints.value = new Float32Array(2 * pointsCount); prismPolygons.maxPointsCount = 0;//单个prism最大点个数 let pointIndex = 0; for(let i=0;i{ let points2 = [] , len = points.length; for(let i=0;ipoints2.push(...add(ps))); return points2 }else if(points.length > 2 && !points[2].equals(points[1])){ return add(points) }else return points } var LineDraw = { createLine: function (posArr, o={}) { //多段普通线 (第二个点和第三个点之间是没有线段的, 所以不用在意线段顺序) var mat; if(o.mat){ mat = o.mat; }else { let prop = Object.assign({ lineWidth: o.lineWidth || 1, //windows无效。 似乎mac/ios上粗细有效 ? color: o.color || defaultColor },o); if(o.deshed ){ prop.dashSize = o.dashSize || 0.1, prop.gapSize = o.gapSize || 0.1; } mat = new THREE$1[o.deshed ? "LineDashedMaterial" : "LineBasicMaterial"](prop); } var line = new LineSegments(new BufferGeometry, mat); line.renderOrder = o.renderOrder || Potree.config.renderOrders.lines; this.moveLine(line, posArr); return line; }, moveLine: function (line, posArr) { //if(posArr.length == 0)return if(!line.uncontinuous || posArr[0] && posArr[0] instanceof Array)posArr = dealPosArr(posArr); let position = []; posArr.forEach(e=>position.push(e.x,e.y,e.z)); line.geometry.setAttribute('position', new Float32BufferAttribute(/* new Float32Array( */position/* ) */, 3)); line.geometry.attributes.position.needsUpdate = true; line.geometry.computeBoundingSphere(); if(line.material instanceof LineDashedMaterial){ line.computeLineDistances(); //line.geometry.attributes.lineDistance.needsUpdate = true; line.geometry.verticesNeedUpdate = true; //没用 } } , createFatLineMat : function(o){ var supportExtDepth = !!Features.EXT_DEPTH.isSupported(); let params = $.extend({}, { //默认 lineWidth : 5, color:0xffffff, transparent : true, depthWrite:false, depthTest:false, dashSize : 0.1, gapSize:0.1, }, o, { //修正覆盖: dashed: o.dashWithDepth ? supportExtDepth && !!o.dashed : !!o.dashed , dashWithDepth:!!o.dashWithDepth,//只在被遮住的部分显示虚线 useDepth: !!o.useDepth, supportExtDepth, }); var mat = new LineMaterial(params); //if(o.dashed)(mat.defines.USE_DASH = "") return mat; }, /* 创建可以改变粗细的线。 */ createFatLine : function(posArr, o){ var geometry = new LineGeometry(); geometry.setColors( o.color || [1,1,1]); var matLine = o.mat || this.createFatLineMat(o); var line = new Line2( geometry, matLine ); //line.computeLineDistances(); line.uncontinuous = o.uncontinuous; //线不连续,由线段组成 line.scale.set( 1, 1, 1 ); line.renderOrder = Potree.config.renderOrders.lines; this.moveFatLine(line, posArr); return line; }, moveFatLine: function(line, posArr){ var geometry = line.geometry; var positions = []; if(!line.uncontinuous || posArr[0] && posArr[0] instanceof Array) posArr = dealPosArr(posArr); posArr.forEach(e=>{positions.push(...e.toArray());}); if(!geometry){ geometry = line.geometry = new LineGeometry(); } if(geometry.attributes.instanceEnd && geometry.attributes.instanceEnd.data.array.length != positions.length){//positions个数改变会有部分显示不出来,所以重建 geometry.dispose(); geometry = new LineGeometry(); line.geometry = geometry; } geometry.setPositions( positions ); if(line.material.defines.USE_DASH != void 0){ //line.geometry.verticesNeedUpdate = true; //没用 line.geometry.computeBoundingSphere(); //for raycaster line.computeLineDistances(); } }, updateLine: function(line, posArr){ if(line instanceof Line2){ LineDraw.moveFatLine(line,posArr); }else { LineDraw.moveLine(line,posArr); } }, /* 为line创建用于检测鼠标的透明mesh,实际是个1-2段圆台。 由于近大远小的原因,假设没有透视畸变、创建的是等粗的圆柱的话, 所看到的线上每个位置的粗细应该和距离成反比。所以将圆柱改为根据距离线性渐变其截面半径的圆台,在最近点(相机到线的垂足)最细。如果最近点在线段上,则分成两段圆台,否则一段。 */ createBoldLine:function(points, o){ o = o || {}; var cylinder = o && o.cylinder; var CD = points[1].clone().sub(points[0]); var rotate = function(){//根据端点旋转好模型 cylinder.lastVector = CD;//记录本次的端点向量 var AB = new Vector3(0,-1,0); var axisVec = AB.clone().cross(CD).normalize(); //得到垂直于它们的向量,也就是旋转轴 var rotationAngle = AB.angleTo(CD); cylinder.quaternion.setFromAxisAngle( axisVec, rotationAngle ); }; if(o && o.type == "init"){ cylinder = new Mesh(); cylinder.material = o.mat; if(CD.length() == 0)return cylinder; rotate(); } if(CD.length() == 0)return cylinder; if(o.type != "update"){ var CDcenter = points[0].clone().add(points[1]).multiplyScalar(.5); cylinder.position.copy(CDcenter); if(!cylinder.lastVector || o.type == "moveAndRotate")rotate(); else if(cylinder.lastVector && CD.angleTo(cylinder.lastVector)>0) rotate();//线方向改了or线反向了 重新旋转一下模型 if(config.isEdit && !objects.mainDesign.editing )return cylinder;//节省初始加载时间? } //为了保证线段任何地方的可检测点击范围看起来一样大,更新圆台的结构(但是在镜头边缘会比中心看起来大) var height = points[0].distanceTo(points[1]); var standPos = o && o.standPos || objects.player.position; var k = config.isMobile ? 20 : 40; var dis1 = points[0].distanceTo(standPos); var dis2 = points[1].distanceTo(standPos); var foot = math.getFootPoint(standPos, points[0], points[1]);//垂足 if(o.constantBold || objects.player.mode != "panorama"){ var width = 0.1;//0.08; var pts = [new Vector2(width ,height/2),new Vector2(width ,-height/2)]; }else if(foot.clone().sub(points[0]).dot( foot.clone().sub(points[1]) ) > 0){//foot不在线段上 var pts = [new Vector2(dis1 / k,height/2),new Vector2(dis2 / k,-height/2)]; }else {//在线段上的话,要在垂足这加一个节点,因它距离站位最近,而两端较远 var dis3 = foot.distanceTo(standPos); var len = foot.distanceTo(points[0]); var pts = [new Vector2(dis1 / k,height/2), new Vector2(dis3 / k,height/2-len), new Vector2(dis2 / k,-height/2)]; } cylinder.geometry && cylinder.geometry.dispose();//若不删除会占用内存 cylinder.geometry = new LatheBufferGeometry( pts, 4/* Math.min(dis1,dis2)<10?4:3 */ ); cylinder.renderOrder = 2; return cylinder; }, updateBoldLine:function(cylinder, points, type, standPos, constantBold){ this.createBoldLine(points,{type:type, cylinder : cylinder, standPos:standPos, constantBold}); //type:move:平移 会改长短 , type:update根据距离和角度更新 不改长短 }, }; var MeshDraw = { getShape: function(shapes, holes){ //不一定闭合 暂时所有shapes共享holes。如果要单独的话, shapes改为[{shape:[],holes:[]},{}]的形式 if(shapes[0] && !(shapes[0] instanceof Array) ){//仅是一个shape的点 shapes = [shapes]; } let holesArr = []; if(holes){//挖空 holes.forEach((points)=>{ var holePath = new Path(); holePath.moveTo( points[0].x, points[0].y ); for(var i=1,len=points.length; i{ var shape = new Shape(); shape.moveTo( points[0].x, points[0].y ); for(var i=1,len=points.length; i{ shape.moveTo( points[0].x, points[0].y ); for(var i=1,len=points.length; i{ var holePath = new THREE.Path() holePath.moveTo( points[0].x, points[0].y ) for(var i=1,len=points.length; i{ if(currentIndex == 0)return 0 return total + currentValue.distanceTo(arr[currentIndex-1]); },0); //options.extrudePath = new THREE.CatmullRomCurve3(options.extrudePath) if(options.extrudePath.length == 2){ options.tension = 0 ;//否则一端扭曲 options.steps = 1; } {//去掉重复的点 let path = []; const minDis = options.dontSmooth ? 0 : 0.2; //CatmullRomCurve3 经常扭曲,如果两个点靠得很近可能会扭曲,这里去除靠的太近的点。但去除后依旧会出现一定扭曲. options.extrudePath.forEach((p,i)=>{ if(i==0 || i== options.extrudePath.length-1)return path.push(p) //首尾直接加入 let last = path[path.length-1];//和上一个比 let dis = last.distanceTo(p); if(dis <= minDis){ console.log(`第${i}个点(${p.toArray()})因为和上一个数据(${last.toArray()})太接近(dis:${dis})所以删除`); }else if(i == options.extrudePath.length - 2){//因为最后一个必定加入,所以倒数第二个还也不能太靠近最后一个 last = options.extrudePath[options.extrudePath.length-1]; //和下一个(最后一个比) if(dis <= minDis){ console.log(`第${i}个点(${p.toArray()})因为和下一个数据(${last.toArray()})太接近(dis:${dis})所以删除`); }else { path.push(p); } }else { path.push(p); } }); options.extrudePath = path; } if(options.dontSmooth){ let curvePath = new CurvePath();//通用的曲线路径对象,它可以包含直线段和曲线段。在这里只做折线 for (let i = 0; i < options.extrudePath.length - 1; i++){ let curve3 = new LineCurve3(options.extrudePath[i], options.extrudePath[i + 1]);//添加线段 curvePath.add(curve3); } options.extrudePath = curvePath; }else { //平滑连续的曲线(但经常会有扭曲的问题,tension:0能缓解, 另外shape和path都最好在原点附近,也就是点需减去bound.min ) options.extrudePath = new CatmullRomCurve3(options.extrudePath, options.closed , 'catmullrom' /* 'centripetal' */ , options.tension);//tension:拐弯剧烈程度,但随着长度增长,该值需要减小,否则会扭曲 } } var extrudeSettings = $.extend(options,{ steps: options.steps != void 0 ? options.steps : ( options.extrudePath ? Math.round(length/(options.spaceDis || 0.3)) : 1), //分成几段 spaceDis每段长度 bevelEnabled: false, //不加的话,height为0时会有圆弧高度 //openEnded默认false }); var geometry = new ExtrudeBufferGeometry( shape, extrudeSettings ); //修改了three.js文件, buildLidFaces处,创建顶底面加了选项,可以选择开口。 return geometry; }, getUnPosPlaneGeo : function(){//获取还没有赋值位置的plane geometry var e = new Uint16Array([0, 1, 2, 0, 2, 3]) // , t = new Float32Array([-.5, -.5, 0, .5, -.5, 0, .5, .5, 0, -.5, .5, 0]) , i = new Float32Array([0, 0, 1, 0, 1, 1, 0, 1]) , g = new BufferGeometry; g.setIndex(new BufferAttribute(e, 1)), //g.addAttribute("position", new n.BufferAttribute(t, 3)), g.setAttribute("uv", new BufferAttribute(i, 2)); return function(){ return g } }(), getPlaneGeo : function(A,B,C,D){ var geo = this.getUnPosPlaneGeo().clone(); var pos = [ A.x, A.y, A.z, B.x, B.y, B.z, C.x, C.y, C.z, D.x, D.y, D.z ]; //geo.addAttribute("position", new THREE.BufferAttribute(pos, 3)) geo.setAttribute('position', new Float32BufferAttribute(pos, 3)); geo.computeVertexNormals(); geo.computeBoundingSphere(); //for raycaster return geo; }, drawPlane : function(A,B,C,D, material){ var wall = new Mesh(this.getPlaneGeo(A,B,C,D), material); return wall; }, movePlane: function(mesh, A,B,C,D){ var pos = new Float32Array([ A.x, A.y, A.z, B.x, B.y, B.z, C.x, C.y, C.z, D.x, D.y, D.z ]); mesh.geometry.addAttribute("position", new BufferAttribute(pos, 3)); mesh.geometry.computeBoundingSphere();//for checkIntersect } , createGeometry:function(posArr, faceArr, uvArr, normalArr ){//创建复杂mesh. faceArr:[[0,1,2],[0,2,3]] let geo = new BufferGeometry; let positions = []; posArr.forEach(p=>positions.push(p.x,p.y,p.z)); geo.setAttribute('position', new Float32BufferAttribute(positions, 3)); if(faceArr){ let indice = []; faceArr.forEach(f=>indice.push(...f)); geo.setIndex(indice); // auto set Uint16BufferAttribute or Uint32BufferAttribute } if(uvArr){ let uvs = []; uvArr.forEach(uv=>uvs.push(uv.x,uv.y)); geo.setAttribute("uv", new Float32BufferAttribute(uvs, 2)); } if(normalArr){ let normals = []; normalArr.forEach(n=>normals.push(n.x,n.y,n.z)); geo.setAttribute("normal", new Float32BufferAttribute(normals, 3)); } /* geo.computeVertexNormals() geo.computeBoundingSphere() //for raycaster */ return geo }, updateGeometry:function(geo, posArr, faceArr, uvArr, normalArr ){//创建复杂mesh. faceArr:[[0,1,2],[0,2,3]] let positions = []; posArr.forEach(p=>positions.push(p.x,p.y,p.z)); geo.setAttribute('position', new Float32BufferAttribute(positions, 3)); geo.attributes.position.needsUpdate = true; if(faceArr){ let indice = []; faceArr.forEach(f=>indice.push(...f)); geo.setIndex(indice); // auto set Uint16BufferAttribute or Uint32BufferAttribute } if(uvArr){ let uvs = []; uvArr.forEach(uv=>uvs.push(uv.x,uv.y)); geo.setAttribute("uv", new Float32BufferAttribute(uvs, 2)); } if(normalArr){ let normals = []; normalArr.forEach(n=>normals.push(n.x,n.y,n.z)); geo.setAttribute("normal", new Float32BufferAttribute(normals, 3)); } /* geo.computeVertexNormals() */ geo.computeBoundingSphere(); //for raycaster and visi return geo } }; class DepthBasicMaterial extends ShaderMaterial{ constructor(o={}){ let {width, height} = viewer.renderer.getSize(new Vector2()); let uniforms = { resolution: { type: 'v2', value: new Vector2(width, height ) }, viewportOffset: { type: 'v2', value: new Vector2(0, 0 ) }, //left, top //uUseOrthographicCamera:{ type: "b", value: false }, nearPlane: { type: 'f', value: 0.1 }, farPlane: { type: 'f', value: 10000 }, depthTexture: { type: 't', value: null }, opacity: { type: 'f', value: 1 }, map: { type: 't', value: o.map }, baseColor: {type:'v3', value: o.color ? new Color(o.color) : new Color("#ffffff")}, backColor: {type:'v3', value: o.backColor ? new Color(o.backColor) : new Color("#ddd")}, clipDistance : { type: 'f', value: o.clipDistance || 4 }, //消失距离 occlusionDistance : { type: 'f', value: o.occlusionDistance || 1 }, //变为backColor距离 maxClipFactor : { type: 'f', value: o.maxClipFactor || 1 }, //0-1 maxOcclusionFactor : { type: 'f', value: o.maxOcclusionFactor || 1 }, //0-1 mapScale: { type: 'f', value: o.mapScale || 1 }, //0-1 }; let {vs,fs} = Common.changeShaderToWebgl2(Shaders['depthBasic.vs'], Shaders['depthBasic.fs'], 'ShaderMaterial'); super({ uniforms, vertexShader: vs, fragmentShader: fs, depthWrite: false, depthTest: false, transparent: o.transparent == void 0 ? true : o.transparent, side: o.side || 0 /* THREE.DoubleSide */, }); this.events = { setSize:(e)=>{//如果出现横条状的异常,往往是viewportOffset出错 //地图不需要 if(!this.realUseDepth || !e.viewport)return let viewport = e.viewport; let viewportOffset = viewport.offset || new Vector2(); this.uniforms.resolution.value.copy(viewport.resolution2); //2023.6.12突然发现ratio>1的用resolution不对,得用2才对。但是之前明明记得不是这样 this.uniforms.viewportOffset.value.copy(viewportOffset); }, render:(e)=>{//before render 如果有大于两个viewport的话,不同viewport用不同的depthTex this.updateDepthParams(e); }, /* cameraChange:(e)=>{ if(e.changeInfo.projectionChanged){//resize时也会触发。虽然保守起见的话加上resize比较好//所以当时为何不用resize //console.log('projectionChanged') this.events.setSize(e) } } */ }; //-----其他---- this.autoDepthTest = o.autoDepthTest; if(o.opacity != void 0){ this.opacity = o.opacity; } this.useDepth = o.useDepth; this.map = o.map; } get useDepth(){ return this.useDepth_ } set useDepth(value){ value = value && Features.EXT_DEPTH.isSupported(); //如果不支持 EXT_DEPTH 的话会失效 if(this.useDepth_ != value){ this.setRealDepth(value); this.useDepth_ = value; if(value){ viewer.addEventListener("render.begin", this.events.render); //viewer.addEventListener('camera_changed', this.events.cameraChange) viewer.addEventListener('resize', this.events.setSize); this.updateDepthParams(); }else { viewer.removeEventListener("render.begin", this.events.render); viewer.removeEventListener('resize', this.events.setSize); } } } setRealDepth(useDepth, viewport){//确实使用到depthTex if(this.realUseDepth != useDepth){ if(useDepth ){ this.defines.useDepth = ''; }else { delete this.defines.useDepth; } this.realUseDepth = useDepth; if(this.autoDepthTest)this.depthWrite = this.depthTest = !useDepth; //如果useDepth = false,使用原始的depthTest this.needsUpdate = true; if(!viewport)viewport = viewer.mainViewport; //暂时这么设置 useDepth && this.events.setSize({viewport}); } } get map(){ return this.uniforms.map.value } set map(map){ this.uniforms.map.value = map; if(map){ this.defines.use_map = ''; }else { delete this.defines.use_map; } } get opacity(){ return this.uniforms.opacity.value } set opacity(o){ this.uniforms && (this.uniforms.opacity.value = o); } get color(){ return this.uniforms.baseColor.value } set color(c){ this.uniforms && (this.uniforms.baseColor.value.set(c)); } /* dispose(){ super.dispose() viewer.depthBasic } */ copy(source){ super.copy(source); this.useDepth = source.useDepth; this.map = source.map; return this } updateDepthParams(e={}){//主要用于点云遮住mesh var viewport = e.viewport || viewer.mainViewport; var camera = viewport.camera; let hasDepth = this.useDepth && camera.isPerspectiveCamera && (Potree.settings.pointEnableRT || Potree.settings.displayMode == 'showPanos' || viewer.useEDL); this.setRealDepth(hasDepth, viewport); if(hasDepth){ this.uniforms.depthTexture.value = viewer.getPRenderer().getRtEDL(viewport).depthTexture; //其实只赋值一次就行 this.uniforms.nearPlane.value = camera.near; this.uniforms.farPlane.value = camera.far; } //this.uniforms.uUseOrthographicCamera.value = !camera.isPerspectiveCamera } } const geo = new PlaneBufferGeometry(1,1); class Sprite$2 extends Mesh{ constructor(options={}){ super(geo, options.mat || new DepthBasicMaterial(options));/* ({map:options.map, useDepth:options.useDepth})) */ this.root = options.root || this; this.renderOrder = options.renderOrder != void 0 ? options.renderOrder : 4; this.pickOrder = options.pickOrder || 0; this.sizeInfo = options.sizeInfo; this.dontFixOrient = options.dontFixOrient; this.options = options; this.position.y = options.disToLine || 0; //离线距离 this.matrixAutoUpdate = false; this.matrixMap = new Map(); if(this.root != this){ this.matrixMapRoot = new Map(); this.root.matrixAutoUpdate = false; } this.visiMap = new Map(); this.name = options.name || 'sprite'; this.useViewport = null; this.viewports = options.viewports;//指定更新的viewports this.visible_ = true; let clear = (e)=>{ this.matrixMap.clear();//清空后在所有viewport上都必须更新才能渲染 //this.needsUpdate = true }; viewer.mapViewer && viewer.mapViewer.addEventListener("camera_changed", clear); viewer.addEventListener("camera_changed", clear); /* if(viewer.viewports.length == 1){//直接更新。如果有多个不在这更新,在"render.begin" this.update(e) } */ let applyMatrix = (e)=>{ this.applyMatrix(e); }; viewer.addEventListener("raycaster", applyMatrix); //before render viewer.addEventListener("render.begin", applyMatrix); //before render viewer.addEventListener("render.begin2", applyMatrix); viewer.addEventListener("cameraSetLayers", applyMatrix); this.addEventListener('dispose', ()=>{ viewer.mapViewer && viewer.mapViewer.removeEventListener("camera_changed", clear); viewer.removeEventListener("camera_changed", clear); viewer.removeEventListener("raycaster", applyMatrix); //before render viewer.removeEventListener("render.begin", applyMatrix); viewer.removeEventListener("render.begin2", applyMatrix); }); } set visible(v){ let oldV = this.visible_; this.visible_ = v; if(v && !oldV){ this.matrixMap && this.matrixMap.clear(); //this.update() //update内有unableCompute会无限回调 } } get visible(){ return this.visible_ } realVisible(viewport, interactables/* , raycaster */){ if(interactables){ if(!interactables.some((object)=>{//interactables中是否能找到this let finded; object.traverse((object)=>{ if(object == this){ finded = true; return {stopContinue:true} } }); return finded }))return } /* if(interactables && viewport.name == 'mapViewport'){ console.log(this) } */ if(!(/* raycaster || */viewport.camera).layers.test(this.layers)){//如地图上一般不可见测量线 return false } if(!this.visible && this.unvisibleReasons && this.unvisibleReasons.some(e=>e.reason != 'unableCompute')){ return false } let v = true; let parent = this.parent; let lastParent = this; while(parent){ if(parent.visible === false){ v = false; break; } lastParent = parent; parent = parent.parent; } if(v && !(lastParent instanceof Scene)){//已被删除 v = false; } /* if(!this.latestRealVisi && v){//变为可见后先update this.latestRealVisi = true setTimeout(()=>{ this.update() },1)//延迟 防止无限调用 return false } this.latestRealVisi = v */ return v; } waitUpdate(){ this.matrixMap.clear();//清空后在所有viewport上都必须更新才能渲染 //viewer.dispatchEvent('content_changed') } update(e={}){ if(!e.viewport){ let viewports = this.viewports || viewer.viewports; if(!viewports)return viewports.forEach(view=>{ this.update({viewport:view}); }); return; } if(!this.root || ! this.realVisible(e.viewport, e.interactables) /* this.visible */ )return if(this.viewports && !this.viewports.includes(e.viewport) )return if(e.viewport.name == 'magnifier')return let camera = e.viewport.camera; //rotation if(!this.dontFixOrient){ //orthoCamera一般要加dontFixOrient let orient2dAngle; if(this.root.lineDir){ this.root.updateMatrix();//先更新,getWorldPosition才能得到正确的 this.root.updateMatrixWorld(true); let center = this.root.getWorldPosition(new Vector3()); //由于两个端点容易在屏幕外,所以使用center和center加dir let setVisi = (state)=>{ this.visiMap.set(e.viewport, state); Potree.Utils.updateVisible(this, 'unableCompute', !!state); }; let renderer = e.viewer ? e.viewer.renderer : e.renderer; let r1 = Potree.Utils.getPos2d(center, e.viewport, renderer.domElement.parentElement, renderer); //let r1 = Potree.Utils.getPos2d(center, e.viewport, viewer.renderArea, viewer.renderer); if(!r1.trueSide)return setVisi(false);// 但这句会使realVisible为false从而无法更新//console.error('!r1.trueSide') //中心点如果在背面直接不渲染了 let r2, point2; let p2State = '', len=1, p2StateHistory = []; while(p2State != 'got' && p2StateHistory.length<10){ point2 = center.clone().add(this.root.lineDir.clone().multiplyScalar(len)); r2 = Potree.Utils.getPos2d(point2, e.viewport , renderer.domElement.parentElement, renderer ); if(!r2.trueSide){ //很少遇到点2在背面的 if(!p2StateHistory.includes('tooLong-reverse')){ p2State = 'tooLong-reverse'; //先尝试反向 len = -len; }else { p2State = 'tooLong'; len = len / 2; } }else { let dis = r2.pos.distanceTo(r1.pos); if(math.closeTo(dis,0)){ //console.log('dis == 0') setVisi(false); return break } if(dis<10 && !p2StateHistory.includes('tooLong')){//和r1的屏幕距离太近,要加长,否则精度过低 p2State = 'tooShort'; len = 100/dis * len; }else { p2State = 'got'; break; } } p2StateHistory.push(p2State); } //console.log(p2StateHistory,len) if(!r2.trueSide){ return setVisi(false)//, console.log(' !r2.trueSide', ) } let p1 = r1.pos, p2 = r2.pos; if(p2StateHistory.filter(e=>e == 'tooLong-reverse').length%2 == 1){//反,for marker p2 = r1.pos, p1 = r2.pos; } let vec = new Vector2().subVectors(p1,p2); orient2dAngle = -vec.angle(); //根据测量线在屏幕上的角度在旋转label,使之和屏幕上的二维线平行。 let y = Math.abs(this.position.y); let facePlane = this.root.measure && this.root.measure.facePlane; let eyeDir = new Vector3().subVectors(center,camera.position); let clockWise = facePlane && facePlane.normal.dot(eyeDir/* e.viewport.view.direction */) < 0; if(p1.x < p2.x){ orient2dAngle += Math.PI; //避免字是倒着的情况。(使字一直在线的下方) clockWise != void 0 && (this.position.y = clockWise ? y : -y); }else { clockWise != void 0 && (this.position.y = clockWise ? -y : y); //使area类型的edgeLabel都在外侧 } //this.parent.text && console.log(this.parent.text, clockWise, this.position.y, e.viewport.name /* THREE.Math.radToDeg(angle), p1.x < p2.x */ ) setVisi(true); } let parentQua = this.root.parent.getWorldQuaternion(new Quaternion); this.root.quaternion.multiplyQuaternions(parentQua.invert(),camera.quaternion); //乘上parentQua.invert()是为了中和掉父结点的qua,使只剩下camera.quaternion if(orient2dAngle){ let qua = new Quaternion().setFromAxisAngle(new Vector3(0,0,1), orient2dAngle); this.root.quaternion.multiply(qua); } } //scale var info = this.sizeInfo; if(info){ this.root.updateMatrix();//先更新,getWorldPosition才能得到正确的 this.root.updateMatrixWorld(true); var scale; if(info.nearBound == void 0 && info.farBound != void 0 || info.nearBound != void 0 && info.farBound == void 0){//仅限制最大或最小的话,不判断像素大小,直接限制mesh的scale //这个判断也可以写到getScaleForConstantSize里,可以更严谨控制像素宽度,这里只简单计算大小 var dis = camera.position.distanceTo(this.root.getWorldPosition(new Vector3())); if(info.farBound == void 0 && dis < info.nearBound){ scale = info.scale * dis / info.nearBound; }else if(info.nearBound == void 0 && dis > info.farBound){ scale = info.scale * dis / info.farBound; }else { scale = info.scale; } }else { scale = math.getScaleForConstantSize($.extend(info,{//规定下最小最大像素 camera , position:this.root.getWorldPosition(new Vector3()) , resolution: e.viewport.resolution//2 })); } if(!isNaN(scale)){ this.root.scale.set(scale, scale, scale); } } this.updateMatrix(); //this.root.updateMatrixWorld(true) //console.log(this.root.text, this.root.matrix.elements) this.matrixMap.set(e.viewport, this.matrix.clone()); if(this.root != this){ this.root.updateMatrix(); //因this.position可能在两个viewport不同 this.matrixMapRoot.set(e.viewport, this.root.matrix.clone()); } this.needsUpdate = false; this.useViewport = e.viewport; } applyMatrix(e){ if(!e)e = {viewport:viewer.mainViewport};//随便写一个viewport let visi = this.visiMap.get(e.viewport); //还原可见性 Potree.Utils.updateVisible(this, 'unableCompute', visi == false ? false : true ); /* if(e.viewport.name == 'mapViewport' && visi && this.visiMap.get(viewer.mainViewport) == false){ console.log(1) } */ if(e.viewport.name == 'magnifier')return if(this.viewports && !this.viewports.includes(e.viewport) )return if( !this.root || !this.realVisible(e.viewport, e.interactables) )return var matrix = this.matrixMap.get(e.viewport); if(!matrix){ this.update(e); matrix = this.matrixMap.get(e.viewport); if(!matrix)return } if(e.viewport == this.useViewport){ return } this.useViewport = e.viewport; this.matrix.copy(matrix); if(this.root != this){ var matrix2 = this.matrixMapRoot.get(e.viewport); this.root.matrix.copy(matrix2); } e.raycaster && this.root.updateMatrixWorld(true);//渲染前会自动updateMatrixWorld,但raycaster不会 //console.log(this.root.name + e.viewport.name + " : "+this.root.matrixWorld.elements) } setUniforms(name,value){ this.material.setUniforms(name,value); } dispose(){ this.removeAllListeners(); this.parent && this.parent.remove(this); this.dispatchEvent('dispose'); } } /* let orient2d if(this.lineDir){ this.root.updateMatrix();//先更新,getWorldPosition才能得到正确的 this.root.updateMatrixWorld(true) let center = this.root.getWorldPosition(new THREE.Vector3()) //由于两个端点容易在屏幕外,所以使用center和center加dir let lineDir = this.lineDir.clone(); let r1 = Potree.Utils.getPos2d(center, camera, viewer.renderArea, e.viewport); if(!r1.trueSide)return Potree.Utils.updateVisible(this, 'unableCompute', false);// 但这句会使realVisible为false从而无法更新//console.error('!r1.trueSide') //中心点如果在背面直接不渲染了 let r2, point2 let p2State = '', len=1, p2StateHistory = [] while(p2State != 'got' && p2StateHistory.length<10){ point2 = center.clone().add(lineDir.multiplyScalar(len)); r2 = Potree.Utils.getPos2d(point2, camera, viewer.renderArea, e.viewport); if(!r2.trueSide){ //很少遇到点2在背面的 if(!p2StateHistory.includes('tooLong-reverse')){ p2State = 'tooLong-reverse' //先尝试反向 len = -len }else{ p2State = 'tooLong' len = len / 2 } }else{ let dis = r2.pos.distanceTo(r1.pos) if(dis == 0){ //console.log('dis == 0') Potree.Utils.updateVisible(this, 'unableCompute', false) return break } if(dis<10 && !p2StateHistory.includes('tooLong')){//和r1的屏幕距离太近,要加长,否则精度过低 p2State = 'tooShort' len = 100/dis * len }else{ p2State = 'got'; break; } } p2StateHistory.push(p2State) } //console.log(p2StateHistory,len) if(!r2.trueSide){ return Potree.Utils.updateVisible(this, 'unableCompute', false)//, console.log(' !r2.trueSide', ) } Potree.Utils.updateVisible(this, 'unableCompute', true) let p1 = r1.pos, p2 = r2.pos let vec = new THREE.Vector2().subVectors(p1,p2); let angle = -vec.angle() //根据测量线在屏幕上的角度在旋转label,使之和屏幕上的二维线平行。 if(p1.x < p2.x) angle += Math.PI //避免字是倒着的情况 orient2d = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0,0,1), angle) //console.log(this.parent.text, THREE.Math.radToDeg(angle), p1.x < p2.x ) } let parentQua = this.root.parent.getWorldQuaternion(new THREE.Quaternion) this.root.quaternion.multiplyQuaternions(parentQua.invert(),camera.quaternion) //乘上parentQua.invert()是为了中和掉父结点的qua,使只剩下camera.quaternion if(this.lineDir){ this.root.quaternion.multiply(orient2d) } */ //可能还是要用html写,因为要加按钮和图片 class TextSprite$2 extends Object3D{ //注:为了分两层控制scale,不直接extend Sprite constructor( options={}){ super(); let map = new Texture(); map.minFilter = LinearFilter; map.magFilter = LinearFilter; this.sprite = new Sprite$2( Object.assign({ root:this } ,options, { map, }) ); this.add(this.sprite); this.fontWeight = options.fontWeight == void 0 ? 'Bold' : options.fontWeight; this.rectBorderThick = options.rectBorderThick || 0; this.textBorderThick = options.textBorderThick || 0; this.fontface = 'Arial'; this.fontsize = options.fontsize || 16; this.textBorderColor = options.textBorderColor ? Common.CloneObject(options.textBorderColor):{ r: 0, g: 0, b: 0, a: 0.0 }; this.backgroundColor = options.backgroundColor ? Common.CloneObject(options.backgroundColor):{ r: 255, g: 255, b: 255, a: 1.0 }; this.textColor = options.textColor ? Common.CloneObject(options.textColor):{r: 0, g: 0, b: 0, a: 1.0}; this.borderColor = options.borderColor ? Common.CloneObject(options.borderColor):{ r: 0, g: 0, b: 0, a: 0.0 }; this.borderRadius = options.borderRadius || 6; this.margin = options.margin; this.setText(options.text); this.name = options.name; //this.setText(text); } setText(text){ if(text == void 0)text = ''; if (this.text !== text) { if (!(text instanceof Array)) { this.text = [text + '']; } else this.text = text; this.updateTexture(); this.sprite.waitUpdate(); //重新计算各个viewport的matrix } } setTextColor(color){ this.textColor = Common.CloneObject(color); this.updateTexture(); } setBorderColor(color){ this.borderColor = Common.CloneObject(color); this.updateTexture(); } setBackgroundColor(color){ this.backgroundColor = Common.CloneObject(color); this.updateTexture(); } setPos(pos){ this.position.copy(pos); this.sprite.waitUpdate(); } update(){ this.sprite.waitUpdate(); } /* setVisible(v){ Potree.Utils.updateVisible(this, 'setVisible', v) } */ setUniforms(name,value){ this.sprite.setUniforms(name,value); } updateTexture1(){ let canvas = document.createElement('canvas'); let context = canvas.getContext('2d'); const r = window.devicePixelRatio; context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface; //context["font-weight"] = 100; //语法与 CSS font 属性相同。 //this.text = '啊啊啊啊啊啊fag' let metrics = context.measureText(this.text ); let textWidth = metrics.width; let margin = (this.margin ? new Vector2().copy(this.margin) : new Vector2(this.fontsize, Math.max( this.fontsize*0.4, 10) )).clone().multiplyScalar(r); let spriteWidth = 2 * margin.x + textWidth + 2 * this.rectBorderThick * r ; let spriteHeight = 2 * margin.y + this.fontsize * r + 2 * this.rectBorderThick * r; context.canvas.width = spriteWidth; context.canvas.height = spriteHeight; context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface; /* let diff = 2//针对英文大部分在baseLine之上所以降低一点(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2 context.textBaseline = "middle" */ let expand = Math.max(1, Math.pow(this.fontsize / 16, 1.3)) * r; // 针对英文大部分在baseLine之上所以降低一点,或者可以识别当不包含jgqp时才加这个值 //canvas原点在左上角 context.textBaseline = 'alphabetic'; // "middle" //设置文字基线。当起点y设置为0时,只有该线以下的部分被绘制出来。middle时文字显示一半(但是对该字体所有字的一半,有的字是不一定显示一半的,尤其汉字),alphabetic时是英文字母的那条基线。 //let actualHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent; // 当前文本字符串在这个字体下用的实际高度 //文字y向距离从textBaseline向上算 let actualBoundingBoxAscent = metrics.actualBoundingBoxAscent == void 0 ? this.fontsize * r * 0.8 : metrics.actualBoundingBoxAscent; //有的流览器没有。只能大概给一个 let y = actualBoundingBoxAscent + margin.y + expand; //console.log(this.text, 'y' , y, 'actualBoundingBoxAscent', metrics.actualBoundingBoxAscent,'expand',expand ) // border color context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' + this.borderColor.b + ',' + this.borderColor.a + ')'; let rectBorderThick = this.rectBorderThick * r; context.lineWidth = rectBorderThick; // background color context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' + this.backgroundColor.b + ',' + this.backgroundColor.a + ')'; this.roundRect(context, rectBorderThick / 2 , rectBorderThick / 2, spriteWidth - rectBorderThick, spriteHeight - rectBorderThick, this.borderRadius * r); // text color if(this.textBorderThick){ context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' + this.textBorderColor.b + ',' + this.textBorderColor.a + ')'; context.lineWidth = this.textBorderThick * r; context.strokeText(this.text , rectBorderThick + margin.x, y /* spriteHeight/2 + diff */ ); } context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' + this.textColor.b + ',' + this.textColor.a + ')'; context.fillText(this.text , rectBorderThick + margin.x, y/* spriteHeight/2 + diff */ );//x,y let texture = new Texture(canvas); texture.minFilter = LinearFilter; texture.magFilter = LinearFilter; texture.needsUpdate = true; //this.material.needsUpdate = true; if(this.sprite.material.map){ this.sprite.material.map.dispose(); } this.sprite.material.map = texture; this.sprite.scale.set(spriteWidth * 0.01 / r, spriteHeight * 0.01 / r, 1.0); } updateTexture(){ let canvas = document.createElement('canvas'); let context = canvas.getContext('2d'); const r = window.devicePixelRatio; context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface; //context["font-weight"] = 100; //语法与 CSS font 属性相同。 //this.text = '啊啊啊啊啊啊fag' let textMaxWidth = 0, infos = []; for (let text of this.text) { let metrics = context.measureText(text); let textWidth = metrics.width; infos.push(metrics); textMaxWidth = Math.max(textMaxWidth, textWidth); } let margin = (this.margin ? new Vector2().copy(this.margin) : new Vector2(this.fontsize, Math.max( this.fontsize*0.4, 10) )).clone().multiplyScalar(r); const lineSpace = (this.fontsize + margin.y) * 0.5; let spriteWidth = 2 * margin.x + textMaxWidth + 2 * (this.rectBorderThick + this.textBorderThick)* r; //还要考虑this.textshadowColor,太麻烦了不写了 let spriteHeight = 2 * margin.y + (this.fontsize + this.textBorderThick*2)* r * this.text.length + 2 * this.rectBorderThick * r + lineSpace * (this.text.length - 1); context.canvas.width = spriteWidth; context.canvas.height = spriteHeight; context.font = this.fontWeight + ' ' + this.fontsize * r + 'px ' + this.fontface; if(spriteWidth>4000){ console.error('spriteWidth',spriteWidth,'spriteHeight',spriteHeight,this.fontsize,r,this.text,margin); } /* let diff = 2//针对英文大部分在baseLine之上所以降低一点(metrics.fontBoundingBoxAscent - metrics.fontBoundingBoxDescent) / 2 context.textBaseline = "middle" */ let expand = Math.max(1, Math.pow(this.fontsize / 16, 1.3)) * r; // 针对英文大部分在baseLine之上所以降低一点,或者可以识别当不包含jgqp时才加这个值 //canvas原点在左上角 context.textBaseline = 'alphabetic'; // "middle" //设置文字基线。当起点y设置为0时,只有该线以下的部分被绘制出来。middle时文字显示一半(但是对该字体所有字的一半,有的字是不一定显示一半的,尤其汉字),alphabetic时是英文字母的那条基线。 // border color context.strokeStyle = 'rgba(' + this.borderColor.r + ',' + this.borderColor.g + ',' + this.borderColor.b + ',' + this.borderColor.a + ')'; let rectBorderThick = this.rectBorderThick * r; context.lineWidth = rectBorderThick; // background color context.fillStyle = 'rgba(' + this.backgroundColor.r + ',' + this.backgroundColor.g + ',' + this.backgroundColor.b + ',' + this.backgroundColor.a + ')'; this.roundRect(context, rectBorderThick / 2 , rectBorderThick / 2, spriteWidth - rectBorderThick, spriteHeight - rectBorderThick, this.borderRadius * r); context.fillStyle = 'rgba(' + this.textColor.r + ',' + this.textColor.g + ',' + this.textColor.b + ',' + this.textColor.a + ')'; let y = margin.y; for (let i = 0; i < this.text.length; i++) { //let actualHeight = metrics.actualBoundingBoxAscent + metrics.actualBoundingBoxDescent // 当前文本字符串在这个字体下用的实际高度 //文字y向距离从textBaseline向上算 let actualBoundingBoxAscent = infos[i].actualBoundingBoxAscent == void 0 ? this.fontsize * r * 0.8 : infos[i].actualBoundingBoxAscent; //有的流览器没有。只能大概给一个 y += actualBoundingBoxAscent + expand + this.textBorderThick; //console.log(actualBoundingBoxAscent) //console.log(this.text, 'y' , y, 'actualBoundingBoxAscent', metrics.actualBoundingBoxAscent,'expand',expand ) let textLeftSpace = (textMaxWidth - infos[i].width) / 2; let x = this.rectBorderThick + margin.x + textLeftSpace; // text color if (this.textBorderThick) { context.strokeStyle = 'rgba(' + this.textBorderColor.r + ',' + this.textBorderColor.g + ',' + this.textBorderColor.b + ',' + this.textBorderColor.a + ')'; context.lineWidth = this.textBorderThick * r; context.strokeText(this.text[i], x, y); } if (this.textshadowColor) { context.shadowOffsetX = 0; context.shadowOffsetY = 0; context.shadowColor = this.textshadowColor; context.shadowBlur = 12 * r; } context.fillText(this.text[i], x, y); y += lineSpace; } let texture = new Texture(canvas); texture.minFilter = LinearFilter; texture.magFilter = LinearFilter; texture.needsUpdate = true; //this.material.needsUpdate = true; if(this.sprite.material.map){ this.sprite.material.map.dispose(); } this.sprite.material.map = texture; this.sprite.scale.set(spriteWidth * 0.01 / r, spriteHeight * 0.01 / r, 1.0); } roundRect(ctx, x, y, w, h, r){ ctx.beginPath(); ctx.moveTo(x + r, y); ctx.lineTo(x + w - r, y); ctx.arcTo(x + w, y, x + w, y + r, r );//圆弧。前四个参数同quadraticCurveTo //ctx.quadraticCurveTo(x + w, y, x + w, y + r); //二次贝塞尔曲线需要两个点。第一个点是用于二次贝塞尔计算中的控制点,第二个点是曲线的结束点。 ctx.lineTo(x + w, y + h - r); ctx.arcTo(x + w, y + h, x + w - r, y + h, r ); ctx.lineTo(x + r, y + h); ctx.arcTo(x, y + h, x, y + h - r, r ); ctx.lineTo(x, y + r); ctx.arcTo(x, y, x + r, y, r ); ctx.closePath(); ctx.fill(); ctx.stroke(); } dispose(){ this.sprite.material.uniforms.map.value.dispose(); this.parent && this.parent.remove(this); this.sprite.dispose(); this.removeAllListeners(); this.dispatchEvent('dispose'); } } /* z | | | | x <-------| 中心为点云position加boudingbox中心 / / y */ var lineLen$1 = 2, stemLen = 4, arrowLen = 2, lineDisToStem = 5; var opacity = 0.5; class Axis extends Object3D {// 坐标轴 constructor () { super(); this.getArrow(); this.createArrows(); //this.position.copy(position) 点云的中心点就是在(0,0,0) //this.scale.set(2,2,2) /* viewer.addEventListener('camera_changed', e => { if(e.viewport.name != 'MainView')return //只调整mainView,否则需要每次渲染前调整。缺点:地图上的大小变来变去 let s = Potree.math.getScaleForConstantSize( {//规定下最小最大像素 width2d:50, camera:e.camera , position:this.position, resolution: e.viewport.resolution//2 }) this.scale.set(s,s,s) }) */ } getArrow(){ var arrowGroup = new Object3D(); var line = LineDraw.createLine([new Vector3, new Vector3(0,0,lineLen$1)]); var stem = new Mesh(new BoxGeometry(0.3, 0.3, stemLen)); stem.position.set(0,0,lineLen$1+lineDisToStem+stemLen/2); var arrow = new Mesh(new CylinderBufferGeometry( 0, 0.6, arrowLen, 12, 1, false ));//radiusTop = 1, radiusBottom = 1, height = 1, radialSegments = 8, heightSegments = 1, openEnded = false, thetaStart = 0, thetaLength = Math.PI * 2 arrow.position.set(0,0,lineLen$1+lineDisToStem+stemLen + arrowLen/2); arrow.rotation.set(Math.PI/2,0,0); arrowGroup.add(stem); arrowGroup.add(line); arrowGroup.add(arrow); this.arrowGroup = arrowGroup; } createArrows(){ var material = new MeshBasicMaterial({color:"#00d7df",side:2,transparent:true,opacity:0.8, depthWrite:false}); ['x','y','z'].forEach((axisText)=>{ let color = new Color().set(Potree.config.axis[axisText].color); var group = this.arrowGroup.clone(); group.children.forEach(e=>{ e.material = e.material.clone(); /* e.material.opacity = opacity e.material.transparent = true */ e.material.color.copy(color); }); var label = this.createLabel(axisText, color); label.position.set(0, 0, lineLen$1 + stemLen + arrowLen + lineDisToStem + 3); group.add(label); if(axisText == 'y'){ group.rotation.x = -Math.PI / 2; }else if(axisText == 'x'){ group.rotation.y = Math.PI / 2; } this.add(group); }); } createLabel(text,color){ let label = new TextSprite$2({ //无法解决 因其祖先有设定quaternion, 无法对着镜头 backgroundColor: {r: 0, g: 0, b: 0, a:0}, textColor: {r: color.r * 255, g: color.g*255, b: color.b*255, a:1}, fontsize:120, //useDepth : true , renderOrder : 5,// pickOrder:5, text, name:'axis' }); label.scale.set(3,3,3); return label } /* createLabel(text,color){ var canvas = document.createElement("canvas") var context = canvas.getContext("2d"); canvas.width = 256, canvas.height = 256; var fontSize = 120 context.fillStyle = color //"#00ffee"; context.font = "normal " + fontSize + "px 微软雅黑" var textWidth = context.measureText(text).width; context.clearRect(0,0,canvas.width,canvas.height); context.fillText(text, (canvas.width - textWidth) / 2 , (canvas.height + fontSize) / 2); var tex = new THREE.Texture(canvas); tex.needsUpdate = true tex.minFilter = THREE.NearestFilter//防止边缘发黑 tex.magFilter = THREE.NearestFilter//防止边缘发黑 var sprite = new THREE.Sprite(new THREE.SpriteMaterial({ map: tex , // depthWrite:false, })) sprite.renderOrder = 1//防止在透明后还是出现白矩形挡住其他mesh sprite.scale.set(3,3,3) return sprite } */ } class Action extends EventDispatcher$1 { constructor (args = {}) { super(); this.icon = args.icon || ''; this.tooltip = args.tooltip; if (args.onclick !== undefined) { this.onclick = args.onclick; } } onclick (event) { } pairWith (object) { } setIcon (newIcon) { let oldIcon = this.icon; if (newIcon === oldIcon) { return; } this.icon = newIcon; this.dispatchEvent({ type: 'icon_changed', action: this, icon: newIcon, oldIcon: oldIcon }); } }; //Potree.Actions = {}; // //Potree.Actions.ToggleAnnotationVisibility = class ToggleAnnotationVisibility extends Potree.Action { // constructor (args = {}) { // super(args); // // this.icon = Potree.resourcePath + '/icons/eye.svg'; // this.showIn = 'sidebar'; // this.tooltip = 'toggle visibility'; // } // // pairWith (annotation) { // if (annotation.visible) { // this.setIcon(Potree.resourcePath + '/icons/eye.svg'); // } else { // this.setIcon(Potree.resourcePath + '/icons/eye_crossed.svg'); // } // // annotation.addEventListener('visibility_changed', e => { // let annotation = e.annotation; // // if (annotation.visible) { // this.setIcon(Potree.resourcePath + '/icons/eye.svg'); // } else { // this.setIcon(Potree.resourcePath + '/icons/eye_crossed.svg'); // } // }); // } // // onclick (event) { // let annotation = event.annotation; // // annotation.visible = !annotation.visible; // // if (annotation.visible) { // this.setIcon(Potree.resourcePath + '/icons/eye.svg'); // } else { // this.setIcon(Potree.resourcePath + '/icons/eye_crossed.svg'); // } // } //}; class Annotation extends EventDispatcher$1 { constructor (args = {}) { super(); this.scene = null; this._title = args.title || 'No Title'; this._description = args.description || ''; this.offset = new Vector3(); this.uuid = MathUtils.generateUUID(); if (!args.position) { this.position = null; } else if (args.position.x != null) { this.position = args.position; } else { this.position = new Vector3(...args.position); } this.cameraPosition = (args.cameraPosition instanceof Array) ? new Vector3().fromArray(args.cameraPosition) : args.cameraPosition; this.cameraTarget = (args.cameraTarget instanceof Array) ? new Vector3().fromArray(args.cameraTarget) : args.cameraTarget; this.radius = args.radius; this.view = args.view || null; this.keepOpen = false; this.descriptionVisible = false; this.showDescription = true; this.actions = args.actions || []; this.isHighlighted = false; this._visible = true; this.__visible = true; this._display = true; this._expand = false; this.collapseThreshold = [args.collapseThreshold, 100].find(e => e !== undefined); this.children = []; this.parent = null; this.boundingBox = new Box3(); let iconClose = exports.resourcePath + '/icons/close.svg'; this.domElement = $(`
${this._description}
`); this.elTitlebar = this.domElement.find('.annotation-titlebar'); this.elTitle = this.elTitlebar.find('.annotation-label'); this.elTitle.append(this._title); this.elDescription = this.domElement.find('.annotation-description'); this.elDescriptionClose = this.elDescription.find('.annotation-description-close'); // this.elDescriptionContent = this.elDescription.find(".annotation-description-content"); this.clickTitle = () => { if(this.hasView()){ this.moveHere(this.scene.getActiveCamera()); } this.dispatchEvent({type: 'click', target: this}); }; this.elTitle.click(this.clickTitle); this.actions = this.actions.map(a => { if (a instanceof Action) { return a; } else { return new Action(a); } }); for (let action of this.actions) { action.pairWith(this); } let actions = this.actions.filter( a => a.showIn === undefined || a.showIn.includes('scene')); for (let action of actions) { let elButton = $(``); this.elTitlebar.append(elButton); elButton.click(() => action.onclick({annotation: this})); } this.elDescriptionClose.hover( e => this.elDescriptionClose.css('opacity', '1'), e => this.elDescriptionClose.css('opacity', '0.5') ); this.elDescriptionClose.click(e => this.setHighlighted(false)); // this.elDescriptionContent.html(this._description); this.domElement.mouseenter(e => this.setHighlighted(true)); this.domElement.mouseleave(e => this.setHighlighted(false)); this.domElement.on('touchstart', e => { this.setHighlighted(!this.isHighlighted); }); this.display = false; //this.display = true; } installHandles(viewer){ if(this.handles !== undefined){ return; } let domElement = $(`
`); let svg = domElement.find("svg")[0]; let elLine = domElement.find("line")[0]; let elStart = domElement.find("circle")[0]; let elEnd = domElement.find("circle")[1]; let setCoordinates = (start, end) => { elStart.setAttribute("cx", `${start.x}`); elStart.setAttribute("cy", `${start.y}`); elEnd.setAttribute("cx", `${end.x}`); elEnd.setAttribute("cy", `${end.y}`); elLine.setAttribute("x1", start.x); elLine.setAttribute("y1", start.y); elLine.setAttribute("x2", end.x); elLine.setAttribute("y2", end.y); let box = svg.getBBox(); svg.setAttribute("width", `${box.width}`); svg.setAttribute("height", `${box.height}`); svg.setAttribute("viewBox", `${box.x} ${box.y} ${box.width} ${box.height}`); let ya = start.y - end.y; let xa = start.x - end.x; if(ya > 0){ start.y = start.y - ya; } if(xa > 0){ start.x = start.x - xa; } domElement.css("left", `${start.x}px`); domElement.css("top", `${start.y}px`); }; $(viewer.renderArea).append(domElement); let annotationStartPos = this.position.clone(); let annotationStartOffset = this.offset.clone(); $(this.domElement).draggable({ start: (event, ui) => { annotationStartPos = this.position.clone(); annotationStartOffset = this.offset.clone(); $(this.domElement).find(".annotation-titlebar").css("pointer-events", "none"); console.log($(this.domElement).find(".annotation-titlebar")); }, stop: () => { $(this.domElement).find(".annotation-titlebar").css("pointer-events", ""); }, drag: (event, ui ) => { let renderAreaWidth = viewer.renderer.getSize(new Vector2()).width; //let renderAreaHeight = viewer.renderer.getSize().height; let diff = { x: ui.originalPosition.left - ui.position.left, y: ui.originalPosition.top - ui.position.top }; let nDiff = { x: -(diff.x / renderAreaWidth) * 2, y: (diff.y / renderAreaWidth) * 2 }; let camera = viewer.scene.getActiveCamera(); let oldScreenPos = new Vector3() .addVectors(annotationStartPos, annotationStartOffset) .project(camera); let newScreenPos = oldScreenPos.clone(); newScreenPos.x += nDiff.x; newScreenPos.y += nDiff.y; let newPos = newScreenPos.clone(); newPos.unproject(camera); let newOffset = new Vector3().subVectors(newPos, this.position); this.offset.copy(newOffset); } }); let updateCallback = () => { let position = this.position; let scene = viewer.scene; const renderAreaSize = viewer.renderer.getSize(new Vector2()); let renderAreaWidth = renderAreaSize.width; let renderAreaHeight = renderAreaSize.height; let start = this.position.clone(); let end = new Vector3().addVectors(this.position, this.offset); let toScreen = (position) => { let camera = scene.getActiveCamera(); let screenPos = new Vector3(); let worldView = new Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse); let ndc = new Vector4(position.x, position.y, position.z, 1.0).applyMatrix4(worldView); // limit w to small positive value, in case position is behind the camera ndc.w = Math.max(ndc.w, 0.1); ndc.divideScalar(ndc.w); screenPos.copy(ndc); screenPos.x = renderAreaWidth * (screenPos.x + 1) / 2; screenPos.y = renderAreaHeight * (1 - (screenPos.y + 1) / 2); return screenPos; }; start = toScreen(start); end = toScreen(end); setCoordinates(start, end); }; viewer.addEventListener("update", updateCallback); this.handles = { domElement: domElement, setCoordinates: setCoordinates, updateCallback: updateCallback }; } removeHandles(viewer){ if(this.handles === undefined){ return; } //$(viewer.renderArea).remove(this.handles.domElement); this.handles.domElement.remove(); viewer.removeEventListener("update", this.handles.updateCallback); delete this.handles; } get visible () { return this._visible; } set visible (value) { if (this._visible === value) { return; } this._visible = value; //this.traverse(node => { // node.display = value; //}); this.dispatchEvent({ type: 'visibility_changed', annotation: this }); } get display () { return this._display; } set display (display) { if (this._display === display) { return; } this._display = display; if (display) { // this.domElement.fadeIn(200); this.domElement.show(); } else { // this.domElement.fadeOut(200); this.domElement.hide(); } } get expand () { return this._expand; } set expand (expand) { if (this._expand === expand) { return; } if (expand) { this.display = false; } else { this.display = true; this.traverseDescendants(node => { node.display = false; }); } this._expand = expand; } get title () { return this._title; } set title (title) { if (this._title === title) { return; } this._title = title; this.elTitle.empty(); this.elTitle.append(this._title); this.dispatchEvent({ type: "annotation_changed", annotation: this, }); } get description () { return this._description; } set description (description) { if (this._description === description) { return; } this._description = description; const elDescriptionContent = this.elDescription.find(".annotation-description-content"); elDescriptionContent.empty(); elDescriptionContent.append(this._description); this.dispatchEvent({ type: "annotation_changed", annotation: this, }); } add (annotation) { if (!this.children.includes(annotation)) { this.children.push(annotation); annotation.parent = this; let descendants = []; annotation.traverse(a => { descendants.push(a); }); for (let descendant of descendants) { let c = this; while (c !== null) { c.dispatchEvent({ 'type': 'annotation_added', 'annotation': descendant }); c = c.parent; } } } } level () { if (this.parent === null) { return 0; } else { return this.parent.level() + 1; } } hasChild(annotation) { return this.children.includes(annotation); } remove (annotation) { if (this.hasChild(annotation)) { annotation.removeAllChildren(); annotation.dispose(); this.children = this.children.filter(e => e !== annotation); annotation.parent = null; } } removeAllChildren() { this.children.forEach((child) => { if (child.children.length > 0) { child.removeAllChildren(); } this.remove(child); }); } updateBounds () { let box = new Box3(); if (this.position) { box.expandByPoint(this.position); } for (let child of this.children) { child.updateBounds(); box.union(child.boundingBox); } this.boundingBox.copy(box); } traverse (handler) { let expand = handler(this); if (expand === undefined || expand === true) { for (let child of this.children) { child.traverse(handler); } } } traverseDescendants (handler) { for (let child of this.children) { child.traverse(handler); } } flatten () { let annotations = []; this.traverse(annotation => { annotations.push(annotation); }); return annotations; } descendants () { let annotations = []; this.traverse(annotation => { if (annotation !== this) { annotations.push(annotation); } }); return annotations; } setHighlighted (highlighted) { if (highlighted) { this.domElement.css('opacity', '0.8'); this.elTitlebar.css('box-shadow', '0 0 5px #fff'); this.domElement.css('z-index', '1000'); if (this._description) { this.descriptionVisible = true; this.elDescription.fadeIn(200); this.elDescription.css('position', 'relative'); } } else { this.domElement.css('opacity', '0.5'); this.elTitlebar.css('box-shadow', ''); this.domElement.css('z-index', '100'); this.descriptionVisible = false; this.elDescription.css('display', 'none'); } this.isHighlighted = highlighted; } hasView () { let hasPosTargetView = this.cameraTarget.x != null; hasPosTargetView = hasPosTargetView && this.cameraPosition.x != null; let hasRadiusView = this.radius !== undefined; let hasView = hasPosTargetView || hasRadiusView; return hasView; }; moveHere (camera) { if (!this.hasView()) { return; } let view = this.scene.view; let animationDuration = 500; let easing = TWEEN.Easing.Quartic.Out; let endTarget; if (this.cameraTarget) { endTarget = this.cameraTarget; } else if (this.position) { endTarget = this.position; } else { endTarget = this.boundingBox.getCenter(new Vector3()); } if (this.cameraPosition) { let endPosition = this.cameraPosition; Utils.moveTo(this.scene, endPosition, endTarget); } else if (this.radius) { let direction = view.direction; let endPosition = endTarget.clone().add(direction.multiplyScalar(-this.radius)); let startRadius = view.radius; let endRadius = this.radius; { // animate camera position let tween = new TWEEN.Tween(view.position).to(endPosition, animationDuration); tween.easing(easing); tween.start(); } { // animate radius let t = {x: 0}; let tween = new TWEEN.Tween(t) .to({x: 1}, animationDuration) .onUpdate(function () { view.radius = this.x * endRadius + (1 - this.x) * startRadius; }); tween.easing(easing); tween.start(); } } }; dispose () { if (this.domElement.parentElement) { this.domElement.parentElement.removeChild(this.domElement); } }; toString () { return 'Annotation: ' + this._title; } }; class Scene$1 extends EventDispatcher$1{//base constructor(){ super(); this.annotations = new Annotation(); this.scene = new Scene(); this.sceneBG = new Scene(); this.scenePointCloud = new Scene(); this.cameraP = new PerspectiveCamera(this.fov, 1, 0.1, 1000*1000); this.cameraO = new OrthographicCamera(-1, 1, 1, -1, 0.1, 1000*1000); this.cameraVR = new PerspectiveCamera(); this.cameraBG = new Camera(); this.cameraScreenSpace = new OrthographicCamera(-1, 1, 1, -1, 0.1, 10); this.cameraMode = CameraMode.PERSPECTIVE; this.overrideCamera = null; this.pointclouds = []; this.measurements = []; this.profiles = []; this.volumes = []; this.polygonClipVolumes = []; this.cameraAnimations = []; this.orientedImages = []; this.images360 = []; this.geopackages = []; this.fpControls = null; this.orbitControls = null; this.earthControls = null; this.geoControls = null; this.deviceControls = null; this.inputHandler = null; this.view = new ExtendView(); this.directionalLight = null; this.initialize(); } estimateHeightAt (position) { let height = null; let fromSpacing = Infinity; for (let pointcloud of this.pointclouds) { if (pointcloud.root.geometryNode === undefined) { continue; } let pHeight = null; let pFromSpacing = Infinity; let lpos = position.clone().sub(pointcloud.position); lpos.z = 0; let ray = new Ray(lpos, new Vector3(0, 0, 1)); let stack = [pointcloud.root]; while (stack.length > 0) { let node = stack.pop(); let box = node.getBoundingBox(); let inside = ray.intersectBox(box); if (!inside) { continue; } let h = node.geometryNode.mean.z + pointcloud.position.z + node.geometryNode.boundingBox.min.z; if (node.geometryNode.spacing <= pFromSpacing) { pHeight = h; pFromSpacing = node.geometryNode.spacing; } for (let index of Object.keys(node.children)) { let child = node.children[index]; if (child.geometryNode) { stack.push(node.children[index]); } } } if (height === null || pFromSpacing < fromSpacing) { height = pHeight; fromSpacing = pFromSpacing; } } return height; } getBoundingBox(pointclouds = this.pointclouds){ let box = new Box3(); this.scenePointCloud.updateMatrixWorld(true); this.referenceFrame.updateMatrixWorld(true); for (let pointcloud of pointclouds) { pointcloud.updateMatrixWorld(true); let pointcloudBox = pointcloud.pcoGeometry.tightBoundingBox ? pointcloud.pcoGeometry.tightBoundingBox : pointcloud.boundingBox; let boxWorld = Utils.computeTransformedBoundingBox(pointcloudBox, pointcloud.matrixWorld); box.union(boxWorld); } return box; } addPointCloud (pointcloud) { this.pointclouds.push(pointcloud); this.scenePointCloud.add(pointcloud); this.dispatchEvent({ type: 'pointcloud_added', pointcloud: pointcloud }); } addVolume (volume) { this.volumes.push(volume); this.dispatchEvent({ 'type': 'volume_added', 'scene': this, 'volume': volume }); viewer.dispatchEvent('content_changed'); } addOrientedImages(images){ this.orientedImages.push(images); this.scene.add(images.node); this.dispatchEvent({ 'type': 'oriented_images_added', 'scene': this, 'images': images }); }; removeOrientedImages(images){ let index = this.orientedImages.indexOf(images); if (index > -1) { this.orientedImages.splice(index, 1); this.dispatchEvent({ 'type': 'oriented_images_removed', 'scene': this, 'images': images }); } }; add360Images(images){ this.images360.push(images); this.scene.add(images.node); this.dispatchEvent({ 'type': '360_images_added', 'scene': this, 'images': images }); } remove360Images(images){ let index = this.images360.indexOf(images); if (index > -1) { this.images360.splice(index, 1); this.dispatchEvent({ 'type': '360_images_removed', 'scene': this, 'images': images }); } } addGeopackage(geopackage){ this.geopackages.push(geopackage); this.scene.add(geopackage.node); this.dispatchEvent({ 'type': 'geopackage_added', 'scene': this, 'geopackage': geopackage }); }; removeGeopackage(geopackage){ let index = this.geopackages.indexOf(geopackage); if (index > -1) { this.geopackages.splice(index, 1); this.dispatchEvent({ 'type': 'geopackage_removed', 'scene': this, 'geopackage': geopackage }); } }; removeVolume (volume) { let index = this.volumes.indexOf(volume); if (index > -1) { this.volumes.splice(index, 1); this.dispatchEvent({ 'type': 'volume_removed', 'scene': this, 'volume': volume }); } viewer.dispatchEvent('content_changed'); }; addCameraAnimation(animation) { this.cameraAnimations.push(animation); this.dispatchEvent({ 'type': 'camera_animation_added', 'scene': this, 'animation': animation }); }; removeCameraAnimation(animation){ let index = this.cameraAnimations.indexOf(volume); if (index > -1) { this.cameraAnimations.splice(index, 1); this.dispatchEvent({ 'type': 'camera_animation_removed', 'scene': this, 'animation': animation }); } }; addPolygonClipVolume(volume){ this.polygonClipVolumes.push(volume); this.dispatchEvent({ "type": "polygon_clip_volume_added", "scene": this, "volume": volume }); }; removePolygonClipVolume(volume){ let index = this.polygonClipVolumes.indexOf(volume); if (index > -1) { this.polygonClipVolumes.splice(index, 1); this.dispatchEvent({ "type": "polygon_clip_volume_removed", "scene": this, "volume": volume }); } }; addMeasurement(measurement){ measurement.lengthUnit = this.lengthUnit; measurement.lengthUnitDisplay = this.lengthUnitDisplay; this.measurements.push(measurement); this.dispatchEvent({ 'type': 'measurement_added', 'scene': this, 'measurement': measurement }); viewer.dispatchEvent('content_changed'); }; removeMeasurement (measurement) { let index = this.measurements.indexOf(measurement); if (index > -1) { this.measurements.splice(index, 1); this.dispatchEvent({ 'type': 'measurement_removed', 'scene': this, 'measurement': measurement }); viewer.dispatchEvent('content_changed'); } } addProfile (profile) { this.profiles.push(profile); this.dispatchEvent({ 'type': 'profile_added', 'scene': this, 'profile': profile }); } removeProfile (profile) { let index = this.profiles.indexOf(profile); if (index > -1) { this.profiles.splice(index, 1); this.dispatchEvent({ 'type': 'profile_removed', 'scene': this, 'profile': profile }); } } removeAllMeasurements () { while (this.measurements.length > 0) { this.removeMeasurement(this.measurements[0]); } while (this.profiles.length > 0) { this.removeProfile(this.profiles[0]); } while (this.volumes.length > 0) { this.removeVolume(this.volumes[0]); } } removeAllClipVolumes(){ let clipVolumes = this.volumes.filter(volume => volume.clip === true); for(let clipVolume of clipVolumes){ this.removeVolume(clipVolume); } while(this.polygonClipVolumes.length > 0){ this.removePolygonClipVolume(this.polygonClipVolumes[0]); } } getActiveCamera() { if(this.overrideCamera){ return this.overrideCamera; } if(this.cameraMode === CameraMode.PERSPECTIVE){ return this.cameraP; }else if(this.cameraMode === CameraMode.ORTHOGRAPHIC){ return this.cameraO; }else if(this.cameraMode === CameraMode.VR){ return this.cameraVR; } return null; } initialize(){ this.referenceFrame = new Object3D(); this.referenceFrame.matrixAutoUpdate = false; this.scenePointCloud.add(this.referenceFrame); this.cameraP.up.set(0, 0, 1); this.cameraP.position.set(1000, 1000, 1000); this.cameraO.up.set(0, 0, 1); this.cameraO.position.set(1000, 1000, 1000); //this.camera.rotation.y = -Math.PI / 4; //this.camera.rotation.x = -Math.PI / 6; this.cameraScreenSpace.lookAt(new Vector3(0, 0, 0), new Vector3(0, 0, -1), new Vector3(0, 1, 0)); this.directionalLight = new DirectionalLight( 0xffffff, 0.5 ); this.directionalLight.position.set( 10, 10, 10 ); this.directionalLight.lookAt( new Vector3(0, 0, 0)); this.scenePointCloud.add( this.directionalLight ); let light = new AmbientLight( 0x555555 ); // soft white light this.scenePointCloud.add( light ); { // background let texture = Utils.createBackgroundTexture(512, 512); texture.minFilter = texture.magFilter = NearestFilter; texture.minFilter = texture.magFilter = LinearFilter; let bg = new Mesh( new PlaneBufferGeometry(2, 2, 1), new MeshBasicMaterial({ map: texture }) ); bg.material.depthTest = false; bg.material.depthWrite = false; this.sceneBG.add(bg); } // { // lights // { // let light = new THREE.DirectionalLight(0xffffff); // light.position.set(10, 10, 1); // light.target.position.set(0, 0, 0); // this.scene.add(light); // } // { // let light = new THREE.DirectionalLight(0xffffff); // light.position.set(-10, 10, 1); // light.target.position.set(0, 0, 0); // this.scene.add(light); // } // { // let light = new THREE.DirectionalLight(0xffffff); // light.position.set(0, -10, 20); // light.target.position.set(0, 0, 0); // this.scene.add(light); // } // } } addAnnotation(position, args = {}){ if(position instanceof Array){ args.position = new Vector3().fromArray(position); } else if (position.x != null) { args.position = position; } let annotation = new Annotation(args); this.annotations.add(annotation); return annotation; } getAnnotations () { return this.annotations; }; removeAnnotation(annotationToRemove) { this.annotations.remove(annotationToRemove); } }; class ExtendScene extends Scene$1{ constructor(){ super(); delete this.sceneBG; this.cameraP = new PerspectiveCamera(this.fov, 1, Potree.config.view.near, Potree.config.view.near); this.cameraO = new OrthographicCamera(-1, 1, 1, -1, Potree.config.view.near, Potree.settings.cameraFar); this.cameraP.limitFar = true;//add this.initializeExtend(); //------------- this.axisArrow = new Axis(); this.scene.add(this.axisArrow); if(!Potree.settings.isDebug && !Potree.settings.showAxis)this.axisArrow.visible = false; Potree.Utils.setObjectLayers(this.axisArrow, 'bothMapAndScene' ); this.tags = new Object3D; this.scene.add(this.tags); } estimateHeightAt (position) { let height = null; let fromSpacing = Infinity; for (let pointcloud of this.pointclouds) { if (pointcloud.root.geometryNode === undefined) { continue; } let pHeight = null; let pFromSpacing = Infinity; let lpos = position.clone().sub(pointcloud.position); lpos.z = 0; let ray = new Ray(lpos, new Vector3(0, 0, 1)); let stack = [pointcloud.root]; while (stack.length > 0) { let node = stack.pop(); let box = node.getBoundingBox(); let inside = ray.intersectBox(box); if (!inside) { continue; } let h = node.geometryNode.mean.z + pointcloud.position.z + node.geometryNode.boundingBox.min.z; if (node.geometryNode.spacing <= pFromSpacing) { pHeight = h; pFromSpacing = node.geometryNode.spacing; } for (let index of Object.keys(node.children)) { let child = node.children[index]; if (child.geometryNode) { stack.push(node.children[index]); } } } if (height === null || pFromSpacing < fromSpacing) { height = pHeight; fromSpacing = pFromSpacing; } } return height; } //add: removePointCloud (pointcloud) { let index = this.pointclouds.indexOf(pointcloud); if(index == -1)return this.pointclouds.splice(index, 1); this.scenePointCloud.remove(pointcloud); pointcloud.panos.forEach(pano=>{ pano.dispose(); }); } removeCameraAnimation(animation){ let index = this.cameraAnimations.indexOf(animation); if (index > -1) { this.cameraAnimations.splice(index, 1); this.dispatchEvent({ 'type': 'camera_animation_removed', 'scene': this, 'animation': animation }); } }; getActiveCamera() { return viewer.mainViewport.camera } initialize(){//不用旧的 因为还没创建完变量 } initializeExtend(){//add 新的initialize this.referenceFrame = new Object3D(); this.referenceFrame.matrixAutoUpdate = false; this.scenePointCloud.add(this.referenceFrame); if(window.axisYup){ }else { this.cameraP.up.set(0, 0, 1); this.cameraO.up.set(0, 0, 1); } this.cameraP.position.set(1000, 1000, 1000); this.cameraO.position.set(1000, 1000, 1000); //this.camera.rotation.y = -Math.PI / 4; //this.camera.rotation.x = -Math.PI / 6; this.cameraScreenSpace.lookAt(new Vector3(0, 0, 0), new Vector3(0, 0, -1), new Vector3(0, 1, 0)); this.directionalLight = new DirectionalLight( 0xffffff, 0.5 ); this.directionalLight.position.set( 10, 10, 10 ); this.directionalLight.lookAt( new Vector3(0, 0, 0)); this.scenePointCloud.add( this.directionalLight ); let light = new AmbientLight( 0x555555 ); // soft white light this.scenePointCloud.add( light ); //add:------给空间模型的box 或其他obj------ let light2 = new AmbientLight( 16777215, 0.5 ); Potree.Utils.setObjectLayers(light2, 'light'/* 'bothMapAndScene' */); this.scene.add(light2); let light3 = new DirectionalLight( 16777215, 0.7); light3.position.set( 10, 10, 10 ); light3.lookAt( new Vector3(0, 0, 0)); Potree.Utils.setObjectLayers(light3, 'light'); this.scene.add(light3); let light4 = new DirectionalLight( 16777215, 0.3); //补光 light4.position.set( -10, -5, -7 ); light4.lookAt( new Vector3(0, 0, 0)); Potree.Utils.setObjectLayers(light4, 'light'); this.scene.add(light4); //-------------------------------------------- { // background let texture = Utils.createBackgroundTexture(512, 512); texture.minFilter = texture.magFilter = NearestFilter; texture.minFilter = texture.magFilter = LinearFilter; let bg = new Mesh( new PlaneBufferGeometry(2, 2, 1), new MeshBasicMaterial({ map: texture }) ); bg.material.depthTest = false; bg.material.depthWrite = false; bg.name = 'bg'; //this.sceneBG.add(bg); this.scene.add(bg); bg.layers.set(Potree.config.renderLayers.bg); } { // background color let bg2 = new Mesh( new PlaneBufferGeometry(2, 2, 1), new MeshBasicMaterial({ transparent : true }) ); bg2.material.depthTest = false; bg2.material.depthWrite = false; bg2.name = 'bg2'; this.scene.add(bg2); bg2.layers.set(Potree.config.renderLayers.bg2); this.bg2 = bg2; } // { // lights // { // let light = new THREE.DirectionalLight(0xffffff); // light.position.set(10, 10, 1); // light.target.position.set(0, 0, 0); // this.scene.add(light); // } // { // let light = new THREE.DirectionalLight(0xffffff); // light.position.set(-10, 10, 1); // light.target.position.set(0, 0, 0); // this.scene.add(light); // } // { // let light = new THREE.DirectionalLight(0xffffff); // light.position.set(0, -10, 20); // light.target.position.set(0, 0, 0); // this.scene.add(light); // } // } } }; KeyCodes.BACKSPACE = 8; //注意,这时候Potree.js中export的内容还不在Potree变量中 var texLoader$1 = new TextureLoader(); texLoader$1.crossOrigin = "anonymous"; {//defines: Potree.defines = {}; Potree.defines.Buttons = {// MouseEvent.buttons //buttons,设置按下了鼠标哪些键,是一个3个比特位的二进制值,默认为0。1表示按下主键(通常是左键),2表示按下次要键(通常是右键),4表示按下辅助键(通常是中间的键)。 NONE:0,//add LEFT: 0b0001, RIGHT: 0b0010, MIDDLE: 0b0100 }; /* 如果访问的是button, 用THREE.MOUSE来判断: button,设置按下了哪一个鼠标按键,默认为0。-1表示没有按键,0表示按下主键(通常是左键),1表示按下辅助键(通常是中间的键),2表示按下次要键(通常是右键) */ Potree.browser = browser; /////////// add ////////////////////////////////// Potree.defines.GLCubeFaces = { GL_TEXTURE_CUBE_MAP_POSITIVE_X: 0, GL_TEXTURE_CUBE_MAP_NEGATIVE_X: 1, GL_TEXTURE_CUBE_MAP_POSITIVE_Y: 2, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: 3, GL_TEXTURE_CUBE_MAP_POSITIVE_Z: 4, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: 5 }; Potree.defines.PanoSizeClass = { BASE: 1, STANDARD: 2, HIGH: 3, ULTRAHIGH: 4 }; Potree.defines.PanoRendererEvents = { PanoRenderComplete: "panorama.render.complete", TileRenderFailure: "panorama.tile.render.failed", TileRenderSuccess: "panorama.tile.render.success", TileUploadAttempted: "panorama.tile.upload.attempted", UploadAttemptedForAllTiles: "panorama.upload.attempted.all.tiles", ZoomLevelRenderStarted: "panorama.zoom.render.started" }; Potree.defines.SceneRendererEvents = { ContextCreated: "scene-renderer-context-created", AfterRender: "after-render", MemoryUsageUpdated: "scene-renderer-memory-usage-updated" }; Potree.defines.TileDownloaderEvents = { TileDownloadSuccess: "tiledownloader.download.success", TileDownloadFailure: "tiledownloader.download.failure", PanoDownloadComplete: "tiledownloader.pano.download.complete" }; Potree.defines.Vectors = { UP: new Vector3(0,1,0), DOWN: new Vector3(0,-1,0), LEFT: new Vector3(-1,0,0), RIGHT: new Vector3(1,0,0), FORWARD: new Vector3(0,0,-1), BACK: new Vector3(0,0,1) }; /* var Vectors2 = {} for(var i in Vectors){ Vectors2[i] = math.convertVector.YupToZup(Vectors[i]) } */ Potree.defines.DownloadStatus = Object.freeze({ None: 0, Queued: 1, ForceQueued: 2, Downloading: 3, Downloaded: 4, DownloadFailed: 5 }); Potree.defines.ModelManagerEvents = { ModelAdded: "model-added", ActiveModelChanged: "active-model-changed" }; Potree.defines.PanoramaEvents = { Enter: 'panorama.enter', Exit: 'panorama.exit', LoadComplete: "panorama.load.complete", LoadFailed: "panorama.load.failed", TileLoaded: "panorama.tile.loaded", VideoRendered: "panorama.video.rendered" }; ClipTask.SHOW_INSIDE_Big = 4; } {//Features let gl_; Features.EXT_DEPTH = { isSupported: function (gl) { gl = gl || gl_; gl_ = gl; if(browser.detectIOS()){ let {major,minor,patch} = browser.iosVersion(); //console.warn('iosVersion',major,minor,patch) if(major == 15 && minor == 4 && patch == 1){ console.warn('检测到是ios15.4.1, 关闭EXT_frag_depth');//该版本ext_depth有问题,导致clear错乱。没有解决办法先关闭。 return false } } return (typeof WebGL2RenderingContext != 'undefined' && gl instanceof WebGL2RenderingContext) || gl.getExtension('EXT_frag_depth'); //shader中的GL_EXT_frag_depth需要判断一下detectIOS吗。。 } }; } Utils.loadSkybox = function(path, oldSky, callback ) { let camera, scene, parent , cameraOrtho; if(!oldSky){ parent = new Object3D("skybox_root"); camera = new PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 100000); cameraOrtho = new OrthographicCamera(-1, 1, 1, -1, Potree.config.view.near, Potree.settings.cameraFar); if(!window.axisYup) camera.up.set(0, 0, 1);//add scene = new Scene(); let skyboxBgWidth = Potree.config.skyboxBgWidth; let skyGeometry = new BoxBufferGeometry(skyboxBgWidth,skyboxBgWidth,skyboxBgWidth); let skybox = new Mesh(skyGeometry, new ShaderMaterial({ vertexShader: Shaders['skybox.vs'], fragmentShader: Shaders['skybox.fs'], side: BackSide, uniforms:{ tDiffuse: { type: "t", value: null }, matrix:{ type: "m4", value: new Matrix4 } }, depthTest:false, depthWrite:false }) ); scene.add(skybox); scene.traverse(n => n.frustumCulled = false); // z up //scene.rotation.x = Math.PI / 2; parent.children.push(camera); camera.parent = parent; }else { camera = oldSky.camera, scene = oldSky.scene; parent = oldSky.parent; cameraOrtho = oldSky.cameraOrtho; } let texture = texLoader$1.load( path, ()=>{ console.log('loadSkybox成功',path); texture.wrapS = RepeatWrapping; texture.flipY = false; texture.magFilter = LinearFilter; texture.minFilter = LinearFilter; scene.children[0].material.uniforms.tDiffuse.value = texture; callback && callback(); viewer.dispatchEvent('content_changed'); },null,(e)=>{//error console.error('loadSkybox失败',path); }); return {camera, scene, parent, cameraOrtho}; }; Utils.getMousePointCloudIntersection = function(viewport, mouse, pointer, camera, viewer, pointclouds, pickParams = {} ) { //getIntersectByDepthTex /* let result = viewer.edlRenderer.depthTexSampler.sample(viewport, mouse)//add if(result != 'unsupport')return result */ if(!pointclouds || pointclouds.length == 0)return //console.log('getMousePointCloudIntersection') let renderer = viewer.renderer; if(viewport){ //转换到类似整个画面时 /*let mouseInViewport = Utils.convertNDCToScreenPosition(pointer, null, viewport.resolution.x, viewport.resolution.y) pickParams.x = mouseInViewport.x //mouse.x / viewport.width; pickParams.y = mouseInViewport.y //renderer.domElement.clientHeight - mouse.y / viewport.height; */ pickParams.x = mouse.x; pickParams.y = viewport.resolution.y - mouse.y; }else { pickParams.x = mouse.x; pickParams.y = renderer.domElement.clientHeight - mouse.y; } //console.log('getMousePointCloudIntersection') /* if(!raycaster){ raycaster = new THREE.Raycaster(); raycaster.setFromCamera(pointer, camera); } */ let raycaster = new Raycaster(); raycaster.setFromCamera(pointer, camera); let ray = raycaster.ray; let selectedPointcloud = null; let closestDistance = Infinity; let closestIntersection = null; let closestPoint = null; //-----------add-------------------- let old_clipBoxes_in = new Map(); let old_clipBoxes_out = new Map(); let old_bigClipInBox = new Map(); let old_highlightBoxes = new Map(); let old_visibleNodes = new Map(); //bigClipInBox 最好也写下 let density; let sizeType; let size = new Map(); let visiMap = new Map(); let needsUpdate = false; if(pickParams.measuring || Potree.settings.displayMode == 'showPanos') { //测量或无深度图时的全景模式提高精准度. (全景模式有深度图时不会执行到这) density = Potree.settings.pointDensity; Potree.settings.pointDensity = 'magnifier'; //加载最高level pointclouds.forEach(e=>{//因为全景模式的pointSizeType是fixed所以要还原下 visiMap.set(e,e.visible); e.visible = Potree.Utils.getObjVisiByReason(e, 'datasetSelection'); //先将隐藏的点云显示 if(!e.visible)return size.set(e, e.temp.pointSize); sizeType = e.material.pointSizeType; e.material.pointSizeType = Potree.config.material.pointSizeType; //e.changePointSize(Potree.config.material.realPointSize*2, true)//更改点云大小到能铺满为止,否则容易识别不到 }); needsUpdate = true; }else { if(viewer.viewports.filter(e=>!e.noPointcloud && e.active).length>1 || pickParams.cameraChanged){//在pick时相机和渲染时不一样的话 viewport.beforeRender && viewport.beforeRender(); needsUpdate = true; //不updatePointClouds的话hover久了会不准 因node是错的 //但依旧需要camera真的移动到那个位置才能加载出点云 } } if(!pickParams.pickClipped){// 无视clipBoxes for(let pointcloud of pointclouds){ old_clipBoxes_in.set(pointcloud, pointcloud.clipBoxes_in); old_clipBoxes_out.set(pointcloud, pointcloud.clipBoxes_out); old_bigClipInBox.set(pointcloud, pointcloud.bigClipInBox); old_highlightBoxes.set(pointcloud, pointcloud.highlightBoxes); pointcloud.material.setClipBoxes(null, [],[],[]); } needsUpdate = true; } if(needsUpdate) { for(let pointcloud of pointclouds){ old_visibleNodes.set(pointcloud, pointcloud.visibleNodes); } if(window.notViewOffset){ Potree.updatePointClouds(pointclouds, camera, viewport.resolution ); }else { //尽量减少点云加载的范围,集中在pick的空间(这部分以外还是会加载一些散点的) let viewWidth = 80; //viewer.magnifier ? viewer.magnifier.viewport.resolution.x : 200 let camera_ = camera.clone(); camera_.setViewOffset( viewport.resolution.x, viewport.resolution.y, pickParams.x-viewWidth/2, (viewport.resolution.y - pickParams.y)-viewWidth/2, viewWidth, viewWidth ); //注意offsetY是从上到下,和一般的不同 Potree.updatePointClouds(pointclouds, camera_, viewport.resolution ); } } //------------------------------------------------ let allPointclouds = []; for(let pointcloud of pointclouds){ let point = pointcloud.pick(viewer, viewport, camera, ray, pickParams ); if(!point){ continue; } allPointclouds.push(pointcloud); let distance = camera.position.distanceTo(point.position); if (distance < closestDistance) { closestDistance = distance; selectedPointcloud = pointcloud; closestIntersection = point.position; closestPoint = point; } } //恢复 if(pickParams.measuring || Potree.settings.displayMode == 'showPanos'){ Potree.settings.pointDensity = density; pointclouds.forEach(e=>{ if(e.visible){ e.material.pointSizeType = sizeType; //e.changePointSize(size.get(e)) } e.visible = visiMap.get(e); }); }else { /* if(viewer.viewports.filter(e=>!e.noPointcloud).length>1){ viewport.afterRender && viewport.afterRender() } */ } if(!pickParams.pickClipped){//add for(let pointcloud of pointclouds){ pointcloud.material.setClipBoxes(old_bigClipInBox.get(pointcloud), old_clipBoxes_in.get(pointcloud), old_clipBoxes_out.get(pointcloud), old_highlightBoxes.get(pointcloud)); } } if(needsUpdate){ for(let pointcloud of pointclouds){ //不恢复的话(尤其cameraChanged时),在下次render前,再次pick可能是错的。表现为多数据集刚开始reticule消失了,直到ifPointBlockedByIntersect停止 pointcloud.visibleNodes = old_visibleNodes.get(pointcloud); } } if (selectedPointcloud) { return { location: closestIntersection, distance: closestDistance, pointcloud: selectedPointcloud, point: closestPoint, pointclouds: allPointclouds, //add normal: closestPoint.normal && new Vector3().fromArray(closestPoint.normal ).applyMatrix4(selectedPointcloud.rotateMatrix)//add }; } else { return null; } }; Utils.pixelsArrayToDataUrl = function(pixels, width, height, compressRatio = 0.7) { let canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; let context = canvas.getContext('2d'); pixels = new pixels.constructor(pixels); /* for (let i = 0; i < pixels.length; i++) { pixels[i * 4 + 3] = 255; } */ // flip vertically let bytesPerLine = width * 4; for(let i = 0; i < parseInt(height / 2); i++){ let j = height - i - 1; let lineI = pixels.slice(i * bytesPerLine, i * bytesPerLine + bytesPerLine); let lineJ = pixels.slice(j * bytesPerLine, j * bytesPerLine + bytesPerLine); pixels.set(lineJ, i * bytesPerLine); pixels.set(lineI, j * bytesPerLine); } let imageData = context.createImageData(width, height); imageData.data.set(pixels); context.putImageData(imageData, 0, 0); let dataURL = canvas.toDataURL(compressRatio); return dataURL; }; Utils.renderTargetToDataUrl = function(renderTarget, width, height, renderer, compressRatio = 0.7){ let pixelCount = width * height; let buffer = new Uint8Array(4 * pixelCount); renderer.readRenderTargetPixels(renderTarget, 0, 0, width, height, buffer); var dataUrl = Utils.pixelsArrayToDataUrl(buffer, width, height, compressRatio); return dataUrl }; Utils.mouseToRay = function(pointer, camera ){ let vector = new Vector3(pointer.x, pointer.y, 1); let origin = new Vector3(pointer.x, pointer.y, -1); //不能用camera.position,在orbitCamera时不准 vector.unproject(camera); origin.unproject(camera); let direction = new Vector3().subVectors(vector, origin).normalize(); let ray = new Ray(origin, direction); return ray; }; Utils.getPos2d = function(point, viewport , dom, renderer ){//获取一个三维坐标对应屏幕中的二维坐标 var pos; if(math.closeTo(viewport.camera.position, point, 1e-5) ){ //和相机位置重合时显示会四处飘,看是要改成一直显示中间还是隐藏? pos = new Vector3(0,0,1.5); //1.5是为了不可见 }else { pos = point.clone().project(viewport.camera); //比之前hotspot的计算方式写得简单 project用于3转2(求法同shader); unproject用于2转3 :new r.Vector3(e.x, e.y, -1).unproject(this.camera); } let size = renderer && renderer.getSize(new Vector2); //如果是渲染到renderTarget上,resolution和dom的大小不一致。如果输出的结果给前端2d用,就使用clinetWidth,如果自己场景用,用renderer.size let w = renderer ? size.x : dom.clientWidth; let h = renderer ? size.y : dom.clientHeight; var x,y,left,top; x = (pos.x + 1) / 2 * w * viewport.width; y = (1 - (pos.y + 1) / 2) * h * viewport.height; left = viewport.left * w; top = (1- viewport.bottom - viewport.height) * h; var inSight = pos.x <= 1 && pos.x >= -1 //是否在屏幕中 && pos.x <= 1 && pos.y >= -1; return { pos: new Vector2(left+x,top+y) ,// 屏幕像素坐标 vector: pos, //(范围 -1 ~ 1) trueSide : pos.z<1, //trueSide为false时,即使在屏幕范围内可见,也是反方向的另一个不可以被渲染的点 参见Tag.update inSight : inSight, //在屏幕范围内可见, posInViewport: new Vector2(x,y), }; }; Utils.screenPass = new function () { this.screenScene = new Scene(); this.screenQuad = new Mesh(new PlaneBufferGeometry(2, 2, 1)); this.screenQuad.material.depthTest = true; this.screenQuad.material.depthWrite = true; this.screenQuad.material.transparent = true; this.screenScene.add(this.screenQuad); this.camera = new Camera(); this.render = function (renderer, material, target, composer) { this.screenQuad.material = material; if (typeof target === 'undefined') { (composer || renderer).render(this.screenScene, this.camera); } else { let oldTarget = renderer.getRenderTarget(); renderer.setRenderTarget(target); //renderer.clear(); //有时候不能clear,如renderBG后再 (composer || renderer).render(this.screenScene, this.camera); renderer.setRenderTarget(oldTarget); } }; }(); //add Utils.computePointcloudsBound = function(pointclouds){ var boundingBox = new Box3(); pointclouds.forEach(pointcloud=>{ pointcloud.updateBound(); boundingBox.union(pointcloud.bound2); }); var boundSize = boundingBox.getSize(new Vector3); var center = boundingBox.getCenter(new Vector3); return {boundSize, center, boundingBox} }; Utils.convertScreenPositionToNDC = function(pointer, mouse, width, height) { return pointer = pointer || new Vector2, pointer.x = mouse.x / width * 2 - 1, pointer.y = 2 * -(mouse.y / height) + 1, pointer }; Utils.convertNDCToScreenPosition = function(pointer, mouse, width, height) { return mouse = mouse || new Vector2, mouse.x = Math.round((pointer.x + 1 ) / 2 * width), mouse.y = Math.round(-(pointer.y - 1 ) / 2 * height), mouse }; Utils.getOrthoCameraMoveVec = function(pointerDelta, camera ){//获取当camera为Ortho型时 屏幕点1 到 屏幕点2 的三维距离 let cameraViewWidth = camera.right / camera.zoom; let cameraViewHeight = camera.top / camera.zoom; let moveVec = new Vector3; moveVec.set( pointerDelta.x * cameraViewWidth , pointerDelta.y * cameraViewHeight , 0).applyQuaternion(camera.quaternion); return moveVec }; Utils.VectorFactory = { fromArray : function(t) { if (t) { if (t.length < 2 || t.length > 3) console.error("Wrong number of ordinates for a point!"); return 3 === t.length ? (new Vector3).fromArray(t) : (new Vector2).fromArray(t) } }, fromArray3 : function(t) { if (t) { if (3 !== t.length) console.error("Wrong number of ordinates for a point!"); return (new Vector3).fromArray(t) } }, fromArray2 : function(t) { if (t) { if (2 !== t.length) console.error("Wrong number of ordinates for a point!"); return (new Vector2).fromArray(t) } }, toString : function(t) { return t.x.toFixed(8) + "," + t.y.toFixed(8) + "," + t.z.toFixed(3) } }; Utils.QuaternionFactory = { rot90 : (new Quaternion).setFromAxisAngle(new Vector3(0,0,1), MathUtils.degToRad(-90)), fromArray : function(t) { if (t) { if (4 !== t.length) console.error("Wrong number of ordinates for a quaternion!"); return new Quaternion(t[1],t[2],t[3],t[0]).multiply(this.rot90) } } , toArray : function(t) { if (t) { var e = t.clone().multiply(a).toArray(); return [e[3], e[0], e[1], e[2]] } } , fromLonLat : function(t) { if (t) return (new Quaternion).setFromEuler(new Euler(t.lon,t.lat,0)) } , toLonLat : function(t) { if (t) { var e = (new Euler).setFromQuaternion(t); return { lon: e.x, lat: e.y } } } }; Utils.datasetPosTransform = function(o={}){ let pointcloud = o.pointcloud || viewer.scene.pointclouds.find(e=>e.dataset_id == o.datasetId); let tranMatrix; if(pointcloud){ if(Potree.settings.editType == 'merge'){ tranMatrix = o.fromDataset ? pointcloud.matrixWorld : new Matrix4().copy(pointcloud.matrixWorld).invert(); }else { tranMatrix = o.fromDataset ? pointcloud.transformMatrix : pointcloud.transformInvMatrix; } }else { if(Potree.settings.intersectOnObjs){ let object = o.object || viewer.objs.children.find(e=>e.dataset_id == o.datasetId); if(object){ tranMatrix = o.fromDataset ? object.matrixWorld : new Matrix4().copy(object.matrixWorld).invert(); } } } if(tranMatrix){ return (new Vector3).copy(o.position).applyMatrix4(tranMatrix) }else { if(o.datasetId != void 0){ console.error(`datasetPosTransform找不到datasetId为${o.datasetId}的数据集或模型,请检查数据, 模型未创建或删除`); //很可能是旧的热点,需要删除 } } }; Utils.datasetRotTransform = function(o={}){ let pointcloud = o.pointcloud || viewer.scene.pointclouds.find(e=>e.dataset_id == o.datasetId); if(pointcloud){ var matrix, newMatrix, result; if(o.rotation){ matrix = new Matrix4().makeRotationFromEuler(o.rotation); }else if(o.quaternion){ matrix = new Matrix4().makeRotationFromQuaternion(o.quaternion); }else if(o.matrix){ matrix = o.matrix.clone(); }else { return } let rotateMatrix = o.fromDataset ? pointcloud.rotateMatrix : pointcloud.rotateInvMatrix; newMatrix = new Matrix4().multiplyMatrices(rotateMatrix, matrix ); if(o.getRotation){ result = new Euler().setFromRotationMatrix(newMatrix); }else if(o.getQuaternion){ result = new Quaternion().setFromRotationMatrix(newMatrix); }else if(o.getMatrix){ result = newMatrix; } return result } }; Utils.isInsideFrustum = function(bounding, camera){// bounding是否在视野范围内有可见部分(视野就是一个锥状box) let frustumMatrix = new Matrix4; frustumMatrix.multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse); let frustum = new Frustum(); frustum.setFromProjectionMatrix(frustumMatrix); if(bounding instanceof Sphere){ return frustum.intersectsSphere(bounding) }else { return frustum.intersectsBox(bounding) } }; Utils.isIntersectBox = function(object, boxMatrix){//object是否有在box中的部分。 object可以是点或者bounding, box原为1*1*1,但可能形变 //let frustum = new THREE.Frustum(); //frustum.setFromProjectionMatrix(boxMatrixInverse) --错 let px = new Vector3(+0.5, 0, 0).applyMatrix4(boxMatrix); let nx = new Vector3(-0.5, 0, 0).applyMatrix4(boxMatrix); let py = new Vector3(0, +0.5, 0).applyMatrix4(boxMatrix); let ny = new Vector3(0, -0.5, 0).applyMatrix4(boxMatrix); let pz = new Vector3(0, 0, +0.5).applyMatrix4(boxMatrix); let nz = new Vector3(0, 0, -0.5).applyMatrix4(boxMatrix); let pxN = new Vector3().subVectors(nx, px).normalize(); let nxN = pxN.clone().multiplyScalar(-1); let pyN = new Vector3().subVectors(ny, py).normalize(); let nyN = pyN.clone().multiplyScalar(-1); let pzN = new Vector3().subVectors(nz, pz).normalize(); let nzN = pzN.clone().multiplyScalar(-1); let pxPlane = new Plane().setFromNormalAndCoplanarPoint(pxN, px); let nxPlane = new Plane().setFromNormalAndCoplanarPoint(nxN, nx); let pyPlane = new Plane().setFromNormalAndCoplanarPoint(pyN, py); let nyPlane = new Plane().setFromNormalAndCoplanarPoint(nyN, ny); let pzPlane = new Plane().setFromNormalAndCoplanarPoint(pzN, pz); let nzPlane = new Plane().setFromNormalAndCoplanarPoint(nzN, nz); let frustum = new Frustum(pxPlane, nxPlane, pyPlane, nyPlane, pzPlane, nzPlane); if(object instanceof Box3){ var boxBound = new Box3( new Vector3(-0.5,-0.5,-0.5), new Vector3(0.5,0.5,0.5), ).applyMatrix4(boxMatrix); //large boundingbox if(!object.intersectsBox(boxBound))return return frustum.intersectsBox(object) //根据该函数, 若存在某个plane在box上的对应点都在plane背面,则不相交. 可得知在box构成的frustum倾斜时不准确,不相交也判断为相交,甚至不如bound相交准确。所以前面加步骤排除下,但仍不完全准确。(可在裁剪中将box放置到数据集上方旋转下校验) }else if(object instanceof Array){//点合集, 只能粗略计算下 let sphere = new Sphere(); sphere.setFromPoints(object); return this.isIntersectBox(sphere, boxMatrix) }else if(object instanceof Sphere){ return frustum.intersectsSphere(object) }else if(object instanceof Vector3){ return frustum.containsPoint(object) }else if(object instanceof Matrix4){//第一个参数如果和第二个参数一样都是box的worldMatrix } /* containsPoint: ƒ containsPoint( point ) intersectsBox: ƒ intersectsBox( box ) intersectsObject: ƒ intersectsObject( object )//geo intersectsSphere: ƒ intersectsSphere( sphere ) intersectsSprite: ƒ intersectsSprite( sprite ) */ }; Utils.getIntersect = function (camera, meshes, pointer, raycaster) { //获取鼠标和meshes交点 if(!raycaster){//getMouseIntersect camera.updateMatrixWorld(); raycaster = new Raycaster(); var origin = new Vector3(pointer.x, pointer.y, -1).unproject(camera), end = new Vector3(pointer.x, pointer.y, 1).unproject(camera); var dir = end.sub(origin).normalize(); raycaster.set(origin, dir); } meshes.forEach(e=>{ raycaster.layers.enable(math.getBaseLog(2,e.layers.mask)); }); var n = raycaster.intersectObjects(meshes); if (0 === n.length) return null return n[0] }; Utils.addOrRemoveDefine = function(material, defineName, type, value=''){ let defines = material.defines; if(type == 'add'){ if(defines[defineName] != void 0 && defines[defineName] == value)return defines[defineName] = value; }else { if(defines[defineName] != void 0)return; delete defines[defineName]; } material.needsUpdate = true; }; Utils.makeTexDontResize = function(map){//避免贴图因非2的次方而缩小。小心使用 if(!map || !map.image){ return console.log('!map || !map.image', map, map&&map.image) } if(MathUtils.isPowerOfTwo(map.image.width ) && MathUtils.isPowerOfTwo(map.image.height ))return map.wrapS = map.wrapT = ClampToEdgeWrapping; //原默认 RepeatWrapping map.minFilter = LinearFilter; // or THREE.NearestFilter 原默认 LinearMipmapLinearFilter map.generateMipmaps = false; map.needsUpdate = true; }; Utils.updateVisible = function(object, reason, ifShow, level=0, type){//当所有加入的条件都不为false时才显示. reason='force'一般是强制、临时的 if(!object.unvisibleReasons) object.unvisibleReasons = []; //如果length>0代表不可见 if(!object.visibleReasons) object.visibleReasons = []; //在同级时,优先可见 var update = function(){ //先按从高到低的level排列 object.unvisibleReasons = object.unvisibleReasons.sort((a,b)=>b.level-a.level); object.visibleReasons = object.visibleReasons.sort((a,b)=>b.level-a.level); var maxVisiLevel = object.visibleReasons[0] ? object.visibleReasons[0].level : -1; var maxunVisiLevel = object.unvisibleReasons[0] ? object.unvisibleReasons[0].level : -1; var shouldVisi = maxVisiLevel >= maxunVisiLevel; var visiBefore = object.visible; if(visiBefore != shouldVisi){ object.visible = shouldVisi; object.dispatchEvent({ type: 'isVisible', visible: shouldVisi, reason, }); } }; if(ifShow){ var index = object.unvisibleReasons.findIndex(e=>e.reason == reason); if(index > -1){ type = 'cancel'; object.unvisibleReasons.splice(index, 1); } if(type == 'add' ){ if(!object.visibleReasons.some(e=>e.reason == reason)){ object.visibleReasons.push({reason,level}); } } }else { var index = object.visibleReasons.findIndex(e=>e.reason == reason); if(index > -1){ type = 'cancel'; object.visibleReasons.splice(index, 1); } if(type != 'cancel' ){ if(!object.unvisibleReasons.some(e=>e.reason == reason)){ object.unvisibleReasons.push({reason,level}); } } } update(); }; /* 复杂案例: 如果物体默认隐藏, 当符合任何一个其他条件时可见,则可: Potree.Utils.updateVisible(this, "default", false, 0 ) //默认隐藏 Potree.Utils.updateVisible(this, 条件名, ifShow, 1, ifShow?'add':'cancel' ) //其他的条件 */ Utils.getObjVisiByReason = function(object,reason){//获取在某条件下是否可见. 注: 用户在数据集选择可不可见为"datasetSelection" if(object.visible)return true else { return !object.unvisibleReasons || !object.unvisibleReasons.some(e=>e.reason == reason) } }; Utils.setCameraLayers = function(camera, enableLayers, extraEnableLayers=[]){//add camera.layers.disableAll(); enableLayers.concat(extraEnableLayers).forEach(e=>{ let layer = Potree.config.renderLayers[e]; if(layer == void 0){ console.error('setCameraLayer没找到layer!'); return } camera.layers.enable(layer); }); }; Utils.setObjectLayers = function(object, layerName){//add let layer = Potree.config.renderLayers[layerName]; if(layer == void 0){ console.error('setObjectLayers没找到layer!',layerName); return } object.traverse(e=>{ e.layers.set(layer); }); }; Utils.imgAddText = async (img, text, labelInfo)=>{ let label = new Potree.TextSprite(Object.assign({//如果直接在canvas里写字,要另外写很多和canvas.drawText有关的,所以还是借助textSprite吧 backgroundColor: { r: 0, g: 0, b: 0, a: 0 }, textColor: { r: 255, g: 255, b: 255, a: 1 }, margin:{x:3,y:3}, renderOrder: 50, fontsize : 20, text, },labelInfo)); let labelImg = new Image; labelImg.src = label.sprite.material.map.image.toDataURL('image/png'); return new Promise((resolve,reject)=>{ labelImg.onload = ()=>{ if(labelInfo.horizonCenter){//水平居中(对img来说) labelInfo.leftRatioToImg = 0.5 - (labelImg.width / img.width)/2; } let result = Common.imgAddLabel(img,labelImg,labelInfo); label.dispose(); resolve(result); }; }) }; Utils.combineImgs = async (imgs, compressRatio, width, height)=>{//拼合图片,顺序从上到下从左到右, 每张图大小不一定一致,但同列的宽一致,同行宽一致 return new Promise((resolve,reject)=>{ let item = imgs[0][0]; let wc=imgs.length, hc=imgs[0].length, loadCount = 0, amount = wc * hc; width = width || imgs.reduce((w,c)=>w + c[0].width, 0), //相加得到的可能比想要得到的大几个像素,之后会重叠 height = height || imgs[0].reduce((w,c)=>w + c.height, 0); let canvas = document.createElement('canvas'); canvas.width = width; canvas.height = height; let context = canvas.getContext('2d'); for(let i=0;i{ loadCount++; context.drawImage(img, img.index.i * img.width, img.index.j * img.height, img.width, img.height); if(loadCount == amount){ var dataUrl = canvas.toDataURL('image/png',compressRatio); context.clearRect(0,0,width,height); resolve(dataUrl); } }; } } }) }; BinaryLoader.prototype.load = function(node, callback){//解析点云 if (node.loaded) { return; } let url = node.getURL(); if (this.version.equalOrHigher('1.4')) { url += '.bin'; } url += '?m='+node.pcoGeometry.timeStamp; //add let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if((xhr.status === 200 || xhr.status === 0) && xhr.response !== null){ let buffer = xhr.response; this.parse(node, buffer, callback); } else { node.loadFailed = 'status:'+xhr.status+",url:"+url; Potree.numNodesLoading--; //console.error(`Failed to load file! HTTP status: ${xhr.status}, file: ${url}`); throw new Error(`Failed to load file! HTTP status: ${xhr.status}, file: ${url}`); } } }; try { xhr.send(null); } catch (e) { node.loadFailed = 'catchError'; Potree.numNodesLoading--; console.error('加载点云node出错 ', url, e ); } }; PointAttribute.RGBA_PACKED = new PointAttribute("rgba", PointAttributeTypes.DATA_TYPE_INT8, 4); PointAttribute.COLOR_PACKED = PointAttribute.RGBA_PACKED; PointAttribute.INTENSITY = new PointAttribute("intensity", PointAttributeTypes.DATA_TYPE_UINT16, 1); PointAttribute.CLASSIFICATION = new PointAttribute("classification", PointAttributeTypes.DATA_TYPE_UINT8, 1); PointAttribute.GPS_TIME = new PointAttribute("gps-time", PointAttributeTypes.DATA_TYPE_DOUBLE, 1); ProfileWindow.prototype.initTHREE = function(){ this.renderer = new WebGLRenderer({alpha: true, premultipliedAlpha: false}); this.renderer.setClearColor(0x000000, 0); this.renderer.setSize(10, 10); this.renderer.autoClear = false; this.renderArea.append($(this.renderer.domElement)); this.renderer.domElement.tabIndex = '2222'; $(this.renderer.domElement).css('width', '100%'); $(this.renderer.domElement).css('height', '100%'); { let gl = this.renderer.getContext(); if(gl.createVertexArray == null){ let extVAO = gl.getExtension('OES_vertex_array_object'); if(!extVAO){ throw new Error("OES_vertex_array_object extension not supported"); } gl.createVertexArray = extVAO.createVertexArrayOES.bind(extVAO); gl.bindVertexArray = extVAO.bindVertexArrayOES.bind(extVAO); } } this.camera = new OrthographicCamera(-1000, 1000, 1000, -1000, -1000, 1000); this.camera.up.set(0, 0, 1); this.camera.rotation.order = "ZXY"; this.camera.rotation.x = Math.PI / 2.0; this.scene = new Scene(); this.profileScene = new Scene(); let sg = new SphereGeometry(1, 16, 16); let sm = new MeshNormalMaterial(); this.pickSphere = new Mesh(sg, sm); this.scene.add(this.pickSphere); this.viewerPickSphere = new Mesh(sg, sm); }; //Potree_update_visibility Potree.updatePointClouds = function(pointclouds,camera, areaSize ){ viewer.addTimeMark('updateClouds','start'); for (let pointcloud of pointclouds) { let start = performance.now(); for (let profileRequest of pointcloud.profileRequests) { profileRequest.update(); let duration = performance.now() - start; if(duration > 5){ break; } } let duration = performance.now() - start; } let result = Potree.updateVisibility(pointclouds, camera, areaSize ); for (let pointcloud of pointclouds) { //pointcloud.updateMaterial(pointcloud.material, pointcloud.visibleNodes, camera, renderer);//转移到渲染时 pointcloud.updateVisibleBounds(); } Potree.lru.freeMemory();//即Potree.lru 能看到所有在加载的node viewer.addTimeMark('updateClouds','end'); return result; }; Potree.updateVisibilityStructures = function(pointclouds, camera, areaSize) { let frustums = {}; let camObjPositions = {}; let camObjDirs = {}; //add let priorityQueue = new BinaryHeap(function (x) { return -x.weight /* 1 / x.weight; */ });//二叉堆。 改,之前的weight不支持负数 viewer.addTimeMark('visiStructure','start'); //camera.updateMatrixWorld(); let viewI = camera.matrixWorldInverse; let proj = camera.projectionMatrix; let view = camera.matrixWorld; let projViewI = new Matrix4().multiply(proj).multiply(viewI); /* let list = pointclouds // stopWhenAllUsed = !viewer.lastFrameChanged let min = 5, max = Math.max(20 , Math.round(list.length / 10 )) let result = Common.batchHandling.getSlice('pcGetFrustum', list, { min,max, durBound1: 3, durBound2: 10} ) //iphonex稳定后大概在7-10。 */ for (let i = 0; i < pointclouds.length; i++) { let pointcloud = pointclouds[i]; if (!pointcloud.initialized()) { continue; } /* let info = history.get(pointcloud) if() */ pointcloud.numVisibleNodes = 0; pointcloud.numVisiblePoints = 0; pointcloud.deepestVisibleLevel = 0; pointcloud.visibleNodes = []; pointcloud.visibleGeometry = []; // 因漫游模式而隐藏的话 依旧需要加入visibleNodes,因为pick需要 /* if (pointcloud.visible && pointcloud.root !== null) { priorityQueue.push({pointcloud: i, node: pointcloud.root, weight: Number.MAX_VALUE}); } */ if (pointcloud.visible || !pointcloud.hasDepthTex && pointcloud.unvisibleReasons && pointcloud.unvisibleReasons.length == 1 && pointcloud.unvisibleReasons[0].reason == 'displayMode' && pointcloud.root !== null) {//改 visible -> priorityQueue.push({pointcloud: i, node: pointcloud.root, weight: Number.MAX_VALUE}); }else { continue } // frustum in object space let frustum = new Frustum(); let world = pointcloud.matrixWorld; // use close near plane for frustum intersection /* let frustumCam = camera.clone(); frustumCam.zoom = camera.zoom //add frustumCam.near = Math.min(camera.near, 0.1); frustumCam.updateProjectionMatrix(); */ //----没用到frustumCam,删了 let fm = new Matrix4().multiply(projViewI).multiply(world); frustum.setFromProjectionMatrix(fm); frustums[i] = frustum; //frustums.push(frustum); // camera position in object space let worldI = pointcloud.matrixWorldInverse; let camMatrixObject = new Matrix4().multiply(worldI).multiply(view);//假设点云无变换的话,相机相对于点云的变换矩阵 let camObjPos = new Vector3().setFromMatrixPosition(camMatrixObject); camObjPositions[i] = camObjPos;//camObjPositions.push(camObjPos); let quaternion = new Quaternion().setFromRotationMatrix(camMatrixObject); let camDir = (new Vector3(0,0,-1)).applyQuaternion(quaternion); camObjDirs[i] = camDir; // hide all previously visible nodes // if(pointcloud.root instanceof PointCloudOctreeNode){ // pointcloud.hideDescendants(pointcloud.root.sceneNode); // } if (pointcloud.root.isTreeNode()) { pointcloud.hideDescendants(pointcloud.root.sceneNode); } for (let j = 0; j < pointcloud.boundingBoxNodes.length; j++) { pointcloud.boundingBoxNodes[j].visible = false; } } viewer.addTimeMark('visiStructure','end'); return { 'frustums': frustums, 'camObjPositions': camObjPositions, 'priorityQueue': priorityQueue, camObjDirs }; }; Potree.updateVisibility = function(pointclouds, camera, areaSize){ let numVisibleNodes = 0; let numVisiblePoints = 0; let numVisiblePointsInPointclouds = new Map(pointclouds.map(pc => [pc, 0])); let visibleNodes = []; let visibleGeometry = []; let unloadedGeometry = []; let lowestSpacing = Infinity; // calculate object space frustum and cam pos and setup priority queue let s = Potree.updateVisibilityStructures(pointclouds, camera, areaSize);//得到相机可见范围 let frustums = s.frustums; let camObjPositions = s.camObjPositions; let priorityQueue = s.priorityQueue; let camObjDirs = s.camObjDirs; let loadedToGPUThisFrame = 0; let domWidth = areaSize.x; //renderer.domElement.clientWidth; let domHeight = areaSize.y;//renderer.domElement.clientHeight; let fov = (camera.fov * Math.PI) / 180; let slope = Math.tan(fov / 2); let projFactor0 = (0.5 * domHeight) / slope ; // check if pointcloud has been transformed // some code will only be executed if changes have been detected if(!Potree._pointcloudTransformVersion){ Potree._pointcloudTransformVersion = new Map(); } let pointcloudTransformVersion = Potree._pointcloudTransformVersion; for(let pointcloud of pointclouds){ if(pointcloud.hasDepthTex ? !pointcloud.visible : !Potree.Utils.getObjVisiByReason(pointcloud, 'datasetSelection')){//改 visible -> continue; } //if(!pointcloud.visible) continue pointcloud.updateMatrixWorld(); if(!pointcloudTransformVersion.has(pointcloud)){ pointcloudTransformVersion.set(pointcloud, {number: 0, transform: pointcloud.matrixWorld.clone()}); }else { let version = pointcloudTransformVersion.get(pointcloud); if(!version.transform.equals(pointcloud.matrixWorld)){ version.number++; version.transform.copy(pointcloud.matrixWorld); pointcloud.dispatchEvent({ type: "transformation_changed", target: pointcloud }); } } } while (priorityQueue.size() > 0) { let element = priorityQueue.pop(); //取出权重最大的一个 let node = element.node; let parent = element.parent; let pointcloud = pointclouds[element.pointcloud]; // { // restrict to certain nodes for debugging // let allowedNodes = ["r", "r0", "r4"]; // if(!allowedNodes.includes(node.name)){ // continue; // } // } let box = node.getBoundingBox(); let frustum = frustums[element.pointcloud]; let camObjPos = camObjPositions[element.pointcloud]; if(!frustum) continue //add let camObjDir = camObjDirs[element.pointcloud]; let insideFrustum = frustum.intersectsBox(box); let maxLevel = pointcloud.maxLevel == void 0 ? Infinity : pointcloud.maxLevel; let minLevel = pointcloud.minLevel == void 0 ? 0 : pointcloud.minLevel; //add let level = node.getLevel(); let visible = insideFrustum; visible = visible && !(numVisiblePoints + node.getNumPoints() > Potree.pointBudget); visible = visible && !(numVisiblePointsInPointclouds.get(pointcloud) + node.getNumPoints() > pointcloud.pointBudget); // pointcloud.pointBudget一直是Infinity visible = visible && level <= maxLevel /* && level >= minLevel */ ; //< 改为 <= //visible = visible || node.getLevel() <= 2; let pcWorldInverse = pointcloud.matrixWorld.clone().invert(); /* let m = pcWorldInverse.elements let pcWorldInvM3 = new THREE.Matrix3().set(m[0],m[4],m[12],m[1],m[5],m[13],m[3],m[7],m[15]) //去掉z的 */ //pointcloud.pcMatrix3 = new THREE.Matrix3().set(m[0],m[4],m[12],m[1],m[5],m[13],m[3],m[7],m[15]) //去掉z的 let intersectBox = (clipBox)=>{ let toPCObject = pcWorldInverse.clone().multiply(clipBox.box.matrixWorld); //box乘上点云逆矩阵 return Potree.Utils.isIntersectBox(box, toPCObject) }; //改 总共两种box : 可见和不可见(都是并集) let clipBoxes_in = pointcloud.material.clipBoxes_in; let clipBoxes_out = pointcloud.material.clipBoxes_out; let bigClipInBox = pointcloud.material.bigClipInBox; if(visible && bigClipInBox){//不在剪裁下载的框内 if(!intersectBox(bigClipInBox)){ visible = false; } } if(visible && clipBoxes_in.length > 0){//当有可见box时,需要在任一可见box内才可见 let visi = false; for(let i = 0, length=clipBoxes_in.length; i < length; i++){ if(intersectBox(clipBoxes_in[i])){ visi = true; break; } } if(!visi){ visible = false; } } //outside不做处理。因为node必须完全在clipBox内才能完全隐藏,而这里的intersect只能识别出部分在clipBox内。因而只能说明不在任意一个box内绝对可见,没有意义,这里需要找出不可见的。 if(visible){ let prism = pointcloud.material.activeAttributeName == 'prismHeight' && pointcloud.material.prisms && pointcloud.material.prisms.find(e=>e.computing); if(prism){ let bound = box.clone().applyMatrix4(pointcloud.matrixWorld); if(bound.intersectsBox(prism.prismBound)){ /* //node box是否包含points中的一个点 let box2 = new THREE.Box2().copy(box) let points2d = prisms.points.map(e=>new THREE.Vector2().copy(e).applyMatrix3(pcWorldInvM3)) let intersect = points2d.some(e=>{ return box2.containsPoint(e) }) if(!intersect){ //或者多边形中是否包含node box中的一个点 intersect = [ new THREE.Vector2(box.min.x, box.min.y), new THREE.Vector2(box.max.x, box.max.y), new THREE.Vector2(box.min.x, box.max.y), new THREE.Vector2(box.max.x, box.min.y), ].some(e=>{ if(math.isPointInArea(points2d, null, e) ){ return true } }) //z是不是在外层已经判断好了? if(!intersect){ visible = false } } */ //会有两个互不包含点但是交叉了的情况,所以就不仔细判断了(如横竖两个矩形构成十字架) }else visible = false; } } if (node.spacing) { lowestSpacing = Math.min(lowestSpacing, node.spacing); } else if (node.geometryNode && node.geometryNode.spacing) { lowestSpacing = Math.min(lowestSpacing, node.geometryNode.spacing); } if (numVisiblePoints + node.getNumPoints() > Potree.pointBudget) { viewer.dispatchEvent({type:'overPointBudget', restQueueSize: priorityQueue.size(), numVisiblePoints} ); break; } if (!visible) { continue; } // TODO: not used, same as the declaration? // numVisibleNodes++; numVisiblePoints += node.getNumPoints(); let numVisiblePointsInPointcloud = numVisiblePointsInPointclouds.get(pointcloud); numVisiblePointsInPointclouds.set(pointcloud, numVisiblePointsInPointcloud + node.getNumPoints()); pointcloud.numVisibleNodes++; pointcloud.numVisiblePoints += node.getNumPoints(); if (node.isGeometryNode() && (!parent || parent.isTreeNode())) { if (node.isLoaded() && loadedToGPUThisFrame < 2) { node = pointcloud.toTreeNode(node, parent); loadedToGPUThisFrame++; } else { //console.log('unloadedGeometry',node) unloadedGeometry.push({pointcloud,node}); //加载点云。虽然还没加载,但也计入了visibleNodes,只是无children,numPoints=0 visibleGeometry.push(node); } } if (node.isTreeNode()) { Potree.lru.touch(node.geometryNode);//在缓存中计入点云 node.sceneNode.visible = true; node.sceneNode.material = pointcloud.material; /* level >= minLevel && */visibleNodes.push(node); /* level >= minLevel && */pointcloud.visibleNodes.push(node); if(node._transformVersion === undefined){ node._transformVersion = -1; } let transformVersion = pointcloudTransformVersion.get(pointcloud); if(node._transformVersion !== transformVersion.number){ node.sceneNode.updateMatrix(); //node.sceneNode.matrixWorld.multiplyMatrices(pointcloud.matrixWorld, node.sceneNode.matrix); node.sceneNode.matrixWorld.multiplyMatrices(pointcloud.matrixWorld, node.sceneNode.matrix); node._transformVersion = transformVersion.number; } if (pointcloud.showBoundingBox && !node.boundingBoxNode && node.getBoundingBox) { let colorHue = level / (maxLevel+1); let s = 0.1 + level / (maxLevel+1); let color = (new Color()).setHSL(colorHue, s, s); let boxHelper = new Box3Helper$1(node.getBoundingBox(),color); boxHelper.matrixAutoUpdate = false; pointcloud.boundingBoxNodes.push(boxHelper); node.boundingBoxNode = boxHelper; node.boundingBoxNode.matrix.copy(pointcloud.matrixWorld); } else if (pointcloud.showBoundingBox) { node.boundingBoxNode.visible = true; node.boundingBoxNode.matrix.copy(pointcloud.matrixWorld); } else if (!pointcloud.showBoundingBox && node.boundingBoxNode) { node.boundingBoxNode.visible = false; } // if(node.boundingBoxNode !== undefined && exports.debug.allowedNodes !== undefined){ // if(!exports.debug.allowedNodes.includes(node.name)){ // node.boundingBoxNode.visible = false; // } // } } // add child nodes to priorityQueue 由近及远、由大及小逐渐加载 let children = node.getChildren(); for (let i = 0; i < children.length; i++) { let child = children[i]; let weight = 0; if(camera.isPerspectiveCamera){ let sphere = child.getBoundingSphere(); let center = sphere.center; let dd = sphere.center.distanceToSquared(camObjPos); let addPow = 0.2;//viewer.mainViewport.view.isFlying() ? 0 : 0.5 //0-0.5,正常原本是0. 数字越大近处加载越快。但会造成远处加载慢甚至因pointBudge限制不加载。 isFlying:漫游时需要尽量加载一下远处的点云 //addPow *= window.devicePixelRatio //devicePixelRatio高的手机需要优先加载最近的高级点云,减少远处的中高级点云。 let distance = Math.pow(dd,0.5+addPow);//Math.sqrt(dd); //提高距离权重,为了提高近处加载速度。 某些场景近处加载慢优化明显,如SS-t-cqCAL6rJ5i //let attenuateDis = 10;//add let radius = sphere.radius; let projFactor = projFactor0 / distance; let screenPixelRadius = radius * projFactor; /* if(distance > attenuateDis){ screenPixelRadius -= (distance - attenuateDis) * Math.sqrt(radius) * projFactor0 * 0.002 } */ //screenPixelRadius 和 domHeight 成正比,所以手机横屏后screenPixelRadius会变小。这是正常的,因为vhov不变,相同物体高度在横屏后高度变小,所需要的密度不需要那么高了。但hfov横屏后扩大,所以可见的node范围变大,又增加了一些可见node;只是总体的可见node还是减少了。 //使用hfov和domWidth计算结果相同。 if(screenPixelRadius < pointcloud.minimumNodePixelSize / Math.pow(dd,addPow)){ //理论上因手机像素小,更不容易堆叠铺满,minimumNodePixelSize应该除以window.deviceRatio 但会造成加载过多,而内存小 continue; } weight = screenPixelRadius; if( !sphere.containsPoint(camObjPos) ){ //add 优先加载屏幕中央的点云(手机端缩小离远效果明显,不会那么稀疏) let dir = new Vector3().subVectors(center, camObjPos).normalize(); let cos = 1+dir.dot(camObjDir); //0-2 weight *= cos/2;//Math.pow(cos,0.5) //幂越高,旁边的容易加载不到,出现缺块 如SS-t-7DUfWAUZ3V } if(distance - radius < 0){ weight = Number.MAX_VALUE; } //如果能得到每个方向上的密度,也就是node数量,密度大的远处少加载,因为被遮挡了显示也没有意义,就好了。 } else { // TODO ortho visibility //let bb = child.getBoundingBox(); let sphere = child.getBoundingSphere(); //let diagonal = bb.max.clone().sub(bb.min).length(); const reduce = 0; //0-0.5,正常原本是0. if( sphere.radius * /* Math.pow( */camera.zoom/* ,1-reduce) */ < pointcloud.minimumNodePixelSize ){ continue; } let distance = sphere.center.distanceToSquared(camObjPos); //先加载中间然后四周 weight = sphere.radius / distance; /* let vec = new THREE.Vector3().subVectors(sphere.center, camObjPos) let disOnCamDir = vec.dot(camObjDir) let vecOnCamDir = camObjDir.clone().multiplyScalar(disOnCamDir) let vecSide = new THREE.Vector3().subVectors(vec, vecOnCamDir) //在屏幕上从中心到该node的向量 let disSide = vecSide.length() //weight = sphere.radius / disSide * camera.zoom - disOnCamDir * 2; //如果用除的,ortho的camera离远了的话dis的影响就小了 weight = sphere.radius / ( disSide * 0.1 + disOnCamDir * 14 ) */ //weight = diagonal; } priorityQueue.push({pointcloud: element.pointcloud, node: child, parent: node, weight: weight}); //貌似好像二叉堆中子节点和父节点没什么关系,就只是为了方便排序层层遍历 } //手机上像素点更小,所以远处感觉会更稀疏 }// end priority queue loop { // update DEM 这是什么 let maxDEMLevel = 4; let candidates = pointclouds.filter(p => (p.generateDEM && p.dem instanceof Potree.DEM)); for (let pointcloud of candidates) { let updatingNodes = pointcloud.visibleNodes.filter(n => n.getLevel() <= maxDEMLevel); pointcloud.dem.update(updatingNodes); } } unloadedGeometry = unloadedGeometry.filter(e=>!e.loadFailed); //过滤加载失败的,否则有失败的就无法发送加载完成 if(unloadedGeometry.length){//加载点云 let maxNodesLoading = Common.getBestCount('unloadedGeometry', viewer.lastFrameChanged?1:3, 6, 4, 15 /* , true */ );//dur在iphoneX中静止有7,pc是2 //!lastFrameChanged静止时加速下载 //THREE.Math.clamp(Math.round(9 - dur), 1, 6 ) //console.log('unloadedGeometry', unloadedGeometry.length) //主要在手机端有效果。不改之前在展示的点云较多时前进会卡。 for (let i = 0; i < Math.min(maxNodesLoading, unloadedGeometry.length); i++) { unloadedGeometry[i].node.load(unloadedGeometry[i].pointcloud.pcoGeometry); } if(!Potree.pointsLoading){ Potree.pointsLoading = true; //console.log('startLoad') viewer.dispatchEvent('startLoadPoints'); } }else { if(Potree.pointsLoading){ Potree.pointsLoading = false; //console.log('load done!') setTimeout(()=>{ Potree.pointsLoading || viewer.dispatchEvent('pointsLoaded'); },document.hidden ? 3000 : 50); //hidden时可能好几秒才更新一次,所以这个并不准 } } Potree.unloadedGeometry = unloadedGeometry; //add: Potree.numVisiblePoints = numVisiblePoints; return { visibleNodes: visibleNodes, numVisiblePoints: numVisiblePoints, lowestSpacing: lowestSpacing }; }; Potree.numVisiblePoints = 0; /* note: 缓存中的点数 Potree.lru.numPoints 一般会 大于 每个点云显示点总数的numVisiblePoints 当超出缓冲区最大点云数时,加载的点云节点会被dispose彻底消除;否则,隐藏的节点就会等待再次被使用显示 由于加载按照由近及远、由大及小的顺序,要降低卡顿,就只需要降低Potree.pointBudget即可。但目前只设置了三个层次;另外提供maxLevel细节调节,能显示更均匀. 最好多一个调节pointBudge的滑动条 Potree.lru.numPoints Potree.numVisiblePoints viewer.scene.pointclouds[0].visibleNodes.length */ {//HQSplatRenderer let oldInit = HQSplatRenderer.prototype.init; HQSplatRenderer.prototype.init = function(){ oldInit(); viewer.addEventListener('resize',this.resize.bind(this)); }; HQSplatRenderer.prototype.resize = function(e){ this.rtDepth.setSize(e.canvasWidth, e.canvasHeight); this.rtAttribute.setSize(e.canvasWidth, e.canvasHeight); }; HQSplatRenderer.prototype.clear = function(params={}){ this.init(); const {renderer, background} = this.viewer; if(background === "skybox"){ renderer.setClearColor(0x000000, 0); } else if (background === 'gradient') { renderer.setClearColor(0x000000, 0); } else if (background === 'black') { renderer.setClearColor(0x000000, 1); } else if (background === 'white') { renderer.setClearColor(0xFFFFFF, 1); } else { renderer.setClearColor(0x000000, 0); } params.target || renderer.clear(); this.clearTargets(params); }; HQSplatRenderer.prototype.render = function(params={}) { this.init(); const viewer = this.viewer; const camera = params.camera ? params.camera : viewer.scene.getActiveCamera(); const {width, height} = params.width ? params : this.viewer.renderer.getSize(new Vector2()); viewer.renderer.setRenderTarget(params.target||null); viewer.dispatchEvent({type: "render.pass.begin",viewer: viewer}); //params.target || this.resize(width, height); const visiblePointClouds = viewer.scene.pointclouds.filter(pc => pc.visible); const originalMaterials = new Map(); for(let pointcloud of visiblePointClouds){ originalMaterials.set(pointcloud, pointcloud.material); if(!this.attributeMaterials.has(pointcloud)){ let attributeMaterial = new ExtendPointCloudMaterial(); this.attributeMaterials.set(pointcloud, attributeMaterial); } if(!this.depthMaterials.has(pointcloud)){ let depthMaterial = new ExtendPointCloudMaterial(); depthMaterial.setDefine("depth_pass", "#define hq_depth_pass"); depthMaterial.setDefine("use_edl", "#define use_edl"); this.depthMaterials.set(pointcloud, depthMaterial); } } { // DEPTH PASS for (let pointcloud of visiblePointClouds) { let octreeSize = pointcloud.pcoGeometry.boundingBox.getSize(new Vector3()).x; let material = originalMaterials.get(pointcloud); let depthMaterial = this.depthMaterials.get(pointcloud); depthMaterial.size = material.size; depthMaterial.minSize = material.minSize; depthMaterial.maxSize = material.maxSize; depthMaterial.pointSizeType = material.pointSizeType; depthMaterial.visibleNodesTexture = material.visibleNodesTexture; depthMaterial.weighted = false; depthMaterial.screenWidth = width; depthMaterial.shape = PointShape.CIRCLE; depthMaterial.screenHeight = height; depthMaterial.uniforms.visibleNodes.value = material.visibleNodesTexture; depthMaterial.uniforms.octreeSize.value = octreeSize; depthMaterial.spacing = pointcloud.pcoGeometry.spacing; // * Math.max(...pointcloud.scale.toArray()); depthMaterial.classification = material.classification; depthMaterial.uniforms.classificationLUT.value.image.data = material.uniforms.classificationLUT.value.image.data; depthMaterial.classificationTexture.needsUpdate = true; depthMaterial.uniforms.uFilterReturnNumberRange.value = material.uniforms.uFilterReturnNumberRange.value; depthMaterial.uniforms.uFilterNumberOfReturnsRange.value = material.uniforms.uFilterNumberOfReturnsRange.value; depthMaterial.uniforms.uFilterGPSTimeClipRange.value = material.uniforms.uFilterGPSTimeClipRange.value; depthMaterial.uniforms.uFilterPointSourceIDClipRange.value = material.uniforms.uFilterPointSourceIDClipRange.value; depthMaterial.clipTask = material.clipTask; depthMaterial.clipMethod = material.clipMethod; depthMaterial.setClipBoxes(material.clipBoxes); depthMaterial.setClipPolygons(material.clipPolygons); pointcloud.material = depthMaterial; } viewer.pRenderer.render(viewer.scene.scenePointCloud, camera, (params.rtEDL || this.rtDepth), { clipSpheres: viewer.scene.volumes.filter(v => (v instanceof SphereVolume)), }); } { // ATTRIBUTE PASS for (let pointcloud of visiblePointClouds) { let octreeSize = pointcloud.pcoGeometry.boundingBox.getSize(new Vector3()).x; let material = originalMaterials.get(pointcloud); let attributeMaterial = this.attributeMaterials.get(pointcloud); attributeMaterial.size = material.size; attributeMaterial.minSize = material.minSize; attributeMaterial.maxSize = material.maxSize; attributeMaterial.pointSizeType = material.pointSizeType; attributeMaterial.activeAttributeName = material.activeAttributeName; attributeMaterial.visibleNodesTexture = material.visibleNodesTexture; attributeMaterial.weighted = true; attributeMaterial.screenWidth = width; attributeMaterial.screenHeight = height; attributeMaterial.shape = PointShape.CIRCLE; attributeMaterial.uniforms.visibleNodes.value = material.visibleNodesTexture; attributeMaterial.uniforms.octreeSize.value = octreeSize; attributeMaterial.spacing = pointcloud.pcoGeometry.spacing; // * Math.max(...pointcloud.scale.toArray()); attributeMaterial.classification = material.classification; attributeMaterial.uniforms.classificationLUT.value.image.data = material.uniforms.classificationLUT.value.image.data; attributeMaterial.classificationTexture.needsUpdate = true; attributeMaterial.uniforms.uFilterReturnNumberRange.value = material.uniforms.uFilterReturnNumberRange.value; attributeMaterial.uniforms.uFilterNumberOfReturnsRange.value = material.uniforms.uFilterNumberOfReturnsRange.value; attributeMaterial.uniforms.uFilterGPSTimeClipRange.value = material.uniforms.uFilterGPSTimeClipRange.value; attributeMaterial.uniforms.uFilterPointSourceIDClipRange.value = material.uniforms.uFilterPointSourceIDClipRange.value; attributeMaterial.elevationGradientRepeat = material.elevationGradientRepeat; attributeMaterial.elevationRange = material.elevationRange; attributeMaterial.gradient = material.gradient; attributeMaterial.matcap = material.matcap; attributeMaterial.intensityRange = material.intensityRange; attributeMaterial.intensityGamma = material.intensityGamma; attributeMaterial.intensityContrast = material.intensityContrast; attributeMaterial.intensityBrightness = material.intensityBrightness; attributeMaterial.rgbGamma = material.rgbGamma; attributeMaterial.rgbContrast = material.rgbContrast; attributeMaterial.rgbBrightness = material.rgbBrightness; attributeMaterial.weightRGB = material.weightRGB; attributeMaterial.weightIntensity = material.weightIntensity; attributeMaterial.weightElevation = material.weightElevation; attributeMaterial.weightRGB = material.weightRGB; attributeMaterial.weightClassification = material.weightClassification; attributeMaterial.weightReturnNumber = material.weightReturnNumber; attributeMaterial.weightSourceID = material.weightSourceID; attributeMaterial.color = material.color; attributeMaterial.clipTask = material.clipTask; attributeMaterial.clipMethod = material.clipMethod; attributeMaterial.setClipBoxes(material.clipBoxes); attributeMaterial.setClipPolygons(material.clipPolygons); pointcloud.material = attributeMaterial; } let gl = this.gl; //viewer.renderer.setRenderTarget(null); viewer.pRenderer.render(viewer.scene.scenePointCloud, camera, this.rtAttribute, { clipSpheres: viewer.scene.volumes.filter(v => (v instanceof SphereVolume)), //material: this.attributeMaterial, blendFunc: [gl.SRC_ALPHA, gl.ONE], //depthTest: false, depthWrite: false }); } for(let [pointcloud, material] of originalMaterials){ pointcloud.material = material; } if(viewer.background === "skybox"){ viewer.renderer.setClearColor(0x000000, 0); viewer.renderer.clear(); viewer.skybox.camera.rotation.copy(viewer.scene.cameraP.rotation); viewer.skybox.camera.fov = viewer.scene.cameraP.fov; viewer.skybox.camera.aspect = viewer.scene.cameraP.aspect; viewer.skybox.parent.rotation.x = 0; viewer.skybox.parent.updateMatrixWorld(); viewer.skybox.camera.updateProjectionMatrix(); viewer.renderer.render(viewer.skybox.scene, viewer.skybox.camera); } else if (viewer.background === 'gradient') { viewer.renderer.setClearColor(0x000000, 0); viewer.renderer.clear(); viewer.renderer.render(viewer.scene.sceneBG, viewer.scene.cameraBG); } else if (viewer.background === 'black') { viewer.renderer.setClearColor(0x000000, 1); viewer.renderer.clear(); } else if (viewer.background === 'white') { viewer.renderer.setClearColor(0xFFFFFF, 1); viewer.renderer.clear(); } else { viewer.renderer.setClearColor(0x000000, 0); viewer.renderer.clear(); } { // NORMALIZATION PASS let normalizationMaterial = this.useEDL ? this.normalizationEDLMaterial : this.normalizationMaterial; if(this.useEDL){ normalizationMaterial.uniforms.edlStrength.value = viewer.edlStrength; normalizationMaterial.uniforms.radius.value = viewer.edlRadius; normalizationMaterial.uniforms.screenWidth.value = width; normalizationMaterial.uniforms.screenHeight.value = height; normalizationMaterial.uniforms.uEDLMap.value = (params.rtEDL || this.rtDepth).texture; } normalizationMaterial.uniforms.uWeightMap.value = this.rtAttribute.texture; normalizationMaterial.uniforms.uDepthMap.value = this.rtAttribute.depthTexture; Utils.screenPass.render(viewer.renderer, normalizationMaterial); } viewer.renderer.render(viewer.scene.scene, camera); viewer.dispatchEvent({type: "render.pass.scene", viewer: viewer}); viewer.renderer.render(viewer.scene.sceneOverlay, camera);// add 透明贴图层 viewer.renderer.clearDepth(); viewer.transformationTool.update(); if(!params.target){ //测量线 viewer.dispatchEvent({type: "render.pass.perspective_overlay",viewer: viewer, camera}); viewer.renderer.render(viewer.overlay, camera);//从 viewer.renderDefault搬过来,为了reticule不遮住测量线 } viewer.renderer.render(viewer.controls.sceneControls, camera); viewer.renderer.render(viewer.clippingTool.sceneVolume, camera); viewer.renderer.render(viewer.transformationTool.scene, camera); viewer.renderer.setViewport(width - viewer.navigationCube.width, height - viewer.navigationCube.width, viewer.navigationCube.width, viewer.navigationCube.width); viewer.renderer.render(viewer.navigationCube, viewer.navigationCube.camera); viewer.renderer.setViewport(0, 0, width, height); viewer.dispatchEvent({type: "render.pass.end",viewer: viewer}); viewer.renderer.setRenderTarget(null); }; } //PointCloudOctreeGeometry.js PointCloudOctreeGeometryNode.prototype.loadHierachyThenPoints = function(pointcloud){ let node = this; // load hierarchy let callback = function (node, hbuffer) { let tStart = performance.now(); let view = new DataView(hbuffer); let stack = []; let children = view.getUint8(0); let numPoints = view.getUint32(1, true); node.numPoints = numPoints; stack.push({children: children, numPoints: numPoints, name: node.name}); let decoded = []; let offset = 5; while (stack.length > 0) { let snode = stack.shift(); let mask = 1; for (let i = 0; i < 8; i++) { if ((snode.children & mask) !== 0) { let childName = snode.name + i; let childChildren = view.getUint8(offset); let childNumPoints = view.getUint32(offset + 1, true); stack.push({children: childChildren, numPoints: childNumPoints, name: childName}); decoded.push({children: childChildren, numPoints: childNumPoints, name: childName}); offset += 5; } mask = mask * 2; } if (offset === hbuffer.byteLength) { break; } } // console.log(decoded); let nodes = {}; nodes[node.name] = node; let pco = node.pcoGeometry; let maxLevel_ = 0; for (let i = 0; i < decoded.length; i++) { let name = decoded[i].name; let decodedNumPoints = decoded[i].numPoints; let index = parseInt(name.charAt(name.length - 1)); let parentName = name.substring(0, name.length - 1); let parentNode = nodes[parentName]; let level = name.length - 1; maxLevel_ = Math.max(maxLevel_,level);//add let boundingBox = Utils.createChildAABB(parentNode.boundingBox, index); let currentNode = new PointCloudOctreeGeometryNode(name, pco, boundingBox); currentNode.level = level; currentNode.numPoints = decodedNumPoints; currentNode.hasChildren = decoded[i].children > 0; currentNode.spacing = pco.spacing / Math.pow(2, level); parentNode.addChild(currentNode); nodes[name] = currentNode; } pco.dispatchEvent({type:'updateNodeMaxLevel',level:maxLevel_});//add let duration = performance.now() - tStart; if(duration > 5){ /* let msg = `duration: ${duration}ms, numNodes: ${decoded.length}`; console.log(msg); */ } node.loadPoints(); }; if ((node.level % node.pcoGeometry.hierarchyStepSize) === 0) { // let hurl = node.pcoGeometry.octreeDir + "/../hierarchy/" + node.name + ".hrc"; let hurl = node.pcoGeometry.octreeDir + '/' + node.getHierarchyPath() + '/' + node.name + '.hrc'; hurl += '?m='+node.pcoGeometry.timeStamp; //add let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', hurl, true); xhr.responseType = 'arraybuffer'; xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200 || xhr.status === 0) { let hbuffer = xhr.response; callback(node, hbuffer); } else { console.log('Failed to load file! HTTP status: ' + xhr.status + ', file: ' + hurl); Potree.numNodesLoading--; } } }; try { xhr.send(null); } catch (e) { console.log('fehler beim laden der punktwolke: ' + e); } } }; PointCloudOctreeGeometryNode.prototype.loadPoints = function(){ let name = this.name; this.pcoGeometry.loader.load(this, ()=>{//callback viewer.dispatchEvent('pointcloud_changed'); //console.log('loadPoints success ', name) }); }; //加载点云成功->准备渲染画面->更新点云可见性updateVisibility->请求加载新的点云 PointCloudOctreeGeometryNode.prototype.traverse = function(t, e){//add from navvis 25.js void 0 === e && (e = !0); for (var n, i = e ? [this] : []; void 0 !== (n = i.pop()); ) { t(n); for (var o = 0, r = n.children; o < r.length; o++) { var a = r[o]; null !== a && i.push(a); } } }; Object.assign( PointCloudOctreeGeometry.prototype, EventDispatcher.prototype ); LRU.prototype.freeMemory = function(){ if (this.elements <= 1) { return; } let memoryRatio = browser.isMobile() ? 2 : 5; //改成navvis的,使用pointBudget,否则四屏点云闪烁。 (似乎要比updateVisiblede的node时限制要宽些,作为缓存继续存着。否则会闪烁) let max = MathUtils.clamp( viewer.viewports.length * memoryRatio * Potree.pointBudget, 0, 40e6); for (; this.numPoints > max; ) { var node = this.getLRUItem(); node && this.disposeDescendants(node); } }; VolumeTool.prototype.update = function(){}; VolumeTool.prototype.startInsertion = function(args = {}){ let volume; if(args.type){ volume = new args.type(); }else { volume = new Potree.BoxVolume(Object.assign(args,{clip:true}) ); } volume.highlight = true; volume.name = args.name || 'Volume-'+args.clipTask; volume.isNew = true; viewer.transformObject(null);//先清空 //console.log('startInsertion',volume.uuid) let oldVisiBoxes; if(args.clipTask == Potree.ClipTask.SHOW_INSIDE){ //如果是显示类型,需要将所有同类型的解除效果,否则看不到效果。 (或者可以在添加非第一个时去除highlight效果,会更自然,但看不清全貌) oldVisiBoxes = viewer.scene.volumes.filter(v => v.clipTask == Potree.ClipTask.SHOW_INSIDE && !v.highlight ); oldVisiBoxes.forEach(box=>box.highlight = true); } let updatePose = ()=>{ //保证在视野中的大小一致: let camera = this.viewer.scene.getActiveCamera(); let w = math.getScaleForConstantSize({ width2d: 300, camera , position:volume.getWorldPosition(new Vector3()) , resolution: viewer.mainViewport.resolution//2 }); /* let wp = volume.getWorldPosition(new THREE.Vector3()).applyMatrix4(camera.matrixWorldInverse); // let pp = new THREE.Vector4(wp.x, wp.y, wp.z).applyMatrix4(camera.projectionMatrix); let w = Math.abs((wp.z / 3));*/ if(!isNaN(w))volume.scale.set(w, w, w); {//使水平朝向与camera一致 let direction = viewer.mainViewport.view.direction.setZ(0); volume.quaternion.copy(math.getQuaByAim(direction)); } }; this.dispatchEvent({ type: 'start_inserting_volume', volume: volume }); updatePose(); this.viewer.scene.addVolume(volume); this.scene.add(volume); let drag = e => { if(e.hoverViewport.name == 'mapViewport')return let I = Utils.getMousePointCloudIntersection( viewer.mainViewport, viewer.inputHandler.mouse, viewer.inputHandler.pointer, this.viewer.scene.getActiveCamera(), this.viewer, this.viewer.scene.pointclouds, {pickClipped: args.clipTask == Potree.ClipTask.SHOW_OUTSIDE } //无视clip状态 ); var worldPos = I && I.location; if(!worldPos){ return } volume.position.copy(worldPos); updatePose(); }; let cancel = ()=>{ end('remove'); }; let end = (e) => { if(e.button == MOUSE.RIGHT && e.pressDistance<=Potree.config.clickMaxDragDis) {//remove e = 'remove'; } //console.log('end',volume.uuid, e) if(e != 'remove' && (!e.isAtDomElement || e.pressDistance>Potree.config.clickMaxDragDis))return continueDrag() volume.removeEventListener('drag', drag); volume.removeEventListener('drop', end); this.viewer.removeEventListener('cancel_insertions', cancel); volume.isNew = false; viewer.removeEventListener('camera_changed', updatePose); if(e == 'remove'){ viewer.scene.removeVolume(volume); //删除没完成的 }else { viewer.transformObject(volume); volume.highlight = false; } volume.dispatchEvent({type:'createFinish', success:e != 'remove' }); oldVisiBoxes && oldVisiBoxes.forEach(box=>box.highlight = false); }; let continueDrag = ( )=>{ //console.log('continueDrag',volume.uuid ) var timer = setTimeout(()=>{//等 drag=null之后 //右键拖拽结束后需要重新得到drag if(volume.parent && volume.isNew){ viewer.inputHandler.startDragging( volume , {notPressMouse:true} /* {endDragFun: e.drag.endDragFun, notPressMouse:e.drag.notPressMouse, dragViewport:e.drag.dragViewport} */ ); } },1); return timer }; volume.addEventListener('drag', drag); volume.addEventListener('drop', end); this.viewer.addEventListener('cancel_insertions', cancel); viewer.addEventListener('camera_changed', updatePose); this.viewer.inputHandler.startDragging(volume, {notPressMouse:true}); return volume; }; LineGeometry.prototype.setPositions = function( array ) { //xzw改成类似LineSegments的多段线 (第二个点和第三个点之间是没有线段的, 所以不用在意线段顺序) const points = new Float32Array( array ); LineSegmentsGeometry.prototype.setPositions.call(this, points ); return this; }; Object.assign(ExtendView.prototype, EventDispatcher.prototype); Object.assign(ExtendScene.prototype, EventDispatcher.prototype ); function start(dom, mapDom, number ){ //t-Zvd3w0m /* { let obj = JSON.parse(localStorage.getItem('setting')) for(let i in obj){ console.log(i + ': ' + obj[i]) } } */ Potree.settings.number = number || 't-o5YMR13';// 't-iksBApb'// 写在viewer前 if(!Potree.settings.isOfficial){ if(/* Potree.settings.isTest && */ browser.isMobile()){ changeLog(); } } if(browser.urlHasValue('google'))Potree.settings.mapCompany = 'google'; if(browser.urlHasValue('timing'))Potree.measureTimings = 1; let viewer = new Potree.Viewer(dom , mapDom); /* viewer.addEventListener('allLoaded',()=>{ const baseGeometry = new THREE.BufferGeometry(); baseGeometry.setIndex([0, 1, 2, 0, 2, 3]); const positionsArray = new Float32Array(4 * 3); const positions = new THREE.BufferAttribute(positionsArray, 3); baseGeometry.setAttribute('position', positions); positions.setXYZ(0, -1.0, -1.0, 0.0); positions.setXYZ(1, -1.0, 1.0, 0.0); positions.setXYZ(2, 1.0, 1.0, 0.0); positions.setXYZ(3, 1.0, -1.0, 0.0); positions.needsUpdate = true; const geometry = new THREE.InstancedBufferGeometry().copy(baseGeometry); let maxSplatCount = 8 // Splat index buffer const splatIndexArray = new Uint32Array(maxSplatCount); const splatIndexes = new THREE.InstancedBufferAttribute(splatIndexArray, 1, false); splatIndexes.setUsage(THREE.DynamicDrawUsage); geometry.setAttribute('splatIndex', splatIndexes); const splatPosArray = new Float32Array(maxSplatCount*3); const splatPoses = new THREE.InstancedBufferAttribute(splatPosArray, 3, false); splatPoses.setUsage(THREE.DynamicDrawUsage); geometry.setAttribute('splatCenter', splatPoses); splatPosArray.set([0,0,0, 1,1,1, 2,2,2, 3,3,3, 4,4,4, 5,5,5, 6,6,6, 7,7,7]) let vertexShaderSource = ` precision highp float; #include attribute vec3 splatCenter; attribute float splatIndex; varying vec2 vPosition; const float sqrt8 = sqrt(8.0); const float minAlpha = 1.0 / 255.0; void main () { mat4 transformModelViewMatrix = modelViewMatrix; vec4 viewCenter = transformModelViewMatrix * vec4(splatCenter, 1.0); vec4 clipCenter = projectionMatrix * viewCenter; float clip = 1.2 * clipCenter.w; if (clipCenter.z < -clip || clipCenter.x < -clip || clipCenter.x > clip || clipCenter.y < -clip || clipCenter.y > clip) { gl_Position = vec4(0.0, 0.0, 2.0, 1.0); return; } vPosition = position.xy; vec3 ndcCenter = clipCenter.xyz / clipCenter.w; float eigenValue1 = 0.2; float eigenValue2 = 0.2; //vec2 eigenVector1 = normalize(vec2(b, eigenValue1 - a)); vec2 eigenVector1 = normalize(vec2(0.5, 0.5)); vec2 eigenVector2 = vec2(eigenVector1.y, -eigenVector1.x); vec2 basisVector1 = eigenVector1 * min(sqrt8 * sqrt(eigenValue1), 2048.0); vec2 basisVector2 = eigenVector2 * min(sqrt8 * sqrt(eigenValue2), 2048.0); vec2 ndcOffset = vec2(vPosition.x * basisVector1 + vPosition.y * basisVector2) / viewport * 2.0 ; gl_Position = vec4(ndcCenter.xy + ndcOffset, ndcCenter.z , 1.0); vPosition *= sqrt8; }`; //面片模拟椭球,总是有厚度 const fragmentShaderSource = ` precision highp float; #include uniform vec3 color; varying vec2 vPosition; void main () { float A = dot(vPosition, vPosition); if (A > 8.0) discard; //position的范围半径为1。指一个rectangle面中的范围。椭圆外的完全透明 float opacity = exp(-0.5 * A) * vColor.a; //周围的透明度降低 gl_FragColor = vec4(color.rgb, opacity); }`; const uniforms = { 'viewport': { 'type': 'v2', 'value': new THREE.Vector2() } , color:{ 'type': 'v3', 'value': new THREE.Color() } }; const material = new THREE.ShaderMaterial({ uniforms: uniforms, vertexShader: vertexShaderSource, fragmentShader: fragmentShaderSource, transparent: true, alphaTest: 1.0, blending: THREE.NormalBlending, depthTest: true, depthWrite: false, side: THREE.DoubleSide }); window.splatMesh = new THREE.Mesh(geometry, material) viewer.scene.scene.add(window.splatMesh) viewer.addEventListener('resize',(e)=>{ if(e.viewport.name == 'mainViewport'){ uniforms.viewport.copy(e.viewport.resolution) } }) window.setSplatIndexes = (globalIndexes)=>{ geometry.attributes.splatIndex.set(globalIndexes); geometry.attributes.splatIndex.needsUpdate = true; geometry.instanceCount = globalIndexes.length } }) */ let Alignment = viewer.modules.Alignment; viewer.setEDLEnabled(false); viewer.setFOV(Potree.config.view.fov); //viewer.loadSettingsFromURL(); Potree.settings.cameraFar = Potree.config.view.far; if(!Potree.settings.isOfficial){ viewer.loadGUI(() => { viewer.setLanguage('en'); //$("#menu_appearance").next().show(); $("#menu_tools").next().show(); $("#menu_scene").next().show(); $("#siteModel").show(); //$("#alignment").show(); viewer.toggleSidebar(); }); Potree.settings.sizeFitToLevel = true;//当type为衰减模式时自动根据level调节大小。每长一级,大小就除以2 Potree.settings.rotAroundPoint = false; } Potree.loadDatasetsCallback = function(data, ifReload){ if(!data || data.length == 0)return console.error('getDataSet加载的数据为空') Potree.datasetData = data; viewer.transform = null; var datasetLength = data.length; var pointcloudLoaded = 0; var panosLoaded = 0; var pointcloudLoadDone = function(){//点云cloud.js加载完毕后 }; var panosLoadDone = function(){ viewer.images360.loadDone(); viewer.scene.add360Images(viewer.images360); viewer.mapViewer.addListener(viewer.images360); viewer.updateModelBound(); //需等pano加载完 let {boundSize, center} = viewer.bound; if(!Potree.settings.isOfficial){ Potree.loadMapEntity('all'); //加载floorplan } if(!ifReload){ viewer.dispatchEvent({type:'loadPointCloudDone'}); if(!Potree.settings.UserPointDensity){ Potree.settings.UserPointDensity = 'high';//'middle' } Potree.Log('loadPointCloudDone 点云加载完毕', {font:[null, 10]}); } {//初始位置 var panoId = browser.urlHasValue('pano',true); if(panoId !== ''){ var pos; var pano = viewer.images360.panos.find(e=>e.id==panoId); if(pano){ viewer.images360.focusPano({ pano, duration:0, callback:()=>{/* Potree.settings.displayMode = 'showPanos' */} }); } }else {//考虑到多数据集距离很远,或者像隧道那种场景,要使视野范围内一定能看到点云,最好初始点设置在漫游点上 let {boundSize, center} = viewer.bound; let pano = viewer.images360.findNearestPano(center); if(pano){ viewer.images360.flyToPano({ pano, duration:0, target : viewer.images360.bound.center.setZ(pano.position.z) //平视中心区域(但也不能保证这个方向一定能看到点云密集区,如果在边缘的话) }); }else {// 无漫游点 避免加载时看不到点云 SG-t-DXmdymgZ2sX SG-t-rVB03a5GXr8 //SG-t-XPf1k9pv3Zg 总有极端的场景,如这是倾斜的桥 - -, 只能调整为看到全局了 viewer.mainViewport.view.pitch = -0.7; //相对俯视 let bound = viewer.bound.boundingBox.clone(); viewer.focusOnObject({boundingBox:bound},'boundingBox',0,{dontChangeCamDir:true}); if(viewer.bound.boundSize.length() > 20){ //否则有可能超出far范围 viewer.mainViewport.camera.far = 10000; viewer.mainViewport.camera.updateProjectionMatrix(); viewer.fixCamFar = true; //不允许修改camera.far //等有点云加载出来后,再去focus其中一个,使camera.far不超过最大值 let count_ = 0; let done = ()=>{ viewer.fixCamFar = false; viewer.mainViewport.camera.far = Potree.settings.cameraFar; viewer.mainViewport.camera.updateProjectionMatrix(); viewer.removeEventListener('pageVisible', focusPoint ); clearTimeout(timer); }; let timer; let focusPoint = (e)=>{//拉近到某个点 if(e && e.v === false)return viewer.removeEventListener('pageVisible', focusPoint ); let pointcloud = viewer.scene.pointclouds.find(e=>e.root.geometryNode); console.log('初始加载focus点云', e , pointcloud); if(!pointcloud){ if(count_ < 10 ){//可能没加载到,可能被隐藏 if(document.hidden){//等回到页面再focus console.log('focus hidden'); return viewer.addEventListener('pageVisible', focusPoint ) } count_ ++; //如果在别的 timer = setTimeout(focusPoint, 200); }else { //放弃 console.log('初始加载focus点云 放弃'); done(); } return console.warn('no!!!!!!!!!!!!!!') } viewer.flyToDataset({focusOnPoint:true, pointcloud, duration:0, }); console.warn('ok!!!!!!!!!!!!!!!!'); done(); }; let focus = ()=>{ timer = setTimeout(focusPoint, 300); }; viewer.addEventListener('setPose',()=>{//设置了初始画面 viewer.removeEventListener('pointcloud_changed',focus); done(); }); viewer.addEventListener('pointcloud_changed',focus,{once:true});//加载了点之后 } } } } viewer.addVideo();//addFire() viewer.scene.pointclouds.some(e=>!e.hasDepthTex) && (Potree.config.shelterMargin*=3); //没有深度图的话在全景模式测量有偏差 console.log('allLoaded'); viewer.addTube({ 't-8KbK1JjubE':[ {//地上管子 黄色 path:[{"x":-109.83,"y":-68.33,"z":-7.52},{"x":-95.17,"y":-59.3,"z":-7.38}, {"x":-38.75,"y":-24.01,"z":-6.01},{"x":0.5,"y":0.19,"z":-3.89},{"x":39.29,"y":24.41,"z":-1.31} ,{"x":43.58,"y":27.7,"z":-0.97},{"x":40.22,"y":35.37,"z":-0.67}// 拐弯向右 ,{"x":39.18,"y":36.71,"z":0.35},{"x":38.69,"y":36.04,"z":18.04} // 拐弯向上 } ], color:'#b86', radius:0.08, height:0.13 }, {//地下管子 藍色 path:[{"x":-108.24,"y":-70.61,"z":-7.52}, {"x":-57.8,"y":-39.31,"z":-6.72},{"x":-18.8,"y":-15.35,"z":-5.01},{"x":55.87,"y":31.67,"z":-0.04},{"x":110.53,"y":66.48,"z":5.14} ], color:'#48a', radius:0.08, height:-0.5 } ] , /* 'SG-t-h0I7LnfGFwj':[ //港华M层 { path:[[20.295,-10.269,-1.152],[19.996,-10.252,-1.153],[19.98,-10.253,-1.156],[19.967,-10.252,-1.165],[19.957,-10.251,-1.179],[19.953,-10.251,-1.225],[19.961,-10.242,-1.267],[19.968,-10.223,-1.296],[19.967,-10.193,-1.304],[20.007,-10.092,-1.321],[19.997,-10.063,-1.309],[19.979,-10.039,-1.284],[19.989,-10.028,-1.256],[19.993,-10.024,-1.23],[19.972,-10.026,-1.181],[20.009,-10.045,-1.026],[20.025,-10.048,-1.013],[20.045,-10.05,-1.006],[20.056,-10.052,-1.001],[20.413,-10.073,-1.016],[20.461,-10.075,-1.02],[20.498,-10.053,-1.018],[20.525,-10.022,-1.025],[20.538,-9.983,-1.029],[20.554,-9.536,-1.111],[20.554,-9.487,-1.118],[20.545,-9.445,-1.129],[20.521,-9.418,-1.14],[20.478,-9.396,-1.156],[20.427,-9.379,-1.182],[20.367,-9.369,-1.198],[19.971,-9.304,-1.221],[19.749,-9.197,-1.345]] ,color:'#8c99aa', radius:0.025, height:-0, fromDataset:true, tension:0.01, spaceDis:0.02,visiEntity:'厨房' }, { path:[[19.708,-2.921,-1.145],[19.721,-2.924,-1.119],[20.704,-2.881,-1.134],[20.736,-2.87,-1.138],[20.76,-2.852,-1.147],[20.773,-2.828,-1.148],[20.777,-2.809,-1.148],[20.794,-2.493,-1.167],[20.793,-2.461,-1.169],[20.776,-2.436,-1.174],[20.742,-2.425,-1.198],[20.14,-2.459,-1.309],[20.123,-2.457,-1.309],[20.108,-2.451,-1.31],[20.101,-2.439,-1.311],[20.095,-2.42,-1.313],[20.094,-2.399,-1.311],[20.092,-1.966,-1.295],[20.093,-1.945,-1.286],[20.094,-1.931,-1.269],[20.093,-1.929,-1.251],[20.09,-1.921,-1.197],[20.096,-1.914,-1.181],[20.107,-1.909,-1.174],[20.176,-1.893,-1.141],[20.699,-1.848,-1.076],[20.759,-1.824,-1.068],[20.81,-1.754,-1.076],[20.837,-1.71,-1.086],[20.85,-1.634,-1.1],[20.815,-1.01,-1.104],[20.824,-0.979,-1.115],[20.83,-0.957,-1.124],[20.843,-0.936,-1.132],[20.871,-0.925,-1.139],[21.065,-0.936,-1.166],[21.096,-0.935,-1.167],[21.115,-0.919,-1.167],[21.129,-0.887,-1.165],[21.136,-0.783,-1.167],[21.136,0.555,-1.201],[21.128,0.599,-1.201],[21.1,0.627,-1.199],[21.058,0.639,-1.198],[20.832,0.616,-1.179],[20.782,0.62,-1.186],[20.748,0.637,-1.19],[20.728,0.698,-1.21],[20.731,1.311,-1.233],[20.68,2.081,-1.292],[20.654,2.157,-1.307],[20.576,2.215,-1.314],[20.484,2.274,-1.321],[20.035,2.354,-1.353],[20.013,2.367,-1.363],[20.008,2.39,-1.371],[19.989,3.124,-1.384],[19.993,3.175,-1.39],[19.995,3.208,-1.393],[19.995,3.233,-1.374],[20.002,3.246,-1.324],[20.006,3.248,-1.266],[20.027,3.243,-1.245],[20.063,3.241,-1.239],[20.62,3.229,-1.226],[20.645,3.235,-1.204],[20.658,3.251,-1.191],[20.667,3.278,-1.184],[20.675,3.312,-1.193],[20.716,3.71,-1.24],[20.722,3.767,-1.251],[20.704,3.823,-1.268],[20.652,3.864,-1.279],[20.026,3.901,-1.368],[19.999,3.919,-1.37],[19.982,3.95,-1.361],[19.933,4.69,-1.285],[19.972,4.812,-1.332],[19.978,4.817,-1.233],[19.992,4.814,-1.203],[20.047,4.81,-1.196],[20.738,4.794,-1.196]] ,color:'#8c99aa', radius:0.025, height:-0, fromDataset:true, tension:0.01, spaceDis:0.02,visiEntity:'厨房' }, { path:[[19.685,-2.896,-1.115],[19.699,-2.93,-1.274],[19.701,-2.936,-1.295],[19.707,-2.94,-1.316],[19.712,-2.951,-1.34],[19.714,-2.965,-1.353],[19.716,-2.986,-1.359],[19.7,-3.595,-1.309],[19.703,-3.619,-1.303],[19.704,-3.628,-1.274],[19.719,-3.632,-1.256],[20.063,-3.633,-1.275],[20.089,-3.637,-1.254],[20.103,-3.64,-1.227],[20.11,-3.642,-1.191],[20.083,-3.669,-1.027],[20.075,-3.68,-0.999],[20.053,-3.694,-0.978],[20.053,-3.805,-0.957],[20.063,-3.827,-0.963],[20.045,-3.836,-0.962],[20.01,-3.841,-0.957],[19.737,-3.818,-0.964],[19.712,-3.814,-0.985],[19.699,-3.819,-1.059],[19.724,-3.817,-1.247],[19.725,-3.827,-1.28],[19.719,-3.842,-1.309],[19.719,-3.858,-1.33],[19.714,-3.881,-1.341],[19.699,-4.56,-1.339],[19.7,-4.62,-1.336],[19.7,-4.643,-1.334],[19.699,-4.66,-1.326],[19.698,-4.675,-1.31],[19.697,-4.678,-1.279],[19.697,-4.678,-1.21],[19.704,-4.682,-1.187],[19.716,-4.682,-1.173],[19.736,-4.683,-1.164],[20.481,-4.817,-1.026],[20.529,-4.847,-0.984],[20.562,-4.89,-0.935],[20.585,-4.932,-0.909],[20.59,-4.992,-0.887],[20.565,-5.98,-0.847],[20.547,-6.08,-0.846],[20.541,-6.17,-0.86],[20.476,-6.21,-0.858],[19.847,-6.207,-0.808],[19.803,-6.22,-0.827],[19.775,-6.217,-0.885],[19.75,-6.188,-1.289],[19.743,-6.193,-1.341],[19.742,-6.207,-1.375],[19.75,-6.232,-1.401],[19.722,-6.905,-1.359],[19.723,-6.958,-1.352],[19.722,-6.987,-1.342],[19.721,-7.001,-1.31],[19.721,-7.004,-1.231],[19.734,-7.03,-0.976],[19.742,-7.041,-0.947],[19.747,-7.059,-0.922],[19.755,-7.284,-0.764],[19.761,-7.312,-0.731],[19.776,-7.337,-0.711],[19.795,-7.355,-0.702],[20.502,-7.453,-0.715],[20.578,-7.496,-0.715],[20.631,-7.557,-0.719],[20.66,-7.653,-0.718],[20.658,-8.639,-0.766],[20.654,-8.789,-0.763],[20.639,-8.856,-0.763],[20.605,-8.89,-0.76],[20.57,-8.916,-0.761],[20.539,-8.945,-0.773],[19.879,-9.028,-0.814],[19.853,-9.032,-0.828],[19.834,-9.033,-0.842],[19.83,-9.033,-0.876],[19.857,-9.033,-1.292],[19.837,-9.037,-1.333],[19.819,-9.051,-1.354],[19.812,-9.082,-1.381],[19.79,-9.184,-1.386]] ,color:'#8c99aa', radius:0.02, height:-0, fromDataset:true, tension:0.01, spaceDis:0.02,visiEntity:'厨房' } ], 'SG-t-RGUFiJAoxvL':[ //济南工地覆土后 {//--旧的 整条 path: [[23.522,-19.432,-1.744],[25.344,20.312,-1.855],[25.344,21.302,-1.861],[25.253,21.904,-1.861],[25.088,22.381,-1.859],[24.748,22.772,-1.861],[24.222,22.893,-1.873],[-24.547,16.774,-1.319]] ,color:'#445', radius:0.08, height:-1.16, fromDataset:true, tension:0.01,// spaceDis:0.1 }, { path:[[22.726,22.678,-1.938],[-24.541,16.782,-1.319]] ,color:'#445', radius:0.08, height:-1.16, fromDataset:true, tension:0,// spaceDis:0.1 }, { path:[[23.519,-19.473,-1.744],[25.117,15.447,-1.783]] ,color:'#445', radius:0.08, height:-1.16, fromDataset:true, tension:0,// spaceDis:0.1 } ], 'SG-t-TK0S5EqWBxd':[ //济南工地 { path:[[-25.768,-12.695,-1.305],[-26.334,-21.834,-1.511],[-26.355,-22.153,-1.514],[-26.366,-22.418,-1.513],[-26.31,-22.66,-1.515],[-26.126,-22.841,-1.519],[-25.871,-22.968,-1.525],[-25.443,-22.969,-1.532],[-24.953,-22.956,-1.585],[-23.697,-22.854,-1.637]] ,color:'#445', radius:0.08, height:-1.16, fromDataset:true, spaceDis:0.2// tension:0.01, }, ],*/ }); viewer.dispatchEvent('allLoaded'); }; var transformPointcloud = (pointcloud, dataset)=>{ var locationLonLat = dataset.location.slice(0,3); // [lon,lat,高程海拔] //当只有一个dataset时,无论如何transform 点云和漫游点都能对应上。 var location = viewer.transform.lonlatToLocal.forward(locationLonLat); //transform.inverse() //初始化位置 viewer.sidebar && viewer.sidebar.addAlignmentButton(pointcloud); //dataset.orientation = 0 Alignment.rotate(pointcloud, null, dataset.orientation); Alignment.translate(pointcloud, new Vector3(location[0], location[1], dataset.location[2]-originDataset.location[2])); //要使初始数据集的z为0,所以要减去初始数据集的z pointcloud.updateMatrixWorld(); Potree.Log(`点云${pointcloud.dataset_id}(${pointcloud.name})旋转值:${pointcloud.orientationUser}, 位置${math.toPrecision(pointcloud.translateUser.toArray(),3)}, 经纬度 ${dataset.location}, spacing ${pointcloud.material.spacing}`, {font:[null, 13]} ); }; if(!Potree.settings.originDatasetId)Potree.settings.originDatasetId = data[0].id; var originDataset = data.find(e=>e.id == Potree.settings.originDatasetId); /* originDataset.location[0] = 113.60608878174709 originDataset.location[1] = 22.381189423935155 let ano = data.find(e=>e.name == 'SG-t-Jw0xyhL6oSY') ano.location[0] = 113.6060509967498 ano.location[1] = 22.381061273279244 */ {//拿初始数据集作为基准。它的位置要放到000 var locationLonLat = originDataset.location.slice(0,2); if(window.AMapWith84){//需要转换为高德的,但该函数不准确,转入后再转出,和原来的有偏差. navvis的我看data中存的globalLocation直接输入到高德地图后的定位和其要展示的定位一致,而我们要转为高德后才一致,猜测是navvis后台转为了高德可用的经纬度。 若不转的话,其他看起来没问题,仅高德地图定位不准确,因其为被加密后的火星坐标系。 locationLonLat = AMapWith84.wgs84ToAMap({x:locationLonLat[0], y:locationLonLat[1]}); locationLonLat = [locationLonLat.x,locationLonLat.y]; } proj4.defs("LOCAL", "+proj=tmerc +ellps=WGS84 +lon_0=" + locationLonLat[0].toPrecision(15) + " +lat_0=" + locationLonLat[1].toPrecision(15)); //高德坐标系 proj4.defs("LOCAL_MAP", "+proj=tmerc +ellps=WGS84 +lon_0=" + locationLonLat[0].toPrecision(15) + " +lat_0=" + locationLonLat[1].toPrecision(15)); //地图和本地一样 proj4.defs("WGS84", "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"); let transform1 = proj4("WGS84", "LOCAL"); //这个ok 是展开的平面投影 LOCAL即NAVVIS:TMERC let transform2 = proj4("+proj=tmerc +lat_0=0 +lon_0=123 +k=1 +x_0=500000 +y_0=0 +ellps=GRS80 +units=m +no_defs;"); //注:转入后再转出,和原来的有偏差。如果输入是local坐标,数字越大偏差越大,当百万时就明显了。如果是lonlat,很奇怪经度小于50时就乱了。 viewer.transform = { lonlatToLocal : transform1, lonlatTo4550 : transform2 // 转大地坐标EPSG:4550 }; if(window.AMapWith84 && Potree.settings.mapCompany != 'google'){//需要转换, 因本地高德用的lonlat和数据里的84不一样. (google地图在国内也用的高德,国外84) let change = (transform)=>{ let forward = transform.forward; let inverse = transform.inverse; transform.forward = function(e, not84){ let needTran = e.x == void 0; if(needTran)var a1 = {x:e[0],y:e[1]}; else var a1 = e; var a = not84 ? a1 : AMapWith84.wgs84ToAMap(a1); if(needTran){ a = [a.x, a.y]; e[2] != void 0 && (a[2] = e[2]); }else { e.z != void 0 && (a.z = e.z); } return forward(a) }; transform.inverse = function(e, not84){ let needTran = e.x == void 0; var a = inverse(e); needTran && (a = {x:a[0],y:a[1]}); a = not84 ? a : AMapWith84.aMapToWgs84(a); if(needTran){ a = [a.x,a.y]; e[2] != void 0 && (a[2] = e[2]); }else { e.z != void 0 && (a.z = e.z); } return a }; }; for(let f in viewer.transform){ change(viewer.transform[f]); } } viewer.mapViewer && viewer.mapViewer.mapLayer.maps[0].updateProjection(); } data.forEach((dataset,index)=>{ if(!ifReload){ var datasetCode = dataset.sceneCode || dataset.name; //对应4dkk的场景码 if(Potree.settings.isLocal && dataset.mapping){ var cloudPath = `${Potree.settings.urls.prefix1}/${dataset.mapping}/${dataset.webBin}`; //webBin添加原因:每次裁剪之类的操作会换路径,因为oss文件缓存太严重,更新慢 }else { var cloudPath = `${Potree.settings.urls.prefix1}/${dataset.webBin}`; //webBin添加原因:每次裁剪之类的操作会换路径,因为oss文件缓存太严重,更新慢 } //var cloudPath = `${Potree.scriptPath}/data/test/${dataset.name}/cloud.js` var timeStamp = dataset.updateTime ? dataset.updateTime.replace(/[^0-9]/ig,'') : ''; //每重算一次后缀随updateTime更新一次 //console.warn(dataset.name, 'timeStamp', timeStamp) Potree.loadPointCloud(cloudPath, dataset.name ,datasetCode, timeStamp, e => { let scene = viewer.scene; let pointcloud = e.pointcloud; let config = Potree.config.material; let material = pointcloud.material; pointcloud.datasetData = dataset; pointcloud.dataset_id = dataset.id;//供漫游点找到属于的dataset点云 pointcloud.hasDepthTex = Potree.settings.useDepthTex && (!!dataset.has_depth || Potree.settings.isLocalhost && Potree.settings.number == 'SS-t-7DUfWAUZ3V'); //test material.minSize = config.minSize; material.maxSize = config.maxSize; material.pointSizeType =/* Potree.settings.isOfficial ? */ config.pointSizeType; /* : 'ADAPTIVE' */ //Potree.PointSizeType[config.pointSizeType]//Potree.PointSizeType.ADAPTIVE;//FIXED pointcloud.changePointSize(config.realPointSize); //material.size = config.pointSize; pointcloud.changePointOpacity(1); material.shape = Potree.PointShape.SQUARE; pointcloud.color = pointcloud.material.color = dataset.color; pointcloud.timeStamp = timeStamp; transformPointcloud(pointcloud,dataset); scene.addPointCloud(pointcloud); if(!Potree.settings.isOfficial){ Potree.settings.floorplanEnables[dataset.id] = true; Potree.settings.floorplanType[dataset.id] = 'default'; } pointcloudLoaded ++; if(pointcloudLoaded == datasetLength)pointcloudLoadDone(); Potree.loadPanos(dataset.id, (data) => { //console.log('loadPanos',dataset.sceneCode, dataset.id, data) viewer.images360.addPanoData(data, dataset.id ); panosLoaded ++; if(panosLoaded == datasetLength){ Potree.loadImgVersion((e={})=>{ Potree.settings.panoVersion = e.imgVersion;//全景图被替换后 panosLoadDone(); }); } }); }); }else { let pointcloud = viewer.scene.pointclouds.find(p => p.dataset_id == dataset.id); if(!pointcloud){ Potree.Log('数据集id变了,自动使用第一个', {font:['#500' ]} ); pointcloud = viewer.scene.pointclouds[0]; } //先归零 Alignment.translate(pointcloud, pointcloud.translateUser.clone().negate()); Alignment.rotate(pointcloud, null, - pointcloud.orientationUser); transformPointcloud(pointcloud, dataset); } }); if(ifReload){ //loadDone() } }; number && Potree.loadDatasets(Potree.loadDatasetsCallback); /* //调试用,加载多个本地 Potree.loadDatasetsCallback([ {name:'webcloud_0',id:0, orientation:0, location:[0,0,0]}, {name:'webcloud_1',id:1, orientation:0.047234761795199476, location:[-0.07925513345058573,-0.0010590072536559839,-2.403613132687564]}, {name:'webcloud_2',id:2, orientation:-1.5299545647758208, location:[1.5603736310030292, -0.0009340812579088904, -2.4464530770974139]}, ]) */ window.testTransform = function(locationLonLat, location1, location2){ proj4.defs("NAVVIS:test", "+proj=tmerc +ellps=WGS84 +lon_0=" + locationLonLat[0].toPrecision(15) + " +lat_0=" + locationLonLat[1].toPrecision(15)); let transform = proj4("WGS84", "NAVVIS:test"); //这个ok navvis里也是这两种转换 见proj4Factory if(location1){//经纬度 return transform.forward(location1) }else { return transform.inverse(location2) } }; window.THREE = THREE$1; window.buttonFunction = function(){ viewer.scene.pointclouds.forEach(e=>e.predictNodeMaxLevel()); /* viewer.startScreenshot({type:'measure', measurement:viewer.scene.measurements[0]}) viewer.modules.RouteGuider.routeStart = new THREE.Vector3(0,0,-1.3) viewer.modules.RouteGuider.routeEnd = new THREE.Vector3(-10,0,-1.3) */ }; if(Potree.settings.isLocalhost){ let before = {}; viewer.inputHandler.addEventListener('keydown',e=>{ //测试的代码 if(e.event.key == 't'){ viewer.images360.cube.visible = true; viewer.images360.cube.material.wireframe = true; }else if(e.event.key == 'y'){ viewer.images360.cube.material.wireframe = false; viewer.images360.cube.visible = Potree.settings.displayMode == 'showPanos'; } }); //-------------------------------- /* if(!number){ Potree.settings.boundAddObjs = true Potree.settings.intersectOnObjs = true // Load untextured bunny from ply viewer.loadModel({ fileType:'ply', url:Potree.resourcePath + "/models/indoor.ply", name:'test', }, (object)=>{ object.isModel = true viewer.updateModelBound() } ) } */ } } //======================================================================= /* 漫游点编辑 */ //======================================================================= function panoEditStart(dom, number, fileServer){ Potree.settings.editType = 'pano'; Potree.settings.number = number; Potree.settings.unableNavigate = true; Potree.settings.sizeFitToLevel = true;//当type为衰减模式时自动根据level调节大小。每长一级,大小就除以2 let viewer = new Potree.Viewer(dom); let Alignment = viewer.modules.Alignment; viewer.setEDLEnabled(false); viewer.setFOV(Potree.config.view.fov); //viewer.loadSettingsFromURL(); let datasetLoaded = 0; if(!Potree.settings.isOfficial){ viewer.loadGUI(() => { viewer.setLanguage('en'); $("#menu_tools").next().show(); $("#panos").show(); $("#alignment").show(); viewer.toggleSidebar(); }); Potree.settings.sizeFitToLevel = true; } var pointcloudLoadDone = function( ){//所有点云cloud.js加载完毕后 viewer.scene.pointclouds.forEach(c=>{ transformPointcloud(c); }); viewer.images360.loadDone(); viewer.scene.add360Images(viewer.images360); viewer.updateModelBound(); let {boundSize, center} = viewer.bound; Potree.Log(`中心点: ${math.toPrecision(center.toArray(),2)}, boundSize: ${math.toPrecision(boundSize.toArray(),2)} ` , {font:[null, 12]} ); viewer.scene.view.setView({ position: center.clone().add(new Vector3(10,5,10)), target: center }); viewer.dispatchEvent({type:'loadPointCloudDone'}); if(!Potree.settings.UserPointDensity){ Potree.settings.UserPointDensity = 'panoEdit';//'middle' } Potree.Log('loadPointCloudDone 点云加载完毕',{font:[null, 10]}); viewer.dispatchEvent('allLoaded'); }; var transformPointcloud = (pointcloud )=>{ //初始化位置 viewer.sidebar && viewer.sidebar.addAlignmentButton(pointcloud); let orientation = pointcloud.panos[0].dataRotation.z + Math.PI; let location = pointcloud.panos[0].dataPosition.clone();//.negate() Alignment.rotate(pointcloud, null, orientation ); Alignment.translate(pointcloud, location ); pointcloud.updateMatrixWorld(); }; let loadPanosDone = Potree.loadPanosDone = (datasetId, panoData )=>{ //一个数据集获取到它的panos后 Potree.settings.datasetsPanos[datasetId] = {panoData, panos:[]}; console.log('panoData', datasetId, panoData); let panoCount = panoData.length; let pointcloudLoaded = 0; let datasetsCount = Object.keys(Potree.settings.datasetsPanos).length; panoData.forEach((pano, index)=>{ //let cloudPath = `${Potree.scriptPath}/data/panoEdit/uuidcloud/${pano.uuid}/cloud.js` let cloudPath = `${Potree.settings.urls.prefix1}/${Potree.settings.webSite}/${Potree.settings.number}/data/bundle_${Potree.settings.number}/building/uuidcloud/${pano.uuid}/cloud.js`; /* if(Potree.settings.isLocal && dataset.mapping){ var cloudPath = `${Potree.settings.urls.prefix1}/${dataset.mapping}/${dataset.webBin}` //webBin添加原因:每次裁剪之类的操作会换路径,因为oss文件缓存太严重,更新慢 }else{ var cloudPath = `${Potree.settings.urls.prefix1}/${dataset.webBin}` //webBin添加原因:每次裁剪之类的操作会换路径,因为oss文件缓存太严重,更新慢 } */ let name = datasetId + '-'+pano.uuid; let timeStamp = 0; pano.index = index; //注意:index不等于uuid,因为有的uuid缺失。但是visibles中存的是下标! Potree.loadPointCloud(cloudPath, name , name, timeStamp, e => { //开始加载点云 let scene = viewer.scene; let pointcloud = e.pointcloud; let config = Potree.config.material; let material = pointcloud.material; material.minSize = config.minSize; material.maxSize = config.maxSize; material.pointSizeType = /* 'ADAPTIVE'// */config.pointSizeType; //Potree.PointSizeType[config.pointSizeType]//Potree.PointSizeType.ADAPTIVE;//FIXED pointcloud.changePointSize( 0.1 /* config.realPointSize */ ); //material.size = config.pointSize; pointcloud.changePointOpacity(1); material.shape = Potree.PointShape.SQUARE; pointcloud.color = config.pointColor; pointcloud.dataset_id = datasetId; //多个点云指向一个datasetId pointcloud.panoUuid = pano.uuid; pointcloud.timeStamp = timeStamp; //transformPointcloud(pointcloud, pano) scene.addPointCloud(pointcloud); pointcloudLoaded ++; if(pointcloudLoaded == panoCount ){ datasetLoaded ++; viewer.images360.addPanoData(panoData , datasetId ); if(datasetLoaded == datasetsCount){ pointcloudLoadDone(); } } }); }); }; if(!Potree.settings.isOfficial){ Potree.settings.datasetsPano = {'testDataset':null}; Potree.loadPanosInfo( data=>{loadPanosDone('testDataset', data.sweepLocations);} ); } } function mergeEditStart(dom, mapDom){ Potree.settings.editType = 'merge'; Potree.settings.intersectOnObjs = true; Potree.settings.boundAddObjs = true; Potree.settings.unableNavigate = true; let viewer = new Potree.Viewer(dom, mapDom ); let Alignment = viewer.modules.Alignment; viewer.setEDLEnabled(false); viewer.setFOV(Potree.config.view.fov); //viewer.loadSettingsFromURL(); { viewer.mainViewport.view.position.set(30,30,30); viewer.mainViewport.view.lookAt(0,0,0); viewer.updateModelBound();//init //this.bound = new THREE.Box3(new THREE.Vector3(-1,-1,-1),new THREE.Vector3(1,1,1)) viewer.transformationTool.setModeEnable(['translation','rotation'] ); //viewer.ssaaRenderPass.sampleLevel = 1 // sampleLevel为1 的话,ground就不会变黑 viewer.inputHandler.fixSelection = true; //不通过点击屏幕而切换transfrom选中状态 } Potree.settings.sizeFitToLevel = true;//当type为衰减模式时自动根据level调节大小。每长一级,大小就除以2 Potree.loadPointCloudScene = function(url, type, id, title, done, onError){//对应4dkk的场景码 let loadCloud = ({cloudPath, sceneName, sceneCode, timeStamp, color } )=>{ Potree.loadPointCloud(cloudPath, sceneName , sceneCode, timeStamp, e => { let scene = viewer.scene; let pointcloud = e.pointcloud; let config = Potree.config.material; let material = pointcloud.material; material.minSize = config.minSize; material.maxSize = config.maxSize; material.pointSizeType = config.pointSizeType; //Potree.PointSizeType[config.pointSizeType]//Potree.PointSizeType.ADAPTIVE;//FIXED pointcloud.changePointSize(config.realPointSize); //material.size = config.pointSize; pointcloud.changePointOpacity(1); material.shape = Potree.PointShape.SQUARE; color && (pointcloud.color = pointcloud.material.color = color); pointcloud.timeStamp = timeStamp; //transformPointcloud(pointcloud, originDataset) scene.addPointCloud(pointcloud); { viewer.updateModelBound(); let {boundSize, center} = viewer.bound; viewer.dispatchEvent({type:'loadPointCloudDone'}); if(!Potree.settings.UserPointDensity){ Potree.settings.UserPointDensity = 'high';//'middle' } Potree.Log('loadPointCloudDone 点云加载完毕', {font:[null,10] }); } /* Potree.loadPanos(dataset.id, (data) => { //暂时不加载panos了,因为没有id //console.log('loadPanos',dataset.sceneCode, dataset.id, data) viewer.images360.addPanoData(data, dataset.id ) viewer.images360.loadDone() viewer.scene.add360Images(viewer.images360); */ viewer.dispatchEvent('allLoaded'); done(pointcloud); },onError); }; if(type == 'laser'){ let sceneCode = url; Potree.loadDatasets((data)=>{ let originDataset = data.find(e=>e.sceneCode == sceneCode);//只加载初始数据集 let timeStamp = originDataset.updateTime ? originDataset.updateTime.replace(/[^0-9]/ig,'') : ''; //每重算一次后缀随updateTime更新一次 //let cloudPath = `${Potree.settings.urls.prefix1}/${Potree.settings.webSite}/${sceneCode}/data/${sceneCode}/webcloud/cloud.js` let cloudPath = `${Potree.settings.urls.prefix1}/${originDataset.webBin}`; //webBin添加原因:每次裁剪之类的操作会换路径,因为oss文件缓存太严重,更新慢 loadCloud({cloudPath, sceneName:originDataset.sceneName, sceneCode, timeStamp, color:originDataset.color}); }, sceneCode, onError); }else {//las or ply 直接用url let name = type + '|' + id + '|' + title; if(url instanceof Array){ if(url.length == 1){ url = url[0]; }else { console.error('有多个点云?暂时还不支持', url, name);//多个点云要一起移动没想好怎么写 } } let cloudPath = url + '/cloud.js'; loadCloud({cloudPath, sceneName:name, sceneCode:name, timeStamp:'' }); } }; let setMatrix = (pointcloud)=>{//为了漫游点变换,要算一下 类似setMatrix /* pointcloud.transformMatrix = new THREE.Matrix4().multiplyMatrices(pointcloud.matrix, pointcloud.pos1MatrixInvert)//还原一点位移 pointcloud.transformInvMatrix.copy(pointcloud.transformMatrix).invert() pointcloud.rotateMatrix = new THREE.Matrix4().makeRotationFromEuler(pointcloud.rotation); pointcloud.rotateInvMatrix.copy(pointcloud.rotateMatrix).invert() pointcloud.panos.forEach(e=>e.transformByPointcloud()) */ //pointcloud.updateBound() //pointcloud.getPanosBound() viewer.updateModelBound(); }; let moveModel = (e)=>{//根据鼠标移动的位置改变位置 let camera = viewer.mainViewport.camera; var origin = new Vector3(e.pointer.x, e.pointer.y, -1).unproject(camera), end = new Vector3(e.pointer.x, e.pointer.y, 1).unproject(camera); var dir = end.sub(origin); let planeZ = 0; let r = (planeZ - origin.z)/dir.z; let x = r * dir.x + origin.x; let y = r * dir.y + origin.y; //过后改为根据intersect的点来设置底部高度;这样的话,需要发送高度 /*let pos = new THREE.Vector3(x,y, planeZ ) modelEditing.updateMatrixWorld() let boundCenter = modelEditing.boundingBox.getCenter(new THREE.Vector3).applyMatrix4(modelEditing.matrixWorld); */ MergeEditor.moveBoundCenterTo(modelEditing,new Vector3(x,y, modelEditing.boundCenter.z)); //使模型中心的xy在鼠标所在位置 modelEditing.dispatchEvent("position_changed"); }; let cancelMove = ()=>{ modelEditing = null; viewer.removeEventListener('global_mousemove', moveModel); viewer.removeEventListener('global_click', confirmPos); }; let confirmPos = ()=>{ MergeEditor.focusOn(modelEditing); cancelMove(); return {stopContinue:true} }; let modelType, modelEditing, MergeEditor = viewer.modules.MergeEditor; Potree.addModel = function(prop, done, onProgress, onError){ //加载模型 let loadDone = (model)=>{ model.dataset_id = prop.id; //唯一标识 if(prop.position){ model.position.copy(prop.position); } if(prop.rotation){ //model.rotation.setFromVector3(prop.rotation) model.rotation.copy(prop.rotation); } if(prop.scale){ model.scale.set(prop.scale,prop.scale,prop.scale); } if(model.isPointcloud){ model.renderOrder = Potree.config.renderOrders.model; //same as glb } if(Potree.settings.maintainBtmZ) {//transform --------维持离地高度和中心点的版本(local ver) let updateBound = ()=>{ model.updateMatrixWorld(); viewer.updateModelBound(); }; let maintainBtmZAndCenter = ()=>{ MergeEditor.maintainBoundXY(model); MergeEditor.setModelBtmHeight(model); updateBound(); model.dispatchEvent('transformChanged'); }; model.addEventListener('position_changed', ()=>{ updateBound(); MergeEditor.getBoundCenter(model);//更新boundcenter MergeEditor.computeBtmHeight(model); if(prop.bottomRange && (model.btmHeight > prop.bottomRange.max || model.btmHeight < prop.bottomRange.min)){ model.btmHeight = MathUtils.clamp(model.btmHeight, prop.bottomRange.min, prop.bottomRange.max); MergeEditor.setModelBtmHeight(model); updateBound(); } model.dispatchEvent('transformChanged'); }); model.addEventListener("rotation_changed", maintainBtmZAndCenter ); model.addEventListener("scale_changed", maintainBtmZAndCenter ); model.addEventListener('transformChanged', ()=>{ MergeEditor.modelTransformCallback(model); }); //离地高度只是boundingbox在transform后的最低点的高度,而非模型transform后的最低点的高度,所以旋转过后看起来不太准确 } else {//transform --------维持中心点的版本 let updateBound = ()=>{ model.updateMatrixWorld(); viewer.updateModelBound(); }; let maintainCenter = ()=>{ //MergeEditor.maintainBoundXY(model) MergeEditor.maintainBoundCenter(model); updateBound(); model.dispatchEvent('transformChanged'); }; model.addEventListener('position_changed', ()=>{ updateBound(); MergeEditor.getBoundCenter(model);//更新boundcenter model.dispatchEvent('transformChanged'); }); model.addEventListener("rotation_changed", maintainCenter ); model.addEventListener("scale_changed", maintainCenter ); model.addEventListener('transformChanged', ()=>{ MergeEditor.modelTransformCallback(model); }); } model.updateMatrixWorld(); viewer.updateModelBound(); MergeEditor.getBoundCenter(model); //初始化 model.lastMatrixWorld = model.matrixWorld.clone(); done(model); // 先发送成功,因为2d界面会随机执行changePosition等初始化,然后这边再将模型移到中心地面上 if(prop.isFirstLoad){ MergeEditor.moveBoundCenterTo(model, new Vector3(0,0,0)); MergeEditor.setModelBtmHeight(model, 0); //初始加载设置离地高度为0 if(prop.mode != 'single'){//如果不是模型展示页,模型会随着鼠标位置移动 modelEditing = model; /* if(model.fileType == '3dTiles'){ setTimeout(()=>{ moveModel({pointer:{x:0,y:0}}) //3dTiles的移动会错乱,先默认放在当前视图中间吧 confirmPos() },1) }else{ */ viewer.addEventListener('global_mousemove', moveModel); viewer.addEventListener('global_click', confirmPos, {importance:3}); //} } model.dispatchEvent("position_changed"); }else { //MergeEditor.setModelBtmHeight(model, prop.bottom || 0) //默认离地高度为0 modelEditing = null; } MergeEditor.modelAdded(model); }; if(prop.type == 'glb'){ let callback = (object)=>{ //focusOnSelect(object, 1000) object.isModel = true; //object.dataset_id = Date.now() //暂时 object.traverse(e=>e.material && (e.material.transparent = true)); /* object.addEventListener('click',(e)=>{ //只是为了能得到hoverElement识别才加这个侦听 }) */ loadDone(object); }; let info = { fileType: prop.type, id: prop.id, //unlit:true, url : prop.url, name : prop.title, /* transform : { position : prop.position, rotation : new THREE.Euler().setFromVector3(prop.rotation), scale: new THREE.Vector3(prop.scale,prop.scale,prop.scale), } */ }; viewer.loadModel(info , callback, onProgress, onError); }else if(prop.type == 'osgb' || prop.type == 'b3dm'){ //3d tiles let callback = (object)=>{ object.isModel = true; //透明度怎么办 //object.traverse(e=>e.material && (e.material.transparent = true)) loadDone(object); }; viewer.loadModel({ fileType: '3dTiles', id: prop.id, name : prop.title, maximumScreenSpaceError: prop.maximumScreenSpaceError, /* tilesUrl: 'https://4dkk.4dage.com/scene_view_data/SS-Ds19qsmuFA/images/3dtiles/tileset.json', transform : { rotation : [Math.PI/2, 0, 0], position : [0,0,0] } tilesUrl: 'https://testgis.4dage.com/LVBADUI_qp/tileset.json', transform : { rotation : [0, 0, 0], position : [0,0,0] } */ url:prop.url, },callback,onprogress); }else if(prop.type == 'shp'){ viewer.loadModel({ fileType: 'shp', id: prop.id, name : prop.title, url:prop.url, },callback,onprogress); //shpModel.position.set(-330000, 900000,10)//尽量移动到原点。原位置在江门那 }else if(prop.type == '3dgs'){ let callback = (object)=>{ object.isModel = true; loadDone(object); }; viewer.loadModel({ fileType: '3dgs', id: prop.id, name : prop.title, url:prop.url, },callback,onprogress); }else { //else if(prop.type == 'las' || prop.type == 'ply' || prop.type == 'laz' ) Potree.loadPointCloudScene(prop.url, prop.type, prop.modelId, prop.title, (pointcloud)=>{ pointcloud.matrixAutoUpdate = true; pointcloud.initialPosition = pointcloud.position.clone(); pointcloud.pos1MatrixInvert = new Matrix4().setPosition(pointcloud.initialPosition).invert(); /* let maintainBtmZ = ()=>{ MergeEditor.setModelBtmHeight(pointcloud) updateMatrix() } let updateMatrix = ()=>{ setMatrix(pointcloud) pointcloud.dispatchEvent('transformChanged') } pointcloud.addEventListener('position_changed', updateMatrix ) pointcloud.addEventListener("orientation_changed", maintainBtmZ ) pointcloud.addEventListener("scale_changed", maintainBtmZ ) */ loadDone(pointcloud); /* pointcloud.addEventListener('select',(e)=>{ if(Potree.settings.displayMode == 'showPanos')return console.log('select',e) //viewer.setControls(viewer.orbitControls) MergeEditor.focusOnSelect(pointcloud) viewer.outlinePass.selectedObjects = [pointcloud] return {stopContinue:true} },{importance:1}) pointcloud.addEventListener('deselect',(e)=>{ console.log('deselect',e) //viewer.setControls(viewer.fpControls) viewer.outlinePass.selectedObjects = [] }) */ }, onError); } }; return {THREE: THREE$1} } var changeLog = ()=>{ var textarea = document.createElement('textarea'); textarea.id = "consoleLog"; textarea.style.width = '160px'; textarea.style.height = '200px'; textarea.style.position = 'fixed'; textarea.style.left = 0; textarea.style.bottom = '50px'; textarea.style['z-index'] = 9999; textarea.style.color = 'black'; textarea.style.opacity = 0.9; textarea.style['font-size'] = '12px'; textarea.style['backgroundColor'] = '#ffffff'; document.getElementsByTagName("body")[0].appendChild(textarea); var list = ["log", "error", "warn", "debug", "info", "time", "timeEnd"]; var exchange = function (o) { console["old" + o] = console[o]; console[o] = function () { var args = Array.from(arguments); console["old" + o].apply(this, arguments); var t = document.getElementById("consoleLog").innerHTML; var str = ''; args.forEach(a=>{ str += a + ' '; }); document.getElementById("consoleLog").innerHTML = str + "\n\n" + t; }; }; for (var i = 0; i < list.length; i++) { exchange(list[i]); } }; /* 坐标转换问题: 由于控制点可以随便输入,所以本地和地理位置的转换也是可拉伸的。而navvis的转换是等比由中心展开, 所以对比两种转化方式时误差较大。 另外地理注册控制点是有参考数据集的,若参考数据集和我放置在0,0,0的数据集一致,就可直接使用,否则要转换。 --------- lonlat和空间坐标其实并非线性关系,因为lonlat其实是角度。当两个数据集在地球两端时,它们之间的夹角都相差180度了。 所以若要准确展示的话,需要将点云内所有物体,如漫游点,都先获取lonlat再去算local。或者直接将点云整体的transformMatrix考虑上在地球上相对于初始数据集的偏转。 支持ctrl+z、ctrl+Y 撤销回退的页面有: 测量、土方量、空间模型 这三个页面的点线拖拽;点云裁剪、点云下载中的裁剪 的框; 点云编辑的变换; 数据集校准; (所有数据一旦删除则无效 ) 其他快捷键: 按alt鼠标滚轮或WS键放慢。 测量or土方量: 按Alt键可以平行拖拽点。&dragPolyBeyondPoint 后缀则可平行拖拽到无点云区域 。 按M键拖拽点可以复制出当前点 点云按空格键+左键拖拽场景,可以不改相机位置的旋转视角 */ //xzw add const config$1 = {//配置参数 不可修改 displayMode:{ showPointCloud:{ atPano:{ showPoint : true, showSkybox: false, pointUsePanoTex : false }, transition:{ showPoint: true, showSkybox: false, pointUsePanoTex: false }, canLeavePano: true //是否能离开pano位置 }, showPanos:{ atPano:{ showPoint : false, showSkybox: true, pointUsePanoTex : false }, transition:{ //showPoint: true, showSkybox: true, //pointUsePanoTex: true //是否使用全景贴图 }, canLeavePano: false }, showBoth:{ atPano:{ showPoint : true, showSkybox: true, pointUsePanoTex : false //? }, transition:{ showPoint: true, showSkybox: true, pointUsePanoTex: true }, canLeavePano: true //是否能离开pano位置 离开后自动变为showPointCloud }, //test: pointUsePanoTex:{ //---静止时调点云 atPano:{ showPoint : true, showSkybox: false, pointUsePanoTex : true }, transition:{ showPoint: true, showSkybox: true, pointUsePanoTex: true //是否使用全景贴图 }, canLeavePano: false }, }, urls:{ //localTextures:'../resources/textures/', prefix1: 'https://laser-oss.4dkankan.com',//oss prefix2: 'https://testlaser.4dkankan.com', prefix3: 'https://4dkk.4dage.com', prefix4: 'https://uat-laser.4dkankan.com',//test.4dkankan prefix5: 'https://laser.4dkankan.com/backend', prefix6: 'https://mix3d.4dkankan.com/backend', //融合 prefix7: 'https://xfhd.4dkankan.com/backend', //融合 }, transitionsTime:{ flyMinTime : 650 , // 毫秒/米 flytimeDistanceMultiplier: 120 , } , view:{ fov:70, //navvis:50 near:0.1, far: 10000, }, map:{//mapViewer mapHeight : -1000,//要比点云低。最低 cameraHeight : 1000, //最高 ,注意(如sitemodel)其他的物体不能超过这个高度 }, minNodeSize:30, // perspectiveCamera允许加载的node的最小可见像素宽度。越大越省性能 tiles3DMaxMemory: 100,//M. 最大支持3dTiles的内存大小 超出会崩溃。 改太小太大都会卡,太大崩溃 pointDensity:{ magnifier:{ maxLevelPercent: 1, pointBudget : 1*1000*1000, //至少显示这么多 minNodeSize : 5, //pick时调高精度 }, panorama:{//显示全景时的漫游。因为点只能显示1个像素的大小,所以必须很密集,但又要限制点的数量 maxLevelPercent: 0.6, pointBudget : /* 4*1000*1000// */browser.isMobile() ? 0.2*1000*1000 : 0.4*1000*1000, //点云总最大数 minNodeSize : 100, }, fourViewports:{//分四屏时防止卡顿 maxLevelPercent: 0.9, pointBudget :3*1000*1000, // 只要限制这个就足够 (为什么分屏focus区域不同会闪烁,navvis不会)(navvis:maxLevel:5,pointBudget:1*1000*1000) minNodeSize : 70, }, fourViewportsMain:{//分四屏时防止卡顿 maxLevelPercent: 0.9, pointBudget :3*1000*1000, // 只要限制这个就足够 (为什么分屏focus区域不同会闪烁,navvis不会)(navvis:maxLevel:5,pointBudget:1*1000*1000) minNodeSize : 70, } , panoEdit:{ maxLevelPercent: 1, //在远处时由于pointBudget限制而展示稀疏,凑近时就变为最高质量了 pointBudget :4*1000*1000, //要使点云达到200个以上时还不卡 percentByUser:true, minNodeSize : 80 , //点云多的话远处的尽量就不可见吧 }, low:{//highPerformance maxLevelPercent: 0.4, //最小为0 percentByUser:true, //如果用户定义了percent,使用用户的 pointBudget : browser.isMobile() ? 1*1000*1000 : 2*1000*1000, minNodeSize : 40 / window.devicePixelRatio , }, middle:{//balanced //不同场景相同级别所产生的numVisibleNodes和numVisiblePoints不同,如果分层比较细,可能要到level8才能看清,那么level5看到的点就很大且很少,如隧道t-e2Kb2iU maxLevelPercent: 0.7, percentByUser:true, pointBudget: browser.isMobile() ? 1.5*1000*1000 : 3.5*1000*1000, minNodeSize : 30 / window.devicePixelRatio , }, high:{//highQuality maxLevelPercent: 1, percentByUser:true, pointBudget:browser.isMobile() ? 3*1000*1000 : 6*1000*1000, //原本最高是8,但是大部分电脑都太卡了,降 minNodeSize : 20 / window.devicePixelRatio , //手机上因为像素点小,远一点的时候更需要加载密集的点云。(没事,有pointBudget限制着,会先从近处加载高级node,再远就不加载了) }, screenshot:{ maxLevelPercent: 1, pointBudget: browser.isMobile() ? 4*1000*1000 : 10*1000*1000, //一般只有电脑需要截图,手机的加载多会崩 minNodeSize : 40 / window.devicePixelRatio , }, screenshot2:{ maxLevelPercent: 1, pointBudget: browser.isMobile() ? 8*1000*1000 : 15*1000*1000, minNodeSize : 20 / window.devicePixelRatio , }, ultraHigh:{ maxLevelPercent: 1, pointBudget: 20*1000*1000, minNodeSize : 10 / window.devicePixelRatio, } //数值由testLevelSteps得来,其中nodeMaxLevel为2时,low和middle的都是1,如果真有这么低的点云就单独处理下。 //多个viewport尽量保证pointBudget一样,或者pointBudget不能太低于所需,否则会反复加载了又清除 }, clip:{ color: '#FFC266', //map }, measure:{ color:'#00C8AF', default:{ color:"#64C8BB",//"#00c7b2", opacity:0.7 }, highlight:{ color:'#00C8AF',//"#00c7b2", opacity:1, labelOpacity:0.7//1会挡住线和端点 }, guide:{ color:'#FFFFFF', opacity:0.8 } , backColor:'#333333', lineWidth: 3, textColor: "#000000", //"#FFFFFF" mulLabelHideFaraway : false ,// 多折线根据远近显示label adsorptMinDis : 30, //最小吸附距离(像素) }, material:{//初始化 pointSize: 0.1, realPointSize : 0.1,//实际上的ui滑动条默认大小(兼容旧的版本) minSize: 0.1, maxSize: 10000, pointSizeType: 'ATTENUATED', //'ADAPTIVE'//'ADAPTIVE' \ FIXED //ADAPTIVE的在小房间里大小会不太匹配,但在远景似乎更好 /* ATTENUATED : 衰减 真实大小,靠近时感觉是点云一点点变多,缝隙变小 ADAPTIVE: 自适应 大小根据level变化,越高越小。靠近时感觉点云由大慢慢细分成小份。这个感觉更佳但是navvis为何不用这个 */ absolutePanoramaSize: 1.3 ,//全景漫游时的size 是fixed的模式 //sizeAtPanoRtEDL : 2000, pointColor:'#ffffff', //sizeAddAtPanoRtEDL : 0.5, //全景模式静止时的size //ADAPTIVE : 字会失真扭曲 //'ATTENUATED' 往旁边看有缝隙、点在浮动 } , skyboxBgWidth : 100 , renderLayers:{//渲染层,方便分批渲染管理,替代scene的创建。数字不代表顺序。(数字不能太大) bg: 20, bg2: 21, skybox: 1, pointcloud: 11, sceneObjects:0,//default model : 2, light: 15, measure:4, magnifier:5, magnifierContent:16, volume:6, transformationTool:7, map:8, mapObjects:9,//default bothMapAndScene: 3, siteModeOnlyMapVisi:12,//只能mapViewer可见 siteModelMapUnvisi:13,//只有mapViewer不可见 siteModeSideVisi:14,//只有侧面可见 layer1: 18,// 备用1 layer2: 17,// 备用2 }, renderOrders:{ //会影响到绘制、pick时的顺序。 model:10, reticule:5, measureMarker: 6, measureLabelSub: 7, measureLabel: 8, sorptionSign:10, model:10, magnifier:50, }, siteModel:{ names:{ 'building': '建筑', 'floor':'楼层', 'room':'房间' }, floorHeightDefault: 5,//一层楼的高度 }, panosEdit:{ }, tiling: { panoPreRenderRepeatDelay: 2500, panoPreRenderDelay: 500, preRenderTourPanos: browser.valueFromHash("tileprerender", 0), tilingFlagNames: ["usetiles", "tiles"], maxNavPanoQuality: browser.valueFromHash("maxtileq", null), maxZoomPanoQuality: browser.valueFromHash("maxztileq", null), overlayStyle: browser.valueFromHash("tileoverlay", 0), uploadIntervalDelay: browser.valueFromHash("tileupdelay", 10 ), initialIntervalDelay: browser.valueFromHash("itiledelay", 0), maxNonBaseUploadsPerFrame: browser.valueFromHash("maxnbtpf", 1), maxBaseUploadsPerFrame: browser.valueFromHash("maxbtpf",6), customCompression: browser.valueFromHash("tilecustcomp", 0), mobileHighQualityOverride: !1, allowUltraHighResolution: !0 }, navigation: { panoScores: !1, mouseDirection: !0, filterStrictness: .75, angleFactor: -30, directionFactor: 10, distanceFactor: -1, optionalityFactor: 3 } , axis : { 'x':{color:'#ea3f3f'/* '#d0021b' */ /* 'red' */}, 'y':{ color:'#86c215' /* '#86c542' *//* 'green' */}, 'z': {color:'#3396f8'/* '#3399c8' */ /* 'blue' */}, 'xyz':{color:'#ccc',}}, shelterMargin:0.15, //多少米内不算遮挡 (有的场景深度图不准,和点云差别蛮大如SG-t-24F0iT3pKAO) highQualityMaxZoom: 2, ultraHighQualityMaxZoom: 3, panoFieldRadius : 10, //当前位置多远范围内可以切全景模式 clickMaxDragDis:3, clickMaxPressTime:200, //ms doubleClickTime:300,//双击间隔时间 testNodeCount1: browser.isMobile() ? 6 : 4, //testMaxNode次数达到这个数字时,changePointSize才使用nodeMaxLevel。 (调试时比较卡,在线上实际只需要3) background: '#232323', mapBG:/* '#232323', */ '#F5F5F5', //地图的clearColor pickFrontPointRatio:50, colors: { //from navvis red: [213,0,0], pink: [197,17,98], purple: [170,0,255], "deep purple": [98,0,234], blue: [ 41,98,255], "light blue": [ 0,145,234], cyan: [ 0,184,212], teal: [ 0,191,165], green: [0,200,83], "light green": [ 100,221,23], lime: [ 174,234,0], yellow: [ 255,214,0], amber: [ 255,171,0], orange: [ 255,109,0], "deep orange": [ 255,61,0], }, depthTexUVyLimit: 0.141, // 在这个范围内是没有深度的,从图片算的0.14003, 设置为稍大于这个数值 }; config$1.OrthoCameraLimit = { standard:{ zoom:{min:0.0004, max:500}, //如果camera缩太小,地图会因为数字边界问题而扭曲 latPad:20, xBound:[-4e6, 4e6], }, expand:{ zoom:{min:0.0004, max:500}, //如果camera缩太小,地图会因为数字边界问题而扭曲 latPad:20, xBound:[-6e6, 6e6], }, }; /* 显示模式: 1只显示点云: 滚轮为前进后退,方向键可以行走。进入漫游点时自动变为混合(这样全景可以弥补缝隙),过渡时只显示点云。 2只显示全景: 不能任意行走。 过渡时显示贴图材质非edl的点云(否则有折痕不贴合)。 3混合:都显示。 不能任意行走。过渡时显示贴图材质非edl的点云(因为只显示点云的话不太美,点云很碎,不细腻) */ window.testLevelSteps = function(steps){//[0.4,0.7,1] if(!steps){ let s = Potree.config.pointDensity; steps = [s.low.maxLevelPercent, s.middle.maxLevelPercent, s.high.maxLevelPercent, ]; } let max = 1; while(++max<=12){ let r1 = steps.map(e=>e * max); let r2 = steps.map(e=>Math.round(e * max)); console.log(`当nodeMaxLevel为${max}时,每一级的level分别为${r2}, (小数:${r1})`); } console.log('请检查每一层的三个level是否有重复'); }; function getPrefix(){ let u = window.location.href.split('//'); let v = u[1].split('/'); return v[0] } let isTest = browser.urlHasValue('test'); let settings = {//设置 可修改 editType : '', number: '', //场景序号 originDatasetId:'',//场景原本的数据集id,应该就是数据集第一个吧 isOfficial:false, webSite:'testdata',//正式:'datav1', //不同环境对应的静态文件的地址不同 isLocal:false, //是否本地 局域网版本 libsUrl:'../libs/', displayMode:'', isTest , prefix: getPrefix(), pointDensity: '', UserPointDensity:'',//pointDensity会随着进入不同的模块而自动改变,UserPointDensity记录了用户的设置 UserDensityPercent:null,//点云密度百分比 ifShowMarker:true,//显示漫游点 floorplanType:{},//平面图类型 'default' | 'diy' 不同数据集不同{datasetId:...} floorplanEnable:false, floorplanEnables:{}, floorplanRequests:{},//开始加载了的 mapEnable:true,//地图区域是否加载地图 cameraFar : config$1.view.far, //相机最远范围 1-300 //limitFar: true, //是否使用setting的cameraFar来限制(如在点云裁剪时为false) showPanoMesh:false, //显示小球, dblToFocusPoint:false,//调试时如果需要双击飞向某个点云的点,就打开。此时不在漫游点的话单击将无法漫游。//因和单击漫游冲突 unableNavigate : false,//进入如裁剪界面时 禁止漫游 sizeFitToLevel: false,//当type为衰减模式时自动根据level调节大小。每长一级,大小就除以2 zoom:{ enabled : true, min:1, max: config$1.highQualityMaxZoom }, navConstantly:true, navTileClass: /* browser.isMobile() ? '1k' : */ '2k', //默认加载到 tileClass:'4k', //最高可达 /* loadTilesWhenUnfocus:false, //页面unfocus时也仍在加载tiles loadPointsWhenUnfocus:true, //页面unfocus时也仍在加载点云 */ //initialShowPano:true drawEntityData: false, //包括marker、线 zoomFromPointert:{//定点缩放(包括点云模式、全景模式、地图) whenPanos:true, whenPointCloud:true, map:true, }, rotAroundPoint:true,//点云模式是否能绕intersectPoint旋转 tourTestCameraMove:false, //测试镜头时,不移动真实的镜头, 只移动frustum cameraAniSmoothRatio : 20, //镜头动画平滑系数,越高越平滑 urls : $.extend({}, config$1.urls, { prefix : config$1.urls.prefix4 //主要使用的 是测试环境,根据不同工程更改 }), useDepthTex: true,//使用深度贴图,但不代表一定有(得到的intersect更快速准确和稳定) SS-t-7DUfWAUZ3V //matUseDepth:false, //panoEdit: datasetsPanos:{}, //mergeModel: boundAddObjs:false, intersectOnObjs:false, intersectWhenHover:true, depTexLocBindDataset: true,//是否在pano模式下,使用深度图得到intersect的话,改intersect能属于该pano所在的点云。也就相当于在全景模式下intersect的点属于该全景图 notAdditiveBlending:false, //点云是否使用普通的blend, 否则会曝光过渡 precision:2, // 两位小数 unit: 'm', useV4url:true, //v4的全景图等路径不一样 scene_view_data useRTskybox:true, //直接使用rtEDL绘制到屏幕,当是全景模式时. 在降4倍时能给render节省1毫秒,gpu时间未测 useRTPoint:true, //直接使用rtEDL绘制到屏幕,当是点云模式时。可以大大节省gpu时间。但有锯齿 pointEnableRT:false,//点云模式时是否绘制到rtEDL。如果不需要遮挡效果就不需绘制 cloudSameMat:true, //因为点云个数较多,就使用相同的材质,可见降低绘制速度(要保证所有点云的maxNodelevel一样,且要算出 material.spacing的平均值) showCompass : false, showAxis : isTest, //testCube : true, // moveToCenter:true, //针对数据集间隔很远的场景 dis>5000 容易抖动 tiles3DMaxMemory: config$1.tiles3DMaxMemory, adsorption:false,//测量时吸附点 pickFrontPointRatio:config$1.pickFrontPointRatio, //默认pick点云时选中靠近镜头的点的偏向 dragPolyBeyondPoint: browser.urlHasValue('dragPolyBeyondPoint'), //ctrlPolygon是否可以拖拽到没点云的地方 panoZoomByPointer: false,//全景图是否定点缩放 areaAtNotPlane: false, fastTran: isTest }; Potree.config = config$1; Potree.settings = settings; settings.isLocalhost = settings.prefix.includes('localhost:') || settings.prefix.includes('192.168.0.113:'); settings.isFormal = browser.urlHasValue('formal');//正式环境 本地测试 if(settings.isFormal){ settings.urls.prefix = settings.urls.prefix5; settings.webSite = 'datav1'; } /* let sid = 0 let getName = ()=>{ return sid ++ } */ class BasicMaterial extends ShaderMaterial{ constructor(o={}){ super( Object.assign({},{ uniforms:{ color: {type:'v3', value: o.color || new Color("#FFF")} , map: {type: 't', value: o.map }, opacity : {type:'f', value : o.opacity == void 0 ? 1 : o.opacity } }, vertexShader: Shaders['basicTextured.vs'], fragmentShader: Shaders['basicTextured.fs'], defines:{HasColor:'' } },o)); //this.name111 = getName() } copy(source){ super.copy(source); //console.log('copy', source.name111, this.name111, !!source.map ) this.map = source.map; return this } set opacity(o){ this.uniforms && (this.uniforms.opacity.value = o); } get opacity(){ return this.uniforms.opacity.value } set map(o){ this.uniforms.map.value = o; if(o){ this.defines.HasMap = ''; }else { delete this.defines.HasMap; } //可能需要needsUpdate //console.log('hasMap', !!o, this.name111 ) } get map(){ return this.uniforms.map.value } } let texLoader$2 = new TextureLoader(); texLoader$2.crossOrigin = "anonymous"; let createErrorMaterial = function() { var t = new MeshBasicMaterial({ transparent: !0, depthWrite: !1, depthTest: !0, opacity: 1, side: DoubleSide }); return t.color = new Color(3355443), t }; let tempVector = new Vector3, //sharedata face1 = new Face3(0,1,2), face2 = new Face3(2,3,0), errorMaterial = createErrorMaterial(), uv00 = new Vector2(0,0), uv01 = new Vector2(0,1), uv10 = new Vector2(1,0), uv11 = new Vector2(1,1), face1UV = [uv00, uv10, uv11], face2UV = [uv11, uv01, uv00]; const HALF_WORLD_SIZE = 21e6; //略大于半个周长(mapSizeM/2) const MAX_VERTICAL_DIST = 2; const MAX_VERTICAL_DIST_TO_BEST = 1; function defineLocalProj(locationLonLat){ proj4.defs("LOCAL_MAP", "+proj=tmerc +ellps=WGS84 +lon_0=" + locationLonLat[0].toPrecision(15) + " +lat_0=" + locationLonLat[1].toPrecision(15)); } const getSid = (function(){ let sid = 0; return function(){ return sid++ } })(); //高德坐标拾取工具 : https://lbs.amap.com/tools/picker class MapLayer extends EventDispatcher{ // 包括了 MapLayerBase SceneLayer constructor(viewer_, viewport){ super(); this.sceneGroup = new Object3D; this.sceneGroup.name = "MapLayer"; this.waitQueue = []; //等待加载的 this.loadingInProgress = 0; this.maps = []; this.frustum = new Frustum; this.frustumMatrix = new Matrix4; this.tileColor = new Color(16777215); this.viewport = viewport; this.changeViewer(viewer_); //添加地图 var map = new TiledMapOpenStreetMap(this, this.tileColor ); this.addMap(map); //map.setEnable(false) this.sceneGroup.addEventListener('isVisible',()=>{ this.viewer.mapChanged = true; }); } addMapEntity(data, datasetId){ if(!data || !data[0]){ Potree.Log('平面图无数据',{font:'red'}); return } data[0].datasetId = datasetId; var floorplan = new TiledMapFromEntity(this, this.tileColor, data[0] );//[0]? if(floorplan){ floorplan.name += "_"+ datasetId; this.addMap(floorplan); floorplan.updateProjection(); floorplan.updateObjectGroup(); let visible = false; if(datasetId in Potree.settings.floorplanEnables){ visible = Potree.settings.floorplanEnables[datasetId]; }else { visible = Potree.settings.floorplanEnable; } if(visible){ this.needUpdate = true; }else { floorplan.setEnable(false); } this.dispatchEvent({type:'floorplanLoaded', floorplan}); } return floorplan } getFloorplan(datasetId){ return this.maps.find(e=>e.name == 'floorplan'+"_"+ datasetId ) } addMap(t){ this.maps.push(t); //this.view.invalidateScene() this.needUpdate = true; this.viewer.mapChanged = true; } removeMap(t){ var e = this.maps.indexOf(t); if(e >= 0){ t.removeFromSceneGroup(this.sceneGroup); this.maps.splice(e, 1); } /* this.view.invalidateScene() */ this.needUpdate = true; this.viewer.mapChanged = true; } changeViewer(viewer_){//add this.viewer = viewer_; } initProjection(){ this.maps.forEach(map=>{ map.updateProjection(); map.updateObjectGroup(); }); } visibilityChanged(){ if (!this.visible) for (var t = 0, e = this.maps; t < e.length; t++){ e[t].removeFromSceneGroup(this.sceneGroup); } } update(){ this.needUpdate = false; if(this.disabled || !this.maps.find(e=>!e.disabled) || !this.maps.find(e=>e.objectGroup.visible) )return //add this.viewer.mapChanged = true; var e, n, i, r, o; this.updateTimer = void 0, e = this.viewport.camera, n = e.projectionMatrix.clone(); let expandRatio = 1.3; n.elements[0] /= expandRatio; n.elements[5] /= expandRatio; // 为了缓存吗,使边界处也提前加载,扩大显示区域 this.frustumMatrix.multiplyMatrices(n, e.matrixWorldInverse), this.frustum.setFromProjectionMatrix(this.frustumMatrix), this.frustum.planes[4].setComponents(0, 0, 0, 0), this.frustum.planes[5].setComponents(0, 0, 0, 0), i = !0; //console.log('-------------update-----------') for (r = 0; r < this.maps.length; r++){ var map = this.maps[r]; i = map.update(this.frustum, this.sceneGroup) && i; } return [2, i] } updateProjection(){ for (var t = 0, e = this.maps; t < e.length; t++){ var n = e[t]; n.clearProjection(), n.updateObjectGroup(); } } } class TiledMapBase extends EventDispatcher{ constructor( name, mapLayer, tileColor, projection){ super(); this.name = name; this.mapLayer = mapLayer, this.tileColor = tileColor, this.bias = 0; this.zIndex = -1; this.objectGroup = new Object3D; this.objectGroup.name = name; this.objectGroupAdded = !1, this.baseTile = new MapTile(this, this.objectGroup, this.tileColor, null, '0'), this.isTileVisibleBox = new Box3, this.isTileVisibleVec = new Vector3; this.projection = projection; this._zoomLevel = 0;//1-20 this.objectGroup.addEventListener('isVisible',()=>{ this.mapLayer.viewer.mapChanged = true; }); this.computeCount = 0; this.maxLoading = 3; this.loadFailCount = 0; this.loadingInProgress = 0; } get zoomLevel(){ return this._zoomLevel } set zoomLevel(zoomLevel){ if(this._zoomLevel != zoomLevel){ this._zoomLevel = zoomLevel; this.dispatchEvent({type:'zoomLevelChange',zoomLevel}); //if(this.name == 'map')console.log(zoomLevel,viewer.mapViewer.camera.zoom) } } updateObjectGroup(){ this.position && this.objectGroup.position.copy(this.position).setZ(0), this.quaternion && this.objectGroup.quaternion.copy(this.quaternion), this.objectGroup.updateMatrixWorld(!0); } updateProjection(){ if(!this.transformMapToLocal){ this.transformMapToLocal = proj4(this.projection, "LOCAL_MAP"); } } clearProjection(){ this.transformMapToLocal = void 0; this.projection !== 'LOCAL_MAP' && this.baseTile.remove(); } setEnable(enable){//add if(!this.disabled == enable)return if(enable){ //console.log('setEnable',true) } this.disabled = !enable; Potree.Utils.updateVisible(this.objectGroup, 'setEnable', enable); if(!enable){ this.baseTile.remove(); }else { this.mapLayer.needUpdate = true; } } update(e, n){ this.computeCount = 0; var unavailable = (this.disabled || !this.objectGroup.visible);//地图即使不显示也要获得zoomlevel if(this.name != 'map' && unavailable)return this.updateProjection(); if(!this.transformMapToLocal)return if (!this.isTileVisible(new Vector3(0,0,0), this.mapSizeM, e)) return this.removeFromSceneGroup(n), !0; let viewport = this.mapLayer.viewport; var i = new Vector3(-.5 * this.mapSizeM,0,0); i.applyMatrix4(this.objectGroup.matrixWorld), i.project(viewport.camera); var o = new Vector3(.5 * this.mapSizeM,0,0); o.applyMatrix4(this.objectGroup.matrixWorld), o.project(viewport.camera); var a = viewport.resolution.x , s = viewport.resolution.y; if (a <= 0 || s <= 0 || isNaN(i.x) || isNaN(o.x)) return !1; i.sub(o), i.x *= a / 2, i.y *= s / 2; let scale; if(this.name == 'map'){ //add 高纬度的因倾斜而造成tile较小,所以放大些,否则会造成显示的tile过多而卡 let lonlat = viewer.transform.lonlatToLocal.inverse(viewport.camera.position.clone()); let cos = Math.cos(MathUtils.degToRad(lonlat.y)); //越小就在纬度上越高,tile表现越小 //为什么lonlat.y会超出90? /* if(lonlat.y>90){ console.log('lonlat.y>90',lonlat.y) } */ cos = MathUtils.clamp(cos, 0,1); let lonShift = Math.abs(viewer.mapViewer.camera.position.x / this.mapSizeM * 16 ); //越大就在经度离中心越远,tile表现越大 。 lonShift = MathUtils.clamp(lonShift, 0, Math.PI); lonShift = (1 - Math.sin( 1/2 * lonShift + Math.PI/2 )) * Math.PI; // 0-Math.PI sin增速向上 scale = 0.5 * cos * (1+lonShift) + 0.5 * Math.pow(cos, lonShift); }else { scale = 1; } var c = this.tileSizePx / i.length() / scale //多除以一个scale缩放因子,scale越大level越小 , level = Math.ceil(-Math.log(c) / Math.log(2) - this.bias); level = Math.max(level, 0); level = Math.min(level, void 0 === this.maxDepth ? 1 / 0 : this.maxDepth); this.zoomLevel = level;//add /* if(isNaN(this.zoomLevel )){ console.log(level, cos , scale , lonlat ) } */ if(!unavailable){ this.addToSceneGroup(n); return this.baseTile.update(this, e, level, this.mapSizeM, 0, 0, "") } } isTileVisible(e, n, i){ if (n > HALF_WORLD_SIZE) return !0; var r = .5 * n; //简单版: this.transformMapToLocal.forward(e); //e转化为local this.isTileVisibleBox.makeEmpty(); this.isTileVisibleVec.set(e.x - r, e.y - r, e.z).applyMatrix4(this.objectGroup.matrixWorld); this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec); this.isTileVisibleVec.set(e.x - r, e.y + r, e.z).applyMatrix4(this.objectGroup.matrixWorld); this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec); this.isTileVisibleVec.set(e.x + r, e.y - r, e.z).applyMatrix4(this.objectGroup.matrixWorld); this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec); this.isTileVisibleVec.set(e.x + r, e.y + r, e.z).applyMatrix4(this.objectGroup.matrixWorld); this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec); //仿造createMesh写的准确版,但会因为大的tile非矩形,而视口是矩形,若视口刚好在tile的曲线边缘外却识别为可见,就会创建冗余tile。 但上面那个简单版在zoomlevel低的时候地球边缘容易有识别不到的tile,造成黑色三角形。 //容易出现奇怪的mesh /* this.isTileVisibleBox.makeEmpty() this.isTileVisibleVec.set(e.x - r, e.y - r, e.z) this.transformMapToLocal.forward(this.isTileVisibleVec) this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld) this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec) this.isTileVisibleVec.set(e.x - r, e.y + r, e.z) this.transformMapToLocal.forward(this.isTileVisibleVec) this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld) this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec) this.isTileVisibleVec.set(e.x + r, e.y - r, e.z) this.transformMapToLocal.forward(this.isTileVisibleVec) this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld) this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec) this.isTileVisibleVec.set(e.x + r, e.y + r, e.z) this.transformMapToLocal.forward(this.isTileVisibleVec) this.isTileVisibleVec.applyMatrix4(this.objectGroup.matrixWorld) this.isTileVisibleBox.expandByPoint(this.isTileVisibleVec) */ return i.intersectsBox(this.isTileVisibleBox) } addToSceneGroup(t){ this.objectGroupAdded || (t.add(this.objectGroup), this.objectGroupAdded = !0); } removeFromSceneGroup(t){ this.baseTile.remove(), this.objectGroupAdded && (t.remove(this.objectGroup), this.objectGroupAdded = !1); } } const loadDone = (tile, success)=>{ tile.map.mapLayer.loadingInProgress--; tile.map.loadingInProgress--; //console.log('loaddone', tile.name, 'loadingInProgress',tile.map.mapLayer.loadingInProgress, Date.now()) tile.loading = false; let next = tile.map.mapLayer.waitQueue[0]; if(next){ addLoadTile(next); }else { if(tile.map.mapLayer.loadingInProgress == 0){//注意这时候不一定就加载完了,300ms后可能还会有新的tile加载 //console.log('loadDone All ?', Date.now()) tile.map.mapLayer.dispatchEvent('loadDone'); } } tile.mesh && (tile.mesh.material.needsUpdate = true); }; function addLoadTile(tile){ /* if(tile.texURL && tile.texURL.includes('testdata') ){ console.error('addLoadTile', tile.texURL.split('map_tiles/')[1] ) } */ if(tile.map.loadingInProgress < tile.map.maxLoading){ if(!tile.mesh)return; //有时候会遇到这种情况, 为什么没有被cancelLoad呢? tile.map.mapLayer.loadingInProgress++; tile.map.loadingInProgress ++; tile.map.mapLayer.dispatchEvent('startLoad'); //console.log('addLoad', 'loadingInProgress',tile.map.mapLayer.loadingInProgress, Date.now()) //tile.texURL && tile.texURL.includes('testdata') && console.log('startloadTile ', tile.texURL.split('map_tiles/')[1] ) tile.loading = true; let index = tile.map.mapLayer.waitQueue.indexOf(tile); index > -1 && tile.map.mapLayer.waitQueue.splice(index,1); let tex = tile.mesh.material.map = texLoader$2.load(tile.texURL, (tex)=>{ //如果一直加载不了会影响其他的加载,如google地图没有vpn会使全景图一直加载不了 if(tile.mesh){//如果还要显示的话 tile.textureLoaded = true; tile.mesh.material.opacity = 1; tile.map.mapLayer.viewer.mapChanged = true; tile.map.mapLayer.needUpdate = true; //表示还要继续update(以removeChildren) if(tile.map instanceof TiledMapOpenStreetMap){ tile.map.maxLoading = browser.isMobile() ? 5 : 10; } }else { //tile.texURL && tile.texURL.includes('testdata') && console.log('loadDone and dispose', tile.texURL.split('map_tiles/')[1] ) tex.dispose(); } loadDone(tile, true); } , void 0, (()=>{//error tile.textureLoaded = !0; if(tile.mesh){ tile.mesh.material.dispose(); tile.mesh.material = errorMaterial; tile.map.mapLayer.viewer.mapChanged = true; } loadDone(tile, false); tile.map.loadFailCount ++ ; if(tile.map instanceof TiledMapOpenStreetMap && Potree.settings.mapCompany == 'google' && tile.map.loadFailCount > 3){//极有可能没有vpn为了防止影响到其他资源加载,减少加载的个数 tile.map.maxLoading = 2; } })); tex.anisotropy = 0; tex.generateMipmaps = !1; tex.minFilter = LinearFilter; tex.magFilter = LinearFilter; }else { tile.map.mapLayer.waitQueue.includes(tile) || tile.map.mapLayer.waitQueue.push(tile); } } function cancelLoad(tile,log){//如果等待加载,但还没开始加载,取消加载 if(!tile.loading){ let index = tile.map.mapLayer.waitQueue.indexOf(tile); index > -1 && tile.map.mapLayer.waitQueue.splice(index,1); //index > -1 && tile.texURL && tile.texURL.includes('testdata') && console.log('cancelLoad', tile.texURL.split('map_tiles/')[1]/* , (log && waitQueue.indexOf(tile)>-1) ? log:'' , tile.loading */ ) } } class MapTile{ constructor(map, e, n, parent, name){ this.map = map; this.name = name; this.parent = parent; this.objectGroup = e, this.tileColor = n, this.meshAdded = !1, this.textureLoaded = !1, this.children = []; this.id = getSid(); } update(e, n, i, r, o, a, s){ return !!this.doesNotContainTilesToBeDisplayed(e) || (0 === i ? this.updateTile(e, r, o, a) : this.updateSubTiles(e, n, i, r, o, a, s)) } doesNotContainTilesToBeDisplayed(t){ return t.tilePresenceMap && t.tilePresenceMap.empty } updateTile(t, e, n, i){ //真正显示mesh的是这一层,最高level //if(this.map.name.includes('floorplan'))console.log('updateTile',this.name) if(!this.mesh){ this.createTileObject(t, e, n, i); } if(!this.meshAdded){ this.objectGroup.add(this.mesh); this.meshAdded = !0; } if(this.textureLoaded){//贴图加载完就不需要子集了 this.removeChildren(); }else { this.cancelChildren(); //add 停止加载子集 } return this.textureLoaded } updateSubTiles(entity, n, level, o, a, s, c){ //if(entity.name.includes('floorplan'))console.log('updateSubTiles',this.name) //名字越长代表level越高 for (var childrenLoaded = !0, u = [-.25 * o, .25 * o, -.25 * o, .25 * o], d = [.25 * o, .25 * o, -.25 * o, -.25 * o], p = 0; p < 4; ++p){ var h = c + p.toString(10); //一级(512):0 1 2 3分别为左上、右上、左下、右下。二级(1024)就是把一级的每一块分裂,如00 01 02 03分别是0的左上、右上、左下、右下…… if (!entity.tilePresenceMap || entity.tilePresenceMap[h]){ //去掉判断,直接显示 var f = a + u[p] , m = s + d[p]; tempVector.set(f, m, 0); this.map.computeCount ++; //console.log(this.map.computeCount, this.name, 'level:',level) if (entity.isTileVisible(tempVector, .5 * o, n)){ this.children[p] || (this.children[p] = new MapTile(this.map, this.objectGroup,this.tileColor, this, this.name+p )); //childrenLoaded = childrenLoaded && this.children[p].update(entity, n, level - 1, .5 * o, f, m, h) //这句会使若有一个tile还在加载,就阻断了。原版是这么写的。但是为了加快加载速度,改成下面两行。感觉直接全部updateTile也没太卡,不知道很大的场景会不会卡,单帧updateTile次数超过100次的话(应该不会吧,地图大小会限制住个数) -- 2023.12 let childLoaded = this.children[p].update(entity, n, level - 1, .5 * o, f, m, h); childrenLoaded = childrenLoaded && childLoaded; } else { if (this.children[p]){ this.children[p].remove(); delete this.children[p]; } } } } return childrenLoaded && this.removeObject3D(), childrenLoaded //子项加载完,母项mesh可以去除。(最后母项的母项以及前面的都会被删除,只留最后的叶子结点) } /* 一层层往后加载。加入第一次加载到第4层(因为level精细度是第4层),给第4层可见tile加上mesh。 然后下一次加载到第5层,那么第4层的mesh就要被清空(当它所属的第5层子集都加载完后) */ createTileObject(t, e, n, a){ var s = this; this.mesh = this.createMesh(t.transformMapToLocal, e, n, a), this.textureLoaded = !1; var c = t.mapSizeM / e , l = Math.log(c) / Math.log(2) , u = n / e + .5 * (c - 1) , d = -a / e + .5 * (c - 1) , p = t.getTileUrl(Math.round(l), Math.round(u), Math.round(d)); Potree.Utils.setObjectLayers(this.mesh, 'map' ); this.mesh.renderOrder = -(1e6 - l - 100 * (t.zIndex || 0)); this.mesh.name = this.name; //add this.texURL = p; /* let area = math.getArea(this.mesh.geometry.vertices.slice(0,3)); if(area >0){ this.mesh.visible = false console.log('area>0',this.mesh.name) } */ addLoadTile(this); } createMesh(t, e, n, o){ var a = new Geometry; return tempVector.set(n - e / 2, o - e / 2, 0), a.vertices.push(new Vector3().copy(t.forward(tempVector))), tempVector.set(n + e / 2, o - e / 2, 0), a.vertices.push(new Vector3().copy(t.forward(tempVector))), tempVector.set(n + e / 2, o + e / 2, 0), a.vertices.push(new Vector3().copy(t.forward(tempVector))), tempVector.set(n - e / 2, o + e / 2, 0), a.vertices.push(new Vector3().copy(t.forward(tempVector))), a.faces.push(face1), a.faces.push(face2), a.faceVertexUvs[0].push(face1UV), a.faceVertexUvs[0].push(face2UV), new Mesh(a,this.createMaterial()) } createMaterial(){ var t = new MeshBasicMaterial({ transparent: !0, depthWrite: !1, depthTest: !0, opacity: 0, side: DoubleSide, }); /* var t = new BasicMaterial({ transparent: !0, depthWrite: !1, depthTest: !0, opacity: 0, side: THREE.DoubleSide }) t.defines.InverseColor = '' */ if(Potree.settings.isTest) { var colorHue = Math.random(); t.color = new Color().setHSL(colorHue, 0.6, 0.92); }else { t.color = this.tileColor ? this.tileColor : new Color(16777215); } return t } traverse(f){//add return Mesh.prototype.traverse.call(this,f) } remove(){ this.removeObject3D(), this.removeChildren(); } removeObject3D(){ let hasMesh = !!this.mesh; if (this.mesh){ this.objectGroup.remove(this.mesh); if (this.textureLoaded){ var t = this.mesh.material.map; t && t.dispose(); }else { cancelLoad(this); } this.mesh.material.dispose(); //o.disposeMeshMaterial(this.mesh), this.mesh.geometry.dispose(); this.mesh = void 0; } this.meshAdded = !1, this.textureLoaded = !1; //this.texURL && this.texURL.includes('testdata') && console.log('removeObject3D', this.id, 'hasMesh',hasMesh, this.texURL.split('map_tiles/')[1] ) } removeChildren(){ for (var t = 0, e = this.children; t < e.length; t++){ var n = e[t]; n && (n.removeObject3D(), n.removeChildren()); } this.children.length = 0; } cancelChildren(){//子集全部停止加载 for (var t = 0, e = this.children; t < e.length; t++){ var n = e[t]; n && (cancelLoad(n, '提前'), n.cancelChildren()); } } } proj4.defs("EPSG:3857", "+title=WGS 84 / Pseudo-Mercator +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"); //这里地图世界的中心是不是lon:0,lat:0 class TiledMapOpenStreetMap extends TiledMapBase{ constructor(mapLayer, tileColor){ //Potree.settings.mapCompany = 'google' super('map', mapLayer, tileColor/* , projection */ ); //EPSG projection //this.baseUrl = "https://wprd03.is.autonavi.com/appmaptile?style=7&x=${x}&y=${y}&z=${z}", //this.baseUrl = "https://webrd01.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x=${x}&y=${y}&z=${z}" //最高只到18 level this.switchStyle(); this.tileSizePx = 256; this.mapSizeM = 40075017; //总占据多少米(地球赤道周长) 和三维空间的不一样 - -, 空间上的是直径,地图上的是半个圆周 this.bias = 0.5; if(Potree.settings.mapCompany == 'google'){ this.attribution = "© PopSmart, © 谷歌地图"; this.projection = "EPSG:900913"; //"EPSG:4326"//4550 }else { this.attribution = "© PopSmart, © 高德地图"; this.projection = "EPSG:3857"; } } getTileUrl(t, e, n){ return this.baseUrl.replace(/\${z}/, t.toString(10)).replace(/\${x}/, e.toString(10)).replace(/\${y}/, n.toString(10)) } switchStyle(style = 'standard'){ //if(Potree.settings.mapCompany == 'google')return if(style == this.style)return if(Potree.settings.mapCompany == 'google'){ if(style == 'satellite'){//卫星 this.baseUrl = "https://mt2.google.com/vt/lyrs=y@159000000&hl=zh-CN&gl=cn&x=${x}&y=${y}&z=${z}&s=mt1"; /* "http://mt2.google.cn/vt/lyrs=m@177000000&hl=zh-CN&gl=cn&src=app&x=${x}&y=${y}&z=${z}" */ //最高只到19 this.maxDepth = 22; }else { this.baseUrl = "https://mt2.google.com/vt/lyrs=m@159000000&hl=zh-CN&gl=cn&x=${x}&y=${y}&z=${z}&s=mt1"; /* "http://mt2.google.cn/vt/lyrs=m@177000000&hl=zh-CN&gl=cn&src=app&x=${x}&y=${y}&z=${z}" */ //最高只到19 this.maxDepth = 22; } /* 1)lyrs= 表示的是图层类型,即瓦片类型,具体含义如下: m:路线图 t:地形图 p:带标签的地形图 s:卫星图 y:带标签的卫星图 h:标签层(路名、地名等) 2)& gl=CN 谷歌地图针对中国有两套坐标,一套做了偏移,一套没有。经测试在url加入gl=cn地图会有偏移。 Tips:如果谷歌地图和RTK测量的WGS84坐标有偏差,可以尝试在url里去掉& gl=cn。 5)&hl= 设置地图注记文字语言类型,缺省默认为中文。 hl=nl 中英双语 hl=zh-CN 中文 */ }else { //baseUrl = "https://webst01.is.autonavi.com/appmaptile?lang=zh_cn&style=6&yrs=m&x=${x}&y=${y}&z=${z}" //卫星 maxDepth = 18 //搜索高德地图瓦片url if(Potree.settings.isJiangMen){ if(style == 'satellite'){//卫星 this.maxDepth = 18; this.baseUrl = "http://a.map.jms.gd/tile/weixing/${z}/${x}/${y}.png"; }else { this.maxDepth = 19; this.baseUrl = "http://a.map.jms.gd/tile/gd_xiangtu/${z}/${x}/${y}.png"; } }else { if(style == 'satellite'){//卫星 this.maxDepth = 18; this.baseUrl = "https://webst01.is.autonavi.com/appmaptile?lang=zh_cn&style=6&yrs=m&x=${x}&y=${y}&z=${z}"; }else if(style == 'dark-standard'){ this.maxDepth = 18; this.baseUrl = "https://wprd01.is.autonavi.com/appmaptile?lang=zh_cn&style=8&size=1&yrs=m&x=${x}&y=${y}&z=${z}"; //路网 加上scl=2后去掉名字 }else { this.maxDepth = 19; this.baseUrl = "https://wprd04.is.autonavi.com/appmaptile?lang=zh_cn&style=7&yrs=m&x=${x}&y=${y}&z=${z}"; // } }//详解 https://www.cnblogs.com/lucio110/p/17310054.html } this.style = style; this.setEnable(false); this.setEnable(true); viewer.dispatchEvent('content_changed'); } } class TiledMapFromEntity extends TiledMapBase{ constructor(mapLayer, tileColor, data){ super('floorplan', mapLayer, tileColor, "LOCAL" /* "EPSG:3857" *//* "WGS84" */); //直接就是本地坐标,没有projec let entity = this.tiledMapEntity = this.fillFromData(data); let time = entity.updateTime || entity.createTime; this.tileSizePx = entity.tileSizePx, this.mapSizeM = entity.mapSizeM, this.maxDepth = entity.maxDepth; this.postStamp = time ? time.replace(/[^0-9]/ig,'') : (new Date).getTime(); //this.projection = n.crsLocal, this.zIndex = 0, this.tilePresenceMap = this.decodeBitStream(this.tiledMapEntity.quadtree); //包含tile分裂信息,如果写错了会造成tile显示不全 this.maxLoading = 5; this.bias = 0.5; //越大加载层级越低,越模糊 if(window.devicePixelRatio>=2 && window.innerHeight * window.innerWidth < 768*1024){ //手机还是加载高清点(反正也不需要截图等待),但平板太大了,要铺满屏幕可能慢,所以稍微模糊点?(反正可以继续放大去看) this.bias = 0; //level更高些 } } fillFromData(e){ let data = {}; data.id = e.id; data.globalLocation = Potree.Utils.VectorFactory.fromArray3(e.location); data.orientation = Potree.Utils.QuaternionFactory.fromArray(e.orientation); let pointcloud = viewer.scene.pointclouds.find(p=>p.dataset_id == e.datasetId); if(pointcloud.datasetData.mapping){ data.filePath = `${Potree.settings.urls.prefix1}/${pointcloud.datasetData.mapping}${e.file_path}`; }else { data.filePath = `${Potree.settings.urls.prefix1}${e.file_path}`; } data.fileName = '$DEPTH/$X/$Y.png'; data.type = e.type, data.mapSizeM = e.map_size_m, data.tileSizePx = e.tile_size_px, data.maxDepth = e.max_depth, data.quadtree = e.quadtree, data.floorId = e.floor_id, data.bundleId = e.bundle_id; //this.computeLocalCoordinates() return data } computeLocalCoordinates(){ if(proj4.defs("LOCAL_MAP")){ let lonlat = this.tiledMapEntity.globalLocation; /* if(window.AMapWith84){//需要转换 lonlat = AMapWith84.wgs84ToAMap(lonlat) } */ lonlat = viewer.transform.lonlatToLocal.forward(lonlat); this.tiledMapEntity.location = new Vector3().copy(lonlat); } } updateProjection() { super.updateProjection(); if(!this.position){ this.computeLocalCoordinates(); } /* this.projection = this.TransformService.crsLocal, t.prototype.updateProjection.call(this) */ } get position(){ return this.tiledMapEntity.location /* enumerable: !0, configurable: !0 */ } get quaternion(){ return this.tiledMapEntity.orientation /* enumerable: !0, configurable: !0 */ } getTileUrl(t, e, n) { var i = (this.tiledMapEntity.filePath + "/" + this.tiledMapEntity.fileName).replace(/\$DEPTH/g, t.toString(10)).replace(/\$X/g, e.toString(10)).replace(/\$Y/g, n.toString(10)); return i += "?t=" + this.postStamp //this.RestService.addAuthorizationQueryParameter(i) //???? } decodeBitStream(t) { if (!t) return { empty: !0 }; for (var e = {}, n = [e], i = 0; i < t.length; i++) { var r = n.shift() , o = parseInt(t.substr(i, 1), 16); if (1 & o) { var a = {}; r[0] = a, n.push(a); } 2 & o && (a = {}, r[1] = a, n.push(a)), 4 & o && (a = {}, r[2] = a, n.push(a)), 8 & o && (a = {}, r[3] = a, n.push(a)); } var s = { empty: !0 }; return this.computeHashes(s, e, ""), s } computeHashes(t, e, n) { for (var i = 0; i < 4; i++) e[i] && (t[n + i.toString(10)] = !0, t.empty = !1, this.computeHashes(t, e[i], n + i.toString(10))); } } /* note: 目前缩小了能看出形态是一个地球。相机在高空朝下观测,地球平放着。 所以越靠近赤道和地球朝上的那面所在的中央经度(也就是local 0,0,0所对应的初始经度),tile越接近正方形。 所以在两极地区要怎么显示? 注册地理坐标时需要滚动地球吗?(修改初始经度、重定义NAVVIS:TMERC, 就需要更新所有三维世界中的物体位置) 切换中心点: var locationLonLat = viewer.transform.lonlatToLocal.inverse(viewer.mapViewer.camera.position.clone()) proj4.defs("LOCAL_MAP", "+proj=tmerc +ellps=WGS84 +lon_0=" + locationLonLat.x.toPrecision(15) + " +lat_0=" + locationLonLat.y.toPrecision(15)); viewer.mapViewer.mapLayer.maps[0].transformMapToLocal = null 地理注册部分地图上的1和2标记有两层意思。当地图全屏展示时,标记的是当前右侧经纬度的位置;当地图为小窗时,标记的是对应场景里三维位置。(所以感觉最好换一个ui?)且在2023.2.1之前才改好,之前都是后者。 为什么边缘总是有奇怪的mesh,是因为有顶点到背面了吗 https://lbs.amap.com/tools/picker 高德坐标拾取器(手机登录),但和这里展示的不一样, 要用AMapWith84.aMapToWgs84({x: ,y: })转成84。 (qq or 手机登录) 所以potree本地和有AMapWith84函数的laser地图展现不一样 https://www.google.com/maps/@77.7730021,-34.4952712,4z google取点 打印所有mapTile的名字,字符串最长的代表有显示的mesh。 viewer.mapViewer.mapLayer.maps[0].baseTile.traverse(function(e){console.log(e.name)}) 能查看有几个显示的mesh viewer.mapViewer.mapLayer.maps[0].objectGroup.children 图片地址中 tiles/4/3/9.png 第一个数字越高代表level越高,放得越大 . (tile.name.length也能反映出 viewer.mapViewer.mapLayer.maps[1].objectGroup.children.map(e=>e.name.length-1) 目前看的几个场景floorplan原图是1米=33.03个像素 图宽度= 512*2^(max_depth-1) , map_size_m 代表整个地图是多少米 经纬度精度小数点后5位为米级别,6位为分米,7位是厘米 */ /** * @author mschuetz / http://mschuetz.at * * adapted from THREE.OrbitControls by * * @author qiao / https://github.com/qiao * @author mrdoob / http://mrdoob.com * @author alteredq / http://alteredqualia.com/ * @author WestLangley / http://github.com/WestLangley * @author erich666 / http://erichaines.com * * * */ let Buttons = Potree.defines.Buttons; class FirstPersonControls extends EventDispatcher { constructor (viewer, viewport) { super(); this.viewer = viewer; this.renderer = viewer.renderer; this.scene = viewer.scene; this.rotationSpeed = 200; this.moveSpeed = 10; this.setCurrentViewport({hoverViewport:viewport, force:true}); //this.currentViewport = viewport this.keys = { FORWARD: ['W'.charCodeAt(0), 38], BACKWARD: ['S'.charCodeAt(0), 40], LEFT: ['A'.charCodeAt(0), 37], RIGHT: ['D'.charCodeAt(0), 39], UP: ['Q'.charCodeAt(0)], DOWN: ['E'.charCodeAt(0)], //SHIFT : [16], ALT : [18], SPACE:[32], Rotate_LEFT : ['L'.charCodeAt(0)], Rotate_RIGHT : ['J'.charCodeAt(0)], Rotate_UP : ['K'.charCodeAt(0)], Rotate_DOWN : ['I'.charCodeAt(0)], }; this.fadeFactor = 20; this.yawDelta = 0; this.pitchDelta = 0; this.translationDelta = new Vector3(0, 0, 0); this.translationWorldDelta = new Vector3(0, 0, 0); this.tweens = []; this.dollyStart = new Vector2; this.dollyEnd = new Vector2; //this.enableChangePos = true this.viewer.addEventListener('camera_changed',(e)=>{ if(this.viewer.name == 'mapViewer' || e.changeInfo && e.changeInfo.positionChanged && !viewer.mainViewport.view.isFlying() ){ this.setFPCMoveSpeed(e.viewport); } }); let drag = (e) => { if(!this.enabled)return let viewport = e.dragViewport; if(!viewport)return let camera = viewport.camera; let mode; if(e.isTouch){ if(e.touches.length == 1){ mode = (!e.dragViewport || e.dragViewport.name == 'MainView') ? 'rotate' : 'pan'; }else if(e.touches.length == 2){ mode = Potree.settings.displayMode == 'showPanos' ? 'scale' : 'pan-scale'; }else { mode = (!e.dragViewport || e.dragViewport.name == 'MainView') ? 'pan' : 'scale'; } }else { //mode = e.buttons === Buttons.LEFT && (!e.dragViewport || e.dragViewport.name == 'MainView') ? 'rotate' : 'pan' mode = e.buttons === Buttons.LEFT && camera.type != 'OrthographicCamera' ? 'rotate' : 'pan'; } //console.log('mode ', mode ) let moveSpeed = this.currentViewport.getMoveSpeed(); if (e.drag.startHandled === undefined) {///??????? e.drag.startHandled = true; this.dispatchEvent({type: 'start'}); } if (mode.includes('rotate')) {//旋转 //来自panoramaControl updateRotation if(!this.pointerDragStart){ return this.pointerDragStart = e.pointer.clone() } let view = this.scene.view; if(this.rotateStartInfo.rotAroundPoint){//定点旋转: 以当前intersect的点为target旋转,不改点在屏幕中的位置 let distance = camera.position.distanceTo(this.rotateStartInfo.rotCenter/* this.intersectStart.location */); //不按下ctrl的话 let posDir = new Vector3().subVectors(this.rotateStartInfo.rotCenter, view.position); if(posDir.dot(view.direction) < 0 )distance *= -1; //在背面 //按照orbitControl的方式旋转: let rotationSpeed = 2; this.yawDelta -= e.drag.pointerDelta.x * rotationSpeed; this.pitchDelta += e.drag.pointerDelta.y * rotationSpeed; /* //旋转方向和偏移量尽量和在漫游点处旋转方式的一样 this.yawDelta += e.drag.pointerDelta.x / 500 * viewport.resolution.x this.pitchDelta -= e.drag.pointerDelta.y / 500 * viewport.resolution.y */ //先更新一下相机: this.update(); view.applyToCamera(camera); //然后得到新的相机角度下,原先点在屏幕中的位置所对应的3d点现在的坐标。只需要平移一下新旧坐标差值即可。//感觉貌似也可以用project和unproject后的差值的方式,还不用判断z背面 let newPointerDir = viewer.inputHandler.getMouseDirection(this.rotateStartInfo.rotCenter2d).direction.clone().multiplyScalar(distance); let pivot = new Vector3().addVectors(camera.position, newPointerDir); //新的3d点 let moveVec = new Vector3().subVectors(pivot, this.rotateStartInfo.rotCenter); this.translationWorldDelta.copy(moveVec.negate()); //立即更新下,防止因update和此drag频率不同而打滑。 this.update(); view.applyToCamera(camera); }else { let _matrixWorld = camera.matrixWorld; camera.matrixWorld = new Matrix4;//unproject 前先把相机置于原点 var e1 = new Vector3(this.pointerDragStart.x,this.pointerDragStart.y,-1).unproject(camera) , t = new Vector3(e.pointer.x,e.pointer.y,-1).unproject(camera) , i = Math.sqrt(e1.x * e1.x + e1.z * e1.z) , n = Math.sqrt(t.x * t.x + t.z * t.z) , o = Math.atan2(e1.y, i) , a = Math.atan2(t.y, n); this.pitchDelta += o - a; //上下旋转 e1.y = 0, t.y = 0; var s = Math.acos(e1.dot(t) / e1.length() / t.length()); if(!isNaN(s)){ var yawDelta = s; //左右旋转 this.pointerDragStart.x > e.pointer.x && (yawDelta *= -1); this.yawDelta += yawDelta; } //console.log('rotate:', this.pitchDelta, e.pointer.toArray(), this.pointerDragStart.toArray()) this.pointerDragStart.copy(e.pointer); camera.matrixWorld = _matrixWorld ; } } if (mode.includes('pan')) {//平移 if(!this.canMovePos(viewport)){ return } if(camera.type == "OrthographicCamera"){ //console.log(e.drag.pointerDelta, e.pointer, e.drag.end) let moveVec = Utils.getOrthoCameraMoveVec(e.drag.pointerDelta, camera );//最近一次移动向量 let pointclouds; let Alignment = window.viewer.modules.Alignment; let MergeEditor = window.viewer.modules.MergeEditor; let handleState = Alignment.handleState; //右键平移视图、左键操作点云 let a = e.buttons === Buttons.LEFT && viewport.alignment && handleState && viewport.alignment[handleState]; if(Potree.settings.editType == 'pano'){ let PanoEditor = window.viewer.modules.PanoEditor; if(a && PanoEditor.selectedPano){ if(/* !PanoEditor.selectedGroup || */!PanoEditor.checkIfAllLinked({group:PanoEditor.selectedGroup}) ){ if(handleState == 'translate' && ( e.drag.intersectStart.pointclouds && Common.getMixedSet(PanoEditor.selectedClouds, e.drag.intersectStart.pointclouds).length || PanoEditor.selectedPano.hovered)//平移时 拖拽到点云上 或 circle。(其中点云只需要intersect的点云中包含选择的点云中之一即可) || handleState == 'rotate' ) //旋转模式不需要intersect { pointclouds = PanoEditor.selectedClouds; } }else { PanoEditor.dispatchEvent('needToDisConnect'); console.warn('选中的漫游点连通了整个数据集,不允许移动'); } } if(!pointclouds && e.buttons === Buttons.LEFT && viewport.rotateSide){//侧视图 (有时候会卡顿,是mousemove执行延迟了,一般发生在突然加载很多点云时) //console.log('rotateSide', -e.drag.pointerDelta.x ) return PanoEditor.rotateSideCamera(-e.drag.pointerDelta.x) } }else if(Potree.settings.editType == 'merge'){ if(e.buttons === Buttons.LEFT && viewport.rotateSide){ return MergeEditor.rotateSideCamera(-e.drag.pointerDelta.x) } }else { /* if(Alignment.selectedClouds && Alignment.selectedClouds.length){//多个点云 pointclouds = a && e.drag.intersectStart.pointclouds && Common.getMixedSet(Alignment.selectedClouds, e.drag.intersectStart.pointclouds).length && Alignment.selectedClouds }else{ */ //pointclouds = a && e.drag.intersectStart.pointcloud && [e.drag.intersectStart.pointcloud] //} if(a && e.drag.intersectStart.pointcloud){ pointclouds = [e.drag.intersectStart.pointcloud]; if(e.drag.intersectStart.pointcloud.dataset_id == Potree.settings.originDatasetId){ let p = e.drag.intersectStart.pointclouds.find(p=>p.dataset_id != Potree.settings.originDatasetId); if(p) pointclouds = [p]; } } } if(pointclouds){ if(handleState == 'translate' && viewport.alignment.translateVec){//只能沿某个方向移动 moveVec.projectOnVector(viewport.alignment.translateVec); } this.dispatchEvent({ type : "transformPointcloud", intersect: e.intersect.orthoIntersect, intersectStart: e.drag.intersectStart.orthoIntersect, moveVec, pointclouds, camera }); }else { this.translationWorldDelta.add(moveVec.negate()); } }else { //perspectiveCamera: if(e.drag.intersectStart){//如果拖拽着点云 let ifInit = e.drag.z == void 0; let pointerStartPos2d = e.drag.intersectStart.location.clone().project(camera);//识别到的点云点的位置 e.drag.z = pointerStartPos2d.z; //记录z,保持拖拽物体到屏幕距离不变,所以z深度不变(如果拖拽过程中没有缩放,这个z其实不变) if(ifInit){//拖拽开始 e.drag.projectionMatrixInverse = camera.projectionMatrixInverse.clone(); //防止吸附到最近点上(因为鼠标所在位置并非识别到的点云点的位置,需要得到鼠标所在位置的3d坐标。) let pointerStartPos2dReal = new Vector3(this.pointerDragStart.x,this.pointerDragStart.y, e.drag.z); e.drag.translateStartPos = pointerStartPos2dReal.clone().unproject(camera); //console.log('开始拖拽', e.pointer.clone()) } //拖拽的过程中将projectionMatrixInverse替换成开始拖拽时的,因为near、far一直在变,会导致unproject计算出的3d坐标改变很大而闪烁。 var _projectionMatrixInverse = camera.projectionMatrixInverse; camera.projectionMatrixInverse = e.drag.projectionMatrixInverse; let newPos2d = new Vector3(e.pointer.x,e.pointer.y, e.drag.z ); let newPos3d = newPos2d.clone().unproject(camera); let moveVec = newPos3d.clone().sub( e.drag.translateStartPos /* e.drag.intersectStart.location */ );//移动相机,保持鼠标下的位置永远不变,所以用鼠标下的新位置减去鼠标下的原始位置 camera.projectionMatrixInverse = _projectionMatrixInverse; this.translationWorldDelta.copy(moveVec.negate()); //这里没法用add,原因未知,打开console时会跳动 //console.log('pan 1', this.translationWorldDelta.clone()) //四指松开剩三指时会偏移一下,暂不知道哪里的问题,或许跟开头防止点云吸附有关? }else { //如果鼠标没有找到和点云的交点,就假设移动整个模型(也可以去扩大范围寻找最近点云) /* let center = viewer.scene.pointclouds[0].position; let radius = camera.position.distanceTo(center); let ratio = radius * Math.tan(THREE.Math.degToRad(camera.fov)/2) / 1000 */ /* let speed = this.currentViewport.getMoveSpeed() if(FirstPersonControls.boundPlane){ speed = FirstPersonControls.boundPlane.distanceToPoint(this.currentViewport.position) speed = Math.max(1 , speed) } */ let lastIntersect = this.target || viewport.lastIntersect && (viewport.lastIntersect.location || viewport.lastIntersect);//该viewport的最近一次鼠标和点云的交点 if(!lastIntersect || !(lastIntersect instanceof Vector3))lastIntersect = viewer.bound.center; let speed = camera.position.distanceTo(lastIntersect); let fov = cameraLight.getHFOVForCamera(camera, true); let ratio = speed * Math.tan(fov/2); this.translationDelta.x -= e.drag.pointerDelta.x * ratio; this.translationDelta.z -= e.drag.pointerDelta.y * ratio; //console.log('pan2', e.drag.pointerDelta) } } this.useAttenuation = false; } if(mode.includes('scale')){//触屏缩放 this.dollyEnd.subVectors(e.touches[0].pointer, e.touches[1].pointer); //if(!this.dollyStart)return var scale = this.dollyEnd.length() / this.dollyStart.length(); let pointer = new Vector2().addVectors(e.touches[0].pointer, e.touches[1].pointer).multiplyScalar(0.5);//两个指头的中心点 dolly({ pointer, scale, camera, drag:e.drag }); this.dollyStart.copy(this.dollyEnd); } //最好按ctrl可以变为dollhouse的那种旋转 }; let drop = e => { if(!this.enabled)return this.dispatchEvent({type: 'end'}); }; let dolly = (e={})=>{ if(Potree.settings.displayMode == 'showPanos' && this.currentViewport == viewer.mainViewport/* this.currentViewport.unableChangePos */){//全景时 this.dispatchEvent({type:'dollyStopCauseUnable',delta:e.delta, scale:e.scale}); return } let camera = e.camera; if(camera.type == "OrthographicCamera"){ let ratio; if(e.delta != void 0){//滚轮缩放 if(e.delta == 0){//mac return }else if (e.delta < 0) { ratio = 0.9; } else if (e.delta > 0) { ratio = 1.12; //增加的需要比减少的多一些,否则缩放相同次数下,r0 * ([1+s)(1-s)]^n = r0 * (1-s^2)^n < r0 } }else { ratio = e.scale; //触屏缩放 } let zoom = camera.zoom * ratio; let limit = camera.zoomLimit; if(limit) zoom = MathUtils.clamp(zoom, limit.min,limit.max ); let pointerPos = new Vector3(e.pointer.x, e.pointer.y,0.5); let oldPos = pointerPos.clone().unproject(camera); if(camera.zoom != zoom){ camera.zoom = zoom; camera.updateProjectionMatrix(); } let newPos = pointerPos.clone().unproject(camera); //定点缩放, 恢复一下鼠标所在位置的位置改变量 let moveVec = new Vector3().subVectors(newPos,oldPos); this.translationWorldDelta.add(moveVec.negate()); this.useAttenuation = false; }else { let speed = this.currentViewport.getMoveSpeed() * 7, direction; if(e.delta != void 0){//滚轮缩放 if(e.delta == 0)return //mac /*if(this.target && !e.intersect){//如果没有intersect点云且有target的话,就朝target的方向. 但无限靠近时有问题,且到背面时前进却是后退 direction = new THREE.Vector3().subVectors(this.target, camera.position).normalize() }else{ */ direction = this.viewer.inputHandler.getMouseDirection().direction; //定点缩放 if(e.intersect && e.intersect.location){//和intersect的墙越接近,速度越慢,便于focus细节 let dis = camera.position.distanceTo(e.intersect.location); speed = MathUtils.clamp(dis * 0.1, 0.3, speed); } if (e.delta < 0) { speed *= -1; } let slightly = this.keys.ALT.some(e => this.viewer.inputHandler.pressedKeys[e]); slightly && (speed *= 0.2); this.useAttenuation = true; }else {//触屏缩放 direction = this.viewer.inputHandler.getMouseDirection(e.pointer).direction; //定点缩放 if(e.drag.intersectStart){//和intersect的墙越接近,速度越慢,便于focus细节 let dis = camera.position.distanceTo(e.drag.intersectStart.location); let r = 1-1/e.scale; let closeMin = 0.1, standardMin = 0.001, disBound1 = 2, disBound2 = 5; if(math.closeTo(e.scale,1,0.03)){//如果偏差小于0.01,就不限制最小值,因为平移容易正负抖动,近距离有最小值的话抖动明显 closeMin = 0; //所以若缩放不明显(双指滑动慢),就不设置最低值。(这时候穿越障碍物会比较困难。) } //console.log('closeMin',closeMin) let min = math.linearClamp(dis, [disBound1, disBound2], [closeMin, standardMin]); //触屏和滚轮不一样,触发较为连续,所以最小值设低一点。若要保持双指相对点云位置不变,理想最小值是0,但那样就无法穿越点云(最小值太小的话穿越密集点云如树丛很困难;太大会打滑)所以当离点云近时增大最小值 speed = Math.sign(r) * MathUtils.clamp(dis * Math.abs(r), min, speed); //console.log(speed, dis, e.scale) }else { this.useAttenuation = true; let accelerate = 40; if(math.closeTo(e.scale,1,0.02)){//缩放小的时候很可能是双指平移时,容易抖动,所以降低移动速度 accelerate *= Math.min(40*Math.abs(e.scale-1), 0.8); } // console.log('accelerate',accelerate) const constantDis = this.currentViewport.getMoveSpeed() * accelerate; //constantDis = 10;//常量系数,当放大一倍时前进的距离。可以调整 speed = (e.scale-1)*constantDis; } } var vec = direction.multiplyScalar(speed ); //this.translationWorldDelta.copy(vec) this.translationWorldDelta.add(vec); //console.log(direction.toArray(), speed, e.scale) } return true }; let scroll = (e) => { if(!this.enabled || !e.hoverViewport)return this.setCurrentViewport(e); e.camera = e.hoverViewport.camera; dolly(e); }; let dblclick = (e) => { if(!this.enabled)return if(!Potree.settings.dblToFocusPoint)return;//调试时才可双击 if(Potree.settings.displayMode == 'showPointCloud'/* !viewer.images360.isAtPano() */) this.zoomToLocation(e.mouse); }; this.viewer.addEventListener('global_drag', drag); /* this.viewer.addEventListener('global_touchmove', (e)=>{ if(!this.enabled)return if(e.touches.length>1){//单指的就触发上一句 //console.log('global_touchmove' ) drag(e) } }); */ this.viewer.addEventListener('global_drop', drop); this.viewer.addEventListener('global_mousewheel', scroll); this.viewer.addEventListener('global_dblclick', dblclick); let prepareScale = (e)=>{//触屏的scale this.dollyStart.subVectors(e.touches[0].pointer, e.touches[1].pointer); e.drag.camDisToPointStart = null; }; let prepareRotate = (e)=>{ this.pointerDragStart = e.pointer.clone(); if(e.viewer.name != 'mainViewer' )return let intersect = e.intersect || e.dragViewport.lastIntersect; //在数据集外部时绕中心点旋转,在数据集内部时绕intersect点旋转(其他数据集的点也可以) 或者 原地旋转镜头 let rotAroundPoint = Potree.settings.rotAroundPoint && e.dragViewport.camera.type != 'OrthographicCamera' && (viewer.atDatasets.length == 0 || intersect) && this.canMovePos(viewport) && !viewer.images360.isAtPano() && !this.viewer.inputHandler.pressedKeys[32]; let rotCenter2d, rotCenter; if(rotAroundPoint){ let pivotType = this.target ? 'target' : viewer.atDatasets.length > 0 ? 'intersect' : viewer.inputHandler.selection.length ? 'selection' : this.target2 ? 'target2' : 'boundCenter'; rotCenter = pivotType == 'target'? this.target :pivotType == 'intersect' ? intersect.location : pivotType == 'selection' ? viewer.inputHandler.selection[0].position : pivotType == 'target2' ? this.target2 : viewer.bound && viewer.bound.center; if(rotCenter){ rotCenter2d = rotCenter.clone().project(e.dragViewport.camera); //点在屏幕中的位置。 若z>1 则在背面 或 超出far范围 }else { rotAroundPoint = false; } } this.rotateStartInfo = { rotAroundPoint, //定点旋转 rotCenter, rotCenter2d }; //缺点:多数据集绕中心点转很难操作,感觉可以也改为绕lastIntersect //console.log('prepareRotate' ) }; let preparePan = (e)=>{//触屏的pan点云 还是会偏移 this.pointerDragStart = e.pointer.clone(); e.drag.z = void 0; //清空 drag(e); //触屏点击时更新的pointer直接用一次drag //console.log('preparePan ' ) }; this.viewer.addEventListener('global_mousedown'/* 'startDragging' */, (e)=>{ if(!this.enabled)return this.setCurrentViewport(e); prepareRotate(e); }); //注意,每次增减指头都会修改pointer,需要更新下状态 this.viewer.addEventListener('global_touchstart', (e)=>{ if(!this.enabled)return if(e.touches.length==2){//只监听开头两个指头 prepareScale(e); preparePan(e); }else if(e.touches.length>=3){ preparePan(e); } }); this.viewer.addEventListener('global_touchend', (e)=>{//e.touches是剩余的指头 if(!this.enabled)return if(e.touches.length==2){//停止平移,开始scale prepareScale(e); preparePan(e); }else if(e.touches.length==1){//停止scale,开始rotate prepareRotate(e); }else if(e.touches.length>=3){//重新准备下平移(因为抬起的指头可能包含平移使用的数据),否则抬起时漂移 preparePan(e); } }); /* this.viewer.addEventListener('enableChangePos', (e)=>{ if(!this.enabled)return this.enableChangePos = e.canLeavePano }) */ } canMovePos(viewport){ if(viewport == viewer.mainViewport && (Potree.settings.displayMode == 'showPanos' || viewer.images360.bumping || viewer.images360.latestToPano))return false else return true } setEnable(enabled){ this.enabled = enabled; } setTarget(target, index){//绕该点旋转,类似orbitControl if(index == 2)this.target2 = target; else this.target = target; } setFPCMoveSpeed(viewport){ if(viewport.camera.type == 'OrthographicCamera'){ let s = 1 / viewport.camera.zoom; viewport.setMoveSpeed(s); }else { //根据和漫游点的最短距离算moveSpeed。缺点:对于导入的无漫游点的数据集没有意义。 if(viewport == viewer.mainViewport && viewer.images360){ let position = viewer.mainViewport.view.position; let speed; let pano = viewer.images360.findNearestPano(); if(!pano){ if(!viewer.bound || viewer.bound.boundSize.x == 0)return let boundFloor = viewer.bound.boundingBox.clone(); boundFloor.max.z = boundFloor.min.z; speed = boundFloor.distanceToPoint(viewer.mainViewport.view.position); speed = Math.sqrt(speed) / 50; }else { let dis = pano.position.distanceTo(position); let minSpeed = 0.05, minDis = 3, multiplier = 0.005; speed = dis <= minDis ? minSpeed : minSpeed + (dis-minDis) * multiplier; //console.log('dis', dis, 'speed', speed, pano.id ) } viewer.setMoveSpeed(speed*2); } //调试场景t-FhDWmV5xur 两个数据集,大的数据集没有漫游点。 } } setCurrentViewport(o={}){//add if(!this.enabled && !o.force )return if(o.hoverViewport && this.currentViewport != o.hoverViewport ){ this.currentViewport = o.hoverViewport; //this.viewer.setMoveSpeed(this.currentViewport.radius/100); this.setFPCMoveSpeed(this.currentViewport); } if(this.currentViewport.camera.type == 'OrthographicCamera'){ this.lockElevationOri = true; this.lockRotation = true; }else { this.lockElevationOri = false; this.lockRotation = false; } } setScene (scene) { this.scene = scene; } stop(){ this.yawDelta = 0; this.pitchDelta = 0; this.translationDelta.set(0, 0, 0); } zoomToLocation(mouse){ if(!this.enabled)return let camera = this.scene.getActiveCamera(); /* let I = Utils.getMousePointCloudIntersection( mouse, camera, this.viewer, this.scene.pointclouds); */ var I = this.viewer.inputHandler.intersect; if (!I) { return; } let targetRadius = 0; { let minimumJumpDistance = 0.2; let domElement = this.renderer.domElement; let ray = Utils.mouseToRay(this.viewer.inputHandler.pointer, camera); let {origin, direction} = this.viewer.inputHandler.getMouseDirection(); let raycaster = new Raycaster(); raycaster.ray.set(origin, direction); let nodes = I.pointcloud.nodesOnRay(I.pointcloud.visibleNodes, ray); let nodes2 = I.pointcloud.nodesOnRay(I.pointcloud.visibleNodes, raycaster.ray); let lastNode = nodes[nodes.length - 1]; let radius = lastNode.getBoundingSphere(new Sphere()).radius; targetRadius = Math.min(this.scene.view.radius, radius); targetRadius = Math.max(minimumJumpDistance, targetRadius); } let d = this.scene.view.direction.multiplyScalar(-1); let cameraTargetPosition = new Vector3().addVectors(I.location, d.multiplyScalar(targetRadius)); // TODO Unused: let controlsTargetPosition = I.location; let animationDuration = 600; let easing = TWEEN.Easing.Quartic.Out; { // animate let value = {x: 0}; let tween = new TWEEN.Tween(value).to({x: 1}, animationDuration); tween.easing(easing); this.tweens.push(tween); let startPos = this.scene.view.position.clone(); let targetPos = cameraTargetPosition.clone(); let startRadius = this.scene.view.radius; let targetRadius = cameraTargetPosition.distanceTo(I.location); tween.onUpdate(() => { let t = value.x; this.scene.view.position.x = (1 - t) * startPos.x + t * targetPos.x; this.scene.view.position.y = (1 - t) * startPos.y + t * targetPos.y; this.scene.view.position.z = (1 - t) * startPos.z + t * targetPos.z; this.scene.view.radius = (1 - t) * startRadius + t * targetRadius; this.viewer.setMoveSpeed(this.scene.view.radius / 2.5); }); tween.onComplete(() => { this.tweens = this.tweens.filter(e => e !== tween); }); tween.start(); } } update (delta=1) { if(!this.enabled)return //console.log('update') let view = this.currentViewport.view; { // cancel move animations on user input let changes = [ this.yawDelta, this.pitchDelta, this.translationDelta.length(), this.translationWorldDelta.length() ]; let changeHappens = changes.some(e => Math.abs(e) > 0.001); if (changeHappens && this.tweens.length > 0) { this.tweens.forEach(e => e.stop()); this.tweens = []; } } { // accelerate while input is given let ih = this.viewer.inputHandler; let moveForward = this.keys.FORWARD.some(e => ih.pressedKeys[e]); let moveBackward = this.keys.BACKWARD.some(e => ih.pressedKeys[e]); let moveLeft = this.keys.LEFT.some(e => ih.pressedKeys[e]); let moveRight = this.keys.RIGHT.some(e => ih.pressedKeys[e]); let moveUp = this.keys.UP.some(e => ih.pressedKeys[e]); let moveDown = this.keys.DOWN.some(e => ih.pressedKeys[e]); let rotateLeft = this.keys.Rotate_LEFT.some(e => ih.pressedKeys[e]); let rotateRight = this.keys.Rotate_RIGHT.some(e => ih.pressedKeys[e]); let rotateUp = this.keys.Rotate_UP.some(e => ih.pressedKeys[e]); let rotateDown = this.keys.Rotate_DOWN.some(e => ih.pressedKeys[e]); this.lockElevation = this.lockElevationOri || this.keys.SPACE.some(e => ih.pressedKeys[e]); let slightly = this.keys.ALT.some(e => ih.pressedKeys[e]); if(!this.lockRotation){ if(rotateLeft){ this.yawDelta -= 0.01; }else if(rotateRight){ this.yawDelta += 0.01; } if(rotateUp){ this.pitchDelta -= 0.01; }else if(rotateDown){ this.pitchDelta += 0.01; } } if(this.canMovePos(this.currentViewport) && !this.lockKey){ if(this.lockElevation){ let dir = view.direction; dir.z = 0; dir.normalize(); if (moveForward && moveBackward) { this.translationWorldDelta.set(0, 0, 0); } else if (moveForward) { this.translationWorldDelta.copy(dir.multiplyScalar(this.currentViewport.getMoveSpeed())); } else if (moveBackward) { this.translationWorldDelta.copy(dir.multiplyScalar(-this.currentViewport.getMoveSpeed())); } }else { if (moveForward && moveBackward) { this.translationDelta.y = 0; } else if (moveForward) { this.translationDelta.y = this.currentViewport.getMoveSpeed() * (slightly ? 0.2 : 1); } else if (moveBackward) { this.translationDelta.y = -this.currentViewport.getMoveSpeed() * (slightly ? 0.2 : 1); } } if (moveLeft && moveRight) { this.translationDelta.x = 0; } else if (moveLeft) { this.translationDelta.x = -this.currentViewport.getMoveSpeed(); } else if (moveRight) { this.translationDelta.x = this.currentViewport.getMoveSpeed(); } if (moveUp && moveDown) { this.translationWorldDelta.z = 0; } else if (moveUp) { this.translationWorldDelta.z = this.currentViewport.getMoveSpeed(); } else if (moveDown) { this.translationWorldDelta.z = -this.currentViewport.getMoveSpeed(); } if(moveUp || moveDown || moveForward || moveBackward){ this.useAttenuation = false; } } } { // apply rotation let yaw = view.yaw; let pitch = view.pitch; yaw += this.yawDelta; /* * delta; */ pitch += this.pitchDelta;/* * delta; */ view.yaw = yaw; view.pitch = pitch; if(this.yawDelta || this.pitchDelta){ view.cancelFlying('rotate'); } this.yawDelta = 0; this.pitchDelta = 0; } /* if(this.translationWorldDelta.length()>0) { // console.log('translationDelta') } */ { // apply translation view.translate( this.translationDelta.x, /* * delta, */ this.translationDelta.y, /* * delta, */ this.translationDelta.z, /* * delta */ ); this.translationDelta.set(0,0,0); //if(this.translationWorldDelta.length())console.log(translationWorldDelta) view.translateWorld( this.translationWorldDelta.x /* * delta */, this.translationWorldDelta.y /* * delta */, this.translationWorldDelta.z /* * delta */ ); //this.translationWorldDelta.set(0,0,0) } { // set view target according to speed //view.radius = 1 * this.currentViewport.getMoveSpeed(); /* if(viewer.bound) view.radius = view.position.distanceTo(viewer.bound.center) let speed = view.radius/100; this.viewer.setMoveSpeed(speed); */ //this.setMoveSpeed() } if(this.useAttenuation){ //只有滚轮缩放时开启 let attenuation = Math.max(0, 1 - this.fadeFactor * delta); /*this.yawDelta *= attenuation; this.pitchDelta *= attenuation; this.translationDelta.multiplyScalar(attenuation);*/ this.translationWorldDelta.multiplyScalar(attenuation); }else { this.translationWorldDelta.set(0,0,0); } } }; /** * @author mschuetz / http://mschuetz.at * * */ let {Buttons: Buttons$1} = Potree.defines; class InputHandler extends EventDispatcher { constructor (viewer,scene) { super(); this.viewer = viewer; this.renderer = viewer.renderer; this.domElement = this.renderer.domElement; this.enabled = true; this.scene = scene; this.interactiveScenes = []; this.interactiveObjects = new Set(); this.inputListeners = []; this.blacklist = new Set(); this.drag = null; this.mouse = new Vector2(0, 0); //add: this.pointer = new Vector2(0, 0); //交互点的屏幕坐标,有别于DOM坐标,在此存放NDC坐标。(NDC,三维常用坐标系,二维坐标,整个屏幕映射范围(-1,1),屏幕中心为原点,+Y朝上,+X朝右) this.mouseDownMouse = new Vector2(0, 0); this.selection = []; this.hoveredElements = []; this.pressedKeys = {}; this.wheelDelta = 0; this.speed = 1; this.logMessages = false; if (this.domElement.tabIndex === -1) { this.domElement.tabIndex = 2222; } this.lastPointerUpTime = 0; this.touches = []; this.interactHistory = {move:0}; //add this.hoverViewport = viewer.viewports[0]; this.domElement.addEventListener('contextmenu', (event) => { event.preventDefault(); }, false); this.domElement.addEventListener('click', this.onMouseClick.bind(this), false); this.domElement.addEventListener('mousedown', this.onMouseDown.bind(this), false); window.addEventListener('mouseup', this.onMouseUp.bind(this), false); if(Potree.isIframeChild){//子页面的话在父页面也要加侦听(应该不会有多层吧?否则要一直加到最外层) //window.parent.addEventListener('mouseup', this.onMouseUp.bind(this), false); //可能跨域 //window.parent.postMessage('listenMouseup', '*'); window.addEventListener('mouseout', this.onMouseUp.bind(this), false); //cancel drag } this.domElement.addEventListener('mouseout', ()=>{ this.containsMouse = false; }, false); this.domElement.addEventListener('mousemove', this.onMouseMove.bind(this), false); //add /* this.domElement.addEventListener("pointerout", this.onMouseUp.bind(this)), this.domElement.addEventListener("pointercancel", this.onMouseUp.bind(this)), */ this.domElement.addEventListener('mousewheel', this.onMouseWheel.bind(this), false); this.domElement.addEventListener('DOMMouseScroll', this.onMouseWheel.bind(this), false); // Firefox //this.domElement.addEventListener('dblclick', this.onDoubleClick.bind(this)); //因为双击时间间隔是跟随系统的所以不好判断 window.addEventListener('keydown', this.onKeyDown.bind(this));//原先是this.domElement,这样的话一开始要点击屏幕后才能监听到 window.addEventListener('keyup', this.onKeyUp.bind(this)); window.addEventListener('blur',this.onKeyUp.bind(this)); //add this.domElement.addEventListener('touchstart', this.onTouchStart.bind(this)); this.domElement.addEventListener('touchend', this.onTouchEnd.bind(this)); this.domElement.addEventListener('touchmove', this.onTouchMove.bind(this)); { this.measuring = []; //正在编辑的measure //let mesureInfo = new THREE.EventDispatcher() this.addEventListener('measuring',(e)=>{ //true优先级高于false, 正在添加时dropMarker也不会停止 //Potree.Utils.updateVisible(mesureInfo, e.situation, e.v, 0, e.v?'add':'cancel' )//借用该函数,使true优先级高于false,防止正在添加时dropMarker而停止 if(e.v){ this.measuring.includes(e.object) || this.measuring.push(e.object); }else { let index = this.measuring.indexOf(e.object); index > -1 && this.measuring.splice(index, 1); } if(this.measuring.length == 0 && this.measuring.length>0 ){ this.viewer.viewports.forEach((viewport)=>{ this.collectClosePoints(viewport, true );//forceUpdate }); } //console.log('measuring',e.v, e.cause, e.situation, this.measuring.length ) }); } window.viewer.addEventListener('loopStart',()=>{ this.interactHistory = {}; //清空 }); } /* addInputListener (listener) { this.inputListeners.push(listener); } removeInputListener (listener) { this.inputListeners = this.inputListeners.filter(e => e !== listener); } getSortedListeners(){ return this.inputListeners.sort( (a, b) => { let ia = (a.importance !== undefined) ? a.importance : 0; let ib = (b.importance !== undefined) ? b.importance : 0; return ib - ia; }); } */ //统一跟第一个触碰的viewport相同 updateTouchesInfo(e){ var viewport, pointer, camera; let oldTouches = this.touches; let changedTouches = Array.from(e.changedTouches); let touches = Array.from(e.touches); this.touches = touches.map(touch=>{ let touch_ = oldTouches.find(a=>a.touch.identifier == touch.identifier); let pointer = touch_ && touch_.pointer; //复制原先的值 return { touch, pointer, } }); if(e.touches.length > 0){ let newTouches = touches.filter(e=>! oldTouches.some(a=>a.touch.identifier == e.identifier) && !changedTouches.some(a=>a.identifier == e.identifier) ); //从按钮处划过时e.touches中会出现this.touches和changedTouches中都没有的identifier if(newTouches.length>0){ console.warn('has new',newTouches.map(e=>e.identifier)); } newTouches.concat(changedTouches).forEach(touch=>{ //修改changedTouches的 let touch_ = this.touches.find(a=>a.touch.identifier == touch.identifier); if(touch_){ let a = this.getPointerInViewport(touch.pageX, touch.pageY, this.dragViewport||viewport, new Vector2); touch_.pointer = a.pointer.clone(); viewport = a.viewport; camera = a.camera; } }); //使用当前touches的平均 if(e.touches.length > 1){ let pageX = Common.average(e.touches, "pageX"); let pageY = Common.average(e.touches, "pageY"); let a = this.getPointerInViewport(pageX, pageY, viewport, new Vector2); this.pointer.copy(a.pointer); //console.log('updateTouchesInfo', this.pointer.clone()) }else { this.pointer = this.touches[0].pointer.clone(); //更新,使用当前touches中的第一个 } /* if(this.touches.find(e=>!e.pointer)){ console.error(' touches has no pointer', oldTouches.map(e=>e.touch.identifier), Array.from(e.touches).map(e=>e.identifier), Array.from(e.changedTouches).map(e=>e.identifier) ) } */ //console.log(this.touches) //console.log('更新pointer1',this.pointer.toArray()) return {viewport, camera/* , pointer:this.pointer */} } } onTouchStart (e) { if (this.logMessages) console.log(this.constructor.name + ': onTouchStart'); e.preventDefault(); /* if (e.touches.length === 1 || !this.drag) { //!this.drag代表一次性下了两个指头 let rect = this.domElement.getBoundingClientRect(); let x = e.touches[0].pageX let y = e.touches[0].pageY this.dealPointerDown(x,y,e,true) }else{ this.updateTouchesInfo(e) this.drag.end.copy(this.pointer) } */ this.dealPointerDown(e,true); this.viewer.dispatchEvent($.extend( this.getEventDesc(e,true), { type: 'global_' + e.type, changedTouches: e.changedTouches } )); /* console.log('targetTouches :', Array.from(e.targetTouches).map(e=>'| identifier: '+ e.identifier), 'changedTouches :', Array.from(e.changedTouches).map(e=>'| identifier: '+ e.identifier) ) */ //console.log('') } onTouchMove (e) { if (this.logMessages) console.log(this.constructor.name + ': onTouchMove'); e.preventDefault(); /* if (e.touches.length === 1) { let rect = this.domElement.getBoundingClientRect(); let x = e.touches[0].pageX; let y = e.touches[0].pageY; }else{ this.updateTouchesInfo(e) this.drag.pointerDelta.subVectors(this.pointer, this.drag.end) this.drag.end.copy(this.pointer) } */ this.dealPointerMove(e, true); this.viewer.dispatchEvent($.extend( this.getEventDesc(e,true), { type: 'global_' + e.type, changedTouches: e.changedTouches } )); /* console.log('targetTouches :', Array.from(e.targetTouches).map(e=>'| identifier: '+ e.identifier), 'changedTouches :', Array.from(e.changedTouches).map(e=>'| identifier: '+ e.identifier) ) */ } onTouchEnd (e) { if (this.logMessages) console.log(this.constructor.name + ': onTouchEnd'); e.preventDefault(); //console.log('onTouchEnd') this.updateTouchesInfo(e); /* if (e.touches.length === 0) { let rect = this.domElement.getBoundingClientRect(); let x = e.changedTouches[0].pageX //万一一次松开两个指头的怎么办 let y = e.changedTouches[0].pageY this.dealPointerUp(x,y,e,true) }else { this.drag.end.copy(this.pointer) } */ this.dealPointerUp(e,true); this.viewer.dispatchEvent($.extend( this.getEventDesc(e,true), { type: 'global_' + e.type, } )); //console.log('touchend length '+e.touches.length, this.touches.length) } onKeyDown (e) { if (this.logMessages) console.log(this.constructor.name + ': onKeyDown'); if(e.target.nodeName == 'INPUT' || e.target.nodeName == 'TEXTAREA') return //正在输入文字 或e.srcElement if(!this.containsMouse)return //在别的ui上无效 // DELETE /* if (e.keyCode === KeyCodes.DELETE && this.selection.length > 0) { this.dispatchEvent({ type: 'delete', selection: this.selection }); this.deselectAll(); } */ this.dispatchEvent({ type: 'keydown', keyCode: e.keyCode, event: e }); // for(let l of this.getSortedListeners()){ // l.dispatchEvent({ // type: "keydown", // keyCode: e.keyCode, // event: e // }); // } this.pressedKeys[e.keyCode] = true; // e.preventDefault(); } onKeyUp (e) { if (this.logMessages) console.log(this.constructor.name + ': onKeyUp'); if(e.keyCode != void 0){ delete this.pressedKeys[e.keyCode]; }else { this.pressedKeys = {}; } e.preventDefault(); } onDoubleClick (e) { if (this.logMessages) console.log(this.constructor.name + ': onDoubleClick'); let consumed = false; for (let hovered of this.hoveredElements) { if (hovered._listeners && hovered._listeners['dblclick']) { hovered.object.dispatchEvent({ type: 'dblclick', mouse: this.mouse, object: hovered.object }); consumed = true; break; } } if (!consumed) { /* for (let inputListener of this.getSortedListeners()) { inputListener. */this.viewer.dispatchEvent({ type: 'global_dblclick', mouse: this.mouse, object: null }); //} } this.needSingleClick = false;//add e.preventDefault(); } onMouseClick (e) { if (this.logMessages) console.log(this.constructor.name + ': onMouseClick'); e.preventDefault(); } dealPointerDown(e,isTouch){ e.preventDefault(); //重新获取一下pointer, 因点击了浏览器的按钮展开列表时 move回来不会触发onmousemove,所以pointer是旧的 if(isTouch){ var { camera, viewport } = this.updateTouchesInfo(e); if(this.drag){ //因为触屏在按下前缺少pointermove所以要更新下 this.drag.end = this.pointer.clone(); } }else { var { camera, viewport } = this.getPointerInViewport(e.clientX, e.clientY ); } this.dragViewport = this.hoverViewport = viewport; //if(isTouch || !Potree.settings.intersectWhenHover ){ if(isTouch || !this.dragViewport.view.isFlying() && Potree.settings.intersectWhenHover && Potree.settings.editType != 'pano' ){//漫游点编辑如果拖拽前getIntersect旋转会延迟 //isTouch必须更新 否则是旧的 this.hoveredElements = this.getHoveredElements(); let dontIntersect = false; this.intersect = this.getIntersect({viewport, dontIntersect, clientX:e.clientX, clientY:e.clientY}); //更新intersect,避免在没有mousemove但flyToPano后intersect未更新。 //this.intersect = this.getWholeIntersect() } if(!viewport)return //why add this? if (!this.drag) { let target = (isTouch||e.button == MOUSE.LEFT) && this.hoveredElements.find(el => (//只有左键能拖拽 el.object._listeners && el.object._listeners['drag'] && el.object._listeners['drag'].length > 0)); if (target) { this.startDragging(target.object, {location: target.point}); } else { this.startDragging(null); } } this.drag.intersectStart = this.intersect; if(!isTouch || e.touches.length == 1){ let consumed = false; let consume = () => { return consumed = true; }; //if (this.hoveredElements.length === 0) { this.viewer.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'global_mousedown' } )); for(let hovered of this.hoveredElements){ let object = hovered.object; object.dispatchEvent({ type: 'mousedown', viewer: this.viewer, consume: consume }); if(consumed){ break; } } } this.mouseDownMouse = this.mouse.clone(); this.pointerDownTime = Date.now(); } onMouseDown (e) { if (this.logMessages) console.log(this.constructor.name + ': onMouseDown'); this.dealPointerDown(e); } /* getWholeIntersect(hoveredElements, intersectPoint){//add hoveredElements = hoveredElements || this.hoveredElements intersectPoint = intersectPoint || this.intersectPoint if(Potree.settings.intersectOnObjs && hoveredElements[0] && hoveredElements[0].object.isModel){ return {//模拟点云的intersectPoint的结构写法 hoveredElement : hoveredElements[0] , location: hoveredElements[0].point, point: {normal: hoveredElements[0].face.normal }, distance: hoveredElements[0].distance, object: hoveredElements[0].object } }else return intersectPoint } */ getEventDesc(e,isTouch){//搜集dispatchEvent要给的一般数据 let o = { viewer: this.viewer, mouse: this.mouse, pointer:this.pointer, drag :this.drag, isTouch, dragViewport : this.dragViewport, hoverViewport: this.hoverViewport, // button: isTouch ? 0 : e.button, //intersectPoint:this.intersectPoint, hoveredElement: this.hoveredElements[0], intersect: this.intersect//this.getWholeIntersect() , //可能包含mesh上的,针对融合页面 }; if(e){ o.isAtDomElement = e.target == this.domElement; } if(isTouch){ o.touches = this.touches; }else if(e){ o.button = e.button; o.buttons = e.buttons; } return o; } dealPointerUp(e,isTouch){ if(!this.drag){// 在canvas外mousedown return } this.drag.end.copy(this.pointer); if(isTouch && e.touches.length >= 1){ return } let now = Date.now(); if (this.logMessages) console.log(this.constructor.name + ': onMouseUp'); e.preventDefault(); let pressDistance = this.mouseDownMouse.distanceTo(this.mouse); let pressTime = now - this.pointerDownTime; let noMovement = this.drag.pointerDelta.length() == 0;//this.getNormalizedDrag().length() === 0; let consumed = false; let consume = () => { return consumed = true; }; //if (this.hoveredElements.length === 0) { /* for (let inputListener of this.getSortedListeners()) { inputListener */this.viewer.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'global_mouseup', pressDistance, consume, } )); /* if(consumed){//?? break; } */ //} //} if (this.hoveredElements.length > 0) { let hovered = this.hoveredElements .map(e => e.object) .find(e => (e._listeners && e._listeners['mouseup'])); if(hovered){ hovered.dispatchEvent({ type: 'mouseup', viewer: this.viewer, consume: consume }); } } if (this.drag) { //拖拽结束 if (this.drag.object/* && e.button == THREE.MOUSE.LEFT */) {//add LEFT if (this.logMessages) console.log(`${this.constructor.name}: drop ${this.drag.object.name}`); this.drag.object.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'drop', pressDistance, } )); } else { this.viewer.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'global_drop', pressDistance } )); } // check for a click if(pressDistance < Potree.config.clickMaxDragDis && pressTimee.object._listeners['click']); if(clickElement){ let canceled; let cancel = () => { return canceled = true; }; //console.log('clickElement',clickElement) if (this.logMessages) console.log(`${this.constructor.name}: click ${clickElement.name}`); clickElement.object.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'click', pressDistance , cancel } )); if(canceled){//比如只需要右键的话,可以忽视左键的点击 clickElement = null; } } } let selectable; if(/* !consumed && */ !this.fixSelection){ if (e.button === MOUSE.LEFT) { //if (noMovement) { selectable = this.hoveredElements.find(el => el.object._listeners && el.object._listeners['select']); if (selectable) { selectable = selectable.object; if (this.isSelected(selectable)) { this.deselectAll(); } else { this.deselectAll(); this.toggleSelection(selectable); } consumed = true; //add } else { if(this.selection.length>0)consumed = true; //add 取消选择后,阻断后续 this.deselectAll(); } //} } else if ((e.button === MOUSE.RIGHT) /* && noMovement */) { this.deselectAll(); } } let consume = () => { return consumed = true; }; let desc = this.getEventDesc(e,isTouch); if(!consumed){ this.viewer.dispatchEvent($.extend( desc, { type: 'global_click', pressDistance, clickElement:clickElement/* || selectable */, consume } )); } //增加 单击: this.needSingleClick = true; consumed || setTimeout(()=>{ if(this.needSingleClick){ this.viewer.dispatchEvent($.extend( desc, { type: 'global_single_click', pressDistance, clickElement } )); } }, Potree.config.doubleClickTime+1); //自行执行双击: if(now - this.lastClickTime < Potree.config.doubleClickTime){ this.onDoubleClick(e); } this.lastClickTime = now; } this.drag = null; } this.dragViewport = null; } onMouseUp (e) { this.dealPointerUp( e ); } getPointerInViewport(clientX, clientY, viewForceAt, pointer ){ let rect = this.domElement.getBoundingClientRect(); let x = clientX - rect.left; let y = clientY - rect.top; let camera; let viewport; pointer = pointer ||this.pointer; //if(this.viewer.viewports || viewForceAt){ var getDimension = (view)=>{ var left = Math.ceil(this.domElement.clientWidth * view.left) , bottom = Math.ceil(this.domElement.clientHeight * view.bottom) , width = Math.ceil(this.domElement.clientWidth * view.width) , height = Math.ceil(this.domElement.clientHeight * view.height) , top = this.domElement.clientHeight - bottom - height; return {left, bottom, width, height, top} }; var getView = (view, left, bottom, width, height, top)=>{ this.mouse.set(x-left, y - top ); Utils.convertScreenPositionToNDC(pointer, this.mouse, width, height); //console.log('更新pointer2',this.pointer.toArray()) camera = view.camera; viewport = view; }; if(viewForceAt){ let {left, bottom, width, height, top} = getDimension(viewForceAt); getView(viewForceAt, left, bottom, width, height, top); }else { var length = this.viewer.viewports.length; //var getif = false for(var i=0;i= left && x <= left + width && y >= top && y <= top + height){ getView(view, left, bottom, width, height, top); //getif = true break; } } } return { camera, viewport, pointer } } ifBlockedByIntersect({point, margin=0, cameraPos, pickWindowSize, pano, useDepthTex, viewport}={}){//某点是否被遮挡(不允许camera修改位置, 因为depthTex不好置换) viewport = viewport || this.hoverViewport || viewer.mainViewport; let intersect = this.getIntersect({viewport, onlyGetIntersect:true, pickWindowSize, useDepthTex, point, cameraPos, pano }); let cameraPos_ = (!cameraPos && pano) ? pano.position : (cameraPos||viewport.view.position); if(intersect && intersect.distance+margin <= point.distanceTo(cameraPos_)){ return intersect //被遮挡 } //点云模式,对没加载出的点云不准确。 尤其是需要修改相机位置时,因临时修改并不能使点云加载。 } collectClosePoints(viewport, forceUpdate){//获取吸附点 if(!Potree.settings.adsorption)return let point2ds = []; //吸附测量线端点 viewer.scene.measurements.forEach(e=>{ if(this.measuring.includes(e)) return//不吸附到正在拖拽的自身 point2ds.push(...e.getPointsPos2d(viewport, forceUpdate)); }); return point2ds } getIntersect({viewport, onlyGetIntersect, pickWindowSize, dontIntersect, usePointcloud, useDepthTex, cameraPos, point, pano, clientX, clientY}={}){// usePointcloud:必须使用点云 let intersect, intersectPoint, intersectOnModel, allElements; let camera = viewport.camera; let raycaster; viewer.addTimeMark('getIntersect','start'); let getByDepthTex = ()=>{ let intersect; if(point){ let cameraPos = pano ? pano.position : camera.position; let dir = new Vector3().subVectors(point, cameraPos).normalize(); intersect = {dir}; }else { intersect = Utils.getIntersect(camera, [viewer.images360.cube], this.pointer, raycaster); } intersectPoint = viewer.images360.depthSampler.sample(intersect, pano, !!point); //可能不准确, 因pano可能未加载depthTex if(intersectPoint && Potree.settings.depTexLocBindDataset){ intersectPoint.pointcloud = ( pano || viewer.images360.currentPano).pointcloud; //在全景模式下,虽然深度图上的点可能对应别的pointcloud,但因为是在当前全景图处得到的,所以即使将原本对应的点云移走,该点也不移动是有道理的。它可以永远跟着该全景图。 } }; let getByCloud = ()=>{ let pointer, mouse; if(point){//指定了目标点,而非只是用pointer所在位置 cameraPos && camera.position.copy( cameraPos); camera.lookAt(point); camera.updateMatrixWorld(); pointer = this.pointer.clone(); mouse = this.mouse.clone(); this.pointer.set(0,0); //画布中心 this.mouse.set(Math.round(viewport.resolution.x/2), Math.round(viewport.resolution.y/2)); } intersectPoint = (viewport.noPointcloud || dontIntersect)? null : Utils.getMousePointCloudIntersection( viewport, this.mouse, this.pointer, camera, this.viewer, this.viewer.scene.pointclouds, {pickClipped: true, measuring: this.measuring.length>0, pickWindowSize, cameraChanged: !!point } ); //恢复 if(point){ viewport.view.applyToCamera(camera); this.pointer.copy(pointer); this.mouse.copy(mouse); } }; if(this.measuring.length && Potree.settings.adsorption ){//吸附 let points = this.collectClosePoints(viewport); let points2 = points.filter(e=>e.trueSide && e.inSight && math.closeTo(this.mouse, e.posInViewport, Potree.config.measure.adsorptMinDis) ); let disArr = points2.map(e=> e.pos.distanceToSquared(this.mouse) ); let min = points2.slice().sort((a,b)=>disArr[points.indexOf(a)] - disArr[points.indexOf(b)]); if(min[0]){ intersect = { //hoveredElement location: min[0].pos3d, //point: {normal: allElements[0].face.normal }, //normal //distance object: min[0].object, adsorption:true }; console.log('找到吸附点', min[0].pos3d, min[0].object.uuid); } } if(!intersect){ let canUseDepthTex = (Potree.settings.displayMode == 'showPanos' || useDepthTex) && viewer.images360.currentPano.pointcloud.hasDepthTex && viewport == viewer.mainViewport && !usePointcloud; if(canUseDepthTex)getByDepthTex(); else getByCloud(); /* if(canUseDepthTex && this.measuring.length){ getByDepthTex() }else{ getByCloud() if(!intersectPoint && canUseDepthTex ){ //若在测量,先尝试点云,再用全景 //后来发现有深度图的点云全景visibleNode为空,pick不到的 getByDepthTex() } } */ //console.log(viewport.name , intersectPoint && intersectPoint.location ) if(Potree.settings.intersectOnObjs && !dontIntersect){ if(point){ raycaster = new Raycaster(); var dir = new Vector3().subVectors(point, camera.position).normalize(); raycaster.set(camera.position, dir); //var origin = new THREE.Vector3(pointer.x, pointer.y, -1).unproject(camera), } allElements = this.getHoveredElements(viewer.objs.children, true, raycaster); if(allElements[0]){ let quaternion = new Quaternion; let nor = allElements[0].face && allElements[0].face.normal; nor && allElements[0].oriObject.matrixWorld.decompose( new Vector3, quaternion, new Vector3 ); intersectOnModel = {//模拟点云的intersectPoint的结构写法 hoveredElement : allElements[0] , location: allElements[0].point, //point: {normal: allElements[0].face.normal }, normal: nor && nor.applyQuaternion(quaternion), distance: allElements[0].distance, object: allElements[0].object }; } } if(intersectPoint && intersectOnModel){ if(intersectPoint.distance < intersectOnModel.distance){ intersect = intersectPoint; }else { intersect = intersectOnModel; } }else { intersect = intersectOnModel || intersectPoint; } } if(viewport.camera.type == 'OrthographicCamera'/* == 'mapViewport' */){ let pos3d = new Vector3(this.pointer.x,this.pointer.y,-1).unproject(viewport.camera); //z:-1朝外 if(!intersect){ intersect = {}; } intersect.orthoIntersect = pos3d.clone(); } //记录全部hover到的: if(intersect){ intersect.allElements = allElements; intersect.pointclouds = intersectPoint ? intersectPoint.pointclouds : []; } viewer.addTimeMark('getIntersect','end'); //点云费时:2-15ms //深度图费时: 0.1-0.2ms /* intersect && intersect.location && intersect.location.applyMatrix4(viewer.scene.scene.matrix)//add */ if(onlyGetIntersect){ return intersect } if (intersect) { if(viewer.showCoordType){ //显示坐标位置时 let pos = intersect.point.position.toArray(); if(viewer.showCoordType == "local"){ }else if(viewer.showCoordType == "lonlat"){ pos = viewer.transform.lonlatToLocal.inverse(pos); }else { pos = viewer.transform.lonlatToLocal.inverse(pos); pos = viewer.transform.lonlatTo4550.forward(pos); } viewer.dispatchEvent({ type : "coordinateChange", pos }); } } //console.log('getIntersect', !!intersectPoint) this.intersect = intersect; intersect && (this.hoverViewport.lastIntersect = intersect); return intersect } onMouseMove (e) { return this.dealPointerMove( e ) } dealPointerMove(e, isTouch){ if(this.interactHistory.move) return //一帧只触发一次 this.interactHistory.move = 1; if(isTouch){ var { camera, viewport } = this.updateTouchesInfo(e); }else { var { camera, viewport } = this.getPointerInViewport(e.clientX, e.clientY, this.dragViewport); } this.hoverViewport = viewport; if(!viewport)return//刚变化viewport时会找不到 let isFlying = this.viewer.viewports.some(e=>e.view.isFlying()) || viewer.scene.cameraAnimations.some(c=>c.onUpdate); let intersect; if(e.onlyGetIntersect || (!this.drag || this.drag.object || viewport.alignment ) ){ //没有拖拽物体,但按下鼠标了的话,不intersect。触屏的就能直接避免intersect let dontIntersect = this.drag && viewport.alignment || isFlying || !Potree.settings.intersectWhenHover; // flying 时可能卡顿 //console.log('dontIntersectPointcloud',dontIntersectPointcloud) intersect = this.getIntersect(Object.assign({}, e, {viewport, dontIntersect, clientX:e.clientX, clientY:e.clientY })); //数据集多的时候卡顿 } if(e.onlyGetIntersect){ return intersect } e.preventDefault(); if (this.drag) {//有拖拽(不一定拖拽了物体, 也不一定按下了鼠标) this.drag.mouse = isTouch ? 1 : e.buttons; //add: //this.drag.pointer = this.pointer.clone(); //this.drag.hoverViewport = this.hoverViewport this.drag.pointerDelta.subVectors(this.pointer, this.drag.end); this.drag.end.copy(this.pointer); let dragConsumed = false; if (this.drag.object && (e.buttons == Buttons$1.NONE || !this.drag.notPressMouse )){//如果是本不需要按鼠标的拖拽,但按下了鼠标,就不执行这段(改为拖拽场景,如添加测量时突然拖拽画面) if (this.logMessages) console.log(this.constructor.name + ': drag: ' + this.drag.object.name); let refused; this.drag.object.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'drag', //拖拽物体 refuse: () => {refused = true;} } )); if(!refused)dragConsumed = true; viewer.dispatchEvent('content_changed'); } if(!dragConsumed){ if (this.logMessages) console.log(this.constructor.name + ': drag: '); this.viewer.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'global_drag', //拖拽画面 //consume: () => {dragConsumed = true;} } )); } } if(!isTouch || e.touches.length == 1){ if((!this.drag || this.drag.notPressMouse || Potree.settings.intersectOnObjs && this.drag.object) && !isFlying){ /* let blacklist = this.drag && this.drag */ let hoveredElements = this.getHoveredElements( ); if(hoveredElements.length > 0){ let names = hoveredElements.map(h => h.object.name).join(", "); if (this.logMessages) console.log(`${this.constructor.name}: onMouseMove; hovered: '${names}'`); } let cur = hoveredElements.find(a => a.object); let curr = cur && cur.object;//hoveredElements.map(a => a.object).find(a => true);//只取第一个 let prev = this.lastMouseoverElement; //this.hoveredElements.map(a => a.object).find(a => true); if(curr !== prev){ if(curr){ if (this.logMessages) console.log(`${this.constructor.name}: mouseover: ${curr.name}`); curr.dispatchEvent({ type: 'mouseover', object: curr, hoveredElement:cur }); } if(prev){ if (this.logMessages) console.log(`${this.constructor.name}: mouseleave: ${prev.name}`); prev.dispatchEvent({ type: 'mouseleave', object: prev, }); } this.lastMouseoverElement = curr; viewer.dispatchEvent('content_changed'); } if(hoveredElements.length > 0){ let object = hoveredElements .map(e => e.object) .find(e => (e._listeners && e._listeners['mousemove'])); if(object){ object.dispatchEvent({ type: 'mousemove', object: object }); } } this.hoveredElements = hoveredElements; } //this.intersect = this.getWholeIntersect() this.viewer.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'global_mousemove', } )); this.containsMouse = true; } } onMouseWheel(e){ if(!this.enabled) return; if(this.logMessages) console.log(this.constructor.name + ": onMouseWheel"); e.preventDefault(); let delta = 0; if (e.wheelDelta !== undefined) { // WebKit / Opera / Explorer 9 delta = e.wheelDelta; } else if (e.detail !== undefined) { // Firefox delta = -e.detail; } let ndelta = Math.sign(delta); // this.wheelDelta += Math.sign(delta); if(!this.hoverViewport){//调试手机版时会无 var { viewport } = this.getPointerInViewport(e.clientX, e.clientY ); this.hoverViewport = viewport; } if (this.hoveredElement) { this.hoveredElement.object.dispatchEvent($.extend( this.getEventDesc(e,isTouch), { type: 'mousewheel', delta: ndelta, object: this.hoveredElement.object } )); } else { this.viewer.dispatchEvent($.extend( this.getEventDesc(e), { type: 'global_mousewheel', delta: ndelta, } )); } setTimeout(()=>{ this.dealPointerMove(e );//add 在更新完view后重新获取intersect 和 drag },1);//只延迟1会崩溃吗 } startDragging (object, args = null) { let name = object ? object.name : "no name"; if (this.logMessages) console.log(`${this.constructor.name}: startDragging: '${name}'`); this.drag = { start: this.pointer.clone(), end: this.pointer.clone(), pointerDelta: new Vector2(0, 0), object: object, hoverViewport: this.hoverViewport, //会变化 dragViewport: this.hoverViewport, //不变 }; if (args) { for (let key of Object.keys(args)) { this.drag[key] = args[key]; } } if(object){ object.dispatchEvent($.extend( this.getEventDesc(), { type: 'startDragging' } )); } } /* getMousePointCloudIntersection (mouse) { return Utils.getMousePointCloudIntersection( this.mouse, this.scene.getActiveCamera(), this.viewer, this.scene.pointclouds); } */ toggleSelection (object) { let oldSelection = this.selection; let index = this.selection.indexOf(object); if (index === -1) { this.selection.push(object); object.dispatchEvent({ type: 'select' }); } else { this.selection.splice(index, 1); object.dispatchEvent({ type: 'deselect' }); } this.dispatchEvent({ type: 'selection_changed', oldSelection: oldSelection, selection: this.selection }); viewer.dispatchEvent('content_changed'); } deselect(object){ let oldSelection = this.selection; let index = this.selection.indexOf(object); if(index >= 0){ this.selection.splice(index, 1); object.dispatchEvent({ type: 'deselect' }); this.dispatchEvent({ type: 'selection_changed', oldSelection: oldSelection, selection: this.selection }); } viewer.dispatchEvent('content_changed'); } deselectAll () { for (let object of this.selection) { object.dispatchEvent({ type: 'deselect' }); } let oldSelection = this.selection; if (this.selection.length > 0) { this.selection = []; this.dispatchEvent({ type: 'selection_changed', oldSelection: oldSelection, selection: this.selection }); } viewer.dispatchEvent('content_changed'); } isSelected (object) { let index = this.selection.indexOf(object); return index !== -1; } registerInteractiveObject(object){ this.interactiveObjects.add(object); } removeInteractiveObject(object){ this.interactiveObjects.delete(object); } registerInteractiveScene (scene) { let index = this.interactiveScenes.indexOf(scene); if (index === -1) { this.interactiveScenes.push(scene); } } unregisterInteractiveScene (scene) { let index = this.interactiveScenes.indexOf(scene); if (index > -1) { this.interactiveScenes.splice(index, 1); } } getHoveredElement () { let hoveredElements = this.getHoveredElements(); if (hoveredElements.length > 0) { return hoveredElements[0]; } else { return null; } } getHoveredElements (interactables, dontCheckDis, raycaster) { if(!interactables){ let scenes = this.hoverViewport.interactiveScenes || this.interactiveScenes.concat(this.scene); let interactableListeners = ['mouseup', 'mousemove', 'mouseover', 'mouseleave', 'drag', 'drop', 'click', 'select', 'deselect']; interactables = []; for (let scene of scenes) { scene.traverseVisible(node => {//检测加了侦听的object if (node._listeners && node.visible && !this.blacklist.has(node) ) { let hasInteractableListener = interactableListeners.filter((e) => { return node._listeners[e] !== undefined; }).length > 0; if (hasInteractableListener) { interactables.push(node); } } }); } }else interactables = interactables.filter(e=>e.visible); let camera = this.hoverViewport.camera; if(!raycaster){ let ray = Utils.mouseToRay(this.pointer, camera ); raycaster = new Raycaster(); raycaster.ray.set(ray.origin, ray.direction); raycaster.camera = camera; //add } if(camera.type == "OrthographicCamera"){//使无论多远,threshold区域都是一样宽的 raycaster.params.Line.threshold = 20/camera.zoom; }else { raycaster.params.Line.threshold = 0.04; //相对长度 } raycaster.params.Line2 = {threshold : browser.isMobile()?100:20 }; //拓宽的lineWidth //raycaster.layers.enableAll()//add Potree.Utils.setCameraLayers(raycaster, //设置能识别到的layers(如空间模型里只有mapViewer能识别到marker) ['sceneObjects','mapObjects','measure', 'transformationTool', 'model', 'bothMapAndScene'], this.hoverViewport && this.hoverViewport.extraEnableLayers ); //this.hoverViewport.beforeRender && this.hoverViewport.beforeRender() viewer.dispatchEvent( {type:'raycaster', viewport: this.hoverViewport, raycaster, viewer:this.viewer, interactables });//add let intersections = raycaster.intersectObjects(interactables , true, null, true); //原本是false 检测不到children let intersectionsCopy = intersections.slice(); if(this.intersect && this.intersect.distance != void 0 && !dontCheckDis){//add intersections = intersections.filter(e=>{ if( this.intersect.hoveredElement && this.intersect.hoveredElement.oriObject == e.object)return true let material = e.object.material; return e.object.pickDontCheckDis || ( material.depthTest == false || material.depthWrite == false) && !material.realUseDepth //!material.depthTestWhenPick || ( material.useDepth ? e.distance <= this.intersect.distance + material.uniforms.clipDistance.value : e.distance <= this.intersect.distance ) //maxClipFactor是否需要考虑? }); } intersections = intersections.map(e=>{//add 转化为interactables var object = e.object; do{ if(interactables.includes(object)) { e.oriObject = e.object; e.object = object; break } object = object.parent; }while(object) return e }); //add for测量线,在检测到sphere时优先选中sphere而非线 //intersections = intersections.sort(function(a,b){return b.object.renderOrder-a.object.renderOrder}) // 降序 intersections = intersections.sort(function(a,b){ let order2 = b.object.pickOrder || 0; let order1 = a.object.pickOrder || 0; return order2-order1 }); // 降序 //console.log('getHoveredElement ', intersections) return intersections; } /* setScene (scene) { this.deselectAll(); this.scene = scene; } */ update (delta) { } /*getNormalizedDrag () { if (!this.drag) { return new THREE.Vector2(0, 0); } let diff = new THREE.Vector2().subVectors(this.drag.end, this.drag.start); diff.x = diff.x / this.domElement.clientWidth; diff.y = diff.y / this.domElement.clientHeight; return diff; } getNormalizedLastDrag () { if (!this.drag) { return new THREE.Vector2(0, 0); } let mouseDelta = this.drag.mouseDelta.clone(); mouseDelta.x = mouseDelta.x / this.domElement.clientWidth; mouseDelta.y = mouseDelta.y / this.domElement.clientHeight; return mouseDelta; } */ getMouseDirection(pointer) {//add pointer = pointer || this.pointer; let camera = this.hoverViewport.camera; var t = new Vector3(pointer.x, pointer.y, -1).unproject(camera), i = new Vector3(pointer.x, pointer.y, 1).unproject(camera); return {origin: t, direction:i.clone().sub(t).normalize() } } } class ViewerBase extends EventDispatcher{ constructor(domElement, args = {}){ super(); this.name = args.name; this.renderArea = domElement; this.oldResolution = new Vector2(); this.oldResolution2 = new Vector2(); this.screenSizeInfo = { W:0, H:0, pixelRatio:1 , windowWidth:0, windowHeight:0 }; this.initContext(args); this.addEventListener('content_changed', ()=>{//画面改变,需要渲染 this.needRender = true; //console.log('needRender') }); } initContext(args){ //console.log(`initializing three.js ${THREE.REVISION}`); let width = this.renderArea.clientWidth; let height = this.renderArea.clientHeight; let contextAttributes = { alpha: true,//支持透明 depth: true, stencil: false, antialias: !!args.antialias, preserveDrawingBuffer: args.preserveDrawingBuffer || false , powerPreference: "high-performance", }; let canvas = document.createElement("canvas"); let context = canvas.getContext('webgl2', contextAttributes ); //不用webgl2是因为有的写法在webgl2不支持 如gl_FragDepthEXT if(context){ Potree.settings.isWebgl2 = true; } this.renderer = new WebGLRenderer({ premultipliedAlpha: false, canvas: canvas, context: context, }); this.renderer.sortObjects = true; //原先false 打开了renderOrder才奏效 //this.renderer.setSize(width, height); this.renderer.autoClear = args.autoClear || false; //args.clearColor = args.clearColor || '#aa0033' args.clearColor && this.renderer.setClearColor(args.clearColor); this.renderArea.appendChild(this.renderer.domElement); this.renderer.domElement.tabIndex = '2222'; this.renderer.domElement.style.position = 'absolute'; this.renderer.domElement.addEventListener('mousedown', () => { this.renderer.domElement.focus(); }); //this.renderer.domElement.focus(); // NOTE: If extension errors occur, pass the string into this.renderer.extensions.get(x) before enabling // enable frag_depth extension for the interpolation shader, if available let gl = this.renderer.getContext(); gl.getExtension('EXT_frag_depth'); gl.getExtension('WEBGL_depth_texture'); gl.getExtension('WEBGL_color_buffer_float'); // Enable explicitly for more portability, EXT_color_buffer_float is the proper name in WebGL 2 if(gl.createVertexArray == null){ let extVAO = gl.getExtension('OES_vertex_array_object'); if(!extVAO){ throw new Error("OES_vertex_array_object extension not supported"); } gl.createVertexArray = extVAO.createVertexArrayOES.bind(extVAO); gl.bindVertexArray = extVAO.bindVertexArrayOES.bind(extVAO); } /* let oldClear = gl.clear; gl.clear = (bits)=>{ console.error('clear') } */ } updateScreenSize(o={}) { //有可能需要让viewport来判断,当窗口大小不变但viewport大小变时 if(this.screenshoting && !o.forceUpdateSize) return //截图时不允许因窗口改变大小而updateScreenSize var render = false, ratio, w, h; //记录应当render的大小 if (o.width != void 0 && o.height != void 0) { w = o.width; h = o.height; render = true; ratio = 1; }else { w = this.renderArea.clientWidth; h = this.renderArea.clientHeight; if(w !== this.screenSizeInfo.W || h !== this.screenSizeInfo.H || o.forceUpdateSize || this.screenSizeInfo.pixelRatio != window.devicePixelRatio){ this.screenSizeInfo.W = w; this.screenSizeInfo.H = h; render = true; this.screenSizeInfo.pixelRatio = window.devicePixelRatio; //如果player放在小窗口了,也要监测devicePixelRatio,因为缩放时client宽高不会改变 //config.isMobile ? (ratio = Math.min(window.devicePixelRatio, 2)) : (ratio = window.devicePixelRatio) ratio = window.devicePixelRatio; } } if (render) { this.setSize(w, h, ratio, o.forTarget ); } } setSize(width, height, devicePixelRatio, onlyForTarget){ //console.log('setSize', width) if(!onlyForTarget){//onlyForTarget表示不更改当前renderer,只是为了rendertarget才要改变viewport this.renderer.setPixelRatio(devicePixelRatio); this.renderer.setSize(width, height ); // resize之后会自动clear(似乎因为setScissor ),所以一定要立刻绘制,所以setSize要在cameraChanged、update之前 } //this.composer && this.composer.setSize(width, height); if(this.viewports){ this.viewports.forEach((view,i)=>{ //if(!view.active)return var width_ = width * view.width; var height_ = height * view.height; view.setResolution(Math.ceil(width_), Math.ceil(height_), width, height ); if(height_ == 0)return //avoid NAN let aspect = width_ / height_; //camera的参数精确些,不用视口的归整的resolution像素值,否则hasChange无法为true, 导致canvasResize了但map没update从而闪烁 view.camera.aspect = aspect; if(view.camera.type == "OrthographicCamera"){ /* //不改宽度 同4dkk var heightHalf = view.camera.right / aspect view.camera.top = heightHalf view.camera.bottom = -heightHalf */ //高宽都改 使大小不随视口大小改变 navvis (直接和视口大小一致即可,通过zoom来定大小) view.camera.left = -width_/2; view.camera.right = width_/2; view.camera.bottom = -height_/2; view.camera.top = height_/2; }else { } view.camera.updateProjectionMatrix(); }); } if(!onlyForTarget){//因为onlyForTarget不传递devicePixelRatio所以不发送了 this.dispatchEvent('viewerResize'); this.viewports.forEach(e=>{ this.ifEmitResize({viewport:e, deviceRatio:devicePixelRatio}); }); } } ifEmitResize(e){//切换viewport渲染时, 若这些viewport大小不同就发送一次, 通知一些材质更新resolution。 //console.log('ifEmitResize',e.viewport.name,e.viewport.resolution2 ) if(!e.viewport.resolution.equals(this.oldResolution)||!e.viewport.resolution2.equals(this.oldResolution2)){ this.dispatchEvent($.extend(e, {type:'resize'})); this.oldResolution.copy(e.viewport.resolution); this.oldResolution2.copy(e.viewport.resolution2); } } cameraChanged() {//判断相机是否改变 var changed = false; /* if(this.needRender){ this.needRender = false return true } */ for(let i=0,j=this.viewports.length;i -1){ this.extraEnableLayers.splice(index, 1); } } cameraChanged() { var copy = ()=>{ this.previousState = { projectionMatrix: this.camera.projectionMatrix.clone(),//worldMatrix在this.control时归零了所以不用了吧,用position和qua也一样 position: this.camera.position.clone(), quaternion: this.camera.quaternion.clone(), active:this.active, resolution:this.resolution.clone(), resolution2:this.resolution2.clone(), //有时clientWidth没变但是ratio缩放了 }; }; let projectionChanged = true, positionChanged = true, quaternionChanged = true, activeChanged = true, resolutionChanged = true; let getChanged = ()=>{ return { projectionChanged,positionChanged,quaternionChanged, activeChanged, resolutionChanged, changed:projectionChanged || positionChanged || quaternionChanged || activeChanged || resolutionChanged } }; if (this.previousState){ projectionChanged = !this.camera.projectionMatrix.equals(this.previousState.projectionMatrix); positionChanged = !this.camera.position.equals(this.previousState.position); quaternionChanged = !this.camera.quaternion.equals(this.previousState.quaternion); activeChanged = this.active != this.previousState.active; resolutionChanged = !this.resolution.equals(this.previousState.resolution) || !this.resolution2.equals(this.previousState.resolution2); } copy(); return getChanged() } setResolution(w,h, wholeW=0, wholeH=0){ this.resolution.set(w,h);//是client的width height this.resolution2.copy(this.resolution).multiplyScalar(this.pixelRatio || window.devicePixelRatio); this.offset.set(wholeW,wholeH).multiply(new Vector2(this.left,this.bottom));//.multiplyScalar(window.devicePixelRatio) this.dispatchEvent({type:'resize'}); } } const prefixVertex ="precision highp float;\nprecision highp int;\n\nuniform mat4 modelMatrix;\nuniform mat4 modelViewMatrix;\nuniform mat4 projectionMatrix;\nuniform mat4 viewMatrix;\nuniform mat3 normalMatrix;\nuniform vec3 cameraPosition;\n attribute vec3 position;\n attribute vec3 normal;\n attribute vec2 uv;\n"; const prefixFragment ="precision highp float;\nprecision highp int;\n\nuniform mat4 viewMatrix;\nuniform vec3 cameraPosition;\n"; let shader = { uniforms: { opacity: { type: "f", // value: 1 }, progress: { type: "f", value: 0 }, pano0Map: { type: "t", value: null }, pano1Map: { type: "t", value: null }, depthMap0: { type: "t", value: null }, depthMap1: { type: "t", value: null }, pano0Position: { type: "v3", value: new Vector3 }, pano0Matrix: { type: "m4", value: new Matrix4 }, pano1Position: { type: "v3", value: new Vector3 }, pano1Matrix: { type: "m4", value: new Matrix4 }, /* pano1Matrix2: { type: "m4", value: new THREE.Matrix4 }, */ inverseProjectionMatrix: { value: new Matrix4 }, /* projectionMatrix:{//需要再写一遍吗 value: new THREE.Matrix4 }, */ viewport: { value: new Vector4 }, //如 {x: 0, y: 0, z: 428, w: 969} xy应该是offset, zw是宽高 cameraHeight0: { type: "f", value: 1 }, cameraHeight1: { type: "f", value: 1 }, ceilHeight0:{ type: "f", value: 2 }, ceilHeight1:{ type: "f", value: 2 }, }, vertexShader: prefixVertex + ` uniform vec3 pano0Position; uniform mat4 pano0Matrix; uniform vec3 pano1Position; uniform mat4 pano1Matrix; varying vec2 vUv; varying vec3 vWorldPosition0; varying vec3 vWorldPosition1; varying vec3 vWorldPosition12; vec3 transformAxis( vec3 direction ) //navvis->4dkk { float y = direction.y; direction.y = direction.z; direction.z = -y; return direction; } void main() { vUv = uv; vec4 worldPosition = modelMatrix * vec4(position, 1.0); vec3 positionLocalToPanoCenter0 = worldPosition.xyz - pano0Position; vWorldPosition0 = (vec4(positionLocalToPanoCenter0, 1.0) * pano0Matrix).xyz; vWorldPosition0.x *= -1.0; vWorldPosition0 = transformAxis(vWorldPosition0); vec3 positionLocalToPanoCenter1 = worldPosition.xyz - pano1Position; vWorldPosition1 = (vec4(positionLocalToPanoCenter1, 1.0) * pano1Matrix).xyz; vWorldPosition1.x *= -1.0; vWorldPosition1 = transformAxis(vWorldPosition1); /* vec3 positionLocalToPanoCenter12 = worldPosition.xyz - pano1Position; vWorldPosition12 = (vec4(positionLocalToPanoCenter12, 1.0) * pano1Matrix2).xyz; vWorldPosition12.x *= -1.0; vWorldPosition12 = transformAxis(vWorldPosition12); */ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); } `, fragmentShader: prefixFragment + ` #extension GL_EXT_frag_depth : enable // otherwise error: 'GL_EXT_frag_depth' : extension is disabled #define PI 3.141592653 uniform float modelAlpha; uniform float opacity; uniform float progress; uniform int tranType; uniform vec3 pano0Position; uniform vec3 pano1Position; uniform float maxDistance; uniform float minDistance; uniform float minOpa; uniform samplerCube pano0Map; uniform samplerCube pano1Map; varying vec2 vUv; varying vec3 vWorldPosition0; varying vec3 vWorldPosition1; /* vec2 getSamplerCoord( vec3 direction ) { direction = normalize(direction); float tx=atan(direction.x,-direction.y)/(PI*2.0)+0.5; float ty=acos(direction.z)/PI; return vec2(tx,ty); } */ vec2 getSamplerCoord2( vec3 direction ) { direction = normalize(direction); float tx=atan(direction.x,direction.z)/(PI*2.0)+0.5; float ty=acos(direction.y)/PI; return vec2(tx,ty); } #if defined(GL_EXT_frag_depth) && defined(hasDepthTex) uniform sampler2D depthMap0; uniform sampler2D depthMap1; uniform mat4 inverseProjectionMatrix; uniform mat4 projectionMatrix; uniform vec4 viewport; uniform float cameraHeight0; uniform float cameraHeight1; uniform float ceilHeight0; uniform float ceilHeight1; vec2 getDepth(vec3 dir, sampler2D depthMap, float heightDown, float heightUp, vec4 eyePos){ vec2 depthValue = vec2(0.0, 0.0); vec2 uv2 = getSamplerCoord2( dir.xyz); //暂时只用基于目标漫游点的方向 uv2.x -= 0.25; //全景图和Cube的水平采样起始坐标相差90度,这里矫正 0.25 个采样偏移 vec4 depth = texture2D(depthMap, uv2); //float distance = depth.r + 256. * (depth.g + 256. * depth.b); //distance *= 255. * .001; // distance is now in meters //更改 float distance = (depth.g + depth.r / 256.) * 255.; if(distance == 0.0){//漫游点底部识别不到的区域,给一个地板高度 if(uv2.y < depthTexUVyLimit) distance = heightUp / dir.y; else if(uv2.y > 1.0 - depthTexUVyLimit) distance = heightDown / -dir.y; else distance = 100000.0;//给个超级远的值 } if(distance == 0.0)distance = 100000.0;//给个超级远的值 depthValue.x = distance; distance += .1; // add a safety margin vec4 eyePos2 = vec4(normalize(eyePos.xyz) * distance, 1.); vec4 clipPos2 = projectionMatrix * eyePos2; vec4 ndcPos2 = clipPos2 * 1. / clipPos2.w; depthValue.y = 0.5 * ((gl_DepthRange.far - gl_DepthRange.near) * ndcPos2.z + gl_DepthRange.near + gl_DepthRange.far); return depthValue; } //注:未加载好的话,depth为0,导致第一次漫游过去的时候许多mesh会立刻被遮挡,所以要确保加载完 #endif void main() { vec3 vWorldPosition0N = normalize(vWorldPosition0); vec3 vWorldPosition1N = normalize(vWorldPosition1); float progress_ = progress; vec4 colorFromPano0 = vec4(0.0,0.0,0.0,0.0); #if defined(usePanoMap0) //即progress < 1.0 通常是1 colorFromPano0=textureCube(pano0Map,vWorldPosition0N.xyz); #else progress_ = 1.0; #endif vec4 colorFromPano1=textureCube(pano1Map,vWorldPosition1N.xyz); gl_FragColor = mix(colorFromPano0,colorFromPano1,progress_); //深度图修改深度 #if defined(GL_EXT_frag_depth) && defined(hasDepthTex) vec4 ndcPos; ndcPos.xy = ((2.0 * gl_FragCoord.xy) - (2.0 * viewport.xy)) / (viewport.zw) - 1.; ndcPos.z = (2.0 * gl_FragCoord.z - gl_DepthRange.near - gl_DepthRange.far) / (gl_DepthRange.far - gl_DepthRange.near); ndcPos.w = 1.0; vec4 clipPos = ndcPos / gl_FragCoord.w; vec4 eyePos = inverseProjectionMatrix * clipPos; vec2 depth0 = vec2(0.0,0.0); #if defined(usePanoMap0) depth0 = getDepth(vWorldPosition0N, depthMap0, cameraHeight0, ceilHeight0, eyePos); #endif vec2 depth1 = getDepth(vWorldPosition1N, depthMap1, cameraHeight1, ceilHeight1, eyePos); gl_FragDepthEXT = mix(depth0.y,depth1.y,progress_); gl_FragDepthEXT = clamp(gl_FragDepthEXT, 0.0, 1.0); //防止部分手机出现黑块。ios 16 。 因为我给的超远值超出范围 #endif } ` }; //注:gl_FragDepthEXT 修改了确实能像真实mesh那样遮挡住在后面的物体。但是为过渡时不能直接像有模型那样,和角度有关。 class ModelTextureMaterial extends RawShaderMaterial { constructor( ){ let defines = {depthTexUVyLimit: Potree.config.depthTexUVyLimit}; let {vs,fs} = Common.changeShaderToWebgl2(shader.vertexShader, shader.fragmentShader, 'RawShaderMaterial'); super({ fragmentShader: fs, vertexShader: vs, uniforms: UniformsUtils.clone(shader.uniforms), side:DoubleSide, name: "ModelTextureMaterial", defines, }); this.glslVersion = Potree.settings.isWebgl2 && '300 es'; let setSize = (e)=>{ let viewport = e.viewport; //let viewportOffset = viewport.offset || new Vector2() let resolution = viewport.resolution2; //this.uniforms.viewport.value.set(viewportOffset.x, viewportOffset.y, resolution.x, resolution.y) this.uniforms.viewport.value.set(0,0, resolution.x, resolution.y);// xy是在viewport中的left和bottom,和整个窗口没有关系,所以不是viewportOffset。几乎都是0,0 }; let viewport = viewer.mainViewport; setSize({viewport}); viewer.addEventListener('resize',(e)=>{ if(e.viewport.name != "MainView")return setSize(e); }); //var supportExtDepth = !!Features.EXT_DEPTH.isSupported() { //add viewer.addEventListener('camera_changed', (e)=>{ if(e.viewport.name != "MainView")return //this.uniforms.projectionMatrix.value.copy(e.camera.projectionMatrix) e.camera && this.uniforms.inverseProjectionMatrix.value.copy(e.camera.projectionMatrixInverse); }); } let progress = 0; Object.defineProperty(this.uniforms.progress, 'value', { get: function () { return progress }, set: e => { if (e < 1 && !Potree.settings.fastTran ) { if (!('usePanoMap0' in this.defines)) { this.defines.usePanoMap0 = ''; this.needsUpdate = true; } } else { if ('usePanoMap0' in this.defines) { delete this.defines.usePanoMap0; this.needsUpdate = true; } } progress = e; }, }); //------------------------------------- } /** * * @param {Panorama} pano0 * @param {Panorama} pano1 * @param {boolean} flag 更新全景图的材质uniforms */ setProjectedPanos(pano0, pano1, progressValue ){ progressValue!=void 0 && (this.uniforms.progress.value = progressValue); //pano0.ensureSkyboxReadyForRender(); if(pano0){ this.uniforms.pano0Map.value = pano0.getSkyboxTexture();//pano0.texture this.uniforms.pano0Position.value.copy(pano0.position); this.uniforms.pano0Matrix.value.copy(pano0.panoMatrix/* pano0.mesh.matrixWorld */ ); //pano1.ensureSkyboxReadyForRender(); } this.uniforms.pano1Map.value = pano1.getSkyboxTexture();//pano1.texture; this.uniforms.pano1Position.value.copy(pano1.position); this.uniforms.pano1Matrix.value.copy(pano1.panoMatrix /* pano1.mesh.matrixWorld */ ); this.pano0 = pano0; this.pano1 = pano1; this.updateDepthTex(pano0); this.updateDepthTex(pano1); //console.log('setProjectedPanos', pano0&&pano0.id, pano1&&pano1.id) this.needsUpdate = true; } updateDepthTex(pano, extra){ if( !Potree.settings.useDepthTex || !pano || !pano.depthTex || pano!=this.pano0 && pano!=this.pano1)return //console.log('updateDepthTex', pano.id, this.pano0 && this.pano0.id, this.pano1 && this.pano1.id) if(this.pano0){ this.uniforms.depthMap0.value = this.pano0.entered ? this.pano0.depthTex : null; //dispose了就不要赋值否则dispose会失败 this.uniforms.cameraHeight0.value = this.pano0.floorPosition.distanceTo(this.pano0.position); this.uniforms.ceilHeight0.value = this.pano0.getCeilHeight() - this.pano0.position.z; } if(this.pano1){ this.uniforms.depthMap1.value = this.pano1.depthTex; //pano1还没entered时也需要,可能在飞入 this.uniforms.cameraHeight1.value = this.pano1.floorPosition.distanceTo(this.pano1.position); this.uniforms.ceilHeight1.value = this.pano1.getCeilHeight() - this.pano1.position.z; } this.updateDepthTexEnable(); } updateDepthTexEnable(){ let hasDepthTex = this.pano0 && this.pano1 && this.pano0.pointcloud.hasDepthTex && this.pano1.pointcloud.hasDepthTex; //暂时不知道一个有图一个没图怎么写所以 Potree.Utils.addOrRemoveDefine(this, 'hasDepthTex', hasDepthTex?'add':'remove' ); } /* EnableDepthTex(){//开启DepthTex if(this.defines['hasDepthTex']){ return } this.defines['hasDepthTex'] = '' this.needsUpdate = true; } */ } class FastTranPass { constructor(renderer){ this.renderer = renderer; this.coverRenderTarget = new WebGLRenderTarget( 100, 100, { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat }); this.coverTex = this.coverRenderTarget.texture; this.enabled = false; /* this.oldClearColor = new THREE.Color(); this.oldClearAlpha = 1; this.camera = new THREE.OrthographicCamera( - 1, 1, 1, - 1, 0, 1 ); this.scene = new THREE.Scene(); */ this.material = this.getMaskMaterial(); /* var copyShader = THREE.CopyShader; this.materialCopy = new THREE.ShaderMaterial( { uniforms: this.copyUniforms, vertexShader: copyShader.vertexShader, fragmentShader: copyShader.fragmentShader, blending: THREE.NoBlending, depthTest: false, depthWrite: false, transparent: true } ); this.quad = new THREE.Mesh( new THREE.PlaneBufferGeometry( 2, 2 ), this.material); this.quad.frustumCulled = false; // Avoid getting clipped this.scene.add( this.quad ); this.renderToScreen = true*/ } setSize( width, height ) { this.coverRenderTarget.setSize( width, height ); } start(){ this.enabled = true; let {x,y} = viewer.mainViewport.resolution2; this.setSize(x,y); //draw coverTex let oldTarget = this.renderer.getRenderTarget(); //let oldClearColor = this.renderer.getClearColor() this.renderer.setRenderTarget(this.coverRenderTarget); //this.renderer.setClearColor( 0x000000, 0) let oldLayer = viewer.mainViewport.camera.layers.mask; Potree.Utils.setCameraLayers(viewer.mainViewport.camera, ['skybox']); this.renderer.render( viewer.scene.scene, viewer.mainViewport.camera ); //this.renderer.setClearColor( 0x000000, 0) this.renderer.setRenderTarget(oldTarget); viewer.mainViewport.camera.layers.mask = oldLayer; this.material.uniforms.progress.value = 1; console.log('start111'); } render( scene, camera, viewports, renderer, writeBuffer, readBuffer ) { /* var oldAutoClear = renderer.autoClear; renderer.autoClear = false; */ let {x,y} = viewer.mainViewport.resolution2; var uniforms = this.material.uniforms; //uniforms.bgTex.value = readBuffer.texture; //更新 uniforms.coverTex.value = this.coverTex; uniforms.progress.value = viewer.images360.cube.material.uniforms.progress.value; uniforms.screenRatio.value = x / y; // 使波纹为圆形 uniforms.screenRatio.value *= uniforms.screenRatio.value; Potree.Utils.screenPass.render(viewer.renderer, viewer.images360.fastTranMaskPass.material); //renderer.autoClear = oldAutoClear; } stop(){ this.enabled = false; console.log('stop111'); } getMaskMaterial(){ return new ShaderMaterial( { uniforms: { coverTex: { type: "t", value: null }, progress:{ type: "f", value: 0 }, screenRatio:{ type: "f", value: 1 } }, vertexShader: ` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); } `, fragmentShader: ` uniform sampler2D coverTex; uniform float progress; uniform float screenRatio; varying vec2 vUv; void main() { const float maxRadius = 0.708; // sqrt(0.5^2+0.5^2) const float minRadius = 0.0 ; float radius = screenRatio>1.0 ? sqrt((vUv.x - 0.5)*(vUv.x - 0.5) + (vUv.y - 0.5)*(vUv.y - 0.5)/screenRatio) : sqrt((vUv.x - 0.5)*(vUv.x - 0.5)*screenRatio+ (vUv.y - 0.5)*(vUv.y - 0.5)); float diff = 0.292; //1.0-maxRadius; float radiusIn = maxRadius * progress + minRadius * (1.0-progress); float radiusOut = radiusIn + diff; if(radius < radiusIn) { discard; }else if(radius>radiusOut){ gl_FragColor = texture2D(coverTex, vUv) ; //gl_FragColor = vec4(1.0,1.0,0.0,1.0);// }else{ /* vec4 color1 = texture2D(bgTex, vUv); vec4 color2 = texture2D(coverTex, vUv); float rotio = smoothstep(radiusIn ,radiusOut,radius); gl_FragColor = mix(color1, color2, rotio); */ float rotio = smoothstep(radiusIn ,radiusOut, radius); vec4 color2 = texture2D(coverTex, vUv); color2.a = rotio; } } ` } ); } /* getMaskMaterial(){ return new THREE.ShaderMaterial( { uniforms: { coverTex: { type: "t", value: null }, bgTex: { type: "t", value: null }, progress:{ type: "f", value: 0 }, screenRatio:{ type: "f", value: 1 } }, vertexShader: ` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); } `, fragmentShader: ` uniform sampler2D coverTex; uniform sampler2D bgTex; uniform float progress; uniform float screenRatio; varying vec2 vUv; void main() { const float maxRadius = 0.708; // sqrt(0.5^2+0.5^2) const float minRadius = 0.0 ; float radius = screenRatio>1.0 ? sqrt((vUv.x - 0.5)*(vUv.x - 0.5) + (vUv.y - 0.5)*(vUv.y - 0.5)/screenRatio) : sqrt((vUv.x - 0.5)*(vUv.x - 0.5)*screenRatio+ (vUv.y - 0.5)*(vUv.y - 0.5)); float diff = 0.292; //1.0-maxRadius; float radiusIn = maxRadius * progress + minRadius * (1.0-progress); float radiusOut = radiusIn + diff; if(radius < radiusIn) { gl_FragColor = texture2D(bgTex, vUv); //gl_FragColor = vec4(0.0,0.0,1.0,1.0);// }else if(radius>radiusOut){ gl_FragColor = texture2D(coverTex, vUv) ; //gl_FragColor = vec4(1.0,1.0,0.0,1.0);// }else{ vec4 color1 = texture2D(bgTex, vUv); vec4 color2 = texture2D(coverTex, vUv); float rotio = smoothstep(radiusIn ,radiusOut,radius); gl_FragColor = mix(color1, color2, rotio); } } ` } ); } */ } let GLCubeFaces$1 = Potree.defines.GLCubeFaces; var TileUtils = {}; TileUtils.TILE_SIZE = 512, TileUtils.FACES_PER_PANO = 6, TileUtils.LocationOnTile = { Center: 0, UpperLeft: 1, UpperRight: 2, LowerRight: 3, LowerLeft: 4 }, /* * 获取某tile在cube中的方向 direction (向量起点在cube中心,终点在tile图的指定位置)。spherical通过先求uv,再直接得到dir * @param {*} size 面分辨率 * @param {*} cubeFace 所在面 * @param {*} Center 在tile上的目标位置,默认为中心,其他位置就是四个顶点 * @param {*} c 似乎是在tile的缩进百分比,根据所在面的不同,分别向不同方向缩进,但都是向tile的中心 * @param {*} dir 所求方向 */ TileUtils.getTileVector = function() {//获取某tile在cube中的方向 direction (向量起点在cube中心,终点在tile图的中心) return function(size, tileSize, cubeFace, tileX, tileY, Center, c, dir) {//c似乎是缩进百分比 Center = Center || TileUtils.LocationOnTile.Center; //假设该cube边长为2: var u = size / tileSize , d = tileX / u; tileY = -tileY + (u - 1); var p = tileY / u , f = tileSize / size , g = 2 * f //一个tile的宽度 (乘以2是因为cube边长是2) , m = g / 2 , v = 2 * d - 1 + m , A = 2 * p - 1 + m; switch (Center) {//计算在tile中指定位置带来的偏移 case TileUtils.LocationOnTile.UpperLeft: //1 v -= m, A += m, v += c * g; //似乎是向内缩进 break; case TileUtils.LocationOnTile.UpperRight: v += m, A += m, A -= c * g; break; case TileUtils.LocationOnTile.LowerRight: v += m, A -= m, v -= c * g; break; case TileUtils.LocationOnTile.LowerLeft: v -= m, A -= m, A += c * g; break; case TileUtils.LocationOnTile.Center: //0 } switch (cubeFace) { case GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_POSITIVE_X: MathLight.setVector(dir, -1, A, -v); break; case GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_NEGATIVE_X: MathLight.setVector(dir, 1, A, v); break; case GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_POSITIVE_Y: //顶面 MathLight.setVector(dir, -v, 1, -A); break; case GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: MathLight.setVector(dir, -v, -1, A); break; case GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_POSITIVE_Z: MathLight.setVector(dir, -v, A, 1); break; case GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: MathLight.setVector(dir, v, A, -1); } MathLight.normalize(dir); } }(), /* * 获取该tile在第几个面(简易装载法) */ TileUtils.getFaceForTile = function(size, index) {//获取该tile在第几个面 var tileSize = TileUtils.TILE_SIZE; size < TileUtils.TILE_SIZE && (tileSize = size); var n = Math.floor(size / tileSize) , sum = n * n; //得每个面tile总数 return Math.floor(index / sum) } , TileUtils.getTileLocation = function(size, t, result) { var tileSize = TileUtils.TILE_SIZE; size < TileUtils.TILE_SIZE && (tileSize = size); var r = TileUtils.getFaceForTile(size, t) , a = Math.floor(size / tileSize) , s = a * a , l = t - r * s; result.tileX = l % a; result.tileY = Math.floor(l / a); result.face = r; result.faceTileIndex = l; return result } , /* * 求size分辨率需要多少张tile */ TileUtils.getTileCountForSize = function(e) { if (e <= TileUtils.TILE_SIZE) return TileUtils.FACES_PER_PANO; var t = Math.floor(e / TileUtils.TILE_SIZE) , i = t * t , n = i * TileUtils.FACES_PER_PANO; return n } , TileUtils.getRelativeDirection = function() { var e = new MathLight.Matrix4 , t = new MathLight.Quaternion; return function(i, n) {//i是pano.quaternion, n是camera的direction t.copy(i), t.inverse(), e.makeRotationFromQuaternion(t), e.applyToVector3(n), MathLight.normalize(n); } }(), /* * 根据方向寻找合适的tile加载 */ TileUtils.matchingTilesInDirection = function() { var e = new MathLight.Vector3 , t = new MathLight.Vector3(0,0,-1) , i = new MathLight.Quaternion , n = function(e, t) { e.push({ face: t.face, faceTileIndex: t.faceTileIndex, tileX: t.tileX, tileY: t.tileY }); } , a = function() { var e = { face: -1, faceTileIndex: -1, tileX: -1, tileY: -1 }; return function(size, i, r) { for (var a = TileUtils.getTileCountForSize(size), s = 0, l = 0; l < a; l++) TileUtils.getTileLocation(size, l, e), i && !i(e) || (s++, r && n(r, e)); return s } }(); return function(pano, size, dir, hFov, vFov, result) { var d = size < TileUtils.TILE_SIZE ? size : TileUtils.TILE_SIZE; //TileUtils.getTileCountForSize(size); if (!hFov && !vFov) return a(size, null, result); var p = !!vFov; vFov = vFov || hFov, vFov = Math.max(0, Math.min(vFov, 360)), hFov = Math.max(0, Math.min(hFov, 360)), MathLight.copyVector(dir, e), TileUtils.getRelativeDirection(pano.quaternion4dkk, e); if(p){//如果有vFov hFov i.setFromUnitVectors(e, t); var f = function(e) { return TileUtils.isTileWithinFrustum(size, d, e.face, e.tileX, e.tileY, i, hFov, vFov)//在视野中的 }; return a(size, f, result) } var g = function(t) {//如果仅有hFov return TileUtils.isTileWithinFOV(size, d, t.face, t.tileX, t.tileY, e, hFov) }; return a(size, g, result) } }(), /* * 是否在屏幕范围内 */ TileUtils.isTileWithinFrustum = function() { var e = new MathLight.Vector3 , t = 1e-5; return function(i, n, a, s, l, c, h, u) { for (var d = Math.tan(.5 * u * MathLight.RADIANS_PER_DEGREE), p = -d, f = Math.tan(.5 * h * MathLight.RADIANS_PER_DEGREE), g = -f, m = TileUtils.mapFaceToCubemapFace(a), v = 0, A = 0, y = 0, C = 0, I = 0, E = 0, b = TileUtils.LocationOnTile.Center; b <= TileUtils.LocationOnTile.LowerLeft; b++){ TileUtils.getTileVector(i, n, m, s, l, b, 0, e),//get e // size, tileSize, cubeFace, tileX, tileY, Center, c, dir MathLight.applyQuaternionToVector(c, e); if (e.z >= -t)//似乎是在相机背面 I++; else { var w = -1 / e.z , _ = e.x * w , T = e.y * w; T > d ? v++ : T < p && A++, //这四种似乎代表在这个画框之外,如在左、在上、在下、在右 _ > f ? y++ : _ < g && C++, E++; } } return A !== E && v !== E && y !== E && C !== E //如果有一项和E相等代表要么是在相机背面要么是tile的四个顶点都画在画布的同一边,所以肯定不在画布上 } }(), /* * 是否在FOV范围内 */ TileUtils.isTileWithinFOV = function() { var e = new MathLight.Vector3 , t = new MathLight.Vector3(0,1,0) , i = new MathLight.Vector3(1,0,0); return function(panoSize, tileSize, face, tileX, tileY, direction, fov) {//direction是作用了pano.quaternion的camera.direction var d = TileUtils.mapFaceToCubemapFace(face); MathLight.cross(direction, t, i); //get i 好像没用到 TileUtils.getTileVector(panoSize, tileSize, d, tileX, tileY, TileUtils.LocationOnTile.Center, 0, e); if (TileUtils.isWithinFOV(e, direction, fov, null))//先判断tile中心在不在FOV内 return !0; for (var p = fov / 360, f = Math.floor(1 / p), g = 0, m = 0; m < f; m++) { for (var v = TileUtils.LocationOnTile.UpperLeft; v <= TileUtils.LocationOnTile.LowerLeft; v++) if (TileUtils.getTileVector(panoSize, tileSize, d, tileX, tileY, v, g, e), TileUtils.isWithinFOV(e, direction, fov, null)) return !0; g += p; //可能是考虑到有可能tile比fov覆盖了fov(虽然一般不可能,除非fov特别小),所以将tile分成若干段,取tile中的点再检测下 } return !1 } }(), TileUtils.isWithinFOV = function() { var e = new MathLight.Vector3 , t = new MathLight.Vector3; return function(dir, cameraDir, fov, a) { if (MathLight.copyVector(dir, t), a) { MathLight.copyVector(a, e), MathLight.normalize(e); var s = MathLight.dot(e, dir); e.x *= s, e.y *= s, e.z *= s, MathLight.subVector(t, e); } var l = fov / 2 * MathLight.RADIANS_PER_DEGREE , c = Math.cos(l) , h = MathLight.dot(t, cameraDir); return h >= c } }(), TileUtils.mapFaceToCubemapFace = function() { var e = { 0: GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 1: GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 2: GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 3: GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 4: GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 5: GLCubeFaces$1.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y }; return function(t) { return e[t] } }(); let { PanoRendererEvents, PanoramaEvents, PanoSizeClass} = Potree.defines; var texLoader$3 = new TextureLoader(); const markerOpacitys ={ default : 0.4, hovered : 1, }; const labelProp = { sizeInfo: {minSize : 200 , maxSize : 250, nearBound : 0.8, farBound : 10}, backgroundColor:{r: 255, g: 255, b: 255, a: 0.4 }, textColor:{r: 0, g: 0, b: 0, a: 1 }, borderRadius: 15, renderOrder:10, useDepth:true, clipDistance: 30, maxClipFactor:0.3, occlusionDistance:3, }; const labelProp2 = { //sizeInfo: {minSize : 200 , maxSize : 250, nearBound : 0.8, farBound : 10}, backgroundColor:{r: 255, g: 255, b: 255, a: 0 }, textColor:{r:255 , g: 255, b: 255, a: 1 }, textBorderColor:{r:30 , g:30, b: 30, a: 1 }, textBorderThick:3, dontFixOrient:true, renderOrder:10, fontsize:30, }; let markerTex; //显示全景图时marker没有被遮挡,如果需要,要换成depthBasicMaterial 或者直接把skybox的深度修改(拿到深度贴图后更如此) let planeGeo = new PlaneBufferGeometry(0.2,0.2); let sg = new SphereGeometry(0.1, 8, 8); let smHovered = new MeshBasicMaterial({/* side: THREE.BackSide, */color: 0xff0000}); let sm = new MeshBasicMaterial({/* side: THREE.BackSide */}); var rot90 = new Quaternion().setFromAxisAngle(new Vector3(0,0,1), Math.PI/2 ); //使用的是刚好适合全景图的,给cube贴图需要转90° //var rot90 = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(1,0,0), -Math.PI/2 ); //4dkk->navvis //var rot901 = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0,1,0), -Math.PI/2 ); //整张球幕图要旋转下 //rot90 = new THREE.Quaternion().multiplyQuaternions( rot901, rot90) var old = null; /* 转成四维看看的axis: var a = new THREE.Quaternion().setFromAxisAngle(new THREE.Vector3(0,0,1), THREE.Math.degToRad(-90)) 因为四维的要绕y转90 这里的quaternion.multiply(a); 先乘再换顺序 w : q.w, x:q.x , y:-q.z, z:q.y */ //暂时直接用4dkkconsole输出的数据 class Panorama extends EventDispatcher{ constructor(o, images360){//file, time, longitude, latitude, altitude, course, pitch, roll super(); this.id = o.id; //唯一标识 this.images360 = images360; this.visible = true; //for updateVisible this.enabled = true;//是否可以走 this.addEventListener('isVisible',(e)=>{//是否显示该点的mesh(不显示也能走) //console.log('pano isVisible', this.id, e.visible) Potree.Utils.updateVisible(this.marker, 'panoVisi', e.visible); Potree.settings.showPanoMesh && (this.mesh.visible = e.visible); if(e.reason == 'screenshot' || e.visible){ this.label && (this.label.visible = e.visible);//截图时隐藏下 } this.label2 && Potree.Utils.updateVisible(this.label2, 'panoVisi', e.visible); }); /* 漫游点可见性:旧 level reason 类型 2(最高)buildingChange(不在此楼层) unvisible 1 modeIsShowPanos(漫游模式) visible //不记得为什么加这个了,所以重写 0 pointcloudVisi(隐藏了数据集) unvisible */ /* 漫游点可见性:新 level reason 类型 2(最高)buildingChange(不在此楼层) unvisible 1 ifShowMarker(marker显示开关) unvisible 0 pointcloudVisi(隐藏了数据集) unvisible */ if(Potree.settings.editType == 'pano'){//漫游点拼合编辑 this.uuid = o.uuid; //因为有多个数据集 所以会重复 this.index = this.originID = o.index; //下标, 用于visibles this.pointcloud = viewer.scene.pointclouds.find(e=>e.panoUuid == o.uuid); this.pointcloud.panos.push(this); this.sid = this.pointcloud.dataset_id + '|' + this.uuid; //不会更改的标记 用于entity.panos里的标记 this.panosData = o; //数据中原本的位置朝向 this.dataPosition = new Vector3().copy(o.pose.translation); this.dataQuaternion = new Quaternion().copy(o.pose.rotation); this.dataRotation = new Euler().setFromQuaternion(this.dataQuaternion); //因为位置朝向随着点云位置改变,所以直接运用到点云上,这里清零 this.originPosition = new Vector3(); //{x: 0, y: 0, z: 0} this.quaternion = new Quaternion(); //{w: 0, x: 0, y: 0, z: 1} //this.quaternion4dkk = math.convertVisionQuaternion(this.quaternion)//4dkk内使用的quaternion this.visibles = o.visibles; this.rtkState = o.has_rtk ? o.use_rtk : null; const height = 1.4; //相机高度 this.originFloorPosition = this.originPosition.clone(); this.originFloorPosition.z -= height; /* this.originPosition = new THREE.Vector3().copy(o.pose.translation) //{x: 0, y: 0, z: 0} this.quaternion = new THREE.Quaternion().copy(o.pose.rotation) //{w: 0, x: 0, y: 0, z: 1} //this.quaternion4dkk = math.convertVisionQuaternion(this.quaternion)//4dkk内使用的quaternion this.visibles = o.visibles this.pointcloud = viewer.scene.pointclouds.find(e=>e.dataset_id == o.uuid) this.pointcloud.panos.push(this) const height = 1.5; //相机高度 this.originFloorPosition = this.originPosition.clone() this.originFloorPosition.z -= height */ }else { this.originPosition = new Vector3().fromArray(o.dataset_location); //完全对应vision.txt的translation this.originFloorPosition = new Vector3().fromArray(o.dataset_floor_location); this.originID = parseInt(o.file_id);//"file_id":"00022"对应是原本的4dkk的id --来自vision.txt this.pointcloud = viewer.scene.pointclouds.find(e=>e.dataset_id == o.dataset_id) || viewer.scene.pointclouds[0]; this.pointcloud.panos.push(this); //this.sid = this.pointcloud.sceneCode + '|' + this.originID //不会更改的标记 this.sid = this.pointcloud.dataset_id + '|' + this.originID; //不会更改的标记 //全景图和Cube的水平采样起始坐标相差90度 /* if(from4dkk){ var qua = o.dataset_orientation var quaternion = new THREE.Quaternion().fromArray(qua) quaternion = new THREE.Quaternion().multiplyQuaternions(quaternion, rot901);//整张球幕图要旋转下 因为在4dkk里转过,还原。如果是tiles的不用 this.quaternion = new THREE.Quaternion(quaternion.x, -quaternion.z, quaternion.y, quaternion.w) //转化坐标 }else{ */ var qua = o.dataset_orientation; //完全对应vision.txt的rotation qua = [qua[1], qua[2], qua[3], qua[0]]; this.quaternion = new Quaternion().fromArray(qua); this.quaternion4dkk = math.convertVisionQuaternion(this.quaternion);//4dkk内使用的quaternion this.quaternion2 = this.quaternion.clone(); this.quaternion = new Quaternion().multiplyQuaternions(this.quaternion, rot90);//全景图和Cube的水平采样起始坐标相差90度,cubeTex转90度 this.rotation4dkk = new Euler().setFromQuaternion(this.quaternion4dkk); //} //this.quaternion1 = Potree.Utils.QuaternionFactory.fromArray(o.dataset_orientation) //同quaternion //let xy = this.transform.forward([this.longitude, this.latitude]); //this.file = `https://4dkk.4dage.com/images/images${Potree.settings.number}/pan/high/${this.id}.jpg` this.neighbours = []; } this.rotation = new Euler().setFromQuaternion(this.quaternion); this.build(); this.transformByPointcloud(); //初始化位移 {//tile this.minimumTiledPanoLoaded = !1; this.highestPartialTileRenderOpCompleted = 0; this.highestFullTileRenderOpCompleted = 0; this.shouldRedrawOnBaseLoaded = !1; this.resolutionPromise = {}; this.tiledPanoRenderTarget = null; this.zoomed = !1; images360.panoRenderer.addEventListener(PanoRendererEvents.TileRenderSuccess, this.onTileRendered.bind(this)); images360.panoRenderer.addEventListener(PanoRendererEvents.PanoRenderComplete, this.onPanoRendered.bind(this)); images360.panoRenderer.addEventListener(PanoRendererEvents.TileRenderFailure, this.onTileRenderFail.bind(this)); images360.panoRenderer.addEventListener(PanoRendererEvents.UploadAttemptedForAllTiles, this.onUploadAttemptedForAllTiles.bind(this)); } this.addEventListener('hoverOn', (e)=>{//from Map if(!e.byMainView){ this.hoverOn(e); } }); this.addEventListener('hoverOff', (e)=>{ if(!e.byMainView){ this.hoverOff(e); } }); } get noNeighbour(){//是否绝对到不到的孤立点 for(let i=0,j=this.images360.panos.length; i{ this.depthTex = texture; this.dispatchEvent({type:'loadedDepthImg', loaded:true}); this.images360.dispatchEvent({type:'loadedDepthImg', pano:this}); this.depthTexLoading = false; this.images360.updateDepthTex(this); },null,(e)=>{//error console.error('loadDepthImg失败, 数据集sceneCode'+ this.pointcloud.sceneCode, this.id ); this.pointcloud.hasDepthTex = false; this.dispatchEvent({type:'loadedDepthImg' }); }); texture.wrapS = RepeatWrapping; texture.flipY = false; texture.magFilter = LinearFilter; texture.minFilter = LinearFilter; texture.generateMipmaps = false; } build(){ { // orientation //add //var quaternion = new THREE.Quaternion().multiplyQuaternions(this.quaternion, rot901);//改 为球目全 //quaternion.premultiply(rot90) this.panoMatrix = new Matrix4().makeRotationFromQuaternion(this.quaternion); this.oriPanoMatrix = this.panoMatrix.clone(); if(this.quaternion2)this.oriPanoMatrix2 = new Matrix4().makeRotationFromQuaternion(this.quaternion2); //console.log(this.quaternion) //this.quaternion = quaternion } /* let marker = new THREE.Mesh(planeGeo, this.getMarkerMat() ) //marker.lookAt(marker.up) marker.scale.set(2,2,2) */ let marker = new Mesh(planeGeo, this.getMarkerMat() ); //new Sprite({mat:this.getMarkerMat(), dontFixOrient:true }) marker.scale.set(2,2,2);//marker.scale.set(0.4,0.4,0.4) marker.name = 'marker_'+this.id; marker.up.set(0,0,1); this.addEventListener('changeMarkerTex',(e)=>{ marker.material.map = markerTex[e.name]; }); this.marker = marker; marker.pano = this; this.images360.node.add(marker); Potree.settings.isTest && this.addLabel(); //this.addLabel2() marker.addEventListener('mouseover', this.hoverOn.bind(this)); marker.addEventListener('mouseleave', this.hoverOff.bind(this)); } transformByPointcloud(){ let position = this.originPosition.clone().applyMatrix4(this.pointcloud.transformMatrix);//也可以用datasetPosTransform算 let floorPosition = this.originFloorPosition.clone().applyMatrix4(this.pointcloud.transformMatrix); this.setPosition(position, floorPosition); this.panoMatrix = new Matrix4().multiplyMatrices(this.pointcloud.rotateMatrix, this.oriPanoMatrix ); //this.panoMatrix2 = Potree.Utils.datasetRotTransform({fromDataset:true, pointcloud:this.pointcloud, matrix:this.oriPanoMatrix, getMatrix:true}) //和上一行结果一样 //quaternion也变下 if(this.oriPanoMatrix2){ this.panoMatrix2 = new Matrix4().multiplyMatrices(this.pointcloud.rotateMatrix, this.oriPanoMatrix2 );//供DepthImageSampler使用 this.panoMatrix2Inverse = this.panoMatrix2.clone().invert(); } this.dispatchEvent('rePos'); } setPosition(position, floorPosition){ this.position = position; this.floorPosition = floorPosition; //this.mesh.position.copy(this.position) this.marker.position.copy(this.floorPosition); this.marker.position.z+=0.04;//会被点云遮住 if(this.label){ if(Potree.settings.editType == 'pano'){ this.label.position.copy(this.position); }else { this.label.position.copy(this.floorPosition); } this.label.position.z+=0.14; this.label.update(); } /* if(this.label2){ if(Potree.settings.editType == 'pano'){ this.label2.position.copy(this.position) }else{ this.label2.position.copy(this.floorPosition) } this.label2.position.copy(this.marker.position) this.label2.update() } */ } /* getRealPos(){//当整体移动以后 return this.position.clone().applyMatrix4(viewer.scene.scene.matrix) } */ getMarkerMat(){ if(!markerTex) { markerTex = { default:texLoader$3.load( Potree.resourcePath+'/textures/marker.png' ), ring:texLoader$3.load( Potree.resourcePath+'/textures/marker2.png' ) }; markerTex.default.anisotropy = 4; // 各向异性过滤 .防止倾斜模糊 markerTex.ring.anisotropy = 4; //有可能被点云遮住吗。 } return new DepthBasicMaterial({opacity: markerOpacitys.default, side: DoubleSide , map:markerTex.default ,transparent:true, clipDistance: 2, occlusionDistance:1, //不能设置太短,因为过渡时深度不准确 useDepth: !!(Potree.settings.useDepthTex && this.pointcloud.hasDepthTex), autoDepthTest:true //改为DepthBasicMaterial是因为原Basic的材质在有深度图时过渡会先隐藏后出现。 注:没有深度图时全景模式的marker无法遮挡 }) } hoverOn(e={}) { //console.log("hoverOn " + this.id ) transitions.start(lerp.property(this.marker.material, "opacity", markerOpacitys.hovered,()=>{ viewer.dispatchEvent('content_changed'); }), this.marker.visible ? 250 : 0); if(!e.byMap) this.dispatchEvent({type:'hoverOn', byMainView:true}); if(!e.byImages360) this.images360.dispatchEvent({type:'markerHover', hovered:true, pano:this}); } hoverOff(e={}){ //console.log("hoverOff " + this.id ) transitions.start(lerp.property(this.marker.material, "opacity", markerOpacitys.default,()=>{ viewer.dispatchEvent('content_changed'); }), this.marker.visible ? 250 : 0); if(!e.byMap) this.dispatchEvent({type:'hoverOff', byMainView:true}); if(!e.byImages360) this.images360.dispatchEvent({type:'markerHover', hovered:false, pano:this}); } setZoomed(zoomed){ this.zoomed = zoomed; Potree.settings.displayMode == 'showPanos' && this.updateSkyboxForZoomLevel(); //放大后换成zoomTarget贴图 viewer.dispatchEvent({type:'panoSetZoom', zoomed}); } enter(){ this.entered = true; this.setZoomed(!1); viewer.dispatchEvent({type:PanoramaEvents.Enter, oldPano:old, newPano:this } ); old = this; //console.log("enter pano "+ this.id) } exit(){ this.clearWaitDeferreds(); this.minimumTiledPanoLoaded = !1; this.tiledPanoRenderTarget = null; this.setZoomed(!1); this.images360.panoRenderer.deactivateTiledPano(this); this.highestPartialTileRenderOpCompleted = 0; this.highestFullTileRenderOpCompleted = 0; this.depthTex && this.depthTex.dispose(); //贴图不使用后先dispose,下次到该点时会自动还原 this.entered = false; //add //console.log("exit pano "+ this.id) viewer.dispatchEvent({type:PanoramaEvents.Exit, pano:this}); } updateSkyboxForZoomLevel(){ if(this.minimumTiledPanoLoaded){ this.images360.updateProjectedPanos(); } } getSkyboxTexture(){ if(this.minimumTiledPanoLoaded) { if(this.zoomed && this.images360.qualityManager.maxRenderTargetSize > this.images360.qualityManager.maxNavPanoSize)//change 如果放大后和不放大都是2k就不用这个 { return this.images360.panoRenderer.zoomRenderTarget.texture; } else { this.tiledPanoRenderTarget.texture.mapping = UVMapping;//add return this.tiledPanoRenderTarget.texture; } } else { return null; } } isLoaded(e){ if (e && "string" == typeof e) console.error("Wrong panoSize given to Panorama.isLoaded(); a tiled pano uses PanoSizeClass"); return !!this.minimumTiledPanoLoaded && (!e || this.highestFullTileRenderOpCompleted >= e)//改:原本是:this.highestPartialTileRenderOpCompleted >= e, 希望这代表全部加载完 } getWaitDeferred(size){//获取不同size的tile贴图的promiss var t = this.resolutionPromise[this.id]; t || (t = {}, this.resolutionPromise[this.id] = t); var i = t[size]; return i || (i = { deferred: $.Deferred(), active: !1 }, t[size] = i), i } clearWaitDeferreds(){ var e = this.resolutionPromise[this.id]; e || (e = {}, this.resolutionPromise[this.id] = e); for (var t in e) if (e.hasOwnProperty(t)) { var i = e[t]; i.active = !1, i.deferred = $.Deferred(); } } resetWaitDeferred(e){ var t = this.getWaitDeferred(e); t.active = !1; t.deferred = $.Deferred(); } onTileRendered(ev){ ev.id === this.id && this.dispatchEvent({ type:PanoramaEvents.TileLoaded, size:ev.panoSize, index:ev.tileIndex, count:ev.totalTiles }); } onPanoRendered(ev) { if(ev.id === this.id) { this.minimumTiledPanoLoaded = !0; this.updateSkyboxForZoomLevel();//更新贴图 setProjected ev.panoSize > this.highestPartialTileRenderOpCompleted && (this.highestPartialTileRenderOpCompleted = ev.panoSize);//应该是更新最高获取到的Partial size ev.updateFullComplete && ev.panoSize > this.highestFullTileRenderOpCompleted && (this.highestFullTileRenderOpCompleted = ev.panoSize); //应该是更新最高获取到的Full size //this.dispatchEvent("load", ev.panoSize); viewer.ifAllLoaded( this); this.dispatchEvent({type:PanoramaEvents.LoadComplete, size:ev.panoSize, count:ev.totalTiles}); } } onTileRenderFail(ev) { ev.id === this.id && this.dispatchEvent({type:PanoramaEvents.LoadFailed }); } onUploadAttemptedForAllTiles(ev) { if (ev.id === this.id) { var n = this.images360.qualityManager.getPanoSize(PanoSizeClass.BASE); if(ev.panoSize === n && this.shouldRedrawOnBaseLoaded) //shouldRedrawOnBaseLoaded一直是false。在4dkk里只有初始点在quickstart后变为true。 { this.shouldRedrawOnBaseLoaded = !1; this.panoRenderer.resetRenderStatus(this.id, !0, !1); this.panoRenderer.renderPanoTiles(this.id, null, !0, !0); } } } addLabel(){ this.removeTextLabel(); this.label = new TextSprite$2(Object.assign({}, labelProp, {text: this.id + "("+this.originID+")"}) //{text: `id:${this.id}, dataset:${this.pointcloud.name}, 4dkkId:${this.originID}`} ); this.images360.node.add(this.label); this.floorPosition && this.label.position.copy(this.floorPosition); } addLabel2(){ if(this.label2)return this.label2 = new TextSprite$2(Object.assign({}, labelProp2, {text: /* this.originID */ parseInt(this.id)+1 }) //{text: `id:${this.id}, dataset:${this.pointcloud.name}, 4dkkId:${this.originID}`} ); //this.images360.node.add(this.label2); this.marker.add(this.label2); //this.floorPosition && this.label2.position.copy(this.floorPosition) let s = 0.2; this.label2.scale.set(s,s,s); Potree.Utils.updateVisible(this.label2, 'notDisplay', false); Potree.Utils.updateVisible(this.label2, 'panoVisi', this.visible); //Potree.Utils.setObjectLayers(this.label2, 'bothMapAndScene') } removeTextLabel(){ if(this.label){ this.label.parent.remove(this.label); } } dispose(){ let i = viewer.images360.panos.indexOf(this); if(i==-1)return this.marker.parent.remove(this.marker); this.removeTextLabel(); if(this.depthTex) this.depthTex.dispose(); viewer.images360.panos.splice(i,1); this.dispatchEvent('dispose'); //删除tile贴图、depthTex等以后再写 } getCeilHeight(){//天花板高度值 (假设不存在depth为0的点,所有为0的要么是在盲区,要么是无穷远。) if(this.ceilZ == void 0){ //const depthTiming = Potree.timeCollect.depthSampler.median //pc firefox达到4. chrome为0.01 //用三个间隔120度散开,和中心垂直线成一定夹角的三个向量去求 最高高度 (不求平均的原因:万一是0不好算) let rotMat = new Matrix4().makeRotationX((Potree.config.depthTexUVyLimit+0.01)*Math.PI);// 角度不能小于天花板中空的半径 let dir0 = new Vector3(0,0,1).applyMatrix4(rotMat); let dirs = [ dir0, dir0.clone().applyMatrix4(new Matrix4().makeRotationZ(Math.PI*2 / 3)), dir0.clone().applyMatrix4(new Matrix4().makeRotationZ(-Math.PI*2 / 3)) ]; /* if(depthTiming < 1){ let rotMat1 = new THREE.Matrix4().makeRotationZ(Math.PI*2 / 3); dirs.push(dirs[0].clone().applyMatrix4(rotMat1)) } if(depthTiming < 0.3){ let rotMat2 = new THREE.Matrix4().makeRotationZ(-Math.PI*2 / 3); dirs.push(dirs[0].clone().applyMatrix4(rotMat2)); } */ let zs = dirs.map(dir_=>{ let dir = dir_.clone().applyMatrix4(this.panoMatrix2); //pano不一定是垂直的, 需要把之前的dirInPano先转成真实的dir,防止超出角度限制 let intersect = viewer.images360.getIntersect(this, dir); let z = intersect ? intersect.location.z : Infinity;/* this.position.z+skyHeight */ //没有intersect代表可能是天空 return z }); zs.sort((a,b)=>{return b-a});//得最大值 (不用中位数的原因:在屋檐处,如果仅有一个intersect是天空,因到了室外所以也用天空高度) this.ceilZ = zs[0]; let min = this.position.z + 1; // 防止意外太低 this.ceilZ = Math.max(min, this.ceilZ); //console.log(this.id, 'ceilZ:', this.ceilZ ) } return this.ceilZ } }; Panorama.prototype.loadTiledPano = function() { //var downloads = [] , t = []; var downloaded = {} , eventAdded = {}, latestPartialRequest = {}; //每个pano对应一组这些 return function(size, dirs, fov, o, a, download) { var dir = dirs.datasetsLocal.find(e=>e.datasetId == this.pointcloud.dataset_id).direction; //var dir = dirs null !== o && void 0 !== o || (o = !0), null !== a && void 0 !== a || (a = !0); var l = this.getWaitDeferred(size) , c = l.deferred , h = null , u = null; fov && ("number" == typeof fov ? h = fov : (h = fov.hFov, u = fov.vFov)); if (!this.isLoaded(size)) { //console.log('loadTiledPano', this.id, size, fov) if (!l.active) { l.active = !0; let name = this.id + ":" + size; downloaded[name] = downloaded[name] || []; /* this.downloaded = downloaded this.latestPartialRequest = latestPartialRequest */ latestPartialRequest[name] = null; if (fov) { let tileArr = [];//add var d = TileUtils.matchingTilesInDirection(this, size, dir, h, u, tileArr); latestPartialRequest[name] = tileArr; downloaded[name].forEach((e)=>{ let item = latestPartialRequest[name].find(a=>e.faceTileIndex == a.faceTileIndex && e.face == a.face); if(item){ item.loaded = true; } }); if(!latestPartialRequest[name].some(e=>!e.loaded)){//所需要的全部加载成功 //let total = TileUtils.getTileCountForSize(size) //this.onPanoRendered(this.id, size, total, !0); c.resolve(size/* , total */); this.resetWaitDeferred(size); //console.log('该部分早已经加载好了'+size, this.id) latestPartialRequest[name] = null; } //console.log("Loading partial pano: " + this.id + " with " + d + " tiles") } if(!eventAdded[this.id]) { eventAdded[this.id] = !0; this.addEventListener(PanoramaEvents.LoadComplete, function(ev/* e, t */) {//本次任务全部加载完毕 //console.warn('点位(可能部分)下载完成 ', 'id:'+this.id, 'size:'+ev.size ) var i = this.getWaitDeferred(ev.size).deferred;//"pending"为还未完成 i && "pending" === i.state() && this.highestPartialTileRenderOpCompleted >= ev.size && (i.resolve(ev.size, ev.count), this.resetWaitDeferred(ev.size));//恢复active为false }.bind(this)); this.addEventListener(PanoramaEvents.LoadFailed, function(ev) { var t = this.getWaitDeferred(e).deferred; t && "pending" === t.state() && this.highestPartialTileRenderOpCompleted >= ev.t && (t.reject(ev.t), this.resetWaitDeferred(ev.t));//恢复active为false }.bind(this)); this.addEventListener(PanoramaEvents.TileLoaded, function(ev/* t, i, n */) {//每张加载完时 //console.log('tileLoaded', 'id:'+this.id, 'size:'+ev.size, 'tileIndex:'+ev.index ) let tileIndex = ev.index; let total = ev.count; let size = ev.size; let name = this.id + ":" + size; downloaded[name] = downloaded[name] || []; //不是所有的加载都是从loadTiledPano获取的所以会有未定义的情况 let {faceTileIndex,face} = TileUtils.getTileLocation(size, tileIndex, {}); downloaded[name].push({faceTileIndex,face}); var r = this.getWaitDeferred(size).deferred; if (r && "pending" === r.state()) { r.notify(size, tileIndex, total); if(latestPartialRequest[name]){ let item = latestPartialRequest[name].find(e=>e.faceTileIndex == faceTileIndex && e.face == face); item && (item.loaded = true ); if(!latestPartialRequest[name].some(e=>!e.loaded)){//所需要的局部tiles全部加载成功 this.onPanoRendered(this.id, size, total, !0); //onPanoRendered还会触发 PanoramaEvents.LoadComplete r.resolve(size, total); this.resetWaitDeferred(size); //console.log('该部分加载好了'+size, this.id) latestPartialRequest[name] = null; } } } viewer.dispatchEvent('content_changed'); /* var r = this.getWaitDeferred(ev.size).deferred; if (r && "pending" === r.state()) { r.notify(ev.size, ev.index, ev.count); var o = downloads[this.id + ":" + ev.size]; if(o){//如果有规定下载哪些tile,只需要下载这些tile则LoadComplete o.tileCount++ if(o.tileCount === o.targetTileCount){//达到下载目标数 this.onPanoRendered(this.id, ev.size, ev.count, !0); r.resolve(ev.size, ev.count); this.resetWaitDeferred(ev.size) } } } */ }.bind(this)); } } this.images360.tileDownloader.clearForceQueue(); this.images360.tileDownloader.forceQueueTilesForPano(this, size, dir, h, u, download); this.tiledPanoRenderTarget = this.images360.panoRenderer.activateTiledPano(this, this.images360.qualityManager.getMaxNavPanoSize(), o); this.images360.panoRenderer.renderPanoTiles(this.id, dirs, a); }else { //console.log('早已经全加载好了' +size, this.id) c.resolve(size); } return c.promise() } }(); let {ModelManagerEvents,PanoSizeClass: PanoSizeClass$1} = Potree.defines; class QualityManager { constructor(e, t, i) { this.maxNavPanoSize = -1; this.maxZoomPanoSize = -1; this.devicePixelDensity = e; this.deviceScreenSize = t; this.clientBandwidth = i; this.panoSizeClassMap = {}; this.useHighResolutionPanos = !0; //是否能够使用2k及以上图 this.useUltraHighResolutionPanos = !1; this.modelHasUltraHighPanos = !1; this.qualityManager = this; this.maxRenderTargetSize = browser.isMobile() ? 2048 : 4096; //add this.init(); } init(e ) { //var metadata = store.getters['scene/metadata'] ;//有时候请求不到 //if(metadata.sceneSource == 11 || metadata.sceneScheme == 12){ /* if(config.tileClass == '1k'){ this.useHighResolutionPanos = false //xzw add 只加载1k } */ this.buildPanoSizeClassMap(this.devicePixelDensity, this.deviceScreenSize, this.clientBandwidth); this.ultraHighSize = this.getPanoSize(PanoSizeClass$1.ULTRAHIGH); this.highSize = this.getPanoSize(PanoSizeClass$1.HIGH); this.standardSize = this.getPanoSize(PanoSizeClass$1.STANDARD); this.baseSize = this.getPanoSize(PanoSizeClass$1.BASE); config$1.tiling.maxZoomPanoQuality && this.ultraHighSize <= config$1.tiling.maxZoomPanoQuality && (config$1.tiling.allowUltraHighResolution = !0); this.highQualityThreshold = browser.valueFromHash("threshold2k", config$1.windowHeightHighQualityThreshold); this.updateMaximums(); //e.on(ModelManagerEvents.ActiveModelChanged, this.onModelChanged.bind(this)); } updateFromModel(e) { //this.updateHighResolutionSettings(e) this.updateUltraHighResolutionSettings(e); } /* updateHighResolutionSettings(e) { this.useHighResolutionPanos = !0 this.updateMaximums() } */ updateUltraHighResolutionSettings(e) { if (config$1.tiling.allowUltraHighResolution && this.modelHasUltraHighPanos) { this.useUltraHighResolutionPanos = !0; } else { this.useUltraHighResolutionPanos = !1; } this.updateMaximums(); } enableUltraHighQualityMode() { this.modelHasUltraHighPanos = !0; this.updateUltraHighResolutionSettings(null); } ultraHighQualityModeEnabled() { return this.modelHasUltraHighPanos } onModelChanged(e) { this.updateFromModel(e.model), this.updateMaximums(); } updateMaximums() { this.maxNavPanoSize = config$1.tiling.maxNavPanoQuality || this.detectMaxNavPanoSize(), this.maxZoomPanoSize = config$1.tiling.maxZoomPanoQuality || this.detectMaxZoomPanoSize(), this.maxZoomPanoSize < this.maxNavPanoSize && (this.maxNavPanoSize = this.maxZoomPanoSize); } buildPanoSizeClassMap() { this.panoSizeClassMap[PanoSizeClass$1.BASE] = 512, this.panoSizeClassMap[PanoSizeClass$1.STANDARD] = 1024, this.panoSizeClassMap[PanoSizeClass$1.HIGH] = 2048, this.panoSizeClassMap[PanoSizeClass$1.ULTRAHIGH] = 4096; } getPanoSize(e) { return this.panoSizeClassMap[e] } getMaxPossiblePanoSize() { return this.getPanoSize(PanoSizeClass$1.ULTRAHIGH) } getMaxPanoSize() { return this.maxZoomPanoSize } getMaxNavPanoSize() { return this.maxNavPanoSize } getMaxZoomPanoSize() { return this.maxZoomPanoSize } detectMaxNavPanoSizeClass() { //return this.useHighResolutionPanos ? browser.isMobile() ? PanoSizeClass.STANDARD : window.innerHeight < this.highQualityThreshold ? PanoSizeClass.STANDARD : PanoSizeClass.HIGH : PanoSizeClass.STANDARD /* if(config.name == 'decor'){ return PanoSizeClass.STANDARD } return PanoSizeClass.HIGH */ switch(Potree.settings.navTileClass){ case '1k': return PanoSizeClass$1.STANDARD; break; case '2k': default: return PanoSizeClass$1.HIGH; } } detectMaxNavPanoSize() { var e = this.detectMaxNavPanoSizeClass(); return this.getPanoSize(e) } detectMaxZoomPanoSize() { //获取当前zoomRenderTarget应下载的最高级别 //若是有三个级别,每次只需要加载到当前的zoomLevel;而两级时因为有zoomed来判断是使用基本贴图还是zoomRenderTarget,所以只需要返回最大的即可 if(this.zoomLevelResolution){//有三个级别 if(this.zoomLevelResolution == '4k' && this.useUltraHighResolutionPanos){ return this.getPanoSize(PanoSizeClass$1.ULTRAHIGH); }else if(this.zoomLevelResolution == '1k' || !this.useHighResolutionPanos){ return this.getPanoSize(PanoSizeClass$1.STANDARD); }else { return this.getPanoSize(PanoSizeClass$1.HIGH); } }else { if (this.useHighResolutionPanos) { /* if (browser.isMobile()) {//手机版如果要2k的将这里去掉 if (settings.tiling.mobileHighQualityOverride) { return this.getPanoSize(PanoSizeClass.HIGH); } else { return this.getPanoSize(PanoSizeClass.STANDARD); } } else */if (this.useUltraHighResolutionPanos ) { return this.getPanoSize(PanoSizeClass$1.ULTRAHIGH); } else { return this.getPanoSize(PanoSizeClass$1.HIGH); } } else { return this.getPanoSize(PanoSizeClass$1.STANDARD); } } } } let {DownloadStatus} = Potree.defines; var h = Object.freeze({ None: 0, DirectionalFOV: 1 }); var u = function () { var e = function e(t, i) { var n = e._panoSpaceDir, r = e._fovThreshold, o = e._fovThresholdNarrow, a = Math.max(Math.min(n.dot(t.direction), 1), -1), s = Math.max(Math.min(n.dot(i.direction), 1), -1); return t._dot = a, i._dot = s, a >= r && s < r ? -1 : a < r && s >= r ? 1 : a >= o && s < o ? -1 : a < o && s >= o ? 1 : t.panoSize > i.panoSize ? 1 : i.panoSize > t.panoSize ? -1 : -(a - s) }; return e._panoSpaceDir = new Vector3, e._fovThreshold = -1, e._fovThresholdNarrow = -1, e }(); class TilePrioritizer {//优先级处理序列 constructor(e,t, i, o, a) { this.qualityManager = e; this.maxNavQuality = this.qualityManager.getMaxNavPanoSize(); this.maxZoomQuality = this.qualityManager.getMaxZoomPanoSize(); this.baseSize = t; this.standardSize = i; this.highSize = o; this.ultraHighSize = a; this.priorityCriteria = new TilePrioritizer.PriorityCriteria(null, new Vector3(0, 0, 0), new Vector3(0, 0, -1), new Vector3(0, 0, -1)); } updateCriteria(e, t, i, n) {//由player更新 this.priorityCriteria.pano = e, this.priorityCriteria.cameraPosition.copy(t), //this.priorityCriteria.cameraDir.copy(i), this.priorityCriteria.cameraDirs = i; this.priorityCriteria.upcomingPanos = n, this.maxNavQuality = this.qualityManager.getMaxNavPanoSize(), this.maxZoomQuality = this.qualityManager.getMaxZoomPanoSize(); } canDownloadSize(e) { return this.maxNavQuality >= e || this.maxZoomQuality >= e && this.zoomingActive } /* populateNeighborPanos(e, t, i) { i = i || [], i.length = 0; var n = t.getNeighbours(e); for (var r in n) if (n.hasOwnProperty(r)) { var o = t.get(r); if(!o){ console.log(1) } i.push(o) } return i } */ populateScoredPanos(e, t, i, dirs, a, dontFilterDir) { i = i || [], i.length = 0; var s = [Images360.filters.not(e)], l = [Images360.scoreFunctions.distanceSquared(e), Images360.scoreFunctions.direction(e.position, dirs)], c = Common.sortByScore(t, s, l); if(!dontFilterDir){ s.push(Images360.filters.inPanoDirection(e.position, dirs, TilePrioritizer.DIRECTION_SCORE_STRICTNESS),); } if (c) for (var h = 0; h < c.length && h < a; h++) { var u = c[h].item; i.push(u); } return i } queueTilesForPanos(e, t, i, n, r) { for (var o = 0, a = 0; a < t.length; a++) { var s = t[a], l = this.queueTilesForPano(e, i, s, n); if (o += l > 0 ? 1 : 0, r && o >= r) break } return o } /* queueTilesInDirectionForPanos(e, t, i, n, r, o, a, s) {//没用到 for (var l = 0, c = 0; c < i.length; c++) { var h = i[c], u = this.queueTilesInDirectionForPano(e, t, h, n, o, a); if (l += u > 0 ? 1 : 0, s && l >= s) break } return l } */ canIncludeDescriptor(e) { return e.status !== DownloadStatus.Downloading && e.status !== DownloadStatus.Downloaded } canIncludePano(e, t) { return !e.isLoaded(t) } getFOVDotThreshold(e) { return Math.cos(MathUtils.degToRad(e / 2)) } setZoomingActive(e) { e !== this.zoomingActive && (this.zoomingActive = e); } } TilePrioritizer.PriorityCriteria = function (e, t, i, n, o) { this.pano = e, this.cameraPosition = (new Vector3).copy(t), //this.cameraDir = (new THREE.Vector3).copy(i), this.cameraDirs = [], // this.panoSpaceDir = (new Vector3).copy(n), this.upcomingPanos = o, this.copy = function (e) { this.pano = e.pano, this.cameraPosition.copy(e.cameraPosition), //this.cameraDir.copy(e.cameraDir), this.cameraDirs = e.cameraDirs; this.panoSpaceDir.copy(e.panoSpaceDir), this.upcomingPanos = o; }, this.zoomingActive = !1; }; TilePrioritizer.DIRECTIONAL_FOV = 180; TilePrioritizer.DIRECTIONAL_FOV_NARROW = 120; TilePrioritizer.MAX_SCORED_PANOS_TOCONSIDER = 6; TilePrioritizer.MAX_SCORED_PANOS_TOADD = 2; TilePrioritizer.MAX_UPCOMING_PANOS_TOADD = 3; TilePrioritizer.DIRECTION_SCORE_STRICTNESS = .75; TilePrioritizer.appendQueue = function (e, t) { if (e && t) for (var i = 0; i < t.length; i++){ e.push(t[i]); //console.log(t[i]) } }; TilePrioritizer.sortPanoTiles = function (descriptors, pano, dir) { if(dir.datasetsLocal) dir = dir.datasetsLocal.find(e=>e.datasetId == pano.pointcloud.dataset_id).direction;//add u._panoSpaceDir.copy(dir); TileUtils.getRelativeDirection(pano.quaternion4dkk, u._panoSpaceDir); //应该是将dir根据quaternion转化下 u._fovThresholdNarrow = math.getFOVDotThreshold(TilePrioritizer.DIRECTIONAL_FOV_NARROW); u._fovThreshold = math.getFOVDotThreshold(TilePrioritizer.DIRECTIONAL_FOV); descriptors.sort(u); }; TilePrioritizer.insertSortedPanoTile = function (e, t, pano, dir) { if(dir.datasetsLocal) dir = dir.datasetsLocal.find(e=>e.datasetId == pano.pointcloud.dataset_id).direction;//add u._panoSpaceDir.copy(dir), TileUtils.getRelativeDirection(pano.quaternion4dkk, u._panoSpaceDir), u._fovThresholdNarrow = math.getFOVDotThreshold(TilePrioritizer.DIRECTIONAL_FOV_NARROW), u._fovThreshold = math.getFOVDotThreshold(TilePrioritizer.DIRECTIONAL_FOV); for (var o = -1, a = 0; a < e.length; a++) { var s = u(t, e[a]); if (s <= 0) { o = a; break } } if (o === -1) e[e.length] = t; else { for (var h = e.length; h > o; h--) e[h] = e[h - 1]; e[o] = t; } }; TilePrioritizer.prototype.filterDepthTex = function (panos ) {// 下载depthTex if(!Potree.settings.useDepthTex || !this.priorityCriteria.pano || viewer.mainViewport.view.isFlying())return let cameraDirLocals = this.priorityCriteria.cameraDirs.vectorForward; let nearPanos = []; //用于获得邻近点位序列 this.populateScoredPanos(this.priorityCriteria.pano, panos, nearPanos, cameraDirLocals , Infinity, true); let dlCount = browser.isMobile() ? 1 : 2; let loadingCount = panos.filter(p=>p.depthTexLoading).length; if(viewer.mapViewer){ let mapLayer = viewer.mapViewer.mapLayer; loadingCount += mapLayer.loadingInProgress * 1.5 + mapLayer.waitQueue.length; } if(loadingCount!p.depthTex).slice(0, dlCount-loadingCount).forEach(p=>p.loadDepthImg()); } this.nearPanos = nearPanos; }; TilePrioritizer.prototype.filterAndPrioritize = function () {//挑选出优先加载的 pano和tile (有点复杂,没看很懂) var e = [], t = [], i = []; return function (queue, panos, tileDownloader) { //this.populateNeighborPanos(this.priorityCriteria.pano, panos, e); /* let cameraDirLocals = this.priorityCriteria.cameraDirs.map(e=>{ //add var dataset = viewer.scene.pointclouds.find(u=>u.dataset_id == e.datasetId) var matrix = new THREE.Matrix4().copy(dataset.rotateMatrix) var direction = math.convertVector.YupToZup(e.direction) return { datasetId:e.datasetId, direction: direction.clone().applyMatrix4(matrix) } }) */ let cameraDirLocals = this.priorityCriteria.cameraDirs.vectorForward; //获得视野范围内的邻近点位序列t this.populateScoredPanos(this.priorityCriteria.pano, panos, t, cameraDirLocals , TilePrioritizer.MAX_SCORED_PANOS_TOCONSIDER); //t.filter(p=>!p.depthTex).slice(0, Potree.config.depTexDlCount).forEach(p=>p.loadDepthImg()) //add var s = this.baseSize //512 , l = this.standardSize //1024 , c = this.highSize //2048 , h = this.ultraHighSize; //4096 this.queueTilesForPano(queue, tileDownloader, this.priorityCriteria.pano, s); //把当前pano的512下载了 if (this.priorityCriteria.upcomingPanos) {// 添加即将走到的点(之前用于导览路线)512 tiles this.queueTilesForPanos(queue, this.priorityCriteria.upcomingPanos, tileDownloader, s, TilePrioritizer.MAX_UPCOMING_PANOS_TOADD); } i.length = 0; //把当前pano角度范围内的tile按照分辨率从低到高加入队列 if (this.canDownloadSize(l)) {//1024如果在限制范围内的话 this.queueTilesInDirectionForPano(i, tileDownloader, this.priorityCriteria.pano, l, this.priorityCriteria.cameraPosition, this.priorityCriteria.cameraDirs, TilePrioritizer.DIRECTIONAL_FOV_NARROW); } TilePrioritizer.sortPanoTiles(i, this.priorityCriteria.pano, this.priorityCriteria.cameraDirs); //排序 TilePrioritizer.appendQueue(queue, i); //添加邻近点t 512的tiles this.queueTilesForPanos(queue, t, tileDownloader, s, TilePrioritizer.MAX_SCORED_PANOS_TOADD); i.length = 0; //NARROW : if (this.canDownloadSize(c)) {//2048 this.queueTilesInDirectionForPano(i, tileDownloader, this.priorityCriteria.pano, c, this.priorityCriteria.cameraPosition, this.priorityCriteria.cameraDirs, TilePrioritizer.DIRECTIONAL_FOV_NARROW); } if (this.canDownloadSize(h)) {//4096 this.queueTilesInDirectionForPano(i, tileDownloader, this.priorityCriteria.pano, h, this.priorityCriteria.cameraPosition, this.priorityCriteria.cameraDirs, TilePrioritizer.DIRECTIONAL_FOV_NARROW); } TilePrioritizer.sortPanoTiles(i, this.priorityCriteria.pano, this.priorityCriteria.cameraDirs);//排序 TilePrioritizer.appendQueue(queue, i); i.length = 0; if (this.canDownloadSize(l)) {//1024 this.queueTilesInDirectionForPano(i, tileDownloader, this.priorityCriteria.pano, l, this.priorityCriteria.cameraPosition, this.priorityCriteria.cameraDirs, TilePrioritizer.DIRECTIONAL_FOV); } if (this.canDownloadSize(c)) {//2048 this.queueTilesInDirectionForPano(i, tileDownloader, this.priorityCriteria.pano, c, this.priorityCriteria.cameraPosition, this.priorityCriteria.cameraDirs, TilePrioritizer.DIRECTIONAL_FOV); } if (this.canDownloadSize(h)) {//4096 this.queueTilesInDirectionForPano(i, tileDownloader, this.priorityCriteria.pano, h, this.priorityCriteria.cameraPosition, this.priorityCriteria.cameraDirs, TilePrioritizer.DIRECTIONAL_FOV); } TilePrioritizer.sortPanoTiles(i, this.priorityCriteria.pano, this.priorityCriteria.cameraDirs);//排序 TilePrioritizer.appendQueue(queue, i); this.queueTilesForPanos(queue, e, tileDownloader, s); // 如果前面有populateNeighborPanos的话,这步就是加neibour } }(); TilePrioritizer.prototype.queueTilesInDirectionForPano = function () { var e = { filter: h.DirectionalFOV, direction: new Vector3, fov: 60 }, t = new Vector3; return function (i, n, pano, o, a, dirs, c) { var dir = dirs.datasetsLocal.find(e=>e.datasetId == pano.pointcloud.dataset_id).direction;//add //var dir = dirs t.copy(dir); TileUtils.getRelativeDirection(pano.quaternion4dkk, t); e.direction.copy(t); e.fov = c; return this.filterAndQueueTileDownloadDescriptors(i, n, pano, o, e) } }(); TilePrioritizer.prototype.filterAndQueueTileDownloadDescriptors = function () { var e = []; return function (t, i, n, r, o) { var a = i.getTileDownloadDescriptors(n, r); e.length = 0, this.filterTileDownloadDescriptors(n, a, e, o); for (var s = 0, l = 0; l < e.length; l++) { var c = e[l]; if (c) { t.push(c); s++; } } return s } }(); TilePrioritizer.prototype.filterTileDownloadDescriptors = function () { new Vector3; return function (e, t, i, n) { var r, o; switch (n.filter) { case h.DirectionalFOV: for (r = 0; r < t.length; r++) o = t[r], TileUtils.isTileWithinFOV(o.panoSize, o.tileSize, o.face, o.tileX, o.tileY, n.direction, n.fov) && i.push(o); break; default: for (r = 0; r < t.length; r++) o = t[r], i.push(o); } for (r = 0; r < i.length; r++) o = i[r], this.canIncludeDescriptor(o) || (i[r] = null); } }(); TilePrioritizer.prototype.queueTilesForPano = function () { var e = { filter: h.None }; return function (t, i, n, r) { return this.filterAndQueueTileDownloadDescriptors(t, i, n, r, e) } }(); /* TilePrioritizer.prototype.queueTilesForPanosInDirection = function () { //没用到 var e = new THREE.Vector3; return function (t, i, n, r, o, a, s, l) { for (var h = 0, u = 0; u < n.length; u++) { var d = n[u]; e.copy(d.position), e.sub(o), e.normalize(); var p = Math.max(Math.min(a.dot(e), 1), -1), f = c.getFOVDotThreshold(s); if (p >= f) { var g = this.queueTilesInDirectionForPano(t, i, d, r, o, a, s); if (h += g > 0 ? 1 : 0, l && h >= l) break } } return h } }() */ //import { i18n } from "@/lang/index" // 媒体名称 /* export const mediaTypes = { image: i18n.t("common.photo"), video: i18n.t("common.video"), audio: i18n.t("common.voice"), } */ // 媒体扩展类型 const mediaMimes = { image: ["jpg", "png", "jpeg", "bmp", "gif"], audio: ["mp3", "aac", "ogg", "wav" /* , "m4a" */], video: ["mp4", "mov", "quicktime", "webm" /* "rmvb", "wmv" */], //ios:mov }; // 媒体大小显示(MB) const mediaMaxSize = { image: 10, video: 20, audio: 5, }; /** * 获取媒体扩展类型 * @param {Stirng} filename 文件名称 */ const getMime = filename => { if (!filename || filename.indexOf(".") === -1) { return "" } return filename .split(".") .pop() .toLowerCase() }; /** * 在路径中获取文件名 * @param {*} path */ const getFilename = path => { const segment = (path || "").split("/"); return segment[segment.length - 1] }; /** * 检测媒体文件是否超过预设限制 * @param {String} type 媒体类型 * @param {Number} size 文件大小 */ const checkSizeLimit = (type, size) => { size = size / 1024 / 1024; return size <= mediaMaxSize[type] }; const checkSizeLimitFree = (size, limit) => { size = size / 1024 / 1024; return size <= limit }; /** * 检测媒体类型 * @param {String} type 媒体类型 * @param {String} filename 文件名称 */ const checkMediaMime = (type, filename) => { const mime = getMime(filename); const find = mediaMimes[type]; if (!find) { return false } return find.indexOf(mime) !== -1 }; const checkMediaMimeByAccept = (accept, filename) => { let mime = getMime(filename); let type = accept; if (type && type.indexOf("jpg") == -1 && type.indexOf("jpeg") != -1) { type += ",image/jpg"; } return (type || "").indexOf(mime) != -1 }; const base64ToBlob = base64 => { let arr = base64.split(","), mime = arr[0].match(/:(.*?);/)[1], bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n); while (n--) { u8arr[n] = bstr.charCodeAt(n); } return new Blob([u8arr], { type: mime }) }; const base64ToDataURL = base64 => { return window.URL.createObjectURL(base64ToBlob(base64)) }; const blobToBase64 = function(blob) { return new Promise(resolve => { var reader = new FileReader(); reader.onload = function() { resolve(reader.result); }; reader.readAsDataURL(blob); }) }; /* * @Author: Rindy * @Date: 2019-08-06 16:25:08 * @LastEditors: Rindy * @LastEditTime: 2021-08-27 12:33:49 * @Description: Request */ //import { $alert, $confirm, $loginTips } from "@/components/shared/message" //import { $waiting } from "@/components/shared/loading" //import { checkLogin } from "@/api" //import { LoginDetector } from "@/core/starter" //import { i18n } from "@/lang" //import { password } from "@/utils/string" //import store from "../Store" // 空函数 const noop = function() {}; // 请求回调队列 let postQueue = []; /** * @property {number} NEXT - 继续执行 * @property {number} SUCCESS - 成功 * @property {number} EXCEPTION - 异常错误 * @property {number} FAILURE_CODE_3001 - 缺少必要参数 * @property {number} FAILURE_CODE_3002 - 访问异常 * @property {number} FAILURE_CODE_3003 - 非法访问 * @property {number} FAILURE_CODE_3004 - 用户未登录 * @property {number} FAILURE_CODE_3005 - 验证码已过期 * @property {number} FAILURE_CODE_3006 - 验证码错误 * @property {number} FAILURE_CODE_3007 - 昵称已存在 * @property {number} FAILURE_CODE_3008 - 该手机已被注册 * @property {number} FAILURE_CODE_3009 - 两次输入的密码不一致 * @property {number} FAILURE_CODE_3010 - 昵称长度错误 * @property {number} FAILURE_CODE_3011 - 密码长度错误 * @property {number} FAILURE_CODE_3012 - 昵称包含敏感词 * @property {number} FAILURE_CODE_3013 - 手机号码格式错误 * @property {number} FAILURE_CODE_3014 - 账号或密码不正确 * @property {number} FAILURE_CODE_3015 - 用户不存在 * @property {number} FAILURE_CODE_3016 - 您没有权限,请联系管理员 * @property {number} FAILURE_CODE_3017 - 空文件 * @property {number} FAILURE_CODE_3018 - 需要上传或使用的文件不存在 * @property {number} FAILURE_CODE_5010 - 找不到该场景对应的相机 * @property {number} FAILURE_CODE_5012 - 数据不正常 * @property {number} FAILURE_CODE_5014 - 无权操作该场景 * @property {number} FAILURE_CODE_5005 - 场景不存在 */ const statusCode = { NEXT: -999, SUCCESS: 0, EXCEPTION: -1, FAILURE_CODE_3001: 3001, FAILURE_CODE_3002: 3002, FAILURE_CODE_3003: 3003, FAILURE_CODE_3004: 3004, FAILURE_CODE_3005: 3005, FAILURE_CODE_3006: 3006, FAILURE_CODE_3007: 3007, FAILURE_CODE_3008: 3008, FAILURE_CODE_3009: 3009, FAILURE_CODE_3010: 3010, FAILURE_CODE_3011: 3011, FAILURE_CODE_3012: 3012, FAILURE_CODE_3013: 3013, FAILURE_CODE_3014: 3014, FAILURE_CODE_3015: 3015, FAILURE_CODE_3016: 3016, FAILURE_CODE_3017: 3017, FAILURE_CODE_3018: 3018, FAILURE_CODE_5010: 5010, FAILURE_CODE_5012: 5012, FAILURE_CODE_5014: 5014, FAILURE_CODE_5005: 5005, }; /** * 获取Token * @function */ function getToken() { var urlToken = browser.urlHasValue("token", true); if (urlToken) { // 设置token共享给用户中心 localStorage.setItem("token", urlToken); } return urlToken || localStorage.getItem("token") || "" } /** * 根据状态码的结果处理后续操作 * @function * @param {number} code - 状态码 * @param {function} callback - 回调 */ function statusCodesHandler(code, callback) { if (code == statusCode.EXCEPTION) { return $alert({ content: i18n.t("tips.exception") }) } if ( code == statusCode.FAILURE_CODE_3002 || code == statusCode.FAILURE_CODE_3003 || code == statusCode.FAILURE_CODE_3004 ) { callback(code); return showLoginTips() } if (code == statusCode.FAILURE_CODE_3001) { callback(code); return $alert({ content: i18n.t("tips.params_notfound") }) } if (code == statusCode.FAILURE_CODE_3017) { callback(code); return $alert({ content: i18n.t("tips.file_notfound") }) } if (code == statusCode.FAILURE_CODE_5005) { /* if (!config.isEdit) { return (location.href = config.pages.NotFound) } */ callback(code); return $alert({ content: i18n.t("tips.scene_notfound") }) } if (code == statusCode.FAILURE_CODE_5010) { callback(code); return $alert({ content: i18n.t("tips.camera_notfound") }) } if (code == statusCode.FAILURE_CODE_5012) { callback(code); return $alert({ content: i18n.t("tips.data_error") }) } if (code == statusCode.FAILURE_CODE_5014) { callback(code); return $alert({ content: i18n.t("tips.auth_deny") }) } return statusCode.NEXT } $.ajaxSetup({ headers: {}, beforeSend: function(xhr) { const token = getToken(); if (token) { xhr.setRequestHeader("token", token); } else if (!token && this.url.indexOf("isLogin") != -1) { showLoginTips(); } /* if (config.oem == "localshow") { // 本地版本兼容当前目录 if (this.url.indexOf("http") == -1 && this.url.indexOf("/") == 0) { this.url = this.url.substr(1) } } */ // if(this.url.indexOf('http')==-1 && this.url.indexOf('/') !=0){ // this.url = '/'+this.url // } }, error: function(xhr, status, error) { // 出错时默认的处理函数 if (this.url.indexOf("/scene.json") != -1 && xhr.status == 404) { return $alert({ content: i18n.t("tips.scene_notfound") }) } else if (this.type === "POST") { return $alert({ content: i18n.t("tips.network_error") }) } }, success: function(result) {}, complete: function() { // Post类型请求无论成功或失败都关闭等待提示 if (this.type === "POST") { http.__loading && $waiting.hide(); } http.__loading = true; }, }); /** * @namespace http * @type {Object} */ const http = { statusCode, __loading: true, __request(xhr, method, url, data, done, fail) { if (typeof done != "function") { done = noop; } if (typeof fail != "function") { fail = noop; } xhr.done(result => { if (typeof result.code !== "undefined") { const flag = statusCodesHandler(result.code, function(code) { // 需要登录的状态 if ( code == statusCode.FAILURE_CODE_3001 || code == statusCode.FAILURE_CODE_3002 || code == statusCode.FAILURE_CODE_3003 || code == statusCode.FAILURE_CODE_3004 ) { if (url.indexOf("isLogin") == -1 && url.indexOf("openSceneBykey") == -1) { postQueue.push(function() { http[method](url, data, done, fail); }); } } fail(); }); if (flag === statusCode.NEXT) { done(result, result.code == 0); } } else { done(result); } }); xhr.fail(fail); xhr.always(() => (xhr = null)); return xhr }, /** * Get请求 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ get(url, data = {}, done, fail) { if (/\.json/.test(url)) { // json文件格式自动调用getJson方法 return this.getJson(url, data, done, fail) } return this.__request($.get(url, data), "get", url, data, done, fail) }, /** * Get Blob请求 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ getText(url, data = {}, done, fail) { return this.__request( $.ajax({ url: url, dataType: "text", }), "getText", url, data, done, fail ) }, /** * GetJson请求 读取json文件数据 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ getJson(url, data = {}, done, fail) { return this.__request($.getJSON(url, data), "get", url, data, done, fail) }, /** * Get Blob请求 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ getBlob(url, data = {}, done, fail) { return this.__request( $.ajax({ url: url, dataType: "blob", }), "getBlob", url, data, done, fail ) }, /** * Get Arraybuffer请求 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ getArraybuffer(url, data = {}, done, fail) { return this.__request( $.ajax({ url: url, dataType: "arraybuffer", }), "getArraybuffer", url, data, done, fail ) }, /** * Post 请求 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ post(url, data = {}, done, fail) { if (url.indexOf("isLogin") == -1) { http.__loading && $waiting.show(); } return this.__request($.post(url, data), "post", url, data, done, fail) }, /** * PostJson 请求 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ postJson(url, data = {}, done, fail) { http.__loading && $waiting.show(); return this.__request( $.ajax({ type: "POST", url: url, contentType: "application/json", data: JSON.stringify(data), }), "postJson", url, data, done, fail ) }, /** * Post 表单 支持文件上传 * @param {String} url 请求地址 * @param {FormData?} formData 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ postForm(url, formData, done, fail, onProgress) { if (typeof onProgress === "function") { return this.__request( $.ajax({ type: "POST", url: url, processData: false, contentType: false, data: formData, xhr: function() { const xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", function(e) { onProgress((e.loaded / e.total) * 100 + "%"); }); return xhr }, }), "postForm", url, formData, done, fail ) } else { http.__loading && $waiting.show(); return this.__request( $.ajax({ type: "POST", url: url, processData: false, contentType: false, data: formData, }), "postForm", url, formData, done, fail ) } }, /** * 加载图片 * @param {String} url 请求地址 * @param {Number?} retry 重试次数,默认为3 */ loadImage(url, retry = 3) { const def = $.Deferred(); const img = new Image(); /* if (process.env.VUE_APP_REGION == "AWS" && url.indexOf("x-oss-process=image") != -1) { var arr = url.split("?") url = arr[0] + encodeURIComponent("?" + arr[1].replace(/\//g, "@")) } */ const load = () => { console.warn("Retrying load image: " + url); this.loadImage(url, retry - 1) .done(def.resolve.bind(def)) .progress(def.notify.bind(def)) .fail(def.reject.bind(def)); }; img.onerror = function() { retry > 0 ? setTimeout(() => load(), 1e3) : def.reject(`[${url}]加载失败`); }; img.onload = function() { def.resolve(img); }; img.crossOrigin = "anonymous"; img.src = url; return def }, /** * 上传文件 * @param {String} url 请求地址 * @param {Object?} data 请求参数 * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ uploadFile(url, data = {}, done, fail, onProgress) { const form = new FormData(); // if (file.needTransfer) { //ie和苹果都不支持dataURLtoFile得传送,所以只能用blob // form.append("file", common.dataURLtoBlob(file.file), file.name || file.file.name); // } else { // form.append("file", file.file, file.name || file.file.name); // } for (let key in data) { if (key == "file") { form.append("file", data[key], data.filename || data[key].name); } else if (key != "filename") { form.append(key, data[key]); } } return this.postForm(url, form, done, fail, onProgress) }, /** * 上传文件 * @param {String} url 请求地址 * @param {Object?} data 请求参数 {file:'base64 string',filename:'image.jpg',...} * @param {Function?} done 成功回调 * @param {Function?} fail 失败回调 */ uploadBlobFile(url, data = {}, done, fail) { const form = new FormData(); for (let key in data) { if (key === "file") { form.append("file", base64ToBlob(data.file), data.filename); } else if (key != "filename") { form.append(key, data[key]); } } return this.postForm(url, form, done, fail) }, }; let {TileDownloaderEvents, DownloadStatus: DownloadStatus$1} = Potree.defines; window.downloaded = {}; window.startdownloads = []; class TileDownloader extends EventDispatcher{ constructor( ) { super(); this.panos = null; this.retryMinimumTime = 1e4; this.panoLoadCallbacks = {}; this.downloadDescriptors = {}; this.priorityQueue = []; this.forceQueue = []; this.activeDownloads = []; this.tilePrioritizer = null; this.refreshInterval = null; this.processPriorityQueue = !1; this.concurrentDownloads = 6;//e.concurrentDownloads || 1; this.downloadTestResults = {}; this.freeze = Object.freeze({ Testing: 1, Success: 2, Fail: 3 }); /* viewer.addEventListener('pageVisible', (e)=>{//不可见时不refreshUpdateInterval //console.log('visibilitychange:', state) Potree.Utils.updateVisible(this, 'pageVisible', e.v) this.judgeStart() }) this.visible = true //add 借用Potree.Utils.updateVisible来判断是否start if(Potree.settings.useDepthTex){ this.judgeStart() //开始下载depthTex }else{ Potree.Utils.updateVisible(this,'showPanos', false ) //默认visible = false } */ } setPanoData(e, t /* , i */) { this.panos = e, this.imagePanos = t; // this.panoGroupId = i } start() { this.downloadCubeTex = true; /* if(!Potree.settings.useDepthTex){ Potree.Utils.updateVisible(this,'showPanos', true ) this.judgeStart() }else{ //this.refreshInterval || this.judgeStart() } */ } stop() { this.downloadCubeTex = false; /* if(!Potree.settings.useDepthTex){ Potree.Utils.updateVisible(this,'showPanos', false ) this.judgeStart() } */ } /* judgeStart(){//add if(this.visible){ //console.log('judgeStart true') this.started = true //this.refreshUpdateInterval(0) }else{ //console.log('judgeStart false') this.started = false //window.clearTimeout(this.refreshInterval) } } */ /* refreshUpdateInterval(e) { e || (e = 0), this.refreshInterval = window.setTimeout(function() { var e = this.update(); e ? this.refreshUpdateInterval(TileDownloader.ACTIVE_REFRESH_DELAY) : this.refreshUpdateInterval(TileDownloader.IDLE_REFRESH_DELAY) } .bind(this), e) } */ update() { if(this.downloadCubeTex){ //可以下载贴图 var e = this.forceQueue.length > 0; this.processQueueForDownloading(this.forceQueue); if (this.processPriorityQueue) { Potree.Common.intervalTool.isWaiting('processPriorityQueue', ()=>{ //延时update,防止崩溃 , 未到时间就拦截(第一次直接执行) this.queuePrioritizedTilesForPanos(this.panos); //这句比较耗时 降四倍时大概1-2毫秒 }, 66); this.priorityQueue.length > 0 && (e = !0); this.processQueueForDownloading(this.priorityQueue); } //return e } Potree.Common.intervalTool.isWaiting('filterDepthTex', ()=>{ this.tilePrioritizer.filterDepthTex(this.panos);//下载深度图 }, 77); } queuePrioritizedTilesForPanos(e) { this.tilePrioritizer && (this.clearQueue(this.priorityQueue), this.tilePrioritizer.filterAndPrioritize(this.priorityQueue, e, this), this.clearFromQueue(this.priorityQueue, DownloadStatus$1.None, !0), //去除state为DownloadStatus.None的(可能是去除已经在下载的) this.setStatusOrRemoveForAllDescriptors(this.priorityQueue, DownloadStatus$1.Queued)); } clearQueue(e) {//停止下载并清空 this.setStatusForAllDescriptors(e, DownloadStatus$1.None), e.length = 0; } clearForceQueue() { this.clearQueue(this.forceQueue); } clearFromQueue(e, t, i) { for (var n = 0; n < e.length; n++) { var r = e[n]; r && (t === r.status && !i || t !== r.status && i) && (e[n] = null); } } setStatusForAllDescriptors(e, t) { for (var i = 0; i < e.length; i++) { var n = e[i]; n && (n.status = t); } } setStatusOrRemoveForAllDescriptors(e, t) { for (var i = 0; i < e.length; i++) { var n = e[i]; n && (n.status !== t ? n.status = t : e[i] = null); } } getTileDownloadDescriptors(pano, size) {//获取该pano的该size的全部的tile的descriptor var i = this.getAllTileDownloadDescriptorsForPano(pano), n = i[size]; return n || (n = this.buildDownloadDescriptorArray(size),//创建的全部是空的 i[size] = n, this.initTileDownloadDescriptors(n, pano, size)),//绑定到该pano size n } getAllTileDownloadDescriptorsForPano(pano) {//新建空Descriptors var t = this.downloadDescriptors[pano.id]; return t || (t = {}, this.downloadDescriptors[pano.id] = t), t } processQueueForDownloading(e, t) {//执行下载任务 this.cleanupActiveDownloads(); if(e.length){ let concurrentDownloads = Potree.Common.getBestCount('concurrentDownloads',1,6, 1.8, 14/* ,true */); //flying ? (isMobile ? 2 : 3) : 6 if (this.activeDownloads.length < concurrentDownloads || t) { var i = t ? e.length : concurrentDownloads - this.activeDownloads.length; for (var n = 0, r = 0; n < i && e.length > 0; r++) { var o = e.shift(); if(o){ //add 为了防止1024的在512前下载完,这里强行等待512下载完毕再开始下载 if(o.panoSize > 512 && !this.isPanoDownloaded(o.pano, 512) ){ //console.log('512的还没下载好呢!') e.push(o); break;//一般512的都是连续下载的,所以后面就都不是512了直接中断 } this.startDownload(o); n++; } } } } } testDownload(panoSize, tileSize, callback) { var n = this.downloadTestResults[panoSize]; if (n) return void(n === this.freeze.Success ? callback(!0) : n === this.freeze.Fail && callback(!1)); this.downloadTestResults[panoSize] = this.freeze.Testing; var r = this.panos[0], o = this.getTileUrl({pano:r, panoSize, tileSize, tileIndex:0} /* r.id, panoSize, tileSize, 0 */), a = function(t) { this.downloadTestResults[panoSize] = this.freeze.Success, callback(!0); } .bind(this), s = function() { this.downloadTestResults[panoSize] = this.freeze.Fail, callback(!1); } .bind(this); this.loadImage(o, 0, a, s); } startDownload(e) {//开始下载啦 //console.log('startDownload') startdownloads.push(e); e.local2SrcFailed = this.local2SrcFailed; e.status = DownloadStatus$1.Downloading; var t = this.getTileUrl(e/* e.pano.id, e.panoSize, e.tileSize, e.tileIndex, e.pano.alignmentType */);//xzw add alignmentType if(!t)return; this.activeDownloads.push(e); this.loadImage(t, TileDownloader.DOWNLOAD_RETRIES, this.downloadComplete.bind(this, e), this.downloadFailed.bind(this, e)); } downloadFailed(e, t) { //add if(Potree.settings.isLocal2 && !e.local2SrcFailed){//为了兼容旧的数据src,如果新src没加载成功,就加载旧的 e.local2SrcFailed = this.local2SrcFailed = true; //this.startDownload(e)//重新下载 var t = this.getTileUrl(e); this.loadImage(t, TileDownloader.DOWNLOAD_RETRIES, this.downloadComplete.bind(this, e), this.downloadFailed.bind(this, e)); } } downloadComplete(e, t) {//下载成功时 //if (e.panoGroupId === this.panoGroupId) { var i = this.getPanoLoadCallbacks(e.pano, e.panoSize); e.status = DownloadStatus$1.Downloaded, i && i.onProgress && i.onProgress(e.pano, e.panoSize); var n = { panoId: e.pano.id, image: t, tileSize: e.tileSize, panoSize: e.panoSize, tileIndex: e.tileIndex, faceTileIndex: e.faceTileIndex, totalTiles: e.totalTiles, face: e.face, tileX: e.tileX, tileY: e.tileY, direction: e.direction }; downloaded[e.pano.id] || (downloaded[e.pano.id]={512:[],1024:[],2048:[]}); downloaded[e.pano.id][e.panoSize] || (downloaded[e.pano.id][e.panoSize] = []); downloaded[e.pano.id][e.panoSize].push(e); if(e.panoSize != 512 && downloaded[e.pano.id][512].length<6){ console.warn('没下完'); } e.image = t, this.dispatchEvent({type:TileDownloaderEvents.TileDownloadSuccess, desc:n} ); this.isPanoDownloaded(e.pano, e.panoSize) && (n = { panoId: e.pano.id, tileSize: e.tileSize, panoSize: e.panoSize }, this.dispatchEvent({type:TileDownloaderEvents.PanoDownloadComplete, desc:n}), i && i.onLoad && i.onLoad(e.pano, e.panoSize)); //} } isPanoDownloaded(e, t) { var i = this.getTileDownloadDescriptors(e, t); if (i.length <= 0) return !1; for (var n = 0; n < i.length; n++) { var r = i[n]; if (r.status !== DownloadStatus$1.Downloaded) return !1 } return !0 } setPanoLoadCallbacks(e, t, i, n, r) { var o = e.id + ":" + this.qualityManager.getPanoSize(t); this.panoLoadCallbacks[o] = { onLoad: i, onFail: n, onProgress: r }; } getPanoLoadCallbacks(e, t) { var i = e.id + ":" + t; return this.panoLoadCallbacks[i] } buildDownloadDescriptorArray(e) { for (var t = TileUtils.getTileCountForSize(e), i = [], n = 0; n < t; n++) { var r = this.buildDownloadDescriptor(); i.push(r); } return i } buildDownloadDescriptor() {//Descriptor! var e = { panoGroupId: null, pano: null, panoSize: -1, tileSize: -1, tileIndex: -1, totalTiles: -1, faceTileIndex: -1, status: DownloadStatus$1.None, url: null, image: null, direction: new Vector3, //该tile在cube中的方向 face: -1, cubeFace: -1, tileX: -1, tileY: -1 }; return e } initTileDownloadDescriptors(e, t, i) { for (var n = 0; n < e.length; n++) { var r = e[n]; this.initTileDownloadDescriptor(r, t, i, n); } } initTileDownloadDescriptor(desc, pano, size, index) { var r = size >= TileUtils.TILE_SIZE ? TileUtils.TILE_SIZE : size; desc.face = TileUtils.getFaceForTile(size, index);//根据顺序得到的face的index desc.cubeFace = TileUtils.mapFaceToCubemapFace(desc.face);//为了贴图而转化的face index //desc.panoGroupId = this.panoGroupId;//就是场景号 desc.pano = pano; desc.panoSize = size; desc.tileSize = r; //瓦片图size 512 desc.tileIndex = index; desc.totalTiles = TileUtils.getTileCountForSize(size); desc.status = DownloadStatus$1.None; desc.image = null; TileUtils.getTileLocation(desc.panoSize, desc.tileIndex, desc);//得到该tile在这个face中的具体位置(tileX等) TileUtils.getTileVector(desc.panoSize, desc.tileSize, desc.cubeFace, desc.tileX, desc.tileY, TileUtils.LocationOnTile.Center, 0, desc.direction); } getTiles(d, sceneNum, useV4url, pointcloud){ let v3OrV4Str = useV4url ? `/scene_view_data/${sceneNum}/images/` : `/images/images${sceneNum}/`; if(Potree.settings.isLocal && !this.local2SrcFailed && pointcloud.datasetData.mapping && !Potree.settings.isLocal2){ //非离线包的话加mapping return `${Potree.settings.urls.prefix3}/${pointcloud.datasetData.mapping}${v3OrV4Str}${d}` } return `${Potree.settings.urls.prefix3}${v3OrV4Str}${d}` /* if(Potree.settings.isLocal && !this.local2SrcFailed || useV4url){//新的地址 scene_view_data/场景码/images/tiles if(pointcloud.datasetData.mapping && !Potree.settings.isLocal2){//非离线包的话加mapping return `${Potree.settings.urls.prefix3}/${pointcloud.datasetData.mapping}/scene_view_data/${sceneNum}/images/${d}` }else{ return `${Potree.settings.urls.prefix3}/scene_view_data/${sceneNum}/images/${d}` } } return `${Potree.settings.urls.prefix3}/images/images${sceneNum}/${d}` */ } loadImage(e, t, i, n) { //自己修改了ajax,把getImage改成了loadImg http.loadImage(e, t).then(function(e) { i(e); }).fail(n); } } TileDownloader.prototype.forceQueueTilesForPano = function() {//根据条件开始加载tile var e = [], t = []; return function(pano, size, dir, hFov, vFov, download) { e.length = 0; for (var u = this.getTileDownloadDescriptors(pano, size), d = 0; d < u.length; d++) { var p = u[d]; p.status !== DownloadStatus$1.None && p.status !== DownloadStatus$1.Queued || e.push(p); } if (dir && e.length > 0) { TilePrioritizer.sortPanoTiles(e, pano, dir); //按最佳方向排序e t.length = 0; TileUtils.matchingTilesInDirection(pano, size, dir, hFov, vFov, t);//得到在符合视野标准的集合t for (var f = 0, g = function(e) { return e.face === m.face && e.faceTileIndex === m.faceTileIndex }; f < e.length;) { //过滤掉不符合角度要求的 var m = e[f], v = t.findIndex(g); v < 0 ? e.splice(f, 1) : f++; } } for (var A = 0; A < e.length; A++){ this.forceQueue.push(e[A]); //装载 } /* if(e.length){ console.log(e) } */ this.setStatusForAllDescriptors(this.forceQueue, DownloadStatus$1.ForceQueued); this.clearFromQueue(this.priorityQueue, DownloadStatus$1.ForceQueued, !1); download && this.processQueueForDownloading(this.forceQueue, !0); } }(); TileDownloader.prototype.cleanupActiveDownloads = function() { var e = []; return function() { e.length = 0; for (var t = 0; t < this.activeDownloads.length; t++) { var i = this.activeDownloads[t]; i.status !== DownloadStatus$1.Downloaded && i.status !== DownloadStatus$1.Failed && e.push(i); } this.activeDownloads.length = 0, this.activeDownloads.push.apply(this.activeDownloads, e); } }(); TileDownloader.prototype.getTileUrl = function() { var e = { 256: "256", 512: "512", 1024: "1k", 2048: "2k", 4096: "4k" }, t = { face: -1, faceTileIndex: -1, tileX: -1, tileY: -1 }; return function(o={} ) { var id = o.pano.originID, //////// panoSize = o.panoSize, tileSize = o.tileSize, tileIndex = o.tileIndex, sceneCode = o.pano.pointcloud.sceneCode, useV4url = Potree.settings.useV4url && o.pano.pointcloud.datasetData.sceneVersion == 'V4'; //v4的全景图等路径不一样 var metadata = {sceneScheme:10}; TileUtils.getTileLocation(panoSize, tileIndex, t); var s = Math.floor(panoSize / tileSize), l = s * s, h = Math.floor(tileIndex / l), u = "", d = '', g = ''; if(Potree.settings.tileOriginUrl){//原始规则 //1 === config.tiling.customCompression && (u = "_" + config.tiling["q" + e[panoSize]]); //1 === o.tiling.customCompression && (u = "_" + o.tiling["q" + e[n]]); d = "tiles/" + id + "/" + e[panoSize] + u + "_face" + h + "_" + t.tileX + "_" + t.tileY + ".jpg"; d = this.getTiles(d, sceneCode, useV4url, o.pano.pointcloud); g = "?"; }else {//阿里云oss的规则 if (metadata.sceneScheme == 10) d = 'tiles/4k/' + id + '_skybox' + h + '.jpg?x-oss-process='; if (e[panoSize] == '512') { d += 'image/resize,h_512'; } else { //4k的图,移动端是1k,pc端是2k,放大才是4k if (e[panoSize] == '1k' || e[panoSize] == '2k') { //https://4dkk.4dage.com/images/imagesx4iqYDG3/tiles/4k/122_skybox0.jpg?x-oss-process=image/resize,m_lfit,w_1024/crop,w_512,h_512,x_511,y_0 d += 'image/resize,m_lfit,w_' + panoSize + '/crop,w_512,h_512,'; } else { d = 'tiles/4k/' + id + '_skybox' + h + '.jpg?x-oss-process=image/crop,w_512,h_512,'; } //起始位置 if (t.tileX == 0) { d += 'x_0,'; } else { d += 'x_' + (512 * t.tileX /* - 1 */) + ','; } if (t.tileY == 0) { d += 'y_0'; } else { d += 'y_' + (512 * t.tileY /* - 1 */); } } d = this.getTiles(d, sceneCode, useV4url, o.pano.pointcloud); g = "&"; } //Potree.settings.panoVersion = 4 d += g + (Potree.settings.panoVersion ? 'version='+Potree.settings.panoVersion : 'time='+o.pano.pointcloud.timeStamp ); //加后缀 return d; /* 4dkk getViewImagesURL(url) { if (url.indexOf('&_=') !== -1) { return url } let version = url.indexOf('/panorama/') !== -1 ? this.linkVersion : this.imageVersion if (url.indexOf('?') !== -1) { return this.$app.config.resource + `scene_view_data/${this.num}/images/${url}&_=${version}` } return this.$app.config.resource + `scene_view_data/${this.num}/images/${url}?_=${version}` } /** */ } }(); TileDownloader.tilegen = true; TileDownloader.IDLE_REFRESH_DELAY = 500; TileDownloader.ACTIVE_REFRESH_DELAY = 16; TileDownloader.DOWNLOAD_RETRIES = 4; function Node$1(e, t) { this.tree = e, //所属树(TileTree) this.parent = t, this.children = [], this.id = ++u$1; } function o(e, t, i, r, a, s, l, h) { if (e) { l = l || TileTree.TraversalType.PreOrder; var u = r * c + i; if (l === TileTree.TraversalType.PreOrder && (a && a(e, t, u, i, r), s && s.push(e)), e.children && 0 !== e.children.length) { for (var d = r * c, p = i * c, f = 0; f < c; f++) for (var g = 0; g < c; g++) o(e.children[g * c + f], t + 1, p + f, d + g, a, s, l, h); l === TileTree.TraversalType.PostOrder && (a && a(e, t, u, i, r), s && s.push(e)); } } } function Plant(seed) { seed.root = Branch(seed, null, 0); } function Branch(seed, parent, level) { if (level > seed.levels) return null; var node = new Node$1(seed, parent); seed.allNodes.push(node); for (var o = 0; o < h$1; o++) node.children[o] = Branch(seed, node, level + 1); return node } function l(parent, t, level, n, r) { if (!parent) return null; if (0 === level) return parent; if (!parent.children || 0 === parent.children.length) return null; var o = Math.pow(c, level), a = o / c, s = n % a, h = r % a, u = Math.floor(r / a), d = Math.floor(n / a), p = u * c + d, f = parent.children[p]; return l(f, t + 1, level - 1, s, h) } /* cube每个面都有一个分层树 用于代表瓦片图的细分? 树4096的分为三层,每层有4个子节点。(最后一层的四个子节点都是null) */ var c = 2, h$1 = c * c; //4个子节点 var u$1 = 0; class TileTree { constructor(e, t) { this.levels = t, this.tileSize = e, this.root = null, this.allNodes = [], Plant(this); } getSubNode(e, t, i) { (!t || e < this.tileSize) && (t = 0), (!i || e < this.tileSize) && (i = 0), e < this.tileSize && (e = this.tileSize); var level = TileTree.getLevelCountForSize(this.tileSize, e), o = l(this.root, 0, level, t, i); return o } breadthFirst(e) {//广度优先搜索 e = e || {}; var t = !!e.nullLevelEnd, i = e.maxLevel, n = e.minLevel, r = e.callback, o = e.saveVisited, a = [], s = {}, l = 0, c = 0; for (a.push(this.root), a.push(s); a.length > 0 && !(i && l > i);) { var h = a.shift(); if (h === s) (!n || l >= n) && (r && t && r(null), o && t && o.push(null)), a.length > 0 && a.push(s), l++, c = 0; else { if (h.children) for (var u = 0; u < h.children.length; u++) { var d = h.children[u]; d && a.push(h.children[u]); } var p = this.getFaceIndexFromNode(h); (!n || l >= n) && (r && r(h, l, p), o && o.push(h)), c++; } } } getFaceIndexFromNode(e) { if (!e) return -1; for (var t = 1, i = e, n = 0, r = 0;;) { var o = i.parent; if (!o) break; for (var a = -1, s = 0; s < o.children.length; s++) o.children[s] === i && (a = s); var l = a % c, h = Math.floor(a / c); n = l * t + n, r = h * t + r, t *= c, i = o; } return r * t + n } depthFirst(e, t, i) { o(this.root, 0, 0, 0, e, t, i, this.tileSize); } } TileTree.TraversalType = Object.freeze({ PreOrder: 0, PostOrder: 1 }); TileTree.getLevelCountForSize = function(tileSize, size) {//512->0 2024->1 var i = 0; for (size < tileSize && (size = tileSize);;) { if (size /= c, size < tileSize) break; i++; } return i }; TileTree.getSizeForLevel = function(e, t) { return Math.pow(c, t) * e }; let {PanoSizeClass: PanoSizeClass$2, PanoRendererEvents: PanoRendererEvents$1 , Vectors,SceneRendererEvents,TileDownloaderEvents: TileDownloaderEvents$1, GLCubeFaces: GLCubeFaces$2} = Potree.defines; function createDescriptor() { var e = { renderTarget: null, inUse: !1, size: -1, pano: null }; return e } /* function upload() { if (!this.uploadIntervalCancelled) { if (this.overlayTilesLoaded || !this.usingTileOverlay) { b = !0 let maxNonBaseUploadsPerFrame = viewer.mainViewport.view.isFlying() ? 1 : this.maxNonBaseUploadsPerFrame //原先2。这是每帧uploadTile非512的瓦片tex的数量。之前的2太卡了,降为1。(检测卡顿方法:在一个pano点旋转至所有2048的tile都加载完,然后之后到这个点看看卡不卡。因为该点tiles都下载完了所以会在飞过来时陆续都加载,所以容易卡) this.updateUploadQueue(maxNonBaseUploadsPerFrame, this.maxBaseUploadsPerFrame) let time = viewer.mainViewport.view.isFlying() ? 60 : w //add 飞行有时候会卡,增长间隔 this.peekNextFromUploadQueue() ? this.refreshUploadInterval(time) : this.uploadInterval = null //定时下一次更新 } else { this.refreshUploadInterval(this.uploadIntervalDelay) } } } */ var b = !1, w = config$1.tiling.uploadIntervalDelay, _ = config$1.tiling.initialIntervalDelay, T = config$1.tiling.maxNonBaseUploadsPerFrame,//2 size>512的每次只upload这么多个 x$1 = config$1.tiling.maxBaseUploadsPerFrame,//6 S = { Base: 0, Remaining: 1 };/* , M = []; */ class PanoRenderer extends EventDispatcher{ constructor(viewer, tileDownloader, qualityManager) { super(); this.tileDirectory = {}; this.activeRenderTargetDescriptors = {}; this.activePanos = []; this.panoLODDescriptors = {}; this.panoDescriptors = {}; this.tileTrees = {}; this.forceQueue = []; this.uploadQueues = {}; this.uploadInterval = null; this.uploadIntervalCancelled = true;//!1; this.usingTileOverlay = !1; this.overlayTilesLoaded = !1; this.overlayTileBase = null; this.overlayTilesBasic = {}; this.overlayTilesEnhanced = {}; this.zoomRenderTarget = null; //用于缩放的rendertarget this.zoomPano = null; this.zoomingActive = !1; this.zoomPanoId = null; this.zoomPanoRenderingDisabled = !1; this.direction = [];//new THREE.Vector3; this.maxBaseUploadsPerFrame = x$1; this.maxNonBaseUploadsPerFrame = T; this.M = [];//move M to here 似乎列表里会有两个 this.viewer = viewer; this.tileDownloader = tileDownloader; this.qualityManager = qualityManager; this.initTime = performance.now(); this.bindEvents(); } getActivePanoTextures(e) { e = e || []; for (var t = 0; t < M.length; t++) { var i = M[t]; i.renderTarget && i.renderTarget.texture && e.push(i.renderTarget.texture); } } hasQueuedTiles() { var e = this.peekNextFromUploadQueue(); return null !== e && void 0 !== e } getActiveRenderTargetDescriptor(e) { return this.activeRenderTargetDescriptors[e] } setActiveRenderTargetDescriptor(e, t) { this.activeRenderTargetDescriptors[e] = t; } bindEvents() { this.tileDownloader.addEventListener(TileDownloaderEvents$1.TileDownloadSuccess, this.onTileDownloaded.bind(this)); } enableUltraHighQualityMode(e) { if(config$1.tileClass == "2k"||config$1.tileClass == "1k")return this.enableHighQuality(e)//xzw add 濡傛灉鏈€澶氬彧瑕?k鐨勮瘽 if (!this.qualityManager.ultraHighQualityModeEnabled()) { var t = this.qualityManager.getPanoSize(PanoSizeClass$2.ULTRAHIGH); this.tileDownloader.testDownload(t, TileUtils.TILE_SIZE, function (t) { t && (this.qualityManager.enableUltraHighQualityMode(), this.setupZoomRenderTarget(), e()); } .bind(this)); } } activateTiledPano(pano, size, i) { i && this.clearAllQueuedUploads(); for (var n = 0; n < TileUtils.FACES_PER_PANO; n++) this.initTileTree(pano.id, n, this.qualityManager.getMaxPossiblePanoSize()); //得到this.tileTrees[pano.id],arr[6] this.linkAllTilesAndNodes(pano); var r = this.getActiveRenderTargetDescriptor(pano.id), l = size; l > this.qualityManager.getMaxNavPanoSize() && (l = this.qualityManager.getMaxNavPanoSize()); if ( !r || l !== r.size) { r && this.deactiveDescripor(r.renderTarget); r = this.activeDescripor(l); if (!r) { var ren = this.initTiledPano(l, Potree.settings.disableAATarget ? false : !browser.isMobile() ); //有的老设备如果抗锯齿打开场景时要多花五秒 r = this.initDescriptor(ren.width); r.renderTarget = ren; } r.pano = pano; this.resetPanoDescriptor(pano.id); this.resetPanoLODDescriptors(pano.id); this.resetRenderStatus(pano.id, !0, !0); } this.setActiveRenderTargetDescriptor(pano.id, r); var h = i ? 0 : 1; this.updateActivePanos(pano, h); //console.log(`index:${this.viewer.index} ${r.renderTarget.texture.id} ${pano.id}`) return r.renderTarget } deactivateTiledPano(pano) { var t = this.getActiveRenderTargetDescriptor(pano.id); if(this.isRenderTargetDescriptorValid(t)){ this.deactiveDescripor(t.renderTarget); this.setActiveRenderTargetDescriptor(pano.id, null); } var i = this.getUploadQueueForPano(pano.id); this.clearUploadQueue(i); this.updateActivePanos(); viewer.cancelLoad(pano);//add } getActivePanoCount() { return this.activePanos.length } resetRenderStatus(e, t, i, n) { var r = null; n && (r = TileTree.getLevelCountForSize(TileUtils.TILE_SIZE, n) + 1); for (var o = function (e, n, r, o) { i && (n.tile.zoomUploaded = !1), t && (n.tile.uploaded = !1); }, a = 0; a < TileUtils.FACES_PER_PANO; a++) { var s = this.getTileTree(e, a); s.breadthFirst({ callback: o.bind(this, a), minLevel: r }); } } copyBaseRenderStatusToZoomed(e) { for (var t = TileTree.getLevelCountForSize(TileUtils.TILE_SIZE, this.qualityManager.getMaxNavPanoSize()), i = function (e, t, i, n) { t.tile.zoomUploaded = t.tile.uploaded, t.zoomCovered = t.covered; }, n = 0; n < TileUtils.FACES_PER_PANO; n++) { var r = this.getTileTree(e, n); r.breadthFirst({ callback: i.bind(this, n), maxLevel: t }); } } isRenderTargetDescriptorValid(e) { return e && e.renderTarget } isPanoActive(e) { var t = this.getActiveRenderTargetDescriptor(e); return this.isRenderTargetDescriptorValid(t) } isPanoZoomed(e) { return this.zoomingActive && this.zoomPanoId === e } initTileTree(e, t, i) { var n = this.tileTrees[e]; n || (n = [], this.tileTrees[e] = n); var r = n[t]; if (!r) { var o = TileTree.getLevelCountForSize(TileUtils.TILE_SIZE, i); r = new TileTree(TileUtils.TILE_SIZE, o), n[t] = r; } } getTileTree(e, t) { var i = this.tileTrees[e]; if (!i) console.error("PanoRenderer.getTileTree() -> Tree array not yet initialized!"); var n = i[t]; if (!n) console.error("PanoRenderer.getTileTree() -> Tree not yet initialized!"); return n } /* * 创建tile的renderTarget, 包括pano.tiledPanoRenderTarget和zoomRenderTarget * @param {number} size 当前的panoSize,每个面的分辨率 */ initTiledPano(size, ifNormalFilter) {//创建 RenderTargetCube var renderer = this.viewer.renderer; var renderTarget, texture; renderTarget = new WebGLCubeRenderTarget(size,{ //THREE.WebGLRenderTargetCube(size, size, { stencilBuffer: !1 }); texture = new CubeTexture([]); texture.image = [null, null, null, null, null, null]; texture.flipY = !0, texture.format = RGBAFormat; ifNormalFilter ? (texture.generateMipmaps = !0, texture.magFilter = LinearFilter, texture.minFilter = LinearMipMapLinearFilter) : (texture.generateMipmaps = !1, texture.magFilter = LinearFilter, //LinearFilter更清晰,但锯齿噪点严重,相当于锐化,其实失真了。对于条纹状的画面移动镜头时锯齿明显 眩晕 texture.minFilter = LinearFilter); //平时还是直接用LinearMipMapLinearFilter,其实并非不清晰,只是没有加锐化,像加了层柔光和抗锯齿,观感更好。放大后使用LinearFilter // 如果抗锯齿的话,采用mipmap,会增加一倍的存储消耗。原版本都是不抗锯齿的。但是抗锯齿效果更柔和 renderTarget.texture = texture; //居然漏了一句,2022.10.9补 renderer.setRenderTarget(renderTarget); renderer.setRenderTarget(null); var o = renderer.properties.get(texture); o.__image__webglTextureCube = o.__webglTexture; //window.tex[r.id] = {texture:r, index:this.viewer.index } return renderTarget } getUploadQueueForPano(e) { var t = this.uploadQueues[e]; return t || (t = [], this.uploadQueues[e] = t), t } isTileUploaded(e) { return this.isPanoZoomed(e.panoId) ? e.zoomUploaded : e.uploaded } setUploaded(e, t) { this.isPanoZoomed(e.panoId) ? e.zoomUploaded = t : e.uploaded = t; } queueTileUpload(e, t, i) { var n = this.getActiveRenderTargetDescriptor(e.panoId); if (this.isRenderTargetDescriptorValid(n) && e.downloaded && !this.isTileUploaded(e) && (!e.uploadQueued || i) && (!(e.panoSize > this.qualityManager.getMaxNavPanoSize())|| this.zoomingActive)) { var r = this.getUploadQueueForPano(e.panoId); //console.log(window.sceneName, 'queueTileUpload: ', e.panoId, e.tileIndex, i) if(i){ //console.log('512下载好了直接uploadTile') this.uploadTile(e, !1);//提交后该tile直接loaded了,无需等待 }else { if(this.shoulPushToFrontOfQueue(e)){//如果是512的优先 this.forceQueue.push(e); }else { if(t && this.direction){ TilePrioritizer.insertSortedPanoTile(r, e, n.pano, this.direction); }else r.push(e); } e.uploadQueued = !0; this.uploadInterval || this.uploadIntervalCancelled || this.refreshUploadInterval(0); } } } shoulPushToFrontOfQueue(e) { return 0 === TileTree.getLevelCountForSize(TileUtils.TILE_SIZE, e.panoSize) } getTopUploadQueue() { for (var e = null, t = null, i = S.Base; i <= S.Remaining; i++) for (var n = 0; n < this.activePanos.length; n++) if (e = this.activePanos[n], t = this.getUploadQueueForPano(e.id), t.length > 0) switch (i) { case S.Base: if (0 === t[0].level) return t; break; case S.Remaining: return t } return null } peekNextFromUploadQueue() { if (this.forceQueue.length > 0) return this.forceQueue[0]; var e = this.getTopUploadQueue(); return e && e.length > 0 ? e[0] : null } clearAllQueuedUploads() { this.clearAllUploadQueues(null, 0); } clearAllQueuedUploadsForPano(e) { this.clearAllUploadQueues(e, 0); } clearAllUploadQueues(e, t) { if (e) this.clearUploadQueue(this.getUploadQueueForPano(e), t), this.clearUploadQueue(this.forceQueue, t, e); else { for (var i = 0; i < this.activePanos.length; i++) { var n = this.activePanos[i]; this.clearUploadQueue(this.getUploadQueueForPano(n.id), t); } this.clearUploadQueue(this.forceQueue, t); } } clearUploadQueue(e, t, i) { void 0 !== t && null !== t || (t = 0); for (var n = 0; n < e.length;) { var r = e[n]; (!i || i && i === r.tile.panoId) && r.level >= t ? (r.uploadQueued = !1, e.splice(n, 1)) : n++; } //若报错, r.tile.panoId改为 r.panoId } updateUploadQueue(maxNPF,maxPF/* e, t */) {//参数是 maxNonBaseUploadsPerFrame and maxBaseUploadsPerFrame, 优先上传512 //maxNPF || (maxNPF = 1); for (var i = 0, n = 0;;) { //let old = this.forceQueue.slice(0) if (n >= maxPF || i >= maxNPF) break; var r = this.getNextFromUploadQueue(); if (!r) break; //r.panoSize <2048 && console.log('panoId', r.panoId, 'panoSize', r.panoSize , old) 0 !== r.level ? i++ : n++; if (!(r.panoSize > this.qualityManager.getMaxNavPanoSize()) || this.zoomingActive) { var o = this.getActiveRenderTargetDescriptor(r.panoId); this.isRenderTargetDescriptorValid(o) && this.uploadTile(r, r.forceUpload); } } } updateDirection(e) { if (e = e || this.direction) { this.direction = e; for (var t = 0; t < this.activePanos.length; t++) { var i = this.activePanos[t], n = this.getUploadQueueForPano(i.id); TilePrioritizer.sortPanoTiles(n, i, this.direction); } } } anyUploaded(e) { if (!e) return !1; if (e.tile && this.isTileUploaded(e.tile)) return !0; if (e.children) for (var t = 0; t < e.children.length; t++) { var i = e.children[t]; if (this.anyUploaded(i)) return !0 } return !1 } setNodeCovered(e, t) { this.isPanoZoomed(e.tile.panoId) ? e.zoomCovered = t : e.covered = t; } isNodeCovered(e) { return !!e && (this.isPanoZoomed(e.tile.panoId) ? e.zoomCovered : e.covered) } addCoverageForNode(e) { if (this.setNodeCovered(e, !0), e.parent && e.covered) { var t = e.parent; this.nodeSubcovered(t) && this.addCoverageForNode(t, !0); } } calcFullCoverage(e) { var t = !1; if (e.children) for (var i = 0; i < e.children.length; i++) { var n = e.children[i]; t = t || this.calcFullCoverage(n); } e.covered = e.tile.uploaded || t; } nodeSubcovered(e) { if (!e.children) return !1; for (var t = 0; t < e.children.length; t++) if (!e.children[t] || !this.isNodeCovered(e.children[t])) return !1; return !0 } resetPanoDescriptor(e) { this.getPanoDescriptor(e); } getPanoDescriptor(e) { var t = this.panoDescriptors[e]; return t || (t = {}, this.panoDescriptors[e] = t), t } resetPanoLODDescriptors(e) { var t = this.getPanoLODDescriptors(e); for (var i in t) if (t.hasOwnProperty(i)) { var n = t[i]; n.uploadCount = 0, n.uploadAttempts = 0; n.uploaded = []; } } getPanoLODDescriptor(e, t) { var i = this.getPanoLODDescriptors(e), n = i[t]; return n || (n = { uploadCount: 0, uploadAttempts: 0, uploaded:[],//add }, i[t] = n), n } getPanoLODDescriptors(e) { var t = this.panoLODDescriptors[e]; return t || (t = {}, this.panoLODDescriptors[e] = t), t } onTileDownloaded(o) { var e = o.desc; var t = TileTree.getLevelCountForSize(TileUtils.TILE_SIZE, e.panoSize), i = this.getTileDirectoryEntry(e.panoId, e.face, t, e.faceTileIndex); i.downloaded = !0; i.image = e.image; i.panoSize = e.panoSize; i.tileX = e.tileX; i.tileY = e.tileY; i.totalTiles = e.totalTiles; i.tileIndex = e.tileIndex; i.faceTileIndex = e.faceTileIndex; i.face = e.face; i.cubeFace = TileUtils.mapFaceToCubemapFace(e.face); i.panoId = e.panoId; i.tileSize = e.tileSize; i.direction = (new Vector3).copy(e.direction); i.node = null; i.level = TileTree.getLevelCountForSize(TileUtils.TILE_SIZE, i.panoSize); if (this.isPanoActive(i.panoId)) { var n = this.getTileTree(i.panoId, i.face); var r = n.getSubNode(i.panoSize, i.tileX, i.tileY); this.linkTileAndNode(i, r); this.queueTileUpload(i, !0); } } getTileDirectoryEntry(panoId, t, i, n) { var r = this.tileDirectory[panoId]; r || (r = {}, this.tileDirectory[panoId] = r); var o = 16384 * t + 1024 * i + n, //t:4096级别 a = r[o]; return a || (a = { downloaded: !1, uploaded: !1, zoomUploaded: !1 }, r[o] = a), a._key = panoId + ":" + t + ":" + i + ":" + n,//panoId : face : level : faceTileIndex a._tileKey = o, a } setZoomingActive(active, pano, i) {//设置当前正在zoom的pano this.zoomPanoRenderingDisabled || active === this.zoomingActive && this.zoomPanoId === pano.id || (this.zoomingActive = active, this.zoomPanoId = pano.id, this.zoomingActive && (this.zoomPanoId !== pano.id || i) && this.updateZoomedPanoFromBase(pano)); } updateZoomedPanoFromBase(pano) {//因更换pano所以将pano的rendertarget渲染到panoRenderer的zoomRenderTarget上 if (!this.zoomPanoRenderingDisabled) { var t = this.getActiveRenderTargetDescriptor(pano.id); if (t && t.renderTarget ) { if(this.zoomRenderTarget){ var i = Math.min(this.qualityManager.maxRenderTargetSize, this.qualityManager.getMaxZoomPanoSize()), //change n = t.renderTarget, r = t.size; this.copyCubeMap(n.texture, this.zoomRenderTarget, r, r, i, i); } this.copyBaseRenderStatusToZoomed(pano.id); } } } add(e) { this.M.push(e); } initDescriptor(size) { var t = createDescriptor(); t.inUse = !0; t.size = size; this.add(t); return t } activeDescripor(e) { for (var t = 0; t < this.M.length; t++) { var i = this.M[t]; if (!i.inUse && i.size === e){ i.inUse = !0; return i } } return null } deactiveDescripor(e) { for (var t = 0; t < this.M.length; t++) { var i = this.M[t]; if (i.renderTarget === e){ i.inUse = !1; return !0 } } return !1 } enableHighQuality(e){//xzw add 如果最多只要2k图的话enableUltraHighQualityMode替换成这个 if (!this.qualityManager.highQualityModeStarted) { this.setupZoomRenderTarget(); e(); //this.qualityManager.updateHighResolutionSettings(e)////////? this.qualityManager.highQualityModeStarted = true; } } linkTileAndNode(e, t) { t.tile = e, e.node = t; } linkAllTilesAndNodes(e) { var t = function (t, i, n, r, o) { var a = this.getTileDirectoryEntry(e.id, i, r, o); this.linkTileAndNode(a, n); }; for (var i = 0; i < TileUtils.FACES_PER_PANO; i++) { var n = this.getTileTree(e.id, i); n.breadthFirst({ callback: t.bind(this, n, i) }); } } //--------------sceneRenderer initSizedTexture2D(e, t, i) { var renderer = this.viewer.renderer, o = renderer.getContext(), a = renderer.state, s = new Texture(null); s.flipY = !1, i !== !0 && (i = !1), s.generateMipmaps = i; var l = renderer.paramThreeToGL(s.format), c = renderer.paramThreeToGL(s.type), h = renderer.properties.get(s), u = o.createTexture(); a.bindTexture(o.TEXTURE_2D, u), o.pixelStorei(o.UNPACK_FLIP_Y_WEBGL, s.flipY), o.texImage2D(o.TEXTURE_2D, 0, l, e, e, 0, l, c, null), s.wrapS = t, s.wrapT = t; var d = renderer.paramThreeToGL(t); return o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_S, d), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_WRAP_T, d), i ? (s.magFilter = LinearFilter, s.minFilter = LinearMipMapLinearFilter, o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MAG_FILTER, o.LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MIN_FILTER, o.LINEAR_MIPMAP_NEAREST), o.generateMipmap(o.TEXTURE_2D)) : (s.magFilter = LinearFilter, s.minFilter = LinearFilter, o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MAG_FILTER, o.LINEAR), o.texParameteri(o.TEXTURE_2D, o.TEXTURE_MIN_FILTER, o.LINEAR)), a.bindTexture(o.TEXTURE_2D, null), h.__webglTexture = u, s } deallocateCubeTexture(e) { var t = this.viewer.renderer, i = t.getContext(), renderer = t.properties.get(e); i.deleteTexture(renderer.__image__webglTextureCube); } uploadTexture2D(img, tex, startX, startY, width, height) { var renderer = this.viewer.renderer, gl = renderer.getContext(), webglState = renderer.state, c = renderer.properties.get(tex); webglState.bindTexture(gl.TEXTURE_2D, c.__webglTexture), gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, tex.flipY), gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, tex.premultiplyAlpha), gl.pixelStorei(gl.UNPACK_ALIGNMENT, tex.unpackAlignment), gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, renderer.paramThreeToGL(tex.wrapS)), gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, renderer.paramThreeToGL(tex.wrapT)), gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, renderer.paramThreeToGL(tex.magFilter)), gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, renderer.paramThreeToGL(tex.minFilter)), gl.texSubImage2D(gl.TEXTURE_2D, 0, startX, startY, gl.RGBA, gl.UNSIGNED_BYTE, img), tex.generateMipmaps && gl.generateMipmap(gl.TEXTURE_2D), webglState.bindTexture(gl.TEXTURE_2D, null); } getCubeOrientationForCubeFace(e, t) { switch (e) { case GLCubeFaces$2.GL_TEXTURE_CUBE_MAP_POSITIVE_X: t.set(0, -Math.PI / 2, 0); break; case GLCubeFaces$2.GL_TEXTURE_CUBE_MAP_NEGATIVE_X: t.set(0, Math.PI / 2, 0); break; case GLCubeFaces$2.GL_TEXTURE_CUBE_MAP_POSITIVE_Y: t.set(Math.PI / 2, Math.PI, 0); break; case GLCubeFaces$2.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: t.set(-Math.PI / 2, Math.PI, 0); break; case GLCubeFaces$2.GL_TEXTURE_CUBE_MAP_POSITIVE_Z: t.set(0, -Math.PI, 0); break; case GLCubeFaces$2.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: t.set(0, 0, 0); } } //dispose所有cubeRenderTarget disposeIdelTargets() { let disposeCount = 0; for (var t = 0; t < this.M.length; t++) { var i = this.M[t]; if (!i.inUse) { disposeCount++; i.renderTarget.dispose(); } } //console.log('disposeIdelTargets ', disposeCount, 'of', this.M.length) } } PanoRenderer.prototype.setupZoomRenderTarget = function(){ var targets = {}; return function(){ if(this.qualityManager.maxRenderTargetSize == 2048 && this.qualityManager.getMaxNavPanoSize()==2048)return; //不使用zoomTarget 直接用pano的tiledPanoRenderTarget,防崩溃 if (this.qualityManager.getMaxZoomPanoSize() >= this.qualityManager.getMaxNavPanoSize() ) { //部分手机2k时copyCubeMap会重载 , 所以如果没有超出当前分辨率,就不使用zoomRenderTarget。但在微信依旧会重载,只是优化了些,safari几乎不会。 if (this.zoomRenderTarget && this.zoomRenderTarget.width === this.qualityManager.getMaxZoomPanoSize()) return; var e = this.zoomRenderTarget; var size = this.qualityManager.getMaxZoomPanoSize(); if(size > this.qualityManager.maxRenderTargetSize){ return } if(targets[size]){ this.zoomRenderTarget = targets[size]; }else { this.zoomRenderTarget = this.initTiledPano(size, false ); //放大后不使用抗锯齿,否则消 targets[size] = this.zoomRenderTarget; } if (e) {//将旧的zoomRenderTarget渲染到新zoomRenderTarget上 var t = e.width, i = this.zoomRenderTarget.width; this.copyCubeMap(e.texture, this.zoomRenderTarget, t, t, i, i), e.texture.dispose(), e.texture.loaded = !1, e.texture.version = 0, this.deallocateCubeTexture(e.texture), e.texture = null; } this.zoomPanoRenderingDisabled = !1; } else this.zoomPanoRenderingDisabled = !0; } }(); PanoRenderer.prototype.updateActivePanos = function () { var e = []; return function (t, i) { e.length = 0; for (var n = 0; n < this.activePanos.length; n++) { t && e.length === i && e.push(t); var r = this.activePanos[n], o = this.getActiveRenderTargetDescriptor(r.id); t && r.id === t.id || !this.isRenderTargetDescriptorValid(o) || e.push(r); } t && i >= e.length && e.push(t), this.activePanos.length = 0, this.activePanos.push.apply(this.activePanos, e); } }(); PanoRenderer.prototype.renderPanoTiles = function () { var e = []; return function (panoId, i, n, r) { this.zoomRenderTarget && this.zoomRenderTarget.width === this.qualityManager.getMaxZoomPanoSize() || this.zoomPanoRenderingDisabled || this.setupZoomRenderTarget(),//如果ZoomRenderTarget大小需要变动就重新创建 i = i || this.direction || Vectors.FORWARD; // [{1:Vectors.FORWARD}]? var o = this.getActiveRenderTargetDescriptor(panoId); if (!this.isRenderTargetDescriptorValid(o)) console.error("PanoRenderer.renderPanoTiles() -> Cannot render to a pano that is not activated."); for (var a = 0; a < TileUtils.FACES_PER_PANO; a++) { var s = this.getTileTree(panoId, a); e.length = 0; s.breadthFirst({//获取该面所有node 85个 = 1+4+16+64 saveVisited: e }); for (var l = 0; l < e.length; l++) { var c = e[l]; this.queueTileUpload(c.tile, !1, r || 0 === l && n);// 第0个是512的,会直接uploadTile } } this.updateDirection(i); } }(); PanoRenderer.prototype.getNextFromUploadQueue = function () { var e = function (e) { var t = e.shift(); return t.uploadQueued = !1, t }; return function () { if (this.forceQueue.length > 0) return e(this.forceQueue); var t = this.getTopUploadQueue(); return t && t.length > 0 ? e(t) : null } }(); /* PanoRenderer.prototype.refreshUploadInterval = function () { var e = null; return function (t) { this.uploadIntervalCancelled || (e || (e = upload.bind(this)), null !== t && void 0 !== t || (t = w), b || (t = _), this.uploadInterval = window.setTimeout(e, t), this.uploadIntervalDelay = t) } }() PanoRenderer.prototype.update = function () { var e = performance.now(), t = 0; return function () { this.uploadIntervalCancelled = !0; window.clearTimeout(this.uploadInterval); this.uploadInterval = null; var i = performance.now() - e; //!(i > w || 0 === t) || !this.overlayTilesLoaded && this.usingTileOverlay || (this.updateUploadQueue(this.maxNonBaseUploadsPerFrame, this.maxBaseUploadsPerFrame), //e = performance.now()), if (!(i > w || 0 === t) || !this.overlayTilesLoaded && this.usingTileOverlay) {} else { this.updateUploadQueue(this.maxNonBaseUploadsPerFrame, this.maxBaseUploadsPerFrame); e = performance.now(); } t++; } }() */ PanoRenderer.prototype.update = function () { //this.uploadIntervalCancelled = true ; //不使用setTimeout,而是在sceneRenderer每帧都update if(viewer.images360.latestRequestMode == 'showPanos'){ let maxNonBaseUploadsPerFrame = viewer.lastFrameChanged ? Common.getBestCount('maxStandard', 0, 2, 1, 10 /* ,true */ ) : 2; // 这是每帧uploadTile非512的瓦片tex的数量。 手机在前进时可能会总是0,直到到漫游点后为2 let maxBaseUploadsPerFrame = viewer.lastFrameChanged ? Common.getBestCount('maxBase',1,6, 1, 14 /* ,true */ ) : 6; //this.maxBaseUploadsPerFrame //原先6. 但持续前进过程中会请求加载下一个漫游图,一次加6张会卡的。 this.updateUploadQueue(maxNonBaseUploadsPerFrame, maxBaseUploadsPerFrame); //注:静止时看不出卡顿所以全速加载 } }; /* function upload() { if (!this.uploadIntervalCancelled) { if (this.overlayTilesLoaded || !this.usingTileOverlay) { b = !0 let maxNonBaseUploadsPerFrame = viewer.mainViewport.view.isFlying() ? 1 : this.maxNonBaseUploadsPerFrame //原先2。这是每帧uploadTile非512的瓦片tex的数量。之前的2太卡了,降为1。(检测卡顿方法:在一个pano点旋转至所有2048的tile都加载完,然后之后到这个点看看卡不卡。因为该点tiles都下载完了所以会在飞过来时陆续都加载,所以容易卡) this.updateUploadQueue(maxNonBaseUploadsPerFrame, this.maxBaseUploadsPerFrame) let time = viewer.mainViewport.view.isFlying() ? 60 : w //add 飞行有时候会卡,增长间隔 this.peekNextFromUploadQueue() ? this.refreshUploadInterval(time) : this.uploadInterval = null //定时下一次更新 } else { this.refreshUploadInterval(this.uploadIntervalDelay) } } } */ PanoRenderer.prototype.uploadTile = function () {//重写 var collection = {}, overlayStyle = config$1.tiling.overlayStyle; var failHistory = {}; return function (info, n) { var id = info.panoId, img = info.image, tileSize = info.tileSize, panoSize = info.panoSize, tileIndex = info.tileIndex, totalTiles = info.totalTiles, tileX = info.tileX, tileY = info.tileY, p = !0, g = !1, ignore = false, //add LodDescripor = (this.getPanoDescriptor(id), this.getPanoLODDescriptor(id, panoSize)), activeDescripor = this.getActiveRenderTargetDescriptor(id), renderTarget = activeDescripor.renderTarget, size = activeDescripor.size;//当前要渲染的面的分辨率,也就是MaxNavPanoSize if (this.isPanoZoomed(id) && this.zoomRenderTarget) { renderTarget = this.zoomRenderTarget; size = this.zoomRenderTarget.width; //this.qualityManager.getMaxZoomPanoSize(); //放大后可能2048或4096 } //console.log(window.sceneName, 'uploadTile ', id, tileIndex) let done = ()=>{ if(!LodDescripor.uploaded.includes(tileIndex)){//已经upload过(本来这时候直接返回,但发现缩放后这不会归零,导致清晰度不更新,所以还是redraw且emit吧) //console.log('try to reupload and return',tileIndex) LodDescripor.uploaded.push(tileIndex); LodDescripor.uploadCount++; } this.dispatchEvent({type:PanoRendererEvents$1.TileRenderSuccess, id, panoSize, tileIndex, totalTiles}); LodDescripor.uploadCount === totalTiles && this.dispatchEvent({type:PanoRendererEvents$1.PanoRenderComplete, id, panoSize, totalTiles, updateFullComplete:true}); this.setUploaded(info, !0); this.addCoverageForNode(info.node); }; {//已经uploadTile过了不再uploadTile if(!this.isRenderTargetDescriptorValid(activeDescripor)){ p = !1; g = !1; } if(!n){ this.anyUploaded(info.node) && (p = !1, g = !0,ignore = true ); //包括子集也uploadTile了 this.isTileUploaded(info) && (p = !1, g = !1,ignore = true ); //当前tile uploadTile了 } } if (p) { /*if(failHistory[id+':'+ panoSize+ ':' +tileIndex]){ console.log('uploadTile retry',id, panoSize, tileIndex) } console.log('uploadTile 成功', id, panoSize, tileIndex) */ var C = tileX * tileSize, I = tileY * tileSize, E = tileSize / panoSize * size, // tile在renderTarget上渲染出的宽度 b = C / panoSize * size, // tile在renderTarget上渲染的startX w = I / panoSize * size; // tile在renderTarget上渲染的startY if(panoSize > this.qualityManager.maxRenderTargetSize ){ //4096 改 /* var tex = this.initSizedTexture2D(tileSize, THREE.ClampToEdgeWrapping); var loaded = this.viewer.images360.isHighMapLoaded(info.cubeFace, tileX,tileY) */ this.viewer.images360.getHighImage(img, info.cubeFace, tileX, tileY); }else { collection[tileSize] || (collection[tileSize] = this.initSizedTexture2D(tileSize, ClampToEdgeWrapping)); var tex = collection[tileSize]; this.uploadTexture2D(img, tex, 0, 0, tileSize, tileSize);//只替换tex对应的img,不新建 if(panoSize > this.qualityManager.maxRenderTargetSize){ loaded || this.viewer.images360.updateHighMap(tex, info.cubeFace, tileX,tileY); }else { if (1 === overlayStyle || 2 === overlayStyle) { var T = 1 === overlayStyle ? this.overlayTilesBasic : this.overlayTilesEnhanced; this.renderToCubeMap(tex, renderTarget, tileSize, tileSize, 0, 0, tileSize, tileSize, b, w, E, E, info.cubeFace); this.renderToCubeMap(T[panoSize], renderTarget, tileSize, tileSize, 0, 0, tileSize, tileSize, b, w, E, E, info.cubeFace, NormalBlending, !0, .5); } else { this.renderToCubeMap(tex, renderTarget, tileSize, tileSize, 0, 0, tileSize, tileSize, b, w, E, E, info.cubeFace); } } } done(); }else if(ignore){ //console.log('finish because anyUploaded',id,panoSize,tileIndex) done(); //改: 如果因为这部分更高清的贴图已加载所以才不绘制的话,直接完成 }else { //console.log('uploadTile 失败', id, panoSize, tileIndex) if(panoSize == 512){ //console.log("!!!!!!!!!!!!!") } failHistory[id+':'+ panoSize+ ':' +tileIndex] = true; this.setUploaded(info, !1); } info.uploadAttempted || (LodDescripor.uploadAttempts++, this.dispatchEvent({type:PanoRendererEvents$1.TileUploadAttempted, id, panoSize, tileIndex, totalTiles})), info.uploadAttempted = !0; LodDescripor.uploadAttempts === totalTiles && this.dispatchEvent({type:PanoRendererEvents$1.UploadAttemptedForAllTiles, id, panoSize, totalTiles}); return g; } }(); /* 注:tileY的方向同UV,从下到上 renderToCubeMap里的画布or镜头的xy范围是-0.5到0.5 */ PanoRenderer.prototype.renderToCubeMap = function() { var inited = !1, scene = null, camera = null, material = null, geo = null, plane = null, l = 1; return function(texture, renderTarget, tileWidth, tileHeight, startXinTile, startYinTile, widthinTile, heightinTile, startX, startY, width, height, cubeFace, E, b, w) { var renderer = this.viewer.renderer; inited || (camera = new OrthographicCamera(l / -2, l / 2, l / 2, l / -2, -200, 200), camera.position.z = 150, scene = new Scene, scene.add(camera), material = new BasicMaterial({ depthWrite: !1, depthTest: !1, side: 0,}), geo = new PlaneBufferGeometry(l, l), plane = new Mesh(geo, material), plane.position.z = 0, scene.add(plane), inited = !0); var uv = geo.getAttribute("uv"); uv.setDynamic(!0), //setUsage uv.needsUpdate = !0; var uvArr = uv.array, S = startXinTile / tileWidth, //uv这几个值基本是固定的startXinTile:0,startYinTile:0,widthinTile:512,widthinTile:512,tileWidth:512,tileHeight:512 也就是说uv不会变、每张tile的有效范围是100% M = startYinTile / tileHeight, R = widthinTile / tileWidth, P = heightinTile / tileHeight; uvArr[0] = S, uvArr[1] = M + P, uvArr[2] = S + R, uvArr[3] = M + P, uvArr[4] = S, uvArr[5] = M, uvArr[6] = S + R, uvArr[7] = M; //修改posistion,使该plane只占据需要绘制的部分。类似拼图。 //startX startY width height 都是在画布上的大小,比如画布大小为2048*2048,此tile为16分之一,tileX是1,tileY是1,则startX=2048/4,startY=2048/4 var pos = geo.getAttribute("position"); pos.setDynamic(!0), pos.needsUpdate = !0; var posArr = pos.array, D = startX / renderTarget.width - l / 2 // 起始x , N = startY / renderTarget.height - l / 2 // 起始y , B = width / renderTarget.width // 宽 , F = height / renderTarget.height; // 高 posArr[0] = D, posArr[1] = N + F, posArr[3] = D + B, posArr[4] = N + F, posArr[6] = D, posArr[7] = N, posArr[9] = D + B, posArr[10] = N; //renderer.properties.get(scene); material.map = texture;//material.uniforms.map.value = texture; material.blending = E || NoBlending, material.transparent = !!b; void 0 !== w && null !== w || (w = 1), material.uniforms.opacity.value = w, // material.needUpdate = !0 //renderTarget.activeCubeFace = cubeFace, //0-5 应该是指定渲染h中的面 失效 /* renderer.setScissorTest(!0) //指定绘制区域,类似遮罩(相对于屏幕) renderer.setScissor(startX,startY,width,height) //加上这个会不会快一些,尤其是spherical //指定绘制视口位置和大小(相对于屏幕) */ renderTarget.viewport.set(0, 0, renderTarget.width, renderTarget.height); var V = renderer.autoClear; var oldTarget = renderer.getRenderTarget(); renderer.autoClear = !1; renderer.setRenderTarget(renderTarget, cubeFace); renderer.render(scene, camera/* , renderTarget, !1 */); renderer.setRenderTarget(oldTarget); renderer.autoClear = V; //renderer.setScissorTest(!1) /* this.renderer.render(scene, camera, this.planeTargets[cubeFace], !1),//针对有的场景app第一个点图加载不成功的问题 console.log(`图index ${cubeFace} , ${startX}, ${startY}, ${width}, ${height}`) this.targetList[cubeFace] || (this.targetList[cubeFace] = []) this.targetList[cubeFace].push([startX,startY,width,height])*/ } }(); /* * 将texture渲染到zoomRenderTarget上(目的是复制贴图到zoomRenderTarget) */ PanoRenderer.prototype.copyCubeMap = function() {//将texture渲染到zoomRenderTarget上 var inited = !1, scene = null, camera = null, material = null, geo = null, mesh = null, testCube = null, //add c = new Euler; return function(texture, renderTarget, tWidth, tHeight, rWidth, rHeight, m, v, A) { if(rWidth > this.qualityManager.maxRenderTargetSize) return; //add if (!inited) { var w = 2; camera = new OrthographicCamera(w / -2, w / 2, w / 2, w / -2, 0, 200), camera.position.set(0, 0, 0), scene = new Scene, scene.add(camera); material = new ShaderMaterial({ uniforms: { tDiffuse: { type: "t", value: null }, alpha: { type: "f", value: 1 } }, vertexShader: Shaders['copyCubeMap.vs'], fragmentShader:Shaders['copyCubeMap.fs'], depthWrite: !1, depthTest: !1, side: DoubleSide }); geo = new BoxGeometry(w, w, w), mesh = new Mesh(geo, material), scene.add(mesh); inited = !0; /* testCube = mesh.clone(); viewer.scene.scene.add(testCube); Potree.Utils.setObjectLayers(testCube, 'sceneObjects') */ } let autoClear = this.viewer.renderer.autoClear; let oldTarget = this.viewer.renderer.getRenderTarget(); this.viewer.renderer.autoClear = false; material.uniforms.tDiffuse.value = texture; material.blending = m || NoBlending; material.transparent = !!v; void 0 !== A && null !== A || (A = 1); material.uniforms.alpha.value = A; material.needUpdate = !0; for (var C = 0; C < 6; C++){ this.getCubeOrientationForCubeFace(C, c); mesh.rotation.copy(c); mesh.matrixWorldNeedsUpdate = !0; mesh.updateMatrixWorld(); renderTarget.viewport.set(0, 0, rWidth, rHeight); this.viewer.renderer.setRenderTarget(renderTarget, C ); //renderTarget.activeCubeFace = C//失效 this.viewer.renderer.render(scene, camera); } //console.warn('copyCubeMap' + rWidth) this.viewer.renderer.autoClear = autoClear; this.viewer.renderer.setRenderTarget(oldTarget); } }(); class DepthImageSampler extends EventDispatcher{ constructor(){ super(); var canvas = document.createElement("canvas"); this.canvas = canvas; this.context = canvas.getContext("2d", {willReadFrequently: true}); this.imgDatas = []; /* document.getElementsByTagName('body')[0].appendChild(canvas); canvas.style.position = 'fixed'; canvas.style.width = '1024px'; canvas.style.top = canvas.style.left = 0 canvas.style['z-index'] = 100 */ this.maxDataCount = browser.isMobile() ? 6 : 20; //手机会崩溃. 平均每张图为8M数据量(以200个点的园区为例,加载时间久一些后,总内存=700 + 每张图的8M * maxDataCount) this.maxNeighCount = browser.isMobile() ? 3 : 14; //包含在maxDataCount内的nearPanos最大个数.至少比maxDataCount少3个,留出空位给最近更新的pano this.nearPanos = []; } updateNearPanos(panos){ this.nearPanos = panos.slice(0,this.maxNeighCount); } changeImg(img, pano){ this.pano = pano; let item = this.imgDatas.find(p=>p.pano == pano); if(/* this.img == img || */item){ //最新的在末尾,所以换到末尾 let index = this.imgDatas.indexOf(item); this.imgDatas.splice(index,1); this.imgDatas.push(item); //console.log('重复使用',item.pano.id) return } Potree.timeCollect.depthSamChangeImg.start = true; //不过在刚启动时得到的用时较大 try{ viewer.addTimeMark('depthSamChangeImg','start'); this.canvas.width = img.width; this.canvas.height = img.height; this.context.drawImage(img, 0, 0); let o = this.context.getImageData(0, 0, img.width , img.height ); let data = o && o.data; //getImageData 1px时 : pc chrome 耗时0.01毫秒左右(排除第一次的50) , 但firefox: 4。但换贴图之后就多达5甚至几十 //console.log('changeImg',pano.id ) //this.img = img if(!data){ console.error('getImageData 得不到?!why!'); return false } if(this.imgDatas.length >= this.maxDataCount){ let old = this.imgDatas.find(e=>!this.nearPanos.includes(e.pano)); //console.log('推出',old.pano.id) this.imgDatas.splice(this.imgDatas.indexOf(old), 1);//推出使用时间最早的一个非nearPano } this.imgDatas.push({pano, data}); this.dispatchEvent({type:'changeImg',pano}); viewer.addTimeMark('depthSamChangeImg','end'); //耗时chrome 25ms,firefox: 38ms, iphoneX:33ms }catch(e){ console.error(e ); //内存不足 Failed to execute 'getImageData' on 'CanvasRenderingContext2D': Out of memory at ImageData creation return false } /* pano.depthData = {} for(let h=0; h0) pano.depthData[h+'|'+w] = depth } } */ } clearTexData(){ this.imgDatas.length = 0; } getDepth( UVx, UVy, useNeighIfZero) {//根据图片像素获取深度值 var x = Math.round(UVx * (this.canvas.width - 1)) , y = Math.round(UVy * (this.canvas.height - 1)); let imgData = this.imgDatas.find(p=>p.pano == this.pano); let get = (x,y,)=>{ if (!(x < 0 || y < 0 || x >= this.canvas.width || y >= this.canvas.height)){ let blockIndex = this.canvas.width * y + x; let color = imgData.data.slice(blockIndex*4, (blockIndex+1)*4); return color[1] + color[0] / 256 //为什么不是除以255见聊天记录.(验证过比点云的远一点点) } }; /* viewer.addTimeMark('depthSampler','start') var r = this.context.getImageData(x, y, 1, 1).data; //pc chrome 耗时0.01毫秒左右(排除第一次的50) , 但firefox: 4。但换贴图之后就多达5甚至几十 viewer.addTimeMark('depthSampler','end') */ let depth = get(x,y); if(!depth && useNeighIfZero){ //遇到过有的相隔很远且有阻挡的两个漫游点间居然depth为0,没有阻挡?但是周围点有四个非零。所以为了修正会飞到很远的点加个识别周围像素的depth 。 2024.3.19 let results = []; let d = 0, sum = 0, count = 0, maxResDis = 1; while(d++{return w+c},0) / results.length; //console.error('useNeighIfZero', results, depth) } //console.log('useNeighIfZero', results, depth) } return depth } sample( intersect, currentPano, onlyPos, useNeighIfZero ) {//通过和skybox的intersect得到真实的intersect的位置 if(!intersect)return let location = new Vector3; let normal; currentPano = currentPano || viewer.images360.currentPano; if(currentPano != this.currentPano || !this.imgDatas.find(p=>p.pano == currentPano) ){ if(!currentPano.depthTex/* || !currentPano.depthTex.image */) return //未加载 if(this.changeImg(currentPano.depthTex.image, currentPano) === false){ console.log('失败', currentPano.id); return false //失败 } this.currentPano = currentPano; } let origin = currentPano.position; let dir = intersect.dir || new Vector3().subVectors(intersect.point, origin).normalize(); //var uv = intersect.uv //let dirInPano = math.getNormalDir(dir, currentPano)//转化为考虑漫游点旋转的方向 let dirInPano = dir.clone().applyMatrix4(currentPano.panoMatrix2Inverse).normalize(); //转化为考虑漫游点旋转的方向 let uv = math.getUVfromDir(dirInPano);//转化为uv let distance = this.getDepth( uv.x, uv.y, useNeighIfZero); //viewer.addTimeMark(markName,'end') if (!distance){ const margin = 0.1; if(uv.y > 1-Potree.config.depthTexUVyLimit){//漫游点底部识别不到的区域,给一个地板高度 distance = (currentPano.floorPosition.z - origin.z - margin) / dir.z; location.copy(dir).multiplyScalar(distance).add(origin); let normal = new Vector3(0,0,1); return {location, normal, distance} }else if(uv.y < Potree.config.depthTexUVyLimit){ let ceilZ = currentPano.getCeilHeight(); if(ceilZ == Infinity)return !1 else { distance = (ceilZ - origin.z - margin) / dir.z; location.copy(dir).multiplyScalar(distance).add(origin); let normal = new Vector3(0,0,-1); return {location, normal, distance} } } //console.log('无穷远') return !1; //应该是天空或模型外 , 因为很少有漫游点的地方还拍不到地板 } //console.log('distance', distance, dirInPano.clone().multiplyScalar(distance)) location.copy(dir).multiplyScalar(distance).add(origin); if(!onlyPos){ var pL = this.getNearbyPoint(origin, uv, -1, 0) , pR = this.getNearbyPoint(origin, uv, 1, 0) , pB = this.getNearbyPoint(origin, uv, 0, -1) , pT = this.getNearbyPoint(origin, uv, 0, 1); normal = this.planeFit(dir,location, pL,pR,pB,pT ); } /* if(normal.x != normal.x ){ console.log('NAN', normal) var pL = this.getNearbyPoint(origin, uv, -1, 0) , pR = this.getNearbyPoint(origin, uv, 1, 0) , pB = this.getNearbyPoint(origin, uv, 0, -1) , pT = this.getNearbyPoint(origin, uv, 0, 1); } */ //console.log(location, normal, distance) return {location, normal, distance} } getNearbyPoint( origin, uv, x, y) { //获取附近的若干像素距离的点 let uv2 = uv.clone(); uv2.x += x/(this.canvas.width-1); uv2.x = this.clampUV(uv2.x); uv2.y += y/(this.canvas.height-1); uv2.y = this.clampUV(uv2.y); /* if(uv2.x < 0 || uv2.y < 0 || uv2.x > 1 || uv2.y > 1){ console.log('will nan') } */ let dir = math.getDirFromUV(uv2);//从uv获取到方向 dir.applyMatrix4(viewer.images360.currentPano.panoMatrix2); let depth = this.getDepth(uv2.x, uv2.y); /* if(Math.abs(depth - this.mainDepth) > 0.3){ console.log('Math.abs(depth - this.mainDepth) > 0.3') } */ //let dir = new THREE.Vector3().subVectors(intersect.point, origin).normalize() let position = new Vector3().copy(dir).multiplyScalar(depth).add(origin); //console.log('getNearbyPoint', uv2, depth, dir, position ) return position } clampUV(v){ return (v + 1) % 1; // 使输出在 0-1 } planeFit(dir, position, pL,pR,pB,pT ) {//求平均法线 let normal = new Vector3; let plane = new Plane; function addNormal(p1, p2) {//根据临接的四个点,分别求法线,然后法线相加能得到平均法线 if(!p1 || !p2)return plane.setFromCoplanarPoints(position, p1, p2); //console.log('normalSub', plane.normal) normal.addScaledVector(plane.normal, dir.dot(plane.normal) < 0 ? 1 : -1);//根据面的朝向判断加还是减 } addNormal(pL, pB); addNormal(pL, pT); addNormal(pR, pB); addNormal(pR, pT); if(0 !== normal.x || 0 !== normal.y || 0 !== normal.z){ normal.normalize(); //console.log(normal) return normal } /* 四个面拼成一个菱形 */ } } let {PanoSizeClass: PanoSizeClass$3,Vectors: Vectors$1,GLCubeFaces: GLCubeFaces$3, PanoramaEvents: PanoramaEvents$1} = Potree.defines; var rot90$1 = new Quaternion().setFromAxisAngle(new Vector3(0,0,1), Math.PI/2 ); //使用的是刚好适合全景图的,给cube贴图需要转90° let raycaster = new Raycaster(); //let currentlyHovered = null; let texLoader$4 = new TextureLoader(); let addLine = (origin, dir, len, color='#fff') => { var line1 = LineDraw.createLine([origin, origin.clone().add(dir.clone().multiplyScalar(len || 1))], { color }); //console.log(origin.toArray(), dir.toArray()) viewer.scene.scene.add(line1); return line1 }; let tileArr = []; let previousView = { controls: null, position: null, target: null, }; const HighMapCubeWidth = 1; const directionFactor = 400; //原先10,几乎只往距离近的走了;设置太大容易略过近处漫游点走向下坡,因为鼠标一般在地面,下坡的漫游点更有利 class Images360 extends EventDispatcher{ constructor(viewer ){ super(); this.viewer = viewer; this.panos = []; this.neighbourMap = {}; this.node = new Object3D(); this.node.name = 'ImagesNode'; this.cubePanos = []; this.fastTranMaskPass = new FastTranPass(viewer.renderer); { this.cube = new Mesh(new BoxBufferGeometry(1,1,1,1),new ModelTextureMaterial()); Potree.Utils.updateVisible(this.cube,'showSkybox', false ); this.cube.layers.set(Potree.config.renderLayers.skybox); this.cube.name = 'skyboxCube'; viewer.scene.scene.add(this.cube); } if(Potree.settings.testCube){ this.cubeTest = this.cube.clone(); this.cubeTest.material = new MeshBasicMaterial({ wireframe:true, color:'#FF3377', transparent:true, opacity:0.7, depthWrite:false, depthTest:false, }); viewer.scene.scene.add(this.cubeTest); this.cubeTest.visible = true; } this.flying_ = false; this.currentPano = null; this.mouseLastMoveTime = Date.now(); this.scrollZoomSpeed = 0.06; this.zoomLevel = 1; this.tileDownloader = new TileDownloader; this.qualityManager = new QualityManager; this.panoRenderer = new PanoRenderer(viewer, this.tileDownloader, this.qualityManager); this.basePanoSize = this.qualityManager.getPanoSize(PanoSizeClass$3.BASE); this.standardPanoSize = this.qualityManager.getPanoSize(PanoSizeClass$3.STANDARD); this.highPanoSize = this.qualityManager.getPanoSize(PanoSizeClass$3.HIGH); this.ultraHighPanoSize = this.qualityManager.getPanoSize(PanoSizeClass$3.ULTRAHIGH); this.tileDownloader.processPriorityQueue = !1; this.tileDownloader.tilePrioritizer = new TilePrioritizer(this.qualityManager, this.basePanoSize, this.standardPanoSize, this.highPanoSize, this.ultraHighPanoSize); {//高分辨率cube 放大 this.addHighMapCube(); viewer.addEventListener(PanoramaEvents$1.Enter,(e)=>{ this.setHighMap(e.newPano); }); viewer.addEventListener('panoSetZoom',(e)=>{ if(Potree.settings.displayMode == 'showPanos'){ e.zoomed ? this.showHighMap() : this.hideHighMap(); //add } }); } this.depthSampler = new DepthImageSampler(); viewer.fpControls.addEventListener('dollyStopCauseUnable',(e)=>{ if(/* e.hoverViewport != viewer.mainViewport || */!Potree.settings.zoom.enabled)return if(e.scale != void 0){//触屏 this.zoomBy(e.scale, e.pointer); }else {//滚轮 let zoom; if(e.delta > 0){ zoom = 1 + this.scrollZoomSpeed; }else { zoom = 1 - this.scrollZoomSpeed; } e.delta != 0 && this.zoomBy(zoom); } }); let click = (e) => {//不用"mouseup" 是因为 mouseup有drag object时也会触发 if(e.clickElement || Potree.settings.unableNavigate || this.flying || !e.isTouch && e.button != MOUSE.LEFT || e.drag && e.drag.object //拖拽结束时不算 || Potree.settings.editType == 'pano' && viewer.modules.PanoEditor.activeViewName != 'mainView' || Potree.settings.editType == 'merge' && !e.intersectPoint || viewer.inputHandler.hoveredElements[0] && viewer.inputHandler.hoveredElements[0].isModel && e.intersectPoint.distance > viewer.inputHandler.hoveredElements[0].distance ) return if(Potree.settings.editType != 'pano' && Potree.settings.editType != 'merge'){ if( e.hoverViewport == viewer.mapViewer.viewports[0]){ return viewer.mapViewer.dispatchEvent(e/* {type:'global_click',e } */) }else if(e.hoverViewport != viewer.mainViewport){ //如数据集校准其他viewport return } } if(!Potree.settings.dblToFocusPoint/* && this.currentPano */){//双击不会focus点云 或者 已经focusPano了 this.flyToPanoClosestToMouse(); } }; viewer.addEventListener('global_click' , click); viewer.addEventListener("global_mousemove", (e) => { if(!Potree.settings.unableNavigate && Potree.settings.ifShowMarker && e.hoverViewport == viewer.mainViewport){//如果不显示marker,就在点击时再更新 this.updateClosestPano(e.intersect); } }); this.addEventListener('markerHover',(e)=>{ this.updateClosestPano(e.pano, e.hovered); }); if(!Potree.settings.isOfficial){ this.domRoot = viewer.renderer.domElement.parentElement; let elUnfocus = $(""); elUnfocus.css({ position : "absolute", right : '25%', bottom: '20px', zIndex: "10000", fontSize:'1em', color:"black", display:'none', background:'rgba(255,255,255,0.8)', }); elUnfocus.on("click", () => this.unfocus()); this.elUnfocus = elUnfocus; this.domRoot.appendChild(elUnfocus[0]); if(Potree.settings.editType != 'merge'){ let elHide = $(""); elHide.css({ position : "absolute", right : '40%', bottom: '20px', zIndex: "10000", fontSize:'1em' ,color:"black", width : '100px', background:'rgba(255,255,255,0.8)', }); this.domRoot.appendChild(elHide[0]); elHide.on("click", (e) => { let visi = Potree.Utils.getObjVisiByReason(viewer.scene.pointclouds[0], 'force'); viewer.scene.pointclouds.forEach(e=>{ Potree.Utils.updateVisible(e, 'force', !visi); }); elHide.val(!visi ? "隐藏点云" : "显示点云"); }); } let elDisplayModel = $(""); elDisplayModel.css({ position : "absolute", right : '65%', bottom: '20px', zIndex: "10000", fontSize:'1em',color:"black", width : '100px', background:'rgba(255,255,255,0.8)', }); this.domRoot.appendChild(elDisplayModel[0]); elDisplayModel.on("click", (e) => { if(Potree.settings.displayMode == 'showPointCloud' && this.panos.length == 0)return Potree.settings.displayMode = Potree.settings.displayMode == 'showPointCloud' ? 'showPanos' : 'showPointCloud'; }); this.elDisplayModel = elDisplayModel; if(viewer.mapViewer){ let mapStyleBtn = $(""); mapStyleBtn.css({ position : "absolute", right : '50%', bottom: '20px', zIndex: "10000", fontSize:'1em' ,color:"black", width : '100px', background:'rgba(255,255,255,0.8)', }); this.domRoot.appendChild(mapStyleBtn[0]); let map = viewer.mapViewer.mapLayer.maps.find(e=>e.name == 'map'); mapStyleBtn.on("click", (e) => { map.switchStyle(map.style == 'satellite' ? 'standard' : 'satellite'); mapStyleBtn.val(map.style == 'satellite' ? '卫星' : '普通'); }); } } {//切换模式 let displayMode = ''; this.latestRequestMode = '';//因为可能延迟,所以记录下每次的请求模式,延迟后判断这个 Object.defineProperty(Potree.settings , "displayMode",{ get: function() { return displayMode }, set: (mode)=> { this.latestRequestMode = mode; console.warn('Request setMode: ' + mode); this.dispatchEvent({type:'requestMode',mode}); let config2; let config = Potree.config.displayMode[mode]; if(this.isAtPano() && !this.latestToPano){ config2 = config.atPano; }else { config2 = config.transition; } let changeTileDownload = ()=>{ if(config2.showSkybox || config2.showPoint && config2.pointUsePanoTex){ this.tileDownloader.start(); this.currentPano && this.currentPano.enter(); }else { this.tileDownloader.stop(); this.currentPano && this.currentPano.exit(); this.nextPano && (viewer.cancelLoad(this.nextPano)); } }; if(mode != displayMode){ let camera = viewer.scene.getActiveCamera(); if(mode == 'showPanos' && viewer.mainViewport.view.isFlying()){//飞完才能切换全景 let f = ()=>{ if(this.latestRequestMode == mode){//如果ui还是停在这个模式的话 Potree.settings.displayMode = mode; } viewer.mainViewport.view.removeEventListener('flyingDone', f); }; viewer.mainViewport.view.addEventListener('flyingDone', f); //once return } if(this.isAtPano() && !this.latestToPano){ config2 = config.atPano; }else { config2 = config.transition; if(mode == 'showPanos'){//自动飞入一个pano //要改成飞进最近的。。。 if(this.panos.length == 0)return //this.modeChanging = true //主要是因为到全景图不会立刻成功 let wait = (e)=>{ console.log('flyToPanoDone'); this.removeEventListener('flyToPanoDone',wait); setTimeout(()=>{ if(this.latestRequestMode == mode ){ Potree.settings.displayMode = mode; } },e.makeIt ? 1 : 50); }; this.addEventListener('flyToPanoDone',wait); //等待飞行完毕。flyToPano的callback可能不执行所以换这个。但也可能被cancel this.flyToPano({ pano: this.findNearestPano(), //dealDoneWhenCancel:true, /* callback: ()=>{ setTimeout(()=>{ //防止循环,所以延迟 if(this.latestRequestMode == mode ){ Potree.settings.displayMode = mode } },1) } */ }); return; }else { } } changeTileDownload(); if(config2.showSkybox || config2.pointUsePanoTex){ let wait = ( )=> { //console.log('waitdone') //if(e.pano && e.pano != this.currentPano)return //loadedDepthImg setTimeout( ()=>{ if(this.latestRequestMode == mode ){ Potree.settings.displayMode = mode; } },1); }; //this.updateDepthTex() if(this.checkAndWaitForPanoLoad(this.currentPano, this.basePanoSize, wait)){ //console.log('等待贴图加载2', this.currentPano.id) return } } viewer.scene.pointclouds.forEach(e=>{ Potree.Utils.updateVisible(e, 'displayMode', config2.showPoint, 2 ); }); if(config2.pointUsePanoTex){ viewer.scene.pointclouds.forEach(e=>{ e.material.setProjectedPanos(this.currentPano,this.currentPano, 1); }); }else { viewer.scene.pointclouds.forEach(e=>{ e.material.stopProjectedPanos(); }); } Potree.Utils.updateVisible(this.cube,'showSkybox',config2.showSkybox );// this.cube.visible = config2.showSkybox //this.cube.visible = config.atPano.showSkybox if(this.cube.visible){ //this.cube.material.setProjectedPanos(this.currentPano, this.currentPano, 1) this.setProjectedPanos({ progress:1, ifSkybox: true, ifPointcloud : false, easeInOutRatio : 0, pano0:this.currentPano, pano1:this.currentPano }); }else { this.smoothZoomTo(1); this.resetHighMap(); this.hideHighMap(); } /* viewer.dispatchEvent({ type: "enableChangePos", canLeavePano : config.canLeavePano , viewport: }) */ //viewer.mainViewport.unableChangePos = !config.canLeavePano displayMode = mode; if(mode == 'showPanos'){ camera.far = viewer.farWhenShowPano; //修改far Potree.settings.pointDensity = 'panorama'; if(Potree.config.displayMode.showPanos.transition.pointUsePanoTex){ viewer.scene.pointclouds.forEach(e=>{ e.material.pointSizeType = 'FIXED'; }); } this.updateCube(this.currentPano); }else { if(camera.limitFar) camera.far = Potree.settings.cameraFar;//修改far Potree.settings.pointDensity = Potree.settings.UserPointDensity; //Potree.sdk && Potree.sdk.scene.changePointOpacity() if(Potree.config.displayMode.showPanos.transition.pointUsePanoTex){ viewer.scene.pointclouds.forEach(e=>{ e.material.pointSizeType = Potree.config.material.pointSizeType; }); } } camera.updateProjectionMatrix(); if(this.elDisplayModel){ this.elDisplayModel.val( mode == 'showPointCloud' ? ">>全景" : '>>点云'); } /* this.panos.forEach(e=>{ Potree.Utils.updateVisible(e, 'modeIsShowPanos', mode == 'showPanos', 1, mode == 'showPanos' ? 'add':'cancel') // }) */ this.dispatchEvent({type:'endChangeMode',mode}); console.log('setModeSuccess: ' + mode); }else { changeTileDownload(); //this.dispatchEvent({type:'endChangeMode',mode}) } } }); Potree.settings.displayMode = 'showPointCloud'; }// 切换模式 end {// let currentPano = null; Object.defineProperty(this , "currentPano",{ get: function() { return currentPano }, set: function(e) { if(e != currentPano){ //console.log('set currentPano ', e.id) currentPano && currentPano.exit(); e && e.enter(); currentPano = e; } } }); } {//是否显示marker let ifShowMarker = true; Object.defineProperty(Potree.settings, "ifShowMarker",{ get: function() { return ifShowMarker }, set: (show)=>{ show = !!show; if(show != ifShowMarker){ this.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano, 'ifShowMarker', show, 1 ); }); //this.emit('markersDisplayChange', show) ifShowMarker = show; viewer.dispatchEvent('showMarkerChanged'); viewer.dispatchEvent('content_changed'); } } }); } viewer.addEventListener("update", () => { this.update(viewer); }); //viewer.inputHandler.addInputListener(this); var keys = { FORWARD: ['W'.charCodeAt(0), 38], BACKWARD: ['S'.charCodeAt(0), 40], LEFT: ['A'.charCodeAt(0), 37], RIGHT: ['D'.charCodeAt(0), 39], }; viewer.inputHandler.addEventListener('keydown',(e)=>{ if(Potree.settings.displayMode == 'showPanos'){ for(let i in keys){ if(keys[i].some(a => a == e.keyCode)){ switch(i){ case 'FORWARD': this.flyLocalDirection(Vectors$1.FORWARD.clone()); break; case 'BACKWARD': this.flyLocalDirection(Vectors$1.BACK.clone()); break; case 'LEFT': this.flyLocalDirection(Vectors$1.LEFT.clone()); break; case 'RIGHT': this.flyLocalDirection(Vectors$1.RIGHT.clone()); break; } break; } } } }); }; updateDepthTex(pano){ if(this.currentPano != pano || !pano.depthTex)return //this.depthSampler.changeImg(pano.depthTex.image); //pick sampler要飞到了才能切换图,而skybox贴图是随着全景图切换而切换的 this.cube.material.updateDepthTex(pano); //确保一下 } findNearestPano(pos){ pos = pos ? new Vector3().copy(pos) : this.position; let result = Common.sortByScore(this.panos,[Images360.filters.isEnabled()],[e=>-e.position.distanceTo(pos)]); let pano = result[0] && result[0].item; return pano } /* set flying(v){//正在飞向pano this.flying_ = !!v //console.log('this.flying_ ', !!v ) //this.emit('flying', this.flying_) let config = Potree.config.displayMode[Potree.settings.displayMode] viewer.mainViewport.unableChangePos = !config.canLeavePano || !!v } get flying(){ return this.flying_ } */ flyLocalDirection(dir) { var direction = this.getDirection(dir), option1 = 1 === dir.y ? .4 : .75, option2 = 1 === Math.abs(dir.x); return this.flyDirection(direction, option1, option2, true) } getDirection(e) { if(!e){ return viewer.scene.view.direction }else { return e = e ? e : (new Vector3).copy(Vectors$1.FORWARD), e.applyQuaternion(viewer.mainViewport.camera.quaternion) } } get position(){ return this.viewer.scene.view.position.clone() } isAtPano(precision){//是否在某个漫游点上 if(precision){ return this.currentPano && math.closeTo(viewer.scene.view.position, this.currentPano.position, precision) } return this.currentPano && viewer.scene.view.position.equals(this.currentPano.position) } updateProjectedPanos(){//更新材质贴图 //console.warn('updateProjectedPanos') this.projectedPano0 && this.projectedPano1 && this.setProjectedPanos({pano0:this.projectedPano0, pano1:this.projectedPano1}); } setProjectedPanos(o={}){//设置cube和点云的材质贴图 this.cube.material.setProjectedPanos(o.pano0, o.pano1, o.progress); if(o.ifPointcloud){ viewer.scene.pointclouds.forEach(e=>{ e.material.setProjectedPanos(o.pano0, o.pano1, o.progress, o.easeInOutRatio); }); } //console.warn('setProjectedPanos ', o.pano0.id , o.pano1.id) this.projectedPano0 = o.pano0; this.projectedPano1 = o.pano1; } cancelFlyToPano(toPano){//取消当前已有的飞行准备,前提是相机还未移动 if(viewer.mainViewport.view.isFlying() || toPano && this.latestToPano != toPano)return //Potree.Log('cancelFlyToPano', this.latestToPano && this.latestToPano.pano.id) this.nextPano = null; this.latestToPano = null; } flyToPano(toPano) { //飞向漫游点 if(!toPano)return if(typeof toPano == 'number')toPano = this.panos[toPano]; if(toPano instanceof Panorama){ toPano = {pano: toPano}; } let done = (makeIt, disturb)=>{ //console.log('flyToPano done ', toPano.pano.id, makeIt, disturb ) if(makeIt || disturb) { // disturb已经开始飞行但中途取消 toPano.callback && toPano.callback(makeIt); //this.flying = false this.cancelFlyToPano(toPano); this.updateClosestPano(this.closestPano,false); //飞行结束后取消点击漫游点时得到的closestPano }else { } this.dispatchEvent({type:'flyToPanoDone', makeIt, disturb}); this.fastTranMaskPass.stop(); toPano.deferred && toPano.deferred.resolve(makeIt); //测量线截图时发现,resolve需要写在flying=false 后才行。 }; if(!toPano.pano.enabled)return done(false,true); //Potree.Log('hope flyToPano: '+toPano.pano.id, toPano.pano.position.toArray() ) if(this.latestToPano && this.latestToPano != toPano && (//还在飞 this.latestToPano.pano != this.currentPano || !this.isAtPano())){//如果旧的toPano只在pano旋转镜头,就直接取消旧的,继续执行 return done(false) } if(this.currentPano == toPano.pano && this.isAtPano() && !toPano.target && !toPano.quaternion ){ //已在该pano this.dispatchEvent({type:'flyToPano', toPano}); return done(true); } //Potree.Log('flyToPano: '+toPano.pano.id, toPano.pano.position.toArray() /* this.latestToPano && this.latestToPano.pano.id */ ) let target = toPano.target; let config = Potree.config.displayMode[Potree.settings.displayMode]; let pano = toPano.pano; let dis = pano.position.distanceTo(this.position); this.nextPano = pano; this.latestToPano = toPano; //this.flying = true //防止新的请求 //Potree.Log('flyToPano:'+pano.id + ' , duration:'+toPano.duration, null, 12) {//不飞的话是否不要执行这段? let wait = (e)=> { //console.log('wait done', pano.id) if(/* e.pano && */this.latestToPano && pano != this.latestToPano.pano)return//loadedDepthImg if(this.latestToPano != toPano)return Potree.Log('已经取消', pano.id) //如果取消了 setTimeout(()=>{ if(this.latestToPano != toPano)return this.flyToPano(toPano); },1); }; if(!pano.depthTex && pano.pointcloud.hasDepthTex){ //点云模式也要加载depthTex,因获取neighbour需要用到 console.log('等待加载depthtex', pano.id); pano.addEventListener('loadedDepthImg', wait, {once:true}); return pano.loadDepthImg() } if(config.atPano.showSkybox || config.atPano.pointUsePanoTex){ let a = this.updateCube(this.currentPano, toPano.pano); if(a == 'useBound'){ toPano.useBound = true; } if(this.checkAndWaitForPanoLoad(pano, toPano.basePanoSize || this.basePanoSize, wait )){ //console.log('等待贴图加载',pano.id) return } } } config = Potree.config.displayMode[Potree.settings.displayMode]; //可能换了 let pointcloudVisi = config.atPano.showPoint; //viewer.scene.pointclouds[0].visible Potree.Utils.updateVisible(this.cube,'showSkybox', config.atPano.showSkybox ); // this.cube.visible = config.atPano.showSkybox //console.log('开始飞1') if(config.transition.showPoint){ viewer.scene.pointclouds.forEach(e=>{ Potree.Utils.updateVisible(e, 'displayMode', true); }); } const endPosition = pano.position.clone(); let T = Potree.config.transitionsTime; /* let maxTime = this.isAtPano() ? T.panoToPanoMax : T.flyIn let duration = toPano.duration == void 0 ? (T.flyMinTime+Math.min(T.flytimeDistanceMultiplier * dis, maxTime)) : toPano.duration */ /* let maxDis = this.isAtPano() ? T.maxDistanceThreshold : T.maxDistanceThresholdFlyIn let duration = toPano.duration == void 0 ? Math.min(dis, T.maxDistanceThreshold) * T.flytimeDistanceMultiplier + T.flyMinTime : toPano.duration */ let maxDis = 7; let duration = toPano.duration == void 0 ? Math.min(dis, maxDis) * T.flytimeDistanceMultiplier + T.flyMinTime : toPano.duration; if(config.transition.showSkybox || config.transition.pointUsePanoTex){ if(Potree.settings.fastTran ){ this.fastTranMaskPass.start(); //截图当前画面 viewer.scene.view.position.copy(endPosition); } this.setProjectedPanos({ progress: 0, ifSkybox: this.cube.visible, ifPointcloud : config.transition.pointUsePanoTex, easeInOutRatio : pointcloudVisi ? 0.3 : 0, pano0:this.currentPano, pano1:pano }); } if(toPano.useBound){ duration = Math.min(1500, duration); toPano.easeName = 'easeInOutQuad'; }else { if(endPosition.equals(this.position))toPano.easeName = 'easeOutSine'; } { toPano.easeName = toPano.easeName || 'linearTween'; toPano.duration = duration; this.beforeFlyToPano(toPano); } /* let fly = ()=>{ this.dispatchEvent({type:'flyToPano', toPano}) viewer.scene.view.setView({position:endPosition, target, quaternion:toPano.quaternion , duration, callback:()=>{ if(!config.atPano.pointUsePanoTex){ viewer.scene.pointclouds.forEach(e=>{ e.material.stopProjectedPanos() }) } this.currentPano = pano; this.nextPano = null; if(Potree.settings.displayMode == 'showPanos'){ viewer.scene.pointclouds.forEach(e=>{ Potree.Utils.updateVisible(e, 'displayMode',pointcloudVisi) }) } done(true); this.updateDepthTex(this.currentPano) }, onUpdate:(progress)=>{ //console.log('uniforms progress',progress) this.cube.material.uniforms.progress.value = progress viewer.scene.pointclouds.forEach(e=>{ e.material.uniforms.progress.value = progress }) }, cancelFun:()=>{ done(false, true) }, Easing:easeName }) //duration > 0 && (this.flying = true) //再写一遍 防止cancel其他项目导致flying为false } */ let onUpdate = (progress)=>{ this.cube.material.uniforms.progress.value = progress; viewer.scene.pointclouds.forEach(e=>{ e.material.uniforms.progress.value = progress; }); }; let fly = ()=>{ let startProgress = toPano.progress = toPano.progress || 0; let loadNextProgress = MathUtils.clamp(1 - 2.5 / dis, 0.3, 0.8); //console.log(loadNextProgress, (1-loadNextProgress) * dis ) this.dispatchEvent({type:'flyToPano', toPano}); viewer.scene.view.setView({position:endPosition, target, quaternion:toPano.quaternion , duration:toPano.duration, onUpdate:(progress_, delta)=>{ let progress = startProgress + progress_ * (1 - startProgress); let currentSpeed; if (progress_ != 1 && progress_ != 0) { // 1的时候不准,往往偏小, 0的时候速度为0,也不记录 currentSpeed = ((progress - toPano.progress) * dis) / delta; //记录下当前速度,当变为匀速时可以过渡到flySpeed } else { currentSpeed = toPano.currentSpeed || 0; } toPano.currentSpeed = currentSpeed; toPano.progress = progress; //console.log('progress_', progress_, 'delta',delta , 'progress', progress/*, 'currentSpeed', currentSpeed, */ ) if (progress > loadNextProgress && toPano.easeName == 'linearTween' && currentSpeed){// 减速. 如果仅旋转就不停止 //console.log('减速', /* currentSpeed , */progress_ ) toPano.easeName = 'easeOutSine'; let restDis = (1 - progress) * dis; toPano.duration = (Math.PI / 2 * restDis) / currentSpeed; // 这样能保证初始速度为currentSpeed viewer.scene.view.cancelFlying('all',false); //为了防止执行cancelFun先主动cancel toPano.flyCount = 2; fly(toPano); } onUpdate(progress); }, callback:()=>{ if(!config.atPano.pointUsePanoTex){ viewer.scene.pointclouds.forEach(e=>{ e.material.stopProjectedPanos(); }); } this.lastPano = this.currentPano; //记录,调试 this.currentPano = pano; this.nextPano = null; if(Potree.settings.displayMode == 'showPanos'){ viewer.scene.pointclouds.forEach(e=>{ Potree.Utils.updateVisible(e, 'displayMode',pointcloudVisi); }); } done(true); this.updateDepthTex(this.currentPano); //删除dispose的depthTex }, cancelFun:()=>{ done(false, true); }, Easing:toPano.easeName, ignoreFirstFrame : toPano.flyCount != 2 //变换transition时不停一帧 }); }; if(Potree.settings.displayMode == 'showPanos'){ setTimeout(fly, 40); //更新geo后缓冲 }else { fly(); } //console.log('flyToPano:', toPano.pano.id) } beforeFlyToPano(toPano){ if(this.currentPano != toPano.pano) { if(Potree.settings.displayMode == 'showPanos'){ this.resetHighMap(); } this.smoothZoomTo(toPano.zoomLevel || 1, toPano.duration / 2); } } /* updateCube(pano0, pano1){ if(Potree.settings.displayMode != 'showPanos')return if(!viewer.scene.pointclouds.some(e=>!e.hasDepthTex)) return this.updateCube2(pano0, pano1) //都hasDepthTex的话 let useBound = (bound, size)=>{ size = size || bound.getSize(new THREE.Vector3) let center = bound.getCenter(new THREE.Vector3) size.max(new THREE.Vector3(HighMapCubeWidth,HighMapCubeWidth,HighMapCubeWidth)) this.cube.scale.copy(size) this.cube.position.copy(center) } let getPanoBound = (pano)=>{//因漫游点可能在点云外部,如室外平地,所以需要union进漫游点 let panoBound = new THREE.Box3 panoBound.expandByPoint(pano.position) panoBound.expandByVector(new THREE.Vector3(10,10,10));//give pano a margin return pano.pointcloud.bound.clone().union(panoBound) } let getDis = (bound1, bound2)=>{ //获取bound1边界到bound2边界距离 if(bound1.intersectsBox(bound2))return 0 let center1 = bound1.getCenter(new THREE.Vector3) let center2 = bound2.getCenter(new THREE.Vector3) let dis = center1.distanceTo(center2) let dis1 = bound1.distanceToPoint(center2) let dis2 = bound2.distanceToPoint(center1) return dis1 + dis2 - dis } if(pano1){//过渡 if(pano0.pointcloud == pano1.pointcloud){//同一个数据集内的过渡 useBound(getPanoBound(pano0)) }else{//非同一个数据集内的过渡 let bound = getPanoBound(pano0).union(getPanoBound(pano1)) if(getDis(pano0.pointcloud.bound, pano1.pointcloud.bound) < 100){ useBound(bound) }else{//如果两个数据集boundingbox距离大于这个距离,扩大一下,防止精度问题导致失真//对很远的数据集似乎没有什么用 let size = bound.getSize(new THREE.Vector3) let max = Math.max(size.x, size.y, size.z) size.set(max,max,max) useBound(pano0.pointcloud.bound.clone().union(pano1.pointcloud.bound), size) } } }else{ useBound(getPanoBound(pano0)) //假定一个数据集不会太大,否则会造成失真,且bump时移动距离看起来很小。或者可以考虑用固定大小 } } */ getIntersect(pano, dir, origin){ if(pano && pano.pointcloud.hasDepthTex ){ return this.depthSampler.sample( {dir }, pano, true ) }else { origin = origin || pano.position; return viewer.inputHandler.getIntersect({ viewport:viewer.inputHandler.hoverViewport, onlyGetIntersect:true, usePointcloud:true, point: origin.clone().add(dir), cameraPos: origin }) } } /* updateCube(pano0, pano1){//增加细分的版本,且垂直方向上也分多个 if(Potree.settings.displayMode != 'showPanos')return console.log('updateCube',pano0.id, pano1&&pano1.id) let f = (bound, size)=>{ size = size || bound.getSize(new THREE.Vector3) let center = bound.getCenter(new THREE.Vector3) size.max(new THREE.Vector3(HighMapCubeWidth,HighMapCubeWidth,HighMapCubeWidth)) this.cube.scale.copy(size) this.cube.position.copy(center) } this.cube.geometry.dispose(); if(pano1){//过渡 let count1, count2; let panoIndex = 0 let add = (pano, dir )=>{ let getPI = function(index, panoIndex_){ if(panoIndex_ == void 0) panoIndex_ = panoIndex return (count1 * count2 + 2 ) * panoIndex_ + index + 2 } let minZ, maxZ minZ = pano.floorPosition.z {//天花板高度值 //用三个间隔120度散开,和中心垂直线成一定夹角的三个向量去求 最高高度 (不求平均的原因:万一是0不好算) let rotMat = new THREE.Matrix4().makeRotationX(THREE.Math.degToRad(40))// 角度不能小于天花板中空的半径,大概就是0.2*Math.PI=36度 let dir1 = new THREE.Vector3(0,0,1).applyMatrix4(rotMat) let rotMat1 = new THREE.Matrix4().makeRotationZ(Math.PI*2 / 3); let rotMat2 = new THREE.Matrix4().makeRotationZ(-Math.PI*2 / 3); let dir2 = dir1.clone().applyMatrix4(rotMat1) let dir3 = dir1.clone().applyMatrix4(rotMat2) let zs = [dir1,dir2,dir3].map(dir_=>{ let intersect = this.depthSampler.sample( {dir: dir_}, pano, true ) let z = intersect ? intersect.location.z : pano.position.z return z }) zs.sort((a,b)=>{return b-a});//得最大值 maxZ = zs[0] let min = pano.position.z + 1 if(maxZ < pano.position.z + 0.04){//户外 maxZ = pano.position.z + 50 } maxZ = Math.max(min,maxZ) console.log(pano.id, 'maxZ:',maxZ ) console.log(pano.id, 'minZ:',minZ ) } [maxZ, minZ ].forEach(z=>{ posArr.push(pano.position.clone().setZ(z)) }) const angle = Math.PI/8 let getDir = (angle_)=>{ let rotMat = new THREE.Matrix4().makeRotationZ(angle_) return dir.clone().applyMatrix4(rotMat) } let dirs = [ getDir(angle*4), getDir(angle*3), getDir(angle*2), getDir(angle), dir.clone(), getDir(-angle) , getDir(-angle*2), getDir(-angle*3), getDir(-angle*4) ]; // 夹角总和180度 count1 = dirs.length dirs.forEach((dir, index)=>{//获取在这个方向上和墙体intersect的最远距离,使用中、上、下三个方向 let dirs_ = [ dir.clone().setZ(Math.tan(THREE.Math.degToRad(30))).normalize(), dir.clone().setZ(Math.tan(THREE.Math.degToRad(15))).normalize(), dir.clone(), // 水平方向 dir.clone().setZ(-Math.tan(THREE.Math.degToRad(15))).normalize(), dir.clone().setZ(-Math.tan(THREE.Math.degToRad(30))).normalize(), ]; count2 = dirs_.length dirs_.forEach((dir_, i) =>{ let intersect = this.depthSampler.sample( {dir: dir_}, pano, true ) let location if(!intersect){ //超级远 let distance = 100 location = dir_.clone().multiplyScalar(distance).add(pano.position) intersect = {distance,location} } //intersect.distance location = intersect.location.clone() let shouldZ// = THREE.Math.clamp(intersect.location.z, minZ, maxZ); let needShrink = false if(i == 0){//最上方的点高度直接等于天花板 shouldZ = maxZ if(location.zminZ)location.z = minZ else needShrink = true } if(needShrink ){//需要在保持dir_不变的情况下修改z let len = (shouldZ - pano.position.z) / (intersect.location.z - pano.position.z) * intersect.distance location = dir_.clone().multiplyScalar(len).add(pano.position) } posArr.push(location) if(index!=0 && i!=0){//加入一块四方面 let curIndex = getPI(count2 * index + i); faceArr.push([curIndex-1, curIndex-count2-1, curIndex-count2], [curIndex-1, curIndex, curIndex-count2]) } }) if(index!=0){ let top = -2; let btm = -1 faceArr.push([getPI(count2*(index-1) ), getPI(count2*index ), getPI(top) ])//天花板扇形 faceArr.push([getPI(count2*index-1), getPI(count2*(index+1)-1), getPI(btm) ] )//地板扇形 }else{ //加入和另一个pano连接的其中一个侧边 let panoIndex2 = (panoIndex + 1) % 2; for(let i=1;i{ //该点前方近处是否都被遮挡 。 为了防止杂点干扰,多个方向探测 let vec = new Vector3().subVectors(subPano.position, mainPano.position).normalize(); let getDir = (angle_, upDown)=>{ //基于vec的方向 向左向右旋转 //if(upDown) vec.clone().applyAxisAngle(new THREE.Vector3(1, 0, 0), upDown); return vec.clone().applyAxisAngle(new Vector3(0, 0, 1), angle_); }; let minScore = 0.25; let angles = [20,16,12.5,10,8,6,4.5,3,1, -1,-3,-4.5,-6,-8,-10,-12.5,-16,-20]; //水平方向角度 3之间有较大概率杂点, 3以外一部分有较大概率有空隙所以紧凑点 let wellCount = 0; let seccess = []; for(let i=0; i dis ){//投影在vec方向的长度不超过漫游点间距 wellCount ++ ; seccess.push(angles[i]); color = new Color('#0f8'); } ifLog && addLine(mainPano.position, dir, dis, color); } //console.log('ifSheltered') if(ifLog){ console.log('ifSheltered seccess', seccess, 'id:', mainPano.originID, subPano.originID); } let score = wellCount / angles.length; if(score >= minScore){//合格个数 return score } }; /* 识别一定角度范围内是否有遮挡时 几种奇葩的情况: 1 在墙的两边的但是墙是倾斜的漫游点: A / / / B 从B出发和BA成20度的射线和墙面的交点会远于A,导致识别为无遮挡。 所以还是改为识别范围内多个深度图像素, 合格像素个数占比 至少要大于50%,因为半边墙壁是50% 如SG-9UsbysDufBw&formal&test 的 6、9点。 而SS-GTmFBp1JN8k&formal&test的 2、3两点是人物遮挡 2 可以通行,但是右边是墙,右边的都检测失败,左侧的又有一些杂点,导致成功dir个数很低。 所以minRatio 要降低于0.5 */ //三个方向 : position0到position1, position0到floorPosition1, position1到floorPosition0。 只要有一个满足ifNeighbour就为true。 不过为了不使sampler总换图,先只考虑从主pano到副pano的方向 let getNeighbour = (mainPano, subPano )=>{ let dirPoints = [[subPano.position, mainPano.position]]; //注: 点A能到B不代表点B能到A,因为拍摄时物体会移动,或点位相对位置不对,无论是否是意外遮挡都且记下来,反正最后只要有一方可行就算相邻。 if(dis < 20){//在远处去掉对floorPosition的判断 dirPoints.push([subPano.floorPosition.clone().add(new Vector3(0,0,0.1)), mainPano.position]); } if(dis < 12){//为了防止楼梯拐角、杂点遮挡。 尽量只在穿墙时不可通行 dirPoints.push([subPano.position.clone().add(new Vector3(0,0,0.8)), mainPano.position]); let normal = math.getNormal2d({p1:subPano.position, p2:mainPano.position}).multiplyScalar(0.3); //左右方向 dirPoints.push([subPano.position.clone().add(new Vector3(normal.x,normal.y,0.1)), mainPano.position]); dirPoints.push([subPano.position.clone().add(new Vector3(-normal.x,-normal.y,0.1)), mainPano.position]); } //console.warn('getNeighbour', mainPano.id,subPano.id) for(let i=0; i dirPoints[i][0].distanceTo(dirPoints[i][1])){ //if(i>2)console.log('haha',i,'id:',mainPano.id,subPano.id) return true } } }; //if(!ifNeighbour && (map0[pano1.id] == void 0 || computeTwoDir && !map0[pano1.id] && map1[pano0.id] == void 0 )) {//主方向为空且不为邻居 if(pano0.pointcloud.hasDepthTex || pano1.pointcloud.hasDepthTex){ if(computeTwoDir ? (map0[pano1.id] == void 0 || map1[pano0.id] == void 0) : (map0[pano1.id] == void 0 && map1[pano0.id] !== false) ){ //2024.3.20改为必须两个方向都为true才行。 因为如果两个点分别在两个数据集相距很远,A确定有墙无法到B,B朝A看 没有B所在的数据集点云遮挡,因深度图不会包含别的数据集的点,故深度图探测到A处为无遮挡,就会导致会飞到遥远的A。 即使在同一个数据集,因为点云可以编辑位置,导致AB相对位置错误,也要避免这种情况可以互相乱走。 缺点是更容易被杂点阻挡,所以需要ifShelter降低阈值。 if(map0[pano1.id] == void 0 && pano0.depthTex){ let is = getNeighbour(pano0, pano1); map0[pano1.id] = !!is; } if(computeTwoDir && map0[pano1.id] && pano1.depthTex && map1[pano0.id] == void 0 ){ //computeTwoDir : 允许反向计算,即可以读取另一张的深度图 。 如果map0为false,map1就不用计算,只需要等待计算ifShelter let is = getNeighbour(pano1, pano0); map1[pano0.id] = !!is; } if( map0[pano1.id] === false || map1[pano0.id] === false){ if(dis<20) {//再检查下 只要两方都没有被完全遮挡就算可通行 针对杂点较多的情况 if(pano0.depthTex && pano1.depthTex){ ifNeighbour = false; let a = ifSheltered(pano0,pano1); if(a){ let b = ifSheltered(pano1,pano0); if(b && (a+b >= 0.6)){ ifNeighbour = true; } } map0[pano1.id] = map1[pano0.id] = ifNeighbour; } }else { //标记为无需再计算, 因已经失败。 当map0和map1都不为void 0后就无法再计算了。 if(map0[pano1.id] === void 0) map0[pano1.id] = 0 ; if(map1[pano0.id] === void 0) map1[pano0.id] = 0 ; } } ifNeighbour = map1[pano0.id] == void 0 ? map0[pano1.id] : map0[pano1.id] && map1[pano0.id]; //如果map1没计算就只要返回map0 if(ifLog){ console.log('isNeighbour', pano0.id+"("+pano0.originID+")", pano1.id+"("+pano1.originID+")", map0[pano1.id], map1[pano0.id]); } } }else if(!onlyUseTex && !ifNeighbour){//使用点云判断(有深度贴图时不会执行到这) let inDirection = ()=>{ let dir = new Vector3().subVectors(pano1.position,pano0.position).normalize(); let dis = pano1.position.distanceTo(pano0.position); let fov = MathUtils.degToRad(viewer.mainViewport.camera.fov); let hfov = cameraLight.getHFOVForCamera(viewer.mainViewport.camera , true ); let hov = Math.min(fov, hfov); let max = Math.cos(MathUtils.degToRad(10)); let min = Math.cos(MathUtils.degToRad(40)); if(this.getDirection().dot(dir) > MathUtils.clamp(Math.cos(hov/2 ) * dis / 10, min, max )){//距离越远要求和视线角度越接近 return true } }; if(computeDirFirst){//先计算方向,防止重复计算ifBlockedByIntersect if(inDirection()){ ifNeighbour = !viewer.inputHandler.ifBlockedByIntersect({point:pano1.position, margin, cameraPos:pano0.position}); } }else { ifNeighbour = !viewer.inputHandler.ifBlockedByIntersect({point:pano1.position, margin, cameraPos:pano0.position}); if(ifNeighbour && !inDirection()){ ifNeighbour = undefined; //不确定 } } map0[pano1.id] = map1[pano0.id] = ifNeighbour ? 'byCloud' : ifNeighbour;//写简单点 } if(map0[pano1.id] && map1[pano0.id]){ pano0.neighbours.includes(pano1) || pano0.neighbours.push(pano1); pano1.neighbours.includes(pano0) || pano1.neighbours.push(pano0); } return ifNeighbour } bump(direction) {//撞墙弹回效果 if (!this.bumping && !this.latestToPano) { let distance = Potree.settings.displayMode == 'showPanos' ? 0.15 : 0.12;//感觉点云模式比全景模式更明显,所以降低 let currentPos = this.position.clone(); let endPosition = new Vector3().addVectors(this.position, direction.clone().multiplyScalar(distance)); let duration = 150; viewer.scene.view.setView({position:endPosition, duration, callback:()=>{ viewer.scene.view.setView({position:currentPos, duration: duration*5, callback: ()=>{ this.bumping = false; //this.dispatchEvent('cameraMoveDone') }, Easing:'easeInOutSine', cancelFun:()=>{this.bumping = false;} }); this.bumping = true; }, cancelFun:()=>{this.bumping = false;}, Easing:'easeInOutSine' }); this.bumping = true; } //备注:将4dkk中的‘前后方向变化fov、左右方向移动镜头’ 都改为移动镜头。 因为这里无法判断左右离壁距离。 } flyToPanoClosestToMouse() { /* if (Date.now() - this.mouseLastMoveTime > 50) { //this.intersect = this.getMouseIntersect(); this.intersect && this.updateClosestPano(this.intersect); } */ if(!Potree.settings.ifShowMarker){//不显示marker的时候mousemove没更新鼠标最近点所以更新 this.updateClosestPano(viewer.inputHandler.intersect); } //console.log('flyToPanoClosestToMouse',this.closestPano) if (this.closestPano) { let pano = this.closestPano; return this.flyToPano({ pano, easeName: this.isAtPano() ? 'linearTween' : 'easeInOutQuad' }); } var direction = this.viewer.inputHandler.getMouseDirection().direction; this.flyDirection(direction); } flyDirection(direction, option1, option2, byKey) { if(viewer.mainViewport.view.isFlying()){// closestPanoInDirection函数耗时长,飞行时运行会卡顿(如果以后加无缝过渡再说) return } var deferred = $.Deferred(); //this.history.invalidate(); var panoSet = this.closestPanoInDirection(direction, option1, option2, byKey); if (panoSet) { this.flyToPano({ pano: panoSet, callback: deferred.resolve.bind(deferred, !0) } ); } else { //如果离数据集较远,转动也很难找到点云,就飞到就近点: if(Potree.settings.displayMode == 'showPointcloud'){ let po = viewer.scene.pointclouds.find(e=>e.visibleNodes.some(a=>a.getLevel() > Math.ceil(e.maxLevel * 0.15) )); //虽然当点云在前方很远的地方也可能符合 if(!po){//无可见点云 //console.log('no visi cloud') if(!this.panos.length){//如果场景中没有漫游点,如SG-t-XPf1k9pv3Zg 点击后回到可见区域 if(viewer.scene.pointclouds.length == 0)return let map = new Map(); let clouds = viewer.scene.pointclouds.filter(e=>e.root.geometryNode); clouds.forEach(e=>map.set(e, e.bound.distanceToPoint(this.position))); clouds.sort((a,b)=>map.get(a) - map.get(b)); viewer.flyToDataset({focusOnPoint:true, pointcloud:clouds[0], duration:500 });//飞最近的一个点云 return deferred.promise(); } this.flyToPano({ pano: this.findNearestPano(), duration:500, callback: deferred.resolve.bind(deferred, !0) }); return deferred.promise(); } } this.bump(direction); deferred.resolve(!1); } return deferred.promise(); } closestPanoInDirection(direction, option1, option2, byKey) { return this.rankedPanoInDirection(0, direction, option1, option2, byKey) } rankedPanoInDirection(t, direction, option1, option2, byKey){ //此direction为mouseDirection,是否需要加上相机角度的权重 let startTime = Date.now(); var panoSet = { pano: null, candidates: [] //缓存顺序--如果需要打印的话 }; t || (t = 0); option1 = void 0 !== option1 ? option1 : 0.6; //.75; 2024.3.22改小,也就是可以走的角度范围增加,主要针对像山野那种很大的场景,不知道可以走哪,容易因角度范围内无近点而飞到远处的情况。但不能太小,总是横着走路 var o = option2 ? "angle" : "direction"; var floor = viewer.modules.SiteModel.currentFloor; var entity = viewer.modules.SiteModel.inEntity; var getHeightDis = (pano)=>{ if(floor && !floor.panos.includes(pano) && pano.position.z < this.position.z){ //若是上方的漫游点,就正常走。因为一般不会点击天花板。 return this.position.z - pano.position.z }else { return 0 } }; let disSquareMap = new Map(); this.panos.forEach(pano=>{ let dis2 = pano.position.distanceToSquared(this.position); //距离目标点 disSquareMap.set(pano, dis2); }); let changeTexCount = 0, maxWaitDur = 300; //maxSamplerChangeTex = THREE.Math.clamp( maxWaitDur / Potree.timeCollect.depthSampler.median, 2, 10) //计算换贴图最大数目 var request = [//必要条件 Images360.filters.not(this.currentPano), Images360.filters.isEnabled(), //Images360.filters.inFloorDirection( this.position, direction, option1 ), //原先用inPanoDirection,但容易穿楼层,当mouse较低或较高 //因不束缚纵向所以可能往相反反向 (pano)=>{ /* let isNeighbour = this.isNeighbour(this.currentPano, pano, true, true); //不计算的 if(isNeighbour == void 0){ let willChangeTex = pano.depthTex && !this.depthSampler.imgDatas.some(e=>e.pano == pano) if(changeTexCount < maxSamplerChangeTex || !willChangeTex ){ isNeighbour = this.isNeighbour(this.currentPano, pano, false, true);//计算 if(willChangeTex && isNeighbour != void 0) changeTexCount++ }else{ if(disSquareMap.get(pano) < 500)return true //因为费时,超过一定个数就不计算了,距离近的话先可通行。 } } */ // 不会再changeTex了 let isNeighbour = this.isNeighbour(this.currentPano, pano, {onlyUseTex:true}); if(isNeighbour || pano.noNeighbour && disSquareMap.get(pano) < 200){//在靠近孤立点时可以通行。但是不好把握这个距离,太远的话很多地方都会不小心到孤立点,太近的话可能永远到不了。 return true } }, /* (pano)=>{ //防止不小心穿越地板到下一层, 尽量走楼梯,实在没有楼梯或楼梯漫游点稀疏的话就通过楼层按钮。 let dis = getHeightDis(pano) //console.log('getHeightDis',pano.id,dis) if(dis < 3){//不能超过最大高度差( 最大高度差暂定为接近一层楼的高度。)(最好的解决方案是设置漫游可行) return true }else{ return this.isNeighbour(this.currentPano, pano) } } */ ]; if(!byKey){ request.push(Images360.filters.inPanoDirection( this.position, this.getDirection(), option1/* , true */)); //垂直方向上再稍微限制一下, 要接近视线方向,避免点击前方时因无路而到下一楼。但不能太高,否则楼梯上稍微朝下点击都到不了上方。之所以使用视线方向是因为镜头方向比鼠标方向目的性更强。 } var list = [//决胜项目 (pano)=>{ return -disSquareMap.get(pano) }, Images360.scoreFunctions[o]( this.position, direction, true), (pano)=>{ let neighbour = this.isNeighbour(this.currentPano, pano, {dontCompute:true, isNeighbour:true}); //不计算的 return neighbour ? directionFactor : 0; } , /* (pano)=>{//尽量不穿越地板到下一层 let dis = getHeightDis(pano) return -dis * directionFactor * 0.1; }, */ /* (pano)=>{ //尽量在一个建筑内行走,这样不易穿墙 let score = 0 if(entity ){ if(!entity.panos.includes(pano) ) score -= directionFactor * 0.01; //不能设置太高,否则会走不进大房间的小房间中,因为容易穿过小房间的一个门到另一个门外 if(!floor.panos.includes(pano)) {//避免穿楼层 let heightDiff = Math.abs(pano.position.z - this.position.z); if(heightDiff > 2){ score -= directionFactor * 0.1 * heightDiff; } } } return score } */ ]; if(!byKey && viewer.inputHandler.intersect && this.currentPano ){//方便上下楼, 考虑panos之间的角度差 let pos1 = this.currentPano.floorPosition; let vec1 = new Vector3().subVectors(viewer.inputHandler.intersect.location, pos1 ).normalize();//应该只有atPano时才会执行到这吧? list.push( function(pano) { var pos2 = pano.floorPosition; var vec2 = pos2.clone().sub(pos1).normalize(); return vec2.dot(vec1) * directionFactor * 4 }); } this.findRankedByScore(t,request,list,panoSet); //console.log( 'costTime:',Date.now() - startTime) return panoSet.pano; } findRankedByScore(e, t, i, n) { n && (n.candidates = null, //candidates 缓存顺序--如果需要打印的话 n.pano = null), e || (e = 0); var r = Common.sortByScore(this.panos, t, i); //console.log('findRankedByScore', r && r.map(u=>u.item.id + '| ' + math.toPrecision(u.score,4) + " | " + math.toPrecision(u.scores,4))) return !r || 0 === r.length || e >= r.length ? null : (n && (n.candidates = r, n.pano = r[e].item), r[e].item) } updateClosestPano(intersect, state) {//hover到的pano 大多数时候是null var pano; if(intersect instanceof Panorama){ //漫游模式 pano = state ? intersect : null; }else { if(this.isAtPano() || this.bumping){ return }else { var filterFuncs = []; intersect = intersect && intersect.location; if(!intersect)return let sortFuncs = Potree.settings.editType != 'pano'? [Images360.sortFunctions.floorDisSquaredToPoint(intersect)] : [Images360.sortFunctions.disSquaredToPoint(intersect)]; pano = Common.find(this.panos, filterFuncs, sortFuncs); } } if (pano != this.closestPano) { pano && (this.isPanoHover = !0); this.closestPanoChanging(this.closestPano, pano); // 高亮marker //console.log('closestPano '+ (pano ? pano.id : 'null' )) this.closestPano = pano; } else { this.isPanoHover = !1; } } closestPanoChanging(oldPano, newPano){ if(!Potree.settings.ifShowMarker)return oldPano && oldPano.hoverOff({byImages360:true}); newPano && newPano.hoverOn({byImages360:true}); } getTileDirection(){//根据不同dataset的来存储 var vectorForward = viewer.scene.view.direction.clone(); var vectorForwards = viewer.scene.pointclouds.map(e=>{ var inv = new Matrix4().copy(e.rotateMatrix).invert();//乘上dataset的旋转的反转 var direction = vectorForward.clone().applyMatrix4(inv); return { datasetId: e.dataset_id, direction: math.convertVector.ZupToYup(direction) } }); //return vectorForwards[0].direction return { datasetsLocal: vectorForwards, vectorForward } } update(){ //if(this.tileDownloader.started){ var vectorForwards = this.getTileDirection(); //vectorForwards = vectorForwards[0].direction this.updateTileDownloader(tileArr, vectorForwards); this.updatePanoRenderer(vectorForwards); //} this.updateZoomPano(); } updateTileDownloader(t, vectorForward) { var i = this.nextPano || this.currentPano; if(i){ this.tileDownloader.tilePrioritizer.updateCriteria(i, viewer.scene.view.position.clone() , vectorForward, t.length > 0 ? t : null), this.tileDownloader.processPriorityQueue = !0; } } updatePanoRenderer(vectorForward) { var i = this.nextPano || this.currentPano; if(i){ if (this.panoRenderer.hasQueuedTiles() && i) { this.panoRenderer.updateDirection(vectorForward); } } } //等待部分加载完 checkAndWaitForTiledPanoLoad(pano, basePanoSize, callback1, callback2, progressCallback, iswait, isclear, l) { //console.log('checkAndWaitForTiledPanoLoad',pano.id) if (!pano) { console.error("Player.checkAndWaitForTiledPanoLoad() -> Cannot load texture for null pano."); } var vectorForward = this.getTileDirection(); if (!pano.isLoaded(basePanoSize)) { iswait && viewer.waitForLoad(pano, function() {//发送loading return pano.isLoaded(basePanoSize) }); /* var fov = {//test for direction 预加载的边缘有一丢丢不准确,尤其在相机倾斜时(4dkk也是)。 hFov: cameraLight.getHFOVForCamera(viewer.scene.getActiveCamera() ), vFov: viewer.scene.getActiveCamera().fov }//原先是null,不要求方向 */ var fov = null; //若不为null的话,因为可能可见范围的tile下载过了从而无法触发下载,然后得不到下载成功的消息,怎么办 pano.loadTiledPano(/* 1024 */ basePanoSize , vectorForward, fov, isclear, l, null).done(function(e, t) { callback1 && callback1(e, t); } .bind(this)).fail(function(msg) { callback2 && callback2(msg); } .bind(this)).progress(function(e, t, i) { progressCallback && progressCallback(e, t, i); } .bind(this)); return !0; } } fitPanoTowardPoint(o){ //寻找最适合的点位 var point = o.point, //相机最佳位置 target = o.target || o.point, //实际要看的位置 require = o.require || [], rank = o.rank || [], force = o.force, getAll = o.getAll, bestDistance = o.bestDistance || 0, sameFloor = o.sameFloor, maxDis = o.maxDis, dir = o.dir; let camera = viewer.scene.getActiveCamera(); if(target && !dir){ dir = new Vector3().subVectors(target,point).normalize(); } let atFloor = sameFloor && viewer.modules.SiteModel.pointInWhichEntity(point, 'floor'); //if(o.floor)require.push(Panorama.filters.atFloor(o.floor)) let checkIntersect = o.checkIntersect; let base = Math.max(300, viewer.bound.boundSize.length()*3); if(o.boundSphere){//只接受boundSphere let aspect = 1;//size.x / size.y let dis; if(camera.aspect > aspect){//视野更宽则用bound的纵向来决定 dis = /* size.y */o.boundSphere.radius/* / 2 *// MathUtils.degToRad(camera.fov / 2); }else { let hfov = cameraLight.getHFOVForCamera(camera , true ); dis = /* size.x */ o.boundSphere.radius /* / 2 */ / (hfov / 2); } bestDistance = dis;//*0.8 } let disSquareMap = new Map(); let bestDisSquared = bestDistance * bestDistance; let maxDisSquared = maxDis && (maxDis * maxDis); this.panos.forEach(pano=>{ let dis2 = pano.position.distanceToSquared(target); //距离目标点 disSquareMap.set(pano, dis2); }); let panos = this.panos.sort((p1,p2)=>{return disSquareMap.get(p1)-disSquareMap.get(p2)}); if(maxDisSquared){//热点超过最大距离不可见的 let panos2 = [], pano, i=0; while(pano = panos[i], disSquareMap.get(pano) < maxDisSquared){ panos2.push(pano); i++; } if(panos2.length == 0)return {pano, msg:'tooFar'} //全部都大于maxDis, 就返回最近的 panos = panos2; } rank.push((pano)=>{ let dis1 = Math.abs(pano.position.distanceToSquared(point) - bestDisSquared); //距离最佳位置 disSquareMap.set(pano, dis1); if(!target){ return -dis1 }else { let dis2 = disSquareMap.get(pano); if(o.gotoBestView){//忽略和当前视线的角度 return -(dis1 + dis2*0.3) } let vec2 = new Vector3().subVectors(target,pano.position).normalize(); let cos = dir.dot(vec2); //let result = (- dis1 - Math.pow(dis2 , 1.5)) / (cos + 2) // cos+2是为了调整到1-3, let result = (dis1 + dis2*0.3) * ( -1 + cos*0.9 ); //尽量贴近最佳位置的角度, 或贴近相机原来的角度 。尽量靠近最佳观测点,并且优先选择靠近目标点的位置.(注意cos的乘数不能太接近1,否则容易只考虑角度) //Potree.Log(pano.id, dis1, dis2, cos, result,{font:{toFixed:2,fontSize:10}}) return result } //注:热点最好加上法线信息,这样可以多加一个限制,尽量顺着热点像展示的方向。 }, (pano)=>{ let score = 0; if(pano.depthTex && checkIntersect){ let intersect = !!viewer.ifPointBlockedByIntersect(target, pano.id, true); //viewer.inputHandler.ifBlockedByIntersect({point:target, margin:0.1, cameraPos:pano}) if(intersect){ score = 0; }else { score = base * 2; } }else { score = base * 1.5; //没加载好的话,不管了 , 几乎当做无遮挡,否则容易到不了最近点 } return score } ); var g = Common.sortByScore(panos, require, rank); // console.log(g) /* let result1 = g && g.slice(0, 10) if(result1){ g = Common.sortByScore(result1, [], [(e)=>{//避免遮挡 let pano = e.item; let score = 0, log = '' if(atFloor && atFloor.panos.includes(pano)){//如果不在任何一楼呢? score += 600, log+='atFloor' } return {score, log} }]); if(g){ g.forEach(e=>{e.item = e.item.item}) } console.log(g) } */ let pano = g && g.length > 0 && g[0].item; if(pano && checkIntersect){ let intersect = !!viewer.ifPointBlockedByIntersect(target, pano.id, true); if(intersect){ return {pano, msg : 'sheltered'} } } //if(getAll)return g; return pano //注:深度图有的位置会不准确,以至于会算出错误的遮挡、选择错误的pano,解决办法只能是移动下热点到更好的位置 } //---------------scroll zoom ------------------------------------------ /* zoomIn = function() { //放大 this.zoomBy(1 + this.zoomSpeed); } zoomOut = function() {//缩小 this.zoomBy(1 - this.zoomSpeed); } */ zoomBy(e, pointer) {//以倍数 this.zoomTo(this.zoomLevel * e, pointer); } zoomTo(zoomLevel, pointer) {//缩放到某绝对zoomLevel let zoom = Potree.settings.zoom; if (zoom.enabled) { zoomLevel = MathUtils.clamp(zoomLevel, zoom.min, zoom.max); //console.log(zoomLevel) if(zoomLevel == this.zoomLevel) return; this.zoomLevel = zoomLevel; if(Potree.settings.panoZoomByPointer){ //定点缩放:使当前鼠标所在的位置缩放后不变 let view = viewer.scene.view; let originDir = viewer.scene.view.direction; let oldPointerDir = viewer.inputHandler.getMouseDirection(pointer).direction; viewer.setFOV(Potree.config.view.fov * (1 / this.zoomLevel)); let newPointerDir = viewer.inputHandler.getMouseDirection(pointer).direction; view.direction = oldPointerDir; //获取一下鼠标所在位置的yaw 和 pitch let oldPitch = view.pitch, oldYaw = view.yaw; view.direction = newPointerDir; let newPitch = view.pitch, newYaw = view.yaw; view.direction = originDir; //还原 viewer.scene.view.pitch -= newPitch - oldPitch; viewer.scene.view.yaw -= newYaw - oldYaw; }else { viewer.setFOV(Potree.config.view.fov * (1 / this.zoomLevel)); } } } zoomFovTo( fov ) { //通过fov来算zoomLevel let zoomLevel = Potree.config.view.fov /* this.baseFov */ / fov; this.zoomTo( zoomLevel ); } smoothZoomTo(aimLevel, dur=0) { var currentLevel = this.zoomLevel; if(currentLevel == aimLevel)return; var fun = (progress)=>{ //progress > 1 && (progress = 1) let level = currentLevel * (1 - progress) + aimLevel * progress; this.zoomTo(level, !0); }; transitions.start(fun, dur, null, null, 0 , easing['easeInOutQuad'] ); } updateZoomPano() { if (!this.panoRenderer.zoomPanoRenderingDisabled && Potree.settings.displayMode == 'showPanos') { var currentPano = this.currentPano; if (currentPano) { let levelThreshold1 = Potree.settings.navTileClass == '1k' ? 1.3 : 1.7 , levelThreshold2 = 2; var t = this.zoomLevel > levelThreshold1, n = !(this.flying && this.nextPano && this.nextPano !== this.currentPano), r = t && n; this.tileDownloader.tilePrioritizer.setZoomingActive(r); this.panoRenderer.setZoomingActive(r, currentPano, !0); var o = (pano, ifZoom)=>{ this.panoRenderer.resetRenderStatus(pano.id, !1, !0, this.qualityManager.getMaxNavPanoSize()); this.panoRenderer.clearAllQueuedUploadsForPano(pano.id); this.panoRenderer.renderPanoTiles(pano.id, null, !1, !1); pano.setZoomed(ifZoom); }; if (r && (!currentPano.zoomed || this.qualityManager.zoomLevelResolution && this.qualityManager.zoomLevelResolution != '4k')) {//needZoom currentPano.zoomed || o(currentPano, !0); if(Potree.settings.navTileClass == '1k' && Potree.settings.tileClass != '1k' && this.zoomLevel < levelThreshold2){ this.panoRenderer.enableHighQuality( function() {//开启2k if(Potree.settings.tileClass != '4k') o(currentPano, !0); }.bind(this)); }else { this.panoRenderer.enableUltraHighQualityMode(function() {//开启4k getMaxZoomPanoSize this.qualityManager.useUltraHighResolutionPanos && (Potree.settings.zoom.max = Potree.config.ultraHighQualityMaxZoom); o(currentPano, !0); }.bind(this)); } } else { !t && currentPano.zoomed && o(currentPano, !1); } if(r && Potree.settings.navTileClass == '1k' && Potree.settings.tileClass == '4k' ){ //目前只有手机端navTileClass == '1k' (分三个梯度) var change = (zoomedFlag)=>{ this.qualityManager.updateMaximums();//更新maxZoomPanoSize this.panoRenderer.setupZoomRenderTarget(); //更新renderTarget //currentPano.setZoomed(t);//更新uniforms贴图 }; this.qualityManager.zoomLevelResolution = this.zoomLevel >= levelThreshold2 ? '4k' : this.zoomLevel > levelThreshold1 ? '2k' : '1k'; if(this.oldZoomLevel < levelThreshold2 && this.zoomLevel >= levelThreshold2){//1k/2k-4k change(); o(currentPano, t); }else if(this.oldZoomLevel <= levelThreshold1 && this.zoomLevel > levelThreshold1){//1k-2k change(); }else if(this.oldZoomLevel > levelThreshold2 && this.zoomLevel <= levelThreshold2){//4k-2k/1k change(); o(currentPano, t); }else if(this.oldZoomLevel > levelThreshold1 && this.zoomLevel <= levelThreshold1){//2k-1k change(); } this.oldZoomLevel = this.zoomLevel; } } } } //-------------------- addHighMapCube(){//创建8*8的tile cube 主要因手机版崩溃 要在电脑端测试得设置maxRenderTargetSize if( Potree.settings.tileClass == '4k' && this.qualityManager.maxRenderTargetSize == 2048){ var geo = new PlaneGeometry(1, 1, 1, 1); var cube = new Object3D; cube.tiles = []; for(var cubeIndex=0; cubeIndex<6; cubeIndex++){ var face = new Object3D; for(var i=0;i<8;i++){ for(var j=0;j<8;j++){ var tile = new Mesh(geo, new MeshBasicMaterial({ //side:THREE.DoubleSide transparent:true, opacity : 0.4, depthTest:false, })); tile.position.set(i-3.5, j-3.5, -4); if(Potree.settings.isTest){ var colorHue = Math.random(); tile.material.color = (new Color()).setHSL(colorHue, 0.6, 0.7); /* tile.scoreLabel = new TextSprite( { backgroundColor: { r: 0, g: 0, b: 0, a: 0 }, textColor: { r: 0, g: 0, b: 0, a: 1 }, borderRadius: 15, renderOrder: 50, fontsize : 13, text: '' , dontFixOrient:true }) tile.scoreLabel.sprite.material.side = 2; tile.scoreLabel.rotation.x = Math.PI tile.add(tile.scoreLabel) */ } tile.visible = false; tile.tileX = i; tile.tileY = j; tile.cubeFace = cubeIndex; //tile.renderOrder = RenderOrder.highTileCube cube.tiles.push(tile); face.add(tile); } } switch(cubeIndex){ case GLCubeFaces$3.GL_TEXTURE_CUBE_MAP_POSITIVE_X: face.rotation.set(0,Math.PI/2,0); break; case GLCubeFaces$3.GL_TEXTURE_CUBE_MAP_NEGATIVE_X: face.rotation.set(0,-Math.PI/2,0); break; case GLCubeFaces$3.GL_TEXTURE_CUBE_MAP_POSITIVE_Y: var rot1 = new Quaternion().setFromAxisAngle(new Vector3(0,1,0),Math.PI); var rot2 = new Quaternion().setFromAxisAngle(new Vector3(1,0,0),Math.PI/2); face.quaternion.copy(rot1).multiply(rot2); //face.rotation.set(Math.PI/2,0,0); break; case GLCubeFaces$3.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y: //face.rotation.set(-Math.PI/2,0,0); var rot1 = new Quaternion().setFromAxisAngle(new Vector3(0,1,0),Math.PI); var rot2 = new Quaternion().setFromAxisAngle(new Vector3(1,0,0),-Math.PI/2); face.quaternion.copy(rot1).multiply(rot2); break; case GLCubeFaces$3.GL_TEXTURE_CUBE_MAP_POSITIVE_Z: face.rotation.set(0,Math.PI,0); break; case GLCubeFaces$3.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z: face.rotation.set(0,0,0); } face.scale.set(1,-1,1); face.cubeFace = cubeIndex; cube.add(face); } cube.name = 'highMapCube'; this.highMapCube = cube; viewer.scene.scene.add(cube); { let s = 0.1; cube.scale.set(s,s,s); }//注:由于原本的mesh上加了深度贴图,可能距离镜头比这里的近。凡是在cube以内的部分都会挡住cube导致模糊。但是应该不常见吧(另外到天空的边缘也是很近)。姑且depthTest=false this.highMapCube.visible = false; Potree.Utils.setObjectLayers(this.highMapCube, 'sceneObjects'/* 'skybox' */); //因它的深度是错误的,故不在skybox层渲染,影响edlRT, 而在renderOverlay时渲染覆盖。 //console.warn('addHighMapCube') viewer.addEventListener('update',()=>{ if (this.highMapCube.visibleTiles) { this.updateTiles(); //逐步将visibleTiles加载完 } }); viewer.addEventListener('camera_changed',(e)=>{ if(e.viewport == viewer.mainViewport){ //重新获取visibleTiles Common.intervalTool.isWaiting( 'update4kTiles', () => { let vectorForward = this.getDirection(); this.updateTiles(vectorForward); }, 500 ); } }); } } isHighMapLoaded( cubeFace, tileX, tileY){ var tile = this.highMapCube.children[cubeFace].children[tileX*8+tileY]; return !!tile.material.map } updateTiles(direction) { if (!this.highMapCube || !this.highMapCube.visible) return if (this.highMapCube.tiles.filter(e => e.image).length <= 10) return //加载的太少了 //performance.mark('updateTiles-start') if (direction) { let camera = viewer.mainViewport.camera; let hfov = cameraLight.getHFOVForCamera(camera, true) / 2; let vfov = MathUtils.degToRad(camera.fov) / 2; /* let hcos = Math.cos(hfov / 2) let vcos = Math.cos(vfov / 2) */ let list = this.highMapCube.tiles; list.forEach(e => { //屏幕外的不显示 let pos = e.getWorldPosition(new Vector3()); let dir = new Vector3().subVectors(pos, this.highMapCube.position).normalize(); let hcos_ = dir.clone().setZ(direction.z).normalize().dot(direction); //在direction的斜面上水平角度差 let hRad = Math.acos(hcos_); let vRad = -200; if (/* hRad > hfov + 0.08 */ hRad / hfov > 1.5 ) { e.score = -100; } else { vRad = Math.abs(Math.acos(dir.z) - Math.acos(direction.z)); if (/* vRad > vfov + 0.08 */ vRad / vfov > 1.5 ) { e.score = -100; } else { e.score = -(hRad / hfov + vRad / vfov); } } e.scores = hRad.toFixed(3) + ', ' + vRad.toFixed(3); if (e.score == -100) { this.resetTile(e); } }); this.highMapCube.visibleTiles = list.filter(e => e.score > -100); //list.forEach(e=>e.scoreLabel.setText(e.scores)) } let needRecover = this.highMapCube.visibleTiles.filter(e => !e.material.map); if (needRecover.length) { let maxCount = Common.getBestCount( '4kmaxTileRecover', 0, 2, 1.5, 6, false, 2 ); let count = 0; console.log(maxCount); needRecover.forEach((e, i) => { //只更新若干个,因为太耗时了, 其余的等下帧更新 if (count >= maxCount) return let r = this.recoverTile(e); if (r) count++; }); } } getHighImage(image, cubeFace, tileX, tileY) { let tile = this.highMapCube.children[cubeFace].children[tileX * 8 + tileY]; tile.image = image; //先记录下来 } updateHighMap(image, cubeFace, tileX, tileY){ //console.warn('updateHighMap') var tile = this.highMapCube.children[cubeFace].children[tileX*8+tileY]; if (image) tile.image = image; //先记录下来 if(tile.material.map)return if (this.highMapCube.visibleTiles && !this.highMapCube.visibleTiles.includes(tile) /* this.highMapCube.texLoadedCount >= this.getMaxTileCount() */) { return } //简易创建贴图 /* var tex = this.$app.core.get('SceneRenderer').initSizedTexture2D(512, THREE.ClampToEdgeWrapping) //var loaded = this.$app.core.get('Player').model.isHighMapLoaded(tile.cubeFace, tile.tileX, tile.tileY) this.$app.core.get('SceneRenderer').uploadTexture2D(image, tex, 0, 0, 512, 512) //只替换tex对应的img,不新建 */ var tex = new Texture(); tex.image = image; tex.flipY = false; tex.wrapS = tex.wrapT = ClampToEdgeWrapping; tex.generateMipmaps = false; tex.minFilter = LinearFilter; tex.needsUpdate = true; tile.material.map = tex; tile.material.opacity = 1; tile.material.transparent = false; tile.visible = true; tile.material.needsUpdate = true; //发现每次开始放大但还未放大到4k时也会把之前加载过的4k加载 } recoverTile(tile) { if (tile.material.map) return if (tile.image) { this.updateHighMap(tile.image, tile.cubeFace, tile.tileX, tile.tileY); return true } else { //console.log('no image') } } resetTile(tile, kill) { if (kill) { //完全消灭 tile.image = null; } let map = tile.material.map; if (map) { map.dispose(); //这句执行了以后,h.__webglTexture一直就是undefined map.loaded = !1; map.version = 0; //保底再执行一下这个,类似app.sceneRenderer.deallocateCubeTexture(tile.material.map) var h = viewer.renderer.properties.get(map); //console.log('__webglTexture',!!h.__webglTexture) viewer.renderer.getContext().deleteTexture(h.__webglTexture); tile.material.map = null; tile.material.needsUpdate = true; tile.visible = false; //this.highMapCube.texLoadedCount -- //console.log('resetTile'/* , tile.cubeFace, tile.tileX, tile.tileY */) } } resetHighMap() { if (!this.highMapCube) return this.highMapCube.children.forEach(e => e.children.forEach(tile => { this.resetTile(tile, true); }) ); //this.highMapCube.texLoadedCount = 0 this.highMapCube.visibleTiles = null; this.hideHighMap(); //console.log('resetHighMap') } setHighMap(pano){ if(!this.highMapCube) return this.highMapCube.position.copy(pano.position); //可以利用第0个pano查看,其 rotation4dkk是(_x: 0, _y: -1.5707963267948966, _z: 0 )而手动旋转至(_x:1.5707963, _y: -1.57079, _z: 0)时才正确,说明要在4dkk的旋转基础上,绕x轴转90度,(也就是转成navvis坐标系), 然后得到YupToZup的函数写法的 this.highMapCube.quaternion.copy( math.convertQuaternion.YupToZup( pano.quaternion4dkk ) ); //乘上数据集整体的旋转: let modelRotQua = new Quaternion().setFromRotationMatrix(pano.pointcloud.rotateMatrix); this.highMapCube.quaternion.premultiply(modelRotQua); } showHighMap(){ if(!this.highMapCube) return //console.warn('showHighMap') this.highMapCube.visible = true; } hideHighMap(){ if(!this.highMapCube) return //console.warn('hideHighMap') this.highMapCube.visible = false; } //缩小后继续显示cube呢还是不显示? 不显示的话,就要把cube上的复制到renderTarget上……会不会又崩溃,or没加载的显示??? addPanoData(data, datasetId ){//加载漫游点 //data[0].file_id = '00019' if(data.data) data = data.data; if(data.length == 0)console.error(datasetId + ' 没有漫游点'); //data = data.sort(function(a,b){return a.id-b.id}) data.forEach((info)=>{ //if(Potree.fileServer){ info.id = this.panos.length; //把info的id的一长串数字改简单点 //} let pano = new Panorama( info, this ); pano.addEventListener('dispose',(e)=>{ if(this.closestPano == pano) this.closestPano = null; }); this.panos.push(pano); if(Potree.settings.editType == 'pano'){ Potree.settings.datasetsPanos[datasetId].panos.push(pano); } }); } loadDone(){ Potree.Utils.setObjectLayers(this.node, 'sceneObjects'); //this.updateCube(/* viewer.bound */) this.panos.forEach(e=>{ this.neighbourMap[e.id] = {}; e.label && Potree.Utils.setObjectLayers(e.label, 'bothMapAndScene'); e.label2 && Potree.Utils.setObjectLayers(e.label2, 'bothMapAndScene'); }); this.tileDownloader.setPanoData(this.panos, [] /* , Potree.settings.number */); { let minSize = new Vector3(1,1,1); this.bound = math.getBoundByPoints(this.panos.map(e=>e.position), minSize); viewer.scene.pointclouds.forEach(pointcloud=>pointcloud.getPanosBound()); } if(viewer.scene.pointclouds.some(e=>e.panos.length == 0)){ //console.warn('存在数据集没有pano'); viewer.hasNoPanoDataset = true; } } getPano(value, typeName='id'){ //默认找的是id,也可以是sid、uuid return this.panos.find(p=>p[typeName] == value) } }; //判断当前点是否加载了全景图 Images360.prototype.checkAndWaitForPanoLoad = function() { var isLoadedPanos = {}, LoadedTimePanos = {}, loadedCallback = {}, //add maxTime = 5e3; var withinTime = function() { for (var panoId in isLoadedPanos) if (isLoadedPanos.hasOwnProperty(panoId) && isLoadedPanos[panoId]) { var differTime = performance.now() - LoadedTimePanos[panoId]; if (differTime < maxTime) return !0 } return !1 }; return function(pano, basePanoSize, doneFun1, doneFun2, progressCallback, iswait, isclear, p ) { loadedCallback[pano.id] = doneFun1;//add 因为有可能之前请求的没加doneFun1, 如果加载好就执行最新的doneFun1 if (withinTime()){//距离上次请求时间很近 //console.log(11) return !0; //这里感觉应该是!1 } let changeMode = (e)=>{ if(e.mode == 'showPointCloud'){ console.warn('切到点云模式了,就删除loadedCallback记录',pano.id);//否则再次转为showPanos会被withinTime阻拦 delete isLoadedPanos[pano.id]; viewer.cancelLoad(pano); loadedCallback[pano.id] && loadedCallback[pano.id](); //可以飞行了 doneFun2 && doneFun2(); this.removeEventListener('requestMode', changeMode); } }; this.addEventListener('requestMode', changeMode); var callback1 = (param1, param2)=>{ setTimeout(()=>{ isLoadedPanos[pano.id] = !1; loadedCallback[pano.id] && loadedCallback[pano.id](param1, param2); this.removeEventListener('requestMode', changeMode); },1); }; var callback2 = (param)=>{//没有看到有传doneFun2的 setTimeout(()=>{ isLoadedPanos[pano.id] = !1; doneFun2 && doneFun2(param); },1); }; try { null !== iswait && void 0 !== iswait || (iswait = !0); isLoadedPanos[pano.id] = this.checkAndWaitForTiledPanoLoad(pano, basePanoSize, callback1, callback2, progressCallback, iswait, isclear, p ); //true代表没加载好 isLoadedPanos[pano.id] && (LoadedTimePanos[pano.id] = performance.now()); return isLoadedPanos[pano.id]; } catch (msg) { isLoadedPanos[pano.id] = !1; LoadedTimePanos[pano.id] = performance.now() - maxTime; throw msg; } } }();//加载全景图的流程:请求下载当前点的最低分辨率图(不过有可能已经下载了),并且请求uploadTile渲染当前漫游点,若tile都upload完(先6个)会发送消息,LoadComplete 从而回调成功。 Images360.prototype.getNeighbours = function(){ //逐渐自动获取neighbours。 200个点差不多在半分钟内算完 let lastIndex, inited;//标记上次查询到哪,防止重新sortByScore return function( interacted){ if(!this.currentPano || viewer.mainViewport.view.isFlying() || viewer.lastFrameChanged || viewer.inputHandler.drag /* interacted */){ //拖拽时不更新,否则移动端卡 return lastIndex = 0; } let nearPanos = this.tileDownloader.tilePrioritizer.nearPanos; if(!nearPanos)return; //let startTime = Date.now() let panos = [this.currentPano, ...nearPanos ]; this.depthSampler.updateNearPanos(panos); let maxWaitDur = browser.isMobile() ? 40 : 60; let changeCount = 0, maxChangeTex = browser.isMobile() ? 1 : 3, getCount = 0; let changeTexCount = ()=>{ changeCount ++; }; let median = Potree.timeCollect.depthSamChangeImg.median; let ifOverTime = ()=>{ let is = changeCount >= maxChangeTex || changeCount * median + getCount * 0.01 > maxWaitDur;//不换贴图也要一丢丢计算时间 /* if(is){ console.log('OverTime, changeCount', changeCount) } */ return is }; this.depthSampler.addEventListener('changeImg', changeTexCount); outer: for(let i=lastIndex,j=panos.length; i0)break; //点云的情况下最好不改相机位置,因为其他位置点云还没加载完,所以不判断当前点以外的漫游点 var g = Common.sortByScore(others, [ ], [ Images360.scoreFunctions.distanceSquared(pano.position) ]); for(let a=0, b=g.length; a{ //console.log('loadedDepthImg',e.pano) lastIndex = 0; //主要针对刚打开场景第一个点有杂点时单向不成功,要立即进一步双向计算ifShelter }); } /* let costTime = Date.now() - startTime costTime > maxWaitDur && console.log( 'costTime:',costTime) */ } }(); Images360.filters = { inPanoDirection : function(pos, dir, i, log) { //pano在mouse的方向上 return function(pano) { var r = pano.floorPosition.clone().sub(pos).normalize(); var o = pano.position.clone().sub(pos).normalize(); log && console.log('dire',pano.id, r.dot(dir), o.dot(dir) ); return r.dot(dir) > i || o.dot(dir) > i } }, inFloorDirection: function(pos, dir, min, log) { //pano在mouse的水平方向上 return function(pano) { var vec = new Vector2().subVectors(pano.floorPosition, pos).normalize(); var dir_ = new Vector2().copy(dir).normalize(); log && console.log('dire', pano.id, vec.dot(dir_) ); return vec.dot(dir_) > min /* var i = pano.floorPosition.clone().sub(pos).setZ(0).normalize();//改成在xz方向上,否则点击墙面不会移动 return i.dot(dir.clone().setZ(0)) > min */ } }, isNotBehindNormal: function(e, t) { var i = new Vector3; return t = t.clone(), function(n) { var r = i.copy(n.position).sub(e).normalize(); return r.dot(t) > 0 } }, isCloseEnoughTo: function(e, t) { return function(i) {//因为marker可能比地面高,所以识别范围要比marker看起来更近一些。(因为投影到地板的位置比marker更近) return e.distanceTo(i.floorPosition) < t //许钟文 } }, not: function(e) { return function(t) { return t !== e } } , isEnabled:function() { return function(t) { return t.enabled } }, isVisible:function() { return function(t) { return t.visible } } }; Images360.scoreFunctions = { direction: function(curPos, dir, ifLog) { return function(pano) { var pos1 = /* pano.floorPosition */ pano.position; //旧:改为权重放在marker上,这样对有斜坡的更准确,如上楼, 但这样近距离的pano角度就会向下了,以致于走不到 var n = pos1.clone().sub(curPos).normalize(); //ifLog && console.log('direction', pano.id, n.dot(dir) * directionFactor ) return n.dot(dir) * directionFactor } }, distance: function(pos1, r=1, ifLog) { if(pos1.position)pos1 = pos1.position; return function(pano) {//许钟文 改 var pos2 = pano.position.clone(); //ifLog && console.log('distance', pano.id, pos1.distance(pos2) * -1 ) return pos1.distanceTo(pos2) * -1 * r; } }, distanceSquared: function(pos1, r=1 ) { if(pos1.position)pos1 = pos1.position; return function(pano) {//许钟文 改 var pos2 = pano.position.clone(); return pos1.distanceToSquared(pos2) * -1 * r; } }, angle: function(e, t) { return function(i) { var n = i.position.clone().sub(e).normalize(); return n.angleTo(t) * Potree.config.navigation.angleFactor } }, }; Images360.sortFunctions = {//排序函数,涉及到两个item相减 floorDisSquaredToPoint: function(e) { return function(t, i) { return t.floorPosition.distanceToSquared(e) - i.floorPosition.distanceToSquared(e) } }, disSquaredToPoint: function(e) { return function(t, i) { return t.position.distanceToSquared(e) - i.position.distanceToSquared(e) } }, }; Images360.prototype.updateCube = (function(){//增加细分的版本,且垂直方向上取中位数 侧边多条 const minDis = 0.2; //pano和墙距离不能小于相机的near const height = 1; //准确计算的话要分别计算两个pano的地面、天花板之间的俯仰角,和pano之间的还不一样。这里统一假设一个比较小的高度,因高度越大 maxSinAlpha 越大 let minTanBeta = minDis / height; /* (pano0.position.z - pano0.floorPosition.z) */ let minBeta = Math.atan(minTanBeta); const maxSinAlpha = Math.cos(minBeta); // 注:beta = Math/2 - alpha const skyHeight = 50; return function(pano0, pano1){ if(Potree.settings.displayMode != 'showPanos' || pano0 == pano1 || this.cubePanos.includes(pano0) && this.cubePanos.includes(pano1) ) return this.cubePanos = [pano0, pano1]; viewer.addTimeMark('updateCube','start'); //console.log('updateCube',pano0.id, pano1&&pano1.id) let useBound = (bound, size)=>{ size = size || bound.getSize(new Vector3); let center = bound.getCenter(new Vector3); size.max(new Vector3(HighMapCubeWidth,HighMapCubeWidth,HighMapCubeWidth)); this.cube.geometry = new BoxBufferGeometry(1,1,1,1); this.cube.scale.copy(size); this.cube.position.copy(center); if(Potree.settings.testCube){ this.cubeTest.geometry = this.cube.geometry; this.cubeTest.scale.copy(size); this.cubeTest.position.copy(center); } return 'useBound' }; let getPanoBound = (pano, dis)=>{//因漫游点可能在点云外部,如室外平地,所以需要union进漫游点 let panoBound = new Box3; panoBound.expandByPoint(pano.position); let margin = dis ? Math.max(dis * 0.1, 10) : 10; panoBound.expandByVector(new Vector3(margin,margin,margin)); //give pano a margin 。 随着bound扩大,margin要相应扩大,否则当距离很远时看起来就像没有 return pano.pointcloud.bound.clone().union(panoBound) }; this.cube.geometry.dispose(); if(pano1){//过渡 let dontAddSides; let dis = pano0.position.distanceTo(pano1.position); if(dis == 0)return let sinAlpha = Math.abs(pano0.position.z - pano1.position.z) / dis; //俯仰角的sin,随角度增大而增大 0-1 let score = (1+sinAlpha*20) * dis; //score越大创建的mesh越不适合 let isNeighbour = this.isNeighbour(pano0, pano1); //console.log(pano0.id, pano1.id, maxSinAlpha.toFixed(2), sinAlpha.toFixed(2), score.toFixed(2), isNeighbour) if(sinAlpha>maxSinAlpha || !pano0.pointcloud.hasDepthTex || !pano1.pointcloud.hasDepthTex || (isNeighbour ? score > 100 : score > 50 ) ){ let bound = getPanoBound(pano0, dis).union(getPanoBound(pano1, dis)); let size = bound.getSize(new Vector3); let max = Math.max(size.x, size.y, size.z); size.set(max,max,max); //距离太远的数据集,过渡会畸变。所以扩大skybox,且为立方体 return useBound(bound, size) }else if( isNeighbour ? score > 70 : score > 15 ){ dontAddSides = true; //pano间有阻挡时得到的side点可能使通道变窄,所以去掉。 } //俯仰角增大时可能不在同一楼层,算出来的mesh不太好,所以更倾向直接使用cube,或去除side。 let half = browser.isMobile() ? 4 : 8; //自行输入 (点云计算的慢,还不准) let count1 = 2*half;//偶数个 每个pano向 外dir 个数 //奇数个的好处:在窄空间内能探测到最远距离,坏处是前方有尖角。偶数个的坏处就是可能检测距离太近。 let getDir = (angle_, vec)=>{ //旋转获得水平向量 let rotMat = new Matrix4().makeRotationZ(angle_); return vec.clone().applyMatrix4(rotMat) }; let getFar = (dir, pano, origin, height)=>{//获取在这个方向上和墙体intersect的距离 //在垂直方向上分出多个方向,取一个最可能的接近真实的距离 let maxH = 40, minH = 2, minR = 0.5, maxR = 2; height = height == void 0 ? Math.min(skyHeight, pano.ceilZ - pano.floorPosition.z) : height; //let r = height (maxH - minH)* 0.14 // 高度越小,角度越小 //let r = minR + ( maxR - minR) * THREE.Math.clamp((height - minH) / (maxH - minH),0,1) //THREE.Math.smoothstep(currentDis, op.nearBound, op.farBound); let r = math.linearClamp(height, [minH,maxH], [minR, maxR]); let getZ = (deg)=>{ deg *= r; deg = MathUtils.clamp(deg, 1, 80); return Math.tan(MathUtils.degToRad(deg)) }; let dirs_; //注意:角度太大会碰到天花板或地板,越远越容易碰到, 在地下停车场就会伸展不开。 更多取接近0度的,否则围墙矮一点就会取到远处。 dirs_ = [15, 8, 3, -1, -5]; dirs_ = dirs_.map(deg=> dir.clone().setZ(getZ(deg)).normalize() ); let max = 50; let count2 = dirs_.length; let disArr = dirs_.map((dir_, i) =>{ let intersect = this.getIntersect(pano, dir_, origin); let projectLen = intersect && intersect.distance ? dir_.dot(dir)*intersect.distance : max; //得到project在dir的长度 return projectLen //得水平距离 }); //console.log(pano ? pano.id : 'side','disArr', disArr.slice(0)) disArr.sort((a,b)=>{return b-a}); //从大到小 //console.log(pano ? pano.id : 'side','disArr', disArr) let dis = disArr[Math.floor(count2/2-0.5)]; //对半、取前(中位数) return dis }; let sideCount = [0,0]; let addPos = (pano, vec )=>{//添加这个pano这一侧向外半圆的顶点 //添加pano位置对应的最高点最低点: let minZ, maxZ; minZ = pano.floorPosition.z; maxZ = pano.getCeilHeight(); if(maxZ == Infinity) maxZ = skyHeight + pano.position.z; //maxZ = Math.max(skyHeight, maxZ) [maxZ, minZ ].forEach(z=>{ posArr.push(pano.position.clone().setZ(z)); }); //在画面上线条从左往右数 const angle = Math.PI/(count1-1); const dirs = []; //平分这半边180度 for(let i=0;i{ return { dir, dis: getFar(dir, pano) } }); //剔除那些突然间离相机很近的dir。有可能是拍摄的人、或者杆子、树 /* dirs2.forEach((e,i)=>{ console.log(i, e.dis) let smallThanBefore = ()=>{ return dirs2[i-1].dis / e.dis > maxRatio } let smallThanAfter = ()=>{ return dirs2[i+1].dis / e.dis > maxRatio } if(i>0 && i{ start+=1; //不包含start和end let count = end - start ; let dis = 0; for(let m=start;m{ //不包含start start+=1; //不包含start和end for(let m=start;mstart && dirs2[i].dis / dirs2[j].dis > maxRatio1){ ratios += dirs2[i].dis / dirs2[j].dis; j--; } let count = i-j-1; ratios /= count; if(count > 0 && computeWidth(j,i)< minWidth * ratios / maxRatio2 ){ //怎么感觉好像改成了视觉宽度小于某个值即可,那直接用count好了? changeDis(j,i); start = i; //在此之前的修改过,之后不用再判断 } /* if(count > 0 && (count == 1 || computeWidth(j,i){ let dir = e.dir.clone().multiplyScalar(e.disB || e.dis); [maxZ,minZ].forEach(z=>{ posArr.push(pano.position.clone().setZ(z).add(dir)); //获取到外墙点 }); }); }; let addSide = ()=>{//两个漫游点间两边各加一些侧线 //中点处的 let top0 = pano0.ceilZ == Infinity ? pano0.position.z+skyHeight : pano0.ceilZ; let top1 = pano1.ceilZ == Infinity ? pano1.position.z+skyHeight : pano1.ceilZ; let midMaxZ = (top0 + top1)/2; let midMinZ = (pano0.floorPosition.z+pano1.floorPosition.z)/2; let mid = new Vector3().addVectors(pano0.position, pano1.position).multiplyScalar(0.5); if(!dontAddSides){ if( pano0.pointcloud.hasDepthTex && pano0.pointcloud.hasDepthTex){ let panos = [pano0,pano1]; let vecs = [vec.clone().negate(), vec]; let axis = [[-1,1],[1,-1]]; let dis2d = new Vector2().subVectors(pano0.position, pano1.position).length();//水平上的距离 let maxDis = 50, minDis = 0.5, minR = 0.2, maxR = 1.2; //let r = maxR - ( maxR - minR) * THREE.Math.clamp((dis2d - minDis) / (maxDis - minDis),0,1) //dis2d越大,角度要越小 //THREE.Math.smoothstep(currentDis, op.nearBound, op.farBound); let r = math.linearClamp(dis2d, [minDis,maxDis], [maxR, minR]); //console.log('dis2d',dis2d,'r',r) let angles = ((browser.isMobile()/* || dis2d<4 */)? [60] : [50,70] /* [35,65] */).map(deg=>{ //正的在左边 尽量能够平分中间这段墙体。 (角度为从中心向外) let angle = MathUtils.clamp(deg * r, 5, 80); //console.log('angle',angle) return MathUtils.degToRad(angle) }); axis.forEach((axis_, index0)=>{ let disToSides = []; let accordingPano = index0 == 0 ? pano0 : pano1; //根据离该点在vec方向上的距离顺序来存顶点 panos.forEach((pano,index)=>{ let dirs = angles.map(angle=>getDir(axis_[index]*angle, vecs[index]));//一侧的若干角度 dirs.forEach((dir_,i)=>{ let dis1 = getFar(dir_, pano); let disToPano2d = dis1 * Math.cos(angles[i]); if(disToPano2d{return b-a});//从大到小 //由距离accordingPano的近到远: disToSides.sort((a,b)=>{return a.disToPano2d-b.disToPano2d}); //console.log('disToSides', index0, disToSides) if(disToSides.length == 1 && disToSides[0].disToSide < 0.5){ disToSides = []; //如果太近直接去除 } disToSides.forEach(e=>{//求z let ratio = e.disToPano2d / dis2d; let r = accordingPano == pano0 ? (1-ratio) : ratio; let sideMaxZ_ = top0 * r + top1 * (1-r); let sideMinZ_ = pano0.floorPosition.z * r + pano1.floorPosition.z * (1-r); [sideMaxZ_,sideMinZ_].forEach(z=>{ posArr.push(e.pano.position.clone().setZ(z).add(e.dir_)); //是直接使用最长dis的那个intersect点好还是mid }); }); } sideCount[index0] = disToSides.length; //记录侧边个数 }); }else { //这段针对点云时,仅测试才会执行到 sideCount = [1,1]; let sideDirs = [getDir(Math.PI/2, vec), getDir(-Math.PI/2, vec)]; sideDirs.forEach((dir_ ,index)=>{ let dis = getFar(dir_, null, mid, midMaxZ-midMinZ); //直接从中点求两侧的距离 dir_.multiplyScalar( /* Math.max( */dis/* , sideDis[index]) */ ); [midMaxZ,midMinZ].forEach(z=>{ posArr.push(mid.clone().setZ(z).add(dir_)); }); }); } } //中心: [midMaxZ,midMinZ].forEach(z=>{ posArr.push(mid.clone().setZ(z)); }); }; //positions存放顺序:pano的每边的 zMax和zMin 、count1个dir的点 ;侧边的点 ;连接处顶底的中点 let addFaces = ()=>{ let getPI = function(index, posType, panoIndex){//获取顶点序号 return 2 + (count1*2 + 2 ) * panoIndex + index*2 + (posType == 'top' ? 0 : 1) }; let getSidePI = function(index, posType, panoIndex){//获取侧边顶点序号 if(panoIndex == 1) index += sideCount[0]; return getPI(index, posType, 2)-2 }; let getPanoPI = function(posType, panoIndex){//获取pano处对应的点序号 return getPI(-1, posType, panoIndex) }; let topCenter = posArr.length-2; //最后添加的两个中心点 let btmCenter = posArr.length-1; for(let i=0;i<2;i++){ for(let index=1; index{ let needUpdateCursor; if(e.viewport == viewer.mainViewport){ needUpdateCursor = true; }else if(e.viewport == this.viewports[0]){//attachedToViewer needUpdateCursor = true; this.mapChanged = true; this.updateWhenAtViewer(); e.changeInfo.projectionChanged && this.setViewLimit(); } needUpdateCursor && this.updateCursor(); }); this.addEventListener("camera_changed", (e)=>{ e.changeInfo.projectionChanged && this.setViewLimit(); }); //viewer.addEventListener("global_mousemove", this.updateWhenAtViewer.bind(this)) //鼠标移动时reticule也动,所以直接就needRender /* viewer.reticule.addEventListener('update',(e)=>{ if(this.attachedToViewer)this.needRender = true }) */ viewer.scene.addEventListener("360_images_added", this.addPanos.bind(this)); viewer.addEventListener("loadPointCloudDone", this.initProjection.bind(this)); this.addEventListener('global_click',(e)=>{ if(!e.isTouch && e.button != MOUSE.LEFT)return this.updateClosestPano(e.intersect); }); this.addEventListener('add',(e)=>{//添加其他mesh this.scene.add(e.object); if(e.name == 'route'){ e.object.position.z = routeLayerHeight; } Potree.Utils.setObjectLayers(e.object, 'mapObjects' ); }); viewer.addEventListener('allLoaded',()=>{ this.setViewLimit('standard'); }); if(!Potree.settings.isOfficial){ let domRoot = viewer.renderer.domElement.parentElement; let elAttach = $(""); elAttach.css({ position : "absolute", right : '80%', bottom: '20px', zIndex: "10000", fontSize:'1em', color:"black", background:'rgba(255,255,255,0.8)', }); let state = false; elAttach.on("click", () => { state = !state; this.attachToMainViewer(state, 'measure'); elAttach.val(state ? 'map分离':'map绑定'); }); domRoot.appendChild(elAttach[0]); } } get needRender(){ return this.needRender_ } set needRender(n){ this.needRender_ = n; n && (this.viewports[0].needRender = true); //使attachedToViewer时在renderDefault中可渲染 } get mapChanged(){ return this.mapChanged_ } set mapChanged(c){//镜头移动、地图内容改变都会为true this.mapChanged_ = c; c && (this.needRender = true); } waitLoadDone(callback){//确保加载完后执行 let timer; //等待一段时间看有没有新加载的tile,如果超过这个时间没有就不等了,算加载结束 let pauseCountDown = ()=>{//重新等待加载结束,中断倒计时 clearTimeout(timer); //console.log('pauseCountDown') }; let freshCountDown = ()=>{//刷新倒计时 //console.log('freshCountDown') clearTimeout(timer); timer = setTimeout(()=>{ this.mapLayer.removeEventListener('loadDone', freshCountDown); this.mapLayer.removeEventListener('startLoad', pauseCountDown); callback(); }, document.hidden ? 5000 : 500); }; this.mapLayer.addEventListener('loadDone', freshCountDown); this.mapLayer.addEventListener('startLoad', pauseCountDown); if(this.mapLayer.loadingInProgress == 0){ freshCountDown(); } } addListener(images360){ images360.addEventListener('flyToPano',e=>{ let toPano = e.toPano; if(toPano.dontMoveMap) return /* transitions.start(lerp.vector(this.view.position, toPano.pano.position.clone().setZ(cameraHeight), (pos, progress)=>{ }), toPano.duration, null, 0, easing[toPano.easeName] ); */ let boundSize;// = new THREE.Vector2(10,10) this.moveTo(toPano.pano.position.clone().setZ(Potree.config.map.cameraHeight), boundSize, toPano.duration, null, toPano.easeName); }); } initProjection(){ this.started = true; this.mapLayer.initProjection(); } initScene(){ let w = initCameraFeildWidth; let width = this.renderArea.clientWidth; let height = this.renderArea.clientHeight; //let aspect = width / height this.camera = new OrthographicCamera(-width/2,width/2,height/2,-height/2/* -w/2, w/2, w/2/aspect, -w/2/aspect */, 0.01, 10000); this.camera.zoom = width / w; //zoom越大视野越小 //this.camera.position.set(0,0,10); this.camera.up.set(0,0,1); //this.camera.lookAt(new THREE.Vector3()) //this.camera.updateMatrixWorld() this.view = new ExtendView(); this.view.position.set(0,0,Potree.config.map.cameraHeight); this.view.lookAt(0,0,0); let viewport = new Viewport( this.view, this.camera, { left:0, bottom:0, width:1, height: 1, name:'mapViewport' }); viewport.axis = ["x","y"]; viewport.axisSign = [1,1]; viewport.noPointcloud = true; //不要渲染点云 viewport.render = this.render.bind(this);//标志给mainView渲染 //viewport.unableDepth = true //depthBasicMaterial等在此viewport中不开启depth viewport.addEventListener('resize',()=>{ this.copyBuffer.setSize(viewport.resolution2.x, viewport.resolution2.y); }); this.viewports = [viewport]; this.controls = new FirstPersonControls(this, this.viewports[0]); this.controls.setEnable(true); this.scene = new Scene(); this.inputHandler = new InputHandler(this, this.scene); this.inputHandler.name = 'mapInputHandler'; //this.inputHandler.addInputListener(this.controls); this.inputHandler.registerInteractiveScene(this.scene);//interactiveScenes this.viewports[0].interactiveScenes = this.inputHandler.interactiveScenes;//供viewer的inputHandler使用 var cursor = new Mesh(planeGeo$1, new MeshBasicMaterial({ transparent:true, opacity:0.9, depthTest : false, //防止透明冲突 map: texLoader$5.load(Potree.resourcePath+'/textures/pic_location128.png' ) })); cursor.position.set(0,0,cursorHeight); this.cursor = cursor; this.scene.add(cursor); Potree.Utils.setObjectLayers(this.scene, 'mapObjects' ); } setViewLimit(state){//设置边界,当编辑空间模型等时要解禁 if(!state)state = this.limitBoundState; if(!state)return this.limitBoundState = state; let setting = Potree.config.OrthoCameraLimit[state]; if(setting){ this.camera.zoomLimit = $.extend({},setting.zoom); let lonlatCenter = viewer.transform.lonlatToLocal.inverse([0,0]); let minY = viewer.transform.lonlatToLocal.forward([lonlatCenter[0], -90+setting.latPad])[1]; //屏幕边界的bound let maxY = viewer.transform.lonlatToLocal.forward([lonlatCenter[0], 90-setting.latPad])[1]; /*this.view.limitBound = new THREE.Box3( new THREE.Vector3(setting.xBound[0], minY, Potree.config.map.cameraHeight), new THREE.Vector3(setting.xBound[1], maxY, 1 / 0) ) */ let halfHeight = this.viewports[0].resolution.y / 2 / this.camera.zoom;//屏幕所占高度的一半 let halfWidth = this.viewports[0].resolution.x / 2 / this.camera.zoom; this.view.limitBound = new Box3( new Vector3(setting.xBound[0]+halfWidth, minY+halfHeight, Potree.config.map.cameraHeight), new Vector3(setting.xBound[1]-halfWidth, maxY-halfHeight, 1 / 0) ); }else { this.view.limitBound = null; this.camera.zoomLimit = null; } } updateCursor(){ //console.log('pos', viewer.mainViewport.camera.position.toArray() ) var scale = math.getScaleForConstantSize( {//规定下最小最大像素 minSize : 80, maxSize : 200, nearBound : initCameraFeildWidth*0.1 , farBound : initCameraFeildWidth*2, camera:this.camera , position: this.cursor.getWorldPosition(new Vector3()), resolution: this.viewports[0].resolution//2 }); this.cursor.scale.set(scale, scale, scale);//当地图缩放时 this.cursor.position.copy(viewer.mainViewport.camera.position).setZ(cursorHeight);//当场景镜头旋转移动时 this.cursor.rotation.z = viewer.mainViewport.view.yaw; this.needRender = true; } addPanos(e){ var panosGroup = new Object3D; panosGroup.name = 'markers'; panoMarkerMats = { default: new MeshBasicMaterial({ transparent:true, opacity: 0.5, map: texLoader$5.load(Potree.resourcePath+'/textures/map_marker.png' ), }), selected: new MeshBasicMaterial({ transparent:true, opacity: 1, map: texLoader$5.load(Potree.resourcePath+'/textures/map_marker.png' ), }) }; e.images.panos.forEach(pano=>{ pano.mapMarker = new Mesh(planeGeo$1, panoMarkerMats.default); pano.mapMarker.position.copy(pano.position).setZ(0); pano.mapMarker.scale.set(markerSize,markerSize,markerSize); pano.mapMarker.name = 'mapMarker'; panosGroup.add(pano.mapMarker); let mouseover = (e)=>{ if(!e.byMap){ pano.mapMarker.material = panoMarkerMats.selected; if(!e.byMainView) pano.dispatchEvent({type: "hoverOn", byMap:true}); this.needRender = true; } }; let mouseleave = (e)=>{ if(!e.byMap){ pano.mapMarker.material = panoMarkerMats.default; if(!e.byMainView) pano.dispatchEvent({type: "hoverOff", byMap:true}); this.needRender = true; } }; pano.mapMarker.addEventListener('mouseover', mouseover); pano.mapMarker.addEventListener('mouseleave', mouseleave); pano.addEventListener('hoverOn', mouseover); pano.addEventListener('hoverOff', mouseleave); let onclick = (e)=>{ viewer.images360.flyToPano(pano); }; pano.mapMarker.addEventListener('click', onclick); pano.addEventListener('isVisible',(e)=>{//是否显示该点的mesh(不显示也能走) //console.log('panoMarker isVisible', pano.id, e.visible) Potree.Utils.updateVisible(pano.mapMarker, 'panoVisible', e.visible ); this.needRender = true; }); pano.addEventListener('rePos',(e)=>{ pano.mapMarker.position.copy(pano.position).setZ(0); }); }); this.scene.add(panosGroup); panosGroup.position.z = panosHeight; this.panosGroup = panosGroup; Potree.Utils.setObjectLayers(panosGroup, 'mapObjects' ); /* e.images.on('markersDisplayChange', (show)=>{ panosGroup.visible = show this.needRender = true }) */ //------- //this.fitPanosToViewport() this.initFitView(); } updateClosestPano(intersect){ if(viewer.images360.flying)return; intersect = intersect && intersect.orthoIntersect; if(!intersect)return intersect = intersect.clone().setZ(0); const minDis = 20; //距离鼠标不能太远 var filterFuncs = [ Images360.filters.isEnabled(), Images360.filters.isVisible(),//只走显示的点,否则会走到别的层 (pano)=>{ return pano.position.clone().setZ(0).distanceTo(intersect) < minDis }, ]; var pano = Common.find(viewer.images360.panos, filterFuncs, [Images360.sortFunctions.floorDisSquaredToPoint(intersect)]); if (pano && pano != viewer.images360.currentPano ) { viewer.images360.flyToPano(pano); } } fitPanosToViewport(){//使所有漫游点占满viewport //var w = viewer.bound.boundSize.x; var boundSize = viewer.images360.bound.size.clone().multiplyScalar(1.1); boundSize.max(new Vector3(4,4,4)); let endPosition = viewer.images360.bound.center.clone(); this.moveTo(endPosition, boundSize, 0); } fitToPointcloud(pointcloud, duration=400){ var boundSize = pointcloud.bound.getSize(new Vector3);/* .multiplyScalar(1.1); */ boundSize.max(new Vector3(4,4,4)); let endPosition = pointcloud.bound.getCenter(new Vector3); this.moveTo(endPosition, boundSize, duration); //给点duration去变化 否则地图放大后所占的还是很小 } initFitView(){ let dis = 5 , px = 70; //地图上px像素长度代表的距离为dis //px是手动缩放到5m后发现是这个长度 let zoom = px / dis; this.camera.zoom = zoom; this.moveTo(viewer.images360.position/* viewer.images360.bound.center */); this.camera.updateProjectionMatrix(); } fitToDatasets(datasets){ let bound = new Box3; datasets.forEach(e=>bound.union(e.bound)); let center = bound.getCenter(new Vector3); let size = bound.getSize(new Vector3); this.moveTo(center, size, 200 ); //给duration是为了顺应视口大小改变,缓冲 } moveTo(endPosition, boundSize, duration=0, margin, easeName, callback ){//前两个参数有xy即可 endPosition = new Vector3(endPosition.x,endPosition.y,Potree.config.map.cameraHeight); this.view.moveOrthoCamera(this.viewports[0], {endPosition, boundSize, margin, callback}, duration, easeName); /* let endZoom, startZoom = this.camera.zoom //修改相机为bound中心,这样能看到全部(宽度范围内) this.view.setView({ position:endPosition, duration, callback:()=>{//done }, onUpdate:(progress)=>{ if(boundSize){ let aspect = boundSize.x / boundSize.y let w, h; if(this.camera.aspect > aspect){//视野更宽则用bound的纵向来决定 h = boundSize.y //w = h * this.camera.aspect endZoom = this.viewports[0].resolution.y / h }else{ w = boundSize.x; //h = w / this.camera.aspect endZoom = this.viewports[0].resolution.x / w } //onUpdate时更新endzoom是因为画布大小可能更改 this.camera.zoom = endZoom * progress + startZoom * (1 - progress) this.camera.updateProjectionMatrix() } }, Easing:easeName }) */ } updateWhenAtViewer(e){//两个触发来源: 1 camera_changed时 2 mapLayer.needUpdate时。 render在viewer中执行 if(this.attachedToViewer){ if(this.started) this.mapLayer.update(); this.needRender = true; } } update(delta, areaSize ){ if(!this.visible && !this.attachedToViewer )return if(this.attachedToViewer){ if(this.mapLayer.needUpdate){ this.updateWhenAtViewer(); } return } this.updateScreenSize(); this.controls.update(delta); this.view.applyToCamera(this.camera); let changed = this.cameraChanged(); if(this.started && (changed || this.mapLayer.needUpdate))this.mapLayer.update(); if(changed /*|| || this.needRender */){ /* this.dispatchEvent({ type: "camera_changed", camera: this.camera, viewport : this.viewports[0] }) */ this.mapChanged = true; this.needRender = true; this.updateCursor();//更改大小 } this.render(); } attachToMainViewer(state, desc, mapRatio=0.5, options={}){//转移到viewer中。测量时展示or截图需要 if(!Potree.settings.isOfficial)this.renderArea.style.display = state ? 'none':'block'; if(state){ this.enabledOld = this.enabled; this.enabled = true; if(mapRatio != 'dontSet'){ this.changeSplitScreenDir(options.dir, mapRatio); if(this.attachedToViewer){ //this.fitPanosToViewport() viewer.updateScreenSize({forceUpdateSize:true}); return } viewer.viewports = [viewer.mainViewport, viewer.mapViewer.viewports[0] ];//因为mainViewer的相机变化会触发map的变化,所以先渲染mainViewer } if(desc == 'measure') this.inputHandler.registerInteractiveScene(viewer.measuringTool.scene);//虽然用的是viewer的inputHandler,但借用了this.inputHandler的interactiveScenes else if(desc == 'split4Screens') { this.inputHandler.registerInteractiveScene(viewer.scene.scene); } }else { if(!this.attachedToViewer)return viewer.mainViewport.left = 0; viewer.mainViewport.bottom = 0; viewer.mainViewport.width = 1; viewer.mainViewport.height = 1; this.viewports[0].width = 1; this.viewports[0].bottom = 0; this.viewports[0].height = 1; this.viewports[0].left = 0; this.renderMeasure || this.inputHandler.unregisterInteractiveScene(viewer.measuringTool.scene); this.inputHandler.unregisterInteractiveScene(viewer.scene.scene); viewer.viewports = [viewer.mainViewport]; this.updateScreenSize({forceUpdateSize:true}); //更新相机projectionMatrix } //if(desc == 'measure')this.renderMeasure = state this.attachedToViewer = state; viewer.updateScreenSize({forceUpdateSize:true}); //mapRatio != 'dontSet' && !options.dontFit && this.moveTo(...)//要写在updateScreenSize后面,因为要根据视图大小来fit if(options.moveToCurrentPos){ let boundSize = new Vector2(10,10); let duration = 1000; this.moveTo(viewer.images360.position.clone(), boundSize, duration); } this.needRender = true; } setDrawMeasure(draw){ this.renderMeasure = !!draw; if(draw){ this.inputHandler.registerInteractiveScene(viewer.measuringTool.scene); }else { this.inputHandler.unregisterInteractiveScene(viewer.measuringTool.scene); } } changeSplitScreenDir(dir, mapRatio){//左右 | 上下 //if(!dir || dir == this.dir)return if(dir )this.splitDir = dir; this.updateSplitSize(mapRatio); /* if(this.attachedToViewer){ //如果已经分屏了,中途修改方向的话…… this.updateSplitSize() } */ } updateSplitSize(mapRatio){ if(mapRatio != void 0) this.mapRatio = mapRatio; //console.log(this.mapRatio) if(this.splitDir == 'leftRight'){//地图在左方 viewer.mainViewport.left = this.mapRatio; viewer.mainViewport.width = 1-this.mapRatio; this.viewports[0].width = this.mapRatio; viewer.mainViewport.bottom = this.viewports[0].bottom = 0; viewer.mainViewport.height = this.viewports[0].height = 1; }else if(this.splitDir == 'upDown'){ //地图在下方 viewer.mainViewport.bottom = this.mapRatio; viewer.mainViewport.height = 1-this.mapRatio; this.viewports[0].height = this.mapRatio; viewer.mainViewport.left = this.viewports[0].left = 0; viewer.mainViewport.width = this.viewports[0].width = 1; } if(this.attachedToViewer){ viewer.updateScreenSize({forceUpdateSize:true}); } } render1(params={}){//viewer的preserveDrawingBuffer为false时的版本 let needCopy, waitCopy; if(!this.visible && !this.attachedToViewer || !this.needRender && !params.force){ if(this.attachedToViewer ){ needCopy = true; }else { return } } waitCopy = this.attachedToViewer && this.needRender && !params.force;//是否写入到copyBuffer。双屏时,若needRender就拷贝到copyBuffer中,双屏时就直接使用copyBuffer。 四屏时因渲染点云会每帧都渲染,所以不需要缓存。 let renderer = params.renderer || this.renderer; if(waitCopy){ this.copyBuffer.setSize(params.viewport.resolution2.x, params.viewport.resolution2.y); renderer.setRenderTarget(this.copyBuffer); }else if(params.target){ renderer.setRenderTarget(params.target); } /* if(params.resize){ this.emitResizeMsg(new THREE.Vector2(params.width,params.height, viewport:params.viewport)) } */ params.clear ? params.clear() : renderer.clear(); if(!needCopy || waitCopy){//重绘 viewer.dispatchEvent({type: "render.begin", viewer: this, viewport:this.viewports[0], params }); Potree.Utils.setCameraLayers(this.camera, ['map','mapObjects' , 'bothMapAndScene' ]); if(this.mapGradientBG){//渲染背景 viewer.scene.cameraBG.layers.set(Potree.config.renderLayers.bg); renderer.render(viewer.scene.scene, viewer.scene.cameraBG); } renderer.render(this.scene, this.camera); renderer.render(viewer.scene.scene, this.camera); //测量线等 //params.renderOverlay && params.renderOverlay( $.extend({}, params, { isMap:true })) renderer.setRenderTarget(params.target||null); } if(needCopy || waitCopy){//使用缓存 ----当viewer的preserveDrawingBuffer为false的话,使用buffer this.copyPass.render(null,null, null, renderer, params.target||null, this.copyBuffer); } this.needRender = false; return true } clear(params){ if(this.transparentBG){ this.renderer.setClearColor( 0x000000, 0 ); }else { this.renderer.setClearColor( Potree.config.mapBG, 1 ); } (params.renderer || this.renderer).clear(); } //拆成两次渲染,一个地图一个其他物体,且地图渲染后保存在buffer中,只有当地图变化后才重渲染。 render(params={}){ if(!this.visible && !this.attachedToViewer || !this.needRender && !params.force){ //注意:mapViewer.needRender的权重高于它的viewport的needRender,也就是说,当attachedToViewer时,viewer即使needRender, mapViewer也不一定会渲染。 return } viewer.addTimeMark('mapRender','start'); let renderer = params.renderer || this.renderer; if(this.mapChanged){ //渲染地图背景 renderer.setRenderTarget(this.copyBuffer); params.clear ? params.clear(params) : this.clear(params); if(this.mapGradientBG){//渲染背景 viewer.scene.cameraBG.layers.set(Potree.config.renderLayers.bg); renderer.render(viewer.scene.scene, viewer.scene.cameraBG); } Potree.Utils.setCameraLayers(this.camera, ['map']); renderer.render(this.scene, this.camera); params.renderBG && params.renderBG(this.viewports[0]); this.mapChanged = false; renderer.setRenderTarget(params.target||null); } params.clear ? params.clear(params) : this.clear(params); this.copyPass.render(null,null, null,renderer, params.target||null, this.copyBuffer); //拷贝地图背景 renderer.clearDepth(); //防止地图遮挡其他物体 //绘制其他物体 Potree.Utils.setCameraLayers(this.camera, ['mapObjects' , 'bothMapAndScene' ]); viewer.dispatchEvent({type: "render.begin", viewer: this, viewport:this.viewports[0], params }); this.attachedToViewer || renderer.render(viewer.scene.scene, this.camera); //类同renderOverlay renderer.render(this.scene, this.camera); if(!this.attachedToViewer && this.renderMeasure){//在未attach到主页面时也要渲染测量线 viewer.dispatchEvent({type: "render.pass.perspective_overlay", camera:this.camera, /* screenshot:params.screenshot, */viewport:this.viewports[0], renderer}); } renderer.setRenderTarget(null); this.needRender = false; viewer.addTimeMark('mapRender','end'); return true } renderOverlay(){//偶尔需要绘制测量线 viewer.renderOverlay({ camera: this.camera, viewport:this.viewports[0], renderer:this.renderer }); } } /* 渲染顺序: 地图 背景Overlay 地图scene的物体,如cursor、 marker 点云(如果有) overlay,两层:第一层:viewer的scene中bothMapAndScene的如reticule. 第二层:如测量线(attachToMainViewer时才渲染) */ //本地调试地图白屏是因为代码自动更新了 但没刷新 class U { static toVector3(v, offset) { return new Vector3().fromArray(v, offset || 0); } static toBox3(b) { return new Box3(U.toVector3(b), U.toVector3(b, 3)); }; static findDim(schema, name) { var dim = schema.find((dim) => dim.name == name); if (!dim) throw new Error('Failed to find ' + name + ' in schema'); return dim; } static sphereFrom(b) { return b.getBoundingSphere(new Sphere()); } }; class PointCloudEptGeometry { constructor(url, info) { let version = info.version; let schema = info.schema; let bounds = info.bounds; let boundsConforming = info.boundsConforming; let xyz = [ U.findDim(schema, 'X'), U.findDim(schema, 'Y'), U.findDim(schema, 'Z') ]; let scale = xyz.map((d) => d.scale || 1); let offset = xyz.map((d) => d.offset || 0); this.eptScale = U.toVector3(scale); this.eptOffset = U.toVector3(offset); this.url = url; this.info = info; this.type = 'ept'; this.schema = schema; this.span = info.span || info.ticks; this.boundingBox = U.toBox3(bounds); this.tightBoundingBox = U.toBox3(boundsConforming); this.offset = U.toVector3([0, 0, 0]); this.boundingSphere = U.sphereFrom(this.boundingBox); this.tightBoundingSphere = U.sphereFrom(this.tightBoundingBox); this.version = new Potree.Version('1.7'); this.projection = null; this.fallbackProjection = null; if (info.srs && info.srs.horizontal) { this.projection = info.srs.authority + ':' + info.srs.horizontal; } if (info.srs.wkt) { if (!this.projection) this.projection = info.srs.wkt; else this.fallbackProjection = info.srs.wkt; } { // TODO [mschuetz]: named projections that proj4 can't handle seem to cause problems. // remove them for now try{ proj4(this.projection); }catch(e){ this.projection = null; } } { const attributes = new PointAttributes(); attributes.add(PointAttribute.POSITION_CARTESIAN); attributes.add(new PointAttribute("rgba", PointAttributeTypes.DATA_TYPE_UINT8, 4)); attributes.add(new PointAttribute("intensity", PointAttributeTypes.DATA_TYPE_UINT16, 1)); attributes.add(new PointAttribute("classification", PointAttributeTypes.DATA_TYPE_UINT8, 1)); attributes.add(new PointAttribute("gps-time", PointAttributeTypes.DATA_TYPE_DOUBLE, 1)); attributes.add(new PointAttribute("returnNumber", PointAttributeTypes.DATA_TYPE_UINT8, 1)); attributes.add(new PointAttribute("number of returns", PointAttributeTypes.DATA_TYPE_UINT8, 1)); attributes.add(new PointAttribute("return number", PointAttributeTypes.DATA_TYPE_UINT8, 1)); attributes.add(new PointAttribute("source id", PointAttributeTypes.DATA_TYPE_UINT16, 1)); this.pointAttributes = attributes; } this.spacing = (this.boundingBox.max.x - this.boundingBox.min.x) / this.span; let hierarchyType = info.hierarchyType || 'json'; const dataType = info.dataType; if (dataType == 'laszip') { this.loader = new Potree.EptLaszipLoader(); } else if (dataType == 'binary') { this.loader = new Potree.EptBinaryLoader(); } else if (dataType == 'zstandard') { this.loader = new Potree.EptZstandardLoader(); } else { throw new Error('Could not read data type: ' + dataType); } } }; class EptKey { constructor(ept, b, d, x, y, z) { this.ept = ept; this.b = b; this.d = d; this.x = x || 0; this.y = y || 0; this.z = z || 0; } name() { return this.d + '-' + this.x + '-' + this.y + '-' + this.z; } step(a, b, c) { let min = this.b.min.clone(); let max = this.b.max.clone(); let dst = new Vector3().subVectors(max, min); if (a) min.x += dst.x / 2; else max.x -= dst.x / 2; if (b) min.y += dst.y / 2; else max.y -= dst.y / 2; if (c) min.z += dst.z / 2; else max.z -= dst.z / 2; return new Potree.EptKey( this.ept, new Box3(min, max), this.d + 1, this.x * 2 + a, this.y * 2 + b, this.z * 2 + c); } children() { var result = []; for (var a = 0; a < 2; ++a) { for (var b = 0; b < 2; ++b) { for (var c = 0; c < 2; ++c) { var add = this.step(a, b, c).name(); if (!result.includes(add)) result = result.concat(add); } } } return result; } } class PointCloudEptGeometryNode extends PointCloudTreeNode { constructor(ept, b, d, x, y, z) { super(); this.ept = ept; this.key = new Potree.EptKey( this.ept, b || this.ept.boundingBox, d || 0, x, y, z); this.id = PointCloudEptGeometryNode.IDCount++; this.geometry = null; this.boundingBox = this.key.b; this.tightBoundingBox = this.boundingBox; this.spacing = this.ept.spacing / Math.pow(2, this.key.d); this.boundingSphere = U.sphereFrom(this.boundingBox); // These are set during hierarchy loading. this.hasChildren = false; this.children = { }; this.numPoints = -1; this.level = this.key.d; this.loaded = false; this.loading = false; this.oneTimeDisposeHandlers = []; let k = this.key; this.name = this.toPotreeName(k.d, k.x, k.y, k.z); this.index = parseInt(this.name.charAt(this.name.length - 1)); } isGeometryNode() { return true; } getLevel() { return this.level; } isTreeNode() { return false; } isLoaded() { return this.loaded; } getBoundingSphere() { return this.boundingSphere; } getBoundingBox() { return this.boundingBox; } url() { return this.ept.url + 'ept-data/' + this.filename(); } getNumPoints() { return this.numPoints; } filename() { return this.key.name(); } getChildren() { let children = []; for (let i = 0; i < 8; i++) { if (this.children[i]) { children.push(this.children[i]); } } return children; } addChild(child) { this.children[child.index] = child; child.parent = this; } load() { if (this.loaded || this.loading) return; if (Potree.numNodesLoading >= Potree.maxNodesLoading) return; this.loading = true; ++Potree.numNodesLoading; if (this.numPoints == -1) this.loadHierarchy(); this.loadPoints(); } loadPoints(){ this.ept.loader.load(this); } async loadHierarchy() { let nodes = { }; nodes[this.filename()] = this; this.hasChildren = false; let eptHierarchyFile = `${this.ept.url}ept-hierarchy/${this.filename()}.json`; let response = await fetch(eptHierarchyFile); let hier = await response.json(); // Since we want to traverse top-down, and 10 comes // lexicographically before 9 (for example), do a deep sort. var keys = Object.keys(hier).sort((a, b) => { let [da, xa, ya, za] = a.split('-').map((n) => parseInt(n, 10)); let [db, xb, yb, zb] = b.split('-').map((n) => parseInt(n, 10)); if (da < db) return -1; if (da > db) return 1; if (xa < xb) return -1; if (xa > xb) return 1; if (ya < yb) return -1; if (ya > yb) return 1; if (za < zb) return -1; if (za > zb) return 1; return 0; }); keys.forEach((v) => { let [d, x, y, z] = v.split('-').map((n) => parseInt(n, 10)); let a = x & 1, b = y & 1, c = z & 1; let parentName = (d - 1) + '-' + (x >> 1) + '-' + (y >> 1) + '-' + (z >> 1); let parentNode = nodes[parentName]; if (!parentNode) return; parentNode.hasChildren = true; let key = parentNode.key.step(a, b, c); let node = new Potree.PointCloudEptGeometryNode( this.ept, key.b, key.d, key.x, key.y, key.z); node.level = d; node.numPoints = hier[v]; parentNode.addChild(node); nodes[key.name()] = node; }); } doneLoading(bufferGeometry, tightBoundingBox, np, mean) { bufferGeometry.boundingBox = this.boundingBox; this.geometry = bufferGeometry; this.tightBoundingBox = tightBoundingBox; this.numPoints = np; this.mean = mean; this.loaded = true; this.loading = false; --Potree.numNodesLoading; } toPotreeName(d, x, y, z) { var name = 'r'; for (var i = 0; i < d; ++i) { var shift = d - i - 1; var mask = 1 << shift; var step = 0; if (x & mask) step += 4; if (y & mask) step += 2; if (z & mask) step += 1; name += step; } return name; } dispose() { if (this.geometry && this.parent != null) { this.geometry.dispose(); this.geometry = null; this.loaded = false; // this.dispatchEvent( { type: 'dispose' } ); for (let i = 0; i < this.oneTimeDisposeHandlers.length; i++) { let handler = this.oneTimeDisposeHandlers[i]; handler(); } this.oneTimeDisposeHandlers = []; } } } PointCloudEptGeometryNode.IDCount = 0; 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 Sphere()); this.nodeMaxLevel = 0;//add if (!this.pcoGeometry.loader.version.equalOrHigher('1.5')) {//las 一级一级增长的,但是testNodeMaxLevel时需要大于0 this.nodeMaxLevel = 1;//add } this.maxLevel = Infinity; this.temp = { sizeFitToLevel:{}, opacity:{}};//add //add this.panos = []; this.matrixAutoUpdate = false; //最好禁止updateMatrix 直接使用matrixWorld this.orientationUser = 0; this.translateUser = new Vector3; this.rotateMatrix = new Matrix4; this.transformMatrix = new Matrix4;// 数据集的变化矩阵 this.transformInvMatrix = new Matrix4; /* this.transformMatrix2 = new THREE.Matrix4;// 数据集的变化矩阵 this.transformInvMatrix2 = new THREE.Matrix4; */ this.rotateInvMatrix = new 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); setTimeout(()=>{ this.setPointLevel();//重新计算 延迟是因为testNodeMax会变回旧的 },1); 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 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 pickWindowSize = params.pickWindowSize; //拾取像素边长,越小越精准,但点云稀疏的话可能容易出现识别不到的情况。 另外左下侧会有缝隙无法识别到,缝隙大小和这个值有关//突然发现pickWindowSize在一百以内的变化对pick费时影响甚微,1和100差1毫秒不到,但400时会多4毫秒 if(pickWindowSize == void 0){ if(Potree.settings.displayMode == 'showPanos'){ pickWindowSize = 50; }else { let r0 = this.nodeMaxLevel > 0 ? this.maxLevel/this.nodeMaxLevel : 0.5; pickWindowSize = MathUtils.clamp( Math.round((1.1-r0)*80), 15, 100); } } if(camera.type == 'OrthographicCamera'){ var cameraDir = new Vector3(0,0,-1).applyQuaternion(camera.quaternion); pickWindowSize *= 4; //pointsize比较大时截取太小会没多少点可以选 } let pickOutsideClipRegion = getVal(params.pickOutsideClipRegion, false); let size = viewport ? viewport.resolution : renderer.getSize(new 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); Potree.Common.downloadFile(dataUrl, 'screenshot.png'); 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 Scene(); let material = new ExtendPointCloudMaterial(); material.activeAttributeName = "indices";//indices let renderTarget = new WebGLRenderTarget( 1, 1, { minFilter: LinearFilter, magFilter: NearestFilter, format: 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.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 Vector2(width, height)); } pickState.renderTarget.setSize(width, height); //仅绘制屏幕大小的,而不乘以devicePixelRatio let pixelPos = new Vector2(params.x, params.y); let gl = renderer.getContext(); //规定渲染范围,只渲染一小块 /* renderer.setScissorTest(true); gl.enable(gl.SCISSOR_TEST); gl.scissor( parseInt(pixelPos.x - (pickWindowSize - 1) / 2), parseInt(pixelPos.y - (pickWindowSize - 1) / 2), parseInt(pickWindowSize), parseInt(pickWindowSize)); */ //---这段没用 pickState.renderTarget.scissor.set(parseInt(pixelPos.x - (pickWindowSize - 1) / 2), parseInt(pixelPos.y - (pickWindowSize - 1) / 2),parseInt(pickWindowSize), parseInt(pickWindowSize)); pickState.renderTarget.scissorTest = true; renderer.state.buffers.depth.setTest(pickMaterial.depthTest); renderer.state.buffers.depth.setMask(pickMaterial.depthWrite); renderer.state.setBlending(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); //只绘制ray pick到的部分nodes 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 0){//最后选取的是最靠近鼠标的那个pick if(distance < hits[0].distanceToCenter){ hits[0] = hit; } }else { hits.push(hit); } if(distance < rSquare) hits2.push(hit); //add } } } } if(!params.all && Potree.settings.pickFrontPointRatio){ 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 = $(` //
`); // $(document.body).append(this.debugDIV); // } // this.debugDIV.empty(); // this.debugDIV.append($(``)); // //$(this.debugWindow.document).append($(``)); // //this.debugWindow.document.write(''); // } 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 Vector3(x, y, z); position.applyMatrix4( pc.matrixWorld ); point[attributeName] = position; //add if(!params.all && Potree.settings.pickFrontPointRatio ){ if(camera.type == 'OrthographicCamera'){ let vec = new 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 { if(Potree.settings.pickFrontPointRatio == 0){//如果不需要选偏离镜头近的,就要离中心近的即可 //console.log(hits[0], hits2, pixelPos) return hits[0].point; } //为了防止透过点云缝隙,选到后排的点云,将选取的位置离相机的距离考虑进去。倾向选择离相机近、且离鼠标位置近的点。(否则按照原方案只选离鼠标位置最近的,可能从高楼不小心走到下层,导航选点也是) let sorted1 = hits.sort( (a, b) => a.disSquare - b.disSquare ).slice(); let nearest = sorted1[0]; //return nearest.point; //直接用最近点 在点云稀疏时不太跟手,如地面上,最近点往往在鼠标下方 hits.forEach( hit=>{ let disDiff = hit.disSquare - nearest.disSquare; //和最近点的偏差 hit.disDiff = disDiff; hit.score = -hit.distanceToCenter - disDiff * Potree.settings.pickFrontPointRatio; }); 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) * 5 nodeMaxLevel = this.testMaxNodeCount >= Potree.config.testNodeCount1 ? this.nodeMaxLevel : Math.max(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 { if(this.maxLevel == Infinity)return 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 * 20 * 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 * 20 * 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 = MathUtils.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 = MathUtils.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 Box3; panoBound.expandByPoint(pano.position); panoBound.expandByVector(new Vector3(1,1,1));//give pano a margin bound.union(panoBound); }); return bound } getPanosBound(){//仅由所有pano构成的bound if(this.panos.length > 0){ let minSize = new 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 Vector3(bound.min.x, bound.min.y, bound.min.z), new Vector3(bound.max.x, bound.min.y, bound.min.z), new Vector3(bound.max.x, bound.max.y,bound.min.z), new Vector3(bound.min.x, bound.max.y,bound.min.z), new Vector3(bound.min.x, bound.min.y, bound.max.z), new Vector3(bound.max.x, bound.min.y, bound.max.z), new Vector3(bound.max.x, bound.max.y,bound.max.z), new Vector3(bound.min.x, bound.max.y,bound.max.z), ].map(e=>e.applyMatrix4(this.matrixWorld)) }else return [new Vector3(bound.min.x, bound.min.y,0), new Vector3(bound.max.x, bound.min.y,0), new Vector3(bound.max.x, bound.max.y,0), new 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 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 Vector3); let size = box.getSize(new Vector3); let boxMatrix = new 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) } } class ProfileData { constructor (profile) { this.profile = profile; this.segments = []; this.boundingBox = new Box3(); for (let i = 0; i < profile.points.length - 1; i++) { let start = profile.points[i]; let end = profile.points[i + 1]; let startGround = new Vector3(start.x, start.y, 0); let endGround = new Vector3(end.x, end.y, 0); let center = new Vector3().addVectors(endGround, startGround).multiplyScalar(0.5); let length = startGround.distanceTo(endGround); let side = new Vector3().subVectors(endGround, startGround).normalize(); let up = new Vector3(0, 0, 1); let forward = new Vector3().crossVectors(side, up).normalize(); let N = forward; let cutPlane = new Plane().setFromNormalAndCoplanarPoint(N, startGround); let halfPlane = new Plane().setFromNormalAndCoplanarPoint(side, center); let segment = { start: start, end: end, cutPlane: cutPlane, halfPlane: halfPlane, length: length, points: new Points$1() }; this.segments.push(segment); } } size () { let size = 0; for (let segment of this.segments) { size += segment.points.numPoints; } return size; } }; class ProfileRequest { constructor (pointcloud, profile, maxDepth, callback) { this.pointcloud = pointcloud; this.profile = profile; this.maxDepth = maxDepth || Number.MAX_VALUE; this.callback = callback; this.temporaryResult = new ProfileData(this.profile); this.pointsServed = 0; this.highestLevelServed = 0; this.priorityQueue = new BinaryHeap(function (x) { return 1 / x.weight; }); this.initialize(); } initialize () { this.priorityQueue.push({node: this.pointcloud.pcoGeometry.root, weight: Infinity}); }; // traverse the node and add intersecting descendants to queue traverse (node) { let stack = []; for (let i = 0; i < 8; i++) { let child = node.children[i]; if (child && this.pointcloud.nodeIntersectsProfile(child, this.profile)) { stack.push(child); } } while (stack.length > 0) { let node = stack.pop(); let weight = node.boundingSphere.radius; this.priorityQueue.push({node: node, weight: weight}); // add children that intersect the cutting plane if (node.level < this.maxDepth) { for (let i = 0; i < 8; i++) { let child = node.children[i]; if (child && this.pointcloud.nodeIntersectsProfile(child, this.profile)) { stack.push(child); } } } } } update(){ if(!this.updateGeneratorInstance){ this.updateGeneratorInstance = this.updateGenerator(); } let result = this.updateGeneratorInstance.next(); if(result.done){ this.updateGeneratorInstance = null; } } * updateGenerator(){ // load nodes in queue // if hierarchy expands, also load nodes from expanded hierarchy // once loaded, add data to this.points and remove node from queue // only evaluate 1-50 nodes per frame to maintain responsiveness let start = performance.now(); let maxNodesPerUpdate = 1; let intersectedNodes = []; for (let i = 0; i < Math.min(maxNodesPerUpdate, this.priorityQueue.size()); i++) { let element = this.priorityQueue.pop(); let node = element.node; if(node.level > this.maxDepth){ continue; } if (node.loaded) { // add points to result intersectedNodes.push(node); exports.lru.touch(node); this.highestLevelServed = Math.max(node.getLevel(), this.highestLevelServed); var geom = node.pcoGeometry; var hierarchyStepSize = geom ? geom.hierarchyStepSize : 1; var doTraverse = node.getLevel() === 0 || (node.level % hierarchyStepSize === 0 && node.hasChildren); if (doTraverse) { this.traverse(node); } } else { node.load(); this.priorityQueue.push(element); } } if (intersectedNodes.length > 0) { for(let done of this.getPointsInsideProfile(intersectedNodes, this.temporaryResult)){ if(!done){ //console.log("updateGenerator yields"); yield false; } } if (this.temporaryResult.size() > 100) { this.pointsServed += this.temporaryResult.size(); this.callback.onProgress({request: this, points: this.temporaryResult}); this.temporaryResult = new ProfileData(this.profile); } } if (this.priorityQueue.size() === 0) { // we're done! inform callback and remove from pending requests if (this.temporaryResult.size() > 0) { this.pointsServed += this.temporaryResult.size(); this.callback.onProgress({request: this, points: this.temporaryResult}); this.temporaryResult = new ProfileData(this.profile); } this.callback.onFinish({request: this}); let index = this.pointcloud.profileRequests.indexOf(this); if (index >= 0) { this.pointcloud.profileRequests.splice(index, 1); } } yield true; }; * getAccepted(numPoints, node, matrix, segment, segmentDir, points, totalMileage){ let checkpoint = performance.now(); let accepted = new Uint32Array(numPoints); let mileage = new Float64Array(numPoints); let acceptedPositions = new Float32Array(numPoints * 3); let numAccepted = 0; let pos = new Vector3(); let svp = new Vector3(); let view = new Float32Array(node.geometry.attributes.position.array); for (let i = 0; i < numPoints; i++) { pos.set( view[i * 3 + 0], view[i * 3 + 1], view[i * 3 + 2]); pos.applyMatrix4(matrix); let distance = Math.abs(segment.cutPlane.distanceToPoint(pos)); let centerDistance = Math.abs(segment.halfPlane.distanceToPoint(pos)); if (distance < this.profile.width / 2 && centerDistance < segment.length / 2) { svp.subVectors(pos, segment.start); let localMileage = segmentDir.dot(svp); accepted[numAccepted] = i; mileage[numAccepted] = localMileage + totalMileage; points.boundingBox.expandByPoint(pos); pos.sub(this.pointcloud.position); acceptedPositions[3 * numAccepted + 0] = pos.x; acceptedPositions[3 * numAccepted + 1] = pos.y; acceptedPositions[3 * numAccepted + 2] = pos.z; numAccepted++; } if((i % 1000) === 0){ let duration = performance.now() - checkpoint; if(duration > 4){ //console.log(`getAccepted yield after ${duration}ms`); yield false; checkpoint = performance.now(); } } } accepted = accepted.subarray(0, numAccepted); mileage = mileage.subarray(0, numAccepted); acceptedPositions = acceptedPositions.subarray(0, numAccepted * 3); //let end = performance.now(); //let duration = end - start; //console.log("accepted duration ", duration) //console.log(`getAccepted finished`); yield [accepted, mileage, acceptedPositions]; } * getPointsInsideProfile(nodes, target){ let checkpoint = performance.now(); let totalMileage = 0; let pointsProcessed = 0; for (let segment of target.segments) { for (let node of nodes) { let numPoints = node.numPoints; let geometry = node.geometry; if(!numPoints){ continue; } { // skip if current node doesn't intersect current segment let bbWorld = node.boundingBox.clone().applyMatrix4(this.pointcloud.matrixWorld); let bsWorld = bbWorld.getBoundingSphere(new Sphere()); let start = new Vector3(segment.start.x, segment.start.y, bsWorld.center.z); let end = new Vector3(segment.end.x, segment.end.y, bsWorld.center.z); let closest = new Line3(start, end).closestPointToPoint(bsWorld.center, true, new Vector3()); let distance = closest.distanceTo(bsWorld.center); let intersects = (distance < (bsWorld.radius + target.profile.width)); if(!intersects){ continue; } } //{// DEBUG // console.log(node.name); // let boxHelper = new Potree.Box3Helper(node.getBoundingBox()); // boxHelper.matrixAutoUpdate = false; // boxHelper.matrix.copy(viewer.scene.pointclouds[0].matrixWorld); // viewer.scene.scene.add(boxHelper); //} let sv = new Vector3().subVectors(segment.end, segment.start).setZ(0); let segmentDir = sv.clone().normalize(); let points = new Points$1(); let nodeMatrix = new Matrix4().makeTranslation(...node.boundingBox.min.toArray()); let matrix = new Matrix4().multiplyMatrices( this.pointcloud.matrixWorld, nodeMatrix); pointsProcessed = pointsProcessed + numPoints; let accepted = null; let mileage = null; let acceptedPositions = null; for(let result of this.getAccepted(numPoints, node, matrix, segment, segmentDir, points,totalMileage)){ if(!result){ let duration = performance.now() - checkpoint; //console.log(`getPointsInsideProfile yield after ${duration}ms`); yield false; checkpoint = performance.now(); }else { [accepted, mileage, acceptedPositions] = result; } } let duration = performance.now() - checkpoint; if(duration > 4){ //console.log(`getPointsInsideProfile yield after ${duration}ms`); yield false; checkpoint = performance.now(); } points.data.position = acceptedPositions; let relevantAttributes = Object.keys(geometry.attributes).filter(a => !["position", "indices"].includes(a)); for(let attributeName of relevantAttributes){ let attribute = geometry.attributes[attributeName]; let numElements = attribute.array.length / numPoints; if(numElements !== parseInt(numElements)){ debugger; } let Type = attribute.array.constructor; let filteredBuffer = new Type(numElements * accepted.length); let source = attribute.array; let target = filteredBuffer; for(let i = 0; i < accepted.length; i++){ let index = accepted[i]; let start = index * numElements; let end = start + numElements; let sub = source.subarray(start, end); target.set(sub, i * numElements); } points.data[attributeName] = filteredBuffer; } points.data['mileage'] = mileage; points.numPoints = accepted.length; segment.points.add(points); } totalMileage += segment.length; } for (let segment of target.segments) { target.boundingBox.union(segment.points.boundingBox); } //console.log(`getPointsInsideProfile finished`); yield true; }; finishLevelThenCancel () { if (this.cancelRequested) { return; } this.maxDepth = this.highestLevelServed; this.cancelRequested = true; //console.log(`maxDepth: ${this.maxDepth}`); }; cancel () { this.callback.onCancel(); this.priorityQueue = new BinaryHeap(function (x) { return 1 / x.weight; }); let index = this.pointcloud.profileRequests.indexOf(this); if (index >= 0) { this.pointcloud.profileRequests.splice(index, 1); } }; } class WorkerPool{ constructor(){ this.workers = {}; } getWorker(url){ if (!this.workers[url]){ this.workers[url] = []; } if (this.workers[url].length === 0){ let worker = new Worker(url); this.workers[url].push(worker); } let worker = this.workers[url].pop(); return worker; } returnWorker(url, worker){ this.workers[url].push(worker); } }; function createPointcloudData(pointcloud) { let material = pointcloud.material; let ranges = []; for(let [name, value] of material.ranges){ ranges.push({ name: name, value: value, }); } if(typeof material.elevationRange[0] === "number"){ ranges.push({ name: "elevationRange", value: material.elevationRange, }); } if(typeof material.intensityRange[0] === "number"){ ranges.push({ name: "intensityRange", value: material.intensityRange, }); } let pointSizeTypeName = Object.entries(Potree.PointSizeType).find(e => e[1] === material.pointSizeType)[0]; let jsonMaterial = { activeAttributeName: material.activeAttributeName, ranges: ranges, size: material.size, minSize: material.minSize, pointSizeType: pointSizeTypeName, matcap: material.matcap, }; const pcdata = { name: pointcloud.name, url: pointcloud.pcoGeometry.url, position: pointcloud.position.toArray(), rotation: pointcloud.rotation.toArray(), scale: pointcloud.scale.toArray(), material: jsonMaterial, }; return pcdata; } function createProfileData(profile){ const data = { uuid: profile.uuid, name: profile.name, points: profile.points.map(p => p.toArray()), height: profile.height, width: profile.width, }; return data; } function createVolumeData(volume){ const data = { uuid: volume.uuid, type: volume.constructor.name, name: volume.name, position: volume.position.toArray(), rotation: volume.rotation.toArray(), scale: volume.scale.toArray(), visible: volume.visible, clip: volume.clip, }; return data; } function createCameraAnimationData(animation){ const controlPoints = animation.controlPoints.map( cp => { const cpdata = { position: cp.position.toArray(), target: cp.target.toArray(), }; return cpdata; }); const data = { uuid: animation.uuid, name: animation.name, duration: animation.duration, t: animation.t, curveType: animation.curveType, visible: animation.visible, controlPoints: controlPoints, }; return data; } function createMeasurementData(measurement){ const data = { uuid: measurement.uuid, name: measurement.name, points: measurement.points.map(p => p.position.toArray()), showDistances: measurement.showDistances, showCoordinates: measurement.showCoordinates, showArea: measurement.showArea, closed: measurement.closed, showAngles: measurement.showAngles, showHeight: measurement.showHeight, showCircle: measurement.showCircle, showAzimuth: measurement.showAzimuth, showEdges: measurement.showEdges, color: measurement.color.toArray(), }; return data; } function createOrientedImagesData(images){ const data = { cameraParamsPath: images.cameraParamsPath, imageParamsPath: images.imageParamsPath, }; return data; } function createGeopackageData(geopackage){ const data = { path: geopackage.path, }; return data; } function createAnnotationData(annotation){ const data = { uuid: annotation.uuid, title: annotation.title.toString(), description: annotation.description, position: annotation.position.toArray(), offset: annotation.offset.toArray(), children: [], }; if(annotation.cameraPosition){ data.cameraPosition = annotation.cameraPosition.toArray(); } if(annotation.cameraTarget){ data.cameraTarget = annotation.cameraTarget.toArray(); } if(typeof annotation.radius !== "undefined"){ data.radius = annotation.radius; } return data; } function createAnnotationsData(viewer){ const map = new Map(); viewer.scene.annotations.traverseDescendants(a => { const aData = createAnnotationData(a); map.set(a, aData); }); for(const [annotation, data] of map){ for(const child of annotation.children){ const childData = map.get(child); data.children.push(childData); } } const annotations = viewer.scene.annotations.children.map(a => map.get(a)); return annotations; } function createSettingsData(viewer){ return { pointBudget: viewer.getPointBudget(), fov: viewer.getFOV(), edlEnabled: viewer.getEDLEnabled(), edlRadius: viewer.getEDLRadius(), edlStrength: viewer.getEDLStrength(), background: viewer.getBackground(), minNodeSize: viewer.getMinNodeSize(), showBoundingBoxes: viewer.getShowBoundingBox(), }; } function createSceneContentData(viewer){ const data = []; const potreeObjects = []; viewer.scene.scene.traverse(node => { if(node.potree){ potreeObjects.push(node); } }); for(const object of potreeObjects){ if(object.potree.file){ const saveObject = { file: object.potree.file, }; data.push(saveObject); } } return data; } function createViewData(viewer){ const view = viewer.scene.view; const data = { position: view.position.toArray(), target: view.getPivot().toArray(), }; return data; } function createClassificationData(viewer){ const classifications = viewer.classifications; const data = classifications; return data; } function saveProject(viewer) { const scene = viewer.scene; const data = { type: "Potree", version: 1.7, settings: createSettingsData(viewer), view: createViewData(viewer), classification: createClassificationData(viewer), pointclouds: scene.pointclouds.map(createPointcloudData), measurements: scene.measurements.map(createMeasurementData), volumes: scene.volumes.map(createVolumeData), cameraAnimations: scene.cameraAnimations.map(createCameraAnimationData), profiles: scene.profiles.map(createProfileData), annotations: createAnnotationsData(viewer), orientedImages: scene.orientedImages.map(createOrientedImagesData), geopackages: scene.geopackages.map(createGeopackageData), // objects: createSceneContentData(viewer), }; return data; } class ControlPoint{ constructor(){ this.position = new Vector3(0, 0, 0); this.target = new Vector3(0, 0, 0); this.positionHandle = null; this.targetHandle = null; } }; class CameraAnimation extends EventDispatcher$1{ constructor(viewer){ super(); this.viewer = viewer; this.selectedElement = null; this.controlPoints = []; this.uuid = MathUtils.generateUUID(); this.node = new Object3D(); this.node.name = "camera animation"; this.viewer.scene.scene.add(this.node); this.frustum = this.createFrustum(); this.node.add(this.frustum); this.name = "Camera Animation"; this.duration = 5; this.t = 0; // "centripetal", "chordal", "catmullrom" this.curveType = "centripetal"; this.visible = true; this.createUpdateHook(); this.createPath(); } static defaultFromView(viewer){ const animation = new CameraAnimation(viewer); const camera = viewer.scene.getActiveCamera(); const target = viewer.scene.view.getPivot(); const cpCenter = new Vector3( 0.3 * camera.position.x + 0.7 * target.x, 0.3 * camera.position.y + 0.7 * target.y, 0.3 * camera.position.z + 0.7 * target.z, ); const targetCenter = new Vector3( 0.05 * camera.position.x + 0.95 * target.x, 0.05 * camera.position.y + 0.95 * target.y, 0.05 * camera.position.z + 0.95 * target.z, ); const r = camera.position.distanceTo(target) * 0.3; //const dir = target.clone().sub(camera.position).normalize(); const angle = Utils.computeAzimuth(camera.position, target); const n = 5; for(let i = 0; i < n; i++){ let u = 1.5 * Math.PI * (i / n) + angle; const dx = r * Math.cos(u); const dy = r * Math.sin(u); const cpPos = [ cpCenter.x + dx, cpCenter.y + dy, cpCenter.z, ]; const targetPos = [ targetCenter.x + dx * 0.1, targetCenter.y + dy * 0.1, targetCenter.z, ]; const cp = animation.createControlPoint(); cp.position.set(...cpPos); cp.target.set(...targetPos); } return animation; } createUpdateHook(){ const viewer = this.viewer; viewer.addEventListener("update", () => { const camera = viewer.scene.getActiveCamera(); const {width, height} = viewer.renderer.getSize(new Vector2()); this.node.visible = this.visible; for(const cp of this.controlPoints){ { // position const projected = cp.position.clone().project(camera); const visible = this.visible && (projected.z < 1 && projected.z > -1); if(visible){ const x = width * (projected.x * 0.5 + 0.5); const y = height - height * (projected.y * 0.5 + 0.5); cp.positionHandle.svg.style.left = x - cp.positionHandle.svg.clientWidth / 2; cp.positionHandle.svg.style.top = y - cp.positionHandle.svg.clientHeight / 2; cp.positionHandle.svg.style.display = ""; }else { cp.positionHandle.svg.style.display = "none"; } } { // target const projected = cp.target.clone().project(camera); const visible = this.visible && (projected.z < 1 && projected.z > -1); if(visible){ const x = width * (projected.x * 0.5 + 0.5); const y = height - height * (projected.y * 0.5 + 0.5); cp.targetHandle.svg.style.left = x - cp.targetHandle.svg.clientWidth / 2; cp.targetHandle.svg.style.top = y - cp.targetHandle.svg.clientHeight / 2; cp.targetHandle.svg.style.display = ""; }else { cp.targetHandle.svg.style.display = "none"; } } } this.line.material.resolution.set(width, height); this.updatePath(); { // frustum const frame = this.at(this.t); const frustum = this.frustum; frustum.position.copy(frame.position); frustum.lookAt(...frame.target.toArray()); frustum.scale.set(20, 20, 20); frustum.material.resolution.set(width, height); } }); } createControlPoint(index){ if(index === undefined){ index = this.controlPoints.length; } const cp = new ControlPoint(); if(this.controlPoints.length >= 2 && index === 0){ const cp1 = this.controlPoints[0]; const cp2 = this.controlPoints[1]; const dir = cp1.position.clone().sub(cp2.position).multiplyScalar(0.5); cp.position.copy(cp1.position).add(dir); const tDir = cp1.target.clone().sub(cp2.target).multiplyScalar(0.5); cp.target.copy(cp1.target).add(tDir); }else if(this.controlPoints.length >= 2 && index === this.controlPoints.length){ const cp1 = this.controlPoints[this.controlPoints.length - 2]; const cp2 = this.controlPoints[this.controlPoints.length - 1]; const dir = cp2.position.clone().sub(cp1.position).multiplyScalar(0.5); cp.position.copy(cp1.position).add(dir); const tDir = cp2.target.clone().sub(cp1.target).multiplyScalar(0.5); cp.target.copy(cp2.target).add(tDir); }else if(this.controlPoints.length >= 2){ const cp1 = this.controlPoints[index - 1]; const cp2 = this.controlPoints[index]; cp.position.copy(cp1.position.clone().add(cp2.position).multiplyScalar(0.5)); cp.target.copy(cp1.target.clone().add(cp2.target).multiplyScalar(0.5)); } // cp.position.copy(viewer.scene.view.position); // cp.target.copy(viewer.scene.view.getPivot()); cp.positionHandle = this.createHandle(cp.position); cp.targetHandle = this.createHandle(cp.target); this.controlPoints.splice(index, 0, cp); this.dispatchEvent({ type: "controlpoint_added", controlpoint: cp, }); return cp; } removeControlPoint(cp){ this.controlPoints = this.controlPoints.filter(_cp => _cp !== cp); this.dispatchEvent({ type: "controlpoint_removed", controlpoint: cp, }); cp.positionHandle.svg.remove(); cp.targetHandle.svg.remove(); // TODO destroy cp } createPath(){ { // position const geometry = new LineGeometry(); let material = new LineMaterial({ color: 0x00ff00, dashSize: 5, gapSize: 2, linewidth: 2, resolution: new Vector2(1000, 1000), }); const line = new Line2(geometry, material); this.line = line; this.node.add(line); } { // target const geometry = new LineGeometry(); let material = new LineMaterial({ color: 0x0000ff, dashSize: 5, gapSize: 2, linewidth: 2, resolution: new Vector2(1000, 1000), }); const line = new Line2(geometry, material); this.targetLine = line; this.node.add(line); } } createFrustum(){ const f = 0.3; const positions = [ 0, 0, 0, -f, -f, +1, 0, 0, 0, f, -f, +1, 0, 0, 0, f, f, +1, 0, 0, 0, -f, f, +1, -f, -f, +1, f, -f, +1, f, -f, +1, f, f, +1, f, f, +1, -f, f, +1, -f, f, +1, -f, -f, +1, ]; const geometry = new LineGeometry(); geometry.setPositions(positions); geometry.verticesNeedUpdate = true; geometry.computeBoundingSphere(); let material = new LineMaterial({ color: 0xff0000, linewidth: 2, resolution: new Vector2(1000, 1000), }); const line = new Line2(geometry, material); line.computeLineDistances(); return line; } updatePath(){ { // positions const positions = this.controlPoints.map(cp => cp.position); const first = positions[0]; const curve = new CatmullRomCurve3(positions); curve.curveType = this.curveType; const n = 100; const curvePositions = []; for(let k = 0; k <= n; k++){ const t = k / n; const position = curve.getPoint(t).sub(first); curvePositions.push(position.x, position.y, position.z); } this.line.geometry.setPositions(curvePositions); this.line.geometry.verticesNeedUpdate = true; this.line.geometry.computeBoundingSphere(); this.line.position.copy(first); this.line.computeLineDistances(); this.cameraCurve = curve; } { // targets const positions = this.controlPoints.map(cp => cp.target); const first = positions[0]; const curve = new CatmullRomCurve3(positions); curve.curveType = this.curveType; const n = 100; const curvePositions = []; for(let k = 0; k <= n; k++){ const t = k / n; const position = curve.getPoint(t).sub(first); curvePositions.push(position.x, position.y, position.z); } this.targetLine.geometry.setPositions(curvePositions); this.targetLine.geometry.verticesNeedUpdate = true; this.targetLine.geometry.computeBoundingSphere(); this.targetLine.position.copy(first); this.targetLine.computeLineDistances(); this.targetCurve = curve; } } at(t){ if(t > 1){ t = 1; }else if(t < 0){ t = 0; } const camPos = this.cameraCurve.getPointAt(t); const target = this.targetCurve.getPointAt(t); const frame = { position: camPos, target: target, }; return frame; } set(t){ this.t = t; } createHandle(vector){ const svgns = "http://www.w3.org/2000/svg"; const svg = document.createElementNS(svgns, "svg"); svg.setAttribute("width", "2em"); svg.setAttribute("height", "2em"); svg.setAttribute("position", "absolute"); svg.style.left = "50px"; svg.style.top = "50px"; svg.style.position = "absolute"; svg.style.zIndex = "10000"; const circle = document.createElementNS(svgns, 'circle'); circle.setAttributeNS(null, 'cx', "1em"); circle.setAttributeNS(null, 'cy', "1em"); circle.setAttributeNS(null, 'r', "0.5em"); circle.setAttributeNS(null, 'style', 'fill: red; stroke: black; stroke-width: 0.2em;' ); svg.appendChild(circle); const element = this.viewer.renderer.domElement.parentElement; element.appendChild(svg); const startDrag = (evt) => { this.selectedElement = svg; document.addEventListener("mousemove", drag); }; const endDrag = (evt) => { this.selectedElement = null; document.removeEventListener("mousemove", drag); }; const drag = (evt) => { if (this.selectedElement) { evt.preventDefault(); const rect = viewer.renderer.domElement.getBoundingClientRect(); const x = evt.clientX - rect.x; const y = evt.clientY - rect.y; const {width, height} = this.viewer.renderer.getSize(new Vector2()); const camera = this.viewer.scene.getActiveCamera(); //const cp = this.controlPoints.find(cp => cp.handle.svg === svg); const projected = vector.clone().project(camera); projected.x = ((x / width) - 0.5) / 0.5; projected.y = (-(y - height) / height - 0.5) / 0.5; const unprojected = projected.clone().unproject(camera); vector.set(unprojected.x, unprojected.y, unprojected.z); } }; svg.addEventListener('mousedown', startDrag); svg.addEventListener('mouseup', endDrag); const handle = { svg: svg, }; return handle; } setVisible(visible){ this.node.visible = visible; const display = visible ? "" : "none"; for(const cp of this.controlPoints){ cp.positionHandle.svg.style.display = display; cp.targetHandle.svg.style.display = display; } this.visible = visible; } setDuration(duration){ this.duration = duration; } getDuration(duration){ return this.duration; } play(){ const tStart = performance.now(); const duration = this.duration; const originalyVisible = this.visible; this.setVisible(false); const onUpdate = (delta) => { let tNow = performance.now(); let elapsed = (tNow - tStart) / 1000; let t = elapsed / duration; this.set(t); const frame = this.at(t); viewer.scene.view.position.copy(frame.position); viewer.scene.view.lookAt(frame.target); if(t > 1){ this.setVisible(originalyVisible); this.viewer.removeEventListener("update", onUpdate); } }; this.viewer.addEventListener("update", onUpdate); } } function loadPointCloud(viewer, data){ let loadMaterial = (target) => { if(data.material){ if(data.material.activeAttributeName != null){ target.activeAttributeName = data.material.activeAttributeName; } if(data.material.ranges != null){ for(let range of data.material.ranges){ if(range.name === "elevationRange"){ target.elevationRange = range.value; }else if(range.name === "intensityRange"){ target.intensityRange = range.value; }else { target.setRange(range.name, range.value); } } } if(data.material.size != null){ target.size = data.material.size; } if(data.material.minSize != null){ target.minSize = data.material.minSize; } if(data.material.pointSizeType != null){ target.pointSizeType = PointSizeType[data.material.pointSizeType]; } if(data.material.matcap != null){ target.matcap = data.material.matcap; } }else if(data.activeAttributeName != null){ target.activeAttributeName = data.activeAttributeName; }else { // no material data } }; const promise = new Promise((resolve) => { const names = viewer.scene.pointclouds.map(p => p.name); const alreadyExists = names.includes(data.name); if(alreadyExists){ resolve(); return; } Potree.loadPointCloud(data.url, data.name, (e) => { const {pointcloud} = e; pointcloud.position.set(...data.position); pointcloud.rotation.set(...data.rotation); pointcloud.scale.set(...data.scale); loadMaterial(pointcloud.material); viewer.scene.addPointCloud(pointcloud); resolve(pointcloud); }); }); return promise; } function loadMeasurement(viewer, data){ const duplicate = viewer.scene.measurements.find(measure => measure.uuid === data.uuid); if(duplicate){ return; } const measure = new Measure(); measure.uuid = data.uuid; measure.name = data.name; measure.showDistances = data.showDistances; measure.showCoordinates = data.showCoordinates; measure.showArea = data.showArea; measure.closed = data.closed; measure.showAngles = data.showAngles; measure.showHeight = data.showHeight; measure.showCircle = data.showCircle; measure.showAzimuth = data.showAzimuth; measure.showEdges = data.showEdges; // color for(const point of data.points){ const pos = new Vector3(...point); measure.addMarker(pos); } viewer.scene.addMeasurement(measure); } function loadVolume(viewer, data){ const duplicate = viewer.scene.volumes.find(volume => volume.uuid === data.uuid); if(duplicate){ return; } let volume = new Potree[data.type]; volume.uuid = data.uuid; volume.name = data.name; volume.position.set(...data.position); volume.rotation.set(...data.rotation); volume.scale.set(...data.scale); volume.visible = data.visible; volume.clip = data.clip; viewer.scene.addVolume(volume); } function loadCameraAnimation(viewer, data){ const duplicate = viewer.scene.cameraAnimations.find(a => a.uuid === data.uuid); if(duplicate){ return; } const animation = new CameraAnimation(viewer); animation.uuid = data.uuid; animation.name = data.name; animation.duration = data.duration; animation.t = data.t; animation.curveType = data.curveType; animation.visible = data.visible; animation.controlPoints = []; for(const cpdata of data.controlPoints){ const cp = animation.createControlPoint(); cp.position.set(...cpdata.position); cp.target.set(...cpdata.target); } viewer.scene.addCameraAnimation(animation); } function loadOrientedImages(viewer, images){ const {cameraParamsPath, imageParamsPath} = images; const duplicate = viewer.scene.orientedImages.find(i => i.imageParamsPath === imageParamsPath); if(duplicate){ return; } Potree.OrientedImageLoader.load(cameraParamsPath, imageParamsPath, viewer).then( images => { viewer.scene.addOrientedImages(images); }); } function loadGeopackage(viewer, geopackage){ const path = geopackage.path; const duplicate = viewer.scene.geopackages.find(i => i.path === path); if(duplicate){ return; } const projection = viewer.getProjection(); proj4.defs("WGS84", "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"); proj4.defs("pointcloud", projection); const transform = proj4("WGS84", "pointcloud"); const params = { transform: transform, }; Potree.GeoPackageLoader.loadUrl(path, params).then(data => { viewer.scene.addGeopackage(data); }); } function loadSettings(viewer, data){ if(!data){ return; } viewer.setPointBudget(data.pointBudget); viewer.setFOV(data.fov); viewer.setEDLEnabled(data.edlEnabled); viewer.setEDLRadius(data.edlRadius); viewer.setEDLStrength(data.edlStrength); viewer.setBackground(data.background); viewer.setMinNodeSize(data.minNodeSize); viewer.setShowBoundingBox(data.showBoundingBoxes); } function loadView(viewer, view){ viewer.scene.view.position.set(...view.position); viewer.scene.view.lookAt(...view.target); } function loadAnnotationItem(item){ const annotation = new Annotation({ position: item.position, title: item.title, cameraPosition: item.cameraPosition, cameraTarget: item.cameraTarget, }); annotation.description = item.description; annotation.uuid = item.uuid; if(item.offset){ annotation.offset.set(...item.offset); } return annotation; } function loadAnnotations(viewer, data){ if(!data){ return; } const findDuplicate = (item) => { let duplicate = null; viewer.scene.annotations.traverse( a => { if(a.uuid === item.uuid){ duplicate = a; } }); return duplicate; }; const traverse = (item, parent) => { const duplicate = findDuplicate(item); if(duplicate){ return; } const annotation = loadAnnotationItem(item); for(const childItem of item.children){ traverse(childItem, annotation); } parent.add(annotation); }; for(const item of data){ traverse(item, viewer.scene.annotations); } } function loadProfile(viewer, data){ const {name, points} = data; const duplicate = viewer.scene.profiles.find(profile => profile.uuid === data.uuid); if(duplicate){ return; } let profile = new Potree.Profile(); profile.name = name; profile.uuid = data.uuid; profile.setWidth(data.width); for(const point of points){ profile.addMarker(new Vector3(...point)); } viewer.scene.addProfile(profile); } function loadClassification(viewer, data){ if(!data){ return; } const classifications = data; viewer.setClassifications(classifications); } async function loadProject(viewer, data){ if(data.type !== "Potree"){ console.error("not a valid Potree project"); return; } loadSettings(viewer, data.settings); loadView(viewer, data.view); const pointcloudPromises = []; for(const pointcloud of data.pointclouds){ const promise = loadPointCloud(viewer, pointcloud); pointcloudPromises.push(promise); } for(const measure of data.measurements){ loadMeasurement(viewer, measure); } for(const volume of data.volumes){ loadVolume(viewer, volume); } for(const animation of data.cameraAnimations){ loadCameraAnimation(viewer, animation); } for(const profile of data.profiles){ loadProfile(viewer, profile); } if(data.orientedImages){ for(const images of data.orientedImages){ loadOrientedImages(viewer, images); } } loadAnnotations(viewer, data.annotations); loadClassification(viewer, data.classification); // need to load at least one point cloud that defines the scene projection, // before we can load stuff in other projections such as geopackages //await Promise.any(pointcloudPromises); // (not yet supported) Utils.waitAny(pointcloudPromises).then( () => { if(data.geopackages){ for(const geopackage of data.geopackages){ loadGeopackage(viewer, geopackage); } } }); await Promise.all(pointcloudPromises); } // // Algorithm by Christian Boucheny // shader code taken and adapted from CloudCompare // // see // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL // http://www.kitware.com/source/home/post/9 // https://tel.archives-ouvertes.fr/tel-00438464/document p. 115+ (french) class EyeDomeLightingMaterial extends RawShaderMaterial{//base constructor(parameters = {}){ super(); let uniforms = { screenWidth: { type: 'f', value: 0 }, screenHeight: { type: 'f', value: 0 }, edlStrength: { type: 'f', value: 1.0 }, uNear: { type: 'f', value: 1.0 }, uFar: { type: 'f', value: 1.0 }, radius: { type: 'f', value: 1.0 }, neighbours: { type: '2fv', value: [] }, depthMap: { type: 't', value: null }, uEDLColor: { type: 't', value: null }, uEDLDepth: { type: 't', value: null }, opacity: { type: 'f', value: 1.0 }, uProj: { type: "Matrix4fv", value: [] }, }; this.setValues({ uniforms: uniforms, vertexShader: this.getDefines() + Shaders['edl.vs'], fragmentShader: this.getDefines() + Shaders['edl.fs'], lights: false }); this.neighbourCount = 8; } getDefines() { let defines = ''; defines += '#define NEIGHBOUR_COUNT ' + this.neighbourCount + '\n'; return defines; } updateShaderSource() { let vs = this.getDefines() + Shaders['edl.vs']; let fs = this.getDefines() + Shaders['edl.fs']; this.setValues({ vertexShader: vs, fragmentShader: fs }); this.uniforms.neighbours.value = this.neighbours; this.needsUpdate = true; } get neighbourCount(){ return this._neighbourCount; } set neighbourCount(value){ if (this._neighbourCount !== value) {//周围八个格子 this._neighbourCount = value; this.neighbours = new Float32Array(this._neighbourCount * 2); for (let c = 0; c < this._neighbourCount; c++) { this.neighbours[2 * c + 0] = Math.cos(2 * c * Math.PI / this._neighbourCount); this.neighbours[2 * c + 1] = Math.sin(2 * c * Math.PI / this._neighbourCount); } this.updateShaderSource(); } } } /** * laslaz code taken and adapted from plas.io js-laslaz * http://plas.io/ * https://github.com/verma/plasio * * Thanks to Uday Verma and Howard Butler * */ class LasLazLoader { constructor (version, extension) { if (typeof (version) === 'string') { this.version = new Version(version); } else { this.version = version; } this.extension = extension; } static progressCB () { } load (node, callback) { if (node.loaded) { return; } let url = node.getURL(); if (this.version.equalOrHigher('1.4')) { url += `.${this.extension}`; } let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200 || xhr.status === 0) { let buffer = xhr.response; this.parse(node, buffer, callback); } else { console.log('Failed to load file! HTTP status: ' + xhr.status + ', file: ' + url); } } }; xhr.send(null); } async parse(node, buffer, callback){//add callback let lf = new LASFile(buffer); let handler = new LasLazBatcher(node); try{ await lf.open(); lf.isOpen = true; }catch(e){ console.log("failed to open file. :("); return; } let header = await lf.getHeader(); let skip = 1; let totalRead = 0; let totalToRead = (skip <= 1 ? header.pointsCount : header.pointsCount / skip); let hasMoreData = true; while(hasMoreData){ let data = await lf.readData(1000 * 1000, 0, skip); handler.push(new LASDecoder(data.buffer, header.pointsFormatId, header.pointsStructSize, data.count, header.scale, header.offset, header.mins, header.maxs)); totalRead += data.count; LasLazLoader.progressCB(totalRead / totalToRead); hasMoreData = data.hasMoreData; } header.totalRead = totalRead; header.versionAsString = lf.versionAsString; header.isCompressed = lf.isCompressed; LasLazLoader.progressCB(1); try{ await lf.close(); lf.isOpen = false; }catch(e){ console.error("failed to close las/laz file!!!"); throw e; } callback(); //add } handle (node, url) { } }; class LasLazBatcher{ constructor (node) { this.node = node; } push (lasBuffer) { const workerPath = Potree.scriptPath + '/workers/LASDecoderWorker.js'; const worker = Potree.workerPool.getWorker(workerPath); const node = this.node; const pointAttributes = node.pcoGeometry.pointAttributes; worker.onmessage = (e) => { let geometry = new BufferGeometry(); let numPoints = lasBuffer.pointsCount; let positions = new Float32Array(e.data.position); let colors = new Uint8Array(e.data.color); let intensities = new Float32Array(e.data.intensity); let classifications = new Uint8Array(e.data.classification); let returnNumbers = new Uint8Array(e.data.returnNumber); let numberOfReturns = new Uint8Array(e.data.numberOfReturns); let pointSourceIDs = new Uint16Array(e.data.pointSourceID); let indices = new Uint8Array(e.data.indices); geometry.setAttribute('position', new BufferAttribute(positions, 3)); geometry.setAttribute('color', new BufferAttribute(colors, 4, true)); geometry.setAttribute('intensity', new BufferAttribute(intensities, 1)); geometry.setAttribute('classification', new BufferAttribute(classifications, 1)); geometry.setAttribute('return number', new BufferAttribute(returnNumbers, 1)); geometry.setAttribute('number of returns', new BufferAttribute(numberOfReturns, 1)); geometry.setAttribute('source id', new BufferAttribute(pointSourceIDs, 1)); geometry.setAttribute('indices', new BufferAttribute(indices, 4)); geometry.attributes.indices.normalized = true; for(const key in e.data.ranges){ const range = e.data.ranges[key]; const attribute = pointAttributes.attributes.find(a => a.name === key); attribute.range[0] = Math.min(attribute.range[0], range[0]); attribute.range[1] = Math.max(attribute.range[1], range[1]); } let tightBoundingBox = new Box3( new Vector3().fromArray(e.data.tightBoundingBox.min), new Vector3().fromArray(e.data.tightBoundingBox.max) ); geometry.boundingBox = this.node.boundingBox; this.node.tightBoundingBox = tightBoundingBox; this.node.geometry = geometry; this.node.numPoints = numPoints; this.node.loaded = true; this.node.loading = false; Potree.numNodesLoading--; this.node.mean = new Vector3(...e.data.mean); Potree.workerPool.returnWorker(workerPath, worker); }; let message = { buffer: lasBuffer.arrayb, numPoints: lasBuffer.pointsCount, pointSize: lasBuffer.pointSize, pointFormatID: 2, scale: lasBuffer.scale, offset: lasBuffer.offset, mins: lasBuffer.mins, maxs: lasBuffer.maxs }; worker.postMessage(message, [message.buffer]); }; } function parseAttributes(cloudjs){ let version = new Version(cloudjs.version); const replacements = { "COLOR_PACKED": "rgba", "RGBA": "rgba", "INTENSITY": "intensity", "CLASSIFICATION": "classification", "GPS_TIME": "gps-time", }; const replaceOldNames = (old) => { if(replacements[old]){ return replacements[old]; }else { return old; } }; const pointAttributes = []; if(version.upTo('1.7')){ for(let attributeName of cloudjs.pointAttributes){ const oldAttribute = PointAttribute[attributeName]; const attribute = { name: oldAttribute.name, size: oldAttribute.byteSize, elements: oldAttribute.numElements, elementSize: oldAttribute.byteSize / oldAttribute.numElements, type: oldAttribute.type.name, description: "", }; pointAttributes.push(attribute); } }else { pointAttributes.push(...cloudjs.pointAttributes); } { const attributes = new PointAttributes(); const typeConversion = { int8: PointAttributeTypes.DATA_TYPE_INT8, int16: PointAttributeTypes.DATA_TYPE_INT16, int32: PointAttributeTypes.DATA_TYPE_INT32, int64: PointAttributeTypes.DATA_TYPE_INT64, uint8: PointAttributeTypes.DATA_TYPE_UINT8, uint16: PointAttributeTypes.DATA_TYPE_UINT16, uint32: PointAttributeTypes.DATA_TYPE_UINT32, uint64: PointAttributeTypes.DATA_TYPE_UINT64, double: PointAttributeTypes.DATA_TYPE_DOUBLE, float: PointAttributeTypes.DATA_TYPE_FLOAT, }; for(let jsAttribute of pointAttributes){ if(jsAttribute.name == void 0){ //add 有的是这个,也有的不是(点云编辑页的) var attribute_ = PointAttribute[jsAttribute]; attributes.add(attribute_); continue; } const name = replaceOldNames(jsAttribute.name); const type = typeConversion[jsAttribute.type]; const numElements = jsAttribute.elements; const description = jsAttribute.description; const attribute = new PointAttribute(name, type, numElements); attributes.add(attribute); } { // check if it has normals let hasNormals = pointAttributes.find(a => a.name === "NormalX") !== undefined && pointAttributes.find(a => a.name === "NormalY") !== undefined && pointAttributes.find(a => a.name === "NormalZ") !== undefined; if(hasNormals){ let vector = { name: "NORMAL", attributes: ["NormalX", "NormalY", "NormalZ"], }; attributes.addVector(vector); } } return attributes; } } function lasLazAttributes(fMno){ const attributes = new PointAttributes(); attributes.add(PointAttribute.POSITION_CARTESIAN); attributes.add(new PointAttribute("rgba", PointAttributeTypes.DATA_TYPE_UINT8, 4)); attributes.add(new PointAttribute("intensity", PointAttributeTypes.DATA_TYPE_UINT16, 1)); attributes.add(new PointAttribute("classification", PointAttributeTypes.DATA_TYPE_UINT8, 1)); attributes.add(new PointAttribute("gps-time", PointAttributeTypes.DATA_TYPE_DOUBLE, 1)); attributes.add(new PointAttribute("number of returns", PointAttributeTypes.DATA_TYPE_UINT8, 1)); attributes.add(new PointAttribute("return number", PointAttributeTypes.DATA_TYPE_UINT8, 1)); attributes.add(new PointAttribute("source id", PointAttributeTypes.DATA_TYPE_UINT16, 1)); //attributes.add(new PointAttribute("pointSourceID", PointAttributeTypes.DATA_TYPE_INT8, 4)); return attributes; } class POCLoader { static load(url, timeStamp, callback){ //add timeStamp try { let pco = new PointCloudOctreeGeometry(); pco.timeStamp = timeStamp; pco.url = url; let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', url+'?m='+timeStamp, true); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && (xhr.status === 200 || xhr.status === 0)) { let fMno = JSON.parse(xhr.responseText); let version = new Version(fMno.version); // assume octreeDir is absolute if it starts with http if (fMno.octreeDir.indexOf('http') === 0) { pco.octreeDir = fMno.octreeDir; } else { pco.octreeDir = url + '/../' + fMno.octreeDir; } pco.spacing = fMno.spacing; pco.hierarchyStepSize = fMno.hierarchyStepSize; pco.pointAttributes = fMno.pointAttributes; let min = new Vector3(fMno.boundingBox.lx, fMno.boundingBox.ly, fMno.boundingBox.lz); let max = new Vector3(fMno.boundingBox.ux, fMno.boundingBox.uy, fMno.boundingBox.uz); let boundingBox = new Box3(min, max); let tightBoundingBox = boundingBox.clone(); if (fMno.tightBoundingBox) {//这个才是真实的bounding,前面那个bounding的size是个正方体,似乎取了最长边作为边长 tightBoundingBox.min.copy(new Vector3(fMno.tightBoundingBox.lx, fMno.tightBoundingBox.ly, fMno.tightBoundingBox.lz)); tightBoundingBox.max.copy(new Vector3(fMno.tightBoundingBox.ux, fMno.tightBoundingBox.uy, fMno.tightBoundingBox.uz)); } let offset = min.clone(); //将成为点云的position,被我用作旋转中心(但在点云中不那么居中,navvis也是这样, 这样可能是为了让模型在这数据的bounding上) boundingBox.min.sub(offset); //点云的真实坐标的min都是0,0,0吗(我看案例是,因绕角落旋转,也就是原点) boundingBox.max.sub(offset); tightBoundingBox.min.sub(offset); tightBoundingBox.max.sub(offset); //改 //pco.projection = fMno.projection || "+proj=somerc +lat_0=46.95240555555556 +lon_0=7.439583333333333 +k_0=1 +x_0=2600000 +y_0=1200000 +ellps=bessel +towgs84=674.374,15.056,405.346,0,0,0,0 +units=m +no_defs ", //"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs" //给地图 pco.boundingBox = boundingBox; pco.tightBoundingBox = tightBoundingBox; pco.boundingSphere = boundingBox.getBoundingSphere(new Sphere()); pco.tightBoundingSphere = tightBoundingBox.getBoundingSphere(new Sphere()); pco.offset = offset; if (fMno.pointAttributes === 'LAS') { pco.loader = new LasLazLoader(fMno.version, "las"); pco.pointAttributes = lasLazAttributes(fMno); } else if (fMno.pointAttributes === 'LAZ') { pco.loader = new LasLazLoader(fMno.version, "laz"); pco.pointAttributes = lasLazAttributes(fMno); } else { pco.loader = new BinaryLoader(fMno.version, boundingBox, fMno.scale); pco.pointAttributes = parseAttributes(fMno); } let nodes = {}; { // load root let name = 'r'; let root = new PointCloudOctreeGeometryNode(name, pco, boundingBox); root.level = 0; root.hasChildren = true; root.spacing = pco.spacing; if (version.upTo('1.5')) { root.numPoints = fMno.hierarchy[0][1]; } else { root.numPoints = 0; } pco.root = root; pco.root.load(); nodes[name] = root; } // load remaining hierarchy if (version.upTo('1.4')) { for (let i = 1; i < fMno.hierarchy.length; i++) { let name = fMno.hierarchy[i][0]; let numPoints = fMno.hierarchy[i][1]; let index = parseInt(name.charAt(name.length - 1)); let parentName = name.substring(0, name.length - 1); let parentNode = nodes[parentName]; let level = name.length - 1; //let boundingBox = POCLoader.createChildAABB(parentNode.boundingBox, index); let boundingBox = Utils.createChildAABB(parentNode.boundingBox, index); let node = new PointCloudOctreeGeometryNode(name, pco, boundingBox); node.level = level; node.numPoints = numPoints; node.spacing = pco.spacing / Math.pow(2, level); parentNode.addChild(node); nodes[name] = node; } } pco.nodes = nodes; callback(pco); } }; xhr.send(null); } catch (e) { console.log("loading failed: '" + url + "'"); console.log(e); callback(); } } loadPointAttributes(mno){ let fpa = mno.pointAttributes; let pa = new PointAttributes(); for (let i = 0; i < fpa.length; i++) { let pointAttribute = PointAttribute[fpa[i]]; pa.add(pointAttribute); } return pa; } createChildAABB(aabb, index){ let min = aabb.min.clone(); let max = aabb.max.clone(); let size = new Vector3().subVectors(max, min); if ((index & 0b0001) > 0) { min.z += size.z / 2; } else { max.z -= size.z / 2; } if ((index & 0b0010) > 0) { min.y += size.y / 2; } else { max.y -= size.y / 2; } if ((index & 0b0100) > 0) { min.x += size.x / 2; } else { max.x -= size.x / 2; } return new Box3(min, max); } } class OctreeGeometry{ constructor(){ this.url = null; this.spacing = 0; this.boundingBox = null; this.root = null; this.pointAttributes = null; this.loader = null; } }; class OctreeGeometryNode{ constructor(name, octreeGeometry, boundingBox){ this.id = OctreeGeometryNode.IDCount++; this.name = name; this.index = parseInt(name.charAt(name.length - 1)); this.octreeGeometry = octreeGeometry; this.boundingBox = boundingBox; this.boundingSphere = boundingBox.getBoundingSphere(new Sphere()); this.children = {}; this.numPoints = 0; this.level = null; this.oneTimeDisposeHandlers = []; } isGeometryNode(){ return true; } getLevel(){ return this.level; } isTreeNode(){ return false; } isLoaded(){ return this.loaded; } getBoundingSphere(){ return this.boundingSphere; } getBoundingBox(){ return this.boundingBox; } getChildren(){ let children = []; for (let i = 0; i < 8; i++) { if (this.children[i]) { children.push(this.children[i]); } } return children; } getBoundingBox(){ return this.boundingBox; } load(){ if (Potree.numNodesLoading >= Potree.maxNodesLoading) { return; } this.octreeGeometry.loader.load(this); } getNumPoints(){ return this.numPoints; } dispose(){ if (this.geometry && this.parent != null) { this.geometry.dispose(); this.geometry = null; this.loaded = false; // this.dispatchEvent( { type: 'dispose' } ); for (let i = 0; i < this.oneTimeDisposeHandlers.length; i++) { let handler = this.oneTimeDisposeHandlers[i]; handler(); } this.oneTimeDisposeHandlers = []; } } }; OctreeGeometryNode.IDCount = 0; // let loadedNodes = new Set(); class NodeLoader{ constructor(url){ this.url = url; } async load(node){ if(node.loaded || node.loading){ return; } node.loading = true; Potree.numNodesLoading++; // console.log(node.name, node.numPoints); // if(loadedNodes.has(node.name)){ // // debugger; // } // loadedNodes.add(node.name); try{ if(node.nodeType === 2){ await this.loadHierarchy(node); } let {byteOffset, byteSize} = node; let urlOctree = `${this.url}/../octree.bin`; let first = byteOffset; let last = byteOffset + byteSize - 1n; let buffer; if(byteSize === 0n){ buffer = new ArrayBuffer(0); console.warn(`loaded node with 0 bytes: ${node.name}`); }else { let response = await fetch(urlOctree, { headers: { 'content-type': 'multipart/byteranges', 'Range': `bytes=${first}-${last}`, }, }); buffer = await response.arrayBuffer(); } let workerPath; if(this.metadata.encoding === "BROTLI"){ workerPath = Potree.scriptPath + '/workers/2.0/DecoderWorker_brotli.js'; }else { workerPath = Potree.scriptPath + '/workers/2.0/DecoderWorker.js'; } let worker = Potree.workerPool.getWorker(workerPath); worker.onmessage = function (e) { let data = e.data; let buffers = data.attributeBuffers; Potree.workerPool.returnWorker(workerPath, worker); let geometry = new BufferGeometry(); for(let property in buffers){ let buffer = buffers[property].buffer; if(property === "position"){ geometry.setAttribute('position', new BufferAttribute(new Float32Array(buffer), 3)); }else if(property === "rgba"){ geometry.setAttribute('rgba', new BufferAttribute(new Uint8Array(buffer), 4, true)); }else if(property === "NORMAL"){ //geometry.setAttribute('rgba', new THREE.BufferAttribute(new Uint8Array(buffer), 4, true)); geometry.setAttribute('normal', new BufferAttribute(new Float32Array(buffer), 3)); }else if (property === "INDICES") { let bufferAttribute = new BufferAttribute(new Uint8Array(buffer), 4); bufferAttribute.normalized = true; geometry.setAttribute('indices', bufferAttribute); }else { const bufferAttribute = new BufferAttribute(new Float32Array(buffer), 1); let batchAttribute = buffers[property].attribute; bufferAttribute.potree = { offset: buffers[property].offset, scale: buffers[property].scale, preciseBuffer: buffers[property].preciseBuffer, range: batchAttribute.range, }; geometry.setAttribute(property, bufferAttribute); } } // indices ?? node.density = data.density; node.geometry = geometry; node.loaded = true; node.loading = false; Potree.numNodesLoading--; }; let pointAttributes = node.octreeGeometry.pointAttributes; let scale = node.octreeGeometry.scale; let box = node.boundingBox; let min = node.octreeGeometry.offset.clone().add(box.min); let size = box.max.clone().sub(box.min); let max = min.clone().add(size); let numPoints = node.numPoints; let offset = node.octreeGeometry.loader.offset; let message = { name: node.name, buffer: buffer, pointAttributes: pointAttributes, scale: scale, min: min, max: max, size: size, offset: offset, numPoints: numPoints }; worker.postMessage(message, [message.buffer]); }catch(e){ node.loaded = false; node.loading = false; Potree.numNodesLoading--; console.log(`failed to load ${node.name}`); console.log(e); console.log(`trying again!`); } } parseHierarchy(node, buffer){ let view = new DataView(buffer); let tStart = performance.now(); let bytesPerNode = 22; let numNodes = buffer.byteLength / bytesPerNode; let octree = node.octreeGeometry; // let nodes = [node]; let nodes = new Array(numNodes); nodes[0] = node; let nodePos = 1; for(let i = 0; i < numNodes; i++){ let current = nodes[i]; let type = view.getUint8(i * bytesPerNode + 0); let childMask = view.getUint8(i * bytesPerNode + 1); let numPoints = view.getUint32(i * bytesPerNode + 2, true); let byteOffset = view.getBigInt64(i * bytesPerNode + 6, true); let byteSize = view.getBigInt64(i * bytesPerNode + 14, true); // if(byteSize === 0n){ // // debugger; // } if(current.nodeType === 2){ // replace proxy with real node current.byteOffset = byteOffset; current.byteSize = byteSize; current.numPoints = numPoints; }else if(type === 2){ // load proxy current.hierarchyByteOffset = byteOffset; current.hierarchyByteSize = byteSize; current.numPoints = numPoints; }else { // load real node current.byteOffset = byteOffset; current.byteSize = byteSize; current.numPoints = numPoints; } if(current.byteSize === 0n){ // workaround for issue #1125 // some inner nodes erroneously report >0 points even though have 0 points // however, they still report a byteSize of 0, so based on that we now set node.numPoints to 0 current.numPoints = 0; } current.nodeType = type; if(current.nodeType === 2){ continue; } for(let childIndex = 0; childIndex < 8; childIndex++){ let childExists = ((1 << childIndex) & childMask) !== 0; if(!childExists){ continue; } let childName = current.name + childIndex; let childAABB = createChildAABB(current.boundingBox, childIndex); let child = new OctreeGeometryNode(childName, octree, childAABB); child.name = childName; child.spacing = current.spacing / 2; child.level = current.level + 1; current.children[childIndex] = child; child.parent = current; // nodes.push(child); nodes[nodePos] = child; nodePos++; } // if((i % 500) === 0){ // yield; // } } let duration = (performance.now() - tStart); // if(duration > 20){ // let msg = `duration: ${duration}ms, numNodes: ${numNodes}`; // console.log(msg); // } } async loadHierarchy(node){ let {hierarchyByteOffset, hierarchyByteSize} = node; let hierarchyPath = `${this.url}/../hierarchy.bin`; let first = hierarchyByteOffset; let last = first + hierarchyByteSize - 1n; let response = await fetch(hierarchyPath, { headers: { 'content-type': 'multipart/byteranges', 'Range': `bytes=${first}-${last}`, }, }); let buffer = await response.arrayBuffer(); this.parseHierarchy(node, buffer); // let promise = new Promise((resolve) => { // let generator = this.parseHierarchy(node, buffer); // let repeatUntilDone = () => { // let result = generator.next(); // if(result.done){ // resolve(); // }else{ // requestAnimationFrame(repeatUntilDone); // } // }; // repeatUntilDone(); // }); // await promise; } } let tmpVec3 = new Vector3(); function createChildAABB(aabb, index){ let min = aabb.min.clone(); let max = aabb.max.clone(); let size = tmpVec3.subVectors(max, min); if ((index & 0b0001) > 0) { min.z += size.z / 2; } else { max.z -= size.z / 2; } if ((index & 0b0010) > 0) { min.y += size.y / 2; } else { max.y -= size.y / 2; } if ((index & 0b0100) > 0) { min.x += size.x / 2; } else { max.x -= size.x / 2; } return new Box3(min, max); } let typenameTypeattributeMap = { "double": PointAttributeTypes.DATA_TYPE_DOUBLE, "float": PointAttributeTypes.DATA_TYPE_FLOAT, "int8": PointAttributeTypes.DATA_TYPE_INT8, "uint8": PointAttributeTypes.DATA_TYPE_UINT8, "int16": PointAttributeTypes.DATA_TYPE_INT16, "uint16": PointAttributeTypes.DATA_TYPE_UINT16, "int32": PointAttributeTypes.DATA_TYPE_INT32, "uint32": PointAttributeTypes.DATA_TYPE_UINT32, "int64": PointAttributeTypes.DATA_TYPE_INT64, "uint64": PointAttributeTypes.DATA_TYPE_UINT64, }; class OctreeLoader{ static parseAttributes(jsonAttributes){ let attributes = new PointAttributes(); let replacements = { "rgb": "rgba", }; for (const jsonAttribute of jsonAttributes) { let {name, description, size, numElements, elementSize, min, max} = jsonAttribute; let type = typenameTypeattributeMap[jsonAttribute.type]; let potreeAttributeName = replacements[name] ? replacements[name] : name; let attribute = new PointAttribute(potreeAttributeName, type, numElements); if(numElements === 1){ attribute.range = [min[0], max[0]]; }else { attribute.range = [min, max]; } if (name === "gps-time") { // HACK: Guard against bad gpsTime range in metadata, see potree/potree#909 if (attribute.range[0] === attribute.range[1]) { attribute.range[1] += 1; } } attribute.initialRange = attribute.range; attributes.add(attribute); } { // check if it has normals let hasNormals = attributes.attributes.find(a => a.name === "NormalX") !== undefined && attributes.attributes.find(a => a.name === "NormalY") !== undefined && attributes.attributes.find(a => a.name === "NormalZ") !== undefined; if(hasNormals){ let vector = { name: "NORMAL", attributes: ["NormalX", "NormalY", "NormalZ"], }; attributes.addVector(vector); } } return attributes; } static async load(url){ let response = await fetch(url); let metadata = await response.json(); let attributes = OctreeLoader.parseAttributes(metadata.attributes); let loader = new NodeLoader(url); loader.metadata = metadata; loader.attributes = attributes; loader.scale = metadata.scale; loader.offset = metadata.offset; let octree = new OctreeGeometry(); octree.url = url; octree.spacing = metadata.spacing; octree.scale = metadata.scale; // let aPosition = metadata.attributes.find(a => a.name === "position"); // octree let min = new Vector3(...metadata.boundingBox.min); let max = new Vector3(...metadata.boundingBox.max); let boundingBox = new Box3(min, max); let offset = min.clone(); boundingBox.min.sub(offset); boundingBox.max.sub(offset); octree.projection = metadata.projection; octree.boundingBox = boundingBox; octree.tightBoundingBox = boundingBox.clone(); octree.boundingSphere = boundingBox.getBoundingSphere(new Sphere()); octree.tightBoundingSphere = boundingBox.getBoundingSphere(new Sphere()); octree.offset = offset; octree.pointAttributes = OctreeLoader.parseAttributes(metadata.attributes); octree.loader = loader; let root = new OctreeGeometryNode("r", octree, boundingBox); root.level = 0; root.nodeType = 2; root.hierarchyByteOffset = 0n; root.hierarchyByteSize = BigInt(metadata.hierarchy.firstChunkSize); root.hasChildren = false; root.spacing = octree.spacing; root.byteOffset = 0; octree.root = root; loader.load(root); let result = { geometry: octree, }; return result; } }; /** * @author Connor Manning */ class EptLoader { static async load(file, callback) { let response = await fetch(file); let json = await response.json(); let url = file.substr(0, file.lastIndexOf('ept.json')); let geometry = new Potree.PointCloudEptGeometry(url, json); let root = new Potree.PointCloudEptGeometryNode(geometry); geometry.root = root; geometry.root.load(); callback(geometry); } }; class EptBinaryLoader { extension() { return '.bin'; } workerPath() { return Potree.scriptPath + '/workers/EptBinaryDecoderWorker.js'; } load(node) { if (node.loaded) return; let url = node.url() + this.extension(); let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { let buffer = xhr.response; this.parse(node, buffer); } else { console.log('Failed ' + url + ': ' + xhr.status); } } }; try { xhr.send(null); } catch (e) { console.log('Failed request: ' + e); } } parse(node, buffer) { let workerPath = this.workerPath(); let worker = Potree.workerPool.getWorker(workerPath); worker.onmessage = function(e) { let g = new BufferGeometry(); let numPoints = e.data.numPoints; let position = new Float32Array(e.data.position); g.setAttribute('position', new BufferAttribute(position, 3)); let indices = new Uint8Array(e.data.indices); g.setAttribute('indices', new BufferAttribute(indices, 4)); if (e.data.color) { let color = new Uint8Array(e.data.color); g.setAttribute('color', new BufferAttribute(color, 4, true)); } if (e.data.intensity) { let intensity = new Float32Array(e.data.intensity); g.setAttribute('intensity', new BufferAttribute(intensity, 1)); } if (e.data.classification) { let classification = new Uint8Array(e.data.classification); g.setAttribute('classification', new BufferAttribute(classification, 1)); } if (e.data.returnNumber) { let returnNumber = new Uint8Array(e.data.returnNumber); g.setAttribute('return number', new BufferAttribute(returnNumber, 1)); } if (e.data.numberOfReturns) { let numberOfReturns = new Uint8Array(e.data.numberOfReturns); g.setAttribute('number of returns', new BufferAttribute(numberOfReturns, 1)); } if (e.data.pointSourceId) { let pointSourceId = new Uint16Array(e.data.pointSourceId); g.setAttribute('source id', new BufferAttribute(pointSourceId, 1)); } g.attributes.indices.normalized = true; let tightBoundingBox = new Box3( new Vector3().fromArray(e.data.tightBoundingBox.min), new Vector3().fromArray(e.data.tightBoundingBox.max) ); node.doneLoading( g, tightBoundingBox, numPoints, new Vector3(...e.data.mean)); Potree.workerPool.returnWorker(workerPath, worker); }; let toArray = (v) => [v.x, v.y, v.z]; let message = { buffer: buffer, schema: node.ept.schema, scale: node.ept.eptScale, offset: node.ept.eptOffset, mins: toArray(node.key.b.min) }; worker.postMessage(message, [message.buffer]); } }; /** * laslaz code taken and adapted from plas.io js-laslaz * http://plas.io/ * https://github.com/verma/plasio * * Thanks to Uday Verma and Howard Butler * */ class EptLaszipLoader { load(node) { if (node.loaded) return; let url = node.url() + '.laz'; let xhr = XHRFactory.createXMLHttpRequest(); xhr.open('GET', url, true); xhr.responseType = 'arraybuffer'; xhr.overrideMimeType('text/plain; charset=x-user-defined'); xhr.onreadystatechange = () => { if (xhr.readyState === 4) { if (xhr.status === 200) { let buffer = xhr.response; this.parse(node, buffer); } else { console.log('Failed ' + url + ': ' + xhr.status); } } }; xhr.send(null); } async parse(node, buffer){ let lf = new LASFile(buffer); let handler = new EptLazBatcher(node); try{ await lf.open(); lf.isOpen = true; const header = await lf.getHeader(); { let i = 0; let toArray = (v) => [v.x, v.y, v.z]; let mins = toArray(node.key.b.min); let maxs = toArray(node.key.b.max); let hasMoreData = true; while(hasMoreData){ const data = await lf.readData(1000000, 0, 1); let d = new LASDecoder( data.buffer, header.pointsFormatId, header.pointsStructSize, data.count, header.scale, header.offset, mins, maxs); d.extraBytes = header.extraBytes; d.pointsFormatId = header.pointsFormatId; handler.push(d); i += data.count; hasMoreData = data.hasMoreData; } header.totalRead = i; header.versionAsString = lf.versionAsString; header.isCompressed = lf.isCompressed; await lf.close(); lf.isOpen = false; } }catch(err){ console.error('Error reading LAZ:', err); if (lf.isOpen) { await lf.close(); lf.isOpen = false; } throw err; } } }; class EptLazBatcher { constructor(node) { this.node = node; } push(las) { let workerPath = Potree.scriptPath + '/workers/EptLaszipDecoderWorker.js'; let worker = Potree.workerPool.getWorker(workerPath); worker.onmessage = (e) => { let g = new BufferGeometry(); let numPoints = las.pointsCount; let positions = new Float32Array(e.data.position); let colors = new Uint8Array(e.data.color); let intensities = new Float32Array(e.data.intensity); let classifications = new Uint8Array(e.data.classification); let returnNumbers = new Uint8Array(e.data.returnNumber); let numberOfReturns = new Uint8Array(e.data.numberOfReturns); let pointSourceIDs = new Uint16Array(e.data.pointSourceID); let indices = new Uint8Array(e.data.indices); let gpsTime = new Float32Array(e.data.gpsTime); g.setAttribute('position', new BufferAttribute(positions, 3)); g.setAttribute('rgba', new BufferAttribute(colors, 4, true)); g.setAttribute('intensity', new BufferAttribute(intensities, 1)); g.setAttribute('classification', new BufferAttribute(classifications, 1)); g.setAttribute('return number', new BufferAttribute(returnNumbers, 1)); g.setAttribute('number of returns', new BufferAttribute(numberOfReturns, 1)); g.setAttribute('source id', new BufferAttribute(pointSourceIDs, 1)); g.setAttribute('indices', new BufferAttribute(indices, 4)); g.setAttribute('gpsTime', new BufferAttribute(gpsTime, 1)); this.node.gpsTime = e.data.gpsMeta; g.attributes.indices.normalized = true; let tightBoundingBox = new Box3( new Vector3().fromArray(e.data.tightBoundingBox.min), new Vector3().fromArray(e.data.tightBoundingBox.max) ); this.node.doneLoading( g, tightBoundingBox, numPoints, new Vector3(...e.data.mean)); Potree.workerPool.returnWorker(workerPath, worker); }; let message = { buffer: las.arrayb, numPoints: las.pointsCount, pointSize: las.pointSize, pointFormatID: las.pointsFormatId, scale: las.scale, offset: las.offset, mins: las.mins, maxs: las.maxs }; worker.postMessage(message, [message.buffer]); }; }; class EptZstandardLoader extends EptBinaryLoader { extension() { return '.zst'; } workerPath() { return Potree.scriptPath + '/workers/EptZstandardDecoderWorker.js'; } }; class ShapefileLoader{ constructor(){ this.transform = null; } async load(path, color){ const matLine = new LineMaterial( { color: color || 0xff0000, lineWidth: 3, // in pixels resolution: new Vector2(1000, 1000), dashed: false } ); const features = await this.loadShapefileFeatures(path); const node = new Object3D(); let jump = 0; for(const feature of features){//5是碎的 //if(feature.geometry.type!= 'MultiLineString' || ++jump != 5) continue const fnode = this.featureToSceneNode(feature, matLine); fnode && node.add(fnode); } let setResolution = (x, y) => { matLine.resolution.set(x, y); }; const result = { features: features, node: node, setResolution: setResolution, }; return result; } featureToSceneNode(feature, matLine){ //console.log(feature) let geometry = feature.geometry; let color = new Color(1, 1, 1); let transform = this.transform; if(transform === null){ transform = {forward: (v) => v}; } if(geometry.type === "Point"){ let sg = new SphereGeometry(1, 18, 18); let sm = new MeshNormalMaterial(); let s = new Mesh(sg, sm); let [long, lat] = geometry.coordinates; let pos = transform.forward([long, lat]); s.position.set(...pos, 20); s.scale.set(10, 10, 10); return s; }else if(geometry.type === "LineString"){ let coordinates = []; let min = new Vector3(Infinity, Infinity, Infinity); for(let i = 0; i < geometry.coordinates.length; i++){ let [long, lat] = geometry.coordinates[i]; let pos = transform.forward([long, lat]); min.x = Math.min(min.x, pos[0]); min.y = Math.min(min.y, pos[1]); min.z = Math.min(min.z, 20); coordinates.push(...pos, 20); if(i > 0 && i < geometry.coordinates.length - 1){ coordinates.push(...pos, 20); } } for(let i = 0; i < coordinates.length; i += 3){ coordinates[i+0] -= min.x; coordinates[i+1] -= min.y; coordinates[i+2] -= min.z; } const lineGeometry = new LineGeometry(); lineGeometry.setPositions( coordinates ); const line = new Line2( lineGeometry, matLine ); line.computeLineDistances(); line.scale.set( 1, 1, 1 ); line.position.copy(min); return line; }else if(geometry.type === "MultiLineString"){//xzw add 江门的那个文件 let coordinates = []; //console.warn('MultiLineString') //多组连续线段,组之间不连续 let min = new Vector3(Infinity, Infinity, Infinity); for(let i = 0; i < geometry.coordinates.length; i++){ let points = geometry.coordinates[i]; //有时候是两个点有时候多个 let coordinateSlice = []; points.forEach(point=>{ let [long, lat] = point; let pos = transform.forward([long, lat]); min.x = Math.min(min.x, pos[0]); min.y = Math.min(min.y, pos[1]); min.z = Math.min(min.z, 20); coordinateSlice.push(new Vector3(pos[0], pos[1], 20)); }); coordinates.push(coordinateSlice); } coordinates.forEach(coordinateSlice=> coordinateSlice.forEach(point=>point.sub(min) )); let line = LineDraw.createFatLine(coordinates,{ uncontinuous: true, mat:matLine /* color: 0xff0000, dashSize: 0.5, gapSize: 0.2, lineWidth: config$1.measure.lineWidth, */ }); line.position.copy(min); return line; }else if(geometry.type === "Polygon"){ for(let pc of geometry.coordinates){ let coordinates = []; let min = new Vector3(Infinity, Infinity, Infinity); for(let i = 0; i < pc.length; i++){ let [long, lat] = pc[i]; let pos = transform.forward([long, lat]); min.x = Math.min(min.x, pos[0]); min.y = Math.min(min.y, pos[1]); min.z = Math.min(min.z, 20); coordinates.push(...pos, 20); if(i > 0 && i < pc.length - 1){ coordinates.push(...pos, 20); } } for(let i = 0; i < coordinates.length; i += 3){ coordinates[i+0] -= min.x; coordinates[i+1] -= min.y; coordinates[i+2] -= min.z; } const lineGeometry = new LineGeometry(); lineGeometry.setPositions( coordinates ); const line = new Line2( lineGeometry, matLine ); line.computeLineDistances(); line.scale.set( 1, 1, 1 ); line.position.copy(min); return line; } }else { console.log("unhandled feature: ", geometry.type); } function getLine(coordinates){ } } async loadShapefileFeatures(file){ let features = []; if(file.split('.').pop() == 'shp'){ let source = await shapefile.open(file); while(true){ let result = await source.read(); if (result.done) { break; } if (result.value && result.value.type === 'Feature' && result.value.geometry !== undefined) { features.push(result.value); } } return features; }else if(file.split('.').pop() == 'json'){ return new Promise((resolve,reject)=>{ Potree.loadFile(file, {/* returnText:true */} , (data)=>{ console.log(data); resolve(data.features); }); }) } } }; const defaultColors = { "landuse": [0.5, 0.5, 0.5], "natural": [0.0, 1.0, 0.0], "places": [1.0, 0.0, 1.0], "points": [0.0, 1.0, 1.0], "roads": [1.0, 1.0, 0.0], "waterways": [0.0, 0.0, 1.0], "default": [0.9, 0.6, 0.1], }; function getColor(feature){ let color = defaultColors[feature]; if(!color){ color = defaultColors["default"]; } return color; } class Geopackage$1{ constructor(){ this.path = null; this.node = null; } }; class GeoPackageLoader{ constructor(){ } static async loadUrl(url, params){ await Promise.all([ Utils.loadScript(`${Potree.scriptPath}/lazylibs/geopackage/geopackage.js`), Utils.loadScript(`${Potree.scriptPath}/lazylibs/sql.js/sql-wasm.js`), ]); const result = await fetch(url); const buffer = await result.arrayBuffer(); params = params || {}; params.source = url; return GeoPackageLoader.loadBuffer(buffer, params); } static async loadBuffer(buffer, params){ await Promise.all([ Utils.loadScript(`${Potree.scriptPath}/lazylibs/geopackage/geopackage.js`), Utils.loadScript(`${Potree.scriptPath}/lazylibs/sql.js/sql-wasm.js`), ]); params = params || {}; const resolver = async (resolve) => { let transform = params.transform; if(!transform){ transform = {forward: (arg) => arg}; } const wasmPath = `${Potree.scriptPath}/lazylibs/sql.js/sql-wasm.wasm`; const SQL = await initSqlJs({ locateFile: filename => wasmPath}); const u8 = new Uint8Array(buffer); const data = await geopackage.open(u8); window.data = data; const geopackageNode = new Object3D(); geopackageNode.name = params.source; geopackageNode.potree = { source: params.source, }; const geo = new Geopackage$1(); geo.path = params.source; geo.node = geopackageNode; const tables = data.getTables(); for(const table of tables.features){ const dao = data.getFeatureDao(table); let boundingBox = dao.getBoundingBox(); boundingBox = boundingBox.projectBoundingBox(dao.projection, 'EPSG:4326'); const geoJson = data.queryForGeoJSONFeaturesInTable(table, boundingBox); const matLine = new LineMaterial( { color: new Color().setRGB(...getColor(table)), linewidth: 2, resolution: new Vector2(1000, 1000), dashed: false } ); const node = new Object3D(); node.name = table; geo.node.add(node); for(const [index, feature] of Object.entries(geoJson)){ //const featureNode = GeoPackageLoader.featureToSceneNode(feature, matLine, transform); const featureNode = GeoPackageLoader.featureToSceneNode(feature, matLine, dao.projection, transform); node.add(featureNode); } } resolve(geo); }; return new Promise(resolver); } static featureToSceneNode(feature, matLine, geopackageProjection, transform){ let geometry = feature.geometry; let color = new Color(1, 1, 1); if(feature.geometry.type === "Point"){ let sg = new SphereGeometry(1, 18, 18); let sm = new MeshNormalMaterial(); let s = new Mesh(sg, sm); let [long, lat] = geometry.coordinates; let pos = transform.forward(geopackageProjection.forward([long, lat])); s.position.set(...pos, 20); s.scale.set(10, 10, 10); return s; }else if(geometry.type === "LineString"){ let coordinates = []; let min = new Vector3(Infinity, Infinity, Infinity); for(let i = 0; i < geometry.coordinates.length; i++){ let [long, lat] = geometry.coordinates[i]; let pos = transform.forward(geopackageProjection.forward([long, lat])); min.x = Math.min(min.x, pos[0]); min.y = Math.min(min.y, pos[1]); min.z = Math.min(min.z, 20); coordinates.push(...pos, 20); if(i > 0 && i < geometry.coordinates.length - 1){ coordinates.push(...pos, 20); } } for(let i = 0; i < coordinates.length; i += 3){ coordinates[i+0] -= min.x; coordinates[i+1] -= min.y; coordinates[i+2] -= min.z; } const lineGeometry = new LineGeometry(); lineGeometry.setPositions( coordinates ); const line = new Line2( lineGeometry, matLine ); line.computeLineDistances(); line.scale.set( 1, 1, 1 ); line.position.copy(min); return line; }else if(geometry.type === "Polygon"){ for(let pc of geometry.coordinates){ let coordinates = []; let min = new Vector3(Infinity, Infinity, Infinity); for(let i = 0; i < pc.length; i++){ let [long, lat] = pc[i]; let pos = transform.forward(geopackageProjection.forward([long, lat])); min.x = Math.min(min.x, pos[0]); min.y = Math.min(min.y, pos[1]); min.z = Math.min(min.z, 20); coordinates.push(...pos, 20); if(i > 0 && i < pc.length - 1){ coordinates.push(...pos, 20); } } for(let i = 0; i < coordinates.length; i += 3){ coordinates[i+0] -= min.x; coordinates[i+1] -= min.y; coordinates[i+2] -= min.z; } const lineGeometry = new LineGeometry(); lineGeometry.setPositions( coordinates ); const line = new Line2( lineGeometry, matLine ); line.computeLineDistances(); line.scale.set( 1, 1, 1 ); line.position.copy(min); return line; } }else { console.log("unhandled feature: ", feature); } } }; class ClipVolume extends Object3D{ constructor(args){ super(); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = "clip_volume_" + this.constructor.counter; let alpha = args.alpha || 0; let beta = args.beta || 0; let gamma = args.gamma || 0; this.rotation.x = alpha; this.rotation.y = beta; this.rotation.z = gamma; this.clipOffset = 0.001; this.clipRotOffset = 1; let boxGeometry = new BoxGeometry(1, 1, 1); boxGeometry.computeBoundingBox(); let boxFrameGeometry = new Geometry(); { // bottom boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.5)); // top boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.5)); // sides boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, -0.5)); boxFrameGeometry.colors.push(new Vector3(1, 1, 1)); } let planeFrameGeometry = new Geometry(); { // middle line planeFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.0)); planeFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.0)); planeFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.0)); planeFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.0)); planeFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.0)); planeFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.0)); planeFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.0)); planeFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.0)); } this.dimension = new Vector3(1, 1, 1); this.material = new MeshBasicMaterial( { color: 0x00ff00, transparent: true, opacity: 0.3, depthTest: true, depthWrite: false} ); this.box = new Mesh(boxGeometry, this.material); this.box.geometry.computeBoundingBox(); this.boundingBox = this.box.geometry.boundingBox; this.add(this.box); this.frame = new LineSegments( boxFrameGeometry, new LineBasicMaterial({color: 0x000000})); this.add(this.frame); this.planeFrame = new LineSegments( planeFrameGeometry, new LineBasicMaterial({color: 0xff0000})); this.add(this.planeFrame); // set default thickness this.setScaleZ(0.1); // create local coordinate system let createArrow = (name, direction, color) => { let material = new MeshBasicMaterial({ color: color, depthTest: false, depthWrite: false}); let shaftGeometry = new Geometry(); shaftGeometry.vertices.push(new Vector3(0, 0, 0)); shaftGeometry.vertices.push(new Vector3(0, 1, 0)); let shaftMaterial = new LineBasicMaterial({ color: color, depthTest: false, depthWrite: false, transparent: true }); let shaft = new Line(shaftGeometry, shaftMaterial); shaft.name = name + "_shaft"; let headGeometry = new CylinderGeometry(0, 0.04, 0.1, 10, 1, false); let headMaterial = material; let head = new Mesh(headGeometry, headMaterial); head.name = name + "_head"; head.position.y = 1; let arrow = new Object3D(); arrow.name = name; arrow.add(shaft); arrow.add(head); return arrow; }; this.arrowX = createArrow("arrow_x", new Vector3(1, 0, 0), 0xFF0000); this.arrowY = createArrow("arrow_y", new Vector3(0, 1, 0), 0x00FF00); this.arrowZ = createArrow("arrow_z", new Vector3(0, 0, 1), 0x0000FF); this.arrowX.rotation.z = -Math.PI / 2; this.arrowZ.rotation.x = Math.PI / 2; this.arrowX.visible = false; this.arrowY.visible = false; this.arrowZ.visible = false; this.add(this.arrowX); this.add(this.arrowY); this.add(this.arrowZ); { // event listeners this.addEventListener("ui_select", e => { this.arrowX.visible = true; this.arrowY.visible = true; this.arrowZ.visible = true; }); this.addEventListener("ui_deselect", e => { this.arrowX.visible = false; this.arrowY.visible = false; this.arrowZ.visible = false; }); this.addEventListener("select", e => { let scene_header = $("#" + this.name + " .scene_header"); if(!scene_header.next().is(":visible")) { scene_header.click(); } }); this.addEventListener("deselect", e => { let scene_header = $("#" + this.name + " .scene_header"); if(scene_header.next().is(":visible")) { scene_header.click(); } }); } this.update(); }; setClipOffset(offset) { this.clipOffset = offset; } setClipRotOffset(offset) { this.clipRotOffset = offset; } setScaleX(x) { this.box.scale.x = x; this.frame.scale.x = x; this.planeFrame.scale.x = x; } setScaleY(y) { this.box.scale.y = y; this.frame.scale.y = y; this.planeFrame.scale.y = y; } setScaleZ(z) { this.box.scale.z = z; this.frame.scale.z = z; this.planeFrame.scale.z = z; } offset(args) { let cs = args.cs || null; let axis = args.axis || null; let dir = args.dir || null; if(!cs || !axis || !dir) return; if(axis === "x") { if(cs === "local") { this.position.add(this.localX.clone().multiplyScalar(dir * this.clipOffset)); } else if(cs === "global") { this.position.x = this.position.x + dir * this.clipOffset; } }else if(axis === "y") { if(cs === "local") { this.position.add(this.localY.clone().multiplyScalar(dir * this.clipOffset)); } else if(cs === "global") { this.position.y = this.position.y + dir * this.clipOffset; } }else if(axis === "z") { if(cs === "local") { this.position.add(this.localZ.clone().multiplyScalar(dir * this.clipOffset)); } else if(cs === "global") { this.position.z = this.position.z + dir * this.clipOffset; } } this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); } rotate(args) { let cs = args.cs || null; let axis = args.axis || null; let dir = args.dir || null; if(!cs || !axis || !dir) return; if(cs === "local") { if(axis === "x") { this.rotateOnAxis(new Vector3(1, 0, 0), dir * this.clipRotOffset * Math.PI / 180); } else if(axis === "y") { this.rotateOnAxis(new Vector3(0, 1, 0), dir * this.clipRotOffset * Math.PI / 180); } else if(axis === "z") { this.rotateOnAxis(new Vector3(0, 0, 1), dir * this.clipRotOffset * Math.PI / 180); } } else if(cs === "global") { let rotaxis = new Vector4(1, 0, 0, 0); if(axis === "y") { rotaxis = new Vector4(0, 1, 0, 0); } else if(axis === "z") { rotaxis = new Vector4(0, 0, 1, 0); } this.updateMatrixWorld(); let invM = newthis.matrixWorld.clone().invert(); rotaxis = rotaxis.applyMatrix4(invM).normalize(); rotaxis = new Vector3(rotaxis.x, rotaxis.y, rotaxis.z); this.rotateOnAxis(rotaxis, dir * this.clipRotOffset * Math.PI / 180); } this.updateLocalSystem(); this.dispatchEvent({"type": "clip_volume_changed", "viewer": viewer, "volume": this}); } update(){ this.boundingBox = this.box.geometry.boundingBox; this.boundingSphere = this.boundingBox.getBoundingSphere(new Sphere()); this.box.visible = false; this.updateLocalSystem(); }; updateLocalSystem() { // extract local coordinate axes let rotQuat = this.getWorldQuaternion(); this.localX = new Vector3(1, 0, 0).applyQuaternion(rotQuat).normalize(); this.localY = new Vector3(0, 1, 0).applyQuaternion(rotQuat).normalize(); this.localZ = new Vector3(0, 0, 1).applyQuaternion(rotQuat).normalize(); } raycast(raycaster, intersects){ let is = []; this.box.raycast(raycaster, is); if(is.length > 0){ let I = is[0]; intersects.push({ distance: I.distance, object: this, point: I.point.clone() }); } }; }; //这个文件只是clipping polygon的,没用到 class ClippingTool extends EventDispatcher$1{ constructor(viewer){ super(); this.viewer = viewer; this.maxPolygonVertices = 8; this.addEventListener("start_inserting_clipping_volume", e => { this.viewer.dispatchEvent({ type: "cancel_insertions" }); }); this.sceneMarker = new Scene(); this.sceneVolume = new Scene(); this.sceneVolume.name = "scene_clip_volume"; this.viewer.inputHandler.registerInteractiveScene(this.sceneVolume); this.onRemove = e => { this.sceneVolume.remove(e.volume); }; this.onAdd = e => { this.sceneVolume.add(e.volume); }; this.viewer.inputHandler.addEventListener("delete", e => { let volumes = e.selection.filter(e => (e instanceof ClipVolume)); volumes.forEach(e => this.viewer.scene.removeClipVolume(e)); let polyVolumes = e.selection.filter(e => (e instanceof PolygonClipVolume)); polyVolumes.forEach(e => this.viewer.scene.removePolygonClipVolume(e)); }); } setScene(scene){ if(this.scene === scene){ return; } if(this.scene){ this.scene.removeEventListeners("clip_volume_added", this.onAdd); this.scene.removeEventListeners("clip_volume_removed", this.onRemove); this.scene.removeEventListeners("polygon_clip_volume_added", this.onAdd); this.scene.removeEventListeners("polygon_clip_volume_removed", this.onRemove); } this.scene = scene; this.scene.addEventListener("clip_volume_added", this.onAdd); this.scene.addEventListener("clip_volume_removed", this.onRemove); this.scene.addEventListener("polygon_clip_volume_added", this.onAdd); this.scene.addEventListener("polygon_clip_volume_removed", this.onRemove); } startInsertion(args = {}) { let type = args.type || null; if(!type) return null; let domElement = this.viewer.renderer.domElement; let canvasSize = this.viewer.renderer.getSize(new Vector2()); let svg = $(` `); $(domElement.parentElement).append(svg); let polyClipVol = new PolygonClipVolume(this.viewer.scene.getActiveCamera().clone()); this.dispatchEvent({"type": "start_inserting_clipping_volume"}); this.viewer.scene.addPolygonClipVolume(polyClipVol); this.sceneMarker.add(polyClipVol); let cancel = { callback: null }; let insertionCallback = (e) => { if(e.button === MOUSE.LEFT){ polyClipVol.addMarker(); // SVC Screen Line svg.find("polyline").each((index, target) => { let newPoint = svg[0].createSVGPoint(); newPoint.x = e.offsetX; newPoint.y = e.offsetY; let polyline = target.points.appendItem(newPoint); }); if(polyClipVol.markers.length > this.maxPolygonVertices){ cancel.callback(); } this.viewer.inputHandler.startDragging( polyClipVol.markers[polyClipVol.markers.length - 1]); }else if(e.button === MOUSE.RIGHT){ cancel.callback(e); } }; cancel.callback = e => { //let first = svg.find("polyline")[0].points[0]; //svg.find("polyline").each((index, target) => { // let newPoint = svg[0].createSVGPoint(); // newPoint.x = first.x; // newPoint.y = first.y; // let polyline = target.points.appendItem(newPoint); //}); svg.remove(); if(polyClipVol.markers.length > 3) { polyClipVol.removeLastMarker(); polyClipVol.initialized = true; } else { this.viewer.scene.removePolygonClipVolume(polyClipVol); } this.viewer.renderer.domElement.removeEventListener("mouseup", insertionCallback, true); this.viewer.removeEventListener("cancel_insertions", cancel.callback); this.viewer.inputHandler.enabled = true; }; this.viewer.addEventListener("cancel_insertions", cancel.callback); this.viewer.renderer.domElement.addEventListener("mouseup", insertionCallback , true); this.viewer.inputHandler.enabled = false; polyClipVol.addMarker(); this.viewer.inputHandler.startDragging( polyClipVol.markers[polyClipVol.markers.length - 1]); return polyClipVol; } update() { } }; var GeoTIFF = (function (exports) { 'use strict'; const Endianness = new Enum({ LITTLE: "II", BIG: "MM", }); const Type = new Enum({ BYTE: {value: 1, bytes: 1}, ASCII: {value: 2, bytes: 1}, SHORT: {value: 3, bytes: 2}, LONG: {value: 4, bytes: 4}, RATIONAL: {value: 5, bytes: 8}, SBYTE: {value: 6, bytes: 1}, UNDEFINED: {value: 7, bytes: 1}, SSHORT: {value: 8, bytes: 2}, SLONG: {value: 9, bytes: 4}, SRATIONAL: {value: 10, bytes: 8}, FLOAT: {value: 11, bytes: 4}, DOUBLE: {value: 12, bytes: 8}, }); const Tag = new Enum({ IMAGE_WIDTH: 256, IMAGE_HEIGHT: 257, BITS_PER_SAMPLE: 258, COMPRESSION: 259, PHOTOMETRIC_INTERPRETATION: 262, STRIP_OFFSETS: 273, ORIENTATION: 274, SAMPLES_PER_PIXEL: 277, ROWS_PER_STRIP: 278, STRIP_BYTE_COUNTS: 279, X_RESOLUTION: 282, Y_RESOLUTION: 283, PLANAR_CONFIGURATION: 284, RESOLUTION_UNIT: 296, SOFTWARE: 305, COLOR_MAP: 320, SAMPLE_FORMAT: 339, MODEL_PIXEL_SCALE: 33550, // [GeoTIFF] TYPE: double N: 3 MODEL_TIEPOINT: 33922, // [GeoTIFF] TYPE: double N: 6 * NUM_TIEPOINTS GEO_KEY_DIRECTORY: 34735, // [GeoTIFF] TYPE: short N: >= 4 GEO_DOUBLE_PARAMS: 34736, // [GeoTIFF] TYPE: short N: variable GEO_ASCII_PARAMS: 34737, // [GeoTIFF] TYPE: ascii N: variable }); const typeMapping = new Map([ [Type.BYTE, Uint8Array], [Type.ASCII, Uint8Array], [Type.SHORT, Uint16Array], [Type.LONG, Uint32Array], [Type.RATIONAL, Uint32Array], [Type.SBYTE, Int8Array], [Type.UNDEFINED, Uint8Array], [Type.SSHORT, Int16Array], [Type.SLONG, Int32Array], [Type.SRATIONAL, Int32Array], [Type.FLOAT, Float32Array], [Type.DOUBLE, Float64Array], ]); class IFDEntry{ constructor(tag, type, count, offset, value){ this.tag = tag; this.type = type; this.count = count; this.offset = offset; this.value = value; } } class Image{ constructor(){ this.width = 0; this.height = 0; this.buffer = null; this.metadata = []; } } class Reader{ constructor(){ } static read(data){ let endiannessTag = String.fromCharCode(...Array.from(data.slice(0, 2))); let endianness = Endianness.fromValue(endiannessTag); let tiffCheckTag = data.readUInt8(2); if(tiffCheckTag !== 42){ throw new Error("not a valid tiff file"); } let offsetToFirstIFD = data.readUInt32LE(4); console.log("offsetToFirstIFD", offsetToFirstIFD); let ifds = []; let IFDsRead = false; let currentIFDOffset = offsetToFirstIFD; let i = 0; while(IFDsRead || i < 100){ console.log("currentIFDOffset", currentIFDOffset); let numEntries = data.readUInt16LE(currentIFDOffset); let nextIFDOffset = data.readUInt32LE(currentIFDOffset + 2 + numEntries * 12); console.log("next offset: ", currentIFDOffset + 2 + numEntries * 12); let entryBuffer = data.slice(currentIFDOffset + 2, currentIFDOffset + 2 + 12 * numEntries); for(let i = 0; i < numEntries; i++){ let tag = Tag.fromValue(entryBuffer.readUInt16LE(i * 12)); let type = Type.fromValue(entryBuffer.readUInt16LE(i * 12 + 2)); let count = entryBuffer.readUInt32LE(i * 12 + 4); let offsetOrValue = entryBuffer.readUInt32LE(i * 12 + 8); let valueBytes = type.bytes * count; let value; if(valueBytes <= 4){ value = offsetOrValue; }else { let valueBuffer = new Uint8Array(valueBytes); valueBuffer.set(data.slice(offsetOrValue, offsetOrValue + valueBytes)); let ArrayType = typeMapping.get(type); value = new ArrayType(valueBuffer.buffer); if(type === Type.ASCII){ value = String.fromCharCode(...value); } } let ifd = new IFDEntry(tag, type, count, offsetOrValue, value); ifds.push(ifd); } console.log("nextIFDOffset", nextIFDOffset); if(nextIFDOffset === 0){ break; } currentIFDOffset = nextIFDOffset; i++; } let ifdForTag = (tag) => { for(let entry of ifds){ if(entry.tag === tag){ return entry; } } return null; }; let width = ifdForTag(Tag.IMAGE_WIDTH, ifds).value; let height = ifdForTag(Tag.IMAGE_HEIGHT, ifds).value; let compression = ifdForTag(Tag.COMPRESSION, ifds).value; let rowsPerStrip = ifdForTag(Tag.ROWS_PER_STRIP, ifds).value; let ifdStripOffsets = ifdForTag(Tag.STRIP_OFFSETS, ifds); let ifdStripByteCounts = ifdForTag(Tag.STRIP_BYTE_COUNTS, ifds); let numStrips = Math.ceil(height / rowsPerStrip); let stripByteCounts = []; for(let i = 0; i < ifdStripByteCounts.count; i++){ let type = ifdStripByteCounts.type; let offset = ifdStripByteCounts.offset + i * type.bytes; let value; if(type === Type.SHORT){ value = data.readUInt16LE(offset); }else if(type === Type.LONG){ value = data.readUInt32LE(offset); } stripByteCounts.push(value); } let stripOffsets = []; for(let i = 0; i < ifdStripOffsets.count; i++){ let type = ifdStripOffsets.type; let offset = ifdStripOffsets.offset + i * type.bytes; let value; if(type === Type.SHORT){ value = data.readUInt16LE(offset); }else if(type === Type.LONG){ value = data.readUInt32LE(offset); } stripOffsets.push(value); } let imageBuffer = new Uint8Array(width * height * 3); let linesProcessed = 0; for(let i = 0; i < numStrips; i++){ let stripOffset = stripOffsets[i]; let stripBytes = stripByteCounts[i]; let stripData = data.slice(stripOffset, stripOffset + stripBytes); let lineBytes = width * 3; for(let y = 0; y < rowsPerStrip; y++){ let line = stripData.slice(y * lineBytes, y * lineBytes + lineBytes); imageBuffer.set(line, linesProcessed * lineBytes); if(line.length === lineBytes){ linesProcessed++; }else { break; } } } console.log(`width: ${width}`); console.log(`height: ${height}`); console.log(`numStrips: ${numStrips}`); console.log("stripByteCounts", stripByteCounts.join(", ")); console.log("stripOffsets", stripOffsets.join(", ")); let image = new Image(); image.width = width; image.height = height; image.buffer = imageBuffer; image.metadata = ifds; return image; } } class Exporter{ constructor(){ } static toTiffBuffer(image, params = {}){ let offsetToFirstIFD = 8; let headerBuffer = new Uint8Array([0x49, 0x49, 42, 0, offsetToFirstIFD, 0, 0, 0]); let [width, height] = [image.width, image.height]; let ifds = [ new IFDEntry(Tag.IMAGE_WIDTH, Type.SHORT, 1, null, width), new IFDEntry(Tag.IMAGE_HEIGHT, Type.SHORT, 1, null, height), new IFDEntry(Tag.BITS_PER_SAMPLE, Type.SHORT, 4, null, new Uint16Array([8, 8, 8, 8])), new IFDEntry(Tag.COMPRESSION, Type.SHORT, 1, null, 1), new IFDEntry(Tag.PHOTOMETRIC_INTERPRETATION, Type.SHORT, 1, null, 2), new IFDEntry(Tag.ORIENTATION, Type.SHORT, 1, null, 1), new IFDEntry(Tag.SAMPLES_PER_PIXEL, Type.SHORT, 1, null, 4), new IFDEntry(Tag.ROWS_PER_STRIP, Type.LONG, 1, null, height), new IFDEntry(Tag.STRIP_BYTE_COUNTS, Type.LONG, 1, null, width * height * 3), new IFDEntry(Tag.PLANAR_CONFIGURATION, Type.SHORT, 1, null, 1), new IFDEntry(Tag.RESOLUTION_UNIT, Type.SHORT, 1, null, 1), new IFDEntry(Tag.SOFTWARE, Type.ASCII, 6, null, "......"), new IFDEntry(Tag.STRIP_OFFSETS, Type.LONG, 1, null, null), new IFDEntry(Tag.X_RESOLUTION, Type.RATIONAL, 1, null, new Uint32Array([1, 1])), new IFDEntry(Tag.Y_RESOLUTION, Type.RATIONAL, 1, null, new Uint32Array([1, 1])), ]; if(params.ifdEntries){ ifds.push(...params.ifdEntries); } let valueOffset = offsetToFirstIFD + 2 + ifds.length * 12 + 4; // create 12 byte buffer for each ifd and variable length buffers for ifd values let ifdEntryBuffers = new Map(); let ifdValueBuffers = new Map(); for(let ifd of ifds){ let entryBuffer = new ArrayBuffer(12); let entryView = new DataView(entryBuffer); let valueBytes = ifd.type.bytes * ifd.count; entryView.setUint16(0, ifd.tag.value, true); entryView.setUint16(2, ifd.type.value, true); entryView.setUint32(4, ifd.count, true); if(ifd.count === 1 && ifd.type.bytes <= 4){ entryView.setUint32(8, ifd.value, true); }else { entryView.setUint32(8, valueOffset, true); let valueBuffer = new Uint8Array(ifd.count * ifd.type.bytes); if(ifd.type === Type.ASCII){ valueBuffer.set(new Uint8Array(ifd.value.split("").map(c => c.charCodeAt(0)))); }else { valueBuffer.set(new Uint8Array(ifd.value.buffer)); } ifdValueBuffers.set(ifd.tag, valueBuffer); valueOffset = valueOffset + valueBuffer.byteLength; } ifdEntryBuffers.set(ifd.tag, entryBuffer); } let imageBufferOffset = valueOffset; new DataView(ifdEntryBuffers.get(Tag.STRIP_OFFSETS)).setUint32(8, imageBufferOffset, true); let concatBuffers = (buffers) => { let totalLength = buffers.reduce( (sum, buffer) => (sum + buffer.byteLength), 0); let merged = new Uint8Array(totalLength); let offset = 0; for(let buffer of buffers){ merged.set(new Uint8Array(buffer), offset); offset += buffer.byteLength; } return merged; }; let ifdBuffer = concatBuffers([ new Uint16Array([ifds.length]), ...ifdEntryBuffers.values(), new Uint32Array([0])]); let ifdValueBuffer = concatBuffers([...ifdValueBuffers.values()]); let tiffBuffer = concatBuffers([ headerBuffer, ifdBuffer, ifdValueBuffer, image.buffer ]); return {width: width, height: height, buffer: tiffBuffer}; } } exports.Tag = Tag; exports.Type = Type; exports.IFDEntry = IFDEntry; exports.Image = Image; exports.Reader = Reader; exports.Exporter = Exporter; return exports; }({})); class Message{ constructor(content){ this.content = content; let closeIcon = `${exports.resourcePath}/icons/close.svg`; this.element = $(`
`); this.elClose = this.element.find("img[name=close]"); this.elContainer = this.element.find("span[name=content_container]"); if(typeof content === "string"){ this.elContainer.append($(`${content}`)); }else { this.elContainer.append(content); } } setMessage(content){ this.elContainer.empty(); if(typeof content === "string"){ this.elContainer.append($(`${content}`)); }else { this.elContainer.append(content); } } } class PointCloudSM{ constructor(potreeRenderer){ this.potreeRenderer = potreeRenderer; this.threeRenderer = this.potreeRenderer.threeRenderer; this.target = new WebGLRenderTarget(2 * 1024, 2 * 1024, { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat, type: FloatType }); this.target.depthTexture = new DepthTexture(); this.target.depthTexture.type = UnsignedIntType; //this.threeRenderer.setClearColor(0x000000, 1); this.threeRenderer.setClearColor(0xff0000, 1); //HACK? removed while moving to three.js 109 //this.threeRenderer.clearTarget(this.target, true, true, true); { const oldTarget = this.threeRenderer.getRenderTarget(); this.threeRenderer.setRenderTarget(this.target); this.threeRenderer.clear(true, true, true); this.threeRenderer.setRenderTarget(oldTarget); } } setLight(light){ this.light = light; let fov = (180 * light.angle) / Math.PI; let aspect = light.shadow.mapSize.width / light.shadow.mapSize.height; let near = 0.1; let far = light.distance === 0 ? 10000 : light.distance; this.camera = new PerspectiveCamera(fov, aspect, near, far); this.camera.up.set(0, 0, 1); this.camera.position.copy(light.position); let target = new Vector3().subVectors(light.position, light.getWorldDirection(new Vector3())); this.camera.lookAt(target); this.camera.updateProjectionMatrix(); this.camera.updateMatrix(); this.camera.updateMatrixWorld(); this.camera.matrixWorldInverse.copy(this.camera.matrixWorld).invert(); } setSize(width, height){ if(this.target.width !== width || this.target.height !== height){ this.target.dispose(); } this.target.setSize(width, height); } render(scene, camera){ this.threeRenderer.setClearColor(0x000000, 1); const oldTarget = this.threeRenderer.getRenderTarget(); this.threeRenderer.setRenderTarget(this.target); this.threeRenderer.clear(true, true, true); this.potreeRenderer.render(scene, this.camera, this.target, {}); this.threeRenderer.setRenderTarget(oldTarget); } } class Label extends EventDispatcher{ constructor(o={}){ super(); this.position = o.pos; this.text = o.text || ''; this.elem = $('
'); o.className && this.elem.addClass(o.className); this.elem.find('a').html(this.text); $("#potree_labels").append(this.elem); this.pos2d = new Vector3; this.dom = o.dom || viewer.renderArea; this.camera = o.camera || viewer.scene.getActiveCamera(); let update = (e)=>{ this.update(e); }; viewer.addEventListener('camera_changed', update); this.addEventListener('dispose', ()=>{ viewer.removeEventListener('camera_changed', update); this.dispose(); }); } update(){ if(!this.position || this.elem.hasClass('unvisible'))return var p = Utils.getPos2d(this.position, viewer.mainViewport,this.dom); if(!p.trueSide){ this.elem.addClass("hide"); return; } this.elem.css({ left: p.pos.x +'px', top: p.pos.y +'px' }); this.elem.removeClass("hide"); this.pos2d = p.vector; } setVisible(visi){ if(!visi){ this.elem.addClass("unvisible"); }else { this.elem.removeClass("unvisible"); this.update(); } } setText(text){ this.text = text || ''; this.elem.find('a').html(this.text); } setPos(pos){ this.position = pos; } dispose(){ this.elem.remove(); this.removeAllListeners(); } } //THREE.Vector2.name1 = 'ctrlPolygon' const verticalLine = new Line3(); //控制点和边的合集。具有可以拖拽修改的功能,拖拽时能防止线相交。 class ctrlPolygon extends Object3D { constructor (type, prop) { super(); this.type = type; this.maxMarkers = Number.MAX_SAFE_INTEGER; this.transformData(prop); for(let i in prop){ this[i] = prop[i]; } if((this.atPlane || this.showArea ) && this.closed && this.dimension == '2d'){ this.areaPlane = this.createAreaPlane(); this.add(this.areaPlane); } //数据--刚开始一定是空的 this.points = []; //mesh 不一定有 this.markers = []; this.edges = []; this.center; } initData(prop){ //开始加数据 prop.dataset_points && (this.dataset_points = prop.dataset_points); prop.datasetId && (this.datasetId = prop.datasetId); prop.points_datasets && (this.points_datasets = prop.points_datasets); if(Potree.settings.editType == 'merge' || this.measureType == 'MulDistance Ring'){ //融合页面没有地图,measure的不需要指定datasetId,每个点都有各自的datasetId,跟着各自的模型走 if(this.dataset_points){ this.dataset_points = this.dataset_points.map(e=>{ return e && new Vector3().copy(e) }); prop.points = this.dataset_points.map((p,i)=>{ return Potree.Utils.datasetPosTransform({fromDataset:true, datasetId:this.points_datasets[i], position: p}) }); if(prop.points.some(e=>e == void 0)){ return false } }else { this.dataset_points = []; } } if(prop.points){ for(const p of prop.points){ const pos = new Vector3().copy(p); this.addMarker({point:pos}); } if(Potree.settings.editType != 'merge' && this.measureType != 'MulDistance Ring'){ if(this.datasetId != void 0){//初始化位置 if(this.dataset_points){ this.dataset_points = this.dataset_points.map(e=>{ return e && new Vector3().copy(e) }); this.transformByPointcloud(); //根据dataset_points和this.datasetId生成points } }else { if(prop.dataset_points && prop.dataset_points.some(e=>e != void 0)){ console.error('存在测量线的datasetId为空而dataset_points有值,请检查并删除:'+this.sid);//存在过的bug,原因未知,可能是后台处理dataset时替换的错误:http://192.168.0.21/index.php?m=bug&f=view&bugID=23601 console.log(this); } } } this.facePlane = this.getFacePlane(); this.getPoint2dInfo(this.points); this.update({ifUpdateMarkers:true}); //this.dragChange(new THREE.Vector3().copy(prop.points[prop.points.length-1]), prop.points.length-1); this.setSelected(false ); this.markers.forEach(marker=>{marker.dispatchEvent('addHoverEvent'); }); return true } } addMarker(o={}){ var index = o.index == void 0 ? this.points.length : o.index; //要当第几个 this.points = [...this.points.slice(0,index), o.point.clone(), ...this.points.slice(index,this.points.length)]; //this.points.push(o.point); if('dataset_point' in o){//需要增加,可能是null this.dataset_points = this.dataset_points ? [...this.dataset_points.slice(0,index), o.dataset_point && o.dataset_point.clone(), ...this.dataset_points.slice(index,this.dataset_points.length)] : null; //如果原本是null,新加的应该也是 } if('points_dataset' in o){ this.points_datasets = [...this.points_datasets.slice(0,index), o.points_dataset, ...this.points_datasets.slice(index,this.points_datasets.length)]; } if(o.marker){ this.add(o.marker); this.markers = [...this.markers.slice(0,index), o.marker, ...this.markers.slice(index,this.markers.length)]; this.updateMarker(o.marker, o.point); o.marker.addEventListener('drag', this.dragMarker.bind(this),{importance:2}); o.marker.addEventListener('drop', this.dropMarker.bind(this),{importance:2}); o.marker.createTime = Date.now(); let addHoverEvent = (e)=>{ let mouseover = (e) => { this.setMarkerSelected(e.object, 'hover', 'single'); viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"markerMove" }); }; let mouseleave = (e) => { this.setMarkerSelected(e.object, 'unhover', 'single'); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"markerMove" }); }; o.marker.addEventListener('mouseover', mouseover); o.marker.addEventListener('mouseleave', mouseleave); o.marker.addEventListener('startDragging', (e)=>{//for mobile this.setMarkerSelected(o.marker, 'hover', 'single'); this.dispatchEvent('startDragging'); }); o.marker.addEventListener('drop', (e)=>{//for mobile this.setMarkerSelected(o.marker, 'unhover', 'single'); }); o.marker.removeEventListener('addHoverEvent',addHoverEvent); }; o.marker.addEventListener('addHoverEvent',addHoverEvent);//当非isNew时才添加事件 if(!this.isNew){ o.marker.dispatchEvent('addHoverEvent'); } } if(o.edge){ this.add(o.edge); this.edges = [...this.edges.slice(0,index), o.edge, ...this.edges.slice(index,this.edges.length)]; } } dragMarker(e){ var I, atMap; if(e.hoverViewport != e.drag.dragViewport){//不能使用e.dragViewport,要使用drag中的,因为drag中存储的要一直继承下来,不因mouseup了而改变。 viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"polygon_AtWrongPlace" }); return } if(e.drag.pointerDelta.length() == 0 && !this.isNew){ //部分设备(华为matePad11)在touchstart后立即执行了touchmove,导致marker立即移动,需要屏蔽..(刚创建时也会是0) return } viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace" }); atMap = e.drag.dragViewport.name == 'mapViewport'; if(atMap && this.unableDragAtMap){ e.drag.object = null; //取消拖拽 return } e.drag.object.isDragging = true; I = e.intersect && (e.intersect.adsorption ? e.intersect.location : (e.intersect.orthoIntersect || e.intersect.location)); if(viewer.inputHandler.pressedKeys[18] || Potree.settings.dragPolyBeyondPoint&&!I ){//alt dragPolyBeyondPoint可以平移拖拽到无点的地方---测试用 let i = this.markers.indexOf(e.drag.object); I = this.points[i].clone(); const projected = I.clone().project(e.drag.dragViewport.camera); projected.x = e.pointer.x; projected.y = e.pointer.y; const unprojected = projected.clone().unproject(e.drag.dragViewport.camera); I.copy(unprojected); } if (I) { let i = this.markers.indexOf(e.drag.object); if (i !== -1) { this.dragChange(I.clone(), i, atMap); if(this.points_datasets){ if(e.intersect){ if(e.intersect.pointcloud) this.points_datasets[i] = e.intersect.pointcloud.dataset_id; else if(e.intersect.object) this.points_datasets[i] = e.intersect.object.dataset_id; else { //地图 if(this.measureType == 'MulDistance Ring'){//因为它的每个point跟着各自的dataset走,而不是整体的dataset let pointcloud = viewer.findClosestDatasetOnMap(I) || viewer.scene.pointclouds[0]; this.points_datasets[i] = pointcloud.dataset_id; }else this.points_datasets[i] = null; } } } } this.editStateChange(true); return true } }; dragChange(intersectPos, i, atMap){ let len = this.markers.length; let oldPoint = this.points[i]; if(atMap){ intersectPos.setZ(oldPoint.z); //在地图上拖拽,不改变其高度。 } let location = intersectPos.clone(); if(this.faceDirection && this.maxMarkers == 2 && len == 2){//add 固定方向的点不直接拖拽 var p1 = this.markers[0].position; if(this.faceDirection == 'horizontal'){ var projectPos = location.clone().setZ(p1.z); }else { var projectPos = p1.clone().setZ(location.z); } //var p2 = p1.clone().add(this.direction) //var projectPos = math.getFootPoint(location, p1, p2) LineDraw.updateLine(this.guideLine, [location, projectPos]); location = projectPos; this.guideLine.visible = true; }else if( len > 1 ){ var points = this.points.map(e=>e.clone()); points[i].copy(location); //算normal需要提前确认point //若为定义了面朝向的矩形 if(this.faceDirection == 'horizontal'){ if(len == 2){ location.setZ(points[0].z); } if(!this.facePlane){//一个点就能确定面 this.facePlane = new Plane().setFromNormalAndCoplanarPoint( new Vector3(0,0,1), this.points[0] ); } }else if(this.faceDirection == 'vertical'){//当有两个点时, 有两个方向的可能 if(len == 2){ if(this.isRect){ let vec = points[0].clone().sub(location); if(Math.sqrt(vec.x*vec.x+vec.y*vec.y) > Math.abs(vec.z) ){//水平(高度差小于水平距离时) location.setZ(points[0].z); //this.cannotConfirmNormal = false;//能确定面为水平方向 }else {//垂直 (当两点一样时也属于这种) location.setX(points[0].x); location.setY(points[0].y); //this.cannotConfirmNormal = true; //不能确定面,因第三点可绕着纵轴线自由移动 } } }else { {//判断cannotConfirmNormal. 如果前几段都在竖直线上,就不能固定出面方向。 this.cannotConfirmNormal = true; let max = this.isRect ? 1 : len-2; for(let i=0;inew Vector2(e.x,e.y)); var points2 = getDifferentPoint(points_, 2); if(points2){ let normal = math.getNormal2d({p1:points2[0], p2:points2[1]}); normal = new Vector3(normal.x, normal.y, 0); this.facePlane = new Plane().setFromNormalAndCoplanarPoint( normal, this.points[0] ); } } } } if(len > 2){ if(!this.faceDirection && this.showArea){ if(len == 3 || this.isRect) this.cannotConfirmNormal = true; //当第三个点固定后(有四个点时)才能固定面 if(!this.facePlane || this.cannotConfirmNormal){ var points3 = getDifferentPoint(points, 3);//只有找到三个不同的点算拥有面和area if(points3){ this.facePlane = new Plane().setFromCoplanarPoints(...points3 ); } } } if(this.atPlane && this.facePlane && !this.cannotConfirmNormal ){//之后加的点一定要在面上 if(atMap){ //地图上用垂直线,得到和面的交点。 verticalLine.set(location.clone().setZ(100000), location.clone().setZ(-100000));//确保长度范围覆盖所有测量面 location = this.facePlane.intersectLine(verticalLine, new Vector3() ); if(!location) return; }else { location = this.facePlane.projectPoint(intersectPos, new Vector3() ); } } points[i].copy(location);//再copy确认一次 if(len == 3 && this.faceDirection == 'horizontal' && this.closed){ //normal方向还不确定 会影响label在里侧还是外侧 let facePlane = new Plane().setFromCoplanarPoints(...points); if(facePlane.normal.z && facePlane.normal.z * this.facePlane.normal.z < 0){ this.facePlane.normal.z *= -1, this.facePlane.constant *= -1; //console.log(this.facePlane.normal, this.facePlane.constant) } } if(this.isRect){ //是矩形 (即使没有faceDirection也能执行) //根据前两个点计算当前和下一个点 var p1 = points[(i-2+len)%len]; var p2 = points[(i-1+len)%len]; if(p1.equals(p2)){//意外情况:重复点两次 ( bug点,改了好多遍) if(this.faceDirection == 'vertical'){ p2.add(new Vector3(0,0,0.0001)); }else { p2.add(new Vector3(0,0.0001,0)); } } //p3 : location var foot = math.getFootPoint(location, p1, p2);//p2 修改p2到垂足的位置 var vec = foot.clone().sub(location); var p4 = p1.clone().sub(vec); points[(i-1+len)%len].copy(foot); points[(i+1)%len].copy(p4); this.setPosition((i-1+len)%len, foot);//p2 this.setPosition((i+1)%len, p4); } this.getPoint2dInfo(points); var isIntersectSelf = this.atPlane && this.closed && !this.isRect && this.point2dInfo && this.intersectSelf(this.point2dInfo.points2d);//检测相交 this.isIntersectSelf = isIntersectSelf; if(isIntersectSelf){//not-allowed if(!this.isNew && isIntersectSelf == 'lastLine') this.isIntersectSelf = 'all'; //已经画好了就不用特别对待最后一条线 if(this.isIntersectSelf == 'lastLine'){ Potree.Utils.updateVisible(this.areaPlane, 'intersectLastLine', false); Potree.Utils.updateVisible(this.areaLabel, 'intersectLastLine', false); }else { viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"polygon_isIntersectSelf" }); return } } if(!this.isIntersectSelf){ this.areaPlane && Potree.Utils.updateVisible(this.areaPlane, 'intersectLastLine', true); this.areaLabel && Potree.Utils.updateVisible(this.areaLabel, 'intersectLastLine', true); } if(!this.isIntersectSelf || this.isIntersectSelf == 'lastLine' ){ viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"polygon_isIntersectSelf" }); } } var showGuideLine = len>1 && (this.faceDirection || len > 3); if(showGuideLine && this.guideLine){ LineDraw.updateLine(this.guideLine, [intersectPos, location]); this.guideLine.visible = true; } //console.log(this.points.map(e=>e.toArray())) } if(this.restrictArea){ let holes = this.restrictArea.holes.concat(this.restrictArea.parentHoles); let holesPoints = holes.filter(e=>e!=this && e.points.length>2).map(e=>e.points); if(!math.isPointInArea(this.restrictArea.points, holesPoints, location)){ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"polygon_AtWrongPlace" }); this.isAtWrongPlace = true; return } //就不处理相交线了。 有个缺点:floor上的hole可以限制room,但hole不受room限制,会导致room的marker被框在hole里而动不了。只能去调整hole了 } viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace" }); this.isAtWrongPlace = false; this.setPosition(i, location); this.update({index: this.isRect ? null : i}); this.dispatchEvent({type:'dragChange', index:i}); } dropMarker(e){ //console.log('dropMarker') if (this.isNew && e.pressDistance>Potree.config.clickMaxDragDis){//拖拽的话返回 return this.continueDrag(null,e) } if(e.hoverViewport != e.drag.dragViewport){//copy from dragMarker, for sitemodel, only mapViewport can be dropped return this.continueDrag(null,e) } if(e.isTouch){ if(e.hoverViewport != viewer.mainViewport && this.unableDragAtMap){ viewer.dispatchEvent({type:'reticule_forbit', v:true}); //console.log('reticule_forbit',true) return this.continueDrag(null,e) }else { viewer.dispatchEvent({type:'reticule_forbit', v:false}); //console.log('reticule_forbit',false) } this.isNew && this.dragMarker(e); //isNew触屏点击时必须先更新下点,因为指尖不在屏幕上时没更新。但对已经创建的marker点击时不应该更新 } if (e.button != MOUSE.RIGHT && (//右键click的话继续执行,因为会停止 this.isIntersectSelf == 'all' && this.isNew //有线相交了 || this.isAtWrongPlace && this.isNew || !e.isAtDomElement && this.isNew//如果是刚添加时在其他dom点击, 不要响应 || e.hoverViewport != viewer.mainViewport && this.unableDragAtMap //垂直的测量线不允许在地图上放点 || this.isNew && !getDifferentPoint(this.points, this.points.length ) //不允许和之前的点相同, 但这句在点云稀疏时会导致难结束 ) ){ return this.continueDrag(null,e) } //console.log('drop marker' ) let i = this.markers.indexOf(e.drag.object); if (i !== -1) { this.dispatchEvent({ 'type': 'marker_dropped', 'index': i }); if(this.markers.length>2 && this.facePlane)this.cannotConfirmNormal = false; this.guideLine &&(this.guideLine.visible = false); } this.setMarkerSelected(e.drag.object, 'unhover', 'single'); this.editStateChange(false); e.drag.endDragFun && e.drag.endDragFun(e);// addmarker //if(this.changeCallBack)this.changeCallBack() return true }; getFacePlane(){//最普通一种get方法,根据顶点。且假设所有点已经共面,且不重合 if(/* !this.atPlane || */this.points.length<3) return /* this.facePlane = new THREE.Plane().setFromCoplanarPoints(...this.points.slice(0,3) ) */ let facePlane = this.facePlane; if(!this.atPlane || !facePlane){//多折线 没有实时更新facePlane所以重新算 let normal = new Vector3, len = this.points.length - 2; for(let i=0;ie.clone().applyQuaternion(qua)); this.point2dInfo = { originPoint0 , points2d, quaInverse : qua.clone().invert() }; } } setPosition (index, position) {//拖拽后设置位置 let point = this.points[index]; point.copy(position); /* if(this.datasetId){ this.dataset_points[index] = Potree.Utils.datasetPosTransform({toDataset:true, datasetId:this.datasetId, position:point.clone()}) } */ /* if(Potree.settings.editType == 'merge'){ this.dataset_points[index] = Potree.Utils.datasetPosTransform({toDataset:true,this.points_datasets[i], position:point.clone()}) } */ let marker = this.markers[index]; this.updateMarker(marker, point); } updateMarker(marker, pos){ marker.position.copy(pos); marker.waitUpdate(); } intersectSelf(points2d){//add var len = points2d.length; for(var i=0;i2){ if(this.isPrism){ let z = this.horizonZ || 0; let points2d = this.points.map(e=>e.clone().setZ(z)); this.areaPlane.geometry = MeshDraw.getShapeGeo(points2d); //z=0 let center = math.getCenterOfGravityPoint(points2d); //重心 this.areaPlaneCenter = new Vector3(center.x, center.y, z); this.areaPlane.position.z = z; }else if(this.point2dInfo){ if(Potree.settings.areaAtNotPlane){ //测点不在同一平面的面积. (缺点:容易出现三角缺块, 以及搭错线导致超出理想边界外。在平面上n个点能构成n-2个三角形,但空间中更多个,所以若要指定出类似平面那样n-2个三角形是有多种可能的。故暂不开放。 用d3.Delaunay结果更差) //测试桥面 SG-arqnGgAR7om&formal //参考 http://indoor.popsmart.cn:8084/sxswsw-sx/ 找到场景中面积测量线的方法:window.scene.children[18].children[0].material.color.r = 1 let geometry = new Geometry; let faceArray = [], triangles = ShapeUtils.triangulateShape(this.point2dInfo.points2d, [] ); //因为它只关注xy平面,垂直画就容易错,所以用转到平面的points2d来识别三角。 if(triangles.length == 0)return this._area = 0; for( var i = 0; i < triangles.length; i++ ){ faceArray[i] = new Face3( triangles[i][0], triangles[i][1], triangles[i][2] ); let nor = new Vector3().crossVectors( new Vector3().subVectors(this.points[triangles[i][0]], this.points[triangles[i][2]] ), new Vector3().subVectors(this.points[triangles[i][1]], this.points[triangles[i][2]]) ); this._area += nor.length() / 2; nor.normalize(); faceArray[i].normal.copy(nor); } geometry.vertices = this.points; geometry.faces = faceArray; this.areaPlane.geometry = new BufferGeometry().fromGeometry(geometry); //不知道为何不转为bufferGeometry显示不出 //this.center //参考getFacePlane 在这里建立_normal? }else { //if(this.point2dInfo){//旧的。 之前的规定点要在同一平面 this.areaPlane.geometry = MeshDraw.getShapeGeo(this.point2dInfo.points2d); let center = math.getCenterOfGravityPoint(this.point2dInfo.points2d); //重心 let firstPos = this.point2dInfo.points2d[0].clone(); firstPos.z = 0; //因为shape只读取了xy,所以位移下, 再算出最终位置,得到差距 firstPos.applyQuaternion(this.point2dInfo.quaInverse); let vec = this.point2dInfo.originPoint0.clone().sub(firstPos); center = new Vector3(center.x, center.y, 0); center.applyQuaternion(this.point2dInfo.quaInverse); this.areaPlane.quaternion.copy(this.point2dInfo.quaInverse); this.areaPlane.position.copy(vec); center.add(vec); this.center = center; //} } } }else { this.areaPlane.geometry = new Geometry(); } } getIndex(index, add){ let lastIndex = this.points.length - 1; if(add == -1) return (index === 0) ? lastIndex : index - 1; else if(add == 1) return (index + 1 > lastIndex) ? 0 : index + 1; } update(options={}){ if(this.points.length === 0){ return; } //performance.mark('measureUpdate-start') let lastIndex = this.points.length - 1; if(options.index != void 0){//更新第几个点 this.updateMarker(this.markers[options.index], this.points[options.index]); let previousIndex = this.getIndex(options.index, -1); let nextIndex = this.getIndex(options.index, +1); if( this.closed || nextIndex != 0 ) LineDraw.updateLine( this.edges[options.index], [this.points[options.index], this.points[nextIndex]]); if( this.closed || previousIndex != lastIndex ) LineDraw.updateLine( this.edges[previousIndex], [this.points[options.index], this.points[previousIndex]]); }else { for (let index = 0; index <= lastIndex; index++) { let nextIndex = this.getIndex(index, +1); let previousIndex = this.getIndex(index, -1); let point = this.points[index]; let nextPoint = this.points[nextIndex]; let previousPoint = this.points[previousIndex]; if(options.ifUpdateMarkers){ this.updateMarker(this.markers[index], point); } if(!this.closed && nextIndex == 0 )break; //add { let edge = this.edges[index]; if(edge){ LineDraw.updateLine(edge, [point, nextPoint]); } } } } if(this.areaPlane){ this.updateAreaPlane(); } //this.dispatchEvent({type:'update'}) viewer.dispatchEvent('content_changed'); viewer.mapViewer && viewer.mapViewer.dispatchEvent('content_changed'); //暂时先这么都通知 //performance.mark('measureUpdate-end') //let measure = performance.measure('measure-','measureUpdate-start', 'measureUpdate-end' ); //console.log('update-time', measure.duration) } createPrismLines(color){ this.lineMesh = LineDraw.createLine([],{color}); this.lineMesh.name = 'PrismLines'; this.add(this.lineMesh); } updatePrismLines(){ if(!this.lineMesh)return let positions = []; let length = this.points.length; this.points.forEach((point, index)=>{ //竖线: positions.push(point.clone().setZ(this.zMin), point.clone().setZ(this.zMax)); //横线 let nextPoint = this.points[(index+1)%length]; if(!nextPoint)return;//when length==1 positions.push(point.clone().setZ(this.zMax), nextPoint.clone().setZ(this.zMax));//上横线 positions.push(point.clone().setZ(this.zMin), nextPoint.clone().setZ(this.zMin));//下横线 }); LineDraw.moveLine(this.lineMesh,positions); viewer.dispatchEvent('content_changed'); } dispose(){//add this.parent.remove(this); this.markers.forEach(e=>e.dispose()); this.edges.forEach(e=>e.geometry.dispose()); } reDraw(restMarkerCount=0){//重新开始画 let pointCount = this.points.length - restMarkerCount; // restMarkerCount为需要留下的marker数量 while(pointCount > 0){ this.removeMarker(--pointCount); } this.point2dInfo = null; this.facePlane = null; } setMarkerSelected(){} editStateChange(state){ if(!state){ viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"polygon_isIntersectSelf" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace" }); viewer.dispatchEvent({type:'reticule_forbit', v:false}); this.markers.forEach(e=>e.isDragging = false ); } } transformData(prop){ const pick = (defaul, alternative) => { if(defaul != null){ return defaul; }else { return alternative; } }; prop.showDistances = (prop.showDistances === null) ? true : prop.showDistances; prop.showArea = pick(prop.showArea, false); prop.showAngles = pick(prop.showAngles, false); prop.showCoordinates = pick(prop.showCoordinates, false); prop.showHeight = pick(prop.showHeight, false); prop.showCircle = pick(prop.showCircle, false); prop.showAzimuth = pick(prop.showAzimuth, false); prop.showEdges = pick(prop.showEdges, true); prop.closed = pick(prop.closed, false); prop.maxMarkers = pick(prop.maxMarkers, Infinity); prop.direction = prop.direction;//add prop.type = prop.type; prop.showGuideLine = pick(prop.showGuideLine, false); prop.isRect = pick(prop.isRect, false); } setSelected(){} continueDrag(marker, e){ let object = marker || e.drag.object; object.isDragging = true; this.editStateChange(true); var timer = setTimeout(()=>{//等 drag=null之后 //右键拖拽结束后需要重新得到drag if(this.parent && object.parent && object.isDragging){ //console.log('continueDrag') viewer.inputHandler.startDragging( object , {endDragFun: e.drag.endDragFun, notPressMouse:e.drag.notPressMouse, dragViewport:e.drag.dragViewport} ); } },1); return timer } } function getDifferentPoint(points, count){//for facePlane var result = []; for(let i=0;ie.equals(p)))continue; else result.push(p); if(result.length == count)break } if(result.length == count)return result } let texLoader$6 = new TextureLoader(); let defaultColor$1 = new Color(config$1.measure.default.color); let highlightColor = new Color(config$1.measure.highlight.color); let color = new Color(config$1.measure.color); let textColor = new Color(config$1.measure.textColor); var markerMats; var lineMats; var planeMats; const textSizeRatio = math.linearClamp(window.outerWidth * window.outerHeight , [360*720, 1920*1080], [0.7, 1]); //pc字显示大一些 用 const lineDepthInfo = { clipDistance : 15,//4,//消失距离 occlusionDistance: 3,//1,//变为backColor距离 }; const markerMapShrink = browser.isMobile() ? 0.4 : 0.8; //触屏需要更大的热区 const markerSizeInfo = { width2d : 18 / markerMapShrink , // nearBound : 1.5, farBound : 15, }; /* const markerSizeInfo = { minSize : 10 , maxSize : 15 , nearBound : 1.5, farBound : 15, } */ const labelSizeInfo = {width2d:200}; const mainLabelProp = { //backgroundColor: {r: defaultColor.r*255, g: defaultColor.g*255, b: defaultColor.b*255, a:config.measure.default.opacity}, backgroundColor: {r: 0, g: 0, b: 0, a:0}, textColor: {r: textColor.r*255, g: textColor.g*255, b: textColor.b*255, a: 1.0}, textBorderColor: {r:255, g: 255, b:255, a: 1.0}, textBorderThick:3 , fontsize: 15 * textSizeRatio, borderRadius : 12, margin:{x:20,y:4}, renderOrder : Potree.config.renderOrders.measureLabel, pickOrder: Potree.config.renderOrders.measureLabel, disToLine:-0.15, useDepth : true , // 2023.10 尽量不让数字被挡住 clipDistance : 10,//消失距离 occlusionDistance: 10,//变为backColor距离 maxOcclusionFactor:0.3, maxClipFactor:0.8 }; const subLabelProp = { backgroundColor: {r: 255, g: 255, b: 255, a:0}, textColor: {r: textColor.r*255, g: textColor.g*255, b: textColor.b*255, a: 1.0}, textBorderColor: {r:255, g: 255, b:255, a: 1.0}, textBorderThick:3 , fontsize: 14 * textSizeRatio, renderOrder : Potree.config.renderOrders.measureLabelSub, pickOrder: Potree.config.renderOrders.measureLabelSub, disToLine:-0.13, }; const angle = MathUtils.degToRad(5);//显示水平垂直辅助线的最小角度 const guideShowMinAngle = {min: angle, max: Math.PI/2 - angle}; class Measure$1 extends ctrlPolygon{ constructor (prop) { prop.dimension = '2d'; super('measure',prop); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = this.measureType + this.constructor.counter; //'Measure_' + this.constructor.counter; this.markerLabels = []; this.edgeLabels = []; this.angleLabels = []; this.coordinateLabels = []; this.area = {value:0,string:''}; if( this.showArea ){ this.areaLabel = this.createAreaLabel(); this.add(this.areaLabel); } //add: if(this.atPlane || this.faceDirection){ //是一个平面上的话 this.createGuideLine(); } if(this.measureType == 'Distance' /* || this.measureType.includes('MulDistance') */){ this.createHorVerGuideLine(); } this.selectStates = {}; this.setUnitSystem(prop.unit || viewer.unitConvert.UnitService.defaultSystem); Potree.Utils.setObjectLayers(this, 'measure' ); if(this.measureType == 'MulDistance' || this.measureType == 'Hor MulDistance' || this.measureType == 'Ver MulDistance'){ //this.showTotalDis = true this.totalDisLabel = this.createTotalDisLabel(); this.add(this.totalDisLabel); } //addMarkers: this.initData(prop); this.pointsPos2d = new Map; //屏幕上的二维坐标 this.points_datasets || (this.points_datasets = []); //存每个点是哪个数据集 this.addEventListener('marker_dropped',(e)=>{ this.updateDatasetBelong(e.index); }); this.addEventListener('isVisible', ()=>{ viewer.mapViewer && viewer.mapViewer.dispatchEvent({type:'content_changed'}); }); this.lastDropTime = 0; } initData(prop){ let makeIt = super.initData(prop); if(makeIt){ this.edges.forEach(edge=>{edge.dispatchEvent('addHoverEvent'); }); }else { this.failBuilded = true; } } updateDatasetBelong(changeIndex){//更新所属数据集 if(Potree.settings.editType == "merge" || this.measureType == 'MulDistance Ring'){//点直接跟着数据集走,不用找整体的datasetId this.dataset_points[changeIndex] = Potree.Utils.datasetPosTransform({toDataset:true, datasetId:this.points_datasets[changeIndex], position:this.points[changeIndex].clone()}); return } let old = this.datasetId; let maxCount = {id:null,count:0}; let datasets = {}; this.points_datasets.forEach(e=>{ if(e == void 0)return if(datasets[e]){ datasets[e] ++; }else { datasets[e] = 1; } }); for(let i in datasets) { if(datasets[i]>maxCount.count){ maxCount = {id:i, count:datasets[i]}; } } this.datasetId = maxCount.count > 0 ? maxCount.id : null; //if(this.datasetId != old){ //this.dispatchEvent({type:'changeDatasetId'}) if(this.datasetId == void 0){ this.dataset_points = null; //可能为空或[null,null...] }else { this.dataset_points = this.points.map(e=>{ return Potree.Utils.datasetPosTransform({toDataset:true,datasetId:this.datasetId, position:e.clone()}) }); } //} } transformByPointcloud(){//每次移动点云 or 加载测量线时要获取一下当前position //有地图时 if(this.datasetId == void 0)return this.points = this.dataset_points.map(e=>{ return Potree.Utils.datasetPosTransform({fromDataset:true, datasetId:this.datasetId, position:e.clone()}) }); this.getPoint2dInfo(this.points); this.update({ifUpdateMarkers:true}); this.setSelected(false);//隐藏edgelabel } update(options={}) { if(options.index == -1)return super.update(options); if(this.showCoordinates && this.points.length>0){ let position = this.points[0]; this.markers[0].position.copy(position); { // coordinate labels let coordinateLabel = this.coordinateLabels[0]; let pos = [ position.toArray() ]; if(viewer.transform){ let lonlat = viewer.transform.lonlatToLocal.inverse(position.toArray()); let EPSG4550 = viewer.transform.lonlatTo4550.forward(lonlat); pos.push(lonlat,EPSG4550); } //let msg = position.toArray().map(p => Utils.addCommas(p.toFixed(2))).join(" / "); let msg = pos.map(a=> a.map(p => Utils.addCommas(p.toFixed(10))).join(", ") ).join("
"); coordinateLabel.setText(msg); coordinateLabel.setPos(position); coordinateLabel.setVisible(true);//this.showCoordinates; } return } let setEdgeLabel = (label,p1,p2,distance)=>{//设置label位置和字 this.setEdgeLabelPos(label,p1,p2); distance = distance == void 0 ? p1.distanceTo(p2) : distance; //var text = viewer.unitConvert.convert(distance, 'distance', Potree.settings.precision, this.unitSystem, 1 , true)//distance要传0.1 这个factor var text = this.getConvertString(distance, 'distance'); label.setText(text); return distance }; /* let setEdgeLabel = (label,p1,p2,distance)=>{//设置label位置和字 this.setEdgeLabelPos(label,p1,p2) distance = distance == void 0 ? p1.distanceTo(p2) : distance; var text = this.labelText || viewer.unitConvert.convert(distance, 'distance', Potree.settings.precision , this.unitSystem, 0.001 , true, true)//distance要传0.1 这个factor label.setText(text) } */ let lastIndex = this.points.length - 1; let setLabel = (index)=>{ let previousIndex = this.getIndex(index, -1); let nextIndex = this.getIndex(index, +1); let previousPoint = this.points[previousIndex]; let point = this.points[index]; let nextPoint = this.points[nextIndex]; if(this.showDistances){ // edge labels let edgeLabel = this.edgeLabels[index]; let distance = point.distanceTo(nextPoint); this.edges[index].distance_ = distance; edgeLabel.shouldVisi = (index < lastIndex || this.isRect || this.closed && !this.isNew ) && distance>0; //this.closed || edgeLabel.setVisible(edgeLabel.shouldVisi) //closed的在setEdgesDisplay中设置 Utils.updateVisible(edgeLabel, 'shouldVisi', edgeLabel.shouldVisi, 2); if(edgeLabel.shouldVisi){ edgeLabel.lineDir = new Vector3().subVectors(point,nextPoint).normalize(); //[point,nextPoint] setEdgeLabel(edgeLabel,point,nextPoint,distance); } } }; if(options.index != void 0){//更新第几个点 setLabel(options.index); let previousIndex = this.getIndex(options.index, -1); setLabel(previousIndex); }else { for (let index = 0; index <= lastIndex; index++) { setLabel(index); } } if(Potree.config.measure.mulLabelHideFaraway ){ this.measureType == 'MulDistance' && this.clearEdgeLabelVisi(); } if(this.measureType == 'Distance' && this.points.length>1){//设置水平垂直辅助线 var pTop, pBtm; if(this.points[0].z > this.points[1].z ){ pTop = this.points[0]; pBtm = this.points[1]; }else { pTop = this.points[1]; pBtm = this.points[0]; } let projectPos = new Vector3(pTop.x, pTop.y, pBtm.z);//两条guideline的交点 {//倾斜角度太小的时候不显示 let tan = pTop.distanceTo(projectPos) / pBtm.distanceTo(projectPos); let angle = Math.atan(tan); this.shouldShowHorVerGuide = angle > guideShowMinAngle.min && angle < guideShowMinAngle.max; } LineDraw.updateLine(this.verGuideEdge, [pTop, projectPos]); LineDraw.updateLine(this.horGuideEdge, [pBtm, projectPos]); setEdgeLabel(this.verEdgeLabel,pTop,projectPos); setEdgeLabel(this.horEdgeLabel,pBtm,projectPos); this.verGuideEdge.visible = this.horGuideEdge.visible = this.shouldShowHorVerGuide; this.verEdgeLabel.visible = this.horEdgeLabel.visible = this.shouldShowHorVerGuide; } if(this.showArea && this.points.length > 2){ // update area let msg = this.getArea().string; this.areaLabel.setPos(this.getCenter('areaPlaneCenter')); this.areaLabel.setText(msg); Utils.updateVisible(this.areaLabel, 'setVisible', true); //this.areaLabel.setVisible(true) } if(this.totalDisLabel){ this.ifShowTotalDis(); Utils.updateVisible(this.totalDisLabel,'setVisible', this.showTotalDis); this.edgeLabels.forEach(e=> Utils.updateVisible(e, 'showTotalDis', !this.showTotalDis)); if(this.showTotalDis){ let dis = this.getTotalDistance(); let msg = this.getConvertString(dis, 'distance'); this.center = null; this.center = this.getCenter(); this.totalDisLabel.setPos(this.center); this.totalDisLabel.setText(msg); } } }; getArea(){ let area; if(this._area != void 0){ area = this._area; }else if(this.point2dInfo){ area = Math.abs(math.getArea(this.point2dInfo.points2d));//this.getArea(); }else {//mulDistance Ring 2d面 area = Math.abs(math.getArea(this.points)); } let msg = this.getConvertString(area, 'area'); //let msg = viewer.unitConvert.convert(area, 'area', Potree.settings.precision, this.unitSystem/* , 0.1 */ ) this.area = {value:area, string:msg}; return this.area } getConvertString(num, type){ return viewer.unitConvert.convert(num, type, Potree.settings.precision, this.unitSystem, true , { 'imperial': {minFactor: 0.01 }, 'metric': {minFactor: 0.01} } ) } ifShowTotalDis(){ let show = this.points.length > 2; if(show){ let maxDis = 0.15; let lastIndex = this.points.length - 1; for(let i=0;i maxDis){ show = false; break; } } } this.showTotalDis = show; /* 连续测量: 1. ≥2次测量,单个距离<15cm时,居中显示总长, hover、选中时显示每段长度 2. 若连续测量的线段中,大于等于1段超出15cm,所有线段均显示长度 ------------------- */ } clearEdgeLabelVisi(){//修改点位置后清空,下次render时会自动getEdgeLabelVisi let lastIndex = this.points.length - 1; for (let index = 0; index <= lastIndex; index++) { if(!this.closed && index == lastIndex)continue let edgeLabel = this.edgeLabels[index]; edgeLabel.visiMap.clear(); } } getEdgeLabelVisi(viewport){//获取多折线的edgelabel在不同视图里的可见性。要保证任何时候label能出现的线最小二维长度一致 let camera = viewport.camera; let lastIndex = this.points.length - 1; /* let pos2ds = this.points.map(point=> point.clone().project(camera) ) //即使只是旋转也会变动,尤其是转到屏幕外后变为显示。所以不用这种 let minDis = 0.01; */ let minDis = 0.02 , minAngleRatio = 0.07, minAngle; let vecs; let forceShow; if(camera.type == 'OrthographicCamera'){ minDis *= Math.pow(camera.top / camera.zoom, 2); //console.log(minDis) }else { if(Potree.settings.displayMode == 'showPanos' && viewer.images360.zoomLevel == Potree.settings.zoom.max){ forceShow = true; //当zoom到最大时强制显示,避免有的线太短永远显示不出长度 }else { vecs = this.points.map(point=> new Vector3().subVectors(point, camera.position).normalize()); minAngleRatio /= viewport.resolution.y / 1000 / textSizeRatio; //角度占fov最小比率 minAngle = minAngleRatio * MathUtils.degToRad(camera.fov); } } for (let index = 0; index <= lastIndex; index++) { if(!this.closed && index == lastIndex)continue let edgeLabel = this.edgeLabels[index]; let nextIndex = (index + 1 > lastIndex) ? 0 : index + 1; let previousIndex = (index === 0) ? lastIndex : index - 1; let point = this.points[index]; let nextPoint = this.points[nextIndex]; /* let point2d = pos2ds[index]; let nextPoint2d = pos2ds[nextIndex]; let dis2d = point2d.distanceToSquared(nextPoint2d) let v = dis2d > minDis //可见长度太小,为避免拥挤,不显示 edgeLabel.visiMap.set(camera, v) */ let v; if(forceShow){ v = true; }else if(camera.type == 'OrthographicCamera'){ let vec = new Vector3().subVectors(point,nextPoint); let projVec = vec.projectOnPlane(viewport.view.direction); v = projVec.lengthSq() > minDis; }else { let vec0 = vecs[index]; let vec1 = vecs[nextIndex]; v = Math.acos(vec0.dot(vec1)) > minAngle; //角度过小代表可见长度太小,为避免拥挤,不显示 } edgeLabel.visiMap.set(camera, v); } } setEdgeLabelPos(label,p1,p2){ //调整label的位置,使倾斜后看起来在线的中心,而不要挡住端点 let center = new Vector3().addVectors(p1,p2).multiplyScalar(0.5); return label.setPos(center) if(label.lineDir && label.lineDir.length() > 0){ if(viewer.mainViewport.camera.type == 'OrthographicCamera'){ label.setPos(center); }else { //根据视线和线的夹角(后又加入相机和两个端点距离差)来决定标签偏移位置。+ let eyePos = viewer.mainViewport.camera.position; let dir = viewer.mainViewport.view.direction; //new THREE.Vector3().subVectors(center,eyePos).normalize() /*let centerDir = new THREE.Vector3().subVectors(center,eyePos).normalize() if(centerDir.dot(dir)<0){//中点在相机后方,就不设置 label.setPos(center) return } */ let cos = dir.dot(label.lineDir); let nearPoint = cos > 0 ? p2 : p1; //近端点。 let far = cos > 0 ? p1 : p2; //远端点。 let nearPointDir = new Vector3().subVectors(nearPoint,eyePos);//.normalize() //使label在中点和近端点中变化, 近端点可能到了相机后方,需要投影到相机所在平面上 if(nearPointDir.dot(dir)<0){//近端点到了相机后方,前移。 //let hfov = cameraLight.getHFOVForCamera(viewer.mainViewport.camera , true ); //暂且只看水平fov //if(nearPointDir.dot(dir)0?label.lineDir:label.lineDir.clone().negate()); let camDirPlane = new Plane().setFromNormalAndCoplanarPoint(dir, eyePos); nearPoint = ray.ray.intersectPlane(camDirPlane, new Vector3()); if(!nearPoint){//线是垂直的,视线是水平的时候 return label.setPos(center) } } //防止离远了之后也偏移很多,但远了之后相机到端点vec和到中点的vec的夹角接近,不需要怎么偏移的。 let dis1 = nearPoint.distanceToSquared(eyePos); let dis2 = far.distanceToSquared(eyePos); let diff = Math.abs(dis1/dis2); diff<1 && (diff = 1/diff); diff = math.linearClamp(diff,[0, 30], [ 0,1 ]); let efficiency = 0.7; // 0-1 数值越高,r越容易接近1或-1,label越容易在倾斜后靠近近端点。 //let r = 0.5*efficiency*cos + 0.5 let r = 0.5*efficiency*diff*cos + 0.5; r = MathUtils.clamp(r,0.1,0.9); //视线越接近线的方向,标签应该越往近端点偏移,防止看起来几乎在远端。 if(cos > 0){ center = p1.clone().multiplyScalar(1-r).add(nearPoint.clone().multiplyScalar(r)); //label在线上滑动,使尽量保持在视觉中心 }else { center = nearPoint.clone().multiplyScalar(1-r).add(p2.clone().multiplyScalar(r)); //label在线上滑动,使尽量保持在视觉中心 } label.setPos(center); } //归零 //this.orient2dInfo = null //this.markers.forEach(e=>e.needsUpdate=true) }else { label.setPos(center); } } cloneMarker(cloneIndex, index){ return this.addMarker({ index, point: this.points[cloneIndex], dataset_point:this.dataset_points && this.dataset_points[cloneIndex], points_dataset:this.points_datasets[cloneIndex] }) } addMarker (o={}) { var index = o.index == void 0 ? this.points.length : o.index; //要当第几个 let marker = new Sprite$2({mat:this.getMarkerMaterial('default'), sizeInfo: markerSizeInfo, name:"measure_point"} ); Potree.Utils.setObjectLayers(marker, 'measure' ); marker.pickOrder = marker.renderOrder = Potree.config.renderOrders.measureMarker; marker.markerSelectStates = {}; marker.addEventListener('startDragging',(e)=>{ /* if(e.drag.dragViewport.name == 'MainView') */viewer.inputHandler.dispatchEvent( {type: 'measuring',v:true, cause:'startDragging', situation:'dragging', object:this}); //add for 调试,方便后期增加点 if(!this.isNew && viewer.inputHandler.pressedKeys['M'.charCodeAt(0)] && this.points.length{ if( e.button != MOUSE.LEFT )return viewer.inputHandler.dispatchEvent({type: 'measuring', v:false, cause:'stopDragging', situation:'dragging', object:this} ); this.lastDropTime = Date.now(); if(Potree.settings.adsorption){ this.isNew || viewer.viewports.forEach((viewport)=>{ this.getPointsPos2d(viewport, true );//forceUpdate }); } this.isNew || viewer.measuringTool.history.afterChange(this); }); marker.addEventListener('click',()=>{ if(viewer.measuringTool.editMode == 'delPoint' ){ /* if(this.points.length == this.minMarkers){//--前端去重绘 viewer.scene.removeMeasurement(this) }else{ */ viewer.measuringTool.history.beforeChange(this); let index = this.markers.indexOf(marker); this.removeMarker(index); viewer.measuringTool.history.afterChange(this); this.dispatchEvent('changed'); //} } }); //marker.measure = this let edge; { // edges edge = LineDraw.createFatLine( [ ],{mat:this.getLineMat('edgeDefault')} ); edge.pickOrder = 0; Potree.Utils.setObjectLayers(edge, 'measure' ); let addHoverEvent = ()=>{ //当非isNew时才添加事件 let mouseover = (e) => { /* if(this.measureType == 'MulDistance'){ } */ this.setSelected(true, 'edge'); }; let mouseleave = (e) => { this.setSelected(false, 'edge'); }; edge.addEventListener('mouseover', mouseover); edge.addEventListener('mouseleave', mouseleave); edge.removeEventListener('addHoverEvent', addHoverEvent); edge.addEventListener('click',(e)=>{ let now = Date.now(); if(now - this.lastDropTime<100)return ;//防止拖拽marker时误触导致focus, 以及点到marker不focus if(viewer.measuringTool.editMode == 'addPoint' && this.points.length < this.maxMarkers){ viewer.measuringTool.history.beforeChange(this); let index = this.edges.indexOf(edge) + 1; let nextIndex = index % this.edges.length; let point = math.getFootPoint(e.hoveredElement.point, this.points[index-1], this.points[nextIndex] ); this.addMarker({ index, point, dataset_point: this.dataset_points && new Vector3 , //初始化 points_dataset : this.points_datasets[index-1] //使用前一个的 }); this.updateDatasetBelong(index); //获取dataset_point viewer.measuringTool.history.afterChange(this); this.dispatchEvent('changed'); //this.update({}) }else { this.isNew || viewer.measuringTool.isAdding || viewer.focusOnObject(this, 'measure'); //正在添加测量线时不要focus其他线(容易误触) } }); }; edge.addEventListener('addHoverEvent', addHoverEvent); if(!this.isNew){ edge.dispatchEvent('addHoverEvent'); } } super.addMarker(Object.assign(o, {index, marker, edge})); if(this.showEdges){ // edge labels const edgeLabel = this.createEdgeLabel('edgeLabel', !this.closed); this.edgeLabels = [...this.edgeLabels.slice(0,index), edgeLabel, ...this.edgeLabels.slice(index,this.edgeLabels.length)]; } if(this.showCoordinates){ // coordinate labels let coordinateLabel = new Label({ className:'measure_pointPos', camera: viewer.scene.getActiveCamera() }); coordinateLabel.setVisible(false); this.coordinateLabels.push(coordinateLabel); } let event = { type: 'marker_added', measurement: this, marker: marker }; this.dispatchEvent(event); //this.setMarker(this.points.length - 1, point); this.update({index});//更新一下倒数第二条线 return marker;//add }; editStateChange(state){ //主要针对edgeLabels显示切换,编辑时显示 super.editStateChange(state); if(!state){ this.editStateTimer = setTimeout(()=>{ if(!this.isEditing){ this.dispatchEvent({type:'editStateChange',state:false}); this.setEdgesDisplay(false); this.areaPlane && Potree.Utils.updateVisible(this.areaPlane, 'intersectLastLine', true); this.areaLabel && Potree.Utils.updateVisible(this.areaLabel, 'intersectLastLine', true); } },100); }else { if(!this.isEditing){ this.dispatchEvent({type:'editStateChange',state:true}); this.setEdgesDisplay(true); clearTimeout(this.editStateTimer); } } this.isEditing = state; } setMarkerSelected(marker, state, hoverObject){ //console.warn(marker.id , state, hoverObject) marker.markerSelectStates[hoverObject] = state; let absoluteState = false; for(var i in marker.markerSelectStates){ if(marker.markerSelectStates[i] == 'hover'){ absoluteState = true; break; } } if(absoluteState){ marker.material = this.getMarkerMaterial('select'); marker.renderOrder = marker.pickOrder = Potree.config.renderOrders.measureMarker+1; }else { marker.material = this.getMarkerMaterial('default'); marker.renderOrder = marker.pickOrder = Potree.config.renderOrders.measureMarker; } marker.selected = absoluteState; viewer.mapViewer && viewer.mapViewer.dispatchEvent('content_changed'); viewer.dispatchEvent('content_changed'); } setEdgesDisplay(state, ignoreGuideLine){ this.closed && this.edgeLabels.forEach(e=>Utils.updateVisible(e,'hover',state)); if(this.totalDisLabel && !viewer.screenshoting){ this.edgeLabels.forEach(e=> Utils.updateVisible(e, 'hover', state, 1, state ? 'add' : 'cancel')); Utils.updateVisible(this.totalDisLabel,'hover', !state ); } if(!ignoreGuideLine && this.measureType == 'Distance'){ this.horEdgeLabel.visible = this.verEdgeLabel.visible = this.horGuideEdge.visible = this.verGuideEdge.visible = !!(state && this.shouldShowHorVerGuide); } } setSelected(state, hoverObject){//add //console.log('setSelected',state, hoverObject) let absoluteState = !!state; if(hoverObject){//如果没有hoverObject且state为false 就强制取消选中态 this.selectStates[hoverObject] = state; for(var i in this.selectStates){ if(this.selectStates[i]){ absoluteState = true; break; } } } if(absoluteState){ this.markers.forEach(e=>this.setMarkerSelected(e, 'hover', 'selectAll' ) ); this.edges.forEach(e=>{ e.renderOrder = Potree.config.renderOrders.lines + 1; e.material = this.getLineMat('edgeSelect'); }); this.areaPlane && (this.areaPlane.material = planeMats.selected); //this.areaLabel && this.areaLabel.elem.addClass('highLight') //this.closed || this.edgeLabels.forEach(e=>e.elem.addClass('highLight') ) this.setEdgesDisplay(true, hoverObject=="screenshot"); this.areaLabel && setLabelHightState(this.areaLabel, true); this.closed || this.edgeLabels.forEach(e=>setLabelHightState(e, true) ); }else { this.markers.forEach(e=>this.setMarkerSelected(e, 'unhover', 'selectAll' )); this.edges.forEach(e=>e.material = this.getLineMat('edgeDefault') ); this.areaPlane && (this.areaPlane.material = planeMats.default); this.setEdgesDisplay(false, hoverObject=="screenshot"); //this.areaLabel && this.areaLabel.elem.removeClass('highLight') //this.closed || this.edgeLabels.forEach(e=>e.elem.removeClass('highLight') ) this.areaLabel && setLabelHightState(this.areaLabel, false); this.closed || this.edgeLabels.forEach(e=>setLabelHightState(e, false) ); } this.selected = absoluteState; if(hoverObject != 'byList'){ //this.bus && this.bus.emit('highlight', this.selected) this.dispatchEvent({type:'highlight',state:this.selected});//列表高亮 } viewer.dispatchEvent('content_changed'); viewer.mapViewer && viewer.mapViewer.dispatchEvent('content_changed'); } removeMarker(index ){ super.removeMarker(index); this.points_datasets.splice(index, 1); this.dataset_points && this.dataset_points.splice(index, 1); this.coordinateLabels.splice(index, 1); let edgeIndex = index;//(index === 0) ? 0 : (index - 1); if(this.edgeLabels[edgeIndex]){ this.edgeLabels[edgeIndex].dispose(); this.edgeLabels.splice(edgeIndex, 1); } this.update({index: this.getIndex(index, -1)}); this.dispatchEvent({type: 'marker_removed', measurement: this}); } setPosition(index, position) { super.setPosition(index, position); let event = { type: 'marker_moved', measure: this, index: index, position: position.clone() }; this.dispatchEvent(event); } dispose(){//add var labels = this.edgeLabels.concat(this.coordinateLabels); this.areaLabel && labels.push(this.areaLabel); labels.forEach(e=>e.dispose()); super.dispose(); this.dispatchEvent('disposed'); } getTotalDistance () { if (this.points.length === 0) { return 0; } let distance = 0; for (let i = 1; i < this.points.length; i++) { let prev = this.points[i - 1]; let curr = this.points[i]; let d = prev.distanceTo(curr); distance += d; } if (this.closed && this.points.length > 1) { let first = this.points[0]; let last = this.points[this.points.length - 1]; let d = last.distanceTo(first); distance += d; } return distance; } getAngleBetweenLines (cornerPoint, point1, point2) { let v1 = new Vector3().subVectors(point1, cornerPoint); let v2 = new Vector3().subVectors(point2, cornerPoint); // avoid the error printed by threejs if denominator is 0 const denominator = Math.sqrt( v1.lengthSq() * v2.lengthSq() ); if(denominator === 0){ return 0; }else { return v1.angleTo(v2); } }; getAngle (index) { if (this.points.length < 3 || index >= this.points.length) { return 0; } let previous = (index === 0) ? this.points[this.points.length - 1] : this.points[index - 1]; let point = this.points[index]; let next = this.points[(index + 1) % (this.points.length)]; return this.getAngleBetweenLines(point, previous, next); } getCenter(type){ if(this.center){ return this.center.clone() }else { let center = this.points.reduce(function(total, currentValue ){ return total.add(currentValue) }, new Vector3 ); this.points.length && center.multiplyScalar(1/this.points.length); return center //求不出重心呜呜 } } // updateAzimuth(){ // // if(this.points.length !== 2){ // // return; // // } // // const azimuth = this.azimuth; // // const [p0, p1] = this.points; // // const r = p0.distanceTo(p1); // } createGuideLine(){//add 辅助线 var guideLine = LineDraw.createFatLine([ ],{mat:this.getLineMat('guide')} ); guideLine.visible = false; this.guideLine = guideLine; this.add(guideLine); } createHorVerGuideLine(){//创建水平与垂直辅助线,仅距离测量有。 var verGuideEdge = LineDraw.createFatLine([ ],{mat:this.getLineMat('guide')} ); verGuideEdge.visible = false; this.verGuideEdge = verGuideEdge; verGuideEdge.name = 'verGuideEdge'; var horGuideEdge = LineDraw.createFatLine([ ],{mat:this.getLineMat('guide')} ); horGuideEdge.visible = false; horGuideEdge.name = 'horGuideEdge'; this.horGuideEdge = horGuideEdge; this.add(this.verGuideEdge); this.add(this.horGuideEdge); //label: this.verEdgeLabel = this.createEdgeLabel('verGuideEdge'); this.horEdgeLabel = this.createEdgeLabel('horGuideEdge'); } createEdgeLabel(name, hasHoverEvent){ let inf = { sizeInfo: labelSizeInfo, name:name||'edgeLabel', }; if(name && name.includes('Guide')){ inf.fontsize = 12; } const edgeLabel = new TextSprite$2( $.extend({}, hasHoverEvent ? mainLabelProp : subLabelProp, inf) ); if(hasHoverEvent){ edgeLabel.addEventListener('mouseover',()=>{ this.setSelected(true, 'edgeLabel'); }); edgeLabel.addEventListener('mouseleave',()=>{ this.setSelected(false, 'edgeLabel'); }); edgeLabel.addEventListener('click',()=>{ this.isNew || viewer.measuringTool.isAdding || viewer.focusOnObject(this, 'measure'); }); } edgeLabel.visible = false; edgeLabel.measure = this; edgeLabel.sprite.material.depthTestWhenPick = true; Potree.Utils.setObjectLayers(edgeLabel, 'measure' ); this.add(edgeLabel); if(this.measureType == 'MulDistance'){ edgeLabel.visiMap = new Map(); } return edgeLabel } createAreaLabel(){ const areaLabel = this.createCenterLabel('areaLabel'); return areaLabel; } createTotalDisLabel(){ const totalDisLabel = this.createCenterLabel('totalDisLabel'); return totalDisLabel; } createCenterLabel(name){ const centerLabel = new TextSprite$2( $.extend({},mainLabelProp,{sizeInfo: labelSizeInfo, name, disToLine:0, fontsize:16*textSizeRatio} ) ); centerLabel.addEventListener('mouseover',()=>{ this.isNew || this.setSelected(true, 'centerLabel'); }); centerLabel.addEventListener('mouseleave',()=>{ this.isNew || this.setSelected(false, 'centerLabel'); }); centerLabel.addEventListener('click',()=>{ this.isNew || viewer.measuringTool.isAdding || viewer.focusOnObject(this, 'measure'); }); Potree.Utils.setObjectLayers(centerLabel, 'measure' ); Utils.updateVisible(centerLabel, 'setVisible', false); return centerLabel; } getMarkerMaterial(type) { if(!markerMats){ markerMats = { default: new DepthBasicMaterial($.extend({},lineDepthInfo,{ transparent: !0, opacity: 1, map: texLoader$6.load(Potree.resourcePath+'/textures/pic_point_s32.png' ), useDepth:true , mapScale: markerMapShrink })), select: new MeshBasicMaterial({ transparent: !0, opacity: 1, depthTest:false, map: texLoader$6.load(Potree.resourcePath+'/textures/pic_point32.png'/* , null, null, { antialias: false } */), }), }; Measure$1.markerMats = markerMats; markerMats.select.map.repeat.set(1/markerMapShrink,1/markerMapShrink); markerMats.select.map.offset.set((markerMapShrink-1)/2/markerMapShrink, (markerMapShrink-1)/2/markerMapShrink); //markerMats.select.map.offset.set( -1.1 , -1.1 ) } return markerMats[type] } getLineMat(type) { if(!Measure$1.lineMats){ Measure$1.lineMats = { edgeDefault: LineDraw.createFatLineMat($.extend({},lineDepthInfo,{ color: config$1.measure.default.color, lineWidth: config$1.measure.lineWidth, useDepth :true, dashWithDepth :true, // 只在被遮住的部分显示虚线,因为实线容易挡住label dashed :true, dashSize : 0.04, gapSize: 0.04, transparent: true, opacity: config$1.measure.default.opacity, depthTestWhenPick:true, })), edgeSelect: LineDraw.createFatLineMat($.extend({},lineDepthInfo,{ color: config$1.measure.highlight.color,//'#f0ff00', dashSize: 0.5, gapSize: 0.2, lineWidth: config$1.measure.lineWidth , transparent: true, opacity: config$1.measure.highlight.opacity })), guide: LineDraw.createFatLineMat($.extend({},lineDepthInfo,{ color:config$1.measure.guide.color, dashSize: 0.1, gapSize: 0.02, dashed: true, lineWidth: config$1.measure.lineWidth/2 })), }; } return Measure$1.lineMats[type] } createAreaPlane(){ planeMats || (planeMats = { default: new DepthBasicMaterial( $.extend({},lineDepthInfo,{ color:color, side:DoubleSide, opacity:0.2, transparent:true, useDepth:true, })), selected: new MeshBasicMaterial({ color: color , side:DoubleSide, opacity:0.3, transparent:true, //wireframe:true }) },Measure$1.planeMats = planeMats); return super.createAreaPlane(planeMats.default) } raycast (raycaster, intersects) { for (let i = 0; i < this.points.length; i++) { let marker = this.markers[i]; marker.raycast(raycaster, intersects); } // recalculate distances because they are not necessarely correct // for scaled objects. // see https://github.com/mrdoob/three.js/issues/5827 // TODO: remove this once the bug has been fixed for (let i = 0; i < intersects.length; i++) { let I = intersects[i]; I.distance = raycaster.ray.origin.distanceTo(I.point); } intersects.sort(function (a, b) { return a.distance - b.distance; }); }; getPointsPos2d(viewport, update){//获取屏幕上的二维坐标 let ps = this.pointsPos2d.get(viewport); if(update || !ps){ let points = this.points.map(e=>{ let p = Potree.Utils.getPos2d(e, viewport, viewer.renderArea ); p.pos3d = e.clone(), p.object = this; return p }); this.pointsPos2d.set(viewport, points); console.log('updatePointsPos2d',this.uuid,viewport.name); } return this.pointsPos2d.get(viewport) } transformData(prop){ if(prop.measureType == 'Point'){ prop.showCoordinates = true, prop.closed = true, prop.maxMarkers = 1, prop.minMarkers = 1; }else if(prop.measureType == 'Distance'){ prop.showDistances = true, prop.showEdges = true, prop.maxMarkers = 2, prop.minMarkers = 2; }else if(prop.measureType == 'MulDistance'){ prop.showDistances = true, prop.showEdges = true, prop.minMarkers = 2; }else if(prop.measureType == 'MulDistance Ring'){ prop.showDistances = true, prop.showEdges = true, prop.showArea = true, prop.closed = true, prop.minMarkers = 3; }else if(prop.measureType == 'Ver MulDistance'){ prop.showDistances = true, prop.atPlane = true, prop.showEdges = true, prop.minMarkers = 2; prop.faceDirection = "vertical"; prop.unableDragAtMap = true; }else if(prop.measureType == 'Hor MulDistance'){ prop.showDistances = true, prop.atPlane = true, prop.showEdges = true, prop.minMarkers = 2; prop.faceDirection = "horizontal"; }else if(prop.measureType == 'Ver Distance'){ prop.showDistances = true, prop.showEdges = true, prop.maxMarkers = 2, prop.minMarkers = 2, prop.faceDirection = "vertical"; prop.unableDragAtMap = true; }else if(prop.measureType == 'Hor Distance'){ prop.showDistances = true, prop.showEdges = true, prop.maxMarkers = 2, prop.minMarkers = 2, prop.faceDirection = "horizontal"; }else if(prop.measureType == 'Area'){ prop.showDistances = true, Potree.settings.areaAtNotPlane || (prop.atPlane = true); prop.showEdges = true, prop.closed = true, prop.minMarkers = 3; }else if(prop.measureType == 'Hor Area'){ prop.showDistances = true, prop.atPlane = true, prop.showEdges = true, prop.closed = true, prop.minMarkers = 3; prop.faceDirection = "horizontal"; }else if(prop.measureType == 'Ver Area'){ prop.showDistances = true, prop.atPlane = true, prop.showEdges = true, prop.closed = true, prop.minMarkers = 3; prop.faceDirection = "vertical"; prop.unableDragAtMap = true; }else if(prop.measureType == 'Rect Area'){ prop.showDistances = true, prop.atPlane = true, prop.showEdges = true, prop.closed = true, prop.minMarkers = 4; prop.maxMarkers = 4; }else if(prop.measureType == 'Hor Rect Area'){ prop.showDistances = true, prop.atPlane = true, prop.showEdges = true, prop.closed = true, prop.minMarkers = 4; prop.maxMarkers = 4; prop.isRect = true; prop.faceDirection = "horizontal"; }else if(prop.measureType == 'Ver Rect Area'){ prop.showDistances = true, prop.atPlane = true, prop.showEdges = true, prop.closed = true, prop.minMarkers = 4; prop.maxMarkers = 4; prop.isRect = true; prop.faceDirection = "vertical"; prop.unableDragAtMap = true; } if(prop.atPlane && prop.closed){ //atPlane在同一平面上 prop.showArea = true; } super.transformData(prop); } setUnitSystem(unitSystem){ //console.log(this.name +':' +this.unitSystem) if(unitSystem != this.unitSystem){ if(unitSystem == "metric"){ }else if(unitSystem == 'imperial'){ } this.unitSystem = unitSystem; this.update(); } } reDraw(restMarkerCount=0){//重新开始画 super.reDraw(restMarkerCount); if(this.measureType == 'Distance'){ this.shouldShowHorVerGuide = false; this.setEdgesDisplay(false); } if(this.showArea){ this.area = {value:0}; this.areaLabel && Utils.updateVisible(this.areaLabel, 'setVisible', false ); } if(this.totalDisLabel && this.showTotalDis){ Utils.updateVisible(this.totalDisLabel, 'setVisible', false ); } viewer.inputHandler.dispatchEvent( {type:'measuring', v:true, cause:'reDraw',object:this, situation:'dragging'} ); } } function setLabelHightState(label, state){ if(state){ let color = new Color(Potree.config.measure.highlight.color); //label.sprite.material.opacity = config.measure.highlight.opacity //label.setBackgroundColor({r:255*color.r, g:255*color.g, b:255*color.b, a:config.measure.highlight.opacity}) label.sprite.material.useDepth = false; //label.textColor = {r: this.color.r*255, g: this.color.g*255, b: this.color.b*255, a: 1} }else { //label.setBackgroundColor({r: this.color.r*255, g: this.color.g*255, b: this.color.b*255, a:config.measure.default.opacity}) label.sprite.material.useDepth = true; //label.sprite.material.opacity = 0.98 //label.textColor = {r: 255, g: 255, b: 255, a: 1} } label.updateTexture(); } /* function setLabelHightState(label, state){ if(state){ label.setBackgroundColor({r: highlightColor.r*255, g: highlightColor.g*255, b: highlightColor.b*255, a:config.measure.highlight.labelOpacity}) label.sprite.material.useDepth = false; }else{ label.setBackgroundColor(mainLabelProp.backgroundColor) label.sprite.material.useDepth = true } label.updateTexture() //label.sprite.material.needsUpdate = true } */ function createCircleRadiusLabel$1(){ const circleRadiusLabel = new TextSprite$2(""); circleRadiusLabel.setTextColor({r: 140, g: 250, b: 140, a: 1.0}); circleRadiusLabel.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); circleRadiusLabel.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); circleRadiusLabel.fontsize = 16; circleRadiusLabel.material.depthTest = false; circleRadiusLabel.material.opacity = 1; circleRadiusLabel.visible = false; return circleRadiusLabel; } function createCircleRadiusLine$1(){ /* const lineGeometry = new LineGeometry(); lineGeometry.setPositions([ 0, 0, 0, 0, 0, 0, ]); const lineMaterial = new LineMaterial({ color: 0xff0000, lineWidth: 2, resolution: new THREE.Vector2(1000, 1000), gapSize: 1, dashed: true, }); lineMaterial.depthTest = false; const circleRadiusLine = new Line2(lineGeometry, lineMaterial);*/ var circleRadiusLine = LineDraw.createFatLine([ ],{ color:0xff0000, dashSize: 0.5, gapSize: 0.2, lineWidth: config$1.measure.lineWidth }); circleRadiusLine.visible = false; return circleRadiusLine; } function createCircleLine$1(){ const coordinates = []; let n = 128; for(let i = 0; i <= n; i++){ let u0 = 2 * Math.PI * (i / n); let u1 = 2 * Math.PI * (i + 1) / n; let p0 = new Vector3( Math.cos(u0), Math.sin(u0), 0 ); let p1 = new Vector3( Math.cos(u1), Math.sin(u1), 0 ); coordinates.push( p0, p1 ); } /* const geometry = new LineGeometry(); geometry.setPositions(coordinates); const material = new LineMaterial({ color: 0xff0000, dashSize: 5, gapSize: 2, lineWidth: 2, resolution: new THREE.Vector2(1000, 1000), }); material.depthTest = false; const circleLine = new Line2(geometry, material); circleLine.visible = false; circleLine.computeLineDistances();*/ var circleLine = LineDraw.createFatLine(coordinates,{ color: 0xff0000, dashSize: 0.5, gapSize: 0.2, lineWidth: config$1.measure.lineWidth }); return circleLine; } /* function createCircleCenter(){ const sg = new THREE.markerGeometry(1, 32, 32); const sm = new THREE.MeshNormalMaterial(); const circleCenter = new THREE.Mesh(sg, sm); circleCenter.visible = false; return circleCenter; } */ function createLine$1(){ const line = LineDraw.createFatLine([ ],{ color: 0xff0000, dashSize: 0.5, gapSize: 0.2, lineWidth: config$1.measure.lineWidth }); return line; } function createCircle$1(){ const coordinates = []; let n = 128; for(let i = 0; i <= n; i++){ let u0 = 2 * Math.PI * (i / n); let u1 = 2 * Math.PI * (i + 1) / n; let p0 = new Vector3( Math.cos(u0), Math.sin(u0), 0 ); let p1 = new Vector3( Math.cos(u1), Math.sin(u1), 0 ); coordinates.push( p0, p1 ); } var line = LineDraw.createFatLine(coordinates,{ color: 0xff0000, dashSize: 0.5, gapSize: 0.2, lineWidth: config$1.measure.lineWidth }); return line; } /* function createAzimuth(){ const azimuth = { label: null, center: null, target: null, north: null, centerToNorth: null, centerToTarget: null, centerToTargetground: null, targetgroundToTarget: null, circle: null, node: null, }; const sg = new THREE.markerGeometry(1, 32, 32); const sm = new THREE.MeshNormalMaterial(); { const label = new TextSprite(""); label.setTextColor({r: 140, g: 250, b: 140, a: 1.0}); label.setBorderColor({r: 0, g: 0, b: 0, a: 1.0}); label.setBackgroundColor({r: 0, g: 0, b: 0, a: 1.0}); label.fontsize = 16; label.material.depthTest = false; label.material.opacity = 1; azimuth.label = label; } azimuth.center = new THREE.Mesh(sg, sm); azimuth.target = new THREE.Mesh(sg, sm); azimuth.north = new THREE.Mesh(sg, sm); azimuth.centerToNorth = createLine(); azimuth.centerToTarget = createLine(); azimuth.centerToTargetground = createLine(); azimuth.targetgroundToTarget = createLine(); azimuth.circle = createCircle(); azimuth.node = new THREE.Object3D(); azimuth.node.add( azimuth.centerToNorth, azimuth.centerToTarget, azimuth.centerToTargetground, azimuth.targetgroundToTarget, azimuth.circle, azimuth.label, azimuth.center, azimuth.target, azimuth.north, ); return azimuth; } */ /* 按alt鼠标滚轮或WS键放慢。 按Alt键可以平行屏幕拖拽点。&dragPolyBeyondPoint 后缀在拖拽到无点云区域也是此效果。 按M键拖拽点可以复制出当前点 */ class History extends EventDispatcher{ constructor(o){ super(); this.undoList = []; this.redoList = []; this.applyData = o.applyData; //应用数据的方法 this.getData = o.getData; //获取数据的方法 this.dataBefore ;(o.viewer || viewer).inputHandler.addEventListener('keydown', (e)=>{ if(e.keyCode == 90 && e.event.ctrlKey){//Z this.undo(); }else if(e.keyCode == 89 && e.event.ctrlKey){//Y this.redo(); } }); } undo(){//回退 let length = this.undoList.length; if(length>0){ let unExist; let last = this.undoList.pop(); this.applyData && (unExist = !this.applyData(last.before)); unExist || this.redoList.push(last); unExist && this.undo();//找不到就回退下一个。 this.dispatchEvent('undo'); //console.log('undo',last) } } redo(){//撤销回退 let length = this.undoList.length; let last = this.redoList.pop(); if(last){ //注意:每行的顺序不能乱 this.undoList.push(last); this.applyData && this.applyData(last.after); this.dispatchEvent('redo'); //console.log('redo',last) } } beforeChange(o){//在变化之前(可能执行好几次直到变化完,但只有第一次有效)。 o的内容完全根据getData怎么定义 if(!this.dataBefore){ let data = this.getData(o); data && (this.dataBefore = data); } } afterChange(o){//变化结束,从beforeChange到此算一次操作。 if(this.dataBefore){ this.writeIn({before:this.dataBefore, after:this.getData(o)} ); //写入某物体变化前和变化后的状态 this.dataBefore = null; } } writeIn(data ){ this.redoList.length = 0; //一旦录入新的操作,就不允许undo了 this.undoList.push(data ); //console.log('新增undo', data) } clear(){ this.redoList.length = 0; this.undoList.length = 0; this.dataBefore = null; } } let areaPlaneMats = { }; class Prism extends Measure$1{ constructor(args){ super(args); this.isPrism = true; this.volumeInfo = {}; this.needsCompute = true; /*this.createPrismLines(new THREE.Color('#FFF') ) this.lineMesh.material.transparent = true this.lineMesh.material.opacity = 0.5 Potree.Utils.setObjectLayers(this.lineMesh, 'measure' ) */ this.setClipBox(); this.setTopOrBtm('zMin'); this.setTopOrBtm('zMax'); this.setHorizonHeight('btm'); let update =()=>{ this.setHorizonHeight(this.horizonType, true);//更新 this.needsCompute = true; this.getBound(); this.dispatchEvent('updated'); }; this.addEventListener('marker_dropped'/* 'marker_moved' */,update); this.addEventListener('changeByHistory',update); this.addEventListener('changed',update); /* this.addEventListener('createDone',()=>{ this.setHorizonHeight()//更新 this.updateAreaPlane() }) */ this.updateAreaPlane(); this.getBound(); this.addEventListener('needsCompute',()=>{ this.needsCompute = true; },10); //Potree.Utils.setObjectLayers(this.lineMesh, 'bothMapAndScene' ) } set needsCompute(state){ if(state == 'byVolume'){ this.needsCompute_ = this.volumeInfo.Vupper == void 0; }else { this.needsCompute_ = !!state; //console.error('needsCompute', state) } } get needsCompute(){ return this.needsCompute_ } setHorizonHeight(v){ let old = this.horizonZ; if(typeof v == 'number'){ this.horizonType = 'number'; this.horizonZ = v; }else { if(v){ this.horizonType = v; } if(!this.horizonType) this.horizonType = 'btm'; if(this.horizonType == 'number')return let zs = this.points.map(p=>p.z).sort((a,b)=>a-b); this.horizonZ = this.horizonType == 'btm' ? zs[0] : zs[this.points.length-1]; } //this.horizonZ = THREE.Math.clamp(this.horizonZ, this.zMin, this.zMax ) if(this.horizonZ != old ){ this.needsCompute = true; this.update(); //update areaPlane and areaLabel pos this.dispatchEvent('horizonZChanged'); } //console.log(this.horizonZ) } setBaseModel(model, modelHaventLoad){//三种状态:使用model、使用基准高度、 什么都没有(不允许计算、不高亮、等待模型上传) this.baseModel = model; this.modelHaventLoad = modelHaventLoad; Potree.Utils.updateVisible(this.areaPlane,'unuseHorizonH',!model && !modelHaventLoad); } setClipBox(boxes){//clipBoxes_out this.clipBoxes = boxes || []; } setTopOrBtm(name,v){ if(v != void 0 ){ this[name] = v; this.updatePrismLines(); }else { this[name] = viewer.bound.boundingBox[name == 'zMin' ? 'min' : 'max'].z; } this.getBound(); this.needsCompute = true; } getBound(){ let bound = Potree.math.getBound(this.points); bound.min.z = this.zMin; bound.max.z = this.zMax; this.prismBound = bound; } setVolumeInfo(volumeInfo){ this.volumeInfo = volumeInfo; this.getVolumeString(); this.needsCompute = false; //但得到的不一定是对的可能 检查下 } getVolumeString(){ if(!this.volumeInfo || this.volumeInfo.Vupper == void 0)return; this.VupperString = this.getConvertString(this.volumeInfo.Vupper, 'volume', true); this.VlowerString = this.getConvertString(this.volumeInfo.Vlower, 'volume', true); this.highestString = this.getConvertString(this.volumeInfo.highest, 'distance', true); this.lowestString = this.getConvertString(this.volumeInfo.lowest, 'distance', true); } getConvertString(num, type, restrictUnit){ if(!restrictUnit)return super.getConvertString(num, type) return viewer.unitConvert.convert(num, type, Potree.settings.precision, this.unitSystem, null , {//在pdf里的高度要统一单位 'imperial': {restrictUnit: 'Foot'}, //ft 'metric': {restrictUnit: 'Meter'} } ) } setUnitSystem(unitSystem){ let old = this.unitSystem; super.setUnitSystem(unitSystem); if(unitSystem != old){ this.getVolumeString(); } } getCenter(type){ if(type == 'areaPlaneCenter'){//areaPlane中心 return this.areaPlaneCenter } return super.getCenter() //点的中心 } update(){ super.update.apply(this, arguments); this.updatePrismLines(); } getTransformationMatrix(pointcloud) {//剪裁矩阵 let invMatrix = new Matrix4; //把当前z移动到-0.5 ~ 0.5所需要的变换 let minZ = this.zMin;//viewer.bound.boundingBox.min.z let maxZ = this.zMax;//viewer.bound.boundingBox.max.z let s = 1/(maxZ-minZ); let pos = new Vector3(0,0, -0.5 - minZ * s ), scale = new Vector3(1,1,s); invMatrix.compose( pos, new Quaternion, scale ); //先缩放后位移 return (new Matrix4).multiplyMatrices(invMatrix, pointcloud.transformMatrix).transpose() } changeStyleForScreenshot(state, {hideLabel,showName}={}){//截图前变一下外观。根据高度变化颜色 let config = Potree.settings.prismHeightColor; let color;//, lineWidth = this.edges[0].material.lineWidth if(this.volumeInfo.Vupper > 0.01 && this.volumeInfo.Vlower > 0.01){ color = config.every.color; }else if(this.volumeInfo.Vupper > this.volumeInfo.Vlower){ for(let i = config.dig.length-1; i>=0; i--){ if(this.volumeInfo.Vupper >= config.dig[i].min){ color = config.dig[i].color; break } } }else { for(let i = config.fill.length-1; i>=0; i--){ if(this.volumeInfo.Vlower >= config.fill[i].min){ color = config.fill[i].color; break } } } color = new Color(color[0]/255,color[1]/255,color[2]/255); let name = color.getHexString(); if(state){ Potree.Utils.updateVisible(this.areaPlane,'screenshot', true, 2, 'add'); //强制显示 let mat = areaPlaneMats[name]; if(!mat){ mat = new MeshBasicMaterial({ color, transparent:true, opacity:0.6, side:DoubleSide, }); areaPlaneMats[name] = mat; } this.oldAreaPlaneMat = this.areaPlane.material; this.areaPlane.material = mat; if(hideLabel){ Potree.Utils.updateVisible(this.areaLabel, 'screenshot-single',false); } if(showName){ this.areaLabel.setText([this.name, this.area.string]); /* let btm = this.areaPlaneCenter.clone().setY(this.prismBound.min.y) //置于土方2d块底部 this.areaLabel.setPos(btm) this.areaLabel.sprite.position.y = -0.25 */ } this.markers.forEach(marker=>Potree.Utils.updateVisible(marker,'screenshot-Prism',false)); this.edges.forEach(edge=>Potree.Utils.updateVisible(edge,'screenshot-Prism',false)); //Potree.Utils.updateVisible(this.lineMesh,'screenshot-Prism',false) this.styleRecover = ()=>{ this.areaPlane.material = this.oldAreaPlaneMat; if(hideLabel){ Potree.Utils.updateVisible(this.areaLabel, 'screenshot-single', true); } if(showName){ this.areaLabel.setText(this.area.string); /* this.areaLabel.setPos(this.areaPlaneCenter) this.areaLabel.sprite.position.y = 0 */ } this.markers.forEach(marker=>Potree.Utils.updateVisible(marker,'screenshot-Prism',true)); this.edges.forEach(edge=>Potree.Utils.updateVisible(edge,'screenshot-Prism',true)); //Potree.Utils.updateVisible(this.lineMesh,'screenshot-Prism',true) this.styleRecover = null; }; }else { this.styleRecover && this.styleRecover(); Potree.Utils.updateVisible(this.areaPlane, 'screenshot', false, 2, 'cancel'); } } dispose(){ super.dispose(); this.clipBoxes.forEach(e=>viewer.scene.removeVolume(e)); } setEditState(state){//编辑页面or截图 state = !!state; this.editing = state; this.dontHighlight = !state; this.clipBoxes.forEach(box=>{ Potree.Utils.updateVisible(box,'hidden',state), box.clip = state; }); } /* { fill: [ {color: [236, 213, 143, 1], min: 0, max: 5}, {color: [223, 118, 21, 1], min: 5, max: 10}, // 没有min表示大于 {color: [186, 57, 57, 1], min: 10} ], dig: [ {color: [144, 193, 190, 1], min: 0, max: 5}, {color: [76, 155, 211, 1], min: 5, max: 10}, // 没有min表示大于 {color: [79, 76, 211, 1], min: 10} ], // 填挖并存 every: { color: [49, 200, 181, 1] } } */ } class MeasuringTool extends EventDispatcher{ constructor (viewer) { super(); this.viewer = viewer; this.renderer = viewer.renderer; this.viewer.addEventListener('start_inserting_measurement', e => { this.viewer.dispatchEvent({ type: 'cancel_insertions' }); }); this.showLabels = true; this.scene = new Scene(); this.scene.name = 'scene_measurement'; //this.light = new THREE.PointLight(0xffffff, 1.0); //this.scene.add(this.light); this.viewer.inputHandler.registerInteractiveScene(this.scene); this.history = new History({ applyData: (data)=>{ if(data.measure.parent && data.measure.visible){ data = Potree.Common.CloneObject(data); //避免使用后更改数据又被使用 data.measure.reDraw(); data.measure.initData(data); data.measure.isNew = data.isNew; data.measure.dispatchEvent('changeByHistory'); /* if(data.measure.isPrism){ data.measure.needsCompute = data.needsCompute } */ data.measure.facePlane = data.facePlane && data.facePlane.clone(); data.measure.cannotConfirmNormal = data.cannotConfirmNormal; return true } }, getData:(measure)=>{ let data = { measure, points: measure.points.map(e=>e.clone()), dataset_points: measure.dataset_points ? measure.dataset_points.map(e=>e&&e.clone()) : null, points_datasets: measure.points_datasets.slice(), datasetId: measure.datasetId, isNew: measure.isNew, facePlane: measure.facePlane && measure.facePlane.clone(), cannotConfirmNormal: measure.cannotConfirmNormal }; /* if(measure.isPrism){ //没用 data.volumeInfo = measure.volumeInfo data.needsCompute = measure.needsCompute //但不记录其他信息 } */ return data } }), //this.scene = viewer.overlay// this.onRemove = (e) => { e.measurement.dispose();/* this.scene.remove(e.measurement); */}; this.onAdd = e => {this.scene.add(e.measurement);}; for(let measurement of viewer.scene.measurements){ this.onAdd({measurement: measurement}); } viewer.addEventListener('camera_changed', this.update.bind(this),{importance:10});//优先级高于sprite if(Potree.config.measure.mulLabelHideFaraway ){ viewer.addEventListener("raycaster", this.beforeDraw.bind(this),{importance:10}); //before render viewer.addEventListener("render.begin", this.beforeDraw.bind(this),{importance:10}); //before render viewer.addEventListener("render.begin2", this.beforeDraw.bind(this),{importance:10}); } //viewer.addEventListener("update", this.update.bind(this)); viewer.addEventListener("render.pass.perspective_overlay", this.render.bind(this)); viewer.addEventListener("scene_changed", this.onSceneChange.bind(this)); viewer.scene.addEventListener('measurement_added', this.onAdd); viewer.scene.addEventListener('measurement_removed', this.onRemove); viewer.addEventListener('resize',this.setSize.bind(this)); } onSceneChange(e){ if(e.oldScene){ e.oldScene.removeEventListener('measurement_added', this.onAdd); e.oldScene.removeEventListener('measurement_removed', this.onRemove); } e.scene.addEventListener('measurement_added', this.onAdd); e.scene.addEventListener('measurement_removed', this.onRemove); } createMeasureFromData(data){//add const measure = data.measureType == 'MulDistance Ring' ? new Prism(data) : new Measure$1(data); if(measure.failBuilded){ return } viewer.scene.addMeasurement(measure); if(measure.guideLine)measure.guideLine.visible = false; return measure } beforeDraw(e){ if(e.viewport.name == 'mapViewport' && !viewer.mapViewer.attachedToViewer)return //no measures viewer.scene.measurements.forEach(measure=>{ if(measure.measureType != 'MulDistance' || (measure.isNew ? measure.points.length<4 : measure.points.length<3)) return let lastIndex = measure.points.length - 1; for (let index = 0; index <= lastIndex; index++) { if(!measure.closed && index == lastIndex)continue let edgeLabel = measure.edgeLabels[index]; let v = edgeLabel.visiMap.get(e.viewport.camera); if(v == void 0){ return measure.getEdgeLabelVisi(e.viewport) } Potree.Utils.updateVisible(edgeLabel, 'tooFar', v === false ? false : true); } }); } update(e){ /* viewer.scene.measurements.forEach(measure=>{ if(measure.measureType != 'MulDistance' || (measure.isNew ? measure.points.length<4 : measure.points.length<3)) return }) */ if(viewer.inputHandler.measuring && Potree.settings.adsorption){ viewer.scene.measurements.forEach(measure=>{ measure.getPointsPos2d(e.viewport, true); }); } if(Potree.config.measure.mulLabelHideFaraway){ if(e.changeInfo.projectionChanged || e.viewport.camera.type == 'PerspectiveCamera' && e.changeInfo.positionChanged){//for MulDistance viewer.scene.measurements.forEach(measure=>{ if(measure.measureType != 'MulDistance' || (measure.isNew ? measure.points.length<4 : measure.points.length<3)) return measure.getEdgeLabelVisi(e.viewport); return let lastIndex = measure.points.length - 1; for (let index = 0; index <= lastIndex; index++) { if(!measure.closed && index == lastIndex)continue let nextIndex = (index + 1 > lastIndex) ? 0 : index + 1; let previousIndex = (index === 0) ? lastIndex : index - 1; let point = measure.points[index]; let nextPoint = measure.points[nextIndex]; let previousPoint = measure.points[previousIndex]; let point2d = new Vector2().copy(pos2ds[index]); let nextPoint2d = new Vector2().copy(pos2ds[nextIndex]); if(measure.showDistances || measure.labelText){ // edge labels let edgeLabel = measure.edgeLabels[index]; if(edgeLabel.visible){ measure.setEdgeLabelPos(edgeLabel, point, nextPoint ); } } } }); } } return; let camera = this.viewer.scene.getActiveCamera(); let domElement = this.renderer.domElement; let measurements = this.viewer.scene.measurements; // make size independant of distance let mainLabels = [], subLabels = []; for (let measure of measurements) { measure.lengthUnit = this.viewer.lengthUnit; measure.lengthUnitDisplay = this.viewer.lengthUnitDisplay; //measure.update(); updateAzimuth(this.viewer, measure); /* [...measure.markers, ...measure.edgeLabels, measure.areaLabel].forEach(e=>{ e && e.update() }); */ // labels /* let labels = measure.edgeLabels.concat(measure.angleLabels); for(let label of labels){ label.update() if(label.elem.hasClass('sub')){ subLabels.push(label) }else{ mainLabels.push(label) } } // coordinate labels for (let j = 0; j < measure.coordinateLabels.length; j++) { let label = measure.coordinateLabels[j]; label.update() mainLabels.push(label) } if(measure.showArea){ // area label let label = measure.areaLabel; label.update() mainLabels.push(label) } */ /* if(measure.showCircle){ // radius label let label = measure.circleRadiusLabel; let distance = label.position.distanceTo(camera.position); let pr = Utils.projectedRadius(1, camera, distance, clientWidth, clientHeight); let scale = (70 / pr); label.scale.set(scale, scale, scale); } */ if(!this.showLabels){ const labels = [ ...measure.sphereLabels, ...measure.angleLabels, measure.circleRadiusLabel, ]; for(const label of labels){ label.visible = false; } } } //this.updateLabelZIndex([{labels:subLabels},{labels:mainLabels}]) } setSize(e){ //e.resolution /* if(Measure.lineMats){ for(var m in Measure.lineMats){ Measure.lineMats[m].resolution.set(e.canvasWidth, e.canvasHeight); } } if(Measure.sphereMats){ for(var s in Measure.sphereMats){ Measure.sphereMats[s].uniforms.resolution.value.set(e.canvasWidth, e.canvasHeight); } } for (let measure of this.viewer.scene.measurements) { measure.edgeLabels.concat(measure.areaLabel).forEach(label=>{ label.sprite.material.uniforms.resolution.value.set(e.canvasWidth, e.canvasHeight); }) } */ } updateLabelZIndex(group){//[{labels:[]},{}] 顺序按照z-index低到高 group.forEach((e,i)=>{ e.base = group[i-1] ? group[i-1].base + group[i-1].labels.length : 0; var labels = e.labels.sort((a,b)=>{ return b.pos2d.z - a.pos2d.z }); labels.forEach((label,index)=>{ $(label.elem).css('z-index', e.base+index); }); }); } changeEditMode(mode){ viewer.dispatchEvent({type : "CursorChange", action : "remove", name:"addPoint"}); viewer.dispatchEvent({type : "CursorChange", action : "remove", name:"delPoint"}); if(mode){ if(mode == 'addPoint'){ viewer.dispatchEvent({type : "CursorChange", action : "add", name:"addPoint"}); }else { viewer.dispatchEvent({type : "CursorChange", action : "add", name:"delPoint"}); } } this.editMode = mode; } editStateChange(e){ //console.log("editStateChange" , e.state) let state = e.state; if(!state){ state = viewer.scene.measurements.some(e=>e.isEditing); } if(state){ viewer.dispatchEvent({type:"measureMovePoint"});//重新激活reticule状态 }else { viewer.dispatchEvent({type:"endMeasureMove"}); } //this.editing = } startInsertion (args = {}, callback, cancelFun) { let domElement = this.viewer.renderer.domElement; let measure = args.measureType == 'MulDistance Ring' ? new Prism(args) : new Measure$1(args); this.scene.add(measure); measure.isNew = true; this.viewer.dispatchEvent({ type: 'start_inserting_measurement', measure: measure }); measure.addEventListener('editStateChange', this.editStateChange.bind(this)); measure.editStateChange(true); let timer; this.isAdding = true; let endDragFun = (e) => { let length = measure.points.length; if (e.button == MOUSE.LEFT || e.isTouch) { if (length >= measure.maxMarkers) { end({finish:true}); }else { this.history.beforeChange(measure); let marker = measure.cloneMarker(length - 1, length); if(args.isRect && measure.markers.length == 3){//marker全可见 measure.cloneMarker(0, 3); }else { measure.markers[length].visible = false; measure.edges[length].visible = false; } measure.edges[length-1].visible = true; measure.markers[length-1].visible = true; marker.isDragging = true; this.history.afterChange(measure); measure.continueDrag(marker, e); } } else if (e.button === MOUSE.RIGHT ) { //触屏怎么取消? if(e.pressDistance < Potree.config.clickMaxDragDis){//非拖拽的话 var isIntersectSelf = measure.atPlane && measure.closed && !measure.isRect && measure.point2dInfo && measure.intersectSelf(measure.point2dInfo.points2d.slice(0,measure.point2dInfo.points2d.length-1));//检测除了最后一个点的相交情况 if(!isIntersectSelf)end(e); else measure.continueDrag(null, e); }else measure.continueDrag(null, e); } }; let end = (e={}) => {//确定、结束 if(!measure.isNew )return if(args.minMarkers != void 0 ){ if(!e.finish && measure.markers.length<=args.minMarkers ){//右键 当个数不够时取消 //this.viewer.scene.removeMeasurement(measure) //重新开始画 if(measure.markers.length>0){ measure.markers[0].removeEventListener('mousedown',end); measure.reDraw(); this.viewer.addEventListener('global_click', click, {importance:10}); measure.editStateChange(true); } return /* if(!Potree.settings.isOfficial) this.viewer.scene.removeMeasurement(measure) else if(e.drag){ //正式版本不允许右键退出, 继续 continueDrag(e.drag.object) measure.editStateChange(true) return } */ } } let lastMarker = measure.markers[measure.markers.length-1]; if (/* !e.finish&& */ measure.markers.length > args.minMarkers) { measure.removeMarker(measure.points.length - 1); measure.markers[0].removeEventListener('mouseover', mouseover); measure.markers[0].removeEventListener('mouseleave', mouseleave); measure.markers[0].removeEventListener('click'/* 'mousedown' */,Exit); if(e.byClickMarker && measure.markers.length > args.minMarkers ){//通过点击第一个marker而结束的话,会多一个marker。但点击marker后可能会因为和它坐标一致而没有添加成功,就不删,根据添加的时间来判断。 let delta = Date.now() - lastMarker.createTime; //console.log('delta',delta) if(delta < 10){//刚生成的,说明点击后新增了一个marker measure.removeMarker(measure.points.length - 1); } } } measure.isNew = false; let length = measure.points.length; if(length){ measure.markers[length-1].visible = true; measure.edges[length-1].visible = !!measure.closed; measure.markers.forEach(marker=>{marker.dispatchEvent('addHoverEvent'); }); measure.edges.forEach(edge=>{edge.dispatchEvent('addHoverEvent'); }); measure.update();//update last edgeLabel } clearTimeout(timer); this.viewer.removeEventListener('cancel_insertions', Exit); //pressExit && this.viewer.inputHandler.removeEventListener('keydown', pressExit); this.viewer.removeEventListener('global_click', click); this.viewer.removeEventListener('global_mousemove', ifAtWrongPlace); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace" }); viewer.inputHandler.dispatchEvent({type:'measuring', v:false, cause:'stopInsertion', situation:'adding', object:measure} ); //var isIntersectSelf = measure.atPlane && measure.closed && !measure.isRect && measure.point2dInfo && measure.intersectSelf(measure.point2dInfo.points2d.slice(0,measure.point2dInfo.points2d.length-1))//检测除了最后一个点的相交情况 var isIntersectSelf = measure.atPlane && measure.closed && !measure.isRect && measure.point2dInfo && measure.intersectSelf(measure.point2dInfo.points2d); if(isIntersectSelf) return cancelFun && cancelFun() //请求删除,不重建 e.remove || callback && callback(); /* this.viewer.dispatchEvent({ type: 'finish_inserting_measurement', measure: measure }); */ this.isAdding = false; measure.dispatchEvent('createDone'); }; measure.addEventListener('finish', end); //完成 let Exit = (e)=>{//强制退出 if(e.measure && e.measure != measure || !viewer.scene.measurements.includes(measure) || !measure.isNew){ return;//若指定了退出的measure但和该measure不一致,就返回 } if(e.remove || e.type == 'cancel_insertions'){ viewer.scene.removeMeasurement(measure); } measure.editStateChange(false); measure.cannotConfirmNormal = false; //一些dropMarker中的句子 measure.guideLine && (measure.guideLine.visible = false); /* if(this.viewer.inputHandler.drag && !e.remove ){//还未触发drop的话 this.viewer.inputHandler.drag.object.dispatchEvent({ //这句会导致又增一个marker type: 'drop', drag: this.viewer.inputHandler.drag, viewer: this.viewer, pressDistance:0, button : THREE.MOUSE.RIGHT }); } else {*///未结束时添加新的measure时会触发 end({finish:true, remove:e.remove, byClickMarker: e.type == 'click'}); //} this.viewer.inputHandler.drag && (this.viewer.inputHandler.drag.object = null); }; this.viewer.addEventListener('cancel_insertions', Exit); /*let pressExit if(!Potree.settings.isOfficial){ pressExit = (e)=>{ if(e.keyCode == 27){//Esc //Exit() //怎么模拟右键???//现由前端发出 } } this.viewer.inputHandler.addEventListener('keydown', pressExit) } */ let mouseover = (e) => { measure.setMarkerSelected(e.object, 'hover', 'single'); }; let mouseleave = (e) => { measure.setMarkerSelected(e.object, 'unhover', 'single'); }; let click = (e)=>{//一旦点击就立刻增加两marker if(ifAtWrongPlace(e))return if(e.clickElement)return //如点击label时focusOnObject if(e.button === MOUSE.RIGHT)return //console.log('measure clicked33', !!e.intersectPoint) //var I = e.intersectPoint && (e.intersectPoint.orthoIntersect || e.intersectPoint.location) var I = e.intersect && (e.intersect.orthoIntersect || e.intersect.location); if(!I){ return measure.dispatchEvent('intersectNoPointcloud') } var atMap = e.drag.dragViewport.name == 'mapViewport'; //在地图上测量的首个点按楼层高度(暂时先只按mainViewport相机高度吧,但navvis是按楼层,画在楼层的地面上,可能因为平面图显示的是楼层近地面), if(atMap){ I = I.clone().setZ(viewer.mainViewport.camera.position.z ); } var marker = measure.addMarker({point:I}); marker.isDragging = true; this.viewer.inputHandler.startDragging(marker , {endDragFun, notPressMouse:true} ); //notPressMouse代表不是通过按下鼠标来拖拽 e.drag = this.viewer.inputHandler.drag; /* e.drag.endDragFun = endDragFun e.drag.notPressMouse = true */ //if(!measure.dragMarker(e) || !measure.dropMarker(e))return measure.dragMarker(e); measure.dropMarker(e); if(measure.maxMarkers > 1 ){ measure.markers[1].visible = false; measure.edges[1].visible = false; } if(measure.closed && !measure.isRect){ measure.markers[0].addEventListener('mouseover', mouseover); measure.markers[0].addEventListener('mouseleave', mouseleave); measure.markers[0].addEventListener('click'/* 'mousedown' */,Exit); //点击到第一个marker就结束 } this.viewer.removeEventListener('global_click', click);///* global_drop */ //console.log('measure clicked') e.consume && e.consume(); return {stopContinue:true}//防止继续执行别的侦听,如flytopano }; //点击第n下拥有n+1个marker, n>0 viewer.inputHandler.dispatchEvent({type: 'measuring', v: true, cause:'startInsertion', situation:'adding', object:measure}); this.viewer.addEventListener('global_click', click, {importance:10});//add importance let ifAtWrongPlace = (e)=>{ if(measure.unableDragAtMap && e.hoverViewport.name == 'mapViewport' ){ if(e.isTouch){ viewer.dispatchEvent({type:'reticule_forbit', v:true}); }else { viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"polygon_AtWrongPlace" }); } return true }else { if(e.isTouch){ viewer.dispatchEvent({type:'reticule_forbit',v:false}); }else { viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"polygon_AtWrongPlace" }); } } }; if(measure.unableDragAtMap){ this.viewer.addEventListener('global_mousemove', ifAtWrongPlace); } let changeByHistory = (e)=>{ if(!measure.isNew)return let marker = measure.markers[measure.points.length-1]; this.viewer.inputHandler.startDragging(marker , {endDragFun, notPressMouse:true} ); var I = viewer.inputHandler.intersect && (viewer.inputHandler.intersect.orthoIntersect || viewer.inputHandler.intersect.location); if(I){ measure.dragChange(I.clone(), measure.points.length-1 ); //使最后一个点在鼠标处 } /* if(measure.markers.length == 1){ Common.updateVisible(marker, ,false) } */ args.isRect || ( measure.edges[measure.points.length-1].visible = false); //measure.continueDrag(measure.markers[measure.points.length-1], o ) }; measure.addEventListener('changeByHistory',changeByHistory); this.viewer.scene.addMeasurement(measure); return measure; } render(o={}){ if(this.scene.children.filter(e=>e.visible).length == 0)return let renderer = o.renderer || this.viewer.renderer; Potree.Utils.setCameraLayers(o.camera, ['measure']); /* if(o.screenshot && this.viewer.fxaaPass.enabled){ //抗锯齿 this.viewer.ssaaRenderPass.sampleLevel = 4 this.viewer.composer.render(this.scene, o.camera ); }else{ */ viewer.dispatchEvent({type: "render.begin2" , name:'measure', viewport:o.viewport, renderer:o.renderer }); renderer.render(this.scene, o.camera ); //} } }; function updateAzimuth(viewer, measure){ if(!measure.showAzimuth)return const azimuth = measure.azimuth; const isOkay = measure.points.length === 2; azimuth.node.visible = isOkay; if(!azimuth.node.visible){ return; } const camera = viewer.scene.getActiveCamera(); const renderAreaSize = viewer.renderer.getSize(new Vector2()); const width = renderAreaSize.width; const height = renderAreaSize.height; const [p0, p1] = measure.points; const r = p0.position.distanceTo(p1.position); const northVec = Utils.getNorthVec(p0.position, r, viewer.getProjection()); const northPos = p0.position.clone().add(northVec); azimuth.center.position.copy(p0.position); azimuth.center.scale.set(2, 2, 2); azimuth.center.visible = false; // azimuth.target.visible = false; { // north azimuth.north.position.copy(northPos); azimuth.north.scale.set(2, 2, 2); let distance = azimuth.north.position.distanceTo(camera.position); let pr = Utils.projectedRadius(1, camera, distance, width, height); let scale = (5 / pr); azimuth.north.scale.set(scale, scale, scale); } { // target azimuth.target.position.copy(p1.position); azimuth.target.position.z = azimuth.north.position.z; let distance = azimuth.target.position.distanceTo(camera.position); let pr = Utils.projectedRadius(1, camera, distance, width, height); let scale = (5 / pr); azimuth.target.scale.set(scale, scale, scale); } azimuth.circle.position.copy(p0.position); azimuth.circle.scale.set(r, r, r); azimuth.circle.material.resolution.set(width, height); // to target azimuth.centerToTarget.geometry.setPositions([ 0, 0, 0, ...p1.position.clone().sub(p0.position).toArray(), ]); azimuth.centerToTarget.position.copy(p0.position); azimuth.centerToTarget.geometry.verticesNeedUpdate = true; azimuth.centerToTarget.geometry.computeBoundingSphere(); azimuth.centerToTarget.computeLineDistances(); azimuth.centerToTarget.material.resolution.set(width, height); // to target ground azimuth.centerToTargetground.geometry.setPositions([ 0, 0, 0, p1.position.x - p0.position.x, p1.position.y - p0.position.y, 0, ]); azimuth.centerToTargetground.position.copy(p0.position); azimuth.centerToTargetground.geometry.verticesNeedUpdate = true; azimuth.centerToTargetground.geometry.computeBoundingSphere(); azimuth.centerToTargetground.computeLineDistances(); azimuth.centerToTargetground.material.resolution.set(width, height); // to north azimuth.centerToNorth.geometry.setPositions([ 0, 0, 0, northPos.x - p0.position.x, northPos.y - p0.position.y, 0, ]); azimuth.centerToNorth.position.copy(p0.position); azimuth.centerToNorth.geometry.verticesNeedUpdate = true; azimuth.centerToNorth.geometry.computeBoundingSphere(); azimuth.centerToNorth.computeLineDistances(); azimuth.centerToNorth.material.resolution.set(width, height); // label const radians = Utils.computeAzimuth(p0.position, p1.position, viewer.getProjection()); let degrees = MathUtils.radToDeg(radians); if(degrees < 0){ degrees = 360 + degrees; } const txtDegrees = `${degrees.toFixed(2)}°`; const labelDir = northPos.clone().add(p1.position).multiplyScalar(0.5).sub(p0.position); if(labelDir.length() > 0){ labelDir.z = 0; labelDir.normalize(); const labelVec = labelDir.clone().multiplyScalar(r); const labelPos = p0.position.clone().add(labelVec); azimuth.label.position.copy(labelPos); } azimuth.label.setText(txtDegrees); let distance = azimuth.label.position.distanceTo(camera.position); let pr = Utils.projectedRadius(1, camera, distance, width, height); let scale = (70 / pr); azimuth.label.scale.set(scale, scale, scale); } class ProfileTool extends EventDispatcher$1 { constructor (viewer) { super(); this.viewer = viewer; this.renderer = viewer.renderer; this.addEventListener('start_inserting_profile', e => { this.viewer.dispatchEvent({ type: 'cancel_insertions' }); }); this.scene = new Scene(); this.scene.name = 'scene_profile'; this.light = new PointLight(0xffffff, 1.0); this.scene.add(this.light); this.viewer.inputHandler.registerInteractiveScene(this.scene); this.onRemove = e => this.scene.remove(e.profile); this.onAdd = e => this.scene.add(e.profile); for(let profile of viewer.scene.profiles){ this.onAdd({profile: profile}); } viewer.addEventListener("update", this.update.bind(this)); viewer.addEventListener("render.pass.perspective_overlay", this.render.bind(this)); viewer.addEventListener("scene_changed", this.onSceneChange.bind(this)); viewer.scene.addEventListener('profile_added', this.onAdd); viewer.scene.addEventListener('profile_removed', this.onRemove); } onSceneChange(e){ if(e.oldScene){ e.oldScene.removeEventListeners('profile_added', this.onAdd); e.oldScene.removeEventListeners('profile_removed', this.onRemove); } e.scene.addEventListener('profile_added', this.onAdd); e.scene.addEventListener('profile_removed', this.onRemove); } startInsertion (args = {}) { let domElement = this.viewer.renderer.domElement; let profile = new Profile(); profile.name = args.name || 'Profile'; this.dispatchEvent({ type: 'start_inserting_profile', profile: profile }); this.scene.add(profile); let cancel = { callback: null }; let insertionCallback = (e) => { if(e.button === MOUSE.LEFT){ if(profile.points.length <= 1){ let camera = this.viewer.scene.getActiveCamera(); let distance = camera.position.distanceTo(profile.points[0]); let clientSize = this.viewer.renderer.getSize(new Vector2()); let pr = Utils.projectedRadius(1, camera, distance, clientSize.width, clientSize.height); let width = (10 / pr); profile.setWidth(width); } profile.addMarker(profile.points[profile.points.length - 1].clone()); this.viewer.inputHandler.startDragging( profile.spheres[profile.spheres.length - 1]); } else if (e.button === MOUSE.RIGHT) { cancel.callback(); } }; cancel.callback = e => { profile.removeMarker(profile.points.length - 1); domElement.removeEventListener('mouseup', insertionCallback, false); this.viewer.removeEventListener('cancel_insertions', cancel.callback); }; this.viewer.addEventListener('cancel_insertions', cancel.callback); domElement.addEventListener('mouseup', insertionCallback, false); profile.addMarker(new Vector3(0, 0, 0)); this.viewer.inputHandler.startDragging( profile.spheres[profile.spheres.length - 1]); this.viewer.scene.addProfile(profile); return profile; } update(){ let camera = this.viewer.scene.getActiveCamera(); let profiles = this.viewer.scene.profiles; let renderAreaSize = this.viewer.renderer.getSize(new Vector2()); let clientWidth = renderAreaSize.width; let clientHeight = renderAreaSize.height; this.light.position.copy(camera.position); // make size independant of distance for(let profile of profiles){ for(let sphere of profile.spheres){ let distance = camera.position.distanceTo(sphere.getWorldPosition(new Vector3())); let pr = Utils.projectedRadius(1, camera, distance, clientWidth, clientHeight); let scale = (15 / pr); sphere.scale.set(scale, scale, scale); } } } render(){ this.viewer.renderer.render(this.scene, this.viewer.scene.getActiveCamera()); } } class ScreenBoxSelectTool extends EventDispatcher$1{ constructor(viewer){ super(); this.viewer = viewer; this.scene = new Scene(); viewer.addEventListener("update", this.update.bind(this)); viewer.addEventListener("render.pass.perspective_overlay", this.render.bind(this)); viewer.addEventListener("scene_changed", this.onSceneChange.bind(this)); } onSceneChange(scene){ console.log("scene changed"); } startInsertion(){ let domElement = this.viewer.renderer.domElement; let volume = new BoxVolume(); volume.position.set(12345, 12345, 12345); volume.showVolumeLabel = false; volume.visible = false; volume.update(); this.viewer.scene.addVolume(volume); this.importance = 10; let selectionBox = $(`
`); $(domElement.parentElement).append(selectionBox); selectionBox.css("right", "10px"); selectionBox.css("bottom", "10px"); let drag = e =>{ volume.visible = true; let mStart = e.drag.start; let mEnd = e.drag.end; let box2D = new Box2(); box2D.expandByPoint(mStart); box2D.expandByPoint(mEnd); selectionBox.css("left", `${box2D.min.x}px`); selectionBox.css("top", `${box2D.min.y}px`); selectionBox.css("width", `${box2D.max.x - box2D.min.x}px`); selectionBox.css("height", `${box2D.max.y - box2D.min.y}px`); let camera = e.viewer.scene.getActiveCamera(); let size = e.viewer.renderer.getSize(new Vector2()); let frustumSize = new Vector2( camera.right - camera.left, camera.top - camera.bottom); let screenCentroid = new Vector2().addVectors(e.drag.end, e.drag.start).multiplyScalar(0.5); let ray = Utils.mouseToRay(screenCentroid, camera, size.width, size.height); let diff = new Vector2().subVectors(e.drag.end, e.drag.start); diff.divide(size).multiply(frustumSize); volume.position.copy(ray.origin); volume.up.copy(camera.up); volume.rotation.copy(camera.rotation); volume.scale.set(diff.x, diff.y, 1000 * 100); e.consume(); }; let drop = e => { this.importance = 0; $(selectionBox).remove(); this.viewer.inputHandler.deselectAll(); this.viewer.inputHandler.toggleSelection(volume); let camera = e.viewer.scene.getActiveCamera(); let size = e.viewer.renderer.getSize(new Vector2()); let screenCentroid = new Vector2().addVectors(e.drag.end, e.drag.start).multiplyScalar(0.5); let ray = Utils.mouseToRay(screenCentroid, camera, size.width, size.height); let line = new Line3(ray.origin, new Vector3().addVectors(ray.origin, ray.direction)); this.removeEventListener("drag", drag); this.removeEventListener("drop", drop); let allPointsNear = []; let allPointsFar = []; // TODO support more than one point cloud for(let pointcloud of this.viewer.scene.pointclouds){ if(!pointcloud.visible){ continue; } let volCam = camera.clone(); volCam.left = -volume.scale.x / 2; volCam.right = +volume.scale.x / 2; volCam.top = +volume.scale.y / 2; volCam.bottom = -volume.scale.y / 2; volCam.near = -volume.scale.z / 2; volCam.far = +volume.scale.z / 2; volCam.rotation.copy(volume.rotation); volCam.position.copy(volume.position); volCam.updateMatrix(); volCam.updateMatrixWorld(); volCam.updateProjectionMatrix(); volCam.matrixWorldInverse.copy(volCam.matrixWorld).invert(); let ray = new Ray(volCam.getWorldPosition(new Vector3()), volCam.getWorldDirection(new Vector3())); let rayInverse = new Ray( ray.origin.clone().add(ray.direction.clone().multiplyScalar(volume.scale.z)), ray.direction.clone().multiplyScalar(-1)); let pickerSettings = { width: 8, height: 8, pickWindowSize: 8, all: true, pickClipped: true, pointSizeType: PointSizeType.FIXED, pointSize: 1}; let pointsNear = pointcloud.pick(viewer, volCam, ray, pickerSettings); volCam.rotateX(Math.PI); volCam.updateMatrix(); volCam.updateMatrixWorld(); volCam.updateProjectionMatrix(); volCam.matrixWorldInverse.copy(volCam.matrixWorld).invert(); let pointsFar = pointcloud.pick(viewer, volCam, rayInverse, pickerSettings); allPointsNear.push(...pointsNear); allPointsFar.push(...pointsFar); } if(allPointsNear.length > 0 && allPointsFar.length > 0){ let viewLine = new Line3(ray.origin, new Vector3().addVectors(ray.origin, ray.direction)); let closestOnLine = allPointsNear.map(p => viewLine.closestPointToPoint(p.position, false, new Vector3())); let closest = closestOnLine.sort( (a, b) => ray.origin.distanceTo(a) - ray.origin.distanceTo(b))[0]; let farthestOnLine = allPointsFar.map(p => viewLine.closestPointToPoint(p.position, false, new Vector3())); let farthest = farthestOnLine.sort( (a, b) => ray.origin.distanceTo(b) - ray.origin.distanceTo(a))[0]; let distance = closest.distanceTo(farthest); let centroid = new Vector3().addVectors(closest, farthest).multiplyScalar(0.5); volume.scale.z = distance * 1.1; volume.position.copy(centroid); } volume.clip = true; }; this.addEventListener("drag", drag); this.addEventListener("drop", drop); viewer.inputHandler.addInputListener(this); return volume; } update(e){ //console.log(e.delta) } render(){ this.viewer.renderer.render(this.scene, this.viewer.scene.getActiveCamera()); } } class SpotLightHelper$1 extends Object3D{ constructor(light, color){ super(); this.light = light; this.color = color; //this.up.set(0, 0, 1); this.updateMatrix(); this.updateMatrixWorld(); { // SPHERE let sg = new SphereGeometry(1, 32, 32); let sm = new MeshNormalMaterial(); this.sphere = new Mesh(sg, sm); this.sphere.scale.set(0.5, 0.5, 0.5); this.add(this.sphere); } { // LINES let positions = new Float32Array([ +0, +0, +0, +0, +0, -1, +0, +0, +0, -1, -1, -1, +0, +0, +0, +1, -1, -1, +0, +0, +0, +1, +1, -1, +0, +0, +0, -1, +1, -1, -1, -1, -1, +1, -1, -1, +1, -1, -1, +1, +1, -1, +1, +1, -1, -1, +1, -1, -1, +1, -1, -1, -1, -1, ]); let geometry = new BufferGeometry(); geometry.setAttribute("position", new BufferAttribute(positions, 3)); let material = new LineBasicMaterial(); this.frustum = new LineSegments(geometry, material); this.add(this.frustum); } this.update(); } update(){ this.light.updateMatrix(); this.light.updateMatrixWorld(); let position = this.light.position; let target = new Vector3().addVectors( this.light.position, this.light.getWorldDirection(new Vector3()).multiplyScalar(-1)); let quat = new Quaternion().setFromRotationMatrix( new Matrix4().lookAt( position, target, new Vector3( 0, 0, 1 ) ) ); this.setRotationFromQuaternion(quat); this.position.copy(position); let coneLength = (this.light.distance > 0) ? this.light.distance : 1000; let coneWidth = coneLength * Math.tan( this.light.angle * 0.5 ); this.frustum.scale.set(coneWidth, coneWidth, coneLength); } } //问题:如何转换到世界坐标?(缩放方向有bug。) //add------------------------------------- const OpaWhenNotSelect = 0.6; const ScaleRatio = 3; const OutlineColor = 0x666666; //---------------------------------------- const hideFocusHandles = true;//add class TransformationTool extends EventDispatcher{ constructor(viewer ) { super(); this.viewer = viewer; this.modesEnabled = {};//add this.style = 'singleMode'; //建mesh时按照singleModes建的 this.scene = new Scene(); this.selection = []; this.pivot = new Vector3(); this.dragging = false; this.showPickVolumes = false; this.viewer.inputHandler.registerInteractiveScene(this.scene); this.viewer.inputHandler.addEventListener('selection_changed', (e) => { /* for(let selected of this.selection){ //若不删除,应该会穿过选中的物体选到别的而不是取消选择 this.viewer.inputHandler.blacklist.delete(selected); } */ this.selection = e.selection; /* for(let selected of this.selection){ this.viewer.inputHandler.blacklist.add(selected); } */ }); this.viewer.addEventListener('global_touchstart',(e)=>{ //add this.update(); }); this.viewer.addEventListener('global_mousemove',(e)=>{ //add this.onPointerMove(); }); let red = Potree.config.axis.x.color; let green = Potree.config.axis.y.color; let blue = Potree.config.axis.z.color; let white = Potree.config.axis.xyz.color; this.activeHandle = null; this.scaleHandles = { "scale.x+": {name: "scale.x+", node: new Object3D(), color: red, alignment: [+1, +0, +0]}, "scale.x-": {name: "scale.x-", node: new Object3D(), color: red, alignment: [-1, +0, +0]}, "scale.y+": {name: "scale.y+", node: new Object3D(), color: green, alignment: [+0, +1, +0]}, "scale.y-": {name: "scale.y-", node: new Object3D(), color: green, alignment: [+0, -1, +0]}, "scale.z+": {name: "scale.z+", node: new Object3D(), color: blue, alignment: [+0, +0, +1]}, "scale.z-": {name: "scale.z-", node: new Object3D(), color: blue, alignment: [+0, +0, -1]}, "lines": {name:'lines', node: new Object3D(), dontScale:true }//add }; this.focusHandles = { "focus.x+": {name: "focus.x+", node: new Object3D(), color: red, alignment: [+1, +0, +0]}, "focus.x-": {name: "focus.x-", node: new Object3D(), color: red, alignment: [-1, +0, +0]}, "focus.y+": {name: "focus.y+", node: new Object3D(), color: green, alignment: [+0, +1, +0]}, "focus.y-": {name: "focus.y-", node: new Object3D(), color: green, alignment: [+0, -1, +0]}, "focus.z+": {name: "focus.z+", node: new Object3D(), color: blue, alignment: [+0, +0, +1]}, "focus.z-": {name: "focus.z-", node: new Object3D(), color: blue, alignment: [+0, +0, -1]}, }; this.translationHandles = { "translation.x": {name: "translation.x", node: new Object3D(), color: red, alignment: [1, 0, 0]}, "translation.y": {name: "translation.y", node: new Object3D(), color: green, alignment: [0, 1, 0]}, "translation.z": {name: "translation.z", node: new Object3D(), color: blue, alignment: [0, 0, 1]}, //add 'translation.xyz':{name: "translation.xyz", node: new Object3D(),color: white, alignment: [0, 0, 0], alignment2: [1, 1, 1]}, 'translation.plane.xy':{name: "translation.plane.xy", node: new Object3D(),color: blue,alignment: [0, 0, 1], alignment2: [1, 1, 0]}, 'translation.plane.yz':{name: "translation.plane.yz", node: new Object3D(),color: red, alignment: [1, 0, 0], alignment2: [0, 1, 1]}, 'translation.plane.xz':{name: "translation.plane.xz", node: new Object3D(),color: green, alignment: [0, 1, 0], alignment2: [1, 0, 1]}, }; this.rotationHandles = { "rotation.x": {name: "rotation.x", node: new Object3D(), color: red, alignment: [1, 0, 0]}, "rotation.y": {name: "rotation.y", node: new Object3D(), color: green, alignment: [0, 1, 0]}, "rotation.z": {name: "rotation.z", node: new Object3D(), color: blue, alignment: [0, 0, 1]}, }; this.handles = Object.assign({}, this.scaleHandles, hideFocusHandles?{}:this.focusHandles, this.translationHandles, this.rotationHandles); this.pickVolumes = []; this.initializeScaleHandles(); this.initializeFocusHandles(); this.initializeTranslationHandles(); this.initializeRotationHandles(); let boxFrameGeometry = new Geometry(); { // bottom boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.5)); // top boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.5)); // sides boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, 0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(0.5, 0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, -0.5, -0.5)); boxFrameGeometry.vertices.push(new Vector3(-0.5, 0.5, -0.5)); } this.frame = new LineSegments(boxFrameGeometry, new LineBasicMaterial({color: 0xffff00})); this.scene.add(this.frame); //------------------add----------------------- this.setModeEnable(['scale','translation','rotation']); Potree.Utils.setObjectLayers(this.scene, 'transformationTool' ); this.scene.traverse(e=>{ e.pickDontCheckDis = true; //pick时不需要识别是否在点云之上 }); { let exist = (object)=>{//是否没被删除(暂时不考虑换了parent) while(object.parent){ object = object.parent; } if(object instanceof Scene){ return true } }; this.history = new History({ //也可以写到全局,但需要加个判断物品是否存在的函数 applyData: (data)=>{ if(exist(data.object)){//viewer.scene.volumes.includes(data.box) //或许还要识别是否matrixAuto data.matrix.decompose( data.object.position, data.object.quaternion, data.object.scale ); this.dispatchEvent('changeByHistory'); viewer.dispatchEvent('content_changed'); return true } } , getData:(data)=>{ return data } }); this.addEventListener('transformed', (e)=>{ let object = viewer.transformationTool.selection[0]; this.history.beforeChange({object, matrix:e.matrixBefore.clone()} ); this.viewer.dispatchEvent('content_changed'); }); this.addEventListener('stopDrag', (e)=>{ let object = viewer.transformationTool.selection[0]; object && this.history.afterChange({object, matrix:object.matrix.clone()} ); }); /* viewer.inputHandler.addEventListener('keydown', (e)=>{ if(e.keyCode == 90 && e.event.ctrlKey){//Z this.history.undo() }else if(e.keyCode == 89 && e.event.ctrlKey){//Y this.history.redo() } }) */ } } setModeEnable(enableModes=[] ){//xzw add let length=0; ['translation','scale','rotation'].forEach(mode=>{ let handels = this[mode + 'Handles']; let enable = enableModes.includes(mode); for(let o in handels){ Potree.Utils.updateVisible(handels[o].node,'modeForce', !!enable); } this.modesEnabled[mode] = !!enable; enable && length++; }); if(this.style == 'singleMode' && length > 1){ this.changeStyle('mixedModes'); }else if(this.style == 'mixedModes' && length == 1){ this.changeStyle('singleMode'); } } changeStyle(style){// 切换单个mode & 多个mode混合 风格(因多个混合比较拥挤,需要做调整) let s1 = style == 'singleMode' ? 30 : 25; let s2 = style == 'singleMode' ? 15 : 10 ;['x','y','z'].forEach(axis=>{ this.translationHandles['translation.'+axis].node.children.forEach(mesh=>{ if(mesh.name.includes('arrow')){ Potree.Utils.updateVisible(mesh, 'modeStyle', style == 'singleMode'); }else if(mesh.name.includes('handle')){ mesh.material.lineWidth = style == 'singleMode' ? 5 : 7; } }); this.rotationHandles['rotation.'+axis].translateNode.scale.set(s1,s1,s1); }); ['xy','yz','xz'].forEach(axis=>{ let handle = this.translationHandles['translation.plane.'+axis]; handle.node.children[0].scale.set(s2,s2,s2); handle.node.children[0].position.fromArray(handle.alignment2).multiplyScalar(s2*1.5); }); Potree.Utils.updateVisible(this.scaleHandles['lines'].node, 'modeStyle', style == 'singleMode'); this.style = style; } initializeTranslationHandles(){//大改 let boxGeometry = new BoxGeometry(1, 1, 1); let length = 100 , arrowRadius = 4, arrowHeight = 10; let arrowGeometry = new CylinderBufferGeometry( 0, arrowRadius, arrowHeight, 12, 1, false );//add 箭头朝(0,1,0) let arrowInitialQua = new Quaternion().setFromAxisAngle(new Vector3(1,0,0), -Math.PI/2 );//先将qua旋转到朝向0,0,-1, 因为一般quaternion不设置时默认表示朝向0,0,-1, let octahedronGeometry = new OctahedronBufferGeometry( 5, 0 ); let planeGeometry = new PlaneBufferGeometry( 1, 1 ); for(let handleName of Object.keys(this.translationHandles)){ let handle = this.handles[handleName]; let node = handle.node; this.scene.add(node); node.name = handleName;//add let alignment = new Vector3(...handle.alignment); let geometry , mesh, pickVolume, meshScale, pickScale, rotation, position, lookAtPoint, hasPick = true, meshPickable = false, renderOrder; let matProp = { color: handle.color, opacity: OpaWhenNotSelect, transparent: true, side: handleName.includes('plane') ? DoubleSide : FrontSide }; let material = new MeshBasicMaterial(matProp); let pickMaterial = new MeshNormalMaterial({ opacity: 0.2, transparent: true, visible: this.showPickVolumes }); if(handleName.includes('xyz') ){ geometry = octahedronGeometry; meshPickable = true; renderOrder = 12; }else if(handleName.includes('plane') ){ geometry = planeGeometry; meshPickable = true; position = new Vector3(...handle.alignment2); lookAtPoint = alignment; if(handleName.includes('xy') ) { }else if(handleName.includes('yz')){ }else if(handleName.includes('xz')){ } }else { geometry = boxGeometry; let point = new Vector3(0,0,length/2);//new THREE.Vector3().copy(alignment).multiplyScalar(length/2) mesh = LineDraw.createFatLine([point, point.clone().negate()],{lineWidth:6, mat : new LineDraw.createFatLineMat(matProp)}); lookAtPoint = alignment; renderOrder = 10; pickScale = new Vector3(4, 4, length+arrowHeight*2); //pickScale = new THREE.Vector3(4, 4, 1.2) { let arrow = new Mesh(arrowGeometry, material); arrow.name = `${handleName}.arrow`; arrow.renderOrder = 11; arrow.position.set( ...handle.alignment).multiplyScalar(length/2+arrowHeight/2); arrow.lookAt(0,0,0); node.add(arrow); let arrow2 = arrow.clone(); arrow2.position.negate(); arrow2.lookAt(0,0,0); node.add(arrow2); arrow.quaternion.multiply(arrowInitialQua); //乘上初始旋转 arrow2.quaternion.multiply(arrowInitialQua); } } mesh || (mesh = new Mesh(geometry,material)); mesh.name = `${handleName}.handle`; node.add(mesh); renderOrder && (mesh.renderOrder = renderOrder); meshScale && mesh.scale.copy(meshScale); rotation && mesh.rotation.copy(rotation); lookAtPoint && mesh.lookAt(lookAtPoint); position && mesh.position.copy(position); if(!meshPickable && hasPick){ pickVolume = new Mesh(geometry, pickMaterial); pickScale && pickVolume.scale.copy(pickScale); }else if(hasPick && meshPickable){ pickVolume = mesh; } if(pickVolume){ if(mesh != pickVolume){ mesh.add(pickVolume); pickVolume.name = `${handleName}.pick_volume`; }else { pickVolume.name += ' & pick_volume'; } pickVolume.handle = handleName; this.pickVolumes.push(pickVolume); } node.setOpacity = (target) => { if(handleName.includes('plane')){ let more = 1.5; //减掉更多,使min更低,max还是1 target = 1 - (1-target)*more; } let opacity = {x: material.opacity}; let t = new TWEEN.Tween(opacity).to({x: target}, 0); t.onUpdate(() => { mesh.visible = opacity.x > 0; pickVolume && (pickVolume.visible = opacity.x > 0); material.opacity = opacity.x; mesh.material.opacity = opacity.x; //outlineMaterial.opacity = opacity.x; pickMaterial.opacity = opacity.x * 0.5; }); t.start(); }; pickVolume.addEventListener("drag", (e) => {this.dragTranslationHandle(e);}); pickVolume.addEventListener("drop", (e) => {this.dropTranslationHandle(e);}); } } initializeScaleHandles(){ let sgSphere = new SphereGeometry(1, 32, 32); let sgLowPolySphere = new SphereGeometry(1, 16, 16); for(let handleName of Object.keys(this.scaleHandles)){ let handle = this.scaleHandles[handleName]; let node = handle.node; node.name = handleName;//add this.scene.add(node); if(handleName == 'lines'){ //add ['x','y','z'].forEach(axis=>{ let handle1_ = this.scaleHandles['scale.'+axis+'+']; let handle2_ = this.scaleHandles['scale.'+axis+'-']; let line = LineDraw.createFatLine([ new Vector3().fromArray(handle1_.alignment).multiplyScalar(0.5), new Vector3().fromArray(handle2_.alignment).multiplyScalar(0.5) ], {color:handle1_.color, lineWidth:4} // , dontAlwaysSeen:true ); node.add(line); }); node.setOpacity = (opacity) => { opacity *= 0.6; node.children.forEach(e=>e.material.opacity = opacity); }; continue; } node.position.set(...handle.alignment).multiplyScalar(0.5); let material = new MeshBasicMaterial({ color: handle.color, side:DoubleSide,//xzw add opacity: OpaWhenNotSelect, transparent: true }); let outlineMaterial = new MeshBasicMaterial({ color: OutlineColor, side: BackSide, opacity: OpaWhenNotSelect, transparent: true}); let pickMaterial = new MeshNormalMaterial({ opacity: 0.2, transparent: true, side:DoubleSide,//xzw add for orthoCam, 缩小画面时因球体放大导致到相机背面去了而看不到球体正面 visible: this.showPickVolumes}); let sphere = new Mesh(sgSphere, material); sphere.scale.set(5, 5, 5 ); sphere.name = `${handleName}.handle`; node.add(sphere); sphere.renderOrder = 10; /* let outline = new THREE.Mesh(sgSphere, outlineMaterial); outline.scale.set(1.1, 1.1, 1.1); outline.name = `${handleName}.outline`; sphere.add(outline); */ let pickSphere = new Mesh(sgLowPolySphere, pickMaterial); pickSphere.name = `${handleName}.pick_volume`; pickSphere.scale.set(1.5, 1.5, 1.5); sphere.add(pickSphere); pickSphere.handle = handleName; this.pickVolumes.push(pickSphere); node.setOpacity = (target) => { let opacity = {x: material.opacity}; let t = new TWEEN.Tween(opacity).to({x: target}, 0); //xzw改 原100毫秒,因为太慢容易选错 t.onUpdate(() => { sphere.visible = opacity.x > 0; pickSphere.visible = opacity.x > 0; material.opacity = opacity.x; outlineMaterial.opacity = opacity.x; pickSphere.material.opacity = opacity.x * 0.5; }); t.start(); }; pickSphere.addEventListener("drag", (e) => this.dragScaleHandle(e)); pickSphere.addEventListener("drop", (e) => this.dropScaleHandle(e)); pickSphere.addEventListener("mouseover", e => { //node.setOpacity(1); }); pickSphere.addEventListener("click", e => { //e.consume(); }); pickSphere.addEventListener("mouseleave", e => { //node.setOpacity(OpaWhenNotSelect); }); } } initializeRotationHandles(){ let boldAdjust = 2.3; let torusGeometry = new TorusGeometry(1.3, boldAdjust * 0.015, 8, 64, Math.PI / 2); //let outlineGeometry = new THREE.TorusGeometry(1, boldAdjust * 0.018, 8, 64, Math.PI / 2); let pickGeometry = new TorusGeometry(1.3, boldAdjust * 0.06, 6, 4, Math.PI / 2); for(let handleName of Object.keys(this.rotationHandles)){ let handle = this.handles[handleName]; let node = handle.node; this.scene.add(node); node.name = handleName;//add let material = new MeshBasicMaterial({ color: handle.color, opacity: OpaWhenNotSelect, transparent: true }); /* let outlineMaterial = new THREE.MeshBasicMaterial({ color: OutlineColor, side: THREE.BackSide, opacity: OpaWhenNotSelect, transparent: true }); */ let pickMaterial = new MeshNormalMaterial({ opacity: 0.2, transparent: true, visible: this.showPickVolumes }); let box = new Mesh(torusGeometry, material); box.name = `${handleName}.handle`; box.scale.set(30, 30, 30); box.lookAt(new Vector3(...handle.alignment)); node.add(box); handle.translateNode = box; /* let outline = new THREE.Mesh(outlineGeometry, outlineMaterial); outline.name = `${handleName}.outline`; outline.scale.set(1, 1, 1); outline.renderOrder = 0; box.add(outline); */ let pickVolume = new Mesh(pickGeometry, pickMaterial); pickVolume.name = `${handleName}.pick_volume`; pickVolume.scale.set(1, 1, 1); pickVolume.handle = handleName; box.add(pickVolume); this.pickVolumes.push(pickVolume); node.setOpacity = (target) => { let opacity = {x: material.opacity}; let t = new TWEEN.Tween(opacity).to({x: target}, 0); t.onUpdate(() => { box.visible = opacity.x > 0; pickVolume.visible = opacity.x > 0; material.opacity = opacity.x; //outlineMaterial.opacity = opacity.x; pickMaterial.opacity = opacity.x * 0.5; }); t.start(); }; //pickVolume.addEventListener("mouseover", (e) => { // //let a = this.viewer.scene.getActiveCamera().getWorldDirection(new THREE.Vector3()).dot(pickVolume.getWorldDirection(new THREE.Vector3())); // console.log(pickVolume.getWorldDirection(new THREE.Vector3())); //}); pickVolume.addEventListener("drag", (e) => {this.dragRotationHandle(e);}); pickVolume.addEventListener("drop", (e) => {this.dropRotationHandle(e);}); } } initializeFocusHandles(){ if(hideFocusHandles)return//add //let sgBox = new THREE.BoxGeometry(1, 1, 1); let sgPlane = new PlaneGeometry(4, 4, 1, 1); let sgLowPolySphere = new SphereGeometry(1, 16, 16); let texture = new TextureLoader().load(`${exports.resourcePath}/icons/eye_2.png`); for(let handleName of Object.keys(this.focusHandles)){ let handle = this.focusHandles[handleName]; let node = handle.node; this.scene.add(node); let align = handle.alignment; node.name = handleName;//add //node.lookAt(new THREE.Vector3().addVectors(node.position, new THREE.Vector3(...align))); node.lookAt(new Vector3(...align)); let off = 0.8; if(align[0] === 1){ node.position.set(1, off, -off).multiplyScalar(0.5); node.rotation.z = Math.PI / 2; }else if(align[0] === -1){ node.position.set(-1, -off, -off).multiplyScalar(0.5); node.rotation.z = Math.PI / 2; }else if(align[1] === 1){ node.position.set(-off, 1, -off).multiplyScalar(0.5); node.rotation.set(Math.PI / 2, Math.PI, 0.0); }else if(align[1] === -1){ node.position.set(off, -1, -off).multiplyScalar(0.5); node.rotation.set(Math.PI / 2, 0.0, 0.0); }else if(align[2] === 1){ node.position.set(off, off, 1).multiplyScalar(0.5); }else if(align[2] === -1){ node.position.set(-off, off, -1).multiplyScalar(0.5); } let material = new MeshBasicMaterial({ color: handle.color, opacity: 0, transparent: true, map: texture }); //let outlineMaterial = new THREE.MeshBasicMaterial({ // color: 0x000000, // side: THREE.BackSide, // opacity: 0, // transparent: true}); let pickMaterial = new MeshNormalMaterial({ //opacity: 0, transparent: true, visible: this.showPickVolumes}); let box = new Mesh(sgPlane, material); box.name = `${handleName}.handle`; box.scale.set(1.5, 1.5, 1.5); box.position.set(0, 0, 0); box.visible = false; node.add(box); //handle.focusNode = box; //let outline = new THREE.Mesh(sgPlane, outlineMaterial); //outline.scale.set(1.4, 1.4, 1.4); //outline.name = `${handleName}.outline`; //box.add(outline); let pickSphere = new Mesh(sgLowPolySphere, pickMaterial); pickSphere.name = `${handleName}.pick_volume`; pickSphere.scale.set(2, 2, 2); box.add(pickSphere); pickSphere.handle = handleName; this.pickVolumes.push(pickSphere); node.setOpacity = (target) => { let opacity = {x: material.opacity}; let t = new TWEEN.Tween(opacity).to({x: target}, 0); t.onUpdate(() => { pickSphere.visible = opacity.x > 0; box.visible = opacity.x > 0; material.opacity = opacity.x; //outlineMaterial.opacity = opacity.x; pickSphere.material.opacity = opacity.x * 0.5; }); t.start(); }; //pickSphere.addEventListener("drag", e => {}); pickSphere.addEventListener("mouseup", e => { //e.consume(); }); pickSphere.addEventListener("mousedown", e => { //e.consume(); }); pickSphere.addEventListener("click", e => { //e.consume(); let selected = this.selection[0]; let maxScale = Math.max(...selected.scale.toArray()); let minScale = Math.min(...selected.scale.toArray()); let handleLength = Math.abs(selected.scale.dot(new Vector3(...handle.alignment))); let alignment = new Vector3(...handle.alignment).multiplyScalar(2 * maxScale / handleLength); alignment.applyMatrix4(selected.matrixWorld); let newCamPos = alignment; let newCamTarget = selected.getWorldPosition(new Vector3()); Utils.moveTo(this.viewer.scene, newCamPos, newCamTarget); }); pickSphere.addEventListener("mouseover", e => { //box.setOpacity(1); }); pickSphere.addEventListener("mouseleave", e => { //box.setOpacity(OpaWhenNotSelect); }); } } dragRotationHandle(e){ let drag = e.drag; let handle = this.activeHandle; let camera = this.viewer.mainViewport.camera;//this.viewer.scene.getActiveCamera(); if(!handle || !handle.name.includes('rotation') ){ return }; let localNormal = new Vector3(...handle.alignment); let n = new Vector3(); n.copy(new Vector4(...localNormal.toArray(), 0).applyMatrix4(handle.node.matrixWorld)); n.normalize(); if (!drag.intersectionStart){ //this.viewer.scene.scene.remove(this.debug); //this.debug = new THREE.Object3D(); //this.viewer.scene.scene.add(this.debug); //Utils.debugSphere(this.debug, drag.location, 3, 0xaaaaaa); //let debugEnd = drag.location.clone().add(n.clone().multiplyScalar(20)); //Utils.debugLine(this.debug, drag.location, debugEnd, 0xff0000); drag.intersectionStart = drag.location; drag.objectStart = drag.object.getWorldPosition(new Vector3()); drag.handle = handle; let plane = new Plane().setFromNormalAndCoplanarPoint(n, drag.intersectionStart); drag.dragPlane = plane; drag.pivot = drag.intersectionStart; }else { handle = drag.handle; } if(!drag.dragPlane)return//xzw add 因有时候没有 this.dragging = true; let pointer = this.viewer.inputHandler.pointer; let domElement = this.viewer.renderer.domElement; let ray = Utils.mouseToRay(pointer, camera, domElement.clientWidth, domElement.clientHeight); let I = ray.intersectPlane(drag.dragPlane, new Vector3()); if (I) { let center = this.scene.getWorldPosition(new Vector3()); //bounding中心 let from = drag.pivot; let to = I; let v1 = from.clone().sub(center).normalize(); let v2 = to.clone().sub(center).normalize(); let angle = Math.acos(v1.dot(v2)); let sign = Math.sign(v1.cross(v2).dot(n)); angle = angle * sign; if (Number.isNaN(angle)) { return; } let matrixBefore = this.selection[0].matrix.clone(); let normal = new Vector3(...handle.alignment); for (let selection of this.selection) { //直接修改matrix,使整体绕boundingBox中心旋转。 xzw let quaternion = selection.quaternion.clone(); let diffQua = new Quaternion().setFromAxisAngle( normal, angle );//变化量,参考 selection.rotateOnAxis(normal, angle); let moveToZero = new Matrix4().setPosition(center.clone().negate()); //先将boundingBox中心(整体)移动到原点 let rotM = new Matrix4().makeRotationFromQuaternion(quaternion.clone().inverse().premultiply(diffQua).premultiply(quaternion) );//再旋转。根据selection.rotateOnAxis,应该是旧的qua 右乘 diffQua,所以先用invert消掉旧的qua let moveBack = new Matrix4().setPosition(center.clone());//移动回去,使boundingBox中心位置还原 selection.matrix.premultiply(moveToZero).premultiply(rotM).premultiply(moveBack); selection.matrix.decompose(selection.position, selection.quaternion, selection.scale);//记录 (scale基本不变) selection.updateMatrixWorld();//xzw add 保险起见立即update selection.dispatchEvent({ type: "orientation_changed", object: selection }); selection.dispatchEvent({//当boundingBox中心不在原点时 type: "position_changed", object: selection }); } this.dispatchEvent({type:'transformed', changeType: ['orientation'], matrixBefore });//add drag.pivot = I; } } dropRotationHandle(e){ this.dragging = false; this.setActiveHandle(null); this.dispatchEvent({type:'stopDrag', handle:'rotation'});//add } dragTranslationHandle(e){//---大改,参考transformControls,为了加上xyz xy yz xz 这四个方向的变换。 (但感觉好像plane上有丢丢延迟?是因为drag延迟还是worldmatrix没更新) let drag = e.drag; let handle = this.activeHandle; let camera = this.viewer.mainViewport.camera;//this.viewer.scene.getActiveCamera(); if(handle && handle.name.includes('translation') && this.selection[0]){ let posWorld = this.scene.getWorldPosition(new Vector3());//bounding中心 if(!drag.intersectionStart){ drag.intersectionStart = drag.location; drag.worldPositionStart = this.selection[0].getWorldPosition(new Vector3()); drag.objectQua = this.selection[0].quaternion.clone();//不考虑父级 drag.objectQuaInv = drag.objectQua.clone().invert(); this.dragging = true; } if(drag.intersectionStart){ let pointer = this.viewer.inputHandler.pointer; let ray = Utils.mouseToRay(pointer, camera); //方向滑动所在面 let normal; let axisName = handle.name.split('.').pop(); if(axisName == 'xyz'){ // 平行于屏幕滑动 normal = viewer.mainViewport.view.direction; }else { var alignVector = new Vector3(); normal = new Vector3; let unitX = new Vector3( 1, 0, 0 ).applyQuaternion(drag.objectQua); let unitY = new Vector3( 0, 1, 0 ).applyQuaternion(drag.objectQua); let unitZ = new Vector3( 0, 0, 1 ).applyQuaternion(drag.objectQua); switch (axisName) { case 'x': alignVector.copy( viewer.mainViewport.view.direction ).cross( unitX ); normal.copy( unitX ).cross( alignVector ); break; case 'y': alignVector.copy( viewer.mainViewport.view.direction ).cross( unitY ); normal.copy( unitY ).cross( alignVector ); break; case 'z': alignVector.copy( viewer.mainViewport.view.direction ).cross( unitZ ); normal.copy( unitZ ).cross( alignVector ); break; case 'xy': normal.copy( unitZ ); break; case 'yz': normal.copy( unitX ); break; case 'xz': normal.copy( unitY ); break; }//参考transformControls.使跟手 } drag.dragPlane = new Plane().setFromNormalAndCoplanarPoint(normal, posWorld/* drag.worldPositionStart */);//过center的与视线垂直的平面 let I = ray.intersectPlane(drag.dragPlane, new Vector3()); if (I) { let offset = new Vector3().subVectors( I , drag.worldPositionStart ); //let offset = new THREE.Vector3().subVectors(iOnLine, drag.worldPositionStart); if(!drag.pointStart){ drag.pointStart = offset; }else { drag.pointEnd = offset; let diff = new Vector3().subVectors(drag.pointEnd, drag.pointStart); diff.applyQuaternion(drag.objectQuaInv); // 得到在该物体local空间上的offset if(!handle.name.includes('x')) diff.x = 0; if(!handle.name.includes('y')) diff.y = 0; if(!handle.name.includes('z')) diff.z = 0; //恢复为world offset diff.applyQuaternion(drag.objectQua); //------------- let matrixBefore = this.selection[0].matrix.clone(); this.selection[0].position.copy( diff ).add( drag.worldPositionStart ); for (let selection of this.selection) { selection.updateMatrixWorld();//xzw add 保险起见立即update selection.dispatchEvent({ type: "position_changed", object: selection }); } this.dispatchEvent({type:'transformed', changeType: ['position'], matrixBefore});//add } } } } } dropTranslationHandle(e){ this.dragging = false; this.setActiveHandle(null); this.dispatchEvent({type:'stopDrag', handle:'translation'});//add } dropScaleHandle(e){ this.dragging = false; this.setActiveHandle(null); this.dispatchEvent({type:'stopDrag', handle:'scale'});//add } dragScaleHandle(e){ let drag = e.drag; let handle = this.activeHandle; if(!handle || !handle.name.includes('scale'))return let camera = this.viewer.mainViewport.camera;//this.viewer.scene.getActiveCamera(); if(!drag.intersectionStart){ drag.intersectionStart = drag.location; drag.objectStart = drag.object.getWorldPosition(new Vector3()); drag.handle = handle; let start = drag.intersectionStart; let dir = new Vector4(...handle.alignment, 0).applyMatrix4(this.scene.matrixWorld); let end = new Vector3().addVectors(start, dir); let line = new Line3(start.clone(), end.clone()); drag.line = line; let normal; if(camera.type == 'OrthographicCamera'){//xzw add normal = new Vector3(0,0,-1).applyQuaternion(camera.quaternion); }else { let camOnLine = line.closestPointToPoint(camera.position, false, new Vector3()); normal = new Vector3().subVectors(camera.position, camOnLine); } let plane = new Plane().setFromNormalAndCoplanarPoint(normal, drag.intersectionStart); //过轴线的一个能铺满屏幕的平面 drag.dragPlane = plane; drag.pivot = drag.intersectionStart; //Utils.debugSphere(viewer.scene.scene, drag.pivot, 0.05); }else { handle = drag.handle; } this.dragging = true; if(drag.dragPlane){//xzw add 因有时候没有 let pointer = this.viewer.inputHandler.pointer; let domElement = this.viewer.renderer.domElement; let ray = Utils.mouseToRay(pointer, camera, domElement.clientWidth, domElement.clientHeight); let I = ray.intersectPlane(drag.dragPlane, new Vector3()); if (I) { let iOnLine = drag.line.closestPointToPoint(I, false, new Vector3()); let direction = handle.alignment.reduce( (a, v) => a + v, 0); let toObjectSpace = this.selection[0].matrixWorld.clone().invert(); let iOnLineOS = iOnLine.clone().applyMatrix4(toObjectSpace); let pivotOS = drag.pivot.clone().applyMatrix4(toObjectSpace); let diffOS = new Vector3().subVectors(iOnLineOS, pivotOS); let dragDirectionOS = diffOS.clone().normalize(); if(iOnLine.distanceTo(drag.pivot) === 0){ dragDirectionOS.set(0, 0, 0); } let dragDirection = dragDirectionOS.dot(new Vector3(...handle.alignment)); let alignment = new Vector3(...handle.alignment); let diff = new Vector3().subVectors(iOnLine, drag.pivot); let diffScale = alignment.clone().multiplyScalar(diff.length() * direction * dragDirection); //let diffPosition = diff.clone().multiplyScalar(0.5); //这个仅当bound包含原点才准确。 let matrixBefore = this.selection[0].matrix.clone(); for (let selection of this.selection) { //xzw 改:否则不跟手 let {min,max} = selection.boundingBox; let fixPoint = alignment.x != 0 ? (alignment.x > 0 ? min : max).clone().setY(0).setZ(0) : //要保证另一边不能移动 alignment.y != 0 ? (alignment.y > 0 ? min : max).clone().setX(0).setZ(0) : (alignment.z > 0 ? min : max).clone().setX(0).setY(0); let fixPointBefore = fixPoint.clone().applyMatrix4(selection.matrixWorld); let size = selection.boundingBox.getSize(new Vector3); let diffScale_ = diffScale.clone().divide(size); size.x == 0 && (diffScale_.x = 0); //add 若为0,不改此轴大小 size.y == 0 && (diffScale_.y = 0); size.z == 0 && (diffScale_.z = 0); selection.scale.add(diffScale_); selection.scale.max(new Vector3(0.1,0.1,0.1)); //selection.position.add(diffPosition); selection.updateMatrixWorld(); let fixPointAfter = fixPoint.clone().applyMatrix4(this.selection[0].matrixWorld); selection.position.sub(new Vector3().subVectors(fixPointAfter,fixPointBefore));//保证另一边不能移动所需要的位移 selection.updateMatrixWorld();//xzw add 保险起见立即update selection.dispatchEvent({ type: "position_changed", object: selection }); selection.dispatchEvent({ type: "scale_changed", object: selection }); } this.dispatchEvent({type:'transformed', changeType: ['position','scale'], matrixBefore});//add drag.pivot.copy(iOnLine); //Utils.debugSphere(viewer.scene.scene, drag.pivot, 0.05); } } } setActiveHandle(handle){ if(this.dragging){ return; } if(this.activeHandle === handle){ return; } this.activeHandle = handle; if(handle === null){ for(let handleName of Object.keys(this.handles)){ let handle = this.handles[handleName]; handle.node.setOpacity(0); } } viewer.dispatchEvent({type:'CursorChange', action: this.activeHandle ? 'add' : 'remove', name:'hoverTranHandle'}); if(!hideFocusHandles){ for(let handleName of Object.keys(this.focusHandles)){ let handle = this.focusHandles[handleName]; if(this.activeHandle === handle){ handle.node.setOpacity(1.0); }else { handle.node.setOpacity(OpaWhenNotSelect); } } } for(let handleName of Object.keys(this.translationHandles)){ let handle = this.translationHandles[handleName]; if(this.activeHandle === handle){ handle.node.setOpacity(1.0); }else { handle.node.setOpacity(OpaWhenNotSelect); } } for(let handleName of Object.keys(this.rotationHandles)){ let handle = this.rotationHandles[handleName]; //if(this.activeHandle === handle){ // handle.node.setOpacity(1.0); //}else{ // handle.node.setOpacity(OpaWhenNotSelect) //} handle.node.setOpacity(OpaWhenNotSelect); } for(let handleName of Object.keys(this.scaleHandles)){ let handle = this.scaleHandles[handleName]; if(this.activeHandle === handle){ handle.node.setOpacity(1.0); if(!hideFocusHandles){ let relatedFocusHandle = this.focusHandles[handle.name.replace("scale", "focus")]; let relatedFocusNode = relatedFocusHandle.node; relatedFocusNode.setOpacity(OpaWhenNotSelect); } for(let translationHandleName of Object.keys(this.translationHandles)){ let translationHandle = this.translationHandles[translationHandleName]; translationHandle.node.setOpacity(OpaWhenNotSelect); } //let relatedTranslationHandle = this.translationHandles[ // handle.name.replace("scale", "translation").replace(/[+-]/g, "")]; //let relatedTranslationNode = relatedTranslationHandle.node; //relatedTranslationNode.setOpacity(OpaWhenNotSelect); }else { handle.node.setOpacity(OpaWhenNotSelect); } } if(handle){ handle.node.setOpacity(1.0); } viewer.dispatchEvent('content_changed'); } update () { if(this.selection.length === 1){ this.scene.visible = true; this.scene.updateMatrix(); this.scene.updateMatrixWorld(); let selected = this.selection[0]; //selected.updateMatrixWorld();//add 否则scale的sphere抖动 let world = selected.matrixWorld; let camera = this.viewer.mainViewport.camera;//this.viewer.scene.getActiveCamera(); let domElement = this.viewer.renderer.domElement; let center = selected.boundingBox.getCenter(new Vector3()).clone().applyMatrix4(selected.matrixWorld); let scale = selected.boundingBox.getSize(new Vector3()).multiply(selected.scale); scale.max(new Vector3(.1,.1,.1)); //xzw add 如果是plane,没有厚度,会导致该tool消失 this.scene.scale.copy(scale); this.scene.position.copy(center); this.scene.rotation.copy(selected.rotation); //这里只考虑当前子级的scale rotation //如果是世界坐标 (缩放方向有bug。) /* let boundingBox = selected.boundingBox.clone().applyMatrix4(selected.matrixWorld); let center = boundingBox.getCenter(new THREE.Vector3()) this.scene.position.copy(center); this.scene.scale.copy(boundingBox.getSize(new THREE.Vector3())); */ this.scene.updateMatrixWorld(); { // adjust rotation handles if(!this.dragging){ if(this.modesEnabled.rotation || this.modesEnabled.translation){ let tWorld = this.scene.matrixWorld; let tObject = tWorld.clone().invert(); let camObjectPos = camera.getWorldPosition(new Vector3()).applyMatrix4(tObject); if(this.modesEnabled.translation){//add ['xy','yz','xz'].forEach(axis=>{ let handle = this.translationHandles["translation.plane."+axis]; let pos = handle.node.children[0].position; camObjectPos.x && (pos.x = Math.sign(camObjectPos.x) * Math.abs(pos.x)); camObjectPos.y && (pos.y = Math.sign(camObjectPos.y) * Math.abs(pos.y)); camObjectPos.z && (pos.z = Math.sign(camObjectPos.z) * Math.abs(pos.z)); }); } if(this.modesEnabled.rotation){ let above = camObjectPos.z > 0; let below = !above; let PI_HALF = Math.PI / 2; let x = this.rotationHandles["rotation.x"].node.rotation; let y = this.rotationHandles["rotation.y"].node.rotation; let z = this.rotationHandles["rotation.z"].node.rotation; x.order = "ZYX"; y.order = "ZYX"; if(above){ if(camObjectPos.x > 0 && camObjectPos.y > 0){ x.x = 1 * PI_HALF; y.y = 3 * PI_HALF; z.z = 0 * PI_HALF; }else if(camObjectPos.x < 0 && camObjectPos.y > 0){ x.x = 1 * PI_HALF; y.y = 2 * PI_HALF; z.z = 1 * PI_HALF; }else if(camObjectPos.x < 0 && camObjectPos.y < 0){ x.x = 2 * PI_HALF; y.y = 2 * PI_HALF; z.z = 2 * PI_HALF; }else if(camObjectPos.x > 0 && camObjectPos.y < 0){ x.x = 2 * PI_HALF; y.y = 3 * PI_HALF; z.z = 3 * PI_HALF; } }else if(below){ if(camObjectPos.x > 0 && camObjectPos.y > 0){ x.x = 0 * PI_HALF; y.y = 0 * PI_HALF; z.z = 0 * PI_HALF; }else if(camObjectPos.x < 0 && camObjectPos.y > 0){ x.x = 0 * PI_HALF; y.y = 1 * PI_HALF; z.z = 1 * PI_HALF; }else if(camObjectPos.x < 0 && camObjectPos.y < 0){ x.x = 3 * PI_HALF; y.y = 1 * PI_HALF; z.z = 2 * PI_HALF; }else if(camObjectPos.x > 0 && camObjectPos.y < 0){ x.x = 3 * PI_HALF; y.y = 0 * PI_HALF; z.z = 3 * PI_HALF; } } } } } // adjust scale of components for(let handleName of Object.keys(this.handles)){ let handle = this.handles[handleName]; let node = handle.node; //xzw add:---- -当该轴正对相机时隐藏。(主要针对ortho类型camera。 if(!Potree.Utils.getObjVisiByReason(node,'modeForce') )continue; let alignment = handle.alignment; if(alignment && (!handleName.includes('rotation') || camera.type == 'OrthographicCamera')){//旋转的话正常都应该显示 let normal; let dir = new Vector3(...alignment).applyQuaternion(this.scene.quaternion); if(camera.type == 'OrthographicCamera'){ normal = new Vector3(0,0,-1).applyQuaternion(camera.quaternion); }else { normal = new Vector3().subVectors(center, camera.position).normalize(); } let ifOnLine; if(handleName.includes('rotation') || handleName.includes('plane')){ // 旋转轴和视线垂直时隐藏 ifOnLine = Math.abs(dir.dot(normal)) < 0.1; }else { ifOnLine = Math.abs(dir.dot(normal)) > 0.995; } Potree.Utils.updateVisible(node, 'faceToCamHide', !ifOnLine); }else { Potree.Utils.updateVisible(node, 'faceToCamHide', true); } if(!node.visible)continue; //------------------------------------------------------------------------ if(handle.dontScale)continue; //add let handlePos = node.getWorldPosition(new Vector3()); let distance = handlePos.distanceTo(camera.position); let pr = Utils.projectedRadius(1, camera, distance, domElement.clientWidth, domElement.clientHeight); let ws = node.parent.getWorldScale(new Vector3()); let s = (ScaleRatio / pr); let scale = new Vector3(s, s, s).divide(ws); let rot = new Matrix4().makeRotationFromEuler(node.rotation); //需要使用到旋转,所以我把设置scale的移到旋转后了,否则在视图上下旋转的分界线处rotateHandel会被拉长从而闪烁。 let rotInv = rot.clone().invert(); scale.applyMatrix4(rotInv); scale.x = Math.abs(scale.x); scale.y = Math.abs(scale.y); scale.z = Math.abs(scale.z); node.scale.copy(scale); } } }else { this.scene.visible = false; } } onPointerMove(){ let pointer = this.viewer.inputHandler.pointer; let camera = this.viewer.mainViewport.camera; if( this.selection.length === 1 && !this.dragging ){ //xzw 添加dragging条件 let ray = Utils.mouseToRay(pointer, camera ); let raycaster = new Raycaster(ray.origin, ray.direction); raycaster.layers.enableAll();//add let pickVolumes = this.pickVolumes.filter(v=>{ let mode = v.handle.split('.')[0]; if(!this.modesEnabled[mode])return return v.parent.parent.visible //可能被隐藏该轴 }); let intersects = raycaster.intersectObjects(pickVolumes, true); intersects = intersects.sort(function(a,b){ let order2 = b.object.renderOrder || 0; let order1 = a.object.renderOrder || 0; return order2-order1 }); // 降序 if(intersects.length > 0){ let I = intersects[0]; let handleName = I.object.handle; //console.log(handleName) this.setActiveHandle(this.handles[handleName]); }else { this.setActiveHandle(null); } } } }; /* note: transformationTool.scene会跟随选中物体,其scale就是boundingbox的大小。因此transformationTool.frame这个框也会跟着缩放 */ const boxOpacity = { hovered : 0.1, selected : 0.2 }; const LineOpacity = { default : 0.6, selected: 1, }; const colors = { 2: 0x2ee4ce,//0x00ff80, //可见 3: 0xffc23b,//0xff3158, //不可见 4: 0xffffff, // }; class Volume$1 extends Object3D { constructor (args = {}) { super(); if(this.constructor.name === "Volume"){ console.warn("Can't create object of class Volume directly. Use classes BoxVolume or SphereVolume instead."); } this._clip = args.clip || false; this._visible = true; //this.showVolumeLabel = true; this._modifiable = args.modifiable || true; { // event listeners this.addEventListener('select', e => { //console.log('select') this.setSelected(true); }); this.addEventListener('deselect', e => { //console.log('deselect') this.setSelected(false); }); this.addEventListener('mouseover', e => { this.hovered = true; this.update(); }); this.addEventListener('mouseleave', e => { this.hovered = false; this.update(); }); } } setSelected(state){//add this.selected = !!state; this.update(); } get visible(){ return this._visible; } set visible(value){ if(this._visible !== value){ this._visible = value; this.dispatchEvent({type: "visibility_changed", object: this}); } } getVolume () { console.warn("override this in subclass"); } update () { }; raycast (raycaster, intersects) { } get clip () { return this._clip; } set clip (value) { if(this._clip !== value){ this._clip = value; this.update(); this.dispatchEvent({ type: "clip_changed", object: this }); } } get modifieable () { return this._modifiable; } set modifieable (value) { this._modifiable = value; this.update(); } }; class BoxVolume$1 extends Volume$1{ constructor(args = {}){ super(args); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = 'box_' + this.constructor.counter; this.clipTask = args.clipTask || Potree.ClipTask.SHOW_INSIDE; //add this.showBox = true; let boxGeometry = new BoxGeometry(1, 1, 1); boxGeometry.computeBoundingBox(); let boxFrameGeometry = new Geometry(); let Vector3$1 = Vector3; { boxFrameGeometry.vertices.push( // bottom new Vector3$1(-0.5, -0.5, 0.5), new Vector3$1(0.5, -0.5, 0.5), new Vector3$1(0.5, -0.5, 0.5), new Vector3$1(0.5, -0.5, -0.5), new Vector3$1(0.5, -0.5, -0.5), new Vector3$1(-0.5, -0.5, -0.5), new Vector3$1(-0.5, -0.5, -0.5), new Vector3$1(-0.5, -0.5, 0.5), // top new Vector3$1(-0.5, 0.5, 0.5), new Vector3$1(0.5, 0.5, 0.5), new Vector3$1(0.5, 0.5, 0.5), new Vector3$1(0.5, 0.5, -0.5), new Vector3$1(0.5, 0.5, -0.5), new Vector3$1(-0.5, 0.5, -0.5), new Vector3$1(-0.5, 0.5, -0.5), new Vector3$1(-0.5, 0.5, 0.5), // sides new Vector3$1(-0.5, -0.5, 0.5), new Vector3$1(-0.5, 0.5, 0.5), new Vector3$1(0.5, -0.5, 0.5), new Vector3$1(0.5, 0.5, 0.5), new Vector3$1(0.5, -0.5, -0.5), new Vector3$1(0.5, 0.5, -0.5), new Vector3$1(-0.5, -0.5, -0.5), new Vector3$1(-0.5, 0.5, -0.5), ); } this.material = new DepthBasicMaterial({ color: colors[this.clipTask], side:DoubleSide, transparent: true, opacity: boxOpacity.hovered, depthTest: true, depthWrite: false, useDepth:true, clipDistance : 2,//消失距离 occlusionDistance: 0.1,//变为backColor距离 maxClipFactor: 0.9, }); this.box = new Mesh(boxGeometry, this.material); this.box.geometry.computeBoundingBox(); this.boundingBox = this.box.geometry.boundingBox; this.add(this.box); //this.frame = new THREE.LineSegments(boxFrameGeometry, new THREE.LineBasicMaterial({color: colors[this.clipTask], opacity:LineOpacity.default/* 0xff2050 */})); this.frame = LineDraw.createFatLine( boxFrameGeometry.vertices, { color: colors[this.clipTask], opacity:LineOpacity.default, lineWidth:1, dontAlwaysSeen:true} ); // this.frame.mode = THREE.Lines; this.add(this.frame); this.update(); } update(){ this.boundingBox = this.box.geometry.boundingBox; this.boundingSphere = this.boundingBox.getBoundingSphere(new Sphere()); Potree.Utils.updateVisible(this.box, 'selected', (this.selected || this.hovered) && this.showBox); this.box.material.opacity = this.selected ? boxOpacity.selected : boxOpacity.hovered; this.box.material.color.set(colors[this.clipTask]); this.frame.material.color.set(colors[this.clipTask]); this.frame.material.opacity = this.selected ? LineOpacity.selected : LineOpacity.default; this.frame.material.lineWidth = this.selected ? 2 : 1; } raycast (raycaster, intersects) { let is = []; this.box.raycast(raycaster, is); if (is.length > 0) { let I = is[0]; intersects.push({ distance: I.distance, object: this, point: I.point.clone() }); } } getVolume(){ return Math.abs(this.scale.x * this.scale.y * this.scale.z); } }; class SphereVolume$2 extends Volume$1{ constructor(args = {}){ super(args); this.constructor.counter = (this.constructor.counter === undefined) ? 0 : this.constructor.counter + 1; this.name = 'sphere_' + this.constructor.counter; let sphereGeometry = new SphereGeometry(1, 32, 32); sphereGeometry.computeBoundingBox(); this.material = new MeshBasicMaterial({ color: 0x00ff00, transparent: true, opacity: 0.3, depthTest: true, depthWrite: false}); this.sphere = new Mesh(sphereGeometry, this.material); this.sphere.visible = false; this.sphere.geometry.computeBoundingBox(); this.boundingBox = this.sphere.geometry.boundingBox; this.add(this.sphere); this.label.visible = false; let frameGeometry = new Geometry(); { let steps = 64; let uSegments = 8; let vSegments = 5; let r = 1; for(let uSegment = 0; uSegment < uSegments; uSegment++){ let alpha = (uSegment / uSegments) * Math.PI * 2; let dirx = Math.cos(alpha); let diry = Math.sin(alpha); for(let i = 0; i <= steps; i++){ let v = (i / steps) * Math.PI * 2; let vNext = v + 2 * Math.PI / steps; let height = Math.sin(v); let xyAmount = Math.cos(v); let heightNext = Math.sin(vNext); let xyAmountNext = Math.cos(vNext); let vertex = new Vector3(dirx * xyAmount, diry * xyAmount, height); frameGeometry.vertices.push(vertex); let vertexNext = new Vector3(dirx * xyAmountNext, diry * xyAmountNext, heightNext); frameGeometry.vertices.push(vertexNext); } } // creates rings at poles, just because it's easier to implement for(let vSegment = 0; vSegment <= vSegments + 1; vSegment++){ //let height = (vSegment / (vSegments + 1)) * 2 - 1; // -1 to 1 let uh = (vSegment / (vSegments + 1)); // -1 to 1 uh = (1 - uh) * (-Math.PI / 2) + uh *(Math.PI / 2); let height = Math.sin(uh); console.log(uh, height); for(let i = 0; i <= steps; i++){ let u = (i / steps) * Math.PI * 2; let uNext = u + 2 * Math.PI / steps; let dirx = Math.cos(u); let diry = Math.sin(u); let dirxNext = Math.cos(uNext); let diryNext = Math.sin(uNext); let xyAmount = Math.sqrt(1 - height * height); let vertex = new Vector3(dirx * xyAmount, diry * xyAmount, height); frameGeometry.vertices.push(vertex); let vertexNext = new Vector3(dirxNext * xyAmount, diryNext * xyAmount, height); frameGeometry.vertices.push(vertexNext); } } } this.frame = new LineSegments(frameGeometry, new LineBasicMaterial({color: 0x000000})); this.add(this.frame); let frameMaterial = new MeshBasicMaterial({wireframe: true, color: 0x000000}); this.frame = new Mesh(sphereGeometry, frameMaterial); //this.add(this.frame); //this.frame = new THREE.LineSegments(boxFrameGeometry, new THREE.LineBasicMaterial({color: 0x000000})); // this.frame.mode = THREE.Lines; //this.add(this.frame); this.update(); } update(){ this.boundingBox = this.sphere.geometry.boundingBox; this.boundingSphere = this.boundingBox.getBoundingSphere(new Sphere()); //if (this._clip) { // this.sphere.visible = false; // this.label.visible = false; //} else { // this.sphere.visible = true; // this.label.visible = this.showVolumeLabel; //} } raycast (raycaster, intersects) { let is = []; this.sphere.raycast(raycaster, is); if (is.length > 0) { let I = is[0]; intersects.push({ distance: I.distance, object: this, point: I.point.clone() }); } } // see https://en.wikipedia.org/wiki/Ellipsoid#Volume getVolume(){ return (4 / 3) * Math.PI * this.scale.x * this.scale.y * this.scale.z; } }; const renderOrders = { line: 0 , spot: 15, //高过模型 }; const planeGeo$2 = new PlaneGeometry(1,1); let texLoader$7 = new TextureLoader(); let lineMat = new LineBasicMaterial({ color: '#ffffff', }); let spotMat; const defaultLineLength = 0.6; const defaultSpotScale = 0.4; class Tag extends Object3D{ constructor(o){ super(); this.lineLength = o.lineLength != void 0 ? o.lineLength : defaultLineLength; this.position.copy(o.position); this.normal = o.normal != void 0 ? o.normal : new Vector3(0,0,-1); this.root = o.root; //this.matrixAutoUpdate = false this.build(); /* this.spot.addEventListener('mouseover',()=>{ }) */ } build(){ if(!spotMat){ spotMat = new MeshBasicMaterial({ transparent:true, map: texLoader$7.load(Potree.resourcePath+'/textures/spot_default.png' ), }); } let endPos = this.normal.clone().multiplyScalar(this.lineLength); this.line = LineDraw.createLine([ new Vector3(0,0,0), endPos ], {mat:lineMat}); let group = new Object3D(); this.spot = new Mesh(planeGeo$2, spotMat); this.spot.scale.set(defaultSpotScale,defaultSpotScale,defaultSpotScale); this.titleLabel = new TextSprite$2({root: group, text:'1', sizeInfo:{width2d:200}, textColor:{r:255,g:255,b:255,a:1.0}, backgroundColor:{r:0,g:0,b:0,a:0.8}, borderRadius: 6, fontsize:13, fontWeight:'',//thick renderOrder : renderOrders.spot, pickOrder:renderOrders.spot, }); //更新sprite时,实际更新的是root: spot的矩阵 this.spot.renderOrder = renderOrders.spot; /* const mainLabelProp = { backgroundColor: {r: defaultColor.r*255, g: defaultColor.g*255, b: defaultColor.b*255, a:config.measure.default.opacity}, textColor: {r: textColor.r*255, g: textColor.g*255, b: textColor.b*255, a: 1.0}, fontsize:16, useDepth : true , renderOrder : 5, pickOrder:5, } */ this.titleLabel.position.set(0,0.4,0); this.titleLabel.sprite.material.depthTest = this.titleLabel.sprite.material.depthWrite = true; group.position.copy(endPos); group.add(this.spot); group.add(this.titleLabel); this.add(group); this.add(this.line); viewer.scene.tags.add(this); } changeTitle(title){ this.titleLabel.changeText(title); } updateMatrixWorld(force){ //重写,只为了将root当做parent this.updateMatrix(); this.matrixWorld.multiplyMatrices( this.root.matrixWorld, this.matrix ); const children = this.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].updateMatrixWorld( force ); } } updateWorldMatrix( updateParents, updateChildren ) {//重写,只为了将root当做parent if ( updateParents === true && this.root !== null ) { this.root.updateWorldMatrix( true, false ); } if ( this.matrixAutoUpdate ) this.updateMatrix(); this.matrixWorld.multiplyMatrices( this.root.matrixWorld, this.matrix ); if ( updateChildren === true ) { const children = this.children; for ( let i = 0, l = children.length; i < l; i ++ ) { children[ i ].updateWorldMatrix( false, true ); } } } dispose(){ this.parent.remove(this); this.titleLabel.dispose(); } } class TagTool extends EventDispatcher{ constructor (viewer) { super(); this.viewer = viewer; this.viewer.addEventListener('start_inserting_tag', e => { this.viewer.dispatchEvent({ type: 'cancel_insertions' }); }); } createTagFromData(data){ let tag = new Tag({ title: data.title, position: data.position, normal: data.normal, root: data.root //e.intersect.pointcloud || e.intersect.object }); return tag } startInsertion (args = {}, callback, cancelFun) { let deferred = $.Deferred(); this.viewer.dispatchEvent({ type: 'start_inserting_tag' }); this.adding = true; let cancel = ()=>{ end(); }; let end = ()=>{ this.adding = false; viewer.dispatchEvent({type:"endTagMove"}); this.viewer.removeEventListener('global_click', click); }; let click = (e)=>{ var worldPos = e.intersect && (/* e.intersect.orthoIntersect || */e.intersect.location); if(!worldPos){ return } let localPos = Potree.Utils.datasetPosTransform({ toDataset: true, pointcloud:e.intersect.pointcloud, object:e.intersect.object, position:worldPos }); let tag = new Tag({ title: '1', position: localPos, normal:e.intersect.normal, root: e.intersect.pointcloud || e.intersect.object }); //pointcloud里加一个normal 的非float32 end(); e.consume && e.consume(); deferred.resolve(tag); return {stopContinue:true} }; this.viewer.addEventListener('global_click', click, {importance:10}); return deferred.promise() } } const initDir = new Vector3(0,1,0);//指南针模型的北方向 向屏幕里 class Compass extends EventDispatcher{ constructor(dom, viewport){ super(); this.angle = 0; this.show = false; if(dom){ this.dom = $(dom); } this.viewport = viewport; this.init(); } init(){ var width = 100, height = 100; if(!this.dom){ this.dom = $('
'); $(viewer.renderArea).append(this.dom); } this.dom.css({ display:"none", position:"absolute",right:"1%",top: "60px",width:width+"px",height:height+"px", "z-index":100,"pointer-events":"none" }); let child = $("
"+/* (config.lang=='zh'? */'北'/* :'N') */+"
"); this.dom.append(child); this.dom.find(".dirText").css({textAlign:"center","font-size":"10px","position":"absolute", width: "100%", height: "25px", "line-height": "25px"}); this.dom.find(".north").css({"color":"#02a0e9","top":"0"}); this.dom.find(".south").css({"color":"#ff1414","bottom":"0"}); this.dom.find(".center").css({ //"background":`url(${config.getStaticResource('img')}/dire.png)`, width: width/2+"px", height: height/2+"px", "background-size": "contain", "background-position": "center", left: "50%", top: "50%", transform: "translate(-50%,-50%)", position: "absolute" }); this.dom.find(".dirText").css({ "text-align": "center", "font-size": "10px", "color": "rgb(255, 255, 255)", "position": "absolute", "top": "50%", "left": "50%", "width": "45%", "height": "0px", "transform-origin": "left center", }); this.dom.find(".dirText span").css({ display: "block", position: "absolute", right: "5px", top: "0", width: "20px", height: "20px", "line-height": "20px", // "font-size": ".75rem ", "margin-top": "-10px", }); try { this.renderer = new WebGLRenderer({ antialias: true, alpha:true });//许钟文 添加个抗锯齿,否则添加的线条锯齿严重, this.renderer.autoClear = !0; this.renderer.setPixelRatio(window.devicePixelRatio ? window.devicePixelRatio : 1); this.renderer.domElement.setAttribute('name','compass'); this.renderer.setClearAlpha(0.0); //xst修改 //this.renderer.setSize(width/2, height/2, false, window.devicePixelRatio ? window.devicePixelRatio : 1); //xst修改 //this.renderer.setPixelRatio(window.devicePixelRatio ? window.devicePixelRatio : 1); //this.renderer.setSize(width/2, height/2); //xst修改 this.renderer.setDrawingBufferSize( width/2, height/2, window.devicePixelRatio ? window.devicePixelRatio : 1 ); //this.emit(SceneRendererEvents.ContextCreated) } catch (e) { viewer.dispatchEvent('webglError', {msg:e}); } this.dom.find(".center")[0].appendChild(this.renderer.domElement); this.renderer.domElement.style.width = this.renderer.domElement.style.height = '100%'; this.camera = new PerspectiveCamera; this.camera.fov = 50; this.camera.updateProjectionMatrix(); this.scene = new Scene, this.scene.add(this.camera); this.createCompass(); viewer.addEventListener('camera_changed', e => { if (e.viewport == this.viewport && (e.changeInfo.quaternionChanged /* || e.changeInfo.quaternionChanged */)) { this.update(); } }); this.setDomPos(); if(this.viewport)this.setDisplay(true); } createCompass(){ //ConeBufferGeometry(radius : Float, height : Float, radialSegments : Integer, heightSegments : Integer, openEnded : Boolean, thetaStart : Float, thetaLength : Float) const height = 2; const geometry1 = new ConeBufferGeometry( 0.7, height, 4, true ); const geometry2 = new ConeBufferGeometry( 0.7, height, 4, true ); const material = new MeshBasicMaterial({ vertexColors :true }); //指南针由两个四棱锥拼成,为了渐变颜色,采用指定vertexColor的方式。 var setColor = function(geometry, color1,color2){ const colors = []; for ( let i = 0, n = geometry.attributes.position.count; i < n; ++ i ) { colors.push( 1, 1, 1 ); } var set = function(index, color){//设置第index个点的颜色 colors[index*3+0] = color[0]; colors[index*3+1] = color[1]; colors[index*3+2] = color[2]; }; var mid = [(color1[0]+color2[0])/2, (color1[1]+color2[1])/2, (color1[2]+color2[2])/2 ]; set(1,color1); set(5,color1);set(6,color1); set(2,mid); set(3,mid);set(7,mid); set(4,color2); set(8,color2);set(9,color2); geometry.setAttribute("color", new BufferAttribute(new Float32Array(colors), 3)); }; var blue1 = [1/255,238/255,245/255]; //逐渐变深 var blue2 = [20/255,146/255,170/255]; var blue3 = [40/255,60/255,103/255]; setColor(geometry1, blue1,blue2); setColor(geometry2, blue2,blue3); /* 朝箭头方向看点构成如下 虽然geometry.attributes.position.count = 19 只有1-9设置的颜色是有效的 另外为什么7决定了上下两边的颜色呢…… 5、9可将其分成上下两个颜色 6 /|\ / | \ 7 /_2|1_\ 5 \ 3|4 / 9 \ | / \|/ 8 */ const cone = new Mesh( geometry1, material ); cone.position.setY(height/2); geometry1.computeVertexNormals();//computeFaceNormals geometry2.computeVertexNormals(); const cones = new Object3D(); cones.add(cone); let cone2 = new Mesh( geometry2, material ); cone2.rotation.x = Math.PI; cone2.position.setY(-height/2); cones.add(cone2); //cones.rotation.x = Math.PI / 2;//转向initDir的方向 //cones.rotation.z = Math.PI / 2; cones.rotation.z = Math.PI ;//转向initDir的方向 cones.scale.set(0.7,0.7,0.7); this.scene.add(cones); this.cones = cones; } setNorth(){ //设置北方向,这决定了指南针自身的朝向。 const floors = store.getters['scene/houstFloor'].floors; if(!floors || !floors.length){ return } const floor = floors[0]; const metadata = app.store.getters['scene/metadata'] || {}; this.angle = (floor && floor.dire || 0) + MathUtils.radToDeg(parseFloat(metadata.floorPlanAngle || 0)); //基础朝向 this.cones.rotation.y = Math.PI / 2 - MathUtils.degToRad(this.angle); //console.log("dir:"+floor.dire+", floorPlanAngle:"+metadata.floorPlanAngle) this.update(); } update(quaternion){ if(!this.show)return; if(!quaternion) quaternion = this.viewport.camera.quaternion.clone(); this.updateCamera(quaternion); this.updateLabel(quaternion); this.render(); } /*updateLabel(quaternion){//更新北标签 var dir = viewer.mainViewport.view.direction; var oriDir = initDir.clone() //指南针最初始时的北方向 var extraQua if(objects.player.mode == "transitioning"){//当transitioning时,相机的quaternion不是用control的lookAt算出来,而是直接由一个quaternion过渡到另一个,这样相机将会是歪的,投影面也就不会是原先的水平面。 var tempCamera = new THREE.Camera(); //借用camera的lookAt算出如果正视同样的target, quaternion会是什么值。 将它乘以当前相机quaternion,得到的就是相机歪的旋转值。 tempCamera.position.copy(this.camera.position); tempCamera.lookAt(tempCamera.position.clone().add(dir)) var q = tempCamera.quaternion.inverse() extraQua = q.premultiply(quaternion) //歪掉的额外旋转值 } //北标签的方向为指南针轮盘方向,也就是要将camera的方向投影到水平面上。 但是如果相机歪了,看到的世界都会歪一定角度,投影面也要歪一定角度。 var up = new THREE.Vector3(0,0,1) //投影水平面的法线,也是相机的摆正的up方向 extraQua && up.applyQuaternion(extraQua) dir.projectOnPlane(up) //将方向投影到水平面上; 如果相机不是正视(extraQua不为0001),就要将水平面也转动 oriDir.projectOnPlane(up)//为什么initDir投影了和没有投影angle结果一样 var angle = dir.angleTo(oriDir) if(dir.cross(oriDir).y > 0)angle = -angle var deg = this.angle - 90 + THREE.Math.radToDeg(angle) //因为css写的样式初始是指向右方,和initDir差了90°,所以减去。 this.dom.find(".dirText").css( "transform","rotate("+deg+"deg)" ) this.dom.find(".dirText span").css("transform","rotate("+(-deg)+"deg)") } */ updateLabel(quaternion){//更新北标签 let deg = MathUtils.radToDeg(this.viewport.view.yaw) - 90; this.dom.find(".dirText").css( "transform","rotate("+deg+"deg)" ); this.dom.find(".dirText span").css("transform","rotate("+(-deg)+"deg)"); } updateCamera(quaternion){ //更新canvas中的指南针表现,也就是更新相机,和场景中的相机朝向一致。 const radius = 5; //相机距离 this.camera.quaternion.copy(quaternion); var dir = this.viewport.view.direction; //相机朝向 this.camera.position.copy(dir.multiplyScalar(radius).negate()); //相机绕着指南针中心(000)转动 } changeViewport(viewport){ this.viewport = viewport; this.update(); //因相机更新了 } render(){ this.renderer.render(this.scene, this.camera); } setDisplay(state){ this.show = !!state; if(this.show){ this.update(); this.dom.fadeIn(100); }else { this.dom.fadeOut(100); } } setAutoDisplay(state){//被直接改变了dom的显示 this.autoJudgeShow = state; if(state){ this._autoDisplayEvent = ()=>{ let oldShow = this.show; this.show = !!this.renderer.domElement.clientHeight; if(oldShow != this.show){ this.update(); } }; viewer.addEventListener('update_start',this._autoDisplayEvent); }else { viewer.removeEventListener('update_start',this._autoDisplayEvent); } } setDomPos(){ if(!this.viewport)return let right = this.viewport.left + this.viewport.width; this.dom.css({'right':((1-right)*100 + 1) + '%'}); } } class AxisViewer extends ViewerBase{ constructor(listenViewport, parentArea, options={}){ let domElement = $(`
`); $(parentArea).append(domElement); let posInfo = Object.assign({ //default: right: 0, top:0, width:'100px', height:'100px' },options.domStyle,{ position:'absolute' }); if(options.domStyle){ if(options.domStyle.bottom != void 0){ delete posInfo.top; } if(options.domStyle.left != void 0){ delete posInfo.right; } } domElement.css(posInfo); super(domElement[0],{name:'axis', antialias:true } ); this.listenViewport = listenViewport; Object.defineProperty(listenViewport, 'axis', { value: this, writable: true, // 可写(可选) enumerable: false, // 不可枚举 configurable: true // 可配置(可选) }); this.scene = new Scene; //this.camera = new THREE.PerspectiveCamera(30, 1, 1, this.radius*3); let w = 50; this.camera = new OrthographicCamera(-w/2,w/2,w/2,-w/2 , 1, w*4); this.camera.zoom = 2.7;//(domElement.clientWidth || 300) / w * 1.267//zoom越大视野越小 this.camera.updateProjectionMatrix(); this.view = new ExtendView(); this.view.radius = 70; this.viewports = [new Viewport( this.view, this.camera, { left:0, bottom:0, width:1, height: 1, name:'axis' }) ]; this.updateScreenSize(); this.createAxis(); let updateCamera = e => { if (e.viewport == listenViewport && e.changeInfo.quaternionChanged) { this.update(); } }; viewer.addEventListener('camera_changed', updateCamera); let repos = ()=>{ if(!listenViewport.active || listenViewport.width == 0 || listenViewport.height == 0){ return domElement.css('visibility','hidden') } domElement.css('visibility','visible'); let props = [['left','right', 'width'],['bottom','top', 'height']]; let set = (prop)=>{ let str; let name = posInfo[prop[1]] != void 0 ? prop[1] : prop[0]; let percent = posInfo[prop[1]] != void 0 ? (1-listenViewport[prop[0]] - listenViewport[prop[2]]) : listenViewport[prop[0]]; if(posInfo[name] == 0 || posInfo[name].includes('%')){ str = (parseFloat(posInfo[name]) + percent * 100) + '%'; }else {//px str = 'calc('+(percent * 100) + '% + ' + posInfo[name] + ')'; } domElement.css(name, str); }; set(props[0]); set(props[1]); }; this.listenViewport.addEventListener('resize',repos); this.addEventListener('dispose',()=>{ viewer.removeEventListener('camera_changed', updateCamera); viewer.removeEventListener('resize', repos); }); repos(); this.update(); } createAxis(){ let axis = new Object3D ;['x','y','z'].forEach((axisText)=>{ let color = new Color().set(Potree.config.axis[axisText].color); let group = new Object3D; let line = LineDraw.createLine([new Vector3, new Vector3(0,0,7)],{color}); let label = this.createLabel(axisText, color); label.position.set(0,0,11 /* + this.renderArea.clientWidth/200 */); if(axisText == 'y'){ group.rotation.x = -Math.PI / 2; }else if(axisText == 'x'){ group.rotation.y = Math.PI / 2; } group.add(line); group.add(label); axis.add(group); }); this.axis = axis; this.scene.add(this.axis); } createLabel(text, color){ let fontsize = this.renderArea.clientWidth / 5; //20//Math.round( Potree.math.linearClamp(this.renderArea.clientWidth, [80,500],[12,50])) let s = 3100 / this.renderArea.clientWidth; console.log('fontsize',fontsize); let label = new TextSprite$2({ backgroundColor: {r: 0, g: 0, b: 0, a:0}, textColor: {r: color.r * 255, g: color.g*255, b: color.b*255, a:1}, fontsize, //useDepth : true , renderOrder : 5,// pickOrder:5, fontWeight:'Lighter', text, name:'axis' , viewports: this.viewports }); label.scale.set(s,s,s); return label } update(){ this.updateCamera(); this.render(); } updateCamera(){ let view = this.listenViewport.view; this.view.yaw = view.yaw; this.view.pitch = view.pitch; var dir = view.direction; //相机朝向 this.view.position.copy(dir.multiplyScalar(this.view.radius).negate()); //相机绕着指南针中心(000)转动 this.view.applyToCamera(this.camera); } render(){ viewer.dispatchEvent({type:"render.begin", viewport:this.viewports[0]}); //update sprite this.renderer.render(this.scene, this.camera); } dispose(){ this.axis.traverse((child)=>{ if(child instanceof TextSprite$2){ child.dispose(); }else if(child instanceof LineSegments){ child.material.dispose(); child.geometry.dispose(); } }); super.dispose(); delete this.listenViewport.axis; } } // Copied from three.js: WebGLRenderer.js function paramThreeToGL$1(_gl, p) { let extension; if (p === RepeatWrapping) return _gl.REPEAT; if (p === ClampToEdgeWrapping) return _gl.CLAMP_TO_EDGE; if (p === MirroredRepeatWrapping) return _gl.MIRRORED_REPEAT; if (p === NearestFilter) return _gl.NEAREST; if (p === NearestMipMapNearestFilter) return _gl.NEAREST_MIPMAP_NEAREST; if (p === NearestMipMapLinearFilter) return _gl.NEAREST_MIPMAP_LINEAR; if (p === LinearFilter) return _gl.LINEAR; if (p === LinearMipMapNearestFilter) return _gl.LINEAR_MIPMAP_NEAREST; if (p === LinearMipMapLinearFilter) return _gl.LINEAR_MIPMAP_LINEAR; if (p === UnsignedByteType) return _gl.UNSIGNED_BYTE; if (p === UnsignedShort4444Type) return _gl.UNSIGNED_SHORT_4_4_4_4; if (p === UnsignedShort5551Type) return _gl.UNSIGNED_SHORT_5_5_5_1; if (p === UnsignedShort565Type) return _gl.UNSIGNED_SHORT_5_6_5; if (p === ByteType) return _gl.BYTE; if (p === ShortType) return _gl.SHORT; if (p === UnsignedShortType) return _gl.UNSIGNED_SHORT; if (p === IntType) return _gl.INT; if (p === UnsignedIntType) return _gl.UNSIGNED_INT; if (p === FloatType) return _gl.FLOAT; if (p === HalfFloatType) { extension = extensions.get('OES_texture_half_float'); if (extension !== null) return extension.HALF_FLOAT_OES; } if (p === AlphaFormat) return _gl.ALPHA; if (p === RGBFormat) return _gl.RGB; if (p === RGBAFormat) return _gl.RGBA; if (p === LuminanceFormat) return _gl.LUMINANCE; if (p === LuminanceAlphaFormat) return _gl.LUMINANCE_ALPHA; if (p === DepthFormat) return _gl.DEPTH_COMPONENT; if (p === DepthStencilFormat) return _gl.DEPTH_STENCIL; if (p === AddEquation) return _gl.FUNC_ADD; if (p === SubtractEquation) return _gl.FUNC_SUBTRACT; if (p === ReverseSubtractEquation) return _gl.FUNC_REVERSE_SUBTRACT; if (p === ZeroFactor) return _gl.ZERO; if (p === OneFactor) return _gl.ONE; if (p === SrcColorFactor) return _gl.SRC_COLOR; if (p === OneMinusSrcColorFactor) return _gl.ONE_MINUS_SRC_COLOR; if (p === SrcAlphaFactor) return _gl.SRC_ALPHA; if (p === OneMinusSrcAlphaFactor) return _gl.ONE_MINUS_SRC_ALPHA; if (p === DstAlphaFactor) return _gl.DST_ALPHA; if (p === OneMinusDstAlphaFactor) return _gl.ONE_MINUS_DST_ALPHA; if (p === DstColorFactor) return _gl.DST_COLOR; if (p === OneMinusDstColorFactor) return _gl.ONE_MINUS_DST_COLOR; if (p === SrcAlphaSaturateFactor) return _gl.SRC_ALPHA_SATURATE; if (p === RGB_S3TC_DXT1_Format || p === RGBA_S3TC_DXT1_Format || p === RGBA_S3TC_DXT3_Format || p === RGBA_S3TC_DXT5_Format) { extension = extensions.get('WEBGL_compressed_texture_s3tc'); if (extension !== null) { if (p === RGB_S3TC_DXT1_Format) return extension.COMPRESSED_RGB_S3TC_DXT1_EXT; if (p === RGBA_S3TC_DXT1_Format$1) return extension.COMPRESSED_RGBA_S3TC_DXT1_EXT; if (p === RGBA_S3TC_DXT3_Format) return extension.COMPRESSED_RGBA_S3TC_DXT3_EXT; if (p === RGBA_S3TC_DXT5_Format$1) return extension.COMPRESSED_RGBA_S3TC_DXT5_EXT; } } if (p === RGB_PVRTC_4BPPV1_Format || p === RGB_PVRTC_2BPPV1_Format || p === RGBA_PVRTC_4BPPV1_Format || p === RGBA_PVRTC_2BPPV1_Format) { extension = extensions.get('WEBGL_compressed_texture_pvrtc'); if (extension !== null) { if (p === RGB_PVRTC_4BPPV1_Format) return extension.COMPRESSED_RGB_PVRTC_4BPPV1_IMG; if (p === RGB_PVRTC_2BPPV1_Format) return extension.COMPRESSED_RGB_PVRTC_2BPPV1_IMG; if (p === RGBA_PVRTC_4BPPV1_Format) return extension.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG; if (p === RGBA_PVRTC_2BPPV1_Format) return extension.COMPRESSED_RGBA_PVRTC_2BPPV1_IMG; } } if (p === RGB_ETC1_Format) { extension = extensions.get('WEBGL_compressed_texture_etc1'); if (extension !== null) return extension.COMPRESSED_RGB_ETC1_WEBGL; } if (p === MinEquation || p === MaxEquation) { extension = extensions.get('EXT_blend_minmax'); if (extension !== null) { if (p === MinEquation) return extension.MIN_EXT; if (p === MaxEquation) return extension.MAX_EXT; } } if (p === UnsignedInt248Type) { extension = extensions.get('WEBGL_depth_texture'); if (extension !== null) return extension.UNSIGNED_INT_24_8_WEBGL; } return 0; }; let attributeLocations$1 = { "position": {name: "position", location: 0}, "color": {name: "color", location: 1}, "rgba": {name: "color", location: 1}, "intensity": {name: "intensity", location: 2}, "classification": {name: "classification", location: 3}, "returnNumber": {name: "returnNumber", location: 4}, "return number": {name: "returnNumber", location: 4}, "returns": {name: "returnNumber", location: 4}, "numberOfReturns": {name: "numberOfReturns", location: 5}, "number of returns": {name: "numberOfReturns", location: 5}, "pointSourceID": {name: "pointSourceID", location: 6}, "source id": {name: "pointSourceID", location: 6}, "point source id": {name: "pointSourceID", location: 6}, "indices": {name: "indices", location: 7}, "normal": {name: "normal", location: 8}, "spacing": {name: "spacing", location: 9}, "gps-time": {name: "gpsTime", location: 10}, "aExtra": {name: "aExtra", location: 11}, }; class Shader$1 { constructor(gl, name, vsSource, fsSource) { this.gl = gl; this.name = name; this.vsSource = vsSource; this.fsSource = fsSource; this.cache = new Map(); this.vs = null; this.fs = null; this.program = null; this.uniformLocations = {}; this.attributeLocations = {}; this.uniformBlockIndices = {}; this.uniformBlocks = {}; this.uniforms = {}; this.update(vsSource, fsSource); } update(vsSource, fsSource) { this.vsSource = vsSource; this.fsSource = fsSource; this.linkProgram(); } compileShader(shader, source){ let gl = this.gl; gl.shaderSource(shader, source); gl.compileShader(shader); let success = gl.getShaderParameter(shader, gl.COMPILE_STATUS); if (!success) { let info = gl.getShaderInfoLog(shader); let numberedSource = source.split("\n").map((a, i) => `${i + 1}`.padEnd(5) + a).join("\n"); throw `could not compile shader ${this.name}: ${info}, \n${numberedSource}`; } } linkProgram() { const tStart = performance.now(); let gl = this.gl; this.uniformLocations = {}; this.attributeLocations = {}; this.uniforms = {}; gl.useProgram(null); let cached = this.cache.get(`${this.vsSource}, ${this.fsSource}`); if (cached) { this.program = cached.program; this.vs = cached.vs; this.fs = cached.fs; this.attributeLocations = cached.attributeLocations; this.uniformLocations = cached.uniformLocations; this.uniformBlocks = cached.uniformBlocks; this.uniforms = cached.uniforms; return; } else { this.vs = gl.createShader(gl.VERTEX_SHADER); this.fs = gl.createShader(gl.FRAGMENT_SHADER); this.program = gl.createProgram(); if( !gl.isProgram(this.program )){//创建失败 开启多个页面可能会,原因是webglcontextlost //console.error('创建program失败'); viewer.dispatchEvent('webglError', {msg: 'potreeRenderer创建program失败'}); console.log(this.vs); console.log(this.fs); return; } for(let name of Object.keys(attributeLocations$1)){ let location = attributeLocations$1[name].location; let glslName = attributeLocations$1[name].name; gl.bindAttribLocation(this.program, location, glslName); } this.compileShader(this.vs, this.vsSource); this.compileShader(this.fs, this.fsSource); let program = this.program; gl.attachShader(program, this.vs); gl.attachShader(program, this.fs); gl.linkProgram(program); gl.detachShader(program, this.vs); gl.detachShader(program, this.fs); // 检测当前程序链接状态 let success = gl.getProgramParameter(program, gl.LINK_STATUS); if (!success) { let info = gl.getProgramInfoLog(program); throw `could not link program ${this.name}: ${info}`; } { // attribute locations let numAttributes = gl.getProgramParameter(program, gl.ACTIVE_ATTRIBUTES); for (let i = 0; i < numAttributes; i++) { let attribute = gl.getActiveAttrib(program, i); let location = gl.getAttribLocation(program, attribute.name); this.attributeLocations[attribute.name] = location; } } { // uniform locations let numUniforms = gl.getProgramParameter(program, gl.ACTIVE_UNIFORMS); for (let i = 0; i < numUniforms; i++) { let uniform = gl.getActiveUniform(program, i); let location = gl.getUniformLocation(program, uniform.name); this.uniformLocations[uniform.name] = location; this.uniforms[uniform.name] = { location: location, value: null, }; } } // uniform blocks if( typeof WebGL2RenderingContext !== 'undefined' && gl instanceof WebGL2RenderingContext){ //WebGL2RenderingContext在mac的safari14以下是没有定义的 let numBlocks = gl.getProgramParameter(program, gl.ACTIVE_UNIFORM_BLOCKS); for (let i = 0; i < numBlocks; i++) { let blockName = gl.getActiveUniformBlockName(program, i); let blockIndex = gl.getUniformBlockIndex(program, blockName); this.uniformBlockIndices[blockName] = blockIndex; gl.uniformBlockBinding(program, blockIndex, blockIndex); let dataSize = gl.getActiveUniformBlockParameter(program, blockIndex, gl.UNIFORM_BLOCK_DATA_SIZE); let uBuffer = gl.createBuffer(); gl.bindBuffer(gl.UNIFORM_BUFFER, uBuffer); gl.bufferData(gl.UNIFORM_BUFFER, dataSize, gl.DYNAMIC_READ); gl.bindBufferBase(gl.UNIFORM_BUFFER, blockIndex, uBuffer); gl.bindBuffer(gl.UNIFORM_BUFFER, null); this.uniformBlocks[blockName] = { name: blockName, index: blockIndex, dataSize: dataSize, buffer: uBuffer }; } } let cached = { program: this.program, vs: this.vs, fs: this.fs, attributeLocations: this.attributeLocations, uniformLocations: this.uniformLocations, uniforms: this.uniforms, uniformBlocks: this.uniformBlocks, }; this.cache.set(`${this.vsSource}, ${this.fsSource}`, cached); } const tEnd = performance.now(); const duration = tEnd - tStart; //console.log(`shader compile duration: ${duration.toFixed(3)}`); } setUniformMatrix4(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } let tmp = new Float32Array(value.elements); gl.uniformMatrix4fv(location, false, tmp); } setUniform1f(name, value) { const gl = this.gl; const uniform = this.uniforms[name]; if (uniform === undefined) { return; } if(uniform.value === value){ return; } uniform.value = value; gl.uniform1f(uniform.location, value); } setUniformBoolean(name, value) { const gl = this.gl; const uniform = this.uniforms[name]; if (uniform === undefined) { return; } if(uniform.value === value){ return; } uniform.value = value; gl.uniform1i(uniform.location, value); } setUniformTexture(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform1i(location, value); } setUniform2f(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform2f(location, value[0], value[1]); } setUniform3f(name, value) { const gl = this.gl; const location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform3f(location, value[0], value[1], value[2]); } setUniform(name, value) { if (value.constructor === Matrix4) { this.setUniformMatrix4(name, value); } else if (typeof value === "number") { this.setUniform1f(name, value); } else if (typeof value === "boolean") { this.setUniformBoolean(name, value); } else if (value instanceof WebGLTexture$1) { this.setUniformTexture(name, value); } else if (value instanceof Array) { if (value.length === 2) { this.setUniform2f(name, value); } else if (value.length === 3) { this.setUniform3f(name, value); } } else { console.error("unhandled uniform type: ", name, value); } } setUniform1i(name, value) { let gl = this.gl; let location = this.uniformLocations[name]; if (location == null) { return; } gl.uniform1i(location, value); } }; class WebGLTexture$1 { constructor(gl, texture, threeRenderer) { this.gl = gl; this.texture = texture; if(texture.image && !(texture.image instanceof Image) && !texture.isCanvasTexture && !texture.isDataTexture){ //renderTarget的texture在创建renderTarget时已经初始化过 见setupRenderTarget this.id = threeRenderer.properties.get(texture).__webglTexture || gl.createTexture(); this.isFromRenderTarget = true; }else { this.id = gl.createTexture(); } this.target = gl.TEXTURE_2D; this.version = -1; this.update(texture); } update() { if (!this.texture.image) { this.version = this.texture.version; return; } //if(this.isFromRenderTarget)return //没找到怎么update。 在three.js里的uploadTexture没找到。 这里会报错,可能是 state.texImage2D( 3553, 0, glInternalFormat, image.width, image.height, 0, glFormat, glType, null ),类似depthTex的写法 let gl = this.gl; let texture = this.texture; if (this.version === texture.version) { return; } this.target = gl.TEXTURE_2D; gl.bindTexture(this.target, this.id); let level = 0; let internalFormat = paramThreeToGL$1(gl, texture.format); let width = texture.image.width; let height = texture.image.height; let border = 0; let srcFormat = internalFormat; let srcType = paramThreeToGL$1(gl, texture.type); let data; gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, texture.flipY); gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, texture.premultiplyAlpha); gl.pixelStorei(gl.UNPACK_ALIGNMENT, texture.unpackAlignment); if (texture instanceof DataTexture) { data = texture.image.data; gl.texParameteri(this.target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(this.target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); gl.texParameteri(this.target, gl.TEXTURE_MAG_FILTER, paramThreeToGL$1(gl, texture.magFilter)); gl.texParameteri(this.target, gl.TEXTURE_MIN_FILTER, paramThreeToGL$1(gl, texture.minFilter)); gl.texImage2D(this.target, level, internalFormat, width, height, border, srcFormat, srcType, data); } else if ((texture instanceof CanvasTexture) || (texture instanceof Texture)) { data = texture.image; gl.texParameteri(this.target, gl.TEXTURE_WRAP_S, paramThreeToGL$1(gl, texture.wrapS)); gl.texParameteri(this.target, gl.TEXTURE_WRAP_T, paramThreeToGL$1(gl, texture.wrapT)); gl.texParameteri(this.target, gl.TEXTURE_MAG_FILTER, paramThreeToGL$1(gl, texture.magFilter)); gl.texParameteri(this.target, gl.TEXTURE_MIN_FILTER, paramThreeToGL$1(gl, texture.minFilter)); if(this.isFromRenderTarget){ //咋写? //gl.texImage2D( 3553, 0, internalFormat, width, height, 0, srcFormat, srcType, null ) //console.log('isFromRenderTarget') }else { gl.texImage2D(this.target, level, internalFormat, internalFormat, srcType, data); //这个参数怎么跟我查的不一样呢 } if (texture instanceof Texture) {gl.generateMipmap(gl.TEXTURE_2D);} } gl.bindTexture(this.target, null); this.version = texture.version; } }; class WebGLBuffer$1 { constructor() { this.numElements = 0; this.vao = null; this.vbos = new Map(); } }; class Renderer$1 { constructor(threeRenderer) { this.threeRenderer = threeRenderer; this.gl = this.threeRenderer.getContext(); this.buffers = new Map(); this.shaders = new Map(); this.textures = new Map(); this.glTypeMapping = new Map(); this.glTypeMapping.set(Float32Array, this.gl.FLOAT); this.glTypeMapping.set(Uint8Array, this.gl.UNSIGNED_BYTE); this.glTypeMapping.set(Uint16Array, this.gl.UNSIGNED_SHORT); this.toggle = 0; } deleteBuffer(geometry) { let gl = this.gl; let webglBuffer = this.buffers.get(geometry); if (webglBuffer != null) { for (let attributeName in geometry.attributes) { gl.deleteBuffer(webglBuffer.vbos.get(attributeName).handle); } this.buffers.delete(geometry); } } createBuffer(geometry){ let gl = this.gl; let webglBuffer = new WebGLBuffer$1(); webglBuffer.vao = gl.createVertexArray(); webglBuffer.numElements = geometry.attributes.position.count; gl.bindVertexArray(webglBuffer.vao); for(let attributeName in geometry.attributes){ let bufferAttribute = geometry.attributes[attributeName]; let vbo = gl.createBuffer(); gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, bufferAttribute.array, gl.STATIC_DRAW); let normalized = bufferAttribute.normalized; let type = this.glTypeMapping.get(bufferAttribute.array.constructor); if(attributeLocations$1[attributeName] === undefined){ //attributeLocation = attributeLocations["aExtra"]; }else { let attributeLocation = attributeLocations$1[attributeName].location; gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } webglBuffer.vbos.set(attributeName, { handle: vbo, name: attributeName, count: bufferAttribute.count, itemSize: bufferAttribute.itemSize, type: geometry.attributes.position.array.constructor, version: 0 }); } gl.bindBuffer(gl.ARRAY_BUFFER, null); gl.bindVertexArray(null); let disposeHandler = (event) => { this.deleteBuffer(geometry); geometry.removeEventListener("dispose", disposeHandler); }; geometry.addEventListener("dispose", disposeHandler); return webglBuffer; } updateBuffer(geometry){ let gl = this.gl; let webglBuffer = this.buffers.get(geometry); gl.bindVertexArray(webglBuffer.vao); for(let attributeName in geometry.attributes){ let bufferAttribute = geometry.attributes[attributeName]; let normalized = bufferAttribute.normalized; let type = this.glTypeMapping.get(bufferAttribute.array.constructor); let vbo = null; if(!webglBuffer.vbos.has(attributeName)){ vbo = gl.createBuffer(); webglBuffer.vbos.set(attributeName, { handle: vbo, name: attributeName, count: bufferAttribute.count, itemSize: bufferAttribute.itemSize, type: geometry.attributes.position.array.constructor, version: bufferAttribute.version }); }else { vbo = webglBuffer.vbos.get(attributeName).handle; webglBuffer.vbos.get(attributeName).version = bufferAttribute.version; } gl.bindBuffer(gl.ARRAY_BUFFER, vbo); gl.bufferData(gl.ARRAY_BUFFER, bufferAttribute.array, gl.STATIC_DRAW); if(attributeLocations$1[attributeName] === undefined){ //attributeLocation = attributeLocations["aExtra"]; }else { let attributeLocation = attributeLocations$1[attributeName].location; gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } } gl.bindBuffer(gl.ARRAY_BUFFER, null); gl.bindVertexArray(null); } traverse(scene) { let octrees = []; let stack = [scene]; while (stack.length > 0) { let node = stack.pop(); if (node instanceof PointCloudTree) { octrees.push(node); continue; } let visibleChildren = node.children.filter(c => c.visible); stack.push(...visibleChildren); } let result = { octrees: octrees }; return result; } renderNodes(octree, nodes, visibilityTextureData, camera, target, shader, params) { viewer.addTimeMark('renderNodes','start'); let gl = this.gl; let material = params.material ? params.material : octree.material; let shadowMaps = params.shadowMaps == null ? [] : params.shadowMaps; let view = camera.matrixWorldInverse; if(params.viewOverride){ view = params.viewOverride; } let worldView = new Matrix4(); let mat4holder = new Float32Array(16); let i = 0; //---------从renderOctree搬到这---- shader.setUniform1f("size", material.usePanoMap ? Potree.config.material.absolutePanoramaSize * Math.min(window.devicePixelRatio,2) : material.size);//usePanoMap时控制在不大不小的范围内感觉较好,考虑到有的点云稀疏,用大一点的点 shader.setUniform1f("uOpacity", material.usePanoMap ? 1: material.opacity); shader.setUniform3f("uColor", material.color.toArray()); let currentTextureBindingPoint = params.currentTextureBindingPoint; if (material.pointSizeType >= 0/* && window.needvisibilityTexture */) { if (material.pointSizeType === PointSizeType.ADAPTIVE || material.activeAttributeName === "level of detail") { let vnNodes = (params.vnTextureNodes != null) ? params.vnTextureNodes : nodes; visibilityTextureData = octree.computeVisibilityTextureData(vnNodes, camera); const vnt = material.visibleNodesTexture; const data = vnt.image.data; data.set(visibilityTextureData.data); vnt.needsUpdate = true; let webGLTexture = this.textures.get(vnt); webGLTexture.update(); //不加这个会闪烁 let vnWebGLTexture = this.textures.get(material.visibleNodesTexture); //不知道为什么这段从renderOctree中移过来,会崩溃。暂时不移动了 if(vnWebGLTexture){ shader.setUniform1i("visibleNodes", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(vnWebGLTexture.target, vnWebGLTexture.id); currentTextureBindingPoint++; } } } /* if (material.pointSizeType >= 0) { if (material.pointSizeType === PointSizeType.ADAPTIVE || material.activeAttributeName === "level of detail") { let vnNodes = (params.vnTextureNodes != null) ? params.vnTextureNodes : nodes; visibilityTextureData = octree.computeVisibilityTextureData(vnNodes, camera); const vnt = material.visibleNodesTexture; const data = vnt.image.data; data.set(visibilityTextureData.data); vnt.needsUpdate = true; } } */ let transparent = false; if(params.transparent !== undefined){ transparent = params.transparent && material.opacity < 1; }else { transparent = material.usePanoMap ? false : (material.useFilterByNormal || material.opacity < 1); //add useFilterByNormal } if (transparent){ gl.enable(gl.BLEND); if(params.notAdditiveBlending){ gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA ); //NormalBlending /* gl.enable(gl.DEPTH_TEST); gl.depthMask(true); //如果不开启depthWrite, 深度会错乱。 */ //无解 gl.disable(gl.DEPTH_TEST); gl.depthMask(true); }else { gl.blendFunc(gl.SRC_ALPHA, gl.ONE); //AdditiveBlending 原本 gl.disable(gl.DEPTH_TEST); gl.depthMask(false); } } else { gl.disable(gl.BLEND); gl.depthMask(true); gl.enable(gl.DEPTH_TEST); } if(params.blendFunc !== undefined){ gl.enable(gl.BLEND); gl.blendFunc(...params.blendFunc); } if(params.depthTest !== undefined){ if(params.depthTest === true){ gl.enable(gl.DEPTH_TEST); }else { gl.disable(gl.DEPTH_TEST); } } if(params.depthWrite !== undefined){ if(params.depthWrite === true){ gl.depthMask(true); }else { gl.depthMask(false); } } //--------------------------------- for (let node of nodes) { if(exports.debug.allowedNodes !== undefined){ if(!exports.debug.allowedNodes.includes(node.name)){ continue; } } let world = node.sceneNode.matrixWorld; worldView.multiplyMatrices(view, world); if (visibilityTextureData) { let vnStart = visibilityTextureData.offsets.get(node); //console.log('vnStart',vnStart) shader.setUniform1f("uVNStart", vnStart); } let level = node.getLevel(); if(node.debug){ shader.setUniform("uDebug", true); }else { shader.setUniform("uDebug", false); } // let isLeaf = false; // if(node instanceof PointCloudOctreeNode){ // isLeaf = Object.keys(node.children).length === 0; // }else if(node instanceof PointCloudArena4DNode){ // isLeaf = node.geometryNode.isLeaf; // } // shader.setUniform("uIsLeafNode", isLeaf); // let isLeaf = node.children.filter(n => n != null).length === 0; // if(!isLeaf){ // continue; // } // TODO consider passing matrices in an array to avoid uniformMatrix4fv overhead const lModel = shader.uniformLocations["modelMatrix"]; if (lModel) { mat4holder.set(world.elements); gl.uniformMatrix4fv(lModel, false, mat4holder); } const lModelView = shader.uniformLocations["modelViewMatrix"]; //mat4holder.set(worldView.elements); // faster then set in chrome 63 for(let j = 0; j < 16; j++){ mat4holder[j] = worldView.elements[j]; } gl.uniformMatrix4fv(lModelView, false, mat4holder); { // Clip Polygons if(material.clipPolygons && material.clipPolygons.length > 0){ let clipPolygonVCount = []; let worldViewProjMatrices = []; for(let clipPolygon of material.clipPolygons){ let view = clipPolygon.viewMatrix; let proj = clipPolygon.projMatrix; let worldViewProj = proj.clone().multiply(view).multiply(world); clipPolygonVCount.push(clipPolygon.markers.length); worldViewProjMatrices.push(worldViewProj); } let flattenedMatrices = [].concat(...worldViewProjMatrices.map(m => m.elements)); let flattenedVertices = new Array(8 * 3 * material.clipPolygons.length); for(let i = 0; i < material.clipPolygons.length; i++){ let clipPolygon = material.clipPolygons[i]; for(let j = 0; j < clipPolygon.markers.length; j++){ flattenedVertices[i * 24 + (j * 3 + 0)] = clipPolygon.markers[j].position.x; flattenedVertices[i * 24 + (j * 3 + 1)] = clipPolygon.markers[j].position.y; flattenedVertices[i * 24 + (j * 3 + 2)] = clipPolygon.markers[j].position.z; } } const lClipPolygonVCount = shader.uniformLocations["uClipPolygonVCount[0]"]; gl.uniform1iv(lClipPolygonVCount, clipPolygonVCount); const lClipPolygonVP = shader.uniformLocations["uClipPolygonWVP[0]"]; gl.uniformMatrix4fv(lClipPolygonVP, false, flattenedMatrices); const lClipPolygons = shader.uniformLocations["uClipPolygonVertices[0]"]; gl.uniform3fv(lClipPolygons, flattenedVertices); } } //shader.setUniformMatrix4("modelMatrix", world); //shader.setUniformMatrix4("modelViewMatrix", worldView); shader.setUniform1f("uLevel", level); shader.setUniform1f("levelPercent", octree.nodeMaxLevel ? level / octree.nodeMaxLevel : 0.5);//xzw add shader.setUniform1f("uNodeSpacing", node.geometryNode.estimatedSpacing); shader.setUniform1f("uPCIndex", i); // uBBSize if (shadowMaps.length > 0) { const lShadowMap = shader.uniformLocations["uShadowMap[0]"]; shader.setUniform3f("uShadowColor", material.uniforms.uShadowColor.value); let bindingStart = 5; let bindingPoints = new Array(shadowMaps.length).fill(bindingStart).map((a, i) => (a + i)); gl.uniform1iv(lShadowMap, bindingPoints); for (let i = 0; i < shadowMaps.length; i++) { let shadowMap = shadowMaps[i]; let bindingPoint = bindingPoints[i]; let glTexture = this.threeRenderer.properties.get(shadowMap.target.texture).__webglTexture; gl.activeTexture(gl[`TEXTURE${bindingPoint}`]); gl.bindTexture(gl.TEXTURE_2D, glTexture); } { let worldViewMatrices = shadowMaps .map(sm => sm.camera.matrixWorldInverse) .map(view => new Matrix4().multiplyMatrices(view, world)); let flattenedMatrices = [].concat(...worldViewMatrices.map(c => c.elements)); const lWorldView = shader.uniformLocations["uShadowWorldView[0]"]; gl.uniformMatrix4fv(lWorldView, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...shadowMaps.map(sm => sm.camera.projectionMatrix.elements)); const lProj = shader.uniformLocations["uShadowProj[0]"]; gl.uniformMatrix4fv(lProj, false, flattenedMatrices); } } const geometry = node.geometryNode.geometry; if(geometry.attributes["gps-time"]){ const bufferAttribute = geometry.attributes["gps-time"]; const attGPS = octree.getAttribute("gps-time"); let initialRange = attGPS.initialRange; let initialRangeSize = initialRange[1] - initialRange[0]; let globalRange = attGPS.range; let globalRangeSize = globalRange[1] - globalRange[0]; let scale = initialRangeSize / globalRangeSize; let offset = -(globalRange[0] - initialRange[0]) / initialRangeSize; scale = Number.isNaN(scale) ? 1 : scale; offset = Number.isNaN(offset) ? 0 : offset; shader.setUniform1f("uGpsScale", scale); shader.setUniform1f("uGpsOffset", offset); //shader.setUniform2f("uFilterGPSTimeClipRange", [-Infinity, Infinity]); let uFilterGPSTimeClipRange = material.uniforms.uFilterGPSTimeClipRange.value; // let gpsCliPRangeMin = uFilterGPSTimeClipRange[0] // let gpsCliPRangeMax = uFilterGPSTimeClipRange[1] // shader.setUniform2f("uFilterGPSTimeClipRange", [gpsCliPRangeMin, gpsCliPRangeMax]); let normalizedClipRange = [ (uFilterGPSTimeClipRange[0] - globalRange[0]) / globalRangeSize, (uFilterGPSTimeClipRange[1] - globalRange[0]) / globalRangeSize, ]; shader.setUniform2f("uFilterGPSTimeClipRange", normalizedClipRange); // // ranges in full gps coordinate system // const globalRange = attGPS.range; // const bufferRange = bufferAttribute.potree.range; // // ranges in [0, 1] // // normalizedGlobalRange = [0, 1] // // normalizedBufferRange: norm buffer within norm global range e.g. [0.2, 0.8] // const globalWidth = globalRange[1] - globalRange[0]; // const normalizedBufferRange = [ // (bufferRange[0] - globalRange[0]) / globalWidth, // (bufferRange[1] - globalRange[0]) / globalWidth, // ]; // shader.setUniform2f("uNormalizedGpsBufferRange", normalizedBufferRange); // let uFilterGPSTimeClipRange = material.uniforms.uFilterGPSTimeClipRange.value; // let gpsCliPRangeMin = uFilterGPSTimeClipRange[0] // let gpsCliPRangeMax = uFilterGPSTimeClipRange[1] // shader.setUniform2f("uFilterGPSTimeClipRange", [gpsCliPRangeMin, gpsCliPRangeMax]); // shader.setUniform1f("uGpsScale", bufferAttribute.potree.scale); // shader.setUniform1f("uGpsOffset", bufferAttribute.potree.offset); } { let uFilterReturnNumberRange = material.uniforms.uFilterReturnNumberRange.value; let uFilterNumberOfReturnsRange = material.uniforms.uFilterNumberOfReturnsRange.value; let uFilterPointSourceIDClipRange = material.uniforms.uFilterPointSourceIDClipRange.value; shader.setUniform2f("uFilterReturnNumberRange", uFilterReturnNumberRange); shader.setUniform2f("uFilterNumberOfReturnsRange", uFilterNumberOfReturnsRange); shader.setUniform2f("uFilterPointSourceIDClipRange", uFilterPointSourceIDClipRange); } let webglBuffer = null; if(!this.buffers.has(geometry)){ webglBuffer = this.createBuffer(geometry); this.buffers.set(geometry, webglBuffer); }else { webglBuffer = this.buffers.get(geometry); for(let attributeName in geometry.attributes){ let attribute = geometry.attributes[attributeName]; if(attribute.version > webglBuffer.vbos.get(attributeName).version){ this.updateBuffer(geometry); } } } gl.bindVertexArray(webglBuffer.vao); let isExtraAttribute = attributeLocations$1[material.activeAttributeName] === undefined && Object.keys(geometry.attributes).includes(material.activeAttributeName); if(isExtraAttribute){ const attributeLocation = attributeLocations$1["aExtra"].location; for(const attributeName in geometry.attributes){ const bufferAttribute = geometry.attributes[attributeName]; const vbo = webglBuffer.vbos.get(attributeName); gl.bindBuffer(gl.ARRAY_BUFFER, vbo.handle); gl.disableVertexAttribArray(attributeLocation); } const attName = material.activeAttributeName; const bufferAttribute = geometry.attributes[attName]; const vbo = webglBuffer.vbos.get(attName); if(bufferAttribute !== undefined && vbo !== undefined){ let type = this.glTypeMapping.get(bufferAttribute.array.constructor); let normalized = bufferAttribute.normalized; gl.bindBuffer(gl.ARRAY_BUFFER, vbo.handle); gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } { const attExtra = octree.pcoGeometry.pointAttributes.attributes .find(a => a.name === attName); let range = material.getRange(attName); if(!range){ range = attExtra.range; } if(!range){ range = [0, 1]; } let initialRange = attExtra.initialRange; let initialRangeSize = initialRange[1] - initialRange[0]; let globalRange = range; let globalRangeSize = globalRange[1] - globalRange[0]; let scale = initialRangeSize / globalRangeSize; let offset = -(globalRange[0] - initialRange[0]) / initialRangeSize; scale = Number.isNaN(scale) ? 1 : scale; offset = Number.isNaN(offset) ? 0 : offset; shader.setUniform1f("uExtraScale", scale); shader.setUniform1f("uExtraOffset", offset); } }else { for(const attributeName in geometry.attributes){ const bufferAttribute = geometry.attributes[attributeName]; const vbo = webglBuffer.vbos.get(attributeName); if(attributeLocations$1[attributeName] !== undefined){ const attributeLocation = attributeLocations$1[attributeName].location; let type = this.glTypeMapping.get(bufferAttribute.array.constructor); let normalized = bufferAttribute.normalized; gl.bindBuffer(gl.ARRAY_BUFFER, vbo.handle); gl.vertexAttribPointer(attributeLocation, bufferAttribute.itemSize, type, normalized, 0, 0); gl.enableVertexAttribArray(attributeLocation); } } } let numPoints = webglBuffer.numElements; gl.drawArrays(gl.POINTS, 0, numPoints); //gl.drawArrays(gl.TRIANGLES, 0, numPoints); i++; } gl.bindVertexArray(null); viewer.addTimeMark('renderNodes','end'); } renderOctree(octrees, nodes, camera, target, params = {}){ viewer.addTimeMark('renderOctree','start'); let octree; if(octrees instanceof Array){ octree = octrees[0]; }else { octree = octrees; octrees = [octree]; } let gl = this.gl; let material = params.material ? params.material : octree.material; let shadowMaps = params.shadowMaps == null ? [] : params.shadowMaps; let view = camera.matrixWorldInverse; let viewInv = camera.matrixWorld; if(params.viewOverride){ view = params.viewOverride; viewInv = view.clone().invert(); } let proj = camera.projectionMatrix; let projInv = proj.clone().invert(); //let worldView = new THREE.Matrix4(); let shader = null; let visibilityTextureData = null; let currentTextureBindingPoint = 0; /* if (material.pointSizeType >= 0) {//最好搬到renderNodes if (material.pointSizeType === PointSizeType.ADAPTIVE || material.activeAttributeName === "level of detail") { let vnNodes = (params.vnTextureNodes != null) ? params.vnTextureNodes : nodes; visibilityTextureData = octree.computeVisibilityTextureData(vnNodes, camera); const vnt = material.visibleNodesTexture; const data = vnt.image.data; data.set(visibilityTextureData.data); vnt.needsUpdate = true; } } */ { // UPDATE SHADER AND TEXTURES if (!this.shaders.has(material)) { let [vs, fs] = [material.vertexShader, material.fragmentShader]; let shader = new Shader$1(gl, "pointcloud", vs, fs); this.shaders.set(material, shader); } shader = this.shaders.get(material); if(material.shaderNeedsUpdate) { let [vs, fs] = [material.vertexShader, material.fragmentShader]; let numSnapshots = material.snapEnabled ? material.numSnapshots : 0; let num_in_clipboxes = (material.clipBoxes_in && material.clipBoxes_in.length) ? material.clipBoxes_in.length : 0; let num_out_clipboxes = (material.clipBoxes_out && material.clipBoxes_out.length) ? material.clipBoxes_out.length : 0; let num_highlightBox = (material.highlightBoxes && material.highlightBoxes.length) ? material.highlightBoxes.length : 0; let numClipSpheres = (params.clipSpheres && params.clipSpheres.length) ? params.clipSpheres.length : 0; let numClipPolygons = (material.clipPolygons && material.clipPolygons.length) ? material.clipPolygons.length : 0; let defines = [ `#define num_shadowmaps ${shadowMaps.length}`, `#define num_snapshots ${numSnapshots}`, `#define num_in_clipboxes ${num_in_clipboxes}`, //改 `#define num_out_clipboxes ${num_out_clipboxes}`, //改 `#define num_highlightBox ${num_highlightBox}`, //改 `#define num_clipspheres ${numClipSpheres}`, `#define num_prism ${material.prisms.pointsCount ? material.prisms.length : 0}`, //土方量数 如果num_prism>0,prismPointCountSum为0 会报错 :array size must be greater than zero `#define prismPointCountSum ${material.prisms.pointsCount}`,//点总个数 `#define prism_maxPointsCount ${material.prisms.maxPointsCount}`, //单个prism最大点个数 (如果define也能传递个数数组,就不用再uniform里传了,呜 ) ]; //add:----------- if(material.bigClipInBox ){//裁剪下载 defines.push('#define bigClipInBox'); } if(material.usePanoMap){ defines.push("#define usePanoMap"); } if(material.useFilterByNormal){ defines.push("#define use_filter_by_normal"); //Potree.settings.editType == 'pano' ? defines.push("#define attenuated_opacity2") : defines.push("#define attenuated_opacity"); } if(material.uniforms.baseHeightAreaMap.value){//根据模型高亮土方 defines.push('#define showBaseHeight'); } //--------------- if(octree.pcoGeometry.root.isLoaded()){ let attributes = octree.pcoGeometry.root.geometry.attributes; if(attributes["gps-time"]){ defines.push("#define clip_gps_enabled"); } if(attributes["return number"]){ defines.push("#define clip_return_number_enabled"); } if(attributes["number of returns"]){ defines.push("#define clip_number_of_returns_enabled"); } if(attributes["source id"] || attributes["point source id"]){ defines.push("#define clip_point_source_id_enabled"); } } let definesString = defines.join("\n"); let vsVersionIndex = vs.indexOf("#version "); let fsVersionIndex = fs.indexOf("#version "); if(vsVersionIndex >= 0){ vs = vs.replace(/(#version .*)/, `$1\n${definesString}`); }else { vs = `${definesString}\n${vs}`; } if(fsVersionIndex >= 0){ fs = fs.replace(/(#version .*)/, `$1\n${definesString}`); }else { fs = `${definesString}\n${fs}`; } shader.update(vs, fs); material.shaderNeedsUpdate = false; } for (let uniformName of Object.keys(material.uniforms)) { let uniform = material.uniforms[uniformName]; if (uniform.type == "t") { let texture = uniform.value; if (!texture) { continue; } //add if(uniformName == 'pano0Map' || uniformName == 'pano1Map' ){ //属于cubeTex,另外设置 continue } /* if(texture.image && !(texture.image instanceof Image) && !(texture instanceof THREE.CanvasTexture)){ //renderTarget的texture在创建renderTarget时已经初始化过 见setupRenderTarget continue } */ if (!this.textures.has(texture) || texture.needsRebuild) { let webglTexture = new WebGLTexture$1(gl, texture, this.threeRenderer); this.textures.set(texture, webglTexture); delete texture.needsRebuild; //renderTarget在resize后会触发dispose, 然后 _gl.deleteTexture( textureProperties.__webglTexture )所以需要重新建立 } let webGLTexture = this.textures.get(texture); webGLTexture.update(); } } } gl.useProgram(shader.program); /* let transparent = false; if(params.transparent !== undefined){ transparent = params.transparent && material.opacity < 1; }else{ transparent = material.usePanoMap ? false : (material.useFilterByNormal || material.opacity < 1); //add useFilterByNormal } if (transparent){ gl.enable(gl.BLEND); if(params.notAdditiveBlending){ gl.blendFuncSeparate( gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA, gl.ONE, gl.ONE_MINUS_SRC_ALPHA ); //NormalBlending gl.enable(gl.DEPTH_TEST); gl.depthMask(true); //如果不开启depthWrite,深度会错乱。 }else{ gl.blendFunc(gl.SRC_ALPHA, gl.ONE); //AdditiveBlending 原本 gl.disable(gl.DEPTH_TEST); gl.depthMask(false); } } else { gl.disable(gl.BLEND); gl.depthMask(true); gl.enable(gl.DEPTH_TEST); } if(params.blendFunc !== undefined){ gl.enable(gl.BLEND); gl.blendFunc(...params.blendFunc); } if(params.depthTest !== undefined){ if(params.depthTest === true){ gl.enable(gl.DEPTH_TEST); }else{ gl.disable(gl.DEPTH_TEST); } } if(params.depthWrite !== undefined){ if(params.depthWrite === true){ gl.depthMask(true); }else{ gl.depthMask(false); } } */ { // UPDATE UNIFORMS shader.setUniformMatrix4("projectionMatrix", proj); shader.setUniformMatrix4("viewMatrix", view); shader.setUniformMatrix4("uViewInv", viewInv); shader.setUniformMatrix4("uProjInv", projInv); /* let screenWidth = target ? target.width : material.screenWidth; let screenHeight = target ? target.height : material.screenHeight; shader.setUniform1f("uScreenWidth", screenWidth); shader.setUniform1f("uScreenHeight", screenHeight); */ shader.setUniform2f('resolution', material.resolution.toArray()); shader.setUniform1f("fov", Math.PI * camera.fov / 180); shader.setUniform1f("near", camera.near); shader.setUniform1f("far", camera.far); if(camera instanceof OrthographicCamera){ shader.setUniform("uUseOrthographicCamera", true); shader.setUniform("uOrthoWidth", (camera.right - camera.left) / camera.zoom); //改 shader.setUniform("uOrthoHeight", camera.top - camera.bottom); }else { shader.setUniform("uUseOrthographicCamera", false); } /* if(material.clipBoxes.length + material.clipPolygons.length === 0){//改 shader.setUniform1i("clipTask", ClipTask.NONE); }else{ shader.setUniform1i("clipTask", material.clipTask); } shader.setUniform1i("clipMethod", material.clipMethod);*/ //改 if (material.clipBoxes_in && material.clipBoxes_in.length > 0) { //let flattenedMatrices = [].concat(...material.clipBoxes.map(c => c.inverse.elements)); const lClipBoxes = shader.uniformLocations["clipBoxes_in[0]"]; gl.uniformMatrix4fv(lClipBoxes, false, material.uniforms.clipBoxes_in.value); } if (material.clipBoxes_out && material.clipBoxes_out.length > 0) {//add const lClipBoxes2 = shader.uniformLocations["clipBoxes_out[0]"]; gl.uniformMatrix4fv(lClipBoxes2, false, material.uniforms.clipBoxes_out.value); } if (material.highlightBoxes && material.highlightBoxes.length > 0) {//add const boxes_highlight = shader.uniformLocations["boxes_highlight[0]"]; gl.uniformMatrix4fv(boxes_highlight, false, material.uniforms.boxes_highlight.value); } if (material.bigClipInBox ) {//add shader.setUniformMatrix4("clipBoxBig_in", material.uniforms.clipBoxBig_in.value); } if(material.uniforms.baseHeightAreaMap.value ){//根据模型高亮土方 const baseHeightBoundZ = shader.uniformLocations["baseHeightBoundZ"]; gl.uniform2f(baseHeightBoundZ, ...material.uniforms.baseHeightBoundZ.value.toArray()); const baseHeightBoundXY = shader.uniformLocations["baseHeightBoundXY"]; gl.uniform4f(baseHeightBoundXY, ...material.uniforms.baseHeightBoundXY.value.toArray()); } if(material.prisms.length){ const prismList = shader.uniformLocations["prismList[0]"]; gl.uniformMatrix3fv(prismList, false, material.uniforms.prismList.value); const prismPoints = shader.uniformLocations["prismPoints[0]"]; gl.uniform2fv(prismPoints, material.uniforms.prismPoints.value); } // TODO CLIPSPHERES if(params.clipSpheres && params.clipSpheres.length > 0){ let clipSpheres = params.clipSpheres; let matrices = []; for(let clipSphere of clipSpheres){ //let mScale = new THREE.Matrix4().makeScale(...clipSphere.scale.toArray()); //let mTranslate = new THREE.Matrix4().makeTranslation(...clipSphere.position.toArray()); //let clipToWorld = new THREE.Matrix4().multiplyMatrices(mTranslate, mScale); let clipToWorld = clipSphere.matrixWorld; let viewToWorld = camera.matrixWorld; let worldToClip = clipToWorld.clone().invert(); let viewToClip = new Matrix4().multiplyMatrices(worldToClip, viewToWorld); matrices.push(viewToClip); } let flattenedMatrices = [].concat(...matrices.map(matrix => matrix.elements)); const lClipSpheres = shader.uniformLocations["uClipSpheres[0]"]; gl.uniformMatrix4fv(lClipSpheres, false, flattenedMatrices); //const lClipSpheres = shader.uniformLocations["uClipSpheres[0]"]; //gl.uniformMatrix4fv(lClipSpheres, false, material.uniforms.clipSpheres.value); } shader.setUniform1f("orthoMaxSize", material.uniforms.orthoMaxSize.value); shader.setUniform1f("maxSize", material.uniforms.maxSize.value); shader.setUniform1f("minSize", material.uniforms.minSize.value); // uniform float uPCIndex shader.setUniform1f("uOctreeSpacing", material.spacing); shader.setUniform("uOctreeSize", material.uniforms.octreeSize.value); shader.setUniform2f("elevationRange", material.elevationRange); shader.setUniform2f("intensityRange", material.intensityRange); shader.setUniform3f("uIntensity_gbc", [ material.intensityGamma, material.intensityBrightness, material.intensityContrast ]); shader.setUniform3f("uRGB_gbc", [ material.rgbGamma, material.rgbBrightness, material.rgbContrast ]); shader.setUniform1f("uTransition", material.transition); shader.setUniform1f("wRGB", material.weightRGB); shader.setUniform1f("wIntensity", material.weightIntensity); shader.setUniform1f("wElevation", material.weightElevation); shader.setUniform1f("wClassification", material.weightClassification); shader.setUniform1f("wReturnNumber", material.weightReturnNumber); shader.setUniform1f("wSourceID", material.weightSourceID); shader.setUniform("backfaceCulling", material.uniforms.backfaceCulling.value); //========================== //gl.TEXTURE_CUBE_MAP: 34067 //gl.TEXTURE0=33984 , vnWebGLTexture.target=gl.TEXTURE_2D = 3353 /* let vnWebGLTexture = this.textures.get(material.visibleNodesTexture);//最好搬到renderNodes if(vnWebGLTexture){ shader.setUniform1i("visibleNodes", currentTextureBindingPoint); //为何之前写的是"visibleNodesTexture",但和"visibleNodes"效果相同?可shader里只有"visibleNodes" gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(vnWebGLTexture.target, vnWebGLTexture.id); currentTextureBindingPoint++; } */ let gradientTexture = this.textures.get(material.gradientTexture); shader.setUniform1i("gradient", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(gradientTexture.target, gradientTexture.id); currentTextureBindingPoint++; const repeat = material.elevationGradientRepeat; if(repeat === ElevationGradientRepeat.REPEAT){ gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_S, gl.REPEAT); gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_T, gl.REPEAT); }else if(repeat === ElevationGradientRepeat.MIRRORED_REPEAT){ gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_S, gl.MIRRORED_REPEAT); gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_T, gl.MIRRORED_REPEAT); }else { gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE); gl.texParameteri(gradientTexture.target, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE); } currentTextureBindingPoint++; let classificationTexture = this.textures.get(material.classificationTexture); shader.setUniform1i("classificationLUT", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(classificationTexture.target, classificationTexture.id); currentTextureBindingPoint++; /* let matcapTexture = this.textures.get(material.matcapTexture); shader.setUniform1i("matcapTextureUniform", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(matcapTexture.target, matcapTexture.id); currentTextureBindingPoint++; */ let baseHeightAreaMap = material.uniforms.baseHeightAreaMap.value; if(baseHeightAreaMap){//根据模型高亮土方 let map = this.textures.get(baseHeightAreaMap); shader.setUniform1i("baseHeightAreaMap", currentTextureBindingPoint); gl.activeTexture(gl.TEXTURE0 + currentTextureBindingPoint); gl.bindTexture(map.target, map.id); currentTextureBindingPoint++; } if (material.snapEnabled === true) { { const lSnapshot = shader.uniformLocations["uSnapshot[0]"]; const lSnapshotDepth = shader.uniformLocations["uSnapshotDepth[0]"]; let bindingStart = currentTextureBindingPoint; let lSnapshotBindingPoints = new Array(5).fill(bindingStart).map((a, i) => (a + i)); let lSnapshotDepthBindingPoints = new Array(5) .fill(1 + Math.max(...lSnapshotBindingPoints)) .map((a, i) => (a + i)); currentTextureBindingPoint = 1 + Math.max(...lSnapshotDepthBindingPoints); gl.uniform1iv(lSnapshot, lSnapshotBindingPoints); gl.uniform1iv(lSnapshotDepth, lSnapshotDepthBindingPoints); for (let i = 0; i < 5; i++) { let texture = material.uniforms[`uSnapshot`].value[i]; let textureDepth = material.uniforms[`uSnapshotDepth`].value[i]; if (!texture) { break; } let snapTexture = this.threeRenderer.properties.get(texture).__webglTexture; let snapTextureDepth = this.threeRenderer.properties.get(textureDepth).__webglTexture; let bindingPoint = lSnapshotBindingPoints[i]; let depthBindingPoint = lSnapshotDepthBindingPoints[i]; gl.activeTexture(gl[`TEXTURE${bindingPoint}`]); gl.bindTexture(gl.TEXTURE_2D, snapTexture); gl.activeTexture(gl[`TEXTURE${depthBindingPoint}`]); gl.bindTexture(gl.TEXTURE_2D, snapTextureDepth); } } { let flattenedMatrices = [].concat(...material.uniforms.uSnapView.value.map(c => c.elements)); const lSnapView = shader.uniformLocations["uSnapView[0]"]; gl.uniformMatrix4fv(lSnapView, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...material.uniforms.uSnapProj.value.map(c => c.elements)); const lSnapProj = shader.uniformLocations["uSnapProj[0]"]; gl.uniformMatrix4fv(lSnapProj, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...material.uniforms.uSnapProjInv.value.map(c => c.elements)); const lSnapProjInv = shader.uniformLocations["uSnapProjInv[0]"]; gl.uniformMatrix4fv(lSnapProjInv, false, flattenedMatrices); } { let flattenedMatrices = [].concat(...material.uniforms.uSnapViewInv.value.map(c => c.elements)); const lSnapViewInv = shader.uniformLocations["uSnapViewInv[0]"]; gl.uniformMatrix4fv(lSnapViewInv, false, flattenedMatrices); } } //=============add=========== if(material.usePanoMap){//为什么pointsize失效 shader.setUniform1f("progress", material.uniforms.progress.value); shader.setUniform1f("easeInOutRatio", material.uniforms.easeInOutRatio.value); shader.setUniform3f("pano0Position", material.uniforms.pano0Position.value.toArray()); shader.setUniform3f("pano1Position", material.uniforms.pano1Position.value.toArray()); shader.setUniform('pano0Matrix', material.uniforms.pano0Matrix.value); shader.setUniform('pano1Matrix', material.uniforms.pano1Matrix.value); let pano0Map = material.uniforms.pano0Map.value; if(pano0Map){ this.threeRenderer._textures.safeSetTextureCube( pano0Map, ++currentTextureBindingPoint ); shader.setUniform1i('pano0Map', currentTextureBindingPoint); } let pano1Map = material.uniforms.pano1Map.value; if(pano1Map){ this.threeRenderer._textures.safeSetTextureCube( pano1Map, ++currentTextureBindingPoint ); shader.setUniform1i('pano1Map', currentTextureBindingPoint); } //注: three.js我添加了个 _textures, safeSetTextureCube里主要就是activeTexture和bindTexture } } viewer.addTimeMark('renderOctree','end'); params.currentTextureBindingPoint = ++currentTextureBindingPoint; octrees.forEach(octree=>{ this.renderNodes(octree, nodes || octree.visibleNodes, visibilityTextureData, camera, target, shader, params); }); gl.activeTexture(gl.TEXTURE2); gl.bindTexture(gl.TEXTURE_2D, null); gl.activeTexture(gl.TEXTURE0); //gl.bindTexture(gl.TEXTURE_2D, null); //add //add 恢复为不透明(否则renderToCubeMap时的贴图会被渲染成高亮的颜色) gl.disable(gl.BLEND); gl.depthMask(true); gl.enable(gl.DEPTH_TEST); //DEPTH_TEST等需要恢复吗 } render(scene, camera, target = null, params = {}) { const gl = this.gl; // PREPARE if (target != null) { this.threeRenderer.setRenderTarget(target); } //camera.updateProjectionMatrix(); // camera.matrixWorldInverse.invert(camera.matrixWorld); const traversalResult = this.traverse(scene); //排序 if(params.notAdditiveBlending){//add traversalResult.octrees.forEach(tree=>{ if(tree.material.opacity==1){ tree._z = Infinity; //不透明的先渲染 }else { let center = tree.boundCenter ? tree.boundCenter.clone() : tree.boundingBox.getCenter(tree.boundCenter).applyMatrix4(tree.matrixWorld); center.project(camera); tree._z = center.z; } }); traversalResult.octrees.sort((tree1,tree2)=>{ return tree2._z - tree1._z //降序 (-1 朝外)。 离屏幕近的后渲染 }); } // RENDER let mat = params.material || traversalResult.octrees[0].material; if(Potree.settings.cloudSameMat && viewer.scene.volumes.length == 0 && mat.pointSizeType != PointSizeType.ADAPTIVE && mat.activeAttributeName != "level of detail"){ this.renderOctree(traversalResult.octrees, null, camera, target, params); //所有点云除了个别属性需要在shader中更新,其他都使用第一个点云的材质 }else for (const octree of traversalResult.octrees) { for (const octree of traversalResult.octrees) { this.renderOctree(octree, octree.visibleNodes, camera, target, params); } } //if (octree material.pointSizeType === PointSizeType.ADAPTIVE || material.activeAttributeName === "level of detail") { // CLEANUP gl.activeTexture(gl.TEXTURE1); gl.bindTexture(gl.TEXTURE_2D, null); gl.bindBuffer(gl.ARRAY_BUFFER, null); gl.bindVertexArray(null); this.threeRenderer.resetState(); } }; /* 中东的链接http://indoor.popsmart.cn:8094/zdoblh-yz/?vlon=5.14&vlat=-0.13&fov=100.0&pc=true&lon=121.61136592&lat=29.87855579&z=16.577 geometry: 有的attributes: 属性是: classification: color: indices: normal: position: 最好有个spacing */ // // Algorithm by Christian Boucheny // shader code taken and adapted from CloudCompare // // see // https://github.com/cloudcompare/trunk/tree/master/plugins/qEDL/shaders/EDL // http://www.kitware.com/source/home/post/9 // https://tel.archives-ouvertes.fr/tel-00438464/document p. 115+ (french) class ExtendEyeDomeLightingMaterial extends EyeDomeLightingMaterial{ constructor(parameters = {}){ super(parameters); delete this.uniforms.screenWidth; delete this.uniforms.screenHeight; this.uniforms.resolution = { type: 'v2', value: new Vector2()}; this.uniforms.useEDL = { type: 'i', value: 1 }; this.vertexShader = this.getDefines() + Shaders['edl_new.vs']; //改 this.fragmentShader = this.getDefines() + Shaders['edl_new.fs']; //改 } } const copyShader = { uniforms: { tDiffuse: { type: "t", value: null }, opacity: { type: "f", value: 1 }, depthTex: { type: "t", value: null } }, vertexShader: ` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); } `, fragmentShader: ` #extension GL_EXT_frag_depth : enable uniform float opacity; uniform sampler2D tDiffuse; #if defined(GL_EXT_frag_depth) && defined(useDepth) uniform sampler2D depthTex; #endif varying vec2 vUv; void main() { #if defined(GL_EXT_frag_depth) && defined(useDepth) float depth = texture2D(depthTex, vUv).r; /* if(depth >= 1.0){ //超级远(但是在modelTex里我把天空距离超出far了,所以不删) discard; } */ gl_FragDepthEXT = depth; #endif vec4 texel = texture2D( tDiffuse, vUv ); gl_FragColor = opacity * texel; } ` }; //import DepthTexSampler from "../custom/utils/DepthTexSampler.js"; class EDLRenderer{//Eye-Dome Lighting 眼罩照明 constructor(viewer){ this.viewer = viewer; this.edlMaterial = null; //this.rtRegular; this.rtEDLs = new Map; this.gl = viewer.renderer.getContext(); //反正也没用到,注释了: //this.shadowMap = new PointCloudSM(this.viewer.pRenderer); viewer.addEventListener('resize',this.resize.bind(this)); this.initEDL(viewer); } initEDL(viewer){ if (this.edlMaterial != null || !Features.EXT_DEPTH.isSupported()){ return; } this.edlMaterial = new ExtendEyeDomeLightingMaterial(); this.edlMaterial.depthTest = true; this.edlMaterial.depthWrite = true; this.edlMaterial.transparent = true; let copyUniforms = UniformsUtils.clone( copyShader.uniforms ); let {vs,fs} = Common.changeShaderToWebgl2(copyShader.vertexShader, copyShader.fragmentShader, 'ShaderMaterial'); this.recoverToScreenMat = new ShaderMaterial({ uniforms: copyUniforms, vertexShader: vs, fragmentShader: fs, transparent: true, defines:{ useDepth: true //开启后,其他物体才能被遮挡 } }); /* let copyUniforms = THREE.UniformsUtils.clone( copyShader.uniforms ); this.copyMaterial = new THREE.ShaderMaterial( { uniforms: copyUniforms, vertexShader: copyShader.vertexShader, fragmentShader: copyShader.fragmentShader, //premultipliedAlpha: true, transparent: true, //blending: THREE.AdditiveBlending, depthTest: false, depthWrite: false }); */ if(Potree.settings.useRTskybox != Potree.settings.useRTPoint){//如果两个只开了一个 viewer.images360.addEventListener('endChangeMode',()=>{ this.resize({viewport:viewer.mainViewport}); }); } //this.depthTexSampler = new DepthTexSampler(this); }; resize(e){ if(Features.EXT_DEPTH.isSupported()){ let viewport = e.viewport; let size = ( Potree.settings.displayMode == 'showPanos' ? Potree.settings.useRTskybox : Potree.settings.useRTPoint) ? viewport.resolution2 : viewport.resolution; //若要渲染skybox,需要和设备一样精度的rt this.getRtEDL(viewport).setSize( size.x, size.y ); //理论上可以是任意尺寸,但会影响精度,且aspect最好和渲染的一致 } } clearTargets(params={}){ const viewer = this.viewer; const {renderer} = viewer; const oldTarget = renderer.getRenderTarget(); if(params.target){//add renderer.setRenderTarget( params.target); renderer.clear(); } if(Features.EXT_DEPTH.isSupported()){ if(params.rtEDL){ renderer.setRenderTarget( params.rtEDL); renderer.clear(); }else { var rtEDL = this.getRtEDL(params.viewport); if(rtEDL){ renderer.setRenderTarget( rtEDL ); renderer.setClearAlpha(0); renderer.clear( true, true, true ); } } } //renderer.setRenderTarget( this.rtRegular ); //renderer.clear( true, true, false ); renderer.setRenderTarget(oldTarget); } getRtEDL(viewport){//根据不同viewport返回rtEDL的texture if(!viewport){ console.warn('getRtEDL没传viewport!!!! !!!!!!!!!!'); viewport = viewer.mainViewport; } var rtEDL = this.rtEDLs.get(viewport); if(!rtEDL){ if(Features.EXT_DEPTH.isSupported()){ rtEDL = new WebGLRenderTarget(viewport.resolution.x, viewport.resolution.y, { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat, type: FloatType, depthTexture: new DepthTexture(undefined, undefined, UnsignedIntType) }); //注: 部分手机在resize时会崩溃,经检验去掉rtEDL的resize可以解决,所以更应该注释掉这个 this.rtEDLs.set(viewport, rtEDL); } } //注:当pc窗口缩小,deviceRatio变小后,resolution2会小于resolution,然后遮挡会出现精度损失而有细微不准,是正常现象。 return rtEDL } renderShadowMap(visiblePointClouds, camera, lights){ const {viewer} = this; const doShadows = lights.length > 0 && !(lights[0].disableShadowUpdates); if(doShadows){ let light = lights[0]; this.shadowMap.setLight(light); let originalAttributes = new Map(); for(let pointcloud of viewer.scene.pointclouds){ // TODO IMPORTANT !!! check originalAttributes.set(pointcloud, pointcloud.material.activeAttributeName); pointcloud.material.disableEvents(); pointcloud.material.activeAttributeName = "depth"; //pointcloud.material.pointColorType = PointColorType.DEPTH; } this.shadowMap.render(viewer.scene.scenePointCloud, camera); for(let pointcloud of visiblePointClouds){ let originalAttribute = originalAttributes.get(pointcloud); // TODO IMPORTANT !!! check pointcloud.material.activeAttributeName = originalAttribute; pointcloud.material.enableEvents(); } viewer.shadowTestCam.updateMatrixWorld(); viewer.shadowTestCam.matrixWorldInverse.copy(viewer.shadowTestCam.matrixWorld).invert(); viewer.shadowTestCam.updateProjectionMatrix(); } } render(params={}){ const viewer = this.viewer; let camera = params.camera ? params.camera : viewer.scene.getActiveCamera(); let rtEDL = (Potree.settings.pointEnableRT || Potree.settings.displayMode == 'showPanos' || viewer.useEDL) && Features.EXT_DEPTH.isSupported() && camera.type != "OrthographicCamera" && !params.dontRenderRtEDL && (params.rtEDL || this.getRtEDL(params.viewport)); // 平面相机不用depthTex直接打开depthTest?且不使用edl let useEDL = viewer.useEDL && rtEDL && Potree.settings.displayMode != 'showPanos'; let target = params.target || null; const resolution = (rtEDL && Potree.settings.useRTPoint) ? new Vector2(rtEDL.width,rtEDL.height) : params.target ? new Vector2(params.target.width, params.target.height ) : params.viewport ? params.viewport.resolution2 : this.viewer.renderer.getSize(new Vector2());//截图时需要用target的大小 viewer.renderer.setRenderTarget(target); //viewer.dispatchEvent({type: "render.pass.begin",viewer: viewer}); let lights = []; /* viewer.scene.scene.traverse(node => { if(node.type === "SpotLight"){ lights.push(node); } }); */ //skybox 全景图 if(!params.magnifier){ if(Potree.settings.displayMode == 'showPanos' || Potree.settings.testCube ){ Potree.Utils.setCameraLayers(camera, ['skybox']); if((Potree.settings.displayMode == 'showPanos' )&& viewer.images360.currentPano.pointcloud.hasDepthTex && rtEDL){//渲染深度图 viewer.renderer.setRenderTarget(rtEDL); //将带有深度图的skybox画在rtEDL一下,这样就不需要绘制后边的点云了 viewer.renderer.render(viewer.scene.scene, camera); viewer.renderer.setRenderTarget(target); if(Potree.settings.useRTskybox){//直接使用rtEDL,但是会失去抗锯齿,不知在skybox上需要抗锯齿吗 this.recoverToScreenMat.uniforms.depthTex.value = rtEDL.depthTexture; this.recoverToScreenMat.uniforms.tDiffuse.value = rtEDL.texture; Utils.screenPass.render(viewer.renderer, this.recoverToScreenMat, target); }else { viewer.renderer.render(viewer.scene.scene, camera); } }else { viewer.renderer.render(viewer.scene.scene, camera); } if(Potree.settings.displayMode == 'showPanos' ){ if(Potree.settings.fastTran && viewer.images360.fastTranMaskPass.enabled){ viewer.images360.fastTranMaskPass.render(); } return } } } const visiblePointClouds2 = viewer.scene.pointclouds.filter(pc => Potree.Utils.getObjVisiByReason(pc,'datasetSelection') ); //需要绘制到rtEDL的 const showPointClouds = params.magnifier ? visiblePointClouds2.length>0 : viewer.scene.pointclouds.some(e=>e.visible); //是否有需要绘制到屏幕的 visiblePointClouds2.forEach(e=>{//为了绘制到depthTexture,先显示(展示全景图时隐藏了点云,所以需要显示下。且放大镜需要绘制点云) e.oldVisi = e.visible; e.visible = true; }); Potree.Utils.setCameraLayers(camera, ['pointcloud']); //设置多少都会渲染出来,因为渲染里没有sort //camera.layers.set(Potree.config.renderLayers.pointcloud); //TODO adapt to multiple lights //this.renderShadowMap(visiblePointClouds2, camera, lights); //??????? { for (let pointcloud of visiblePointClouds2) { let material = pointcloud.material; let octreeSize = pointcloud.pcoGeometry.boundingBox.getSize(new Vector3()).x; material.fov = MathUtils.degToRad(camera.fov); material.resolution = resolution; material.spacing = pointcloud.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 = octreeSize; if(useEDL ){ material.useEDL = true; //material.fakeEDL = false; //add }else { material.useEDL = false; //material.fakeEDL = true; //add 使也输出深度 } } if(rtEDL ){ //借用rtEDL存储深度信息 viewer.renderer.setRenderTarget( rtEDL ); if(visiblePointClouds2.length>0){ //渲染scenePointCloud到rtEDL viewer.pRenderer.render(viewer.scene.scenePointCloud, camera, rtEDL, { shadowMaps: lights.length > 0 ? [this.shadowMap] : null, clipSpheres: viewer.scene.volumes.filter(v => (v instanceof SphereVolume$1)), transparent: true, //如果点云透明需要透明 notAdditiveBlending: Potree.settings.notAdditiveBlending }); } if(Potree.settings.intersectOnObjs){// model也要渲染到rtEDL Potree.Utils.setCameraLayers(camera, ['model','light']); viewer.objs.traverse(e=>{if(e.material)e._OlddepthWrite = e.material.depthWrite, e.material.depthWrite = true;}); //否则半透明的mesh无法遮住测量线 viewer.renderer.render(viewer.scene.scene, camera); viewer.objs.traverse(e=>{if(e.material)e.material.depthWrite = e._OlddepthWrite;}); //缺点:半透明的model 就算完全透明, 也会遮住测量线 } } } //渲染到rtEDL完毕 viewer.dispatchEvent({type: "render.pass.scene", viewer: viewer }); viewer.renderer.setRenderTarget( target ); if(!params.magnifier)visiblePointClouds2.forEach(e=>{//放大镜显示点云 e.visible = e.oldVisi; }); if(showPointClouds){ //绘制点云到画布 if(useEDL){ //设置edlMaterial //Features.EXT_DEPTH不支持的话不会到这一块 const uniforms = this.edlMaterial.uniforms; uniforms.resolution.value.copy(resolution); uniforms.edlStrength.value = viewer.edlStrength; uniforms.radius.value = viewer.edlRadius; uniforms.useEDL.value = 1;//add let proj = camera.projectionMatrix; let projArray = new Float32Array(16); projArray.set(proj.elements); uniforms.uProj.value = projArray; uniforms.uEDLColor.value = rtEDL.texture; uniforms.opacity.value = viewer.edlOpacity; // HACK Utils.screenPass.render(viewer.renderer, this.edlMaterial, target); //相当于一个描边后期特效。 缺点: 因为target上的没有抗锯齿,所以点云在晃动镜头时会不稳定地闪烁1px位置。优点:比不打开edl少绘制一次点云,更流畅了?! }else if(Potree.settings.useRTPoint && rtEDL){ this.recoverToScreenMat.uniforms.tDiffuse.value = rtEDL.texture; if(this.recoverToScreenMat.defines.useDepth){ this.recoverToScreenMat.uniforms.depthTex.value = rtEDL.depthTexture; } Utils.screenPass.render(viewer.renderer, this.recoverToScreenMat, target/* , Potree.settings.useFxaa && viewer.composer2 */); params.drawedModelOnRT = Potree.settings.intersectOnObjs; }else { //渲染点云 (直接用rtEDL上的会失去抗锯齿, 导致频闪、密集时出现条纹, 自己写抗锯齿也要渲染好几次。另外透明度也要处理下) let prop = { shadowMaps: lights.length > 0 ? [this.shadowMap] : null, clipSpheres: viewer.scene.volumes.filter(v => (v instanceof SphereVolume$1)) , notAdditiveBlending: Potree.settings.notAdditiveBlending//add 否则透明的点云会挡住后面的模型。 加上这句后竟然透明不会叠加了! }; viewer.pRenderer.render(viewer.scene.scenePointCloud, camera, null , prop); } } visiblePointClouds2.forEach(e=>{ e.visible = e.oldVisi; }); //viewer.dispatchEvent({type: "render.pass.end",viewer: viewer}); } /* 渲染顺序: 底层:背景 -> skybox(也可中间) 中间层(含有深度信息):1 点云、marker等mesh, 2 测量线(现在被做成借用depthTex 顶层:maginifier magnifier的贴图渲染不需要顶层、中间层只需要点云。 */ } class NavigationCube extends Object3D { constructor(viewer){ super(); this.viewer = viewer; let createPlaneMaterial = (img) => { let material = new MeshBasicMaterial( { depthTest: true, depthWrite: true, side: DoubleSide }); new TextureLoader().load( exports.resourcePath + '/textures/navigation/' + img, function(texture) { texture.anisotropy = viewer.renderer.capabilities.getMaxAnisotropy(); material.map = texture; material.needsUpdate = true; }); return material; }; let planeGeometry = new PlaneGeometry(1, 1); this.front = new Mesh(planeGeometry, createPlaneMaterial('F.png')); this.front.position.y = -0.5; this.front.rotation.x = Math.PI / 2.0; this.front.updateMatrixWorld(); this.front.name = "F"; this.add(this.front); this.back = new Mesh(planeGeometry, createPlaneMaterial('B.png')); this.back.position.y = 0.5; this.back.rotation.x = Math.PI / 2.0; this.back.updateMatrixWorld(); this.back.name = "B"; this.add(this.back); this.left = new Mesh(planeGeometry, createPlaneMaterial('L.png')); this.left.position.x = -0.5; this.left.rotation.y = Math.PI / 2.0; this.left.updateMatrixWorld(); this.left.name = "L"; this.add(this.left); this.right = new Mesh(planeGeometry, createPlaneMaterial('R.png')); this.right.position.x = 0.5; this.right.rotation.y = Math.PI / 2.0; this.right.updateMatrixWorld(); this.right.name = "R"; this.add(this.right); this.bottom = new Mesh(planeGeometry, createPlaneMaterial('D.png')); this.bottom.position.z = -0.5; this.bottom.updateMatrixWorld(); this.bottom.name = "D"; this.add(this.bottom); this.top = new Mesh(planeGeometry, createPlaneMaterial('U.png')); this.top.position.z = 0.5; this.top.updateMatrixWorld(); this.top.name = "U"; this.add(this.top); this.width = 150; // in px this.camera = new OrthographicCamera(-1, 1, 1, -1, -1, 1); this.camera.position.copy(new Vector3(0, 0, 0)); this.camera.lookAt(new Vector3(0, 1, 0)); this.camera.updateMatrixWorld(); this.camera.rotation.order = "ZXY"; let onMouseDown = (event) => { if (!this.visible) { return; } this.pickedFace = null; let mouse = new Vector2(); mouse.x = event.clientX - (window.innerWidth - this.width); mouse.y = event.clientY; if(mouse.x < 0 || mouse.y > this.width) return; mouse.x = (mouse.x / this.width) * 2 - 1; mouse.y = -(mouse.y / this.width) * 2 + 1; let raycaster = new Raycaster(); raycaster.setFromCamera(mouse, this.camera); raycaster.ray.origin.sub(this.camera.getWorldDirection(new Vector3())); let intersects = raycaster.intersectObjects(this.children); let minDistance = 1000; for (let i = 0; i < intersects.length; i++) { if(intersects[i].distance < minDistance) { this.pickedFace = intersects[i].object.name; minDistance = intersects[i].distance; } } if(this.pickedFace) { this.viewer.setView(this.pickedFace); } }; this.viewer.renderer.domElement.addEventListener('mousedown', onMouseDown, false); } update(rotation) { this.camera.rotation.copy(rotation); this.visible && this.camera.updateMatrixWorld(); } } /** * * @author sigeom sa / http://sigeom.ch * @author Ioda-Net Sàrl / https://www.ioda-net.ch/ * @author Markus Schütz / http://potree.org * */ class GeoJSONExporter{ static measurementToFeatures (measurement) { let coords = measurement.points.map(e => e.position.toArray()); let features = []; if (coords.length === 1) { let feature = { type: 'Feature', geometry: { type: 'Point', coordinates: coords[0] }, properties: { name: measurement.name } }; features.push(feature); } else if (coords.length > 1 && !measurement.closed) { let object = { 'type': 'Feature', 'geometry': { 'type': 'LineString', 'coordinates': coords }, 'properties': { name: measurement.name } }; features.push(object); } else if (coords.length > 1 && measurement.closed) { let object = { 'type': 'Feature', 'geometry': { 'type': 'Polygon', 'coordinates': [[...coords, coords[0]]] }, 'properties': { name: measurement.name } }; features.push(object); } if (measurement.showDistances) { measurement.edgeLabels.forEach((label) => { let labelPoint = { type: 'Feature', geometry: { type: 'Point', coordinates: label.position.toArray() }, properties: { distance: label.text } }; features.push(labelPoint); }); } if (measurement.showArea) { let point = measurement.areaLabel.position; let labelArea = { type: 'Feature', geometry: { type: 'Point', coordinates: point.toArray() }, properties: { area: measurement.areaLabel.text } }; features.push(labelArea); } return features; } static toString (measurements) { if (!(measurements instanceof Array)) { measurements = [measurements]; } measurements = measurements.filter(m => m instanceof Measure); let features = []; for (let measure of measurements) { let f = GeoJSONExporter.measurementToFeatures(measure); features = features.concat(f); } let geojson = { 'type': 'FeatureCollection', 'features': features }; return JSON.stringify(geojson, null, '\t'); } } /** * * @author sigeom sa / http://sigeom.ch * @author Ioda-Net Sàrl / https://www.ioda-net.ch/ * @author Markus Schuetz / http://potree.org * */ class DXFExporter { static measurementPointSection (measurement) { let position = measurement.points[0].position; if (!position) { return ''; } let dxfSection = `0 CIRCLE 8 layer_point 10 ${position.x} 20 ${position.y} 30 ${position.z} 40 1.0 `; return dxfSection; } static measurementPolylineSection (measurement) { // bit code for polygons/polylines: // https://www.autodesk.com/techpubs/autocad/acad2000/dxf/polyline_dxf_06.htm let geomCode = 8; if (measurement.closed) { geomCode += 1; } let dxfSection = `0 POLYLINE 8 layer_polyline 62 1 66 1 10 0.0 20 0.0 30 0.0 70 ${geomCode} `; let xMax = 0.0; let yMax = 0.0; let zMax = 0.0; for (let point of measurement.points) { point = point.position; xMax = Math.max(xMax, point.x); yMax = Math.max(yMax, point.y); zMax = Math.max(zMax, point.z); dxfSection += `0 VERTEX 8 0 10 ${point.x} 20 ${point.y} 30 ${point.z} 70 32 `; } dxfSection += `0 SEQEND `; return dxfSection; } static measurementSection (measurement) { // if(measurement.points.length <= 1){ // return ""; // } if (measurement.points.length === 0) { return ''; } else if (measurement.points.length === 1) { return DXFExporter.measurementPointSection(measurement); } else if (measurement.points.length >= 2) { return DXFExporter.measurementPolylineSection(measurement); } } static toString(measurements){ if (!(measurements instanceof Array)) { measurements = [measurements]; } measurements = measurements.filter(m => m instanceof Measure); let points = measurements.filter(m => (m instanceof Measure)) .map(m => m.points) .reduce((a, v) => a.concat(v)) .map(p => p.position); let min = new Vector3(Infinity, Infinity, Infinity); let max = new Vector3(-Infinity, -Infinity, -Infinity); for (let point of points) { min.min(point); max.max(point); } let dxfHeader = `999 DXF created from potree 0 SECTION 2 HEADER 9 $ACADVER 1 AC1006 9 $INSBASE 10 0.0 20 0.0 30 0.0 9 $EXTMIN 10 ${min.x} 20 ${min.y} 30 ${min.z} 9 $EXTMAX 10 ${max.x} 20 ${max.y} 30 ${max.z} 0 ENDSEC `; let dxfBody = `0 SECTION 2 ENTITIES `; for (let measurement of measurements) { dxfBody += DXFExporter.measurementSection(measurement); } dxfBody += `0 ENDSEC `; let dxf = dxfHeader + dxfBody + '0\nEOF'; return dxf; } } class HandleSvg extends EventDispatcher{ constructor(position, color){ super(); this.position = position; this.color = '#'+new Color(color).getHexString(); this.svg = this.create(); this.visible_ = true; let update = ()=>{ this.update(); }; viewer.addEventListener("camera_changed", update); this.addEventListener('dispose', ()=>{ viewer.removeEventListener("camera_changed", update); }); } create(){ const svgns = "http://www.w3.org/2000/svg"; const svg = document.createElementNS(svgns, "svg"); svg.setAttribute("width", "2em"); svg.setAttribute("height", "2em"); svg.setAttribute("position", "absolute"); svg.style.left = "50px"; svg.style.top = "50px"; svg.style.position = "absolute"; svg.style.zIndex = "10000"; svg.style.cursor = 'grab'; svg.style.transform = 'translate(-50%,-50%)'; const circle = document.createElementNS(svgns, 'circle'); circle.setAttributeNS(null, 'cx', "1em"); circle.setAttributeNS(null, 'cy', "1em"); circle.setAttributeNS(null, 'r', "0.5em"); circle.setAttributeNS(null, 'style', 'fill: '+this.color+'; stroke: black; stroke-width: 0.2em;' ); svg.appendChild(circle); const element = viewer.renderer.domElement.parentElement; element.appendChild(svg); const startDrag = (evt) => { /* if(evt.button === THREE.MOUSE.RIGHT){ return } */ this.selectedElement = svg; document.addEventListener("mousemove", drag); }; const endDrag = (evt) => { this.selectedElement = null; document.removeEventListener("mousemove", drag); }; const drag = (evt) => { if (this.selectedElement) { evt.preventDefault(); const rect = viewer.renderer.domElement.getBoundingClientRect(); const x = evt.clientX - rect.x; const y = evt.clientY - rect.y; const {width, height} = viewer.renderer.getSize(new Vector2()); const camera = viewer.scene.getActiveCamera(); const projected = this.position.clone().project(camera); projected.x = ((x / width) - 0.5) / 0.5; projected.y = (-(y - height) / height - 0.5) / 0.5; const unprojected = projected.clone().unproject(camera); this.position.set(unprojected.x, unprojected.y, unprojected.z); this.update(); this.dispatchEvent({type:'dragged', position: this.position}); } }; svg.addEventListener('mousedown', startDrag); svg.addEventListener('mouseup', endDrag); svg.style.display = this.visible ? "" : "none"; this.addEventListener('dispose',()=>{ svg.removeEventListener('mousedown', startDrag); svg.removeEventListener('mouseup', endDrag); }); return svg } set visible(v){ this.visible_ = v; if(v){ this.update(); }else { this.svg.style.display = "none"; } } get visible(){ return this.visible_ } update(){ if(!this.visible)return let camera = viewer.scene.getActiveCamera(); var p = Potree.Utils.getPos2d( this.position, viewer.mainViewport, viewer.renderArea); if(!p.trueSide){ return this.svg.style.display = 'none'; } this.svg.style.left = p.posInViewport.x; this.svg.style.top = p.posInViewport.y; this.svg.style.display = ''; } dispose(){ this.svg.remove(); this.dispatchEvent('dispose'); } } /* 两种拖拽方式: 1 只依附在点云上 2 平行于镜头view移动 */ const geo$1 = new PlaneBufferGeometry(1,1); class HandleSprite extends Sprite$2{ constructor(position,options={}){ options.sizeInfo = {width2d:60}; super(options); this.position.copy(position); this.dragStyle = options.dragStyle || 'default'; //'default'||'onPointCloud' this.bindEvent(); } bindEvent(){ let projectedStart, pointerStart; const drag = (e)=>{ /* if(e.hoverViewport != e.drag.dragViewport){//不能使用e.dragViewport,要使用drag中的,因为drag中存储的要一直继承下来,不因mouseup了而改变。 viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"polygon_AtWrongPlace" }) return } */ const camera = viewer.scene.getActiveCamera(); if(projectedStart){ let move2d = new Vector2().subVectors(e.pointer, pointerStart); let projectNow = projectedStart.clone(); projectNow.x += move2d.x; projectNow.y += move2d.y; let unprojected = projectNow.clone().unproject(camera); this.position.set(unprojected.x, unprojected.y, unprojected.z); }else { projectedStart = this.position.clone().project(camera); pointerStart = e.pointer.clone(); } this.update(); this.dispatchEvent({type:'dragged', position: this.position }); }; const drop = (e)=>{ projectedStart = null, pointerStart = null; }; const mouseover = (e) => { viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"markerMove" }); }; const mouseleave = (e) => { viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"markerMove" }); }; this.addEventListener('drag', drag); this.addEventListener('drop', drop); this.addEventListener('mouseover', mouseover); this.addEventListener('mouseleave', mouseleave); } } const sphere = new Mesh(new SphereBufferGeometry(0.08,0.08,3,2), new MeshBasicMaterial({color:'#f88'})); class CurveCtrl extends Object3D { constructor(points, lineMat, color, name, options={}){ super(); this.curve = new CatmullRomCurve3(points, false, "centripetal" /* , tension */); this.name = name || 'curveNode'; this.handleMat = options.handleMat; this.lineMat = lineMat; this.createPath(); this.color = color; this.handles = []; this.wholeLength = 0; this.viewports = options.viewports || [viewer.mainViewport]; //for HandleSprite for(let i=0,j=this.points.length; i0){ this.spheres.add(sphere.clone()); i--; } */ this.add(this.spheres); } this.updatePath(); } addPoint(position, index, ifUpdate){ let length = this.points.length; if(index == void 0 ){ index = length; } let handle = this.createHandle(position); this.handles = [...this.handles.slice(0,index), handle, ...this.handles.slice(index,length)]; this.points = [...this.points.slice(0,index), position, ...this.points.slice(index,length)]; ifUpdate && (this.updatePath(), this.updateHandle(index)); } removePoint(index){ let handle = this.handles[index]; handle.dispose(); this.handles.splice(index,1); this.points.splice(index,1); this.updatePath(); } createPath(){ const line = LineDraw.createFatLine( [ ],this.lineMat); this.line = line; this.add(line); } updatePath(){ this.curve.needsUpdate = true; //如果不更新,得到的点不均匀,开头点少。 let points, length = this.points.length; this.wholeLength = this.points.reduce((total, currentValue, currentIndex, arr)=>{ //所有端点的距离总和 if(currentIndex == 0)return 0 return total + currentValue.distanceTo(arr[currentIndex-1]); },0); if(length > 1){ const count = MathUtils.clamp(Math.ceil(this.wholeLength * 5), 30, 500); points = this.curve.getSpacedPoints( count ); if(this.needsPercent){ //获取每个节点在整条中的百分比,便于定位(但不精确) this.pointsPercent = [0]; let sums = [0]; let sum = 0, last = points[0]; for(let i=1;ie.visible = false); points.forEach((e,i)=>{ let sphere1 = this.spheres.children[i]; if(!sphere1){ sphere1 = sphere.clone(); this.spheres.add(sphere1); } sphere1.position.copy(e); sphere1.visible = true; }); } }else { points = []; } LineDraw.updateLine(this.line, points); this.dispatchEvent('updatePath'); } createHandle(position){ if(this.handleMat){ var handle = new HandleSprite(position, {mat:this.handleMat, viewports:this.viewports}); this.add(handle); }else { var handle = new HandleSvg(position, this.color); } handle.visible = this.visible; handle.addEventListener('dragged',(e)=>{ let index = this.handles.indexOf(handle); this.points[index].copy(e.position); this.updatePath(); this.dispatchEvent({type:'dragCurvePoint', index}); }); return handle } updateHandle(index){ if(!this.visible)return this.handles[index].update(); } updateHandles(){ this.handles.forEach((handle,index)=>{ this.updateHandle(index); }); } update(){ this.updateHandles(); this.updatePath(); } set visible(v){ if(v != this.visible_ ){ this.visible_ = v; this.visible = v; if(this.handles){ this.handles.forEach(e=>e.visible = v ); if(v) this.updateHandles(); //因为不可见时没更新位置 } } } get visible(){ return this.visible_ } /* set visible(v){ this.handles.forEach(e=>e.svg.style.display = v ? "" : "none" ) if(v) this.updateHandles() //因为不可见时没更新位置 } */ get points(){ return this.curve.points; } set points(points){ this.curve.points = points; } getPointAt(t){ return this.curve.getPointAt(t) } getSpacedPoints(t){ return this.curve.getSpacedPoints(t) } dispose(){ this.parent && this.parent.remove(this); this.handles.forEach(e=>e.dispose() ); this.line.geometry && this.line.geometry.dispose(); } } const colors$1 = { position: 'red', target : 'blue' }; let lineMats$1; const getLineMat = function(name){ if(!lineMats$1){ lineMats$1 = { position: LineDraw.createFatLineMat({ color: colors$1.position, lineWidth: 3 }), target : LineDraw.createFatLineMat({ color: colors$1.target, lineWidth: 3 }), frustum: LineDraw.createFatLineMat({ color: colors$1.position, lineWidth: 2 }), aimAtTarget: new LineBasicMaterial({color:colors$1.target, depthTest:false}) }; } return lineMats$1[name] }; class CameraAnimation$1 extends EventDispatcher{ constructor(viewer){ super(); this.viewer = viewer; this.selectedElement = null; //this.controlPoints = []; this.uuid = MathUtils.generateUUID(); this.node = new Object3D(); this.node.name = "camera animation"; this.viewer.scene.scene.add(this.node); this.frustum = this.createFrustum(); this.node.add(this.frustum); this.name = "Camera Animation"; // "centripetal", "chordal", "catmullrom" this.curveType = "centripetal"; this.visible = true; this.targets = []; this.createPath(); this.duration = 5; this.percent = 0; this.currentIndex = 0; this.durations = []; this.quaternions = []; if(!Potree.settings.isTest && Potree.settings.isOfficial ){ this.setVisible(false); } this.addEventListener('dispose', ()=>{ this.dispose(); }); this.targetLines = new Object3D; this.node.add(this.targetLines); } static defaultFromView(viewer){ const animation = new CameraAnimation$1(viewer); const camera = viewer.scene.getActiveCamera(); const target = viewer.scene.view.getPivot(); const cpCenter = new Vector3( 0.3 * camera.position.x + 0.7 * target.x, 0.3 * camera.position.y + 0.7 * target.y, 0.3 * camera.position.z + 0.7 * target.z, ); const targetCenter = new Vector3( 0.05 * camera.position.x + 0.95 * target.x, 0.05 * camera.position.y + 0.95 * target.y, 0.05 * camera.position.z + 0.95 * target.z, ); const r = 2;//camera.position.distanceTo(target) * 0.3; //const dir = target.clone().sub(camera.position).normalize(); const angle = Utils.computeAzimuth(camera.position, target); const n = 5; for(let i = 0; i < n; i++){ let u = 1.5 * Math.PI * (i / n) + angle; const dx = r * Math.cos(u); const dy = r * Math.sin(u); const cpPos = new Vector3( cpCenter.x + dx, cpCenter.y + dy, cpCenter.z, ); const targetPos = new Vector3( targetCenter.x + dx * 0.1, targetCenter.y + dy * 0.1, targetCenter.z, ); animation.createControlPoint(null,{position:cpPos, target:targetPos}); } animation.changeCallback(); return animation; } createControlPoint(index, posInfo ){ const length = this.posCurve.points.length; const position = new Vector3; const target = new Vector3; if(index == void 0 ){ index = length; } if(!posInfo){ if(length >= 2 && index === 0){ const dir = new Vector3().subVectors(this.posCurve.points[0], this.posCurve.points[1] ); position.copy(this.posCurve.points[0]).add(dir); const tDir = new Vector3().subVectors(this.targets[0].position, this.targets[1].position ); target.copy(this.targets[0].position).add(dir); }else if(length >= 2 && index === length){ const dir = new Vector3().subVectors(this.posCurve.points[length-1], this.posCurve.points[length-2] ); position.copy(this.posCurve.points[length-2]).add(dir); const tDir = new Vector3().subVectors(this.targets[length-1].position, this.targets[length-2].position ); target.copy(this.targets[length-2].position).add(dir); }else if(length >= 2){ position.copy(this.posCurve.points[index-1].clone().add(this.posCurve.points[index]).multiplyScalar(0.5)); target.copy(this.targets[index-1].position.clone().add(this.targets[index].position).multiplyScalar(0.5)); } }else { position.copy(posInfo.position); target.copy(posInfo.target); } this.posCurve.addPoint(position, index/* , true */); //this.targetCurve.addPoint(target, index/* , true */) let targetSvg = new HandleSvg(target, colors$1.target); targetSvg.visible = this.visible; this.targets = [...this.targets.slice(0,index), targetSvg, ...this.targets.slice(index,length)]; if(this.useDurSlice){//不使用全局的duration,而是分段的 this.durations = [...this.durations.slice(0,index), posInfo.duration, ...this.durations.slice(index,length)]; } this.dispatchEvent({ type: "controlpoint_added", index }); { let targetLine = LineDraw.createLine([position,target] ,{mat: getLineMat('aimAtTarget')}); this.targetLines.children = [...this.targetLines.children.slice(0,index), targetLine, ...this.targetLines.children.slice(index,length)]; this.targets[index].addEventListener('dragged', (e)=>{ this.updatePathCallback(); this.dragPointCallback(e); }); } } dragPointCallback(e){ let index = e.index; if(e.index == void 0){ index = this.targets.indexOf(e.target); } LineDraw.moveLine(this.targetLines.children[index], [this.posCurve.points[index], this.targets[index].position] ); viewer.dispatchEvent('content_changed'); this.updateFrustum(); } updatePathCallback(){ { this.quaternions = []; let length = this.posCurve.points.length; for(let i=0; ie.z *= -1); //因为得到的rotation是camera的,作用在物体上要反向,所以这里反向一下 //geometry.computeBoundingSphere();//? const line = LineDraw.createFatLine( positions, {mat:getLineMat('frustum')}); //line.scale.set(20, 20, 20); line.visible = false; return line; } reMapCurvePercent(){ //因在不同点在相同位置旋转,由于间隔仅和位置距离相关,导致时间间隔为0,采取重新调整间隔的策略。 var length = this.posCurve.points.length; if(length<2){ return this.newPointsPercents = [] } var newPercents = [0]; if(this.useDurSlice){ //已经设定好了每一段的duration的话 let sums = [0]; let sum = 0, last; for(let i=0;i1 if( !math.closeTo(maxPercent, 1)){ let scale = 1 / maxPercent; //需要压缩的比例 <1 这一步会让实际得到的间隔更小 newPercents = newPercents.map(e=> e*=scale ); } } this.newPointsPercents = newPercents; //console.log(newPercents) } at(originPercent, delta, transitionRatio){ originPercent = MathUtils.clamp(originPercent, 0, 1); //修改第一层:起始时间 let percent = originPercent; /* const easePercent = 0.3; //缓动占比 //如果能在所有从静止到运动的中间加缓动就好了呀:lastPos * 0.9 + currentPos * 0.1 ? if(originPercent < easePercent){ console.log('easeIn') percent = easing.easeInSine(originPercent, 0, easePercent, easePercent) //currentTime, startY, wholeY, duration 选了一个衔接时接近斜率1的缓动函数 }else if(originPercent > 1-easePercent){ console.log('easeOut') percent = easing.easeOutSine(originPercent-(1-easePercent), 1-easePercent, easePercent, easePercent) } */ let quaternion; if(percent < 1){ //修改第二层:使用每个点的重定位的 newPointsPercents this.currentIndex = this.newPointsPercents.findIndex(e=> e>percent ) - 1; //假设每个节点的百分比是精确的,那么: let curIndexPercent = this.newPointsPercents[this.currentIndex]; let nextIndexPercent = this.newPointsPercents[this.currentIndex+1]; let progress = (percent - curIndexPercent) / (nextIndexPercent - curIndexPercent);//在这两个节点间的百分比 //投影到原本的 posCurve.pointsPercent上: let curIndexOriPercent = this.posCurve.pointsPercent[this.currentIndex]; let nextIndexOriPercent = this.posCurve.pointsPercent[this.currentIndex+1]; percent = curIndexOriPercent + (nextIndexOriPercent - curIndexOriPercent) * progress; let endQuaternion = this.quaternions[this.currentIndex+1]; let startQuaternion = this.quaternions[this.currentIndex]; quaternion = (new Quaternion()).copy(startQuaternion); lerp.quaternion(quaternion, endQuaternion)(progress); }else { this.currentIndex = this.posCurve.points.length - 1; quaternion = math.getQuaFromPosAim(this.posCurve.points[this.currentIndex], this.targets[this.currentIndex].position); } const position = this.posCurve.getPointAt(percent); // 需要this.posCurve.points.length>1 否则报错 //console.log(this.currentIndex, originPercent) //缓动: var aimQua, aimPos; if(delta != void 0 ){ if(Potree.settings.tourTestCameraMove){ aimQua = this.frustum.quaternion.clone(); aimPos = this.frustum.position.clone(); }else { var camera = viewer.scene.getActiveCamera(); aimQua = camera.quaternion.clone(); aimPos = camera.position.clone(); } transitionRatio = transitionRatio || 1 / Potree.settings.cameraAniSmoothRatio;//渐变系数,越小缓动程度越高,越平滑 transitionRatio *= delta * 60; //假设标准帧率为60fps,当帧率低时(delta大时)要降低缓动 //console.log(transitionRatio, delta) //画面ui变化会使delta变大 transitionRatio = MathUtils.clamp(transitionRatio, 0, 1); lerp.quaternion(aimQua, quaternion)(transitionRatio); //每次只改变一点点 lerp.vector(aimPos, position)(transitionRatio); }else { aimQua = quaternion; aimPos = position; } let rotation = new Euler().setFromQuaternion(aimQua ); const frame = { position: aimPos, rotation }; return frame; } set(percent){ this.percent = percent; } setVisible(visible){ this.node.visible = visible; this.posCurve.visible = visible; this.targets.forEach(e=>e.visible = visible ); this.visible = visible; } setDuration(duration){ if(duration != this.duration){ this.duration = duration; if(this.quaternions.length == this.posCurve.points.length)this.reMapCurvePercent(); } } getDuration(duration){ return this.duration; } play(startOptions={}){ if(this.onUpdate){ return console.error('已经开始播放') } let startPercent = 0, currentIndex = 0; if(startOptions.percent != void 0 ){ startPercent = startOptions.percent; }else if(startOptions.index){ currentIndex = index; //startPercent = index/(this.posCurve.points.length-1) startPercent = this.posCurve.pointsPercent[index]; } //const tStart = performance.now(); const duration = this.duration; this.originalyVisible = this.visible; Potree.settings.tourTestCameraMove || this.setVisible(false); let tStart, startTransitionRatio = 0.2; let startDelay = 1/startTransitionRatio / 20 ;//因为缓动所以延迟开始,前面前都是at(0),使过渡到开始点位(但是依旧不能准确停在起始点,因为缓动是乘百分比有残留。所以直接平滑衔接到开始后的位置) let hasPlayedTime = 0; let finishDelay = Potree.settings.cameraAniSmoothRatio / 60 * 3;//结束后还需要多久时间才能大致达到缓动的最终目标 let hasStoppedTime = 0; this.onUpdate = (e) => { if(this.posCurve.points.length<2){ if(this.posCurve.points.length == 1){ viewer.scene.view.position.copy(this.posCurve.points[0]); viewer.scene.view.rotation = new Euler().setFromQuaternion(this.quaternions[0]); } this.pause(); return } let percent, transitionRatio; if(tStart){ let tNow = performance.now(); let elapsed = (tNow - tStart) / 1000; percent = elapsed / duration + startPercent; }else {//从当前位置过渡到开始位置 percent = 0; hasPlayedTime += e.delta; transitionRatio = startTransitionRatio; //console.log('延迟开始') if(hasPlayedTime > startDelay){ tStart = performance.now(); } } this.set(percent); const frame = this.at(percent, e.delta, transitionRatio); if(currentIndex != this.currentIndex){ currentIndex = this.currentIndex; console.log('updateCurrentIndex', currentIndex); this.dispatchEvent({type:'updateCurrentIndex', currentIndex }); } if(!Potree.settings.tourTestCameraMove){ viewer.scene.view.position.copy(frame.position); //viewer.scene.view.lookAt(frame.target); viewer.scene.view.rotation = frame.rotation; } this.updateFrustum(frame); if(percent >= 1){ if(hasStoppedTime > finishDelay){ this.pause(); }else { hasStoppedTime += e.delta; //console.log('延迟结束') } } }; this.viewer.addEventListener("update", this.onUpdate); } pause(){ this.setVisible(this.originalyVisible); this.viewer.removeEventListener("update", this.onUpdate); this.dispatchEvent('playDone'); this.onUpdate = null; } updateFrustum(frame){ const frustum = this.frustum; if(this.posCurve.points.length>1){ frustum.visible = true; }else { frustum.visible = false; return } frame = frame || this.at(this.percent); frustum.position.copy(frame.position); //frustum.lookAt(...frame.target.toArray()); frustum.rotation.copy(frame.rotation); viewer.dispatchEvent('content_changed'); } changeCallback(i){ this.posCurve.update(); //this.targets.forEach(e=>e.update()) this.updatePathCallback(); if(i==void 0){ let length = this.posCurve.points.length; for(let i=0; ie.dispose()); this.durations = []; this.node.parent.remove(this.node); } } //scene.removeCameraAnimation //修改:不使用targetCurve作为target曲线,因为播放时posCuve的节点和targetCurve并没有对应,且使用target的曲线会使角度变化大的情况过渡生硬。 // 改完旋转了。但是位置也有问题。速度完全和路程相关,当在同一位置设置多点时,这段的总时长为0. (是否要设置最小时长?不过也做不到 - -) class MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ this.viewer = viewer; this.measurement = measurement; this.propertiesPanel = propertiesPanel; this._update = () => { this.update(); }; } createCoordinatesTable(points){ let table = $(`
x y z
`); let copyIconPath = Potree.resourcePath + '/icons/copy.svg'; for (let point of points) { let x = Utils.addCommas(point.x.toFixed(3)); let y = Utils.addCommas(point.y.toFixed(3)); let z = Utils.addCommas(point.z.toFixed(3)); let row = $(` ${x} ${y} ${z} `); this.elCopy = row.find("img[name=copy]"); this.elCopy.click( () => { let msg = point.toArray().map(c => c.toFixed(3)).join(", "); Utils.clipboardCopy(msg); this.viewer.postMessage( `Copied value to clipboard:
'${msg}'`, {duration: 3000}); }); table.append(row); } return table; }; createAttributesTable(){ let elTable = $('
'); let point = this.measurement.points[0]; /* for(let attributeName of Object.keys(point)){ if(attributeName === "position"){ }else if(attributeName === "rgba"){ let color = point.rgba; let text = color.join(', '); elTable.append($(` rgb ${text} `)); }else{ let value = point[attributeName]; let text = value.join(', '); elTable.append($(` ${attributeName} ${text} `)); } } */ return elTable; } update(){ } }; class DistancePanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; this.elContent = $(`

`); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeMeasurement(measurement); }); this.elMakeProfile = this.elContent.find("input[name=make_profile]"); this.elMakeProfile.click( () => { //measurement.points; const profile = new Profile(); profile.name = measurement.name; profile.width = measurement.getTotalDistance() / 50; for(const point of measurement.points){ profile.addMarker(point.position.clone()); } this.viewer.scene.addProfile(profile); }); this.propertiesPanel.addVolatileListener(measurement, "marker_added", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_removed", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_moved", this._update); this.update(); } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable(this.measurement.points.map(p => p.position))); let positions = this.measurement.points.map(p => p.position); let distances = []; for (let i = 0; i < positions.length - 1; i++) { let d = positions[i].distanceTo(positions[i + 1]); distances.push(d.toFixed(3)); } let totalDistance = this.measurement.getTotalDistance().toFixed(3); let elDistanceTable = this.elContent.find(`#distances_table`); elDistanceTable.empty(); for (let i = 0; i < distances.length; i++) { let label = (i === 0) ? 'Distances: ' : ''; let distance = distances[i]; let elDistance = $(` ${label} ${distance} `); elDistanceTable.append(elDistance); } let elTotal = $(` Total: ${totalDistance} `); elDistanceTable.append(elTotal); } }; class PointPanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; this.elContent = $(`

`); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeMeasurement(measurement); }); this.propertiesPanel.addVolatileListener(measurement, "marker_added", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_removed", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_moved", this._update); this.update(); } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable(this.measurement.points)); let elAttributesContainer = this.elContent.find('.attributes_table_container'); elAttributesContainer.empty(); elAttributesContainer.append(this.createAttributesTable()); } }; class AreaPanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; this.elContent = $(`

Area:
`); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeMeasurement(measurement); }); this.propertiesPanel.addVolatileListener(measurement, "marker_added", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_removed", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_moved", this._update); this.update(); } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable(this.measurement.points)); let elArea = this.elContent.find(`#measurement_area`); elArea.html(this.measurement.area.value.toFixed(3)); } }; class AnglePanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; this.elContent = $(`

\u03b1 \u03b2 \u03b3
`); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeMeasurement(measurement); }); this.propertiesPanel.addVolatileListener(measurement, "marker_added", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_removed", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_moved", this._update); this.update(); } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable(this.measurement.points.map(p => p.position))); let angles = []; for(let i = 0; i < this.measurement.points.length; i++){ angles.push(this.measurement.getAngle(i) * (180.0 / Math.PI)); } angles = angles.map(a => a.toFixed(1) + '\u00B0'); let elAlpha = this.elContent.find(`#angle_cell_alpha`); let elBetta = this.elContent.find(`#angle_cell_betta`); let elGamma = this.elContent.find(`#angle_cell_gamma`); elAlpha.html(angles[0]); elBetta.html(angles[1]); elGamma.html(angles[2]); } }; class CirclePanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; this.elContent = $(`

`); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeMeasurement(measurement); }); this.propertiesPanel.addVolatileListener(measurement, "marker_added", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_removed", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_moved", this._update); this.update(); } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable(this.measurement.points.map(p => p.position))); const elInfos = this.elContent.find(`#infos_table`); if(this.measurement.points.length !== 3){ elInfos.empty(); return; } const A = this.measurement.points[0].position; const B = this.measurement.points[1].position; const C = this.measurement.points[2].position; const center = Potree.Utils.computeCircleCenter(A, B, C); const radius = center.distanceTo(A); const circumference = 2 * Math.PI * radius; const format = (number) => { return Potree.Utils.addCommas(number.toFixed(3)); }; const txtCenter = `${format(center.x)} ${format(center.y)} ${format(center.z)}`; const txtRadius = format(radius); const txtCircumference = format(circumference); const thStyle = `style="text-align: left"`; const tdStyle = `style="width: 100%; padding: 5px;"`; elInfos.html(` Center: ${txtCenter} Radius: ${txtRadius} Circumference: ${txtCircumference} `); } }; class HeightPanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; this.elContent = $(`

Height:
`); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeMeasurement(measurement); }); this.propertiesPanel.addVolatileListener(measurement, "marker_added", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_removed", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_moved", this._update); this.update(); } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable(this.measurement.points.map(p => p.position))); { let points = this.measurement.points; let sorted = points.slice().sort((a, b) => a.position.z - b.position.z); let lowPoint = sorted[0].position.clone(); let highPoint = sorted[sorted.length - 1].position.clone(); let min = lowPoint.z; let max = highPoint.z; let height = max - min; height = height.toFixed(3); this.elHeightLabel = this.elContent.find(`#height_label`); this.elHeightLabel.html(`Height: ${height}`); } } }; class VolumePanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let copyIconPath = Potree.resourcePath + '/icons/copy.svg'; let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; let lblLengthText = new Map([ [BoxVolume, "length"], [SphereVolume$1, "rx"], ]).get(measurement.constructor); let lblWidthText = new Map([ [BoxVolume, "width"], [SphereVolume$1, "ry"], ]).get(measurement.constructor); let lblHeightText = new Map([ [BoxVolume, "height"], [SphereVolume$1, "rz"], ]).get(measurement.constructor); this.elContent = $(`
\u03b1 \u03b2 \u03b3
${lblLengthText} ${lblWidthText} ${lblHeightText}

Volume:
  • `); { // download this.elDownloadButton = this.elContent.find("input[name=download_volume]"); if(this.propertiesPanel.viewer.server){ this.elDownloadButton.click(() => this.download()); } else { this.elDownloadButton.hide(); } } this.elCopyRotation = this.elContent.find("img[name=copyRotation]"); this.elCopyRotation.click( () => { let rotation = this.measurement.rotation.toArray().slice(0, 3); let msg = rotation.map(c => c.toFixed(3)).join(", "); Utils.clipboardCopy(msg); this.viewer.postMessage( `Copied value to clipboard:
    '${msg}'`, {duration: 3000}); }); this.elCopyScale = this.elContent.find("img[name=copyScale]"); this.elCopyScale.click( () => { let scale = this.measurement.scale.toArray(); let msg = scale.map(c => c.toFixed(3)).join(", "); Utils.clipboardCopy(msg); this.viewer.postMessage( `Copied value to clipboard:
    '${msg}'`, {duration: 3000}); }); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeVolume(measurement); }); this.elContent.find("#volume_reset_orientation").click(() => { measurement.rotation.set(0, 0, 0); }); this.elContent.find("#volume_make_uniform").click(() => { let mean = (measurement.scale.x + measurement.scale.y + measurement.scale.z) / 3; measurement.scale.set(mean, mean, mean); }); this.elCheckClip = this.elContent.find('#volume_clip'); this.elCheckClip.click(event => { this.measurement.clip = event.target.checked; }); this.elCheckShow = this.elContent.find('#volume_show'); this.elCheckShow.click(event => { this.measurement.visible = event.target.checked; }); this.propertiesPanel.addVolatileListener(measurement, "position_changed", this._update); this.propertiesPanel.addVolatileListener(measurement, "orientation_changed", this._update); this.propertiesPanel.addVolatileListener(measurement, "scale_changed", this._update); this.propertiesPanel.addVolatileListener(measurement, "clip_changed", this._update); this.update(); } async download(){ let clipBox = this.measurement; let regions = []; //for(let clipBox of boxes){ { let toClip = clipBox.matrixWorld; let px = new Vector3(+0.5, 0, 0).applyMatrix4(toClip); let nx = new Vector3(-0.5, 0, 0).applyMatrix4(toClip); let py = new Vector3(0, +0.5, 0).applyMatrix4(toClip); let ny = new Vector3(0, -0.5, 0).applyMatrix4(toClip); let pz = new Vector3(0, 0, +0.5).applyMatrix4(toClip); let nz = new Vector3(0, 0, -0.5).applyMatrix4(toClip); let pxN = new Vector3().subVectors(nx, px).normalize(); let nxN = pxN.clone().multiplyScalar(-1); let pyN = new Vector3().subVectors(ny, py).normalize(); let nyN = pyN.clone().multiplyScalar(-1); let pzN = new Vector3().subVectors(nz, pz).normalize(); let nzN = pzN.clone().multiplyScalar(-1); let planes = [ new Plane().setFromNormalAndCoplanarPoint(pxN, px), new Plane().setFromNormalAndCoplanarPoint(nxN, nx), new Plane().setFromNormalAndCoplanarPoint(pyN, py), new Plane().setFromNormalAndCoplanarPoint(nyN, ny), new Plane().setFromNormalAndCoplanarPoint(pzN, pz), new Plane().setFromNormalAndCoplanarPoint(nzN, nz), ]; let planeQueryParts = []; for(let plane of planes){ let part = [plane.normal.toArray(), plane.constant].join(","); part = `[${part}]`; planeQueryParts.push(part); } let region = "[" + planeQueryParts.join(",") + "]"; regions.push(region); } let regionsArg = regions.join(","); let pointcloudArgs = []; for(let pointcloud of this.viewer.scene.pointclouds){ if(!pointcloud.visible){ continue; } let offset = pointcloud.pcoGeometry.offset.clone(); let negateOffset = new Matrix4().makeTranslation(...offset.multiplyScalar(-1).toArray()); let matrixWorld = pointcloud.matrixWorld; let transform = new Matrix4().multiplyMatrices(matrixWorld, negateOffset); let path = `${window.location.pathname}/../${pointcloud.pcoGeometry.url}`; let arg = { path: path, transform: transform.elements, }; let argString = JSON.stringify(arg); pointcloudArgs.push(argString); } let pointcloudsArg = pointcloudArgs.join(","); let elMessage = this.elContent.find("div[name=download_message]"); let error = (message) => { elMessage.html(`
    ERROR: ${message}
    `); }; let info = (message) => { elMessage.html(`${message}`); }; let handle = null; { // START FILTER let url = `${viewer.server}/create_regions_filter?pointclouds=[${pointcloudsArg}]®ions=[${regionsArg}]`; //console.log(url); info("estimating results ..."); let response = await fetch(url); let jsResponse = await response.json(); //console.log(jsResponse); if(!jsResponse.handle){ error(jsResponse.message); return; }else { handle = jsResponse.handle; } } { // WAIT, CHECK PROGRESS, HANDLE FINISH let url = `${viewer.server}/check_regions_filter?handle=${handle}`; let sleep = (function(duration){ return new Promise( (res, rej) => { setTimeout(() => { res(); }, duration); }); }); let handleFiltering = (jsResponse) => { let {progress, estimate} = jsResponse; let progressFract = progress["processed points"] / estimate.points; let progressPercents = parseInt(progressFract * 100); info(`progress: ${progressPercents}%`); }; let handleFinish = (jsResponse) => { let message = "downloads ready:
    "; message += "
      "; for(let i = 0; i < jsResponse.pointclouds.length; i++){ let url = `${viewer.server}/download_regions_filter_result?handle=${handle}&index=${i}`; message += `
    • result_${i}.las
    • \n`; } let reportURL = `${viewer.server}/download_regions_filter_report?handle=${handle}`; message += `
    • report.json
    • \n`; message += "
    "; info(message); }; let handleUnexpected = (jsResponse) => { let message = `Unexpected Response.
    status: ${jsResponse.status}
    message: ${jsResponse.message}`; info(message); }; let handleError = (jsResponse) => { let message = `ERROR: ${jsResponse.message}`; error(message); throw new Error(message); }; let start = Date.now(); while(true){ let response = await fetch(url); let jsResponse = await response.json(); if(jsResponse.status === "ERROR"){ handleError(jsResponse); }else if(jsResponse.status === "FILTERING"){ handleFiltering(jsResponse); }else if(jsResponse.status === "FINISHED"){ handleFinish(jsResponse); break; }else { handleUnexpected(jsResponse); } let durationS = (Date.now() - start) / 1000; let sleepAmountMS = durationS < 10 ? 100 : 1000; await sleep(sleepAmountMS); } } } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable([this.measurement.position])); { let angles = this.measurement.rotation.toVector3(); angles = angles.toArray(); //angles = [angles.z, angles.x, angles.y]; angles = angles.map(v => 180 * v / Math.PI); angles = angles.map(a => a.toFixed(1) + '\u00B0'); let elAlpha = this.elContent.find(`#angle_cell_alpha`); let elBetta = this.elContent.find(`#angle_cell_betta`); let elGamma = this.elContent.find(`#angle_cell_gamma`); elAlpha.html(angles[0]); elBetta.html(angles[1]); elGamma.html(angles[2]); } { let dimensions = this.measurement.scale.toArray(); dimensions = dimensions.map(v => Utils.addCommas(v.toFixed(2))); let elLength = this.elContent.find(`#cell_length`); let elWidth = this.elContent.find(`#cell_width`); let elHeight = this.elContent.find(`#cell_height`); elLength.html(dimensions[0]); elWidth.html(dimensions[1]); elHeight.html(dimensions[2]); } { let elVolume = this.elContent.find(`#measurement_volume`); let volume = this.measurement.getVolume(); elVolume.html(Utils.addCommas(volume.toFixed(2))); } this.elCheckClip.prop("checked", this.measurement.clip); this.elCheckShow.prop("checked", this.measurement.visible); } }; class ProfilePanel extends MeasurePanel{ constructor(viewer, measurement, propertiesPanel){ super(viewer, measurement, propertiesPanel); let removeIconPath = Potree.resourcePath + '/icons/remove.svg'; this.elContent = $(`

    Width:

  • `); this.elRemove = this.elContent.find("img[name=remove]"); this.elRemove.click( () => { this.viewer.scene.removeProfile(measurement); }); { // download this.elDownloadButton = this.elContent.find(`input[name=download_profile]`); if(this.propertiesPanel.viewer.server){ this.elDownloadButton.click(() => this.download()); } else { this.elDownloadButton.hide(); } } { // width spinner let elWidthSlider = this.elContent.find(`#sldProfileWidth`); elWidthSlider.spinner({ min: 0, max: 10 * 1000 * 1000, step: 0.01, numberFormat: 'n', start: () => {}, spin: (event, ui) => { let value = elWidthSlider.spinner('value'); measurement.setWidth(value); }, change: (event, ui) => { let value = elWidthSlider.spinner('value'); measurement.setWidth(value); }, stop: (event, ui) => { let value = elWidthSlider.spinner('value'); measurement.setWidth(value); }, incremental: (count) => { let value = elWidthSlider.spinner('value'); let step = elWidthSlider.spinner('option', 'step'); let delta = value * 0.05; let increments = Math.max(1, parseInt(delta / step)); return increments; } }); elWidthSlider.spinner('value', measurement.getWidth()); elWidthSlider.spinner('widget').css('width', '100%'); let widthListener = (event) => { let value = elWidthSlider.spinner('value'); if (value !== measurement.getWidth()) { elWidthSlider.spinner('value', measurement.getWidth()); } }; this.propertiesPanel.addVolatileListener(measurement, "width_changed", widthListener); } let elShow2DProfile = this.elContent.find(`#show_2d_profile`); elShow2DProfile.click(() => { this.propertiesPanel.viewer.profileWindow.show(); this.propertiesPanel.viewer.profileWindowController.setProfile(measurement); }); this.propertiesPanel.addVolatileListener(measurement, "marker_added", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_removed", this._update); this.propertiesPanel.addVolatileListener(measurement, "marker_moved", this._update); this.update(); } update(){ let elCoordiantesContainer = this.elContent.find('.coordinates_table_container'); elCoordiantesContainer.empty(); elCoordiantesContainer.append(this.createCoordinatesTable(this.measurement.points)); } async download(){ let profile = this.measurement; let regions = []; { let segments = profile.getSegments(); let width = profile.width; for(let segment of segments){ let start = segment.start.clone().multiply(new Vector3(1, 1, 0)); let end = segment.end.clone().multiply(new Vector3(1, 1, 0)); let center = new Vector3().addVectors(start, end).multiplyScalar(0.5); let startEndDir = new Vector3().subVectors(end, start).normalize(); let endStartDir = new Vector3().subVectors(start, end).normalize(); let upDir = new Vector3(0, 0, 1); let rightDir = new Vector3().crossVectors(startEndDir, upDir); let leftDir = new Vector3().crossVectors(endStartDir, upDir); console.log(leftDir); let right = rightDir.clone().multiplyScalar(width * 0.5).add(center); let left = leftDir.clone().multiplyScalar(width * 0.5).add(center); let planes = [ new Plane().setFromNormalAndCoplanarPoint(startEndDir, start), new Plane().setFromNormalAndCoplanarPoint(endStartDir, end), new Plane().setFromNormalAndCoplanarPoint(leftDir, right), new Plane().setFromNormalAndCoplanarPoint(rightDir, left), ]; let planeQueryParts = []; for(let plane of planes){ let part = [plane.normal.toArray(), plane.constant].join(","); part = `[${part}]`; planeQueryParts.push(part); } let region = "[" + planeQueryParts.join(",") + "]"; regions.push(region); } } let regionsArg = regions.join(","); let pointcloudArgs = []; for(let pointcloud of this.viewer.scene.pointclouds){ if(!pointcloud.visible){ continue; } let offset = pointcloud.pcoGeometry.offset.clone(); let negateOffset = new Matrix4().makeTranslation(...offset.multiplyScalar(-1).toArray()); let matrixWorld = pointcloud.matrixWorld; let transform = new Matrix4().multiplyMatrices(matrixWorld, negateOffset); let path = `${window.location.pathname}/../${pointcloud.pcoGeometry.url}`; let arg = { path: path, transform: transform.elements, }; let argString = JSON.stringify(arg); pointcloudArgs.push(argString); } let pointcloudsArg = pointcloudArgs.join(","); let elMessage = this.elContent.find("div[name=download_message]"); let error = (message) => { elMessage.html(`
    ERROR: ${message}
    `); }; let info = (message) => { elMessage.html(`${message}`); }; let handle = null; { // START FILTER let url = `${viewer.server}/create_regions_filter?pointclouds=[${pointcloudsArg}]®ions=[${regionsArg}]`; //console.log(url); info("estimating results ..."); let response = await fetch(url); let jsResponse = await response.json(); //console.log(jsResponse); if(!jsResponse.handle){ error(jsResponse.message); return; }else { handle = jsResponse.handle; } } { // WAIT, CHECK PROGRESS, HANDLE FINISH let url = `${viewer.server}/check_regions_filter?handle=${handle}`; let sleep = (function(duration){ return new Promise( (res, rej) => { setTimeout(() => { res(); }, duration); }); }); let handleFiltering = (jsResponse) => { let {progress, estimate} = jsResponse; let progressFract = progress["processed points"] / estimate.points; let progressPercents = parseInt(progressFract * 100); info(`progress: ${progressPercents}%`); }; let handleFinish = (jsResponse) => { let message = "downloads ready:
    "; message += "
      "; for(let i = 0; i < jsResponse.pointclouds.length; i++){ let url = `${viewer.server}/download_regions_filter_result?handle=${handle}&index=${i}`; message += `
    • result_${i}.las
    • \n`; } let reportURL = `${viewer.server}/download_regions_filter_report?handle=${handle}`; message += `
    • report.json
    • \n`; message += "
    "; info(message); }; let handleUnexpected = (jsResponse) => { let message = `Unexpected Response.
    status: ${jsResponse.status}
    message: ${jsResponse.message}`; info(message); }; let handleError = (jsResponse) => { let message = `ERROR: ${jsResponse.message}`; error(message); throw new Error(message); }; let start = Date.now(); while(true){ let response = await fetch(url); let jsResponse = await response.json(); if(jsResponse.status === "ERROR"){ handleError(jsResponse); }else if(jsResponse.status === "FILTERING"){ handleFiltering(jsResponse); }else if(jsResponse.status === "FINISHED"){ handleFinish(jsResponse); break; }else { handleUnexpected(jsResponse); } let durationS = (Date.now() - start) / 1000; let sleepAmountMS = durationS < 10 ? 100 : 1000; await sleep(sleepAmountMS); } } } }; class CameraPanel{ constructor(viewer, propertiesPanel){ this.viewer = viewer; this.propertiesPanel = propertiesPanel; this._update = () => { this.update(); }; let copyIconPath = Potree.resourcePath + '/icons/copy.svg'; this.elContent = $(`
    position
    target
    `); this.elCopyPosition = this.elContent.find("img[name=copyPosition]"); this.elCopyPosition.click( () => { let pos = this.viewer.scene.getActiveCamera().position.toArray(); let msg = pos.map(c => c.toFixed(3)).join(", "); Utils.clipboardCopy(msg); this.viewer.postMessage( `Copied value to clipboard:
    '${msg}'`, {duration: 3000}); }); this.elCopyTarget = this.elContent.find("img[name=copyTarget]"); this.elCopyTarget.click( () => { let pos = this.viewer.scene.view.getPivot().toArray(); let msg = pos.map(c => c.toFixed(3)).join(", "); Utils.clipboardCopy(msg); this.viewer.postMessage( `Copied value to clipboard:
    '${msg}'`, {duration: 3000}); }); this.propertiesPanel.addVolatileListener(viewer, "camera_changed", this._update); this.update(); } update(){ //console.log("updating camera panel"); let camera = this.viewer.scene.getActiveCamera(); let view = this.viewer.scene.view; let pos = camera.position.toArray().map(c => Utils.addCommas(c.toFixed(3))); this.elContent.find("#camera_position_x").html(pos[0]); this.elContent.find("#camera_position_y").html(pos[1]); this.elContent.find("#camera_position_z").html(pos[2]); let target = view.getPivot().toArray().map(c => Utils.addCommas(c.toFixed(3))); this.elContent.find("#camera_target_x").html(target[0]); this.elContent.find("#camera_target_y").html(target[1]); this.elContent.find("#camera_target_z").html(target[2]); } }; class AnnotationPanel{ constructor(viewer, propertiesPanel, annotation){ this.viewer = viewer; this.propertiesPanel = propertiesPanel; this.annotation = annotation; this._update = () => { this.update(); }; let copyIconPath = `${Potree.resourcePath}/icons/copy.svg`; this.elContent = $(`
    position
    Title
    Annotation Title
    Description
    A longer description of this annotation. Can be multiple lines long. TODO: the user should be able to modify title and description.
    `); this.elCopyPosition = this.elContent.find("img[name=copyPosition]"); this.elCopyPosition.click( () => { let pos = this.annotation.position.toArray(); let msg = pos.map(c => c.toFixed(3)).join(", "); Utils.clipboardCopy(msg); this.viewer.postMessage( `Copied value to clipboard:
    '${msg}'`, {duration: 3000}); }); this.elTitle = this.elContent.find("#annotation_title").html(annotation.title); this.elDescription = this.elContent.find("#annotation_description").html(annotation.description); this.elTitle[0].addEventListener("input", () => { const title = this.elTitle.html(); annotation.title = title; }, false); this.elDescription[0].addEventListener("input", () => { const description = this.elDescription.html(); annotation.description = description; }, false); this.update(); } update(){ const {annotation, elContent, elTitle, elDescription} = this; let pos = annotation.position.toArray().map(c => Utils.addCommas(c.toFixed(3))); elContent.find("#annotation_position_x").html(pos[0]); elContent.find("#annotation_position_y").html(pos[1]); elContent.find("#annotation_position_z").html(pos[2]); elTitle.html(annotation.title); elDescription.html(annotation.description); } }; class CameraAnimationPanel{ constructor(viewer, propertiesPanel, animation){ this.viewer = viewer; this.propertiesPanel = propertiesPanel; this.animation = animation; this.elContent = $(`
    Duration: Time:
    `); const elPlay = this.elContent.find("input[name=play]"); elPlay.click( () => { animation.play(); }); const elPause = this.elContent.find("input[name=pause]"); elPause.click( () => { animation.pause(); }); const elSlider = this.elContent.find('#sldTime'); elSlider.slider({ value: 0, min: 0, max: 1, step: 0.001, slide: (event, ui) => { animation.set(ui.value); animation.updateFrustum(); } }); let elDuration = this.elContent.find(`input[name=spnDuration]`); elDuration.spinner({ min: 0, max: 300, step: 0.01, numberFormat: 'n', start: () => {}, spin: (event, ui) => { let value = elDuration.spinner('value'); animation.setDuration(value); }, change: (event, ui) => { let value = elDuration.spinner('value'); animation.setDuration(value); }, stop: (event, ui) => { let value = elDuration.spinner('value'); animation.setDuration(value); }, incremental: (count) => { let value = elDuration.spinner('value'); let step = elDuration.spinner('option', 'step'); let delta = value * 0.05; let increments = Math.max(1, parseInt(delta / step)); return increments; } }); elDuration.spinner('value', animation.getDuration()); elDuration.spinner('widget').css('width', '100%'); const elKeyframes = this.elContent.find("#animation_keyframes"); const updateKeyframes = () => { elKeyframes.empty(); //let index = 0; // // // const addNewKeyframeItem = (index) => { let elNewKeyframe = $(`
    `); const elAdd = elNewKeyframe.find("input[name=add]"); elAdd.click( () => { animation.createControlPoint(index, { position: viewer.scene.view.position, target: viewer.scene.view.getPivot() }); animation.changeCallback(); }); elKeyframes.append(elNewKeyframe); }; const addKeyframeItem = (index) => { let elKeyframe = $(`
    keyframe
    `); const elAssign = elKeyframe.find("img[name=assign]"); const elMove = elKeyframe.find("img[name=move]"); const elDelete = elKeyframe.find("img[name=delete]"); elAssign.click( () => { //修改 animation.posCurve.points[index].copy(viewer.scene.view.position); animation.targets[index].position.copy(viewer.scene.view.getPivot()); animation.changeCallback(index); }); elMove.click( () => { viewer.scene.view.position.copy(animation.posCurve.points[index]); viewer.scene.view.lookAt(animation.targets[index].position); }); elDelete.click( () => { animation.removeControlPoint(index); animation.changeCallback(); }); elKeyframes.append(elKeyframe); }; let index = 0; addNewKeyframeItem(index); animation.posCurve.points.forEach(e=>{ addKeyframeItem(index); index++; addNewKeyframeItem(index); }); }; updateKeyframes(); animation.addEventListener("controlpoint_added", updateKeyframes); animation.addEventListener("controlpoint_removed", updateKeyframes); // this._update = () => { this.update(); }; // this.update(); } update(){ } }; class PropertiesPanel{ constructor(container, viewer){ this.container = container; this.viewer = viewer; this.object = null; this.cleanupTasks = []; this.scene = null; } setScene(scene){ this.scene = scene; } set(object){ if(this.object === object){ return; } this.object = object; for(let task of this.cleanupTasks){ task(); } this.cleanupTasks = []; this.container.empty(); if(object instanceof PointCloudTree){ this.setPointCloud(object); }else if(object instanceof Measure || object instanceof Profile || object instanceof Volume){ this.setMeasurement(object); }else if(object instanceof Camera){ this.setCamera(object); }else if(object instanceof Annotation){ this.setAnnotation(object); }else if(object instanceof CameraAnimation$1){//改 this.setCameraAnimation(object); } } // // Used for events that should be removed when the property object changes. // This is for listening to materials, scene, point clouds, etc. // not required for DOM listeners, since they are automatically cleared by removing the DOM subtree. // addVolatileListener(target, type, callback){ target.addEventListener(type, callback); this.cleanupTasks.push(() => { target.removeEventListener(type, callback); }); } setPointCloud(pointcloud){ let material = pointcloud.material; let panel = $(`

    • :
    • Attribute
    • Attribute Weights
    • RGB:
    • Intensity:
    • Elevation:
    • Classification:
    • Return Number:
    • Source ID:
    • RGB
    • Gamma:
    • Brightness:
    • Contrast:
    • Extra Attribute
    • :
    • Gamma:
    • Brightness:
    • Contrast:
    • MATCAP
    • Color
      Elevation
    • :
    • Gradient Scheme:
    • Transition
    • transition:
    • Intensity
    • Range:
    • Gamma:
    • Brightness:
    • Contrast:
    • GPS Time
      Indices
    `); panel.i18n(); this.container.append(panel); { // POINT SIZE let sldPointSize = panel.find(`#sldPointSize`); let lblPointSize = panel.find(`#lblPointSize`); sldPointSize.slider({ value: material.size, min: 0, max: 3, step: 0.01, slide: function (event, ui) { material.size = ui.value; } }); let update = (e) => { lblPointSize.html(material.size.toFixed(2)); sldPointSize.slider({value: material.size}); }; this.addVolatileListener(material, "point_size_changed", update); update(); } { // MINIMUM POINT SIZE let sldMinPointSize = panel.find(`#sldMinPointSize`); let lblMinPointSize = panel.find(`#lblMinPointSize`); sldMinPointSize.slider({ value: material.size, min: 0, max: 3, step: 0.01, slide: function (event, ui) { material.minSize = ui.value; } }); let update = (e) => { lblMinPointSize.html(material.minSize.toFixed(2)); sldMinPointSize.slider({value: material.minSize}); }; this.addVolatileListener(material, "point_size_changed", update); update(); } { // POINT SIZING let strSizeType = Object.keys(PointSizeType)[material.pointSizeType]; let opt = panel.find(`#optPointSizing`); opt.selectmenu(); opt.val(strSizeType).selectmenu('refresh'); opt.selectmenu({ change: (event, ui) => { material.pointSizeType = PointSizeType[ui.item.value]; } }); } { // SHAPE let opt = panel.find(`#optShape`); opt.selectmenu({ change: (event, ui) => { let value = ui.item.value; material.shape = PointShape$1[value]; } }); let update = () => { let typename = Object.keys(PointShape$1)[material.shape]; opt.selectmenu().val(typename).selectmenu('refresh'); }; this.addVolatileListener(material, "point_shape_changed", update); update(); } { // BACKFACE CULLING let opt = panel.find(`#set_backface_culling`); opt.click(() => { material.backfaceCulling = opt.prop("checked"); }); let update = () => { let value = material.backfaceCulling; opt.prop("checked", value); }; this.addVolatileListener(material, "backface_changed", update); update(); let blockBackface = $('#materials_backface_container'); blockBackface.css('display', 'none'); const pointAttributes = pointcloud.pcoGeometry.pointAttributes; const hasNormals = pointAttributes.hasNormals ? pointAttributes.hasNormals() : false; if(hasNormals) { blockBackface.css('display', 'block'); } /* opt.checkboxradio({ clicked: (event, ui) => { // let value = ui.item.value; let value = ui.item.checked; console.log(value); material.backfaceCulling = value; // $('#set_freeze').prop("checked"); } }); */ } { // OPACITY let sldOpacity = panel.find(`#sldOpacity`); let lblOpacity = panel.find(`#lblOpacity`); sldOpacity.slider({ value: material.opacity, min: 0, max: 1, step: 0.001, slide: function (event, ui) { material.opacity = ui.value; } }); let update = (e) => { lblOpacity.html(material.opacity.toFixed(2)); sldOpacity.slider({value: material.opacity}); }; this.addVolatileListener(material, "opacity_changed", update); update(); } { const attributes = pointcloud.pcoGeometry.pointAttributes.attributes; let options = []; options.push(...attributes.map(a => a.name)); const intensityIndex = options.indexOf("intensity"); if(intensityIndex >= 0){ options.splice(intensityIndex + 1, 0, "intensity gradient"); } options.push( "elevation", "color", 'matcap', 'indices', 'level of detail', 'composite' ); const blacklist = [ "POSITION_CARTESIAN", "position", ]; options = options.filter(o => !blacklist.includes(o)); let attributeSelection = panel.find('#optMaterial'); for(let option of options){ let elOption = $(``); attributeSelection.append(elOption); } let updateMaterialPanel = (event, ui) => { let selectedValue = attributeSelection.selectmenu().val(); material.activeAttributeName = selectedValue; let attribute = pointcloud.getAttribute(selectedValue); if(selectedValue === "intensity gradient"){ attribute = pointcloud.getAttribute("intensity"); } const isIntensity = attribute ? ["intensity", "intensity gradient"].includes(attribute.name) : false; if(isIntensity){ if(pointcloud.material.intensityRange[0] === Infinity){ pointcloud.material.intensityRange = attribute.range; } const [min, max] = attribute.range; panel.find('#sldIntensityRange').slider({ range: true, min: min, max: max, step: 0.01, values: [min, max], slide: (event, ui) => { let min = ui.values[0]; let max = ui.values[1]; material.intensityRange = [min, max]; } }); } else if(attribute){ const [min, max] = attribute.range; let selectedRange = material.getRange(attribute.name); if(!selectedRange){ selectedRange = [...attribute.range]; } let minMaxAreNumbers = typeof min === "number" && typeof max === "number"; if(minMaxAreNumbers){ panel.find('#sldExtraRange').slider({ range: true, min: min, max: max, step: 0.01, values: selectedRange, slide: (event, ui) => { let [a, b] = ui.values; material.setRange(attribute.name, [a, b]); } }); } } let blockWeights = $('#materials\\.composite_weight_container'); let blockElevation = $('#materials\\.elevation_container'); let blockRGB = $('#materials\\.rgb_container'); let blockExtra = $('#materials\\.extra_container'); let blockColor = $('#materials\\.color_container'); let blockIntensity = $('#materials\\.intensity_container'); let blockIndex = $('#materials\\.index_container'); let blockTransition = $('#materials\\.transition_container'); let blockGps = $('#materials\\.gpstime_container'); let blockMatcap = $('#materials\\.matcap_container'); blockIndex.css('display', 'none'); blockIntensity.css('display', 'none'); blockElevation.css('display', 'none'); blockRGB.css('display', 'none'); blockExtra.css('display', 'none'); blockColor.css('display', 'none'); blockWeights.css('display', 'none'); blockTransition.css('display', 'none'); blockMatcap.css('display', 'none'); blockGps.css('display', 'none'); if (selectedValue === 'composite') { blockWeights.css('display', 'block'); blockElevation.css('display', 'block'); blockRGB.css('display', 'block'); blockIntensity.css('display', 'block'); } else if (selectedValue === 'elevation') { blockElevation.css('display', 'block'); } else if (selectedValue === 'RGB and Elevation') { blockRGB.css('display', 'block'); blockElevation.css('display', 'block'); } else if (selectedValue === 'rgba') { blockRGB.css('display', 'block'); } else if (selectedValue === 'color') { blockColor.css('display', 'block'); } else if (selectedValue === 'intensity') { blockIntensity.css('display', 'block'); } else if (selectedValue === 'intensity gradient') { blockIntensity.css('display', 'block'); } else if (selectedValue === "indices" ){ blockIndex.css('display', 'block'); } else if (selectedValue === "matcap" ){ blockMatcap.css('display', 'block'); } else if (selectedValue === "classification" ){ // add classification color selctor? } else if (selectedValue === "gps-time" ){ blockGps.css('display', 'block'); } else if(selectedValue === "number of returns"){ } else if(selectedValue === "return number"){ } else if(["source id", "point source id"].includes(selectedValue)){ } else { blockExtra.css('display', 'block'); } }; attributeSelection.selectmenu({change: updateMaterialPanel}); let update = () => { attributeSelection.val(material.activeAttributeName).selectmenu('refresh'); }; this.addVolatileListener(material, "point_color_type_changed", update); this.addVolatileListener(material, "active_attribute_changed", update); update(); updateMaterialPanel(); } { const schemes = Object.keys(Potree.Gradients).map(name => ({name: name, values: Gradients[name]})); let elSchemeContainer = panel.find("#elevation_gradient_scheme_selection"); for(let scheme of schemes){ let elScheme = $(` `); const svg = Potree.Utils.createSvgGradient(scheme.values); svg.setAttributeNS(null, "class", `button-icon`); elScheme.append($(svg)); elScheme.click( () => { material.gradient = Gradients[scheme.name]; }); elSchemeContainer.append(elScheme); } } { let matcaps = [ {name: "Normals", icon: `${Potree.resourcePath}/icons/matcap/check_normal+y.jpg`}, {name: "Basic 1", icon: `${Potree.resourcePath}/icons/matcap/basic_1.jpg`}, {name: "Basic 2", icon: `${Potree.resourcePath}/icons/matcap/basic_2.jpg`}, {name: "Basic Dark", icon: `${Potree.resourcePath}/icons/matcap/basic_dark.jpg`}, {name: "Basic Side", icon: `${Potree.resourcePath}/icons/matcap/basic_side.jpg`}, {name: "Ceramic Dark", icon: `${Potree.resourcePath}/icons/matcap/ceramic_dark.jpg`}, {name: "Ceramic Lightbulb", icon: `${Potree.resourcePath}/icons/matcap/ceramic_lightbulb.jpg`}, {name: "Clay Brown", icon: `${Potree.resourcePath}/icons/matcap/clay_brown.jpg`}, {name: "Clay Muddy", icon: `${Potree.resourcePath}/icons/matcap/clay_muddy.jpg`}, {name: "Clay Studio", icon: `${Potree.resourcePath}/icons/matcap/clay_studio.jpg`}, {name: "Resin", icon: `${Potree.resourcePath}/icons/matcap/resin.jpg`}, {name: "Skin", icon: `${Potree.resourcePath}/icons/matcap/skin.jpg`}, {name: "Jade", icon: `${Potree.resourcePath}/icons/matcap/jade.jpg`}, {name: "Metal_ Anisotropic", icon: `${Potree.resourcePath}/icons/matcap/metal_anisotropic.jpg`}, {name: "Metal Carpaint", icon: `${Potree.resourcePath}/icons/matcap/metal_carpaint.jpg`}, {name: "Metal Lead", icon: `${Potree.resourcePath}/icons/matcap/metal_lead.jpg`}, {name: "Metal Shiny", icon: `${Potree.resourcePath}/icons/matcap/metal_shiny.jpg`}, {name: "Pearl", icon: `${Potree.resourcePath}/icons/matcap/pearl.jpg`}, {name: "Toon", icon: `${Potree.resourcePath}/icons/matcap/toon.jpg`}, {name: "Check Rim Light", icon: `${Potree.resourcePath}/icons/matcap/check_rim_light.jpg`}, {name: "Check Rim Dark", icon: `${Potree.resourcePath}/icons/matcap/check_rim_dark.jpg`}, {name: "Contours 1", icon: `${Potree.resourcePath}/icons/matcap/contours_1.jpg`}, {name: "Contours 2", icon: `${Potree.resourcePath}/icons/matcap/contours_2.jpg`}, {name: "Contours 3", icon: `${Potree.resourcePath}/icons/matcap/contours_3.jpg`}, {name: "Reflection Check Horizontal", icon: `${Potree.resourcePath}/icons/matcap/reflection_check_horizontal.jpg`}, {name: "Reflection Check Vertical", icon: `${Potree.resourcePath}/icons/matcap/reflection_check_vertical.jpg`}, ]; let elMatcapContainer = panel.find("#matcap_scheme_selection"); for(let matcap of matcaps){ let elMatcap = $(` `); elMatcap.click( () => { material.matcap = matcap.icon.substring(matcap.icon.lastIndexOf('/')); }); elMatcapContainer.append(elMatcap); } } { panel.find('#sldRGBGamma').slider({ value: material.rgbGamma, min: 0, max: 4, step: 0.01, slide: (event, ui) => {material.rgbGamma = ui.value;} }); panel.find('#sldRGBContrast').slider({ value: material.rgbContrast, min: -1, max: 1, step: 0.01, slide: (event, ui) => {material.rgbContrast = ui.value;} }); panel.find('#sldRGBBrightness').slider({ value: material.rgbBrightness, min: -1, max: 1, step: 0.01, slide: (event, ui) => {material.rgbBrightness = ui.value;} }); panel.find('#sldExtraGamma').slider({ value: material.extraGamma, min: 0, max: 4, step: 0.01, slide: (event, ui) => {material.extraGamma = ui.value;} }); panel.find('#sldExtraBrightness').slider({ value: material.extraBrightness, min: -1, max: 1, step: 0.01, slide: (event, ui) => {material.extraBrightness = ui.value;} }); panel.find('#sldExtraContrast').slider({ value: material.extraContrast, min: -1, max: 1, step: 0.01, slide: (event, ui) => {material.extraContrast = ui.value;} }); panel.find('#sldHeightRange').slider({ range: true, min: 0, max: 1000, step: 0.01, values: [0, 1000], slide: (event, ui) => { material.heightMin = ui.values[0]; material.heightMax = ui.values[1]; } }); panel.find('#sldIntensityGamma').slider({ value: material.intensityGamma, min: 0, max: 4, step: 0.01, slide: (event, ui) => {material.intensityGamma = ui.value;} }); panel.find('#sldIntensityContrast').slider({ value: material.intensityContrast, min: -1, max: 1, step: 0.01, slide: (event, ui) => {material.intensityContrast = ui.value;} }); panel.find('#sldIntensityBrightness').slider({ value: material.intensityBrightness, min: -1, max: 1, step: 0.01, slide: (event, ui) => {material.intensityBrightness = ui.value;} }); panel.find('#sldWeightRGB').slider({ value: material.weightRGB, min: 0, max: 1, step: 0.01, slide: (event, ui) => {material.weightRGB = ui.value;} }); panel.find('#sldWeightIntensity').slider({ value: material.weightIntensity, min: 0, max: 1, step: 0.01, slide: (event, ui) => {material.weightIntensity = ui.value;} }); panel.find('#sldWeightElevation').slider({ value: material.weightElevation, min: 0, max: 1, step: 0.01, slide: (event, ui) => {material.weightElevation = ui.value;} }); panel.find('#sldWeightClassification').slider({ value: material.weightClassification, min: 0, max: 1, step: 0.01, slide: (event, ui) => {material.weightClassification = ui.value;} }); panel.find('#sldWeightReturnNumber').slider({ value: material.weightReturnNumber, min: 0, max: 1, step: 0.01, slide: (event, ui) => {material.weightReturnNumber = ui.value;} }); panel.find('#sldWeightSourceID').slider({ value: material.weightSourceID, min: 0, max: 1, step: 0.01, slide: (event, ui) => {material.weightSourceID = ui.value;} }); panel.find(`#materials\\.color\\.picker`).spectrum({ flat: true, showInput: true, preferredFormat: 'rgb', cancelText: '', chooseText: 'Apply', color: `#${material.color.getHexString()}`, move: color => { let cRGB = color.toRgb(); let tc = new Color().setRGB(cRGB.r / 255, cRGB.g / 255, cRGB.b / 255); material.color = tc; }, change: color => { let cRGB = color.toRgb(); let tc = new Color().setRGB(cRGB.r / 255, cRGB.g / 255, cRGB.b / 255); material.color = tc; } }); this.addVolatileListener(material, "color_changed", () => { panel.find(`#materials\\.color\\.picker`) .spectrum('set', `#${material.color.getHexString()}`); }); let updateHeightRange = function () { let aPosition = pointcloud.getAttribute("position"); let bMin, bMax; if(aPosition){ // for new format 2.0 and loader that contain precomputed min/max of attributes let min = aPosition.range[0][2]; let max = aPosition.range[1][2]; let width = max - min; bMin = min - 0.2 * width; bMax = max + 0.2 * width; }else { // for format up until exlusive 2.0 let box = [pointcloud.pcoGeometry.tightBoundingBox, pointcloud.getBoundingBoxWorld()] .find(v => v !== undefined); pointcloud.updateMatrixWorld(true); box = Utils.computeTransformedBoundingBox(box, pointcloud.matrixWorld); let bWidth = box.max.z - box.min.z; bMin = box.min.z - 0.2 * bWidth; bMax = box.max.z + 0.2 * bWidth; } let range = material.elevationRange; panel.find('#lblHeightRange').html(`${range[0].toFixed(2)} to ${range[1].toFixed(2)}`); panel.find('#sldHeightRange').slider({min: bMin, max: bMax, values: range}); }; let updateExtraRange = function () { let attributeName = material.activeAttributeName; let attribute = pointcloud.getAttribute(attributeName); if(attribute == null){ return; } let range = material.getRange(attributeName); if(range == null){ range = attribute.range; } // currently only supporting scalar ranges. // rgba, normals, positions, etc have vector ranges, however let isValidRange = (typeof range[0] === "number") && (typeof range[1] === "number"); if(!isValidRange){ return; } if(range){ let msg = `${range[0].toFixed(2)} to ${range[1].toFixed(2)}`; panel.find('#lblExtraRange').html(msg); }else { panel.find("could not deduce range"); } }; let updateIntensityRange = function () { let range = material.intensityRange; panel.find('#lblIntensityRange').html(`${parseInt(range[0])} to ${parseInt(range[1])}`); }; { updateHeightRange(); panel.find(`#sldHeightRange`).slider('option', 'min'); panel.find(`#sldHeightRange`).slider('option', 'max'); } { let elGradientRepeat = panel.find("#gradient_repeat_option"); elGradientRepeat.selectgroup({title: "Gradient"}); elGradientRepeat.find("input").click( (e) => { this.viewer.setElevationGradientRepeat(ElevationGradientRepeat[e.target.value]); }); let current = Object.keys(ElevationGradientRepeat) .filter(key => ElevationGradientRepeat[key] === this.viewer.elevationGradientRepeat); elGradientRepeat.find(`input[value=${current}]`).trigger("click"); } let onIntensityChange = () => { let gamma = material.intensityGamma; let contrast = material.intensityContrast; let brightness = material.intensityBrightness; updateIntensityRange(); panel.find('#lblIntensityGamma').html(gamma.toFixed(2)); panel.find('#lblIntensityContrast').html(contrast.toFixed(2)); panel.find('#lblIntensityBrightness').html(brightness.toFixed(2)); panel.find('#sldIntensityGamma').slider({value: gamma}); panel.find('#sldIntensityContrast').slider({value: contrast}); panel.find('#sldIntensityBrightness').slider({value: brightness}); }; let onRGBChange = () => { let gamma = material.rgbGamma; let contrast = material.rgbContrast; let brightness = material.rgbBrightness; panel.find('#lblRGBGamma').html(gamma.toFixed(2)); panel.find('#lblRGBContrast').html(contrast.toFixed(2)); panel.find('#lblRGBBrightness').html(brightness.toFixed(2)); panel.find('#sldRGBGamma').slider({value: gamma}); panel.find('#sldRGBContrast').slider({value: contrast}); panel.find('#sldRGBBrightness').slider({value: brightness}); }; this.addVolatileListener(material, "material_property_changed", updateExtraRange); this.addVolatileListener(material, "material_property_changed", updateHeightRange); this.addVolatileListener(material, "material_property_changed", onIntensityChange); this.addVolatileListener(material, "material_property_changed", onRGBChange); updateExtraRange(); updateHeightRange(); onIntensityChange(); onRGBChange(); } } setMeasurement(object){ let TYPE = { DISTANCE: {panel: DistancePanel}, AREA: {panel: AreaPanel}, POINT: {panel: PointPanel}, ANGLE: {panel: AnglePanel}, HEIGHT: {panel: HeightPanel}, PROFILE: {panel: ProfilePanel}, VOLUME: {panel: VolumePanel}, CIRCLE: {panel: CirclePanel}, OTHER: {panel: PointPanel}, }; let getType = (measurement) => { if (measurement instanceof Measure) { if (measurement.showDistances && !measurement.showArea && !measurement.showAngles) { return TYPE.DISTANCE; } else if (measurement.showDistances && measurement.showArea && !measurement.showAngles) { return TYPE.AREA; } else if (measurement.maxMarkers === 1) { return TYPE.POINT; } else if (!measurement.showDistances && !measurement.showArea && measurement.showAngles) { return TYPE.ANGLE; } else if (measurement.showHeight) { return TYPE.HEIGHT; } else if (measurement.showCircle) { return TYPE.CIRCLE; } else { return TYPE.OTHER; } } else if (measurement instanceof Profile) { return TYPE.PROFILE; } else if (measurement instanceof Volume) { return TYPE.VOLUME; } }; //this.container.html("measurement"); let type = getType(object); let Panel = type.panel; let panel = new Panel(this.viewer, object, this); this.container.append(panel.elContent); } setCamera(camera){ let panel = new CameraPanel(this.viewer, this); this.container.append(panel.elContent); } setAnnotation(annotation){ let panel = new AnnotationPanel(this.viewer, this, annotation); this.container.append(panel.elContent); } setCameraAnimation(animation){ let panel = new CameraAnimationPanel(this.viewer, this, animation); this.container.append(panel.elContent); } } function addCommas(nStr){ nStr += ''; let x = nStr.split('.'); let x1 = x[0]; let x2 = x.length > 1 ? '.' + x[1] : ''; let rgx = /(\d+)(\d{3})/; while (rgx.test(x1)) { x1 = x1.replace(rgx, '$1' + ',' + '$2'); } return x1 + x2; }; function format(value){ return addCommas(value.toFixed(3)); }; class HierarchicalSlider{ constructor(params = {}){ this.element = document.createElement("div"); this.labels = []; this.sliders = []; this.range = params.range != null ? params.range : [0, 1]; this.slide = params.slide != null ? params.slide : null; this.step = params.step != null ? params.step : 0.0001; let levels = params.levels != null ? params.levels : 1; for(let level = 0; level < levels; level++){ this.addLevel(); } } setRange(range){ this.range = [...range]; { // root slider let slider = this.sliders[0]; $(slider).slider({ min: range[0], max: range[1], }); } for(let i = 1; i < this.sliders.length; i++){ let parentSlider = this.sliders[i - 1]; let slider = this.sliders[i]; let parentValues = $(parentSlider).slider("option", "values"); let childRange = [...parentValues]; $(slider).slider({ min: childRange[0], max: childRange[1], }); } this.updateLabels(); } setValues(values){ for(let slider of this.sliders){ $(slider).slider({ values: [...values], }); } this.updateLabels(); } addLevel(){ const elLevel = document.createElement("li"); const elRange = document.createTextNode("Range: "); const label = document.createElement("span"); const slider = document.createElement("div"); let level = this.sliders.length; let [min, max] = [0, 0]; if(this.sliders.length === 0){ [min, max] = this.range; }else { let parentSlider = this.sliders[this.sliders.length - 1]; [min, max] = $(parentSlider).slider("option", "values"); } $(slider).slider({ range: true, min: min, max: max, step: this.step, values: [min, max], slide: (event, ui) => { // set all descendants to same range let levels = this.sliders.length; for(let i = level + 1; i < levels; i++){ let descendant = this.sliders[i]; $(descendant).slider({ range: true, min: ui.values[0], max: ui.values[1], values: [...ui.values], }); } if(this.slide){ let values = [...ui.values]; this.slide({ target: this, range: this.range, values: values, }); } this.updateLabels(); }, }); elLevel.append(elRange, label, slider); this.sliders.push(slider); this.labels.push(label); this.element.append(elLevel); this.updateLabels(); } removeLevel(){ } updateSliders(){ } updateLabels(){ let levels = this.sliders.length; for(let i = 0; i < levels; i++){ let slider = this.sliders[i]; let label = this.labels[i]; let [min, max] = $(slider).slider("option", "values"); let strMin = format(min); let strMax = format(max); let strLabel = `${strMin} to ${strMax}`; label.innerHTML = strLabel; } } } class OrientedImageControls extends EventDispatcher$1{ constructor(viewer){ super(); this.viewer = viewer; this.renderer = viewer.renderer; this.originalCam = viewer.scene.getActiveCamera(); this.shearCam = viewer.scene.getActiveCamera().clone(); this.shearCam.rotation.set(this.originalCam.rotation.toArray()); this.shearCam.updateProjectionMatrix(); this.shearCam.updateProjectionMatrix = () => { return this.shearCam.projectionMatrix; }; this.image = null; this.fadeFactor = 20; this.fovDelta = 0; this.fovMin = 0.1; this.fovMax = 120; this.shear = [0, 0]; // const style = ``; this.elUp = $(``); this.elRight = $(``); this.elDown = $(``); this.elLeft = $(``); this.elExit = $(``); this.elExit.click( () => { this.release(); }); this.elUp.click(() => { const fovY = viewer.getFOV(); const top = Math.tan(MathUtils.degToRad(fovY / 2)); this.shear[1] += 0.1 * top; }); this.elRight.click(() => { const fovY = viewer.getFOV(); const top = Math.tan(MathUtils.degToRad(fovY / 2)); this.shear[0] += 0.1 * top; }); this.elDown.click(() => { const fovY = viewer.getFOV(); const top = Math.tan(MathUtils.degToRad(fovY / 2)); this.shear[1] -= 0.1 * top; }); this.elLeft.click(() => { const fovY = viewer.getFOV(); const top = Math.tan(MathUtils.degToRad(fovY / 2)); this.shear[0] -= 0.1 * top; }); this.scene = null; this.sceneControls = new Scene(); let scroll = (e) => { this.fovDelta += -e.delta * 1.0; }; this.addEventListener('mousewheel', scroll); //this.addEventListener("mousemove", onMove); } hasSomethingCaptured(){ return this.image !== null; } capture(image){ if(this.hasSomethingCaptured()){ return; } this.image = image; this.originalFOV = this.viewer.getFOV(); this.originalControls = this.viewer.getControls(); this.viewer.setControls(this); this.viewer.scene.overrideCamera = this.shearCam; const elCanvas = this.viewer.renderer.domElement; const elRoot = $(elCanvas.parentElement); this.shear = [0, 0]; elRoot.append(this.elUp); elRoot.append(this.elRight); elRoot.append(this.elDown); elRoot.append(this.elLeft); elRoot.append(this.elExit); } release(){ this.image = null; this.viewer.scene.overrideCamera = null; this.elUp.detach(); this.elRight.detach(); this.elDown.detach(); this.elLeft.detach(); this.elExit.detach(); this.viewer.setFOV(this.originalFOV); this.viewer.setControls(this.originalControls); } setScene (scene) { this.scene = scene; } update (delta) { // const view = this.scene.view; // let prevTotal = this.shearCam.projectionMatrix.elements.reduce( (a, i) => a + i, 0); //const progression = Math.min(1, this.fadeFactor * delta); //const attenuation = Math.max(0, 1 - this.fadeFactor * delta); const progression = 1; const attenuation = 0; const oldFov = this.viewer.getFOV(); let fovProgression = progression * this.fovDelta; let newFov = oldFov * ((1 + fovProgression / 10)); newFov = Math.max(this.fovMin, newFov); newFov = Math.min(this.fovMax, newFov); let diff = newFov / oldFov; const mouse = this.viewer.inputHandler.mouse; const canvasSize = this.viewer.renderer.getSize(new Vector2()); const uv = [ (mouse.x / canvasSize.x), ((canvasSize.y - mouse.y) / canvasSize.y) ]; const fovY = newFov; const aspect = canvasSize.x / canvasSize.y; const top = Math.tan(MathUtils.degToRad(fovY / 2)); const height = 2 * top; const width = aspect * height; const shearRangeX = [ this.shear[0] - 0.5 * width, this.shear[0] + 0.5 * width, ]; const shearRangeY = [ this.shear[1] - 0.5 * height, this.shear[1] + 0.5 * height, ]; const shx = (1 - uv[0]) * shearRangeX[0] + uv[0] * shearRangeX[1]; const shy = (1 - uv[1]) * shearRangeY[0] + uv[1] * shearRangeY[1]; const shu = (1 - diff); const newShear = [ (1 - shu) * this.shear[0] + shu * shx, (1 - shu) * this.shear[1] + shu * shy, ]; this.shear = newShear; this.viewer.setFOV(newFov); const {originalCam, shearCam} = this; originalCam.fov = newFov; originalCam.updateMatrixWorld(); originalCam.updateProjectionMatrix(); shearCam.copy(originalCam); shearCam.rotation.set(...originalCam.rotation.toArray()); shearCam.updateMatrixWorld(); shearCam.projectionMatrix.copy(originalCam.projectionMatrix); const [sx, sy] = this.shear; const mShear = new Matrix4().set( 1, 0, sx, 0, 0, 1, sy, 0, 0, 0, 1, 0, 0, 0, 0, 1, ); const proj = shearCam.projectionMatrix; proj.multiply(mShear); shearCam.projectionMatrixInverse.copy(proj).invert(); let total = shearCam.projectionMatrix.elements.reduce( (a, i) => a + i, 0); this.fovDelta *= attenuation; } }; // https://support.pix4d.com/hc/en-us/articles/205675256-How-are-yaw-pitch-roll-defined // https://support.pix4d.com/hc/en-us/articles/202558969-How-are-omega-phi-kappa-defined function createMaterial(){ let vertexShader = ` uniform float uNear; varying vec2 vUV; varying vec4 vDebug; void main(){ vDebug = vec4(0.0, 1.0, 0.0, 1.0); vec4 modelViewPosition = modelViewMatrix * vec4(position, 1.0); // make sure that this mesh is at least in front of the near plane modelViewPosition.xyz += normalize(modelViewPosition.xyz) * uNear; gl_Position = projectionMatrix * modelViewPosition; vUV = uv; } `; let fragmentShader = ` uniform sampler2D tColor; uniform float uOpacity; varying vec2 vUV; varying vec4 vDebug; void main(){ vec4 color = texture2D(tColor, vUV); gl_FragColor = color; gl_FragColor.a = uOpacity; } `; const material = new ShaderMaterial( { uniforms: { // time: { value: 1.0 }, // resolution: { value: new THREE.Vector2() } tColor: {value: new Texture() }, uNear: {value: 0.0}, uOpacity: {value: 1.0}, }, vertexShader: vertexShader, fragmentShader: fragmentShader, side: DoubleSide, } ); material.side = DoubleSide; return material; } const planeGeometry = new PlaneGeometry(1, 1); const lineGeometry = new Geometry(); lineGeometry.vertices.push( new Vector3(-0.5, -0.5, 0), new Vector3( 0.5, -0.5, 0), new Vector3( 0.5, 0.5, 0), new Vector3(-0.5, 0.5, 0), new Vector3(-0.5, -0.5, 0), ); class OrientedImage{ constructor(id){ this.id = id; this.fov = 1.0; this.position = new Vector3(); this.rotation = new Vector3(); this.width = 0; this.height = 0; this.fov = 1.0; const material = createMaterial(); const lineMaterial = new LineBasicMaterial( { color: 0x00ff00 } ); this.mesh = new Mesh(planeGeometry, material); this.line = new Line(lineGeometry, lineMaterial); this.texture = null; this.mesh.orientedImage = this; } set(position, rotation, dimension, fov){ let radians = rotation.map(MathUtils.degToRad); this.position.set(...position); this.mesh.position.set(...position); this.rotation.set(...radians); this.mesh.rotation.set(...radians); [this.width, this.height] = dimension; this.mesh.scale.set(this.width / this.height, 1, 1); this.fov = fov; this.updateTransform(); } updateTransform(){ let {mesh, line, fov} = this; mesh.updateMatrixWorld(); const dir = mesh.getWorldDirection(); const alpha = MathUtils.degToRad(fov / 2); const d = -0.5 / Math.tan(alpha); const move = dir.clone().multiplyScalar(d); mesh.position.add(move); line.position.copy(mesh.position); line.scale.copy(mesh.scale); line.rotation.copy(mesh.rotation); } }; class OrientedImages extends EventDispatcher$1{ constructor(){ super(); this.node = null; this.cameraParams = null; this.imageParams = null; this.images = null; this._visible = true; } set visible(visible){ if(this._visible === visible){ return; } for(const image of this.images){ image.mesh.visible = visible; image.line.visible = visible; } this._visible = visible; this.dispatchEvent({ type: "visibility_changed", images: this, }); } get visible(){ return this._visible; } }; class OrientedImageLoader{ static async loadCameraParams(path){ const res = await fetch(path); const text = await res.text(); const parser = new DOMParser(); const doc = parser.parseFromString(text, "application/xml"); const width = parseInt(doc.getElementsByTagName("width")[0].textContent); const height = parseInt(doc.getElementsByTagName("height")[0].textContent); const f = parseFloat(doc.getElementsByTagName("f")[0].textContent); let a = (height / 2) / f; let fov = 2 * MathUtils.radToDeg(Math.atan(a)); const params = { path: path, width: width, height: height, f: f, fov: fov, }; return params; } static async loadImageParams(path){ const response = await fetch(path); if(!response.ok){ console.error(`failed to load ${path}`); return; } const content = await response.text(); const lines = content.split(/\r?\n/); const imageParams = []; for(let i = 1; i < lines.length; i++){ const line = lines[i]; if(line.startsWith("#")){ continue; } const tokens = line.split(/\s+/); if(tokens.length < 6){ continue; } const params = { id: tokens[0], x: Number.parseFloat(tokens[1]), y: Number.parseFloat(tokens[2]), z: Number.parseFloat(tokens[3]), omega: Number.parseFloat(tokens[4]), phi: Number.parseFloat(tokens[5]), kappa: Number.parseFloat(tokens[6]), }; // const whitelist = ["47518.jpg"]; // if(whitelist.includes(params.id)){ // imageParams.push(params); // } imageParams.push(params); } // debug //return [imageParams[50]]; return imageParams; } static async load(cameraParamsPath, imageParamsPath, viewer){ const tStart = performance.now(); const [cameraParams, imageParams] = await Promise.all([ OrientedImageLoader.loadCameraParams(cameraParamsPath), OrientedImageLoader.loadImageParams(imageParamsPath), ]); const orientedImageControls = new OrientedImageControls(viewer); const raycaster = new Raycaster(); const tEnd = performance.now(); console.log(tEnd - tStart); // const sp = new THREE.PlaneGeometry(1, 1); // const lg = new THREE.Geometry(); // lg.vertices.push( // new THREE.Vector3(-0.5, -0.5, 0), // new THREE.Vector3( 0.5, -0.5, 0), // new THREE.Vector3( 0.5, 0.5, 0), // new THREE.Vector3(-0.5, 0.5, 0), // new THREE.Vector3(-0.5, -0.5, 0), // ); const {width, height} = cameraParams; const orientedImages = []; const sceneNode = new Object3D(); sceneNode.name = "oriented_images"; for(const params of imageParams){ // const material = createMaterial(); // const lm = new THREE.LineBasicMaterial( { color: 0x00ff00 } ); // const mesh = new THREE.Mesh(sp, material); const {x, y, z, omega, phi, kappa} = params; // const [rx, ry, rz] = [omega, phi, kappa] // .map(THREE.Math.degToRad); // mesh.position.set(x, y, z); // mesh.scale.set(width / height, 1, 1); // mesh.rotation.set(rx, ry, rz); // { // mesh.updateMatrixWorld(); // const dir = mesh.getWorldDirection(); // const alpha = THREE.Math.degToRad(cameraParams.fov / 2); // const d = -0.5 / Math.tan(alpha); // const move = dir.clone().multiplyScalar(d); // mesh.position.add(move); // } // sceneNode.add(mesh); // const line = new THREE.Line(lg, lm); // line.position.copy(mesh.position); // line.scale.copy(mesh.scale); // line.rotation.copy(mesh.rotation); // sceneNode.add(line); let orientedImage = new OrientedImage(params.id); // orientedImage.setPosition(x, y, z); // orientedImage.setRotation(omega, phi, kappa); // orientedImage.setDimension(width, height); let position = [x, y, z]; let rotation = [omega, phi, kappa]; let dimension = [width, height]; orientedImage.set(position, rotation, dimension, cameraParams.fov); sceneNode.add(orientedImage.mesh); sceneNode.add(orientedImage.line); orientedImages.push(orientedImage); } let hoveredElement = null; let clipVolume = null; const onMouseMove = (evt) => { const tStart = performance.now(); if(hoveredElement){ hoveredElement.line.material.color.setRGB(0, 1, 0); } evt.preventDefault(); //var array = getMousePosition( container, evt.clientX, evt.clientY ); const rect = viewer.renderer.domElement.getBoundingClientRect(); const [x, y] = [evt.clientX, evt.clientY]; const array = [ ( x - rect.left ) / rect.width, ( y - rect.top ) / rect.height ]; const onClickPosition = new Vector2(...array); //const intersects = getIntersects(onClickPosition, scene.children); const camera = viewer.scene.getActiveCamera(); const mouse = new Vector3( + ( onClickPosition.x * 2 ) - 1, - ( onClickPosition.y * 2 ) + 1 ); const objects = orientedImages.map(i => i.mesh); raycaster.setFromCamera( mouse, camera ); const intersects = raycaster.intersectObjects( objects ); let selectionChanged = false; if ( intersects.length > 0){ //console.log(intersects); const intersection = intersects[0]; const orientedImage = intersection.object.orientedImage; orientedImage.line.material.color.setRGB(1, 0, 0); selectionChanged = hoveredElement !== orientedImage; hoveredElement = orientedImage; }else { hoveredElement = null; } let shouldRemoveClipVolume = clipVolume !== null && hoveredElement === null; let shouldAddClipVolume = clipVolume === null && hoveredElement !== null; if(clipVolume !== null && (hoveredElement === null || selectionChanged)){ // remove existing viewer.scene.removePolygonClipVolume(clipVolume); clipVolume = null; } if(shouldAddClipVolume || selectionChanged){ const img = hoveredElement; const fov = cameraParams.fov; const aspect = cameraParams.width / cameraParams.height; const near = 1.0; const far = 1000 * 1000; const camera = new PerspectiveCamera(fov, aspect, near, far); camera.rotation.order = viewer.scene.getActiveCamera().rotation.order; camera.rotation.copy(img.mesh.rotation); { const mesh = img.mesh; const dir = mesh.getWorldDirection(); const pos = mesh.position; const alpha = MathUtils.degToRad(fov / 2); const d = 0.5 / Math.tan(alpha); const newCamPos = pos.clone().add(dir.clone().multiplyScalar(d)); const newCamDir = pos.clone().sub(newCamPos); const newCamTarget = new Vector3().addVectors( newCamPos, newCamDir.clone().multiplyScalar(viewer.getMoveSpeed())); camera.position.copy(newCamPos); } let volume = new Potree.PolygonClipVolume(camera); let m0 = new Mesh(); let m1 = new Mesh(); let m2 = new Mesh(); let m3 = new Mesh(); m0.position.set(-1, -1, 0); m1.position.set( 1, -1, 0); m2.position.set( 1, 1, 0); m3.position.set(-1, 1, 0); volume.markers.push(m0, m1, m2, m3); volume.initialized = true; viewer.scene.addPolygonClipVolume(volume); clipVolume = volume; } const tEnd = performance.now(); //console.log(tEnd - tStart); }; const moveToImage = (image) => { console.log("move to image " + image.id); const mesh = image.mesh; const newCamPos = image.position.clone(); const newCamTarget = mesh.position.clone(); viewer.scene.view.setView(newCamPos, newCamTarget, 500, () => { orientedImageControls.capture(image); }); if(image.texture === null){ const target = image; const tmpImagePath = `${Potree.resourcePath}/images/loading.jpg`; new TextureLoader().load(tmpImagePath, (texture) => { if(target.texture === null){ target.texture = texture; target.mesh.material.uniforms.tColor.value = texture; mesh.material.needsUpdate = true; } } ); const imagePath = `${imageParamsPath}/../${target.id}`; new TextureLoader().load(imagePath, (texture) => { target.texture = texture; target.mesh.material.uniforms.tColor.value = texture; mesh.material.needsUpdate = true; } ); } }; const onMouseClick = (evt) => { if(orientedImageControls.hasSomethingCaptured()){ return; } if(hoveredElement){ moveToImage(hoveredElement); } }; viewer.renderer.domElement.addEventListener( 'mousemove', onMouseMove, false ); viewer.renderer.domElement.addEventListener( 'mousedown', onMouseClick, false ); viewer.addEventListener("update", () => { for(const image of orientedImages){ const world = image.mesh.matrixWorld; const {width, height} = image; const aspect = width / height; const camera = viewer.scene.getActiveCamera(); const imgPos = image.mesh.getWorldPosition(new Vector3()); const camPos = camera.position; const d = camPos.distanceTo(imgPos); const minSize = 1; // in degrees of fov const a = MathUtils.degToRad(minSize); let r = d * Math.tan(a); r = Math.max(r, 1); image.mesh.scale.set(r * aspect, r, 1); image.line.scale.set(r * aspect, r, 1); image.mesh.material.uniforms.uNear.value = camera.near; } }); const images = new OrientedImages(); images.node = sceneNode; images.cameraParamsPath = cameraParamsPath; images.imageParamsPath = imageParamsPath; images.cameraParams = cameraParams; images.imageParams = imageParams; images.images = orientedImages; Potree.debug.moveToImage = moveToImage; return images; } } // This is a generated file. Do not edit. var Space_Separator = /[\u1680\u2000-\u200A\u202F\u205F\u3000]/; var ID_Start = /[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u0860-\u086A\u08A0-\u08B4\u08B6-\u08BD\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0980\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u09FC\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0AF9\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D\u0C58-\u0C5A\u0C60\u0C61\u0C80\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D54-\u0D56\u0D5F-\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u1884\u1887-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191E\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1C80-\u1C88\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005-\u3007\u3021-\u3029\u3031-\u3035\u3038-\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312E\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FEA\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA69D\uA6A0-\uA6EF\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AE\uA7B0-\uA7B7\uA7F7-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA8FD\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uA9E0-\uA9E4\uA9E6-\uA9EF\uA9FA-\uA9FE\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA7E-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDE80-\uDE9C\uDEA0-\uDED0\uDF00-\uDF1F\uDF2D-\uDF4A\uDF50-\uDF75\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00\uDE10-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE4\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC03-\uDC37\uDC83-\uDCAF\uDCD0-\uDCE8\uDD03-\uDD26\uDD50-\uDD72\uDD76\uDD83-\uDDB2\uDDC1-\uDDC4\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE2B\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEDE\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3D\uDF50\uDF5D-\uDF61]|\uD805[\uDC00-\uDC34\uDC47-\uDC4A\uDC80-\uDCAF\uDCC4\uDCC5\uDCC7\uDD80-\uDDAE\uDDD8-\uDDDB\uDE00-\uDE2F\uDE44\uDE80-\uDEAA\uDF00-\uDF19]|\uD806[\uDCA0-\uDCDF\uDCFF\uDE00\uDE0B-\uDE32\uDE3A\uDE50\uDE5C-\uDE83\uDE86-\uDE89\uDEC0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC2E\uDC40\uDC72-\uDC8F\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD30\uDD46]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD81C-\uD820\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDED0-\uDEED\uDF00-\uDF2F\uDF40-\uDF43\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50\uDF93-\uDF9F\uDFE0\uDFE1]|\uD821[\uDC00-\uDFEC]|\uD822[\uDC00-\uDEF2]|\uD82C[\uDC00-\uDD1E\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB]|\uD83A[\uDC00-\uDCC4\uDD00-\uDD43]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0]|\uD87E[\uDC00-\uDE1D]/; var ID_Continue = /[\xAA\xB5\xBA\xC0-\xD6\xD8-\xF6\xF8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0300-\u0374\u0376\u0377\u037A-\u037D\u037F\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u0483-\u0487\u048A-\u052F\u0531-\u0556\u0559\u0561-\u0587\u0591-\u05BD\u05BF\u05C1\u05C2\u05C4\u05C5\u05C7\u05D0-\u05EA\u05F0-\u05F2\u0610-\u061A\u0620-\u0669\u066E-\u06D3\u06D5-\u06DC\u06DF-\u06E8\u06EA-\u06FC\u06FF\u0710-\u074A\u074D-\u07B1\u07C0-\u07F5\u07FA\u0800-\u082D\u0840-\u085B\u0860-\u086A\u08A0-\u08B4\u08B6-\u08BD\u08D4-\u08E1\u08E3-\u0963\u0966-\u096F\u0971-\u0983\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BC-\u09C4\u09C7\u09C8\u09CB-\u09CE\u09D7\u09DC\u09DD\u09DF-\u09E3\u09E6-\u09F1\u09FC\u0A01-\u0A03\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A3C\u0A3E-\u0A42\u0A47\u0A48\u0A4B-\u0A4D\u0A51\u0A59-\u0A5C\u0A5E\u0A66-\u0A75\u0A81-\u0A83\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABC-\u0AC5\u0AC7-\u0AC9\u0ACB-\u0ACD\u0AD0\u0AE0-\u0AE3\u0AE6-\u0AEF\u0AF9-\u0AFF\u0B01-\u0B03\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3C-\u0B44\u0B47\u0B48\u0B4B-\u0B4D\u0B56\u0B57\u0B5C\u0B5D\u0B5F-\u0B63\u0B66-\u0B6F\u0B71\u0B82\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BBE-\u0BC2\u0BC6-\u0BC8\u0BCA-\u0BCD\u0BD0\u0BD7\u0BE6-\u0BEF\u0C00-\u0C03\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C39\u0C3D-\u0C44\u0C46-\u0C48\u0C4A-\u0C4D\u0C55\u0C56\u0C58-\u0C5A\u0C60-\u0C63\u0C66-\u0C6F\u0C80-\u0C83\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBC-\u0CC4\u0CC6-\u0CC8\u0CCA-\u0CCD\u0CD5\u0CD6\u0CDE\u0CE0-\u0CE3\u0CE6-\u0CEF\u0CF1\u0CF2\u0D00-\u0D03\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D44\u0D46-\u0D48\u0D4A-\u0D4E\u0D54-\u0D57\u0D5F-\u0D63\u0D66-\u0D6F\u0D7A-\u0D7F\u0D82\u0D83\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0DCA\u0DCF-\u0DD4\u0DD6\u0DD8-\u0DDF\u0DE6-\u0DEF\u0DF2\u0DF3\u0E01-\u0E3A\u0E40-\u0E4E\u0E50-\u0E59\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB9\u0EBB-\u0EBD\u0EC0-\u0EC4\u0EC6\u0EC8-\u0ECD\u0ED0-\u0ED9\u0EDC-\u0EDF\u0F00\u0F18\u0F19\u0F20-\u0F29\u0F35\u0F37\u0F39\u0F3E-\u0F47\u0F49-\u0F6C\u0F71-\u0F84\u0F86-\u0F97\u0F99-\u0FBC\u0FC6\u1000-\u1049\u1050-\u109D\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u135D-\u135F\u1380-\u138F\u13A0-\u13F5\u13F8-\u13FD\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u16EE-\u16F8\u1700-\u170C\u170E-\u1714\u1720-\u1734\u1740-\u1753\u1760-\u176C\u176E-\u1770\u1772\u1773\u1780-\u17D3\u17D7\u17DC\u17DD\u17E0-\u17E9\u180B-\u180D\u1810-\u1819\u1820-\u1877\u1880-\u18AA\u18B0-\u18F5\u1900-\u191E\u1920-\u192B\u1930-\u193B\u1946-\u196D\u1970-\u1974\u1980-\u19AB\u19B0-\u19C9\u19D0-\u19D9\u1A00-\u1A1B\u1A20-\u1A5E\u1A60-\u1A7C\u1A7F-\u1A89\u1A90-\u1A99\u1AA7\u1AB0-\u1ABD\u1B00-\u1B4B\u1B50-\u1B59\u1B6B-\u1B73\u1B80-\u1BF3\u1C00-\u1C37\u1C40-\u1C49\u1C4D-\u1C7D\u1C80-\u1C88\u1CD0-\u1CD2\u1CD4-\u1CF9\u1D00-\u1DF9\u1DFB-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u203F\u2040\u2054\u2071\u207F\u2090-\u209C\u20D0-\u20DC\u20E1\u20E5-\u20F0\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2160-\u2188\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D7F-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2DE0-\u2DFF\u2E2F\u3005-\u3007\u3021-\u302F\u3031-\u3035\u3038-\u303C\u3041-\u3096\u3099\u309A\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312E\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FEA\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA62B\uA640-\uA66F\uA674-\uA67D\uA67F-\uA6F1\uA717-\uA71F\uA722-\uA788\uA78B-\uA7AE\uA7B0-\uA7B7\uA7F7-\uA827\uA840-\uA873\uA880-\uA8C5\uA8D0-\uA8D9\uA8E0-\uA8F7\uA8FB\uA8FD\uA900-\uA92D\uA930-\uA953\uA960-\uA97C\uA980-\uA9C0\uA9CF-\uA9D9\uA9E0-\uA9FE\uAA00-\uAA36\uAA40-\uAA4D\uAA50-\uAA59\uAA60-\uAA76\uAA7A-\uAAC2\uAADB-\uAADD\uAAE0-\uAAEF\uAAF2-\uAAF6\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uAB30-\uAB5A\uAB5C-\uAB65\uAB70-\uABEA\uABEC\uABED\uABF0-\uABF9\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE00-\uFE0F\uFE20-\uFE2F\uFE33\uFE34\uFE4D-\uFE4F\uFE70-\uFE74\uFE76-\uFEFC\uFF10-\uFF19\uFF21-\uFF3A\uFF3F\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]|\uD800[\uDC00-\uDC0B\uDC0D-\uDC26\uDC28-\uDC3A\uDC3C\uDC3D\uDC3F-\uDC4D\uDC50-\uDC5D\uDC80-\uDCFA\uDD40-\uDD74\uDDFD\uDE80-\uDE9C\uDEA0-\uDED0\uDEE0\uDF00-\uDF1F\uDF2D-\uDF4A\uDF50-\uDF7A\uDF80-\uDF9D\uDFA0-\uDFC3\uDFC8-\uDFCF\uDFD1-\uDFD5]|\uD801[\uDC00-\uDC9D\uDCA0-\uDCA9\uDCB0-\uDCD3\uDCD8-\uDCFB\uDD00-\uDD27\uDD30-\uDD63\uDE00-\uDF36\uDF40-\uDF55\uDF60-\uDF67]|\uD802[\uDC00-\uDC05\uDC08\uDC0A-\uDC35\uDC37\uDC38\uDC3C\uDC3F-\uDC55\uDC60-\uDC76\uDC80-\uDC9E\uDCE0-\uDCF2\uDCF4\uDCF5\uDD00-\uDD15\uDD20-\uDD39\uDD80-\uDDB7\uDDBE\uDDBF\uDE00-\uDE03\uDE05\uDE06\uDE0C-\uDE13\uDE15-\uDE17\uDE19-\uDE33\uDE38-\uDE3A\uDE3F\uDE60-\uDE7C\uDE80-\uDE9C\uDEC0-\uDEC7\uDEC9-\uDEE6\uDF00-\uDF35\uDF40-\uDF55\uDF60-\uDF72\uDF80-\uDF91]|\uD803[\uDC00-\uDC48\uDC80-\uDCB2\uDCC0-\uDCF2]|\uD804[\uDC00-\uDC46\uDC66-\uDC6F\uDC7F-\uDCBA\uDCD0-\uDCE8\uDCF0-\uDCF9\uDD00-\uDD34\uDD36-\uDD3F\uDD50-\uDD73\uDD76\uDD80-\uDDC4\uDDCA-\uDDCC\uDDD0-\uDDDA\uDDDC\uDE00-\uDE11\uDE13-\uDE37\uDE3E\uDE80-\uDE86\uDE88\uDE8A-\uDE8D\uDE8F-\uDE9D\uDE9F-\uDEA8\uDEB0-\uDEEA\uDEF0-\uDEF9\uDF00-\uDF03\uDF05-\uDF0C\uDF0F\uDF10\uDF13-\uDF28\uDF2A-\uDF30\uDF32\uDF33\uDF35-\uDF39\uDF3C-\uDF44\uDF47\uDF48\uDF4B-\uDF4D\uDF50\uDF57\uDF5D-\uDF63\uDF66-\uDF6C\uDF70-\uDF74]|\uD805[\uDC00-\uDC4A\uDC50-\uDC59\uDC80-\uDCC5\uDCC7\uDCD0-\uDCD9\uDD80-\uDDB5\uDDB8-\uDDC0\uDDD8-\uDDDD\uDE00-\uDE40\uDE44\uDE50-\uDE59\uDE80-\uDEB7\uDEC0-\uDEC9\uDF00-\uDF19\uDF1D-\uDF2B\uDF30-\uDF39]|\uD806[\uDCA0-\uDCE9\uDCFF\uDE00-\uDE3E\uDE47\uDE50-\uDE83\uDE86-\uDE99\uDEC0-\uDEF8]|\uD807[\uDC00-\uDC08\uDC0A-\uDC36\uDC38-\uDC40\uDC50-\uDC59\uDC72-\uDC8F\uDC92-\uDCA7\uDCA9-\uDCB6\uDD00-\uDD06\uDD08\uDD09\uDD0B-\uDD36\uDD3A\uDD3C\uDD3D\uDD3F-\uDD47\uDD50-\uDD59]|\uD808[\uDC00-\uDF99]|\uD809[\uDC00-\uDC6E\uDC80-\uDD43]|[\uD80C\uD81C-\uD820\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD80D[\uDC00-\uDC2E]|\uD811[\uDC00-\uDE46]|\uD81A[\uDC00-\uDE38\uDE40-\uDE5E\uDE60-\uDE69\uDED0-\uDEED\uDEF0-\uDEF4\uDF00-\uDF36\uDF40-\uDF43\uDF50-\uDF59\uDF63-\uDF77\uDF7D-\uDF8F]|\uD81B[\uDF00-\uDF44\uDF50-\uDF7E\uDF8F-\uDF9F\uDFE0\uDFE1]|\uD821[\uDC00-\uDFEC]|\uD822[\uDC00-\uDEF2]|\uD82C[\uDC00-\uDD1E\uDD70-\uDEFB]|\uD82F[\uDC00-\uDC6A\uDC70-\uDC7C\uDC80-\uDC88\uDC90-\uDC99\uDC9D\uDC9E]|\uD834[\uDD65-\uDD69\uDD6D-\uDD72\uDD7B-\uDD82\uDD85-\uDD8B\uDDAA-\uDDAD\uDE42-\uDE44]|\uD835[\uDC00-\uDC54\uDC56-\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDEA5\uDEA8-\uDEC0\uDEC2-\uDEDA\uDEDC-\uDEFA\uDEFC-\uDF14\uDF16-\uDF34\uDF36-\uDF4E\uDF50-\uDF6E\uDF70-\uDF88\uDF8A-\uDFA8\uDFAA-\uDFC2\uDFC4-\uDFCB\uDFCE-\uDFFF]|\uD836[\uDE00-\uDE36\uDE3B-\uDE6C\uDE75\uDE84\uDE9B-\uDE9F\uDEA1-\uDEAF]|\uD838[\uDC00-\uDC06\uDC08-\uDC18\uDC1B-\uDC21\uDC23\uDC24\uDC26-\uDC2A]|\uD83A[\uDC00-\uDCC4\uDCD0-\uDCD6\uDD00-\uDD4A\uDD50-\uDD59]|\uD83B[\uDE00-\uDE03\uDE05-\uDE1F\uDE21\uDE22\uDE24\uDE27\uDE29-\uDE32\uDE34-\uDE37\uDE39\uDE3B\uDE42\uDE47\uDE49\uDE4B\uDE4D-\uDE4F\uDE51\uDE52\uDE54\uDE57\uDE59\uDE5B\uDE5D\uDE5F\uDE61\uDE62\uDE64\uDE67-\uDE6A\uDE6C-\uDE72\uDE74-\uDE77\uDE79-\uDE7C\uDE7E\uDE80-\uDE89\uDE8B-\uDE9B\uDEA1-\uDEA3\uDEA5-\uDEA9\uDEAB-\uDEBB]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0]|\uD87E[\uDC00-\uDE1D]|\uDB40[\uDD00-\uDDEF]/; var unicode = { Space_Separator: Space_Separator, ID_Start: ID_Start, ID_Continue: ID_Continue }; var util = { isSpaceSeparator (c) { return typeof c === 'string' && unicode.Space_Separator.test(c) }, isIdStartChar (c) { return typeof c === 'string' && ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c === '$') || (c === '_') || unicode.ID_Start.test(c) ) }, isIdContinueChar (c) { return typeof c === 'string' && ( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || (c === '$') || (c === '_') || (c === '\u200C') || (c === '\u200D') || unicode.ID_Continue.test(c) ) }, isDigit (c) { return typeof c === 'string' && /[0-9]/.test(c) }, isHexDigit (c) { return typeof c === 'string' && /[0-9A-Fa-f]/.test(c) }, }; let source; let parseState; let stack$1; let pos; let line; let column; let token; let key; let root; var parse = function parse (text, reviver) { source = String(text); parseState = 'start'; stack$1 = []; pos = 0; line = 1; column = 0; token = undefined; key = undefined; root = undefined; do { token = lex(); // This code is unreachable. // if (!parseStates[parseState]) { // throw invalidParseState() // } parseStates[parseState](); } while (token.type !== 'eof') if (typeof reviver === 'function') { return internalize({'': root}, '', reviver) } return root }; function internalize (holder, name, reviver) { const value = holder[name]; if (value != null && typeof value === 'object') { for (const key in value) { const replacement = internalize(value, key, reviver); if (replacement === undefined) { delete value[key]; } else { value[key] = replacement; } } } return reviver.call(holder, name, value) } let lexState; let buffer; let doubleQuote; let sign$1; let c$1; function lex () { lexState = 'default'; buffer = ''; doubleQuote = false; sign$1 = 1; for (;;) { c$1 = peek(); // This code is unreachable. // if (!lexStates[lexState]) { // throw invalidLexState(lexState) // } const token = lexStates[lexState](); if (token) { return token } } } function peek () { if (source[pos]) { return String.fromCodePoint(source.codePointAt(pos)) } } function read () { const c = peek(); if (c === '\n') { line++; column = 0; } else if (c) { column += c.length; } else { column++; } if (c) { pos += c.length; } return c } const lexStates = { default () { switch (c$1) { case '\t': case '\v': case '\f': case ' ': case '\u00A0': case '\uFEFF': case '\n': case '\r': case '\u2028': case '\u2029': read(); return case '/': read(); lexState = 'comment'; return case undefined: read(); return newToken('eof') } if (util.isSpaceSeparator(c$1)) { read(); return } // This code is unreachable. // if (!lexStates[parseState]) { // throw invalidLexState(parseState) // } return lexStates[parseState]() }, comment () { switch (c$1) { case '*': read(); lexState = 'multiLineComment'; return case '/': read(); lexState = 'singleLineComment'; return } throw invalidChar(read()) }, multiLineComment () { switch (c$1) { case '*': read(); lexState = 'multiLineCommentAsterisk'; return case undefined: throw invalidChar(read()) } read(); }, multiLineCommentAsterisk () { switch (c$1) { case '*': read(); return case '/': read(); lexState = 'default'; return case undefined: throw invalidChar(read()) } read(); lexState = 'multiLineComment'; }, singleLineComment () { switch (c$1) { case '\n': case '\r': case '\u2028': case '\u2029': read(); lexState = 'default'; return case undefined: read(); return newToken('eof') } read(); }, value () { switch (c$1) { case '{': case '[': return newToken('punctuator', read()) case 'n': read(); literal('ull'); return newToken('null', null) case 't': read(); literal('rue'); return newToken('boolean', true) case 'f': read(); literal('alse'); return newToken('boolean', false) case '-': case '+': if (read() === '-') { sign$1 = -1; } lexState = 'sign'; return case '.': buffer = read(); lexState = 'decimalPointLeading'; return case '0': buffer = read(); lexState = 'zero'; return case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer = read(); lexState = 'decimalInteger'; return case 'I': read(); literal('nfinity'); return newToken('numeric', Infinity) case 'N': read(); literal('aN'); return newToken('numeric', NaN) case '"': case "'": doubleQuote = (read() === '"'); buffer = ''; lexState = 'string'; return } throw invalidChar(read()) }, identifierNameStartEscape () { if (c$1 !== 'u') { throw invalidChar(read()) } read(); const u = unicodeEscape(); switch (u) { case '$': case '_': break default: if (!util.isIdStartChar(u)) { throw invalidIdentifier() } break } buffer += u; lexState = 'identifierName'; }, identifierName () { switch (c$1) { case '$': case '_': case '\u200C': case '\u200D': buffer += read(); return case '\\': read(); lexState = 'identifierNameEscape'; return } if (util.isIdContinueChar(c$1)) { buffer += read(); return } return newToken('identifier', buffer) }, identifierNameEscape () { if (c$1 !== 'u') { throw invalidChar(read()) } read(); const u = unicodeEscape(); switch (u) { case '$': case '_': case '\u200C': case '\u200D': break default: if (!util.isIdContinueChar(u)) { throw invalidIdentifier() } break } buffer += u; lexState = 'identifierName'; }, sign () { switch (c$1) { case '.': buffer = read(); lexState = 'decimalPointLeading'; return case '0': buffer = read(); lexState = 'zero'; return case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': buffer = read(); lexState = 'decimalInteger'; return case 'I': read(); literal('nfinity'); return newToken('numeric', sign$1 * Infinity) case 'N': read(); literal('aN'); return newToken('numeric', NaN) } throw invalidChar(read()) }, zero () { switch (c$1) { case '.': buffer += read(); lexState = 'decimalPoint'; return case 'e': case 'E': buffer += read(); lexState = 'decimalExponent'; return case 'x': case 'X': buffer += read(); lexState = 'hexadecimal'; return } return newToken('numeric', sign$1 * 0) }, decimalInteger () { switch (c$1) { case '.': buffer += read(); lexState = 'decimalPoint'; return case 'e': case 'E': buffer += read(); lexState = 'decimalExponent'; return } if (util.isDigit(c$1)) { buffer += read(); return } return newToken('numeric', sign$1 * Number(buffer)) }, decimalPointLeading () { if (util.isDigit(c$1)) { buffer += read(); lexState = 'decimalFraction'; return } throw invalidChar(read()) }, decimalPoint () { switch (c$1) { case 'e': case 'E': buffer += read(); lexState = 'decimalExponent'; return } if (util.isDigit(c$1)) { buffer += read(); lexState = 'decimalFraction'; return } return newToken('numeric', sign$1 * Number(buffer)) }, decimalFraction () { switch (c$1) { case 'e': case 'E': buffer += read(); lexState = 'decimalExponent'; return } if (util.isDigit(c$1)) { buffer += read(); return } return newToken('numeric', sign$1 * Number(buffer)) }, decimalExponent () { switch (c$1) { case '+': case '-': buffer += read(); lexState = 'decimalExponentSign'; return } if (util.isDigit(c$1)) { buffer += read(); lexState = 'decimalExponentInteger'; return } throw invalidChar(read()) }, decimalExponentSign () { if (util.isDigit(c$1)) { buffer += read(); lexState = 'decimalExponentInteger'; return } throw invalidChar(read()) }, decimalExponentInteger () { if (util.isDigit(c$1)) { buffer += read(); return } return newToken('numeric', sign$1 * Number(buffer)) }, hexadecimal () { if (util.isHexDigit(c$1)) { buffer += read(); lexState = 'hexadecimalInteger'; return } throw invalidChar(read()) }, hexadecimalInteger () { if (util.isHexDigit(c$1)) { buffer += read(); return } return newToken('numeric', sign$1 * Number(buffer)) }, string () { switch (c$1) { case '\\': read(); buffer += escape$1(); return case '"': if (doubleQuote) { read(); return newToken('string', buffer) } buffer += read(); return case "'": if (!doubleQuote) { read(); return newToken('string', buffer) } buffer += read(); return case '\n': case '\r': throw invalidChar(read()) case '\u2028': case '\u2029': separatorChar(c$1); break case undefined: throw invalidChar(read()) } buffer += read(); }, start () { switch (c$1) { case '{': case '[': return newToken('punctuator', read()) // This code is unreachable since the default lexState handles eof. // case undefined: // return newToken('eof') } lexState = 'value'; }, beforePropertyName () { switch (c$1) { case '$': case '_': buffer = read(); lexState = 'identifierName'; return case '\\': read(); lexState = 'identifierNameStartEscape'; return case '}': return newToken('punctuator', read()) case '"': case "'": doubleQuote = (read() === '"'); lexState = 'string'; return } if (util.isIdStartChar(c$1)) { buffer += read(); lexState = 'identifierName'; return } throw invalidChar(read()) }, afterPropertyName () { if (c$1 === ':') { return newToken('punctuator', read()) } throw invalidChar(read()) }, beforePropertyValue () { lexState = 'value'; }, afterPropertyValue () { switch (c$1) { case ',': case '}': return newToken('punctuator', read()) } throw invalidChar(read()) }, beforeArrayValue () { if (c$1 === ']') { return newToken('punctuator', read()) } lexState = 'value'; }, afterArrayValue () { switch (c$1) { case ',': case ']': return newToken('punctuator', read()) } throw invalidChar(read()) }, end () { // This code is unreachable since it's handled by the default lexState. // if (c === undefined) { // read() // return newToken('eof') // } throw invalidChar(read()) }, }; function newToken (type, value) { return { type, value, line, column, } } function literal (s) { for (const c of s) { const p = peek(); if (p !== c) { throw invalidChar(read()) } read(); } } function escape$1 () { const c = peek(); switch (c) { case 'b': read(); return '\b' case 'f': read(); return '\f' case 'n': read(); return '\n' case 'r': read(); return '\r' case 't': read(); return '\t' case 'v': read(); return '\v' case '0': read(); if (util.isDigit(peek())) { throw invalidChar(read()) } return '\0' case 'x': read(); return hexEscape() case 'u': read(); return unicodeEscape() case '\n': case '\u2028': case '\u2029': read(); return '' case '\r': read(); if (peek() === '\n') { read(); } return '' case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': throw invalidChar(read()) case undefined: throw invalidChar(read()) } return read() } function hexEscape () { let buffer = ''; let c = peek(); if (!util.isHexDigit(c)) { throw invalidChar(read()) } buffer += read(); c = peek(); if (!util.isHexDigit(c)) { throw invalidChar(read()) } buffer += read(); return String.fromCodePoint(parseInt(buffer, 16)) } function unicodeEscape () { let buffer = ''; let count = 4; while (count-- > 0) { const c = peek(); if (!util.isHexDigit(c)) { throw invalidChar(read()) } buffer += read(); } return String.fromCodePoint(parseInt(buffer, 16)) } const parseStates = { start () { if (token.type === 'eof') { throw invalidEOF() } push(); }, beforePropertyName () { switch (token.type) { case 'identifier': case 'string': key = token.value; parseState = 'afterPropertyName'; return case 'punctuator': // This code is unreachable since it's handled by the lexState. // if (token.value !== '}') { // throw invalidToken() // } pop(); return case 'eof': throw invalidEOF() } // This code is unreachable since it's handled by the lexState. // throw invalidToken() }, afterPropertyName () { // This code is unreachable since it's handled by the lexState. // if (token.type !== 'punctuator' || token.value !== ':') { // throw invalidToken() // } if (token.type === 'eof') { throw invalidEOF() } parseState = 'beforePropertyValue'; }, beforePropertyValue () { if (token.type === 'eof') { throw invalidEOF() } push(); }, beforeArrayValue () { if (token.type === 'eof') { throw invalidEOF() } if (token.type === 'punctuator' && token.value === ']') { pop(); return } push(); }, afterPropertyValue () { // This code is unreachable since it's handled by the lexState. // if (token.type !== 'punctuator') { // throw invalidToken() // } if (token.type === 'eof') { throw invalidEOF() } switch (token.value) { case ',': parseState = 'beforePropertyName'; return case '}': pop(); } // This code is unreachable since it's handled by the lexState. // throw invalidToken() }, afterArrayValue () { // This code is unreachable since it's handled by the lexState. // if (token.type !== 'punctuator') { // throw invalidToken() // } if (token.type === 'eof') { throw invalidEOF() } switch (token.value) { case ',': parseState = 'beforeArrayValue'; return case ']': pop(); } // This code is unreachable since it's handled by the lexState. // throw invalidToken() }, end () { // This code is unreachable since it's handled by the lexState. // if (token.type !== 'eof') { // throw invalidToken() // } }, }; function push () { let value; switch (token.type) { case 'punctuator': switch (token.value) { case '{': value = {}; break case '[': value = []; break } break case 'null': case 'boolean': case 'numeric': case 'string': value = token.value; break // This code is unreachable. // default: // throw invalidToken() } if (root === undefined) { root = value; } else { const parent = stack$1[stack$1.length - 1]; if (Array.isArray(parent)) { parent.push(value); } else { parent[key] = value; } } if (value !== null && typeof value === 'object') { stack$1.push(value); if (Array.isArray(value)) { parseState = 'beforeArrayValue'; } else { parseState = 'beforePropertyName'; } } else { const current = stack$1[stack$1.length - 1]; if (current == null) { parseState = 'end'; } else if (Array.isArray(current)) { parseState = 'afterArrayValue'; } else { parseState = 'afterPropertyValue'; } } } function pop () { stack$1.pop(); const current = stack$1[stack$1.length - 1]; if (current == null) { parseState = 'end'; } else if (Array.isArray(current)) { parseState = 'afterArrayValue'; } else { parseState = 'afterPropertyValue'; } } // This code is unreachable. // function invalidParseState () { // return new Error(`JSON5: invalid parse state '${parseState}'`) // } // This code is unreachable. // function invalidLexState (state) { // return new Error(`JSON5: invalid lex state '${state}'`) // } function invalidChar (c) { if (c === undefined) { return syntaxError(`JSON5: invalid end of input at ${line}:${column}`) } return syntaxError(`JSON5: invalid character '${formatChar(c)}' at ${line}:${column}`) } function invalidEOF () { return syntaxError(`JSON5: invalid end of input at ${line}:${column}`) } // This code is unreachable. // function invalidToken () { // if (token.type === 'eof') { // return syntaxError(`JSON5: invalid end of input at ${line}:${column}`) // } // const c = String.fromCodePoint(token.value.codePointAt(0)) // return syntaxError(`JSON5: invalid character '${formatChar(c)}' at ${line}:${column}`) // } function invalidIdentifier () { column -= 5; return syntaxError(`JSON5: invalid identifier character at ${line}:${column}`) } function separatorChar (c) { console.warn(`JSON5: '${formatChar(c)}' in strings is not valid ECMAScript; consider escaping`); } function formatChar (c) { const replacements = { "'": "\\'", '"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t', '\v': '\\v', '\0': '\\0', '\u2028': '\\u2028', '\u2029': '\\u2029', }; if (replacements[c]) { return replacements[c] } if (c < ' ') { const hexString = c.charCodeAt(0).toString(16); return '\\x' + ('00' + hexString).substring(hexString.length) } return c } function syntaxError (message) { const err = new SyntaxError(message); err.lineNumber = line; err.columnNumber = column; return err } var stringify = function stringify (value, replacer, space) { const stack = []; let indent = ''; let propertyList; let replacerFunc; let gap = ''; let quote; if ( replacer != null && typeof replacer === 'object' && !Array.isArray(replacer) ) { space = replacer.space; quote = replacer.quote; replacer = replacer.replacer; } if (typeof replacer === 'function') { replacerFunc = replacer; } else if (Array.isArray(replacer)) { propertyList = []; for (const v of replacer) { let item; if (typeof v === 'string') { item = v; } else if ( typeof v === 'number' || v instanceof String || v instanceof Number ) { item = String(v); } if (item !== undefined && propertyList.indexOf(item) < 0) { propertyList.push(item); } } } if (space instanceof Number) { space = Number(space); } else if (space instanceof String) { space = String(space); } if (typeof space === 'number') { if (space > 0) { space = Math.min(10, Math.floor(space)); gap = ' '.substr(0, space); } } else if (typeof space === 'string') { gap = space.substr(0, 10); } return serializeProperty('', {'': value}) function serializeProperty (key, holder) { let value = holder[key]; if (value != null) { if (typeof value.toJSON5 === 'function') { value = value.toJSON5(key); } else if (typeof value.toJSON === 'function') { value = value.toJSON(key); } } if (replacerFunc) { value = replacerFunc.call(holder, key, value); } if (value instanceof Number) { value = Number(value); } else if (value instanceof String) { value = String(value); } else if (value instanceof Boolean) { value = value.valueOf(); } switch (value) { case null: return 'null' case true: return 'true' case false: return 'false' } if (typeof value === 'string') { return quoteString(value, false) } if (typeof value === 'number') { return String(value) } if (typeof value === 'object') { return Array.isArray(value) ? serializeArray(value) : serializeObject(value) } return undefined } function quoteString (value) { const quotes = { "'": 0.1, '"': 0.2, }; const replacements = { "'": "\\'", '"': '\\"', '\\': '\\\\', '\b': '\\b', '\f': '\\f', '\n': '\\n', '\r': '\\r', '\t': '\\t', '\v': '\\v', '\0': '\\0', '\u2028': '\\u2028', '\u2029': '\\u2029', }; let product = ''; for (let i = 0; i < value.length; i++) { const c = value[i]; switch (c) { case "'": case '"': quotes[c]++; product += c; continue case '\0': if (util.isDigit(value[i + 1])) { product += '\\x00'; continue } } if (replacements[c]) { product += replacements[c]; continue } if (c < ' ') { let hexString = c.charCodeAt(0).toString(16); product += '\\x' + ('00' + hexString).substring(hexString.length); continue } product += c; } const quoteChar = quote || Object.keys(quotes).reduce((a, b) => (quotes[a] < quotes[b]) ? a : b); product = product.replace(new RegExp(quoteChar, 'g'), replacements[quoteChar]); return quoteChar + product + quoteChar } function serializeObject (value) { if (stack.indexOf(value) >= 0) { throw TypeError('Converting circular structure to JSON5') } stack.push(value); let stepback = indent; indent = indent + gap; let keys = propertyList || Object.keys(value); let partial = []; for (const key of keys) { const propertyString = serializeProperty(key, value); if (propertyString !== undefined) { let member = serializeKey(key) + ':'; if (gap !== '') { member += ' '; } member += propertyString; partial.push(member); } } let final; if (partial.length === 0) { final = '{}'; } else { let properties; if (gap === '') { properties = partial.join(','); final = '{' + properties + '}'; } else { let separator = ',\n' + indent; properties = partial.join(separator); final = '{\n' + indent + properties + ',\n' + stepback + '}'; } } stack.pop(); indent = stepback; return final } function serializeKey (key) { if (key.length === 0) { return quoteString(key, true) } const firstChar = String.fromCodePoint(key.codePointAt(0)); if (!util.isIdStartChar(firstChar)) { return quoteString(key, true) } for (let i = firstChar.length; i < key.length; i++) { if (!util.isIdContinueChar(String.fromCodePoint(key.codePointAt(i)))) { return quoteString(key, true) } } return key } function serializeArray (value) { if (stack.indexOf(value) >= 0) { throw TypeError('Converting circular structure to JSON5') } stack.push(value); let stepback = indent; indent = indent + gap; let partial = []; for (let i = 0; i < value.length; i++) { const propertyString = serializeProperty(String(i), value); partial.push((propertyString !== undefined) ? propertyString : 'null'); } let final; if (partial.length === 0) { final = '[]'; } else { if (gap === '') { let properties = partial.join(','); final = '[' + properties + ']'; } else { let separator = ',\n' + indent; let properties = partial.join(separator); final = '[\n' + indent + properties + ',\n' + stepback + ']'; } } stack.pop(); indent = stepback; return final } }; const JSON5$1 = { parse, stringify, }; var lib = JSON5$1; class Sidebar{ constructor(viewer){ this.viewer = viewer; this.measuringTool = viewer.measuringTool; this.profileTool = viewer.profileTool; this.volumeTool = viewer.volumeTool; this.dom = $("#sidebar_root"); } createToolIcon(icon, title, callback){ let element = $(` `); element.click(callback); return element; } init(){ if(Potree.settings.editType == 'merge'){ this.initMergeBar(); this.initToolbar(); this.initScene(); this.initNavigation(); }else { this.initAccordion(); this.initAppearance(); this.initToolbar(); this.initScene(); this.initNavigation(); this.initFilters(); //this.initClippingTool(); //因为修改了clipping,所以这项有bug不能使用 this.initSettings(); if(Potree.settings.editType != 'pano'){ this.initAlignment(); this.initClipModel(); this.initSiteModel(); this.initParitcle(); this.initClippingModel(); }else { this.initPanosEdit(); } } $('#potree_version_number').html(Potree.version.major + "." + Potree.version.minor + Potree.version.suffix); } initAlignment(){ let Alignment = viewer.modules.Alignment; var pannel = $('#alignment'); var buttons = pannel.find('[name="transform"] button'); var applyToPointcloud = (fun, value)=>{ return function(){ var selected = $('#alignment li[name="selectPointCloud"] input:checked' ); Array.from(selected).forEach(e=>{ var pointcloud = viewer.scene.pointclouds.find(p=>p.name == e.name); fun(pointcloud, value); }); } }; //逆时针是正数 buttons.eq(0).on('click', applyToPointcloud(Alignment.rotate, 10)); //viewer.scene.pointclouds[0].rotation.z += THREE.Math.degToRad(10) buttons.eq(1).on('click' ,applyToPointcloud(Alignment.rotate, 1)); buttons.eq(2).on('click', applyToPointcloud(Alignment.rotate, 0.1)); buttons.eq(3).on('click', applyToPointcloud(Alignment.rotate, -10)); buttons.eq(4).on('click',applyToPointcloud(Alignment.rotate, -1)); buttons.eq(5).on('click',applyToPointcloud(Alignment.rotate, -0.1)); buttons.eq(6).on('click', applyToPointcloud(Alignment.translate, new Vector3(-1,0,0))); buttons.eq(7).on('click', applyToPointcloud(Alignment.translate, new Vector3(1,0,0))); buttons.eq(8).on('click', applyToPointcloud(Alignment.translate, new Vector3(0,-1,0))); buttons.eq(9).on('click', applyToPointcloud(Alignment.translate, new Vector3(0,1,0))); buttons.eq(10).on('click', applyToPointcloud(Alignment.translate, new Vector3(0,0,-1))); buttons.eq(11).on('click', applyToPointcloud(Alignment.translate, new Vector3(0,0,1))); pannel.find('#startAlignment').on('click', ()=>{ Alignment.enter(); }); pannel.find('#exitAlignment').on('click', ()=>{ Alignment.save(); Alignment.leave(); }); pannel.find('#rotTool').on('click', ()=>{ Alignment.switchHandle('rotate'); }); pannel.find('#moveTool').on('click', ()=>{ Alignment.switchHandle('translate'); }); } initMergeBar(){//多元融合模块 var pannel = $('#mergeModel'); var buttons = pannel.find('button'); let MergeEditor = viewer.modules.MergeEditor; let loading = false; pannel.find('ul[name="model"] li button').on('click',(e)=>{ if(loading)return console.log('还在加载', loading) let $elem = $(e.target); let parent = $elem.parent(); let name = parent.attr('name'); if($elem.attr('name') == 'select'){ return Potree.selectModel(name) } if($elem.text() == '添加'){ let startTime = Date.now(); Potree.addModel(name,()=>{ loading = false; //$elem.text('删除') let now = Date.now(); console.log('加载完毕', name, '用时', (now-startTime)/1000, 's'); }); loading = name; }else { Potree.removeModel(name); $elem.text('添加'); } }); pannel.find('li button[name="splitScreen"]').on('click',(e)=>{ let $elem = $(e.target); if($elem.text() == '分屏'){ $elem.text('恢复'); MergeEditor.enterSplit(); }else { $elem.text('分屏'); MergeEditor.leaveSplit(); } }); let addingTag = false; pannel.find('li button[name="tag"]').on('click',(e)=>{ let $elem = $(e.target); viewer.tagTool.startInsertion(); }); } initClippingModel(){//实时裁剪 /* 总共两种box : 可见和不可见(都是并集) 当有可见box时,需要在任一可见box内才可见 当有不可见box时,不在所有不可见box内才可见 */ var clipping = viewer.modules.clipping; var pannel = $('#clipping'); var addBtn = pannel.find('[name="operation"] button[name="add"] '); var switchBtn = pannel.find('[name="operation"] button[name="switchView"] '); var enterBtn = pannel.find(' button[name="enter"] '); var exitBtn = pannel.find(' button[name="exit"] '); let list = pannel.find('[name="list"] ul '); enterBtn.on('click',()=>{ clipping.enter(); pannel.find('li[name=operation]').css('display','block'); pannel.find('li[name=list]').css('display','block'); }); exitBtn.on('click',()=>{ clipping.leave(); pannel.find('li[name=operation]').css('display','none'); pannel.find('li[name=list]').css('display','none'); }); switchBtn.on('click',()=>{ clipping.switchView(clipping.activeViewName == 'top' ? 'mainView' : 'top'); }); pannel.find('[name="operation"] button[name="translation"] ').on('click',()=>{ clipping.setTranMode('translation'); }); pannel.find('[name="operation"] button[name="rotation"] ').on('click',()=>{ clipping.setTranMode('rotation'); }); pannel.find('[name="operation"] button[name="scale"] ').on('click',()=>{ clipping.setTranMode('scale'); }); addBtn.on('click',()=>{ let volumeBox = this.volumeTool.startInsertion({clip: true, clipTask:Potree.ClipTask.SHOW_OUTSIDE}); let li = $("
  • "); list.append(li); li.find('button[name=changeTask]').on('click',(e)=>{ if(e.target.innerText == '不可见'){ volumeBox.clipTask = Potree.ClipTask.SHOW_INSIDE; e.target.innerText = '可见'; }else { volumeBox.clipTask = Potree.ClipTask.SHOW_OUTSIDE; e.target.innerText = '不可见'; } volumeBox.update(); }); li.find('button[name=chose]').on('click',(e)=>{ viewer.transformObject(volumeBox);//viewer.inputHandler.toggleSelection(volumeBox) }); li.find('button[name=delete]').on('click',(e)=>{ li.remove(); viewer.scene.removeVolume(volumeBox); }); }); pannel.find('button[name=save]').on('click',(e)=>{ let data = clipping.saveClipData(); }); } addAlignmentButton(pointcloud){ var pannel = $('#alignment li[name="selectPointCloud"]>div'); var option = $(` `); pannel.append(option); /* option.find("input").on('change',function(){ }) */ } initClipModel(){ let Clip = viewer.modules.Clip; var pannel = $('#clipModel'); var buttons = pannel.find('button'); buttons.eq(0).on('click', Clip.enter.bind(Clip)); buttons.eq(1).on('click', Clip.download.bind(Clip)); buttons.eq(2).on('click', Clip.leave.bind(Clip)); } initSiteModel(){ let SiteModel = viewer.modules.SiteModel; var pannel = $('#siteModel'); pannel.find('button[name="start"] ').on('click', SiteModel.enter.bind(SiteModel)); pannel.find('button[name="exit"] ').on('click', SiteModel.leave.bind(SiteModel)); pannel.find('button[name="building"] ').on('click', SiteModel.startInsertion.bind(SiteModel,'building')); pannel.find('button[name="floor"] ').on('click', ()=>{ SiteModel.addFloor(SiteModel.buildings[0], 'top'); } ); pannel.find('button[name="room"] ').on('click', ()=>{ SiteModel.startInsertion('room', SiteModel.buildings[0].buildChildren[0]); }); pannel.find('button[name="digHole"] ').on('click', ()=>{ SiteModel.selected && SiteModel.startInsertion('hole', SiteModel.selected); }); pannel.find('button[name="selectBuilding"] ').on('click', ()=>{ SiteModel.selectEntity(SiteModel.buildings[0] ); } ); pannel.find('button[name="selectFloor"] ').on('click', ()=>{ SiteModel.selectEntity(SiteModel.buildings[0].buildChildren[0]); } ); pannel.find('button[name="selectRoom"] ').on('click', ()=>{ SiteModel.selectEntity(SiteModel.buildings[0].buildChildren[0].buildChildren[0]); }); pannel.find('button[name="removeFirstBuilding"] ').on('click', ()=>{ SiteModel.removeEntity(SiteModel.buildings[0]); }); pannel.find('button[name="removeFirstFloor"] ').on('click', ()=>{ SiteModel.removeEntity(SiteModel.buildings[0].buildChildren[0]); }); pannel.find('button[name="removeFirstRoom"] ').on('click', ()=>{ SiteModel.removeEntity(SiteModel.buildings[0].buildChildren[0].buildChildren[0]); }); pannel.find('button[name="removeFirstHole"] ').on('click', ()=>{ SiteModel.selected.removeHole(SiteModel.selected.holes[0]); }); pannel.find('button[name="removeFirstMarker"] ').on('click', ()=>{ //SiteModel.removeMarker(SiteModel.selected.markers[0]) SiteModel.selected.removeMarker(0); }); } initParitcle(){ let ParticleEditor = viewer.modules.ParticleEditor; var pannel = $('#particle'); pannel.find('button[name="addFire"] ').on('click', ()=>{ ParticleEditor.startInsertion('fire+smoke'); }); pannel.find('button[name="addExplode"] ').on('click', ()=>{ ParticleEditor.startInsertion('explode'); }); } initPanosEdit(){ let PanoEditor = viewer.modules.PanoEditor; let Alignment = viewer.modules.Alignment; var pannel = $('#panos'); pannel.find('button[name="save"] ').on('click', ()=>{ console.log('saveData',PanoEditor.exportSavingData()); }); pannel.find('button[name="translate"] ').on('click', ()=>{ Alignment.switchHandle('translate'); }); pannel.find('button[name="rotate"] ').on('click', ()=>{ Alignment.switchHandle('rotate'); }); pannel.find('button[name="topView"] ').on('click', ()=>{ PanoEditor.switchView('top'); }); pannel.find('button[name="sideView"] ').on('click', ()=>{ PanoEditor.switchView('right'); }); pannel.find('button[name="3DView"] ').on('click', ()=>{ PanoEditor.switchView('mainView'); }); pannel.find('button[name="addLink"] ').on('click', ()=>{ PanoEditor.setLinkOperateState('addLink', true); }); pannel.find('button[name="removeLink"] ').on('click', ()=>{ PanoEditor.setLinkOperateState('removeLink', true); }); pannel.find('button[name="getCloser"] ').on('click', ()=>{ PanoEditor.setZoomInState(true); }); } initToolbar(){ // ANGLE let elToolbar = $('#tools'); elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/angle.png', '[title]tt.angle_measurement', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: false, showAngles: true, showArea: false, closed: true, maxMarkers: 3, minMarkers:3, measureType: 'Angle'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // POINT elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/point.svg', '[title]tt.point_measurement', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: false, showAngles: false, showCoordinates: true, showEdges:false, showArea: false, closed: true, maxMarkers: 1, minMarkers:1, measureType: 'Point'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // DISTANCE elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/distance.svg', '[title]tt.distance_measurement', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: false, closed: false, minMarkers:2, maxMarkers: 2, measureType: 'Distance' }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // DISTANCE2 elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/distance.svg', '[title]MulDistance', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: false, closed: false, minMarkers:2, measureType: 'MulDistance' }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // DISTANCE2 elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/distance.svg', '[title]MulDistance Ring', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ //showArea: false, closed: true, minMarkers:3, measureType: 'MulDistance Ring' }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/distance.svg', '[title]Ver MulDistance', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ measureType: 'Ver MulDistance' }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/distance.svg', '[title]Hor MulDistance', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ measureType: 'Hor MulDistance' }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // HEIGHT elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/height.svg', '[title]tt.height_measurement', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true,//false, showHeight: true, showArea: false, closed: false, maxMarkers: 2, minMarkers:2, //showGuideLine: true: true measureType: 'Ver Distance', }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // CIRCLE elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/circle.svg', '[title]tt.circle_measurement', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: false, showHeight: false, showArea: false, showCircle: true, showEdges: false, closed: false, maxMarkers: 3, minMarkers:3, measureType: 'Circle' }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // AZIMUTH elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/azimuth.svg', 'Azimuth', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: false, showHeight: false, showArea: false, showCircle: false, showEdges: false, showAzimuth: true, closed: false, maxMarkers: 2, minMarkers:2, measureType: 'Azimuth'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // AREA elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/area.svg', '[title]tt.area_measurement', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: true, closed: true, minMarkers:3, //showGuideLine: true: true, measureType: 'Area'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); //Hor AREA elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/area.svg', '[title]Hor Area', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: true, closed: true, minMarkers:3, measureType: 'Hor Area'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // Ver Area elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/area.svg', '[title]Ver Area', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: true, closed: true, minMarkers:3, measureType: 'Ver Area'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // rect area freedom direction elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/area.svg', '[title]area_freedom_rect', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: true, closed: true, minMarkers:4, maxMarkers:4, //showGuideLine: true: true, isRect:true, measureType: 'Rect Area'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // rect area horizontal elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/area.svg', '[title]area_horizontal_rect', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: true, closed: true, minMarkers:4, maxMarkers:4, //showGuideLine: true: true, isRect:true, faceDirection:"horizontal", measureType: 'Hor Rect Area'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // rect area vertical elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/area.svg', '[title]area_vertical_rect', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ showDistances: true, showArea: true, closed: true, minMarkers:4, maxMarkers:4, //showGuideLine: true: true, isRect:true, faceDirection:"vertical", measureType: 'Ver Rect Area'}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // AREA elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/area.svg', '[title]prism', () => { $('#menu_measurements').next().slideDown(); let measurement = this.measuringTool.startInsertion({ measureType: 'MulDistance Ring', unit : 'metric' }); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === measurement.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // VOLUME elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/volume.svg', '[title]tt.volume_measurement', () => { let volume = this.volumeTool.startInsertion(); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === volume.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // SPHERE VOLUME elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/sphere_distances.svg', '[title]tt.volume_measurement', () => { let volume = this.volumeTool.startInsertion({type: SphereVolume$2}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === volume.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // PROFILE elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/profile.svg', '[title]tt.height_profile', () => { $('#menu_measurements').next().slideDown(); ; let profile = this.profileTool.startInsertion(); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === profile.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // ANNOTATION elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/annotation.svg', '[title]tt.annotation', () => { $('#menu_measurements').next().slideDown(); ; let annotation = this.viewer.annotationTool.startInsertion(); let annotationsRoot = $("#jstree_scene").jstree().get_json("annotations"); let jsonNode = annotationsRoot.children.find(child => child.data.uuid === annotation.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // REMOVE ALL elToolbar.append(this.createToolIcon( Potree.resourcePath + '/icons/reset_tools.svg', '[title]tt.remove_all_measurement', () => { this.viewer.scene.removeAllMeasurements(); } )); { // SHOW / HIDE Measurements let elShow = $("#measurement_options_show"); elShow.selectgroup({title: "Show/Hide labels"}); elShow.find("input").click( (e) => { const show = e.target.value === "SHOW"; this.measuringTool.showLabels = show; }); let currentShow = this.measuringTool.showLabels ? "SHOW" : "HIDE"; elShow.find(`input[value=${currentShow}]`).trigger("click"); } } initScene(){ let elScene = $("#menu_scene"); let elObjects = elScene.next().find("#scene_objects"); let elProperties = elScene.next().find("#scene_object_properties"); { let elExport = elScene.next().find("#scene_export"); let geoJSONIcon = `${Potree.resourcePath}/icons/file_geojson.svg`; let dxfIcon = `${Potree.resourcePath}/icons/file_dxf.svg`; let potreeIcon = `${Potree.resourcePath}/icons/file_potree.svg`; elExport.append(` Export:
    `); let elDownloadJSON = elExport.find("img[name=geojson_export_button]").parent(); elDownloadJSON.click( (event) => { let scene = this.viewer.scene; let measurements = [...scene.measurements, ...scene.profiles, ...scene.volumes]; if(measurements.length > 0){ let geoJson = GeoJSONExporter.toString(measurements); let url = window.URL.createObjectURL(new Blob([geoJson], {type: 'data:application/octet-stream'})); elDownloadJSON.attr('href', url); }else { this.viewer.postError("nothing to export"); event.preventDefault(); } }); let elDownloadDXF = elExport.find("img[name=dxf_export_button]").parent(); elDownloadDXF.click( (event) => { let scene = this.viewer.scene; let measurements = [...scene.measurements, ...scene.profiles, ...scene.volumes]; if(measurements.length > 0){ let dxf = DXFExporter.toString(measurements); let url = window.URL.createObjectURL(new Blob([dxf], {type: 'data:application/octet-stream'})); elDownloadDXF.attr('href', url); }else { this.viewer.postError("no measurements to export"); event.preventDefault(); } }); let elDownloadPotree = elExport.find("img[name=potree_export_button]").parent(); elDownloadPotree.click( (event) => { let data = Potree.saveProject(this.viewer); let dataString = lib.stringify(data, null, "\t"); let url = window.URL.createObjectURL(new Blob([dataString], {type: 'data:application/octet-stream'})); elDownloadPotree.attr('href', url); }); } let propertiesPanel = new PropertiesPanel(elProperties, this.viewer); propertiesPanel.setScene(this.viewer.scene); localStorage.removeItem('jstree'); let tree = $(`
    `); elObjects.append(tree); tree.jstree({ 'plugins': ["checkbox", "state"], 'core': { "dblclick_toggle": false, "state": { "checked" : true }, 'check_callback': true, "expand_selected_onload": true }, "checkbox" : { "keep_selected_style": true, "three_state": false, "whole_node": false, "tie_selection": false, }, }); let createNode = (parent, text, icon, object) => { let nodeID = tree.jstree('create_node', parent, { "text": text, "icon": icon, "data": object }, "last", false, false); if(object.visible){ tree.jstree('check_node', nodeID); }else { tree.jstree('uncheck_node', nodeID); } return nodeID; }; let pcID = tree.jstree('create_node', "#", { "text": "Point Clouds", "id": "pointclouds"}, "last", false, false); let measurementID = tree.jstree('create_node', "#", { "text": "Measurements", "id": "measurements" }, "last", false, false); let annotationsID = tree.jstree('create_node', "#", { "text": "Annotations", "id": "annotations" }, "last", false, false); let otherID = tree.jstree('create_node', "#", { "text": "Other", "id": "other" }, "last", false, false); let vectorsID = tree.jstree('create_node', "#", { "text": "Vectors", "id": "vectors" }, "last", false, false); let imagesID = tree.jstree('create_node', "#", { "text": " Images", "id": "images" }, "last", false, false); tree.jstree("check_node", pcID); tree.jstree("check_node", measurementID); tree.jstree("check_node", annotationsID); tree.jstree("check_node", otherID); tree.jstree("check_node", vectorsID); tree.jstree("check_node", imagesID); tree.on('create_node.jstree', (e, data) => { tree.jstree("open_all"); }); tree.on("select_node.jstree", (e, data) => { let object = data.node.data; propertiesPanel.set(object); this.viewer.inputHandler.deselectAll(); if(object instanceof Volume$1){ this.viewer.inputHandler.toggleSelection(object); } $(this.viewer.renderer.domElement).focus(); }); tree.on("deselect_node.jstree", (e, data) => { propertiesPanel.set(null); }); tree.on("delete_node.jstree", (e, data) => { propertiesPanel.set(null); }); tree.on('dblclick','.jstree-anchor', (e) => { let instance = $.jstree.reference(e.target); let node = instance.get_node(e.target); let object = node.data; // ignore double click on checkbox if(e.target.classList.contains("jstree-checkbox")){ return; } if(object instanceof PointCloudTree){ let box = this.viewer.getBoundingBox([object]); let node = new Object3D(); node.boundingBox = box; this.viewer.zoomTo(node, 1, 500); }else if(object instanceof Measure$1){ let points = object.points.map(p => p.position); let box = new Box3().setFromPoints(points); if(box.getSize(new Vector3()).length() == 0){ box.min = box.max.clone();//禁止相同 box.expandByVector(new Vector3(1,1,1)); } let node = new Object3D(); node.boundingBox = box; this.viewer.zoomTo(node, 2, 500); }else if(object instanceof Profile){ let points = object.points; let box = new Box3().setFromPoints(points); if(box.getSize(new Vector3()).length() > 0){ let node = new Object3D(); node.boundingBox = box; this.viewer.zoomTo(node, 1, 500); } }else if(object instanceof Volume$1){ let box = object.boundingBox.clone().applyMatrix4(object.matrixWorld); if(box.getSize(new Vector3()).length() > 0){ let node = new Object3D(); node.boundingBox = box; this.viewer.zoomTo(node, 1, 500); } }else if(object instanceof Annotation){ object.moveHere(this.viewer.scene.getActiveCamera()); }else if(object instanceof PolygonClipVolume){ let dir = object.camera.getWorldDirection(new Vector3()); let target; if(object.camera instanceof OrthographicCamera){ dir.multiplyScalar(object.camera.right); target = new Vector3().addVectors(object.camera.position, dir); this.viewer.setCameraMode(CameraMode.ORTHOGRAPHIC); }else if(object.camera instanceof PerspectiveCamera){ dir.multiplyScalar(this.viewer.scene.view.radius); target = new Vector3().addVectors(object.camera.position, dir); this.viewer.setCameraMode(CameraMode.PERSPECTIVE); } this.viewer.scene.view.position.copy(object.camera.position); this.viewer.scene.view.lookAt(target); }else if(object.type === "SpotLight"){ let distance = (object.distance > 0) ? object.distance / 4 : 5 * 1000; let position = object.position; let target = new Vector3().addVectors( position, object.getWorldDirection(new Vector3()).multiplyScalar(distance)); this.viewer.scene.view.position.copy(object.position); this.viewer.scene.view.lookAt(target); }else if(object instanceof Object3D){ let box = new Box3().setFromObject(object); if(box.getSize(new Vector3()).length() > 0){ let node = new Object3D(); node.boundingBox = box; this.viewer.zoomTo(node, 1, 500); } }else if(object instanceof OrientedImage){ // TODO zoom to images // let box = new THREE.Box3().setFromObject(object); // if(box.getSize(new THREE.Vector3()).length() > 0){ // let node = new THREE.Object3D(); // node.boundingBox = box; // this.viewer.zoomTo(node, 1, 500); // } }else if(object instanceof Images360){ // TODO }else if(object instanceof Geopackage){ // TODO } }); tree.on("uncheck_node.jstree", (e, data) => { let object = data.node.data; if(object){ object.visible = false; } }); tree.on("check_node.jstree", (e, data) => { let object = data.node.data; if(object){ object.visible = true; } }); let onPointCloudAdded = (e) => { let pointcloud = e.pointcloud; let cloudIcon = `${Potree.resourcePath}/icons/cloud.svg`; let node = createNode(pcID, pointcloud.name, cloudIcon, pointcloud); pointcloud.addEventListener("visibility_changed", () => { if(pointcloud.visible){ tree.jstree('check_node', node); }else { tree.jstree('uncheck_node', node); } }); }; let onMeasurementAdded = (e) => { let measurement = e.measurement; let icon = Utils.getMeasurementIcon(measurement); createNode(measurementID, measurement.name, icon, measurement); }; let onVolumeAdded = (e) => { let volume = e.volume; let icon = Utils.getMeasurementIcon(volume); let node = createNode(measurementID, volume.name, icon, volume); volume.addEventListener("visibility_changed", () => { if(volume.visible){ tree.jstree('check_node', node); }else { tree.jstree('uncheck_node', node); } }); }; let onProfileAdded = (e) => { let profile = e.profile; let icon = Utils.getMeasurementIcon(profile); createNode(measurementID, profile.name, icon, profile); }; let onAnnotationAdded = (e) => { let annotation = e.annotation; let annotationIcon = `${Potree.resourcePath}/icons/annotation.svg`; let parentID = this.annotationMapping.get(annotation.parent); let annotationID = createNode(parentID, annotation.title, annotationIcon, annotation); this.annotationMapping.set(annotation, annotationID); annotation.addEventListener("annotation_changed", (e) => { let annotationsRoot = $("#jstree_scene").jstree().get_json("annotations"); let jsonNode = annotationsRoot.children.find(child => child.data.uuid === annotation.uuid); $.jstree.reference(jsonNode.id).rename_node(jsonNode.id, annotation.title); }); }; let onCameraAnimationAdded = (e) => { const animation = e.animation; const animationIcon = `${Potree.resourcePath}/icons/camera_animation.svg`; createNode(otherID, "animation", animationIcon, animation); }; let onOrientedImagesAdded = (e) => { const images = e.images; const imagesIcon = `${Potree.resourcePath}/icons/picture.svg`; const node = createNode(imagesID, "images", imagesIcon, images); images.addEventListener("visibility_changed", () => { if(images.visible){ tree.jstree('check_node', node); }else { tree.jstree('uncheck_node', node); } }); }; let onImages360Added = (e) => { const images = e.images; const imagesIcon = `${Potree.resourcePath}/icons/picture.svg`; const node = createNode(imagesID, "360° images", imagesIcon, images); images.addEventListener("visibility_changed", () => { if(images.visible){ tree.jstree('check_node', node); }else { tree.jstree('uncheck_node', node); } }); }; const onGeopackageAdded = (e) => { const geopackage = e.geopackage; const geopackageIcon = `${Potree.resourcePath}/icons/triangle.svg`; const tree = $(`#jstree_scene`); const parentNode = "vectors"; for(const layer of geopackage.node.children){ const name = layer.name; let shpPointsID = tree.jstree('create_node', parentNode, { "text": name, "icon": geopackageIcon, "object": layer, "data": layer, }, "last", false, false); tree.jstree(layer.visible ? "check_node" : "uncheck_node", shpPointsID); } }; this.viewer.scene.addEventListener("pointcloud_added", onPointCloudAdded); this.viewer.scene.addEventListener("measurement_added", onMeasurementAdded); this.viewer.scene.addEventListener("profile_added", onProfileAdded); this.viewer.scene.addEventListener("volume_added", onVolumeAdded); this.viewer.scene.addEventListener("camera_animation_added", onCameraAnimationAdded); this.viewer.scene.addEventListener("oriented_images_added", onOrientedImagesAdded); this.viewer.scene.addEventListener("360_images_added", onImages360Added); this.viewer.scene.addEventListener("geopackage_added", onGeopackageAdded); this.viewer.scene.addEventListener("polygon_clip_volume_added", onVolumeAdded); this.viewer.scene.annotations.addEventListener("annotation_added", onAnnotationAdded); let onMeasurementRemoved = (e) => { let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === e.measurement.uuid); tree.jstree("delete_node", jsonNode.id); }; let onVolumeRemoved = (e) => { let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === e.volume.uuid); tree.jstree("delete_node", jsonNode.id); }; let onPolygonClipVolumeRemoved = (e) => { let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === e.volume.uuid); tree.jstree("delete_node", jsonNode.id); }; let onProfileRemoved = (e) => { let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === e.profile.uuid); tree.jstree("delete_node", jsonNode.id); }; this.viewer.scene.addEventListener("measurement_removed", onMeasurementRemoved); this.viewer.scene.addEventListener("volume_removed", onVolumeRemoved); this.viewer.scene.addEventListener("polygon_clip_volume_removed", onPolygonClipVolumeRemoved); this.viewer.scene.addEventListener("profile_removed", onProfileRemoved); { let annotationIcon = `${Potree.resourcePath}/icons/annotation.svg`; this.annotationMapping = new Map(); this.annotationMapping.set(this.viewer.scene.annotations, annotationsID); this.viewer.scene.annotations.traverseDescendants(annotation => { let parentID = this.annotationMapping.get(annotation.parent); let annotationID = createNode(parentID, annotation.title, annotationIcon, annotation); this.annotationMapping.set(annotation, annotationID); }); } const scene = this.viewer.scene; for(let pointcloud of scene.pointclouds){ onPointCloudAdded({pointcloud: pointcloud}); } for(let measurement of scene.measurements){ onMeasurementAdded({measurement: measurement}); } for(let volume of [...scene.volumes, ...scene.polygonClipVolumes]){ onVolumeAdded({volume: volume}); } for(let animation of scene.cameraAnimations){ onCameraAnimationAdded({animation: animation}); } for(let images of scene.orientedImages){ onOrientedImagesAdded({images: images}); } for(let images of scene.images360){ onImages360Added({images: images}); } for(const geopackage of scene.geopackages){ onGeopackageAdded({geopackage: geopackage}); } for(let profile of scene.profiles){ onProfileAdded({profile: profile}); } { createNode(otherID, "Camera", null, new Camera()); } this.viewer.addEventListener("scene_changed", (e) => { propertiesPanel.setScene(e.scene); e.oldScene.removeEventListener("pointcloud_added", onPointCloudAdded); e.oldScene.removeEventListener("measurement_added", onMeasurementAdded); e.oldScene.removeEventListener("profile_added", onProfileAdded); e.oldScene.removeEventListener("volume_added", onVolumeAdded); e.oldScene.removeEventListener("polygon_clip_volume_added", onVolumeAdded); e.oldScene.removeEventListener("measurement_removed", onMeasurementRemoved); e.scene.addEventListener("pointcloud_added", onPointCloudAdded); e.scene.addEventListener("measurement_added", onMeasurementAdded); e.scene.addEventListener("profile_added", onProfileAdded); e.scene.addEventListener("volume_added", onVolumeAdded); e.scene.addEventListener("polygon_clip_volume_added", onVolumeAdded); e.scene.addEventListener("measurement_removed", onMeasurementRemoved); }); } initClippingTool(){ this.viewer.addEventListener("cliptask_changed", (event) => { console.log("TODO"); }); this.viewer.addEventListener("clipmethod_changed", (event) => { console.log("TODO"); }); { let elClipTask = $("#cliptask_options"); elClipTask.selectgroup({title: "Clip Task"}); elClipTask.find("input").click( (e) => { this.viewer.setClipTask(ClipTask[e.target.value]); }); let currentClipTask = Object.keys(ClipTask) .filter(key => ClipTask[key] === this.viewer.clipTask); elClipTask.find(`input[value=${currentClipTask}]`).trigger("click"); } { let elClipMethod = $("#clipmethod_options"); elClipMethod.selectgroup({title: "Clip Method"}); elClipMethod.find("input").click( (e) => { this.viewer.setClipMethod(ClipMethod[e.target.value]); }); let currentClipMethod = Object.keys(ClipMethod) .filter(key => ClipMethod[key] === this.viewer.clipMethod); elClipMethod.find(`input[value=${currentClipMethod}]`).trigger("click"); } let clippingToolBar = $("#clipping_tools"); // CLIP VOLUME clippingToolBar.append(this.createToolIcon( Potree.resourcePath + '/icons/clip_volume.svg', '[title]tt.clip_volume', () => { let item = this.volumeTool.startInsertion({clip: true}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === item.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); // CLIP POLYGON clippingToolBar.append(this.createToolIcon( Potree.resourcePath + "/icons/clip-polygon.svg", "[title]tt.clip_polygon", () => { let item = this.viewer.clippingTool.startInsertion({type: "polygon"}); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === item.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); {// SCREEN BOX SELECT let boxSelectTool = new ScreenBoxSelectTool(this.viewer); clippingToolBar.append(this.createToolIcon( Potree.resourcePath + "/icons/clip-screen.svg", "[title]tt.screen_clip_box", () => { if(!(this.viewer.scene.getActiveCamera() instanceof OrthographicCamera)){ this.viewer.postMessage(`Switch to Orthographic Camera Mode before using the Screen-Box-Select tool.`, {duration: 2000}); return; } let item = boxSelectTool.startInsertion(); let measurementsRoot = $("#jstree_scene").jstree().get_json("measurements"); let jsonNode = measurementsRoot.children.find(child => child.data.uuid === item.uuid); $.jstree.reference(jsonNode.id).deselect_all(); $.jstree.reference(jsonNode.id).select_node(jsonNode.id); } )); } { // REMOVE CLIPPING TOOLS clippingToolBar.append(this.createToolIcon( Potree.resourcePath + "/icons/remove.svg", "[title]tt.remove_all_clipping_volumes", () => { this.viewer.scene.removeAllClipVolumes(); } )); } } initFilters(){ this.initClassificationList(); this.initReturnFilters(); this.initGPSTimeFilters(); this.initPointSourceIDFilters(); } initReturnFilters(){ let elReturnFilterPanel = $('#return_filter_panel'); { // RETURN NUMBER let sldReturnNumber = elReturnFilterPanel.find('#sldReturnNumber'); let lblReturnNumber = elReturnFilterPanel.find('#lblReturnNumber'); sldReturnNumber.slider({ range: true, min: 0, max: 7, step: 1, values: [0, 7], slide: (event, ui) => { this.viewer.setFilterReturnNumberRange(ui.values[0], ui.values[1]); } }); let onReturnNumberChanged = (event) => { let [from, to] = this.viewer.filterReturnNumberRange; lblReturnNumber[0].innerHTML = `${from} to ${to}`; sldReturnNumber.slider({values: [from, to]}); }; this.viewer.addEventListener('filter_return_number_range_changed', onReturnNumberChanged); onReturnNumberChanged(); } { // NUMBER OF RETURNS let sldNumberOfReturns = elReturnFilterPanel.find('#sldNumberOfReturns'); let lblNumberOfReturns = elReturnFilterPanel.find('#lblNumberOfReturns'); sldNumberOfReturns.slider({ range: true, min: 0, max: 7, step: 1, values: [0, 7], slide: (event, ui) => { this.viewer.setFilterNumberOfReturnsRange(ui.values[0], ui.values[1]); } }); let onNumberOfReturnsChanged = (event) => { let [from, to] = this.viewer.filterNumberOfReturnsRange; lblNumberOfReturns[0].innerHTML = `${from} to ${to}`; sldNumberOfReturns.slider({values: [from, to]}); }; this.viewer.addEventListener('filter_number_of_returns_range_changed', onNumberOfReturnsChanged); onNumberOfReturnsChanged(); } } initGPSTimeFilters(){ let elGPSTimeFilterPanel = $('#gpstime_filter_panel'); { let slider = new HierarchicalSlider({ levels: 4, slide: (event) => { this.viewer.setFilterGPSTimeRange(...event.values); }, }); let initialized = false; let initialize = () => { let elRangeContainer = $("#gpstime_multilevel_range_container"); elRangeContainer[0].prepend(slider.element); let extent = this.viewer.getGpsTimeExtent(); slider.setRange(extent); slider.setValues(extent); initialized = true; }; this.viewer.addEventListener("update", (e) => { let extent = this.viewer.getGpsTimeExtent(); let gpsTimeAvailable = extent[0] !== Infinity; if(!initialized && gpsTimeAvailable){ initialize(); } slider.setRange(extent); //高耗cpu }); } { const txtGpsTime = elGPSTimeFilterPanel.find("#txtGpsTime"); const btnFindGpsTime = elGPSTimeFilterPanel.find("#btnFindGpsTime"); let targetTime = null; txtGpsTime.on("input", (e) => { const str = txtGpsTime.val(); if(!isNaN(str)){ const value = parseFloat(str); targetTime = value; txtGpsTime.css("background-color", ""); }else { targetTime = null; txtGpsTime.css("background-color", "#ff9999"); } }); btnFindGpsTime.click( () => { if(targetTime !== null){ viewer.moveToGpsTimeVicinity(targetTime); } }); } } initPointSourceIDFilters() { let elPointSourceIDFilterPanel = $('#pointsourceid_filter_panel'); { let slider = new HierarchicalSlider({ levels: 4, range: [0, 65535], precision: 1, slide: (event) => { let values = event.values; this.viewer.setFilterPointSourceIDRange(values[0], values[1]); } }); let initialized = false; let initialize = () => { elPointSourceIDFilterPanel[0].prepend(slider.element); initialized = true; }; this.viewer.addEventListener("update", (e) => { let extent = this.viewer.filterPointSourceIDRange; if(!initialized){ initialize(); slider.setValues(extent); } }); } // let lblPointSourceID = elPointSourceIDFilterPanel.find("#lblPointSourceID"); // let elPointSourceID = elPointSourceIDFilterPanel.find("#spnPointSourceID"); // let slider = new ZoomableSlider(); // elPointSourceID[0].appendChild(slider.element); // slider.update(); // slider.change( () => { // let range = slider.chosenRange; // this.viewer.setFilterPointSourceIDRange(range[0], range[1]); // }); // let onPointSourceIDExtentChanged = (event) => { // let range = this.viewer.filterPointSourceIDExtent; // slider.setVisibleRange(range); // }; // let onPointSourceIDChanged = (event) => { // let range = this.viewer.filterPointSourceIDRange; // let precision = 1; // let from = `${Utils.addCommas(range[0].toFixed(precision))}`; // let to = `${Utils.addCommas(range[1].toFixed(precision))}`; // lblPointSourceID[0].innerHTML = `${from} to ${to}`; // slider.setRange(range); // }; // this.viewer.addEventListener('filter_point_source_id_range_changed', onPointSourceIDChanged); // this.viewer.addEventListener('filter_point_source_id_extent_changed', onPointSourceIDExtentChanged); } initClassificationList(){ let elClassificationList = $('#classificationList'); let addClassificationItem = (code, name) => { const classification = this.viewer.classifications[code]; const inputID = 'chkClassification_' + code; const colorPickerID = 'colorPickerClassification_' + code; const checked = classification.visible ? "checked" : ""; let element = $(`
  • `); const elInput = element.find('input'); const elColorPicker = element.find(`#${colorPickerID}`); elInput.click(event => { this.viewer.setClassificationVisibility(code, event.target.checked); }); let defaultColor = classification.color.map(c => c * 255).join(", "); defaultColor = `rgb(${defaultColor})`; elColorPicker.spectrum({ // flat: true, color: defaultColor, showInput: true, preferredFormat: 'rgb', cancelText: '', chooseText: 'Apply', move: color => { let rgb = color.toRgb(); const c = [rgb.r / 255, rgb.g / 255, rgb.b / 255, 1]; classification.color = c; }, change: color => { let rgb = color.toRgb(); const c = [rgb.r / 255, rgb.g / 255, rgb.b / 255, 1]; classification.color = c; } }); elClassificationList.append(element); }; const addToggleAllButton = () => { // toggle all button const element = $(`
  • `); let elInput = element.find('input'); elInput.click(event => { this.viewer.toggleAllClassificationsVisibility(); }); elClassificationList.append(element); }; const addInvertButton = () => { const element = $(`
  • `); let elInput = element.find('input'); elInput.click( () => { const classifications = this.viewer.classifications; for(let key of Object.keys(classifications)){ let value = classifications[key]; this.viewer.setClassificationVisibility(key, !value.visible); } }); elClassificationList.append(element); }; const populate = () => { addToggleAllButton(); for (let classID in this.viewer.classifications) { addClassificationItem(classID, this.viewer.classifications[classID].name); } addInvertButton(); }; populate(); this.viewer.addEventListener("classifications_changed", () => { elClassificationList.empty(); populate(); }); this.viewer.addEventListener("classification_visibility_changed", () => { { // set checked state of classification buttons for(const classID of Object.keys(this.viewer.classifications)){ const classValue = this.viewer.classifications[classID]; let elItem = elClassificationList.find(`#chkClassification_${classID}`); elItem.prop("checked", classValue.visible); } } { // set checked state of toggle button based on state of all other buttons let numVisible = 0; let numItems = 0; for(const key of Object.keys(this.viewer.classifications)){ if(this.viewer.classifications[key].visible){ numVisible++; } numItems++; } const allVisible = numVisible === numItems; let elToggle = elClassificationList.find("#toggleClassificationFilters"); elToggle.prop("checked", allVisible); } }); } initAccordion(){ $('.accordion > h3').each(function(){ let header = $(this); let content = $(this).next(); //header.addClass('accordion-header ui-widget'); //content.addClass('accordion-content ui-widget'); content.hide(); header.click(() => { content.slideToggle(); }); }); let languages = [ ["EN", "en"], ["FR", "fr"], ["DE", "de"], ["JP", "jp"], ["ES", "es"], ["SE", "se"], ["ZH", "zh"] ]; let elLanguages = $('#potree_languages'); for(let i = 0; i < languages.length; i++){ let [key, value] = languages[i]; let element = $(`${key}`); element.click(() => this.viewer.setLanguage(value)); if(i === 0){ element.css("margin-left", "30px"); } elLanguages.append(element); if(i < languages.length - 1){ elLanguages.append($(document.createTextNode(' - '))); } } // to close all, call // $(".accordion > div").hide() // to open the, for example, tool menu, call: // $("#menu_tools").next().show() } initAppearance(){ const sldPointBudget = this.dom.find('#sldPointBudget'); sldPointBudget.slider({ value: this.viewer.getPointBudget(), min: 100 * 1000, max: 10 * 1000 * 1000, step: 1000, slide: (event, ui) => { this.viewer.setPointBudget(ui.value); } }); this.dom.find('#sldFOV').slider({ value: this.viewer.getFOV(), min: 20, max: 100, step: 1, slide: (event, ui) => { this.viewer.setFOV(ui.value); } }); $('#sldEDLRadius').slider({ value: this.viewer.getEDLRadius(), min: 1, max: 4, step: 0.01, slide: (event, ui) => { this.viewer.setEDLRadius(ui.value); } }); $('#sldEDLStrength').slider({ value: this.viewer.getEDLStrength(), min: 0, max: 5, step: 0.01, slide: (event, ui) => { this.viewer.setEDLStrength(ui.value); } }); $('#sldEDLOpacity').slider({ value: this.viewer.getEDLOpacity(), min: 0, max: 1, step: 0.01, slide: (event, ui) => { this.viewer.setEDLOpacity(ui.value); } }); this.viewer.addEventListener('point_budget_changed', (event) => { $('#lblPointBudget')[0].innerHTML = Utils.addCommas(this.viewer.getPointBudget()); sldPointBudget.slider({value: this.viewer.getPointBudget()}); }); this.viewer.addEventListener('fov_changed', (event) => { $('#lblFOV')[0].innerHTML = parseInt(this.viewer.getFOV()); $('#sldFOV').slider({value: this.viewer.getFOV()}); }); this.viewer.addEventListener('use_edl_changed', (event) => { $('#chkEDLEnabled')[0].checked = this.viewer.getEDLEnabled(); }); this.viewer.addEventListener('edl_radius_changed', (event) => { $('#lblEDLRadius')[0].innerHTML = this.viewer.getEDLRadius().toFixed(1); $('#sldEDLRadius').slider({value: this.viewer.getEDLRadius()}); }); this.viewer.addEventListener('edl_strength_changed', (event) => { $('#lblEDLStrength')[0].innerHTML = this.viewer.getEDLStrength().toFixed(1); $('#sldEDLStrength').slider({value: this.viewer.getEDLStrength()}); }); this.viewer.addEventListener('background_changed', (event) => { $("input[name=background][value='" + this.viewer.getBackground() + "']").prop('checked', true); }); $('#lblPointBudget')[0].innerHTML = Utils.addCommas(this.viewer.getPointBudget()); $('#lblFOV')[0].innerHTML = parseInt(this.viewer.getFOV()); $('#lblEDLRadius')[0].innerHTML = this.viewer.getEDLRadius().toFixed(1); $('#lblEDLStrength')[0].innerHTML = this.viewer.getEDLStrength().toFixed(1); $('#chkEDLEnabled')[0].checked = this.viewer.getEDLEnabled(); { let elBackground = $(`#background_options`); elBackground.selectgroup(); elBackground.find("input").click( (e) => { this.viewer.setBackground(e.target.value); }); let currentBackground = this.viewer.getBackground(); try{ $(`input[name=background_options][value=${currentBackground}]`).trigger("click"); }catch(e){} } $('#chkEDLEnabled').click( () => { this.viewer.setEDLEnabled($('#chkEDLEnabled').prop("checked")); }); } initNavigation(){ let elNavigation = $('#navigation'); let sldMoveSpeed = $('#sldMoveSpeed'); let lblMoveSpeed = $('#lblMoveSpeed'); elNavigation.append(this.createToolIcon( Potree.resourcePath + '/icons/earth_controls_1.png', '[title]tt.earth_control', () => { this.viewer.setControls(this.viewer.earthControls); } )); elNavigation.append(this.createToolIcon( Potree.resourcePath + '/icons/fps_controls.svg', '[title]tt.flight_control', () => { this.viewer.setControls(this.viewer.fpControls); this.viewer.fpControls.lockElevation = false; } )); elNavigation.append(this.createToolIcon( Potree.resourcePath + '/icons/helicopter_controls.svg', '[title]tt.heli_control', () => { this.viewer.setControls(this.viewer.fpControls); this.viewer.fpControls.lockElevation = true; } )); elNavigation.append(this.createToolIcon( Potree.resourcePath + '/icons/orbit_controls.svg', '[title]tt.orbit_control', () => { this.viewer.setControls(this.viewer.orbitControls); } )); elNavigation.append(this.createToolIcon( Potree.resourcePath + '/icons/focus.svg', '[title]tt.focus_control', () => { this.viewer.fitToScreen(); } )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/navigation_cube.svg", "[title]tt.navigation_cube_control", () => {this.viewer.toggleNavigationCube();} )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/images/compas.svg", "[title]tt.compass", () => { const visible = !this.viewer.compass.isVisible(); this.viewer.compass.setVisible(visible); } )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/camera_animation.svg", "[title]tt.camera_animation", () => { const animation = CameraAnimation$1.defaultFromView(this.viewer); viewer.scene.addCameraAnimation(animation); } )); elNavigation.append("
    "); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/left.svg", "[title]tt.left_view_control", () => {this.viewer.setLeftView();} )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/right.svg", "[title]tt.right_view_control", () => {this.viewer.setRightView();} )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/front.svg", "[title]tt.front_view_control", () => {this.viewer.setFrontView();} )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/back.svg", "[title]tt.back_view_control", () => {this.viewer.setBackView();} )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/top.svg", "[title]tt.top_view_control", () => {this.viewer.setTopView();} )); elNavigation.append(this.createToolIcon( Potree.resourcePath + "/icons/bottom.svg", "[title]tt.bottom_view_control", () => {this.viewer.setBottomView();} )); let elCameraProjection = $(` `); elNavigation.append(elCameraProjection); elCameraProjection.selectgroup({title: "Camera Projection"}); elCameraProjection.find("input").click( (e) => { this.viewer.setCameraMode(CameraMode[e.target.value]); }); let cameraMode = Object.keys(CameraMode) .filter(key => CameraMode[key] === this.viewer.scene.cameraMode); elCameraProjection.find(`input[value=${cameraMode}]`).trigger("click"); let speedRange = new Vector2(1, 10 * 1000); let toLinearSpeed = (value) => { return Math.pow(value, 4) * speedRange.y + speedRange.x; }; let toExpSpeed = (value) => { return Math.pow((value - speedRange.x) / speedRange.y, 1 / 4); }; sldMoveSpeed.slider({ value: toExpSpeed(this.viewer.getMoveSpeed()), min: 0, max: 1, step: 0.01, slide: (event, ui) => { this.viewer.setMoveSpeed(toLinearSpeed(ui.value)); } }); this.viewer.addEventListener('move_speed_changed', (event) => { lblMoveSpeed.html(this.viewer.getMoveSpeed().toFixed(1)); sldMoveSpeed.slider({value: toExpSpeed(this.viewer.getMoveSpeed())}); }); lblMoveSpeed.html(this.viewer.getMoveSpeed().toFixed(1)); } initSettings(){ { $('#sldMinNodeSize').slider({ value: this.viewer.getMinNodeSize(), min: 0, max: 1000, step: 0.01, slide: (event, ui) => { this.viewer.setMinNodeSize(ui.value); } }); this.viewer.addEventListener('minnodesize_changed', (event) => { $('#lblMinNodeSize').html(parseInt(this.viewer.getMinNodeSize())); $('#sldMinNodeSize').slider({value: this.viewer.getMinNodeSize()}); }); $('#lblMinNodeSize').html(parseInt(this.viewer.getMinNodeSize())); } { let elSplatQuality = $("#splat_quality_options"); elSplatQuality.selectgroup({title: "Splat Quality"}); elSplatQuality.find("input").click( (e) => { if(e.target.value === "standard"){ this.viewer.useHQ = false; }else if(e.target.value === "hq"){ this.viewer.useHQ = true; } }); let currentQuality = this.viewer.useHQ ? "hq" : "standard"; elSplatQuality.find(`input[value=${currentQuality}]`).trigger("click"); } $('#show_bounding_box').click(() => { this.viewer.setShowBoundingBox($('#show_bounding_box').prop("checked")); }); $('#set_freeze').click(() => { this.viewer.setFreeze($('#set_freeze').prop("checked")); }); } } class AnnotationTool extends EventDispatcher$1{ constructor (viewer) { super(); this.viewer = viewer; this.renderer = viewer.renderer; this.sg = new SphereGeometry(0.1); this.sm = new MeshNormalMaterial(); this.s = new Mesh(this.sg, this.sm); } startInsertion (args = {}) { let domElement = this.viewer.renderer.domElement; let annotation = new Annotation({ position: [589748.270, 231444.540, 753.675], title: "Annotation Title", description: `Annotation Description` }); this.dispatchEvent({type: 'start_inserting_annotation', annotation: annotation}); const annotations = this.viewer.scene.annotations; annotations.add(annotation); let callbacks = { cancel: null, finish: null, }; let insertionCallback = (e) => { if (e.button === MOUSE.LEFT) { callbacks.finish(); } else if (e.button === MOUSE.RIGHT) { callbacks.cancel(); } }; callbacks.cancel = e => { annotations.remove(annotation); domElement.removeEventListener('mouseup', insertionCallback, true); }; callbacks.finish = e => { domElement.removeEventListener('mouseup', insertionCallback, true); }; domElement.addEventListener('mouseup', insertionCallback, true); let drag = (e) => { let I = Utils.getMousePointCloudIntersection( e.drag.end, e.viewer.scene.getActiveCamera(), e.viewer, e.viewer.scene.pointclouds, {pickClipped: true}); if (I) { this.s.position.copy(I.location); annotation.position.copy(I.location); } }; let drop = (e) => { viewer.scene.scene.remove(this.s); this.s.removeEventListener("drag", drag); this.s.removeEventListener("drop", drop); }; this.s.addEventListener('drag', drag); this.s.addEventListener('drop', drop); this.viewer.scene.scene.add(this.s); this.viewer.inputHandler.startDragging(this.s); return annotation; } update(){ // let camera = this.viewer.scene.getActiveCamera(); // let domElement = this.renderer.domElement; // let measurements = this.viewer.scene.measurements; // const renderAreaSize = this.renderer.getSize(new THREE.Vector2()); // let clientWidth = renderAreaSize.width; // let clientHeight = renderAreaSize.height; } render(){ //this.viewer.renderer.render(this.scene, this.viewer.scene.getActiveCamera()); } }; //处理cursor优先级 var CursorDeal = { priorityEvent : [//在前面的优先级高 {'zoomInCloud':'zoom-in'}, {'hoverPano':'pointer'}, {'connectPano':`url({Potree.resourcePath}/images/connect.png),auto`}, {'disconnectPano':`url({Potree.resourcePath}/images/connect-dis.png),auto`}, {'hoverLine':'pointer'}, {'hoverTranHandle':'grab'}, {"movePointcloud":'move'}, {"polygon_isIntersectSelf":'not-allowed'}, {"polygon_AtWrongPlace":'not-allowed'}, {'delPoint':'url("https://4dkk.4dage.com/v4-test/www/sdk/images/polygon_mark/pic_pen_sub.png"), auto'}, {"markerMove":'grab'}, {'addPoint':'url("https://4dkk.4dage.com/v4-test/www/sdk/images/polygon_mark/pic_pen_add.png"), auto'}, {'mapClipMove':'move'}, {'mapClipRotate':`url({Potree.resourcePath}/images/rotate-cursor.png),auto`}, {'rotatePointcloud':`url({Potree.resourcePath}/images/rotate-cursor.png),auto`}, {'siteModelFloorDrag':'row-resize'}, {'addSth':'cell'},//or crosshair ], list:[], //当前存在的cursor状态 currentCursorIndex:null, init : function(viewer, viewers){ this.priorityEvent.forEach(e=>{//刚开始Potree.resourcePath没值,现在换 for(let i in e){ e[i] = Common.replaceAll(e[i],'{Potree.resourcePath}',Potree.resourcePath); } }); this.domElements = viewers.map(e=>e.renderArea); viewer.addEventListener("CursorChange",(e)=>{ if(e.action == 'add'){ this.add(e.name); }else { this.remove(e.name); } }); }, add : function(name){ var priorityItem = this.priorityEvent.find(e=>e[name]); if(!priorityItem){ console.error('CursorDeal 未定义优先级 name:'+ name); return } if(!this.list.includes(name)){ this.judge({addItem: priorityItem, name}); this.list.push(name); } }, remove : function(name){ var index = this.list.indexOf(name); if(index > -1){ this.list.splice(index, 1); this.judge(); } }, judge:function(o={}){ //console.log(o,this.list) if(o.addItem){ var addIndex = this.priorityEvent.indexOf(o.addItem); if(addIndex < this.currentCursorIndex || this.currentCursorIndex == void 0){ this.domElements.forEach(e=>e.style.cursor = o.addItem[o.name] ); this.currentCursorIndex = addIndex; } }else { var levelMax = {index:Infinity, cursor:null }; this.list.forEach(name=>{ var priorityItem = this.priorityEvent.find(e=>e[name]); var index = this.priorityEvent.indexOf(priorityItem); if(index < levelMax.index){ levelMax.index = index; levelMax.cursor = priorityItem[name]; } }); this.currentCursorIndex = levelMax.index; this.domElements.forEach(e=>e.style.cursor = levelMax.cursor || ''); } } }; class SplitScreen extends EventDispatcher{ constructor (args = {}) { super(); } /* viewport.targetPlane // bound中心点处的plane,方向和view一致 viewport.shiftTarget // camera的位置project在targetPlane上的位置 这两个参数的主要目的是为了getPosOutOfModel,以及rotateSideCamera时保持相对位置 */ splitStart(cameraProps){ this.splited = true; let viewports = []; let subViewports = [viewer.mainViewport]; if(viewer.mapViewer){ subViewports.push(viewer.mapViewer.viewports[0]); } let length = cameraProps.length; for(let i=0;ie.name == (prop.name2||prop.name)); if(v){ viewport = v; viewport.left = prop.left; viewport.bottom = prop.bottom; viewport.width = prop.width; viewport.height = prop.height; } if(!viewport){ let view = new ExtendView(); if(prop.limitBound)view.limitBound = prop.limitBound; prop.direction && (view.direction = prop.direction); viewport = new Viewport(view , this.getOrthoCamera(), prop ); if(prop.viewContainsPoints)viewport.viewContainsPoints = prop.viewContainsPoints; //viewport.unableDepth = true //depthBasicMaterial等在此viewport中不开启depth } if(viewport.camera.type == 'OrthographicCamera' ){ viewport.targetPlane = new Plane(); viewport.shiftTarget = new Vector3; //project在targetPlane上的位置 } viewport.fitMargin = prop.margin; viewports.push(viewport); } viewer.viewports = viewports; viewer.updateScreenSize({forceUpdateSize:true}); viewports.forEach(viewport=>{ if(viewport.name == 'MainView')return this.viewportFitBound(viewport, viewer.bound.boundingBox , viewer.bound.center , 0, viewport.fitMargin); }); return viewports } unSplit(){ this.splited = false; this.unfocusViewport(); viewer.inputHandler.hoverViewport = null; //清空 viewer.viewports = [viewer.mainViewport]; viewer.mainViewport.width = 1; viewer.mainViewport.height = 1; viewer.mainViewport.left = 0; viewer.mainViewport.bottom = 0; viewer.updateScreenSize({forceUpdateSize:true}); } viewportFitBound(viewport, bound, center, duration=0, margin){ let view = viewport.view; let info = {bound}; let {boundSize, boundCenter} = this.getViewBound(viewport, bound ); //this.setShiftTarget(viewport, boundCenter) viewport.targetPlane.setFromNormalAndCoplanarPoint( view.direction.clone(), boundCenter ); viewport.targetPlane.projectPoint(center, viewport.shiftTarget); //target转换到过模型中心的平面,以保证镜头一定在模型外 this.shiftTarget是得到的 info.endPosition = this.getPosOutOfModel(viewport, boundSize); //if(viewport.name == 'mapViewport')info.endPosition.z = Math.max(Potree.config.map.cameraHeight, info.endPosition.z) info.margin = margin || {x:30, y:30}; view.moveOrthoCamera(viewport, info , duration ); } getViewBound(viewport, boundingBox){ if(boundingBox){ boundSize = boundingBox.getSize(new Vector3); center = boundingBox.getCenter(new Vector3); }else { var {boundSize, center, boundingBox} = viewer.bound; } let containsPoints = []; this.focusCenter && containsPoints.push(this.focusCenter); viewport.viewContainsPoints && containsPoints.push(...viewport.viewContainsPoints); if(containsPoints.length){//视野范围内必须要包含的点,直接算入模型区域。这时候得到的boundCenter和模型中心不重合 boundingBox = boundingBox.clone(); containsPoints.forEach(point=>{ boundingBox.expandByPoint(point); }); boundSize = boundingBox.getSize(new Vector3); center = boundingBox.getCenter(new Vector3); } return {boundSize, boundCenter:center } } getPosOutOfModel(viewport, boundSize){ //let {boundSize, center} = viewer.bound boundSize = boundSize || this.getViewBound(viewport).boundSize; let expand = 10; let radius = boundSize.length() * 2; let position = viewport.shiftTarget.clone().sub(viewport.view.direction.clone().multiplyScalar(radius + expand)); return position } updateCameraOutOfModel(){//因为移动模型导致模型超出相机外,所以更新位置 viewer.viewports.forEach((viewport, i )=>{ if(viewport.camera.isOrthographicCamera){ //or if(viewport.targetPlane) let {boundSize, boundCenter} = this.getViewBound(viewport); /* viewport.targetPlane.setFromNormalAndCoplanarPoint( viewport.view.direction.clone(), boundCenter) viewport.targetPlane.projectPoint(viewport.view.position, viewport.shiftTarget) //target转换到过模型中心的平面,以保证镜头一定在模型外 this.shiftTarget是得到的 */ this.setShiftTarget(viewport, boundCenter); let endPosition = this.getPosOutOfModel(viewport, boundSize); //if(viewport.name == 'mapViewport')endPosition.z = Math.max(Potree.config.map.cameraHeight, endPosition.z) viewport.view.position.copy(endPosition); } }); } setShiftTarget(viewport, center){ if(!viewport.targetPlane ){ viewport.targetPlane = new Plane(); viewport.shiftTarget = new Vector3; //project在targetPlane上的位置 } viewport.targetPlane.setFromNormalAndCoplanarPoint(viewport.view.direction, center ); viewport.targetPlane.projectPoint(viewport.view.position, viewport.shiftTarget ); //target转换到过模型中心的平面,以保证镜头一定在模型外 } rotateSideCamera(viewport, angle){//侧视图或俯视图绕模型中心水平旋转 //let {boundSize, center} = viewer.bound let {boundSize, boundCenter } = this.getViewBound(viewport); let center = this.focusCenter || boundCenter; //旋转中心,一般是所有模型的中心,除非想指定中心点 this.setShiftTarget(viewport, center); //找到平移向量 let vec = new Vector3().subVectors(center, viewport.shiftTarget);//相对于中心的偏移值,旋转后偏移值也旋转 //旋转 var rotMatrix = new Matrix4().makeRotationAxis(new Vector3(0,0,1), angle); viewport.view.direction = viewport.view.direction.applyMatrix4(rotMatrix); vec.applyMatrix4(rotMatrix); viewport.shiftTarget.subVectors(center,vec); //新的 viewport.view.position = this.getPosOutOfModel(viewport, boundSize); } getOrthoCamera(){ let camera = new OrthographicCamera(-100, 100, 100, 100, 0.01, 10000); camera.up.set(0,0,1); return camera } focusOnViewport(name){//全屏 viewer.viewports.forEach((viewport, i )=>{ if(viewport.name == name){ this.focusInfo = { name, left : viewport.left, bottom : viewport.bottom, height : viewport.height, width : viewport.width }; viewport.left = 0; viewport.bottom = 0; viewport.height = 1; viewport.width = 1; }else { viewport.active = false; } }); viewer.updateScreenSize({forceUpdateSize:true}); } unfocusViewport(){ if(!this.focusInfo)return viewer.viewports.forEach((viewport, i )=>{ if(this.focusInfo.name == viewport.name){//全屏的恢复 viewport.left = this.focusInfo.left; viewport.bottom = this.focusInfo.bottom; viewport.height = this.focusInfo.height; viewport.width = this.focusInfo.width; } viewport.active = true; }); viewer.updateScreenSize({forceUpdateSize:true}); this.focusInfo = null; } } const FEET_TO_INCHES_FACTOR = 12; const EIGHTHS_SYMBOLS = ["", "⅛", "¼", "⅜", "½", "⅝", "¾", "⅞"];//eighths 八分之…… class UnitOfMeasurement{//转化单位工具 constructor(t, e, n, i){ this.name = t, this.symbol = e, this.base = n, this.factor = i; } toBase(t) {//换算到base return t * this.factor } fromBase(t) {//换算到当前 return t / this.factor } } /* var o = function t(e) { this.gettext = e, t.METRIC = this.gettext("metric", void 0, "measurement system"), t.IMPERIAL = this.gettext("imperial", void 0, "measurement system") }; e.UoMSystem = o; let UoMSystem = { } */ /* var MeasurementDomain = { DISTANCE : "DISTANCE", t.AREA = "AREA", t.VOLUME = "VOLUME", t.DATA = "DATA", t } */ var UnitsOfMeasurement = { MILLIMETER : ["Millimeter", "mm"], CENTIMETER : ["Centimeter", "cm"], METER : ["Meter", "m"], KILOMETER : ["Kilometer", "km"], INCH : ["Inch", "in"], FOOT : ["Foot", "ft"], MILE : ["Mile", "mi"], SQUAREMETER : ["SquareMeter", "m²"], SQUAREFOOT : ["SquareFoot", "ft²"], CUBICMETER : ["CubicMeter", "m³"], CUBICFOOT : ["CubicFoot", "ft³"], BYTE : ["Byte", "B"], KILOBYTE : ["Kilobyte", "kB"], MEGABYTE : ["Megabyte", "MB"], GIGABYTE : ["Gigabyte", "GB"], TERABYTE : ["Terabyte", "TB"], PETABYTE : ["Petabyte", "PB"], init : function() { var e, n, i, a, s, c, l, u, d, p, h, f = new UnitOfMeasurement(UnitsOfMeasurement.METER[0],UnitsOfMeasurement.METER[1],void 0,1), g = new UnitOfMeasurement(UnitsOfMeasurement.SQUAREMETER[0],UnitsOfMeasurement.SQUAREMETER[1],void 0,1), m = new UnitOfMeasurement(UnitsOfMeasurement.CUBICMETER[0],UnitsOfMeasurement.CUBICMETER[1],void 0,1), v = new UnitOfMeasurement(UnitsOfMeasurement.BYTE[0],UnitsOfMeasurement.BYTE[1],void 0,1); UnitsOfMeasurement.DISTANCE = ( (e = {})['metric'] = ((n = {})[UnitsOfMeasurement.MILLIMETER[0]] = new UnitOfMeasurement(UnitsOfMeasurement.MILLIMETER[0],UnitsOfMeasurement.MILLIMETER[1],f,.001), n[UnitsOfMeasurement.CENTIMETER[0]] = new UnitOfMeasurement(UnitsOfMeasurement.CENTIMETER[0],UnitsOfMeasurement.CENTIMETER[1],f,.01), n[UnitsOfMeasurement.METER[0]] = f, n[UnitsOfMeasurement.KILOMETER[0]] = new UnitOfMeasurement(UnitsOfMeasurement.KILOMETER[0],UnitsOfMeasurement.KILOMETER[1],f,1e3), n), e['imperial'] = ((i = {})[UnitsOfMeasurement.INCH[0]] = new UnitOfMeasurement(UnitsOfMeasurement.INCH[0],UnitsOfMeasurement.INCH[1],f,.0254), i[UnitsOfMeasurement.FOOT[0]] = new UnitOfMeasurement(UnitsOfMeasurement.FOOT[0],UnitsOfMeasurement.FOOT[1],f,.3048), i[UnitsOfMeasurement.MILE[0]] = new UnitOfMeasurement(UnitsOfMeasurement.MILE[0],UnitsOfMeasurement.MILE[1],f,1609.344), i), e); UnitsOfMeasurement.AREA = ((a = {})['metric'] = ((s = {})[UnitsOfMeasurement.SQUAREMETER[0]] = g, s), a['imperial'] = ((c = {})[UnitsOfMeasurement.SQUAREFOOT[0]] = new UnitOfMeasurement(UnitsOfMeasurement.SQUAREFOOT[0],UnitsOfMeasurement.SQUAREFOOT[1],g,.092903), c), a); UnitsOfMeasurement.VOLUME = ((l = {})['metric'] = ((u = {})[UnitsOfMeasurement.CUBICMETER[0]] = m, u), l['imperial'] = ((d = {})[UnitsOfMeasurement.CUBICFOOT[0]] = new UnitOfMeasurement(UnitsOfMeasurement.CUBICFOOT[0],UnitsOfMeasurement.CUBICFOOT[1],m,.0283168), d), l); //数据大小 var y = ((p = {})[UnitsOfMeasurement.BYTE[0]] = v, p[UnitsOfMeasurement.KILOBYTE[0]] = new UnitOfMeasurement(UnitsOfMeasurement.KILOBYTE[0],UnitsOfMeasurement.KILOBYTE[1],v,1e3), p[UnitsOfMeasurement.MEGABYTE[0]] = new UnitOfMeasurement(UnitsOfMeasurement.MEGABYTE[0],UnitsOfMeasurement.MEGABYTE[1],v,1e6), p[UnitsOfMeasurement.GIGABYTE[0]] = new UnitOfMeasurement(UnitsOfMeasurement.GIGABYTE[0],UnitsOfMeasurement.GIGABYTE[1],v,1e9), p[UnitsOfMeasurement.TERABYTE[0]] = new UnitOfMeasurement(UnitsOfMeasurement.TERABYTE[0],UnitsOfMeasurement.TERABYTE[1],v,1e12), p[UnitsOfMeasurement.PETABYTE[0]] = new UnitOfMeasurement(UnitsOfMeasurement.PETABYTE[0],UnitsOfMeasurement.PETABYTE[1],v,1e15), p); UnitsOfMeasurement.DATA = ((h = {})['metric'] = y, h['imperial'] = y, h); } , getUnitsOfMeasurementByDomain : function(e) { return this[e.toUpperCase()] /* switch (e.toUpperCase()) { case a.DISTANCE: return t.DISTANCE; case a.AREA: return t.AREA; case a.VOLUME: return t.VOLUME; case a.DATA: return t.DATA; default: console.error(e + " measurement domain is not supported.") } */ } , getUnitsOfMeasurementByDomainAndSystem : function(domain, system) { var r = this.getUnitsOfMeasurementByDomain(domain); if (r.hasOwnProperty(system.toLowerCase())) return r[system.toLowerCase()]; console.error(n + " measurement system is not supported."); } , getDefaultUnitByDomainAndSystem : function(e, n) { switch (e.toUpperCase()) { case 'DISTANCE': switch (n.toLowerCase()) { case 'metric': return this.DISTANCE['metric'][this.METER[0]]; case 'imperial': return this.DISTANCE['imperial'][this.FOOT[0]]; default: console.error(n + " measurement system is not supported."); } case 'AREA': switch (n.toLowerCase()) { case 'metric': return this.AREA['metric'][this.SQUAREMETER[0]]; case 'imperial': return this.AREA['imperial'][this.SQUAREFOOT[0]]; default: console.error(n + " measurement system is not supported."); } case 'VOLUME': switch (n.toLowerCase()) { case 'metric': return this.VOLUME['metric'][this.CUBICMETER[0]]; case 'imperial': return this.VOLUME['imperial'][this.CUBICFOOT[0]]; default: console.error(n + " measurement system is not supported."); } case 'DATA': switch (n.toLowerCase()) { case 'metric': return this.DATA['metric'][this.BYTE[0]]; case 'imperial': return this.DATA['imperial'][this.BYTE[0]]; default: console.error(n + " measurement system is not supported."); } default: console.error(e + " measurement domain is not supported."); } } }; class UnitService{ constructor(/* e, n, i */){ //this.LanguageService = e, //this.localStorageService = n, //this.gettext = i, //this.unitChanged = new r.Signal, this.LOCAL_STORAGE_KEY = "iv_unit_key";//? UnitsOfMeasurement.init(); this.unitSystems = ['metric', 'imperial'];//[o.UoMSystem.METRIC, o.UoMSystem.IMPERIAL], this.defaultSystem = 'metric';//'imperial' //var a = this.LanguageService.getBrowserLocaleString().toLowerCase(); //this.defaultSystem = t.isLocaleImperial(a) ? o.UoMSystem.IMPERIAL : o.UoMSystem.METRIC, //this.initUnit() } /* initUnit() { var t = this.localStorageService.get(this.LOCAL_STORAGE_KEY); if (t) for (var e = 0, n = this.unitSystems; e < n.length; e++) { var i = n[e]; if (i === t) return void this.setUnit(i, !0) } this.setUnit(this.defaultSystem, !1) } setUnit(t, e) { this.currentSystem !== t && (this.currentSystem = t, this.unitChanged.emit()), e && this.localStorageService.set(this.LOCAL_STORAGE_KEY, t) } */ /*isLocaleImperial(e) { return t.IMPERIAL_LOCALES.indexOf(e.toLowerCase()) >= 0 } , t.IMPERIAL_LOCALES = ["en_us"], t.ɵfac(e) { return new (e || t)(c.ɵɵinject(l.LanguageService), c.ɵɵinject("localStorageService"),c.ɵɵinject("gettext")) } , t.ɵprov = c.ɵɵdefineInjectable({ token: t, factory: t.ɵfac, providedIn: "root" }), */ } class UoMService{ constructor(/* UnitService */){ this.UnitService = new UnitService();/* UnitService */ } scopedConvert (t, n, precision = 2, r, minFactor) { return this.convert(t, n, precision, r, minFactor) } convert(number, domain, precision = 2, system, ifEighths = !1, o /* minFactor, ifRestrict */) { let {minFactor, restrictUnit} = o[system]; //if (!number) return ""; var s = this.getMostRelevantMeasurement(domain, system || this.UnitService.currentSystem, number, minFactor, restrictUnit); return this.getFormattedMeasurementString(s[0], s[1], precision, ifEighths) } convertBack(number, domain, precision = 2, fromSystem, minFactor ) { //从英制转到'metric' if (!number) return ""; var d = UnitsOfMeasurement.getDefaultUnitByDomainAndSystem(domain,'metric'); var s = this.getMostRelevantMeasurement2(domain, fromSystem, number, minFactor); return this.getFormattedMeasurementString(s[0], d, precision ) /* 栗子: viewer.unitConvert.convertBack(1, 'area', 5, 'imperial') '0.09290 m²' viewer.unitConvert.convertBack(1, 'Distance', 2, 'imperial') '0.03 m' */ } getFormattedMeasurementString(number, unit, precision, ifEighths) { var result; if(ifEighths && unit.name === UnitsOfMeasurement.FOOT[0]){ result = this.formatImperialDistance(number * FEET_TO_INCHES_FACTOR); }else if(ifEighths && unit.name === UnitsOfMeasurement.INCH[0]){ result = this.formatImperialDistance(number); }else { result = number.toLocaleString(void 0, { minimumFractionDigits: precision, maximumFractionDigits: precision }) + " " + unit.symbol; } return result } formatImperialDistance(e) { var n = Math.round(8 * e) , i = Math.floor(n / 8) , r = Math.floor(i / FEET_TO_INCHES_FACTOR) , o = i - r * FEET_TO_INCHES_FACTOR , a = EIGHTHS_SYMBOLS[Math.abs(n % 8)] , s = 0 === o && "" !== a ? "" : o; "" !== s && "" !== a && (a = " " + a); return 0 !== r ? r + "' " + s + a + '"' : "" + s + a + '"' } getMostRelevantMeasurement(domain, system, number, minFactor=0, restrictUnit) { /* var a = r.values(UnitsOfMeasurement.getUnitsOfMeasurementByDomainAndSystem(domain, system)) , s = r.filter(a, function(t) { return t.factor >= i }) , c = r.reduce(s, function(t, e) { return e.fromBase(number) < t.fromBase(number) && e.fromBase(number) >= 1 ? e : t }); */ let a = []; let u = UnitsOfMeasurement.getUnitsOfMeasurementByDomainAndSystem(domain, system); for(let i in u){a.push(u[i]);} let s = a; if(s.length > 1){ if(restrictUnit){//是否只用这一单位,如只用mm。如果不是,就会寻找最大的接近的单位,如设置最小为mmminFactor=0.001),则1.2米时是'1.2m'、0.2米时是'20cm'、0.002米时是'2mm' s = a.filter(m=>m.name == restrictUnit); }else { s = a.filter(m=>m.factor >= minFactor); } } let c = s.reduce(function(prev, currentValue) {//reduce最终值是最后一次return的值 ( 没看懂这句话作用) return currentValue.fromBase(number) < prev.fromBase(number) && currentValue.fromBase(number) >= 1 ? currentValue : prev }); return c ? [c.fromBase(number), c] : void 0 } getMostRelevantMeasurement2(domain, system, number, minFactor=0) {//add let a = []; let u = UnitsOfMeasurement.getUnitsOfMeasurementByDomainAndSystem(domain, system); for(let i in u){a.push(u[i]);} let s = a.filter(m=>m.factor >= minFactor); let c = s.reduce(function(prev, currentValue) {//reduce最终值是最后一次return的值 ( 没看懂这句话作用) return currentValue.toBase(number) < prev.toBase(number) && currentValue.toBase(number) >= 1 ? currentValue : prev }); return c ? [c.toBase(number), c] : void 0 } /* ɵfac(e){ return new (e || t)(c.ɵɵinject(l.UnitService)) } ɵprov = c.ɵɵdefineInjectable({ token: t, factory: t.ɵfac, providedIn: "root" }) */ } let texLoader$8 = new TextureLoader(); let color$1 = new Color(config$1.clip.color); let markerMats$1; let markerSizeInfo$1 = {width2d: 40}; const pickOrders = { marker:1, area: 0, }; class mapClipBox extends ctrlPolygon { constructor (center, scale) { center = center.clone().setZ(0);//所有Z都为0 let prop = { points : getPoints(center, scale, 0), closed : true, atPlane: true, isRect : true, dimension : '2d' }; super('mapClipBox', prop); this.angle = 0; this.createRotateBar(); this.edgeMarkers = []; //addMarkers: this.initData(prop); {//areaPlane event 能拖动 this.areaPlane.addEventListener('mouseover',()=>{ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"mapClipMove" }); }); this.areaPlane.addEventListener('mouseleave',()=>{ viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"mapClipMove" }); }); let lastPos; let drag = (e)=>{ let intersect = e.intersect.orthoIntersect; if(lastPos){ let moveVec = new Vector3().subVectors(intersect, lastPos).setZ(0); this.center.add(moveVec); this.updatePoints(); this.dispatchEvent({type:'repos'}); } lastPos = intersect.clone(); }; let drop = (e)=>{ lastPos = null; }; this.areaPlane.addEventListener('drag', drag); this.areaPlane.addEventListener('drop', drop); } /* this.addEventListener('dragChange',()=>{ this.updateTwoMidMarker(index+1) }) */ Potree.Utils.setObjectLayers(this, 'mapObjects' ); } getScale(){ return new Vector3(this.points[0].distanceTo(this.points[1]), this.points[1].distanceTo(this.points[2]) ,1) } addMarker(o={} ){ let marker = new Sprite$2({mat:this.getMarkerMaterial('default'), pickOrder:pickOrders.marker, sizeInfo: markerSizeInfo$1, dontFixOrient: true, viewports:viewer.mapViewer.viewports, name:"mapClipBox_marker", } ); marker.renderOrder = 3; //marker.markerSelectStates = {} let edge = LineDraw.createLine([new Vector3,new Vector3],{color: color$1 }); let edgeMarker = new Sprite$2({mat:this.getMarkerMaterial('default'), pickOrder:pickOrders.marker, sizeInfo: markerSizeInfo$1, dontFixOrient: true, viewports:viewer.mapViewer.viewports, name:"mapClipBox_edgePoint"} ); let mouseover = (e) => { this.setMarkerSelected(e.object, true, 'single'); viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"markerMove" }); }; let mouseleave = (e) => { this.setMarkerSelected(e.object, false, 'single'); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"markerMove" }); }; edgeMarker.addEventListener('mouseover', mouseover); edgeMarker.addEventListener('mouseleave', mouseleave); let dragInfo = {lastPos:null}; edgeMarker.addEventListener('drag', this.dragEdge.bind(this,dragInfo)); edgeMarker.addEventListener('drop', this.dropEdge.bind(this,dragInfo)); this.edgeMarkers.push(edgeMarker); this.add(edgeMarker); marker.dispatchEvent('addHoverEvent'); super.addMarker({point:o.point, marker:marker, edge}); } dragEdge(dragInfo, e){//拖拽一个边(或一个边类型的marker),带动它的两个端点。 可以转化为拖拽marker往法线方向 var I, atMap; atMap = e.dragViewport.name == 'mapViewport'; I = e.intersect.orthoIntersect; if (I && dragInfo.lastPos) { let i = this.edgeMarkers.indexOf(e.drag.object); if (i !== -1) { let lineNormal = math.getNormal2d({p1:this.points[i], p2:this.points[(i+1)%4]}); let moveVec = new Vector3().subVectors(I, dragInfo.lastPos).setZ(0);//移动的向量 let dragVec = moveVec.projectOnVector(lineNormal).setZ(0);//在法线上移动的向量 let newPos = new Vector3().addVectors(this.points[i], dragVec); //marker应到的位置 this.dragChange(newPos, i, atMap); //转化为这个marker的拖拽 } } dragInfo.lastPos = I.clone(); } dropEdge(dragInfo, e){ dragInfo.lastPos = null; this.setMarkerSelected(e.drag.object, false, 'single'); //拖拽时似乎没有触发mouseout } createAreaPlane(){ var planeMat = new MeshBasicMaterial({ color: color$1,//"#00eeff", side:DoubleSide, opacity:0.3, transparent:true, depthTest:false }); return super.createAreaPlane(planeMat) } getMarkerMaterial(type) { if(!markerMats$1){ markerMats$1 = { default: new MeshBasicMaterial({ transparent: !0, color: color$1, opacity: 0.8, map: texLoader$8.load(Potree.resourcePath+'/textures/whiteCircle.png' ), }), select: new MeshBasicMaterial({ transparent: !0, color: color$1, opacity: 1, map: texLoader$8.load(Potree.resourcePath+'/textures/whiteCircle.png' ), }), }; mapClipBox.markerMats = markerMats$1; } return markerMats$1[type] } setMarkerSelected(marker, state, hoverObject){ //console.warn(marker.id , state, hoverObject) if(state == 'hover'){ marker.material = this.getMarkerMaterial('select'); }else { marker.material = this.getMarkerMaterial('default'); } /* marker.markerSelectStates[hoverObject] = state let absoluteState = false for(var i in marker.markerSelectStates){ if(marker.markerSelectStates[i]){ absoluteState = true; break; } } if(absoluteState){ marker.material = this.getMarkerMaterial('select') }else{ marker.material = this.getMarkerMaterial('default') } marker.selected = absoluteState */ viewer.mapViewer.dispatchEvent('content_changed'); } createRotateBar(){ //中心点在线的一端,整体初始在box上方 const lineLen = 1.5, circleWidth = 2, barOpacity = 0.7; let object = new Object3D; let bar = new Sprite$2({mat:new MeshBasicMaterial({ side:DoubleSide, opacity: barOpacity, transparent:true, depthTest:false, map: texLoader$8.load(Potree.resourcePath+'/textures/rotation_circle.png' ), }) , root:object, sizeInfo: markerSizeInfo$1, dontFixOrient: true, viewports:viewer.mapViewer.viewports, name:"mapClipRotateBar" }); bar.position.set(0,lineLen+circleWidth/2,0); bar.scale.set(circleWidth,circleWidth,circleWidth); bar.addEventListener('mouseover',()=>{ bar.material.opacity = 1; viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"mapClipRotate" }); viewer.mapViewer.dispatchEvent('content_changed'); }); let leave = ()=>{ bar.material.opacity = barOpacity; viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"mapClipRotate" }); viewer.mapViewer.dispatchEvent('content_changed'); }; bar.addEventListener('mouseleave',leave); this.addEventListener('dispose', leave); let lastPos; bar.addEventListener('drag',(e)=>{ var intersect = e.intersect.orthoIntersect; if(lastPos){ let vec1 = new Vector3().subVectors(lastPos, this.center).setZ(0); let vec2 = new Vector3().subVectors(intersect, this.center).setZ(0); let angle = math.getAngle(vec1,vec2,'z'); this.angle += angle; this.rotateBar.rotation.z = this.angle; this.updatePoints(); this.dispatchEvent({type:'rotate', angle: this.angle}); } lastPos = intersect.clone(); }); bar.addEventListener('drop',()=>{ lastPos = null; viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"mapClipRotate" }); }); let line = LineDraw.createLine([new Vector3, new Vector3(0,lineLen,0)],{color: color$1}); object.add(bar); object.add(line); this.add(object); this.rotateBar = object; this.rotateBar.bar = bar; } updatePoints(scale){ this.points = getPoints(this.center, scale || this.getScale(), this.angle); this.getPoint2dInfo(this.points); this.update({ifUpdateMarkers:true}); } update(options={}){ super.update(options); {//update rotateBar let center = new Vector3().addVectors(this.points[0], this.points[1]).multiplyScalar(0.5); this.rotateBar.position.copy(center); this.rotateBar.bar.waitUpdate();//更新sprite matrix } { for(let i=0;i<4;i++){ let current = this.points[i]; let next = this.points[(i+1)%4]; let mid = new Vector3().addVectors(current, next).multiplyScalar(0.5); this.updateMarker(this.edgeMarkers[i], mid); } } } dispose(){ super.dispose(); this.dispatchEvent('dispose'); this.rotateBar.bar.dispose(); } } function getPoints(center, scale, angle=0){ let points = [ new Vector3(-scale.x/2, +scale.y/2, 0), new Vector3(+scale.x/2, +scale.y/2, 0), new Vector3(+scale.x/2, -scale.y/2, 0), new Vector3(-scale.x/2, -scale.y/2, 0), ]; var rotMatrix = new Matrix4().makeRotationAxis(new Vector3(0,0,1), angle);//再旋转 points.forEach(e=>{ e.applyMatrix4(rotMatrix); e.add(center); }); return points } const defaultBoxWidth = 16; //navvis: 10 //navvis position: si {x: 0, y: 0, z: 0} const cameraProps = [ { name : 'top', axis:["x","y"], direction : new Vector3(0,0,-1), //镜头朝向 openCount:0, }, { name : 'front', axis:["x","z"], direction : new Vector3(0,1,0), openCount:0, }, { name : 'mainView', openCount:0, } ]; var Clip = { bus : new EventDispatcher, selectedDatasets : [], changeCallback(force){ if(this.activeViewName != 'mainView'){ this.splitScreenTool.updateCameraOutOfModel(); //更新相机位置 } if(Potree.settings.isOfficial && this.showMap){ let fun = ()=>{ let pointclouds = this.getIntersectPointcloud(); if( Common.getDifferenceSet(pointclouds,this.selectedDatasets).length){ this.selectedDatasets = pointclouds; //console.error('clipSelectedDatasets',selectedDatasets) this.bus.dispatchEvent({type:'updateSelectedDatasets', selectedDatasets:pointclouds.map(e=>e.dataset_id) }); } }; if(force)fun(); else Common.intervalTool.isWaiting('clipSelectedDatasets', fun , 300); } }, enter:function(){ this.initViews(); this.previousView = { position: viewer.images360.position, target: viewer.scene.view.getPivot(), displayMode : Potree.settings.displayMode, //--- ifShowMarker : Potree.settings.ifShowMarker, }; let pointcloud = this.getPointcloud(); let bound = pointcloud.bound; //只选取其中一个数据集的bound,而非整体,是因为担心两个数据集中间有空隙,于是刚好落在没有点云的地方。 let boundSize = bound.getSize(new Vector3()); let target = this.getTarget(bound.getCenter(new Vector3())); //navvis的位置xy是用相机位置 this.ViewService.mainView.getCamera().position 我觉得也可以用第一个漫游点的,或者最接近bound中心的漫游点 let scale = new Vector3(defaultBoxWidth,defaultBoxWidth, boundSize.z);//z和navvis一样 let eyeDir = viewer.scene.view.direction.clone().setZ(0/* -boundSize.z/3 */).multiplyScalar(-defaultBoxWidth); //为了使所在楼层不变,不修改z //let eyeDir = scale.clone().setZ(boundSize.z/3).multiplyScalar(1.3) let position = new Vector3().addVectors(target, eyeDir); Potree.settings.displayMode = 'showPointCloud'; viewer.setView({ position , target, duration:300, callback:function(){ } }); //viewer.setControls(viewer.orbitControls); viewer.setLimitFar(false); //viewer.setClipState(false) //暂时关闭旧的clipping /* new AxisViewer(viewer.mainViewport, viewer.renderArea,{domStyle:{ bottom: '2px',right: '20px', width:'80px',height:'80px'} }) */ { this.box = new BoxVolume$1({ clip:true }); this.box.clipTask = ClipTask['SHOW_INSIDE_Big' /* "SHOW_INSIDE" */]; this.box.showBox = false; this.box.name = "ClipBox"; this.box.position.copy(target); this.box.scale.copy(scale); viewer.controls.setTarget(this.box.position);//绕其旋转 this.splitScreenTool.focusCenter = this.box.position; //带动mapBox this.box.addEventListener('position_changed',e=>{ if(this.showMap){ this.mapBox.center.setX(this.box.position.x); this.mapBox.center.setY(this.box.position.y); this.mapBox.updatePoints(); } this.changeCallback(); }); this.box.addEventListener('scale_changed',e=>{ if(this.showMap){ var scale = this.box.scale; this.mapBox.updatePoints(scale); } this.changeCallback(); }); this.box.addEventListener('orientation_changed',e=>{ if(this.showMap){ this.mapBox.angle = this.box.rotation.z; this.mapBox.rotateBar.rotation.z = this.mapBox.angle; this.mapBox.updatePoints(); } this.changeCallback(); }); viewer.scene.addVolume(this.box); } if(this.showMap){//map let boxRotateBack = ()=>{//不知道是不是这么写。 因为可能z的旋转不一定都在z this.box.rotation.x = 0; this.box.rotation.y = 0; }; this.mapBox = new mapClipBox(target, scale); viewer.mapViewer.scene.add(this.mapBox); //带动box this.mapBox.addEventListener('repos',e=>{ this.box.position.setX(this.mapBox.center.x); this.box.position.setY(this.mapBox.center.y); boxRotateBack(); this.changeCallback(); }); this.mapBox.addEventListener('dragChange',e=>{ var scale = this.mapBox.getScale(); this.box.scale.setX(scale.x); this.box.scale.setY(scale.y); this.box.position.setX(this.mapBox.center.x); this.box.position.setY(this.mapBox.center.y); boxRotateBack(); this.changeCallback(); }); this.mapBox.addEventListener('rotate',e=>{ this.box.rotation.z = this.mapBox.angle; boxRotateBack(); this.changeCallback(); }); } this.switchView('mainView'); { //viewer.setClipTask(ClipTask["SHOW_INSIDE"]) } Potree.settings.unableNavigate = true; Potree.settings.ifShowMarker = false; Potree.Utils.updateVisible(viewer.measuringTool.scene, 'clipModel', false); //Potree.Utils.updateVisible(viewer.mapViewer.cursor, 'clipModel', false)//隐藏地图游标 viewer.inputHandler.toggleSelection(this.box); viewer.inputHandler.fixSelection = true; viewer.transformationTool.setModeEnable(['scale', 'translation', 'rotation'] ); viewer.transformationTool.frame.material.color.set(Potree.config.clip.color);//navvis 15899953 viewer.setPointStandardMat(true); if(this.showMap){ let mapVisi = false; this.events = { flyToPos : (e)=>{ let dis = 2; let target = e.position; target = this.box.position; position = e.position; //为了方便缩放操作,直接使用box中心作为target let duration = e.duration == void 0 ? 1000 : e.duration; viewer.scene.view.setView({position, duration, target}); }, mapVisiChange(e){ mapVisi = e.visible; let delay = 100; //因resize了camera需要时间更新projectionMatrix setTimeout(()=>{ let boundingBox = Clip.box.boundingBox.clone().applyMatrix4(Clip.box.matrixWorld); if(mapVisi){//切换地图 if(viewer.fpVisiDatasets.length == 0){//不会显示任何一个floorplan图,就要就近移动到一个可见数据集里 let clouds = viewer.scene.pointclouds.filter(e=>e.visible); if(clouds.length == 0)clouds = viewer.scene.pointclouds; let scores = clouds.map((e,i)=>{ return [ -viewer.scene.view.position.distanceToSquared(e.bound2) , i] }); scores = scores.sort((a,b)=> a[0] - b[0]); let neareast = clouds[scores[0][1]]; viewer.flyToDataset({ pointcloud : neareast, duration:0}); } if(Clip.switchMapCount == 0 || !Potree.Utils.getPos2d(viewer.scene.view.position, viewer.mapViewer.viewports[0], viewer.mapViewer.renderArea).inSight || !Potree.Utils.isInsideFrustum(boundingBox, viewer.mapViewer.camera)){ //要使box框和游标都在屏幕内。因为游标是3d的当前位置,很可能是准备去框住的位置 let bound = boundingBox.clone(); bound.expandByPoint(viewer.scene.view.position); let size = bound.getSize(new Vector3); let center = bound.getCenter(new Vector3); let margin = viewer.mainViewport.resolution.clone().multiplyScalar(0.3); viewer.mapViewer.moveTo(center, size, 100, margin); } Clip.switchMapCount++; //关于究竟是focus box还是dataset有点纠结,又或是两个的union。box和数据集可能离得很远,且无法确定当前想选择的数据集,且数据集可能无floorplan, 即使有可能也不展示…… }else {//切换3d if(!Potree.Utils.isInsideFrustum(boundingBox, viewer.scene.getActiveCamera())){//屏幕上没有box的话 viewer.focusOnObject({boundingBox}, 'boundingBox', 100 ); } } },delay); } }; this.switchMapCount = 0; this.bus.addEventListener('flyToPos',this.events.flyToPos); viewer.mapViewer.addEventListener('forceVisible',this.events.mapVisiChange); } this.editing = true; setTimeout(()=>{this.changeCallback(true);},1); }, leave:function(){ viewer.inputHandler.fixSelection = false; viewer.scene.removeVolume(this.box); this.showMap && this.mapBox.dispose(); //viewer.setControls(viewer.fpControls); this.switchView('mainView'); Potree.settings.unableNavigate = false; Potree.settings.ifShowMarker = this.previousView.ifShowMarker; Potree.Utils.updateVisible(viewer.measuringTool.scene, 'clipModel', true); //Potree.Utils.updateVisible(viewer.mapViewer.cursor, 'clipModel', true) viewer.setView(this.previousView); viewer.setLimitFar(true); viewer.setPointStandardMat(false); //viewer.setClipState(true) viewer.controls.setTarget(null); this.splitScreenTool.focusCenter = null; //viewer.mainViewport.axis.dispose() cameraProps.forEach(e=>e.openCount = 0); if(this.showMap){ this.bus.removeEventListener('flyToPos',this.events.flyToPos); viewer.mapViewer.removeEventListener('forceVisible',this.events.mapVisiChange); this.events = null; } this.editing = false; }, initViews(){ if(this.views){ this.views.top.pitch = -Math.PI; this.views.top.yaw = 0; this.views.front.pitch = 0; this.views.front.yaw = 0; //--还原for bugID 44757 return } this.views = {}; this.cameras = {}; this.orthoCamera = new OrthographicCamera(-100, 100, 100, 100, 0.01, 10000); this.orthoCamera.up.set(0,0,1); this.splitScreenTool = new SplitScreen; this.targetPlane = viewer.mainViewport.targetPlane = new Plane(); this.shiftTarget = viewer.mainViewport.shiftTarget = new Vector3; //project在targetPlane上的位置 for(let i=0;i<2;i++){ let prop = cameraProps[i]; let view = new ExtendView(); this.views[prop.name] = view; this.cameras[prop.name] = this.orthoCamera; view.name = prop.name; view.direction = prop.direction; } this.views.mainView = viewer.mainViewport.view; this.cameras.mainView = viewer.mainViewport.camera; }, switchView(name ){//替换view和camera到mainViewport if(this.activeViewName == name)return let view = this.views[name]; let camera = this.cameras[name]; let prop = cameraProps.find(e=>e.name == name); let {boundSize, center, boundingBox} = viewer.bound; this.lastViewName = this.activeViewName; this.activeViewName = name; let lastView = this.views[this.lastViewName]; let lastCamera = this.cameras[this.lastViewName]; viewer.mainViewport.view = view; viewer.mainViewport.camera = camera; if(lastCamera)lastView.zoom = lastCamera.zoom; view.zoom && (camera.zoom = view.zoom); viewer.updateScreenSize({forceUpdateSize:true});//更新camera aspect left等 view.applyToCamera(camera); let ifFocus; let boxBound = Clip.box.boundingBox.clone().applyMatrix4(Clip.box.matrixWorld); if(!Potree.Utils.isInsideFrustum(boxBound, camera)){//屏幕上没有box的话 ifFocus = true; } if(name == 'mainView'){ if(lastView){//2d->3d } if(prop.openCount == 0) ifFocus = false; //viewer.fpControls.lockKey = false }else { this.targetPlane.setFromNormalAndCoplanarPoint( view.direction.clone(), center ); this.targetPlane.projectPoint(view.position, this.shiftTarget ); //target转换到过模型中心的平面,以保证镜头一定在模型外 //view.position.copy(this.splitScreenTool.getPosOutOfModel(viewer.mainViewport)) //if(view.zoom)camera.zoom = view.zoom//恢复上次的zoom if(prop.openCount == 0){//至多执行一次 ifFocus = true; } if(this.lastViewName == 'mainView'){//3d->2d }else { } ifFocus || this.splitScreenTool.updateCameraOutOfModel(); //更新相机位置 //if(name == 'top') viewer.mainViewport.alignment = {rotate:true,translate:true}; if(name == 'front'){ //viewer.mainViewport.alignment = {translate:true, rotateSide:true, translateVec:new THREE.Vector3(0,0,1)}; //只能上下移动 viewer.mainViewport.rotateSide = true; }else { viewer.mainViewport.rotateSide = false; } //viewer.fpControls.lockKey = true } ifFocus && this.focusOnObject(this.box); //首次到front最好能自动对准box的长边的角度上 prop.openCount ++; }, focusOnObject(box, duration=0){ if(this.activeViewName == 'mainView'){ viewer.focusOnObject({boundingBox:box.boundingBox.clone().applyMatrix4(box.matrixWorld)}, 'boundingBox', duration); }else { this.orthoMoveFit(box.position, {bound:box.boundingBox.clone().applyMatrix4(box.matrixWorld)}, duration); } }, transform(type, axis, value ){//使用按键微调 this.box[type][axis] += value; }, setModeEnable(type, enable){ let modes = [] ;['scale', 'translation', 'rotation'].forEach(e=>{ if(e == type){ enable && modes.push(e); }else { viewer.transformationTool.modesEnabled[e] && modes.push(e); } }); viewer.transformationTool.setModeEnable(modes); viewer.dispatchEvent('content_changed'); }, rotateSideCamera(angle){//侧视图绕模型中心水平旋转到的角度 angle: -180 ~ 180 let diff = MathUtils.degToRad(angle) - viewer.mainViewport.view.yaw; this.splitScreenTool.rotateSideCamera(viewer.mainViewport, diff); }, orthoMoveFit(pos, info, duration){ var margin = {x:viewer.mainViewport.resolution.x*0.4, y:viewer.mainViewport.resolution.y*0.4}; this.splitScreenTool.viewportFitBound(viewer.mainViewport, info.bound, pos, duration, margin ); }, getPointcloud:function(){ //找一个离当前最近的点云,且最好有漫游点 let pointclouds = viewer.scene.pointclouds.filter(e=>e.panos.length>0); if(pointclouds.length == 0)pointclouds = viewer.scene.pointclouds; let result = Common.sortByScore(pointclouds,[],[e=>{ let center = e.bound.getCenter(new Vector3); let size = e.bound.getSize(new Vector3).length() / 2; let posToCenter = viewer.images360.position.distanceTo(center); return size / posToCenter }]); return result[0].item }, getTarget:function(boundCenter){//box位置。要找一个有点云的地方。方案1相机位置, 方案2接近相机的漫游点, 方案3接近中心的漫游点。选择方案2,因最大概率有点云 var target = new Vector3(); var cameraPos = viewer.images360.position; var pano = Common.find(viewer.images360.panos , [], [Images360.sortFunctions.floorDisSquaredToPoint(cameraPos)]); if(pano){ target.copy(pano.position); target.setZ(boundCenter.z); }else { target.copy(boundCenter); } return target }, /* switchMap:function(state){ }, */ download:function( ){ if(this.getIntersectPointcloud().length == 0){ return null } var visiPointclouds = viewer.scene.pointclouds.filter(e=> Potree.Utils.getObjVisiByReason(e, 'datasetSelection')); visiPointclouds.sort((a,b)=>{return a.dataset_id-b.dataset_id});//缓存需要固定排序好比较 let data = { transformation_matrix: visiPointclouds.map((cloud)=>{ let data = { id: cloud.dataset_id, matrix : this.getTransformationMatrix(cloud).elements, //剪裁大框 visiMatrixes: cloud.material.clipBoxes_in.map(e=>this.getTransformationMatrix(cloud, e.inverse).elements), //若干个可见型小框(虽然现在用不到了,因为普通界面不展示这些剪裁区域) unVisiMatrixes: cloud.material.clipBoxes_out.map(e=>this.getTransformationMatrix(cloud, e.inverse).elements), //若干个不可见型小框 modelMatrix:(new Matrix4).copy(cloud.transformMatrix).transpose().elements }; return data }) , aabb: "b-0.5 -0.5 -0.5 0.5 0.5 0.5" //剪裁空间( 所有点在乘上这个矩阵后, 还能落在 1 * 1 * 1的box内的点就是所裁剪的 }; return data }, downloadNoCrop(){//不剪裁 下载整个点云 var visiPointclouds = viewer.scene.pointclouds.filter(e=> Potree.Utils.getObjVisiByReason(e, 'datasetSelection')); visiPointclouds.sort((a,b)=>{return a.dataset_id-b.dataset_id});//缓存需要固定排序好比较 let data = { transformation_matrix: visiPointclouds.map((cloud)=>{ let data = { id: cloud.dataset_id, matrix : new Matrix4().elements, //固定值 modelMatrix:(new Matrix4).copy(cloud.transformMatrix).transpose().elements }; return data }) , aabb: "b-12742000 -12742000 -12742000 12742000 12742000 12742000" //固定剪裁空间 }; console.log(data); return data }, getTransformationMatrix:function(pointcloud, invMatrix) {//剪裁矩阵 var invMatrix = invMatrix || this.box.matrixWorld.clone().invert(); return (new Matrix4).multiplyMatrices(invMatrix, pointcloud.transformMatrix).transpose() }, getIntersectPointcloud(){ var intersect = (pointcloud)=>{ if(pointcloud.intersectBox(this.box.matrixWorld))return true }; return viewer.scene.pointclouds.filter(e=>intersect(e)) }, getRulerBound(){//坐标尺边界 let camera = viewer.mainViewport.camera; if(!camera.isOrthographicCamera)return let w = camera.right / camera.zoom; //half let h = camera.top / camera.zoom; let points = []; var boundAtCamera = new Box3(); Clip.box.children[0].geometry.vertices.forEach(e=>{ //模仿getPosWithFullBound let p = e.clone().applyMatrix4(Clip.box.matrixWorld); points.push(p); let p1 = p.clone().applyMatrix4(camera.matrixWorldInverse); boundAtCamera.expandByPoint(p1); }); //需要找出clipbox的bound的左上角,它在标尺中是原点 let ClipBoxLeftTop = new Vector2(boundAtCamera.min.x, boundAtCamera.max.y); //相对于相机的位置 let camPos = new Vector2(-ClipBoxLeftTop.x, ClipBoxLeftTop.y);//由于ClipBoxLeftTop要变换到原点,所以相机位置就成了ClipBoxLeftTop的相反数, 但因是从上到下所以y再乘-1 let bound_ = { left: camPos.x - w, right: camPos.x + w, bottom: camPos.y + h, //注意从上到下增大 top: camPos.y - h, }; //console.log(bound) return bound_ }, screenshot: async (rulerBound, rulerMargin, unitText='像素 : 米' )=>{ //测绘图下载。顶视图|侧视图 return new Promise((resolve,reject)=>{ if(Clip.screenshoting )return reject() let visiPointclouds = viewer.scene.pointclouds.filter(e=> Potree.Utils.getObjVisiByReason(e, 'datasetSelection')); let camera = viewer.mainViewport.camera; //if(Clip.activeViewName == 'mainView')return reject() const maxWidth = 8192, minWidth = Potree.browser.urlHasValue('clipMinWidth',true) || 1024; //图片尺寸最大最小值。 //Potree.settings.pointDensity = 'ultraHigh' viewer.inputHandler.deselectAll(); Clip.box.visible = false; Clip.screenshoting = true; let material = new ExtendPointCloudMaterial; material.pointSizeType = Potree.PointSizeType.FIXED; //Potree.PointSizeType material.size = Potree.browser.urlHasValue('clipPointSize',true) || 1; material.uniforms.minSize.value = 0.1; material.activeAttributeName = 'rgba'; //material.classification = pointcloud.material.classification; material.clipBoxBig_in = visiPointclouds[0].material.clipBoxBig_in; material.shaderNeedsUpdate = true; material.bigClipInBox = visiPointclouds[0].material.bigClipInBox; let materials = visiPointclouds.map(e=>{ let mat = e.material; e.material = material; return mat }); //根据boundingBox尺寸来定图片尺寸 let boundingBox = Clip.box.boundingBox.clone().applyMatrix4(Clip.box.matrixWorld); let points = []; var bound = new Box3(); Clip.box.children[0].geometry.vertices.forEach(e=>{ //模仿getPosWithFullBound let p = e.clone().applyMatrix4(Clip.box.matrixWorld); points.push(p); let p1 = p.clone().applyMatrix4(camera.matrixWorldInverse); bound.expandByPoint(p1); }); let boundSize = bound.getSize(new Vector3); let minZoom = Potree.browser.urlHasValue('clipZoom',true) || 80; //1px = 1个点 = 1cm , 如果一个点都是1cm的话 //zoom不宜过大或过小,过小点云重叠、画面模糊;过大点云有间隙,纹理就看不清 //中间部分75-80为佳,文字100,边缘50. 最好支持调节 . 且当下载尺寸变大往往点云也多,可能冲破pointbudget限制 //let text = `1 : ${(1 / camera.zoom).toFixed(4)}(像素 : 米)` let w = boundSize.x * minZoom; let h = boundSize.y * minZoom; let max = Math.max(w,h); let s = 1; if(max > maxWidth){ s = maxWidth/max; }else if(max < minWidth){ s = minWidth/max; } w *= s, h *= s; material.size *= s; s != 1 && console.log('宽度缩放倍数',s); //虽然size>1就是浪费像素,但是太小了视觉效果有点差……可能因为fov不真实。 点云大就像是老式电视机虽然显示像素量少但感觉不出画面模糊,且边缘锐利。 w = Math.round(w); h = Math.round(h); let focusObjectInfo = [{boundingBox, points}, 'boundingBox', 0, { dontChangeCamDir:true, dontMoveMap:true, boundScale:1 }]; //分块渲染截图,最后拼合图片。需要使每一块的点数不超过pointBudget, 且尺寸不超过4096(会崩溃),过小的话一般不会超过pointBudget,除非是深度较大后排点云多。 let r = MathUtils.clamp(Math.sqrt(3.5e7 / boundSize.z) * s, 1024, 4096); //每多少米分一块。 推理 wc*hc = w / r * h / r = zoom * w * s * zoom * h * s / r^2 = xyz / V(常数,每多少立方米会超出pointBudget,就是括号里的数字,给大概值) //let r = THREE.Math.clamp(1024 / boundSize.z, 256, 4096); let wc = w / r; //横向块数 let hc = h / r; //纵向块数 let splitRenderInfo = (wc > 1 || hc > 1) && {wc: Math.ceil(wc), hc:Math.ceil(hc)}; //因为结果需要化为整数所以可能不太理想, 分割数过多。 另外如果面积小但是因z过长而超出pointBudget没关系,因后排点云无需绘制 console.log({wc,hc,w,h}); let meterPerPixel = boundSize.x / w; let text = `1 : ${(meterPerPixel).toFixed(4)}(${unitText})`; let beforeScreenshot = ()=>{ if(rulerBound){ let ruler = Clip.getRulerBound(); Object.assign(rulerBound,ruler); } }; let {getImagePromise, finishPromise} = viewer.startScreenshot({ type: 'default', bgOpacity:0, focusObjectInfo, maxTimeForPointLoad : 3e5, pointDensity:'screenshot2', splitRenderInfo, beforeScreenshot }, w, h, 1 ); finishPromise.done(({dataUrl}) => { viewer.inputHandler.toggleSelection(Clip.box); Clip.box.visible = true; let img = new Image; img.src = dataUrl; img.onload = async ()=>{//加上标尺比例水印 //固定文字大小和边距像素 //如果有一边过小,可能超出画面外,暂时不管这种可能 const fontsize = math.linearClamp(w, [100, 700, 8000], [12, 20, 30]); const marginSelf = {//img外的margin(标尺内) left : rulerBound ? 76 : 40, right : rulerBound ? 30 : 40, bottom : rulerBound ? 20 + fontsize*2 : 40 + fontsize*2, top : rulerBound ? 60 : 40, }; //const Margin = rulerMargin + marginSelf //文字的边距 let bottomRatioToImg = - Math.min(fontsize*2.5, rulerBound ? marginSelf.bottom : marginSelf.bottom*0.9 ) / h;//- (marginSelf.bottom - fontsize)/ h //在img之下. text底部到img底部的距离 //let leftRatioToImg = Margin / w let labelInfo = { bottomRatioToImg, horizonCenter:true,//leftRatioToImg, textColor: rulerBound ? {r: 0, g: 0, b: 0, a: 1} : { r: 255, g: 255, b: 255, a: 1 }, textBorderColor : { r: 30, g: 30, b: 30, a: 1 }, textBorderThick : rulerBound ? 0 : 1 , fontsize, fontWeight: rulerBound ? 'normal':'Bold', outputCanvas : !!rulerBound, bgColor: rulerBound ? {r:255,g:255,b:255,a:255}:null }; labelInfo.bgMargin = {//img外需要多少margin left: marginSelf.left + rulerMargin, //rulerMargin是在标尺外的margin bottom : marginSelf.bottom + rulerMargin, right: marginSelf.right + rulerMargin, top: marginSelf.top + rulerMargin }; if(rulerBound){//因为margin 扩大bound rulerBound.left -= marginSelf.left * meterPerPixel; rulerBound.right += marginSelf.right * meterPerPixel; rulerBound.top -= marginSelf.top * meterPerPixel; rulerBound.bottom += marginSelf.bottom * meterPerPixel; } //console.log('topRatioToImg',topRatioToImg,'leftRatioToImg',leftRatioToImg,'fontsize', labelInfo.fontsize) let result = await Potree.Utils.imgAddText(img, text , labelInfo ); //Common.downloadFile(finalDataUrl, 'screenshot11.png') visiPointclouds.forEach((e,i)=>e.material = materials[i]); Clip.screenshoting = false; viewer.viewports = [viewer.mainViewport]; resolve(result); }; }); }) /* 隐患:无法获知最大可加载的点的数量,也未知需要加载的点的数量。需要掌控好分块数量,避免因pointBudget限制造成周围的点稀疏。 加载点云时间过久 */ return finishPromise } }; /* 裁剪点云时,2D界面显示全部平面图,按楼层切换显示。 所有点云下载后点云所带的本地坐标都是场景显示的local坐标,这样才能看起来一样。 且为了使之和坐标页面对应,坐标页面的本地坐标也是local,而非mesh_local. //保存box viewer.modules.Clip.box.matrix //恢复box let a = new THREE.Matrix4().fromArray([ 14.189120116830964, 0, 0, 0, 0, 9.924790069368392, 0, 0, 0, 0, 0.8751009568437951, 0, -7.433819981633984, 1.528696446650445, 2.9412375879215977, 1 ]) a.decompose(viewer.modules.Clip.box.position,viewer.modules.Clip.box.quaternion,viewer.modules.Clip.box.scale) viewer.modules.Clip.rotateSideCamera(-93) */ const viewportProps = [ { left:0.5, bottom:0.5, width: 0.5,height:0.5, name : 'MainView', //view: viewer.scene.view, active: true, }, { left:0, bottom:0.5, width: 0.5,height:0.5, name : 'top', name2 : 'mapViewport', axis:["x","y"], direction : new Vector3(0,0,-1), //镜头朝向 //axisSign:[1,1], active: true, //相机位置在z轴正向 }, { left:0.5, bottom:0, width: 0.5,height:0.5, name : 'right', axis:["y","z"], direction : new Vector3(1,0,0), //axisSign:[1,1], active: true, //相机位置在x轴负向 右下角屏 }, { left:0, bottom:0, width: 0.5,height:0.5, name : 'back', axis:["x","z"], direction : new Vector3(0,-1,0), //axisSign:[-1,1], // 从镜头方向看 x向左 所以取负 active: true, //相机位置在y轴正向 左下角屏 }, ]; var SplitScreen4Views = new SplitScreen(); SplitScreen4Views.split = function(o={}){ var defaultCamera = viewer.scene.getActiveCamera(); let {boundSize, center} = viewer.bound; viewer.setLimitFar(false); viewer.mapViewer.attachToMainViewer(true,'split4Screens','dontSet'); viewer.renderer.clear(); //clear back的viewport左边的1px宽的部分,因setScissor的bug let viewports = this.splitStart(viewportProps); //覆盖在map上、点云等其他物体之下的一层背景 let mapViewport = viewer.mapViewer.viewports[0]; mapViewport.noPointcloud = false; //隐藏地图游标 //Potree.Utils.updateVisible(viewer.mapViewer.cursor, 'split4Screens', false) /* viewer.images360.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano.mapMarker, 'split4Screens', false) //希望这时候mapMarker已经建好了吧 }) */ /* viewer.images360.panos.forEach(pano=>{ pano.addLabel2() Potree.Utils.updateVisible(pano.label2, 'notDisplay', true) pano.dispatchEvent({type:'changeMarkerTex',name:'ring'}) }) */ //材质 this.statesBefore = { pointDensity : Potree.settings.pointDensity, displayMode : Potree.settings.displayMode, position: viewer.images360.position, target: viewer.scene.view.getPivot(), currentPano: viewer.images360.currentPano, //--- //ifShowMarker : Potree.settings.ifShowMarker, }; viewer.setPointStandardMat(true,null,true); //切换到标准模式(主要为了mainViewport) 点云使用标准大小 var matBefore = { opacity : new Map() }; var newOpacityMap = new Map(); viewer.scene.pointclouds.forEach(e=>{ matBefore.opacity.set(e, e.temp.pointOpacity); matBefore.colorType = e.material.activeAttributeName; /* { var map = new Map() newOpacityMap.set(e, map ) var size = e.bound.getSize() viewports.forEach(viewport=>{//根据bound设置opacity,越小的要靠越近,需要大的opacity。但似乎影响太大了 if(viewport.name == 'MainView')return; var prop = viewportProps.find(v => viewport.name == v.name2||viewport.name == v.name) let axis = prop.axis var width = size[axis[0]] var height = size[axis[1]] var area = width * height map.set(viewport, 5000/area); }) } */ }); let beforeRender = function(){ viewer.dealBeforeRender = true; //使不全部渲染 viewer.scene.pointclouds.forEach(e=>{ if(this.name == "MainView"){ e.material.activeAttributeName = matBefore.colorType; // 'rgba' e.material.useFilterByNormal = false; //e.material.pointSizeType = Potree.config.material.pointSizeType e.changePointOpacity(matBefore.opacity.get(e)); //1 //恢复下 e.temp.pointOpacity 其实就是1 Potree.settings.pointDensity = 'fourViewportsMain';/* 'fourViewports' */ //本来想比另外三屏高一点质量,结果发现会闪烁,因为点云加载需要时间 (navvis仿版也是一样,以后看看能否优化) }else { e.material.activeAttributeName = "color"; e.material.useFilterByNormal = true; //e.material.pointSizeType = 'ADAPTIVE' Potree.settings.pointDensity = 'fourViewports'; //强制降低点云质量 //侧面重叠概率更大,所以透明度调小 e.changePointOpacity(this.name == "mapViewport" ? 0.5 : 0.1/* newOpacityMap.get(e).get(viewport), true */); //多数据集有的数据集很小,放大后显示特别淡 //console.log(e.name, viewport.name, e.temp.pointOpacity, e.material.opacity) } viewer.images360.panos.forEach(pano=>{ if(this.name == 'mapViewport'){ Potree.Utils.updateVisible(pano.marker, 'showOnMap', true, 1, 'add' ); }else { Potree.Utils.updateVisible(pano.marker, 'showOnMap', false, 1, 'cancel' ); } }); }); viewer.dealBeforeRender = false; }; viewports.forEach(viewport=>{ viewport.beforeRender = beforeRender; new AxisViewer(viewport, viewer.renderArea,{domStyle:{ top: '22px',//viewport.name == 'mapViewport' ? '2px' : '-25px', right: (viewport.name == 'mapViewport') ? '30px' : '2px', width:'80px',height:'80px'} }); }); this.enableMap(false); this.enableFloorplan(false); viewer.mapViewer.setViewLimit('null'); //多数据集距离远时可以任意远,所以不限制了。但是这样就会看到地图边界了怎么办?超出后让地图隐藏? //viewer.dispatchEvent({'type': 'beginSplitView' }) //viewer.updateScreenSize({forceUpdateSize:true}) //this.viewportFitBound(mapViewport, boundSize, center) //Potree.settings.ifShowMarker = false Potree.settings.displayMode = 'showPointCloud'; }; SplitScreen4Views.recover = function(){ viewer.viewports.forEach(e=>{e.axis.dispose();}); this.unSplit(); viewer.mapViewer.viewports[0].beforeRender = null; /* const {width, height} = viewer.renderer.getSize(new THREE.Vector2()); viewer.renderer.setViewport(0,0,width,height) viewer.renderer.setScissorTest( false ); */ viewer.setView({ position: this.statesBefore.position, target: this.statesBefore.target, currentPano: this.statesBefore.currentPano, duration:300, callback:function(){ } }); viewer.mainViewport.beforeRender = null; viewer.setLimitFar(true); let mapViewport = viewer.mapViewer.viewports[0]; viewer.mapViewer.attachToMainViewer(false); //Potree.Utils.updateVisible(viewer.mapViewer.cursor, 'split4Screens', true) /* viewer.images360.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano.mapMarker, 'split4Screens', true) }) */ /* viewer.images360.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano.label2, 'notDisplay', false ) pano.dispatchEvent({type:'changeMarkerTex',name:'default'}) }) */ mapViewport.noPointcloud = true; { this.enableMap(Potree.settings.mapEnable); this.enableFloorplan(Potree.settings.floorplanEnable); if(this.floorplanListener){ viewer.mapViewer.mapLayer.removeEventListener( 'floorplanLoaded', this.floorplanListener ); this.floorplanListener = null; } } Potree.settings.pointDensity = this.statesBefore.pointDensity; if(!Potree.settings.isOfficial){ Potree.settings.displayMode = this.statesBefore.displayMode; } viewer.scene.pointclouds.forEach(e=>{ e.material.useFilterByNormal = false; //e.material.pointSizeType = Potree.config.material.pointSizeType }); viewer.setPointStandardMat(false); viewer.mapViewer.setViewLimit('standard'); viewer.images360.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano.marker, 'showOnMap', false, 1, 'cancel' ); }); //Potree.settings.ifShowMarker = this.statesBefore.ifShowMarker //viewer.dispatchEvent({'type': 'finishSplitView' }) //viewer.updateScreenSize({forceUpdateSize:true}) }; SplitScreen4Views.updateMapViewerBG = function(){ let mapViewport = viewer.mapViewer.viewports[0]; if(this.splited && (this.floorplanEnabled || this.mapEnabled)){ mapViewport.background = 'overlayColor'; mapViewport.backgroundColor = new Color(0,0,0); mapViewport.backgroundOpacity = 0.5; }else { mapViewport.background = null; mapViewport.backgroundColor = null; mapViewport.backgroundOpacity = null; } viewer.mapViewer.mapChanged = true; viewer.mapViewer.needUpdate = true; }; SplitScreen4Views.setFloorplanDisplay = function(e, show=false){ e.floorplan.setEnable(show); }; SplitScreen4Views.enableMap = function(enable){ let map = viewer.mapViewer.mapLayer.maps.find(e=>e.name == 'map'); map.setEnable(!!enable); //viewer.mapViewer.mapGradientBG = viewer.background == 'gradient' && !enable this.mapEnabled = enable; this.updateMapViewerBG(); }, //直接覆盖原设置 SplitScreen4Views.enableFloorplan = function(enable){ //是否让自定义的平面图显示 let floorplans = viewer.mapViewer.mapLayer.maps.filter(e=>e.name.includes('floorplan')); if(this.floorplanListener){ viewer.mapViewer.mapLayer.removeEventListener( 'floorplanLoaded', this.floorplanListener ); } this.floorplanListener = (e)=>{ this.setFloorplanDisplay(e, enable); }; viewer.mapViewer.mapLayer.addEventListener( 'floorplanLoaded', this.floorplanListener ); //万一之后才加载 if(!enable){ //隐藏平面图 floorplans.forEach(floorplan=>this.setFloorplanDisplay({floorplan},false)); }else { floorplans.forEach(floorplan=>this.setFloorplanDisplay({floorplan},true)); } if (enable && floorplans.length == 0) Potree.loadMapEntity('all',true); this.floorplanEnabled = enable; this.updateMapViewerBG(); }, /* viewportFitBound:function(viewport, boundSize, center){ //使一个viewport聚焦在某个范围 var prop = viewportProps.find(v => viewport.name == v.name2||viewport.name == v.name) let axis = prop.axis let expand = 10; let position = center.clone() var moveAtAxis = ['x','y','z'].find(e=>!(axis.includes(e))) if(viewport.name == 'mapViewport'){ let ori = viewport.view.position[moveAtAxis] position[moveAtAxis] = ori //不改变这个值,尤其是mapViewer中的z }else{ position[moveAtAxis] += boundSize[moveAtAxis]/2+expand//移动到bounding边缘外 } viewport.view.position.copy(position) var width = Math.max(boundSize[axis[0]], boundSize[axis[1]] * viewport.camera.aspect)//视口宽度(米) var margin = 50 //px viewport.camera.zoom = (viewport.resolution.x - margin) / width viewport.camera.updateProjectionMatrix() }, */ SplitScreen4Views.focusOnPointCloud = function(pointcloud){//三个屏都聚焦在这个点云 var boundSize = pointcloud.bound.getSize(new Vector3); var center = pointcloud.bound.getCenter(new Vector3); let target = pointcloud.panosBound && pointcloud.panosBound.center; //看向pano集中的地方,也就是真正有点云的地方。(因为需要展示所有点云,所以没办法用这个做为center) this.focusOnObject(pointcloud.bound, center,target); viewer.flyToDataset({pointcloud, dontMoveMap:true, duration:0}); }; SplitScreen4Views.focusOnObject = function(bound, center, target, duration=0){ viewer.viewports.forEach(e=>{ if(e.name == 'MainView'){ /* let len = boundSize.length() let distance = THREE.Math.clamp(e.view.position.distanceTo(center), len * 0.01, len*0.3 ) //距离限制 //viewer.focusOnObject({position:center}, 'point', duration, {distance, direction: e.view.direction,dontMoveMap:true} )//平移镜头 //为了方便定位,直接飞到中心位置: e.view.setView({ position:center, duration, target }) */ }else { this.viewportFitBound(e, bound, center); } }); }; var Alignment = { SplitScreen: SplitScreen4Views, handleState:null, //操作状态 'translate'|'rotate' bus: new EventDispatcher(), /* prepareRecord : true, writeToHistory(pointclouds){ if(!this.prepareRecord)return; this.prepareRecord = false let content = this.getTemp(pointclouds) this.history.writeIn(content) }, undo(){//撤销一步 let last = this.history.pop(); last && last.forEach(item=>{ this.applyTemp(item) }) }, */ applyTemp(item){ var pointcloud = viewer.scene.pointclouds.find(p=>p.dataset_id+p.name == item.sid); pointcloud.orientationUser = item.orientationUser; pointcloud.translateUser = item.translateUser; this.setMatrix( pointcloud ); }, getTemp(pointclouds){//记录最近一次保存后的状态,便于恢复 pointclouds = pointclouds || viewer.scene.pointclouds; return pointclouds.map(e=>{ return { sid : e.dataset_id+e.name, orientationUser : e.orientationUser, translateUser : e.translateUser.clone(), } } ) }, init:function(){ let transfromInfo; this.history = new History({ applyData: (data)=>{ data.forEach(item=>{ Alignment.applyTemp(item); }); return true }, getData:(pointclouds)=>{ return Alignment.getTemp(pointclouds) } }); viewer.fpControls.addEventListener("transformPointcloud",(e)=>{ if(e.pointclouds[0].dataset_id == Potree.settings.originDatasetId){//禁止手动移动初始数据集 return this.bus.dispatchEvent('forbitMoveOriginDataset') } this.history.beforeChange(e.pointclouds); //this.writeToHistory( e.pointclouds ) if(!transfromInfo){ transfromInfo = {pointclouds:e.pointclouds}; } if(this.handleState == 'translate'){ e.pointclouds.forEach(cloud=>Alignment.translate(cloud, e.moveVec)); }else if(this.handleState == 'rotate'){ if(Potree.settings.editType == 'pano'){ //旋转中心是intersectStart的版本 /* let center = e.intersectStart //旋转中心是mousedown的位置 if(e.intersect.equals(center))return if(!transfromInfo){ transfromInfo = { orientationUser : e.pointclouds[0].orientationUser, //vecStart : e.moveVec, // 首次移动向量 只有八个方向,精度太小,所以延迟 pointclouds: e.pointclouds } this.bus.dispatchEvent({type:'rotateStart', startPoint:center}) return }else if(!transfromInfo.vecStart){ let vec = new THREE.Vector3().subVectors(e.intersect, center).setZ(0) if(vec.length() * e.camera.zoom > 30){ //在屏幕上距离初始点有一定距离后开始算 //console.log('moveVec',vec) transfromInfo.vecStart = vec } */ let center = e.pointclouds[0].translateUser; //旋转中心是第一个点云的位置 if(e.intersect.equals(center))return if(!transfromInfo.vecStart){ transfromInfo.orientationUser = e.pointclouds[0].orientationUser; transfromInfo.vecStart = new Vector3().subVectors(e.intersectStart, center).setZ(0); }else { let vec = new Vector3().subVectors(e.intersect, center).setZ(0); let angle = math.getAngle(transfromInfo.vecStart,vec,'z'); let diffAngle = transfromInfo.orientationUser + angle - transfromInfo.pointclouds[0].orientationUser; transfromInfo.pointclouds.forEach(cloud=>{ /* let centerNoTranfrom = Potree.Utils.datasetPosTransform({ toDataset: true, pointcloud:cloud, position: center }) //中心点在数据集中的位置 Alignment.rotate(cloud, null, diffAngle) let centerNow = Potree.Utils.datasetPosTransform({ fromDataset: true, pointcloud:cloud, position: centerNoTranfrom }) //中心点的现在位置 let shift = new THREE.Vector3().subVectors( center, centerNow); //偏移量 Alignment.translate(cloud,shift) //使center还保留在原位 //let centerNow1 = Potree.Utils.datasetPosTransform({ fromDataset: true, pointcloud:transfromInfo.pointcloud, position: centerNoTranfrom }) //中心点的现在位置 */ Alignment.rotateAround(center, cloud, null, diffAngle); }); } //this.bus.dispatchEvent({type:'rotate', endPoint: e.intersect}) }else { let center = e.pointclouds[0].translateUser; //移动到的位置就是中心 if(e.intersect.equals(center))return if(!transfromInfo.vecStart){ transfromInfo.orientationUser = e.pointclouds[0].orientationUser; transfromInfo.vecStart = new Vector3().subVectors(e.intersectStart, center).setZ(0); }else { let vec = new Vector3().subVectors(e.intersect, center).setZ(0); let angle = math.getAngle(transfromInfo.vecStart,vec,'z'); let diffAngle = transfromInfo.orientationUser + angle - transfromInfo.pointclouds[0].orientationUser; Alignment.rotate(transfromInfo.pointclouds[0], null, diffAngle); } } } }); viewer.fpControls.addEventListener("end",(e)=>{ transfromInfo && this.history.afterChange(transfromInfo.pointclouds ); transfromInfo = null; }); // cursor: let updateCursor = (e)=>{ if(e.drag || !this.editing)return //仅在鼠标不按下时更新: let handleState = Alignment.handleState; if(e.hoverViewport.alignment && handleState && e.hoverViewport.alignment[handleState]){ if(handleState == 'translate'){ if( e.intersect && e.intersect.location ){ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"movePointcloud" }); }else { viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"movePointcloud" }); } }else if(handleState == 'rotate'){ if( e.intersect && e.intersect.location ){ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"rotatePointcloud" }); }else { viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"rotatePointcloud" }); } } }else { //清空: viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"movePointcloud" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"rotatePointcloud" }); } }; viewer.addEventListener('global_mousemove',updateCursor); viewer.addEventListener('global_drop',updateCursor);//拖拽结束 viewer.addEventListener('updateModelBound', (e)=>{ if(this.editing){ this.SplitScreen.updateCameraOutOfModel(); } }); }, setMatrix : function(pointcloud){ var vec1 = pointcloud.position; //position为数据集内部的偏移,在navvis中对应的是dataset.pointCloudSceneNode的children[0].position var vec2 = pointcloud.translateUser; var angle = pointcloud.orientationUser; var pos1Matrix = new Matrix4().setPosition(vec1);//先移动到点云本身应该在的初始位置(在4dkk里和其他应用中都是在这个位置的,也能和漫游点对应上) var rotMatrix = new Matrix4().makeRotationAxis(new Vector3(0,0,1), angle);//再旋转 var pos2Matrix = new Matrix4().setPosition(vec2);//最后是平移 var matrix = new Matrix4().multiplyMatrices(pos2Matrix, rotMatrix); pointcloud.transformMatrix = matrix.clone();//为该数据集的变化矩阵。 对应navvis的m2w_ pointcloud.transformInvMatrix.copy(pointcloud.transformMatrix).invert(); pointcloud.rotateMatrix = rotMatrix; pointcloud.rotateInvMatrix.copy(rotMatrix).invert(); pointcloud.panos.forEach(e=>e.transformByPointcloud()); matrix = new Matrix4().multiplyMatrices(matrix, pos1Matrix); //matrix.premultiply(viewer.scene.scene.matrix);////////////////////////add pointcloud.matrix = matrix; //pointcloud.matrixWorldNeedsUpdate = true //更新matrixWorld (非计算,直接赋值) pointcloud.updateMatrixWorld(true); if(this.editing){ Alignment.changeCallBack && Alignment.changeCallBack(); } if(pointcloud.spriteNodeRoot){ pointcloud.spriteNodeRoot.matrixWorld.copy(pointcloud.matrixWorld);//.multiplyMatrices(pointcloud.matrixWorld, pointcloud.matrixWorld); } viewer.boundNeedUpdate = true; //pointcloud.updateBound() pointcloud.getPanosBound(); viewer.dispatchEvent('content_changed'); }, rotateAround(center, pointcloud, deg, angle){//绕center点转动 var angle = angle != void 0 ? angle : MathUtils.degToRad(deg); let vec1 = new Vector3().subVectors(pointcloud.translateUser, center); let rotMatrix = new Matrix4().makeRotationAxis(new Vector3(0,0,1), angle); let vec2 = vec1.clone().applyMatrix4(rotMatrix); //将到旋转中心的偏差也转动 let vec3 = new Vector3().subVectors(vec2,vec1); //这个就是多出来的一步translateUser this.rotate(pointcloud, deg, angle); this.translate(pointcloud, vec3); //绕点转动就是比普通转动多一步移动到相对center的某个位置。 1 初始点云移动到自己的position; 2 移动一个vec1 3绕原点旋转 4再移动一个原本的translateUser。 绘制出来后发现移动量就是第二步vec旋转后的偏移 }, rotate:function(pointcloud, deg, angle){//绕各自中心转动(各自的position) 假设点云位移position后0,0,0就是它的中心了(根据navvis观察这样做是绕同一个点旋转的) var angle = angle != void 0 ? angle : MathUtils.degToRad(deg); //正逆负顺 pointcloud.orientationUser += angle; Alignment.setMatrix(pointcloud); }, translate:function(pointcloud, vec){ pointcloud.translateUser.add(vec); Alignment.setMatrix(pointcloud); }, enter:function(){ //this.saveTemp() this.originData = this.getTemp(); this.SplitScreen.split({alignment:true}); /* viewer.images360.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano.mapMarker, 'split4Screens', false) }) */ viewer.viewports.find(e=>e.name == 'mapViewport').alignment = {rotate:true,translate:true}; viewer.viewports.find(e=>e.name == 'right').alignment = {translate:true, translateVec:new Vector3(0,0,1)}; //只能上下移动 viewer.viewports.find(e=>e.name == 'back').alignment = {translate:true, translateVec:new Vector3(0,0,1)}; //只能上下移动 this.editing = true; viewer.updateFpVisiDatasets(); }, leave:function(){ this.switchHandle(null); /* this.originData.forEach(e=>{//恢复 var pointcloud = viewer.scene.pointclouds.find(p=>p.dataset_id == e.id) this.translate(pointcloud, new THREE.Vector3().subVectors(e.translateUser , pointcloud.translateUser)) this.rotate(pointcloud, null, e.orientationUser - pointcloud.orientationUser) }) */ this.originData.forEach(e=>{//恢复 this.applyTemp(e); }); this.SplitScreen.recover(); /* viewer.images360.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano.mapMarker, 'split4Screens', true) }) */ this.editing = false; this.history.clear(); viewer.updateFpVisiDatasets(); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"movePointcloud" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"rotatePointcloud" }); } , switchHandle:function(state){ this.handleState = state; //清空: viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"movePointcloud" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"rotatePointcloud" }); this.bus.dispatchEvent({type:'switchHandle' , state }); }, save: function(){//保存所有数据集的位置和旋转 let callback = ()=>{//保存成功后 this.originData = this.getTemp(); //this.saveTemp(); //需要修改 测量线的position。漫游点已经实时修改了 viewer.scene.measurements.forEach(e=>e.transformByPointcloud()); viewer.images360.updateCube(viewer.bound); }; var data = viewer.scene.pointclouds.map(e=>{ let pos = viewer.transform.lonlatToLocal.inverse(e.translateUser.clone()); let initialPointcloud = viewer.scene.pointclouds.find(e=>e.dataset_id == Potree.settings.originDatasetId); return { id: e.dataset_id, orientation : e.orientationUser, location:[pos.x, pos.y, pos.z + initialPointcloud.datasetData.location[2]], //transformMatrix: e.transformMatrix.elements, } }); //data = JSON.stringify(data) //test: 退出后保留结果 if(!Potree.settings.isOfficial){ callback(); } return {data, callback} } }; let texLoader$9 = new TextureLoader(); let markerMats$2; let markerSizeInfo$2 = {width2d:35}; let color$2 = new Color('#FFF'); let faceMats; let getFaceMat = (name)=>{ if(!faceMats){ //navvis材质可以搜gridTexture let gridTex = texLoader$9.load( Potree.resourcePath+'/textures/gridmap.png' ); gridTex.wrapS = gridTex.wrapT = RepeatWrapping; //gridTex.repeat.set(0.5,0.5)//放大一些 faceMats = { dataset: new MeshStandardMaterial({ color:812922, side:DoubleSide, opacity:0.2, transparent:true, depthTest:false, wireframe:true }), building: new MeshStandardMaterial({ color:812922, metalness: 0.2, roughness:0.8, side:DoubleSide, opacity:0.1, transparent:true, depthTest:true }), buildingSelect: new MeshStandardMaterial({ color:36582, metalness: 0, roughness:1, side:DoubleSide, opacity:0.1, transparent:true, depthTest:true }), floor: new MeshStandardMaterial({ color:11708469, metalness: 0.1, roughness:1, side:DoubleSide,//BackSide, opacity:0.05, transparent:true, depthTest:true, }), floorSelect: new DepthBasicMaterial({ map: gridTex, color:16707151, side:DoubleSide,//BackSide, opacity:1, transparent:true, useDepth : true, clipDistance : 1, occlusionDistance:1, /* occlusionDistance:变为backColor距离, clipDistance:opacity到达0或者1-maxClipFactor时的距离 */ maxClipFactor:0.9, backColor:'#efe' //backColor:"#669988" , }), room: new MeshStandardMaterial({ color:"#ff44ee", metalness: 0, roughness:1, side:DoubleSide,//BackSide, opacity:0.08, transparent:true, depthTest:false, }), roomSelect: new DepthBasicMaterial({ map: gridTex, color:"#ff44ee", side:DoubleSide,//BackSide, opacity:1, transparent:true, useDepth : true, clipDistance : 1, occlusionDistance:0.5, /* occlusionDistance:变为backColor距离, clipDistance:opacity到达0或者1-maxClipFactor时的距离 */ maxClipFactor:0.8, backColor:'#ff88dd'//"#cc99c2" , }) }; } return faceMats[name] }; class BuildingBox extends ctrlPolygon{//建筑实体,包括building, floor, room constructor(prop) { prop.dimension = '3d'; //prop.name = Potree.config.siteModel.names[prop.buildType] + super('siteModel_'+prop.buildType, prop); this.midMarkers = []; this.buildChildren = [];//子实体 this.holes = []; //在这创建的hole this.parentHoles = [];//floor从building那得到的当层holes this.mats = {}; //材质 this.panos = this.panos || []; this.center; //中心点 if(this.buildType=='floor'){ this.points = prop.points = this.buildParent.points;//完全等于建筑的点 this.buildParent.holes.forEach(hole=>{//从building获取holes let floorHole = new BuildingBox({ buildType : 'hole', buildParent:this, originHole : hole, //整栋大楼在当层的hole ifDraw: this.ifDraw || Potree.settings.drawEntityData }); this.parentHoles.push(floorHole); this.add(floorHole); floorHole.points = hole.points;//完全等于建筑的点 }); } if(this.buildType == 'room' || this.buildType == 'hole'){ this.restrictArea = this.buildParent; //不能超出的区域 } if(this.buildParent)this.dontDragFloorHeight = this.buildParent.dontDragFloorHeight; if(this.buildType != 'hole'){ this.box = this.createBox(); //无论是否edit都绘制的原因:为了将在外的点移到在内,需要用mesh来获取intersect this.add(this.box); this.box.visible = !!this.ifDraw; } if(this.ifDraw){ //只存储空间模型信息,不绘制 { this.createPrismLines(color$2); this.lineMesh.visible = false; Potree.Utils.setObjectLayers(this.lineMesh, 'bothMapAndScene' ); } this.addEventListener('dragChange',(e)=>{ //修改中点 this.updateTwoMidMarker(e.index); }); } this.initData(prop); } initData(prop){ if(prop.ifDraw){ super.initData(prop); }else { if(prop.points){ this.points = prop.points; } } } intersectPointcloudVolume(pointcloud){//和pointcloud的重叠体积 return this.intersectPointcloudArea(pointcloud) * this.coverPointcloudHeight(pointcloud) } coverPointcloudHeight(pointcloud, getPercent){ let bound2 = pointcloud.bound; let {zMin , zMax} = this.getRealZ(); let min = Math.min(zMin, bound2.min.z); let max = Math.max(zMax, bound2.max.z); let height1 = zMax - zMin; let height2 = bound2.max.z-bound2.min.z; let coverHeight = height1 + height2 - (max-min);//重叠高度 <=0是没重叠 if(getPercent){ return coverHeight / height1 } return coverHeight } intersectPointcloudArea(pointcloud, getPercent){//和pointcloud的重叠面积 var bound = this.getBound(); let bound2 = pointcloud.bound; if(!bound.intersectsBox(bound2)) return 0; let boxPoints = pointcloud.getUnrotBoundPoint(); //获取tightBound的四个点。 如果是有旋转角度的点云,这个和pointcloud.bound的四个点是不一致的,覆盖面积小于pointcloud.bound let areaWhole = 0; let area1 = this.getArea(); let area2 = Math.abs(math.getArea(boxPoints)); {//计算points与点云总面积 (但是把hole也加入了面积)(并集,重叠部分只算一次) let rings = math.getPolygonsMixedRings([this.points, boxPoints] ); rings.forEach(e=>{ areaWhole+=e.area; }); } let coverHoleArea = 0; //holes与数据集重叠的部分 let holes = this.holes.concat(this.parentHoles); let holesArea = 0; //所有holes面积相加 let areaHoleWithPointcloud = 0; //hole和点云的面积并集 if(holes.length>0){//还要再扣除holes与数据集重叠的部分。其中holes为mix轮廓 let outHoles = [];//没有重合的holes的外轮廓 /* if(holes.length>=2){//合并holes。如果能在绘制时直接合并holes就好啦,这步就转移到那去,但是要删除hole好麻烦 let holes_ = holes.map(e=>e.points) outHoles = math.getPolygonsMixedRings(holes_, true ) outHoles.forEach(e=>{ holesArea+=e.area }) outHoles = outHoles.map(e=>e.points) }else{ outHoles = holes.map(e=>e.points) outHoles.forEach(e=> holesArea += Math.abs(math.getArea(e))) } */ holesArea = this.getHolesArea(); //holes与数据集重叠的部分 { let polygons = outHoles.concat([boxPoints]); let rings = math.getPolygonsMixedRings(polygons); rings.forEach(e=>{ areaHoleWithPointcloud+=e.area; }); coverHoleArea = holesArea + area2 - areaHoleWithPointcloud;//hole和点云的交集 } } let coverArea = area1 + area2 - areaWhole - coverHoleArea; //重叠面积 if(getPercent){ return { toEntity:coverArea / this.getArea(true), //占entity toPointcloud:coverArea / area2 //占点云 } } return coverArea } addHole(points=[]){ let prop = { buildType : 'hole', zMin : this.zMin, zMax : this.zMax, points, buildParent:this, ifDraw: this.ifDraw || Potree.settings.drawEntityData }; //hole的zMin zMax跟随buildParent var hole = new BuildingBox(prop); this.holes.push(hole); if(this.buildType == 'building'){//为每一层添加对应的hole this.buildChildren.forEach(floor=>{ let floorHole = new BuildingBox({ buildType : 'hole', zMin : this.zMin, zMax : this.zMax, buildParent:floor, originHole : hole, //整栋大楼在当层的hole ifDraw: this.ifDraw || Potree.settings.drawEntityData }); floor.parentHoles.push(floorHole); floor.add(floorHole); floorHole.points = hole.points;//完全等于建筑的点 }); } this.add(hole);//直接加在这,不加meshGroup了 this.update(); //update box mesh return hole //hole不创建box,只有它的buildParent需要更新box。 但有线条和marker. hole不在buildChildren里,但有buildParent } removeHole(hole){// 这个hole不会是parentHoles里的。 hole.dispose(); if(this.buildType == 'building'){ //若是整栋大楼的hole,在每层去除它的对应hole this.buildChildren.forEach(floor=>{ let holeAtFloor = floor.parentHoles.find(e=>e.originHole == this ); let index = floor.parentHoles.indexOf(holeAtFloor); index > -1 && floor.parentHoles.splice(index, 1); holeAtFloor.dispose(); }); } let index = this.holes.indexOf(hole); if(index>-1){ this.holes.splice(index, 1); } this.remove(hole); this.update(); } createBox(){ var geometry = new Geometry(); this.mats.boxDefault = getFaceMat(this.buildType); this.mats.boxSelected = getFaceMat(this.buildType+'Select'); var mesh = new Mesh(geometry, this.mats.boxDefault); mesh.name = 'buildingBox'; if(this.buildType == 'floor'){ Potree.Utils.setObjectLayers(mesh, 'siteModelMapUnvisi' ); //楼层默认在地图不显示,为了不会叠加透明度 //mesh.renderOrder = 1 }else { /* if(this.buildType == 'room'){ mesh.renderOrder = 2 } */ Potree.Utils.setObjectLayers(mesh, 'bothMapAndScene' ); } //mesh.frustumCulled = false; return mesh } addMarker(o={} ){ if(this.buildType=='floor')return; //楼层不需要marker let marker = new Sprite$2({mat:this.getMarkerMaterial('default'), renderOrder : 3, sizeInfo: markerSizeInfo$2, dontFixOrient: true, name:"building_marker"} ); Potree.Utils.setObjectLayers(marker, 'siteModeOnlyMapVisi' ); o.marker = marker; super.addMarker(o); if(!this.selected)Potree.Utils.updateVisible(marker,'select',false); let addClickEvent = (e)=>{ let click = (e) => { this.dispatchEvent({type:'clickMarker', marker } ); //由entity发送给sitemodel统一处理 }; marker.addEventListener('click', click); marker.addEventListener('clickSelect', (e)=>{ this.setMarkerSelected(marker, e.state ? 'select' : 'unselect' ); }); marker.removeEventListener('addHoverEvent',addClickEvent); }; marker.addEventListener('addHoverEvent',addClickEvent);//当非isNew时才添加事件 if(!this.isNew){ marker.dispatchEvent('addHoverEvent'); } return marker } removeMarker(index){ super.removeMarker(index); if(!this.isNew){ //重新添加midMarkers this.midMarkers.forEach(e=>this.remove(e)); this.midMarkers = []; this.addMidMarkers(); } this.update(); if(this.points.length == 2 && this.box){//清除原先length>=3时候的 this.box.geometry = new Geometry(); } } addMidMarker(index, point){ if(this.buildType=='floor')return; //楼层不需要marker let marker = new Sprite$2({mat:this.getMarkerMaterial('midPrepare'), sizeInfo: markerSizeInfo$2, dontFixOrient: true, name:"building_midMarker"} ); this.midMarkers = [...this.midMarkers.slice(0,index), marker, ...this.midMarkers.slice(index,this.midMarkers.length)]; marker.renderOrder = 3; Potree.Utils.setObjectLayers(marker, 'siteModeOnlyMapVisi' ); { // Event Listeners let mouseover = (e) => { this.setMarkerSelected(e.object, 'hover', 'single'); viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"markerMove" }); }; let mouseleave = (e) => { this.setMarkerSelected(e.object, 'unhover', 'single'); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"markerMove" }); }; let drag = (e) => { this.dispatchEvent('startDragging'); let index = this.midMarkers.indexOf(marker); let newMarker = this.addMarker({index:(index+1), point:marker.position.clone() }); this.addMidMarker(index+1, new Vector3 ); this.updateTwoMidMarker(index+1); this.setMarkerSelected(marker, 'unhover'); viewer.inputHandler.startDragging(newMarker , {/* dragViewport:viewer.mapViewer.viewports[0], */ } ); //notPressMouse代表不是通过按下鼠标来拖拽. dragViewport指定了只能在地图上拖拽 }; marker.addEventListener('drag', drag ); marker.addEventListener('mouseover', mouseover); marker.addEventListener('mouseleave', mouseleave); } this.add(marker); this.updateMarker(marker, point); if(!this.selected)Potree.Utils.updateVisible(marker,'select',false); return marker } addMidMarkers(){//第一次画好所有marker后,一次性为线段增加中点marker let length = this.points.length; this.points.forEach((point,index)=>{ let nextPoint = this.points[(index+1)%length]; let midPoint = new Vector3().addVectors(point, nextPoint).multiplyScalar(0.5); this.addMidMarker(index, midPoint ); }); } updateTwoMidMarker(index){//更新第index个marker两边的midMarker if(!this.midMarkers.length)return let length = this.points.length; let last = this.points[(index-1+length)%length]; //它之前的marker位置 let next = this.points[(index+1)%length];//它之后的marker位置 let current = this.points[index];//当前位置 let lastMid = new Vector3().addVectors(last, current).multiplyScalar(0.5);//上一个中点 let nextMid = new Vector3().addVectors(next, current).multiplyScalar(0.5);//下一个中点 let lastMidMarker = this.midMarkers[(index-1+length)%length]; let nextMidMarker = this.midMarkers[index]; this.updateMarker(lastMidMarker, lastMid); this.updateMarker(nextMidMarker, nextMid); } dispose(){//销毁geo、remove from parent super.dispose(); this.box && this.box.geometry.dispose(); this.lineMesh && this.lineMesh.geometry.dispose(); this.holes.forEach(e=>e.dispose()); this.parentHoles.forEach(e=>e.dispose()); //this.buildChildren.forEach(e=>e.dispose()) this.dispatchEvent('dispose'); } updateBox(){ if(!this.box)return this.box.geometry.dispose(); var shrink = this.buildType == 'room' ? 0.11 : this.buildType == 'floor' ? 0.082 : 0.2 ;//防止mesh重叠冲突(给一个不寻常的数字) 但离远了还是会有点闪烁 if(this.points.length >= 3){ let holes = this.holes.concat(this.parentHoles); let holesPoints = holes.filter(e=>e.points.length>2).map(e=>e.points); this.box.geometry = MeshDraw.getExtrudeGeo(this.points, holesPoints, { depth:this.zMax-this.zMin-shrink, UVGenerator: new MetricUVGenerator() }); if(this.buildType == 'building' ){ this.box.position.z = this.zMin - shrink / 2; }else { this.box.position.z = this.zMin + shrink / 2; } } } update(options={}){ super.update(this.buildType != 'floor' && options.ifUpdateMarkers); let length = this.points.length; {//确保一下一样 if(this.originHole){ this.points = this.originHole.points; //完全等于building的hole } if(this.buildType == 'hole'){ this.zMin = this.buildParent.zMin; this.zMax = this.buildParent.zMax; } } if(!options.dontUpdateBox){ let boxOwner; if(this.buildType == 'hole'){ if(this.buildParent.buildType == 'building'){ //若是整栋大楼的hole,在每层都要更新下它的对应hole this.buildParent.buildChildren.forEach(floor=>{ let holeAtFloor = floor.parentHoles.find(e=>e.originHole == this ); holeAtFloor && holeAtFloor.update(); //刚开始创建时还没创建对应的 holeAtFloor会为null }); } boxOwner = this.buildParent; }else { boxOwner = this; } boxOwner.updateBox(); } this.updatePrismLines();//update lines if(!options.dontUpdateChildren){ if(this.buildType == 'building' ){ this.buildChildren.forEach(floor=>{ floor.points = this.points; floor.update(); }); } { let holes = this.holes.concat(this.parentHoles); holes.forEach(hole=> { hole.update({dontUpdateBox:true});//父级更新了box,hole就不需要更新box了 }); } } } getHolesArea(){ let holes = this.holes.concat(this.parentHoles); let outHoles, holesArea = 0; if(holes.length>=2){//合并holes。如果能在绘制时直接合并holes就好啦,这步就转移到那去,但是要删除hole好麻烦 let holes_ = holes.map(e=>e.points); outHoles = math.getPolygonsMixedRings(holes_, true ); outHoles.forEach(e=>{ holesArea+=e.area; }); outHoles = outHoles.map(e=>e.points); }else { outHoles = holes.map(e=>e.points); outHoles.forEach(e=> holesArea += Math.abs(math.getArea(e))); } return holesArea } getArea(ifRidOfHoles){//面积 //不排除hole return Math.abs(math.getArea(this.points)) - (ifRidOfHoles ? this.getHolesArea() : 0) } getVolume(ifRidOfHoles){//体积 let {zMin , zMax} = this.getRealZ(); let height = zMax - zMin; if(isNaN(height))height = 0; return this.getArea(ifRidOfHoles) * height } getRealZ(){//求真实高度时用到的 let zMin , zMax; if (this.buildType == 'building') { //因为building只有一个地基平面 所以自身的zMin == 自身的zMax 所以要算 let top = this.buildChildren[this.buildChildren.length - 1]; let btm = this.buildChildren[0]; zMin = btm ? btm.zMin : 0; //建好的建筑不加楼的话是0。 zMax = top ? top.zMax : 0; }else if(this.buildType == 'hole'){ return this.buildParent.getRealZ() }else { zMin = this.zMin, zMax = this.zMax; } return {zMin,zMax} } //架站式多楼层SG-t-ihjV2cDVFlE 有初始的z, 但是总高度范围小于数据集。 不允许修改高度。 /* getDrawZ(){ //画线和box时用到的z let zMin , zMax if(this.buildType == 'hole'){ if(this.buildParent.buildType == 'building' && atFloor){ zMin = atFloor.zMin, zMax = atFloor.zMax }else{ zMin = this.buildParent.zMin, zMax = this.buildParent.zMax } }else{ zMin = this.zMin, zMax = this.zMax } return {zMin, zMax} } */ getBound(){ let bound = new Box3; let {zMin , zMax} = this.getRealZ(); let points = this.buildType == 'floor' ? this.buildParent.points : this.points; points.forEach(p=>{ bound.expandByPoint(p.clone().setZ(zMin)); bound.expandByPoint(p.clone().setZ(zMax)); }); return bound } getMarkerMaterial(type) { if(!markerMats$2){ markerMats$2 = { default: new MeshBasicMaterial({ transparent: !0, color: color$2, opacity: 0.8, map: texLoader$9.load(Potree.resourcePath+'/textures/whiteCircle.png' ), depthTest:false, }), midPrepare: new MeshBasicMaterial({ //线中心的半透明点 transparent: !0, color: color$2, opacity: 0.4, map: texLoader$9.load(Potree.resourcePath+'/textures/whiteCircle.png' ), depthTest:false, }), hover: new MeshBasicMaterial({ transparent: !0, color: color$2, opacity: 1, map: texLoader$9.load(Potree.resourcePath+'/textures/whiteCircle.png' ), depthTest:false, }), select: new MeshBasicMaterial({ transparent: !0, color:new Color('#00C8AF'), opacity: 1, map: texLoader$9.load(Potree.resourcePath+'/textures/whiteCircle.png' ), depthTest:false, }), }; } return markerMats$2[type] } setMarkerSelected(marker, state, hoverObject){ //console.warn(marker.id , state, hoverObject) if(state == 'select'){ marker.selected = true; marker.material = this.getMarkerMaterial('select'); }else if(state == 'unselect'){ marker.selected = false; marker.material = this.getMarkerMaterial('default'); }else { if(marker.selected)return //选中时不允许修改为除了'unselect'以外的状态 if(state == 'hover'){ marker.material = this.getMarkerMaterial('hover'); }else if(state == 'unhover'){ if(marker.name.includes('mid')){ marker.material = this.getMarkerMaterial('midPrepare'); }else { marker.material = this.getMarkerMaterial('default'); } } } } select(){ //最多再显示一层子级的线,如building不会显示room中的hole的线 //box是一直显示的,但会切换材质 /* 选中 box 线 building 自己(底盘)选中 自己, floor不带hole floor 自己选中 自己, room不带hole room 自己选中 自己 */ //注:自己的就代表定包括hole,如果有parentHoles的也(building上的hole的对应) //console.log('select '+this.name, this.selected) if(this.selected)return if(this.box){ this.box.material = this.mats.boxSelected; } if(this.buildType == 'building'|| this.buildType == 'floor'){ this.buildChildren.forEach(e=>{ e.lineMesh.visible = true; }); if(this.buildType == 'floor'){ Potree.Utils.setObjectLayers(this.box, 'bothMapAndScene' ); Potree.Utils.setObjectLayers(this.buildParent.box, 'siteModelMapUnvisi' ); //当选中floor或room时,building在地图不可见 } }else if(this.buildType == 'room'){ Potree.Utils.setObjectLayers(this.buildParent.box, 'bothMapAndScene' ); Potree.Utils.setObjectLayers(this.buildParent.buildParent.box, 'siteModelMapUnvisi' ); } this.lineMesh.visible = true; this.markers && this.markers.forEach(e=>Potree.Utils.updateVisible(e,'select',true) ); this.midMarkers && this.midMarkers.forEach(e=>Potree.Utils.updateVisible(e,'select',true) ); let holes = this.holes.concat(this.parentHoles); holes.forEach(e=>e.select()); this.selected = true; this.dispatchEvent({type:'select'}); } unselect(){ if(!this.selected)return //console.log('unselect '+this.name ) if(this.box){ this.box.material = this.mats.boxDefault; } if(this.buildType == 'building' || this.buildType == 'floor'){ this.buildChildren.forEach(e=>{ //(这里要保证选中前要先取消选中,否则如选中房间后取消了楼层,房间线就隐藏了) e.lineMesh.visible = false; }); if(this.buildType == 'floor'){ Potree.Utils.setObjectLayers(this.box, 'siteModelMapUnvisi' ); Potree.Utils.setObjectLayers(this.buildParent.box, 'bothMapAndScene' ); } }else if(this.buildType == 'room'){ Potree.Utils.setObjectLayers(this.buildParent.box, 'siteModelMapUnvisi' ); Potree.Utils.setObjectLayers(this.buildParent.buildParent.box, 'bothMapAndScene' ); } this.lineMesh.visible = false; this.markers && this.markers.forEach(e=>Potree.Utils.updateVisible(e,'select',false) ); this.midMarkers && this.midMarkers.forEach(e=>Potree.Utils.updateVisible(e,'select',false) ); let holes = this.holes.concat(this.parentHoles); holes.forEach(e=>e.unselect()); this.selected = false; this.dispatchEvent({type:'unselect'}); } ifContainsPoint(position){//看它所定义的空间是否包含某个坐标(要排除hole) let {zMin , zMax} = this.getRealZ(); if(position.z < zMin || position.z > zMax ) return let holes = this.holes.concat(this.parentHoles); let holesPoints = holes.filter(e=>e!=this && e.points.length>2).map(e=>e.points); let inShape = math.isPointInArea(this.points, holesPoints, position); return !!inShape } } class MetricUVGenerator{ constructor(){ this.a = new Vector3, this.b = new Vector3, this.c = new Vector3, this.d = new Vector3; } generateTopUV(t, e, n, r, o) { return [new Vector2(e[3 * n],e[3 * n + 1]), new Vector2(e[3 * r],e[3 * r + 1]), new Vector2(e[3 * o],e[3 * o + 1])] } generateSideWallUV(t, e, n, r, o, a) { var s = e; this.a.set(s[3 * n], s[3 * n + 1], s[3 * n + 2]), this.b.set(s[3 * r], s[3 * r + 1], s[3 * r + 2]), this.c.set(s[3 * o], s[3 * o + 1], s[3 * o + 2]), this.d.set(s[3 * a], s[3 * a + 1], s[3 * a + 2]); var c = this.a.x !== this.b.x , l = c ? this.b : this.d , u = this.a.distanceTo(l) , d = l.distanceTo(this.c); return [new Vector2(this.a.x,0), c ? new Vector2(this.a.x + u,0) : new Vector2(this.a.x,d), new Vector2(this.a.x + u,d), c ? new Vector2(this.a.x,d) : new Vector2(this.a.x + u,0)] } } const minFloorHeight = 0.5; const ifDrawDatasetBound = true; //显示一下数据集的tightBound线框 const minMarkers = 3; const Limit = {zMin:-config$1.map.cameraHeight, zMax:config$1.map.cameraHeight,}; //不能超过camera的高度,为了对称所以也限制了最低 var SiteModel = { bus: new EventDispatcher(), entities:[], //所有实体 buildings:[], //所有建筑父集 meshGroup: new Object3D, inEntity : null, lastPos: new Vector3(Infinity,Infinity,Infinity), init: function(){ viewer.scene.scene.add(this.meshGroup); this.meshGroup.name = 'siteModel'; this.SplitScreen = SplitScreen4Views; if(Potree.settings.editType == 'pano'){ return } this.createHeightPull(); this.history = new History({ applyData: (data)=>{ if(data.entity.parent && data.entity.selected ){ data = Potree.Common.CloneObject(data); //避免使用后更改数据又被使用 data.entity.reDraw(); data.entity.initData(data); data.isNew || data.entity.addMidMarkers(); data.entity.isNew = data.isNew; data.entity.dispatchEvent('changeByHistory'); return true } }, getData:(entity)=>{ //if(entity.isNew)return return { entity, points: entity.points.map(e=>e.clone()), ifDraw: true, isNew : entity.isNew, } } }); if(Potree.settings.isTest && ifDrawDatasetBound){ viewer.addEventListener('allLoaded',()=>{ viewer.scene.pointclouds.forEach(pointcloud=>{ let boxPoints = pointcloud.getUnrotBoundPoint(); let boundingBox = new BuildingBox({ name: '数据集tightBound_'+pointcloud.dataset_id, points: boxPoints, buildType : 'dataset', zMax: pointcloud.bound.max.z, zMin: pointcloud.bound.min.z, ifDraw:true }); this.meshGroup.add(boundingBox); //boundingBox.markers.forEach(e=>e.visible = false) }); }); } if(Potree.settings.isOfficial){ viewer.addEventListener('camera_changed', e => { if(e.changeInfo && e.changeInfo.positionChanged){ this.updateEntityAt(); } }); } { let pressDelete = (e)=>{ if(e.keyCode == KeyCodes.BACKSPACE || e.keyCode == KeyCodes.DELETE){ if(this.selectedMarker){ let entity = this.selectedMarker.parent; let index = entity.markers.indexOf(this.selectedMarker); entity.removeMarker(index); if(entity.points.length<2){//删到只剩一个点时重新画(如果是hole的点,直接删除hole吧?) this.startInsertion('resume',entity); } } } }; viewer.inputHandler.addEventListener('keydown', pressDelete); } { let updated = false; this.bus.addEventListener('updated',()=>{ Common.intervalTool.isWaiting('siteModelUpdated', ()=>{ this.changedCallback(); },500); }); } }, changedCallback(){ this.findPanos(); this.findEntityForDataset(); this.updateEntityAt(true); //更新所在建筑,更新marker显示 //console.log('changedCallback') }, updateEntityAt(force,{measure}={}){ //if(!this.entities.length || this.editing) return //编辑时也要根据位置显示不同楼层的漫游点与cad let fun = ()=>{ //延时update,防止卡顿 let currPos = viewer.mainViewport.view.position; if(this.pauseUpdateEntity)return // console.error('pauseUpdateEntity') //if(force || !currPos.equals(this.lastPos) ){ //console.log('currPos ', currPos.toArray()) this.lastPos.copy(currPos); let entity; if(measure){//因为测量线的点可能在多个房间,所以干脆直接找在哪个楼层 let list = []; measure.points.forEach(e=>{ let entity = this.pointInWhichEntity(e, 'floor' );//每个点都找到其所在楼层 let item = list.find(e=>e.entity == entity); if(item){ item.count ++; }else { list.push({entity, count:1}); } }); list.sort((a,b)=>{return b.count-a.count});//找出出现次数最多的楼层 if(list[0]){ entity = list[0].entity; } }else { let searchPos = Potree.settings.displayMode == 'showPanos' ? viewer.images360.currentPano : currPos; entity = this.pointInWhichEntity(searchPos, 'room'); } if(force || this.inEntity != entity ){ let oldEntity = this.inEntity; this.inEntity = entity; //console.log('buildingChange', entity) this.bus.dispatchEvent({type:'buildingChange',entity}); //this.updatePanosVisible(oldEntity, this.inEntity) let lastFloor = this.currentFloor; //oldEntity ? oldEntity.buildType == 'floor' ? oldEntity : oldEntity.buildType == 'room' ? oldEntity.buildParent : null : null; //基本只会是floor或room let currentFloor = entity ? entity.buildType == 'floor' ? entity : entity.buildType == 'room' ? entity.buildParent : null : null; //基本只会是floor或room if(force || currentFloor != lastFloor){ //console.log('改变了floor',lastFloor,currentFloor) this.currentFloor = currentFloor; this.bus.dispatchEvent({type:'FloorChange',currentFloor}); } } force = false; //} }; if(force)fun(); else Common.intervalTool.isWaiting('sitemodelCameraInterval', fun , 500); }, enter:function(){ Potree.Log('sitemodel enter'); this.clear(); //确保全部清空 this.editing = true; //this.updatePanosVisible(null, null, true)//show all viewer.updateFpVisiDatasets(); let mapViewport = viewer.mapViewer.viewports[0]; this.SplitScreen.split({siteModel:true/* , viewports:[{name:'Top',viewport : mapViewport }] */}); viewer.viewports.forEach(e=>{ if(e.name != 'mapViewport'){ e.layersAdd('siteModelMapUnvisi'); } if(e.name == 'right' || e.name == 'back'){ e.layersAdd('siteModeSideVisi'); } }); viewer.images360.panos.forEach(pano=>{ Potree.Utils.setObjectLayers(pano.marker, 'siteModelMapUnvisi' ); //Potree.Utils.setObjectLayers(pano.label2, 'bothMapAndScene') }); mapViewport.layersAdd('siteModeOnlyMapVisi'); //只有mapViewport能看到marker }, leave:function(){ Potree.Log('sitemodel leave'); let mapViewport = viewer.mapViewer.viewports[0]; this.SplitScreen.recover(); viewer.viewports.forEach(e=>{ if(e.name != 'mapViewport'){ e.layersRemove('siteModelMapUnvisi'); } if(e.name == 'right' || e.name == 'back'){ e.layersRemove('siteModeSideVisi'); } }); viewer.images360.panos.forEach(pano=>{ Potree.Utils.setObjectLayers(pano.marker, 'sceneObjects' ); //Potree.Utils.setObjectLayers(pano.label2, 'bothMapAndScene') }); mapViewport.layersRemove('siteModeOnlyMapVisi'); this.clear(); this.editing = false; this.updateEntityAt(true); viewer.updateFpVisiDatasets(); } , addFloor:function(parent, dirType, sid, name){//dirType:'top'|'bottom'在上方建还是下方。如果建筑中没有楼层,默认在基底建一个 let buildType = 'floor'; let zMin, zMax; if(parent.buildChildren.length == 0){ zMin = parent.zMin; zMax = zMin + Potree.config.siteModel.floorHeightDefault; }else { if(dirType == 'bottom'){ //var btm = Common.find(parent.buildChildren,null,[e=>e.zMin]) var btm = parent.buildChildren[0]; zMax = btm.zMin; zMin = zMax - Potree.config.siteModel.floorHeightDefault; }else { //var top = Common.find(parent.buildChildren,null,[e=>e.zMax]) var top = parent.buildChildren[parent.buildChildren.length - 1]; zMin = top.zMax; zMax = zMin + Potree.config.siteModel.floorHeightDefault; } } let prop = { buildType, //name : Potree.config.siteModel.names[buildType], zMin, zMax, buildParent:parent, sid, name, ifDraw:true }; var floor = new BuildingBox(prop); /* parent.buildChildren.push(floor) this.meshGroup.add(floor); this.entities.push(floor) */ floor.update(); this.addEntity(floor,parent); //this.selectEntity(floor) if(this.selected == parent){//重新选择下,为了显示新楼层线框 parent.unselect(); parent.select(); } this.bus.dispatchEvent('updated'); return floor }, startInsertion:function(buildType, parent, sid, name, callback, cancelFun){ let zMin, zMax, entity, resume; let mapViewport = viewer.mapViewer.viewports[0]; if(buildType == 'resume'){//继续画(使用最后一个点或者新加的点) resume = true; entity = parent; buildType = parent.buildType; //删除原先所有的点,因为它们已经添加了事件,会很麻烦: entity.reDraw(0); entity.isNew = true; //当作新的来画 } if(!resume){ if(buildType == 'hole' || buildType == 'room'){ zMin = parent.zMin; zMax = parent.zMax; }else if(buildType == 'building'){ parent = null; zMin = viewer.bound.boundingBox.min.z; zMax = viewer.bound.boundingBox.min.z; } if(buildType == 'hole'){ entity = parent.addHole(); entity.isNew = true; this.selectEntity(parent); entity.select(); console.log('挖洞 ',entity.uuid); }else { let prop = { buildType, //name : Potree.config.siteModel.names[buildType],//'building', zMin, zMax, buildParent:parent, sid, name, ifDraw:true }; entity = new BuildingBox(prop); entity.isNew = true; this.selectEntity(entity); } this.addEntity(entity, parent); } let timer; let endDragFun = (e) => { if (e.button == MOUSE.LEFT ) { this.history.beforeChange(entity); var marker = entity.addMarker({point:entity.points[entity.points.length - 1].clone()}); this.history.afterChange(entity); //entity.editStateChange(true) //重新激活reticule状态 entity.continueDrag(marker, e); } else if (e.button === MOUSE.RIGHT ) { if(e.pressDistance < Potree.config.clickMaxDragDis )end(e);//非拖拽的话 else entity.continueDrag(null, e/* .drag.object */); } }; let finish = ()=>{//结束绘画 viewer.removeEventListener('cancel_insertions', Exit); entity.removeEventListener('unselect', Exit); clearTimeout(timer); entity.editStateChange(false); //pressExit && viewer.inputHandler.removeEventListener('keydown', pressExit); callback && callback(entity); viewer.dispatchEvent('endBuildEntity'); }; let end = (e={}) => {//尝试结束 /* 退出的三种形式: 1 普通:如果大于三个marker,结束且保留;否则重新画。() 2 删除:直接结束且删除。(remove) 3 结束:如果大于三个marker,结束且保留;否则结束且删除。 (finish) 4 保留:无论几个marker,都保留着,结束。(remain) */ if(e.remove){ finish(); return this.removeEntity(entity) } if(!e.remain && !e.finish && !e.remove && entity.markers.length<=minMarkers){//右键 当个数不够时取消 //重新开始画 entity.reDraw(1); Potree.Utils.updateVisible(entity.markers[0],'unMove',false); var f = ()=>{ Potree.Utils.updateVisible(entity.markers[0],'unMove',true); entity.removeEventListener('dragChange',f); }; entity.addEventListener('dragChange',f); //console.log('waitcontinue') entity.continueDrag(entity.markers[0], e); return } finish(); if (e.remain || !e.remove && entity.markers.length > 3) {//保留 entity.removeMarker(entity.points.length - 1); entity.markers.forEach(marker=>{marker.dispatchEvent('addHoverEvent'); }); if(buildType == 'room'){ this.fitPullBox(); } entity.isNew = false; entity.addMidMarkers(); let boundingBox = entity.getBound(); let center = boundingBox.getCenter(new Vector3()); viewer.controls.setTarget(center,2); }else { this.removeEntity(entity); //直接删除没画好的,比较简单。这样就不用担心旧的continueDrag仍旧触发了 } return entity }; let Exit = (e)=>{ //强制结束 entity.removeEventListener('unselect', Exit); if(viewer.inputHandler.drag){//还未触发drop的话 viewer.inputHandler.drag.object.dispatchEvent({ type: 'drop', drag: viewer.inputHandler.drag, viewer: viewer, pressDistance:0, button : MOUSE.RIGHT }); viewer.inputHandler.drag.object.isDragging = false; }else { end({remain:true}); } viewer.inputHandler.drag = null; }; viewer.dispatchEvent( 'cancel_insertions' );//取消之前的 viewer.addEventListener('cancel_insertions', Exit); setTimeout(()=>{ entity.isNew && entity.addEventListener('unselect', Exit); },1);//因为刚创建会unselect一下所以延迟 var marker = entity.addMarker({point:new Vector3(0, 0, 0)}); Potree.Utils.updateVisible(marker,'unMove',false);//这时候的位置是假的(0,0,0)所以先不可见 var f = ()=>{ Potree.Utils.updateVisible(marker,'unMove',true); entity.removeEventListener('dragChange',f); }; entity.addEventListener('dragChange',f); marker.isDragging = true; viewer.inputHandler.startDragging(marker , {dragViewport:mapViewport, endDragFun, notPressMouse:true} ); //notPressMouse代表不是通过按下鼠标来拖拽. dragViewport指定了只能在地图上拖拽 viewer.dispatchEvent('startBuildEntity'); let changeByHistory = (e)=>{ if(!entity.isNew)return let marker = entity.markers[entity.points.length-1]; viewer.inputHandler.startDragging(marker , {dragViewport:mapViewport, endDragFun, notPressMouse:true} ); var I = viewer.inputHandler.intersect && (viewer.inputHandler.intersect.orthoIntersect || viewer.inputHandler.intersect.location); if(I){ entity.dragChange(I.clone(), entity.points.length-1 ); //使最后一个点在鼠标处 } }; entity.addEventListener('changeByHistory',changeByHistory); return entity; }, getPreDealData(points, zMin, zMax, initial, buildType, parent){ /* if( buildType == 'building' ){ zMax = zMin //强制变得一样,作为基底。如果有必要,保存时再算真实的zMax。目前zMin没有保存所以数据是错的,会直接根据floor计算 } */ var bound = viewer.bound.boundingBox; if(buildType == 'building' && initial){//初始数据错的,要自己建(只有一个building和floor) 原posIsLonlat console.log('空间模型未编辑过, 初始化了一个'); points = [ new Vector3(bound.min.x, bound.min.y,0), new Vector3(bound.max.x, bound.min.y,0), new Vector3(bound.max.x, bound.max.y,0), new Vector3(bound.min.x, bound.max.y,0), ]; zMin = bound.min.z; zMax = bound.max.z; /* points = points.map(e=>{ return viewer.transform.lonlatToLocal.forward(e) }) */ }else {//相对于初始数据集的模型内坐标 points = points.map(e=> this.transform(e, 'fromDataset')); if(buildType == 'floor' && initial){ zMin = bound.min.z; zMax = bound.max.z; } } return {points, zMax, zMin } }, resetFromData:function(entity, points=[], holes=[], zMin, zMax ){ var {points, zMax, zMin} = this.getPreDealData(points, zMin, zMax , this.autoBuild , entity.buildType, entity.buildParent ); if(entity.buildType != 'floor' )entity.points = points; if(entity.buildType == 'room'){ entity.zMin = zMin; entity.zMax = zMax; }else if(entity.buildType == 'floor'){//改楼高 let height = zMax - zMin; let zMax2 = entity.zMin + height; SiteModel.changeZ(entity, 'zMax', zMax2); } { //删除旧的holes重新添加 let holesOld = entity.holes; holesOld.forEach(e=>{ entity.removeHole(e); }); holes.forEach(points =>{ let ps = points.map(e=> this.transform(e, 'fromDataset')); let hole = entity.addHole(ps); hole.addMidMarkers(); }); } entity.update(); return entity } , createFromData:function( buildType, parent ,sid, name, points=[], holes=[], zMin, zMax, initial,panos_,flagPano){ if(buildType != 'building' && buildType != 'floor' && buildType != 'room' ) return var {points, zMax, zMin} = this.getPreDealData(points, zMin, zMax , initial, buildType, parent ); let panos = []; { if(panos_){ panos_.forEach(sid=>{ let pano = viewer.images360.getPano(sid,'sid'); if(pano)panos.push(pano); }); } flagPano = flagPano != void 0 ? viewer.images360.getPano(flagPano,'sid') : null ; //最中心的pano 或者 最靠近该实体的pano(当panos为空时) if(!this.editing && buildType == 'floor' && !flagPano){//没有的话可能是自动添加的floor,直接用parent的吧 panos = parent.panos; flagPano = parent.flagPano; } } let prop = { buildType, points, name, sid, zMin, zMax, buildParent:parent, ifDraw:this.editing || Potree.settings.drawEntityData, panos, flagPano, autoBuild : initial }; let entity = new BuildingBox(prop); SiteModel.addEntity(entity, parent ); if(this.editing){ if(buildType == 'building'|| buildType == 'room'){ entity.addMidMarkers(); } } holes.forEach(points =>{ let ps = points.map(e=> this.transform(e, 'fromDataset')); let hole = entity.addHole(ps); this.editing && hole.addMidMarkers(); }); /* if(buildType == 'floor'){ this.updateBuildingZ(parent) } */ return entity }, transform:function(pos, type){ if(Potree.settings.editType == 'pano'){ // 模型不经转换 return new Vector3().copy(pos).setZ(0); } if(type == 'toDataset'){ let point = Potree.Utils.datasetPosTransform({ toDataset: true, position: pos.clone(), datasetId: Potree.settings.originDatasetId }); return new Vector2().copy(point) }else { let position = new Vector3().copy(pos).setZ(0); return Potree.Utils.datasetPosTransform({ fromDataset: true, position, datasetId: Potree.settings.originDatasetId }) } }, addEntity:function(entity, parent){ if(entity.buildType != 'hole'){ this.meshGroup.add(entity); this.entities.push(entity); if(entity.buildType == 'building'){ this.buildings.push(entity); }else { parent.buildChildren.push(entity); } } if(entity.buildType == 'room'){ entity.addEventListener('marker_dropped',()=>{ this.fitPullBox(); }); }else if(entity.buildType == 'floor'){ this.updateBuildingZ(parent); parent.dispatchEvent({type:'addFloor'}); } {//仅能存在一个marker被选中。选中的点可以被删除 entity.addEventListener('clickMarker', (e)=>{ if(this.selectedMarker == e.marker){ this.selectedMarker.dispatchEvent({type:'clickSelect',state:false}); this.selectedMarker = null; }else { if(this.selectedMarker){ this.selectedMarker.dispatchEvent({type:'clickSelect',state:false}); } this.selectedMarker = e.marker; this.selectedMarker.dispatchEvent({type:'clickSelect',state:true}); } }); entity.addEventListener('removeMarker', (e)=>{ if(this.selectedMarker == e.marker){ this.selectedMarker = null; } }); let unselect = (e)=>{//取消选中实体或删除后 if(this.selectedMarker && entity.markers.includes(this.selectedMarker)){ this.selectedMarker.dispatchEvent({type:'clickSelect',state:false}); this.selectedMarker = null; } }; entity.addEventListener('dispose', unselect); entity.addEventListener('unselect', unselect); } //console.log('添加实体:', entity.buildType, entity.sid, entity.uuid) this.bus.dispatchEvent('updated'); entity.addEventListener('startDragging',()=>{ this.history.beforeChange(entity); }); entity.addEventListener('marker_dropped',()=>{ this.bus.dispatchEvent('updated'); this.history.afterChange(entity); }); }, removeEntity : function(entity){ if(!this.entities.includes(entity))return console.log('删除实体:', entity.buildType, entity.sid); if(this.selected == entity){ this.height_pull_box && (this.height_pull_box.visible = false); this.selectEntity(null); } if(entity.buildType == 'building'){ var index = this.buildings.indexOf(entity); if(index>-1){ this.buildings.splice(index,1); } }else { var index = entity.buildParent.buildChildren.indexOf(entity); if(index>-1){ entity.buildParent.buildChildren.splice(index,1); if(entity.buildType == 'floor' && index == 0){ this.updateBuildingZ(entity.buildParent); } } } var index = this.entities.indexOf(entity); if(index>-1){ this.entities.splice(index,1); } entity.dispose(); let buildChildren = entity.buildChildren.slice(); buildChildren.forEach(e=>this.removeEntity(e)); this.bus.dispatchEvent('updated'); }, updateBuildingZ:function(building){ building.buildChildren = building.buildChildren.sort((e,a)=>e.zMin-a.zMin);//从低到高排序 building.buildChildren[0] && (building.zMin = building.zMax = building.buildChildren[0].zMin); //基底高度 //building.zMax = building.buildChildren[building.buildChildren.length-1].zMax if(this.editing) building.update({dontUpdateChildren:true}); building.dispatchEvent('updateBuildingZ'); }, selectEntity : function(entity, state=true){ viewer.dispatchEvent('content_changed'); if(state === false){ entity.unselect(); if(this.selected == entity)this.selected = null; viewer.controls.setTarget(null,2); return } if(this.selected == entity || entity && entity.buildType == 'hole')return //this.buildings.forEach(e=>e.unselect()) this.selected && this.selected.unselect(); this.height_pull_box && (this.height_pull_box.visible = false); if(entity){ entity.select(); if(!entity.isNew){ let boundingBox = entity.getBound(); let center = boundingBox.getCenter(new Vector3()); //中心点不一定在entity中,比如半环形建筑(所以要不要改成到漫游点呢) viewer.controls.setTarget(center,2); } }else { viewer.controls.setTarget(null,2); } this.selected = entity; if(entity && !entity.dontDragFloorHeight && (entity.buildType == 'floor' || entity.buildType == 'room' )){ this.height_pull_box && (this.height_pull_box.visible = true); this.fitPullBox(); } if(entity && !entity.isNew && (entity.buildType == 'building' || entity.buildType == 'room' ) && entity.points.length<2){ this.startInsertion('resume',entity); //继续画 } }, fitPullBox: function(){ //自适应拖拽楼层的pullMesh if(!this.height_pull_box || !this.selected || this.selected.buildType!= 'floor' && this.selected.buildType!= 'room')return let bound = new Box3(); bound.expandByObject(this.selected.box); let center = bound.getCenter(new Vector3() ); let size = bound.getSize(new Vector3() ); this.height_pull_box.scale.copy(size); this.height_pull_box.position.copy(center); }, changeZ:function(entity, dirType, value){ // floor or room 修改zMin or zMax let max, min; //limit if(entity.buildType == 'floor'){//楼层 let index = entity.buildParent.buildChildren.indexOf(entity); if(dirType == 'zMax'){ let upper = entity.buildParent.buildChildren[index+1]; entity.zMax = Math.min(Limit.zMax, value); min = entity.zMin + minFloorHeight; if(entity.zMax < min){ entity.zMax = min; }else { if(upper){ max = upper.zMax - minFloorHeight; if(entity.zMax > max){ entity.zMax = max; } } } if(upper){ upper.zMin = entity.zMax; upper.update(); upper.dispatchEvent({type:'changeHeight'}); } }else { let lower = entity.buildParent.buildChildren[index-1]; entity.zMin = Math.max(Limit.zMin, value); max = entity.zMax - minFloorHeight; if(entity.zMin > max){ entity.zMin = max; }else { if(lower){ min = lower.zMin + minFloorHeight; if(entity.zMin < min){ entity.zMin = min; } } } if(lower){ lower.zMax = entity.zMin; lower.update(); lower.dispatchEvent({type:'changeHeight'}); } if(index == 0)this.updateBuildingZ(entity.buildParent); } }else if(entity.buildType == 'room'){//房间 //按照navvis的是不一定限制在当前楼层,只要高度不超过当前楼层即可。 let maxHeight = entity.buildParent.zMax - entity.buildParent.zMin; if(dirType == 'zMax'){ min = entity.zMin + minFloorHeight; max = entity.zMin + maxHeight; entity.zMax = MathUtils.clamp(value, min, max); }else { min = entity.zMax - maxHeight; max = entity.zMax - minFloorHeight; entity.zMin = MathUtils.clamp(value, min, max); } } entity.update(); entity.dispatchEvent({type:'changeHeight'}); //this.selected.emit('update') this.fitPullBox(); this.bus.dispatchEvent('updated'); }, createHeightPull:function(){ //拖拽楼层的bounding box let boxGeo = new BoxBufferGeometry( 1, 1, 1/4 ); let boxMat = new MeshBasicMaterial({ color:"#F00", opacity:0, transparent:true, depthTest:false, side:2 }); let height_pull_box_up = new Mesh(boxGeo,boxMat); let height_pull_box_down = new Mesh(boxGeo,boxMat); height_pull_box_up.name = 'height_pull_box_up'; height_pull_box_down.name = 'height_pull_box_down'; this.height_pull_box = new Object3D(); this.height_pull_box.name = 'height_pull_box'; this.height_pull_box.add(height_pull_box_up); this.height_pull_box.add(height_pull_box_down); this.height_pull_box.visible = false; this.meshGroup.add(this.height_pull_box); height_pull_box_up.position.set(0,0,1/2/* 3/8 */); height_pull_box_down.position.set(0,0,-1/2/* -3/8 */); Potree.Utils.setObjectLayers(this.height_pull_box, 'siteModeSideVisi' ); let mouseover = (e)=>{ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"siteModelFloorDrag" }); }; let mouseleave = (e)=>{ viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"siteModelFloorDrag" }); }; let firstZ, firstIntersect; let drag = (e)=>{ var intersect = e.intersect.orthoIntersect; //不要点云的intersect,只要orthocamera算出的平面intersect if(firstIntersect != void 0){ let moveZ = intersect.z - firstIntersect; if(this.selected.buildType == 'floor'){//楼层 //限制高度不能超过上下 if(e.target == height_pull_box_up){ if(firstZ == void 0)firstZ = this.selected.zMax; this.changeZ(this.selected, 'zMax', firstZ + moveZ); }else { if(firstZ == void 0)firstZ = this.selected.zMin; this.changeZ(this.selected, 'zMin', firstZ + moveZ); } }else if(this.selected.buildType == 'room'){//房屋 if(e.target == height_pull_box_up){ if(firstZ == void 0)firstZ = this.selected.zMax; this.changeZ(this.selected, 'zMax', firstZ + moveZ); }else { if(firstZ == void 0)firstZ = this.selected.zMin; this.changeZ(this.selected, 'zMin', firstZ + moveZ); } } }else { firstIntersect = intersect.z; } }; let drop = (e)=>{ firstZ = firstIntersect = null; }; height_pull_box_up.addEventListener('mousemove',mouseover); height_pull_box_down.addEventListener('mousemove',mouseover); height_pull_box_up.addEventListener('mouseleave',mouseleave); height_pull_box_down.addEventListener('mouseleave',mouseleave); height_pull_box_up.addEventListener('drag',drag); height_pull_box_down.addEventListener('drag',drag); height_pull_box_up.addEventListener('drop',drop); height_pull_box_down.addEventListener('drop',drop); }, pointInWhichEntity(location, buildType, ifIgnoreHole){//buildType是要找的建筑类型 //location 可以是pano或者坐标 //由于房间可能在building外,所以房间要另外单独识别。 let lastResult; //最接近的上一层结果,如果没有result返回这个 let result; let level = { building: 0, floor: 1, room: 2 }; let traverse = (parent, buildType)=>{//返回第一个符合标准的实体 let contains; if(location instanceof Vector3){ contains = parent.ifContainsPoint(location); }else {//is pano contains = parent.panos.includes(location); } if(contains){ if(!lastResult || level[lastResult.buildType] < level[parent.buildType] )lastResult = parent; if(parent.buildType == buildType){ return parent }else { for(let i=0,len=parent.buildChildren.length; i{ return traverse(building, 'building') //在building中 }], [(building)=>{ //写法类似pointInWhichPointcloud let boundingBox = building.getBound(); let center = boundingBox.getCenter(new Vector3()); let position = location instanceof Vector3 ? location : location.position; let dis = position.distanceTo(center); let size = boundingBox.getSize(new Vector3()); let length = size.length() / 2; return length / dis }]); let building = result && result[0] && result[0].score > 1 && result[0].item; if(buildType == 'building' || !building)return building result = traverse(building, buildType); /* if(!result && buildType == 'room'){//如果要找的是room, 且按刚才的顺序找不到的话,就单独从所有rooms中找一遍。因为room可能不在floor和building内。 let rooms = this.entities.filter(e=>e.buildType == 'room'); result = rooms.find(e=>e.ifContainsPoint(position)) }*/ //虽然房间可以画到上级之外,但是为了方便起见,假定房间绝对在楼层之内。找不到的话要调整空间模型了。 return result || lastResult } , findPanos: function(){ { this.entities.forEach(entity=>{ //清空: entity.panos = []; entity.flagPano = null; viewer.images360.panos.forEach(pano=>{ if(entity.ifContainsPoint(pano.position)){ entity.panos.push(pano); } }); }); } /* viewer.images360.panos.forEach(pano=>{ //一个漫游点只对应一个实体的话 let result = this.pointInWhichEntity(pano.position, 'room'); {//get panos for every entities let entity = result while(entity){ entity.panos.push(pano); entity = entity.buildParent } } }) */ {//search center pano this.entities.forEach(entity=>{ let panos = entity.panos; if(panos.length == 0)return let bound = entity.getBound(); let center = bound.getCenter(new Vector3); let request = []; let rank = [ Images360.scoreFunctions.distanceSquared({position: center}) ]; //let panos = entity.panos && entity.panos.length ? entity.panos : viewer.images360.panos //entity没有panos的话,就扩大到所有panos let r = Common.sortByScore(panos, request, rank); if(r && r.length){ entity.flagPano = r[0].item; }else { console.error('no flagPano??'); } }); } } , findEntityForDataset:function(){//为每一个数据集寻找它所属的最小实体 /* var entities = this.entities.filter(e=>e.buildType == 'room' || e.buildType == 'floor' && e.buildChildren.length == 0) viewer.scene.pointclouds.forEach(pointcloud=>{ let cloudVolume = pointcloud.getVolume() let scores = [] entities.forEach(entity=>{ let volume = entity.intersectPointcloudVolume(pointcloud) //注:默认已经findPanos过 let panos = entity.panos.filter(e=>pointcloud.panos.includes(e)); let panoCount = panos.length let score = volume / cloudVolume + panoCount / pointcloud.panos.length scores.push({entity, volume, panoCount, score}) }) scores.sort((a,b)=>{ return b.score-a.score }) if(scores.length == 0 || scores[0].volume/cloudVolume < 0.0001 && scores[0].volume < 3 ){//如果约等于0 pointcloud.belongToEntity = null }else{ pointcloud.belongToEntity = scores[0].entity; } }) */ let getScores = (pointcloud, entities, cloudVolume)=>{ let scores = []; entities.forEach(entity=>{ let volume = entity.intersectPointcloudVolume(pointcloud); //注:默认已经findPanos过 let panos = entity.panos.filter(e=>pointcloud.panos.includes(e)); let panoCount = panos.length; let score = volume / cloudVolume; if(pointcloud.panos.length > 0){ score += panoCount / pointcloud.panos.length; }else { //score += 1 } scores.push({entity, volume, panoCount, score}); }); scores.sort((a,b)=>{ return b.score-a.score }); return scores }; viewer.scene.pointclouds.forEach(pointcloud=>{ //先判断父级,如果父集不通过就不判断子级。 let cloudVolume = pointcloud.getVolume(); let entities = this.buildings; while(1){ let scores = getScores(pointcloud, entities, cloudVolume); if(scores.length == 0 || scores[0].volume/cloudVolume < 0.0001 && scores[0].volume < 3 ){//如果约等于0 if(scores[0] && scores[0].entity.buildType == 'room'){ pointcloud.belongToEntity = scores[0].entity; //当该数据集在楼层中,但不在楼层中的任意一个房间时,任意挂载在其中一个房间上。 break } pointcloud.belongToEntity = null; break; }else { entities = scores[0].entity.buildChildren; if(entities.length == 0){ pointcloud.belongToEntity = scores[0].entity; break; } } } }); /* 旧版: 只需要考虑 floor 和 room, 因为building的只有一个基底没高度 floor 和 room 在空间中没有完全的从属关系,因为room可以超出floor之外。所以直接混在一起来查找,但要排除有房间的楼层。 (现在改为层层递进查找,否则数据集包含entity多的,会直接挂载到体积最大的房间里,即使看起来主体点云并不在该房间) 有的数据集虽然很高,但只有近地面的部分才是主体,这部分一般含有全部漫游点。为了防止上层的实体因体积较大而分数高,就把包含漫游点的个数也加入考虑。 重叠体积大、且包含漫游点最多的最小实体将会拥有该点云。 期望: 最好不挂载到最小子级,因为现在有房间都到房间里了。 */ } , clear:function(){//清空 /* entities:[], //所有实体 buildings:[], //所有建筑父集 meshGroup: new THREE.Object3D, */ this.selectEntity(null); let length = this.entities.length; for(let i=0;i e.sid == id); let aimPano; if (!entity) { return console.error('没找到entity ') } if(Potree.settings.displayMode == 'showPanos'){ if (isNearBy && entity.panos.length) { if(entity.panos.includes(viewer.images360.currentPano)) return 'posNoChange' //已在当前实体中 let position = viewer.scene.getActiveCamera().position; let request = []; let rank = [Images360.scoreFunctions.distanceSquared({ position })]; let r = Common.sortByScore(entity.panos, request, rank); aimPano = r[0].item; } else { if (!entity.flagPano) { return console.log('没有flagPano') } aimPano = entity.flagPano; } if(aimPano == viewer.images360.currentPano) return 'posNoChange' viewer.images360.flyToPano(aimPano); }else { if(isNearBy && entity.ifContainsPoint(viewer.images360.position) ){ return 'posNoChange' //已在当前实体中 } let boundingBox = entity.getBound(); let position = boundingBox.getCenter(new Vector3()); //中心点不一定在entity中,比如半环形建筑(所以要不要改成到漫游点呢) let size = boundingBox.getSize(new Vector3()); if(viewer.modules.Clip && viewer.modules.Clip.editing){ viewer.modules.Clip.bus.dispatchEvent({type:'flyToPos', position}); }else { if(math.closeTo(position, viewer.images360.position)) return 'posNoChange' viewer.scene.view.setView({position, duration}); } viewer.mapViewer.moveTo(position, size, duration); } return true }, focusEntity(id){ var entity = this.entities.find(e => e.sid == id); let boundingBox = entity.getBound(); //let boundSize = boundingBox.getSize(new THREE.Vector3()) let center = boundingBox.getCenter(new Vector3()); this.SplitScreen.focusOnObject(boundingBox, center); this.gotoEntity(id, false, 0); }, removeIlligalArchi(){//删除marker数量小于3个的建筑,当保存时 let needDelete = []; this.entities.forEach(e=>{ if(e.points.length<3){ needDelete.push(e); } }); needDelete.forEach(e=>this.removeEntity(e)); }, }; // Author: Fyrestar https://mevedia.com (https://github.com/Fyrestar/THREE.InfiniteGridHelper) class InfiniteGridHelper extends Mesh{ constructor(size1, size2, color, distance, opacity1=0.2, opacity2=1){ color = color || new Color('white'); size1 = size1 || 10; size2 = size2 || 100; distance = distance || 8000; //可视距离?越远越模糊 const geometry = new PlaneBufferGeometry(2, 2, 1, 1); const material = new ShaderMaterial({ side: DoubleSide, uniforms: { uSize1: { value: size1 }, uSize2: { value: size2 }, opacity1:{//线条1的 小格子 value: opacity1 }, opacity2:{//线条2的 value: opacity2 }, uColor: { value: color }, uDistance: { value: distance } }, transparent: true, vertexShader: ` varying vec3 worldPosition; uniform float uDistance; void main() { vec3 pos = position.xyz * uDistance; pos.xy += cameraPosition.xy; worldPosition = pos; gl_Position = projectionMatrix * modelViewMatrix * vec4(pos, 1.0); } `, fragmentShader: ` varying vec3 worldPosition; uniform float uSize1; uniform float uSize2; uniform float opacity1; uniform float opacity2; uniform vec3 uColor; uniform float uDistance; float getGrid(float size) { vec2 r = worldPosition.xy / size; vec2 grid = abs(fract(r - 0.5) - 0.5) / fwidth(r); float line = min(grid.x, grid.y); return 1.0 - min(line, 1.0); } //为何侧面看不到线,因为mesh的正侧面都看不到? void main() { float d = 1.0 - min(distance(cameraPosition.xy, worldPosition.xy) / uDistance, 1.0); float g1 = getGrid(uSize1); float g2 = getGrid(uSize2); gl_FragColor = vec4(uColor.rgb, mix(g2, g1, g1) * pow(d, 3.0)); //gl_FragColor.a = mix(0.5 * gl_FragColor.a, gl_FragColor.a, g2); gl_FragColor.a = mix(opacity1 * gl_FragColor.a, opacity2 * gl_FragColor.a, g2); if ( gl_FragColor.a <= 0.0 ) discard; } `, extensions: { derivatives: true } }); super(geometry, material); this.frustumCulled = false; } }; /* THREE.InfiniteGridHelper.prototype = { ...THREE.Mesh.prototype, ...THREE.Object3D.prototype, ...THREE.EventDispatcher.prototype }; */ var TransformControls = function ( camera, domElement, options ) { if ( domElement === undefined ) { console.warn( 'THREE.TransformControls: The second parameter "domElement" is now mandatory.' ); domElement = document; } Object3D.call( this ); this.visible = false; this.domElement = domElement; var _gizmo = new TransformControlsGizmo(options); this.add( _gizmo ); this._gizmo = _gizmo; var _plane = new TransformControlsPlane(options); this.add( _plane ); /* this.linesAssistance = new TransformControlsLines(options); //水平 垂直 辅助 this.add( this.linesAssistance ); */ var scope = this; //this.player = options.player;//xzw add this.options = options;//xzw add // Define properties with getters/setter // Setting the defined property will automatically trigger change event // Defined properties are passed down to gizmo and plane defineProperty( "camera", camera ); defineProperty( "object", undefined ); defineProperty( "enabled", true ); defineProperty( "axis", null ); defineProperty( "mode", "translate" ); defineProperty( "translationSnap", null ); defineProperty( "rotationSnap", null ); defineProperty( "scaleSnap", null ); defineProperty( "space", "world" ); defineProperty( "size", 1 ); defineProperty( "dragging", false ); defineProperty( "showX", true ); defineProperty( "showY", true ); defineProperty( "showZ", true ); var changeEvent = { type: "change" }; var mouseDownEvent = { type: "mouseDown" }; var mouseUpEvent = { type: "mouseUp", mode: scope.mode }; var objectChangeEvent = { type: "objectChange" }; // Reusable utility variables var ray = new Raycaster(); var _tempVector = new Vector3(); var _tempVector2 = new Vector3(); var _tempQuaternion = new Quaternion(); var _unit = { X: new Vector3( 1, 0, 0 ), Y: new Vector3( 0, 1, 0 ), Z: new Vector3( 0, 0, 1 ) }; var pointStart = new Vector3(); var pointEnd = new Vector3(); var offset = new Vector3(); var rotationAxis = new Vector3(); var startNorm = new Vector3(); var endNorm = new Vector3(); var rotationAngle = 0; var cameraPosition = new Vector3(); var cameraQuaternion = new Quaternion(); var cameraScale = new Vector3(); var parentPosition = new Vector3(); var parentQuaternion = new Quaternion(); var parentQuaternionInv = new Quaternion(); var parentScale = new Vector3(); var worldPositionStart = new Vector3(); var worldQuaternionStart = new Quaternion(); var worldScaleStart = new Vector3(); var worldPosition = new Vector3(); var worldQuaternion = new Quaternion(); var worldQuaternionInv = new Quaternion(); var worldScale = new Vector3(); var eye = new Vector3(); var positionStart = new Vector3(); var quaternionStart = new Quaternion(); var scaleStart = new Vector3(); // TODO: remove properties unused in plane and gizmo defineProperty( "worldPosition", worldPosition ); defineProperty( "worldPositionStart", worldPositionStart ); defineProperty( "worldQuaternion", worldQuaternion ); defineProperty( "worldQuaternionStart", worldQuaternionStart ); defineProperty( "cameraPosition", cameraPosition ); defineProperty( "cameraQuaternion", cameraQuaternion ); defineProperty( "pointStart", pointStart ); defineProperty( "pointEnd", pointEnd ); defineProperty( "rotationAxis", rotationAxis ); defineProperty( "rotationAngle", rotationAngle ); defineProperty( "eye", eye ); { domElement.addEventListener( "mousedown", onPointerDown, false ); domElement.addEventListener( "touchstart", onPointerDown, false ); domElement.addEventListener( "mousemove", onPointerHover, false ); domElement.addEventListener( "touchmove", onPointerHover, false ); domElement.addEventListener( "touchmove", onPointerMove, false ); document.addEventListener( "mouseup", onPointerUp, false ); domElement.addEventListener( "touchend", onPointerUp, false ); domElement.addEventListener( "touchcancel", onPointerUp, false ); domElement.addEventListener( "touchleave", onPointerUp, false ); let drag = ()=>{ if(this.dragging){ this.pointerMove(); return {stopContinue:true} } }; this.addEventListener('dragging',drag,{importance:10}); viewer.addEventListener('global_drag',drag,{importance:10}); } this.setRotateMethod = function(number){//add 注意为2时 旋转期间不能改变位置 space可能不能为local ._gizmo.hideAxis = { rotate:[这里必须包含'e' ] } this.rotateMethod = number; }; this.dispose = function () { domElement.removeEventListener( "mousedown", onPointerDown ); domElement.removeEventListener( "touchstart", onPointerDown ); domElement.removeEventListener( "mousemove", onPointerHover ); domElement.removeEventListener( "mousemove", onPointerMove ); domElement.removeEventListener( "touchmove", onPointerHover ); domElement.removeEventListener( "touchmove", onPointerMove ); document.removeEventListener( "mouseup", onPointerUp ); domElement.removeEventListener( "touchend", onPointerUp ); domElement.removeEventListener( "touchcancel", onPointerUp ); domElement.removeEventListener( "touchleave", onPointerUp ); this.traverse( function ( child ) { if ( child.geometry ) child.geometry.dispose(); if ( child.material ) child.material.dispose(); } ); }; // Set current object this.attach = function ( object ) { this.object = object; this.visible = true; //Config.keyCon = false;//add //this.linesAssistance.setVisible(true) viewer.dispatchEvent('content_changed'); return this; }; // Detatch from object this.detach = function () { this.object = undefined; this.visible = false; this.axis = null; //Config.keyCon = true;//add //this.linesAssistance.setVisible(false) viewer.dispatchEvent('content_changed'); return this; }; //this.space = 'local' /* this.hideAxis = function ( mode, axis=[] ) {//xzw add 设置不可见的axis var handles = []; handles = handles.concat( this._gizmo.picker[ mode ].children ); handles = handles.concat( this._gizmo.gizmo[ mode ].children ); handles = handles.concat( this._gizmo.helper[ mode ].children ); handles.forEach(e=>e.visible = false) axis.forEach(name=>{ handles.filter(handle =>!handle.name.includes(name)) }) handles.forEach(e=>e.visible = true) }; */ // Defined getter, setter and store for a property function defineProperty( propName, defaultValue ) { var propValue = defaultValue; Object.defineProperty( scope, propName, { get: function () { return propValue !== undefined ? propValue : defaultValue; }, set: function ( value ) { if ( propValue !== value ) { propValue = value; _plane[ propName ] = value; _gizmo[ propName ] = value; scope.dispatchEvent( { type: propName + "-changed", value: value } ); scope.dispatchEvent( changeEvent ); } } } ); scope[ propName ] = defaultValue; _plane[ propName ] = defaultValue; _gizmo[ propName ] = defaultValue; } // updateMatrixWorld updates key transformation variables this.updateMatrixWorld = function () { if(!this.visible)return//add if ( this.object !== undefined ) { this.object.updateMatrixWorld(); this.object.parent.matrixWorld.decompose( parentPosition, parentQuaternion, parentScale ); this.object.matrixWorld.decompose( worldPosition, worldQuaternion, worldScale ); //add if(this.object.boundingBox){ let boundingBox = this.object.boundingBox.clone().applyMatrix4(this.object.matrixWorld); boundingBox.getCenter(worldPosition); //bound中心 if(this.pivotOnBottom){ worldPosition.setZ(boundingBox.min.z); //中心点居于模型bound底部,因固定离地高度,旋转时旋转中心在地面上就不会变位置 } } parentQuaternionInv.copy( parentQuaternion ).invert(); worldQuaternionInv.copy( worldQuaternion ).invert(); } this.camera.updateMatrixWorld(); this.camera.matrixWorld.decompose( cameraPosition, cameraQuaternion, cameraScale ); if(this.camera.type == "OrthographicCamera"){//xzw add eye.copy( this.view.direction); }else { eye.copy( cameraPosition ).sub( worldPosition ).normalize(); } Object3D.prototype.updateMatrixWorld.call( this ); }; this.pointerHover = function () { let pointer = viewer.inputHandler.pointer; if ( this.object === undefined)return //if(this.dragging === true /* || ( pointer.button !== undefined && pointer.button !== 0 ) */) return; if(!this.dragging){ let oldAxis = this.axis; //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect let {origin, direction} = viewer.inputHandler.getMouseDirection(); ray.set(origin, direction); Potree.Utils.setCameraLayers(ray, //设置能识别到的layers ['sceneObjects','mapObjects','measure', 'transformationTool', 'model'], viewer.inputHandler.hoverViewport && viewer.inputHandler.hoverViewport.extraEnableLayers ); var intersect = ray.intersectObjects( _gizmo.picker[ this.mode ].children.filter(e=>e.visible), true )[ 0 ] || false; if ( intersect ) { this.axis = intersect.object.name; } else { this.axis = null; } if(oldAxis != this.axis){ viewer.dispatchEvent('content_changed'); } }else { //this.pointerMove() } }; this.pointerDown = function ( /* pointer */ ) { let pointer = viewer.inputHandler.pointer; if ( this.object === undefined || this.dragging === true /* || ( pointer.button !== undefined && pointer.button !== 0 ) */ ) return; if ( /* ( pointer.button === 0 || pointer.button === undefined ) && */ this.axis !== null ) { //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect let {origin, direction} = viewer.inputHandler.getMouseDirection(); ray.set(origin, direction); Potree.Utils.setCameraLayers(ray, //设置能识别到的layers ['sceneObjects','mapObjects','measure', 'transformationTool', 'model'], viewer.inputHandler.hoverViewport && viewer.inputHandler.hoverViewport.extraEnableLayers ); var planeIntersect = ray.intersectObjects( [ _plane ], true )[ 0 ] || false; if ( planeIntersect ) { var space = this.space; if ( this.mode === 'scale' ) { space = 'local'; } else if ( this.axis === 'E' || this.axis === 'XYZE' || this.axis === 'XYZ' ) { space = 'world'; } if ( space === 'local' && this.mode === 'rotate' ) { var snap = this.rotationSnap; if ( this.axis === 'X' && snap ) this.object.rotation.x = Math.round( this.object.rotation.x / snap ) * snap; if ( this.axis === 'Y' && snap ) this.object.rotation.y = Math.round( this.object.rotation.y / snap ) * snap; if ( this.axis === 'Z' && snap ) this.object.rotation.z = Math.round( this.object.rotation.z / snap ) * snap; } this.object.updateMatrixWorld(); this.object.parent.updateMatrixWorld(); positionStart.copy( this.object.position ); quaternionStart.copy( this.object.quaternion ); scaleStart.copy( this.object.scale ); this.object.matrixWorld.decompose( worldPositionStart, worldQuaternionStart, worldScaleStart ); //add: 使坐标轴在boundingBox中心 this.object.boundingBox && this.object.boundingBox.getCenter(worldPositionStart).applyMatrix4(this.object.matrixWorld); pointStart.copy( planeIntersect.point ).sub( worldPositionStart ); /* if(this.player.cameraControls.activeControl){ //this.player.cameraControls.activeControl.locked = true; //add this.player.cameraControls.activeControl.enabled = false; //add } */ } this.dragging = true; mouseDownEvent.mode = this.mode; this.dispatchEvent( mouseDownEvent ); } }; this.pointerMove = function ( /* pointer */ ) { let pointer = viewer.inputHandler.pointer; var axis = this.axis; var mode = this.mode; var object = this.object; var space = this.space; if ( mode === 'scale' ) { space = 'local'; } else if ( axis === 'E' || axis === 'XYZE' || axis === 'XYZ' ) { space = 'world'; } if ( object === undefined || axis === null || this.dragging === false || ( pointer.button !== undefined && pointer.button !== 0 ) ) return; //ray.setFromCamera( pointer, this.camera ); //这句会在floorplan模式get不到intersect let {origin, direction} = viewer.inputHandler.getMouseDirection(); ray.set(origin, direction); Potree.Utils.setCameraLayers(ray, //设置能识别到的layers ['sceneObjects','mapObjects','measure', 'transformationTool', 'model'], viewer.inputHandler.hoverViewport && viewer.inputHandler.hoverViewport.extraEnableLayers ); var planeIntersect = ray.intersectObjects( [ _plane ], true )[ 0 ] || false; if ( planeIntersect === false ) return; pointEnd.copy( planeIntersect.point ).sub( worldPositionStart ); if ( mode === 'translate' ) { // Apply translate offset.copy( pointEnd ).sub( pointStart ); if ( space === 'local' && axis !== 'XYZ' ) { offset.applyQuaternion( worldQuaternionInv ); } if ( axis.indexOf( 'X' ) === - 1 ) offset.x = 0; if ( axis.indexOf( 'Y' ) === - 1 ) offset.y = 0; if ( axis.indexOf( 'Z' ) === - 1 ) offset.z = 0; if ( space === 'local' && axis !== 'XYZ' ) { //xzw 加,否则会反向--------------- object.scale.x < 0 && (offset.x *= -1); object.scale.y < 0 && (offset.y *= -1); object.scale.z < 0 && (offset.z *= -1); //--------------------------------- offset.applyQuaternion( quaternionStart ).divide( parentScale ); } else { offset.applyQuaternion( parentQuaternionInv ).divide( parentScale ); } object.position.copy( offset ).add( positionStart ); // Apply translation snap if ( this.translationSnap ) { if ( space === 'local' ) { object.position.applyQuaternion( _tempQuaternion.copy( quaternionStart ).invert() ); if ( axis.search( 'X' ) !== - 1 ) { object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap; } if ( axis.search( 'Y' ) !== - 1 ) { object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap; } if ( axis.search( 'Z' ) !== - 1 ) { object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap; } object.position.applyQuaternion( quaternionStart ); } if ( space === 'world' ) { if ( object.parent ) { object.position.add( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) ); } if ( axis.search( 'X' ) !== - 1 ) { object.position.x = Math.round( object.position.x / this.translationSnap ) * this.translationSnap; } if ( axis.search( 'Y' ) !== - 1 ) { object.position.y = Math.round( object.position.y / this.translationSnap ) * this.translationSnap; } if ( axis.search( 'Z' ) !== - 1 ) { object.position.z = Math.round( object.position.z / this.translationSnap ) * this.translationSnap; } if ( object.parent ) { object.position.sub( _tempVector.setFromMatrixPosition( object.parent.matrixWorld ) ); } } } //add: object.dispatchEvent({ type: "position_changed" }); } else if ( mode === 'scale' ) { if ( axis.search( 'XYZ' ) !== - 1 ) { var d = pointEnd.length() / pointStart.length(); if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1; if(options.NoScaleZ){//xzw add _tempVector2.set( d, d, 1 ); }else { _tempVector2.set( d, d, d ); } }else if ( axis.search( 'XY' ) !== - 1 ) { //add 等比例for plane var d = pointEnd.length() / pointStart.length(); if ( pointEnd.dot( pointStart ) < 0 ) d *= - 1; _tempVector2.set( d, d, 1 ); }else { _tempVector.copy( pointStart ); _tempVector2.copy( pointEnd ); _tempVector.applyQuaternion( worldQuaternionInv ); _tempVector2.applyQuaternion( worldQuaternionInv ); _tempVector2.divide( _tempVector ); if ( axis.search( 'X' ) === - 1 ) { _tempVector2.x = 1; } if ( axis.search( 'Y' ) === - 1 ) { _tempVector2.y = 1; } if ( axis.search( 'Z' ) === - 1 ) { _tempVector2.z = 1; } } // Apply scale object.scale.copy( scaleStart ).multiply( _tempVector2 ); if ( this.scaleSnap ) { if ( axis.search( 'X' ) !== - 1 ) { object.scale.x = Math.round( object.scale.x / this.scaleSnap ) * this.scaleSnap || this.scaleSnap; } if ( axis.search( 'Y' ) !== - 1 ) { object.scale.y = Math.round( object.scale.y / this.scaleSnap ) * this.scaleSnap || this.scaleSnap; } if ( axis.search( 'Z' ) !== - 1 ) { object.scale.z = Math.round( object.scale.z / this.scaleSnap ) * this.scaleSnap || this.scaleSnap; } } //add: object.dispatchEvent({ type: "scale_changed" }); } else if ( mode === 'rotate' ) { if(this.rotateMethod == 2){//新版参照transfromTool的写法,更跟手,但是有bug,在同时移动位置时或e轴上有问题 if ( axis === 'E' ) {//绕着视线转 rotationAxis.copy( eye );//旋转轴 rotationAngle = pointEnd.angleTo( pointStart ); startNorm.copy( pointStart ).normalize(); endNorm.copy( pointEnd ).normalize(); rotationAngle *= ( endNorm.cross( startNorm ).dot( eye ) < 0 ? 1 : - 1 ); //角度 }else { if ( axis === 'XYZE' ) {//像滚球一样拨动,鼠标滑动方向为拨动方向,在plane面上滚动 offset.copy( pointEnd ).sub( pointStart ); rotationAxis.copy( offset ).cross( eye ).normalize(); }else { rotationAxis.copy( _unit[ axis ] ); } let center = new Vector3;//坐标轴位置 /* if(this.object.boundingBox){ center.copy(worldPosition) //center设置为boundingbox的中心。但center一直更改会导致虽然在按某个时针旋转,但v2相对v1可能会时不时逆向,导致闪烁。所以固定住center,虽然这样计算的角度和绕模型中心的感觉偏颇 }else{ */ center.copy(worldPositionStart); //} let rotationAxis_ = space === 'local' ? rotationAxis.clone().applyQuaternion(worldQuaternion) : rotationAxis; let plane = new Plane().setFromNormalAndCoplanarPoint(rotationAxis_, center);//旋转过程中rotationAxis不会变化,但center可能会 let {origin, direction} = viewer.inputHandler.getMouseDirection(); ray.set(origin, direction); let I = ray.ray.intersectPlane(plane, new Vector3()); if (I) { let v2 = I.clone().sub(center);//.normalize(); if(!this.rotateStart){ this.rotateStart = { v1: v2 }; return } let v1 = this.rotateStart.v1; rotationAngle = math.getAngle( v1, v2, rotationAxis_); //console.log('rot', rotationAngle, center.toArray()) if (Number.isNaN(rotationAngle)) { return; } this.rotateStart.v1 = v2; } } }else { //pointStart 是起始intersect - object中心的向量 //pointEnd 是当前intersect - object中心的向量 offset.copy( pointEnd ).sub( pointStart ); var ROTATION_SPEED = 2 / worldPosition.distanceTo( _tempVector.setFromMatrixPosition( this.camera.matrixWorld ) ); if ( axis === 'E' ) {//绕着视线转 rotationAxis.copy( eye );//旋转轴 rotationAngle = pointEnd.angleTo( pointStart ); startNorm.copy( pointStart ).normalize(); endNorm.copy( pointEnd ).normalize(); rotationAngle *= ( endNorm.cross( startNorm ).dot( eye ) < 0 ? 1 : - 1 ); //角度 } else if ( axis === 'XYZE' ) {//像滚球一样拨动,鼠标滑动方向为拨动方向,在plane面上滚动 rotationAxis.copy( offset ).cross( eye ).normalize(); rotationAngle = offset.dot( _tempVector.copy( rotationAxis ).cross( this.eye ) ) * ROTATION_SPEED; } else if ( axis === 'X' || axis === 'Y' || axis === 'Z' ) { rotationAxis.copy( _unit[ axis ] ); _tempVector.copy( _unit[ axis ] ); if ( space === 'local' ) { _tempVector.applyQuaternion( worldQuaternion ); } rotationAngle = offset.dot( _tempVector.cross( eye ).normalize() ) * ROTATION_SPEED; } // Apply rotation snap if ( this.rotationSnap ) rotationAngle = Math.round( rotationAngle / this.rotationSnap ) * this.rotationSnap; this.rotationAngle = rotationAngle; } // Apply rotate if ( space === 'local' && axis !== 'E' && axis !== 'XYZE' ) { object.scale[axis.toLowerCase()] < 0 && (rotationAngle *= -1); //xzw 加,否则会反向 object.quaternion.copy( quaternionStart ); object.quaternion.multiply( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) ).normalize(); } else { rotationAxis.applyQuaternion( parentQuaternionInv ); object.quaternion.copy( _tempQuaternion.setFromAxisAngle( rotationAxis, rotationAngle ) ); object.quaternion.multiply( quaternionStart ).normalize(); } if ( this.rotateMethod == 2 && axis != 'E' ) { quaternionStart.copy(object.quaternion); } //add: object.dispatchEvent({ type: "rotation_changed" }); } this.dispatchEvent( changeEvent ); this.dispatchEvent( objectChangeEvent ); viewer.dispatchEvent('content_changed'); }; this.pointerUp = function ( pointer ) { //if ( pointer.button !== undefined && pointer.button !== 0 ) return; if ( this.dragging && ( this.axis !== null ) ) { mouseUpEvent.mode = this.mode; this.dispatchEvent( mouseUpEvent ); /* if(this.player.cameraControls.activeControl){ //this.player.cameraControls.activeControl.locked = false; //add this.player.cameraControls.activeControl.pointerDragOn = false //add this.player.cameraControls.activeControl.enabled = true } */ this.rotateStart = null;//add } this.dragging = false; //if ( pointer.button === undefined ) this.axis = null; /* if(this.player.cameraControls.activeControl){ //this.player.cameraControls.activeControl.locked = false; //add this.player.cameraControls.activeControl.pointerDragOn = false //add this.player.cameraControls.activeControl.enabled = true } */ }; // normalize mouse / touch pointer and remap {x,y} to view space. function getPointer( event ) { if(!event){ console.log('hhahhhahah'); return; } if ( document.pointerLockElement ) { return { x: 0, y: 0, button: event.button }; } else { var pointer = event.changedTouches ? event.changedTouches[ 0 ] : event; var rect = domElement.getBoundingClientRect(); return { x: ( pointer.clientX - rect.left ) / rect.width * 2 - 1, y: - ( pointer.clientY - rect.top ) / rect.height * 2 + 1, button: event.button }; } } // mouse / touch event handlers function onPointerHover( event ) { if ( ! scope.enabled ) return; //scope.pointerHover( getPointer( event ) ); scope.pointerHover( ); } function onPointerDown( event ) { if ( ! scope.enabled ) return; //document.addEventListener( "mousemove", onPointerMove, false ); /* scope.pointerHover( getPointer( event ) ); scope.pointerDown( getPointer( event ) ); */ scope.pointerHover( ); scope.pointerDown(); } this.onPointerDown = onPointerDown; function onPointerMove( event ) { if ( ! scope.enabled || !this.dragging) return; //xzw change //scope.pointerMove( getPointer( event ) ); scope.pointerMove( ); } this.onPointerMove = onPointerMove; function onPointerUp( event ) { if ( ! scope.enabled ) return; //document.removeEventListener( "mousemove", onPointerMove, false ); //scope.pointerUp( getPointer( event ) ); scope.pointerUp( ); } this.onPointerUp = onPointerUp; // TODO: deprecate this.getMode = function () { return scope.mode; }; this.setMode = function ( mode ) { scope.mode = mode; }; this.setTranslationSnap = function ( translationSnap ) { scope.translationSnap = translationSnap; }; this.setRotationSnap = function ( rotationSnap ) { scope.rotationSnap = rotationSnap; }; this.setScaleSnap = function ( scaleSnap ) { scope.scaleSnap = scaleSnap; }; this.setSize = function ( size ) { scope.size = size; }; this.setSpace = function ( space ) { scope.space = space; }; this.update = function () { console.warn( 'THREE.TransformControls: update function has no more functionality and therefore has been deprecated.' ); }; }; var TransformControlsGizmo = function (options) { 'use strict'; Object3D.call( this ); this.type = 'TransformControlsGizmo'; // shared materials this.hideAxis = {}; var gizmoMaterial = new MeshBasicMaterial( { depthTest: false, depthWrite: false, transparent: true, side: DoubleSide, fog: false } ); var gizmoLineMaterial = new LineBasicMaterial( { depthTest: false, depthWrite: false, transparent: true, fog: false } ); // Make unique material for each axis/color var matInvisible = gizmoMaterial.clone(); matInvisible.opacity = 0.15; var matHelper = gizmoMaterial.clone(); matHelper.opacity = 0.1; var matRed = gizmoMaterial.clone(); matRed.color.set( 0xff0000 ); var matGreen = gizmoMaterial.clone(); matGreen.color.set( 0x00ff00 ); var matBlue = gizmoMaterial.clone(); matBlue.color.set( 0x0000ff ); var matWhiteTransparent = gizmoMaterial.clone(); matWhiteTransparent.opacity = 0.35; matWhiteTransparent.color.set( 0x00d0fd );//xzw add var matYellowTransparent = matWhiteTransparent.clone(); matYellowTransparent.color.set( 0xffff00 ); var matCyanTransparent = matWhiteTransparent.clone(); matCyanTransparent.color.set( 0x00ffff ); var matMagentaTransparent = matWhiteTransparent.clone(); matMagentaTransparent.color.set( 0xff00ff ); var matYellow = gizmoMaterial.clone(); matYellow.color.set( 0xffff00 ); var matLineRed = gizmoLineMaterial.clone(); matLineRed.color.set( 0xff0000 ); var matLineGreen = gizmoLineMaterial.clone(); matLineGreen.color.set( 0x00ff00 ); var matLineBlue = gizmoLineMaterial.clone(); matLineBlue.color.set( 0x0000ff ); var matLineCyan = gizmoLineMaterial.clone(); matLineCyan.color.set( 0x00ffff ); var matLineMagenta = gizmoLineMaterial.clone(); matLineMagenta.color.set( 0xff00ff ); var matLineYellow = gizmoLineMaterial.clone(); matLineYellow.color.set( 0xffff00 ); var matLineGray = gizmoLineMaterial.clone(); matLineGray.color.set( 0x787878 ); var matLineYellowTransparent = matLineYellow.clone(); matLineYellowTransparent.opacity = 0.25; // reusable geometry var arrowGeometry = new CylinderBufferGeometry( 0, 0.05, 0.2, 12, 1, false ); var scaleHandleGeometry = new BoxBufferGeometry( 0.125, 0.125, 0.125 ); var lineGeometry = new BufferGeometry( ); //lineGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 0, 0 ], 3 ) ); lineGeometry.setAttribute("position", new BufferAttribute(new Float32Array([ 0, 0, 0, 1, 0, 0]), 3 )); var CircleGeometry = function ( radius, arc ) { var geometry = new BufferGeometry( ); var vertices = []; for ( var i = 0; i <= 64 * arc; ++ i ) { vertices.push( 0, Math.cos( i / 32 * Math.PI ) * radius, Math.sin( i / 32 * Math.PI ) * radius ); } //geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) ); geometry.setAttribute("position", new BufferAttribute(new Float32Array(vertices), 3 )); return geometry; }; // Special geometry for transform helper. If scaled with position vector it spans from [0,0,0] to position var TranslateHelperGeometry = function () { var geometry = new BufferGeometry(); //geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( [ 0, 0, 0, 1, 1, 1 ], 3 ) ); geometry.setAttribute("position", new BufferAttribute(new Float32Array([ 0, 0, 0, 1, 1, 1 ]), 3 )); return geometry; }; // Gizmo definitions - custom hierarchy definitions for setupGizmo() function var gizmoTranslate = { X: [ [ new Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, - Math.PI / 2 ], null, 'fwd' ], //[ new THREE.Mesh( arrowGeometry, matRed ), [ 1, 0, 0 ], [ 0, 0, Math.PI / 2 ], null, 'bwd' ], [ new Line( lineGeometry, matLineRed ) ] ], Y: [ [ new Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], null, null, 'fwd' ], //[ new THREE.Mesh( arrowGeometry, matGreen ), [ 0, 1, 0 ], [ Math.PI, 0, 0 ], null, 'bwd' ], [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ]] ], Z: [ [ new Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ Math.PI / 2, 0, 0 ], null, 'fwd' ], //[ new THREE.Mesh( arrowGeometry, matBlue ), [ 0, 0, 1 ], [ - Math.PI / 2, 0, 0 ], null, 'bwd' ], [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ]] ], /* XYZ: [ [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.1, 0 ), matWhiteTransparent.clone() ), [ 0, 0, 0 ], [ 0, 0, 0 ]] ], */ XY: [ [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matYellowTransparent.clone() ), [ 0.15, 0.15, 0 ]], [ new Line( lineGeometry, matLineYellow ), [ 0.18, 0.3, 0 ], null, [ 0.125, 1, 1 ]], [ new Line( lineGeometry, matLineYellow ), [ 0.3, 0.18, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]] ], YZ: [ [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matCyanTransparent.clone() ), [ 0, 0.15, 0.15 ], [ 0, Math.PI / 2, 0 ]], [ new Line( lineGeometry, matLineCyan ), [ 0, 0.18, 0.3 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]], [ new Line( lineGeometry, matLineCyan ), [ 0, 0.3, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]] ], XZ: [ [ new Mesh( new PlaneBufferGeometry( 0.295, 0.295 ), matMagentaTransparent.clone() ), [ 0.15, 0, 0.15 ], [ - Math.PI / 2, 0, 0 ]], [ new Line( lineGeometry, matLineMagenta ), [ 0.18, 0, 0.3 ], null, [ 0.125, 1, 1 ]], [ new Line( lineGeometry, matLineMagenta ), [ 0.3, 0, 0.18 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]] ] }; var pickerTranslate = { X: [ [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0.6, 0, 0 ], [ 0, 0, - Math.PI / 2 ]] ], Y: [ [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0.6, 0 ]] ], Z: [ [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 1, 4, 1, false ), matInvisible ), [ 0, 0, 0.6 ], [ Math.PI / 2, 0, 0 ]] ], /* XYZ: [ [ new THREE.Mesh( new THREE.OctahedronBufferGeometry( 0.2, 0 ), matInvisible ) ] ], */ XY: [ [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0.2, 0 ]] ], YZ: [ [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0, 0.2, 0.2 ], [ 0, Math.PI / 2, 0 ]] ], XZ: [ [ new Mesh( new PlaneBufferGeometry( 0.4, 0.4 ), matInvisible ), [ 0.2, 0, 0.2 ], [ - Math.PI / 2, 0, 0 ]] ] }; var helperTranslate = { START: [ [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ] ], END: [ [ new Mesh( new OctahedronBufferGeometry( 0.01, 2 ), matHelper ), null, null, null, 'helper' ] ], DELTA: [ [ new Line( TranslateHelperGeometry(), matHelper ), null, null, null, 'helper' ] ], X: [ [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ] ], Y: [ [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ] ], Z: [ [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ] ] }; let arc = options.rotFullCircle ? 1 : 0.5;//add var gizmoRotate = { X: [ [ new Line( CircleGeometry( 1, arc ), matLineRed ) ], [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matRed ), [ 0, 0, 0.99 ], null, [ 1, 3, 1 ]], ], Y: [ [ new Line( CircleGeometry( 1, arc ), matLineGreen ), null, [ 0, 0, - Math.PI / 2 ]], [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matGreen ), [ 0, 0, 0.99 ], null, [ 3, 1, 1 ]], ], Z: [ [ new Line( CircleGeometry( 1, arc ), matLineBlue ), null, [ 0, Math.PI / 2, 0 ]], [ new Mesh( new OctahedronBufferGeometry( 0.04, 0 ), matBlue ), [ 0.99, 0, 0 ], null, [ 1, 3, 1 ]], ], E: [ [ new Line( CircleGeometry( 1.25, 1 ), matLineYellowTransparent ), null, [ 0, Math.PI / 2, 0 ]], [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 1.17, 0, 0 ], [ 0, 0, - Math.PI / 2 ], [ 1, 1, 0.001 ]], [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ - 1.17, 0, 0 ], [ 0, 0, Math.PI / 2 ], [ 1, 1, 0.001 ]], [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, - 1.17, 0 ], [ Math.PI, 0, 0 ], [ 1, 1, 0.001 ]], [ new Mesh( new CylinderBufferGeometry( 0.03, 0, 0.15, 4, 1, false ), matLineYellowTransparent ), [ 0, 1.17, 0 ], [ 0, 0, 0 ], [ 1, 1, 0.001 ]], ], XYZE: [ [ new Line( CircleGeometry( 1, 1 ), matLineGray ), null, [ 0, Math.PI / 2, 0 ]] ] }; var helperRotate = { AXIS: [ [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ] ] }; var pickerRotate = { X: [ [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, - Math.PI / 2, - Math.PI / 2 ]], ], Y: [ [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ Math.PI / 2, 0, 0 ]], ], Z: [ [ new Mesh( new TorusBufferGeometry( 1, 0.1, 4, 24 ), matInvisible ), [ 0, 0, 0 ], [ 0, 0, - Math.PI / 2 ]], ], E: [ [ new Mesh( new TorusBufferGeometry( 1.25, 0.1, 2, 24 ), matInvisible ) ] ], XYZE: [ [ new Mesh( new SphereBufferGeometry( 0.7, 10, 8 ), matInvisible ) ] ] }; var gizmoScale = { X: [ [ new Mesh( scaleHandleGeometry, matRed ), [ 0.8, 0, 0 ], [ 0, 0, - Math.PI / 2 ]], [ new Line( lineGeometry, matLineRed ), null, null, [ 0.8, 1, 1 ]] ], Y: [ [ new Mesh( scaleHandleGeometry, matGreen ), [ 0, 0.8, 0 ]], [ new Line( lineGeometry, matLineGreen ), null, [ 0, 0, Math.PI / 2 ], [ 0.8, 1, 1 ]] ], Z: [ [ new Mesh( scaleHandleGeometry, matBlue ), [ 0, 0, 0.8 ], [ Math.PI / 2, 0, 0 ]], [ new Line( lineGeometry, matLineBlue ), null, [ 0, - Math.PI / 2, 0 ], [ 0.8, 1, 1 ]] ], XY: [ [ new Mesh( scaleHandleGeometry, matYellowTransparent ), [ 0.85, 0.85, 0 ], null, [ 2, 2, 0.2 ]], [ new Line( lineGeometry, matLineYellow ), [ 0.855, 0.98, 0 ], null, [ 0.125, 1, 1 ]], [ new Line( lineGeometry, matLineYellow ), [ 0.98, 0.855, 0 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]] ], /* YZ: [ [ new THREE.Mesh( scaleHandleGeometry, matCyanTransparent ), [ 0, 0.85, 0.85 ], null, [ 0.2, 2, 2 ]], [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.855, 0.98 ], [ 0, 0, Math.PI / 2 ], [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineCyan ), [ 0, 0.98, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]] ], XZ: [ [ new THREE.Mesh( scaleHandleGeometry, matMagentaTransparent ), [ 0.85, 0, 0.85 ], null, [ 2, 0.2, 2 ]], [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.855, 0, 0.98 ], null, [ 0.125, 1, 1 ]], [ new THREE.Line( lineGeometry, matLineMagenta ), [ 0.98, 0, 0.855 ], [ 0, - Math.PI / 2, 0 ], [ 0.125, 1, 1 ]] ], XYZX: [ [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 1.1, 0, 0 ]], ],*/ XYZY: [ [ new Mesh( new BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 1.1, 0 ]], ]/* , XYZZ: [ [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.125, 0.125, 0.125 ), matWhiteTransparent.clone() ), [ 0, 0, 1.1 ]], ] */ }; var pickerScale = { X: [ [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0.5, 0, 0 ], [ 0, 0, - Math.PI / 2 ]] ], Y: [ [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0.5, 0 ]] ], Z: [ [ new Mesh( new CylinderBufferGeometry( 0.2, 0, 0.8, 4, 1, false ), matInvisible ), [ 0, 0, 0.5 ], [ Math.PI / 2, 0, 0 ]] ], XY: [ [ new Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0.85, 0 ], null, [ 3, 3, 0.2 ]], ], /*YZ: [ [ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0, 0.85, 0.85 ], null, [ 0.2, 3, 3 ]], ], XZ: [ [ new THREE.Mesh( scaleHandleGeometry, matInvisible ), [ 0.85, 0, 0.85 ], null, [ 3, 0.2, 3 ]], ], XYZX: [ [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 1.1, 0, 0 ]], ],*/ XYZY: [ [ new Mesh( new BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 1.1, 0 ]], ]/* , XYZZ: [ [ new THREE.Mesh( new THREE.BoxBufferGeometry( 0.2, 0.2, 0.2 ), matInvisible ), [ 0, 0, 1.1 ]], ] */ }; var helperScale = { X: [ [ new Line( lineGeometry, matHelper.clone() ), [ - 1e3, 0, 0 ], null, [ 1e6, 1, 1 ], 'helper' ] ], Y: [ [ new Line( lineGeometry, matHelper.clone() ), [ 0, - 1e3, 0 ], [ 0, 0, Math.PI / 2 ], [ 1e6, 1, 1 ], 'helper' ] ], Z: [ [ new Line( lineGeometry, matHelper.clone() ), [ 0, 0, - 1e3 ], [ 0, - Math.PI / 2, 0 ], [ 1e6, 1, 1 ], 'helper' ] ] }; // Creates an THREE.Object3D with gizmos described in custom hierarchy definition. var setupGizmo = function ( gizmoMap ) { var gizmo = new Object3D(); for ( var name in gizmoMap ) { for ( var i = gizmoMap[ name ].length; i --; ) { var object = gizmoMap[ name ][ i ][ 0 ].clone(); var position = gizmoMap[ name ][ i ][ 1 ]; var rotation = gizmoMap[ name ][ i ][ 2 ]; var scale = gizmoMap[ name ][ i ][ 3 ]; var tag = gizmoMap[ name ][ i ][ 4 ]; // name and tag properties are essential for picking and updating logic. object.name = name; object.tag = tag; if ( position ) { object.position.set( position[ 0 ], position[ 1 ], position[ 2 ] ); } if ( rotation ) { object.rotation.set( rotation[ 0 ], rotation[ 1 ], rotation[ 2 ] ); } if ( scale ) { object.scale.set( scale[ 0 ], scale[ 1 ], scale[ 2 ] ); } object.updateMatrix(); var tempGeometry = object.geometry.clone(); tempGeometry.applyMatrix4( object.matrix ); object.geometry = tempGeometry; object.renderOrder = Infinity; object.position.set( 0, 0, 0 ); object.rotation.set( 0, 0, 0 ); object.scale.set( 1, 1, 1 ); gizmo.add( object ); } } return gizmo; }; // Reusable utility variables var tempVector = new Vector3( 0, 0, 0 ); var tempEuler = new Euler(); var alignVector = new Vector3( 0, 1, 0 ); var zeroVector = new Vector3( 0, 0, 0 ); var lookAtMatrix = new Matrix4(); var tempQuaternion = new Quaternion(); var tempQuaternion2 = new Quaternion(); var identityQuaternion = new Quaternion(); var unitX = new Vector3( 1, 0, 0 ); var unitY = new Vector3( 0, 1, 0 ); var unitZ = new Vector3( 0, 0, 1 ); // Gizmo creation this.gizmo = {}; this.picker = {}; this.helper = {}; this.add( this.gizmo[ "translate" ] = setupGizmo( gizmoTranslate ) ); this.add( this.gizmo[ "rotate" ] = setupGizmo( gizmoRotate ) ); this.add( this.gizmo[ "scale" ] = setupGizmo( gizmoScale ) ); this.add( this.picker[ "translate" ] = setupGizmo( pickerTranslate ) ); this.add( this.picker[ "rotate" ] = setupGizmo( pickerRotate ) ); this.add( this.picker[ "scale" ] = setupGizmo( pickerScale ) ); this.add( this.helper[ "translate" ] = setupGizmo( helperTranslate ) ); this.add( this.helper[ "rotate" ] = setupGizmo( helperRotate ) ); this.add( this.helper[ "scale" ] = setupGizmo( helperScale ) ); // Pickers should be hidden always this.picker[ "translate" ].visible = false; this.picker[ "rotate" ].visible = false; this.picker[ "scale" ].visible = false; // updateMatrixWorld will update transformations and appearance of individual handles this.updateMatrixWorld = function () { if(!this.parent.visible)return //add var space = this.space; if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation var quaternion = space === "local" ? this.worldQuaternion : identityQuaternion; // Show only gizmos for current transform mode this.gizmo[ "translate" ].visible = this.mode === "translate"; this.gizmo[ "rotate" ].visible = this.mode === "rotate"; this.gizmo[ "scale" ].visible = this.mode === "scale"; this.helper[ "translate" ].visible = this.mode === "translate"; this.helper[ "rotate" ].visible = this.mode === "rotate"; this.helper[ "scale" ].visible = this.mode === "scale"; var handles = []; handles = handles.concat( this.picker[ this.mode ].children ); handles = handles.concat( this.gizmo[ this.mode ].children ); handles = handles.concat( this.helper[ this.mode ].children ); for ( var i = 0; i < handles.length; i ++ ) { var handle = handles[ i ]; //add if(this.hideAxis[this.mode] && this.hideAxis[this.mode].some(e=>handle.name.includes(e.toUpperCase()))){ Potree.Utils.updateVisible(handle, 'hidden', false); continue } let visible = true; // hide aligned to camera handle.rotation.set( 0, 0, 0 ); handle.position.copy( this.worldPosition ); if(this.camera.type == "OrthographicCamera"){ var eyeDistance = 800 / this.camera.zoom; }else { var eyeDistance = this.worldPosition.distanceTo( this.cameraPosition ); } handle.scale.set( 1, 1, 1 ).multiplyScalar( eyeDistance * this.size / 7 ); // TODO: simplify helpers and consider decoupling from gizmo if ( handle.tag === 'helper' ) { visible = false; if ( handle.name === 'AXIS' ) { handle.position.copy( this.worldPositionStart ); visible = !!this.axis; if ( this.axis === 'X' ) { tempQuaternion.setFromEuler( tempEuler.set( 0, 0, 0 ) ); handle.quaternion.copy( quaternion ).multiply( tempQuaternion ); if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) { visible = false; } } if ( this.axis === 'Y' ) { tempQuaternion.setFromEuler( tempEuler.set( 0, 0, Math.PI / 2 ) ); handle.quaternion.copy( quaternion ).multiply( tempQuaternion ); if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) { visible = false; } } if ( this.axis === 'Z' ) { tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) ); handle.quaternion.copy( quaternion ).multiply( tempQuaternion ); if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > 0.9 ) { visible = false; } } if ( this.axis === 'XYZE' ) { tempQuaternion.setFromEuler( tempEuler.set( 0, Math.PI / 2, 0 ) ); alignVector.copy( this.rotationAxis ); handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( zeroVector, alignVector, unitY ) ); handle.quaternion.multiply( tempQuaternion ); visible = this.dragging; } if ( this.axis === 'E' ) { visible = false; } } else if ( handle.name === 'START' ) { handle.position.copy( this.worldPositionStart ); visible = this.dragging; } else if ( handle.name === 'END' ) { handle.position.copy( this.worldPosition ); visible = this.dragging; } else if ( handle.name === 'DELTA' ) { handle.position.copy( this.worldPositionStart ); handle.quaternion.copy( this.worldQuaternionStart ); tempVector.set( 1e-10, 1e-10, 1e-10 ).add( this.worldPositionStart ).sub( this.worldPosition ).multiplyScalar( - 1 ); tempVector.applyQuaternion( this.worldQuaternionStart.clone().invert() ); handle.scale.copy( tempVector ); visible = this.dragging; } else { handle.quaternion.copy( quaternion ); if ( this.dragging ) { handle.position.copy( this.worldPositionStart ); } else { handle.position.copy( this.worldPosition ); } if ( this.axis ) { visible = this.axis.search( handle.name ) !== - 1; } } // If updating helper, skip rest of the loop Potree.Utils.updateVisible(handle, 'hidden', !!visible); continue; } // Align handles to current local or world rotation handle.quaternion.copy( quaternion ); if ( this.mode === 'translate' || this.mode === 'scale' ) { // Hide translate and scale axis facing the camera var AXIS_HIDE_TRESHOLD = 0.99; var PLANE_HIDE_TRESHOLD = 0.2; var AXIS_FLIP_TRESHOLD = 0.0; if(options.dontHideWhenFaceCamera){//xzw add //正对镜头时不隐藏箭头 }else { if ( handle.name === 'X' || handle.name === 'XYZX' ) { if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) { handle.scale.set( 1e-10, 1e-10, 1e-10 ); visible = false; } } if ( handle.name === 'Y' || handle.name === 'XYZY' ) { if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) { handle.scale.set( 1e-10, 1e-10, 1e-10 ); visible = false; } } if ( handle.name === 'Z' || handle.name === 'XYZZ' ) { if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) > AXIS_HIDE_TRESHOLD ) { handle.scale.set( 1e-10, 1e-10, 1e-10 ); visible = false; } } if ( handle.name === 'XY' ) { if ( Math.abs( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) { handle.scale.set( 1e-10, 1e-10, 1e-10 ); visible = false; } } if ( handle.name === 'YZ' ) { if ( Math.abs( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) { handle.scale.set( 1e-10, 1e-10, 1e-10 ); visible = false; } } if ( handle.name === 'XZ' ) { if ( Math.abs( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) ) < PLANE_HIDE_TRESHOLD ) { handle.scale.set( 1e-10, 1e-10, 1e-10 ); visible = false; } } } // Flip translate and scale axis ocluded behind another axis //xzw 改 去掉反向箭头 if ( handle.name.search( 'X' ) !== - 1 ) { if ( alignVector.copy( unitX ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) { /* if ( handle.tag === 'fwd' ) { handle.visible = false; } else { handle.scale.x *= - 1; } } else if ( handle.tag === 'bwd' ) { handle.visible = false;*/ handle.scale.x *= - 1; } } if ( handle.name.search( 'Y' ) !== - 1 ) { if ( alignVector.copy( unitY ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) { /* if ( handle.tag === 'fwd' ) { handle.visible = false; } else { handle.scale.y *= - 1; } } else if ( handle.tag === 'bwd' ) { handle.visible = false; */ handle.scale.y *= - 1; } } if ( handle.name.search( 'Z' ) !== - 1 ) { if ( alignVector.copy( unitZ ).applyQuaternion( quaternion ).dot( this.eye ) < AXIS_FLIP_TRESHOLD ) { /* if ( handle.tag === 'fwd' ) { handle.visible = false; } else { handle.scale.z *= - 1; } } else if ( handle.tag === 'bwd' ) { handle.visible = false; */ handle.scale.z *= - 1; } } } else if ( this.mode === 'rotate' ) { // Align handles to current local or world rotation tempQuaternion2.copy( quaternion ); alignVector.copy( this.eye ).applyQuaternion( tempQuaternion.copy( quaternion ).invert() ); if ( handle.name.search( "E" ) !== - 1 ) { handle.quaternion.setFromRotationMatrix( lookAtMatrix.lookAt( this.eye, zeroVector, unitY ) ); } if ( handle.name === 'X' ) { tempQuaternion.setFromAxisAngle( unitX, Math.atan2( - alignVector.y, alignVector.z ) ); tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion ); handle.quaternion.copy( tempQuaternion ); } if ( handle.name === 'Y' ) { tempQuaternion.setFromAxisAngle( unitY, Math.atan2( alignVector.x, alignVector.z ) ); tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion ); handle.quaternion.copy( tempQuaternion ); } if ( handle.name === 'Z' ) { tempQuaternion.setFromAxisAngle( unitZ, Math.atan2( alignVector.y, alignVector.x ) ); tempQuaternion.multiplyQuaternions( tempQuaternion2, tempQuaternion ); handle.quaternion.copy( tempQuaternion ); } } // Hide disabled axes visible = visible && ( handle.name.indexOf( "X" ) === - 1 || this.showX ); visible = visible && ( handle.name.indexOf( "Y" ) === - 1 || this.showY ); visible = visible && ( handle.name.indexOf( "Z" ) === - 1 || this.showZ ); visible = visible && ( handle.name.indexOf( "E" ) === - 1 || ( this.showX && this.showY && this.showZ ) ); Potree.Utils.updateVisible(handle, 'hidden', !!visible); // highlight selected axis handle.material._opacity = handle.material._opacity || handle.material.opacity; handle.material._color = handle.material._color || handle.material.color.clone(); handle.material.color.copy( handle.material._color ); handle.material.opacity = handle.material._opacity; if ( ! this.enabled ) { handle.material.opacity *= 0.5; handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 ); } else if ( this.axis ) { if ( handle.name === this.axis ) { handle.material.opacity = 1.0; handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 ); } else if ( this.axis.split( '' ).some( function ( a ) { return handle.name === a; } ) ) { handle.material.opacity = 1.0; handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 ); } else { handle.material.opacity *= 0.25; handle.material.color.lerp( new Color( 1, 1, 1 ), 0.5 ); } } } Object3D.prototype.updateMatrixWorld.call( this ); }; }; /* var TransformControlsLines = function (options) { THREE.Object3D.call( this) let label1 = new Label2D({ innerHTML:`
    与相机水平距离
    ` , domElement:$("#otherLabels")[0] ,autoUpdate:true }) let label2 = new Label2D({ innerHTML:`
    与相机高度差
    ` , domElement:$("#otherLabels")[0] ,autoUpdate:true }) let css = {'opacity': 0.7, 'color': '#07fceb', transform:'translate(-50%, -50%)'} label1.elem.css(css); label2.elem.css(css) //for(let i=0;i<2;i++){ // let line = LineDraw.createLine([new THREE.THREE.Vector3, new THREE.THREE.Vector3],{color:'#07fceb', opacity:0.7 }) // this.add(line) //} this.updateTransform = function(object){ object = object || this.parent.object; let A = player.position.clone(); //当前相机位置 let B = object.position //物体位置 let C = B.clone().setY(A.y); //物体在相机的高度的位置 let D = A.clone().setY(B.y); //相机在物体的高度的位置 //LineDraw.moveLine(this.children[0], [B, C]) // 垂直线 //LineDraw.moveLine(this.children[1], [D, B]) // 水平线 let dis1 = toPrecision(D.distanceTo(B),1) let dis2 = C.y - B.y label1.elem.text('与相机水平距离: '+dis1+'米') label2.elem.text('在相机之'+ (dis2>0?'下' : '上') +' :'+ toPrecision(dis2 , 1) +'米') label1.setPos(new THREE.THREE.Vector3().addVectors(D,B).multiplyScalar(0.5)) label2.setPos(new THREE.THREE.Vector3().addVectors(B,C).multiplyScalar(0.5)) } this.setVisible = (v,reason)=>{ label1.setVisible(v, reason || 'unvisi') label2.setVisible(v, reason || 'unvisi') this.visible = label1.visible if(this.visible){ this.updateTransform() } } this.setVisible(false) player.on("mode.changing",(currentMode, mode, pano, duration)=>{ if(mode != 'panorama'){ this.setVisible(false, 'isPanorama') }else{ this.setVisible(true, 'isPanorama') } }) player.on("flying.started",( )=>{ this.setVisible(false, 'flying') }) player.on("flying.ended",( )=>{ this.setVisible(true, 'flying') }) } */ var TransformControlsPlane = function (options) { 'use strict'; Mesh.call( this, new PlaneBufferGeometry( 100000, 100000, 2, 2 ), new MeshBasicMaterial( { color: "#ff0000", visible: false, wireframe: false, side: DoubleSide, transparent: true, opacity: 0.2 } ) ); this.type = 'TransformControlsPlane'; var unitX = new Vector3( 1, 0, 0 ); var unitY = new Vector3( 0, 1, 0 ); var unitZ = new Vector3( 0, 0, 1 ); var tempVector = new Vector3(); var dirVector = new Vector3(); var alignVector = new Vector3(); var tempMatrix = new Matrix4(); var identityQuaternion = new Quaternion(); this.updateMatrixWorld = function () { if(!this.visible)return//add var space = this.space; this.position.copy( this.worldPosition ); if ( this.mode === 'scale' ) space = 'local'; // scale always oriented to local rotation unitX.set( 1, 0, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion ); unitY.set( 0, 1, 0 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion ); unitZ.set( 0, 0, 1 ).applyQuaternion( space === "local" ? this.worldQuaternion : identityQuaternion ); // Align the plane for current transform mode, axis and space. alignVector.copy( unitY ); switch ( this.mode ) { case 'translate': case 'scale': switch ( this.axis ) { case 'X': alignVector.copy( this.eye ).cross( unitX ); dirVector.copy( unitX ).cross( alignVector ); break; case 'Y': alignVector.copy( this.eye ).cross( unitY ); dirVector.copy( unitY ).cross( alignVector ); break; case 'Z': alignVector.copy( this.eye ).cross( unitZ ); dirVector.copy( unitZ ).cross( alignVector ); break; case 'XY': dirVector.copy( unitZ ); break; case 'YZ': dirVector.copy( unitX ); break; case 'XZ': alignVector.copy( unitZ ); dirVector.copy( unitY ); break; case 'XYZ': case 'E': default: //xzw add for scale xyzz dirVector.set( 0, 0, 0 ); break; } break; case 'rotate': default: // special case for rotate dirVector.set( 0, 0, 0 ); } if ( dirVector.length() === 0 ) {//使物体在视线中平移 // If in rotate mode, make the plane parallel to camera this.quaternion.copy( this.cameraQuaternion ); } else {//方向滑动所在面 tempMatrix.lookAt( tempVector.set( 0, 0, 0 ), dirVector, alignVector ); this.quaternion.setFromRotationMatrix( tempMatrix ); } Object3D.prototype.updateMatrixWorld.call( this ); }; }; TransformControls.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: TransformControls, isTransformControls: true } ); TransformControlsGizmo.prototype = Object.assign( Object.create( Object3D.prototype ), { constructor: TransformControlsGizmo, isTransformControlsGizmo: true } ); TransformControlsPlane.prototype = Object.assign( Object.create( Mesh.prototype ), { constructor: TransformControlsPlane, isTransformControlsPlane: true } ); /* 备注: //这里虽然 使坐标轴在boundingBox中心 boundingBox && this.object.boundingBox.getCenter(worldPosition).applyMatrix4(this.object.matrixWorld); 但是旋转中心并不是这个坐标轴显示的位置,需要再执行 MergeEditor.maintainBoundXY()才能维持在这个中心 */ const texLoader$a = new TextureLoader(); texLoader$a.crossOrigin = "anonymous"; const edgeStrengths = { pointcloud: 4, glb: 100 }; const viewportProps$1 = [{ left:0, bottom:0, width: 0.5,height:1, name : 'top', axis:["x","y"], direction : new Vector3(0,0,-1), //镜头朝向 active: true, //相机位置在z轴正向 limitBound: new Box3(new Vector3(-Infinity,-Infinity, 1),new Vector3(Infinity,Infinity,5000)), //在地面以上 margin:{x:50, y:150} , }, { left:0.5, bottom:0, width: 0.5,height:1, name : 'right', axis:["y","z"], direction : new Vector3(1,0,0), active: true, //相机位置在x轴负向 右下角屏 viewContainsPoints:[new Vector3(0,0,0)], margin:{x:300, y:250} , } ]; let cylinderSkyGeo, oldSkyGeo; let MergeEditor = { bus:new EventDispatcher(), SplitScreen : new SplitScreen(), init(){ this.history = new History({ applyData: (data)=>{ if(data.object.parent /* && data.object == this.selected */){ data = Potree.Common.CloneObject(data); //避免使用后更改数据又被使用 data.matrix.decompose( data.object.position, data.object.quaternion, data.object.scale ); data.object.boundCenter.copy(data.boundCenter); data.object.dispatchEvent('changeByHistory'); data.object.dispatchEvent('transformChanged'); viewer.dispatchEvent('content_changed'); return true } }, getData:(object)=>{ return { object, matrix: object.matrixWorld.clone(), boundCenter: object.boundCenter.clone() } } }); { Potree.settings.notAdditiveBlending = true; let ground = this.ground = new InfiniteGridHelper(1, 10000, new Color('#eee'), 10000, 0.2, 0.3); viewer.scene.scene.add(ground); //再加两条线否则在正侧边看不到 let line1 = LineDraw.createLine([new Vector3(-10000, 0, 0),new Vector3(10000, 0, 0) ], {color:'#aaa', }); let line2 = LineDraw.createLine([new Vector3(0, -10000, 0),new Vector3(0, 10000, 0) ], {mat:line1.material}); ground.renderOrder = Potree.config.renderOrders.model + 1;//line1.renderOrder + 1 //要比模型低,否则模型透明时效果不对 ground.add(line1); ground.add(line2); ground.material.opacity = 0.9; //为了滞后渲染,否则被rt遮住 ground.material.polygonOffset = true; //多边形偏移(视觉上没有移动模型位置),防止闪烁 ground.material.polygonOffsetFactor = 100; //多边形偏移因子 ground.material.polygonOffsetUnits = 10; //多边形偏移单位 ground.material.depthWrite = false; //ground.material.depthTest = false line1.material.polygonOffset = true; line1.material.polygonOffsetFactor = 130; line1.material.polygonOffsetUnits = 10; line1.material.depthWrite = false; //见笔记:透明物体的材质设置 } let oriEdgeStrength = viewer.outlinePass.edgeStrength; { this.transformControls = new TransformControls(viewer.mainViewport.camera, viewer.renderArea,{ dontHideWhenFaceCamera: true, }); //this.transformControls.space = 'local'//为了在当前方向上平移 this.transformControls.setSize(1.5); viewer.scene.scene.add(this.transformControls); this.transformControls._gizmo.hideAxis = {rotate:['e']}; this.transformControls.setRotateMethod(2); //右屏 this.transformControls2 = new TransformControls(viewer.mainViewport.camera, viewer.renderArea,{ dontHideWhenFaceCamera: true, }); this.transformControls2.setSize(1.5); viewer.scene.scene.add(this.transformControls2); Potree.Utils.setObjectLayers(this.transformControls2, 'layer2' ); let mouseDown = (e)=>{ viewer.outlinePass.edgeStrength = 0;//暂时消失线 }; let mouseUp = (e)=>{ //this.updateEdgeStrength() viewer.outlinePass.edgeStrength = oriEdgeStrength; }; this.transformControls.addEventListener('mouseDown',mouseDown); this.transformControls2.addEventListener('mouseDown',mouseDown); this.transformControls.addEventListener('mouseUp',mouseUp); this.transformControls2.addEventListener('mouseUp',mouseUp); this.transformControls.addEventListener('mouseDown', ()=>{ //dragstart this.history.beforeChange(this.selected); }); this.transformControls.addEventListener('mouseUp',()=>{ this.history.afterChange(this.selected); }); } { this.secondCompass = new Compass(null); } viewer.setControls(viewer.orbitControls); //viewer.mainViewport.view.fixZWhenPan = true viewer.orbitControls.constantlyForward = true; viewer.addEventListener('global_single_click',(e)=>{ if( this.noNeedSelection //如模型查看页 || viewer.scene.cameraAnimations.some(c=>c.onUpdate) //正在播放 || e.drag && e.drag.notPressMouse //在加测量线 || viewer.mainViewport.view.isFlying() //有其他校准 || this.split //分屏中 || e.clickElement //触发别的点击事件,如测量时click marker /* && e.clickElement != e.intersect.object */ ){ return } if(e.intersect){ let object = e.intersect.object || e.intersect.pointcloud; let objects = this.getAllObjects(); if(objects.includes(object) && this.selected != object){ this.selectModel(object); }else { //if(!viewer.inputHandler.selection[0]){//正在平移和旋转,不允许取消 this.selectModel(null); //} } }else { //if(!viewer.inputHandler.selection[0]){ this.selectModel(null); //} } }); viewer.inputHandler.addEventListener('keydown', (e)=>{ if((e.event.key).toLowerCase() == "h" ){ this.fadeOutlineAuto = !this.fadeOutlineAuto; this.showModelOutline(this.selected,!!this.selected); } }); //viewer.fxaaPass.enabled = false//viewer.ssaaRenderPass.enabled = false viewer.outlinePass.enabled = true; //Potree.settings.intersectWhenHover = false //Potree.Utils.updateVisible(viewer.reticule, 'force', false) viewer.composer.scaleRatio = 1; viewer.composer.readTarget = false; viewer.mainViewport.camera.near = 0.05; // too small will result in z-fighting viewer.addEventListener('updateModelBound', (e)=>{ if(this.split){ this.SplitScreen.updateCameraOutOfModel(/* this.selected && [this.selected] */); } }); {//校准页面拖拽 //左右屏都可以拖拽模型,旋转只能左屏 let dragInfo; let drag = (e)=>{ if(this.split && this.selected && this.transformState && (e.dragViewport.name == 'top' || this.transformState == 'translate') ){ if(e.type == 'global_mousedown' ){ //开始 //if((e.intersect.object || e.intersect.pointcloud) == this.selected){ if(e.intersect.pointclouds.includes(this.selected) || e.intersect.allElements.some(e=>e.object == this.selected)){ dragInfo = {}; //if(this.selected.isPointcloud){ viewer.outlinePass.edgeStrength = 0;//暂时消失线 //} } } if(e.type == 'global_drag' && dragInfo ){ if(this.transformState == 'translate'){ let moveVec = Potree.Utils.getOrthoCameraMoveVec(e.drag.pointerDelta, e.dragViewport.camera );//最近一次移动向量 this.selected.position.add(moveVec); this.selected.dispatchEvent("position_changed"); }else if(this.transformState == 'rotate'){ let vec = new Vector3().subVectors(e.intersect.orthoIntersect || e.intersect.location, this.selected.boundCenter).setZ(0); if(dragInfo.lastVec == void 0){//global_mousedown dragInfo.lastVec = vec; return } let angle = math.getAngle(dragInfo.lastVec, vec, 'z'); dragInfo.lastVec = vec; //this.selected.rotation.z += angle //局部 /* object.quaternion.copy( .setFromAxisAngle( new THREE.Vector3(0,0,1), angle ) ); object.quaternion.multiply( quaternionStart ).normalize(); */ let diffQua = new Quaternion().setFromAxisAngle( new Vector3(0,0,1), angle ); this.selected.quaternion.premultiply(diffQua); //世界 this.selected.dispatchEvent("rotation_changed"); } return {stopContinue:true} } } }; viewer.addEventListener('global_mousedown', drag); viewer.addEventListener('global_drag', drag, {importance:10}); viewer.addEventListener('global_mousemove', (e)=>{ if(this.split && this.transformState && !e.drag && (e.hoverViewport.name == 'top' || this.transformState == 'translate')){ /* if(this.lastHoverViewport != e.hoverViewport){ this.lastHoverViewport = e.hoverViewport this.transformControls.view = e.hoverViewport.view this.transformControls.camera = e.hoverViewport.camera this.transformControls.hideAxis( this.transformState, e.hoverViewport.name == 'top' ? [z] : [x,y]); } */ let mouseover = e.intersect.pointclouds.includes(this.selected) || e.intersect.allElements.some(e=>e.object == this.selected); //let mouseover = (e.intersect.object || e.intersect.pointcloud) == this.selected if(mouseover){ if(this.transformState == 'translate'){ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"movePointcloud" }); }else { viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"rotatePointcloud" }); } }else { this.clearTranCursor(); } } }); viewer.addEventListener('global_drop', (e)=>{ dragInfo = null; this.clearTranCursor(); //this.updateEdgeStrength() viewer.outlinePass.edgeStrength = oriEdgeStrength; }); } /* viewer.addEventListener('background_changed',()=>{ }) */ viewer.addEventListener('camera_changed',()=>{ Common.intervalTool.isWaiting('updateMemoryUsage', ()=>{ this.updateMemoryUsage(); }, 1000); }); viewer.addEventListener('setDisplay',this.updateMemoryUsage.bind(this)); }, clearTranCursor(){ viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"movePointcloud" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"rotatePointcloud" }); }, enterSplit(){ this.split = true; if(this.selected) this.SplitScreen.focusCenter = this.selected.boundCenter; //旋转中心。注意 boundCenter不能直接赋值,否则改变后focusCenter也要改 else this.SplitScreen.focusCenter = null; this.SplitScreen.splitStart(viewportProps$1); this.beforeSplit = { pointDensity: Potree.settings.pointDensity, }; Potree.settings.pointDensity = 'fourViewports'; //强制降低点云质量 viewer.setControls(viewer.fpControls); let rightViewport = viewer.viewports.find(e=>e.name == 'right'); let topViewport = viewer.viewports.find(e=>e.name == 'top'); topViewport.alignment = true; rightViewport.rotateSide = true; rightViewport.skyboxFixPos = true; rightViewport.skyboxMinZoom = 10; rightViewport.skyboxRenderFun = ()=>{// 使cube的一面永远正向镜头。 因侧视图的camera是ortho类型,需要平视mesh才不会拉伸 viewer.skybox.scene.children[0].rotation.copy(rightViewport.camera.rotation); }; topViewport.skyboxRenderFun = ()=>{ viewer.skybox.scene.children[0].rotation.set(0,0,0); }; viewer.viewports[1].layersAdd('layer2'); viewer.viewports[0].layersAdd('layer1'); Potree.Utils.setObjectLayers(this.transformControls, 'layer1' ); this.transformControls.view = viewer.viewports[0].view; this.transformControls.camera = viewer.viewports[0].camera; this.transformControls._gizmo.hideAxis = {translate:['z'], rotate:['x','y','z'] }; this.transformControls2.view = viewer.viewports[1].view; this.transformControls2.camera = viewer.viewports[1].camera; this.transformControls2._gizmo.hideAxis = {translate:['x','y'], rotate:['x','y','z'] }; this.secondCompass.changeViewport(viewer.viewports[0]); this.secondCompass.setDomPos(); this.secondCompass.setDisplay(true); viewer.compass.changeViewport(viewer.viewports[1]); viewer.compass.setDomPos(); //this.changeSkyboxGeo(true) }, leaveSplit(){ this.split = false; this.SplitScreen.unSplit(); viewer.setControls(viewer.orbitControls); Potree.settings.pointDensity = this.beforeSplit.pointDensity; /* if(this.selected && this.selected.isPointcloud){ this.showModelOutline(this.selected, true) this.selected.material.activeAttributeName = "rgba" } */ this.transformControls.camera = viewer.viewports[0].camera; this.transformControls.view = viewer.viewports[0].view; this.transformControls._gizmo.hideAxis = {rotate:['e']}; Potree.Utils.setObjectLayers(this.transformControls, 'sceneObjects' ); //恢复 viewer.compass.changeViewport(viewer.viewports[0]); //恢复 viewer.compass.setDomPos(); this.secondCompass.setDisplay(false); }, rotateSideCamera(angle){ this.SplitScreen.rotateSideCamera(viewer.viewports.find(e=>e.name == 'right'), angle); }, setTransformState(state){//校准时 this.transformState = state; this.clearTranCursor(); }, //--------------------------- getAllObjects(){ return viewer.objs.children.concat(viewer.scene.pointclouds) }, getModel(id){ let models = this.getAllObjects(); return models.find(e=>e.dataset_id == id) }, removeModel(model){ if(this.selected == model) this.selectModel(null); let dispose = (e)=>{ e.geometry && e.geometry.dispose(); e.material && e.material.dispose(); }; if(model.isPointcloud){ dispose(model); viewer.scene.removePointCloud(model); }else { model.traverse(e=>{ dispose(e); }); viewer.objs.remove(model); this.updateMemoryUsage(); } }, selectModel(model, state=true, fitBound, by2d){ if(!model) { model = this.selected; state = false; } if(state){ if(this.selected){ if(this.selected == model) return else { let transToolAttached = !!this.transformControls.object; this.selectModel(this.selected, false, fitBound, by2d); transToolAttached && this.transformControls.attach(model); } } this.selected = model; MergeEditor.focusOn(model, 500, !!fitBound); //通过在场景里点击模型的话,不focus this.showModelOutline(model); //this.updateEdgeStrength() //console.log('selectModel', model) }else { if(this.selected != model)return //model本来就没选中,不需要处理(防止2d先选中新的再取消旧的) this.showModelOutline(model, false); this.selected = null; this.transformControls.detach(); //viewer.transformObject(null); //console.log('selectModel', null) } if(!by2d && model){ model.dispatchEvent({type:'changeSelect', selected : state}); } }, showModelOutline(model, state){ if(this.fadeOutlineAuto){ if(state === false){ viewer.outlinePass.selectedObjects = []; clearTimeout(this.timer); return } viewer.outlinePass.selectedObjects = [model]; if(this.timer){ clearTimeout(this.timer); } this.timer = setTimeout(()=>{ viewer.outlinePass.selectedObjects = []; viewer.dispatchEvent('content_changed'); }, 1000); }else { if(state === false){ viewer.outlinePass.selectedObjects = []; }else { viewer.outlinePass.selectedObjects = [model]; } } viewer.dispatchEvent('content_changed'); }, /*updateEdgeStrength(){ if(!this.selected)return if(this.selected.isPointcloud){ viewer.outlinePass.edgeStrength = edgeStrengths.pointcloud// / this.selected.material.opacity }else{ viewer.outlinePass.edgeStrength = edgeStrengths.glb } },*/ focusOn(objects, duration = 400, fitBound=true, dontLookUp){ if(!(objects instanceof Array)){ objects = [objects]; } let boundingBox = new Box3; objects.forEach(object=>{ boundingBox.union(object.boundingBox.clone().applyMatrix4(object.matrixWorld)); }); let len = boundingBox.getSize(new Vector3).length(); Potree.settings.cameraFar = Math.max( Potree.settings.cameraFar, len*3 ); if(fitBound){ viewer.focusOnObject({boundingBox}, 'boundingBox', duration, {dontLookUp, dontChangeCamDir:true}); }else { /* let position = viewer.inputHandler.intersect ? viewer.inputHandler.intersect.location : boundingBox.getCenter(new THREE.Vector3) position && viewer.focusOnObject({position}, 'point', duration, {dontChangePos: true}) */ let position = viewer.inputHandler.intersect ? viewer.inputHandler.intersect.location : boundingBox.getCenter(new Vector3); if(!position)return /* let targetOld = viewer.mainViewport.view.getPivot() let projected1 = targetOld.clone().project(viewer.mainViewport.camera); let projected2 = position.clone().project(viewer.mainViewport.camera); //使用其z let targetNew = projected1.clone().setZ(projected2.z).unproject(viewer.mainViewport.camera); viewer.mainViewport.view.lookAt(targetNew) */ viewer.mainViewport.view.radius = viewer.mainViewport.camera.position.distanceTo(position); //为了不改画面,不调节方向了,只能调调radius,一定程度将target靠近model } }, moveBoundCenterTo(model,pos){ //使boundCenter在所要的位置 let diff = new Vector3().subVectors(pos, model.boundCenter); model.position.add(diff); }, getBoundCenter(model){ if(!model.boundCenter) model.boundCenter = new Vector3; model.boundingBox.getCenter(model.boundCenter).applyMatrix4(model.matrixWorld); }, setModelBtmHeight(model, z ){ //无论模型怎么缩放、旋转,都使最低点为z if(z == void 0) z = model.btmHeight; //维持离地高度 else model.btmHeight = z; if(model.btmHeight == void 0)return model.updateMatrixWorld(); let boundingBox2 = model.boundingBox.clone().applyMatrix4(model.matrixWorld); let size = boundingBox2.getSize(new Vector3); let center = boundingBox2.getCenter(new Vector3); let hopeZ = z + size.z / 2; //model.position.z = z + size.z / 2 - center.z model.position.z += (hopeZ - center.z); }, computeBtmHeight(model){ //位移之后重新计算btmHeight model.updateMatrixWorld(); let boundingBox2 = model.boundingBox.clone().applyMatrix4(model.matrixWorld); let size = boundingBox2.getSize(new Vector3); let center = boundingBox2.getCenter(new Vector3); model.btmHeight = center.z - size.z / 2; }, maintainBoundXY(model){ //在旋转和缩放后,立即执行这个函数,使boundCenter保持原位 model.updateMatrixWorld(); let center1 = model.boundCenter.clone();//还未更新的 this.getBoundCenter(model);//更新 let center2 = model.boundCenter.clone(); let diff = new Vector2().subVectors(center1,center2); model.position.x += diff.x; model.position.y += diff.y; model.boundCenter.copy(center1); }, maintainBoundCenter(model){ model.updateMatrixWorld(); let center1 = model.boundCenter.clone();//还未更新的 this.getBoundCenter(model);//更新 let center2 = model.boundCenter.clone(); let diff = new Vector3().subVectors(center1,center2); model.position.add(diff); model.boundCenter.copy(center1); }, modelTransformCallback(model){ model.updateMatrixWorld(); if(model.matrixWorld.equals(model.lastMatrixWorld))return viewer.scene.measurements.forEach(measure=>{ let changed; measure.points_datasets.forEach((dataset_id,i)=>{ if(dataset_id == model.dataset_id){ changed = true; measure.points[i] = Potree.Utils.datasetPosTransform({fromDataset:true,datasetId:dataset_id, position:measure.dataset_points[i].clone()}); measure.updateMarker(measure.markers[i], measure.points[i]); } }); if(changed){//仿transformByPointcloud measure.getPoint2dInfo(measure.points); measure.update(); measure.setSelected(false);//隐藏edgelabel } }); model.lastMatrixWorld = model.matrixWorld.clone(); viewer.dispatchEvent('content_changed'); viewer.mapViewer && Potree.settings.showObjectsOnMap && viewer.mapViewer.dispatchEvent('content_changed'); }, changeOpacity(model, opacity){ let isRoot = model.dataset_id != void 0; //是否是最外层 if(model.isPointcloud){ model.changePointOpacity(opacity); //MergeEditor.updateEdgeStrength() }else { //model.traverse(e=>e.material && setOp(e, opacity)) model.traverse(mesh=>{ if(mesh.material){ if(mesh.material.originOpacity == void 0 ){ mesh.material.originOpacity = mesh.material.opacity; } mesh.material.opacity = mesh.material.originOpacity * opacity; if(mesh.material.opacity<1){ mesh.material.transparent = true; /* if(model.isPointcloud){ mesh.changePointOpacity(realOpacity) }else{ mesh.material.opacity = realOpacity } */ mesh.renderOrder = Potree.config.renderOrders.model+1; //mesh.material.depthWrite = false }else { mesh.material.transparent = false; mesh.renderOrder = Potree.config.renderOrders.model; //mesh.material.depthWrite = true } mesh.material.depthWrite = mesh.material.opacity>0.3; } }); } isRoot && (model.opacity = opacity);//记录在最外层 viewer.dispatchEvent('content_changed'); }, modelAdded(model){ model.addEventListener('isVisible',(e)=>{ if(e.reason == "overlinePass")return //console.log(e) viewer.addEventListener('update',()=>{ //下一次更新结束后 this.updateMemoryUsage(); },{once:true}); }); this.updateMemoryUsage(); }, updateMemoryUsage(){ //obj暂时不管其贴图大小, 因为顶点造成的不仅是内存还有卡顿所以先只看顶点 const maxMemory = Potree.config.tiles3DMaxMemory + 100; //M 实际估计是这个的10倍 const eachObjPosWeight = 100/1000/1000; //M 每个顶点pos是3*4个字节?法线3*4和uv2*4 其实还有贴图 const eachCloudPointWeight = 12/1000/1000; //M 每个点 pos + 颜色 + 法线 大概 const eachVisiCPointWeight = eachCloudPointWeight * 5; // 或 maxMemory / (6*1000*1000) 大概值接近 (再除以一个数是因为显示的要比内存中的耗更多资源 const eachGltfPosWeight = 100/1000/1000; //M 每个顶点pos是3*4个字节?法线3*4和uv2*4 其实还有贴图 let posCount=0; viewer.objs.children.forEach(e=>{ if(e.fileType == 'glb' || e.fileType == 'obj'){ e.traverse((mesh)=>{ if(mesh.geometry){ posCount += mesh.geometry.attributes.position.count; } }); }else if(e.fileType == '3dTiles'){ } }); //获取点云的内存限制 let objWeight = posCount*eachObjPosWeight; let laserWeight = Potree.numVisiblePoints * eachCloudPointWeight; //点云实际显示所占大小 let laserMemoryWeight = Potree.lru.numPoints * eachCloudPointWeight; //点云所使用内存大小 let tiles3DWeight = viewer.tiles3dVisiVCount * eachGltfPosWeight; //M 3dTiles所占内存大小 let tiles3DMemoryWeight = viewer.tiles3dMemoryUsage / 1000 / 1000; //M 3dTiles显示的所占内存大小 /* let min = 0.1, max = 6, minP = 100, maxP = 1000000; let ratio = Math.round(math.linearClamp(score, minP, maxP, max, min )); */ let rest = maxMemory - objWeight - tiles3DWeight; Potree.pointBudget = Math.max(30000, Math.round(rest/eachVisiCPointWeight)); //获取3dTiles的内存限制 let tiles3DMaxMemory = maxMemory - Math.round(objWeight + laserWeight); Potree.settings.tiles3DMaxMemory = MathUtils.clamp(tiles3DMaxMemory , 30, Potree.config.tiles3DMaxMemory ); //还存在的问题:仍然有隐患,因为没用到真实缓存的大小: tiles3DMemoryWeight laserMemoryWeight, 它们比真实可见的要多。不使用是因为它们无法反应出实际需要的内存量,缓存是只增不减 //obj等普通mesh限制不了 //console.log('objWeight',objWeight.toFixed(1), 'laserMemoryWeight',laserMemoryWeight.toFixed(1), 'tiles3DWeight',tiles3DWeight.toFixed(1), 'pointBudget',Potree.pointBudget, 'tiles3DMaxMemory',tiles3DMaxMemory) //总内存 = 内存占用空间+图片缓存 , obj的缓存比较多在图片中 }, setGroundPlaneImg(src,scale,angle){//设置地面图 this.goundScale = scale || 1, this.goundAngle = angle || 0; let oldSrc = this.curGroundImgSrc; this.curGroundImgSrc = src; const ratio = 0.03; if(src){ if(oldSrc == src && this.groundPlane.material.map.image){ //仅修改大小 const s = ratio * this.goundScale; let {width, height} = this.groundPlane.material.map.image; this.groundPlane.scale.set(width*s, height*s); viewer.dispatchEvent('content_changed'); this.groundPlane.rotation.z = MathUtils.degToRad(this.goundAngle); return } let map = texLoader$a.load(src,(tex)=>{ if(this.curGroundImgSrc == src){ const s = ratio * this.goundScale; this.groundPlane.scale.set(tex.image.width*s, tex.image.height*s); this.groundPlane.rotation.z = MathUtils.degToRad(this.goundAngle); viewer.dispatchEvent('content_changed'); } }); Potree.Utils.makeTexDontResize(map); if(!this.groundPlane){ this.groundPlane = new Mesh(new PlaneBufferGeometry(1,1,1), new MeshBasicMaterial({ map, side : 2, })); viewer.scene.scene.add(this.groundPlane); this.groundPlane.position.z = 0.1; }else { this.groundPlane.material.map = map; } Potree.Utils.updateVisible(this.groundPlane,'show',true ); }else { this.groundPlane && Potree.Utils.updateVisible(this.groundPlane,'show',false ); } } }; /* note: 要注意getHoveredElements只在getIntersect时才使interactables包含加载的model, 也就是model上不能有使之成为interactables的事件,否则在鼠标hover到模型上开始转动的一瞬间很卡。 */ const texLoader$b = new TextureLoader(); const arrowSpacing = 1; //间隔 const arrowSize = arrowSpacing * 0.5; const planeGeo$3 = new PlaneBufferGeometry(1,1); const sphereSizeInfo = { nearBound : 0.1, farBound:25, minSize : 50, maxSize : 200 //scale:arrowSize, restricMeshScale : true, }; //const arrowsShowingCount = 25; //场景里最多展示多少个箭头 const arrowShowMinDis = 10; class RouteGuider extends EventDispatcher{ constructor () { super(); this.route = []; this.curve = []; this.scenePoints = []; this.sceneMeshGroup = new Object3D; this.mapMeshGroup = new Object3D; this.generateDeferred; viewer.addEventListener('loadPointCloudDone',this.init.bind(this)); this.lastResult;//保存上一个的结果,以便于反向 this.datasetIds = [];//起始和终点的datasetId } init(){ if(this.inited) return; let zoom, resolution=new Vector2; viewer.mapViewer.addEventListener('camera_changed', e => { if(!this.routeStart || !this.routeEnd) return var camera = e.viewport.camera; if(camera.zoom != zoom || !resolution.equals(e.viewport.resolution)){ Common.intervalTool.isWaiting('routeCameraInterval', ()=>{ //延时update,防止卡顿 if(camera.zoom != zoom || !resolution.equals(e.viewport.resolution)){ //console.log('updateMapArrows') this.updateMapArrows(true); zoom = camera.zoom; resolution.copy(e.viewport.resolution); } }, browser.isMobile()?500:200); } }); viewer.addEventListener('camera_changed', e => { if(!this.routeStart || !this.routeEnd || !e.changeInfo.positionChanged ) return Common.intervalTool.isWaiting('routeCameraInterval2', ()=>{ //延时update,防止卡顿 this.updateArrowDisplay(); }, 1000); }); var polesMats = { shadowMat: new MeshBasicMaterial({ transparent:true, depthTest:false, map: texLoader$b.load(Potree.resourcePath+'/textures/pano_instruction_bottomMarker.png' ) }), sphereMat : new MeshBasicMaterial({ transparent:true, depthTest:false, map: texLoader$b.load(Potree.resourcePath+'/textures/whiteCircle.png' ) }), hatMats:{ start: new MeshBasicMaterial({ transparent:true, depthTest:false, map: texLoader$b.load(Potree.resourcePath+'/textures/pano_instruction_start_route.png' ) }), end: new MeshBasicMaterial({ transparent:true, depthTest:false, map: texLoader$b.load(Potree.resourcePath+'/textures/pano_instruction_target_reached.png' ) }) } }; polesMats.shadowMat.map.anisotropy = 4; this.poleStart = this.createPole(polesMats, 'start'); this.poleEnd = this.createPole(polesMats, 'end'); this.sceneMeshGroup.add(this.poleStart); this.sceneMeshGroup.add(this.poleEnd); let map = texLoader$b.load(Potree.resourcePath+'/textures/routePoint_panorama.png' ); map.anisotropy = 4; // 各向异性过滤 .防止倾斜模糊 this.arrow = new Mesh(planeGeo$3, new MeshBasicMaterial({ transparent:true, depthTest:false, map })); this.arrow.scale.set(arrowSize,arrowSize,arrowSize); Potree.Utils.setObjectLayers(this.arrow, 'sceneObjects' ); /* this.testArrow = this.arrow.clone(); this.testArrow.material = this.arrow.material.clone() this.testArrow.material.color = 'red' */ this.arrows = new Object3D; this.sceneMeshGroup.add(this.arrows); Potree.Utils.setObjectLayers(this.sceneMeshGroup, 'sceneObjects' ); //this.sceneMeshGroup.traverse(e=>e.renderOrder = 90) viewer.scene.scene.add(this.sceneMeshGroup); this.sceneMeshGroup.visible = false; this.poleStart.visible = false; this.poleEnd.visible = false; //-------------map--------------------- /* this.mapMarkStart = new THREE.Mesh( planeGeo, new THREE.MeshBasicMaterial({ transparent:true, depthTest:false, map: texLoader.load(Potree.resourcePath+'/textures/map_instruction_start_route.png' ) })) this.mapMarkEnd = new THREE.Mesh( planeGeo, new THREE.MeshBasicMaterial({ transparent:true, depthTest:false, map: texLoader.load(Potree.resourcePath+'/textures/map_instruction_target_reached.png' ) })) this.mapMarkStart.renderOrder = this.mapMarkEnd.renderOrder = 2//在箭头之上 */ let map2 = texLoader$b.load(Potree.resourcePath+'/textures/routePoint_map_fsna.png' ); this.mapArrowMats = { default: new MeshBasicMaterial({ transparent:true, depthTest:false, map:map2, }), fade: new MeshBasicMaterial({ transparent:true, depthTest:false, map:map2, opacity:0.4 }), }; this.mapArrow = new Mesh( planeGeo$3, this.mapArrowMats.default); this.mapArrow.scale.set(arrowSize,arrowSize,arrowSize); this.mapArrows = new Object3D; this.mapArrows.name = 'mapArrows'; this.mapMeshGroup.add(this.mapArrows); this.mapMeshGroup.name = 'mapRouteLayer'; this.mapMeshGroup.visible = false; viewer.mapViewer.dispatchEvent({type:'add', object:this.mapMeshGroup, name:'route'}); this.mapArrow.layers.mask = this.mapArrows.layers.mask; // 修改成和map中的layer一样的 viewer.modules.SiteModel.bus.addEventListener('FloorChange',()=>{ if(this.routeStart && this.routeEnd){ this.updateOpacityAtMap(); } }); this.inited = true; } updateOpacityAtMap(){//只有当前楼层的透明度为1 var currentFloor = viewer.modules.SiteModel.currentFloor; //console.log('updateOpacityAtMap', currentFloor && currentFloor.name) const lift = 0.3; // 因为发送请求时用的是floorPosition的高度,而它可能会到画好的floor之下,所以有误差 this.mapArrows.children.forEach((arrow,index)=>{ let pos = this.mapPoints[index].clone(); pos.z += lift; let inSide = currentFloor && currentFloor.ifContainsPoint(pos); arrow.material = inSide ? this.mapArrowMats.default : this.mapArrowMats.fade; //console.log('arrow',index, arrow.material.opacity) }); viewer.mapViewer.dispatchEvent('content_changed'); }//但是如果楼层刚好只框柱相机位置而没框住地面位置就不好了…… createPole(polesMats, name){ const height = 1.5, sphereCount = 6, shadowSize = 0.5 /* sphereSizeInfo.scale */, sphereSize = 0.05; var group = new Object3D; group.name = 'pole_'+name; var shadow = new Mesh(planeGeo$3, polesMats.shadowMat); shadow.scale.set(shadowSize,shadowSize,shadowSize); var sliceDis = height / (sphereCount+1); group.add(shadow); for(let i=0;i -(Math.abs(e.position.z - viewer.images360.position.z)) ]); let pano = result && result[0] && result[0].item; return pano ? pano.floorPosition.z : viewer.bound.boundingBox.min.z + 1 //若在平面图上画实在得不到当前楼层的,大概率是楼层画得不好,那就只能去获取当前楼层的了 //navvis的高度取的是主视图所在楼层的中心高度(可能再高些) } setEndPole(pos){ this.endPolePos = pos; this.bus && this.bus.emit('reposEndMarker', pos); } getSourceProjectionIndex(route) {//真正的起始 var e = route.findIndex(function(t) { return t.instruction && t.instruction.type === 'source_projection_to_navgraph' }); return e < 0 ? 0 : e } getDestinationProjectionIndex(route) {//真正的终点 var e = route.findIndex(function(t) { return t.instruction && t.instruction.type === "destination_projection_to_navgraph" }); return e < 0 ? route.length - 1 : e } generateRoute(){ this.sceneMeshGroup.visible = true; if(!this.routeStart || !this.routeEnd){ return } let initialPointcloud = viewer.scene.pointclouds.find(e=>e.dataset_id == Potree.settings.originDatasetId); //array.reduce(function(total, currentValue, currentIndex, arr), initialValue) let create = ()=>{ this.routeLength = this.route.reduce((total, currentValue, currentIndex, arr)=>{ if(currentIndex == 0)return 0 return total + currentValue.distanceTo(arr[currentIndex-1]); },0); let count = Math.max(2,Math.round(this.routeLength / arrowSpacing));//点数 const curve = new CatmullRomCurve3( this.route ); curve.curveType = 'chordal';//'centripetal' 'catmullrom'这个可能会超出路径外 this.curve = curve; const scenePoints = curve.getSpacedPoints( count );//更平均 //const scenePoints = curve.getPoints( count ); scenePoints.splice(0,1);//去掉首尾 scenePoints.pop(); this.scenePoints = scenePoints; this.updateMapArrows(); this.displayRoute(); {//map focus on this area const minBound = new Vector2(1,1);//针对垂直线,在地图上只有一个点 let bound = new Box2; this.route.forEach(e=>{ bound.expandByPoint(e); }); let size = bound.getSize(new Vector2); let markerSize = new Vector2(115,40); //起始和终点的标识呈长方形 let areaSize = viewer.mapViewer.viewports[0].resolution2; let areaArea = areaSize.x * areaSize.y; if(areaArea> 800 * 400){//是放大的 markerSize.multiplyScalar(areaArea / (800 * 400) /* / (size.x * size.y) */); } let margin = size.clone().divide(viewer.mapViewer.viewports[0].resolution2).multiply(markerSize); ///边距 重点是起始和终点的标识占据较大 size.add(margin); let center = bound.getCenter(new Vector2); size.x = Math.max(size.x, minBound.x ); size.y = Math.max(size.y, minBound.y ); let duration = 1000; viewer.mapViewer.moveTo(center, size, duration); } this.bus.emit('gotResult', {dis:this.routeLength}); /* this.generateDeferred && this.generateDeferred.resolve({dis:this.routeLength}) this.generateDeferred = null */ }; if(Potree.fileServer){ let dealData = (data)=>{ if(!data.data){ console.log('没有数据'); let result; if(data && data.code == 4002){ result = data;//正被修改数据集 }else if(this.routeStart.distanceTo(this.routeEnd) < 1){ result = { code: 500, msg: '距离太短,无法规划路线' }; }else { result = { code: 500, msg: '超出数据集范围,无法规划路线' }; } this.clearRoute(); this.setStartPole(this.routeStart); this.setEndPole(this.routeEnd); this.displayRoute(); //还是要显示一下起始 this.bus && this.bus.emit('gotResult', result ); return //this.generateDeferred && this.generateDeferred.resolve() } data = data.data; this.clearRoute(); let length = data.length; if(length < 2){//可能距离太短 console.log('路径点数为'+length+',直接取起点和终点连线'); this.route = [this.routeStart, this.routeEnd]; }else { let startIndex = this.getSourceProjectionIndex(data); let endIndex = this.getDestinationProjectionIndex(data); let effectiveItems = data.slice(startIndex, endIndex + 1 );//只要点云范围内的点 effectiveItems.forEach((item,i)=>{ //let pos = viewer.transform.lonlatToLocal.forward(item.location.slice(0)/* ,true */) let pos = item.location.slice(0); pos = new Vector3().fromArray(pos);//.setZ(item.z) pos.z -= initialPointcloud.datasetData.location[2]; this.route.push(pos); }); //console.log('route', this.route) } this.setStartPole(this.route[0]); this.setEndPole(this.route[this.route.length-1]); create(); /* distance: 0.17581000000000116 distance_to_previous: 0.17581000000000116 id: 567 instruction: {type: 'source_projection_to_navgraph'} latitude: 22.366605927999238 location: (3) [113.5957510575092, 22.366605927999238, -1.12419] longitude: 113.5957510575092 z: -1.12419 */ }; if(this.lastResult && (this.lastResult.data || this.lastResult.data.code != 4002)){//正被修改数据集的话要重新计算 let data = Common.CloneObject(this.lastResult.data) , use; //直接用上次的结果 if(this.lastResult.routeStart.equals(this.routeStart) && this.lastResult.routeEnd.equals(this.routeEnd)){//和上次请求相同 use = true; }else if(this.lastResult.routeStart.equals(this.routeEnd) && this.lastResult.routeEnd.equals(this.routeStart)){//..反向 use = true; if(data.data){ data.data = this.lastResult.data.data.slice(0).reverse(); } } if(use){ console.log('直接用上次的结果'); return setTimeout(()=>{dealData(data);}, 1)//延迟是为了等待获得 RouteGuider.generateDeferred } } let start = this.routeStart.clone(); let end = this.routeEnd.clone(); //let startLonlat = viewer.transform.lonlatToLocal.inverse(start/* , true */) //let endLonlat = viewer.transform.lonlatToLocal.inverse(end/* , true */) /* var query = { source_longitude: start.x, source_latitude: start.y, source_z: start.z, destination_longitude: end.x, destination_latitude: end.y, destination_z: end.z }; */ var query = { source_longitude: start.x, source_latitude: start.y, source_z: start.z+initialPointcloud.datasetData.location[2], destination_longitude: end.x, destination_latitude: end.y, destination_z: end.z+initialPointcloud.datasetData.location[2] }; /* var query = { source_longitude: startLonlat.x, source_latitude: startLonlat.y, source_z: start.z, destination_longitude: endLonlat.x, destination_latitude: endLonlat.y, destination_z: end.z }; */ //let url = `/laser/route/${Potree.settings.number}/getRoute/${this.datasetIds[0]}/${this.datasetIds[1]}?` let url = `/laser/route/${Potree.settings.number}/getRoute/${Potree.settings.originDatasetId}?`; for(let i in query){ url+= (i + '='+ query[i] +'&'); } Potree.fileServer.get(url).then((data)=>{ //console.log('data', data.data) if(!this.routeStart || !this.routeEnd)return this.lastResult = {//保存数据 routeStart : this.routeStart.clone(), routeEnd: this.routeEnd.clone(), data, }; dealData(data); }); }else { //创个直线 /* const sliceDis = 1 let dis = this.routeStart.distanceTo(this.routeEnd); let count = Math.max(2,Math.round(dis / sliceDis))//点数 let realSlideDis = dis / (count-1); let dir = new THREE.Vector3().subVectors(this.routeEnd, this.routeStart).normalize().multiplyScalar(realSlideDis); this.route = [this.routeStart]; for(let i=0;i 300; let count = Math.max(2,Math.round(this.routeLength * zoom / arrowSpacing / (isBig?35:30)));//点数 if(count == this.mapPoints.length+1)return//没变 const mapPoints = this.curve.getSpacedPoints( count ); mapPoints.splice(0,1);//去掉首尾 mapPoints.pop(); this.mapPoints = mapPoints; var scale = (isBig ? 26 : 22)/zoom; this.mapArrow.scale.set(scale,scale,scale); /* this.mapMarkStart.scale.set(scale,scale,scale) this.mapMarkEnd.scale.set(scale,scale,scale) */ if(ifReset){//因为缩放而重新排布箭头 this.clearRoute({resetMap:true}); this.displayRoute({resetMap:true}); } this.updateOpacityAtMap(); } updateArrowDisplay(){//根据当前位置更新显示一定范围内的箭头 if(this.scenePoints.length == 0)return /* var a = Common.sortByScore(this.scenePoints , null, [(point)=>{ //是否还要再requires里限制最远距离? var playerPos = viewer.scene.getActiveCamera().position.clone().setZ(0) var pos = point.clone().setZ(0) return -pos.distanceTo(playerPos); }]); //获得展示的起始点 let start = a[0].item let startIndex = this.scenePoints.indexOf(start) this.arrows.children.forEach((e,i)=>{ if(istartIndex+arrowsShowingCount)e.visible = false else e.visible = true }) */ let cameraPos = viewer.scene.getActiveCamera().position; this.arrows.children.forEach((e,i)=>{ if(e.position.distanceTo(cameraPos) < arrowShowMinDis) e.visible = true; else e.visible = false; }); viewer.dispatchEvent('content_changed'); } displayRoute(o={}){ if(!o.resetMap){ this.poleStart.position.copy(this.startPolePos || this.routeStart); this.poleEnd.position.copy(this.endPolePos || this.routeEnd); /* this.mapMarkStart.position.copy(this.routeStart).setZ(0) this.mapMarkEnd.position.copy(this.routeEnd).setZ(0) */ this.scenePoints.forEach(e=>this.addArrow(e)); this.arrows.children.forEach((e,i)=>this.setArrowDir(this.arrows.children,i)); } this.sceneMeshGroup.visible = true;//.traverse(e=>e.visible = true) this.mapMeshGroup.visible = true; this.mapPoints.forEach(e=>this.addMapArrow(e)); this.mapArrows.children.forEach((e,i)=>this.setArrowDir(this.mapArrows.children,i)); viewer.mapViewer.dispatchEvent({'type':'content_changed'}); this.updateArrowDisplay(); } clearRoute(o={}){ if(!o.resetMap){ this.routeLength = 0; this.route = []; this.scenePoints = []; this.mapPoints = []; let arrows = this.arrows.children.slice(0); arrows.forEach(e=>{ this.arrows.remove(e); }); } let mapArrows = this.mapArrows.children.slice(0); mapArrows.forEach(e=>{ this.mapArrows.remove(e); }); this.sceneMeshGroup.visible = false; //this.sceneMeshGroup.traverse(e=>e.visible = false) //包括sprite也要设置,防止update this.mapMeshGroup.visible = false; viewer.mapViewer.dispatchEvent({'type':'content_changed'}); viewer.dispatchEvent('content_changed'); } clear(){//退出 console.log('导航clear'); this.routeStart = null; this.routeEnd = null; this.clearRoute(); } } //大概每十米要花一秒 /* 存在的问题: 路径不准确。起始点和终点偏移。 https://uat-laser.4dkankan.com/routeDebug/ 可查整个map的通路点位图 */ //import History from "../../utils/History.js" const cameraProps$1 = [ { name : 'top', axis:["x","y"], direction : new Vector3(0,0,-1), //镜头朝向 openCount:0, } ]; class Clipping extends EventDispatcher{ //实时剪裁 constructor(){ super(); this.views = {}; this.cameras = {}; this.orthoCamera = new OrthographicCamera(-100, 100, 100, 100, 0.01, 10000); this.orthoCamera.up.set(0,0,1); } init(){ if(this.inited)return this.initViews(); this.inited = true; this.prepareRecord = true; this.activeViewName = 'mainView'; this.events = { transfromCallback:(e)=>{//拖拽变化时 this.adjustCamHeight(); //检测漫游点、回退等 /* if(this.prepareRecord){ let box = viewer.transformationTool.selection[0] this.history.writeIn({box, matrix:box.matrix.clone()}) this.prepareRecord = false } */ }, /* onTransfromEnd:(e)=>{//拖拽结束、松开 this.prepareRecord = true }, */ selectCallback:(e)=>{ this.adjustCamHeight(); let unableNavigate = this.activeViewName != 'mainView' || e.selection.length > 0; if(Potree.settings.unableNavigate && !unableNavigate){ setTimeout(()=>{ Potree.settings.unableNavigate = this.activeViewName != 'mainView' || e.selection.length > 0; },300);//延迟是因为点击时取消选择后可能立即就会触发flyToPano。 而且有的人喜欢点两下 }else Potree.settings.unableNavigate = unableNavigate; }, onkeydown:(e)=>{ if(e.keyCode == 8 || e.keyCode == 46){// Backspace or Delete viewer.inputHandler.selection[0] && viewer.scene.removeVolume(viewer.inputHandler.selection[0]); } } }; /* this.history = new History({ //也可以写到全局,但需要加个判断物品是否存在的函数 applyData: (data)=>{ if(viewer.scene.volumes.includes(data.box)){ data.matrix.decompose( data.box.position, data.box.quaternion, data.box.scale ); }else{ this.history.undo()//找不到就回退下一个。(直接写这?) } } }) */ } initViews(){ this.splitScreenTool = new SplitScreen; for(let i=0;i<1;i++){ let prop = cameraProps$1[i]; let view = new ExtendView(); this.views[prop.name] = view; this.cameras[prop.name] = this.orthoCamera; view.direction = prop.direction; } this.views.mainView = viewer.mainViewport.view; this.cameras.mainView = viewer.mainViewport.camera; } switchView(name){//替换view和camera到mainViewport if(this.activeViewName == name)return let view = this.views[name]; let camera = this.cameras[name]; let prop = cameraProps$1.find(e=>e.name == name); let {boundSize, center, boundingBox} = viewer.bound; this.lastViewName = this.activeViewName; this.activeViewName = name; let lastView = this.views[this.lastViewName]; let lastCamera = this.cameras[this.lastViewName]; viewer.mainViewport.view = view; viewer.mainViewport.camera = camera; if(lastCamera)lastView.zoom = lastCamera.zoom; /* if(lastView){//2d->3d view.copy(lastView) } */ if(name == 'mainView'){ Potree.settings.unableNavigate = false; /* viewer.transformationTool.handles['scale.z+'].node.visible = true viewer.transformationTool.handles['scale.z-'].node.visible = true */ }else { Potree.settings.unableNavigate = true; /* viewer.transformationTool.handles['scale.z+'].node.visible = false viewer.transformationTool.handles['scale.z-'].node.visible = false */ if(prop.openCount == 0){//至多执行一次 //this.viewportFitBound(name, boundSize, center) this.orthoMoveFit(center, {bound:boundingBox}, 0); this.camHeightOutOfModel = view.position.z; //记录下此刻相机高度。 } prop.openCount++; this.adjustCamHeight(); /* this.targetPlane.setFromNormalAndCoplanarPoint( view.direction.clone(), center ) this.targetPlane.projectPoint(view.position, this.shiftTarget ) //target转换到过模型中心的平面,以保证镜头一定在模型外 view.position.copy(this.splitScreenTool.getPosOutOfModel(viewer.mainViewport)) */ if(view.zoom)camera.zoom = view.zoom;//恢复上次的zoom } viewer.updateScreenSize({forceUpdateSize:true});//更新camera aspect left等 if(viewer.inputHandler.selection.length){ this.focusOnObject(viewer.inputHandler.selection[0]); } } focusOnObject(box, duration=0){ if(this.activeViewName == 'mainView'){ viewer.focusOnObject({boundingBox:box.boundingBox.clone().applyMatrix4(box.matrixWorld)}, 'boundingBox', duration); }else { this.orthoMoveFit(box.position, {bound:box.boundingBox.clone().applyMatrix4(box.matrixWorld)}, duration); } this.adjustCamHeight(); } orthoMoveFit(pos, info, duration){ var margin = {x:viewer.mainViewport.resolution.x*0.4, y:viewer.mainViewport.resolution.y*0.4}; this.splitScreenTool.viewportFitBound(viewer.mainViewport, info.bound, pos, duration, margin ); } adjustCamHeight(){ if(this.activeViewName != 'top')return let view = this.views.top; let height; if(viewer.inputHandler.selection.length){ //相机高度位于选中的box的顶部 let box = viewer.inputHandler.selection[0]; height = box.boundingBox.clone().applyMatrix4(box.matrixWorld).max.z; }else { height = this.camHeightOutOfModel; //显示全部点云 } view.position.z = height; //console.log('adjustCamHeight',height) //缺点:1 会导致缩放很小的时候,transformationTool的轴因放大到了相机背面。(只有scale轴做了处理) //2 无法直接切换 看不到的box,但可以先取消选择 //3 但是俯视图中无法切换到被上层盖住的box(不过把俯视图作为辅助,只针对单个box调动的话,问题不大) } enter(){ this.init(); viewer.transformationTool.setModeEnable(['translation']); //viewer.transformationTool.handles['rotation.x'].node.visible = false viewer.transformationTool.frame.material.visible = false; //不盖住boxVolume的frame this.targetPlane = viewer.mainViewport.targetPlane = new Plane(); this.shiftTarget = viewer.mainViewport.shiftTarget = new Vector3; //project在targetPlane上的位置 this.getAllBoxes().forEach(box=>{ Potree.Utils.updateVisible(box,'hidden',true); //显现 }); viewer.transformationTool.history.clear(); viewer.transformationTool.addEventListener('transformed', this.events.transfromCallback); //viewer.transformationTool.addEventListener('stopDrag', this.events.onTransfromEnd) viewer.inputHandler.addEventListener('selection_changed', this.events.selectCallback); viewer.inputHandler.addEventListener('keydown', this.events.onkeydown); this.setPointLevelAuto(); var initialPointcloud = viewer.scene.pointclouds.find(p => p.dataset_id == Potree.settings.originDatasetId); //隐藏 初始数据集以外的数据集 viewer.scene.pointclouds.forEach(e=>{ if(e.dataset_id!=Potree.settings.originDatasetId){ Potree.Utils.updateVisible(e,'enterClipping',false); //Potree.settings.floorplanEnables[e.dataset_id] = false e.panos.forEach(pano=>pano.setEnable(false)); //禁止漫游 }else { Potree.Utils.updateVisible(e,'enterClipping',true, 1, 'add'); //Potree.settings.floorplanEnables[e.dataset_id] = true } }); viewer.flyToDataset({ pointcloud : initialPointcloud, duration:0}); } enter2(){//在土方量界面的 viewer.transformationTool.setModeEnable(['translation']); viewer.transformationTool.frame.material.visible = false; //不盖住boxVolume的frame viewer.transformationTool.history.clear(); //viewer.inputHandler.addEventListener('keydown', this.events.onkeydown) } leave(){ viewer.transformationTool.setModeEnable(['scale', 'translation', 'rotation'] ); viewer.transformationTool.frame.material.visible = true; //恢复 this.switchView( 'mainView' ); this.getAllBoxes().forEach(box=>{ Potree.Utils.updateVisible(box,'hidden',false);//隐身 }); viewer.transformationTool.removeEventListener('transformed', this.events.transfromCallback); //viewer.transformationTool.removeEventListener('stopDrag', this.events.onTransfromEnd) viewer.inputHandler.removeEventListener('selection_changed', this.events.selectCallback); //viewer.inputHandler.removeEventListener('keydown', this.events.onkeydown) viewer.transformObject(null); viewer.transformationTool.history.clear(); //恢复 初始数据集以外的数据集 viewer.scene.pointclouds.forEach(e=>{ if(e.dataset_id!=Potree.settings.originDatasetId){ Potree.Utils.updateVisible(e,'enterClipping',true); e.panos.forEach(pano=>pano.setEnable(true)); }else { Potree.Utils.updateVisible(e,'enterClipping',false, 0, 'cancel'); } }); } setTranMode(mode){//rotate or translate this.tranMode = mode; viewer.transformationTool.setModeEnable([mode]); viewer.dispatchEvent('content_changed'); } //问:是否要显示其他数据集 setPointLevelAuto(){ /* let visiCount = viewer.images360.panos.length let maxCount = 200, minCount = 20, minPer = 0.7, maxPer = 1 let percent = maxPer - ( maxPer - minPer) * THREE.Math.clamp((visiCount - minCount) / (maxCount - minCount),0,1) Potree.settings.UserDensityPercent = percent ---还是不限制了,尤其是平面图希望更细致点,毕竟剪裁主要要看清剪裁的部位。 */ viewer.setPointBudget(5*1000*1000); //给个中等到高等之间的质量 Potree.settings.sizeFitToLevel = true; viewer.setPointLevels(); } getAllBoxes(){ return viewer.scene.volumes.filter(v=>v.clip && v instanceof Potree.BoxVolume ) } getCalcData(){//给后台矩阵数据,以裁剪点云。 let Clip = viewer.modules.Clip; //裁剪下载模块 let data = { transformation_matrix: viewer.scene.pointclouds.filter(p=>p.dataset_id == Potree.settings.originDatasetId).map((cloud)=>{ let data = { id: cloud.dataset_id, matrix : new Matrix4().elements, //参照downloadNoCrop,给默认值,表示没有最外层裁剪 visiMatrixes: cloud.material.clipBoxes_in.filter(e=>!e.box.isNew).map(e=>Clip.getTransformationMatrix(cloud, e.inverse).elements), unVisiMatrixes: cloud.material.clipBoxes_out.filter(e=>!e.box.isNew).map(e=>Clip.getTransformationMatrix(cloud, e.inverse).elements), modelMatrix: new Matrix4().elements //(new THREE.Matrix4).copy(cloud.transformMatrix).transpose().elements //需要保证没有位移,否则剪裁后的模型位置会变化 }; return data }) , aabb: "b-12742000 -12742000 -12742000 12742000 12742000 12742000" //剪裁空间 }; return data } saveClipData(boxes){//输出所有的clip volumeBox let oldState = !viewer.clipUnabled; //viewer.setClipState(true) let data = (boxes || this.getAllBoxes()).filter(e=>!e.isNew).map(volume=>{ return { clipTask: volume.clipTask, position: Potree.Utils.datasetPosTransform({position:volume.position, toDataset: true, datasetId: Potree.settings.originDatasetId}).toArray(), rotation: Potree.Utils.datasetRotTransform({rotation:volume.rotation, toDataset: true, datasetId: Potree.settings.originDatasetId, getRotation:true}).toArray().slice(0,3), scale: volume.scale.toArray(), } }); //console.log(data) //console.log(JSON.stringify(data)) //viewer.setClipState(oldState) return data } loadFromData(data=[]){ data.forEach(v=>{ let volume = new Potree.BoxVolume({clip:true, clipTask:v.clipTask}); volume.scale.fromArray(v.scale); volume.position.fromArray(v.position); volume.rotation.fromArray(v.rotation); volume.position.copy(Potree.Utils.datasetPosTransform({position:volume.position, fromDataset: true, datasetId:Potree.settings.originDatasetId})); volume.rotation.copy(Potree.Utils.datasetRotTransform({rotation:volume.rotation, fromDataset: true, datasetId:Potree.settings.originDatasetId, getRotation:true})); viewer.scene.addVolume(volume); viewer.volumeTool.scene.add(volume); }); } } //注意:实时裁剪只对初始数据集有效,其他数据集已经隐藏 /* 备注: 2023 发送给后台数据样例: {"transformation_matrix":[{"id":"1626189981883699200","matrix":[1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1],"visiMatrixes":[],"unVisiMatrixes":[[0.18610746638254325,-0.0005022517888587003,0,-0.01638495865566026,0.0002721420073982042,0.10084117292688818,0,0.25897037181878035,0,0,0.17066427841288187,-0.1913158687770112,0,0,0,1]],"modelMatrix":[0.999996358477288,-0.0026987093514294095,0,3.275872500136923e-8,0.0026987093514294095,0.999996358477288,0,-4.2486927309681385e-9,0,0,1,0,0,0,0,1]}],"aabb":"b-12742000 -12742000 -12742000 12742000 12742000 12742000"} 发送给算法部的数据样例: { "model": [ { "cut_transformation": "1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0", "file": "/mnt/data/pvt000004/1079784714115481600/pvt000004_202302271512444930_laserData/laserData/laser.las", "visiMatrixes": [[..],[..]], "dataSetId": "1630105213031026688", "sceneCode": "SS-t-MVQ1Jb80oMG", "modelMatrix": "-0.9338946595415051 -0.35754826930060274 0.0 -3.483651134875185E-8 0.35754826930060274 -0.9338946595415051 0.0 5.664923641290965E-9 0.0 0.0 0.9999999999999999 0.0 0.0 0.0 0.0 1.0", "unVisiMatrixes": [[..],[..]] } ], "aabb": "b-12742000 -12742000 -12742000 12742000 12742000 12742000" } */ const vertexShader = ` attribute float randam; attribute float sprite; attribute float centerHeight; //add //uniform float fireHeight; //add uniform float time; uniform float size; uniform float heightOfNearPlane; //varying float heightRatio; varying float vSprite; varying float vOpacity; float PI = 3.14; float quadraticIn( float t ) { float tt = t * t; return tt * tt; //变化曲线 越来越快 } void main() { float progress = fract( time + ( 2.0 * randam - 1.0 ) ); float progressNeg = 1.0 - progress; float ease = quadraticIn( progress ); float influence = sin( PI * ease ); //vec3 newPosition = position * vec3( 1.0, 1.0 , ease); vec3 newPosition = position; newPosition.z = (newPosition.z - centerHeight) * ease + centerHeight; gl_Position = projectionMatrix * modelViewMatrix * vec4( newPosition, 1.0 ); gl_PointSize = ( heightOfNearPlane * size ) / gl_Position.w; vOpacity = min( influence * 4.0, 1.0 ) * progressNeg; vSprite = sprite; //heightRatio = (newPosition.z - centerHeight) / fireHeight ; } `; const fragmentShader = ` uniform vec3 color; uniform sampler2D u_sampler; varying float vSprite; varying float vOpacity; //varying float heightRatio; void main() { vec2 texCoord = vec2(gl_PointCoord.x * 0.25 + vSprite, gl_PointCoord.y); gl_FragColor = vec4( texture2D( u_sampler, texCoord ).xyz * color * vOpacity, 1.0 ); } `; // import { vertexShader, fragmentShader } from './shaders' let ONE_SPRITE_ROW_LENGTH = 0.25; //对应着色器的0.25 let texture; const getTexture = ()=>{ if(!texture){ texture = new TextureLoader().load( Potree.resourcePath+'/textures/fire.png'); } return texture }; const boxGeo = new BoxBufferGeometry(1,1,1,1); const boxMat = new MeshBasicMaterial({wireframe:true, color:"#ffffff"}); class FireParticle extends Points{ constructor (prop) { super(); for ( var key in prop ){ this[key] = prop[key]; } this.strength = this.strength || 1; this.radius = prop.radius || 1; this.height = prop.height || 5; this.computeParams(); this.geometry = this.createGeometry( this.radius, this.height, this.particleCount ); if(this.color == void 0) this.color = 0xff3200; this.createMaterial( ); //小蓝火:0x00338f //---?: this.velocity = new Vector3(); this.acceleration = new Vector3(); this.angle = 0; this.angleVelocity = 0; this.angleAcceleration = 0; this.size = 16; this.opacity = 1; this.age = 0; this.alive = 0; this.sizeTween = null; this.colorTween = null; this.opacityTween = null; this.setSize({viewport:viewer.mainViewport}); this.setFov(viewer.fov); let setSize = (e)=>{ if(e.viewport.name != "MainView")return this.setSize(e); }; let setFov = (e)=>{ this.setFov(e.fov); }; viewer.addEventListener('resize',setSize); viewer.addEventListener('fov_changed',setFov); this.addEventListener('dispose',()=>{ viewer.removeEventListener('resize',setSize); viewer.removeEventListener('fov_changed',setFov); }); } computeParams(){ let length = (this.curve ? this.curve.wholeLength : 0) + this.radius * 2; //加上首尾的半径 const minSize = 0.3, maxSize = 3, minRadiusBound = 0.3, maxRadiusBound = 10; this.size = minSize + (maxSize - minSize) * MathUtils.smoothstep(this.radius, minRadiusBound, maxRadiusBound); //console.log('fire material particle size:', size ) this.particleCount = Math.ceil( length * Math.sqrt(this.strength * this.height ) * this.radius / (this.size * this.size) * 25 ); //console.log('fire particleCount',this.particleCount) } getPointsForBound(){ return this.boundPoints; //可以用于expand实时bound的点, 不含particle的size等边距 } getBound(points){ // points为生成点(圆心) this.boundPoints = []; let boundingBox = new Box3(); let margin = this.size * 0.13 + 0.3; points.forEach(bottom=>{ let top = bottom.clone(); top.z += this.height; boundingBox.expandByPoint(bottom); boundingBox.expandByPoint(top); this.boundPoints.push(bottom,top); }); let xyExpand = this.radius+margin; boundingBox.expandByVector(new Vector3(xyExpand,xyExpand,margin)); this.boundingBox = boundingBox; /* if(!this.debugBox){ this.debugBox = new THREE.Mesh(boxGeo, boxMat) this.add(this.debugBox) } this.debugBox.scale.copy(boundingBox.getSize(new THREE.Vector3)) this.debugBox.position.copy(boundingBox.getCenter(new THREE.Vector3)) */ } createGeometry( radius, height, particleCount){ let geometry = new BufferGeometry(); let count , points; if(this.positions.length>1){ const spaceDis = 0.2;//间隔距离 count = Math.ceil(this.curve.wholeLength / spaceDis) + 1; //console.log('count', count) points = this.curve.getSpacedPoints( count ); //得到的数量会比count多一个 count = points.length; //得到的点不太均匀,两端容易点少。 this.getBound(points); }else { this.getBound(this.positions); } var position = new Float32Array(particleCount * 3); var randam = new Float32Array(particleCount); var sprite = new Float32Array(particleCount); var centerHeight = new Float32Array(particleCount); for (var i = 0; i < particleCount; i++) { var center = new Vector3().copy(this.positions.length>1 ? points[Math.floor(i/particleCount * count)] : this.positions[0]); centerHeight[i] = center.z; if (i === 0) { // to avoid going out of Frustum position[i * 3 + 0] = center.x; position[i * 3 + 1] = center.y; position[i * 3 + 2] = center.z; }else { var r = Math.sqrt(Math.random()) * radius; var angle = Math.random() * 2 * Math.PI; position[i * 3 + 0] = center.x + Math.cos(angle) * r; position[i * 3 + 1] = center.y + Math.sin(angle) * r; position[i * 3 + 2] = center.z + (radius - r) / radius * height/2 + height/2; //不太明白这句为什么能达到height高度 sprite[i] = 0.25 * (Math.random() * 4 | 0); randam[i] = Math.random(); //center在底部 } } geometry.setAttribute('centerHeight', new BufferAttribute(centerHeight, 1)); geometry.setAttribute('position', new BufferAttribute(position, 3)); geometry.setAttribute('randam', new BufferAttribute(randam, 1)); geometry.setAttribute('sprite', new BufferAttribute(sprite, 1)); return geometry; } updateGeometry(){ this.computeParams(); this.geometry.dispose(); this.geometry = this.createGeometry( this.radius, this.height, this.particleCount ); this.material.uniforms.size.value = this.size; } createMaterial(){ const material = new ShaderMaterial( { uniforms:{ color: { type: "c", value: new Color(this.color) }, size: { type: "f", value: this.size}, u_sampler: { type: "t", value: getTexture() }, time: { type: "f", value: 0.0 }, heightOfNearPlane: { type: "f", value:0}, //相对far ,以确保画面缩放时点的大小也会缩放 height :{ type: "f", value:this.height} , }, vertexShader, fragmentShader, blending: AdditiveBlending, //加法融合模式 glBlendFunc(GL_ONE, GL_ONE) depthTest: true, depthWrite: false, transparent: true } ); this.material = material; this.setPerspective(this.fov, this.screenHeight); } setSize(e){ let viewport = e.viewport; this.screenHeight = viewport.resolution.y; this.setPerspective(this.fov, this.screenHeight); } setFov(fov){ this.fov = fov; this.setPerspective(this.fov, this.screenHeight); } setPerspective(fov, height){ //this.uniforms.heightOfNearPlane.value = Math.abs(height / (2 * Math.tan(THREE.Math.degToRad(fov * 0.5)))); let far = Math.abs(height / (2 * Math.tan(MathUtils.degToRad(fov * 0.5)))); this.material.uniforms.heightOfNearPlane.value = far; } update(delta){ if(!Potree.Utils.getObjVisiByReason(this,'force')){//被手动隐藏了 return } if(!Potree.Utils.isInsideFrustum(this.boundingBox, viewer.scene.getActiveCamera())){ Potree.Utils.updateVisible(this,'isInsideFrustum', false ); //不在视野范围 //console.log('unvi') return }else { Potree.Utils.updateVisible(this,'isInsideFrustum', true ); } delta *= 1;//更改速度 this.material.uniforms.time.value = (this.material.uniforms.time.value + delta) % 1; viewer.dispatchEvent('content_changed'); } dispose(){ this.geometry.dispose(); this.material.dispose(); this.dispatchEvent('dispose'); } } class Tween { constructor(times, values) { this.times = times || []; this.values = values || []; } lerp(t) { if(this.times.length == 0) return let i = 0, n = this.times.length; while(i < n && t > this.times[i]) i++; if(i == 0) return this.values[0] if(i == n) return this.values[n-1] const ratio = (t - this.times[i-1]) / (this.times[i] - this.times[i-1]); if(this.values[0] instanceof Vector3) { return this.values[i-1].clone().lerp(this.values[i], ratio) } else { return this.values[i-1] + ratio * (this.values[i] - this.values[i-1]) } } clone () { return Common.CloneClassObject(this) } } class Particle$1{ constructor(prop={}){ this.position = new Vector3(); this.velocity = new Vector3(); // units per second this.angle = 0; this.angleVelocity = 0; // degrees per second this.angleAcceleration = 0; // degrees per second, per second this.size = 16.0; this.color = new Color(); this.opacity = 1.0; this.age = 0; this.alive = 0; // use float instead of boolean for shader purposes this.lastChangeVage = 0; //add this.sizeTween = prop.sizeTween || new Tween( [0, 1], [32, 128] ); this.opacityTween = prop.opacityTween || new Tween( [0.8, 2], [0.5, 0] ); this.colorTween = prop.colorTween || new Tween( [0.4, 1], [ new Vector3(0,0,0.2), new Vector3(0, 0, 0.5) ] ); } update(dt) { this.position.add(this.velocity.clone().multiplyScalar(dt)); this.velocity.multiplyScalar( 1+this.acceleration*dt ); // convert from degrees to radians: 0.01745329251 = Math.PI/180 this.angle += this.angleVelocity * 0.01745329251 * dt; this.angleVelocity += this.angleAcceleration * 0.01745329251 * dt; this.age += dt; // if the tween for a given attribute is nonempty, // then use it to update the attribute's value if ( this.sizeTween.times.length > 0 ) this.size = this.sizeTween.lerp( this.age/this.deathAge ); if ( this.colorTween.times.length > 0 ) { var colorHSL = this.colorTween.lerp( this.age/this.deathAge ); this.color = new Color().setHSL( colorHSL.x, colorHSL.y, colorHSL.z ); } if ( this.opacityTween.times.length > 0 ) { this.opacity = this.opacityTween.lerp( this.age/this.deathAge); } viewer.dispatchEvent('content_changed'); } } const vertexShader$1 = ` attribute vec3 customColor; attribute float customOpacity; attribute float customSize; attribute float customAngle; attribute float customVisible; uniform float heightOfNearPlane; varying vec4 vColor; varying float vAngle; void main() { if ( customVisible > 0.5 ) vColor = vec4( customColor, customOpacity ); else vColor = vec4(0.0, 0.0, 0.0, 0.0); vAngle = customAngle; vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 ); //gl_PointSize = customSize * ( 300.0 / length( mvPosition.xyz ) ); gl_Position = projectionMatrix * mvPosition; gl_PointSize = ( heightOfNearPlane * customSize ) / gl_Position.w; } `; const fragmentShader$1 = ` uniform sampler2D u_sampler; varying vec4 vColor; varying float vAngle; void main() { gl_FragColor = vColor; float c = cos(vAngle); float s = sin(vAngle); vec2 rotatedUV = vec2(c * (gl_PointCoord.x - 0.5) + s * (gl_PointCoord.y - 0.5) + 0.5, c * (gl_PointCoord.y - 0.5) - s * (gl_PointCoord.x - 0.5) + 0.5); vec4 rotatedTexture = texture2D( u_sampler, rotatedUV ); gl_FragColor = gl_FragColor * rotatedTexture; } `; const Type = Object.freeze({ "CUBE":1, "SPHERE":2 }); let particleTexture; const getTexture$1 = ()=>{ if(!particleTexture){ particleTexture = new TextureLoader().load( Potree.resourcePath+'/textures/smokeparticle.png'); } return particleTexture }; const boxGeo$1 = new BoxBufferGeometry(1,1,1,1); const boxMat$1 = new MeshBasicMaterial({wireframe:true, color:"#ffffff"}); const defaults = { positions: [], positionStyle : "sphere", positionBase : new Vector3( 0, 0, 0 ), positionSpread : new Vector3( 1, 1, 0), //cube radius : 1, // sphere velocityStyle : 'cube', velocityBase : new Vector3( 0, 0, 0.5), // cube 基础速度 velocitySpread : new Vector3( 1, 1, 0.3), accelerationBase : 0.3, //基础加速度 accelerationSpread : 0.6, //没使用 speedBase : 0.1, //sphere speedSpread : 0.5, angleBase : 0, angleSpread : 360, angleVelocityBase : 1, angleVelocitySpread : 30, angleAccelerationBase : 1, angleAccelerationSpread : 5, sizeBase : 0, sizeSpread : 0, sizeTween : [[0, 0.3, 1], [0.3, 1.4, 6 ]], colorBase : new Vector3(0.0, 1.0, 0.5), colorSpread : new Vector3(0.0, 0.0, 0.0), colorTween : new Tween( [0.2, 1], [ new Vector3(0,0,0.4), new Vector3(0, 0, 0.1) ] ), opacityBase : 0.1,//1.0, opacitySpread : 0.2, opacityTween :[ [0, 0.1, 0.9, 1], [0.1, 0.4 , 0.03, 0 ] ], //particlesPerSecond : 20, strength : 1, particleDeathAge : 3, //从底下升起后能持续的时间 //emitterDeathAge : 60 // time (seconds) at which to stop creating particles. height : 3, }; const debugSphere = new Mesh(new SphereBufferGeometry(0.03, 5,5), new MeshBasicMaterial({color:'white',depthTest:false})); class SmokeParticle extends Points{ constructor(prop={}) { super(); this.blendStyle = NormalBlending; // false; this.emitterAge = 0.0; //this.emitterAlive = true; prop = $.extend({}, defaults, prop); for ( var key in prop ){ let value = prop[key]; if(value instanceof Array && value[0] instanceof Array ) this[ key ] = new Tween(...value); else if(value instanceof Vector3 || value instanceof Color){ this[ key ] = value.clone(); }else { this[ key ] = value; } } this.defaultSizeTween = this.sizeTween.clone(); this.defaultOpacityTween = this.opacityTween.clone(); this.geometry = new BufferGeometry(); this.computeParams(); this.createMaterial(); this.createGeometry(); this.dynamic = true; this.sortParticles = true; this.frustumCulled = false;//似乎是禁止相机裁剪,否则会在某些角度消失。但是会不会更耗性能呢? prop.position && this.position.copy(prop.position); //--------------------------------------- this.setSize({viewport:viewer.mainViewport}); this.setFov(viewer.fov); let setSize = (e)=>{ if(e.viewport.name != "MainView")return this.setSize(e); }; let setFov = (e)=>{ this.setFov(e.fov); }; /* let reStart = (e)=>{ if(e.v){//重新一个个放出粒子,否则会一股脑儿全部出来,因为同时大于粒子周期了一起重新生成出现。 setTimeout(()=>{//会先update一次delta为pageUnvisile的时间才触发 //console.log('归零') //this.reStart() },1) } } */ viewer.addEventListener('resize',setSize); viewer.addEventListener('fov_changed',setFov); //viewer.addEventListener('pageVisible', reStart) this.addEventListener('dispose',()=>{ viewer.removeEventListener('resize',setSize); viewer.removeEventListener('fov_changed',setFov); //viewer.removeEventListener('pageVisible', reStart) }); } computeParams(){ let length = (this.curve ? this.curve.wholeLength : 0) + this.radius * 2; //加上首尾的半径 //注意:烟最低高度一米, 0 e*size); } { const minSize = 1 , maxSize = 1.5, minBound = 0.01, maxBound = 1; let opac = minSize + (maxSize - minSize) * MathUtils.smoothstep( this.strength, minBound, maxBound); this.opacityTween.values = this.defaultOpacityTween.values.map(e=> e*opac ); } //console.log('smoke particleCount',this.particleCount) } reStart(){ this.emitterAge = 0; this.createGeometry(); } updateGeometry(){ this.computeParams(); this.reStart(); } createParticle(center) { var particle = new Particle$1({ sizeTween : this.sizeTween, opacityTween : this.opacityTween, colorTween : this.colorTween, }); particle.deathAge = this.particleDeathAge; particle.center = center; if (this.positionStyle == 'cube') particle.position = this.randomVector3( this.positionBase, this.positionSpread ); if (this.positionStyle == 'sphere') { /* var z = 2 * Math.random() - 1 var t = Math.PI * 2 * Math.random(); var r = Math.sqrt( 1 - z*z ) ; var vec3 = new THREE.Vector3( r * Math.cos(t), r * Math.sin(t), z ); particle.position = new THREE.Vector3().addVectors( this.positionBase, vec3.multiplyScalar( this.radius ) ); */ //怎么改半径 let y = 2 * Math.random() - 1; let t = Math.PI * 2 * Math.random(); let r = Math.sqrt( 1 - y*y ) ; //因为 r*r = 1-y*y = x*x + z*z = r*r(cos^2 + sin^2 ); let lowDownRatio = 0.2; //压低近平面 let vec3 = new Vector3( r * Math.cos(t), y, Math.abs(r * Math.sin(t) ) * lowDownRatio); particle.position = new Vector3().addVectors( this.positionBase, vec3.multiplyScalar( this.radius ) ); } particle.position.add(center);//add if ( this.velocityStyle == 'cube' ) { particle.velocity = this.randomVector3( this.velocityBase, this.velocitySpread ); } if ( this.velocityStyle == 'sphere' ) { //var direction = particle.position.clone() var direction = new Vector3(0,0,1); //烟应该都是向上的 var speed = this.randomValue( this.speedBase, this.speedSpread ); particle.velocity = direction.normalize().multiplyScalar( speed ); } particle.acceleration = this.randomValue( this.accelerationBase, this.accelerationSpread ); particle.angle = this.randomValue( this.angleBase, this.angleSpread ); particle.angleVelocity = this.randomValue( this.angleVelocityBase, this.angleVelocitySpread ); particle.angleAcceleration = this.randomValue( this.angleAccelerationBase, this.angleAccelerationSpread ); particle.size = this.randomValue( this.sizeBase, this.sizeSpread ); var color = this.randomVector3( this.colorBase, this.colorSpread ); particle.color = new Color().setHSL( color.x, color.y, color.z ); particle.opacity = this.randomValue( this.opacityBase, this.opacitySpread ); particle.age = 0; particle.alive = 0; // particles initialize as inactive return particle; } getPointsForBound(){ return this.boundPoints; //可以用于expand实时bound的点, 不含particle的size等边距 } getBound(points){ // points为生成点(圆心) this.boundPoints = []; let boundingBox = new Box3(); let maxSize = this.sizeTween.values.slice().sort((a,b)=>b-a)[0]; let margin0 = maxSize * 0.11; let margin1 = margin0 + 0.5 ;//保守估计还会飘出这么多距离吧: size + 飘动 points.forEach(bottom=>{ let top = bottom.clone(); top.z += this.height; boundingBox.expandByPoint(bottom); boundingBox.expandByPoint(top); this.boundPoints.push(bottom,top); }); let xyExpand = this.radius+margin1; boundingBox.expandByVector(new Vector3(xyExpand,xyExpand,0)); boundingBox.min.z -= margin0; boundingBox.max.z += margin1; this.boundingBox = boundingBox; /* if(!this.debugBox){ this.debugBox = new THREE.Mesh(boxGeo, boxMat) this.add(this.debugBox) } this.debugBox.scale.copy(boundingBox.getSize(new THREE.Vector3)) this.debugBox.position.copy(boundingBox.getCenter(new THREE.Vector3)) */ } createGeometry(){ this.particleArray = []; const positions = []; const colors = []; const alives = []; const opacitys = []; const sizes = []; const angles = []; let count, points; if(this.positions.length>1){ const spaceDis = 0.6;//间隔距离 count = Math.ceil(this.curve.wholeLength / spaceDis) + 1; points = this.curve.getSpacedPoints( count ); count = points.length; /* points.forEach(e=> { var sphere = debugSphere.clone(); sphere.position.copy(e) viewer.scene.scene.add(sphere) }) */ let haventGetPoints = points.slice(); var getRanPoints = function(i){ var a = Math.random(); let choseIndex = Math.floor(haventGetPoints.length * a); var point = haventGetPoints[choseIndex]; if(haventGetPoints.length == 1){ haventGetPoints = points.slice(); }else { haventGetPoints.splice(choseIndex, 1); } return point }; this.getBound(points); }else { this.getBound(this.positions); } for (var i = 0; i < this.particleCount; i++) { var center = new Vector3().copy(this.positions.length>1 ? getRanPoints(i) : this.positions[0]); //var center = new THREE.Vector3().copy(this.positions.length>1 ? points[Math.floor(i/this.particleCount * count)] : this.positions[0]) // remove duplicate code somehow, here and in update function below. this.particleArray[i] = this.createParticle(center); positions[3*i] = this.particleArray[i].position.x; positions[3*i+1] = this.particleArray[i].position.y; positions[3*i+2] = this.particleArray[i].position.z; colors[3*i] = this.particleArray[i].color.r; colors[3*i+1] = this.particleArray[i].color.g; colors[3*i+2] = this.particleArray[i].color.b; alives[i] = this.particleArray[i].alive; opacitys[i] = this.particleArray[i].opacity; sizes[i] = this.particleArray[i].size; angles[i] = this.particleArray[i].angle; } this.geometry.setAttribute( 'position', new BufferAttribute( new Float32Array(positions), 3 )); this.geometry.setAttribute( 'customColor', new BufferAttribute( new Float32Array(colors), 3 ) ); this.geometry.setAttribute( 'customVisible', new BufferAttribute( new Float32Array(alives), 1 ) ); this.geometry.setAttribute( 'customOpacity', new BufferAttribute( new Float32Array(opacitys), 1 ) ); this.geometry.setAttribute( 'customSize', new BufferAttribute( new Float32Array(sizes), 1 ) ); this.geometry.setAttribute( 'customAngle', new BufferAttribute( new Float32Array(angles), 1 ) ); } createMaterial(){ this.material = new ShaderMaterial( { uniforms: { u_sampler: { type: "t", value: getTexture$1() }, heightOfNearPlane: { type: "f", value:0} //相对far ,以确保画面缩放时点的大小也会缩放 }, vertexShader: vertexShader$1,vertexShader: vertexShader$1, fragmentShader: fragmentShader$1, transparent: true, alphaTest: 0.5, // if having transparency issues, try including: alphaTest: 0.5, blending: this.blendStyle, depthTest: this.blendStyle != NormalBlending }); this.setPerspective(this.fov, this.screenHeight); } update(dt){ if(!Potree.Utils.getObjVisiByReason(this,'force')){//被手动隐藏了 return } if(!Potree.Utils.isInsideFrustum(this.boundingBox, viewer.scene.getActiveCamera())){ Potree.Utils.updateVisible(this,'isInsideFrustum', false ); //不在视野范围 //console.log('unvi') return }else { Potree.Utils.updateVisible(this,'isInsideFrustum', true ); } if(dt > 1){ console.log('update dt>1', dt); } //dt *= 0.5; const recycleIndices = []; const recycleAges = []; const positions = []; const colors = []; const alives = []; const opacitys = []; const sizes = []; const angles = []; for (var i = 0; i < this.particleCount; i++) { if ( this.particleArray[i].alive ) { if ( this.velocityStyle == 'cube' ) { //一定几率改变下方向 let ratio = Math.random(); if(this.particleArray[i].age - this.particleArray[i].lastChangeVage > this.particleDeathAge*ratio ){ this.particleArray[i].velocity = this.randomVector3( this.velocityBase, this.velocitySpread ); this.particleArray[i].lastChangeVage = this.particleArray[i].age; } }else { /* if(this.particleArray[i].age - this.particleArray[i].lastChangeVage > this.particleDeathAge*0.3 ){ if( Math.random()>0.1){//一定几率改变下方向 var speed = this.randomValue( this.speedBase, this.speedSpread ); this.particleArray[i].velocity = this.randomVector3( new THREE.Vector3, new THREE.Vector3(1,1,1) ); this.particleArray[i].velocity.normalize().multiplyScalar( speed ); } this.particleArray[i].lastChangeVage = this.particleArray[i].age } */ } this.particleArray[i].update(dt); // check if particle should expire // could also use: death by size<0 or alpha<0. if ( this.particleArray[i].age > this.particleDeathAge ) { this.particleArray[i].alive = 0.0; recycleIndices.push(i); recycleAges.push((this.particleArray[i].age - this.particleDeathAge)%(this.particleDeathAge )); } // update particle properties in shader positions[3*i] = this.particleArray[i].position.x; positions[3*i+1] = this.particleArray[i].position.y; positions[3*i+2] = this.particleArray[i].position.z; colors[3*i] = this.particleArray[i].color.r; colors[3*i+1] = this.particleArray[i].color.g; colors[3*i+2] = this.particleArray[i].color.b; alives[i] = this.particleArray[i].alive; opacitys[i] = this.particleArray[i].opacity; sizes[i] = this.particleArray[i].size; angles[i] = this.particleArray[i].angle; } } // check if particle emitter is still running //if ( !this.emitterAlive ) return; this.geometry.setAttribute( 'position', new BufferAttribute( new Float32Array(positions), 3 ) ); this.geometry.setAttribute( 'customColor', new BufferAttribute( new Float32Array(colors), 3 ) ); this.geometry.setAttribute( 'customVisible', new BufferAttribute( new Float32Array(alives), 1 ) ); this.geometry.setAttribute( 'customOpacity', new BufferAttribute( new Float32Array(opacitys), 1 ) ); this.geometry.setAttribute( 'customSize', new BufferAttribute( new Float32Array(sizes), 1 ) ); this.geometry.setAttribute( 'customAngle', new BufferAttribute( new Float32Array(angles), 1 ) ); this.geometry.attributes.customColor.needsUpdate = true; this.geometry.attributes.customVisible.needsUpdate = true; this.geometry.attributes.customOpacity.needsUpdate = true; this.geometry.attributes.customSize.needsUpdate = true; this.geometry.attributes.customAngle.needsUpdate = true; // if no particles have died yet, then there are still particles to activate if ( this.emitterAge < this.particleDeathAge ) //开始时一个个放出来 { let particlesPerSecond = this.particleCount / this.particleDeathAge; // determine indices of particles to activate var startIndex = Math.round( particlesPerSecond * (this.emitterAge + 0) ); var endIndex = Math.round( particlesPerSecond * (this.emitterAge + dt) ); if ( endIndex > this.particleCount ) endIndex = this.particleCount; for (var i = startIndex; i < endIndex; i++) this.particleArray[i].alive = 1.0; } // if any particles have died while the emitter is still running, we imediately recycle them for (var j = 0; j < recycleIndices.length; j++) { var i = recycleIndices[j]; this.particleArray[i] = this.createParticle(this.particleArray[i].center); this.particleArray[i].alive = 1.0; // activate right away this.particleArray[i].age = recycleAges[j]; positions[3*i] = this.particleArray[i].position.x; positions[3*i+1] = this.particleArray[i].position.y; positions[3*i+2] = this.particleArray[i].position.z; } this.geometry.setAttribute( 'position', new BufferAttribute( new Float32Array(positions), 3 ) ); this.geometry.attributes.position.needsUpdate = true; // stop emitter? this.emitterAge += dt; //if ( this.emitterAge > this.emitterDeathAge ) this.emitterAlive = false; } randomValue(base, spread) { //return base + spread * (Math.random() - 0.5); let p = Math.random(); return base * p + spread * (1-p) } randomVector3(base, spread) { var rand3 = new Vector3( Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5 ); return new Vector3().addVectors( base, new Vector3().multiplyVectors( spread, rand3 ) ); } setSize(e){ let viewport = e.viewport; this.screenHeight = viewport.resolution.y; this.setPerspective(this.fov, this.screenHeight); } setFov(fov){ this.fov = fov; this.setPerspective(this.fov, this.screenHeight); } setPerspective(fov, height){ //this.uniforms.heightOfNearPlane.value = Math.abs(height / (2 * Math.tan(THREE.Math.degToRad(fov * 0.5)))); let far = Math.abs(height / (2 * Math.tan(MathUtils.degToRad(fov * 0.5)))); this.material.uniforms.heightOfNearPlane.value = far; } dispose(){ this.geometry.dispose(); this.material.dispose(); this.dispatchEvent('dispose'); } } /* 改进:如果有必要 根据curve中分成的点,分成多个簇,每个簇掌管该部分的可见性和particle的数量。 在camera_changed时根据远近修改每个簇的particle的数量,当然不会大于初始创建的个数。多出的随机隐藏。 */ const vertexShader$2 = ` attribute vec3 color; attribute float size; attribute float angle; attribute float opacity; attribute float visible; varying vec4 vColor; varying float vAngle; uniform float heightOfNearPlane; void main() { if(visible > 0.5) { vColor = vec4(color, opacity); } else { vColor = vec4(0.0, 0.0, 0.0, 0.0); } vAngle = angle; vec4 mvPosition = modelViewMatrix * vec4(position, 1.0); gl_Position = projectionMatrix * mvPosition; gl_PointSize = ( heightOfNearPlane * size ) / gl_Position.w; } `; const fragmentShader$2 = ` uniform sampler2D u_sampler; varying vec4 vColor; varying float vAngle; void main() { gl_FragColor = vColor; float u = cos(vAngle); float v = sin(vAngle); vec2 uv = vec2( u * (gl_PointCoord.x - 0.5) + v * (gl_PointCoord.y - 0.5) + 0.5, u * (gl_PointCoord.y - 0.5) - v * (gl_PointCoord.x - 0.5) + 0.5 ); vec4 texture = texture2D(u_sampler, uv); gl_FragColor = gl_FragColor * texture; } `; // import { vertexShader, fragmentShader } from './shader' const DEG2RAD = Math.PI / 180; class Particle$2 { constructor() { this.position = new Vector3(); this.velocity = new Vector3(); this.angle = 0; this.angleVelocity = 0; this.angleAcceleration = 0; this.size = 16; this.color = new Color(); this.opacity = 1; this.rebornCount = 0;//重生次数 this.age = 0; this.alive = 0; //注意,一开始时是未出生的 this.deadAge = 0;//已死亡时间 this.sizeTween = null; this.colorTween = null; this.opacityTween = null; } update(dt) { //s = s0 + (v0 + at) * t 或 lastS + delta(vt) this.position.add(this.velocity.clone().multiplyScalar(dt)); this.velocity.multiplyScalar( 1+this.acceleration*dt ); this.angle += this.angleVelocity * DEG2RAD * dt; this.angleVelocity += this.angleAcceleration * DEG2RAD * dt; this.age += dt; if(this.sizeTween.times.length > 0) { this.size = this.sizeTween.lerp(this.age/this.deathAge); } if(this.colorTween.times.length > 0) { const colorHSL = this.colorTween.lerp(this.age/this.deathAge); this.color = new Color().setHSL(colorHSL.x, colorHSL.y, colorHSL.z); } if(this.opacityTween.times.length > 0) { this.opacity = this.opacityTween.lerp(this.age/this.deathAge); } viewer.dispatchEvent('content_changed'); } } class Util { constructor() {} randomValue(min, max) { //return min + max * (Math.random() - 0.5) let p = Math.random(); return min * p + max * (1-p) } randomVector3(min, max) { const rand3 = new Vector3(Math.random() - 0.5, Math.random() - 0.5, Math.random() - 0.5); return new Vector3().addVectors(min, new Vector3().multiplyVectors(max, rand3)) } } const util$1 = new Util(); const Shape$1 = { CUBE: 1, SPHERE: 2 }; let particleTexture$1; const getTexture$2 = ()=>{ if (!particleTexture$1) { particleTexture$1 = new TextureLoader().load(Potree.resourcePath + '/textures/explode.png'); } return particleTexture$1 }; const sphereGeo = new SphereBufferGeometry(1, 10,4); const sphereMat = new MeshBasicMaterial({wireframe:true, color:"#ffffff"}); const defaults$1 = { position: new Vector3(0,0,1), positionShape: Shape$1.SPHERE, positionRange: new Vector3(1,1,1), //cube radius: 1.3, //sphere velocityShape: Shape$1.SPHERE, velocity: new Vector3(0,0,2), //cube velocityRange: new Vector3(0,0,3), //sphere speed: 0.4, speedRange: 1, size: 0.4, sizeRange: 2, //sizeTween: new Tween( [0, 0.05, 0.3, 0.45], [0, 1, 3, 0.1] ), sizeTween: [[0, 0.04, 0.2, 1],[0.1, 1, 6, 8]] , color: new Vector3(1.0,1.0,1.0), colorRange: new Vector3(0,0,0), colorTween: new Tween(), opacity: 1.0, opacityRange: 0.0, opacityTween: new Tween([0, 0.06, 0.3, 0.8, 1],[0, 1, 0.3, 0.05, 0]), blendMode: AdditiveBlending, acceleration: 0.5, accelerationRange: 0, angle: 0, angleRange: 0, angleVelocity: 0, angleVelocityRange: 0, angleAcceleration: 0, angleAccelerationRange: 0, strength:1, //particlesPerSecond: 8, particleDeathAge: 0.7 , recycleTimes : 3 , //每个粒子在一次爆炸后循环次数,循环完毕进入particleSpaceTime,等待下一次爆炸. //爆炸时长: particleDeathAge * (recycleTimes+1) particleSpaceTime: 3, //间隔 }; class ExplodeParticle extends Points { constructor(params) { super(); this.age = 0; this.alive = true; //this.deathAge = 60 this.loop = true; this.blendMode = NormalBlending; this.setParameters(params); this.createParticles(); this.frustumCulled = false;//似乎是禁止相机裁剪,否则会在某些角度消失。但是会不会更耗性能呢? //------------------------------------ this.setSize({viewport:viewer.mainViewport}); this.setFov(viewer.fov); let setSize = (e)=>{ if(e.viewport.name != "MainView")return this.setSize(e); }; let setFov = (e)=>{ this.setFov(e.fov); }; /* let reStart = (e)=>{ if(e.v){//重新一个个放出粒子,否则会一股脑儿全部出来,因为同时大于粒子周期了一起重新生成出现。 setTimeout(()=>{//会先update一次delta为pageUnvisile的时间才触发 //console.log('归零') //this.reStart() },1) } } */ viewer.addEventListener('resize',setSize); viewer.addEventListener('fov_changed',setFov); //viewer.addEventListener('pageVisible', reStart) this.addEventListener('dispose',()=>{ viewer.removeEventListener('resize',setSize); viewer.removeEventListener('fov_changed',setFov); //viewer.removeEventListener('pageVisible', reStart) }); } computeParams(){ if(this.curve){ this.position.copy(this.curve.points[0]); } const minSize = 0.8, maxSize = 10, minRadiusBound = 0.2, maxRadiusBound = 20; let size = minSize + (maxSize - minSize) * MathUtils.smoothstep(this.radius*this.strength , minRadiusBound, maxRadiusBound); this.sizeTween.values = this.defaultSizeTween.values.map(e=> e*size); this.particleCount = Math.ceil( this.strength * this.radius * 5 /* * this.radius * this.radius */ ); this.speed = defaults$1.speed * this.radius; this.speedRange = defaults$1.speedRange * this.radius; console.log(this.particleCount); { this.boundPoints = []; this.boundPoints.push(this.position.clone()); let maxSize = this.sizeTween.values.slice().sort((a,b)=>b-a)[0]; let margin = maxSize * 0.35 + 0.5; let scale = this.radius+margin; let sphere = new Sphere(this.position, scale);//加上防止剪裁 this.boundingSphere = sphere; //虽然还是会有一些后续移动的会超出 this.boundingBox = new Box3().setFromCenterAndSize(this.position, new Vector3(scale*2,scale*2,scale*2)); /* if(!this.debugSphere){ this.debugSphere = new THREE.Mesh(sphereGeo, sphereMat) this.add(this.debugSphere) } this.debugSphere.scale.set(scale,scale,scale) */ } } getPointsForBound(){ return this.boundPoints; //可以用于expand实时bound的点, 不含particle的size等边距 } reStart(){ this.age = 0; this.createParticles(); } setParameters(params) { params = $.extend({}, defaults$1, params); for (var key in params) { let value = params[key]; if (key == 'position') this.position.copy(value); else if (value instanceof Array && value[0]instanceof Array){ this[key] = new Tween(...value); }else if(value instanceof Vector3 || value instanceof Color){ this[ key ] = value.clone(); }else { this[key] = value; } } this.defaultSizeTween = this.sizeTween.clone(); //Object.assign(this, params) this.particles = []; this.age = 0.0; this.alive = true; this.geometry = new BufferGeometry(); this.computeParams(); this.material = new ShaderMaterial({ uniforms: { u_sampler: { value: this.texture || getTexture$2() }, heightOfNearPlane: { type: "f", value: 0 }//相对far ,以确保画面缩放时点的大小也会缩放 }, vertexShader: vertexShader$2, fragmentShader: fragmentShader$2, transparent: true, alphaTest: 0.5, depthTest: this.blendMode == NormalBlending, blending: this.blendMode }); } createParticles() { this.particles = []; const count = this.particleCount; const positionArray = new Float32Array(count * 3); const colorArray = new Float32Array(count * 3); const sizeArray = new Float32Array(count); const angleArray = new Float32Array(count); const opacityArray = new Float32Array(count); const visibleArray = new Float32Array(count); for (let i = 0; i < count; i++) { const particle = this.createParticle(); /* positionArray[i * 3] = particle.position.x positionArray[i * 3 + 1] = particle.position.y positionArray[i * 3 + 2] = particle.position.z colorArray[i * 3] = particle.color.r colorArray[i * 3 + 1] = particle.color.g colorArray[i * 3 + 2] = particle.color.b sizeArray[i] = particle.size angleArray[i] = particle.angel opacityArray[i] = particle.opacity visibleArray[i] = particle.alive */ this.particles[i] = particle; } this.geometry.setAttribute('position', new BufferAttribute(positionArray,3)); this.geometry.setAttribute('color', new BufferAttribute(colorArray,3)); this.geometry.setAttribute('angle', new BufferAttribute(angleArray,1)); this.geometry.setAttribute('size', new BufferAttribute(sizeArray,1)); this.geometry.setAttribute('visible', new BufferAttribute(visibleArray,1)); this.geometry.setAttribute('opacity', new BufferAttribute(opacityArray,1)); } createParticle() { const particle = new Particle$2(); particle.sizeTween = this.sizeTween; particle.colorTween = this.colorTween; particle.opacityTween = this.opacityTween; particle.deathAge = this.particleDeathAge; if (this.positionShape == Shape$1.CUBE) { particle.position = util$1.randomVector3(new Vector3, this.positionRange); } if (this.positionShape == Shape$1.SPHERE) { /* const z = 2 * Math.random() - 1 const t = Math.PI * 2 * Math.random() const r = Math.sqrt(1 - z*z) const vec3 = new THREE.Vector3(r * Math.cos(t), r * Math.sin(t), z) particle.position = vec3.multiplyScalar(this.radius) */ const y = 2 * Math.random() - 1; const t = Math.PI * 2 * Math.random(); const r = Math.sqrt(1 - y * y); const vec3 = new Vector3(r * Math.cos(t),y,r * Math.sin(t)); particle.position = vec3.multiplyScalar(this.radius); } if (this.velocityShape == Shape$1.CUBE) { particle.velocity = util$1.randomVector3(this.velocity, this.velocityRange); } if (this.velocityShape == Shape$1.SPHERE) { const direction = new Vector3().addVectors(particle.position, new Vector3(0,0,this.radius*2));//向上升? const speed = util$1.randomValue(this.speed, this.speedRange); particle.velocity = direction.normalize().multiplyScalar(speed); } particle.acceleration = util$1.randomValue(this.acceleration, this.accelerationRange); particle.angle = util$1.randomValue(this.angle, this.angleRange); particle.angleVelocity = util$1.randomValue(this.angleVelocity, this.angleVelocityRange); particle.angleAcceleration = util$1.randomValue(this.angleAcceleration, this.angleAccelerationRange); particle.size = util$1.randomValue(this.size, this.sizeRange); const color = util$1.randomVector3(this.color, this.colorRange); particle.color = new Color().setHSL(color.x, color.y, color.z); particle.opacity = util$1.randomValue(this.opacity, this.opacityRange); return particle } update(dt) { if(!Potree.Utils.getObjVisiByReason(this,'force')){//被手动隐藏了 return } if(this.delayStartTime>0){ // 爆炸延迟 return this.delayStartTime -= dt } if(!Potree.Utils.isInsideFrustum(this.boundingSphere, viewer.scene.getActiveCamera())){ Potree.Utils.updateVisible(this,'isInsideFrustum', false ); //不在视野范围 return }else { Potree.Utils.updateVisible(this,'isInsideFrustum', true ); } //const timeRatio = 0.5 if(dt > 1){ console.log('update dt>1', dt); } //dt *= timeRatio let particleDeathAge = this.particleDeathAge;/* * timeRatio */ let particleSpaceTime = this.particleSpaceTime; /* * timeRatio */ const recycleIndices = []; const recycleAges = []; const recycleRebornCount = []; const positionArray = this.geometry.attributes.position.array; const opacityArray = this.geometry.attributes.opacity.array; const visibleArray = this.geometry.attributes.visible.array; const colorArray = this.geometry.attributes.color.array; const angleArray = this.geometry.attributes.angle.array; const sizeArray = this.geometry.attributes.size.array; for (let i = 0; i < this.particleCount; i++) { const particle = this.particles[i]; if (particle.alive) { particle.update(dt); if (particle.age > particleDeathAge) { particle.alive = 0.0; if(particle.rebornCount >= this.recycleTimes){ particle.deadAge = particle.age - particleDeathAge; //已死亡时间 }else {//直接循环 recycleIndices.push(i); recycleAges.push(/* ( */particle.age - particleDeathAge/* )%(this.particleDeathAge ) */); recycleRebornCount.push(particle.rebornCount+1); } } positionArray[i * 3] = particle.position.x; positionArray[i * 3 + 1] = particle.position.y; positionArray[i * 3 + 2] = particle.position.z; colorArray[i * 3] = particle.color.r; colorArray[i * 3 + 1] = particle.color.g; colorArray[i * 3 + 2] = particle.color.b; visibleArray[i] = particle.alive; opacityArray[i] = particle.opacity; angleArray[i] = particle.angle; sizeArray[i] = particle.size; }else { if(particle.rebornCount >= this.recycleTimes){ if(particle.age > particleDeathAge) {//其他已经死亡的粒子的时间继续增加 particle.deadAge += dt; } } } if (particle.rebornCount >= this.recycleTimes && particle.age > particleDeathAge) {//已经死亡 if(particle.deadAge >= particleSpaceTime){//死亡时间超过设定的间隔时间后重启 recycleIndices.push(i); let wholeTime = particleDeathAge * (this.recycleTimes+1) + particleSpaceTime; recycleAges.push((particle.deadAge - particleSpaceTime)% wholeTime ); //剩余时间就是重生后的age recycleRebornCount.push(0); } } } this.geometry.attributes.size.needsUpdate = true; this.geometry.attributes.color.needsUpdate = true; this.geometry.attributes.angle.needsUpdate = true; this.geometry.attributes.visible.needsUpdate = true; this.geometry.attributes.opacity.needsUpdate = true; this.geometry.attributes.position.needsUpdate = true; if (!this.alive) return if (this.age < particleDeathAge) { let startIndex = Math.round(this.particleCount * (this.age + 0)/ particleDeathAge); let endIndex = Math.round(this.particleCount * (this.age + dt)/ particleDeathAge); if (endIndex > this.particleCount) { endIndex = this.particleCount; } for (let i = startIndex; i < endIndex; i++) { this.particles[i].alive = 1.0; } } for (let j = 0; j < recycleIndices.length; j++) { let i = recycleIndices[j]; this.particles[i] = this.createParticle(); this.particles[i].alive = 1.0; //出生 this.particles[i].age = recycleAges[j]; this.particles[i].rebornCount= recycleRebornCount[j]; /* if(this.particles[i].age < particleDeathAge){ positionArray[i * 3] = this.particles[i].position.x positionArray[i * 3 + 1] = this.particles[i].position.y positionArray[i * 3 + 2] = this.particles[i].position.z visibleArray[i] = particle.alive? } */ } this.geometry.attributes.position.needsUpdate = true; this.age += dt; if (this.age > this.deathAge && !this.loop) { this.alive = false; } } setSize(e) { let viewport = e.viewport; this.screenHeight = viewport.resolution.y; this.setPerspective(this.fov, this.screenHeight); } setFov(fov) { this.fov = fov; this.setPerspective(this.fov, this.screenHeight); } setPerspective(fov, height) { //this.uniforms.heightOfNearPlane.value = Math.abs(height / (2 * Math.tan(THREE.Math.degToRad(fov * 0.5)))); let far = Math.abs(height / (2 * Math.tan(MathUtils.degToRad(fov * 0.5)))); this.material.uniforms.heightOfNearPlane.value = far; } updateGeometry(){ this.computeParams(); this.reStart(); } dispose(){ this.geometry.dispose(); this.material.dispose(); this.dispatchEvent('dispose'); } } const colors$2 = { 'fire+smoke':0xffffff, 'smoke': 0xffffff, 'explode':0xffffff, }; let depthMatPrefix = { clipDistance : 100, occlusionDistance:60, /* 变为backColor距离 */ maxClipFactor:0.5, backColor:"#777" , useDepth:true, transparent: !0, }; let lineMats$2; let getLineMat$1 = function(type){ if(!lineMats$2){ lineMats$2 = { 'fire+smoke':LineDraw.createFatLineMat($.extend(depthMatPrefix,{ color: colors$2['fire+smoke'], lineWidth: 2 })), 'smoke' :LineDraw.createFatLineMat($.extend(depthMatPrefix,{ color: colors$2['smoke'], lineWidth: 2 })), 'explode' :LineDraw.createFatLineMat($.extend(depthMatPrefix,{ color: colors$2['explode'], lineWidth: 2 })), }; } return lineMats$2[type] }; let handleMats; let getHandleMat = function(type){ if(!handleMats){ let texLoader = new TextureLoader(); handleMats = { "fire+smoke" : new DepthBasicMaterial($.extend(depthMatPrefix,{ map: texLoader.load(Potree.resourcePath+'/textures/icon-fire.png' ), color: colors$2['fire+smoke'], })), "smoke" : new DepthBasicMaterial($.extend(depthMatPrefix,{ map: texLoader.load(Potree.resourcePath+'/textures/icon-smoke.png' ), color: colors$2['smoke'], })), "explode" : new DepthBasicMaterial($.extend(depthMatPrefix,{ map: texLoader.load(Potree.resourcePath+'/textures/icon-explode.png' ), color: colors$2['explode'], })), }; } return handleMats[type] }; let ParticleEditor = { bus: new EventDispatcher, particleGroup : new Object3D , curveGroup:new Object3D , init:function(){ this.particleGroup.name = 'particles'; viewer.scene.scene.add( this.particleGroup ); this.curveGroup.name = 'particles-curves'; viewer.scene.scene.add( this.curveGroup ); }, addParticle : function(prop={}){ let particle; if(prop.type == 'fire'){ particle = new FireParticle(prop); }else if(prop.type == 'smoke'){ particle = new SmokeParticle(prop); }else if(prop.type == 'explode'){ particle = new ExplodeParticle(prop); } this.particleGroup.add(particle); return particle } , removeParticle(particle){ //particle.dispatchEvent('delete') particle.dispose(); this.particleGroup.remove(particle); particle.curve.dispose(); } , update(delta){ this.particleGroup.children.forEach(e=>e.update(delta)); } , startInsertion(type = 'fire', prop={}){ //viewer.modules.ParticleEditor.startInsertion() let deferred = $.Deferred(); let particles = []; let finish = (ifDone)=>{ if(ifDone){ deferred.resolve(particles); } viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"addSth" }); viewer.removeEventListener('global_click', click); this.bus.removeEventListener('cancel_insertions',cancel); }; let curve = new CurveCtrl([], getLineMat$1(type), colors$2[type], type+'_curve', {handleMat:getHandleMat(type)} ); this.curveGroup.add(curve); prop.curve = curve; prop.type = type; //console.log('创建curve',type,curve.uuid) let cancel = ()=>{ console.log('cancel_insertions', curve.uuid ); curve.dispose(); finish(false); }; this.bus.dispatchEvent('cancel_insertions');//删除旧的 this.bus.addEventListener('cancel_insertions',cancel); var click = (e)=>{ if(e.button === MOUSE.RIGHT){ if(curve.points.length>=1){ //if(type.includes('fire') || type.includes('smoke') ){ particles = this.createFromData(prop); finish(true); } return } var I = e.intersect && (e.intersect.orthoIntersect || e.intersect.location); if(!I)return curve.addPoint(I, null, true); if(type == 'explode'){ particles = this.createFromData(prop); finish(true); } return {stopContinue:true}//防止继续执行别的侦听,如flytopano }; viewer.addEventListener('global_click', click, {importance:10});//add importance viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"addSth" }); return deferred.promise() }, createFromData(prop){ const type = prop.type; var particles = []; let curve = prop.curve; if(!curve){ curve = new CurveCtrl(prop.points, getLineMat$1(type), colors$2[type], type+'_curve', {handleMat:getHandleMat(type)} ); this.curveGroup.add(curve); } if(type.includes('fire') || type.includes('smoke') ){ if(type.includes('fire')){ var fire = this.addParticle({ type : 'fire', positions : curve.points, curve, radius : prop.radius, height: prop.height, strength : prop.strength, }); particles.push(fire); } if(type.includes('smoke')){ var smoke = this.addParticle({ type : 'smoke', positions : curve.points, curve, positionStyle : 'sphere' , strength : prop.smokeStrength, radius: prop.smokeRadius, height: prop.smokeHeight, }); particles.push(smoke); } }else if(type == 'explode'){ var explode = this.addParticle({ type : 'explode', position : curve.points[0], strength: prop.strength, radius : prop.radius, particleSpaceTime: prop.particleSpaceTime, curve, delayStartTime:prop.delayStartTime, }); particles.push(explode); } curve.addEventListener('dragCurvePoint',()=>{ Common.intervalTool.isWaiting('particlePointChange', ()=>{ //延时update,防止卡顿 particles.forEach(e=>e.updateGeometry()); //geoNeedsUpdate = false curve.dispatchEvent('sendUpdatePoints'); }, 400); }); return particles } }; let CamAniEditor = { createAnimation(data){ let animation = new CameraAnimation$1(viewer); if(data) { animation.name = data.name; animation.duration = data.duration; animation.useDurSlice = data.useDurSlice; for(const cpdata of data.points){ /* const position = Potree.Utils.datasetPosTransform({ fromDataset: true, position: cpdata.position, datasetId: Potree.settings.originDatasetId }) const target = Potree.Utils.datasetPosTransform({ fromDataset: true, position: cpdata.target, datasetId: Potree.settings.originDatasetId }) */ const position = new Vector3().copy(cpdata.position); const target = new Vector3().copy(cpdata.target); const duration = cpdata.time; const cp = animation.createControlPoint(null, {position, target, duration}); } } animation.changeCallback(); viewer.scene.addCameraAnimation(animation); return animation }, removeAnimation(animation){ animation.dispatchEvent('dispose'); viewer.scene.removeCameraAnimation(animation); } }; const clickPanoToDisLink = false;//是否在编辑漫游点连接时,通过点击漫游点能断开连接 let images360, Alignment$1, SiteModel$1, suggestCircleMat; const texLoader$c = new TextureLoader(); texLoader$c.crossOrigin = "anonymous"; const rotQua = new Quaternion().setFromAxisAngle(new Vector3(0,0,1), Math.PI); const lineMats$3 = {}; const circleMats = {}; const renderOrders$1 = { circleSelected:3, circle:2, line:1, }; const pointColor = { /* selected:"#c80", default:'#1ac' */ selected:"#c60", default:'#17c' }; const opacitys = {//点云透明度 'topView':{ default:0.4, selected: 0.6 }, 'sideView':{//侧面重叠概率高 default:0.2, selected: 0.5 }, };//调这么低是因为有的重叠边缘太亮了 const cameraProps$2 = [ { name : 'top', axis:["x","y"], direction : new Vector3(0,0,-1), //镜头朝向 openCount:0, }, { name : 'right', axis:["y","z"], direction : new Vector3(1,0,0), openCount:0, }, { name : 'mainView', openCount:0, } ]; class PanoEditor extends EventDispatcher{ constructor(){ super(); this.panoGroup = [], //分组 this.viewports = {}, this.panoLink = {}, this.panoMeshs = new Object3D, this.lineMeshes = new Object3D; this.views = {}; this.cameras = {}; this.orthoCamera = new OrthographicCamera(-100, 100, 100, 100, 0.01, 10000); this.orthoCamera.up.set(0,0,1); this.selectedPano; this.selectedGroup; this.operation; this.visiblePanos = []; this.suggestLines = []; } init(){ {//init lineMats lineMats$3.default = LineDraw.createFatLineMat({ color: '#eeeeee', lineWidth: 2, depthTest:false }); lineMats$3.hovered = LineDraw.createFatLineMat({ color: '#00c8af', lineWidth: 2, depthTest:false }); lineMats$3.selected = LineDraw.createFatLineMat({ color: '#00c8af', lineWidth: 3, depthTest:false }); lineMats$3.suggestLink = LineDraw.createFatLineMat({ color: '#ff2222', lineWidth: 4, dashed:true, depthTest:false }); } suggestCircleMat = new MeshBasicMaterial({ map: texLoader$c.load(Potree.resourcePath+'/textures/whiteCircle.png' ) , color:'#ff2222', transparent:true, depthTest:false, depthWrite:false }); this.initViews(); /* { this.box = new BoxVolume({ clip:true }) this.box.clipTask = ClipTask['SHOW_INSIDE_Big' ] this.box.name = "panoEditClipBox"; } */ viewer.addEventListener('allLoaded',()=>{ images360 = viewer.images360; Alignment$1 = viewer.modules.Alignment; SiteModel$1 = viewer.modules.SiteModel; this.panoMeshs.name = 'panoMeshs'; viewer.scene.scene.add(this.panoMeshs); this.lineMeshes.name = 'lineMeshes'; viewer.scene.scene.add(this.lineMeshes); Potree.settings.ifShowMarker = false; { this.transformControls = new TransformControls(viewer.mainViewport.camera, viewer.renderArea,{ dontHideWhenFaceCamera: true, rotFullCircle:true }); this.transformControls.setSize(1.5); viewer.scene.scene.add(this.transformControls); this.transformControls._gizmo.hideAxis = {/* translate:['x','y'], */ rotate:['x','y','e'] }; this.transformControls.setRotateMethod(2); this.fakeMarkerForTran = new Mesh(new BoxBufferGeometry(0.3,0.3,0.3) , new MeshBasicMaterial({ color:"#FFFFFF", opacity:0.4, transparent:true, visible:false }));//一个看不见的mesh,只是为了让transformControls移动点云 viewer.scene.scene.add(this.fakeMarkerForTran); let afterMoveCircle = (type)=>{ if(type == 'position'){ let moveVec = new Vector3().subVectors(this.fakeMarkerForTran.position, this.fakeMarkerForTran.oldState.position); this.selectedClouds.forEach(cloud=>Alignment$1.translate(cloud, moveVec)); }else { let center = this.selectedPano.position; let forward = new Vector3(0,1,0); let vec1 = forward.clone().applyQuaternion(this.fakeMarkerForTran.oldState.quaternion); let vec2 = forward.clone().applyQuaternion(this.fakeMarkerForTran.quaternion); let diffAngle = math.getAngle(vec1,vec2,'z'); this.selectedClouds.forEach(cloud=>{ Alignment$1.rotateAround(center, cloud, null, diffAngle); }); } this.fakeMarkerForTran.oldState = { position: this.fakeMarkerForTran.position.clone(), quaternion: this.fakeMarkerForTran.quaternion.clone(), }; Alignment$1.history.beforeChange(this.selectedClouds); }; this.fakeMarkerForTran.addEventListener('position_changed', afterMoveCircle.bind(this,'position')); this.fakeMarkerForTran.addEventListener("rotation_changed", afterMoveCircle.bind(this,'rotation') ); this.transformControls.addEventListener('mouseUp',()=>{ Alignment$1.history.afterChange(this.selectedClouds); }); Alignment$1.history.addEventListener('undo',()=>{ this.updateTranCtl(); }); } this.initPanoLink(); this.addPanoMesh(); viewer.scene.pointclouds.forEach(e=>{ e.material.color = pointColor.default; }); viewer.setEDLEnabled(true); //为了降一倍的绘制. 同时用描边增强立体感,弥补点云稀疏 viewer.setEDLRadius(3); viewer.setEDLStrength(0.02); this.switchView('top'); {//默认选择一个楼层 let panoVisiReady, siteModelReady; let floorInit = ()=>{ if(!panoVisiReady || !siteModelReady)return setTimeout(()=>{ if(this.currentFloor == 'all'){//还未选择楼层的话 let floor = SiteModel$1.entities.find(e=>e.buildType == 'floor' && e.panos.length); //选择有漫游点的一层 if(!floor){ floor = 'all'; //SiteModel.entities.find(e=>e.buildType == 'floor') console.log('没有一层有漫游点?!'); } console.log('initDataDone'); console.log('gotoFloor 1'); this.gotoFloor(floor); } },1); //2d那边用了nextTick ,so setTimeout here }; SiteModel$1.bus.addEventListener('initDataDone',()=>{ siteModelReady = true; floorInit(); },{once:true}); this.addEventListener('panoVisiReady',()=>{//2d初始化完成,才可以由3d修改pano显示 (因为在之前2d会给每个pano传来显示的消息,在这之前的修改都会别覆盖) panoVisiReady = true; floorInit(); },{once:true}); } Alignment$1.bus.addEventListener('switchHandle', this.updateCursor.bind(this)); viewer.addEventListener('global_click',(e)=>{ if(e.button === MOUSE.RIGHT){//取消旋转和平移 //console.log('right click',e) this.setLinkOperateState('addLink',false); this.setLinkOperateState('removeLink',false); }else if(this.clickToZoomInEnabled){ if(this.activeViewName == 'mainView'){ viewer.controls.zoomToLocation(e.mouse); }else { this.zoomIn(e.intersect.orthoIntersect, e.pointer); } this.setZoomInState(false); } }); /* {//旋转时的辅助线--绕某个点旋转的版本 this.rotGuideLine = LineDraw.createLine([], {color:'#aaffee'}) this.rotGuideLine.visible = false this.rotGuideLine.name = 'rotGuideLine' this.rotGuideLine.renderOrder = renderOrders.line viewer.scene.scene.add(this.rotGuideLine) let startPoint Alignment.bus.addEventListener('rotateStart', (e)=>{ startPoint = e.startPoint }) Alignment.bus.addEventListener('rotate', (e)=>{ LineDraw.updateLine(this.rotGuideLine, [startPoint, e.endPoint] ) this.rotGuideLine.visible = true }) viewer.fpControls.addEventListener("end",(e)=>{ startPoint = null this.rotGuideLine.visible = false }) } */ {//连接时的辅助线 this.linkGuideLine = LineDraw.createLine([], {color:'#ddd', deshed:true, dashSize:0.1,gapSize:0.05, depthTest:false}); this.linkGuideLine.visible = false; this.linkGuideLine.name = 'linkGuideLine'; viewer.scene.scene.add(this.linkGuideLine); this.linkGuideLine.renderOrder = renderOrders$1.line; let update = (e)=>{ if(this.operation != 'addLink' || this.activeViewName != 'top' && this.activeViewName != 'mainView' ||!this.selectedPano){ return this.linkGuideLine.visible = false } let endPos; if(this.activeViewName == 'top' ){ endPos = e.intersect.orthoIntersect.clone().setZ(this.selectedPano.position.z); }else if(this.activeViewName == 'mainView' ){ if(!e.intersect || !e.intersect.point)return endPos = e.intersect.point.position; } LineDraw.updateLine(this.linkGuideLine, [this.selectedPano.position, endPos] ); this.linkGuideLine.visible = true; viewer.dispatchEvent('content_changed'); }; viewer.addEventListener('global_mousemove', (e)=>{ update(e); }); //this.addEventListener('updateLinkGuideLine', update) } /* viewer.inputHandler.addEventListener('keydown', (e)=>{ if(e.event.key == "r" ){ this.setTranMode('rotate') }else if(e.event.key == "t"){ this.setTranMode('translate') } }) */ /* { viewer.addEventListener('camera_changed', (e)=>{ Common.intervalTool.isWaiting('updatePointLevels', ()=>{ this.updatePointLevels() }, 1050) }) setTimeout(()=>{ this.updatePointLevels() }, viewer.scene.pointclouds.length*150) //等待差不多updat出了正确的visibleNode时 } */ this.panoReposCallback = ()=>{ viewer.controls.setTarget(this.selectedPano.position); //3d时绕其为中心转动 }; }); } setTranMode(mode){//rotate or translate console.log('setTranMode',mode); this.tranMode = mode; if(this.activeViewName == 'mainView'){ mode && this.transformControls.setMode(mode); this.updateTranCtl(); }else { Alignment$1.switchHandle(mode); } this.updateIntersectEnable(); } updateIntersectEnable(){ //侧面容易因intersect卡住, 如果非必要关闭intersect. 3d页面也会卡,但controls需要所以不能去掉 Potree.settings.intersectWhenHover = !!(this.activeViewName == 'mainView' || this.selectedPano && this.tranMode ); } updateTranCtl(){// 设置3D页面的transformControls相关 if(!this.tranMode || !this.selectedPano || this.activeViewName != 'mainView' ) { return this.transformControls.detach() }else if(this.checkIfAllLinked({group:this.selectedGroup})){ this.dispatchEvent('needToDisConnect'); return this.transformControls.detach() } this.transformControls.attach(this.fakeMarkerForTran); let {position, quaternion} = this.getPanoPose(this.selectedPano); this.fakeMarkerForTran.position.copy(position); this.fakeMarkerForTran.quaternion.copy(quaternion); this.fakeMarkerForTran.oldState = { position: position.clone(), quaternion: quaternion.clone(), }; } ////////////////////////////////// initViews(){ this.splitScreenTool = new SplitScreen; this.targetPlane = viewer.mainViewport.targetPlane = new Plane(); this.shiftTarget = viewer.mainViewport.shiftTarget = new Vector3; //project在targetPlane上的位置 for(let i=0;i<2;i++){ let prop = cameraProps$2[i]; let view = new ExtendView(); this.views[prop.name] = view; this.cameras[prop.name] = this.orthoCamera; view.name = prop.name; view.direction = prop.direction; } this.views.mainView = viewer.mainViewport.view; this.cameras.mainView = viewer.mainViewport.camera; } switchView(name ){//替换view和camera到mainViewport let view = this.views[name]; let camera = this.cameras[name]; let prop = cameraProps$2.find(e=>e.name == name); let {boundSize, center} = viewer.bound; this.lastViewName = this.activeViewName; this.activeViewName = name; let lastView = this.views[this.lastViewName]; let lastCamera = this.cameras[this.lastViewName]; viewer.mainViewport.view = view; viewer.mainViewport.camera = camera; if(lastCamera)lastView.zoom = lastCamera.zoom; this.targetPlane.setFromNormalAndCoplanarPoint( view.direction.clone(), center ); this.targetPlane.projectPoint(view.position, this.shiftTarget ); //target转换到过模型中心的平面,以保证镜头一定在模型外 view.position.copy(this.splitScreenTool.getPosOutOfModel(viewer.mainViewport)); if(view.zoom)camera.zoom = view.zoom;//恢复上次的zoom viewer.updateScreenSize({forceUpdateSize:true});//更新camera aspect left等 this.updateCursor(); if(name == 'mainView'){ viewer.mainViewport.alignment = null; let changeMat = ()=>{ viewer.scene.pointclouds.forEach(e=>{ e.material.activeAttributeName = 'rgba'; e.material.useFilterByNormal = false; e.changePointOpacity(1 ); }); }; /* if(prop.openCount == 0){ //点数较多时,首次转到3D视角会卡顿,因为要切换材质。 let delay1 = THREE.Math.clamp(viewer.scene.pointclouds.length*0.5, 1, 200) setTimeout(()=>{ this.activeViewName == 'mainView' && changeMat() },delay1) //console.log('switchview',delay1 ) }else{ */ changeMat(); //} Potree.Utils.updateVisible(viewer.reticule, 'force', true); if(lastView){//2d->3d view.copy(lastView); let direction = view.direction; let panos = images360.panos.filter(e=>e.circle.visible); let nearestPano = Common.sortByScore(panos , [], [(pano)=>{ let vec = new Vector3().subVectors(pano.position, view.position); return -vec.dot(direction); }], true); //console.log('最近',nearestPano ) if(nearestPano && nearestPano[0] ){ //尽量不变画面范围,使pano点保持原位,转换到mainView let halfHeight = lastCamera.top/lastCamera.zoom; let dis = halfHeight / Math.tan( MathUtils.degToRad(camera.fov/2)); view.position.add(direction.clone().multiplyScalar(-nearestPano[0].score - dis)); //console.log('getCloser', -nearestPano[0].score - dis) this.lastDisToPano = dis; //记录一下 } } viewer.fpControls.lockKey = false; }else { if(this.lastViewName == 'mainView'){//3d->2d let direction = lastView.direction; let panos = images360.panos.filter(e=>e.circle.visible); //尽量靠近画布中心,且距离相机较近 let nearestPano = Common.sortByScore(panos , [], [(pano)=>{ let vec = new Vector3().subVectors(pano.position, lastView.position); let dis = vec.dot(direction); return dis < 0 ? dis * 10 : - dis },(pano)=>{ let vec = new Vector3().subVectors(pano.position, lastView.position); let angle = vec.angleTo(direction); return - angle * 70 }], true); //目前还存在的问题就是不知selectedPano和最近点的取舍 //console.log('panos',nearestPano ) if(nearestPano && nearestPano[0] ){ //console.log('nearestPano',nearestPano[0].item.id ) let pos1 = nearestPano[0].item.position.clone(); let pos2 = pos1.clone(); let dis = new Vector3().subVectors(nearestPano[0].item.position, lastView.position).dot(direction); //-nearestPano[0].score //根据2d->3d的式子逆求zoom let halfHeight = Math.abs(dis) * Math.tan( MathUtils.degToRad(lastCamera.fov/2)); camera.zoom = camera.top / halfHeight; camera.updateProjectionMatrix(); if(name == 'right'){//侧视图 view.direction = direction.clone().setZ(0); //水平方向设定为3d的方向 this.targetPlane.setFromNormalAndCoplanarPoint( view.direction.clone(), center ); this.targetPlane.projectPoint(view.position, this.shiftTarget ); //target转换到过模型中心的平面,以保证镜头一定在模型外 view.position.copy(this.splitScreenTool.getPosOutOfModel(viewer.mainViewport)); } view.applyToCamera(camera);//update pos1.project(lastCamera); pos2.project(camera); //目标是找到画面上最接近中心的一点(最好是漫游点,不然就是点云),让其在转换画面后在画面上的位置不变。万一找到的点不在屏幕中(比如当屏幕中没点云时),就默认让那个点移动到屏幕中央,也就是假设当前它pos1在屏幕中央位置。 // if(pos1.z>1){ console.warn('选取的点在相机背后了!?'); } //如果最近点超出屏幕范围 (-1,1), 最好将其拉到边缘,甚至居中 。这样屏幕上就不会没有漫游点了 let bound = 0.9; pos1.x = MathUtils.clamp(pos1.x, -bound, bound); pos1.y = MathUtils.clamp(pos1.y, -bound, bound); let vecOnscreen = new Vector3().subVectors(pos1,pos2); let moveVec = Potree.Utils.getOrthoCameraMoveVec(vecOnscreen, camera ); //console.log('pos1', pos1) view.position.sub(moveVec); } }else { if(prop.openCount == 0){//至多执行一次 this.viewportFitBound(name, boundSize, center); } } viewer.scene.pointclouds.forEach(e=>{ e.material.activeAttributeName = 'color'; e.material.useFilterByNormal = true; let opaProp = name == 'top' ? opacitys.topView : opacitys.sideView; if(this.selectedPano && this.selectedClouds.includes(e) ){ e.changePointOpacity(opaProp.selected,true); e.material.color = pointColor.selected; }else { e.changePointOpacity(opaProp.default,true); e.material.color = pointColor.default; } }); Potree.Utils.updateVisible(viewer.reticule, 'force', false); if(name == 'top') viewer.mainViewport.alignment = {rotate:true,translate:true}; if(name == 'right'){ viewer.mainViewport.alignment = {translate:true, rotateSide:true, translateVec:new Vector3(0,0,1)}; //只能上下移动 viewer.mainViewport.rotateSide = true; }else { viewer.mainViewport.rotateSide = false; } viewer.fpControls.lockKey = true; } this.updateTranCtl(); this.setTranMode(this.tranMode); // update this.setZoomInState(false); //取消放大模式 //this.updatePointLevels() this.updateIntersectEnable(); prop.openCount ++; } viewportFitBound(){ //使一个viewport聚焦在某个范围 if(viewer.mainViewport.resolution.x == 0 || viewer.mainViewport.resolution.y == 0){ return setTimeout(()=>{ this.viewportFitBound(); },10) } this.gotoFloor(this.currentFloor, true, 0, null, true); } rotateSideCamera(angle){//侧视图绕模型中心水平旋转 this.splitScreenTool.rotateSideCamera(viewer.mainViewport, angle); } zoomIn(intersect, pointer){ let camera = viewer.mainViewport.camera; let endZoom = 200; //this.orthoMoveFit(intersect, {endZoom:viewer.mainViewport.camera.zoom < aimZoom ? aimZoom : null} , 300) let startZoom = camera.zoom; if(startZoom >= endZoom){return} viewer.mainViewport.view.zoomOrthoCamera(camera, endZoom, pointer, 300); } orthoMoveFit(pos, info, duration){ var margin = {x:200, y:230}; this.splitScreenTool.viewportFitBound(viewer.mainViewport, info.bound, pos, duration, margin ); } setZoomInState(state, informinformBy2d){//是否点击后可放大 //if(state && this.activeViewName == 'mainView')return console.log('3D不可放大') this.clickToZoomInEnabled = !!state; if(state){ viewer.dispatchEvent({type : "CursorChange", action : "add", name:"zoomInCloud"} ); }else { viewer.dispatchEvent({type : "CursorChange", action : "remove", name:"zoomInCloud" }); } if(!state && !informinformBy2d){ this.dispatchEvent({type:'operationCancel', operation: 'zoomIn'}); } } gotoFloor(floor, force, duration = 600, informBy2d, fitBound=true){// 选择不同楼层, 切换点位显示。 'all'为全部显示 floor = floor || 'all'; if(this.currentFloor == floor && !force)return if(this.currentFloor != floor){//如果楼层没变,不修改可视 //let pointclouds = viewer.findPointcloudsAtFloor(floor) let panos = floor == 'all' ? viewer.images360.panos : floor.panos; viewer.images360.panos.forEach(pano=>{ let v = panos.includes(pano); this.switchPanoVisible(pano,v); }); } this.updateLinesVisible(); //切换楼层时清空选择状态 if(this.selectedPano && floor != 'all' && !floor.panos.includes(this.selectedPano)){ this.selectedPano.circle.dispatchEvent('click'); } if(this.selectedLine){ this.selectedLine.dispatchEvent('click'); } let bound, center; if(floor == 'all'){ bound = viewer.images360.bound.bounding; center = viewer.images360.bound.center; }else { bound = this.getPanosBound(floor); center = bound.getCenter(new Vector3()); if(floor.panos.length == 0)console.log(floor.name, 'floor无漫游点' ); } if(this.activeViewName != 'mainView' ){ fitBound && this.orthoMoveFit(center, {bound}, duration); }else if(this.activeViewName == 'mainView'){ //if(floor != 'all'){ //切换一下位置,因为原处点云会消失 //viewer.scene.view.setView({position:center, duration }) viewer.focusOnObject({boundingBox:bound},'boundingBox'); //} } this.currentFloor = floor; //if(!informBy2d){ //注释原因:2d居然不会自己变 this.dispatchEvent({type:'changeFloor', floor}); //} } getPanosBound(floor){ if(!floor.panosBound){ if(floor.panos.length == 0){ floor.panosBound = viewer.images360.bound.bounding.clone(); }else { let minSize = new Vector3(10,10,10); let bound = math.getBoundByPoints(floor.panos.map(e=>e.position), minSize); floor.panosBound = bound.bounding; } } return floor.panosBound } switchPanoVisible(pano, v, informBy2d){ //console.log(pano.id,v) Potree.Utils.updateVisible(pano.circle , 'panoEditor', v); Potree.Utils.updateVisible(pano, 'panoEditor', v); Potree.Utils.updateVisible(pano.pointcloud, 'panoEditor', v); if(v){ this.visiblePanos.includes(pano) || this.visiblePanos.push(pano); }else { let index = this.visiblePanos.indexOf(pano); index>-1 && this.visiblePanos.splice(index,1); } if(informBy2d){ this.dispatchEvent('panoVisiReady'); this.updateLinesVisible(); } informBy2d || this.dispatchEvent({type:"switchPanoVisible", pano, v}); /* { setTimeout(()=>{ Common.intervalTool.isWaiting('updatePointLevels2', ()=>{ this.updatePointLevels() }, 50) },1)//等update过visibleNodes } */ } updateLinesVisible(){ this.lineMeshes.children.forEach(line=>{ let names = line.name.split('-'); var pano0 = images360.getPano(names[0]); var pano1 = images360.getPano(names[1]); line.visible = this.visiblePanos.includes(pano0) || this.visiblePanos.includes(pano1); }); } updateCursor(){ let cursor; if(this.activeViewName == 'mainView' || !this.selectedPano){ cursor = null; }else { cursor = Alignment$1.handleState; } if(cursor == 'rotate'){ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"rotatePointcloud" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"movePointcloud" }); }else if(cursor == 'translate'){ viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"movePointcloud" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"rotatePointcloud" }); }else { viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"movePointcloud" }); viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"rotatePointcloud" }); } //this.cursorState = cursor } setLinkOperateState(name, state, informinformBy2d){ if(state && name == this.operation || !state && name != this.operation)return let old = this.operation; this.operation = state ? name : null; if(this.operation == 'removeLink'){ if(this.selectedLine){ this.selectedLine.dispatchEvent('click');//删除 } if(this.selectedPano && clickPanoToDisLink){ this.selectedPano.circle.dispatchEvent('click');//删除 } } if(this.operation != 'addLink'){ this.linkGuideLine.visible = false; } if(!state && !informinformBy2d){ this.dispatchEvent({type: "operationCancel", operation: old}); } if(this.operation == 'addLink'){ viewer.dispatchEvent({type : "CursorChange", action : "add", name:"connectPano"} ); }else { viewer.dispatchEvent({type : "CursorChange", action : "remove", name:"connectPano"} ); } if(this.operation == 'removeLink'){ viewer.dispatchEvent({type : "CursorChange", action : "add", name:"disconnectPano"} ); }else { viewer.dispatchEvent({type : "CursorChange", action : "remove", name:"disconnectPano"} ); } viewer.dispatchEvent('content_changed'); } ///////////////////////////////// initPanoLink(){ images360.panos.forEach((pano)=>{ this.panoLink[pano.id] = {}; this.panoGroup.push([pano]); }); images360.panos.forEach((pano)=>{ pano.visibles.forEach(index=>{//visibles中存的是下标! this.linkChange(pano, images360.getPano(index,'index'), 'add'); }); }); //console.log('panoLink',this.panoLink) } linkChange(pano0, pano1, type){//修改link (type == 'remove'时,pano1可以为空) let temp = []; if(type == 'add'){ if(!pano1)return console.error('不支持add时pano1为空') this.panoLink[pano0.id][pano1.id] = this.panoLink[pano0.id][pano1.id] || {}; this.panoLink[pano1.id][pano0.id] = this.panoLink[pano1.id][pano0.id] || {}; }else { if(!pano1){ for(let id in this.panoLink[pano0.id]){ if(this.panoLink[pano0.id][id]){ this.panoLink[id][pano0.id] = false; temp.push(id); } } this.panoLink[pano0.id] = {}; //全部断连 }else { this.panoLink[pano0.id][pano1.id] = false; this.panoLink[pano1.id][pano0.id] = false; } } if(!pano1){ //全部断连 temp.forEach(id=>{ this.lineChange(pano0, images360.getPano(id) , type); }); }else { this.lineChange(pano0, pano1, type); } this.groupChange(pano0, pano1, type); //this.updateSelectGroup() this.selectPano(this.selectedPano, false,true); //更新选中点云显示 } lineChange(pano0, pano1, type){//修改line if(type == 'add'){ if(this.panoLink[pano0.id][pano1.id].line) return let line = LineDraw.createFatLine([pano0.position, pano1.position], {mat:lineMats$3.default}); line.name = `${pano0.id}-${pano1.id}`; line.renderOrder = line.pickOrder = renderOrders$1.line; this.lineMeshes.add(line); this.panoLink[pano0.id][pano1.id].line = this.panoLink[pano1.id][pano0.id].line = line; line.addEventListener('mouseover', ()=>{ if(this.clickToZoomInEnabled)return //if(this.activeViewName == 'mainView')return if(this.selectedLine != line)line.material = lineMats$3.hovered; viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"hoverLine" }); }); line.addEventListener('mouseleave', ()=>{ if(this.clickToZoomInEnabled)return //if(this.activeViewName == 'mainView')return if(this.selectedLine != line)line.material = lineMats$3.default; viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"hoverLine" }); }); line.addEventListener('click', (e)=>{ if(this.clickToZoomInEnabled)return //if(this.activeViewName == 'mainView')return if(this.operation == 'removeLink'){ if(this.selectedLine == line) this.selectLine(null); return this.linkChange(pano0, pano1, 'remove') } this.selectLine(line); }); }else { let line = this.lineMeshes.children.find(e=>e.name == `${pano0.id}-${pano1.id}` || e.name == `${pano1.id}-${pano0.id}` ); if(line){ this.lineMeshes.remove(line); line.geometry.dispose(); } } } groupChange(pano0, pano1, type){//修改group (type == 'remove'时,pano1可以为空) if(type == 'add'){ Common.pushToGroupAuto([pano0, pano1], this.panoGroup ); }else { let atGroup = this.panoGroup.find(e=>e.includes(pano0) && (e.includes(pano1) || !pano1));//所在组 if(!atGroup){ if(pano1){ console.log('这两个pano原本就不在一个组', pano0.id, pano1.id); }else { console.log('pano0不在任何组', pano0); } return } //断开连接时,因为组内没有其他成员的连接信息,所以需要清除整组,并将剩余的一个个重新连接 this.panoGroup.splice(this.panoGroup.indexOf(atGroup),1); //删除 atGroup.forEach(pano=>{//然后再重新生成这两个和组的关系,各自分组 this.panoGroup.push([pano]); for(let id in this.panoLink[pano.id]){ if(this.panoLink[pano.id][id]){ let pano_ = images360.getPano(id); Common.pushToGroupAuto([pano, pano_], this.panoGroup ); } } }); } } selectLine(line){ if(this.selectedLine == line)return if(this.selectedLine){ this.selectedLine.material = lineMats$3.default; } if(line){ line.material = lineMats$3.selected; } this.selectedLine = line; } addPanoMesh(){ let map = texLoader$c.load(Potree.resourcePath+'/textures/correct_n.png' ); /* circleMats.default_normal = new THREE.MeshBasicMaterial({ map, color: 0xffffff, transparent: true, depthTest: false, depthWrite: false, }) */ window.circleMats = circleMats; circleMats.default_normal = new DepthBasicMaterial({ map, color: 0xffffff, transparent: true, useDepth:true, backColor: 0x33ffdd, occlusionDistance: 10,//变为backColor距离 clipDistance : 5,//消失距离 maxClipFactor: 0.8, maxOcclusionFactor: 0.8, }); circleMats.default_rtk_on = circleMats.default_normal.clone(); circleMats.default_rtk_on.map = texLoader$c.load(Potree.resourcePath+'/textures/rtk-y-n.png' ); circleMats.default_rtk_off = circleMats.default_normal.clone(); circleMats.default_rtk_off.map = texLoader$c.load(Potree.resourcePath+'/textures/rtk-f-n.png' ); circleMats.selected_normal = circleMats.default_normal.clone(); circleMats.selected_normal.map = texLoader$c.load(Potree.resourcePath+'/textures/correct_s.png' ); circleMats.selected_normal.useDepth = false; circleMats.selected_rtk_on = circleMats.selected_normal.clone(); circleMats.selected_rtk_on.map = texLoader$c.load(Potree.resourcePath+'/textures/rtk-y-s.png' ); circleMats.selected_rtk_off = circleMats.selected_normal.clone(); circleMats.selected_rtk_off.map = texLoader$c.load(Potree.resourcePath+'/textures/rtk-f-s.png' ); circleMats.hovered_normal = circleMats.default_normal.clone(); circleMats.hovered_normal.color.set(0x00ff00); circleMats.hovered_normal.useDepth = false; circleMats.hovered_rtk_on = circleMats.default_rtk_on.clone(); circleMats.hovered_rtk_on.color.set(0x00ff00); circleMats.hovered_rtk_on.useDepth = false; circleMats.hovered_rtk_off = circleMats.default_rtk_off.clone(); circleMats.hovered_rtk_off.color.set(0x00ff00); circleMats.hovered_rtk_off.useDepth = false; let setPos = (circle)=>{ circle.position.copy(circle.pano.position); for(let id in this.panoLink[circle.pano.id]){ let linkInfo = this.panoLink[circle.pano.id][id]; if(linkInfo){ LineDraw.updateLine(linkInfo.line, [circle.pano.position, images360.getPano(id).position] ); } } circle.waitUpdate(); //update sprite Matrix }; images360.panos.forEach(pano=>{ var circle = new Sprite$2({mat: circleMats['default' + '_'+ this.getPanoRtkState(pano) ] , sizeInfo:{ minSize : 50 , maxSize : 120, nearBound : 2, farBound : 10, }, renderOrder : renderOrders$1.circle, pickOrder: renderOrders$1.circle }); circle.pickDontCheckDis = true; circle.name = 'panoCircle'; circle.sid = pano.id; circle.pano = pano; pano.circle = circle; this.panoMeshs.add(circle); setPos(circle); pano.addEventListener('rePos', setPos.bind(this,circle)); let drag = (e)=>{ /* if(this.activeViewName == 'mainView' && this.tranMode == 'translate'){//如果3d页不禁止xy的话,这段打开 this.transformControls.dispatchEvent('dragging')//触发拖拽 return } */ if(this.tranMode != 'translate' || this.activeViewName == 'mainView')return e.refuse() this.selectPano(circle.pano); //为了方便拖拽点云,拖动circle就直接选中 viewer.inputHandler.drag.object = null; //取消拖拽状态,否则不触发点云拖动 }; circle.addEventListener('drag', drag); circle.addEventListener('mouseover', ()=>{ this.hoverPano(pano,true); }); circle.addEventListener('mouseleave', ()=>{ this.hoverPano(pano,false); }); circle.addEventListener('click', ()=>{ //if(this.activeViewName == 'mainView')return if(this.clickToZoomInEnabled)return if(clickPanoToDisLink && this.operation == 'removeLink'){ this.linkChange(pano, null, 'remove'); //删除所有连接 } if(this.selectedPano == circle.pano) return this.selectPano(null) if(this.operation == 'addLink' && this.selectedPano){ this.linkChange(this.selectedPano, circle.pano, 'add'); //this.setLinkOperateState('addLink',false) return } //if(this.operation == 'removeLink' && this.selectedPano){ //和选择中心点冲突 // this.linkChange(this.selectedPano, circle.pano, 'remove') // //this.setLinkOperateState('removeLink',false) // return // } this.selectPano(circle.pano); }); }); } hoverPano(pano, state){ if(this.clickToZoomInEnabled)return if(pano && state){ //在hover一个pano之前,一定会先取消已经hover的pano, 最多存在一个hovered的pano if(this.hoveredPano == pano)return if(this.hoveredPano){ this.hoverPano(this.hoveredPano,false); } this.hoveredPano = pano; pano.hovered = true; if(/* this.activeViewName == 'mainView' || */Alignment$1.handleState && this.selectedPano && this.selectedPano == pano)return if(this.operation != 'addLink' || !this.selectedPano || this.selectedPano == pano){ // this.selectedPano == pano? viewer.dispatchEvent({ type : "CursorChange", action : "add", name:"hoverPano" }); } if(this.selectedPano != pano) pano.circle.material = circleMats['hovered' + '_'+ this.getPanoRtkState(pano) ]; }else if(pano && !state){//unhover if(this.hoveredPano != pano)return pano.hovered = false; viewer.dispatchEvent({ type : "CursorChange", action : "remove", name:"hoverPano" }); if(this.selectedPano != pano) pano.circle.material = circleMats['default' + '_'+ this.getPanoRtkState(pano) ]; this.hoveredPano = null; }else {//unhover any if(this.hoveredPano){ this.hoverPano(this.hoveredPano, false); } } } selectPano(pano, informinformBy2d, force){ if(this.selectedPano == pano && !force)return let lastSeletedPano = this.selectedPano; let opaProp = this.activeViewName == 'top' ? opacitys.topView : opacitys.sideView; if(this.selectedPano){ this.selectedPano.circle.material = circleMats['default' + '_'+ this.getPanoRtkState(this.selectedPano) ]; this.selectedPano.circle.renderOrder = renderOrders$1.circle; this.selectedPano.removeEventListener('rePos',this.panoReposCallback); if(this.activeViewName == 'mainView'){ }else { this.selectedClouds.forEach(e=>{ e.changePointOpacity(opaProp.default,true); e.material.color = pointColor.default; }); } } this.selectedPano = pano || null; this.updateSelectGroup(); if(pano){ this.selectedPano.circle.material = circleMats['selected' + '_'+ this.getPanoRtkState(this.selectedPano) ]; this.selectedPano.circle.renderOrder = this.selectedPano.circle.pickOrder = renderOrders$1.circleSelected; //侧视图能显示在最前 viewer.controls.setTarget(this.selectedPano.position); //3d时绕其为中心转动 this.selectedPano.addEventListener('rePos',this.panoReposCallback); if(this.activeViewName == 'mainView'){ }else { this.selectedClouds.forEach(e=>{ e.changePointOpacity(opaProp.selected,true); e.material.color = pointColor.selected; }); } if(this.currentFloor != 'all'){//如果原本不是展示全部楼层的话,自动切换楼层 let atFloor = SiteModel$1.entities.find(e=>e.buildType == 'floor' && e.panos.includes(pano)); if(!atFloor){ atFloor = 'all'; }else { } this.gotoFloor(atFloor, false, 600 ); } }else { viewer.controls.setTarget(null); } this.updateCursor(); this.updateTranCtl(); if(informinformBy2d){ if(this.selectedPano){ if(this.activeViewName == 'mainView'){ //平移,focus选中的pano let distance = this.lastDisToPano || 5; if(lastSeletedPano){ distance = viewer.mainViewport.camera.position.distanceTo(lastSeletedPano.position); } viewer.focusOnObject({ position:this.selectedPano.position}, 'point', null, {distance }); }else { this.orthoMoveFit(this.selectedPano.position, {}, 500); } } }else { this.dispatchEvent({type:'panoSelect', pano }); } this.updateIntersectEnable(); viewer.dispatchEvent('content_changed'); } /* updatePointLevels(){ if(this.pauseUpdateLevels)return let maxBudget = Potree.config.pointDensity.panoEdit.pointBudget let visiCount1 = viewer.scene.pointclouds.filter(e=>e.visible).length let visiCount2 = viewer.scene.pointclouds.filter(e=>e.visibleNodes.length>0).length //屏幕范围内可见的个数 let maxCount = 200, minCount = 1, minPer = 0.45 , maxPer = 1 let percent1 = maxPer - ( maxPer - minPer) * THREE.Math.clamp((visiCount1 - minCount) / (maxCount - minCount),0,1) let percent2 = maxPer - ( maxPer - minPer) * THREE.Math.clamp((visiCount2 - minCount) / (maxCount - minCount),0,1) let percent = percent1*percent2 if(this.activeViewName == 'mainView' ){ //假设每个pointcloud所带的点个数大致相同,那么当可见点云个数越多,所能展示的level越低,否则因总个数超过budget的话密度会参差不齐。 //pointcloud.changePointSize() //console.log('updatePointLevels', percent, visiCount) Potree.settings.UserDensityPercent = Math.sqrt(percent2) viewer.setPointBudget(maxBudget * percent2) }else{ Potree.settings.UserDensityPercent = 1 viewer.setPointBudget(maxBudget * percent) } viewer.setPointBudget(maxBudget * percent) viewer.setPointLevels() //侧面容易卡顿,但和显示的点数无关,似乎是因加载点云多而卡?为何正面不会 //console.warn('setPointBudget', Potree.pointBudget, visiCount1,visiCount2, Potree.settings.UserDensityPercent) } */ getPanoRtkState(pano){ return pano.panosData.has_rtk ? pano.rtkState ? 'rtk_on' : 'rtk_off' : 'normal' } setPanoRtkState(pano,state){ pano.rtkState = state; pano.circle.material = circleMats[(this.selectedPano == pano ? 'selected' : 'default') + '_'+ this.getPanoRtkState(pano) ]; } updateSelectGroup(){//更新选中的组 this.selectedGroup = this.panoGroup.find(e=>e.includes(this.selectedPano)); if(this.selectedGroup){ this.selectedGroup = [this.selectedPano, ...this.selectedGroup.filter(e=>e != this.selectedPano)];//将选中的放第一个,便于旋转时绕其旋转。 } //this.selectedClouds = this.selectedPano ? (this.selectedGroup || [this.selectedPano]).map(e=>e.pointcloud) : [] this.selectedClouds = this.selectedPano ? this.selectedGroup.map(e=>e.pointcloud) : []; } checkIfCanSave(){//如果未全部相连,不能保存 for(let datasetId in Potree.settings.datasetsPanos ) { if(!this.checkIfAllLinked({datasetId})){ console.log('没有全部连通,不能保存。其中一个:', datasetId); return } } return true } checkIfAllLinked(o){//某个(or组所在的)数据集是否全部连通 let datasetId, group; if(o.group){ group = o.group; let pano = o.group[0]; if(!pano)return //会有没有漫游点的点云来编辑吗 datasetId = pano.pointcloud.dataset_id; }else if(o.datasetId){ datasetId = o.datasetId; group = this.panoGroup.find(panos=>panos[0].pointcloud.dataset_id == datasetId ); if(!group)return //要找的数据集的pano全部都孤立了 } if(datasetId == void 0)return let panos = Potree.settings.datasetsPanos[datasetId].panos; return panos.length == group.length } getSuggestLinkPanos(){//给出建议连接的点 let panos = []; let startTime = Date.now(); for(let datasetId in Potree.settings.datasetsPanos ) { if(!this.checkIfAllLinked({datasetId})){ let groups = this.panoGroup.filter(panos=>panos[0].pointcloud.dataset_id == datasetId ); groups = groups.sort((a,b)=>{return b.length - a.length});//找出个数最多的一组来连接其他组 let mainGroup = groups[0].slice(), subGroup; for(let i=1,len=groups.length;i{ let circle = new Mesh(pano.circle.geometry, suggestCircleMat); circle.name = 'suggest-circle'; circle.scale.set(s,s,s); circle.renderOrder = 100; pano.circle.add(circle); }; groups.forEach(panos=>{ createCircle(panos[0]); createCircle(panos[1]); let line = LineDraw.createFatLine([panos[0].position, panos[1].position], {mat: lineMats$3.suggestLink}); this.suggestLines.push(line); line.renderOrder = renderOrders$1.line; viewer.scene.scene.add(line); }); } getPanoPose(pano){ let pose = { position: pano.position.clone(), quaternion: new Quaternion().setFromRotationMatrix(pano.panoMatrix).premultiply(rotQua) , }; return pose } exportSavingData(){//输出漫游点新的坐标和朝向、以及连接信息 let sweepLocations = {}; for(let datasetId in Potree.settings.datasetsPanos ) { let {panos} = Potree.settings.datasetsPanos[datasetId]; let data = panos.map(pano=>{ let visibles = []; for(let id in this.panoLink[pano.id]){ if(this.panoLink[pano.id][id]){ visibles.push(viewer.images360.getPano(id).index); } } let {position, quaternion} = this.getPanoPose(pano); return Object.assign({}, pano.panosData, { uuid: pano.uuid, /* pose:{ translation: dealData(pano.position.clone() ), rotation: dealData(new THREE.Quaternion().setFromRotationMatrix(pano.panoMatrix).premultiply(rotQua) ), }, */ pose : { translation : dealData(position), rotation : dealData(quaternion) }, visibles, use_rtk : !!pano.rtkState //subgroup: 0,group: 1, "id_view":.. }) }); sweepLocations[datasetId] = {sweepLocations:data}; } /* this.lineMeshes.children.forEach(e=>{//从line中搜集连接信息,而不从linkInfo,这样visibles不会重复一次 let names = e.name.split('-') //是不是该转成数字 var pano0 = names[0] var pano1 = names[1] sweepLocations.find(s=>s.uuid == pano0).visibles.push(pano1) }) */ function dealData(value){ let v = math.toPrecision(value, 6); if(v instanceof Quaternion){ return {x:v.x, y:v.y, z:v.z, w:v.w} }else if(v instanceof Vector3){ return {x:v.x, y:v.y, z:v.z} } } //console.log(sweepLocations) return sweepLocations } } /* 不同数据集之间不能连线 不同楼层可能也不能 如果楼层在不同建筑物怎么办? 楼层切换按钮只能在一个建筑内切换。 全部相连时不能移动和旋转 如果未全部相连,不能保存 */ var PanoEditor$1 = new PanoEditor(); //import {Prism} from './Prism.js' const maxWidth = 4096; const surfaceThick = 0.1 ;//最大表面厚度 const pointDensity = 'screenshot'; const maxPointBudge = Potree.config.pointDensity[pointDensity].pointBudget; const Shaders$1 = { 'modelHeight.vs':` precision highp float; uniform vec2 boundZ; varying float heightPercent; float round(float number){ return floor(number + 0.5); } void main() { vec3 worldPos = (modelMatrix * vec4(position, 1.0)).xyz; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); float zMin = boundZ.x, zMax = boundZ.y ; heightPercent = (worldPos.z - zMin) / (zMax - zMin); } ` , 'modelHeight.fs': ` precision highp float; varying float heightPercent; vec3 percentToByte(float num){ //输出是0-1, shader精度不够,只够存3个数,因第四位总是0 float a = 1.0; float result[4]; for(int i=0;i<3;i++){ a = i == 2 ? a/255.0 : a / 256.0 ; //分成256份,其中255份给当前位置,最后一份给后一个位置,而到了最后一个位置时没有下一个位置了所以只分成255份 if(num > a){ float c = num / a; float r = floor(c); r = min(255.0, r); result[i] = r; num -= r * a; }else{ result[i] = 0.0; } } return vec3(result[0]/255.0, result[1]/255.0,result[2]/255.0); } void main() { gl_FragColor = vec4(percentToByte(heightPercent),1.0); } ` }; let horizonZ, zMin, zMax, lowest, highest, wDelta, sDelta, deferred, interrupt, timestamp, buffer1, buffer2, modelZs = []; let testPoint$1 = false; //const asyncTimeout = (delay) => new Promise(resolve => setTimeout(resolve, delay)) const delayForNotify = 5, notifyInterval = 500; let lastNotifyTime = Date.now(); let curPercent; const notify = (areas, num, str )=>{ //num: 0-1 return new Promise(resolve => { if(!deferred)return resolve() let now = Date.now(); if(now - lastNotifyTime < notifyInterval){ return resolve() } lastNotifyTime = now; if(num == void 0){ num = curPercent; }else { let s = 0, e = 1; if(num instanceof Function ){ num = num(); } areas.forEach((area)=>{ let s_ = e * area[0] + s * (1-area[0]); let e_ = e * area[1] + s * (1-area[1]); s = s_, e = e_; }); let p = e * num + s * (1-num); str == 'loadpoints' && Potree.settings.isTest && console.log('notify pro', p, str); deferred.notify(p); curPercent = p; if(isNaN(p)){ console.log('nan'); } } setTimeout(resolve, delayForNotify); }) }; class VolumeComputer extends EventDispatcher{ constructor(){ super(); this.camera = new OrthographicCamera(-100,100,-100,100 , 0.01, 100); this.camera.up.set(0,0,1); Potree.Utils.setCameraLayers(this.camera, ['model']); this.view = new ExtendView; this.viewport = new Viewport(this.view,this.camera,{left:0,bottom:0,width:1,height:1}); this.renderTarget = new WebGLRenderTarget( 1, 1, { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat } ); this.renderTarget2 = new WebGLRenderTarget( 1, 1, { minFilter: LinearFilter, magFilter: NearestFilter, format: RGBAFormat } ); this.renderTarget2.texture.name = 'volumeCptModelTex'; this.material = new ExtendPointCloudMaterial(); this.material.activeAttributeName = testPoint$1 ? "rgba" : 'heightCpt' ;// 'rgba' indices prismHeight this.prisms = []; this.modelMat = new ShaderMaterial({ uniforms:{ boundZ: { type: "vec2", value: new Vector2 } }, vertexShader: Shaders$1['modelHeight.vs'], fragmentShader: Shaders$1['modelHeight.fs'], depthWrite: true, depthTest: true, transparent: false, side: DoubleSide }); this.scene = new Scene; } init(){ viewer.scene.addEventListener('measurement_added',(e)=>{ if(e.measurement.isPrism) this.add(e.measurement); }); viewer.scene.addEventListener('measurement_removed',(e)=>{ if(e.measurement.isPrism) this.remove(e.measurement); }); let this_ = this; this.events = { transformCallback: ()=>{ if(!this_.currentPrism)return //Common.intervalTool.isWaiting('volCptTransformed', ()=>{ //延时update,防止卡顿 let object = viewer.transformationTool.selection[0]; if(object == this_.currentPrism.baseModel ){ this.updateModelBound({modelChange:true}); } this_.currentPrism.dispatchEvent('needsCompute'); //模型变动或clipBox变动后要重算 //}, 1000) }, }; } enter(){ this.entered = true; viewer.measuringTool.history.clear(); //避免撤销到测量线去 this.oldStates = { rotAroundPoint : Potree.settings.rotAroundPoint }; Potree.settings.rotAroundPoint = false; //viewer.objs.traverse(e=>e.material && ( e.material = this.material)) viewer.transformationTool.addEventListener('stopDrag', this.events.transformCallback); viewer.transformationTool.addEventListener('changeByHistory', this.events.transformCallback); } leave(){ this.entered = false; Potree.settings.rotAroundPoint = this.oldStates.rotAroundPoint; this.setCurrentPrism(null); viewer.transformationTool.removeEventListener('stopDrag', this.events.transformCallback); viewer.transformationTool.removeEventListener('changeByHistory', this.events.transformCallback); } add(prism){ this.prisms.push(prism); } remove(prism){ let i = this.prisms.indexOf(prism); i>-1 && this.prisms.splice(i,1); } cancel(){ if(this.computingObject){ interrupt = true; } } startCompute(model,dontCompute){//test this.enter(); let deferred = $.Deferred(); deferred.done((e)=>{ console.log('done', e); }); deferred.fail((e)=>{ console.log('?fail????', e); }); /* deferred.progress((e)=>{ console.log('progress', e) }) */ if(model){ this.prisms[this.prisms.length-1].setBaseModel(model); this.setCurrentPrism(this.prisms[this.prisms.length-1]); dontCompute || this.compute(this.prisms[this.prisms.length-1]); }else { this.compute(this.prisms[this.prisms.length-1], deferred ); } } setCurrentPrism(prism ){//当进入单个prism编辑后才会高亮点云 if(this.currentPrism){ this.currentPrism.removeEventListeners('updated'); } this.currentPrism = prism; //允许没有model if(!prism || !prism.baseModel/* || !prism.baseModel.visible */){ viewer.scene.pointclouds.forEach(e=>{ this.updateMatForModel(e.material ); //取消高亮 }); return } prism.addEventListener('updated',()=>{ if(this.currentPrism != prism){ return console.log('currentPrism changed? 侦听没删除') } this.updateModelBound(); }); this.updateModelBound({regetMap:true}); } setModelMat(model, type){ if(type == 'recover'){ model.traverse(e=>e.originMat_ && (e.material = e.originMat_ )); }else { model.traverse(e=>e.material && e.material != this.modelMat && ( e.originMat_ = e.material, e.material = this.modelMat )); } } updateModelBound({modelChange, regetMap}={}){ //regetMap是在更换currentPrism必须重绘map,但不需要needUpdate let prism = this.currentPrism; let model = this.currentPrism.baseModel; if(!model)return model.currentBound = model.boundingBox.clone().applyMatrix4(model.matrixWorld); //作用同prismBound let oldBound = model.clipBound && model.clipBound.clone(); model.clipBound = model.currentBound.clone();//高亮范围 model.clipBound.min.max(prism.prismBound.min);//xy为交集 model.clipBound.max.min(prism.prismBound.max); model.clipBound.min.z = Math.min(model.currentBound.min.z, prism.prismBound.min.z);//z为并集,范围覆盖prism点、点云、model三者 model.clipBound.max.z = Math.max(model.currentBound.max.z, prism.prismBound.max.z); this.modelMat.uniforms.boundZ.value.set(model.clipBound.min.z,model.clipBound.max.z); let boundChanged = !oldBound || !oldBound.equals(model.clipBound); let boundSize = this.currentPrism.baseModel.clipBound.getSize(new Vector3); this.boundSizeZero = boundSize.x<=0 ||boundSize.y<=0 || boundSize.z<=0; //会造成renderTarget尺寸为0,背景变白,报错:Framebuffer is incomplete: Attachment has zero size 和 The texture is a non-power-of-two texture. if( (modelChange || regetMap || boundChanged) && !this.boundSizeZero){ this.setModelHeightMap();//包含cad在俯视中的范围 + 高度信息 //this.setModelMat(model,this.boundSizeZero ? 'recover' : null) } viewer.scene.pointclouds.forEach(e=>{//暂时先只支持一个高亮区域 this.updateMatForModel(e.material, model); }); //需要重新渲染modelHeightmap的情况:bound改变、模型变动、更换currentPrism后 //注意,即使上传几种都没发生,仅仅移动prism的marker也要重算,假设一个marker在model范围内,移动后bound可能不变但体积肯定变。 /* if(boundChanged){ if(prism.recoverNeedCptNextTime){ prism.needsCompute = 'byVolume' //初始化,有volume就不用重算 prism.recoverNeedCptNextTime = false }else{ prism.dispatchEvent('needsCompute') } } */ } updateMatForModel(material, model){ let oldMap = material.uniforms.baseHeightAreaMap.value; if(model){ material.uniforms.baseHeightAreaMap.value = this.renderTarget2.texture; //model.baseHeightAreaMap ; material.uniforms.baseHeightBoundZ.value.set(model.clipBound.min.z,model.clipBound.max.z); material.uniforms.baseHeightBoundXY.value.set(model.clipBound.min.x, model.clipBound.max.x, model.clipBound.min.y, model.clipBound.max.y); }else { material.uniforms.baseHeightAreaMap.value = null; } if(oldMap != material.uniforms.baseHeightAreaMap.value ){ material.shaderNeedsUpdate = true; } } setModelHeightMap(){ let oldTarget = viewer.renderer.getRenderTarget(); let oldParent = this.currentPrism.baseModel.parent; let oldVisi = this.currentPrism.baseModel.visible; let boundSize = this.currentPrism.baseModel.clipBound.getSize(new Vector3); let boundCenter = this.currentPrism.baseModel.clipBound.getCenter(new Vector3); let {w, h} = this.getResByBound(boundSize,'forModel'); if(w<=0 || h<=0){ return console.error('w<=0 || h<=0!!!!!!!!!', w,h) } /* w = THREE.Math.floorPowerOfTwo( w ); h = THREE.Math.floorPowerOfTwo( h ); */ //for warning:The texture is a non-power-of-two texture this.camera.far = boundSize.z + 10; this.camera.right = w / 2; this.camera.left = -this.camera.right; this.camera.top = h / 2; this.camera.bottom = -this.camera.top; this.viewport.resolution.set(w , h); this.renderTarget2.setSize(w, h); viewer.renderer.setRenderTarget(this.renderTarget2); console.log('setModelHeightMap'); //最高处朝下看 let endPosition = new Vector3; endPosition.copy(boundCenter).setZ(this.currentPrism.baseModel.clipBound.max.z + this.camera.near+0.1); this.view.pitch = -Math.PI / 2; this.view.moveOrthoCamera(this.viewport, {boundSize , endPosition }, 0); this.view.applyToCamera(this.camera); this.scene.add(this.currentPrism.baseModel); this.setModelMat(this.currentPrism.baseModel); this.currentPrism.baseModel.visible = true; let gl = viewer.renderer.getContext(); viewer.renderer.setClearColor(new Color('#000'), 0); viewer.renderer.clear(); viewer.renderer.render(this.scene,this.camera); this.setModelMat(this.currentPrism.baseModel, 'recover'); let pixelCount = this.renderTarget2.width * this.renderTarget2.height; buffer2 = new Uint8Array(4 * pixelCount); //因为需要渲染完立即readPixels才能获取到,所以只能这时候就read了 gl.readPixels(0, 0, this.renderTarget2.width , this.renderTarget2.height, gl.RGBA, gl.UNSIGNED_BYTE, buffer2); //这句花费最多时间 pc:2-4, 即使只有1*1像素 modelZs = []; //等待计算 if(window.needDownload) { let dataUrl = Potree.Utils.renderTargetToDataUrl(this.renderTarget2, this.renderTarget2.width, this.renderTarget2.height, viewer.renderer); Common.downloadFile(dataUrl, (name || 'screenshot') + '-' + Date.now() +'.png'); } oldParent.add(this.currentPrism.baseModel); this.currentPrism.baseModel.visible = oldVisi; viewer.renderer.setRenderTarget(oldTarget); this.renderTarget2.texture.needsRebuild = true; //renderTarget在resize后会触发dispose, 然后 _gl.deleteTexture( textureProperties.__webglTexture )所以需要重新建立 否则图片变黑 return this.renderTarget2.texture } getResByBound(boundSize, type){ //let pxPerMetric = math.linearClamp(Math.max(boundSize.x,boundSize.y), [4, 10, 20, 80, 500, 2000], [300, 200, 100, 30, 6, 3]) let pxPerMetric = math.linearClamp(Math.max(boundSize.x,boundSize.y), [ 10, 60, 100, 500, 2000], [150, 60, 40, 6, 3]); let w = pxPerMetric * boundSize.x, h = boundSize.y * pxPerMetric; let cW = 1 , cH = 1; //横向纵向渲染次数 超过maxWidth时分批渲染 if(w > maxWidth || h > maxWidth){ /* if(){//多数据集判断是否间隔过远,是的话返回 } */ if(type == 'forCompute'){//计算可以分多次渲染 cW = Math.ceil(w / maxWidth), cH = Math.ceil(h / maxWidth); w /= cW, h /= cH; }else if(type == 'forModel'){//但是model的贴图只能有一个 if(w>h){ w = maxWidth, h = w * boundSize.y / boundSize.x; }else { h = maxWidth, w = h * boundSize.x / boundSize.y; } } } w = Math.round(w); h = Math.round(h); return {w,h,pxPerMetric, cW,cH} } //法1:只用一个顶部相机(当挖方遮挡填方时不准,但好算) async compute(prism, deferred_, getResolveResult){ if(!prism || prism.modelHaventLoad){ console.log('prism有问题'); return deferred_.reject() } if(this.computingObject) return console.log('正在计算,请稍等') let oldCurPrism = this.currentPrism; if(prism != this.currentPrism){//在列表外点击计算 this.setCurrentPrism(prism); } let computeDone = ({highest,lowest,Vupper,Vlower}, deferred )=>{ prism && prism.setVolumeInfo({ highest,lowest,Vupper,Vlower }); if(oldCurPrism != prism){ this.setCurrentPrism(oldCurPrism); } deferred && deferred.resolve(getResolveResult && getResolveResult()); }; if(prism.baseModel && this.boundSizeZero){ //bound没有交集 或 prism的boundsize为0 return computeDone({highest:0,lowest:0,Vupper:0,Vlower:0}, deferred_ ) } this.computingObject = {prism, model: prism.baseModel}; deferred = deferred_; let computeFinish = (debugStr, makeit)=>{ makeit || deferred.reject(interrupt ? '主动取消' : (debugStr || '')); this.computingObject = null; deferred = null; interrupt = false; debugStr && console.log('computeFinish',debugStr); Potree.settings.displayMode = statesBefore.mode; Potree.settings.pointDensity = statesBefore.pointDensity; viewer.renderer.setRenderTarget(null); viewer.renderer.state.reset(); viewer.renderer.setScissorTest(false); gl.disable(gl.SCISSOR_TEST); viewer.screenshoting = false; viewer.mainViewport.active = true; viewer.mapViewer && (viewer.mapViewer.viewports[0].active = true ); viewer.magnifier.viewport.active = true; viewer.pauseTestMaxLevel = false; }; let pointcloud = viewer.scene.pointclouds[0]; let center = new Vector3; let boundSize = new Vector3; let bound; let gl = viewer.renderer.getContext(); let statesBefore = { mode: Potree.settings.displayMode, pointDensity:Potree.settings.pointDensity }; timestamp = Date.now(); if(!this.computingObject.model){ horizonZ = prism.horizonZ; zMin = prism.zMin; zMax = prism.zMax; bound = new Box3; for(let i=0;i{return {inverse:box.matrixWorld.clone().invert(), box}} ); this.material.setClipBoxes(null,[], clipBoxes_out,[],[prism]); this.material.pointSizeType = 'FIXED'; //'ADAPTIVE' //node会自动根据的其level更改点大小 使用ADAPTIVE后会使baseModelMap整片是黑的,原因未知 this.material.uniforms.minSize.value = 0.1; this.material.uniforms.orthoMaxSize.value = 5; //px this.material.classification = pointcloud.material.classification; this.material.recomputeClassification(); this.material.shaderNeedsUpdate = true; pointcloud.updateMaterial(this.material, null, this.camera, viewer.renderer, this.viewport.resolution); this.material.size = 1;//this.getPointsize(pointcloud.maxLevel) //0.025 米 //视图缩小(截图面积变大)后点会自动缩小,但至少是1个像素大小 } viewer.renderer.state.buffers.depth.setTest(this.material.depthTest); viewer.renderer.state.buffers.depth.setMask(this.material.depthWrite); viewer.renderer.state.setBlending(NoBlending); //只需要设置不透明即可(使alpha不混合,后方不会影响前方的alpha,否则输出的a通道将没法用) let pixelCount = this.renderTarget.width * this.renderTarget.height; buffer1 = new Uint8Array(4 * pixelCount); viewer.renderer.setRenderTarget(this.renderTarget); let results = []; let boundSize_ = boundSize.clone(); boundSize_.x /= cW, boundSize_.y /= cH; //分批渲染:从左到右 从下到上 for(let i=0; i= infos[0].Vlower 因能找到被上方遮挡住的凹槽 allDiffHeights : [], fillVupperCount:0, fillVlowerCount:0, infos }; let waitFill = []; waitFill.count = 0; let isNeighbor = (a,b)=>{ return Math.abs(a.u - b.u ) <= 1 && Math.abs(a.v - b.v ) <= 1 }; let isBelongToGroup = (group, item)=>{ if(item[0])item = item[0]; let i=group.length; while(--i >=0){//从后往前 if(item.u-group[i].u > 1 && Math.abs(item.v-group[i].v) > 1) break //距离超出,前面肯定不相邻了 if(isNeighbor(group[i], item)){ return true } } }; //计算挖方(但是边缘附近垂直方向上很多点云,体积被削皮,最终得到的会比infos[0]少) let getValue = (u,v)=>{//当到了左上边界是否需要向前几批的查找? if(infos[0].allHeights[u] && infos[0].allHeights[u][v] != void 0 )return infos[0].allHeights[u][v] return result.allFills[u] && result.allFills[u][v] }; let hasValue = (u,v)=>{//在边界内 return u >= 0 && u < infos[0].allHeights.length && v >= 0 && v < infos[0].allHeights[0].length }; let getFill = (u,v)=>{//使用周围的八个点的平均值补洞 let neighbours = [ [-1,0],[0,1],[1,0],[0,-1], [-1,-1], [1,-1], [-1,1],[1,1]];//先左右右先上后下 let i=0, c=0, sum=0; while(c<4 && i<8){ let x = neighbours[i][0]+u, y = neighbours[i][1]+v; if(hasValue(x,y)){ let neigh = getValue(x,y); if(neigh != void 0){ c++; sum += neigh; }else if(i>=3){ //add 2024.5.27 if(c == 0)break //节省时间,主要为了大片透明区域 } } i++; } if(c>0){ sum /= c; result.allFills[u] || (result.allFills[u] = []); result.allFills[u][v] = sum; return sum } }; let add = (v, fill, count)=>{ if(v > 0){ result.Vupper += v; fill && (result.fillVupper+=v, result.fillVupperCount += count); }else {//fill的话才会有 fill && (result.fillVlower-=v, result.fillVlowerCount += count); } }; let compute = async (u,v)=>{ let top = infos[0].allHeights[u][v]; //let btm = infos[2].allHeights[u][v] let fill; if(top == void 0){ if(prism){ let x = endPosition.x - boundSize_.x /2 + wDelta * (u + 0.5); let y = endPosition.y - boundSize_.y /2 + wDelta * (v + 0.5); if(!math.isPointInArea(prism.points, null, {x,y}) ){ return //出界 } }else { if(this.getModelZByRes(u,v,cW,cH,i,j) == void 0){ return //出界 } } fill = true; top = getFill(u,v); if(top == void 0){ //左侧边缘有缺口时会出现 if(waitFill.cancel || waitFill.count > 1000 && waitFill[waitFill.length-1][waitFill[waitFill.length-1].length-1].u - waitFill[0][0].u > 4){ //跨度超过4列 waitFill.cancel = true; //丢弃,不加入补洞, 降低耗时 }else { Common.pushToGroupAuto([{u,v}], waitFill, null, isBelongToGroup); //按邻近点分组 } waitFill.count ++; //当数量达到几万以上会在这个地方花很长时间,不过很少出现这种情况 return } } /* let diff = top-btm let useDiff if(btm != void 0 && diff > surfaceThick ){ useDiff = diff result.allDiffHeights.push(useDiff) }else{ useDiff = top //从顶部一直到水平 }*/ if(waitFill.length){//补 之前没有值的邻居重新获取 if(interrupt){ return computeFinish('12312') } if(waitFill.cancel){ //console.log('直接清空waitFill', waitFill.count, waitFill.map(list=>[list[0], list[list.length-1]])) waitFill.count = 0; waitFill.length = 0; waitFill.cancel = false; }else { ///* //Potree.settings.isTest && */ waitFill.count>1000 && console.log('waitFill.count', waitFill.count, 'groupLen', waitFill.length, waitFill[0][0], waitFill[0][waitFill[0].length-1]) waitFill.slice().forEach(group=>{ //每个组都看看是否跟它相邻 if(isBelongToGroup(group, {u,v})){ let index = waitFill.indexOf(group); waitFill.splice(index,1); waitFill.count -= group.length; group.forEach(e=>{ result.allFills[e.u] || (result.allFills[e.u] = []); result.allFills[e.u][e.v] = top; //该组全部都使用该体积 }); let sum = top * group.length; add(sum, fill, group.length); } }); } } add(top, fill, 1); }; let lastNotifyTime = Date.now(); //从左到右,从下到上一列列扫描 for (let u = 0; u < this.renderTarget.width; u++) { for (let v = 0; v < this.renderTarget.height; v++) { //数据是从图的下到上存储的 if(interrupt){ return computeFinish('1121') } compute(u,v); } await notify([[progressStart, progressEnd], [0.7, 1]] , ()=>{return u / this.renderTarget.width }, 'compute'); } if(waitFill.length){ console.log('waitFill怎么还有??', waitFill);//如果是模型的话是有剩的可能,当它的周围刚好是模型的hole时,就被断开在孤立的组里了,如"回"字型,中间的口是有model但无点云处,外面的口是无model处。 } result.Vupper *= sDelta; result.fillVlower *= sDelta; result.fillVupper *= sDelta; result.Vlower += result.fillVlower; results.push(result); deferred && deferred.notify(progressEnd); } } let Vupper = 0, Vlower = 0, fillVlower = 0, fillVupper = 0; results.forEach((c)=>{ Vupper += c.Vupper; Vlower += c.Vlower; fillVlower += c.fillVlower; fillVupper += c.fillVupper; }); computeDone({ highest,lowest,Vupper,Vlower, }, deferred); computeFinish('finish', true); console.log( {Vupper,Vlower, oldVupper: Vupper - fillVupper, oldVlower: Vlower - fillVlower, highest,lowest, horizonZ, wDelta}, results, this.viewport.resolution, {cW,cH}, 'cost:', Date.now()-timestamp, 'pxPerMetric', pxPerMetric, 'boundSize',boundSize );//firefox用时是chrome两倍,edge和chrome差不多 } getModelZByRes(u,v, cW,cH,wIndex,hIndex){ //传入renderTarget2的像素index //如果两个map的尺寸不同 let x = Math.round(wIndex/cW + u/this.renderTarget.width * this.renderTarget2.width); let y = Math.round(hIndex/cH + v/this.renderTarget.height * this.renderTarget2.height); let z0 = modelZs[x][y]; return z0 //return modelZs[u][v] } async render({i,j, index, cW,cH, reverseRow, infos, /* maxLevels, */ notifyArea, computeFinish}={}){ this.view.applyToCamera(this.camera); Potree.updatePointClouds(viewer.scene.pointclouds, this.camera, this.viewport.resolution ); let gl = viewer.renderer.getContext(); let screenshot = (name)=>{ //if(window.testScreen){ let dataUrl = Potree.Utils.renderTargetToDataUrl(this.renderTarget, this.renderTarget.width, this.renderTarget.height, viewer.renderer); Common.downloadFile(dataUrl, (name || 'screenshot') + '-' + timestamp +'.png'); //为什么图片上不是只有pickWindowSize区域有颜色?? //} }; let name = i+'-'+j+'-第'+index+'张'; console.log('开始渲染', name); let camera = this.camera; let viewport = this.viewport; return new Promise((resolve, reject)=>{ let done = async ()=>{ gl.clearColor(0, 0, 0, 0); viewer.renderer.clear(true, true, true); //viewer.pRenderer.renderOctree(pointcloud, nodes, this.camera, this.renderTarget ,{material:this.material}); viewer.pRenderer.render(viewer.scene.scenePointCloud, this.camera, this.renderTarget ,{material:this.material , /* transparent:!this.computingObject.isPrism */ }); if(interrupt){ return (reject(), computeFinish('re 23')) } Potree.settings.isTest && screenshot(name); gl.readPixels(0, 0, this.renderTarget.width , this.renderTarget.height, gl.RGBA, gl.UNSIGNED_BYTE, buffer1); //这句花费最多时间 pc:2-4, 即使只有1*1像素 notifyArea.push([progressEnd1,1]); let Vupper = 0, Vlower = 0; let upperHeights = [],lowerHeights = [], allHeights = []; for (let u = 0; u < this.renderTarget.width; u++) { let col = []; for (let v = 0; v < this.renderTarget.height; v++) { let _v = reverseRow ? this.renderTarget.height - v - 1: v; let offset = (u + _v * this.renderTarget.width); let rgb = buffer1.slice(4 * offset, 4 * offset+3), a = buffer1[4 * offset+3]; if(a == 0 /* rgb[0] == 0 && rgb[1] == 0 && rgb[2] == 0 */){ //无点 col.push(null); }else { let z = this.convertColorToHeight(rgb, zMin, zMax); let h; if(!this.computingObject.model){ h = z - horizonZ; }else { //和baseModel标准面之间的高度差 /* if(a == 128){//负数 h = -h } ///如果这个也要lowest 那需改shader ,shader不计算高度差 */ let z0 = this.getModelZByRes(u,v, cW,cH,i,j); h = z - z0; } z < lowest && (lowest = z); //上表面最低点 z > highest && (highest = z); //上表面最高点 if(h>0){ Vupper += h; upperHeights.push(h); // }else { Vlower += -h; lowerHeights.push(h); } col.push(h); } if(interrupt){ return (reject(), computeFinish('re 023')) } //notify(notifyArea, (u*this.renderTarget.height + v) /(this.renderTarget.width*this.renderTarget.height)) } allHeights.push(col); await notify(notifyArea, ()=>{return u / this.renderTarget.width}, 'readPixels get'); } Vupper*=sDelta; Vlower*=sDelta; infos.push({Vupper,Vlower,sDelta,upperHeights,lowerHeights,allHeights}); resolve(); }; //let finish , maxTime = 1e6 let progressEnd1 = math.linearClamp( this.camera.zoom, [1,1000], [0.8,0.2]); //放得越大,实际加载的点云相对越少,费时越少 let notifyArea_ = [...notifyArea, [0, progressEnd1]]; if(Potree.pointsLoading ){ //如果有要加载的node let curProgress = 0, beginTime = Date.now(), unloadBefore, tryPreLoadTime = Math.round(math.linearClamp(buffer1.length/this.camera.zoom/this.camera.zoom, [50,10000],[1000,15000])); //console.log('tryPreLoadTime',tryPreLoadTime) let decreaseLevel = ()=>{ //降点云level //暂时先都一起降1 let pointclouds = viewer.scene.pointclouds.filter(e=>e.visibleNodes.length); console.log('准备decreaseLevel, numVisiblePoints', Potree.numVisiblePoints, '最小numVisiblePoints占比', 1/pointclouds.length ); pointclouds.forEach(e=>{ let percent = e.numVisiblePoints / Potree.numVisiblePoints; console.log('numVisiblePoints占总比', e.dataset_id, percent ); if(percent < 1/pointclouds.length)return let old = e.maxLevel; let levels = e.visibleNodes.map(e=>e.getLevel()); let actMaxLevel = Math.max.apply(null, levels); //实际加载到的最高的node level e.maxLevel = actMaxLevel - 1; console.warn(e.dataset_id,'decreaseLevel,新maxLevel', actMaxLevel - 1, '原maxlevel', old ); //this.material.size = this.getPointsize(e.maxLevel) //this.material.size *= 2 }); }; let finish = (makeit)=>{ //viewer.removeEventListener('overPointBudget',decreaseLevel) makeit && done(); viewer.removeEventListener('update',update); viewer.removeEventListener('pointsLoaded',loaded); }; let update = ()=>{ Potree.updatePointClouds(viewer.scene.pointclouds, camera, viewport.resolution ); /* if( Potree.numVisiblePoints > maxPointBudge * 0.9){ decreaseLevel() */ notify(notifyArea_, ()=>{ /* console.log('unloadedGeometry',Potree.unloadedGeometry.length) return curProgress */ /* curProgress = THREE.Math.clamp(Potree.numVisiblePoints/predictPointCount,curProgress,1) //如果decreaseLevel了点会减少,也不管它,就停住吧,反正很快就加载好了 return curProgress */ if(!unloadBefore && Date.now() - beginTime < tryPreLoadTime){ curProgress = (Date.now() - beginTime) / tryPreLoadTime * 0.5; }else { if(!unloadBefore){ unloadBefore = Potree.unloadedGeometry.length; //console.log('unloadBefore', unloadBefore) } unloadBefore = Math.max(Potree.unloadedGeometry.length, unloadBefore); //unloadedGeometry 刚开始可能越涨越多 - - 所以进度条会卡住 let curProgress_ = 0.5 + 0.5 * (1 - Potree.unloadedGeometry.length / unloadBefore ); curProgress = Math.max(curProgress,curProgress_); } //console.log('curProgress ', curProgress, 'numVisiblePoints', Potree.numVisiblePoints, 'unloadedGeometry',Potree.unloadedGeometry.length) return curProgress }, 'loadpoints'); interrupt && (finish(false), reject(), computeFinish('正在loadPoints,unloadedGeometry个数'+Potree.unloadedGeometry.length )); //曾经遇到遇到bug 不发送pointsLoaded 一直update 一直有3个unloadedGeometry加载不了。。 是否为每个加载的node加个计时? }; viewer.addEventListener('update',update); let loaded = ()=>{ //点云加载完时(不一定准确) console.warn('加载完毕', ' numVisiblePoints', Potree.numVisiblePoints ); finish(true); }; viewer.addEventListener('pointsLoaded', loaded); /* let predictPointCount = Math.round(math.linearClamp(buffer.length, [1e5, maxWidth*maxWidth*4],[1e6, maxPointBudge ]))//最终要加载到的数目 */ /* let overTime = ()=>{ if(document.hidden){ return setTimeout(overTime, maxTime) } if(!finish)console.warn('超时, numVisiblePoints', Potree.numVisiblePoints) dealDone() } setTimeout(overTime, maxTime) */ }else { notify(notifyArea_, 1); done(); } }) } convertColorToHeight(color, zMin, zMax){ let percent = 0; let a = 1; for(let i=0;i<3;i++){ a = i == 2 ? a/255 : a / 256; percent += a * color[i]; } let height = (zMax - zMin) * percent; return height + zMin; } getPointsize(maxLevel){//尽量铺满 size单位是米,需要和点云缝隙同宽 return 0.01 /* adaptive:0.2 */ let s = /* this.material.spacing */ 100 / Math.pow(2, maxLevel - 1); //adaptive的话在shader中已经乘了spacing console.log('getPointsize', s, 'maxLevel',maxLevel); return s //会不会直接返回1就好? //不能太小。假如gl_pointsize计算结果是0.1,那么显示就是1px;当level将级后,放大2倍为0.2,也还是1px,看起来就稀疏了。所以要保证这个数在最大level时gl_pointsize计算结果差不多是1px.(虽然我会在渲染大尺寸面积时降画布尺寸,但不能保证刚刚好,且因点云个数不可控不会降尺寸太狠) } download(prisms){ if(this.prisms.length == 0){ return null } var visiPointclouds = viewer.scene.pointclouds.filter(e=> Potree.Utils.getObjVisiByReason(e, 'datasetSelection')); visiPointclouds.sort((a,b)=>{return a.dataset_id-b.dataset_id});//缓存需要固定排序好比较 let data = { transformation_matrix: visiPointclouds.map((cloud)=>{ let data = { id: cloud.dataset_id, matrix: new Matrix4().elements, //参照downloadNoCrop,给默认值,表示没有最外层裁剪 visiMatrixes: prisms.map(prism=>{return { matrix: prism.getTransformationMatrix(cloud).elements,//剪裁框 points: prism.points.map(p=>{return {x:p.x, y:p.y}}) }}), modelMatrix:(new Matrix4).copy(cloud.transformMatrix).transpose().elements }; return data }) , aabb: "b-12742000 -12742000 -12742000 12742000 12742000 12742000" //剪裁空间 }; return data } //体积多边形棱柱,z需要变换到-0.5 ~ 0.5 } /* VolumeComputer.prototype.setClipBoxes = function(){ let lastPrisms = [] return function(){ let prismPolygons = this.prisms.filter(e=>!e.isNew && e.visible) let prismList = [], prismPoints = [], maxPointsCount if(prismPolygons.length){ let pointsCount = prismPolygons.pointsCount = prismPolygons.reduce((w,c)=>{return w+c.points.length},0) prismList = new Float32Array(9 * prismPolygons.length); prismPoints = new Float32Array(2 * pointsCount); prismPolygons.maxPointsCount = 0//单个prism最大点个数 if(!this.entered)this.enter() let pointIndex = 0 for(let i=0;ie.material).concat([this.material]) materials.forEach(e=>{ e.uniforms.prismList.value = prismList e.uniforms.prismPoints.value = prismPoints e.shaderNeedsUpdate = e.shaderNeedsUpdate || doUpdate e.prisms = prismPolygons }) //修改obj材质 this.material.defines.num_prism = this.material.prisms.length//土方量数 this.material.defines.prismPointCountSum = this.material.prisms.pointsCount //点总个数 this.material.defines.prism_maxPointsCount = this.material.prisms.maxPointsCount //单个prism最大点个数 if(doUpdate){ this.material.needsUpdate = true viewer.dispatchEvent('content_changed') } lastPrisms = prismPolygons } }(); */ /* 渲染像素pxPerMetric越高,边缘越细腻、准确; 点size越小,越不容易遮盖住下方点云,造成体积偏大。但会不会有底部点云透出来? 该版本计算用到两张截图,第一张为整体的俯视图,包括挖方和填方;第二张仅包含填方。 缺陷:在补洞时,只利用第一张图的结果,所以整体的挖方会偏多,填方偏少(因为在挖方填方同时存在时,没有分别补挖方和填方,而是放在一起,而上方可能遮盖下方的) 尝试过第一张只绘制挖方部分,但是在遍历时就需要多遍历一次,测得的用时更多一些,结果却没变化 也尝试过补洞时挖方填方分别补各自的,但这样要考虑更多的东西,用时多了两倍以上。(因为目前规定无论范围内实际有没有点云都补上,非挖即填;如果分开的话,填方边缘也都补上肯定是错的,无法找到挖和填的界限,无法分清是只有挖还是填还是两者均有。如果要探测周围点,耗时很多) 如果数据集修改了,土方量需要手动重算。所以场景刷新后显示的体积有可能是错的。 之前记的,太麻烦了,延期处理: 1 一般情况:我需要记录每个土方量范围内存在的数据集的位置,如 {datasetId:"1745733728954093568", location:[113.59550104511611, 22.36677932324081, 0] ,orientation: 0.1} 如果刷新后土方量范围内存在的数据集发生改变,将重算。 数据集也可能显示或隐藏 2 其他特殊情况,如裁剪,难以知道裁剪了哪块,所以只能全部重算 ------------------ baseModel 基于模型的挖方填方 由于模型高度不一致,只能把相机放最高处朝下看,而无法像prism那样在horizonZ处再渲染一张,所以更加不能有多层点云干扰。 如模型在一个房间屋顶上方,那么填方会得到两个高度,屋顶和地板的高度,地板是从屋顶的点云缝隙而得。 如果模型放房间中间,并不能按理想的那样得到其上的挖方和其下的填方,因为相机朝下看时屋顶会遮住屋底,导致填方减少挖方增多。虽然能在bound底部朝上再拍一张,但若是有更复杂的情况依旧不能保证正确。 故需要设置好zMin zMax 如果模型有可能很大,但数据集不大,可以将bound只取交集 */ const texLoader$d = new TextureLoader(); const circleGeo = new CircleGeometry(1.45,100); const sphereGeo$1 = new SphereBufferGeometry(0.008,8,8); const magDisMin = 1;//相机离目标位置的距离的分界线,当离得远时要缩小fov以使看到的视野固定(望远镜效果) const magDisMax = 20; /* const radius_ = 0.2; //当相机离目标位置的距离>magDistance_时,希望看到的视野的半径 const maxFov = THREE.Math.radToDeg(Math.atan(radius_ / magDisMin )) * 2//提前计算出当相机离目标位置的距离= 2 ? ( window.screen.width * window.screen.height >= maxPX ? window.devicePixelRatio/1.2 : window.devicePixelRatio/1.5)*w$1 : w$1); //触屏或高分辨率的可能要放大些。但在手机上不能太大 //console.log('width2dPX', width2dPX) const orthoView = new ExtendView(); class Magnifier extends Object3D {//放大镜or望远镜 constructor (viewer) { super(); this.width = this.height = width2dPX/* * window.devicePixelRatio */; this.camera = new PerspectiveCamera(50, 1, 0.01, 10000); //fov aspect near far this.camera.up = new Vector3(0,0,1); this.viewport = new Viewport( null, this.camera, { left:0, bottom:0, width:1, height: 1, name:'magnifier' , cameraLayers:['magnifierContent'], pixelRatio:1 }); this.viewport.setResolution(this.width, this.height,0,0); { let density; let sizeType; let colorType; let opacityBefore = new Map(); let sizeBefore = new Map(); let visiMap = new Map(); this.viewport.beforeRender = ()=>{ viewer.scene.pointclouds.forEach(e=>{//因为更改pointDensity时会自动变opacity,所以这项最先获取 visiMap.set(e,e.visible); e.visible = Potree.Utils.getObjVisiByReason(e, 'datasetSelection'); //先将隐藏的点云显示 opacityBefore.set(e,e.temp.pointOpacity); sizeBefore.set(e,e.temp.pointSize); }); //使放大镜里的pointDensity是'magnifier' 最高质量。 density = Potree.settings.pointDensity; Potree.settings.pointDensity = 'magnifier'; viewer.scene.pointclouds.forEach(e=>{//因为全景模式的pointSizeType是fixed所以要还原下 sizeType = e.material.pointSizeType; e.material.pointSizeType = Potree.config.material.pointSizeType; //材质 colorType = e.material.activeAttributeName; e.material.activeAttributeName = 'rgba'; e.changePointOpacity(1); //e.changePointSize(Potree.config.material.realPointSize, true) }); }; this.viewport.afterRender = ()=>{ Potree.settings.pointDensity = density; viewer.scene.pointclouds.forEach(e=>{ e.visible = visiMap.get(e); e.material.pointSizeType = sizeType; e.material.activeAttributeName = colorType; e.changePointOpacity(opacityBefore.get(e)); //e.changePointSize(sizeBefore.get(e)) }); }; } this.renderTarget = new WebGLRenderTarget(this.width,this.height, { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat , /* type: THREE.FloatType, minFilter: THREE.NearestFilter, magFilter: THREE.NearestFilter, */ } ); this.rtEDL = new WebGLRenderTarget(this.width, this.height, { //好像没用到? 因为这里不绘制测量线 minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat, type: FloatType, depthTexture: new DepthTexture(undefined, undefined, UnsignedIntType) }); this.mesh = new Mesh(circleGeo, new MeshBasicMaterial({ side: DoubleSide , map: this.renderTarget.texture , transparent:true, depthTest: !1, //depthWrite: !1, })); this.overlayMesh = new Mesh(circleGeo, new MeshBasicMaterial({ side: DoubleSide , map:texLoader$d.load(Potree.resourcePath+'/textures/crosshair.png') , transparent:true, depthTest: !1, //depthWrite: !1, })); this.targetPoint = new Object3D; this.targetPoint.add(new Mesh(sphereGeo$1, new MeshBasicMaterial({ color:"#ff0000", transparent:true, opacity:0.7, }))); this.targetPoint.add(new Mesh(sphereGeo$1, new MeshBasicMaterial({ color:"#ff0000", transparent:true, opacity:0.3, depthTest:false //被遮挡层 }))); this.targetPoint.name = 'magnifierPointTarget'; viewer.scene.scene.add(this.targetPoint); Potree.Utils.setObjectLayers(this.targetPoint, 'magnifierContent' ); this.add(this.mesh); this.add(this.overlayMesh); this.position.set(-1000,-1000,-100000);//令它看不见 this.mesh.renderOrder = Potree.config.renderOrders.magnifier; this.overlayMesh.renderOrder = Potree.config.renderOrders.magnifier+1; this.aimPos; Potree.Utils.setObjectLayers(this, 'magnifier' ); //viewer.inputHandler.addInputListener(this) viewer.addEventListener('camera_changed',(e)=>{ // 平移、滚轮时更新 if(e.viewport == viewer.mainViewport) this.update(); //不过intersectPoint没更新 }); this.mesh.layers.set(Potree.config.renderLayers.magnifier); this.overlayMesh.layers.set(Potree.config.renderLayers.magnifier); //this.layers.set(Potree.config.renderLayers.magnifier);//这句在外层写没用 this.dontRender = false; viewer.addEventListener('global_drag', (e)=>{//拖拽时不渲染。主要是右键平移时渲染延迟了,会闪烁。 this.dontRender = true; }); viewer.addEventListener('global_drop', (e)=>{ this.dontRender = false; }); viewer.addEventListener('global_mouseup', (e)=>{//测量时拖拽场景再mouseup this.dontRender = false; }); var updateVisi = (e)=>{ if(e.hoverViewport == viewer.mainViewport){ Potree.Utils.updateVisible(this,"atViewport", true); this.update(e.intersect && e.intersect.location); }else { Potree.Utils.updateVisible(this,"atViewport", false); //小地图不显示 } }; viewer.addEventListener('global_mousemove', updateVisi); viewer.addEventListener('global_touchstart', updateVisi); viewer.addEventListener('updateMagnifier', updateVisi); /* viewer.addEventListener("beginSplitView",()=>{ this.updateVisible("splitView", false) }) viewer.addEventListener("finishSplitView",()=>{ this.updateVisible("splitView", true) }) */ this.addEventListener("setEnable",(e)=>{ Potree.Utils.updateVisible(this, "enable", e.value); //界面开关 /* if(Potree.settings.displayMode == 'showPanos') && e.value){ Potree.settings.pointDensity = 'magnifier' }else if() */ viewer.dispatchEvent('content_changed'); }); if(Potree.settings.isOfficial){ Potree.Utils.updateVisible(this, "enable", false); }else { Potree.Utils.updateVisible(this, "measure", false); viewer.addEventListener("measureMovePoint",()=>{//测量开始 //Potree.Utils.updateVisible(this, "measure", true) }); viewer.addEventListener("endMeasureMove",()=>{ Potree.Utils.updateVisible(this, "measure", false); viewer.dispatchEvent('content_changed'); }); } this.addEventListener('isVisible',(e)=>{ if(!this.visible) Potree.settings.pointDensity = Potree.settings.pointDensity; //恢复pointBudget }); viewer.scene.view.addEventListener('flyingDone',()=>{ if(!this.visible)return let pickWindowSize = 100; let intersect = viewer.inputHandler.getIntersect({viewport:viewer.mainViewport, usePointcloud:true, pickWindowSize} ); this.update(intersect && intersect.location); }); } //注意:在鼠标没有移动的时候,无法获取到最新的intersect, 放大镜内的内容可能是错误的。全景模式下更奇怪,原因未知 update(aimPos){//相机靠近 navvis的做法 var dontRender = this.dontRender || !(aimPos instanceof Vector3) || Potree.settings.displayMode == 'showPanos' && viewer.images360.flying; aimPos = aimPos instanceof Vector3 ? aimPos : this.aimPos; if(!aimPos || !this.visible)return var playerCamera = viewer.scene.getActiveCamera(); var playerPos = playerCamera.position;//viewer.scene.view.getPivot() var dis = playerPos.distanceTo(aimPos); var dirToCamera = new Vector3().subVectors(playerPos, aimPos ).normalize(); const fareast = 300; //相机位置 if(playerCamera.type == 'OrthographicCamera'){ var finalDisToAim = 2; }else { var finalDisToAim = dis>magDisMin ? dis > fareast ? magDisMax : (dis-magDisMin) / (fareast-magDisMin) * (magDisMax-magDisMin) + magDisMin : dis / 2; //dis>magDistance_ ? magDistance_ : dis / 2; } this.camera.fov = playerCamera.type == 'OrthographicCamera' ? 30 : playerCamera.fov / 2; this.camera.updateProjectionMatrix(); if(playerCamera.type == 'OrthographicCamera'){ orthoView.position.copy(aimPos).sub(viewer.mainViewport.view.direction.multiplyScalar(finalDisToAim)); orthoView.yaw = viewer.mainViewport.view.yaw; orthoView.pitch = viewer.mainViewport.view.pitch; orthoView.applyToCamera(this.camera); }else { this.camera.position.copy(aimPos).add(dirToCamera.multiplyScalar(finalDisToAim)); this.camera.lookAt(aimPos); } //自身位置 //let pos2d = viewer.inputHandler.pointer.clone(); //跟随鼠标 let pos2d_ = Potree.Utils.getPos2d(aimPos, viewer.mainViewport, viewer.renderArea); //更新目标点的实时二维位置 let pos2d = pos2d_.vector; let margin = width2dPX*1.1 / viewer.mainViewport.resolution2.y * 2; //确保到鼠标的间距占放大镜的比例不变(检查mobile、上下分屏后的变化) let screenPos = pos2d.clone().setY(pos2d.y + (pos2d.y >0 ? -margin : margin )); let newPos = new Vector3(screenPos.x,screenPos.y,0.8).unproject(playerCamera); //z:-1朝外 if(playerCamera.type != 'OrthographicCamera'){ let dir = newPos.clone().sub(playerPos).normalize().multiplyScalar(10);//这个数值要大于playerCamera.near this.position.copy(playerPos.clone().add(dir)); }else { viewer.navCubeViewer.splitScreen.setShiftTarget(viewer.mainViewport, viewer.bound.center); viewer.mainViewport.targetPlane.setFromNormalAndCoplanarPoint( viewer.mainViewport.view.direction.clone(), viewer.bound.center ); viewer.mainViewport.targetPlane.projectPoint(newPos, viewer.mainViewport.shiftTarget ); this.position.copy(viewer.mainViewport.shiftTarget.clone() ); //this.position.copy(playerPos.clone().add(dir)) } let s = finalDisToAim * this.camera.fov / 30; this.quaternion.copy(playerCamera.quaternion); this.targetPoint.position.copy(aimPos); this.targetPoint.scale.set(s,s,s); this.aimPos = aimPos; var scale = math.getScaleForConstantSize({// width2d : width2dPX, camera:viewer.scene.getActiveCamera(), position: this.getWorldPosition(new Vector3()), resolution: viewer.mainViewport.resolution2 }); this.scale.set(scale, scale, scale); if(!dontRender){ this.waitRender = true; } viewer.dispatchEvent('content_changed'); } /* update(aimPos){ //仅改fov的版本 aimPos = aimPos instanceof THREE.Vector3 ? aimPos : this.aimPos if(!aimPos || !this.visible)return //相机位置 var playerCamera = viewer.scene.getActiveCamera() var playerPos = playerCamera.position;//viewer.scene.view.getPivot() var dis = playerPos.distanceTo(aimPos); if(dismaxY ? -margin : margin )) let newPos = new THREE.Vector3(screenPos.x,screenPos.y,0.8).unproject(playerCamera); //z:-1朝外 let dir = newPos.clone().sub(playerPos).normalize().multiplyScalar(10);//这个数值要大于playerCamera.near this.position.copy(playerPos.clone().add(dir)) this.aimPos = aimPos this.targetPoint.position.copy(aimPos); var scale = math.getScaleForConstantSize({// width2d : width2dPX, camera:viewer.scene.getActiveCamera(), position: this.getWorldPosition(new THREE.Vector3()), resolution: viewer.mainViewport.resolution2 }) this.scale.set(scale, scale, scale); if(!this.dontRender){ this.waitRender = true } }//位置需要计算,不仅仅是点云,所以需要深度图 */ render(){ if(!this.visible || !this.waitRender && !viewer.needRender)return //viewer.needRender为true要渲染是因为可能是点云node加载完 viewer.render({ target : this.renderTarget, viewports : [this.viewport], camera : this.camera, magnifier : true, rtEDL: this.rtEDL /* width :this.renderTarget.width, height: this.renderTarget.height, */ }); this.waitRender = false; viewer.dispatchEvent('content_changed'); } } /* 如果遇到放大镜内点云很不正常,应该是深度图错误(可能全景和点云没匹配上)。 */ let texLoader$e = new TextureLoader(); let defaultOpacity = 0.6; let Buttons$2 = Potree.defines.Buttons; //鼠标指示小圆片 class Reticule extends Mesh{ constructor(viewer){ var defaultTex = texLoader$e.load(Potree.resourcePath+'/textures/whiteCircle.png'/* reticule-256x256.png' */); super(new PlaneBufferGeometry(0.11,0.11,1,1),new MeshBasicMaterial({ side: DoubleSide , map: defaultTex, transparent:true, depthTest: !1, opacity: defaultOpacity, //depthWrite: !1, })); this.name = 'reticule'; this.defaultTex = defaultTex; this.crosshairTex = texLoader$e.load(Potree.resourcePath+'/textures/reticule_cross_hair.png'); this.forbitTex = texLoader$e.load(Potree.resourcePath+'/textures/pic-forbid.png'); this.defaultTex.anisotropy = 4; this.crosshairTex.anisotropy = 4; this.forbitTex.anisotropy = 4; //this.layers.set(0/* RenderLayers.RETICULE */); this.renderOrder = Potree.config.renderOrders.reticule; this.layers.set(Potree.config.renderLayers.marker); this.direction = new Vector3; this.mouseLastMoveTime = Date.now(); this.hoverViewport; this.matrixMap = new Map; this.matrixAutoUpdate = false; this.hide(0); //viewer.inputHandler.addInputListener(this); Potree.settings.intersectWhenHover && viewer.addEventListener('global_mousemove',this.move.bind(this)); //viewer.addEventListener('global_click',this.move.bind(this)) viewer.addEventListener('global_mousedown',this.move.bind(this));//主要针对触屏 this.state = {}; let startCrossStyle = ()=>{ this.state.cross = true; this.judgeTex(); }; let endCrossStyle = ()=>{ this.state.cross = false; this.judgeTex(); }; viewer.addEventListener('measureMovePoint',startCrossStyle); viewer.addEventListener('endMeasureMove',endCrossStyle); viewer.addEventListener('startBuildEntity',startCrossStyle); viewer.addEventListener('endBuildEntity',endCrossStyle); viewer.addEventListener('start_inserting_tag',startCrossStyle); viewer.addEventListener('endTagMove',endCrossStyle); viewer.addEventListener('reticule_forbit',(e)=>{ if(this.state.forbit != e.v){ console.log('change forbit ',e.v); } this.state.forbit = e.v; this.judgeTex(); }); Potree.Utils.setObjectLayers(this, 'sceneObjects' ); } judgeTex(){ if(this.state.forbit){ this.material.map = this.forbitTex; }else if(this.state.cross){ this.material.map = this.crosshairTex; }else { this.material.map = this.defaultTex; } viewer.mapViewer && viewer.mapViewer.dispatchEvent({type:'content_changed'}); } move(e){ if(e.type == "global_mousemove" && (e.isTouch || e.buttons != Buttons$2.NONE) && !this.state.cross){ return//按下时不更新,除非拖拽测量 } this.mouseLastMoveTime = Date.now(); this.updatePosition(e.intersect, e.hoverViewport); } hide(duration = 500){ if(this.hidden)return this.hidden = !0; transitions.start(lerp.property(this.material , "opacity", 0, ()=>{//progress this.dispatchEvent({type:'update' }); //viewer.dispatchEvent('content_changed') }), duration,()=>{//done this.dispatchEvent({type:'update', visible:false}); }); this.dispatchEvent({type:'update', visible:false}); setTimeout(()=>{ },duration); } show(duration = 300){ if(!Potree.Utils.getObjVisiByReason(this, 'force'))return //console.log("show Reticule") this.hidden = !1; if(this.material.opacity <= 0){ transitions.start(lerp.property(this.material, "opacity", defaultOpacity, ()=>{//progress this.dispatchEvent({type:'update' }); //viewer.dispatchEvent('content_changed') }), duration,()=>{//done this.dispatchEvent({type:'update', visible:false}); }); this.dispatchEvent({type:'update', visible:true}); } } //鼠标静止一段时间它就会消失 updateVisible(){ Date.now() - this.mouseLastMoveTime > 1500 && !this.hidden && this.hide(); } updateScale(viewport){ let s, camera = viewport.camera; if(camera.type == "OrthographicCamera"){ var sizeInfo = this.state.cross ? {width2d:500} : {minSize : 100, maxSize : 400, nearBound : 100 , farBound : 700}; s = math.getScaleForConstantSize($.extend( sizeInfo , {position:this.position, camera, resolution:viewport.resolution/* 2 */} )); }else { let n = camera.position.distanceTo(this.position); s = 0.8 * Math.log2(n+1); //底数为2 ( Math.log()底数为e ) if(this.state.cross ){ //测量时更精细些 s /= viewer.images360.zoomLevel; } } this.scale.set(s, s, s); } updateAtViewports(viewport){//当多个viewports时更新。更新大小等 if(viewport.name == 'magnifier' )return if(this.orthoPos && this.hoverViewport && this.hoverViewport.name == 'mapViewport' && viewport != this.hoverViewport){ //若是在地图上更新,在其他viewport要隐藏。因为在地图上无法得知高度。 Potree.Utils.updateVisible(this, 'hoverMap', false); return; } Potree.Utils.updateVisible(this, 'hoverMap', true); if(viewport.name == 'mapViewport'){ Potree.Utils.setObjectLayers(this, "bothMapAndScene"); }else {//通常地图不显示reticule,只有在特殊编辑时才显示 Potree.Utils.setObjectLayers(this, 'sceneObjects'); } var matrix = this.matrixMap.get(viewport); if(!matrix){ this.updateScale(viewport); this.updateMatrix(); //this.updateMatrixWorld() this.matrixMap.set(viewport, this.matrix.clone()); }else { this.matrix.copy(matrix); //this.updateMatrixWorld() } } updatePosition(intersect, viewport ){ //在地图(当地图融合到viewer时)和场景里都显示且完全相同(大小可能不同) if (Potree.Utils.getObjVisiByReason(this, 'force')) {//没有被强制隐藏,如进入某个页面后强制不显示 if (!intersect /* || !intersect.point.normal */){ return //this.hide(); } var atMap = !intersect.location; let location = intersect.location || intersect.orthoIntersect.clone(); let normal; this.orthoPos = atMap; //地图上要瞬间变化 , 因为要使needRender为true很麻烦 this.show(atMap ? 0 : 300); if(atMap){ normal = new Vector3(0,0,1);//地图无normal location.setZ(0);//低于相机高度即可 this.direction = normal.clone(); }else { /* if(intersect.point){ if(intersect.pointcloud){ normal = new THREE.Vector3().fromArray(intersect.point.normal ).applyMatrix4( intersect.pointcloud.rotateMatrix ); }else{//mesh normal = new THREE.Vector3().copy(intersect.point.normal).applyQuaternion(intersect.object.quaternion) } }else{ normal = intersect.normal //when showPanos } */ normal = intersect.normal; if(normal){ let ratio = /* Potree.settings.useDepthTex ? 1 : */ 0.2; this.direction = this.direction.multiplyScalar(1-ratio); this.direction.add(normal.clone().multiplyScalar(ratio)); } //this.direction = normal.clone() //改为瞬间变化,否则刚hover上某个点时看起来不太对 } this.position.copy(location);/* .add(normal.clone().multiplyScalar(.01)); */ this.updateMatrix(); //lookAt之前要保证得到matrix this.lookAt(this.position.clone().add(this.direction)); this.hoverViewport = viewport; //记录下最近一次hover过的viewport this.updateScale(viewport); {//存储matrix,节省计算 this.updateMatrix(); //this.updateMatrixWorld() this.matrixMap.clear();//重新计算 this.matrixMap.set(viewport, this.matrix.clone()); //别处会updateMatrixWorld } this.dispatchEvent({type:'update'}); //为什么navvis在校准数据集时每个viewport里reticule的朝向都刚好垂直于屏幕,似乎限定在了一定范围内,还是在pick时就只pick范围内的点? } } //navvis在地图等地方看reticule是有厚度的 /* updateMatrixWorld(force){ console.log('updateMatrixWorld', force) super.updateMatrixWorld(force) } */ } var OBJLoader = ( function () { // o object_name | g group_name var object_pattern = /^[og]\s*(.+)?/; // mtllib file_reference var material_library_pattern = /^mtllib /; // usemtl material_name var material_use_pattern = /^usemtl /; // usemap map_name var map_use_pattern = /^usemap /; var vA = new Vector3(); var vB = new Vector3(); var vC = new Vector3(); var ab = new Vector3(); var cb = new Vector3(); function ParserState() { var state = { objects: [], object: {}, vertices: [], normals: [], colors: [], uvs: [], materials: {}, materialLibraries: [], startObject: function ( name, fromDeclaration ) { // If the current object (initial from reset) is not from a g/o declaration in the parsed // file. We need to use it for the first parsed g/o to keep things in sync. if ( this.object && this.object.fromDeclaration === false ) { this.object.name = name; this.object.fromDeclaration = ( fromDeclaration !== false ); return; } var previousMaterial = ( this.object && typeof this.object.currentMaterial === 'function' ? this.object.currentMaterial() : undefined ); if ( this.object && typeof this.object._finalize === 'function' ) { this.object._finalize( true ); } this.object = { name: name || '', fromDeclaration: ( fromDeclaration !== false ), geometry: { vertices: [], normals: [], colors: [], uvs: [], hasUVIndices: false }, materials: [], smooth: true, startMaterial: function ( name, libraries ) { var previous = this._finalize( false ); // New usemtl declaration overwrites an inherited material, except if faces were declared // after the material, then it must be preserved for proper MultiMaterial continuation. if ( previous && ( previous.inherited || previous.groupCount <= 0 ) ) { this.materials.splice( previous.index, 1 ); } var material = { index: this.materials.length, name: name || '', mtllib: ( Array.isArray( libraries ) && libraries.length > 0 ? libraries[ libraries.length - 1 ] : '' ), smooth: ( previous !== undefined ? previous.smooth : this.smooth ), groupStart: ( previous !== undefined ? previous.groupEnd : 0 ), groupEnd: - 1, groupCount: - 1, inherited: false, clone: function ( index ) { var cloned = { index: ( typeof index === 'number' ? index : this.index ), name: this.name, mtllib: this.mtllib, smooth: this.smooth, groupStart: 0, groupEnd: - 1, groupCount: - 1, inherited: false }; cloned.clone = this.clone.bind( cloned ); return cloned; } }; this.materials.push( material ); return material; }, currentMaterial: function () { if ( this.materials.length > 0 ) { return this.materials[ this.materials.length - 1 ]; } return undefined; }, _finalize: function ( end ) { var lastMultiMaterial = this.currentMaterial(); if ( lastMultiMaterial && lastMultiMaterial.groupEnd === - 1 ) { lastMultiMaterial.groupEnd = this.geometry.vertices.length / 3; lastMultiMaterial.groupCount = lastMultiMaterial.groupEnd - lastMultiMaterial.groupStart; lastMultiMaterial.inherited = false; } // Ignore objects tail materials if no face declarations followed them before a new o/g started. if ( end && this.materials.length > 1 ) { for ( var mi = this.materials.length - 1; mi >= 0; mi -- ) { if ( this.materials[ mi ].groupCount <= 0 ) { this.materials.splice( mi, 1 ); } } } // Guarantee at least one empty material, this makes the creation later more straight forward. if ( end && this.materials.length === 0 ) { this.materials.push( { name: '', smooth: this.smooth } ); } return lastMultiMaterial; } }; // Inherit previous objects material. // Spec tells us that a declared material must be set to all objects until a new material is declared. // If a usemtl declaration is encountered while this new object is being parsed, it will // overwrite the inherited material. Exception being that there was already face declarations // to the inherited material, then it will be preserved for proper MultiMaterial continuation. if ( previousMaterial && previousMaterial.name && typeof previousMaterial.clone === 'function' ) { var declared = previousMaterial.clone( 0 ); declared.inherited = true; this.object.materials.push( declared ); } this.objects.push( this.object ); }, finalize: function () { if ( this.object && typeof this.object._finalize === 'function' ) { this.object._finalize( true ); } }, parseVertexIndex: function ( value, len ) { var index = parseInt( value, 10 ); return ( index >= 0 ? index - 1 : index + len / 3 ) * 3; }, parseNormalIndex: function ( value, len ) { var index = parseInt( value, 10 ); return ( index >= 0 ? index - 1 : index + len / 3 ) * 3; }, parseUVIndex: function ( value, len ) { var index = parseInt( value, 10 ); return ( index >= 0 ? index - 1 : index + len / 2 ) * 2; }, addVertex: function ( a, b, c ) { var src = this.vertices; var dst = this.object.geometry.vertices; dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] ); dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] ); }, addVertexPoint: function ( a ) { var src = this.vertices; var dst = this.object.geometry.vertices; dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); }, addVertexLine: function ( a ) { var src = this.vertices; var dst = this.object.geometry.vertices; dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); }, addNormal: function ( a, b, c ) { var src = this.normals; var dst = this.object.geometry.normals; dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] ); dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] ); }, addFaceNormal: function ( a, b, c ) { var src = this.vertices; var dst = this.object.geometry.normals; vA.fromArray( src, a ); vB.fromArray( src, b ); vC.fromArray( src, c ); cb.subVectors( vC, vB ); ab.subVectors( vA, vB ); cb.cross( ab ); cb.normalize(); dst.push( cb.x, cb.y, cb.z ); dst.push( cb.x, cb.y, cb.z ); dst.push( cb.x, cb.y, cb.z ); }, addColor: function ( a, b, c ) { var src = this.colors; var dst = this.object.geometry.colors; if ( src[ a ] !== undefined ) dst.push( src[ a + 0 ], src[ a + 1 ], src[ a + 2 ] ); if ( src[ b ] !== undefined ) dst.push( src[ b + 0 ], src[ b + 1 ], src[ b + 2 ] ); if ( src[ c ] !== undefined ) dst.push( src[ c + 0 ], src[ c + 1 ], src[ c + 2 ] ); }, addUV: function ( a, b, c ) { var src = this.uvs; var dst = this.object.geometry.uvs; dst.push( src[ a + 0 ], src[ a + 1 ] ); dst.push( src[ b + 0 ], src[ b + 1 ] ); dst.push( src[ c + 0 ], src[ c + 1 ] ); }, addDefaultUV: function () { var dst = this.object.geometry.uvs; dst.push( 0, 0 ); dst.push( 0, 0 ); dst.push( 0, 0 ); }, addUVLine: function ( a ) { var src = this.uvs; var dst = this.object.geometry.uvs; dst.push( src[ a + 0 ], src[ a + 1 ] ); }, addFace: function ( a, b, c, ua, ub, uc, na, nb, nc ) { var vLen = this.vertices.length; var ia = this.parseVertexIndex( a, vLen ); var ib = this.parseVertexIndex( b, vLen ); var ic = this.parseVertexIndex( c, vLen ); this.addVertex( ia, ib, ic ); this.addColor( ia, ib, ic ); // normals if ( na !== undefined && na !== '' ) { var nLen = this.normals.length; ia = this.parseNormalIndex( na, nLen ); ib = this.parseNormalIndex( nb, nLen ); ic = this.parseNormalIndex( nc, nLen ); this.addNormal( ia, ib, ic ); } else { this.addFaceNormal( ia, ib, ic ); } // uvs if ( ua !== undefined && ua !== '' ) { var uvLen = this.uvs.length; ia = this.parseUVIndex( ua, uvLen ); ib = this.parseUVIndex( ub, uvLen ); ic = this.parseUVIndex( uc, uvLen ); this.addUV( ia, ib, ic ); this.object.geometry.hasUVIndices = true; } else { // add placeholder values (for inconsistent face definitions) this.addDefaultUV(); } }, addPointGeometry: function ( vertices ) { this.object.geometry.type = 'Points'; var vLen = this.vertices.length; for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) { var index = this.parseVertexIndex( vertices[ vi ], vLen ); this.addVertexPoint( index ); this.addColor( index ); } }, addLineGeometry: function ( vertices, uvs ) { this.object.geometry.type = 'Line'; var vLen = this.vertices.length; var uvLen = this.uvs.length; for ( var vi = 0, l = vertices.length; vi < l; vi ++ ) { this.addVertexLine( this.parseVertexIndex( vertices[ vi ], vLen ) ); } for ( var uvi = 0, l = uvs.length; uvi < l; uvi ++ ) { this.addUVLine( this.parseUVIndex( uvs[ uvi ], uvLen ) ); } } }; state.startObject( '', false ); return state; } // function OBJLoader( manager ) { Loader.call( this, manager ); this.materials = null; } OBJLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: OBJLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new FileLoader( this.manager ); loader.setPath( this.path ); loader.setRequestHeader( this.requestHeader ); loader.setWithCredentials( this.withCredentials ); loader.load( url, function ( text , total ) {// xzw add total try { onLoad( scope.parse( text ) , total ); } catch ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); } }, onProgress, onError ); }, setMaterials: function ( materials ) { this.materials = materials; return this; }, parse: function ( text ) { var state = new ParserState(); if ( text.indexOf( '\r\n' ) !== - 1 ) { // This is faster than String.split with regex that splits on both text = text.replace( /\r\n/g, '\n' ); } if ( text.indexOf( '\\\n' ) !== - 1 ) { // join lines separated by a line continuation character (\) text = text.replace( /\\\n/g, '' ); } var lines = text.split( '\n' ); var line = '', lineFirstChar = ''; var lineLength = 0; var result = []; // Faster to just trim left side of the line. Use if available. var trimLeft = ( typeof ''.trimLeft === 'function' ); for ( var i = 0, l = lines.length; i < l; i ++ ) { line = lines[ i ]; line = trimLeft ? line.trimLeft() : line.trim(); lineLength = line.length; if ( lineLength === 0 ) continue; lineFirstChar = line.charAt( 0 ); // @todo invoke passed in handler if any if ( lineFirstChar === '#' ) continue; if ( lineFirstChar === 'v' ) { var data = line.split( /\s+/ ); switch ( data[ 0 ] ) { case 'v': state.vertices.push( parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ), parseFloat( data[ 3 ] ) ); if ( data.length >= 7 ) { state.colors.push( parseFloat( data[ 4 ] ), parseFloat( data[ 5 ] ), parseFloat( data[ 6 ] ) ); } else { // if no colors are defined, add placeholders so color and vertex indices match state.colors.push( undefined, undefined, undefined ); } break; case 'vn': state.normals.push( parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ), parseFloat( data[ 3 ] ) ); break; case 'vt': state.uvs.push( parseFloat( data[ 1 ] ), parseFloat( data[ 2 ] ) ); break; } } else if ( lineFirstChar === 'f' ) { var lineData = line.substr( 1 ).trim(); var vertexData = lineData.split( /\s+/ ); var faceVertices = []; // Parse the face vertex data into an easy to work with format for ( var j = 0, jl = vertexData.length; j < jl; j ++ ) { var vertex = vertexData[ j ]; if ( vertex.length > 0 ) { var vertexParts = vertex.split( '/' ); faceVertices.push( vertexParts ); } } // Draw an edge between the first vertex and all subsequent vertices to form an n-gon var v1 = faceVertices[ 0 ]; for ( var j = 1, jl = faceVertices.length - 1; j < jl; j ++ ) { var v2 = faceVertices[ j ]; var v3 = faceVertices[ j + 1 ]; state.addFace( v1[ 0 ], v2[ 0 ], v3[ 0 ], v1[ 1 ], v2[ 1 ], v3[ 1 ], v1[ 2 ], v2[ 2 ], v3[ 2 ] ); } } else if ( lineFirstChar === 'l' ) { var lineParts = line.substring( 1 ).trim().split( ' ' ); var lineVertices = [], lineUVs = []; if ( line.indexOf( '/' ) === - 1 ) { lineVertices = lineParts; } else { for ( var li = 0, llen = lineParts.length; li < llen; li ++ ) { var parts = lineParts[ li ].split( '/' ); if ( parts[ 0 ] !== '' ) lineVertices.push( parts[ 0 ] ); if ( parts[ 1 ] !== '' ) lineUVs.push( parts[ 1 ] ); } } state.addLineGeometry( lineVertices, lineUVs ); } else if ( lineFirstChar === 'p' ) { var lineData = line.substr( 1 ).trim(); var pointData = lineData.split( ' ' ); state.addPointGeometry( pointData ); } else if ( ( result = object_pattern.exec( line ) ) !== null ) { // o object_name // or // g group_name // WORKAROUND: https://bugs.chromium.org/p/v8/issues/detail?id=2869 // var name = result[ 0 ].substr( 1 ).trim(); var name = ( ' ' + result[ 0 ].substr( 1 ).trim() ).substr( 1 ); state.startObject( name ); } else if ( material_use_pattern.test( line ) ) { // material state.object.startMaterial( line.substring( 7 ).trim(), state.materialLibraries ); } else if ( material_library_pattern.test( line ) ) { // mtl file state.materialLibraries.push( line.substring( 7 ).trim() ); } else if ( map_use_pattern.test( line ) ) { // the line is parsed but ignored since the loader assumes textures are defined MTL files // (according to https://www.okino.com/conv/imp_wave.htm, 'usemap' is the old-style Wavefront texture reference method) console.warn( 'THREE.OBJLoader: Rendering identifier "usemap" not supported. Textures must be defined in MTL files.' ); } else if ( lineFirstChar === 's' ) { result = line.split( ' ' ); // smooth shading // @todo Handle files that have varying smooth values for a set of faces inside one geometry, // but does not define a usemtl for each face set. // This should be detected and a dummy material created (later MultiMaterial and geometry groups). // This requires some care to not create extra material on each smooth value for "normal" obj files. // where explicit usemtl defines geometry groups. // Example asset: examples/models/obj/cerberus/Cerberus.obj /* * http://paulbourke.net/dataformats/obj/ * or * http://www.cs.utah.edu/~boulos/cs3505/obj_spec.pdf * * From chapter "Grouping" Syntax explanation "s group_number": * "group_number is the smoothing group number. To turn off smoothing groups, use a value of 0 or off. * Polygonal elements use group numbers to put elements in different smoothing groups. For free-form * surfaces, smoothing groups are either turned on or off; there is no difference between values greater * than 0." */ if ( result.length > 1 ) { var value = result[ 1 ].trim().toLowerCase(); state.object.smooth = ( value !== '0' && value !== 'off' ); } else { // ZBrush can produce "s" lines #11707 state.object.smooth = true; } var material = state.object.currentMaterial(); if ( material ) material.smooth = state.object.smooth; } else { // Handle null terminated files without exception if ( line === '\0' ) continue; console.warn( 'THREE.OBJLoader: Unexpected line: "' + line + '"' ); } } state.finalize(); var container = new Group(); container.materialLibraries = [].concat( state.materialLibraries ); var hasPrimitives = ! ( state.objects.length === 1 && state.objects[ 0 ].geometry.vertices.length === 0 ); if ( hasPrimitives === true ) { for ( var i = 0, l = state.objects.length; i < l; i ++ ) { var object = state.objects[ i ]; var geometry = object.geometry; var materials = object.materials; var isLine = ( geometry.type === 'Line' ); var isPoints = ( geometry.type === 'Points' ); var hasVertexColors = false; // Skip o/g line declarations that did not follow with any faces if ( geometry.vertices.length === 0 ) continue; var buffergeometry = new BufferGeometry(); buffergeometry.setAttribute( 'position', new Float32BufferAttribute( geometry.vertices, 3 ) ); if ( geometry.normals.length > 0 ) { buffergeometry.setAttribute( 'normal', new Float32BufferAttribute( geometry.normals, 3 ) ); } if ( geometry.colors.length > 0 ) { hasVertexColors = true; buffergeometry.setAttribute( 'color', new Float32BufferAttribute( geometry.colors, 3 ) ); } if ( geometry.hasUVIndices === true ) { buffergeometry.setAttribute( 'uv', new Float32BufferAttribute( geometry.uvs, 2 ) ); } // Create materials var createdMaterials = []; for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) { var sourceMaterial = materials[ mi ]; var materialHash = sourceMaterial.name + '_' + sourceMaterial.smooth + '_' + hasVertexColors; var material = state.materials[ materialHash ]; if ( this.materials !== null ) { material = this.materials.create( sourceMaterial.name ); // mtl etc. loaders probably can't create line materials correctly, copy properties to a line material. if ( isLine && material && ! ( material instanceof LineBasicMaterial ) ) { var materialLine = new LineBasicMaterial(); Material.prototype.copy.call( materialLine, material ); materialLine.color.copy( material.color ); material = materialLine; } else if ( isPoints && material && ! ( material instanceof PointsMaterial ) ) { var materialPoints = new PointsMaterial( { size: 10, sizeAttenuation: false } ); Material.prototype.copy.call( materialPoints, material ); materialPoints.color.copy( material.color ); materialPoints.map = material.map; material = materialPoints; } } if ( material === undefined ) { if ( isLine ) { material = new LineBasicMaterial(); } else if ( isPoints ) { material = new PointsMaterial( { size: 1, sizeAttenuation: false } ); } else { material = new MeshPhongMaterial(); } material.name = sourceMaterial.name; material.flatShading = sourceMaterial.smooth ? false : true; material.vertexColors = hasVertexColors; state.materials[ materialHash ] = material; } createdMaterials.push( material ); } // Create mesh var mesh; if ( createdMaterials.length > 1 ) { for ( var mi = 0, miLen = materials.length; mi < miLen; mi ++ ) { var sourceMaterial = materials[ mi ]; buffergeometry.addGroup( sourceMaterial.groupStart, sourceMaterial.groupCount, mi ); } if ( isLine ) { mesh = new LineSegments( buffergeometry, createdMaterials ); } else if ( isPoints ) { mesh = new Points( buffergeometry, createdMaterials ); } else { mesh = new Mesh( buffergeometry, createdMaterials ); } } else { if ( isLine ) { mesh = new LineSegments( buffergeometry, createdMaterials[ 0 ] ); } else if ( isPoints ) { mesh = new Points( buffergeometry, createdMaterials[ 0 ] ); } else { mesh = new Mesh( buffergeometry, createdMaterials[ 0 ] ); } } mesh.name = object.name; container.add( mesh ); } } else { // if there is only the default parser state object with no geometry data, interpret data as point cloud if ( state.vertices.length > 0 ) { var material = new PointsMaterial( { size: 1, sizeAttenuation: false } ); var buffergeometry = new BufferGeometry(); buffergeometry.setAttribute( 'position', new Float32BufferAttribute( state.vertices, 3 ) ); if ( state.colors.length > 0 && state.colors[ 0 ] !== undefined ) { buffergeometry.setAttribute( 'color', new Float32BufferAttribute( state.colors, 3 ) ); material.vertexColors = true; } var points = new Points( buffergeometry, material ); container.add( points ); } } return container; } } ); return OBJLoader; } )(); /** * Loads a Wavefront .mtl file specifying materials */ var MTLLoader = function ( manager ) { Loader.call( this, manager ); }; MTLLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: MTLLoader, /** * Loads and parses a MTL asset from a URL. * * @param {String} url - URL to the MTL file. * @param {Function} [onLoad] - Callback invoked with the loaded object. * @param {Function} [onProgress] - Callback for download progress. * @param {Function} [onError] - Callback for download errors. * * @see setPath setResourcePath * * @note In order for relative texture references to resolve correctly * you must call setResourcePath() explicitly prior to load. */ load: function ( url, onLoad, onProgress, onError ) { var scope = this; var path = ( this.path === '' ) ? LoaderUtils.extractUrlBase( url ) : this.path; var loader = new FileLoader( this.manager ); loader.setPath( this.path ); loader.setRequestHeader( this.requestHeader ); loader.setWithCredentials( this.withCredentials ); loader.load( url, function ( text ) { try { onLoad( scope.parse( text, path ) ); } catch ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); } }, onProgress, onError ); }, setMaterialOptions: function ( value ) { this.materialOptions = value; return this; }, /** * Parses a MTL file. * * @param {String} text - Content of MTL file * @return {MTLLoader.MaterialCreator} * * @see setPath setResourcePath * * @note In order for relative texture references to resolve correctly * you must call setResourcePath() explicitly prior to parse. */ parse: function ( text, path ) { var lines = text.split( '\n' ); var info = {}; var delimiter_pattern = /\s+/; var materialsInfo = {}; for ( var i = 0; i < lines.length; i ++ ) { var line = lines[ i ]; line = line.trim(); if ( line.length === 0 || line.charAt( 0 ) === '#' ) { // Blank line or comment ignore continue; } var pos = line.indexOf( ' ' ); var key = ( pos >= 0 ) ? line.substring( 0, pos ) : line; key = key.toLowerCase(); var value = ( pos >= 0 ) ? line.substring( pos + 1 ) : ''; value = value.trim(); if ( key === 'newmtl' ) { // New material info = { name: value }; materialsInfo[ value ] = info; } else { if ( key === 'ka' || key === 'kd' || key === 'ks' || key === 'ke' ) { var ss = value.split( delimiter_pattern, 3 ); info[ key ] = [ parseFloat( ss[ 0 ] ), parseFloat( ss[ 1 ] ), parseFloat( ss[ 2 ] ) ]; } else { info[ key ] = value; } } } var materialCreator = new MTLLoader.MaterialCreator( this.resourcePath || path, this.materialOptions ); materialCreator.setCrossOrigin( this.crossOrigin ); materialCreator.setManager( this.manager ); materialCreator.setMaterials( materialsInfo ); return materialCreator; } } ); /** * Create a new MTLLoader.MaterialCreator * @param baseUrl - Url relative to which textures are loaded * @param options - Set of options on how to construct the materials * side: Which side to apply the material * FrontSide (default), THREE.BackSide, THREE.DoubleSide * wrap: What type of wrapping to apply for textures * RepeatWrapping (default), THREE.ClampToEdgeWrapping, THREE.MirroredRepeatWrapping * normalizeRGB: RGBs need to be normalized to 0-1 from 0-255 * Default: false, assumed to be already normalized * ignoreZeroRGBs: Ignore values of RGBs (Ka,Kd,Ks) that are all 0's * Default: false * @constructor */ MTLLoader.MaterialCreator = function ( baseUrl, options ) { this.baseUrl = baseUrl || ''; this.options = options; this.materialsInfo = {}; this.materials = {}; this.materialsArray = []; this.nameLookup = {}; this.side = ( this.options && this.options.side ) ? this.options.side : FrontSide; this.wrap = ( this.options && this.options.wrap ) ? this.options.wrap : RepeatWrapping; }; MTLLoader.MaterialCreator.prototype = { constructor: MTLLoader.MaterialCreator, crossOrigin: 'anonymous', setCrossOrigin: function ( value ) { this.crossOrigin = value; return this; }, setManager: function ( value ) { this.manager = value; }, setMaterials: function ( materialsInfo ) { this.materialsInfo = this.convert( materialsInfo ); this.materials = {}; this.materialsArray = []; this.nameLookup = {}; }, convert: function ( materialsInfo ) { if ( ! this.options ) return materialsInfo; var converted = {}; for ( var mn in materialsInfo ) { // Convert materials info into normalized form based on options var mat = materialsInfo[ mn ]; var covmat = {}; converted[ mn ] = covmat; for ( var prop in mat ) { var save = true; var value = mat[ prop ]; var lprop = prop.toLowerCase(); switch ( lprop ) { case 'kd': case 'ka': case 'ks': // Diffuse color (color under white light) using RGB values if ( this.options && this.options.normalizeRGB ) { value = [ value[ 0 ] / 255, value[ 1 ] / 255, value[ 2 ] / 255 ]; } if ( this.options && this.options.ignoreZeroRGBs ) { if ( value[ 0 ] === 0 && value[ 1 ] === 0 && value[ 2 ] === 0 ) { // ignore save = false; } } break; default: break; } if ( save ) { covmat[ lprop ] = value; } } } return converted; }, preload: function () { for ( var mn in this.materialsInfo ) { this.create( mn ); } }, getIndex: function ( materialName ) { return this.nameLookup[ materialName ]; }, getAsArray: function () { var index = 0; for ( var mn in this.materialsInfo ) { this.materialsArray[ index ] = this.create( mn ); this.nameLookup[ mn ] = index; index ++; } return this.materialsArray; }, create: function ( materialName ) { if ( this.materials[ materialName ] === undefined ) { this.createMaterial_( materialName ); } return this.materials[ materialName ]; }, createMaterial_: function ( materialName ) { // Create material var scope = this; var mat = this.materialsInfo[ materialName ]; var params = { name: materialName, side: this.side }; function resolveURL( baseUrl, url ) { if ( typeof url !== 'string' || url === '' ) return ''; // Absolute URL if ( /^https?:\/\//i.test( url ) ) return url; return baseUrl + url; } function setMapForType( mapType, value ) { if ( params[ mapType ] ) return; // Keep the first encountered texture var texParams = scope.getTextureParams( value, params ); var map = scope.loadTexture( resolveURL( scope.baseUrl, texParams.url ) ); map.repeat.copy( texParams.scale ); map.offset.copy( texParams.offset ); map.wrapS = scope.wrap; map.wrapT = scope.wrap; params[ mapType ] = map; } for ( var prop in mat ) { var value = mat[ prop ]; var n; if ( value === '' ) continue; switch ( prop.toLowerCase() ) { // Ns is material specular exponent case 'kd': // Diffuse color (color under white light) using RGB values params.color = new Color().fromArray( value ); break; case 'ks': // Specular color (color when light is reflected from shiny surface) using RGB values //params.specular = new Color().fromArray( value ); //console.log('specular',value) break; case 'ke': // Emissive using RGB values params.emissive = new Color().fromArray( value ); break; case 'map_kd': // Diffuse texture map setMapForType( 'map', value ); break; case 'map_ks': // Specular map setMapForType( 'specularMap', value ); break; case 'map_ke': // Emissive map setMapForType( 'emissiveMap', value ); break; case 'norm': setMapForType( 'normalMap', value ); break; case 'map_bump': case 'bump': // Bump texture map setMapForType( 'bumpMap', value ); break; case 'map_d': // Alpha map setMapForType( 'alphaMap', value ); params.transparent = true; break; case 'ns': // The specular exponent (defines the focus of the specular highlight) // A high exponent results in a tight, concentrated highlight. Ns values normally range from 0 to 1000. //params.shininess = parseFloat( value ); //console.log('shininess',value) break; case 'd': n = parseFloat( value ); if ( n < 1 ) { params.opacity = n; params.transparent = true; } break; case 'tr': n = parseFloat( value ); if ( this.options && this.options.invertTrProperty ) n = 1 - n; if ( n > 0 ) { params.opacity = 1 - n; params.transparent = true; } break; default: break; } } this.materials[ materialName ] = new MeshStandardMaterial( params );//MeshPhongMaterial( params ); return this.materials[ materialName ]; }, getTextureParams: function ( value, matParams ) { var texParams = { scale: new Vector2( 1, 1 ), offset: new Vector2( 0, 0 ) }; var items = value.split( /\s+/ ); var pos; pos = items.indexOf( '-bm' ); if ( pos >= 0 ) { matParams.bumpScale = parseFloat( items[ pos + 1 ] ); items.splice( pos, 2 ); } pos = items.indexOf( '-s' ); if ( pos >= 0 ) { texParams.scale.set( parseFloat( items[ pos + 1 ] ), parseFloat( items[ pos + 2 ] ) ); items.splice( pos, 4 ); // we expect 3 parameters here! } pos = items.indexOf( '-o' ); if ( pos >= 0 ) { texParams.offset.set( parseFloat( items[ pos + 1 ] ), parseFloat( items[ pos + 2 ] ) ); items.splice( pos, 4 ); // we expect 3 parameters here! } texParams.url = items.join( ' ' ).trim(); return texParams; }, loadTexture: function ( url, mapping, onLoad, onProgress, onError ) { var texture; var manager = ( this.manager !== undefined ) ? this.manager : DefaultLoadingManager; var loader = manager.getHandler( url ); if ( loader === null ) { loader = new TextureLoader( manager ); } if ( loader.setCrossOrigin ) loader.setCrossOrigin( this.crossOrigin ); texture = loader.load( url, onLoad, onProgress, onError ); if ( mapping !== undefined ) texture.mapping = mapping; return texture; } }; //2022.11.11 copyfrom : https://unpkg.com/three@0.146.0/examples/jsm/loaders/DRACOLoader.js const _taskCache = new WeakMap(); class DRACOLoader extends Loader { constructor( manager ) { super( manager ); this.decoderPath = ''; this.decoderConfig = {}; this.decoderBinary = null; this.decoderPending = null; this.workerLimit = 4; this.workerPool = []; this.workerNextTaskID = 1; this.workerSourceURL = ''; this.defaultAttributeIDs = { position: 'POSITION', normal: 'NORMAL', color: 'COLOR', uv: 'TEX_COORD' }; this.defaultAttributeTypes = { position: 'Float32Array', normal: 'Float32Array', color: 'Float32Array', uv: 'Float32Array' }; } setDecoderPath( path ) { this.decoderPath = path; return this; } setDecoderConfig( config ) { this.decoderConfig = config; 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 ); loader.setWithCredentials( this.withCredentials ); loader.load( url, ( buffer ) => { this.decodeDracoFile( buffer, onLoad ).catch( onError ); }, onProgress, onError ); } decodeDracoFile( buffer, callback, attributeIDs, attributeTypes ) { const taskConfig = { attributeIDs: attributeIDs || this.defaultAttributeIDs, attributeTypes: attributeTypes || this.defaultAttributeTypes, useUniqueIDs: !! attributeIDs }; return this.decodeGeometry( buffer, taskConfig ).then( callback ); } decodeGeometry( buffer, taskConfig ) { const taskKey = JSON.stringify( taskConfig ); // 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 ); if ( cachedTask.key === taskKey ) { return cachedTask.promise; } else if ( buffer.byteLength === 0 ) { // Technically, it would be possible to wait for the previous task to complete, // transfer the buffer back, and decode again with the second configuration. That // is complex, and I don't know of any reason to decode a Draco buffer twice in // different ways, so this is left unimplemented. throw new Error( 'THREE.DRACOLoader: Unable to re-decode a buffer with different ' + 'settings. Buffer has already been transferred.' ); } } // let worker; const taskID = this.workerNextTaskID ++; const taskCost = buffer.byteLength; // Obtain a worker and assign a task, and construct a geometry instance // when the task completes. const geometryPending = this._getWorker( taskID, taskCost ) .then( ( _worker ) => { worker = _worker; return new Promise( ( resolve, reject ) => { worker._callbacks[ taskID ] = { resolve, reject }; worker.postMessage( { type: 'decode', id: taskID, taskConfig, buffer }, [ buffer ] ); // this.debug(); } ); } ) .then( ( message ) => this._createGeometry( message.geometry ) ); // Remove task from the task list. // Note: replaced '.finally()' with '.catch().then()' block - iOS 11 support (#19416) geometryPending .catch( () => true ) .then( () => { if ( worker && taskID ) { this._releaseTask( worker, taskID ); // this.debug(); } } ); // Cache the task result. _taskCache.set( buffer, { key: taskKey, promise: geometryPending } ); return geometryPending; } _createGeometry( geometryData ) { const geometry = new BufferGeometry(); if ( geometryData.index ) { geometry.setIndex( new BufferAttribute( geometryData.index.array, 1 ) ); } for ( let i = 0; i < geometryData.attributes.length; i ++ ) { const attribute = geometryData.attributes[ i ]; const name = attribute.name; const array = attribute.array; const itemSize = attribute.itemSize; geometry.setAttribute( name, new BufferAttribute( array, itemSize ) ); } return geometry; } _loadLibrary( url, responseType ) { const loader = new FileLoader( this.manager ); loader.setPath( this.decoderPath ); loader.setResponseType( responseType ); loader.setWithCredentials( this.withCredentials ); return new Promise( ( resolve, reject ) => { loader.load( url, resolve, undefined, reject ); } ); } preload() { this._initDecoder(); return this; } _initDecoder() { if ( this.decoderPending ) return this.decoderPending; const useJS = typeof WebAssembly !== 'object' || this.decoderConfig.type === 'js'; const librariesPending = []; if ( useJS ) { librariesPending.push( this._loadLibrary( 'draco_decoder.js', 'text' ) ); } else { librariesPending.push( this._loadLibrary( 'draco_wasm_wrapper.js', 'text' ) ); librariesPending.push( this._loadLibrary( 'draco_decoder.wasm', 'arraybuffer' ) ); } this.decoderPending = Promise.all( librariesPending ) .then( ( libraries ) => { const jsContent = libraries[ 0 ]; if ( ! useJS ) { this.decoderConfig.wasmBinary = libraries[ 1 ]; } const fn = DRACOWorker.toString(); const body = [ '/* draco decoder */', jsContent, '', '/* worker */', fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) ) ].join( '\n' ); this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) ); } ); return this.decoderPending; } _getWorker( taskID, taskCost ) { return this._initDecoder().then( () => { if ( this.workerPool.length < this.workerLimit ) { const worker = new Worker( this.workerSourceURL ); worker._callbacks = {}; worker._taskCosts = {}; worker._taskLoad = 0; worker.postMessage( { type: 'init', decoderConfig: this.decoderConfig } ); worker.onmessage = function ( e ) { const message = e.data; switch ( message.type ) { case 'decode': worker._callbacks[ message.id ].resolve( message ); break; case 'error': worker._callbacks[ message.id ].reject( message ); break; default: console.error( 'THREE.DRACOLoader: 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._taskCosts[ taskID ] = taskCost; worker._taskLoad += taskCost; return worker; } ); } _releaseTask( worker, taskID ) { worker._taskLoad -= worker._taskCosts[ taskID ]; delete worker._callbacks[ taskID ]; delete worker._taskCosts[ taskID ]; } debug() { console.log( 'Task load: ', this.workerPool.map( ( worker ) => worker._taskLoad ) ); } dispose() { for ( let i = 0; i < this.workerPool.length; ++ i ) { this.workerPool[ i ].terminate(); } this.workerPool.length = 0; return this; } } /* WEB WORKER */ function DRACOWorker() { let decoderConfig; let decoderPending; onmessage = function ( e ) { const message = e.data; switch ( message.type ) { case 'init': decoderConfig = message.decoderConfig; decoderPending = new Promise( function ( resolve/*, reject*/ ) { decoderConfig.onModuleLoaded = function ( draco ) { // Module is Promise-like. Wrap before resolving to avoid loop. resolve( { draco: draco } ); }; DracoDecoderModule( decoderConfig ); // eslint-disable-line no-undef } ); break; case 'decode': const buffer = message.buffer; const taskConfig = message.taskConfig; decoderPending.then( ( module ) => { const draco = module.draco; const decoder = new draco.Decoder(); const decoderBuffer = new draco.DecoderBuffer(); decoderBuffer.Init( new Int8Array( buffer ), buffer.byteLength ); try { const geometry = decodeGeometry( draco, decoder, decoderBuffer, taskConfig ); const buffers = geometry.attributes.map( ( attr ) => attr.array.buffer ); if ( geometry.index ) buffers.push( geometry.index.array.buffer ); self.postMessage( { type: 'decode', id: message.id, geometry }, buffers ); } catch ( error ) { console.error( error ); self.postMessage( { type: 'error', id: message.id, error: error.message } ); } finally { draco.destroy( decoderBuffer ); draco.destroy( decoder ); } } ); break; } }; function decodeGeometry( draco, decoder, decoderBuffer, taskConfig ) { const attributeIDs = taskConfig.attributeIDs; const attributeTypes = taskConfig.attributeTypes; let dracoGeometry; let decodingStatus; const geometryType = decoder.GetEncodedGeometryType( decoderBuffer ); if ( geometryType === draco.TRIANGULAR_MESH ) { dracoGeometry = new draco.Mesh(); decodingStatus = decoder.DecodeBufferToMesh( decoderBuffer, dracoGeometry ); } else if ( geometryType === draco.POINT_CLOUD ) { dracoGeometry = new draco.PointCloud(); decodingStatus = decoder.DecodeBufferToPointCloud( decoderBuffer, dracoGeometry ); } else { throw new Error( 'THREE.DRACOLoader: Unexpected geometry type.' ); } if ( ! decodingStatus.ok() || dracoGeometry.ptr === 0 ) { throw new Error( 'THREE.DRACOLoader: Decoding failed: ' + decodingStatus.error_msg() ); } const geometry = { index: null, attributes: [] }; // Gather all vertex attributes. for ( const attributeName in attributeIDs ) { const attributeType = self[ attributeTypes[ attributeName ] ]; let attribute; let attributeID; // A Draco file may be created with default vertex attributes, whose attribute IDs // are mapped 1:1 from their semantic name (POSITION, NORMAL, ...). Alternatively, // a Draco file may contain a custom set of attributes, identified by known unique // IDs. glTF files always do the latter, and `.drc` files typically do the former. if ( taskConfig.useUniqueIDs ) { attributeID = attributeIDs[ attributeName ]; attribute = decoder.GetAttributeByUniqueId( dracoGeometry, attributeID ); } else { attributeID = decoder.GetAttributeId( dracoGeometry, draco[ attributeIDs[ attributeName ] ] ); if ( attributeID === - 1 ) continue; attribute = decoder.GetAttribute( dracoGeometry, attributeID ); } geometry.attributes.push( decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) ); } // Add index. if ( geometryType === draco.TRIANGULAR_MESH ) { geometry.index = decodeIndex( draco, decoder, dracoGeometry ); } draco.destroy( dracoGeometry ); return geometry; } function decodeIndex( draco, decoder, dracoGeometry ) { const numFaces = dracoGeometry.num_faces(); const numIndices = numFaces * 3; const byteLength = numIndices * 4; const ptr = draco._malloc( byteLength ); decoder.GetTrianglesUInt32Array( dracoGeometry, byteLength, ptr ); const index = new Uint32Array( draco.HEAPF32.buffer, ptr, numIndices ).slice(); draco._free( ptr ); return { array: index, itemSize: 1 }; } function decodeAttribute( draco, decoder, dracoGeometry, attributeName, attributeType, attribute ) { const numComponents = attribute.num_components(); const numPoints = dracoGeometry.num_points(); const numValues = numPoints * numComponents; const byteLength = numValues * attributeType.BYTES_PER_ELEMENT; const dataType = getDracoDataType( draco, attributeType ); const ptr = draco._malloc( byteLength ); decoder.GetAttributeDataArrayForAllPoints( dracoGeometry, attribute, dataType, byteLength, ptr ); const array = new attributeType( draco.HEAPF32.buffer, ptr, numValues ).slice(); draco._free( ptr ); return { name: attributeName, array: array, itemSize: numComponents }; } function getDracoDataType( draco, attributeType ) { switch ( attributeType ) { case Float32Array: return draco.DT_FLOAT32; case Int8Array: return draco.DT_INT8; case Int16Array: return draco.DT_INT16; case Int32Array: return draco.DT_INT32; case Uint8Array: return draco.DT_UINT8; case Uint16Array: return draco.DT_UINT16; case Uint32Array: return draco.DT_UINT32; } } } /** * @author Deepkolos / https://github.com/deepkolos */ //用于KTX2Loader class WorkerPool$1 { constructor( pool = 4 ) { this.pool = pool; this.queue = []; this.workers = []; this.workersResolve = []; this.workerStatus = 0; } _initWorker( workerId ) { if ( ! this.workers[ workerId ] ) { const worker = this.workerCreator(); worker.addEventListener( 'message', this._onMessage.bind( this, workerId ) ); this.workers[ workerId ] = worker; } } _getIdleWorker() { for ( let i = 0; i < this.pool; i ++ ) if ( ! ( this.workerStatus & ( 1 << i ) ) ) return i; return - 1; } _onMessage( workerId, msg ) { const resolve = this.workersResolve[ workerId ]; resolve && resolve( msg ); if ( this.queue.length ) { const { resolve, msg, transfer } = this.queue.shift(); this.workersResolve[ workerId ] = resolve; this.workers[ workerId ].postMessage( msg, transfer ); } else { this.workerStatus ^= 1 << workerId; } } setWorkerCreator( workerCreator ) { this.workerCreator = workerCreator; } setWorkerLimit( pool ) { this.pool = pool; } postMessage( msg, transfer ) { return new Promise( ( resolve ) => { const workerId = this._getIdleWorker(); if ( workerId !== - 1 ) { this._initWorker( workerId ); this.workerStatus |= 1 << workerId; this.workersResolve[ workerId ] = resolve; this.workers[ workerId ].postMessage( msg, transfer ); } else { this.queue.push( { resolve, msg, transfer } ); } } ); } dispose() { this.workers.forEach( ( worker ) => worker.terminate() ); this.workersResolve.length = 0; this.workers.length = 0; this.queue.length = 0; this.workerStatus = 0; } } const t=0,e$1=1,n$1=2,i$1=3,s=0,a$1=0,r=2,o$1=0,l$1=1,f=160,U$1=161,c$2=162,h$2=163,_$1=0,p=1,g=0,y=1,x$2=2,u$2=3,b$1=4,d=5,m=6,w$2=7,D=8,B=9,L=10,A=11,k=12,v=13,S$1=14,I=15,O=16,T$1=17,V=18,E=0,F=1,P=2,C=3,z=4,M$1=5,W=6,N=7,H=8,K=9,X=10,j=11,R=0,Y=1,q=2,G=13,J=14,Q=15,Z=128,$$1=64,tt=32,et=16,nt=0,it=1,st=2,at=3,rt=4,ot=5,lt=6,ft=7,Ut=8,ct=9,ht=10,_t=13,pt=14,gt=15,yt=16,xt=17,ut=20,bt=21,dt=22,mt=23,wt=24,Dt=27,Bt=28,Lt=29,At=30,kt=31,vt=34,St=35,It=36,Ot=37,Tt=38,Vt=41,Et=42,Ft=43,Pt=44,Ct=45,zt=48,Mt=49,Wt=50,Nt=58,Ht=59,Kt=62,Xt=63,jt=64,Rt=65,Yt=68,qt=69,Gt=70,Jt=71,Qt=74,Zt=75,$t=76,te=77,ee=78,ne=81,ie=82,se=83,ae=84,re=85,oe=88,le=89,fe=90,Ue=91,ce=92,he=95,_e=96,pe=97,ge=98,ye=99,xe=100,ue=101,be=102,de=103,me=104,we=105,De=106,Be=107,Le=108,Ae=109,ke=110,ve=111,Se=112,Ie=113,Oe=114,Te=115,Ve=116,Ee=117,Fe=118,Pe=119,Ce=120,ze=121,Me=122,We=123,Ne=124,He=125,Ke=126,Xe=127,je=128,Re=129,Ye=130,qe=131,Ge=132,Je=133,Qe=134,Ze=135,$e=136,tn=137,en=138,nn=139,sn=140,an=141,rn=142,on=143,ln=144,fn=145,Un=146,cn=147,hn=148,_n=149,pn=150,gn=151,yn=152,xn=153,un=154,bn=155,dn=156,mn=157,wn=158,Dn=159,Bn=160,Ln=161,An=162,kn=163,vn=164,Sn=165,In=166,On=167,Tn=168,Vn=169,En=170,Fn=171,Pn=172,Cn=173,zn=174,Mn=175,Wn=176,Nn=177,Hn=178,Kn=179,Xn=180,jn=181,Rn=182,Yn=183,qn=184,Gn=1000156007,Jn=1000156008,Qn=1000156009,Zn=1000156010,$n=1000156011,ti=1000156017,ei=1000156018,ni=1000156019,ii=1000156020,si=1000156021,ai=1000054e3,ri=1000054001,oi=1000054002,li=1000054003,fi=1000054004,Ui=1000054005,ci=1000054006,hi=1000054007,_i=1000066e3,pi=1000066001,gi=1000066002,yi=1000066003,xi=1000066004,ui=1000066005,bi=1000066006,di=1000066007,mi=1000066008,wi=1000066009,Di=1000066010,Bi=1000066011,Li=1000066012,Ai=1000066013,ki=100034e4,vi=1000340001;class Si{constructor(){this.vkFormat=0,this.typeSize=1,this.pixelWidth=0,this.pixelHeight=0,this.pixelDepth=0,this.layerCount=0,this.faceCount=1,this.supercompressionScheme=0,this.levels=[],this.dataFormatDescriptor=[{vendorId:0,descriptorType:0,descriptorBlockSize:0,versionNumber:2,colorModel:0,colorPrimaries:1,transferFunction:2,flags:0,texelBlockDimension:[0,0,0,0],bytesPlane:[0,0,0,0,0,0,0,0],samples:[]}],this.keyValue={},this.globalData=null;}}class Ii{constructor(t,e,n,i){this._dataView=new DataView(t.buffer,t.byteOffset+e,n),this._littleEndian=i,this._offset=0;}_nextUint8(){const t=this._dataView.getUint8(this._offset);return this._offset+=1,t}_nextUint16(){const t=this._dataView.getUint16(this._offset,this._littleEndian);return this._offset+=2,t}_nextUint32(){const t=this._dataView.getUint32(this._offset,this._littleEndian);return this._offset+=4,t}_nextUint64(){const t=this._dataView.getUint32(this._offset,this._littleEndian)+2**32*this._dataView.getUint32(this._offset+4,this._littleEndian);return this._offset+=8,t}_nextInt32(){const t=this._dataView.getInt32(this._offset,this._littleEndian);return this._offset+=4,t}_skip(t){return this._offset+=t,this}_scan(t,e=0){const n=this._offset;let i=0;for(;this._dataView.getUint8(this._offset)!==e&&i0?c+a.byteLength:0;h%8&&(h+=8-h%8);const _=[],p=new DataView(new ArrayBuffer(3*t.levels.length*8));let g=(h||c+a.byteLength)+n.byteLength;for(let e=0;e0?h:0),!0),x.setBigUint64(60,BigInt(n.byteLength),!0),new Uint8Array(Fi([new Uint8Array(Ti).buffer,y,p.buffer,o,a,h>0?new ArrayBuffer(h-(c+a.byteLength)):new ArrayBuffer(0),n,..._]))} /** * @author Don McCurdy / https://www.donmccurdy.com */ let init, instance, heap; const importObject = { env: { emscripten_notify_memory_growth: function ( index ) { heap = new Uint8Array( instance.exports.memory.buffer ); } } }; /** * ZSTD (Zstandard) decoder. * * Compiled from https://github.com/facebook/zstd/tree/dev/contrib/single_file_libs, with the * following steps: * * ``` * ./combine.sh -r ../../lib -o zstddeclib.c zstddeclib-in.c * emcc zstddeclib.c -Oz -s EXPORTED_FUNCTIONS="['_ZSTD_decompress', '_ZSTD_findDecompressedSize', '_ZSTD_isError', '_malloc', '_free']" -s ALLOW_MEMORY_GROWTH=1 -s MALLOC=emmalloc -o zstddec.wasm * base64 zstddec.wasm > zstddec.txt * ``` * * The base64 string written to `zstddec.txt` is embedded as the `wasm` variable at the bottom * of this file. The rest of this file is written by hand, in order to avoid an additional JS * wrapper generated by Emscripten. */ class ZSTDDecoder { init () { if ( ! init ) { init = fetch( 'data:application/wasm;base64,' + wasm ) .then( ( response ) => response.arrayBuffer() ) .then( ( arrayBuffer ) => WebAssembly.instantiate( arrayBuffer, importObject ) ) .then( ( result ) => { instance = result.instance; importObject.env.emscripten_notify_memory_growth( 0 ); // initialize heap. }); } return init; } decode ( array, uncompressedSize = 0 ) { // Write compressed data into WASM memory. const compressedSize = array.byteLength; const compressedPtr = instance.exports.malloc( compressedSize ); heap.set( array, compressedPtr ); // Decompress into WASM memory. uncompressedSize = uncompressedSize || Number( instance.exports.ZSTD_findDecompressedSize( compressedPtr, compressedSize ) ); const uncompressedPtr = instance.exports.malloc( uncompressedSize ); const actualSize = instance.exports.ZSTD_decompress( uncompressedPtr, uncompressedSize, compressedPtr, compressedSize ); // Read decompressed data and free WASM memory. const dec = heap.slice( uncompressedPtr, uncompressedPtr + actualSize ); instance.exports.free( compressedPtr ); instance.exports.free( uncompressedPtr ); return dec; } } /** * BSD License * * For Zstandard software * * Copyright (c) 2016-present, Yann Collet, Facebook, Inc. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * * Neither the name Facebook nor the names of its contributors may be used to * endorse or promote products derived from this software without specific * prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ const wasm = 'AGFzbQEAAAABpQEVYAF/AX9gAn9/AGADf39/AX9gBX9/f39/AX9gAX8AYAJ/fwF/YAR/f39/AX9gA39/fwBgBn9/f39/fwF/YAd/f39/f39/AX9gAn9/AX5gAn5+AX5gAABgBX9/f39/AGAGf39/f39/AGAIf39/f39/f38AYAl/f39/f39/f38AYAABf2AIf39/f39/f38Bf2ANf39/f39/f39/f39/fwF/YAF/AX4CJwEDZW52H2Vtc2NyaXB0ZW5fbm90aWZ5X21lbW9yeV9ncm93dGgABANpaAEFAAAFAgEFCwACAQABAgIFBQcAAwABDgsBAQcAEhMHAAUBDAQEAAANBwQCAgYCBAgDAwMDBgEACQkHBgICAAYGAgQUBwYGAwIGAAMCAQgBBwUGCgoEEQAEBAEIAwgDBQgDEA8IAAcABAUBcAECAgUEAQCAAgYJAX8BQaCgwAILB2AHBm1lbW9yeQIABm1hbGxvYwAoBGZyZWUAJgxaU1REX2lzRXJyb3IAaBlaU1REX2ZpbmREZWNvbXByZXNzZWRTaXplAFQPWlNURF9kZWNvbXByZXNzAEoGX3N0YXJ0ACQJBwEAQQELASQKussBaA8AIAAgACgCBCABajYCBAsZACAAKAIAIAAoAgRBH3F0QQAgAWtBH3F2CwgAIABBiH9LC34BBH9BAyEBIAAoAgQiA0EgTQRAIAAoAggiASAAKAIQTwRAIAAQDQ8LIAAoAgwiAiABRgRAQQFBAiADQSBJGw8LIAAgASABIAJrIANBA3YiBCABIARrIAJJIgEbIgJrIgQ2AgggACADIAJBA3RrNgIEIAAgBCgAADYCAAsgAQsUAQF/IAAgARACIQIgACABEAEgAgv3AQECfyACRQRAIABCADcCACAAQQA2AhAgAEIANwIIQbh/DwsgACABNgIMIAAgAUEEajYCECACQQRPBEAgACABIAJqIgFBfGoiAzYCCCAAIAMoAAA2AgAgAUF/ai0AACIBBEAgAEEIIAEQFGs2AgQgAg8LIABBADYCBEF/DwsgACABNgIIIAAgAS0AACIDNgIAIAJBfmoiBEEBTQRAIARBAWtFBEAgACABLQACQRB0IANyIgM2AgALIAAgAS0AAUEIdCADajYCAAsgASACakF/ai0AACIBRQRAIABBADYCBEFsDwsgAEEoIAEQFCACQQN0ams2AgQgAgsWACAAIAEpAAA3AAAgACABKQAINwAICy8BAX8gAUECdEGgHWooAgAgACgCAEEgIAEgACgCBGprQR9xdnEhAiAAIAEQASACCyEAIAFCz9bTvtLHq9lCfiAAfEIfiUKHla+vmLbem55/fgsdAQF/IAAoAgggACgCDEYEfyAAKAIEQSBGBUEACwuCBAEDfyACQYDAAE8EQCAAIAEgAhBnIAAPCyAAIAJqIQMCQCAAIAFzQQNxRQRAAkAgAkEBSARAIAAhAgwBCyAAQQNxRQRAIAAhAgwBCyAAIQIDQCACIAEtAAA6AAAgAUEBaiEBIAJBAWoiAiADTw0BIAJBA3ENAAsLAkAgA0F8cSIEQcAASQ0AIAIgBEFAaiIFSw0AA0AgAiABKAIANgIAIAIgASgCBDYCBCACIAEoAgg2AgggAiABKAIMNgIMIAIgASgCEDYCECACIAEoAhQ2AhQgAiABKAIYNgIYIAIgASgCHDYCHCACIAEoAiA2AiAgAiABKAIkNgIkIAIgASgCKDYCKCACIAEoAiw2AiwgAiABKAIwNgIwIAIgASgCNDYCNCACIAEoAjg2AjggAiABKAI8NgI8IAFBQGshASACQUBrIgIgBU0NAAsLIAIgBE8NAQNAIAIgASgCADYCACABQQRqIQEgAkEEaiICIARJDQALDAELIANBBEkEQCAAIQIMAQsgA0F8aiIEIABJBEAgACECDAELIAAhAgNAIAIgAS0AADoAACACIAEtAAE6AAEgAiABLQACOgACIAIgAS0AAzoAAyABQQRqIQEgAkEEaiICIARNDQALCyACIANJBEADQCACIAEtAAA6AAAgAUEBaiEBIAJBAWoiAiADRw0ACwsgAAsMACAAIAEpAAA3AAALQQECfyAAKAIIIgEgACgCEEkEQEEDDwsgACAAKAIEIgJBB3E2AgQgACABIAJBA3ZrIgE2AgggACABKAAANgIAQQALDAAgACABKAIANgAAC/cCAQJ/AkAgACABRg0AAkAgASACaiAASwRAIAAgAmoiBCABSw0BCyAAIAEgAhALDwsgACABc0EDcSEDAkACQCAAIAFJBEAgAwRAIAAhAwwDCyAAQQNxRQRAIAAhAwwCCyAAIQMDQCACRQ0EIAMgAS0AADoAACABQQFqIQEgAkF/aiECIANBAWoiA0EDcQ0ACwwBCwJAIAMNACAEQQNxBEADQCACRQ0FIAAgAkF/aiICaiIDIAEgAmotAAA6AAAgA0EDcQ0ACwsgAkEDTQ0AA0AgACACQXxqIgJqIAEgAmooAgA2AgAgAkEDSw0ACwsgAkUNAgNAIAAgAkF/aiICaiABIAJqLQAAOgAAIAINAAsMAgsgAkEDTQ0AIAIhBANAIAMgASgCADYCACABQQRqIQEgA0EEaiEDIARBfGoiBEEDSw0ACyACQQNxIQILIAJFDQADQCADIAEtAAA6AAAgA0EBaiEDIAFBAWohASACQX9qIgINAAsLIAAL8wICAn8BfgJAIAJFDQAgACACaiIDQX9qIAE6AAAgACABOgAAIAJBA0kNACADQX5qIAE6AAAgACABOgABIANBfWogAToAACAAIAE6AAIgAkEHSQ0AIANBfGogAToAACAAIAE6AAMgAkEJSQ0AIABBACAAa0EDcSIEaiIDIAFB/wFxQYGChAhsIgE2AgAgAyACIARrQXxxIgRqIgJBfGogATYCACAEQQlJDQAgAyABNgIIIAMgATYCBCACQXhqIAE2AgAgAkF0aiABNgIAIARBGUkNACADIAE2AhggAyABNgIUIAMgATYCECADIAE2AgwgAkFwaiABNgIAIAJBbGogATYCACACQWhqIAE2AgAgAkFkaiABNgIAIAQgA0EEcUEYciIEayICQSBJDQAgAa0iBUIghiAFhCEFIAMgBGohAQNAIAEgBTcDGCABIAU3AxAgASAFNwMIIAEgBTcDACABQSBqIQEgAkFgaiICQR9LDQALCyAACy8BAn8gACgCBCAAKAIAQQJ0aiICLQACIQMgACACLwEAIAEgAi0AAxAIajYCACADCy8BAn8gACgCBCAAKAIAQQJ0aiICLQACIQMgACACLwEAIAEgAi0AAxAFajYCACADCx8AIAAgASACKAIEEAg2AgAgARAEGiAAIAJBCGo2AgQLCAAgAGdBH3MLugUBDX8jAEEQayIKJAACfyAEQQNNBEAgCkEANgIMIApBDGogAyAEEAsaIAAgASACIApBDGpBBBAVIgBBbCAAEAMbIAAgACAESxsMAQsgAEEAIAEoAgBBAXRBAmoQECENQVQgAygAACIGQQ9xIgBBCksNABogAiAAQQVqNgIAIAMgBGoiAkF8aiEMIAJBeWohDiACQXtqIRAgAEEGaiELQQQhBSAGQQR2IQRBICAAdCIAQQFyIQkgASgCACEPQQAhAiADIQYCQANAIAlBAkggAiAPS3JFBEAgAiEHAkAgCARAA0AgBEH//wNxQf//A0YEQCAHQRhqIQcgBiAQSQR/IAZBAmoiBigAACAFdgUgBUEQaiEFIARBEHYLIQQMAQsLA0AgBEEDcSIIQQNGBEAgBUECaiEFIARBAnYhBCAHQQNqIQcMAQsLIAcgCGoiByAPSw0EIAVBAmohBQNAIAIgB0kEQCANIAJBAXRqQQA7AQAgAkEBaiECDAELCyAGIA5LQQAgBiAFQQN1aiIHIAxLG0UEQCAHKAAAIAVBB3EiBXYhBAwCCyAEQQJ2IQQLIAYhBwsCfyALQX9qIAQgAEF/anEiBiAAQQF0QX9qIgggCWsiEUkNABogBCAIcSIEQQAgESAEIABIG2shBiALCyEIIA0gAkEBdGogBkF/aiIEOwEAIAlBASAGayAEIAZBAUgbayEJA0AgCSAASARAIABBAXUhACALQX9qIQsMAQsLAn8gByAOS0EAIAcgBSAIaiIFQQN1aiIGIAxLG0UEQCAFQQdxDAELIAUgDCIGIAdrQQN0awshBSACQQFqIQIgBEUhCCAGKAAAIAVBH3F2IQQMAQsLQWwgCUEBRyAFQSBKcg0BGiABIAJBf2o2AgAgBiAFQQdqQQN1aiADawwBC0FQCyEAIApBEGokACAACwkAQQFBBSAAGwsMACAAIAEoAAA2AAALqgMBCn8jAEHwAGsiCiQAIAJBAWohDiAAQQhqIQtBgIAEIAVBf2p0QRB1IQxBACECQQEhBkEBIAV0IglBf2oiDyEIA0AgAiAORkUEQAJAIAEgAkEBdCINai8BACIHQf//A0YEQCALIAhBA3RqIAI2AgQgCEF/aiEIQQEhBwwBCyAGQQAgDCAHQRB0QRB1ShshBgsgCiANaiAHOwEAIAJBAWohAgwBCwsgACAFNgIEIAAgBjYCACAJQQN2IAlBAXZqQQNqIQxBACEAQQAhBkEAIQIDQCAGIA5GBEADQAJAIAAgCUYNACAKIAsgAEEDdGoiASgCBCIGQQF0aiICIAIvAQAiAkEBajsBACABIAUgAhAUayIIOgADIAEgAiAIQf8BcXQgCWs7AQAgASAEIAZBAnQiAmooAgA6AAIgASACIANqKAIANgIEIABBAWohAAwBCwsFIAEgBkEBdGouAQAhDUEAIQcDQCAHIA1ORQRAIAsgAkEDdGogBjYCBANAIAIgDGogD3EiAiAISw0ACyAHQQFqIQcMAQsLIAZBAWohBgwBCwsgCkHwAGokAAsjAEIAIAEQCSAAhUKHla+vmLbem55/fkLj3MqV/M7y9YV/fAsQACAAQn43AwggACABNgIACyQBAX8gAARAIAEoAgQiAgRAIAEoAgggACACEQEADwsgABAmCwsfACAAIAEgAi8BABAINgIAIAEQBBogACACQQRqNgIEC0oBAX9BoCAoAgAiASAAaiIAQX9MBEBBiCBBMDYCAEF/DwsCQCAAPwBBEHRNDQAgABBmDQBBiCBBMDYCAEF/DwtBoCAgADYCACABC9cBAQh/Qbp/IQoCQCACKAIEIgggAigCACIJaiIOIAEgAGtLDQBBbCEKIAkgBCADKAIAIgtrSw0AIAAgCWoiBCACKAIIIgxrIQ0gACABQWBqIg8gCyAJQQAQKSADIAkgC2o2AgACQAJAIAwgBCAFa00EQCANIQUMAQsgDCAEIAZrSw0CIAcgDSAFayIAaiIBIAhqIAdNBEAgBCABIAgQDxoMAgsgBCABQQAgAGsQDyEBIAIgACAIaiIINgIEIAEgAGshBAsgBCAPIAUgCEEBECkLIA4hCgsgCgubAgEBfyMAQYABayINJAAgDSADNgJ8AkAgAkEDSwRAQX8hCQwBCwJAAkACQAJAIAJBAWsOAwADAgELIAZFBEBBuH8hCQwEC0FsIQkgBS0AACICIANLDQMgACAHIAJBAnQiAmooAgAgAiAIaigCABA7IAEgADYCAEEBIQkMAwsgASAJNgIAQQAhCQwCCyAKRQRAQWwhCQwCC0EAIQkgC0UgDEEZSHINAUEIIAR0QQhqIQBBACECA0AgAiAATw0CIAJBQGshAgwAAAsAC0FsIQkgDSANQfwAaiANQfgAaiAFIAYQFSICEAMNACANKAJ4IgMgBEsNACAAIA0gDSgCfCAHIAggAxAYIAEgADYCACACIQkLIA1BgAFqJAAgCQsLACAAIAEgAhALGgsQACAALwAAIAAtAAJBEHRyCy8AAn9BuH8gAUEISQ0AGkFyIAAoAAQiAEF3Sw0AGkG4fyAAQQhqIgAgACABSxsLCwkAIAAgATsAAAsDAAELigYBBX8gACAAKAIAIgVBfnE2AgBBACAAIAVBAXZqQYQgKAIAIgQgAEYbIQECQAJAIAAoAgQiAkUNACACKAIAIgNBAXENACACQQhqIgUgA0EBdkF4aiIDQQggA0EISxtnQR9zQQJ0QYAfaiIDKAIARgRAIAMgAigCDDYCAAsgAigCCCIDBEAgAyACKAIMNgIECyACKAIMIgMEQCADIAIoAgg2AgALIAIgAigCACAAKAIAQX5xajYCAEGEICEAAkACQCABRQ0AIAEgAjYCBCABKAIAIgNBAXENASADQQF2QXhqIgNBCCADQQhLG2dBH3NBAnRBgB9qIgMoAgAgAUEIakYEQCADIAEoAgw2AgALIAEoAggiAwRAIAMgASgCDDYCBAsgASgCDCIDBEAgAyABKAIINgIAQYQgKAIAIQQLIAIgAigCACABKAIAQX5xajYCACABIARGDQAgASABKAIAQQF2akEEaiEACyAAIAI2AgALIAIoAgBBAXZBeGoiAEEIIABBCEsbZ0Efc0ECdEGAH2oiASgCACEAIAEgBTYCACACIAA2AgwgAkEANgIIIABFDQEgACAFNgIADwsCQCABRQ0AIAEoAgAiAkEBcQ0AIAJBAXZBeGoiAkEIIAJBCEsbZ0Efc0ECdEGAH2oiAigCACABQQhqRgRAIAIgASgCDDYCAAsgASgCCCICBEAgAiABKAIMNgIECyABKAIMIgIEQCACIAEoAgg2AgBBhCAoAgAhBAsgACAAKAIAIAEoAgBBfnFqIgI2AgACQCABIARHBEAgASABKAIAQQF2aiAANgIEIAAoAgAhAgwBC0GEICAANgIACyACQQF2QXhqIgFBCCABQQhLG2dBH3NBAnRBgB9qIgIoAgAhASACIABBCGoiAjYCACAAIAE2AgwgAEEANgIIIAFFDQEgASACNgIADwsgBUEBdkF4aiIBQQggAUEISxtnQR9zQQJ0QYAfaiICKAIAIQEgAiAAQQhqIgI2AgAgACABNgIMIABBADYCCCABRQ0AIAEgAjYCAAsLDgAgAARAIABBeGoQJQsLgAIBA38CQCAAQQ9qQXhxQYQgKAIAKAIAQQF2ayICEB1Bf0YNAAJAQYQgKAIAIgAoAgAiAUEBcQ0AIAFBAXZBeGoiAUEIIAFBCEsbZ0Efc0ECdEGAH2oiASgCACAAQQhqRgRAIAEgACgCDDYCAAsgACgCCCIBBEAgASAAKAIMNgIECyAAKAIMIgFFDQAgASAAKAIINgIAC0EBIQEgACAAKAIAIAJBAXRqIgI2AgAgAkEBcQ0AIAJBAXZBeGoiAkEIIAJBCEsbZ0Efc0ECdEGAH2oiAygCACECIAMgAEEIaiIDNgIAIAAgAjYCDCAAQQA2AgggAkUNACACIAM2AgALIAELtwIBA38CQAJAIABBASAAGyICEDgiAA0AAkACQEGEICgCACIARQ0AIAAoAgAiA0EBcQ0AIAAgA0EBcjYCACADQQF2QXhqIgFBCCABQQhLG2dBH3NBAnRBgB9qIgEoAgAgAEEIakYEQCABIAAoAgw2AgALIAAoAggiAQRAIAEgACgCDDYCBAsgACgCDCIBBEAgASAAKAIINgIACyACECchAkEAIQFBhCAoAgAhACACDQEgACAAKAIAQX5xNgIAQQAPCyACQQ9qQXhxIgMQHSICQX9GDQIgAkEHakF4cSIAIAJHBEAgACACaxAdQX9GDQMLAkBBhCAoAgAiAUUEQEGAICAANgIADAELIAAgATYCBAtBhCAgADYCACAAIANBAXRBAXI2AgAMAQsgAEUNAQsgAEEIaiEBCyABC7kDAQJ/IAAgA2ohBQJAIANBB0wEQANAIAAgBU8NAiAAIAItAAA6AAAgAEEBaiEAIAJBAWohAgwAAAsACyAEQQFGBEACQCAAIAJrIgZBB00EQCAAIAItAAA6AAAgACACLQABOgABIAAgAi0AAjoAAiAAIAItAAM6AAMgAEEEaiACIAZBAnQiBkHAHmooAgBqIgIQFyACIAZB4B5qKAIAayECDAELIAAgAhAMCyACQQhqIQIgAEEIaiEACwJAAkACQAJAIAUgAU0EQCAAIANqIQEgBEEBRyAAIAJrQQ9Kcg0BA0AgACACEAwgAkEIaiECIABBCGoiACABSQ0ACwwFCyAAIAFLBEAgACEBDAQLIARBAUcgACACa0EPSnINASAAIQMgAiEEA0AgAyAEEAwgBEEIaiEEIANBCGoiAyABSQ0ACwwCCwNAIAAgAhAHIAJBEGohAiAAQRBqIgAgAUkNAAsMAwsgACEDIAIhBANAIAMgBBAHIARBEGohBCADQRBqIgMgAUkNAAsLIAIgASAAa2ohAgsDQCABIAVPDQEgASACLQAAOgAAIAFBAWohASACQQFqIQIMAAALAAsLQQECfyAAIAAoArjgASIDNgLE4AEgACgCvOABIQQgACABNgK84AEgACABIAJqNgK44AEgACABIAQgA2tqNgLA4AELpgEBAX8gACAAKALs4QEQFjYCyOABIABCADcD+OABIABCADcDuOABIABBwOABakIANwMAIABBqNAAaiIBQYyAgOAANgIAIABBADYCmOIBIABCADcDiOEBIABCAzcDgOEBIABBrNABakHgEikCADcCACAAQbTQAWpB6BIoAgA2AgAgACABNgIMIAAgAEGYIGo2AgggACAAQaAwajYCBCAAIABBEGo2AgALYQEBf0G4fyEDAkAgAUEDSQ0AIAIgABAhIgFBA3YiADYCCCACIAFBAXE2AgQgAiABQQF2QQNxIgM2AgACQCADQX9qIgFBAksNAAJAIAFBAWsOAgEAAgtBbA8LIAAhAwsgAwsMACAAIAEgAkEAEC4LiAQCA38CfiADEBYhBCAAQQBBKBAQIQAgBCACSwRAIAQPCyABRQRAQX8PCwJAAkAgA0EBRg0AIAEoAAAiBkGo6r5pRg0AQXYhAyAGQXBxQdDUtMIBRw0BQQghAyACQQhJDQEgAEEAQSgQECEAIAEoAAQhASAAQQE2AhQgACABrTcDAEEADwsgASACIAMQLyIDIAJLDQAgACADNgIYQXIhAyABIARqIgVBf2otAAAiAkEIcQ0AIAJBIHEiBkUEQEFwIQMgBS0AACIFQacBSw0BIAVBB3GtQgEgBUEDdkEKaq2GIgdCA4h+IAd8IQggBEEBaiEECyACQQZ2IQMgAkECdiEFAkAgAkEDcUF/aiICQQJLBEBBACECDAELAkACQAJAIAJBAWsOAgECAAsgASAEai0AACECIARBAWohBAwCCyABIARqLwAAIQIgBEECaiEEDAELIAEgBGooAAAhAiAEQQRqIQQLIAVBAXEhBQJ+AkACQAJAIANBf2oiA0ECTQRAIANBAWsOAgIDAQtCfyAGRQ0DGiABIARqMQAADAMLIAEgBGovAACtQoACfAwCCyABIARqKAAArQwBCyABIARqKQAACyEHIAAgBTYCICAAIAI2AhwgACAHNwMAQQAhAyAAQQA2AhQgACAHIAggBhsiBzcDCCAAIAdCgIAIIAdCgIAIVBs+AhALIAMLWwEBf0G4fyEDIAIQFiICIAFNBH8gACACakF/ai0AACIAQQNxQQJ0QaAeaigCACACaiAAQQZ2IgFBAnRBsB5qKAIAaiAAQSBxIgBFaiABRSAAQQV2cWoFQbh/CwsdACAAKAKQ4gEQWiAAQQA2AqDiASAAQgA3A5DiAQu1AwEFfyMAQZACayIKJABBuH8hBgJAIAVFDQAgBCwAACIIQf8BcSEHAkAgCEF/TARAIAdBgn9qQQF2IgggBU8NAkFsIQYgB0GBf2oiBUGAAk8NAiAEQQFqIQdBACEGA0AgBiAFTwRAIAUhBiAIIQcMAwUgACAGaiAHIAZBAXZqIgQtAABBBHY6AAAgACAGQQFyaiAELQAAQQ9xOgAAIAZBAmohBgwBCwAACwALIAcgBU8NASAAIARBAWogByAKEFMiBhADDQELIAYhBEEAIQYgAUEAQTQQECEJQQAhBQNAIAQgBkcEQCAAIAZqIggtAAAiAUELSwRAQWwhBgwDBSAJIAFBAnRqIgEgASgCAEEBajYCACAGQQFqIQZBASAILQAAdEEBdSAFaiEFDAILAAsLQWwhBiAFRQ0AIAUQFEEBaiIBQQxLDQAgAyABNgIAQQFBASABdCAFayIDEBQiAXQgA0cNACAAIARqIAFBAWoiADoAACAJIABBAnRqIgAgACgCAEEBajYCACAJKAIEIgBBAkkgAEEBcXINACACIARBAWo2AgAgB0EBaiEGCyAKQZACaiQAIAYLxhEBDH8jAEHwAGsiBSQAQWwhCwJAIANBCkkNACACLwAAIQogAi8AAiEJIAIvAAQhByAFQQhqIAQQDgJAIAMgByAJIApqakEGaiIMSQ0AIAUtAAohCCAFQdgAaiACQQZqIgIgChAGIgsQAw0BIAVBQGsgAiAKaiICIAkQBiILEAMNASAFQShqIAIgCWoiAiAHEAYiCxADDQEgBUEQaiACIAdqIAMgDGsQBiILEAMNASAAIAFqIg9BfWohECAEQQRqIQZBASELIAAgAUEDakECdiIDaiIMIANqIgIgA2oiDiEDIAIhBCAMIQcDQCALIAMgEElxBEAgACAGIAVB2ABqIAgQAkECdGoiCS8BADsAACAFQdgAaiAJLQACEAEgCS0AAyELIAcgBiAFQUBrIAgQAkECdGoiCS8BADsAACAFQUBrIAktAAIQASAJLQADIQogBCAGIAVBKGogCBACQQJ0aiIJLwEAOwAAIAVBKGogCS0AAhABIAktAAMhCSADIAYgBUEQaiAIEAJBAnRqIg0vAQA7AAAgBUEQaiANLQACEAEgDS0AAyENIAAgC2oiCyAGIAVB2ABqIAgQAkECdGoiAC8BADsAACAFQdgAaiAALQACEAEgAC0AAyEAIAcgCmoiCiAGIAVBQGsgCBACQQJ0aiIHLwEAOwAAIAVBQGsgBy0AAhABIActAAMhByAEIAlqIgkgBiAFQShqIAgQAkECdGoiBC8BADsAACAFQShqIAQtAAIQASAELQADIQQgAyANaiIDIAYgBUEQaiAIEAJBAnRqIg0vAQA7AAAgBUEQaiANLQACEAEgACALaiEAIAcgCmohByAEIAlqIQQgAyANLQADaiEDIAVB2ABqEA0gBUFAaxANciAFQShqEA1yIAVBEGoQDXJFIQsMAQsLIAQgDksgByACS3INAEFsIQsgACAMSw0BIAxBfWohCQNAQQAgACAJSSAFQdgAahAEGwRAIAAgBiAFQdgAaiAIEAJBAnRqIgovAQA7AAAgBUHYAGogCi0AAhABIAAgCi0AA2oiACAGIAVB2ABqIAgQAkECdGoiCi8BADsAACAFQdgAaiAKLQACEAEgACAKLQADaiEADAEFIAxBfmohCgNAIAVB2ABqEAQgACAKS3JFBEAgACAGIAVB2ABqIAgQAkECdGoiCS8BADsAACAFQdgAaiAJLQACEAEgACAJLQADaiEADAELCwNAIAAgCk0EQCAAIAYgBUHYAGogCBACQQJ0aiIJLwEAOwAAIAVB2ABqIAktAAIQASAAIAktAANqIQAMAQsLAkAgACAMTw0AIAAgBiAFQdgAaiAIEAIiAEECdGoiDC0AADoAACAMLQADQQFGBEAgBUHYAGogDC0AAhABDAELIAUoAlxBH0sNACAFQdgAaiAGIABBAnRqLQACEAEgBSgCXEEhSQ0AIAVBIDYCXAsgAkF9aiEMA0BBACAHIAxJIAVBQGsQBBsEQCAHIAYgBUFAayAIEAJBAnRqIgAvAQA7AAAgBUFAayAALQACEAEgByAALQADaiIAIAYgBUFAayAIEAJBAnRqIgcvAQA7AAAgBUFAayAHLQACEAEgACAHLQADaiEHDAEFIAJBfmohDANAIAVBQGsQBCAHIAxLckUEQCAHIAYgBUFAayAIEAJBAnRqIgAvAQA7AAAgBUFAayAALQACEAEgByAALQADaiEHDAELCwNAIAcgDE0EQCAHIAYgBUFAayAIEAJBAnRqIgAvAQA7AAAgBUFAayAALQACEAEgByAALQADaiEHDAELCwJAIAcgAk8NACAHIAYgBUFAayAIEAIiAEECdGoiAi0AADoAACACLQADQQFGBEAgBUFAayACLQACEAEMAQsgBSgCREEfSw0AIAVBQGsgBiAAQQJ0ai0AAhABIAUoAkRBIUkNACAFQSA2AkQLIA5BfWohAgNAQQAgBCACSSAFQShqEAQbBEAgBCAGIAVBKGogCBACQQJ0aiIALwEAOwAAIAVBKGogAC0AAhABIAQgAC0AA2oiACAGIAVBKGogCBACQQJ0aiIELwEAOwAAIAVBKGogBC0AAhABIAAgBC0AA2ohBAwBBSAOQX5qIQIDQCAFQShqEAQgBCACS3JFBEAgBCAGIAVBKGogCBACQQJ0aiIALwEAOwAAIAVBKGogAC0AAhABIAQgAC0AA2ohBAwBCwsDQCAEIAJNBEAgBCAGIAVBKGogCBACQQJ0aiIALwEAOwAAIAVBKGogAC0AAhABIAQgAC0AA2ohBAwBCwsCQCAEIA5PDQAgBCAGIAVBKGogCBACIgBBAnRqIgItAAA6AAAgAi0AA0EBRgRAIAVBKGogAi0AAhABDAELIAUoAixBH0sNACAFQShqIAYgAEECdGotAAIQASAFKAIsQSFJDQAgBUEgNgIsCwNAQQAgAyAQSSAFQRBqEAQbBEAgAyAGIAVBEGogCBACQQJ0aiIALwEAOwAAIAVBEGogAC0AAhABIAMgAC0AA2oiACAGIAVBEGogCBACQQJ0aiICLwEAOwAAIAVBEGogAi0AAhABIAAgAi0AA2ohAwwBBSAPQX5qIQIDQCAFQRBqEAQgAyACS3JFBEAgAyAGIAVBEGogCBACQQJ0aiIALwEAOwAAIAVBEGogAC0AAhABIAMgAC0AA2ohAwwBCwsDQCADIAJNBEAgAyAGIAVBEGogCBACQQJ0aiIALwEAOwAAIAVBEGogAC0AAhABIAMgAC0AA2ohAwwBCwsCQCADIA9PDQAgAyAGIAVBEGogCBACIgBBAnRqIgItAAA6AAAgAi0AA0EBRgRAIAVBEGogAi0AAhABDAELIAUoAhRBH0sNACAFQRBqIAYgAEECdGotAAIQASAFKAIUQSFJDQAgBUEgNgIUCyABQWwgBUHYAGoQCiAFQUBrEApxIAVBKGoQCnEgBUEQahAKcRshCwwJCwAACwALAAALAAsAAAsACwAACwALQWwhCwsgBUHwAGokACALC7UEAQ5/IwBBEGsiBiQAIAZBBGogABAOQVQhBQJAIARB3AtJDQAgBi0ABCEHIANB8ARqQQBB7AAQECEIIAdBDEsNACADQdwJaiIJIAggBkEIaiAGQQxqIAEgAhAxIhAQA0UEQCAGKAIMIgQgB0sNASADQdwFaiEPIANBpAVqIREgAEEEaiESIANBqAVqIQEgBCEFA0AgBSICQX9qIQUgCCACQQJ0aigCAEUNAAsgAkEBaiEOQQEhBQNAIAUgDk9FBEAgCCAFQQJ0IgtqKAIAIQwgASALaiAKNgIAIAVBAWohBSAKIAxqIQoMAQsLIAEgCjYCAEEAIQUgBigCCCELA0AgBSALRkUEQCABIAUgCWotAAAiDEECdGoiDSANKAIAIg1BAWo2AgAgDyANQQF0aiINIAw6AAEgDSAFOgAAIAVBAWohBQwBCwtBACEBIANBADYCqAUgBEF/cyAHaiEJQQEhBQNAIAUgDk9FBEAgCCAFQQJ0IgtqKAIAIQwgAyALaiABNgIAIAwgBSAJanQgAWohASAFQQFqIQUMAQsLIAcgBEEBaiIBIAJrIgRrQQFqIQgDQEEBIQUgBCAIT0UEQANAIAUgDk9FBEAgBUECdCIJIAMgBEE0bGpqIAMgCWooAgAgBHY2AgAgBUEBaiEFDAELCyAEQQFqIQQMAQsLIBIgByAPIAogESADIAIgARBkIAZBAToABSAGIAc6AAYgACAGKAIENgIACyAQIQULIAZBEGokACAFC8ENAQt/IwBB8ABrIgUkAEFsIQkCQCADQQpJDQAgAi8AACEKIAIvAAIhDCACLwAEIQYgBUEIaiAEEA4CQCADIAYgCiAMampBBmoiDUkNACAFLQAKIQcgBUHYAGogAkEGaiICIAoQBiIJEAMNASAFQUBrIAIgCmoiAiAMEAYiCRADDQEgBUEoaiACIAxqIgIgBhAGIgkQAw0BIAVBEGogAiAGaiADIA1rEAYiCRADDQEgACABaiIOQX1qIQ8gBEEEaiEGQQEhCSAAIAFBA2pBAnYiAmoiCiACaiIMIAJqIg0hAyAMIQQgCiECA0AgCSADIA9JcQRAIAYgBUHYAGogBxACQQF0aiIILQAAIQsgBUHYAGogCC0AARABIAAgCzoAACAGIAVBQGsgBxACQQF0aiIILQAAIQsgBUFAayAILQABEAEgAiALOgAAIAYgBUEoaiAHEAJBAXRqIggtAAAhCyAFQShqIAgtAAEQASAEIAs6AAAgBiAFQRBqIAcQAkEBdGoiCC0AACELIAVBEGogCC0AARABIAMgCzoAACAGIAVB2ABqIAcQAkEBdGoiCC0AACELIAVB2ABqIAgtAAEQASAAIAs6AAEgBiAFQUBrIAcQAkEBdGoiCC0AACELIAVBQGsgCC0AARABIAIgCzoAASAGIAVBKGogBxACQQF0aiIILQAAIQsgBUEoaiAILQABEAEgBCALOgABIAYgBUEQaiAHEAJBAXRqIggtAAAhCyAFQRBqIAgtAAEQASADIAs6AAEgA0ECaiEDIARBAmohBCACQQJqIQIgAEECaiEAIAkgBUHYAGoQDUVxIAVBQGsQDUVxIAVBKGoQDUVxIAVBEGoQDUVxIQkMAQsLIAQgDUsgAiAMS3INAEFsIQkgACAKSw0BIApBfWohCQNAIAVB2ABqEAQgACAJT3JFBEAgBiAFQdgAaiAHEAJBAXRqIggtAAAhCyAFQdgAaiAILQABEAEgACALOgAAIAYgBUHYAGogBxACQQF0aiIILQAAIQsgBUHYAGogCC0AARABIAAgCzoAASAAQQJqIQAMAQsLA0AgBUHYAGoQBCAAIApPckUEQCAGIAVB2ABqIAcQAkEBdGoiCS0AACEIIAVB2ABqIAktAAEQASAAIAg6AAAgAEEBaiEADAELCwNAIAAgCkkEQCAGIAVB2ABqIAcQAkEBdGoiCS0AACEIIAVB2ABqIAktAAEQASAAIAg6AAAgAEEBaiEADAELCyAMQX1qIQADQCAFQUBrEAQgAiAAT3JFBEAgBiAFQUBrIAcQAkEBdGoiCi0AACEJIAVBQGsgCi0AARABIAIgCToAACAGIAVBQGsgBxACQQF0aiIKLQAAIQkgBUFAayAKLQABEAEgAiAJOgABIAJBAmohAgwBCwsDQCAFQUBrEAQgAiAMT3JFBEAgBiAFQUBrIAcQAkEBdGoiAC0AACEKIAVBQGsgAC0AARABIAIgCjoAACACQQFqIQIMAQsLA0AgAiAMSQRAIAYgBUFAayAHEAJBAXRqIgAtAAAhCiAFQUBrIAAtAAEQASACIAo6AAAgAkEBaiECDAELCyANQX1qIQADQCAFQShqEAQgBCAAT3JFBEAgBiAFQShqIAcQAkEBdGoiAi0AACEKIAVBKGogAi0AARABIAQgCjoAACAGIAVBKGogBxACQQF0aiICLQAAIQogBUEoaiACLQABEAEgBCAKOgABIARBAmohBAwBCwsDQCAFQShqEAQgBCANT3JFBEAgBiAFQShqIAcQAkEBdGoiAC0AACECIAVBKGogAC0AARABIAQgAjoAACAEQQFqIQQMAQsLA0AgBCANSQRAIAYgBUEoaiAHEAJBAXRqIgAtAAAhAiAFQShqIAAtAAEQASAEIAI6AAAgBEEBaiEEDAELCwNAIAVBEGoQBCADIA9PckUEQCAGIAVBEGogBxACQQF0aiIALQAAIQIgBUEQaiAALQABEAEgAyACOgAAIAYgBUEQaiAHEAJBAXRqIgAtAAAhAiAFQRBqIAAtAAEQASADIAI6AAEgA0ECaiEDDAELCwNAIAVBEGoQBCADIA5PckUEQCAGIAVBEGogBxACQQF0aiIALQAAIQIgBUEQaiAALQABEAEgAyACOgAAIANBAWohAwwBCwsDQCADIA5JBEAgBiAFQRBqIAcQAkEBdGoiAC0AACECIAVBEGogAC0AARABIAMgAjoAACADQQFqIQMMAQsLIAFBbCAFQdgAahAKIAVBQGsQCnEgBUEoahAKcSAFQRBqEApxGyEJDAELQWwhCQsgBUHwAGokACAJC8oCAQR/IwBBIGsiBSQAIAUgBBAOIAUtAAIhByAFQQhqIAIgAxAGIgIQA0UEQCAEQQRqIQIgACABaiIDQX1qIQQDQCAFQQhqEAQgACAET3JFBEAgAiAFQQhqIAcQAkEBdGoiBi0AACEIIAVBCGogBi0AARABIAAgCDoAACACIAVBCGogBxACQQF0aiIGLQAAIQggBUEIaiAGLQABEAEgACAIOgABIABBAmohAAwBCwsDQCAFQQhqEAQgACADT3JFBEAgAiAFQQhqIAcQAkEBdGoiBC0AACEGIAVBCGogBC0AARABIAAgBjoAACAAQQFqIQAMAQsLA0AgACADT0UEQCACIAVBCGogBxACQQF0aiIELQAAIQYgBUEIaiAELQABEAEgACAGOgAAIABBAWohAAwBCwsgAUFsIAVBCGoQChshAgsgBUEgaiQAIAILtgMBCX8jAEEQayIGJAAgBkEANgIMIAZBADYCCEFUIQQCQAJAIANBQGsiDCADIAZBCGogBkEMaiABIAIQMSICEAMNACAGQQRqIAAQDiAGKAIMIgcgBi0ABEEBaksNASAAQQRqIQogBkEAOgAFIAYgBzoABiAAIAYoAgQ2AgAgB0EBaiEJQQEhBANAIAQgCUkEQCADIARBAnRqIgEoAgAhACABIAU2AgAgACAEQX9qdCAFaiEFIARBAWohBAwBCwsgB0EBaiEHQQAhBSAGKAIIIQkDQCAFIAlGDQEgAyAFIAxqLQAAIgRBAnRqIgBBASAEdEEBdSILIAAoAgAiAWoiADYCACAHIARrIQhBACEEAkAgC0EDTQRAA0AgBCALRg0CIAogASAEakEBdGoiACAIOgABIAAgBToAACAEQQFqIQQMAAALAAsDQCABIABPDQEgCiABQQF0aiIEIAg6AAEgBCAFOgAAIAQgCDoAAyAEIAU6AAIgBCAIOgAFIAQgBToABCAEIAg6AAcgBCAFOgAGIAFBBGohAQwAAAsACyAFQQFqIQUMAAALAAsgAiEECyAGQRBqJAAgBAutAQECfwJAQYQgKAIAIABHIAAoAgBBAXYiAyABa0F4aiICQXhxQQhHcgR/IAIFIAMQJ0UNASACQQhqC0EQSQ0AIAAgACgCACICQQFxIAAgAWpBD2pBeHEiASAAa0EBdHI2AgAgASAANgIEIAEgASgCAEEBcSAAIAJBAXZqIAFrIgJBAXRyNgIAQYQgIAEgAkH/////B3FqQQRqQYQgKAIAIABGGyABNgIAIAEQJQsLygIBBX8CQAJAAkAgAEEIIABBCEsbZ0EfcyAAaUEBR2oiAUEESSAAIAF2cg0AIAFBAnRB/B5qKAIAIgJFDQADQCACQXhqIgMoAgBBAXZBeGoiBSAATwRAIAIgBUEIIAVBCEsbZ0Efc0ECdEGAH2oiASgCAEYEQCABIAIoAgQ2AgALDAMLIARBHksNASAEQQFqIQQgAigCBCICDQALC0EAIQMgAUEgTw0BA0AgAUECdEGAH2ooAgAiAkUEQCABQR5LIQIgAUEBaiEBIAJFDQEMAwsLIAIgAkF4aiIDKAIAQQF2QXhqIgFBCCABQQhLG2dBH3NBAnRBgB9qIgEoAgBGBEAgASACKAIENgIACwsgAigCACIBBEAgASACKAIENgIECyACKAIEIgEEQCABIAIoAgA2AgALIAMgAygCAEEBcjYCACADIAAQNwsgAwvhCwINfwV+IwBB8ABrIgckACAHIAAoAvDhASIINgJcIAEgAmohDSAIIAAoAoDiAWohDwJAAkAgBUUEQCABIQQMAQsgACgCxOABIRAgACgCwOABIREgACgCvOABIQ4gAEEBNgKM4QFBACEIA0AgCEEDRwRAIAcgCEECdCICaiAAIAJqQazQAWooAgA2AkQgCEEBaiEIDAELC0FsIQwgB0EYaiADIAQQBhADDQEgB0EsaiAHQRhqIAAoAgAQEyAHQTRqIAdBGGogACgCCBATIAdBPGogB0EYaiAAKAIEEBMgDUFgaiESIAEhBEEAIQwDQCAHKAIwIAcoAixBA3RqKQIAIhRCEIinQf8BcSEIIAcoAkAgBygCPEEDdGopAgAiFUIQiKdB/wFxIQsgBygCOCAHKAI0QQN0aikCACIWQiCIpyEJIBVCIIghFyAUQiCIpyECAkAgFkIQiKdB/wFxIgNBAk8EQAJAIAZFIANBGUlyRQRAIAkgB0EYaiADQSAgBygCHGsiCiAKIANLGyIKEAUgAyAKayIDdGohCSAHQRhqEAQaIANFDQEgB0EYaiADEAUgCWohCQwBCyAHQRhqIAMQBSAJaiEJIAdBGGoQBBoLIAcpAkQhGCAHIAk2AkQgByAYNwNIDAELAkAgA0UEQCACBEAgBygCRCEJDAMLIAcoAkghCQwBCwJAAkAgB0EYakEBEAUgCSACRWpqIgNBA0YEQCAHKAJEQX9qIgMgA0VqIQkMAQsgA0ECdCAHaigCRCIJIAlFaiEJIANBAUYNAQsgByAHKAJINgJMCwsgByAHKAJENgJIIAcgCTYCRAsgF6chAyALBEAgB0EYaiALEAUgA2ohAwsgCCALakEUTwRAIAdBGGoQBBoLIAgEQCAHQRhqIAgQBSACaiECCyAHQRhqEAQaIAcgB0EYaiAUQhiIp0H/AXEQCCAUp0H//wNxajYCLCAHIAdBGGogFUIYiKdB/wFxEAggFadB//8DcWo2AjwgB0EYahAEGiAHIAdBGGogFkIYiKdB/wFxEAggFqdB//8DcWo2AjQgByACNgJgIAcoAlwhCiAHIAk2AmggByADNgJkAkACQAJAIAQgAiADaiILaiASSw0AIAIgCmoiEyAPSw0AIA0gBGsgC0Egak8NAQsgByAHKQNoNwMQIAcgBykDYDcDCCAEIA0gB0EIaiAHQdwAaiAPIA4gESAQEB4hCwwBCyACIARqIQggBCAKEAcgAkERTwRAIARBEGohAgNAIAIgCkEQaiIKEAcgAkEQaiICIAhJDQALCyAIIAlrIQIgByATNgJcIAkgCCAOa0sEQCAJIAggEWtLBEBBbCELDAILIBAgAiAOayICaiIKIANqIBBNBEAgCCAKIAMQDxoMAgsgCCAKQQAgAmsQDyEIIAcgAiADaiIDNgJkIAggAmshCCAOIQILIAlBEE8EQCADIAhqIQMDQCAIIAIQByACQRBqIQIgCEEQaiIIIANJDQALDAELAkAgCUEHTQRAIAggAi0AADoAACAIIAItAAE6AAEgCCACLQACOgACIAggAi0AAzoAAyAIQQRqIAIgCUECdCIDQcAeaigCAGoiAhAXIAIgA0HgHmooAgBrIQIgBygCZCEDDAELIAggAhAMCyADQQlJDQAgAyAIaiEDIAhBCGoiCCACQQhqIgJrQQ9MBEADQCAIIAIQDCACQQhqIQIgCEEIaiIIIANJDQAMAgALAAsDQCAIIAIQByACQRBqIQIgCEEQaiIIIANJDQALCyAHQRhqEAQaIAsgDCALEAMiAhshDCAEIAQgC2ogAhshBCAFQX9qIgUNAAsgDBADDQFBbCEMIAdBGGoQBEECSQ0BQQAhCANAIAhBA0cEQCAAIAhBAnQiAmpBrNABaiACIAdqKAJENgIAIAhBAWohCAwBCwsgBygCXCEIC0G6fyEMIA8gCGsiACANIARrSw0AIAQEfyAEIAggABALIABqBUEACyABayEMCyAHQfAAaiQAIAwLkRcCFn8FfiMAQdABayIHJAAgByAAKALw4QEiCDYCvAEgASACaiESIAggACgCgOIBaiETAkACQCAFRQRAIAEhAwwBCyAAKALE4AEhESAAKALA4AEhFSAAKAK84AEhDyAAQQE2AozhAUEAIQgDQCAIQQNHBEAgByAIQQJ0IgJqIAAgAmpBrNABaigCADYCVCAIQQFqIQgMAQsLIAcgETYCZCAHIA82AmAgByABIA9rNgJoQWwhECAHQShqIAMgBBAGEAMNASAFQQQgBUEESBshFyAHQTxqIAdBKGogACgCABATIAdBxABqIAdBKGogACgCCBATIAdBzABqIAdBKGogACgCBBATQQAhBCAHQeAAaiEMIAdB5ABqIQoDQCAHQShqEARBAksgBCAXTnJFBEAgBygCQCAHKAI8QQN0aikCACIdQhCIp0H/AXEhCyAHKAJQIAcoAkxBA3RqKQIAIh5CEIinQf8BcSEJIAcoAkggBygCREEDdGopAgAiH0IgiKchCCAeQiCIISAgHUIgiKchAgJAIB9CEIinQf8BcSIDQQJPBEACQCAGRSADQRlJckUEQCAIIAdBKGogA0EgIAcoAixrIg0gDSADSxsiDRAFIAMgDWsiA3RqIQggB0EoahAEGiADRQ0BIAdBKGogAxAFIAhqIQgMAQsgB0EoaiADEAUgCGohCCAHQShqEAQaCyAHKQJUISEgByAINgJUIAcgITcDWAwBCwJAIANFBEAgAgRAIAcoAlQhCAwDCyAHKAJYIQgMAQsCQAJAIAdBKGpBARAFIAggAkVqaiIDQQNGBEAgBygCVEF/aiIDIANFaiEIDAELIANBAnQgB2ooAlQiCCAIRWohCCADQQFGDQELIAcgBygCWDYCXAsLIAcgBygCVDYCWCAHIAg2AlQLICCnIQMgCQRAIAdBKGogCRAFIANqIQMLIAkgC2pBFE8EQCAHQShqEAQaCyALBEAgB0EoaiALEAUgAmohAgsgB0EoahAEGiAHIAcoAmggAmoiCSADajYCaCAKIAwgCCAJSxsoAgAhDSAHIAdBKGogHUIYiKdB/wFxEAggHadB//8DcWo2AjwgByAHQShqIB5CGIinQf8BcRAIIB6nQf//A3FqNgJMIAdBKGoQBBogB0EoaiAfQhiIp0H/AXEQCCEOIAdB8ABqIARBBHRqIgsgCSANaiAIazYCDCALIAg2AgggCyADNgIEIAsgAjYCACAHIA4gH6dB//8DcWo2AkQgBEEBaiEEDAELCyAEIBdIDQEgEkFgaiEYIAdB4ABqIRogB0HkAGohGyABIQMDQCAHQShqEARBAksgBCAFTnJFBEAgBygCQCAHKAI8QQN0aikCACIdQhCIp0H/AXEhCyAHKAJQIAcoAkxBA3RqKQIAIh5CEIinQf8BcSEIIAcoAkggBygCREEDdGopAgAiH0IgiKchCSAeQiCIISAgHUIgiKchDAJAIB9CEIinQf8BcSICQQJPBEACQCAGRSACQRlJckUEQCAJIAdBKGogAkEgIAcoAixrIgogCiACSxsiChAFIAIgCmsiAnRqIQkgB0EoahAEGiACRQ0BIAdBKGogAhAFIAlqIQkMAQsgB0EoaiACEAUgCWohCSAHQShqEAQaCyAHKQJUISEgByAJNgJUIAcgITcDWAwBCwJAIAJFBEAgDARAIAcoAlQhCQwDCyAHKAJYIQkMAQsCQAJAIAdBKGpBARAFIAkgDEVqaiICQQNGBEAgBygCVEF/aiICIAJFaiEJDAELIAJBAnQgB2ooAlQiCSAJRWohCSACQQFGDQELIAcgBygCWDYCXAsLIAcgBygCVDYCWCAHIAk2AlQLICCnIRQgCARAIAdBKGogCBAFIBRqIRQLIAggC2pBFE8EQCAHQShqEAQaCyALBEAgB0EoaiALEAUgDGohDAsgB0EoahAEGiAHIAcoAmggDGoiGSAUajYCaCAbIBogCSAZSxsoAgAhHCAHIAdBKGogHUIYiKdB/wFxEAggHadB//8DcWo2AjwgByAHQShqIB5CGIinQf8BcRAIIB6nQf//A3FqNgJMIAdBKGoQBBogByAHQShqIB9CGIinQf8BcRAIIB+nQf//A3FqNgJEIAcgB0HwAGogBEEDcUEEdGoiDSkDCCIdNwPIASAHIA0pAwAiHjcDwAECQAJAAkAgBygCvAEiDiAepyICaiIWIBNLDQAgAyAHKALEASIKIAJqIgtqIBhLDQAgEiADayALQSBqTw0BCyAHIAcpA8gBNwMQIAcgBykDwAE3AwggAyASIAdBCGogB0G8AWogEyAPIBUgERAeIQsMAQsgAiADaiEIIAMgDhAHIAJBEU8EQCADQRBqIQIDQCACIA5BEGoiDhAHIAJBEGoiAiAISQ0ACwsgCCAdpyIOayECIAcgFjYCvAEgDiAIIA9rSwRAIA4gCCAVa0sEQEFsIQsMAgsgESACIA9rIgJqIhYgCmogEU0EQCAIIBYgChAPGgwCCyAIIBZBACACaxAPIQggByACIApqIgo2AsQBIAggAmshCCAPIQILIA5BEE8EQCAIIApqIQoDQCAIIAIQByACQRBqIQIgCEEQaiIIIApJDQALDAELAkAgDkEHTQRAIAggAi0AADoAACAIIAItAAE6AAEgCCACLQACOgACIAggAi0AAzoAAyAIQQRqIAIgDkECdCIKQcAeaigCAGoiAhAXIAIgCkHgHmooAgBrIQIgBygCxAEhCgwBCyAIIAIQDAsgCkEJSQ0AIAggCmohCiAIQQhqIgggAkEIaiICa0EPTARAA0AgCCACEAwgAkEIaiECIAhBCGoiCCAKSQ0ADAIACwALA0AgCCACEAcgAkEQaiECIAhBEGoiCCAKSQ0ACwsgCxADBEAgCyEQDAQFIA0gDDYCACANIBkgHGogCWs2AgwgDSAJNgIIIA0gFDYCBCAEQQFqIQQgAyALaiEDDAILAAsLIAQgBUgNASAEIBdrIQtBACEEA0AgCyAFSARAIAcgB0HwAGogC0EDcUEEdGoiAikDCCIdNwPIASAHIAIpAwAiHjcDwAECQAJAAkAgBygCvAEiDCAepyICaiIKIBNLDQAgAyAHKALEASIJIAJqIhBqIBhLDQAgEiADayAQQSBqTw0BCyAHIAcpA8gBNwMgIAcgBykDwAE3AxggAyASIAdBGGogB0G8AWogEyAPIBUgERAeIRAMAQsgAiADaiEIIAMgDBAHIAJBEU8EQCADQRBqIQIDQCACIAxBEGoiDBAHIAJBEGoiAiAISQ0ACwsgCCAdpyIGayECIAcgCjYCvAEgBiAIIA9rSwRAIAYgCCAVa0sEQEFsIRAMAgsgESACIA9rIgJqIgwgCWogEU0EQCAIIAwgCRAPGgwCCyAIIAxBACACaxAPIQggByACIAlqIgk2AsQBIAggAmshCCAPIQILIAZBEE8EQCAIIAlqIQYDQCAIIAIQByACQRBqIQIgCEEQaiIIIAZJDQALDAELAkAgBkEHTQRAIAggAi0AADoAACAIIAItAAE6AAEgCCACLQACOgACIAggAi0AAzoAAyAIQQRqIAIgBkECdCIGQcAeaigCAGoiAhAXIAIgBkHgHmooAgBrIQIgBygCxAEhCQwBCyAIIAIQDAsgCUEJSQ0AIAggCWohBiAIQQhqIgggAkEIaiICa0EPTARAA0AgCCACEAwgAkEIaiECIAhBCGoiCCAGSQ0ADAIACwALA0AgCCACEAcgAkEQaiECIAhBEGoiCCAGSQ0ACwsgEBADDQMgC0EBaiELIAMgEGohAwwBCwsDQCAEQQNHBEAgACAEQQJ0IgJqQazQAWogAiAHaigCVDYCACAEQQFqIQQMAQsLIAcoArwBIQgLQbp/IRAgEyAIayIAIBIgA2tLDQAgAwR/IAMgCCAAEAsgAGoFQQALIAFrIRALIAdB0AFqJAAgEAslACAAQgA3AgAgAEEAOwEIIABBADoACyAAIAE2AgwgACACOgAKC7QFAQN/IwBBMGsiBCQAIABB/wFqIgVBfWohBgJAIAMvAQIEQCAEQRhqIAEgAhAGIgIQAw0BIARBEGogBEEYaiADEBwgBEEIaiAEQRhqIAMQHCAAIQMDQAJAIARBGGoQBCADIAZPckUEQCADIARBEGogBEEYahASOgAAIAMgBEEIaiAEQRhqEBI6AAEgBEEYahAERQ0BIANBAmohAwsgBUF+aiEFAn8DQEG6fyECIAMiASAFSw0FIAEgBEEQaiAEQRhqEBI6AAAgAUEBaiEDIARBGGoQBEEDRgRAQQIhAiAEQQhqDAILIAMgBUsNBSABIARBCGogBEEYahASOgABIAFBAmohA0EDIQIgBEEYahAEQQNHDQALIARBEGoLIQUgAyAFIARBGGoQEjoAACABIAJqIABrIQIMAwsgAyAEQRBqIARBGGoQEjoAAiADIARBCGogBEEYahASOgADIANBBGohAwwAAAsACyAEQRhqIAEgAhAGIgIQAw0AIARBEGogBEEYaiADEBwgBEEIaiAEQRhqIAMQHCAAIQMDQAJAIARBGGoQBCADIAZPckUEQCADIARBEGogBEEYahAROgAAIAMgBEEIaiAEQRhqEBE6AAEgBEEYahAERQ0BIANBAmohAwsgBUF+aiEFAn8DQEG6fyECIAMiASAFSw0EIAEgBEEQaiAEQRhqEBE6AAAgAUEBaiEDIARBGGoQBEEDRgRAQQIhAiAEQQhqDAILIAMgBUsNBCABIARBCGogBEEYahAROgABIAFBAmohA0EDIQIgBEEYahAEQQNHDQALIARBEGoLIQUgAyAFIARBGGoQEToAACABIAJqIABrIQIMAgsgAyAEQRBqIARBGGoQEToAAiADIARBCGogBEEYahAROgADIANBBGohAwwAAAsACyAEQTBqJAAgAgtpAQF/An8CQAJAIAJBB00NACABKAAAQbfIwuF+Rw0AIAAgASgABDYCmOIBQWIgAEEQaiABIAIQPiIDEAMNAhogAEKBgICAEDcDiOEBIAAgASADaiACIANrECoMAQsgACABIAIQKgtBAAsLrQMBBn8jAEGAAWsiAyQAQWIhCAJAIAJBCUkNACAAQZjQAGogAUEIaiIEIAJBeGogAEGY0AAQMyIFEAMiBg0AIANBHzYCfCADIANB/ABqIANB+ABqIAQgBCAFaiAGGyIEIAEgAmoiAiAEaxAVIgUQAw0AIAMoAnwiBkEfSw0AIAMoAngiB0EJTw0AIABBiCBqIAMgBkGAC0GADCAHEBggA0E0NgJ8IAMgA0H8AGogA0H4AGogBCAFaiIEIAIgBGsQFSIFEAMNACADKAJ8IgZBNEsNACADKAJ4IgdBCk8NACAAQZAwaiADIAZBgA1B4A4gBxAYIANBIzYCfCADIANB/ABqIANB+ABqIAQgBWoiBCACIARrEBUiBRADDQAgAygCfCIGQSNLDQAgAygCeCIHQQpPDQAgACADIAZBwBBB0BEgBxAYIAQgBWoiBEEMaiIFIAJLDQAgAiAFayEFQQAhAgNAIAJBA0cEQCAEKAAAIgZBf2ogBU8NAiAAIAJBAnRqQZzQAWogBjYCACACQQFqIQIgBEEEaiEEDAELCyAEIAFrIQgLIANBgAFqJAAgCAtGAQN/IABBCGohAyAAKAIEIQJBACEAA0AgACACdkUEQCABIAMgAEEDdGotAAJBFktqIQEgAEEBaiEADAELCyABQQggAmt0C4YDAQV/Qbh/IQcCQCADRQ0AIAItAAAiBEUEQCABQQA2AgBBAUG4fyADQQFGGw8LAn8gAkEBaiIFIARBGHRBGHUiBkF/Sg0AGiAGQX9GBEAgA0EDSA0CIAUvAABBgP4BaiEEIAJBA2oMAQsgA0ECSA0BIAItAAEgBEEIdHJBgIB+aiEEIAJBAmoLIQUgASAENgIAIAVBAWoiASACIANqIgNLDQBBbCEHIABBEGogACAFLQAAIgVBBnZBI0EJIAEgAyABa0HAEEHQEUHwEiAAKAKM4QEgACgCnOIBIAQQHyIGEAMiCA0AIABBmCBqIABBCGogBUEEdkEDcUEfQQggASABIAZqIAgbIgEgAyABa0GAC0GADEGAFyAAKAKM4QEgACgCnOIBIAQQHyIGEAMiCA0AIABBoDBqIABBBGogBUECdkEDcUE0QQkgASABIAZqIAgbIgEgAyABa0GADUHgDkGQGSAAKAKM4QEgACgCnOIBIAQQHyIAEAMNACAAIAFqIAJrIQcLIAcLrQMBCn8jAEGABGsiCCQAAn9BUiACQf8BSw0AGkFUIANBDEsNABogAkEBaiELIABBBGohCUGAgAQgA0F/anRBEHUhCkEAIQJBASEEQQEgA3QiB0F/aiIMIQUDQCACIAtGRQRAAkAgASACQQF0Ig1qLwEAIgZB//8DRgRAIAkgBUECdGogAjoAAiAFQX9qIQVBASEGDAELIARBACAKIAZBEHRBEHVKGyEECyAIIA1qIAY7AQAgAkEBaiECDAELCyAAIAQ7AQIgACADOwEAIAdBA3YgB0EBdmpBA2ohBkEAIQRBACECA0AgBCALRkUEQCABIARBAXRqLgEAIQpBACEAA0AgACAKTkUEQCAJIAJBAnRqIAQ6AAIDQCACIAZqIAxxIgIgBUsNAAsgAEEBaiEADAELCyAEQQFqIQQMAQsLQX8gAg0AGkEAIQIDfyACIAdGBH9BAAUgCCAJIAJBAnRqIgAtAAJBAXRqIgEgAS8BACIBQQFqOwEAIAAgAyABEBRrIgU6AAMgACABIAVB/wFxdCAHazsBACACQQFqIQIMAQsLCyEFIAhBgARqJAAgBQvjBgEIf0FsIQcCQCACQQNJDQACQAJAAkACQCABLQAAIgNBA3EiCUEBaw4DAwEAAgsgACgCiOEBDQBBYg8LIAJBBUkNAkEDIQYgASgAACEFAn8CQAJAIANBAnZBA3EiCEF+aiIEQQFNBEAgBEEBaw0BDAILIAVBDnZB/wdxIQQgBUEEdkH/B3EhAyAIRQwCCyAFQRJ2IQRBBCEGIAVBBHZB//8AcSEDQQAMAQsgBUEEdkH//w9xIgNBgIAISw0DIAEtAARBCnQgBUEWdnIhBEEFIQZBAAshBSAEIAZqIgogAksNAgJAIANBgQZJDQAgACgCnOIBRQ0AQQAhAgNAIAJBg4ABSw0BIAJBQGshAgwAAAsACwJ/IAlBA0YEQCABIAZqIQEgAEHw4gFqIQIgACgCDCEGIAUEQCACIAMgASAEIAYQXwwCCyACIAMgASAEIAYQXQwBCyAAQbjQAWohAiABIAZqIQEgAEHw4gFqIQYgAEGo0ABqIQggBQRAIAggBiADIAEgBCACEF4MAQsgCCAGIAMgASAEIAIQXAsQAw0CIAAgAzYCgOIBIABBATYCiOEBIAAgAEHw4gFqNgLw4QEgCUECRgRAIAAgAEGo0ABqNgIMCyAAIANqIgBBiOMBakIANwAAIABBgOMBakIANwAAIABB+OIBakIANwAAIABB8OIBakIANwAAIAoPCwJ/AkACQAJAIANBAnZBA3FBf2oiBEECSw0AIARBAWsOAgACAQtBASEEIANBA3YMAgtBAiEEIAEvAABBBHYMAQtBAyEEIAEQIUEEdgsiAyAEaiIFQSBqIAJLBEAgBSACSw0CIABB8OIBaiABIARqIAMQCyEBIAAgAzYCgOIBIAAgATYC8OEBIAEgA2oiAEIANwAYIABCADcAECAAQgA3AAggAEIANwAAIAUPCyAAIAM2AoDiASAAIAEgBGo2AvDhASAFDwsCfwJAAkACQCADQQJ2QQNxQX9qIgRBAksNACAEQQFrDgIAAgELQQEhByADQQN2DAILQQIhByABLwAAQQR2DAELIAJBBEkgARAhIgJBj4CAAUtyDQFBAyEHIAJBBHYLIQIgAEHw4gFqIAEgB2otAAAgAkEgahAQIQEgACACNgKA4gEgACABNgLw4QEgB0EBaiEHCyAHC0sAIABC+erQ0OfJoeThADcDICAAQgA3AxggAELP1tO+0ser2UI3AxAgAELW64Lu6v2J9eAANwMIIABCADcDACAAQShqQQBBKBAQGgviAgICfwV+IABBKGoiASAAKAJIaiECAn4gACkDACIDQiBaBEAgACkDECIEQgeJIAApAwgiBUIBiXwgACkDGCIGQgyJfCAAKQMgIgdCEol8IAUQGSAEEBkgBhAZIAcQGQwBCyAAKQMYQsXP2bLx5brqJ3wLIAN8IQMDQCABQQhqIgAgAk0EQEIAIAEpAAAQCSADhUIbiUKHla+vmLbem55/fkLj3MqV/M7y9YV/fCEDIAAhAQwBCwsCQCABQQRqIgAgAksEQCABIQAMAQsgASgAAK1Ch5Wvr5i23puef34gA4VCF4lCz9bTvtLHq9lCfkL5893xmfaZqxZ8IQMLA0AgACACSQRAIAAxAABCxc/ZsvHluuonfiADhUILiUKHla+vmLbem55/fiEDIABBAWohAAwBCwsgA0IhiCADhULP1tO+0ser2UJ+IgNCHYggA4VC+fPd8Zn2masWfiIDQiCIIAOFC+8CAgJ/BH4gACAAKQMAIAKtfDcDAAJAAkAgACgCSCIDIAJqIgRBH00EQCABRQ0BIAAgA2pBKGogASACECAgACgCSCACaiEEDAELIAEgAmohAgJ/IAMEQCAAQShqIgQgA2ogAUEgIANrECAgACAAKQMIIAQpAAAQCTcDCCAAIAApAxAgACkAMBAJNwMQIAAgACkDGCAAKQA4EAk3AxggACAAKQMgIABBQGspAAAQCTcDICAAKAJIIQMgAEEANgJIIAEgA2tBIGohAQsgAUEgaiACTQsEQCACQWBqIQMgACkDICEFIAApAxghBiAAKQMQIQcgACkDCCEIA0AgCCABKQAAEAkhCCAHIAEpAAgQCSEHIAYgASkAEBAJIQYgBSABKQAYEAkhBSABQSBqIgEgA00NAAsgACAFNwMgIAAgBjcDGCAAIAc3AxAgACAINwMICyABIAJPDQEgAEEoaiABIAIgAWsiBBAgCyAAIAQ2AkgLCy8BAX8gAEUEQEG2f0EAIAMbDwtBun8hBCADIAFNBH8gACACIAMQEBogAwVBun8LCy8BAX8gAEUEQEG2f0EAIAMbDwtBun8hBCADIAFNBH8gACACIAMQCxogAwVBun8LC6gCAQZ/IwBBEGsiByQAIABB2OABaikDAEKAgIAQViEIQbh/IQUCQCAEQf//B0sNACAAIAMgBBBCIgUQAyIGDQAgACgCnOIBIQkgACAHQQxqIAMgAyAFaiAGGyIKIARBACAFIAYbayIGEEAiAxADBEAgAyEFDAELIAcoAgwhBCABRQRAQbp/IQUgBEEASg0BCyAGIANrIQUgAyAKaiEDAkAgCQRAIABBADYCnOIBDAELAkACQAJAIARBBUgNACAAQdjgAWopAwBCgICACFgNAAwBCyAAQQA2ApziAQwBCyAAKAIIED8hBiAAQQA2ApziASAGQRRPDQELIAAgASACIAMgBSAEIAgQOSEFDAELIAAgASACIAMgBSAEIAgQOiEFCyAHQRBqJAAgBQtnACAAQdDgAWogASACIAAoAuzhARAuIgEQAwRAIAEPC0G4fyECAkAgAQ0AIABB7OABaigCACIBBEBBYCECIAAoApjiASABRw0BC0EAIQIgAEHw4AFqKAIARQ0AIABBkOEBahBDCyACCycBAX8QVyIERQRAQUAPCyAEIAAgASACIAMgBBBLEE8hACAEEFYgAAs/AQF/AkACQAJAIAAoAqDiAUEBaiIBQQJLDQAgAUEBaw4CAAECCyAAEDBBAA8LIABBADYCoOIBCyAAKAKU4gELvAMCB38BfiMAQRBrIgkkAEG4fyEGAkAgBCgCACIIQQVBCSAAKALs4QEiBRtJDQAgAygCACIHQQFBBSAFGyAFEC8iBRADBEAgBSEGDAELIAggBUEDakkNACAAIAcgBRBJIgYQAw0AIAEgAmohCiAAQZDhAWohCyAIIAVrIQIgBSAHaiEHIAEhBQNAIAcgAiAJECwiBhADDQEgAkF9aiICIAZJBEBBuH8hBgwCCyAJKAIAIghBAksEQEFsIQYMAgsgB0EDaiEHAn8CQAJAAkAgCEEBaw4CAgABCyAAIAUgCiAFayAHIAYQSAwCCyAFIAogBWsgByAGEEcMAQsgBSAKIAVrIActAAAgCSgCCBBGCyIIEAMEQCAIIQYMAgsgACgC8OABBEAgCyAFIAgQRQsgAiAGayECIAYgB2ohByAFIAhqIQUgCSgCBEUNAAsgACkD0OABIgxCf1IEQEFsIQYgDCAFIAFrrFINAQsgACgC8OABBEBBaiEGIAJBBEkNASALEEQhDCAHKAAAIAynRw0BIAdBBGohByACQXxqIQILIAMgBzYCACAEIAI2AgAgBSABayEGCyAJQRBqJAAgBgsuACAAECsCf0EAQQAQAw0AGiABRSACRXJFBEBBYiAAIAEgAhA9EAMNARoLQQALCzcAIAEEQCAAIAAoAsTgASABKAIEIAEoAghqRzYCnOIBCyAAECtBABADIAFFckUEQCAAIAEQWwsL0QIBB38jAEEQayIGJAAgBiAENgIIIAYgAzYCDCAFBEAgBSgCBCEKIAUoAgghCQsgASEIAkACQANAIAAoAuzhARAWIQsCQANAIAQgC0kNASADKAAAQXBxQdDUtMIBRgRAIAMgBBAiIgcQAw0EIAQgB2shBCADIAdqIQMMAQsLIAYgAzYCDCAGIAQ2AggCQCAFBEAgACAFEE5BACEHQQAQA0UNAQwFCyAAIAogCRBNIgcQAw0ECyAAIAgQUCAMQQFHQQAgACAIIAIgBkEMaiAGQQhqEEwiByIDa0EAIAMQAxtBCkdyRQRAQbh/IQcMBAsgBxADDQMgAiAHayECIAcgCGohCEEBIQwgBigCDCEDIAYoAgghBAwBCwsgBiADNgIMIAYgBDYCCEG4fyEHIAQNASAIIAFrIQcMAQsgBiADNgIMIAYgBDYCCAsgBkEQaiQAIAcLRgECfyABIAAoArjgASICRwRAIAAgAjYCxOABIAAgATYCuOABIAAoArzgASEDIAAgATYCvOABIAAgASADIAJrajYCwOABCwutAgIEfwF+IwBBQGoiBCQAAkACQCACQQhJDQAgASgAAEFwcUHQ1LTCAUcNACABIAIQIiEBIABCADcDCCAAQQA2AgQgACABNgIADAELIARBGGogASACEC0iAxADBEAgACADEBoMAQsgAwRAIABBuH8QGgwBCyACIAQoAjAiA2shAiABIANqIQMDQAJAIAAgAyACIARBCGoQLCIFEAMEfyAFBSACIAVBA2oiBU8NAUG4fwsQGgwCCyAGQQFqIQYgAiAFayECIAMgBWohAyAEKAIMRQ0ACyAEKAI4BEAgAkEDTQRAIABBuH8QGgwCCyADQQRqIQMLIAQoAighAiAEKQMYIQcgAEEANgIEIAAgAyABazYCACAAIAIgBmytIAcgB0J/URs3AwgLIARBQGskAAslAQF/IwBBEGsiAiQAIAIgACABEFEgAigCACEAIAJBEGokACAAC30BBH8jAEGQBGsiBCQAIARB/wE2AggCQCAEQRBqIARBCGogBEEMaiABIAIQFSIGEAMEQCAGIQUMAQtBVCEFIAQoAgwiB0EGSw0AIAMgBEEQaiAEKAIIIAcQQSIFEAMNACAAIAEgBmogAiAGayADEDwhBQsgBEGQBGokACAFC4cBAgJ/An5BABAWIQMCQANAIAEgA08EQAJAIAAoAABBcHFB0NS0wgFGBEAgACABECIiAhADRQ0BQn4PCyAAIAEQVSIEQn1WDQMgBCAFfCIFIARUIQJCfiEEIAINAyAAIAEQUiICEAMNAwsgASACayEBIAAgAmohAAwBCwtCfiAFIAEbIQQLIAQLPwIBfwF+IwBBMGsiAiQAAn5CfiACQQhqIAAgARAtDQAaQgAgAigCHEEBRg0AGiACKQMICyEDIAJBMGokACADC40BAQJ/IwBBMGsiASQAAkAgAEUNACAAKAKI4gENACABIABB/OEBaigCADYCKCABIAApAvThATcDICAAEDAgACgCqOIBIQIgASABKAIoNgIYIAEgASkDIDcDECACIAFBEGoQGyAAQQA2AqjiASABIAEoAig2AgggASABKQMgNwMAIAAgARAbCyABQTBqJAALKgECfyMAQRBrIgAkACAAQQA2AgggAEIANwMAIAAQWCEBIABBEGokACABC4cBAQN/IwBBEGsiAiQAAkAgACgCAEUgACgCBEVzDQAgAiAAKAIINgIIIAIgACkCADcDAAJ/IAIoAgAiAQRAIAIoAghBqOMJIAERBQAMAQtBqOMJECgLIgFFDQAgASAAKQIANwL04QEgAUH84QFqIAAoAgg2AgAgARBZIAEhAwsgAkEQaiQAIAMLywEBAn8jAEEgayIBJAAgAEGBgIDAADYCtOIBIABBADYCiOIBIABBADYC7OEBIABCADcDkOIBIABBADYCpOMJIABBADYC3OIBIABCADcCzOIBIABBADYCvOIBIABBADYCxOABIABCADcCnOIBIABBpOIBakIANwIAIABBrOIBakEANgIAIAFCADcCECABQgA3AhggASABKQMYNwMIIAEgASkDEDcDACABKAIIQQh2QQFxIQIgAEEANgLg4gEgACACNgKM4gEgAUEgaiQAC3YBA38jAEEwayIBJAAgAARAIAEgAEHE0AFqIgIoAgA2AiggASAAKQK80AE3AyAgACgCACEDIAEgAigCADYCGCABIAApArzQATcDECADIAFBEGoQGyABIAEoAig2AgggASABKQMgNwMAIAAgARAbCyABQTBqJAALzAEBAX8gACABKAK00AE2ApjiASAAIAEoAgQiAjYCwOABIAAgAjYCvOABIAAgAiABKAIIaiICNgK44AEgACACNgLE4AEgASgCuNABBEAgAEKBgICAEDcDiOEBIAAgAUGk0ABqNgIMIAAgAUGUIGo2AgggACABQZwwajYCBCAAIAFBDGo2AgAgAEGs0AFqIAFBqNABaigCADYCACAAQbDQAWogAUGs0AFqKAIANgIAIABBtNABaiABQbDQAWooAgA2AgAPCyAAQgA3A4jhAQs7ACACRQRAQbp/DwsgBEUEQEFsDwsgAiAEEGAEQCAAIAEgAiADIAQgBRBhDwsgACABIAIgAyAEIAUQZQtGAQF/IwBBEGsiBSQAIAVBCGogBBAOAn8gBS0ACQRAIAAgASACIAMgBBAyDAELIAAgASACIAMgBBA0CyEAIAVBEGokACAACzQAIAAgAyAEIAUQNiIFEAMEQCAFDwsgBSAESQR/IAEgAiADIAVqIAQgBWsgABA1BUG4fwsLRgEBfyMAQRBrIgUkACAFQQhqIAQQDgJ/IAUtAAkEQCAAIAEgAiADIAQQYgwBCyAAIAEgAiADIAQQNQshACAFQRBqJAAgAAtZAQF/QQ8hAiABIABJBEAgAUEEdCAAbiECCyAAQQh2IgEgAkEYbCIAQYwIaigCAGwgAEGICGooAgBqIgJBA3YgAmogAEGACGooAgAgAEGECGooAgAgAWxqSQs3ACAAIAMgBCAFQYAQEDMiBRADBEAgBQ8LIAUgBEkEfyABIAIgAyAFaiAEIAVrIAAQMgVBuH8LC78DAQN/IwBBIGsiBSQAIAVBCGogAiADEAYiAhADRQRAIAAgAWoiB0F9aiEGIAUgBBAOIARBBGohAiAFLQACIQMDQEEAIAAgBkkgBUEIahAEGwRAIAAgAiAFQQhqIAMQAkECdGoiBC8BADsAACAFQQhqIAQtAAIQASAAIAQtAANqIgQgAiAFQQhqIAMQAkECdGoiAC8BADsAACAFQQhqIAAtAAIQASAEIAAtAANqIQAMAQUgB0F+aiEEA0AgBUEIahAEIAAgBEtyRQRAIAAgAiAFQQhqIAMQAkECdGoiBi8BADsAACAFQQhqIAYtAAIQASAAIAYtAANqIQAMAQsLA0AgACAES0UEQCAAIAIgBUEIaiADEAJBAnRqIgYvAQA7AAAgBUEIaiAGLQACEAEgACAGLQADaiEADAELCwJAIAAgB08NACAAIAIgBUEIaiADEAIiA0ECdGoiAC0AADoAACAALQADQQFGBEAgBUEIaiAALQACEAEMAQsgBSgCDEEfSw0AIAVBCGogAiADQQJ0ai0AAhABIAUoAgxBIUkNACAFQSA2AgwLIAFBbCAFQQhqEAobIQILCwsgBUEgaiQAIAILkgIBBH8jAEFAaiIJJAAgCSADQTQQCyEDAkAgBEECSA0AIAMgBEECdGooAgAhCSADQTxqIAgQIyADQQE6AD8gAyACOgA+QQAhBCADKAI8IQoDQCAEIAlGDQEgACAEQQJ0aiAKNgEAIARBAWohBAwAAAsAC0EAIQkDQCAGIAlGRQRAIAMgBSAJQQF0aiIKLQABIgtBAnRqIgwoAgAhBCADQTxqIAotAABBCHQgCGpB//8DcRAjIANBAjoAPyADIAcgC2siCiACajoAPiAEQQEgASAKa3RqIQogAygCPCELA0AgACAEQQJ0aiALNgEAIARBAWoiBCAKSQ0ACyAMIAo2AgAgCUEBaiEJDAELCyADQUBrJAALowIBCX8jAEHQAGsiCSQAIAlBEGogBUE0EAsaIAcgBmshDyAHIAFrIRADQAJAIAMgCkcEQEEBIAEgByACIApBAXRqIgYtAAEiDGsiCGsiC3QhDSAGLQAAIQ4gCUEQaiAMQQJ0aiIMKAIAIQYgCyAPTwRAIAAgBkECdGogCyAIIAUgCEE0bGogCCAQaiIIQQEgCEEBShsiCCACIAQgCEECdGooAgAiCEEBdGogAyAIayAHIA4QYyAGIA1qIQgMAgsgCUEMaiAOECMgCUEBOgAPIAkgCDoADiAGIA1qIQggCSgCDCELA0AgBiAITw0CIAAgBkECdGogCzYBACAGQQFqIQYMAAALAAsgCUHQAGokAA8LIAwgCDYCACAKQQFqIQoMAAALAAs0ACAAIAMgBCAFEDYiBRADBEAgBQ8LIAUgBEkEfyABIAIgAyAFaiAEIAVrIAAQNAVBuH8LCyMAIAA/AEEQdGtB//8DakEQdkAAQX9GBEBBAA8LQQAQAEEBCzsBAX8gAgRAA0AgACABIAJBgCAgAkGAIEkbIgMQCyEAIAFBgCBqIQEgAEGAIGohACACIANrIgINAAsLCwYAIAAQAwsLqBUJAEGICAsNAQAAAAEAAAACAAAAAgBBoAgLswYBAAAAAQAAAAIAAAACAAAAJgAAAIIAAAAhBQAASgAAAGcIAAAmAAAAwAEAAIAAAABJBQAASgAAAL4IAAApAAAALAIAAIAAAABJBQAASgAAAL4IAAAvAAAAygIAAIAAAACKBQAASgAAAIQJAAA1AAAAcwMAAIAAAACdBQAASgAAAKAJAAA9AAAAgQMAAIAAAADrBQAASwAAAD4KAABEAAAAngMAAIAAAABNBgAASwAAAKoKAABLAAAAswMAAIAAAADBBgAATQAAAB8NAABNAAAAUwQAAIAAAAAjCAAAUQAAAKYPAABUAAAAmQQAAIAAAABLCQAAVwAAALESAABYAAAA2gQAAIAAAABvCQAAXQAAACMUAABUAAAARQUAAIAAAABUCgAAagAAAIwUAABqAAAArwUAAIAAAAB2CQAAfAAAAE4QAAB8AAAA0gIAAIAAAABjBwAAkQAAAJAHAACSAAAAAAAAAAEAAAABAAAABQAAAA0AAAAdAAAAPQAAAH0AAAD9AAAA/QEAAP0DAAD9BwAA/Q8AAP0fAAD9PwAA/X8AAP3/AAD9/wEA/f8DAP3/BwD9/w8A/f8fAP3/PwD9/38A/f//AP3//wH9//8D/f//B/3//w/9//8f/f//P/3//38AAAAAAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEAAAABEAAAASAAAAEwAAABQAAAAVAAAAFgAAABcAAAAYAAAAGQAAABoAAAAbAAAAHAAAAB0AAAAeAAAAHwAAAAMAAAAEAAAABQAAAAYAAAAHAAAACAAAAAkAAAAKAAAACwAAAAwAAAANAAAADgAAAA8AAAAQAAAAEQAAABIAAAATAAAAFAAAABUAAAAWAAAAFwAAABgAAAAZAAAAGgAAABsAAAAcAAAAHQAAAB4AAAAfAAAAIAAAACEAAAAiAAAAIwAAACUAAAAnAAAAKQAAACsAAAAvAAAAMwAAADsAAABDAAAAUwAAAGMAAACDAAAAAwEAAAMCAAADBAAAAwgAAAMQAAADIAAAA0AAAAOAAAADAAEAQeAPC1EBAAAAAQAAAAEAAAABAAAAAgAAAAIAAAADAAAAAwAAAAQAAAAEAAAABQAAAAcAAAAIAAAACQAAAAoAAAALAAAADAAAAA0AAAAOAAAADwAAABAAQcQQC4sBAQAAAAIAAAADAAAABAAAAAUAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEAAAABIAAAAUAAAAFgAAABgAAAAcAAAAIAAAACgAAAAwAAAAQAAAAIAAAAAAAQAAAAIAAAAEAAAACAAAABAAAAAgAAAAQAAAAIAAAAAAAQBBkBIL5gQBAAAAAQAAAAEAAAABAAAAAgAAAAIAAAADAAAAAwAAAAQAAAAGAAAABwAAAAgAAAAJAAAACgAAAAsAAAAMAAAADQAAAA4AAAAPAAAAEAAAAAEAAAAEAAAACAAAAAAAAAABAAEBBgAAAAAAAAQAAAAAEAAABAAAAAAgAAAFAQAAAAAAAAUDAAAAAAAABQQAAAAAAAAFBgAAAAAAAAUHAAAAAAAABQkAAAAAAAAFCgAAAAAAAAUMAAAAAAAABg4AAAAAAAEFEAAAAAAAAQUUAAAAAAABBRYAAAAAAAIFHAAAAAAAAwUgAAAAAAAEBTAAAAAgAAYFQAAAAAAABwWAAAAAAAAIBgABAAAAAAoGAAQAAAAADAYAEAAAIAAABAAAAAAAAAAEAQAAAAAAAAUCAAAAIAAABQQAAAAAAAAFBQAAACAAAAUHAAAAAAAABQgAAAAgAAAFCgAAAAAAAAULAAAAAAAABg0AAAAgAAEFEAAAAAAAAQUSAAAAIAABBRYAAAAAAAIFGAAAACAAAwUgAAAAAAADBSgAAAAAAAYEQAAAABAABgRAAAAAIAAHBYAAAAAAAAkGAAIAAAAACwYACAAAMAAABAAAAAAQAAAEAQAAACAAAAUCAAAAIAAABQMAAAAgAAAFBQAAACAAAAUGAAAAIAAABQgAAAAgAAAFCQAAACAAAAULAAAAIAAABQwAAAAAAAAGDwAAACAAAQUSAAAAIAABBRQAAAAgAAIFGAAAACAAAgUcAAAAIAADBSgAAAAgAAQFMAAAAAAAEAYAAAEAAAAPBgCAAAAAAA4GAEAAAAAADQYAIABBgBcLhwIBAAEBBQAAAAAAAAUAAAAAAAAGBD0AAAAAAAkF/QEAAAAADwX9fwAAAAAVBf3/HwAAAAMFBQAAAAAABwR9AAAAAAAMBf0PAAAAABIF/f8DAAAAFwX9/38AAAAFBR0AAAAAAAgE/QAAAAAADgX9PwAAAAAUBf3/DwAAAAIFAQAAABAABwR9AAAAAAALBf0HAAAAABEF/f8BAAAAFgX9/z8AAAAEBQ0AAAAQAAgE/QAAAAAADQX9HwAAAAATBf3/BwAAAAEFAQAAABAABgQ9AAAAAAAKBf0DAAAAABAF/f8AAAAAHAX9//8PAAAbBf3//wcAABoF/f//AwAAGQX9//8BAAAYBf3//wBBkBkLhgQBAAEBBgAAAAAAAAYDAAAAAAAABAQAAAAgAAAFBQAAAAAAAAUGAAAAAAAABQgAAAAAAAAFCQAAAAAAAAULAAAAAAAABg0AAAAAAAAGEAAAAAAAAAYTAAAAAAAABhYAAAAAAAAGGQAAAAAAAAYcAAAAAAAABh8AAAAAAAAGIgAAAAAAAQYlAAAAAAABBikAAAAAAAIGLwAAAAAAAwY7AAAAAAAEBlMAAAAAAAcGgwAAAAAACQYDAgAAEAAABAQAAAAAAAAEBQAAACAAAAUGAAAAAAAABQcAAAAgAAAFCQAAAAAAAAUKAAAAAAAABgwAAAAAAAAGDwAAAAAAAAYSAAAAAAAABhUAAAAAAAAGGAAAAAAAAAYbAAAAAAAABh4AAAAAAAAGIQAAAAAAAQYjAAAAAAABBicAAAAAAAIGKwAAAAAAAwYzAAAAAAAEBkMAAAAAAAUGYwAAAAAACAYDAQAAIAAABAQAAAAwAAAEBAAAABAAAAQFAAAAIAAABQcAAAAgAAAFCAAAACAAAAUKAAAAIAAABQsAAAAAAAAGDgAAAAAAAAYRAAAAAAAABhQAAAAAAAAGFwAAAAAAAAYaAAAAAAAABh0AAAAAAAAGIAAAAAAAEAYDAAEAAAAPBgOAAAAAAA4GA0AAAAAADQYDIAAAAAAMBgMQAAAAAAsGAwgAAAAACgYDBABBpB0L2QEBAAAAAwAAAAcAAAAPAAAAHwAAAD8AAAB/AAAA/wAAAP8BAAD/AwAA/wcAAP8PAAD/HwAA/z8AAP9/AAD//wAA//8BAP//AwD//wcA//8PAP//HwD//z8A//9/AP///wD///8B////A////wf///8P////H////z////9/AAAAAAEAAAACAAAABAAAAAAAAAACAAAABAAAAAgAAAAAAAAAAQAAAAIAAAABAAAABAAAAAQAAAAEAAAABAAAAAgAAAAIAAAACAAAAAcAAAAIAAAACQAAAAoAAAALAEGgIAsDwBBQ'; /** * Loader for KTX 2.0 GPU Texture containers. * * KTX 2.0 is a container format for various GPU texture formats. The loader * supports Basis Universal GPU textures, which can be quickly transcoded to * a wide variety of GPU texture compression formats, as well as some * uncompressed DataTexture and Data3DTexture formats. * * References: * - KTX: http://github.khronos.org/KTX-Specification/ * - DFD: https://www.khronos.org/registry/DataFormat/specs/1.3/dataformat.1.3.html#basicdescriptor */ const _taskCache$1 = new WeakMap(); let _activeLoaders = 0; let _zstd; class KTX2Loader extends Loader { constructor( manager ) { super( manager ); this.transcoderPath = ''; this.transcoderBinary = null; this.transcoderPending = null; this.workerPool = new WorkerPool$1(); this.workerSourceURL = ''; this.workerConfig = null; if ( typeof MSC_TRANSCODER !== 'undefined' ) { console.warn( 'THREE.KTX2Loader: Please update to latest "basis_transcoder".' + ' "msc_basis_transcoder" is no longer supported in three.js r125+.' ); } } setTranscoderPath( path ) { this.transcoderPath = path; return this; } setWorkerLimit( num ) { this.workerPool.setWorkerLimit( num ); return this; } detectSupport( renderer ) { this.workerConfig = { astcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_astc' ), etc1Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc1' ), etc2Supported: renderer.extensions.has( 'WEBGL_compressed_texture_etc' ), dxtSupported: renderer.extensions.has( 'WEBGL_compressed_texture_s3tc' ), bptcSupported: renderer.extensions.has( 'EXT_texture_compression_bptc' ), pvrtcSupported: renderer.extensions.has( 'WEBGL_compressed_texture_pvrtc' ) || renderer.extensions.has( 'WEBKIT_WEBGL_compressed_texture_pvrtc' ) }; if ( renderer.capabilities.isWebGL2 ) { // https://github.com/mrdoob/three.js/pull/22928 this.workerConfig.etc1Supported = false; } return this; } init() { if ( ! this.transcoderPending ) { // Load transcoder wrapper. const jsLoader = new FileLoader( this.manager ); jsLoader.setPath( this.transcoderPath ); jsLoader.setWithCredentials( this.withCredentials ); const jsContent = jsLoader.loadAsync( 'basis_transcoder.js' ); // Load transcoder WASM binary. const binaryLoader = new FileLoader( this.manager ); binaryLoader.setPath( this.transcoderPath ); binaryLoader.setResponseType( 'arraybuffer' ); binaryLoader.setWithCredentials( this.withCredentials ); const binaryContent = binaryLoader.loadAsync( 'basis_transcoder.wasm' ); this.transcoderPending = Promise.all( [ jsContent, binaryContent ] ) .then( ( [ jsContent, binaryContent ] ) => { const fn = KTX2Loader.BasisWorker.toString(); const body = [ '/* constants */', 'let _EngineFormat = ' + JSON.stringify( KTX2Loader.EngineFormat ), 'let _TranscoderFormat = ' + JSON.stringify( KTX2Loader.TranscoderFormat ), 'let _BasisFormat = ' + JSON.stringify( KTX2Loader.BasisFormat ), '/* basis_transcoder.js */', jsContent, '/* worker */', fn.substring( fn.indexOf( '{' ) + 1, fn.lastIndexOf( '}' ) ) ].join( '\n' ); this.workerSourceURL = URL.createObjectURL( new Blob( [ body ] ) ); this.transcoderBinary = binaryContent; this.workerPool.setWorkerCreator( () => { const worker = new Worker( this.workerSourceURL ); const transcoderBinary = this.transcoderBinary.slice( 0 ); worker.postMessage( { type: 'init', config: this.workerConfig, transcoderBinary }, [ transcoderBinary ] ); return worker; } ); } ); if ( _activeLoaders > 0 ) { // Each instance loads a transcoder and allocates workers, increasing network and memory cost. console.warn( 'THREE.KTX2Loader: Multiple active KTX2 loaders may cause performance issues.' + ' Use a single KTX2Loader instance, or call .dispose() on old instances.' ); } _activeLoaders ++; } return this.transcoderPending; } load( url, onLoad, onProgress, onError ) { if ( this.workerConfig === null ) { throw new Error( 'THREE.KTX2Loader: Missing initialization with `.detectSupport( renderer )`.' ); } const loader = new FileLoader( this.manager ); loader.setResponseType( 'arraybuffer' ); loader.setWithCredentials( this.withCredentials ); loader.load( url, ( buffer ) => { // Check for an existing task using this buffer. A transferred buffer cannot be transferred // again from this thread. if ( _taskCache$1.has( buffer ) ) { const cachedTask = _taskCache$1.get( buffer ); return cachedTask.promise.then( onLoad ).catch( onError ); } this._createTexture( buffer ) .then( ( texture ) => onLoad ? onLoad( texture ) : null ) .catch( onError ); }, onProgress, onError ); } _createTextureFrom( transcodeResult, container ) { const { mipmaps, width, height, format, type, error, dfdTransferFn, dfdFlags } = transcodeResult; if ( type === 'error' ) return Promise.reject( error ); const texture = container.layerCount > 1 ? new CompressedArrayTexture( mipmaps, width, height, container.layerCount, format, UnsignedByteType ) : new CompressedTexture( mipmaps, width, height, format, UnsignedByteType ); texture.minFilter = mipmaps.length === 1 ? LinearFilter : LinearMipmapLinearFilter; texture.magFilter = LinearFilter; texture.generateMipmaps = false; texture.needsUpdate = true; texture.encoding = dfdTransferFn === x$2 ? sRGBEncoding : LinearEncoding; texture.premultiplyAlpha = !! ( dfdFlags & p ); return texture; } /** * @param {ArrayBuffer} buffer * @param {object?} config * @return {Promise} */ async _createTexture( buffer, config = {} ) { const container = Pi( new Uint8Array( buffer ) ); if ( container.vkFormat !== nt ) { return createDataTexture( container ); } // const taskConfig = config; const texturePending = this.init().then( () => { return this.workerPool.postMessage( { type: 'transcode', buffer, taskConfig: taskConfig }, [ buffer ] ); } ).then( ( e ) => this._createTextureFrom( e.data, container ) ); // Cache the task result. _taskCache$1.set( buffer, { promise: texturePending } ); return texturePending; } dispose() { this.workerPool.dispose(); if ( this.workerSourceURL ) URL.revokeObjectURL( this.workerSourceURL ); _activeLoaders --; return this; } } /* CONSTANTS */ KTX2Loader.BasisFormat = { ETC1S: 0, UASTC_4x4: 1, }; KTX2Loader.TranscoderFormat = { ETC1: 0, ETC2: 1, BC1: 2, BC3: 3, BC4: 4, BC5: 5, BC7_M6_OPAQUE_ONLY: 6, BC7_M5: 7, PVRTC1_4_RGB: 8, PVRTC1_4_RGBA: 9, ASTC_4x4: 10, ATC_RGB: 11, ATC_RGBA_INTERPOLATED_ALPHA: 12, RGBA32: 13, RGB565: 14, BGR565: 15, RGBA4444: 16, }; KTX2Loader.EngineFormat = { RGBAFormat: RGBAFormat, RGBA_ASTC_4x4_Format: RGBA_ASTC_4x4_Format, RGBA_BPTC_Format: RGBA_BPTC_Format, RGBA_ETC2_EAC_Format: RGBA_ETC2_EAC_Format, RGBA_PVRTC_4BPPV1_Format: RGBA_PVRTC_4BPPV1_Format, RGBA_S3TC_DXT5_Format: RGBA_S3TC_DXT5_Format$1, RGB_ETC1_Format: RGB_ETC1_Format, RGB_ETC2_Format: RGB_ETC2_Format, RGB_PVRTC_4BPPV1_Format: RGB_PVRTC_4BPPV1_Format, RGB_S3TC_DXT1_Format: RGB_S3TC_DXT1_Format, }; /* WEB WORKER */ KTX2Loader.BasisWorker = function () { let config; let transcoderPending; let BasisModule; const EngineFormat = _EngineFormat; // eslint-disable-line no-undef const TranscoderFormat = _TranscoderFormat; // eslint-disable-line no-undef const BasisFormat = _BasisFormat; // eslint-disable-line no-undef self.addEventListener( 'message', function ( e ) { const message = e.data; switch ( message.type ) { case 'init': config = message.config; init( message.transcoderBinary ); break; case 'transcode': transcoderPending.then( () => { try { const { width, height, hasAlpha, mipmaps, format, dfdTransferFn, dfdFlags } = transcode( message.buffer ); const buffers = []; for ( let i = 0; i < mipmaps.length; ++ i ) { buffers.push( mipmaps[ i ].data.buffer ); } self.postMessage( { type: 'transcode', id: message.id, width, height, hasAlpha, mipmaps, format, dfdTransferFn, dfdFlags }, buffers ); } catch ( error ) { console.error( error ); self.postMessage( { type: 'error', id: message.id, error: error.message } ); } } ); break; } } ); function init( wasmBinary ) { transcoderPending = new Promise( ( resolve ) => { BasisModule = { wasmBinary, onRuntimeInitialized: resolve }; BASIS( BasisModule ); // eslint-disable-line no-undef } ).then( () => { BasisModule.initializeBasis(); if ( BasisModule.KTX2File === undefined ) { console.warn( 'THREE.KTX2Loader: Please update Basis Universal transcoder.' ); } } ); } function transcode( buffer ) { const ktx2File = new BasisModule.KTX2File( new Uint8Array( buffer ) ); function cleanup() { ktx2File.close(); ktx2File.delete(); } if ( ! ktx2File.isValid() ) { cleanup(); throw new Error( 'THREE.KTX2Loader: Invalid or unsupported .ktx2 file' ); } const basisFormat = ktx2File.isUASTC() ? BasisFormat.UASTC_4x4 : BasisFormat.ETC1S; const width = ktx2File.getWidth(); const height = ktx2File.getHeight(); const layers = ktx2File.getLayers() || 1; const levels = ktx2File.getLevels(); const hasAlpha = ktx2File.getHasAlpha(); const dfdTransferFn = ktx2File.getDFDTransferFunc(); const dfdFlags = ktx2File.getDFDFlags(); const { transcoderFormat, engineFormat } = getTranscoderFormat( basisFormat, width, height, hasAlpha ); if ( ! width || ! height || ! levels ) { cleanup(); throw new Error( 'THREE.KTX2Loader: Invalid texture' ); } if ( ! ktx2File.startTranscoding() ) { cleanup(); throw new Error( 'THREE.KTX2Loader: .startTranscoding failed' ); } const mipmaps = []; for ( let mip = 0; mip < levels; mip ++ ) { const layerMips = []; let mipWidth, mipHeight; for ( let layer = 0; layer < layers; layer ++ ) { const levelInfo = ktx2File.getImageLevelInfo( mip, layer, 0 ); mipWidth = levelInfo.origWidth; mipHeight = levelInfo.origHeight; const dst = new Uint8Array( ktx2File.getImageTranscodedSizeInBytes( mip, layer, 0, transcoderFormat ) ); const status = ktx2File.transcodeImage( dst, mip, layer, 0, transcoderFormat, 0, - 1, - 1, ); if ( ! status ) { cleanup(); throw new Error( 'THREE.KTX2Loader: .transcodeImage failed.' ); } layerMips.push( dst ); } mipmaps.push( { data: concat( layerMips ), width: mipWidth, height: mipHeight } ); } cleanup(); return { width, height, hasAlpha, mipmaps, format: engineFormat, dfdTransferFn, dfdFlags }; } // // Optimal choice of a transcoder target format depends on the Basis format (ETC1S or UASTC), // device capabilities, and texture dimensions. The list below ranks the formats separately // for ETC1S and UASTC. // // In some cases, transcoding UASTC to RGBA32 might be preferred for higher quality (at // significant memory cost) compared to ETC1/2, BC1/3, and PVRTC. The transcoder currently // chooses RGBA32 only as a last resort and does not expose that option to the caller. const FORMAT_OPTIONS = [ { if: 'astcSupported', basisFormat: [ BasisFormat.UASTC_4x4 ], transcoderFormat: [ TranscoderFormat.ASTC_4x4, TranscoderFormat.ASTC_4x4 ], engineFormat: [ EngineFormat.RGBA_ASTC_4x4_Format, EngineFormat.RGBA_ASTC_4x4_Format ], priorityETC1S: Infinity, priorityUASTC: 1, needsPowerOfTwo: false, }, { if: 'bptcSupported', basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ], transcoderFormat: [ TranscoderFormat.BC7_M5, TranscoderFormat.BC7_M5 ], engineFormat: [ EngineFormat.RGBA_BPTC_Format, EngineFormat.RGBA_BPTC_Format ], priorityETC1S: 3, priorityUASTC: 2, needsPowerOfTwo: false, }, { if: 'dxtSupported', basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ], transcoderFormat: [ TranscoderFormat.BC1, TranscoderFormat.BC3 ], engineFormat: [ EngineFormat.RGB_S3TC_DXT1_Format, EngineFormat.RGBA_S3TC_DXT5_Format ], priorityETC1S: 4, priorityUASTC: 5, needsPowerOfTwo: false, }, { if: 'etc2Supported', basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ], transcoderFormat: [ TranscoderFormat.ETC1, TranscoderFormat.ETC2 ], engineFormat: [ EngineFormat.RGB_ETC2_Format, EngineFormat.RGBA_ETC2_EAC_Format ], priorityETC1S: 1, priorityUASTC: 3, needsPowerOfTwo: false, }, { if: 'etc1Supported', basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ], transcoderFormat: [ TranscoderFormat.ETC1 ], engineFormat: [ EngineFormat.RGB_ETC1_Format ], priorityETC1S: 2, priorityUASTC: 4, needsPowerOfTwo: false, }, { if: 'pvrtcSupported', basisFormat: [ BasisFormat.ETC1S, BasisFormat.UASTC_4x4 ], transcoderFormat: [ TranscoderFormat.PVRTC1_4_RGB, TranscoderFormat.PVRTC1_4_RGBA ], engineFormat: [ EngineFormat.RGB_PVRTC_4BPPV1_Format, EngineFormat.RGBA_PVRTC_4BPPV1_Format ], priorityETC1S: 5, priorityUASTC: 6, needsPowerOfTwo: true, }, ]; const ETC1S_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) { return a.priorityETC1S - b.priorityETC1S; } ); const UASTC_OPTIONS = FORMAT_OPTIONS.sort( function ( a, b ) { return a.priorityUASTC - b.priorityUASTC; } ); function getTranscoderFormat( basisFormat, width, height, hasAlpha ) { let transcoderFormat; let engineFormat; const options = basisFormat === BasisFormat.ETC1S ? ETC1S_OPTIONS : UASTC_OPTIONS; for ( let i = 0; i < options.length; i ++ ) { const opt = options[ i ]; if ( ! config[ opt.if ] ) continue; if ( ! opt.basisFormat.includes( basisFormat ) ) continue; if ( hasAlpha && opt.transcoderFormat.length < 2 ) continue; if ( opt.needsPowerOfTwo && ! ( isPowerOfTwo( width ) && isPowerOfTwo( height ) ) ) continue; transcoderFormat = opt.transcoderFormat[ hasAlpha ? 1 : 0 ]; engineFormat = opt.engineFormat[ hasAlpha ? 1 : 0 ]; return { transcoderFormat, engineFormat }; } console.warn( 'THREE.KTX2Loader: No suitable compressed texture format found. Decoding to RGBA32.' ); transcoderFormat = TranscoderFormat.RGBA32; engineFormat = EngineFormat.RGBAFormat; return { transcoderFormat, engineFormat }; } function isPowerOfTwo( value ) { if ( value <= 2 ) return true; return ( value & ( value - 1 ) ) === 0 && value !== 0; } /** Concatenates N byte arrays. */ function concat( arrays ) { let totalByteLength = 0; for ( const array of arrays ) { totalByteLength += array.byteLength; } const result = new Uint8Array( totalByteLength ); let byteOffset = 0; for ( const array of arrays ) { result.set( array, byteOffset ); byteOffset += array.byteLength; } return result; } }; // // DataTexture and Data3DTexture parsing. const FORMAT_MAP = { [ Ae ]: RGBAFormat, [ pe ]: RGBAFormat, [ Ot ]: RGBAFormat, [ Ft ]: RGBAFormat, [ de ]: RGFormat, [ se ]: RGFormat, [ yt ]: RGFormat, [ dt ]: RGFormat, [ xe ]: RedFormat, [ $t ]: RedFormat, [ gt ]: RedFormat, [ ct ]: RedFormat, }; const TYPE_MAP = { [ Ae ]: FloatType, [ pe ]: HalfFloatType, [ Ot ]: UnsignedByteType, [ Ft ]: UnsignedByteType, [ de ]: FloatType, [ se ]: HalfFloatType, [ yt ]: UnsignedByteType, [ dt ]: UnsignedByteType, [ xe ]: FloatType, [ $t ]: HalfFloatType, [ gt ]: UnsignedByteType, [ ct ]: UnsignedByteType, }; const ENCODING_MAP = { [ Ft ]: sRGBEncoding, [ dt ]: sRGBEncoding, [ gt ]: sRGBEncoding, }; async function createDataTexture( container ) { const { vkFormat, pixelWidth, pixelHeight, pixelDepth } = container; if ( FORMAT_MAP[ vkFormat ] === undefined ) { throw new Error( 'THREE.KTX2Loader: Unsupported vkFormat.' ); } const level = container.levels[ 0 ]; let levelData; let view; if ( container.supercompressionScheme === t ) { levelData = level.levelData; } else if ( container.supercompressionScheme === n$1 ) { if ( ! _zstd ) { _zstd = new Promise( async ( resolve ) => { const zstd = new ZSTDDecoder(); await zstd.init(); resolve( zstd ); } ); } levelData = ( await _zstd ).decode( level.levelData, level.uncompressedByteLength ); } else { throw new Error( 'THREE.KTX2Loader: Unsupported supercompressionScheme.' ); } if ( TYPE_MAP[ vkFormat ] === FloatType ) { view = new Float32Array( levelData.buffer, levelData.byteOffset, levelData.byteLength / Float32Array.BYTES_PER_ELEMENT ); } else if ( TYPE_MAP[ vkFormat ] === HalfFloatType ) { view = new Uint16Array( levelData.buffer, levelData.byteOffset, levelData.byteLength / Uint16Array.BYTES_PER_ELEMENT ); } else { view = levelData; } // const texture = pixelDepth === 0 ? new DataTexture( view, pixelWidth, pixelHeight ) : new Data3DTexture( view, pixelWidth, pixelHeight, pixelDepth ); texture.type = TYPE_MAP[ vkFormat ]; texture.format = FORMAT_MAP[ vkFormat ]; texture.encoding = ENCODING_MAP[ vkFormat ] || LinearEncoding; texture.needsUpdate = true; // return Promise.resolve( texture ); } // This file is part of meshoptimizer library and is distributed under the terms of MIT License. // Copyright (C) 2016-2020, by Arseny Kapoulkine (arseny.kapoulkine@gmail.com) var MeshoptDecoder = (function() { "use strict"; // Built with clang version 11.0.0 (https://github.com/llvm/llvm-project.git 0160ad802e899c2922bc9b29564080c22eb0908c) // Built from meshoptimizer 0.14 var wasm_base = "B9h9z9tFBBBF8fL9gBB9gLaaaaaFa9gEaaaB9gFaFa9gEaaaFaEMcBFFFGGGEIIILF9wFFFLEFBFKNFaFCx/IFMO/LFVK9tv9t9vq95GBt9f9f939h9z9t9f9j9h9s9s9f9jW9vq9zBBp9tv9z9o9v9wW9f9kv9j9v9kv9WvqWv94h919m9mvqBF8Z9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv94h919m9mvqBGy9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv949TvZ91v9u9jvBEn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9P9jWBIi9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9R919hWBLn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9F949wBKI9z9iqlBOc+x8ycGBM/qQFTa8jUUUUBCU/EBlHL8kUUUUBC9+RKGXAGCFJAI9LQBCaRKAE2BBC+gF9HQBALAEAIJHOAGlAGTkUUUBRNCUoBAG9uC/wgBZHKCUGAKCUG9JyRVAECFJRICBRcGXEXAcAF9PQFAVAFAclAcAVJAF9JyRMGXGXAG9FQBAMCbJHKC9wZRSAKCIrCEJCGrRQANCUGJRfCBRbAIRTEXGXAOATlAQ9PQBCBRISEMATAQJRIGXAS9FQBCBRtCBREEXGXAOAIlCi9PQBCBRISLMANCU/CBJAEJRKGXGXGXGXGXATAECKrJ2BBAtCKZrCEZfIBFGEBMAKhB83EBAKCNJhB83EBSEMAKAI2BIAI2BBHmCKrHYAYCE6HYy86BBAKCFJAICIJAYJHY2BBAmCIrCEZHPAPCE6HPy86BBAKCGJAYAPJHY2BBAmCGrCEZHPAPCE6HPy86BBAKCEJAYAPJHY2BBAmCEZHmAmCE6Hmy86BBAKCIJAYAmJHY2BBAI2BFHmCKrHPAPCE6HPy86BBAKCLJAYAPJHY2BBAmCIrCEZHPAPCE6HPy86BBAKCKJAYAPJHY2BBAmCGrCEZHPAPCE6HPy86BBAKCOJAYAPJHY2BBAmCEZHmAmCE6Hmy86BBAKCNJAYAmJHY2BBAI2BGHmCKrHPAPCE6HPy86BBAKCVJAYAPJHY2BBAmCIrCEZHPAPCE6HPy86BBAKCcJAYAPJHY2BBAmCGrCEZHPAPCE6HPy86BBAKCMJAYAPJHY2BBAmCEZHmAmCE6Hmy86BBAKCSJAYAmJHm2BBAI2BEHICKrHYAYCE6HYy86BBAKCQJAmAYJHm2BBAICIrCEZHYAYCE6HYy86BBAKCfJAmAYJHm2BBAICGrCEZHYAYCE6HYy86BBAKCbJAmAYJHK2BBAICEZHIAICE6HIy86BBAKAIJRISGMAKAI2BNAI2BBHmCIrHYAYCb6HYy86BBAKCFJAICNJAYJHY2BBAmCbZHmAmCb6Hmy86BBAKCGJAYAmJHm2BBAI2BFHYCIrHPAPCb6HPy86BBAKCEJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCIJAmAYJHm2BBAI2BGHYCIrHPAPCb6HPy86BBAKCLJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCKJAmAYJHm2BBAI2BEHYCIrHPAPCb6HPy86BBAKCOJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCNJAmAYJHm2BBAI2BIHYCIrHPAPCb6HPy86BBAKCVJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCcJAmAYJHm2BBAI2BLHYCIrHPAPCb6HPy86BBAKCMJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCSJAmAYJHm2BBAI2BKHYCIrHPAPCb6HPy86BBAKCQJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCfJAmAYJHm2BBAI2BOHICIrHYAYCb6HYy86BBAKCbJAmAYJHK2BBAICbZHIAICb6HIy86BBAKAIJRISFMAKAI8pBB83BBAKCNJAICNJ8pBB83BBAICTJRIMAtCGJRtAECTJHEAS9JQBMMGXAIQBCBRISEMGXAM9FQBANAbJ2BBRtCBRKAfREEXAEANCU/CBJAKJ2BBHTCFrCBATCFZl9zAtJHt86BBAEAGJREAKCFJHKAM9HQBMMAfCFJRfAIRTAbCFJHbAG9HQBMMABAcAG9sJANCUGJAMAG9sTkUUUBpANANCUGJAMCaJAG9sJAGTkUUUBpMAMCBAIyAcJRcAIQBMC9+RKSFMCBC99AOAIlAGCAAGCA9Ly6yRKMALCU/EBJ8kUUUUBAKM+OmFTa8jUUUUBCoFlHL8kUUUUBC9+RKGXAFCE9uHOCtJAI9LQBCaRKAE2BBHNC/wFZC/gF9HQBANCbZHVCF9LQBALCoBJCgFCUFT+JUUUBpALC84Jha83EBALC8wJha83EBALC8oJha83EBALCAJha83EBALCiJha83EBALCTJha83EBALha83ENALha83EBAEAIJC9wJRcAECFJHNAOJRMGXAF9FQBCQCbAVCF6yRSABRECBRVCBRQCBRfCBRICBRKEXGXAMAcuQBC9+RKSEMGXGXAN2BBHOC/vF9LQBALCoBJAOCIrCa9zAKJCbZCEWJHb8oGIRTAb8oGBRtGXAOCbZHbAS9PQBALAOCa9zAIJCbZCGWJ8oGBAVAbyROAb9FRbGXGXAGCG9HQBABAt87FBABCIJAO87FBABCGJAT87FBSFMAEAtjGBAECNJAOjGBAECIJATjGBMAVAbJRVALCoBJAKCEWJHmAOjGBAmATjGIALAICGWJAOjGBALCoBJAKCFJCbZHKCEWJHTAtjGBATAOjGIAIAbJRIAKCFJRKSGMGXGXAbCb6QBAQAbJAbC989zJCFJRQSFMAM1BBHbCgFZROGXGXAbCa9MQBAMCFJRMSFMAM1BFHbCgBZCOWAOCgBZqROGXAbCa9MQBAMCGJRMSFMAM1BGHbCgBZCfWAOqROGXAbCa9MQBAMCEJRMSFMAM1BEHbCgBZCdWAOqROGXAbCa9MQBAMCIJRMSFMAM2BIC8cWAOqROAMCLJRMMAOCFrCBAOCFZl9zAQJRQMGXGXAGCG9HQBABAt87FBABCIJAQ87FBABCGJAT87FBSFMAEAtjGBAECNJAQjGBAECIJATjGBMALCoBJAKCEWJHOAQjGBAOATjGIALAICGWJAQjGBALCoBJAKCFJCbZHKCEWJHOAtjGBAOAQjGIAICFJRIAKCFJRKSFMGXAOCDF9LQBALAIAcAOCbZJ2BBHbCIrHTlCbZCGWJ8oGBAVCFJHtATyROALAIAblCbZCGWJ8oGBAtAT9FHmJHtAbCbZHTyRbAT9FRTGXGXAGCG9HQBABAV87FBABCIJAb87FBABCGJAO87FBSFMAEAVjGBAECNJAbjGBAECIJAOjGBMALAICGWJAVjGBALCoBJAKCEWJHYAOjGBAYAVjGIALAICFJHICbZCGWJAOjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAIAmJCbZHICGWJAbjGBALCoBJAKCGJCbZHKCEWJHOAVjGBAOAbjGIAKCFJRKAIATJRIAtATJRVSFMAVCBAM2BBHYyHTAOC/+F6HPJROAYCbZRtGXGXAYCIrHmQBAOCFJRbSFMAORbALAIAmlCbZCGWJ8oGBROMGXGXAtQBAbCFJRVSFMAbRVALAIAYlCbZCGWJ8oGBRbMGXGXAP9FQBAMCFJRYSFMAM1BFHYCgFZRTGXGXAYCa9MQBAMCGJRYSFMAM1BGHYCgBZCOWATCgBZqRTGXAYCa9MQBAMCEJRYSFMAM1BEHYCgBZCfWATqRTGXAYCa9MQBAMCIJRYSFMAM1BIHYCgBZCdWATqRTGXAYCa9MQBAMCLJRYSFMAMCKJRYAM2BLC8cWATqRTMATCFrCBATCFZl9zAQJHQRTMGXGXAmCb6QBAYRPSFMAY1BBHMCgFZROGXGXAMCa9MQBAYCFJRPSFMAY1BFHMCgBZCOWAOCgBZqROGXAMCa9MQBAYCGJRPSFMAY1BGHMCgBZCfWAOqROGXAMCa9MQBAYCEJRPSFMAY1BEHMCgBZCdWAOqROGXAMCa9MQBAYCIJRPSFMAYCLJRPAY2BIC8cWAOqROMAOCFrCBAOCFZl9zAQJHQROMGXGXAtCb6QBAPRMSFMAP1BBHMCgFZRbGXGXAMCa9MQBAPCFJRMSFMAP1BFHMCgBZCOWAbCgBZqRbGXAMCa9MQBAPCGJRMSFMAP1BGHMCgBZCfWAbqRbGXAMCa9MQBAPCEJRMSFMAP1BEHMCgBZCdWAbqRbGXAMCa9MQBAPCIJRMSFMAPCLJRMAP2BIC8cWAbqRbMAbCFrCBAbCFZl9zAQJHQRbMGXGXAGCG9HQBABAT87FBABCIJAb87FBABCGJAO87FBSFMAEATjGBAECNJAbjGBAECIJAOjGBMALCoBJAKCEWJHYAOjGBAYATjGIALAICGWJATjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAICFJHICbZCGWJAOjGBALCoBJAKCGJCbZCEWJHOATjGBAOAbjGIALAIAm9FAmCb6qJHICbZCGWJAbjGBAIAt9FAtCb6qJRIAKCEJRKMANCFJRNABCKJRBAECSJREAKCbZRKAICbZRIAfCEJHfAF9JQBMMCBC99AMAc6yRKMALCoFJ8kUUUUBAKM/tIFGa8jUUUUBCTlRLC9+RKGXAFCLJAI9LQBCaRKAE2BBC/+FZC/QF9HQBALhB83ENAECFJRKAEAIJC98JREGXAF9FQBGXAGCG6QBEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMALCNJAICFZCGWqHGAICGrCBAICFrCFZl9zAG8oGBJHIjGBABAIjGBABCIJRBAFCaJHFQBSGMMEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMABAICGrCBAICFrCFZl9zALCNJAICFZCGWqHI8oGBJHG87FBAIAGjGBABCGJRBAFCaJHFQBMMCBC99AKAE6yRKMAKM+lLKFaF99GaG99FaG99GXGXAGCI9HQBAF9FQFEXGXGX9DBBB8/9DBBB+/ABCGJHG1BB+yAB1BBHE+yHI+L+TABCFJHL1BBHK+yHO+L+THN9DBBBB9gHVyAN9DBB/+hANAN+U9DBBBBANAVyHcAc+MHMAECa3yAI+SHIAI+UAcAMAKCa3yAO+SHcAc+U+S+S+R+VHO+U+SHN+L9DBBB9P9d9FQBAN+oRESFMCUUUU94REMAGAE86BBGXGX9DBBB8/9DBBB+/Ac9DBBBB9gyAcAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMALAG86BBGXGX9DBBB8/9DBBB+/AI9DBBBB9gyAIAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMABAG86BBABCIJRBAFCaJHFQBSGMMAF9FQBEXGXGX9DBBB8/9DBBB+/ABCIJHG8uFB+yAB8uFBHE+yHI+L+TABCGJHL8uFBHK+yHO+L+THN9DBBBB9gHVyAN9DB/+g6ANAN+U9DBBBBANAVyHcAc+MHMAECa3yAI+SHIAI+UAcAMAKCa3yAO+SHcAc+U+S+S+R+VHO+U+SHN+L9DBBB9P9d9FQBAN+oRESFMCUUUU94REMAGAE87FBGXGX9DBBB8/9DBBB+/Ac9DBBBB9gyAcAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMALAG87FBGXGX9DBBB8/9DBBB+/AI9DBBBB9gyAIAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMABAG87FBABCNJRBAFCaJHFQBMMM/SEIEaE99EaF99GXAF9FQBCBREABRIEXGXGX9D/zI818/AICKJ8uFBHLCEq+y+VHKAI8uFB+y+UHO9DB/+g6+U9DBBB8/9DBBB+/AO9DBBBB9gy+SHN+L9DBBB9P9d9FQBAN+oRVSFMCUUUU94RVMAICIJ8uFBRcAICGJ8uFBRMABALCFJCEZAEqCFWJAV87FBGXGXAKAM+y+UHN9DB/+g6+U9DBBB8/9DBBB+/AN9DBBBB9gy+SHS+L9DBBB9P9d9FQBAS+oRMSFMCUUUU94RMMABALCGJCEZAEqCFWJAM87FBGXGXAKAc+y+UHK9DB/+g6+U9DBBB8/9DBBB+/AK9DBBBB9gy+SHS+L9DBBB9P9d9FQBAS+oRcSFMCUUUU94RcMABALCaJCEZAEqCFWJAc87FBGXGX9DBBU8/AOAO+U+TANAN+U+TAKAK+U+THO9DBBBBAO9DBBBB9gy+R9DB/+g6+U9DBBB8/+SHO+L9DBBB9P9d9FQBAO+oRcSFMCUUUU94RcMABALCEZAEqCFWJAc87FBAICNJRIAECIJREAFCaJHFQBMMM9JBGXAGCGrAF9sHF9FQBEXABAB8oGBHGCNWCN91+yAGCi91CnWCUUU/8EJ+++U84GBABCIJRBAFCaJHFQBMMM9TFEaCBCB8oGUkUUBHFABCEJC98ZJHBjGUkUUBGXGXAB8/BCTWHGuQBCaREABAGlCggEJCTrXBCa6QFMAFREMAEM/lFFFaGXGXAFABqCEZ9FQBABRESFMGXGXAGCT9PQBABRESFMABREEXAEAF8oGBjGBAECIJAFCIJ8oGBjGBAECNJAFCNJ8oGBjGBAECSJAFCSJ8oGBjGBAECTJREAFCTJRFAGC9wJHGCb9LQBMMAGCI9JQBEXAEAF8oGBjGBAFCIJRFAECIJREAGC98JHGCE9LQBMMGXAG9FQBEXAEAF2BB86BBAECFJREAFCFJRFAGCaJHGQBMMABMoFFGaGXGXABCEZ9FQBABRESFMAFCgFZC+BwsN9sRIGXGXAGCT9PQBABRESFMABREEXAEAIjGBAECSJAIjGBAECNJAIjGBAECIJAIjGBAECTJREAGC9wJHGCb9LQBMMAGCI9JQBEXAEAIjGBAECIJREAGC98JHGCE9LQBMMGXAG9FQBEXAEAF86BBAECFJREAGCaJHGQBMMABMMMFBCUNMIT9kBB"; var wasm_simd = "B9h9z9tFBBBFiI9gBB9gLaaaaaFa9gEaaaB9gFaFaEMcBBFBFFGGGEILF9wFFFLEFBFKNFaFCx/aFMO/LFVK9tv9t9vq95GBt9f9f939h9z9t9f9j9h9s9s9f9jW9vq9zBBp9tv9z9o9v9wW9f9kv9j9v9kv9WvqWv94h919m9mvqBG8Z9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv94h919m9mvqBIy9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv949TvZ91v9u9jvBLn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9P9jWBKi9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9R919hWBOn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9F949wBNI9z9iqlBVc+N9IcIBTEM9+FLa8jUUUUBCTlRBCBRFEXCBRGCBREEXABCNJAGJAECUaAFAGrCFZHIy86BBAEAIJREAGCFJHGCN9HQBMAFCx+YUUBJAE86BBAFCEWCxkUUBJAB8pEN83EBAFCFJHFCUG9HQBMMk8lLbaE97F9+FaL978jUUUUBCU/KBlHL8kUUUUBC9+RKGXAGCFJAI9LQBCaRKAE2BBC+gF9HQBALAEAIJHOAGlAG/8cBBCUoBAG9uC/wgBZHKCUGAKCUG9JyRNAECFJRKCBRVGXEXAVAF9PQFANAFAVlAVANJAF9JyRcGXGXAG9FQBAcCbJHIC9wZHMCE9sRSAMCFWRQAICIrCEJCGrRfCBRbEXAKRTCBRtGXEXGXAOATlAf9PQBCBRKSLMALCU/CBJAtAM9sJRmATAfJRKCBREGXAMCoB9JQBAOAKlC/gB9JQBCBRIEXAmAIJREGXGXGXGXGXATAICKrJ2BBHYCEZfIBFGEBMAECBDtDMIBSEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCIJAeDeBJAiCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCNJAeDeBJAiCx+YUUBJ2BBJRKSFMAEAKDBBBDMIBAKCTJRKMGXGXGXGXGXAYCGrCEZfIBFGEBMAECBDtDMITSEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMITAKCIJAeDeBJAiCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMITAKCNJAeDeBJAiCx+YUUBJ2BBJRKSFMAEAKDBBBDMITAKCTJRKMGXGXGXGXGXAYCIrCEZfIBFGEBMAECBDtDMIASEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIAAKCIJAeDeBJAiCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIAAKCNJAeDeBJAiCx+YUUBJ2BBJRKSFMAEAKDBBBDMIAAKCTJRKMGXGXGXGXGXAYCKrfIBFGEBMAECBDtDMI8wSEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHYCEWCxkUUBJDBEBAYCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHYCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMI8wAKCIJAeDeBJAYCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHYCEWCxkUUBJDBEBAYCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHYCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMI8wAKCNJAeDeBJAYCx+YUUBJ2BBJRKSFMAEAKDBBBDMI8wAKCTJRKMAICoBJREAICUFJAM9LQFAERIAOAKlC/fB9LQBMMGXAEAM9PQBAECErRIEXGXAOAKlCi9PQBCBRKSOMAmAEJRYGXGXGXGXGXATAECKrJ2BBAICKZrCEZfIBFGEBMAYCBDtDMIBSEMAYAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCIJAeDeBJAiCx+YUUBJ2BBJRKSGMAYAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPAPDQBFGENVcMILKOSQfbHeD8dBh+BsxoxoUwN0AeD8dFhxoUwkwk+gUa0sHnhTkAnsHnhNkAnsHn7CgFZHiCEWCxkUUBJDBEBAiCx+YUUBJDBBBHeAeDQBBBBBBBBBBBBBBBBAnhAk7CgFZHiCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCNJAeDeBJAiCx+YUUBJ2BBJRKSFMAYAKDBBBDMIBAKCTJRKMAICGJRIAECTJHEAM9JQBMMGXAK9FQBAKRTAtCFJHtCI6QGSFMMCBRKSEMGXAM9FQBALCUGJAbJREALAbJDBGBReCBRYEXAEALCU/CBJAYJHIDBIBHdCFD9tAdCFDbHPD9OD9hD9RHdAIAMJDBIBH8ZCFD9tA8ZAPD9OD9hD9RH8ZDQBTFtGmEYIPLdKeOnHpAIAQJDBIBHyCFD9tAyAPD9OD9hD9RHyAIASJDBIBH8cCFD9tA8cAPD9OD9hD9RH8cDQBTFtGmEYIPLdKeOnH8dDQBFTtGEmYILPdKOenHPAPDQBFGEBFGEBFGEBFGEAeD9uHeDyBjGBAEAGJHIAeAPAPDQILKOILKOILKOILKOD9uHeDyBjGBAIAGJHIAeAPAPDQNVcMNVcMNVcMNVcMD9uHeDyBjGBAIAGJHIAeAPAPDQSQfbSQfbSQfbSQfbD9uHeDyBjGBAIAGJHIAeApA8dDQNVi8ZcMpySQ8c8dfb8e8fHPAPDQBFGEBFGEBFGEBFGED9uHeDyBjGBAIAGJHIAeAPAPDQILKOILKOILKOILKOD9uHeDyBjGBAIAGJHIAeAPAPDQNVcMNVcMNVcMNVcMD9uHeDyBjGBAIAGJHIAeAPAPDQSQfbSQfbSQfbSQfbD9uHeDyBjGBAIAGJHIAeAdA8ZDQNiV8ZcpMyS8cQ8df8eb8fHdAyA8cDQNiV8ZcpMyS8cQ8df8eb8fH8ZDQBFTtGEmYILPdKOenHPAPDQBFGEBFGEBFGEBFGED9uHeDyBjGBAIAGJHIAeAPAPDQILKOILKOILKOILKOD9uHeDyBjGBAIAGJHIAeAPAPDQNVcMNVcMNVcMNVcMD9uHeDyBjGBAIAGJHIAeAPAPDQSQfbSQfbSQfbSQfbD9uHeDyBjGBAIAGJHIAeAdA8ZDQNVi8ZcMpySQ8c8dfb8e8fHPAPDQBFGEBFGEBFGEBFGED9uHeDyBjGBAIAGJHIAeAPAPDQILKOILKOILKOILKOD9uHeDyBjGBAIAGJHIAeAPAPDQNVcMNVcMNVcMNVcMD9uHeDyBjGBAIAGJHIAeAPAPDQSQfbSQfbSQfbSQfbD9uHeDyBjGBAIAGJREAYCTJHYAM9JQBMMAbCIJHbAG9JQBMMABAVAG9sJALCUGJAcAG9s/8cBBALALCUGJAcCaJAG9sJAG/8cBBMAcCBAKyAVJRVAKQBMC9+RKSFMCBC99AOAKlAGCAAGCA9Ly6yRKMALCU/KBJ8kUUUUBAKMNBT+BUUUBM+KmFTa8jUUUUBCoFlHL8kUUUUBC9+RKGXAFCE9uHOCtJAI9LQBCaRKAE2BBHNC/wFZC/gF9HQBANCbZHVCF9LQBALCoBJCgFCUF/8MBALC84Jha83EBALC8wJha83EBALC8oJha83EBALCAJha83EBALCiJha83EBALCTJha83EBALha83ENALha83EBAEAIJC9wJRcAECFJHNAOJRMGXAF9FQBCQCbAVCF6yRSABRECBRVCBRQCBRfCBRICBRKEXGXAMAcuQBC9+RKSEMGXGXAN2BBHOC/vF9LQBALCoBJAOCIrCa9zAKJCbZCEWJHb8oGIRTAb8oGBRtGXAOCbZHbAS9PQBALAOCa9zAIJCbZCGWJ8oGBAVAbyROAb9FRbGXGXAGCG9HQBABAt87FBABCIJAO87FBABCGJAT87FBSFMAEAtjGBAECNJAOjGBAECIJATjGBMAVAbJRVALCoBJAKCEWJHmAOjGBAmATjGIALAICGWJAOjGBALCoBJAKCFJCbZHKCEWJHTAtjGBATAOjGIAIAbJRIAKCFJRKSGMGXGXAbCb6QBAQAbJAbC989zJCFJRQSFMAM1BBHbCgFZROGXGXAbCa9MQBAMCFJRMSFMAM1BFHbCgBZCOWAOCgBZqROGXAbCa9MQBAMCGJRMSFMAM1BGHbCgBZCfWAOqROGXAbCa9MQBAMCEJRMSFMAM1BEHbCgBZCdWAOqROGXAbCa9MQBAMCIJRMSFMAM2BIC8cWAOqROAMCLJRMMAOCFrCBAOCFZl9zAQJRQMGXGXAGCG9HQBABAt87FBABCIJAQ87FBABCGJAT87FBSFMAEAtjGBAECNJAQjGBAECIJATjGBMALCoBJAKCEWJHOAQjGBAOATjGIALAICGWJAQjGBALCoBJAKCFJCbZHKCEWJHOAtjGBAOAQjGIAICFJRIAKCFJRKSFMGXAOCDF9LQBALAIAcAOCbZJ2BBHbCIrHTlCbZCGWJ8oGBAVCFJHtATyROALAIAblCbZCGWJ8oGBAtAT9FHmJHtAbCbZHTyRbAT9FRTGXGXAGCG9HQBABAV87FBABCIJAb87FBABCGJAO87FBSFMAEAVjGBAECNJAbjGBAECIJAOjGBMALAICGWJAVjGBALCoBJAKCEWJHYAOjGBAYAVjGIALAICFJHICbZCGWJAOjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAIAmJCbZHICGWJAbjGBALCoBJAKCGJCbZHKCEWJHOAVjGBAOAbjGIAKCFJRKAIATJRIAtATJRVSFMAVCBAM2BBHYyHTAOC/+F6HPJROAYCbZRtGXGXAYCIrHmQBAOCFJRbSFMAORbALAIAmlCbZCGWJ8oGBROMGXGXAtQBAbCFJRVSFMAbRVALAIAYlCbZCGWJ8oGBRbMGXGXAP9FQBAMCFJRYSFMAM1BFHYCgFZRTGXGXAYCa9MQBAMCGJRYSFMAM1BGHYCgBZCOWATCgBZqRTGXAYCa9MQBAMCEJRYSFMAM1BEHYCgBZCfWATqRTGXAYCa9MQBAMCIJRYSFMAM1BIHYCgBZCdWATqRTGXAYCa9MQBAMCLJRYSFMAMCKJRYAM2BLC8cWATqRTMATCFrCBATCFZl9zAQJHQRTMGXGXAmCb6QBAYRPSFMAY1BBHMCgFZROGXGXAMCa9MQBAYCFJRPSFMAY1BFHMCgBZCOWAOCgBZqROGXAMCa9MQBAYCGJRPSFMAY1BGHMCgBZCfWAOqROGXAMCa9MQBAYCEJRPSFMAY1BEHMCgBZCdWAOqROGXAMCa9MQBAYCIJRPSFMAYCLJRPAY2BIC8cWAOqROMAOCFrCBAOCFZl9zAQJHQROMGXGXAtCb6QBAPRMSFMAP1BBHMCgFZRbGXGXAMCa9MQBAPCFJRMSFMAP1BFHMCgBZCOWAbCgBZqRbGXAMCa9MQBAPCGJRMSFMAP1BGHMCgBZCfWAbqRbGXAMCa9MQBAPCEJRMSFMAP1BEHMCgBZCdWAbqRbGXAMCa9MQBAPCIJRMSFMAPCLJRMAP2BIC8cWAbqRbMAbCFrCBAbCFZl9zAQJHQRbMGXGXAGCG9HQBABAT87FBABCIJAb87FBABCGJAO87FBSFMAEATjGBAECNJAbjGBAECIJAOjGBMALCoBJAKCEWJHYAOjGBAYATjGIALAICGWJATjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAICFJHICbZCGWJAOjGBALCoBJAKCGJCbZCEWJHOATjGBAOAbjGIALAIAm9FAmCb6qJHICbZCGWJAbjGBAIAt9FAtCb6qJRIAKCEJRKMANCFJRNABCKJRBAECSJREAKCbZRKAICbZRIAfCEJHfAF9JQBMMCBC99AMAc6yRKMALCoFJ8kUUUUBAKM/tIFGa8jUUUUBCTlRLC9+RKGXAFCLJAI9LQBCaRKAE2BBC/+FZC/QF9HQBALhB83ENAECFJRKAEAIJC98JREGXAF9FQBGXAGCG6QBEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMALCNJAICFZCGWqHGAICGrCBAICFrCFZl9zAG8oGBJHIjGBABAIjGBABCIJRBAFCaJHFQBSGMMEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMABAICGrCBAICFrCFZl9zALCNJAICFZCGWqHI8oGBJHG87FBAIAGjGBABCGJRBAFCaJHFQBMMCBC99AKAE6yRKMAKM/dLEK97FaF97GXGXAGCI9HQBAF9FQFCBRGEXABABDBBBHECiD+rFCiD+sFD/6FHIAECND+rFCiD+sFD/6FAID/gFAECTD+rFCiD+sFD/6FHLD/gFD/kFD/lFHKCBDtD+2FHOAICUUUU94DtHND9OD9RD/kFHI9DBB/+hDYAIAID/mFAKAKD/mFALAOALAND9OD9RD/kFHIAID/mFD/kFD/kFD/jFD/nFHLD/mF9DBBX9LDYHOD/kFCgFDtD9OAECUUU94DtD9OD9QAIALD/mFAOD/kFCND+rFCU/+EDtD9OD9QAKALD/mFAOD/kFCTD+rFCUU/8ODtD9OD9QDMBBABCTJRBAGCIJHGAF9JQBSGMMAF9FQBCBRGEXABCTJHVAVDBBBHECBDtHOCUU98D8cFCUU98D8cEHND9OABDBBBHKAEDQILKOSQfbPden8c8d8e8fCggFDtD9OD/6FAKAEDQBFGENVcMTtmYi8ZpyHECTD+sFD/6FHID/gFAECTD+rFCTD+sFD/6FHLD/gFD/kFD/lFHE9DB/+g6DYALAEAOD+2FHOALCUUUU94DtHcD9OD9RD/kFHLALD/mFAEAED/mFAIAOAIAcD9OD9RD/kFHEAED/mFD/kFD/kFD/jFD/nFHID/mF9DBBX9LDYHOD/kFCTD+rFALAID/mFAOD/kFCggEDtD9OD9QHLAEAID/mFAOD/kFCaDbCBDnGCBDnECBDnKCBDnOCBDncCBDnMCBDnfCBDnbD9OHEDQNVi8ZcMpySQ8c8dfb8e8fD9QDMBBABAKAND9OALAEDQBFTtGEmYILPdKOenD9QDMBBABCAJRBAGCIJHGAF9JQBMMM/hEIGaF97FaL978jUUUUBCTlREGXAF9FQBCBRIEXAEABDBBBHLABCTJHKDBBBHODQILKOSQfbPden8c8d8e8fHNCTD+sFHVCID+rFDMIBAB9DBBU8/DY9D/zI818/DYAVCEDtD9QD/6FD/nFHVALAODQBFGENVcMTtmYi8ZpyHLCTD+rFCTD+sFD/6FD/mFHOAOD/mFAVALCTD+sFD/6FD/mFHcAcD/mFAVANCTD+rFCTD+sFD/6FD/mFHNAND/mFD/kFD/kFD/lFCBDtD+4FD/jF9DB/+g6DYHVD/mF9DBBX9LDYHLD/kFCggEDtHMD9OAcAVD/mFALD/kFCTD+rFD9QHcANAVD/mFALD/kFCTD+rFAOAVD/mFALD/kFAMD9OD9QHVDQBFTtGEmYILPdKOenHLD8dBAEDBIBDyB+t+J83EBABCNJALD8dFAEDBIBDyF+t+J83EBAKAcAVDQNVi8ZcMpySQ8c8dfb8e8fHVD8dBAEDBIBDyG+t+J83EBABCiJAVD8dFAEDBIBDyE+t+J83EBABCAJRBAICIJHIAF9JQBMMM9jFF97GXAGCGrAF9sHG9FQBCBRFEXABABDBBBHECND+rFCND+sFD/6FAECiD+sFCnD+rFCUUU/8EDtD+uFD/mFDMBBABCTJRBAFCIJHFAG9JQBMMM9TFEaCBCB8oGUkUUBHFABCEJC98ZJHBjGUkUUBGXGXAB8/BCTWHGuQBCaREABAGlCggEJCTrXBCa6QFMAFREMAEMMMFBCUNMIT9tBB"; // Uses bulk-memory and simd extensions var detector = new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,3,2,0,0,5,3,1,0,1,12,1,0,10,22,2,12,0,65,0,65,0,65,0,252,10,0,0,11,7,0,65,0,253,15,26,11]); // Used to unpack wasm var wasmpack = new Uint8Array([32,0,65,253,3,1,2,34,4,106,6,5,11,8,7,20,13,33,12,16,128,9,116,64,19,113,127,15,10,21,22,14,255,66,24,54,136,107,18,23,192,26,114,118,132,17,77,101,130,144,27,87,131,44,45,74,156,154,70,167]); if (typeof WebAssembly !== 'object') { // This module requires WebAssembly to function return { supported: false, }; } var wasm = wasm_base; if (WebAssembly.validate(detector)) { wasm = wasm_simd; console.log("Warning: meshopt_decoder is using experimental SIMD support"); } var instance; var promise = WebAssembly.instantiate(unpack(wasm), {}) .then(function(result) { instance = result.instance; instance.exports.__wasm_call_ctors(); }); function unpack(data) { var result = new Uint8Array(data.length); for (var i = 0; i < data.length; ++i) { var ch = data.charCodeAt(i); result[i] = ch > 96 ? ch - 71 : ch > 64 ? ch - 65 : ch > 47 ? ch + 4 : ch > 46 ? 63 : 62; } var write = 0; for (var i = 0; i < data.length; ++i) { result[write++] = (result[i] < 60) ? wasmpack[result[i]] : (result[i] - 60) * 64 + result[++i]; } return result.buffer.slice(0, write); } function decode(fun, target, count, size, source, filter) { var sbrk = instance.exports.sbrk; var count4 = (count + 3) & ~3; // pad for SIMD filter var tp = sbrk(count4 * size); var sp = sbrk(source.length); var heap = new Uint8Array(instance.exports.memory.buffer); heap.set(source, sp); var res = fun(tp, count, size, sp, source.length); if (res == 0 && filter) { filter(tp, count4, size); } target.set(heap.subarray(tp, tp + count * size)); sbrk(tp - sbrk(0)); if (res != 0) { throw new Error("Malformed buffer data: " + res); } }; var filters = { // legacy index-based enums for glTF 0: "", 1: "meshopt_decodeFilterOct", 2: "meshopt_decodeFilterQuat", 3: "meshopt_decodeFilterExp", // string-based enums for glTF NONE: "", OCTAHEDRAL: "meshopt_decodeFilterOct", QUATERNION: "meshopt_decodeFilterQuat", EXPONENTIAL: "meshopt_decodeFilterExp", }; var decoders = { // legacy index-based enums for glTF 0: "meshopt_decodeVertexBuffer", 1: "meshopt_decodeIndexBuffer", 2: "meshopt_decodeIndexSequence", // string-based enums for glTF ATTRIBUTES: "meshopt_decodeVertexBuffer", TRIANGLES: "meshopt_decodeIndexBuffer", INDICES: "meshopt_decodeIndexSequence", }; return { ready: promise, supported: true, decodeVertexBuffer: function(target, count, size, source, filter) { decode(instance.exports.meshopt_decodeVertexBuffer, target, count, size, source, instance.exports[filters[filter]]); }, decodeIndexBuffer: function(target, count, size, source) { decode(instance.exports.meshopt_decodeIndexBuffer, target, count, size, source); }, decodeIndexSequence: function(target, count, size, source) { decode(instance.exports.meshopt_decodeIndexSequence, target, count, size, source); }, decodeGltfBuffer: function(target, count, size, source, mode, filter) { decode(instance.exports[decoders[mode]], target, count, size, source, instance.exports[filters[filter]]); } }; })(); var DDSLoader = function ( manager ) { CompressedTextureLoader.call( this, manager ); }; DDSLoader.prototype = Object.assign( Object.create( CompressedTextureLoader.prototype ), { constructor: DDSLoader, parse: function ( buffer, loadMipmaps ) { var dds = { mipmaps: [], width: 0, height: 0, format: null, mipmapCount: 1 }; // Adapted from @toji's DDS utils // https://github.com/toji/webgl-texture-utils/blob/master/texture-util/dds.js // All values and structures referenced from: // http://msdn.microsoft.com/en-us/library/bb943991.aspx/ var DDS_MAGIC = 0x20534444; // var DDSD_CAPS = 0x1; // var DDSD_HEIGHT = 0x2; // var DDSD_WIDTH = 0x4; // var DDSD_PITCH = 0x8; // var DDSD_PIXELFORMAT = 0x1000; var DDSD_MIPMAPCOUNT = 0x20000; // var DDSD_LINEARSIZE = 0x80000; // var DDSD_DEPTH = 0x800000; // var DDSCAPS_COMPLEX = 0x8; // var DDSCAPS_MIPMAP = 0x400000; // var DDSCAPS_TEXTURE = 0x1000; var DDSCAPS2_CUBEMAP = 0x200; var DDSCAPS2_CUBEMAP_POSITIVEX = 0x400; var DDSCAPS2_CUBEMAP_NEGATIVEX = 0x800; var DDSCAPS2_CUBEMAP_POSITIVEY = 0x1000; var DDSCAPS2_CUBEMAP_NEGATIVEY = 0x2000; var DDSCAPS2_CUBEMAP_POSITIVEZ = 0x4000; var DDSCAPS2_CUBEMAP_NEGATIVEZ = 0x8000; // var DDSCAPS2_VOLUME = 0x200000; // var DDPF_ALPHAPIXELS = 0x1; // var DDPF_ALPHA = 0x2; var DDPF_FOURCC = 0x4; // var DDPF_RGB = 0x40; // var DDPF_YUV = 0x200; // var DDPF_LUMINANCE = 0x20000; function fourCCToInt32( value ) { return value.charCodeAt( 0 ) + ( value.charCodeAt( 1 ) << 8 ) + ( value.charCodeAt( 2 ) << 16 ) + ( value.charCodeAt( 3 ) << 24 ); } function int32ToFourCC( value ) { return String.fromCharCode( value & 0xff, ( value >> 8 ) & 0xff, ( value >> 16 ) & 0xff, ( value >> 24 ) & 0xff ); } function loadARGBMip( buffer, dataOffset, width, height ) { var dataLength = width * height * 4; var srcBuffer = new Uint8Array( buffer, dataOffset, dataLength ); var byteArray = new Uint8Array( dataLength ); var dst = 0; var src = 0; for ( var y = 0; y < height; y ++ ) { for ( var x = 0; x < width; x ++ ) { var b = srcBuffer[ src ]; src ++; var g = srcBuffer[ src ]; src ++; var r = srcBuffer[ src ]; src ++; var a = srcBuffer[ src ]; src ++; byteArray[ dst ] = r; dst ++; //r byteArray[ dst ] = g; dst ++; //g byteArray[ dst ] = b; dst ++; //b byteArray[ dst ] = a; dst ++; //a } } return byteArray; } var FOURCC_DXT1 = fourCCToInt32( 'DXT1' ); var FOURCC_DXT3 = fourCCToInt32( 'DXT3' ); var FOURCC_DXT5 = fourCCToInt32( 'DXT5' ); var FOURCC_ETC1 = fourCCToInt32( 'ETC1' ); var headerLengthInt = 31; // The header length in 32 bit ints // Offsets into the header array var off_magic = 0; var off_size = 1; var off_flags = 2; var off_height = 3; var off_width = 4; var off_mipmapCount = 7; var off_pfFlags = 20; var off_pfFourCC = 21; var off_RGBBitCount = 22; var off_RBitMask = 23; var off_GBitMask = 24; var off_BBitMask = 25; var off_ABitMask = 26; // var off_caps = 27; var off_caps2 = 28; // var off_caps3 = 29; // var off_caps4 = 30; // Parse header var header = new Int32Array( buffer, 0, headerLengthInt ); if ( header[ off_magic ] !== DDS_MAGIC ) { console.error( 'THREE.DDSLoader.parse: Invalid magic number in DDS header.' ); return dds; } if ( ! header[ off_pfFlags ] & DDPF_FOURCC ) { console.error( 'THREE.DDSLoader.parse: Unsupported format, must contain a FourCC code.' ); return dds; } var blockBytes; var fourCC = header[ off_pfFourCC ]; var isRGBAUncompressed = false; switch ( fourCC ) { case FOURCC_DXT1: blockBytes = 8; dds.format = RGB_S3TC_DXT1_Format; break; case FOURCC_DXT3: blockBytes = 16; dds.format = RGBA_S3TC_DXT3_Format; break; case FOURCC_DXT5: blockBytes = 16; dds.format = RGBA_S3TC_DXT5_Format$1; break; case FOURCC_ETC1: blockBytes = 8; dds.format = RGB_ETC1_Format; break; default: if ( header[ off_RGBBitCount ] === 32 && header[ off_RBitMask ] & 0xff0000 && header[ off_GBitMask ] & 0xff00 && header[ off_BBitMask ] & 0xff && header[ off_ABitMask ] & 0xff000000 ) { isRGBAUncompressed = true; blockBytes = 64; dds.format = RGBAFormat; } else { console.error( 'THREE.DDSLoader.parse: Unsupported FourCC code ', int32ToFourCC( fourCC ) ); return dds; } } dds.mipmapCount = 1; if ( header[ off_flags ] & DDSD_MIPMAPCOUNT && loadMipmaps !== false ) { dds.mipmapCount = Math.max( 1, header[ off_mipmapCount ] ); } var caps2 = header[ off_caps2 ]; dds.isCubemap = caps2 & DDSCAPS2_CUBEMAP ? true : false; if ( dds.isCubemap && ( ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEX ) || ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEX ) || ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEY ) || ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEY ) || ! ( caps2 & DDSCAPS2_CUBEMAP_POSITIVEZ ) || ! ( caps2 & DDSCAPS2_CUBEMAP_NEGATIVEZ ) ) ) { console.error( 'THREE.DDSLoader.parse: Incomplete cubemap faces' ); return dds; } dds.width = header[ off_width ]; dds.height = header[ off_height ]; var dataOffset = header[ off_size ] + 4; // Extract mipmaps buffers var faces = dds.isCubemap ? 6 : 1; for ( var face = 0; face < faces; face ++ ) { var width = dds.width; var height = dds.height; for ( var i = 0; i < dds.mipmapCount; i ++ ) { if ( isRGBAUncompressed ) { var byteArray = loadARGBMip( buffer, dataOffset, width, height ); var dataLength = byteArray.length; } else { var dataLength = Math.max( 4, width ) / 4 * Math.max( 4, height ) / 4 * blockBytes; var byteArray = new Uint8Array( buffer, dataOffset, dataLength ); } var mipmap = { 'data': byteArray, 'width': width, 'height': height }; dds.mipmaps.push( mipmap ); dataOffset += dataLength; width = Math.max( width >> 1, 1 ); height = Math.max( height >> 1, 1 ); } } return dds; } } ); //xzw add:---- let unknownExtensions = {}; let oldSet = MeshBasicMaterial.prototype.setValues; MeshBasicMaterial.prototype.setValues = function(values){ return oldSet.call(this,values) }; class GLTFLoader extends Loader { constructor( manager, renderer, urlPrefix ) { super( manager ); //xzw add: this.dracoLoader = new DRACOLoader; this.ktx2Loader = new KTX2Loader; this.meshoptDecoder = MeshoptDecoder; this.ddsLoader = new DDSLoader; //这个没测过 //路径相对于index.html this.dracoLoader.setDecoderPath( urlPrefix + 'three.js/loaders/draco/' /*or 'https://unpkg.com/three@0.144.0/examples/js/libs/draco/gltf/' 版本可升级 */); //这两个路径可以自己改。在laser的环境也要放一份这个路径 this.ktx2Loader.setTranscoderPath(urlPrefix + 'three.js/loaders/basis/' /*or 'https://unpkg.com/three@0.144.0/examples/js/libs/basis/' 版本可升级 */).detectSupport( renderer ); this.pluginCallbacks = []; this.register( function ( parser ) { return new GLTFMaterialsClearcoatExtension( parser ); } ); this.register( function ( parser ) { return new GLTFTextureBasisUExtension( parser ); } ); this.register( function ( parser ) { return new GLTFTextureWebPExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMaterialsSheenExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMaterialsTransmissionExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMaterialsVolumeExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMaterialsIorExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMaterialsEmissiveStrengthExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMaterialsSpecularExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMaterialsIridescenceExtension( parser ); } ); this.register( function ( parser ) { return new GLTFLightsExtension( parser ); } ); this.register( function ( parser ) { return new GLTFMeshoptCompression( parser ); } ); this.register( function ( parser ) { return new GLTFMeshGpuInstancing( parser ); } ); } load( url, onLoad, onProgress, onError ) { const scope = this; let resourcePath; if ( this.resourcePath !== '' ) { resourcePath = this.resourcePath; } else if ( this.path !== '' ) { resourcePath = this.path; } else { resourcePath = LoaderUtils.extractUrlBase( url ); } // Tells the LoadingManager to track an extra item, which resolves after // the model is fully loaded. This means the count of items loaded will // be incorrect, but ensures manager.onLoad() does not fire early. this.manager.itemStart( url ); const _onError = function ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); scope.manager.itemEnd( url ); }; const loader = new FileLoader( this.manager ); loader.setPath( this.path ); loader.setResponseType( 'arraybuffer' ); loader.setRequestHeader( this.requestHeader ); loader.setWithCredentials( this.withCredentials ); loader.load( url, function ( data ) { try { scope.parse( data, resourcePath, function ( gltf ) { onLoad( gltf ); scope.manager.itemEnd( url ); }, _onError ); } catch ( e ) { _onError( e ); } }, onProgress, _onError ); } setDRACOLoader( dracoLoader ) { this.dracoLoader = dracoLoader; return this; } setDDSLoader() { throw new Error( 'THREE.GLTFLoader: "MSFT_texture_dds" no longer supported. Please update to "KHR_texture_basisu".' ); } setKTX2Loader( ktx2Loader ) { this.ktx2Loader = ktx2Loader; return this; } setMeshoptDecoder( meshoptDecoder ) { this.meshoptDecoder = meshoptDecoder; return this; } register( callback ) { if ( this.pluginCallbacks.indexOf( callback ) === - 1 ) { this.pluginCallbacks.push( callback ); } return this; } unregister( callback ) { if ( this.pluginCallbacks.indexOf( callback ) !== - 1 ) { this.pluginCallbacks.splice( this.pluginCallbacks.indexOf( callback ), 1 ); } return this; } parse( data, path, onLoad, onError ) { let json; const extensions = {}; const plugins = {}; if ( typeof data === 'string' ) { json = JSON.parse( data ); } else if ( data instanceof ArrayBuffer ) { const magic = LoaderUtils.decodeText( new Uint8Array( data, 0, 4 ) ); if ( magic === BINARY_EXTENSION_HEADER_MAGIC ) { try { extensions[ EXTENSIONS.KHR_BINARY_GLTF ] = new GLTFBinaryExtension( data ); } catch ( error ) { if ( onError ) onError( error ); return; } json = JSON.parse( extensions[ EXTENSIONS.KHR_BINARY_GLTF ].content ); } else { json = JSON.parse( LoaderUtils.decodeText( new Uint8Array( data ) ) ); } } else { json = data; } if ( json.asset === undefined || json.asset.version[ 0 ] < 2 ) { if ( onError ) onError( new Error( 'THREE.GLTFLoader: Unsupported asset. glTF versions >=2.0 are supported.' ) ); return; } const parser = new GLTFParser( json, { path: path || this.resourcePath || '', crossOrigin: this.crossOrigin, requestHeader: this.requestHeader, manager: this.manager, ktx2Loader: this.ktx2Loader, meshoptDecoder: this.meshoptDecoder } ); parser.fileLoader.setRequestHeader( this.requestHeader ); for ( let i = 0; i < this.pluginCallbacks.length; i ++ ) { const plugin = this.pluginCallbacks[ i ]( parser ); plugins[ plugin.name ] = plugin; // Workaround to avoid determining as unknown extension // in addUnknownExtensionsToUserData(). // Remove this workaround if we move all the existing // extension handlers to plugin system extensions[ plugin.name ] = true; } if ( json.extensionsUsed ) { for ( let i = 0; i < json.extensionsUsed.length; ++ i ) { const extensionName = json.extensionsUsed[ i ]; const extensionsRequired = json.extensionsRequired || []; switch ( extensionName ) { case EXTENSIONS.KHR_MATERIALS_UNLIT: extensions[ extensionName ] = new GLTFMaterialsUnlitExtension(); break; case EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: extensions[ extensionName ] = new GLTFMaterialsPbrSpecularGlossinessExtension(); break; case EXTENSIONS.KHR_DRACO_MESH_COMPRESSION: extensions[ extensionName ] = new GLTFDracoMeshCompressionExtension( json, this.dracoLoader ); break; case EXTENSIONS.KHR_TEXTURE_TRANSFORM: extensions[ extensionName ] = new GLTFTextureTransformExtension(); break; case EXTENSIONS.KHR_MESH_QUANTIZATION: extensions[ extensionName ] = new GLTFMeshQuantizationExtension(); break; default: if ( extensionsRequired.indexOf( extensionName ) >= 0 && plugins[ extensionName ] === undefined ) { if(!unknownExtensions[extensionName]){ console.warn( 'GLTFLoader: Unknown extension "' + extensionName + '".' ,'使用默认的KHR_materials_unlit材质' ); unknownExtensions[extensionName] = 1; } //xzw add: 默认 json.extensionsRequired = 'KHR_materials_unlit'; json.materials = [{ extensions: {KHR_materials_unlit: {}}, pbrMetallicRoughness:{ baseColorFactor:[1, 1, 1, 1], baseColorTexture: {index: 0, texCoord: 0}, metallicFactor: 0, roughnessFactor: 0.5, } }]; extensions[ json.extensionsRequired ] = new GLTFMaterialsUnlitExtension(); } /* 如原先: json.materials = KHR_techniques_webgl:{ technique: 0, values:{u_diffuse:{index: 0,texCoord: 0}} } */ } } } parser.setExtensions( extensions ); parser.setPlugins( plugins ); parser.parse( onLoad, onError ); } parseAsync( data, path ) { const scope = this; return new Promise( function ( resolve, reject ) { scope.parse( data, path, resolve, reject ); } ); } } /* GLTFREGISTRY */ function GLTFRegistry() { let objects = {}; return { get: function ( key ) { return objects[ key ]; }, add: function ( key, object ) { objects[ key ] = object; }, remove: function ( key ) { delete objects[ key ]; }, removeAll: function () { objects = {}; } }; } /*********************************/ /********** EXTENSIONS ***********/ /*********************************/ const EXTENSIONS = { KHR_BINARY_GLTF: 'KHR_binary_glTF', KHR_DRACO_MESH_COMPRESSION: 'KHR_draco_mesh_compression', KHR_LIGHTS_PUNCTUAL: 'KHR_lights_punctual', KHR_MATERIALS_CLEARCOAT: 'KHR_materials_clearcoat', KHR_MATERIALS_IOR: 'KHR_materials_ior', KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS: 'KHR_materials_pbrSpecularGlossiness', KHR_MATERIALS_SHEEN: 'KHR_materials_sheen', KHR_MATERIALS_SPECULAR: 'KHR_materials_specular', KHR_MATERIALS_TRANSMISSION: 'KHR_materials_transmission', KHR_MATERIALS_IRIDESCENCE: 'KHR_materials_iridescence', KHR_MATERIALS_UNLIT: 'KHR_materials_unlit', KHR_MATERIALS_VOLUME: 'KHR_materials_volume', KHR_TEXTURE_BASISU: 'KHR_texture_basisu', KHR_TEXTURE_TRANSFORM: 'KHR_texture_transform', KHR_MESH_QUANTIZATION: 'KHR_mesh_quantization', KHR_MATERIALS_EMISSIVE_STRENGTH: 'KHR_materials_emissive_strength', EXT_TEXTURE_WEBP: 'EXT_texture_webp', EXT_MESHOPT_COMPRESSION: 'EXT_meshopt_compression', EXT_MESH_GPU_INSTANCING: 'EXT_mesh_gpu_instancing' }; /** * Punctual Lights Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_lights_punctual */ class GLTFLightsExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_LIGHTS_PUNCTUAL; // Object3D instance caches this.cache = { refs: {}, uses: {} }; } _markDefs() { const parser = this.parser; const nodeDefs = this.parser.json.nodes || []; for ( let nodeIndex = 0, nodeLength = nodeDefs.length; nodeIndex < nodeLength; nodeIndex ++ ) { const nodeDef = nodeDefs[ nodeIndex ]; if ( nodeDef.extensions && nodeDef.extensions[ this.name ] && nodeDef.extensions[ this.name ].light !== undefined ) { parser._addNodeRef( this.cache, nodeDef.extensions[ this.name ].light ); } } } _loadLight( lightIndex ) { const parser = this.parser; const cacheKey = 'light:' + lightIndex; let dependency = parser.cache.get( cacheKey ); if ( dependency ) return dependency; const json = parser.json; const extensions = ( json.extensions && json.extensions[ this.name ] ) || {}; const lightDefs = extensions.lights || []; const lightDef = lightDefs[ lightIndex ]; let lightNode; const color = new Color( 0xffffff ); if ( lightDef.color !== undefined ) color.fromArray( lightDef.color ); const range = lightDef.range !== undefined ? lightDef.range : 0; switch ( lightDef.type ) { case 'directional': lightNode = new DirectionalLight( color ); lightNode.target.position.set( 0, 0, - 1 ); lightNode.add( lightNode.target ); break; case 'point': lightNode = new PointLight( color ); lightNode.distance = range; break; case 'spot': lightNode = new SpotLight( color ); lightNode.distance = range; // Handle spotlight properties. lightDef.spot = lightDef.spot || {}; lightDef.spot.innerConeAngle = lightDef.spot.innerConeAngle !== undefined ? lightDef.spot.innerConeAngle : 0; lightDef.spot.outerConeAngle = lightDef.spot.outerConeAngle !== undefined ? lightDef.spot.outerConeAngle : Math.PI / 4.0; lightNode.angle = lightDef.spot.outerConeAngle; lightNode.penumbra = 1.0 - lightDef.spot.innerConeAngle / lightDef.spot.outerConeAngle; lightNode.target.position.set( 0, 0, - 1 ); lightNode.add( lightNode.target ); break; default: throw new Error( 'THREE.GLTFLoader: Unexpected light type: ' + lightDef.type ); } // Some lights (e.g. spot) default to a position other than the origin. Reset the position // here, because node-level parsing will only override position if explicitly specified. lightNode.position.set( 0, 0, 0 ); lightNode.decay = 2; if ( lightDef.intensity !== undefined ) lightNode.intensity = lightDef.intensity; lightNode.name = parser.createUniqueName( lightDef.name || ( 'light_' + lightIndex ) ); dependency = Promise.resolve( lightNode ); parser.cache.add( cacheKey, dependency ); return dependency; } createNodeAttachment( nodeIndex ) { const self = this; const parser = this.parser; const json = parser.json; const nodeDef = json.nodes[ nodeIndex ]; const lightDef = ( nodeDef.extensions && nodeDef.extensions[ this.name ] ) || {}; const lightIndex = lightDef.light; if ( lightIndex === undefined ) return null; return this._loadLight( lightIndex ).then( function ( light ) { return parser._getNodeRef( self.cache, lightIndex, light ); } ); } } /** * Unlit Materials Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_unlit */ class GLTFMaterialsUnlitExtension { constructor() { this.name = EXTENSIONS.KHR_MATERIALS_UNLIT; } getMaterialType() { return MeshBasicMaterial; } extendParams( materialParams, materialDef, parser ) { const pending = []; materialParams.color = new Color( 1.0, 1.0, 1.0 ); materialParams.opacity = 1.0; const metallicRoughness = materialDef.pbrMetallicRoughness; if ( metallicRoughness ) { if ( Array.isArray( metallicRoughness.baseColorFactor ) ) { const array = metallicRoughness.baseColorFactor; materialParams.color.fromArray( array ); materialParams.opacity = array[ 3 ]; } if ( metallicRoughness.baseColorTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture, sRGBEncoding ) ); } } return Promise.all( pending ); } } /** * Materials Emissive Strength Extension * * Specification: https://github.com/KhronosGroup/glTF/blob/5768b3ce0ef32bc39cdf1bef10b948586635ead3/extensions/2.0/Khronos/KHR_materials_emissive_strength/README.md */ class GLTFMaterialsEmissiveStrengthExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_EMISSIVE_STRENGTH; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ] || parser.json.materials; //xzw change if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const emissiveStrength = materialDef.extensions[ this.name ].emissiveStrength; if ( emissiveStrength !== undefined ) { materialParams.emissiveIntensity = emissiveStrength; } return Promise.resolve(); } } /** * Clearcoat Materials Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_clearcoat */ class GLTFMaterialsClearcoatExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_CLEARCOAT; } getMaterialType( materialIndex ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ] || parser.json.materials; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; return MeshPhysicalMaterial; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; //曾因这里报错加了一句 || parser.json.materials; 但是加载的图会先变白 if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const pending = []; const extension = materialDef.extensions[ this.name ]; if ( extension.clearcoatFactor !== undefined ) { materialParams.clearcoat = extension.clearcoatFactor; } if ( extension.clearcoatTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'clearcoatMap', extension.clearcoatTexture ) ); } if ( extension.clearcoatRoughnessFactor !== undefined ) { materialParams.clearcoatRoughness = extension.clearcoatRoughnessFactor; } if ( extension.clearcoatRoughnessTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'clearcoatRoughnessMap', extension.clearcoatRoughnessTexture ) ); } if ( extension.clearcoatNormalTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'clearcoatNormalMap', extension.clearcoatNormalTexture ) ); if ( extension.clearcoatNormalTexture.scale !== undefined ) { const scale = extension.clearcoatNormalTexture.scale; materialParams.clearcoatNormalScale = new Vector2( scale, scale ); } } return Promise.all( pending ); } } /** * Iridescence Materials Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_iridescence */ class GLTFMaterialsIridescenceExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_IRIDESCENCE; } getMaterialType( materialIndex ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; return MeshPhysicalMaterial; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const pending = []; const extension = materialDef.extensions[ this.name ]; if ( extension.iridescenceFactor !== undefined ) { materialParams.iridescence = extension.iridescenceFactor; } if ( extension.iridescenceTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'iridescenceMap', extension.iridescenceTexture ) ); } if ( extension.iridescenceIor !== undefined ) { materialParams.iridescenceIOR = extension.iridescenceIor; } if ( materialParams.iridescenceThicknessRange === undefined ) { materialParams.iridescenceThicknessRange = [ 100, 400 ]; } if ( extension.iridescenceThicknessMinimum !== undefined ) { materialParams.iridescenceThicknessRange[ 0 ] = extension.iridescenceThicknessMinimum; } if ( extension.iridescenceThicknessMaximum !== undefined ) { materialParams.iridescenceThicknessRange[ 1 ] = extension.iridescenceThicknessMaximum; } if ( extension.iridescenceThicknessTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'iridescenceThicknessMap', extension.iridescenceThicknessTexture ) ); } return Promise.all( pending ); } } /** * Sheen Materials Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Khronos/KHR_materials_sheen */ class GLTFMaterialsSheenExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_SHEEN; } getMaterialType( materialIndex ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; return MeshPhysicalMaterial; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const pending = []; materialParams.sheenColor = new Color( 0, 0, 0 ); materialParams.sheenRoughness = 0; materialParams.sheen = 1; const extension = materialDef.extensions[ this.name ]; if ( extension.sheenColorFactor !== undefined ) { materialParams.sheenColor.fromArray( extension.sheenColorFactor ); } if ( extension.sheenRoughnessFactor !== undefined ) { materialParams.sheenRoughness = extension.sheenRoughnessFactor; } if ( extension.sheenColorTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'sheenColorMap', extension.sheenColorTexture, sRGBEncoding ) ); } if ( extension.sheenRoughnessTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'sheenRoughnessMap', extension.sheenRoughnessTexture ) ); } return Promise.all( pending ); } } /** * Transmission Materials Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_transmission * Draft: https://github.com/KhronosGroup/glTF/pull/1698 */ class GLTFMaterialsTransmissionExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_TRANSMISSION; } getMaterialType( materialIndex ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; return MeshPhysicalMaterial; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const pending = []; const extension = materialDef.extensions[ this.name ]; if ( extension.transmissionFactor !== undefined ) { materialParams.transmission = extension.transmissionFactor; } if ( extension.transmissionTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'transmissionMap', extension.transmissionTexture ) ); } return Promise.all( pending ); } } /** * Materials Volume Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_volume */ class GLTFMaterialsVolumeExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_VOLUME; } getMaterialType( materialIndex ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; return MeshPhysicalMaterial; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const pending = []; const extension = materialDef.extensions[ this.name ]; materialParams.thickness = extension.thicknessFactor !== undefined ? extension.thicknessFactor : 0; if ( extension.thicknessTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'thicknessMap', extension.thicknessTexture ) ); } materialParams.attenuationDistance = extension.attenuationDistance || Infinity; const colorArray = extension.attenuationColor || [ 1, 1, 1 ]; materialParams.attenuationColor = new Color( colorArray[ 0 ], colorArray[ 1 ], colorArray[ 2 ] ); return Promise.all( pending ); } } /** * Materials ior Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_ior */ class GLTFMaterialsIorExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_IOR; } getMaterialType( materialIndex ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; return MeshPhysicalMaterial; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const extension = materialDef.extensions[ this.name ]; materialParams.ior = extension.ior !== undefined ? extension.ior : 1.5; return Promise.resolve(); } } /** * Materials specular Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_materials_specular */ class GLTFMaterialsSpecularExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_MATERIALS_SPECULAR; } getMaterialType( materialIndex ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) return null; return MeshPhysicalMaterial; } extendMaterialParams( materialIndex, materialParams ) { const parser = this.parser; const materialDef = parser.json.materials[ materialIndex ]; if ( ! materialDef.extensions || ! materialDef.extensions[ this.name ] ) { return Promise.resolve(); } const pending = []; const extension = materialDef.extensions[ this.name ]; materialParams.specularIntensity = extension.specularFactor !== undefined ? extension.specularFactor : 1.0; if ( extension.specularTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'specularIntensityMap', extension.specularTexture ) ); } const colorArray = extension.specularColorFactor || [ 1, 1, 1 ]; materialParams.specularColor = new Color( colorArray[ 0 ], colorArray[ 1 ], colorArray[ 2 ] ); if ( extension.specularColorTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'specularColorMap', extension.specularColorTexture, sRGBEncoding ) ); } return Promise.all( pending ); } } /** * BasisU Texture Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_texture_basisu */ class GLTFTextureBasisUExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.KHR_TEXTURE_BASISU; } loadTexture( textureIndex ) { const parser = this.parser; const json = parser.json; const textureDef = json.textures[ textureIndex ]; if ( ! textureDef.extensions || ! textureDef.extensions[ this.name ] ) { return null; } const extension = textureDef.extensions[ this.name ]; const loader = parser.options.ktx2Loader; if ( ! loader ) { if ( json.extensionsRequired && json.extensionsRequired.indexOf( this.name ) >= 0 ) { throw new Error( 'THREE.GLTFLoader: setKTX2Loader must be called before loading KTX2 textures' ); } else { // Assumes that the extension is optional and that a fallback texture is present return null; } } return parser.loadTextureImage( textureIndex, extension.source, loader ); } } /** * WebP Texture Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_texture_webp */ class GLTFTextureWebPExtension { constructor( parser ) { this.parser = parser; this.name = EXTENSIONS.EXT_TEXTURE_WEBP; this.isSupported = null; } loadTexture( textureIndex ) { const name = this.name; const parser = this.parser; const json = parser.json; const textureDef = json.textures[ textureIndex ]; if ( ! textureDef.extensions || ! textureDef.extensions[ name ] ) { return null; } const extension = textureDef.extensions[ name ]; const source = json.images[ extension.source ]; let loader = parser.textureLoader; if ( source.uri ) { const handler = parser.options.manager.getHandler( source.uri ); if ( handler !== null ) loader = handler; } return this.detectSupport().then( function ( isSupported ) { if ( isSupported ) return parser.loadTextureImage( textureIndex, extension.source, loader ); if ( json.extensionsRequired && json.extensionsRequired.indexOf( name ) >= 0 ) { throw new Error( 'THREE.GLTFLoader: WebP required by asset but unsupported.' ); } // Fall back to PNG or JPEG. return parser.loadTexture( textureIndex ); } ); } detectSupport() { if ( ! this.isSupported ) { this.isSupported = new Promise( function ( resolve ) { const image = new Image(); // Lossy test image. Support for lossy images doesn't guarantee support for all // WebP images, unfortunately. image.src = 'data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA'; image.onload = image.onerror = function () { resolve( image.height === 1 ); }; } ); } return this.isSupported; } } /** * meshopt BufferView Compression Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_meshopt_compression */ class GLTFMeshoptCompression { constructor( parser ) { this.name = EXTENSIONS.EXT_MESHOPT_COMPRESSION; this.parser = parser; } loadBufferView( index ) { const json = this.parser.json; const bufferView = json.bufferViews[ index ]; if ( bufferView.extensions && bufferView.extensions[ this.name ] ) { const extensionDef = bufferView.extensions[ this.name ]; const buffer = this.parser.getDependency( 'buffer', extensionDef.buffer ); const decoder = this.parser.options.meshoptDecoder; if ( ! decoder || ! decoder.supported ) { if ( json.extensionsRequired && json.extensionsRequired.indexOf( this.name ) >= 0 ) { throw new Error( 'THREE.GLTFLoader: setMeshoptDecoder must be called before loading compressed files' ); } else { // Assumes that the extension is optional and that fallback buffer data is present return null; } } return buffer.then( function ( res ) { const byteOffset = extensionDef.byteOffset || 0; const byteLength = extensionDef.byteLength || 0; const count = extensionDef.count; const stride = extensionDef.byteStride; const source = new Uint8Array( res, byteOffset, byteLength ); if ( decoder.decodeGltfBufferAsync ) { return decoder.decodeGltfBufferAsync( count, stride, source, extensionDef.mode, extensionDef.filter ).then( function ( res ) { return res.buffer; } ); } else { // Support for MeshoptDecoder 0.18 or earlier, without decodeGltfBufferAsync return decoder.ready.then( function () { const result = new ArrayBuffer( count * stride ); decoder.decodeGltfBuffer( new Uint8Array( result ), count, stride, source, extensionDef.mode, extensionDef.filter ); return result; } ); } } ); } else { return null; } } } /** * GPU Instancing Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Vendor/EXT_mesh_gpu_instancing * */ class GLTFMeshGpuInstancing { constructor( parser ) { this.name = EXTENSIONS.EXT_MESH_GPU_INSTANCING; this.parser = parser; } createNodeMesh( nodeIndex ) { const json = this.parser.json; const nodeDef = json.nodes[ nodeIndex ]; if ( ! nodeDef.extensions || ! nodeDef.extensions[ this.name ] || nodeDef.mesh === undefined ) { return null; } const meshDef = json.meshes[ nodeDef.mesh ]; // No Points or Lines + Instancing support yet for ( const primitive of meshDef.primitives ) { if ( primitive.mode !== WEBGL_CONSTANTS.TRIANGLES && primitive.mode !== WEBGL_CONSTANTS.TRIANGLE_STRIP && primitive.mode !== WEBGL_CONSTANTS.TRIANGLE_FAN && primitive.mode !== undefined ) { return null; } } const extensionDef = nodeDef.extensions[ this.name ]; const attributesDef = extensionDef.attributes; // @TODO: Can we support InstancedMesh + SkinnedMesh? const pending = []; const attributes = {}; for ( const key in attributesDef ) { pending.push( this.parser.getDependency( 'accessor', attributesDef[ key ] ).then( accessor => { attributes[ key ] = accessor; return attributes[ key ]; } ) ); } if ( pending.length < 1 ) { return null; } pending.push( this.parser.createNodeMesh( nodeIndex ) ); return Promise.all( pending ).then( results => { const nodeObject = results.pop(); const meshes = nodeObject.isGroup ? nodeObject.children : [ nodeObject ]; const count = results[ 0 ].count; // All attribute counts should be same const instancedMeshes = []; for ( const mesh of meshes ) { // Temporal variables const m = new Matrix4(); const p = new Vector3(); const q = new Quaternion(); const s = new Vector3( 1, 1, 1 ); const instancedMesh = new InstancedMesh( mesh.geometry, mesh.material, count ); for ( let i = 0; i < count; i ++ ) { if ( attributes.TRANSLATION ) { p.fromBufferAttribute( attributes.TRANSLATION, i ); } if ( attributes.ROTATION ) { q.fromBufferAttribute( attributes.ROTATION, i ); } if ( attributes.SCALE ) { s.fromBufferAttribute( attributes.SCALE, i ); } instancedMesh.setMatrixAt( i, m.compose( p, q, s ) ); } // Add instance attributes to the geometry, excluding TRS. for ( const attributeName in attributes ) { if ( attributeName !== 'TRANSLATION' && attributeName !== 'ROTATION' && attributeName !== 'SCALE' ) { mesh.geometry.setAttribute( attributeName, attributes[ attributeName ] ); } } // Just in case Object3D.prototype.copy.call( instancedMesh, mesh ); // https://github.com/mrdoob/three.js/issues/18334 instancedMesh.frustumCulled = false; this.parser.assignFinalMaterial( instancedMesh ); instancedMeshes.push( instancedMesh ); } if ( nodeObject.isGroup ) { nodeObject.clear(); nodeObject.add( ... instancedMeshes ); return nodeObject; } return instancedMeshes[ 0 ]; } ); } } /* BINARY EXTENSION */ const BINARY_EXTENSION_HEADER_MAGIC = 'glTF'; const BINARY_EXTENSION_HEADER_LENGTH = 12; const BINARY_EXTENSION_CHUNK_TYPES = { JSON: 0x4E4F534A, BIN: 0x004E4942 }; class GLTFBinaryExtension { constructor( data ) { this.name = EXTENSIONS.KHR_BINARY_GLTF; this.content = null; this.body = null; const headerView = new DataView( data, 0, BINARY_EXTENSION_HEADER_LENGTH ); this.header = { magic: LoaderUtils.decodeText( new Uint8Array( data.slice( 0, 4 ) ) ), version: headerView.getUint32( 4, true ), length: headerView.getUint32( 8, true ) }; if ( this.header.magic !== BINARY_EXTENSION_HEADER_MAGIC ) { throw new Error( 'THREE.GLTFLoader: Unsupported glTF-Binary header.' ); } else if ( this.header.version < 2.0 ) { throw new Error( 'THREE.GLTFLoader: Legacy binary file detected.' ); } const chunkContentsLength = this.header.length - BINARY_EXTENSION_HEADER_LENGTH; const chunkView = new DataView( data, BINARY_EXTENSION_HEADER_LENGTH ); let chunkIndex = 0; while ( chunkIndex < chunkContentsLength ) { const chunkLength = chunkView.getUint32( chunkIndex, true ); chunkIndex += 4; const chunkType = chunkView.getUint32( chunkIndex, true ); chunkIndex += 4; if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.JSON ) { const contentArray = new Uint8Array( data, BINARY_EXTENSION_HEADER_LENGTH + chunkIndex, chunkLength ); this.content = LoaderUtils.decodeText( contentArray ); } else if ( chunkType === BINARY_EXTENSION_CHUNK_TYPES.BIN ) { const byteOffset = BINARY_EXTENSION_HEADER_LENGTH + chunkIndex; this.body = data.slice( byteOffset, byteOffset + chunkLength ); } // Clients must ignore chunks with unknown types. chunkIndex += chunkLength; } if ( this.content === null ) { throw new Error( 'THREE.GLTFLoader: JSON content not found.' ); } } } /** * DRACO Mesh Compression Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_draco_mesh_compression */ class GLTFDracoMeshCompressionExtension { constructor( json, dracoLoader ) { if ( ! dracoLoader ) { throw new Error( 'THREE.GLTFLoader: No DRACOLoader instance provided.' ); } this.name = EXTENSIONS.KHR_DRACO_MESH_COMPRESSION; this.json = json; this.dracoLoader = dracoLoader; this.dracoLoader.preload(); } decodePrimitive( primitive, parser ) { const json = this.json; const dracoLoader = this.dracoLoader; const bufferViewIndex = primitive.extensions[ this.name ].bufferView; const gltfAttributeMap = primitive.extensions[ this.name ].attributes; const threeAttributeMap = {}; const attributeNormalizedMap = {}; const attributeTypeMap = {}; for ( const attributeName in gltfAttributeMap ) { const threeAttributeName = ATTRIBUTES[ attributeName ] || attributeName.toLowerCase(); threeAttributeMap[ threeAttributeName ] = gltfAttributeMap[ attributeName ]; } for ( const attributeName in primitive.attributes ) { const threeAttributeName = ATTRIBUTES[ attributeName ] || attributeName.toLowerCase(); if ( gltfAttributeMap[ attributeName ] !== undefined ) { const accessorDef = json.accessors[ primitive.attributes[ attributeName ] ]; const componentType = WEBGL_COMPONENT_TYPES[ accessorDef.componentType ]; attributeTypeMap[ threeAttributeName ] = componentType.name; attributeNormalizedMap[ threeAttributeName ] = accessorDef.normalized === true; } } return parser.getDependency( 'bufferView', bufferViewIndex ).then( function ( bufferView ) { return new Promise( function ( resolve ) { dracoLoader.decodeDracoFile( bufferView, function ( geometry ) { for ( const attributeName in geometry.attributes ) { const attribute = geometry.attributes[ attributeName ]; const normalized = attributeNormalizedMap[ attributeName ]; if ( normalized !== undefined ) attribute.normalized = normalized; } resolve( geometry ); }, threeAttributeMap, attributeTypeMap ); } ); } ); } } /** * Texture Transform Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_texture_transform */ class GLTFTextureTransformExtension { constructor() { this.name = EXTENSIONS.KHR_TEXTURE_TRANSFORM; } extendTexture( texture, transform ) { if ( transform.texCoord !== undefined ) { console.warn( 'THREE.GLTFLoader: Custom UV sets in "' + this.name + '" extension not yet supported.' ); } if ( transform.offset === undefined && transform.rotation === undefined && transform.scale === undefined ) { // See https://github.com/mrdoob/three.js/issues/21819. return texture; } texture = texture.clone(); if ( transform.offset !== undefined ) { texture.offset.fromArray( transform.offset ); } if ( transform.rotation !== undefined ) { texture.rotation = transform.rotation; } if ( transform.scale !== undefined ) { texture.repeat.fromArray( transform.scale ); } texture.needsUpdate = true; return texture; } } /** * Specular-Glossiness Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/main/extensions/2.0/Archived/KHR_materials_pbrSpecularGlossiness */ /** * A sub class of StandardMaterial with some of the functionality * changed via the `onBeforeCompile` callback * @pailhead */ class GLTFMeshStandardSGMaterial extends MeshStandardMaterial { constructor( params ) { super(); this.isGLTFSpecularGlossinessMaterial = true; //various chunks that need replacing const specularMapParsFragmentChunk = [ '#ifdef USE_SPECULARMAP', ' uniform sampler2D specularMap;', '#endif' ].join( '\n' ); const glossinessMapParsFragmentChunk = [ '#ifdef USE_GLOSSINESSMAP', ' uniform sampler2D glossinessMap;', '#endif' ].join( '\n' ); const specularMapFragmentChunk = [ 'vec3 specularFactor = specular;', '#ifdef USE_SPECULARMAP', ' vec4 texelSpecular = texture2D( specularMap, vUv );', ' // reads channel RGB, compatible with a glTF Specular-Glossiness (RGBA) texture', ' specularFactor *= texelSpecular.rgb;', '#endif' ].join( '\n' ); const glossinessMapFragmentChunk = [ 'float glossinessFactor = glossiness;', '#ifdef USE_GLOSSINESSMAP', ' vec4 texelGlossiness = texture2D( glossinessMap, vUv );', ' // reads channel A, compatible with a glTF Specular-Glossiness (RGBA) texture', ' glossinessFactor *= texelGlossiness.a;', '#endif' ].join( '\n' ); const lightPhysicalFragmentChunk = [ 'PhysicalMaterial material;', 'material.diffuseColor = diffuseColor.rgb * ( 1. - max( specularFactor.r, max( specularFactor.g, specularFactor.b ) ) );', 'vec3 dxy = max( abs( dFdx( geometryNormal ) ), abs( dFdy( geometryNormal ) ) );', 'float geometryRoughness = max( max( dxy.x, dxy.y ), dxy.z );', 'material.roughness = max( 1.0 - glossinessFactor, 0.0525 ); // 0.0525 corresponds to the base mip of a 256 cubemap.', 'material.roughness += geometryRoughness;', 'material.roughness = min( material.roughness, 1.0 );', 'material.specularColor = specularFactor;', ].join( '\n' ); const uniforms = { specular: { value: new Color().setHex( 0xffffff ) }, glossiness: { value: 1 }, specularMap: { value: null }, glossinessMap: { value: null } }; this._extraUniforms = uniforms; this.onBeforeCompile = function ( shader ) { for ( const uniformName in uniforms ) { shader.uniforms[ uniformName ] = uniforms[ uniformName ]; } shader.fragmentShader = shader.fragmentShader .replace( 'uniform float roughness;', 'uniform vec3 specular;' ) .replace( 'uniform float metalness;', 'uniform float glossiness;' ) .replace( '#include ', specularMapParsFragmentChunk ) .replace( '#include ', glossinessMapParsFragmentChunk ) .replace( '#include ', specularMapFragmentChunk ) .replace( '#include ', glossinessMapFragmentChunk ) .replace( '#include ', lightPhysicalFragmentChunk ); }; Object.defineProperties( this, { specular: { get: function () { return uniforms.specular.value; }, set: function ( v ) { uniforms.specular.value = v; } }, specularMap: { get: function () { return uniforms.specularMap.value; }, set: function ( v ) { uniforms.specularMap.value = v; if ( v ) { this.defines.USE_SPECULARMAP = ''; // USE_UV is set by the renderer for specular maps } else { delete this.defines.USE_SPECULARMAP; } } }, glossiness: { get: function () { return uniforms.glossiness.value; }, set: function ( v ) { uniforms.glossiness.value = v; } }, glossinessMap: { get: function () { return uniforms.glossinessMap.value; }, set: function ( v ) { uniforms.glossinessMap.value = v; if ( v ) { this.defines.USE_GLOSSINESSMAP = ''; this.defines.USE_UV = ''; } else { delete this.defines.USE_GLOSSINESSMAP; delete this.defines.USE_UV; } } } } ); delete this.metalness; delete this.roughness; delete this.metalnessMap; delete this.roughnessMap; this.setValues( params ); } copy( source ) { super.copy( source ); this.specularMap = source.specularMap; this.specular.copy( source.specular ); this.glossinessMap = source.glossinessMap; this.glossiness = source.glossiness; delete this.metalness; delete this.roughness; delete this.metalnessMap; delete this.roughnessMap; return this; } } class GLTFMaterialsPbrSpecularGlossinessExtension { constructor() { this.name = EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS; this.specularGlossinessParams = [ 'color', 'map', 'lightMap', 'lightMapIntensity', 'aoMap', 'aoMapIntensity', 'emissive', 'emissiveIntensity', 'emissiveMap', 'bumpMap', 'bumpScale', 'normalMap', 'normalMapType', 'displacementMap', 'displacementScale', 'displacementBias', 'specularMap', 'specular', 'glossinessMap', 'glossiness', 'alphaMap', 'envMap', 'envMapIntensity' ]; } getMaterialType() { return GLTFMeshStandardSGMaterial; } extendParams( materialParams, materialDef, parser ) { const pbrSpecularGlossiness = materialDef.extensions[ this.name ]; materialParams.color = new Color( 1.0, 1.0, 1.0 ); materialParams.opacity = 1.0; const pending = []; if ( Array.isArray( pbrSpecularGlossiness.diffuseFactor ) ) { const array = pbrSpecularGlossiness.diffuseFactor; materialParams.color.fromArray( array ); materialParams.opacity = array[ 3 ]; } if ( pbrSpecularGlossiness.diffuseTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'map', pbrSpecularGlossiness.diffuseTexture, sRGBEncoding ) ); } materialParams.emissive = new Color( 0.0, 0.0, 0.0 ); materialParams.glossiness = pbrSpecularGlossiness.glossinessFactor !== undefined ? pbrSpecularGlossiness.glossinessFactor : 1.0; materialParams.specular = new Color( 1.0, 1.0, 1.0 ); if ( Array.isArray( pbrSpecularGlossiness.specularFactor ) ) { materialParams.specular.fromArray( pbrSpecularGlossiness.specularFactor ); } if ( pbrSpecularGlossiness.specularGlossinessTexture !== undefined ) { const specGlossMapDef = pbrSpecularGlossiness.specularGlossinessTexture; pending.push( parser.assignTexture( materialParams, 'glossinessMap', specGlossMapDef ) ); pending.push( parser.assignTexture( materialParams, 'specularMap', specGlossMapDef, sRGBEncoding ) ); } return Promise.all( pending ); } createMaterial( materialParams ) { const material = new GLTFMeshStandardSGMaterial( materialParams ); material.fog = true; material.color = materialParams.color; material.map = materialParams.map === undefined ? null : materialParams.map; material.lightMap = null; material.lightMapIntensity = 1.0; material.aoMap = materialParams.aoMap === undefined ? null : materialParams.aoMap; material.aoMapIntensity = 1.0; material.emissive = materialParams.emissive; material.emissiveIntensity = materialParams.emissiveIntensity === undefined ? 1.0 : materialParams.emissiveIntensity; material.emissiveMap = materialParams.emissiveMap === undefined ? null : materialParams.emissiveMap; material.bumpMap = materialParams.bumpMap === undefined ? null : materialParams.bumpMap; material.bumpScale = 1; material.normalMap = materialParams.normalMap === undefined ? null : materialParams.normalMap; material.normalMapType = TangentSpaceNormalMap; if ( materialParams.normalScale ) material.normalScale = materialParams.normalScale; material.displacementMap = null; material.displacementScale = 1; material.displacementBias = 0; material.specularMap = materialParams.specularMap === undefined ? null : materialParams.specularMap; material.specular = materialParams.specular; material.glossinessMap = materialParams.glossinessMap === undefined ? null : materialParams.glossinessMap; material.glossiness = materialParams.glossiness; material.alphaMap = null; material.envMap = materialParams.envMap === undefined ? null : materialParams.envMap; material.envMapIntensity = 1.0; return material; } } /** * Mesh Quantization Extension * * Specification: https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization */ class GLTFMeshQuantizationExtension { constructor() { this.name = EXTENSIONS.KHR_MESH_QUANTIZATION; } } /*********************************/ /********** INTERPOLATION ********/ /*********************************/ // Spline Interpolation // Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#appendix-c-spline-interpolation class GLTFCubicSplineInterpolant extends Interpolant { constructor( parameterPositions, sampleValues, sampleSize, resultBuffer ) { super( parameterPositions, sampleValues, sampleSize, resultBuffer ); } copySampleValue_( index ) { // Copies a sample value to the result buffer. See description of glTF // CUBICSPLINE values layout in interpolate_() function below. const result = this.resultBuffer, values = this.sampleValues, valueSize = this.valueSize, offset = index * valueSize * 3 + valueSize; for ( let i = 0; i !== valueSize; i ++ ) { result[ i ] = values[ offset + i ]; } return result; } interpolate_( i1, t0, t, t1 ) { const result = this.resultBuffer; const values = this.sampleValues; const stride = this.valueSize; const stride2 = stride * 2; const stride3 = stride * 3; const td = t1 - t0; const p = ( t - t0 ) / td; const pp = p * p; const ppp = pp * p; const offset1 = i1 * stride3; const offset0 = offset1 - stride3; const s2 = - 2 * ppp + 3 * pp; const s3 = ppp - pp; const s0 = 1 - s2; const s1 = s3 - pp + p; // Layout of keyframe output values for CUBICSPLINE animations: // [ inTangent_1, splineVertex_1, outTangent_1, inTangent_2, splineVertex_2, ... ] for ( let i = 0; i !== stride; i ++ ) { const p0 = values[ offset0 + i + stride ]; // splineVertex_k const m0 = values[ offset0 + i + stride2 ] * td; // outTangent_k * (t_k+1 - t_k) const p1 = values[ offset1 + i + stride ]; // splineVertex_k+1 const m1 = values[ offset1 + i ] * td; // inTangent_k+1 * (t_k+1 - t_k) result[ i ] = s0 * p0 + s1 * m0 + s2 * p1 + s3 * m1; } return result; } } const _q = new Quaternion(); class GLTFCubicSplineQuaternionInterpolant extends GLTFCubicSplineInterpolant { interpolate_( i1, t0, t, t1 ) { const result = super.interpolate_( i1, t0, t, t1 ); _q.fromArray( result ).normalize().toArray( result ); return result; } } /*********************************/ /********** INTERNALS ************/ /*********************************/ /* CONSTANTS */ const WEBGL_CONSTANTS = { FLOAT: 5126, //FLOAT_MAT2: 35674, FLOAT_MAT3: 35675, FLOAT_MAT4: 35676, FLOAT_VEC2: 35664, FLOAT_VEC3: 35665, FLOAT_VEC4: 35666, LINEAR: 9729, REPEAT: 10497, SAMPLER_2D: 35678, POINTS: 0, LINES: 1, LINE_LOOP: 2, LINE_STRIP: 3, TRIANGLES: 4, TRIANGLE_STRIP: 5, TRIANGLE_FAN: 6, UNSIGNED_BYTE: 5121, UNSIGNED_SHORT: 5123 }; const WEBGL_COMPONENT_TYPES = { 5120: Int8Array, 5121: Uint8Array, 5122: Int16Array, 5123: Uint16Array, 5125: Uint32Array, 5126: Float32Array }; const WEBGL_FILTERS = { 9728: NearestFilter, 9729: LinearFilter, 9984: NearestMipmapNearestFilter, 9985: LinearMipmapNearestFilter, 9986: NearestMipmapLinearFilter, 9987: LinearMipmapLinearFilter }; const WEBGL_WRAPPINGS = { 33071: ClampToEdgeWrapping, 33648: MirroredRepeatWrapping, 10497: RepeatWrapping }; const WEBGL_TYPE_SIZES = { 'SCALAR': 1, 'VEC2': 2, 'VEC3': 3, 'VEC4': 4, 'MAT2': 4, 'MAT3': 9, 'MAT4': 16 }; const ATTRIBUTES = { POSITION: 'position', NORMAL: 'normal', TANGENT: 'tangent', TEXCOORD_0: 'uv', TEXCOORD_1: 'uv2', COLOR_0: 'color', WEIGHTS_0: 'skinWeight', JOINTS_0: 'skinIndex', }; const PATH_PROPERTIES = { scale: 'scale', translation: 'position', rotation: 'quaternion', weights: 'morphTargetInfluences' }; const INTERPOLATION = { CUBICSPLINE: undefined, // We use a custom interpolant (GLTFCubicSplineInterpolation) for CUBICSPLINE tracks. Each // keyframe track will be initialized with a default interpolation type, then modified. LINEAR: InterpolateLinear, STEP: InterpolateDiscrete }; const ALPHA_MODES = { OPAQUE: 'OPAQUE', MASK: 'MASK', BLEND: 'BLEND' }; /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#default-material */ function createDefaultMaterial( cache ) { if ( cache[ 'DefaultMaterial' ] === undefined ) { cache[ 'DefaultMaterial' ] = new MeshStandardMaterial( { color: 0xFFFFFF, emissive: 0x000000, metalness: 1, roughness: 1, transparent: false, depthTest: true, side: FrontSide } ); } return cache[ 'DefaultMaterial' ]; } function addUnknownExtensionsToUserData( knownExtensions, object, objectDef ) { // Add unknown glTF extensions to an object's userData. for ( const name in objectDef.extensions ) { if ( knownExtensions[ name ] === undefined ) { object.userData.gltfExtensions = object.userData.gltfExtensions || {}; object.userData.gltfExtensions[ name ] = objectDef.extensions[ name ]; } } } /** * @param {Object3D|Material|BufferGeometry} object * @param {GLTF.definition} gltfDef */ function assignExtrasToUserData( object, gltfDef ) { if ( gltfDef.extras !== undefined ) { if ( typeof gltfDef.extras === 'object' ) { Object.assign( object.userData, gltfDef.extras ); } else { console.warn( 'THREE.GLTFLoader: Ignoring primitive type .extras, ' + gltfDef.extras ); } } } /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#morph-targets * * @param {BufferGeometry} geometry * @param {Array} targets * @param {GLTFParser} parser * @return {Promise} */ function addMorphTargets( geometry, targets, parser ) { let hasMorphPosition = false; let hasMorphNormal = false; let hasMorphColor = false; for ( let i = 0, il = targets.length; i < il; i ++ ) { const target = targets[ i ]; if ( target.POSITION !== undefined ) hasMorphPosition = true; if ( target.NORMAL !== undefined ) hasMorphNormal = true; if ( target.COLOR_0 !== undefined ) hasMorphColor = true; if ( hasMorphPosition && hasMorphNormal && hasMorphColor ) break; } if ( ! hasMorphPosition && ! hasMorphNormal && ! hasMorphColor ) return Promise.resolve( geometry ); const pendingPositionAccessors = []; const pendingNormalAccessors = []; const pendingColorAccessors = []; for ( let i = 0, il = targets.length; i < il; i ++ ) { const target = targets[ i ]; if ( hasMorphPosition ) { const pendingAccessor = target.POSITION !== undefined ? parser.getDependency( 'accessor', target.POSITION ) : geometry.attributes.position; pendingPositionAccessors.push( pendingAccessor ); } if ( hasMorphNormal ) { const pendingAccessor = target.NORMAL !== undefined ? parser.getDependency( 'accessor', target.NORMAL ) : geometry.attributes.normal; pendingNormalAccessors.push( pendingAccessor ); } if ( hasMorphColor ) { const pendingAccessor = target.COLOR_0 !== undefined ? parser.getDependency( 'accessor', target.COLOR_0 ) : geometry.attributes.color; pendingColorAccessors.push( pendingAccessor ); } } return Promise.all( [ Promise.all( pendingPositionAccessors ), Promise.all( pendingNormalAccessors ), Promise.all( pendingColorAccessors ) ] ).then( function ( accessors ) { const morphPositions = accessors[ 0 ]; const morphNormals = accessors[ 1 ]; const morphColors = accessors[ 2 ]; if ( hasMorphPosition ) geometry.morphAttributes.position = morphPositions; if ( hasMorphNormal ) geometry.morphAttributes.normal = morphNormals; if ( hasMorphColor ) geometry.morphAttributes.color = morphColors; geometry.morphTargetsRelative = true; return geometry; } ); } /** * @param {Mesh} mesh * @param {GLTF.Mesh} meshDef */ function updateMorphTargets( mesh, meshDef ) { mesh.updateMorphTargets(); if ( meshDef.weights !== undefined ) { for ( let i = 0, il = meshDef.weights.length; i < il; i ++ ) { mesh.morphTargetInfluences[ i ] = meshDef.weights[ i ]; } } // .extras has user-defined data, so check that .extras.targetNames is an array. if ( meshDef.extras && Array.isArray( meshDef.extras.targetNames ) ) { const targetNames = meshDef.extras.targetNames; if ( mesh.morphTargetInfluences.length === targetNames.length ) { mesh.morphTargetDictionary = {}; for ( let i = 0, il = targetNames.length; i < il; i ++ ) { mesh.morphTargetDictionary[ targetNames[ i ] ] = i; } } else { console.warn( 'THREE.GLTFLoader: Invalid extras.targetNames length. Ignoring names.' ); } } } function createPrimitiveKey( primitiveDef ) { const dracoExtension = primitiveDef.extensions && primitiveDef.extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ]; let geometryKey; if ( dracoExtension ) { geometryKey = 'draco:' + dracoExtension.bufferView + ':' + dracoExtension.indices + ':' + createAttributesKey( dracoExtension.attributes ); } else { geometryKey = primitiveDef.indices + ':' + createAttributesKey( primitiveDef.attributes ) + ':' + primitiveDef.mode; } return geometryKey; } function createAttributesKey( attributes ) { let attributesKey = ''; const keys = Object.keys( attributes ).sort(); for ( let i = 0, il = keys.length; i < il; i ++ ) { attributesKey += keys[ i ] + ':' + attributes[ keys[ i ] ] + ';'; } return attributesKey; } function getNormalizedComponentScale( constructor ) { // Reference: // https://github.com/KhronosGroup/glTF/tree/master/extensions/2.0/Khronos/KHR_mesh_quantization#encoding-quantized-data switch ( constructor ) { case Int8Array: return 1 / 127; case Uint8Array: return 1 / 255; case Int16Array: return 1 / 32767; case Uint16Array: return 1 / 65535; default: throw new Error( 'THREE.GLTFLoader: Unsupported normalized accessor component type.' ); } } function getImageURIMimeType( uri ) { if ( uri.search( /\.jpe?g($|\?)/i ) > 0 || uri.search( /^data\:image\/jpeg/ ) === 0 ) return 'image/jpeg'; if ( uri.search( /\.webp($|\?)/i ) > 0 || uri.search( /^data\:image\/webp/ ) === 0 ) return 'image/webp'; return 'image/png'; } /* GLTF PARSER */ class GLTFParser { constructor( json = {}, options = {} ) { this.json = json; this.extensions = {}; this.plugins = {}; this.options = options; // loader object cache this.cache = new GLTFRegistry(); // associations between Three.js objects and glTF elements this.associations = new Map(); // BufferGeometry caching this.primitiveCache = {}; // Object3D instance caches this.meshCache = { refs: {}, uses: {} }; this.cameraCache = { refs: {}, uses: {} }; this.lightCache = { refs: {}, uses: {} }; this.sourceCache = {}; this.textureCache = {}; // Track node names, to ensure no duplicates this.nodeNamesUsed = {}; // Use an ImageBitmapLoader if imageBitmaps are supported. Moves much of the // expensive work of uploading a texture to the GPU off the main thread. const isSafari = /^((?!chrome|android).)*safari/i.test( navigator.userAgent ) === true; const isFirefox = navigator.userAgent.indexOf( 'Firefox' ) > - 1; const firefoxVersion = isFirefox ? navigator.userAgent.match( /Firefox\/([0-9]+)\./ )[ 1 ] : - 1; // if ( typeof createImageBitmap === 'undefined' || isSafari || ( isFirefox && firefoxVersion < 98 ) ) { this.textureLoader = new TextureLoader( this.options.manager ); /*} else { //为了防止chrome出现报错 The source image could not be decoded. 导致reject,改用TextureLoader by xzw this.textureLoader = new ImageBitmapLoader( this.options.manager ); } */ this.textureLoader.setCrossOrigin( this.options.crossOrigin ); this.textureLoader.setRequestHeader( this.options.requestHeader ); this.fileLoader = new FileLoader( this.options.manager ); this.fileLoader.setResponseType( 'arraybuffer' ); if ( this.options.crossOrigin === 'use-credentials' ) { this.fileLoader.setWithCredentials( true ); } } setExtensions( extensions ) { this.extensions = extensions; } setPlugins( plugins ) { this.plugins = plugins; } parse( onLoad, onError ) { const parser = this; const json = this.json; const extensions = this.extensions; // Clear the loader cache this.cache.removeAll(); // Mark the special nodes/meshes in json for efficient parse this._invokeAll( function ( ext ) { return ext._markDefs && ext._markDefs(); } ); Promise.all( this._invokeAll( function ( ext ) { return ext.beforeRoot && ext.beforeRoot(); } ) ).then( function () { return Promise.all( [ parser.getDependencies( 'scene' ), parser.getDependencies( 'animation' ), parser.getDependencies( 'camera' ), ] ); } ).then( function ( dependencies ) { const result = { scene: dependencies[ 0 ][ json.scene || 0 ], scenes: dependencies[ 0 ], animations: dependencies[ 1 ], cameras: dependencies[ 2 ], asset: json.asset, parser: parser, userData: {} }; addUnknownExtensionsToUserData( extensions, result, json ); assignExtrasToUserData( result, json ); Promise.all( parser._invokeAll( function ( ext ) { return ext.afterRoot && ext.afterRoot( result ); } ) ).then( function () { onLoad( result ); } ); } ).catch( onError ); } /** * Marks the special nodes/meshes in json for efficient parse. */ _markDefs() { const nodeDefs = this.json.nodes || []; const skinDefs = this.json.skins || []; const meshDefs = this.json.meshes || []; // Nothing in the node definition indicates whether it is a Bone or an // Object3D. Use the skins' joint references to mark bones. for ( let skinIndex = 0, skinLength = skinDefs.length; skinIndex < skinLength; skinIndex ++ ) { const joints = skinDefs[ skinIndex ].joints; for ( let i = 0, il = joints.length; i < il; i ++ ) { nodeDefs[ joints[ i ] ].isBone = true; } } // Iterate over all nodes, marking references to shared resources, // as well as skeleton joints. for ( let nodeIndex = 0, nodeLength = nodeDefs.length; nodeIndex < nodeLength; nodeIndex ++ ) { const nodeDef = nodeDefs[ nodeIndex ]; if ( nodeDef.mesh !== undefined ) { this._addNodeRef( this.meshCache, nodeDef.mesh ); // Nothing in the mesh definition indicates whether it is // a SkinnedMesh or Mesh. Use the node's mesh reference // to mark SkinnedMesh if node has skin. if ( nodeDef.skin !== undefined ) { meshDefs[ nodeDef.mesh ].isSkinnedMesh = true; } } if ( nodeDef.camera !== undefined ) { this._addNodeRef( this.cameraCache, nodeDef.camera ); } } } /** * Counts references to shared node / Object3D resources. These resources * can be reused, or "instantiated", at multiple nodes in the scene * hierarchy. Mesh, Camera, and Light instances are instantiated and must * be marked. Non-scenegraph resources (like Materials, Geometries, and * Textures) can be reused directly and are not marked here. * * Example: CesiumMilkTruck sample model reuses "Wheel" meshes. */ _addNodeRef( cache, index ) { if ( index === undefined ) return; if ( cache.refs[ index ] === undefined ) { cache.refs[ index ] = cache.uses[ index ] = 0; } cache.refs[ index ] ++; } /** Returns a reference to a shared resource, cloning it if necessary. */ _getNodeRef( cache, index, object ) { if ( cache.refs[ index ] <= 1 ) return object; const ref = object.clone(); // Propagates mappings to the cloned object, prevents mappings on the // original object from being lost. const updateMappings = ( original, clone ) => { const mappings = this.associations.get( original ); if ( mappings != null ) { this.associations.set( clone, mappings ); } for ( const [ i, child ] of original.children.entries() ) { updateMappings( child, clone.children[ i ] ); } }; updateMappings( object, ref ); ref.name += '_instance_' + ( cache.uses[ index ] ++ ); return ref; } _invokeOne( func ) { const extensions = Object.values( this.plugins ); extensions.push( this ); for ( let i = 0; i < extensions.length; i ++ ) { const result = func( extensions[ i ] ); if ( result ) return result; } return null; } _invokeAll( func ) { const extensions = Object.values( this.plugins ); extensions.unshift( this ); const pending = []; for ( let i = 0; i < extensions.length; i ++ ) { const result = func( extensions[ i ] ); if ( result ) pending.push( result ); } return pending; } /** * Requests the specified dependency asynchronously, with caching. * @param {string} type * @param {number} index * @return {Promise} */ getDependency( type, index ) { const cacheKey = type + ':' + index; let dependency = this.cache.get( cacheKey ); if ( ! dependency ) { switch ( type ) { case 'scene': dependency = this.loadScene( index ); break; case 'node': dependency = this.loadNode( index ); break; case 'mesh': dependency = this._invokeOne( function ( ext ) { return ext.loadMesh && ext.loadMesh( index ); } ); break; case 'accessor': dependency = this.loadAccessor( index ); break; case 'bufferView': dependency = this._invokeOne( function ( ext ) { return ext.loadBufferView && ext.loadBufferView( index ); } ); break; case 'buffer': dependency = this.loadBuffer( index ); break; case 'material': dependency = this._invokeOne( function ( ext ) { return ext.loadMaterial && ext.loadMaterial( index ); } ); break; case 'texture': dependency = this._invokeOne( function ( ext ) { return ext.loadTexture && ext.loadTexture( index ); } ); break; case 'skin': dependency = this.loadSkin( index ); break; case 'animation': dependency = this._invokeOne( function ( ext ) { return ext.loadAnimation && ext.loadAnimation( index ); } ); break; case 'camera': dependency = this.loadCamera( index ); break; default: throw new Error( 'Unknown type: ' + type ); } this.cache.add( cacheKey, dependency ); } return dependency; } /** * Requests all dependencies of the specified type asynchronously, with caching. * @param {string} type * @return {Promise>} */ getDependencies( type ) { let dependencies = this.cache.get( type ); if ( ! dependencies ) { const parser = this; const defs = this.json[ type + ( type === 'mesh' ? 'es' : 's' ) ] || []; dependencies = Promise.all( defs.map( function ( def, index ) { return parser.getDependency( type, index ); } ) ); this.cache.add( type, dependencies ); } return dependencies; } /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views * @param {number} bufferIndex * @return {Promise} */ loadBuffer( bufferIndex ) { const bufferDef = this.json.buffers[ bufferIndex ]; const loader = this.fileLoader; if ( bufferDef.type && bufferDef.type !== 'arraybuffer' ) { throw new Error( 'THREE.GLTFLoader: ' + bufferDef.type + ' buffer type is not supported.' ); } // If present, GLB container is required to be the first buffer. if ( bufferDef.uri === undefined && bufferIndex === 0 ) { return Promise.resolve( this.extensions[ EXTENSIONS.KHR_BINARY_GLTF ].body ); } const options = this.options; return new Promise( function ( resolve, reject ) { loader.load( LoaderUtils.resolveURL( bufferDef.uri, options.path ), resolve, undefined, function () { reject( new Error( 'THREE.GLTFLoader: Failed to load buffer "' + bufferDef.uri + '".' ) ); } ); } ); } /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#buffers-and-buffer-views * @param {number} bufferViewIndex * @return {Promise} */ loadBufferView( bufferViewIndex ) { const bufferViewDef = this.json.bufferViews[ bufferViewIndex ]; return this.getDependency( 'buffer', bufferViewDef.buffer ).then( function ( buffer ) { const byteLength = bufferViewDef.byteLength || 0; const byteOffset = bufferViewDef.byteOffset || 0; return buffer.slice( byteOffset, byteOffset + byteLength ); } ); } /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#accessors * @param {number} accessorIndex * @return {Promise} */ loadAccessor( accessorIndex ) { const parser = this; const json = this.json; const accessorDef = this.json.accessors[ accessorIndex ]; if ( accessorDef.bufferView === undefined && accessorDef.sparse === undefined ) { // Ignore empty accessors, which may be used to declare runtime // information about attributes coming from another source (e.g. Draco // compression extension). return Promise.resolve( null ); } const pendingBufferViews = []; if ( accessorDef.bufferView !== undefined ) { pendingBufferViews.push( this.getDependency( 'bufferView', accessorDef.bufferView ) ); } else { pendingBufferViews.push( null ); } if ( accessorDef.sparse !== undefined ) { pendingBufferViews.push( this.getDependency( 'bufferView', accessorDef.sparse.indices.bufferView ) ); pendingBufferViews.push( this.getDependency( 'bufferView', accessorDef.sparse.values.bufferView ) ); } return Promise.all( pendingBufferViews ).then( function ( bufferViews ) { const bufferView = bufferViews[ 0 ]; const itemSize = WEBGL_TYPE_SIZES[ accessorDef.type ]; const TypedArray = WEBGL_COMPONENT_TYPES[ accessorDef.componentType ]; // For VEC3: itemSize is 3, elementBytes is 4, itemBytes is 12. const elementBytes = TypedArray.BYTES_PER_ELEMENT; const itemBytes = elementBytes * itemSize; const byteOffset = accessorDef.byteOffset || 0; const byteStride = accessorDef.bufferView !== undefined ? json.bufferViews[ accessorDef.bufferView ].byteStride : undefined; const normalized = accessorDef.normalized === true; let array, bufferAttribute; // The buffer is not interleaved if the stride is the item size in bytes. if ( byteStride && byteStride !== itemBytes ) { // Each "slice" of the buffer, as defined by 'count' elements of 'byteStride' bytes, gets its own InterleavedBuffer // This makes sure that IBA.count reflects accessor.count properly const ibSlice = Math.floor( byteOffset / byteStride ); const ibCacheKey = 'InterleavedBuffer:' + accessorDef.bufferView + ':' + accessorDef.componentType + ':' + ibSlice + ':' + accessorDef.count; let ib = parser.cache.get( ibCacheKey ); if ( ! ib ) { array = new TypedArray( bufferView, ibSlice * byteStride, accessorDef.count * byteStride / elementBytes ); // Integer parameters to IB/IBA are in array elements, not bytes. ib = new InterleavedBuffer( array, byteStride / elementBytes ); parser.cache.add( ibCacheKey, ib ); } bufferAttribute = new InterleavedBufferAttribute( ib, itemSize, ( byteOffset % byteStride ) / elementBytes, normalized ); } else { if ( bufferView === null ) { array = new TypedArray( accessorDef.count * itemSize ); } else { array = new TypedArray( bufferView, byteOffset, accessorDef.count * itemSize ); } bufferAttribute = new BufferAttribute( array, itemSize, normalized ); } // https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#sparse-accessors if ( accessorDef.sparse !== undefined ) { const itemSizeIndices = WEBGL_TYPE_SIZES.SCALAR; const TypedArrayIndices = WEBGL_COMPONENT_TYPES[ accessorDef.sparse.indices.componentType ]; const byteOffsetIndices = accessorDef.sparse.indices.byteOffset || 0; const byteOffsetValues = accessorDef.sparse.values.byteOffset || 0; const sparseIndices = new TypedArrayIndices( bufferViews[ 1 ], byteOffsetIndices, accessorDef.sparse.count * itemSizeIndices ); const sparseValues = new TypedArray( bufferViews[ 2 ], byteOffsetValues, accessorDef.sparse.count * itemSize ); if ( bufferView !== null ) { // Avoid modifying the original ArrayBuffer, if the bufferView wasn't initialized with zeroes. bufferAttribute = new BufferAttribute( bufferAttribute.array.slice(), bufferAttribute.itemSize, bufferAttribute.normalized ); } for ( let i = 0, il = sparseIndices.length; i < il; i ++ ) { const index = sparseIndices[ i ]; bufferAttribute.setX( index, sparseValues[ i * itemSize ] ); if ( itemSize >= 2 ) bufferAttribute.setY( index, sparseValues[ i * itemSize + 1 ] ); if ( itemSize >= 3 ) bufferAttribute.setZ( index, sparseValues[ i * itemSize + 2 ] ); if ( itemSize >= 4 ) bufferAttribute.setW( index, sparseValues[ i * itemSize + 3 ] ); if ( itemSize >= 5 ) throw new Error( 'THREE.GLTFLoader: Unsupported itemSize in sparse BufferAttribute.' ); } } return bufferAttribute; } ); } /** * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#textures * @param {number} textureIndex * @return {Promise} */ loadTexture( textureIndex ) { const json = this.json; const options = this.options; const textureDef = json.textures[ textureIndex ]; const sourceIndex = textureDef.source; const sourceDef = json.images[ sourceIndex ]; let loader = this.textureLoader; if ( sourceDef.uri ) { const handler = options.manager.getHandler( sourceDef.uri ); if ( handler !== null ) loader = handler; } return this.loadTextureImage( textureIndex, sourceIndex, loader ); } loadTextureImage( textureIndex, sourceIndex, loader ) { const parser = this; const json = this.json; const textureDef = json.textures[ textureIndex ]; const sourceDef = json.images[ sourceIndex ]; const cacheKey = ( sourceDef.uri || sourceDef.bufferView ) + ':' + textureDef.sampler; if ( this.textureCache[ cacheKey ] ) { // See https://github.com/mrdoob/three.js/issues/21559. return this.textureCache[ cacheKey ]; } const promise = this.loadImageSource( sourceIndex, loader ).then( function ( texture ) { texture.flipY = false; texture.name = textureDef.name || sourceDef.name || ''; const samplers = json.samplers || {}; const sampler = samplers[ textureDef.sampler ] || {}; Potree.Utils.makeTexDontResize(texture); //add /* //xzw 删除设置,因为已经makeTexDontResize texture.magFilter = WEBGL_FILTERS[ sampler.magFilter ] || LinearFilter; texture.minFilter = WEBGL_FILTERS[ sampler.minFilter ] || LinearMipmapLinearFilter; texture.wrapS = WEBGL_WRAPPINGS[ sampler.wrapS ] || RepeatWrapping; texture.wrapT = WEBGL_WRAPPINGS[ sampler.wrapT ] || RepeatWrapping; */ parser.associations.set( texture, { textures: textureIndex } ); return texture; } ).catch( function () { return null; } ); this.textureCache[ cacheKey ] = promise; return promise; } loadImageSource( sourceIndex, loader ) { const parser = this; const json = this.json; const options = this.options; if ( this.sourceCache[ sourceIndex ] !== undefined ) { return this.sourceCache[ sourceIndex ].then( ( texture ) => texture.clone() ); } const sourceDef = json.images[ sourceIndex ]; const URL = self.URL || self.webkitURL; let sourceURI = sourceDef.uri || ''; let isObjectURL = false; if ( sourceDef.bufferView !== undefined ) { // Load binary image data from bufferView, if provided. sourceURI = parser.getDependency( 'bufferView', sourceDef.bufferView ).then( function ( bufferView ) { isObjectURL = true; const blob = new Blob( [ bufferView ], { type: sourceDef.mimeType } ); sourceURI = URL.createObjectURL( blob ); return sourceURI; } ); } else if ( sourceDef.uri === undefined ) { throw new Error( 'THREE.GLTFLoader: Image ' + sourceIndex + ' is missing URI and bufferView' ); } const promise = Promise.resolve( sourceURI ).then( function ( sourceURI ) { return new Promise( function ( resolve, reject ) { if(json.asset.generator == 'gltfpack 0.18'){ //4dkk场景的模型 let onLoad = resolve; if ( loader.isImageBitmapLoader === true ) { onLoad = function ( imageBitmap ) { const texture = new Texture( imageBitmap ); texture.needsUpdate = true; resolve( texture ); }; } //loader.load( LoaderUtils.resolveURL( sourceURI, options.path ), onLoad, undefined, reject ); let url = parser.textureLoader.manager.resolveURL( sourceURI, options.path ); loader.load(url, onLoad, undefined, reject ); }else { //为了防止chrome出现报错 The source image could not be decoded. 导致reject,重新写贴图加载方式: xzw //但不知道为何有的场景(如4dkk的)不成功 parser.textureLoader.load(sourceURI, (tex)=>{ THREE.LinearMipmapLinearFilter; //原本:NearestMipMapNearestFilter 闪烁 有一个block文件离远了有裂缝,只能使用LinearFilter,但是这样似乎更卡,且锯齿 /* tex.minFilter = THREE.LinearFilter tex.generateMipmaps = false */ //console.log(tex.image.width, tex.image.height) resolve(tex); },null,(e)=>{ console.log('error load tex',e); }); } } ); } ).then( function ( texture ) { // Clean up resources and configure Texture. if ( isObjectURL === true ) { URL.revokeObjectURL( sourceURI ); } texture.userData = {};//xzw add texture.userData.mimeType = sourceDef.mimeType || getImageURIMimeType( sourceDef.uri || sourceDef.mimeType ); return texture; } ).catch( function ( error ) { console.error( 'THREE.GLTFLoader: Couldn\'t load texture', sourceURI ); throw error; } ); this.sourceCache[ sourceIndex ] = promise; return promise; } /** * Asynchronously assigns a texture to the given material parameters. * @param {Object} materialParams * @param {string} mapName * @param {Object} mapDef * @return {Promise} */ assignTexture( materialParams, mapName, mapDef, encoding ) { const parser = this; return this.getDependency( 'texture', mapDef.index ).then( function ( texture ) { // Materials sample aoMap from UV set 1 and other maps from UV set 0 - this can't be configured // However, we will copy UV set 0 to UV set 1 on demand for aoMap if ( mapDef.texCoord !== undefined && mapDef.texCoord != 0 && ! ( mapName === 'aoMap' && mapDef.texCoord == 1 ) ) { console.warn( 'THREE.GLTFLoader: Custom UV set ' + mapDef.texCoord + ' for texture ' + mapName + ' not yet supported.' ); } if ( parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] ) { const transform = mapDef.extensions !== undefined ? mapDef.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ] : undefined; if ( transform ) { const gltfReference = parser.associations.get( texture ); texture = parser.extensions[ EXTENSIONS.KHR_TEXTURE_TRANSFORM ].extendTexture( texture, transform ); parser.associations.set( texture, gltfReference ); } } if ( encoding !== undefined ) { texture.encoding = encoding; } materialParams[ mapName ] = texture; return texture; } ); } /** * Assigns final material to a Mesh, Line, or Points instance. The instance * already has a material (generated from the glTF material options alone) * but reuse of the same glTF material may require multiple threejs materials * to accommodate different primitive types, defines, etc. New materials will * be created if necessary, and reused from a cache. * @param {Object3D} mesh Mesh, Line, or Points instance. */ assignFinalMaterial( mesh ) { const geometry = mesh.geometry; let material = mesh.material; const useDerivativeTangents = geometry.attributes.tangent === undefined; const useVertexColors = geometry.attributes.color !== undefined; const useFlatShading = geometry.attributes.normal === undefined; if ( mesh.isPoints ) { const cacheKey = 'PointsMaterial:' + material.uuid; let pointsMaterial = this.cache.get( cacheKey ); if ( ! pointsMaterial ) { pointsMaterial = new PointsMaterial(); Material.prototype.copy.call( pointsMaterial, material ); pointsMaterial.color.copy( material.color ); pointsMaterial.map = material.map; pointsMaterial.sizeAttenuation = false; // glTF spec says points should be 1px this.cache.add( cacheKey, pointsMaterial ); } material = pointsMaterial; } else if ( mesh.isLine ) { const cacheKey = 'LineBasicMaterial:' + material.uuid; let lineMaterial = this.cache.get( cacheKey ); if ( ! lineMaterial ) { lineMaterial = new LineBasicMaterial(); Material.prototype.copy.call( lineMaterial, material ); lineMaterial.color.copy( material.color ); this.cache.add( cacheKey, lineMaterial ); } material = lineMaterial; } // Clone the material if it will be modified if ( useDerivativeTangents || useVertexColors || useFlatShading ) { let cacheKey = 'ClonedMaterial:' + material.uuid + ':'; if ( material.isGLTFSpecularGlossinessMaterial ) cacheKey += 'specular-glossiness:'; if ( useDerivativeTangents ) cacheKey += 'derivative-tangents:'; if ( useVertexColors ) cacheKey += 'vertex-colors:'; if ( useFlatShading ) cacheKey += 'flat-shading:'; let cachedMaterial = this.cache.get( cacheKey ); if ( ! cachedMaterial ) { cachedMaterial = material.clone(); if ( useVertexColors ) cachedMaterial.vertexColors = true; if ( useFlatShading ) cachedMaterial.flatShading = true; if ( useDerivativeTangents ) { // https://github.com/mrdoob/three.js/issues/11438#issuecomment-507003995 if ( cachedMaterial.normalScale ) cachedMaterial.normalScale.y *= - 1; if ( cachedMaterial.clearcoatNormalScale ) cachedMaterial.clearcoatNormalScale.y *= - 1; } this.cache.add( cacheKey, cachedMaterial ); this.associations.set( cachedMaterial, this.associations.get( material ) ); } material = cachedMaterial; } // workarounds for mesh and geometry if ( material.aoMap && geometry.attributes.uv2 === undefined && geometry.attributes.uv !== undefined ) { geometry.setAttribute( 'uv2', geometry.attributes.uv ); } mesh.material = material; } getMaterialType( /* materialIndex */ ) { return MeshStandardMaterial; } /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#materials * @param {number} materialIndex * @return {Promise} */ loadMaterial( materialIndex ) { const parser = this; const json = this.json; const extensions = this.extensions; const materialDef = json.materials[ materialIndex ]; let materialType; const materialParams = {}; const materialExtensions = materialDef.extensions || {}; const pending = []; if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ] ) { const sgExtension = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ]; materialType = BasicMaterial;//sgExtension.getMaterialType(); //xzw 改 pending.push( sgExtension.extendParams( materialParams, materialDef, parser ) ); } else if ( materialExtensions[ EXTENSIONS.KHR_MATERIALS_UNLIT ] ) { const kmuExtension = extensions[ EXTENSIONS.KHR_MATERIALS_UNLIT ]; materialType = BasicMaterial;//kmuExtension.getMaterialType(); //xzw 改 pending.push( kmuExtension.extendParams( materialParams, materialDef, parser ) ); } else { // Specification: // https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#metallic-roughness-material const metallicRoughness = materialDef.pbrMetallicRoughness || {}; materialParams.color = new Color( 1.0, 1.0, 1.0 ); materialParams.opacity = 1.0; if ( Array.isArray( metallicRoughness.baseColorFactor ) ) { const array = metallicRoughness.baseColorFactor; materialParams.color.fromArray( array ); materialParams.opacity = array[ 3 ]; } if ( metallicRoughness.baseColorTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'map', metallicRoughness.baseColorTexture, sRGBEncoding ) ); } materialParams.metalness = metallicRoughness.metallicFactor !== undefined ? metallicRoughness.metallicFactor : 1.0; materialParams.roughness = metallicRoughness.roughnessFactor !== undefined ? metallicRoughness.roughnessFactor : 1.0; if ( metallicRoughness.metallicRoughnessTexture !== undefined ) { pending.push( parser.assignTexture( materialParams, 'metalnessMap', metallicRoughness.metallicRoughnessTexture ) ); pending.push( parser.assignTexture( materialParams, 'roughnessMap', metallicRoughness.metallicRoughnessTexture ) ); } materialType = this._invokeOne( function ( ext ) { //return BasicMaterial //2024.7放弃使用这个,因为纯色的看上去连成一片 //kmuExtension.getMaterialType(); //xzw 改 return ext.getMaterialType && ext.getMaterialType( materialIndex ); } ); pending.push( Promise.all( this._invokeAll( function ( ext ) { return ext.extendMaterialParams && ext.extendMaterialParams( materialIndex, materialParams ); } ) ) ); } if ( materialDef.doubleSided === true ) { materialParams.side = DoubleSide; } const alphaMode = materialDef.alphaMode || ALPHA_MODES.OPAQUE; if ( alphaMode === ALPHA_MODES.BLEND ) { materialParams.transparent = true; // See: https://github.com/mrdoob/three.js/issues/17706 materialParams.depthWrite = false; } else { materialParams.transparent = false; if ( alphaMode === ALPHA_MODES.MASK ) { materialParams.alphaTest = materialDef.alphaCutoff !== undefined ? materialDef.alphaCutoff : 0.5; } } if ( materialDef.normalTexture !== undefined && materialType !== MeshBasicMaterial ) { pending.push( parser.assignTexture( materialParams, 'normalMap', materialDef.normalTexture ) ); materialParams.normalScale = new Vector2( 1, 1 ); if ( materialDef.normalTexture.scale !== undefined ) { const scale = materialDef.normalTexture.scale; materialParams.normalScale.set( scale, scale ); } } if ( materialDef.occlusionTexture !== undefined && materialType !== MeshBasicMaterial ) { pending.push( parser.assignTexture( materialParams, 'aoMap', materialDef.occlusionTexture ) ); if ( materialDef.occlusionTexture.strength !== undefined ) { materialParams.aoMapIntensity = materialDef.occlusionTexture.strength; } } if ( materialDef.emissiveFactor !== undefined && materialType !== MeshBasicMaterial ) { materialParams.emissive = new Color().fromArray( materialDef.emissiveFactor ); } if ( materialDef.emissiveTexture !== undefined && materialType !== MeshBasicMaterial ) { pending.push( parser.assignTexture( materialParams, 'emissiveMap', materialDef.emissiveTexture, sRGBEncoding ) ); } return Promise.all( pending ).then( function () { let material; if ( materialType === GLTFMeshStandardSGMaterial ) { material = extensions[ EXTENSIONS.KHR_MATERIALS_PBR_SPECULAR_GLOSSINESS ].createMaterial( materialParams ); } else { material = new materialType( materialParams ); } if ( materialDef.name ) material.name = materialDef.name; assignExtrasToUserData( material, materialDef ); parser.associations.set( material, { materials: materialIndex } ); if ( materialDef.extensions ) addUnknownExtensionsToUserData( extensions, material, materialDef ); return material; } ); } /** When Object3D instances are targeted by animation, they need unique names. */ createUniqueName( originalName ) { const sanitizedName = PropertyBinding.sanitizeNodeName( originalName || '' ); let name = sanitizedName; for ( let i = 1; this.nodeNamesUsed[ name ]; ++ i ) { name = sanitizedName + '_' + i; } this.nodeNamesUsed[ name ] = true; return name; } /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#geometry * * Creates BufferGeometries from primitives. * * @param {Array} primitives * @return {Promise>} */ loadGeometries( primitives ) { const parser = this; const extensions = this.extensions; const cache = this.primitiveCache; function createDracoPrimitive( primitive ) { return extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ] .decodePrimitive( primitive, parser ) .then( function ( geometry ) { return addPrimitiveAttributes( geometry, primitive, parser ); } ); } const pending = []; for ( let i = 0, il = primitives.length; i < il; i ++ ) { const primitive = primitives[ i ]; const cacheKey = createPrimitiveKey( primitive ); // See if we've already created this geometry const cached = cache[ cacheKey ]; if ( cached ) { // Use the cached geometry if it exists pending.push( cached.promise ); } else { let geometryPromise; if ( primitive.extensions && primitive.extensions[ EXTENSIONS.KHR_DRACO_MESH_COMPRESSION ] ) { // Use DRACO geometry if available geometryPromise = createDracoPrimitive( primitive ); } else { // Otherwise create a new geometry geometryPromise = addPrimitiveAttributes( new BufferGeometry(), primitive, parser ); } // Cache this geometry cache[ cacheKey ] = { primitive: primitive, promise: geometryPromise }; pending.push( geometryPromise ); } } return Promise.all( pending ); } /** * Specification: https://github.com/KhronosGroup/glTF/blob/master/specification/2.0/README.md#meshes * @param {number} meshIndex * @return {Promise} */ loadMesh( meshIndex ) { const parser = this; const json = this.json; const extensions = this.extensions; const meshDef = json.meshes[ meshIndex ]; const primitives = meshDef.primitives; const pending = []; for ( let i = 0, il = primitives.length; i < il; i ++ ) { const material = primitives[ i ].material === undefined ? createDefaultMaterial( this.cache ) : this.getDependency( 'material', primitives[ i ].material ); pending.push( material ); } pending.push( parser.loadGeometries( primitives ) ); return Promise.all( pending ).then( function ( results ) { const materials = results.slice( 0, results.length - 1 ); const geometries = results[ results.length - 1 ]; const meshes = []; for ( let i = 0, il = geometries.length; i < il; i ++ ) { const geometry = geometries[ i ]; const primitive = primitives[ i ]; // 1. create Mesh let mesh; const material = materials[ i ]; if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLES || primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP || primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN || primitive.mode === undefined ) { // .isSkinnedMesh isn't in glTF spec. See ._markDefs() mesh = meshDef.isSkinnedMesh === true ? new SkinnedMesh( geometry, material ) : new Mesh( geometry, material ); if ( mesh.isSkinnedMesh === true && ! mesh.geometry.attributes.skinWeight.normalized ) { // we normalize floating point skin weight array to fix malformed assets (see #15319) // it's important to skip this for non-float32 data since normalizeSkinWeights assumes non-normalized inputs mesh.normalizeSkinWeights(); } if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_STRIP ) { mesh.geometry = toTrianglesDrawMode( mesh.geometry, TriangleStripDrawMode ); } else if ( primitive.mode === WEBGL_CONSTANTS.TRIANGLE_FAN ) { mesh.geometry = toTrianglesDrawMode( mesh.geometry, TriangleFanDrawMode ); } } else if ( primitive.mode === WEBGL_CONSTANTS.LINES ) { mesh = new LineSegments( geometry, material ); } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_STRIP ) { mesh = new Line( geometry, material ); } else if ( primitive.mode === WEBGL_CONSTANTS.LINE_LOOP ) { mesh = new LineLoop( geometry, material ); } else if ( primitive.mode === WEBGL_CONSTANTS.POINTS ) { mesh = new Points( geometry, material ); } else { throw new Error( 'THREE.GLTFLoader: Primitive mode unsupported: ' + primitive.mode ); } if ( Object.keys( mesh.geometry.morphAttributes ).length > 0 ) { updateMorphTargets( mesh, meshDef ); } mesh.name = parser.createUniqueName( meshDef.name || ( 'mesh_' + meshIndex ) ); assignExtrasToUserData( mesh, meshDef ); if ( primitive.extensions ) addUnknownExtensionsToUserData( extensions, mesh, primitive ); parser.assignFinalMaterial( mesh ); meshes.push( mesh ); } for ( let i = 0, il = meshes.length; i < il; i ++ ) { parser.associations.set( meshes[ i ], { meshes: meshIndex, primitives: i } ); } if ( meshes.length === 1 ) { return meshes[ 0 ]; } const group = new Group(); parser.associations.set( group, { meshes: meshIndex } ); for ( let i = 0, il = meshes.length; i < il; i ++ ) { group.add( meshes[ i ] ); } return group; } ); } /** * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#cameras * @param {number} cameraIndex * @return {Promise} */ loadCamera( cameraIndex ) { let camera; const cameraDef = this.json.cameras[ cameraIndex ]; const params = cameraDef[ cameraDef.type ]; if ( ! params ) { console.warn( 'THREE.GLTFLoader: Missing camera parameters.' ); return; } if ( cameraDef.type === 'perspective' ) { camera = new PerspectiveCamera( MathUtils.radToDeg( params.yfov ), params.aspectRatio || 1, params.znear || 1, params.zfar || 2e6 ); } else if ( cameraDef.type === 'orthographic' ) { camera = new OrthographicCamera( - params.xmag, params.xmag, params.ymag, - params.ymag, params.znear, params.zfar ); } if ( cameraDef.name ) camera.name = this.createUniqueName( cameraDef.name ); assignExtrasToUserData( camera, cameraDef ); return Promise.resolve( camera ); } /** * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#skins * @param {number} skinIndex * @return {Promise} */ loadSkin( skinIndex ) { const skinDef = this.json.skins[ skinIndex ]; const skinEntry = { joints: skinDef.joints }; if ( skinDef.inverseBindMatrices === undefined ) { return Promise.resolve( skinEntry ); } return this.getDependency( 'accessor', skinDef.inverseBindMatrices ).then( function ( accessor ) { skinEntry.inverseBindMatrices = accessor; return skinEntry; } ); } /** * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#animations * @param {number} animationIndex * @return {Promise} */ loadAnimation( animationIndex ) { const json = this.json; const animationDef = json.animations[ animationIndex ]; const pendingNodes = []; const pendingInputAccessors = []; const pendingOutputAccessors = []; const pendingSamplers = []; const pendingTargets = []; for ( let i = 0, il = animationDef.channels.length; i < il; i ++ ) { const channel = animationDef.channels[ i ]; const sampler = animationDef.samplers[ channel.sampler ]; const target = channel.target; const name = target.node; const input = animationDef.parameters !== undefined ? animationDef.parameters[ sampler.input ] : sampler.input; const output = animationDef.parameters !== undefined ? animationDef.parameters[ sampler.output ] : sampler.output; pendingNodes.push( this.getDependency( 'node', name ) ); pendingInputAccessors.push( this.getDependency( 'accessor', input ) ); pendingOutputAccessors.push( this.getDependency( 'accessor', output ) ); pendingSamplers.push( sampler ); pendingTargets.push( target ); } return Promise.all( [ Promise.all( pendingNodes ), Promise.all( pendingInputAccessors ), Promise.all( pendingOutputAccessors ), Promise.all( pendingSamplers ), Promise.all( pendingTargets ) ] ).then( function ( dependencies ) { const nodes = dependencies[ 0 ]; const inputAccessors = dependencies[ 1 ]; const outputAccessors = dependencies[ 2 ]; const samplers = dependencies[ 3 ]; const targets = dependencies[ 4 ]; const tracks = []; for ( let i = 0, il = nodes.length; i < il; i ++ ) { const node = nodes[ i ]; const inputAccessor = inputAccessors[ i ]; const outputAccessor = outputAccessors[ i ]; const sampler = samplers[ i ]; const target = targets[ i ]; if ( node === undefined ) continue; node.updateMatrix(); let TypedKeyframeTrack; switch ( PATH_PROPERTIES[ target.path ] ) { case PATH_PROPERTIES.weights: TypedKeyframeTrack = NumberKeyframeTrack; break; case PATH_PROPERTIES.rotation: TypedKeyframeTrack = QuaternionKeyframeTrack; break; case PATH_PROPERTIES.position: case PATH_PROPERTIES.scale: default: TypedKeyframeTrack = VectorKeyframeTrack; break; } const targetName = node.name ? node.name : node.uuid; const interpolation = sampler.interpolation !== undefined ? INTERPOLATION[ sampler.interpolation ] : InterpolateLinear; const targetNames = []; if ( PATH_PROPERTIES[ target.path ] === PATH_PROPERTIES.weights ) { node.traverse( function ( object ) { if ( object.morphTargetInfluences ) { targetNames.push( object.name ? object.name : object.uuid ); } } ); } else { targetNames.push( targetName ); } let outputArray = outputAccessor.array; if ( outputAccessor.normalized ) { const scale = getNormalizedComponentScale( outputArray.constructor ); const scaled = new Float32Array( outputArray.length ); for ( let j = 0, jl = outputArray.length; j < jl; j ++ ) { scaled[ j ] = outputArray[ j ] * scale; } outputArray = scaled; } for ( let j = 0, jl = targetNames.length; j < jl; j ++ ) { const track = new TypedKeyframeTrack( targetNames[ j ] + '.' + PATH_PROPERTIES[ target.path ], inputAccessor.array, outputArray, interpolation ); // Override interpolation with custom factory method. if ( sampler.interpolation === 'CUBICSPLINE' ) { track.createInterpolant = function InterpolantFactoryMethodGLTFCubicSpline( result ) { // A CUBICSPLINE keyframe in glTF has three output values for each input value, // representing inTangent, splineVertex, and outTangent. As a result, track.getValueSize() // must be divided by three to get the interpolant's sampleSize argument. const interpolantType = ( this instanceof QuaternionKeyframeTrack ) ? GLTFCubicSplineQuaternionInterpolant : GLTFCubicSplineInterpolant; return new interpolantType( this.times, this.values, this.getValueSize() / 3, result ); }; // Mark as CUBICSPLINE. `track.getInterpolation()` doesn't support custom interpolants. track.createInterpolant.isInterpolantFactoryMethodGLTFCubicSpline = true; } tracks.push( track ); } } const name = animationDef.name ? animationDef.name : 'animation_' + animationIndex; return new AnimationClip( name, undefined, tracks ); } ); } createNodeMesh( nodeIndex ) { const json = this.json; const parser = this; const nodeDef = json.nodes[ nodeIndex ]; if ( nodeDef.mesh === undefined ) return null; return parser.getDependency( 'mesh', nodeDef.mesh ).then( function ( mesh ) { const node = parser._getNodeRef( parser.meshCache, nodeDef.mesh, mesh ); // if weights are provided on the node, override weights on the mesh. if ( nodeDef.weights !== undefined ) { node.traverse( function ( o ) { if ( ! o.isMesh ) return; for ( let i = 0, il = nodeDef.weights.length; i < il; i ++ ) { o.morphTargetInfluences[ i ] = nodeDef.weights[ i ]; } } ); } return node; } ); } /** * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#nodes-and-hierarchy * @param {number} nodeIndex * @return {Promise} */ loadNode( nodeIndex ) { const json = this.json; const extensions = this.extensions; const parser = this; const nodeDef = json.nodes[ nodeIndex ]; // reserve node's name before its dependencies, so the root has the intended name. const nodeName = nodeDef.name ? parser.createUniqueName( nodeDef.name ) : ''; return ( function () { const pending = []; const meshPromise = parser._invokeOne( function ( ext ) { return ext.createNodeMesh && ext.createNodeMesh( nodeIndex ); } ); if ( meshPromise ) { pending.push( meshPromise ); } if ( nodeDef.camera !== undefined ) { pending.push( parser.getDependency( 'camera', nodeDef.camera ).then( function ( camera ) { return parser._getNodeRef( parser.cameraCache, nodeDef.camera, camera ); } ) ); } parser._invokeAll( function ( ext ) { return ext.createNodeAttachment && ext.createNodeAttachment( nodeIndex ); } ).forEach( function ( promise ) { pending.push( promise ); } ); return Promise.all( pending ); }() ).then( function ( objects ) { let node; // .isBone isn't in glTF spec. See ._markDefs if ( nodeDef.isBone === true ) { node = new Bone(); } else if ( objects.length > 1 ) { node = new Group(); } else if ( objects.length === 1 ) { node = objects[ 0 ]; } else { node = new Object3D(); } if ( node !== objects[ 0 ] ) { for ( let i = 0, il = objects.length; i < il; i ++ ) { node.add( objects[ i ] ); } } if ( nodeDef.name ) { node.userData.name = nodeDef.name; node.name = nodeName; } assignExtrasToUserData( node, nodeDef ); if ( nodeDef.extensions ) addUnknownExtensionsToUserData( extensions, node, nodeDef ); if ( nodeDef.matrix !== undefined ) { const matrix = new Matrix4(); matrix.fromArray( nodeDef.matrix ); node.applyMatrix4( matrix ); } else { if ( nodeDef.translation !== undefined ) { node.position.fromArray( nodeDef.translation ); } if ( nodeDef.rotation !== undefined ) { node.quaternion.fromArray( nodeDef.rotation ); } if ( nodeDef.scale !== undefined ) { node.scale.fromArray( nodeDef.scale ); } } if ( ! parser.associations.has( node ) ) { parser.associations.set( node, {} ); } parser.associations.get( node ).nodes = nodeIndex; return node; } ); } /** * Specification: https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#scenes * @param {number} sceneIndex * @return {Promise} */ loadScene( sceneIndex ) { const json = this.json; const extensions = this.extensions; const sceneDef = this.json.scenes[ sceneIndex ]; const parser = this; // Loader returns Group, not Scene. // See: https://github.com/mrdoob/three.js/issues/18342#issuecomment-578981172 const scene = new Group(); if ( sceneDef.name ) scene.name = parser.createUniqueName( sceneDef.name ); assignExtrasToUserData( scene, sceneDef ); if ( sceneDef.extensions ) addUnknownExtensionsToUserData( extensions, scene, sceneDef ); const nodeIds = sceneDef.nodes || []; const pending = []; for ( let i = 0, il = nodeIds.length; i < il; i ++ ) { pending.push( buildNodeHierarchy( nodeIds[ i ], scene, json, parser ) ); } return Promise.all( pending ).then( function () { // Removes dangling associations, associations that reference a node that // didn't make it into the scene. const reduceAssociations = ( node ) => { const reducedAssociations = new Map(); for ( const [ key, value ] of parser.associations ) { if ( key instanceof Material || key instanceof Texture ) { reducedAssociations.set( key, value ); } } node.traverse( ( node ) => { const mappings = parser.associations.get( node ); if ( mappings != null ) { reducedAssociations.set( node, mappings ); } } ); return reducedAssociations; }; parser.associations = reduceAssociations( scene ); return scene; } ); } } function buildNodeHierarchy( nodeId, parentObject, json, parser ) { const nodeDef = json.nodes[ nodeId ]; return parser.getDependency( 'node', nodeId ).then( function ( node ) { if ( nodeDef.skin === undefined ) return node; // build skeleton here as well let skinEntry; return parser.getDependency( 'skin', nodeDef.skin ).then( function ( skin ) { skinEntry = skin; const pendingJoints = []; for ( let i = 0, il = skinEntry.joints.length; i < il; i ++ ) { pendingJoints.push( parser.getDependency( 'node', skinEntry.joints[ i ] ) ); } return Promise.all( pendingJoints ); } ).then( function ( jointNodes ) { node.traverse( function ( mesh ) { if ( ! mesh.isMesh ) return; const bones = []; const boneInverses = []; for ( let j = 0, jl = jointNodes.length; j < jl; j ++ ) { const jointNode = jointNodes[ j ]; if ( jointNode ) { bones.push( jointNode ); const mat = new Matrix4(); if ( skinEntry.inverseBindMatrices !== undefined ) { mat.fromArray( skinEntry.inverseBindMatrices.array, j * 16 ); } boneInverses.push( mat ); } else { console.warn( 'THREE.GLTFLoader: Joint "%s" could not be found.', skinEntry.joints[ j ] ); } } mesh.bind( new Skeleton( bones, boneInverses ), mesh.matrixWorld ); } ); return node; } ); } ).then( function ( node ) { // build node hierachy parentObject.add( node ); const pending = []; if ( nodeDef.children ) { const children = nodeDef.children; for ( let i = 0, il = children.length; i < il; i ++ ) { const child = children[ i ]; pending.push( buildNodeHierarchy( child, node, json, parser ) ); } } return Promise.all( pending ); } ); } /** * @param {BufferGeometry} geometry * @param {GLTF.Primitive} primitiveDef * @param {GLTFParser} parser */ function computeBounds( geometry, primitiveDef, parser ) { const attributes = primitiveDef.attributes; const box = new Box3(); if ( attributes.POSITION !== undefined ) { const accessor = parser.json.accessors[ attributes.POSITION ]; const min = accessor.min; const max = accessor.max; // glTF requires 'min' and 'max', but VRM (which extends glTF) currently ignores that requirement. if ( min !== undefined && max !== undefined ) { box.set( new Vector3( min[ 0 ], min[ 1 ], min[ 2 ] ), new Vector3( max[ 0 ], max[ 1 ], max[ 2 ] ) ); if ( accessor.normalized ) { const boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] ); box.min.multiplyScalar( boxScale ); box.max.multiplyScalar( boxScale ); } } else { console.warn( 'THREE.GLTFLoader: Missing min/max properties for accessor POSITION.' ); return; } } else { return; } const targets = primitiveDef.targets; if ( targets !== undefined ) { const maxDisplacement = new Vector3(); const vector = new Vector3(); for ( let i = 0, il = targets.length; i < il; i ++ ) { const target = targets[ i ]; if ( target.POSITION !== undefined ) { const accessor = parser.json.accessors[ target.POSITION ]; const min = accessor.min; const max = accessor.max; // glTF requires 'min' and 'max', but VRM (which extends glTF) currently ignores that requirement. if ( min !== undefined && max !== undefined ) { // we need to get max of absolute components because target weight is [-1,1] vector.setX( Math.max( Math.abs( min[ 0 ] ), Math.abs( max[ 0 ] ) ) ); vector.setY( Math.max( Math.abs( min[ 1 ] ), Math.abs( max[ 1 ] ) ) ); vector.setZ( Math.max( Math.abs( min[ 2 ] ), Math.abs( max[ 2 ] ) ) ); if ( accessor.normalized ) { const boxScale = getNormalizedComponentScale( WEBGL_COMPONENT_TYPES[ accessor.componentType ] ); vector.multiplyScalar( boxScale ); } // Note: this assumes that the sum of all weights is at most 1. This isn't quite correct - it's more conservative // to assume that each target can have a max weight of 1. However, for some use cases - notably, when morph targets // are used to implement key-frame animations and as such only two are active at a time - this results in very large // boxes. So for now we make a box that's sometimes a touch too small but is hopefully mostly of reasonable size. maxDisplacement.max( vector ); } else { console.warn( 'THREE.GLTFLoader: Missing min/max properties for accessor POSITION.' ); } } } // As per comment above this box isn't conservative, but has a reasonable size for a very large number of morph targets. box.expandByVector( maxDisplacement ); } geometry.boundingBox = box; const sphere = new Sphere(); box.getCenter( sphere.center ); sphere.radius = box.min.distanceTo( box.max ) / 2; geometry.boundingSphere = sphere; } /** * @param {BufferGeometry} geometry * @param {GLTF.Primitive} primitiveDef * @param {GLTFParser} parser * @return {Promise} */ function addPrimitiveAttributes( geometry, primitiveDef, parser ) { const attributes = primitiveDef.attributes; const pending = []; function assignAttributeAccessor( accessorIndex, attributeName ) { return parser.getDependency( 'accessor', accessorIndex ) .then( function ( accessor ) { geometry.setAttribute( attributeName, accessor ); } ); } for ( const gltfAttributeName in attributes ) { const threeAttributeName = ATTRIBUTES[ gltfAttributeName ] || gltfAttributeName.toLowerCase(); // Skip attributes already provided by e.g. Draco extension. if ( threeAttributeName in geometry.attributes ) continue; pending.push( assignAttributeAccessor( attributes[ gltfAttributeName ], threeAttributeName ) ); } if ( primitiveDef.indices !== undefined && ! geometry.index ) { const accessor = parser.getDependency( 'accessor', primitiveDef.indices ).then( function ( accessor ) { geometry.setIndex( accessor ); } ); pending.push( accessor ); } assignExtrasToUserData( geometry, primitiveDef ); computeBounds( geometry, primitiveDef, parser ); return Promise.all( pending ).then( function () { return primitiveDef.targets !== undefined ? addMorphTargets( geometry, primitiveDef.targets, parser ) : geometry; } ); } /** * @param {BufferGeometry} geometry * @param {Number} drawMode * @return {BufferGeometry} */ function toTrianglesDrawMode( geometry, drawMode ) { let index = geometry.getIndex(); // generate index if not present if ( index === null ) { const indices = []; const position = geometry.getAttribute( 'position' ); if ( position !== undefined ) { for ( let i = 0; i < position.count; i ++ ) { indices.push( i ); } geometry.setIndex( indices ); index = geometry.getIndex(); } else { console.error( 'THREE.GLTFLoader.toTrianglesDrawMode(): Undefined position attribute. Processing not possible.' ); return geometry; } } // const numberOfTriangles = index.count - 2; const newIndices = []; if ( drawMode === TriangleFanDrawMode ) { // gl.TRIANGLE_FAN for ( let i = 1; i <= numberOfTriangles; i ++ ) { newIndices.push( index.getX( 0 ) ); newIndices.push( index.getX( i ) ); newIndices.push( index.getX( i + 1 ) ); } } else { // gl.TRIANGLE_STRIP for ( let i = 0; i < numberOfTriangles; i ++ ) { if ( i % 2 === 0 ) { newIndices.push( index.getX( i ) ); newIndices.push( index.getX( i + 1 ) ); newIndices.push( index.getX( i + 2 ) ); } else { newIndices.push( index.getX( i + 2 ) ); newIndices.push( index.getX( i + 1 ) ); newIndices.push( index.getX( i ) ); } } } if ( ( newIndices.length / 3 ) !== numberOfTriangles ) { console.error( 'THREE.GLTFLoader.toTrianglesDrawMode(): Unable to generate correct amount of triangles.' ); } // build final geometry const newGeometry = geometry.clone(); newGeometry.setIndex( newIndices ); return newGeometry; } let globalThis = window; //add 有的app没有globalThis. 2023.1 let rtcCenterGlobal = false;//默认false,也就是scale.z要乘以-1 /*! ***************************************************************************** github: https://github.com/nytimes/three-loader-3dtiles Copyright (c) Microsoft Corporation. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ***************************************************************************** */ let visiGeoCount = 0; const maxVertexVisi = 5e6; const maxTexVisi = 500; let maxDepth = 100; function getGpuMemoryUsage(win = window){//总的 let c = 0; viewer.objs.children.filter(e=>{ if(e.fileType == '3dTiles'){ e.traverse(child=>{ if(child.runtime){ let tileset3D = child.runtime.getTileset(); c += tileset3D.gpuMemoryUsageInBytes; return {stopContinue:true} } }); } }); if(win.parent != win){//还有父级页面。 暂时只有子级需要考虑父级,假设父级在前台时子级已经销毁 c += getGpuMemoryUsage(win.parent); } viewer.tiles3dMemoryUsage = c; return c } function __awaiter(thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); } function assert$7(condition, message) { if (!condition) { throw new Error(message || 'loader assertion failed.'); } } const isBrowser$2 = Boolean(typeof process !== 'object' || String(process) !== '[object process]' || process.browser); const matches$1 = typeof process !== 'undefined' && process.version && /v([0-9]*)/.exec(process.version); matches$1 && parseFloat(matches$1[1]) || 0; const VERSION$8 = "3.1.4" ; function assert$6(condition, message) { if (!condition) { throw new Error(message || 'loaders.gl assertion failed.'); } } const globals$1 = { self: typeof self !== 'undefined' && self, window: typeof window !== 'undefined' && window, global: typeof global !== 'undefined' && global, document: typeof document !== 'undefined' && document }; const global_ = globals$1.global || globals$1.self || globals$1.window || {}; const isBrowser$1 = typeof process !== 'object' || String(process) !== '[object process]' || process.browser; const isWorker = typeof importScripts === 'function'; const isMobile = typeof window !== 'undefined' && typeof window.orientation !== 'undefined'; const matches = typeof process !== 'undefined' && process.version && /v([0-9]*)/.exec(process.version); matches && parseFloat(matches[1]) || 0; function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } class WorkerJob { constructor(jobName, workerThread) { _defineProperty(this, "name", void 0); _defineProperty(this, "workerThread", void 0); _defineProperty(this, "isRunning", void 0); _defineProperty(this, "result", void 0); _defineProperty(this, "_resolve", void 0); _defineProperty(this, "_reject", void 0); this.name = jobName; this.workerThread = workerThread; this.isRunning = true; this._resolve = () => {}; this._reject = () => {}; this.result = new Promise((resolve, reject) => { this._resolve = resolve; this._reject = reject; }); } postMessage(type, payload) { this.workerThread.postMessage({ source: 'loaders.gl', type, payload }); } done(value) { assert$6(this.isRunning); this.isRunning = false; this._resolve(value); } error(error) { assert$6(this.isRunning); this.isRunning = false; this._reject(error); } } const workerURLCache = new Map(); function getLoadableWorkerURL(props) { assert$6(props.source && !props.url || !props.source && props.url); let workerURL = workerURLCache.get(props.source || props.url); if (!workerURL) { if (props.url) { workerURL = getLoadableWorkerURLFromURL(props.url); workerURLCache.set(props.url, workerURL); } if (props.source) { workerURL = getLoadableWorkerURLFromSource(props.source); workerURLCache.set(props.source, workerURL); } } assert$6(workerURL); return workerURL; } function getLoadableWorkerURLFromURL(url) { if (!url.startsWith('http')) { return url; } const workerSource = buildScriptSource(url); return getLoadableWorkerURLFromSource(workerSource); } function getLoadableWorkerURLFromSource(workerSource) { const blob = new Blob([workerSource], { type: 'application/javascript' }); return URL.createObjectURL(blob); } function buildScriptSource(workerUrl) { return "try {\n importScripts('".concat(workerUrl, "');\n} catch (error) {\n console.error(error);\n throw error;\n}"); } function getTransferList(object, recursive = true, transfers) { const transfersSet = transfers || new Set(); if (!object) ; else if (isTransferable(object)) { transfersSet.add(object); } else if (isTransferable(object.buffer)) { transfersSet.add(object.buffer); } else if (ArrayBuffer.isView(object)) ; else if (recursive && typeof object === 'object') { for (const key in object) { getTransferList(object[key], recursive, transfersSet); } } return transfers === undefined ? Array.from(transfersSet) : []; } function isTransferable(object) { if (!object) { return false; } if (object instanceof ArrayBuffer) { return true; } if (typeof MessagePort !== 'undefined' && object instanceof MessagePort) { return true; } if (typeof ImageBitmap !== 'undefined' && object instanceof ImageBitmap) { return true; } if (typeof OffscreenCanvas !== 'undefined' && object instanceof OffscreenCanvas) { return true; } return false; } const NOOP = () => {}; class WorkerThread { static isSupported() { return typeof Worker !== 'undefined'; } constructor(props) { _defineProperty(this, "name", void 0); _defineProperty(this, "source", void 0); _defineProperty(this, "url", void 0); _defineProperty(this, "terminated", false); _defineProperty(this, "worker", void 0); _defineProperty(this, "onMessage", void 0); _defineProperty(this, "onError", void 0); _defineProperty(this, "_loadableURL", ''); const { name, source, url } = props; assert$6(source || url); this.name = name; this.source = source; this.url = url; this.onMessage = NOOP; this.onError = error => console.log(error); this.worker = this._createBrowserWorker(); } destroy() { this.onMessage = NOOP; this.onError = NOOP; this.worker.terminate(); this.terminated = true; } get isRunning() { return Boolean(this.onMessage); } postMessage(data, transferList) { transferList = transferList || getTransferList(data); this.worker.postMessage(data, transferList); } _getErrorFromErrorEvent(event) { let message = 'Failed to load '; message += "worker ".concat(this.name, " from ").concat(this.url, ". "); if (event.message) { message += "".concat(event.message, " in "); } if (event.lineno) { message += ":".concat(event.lineno, ":").concat(event.colno); } return new Error(message); } _createBrowserWorker() { this._loadableURL = getLoadableWorkerURL({ source: this.source, url: this.url }); const worker = new Worker(this._loadableURL, { name: this.name }); worker.onmessage = event => { if (!event.data) { this.onError(new Error('No data received')); } else { this.onMessage(event.data); } }; worker.onerror = error => { this.onError(this._getErrorFromErrorEvent(error)); this.terminated = true; }; worker.onmessageerror = event => console.error(event); return worker; } } class WorkerPool$2 { constructor(props) { _defineProperty(this, "name", 'unnamed'); _defineProperty(this, "source", void 0); _defineProperty(this, "url", void 0); _defineProperty(this, "maxConcurrency", 1); _defineProperty(this, "maxMobileConcurrency", 1); _defineProperty(this, "onDebug", () => {}); _defineProperty(this, "reuseWorkers", true); _defineProperty(this, "props", {}); _defineProperty(this, "jobQueue", []); _defineProperty(this, "idleQueue", []); _defineProperty(this, "count", 0); _defineProperty(this, "isDestroyed", false); this.source = props.source; this.url = props.url; this.setProps(props); } destroy() { this.idleQueue.forEach(worker => worker.destroy()); this.isDestroyed = true; } setProps(props) { this.props = { ...this.props, ...props }; if (props.name !== undefined) { this.name = props.name; } if (props.maxConcurrency !== undefined) { this.maxConcurrency = props.maxConcurrency; } if (props.maxMobileConcurrency !== undefined) { this.maxMobileConcurrency = props.maxMobileConcurrency; } if (props.reuseWorkers !== undefined) { this.reuseWorkers = props.reuseWorkers; } if (props.onDebug !== undefined) { this.onDebug = props.onDebug; } } async startJob(name, onMessage = (job, type, data) => job.done(data), onError = (job, error) => job.error(error)) { const startPromise = new Promise(onStart => { this.jobQueue.push({ name, onMessage, onError, onStart }); return this; }); this._startQueuedJob(); return await startPromise; } async _startQueuedJob() { if (!this.jobQueue.length) { return; } const workerThread = this._getAvailableWorker(); if (!workerThread) { return; } const queuedJob = this.jobQueue.shift(); if (queuedJob) { this.onDebug({ message: 'Starting job', name: queuedJob.name, workerThread, backlog: this.jobQueue.length }); const job = new WorkerJob(queuedJob.name, workerThread); workerThread.onMessage = data => queuedJob.onMessage(job, data.type, data.payload); workerThread.onError = error => queuedJob.onError(job, error); queuedJob.onStart(job); try { await job.result; } finally { this.returnWorkerToQueue(workerThread); } } } returnWorkerToQueue(worker) { const shouldDestroyWorker = this.isDestroyed || !this.reuseWorkers || this.count > this._getMaxConcurrency(); if (shouldDestroyWorker) { worker.destroy(); this.count--; } else { this.idleQueue.push(worker); } if (!this.isDestroyed) { this._startQueuedJob(); } } _getAvailableWorker() { if (this.idleQueue.length > 0) { return this.idleQueue.shift() || null; } if (this.count < this._getMaxConcurrency()) { this.count++; const name = "".concat(this.name.toLowerCase(), " (#").concat(this.count, " of ").concat(this.maxConcurrency, ")"); return new WorkerThread({ name, source: this.source, url: this.url }); } return null; } _getMaxConcurrency() { return isMobile ? this.maxMobileConcurrency : this.maxConcurrency; } } const DEFAULT_PROPS$3 = { maxConcurrency: 3, maxMobileConcurrency: 1, onDebug: () => {}, reuseWorkers: true }; class WorkerFarm { static isSupported() { return WorkerThread.isSupported(); } static getWorkerFarm(props = {}) { WorkerFarm._workerFarm = WorkerFarm._workerFarm || new WorkerFarm({}); WorkerFarm._workerFarm.setProps(props); return WorkerFarm._workerFarm; } constructor(props) { _defineProperty(this, "props", void 0); _defineProperty(this, "workerPools", new Map()); this.props = { ...DEFAULT_PROPS$3 }; this.setProps(props); this.workerPools = new Map(); } destroy() { for (const workerPool of this.workerPools.values()) { workerPool.destroy(); } } setProps(props) { this.props = { ...this.props, ...props }; for (const workerPool of this.workerPools.values()) { workerPool.setProps(this._getWorkerPoolProps()); } } getWorkerPool(options) { const { name, source, url } = options; let workerPool = this.workerPools.get(name); if (!workerPool) { workerPool = new WorkerPool$2({ name, source, url }); workerPool.setProps(this._getWorkerPoolProps()); this.workerPools.set(name, workerPool); } return workerPool; } _getWorkerPoolProps() { return { maxConcurrency: this.props.maxConcurrency, maxMobileConcurrency: this.props.maxMobileConcurrency, reuseWorkers: this.props.reuseWorkers, onDebug: this.props.onDebug }; } } _defineProperty(WorkerFarm, "_workerFarm", void 0); const NPM_TAG = 'latest'; function getWorkerURL(worker, options = {}) { const workerOptions = options[worker.id] || {}; const workerFile = "".concat(worker.id, "-worker.js"); let url = workerOptions.workerUrl; if (!url && worker.id === 'compression') { url = options.workerUrl; } if (options._workerType === 'test') { url = "modules/".concat(worker.module, "/dist/").concat(workerFile); } if (!url) { let version = worker.version; if (version === 'latest') { version = NPM_TAG; } const versionTag = version ? "@".concat(version) : ''; url = "https://unpkg.com/@loaders.gl/".concat(worker.module).concat(versionTag, "/dist/").concat(workerFile); } assert$6(url); return url; } function validateWorkerVersion(worker, coreVersion = VERSION$8) { assert$6(worker, 'no worker provided'); const workerVersion = worker.version; if (!coreVersion || !workerVersion) { return false; } return true; } var ChildProcessProxy = {}; var node = /*#__PURE__*/Object.freeze(/*#__PURE__*/Object.assign(/*#__PURE__*/Object.create(null), ChildProcessProxy, { 'default': ChildProcessProxy })); const VERSION$7 = "3.1.4" ; const loadLibraryPromises = {}; async function loadLibrary(libraryUrl, moduleName = null, options = {}) { if (moduleName) { libraryUrl = getLibraryUrl(libraryUrl, moduleName, options); } loadLibraryPromises[libraryUrl] = loadLibraryPromises[libraryUrl] || loadLibraryFromFile(libraryUrl); return await loadLibraryPromises[libraryUrl]; } function getLibraryUrl(library, moduleName, options) { if (library.startsWith('http')) { return library; } const modules = options.modules || {}; if (modules[library]) { return modules[library]; } if (!isBrowser$1) { return "modules/".concat(moduleName, "/dist/libs/").concat(library); } if (options.CDN) { assert$6(options.CDN.startsWith('http')); return "".concat(options.CDN, "/").concat(moduleName, "@").concat(VERSION$7, "/dist/libs/").concat(library); } if (isWorker) { return "../src/libs/".concat(library); } return "modules/".concat(moduleName, "/src/libs/").concat(library); } async function loadLibraryFromFile(libraryUrl) { if (libraryUrl.endsWith('wasm')) { const response = await fetch(libraryUrl); return await response.arrayBuffer(); } if (!isBrowser$1) { try { return node && ChildProcessProxy.requireFromFile && (await ChildProcessProxy.requireFromFile(libraryUrl)); } catch { return null; } } if (isWorker) { return importScripts(libraryUrl); } const response = await fetch(libraryUrl); const scriptSource = await response.text(); return loadLibraryFromString(scriptSource, libraryUrl); } function loadLibraryFromString(scriptSource, id) { if (!isBrowser$1) { return ChildProcessProxy.requireFromString && ChildProcessProxy.requireFromString(scriptSource, id); } if (isWorker) { eval.call(global_, scriptSource); return null; } const script = document.createElement('script'); script.id = id; try { script.appendChild(document.createTextNode(scriptSource)); } catch (e) { script.text = scriptSource; } document.body.appendChild(script); return null; } function canParseWithWorker(loader, options) { if (!WorkerFarm.isSupported()) { return false; } return loader.worker && (options === null || options === void 0 ? void 0 : options.worker); } async function parseWithWorker(loader, data, options, context, parseOnMainThread) { const name = loader.id; const url = getWorkerURL(loader, options); const workerFarm = WorkerFarm.getWorkerFarm(options); const workerPool = workerFarm.getWorkerPool({ name, url }); options = JSON.parse(JSON.stringify(options)); const job = await workerPool.startJob('process-on-worker', onMessage.bind(null, parseOnMainThread)); job.postMessage('process', { input: data, options }); const result = await job.result; return await result.result; } async function onMessage(parseOnMainThread, job, type, payload) { switch (type) { case 'done': job.done(payload); break; case 'error': job.error(new Error(payload.error)); break; case 'process': const { id, input, options } = payload; try { const result = await parseOnMainThread(input, options); job.postMessage('done', { id, result }); } catch (error) { const message = error instanceof Error ? error.message : 'unknown error'; job.postMessage('error', { id, error: message }); } break; default: console.warn("parse-with-worker unknown message ".concat(type)); } } function getFirstCharacters$1(data, length = 5) { if (typeof data === 'string') { return data.slice(0, length); } else if (ArrayBuffer.isView(data)) { return getMagicString$3(data.buffer, data.byteOffset, length); } else if (data instanceof ArrayBuffer) { const byteOffset = 0; return getMagicString$3(data, byteOffset, length); } return ''; } function getMagicString$3(arrayBuffer, byteOffset, length) { if (arrayBuffer.byteLength <= byteOffset + length) { return ''; } const dataView = new DataView(arrayBuffer); let magic = ''; for (let i = 0; i < length; i++) { magic += String.fromCharCode(dataView.getUint8(byteOffset + i)); } return magic; } function parseJSON(string) { try { return JSON.parse(string); } catch (_) { throw new Error("Failed to parse JSON from data starting with \"".concat(getFirstCharacters$1(string), "\"")); } } function isBuffer$1(value) { return value && typeof value === 'object' && value.isBuffer; } function bufferToArrayBuffer(buffer) { if (isBuffer$1(buffer)) { const typedArray = new Uint8Array(buffer.buffer, buffer.byteOffset, buffer.length); return typedArray.slice().buffer; } return buffer; } function toArrayBuffer(data) { if (isBuffer$1(data)) { return bufferToArrayBuffer(data); } if (data instanceof ArrayBuffer) { return data; } if (ArrayBuffer.isView(data)) { if (data.byteOffset === 0 && data.byteLength === data.buffer.byteLength) { return data.buffer; } return data.buffer.slice(data.byteOffset, data.byteOffset + data.byteLength); } if (typeof data === 'string') { const text = data; const uint8Array = new TextEncoder().encode(text); return uint8Array.buffer; } if (data && typeof data === 'object' && data._toArrayBuffer) { return data._toArrayBuffer(); } throw new Error('toArrayBuffer'); } function compareArrayBuffers(arrayBuffer1, arrayBuffer2, byteLength) { byteLength = byteLength || arrayBuffer1.byteLength; if (arrayBuffer1.byteLength < byteLength || arrayBuffer2.byteLength < byteLength) { return false; } const array1 = new Uint8Array(arrayBuffer1); const array2 = new Uint8Array(arrayBuffer2); for (let i = 0; i < array1.length; ++i) { if (array1[i] !== array2[i]) { return false; } } return true; } function concatenateArrayBuffers(...sources) { const sourceArrays = sources.map(source2 => source2 instanceof ArrayBuffer ? new Uint8Array(source2) : source2); const byteLength = sourceArrays.reduce((length, typedArray) => length + typedArray.byteLength, 0); const result = new Uint8Array(byteLength); let offset = 0; for (const sourceArray of sourceArrays) { result.set(sourceArray, offset); offset += sourceArray.byteLength; } return result.buffer; } function sliceArrayBuffer(arrayBuffer, byteOffset, byteLength) { const subArray = byteLength !== undefined ? new Uint8Array(arrayBuffer).subarray(byteOffset, byteOffset + byteLength) : new Uint8Array(arrayBuffer).subarray(byteOffset); const arrayCopy = new Uint8Array(subArray); return arrayCopy.buffer; } function padToNBytes(byteLength, padding) { assert$7(byteLength >= 0); assert$7(padding > 0); return byteLength + (padding - 1) & ~(padding - 1); } function copyToArray(source, target, targetOffset) { let sourceArray; if (source instanceof ArrayBuffer) { sourceArray = new Uint8Array(source); } else { const srcByteOffset = source.byteOffset; const srcByteLength = source.byteLength; sourceArray = new Uint8Array(source.buffer || source.arrayBuffer, srcByteOffset, srcByteLength); } target.set(sourceArray, targetOffset); return targetOffset + padToNBytes(sourceArray.byteLength, 4); } async function concatenateArrayBuffersAsync(asyncIterator) { const arrayBuffers = []; for await (const chunk of asyncIterator) { arrayBuffers.push(chunk); } return concatenateArrayBuffers(...arrayBuffers); } function getHiResTimestamp$1() { let timestamp; if (typeof window !== 'undefined' && window.performance) { timestamp = window.performance.now(); } else if (typeof process !== 'undefined' && process.hrtime) { const timeParts = process.hrtime(); timestamp = timeParts[0] * 1000 + timeParts[1] / 1e6; } else { timestamp = Date.now(); } return timestamp; } class Stat { constructor(name, type) { _defineProperty(this, "name", void 0); _defineProperty(this, "type", void 0); _defineProperty(this, "sampleSize", 1); _defineProperty(this, "time", void 0); _defineProperty(this, "count", void 0); _defineProperty(this, "samples", void 0); _defineProperty(this, "lastTiming", void 0); _defineProperty(this, "lastSampleTime", void 0); _defineProperty(this, "lastSampleCount", void 0); _defineProperty(this, "_count", 0); _defineProperty(this, "_time", 0); _defineProperty(this, "_samples", 0); _defineProperty(this, "_startTime", 0); _defineProperty(this, "_timerPending", false); this.name = name; this.type = type; this.reset(); } setSampleSize(samples) { this.sampleSize = samples; return this; } incrementCount() { this.addCount(1); return this; } decrementCount() { this.subtractCount(1); return this; } addCount(value) { this._count += value; this._samples++; this._checkSampling(); return this; } subtractCount(value) { this._count -= value; this._samples++; this._checkSampling(); return this; } addTime(time) { this._time += time; this.lastTiming = time; this._samples++; this._checkSampling(); return this; } timeStart() { this._startTime = getHiResTimestamp$1(); this._timerPending = true; return this; } timeEnd() { if (!this._timerPending) { return this; } this.addTime(getHiResTimestamp$1() - this._startTime); this._timerPending = false; this._checkSampling(); return this; } getSampleAverageCount() { return this.sampleSize > 0 ? this.lastSampleCount / this.sampleSize : 0; } getSampleAverageTime() { return this.sampleSize > 0 ? this.lastSampleTime / this.sampleSize : 0; } getSampleHz() { return this.lastSampleTime > 0 ? this.sampleSize / (this.lastSampleTime / 1000) : 0; } getAverageCount() { return this.samples > 0 ? this.count / this.samples : 0; } getAverageTime() { return this.samples > 0 ? this.time / this.samples : 0; } getHz() { return this.time > 0 ? this.samples / (this.time / 1000) : 0; } reset() { this.time = 0; this.count = 0; this.samples = 0; this.lastTiming = 0; this.lastSampleTime = 0; this.lastSampleCount = 0; this._count = 0; this._time = 0; this._samples = 0; this._startTime = 0; this._timerPending = false; return this; } _checkSampling() { if (this._samples === this.sampleSize) { this.lastSampleTime = this._time; this.lastSampleCount = this._count; this.count += this._count; this.time += this._time; this.samples += this._samples; this._time = 0; this._count = 0; this._samples = 0; } } } class Stats$1 { constructor(options) { _defineProperty(this, "id", void 0); _defineProperty(this, "stats", {}); this.id = options.id; this.stats = {}; this._initializeStats(options.stats); Object.seal(this); } get(name) { let type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'count'; return this._getOrCreate({ name, type }); } get size() { return Object.keys(this.stats).length; } reset() { for (const key in this.stats) { this.stats[key].reset(); } return this; } forEach(fn) { for (const key in this.stats) { fn(this.stats[key]); } } getTable() { const table = {}; this.forEach(stat => { table[stat.name] = { time: stat.time || 0, count: stat.count || 0, average: stat.getAverageTime() || 0, hz: stat.getHz() || 0 }; }); return table; } _initializeStats() { let stats = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : []; stats.forEach(stat => this._getOrCreate(stat)); } _getOrCreate(stat) { if (!stat || !stat.name) { return null; } const { name, type } = stat; if (!this.stats[name]) { if (stat instanceof Stat) { this.stats[name] = stat; } else { this.stats[name] = new Stat(name, type); } } return this.stats[name]; } } const STAT_QUEUED_REQUESTS = 'Queued Requests'; const STAT_ACTIVE_REQUESTS = 'Active Requests'; const STAT_CANCELLED_REQUESTS = 'Cancelled Requests'; const STAT_QUEUED_REQUESTS_EVER = 'Queued Requests Ever'; const STAT_ACTIVE_REQUESTS_EVER = 'Active Requests Ever'; const DEFAULT_PROPS$2 = { id: 'request-scheduler', throttleRequests: true, maxRequests: 6 }; class RequestScheduler { constructor(props = {}) { _defineProperty(this, "props", void 0); _defineProperty(this, "stats", void 0); _defineProperty(this, "activeRequestCount", 0); _defineProperty(this, "requestQueue", []); _defineProperty(this, "requestMap", new Map()); _defineProperty(this, "deferredUpdate", null); this.props = { ...DEFAULT_PROPS$2, ...props }; this.stats = new Stats$1({ id: this.props.id }); this.stats.get(STAT_QUEUED_REQUESTS); this.stats.get(STAT_ACTIVE_REQUESTS); this.stats.get(STAT_CANCELLED_REQUESTS); this.stats.get(STAT_QUEUED_REQUESTS_EVER); this.stats.get(STAT_ACTIVE_REQUESTS_EVER); } scheduleRequest(handle, getPriority = () => 0) { if (!this.props.throttleRequests) { return Promise.resolve({ done: () => {} }); } if (this.requestMap.has(handle)) { return this.requestMap.get(handle); } const request = { handle, priority: 0, getPriority }; const promise = new Promise(resolve => { request.resolve = resolve; return request; }); this.requestQueue.push(request); this.requestMap.set(handle, promise); this._issueNewRequests(); return promise; } _issueRequest(request) { const { handle, resolve } = request; let isDone = false; const done = () => { if (!isDone) { isDone = true; this.requestMap.delete(handle); this.activeRequestCount--; this._issueNewRequests(); } }; this.activeRequestCount++; return resolve ? resolve({ done }) : Promise.resolve({ done }); } _issueNewRequests() { if (!this.deferredUpdate) { this.deferredUpdate = setTimeout(() => this._issueNewRequestsAsync(), 0); } } _issueNewRequestsAsync() { this.deferredUpdate = null; const freeSlots = Math.max(this.props.maxRequests - this.activeRequestCount, 0); if (freeSlots === 0) { return; } this._updateAllRequests(); for (let i = 0; i < freeSlots; ++i) { const request = this.requestQueue.shift(); if (request) { this._issueRequest(request); } } } _updateAllRequests() { const requestQueue = this.requestQueue; for (let i = 0; i < requestQueue.length; ++i) { const request = requestQueue[i]; if (!this._updateRequest(request)) { requestQueue.splice(i, 1); this.requestMap.delete(request.handle); i--; } } requestQueue.sort((a, b) => a.priority - b.priority); } _updateRequest(request) { request.priority = request.getPriority(request.handle); if (request.priority < 0) { request.resolve(null); return false; } return true; } } let pathPrefix = ''; const fileAliases = {}; function resolvePath(filename) { for (const alias in fileAliases) { if (filename.startsWith(alias)) { const replacement = fileAliases[alias]; filename = filename.replace(alias, replacement); } } if (!filename.startsWith('http://') && !filename.startsWith('https://')) { filename = "".concat(pathPrefix).concat(filename); } return filename; } function filename(url) { const slashIndex = url && url.lastIndexOf('/'); return slashIndex >= 0 ? url.substr(slashIndex + 1) : ''; } function dirname(url) { const slashIndex = url && url.lastIndexOf('/'); return slashIndex >= 0 ? url.substr(0, slashIndex) : ''; } const isBoolean = x => typeof x === 'boolean'; const isFunction = x => typeof x === 'function'; const isObject = x => x !== null && typeof x === 'object'; const isPureObject = x => isObject(x) && x.constructor === {}.constructor; const isIterable = x => x && typeof x[Symbol.iterator] === 'function'; const isAsyncIterable = x => x && typeof x[Symbol.asyncIterator] === 'function'; const isResponse = x => typeof Response !== 'undefined' && x instanceof Response || x && x.arrayBuffer && x.text && x.json; const isBlob = x => typeof Blob !== 'undefined' && x instanceof Blob; const isBuffer = x => x && typeof x === 'object' && x.isBuffer; const isReadableDOMStream = x => typeof ReadableStream !== 'undefined' && x instanceof ReadableStream || isObject(x) && isFunction(x.tee) && isFunction(x.cancel) && isFunction(x.getReader); const isReadableNodeStream = x => isObject(x) && isFunction(x.read) && isFunction(x.pipe) && isBoolean(x.readable); const isReadableStream = x => isReadableDOMStream(x) || isReadableNodeStream(x); const DATA_URL_PATTERN = /^data:([-\w.]+\/[-\w.+]+)(;|,)/; const MIME_TYPE_PATTERN = /^([-\w.]+\/[-\w.+]+)/; function parseMIMEType(mimeString) { const matches = MIME_TYPE_PATTERN.exec(mimeString); if (matches) { return matches[1]; } return mimeString; } function parseMIMETypeFromURL(url) { const matches = DATA_URL_PATTERN.exec(url); if (matches) { return matches[1]; } return ''; } const QUERY_STRING_PATTERN = /\?.*/; function getResourceUrlAndType(resource) { if (isResponse(resource)) { const url = stripQueryString(resource.url || ''); const contentTypeHeader = resource.headers.get('content-type') || ''; return { url, type: parseMIMEType(contentTypeHeader) || parseMIMETypeFromURL(url) }; } if (isBlob(resource)) { return { url: stripQueryString(resource.name || ''), type: resource.type || '' }; } if (typeof resource === 'string') { return { url: stripQueryString(resource), type: parseMIMETypeFromURL(resource) }; } return { url: '', type: '' }; } function getResourceContentLength(resource) { if (isResponse(resource)) { return resource.headers['content-length'] || -1; } if (isBlob(resource)) { return resource.size; } if (typeof resource === 'string') { return resource.length; } if (resource instanceof ArrayBuffer) { return resource.byteLength; } if (ArrayBuffer.isView(resource)) { return resource.byteLength; } return -1; } function stripQueryString(url) { return url.replace(QUERY_STRING_PATTERN, ''); } async function makeResponse(resource) { if (isResponse(resource)) { return resource; } const headers = {}; const contentLength = getResourceContentLength(resource); if (contentLength >= 0) { headers['content-length'] = String(contentLength); } const { url, type } = getResourceUrlAndType(resource); if (type) { headers['content-type'] = type; } const initialDataUrl = await getInitialDataUrl(resource); if (initialDataUrl) { headers['x-first-bytes'] = initialDataUrl; } if (typeof resource === 'string') { resource = new TextEncoder().encode(resource); } const response = new Response(resource, { headers }); Object.defineProperty(response, 'url', { value: url }); return response; } async function checkResponse(response) { if (!response.ok) { const message = await getResponseError(response); throw new Error(message); } } async function getResponseError(response) { let message = "Failed to fetch resource ".concat(response.url, " (").concat(response.status, "): "); try { const contentType = response.headers.get('Content-Type'); let text = response.statusText; if (contentType.includes('application/json')) { text += " ".concat(await response.text()); } message += text; message = message.length > 60 ? "".concat(message.slice(60), "...") : message; } catch (error) {} return message; } async function getInitialDataUrl(resource) { const INITIAL_DATA_LENGTH = 5; if (typeof resource === 'string') { return "data:,".concat(resource.slice(0, INITIAL_DATA_LENGTH)); } if (resource instanceof Blob) { const blobSlice = resource.slice(0, 5); return await new Promise(resolve => { const reader = new FileReader(); reader.onload = event => { var _event$target; return resolve(event === null || event === void 0 ? void 0 : (_event$target = event.target) === null || _event$target === void 0 ? void 0 : _event$target.result); }; reader.readAsDataURL(blobSlice); }); } if (resource instanceof ArrayBuffer) { const slice = resource.slice(0, INITIAL_DATA_LENGTH); const base64 = arrayBufferToBase64(slice); return "data:base64,".concat(base64); } return null; } function arrayBufferToBase64(buffer) { let binary = ''; const bytes = new Uint8Array(buffer); for (let i = 0; i < bytes.byteLength; i++) { binary += String.fromCharCode(bytes[i]); } return btoa(binary); } async function fetchFile(url, options) { if (typeof url === 'string') { url = resolvePath(url); let fetchOptions = options; if (options !== null && options !== void 0 && options.fetch && typeof (options === null || options === void 0 ? void 0 : options.fetch) !== 'function') { fetchOptions = options.fetch; } return await fetch(url, fetchOptions); } return await makeResponse(url); } function isElectron(mockUserAgent) { if (typeof window !== 'undefined' && typeof window.process === 'object' && window.process.type === 'renderer') { return true; } if (typeof process !== 'undefined' && typeof process.versions === 'object' && Boolean(process.versions.electron)) { return true; } const realUserAgent = typeof navigator === 'object' && typeof navigator.userAgent === 'string' && navigator.userAgent; const userAgent = mockUserAgent || realUserAgent; if (userAgent && userAgent.indexOf('Electron') >= 0) { return true; } return false; } function isBrowser() { const isNode = typeof process === 'object' && String(process) === '[object process]' && !process.browser; return !isNode || isElectron(); } const globals = { self: typeof self !== 'undefined' && self, window: typeof window !== 'undefined' && window, global: typeof global !== 'undefined' && global, document: typeof document !== 'undefined' && document, process: typeof process === 'object' && process }; const window_ = globals.window || globals.self || globals.global; const process_ = globals.process || {}; const VERSION$6 = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'untranspiled source'; isBrowser(); function getStorage(type) { try { const storage = window[type]; const x = '__storage_test__'; storage.setItem(x, x); storage.removeItem(x); return storage; } catch (e) { return null; } } class LocalStorage { constructor(id) { let defaultSettings = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; let type = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'sessionStorage'; _defineProperty(this, "storage", void 0); _defineProperty(this, "id", void 0); _defineProperty(this, "config", {}); this.storage = getStorage(type); this.id = id; this.config = {}; Object.assign(this.config, defaultSettings); this._loadConfiguration(); } getConfiguration() { return this.config; } setConfiguration(configuration) { this.config = {}; return this.updateConfiguration(configuration); } updateConfiguration(configuration) { Object.assign(this.config, configuration); if (this.storage) { const serialized = JSON.stringify(this.config); this.storage.setItem(this.id, serialized); } return this; } _loadConfiguration() { let configuration = {}; if (this.storage) { const serializedConfiguration = this.storage.getItem(this.id); configuration = serializedConfiguration ? JSON.parse(serializedConfiguration) : {}; } Object.assign(this.config, configuration); return this; } } function formatTime(ms) { let formatted; if (ms < 10) { formatted = "".concat(ms.toFixed(2), "ms"); } else if (ms < 100) { formatted = "".concat(ms.toFixed(1), "ms"); } else if (ms < 1000) { formatted = "".concat(ms.toFixed(0), "ms"); } else { formatted = "".concat((ms / 1000).toFixed(2), "s"); } return formatted; } function leftPad(string) { let length = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 8; const padLength = Math.max(length - string.length, 0); return "".concat(' '.repeat(padLength)).concat(string); } function formatImage(image, message, scale) { let maxWidth = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : 600; const imageUrl = image.src.replace(/\(/g, '%28').replace(/\)/g, '%29'); if (image.width > maxWidth) { scale = Math.min(scale, maxWidth / image.width); } const width = image.width * scale; const height = image.height * scale; const style = ['font-size:1px;', "padding:".concat(Math.floor(height / 2), "px ").concat(Math.floor(width / 2), "px;"), "line-height:".concat(height, "px;"), "background:url(".concat(imageUrl, ");"), "background-size:".concat(width, "px ").concat(height, "px;"), 'color:transparent;'].join(''); return ["".concat(message, " %c+"), style]; } let COLOR; (function (COLOR) { COLOR[COLOR["BLACK"] = 30] = "BLACK"; COLOR[COLOR["RED"] = 31] = "RED"; COLOR[COLOR["GREEN"] = 32] = "GREEN"; COLOR[COLOR["YELLOW"] = 33] = "YELLOW"; COLOR[COLOR["BLUE"] = 34] = "BLUE"; COLOR[COLOR["MAGENTA"] = 35] = "MAGENTA"; COLOR[COLOR["CYAN"] = 36] = "CYAN"; COLOR[COLOR["WHITE"] = 37] = "WHITE"; COLOR[COLOR["BRIGHT_BLACK"] = 90] = "BRIGHT_BLACK"; COLOR[COLOR["BRIGHT_RED"] = 91] = "BRIGHT_RED"; COLOR[COLOR["BRIGHT_GREEN"] = 92] = "BRIGHT_GREEN"; COLOR[COLOR["BRIGHT_YELLOW"] = 93] = "BRIGHT_YELLOW"; COLOR[COLOR["BRIGHT_BLUE"] = 94] = "BRIGHT_BLUE"; COLOR[COLOR["BRIGHT_MAGENTA"] = 95] = "BRIGHT_MAGENTA"; COLOR[COLOR["BRIGHT_CYAN"] = 96] = "BRIGHT_CYAN"; COLOR[COLOR["BRIGHT_WHITE"] = 97] = "BRIGHT_WHITE"; })(COLOR || (COLOR = {})); function getColor$1(color) { return typeof color === 'string' ? COLOR[color.toUpperCase()] || COLOR.WHITE : color; } function addColor(string, color, background) { if (!isBrowser && typeof string === 'string') { if (color) { color = getColor$1(color); string = "\x1B[".concat(color, "m").concat(string, "\x1B[39m"); } if (background) { color = getColor$1(background); string = "\x1B[".concat(background + 10, "m").concat(string, "\x1B[49m"); } } return string; } function autobind(obj) { let predefined = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['constructor']; const proto = Object.getPrototypeOf(obj); const propNames = Object.getOwnPropertyNames(proto); for (const key of propNames) { if (typeof obj[key] === 'function') { if (!predefined.find(name => key === name)) { obj[key] = obj[key].bind(obj); } } } } function assert$5(condition, message) { if (!condition) { throw new Error(message || 'Assertion failed'); } } function getHiResTimestamp() { let timestamp; if (isBrowser && 'performance' in window_) { var _window$performance, _window$performance$n; timestamp = window_ === null || window_ === void 0 ? void 0 : (_window$performance = window_.performance) === null || _window$performance === void 0 ? void 0 : (_window$performance$n = _window$performance.now) === null || _window$performance$n === void 0 ? void 0 : _window$performance$n.call(_window$performance); } else if ('hrtime' in process_) { var _process$hrtime; const timeParts = process_ === null || process_ === void 0 ? void 0 : (_process$hrtime = process_.hrtime) === null || _process$hrtime === void 0 ? void 0 : _process$hrtime.call(process_); timestamp = timeParts[0] * 1000 + timeParts[1] / 1e6; } else { timestamp = Date.now(); } return timestamp; } const originalConsole = { debug: isBrowser ? console.debug || console.log : console.log, log: console.log, info: console.info, warn: console.warn, error: console.error }; const DEFAULT_SETTINGS = { enabled: true, level: 0 }; function noop$1() {} const cache = {}; const ONCE = { once: true }; class Log { constructor() { let { id } = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : { id: '' }; _defineProperty(this, "id", void 0); _defineProperty(this, "VERSION", VERSION$6); _defineProperty(this, "_startTs", getHiResTimestamp()); _defineProperty(this, "_deltaTs", getHiResTimestamp()); _defineProperty(this, "_storage", void 0); _defineProperty(this, "userData", {}); _defineProperty(this, "LOG_THROTTLE_TIMEOUT", 0); this.id = id; this._storage = new LocalStorage("__probe-".concat(this.id, "__"), DEFAULT_SETTINGS); this.userData = {}; this.timeStamp("".concat(this.id, " started")); autobind(this); Object.seal(this); } set level(newLevel) { this.setLevel(newLevel); } get level() { return this.getLevel(); } isEnabled() { return this._storage.config.enabled; } getLevel() { return this._storage.config.level; } getTotal() { return Number((getHiResTimestamp() - this._startTs).toPrecision(10)); } getDelta() { return Number((getHiResTimestamp() - this._deltaTs).toPrecision(10)); } set priority(newPriority) { this.level = newPriority; } get priority() { return this.level; } getPriority() { return this.level; } enable() { let enabled = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true; this._storage.updateConfiguration({ enabled }); return this; } setLevel(level) { this._storage.updateConfiguration({ level }); return this; } get(setting) { return this._storage.config[setting]; } set(setting, value) { this._storage.updateConfiguration({ [setting]: value }); } settings() { if (console.table) { console.table(this._storage.config); } else { console.log(this._storage.config); } } assert(condition, message) { assert$5(condition, message); } warn(message) { return this._getLogFunction(0, message, originalConsole.warn, arguments, ONCE); } error(message) { return this._getLogFunction(0, message, originalConsole.error, arguments); } deprecated(oldUsage, newUsage) { return this.warn("`".concat(oldUsage, "` is deprecated and will be removed in a later version. Use `").concat(newUsage, "` instead")); } removed(oldUsage, newUsage) { return this.error("`".concat(oldUsage, "` has been removed. Use `").concat(newUsage, "` instead")); } probe(logLevel, message) { return this._getLogFunction(logLevel, message, originalConsole.log, arguments, { time: true, once: true }); } log(logLevel, message) { return this._getLogFunction(logLevel, message, originalConsole.debug, arguments); } info(logLevel, message) { return this._getLogFunction(logLevel, message, console.info, arguments); } once(logLevel, message) { for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { args[_key - 2] = arguments[_key]; } return this._getLogFunction(logLevel, message, originalConsole.debug || originalConsole.info, arguments, ONCE); } table(logLevel, table, columns) { if (table) { return this._getLogFunction(logLevel, table, console.table || noop$1, columns && [columns], { tag: getTableHeader(table) }); } return noop$1; } image(_ref) { let { logLevel, priority, image, message = '', scale = 1 } = _ref; if (!this._shouldLog(logLevel || priority)) { return noop$1; } return isBrowser ? logImageInBrowser({ image, message, scale }) : logImageInNode({ image, message, scale }); } time(logLevel, message) { return this._getLogFunction(logLevel, message, console.time ? console.time : console.info); } timeEnd(logLevel, message) { return this._getLogFunction(logLevel, message, console.timeEnd ? console.timeEnd : console.info); } timeStamp(logLevel, message) { return this._getLogFunction(logLevel, message, console.timeStamp || noop$1); } group(logLevel, message) { let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : { collapsed: false }; const options = normalizeArguments({ logLevel, message, opts }); const { collapsed } = opts; options.method = (collapsed ? console.groupCollapsed : console.group) || console.info; return this._getLogFunction(options); } groupCollapsed(logLevel, message) { let opts = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; return this.group(logLevel, message, Object.assign({}, opts, { collapsed: true })); } groupEnd(logLevel) { return this._getLogFunction(logLevel, '', console.groupEnd || noop$1); } withGroup(logLevel, message, func) { this.group(logLevel, message)(); try { func(); } finally { this.groupEnd(logLevel)(); } } trace() { if (console.trace) { console.trace(); } } _shouldLog(logLevel) { return this.isEnabled() && this.getLevel() >= normalizeLogLevel(logLevel); } _getLogFunction(logLevel, message, method, args, opts) { if (this._shouldLog(logLevel)) { opts = normalizeArguments({ logLevel, message, args, opts }); method = method || opts.method; assert$5(method); opts.total = this.getTotal(); opts.delta = this.getDelta(); this._deltaTs = getHiResTimestamp(); const tag = opts.tag || opts.message; if (opts.once) { if (!cache[tag]) { cache[tag] = getHiResTimestamp(); } else { return noop$1; } } message = decorateMessage(this.id, opts.message, opts); return method.bind(console, message, ...opts.args); } return noop$1; } } _defineProperty(Log, "VERSION", VERSION$6); function normalizeLogLevel(logLevel) { if (!logLevel) { return 0; } let resolvedLevel; switch (typeof logLevel) { case 'number': resolvedLevel = logLevel; break; case 'object': resolvedLevel = logLevel.logLevel || logLevel.priority || 0; break; default: return 0; } assert$5(Number.isFinite(resolvedLevel) && resolvedLevel >= 0); return resolvedLevel; } function normalizeArguments(opts) { const { logLevel, message } = opts; opts.logLevel = normalizeLogLevel(logLevel); const args = opts.args ? Array.from(opts.args) : []; while (args.length && args.shift() !== message) {} switch (typeof logLevel) { case 'string': case 'function': if (message !== undefined) { args.unshift(message); } opts.message = logLevel; break; case 'object': Object.assign(opts, logLevel); break; } if (typeof opts.message === 'function') { opts.message = opts.message(); } const messageType = typeof opts.message; assert$5(messageType === 'string' || messageType === 'object'); return Object.assign(opts, { args }, opts.opts); } function decorateMessage(id, message, opts) { if (typeof message === 'string') { const time = opts.time ? leftPad(formatTime(opts.total)) : ''; message = opts.time ? "".concat(id, ": ").concat(time, " ").concat(message) : "".concat(id, ": ").concat(message); message = addColor(message, opts.color, opts.background); } return message; } function logImageInNode(_ref2) { let { image, message = '', scale = 1 } = _ref2; let asciify = null; try { asciify = module.require('asciify-image'); } catch (error) {} if (asciify) { return () => asciify(image, { fit: 'box', width: "".concat(Math.round(80 * scale), "%") }).then(data => console.log(data)); } return noop$1; } function logImageInBrowser(_ref3) { let { image, message = '', scale = 1 } = _ref3; if (typeof image === 'string') { const img = new Image(); img.onload = () => { const args = formatImage(img, message, scale); console.log(...args); }; img.src = image; return noop$1; } const element = image.nodeName || ''; if (element.toLowerCase() === 'img') { console.log(...formatImage(image, message, scale)); return noop$1; } if (element.toLowerCase() === 'canvas') { const img = new Image(); img.onload = () => console.log(...formatImage(img, message, scale)); img.src = image.toDataURL(); return noop$1; } return noop$1; } function getTableHeader(table) { for (const key in table) { for (const title in table[key]) { return title || 'untitled'; } } return 'empty'; } const probeLog = new Log({ id: 'loaders.gl' }); class NullLog { log() { return () => {}; } info() { return () => {}; } warn() { return () => {}; } error() { return () => {}; } } class ConsoleLog { constructor() { _defineProperty(this, "console", void 0); this.console = console; } log(...args) { return this.console.log.bind(this.console, ...args); } info(...args) { return this.console.info.bind(this.console, ...args); } warn(...args) { return this.console.warn.bind(this.console, ...args); } error(...args) { return this.console.error.bind(this.console, ...args); } } const DEFAULT_LOADER_OPTIONS = { fetch: null, mimeType: undefined, nothrow: false, log: new ConsoleLog(), CDN: 'https://unpkg.com/@loaders.gl', worker: true, maxConcurrency: 3, maxMobileConcurrency: 1, reuseWorkers: true, _workerType: '', limit: 0, _limitMB: 0, batchSize: 'auto', batchDebounceMs: 0, metadata: false, transforms: [] }; const REMOVED_LOADER_OPTIONS = { throws: 'nothrow', dataType: '(no longer used)', uri: 'baseUri', method: 'fetch.method', headers: 'fetch.headers', body: 'fetch.body', mode: 'fetch.mode', credentials: 'fetch.credentials', cache: 'fetch.cache', redirect: 'fetch.redirect', referrer: 'fetch.referrer', referrerPolicy: 'fetch.referrerPolicy', integrity: 'fetch.integrity', keepalive: 'fetch.keepalive', signal: 'fetch.signal' }; function getGlobalLoaderState() { globalThis.loaders = globalThis.loaders || {}; const { loaders } = globalThis; loaders._state = loaders._state || {}; return loaders._state; } const getGlobalLoaderOptions = () => { const state = getGlobalLoaderState(); state.globalOptions = state.globalOptions || { ...DEFAULT_LOADER_OPTIONS }; return state.globalOptions; }; function normalizeOptions(options, loader, loaders, url) { loaders = loaders || []; loaders = Array.isArray(loaders) ? loaders : [loaders]; validateOptions(options, loaders); return normalizeOptionsInternal(loader, options, url); } function getFetchFunction(options, context) { const globalOptions = getGlobalLoaderOptions(); const fetchOptions = options || globalOptions; if (typeof fetchOptions.fetch === 'function') { return fetchOptions.fetch; } if (isObject(fetchOptions.fetch)) { return url => fetchFile(url, fetchOptions); } if (context !== null && context !== void 0 && context.fetch) { return context === null || context === void 0 ? void 0 : context.fetch; } return fetchFile; } function validateOptions(options, loaders) { validateOptionsObject(options, null, DEFAULT_LOADER_OPTIONS, REMOVED_LOADER_OPTIONS, loaders); for (const loader of loaders) { const idOptions = options && options[loader.id] || {}; const loaderOptions = loader.options && loader.options[loader.id] || {}; const deprecatedOptions = loader.deprecatedOptions && loader.deprecatedOptions[loader.id] || {}; validateOptionsObject(idOptions, loader.id, loaderOptions, deprecatedOptions, loaders); } } function validateOptionsObject(options, id, defaultOptions, deprecatedOptions, loaders) { const loaderName = id || 'Top level'; const prefix = id ? "".concat(id, ".") : ''; for (const key in options) { const isSubOptions = !id && isObject(options[key]); const isBaseUriOption = key === 'baseUri' && !id; const isWorkerUrlOption = key === 'workerUrl' && id; if (!(key in defaultOptions) && !isBaseUriOption && !isWorkerUrlOption) { if (key in deprecatedOptions) { probeLog.warn("".concat(loaderName, " loader option '").concat(prefix).concat(key, "' no longer supported, use '").concat(deprecatedOptions[key], "'"))(); } else if (!isSubOptions) { const suggestion = findSimilarOption(key, loaders); probeLog.warn("".concat(loaderName, " loader option '").concat(prefix).concat(key, "' not recognized. ").concat(suggestion))(); } } } } function findSimilarOption(optionKey, loaders) { const lowerCaseOptionKey = optionKey.toLowerCase(); let bestSuggestion = ''; for (const loader of loaders) { for (const key in loader.options) { if (optionKey === key) { return "Did you mean '".concat(loader.id, ".").concat(key, "'?"); } const lowerCaseKey = key.toLowerCase(); const isPartialMatch = lowerCaseOptionKey.startsWith(lowerCaseKey) || lowerCaseKey.startsWith(lowerCaseOptionKey); if (isPartialMatch) { bestSuggestion = bestSuggestion || "Did you mean '".concat(loader.id, ".").concat(key, "'?"); } } } return bestSuggestion; } function normalizeOptionsInternal(loader, options, url) { const loaderDefaultOptions = loader.options || {}; const mergedOptions = { ...loaderDefaultOptions }; addUrlOptions(mergedOptions, url); if (mergedOptions.log === null) { mergedOptions.log = new NullLog(); } mergeNestedFields(mergedOptions, getGlobalLoaderOptions()); mergeNestedFields(mergedOptions, options); return mergedOptions; } function mergeNestedFields(mergedOptions, options) { for (const key in options) { if (key in options) { const value = options[key]; if (isPureObject(value) && isPureObject(mergedOptions[key])) { mergedOptions[key] = { ...mergedOptions[key], ...options[key] }; } else { mergedOptions[key] = options[key]; } } } } function addUrlOptions(options, url) { if (url && !('baseUri' in options)) { options.baseUri = url; } } function isLoaderObject(loader) { var _loader; if (!loader) { return false; } if (Array.isArray(loader)) { loader = loader[0]; } const hasExtensions = Array.isArray((_loader = loader) === null || _loader === void 0 ? void 0 : _loader.extensions); return hasExtensions; } function normalizeLoader(loader) { var _loader2, _loader3; assert$7(loader, 'null loader'); assert$7(isLoaderObject(loader), 'invalid loader'); let options; if (Array.isArray(loader)) { options = loader[1]; loader = loader[0]; loader = { ...loader, options: { ...loader.options, ...options } }; } if ((_loader2 = loader) !== null && _loader2 !== void 0 && _loader2.parseTextSync || (_loader3 = loader) !== null && _loader3 !== void 0 && _loader3.parseText) { loader.text = true; } if (!loader.text) { loader.binary = true; } return loader; } const getGlobalLoaderRegistry = () => { const state = getGlobalLoaderState(); state.loaderRegistry = state.loaderRegistry || []; return state.loaderRegistry; }; function getRegisteredLoaders() { return getGlobalLoaderRegistry(); } const EXT_PATTERN = /\.([^.]+)$/; async function selectLoader(data, loaders = [], options, context) { if (!validHTTPResponse(data)) { return null; } let loader = selectLoaderSync(data, loaders, { ...options, nothrow: true }, context); if (loader) { return loader; } if (isBlob(data)) { data = await data.slice(0, 10).arrayBuffer(); loader = selectLoaderSync(data, loaders, options, context); } if (!loader && !(options !== null && options !== void 0 && options.nothrow)) { throw new Error(getNoValidLoaderMessage(data)); } return loader; } function selectLoaderSync(data, loaders = [], options, context) { if (!validHTTPResponse(data)) { return null; } if (loaders && !Array.isArray(loaders)) { return normalizeLoader(loaders); } let candidateLoaders = []; if (loaders) { candidateLoaders = candidateLoaders.concat(loaders); } if (!(options !== null && options !== void 0 && options.ignoreRegisteredLoaders)) { candidateLoaders.push(...getRegisteredLoaders()); } normalizeLoaders(candidateLoaders); const loader = selectLoaderInternal(data, candidateLoaders, options, context); if (!loader && !(options !== null && options !== void 0 && options.nothrow)) { throw new Error(getNoValidLoaderMessage(data)); } return loader; } function selectLoaderInternal(data, loaders, options, context) { const { url, type } = getResourceUrlAndType(data); const testUrl = url || (context === null || context === void 0 ? void 0 : context.url); let loader = null; if (options !== null && options !== void 0 && options.mimeType) { loader = findLoaderByMIMEType(loaders, options === null || options === void 0 ? void 0 : options.mimeType); } loader = loader || findLoaderByUrl(loaders, testUrl); loader = loader || findLoaderByMIMEType(loaders, type); loader = loader || findLoaderByInitialBytes(loaders, data); loader = loader || findLoaderByMIMEType(loaders, options === null || options === void 0 ? void 0 : options.fallbackMimeType); return loader; } function validHTTPResponse(data) { if (data instanceof Response) { if (data.status === 204) { return false; } } return true; } function getNoValidLoaderMessage(data) { const { url, type } = getResourceUrlAndType(data); let message = 'No valid loader found ('; message += url ? "".concat(filename(url), ", ") : 'no url provided, '; message += "MIME type: ".concat(type ? "\"".concat(type, "\"") : 'not provided', ", "); const firstCharacters = data ? getFirstCharacters(data) : ''; message += firstCharacters ? " first bytes: \"".concat(firstCharacters, "\"") : 'first bytes: not available'; message += ')'; return message; } function normalizeLoaders(loaders) { for (const loader of loaders) { normalizeLoader(loader); } } function findLoaderByUrl(loaders, url) { const match = url && EXT_PATTERN.exec(url); const extension = match && match[1]; return extension ? findLoaderByExtension(loaders, extension) : null; } function findLoaderByExtension(loaders, extension) { extension = extension.toLowerCase(); for (const loader of loaders) { for (const loaderExtension of loader.extensions) { if (loaderExtension.toLowerCase() === extension) { return loader; } } } return null; } function findLoaderByMIMEType(loaders, mimeType) { for (const loader of loaders) { if (loader.mimeTypes && loader.mimeTypes.includes(mimeType)) { return loader; } if (mimeType === "application/x.".concat(loader.id)) { return loader; } } return null; } function findLoaderByInitialBytes(loaders, data) { if (!data) { return null; } for (const loader of loaders) { if (typeof data === 'string') { if (testDataAgainstText(data, loader)) { return loader; } } else if (ArrayBuffer.isView(data)) { if (testDataAgainstBinary(data.buffer, data.byteOffset, loader)) { return loader; } } else if (data instanceof ArrayBuffer) { const byteOffset = 0; if (testDataAgainstBinary(data, byteOffset, loader)) { return loader; } } } return null; } function testDataAgainstText(data, loader) { if (loader.testText) { return loader.testText(data); } const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests]; return tests.some(test => data.startsWith(test)); } function testDataAgainstBinary(data, byteOffset, loader) { const tests = Array.isArray(loader.tests) ? loader.tests : [loader.tests]; return tests.some(test => testBinary(data, byteOffset, loader, test)); } function testBinary(data, byteOffset, loader, test) { if (test instanceof ArrayBuffer) { return compareArrayBuffers(test, data, test.byteLength); } switch (typeof test) { case 'function': return test(data, loader); case 'string': const magic = getMagicString$2(data, byteOffset, test.length); return test === magic; default: return false; } } function getFirstCharacters(data, length = 5) { if (typeof data === 'string') { return data.slice(0, length); } else if (ArrayBuffer.isView(data)) { return getMagicString$2(data.buffer, data.byteOffset, length); } else if (data instanceof ArrayBuffer) { const byteOffset = 0; return getMagicString$2(data, byteOffset, length); } return ''; } function getMagicString$2(arrayBuffer, byteOffset, length) { if (arrayBuffer.byteLength < byteOffset + length) { return ''; } const dataView = new DataView(arrayBuffer); let magic = ''; for (let i = 0; i < length; i++) { magic += String.fromCharCode(dataView.getUint8(byteOffset + i)); } return magic; } const DEFAULT_CHUNK_SIZE$2 = 256 * 1024; function* makeStringIterator(string, options) { const chunkSize = (options === null || options === void 0 ? void 0 : options.chunkSize) || DEFAULT_CHUNK_SIZE$2; let offset = 0; const textEncoder = new TextEncoder(); while (offset < string.length) { const chunkLength = Math.min(string.length - offset, chunkSize); const chunk = string.slice(offset, offset + chunkLength); offset += chunkLength; yield textEncoder.encode(chunk); } } const DEFAULT_CHUNK_SIZE$1 = 256 * 1024; function* makeArrayBufferIterator(arrayBuffer, options = {}) { const { chunkSize = DEFAULT_CHUNK_SIZE$1 } = options; let byteOffset = 0; while (byteOffset < arrayBuffer.byteLength) { const chunkByteLength = Math.min(arrayBuffer.byteLength - byteOffset, chunkSize); const chunk = new ArrayBuffer(chunkByteLength); const sourceArray = new Uint8Array(arrayBuffer, byteOffset, chunkByteLength); const chunkArray = new Uint8Array(chunk); chunkArray.set(sourceArray); byteOffset += chunkByteLength; yield chunk; } } const DEFAULT_CHUNK_SIZE = 1024 * 1024; async function* makeBlobIterator(blob, options) { const chunkSize = (options === null || options === void 0 ? void 0 : options.chunkSize) || DEFAULT_CHUNK_SIZE; let offset = 0; while (offset < blob.size) { const end = offset + chunkSize; const chunk = await blob.slice(offset, end).arrayBuffer(); offset = end; yield chunk; } } function makeStreamIterator(stream, options) { return isBrowser$2 ? makeBrowserStreamIterator(stream, options) : makeNodeStreamIterator(stream); } async function* makeBrowserStreamIterator(stream, options) { const reader = stream.getReader(); let nextBatchPromise; try { while (true) { const currentBatchPromise = nextBatchPromise || reader.read(); if (options !== null && options !== void 0 && options._streamReadAhead) { nextBatchPromise = reader.read(); } const { done, value } = await currentBatchPromise; if (done) { return; } yield toArrayBuffer(value); } } catch (error) { reader.releaseLock(); } } async function* makeNodeStreamIterator(stream, options) { for await (const chunk of stream) { yield toArrayBuffer(chunk); } } function makeIterator(data, options) { if (typeof data === 'string') { return makeStringIterator(data, options); } if (data instanceof ArrayBuffer) { return makeArrayBufferIterator(data, options); } if (isBlob(data)) { return makeBlobIterator(data, options); } if (isReadableStream(data)) { return makeStreamIterator(data, options); } if (isResponse(data)) { const response = data; return makeStreamIterator(response.body, options); } throw new Error('makeIterator'); } const ERR_DATA = 'Cannot convert supplied data type'; function getArrayBufferOrStringFromDataSync(data, loader, options) { if (loader.text && typeof data === 'string') { return data; } if (isBuffer(data)) { data = data.buffer; } if (data instanceof ArrayBuffer) { const arrayBuffer = data; if (loader.text && !loader.binary) { const textDecoder = new TextDecoder('utf8'); return textDecoder.decode(arrayBuffer); } return arrayBuffer; } if (ArrayBuffer.isView(data)) { if (loader.text && !loader.binary) { const textDecoder = new TextDecoder('utf8'); return textDecoder.decode(data); } let arrayBuffer = data.buffer; const byteLength = data.byteLength || data.length; if (data.byteOffset !== 0 || byteLength !== arrayBuffer.byteLength) { arrayBuffer = arrayBuffer.slice(data.byteOffset, data.byteOffset + byteLength); } return arrayBuffer; } throw new Error(ERR_DATA); } async function getArrayBufferOrStringFromData(data, loader, options) { const isArrayBuffer = data instanceof ArrayBuffer || ArrayBuffer.isView(data); if (typeof data === 'string' || isArrayBuffer) { return getArrayBufferOrStringFromDataSync(data, loader); } if (isBlob(data)) { data = await makeResponse(data); } if (isResponse(data)) { const response = data; await checkResponse(response); return loader.binary ? await response.arrayBuffer() : await response.text(); } if (isReadableStream(data)) { data = makeIterator(data, options); } if (isIterable(data) || isAsyncIterable(data)) { return concatenateArrayBuffersAsync(data); } throw new Error(ERR_DATA); } function getLoaderContext(context, options, previousContext = null) { if (previousContext) { return previousContext; } const resolvedContext = { fetch: getFetchFunction(options, context), ...context }; if (!Array.isArray(resolvedContext.loaders)) { resolvedContext.loaders = null; } return resolvedContext; } function getLoadersFromContext(loaders, context) { if (!context && loaders && !Array.isArray(loaders)) { return loaders; } let candidateLoaders; if (loaders) { candidateLoaders = Array.isArray(loaders) ? loaders : [loaders]; } if (context && context.loaders) { const contextLoaders = Array.isArray(context.loaders) ? context.loaders : [context.loaders]; candidateLoaders = candidateLoaders ? [...candidateLoaders, ...contextLoaders] : contextLoaders; } return candidateLoaders && candidateLoaders.length ? candidateLoaders : null; } async function parse$3(data, loaders, options, context) { assert$6(!context || typeof context === 'object'); if (loaders && !Array.isArray(loaders) && !isLoaderObject(loaders)) { context = undefined; options = loaders; loaders = undefined; } data = await data; options = options || {}; const { url } = getResourceUrlAndType(data); const typedLoaders = loaders; const candidateLoaders = getLoadersFromContext(typedLoaders, context); const loader = await selectLoader(data, candidateLoaders, options); if (!loader) { return null; } options = normalizeOptions(options, loader, candidateLoaders, url); context = getLoaderContext({ url, parse: parse$3, loaders: candidateLoaders }, options, context); return await parseWithLoader(loader, data, options, context); } async function parseWithLoader(loader, data, options, context) { validateWorkerVersion(loader); data = await getArrayBufferOrStringFromData(data, loader, options); if (loader.parseTextSync && typeof data === 'string') { options.dataType = 'text'; return loader.parseTextSync(data, options, context, loader); } if (canParseWithWorker(loader, options)) { return await parseWithWorker(loader, data, options, context, parse$3); } if (loader.parseText && typeof data === 'string') { return await loader.parseText(data, options, context, loader); } if (loader.parse) { return await loader.parse(data, options, context, loader); } assert$6(!loader.parseSync); throw new Error("".concat(loader.id, " loader - no parser found and worker is disabled")); } async function load(url, loaders, options, context) { if (!Array.isArray(loaders) && !isLoaderObject(loaders)) { options = loaders; loaders = undefined; } const fetch = getFetchFunction(options); let data = url; if (typeof url === 'string') { data = await fetch(url); } if (isBlob(url)) { data = await fetch(url); } return await parse$3(data, loaders, options); } function assert$4(condition, message) { if (!condition) { throw new Error("math.gl assertion ".concat(message)); } } const RADIANS_TO_DEGREES = 1 / Math.PI * 180; const DEGREES_TO_RADIANS = 1 / 180 * Math.PI; const config$2 = {}; config$2.EPSILON = 1e-12; config$2.debug = false; config$2.precision = 4; config$2.printTypes = false; config$2.printDegrees = false; config$2.printRowMajor = true; function round(value) { return Math.round(value / config$2.EPSILON) * config$2.EPSILON; } function formatValue(value, { precision = config$2.precision || 4 } = {}) { value = round(value); return "".concat(parseFloat(value.toPrecision(precision))); } function isArray(value) { return Array.isArray(value) || ArrayBuffer.isView(value) && !(value instanceof DataView); } function duplicateArray(array) { return array.clone ? array.clone() : new Array(array.length); } function map$1(value, func, result) { if (isArray(value)) { result = result || duplicateArray(value); for (let i = 0; i < result.length && i < value.length; ++i) { result[i] = func(value[i], i, result); } return result; } return func(value); } function toRadians(degrees) { return radians(degrees); } function toDegrees(radians) { return degrees(radians); } function radians(degrees, result) { return map$1(degrees, degrees => degrees * DEGREES_TO_RADIANS, result); } function degrees(radians, result) { return map$1(radians, radians => radians * RADIANS_TO_DEGREES, result); } function clamp(value, min, max) { return map$1(value, value => Math.max(min, Math.min(max, value))); } function equals$1(a, b, epsilon) { const oldEpsilon = config$2.EPSILON; if (epsilon) { config$2.EPSILON = epsilon; } try { if (a === b) { return true; } if (isArray(a) && isArray(b)) { if (a.length !== b.length) { return false; } for (let i = 0; i < a.length; ++i) { if (!equals$1(a[i], b[i])) { return false; } } return true; } if (a && a.equals) { return a.equals(b); } if (b && b.equals) { return b.equals(a); } if (Number.isFinite(a) && Number.isFinite(b)) { return Math.abs(a - b) <= config$2.EPSILON * Math.max(1.0, Math.abs(a), Math.abs(b)); } return false; } finally { config$2.EPSILON = oldEpsilon; } } function _extendableBuiltin(cls) { function ExtendableBuiltin() { var instance = Reflect.construct(cls, Array.from(arguments)); Object.setPrototypeOf(instance, Object.getPrototypeOf(this)); return instance; } ExtendableBuiltin.prototype = Object.create(cls.prototype, { constructor: { value: cls, enumerable: false, writable: true, configurable: true } }); if (Object.setPrototypeOf) { Object.setPrototypeOf(ExtendableBuiltin, cls); } else { ExtendableBuiltin.__proto__ = cls; } return ExtendableBuiltin; } class MathArray extends _extendableBuiltin(Array) { get ELEMENTS() { assert$4(false); return 0; } clone() { return new this.constructor().copy(this); } from(arrayOrObject) { return Array.isArray(arrayOrObject) ? this.copy(arrayOrObject) : this.fromObject(arrayOrObject); } fromArray(array, offset = 0) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] = array[i + offset]; } return this.check(); } to(arrayOrObject) { if (arrayOrObject === this) { return this; } return isArray(arrayOrObject) ? this.toArray(arrayOrObject) : this.toObject(arrayOrObject); } toTarget(target) { return target ? this.to(target) : this; } toArray(array = [], offset = 0) { for (let i = 0; i < this.ELEMENTS; ++i) { array[offset + i] = this[i]; } return array; } toFloat32Array() { return new Float32Array(this); } toString() { return this.formatString(config$2); } formatString(opts) { let string = ''; for (let i = 0; i < this.ELEMENTS; ++i) { string += (i > 0 ? ', ' : '') + formatValue(this[i], opts); } return "".concat(opts.printTypes ? this.constructor.name : '', "[").concat(string, "]"); } equals(array) { if (!array || this.length !== array.length) { return false; } for (let i = 0; i < this.ELEMENTS; ++i) { if (!equals$1(this[i], array[i])) { return false; } } return true; } exactEquals(array) { if (!array || this.length !== array.length) { return false; } for (let i = 0; i < this.ELEMENTS; ++i) { if (this[i] !== array[i]) { return false; } } return true; } negate() { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] = -this[i]; } return this.check(); } lerp(a, b, t) { if (t === undefined) { t = b; b = a; a = this; } for (let i = 0; i < this.ELEMENTS; ++i) { const ai = a[i]; this[i] = ai + t * (b[i] - ai); } return this.check(); } min(vector) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] = Math.min(vector[i], this[i]); } return this.check(); } max(vector) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] = Math.max(vector[i], this[i]); } return this.check(); } clamp(minVector, maxVector) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] = Math.min(Math.max(this[i], minVector[i]), maxVector[i]); } return this.check(); } add(...vectors) { for (const vector of vectors) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] += vector[i]; } } return this.check(); } subtract(...vectors) { for (const vector of vectors) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] -= vector[i]; } } return this.check(); } scale(scale) { if (Array.isArray(scale)) { return this.multiply(scale); } for (let i = 0; i < this.ELEMENTS; ++i) { this[i] *= scale; } return this.check(); } sub(a) { return this.subtract(a); } setScalar(a) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] = a; } return this.check(); } addScalar(a) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] += a; } return this.check(); } subScalar(a) { return this.addScalar(-a); } multiplyScalar(scalar) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] *= scalar; } return this.check(); } divideScalar(a) { return this.scale(1 / a); } clampScalar(min, max) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] = Math.min(Math.max(this[i], min), max); } return this.check(); } multiplyByScalar(scalar) { return this.scale(scalar); } get elements() { return this; } check() { if (config$2.debug && !this.validate()) { throw new Error("math.gl: ".concat(this.constructor.name, " some fields set to invalid numbers'")); } return this; } validate() { let valid = this.length === this.ELEMENTS; for (let i = 0; i < this.ELEMENTS; ++i) { valid = valid && Number.isFinite(this[i]); } return valid; } } function validateVector(v, length) { if (v.length !== length) { return false; } for (let i = 0; i < v.length; ++i) { if (!Number.isFinite(v[i])) { return false; } } return true; } function checkNumber(value) { if (!Number.isFinite(value)) { throw new Error("Invalid number ".concat(value)); } return value; } function checkVector(v, length, callerName = '') { if (config$2.debug && !validateVector(v, length)) { throw new Error("math.gl: ".concat(callerName, " some fields set to invalid numbers'")); } return v; } const map = {}; function deprecated(method, version) { if (!map[method]) { map[method] = true; console.warn("".concat(method, " has been removed in version ").concat(version, ", see upgrade guide for more information")); } } class Vector extends MathArray { get ELEMENTS() { assert$4(false); return 0; } copy(vector) { assert$4(false); return this; } get x() { return this[0]; } set x(value) { this[0] = checkNumber(value); } get y() { return this[1]; } set y(value) { this[1] = checkNumber(value); } len() { return Math.sqrt(this.lengthSquared()); } magnitude() { return this.len(); } lengthSquared() { let length = 0; for (let i = 0; i < this.ELEMENTS; ++i) { length += this[i] * this[i]; } return length; } magnitudeSquared() { return this.lengthSquared(); } distance(mathArray) { return Math.sqrt(this.distanceSquared(mathArray)); } distanceSquared(mathArray) { let length = 0; for (let i = 0; i < this.ELEMENTS; ++i) { const dist = this[i] - mathArray[i]; length += dist * dist; } return checkNumber(length); } dot(mathArray) { let product = 0; for (let i = 0; i < this.ELEMENTS; ++i) { product += this[i] * mathArray[i]; } return checkNumber(product); } normalize() { const length = this.magnitude(); if (length !== 0) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] /= length; } } return this.check(); } multiply(...vectors) { for (const vector of vectors) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] *= vector[i]; } } return this.check(); } divide(...vectors) { for (const vector of vectors) { for (let i = 0; i < this.ELEMENTS; ++i) { this[i] /= vector[i]; } } return this.check(); } lengthSq() { return this.lengthSquared(); } distanceTo(vector) { return this.distance(vector); } distanceToSquared(vector) { return this.distanceSquared(vector); } getComponent(i) { assert$4(i >= 0 && i < this.ELEMENTS, 'index is out of range'); return checkNumber(this[i]); } setComponent(i, value) { assert$4(i >= 0 && i < this.ELEMENTS, 'index is out of range'); this[i] = value; return this.check(); } addVectors(a, b) { return this.copy(a).add(b); } subVectors(a, b) { return this.copy(a).subtract(b); } multiplyVectors(a, b) { return this.copy(a).multiply(b); } addScaledVector(a, b) { return this.add(new this.constructor(a).multiplyScalar(b)); } } /** * Common utilities * @module glMatrix */ // Configuration Constants var EPSILON = 0.000001; var ARRAY_TYPE = typeof Float32Array !== 'undefined' ? Float32Array : Array; if (!Math.hypot) Math.hypot = function () { var y = 0, i = arguments.length; while (i--) { y += arguments[i] * arguments[i]; } return Math.sqrt(y); }; /** * 2 Dimensional Vector * @module vec2 */ /** * Creates a new, empty vec2 * * @returns {vec2} a new 2D vector */ function create$4() { var out = new ARRAY_TYPE(2); if (ARRAY_TYPE != Float32Array) { out[0] = 0; out[1] = 0; } return out; } /** * Transforms the vec2 with a mat2 * * @param {vec2} out the receiving vector * @param {ReadonlyVec2} a the vector to transform * @param {ReadonlyMat2} m matrix to transform with * @returns {vec2} out */ function transformMat2(out, a, m) { var x = a[0], y = a[1]; out[0] = m[0] * x + m[2] * y; out[1] = m[1] * x + m[3] * y; return out; } /** * Transforms the vec2 with a mat2d * * @param {vec2} out the receiving vector * @param {ReadonlyVec2} a the vector to transform * @param {ReadonlyMat2d} m matrix to transform with * @returns {vec2} out */ function transformMat2d(out, a, m) { var x = a[0], y = a[1]; out[0] = m[0] * x + m[2] * y + m[4]; out[1] = m[1] * x + m[3] * y + m[5]; return out; } /** * Transforms the vec2 with a mat3 * 3rd vector component is implicitly '1' * * @param {vec2} out the receiving vector * @param {ReadonlyVec2} a the vector to transform * @param {ReadonlyMat3} m matrix to transform with * @returns {vec2} out */ function transformMat3$1(out, a, m) { var x = a[0], y = a[1]; out[0] = m[0] * x + m[3] * y + m[6]; out[1] = m[1] * x + m[4] * y + m[7]; return out; } /** * Transforms the vec2 with a mat4 * 3rd vector component is implicitly '0' * 4th vector component is implicitly '1' * * @param {vec2} out the receiving vector * @param {ReadonlyVec2} a the vector to transform * @param {ReadonlyMat4} m matrix to transform with * @returns {vec2} out */ function transformMat4$2(out, a, m) { var x = a[0]; var y = a[1]; out[0] = m[0] * x + m[4] * y + m[12]; out[1] = m[1] * x + m[5] * y + m[13]; return out; } /** * Perform some operation over an array of vec2s. * * @param {Array} a the array of vectors to iterate over * @param {Number} stride Number of elements between the start of each vec2. If 0 assumes tightly packed * @param {Number} offset Number of elements to skip at the beginning of the array * @param {Number} count Number of vec2s to iterate over. If 0 iterates over entire array * @param {Function} fn Function to call for each vector in the array * @param {Object} [arg] additional argument to pass to fn * @returns {Array} a * @function */ (function () { var vec = create$4(); return function (a, stride, offset, count, fn, arg) { var i, l; if (!stride) { stride = 2; } if (!offset) { offset = 0; } if (count) { l = Math.min(count * stride + offset, a.length); } else { l = a.length; } for (i = offset; i < l; i += stride) { vec[0] = a[i]; vec[1] = a[i + 1]; fn(vec, vec, arg); a[i] = vec[0]; a[i + 1] = vec[1]; } return a; }; })(); function vec2_transformMat4AsVector(out, a, m) { const x = a[0]; const y = a[1]; const w = m[3] * x + m[7] * y || 1.0; out[0] = (m[0] * x + m[4] * y) / w; out[1] = (m[1] * x + m[5] * y) / w; return out; } function vec3_transformMat4AsVector(out, a, m) { const x = a[0]; const y = a[1]; const z = a[2]; const w = m[3] * x + m[7] * y + m[11] * z || 1.0; out[0] = (m[0] * x + m[4] * y + m[8] * z) / w; out[1] = (m[1] * x + m[5] * y + m[9] * z) / w; out[2] = (m[2] * x + m[6] * y + m[10] * z) / w; return out; } function vec3_transformMat2(out, a, m) { const x = a[0]; const y = a[1]; out[0] = m[0] * x + m[2] * y; out[1] = m[1] * x + m[3] * y; out[2] = a[2]; return out; } function vec4_transformMat3(out, a, m) { const x = a[0]; const y = a[1]; const z = a[2]; out[0] = m[0] * x + m[3] * y + m[6] * z; out[1] = m[1] * x + m[4] * y + m[7] * z; out[2] = m[2] * x + m[5] * y + m[8] * z; out[3] = a[3]; return out; } class Vector2$1 extends Vector { constructor(x = 0, y = 0) { super(2); if (isArray(x) && arguments.length === 1) { this.copy(x); } else { if (config$2.debug) { checkNumber(x); checkNumber(y); } this[0] = x; this[1] = y; } } set(x, y) { this[0] = x; this[1] = y; return this.check(); } copy(array) { this[0] = array[0]; this[1] = array[1]; return this.check(); } fromObject(object) { if (config$2.debug) { checkNumber(object.x); checkNumber(object.y); } this[0] = object.x; this[1] = object.y; return this.check(); } toObject(object) { object.x = this[0]; object.y = this[1]; return object; } get ELEMENTS() { return 2; } horizontalAngle() { return Math.atan2(this.y, this.x); } verticalAngle() { return Math.atan2(this.x, this.y); } transform(matrix4) { return this.transformAsPoint(matrix4); } transformAsPoint(matrix4) { transformMat4$2(this, this, matrix4); return this.check(); } transformAsVector(matrix4) { vec2_transformMat4AsVector(this, this, matrix4); return this.check(); } transformByMatrix3(matrix3) { transformMat3$1(this, this, matrix3); return this.check(); } transformByMatrix2x3(matrix2x3) { transformMat2d(this, this, matrix2x3); return this.check(); } transformByMatrix2(matrix2) { transformMat2(this, this, matrix2); return this.check(); } } /** * 3 Dimensional Vector * @module vec3 */ /** * Creates a new, empty vec3 * * @returns {vec3} a new 3D vector */ function create$3() { var out = new ARRAY_TYPE(3); if (ARRAY_TYPE != Float32Array) { out[0] = 0; out[1] = 0; out[2] = 0; } return out; } /** * Calculates the length of a vec3 * * @param {ReadonlyVec3} a vector to calculate length of * @returns {Number} length of a */ function length$2(a) { var x = a[0]; var y = a[1]; var z = a[2]; return Math.hypot(x, y, z); } /** * Creates a new vec3 initialized with the given values * * @param {Number} x X component * @param {Number} y Y component * @param {Number} z Z component * @returns {vec3} a new 3D vector */ function fromValues(x, y, z) { var out = new ARRAY_TYPE(3); out[0] = x; out[1] = y; out[2] = z; return out; } /** * Normalize a vec3 * * @param {vec3} out the receiving vector * @param {ReadonlyVec3} a vector to normalize * @returns {vec3} out */ function normalize$2(out, a) { var x = a[0]; var y = a[1]; var z = a[2]; var len = x * x + y * y + z * z; if (len > 0) { //TODO: evaluate use of glm_invsqrt here? len = 1 / Math.sqrt(len); } out[0] = a[0] * len; out[1] = a[1] * len; out[2] = a[2] * len; return out; } /** * Calculates the dot product of two vec3's * * @param {ReadonlyVec3} a the first operand * @param {ReadonlyVec3} b the second operand * @returns {Number} dot product of a and b */ function dot$2(a, b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]; } /** * Computes the cross product of two vec3's * * @param {vec3} out the receiving vector * @param {ReadonlyVec3} a the first operand * @param {ReadonlyVec3} b the second operand * @returns {vec3} out */ function cross(out, a, b) { var ax = a[0], ay = a[1], az = a[2]; var bx = b[0], by = b[1], bz = b[2]; out[0] = ay * bz - az * by; out[1] = az * bx - ax * bz; out[2] = ax * by - ay * bx; return out; } /** * Transforms the vec3 with a mat4. * 4th vector component is implicitly '1' * * @param {vec3} out the receiving vector * @param {ReadonlyVec3} a the vector to transform * @param {ReadonlyMat4} m matrix to transform with * @returns {vec3} out */ function transformMat4$1(out, a, m) { var x = a[0], y = a[1], z = a[2]; var w = m[3] * x + m[7] * y + m[11] * z + m[15]; w = w || 1.0; out[0] = (m[0] * x + m[4] * y + m[8] * z + m[12]) / w; out[1] = (m[1] * x + m[5] * y + m[9] * z + m[13]) / w; out[2] = (m[2] * x + m[6] * y + m[10] * z + m[14]) / w; return out; } /** * Transforms the vec3 with a mat3. * * @param {vec3} out the receiving vector * @param {ReadonlyVec3} a the vector to transform * @param {ReadonlyMat3} m the 3x3 matrix to transform with * @returns {vec3} out */ function transformMat3(out, a, m) { var x = a[0], y = a[1], z = a[2]; out[0] = x * m[0] + y * m[3] + z * m[6]; out[1] = x * m[1] + y * m[4] + z * m[7]; out[2] = x * m[2] + y * m[5] + z * m[8]; return out; } /** * Transforms the vec3 with a quat * Can also be used for dual quaternions. (Multiply it with the real part) * * @param {vec3} out the receiving vector * @param {ReadonlyVec3} a the vector to transform * @param {ReadonlyQuat} q quaternion to transform with * @returns {vec3} out */ function transformQuat$1(out, a, q) { // benchmarks: https://jsperf.com/quaternion-transform-vec3-implementations-fixed var qx = q[0], qy = q[1], qz = q[2], qw = q[3]; var x = a[0], y = a[1], z = a[2]; // var qvec = [qx, qy, qz]; // var uv = vec3.cross([], qvec, a); var uvx = qy * z - qz * y, uvy = qz * x - qx * z, uvz = qx * y - qy * x; // var uuv = vec3.cross([], qvec, uv); var uuvx = qy * uvz - qz * uvy, uuvy = qz * uvx - qx * uvz, uuvz = qx * uvy - qy * uvx; // vec3.scale(uv, uv, 2 * w); var w2 = qw * 2; uvx *= w2; uvy *= w2; uvz *= w2; // vec3.scale(uuv, uuv, 2); uuvx *= 2; uuvy *= 2; uuvz *= 2; // return vec3.add(out, a, vec3.add(out, uv, uuv)); out[0] = x + uvx + uuvx; out[1] = y + uvy + uuvy; out[2] = z + uvz + uuvz; return out; } /** * Rotate a 3D vector around the x-axis * @param {vec3} out The receiving vec3 * @param {ReadonlyVec3} a The vec3 point to rotate * @param {ReadonlyVec3} b The origin of the rotation * @param {Number} rad The angle of rotation in radians * @returns {vec3} out */ function rotateX$2(out, a, b, rad) { var p = [], r = []; //Translate point to the origin p[0] = a[0] - b[0]; p[1] = a[1] - b[1]; p[2] = a[2] - b[2]; //perform rotation r[0] = p[0]; r[1] = p[1] * Math.cos(rad) - p[2] * Math.sin(rad); r[2] = p[1] * Math.sin(rad) + p[2] * Math.cos(rad); //translate to correct position out[0] = r[0] + b[0]; out[1] = r[1] + b[1]; out[2] = r[2] + b[2]; return out; } /** * Rotate a 3D vector around the y-axis * @param {vec3} out The receiving vec3 * @param {ReadonlyVec3} a The vec3 point to rotate * @param {ReadonlyVec3} b The origin of the rotation * @param {Number} rad The angle of rotation in radians * @returns {vec3} out */ function rotateY$2(out, a, b, rad) { var p = [], r = []; //Translate point to the origin p[0] = a[0] - b[0]; p[1] = a[1] - b[1]; p[2] = a[2] - b[2]; //perform rotation r[0] = p[2] * Math.sin(rad) + p[0] * Math.cos(rad); r[1] = p[1]; r[2] = p[2] * Math.cos(rad) - p[0] * Math.sin(rad); //translate to correct position out[0] = r[0] + b[0]; out[1] = r[1] + b[1]; out[2] = r[2] + b[2]; return out; } /** * Rotate a 3D vector around the z-axis * @param {vec3} out The receiving vec3 * @param {ReadonlyVec3} a The vec3 point to rotate * @param {ReadonlyVec3} b The origin of the rotation * @param {Number} rad The angle of rotation in radians * @returns {vec3} out */ function rotateZ$2(out, a, b, rad) { var p = [], r = []; //Translate point to the origin p[0] = a[0] - b[0]; p[1] = a[1] - b[1]; p[2] = a[2] - b[2]; //perform rotation r[0] = p[0] * Math.cos(rad) - p[1] * Math.sin(rad); r[1] = p[0] * Math.sin(rad) + p[1] * Math.cos(rad); r[2] = p[2]; //translate to correct position out[0] = r[0] + b[0]; out[1] = r[1] + b[1]; out[2] = r[2] + b[2]; return out; } /** * Get the angle between two 3D vectors * @param {ReadonlyVec3} a The first operand * @param {ReadonlyVec3} b The second operand * @returns {Number} The angle in radians */ function angle$1(a, b) { var ax = a[0], ay = a[1], az = a[2], bx = b[0], by = b[1], bz = b[2], mag1 = Math.sqrt(ax * ax + ay * ay + az * az), mag2 = Math.sqrt(bx * bx + by * by + bz * bz), mag = mag1 * mag2, cosine = mag && dot$2(a, b) / mag; return Math.acos(Math.min(Math.max(cosine, -1), 1)); } /** * Alias for {@link vec3.length} * @function */ var len = length$2; /** * Perform some operation over an array of vec3s. * * @param {Array} a the array of vectors to iterate over * @param {Number} stride Number of elements between the start of each vec3. If 0 assumes tightly packed * @param {Number} offset Number of elements to skip at the beginning of the array * @param {Number} count Number of vec3s to iterate over. If 0 iterates over entire array * @param {Function} fn Function to call for each vector in the array * @param {Object} [arg] additional argument to pass to fn * @returns {Array} a * @function */ (function () { var vec = create$3(); return function (a, stride, offset, count, fn, arg) { var i, l; if (!stride) { stride = 3; } if (!offset) { offset = 0; } if (count) { l = Math.min(count * stride + offset, a.length); } else { l = a.length; } for (i = offset; i < l; i += stride) { vec[0] = a[i]; vec[1] = a[i + 1]; vec[2] = a[i + 2]; fn(vec, vec, arg); a[i] = vec[0]; a[i + 1] = vec[1]; a[i + 2] = vec[2]; } return a; }; })(); const ORIGIN = [0, 0, 0]; const constants$2 = {}; class Vector3$1 extends Vector { static get ZERO() { return constants$2.ZERO = constants$2.ZERO || Object.freeze(new Vector3$1(0, 0, 0, 0)); } constructor(x = 0, y = 0, z = 0) { super(-0, -0, -0); if (arguments.length === 1 && isArray(x)) { this.copy(x); } else { if (config$2.debug) { checkNumber(x); checkNumber(y); checkNumber(z); } this[0] = x; this[1] = y; this[2] = z; } } set(x, y, z) { this[0] = x; this[1] = y; this[2] = z; return this.check(); } copy(array) { this[0] = array[0]; this[1] = array[1]; this[2] = array[2]; return this.check(); } fromObject(object) { if (config$2.debug) { checkNumber(object.x); checkNumber(object.y); checkNumber(object.z); } this[0] = object.x; this[1] = object.y; this[2] = object.z; return this.check(); } toObject(object) { object.x = this[0]; object.y = this[1]; object.z = this[2]; return object; } get ELEMENTS() { return 3; } get z() { return this[2]; } set z(value) { this[2] = checkNumber(value); } angle(vector) { return angle$1(this, vector); } cross(vector) { cross(this, this, vector); return this.check(); } rotateX({ radians, origin = ORIGIN }) { rotateX$2(this, this, origin, radians); return this.check(); } rotateY({ radians, origin = ORIGIN }) { rotateY$2(this, this, origin, radians); return this.check(); } rotateZ({ radians, origin = ORIGIN }) { rotateZ$2(this, this, origin, radians); return this.check(); } transform(matrix4) { return this.transformAsPoint(matrix4); } transformAsPoint(matrix4) { transformMat4$1(this, this, matrix4); return this.check(); } transformAsVector(matrix4) { vec3_transformMat4AsVector(this, this, matrix4); return this.check(); } transformByMatrix3(matrix3) { transformMat3(this, this, matrix3); return this.check(); } transformByMatrix2(matrix2) { vec3_transformMat2(this, this, matrix2); return this.check(); } transformByQuaternion(quaternion) { transformQuat$1(this, this, quaternion); return this.check(); } } class Matrix extends MathArray { get ELEMENTS() { assert$4(false); return 0; } get RANK() { assert$4(false); return 0; } toString() { let string = '['; if (config$2.printRowMajor) { string += 'row-major:'; for (let row = 0; row < this.RANK; ++row) { for (let col = 0; col < this.RANK; ++col) { string += " ".concat(this[col * this.RANK + row]); } } } else { string += 'column-major:'; for (let i = 0; i < this.ELEMENTS; ++i) { string += " ".concat(this[i]); } } string += ']'; return string; } getElementIndex(row, col) { return col * this.RANK + row; } getElement(row, col) { return this[col * this.RANK + row]; } setElement(row, col, value) { this[col * this.RANK + row] = checkNumber(value); return this; } getColumn(columnIndex, result = new Array(this.RANK).fill(-0)) { const firstIndex = columnIndex * this.RANK; for (let i = 0; i < this.RANK; ++i) { result[i] = this[firstIndex + i]; } return result; } setColumn(columnIndex, columnVector) { const firstIndex = columnIndex * this.RANK; for (let i = 0; i < this.RANK; ++i) { this[firstIndex + i] = columnVector[i]; } return this; } } /** * 3x3 Matrix * @module mat3 */ /** * Creates a new identity mat3 * * @returns {mat3} a new 3x3 matrix */ function create$2() { var out = new ARRAY_TYPE(9); if (ARRAY_TYPE != Float32Array) { out[1] = 0; out[2] = 0; out[3] = 0; out[5] = 0; out[6] = 0; out[7] = 0; } out[0] = 1; out[4] = 1; out[8] = 1; return out; } /** * Transpose the values of a mat3 * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the source matrix * @returns {mat3} out */ function transpose$1(out, a) { // If we are transposing ourselves we can skip a few steps but have to cache some values if (out === a) { var a01 = a[1], a02 = a[2], a12 = a[5]; out[1] = a[3]; out[2] = a[6]; out[3] = a01; out[5] = a[7]; out[6] = a02; out[7] = a12; } else { out[0] = a[0]; out[1] = a[3]; out[2] = a[6]; out[3] = a[1]; out[4] = a[4]; out[5] = a[7]; out[6] = a[2]; out[7] = a[5]; out[8] = a[8]; } return out; } /** * Inverts a mat3 * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the source matrix * @returns {mat3} out */ function invert$2(out, a) { var a00 = a[0], a01 = a[1], a02 = a[2]; var a10 = a[3], a11 = a[4], a12 = a[5]; var a20 = a[6], a21 = a[7], a22 = a[8]; var b01 = a22 * a11 - a12 * a21; var b11 = -a22 * a10 + a12 * a20; var b21 = a21 * a10 - a11 * a20; // Calculate the determinant var det = a00 * b01 + a01 * b11 + a02 * b21; if (!det) { return null; } det = 1.0 / det; out[0] = b01 * det; out[1] = (-a22 * a01 + a02 * a21) * det; out[2] = (a12 * a01 - a02 * a11) * det; out[3] = b11 * det; out[4] = (a22 * a00 - a02 * a20) * det; out[5] = (-a12 * a00 + a02 * a10) * det; out[6] = b21 * det; out[7] = (-a21 * a00 + a01 * a20) * det; out[8] = (a11 * a00 - a01 * a10) * det; return out; } /** * Calculates the determinant of a mat3 * * @param {ReadonlyMat3} a the source matrix * @returns {Number} determinant of a */ function determinant$1(a) { var a00 = a[0], a01 = a[1], a02 = a[2]; var a10 = a[3], a11 = a[4], a12 = a[5]; var a20 = a[6], a21 = a[7], a22 = a[8]; return a00 * (a22 * a11 - a12 * a21) + a01 * (-a22 * a10 + a12 * a20) + a02 * (a21 * a10 - a11 * a20); } /** * Multiplies two mat3's * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the first operand * @param {ReadonlyMat3} b the second operand * @returns {mat3} out */ function multiply$2(out, a, b) { var a00 = a[0], a01 = a[1], a02 = a[2]; var a10 = a[3], a11 = a[4], a12 = a[5]; var a20 = a[6], a21 = a[7], a22 = a[8]; var b00 = b[0], b01 = b[1], b02 = b[2]; var b10 = b[3], b11 = b[4], b12 = b[5]; var b20 = b[6], b21 = b[7], b22 = b[8]; out[0] = b00 * a00 + b01 * a10 + b02 * a20; out[1] = b00 * a01 + b01 * a11 + b02 * a21; out[2] = b00 * a02 + b01 * a12 + b02 * a22; out[3] = b10 * a00 + b11 * a10 + b12 * a20; out[4] = b10 * a01 + b11 * a11 + b12 * a21; out[5] = b10 * a02 + b11 * a12 + b12 * a22; out[6] = b20 * a00 + b21 * a10 + b22 * a20; out[7] = b20 * a01 + b21 * a11 + b22 * a21; out[8] = b20 * a02 + b21 * a12 + b22 * a22; return out; } /** * Translate a mat3 by the given vector * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the matrix to translate * @param {ReadonlyVec2} v vector to translate by * @returns {mat3} out */ function translate$1(out, a, v) { var a00 = a[0], a01 = a[1], a02 = a[2], a10 = a[3], a11 = a[4], a12 = a[5], a20 = a[6], a21 = a[7], a22 = a[8], x = v[0], y = v[1]; out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a10; out[4] = a11; out[5] = a12; out[6] = x * a00 + y * a10 + a20; out[7] = x * a01 + y * a11 + a21; out[8] = x * a02 + y * a12 + a22; return out; } /** * Rotates a mat3 by the given angle * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the matrix to rotate * @param {Number} rad the angle to rotate the matrix by * @returns {mat3} out */ function rotate$1(out, a, rad) { var a00 = a[0], a01 = a[1], a02 = a[2], a10 = a[3], a11 = a[4], a12 = a[5], a20 = a[6], a21 = a[7], a22 = a[8], s = Math.sin(rad), c = Math.cos(rad); out[0] = c * a00 + s * a10; out[1] = c * a01 + s * a11; out[2] = c * a02 + s * a12; out[3] = c * a10 - s * a00; out[4] = c * a11 - s * a01; out[5] = c * a12 - s * a02; out[6] = a20; out[7] = a21; out[8] = a22; return out; } /** * Scales the mat3 by the dimensions in the given vec2 * * @param {mat3} out the receiving matrix * @param {ReadonlyMat3} a the matrix to rotate * @param {ReadonlyVec2} v the vec2 to scale the matrix by * @returns {mat3} out **/ function scale$3(out, a, v) { var x = v[0], y = v[1]; out[0] = x * a[0]; out[1] = x * a[1]; out[2] = x * a[2]; out[3] = y * a[3]; out[4] = y * a[4]; out[5] = y * a[5]; out[6] = a[6]; out[7] = a[7]; out[8] = a[8]; return out; } /** * Calculates a 3x3 matrix from the given quaternion * * @param {mat3} out mat3 receiving operation result * @param {ReadonlyQuat} q Quaternion to create matrix from * * @returns {mat3} out */ function fromQuat$1(out, q) { var x = q[0], y = q[1], z = q[2], w = q[3]; var x2 = x + x; var y2 = y + y; var z2 = z + z; var xx = x * x2; var yx = y * x2; var yy = y * y2; var zx = z * x2; var zy = z * y2; var zz = z * z2; var wx = w * x2; var wy = w * y2; var wz = w * z2; out[0] = 1 - yy - zz; out[3] = yx - wz; out[6] = zx + wy; out[1] = yx + wz; out[4] = 1 - xx - zz; out[7] = zy - wx; out[2] = zx - wy; out[5] = zy + wx; out[8] = 1 - xx - yy; return out; } const IDENTITY$1 = Object.freeze([1, 0, 0, 0, 1, 0, 0, 0, 1]); const ZERO$1 = Object.freeze([0, 0, 0, 0, 0, 0, 0, 0, 0]); const INDICES$1 = Object.freeze({ COL0ROW0: 0, COL0ROW1: 1, COL0ROW2: 2, COL1ROW0: 3, COL1ROW1: 4, COL1ROW2: 5, COL2ROW0: 6, COL2ROW1: 7, COL2ROW2: 8 }); const constants$1 = {}; class Matrix3$1 extends Matrix { static get IDENTITY() { constants$1.IDENTITY = constants$1.IDENTITY || Object.freeze(new Matrix3$1(IDENTITY$1)); return constants$1.IDENTITY; } static get ZERO() { constants$1.ZERO = constants$1.ZERO || Object.freeze(new Matrix3$1(ZERO$1)); return constants$1.ZERO; } get ELEMENTS() { return 9; } get RANK() { return 3; } get INDICES() { return INDICES$1; } constructor(array) { super(-0, -0, -0, -0, -0, -0, -0, -0, -0); if (arguments.length === 1 && Array.isArray(array)) { this.copy(array); } else { this.identity(); } } copy(array) { this[0] = array[0]; this[1] = array[1]; this[2] = array[2]; this[3] = array[3]; this[4] = array[4]; this[5] = array[5]; this[6] = array[6]; this[7] = array[7]; this[8] = array[8]; return this.check(); } set(m00, m10, m20, m01, m11, m21, m02, m12, m22) { this[0] = m00; this[1] = m10; this[2] = m20; this[3] = m01; this[4] = m11; this[5] = m21; this[6] = m02; this[7] = m12; this[8] = m22; return this.check(); } setRowMajor(m00, m01, m02, m10, m11, m12, m20, m21, m22) { this[0] = m00; this[1] = m10; this[2] = m20; this[3] = m01; this[4] = m11; this[5] = m21; this[6] = m02; this[7] = m12; this[8] = m22; return this.check(); } determinant() { return determinant$1(this); } identity() { return this.copy(IDENTITY$1); } fromQuaternion(q) { fromQuat$1(this, q); return this.check(); } transpose() { transpose$1(this, this); return this.check(); } invert() { invert$2(this, this); return this.check(); } multiplyLeft(a) { multiply$2(this, a, this); return this.check(); } multiplyRight(a) { multiply$2(this, this, a); return this.check(); } rotate(radians) { rotate$1(this, this, radians); return this.check(); } scale(factor) { if (Array.isArray(factor)) { scale$3(this, this, factor); } else { scale$3(this, this, [factor, factor, factor]); } return this.check(); } translate(vec) { translate$1(this, this, vec); return this.check(); } transform(vector, result) { switch (vector.length) { case 2: result = transformMat3$1(result || [-0, -0], vector, this); break; case 3: result = transformMat3(result || [-0, -0, -0], vector, this); break; case 4: result = vec4_transformMat3(result || [-0, -0, -0, -0], vector, this); break; default: throw new Error('Illegal vector'); } checkVector(result, vector.length); return result; } transformVector(vector, result) { deprecated('Matrix3.transformVector'); return this.transform(vector, result); } transformVector2(vector, result) { deprecated('Matrix3.transformVector'); return this.transform(vector, result); } transformVector3(vector, result) { deprecated('Matrix3.transformVector'); return this.transform(vector, result); } } /** * Set a mat4 to the identity matrix * * @param {mat4} out the receiving matrix * @returns {mat4} out */ function identity$1(out) { out[0] = 1; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 0; out[5] = 1; out[6] = 0; out[7] = 0; out[8] = 0; out[9] = 0; out[10] = 1; out[11] = 0; out[12] = 0; out[13] = 0; out[14] = 0; out[15] = 1; return out; } /** * Transpose the values of a mat4 * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the source matrix * @returns {mat4} out */ function transpose(out, a) { // If we are transposing ourselves we can skip a few steps but have to cache some values if (out === a) { var a01 = a[1], a02 = a[2], a03 = a[3]; var a12 = a[6], a13 = a[7]; var a23 = a[11]; out[1] = a[4]; out[2] = a[8]; out[3] = a[12]; out[4] = a01; out[6] = a[9]; out[7] = a[13]; out[8] = a02; out[9] = a12; out[11] = a[14]; out[12] = a03; out[13] = a13; out[14] = a23; } else { out[0] = a[0]; out[1] = a[4]; out[2] = a[8]; out[3] = a[12]; out[4] = a[1]; out[5] = a[5]; out[6] = a[9]; out[7] = a[13]; out[8] = a[2]; out[9] = a[6]; out[10] = a[10]; out[11] = a[14]; out[12] = a[3]; out[13] = a[7]; out[14] = a[11]; out[15] = a[15]; } return out; } /** * Inverts a mat4 * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the source matrix * @returns {mat4} out */ function invert$1(out, a) { var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; var b00 = a00 * a11 - a01 * a10; var b01 = a00 * a12 - a02 * a10; var b02 = a00 * a13 - a03 * a10; var b03 = a01 * a12 - a02 * a11; var b04 = a01 * a13 - a03 * a11; var b05 = a02 * a13 - a03 * a12; var b06 = a20 * a31 - a21 * a30; var b07 = a20 * a32 - a22 * a30; var b08 = a20 * a33 - a23 * a30; var b09 = a21 * a32 - a22 * a31; var b10 = a21 * a33 - a23 * a31; var b11 = a22 * a33 - a23 * a32; // Calculate the determinant var det = b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; if (!det) { return null; } det = 1.0 / det; out[0] = (a11 * b11 - a12 * b10 + a13 * b09) * det; out[1] = (a02 * b10 - a01 * b11 - a03 * b09) * det; out[2] = (a31 * b05 - a32 * b04 + a33 * b03) * det; out[3] = (a22 * b04 - a21 * b05 - a23 * b03) * det; out[4] = (a12 * b08 - a10 * b11 - a13 * b07) * det; out[5] = (a00 * b11 - a02 * b08 + a03 * b07) * det; out[6] = (a32 * b02 - a30 * b05 - a33 * b01) * det; out[7] = (a20 * b05 - a22 * b02 + a23 * b01) * det; out[8] = (a10 * b10 - a11 * b08 + a13 * b06) * det; out[9] = (a01 * b08 - a00 * b10 - a03 * b06) * det; out[10] = (a30 * b04 - a31 * b02 + a33 * b00) * det; out[11] = (a21 * b02 - a20 * b04 - a23 * b00) * det; out[12] = (a11 * b07 - a10 * b09 - a12 * b06) * det; out[13] = (a00 * b09 - a01 * b07 + a02 * b06) * det; out[14] = (a31 * b01 - a30 * b03 - a32 * b00) * det; out[15] = (a20 * b03 - a21 * b01 + a22 * b00) * det; return out; } /** * Calculates the determinant of a mat4 * * @param {ReadonlyMat4} a the source matrix * @returns {Number} determinant of a */ function determinant(a) { var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; var b00 = a00 * a11 - a01 * a10; var b01 = a00 * a12 - a02 * a10; var b02 = a00 * a13 - a03 * a10; var b03 = a01 * a12 - a02 * a11; var b04 = a01 * a13 - a03 * a11; var b05 = a02 * a13 - a03 * a12; var b06 = a20 * a31 - a21 * a30; var b07 = a20 * a32 - a22 * a30; var b08 = a20 * a33 - a23 * a30; var b09 = a21 * a32 - a22 * a31; var b10 = a21 * a33 - a23 * a31; var b11 = a22 * a33 - a23 * a32; // Calculate the determinant return b00 * b11 - b01 * b10 + b02 * b09 + b03 * b08 - b04 * b07 + b05 * b06; } /** * Multiplies two mat4s * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the first operand * @param {ReadonlyMat4} b the second operand * @returns {mat4} out */ function multiply$1(out, a, b) { var a00 = a[0], a01 = a[1], a02 = a[2], a03 = a[3]; var a10 = a[4], a11 = a[5], a12 = a[6], a13 = a[7]; var a20 = a[8], a21 = a[9], a22 = a[10], a23 = a[11]; var a30 = a[12], a31 = a[13], a32 = a[14], a33 = a[15]; // Cache only the current line of the second matrix var b0 = b[0], b1 = b[1], b2 = b[2], b3 = b[3]; out[0] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; out[1] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; out[2] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; out[3] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; b0 = b[4]; b1 = b[5]; b2 = b[6]; b3 = b[7]; out[4] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; out[5] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; out[6] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; out[7] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; b0 = b[8]; b1 = b[9]; b2 = b[10]; b3 = b[11]; out[8] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; out[9] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; out[10] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; out[11] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; b0 = b[12]; b1 = b[13]; b2 = b[14]; b3 = b[15]; out[12] = b0 * a00 + b1 * a10 + b2 * a20 + b3 * a30; out[13] = b0 * a01 + b1 * a11 + b2 * a21 + b3 * a31; out[14] = b0 * a02 + b1 * a12 + b2 * a22 + b3 * a32; out[15] = b0 * a03 + b1 * a13 + b2 * a23 + b3 * a33; return out; } /** * Translate a mat4 by the given vector * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the matrix to translate * @param {ReadonlyVec3} v vector to translate by * @returns {mat4} out */ function translate(out, a, v) { var x = v[0], y = v[1], z = v[2]; var a00, a01, a02, a03; var a10, a11, a12, a13; var a20, a21, a22, a23; if (a === out) { out[12] = a[0] * x + a[4] * y + a[8] * z + a[12]; out[13] = a[1] * x + a[5] * y + a[9] * z + a[13]; out[14] = a[2] * x + a[6] * y + a[10] * z + a[14]; out[15] = a[3] * x + a[7] * y + a[11] * z + a[15]; } else { a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; out[0] = a00; out[1] = a01; out[2] = a02; out[3] = a03; out[4] = a10; out[5] = a11; out[6] = a12; out[7] = a13; out[8] = a20; out[9] = a21; out[10] = a22; out[11] = a23; out[12] = a00 * x + a10 * y + a20 * z + a[12]; out[13] = a01 * x + a11 * y + a21 * z + a[13]; out[14] = a02 * x + a12 * y + a22 * z + a[14]; out[15] = a03 * x + a13 * y + a23 * z + a[15]; } return out; } /** * Scales the mat4 by the dimensions in the given vec3 not using vectorization * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the matrix to scale * @param {ReadonlyVec3} v the vec3 to scale the matrix by * @returns {mat4} out **/ function scale$2(out, a, v) { var x = v[0], y = v[1], z = v[2]; out[0] = a[0] * x; out[1] = a[1] * x; out[2] = a[2] * x; out[3] = a[3] * x; out[4] = a[4] * y; out[5] = a[5] * y; out[6] = a[6] * y; out[7] = a[7] * y; out[8] = a[8] * z; out[9] = a[9] * z; out[10] = a[10] * z; out[11] = a[11] * z; out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15]; return out; } /** * Rotates a mat4 by the given angle around the given axis * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the matrix to rotate * @param {Number} rad the angle to rotate the matrix by * @param {ReadonlyVec3} axis the axis to rotate around * @returns {mat4} out */ function rotate(out, a, rad, axis) { var x = axis[0], y = axis[1], z = axis[2]; var len = Math.hypot(x, y, z); var s, c, t; var a00, a01, a02, a03; var a10, a11, a12, a13; var a20, a21, a22, a23; var b00, b01, b02; var b10, b11, b12; var b20, b21, b22; if (len < EPSILON) { return null; } len = 1 / len; x *= len; y *= len; z *= len; s = Math.sin(rad); c = Math.cos(rad); t = 1 - c; a00 = a[0]; a01 = a[1]; a02 = a[2]; a03 = a[3]; a10 = a[4]; a11 = a[5]; a12 = a[6]; a13 = a[7]; a20 = a[8]; a21 = a[9]; a22 = a[10]; a23 = a[11]; // Construct the elements of the rotation matrix b00 = x * x * t + c; b01 = y * x * t + z * s; b02 = z * x * t - y * s; b10 = x * y * t - z * s; b11 = y * y * t + c; b12 = z * y * t + x * s; b20 = x * z * t + y * s; b21 = y * z * t - x * s; b22 = z * z * t + c; // Perform rotation-specific matrix multiplication out[0] = a00 * b00 + a10 * b01 + a20 * b02; out[1] = a01 * b00 + a11 * b01 + a21 * b02; out[2] = a02 * b00 + a12 * b01 + a22 * b02; out[3] = a03 * b00 + a13 * b01 + a23 * b02; out[4] = a00 * b10 + a10 * b11 + a20 * b12; out[5] = a01 * b10 + a11 * b11 + a21 * b12; out[6] = a02 * b10 + a12 * b11 + a22 * b12; out[7] = a03 * b10 + a13 * b11 + a23 * b12; out[8] = a00 * b20 + a10 * b21 + a20 * b22; out[9] = a01 * b20 + a11 * b21 + a21 * b22; out[10] = a02 * b20 + a12 * b21 + a22 * b22; out[11] = a03 * b20 + a13 * b21 + a23 * b22; if (a !== out) { // If the source and destination differ, copy the unchanged last row out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15]; } return out; } /** * Rotates a matrix by the given angle around the X axis * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the matrix to rotate * @param {Number} rad the angle to rotate the matrix by * @returns {mat4} out */ function rotateX$1(out, a, rad) { var s = Math.sin(rad); var c = Math.cos(rad); var a10 = a[4]; var a11 = a[5]; var a12 = a[6]; var a13 = a[7]; var a20 = a[8]; var a21 = a[9]; var a22 = a[10]; var a23 = a[11]; if (a !== out) { // If the source and destination differ, copy the unchanged rows out[0] = a[0]; out[1] = a[1]; out[2] = a[2]; out[3] = a[3]; out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15]; } // Perform axis-specific matrix multiplication out[4] = a10 * c + a20 * s; out[5] = a11 * c + a21 * s; out[6] = a12 * c + a22 * s; out[7] = a13 * c + a23 * s; out[8] = a20 * c - a10 * s; out[9] = a21 * c - a11 * s; out[10] = a22 * c - a12 * s; out[11] = a23 * c - a13 * s; return out; } /** * Rotates a matrix by the given angle around the Y axis * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the matrix to rotate * @param {Number} rad the angle to rotate the matrix by * @returns {mat4} out */ function rotateY$1(out, a, rad) { var s = Math.sin(rad); var c = Math.cos(rad); var a00 = a[0]; var a01 = a[1]; var a02 = a[2]; var a03 = a[3]; var a20 = a[8]; var a21 = a[9]; var a22 = a[10]; var a23 = a[11]; if (a !== out) { // If the source and destination differ, copy the unchanged rows out[4] = a[4]; out[5] = a[5]; out[6] = a[6]; out[7] = a[7]; out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15]; } // Perform axis-specific matrix multiplication out[0] = a00 * c - a20 * s; out[1] = a01 * c - a21 * s; out[2] = a02 * c - a22 * s; out[3] = a03 * c - a23 * s; out[8] = a00 * s + a20 * c; out[9] = a01 * s + a21 * c; out[10] = a02 * s + a22 * c; out[11] = a03 * s + a23 * c; return out; } /** * Rotates a matrix by the given angle around the Z axis * * @param {mat4} out the receiving matrix * @param {ReadonlyMat4} a the matrix to rotate * @param {Number} rad the angle to rotate the matrix by * @returns {mat4} out */ function rotateZ$1(out, a, rad) { var s = Math.sin(rad); var c = Math.cos(rad); var a00 = a[0]; var a01 = a[1]; var a02 = a[2]; var a03 = a[3]; var a10 = a[4]; var a11 = a[5]; var a12 = a[6]; var a13 = a[7]; if (a !== out) { // If the source and destination differ, copy the unchanged last row out[8] = a[8]; out[9] = a[9]; out[10] = a[10]; out[11] = a[11]; out[12] = a[12]; out[13] = a[13]; out[14] = a[14]; out[15] = a[15]; } // Perform axis-specific matrix multiplication out[0] = a00 * c + a10 * s; out[1] = a01 * c + a11 * s; out[2] = a02 * c + a12 * s; out[3] = a03 * c + a13 * s; out[4] = a10 * c - a00 * s; out[5] = a11 * c - a01 * s; out[6] = a12 * c - a02 * s; out[7] = a13 * c - a03 * s; return out; } /** * Returns the scaling factor component of a transformation * matrix. If a matrix is built with fromRotationTranslationScale * with a normalized Quaternion paramter, the returned vector will be * the same as the scaling vector * originally supplied. * @param {vec3} out Vector to receive scaling factor component * @param {ReadonlyMat4} mat Matrix to be decomposed (input) * @return {vec3} out */ function getScaling(out, mat) { var m11 = mat[0]; var m12 = mat[1]; var m13 = mat[2]; var m21 = mat[4]; var m22 = mat[5]; var m23 = mat[6]; var m31 = mat[8]; var m32 = mat[9]; var m33 = mat[10]; out[0] = Math.hypot(m11, m12, m13); out[1] = Math.hypot(m21, m22, m23); out[2] = Math.hypot(m31, m32, m33); return out; } /** * Calculates a 4x4 matrix from the given quaternion * * @param {mat4} out mat4 receiving operation result * @param {ReadonlyQuat} q Quaternion to create matrix from * * @returns {mat4} out */ function fromQuat(out, q) { var x = q[0], y = q[1], z = q[2], w = q[3]; var x2 = x + x; var y2 = y + y; var z2 = z + z; var xx = x * x2; var yx = y * x2; var yy = y * y2; var zx = z * x2; var zy = z * y2; var zz = z * z2; var wx = w * x2; var wy = w * y2; var wz = w * z2; out[0] = 1 - yy - zz; out[1] = yx + wz; out[2] = zx - wy; out[3] = 0; out[4] = yx - wz; out[5] = 1 - xx - zz; out[6] = zy + wx; out[7] = 0; out[8] = zx + wy; out[9] = zy - wx; out[10] = 1 - xx - yy; out[11] = 0; out[12] = 0; out[13] = 0; out[14] = 0; out[15] = 1; return out; } /** * Generates a frustum matrix with the given bounds * * @param {mat4} out mat4 frustum matrix will be written into * @param {Number} left Left bound of the frustum * @param {Number} right Right bound of the frustum * @param {Number} bottom Bottom bound of the frustum * @param {Number} top Top bound of the frustum * @param {Number} near Near bound of the frustum * @param {Number} far Far bound of the frustum * @returns {mat4} out */ function frustum(out, left, right, bottom, top, near, far) { var rl = 1 / (right - left); var tb = 1 / (top - bottom); var nf = 1 / (near - far); out[0] = near * 2 * rl; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 0; out[5] = near * 2 * tb; out[6] = 0; out[7] = 0; out[8] = (right + left) * rl; out[9] = (top + bottom) * tb; out[10] = (far + near) * nf; out[11] = -1; out[12] = 0; out[13] = 0; out[14] = far * near * 2 * nf; out[15] = 0; return out; } /** * Generates a perspective projection matrix with the given bounds. * Passing null/undefined/no value for far will generate infinite projection matrix. * * @param {mat4} out mat4 frustum matrix will be written into * @param {number} fovy Vertical field of view in radians * @param {number} aspect Aspect ratio. typically viewport width/height * @param {number} near Near bound of the frustum * @param {number} far Far bound of the frustum, can be null or Infinity * @returns {mat4} out */ function perspective(out, fovy, aspect, near, far) { var f = 1.0 / Math.tan(fovy / 2), nf; out[0] = f / aspect; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 0; out[5] = f; out[6] = 0; out[7] = 0; out[8] = 0; out[9] = 0; out[11] = -1; out[12] = 0; out[13] = 0; out[15] = 0; if (far != null && far !== Infinity) { nf = 1 / (near - far); out[10] = (far + near) * nf; out[14] = 2 * far * near * nf; } else { out[10] = -1; out[14] = -2 * near; } return out; } /** * Generates a orthogonal projection matrix with the given bounds * * @param {mat4} out mat4 frustum matrix will be written into * @param {number} left Left bound of the frustum * @param {number} right Right bound of the frustum * @param {number} bottom Bottom bound of the frustum * @param {number} top Top bound of the frustum * @param {number} near Near bound of the frustum * @param {number} far Far bound of the frustum * @returns {mat4} out */ function ortho(out, left, right, bottom, top, near, far) { var lr = 1 / (left - right); var bt = 1 / (bottom - top); var nf = 1 / (near - far); out[0] = -2 * lr; out[1] = 0; out[2] = 0; out[3] = 0; out[4] = 0; out[5] = -2 * bt; out[6] = 0; out[7] = 0; out[8] = 0; out[9] = 0; out[10] = 2 * nf; out[11] = 0; out[12] = (left + right) * lr; out[13] = (top + bottom) * bt; out[14] = (far + near) * nf; out[15] = 1; return out; } /** * Generates a look-at matrix with the given eye position, focal point, and up axis. * If you want a matrix that actually makes an object look at another object, you should use targetTo instead. * * @param {mat4} out mat4 frustum matrix will be written into * @param {ReadonlyVec3} eye Position of the viewer * @param {ReadonlyVec3} center Point the viewer is looking at * @param {ReadonlyVec3} up vec3 pointing up * @returns {mat4} out */ function lookAt(out, eye, center, up) { var x0, x1, x2, y0, y1, y2, z0, z1, z2, len; var eyex = eye[0]; var eyey = eye[1]; var eyez = eye[2]; var upx = up[0]; var upy = up[1]; var upz = up[2]; var centerx = center[0]; var centery = center[1]; var centerz = center[2]; if (Math.abs(eyex - centerx) < EPSILON && Math.abs(eyey - centery) < EPSILON && Math.abs(eyez - centerz) < EPSILON) { return identity$1(out); } z0 = eyex - centerx; z1 = eyey - centery; z2 = eyez - centerz; len = 1 / Math.hypot(z0, z1, z2); z0 *= len; z1 *= len; z2 *= len; x0 = upy * z2 - upz * z1; x1 = upz * z0 - upx * z2; x2 = upx * z1 - upy * z0; len = Math.hypot(x0, x1, x2); if (!len) { x0 = 0; x1 = 0; x2 = 0; } else { len = 1 / len; x0 *= len; x1 *= len; x2 *= len; } y0 = z1 * x2 - z2 * x1; y1 = z2 * x0 - z0 * x2; y2 = z0 * x1 - z1 * x0; len = Math.hypot(y0, y1, y2); if (!len) { y0 = 0; y1 = 0; y2 = 0; } else { len = 1 / len; y0 *= len; y1 *= len; y2 *= len; } out[0] = x0; out[1] = y0; out[2] = z0; out[3] = 0; out[4] = x1; out[5] = y1; out[6] = z1; out[7] = 0; out[8] = x2; out[9] = y2; out[10] = z2; out[11] = 0; out[12] = -(x0 * eyex + x1 * eyey + x2 * eyez); out[13] = -(y0 * eyex + y1 * eyey + y2 * eyez); out[14] = -(z0 * eyex + z1 * eyey + z2 * eyez); out[15] = 1; return out; } /** * 4 Dimensional Vector * @module vec4 */ /** * Creates a new, empty vec4 * * @returns {vec4} a new 4D vector */ function create$1() { var out = new ARRAY_TYPE(4); if (ARRAY_TYPE != Float32Array) { out[0] = 0; out[1] = 0; out[2] = 0; out[3] = 0; } return out; } /** * Adds two vec4's * * @param {vec4} out the receiving vector * @param {ReadonlyVec4} a the first operand * @param {ReadonlyVec4} b the second operand * @returns {vec4} out */ function add$1(out, a, b) { out[0] = a[0] + b[0]; out[1] = a[1] + b[1]; out[2] = a[2] + b[2]; out[3] = a[3] + b[3]; return out; } /** * Scales a vec4 by a scalar number * * @param {vec4} out the receiving vector * @param {ReadonlyVec4} a the vector to scale * @param {Number} b amount to scale the vector by * @returns {vec4} out */ function scale$1(out, a, b) { out[0] = a[0] * b; out[1] = a[1] * b; out[2] = a[2] * b; out[3] = a[3] * b; return out; } /** * Calculates the length of a vec4 * * @param {ReadonlyVec4} a vector to calculate length of * @returns {Number} length of a */ function length$1(a) { var x = a[0]; var y = a[1]; var z = a[2]; var w = a[3]; return Math.hypot(x, y, z, w); } /** * Calculates the squared length of a vec4 * * @param {ReadonlyVec4} a vector to calculate squared length of * @returns {Number} squared length of a */ function squaredLength$1(a) { var x = a[0]; var y = a[1]; var z = a[2]; var w = a[3]; return x * x + y * y + z * z + w * w; } /** * Normalize a vec4 * * @param {vec4} out the receiving vector * @param {ReadonlyVec4} a vector to normalize * @returns {vec4} out */ function normalize$1(out, a) { var x = a[0]; var y = a[1]; var z = a[2]; var w = a[3]; var len = x * x + y * y + z * z + w * w; if (len > 0) { len = 1 / Math.sqrt(len); } out[0] = x * len; out[1] = y * len; out[2] = z * len; out[3] = w * len; return out; } /** * Calculates the dot product of two vec4's * * @param {ReadonlyVec4} a the first operand * @param {ReadonlyVec4} b the second operand * @returns {Number} dot product of a and b */ function dot$1(a, b) { return a[0] * b[0] + a[1] * b[1] + a[2] * b[2] + a[3] * b[3]; } /** * Performs a linear interpolation between two vec4's * * @param {vec4} out the receiving vector * @param {ReadonlyVec4} a the first operand * @param {ReadonlyVec4} b the second operand * @param {Number} t interpolation amount, in the range [0-1], between the two inputs * @returns {vec4} out */ function lerp$1(out, a, b, t) { var ax = a[0]; var ay = a[1]; var az = a[2]; var aw = a[3]; out[0] = ax + t * (b[0] - ax); out[1] = ay + t * (b[1] - ay); out[2] = az + t * (b[2] - az); out[3] = aw + t * (b[3] - aw); return out; } /** * Transforms the vec4 with a mat4. * * @param {vec4} out the receiving vector * @param {ReadonlyVec4} a the vector to transform * @param {ReadonlyMat4} m matrix to transform with * @returns {vec4} out */ function transformMat4(out, a, m) { var x = a[0], y = a[1], z = a[2], w = a[3]; out[0] = m[0] * x + m[4] * y + m[8] * z + m[12] * w; out[1] = m[1] * x + m[5] * y + m[9] * z + m[13] * w; out[2] = m[2] * x + m[6] * y + m[10] * z + m[14] * w; out[3] = m[3] * x + m[7] * y + m[11] * z + m[15] * w; return out; } /** * Transforms the vec4 with a quat * * @param {vec4} out the receiving vector * @param {ReadonlyVec4} a the vector to transform * @param {ReadonlyQuat} q quaternion to transform with * @returns {vec4} out */ function transformQuat(out, a, q) { var x = a[0], y = a[1], z = a[2]; var qx = q[0], qy = q[1], qz = q[2], qw = q[3]; // calculate quat * vec var ix = qw * x + qy * z - qz * y; var iy = qw * y + qz * x - qx * z; var iz = qw * z + qx * y - qy * x; var iw = -qx * x - qy * y - qz * z; // calculate result * inverse quat out[0] = ix * qw + iw * -qx + iy * -qz - iz * -qy; out[1] = iy * qw + iw * -qy + iz * -qx - ix * -qz; out[2] = iz * qw + iw * -qz + ix * -qy - iy * -qx; out[3] = a[3]; return out; } /** * Perform some operation over an array of vec4s. * * @param {Array} a the array of vectors to iterate over * @param {Number} stride Number of elements between the start of each vec4. If 0 assumes tightly packed * @param {Number} offset Number of elements to skip at the beginning of the array * @param {Number} count Number of vec4s to iterate over. If 0 iterates over entire array * @param {Function} fn Function to call for each vector in the array * @param {Object} [arg] additional argument to pass to fn * @returns {Array} a * @function */ (function () { var vec = create$1(); return function (a, stride, offset, count, fn, arg) { var i, l; if (!stride) { stride = 4; } if (!offset) { offset = 0; } if (count) { l = Math.min(count * stride + offset, a.length); } else { l = a.length; } for (i = offset; i < l; i += stride) { vec[0] = a[i]; vec[1] = a[i + 1]; vec[2] = a[i + 2]; vec[3] = a[i + 3]; fn(vec, vec, arg); a[i] = vec[0]; a[i + 1] = vec[1]; a[i + 2] = vec[2]; a[i + 3] = vec[3]; } return a; }; })(); const IDENTITY = Object.freeze([1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]); const ZERO = Object.freeze([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]); const INDICES = Object.freeze({ COL0ROW0: 0, COL0ROW1: 1, COL0ROW2: 2, COL0ROW3: 3, COL1ROW0: 4, COL1ROW1: 5, COL1ROW2: 6, COL1ROW3: 7, COL2ROW0: 8, COL2ROW1: 9, COL2ROW2: 10, COL2ROW3: 11, COL3ROW0: 12, COL3ROW1: 13, COL3ROW2: 14, COL3ROW3: 15 }); const constants = {}; class Matrix4$1 extends Matrix { static get IDENTITY() { constants.IDENTITY = constants.IDENTITY || Object.freeze(new Matrix4$1(IDENTITY)); return constants.IDENTITY; } static get ZERO() { constants.ZERO = constants.ZERO || Object.freeze(new Matrix4$1(ZERO)); return constants.ZERO; } get INDICES() { return INDICES; } get ELEMENTS() { return 16; } get RANK() { return 4; } constructor(array) { super(-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0); if (arguments.length === 1 && Array.isArray(array)) { this.copy(array); } else { this.identity(); } } copy(array) { this[0] = array[0]; this[1] = array[1]; this[2] = array[2]; this[3] = array[3]; this[4] = array[4]; this[5] = array[5]; this[6] = array[6]; this[7] = array[7]; this[8] = array[8]; this[9] = array[9]; this[10] = array[10]; this[11] = array[11]; this[12] = array[12]; this[13] = array[13]; this[14] = array[14]; this[15] = array[15]; return this.check(); } set(m00, m10, m20, m30, m01, m11, m21, m31, m02, m12, m22, m32, m03, m13, m23, m33) { this[0] = m00; this[1] = m10; this[2] = m20; this[3] = m30; this[4] = m01; this[5] = m11; this[6] = m21; this[7] = m31; this[8] = m02; this[9] = m12; this[10] = m22; this[11] = m32; this[12] = m03; this[13] = m13; this[14] = m23; this[15] = m33; return this.check(); } setRowMajor(m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33) { this[0] = m00; this[1] = m10; this[2] = m20; this[3] = m30; this[4] = m01; this[5] = m11; this[6] = m21; this[7] = m31; this[8] = m02; this[9] = m12; this[10] = m22; this[11] = m32; this[12] = m03; this[13] = m13; this[14] = m23; this[15] = m33; return this.check(); } toRowMajor(result) { result[0] = this[0]; result[1] = this[4]; result[2] = this[8]; result[3] = this[12]; result[4] = this[1]; result[5] = this[5]; result[6] = this[9]; result[7] = this[13]; result[8] = this[2]; result[9] = this[6]; result[10] = this[10]; result[11] = this[14]; result[12] = this[3]; result[13] = this[7]; result[14] = this[11]; result[15] = this[15]; return result; } identity() { return this.copy(IDENTITY); } fromQuaternion(q) { fromQuat(this, q); return this.check(); } frustum({ left, right, bottom, top, near, far }) { if (far === Infinity) { Matrix4$1._computeInfinitePerspectiveOffCenter(this, left, right, bottom, top, near); } else { frustum(this, left, right, bottom, top, near, far); } return this.check(); } static _computeInfinitePerspectiveOffCenter(result, left, right, bottom, top, near) { const column0Row0 = 2.0 * near / (right - left); const column1Row1 = 2.0 * near / (top - bottom); const column2Row0 = (right + left) / (right - left); const column2Row1 = (top + bottom) / (top - bottom); const column2Row2 = -1.0; const column2Row3 = -1.0; const column3Row2 = -2.0 * near; result[0] = column0Row0; result[1] = 0.0; result[2] = 0.0; result[3] = 0.0; result[4] = 0.0; result[5] = column1Row1; result[6] = 0.0; result[7] = 0.0; result[8] = column2Row0; result[9] = column2Row1; result[10] = column2Row2; result[11] = column2Row3; result[12] = 0.0; result[13] = 0.0; result[14] = column3Row2; result[15] = 0.0; return result; } lookAt(eye, center, up) { if (arguments.length === 1) { ({ eye, center, up } = eye); } center = center || [0, 0, 0]; up = up || [0, 1, 0]; lookAt(this, eye, center, up); return this.check(); } ortho({ left, right, bottom, top, near = 0.1, far = 500 }) { ortho(this, left, right, bottom, top, near, far); return this.check(); } orthographic({ fovy = 45 * Math.PI / 180, aspect = 1, focalDistance = 1, near = 0.1, far = 500 }) { if (fovy > Math.PI * 2) { throw Error('radians'); } const halfY = fovy / 2; const top = focalDistance * Math.tan(halfY); const right = top * aspect; return new Matrix4$1().ortho({ left: -right, right, bottom: -top, top, near, far }); } perspective({ fovy = undefined, fov = 45 * Math.PI / 180, aspect = 1, near = 0.1, far = 500 } = {}) { fovy = fovy || fov; if (fovy > Math.PI * 2) { throw Error('radians'); } perspective(this, fovy, aspect, near, far); return this.check(); } determinant() { return determinant(this); } getScale(result = [-0, -0, -0]) { result[0] = Math.sqrt(this[0] * this[0] + this[1] * this[1] + this[2] * this[2]); result[1] = Math.sqrt(this[4] * this[4] + this[5] * this[5] + this[6] * this[6]); result[2] = Math.sqrt(this[8] * this[8] + this[9] * this[9] + this[10] * this[10]); return result; } getTranslation(result = [-0, -0, -0]) { result[0] = this[12]; result[1] = this[13]; result[2] = this[14]; return result; } getRotation(result = [-0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0, -0], scaleResult = null) { const scale = this.getScale(scaleResult || [-0, -0, -0]); const inverseScale0 = 1 / scale[0]; const inverseScale1 = 1 / scale[1]; const inverseScale2 = 1 / scale[2]; result[0] = this[0] * inverseScale0; result[1] = this[1] * inverseScale1; result[2] = this[2] * inverseScale2; result[3] = 0; result[4] = this[4] * inverseScale0; result[5] = this[5] * inverseScale1; result[6] = this[6] * inverseScale2; result[7] = 0; result[8] = this[8] * inverseScale0; result[9] = this[9] * inverseScale1; result[10] = this[10] * inverseScale2; result[11] = 0; result[12] = 0; result[13] = 0; result[14] = 0; result[15] = 1; return result; } getRotationMatrix3(result = [-0, -0, -0, -0, -0, -0, -0, -0, -0], scaleResult = null) { const scale = this.getScale(scaleResult || [-0, -0, -0]); const inverseScale0 = 1 / scale[0]; const inverseScale1 = 1 / scale[1]; const inverseScale2 = 1 / scale[2]; result[0] = this[0] * inverseScale0; result[1] = this[1] * inverseScale1; result[2] = this[2] * inverseScale2; result[3] = this[4] * inverseScale0; result[4] = this[5] * inverseScale1; result[5] = this[6] * inverseScale2; result[6] = this[8] * inverseScale0; result[7] = this[9] * inverseScale1; result[8] = this[10] * inverseScale2; return result; } transpose() { transpose(this, this); return this.check(); } invert() { invert$1(this, this); return this.check(); } multiplyLeft(a) { multiply$1(this, a, this); return this.check(); } multiplyRight(a) { multiply$1(this, this, a); return this.check(); } rotateX(radians) { rotateX$1(this, this, radians); return this.check(); } rotateY(radians) { rotateY$1(this, this, radians); return this.check(); } rotateZ(radians) { rotateZ$1(this, this, radians); return this.check(); } rotateXYZ([rx, ry, rz]) { return this.rotateX(rx).rotateY(ry).rotateZ(rz); } rotateAxis(radians, axis) { rotate(this, this, radians, axis); return this.check(); } scale(factor) { if (Array.isArray(factor)) { scale$2(this, this, factor); } else { scale$2(this, this, [factor, factor, factor]); } return this.check(); } translate(vec) { translate(this, this, vec); return this.check(); } transform(vector, result) { if (vector.length === 4) { result = transformMat4(result || [-0, -0, -0, -0], vector, this); checkVector(result, 4); return result; } return this.transformAsPoint(vector, result); } transformAsPoint(vector, result) { const { length } = vector; switch (length) { case 2: result = transformMat4$2(result || [-0, -0], vector, this); break; case 3: result = transformMat4$1(result || [-0, -0, -0], vector, this); break; default: throw new Error('Illegal vector'); } checkVector(result, vector.length); return result; } transformAsVector(vector, result) { switch (vector.length) { case 2: result = vec2_transformMat4AsVector(result || [-0, -0], vector, this); break; case 3: result = vec3_transformMat4AsVector(result || [-0, -0, -0], vector, this); break; default: throw new Error('Illegal vector'); } checkVector(result, vector.length); return result; } makeRotationX(radians) { return this.identity().rotateX(radians); } makeTranslation(x, y, z) { return this.identity().translate([x, y, z]); } transformPoint(vector, result) { deprecated('Matrix4.transformPoint', '3.0'); return this.transformAsPoint(vector, result); } transformVector(vector, result) { deprecated('Matrix4.transformVector', '3.0'); return this.transformAsPoint(vector, result); } transformDirection(vector, result) { deprecated('Matrix4.transformDirection', '3.0'); return this.transformAsVector(vector, result); } } /** * Quaternion * @module quat */ /** * Creates a new identity quat * * @returns {quat} a new quaternion */ function create() { var out = new ARRAY_TYPE(4); if (ARRAY_TYPE != Float32Array) { out[0] = 0; out[1] = 0; out[2] = 0; } out[3] = 1; return out; } /** * Set a quat to the identity quaternion * * @param {quat} out the receiving quaternion * @returns {quat} out */ function identity(out) { out[0] = 0; out[1] = 0; out[2] = 0; out[3] = 1; return out; } /** * Sets a quat from the given angle and rotation axis, * then returns it. * * @param {quat} out the receiving quaternion * @param {ReadonlyVec3} axis the axis around which to rotate * @param {Number} rad the angle in radians * @returns {quat} out **/ function setAxisAngle(out, axis, rad) { rad = rad * 0.5; var s = Math.sin(rad); out[0] = s * axis[0]; out[1] = s * axis[1]; out[2] = s * axis[2]; out[3] = Math.cos(rad); return out; } /** * Multiplies two quat's * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a the first operand * @param {ReadonlyQuat} b the second operand * @returns {quat} out */ function multiply(out, a, b) { var ax = a[0], ay = a[1], az = a[2], aw = a[3]; var bx = b[0], by = b[1], bz = b[2], bw = b[3]; out[0] = ax * bw + aw * bx + ay * bz - az * by; out[1] = ay * bw + aw * by + az * bx - ax * bz; out[2] = az * bw + aw * bz + ax * by - ay * bx; out[3] = aw * bw - ax * bx - ay * by - az * bz; return out; } /** * Rotates a quaternion by the given angle about the X axis * * @param {quat} out quat receiving operation result * @param {ReadonlyQuat} a quat to rotate * @param {number} rad angle (in radians) to rotate * @returns {quat} out */ function rotateX(out, a, rad) { rad *= 0.5; var ax = a[0], ay = a[1], az = a[2], aw = a[3]; var bx = Math.sin(rad), bw = Math.cos(rad); out[0] = ax * bw + aw * bx; out[1] = ay * bw + az * bx; out[2] = az * bw - ay * bx; out[3] = aw * bw - ax * bx; return out; } /** * Rotates a quaternion by the given angle about the Y axis * * @param {quat} out quat receiving operation result * @param {ReadonlyQuat} a quat to rotate * @param {number} rad angle (in radians) to rotate * @returns {quat} out */ function rotateY(out, a, rad) { rad *= 0.5; var ax = a[0], ay = a[1], az = a[2], aw = a[3]; var by = Math.sin(rad), bw = Math.cos(rad); out[0] = ax * bw - az * by; out[1] = ay * bw + aw * by; out[2] = az * bw + ax * by; out[3] = aw * bw - ay * by; return out; } /** * Rotates a quaternion by the given angle about the Z axis * * @param {quat} out quat receiving operation result * @param {ReadonlyQuat} a quat to rotate * @param {number} rad angle (in radians) to rotate * @returns {quat} out */ function rotateZ(out, a, rad) { rad *= 0.5; var ax = a[0], ay = a[1], az = a[2], aw = a[3]; var bz = Math.sin(rad), bw = Math.cos(rad); out[0] = ax * bw + ay * bz; out[1] = ay * bw - ax * bz; out[2] = az * bw + aw * bz; out[3] = aw * bw - az * bz; return out; } /** * Calculates the W component of a quat from the X, Y, and Z components. * Assumes that quaternion is 1 unit in length. * Any existing W component will be ignored. * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a quat to calculate W component of * @returns {quat} out */ function calculateW(out, a) { var x = a[0], y = a[1], z = a[2]; out[0] = x; out[1] = y; out[2] = z; out[3] = Math.sqrt(Math.abs(1.0 - x * x - y * y - z * z)); return out; } /** * Performs a spherical linear interpolation between two quat * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a the first operand * @param {ReadonlyQuat} b the second operand * @param {Number} t interpolation amount, in the range [0-1], between the two inputs * @returns {quat} out */ function slerp(out, a, b, t) { // benchmarks: // http://jsperf.com/quaternion-slerp-implementations var ax = a[0], ay = a[1], az = a[2], aw = a[3]; var bx = b[0], by = b[1], bz = b[2], bw = b[3]; var omega, cosom, sinom, scale0, scale1; // calc cosine cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary) if (cosom < 0.0) { cosom = -cosom; bx = -bx; by = -by; bz = -bz; bw = -bw; } // calculate coefficients if (1.0 - cosom > EPSILON) { // standard case (slerp) omega = Math.acos(cosom); sinom = Math.sin(omega); scale0 = Math.sin((1.0 - t) * omega) / sinom; scale1 = Math.sin(t * omega) / sinom; } else { // "from" and "to" quaternions are very close // ... so we can do a linear interpolation scale0 = 1.0 - t; scale1 = t; } // calculate final values out[0] = scale0 * ax + scale1 * bx; out[1] = scale0 * ay + scale1 * by; out[2] = scale0 * az + scale1 * bz; out[3] = scale0 * aw + scale1 * bw; return out; } /** * Calculates the inverse of a quat * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a quat to calculate inverse of * @returns {quat} out */ function invert(out, a) { var a0 = a[0], a1 = a[1], a2 = a[2], a3 = a[3]; var dot = a0 * a0 + a1 * a1 + a2 * a2 + a3 * a3; var invDot = dot ? 1.0 / dot : 0; // TODO: Would be faster to return [0,0,0,0] immediately if dot == 0 out[0] = -a0 * invDot; out[1] = -a1 * invDot; out[2] = -a2 * invDot; out[3] = a3 * invDot; return out; } /** * Calculates the conjugate of a quat * If the quaternion is normalized, this function is faster than quat.inverse and produces the same result. * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a quat to calculate conjugate of * @returns {quat} out */ function conjugate(out, a) { out[0] = -a[0]; out[1] = -a[1]; out[2] = -a[2]; out[3] = a[3]; return out; } /** * Creates a quaternion from the given 3x3 rotation matrix. * * NOTE: The resultant quaternion is not normalized, so you should be sure * to renormalize the quaternion yourself where necessary. * * @param {quat} out the receiving quaternion * @param {ReadonlyMat3} m rotation matrix * @returns {quat} out * @function */ function fromMat3(out, m) { // Algorithm in Ken Shoemake's article in 1987 SIGGRAPH course notes // article "Quaternion Calculus and Fast Animation". var fTrace = m[0] + m[4] + m[8]; var fRoot; if (fTrace > 0.0) { // |w| > 1/2, may as well choose w > 1/2 fRoot = Math.sqrt(fTrace + 1.0); // 2w out[3] = 0.5 * fRoot; fRoot = 0.5 / fRoot; // 1/(4w) out[0] = (m[5] - m[7]) * fRoot; out[1] = (m[6] - m[2]) * fRoot; out[2] = (m[1] - m[3]) * fRoot; } else { // |w| <= 1/2 var i = 0; if (m[4] > m[0]) i = 1; if (m[8] > m[i * 3 + i]) i = 2; var j = (i + 1) % 3; var k = (i + 2) % 3; fRoot = Math.sqrt(m[i * 3 + i] - m[j * 3 + j] - m[k * 3 + k] + 1.0); out[i] = 0.5 * fRoot; fRoot = 0.5 / fRoot; out[3] = (m[j * 3 + k] - m[k * 3 + j]) * fRoot; out[j] = (m[j * 3 + i] + m[i * 3 + j]) * fRoot; out[k] = (m[k * 3 + i] + m[i * 3 + k]) * fRoot; } return out; } /** * Adds two quat's * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a the first operand * @param {ReadonlyQuat} b the second operand * @returns {quat} out * @function */ var add = add$1; /** * Scales a quat by a scalar number * * @param {quat} out the receiving vector * @param {ReadonlyQuat} a the vector to scale * @param {Number} b amount to scale the vector by * @returns {quat} out * @function */ var scale = scale$1; /** * Calculates the dot product of two quat's * * @param {ReadonlyQuat} a the first operand * @param {ReadonlyQuat} b the second operand * @returns {Number} dot product of a and b * @function */ var dot = dot$1; /** * Performs a linear interpolation between two quat's * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a the first operand * @param {ReadonlyQuat} b the second operand * @param {Number} t interpolation amount, in the range [0-1], between the two inputs * @returns {quat} out * @function */ var lerp$2 = lerp$1; /** * Calculates the length of a quat * * @param {ReadonlyQuat} a vector to calculate length of * @returns {Number} length of a */ var length = length$1; /** * Calculates the squared length of a quat * * @param {ReadonlyQuat} a vector to calculate squared length of * @returns {Number} squared length of a * @function */ var squaredLength = squaredLength$1; /** * Normalize a quat * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a quaternion to normalize * @returns {quat} out * @function */ var normalize = normalize$1; /** * Sets a quaternion to represent the shortest rotation from one * vector to another. * * Both vectors are assumed to be unit length. * * @param {quat} out the receiving quaternion. * @param {ReadonlyVec3} a the initial vector * @param {ReadonlyVec3} b the destination vector * @returns {quat} out */ var rotationTo = function () { var tmpvec3 = create$3(); var xUnitVec3 = fromValues(1, 0, 0); var yUnitVec3 = fromValues(0, 1, 0); return function (out, a, b) { var dot = dot$2(a, b); if (dot < -0.999999) { cross(tmpvec3, xUnitVec3, a); if (len(tmpvec3) < 0.000001) cross(tmpvec3, yUnitVec3, a); normalize$2(tmpvec3, tmpvec3); setAxisAngle(out, tmpvec3, Math.PI); return out; } else if (dot > 0.999999) { out[0] = 0; out[1] = 0; out[2] = 0; out[3] = 1; return out; } else { cross(tmpvec3, a, b); out[0] = tmpvec3[0]; out[1] = tmpvec3[1]; out[2] = tmpvec3[2]; out[3] = 1 + dot; return normalize(out, out); } }; }(); /** * Performs a spherical linear interpolation with two control points * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a the first operand * @param {ReadonlyQuat} b the second operand * @param {ReadonlyQuat} c the third operand * @param {ReadonlyQuat} d the fourth operand * @param {Number} t interpolation amount, in the range [0-1], between the two inputs * @returns {quat} out */ (function () { var temp1 = create(); var temp2 = create(); return function (out, a, b, c, d, t) { slerp(temp1, a, d, t); slerp(temp2, b, c, t); slerp(out, temp1, temp2, 2 * t * (1 - t)); return out; }; })(); /** * Sets the specified quaternion with values corresponding to the given * axes. Each axis is a vec3 and is expected to be unit length and * perpendicular to all other specified axes. * * @param {ReadonlyVec3} view the vector representing the viewing direction * @param {ReadonlyVec3} right the vector representing the local "right" direction * @param {ReadonlyVec3} up the vector representing the local "up" direction * @returns {quat} out */ (function () { var matr = create$2(); return function (out, view, right, up) { matr[0] = right[0]; matr[3] = right[1]; matr[6] = right[2]; matr[1] = up[0]; matr[4] = up[1]; matr[7] = up[2]; matr[2] = -view[0]; matr[5] = -view[1]; matr[8] = -view[2]; return normalize(out, fromMat3(out, matr)); }; })(); const IDENTITY_QUATERNION = [0, 0, 0, 1]; class Quaternion$1 extends MathArray { constructor(x = 0, y = 0, z = 0, w = 1) { super(-0, -0, -0, -0); if (Array.isArray(x) && arguments.length === 1) { this.copy(x); } else { this.set(x, y, z, w); } } copy(array) { this[0] = array[0]; this[1] = array[1]; this[2] = array[2]; this[3] = array[3]; return this.check(); } set(x, y, z, w) { this[0] = x; this[1] = y; this[2] = z; this[3] = w; return this.check(); } fromMatrix3(m) { fromMat3(this, m); return this.check(); } identity() { identity(this); return this.check(); } fromAxisRotation(axis, rad) { setAxisAngle(this, axis, rad); return this.check(); } setAxisAngle(axis, rad) { return this.fromAxisRotation(axis, rad); } get ELEMENTS() { return 4; } get x() { return this[0]; } set x(value) { this[0] = checkNumber(value); } get y() { return this[1]; } set y(value) { this[1] = checkNumber(value); } get z() { return this[2]; } set z(value) { this[2] = checkNumber(value); } get w() { return this[3]; } set w(value) { this[3] = checkNumber(value); } len() { return length(this); } lengthSquared() { return squaredLength(this); } dot(a, b) { if (b !== undefined) { throw new Error('Quaternion.dot only takes one argument'); } return dot(this, a); } rotationTo(vectorA, vectorB) { rotationTo(this, vectorA, vectorB); return this.check(); } add(a, b) { if (b !== undefined) { throw new Error('Quaternion.add only takes one argument'); } add(this, this, a); return this.check(); } calculateW() { calculateW(this, this); return this.check(); } conjugate() { conjugate(this, this); return this.check(); } invert() { invert(this, this); return this.check(); } lerp(a, b, t) { lerp$2(this, a, b, t); return this.check(); } multiplyRight(a, b) { assert$4(!b); multiply(this, this, a); return this.check(); } multiplyLeft(a, b) { assert$4(!b); multiply(this, a, this); return this.check(); } normalize() { const length = this.len(); const l = length > 0 ? 1 / length : 0; this[0] = this[0] * l; this[1] = this[1] * l; this[2] = this[2] * l; this[3] = this[3] * l; if (length === 0) { this[3] = 1; } return this.check(); } rotateX(rad) { rotateX(this, this, rad); return this.check(); } rotateY(rad) { rotateY(this, this, rad); return this.check(); } rotateZ(rad) { rotateZ(this, this, rad); return this.check(); } scale(b) { scale(this, this, b); return this.check(); } slerp(start, target, ratio) { switch (arguments.length) { case 1: ({ start = IDENTITY_QUATERNION, target, ratio } = arguments[0]); break; case 2: [target, ratio] = arguments; start = this; break; } slerp(this, start, target, ratio); return this.check(); } transformVector4(vector, result = vector) { transformQuat(result, vector, this); return checkVector(result, 4); } lengthSq() { return this.lengthSquared(); } setFromAxisAngle(axis, rad) { return this.setAxisAngle(axis, rad); } premultiply(a, b) { return this.multiplyLeft(a, b); } multiply(a, b) { return this.multiplyRight(a, b); } } var _MathUtils = { EPSILON1: 1e-1, EPSILON2: 1e-2, EPSILON3: 1e-3, EPSILON4: 1e-4, EPSILON5: 1e-5, EPSILON6: 1e-6, EPSILON7: 1e-7, EPSILON8: 1e-8, EPSILON9: 1e-9, EPSILON10: 1e-10, EPSILON11: 1e-11, EPSILON12: 1e-12, EPSILON13: 1e-13, EPSILON14: 1e-14, EPSILON15: 1e-15, EPSILON16: 1e-16, EPSILON17: 1e-17, EPSILON18: 1e-18, EPSILON19: 1e-19, EPSILON20: 1e-20, PI_OVER_TWO: Math.PI / 2, PI_OVER_FOUR: Math.PI / 4, PI_OVER_SIX: Math.PI / 6, TWO_PI: Math.PI * 2 }; const WGS84_RADIUS_X$1 = 6378137.0; const WGS84_RADIUS_Y$1 = 6378137.0; const WGS84_RADIUS_Z$1 = 6356752.3142451793; const noop$2 = x => x; const scratchVector$6 = new Vector3$1(); function fromCartographic(cartographic, result, map = noop$2) { if (isArray(cartographic)) { result[0] = map(cartographic[0]); result[1] = map(cartographic[1]); result[2] = cartographic[2]; } else if ('longitude' in cartographic) { result[0] = map(cartographic.longitude); result[1] = map(cartographic.latitude); result[2] = cartographic.height; } else { result[0] = map(cartographic.x); result[1] = map(cartographic.y); result[2] = cartographic.z; } return result; } function fromCartographicToRadians(cartographic, vector = scratchVector$6) { return fromCartographic(cartographic, vector, config$2._cartographicRadians ? noop$2 : toRadians); } function toCartographic(vector, cartographic, map = noop$2) { if (isArray(cartographic)) { cartographic[0] = map(vector[0]); cartographic[1] = map(vector[1]); cartographic[2] = vector[2]; } else if ('longitude' in cartographic) { cartographic.longitude = map(vector[0]); cartographic.latitude = map(vector[1]); cartographic.height = vector[2]; } else { cartographic.x = map(vector[0]); cartographic.y = map(vector[1]); cartographic.z = vector[2]; } return cartographic; } function toCartographicFromRadians(vector, cartographic) { return toCartographic(vector, cartographic, config$2._cartographicRadians ? noop$2 : toDegrees); } const scratchVector$5 = new Vector3$1(); const scaleToGeodeticSurfaceIntersection = new Vector3$1(); const scaleToGeodeticSurfaceGradient = new Vector3$1(); function scaleToGeodeticSurface(cartesian, ellipsoid, result = new Vector3$1()) { const { oneOverRadii, oneOverRadiiSquared, centerToleranceSquared } = ellipsoid; scratchVector$5.from(cartesian); const positionX = cartesian.x; const positionY = cartesian.y; const positionZ = cartesian.z; const oneOverRadiiX = oneOverRadii.x; const oneOverRadiiY = oneOverRadii.y; const oneOverRadiiZ = oneOverRadii.z; const x2 = positionX * positionX * oneOverRadiiX * oneOverRadiiX; const y2 = positionY * positionY * oneOverRadiiY * oneOverRadiiY; const z2 = positionZ * positionZ * oneOverRadiiZ * oneOverRadiiZ; const squaredNorm = x2 + y2 + z2; const ratio = Math.sqrt(1.0 / squaredNorm); if (!Number.isFinite(ratio)) { return undefined; } const intersection = scaleToGeodeticSurfaceIntersection; intersection.copy(cartesian).scale(ratio); if (squaredNorm < centerToleranceSquared) { return intersection.to(result); } const oneOverRadiiSquaredX = oneOverRadiiSquared.x; const oneOverRadiiSquaredY = oneOverRadiiSquared.y; const oneOverRadiiSquaredZ = oneOverRadiiSquared.z; const gradient = scaleToGeodeticSurfaceGradient; gradient.set(intersection.x * oneOverRadiiSquaredX * 2.0, intersection.y * oneOverRadiiSquaredY * 2.0, intersection.z * oneOverRadiiSquaredZ * 2.0); let lambda = (1.0 - ratio) * cartesian.len() / (0.5 * gradient.len()); let correction = 0.0; let xMultiplier; let yMultiplier; let zMultiplier; let func; do { lambda -= correction; xMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredX); yMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredY); zMultiplier = 1.0 / (1.0 + lambda * oneOverRadiiSquaredZ); const xMultiplier2 = xMultiplier * xMultiplier; const yMultiplier2 = yMultiplier * yMultiplier; const zMultiplier2 = zMultiplier * zMultiplier; const xMultiplier3 = xMultiplier2 * xMultiplier; const yMultiplier3 = yMultiplier2 * yMultiplier; const zMultiplier3 = zMultiplier2 * zMultiplier; func = x2 * xMultiplier2 + y2 * yMultiplier2 + z2 * zMultiplier2 - 1.0; const denominator = x2 * xMultiplier3 * oneOverRadiiSquaredX + y2 * yMultiplier3 * oneOverRadiiSquaredY + z2 * zMultiplier3 * oneOverRadiiSquaredZ; const derivative = -2.0 * denominator; correction = func / derivative; } while (Math.abs(func) > _MathUtils.EPSILON12); return scratchVector$5.scale([xMultiplier, yMultiplier, zMultiplier]).to(result); } const EPSILON14 = 1e-14; const scratchOrigin = new Vector3$1(); const VECTOR_PRODUCT_LOCAL_FRAME = { up: { south: 'east', north: 'west', west: 'south', east: 'north' }, down: { south: 'west', north: 'east', west: 'north', east: 'south' }, south: { up: 'west', down: 'east', west: 'down', east: 'up' }, north: { up: 'east', down: 'west', west: 'up', east: 'down' }, west: { up: 'north', down: 'south', north: 'down', south: 'up' }, east: { up: 'south', down: 'north', north: 'up', south: 'down' } }; const degeneratePositionLocalFrame = { north: [-1, 0, 0], east: [0, 1, 0], up: [0, 0, 1], south: [1, 0, 0], west: [0, -1, 0], down: [0, 0, -1] }; const scratchAxisVectors = { east: new Vector3$1(), north: new Vector3$1(), up: new Vector3$1(), west: new Vector3$1(), south: new Vector3$1(), down: new Vector3$1() }; const scratchVector1 = new Vector3$1(); const scratchVector2$1 = new Vector3$1(); const scratchVector3$1 = new Vector3$1(); function localFrameToFixedFrame(ellipsoid, firstAxis, secondAxis, thirdAxis, cartesianOrigin, result) { const thirdAxisInferred = VECTOR_PRODUCT_LOCAL_FRAME[firstAxis] && VECTOR_PRODUCT_LOCAL_FRAME[firstAxis][secondAxis]; assert$4(thirdAxisInferred && (!thirdAxis || thirdAxis === thirdAxisInferred)); let firstAxisVector; let secondAxisVector; let thirdAxisVector; const origin = scratchOrigin.copy(cartesianOrigin); const atPole = equals$1(origin.x, 0.0, EPSILON14) && equals$1(origin.y, 0.0, EPSILON14); if (atPole) { const sign = Math.sign(origin.z); firstAxisVector = scratchVector1.fromArray(degeneratePositionLocalFrame[firstAxis]); if (firstAxis !== 'east' && firstAxis !== 'west') { firstAxisVector.scale(sign); } secondAxisVector = scratchVector2$1.fromArray(degeneratePositionLocalFrame[secondAxis]); if (secondAxis !== 'east' && secondAxis !== 'west') { secondAxisVector.scale(sign); } thirdAxisVector = scratchVector3$1.fromArray(degeneratePositionLocalFrame[thirdAxis]); if (thirdAxis !== 'east' && thirdAxis !== 'west') { thirdAxisVector.scale(sign); } } else { const { up, east, north } = scratchAxisVectors; east.set(-origin.y, origin.x, 0.0).normalize(); ellipsoid.geodeticSurfaceNormal(origin, up); north.copy(up).cross(east); const { down, west, south } = scratchAxisVectors; down.copy(up).scale(-1); west.copy(east).scale(-1); south.copy(north).scale(-1); firstAxisVector = scratchAxisVectors[firstAxis]; secondAxisVector = scratchAxisVectors[secondAxis]; thirdAxisVector = scratchAxisVectors[thirdAxis]; } result[0] = firstAxisVector.x; result[1] = firstAxisVector.y; result[2] = firstAxisVector.z; result[3] = 0.0; result[4] = secondAxisVector.x; result[5] = secondAxisVector.y; result[6] = secondAxisVector.z; result[7] = 0.0; result[8] = thirdAxisVector.x; result[9] = thirdAxisVector.y; result[10] = thirdAxisVector.z; result[11] = 0.0; result[12] = origin.x; result[13] = origin.y; result[14] = origin.z; result[15] = 1.0; return result; } const scratchVector$4 = new Vector3$1(); const scratchNormal$2 = new Vector3$1(); const scratchK = new Vector3$1(); const scratchPosition$2 = new Vector3$1(); const scratchHeight = new Vector3$1(); const scratchCartesian = new Vector3$1(); let wgs84; class Ellipsoid { static get WGS84() { wgs84 = wgs84 || new Ellipsoid(WGS84_RADIUS_X$1, WGS84_RADIUS_Y$1, WGS84_RADIUS_Z$1); return wgs84; } constructor(x = 0.0, y = 0.0, z = 0.0) { assert$4(x >= 0.0); assert$4(y >= 0.0); assert$4(z >= 0.0); this.radii = new Vector3$1(x, y, z); this.radiiSquared = new Vector3$1(x * x, y * y, z * z); this.radiiToTheFourth = new Vector3$1(x * x * x * x, y * y * y * y, z * z * z * z); this.oneOverRadii = new Vector3$1(x === 0.0 ? 0.0 : 1.0 / x, y === 0.0 ? 0.0 : 1.0 / y, z === 0.0 ? 0.0 : 1.0 / z); this.oneOverRadiiSquared = new Vector3$1(x === 0.0 ? 0.0 : 1.0 / (x * x), y === 0.0 ? 0.0 : 1.0 / (y * y), z === 0.0 ? 0.0 : 1.0 / (z * z)); this.minimumRadius = Math.min(x, y, z); this.maximumRadius = Math.max(x, y, z); this.centerToleranceSquared = _MathUtils.EPSILON1; if (this.radiiSquared.z !== 0) { this.squaredXOverSquaredZ = this.radiiSquared.x / this.radiiSquared.z; } Object.freeze(this); } equals(right) { return this === right || Boolean(right && this.radii.equals(right.radii)); } toString() { return this.radii.toString(); } cartographicToCartesian(cartographic, result = [0, 0, 0]) { const normal = scratchNormal$2; const k = scratchK; const [,, height] = cartographic; this.geodeticSurfaceNormalCartographic(cartographic, normal); k.copy(this.radiiSquared).scale(normal); const gamma = Math.sqrt(normal.dot(k)); k.scale(1 / gamma); normal.scale(height); k.add(normal); return k.to(result); } cartesianToCartographic(cartesian, result = [0, 0, 0]) { scratchCartesian.from(cartesian); const point = this.scaleToGeodeticSurface(scratchCartesian, scratchPosition$2); if (!point) { return undefined; } const normal = this.geodeticSurfaceNormal(point, scratchNormal$2); const h = scratchHeight; h.copy(scratchCartesian).subtract(point); const longitude = Math.atan2(normal.y, normal.x); const latitude = Math.asin(normal.z); const height = Math.sign(dot$2(h, scratchCartesian)) * length$2(h); return toCartographicFromRadians([longitude, latitude, height], result); } eastNorthUpToFixedFrame(origin, result = new Matrix4$1()) { return localFrameToFixedFrame(this, 'east', 'north', 'up', origin, result); } localFrameToFixedFrame(firstAxis, secondAxis, thirdAxis, origin, result = new Matrix4$1()) { return localFrameToFixedFrame(this, firstAxis, secondAxis, thirdAxis, origin, result); } geocentricSurfaceNormal(cartesian, result = [0, 0, 0]) { return scratchVector$4.from(cartesian).normalize().to(result); } geodeticSurfaceNormalCartographic(cartographic, result = [0, 0, 0]) { const cartographicVectorRadians = fromCartographicToRadians(cartographic); const longitude = cartographicVectorRadians[0]; const latitude = cartographicVectorRadians[1]; const cosLatitude = Math.cos(latitude); scratchVector$4.set(cosLatitude * Math.cos(longitude), cosLatitude * Math.sin(longitude), Math.sin(latitude)).normalize(); return scratchVector$4.to(result); } geodeticSurfaceNormal(cartesian, result = [0, 0, 0]) { return scratchVector$4.from(cartesian).scale(this.oneOverRadiiSquared).normalize().to(result); } scaleToGeodeticSurface(cartesian, result) { return scaleToGeodeticSurface(cartesian, this, result); } scaleToGeocentricSurface(cartesian, result = [0, 0, 0]) { scratchPosition$2.from(cartesian); const positionX = scratchPosition$2.x; const positionY = scratchPosition$2.y; const positionZ = scratchPosition$2.z; const oneOverRadiiSquared = this.oneOverRadiiSquared; const beta = 1.0 / Math.sqrt(positionX * positionX * oneOverRadiiSquared.x + positionY * positionY * oneOverRadiiSquared.y + positionZ * positionZ * oneOverRadiiSquared.z); return scratchPosition$2.multiplyScalar(beta).to(result); } transformPositionToScaledSpace(position, result = [0, 0, 0]) { return scratchPosition$2.from(position).scale(this.oneOverRadii).to(result); } transformPositionFromScaledSpace(position, result = [0, 0, 0]) { return scratchPosition$2.from(position).scale(this.radii).to(result); } getSurfaceNormalIntersectionWithZAxis(position, buffer = 0.0, result = [0, 0, 0]) { assert$4(equals$1(this.radii.x, this.radii.y, _MathUtils.EPSILON15)); assert$4(this.radii.z > 0); scratchPosition$2.from(position); const z = scratchPosition$2.z * (1 - this.squaredXOverSquaredZ); if (Math.abs(z) >= this.radii.z - buffer) { return undefined; } return scratchPosition$2.set(0.0, 0.0, z).to(result); } } class DoublyLinkedListNode { constructor(item, previous, next) { _defineProperty(this, "item", void 0); _defineProperty(this, "previous", void 0); _defineProperty(this, "next", void 0); this.item = item; this.previous = previous; this.next = next; } } class DoublyLinkedList { constructor() { _defineProperty(this, "head", null); _defineProperty(this, "tail", null); _defineProperty(this, "_length", 0); } get length() { return this._length; } add(item) { const node = new DoublyLinkedListNode(item, this.tail, null); if (this.tail) { this.tail.next = node; this.tail = node; } else { this.head = node; this.tail = node; } ++this._length; return node; } remove(node) { if (!node) { return; } if (node.previous && node.next) { node.previous.next = node.next; node.next.previous = node.previous; } else if (node.previous) { node.previous.next = null; this.tail = node.previous; } else if (node.next) { node.next.previous = null; this.head = node.next; } else { this.head = null; this.tail = null; } node.next = null; node.previous = null; --this._length; } splice(node, nextNode) { if (node === nextNode) { return; } this.remove(nextNode); this._insert(node, nextNode); } _insert(node, nextNode) { const oldNodeNext = node.next; node.next = nextNode; if (this.tail === node) { this.tail = nextNode; } else { oldNodeNext.previous = nextNode; } nextNode.next = oldNodeNext; nextNode.previous = node; ++this._length; } } function defined$5(x) { return x !== undefined && x !== null; } class TilesetCache { constructor() { _defineProperty(this, "_list", void 0); _defineProperty(this, "_sentinel", void 0); _defineProperty(this, "_trimTiles", void 0); this._list = new DoublyLinkedList(); this._sentinel = this._list.add('sentinel'); this._trimTiles = false; } reset() { this._list.splice(this._list.tail, this._sentinel); } touch(tile) { const node = tile._cacheNode; if (defined$5(node)) { this._list.splice(this._sentinel, node); } } add(tileset, tile, addCallback) { if (!defined$5(tile._cacheNode)) { tile._cacheNode = this._list.add(tile); if (addCallback) { addCallback(tileset, tile); } } } unloadTile(tileset, tile, unloadCallback) { const node = tile._cacheNode; if (!defined$5(node)) { return; } this._list.remove(node); tile._cacheNode = undefined; if (unloadCallback) { unloadCallback(tileset, tile); } } unloadTiles(tileset, unloadCallback) { const trimTiles = this._trimTiles; this._trimTiles = false; const list = this._list; const maximumMemoryUsageInBytes = (Potree.settings.tiles3DMaxMemory || tileset.maximumMemoryUsage) * 1024 * 1024; const sentinel = this._sentinel; let node = list.head; /* while (node !== sentinel && (tileset.gpuMemoryUsageInBytes > maximumMemoryUsageInBytes || trimTiles)) { const tile = node.item; node = node.next; this.unloadTile(tileset, tile, unloadCallback); } */ //改 while (node !== sentinel && ( getGpuMemoryUsage() > maximumMemoryUsageInBytes || trimTiles)) { const tile = node.item; node = node.next; this.unloadTile(tileset, tile, unloadCallback); if(viewer.visiVertexCount > 1500000){ //add: 针对部分特别精细的很卡如 https://4dkk.4dage.com/scene_view_data/SG-t-WReTBN204dQ/images/3dtiles/tileset.json tileset.options.maximumScreenSpaceError *= 1.1; tileset.options.maximumScreenSpaceError = Math.min(1000, tileset.options.maximumScreenSpaceError); } } } trim() { this._trimTiles = true; } } function calculateTransformProps(tileHeader, tile) { assert$7(tileHeader); assert$7(tile); const { rtcCenter, gltfUpAxis } = tile; const { computedTransform, boundingVolume: { center } } = tileHeader; let modelMatrix = new Matrix4$1(computedTransform); if (rtcCenter) { modelMatrix.translate(rtcCenter); } switch (gltfUpAxis) { case 'Z': break; case 'Y': const rotationY = new Matrix4$1().rotateX(Math.PI / 2); modelMatrix = modelMatrix.multiplyRight(rotationY); break; case 'X': const rotationX = new Matrix4$1().rotateY(-Math.PI / 2); modelMatrix = modelMatrix.multiplyRight(rotationX); break; } if (tile.isQuantized) { modelMatrix.translate(tile.quantizedVolumeOffset).scale(tile.quantizedVolumeScale); } const cartesianOrigin = new Vector3$1(center); tile.cartesianModelMatrix = modelMatrix; tile.cartesianOrigin = cartesianOrigin; const cartographicOrigin = Ellipsoid.WGS84.cartesianToCartographic(cartesianOrigin, new Vector3$1()); const fromFixedFrameMatrix = Ellipsoid.WGS84.eastNorthUpToFixedFrame(cartesianOrigin); const toFixedFrameMatrix = fromFixedFrameMatrix.invert(); tile.cartographicModelMatrix = toFixedFrameMatrix.multiplyRight(modelMatrix); tile.cartographicOrigin = cartographicOrigin; if (!tile.coordinateSystem) { tile.modelMatrix = tile.cartographicModelMatrix; } } const INTERSECTION = Object.freeze({ OUTSIDE: -1, INTERSECTING: 0, INSIDE: 1 }); new Vector3$1(); new Vector3$1(); const scratchVector$3 = new Vector3$1(); const scratchVector2 = new Vector3$1(); class BoundingSphere { constructor(center = [0, 0, 0], radius = 0.0) { this.radius = -0; this.center = new Vector3$1(); this.fromCenterRadius(center, radius); } fromCenterRadius(center, radius) { this.center.from(center); this.radius = radius; return this; } fromCornerPoints(corner, oppositeCorner) { oppositeCorner = scratchVector$3.from(oppositeCorner); this.center = new Vector3$1().from(corner).add(oppositeCorner).scale(0.5); this.radius = this.center.distance(oppositeCorner); return this; } equals(right) { return this === right || Boolean(right) && this.center.equals(right.center) && this.radius === right.radius; } clone() { return new BoundingSphere(this.center, this.radius); } union(boundingSphere) { const leftCenter = this.center; const leftRadius = this.radius; const rightCenter = boundingSphere.center; const rightRadius = boundingSphere.radius; const toRightCenter = scratchVector$3.copy(rightCenter).subtract(leftCenter); const centerSeparation = toRightCenter.magnitude(); if (leftRadius >= centerSeparation + rightRadius) { return this.clone(); } if (rightRadius >= centerSeparation + leftRadius) { return boundingSphere.clone(); } const halfDistanceBetweenTangentPoints = (leftRadius + centerSeparation + rightRadius) * 0.5; scratchVector2.copy(toRightCenter).scale((-leftRadius + halfDistanceBetweenTangentPoints) / centerSeparation).add(leftCenter); this.center.copy(scratchVector2); this.radius = halfDistanceBetweenTangentPoints; return this; } expand(point) { point = scratchVector$3.from(point); const radius = point.subtract(this.center).magnitude(); if (radius > this.radius) { this.radius = radius; } return this; } transform(transform) { this.center.transform(transform); const scale = getScaling(scratchVector$3, transform); this.radius = Math.max(scale[0], Math.max(scale[1], scale[2])) * this.radius; return this; } distanceSquaredTo(point) { const d = this.distanceTo(point); return d * d; } distanceTo(point) { point = scratchVector$3.from(point); const delta = point.subtract(this.center); return Math.max(0, delta.len() - this.radius); } intersectPlane(plane) { const center = this.center; const radius = this.radius; const normal = plane.normal; const distanceToPlane = normal.dot(center) + plane.distance; if (distanceToPlane < -radius) { return INTERSECTION.OUTSIDE; } if (distanceToPlane < radius) { return INTERSECTION.INTERSECTING; } return INTERSECTION.INSIDE; } } const scratchVector3 = new Vector3$1(); const scratchOffset = new Vector3$1(); const scratchVectorU = new Vector3$1(); const scratchVectorV = new Vector3$1(); const scratchVectorW = new Vector3$1(); const scratchCorner = new Vector3$1(); const scratchToCenter = new Vector3$1(); const MATRIX3 = { COLUMN0ROW0: 0, COLUMN0ROW1: 1, COLUMN0ROW2: 2, COLUMN1ROW0: 3, COLUMN1ROW1: 4, COLUMN1ROW2: 5, COLUMN2ROW0: 6, COLUMN2ROW1: 7, COLUMN2ROW2: 8 }; class OrientedBoundingBox { constructor(center = [0, 0, 0], halfAxes = [0, 0, 0, 0, 0, 0, 0, 0, 0]) { this.center = new Vector3$1().from(center); this.halfAxes = new Matrix3$1(halfAxes); } get halfSize() { const xAxis = this.halfAxes.getColumn(0); const yAxis = this.halfAxes.getColumn(1); const zAxis = this.halfAxes.getColumn(2); return [new Vector3$1(xAxis).len(), new Vector3$1(yAxis).len(), new Vector3$1(zAxis).len()]; } get quaternion() { const xAxis = this.halfAxes.getColumn(0); const yAxis = this.halfAxes.getColumn(1); const zAxis = this.halfAxes.getColumn(2); const normXAxis = new Vector3$1(xAxis).normalize(); const normYAxis = new Vector3$1(yAxis).normalize(); const normZAxis = new Vector3$1(zAxis).normalize(); return new Quaternion$1().fromMatrix3(new Matrix3$1([...normXAxis, ...normYAxis, ...normZAxis])); } fromCenterHalfSizeQuaternion(center, halfSize, quaternion) { const quaternionObject = new Quaternion$1(quaternion); const directionsMatrix = new Matrix3$1().fromQuaternion(quaternionObject); directionsMatrix[0] = directionsMatrix[0] * halfSize[0]; directionsMatrix[1] = directionsMatrix[1] * halfSize[0]; directionsMatrix[2] = directionsMatrix[2] * halfSize[0]; directionsMatrix[3] = directionsMatrix[3] * halfSize[1]; directionsMatrix[4] = directionsMatrix[4] * halfSize[1]; directionsMatrix[5] = directionsMatrix[5] * halfSize[1]; directionsMatrix[6] = directionsMatrix[6] * halfSize[2]; directionsMatrix[7] = directionsMatrix[7] * halfSize[2]; directionsMatrix[8] = directionsMatrix[8] * halfSize[2]; this.center = new Vector3$1().from(center); this.halfAxes = directionsMatrix; return this; } clone() { return new OrientedBoundingBox(this.center, this.halfAxes); } equals(right) { return this === right || Boolean(right) && this.center.equals(right.center) && this.halfAxes.equals(right.halfAxes); } getBoundingSphere(result = new BoundingSphere()) { const halfAxes = this.halfAxes; const u = halfAxes.getColumn(0, scratchVectorU);//前三个 const v = halfAxes.getColumn(1, scratchVectorV); const w = halfAxes.getColumn(2, scratchVectorW); const cornerVector = scratchVector3.copy(u).add(v).add(w); result.center.copy(this.center); result.radius = cornerVector.magnitude(); return result; } intersectPlane(plane) { const center = this.center; const normal = plane.normal; const halfAxes = this.halfAxes; const normalX = normal.x; const normalY = normal.y; const normalZ = normal.z; const radEffective = Math.abs(normalX * halfAxes[MATRIX3.COLUMN0ROW0] + normalY * halfAxes[MATRIX3.COLUMN0ROW1] + normalZ * halfAxes[MATRIX3.COLUMN0ROW2]) + Math.abs(normalX * halfAxes[MATRIX3.COLUMN1ROW0] + normalY * halfAxes[MATRIX3.COLUMN1ROW1] + normalZ * halfAxes[MATRIX3.COLUMN1ROW2]) + Math.abs(normalX * halfAxes[MATRIX3.COLUMN2ROW0] + normalY * halfAxes[MATRIX3.COLUMN2ROW1] + normalZ * halfAxes[MATRIX3.COLUMN2ROW2]); const distanceToPlane = normal.dot(center) + plane.distance; if (distanceToPlane <= -radEffective) { return INTERSECTION.OUTSIDE; } else if (distanceToPlane >= radEffective) { return INTERSECTION.INSIDE; } return INTERSECTION.INTERSECTING; //在相机frustum内 } distanceTo(point) { return Math.sqrt(this.distanceSquaredTo(point)); } /* distanceSquaredTo(point) {//相机到bound外壳的距离平方 let center = this.center.clone() center.z *= -1 const offset = scratchOffset.from(point).subtract(center); const halfAxes = this.halfAxes; const u = halfAxes.getColumn(0, scratchVectorU); const v = halfAxes.getColumn(1, scratchVectorV); const w = halfAxes.getColumn(2, scratchVectorW); u.z *= -1 v.z *= -1 w.z *= -1 const uHalf = u.magnitude(); const vHalf = v.magnitude(); const wHalf = w.magnitude(); u.normalize(); v.normalize(); w.normalize(); let distanceSquared = 0.0; let d; d = Math.abs(offset.dot(u)) - uHalf; if (d > 0) { distanceSquared += d * d; } d = Math.abs(offset.dot(v)) - vHalf; if (d > 0) { distanceSquared += d * d; } d = Math.abs(offset.dot(w)) - wHalf; if (d > 0) { distanceSquared += d * d; } return distanceSquared; } */ distanceSquaredTo(point) {//相机到bound外壳的距离平方 const offset = scratchOffset.from(point).subtract(this.center); const halfAxes = this.halfAxes; const u = halfAxes.getColumn(0, scratchVectorU); const v = halfAxes.getColumn(1, scratchVectorV); const w = halfAxes.getColumn(2, scratchVectorW); const uHalf = u.magnitude(); const vHalf = v.magnitude(); const wHalf = w.magnitude(); u.normalize(); v.normalize(); w.normalize(); let distanceSquared = 0.0; let d; d = Math.abs(offset.dot(u)) - uHalf; if (d > 0) { distanceSquared += d * d; } d = Math.abs(offset.dot(v)) - vHalf; if (d > 0) { distanceSquared += d * d; } d = Math.abs(offset.dot(w)) - wHalf; if (d > 0) { distanceSquared += d * d; } return distanceSquared; } computePlaneDistances(position, direction, result = [-0, -0]) { let minDist = Number.POSITIVE_INFINITY; let maxDist = Number.NEGATIVE_INFINITY; const center = this.center; const halfAxes = this.halfAxes; const u = halfAxes.getColumn(0, scratchVectorU); const v = halfAxes.getColumn(1, scratchVectorV); const w = halfAxes.getColumn(2, scratchVectorW); const corner = scratchCorner.copy(u).add(v).add(w).add(center); const toCenter = scratchToCenter.copy(corner).subtract(position); let mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); corner.copy(center).add(u).add(v).subtract(w); toCenter.copy(corner).subtract(position); mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); corner.copy(center).add(u).subtract(v).add(w); toCenter.copy(corner).subtract(position); mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); corner.copy(center).add(u).subtract(v).subtract(w); toCenter.copy(corner).subtract(position); mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); center.copy(corner).subtract(u).add(v).add(w); toCenter.copy(corner).subtract(position); mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); center.copy(corner).subtract(u).add(v).subtract(w); toCenter.copy(corner).subtract(position); mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); center.copy(corner).subtract(u).subtract(v).add(w); toCenter.copy(corner).subtract(position); mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); center.copy(corner).subtract(u).subtract(v).subtract(w); toCenter.copy(corner).subtract(position); mag = direction.dot(toCenter); minDist = Math.min(mag, minDist); maxDist = Math.max(mag, maxDist); result[0] = minDist; result[1] = maxDist; return result; } transform(transformation) { this.center.transformAsPoint(transformation); const xAxis = this.halfAxes.getColumn(0, scratchVectorU); xAxis.transformAsPoint(transformation); const yAxis = this.halfAxes.getColumn(1, scratchVectorV); yAxis.transformAsPoint(transformation); const zAxis = this.halfAxes.getColumn(2, scratchVectorW); zAxis.transformAsPoint(transformation); this.halfAxes = new Matrix3$1([...xAxis, ...yAxis, ...zAxis]); return this; } getTransform() { throw new Error('not implemented'); } } const scratchPosition$1 = new Vector3$1(); const scratchNormal$1 = new Vector3$1(); class Plane$1 { constructor(normal = [0, 0, 1], distance = 0) { this.normal = new Vector3$1(); this.distance = -0; this.fromNormalDistance(normal, distance); } fromNormalDistance(normal, distance) { assert$4(Number.isFinite(distance)); this.normal.from(normal).normalize(); this.distance = distance; return this; } fromPointNormal(point, normal) { point = scratchPosition$1.from(point); this.normal.from(normal).normalize(); const distance = -this.normal.dot(point); this.distance = distance; return this; } fromCoefficients(a, b, c, d) { this.normal.set(a, b, c); assert$4(equals$1(this.normal.len(), 1)); this.distance = d; return this; } clone(plane) { return new Plane$1(this.normal, this.distance); } equals(right) { return equals$1(this.distance, right.distance) && equals$1(this.normal, right.normal); } getPointDistance(point) { return this.normal.dot(point) + this.distance; } transform(matrix4) { const normal = scratchNormal$1.copy(this.normal).transformAsVector(matrix4).normalize(); const point = this.normal.scale(-this.distance).transform(matrix4); return this.fromPointNormal(point, normal); } projectPointOntoPlane(point, result = [0, 0, 0]) { point = scratchPosition$1.from(point); const pointDistance = this.getPointDistance(point); const scaledNormal = scratchNormal$1.copy(this.normal).scale(pointDistance); return point.subtract(scaledNormal).to(result); } } const faces = [new Vector3$1([1, 0, 0]), new Vector3$1([0, 1, 0]), new Vector3$1([0, 0, 1])]; const scratchPlaneCenter = new Vector3$1(); const scratchPlaneNormal$1 = new Vector3$1(); new Plane$1(new Vector3$1(1.0, 0.0, 0.0), 0.0); class CullingVolume { static get MASK_OUTSIDE() { return 0xffffffff; } static get MASK_INSIDE() { return 0x00000000; } static get MASK_INDETERMINATE() { return 0x7fffffff; } constructor(planes = []) { this.planes = planes; assert$4(this.planes.every(plane => plane instanceof Plane$1)); } fromBoundingSphere(boundingSphere) { this.planes.length = 2 * faces.length; const center = boundingSphere.center; const radius = boundingSphere.radius; let planeIndex = 0; for (const faceNormal of faces) { let plane0 = this.planes[planeIndex]; let plane1 = this.planes[planeIndex + 1]; if (!plane0) { plane0 = this.planes[planeIndex] = new Plane$1(); } if (!plane1) { plane1 = this.planes[planeIndex + 1] = new Plane$1(); } const plane0Center = scratchPlaneCenter.copy(faceNormal).scale(-radius).add(center); -faceNormal.dot(plane0Center); plane0.fromPointNormal(plane0Center, faceNormal); const plane1Center = scratchPlaneCenter.copy(faceNormal).scale(radius).add(center); const negatedFaceNormal = scratchPlaneNormal$1.copy(faceNormal).negate(); -negatedFaceNormal.dot(plane1Center); plane1.fromPointNormal(plane1Center, negatedFaceNormal); planeIndex += 2; } return this; } computeVisibility(boundingVolume) { assert$4(boundingVolume); let intersect = INTERSECTION.INSIDE; for (const plane of this.planes) { const result = boundingVolume.intersectPlane(plane); switch (result) { case INTERSECTION.OUTSIDE: return INTERSECTION.OUTSIDE; case INTERSECTION.INTERSECTING: intersect = INTERSECTION.INTERSECTING; break; } } return intersect; } computeVisibilityWithPlaneMask(boundingVolume, parentPlaneMask) { assert$4(boundingVolume, 'boundingVolume is required.'); assert$4(Number.isFinite(parentPlaneMask), 'parentPlaneMask is required.'); if (parentPlaneMask === CullingVolume.MASK_OUTSIDE || parentPlaneMask === CullingVolume.MASK_INSIDE) { return parentPlaneMask; } let mask = CullingVolume.MASK_INSIDE; const planes = this.planes; for (let k = 0; k < this.planes.length; ++k) { const flag = k < 31 ? 1 << k : 0; if (k < 31 && (parentPlaneMask & flag) === 0) { continue; } const plane = planes[k]; const result = boundingVolume.intersectPlane(plane); if (result === INTERSECTION.OUTSIDE) { return CullingVolume.MASK_OUTSIDE; } else if (result === INTERSECTION.INTERSECTING) { mask |= flag; } } return mask; } } const scratchPlaneUpVector = new Vector3$1(); const scratchPlaneRightVector = new Vector3$1(); const scratchPlaneNearCenter = new Vector3$1(); const scratchPlaneFarCenter = new Vector3$1(); const scratchPlaneNormal = new Vector3$1(); class PerspectiveOffCenterFrustum { constructor(options = {}) { options = { near: 1.0, far: 500000000.0, ...options }; this.left = options.left; this._left = undefined; this.right = options.right; this._right = undefined; this.top = options.top; this._top = undefined; this.bottom = options.bottom; this._bottom = undefined; this.near = options.near; this._near = this.near; this.far = options.far; this._far = this.far; this._cullingVolume = new CullingVolume([new Plane$1(), new Plane$1(), new Plane$1(), new Plane$1(), new Plane$1(), new Plane$1()]); this._perspectiveMatrix = new Matrix4$1(); this._infinitePerspective = new Matrix4$1(); } clone() { return new PerspectiveOffCenterFrustum({ right: this.right, left: this.left, top: this.top, bottom: this.bottom, near: this.near, far: this.far }); } equals(other) { return other && other instanceof PerspectiveOffCenterFrustum && this.right === other.right && this.left === other.left && this.top === other.top && this.bottom === other.bottom && this.near === other.near && this.far === other.far; } get projectionMatrix() { update$1(this); return this._perspectiveMatrix; } get infiniteProjectionMatrix() { update$1(this); return this._infinitePerspective; } computeCullingVolume(position, direction, up) { assert$4(position, 'position is required.'); assert$4(direction, 'direction is required.'); assert$4(up, 'up is required.'); const planes = this._cullingVolume.planes; up = scratchPlaneUpVector.copy(up).normalize(); const right = scratchPlaneRightVector.copy(direction).cross(up).normalize(); const nearCenter = scratchPlaneNearCenter.copy(direction).multiplyByScalar(this.near).add(position); const farCenter = scratchPlaneFarCenter.copy(direction).multiplyByScalar(this.far).add(position); let normal = scratchPlaneNormal; normal.copy(right).multiplyByScalar(this.left).add(nearCenter).subtract(position).cross(up); planes[0].fromPointNormal(position, normal); normal.copy(right).multiplyByScalar(this.right).add(nearCenter).subtract(position).cross(up).negate(); planes[1].fromPointNormal(position, normal); normal.copy(up).multiplyByScalar(this.bottom).add(nearCenter).subtract(position).cross(right).negate(); planes[2].fromPointNormal(position, normal); normal.copy(up).multiplyByScalar(this.top).add(nearCenter).subtract(position).cross(right); planes[3].fromPointNormal(position, normal); normal = new Vector3$1().copy(direction); planes[4].fromPointNormal(nearCenter, normal); normal.negate(); planes[5].fromPointNormal(farCenter, normal); return this._cullingVolume; } getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result) { update$1(this); assert$4(Number.isFinite(drawingBufferWidth) && Number.isFinite(drawingBufferHeight)); assert$4(drawingBufferWidth > 0); assert$4(drawingBufferHeight > 0); assert$4(distance > 0); assert$4(result); const inverseNear = 1.0 / this.near; let tanTheta = this.top * inverseNear; const pixelHeight = 2.0 * distance * tanTheta / drawingBufferHeight; tanTheta = this.right * inverseNear; const pixelWidth = 2.0 * distance * tanTheta / drawingBufferWidth; result.x = pixelWidth; result.y = pixelHeight; return result; } } function update$1(frustum) { assert$4(Number.isFinite(frustum.right) && Number.isFinite(frustum.left) && Number.isFinite(frustum.top) && Number.isFinite(frustum.bottom) && Number.isFinite(frustum.near) && Number.isFinite(frustum.far)); const { top, bottom, right, left, near, far } = frustum; if (top !== frustum._top || bottom !== frustum._bottom || left !== frustum._left || right !== frustum._right || near !== frustum._near || far !== frustum._far) { assert$4(frustum.near > 0 && frustum.near < frustum.far, 'near must be greater than zero and less than far.'); frustum._left = left; frustum._right = right; frustum._top = top; frustum._bottom = bottom; frustum._near = near; frustum._far = far; frustum._perspectiveMatrix = new Matrix4$1().frustum({ left, right, bottom, top, near, far }); frustum._infinitePerspective = new Matrix4$1().frustum({ left, right, bottom, top, near, far: Infinity }); } } const defined$4 = val => val !== null && typeof val !== 'undefined'; class PerspectiveFrustum { constructor(options = {}) { options = { near: 1.0, far: 500000000.0, xOffset: 0.0, yOffset: 0.0, ...options }; this._offCenterFrustum = new PerspectiveOffCenterFrustum(); this.fov = options.fov; this._fov = undefined; this._fovy = undefined; this._sseDenominator = undefined; this.aspectRatio = options.aspectRatio; this._aspectRatio = undefined; this.near = options.near; this._near = this.near; this.far = options.far; this._far = this.far; this.xOffset = options.xOffset; this._xOffset = this.xOffset; this.yOffset = options.yOffset; this._yOffset = this.yOffset; } clone() { return new PerspectiveFrustum({ aspectRatio: this.aspectRatio, fov: this.fov, near: this.near, far: this.far }); } equals(other) { if (!defined$4(other) || !(other instanceof PerspectiveFrustum)) { return false; } update(this); update(other); return this.fov === other.fov && this.aspectRatio === other.aspectRatio && this.near === other.near && this.far === other.far && this._offCenterFrustum.equals(other._offCenterFrustum); } get projectionMatrix() { update(this); return this._offCenterFrustum.projectionMatrix; } get infiniteProjectionMatrix() { update(this); return this._offCenterFrustum.infiniteProjectionMatrix; } get fovy() { update(this); return this._fovy; } get sseDenominator() { update(this); return this._sseDenominator; } computeCullingVolume(position, direction, up) { update(this); return this._offCenterFrustum.computeCullingVolume(position, direction, up); } getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result) { update(this); return this._offCenterFrustum.getPixelDimensions(drawingBufferWidth, drawingBufferHeight, distance, result); } } function update(frustum) { assert$4(Number.isFinite(frustum.fov) && Number.isFinite(frustum.aspectRatio) && Number.isFinite(frustum.near) && Number.isFinite(frustum.far)); const f = frustum._offCenterFrustum; if (frustum.fov !== frustum._fov || frustum.aspectRatio !== frustum._aspectRatio || frustum.near !== frustum._near || frustum.far !== frustum._far || frustum.xOffset !== frustum._xOffset || frustum.yOffset !== frustum._yOffset) { assert$4(frustum.fov >= 0 && frustum.fov < Math.PI); //assert$4(frustum.aspectRatio > 0); if(frustum.aspectRatio == 0){ console.log(1); } assert$4(frustum.near >= 0 && frustum.near < frustum.far); frustum._aspectRatio = Math.max(0.001, frustum.aspectRatio); frustum._fov = frustum.fov; frustum._fovy = frustum._aspectRatio <= 1 ? frustum.fov : Math.atan(Math.tan(frustum.fov * 0.5) / frustum._aspectRatio) * 2.0; frustum._near = frustum.near; frustum._far = frustum.far; frustum._sseDenominator = 2.0 * Math.tan(0.5 * frustum._fovy); frustum._xOffset = frustum.xOffset; frustum._yOffset = frustum.yOffset; f.top = frustum.near * Math.tan(0.5 * frustum._fovy); f.bottom = -f.top; f.right = frustum._aspectRatio * f.top; f.left = -f.right; f.near = frustum.near; f.far = frustum.far; f.right += frustum.xOffset; f.left += frustum.xOffset; f.top += frustum.yOffset; f.bottom += frustum.yOffset; } } new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Matrix3$1(); new Matrix3$1(); new Matrix3$1(); new Matrix3$1(); new Matrix3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); new Matrix3$1(); ({ diagonal: new Matrix3$1(), unitary: new Matrix3$1() }); const scratchVector$2 = new Vector3$1(); const scratchPosition = new Vector3$1(); const cullingVolume = new CullingVolume([new Plane$1(), new Plane$1(), new Plane$1(), new Plane$1(), new Plane$1(), new Plane$1()]); function getFrameState(viewport, frameNumber) { const { cameraDirection, cameraUp, height } = viewport; const { metersPerUnit } = viewport.distanceScales; const viewportCenterCartographic = viewport.unprojectPosition(viewport.center); const viewportCenterCartesian = Ellipsoid.WGS84.cartographicToCartesian(viewportCenterCartographic, new Vector3$1()); const enuToFixedTransform = Ellipsoid.WGS84.eastNorthUpToFixedFrame(viewportCenterCartesian); const cameraPositionCartographic = viewport.unprojectPosition(viewport.cameraPosition); const cameraPositionCartesian = Ellipsoid.WGS84.cartographicToCartesian(cameraPositionCartographic, new Vector3$1()); const cameraDirectionCartesian = new Vector3$1(enuToFixedTransform.transformAsVector(new Vector3$1(cameraDirection).scale(metersPerUnit))).normalize(); const cameraUpCartesian = new Vector3$1(enuToFixedTransform.transformAsVector(new Vector3$1(cameraUp).scale(metersPerUnit))).normalize(); commonSpacePlanesToWGS84(viewport, viewportCenterCartesian); return { camera: { position: cameraPositionCartesian, direction: cameraDirectionCartesian, up: cameraUpCartesian }, viewport, height, cullingVolume, frameNumber, sseDenominator: 1.15 }; } function commonSpacePlanesToWGS84(viewport, viewportCenterCartesian) { const frustumPlanes = viewport.getFrustumPlanes(); let i = 0; for (const dir in frustumPlanes) { const plane = frustumPlanes[dir]; const distanceToCenter = plane.normal.dot(viewport.center); scratchPosition.copy(plane.normal).scale(plane.distance - distanceToCenter).add(viewport.center); const cartographicPos = viewport.unprojectPosition(scratchPosition); const cartesianPos = Ellipsoid.WGS84.cartographicToCartesian(cartographicPos, new Vector3$1()); cullingVolume.planes[i++].fromPointNormal(cartesianPos, scratchVector$2.copy(viewportCenterCartesian).subtract(cartesianPos)); } } const WGS84_RADIUS_X = 6378137.0; const WGS84_RADIUS_Y = 6378137.0; const WGS84_RADIUS_Z = 6356752.3142451793; const scratchVector$1 = new Vector3$1(); function getZoomFromBoundingVolume(boundingVolume) { const { halfAxes, radius, width, height } = boundingVolume; if (halfAxes) { const obbSize = getObbSize(halfAxes); return Math.log2(WGS84_RADIUS_Z / obbSize); } else if (radius) { return Math.log2(WGS84_RADIUS_Z / radius); } else if (height && width) { const zoomX = Math.log2(WGS84_RADIUS_X / width); const zoomY = Math.log2(WGS84_RADIUS_Y / height); return (zoomX + zoomY) / 2; } return 1; } function getObbSize(halfAxes) { halfAxes.getColumn(0, scratchVector$1); const axeY = halfAxes.getColumn(1); const axeZ = halfAxes.getColumn(2); const farthestVertex = scratchVector$1.add(axeY).add(axeZ); const size = farthestVertex.len(); return size; } const TILE_CONTENT_STATE = { UNLOADED: 0, LOADING: 1, PROCESSING: 2, READY: 3, EXPIRED: 4, FAILED: 5 }; const TILE_REFINEMENT = { ADD: 1, REPLACE: 2 }; const TILE_TYPE = { EMPTY: 'empty', SCENEGRAPH: 'scenegraph', POINTCLOUD: 'pointcloud', MESH: 'mesh' }; const TILESET_TYPE = { I3S: 'I3S', TILES3D: 'TILES3D' }; const LOD_METRIC_TYPE = { GEOMETRIC_ERROR: 'geometricError', MAX_SCREEN_THRESHOLD: 'maxScreenThreshold' }; const TILE3D_OPTIMIZATION_HINT = { NOT_COMPUTED: -1, USE_OPTIMIZATION: 1, SKIP_OPTIMIZATION: 0 }; function defined$3(x) { return x !== undefined && x !== null; } const scratchScale = new Vector3$1(); const scratchNorthWest = new Vector3$1(); const scratchSouthEast = new Vector3$1(); function createBoundingVolume(boundingVolumeHeader, transform, result) { assert$7(boundingVolumeHeader, '3D Tile: boundingVolume must be defined'); if (boundingVolumeHeader.box) { return createBox(boundingVolumeHeader.box, transform, result); } if (boundingVolumeHeader.region) { const [west, south, east, north, minHeight, maxHeight] = boundingVolumeHeader.region; const northWest = Ellipsoid.WGS84.cartographicToCartesian([degrees(west), degrees(north), minHeight], scratchNorthWest); const southEast = Ellipsoid.WGS84.cartographicToCartesian([degrees(east), degrees(south), maxHeight], scratchSouthEast); const centerInCartesian = new Vector3$1().addVectors(northWest, southEast).multiplyScalar(0.5); const radius = new Vector3$1().subVectors(northWest, southEast).len() / 2.0; return createSphere([centerInCartesian[0], centerInCartesian[1], centerInCartesian[2], radius], new Matrix4$1()); } if (boundingVolumeHeader.sphere) { return createSphere(boundingVolumeHeader.sphere, transform, result); } throw new Error('3D Tile: boundingVolume must contain a sphere, region, or box'); } function createBox(box, transform, result) { const center = new Vector3$1(box[0], box[1], box[2]); result && (result.oriCenter = center);//add transform.transform(center, center); let origin = []; if (box.length === 10) { const halfSize = box.slice(3, 6); const quaternion = new Quaternion$1(); quaternion.fromArray(box, 6); const x = new Vector3$1([1, 0, 0]); const y = new Vector3$1([0, 1, 0]); const z = new Vector3$1([0, 0, 1]); x.transformByQuaternion(quaternion); x.scale(halfSize[0]); y.transformByQuaternion(quaternion); y.scale(halfSize[1]); z.transformByQuaternion(quaternion); z.scale(halfSize[2]); origin = [...x.toArray(), ...y.toArray(), ...z.toArray()]; } else { origin = [...box.slice(3, 6), ...box.slice(6, 9), ...box.slice(9, 12)]; } const xAxis = transform.transformAsVector(origin.slice(0, 3)); const yAxis = transform.transformAsVector(origin.slice(3, 6)); const zAxis = transform.transformAsVector(origin.slice(6, 9)); const halfAxes = new Matrix3$1([xAxis[0], xAxis[1], xAxis[2], yAxis[0], yAxis[1], yAxis[2], zAxis[0], zAxis[1], zAxis[2]]); //const halfAxes = new Matrix3([xAxis[0], xAxis[1], -xAxis[2], yAxis[0], yAxis[1], -yAxis[2], zAxis[0], zAxis[1], -zAxis[2]]);//改 //center.z *= -1//add if (defined$3(result)) { result.center = center; result.halfAxes = halfAxes; return result; } return new OrientedBoundingBox(center, halfAxes); } function createSphere(sphere, transform, result) { const center = new Vector3$1(sphere[0], sphere[1], sphere[2]); transform.transform(center, center); const scale = transform.getScale(scratchScale); const uniformScale = Math.max(Math.max(scale[0], scale[1]), scale[2]); const radius = sphere[3] * uniformScale; if (defined$3(result)) { result.center = center; result.radius = radius; return result; } return new BoundingSphere(center, radius); } new Vector3$1(); new Vector3$1(); new Matrix4$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); function fog(distanceToCamera, density) { const scalar = distanceToCamera * density; return 1.0 - Math.exp(-(scalar * scalar)); } function getDynamicScreenSpaceError(tileset, distanceToCamera) { if (tileset.dynamicScreenSpaceError && tileset.dynamicScreenSpaceErrorComputedDensity) { const density = tileset.dynamicScreenSpaceErrorComputedDensity; const factor = tileset.dynamicScreenSpaceErrorFactor; const dynamicError = fog(distanceToCamera, density) * factor; return dynamicError; } return 0; } function getTiles3DScreenSpaceError(tile, frameState, useParentLodMetric) { const tileset = tile.tileset; const parentLodMetricValue = tile.parent && tile.parent.lodMetricValue || tile.lodMetricValue; const lodMetricValue = useParentLodMetric ? parentLodMetricValue : tile.lodMetricValue; if (lodMetricValue === 0.0) { return 0.0; } const distance = Math.max(tile._distanceToCamera, 1e-7); const { height, sseDenominator } = frameState; const { viewDistanceScale } = tileset.options; let error = lodMetricValue * height * (viewDistanceScale || 1.0) / (distance * sseDenominator); error -= getDynamicScreenSpaceError(tileset, distance); return error; } function getLodStatus(tile, frameState) { if (tile.lodMetricValue === 0 || isNaN(tile.lodMetricValue)) { return 'DIG'; } const screenSize = 2 * getProjectedRadius(tile, frameState); if (screenSize < 2) { return 'OUT'; } if (!tile.header.children || screenSize <= tile.lodMetricValue) { return 'DRAW'; } else if (tile.header.children) { return 'DIG'; } return 'OUT'; } function getProjectedRadius(tile, frameState) { const originalViewport = frameState.viewport; const ViewportClass = originalViewport.constructor; const { longitude, latitude, height, width, bearing, zoom } = originalViewport; const viewport = new ViewportClass({ longitude, latitude, height, width, bearing, zoom, pitch: 0 }); const mbsLat = tile.header.mbs[1]; const mbsLon = tile.header.mbs[0]; const mbsZ = tile.header.mbs[2]; const mbsR = tile.header.mbs[3]; const mbsCenterCartesian = [...tile.boundingVolume.center]; const cameraPositionCartographic = viewport.unprojectPosition(viewport.cameraPosition); const cameraPositionCartesian = Ellipsoid.WGS84.cartographicToCartesian(cameraPositionCartographic, new Vector3$1()); const toEye = new Vector3$1(cameraPositionCartesian).subtract(mbsCenterCartesian).normalize(); const enuToCartesianMatrix = new Matrix4$1(); Ellipsoid.WGS84.eastNorthUpToFixedFrame(mbsCenterCartesian, enuToCartesianMatrix); const cartesianToEnuMatrix = new Matrix4$1(enuToCartesianMatrix).invert(); const cameraPositionEnu = new Vector3$1(cameraPositionCartesian).transform(cartesianToEnuMatrix); const projection = Math.sqrt(cameraPositionEnu[0] * cameraPositionEnu[0] + cameraPositionEnu[1] * cameraPositionEnu[1]); const extraZ = projection * projection / cameraPositionEnu[2]; const extraVertexEnu = new Vector3$1([cameraPositionEnu[0], cameraPositionEnu[1], extraZ]); const extraVertexCartesian = extraVertexEnu.transform(enuToCartesianMatrix); const extraVectorCartesian = new Vector3$1(extraVertexCartesian).subtract(mbsCenterCartesian).normalize(); const radiusVector = toEye.cross(extraVectorCartesian).normalize().scale(mbsR); const sphereMbsBorderVertexCartesian = new Vector3$1(mbsCenterCartesian).add(radiusVector); const sphereMbsBorderVertexCartographic = Ellipsoid.WGS84.cartesianToCartographic(sphereMbsBorderVertexCartesian); const projectedOrigin = viewport.project([mbsLon, mbsLat, mbsZ]); const projectedMbsBorderVertex = viewport.project(sphereMbsBorderVertexCartographic); const projectedRadius = new Vector3$1(projectedOrigin).subtract(projectedMbsBorderVertex).magnitude(); return projectedRadius; } function get3dTilesOptions(tileset) { return { assetGltfUpAxis: tileset.asset && tileset.asset.gltfUpAxis || 'Y' }; } class ManagedArray { constructor(length = 0) { _defineProperty(this, "_map", new Map()); _defineProperty(this, "_array", void 0); _defineProperty(this, "_length", void 0); this._array = new Array(length); this._length = length; } get length() { return this._length; } set length(length) { this._length = length; if (length > this._array.length) { this._array.length = length; } } get values() { return this._array; } get(index) { assert$7(index < this._array.length); return this._array[index]; } set(index, element) { assert$7(index >= 0); if (index >= this.length) { this.length = index + 1; } if (this._map.has(this._array[index])) { this._map.delete(this._array[index]); } this._array[index] = element; this._map.set(element, index); } delete(element) { const index = this._map.get(element); if (index >= 0) { this._array.splice(index, 1); this._map.delete(element); this.length--; } } peek() { return this._array[this._length - 1]; } push(element) { if (!this._map.has(element)) { const index = this.length++; this._array[index] = element; this._map.set(element, index); } } pop() { const element = this._array[--this.length]; this._map.delete(element); return element; } reserve(length) { assert$7(length >= 0); if (length > this._array.length) { this._array.length = length; } } resize(length) { assert$7(length >= 0); this.length = length; } trim(length) { if (length === null || length === undefined) { length = this.length; } this._array.length = length; } reset() { this._array = []; this._map = new Map(); this._length = 0; } find(target) { return this._map.has(target); } } const DEFAULT_PROPS$1 = { loadSiblings: false, skipLevelOfDetail: false, maximumScreenSpaceError: 2, updateTransforms: true, onTraversalEnd: () => {}, viewportTraversersMap: {}, basePath: '' }; class TilesetTraverser { constructor(options) { _defineProperty(this, "options", void 0); _defineProperty(this, "root", void 0); _defineProperty(this, "requestedTiles", void 0); _defineProperty(this, "selectedTiles", void 0); _defineProperty(this, "emptyTiles", void 0); _defineProperty(this, "_traversalStack", void 0); _defineProperty(this, "_emptyTraversalStack", void 0); _defineProperty(this, "_frameNumber", void 0); this.options = { ...DEFAULT_PROPS$1, ...options }; this._traversalStack = new ManagedArray(); this._emptyTraversalStack = new ManagedArray(); this._frameNumber = null; this.root = null; this.selectedTiles = {}; this.requestedTiles = {}; this.emptyTiles = {}; } traverse(root, frameState, options) { this.root = root; this.options = { ...this.options, ...options }; this.reset(); this.updateTile(root, frameState); this._frameNumber = frameState.frameNumber; this.executeTraversal(root, frameState); } reset() { this.requestedTiles = {}; this.selectedTiles = {}; this.emptyTiles = {}; this._traversalStack.reset(); this._emptyTraversalStack.reset(); } executeTraversal(root, frameState) {// 遍历root const stack = this._traversalStack; root._selectionDepth = 1; stack.push(root); while (stack.length > 0) { const tile = stack.pop(); let shouldRefine = false; // !zeg改 循环遍历子tile, maxDepth用来限制可加载的最大深度 if (this.canTraverse(tile, frameState) && (tile.depth < /* this.options. */maxDepth || tile.type != 'scenegraph' )) {//add maxDepth 是否继续遍历子集 this.updateChildTiles(tile, frameState); shouldRefine = this.updateAndPushChildren(tile, frameState, stack, tile.hasRenderContent ? tile._selectionDepth + 1 : tile._selectionDepth); } const parent = tile.parent; const parentRefines = Boolean(!parent || parent._shouldRefine); const stoppedRefining = !shouldRefine; if (!tile.hasRenderContent) { this.emptyTiles[tile.id] = tile; this.loadTile(tile, frameState); if (stoppedRefining) { this.selectTile(tile, frameState); } } else if (tile.refine === TILE_REFINEMENT.ADD) { this.loadTile(tile, frameState); this.selectTile(tile, frameState); } else if (tile.refine === TILE_REFINEMENT.REPLACE) { this.loadTile(tile, frameState); if (stoppedRefining) { this.selectTile(tile, frameState); } } this.touchTile(tile, frameState); tile._shouldRefine = shouldRefine && parentRefines; } this.options.onTraversalEnd(frameState); } updateChildTiles(tile, frameState) { const children = tile.children; for (const child of children) { this.updateTile(child, frameState); } return true; } updateAndPushChildren(tile, frameState, stack, depth) { const { loadSiblings, skipLevelOfDetail } = this.options; const children = tile.children; children.sort(this.compareDistanceToCamera.bind(this)); const checkRefines = tile.refine === TILE_REFINEMENT.REPLACE && tile.hasRenderContent && !skipLevelOfDetail; let hasVisibleChild = false; let refines = true; for (const child of children) { child._selectionDepth = depth; if (child.isVisibleAndInRequestVolume) { if (stack.find(child)) { stack.delete(child); } stack.push(child); hasVisibleChild = true; } else if (checkRefines || loadSiblings) { this.loadTile(child, frameState); this.touchTile(child, frameState); } if (checkRefines) { let childRefines; if (!child._inRequestVolume) { childRefines = false; } else if (!child.hasRenderContent) { childRefines = this.executeEmptyTraversal(child, frameState); } else { childRefines = child.contentAvailable; } refines = refines && childRefines; if (!refines) { return false; } } } if (!hasVisibleChild) { refines = false; } return refines; } updateTile(tile, frameState) { this.updateTileVisibility(tile, frameState); } selectTile(tile, frameState) { if (this.shouldSelectTile(tile)) { tile._selectedFrame = frameState.frameNumber; this.selectedTiles[tile.id] = tile; } } loadTile(tile, frameState) { if (this.shouldLoadTile(tile)) { tile._requestedFrame = frameState.frameNumber; tile._priority = tile._getPriority(); this.requestedTiles[tile.id] = tile; } } touchTile(tile, frameState) { tile.tileset._cache.touch(tile); tile._touchedFrame = frameState.frameNumber; } canTraverse(tile, frameState, useParentMetric = false, ignoreVisibility = false) { if (!tile.hasChildren) { return false; } if (tile.hasTilesetContent) { return !tile.contentExpired; } if (!ignoreVisibility && !tile.isVisibleAndInRequestVolume) { return false; } return this.shouldRefine(tile, frameState, useParentMetric); } shouldLoadTile(tile) { return tile.hasUnloadedContent || tile.contentExpired; } shouldSelectTile(tile) { return tile.contentAvailable && !this.options.skipLevelOfDetail; } shouldRefine(tile, frameState, useParentMetric) { let screenSpaceError = tile._screenSpaceError; if (useParentMetric) { screenSpaceError = tile.getScreenSpaceError(frameState, true); } return screenSpaceError > this.options.maximumScreenSpaceError; } updateTileVisibility(tile, frameState) { const viewportIds = []; if (this.options.viewportTraversersMap) { for (const key in this.options.viewportTraversersMap) { const value = this.options.viewportTraversersMap[key]; if (value === frameState.viewport.id) { viewportIds.push(key); } } } else { viewportIds.push(frameState.viewport.id); } tile.updateVisibility(frameState, viewportIds); } compareDistanceToCamera(b, a) { return b._distanceToCamera - a._distanceToCamera; } anyChildrenVisible(tile, frameState) { let anyVisible = false; for (const child of tile.children) { child.updateVisibility(frameState); anyVisible = anyVisible || child.isVisibleAndInRequestVolume; } return anyVisible; } executeEmptyTraversal(root, frameState) { let allDescendantsLoaded = true; const stack = this._emptyTraversalStack; stack.push(root); while (stack.length > 0 && allDescendantsLoaded) { const tile = stack.pop(); this.updateTile(tile, frameState); if (!tile.isVisibleAndInRequestVolume) { this.loadTile(tile, frameState); } this.touchTile(tile, frameState); const traverse = !tile.hasRenderContent && this.canTraverse(tile, frameState, false, true); if (traverse) { const children = tile.children; for (const child of children) { if (stack.find(child)) { stack.delete(child); } stack.push(child); } } else if (!tile.contentAvailable) { allDescendantsLoaded = false; } } return allDescendantsLoaded; } } const scratchVector = new Vector3$1(); function defined$2(x) { return x !== undefined && x !== null; } class TileHeader { constructor(tileset, header, parentHeader, extendedId = '') { _defineProperty(this, "tileset", void 0); _defineProperty(this, "header", void 0); _defineProperty(this, "id", void 0); _defineProperty(this, "url", void 0); _defineProperty(this, "parent", void 0); _defineProperty(this, "refine", void 0); _defineProperty(this, "type", void 0); _defineProperty(this, "contentUrl", void 0); _defineProperty(this, "lodMetricType", void 0); _defineProperty(this, "lodMetricValue", void 0); _defineProperty(this, "boundingVolume", void 0); _defineProperty(this, "content", void 0); _defineProperty(this, "contentState", void 0); _defineProperty(this, "gpuMemoryUsageInBytes", void 0); _defineProperty(this, "children", void 0); _defineProperty(this, "depth", void 0); _defineProperty(this, "viewportIds", void 0); _defineProperty(this, "transform", void 0); _defineProperty(this, "extensions", void 0); _defineProperty(this, "userData", void 0); _defineProperty(this, "computedTransform", void 0); _defineProperty(this, "hasEmptyContent", void 0); _defineProperty(this, "hasTilesetContent", void 0); _defineProperty(this, "traverser", void 0); _defineProperty(this, "_cacheNode", void 0); _defineProperty(this, "_frameNumber", void 0); _defineProperty(this, "_lodJudge", void 0); _defineProperty(this, "_expireDate", void 0); _defineProperty(this, "_expiredContent", void 0); _defineProperty(this, "_shouldRefine", void 0); _defineProperty(this, "_distanceToCamera", void 0); _defineProperty(this, "_centerZDepth", void 0); _defineProperty(this, "_screenSpaceError", void 0); _defineProperty(this, "_visibilityPlaneMask", void 0); _defineProperty(this, "_visible", void 0); _defineProperty(this, "_inRequestVolume", void 0); _defineProperty(this, "_stackLength", void 0); _defineProperty(this, "_selectionDepth", void 0); _defineProperty(this, "_touchedFrame", void 0); _defineProperty(this, "_visitedFrame", void 0); _defineProperty(this, "_selectedFrame", void 0); _defineProperty(this, "_requestedFrame", void 0); _defineProperty(this, "_priority", void 0); _defineProperty(this, "_contentBoundingVolume", void 0); _defineProperty(this, "_viewerRequestVolume", void 0); _defineProperty(this, "_initialTransform", void 0); _defineProperty(this, "tilesetMatrix", void 0);//add _defineProperty(this, "tileContent", void 0);//add _defineProperty(this, "contentTransform", void 0);//add _defineProperty(this, "volumeBox", void 0);//add _defineProperty(this, "boundUntransformed", void 0);//add _defineProperty(this, "rtcCenterState", rtcCenterGlobal);//add 初始时的状态,当mesh加载完检查下 this.header = header; this.tileset = tileset; this.id = extendedId || header.id; this.url = header.url; this.parent = parentHeader; this.refine = this._getRefine(header.refine); this.type = header.type; this.contentUrl = header.contentUrl; this.lodMetricType = 'geometricError'; this.lodMetricValue = 0; this.boundingVolume = null; this.content = null; this.contentState = TILE_CONTENT_STATE.UNLOADED; this.gpuMemoryUsageInBytes = 0; this.children = []; this.hasEmptyContent = false; this.hasTilesetContent = false; this.depth = 0; this.viewportIds = []; this.userData = {}; this.extensions = null; this._priority = 0; this._touchedFrame = 0; this._visitedFrame = 0; this._selectedFrame = 0; this._requestedFrame = 0; this._screenSpaceError = 0; this._cacheNode = null; this._frameNumber = null; this._cacheNode = null; this.traverser = new TilesetTraverser({}); this._shouldRefine = false; this._distanceToCamera = 0; this._centerZDepth = 0; this._visible = undefined; this._inRequestVolume = false; this._stackLength = 0; this._selectionDepth = 0; this._initialTransform = new Matrix4$1(); this.transform = new Matrix4$1(); this._initializeLodMetric(header); this._initializeTransforms(header); this._initializeBoundingVolumes(header); this._initializeContent(header); this._initializeRenderingState(header); this._lodJudge = null; this._expireDate = null; this._expiredContent = null; Object.seal(this); } destroy() { this.header = null; } isDestroyed() { return this.header === null; } get selected() { return this._selectedFrame === this.tileset._frameNumber; } get isVisible() { return this._visible; } get isVisibleAndInRequestVolume() { let v = /* this._visible && */this.tileset.visible && this._inRequestVolume; //用_updateBoundingVolume这个算的在相机靠近时不准确,容易缺块 if(window.tileVisi2)v = this._visible && v; return v } get hasRenderContent() { return !this.hasEmptyContent && !this.hasTilesetContent; } get hasChildren() { return this.children.length > 0 || this.header.children && this.header.children.length > 0; } get contentReady() { return this.contentState === TILE_CONTENT_STATE.READY || this.hasEmptyContent; } get contentAvailable() { return Boolean(this.contentReady && this.hasRenderContent || this._expiredContent && !this.contentFailed); } get hasUnloadedContent() { return this.hasRenderContent && this.contentUnloaded; } get contentUnloaded() { return this.contentState === TILE_CONTENT_STATE.UNLOADED; } get contentExpired() { return this.contentState === TILE_CONTENT_STATE.EXPIRED; } get contentFailed() { return this.contentState === TILE_CONTENT_STATE.FAILED; } getScreenSpaceError(frameState, useParentLodMetric) { switch (this.tileset.type) { case TILESET_TYPE.I3S: return getProjectedRadius(this, frameState); case TILESET_TYPE.TILES3D: return getTiles3DScreenSpaceError(this, frameState, useParentLodMetric); default: throw new Error('Unsupported tileset type'); } } _getPriority() { const traverser = this.tileset._traverser; const { skipLevelOfDetail } = traverser.options; const maySkipTile = this.refine === TILE_REFINEMENT.ADD || skipLevelOfDetail; if (maySkipTile && !this.isVisible && this._visible !== undefined) { return -1; } if (this.tileset._frameNumber - this._touchedFrame >= 1) { return -1; } if (this.contentState === TILE_CONTENT_STATE.UNLOADED) { return -1; } const parent = this.parent; const useParentScreenSpaceError = parent && (!maySkipTile || this._screenSpaceError === 0.0 || parent.hasTilesetContent); const screenSpaceError = useParentScreenSpaceError ? parent._screenSpaceError : this._screenSpaceError; const rootScreenSpaceError = traverser.root ? traverser.root._screenSpaceError : 0.0; let v = Math.max(rootScreenSpaceError - screenSpaceError, 0); if(!this._visible){ v = THREE.Math.clamp(v * 0.1, -0.9, 100);//xzw add 因为this._visible我也令其显示所以这里降低这些的优先级 } return v } async loadContent() { if (this.hasEmptyContent) { return false; } if (this.content) { return true; } const expired = this.contentExpired; if (expired) { this._expireDate = null; } this.contentState = TILE_CONTENT_STATE.LOADING; const requestToken = await this.tileset._requestScheduler.scheduleRequest(this.id, this._getPriority.bind(this)); if (!requestToken) { this.contentState = TILE_CONTENT_STATE.UNLOADED; return false; } try { const contentUrl = this.tileset.getTileUrl(this.contentUrl)+ `?_=${this.tileset.options.updateTime}`; // !zeg改 添加version后缀 原:imageVersion const loader = this.tileset.loader; const options = { ...this.tileset.loadOptions, [loader.id]: { ...this.tileset.loadOptions[loader.id], isTileset: this.type === 'json', ...this._getLoaderSpecificOptions(loader.id) } }; this.content = await load(contentUrl, loader, options); // !zeg改 if (/* this.tileset.options. */maxDepth < this.depth && this.type == 'scenegraph') {//有的场景depth为1的不是mesh,只是json,即实际最低depth>1 this.unloadContent(); return false } if (this.tileset.options.contentLoader) { await this.tileset.options.contentLoader(this); } if (this._isTileset()) { this.tileset._initializeTileHeaders(this.content, this); } this.contentState = TILE_CONTENT_STATE.READY; this._onContentLoaded(); return true; } catch (error) { this.contentState = TILE_CONTENT_STATE.FAILED; throw error; } finally { requestToken.done(); } } unloadContent() { if (this.content && this.content.destroy) { this.content.destroy(); } this.content = null; if (this.header.content && this.header.content.destroy) { this.header.content.destroy(); } this.header.content = null; this.contentState = TILE_CONTENT_STATE.UNLOADED; return true; } updateVisibility(frameState, viewportIds) { if (this._frameNumber === frameState.frameNumber) { return; } const parent = this.parent; const parentVisibilityPlaneMask = parent ? parent._visibilityPlaneMask : CullingVolume.MASK_INDETERMINATE; if (this.tileset._traverser.options.updateTransforms) { const parentTransform = parent ? parent.computedTransform : new Matrix4$1().elements; //this.tileset.modelMatrix; 改:去掉左乘modelMatrix this._updateTransform(parentTransform); } this._distanceToCamera = this.distanceToTile(frameState); this._screenSpaceError = this.getScreenSpaceError(frameState, false); this._visibilityPlaneMask = this.visibility(frameState, parentVisibilityPlaneMask); this._visible = this._visibilityPlaneMask !== CullingVolume.MASK_OUTSIDE; this._inRequestVolume = this.insideViewerRequestVolume(frameState); this._frameNumber = frameState.frameNumber; this.viewportIds = viewportIds; } visibility(frameState, parentVisibilityPlaneMask) { const { cullingVolume } = frameState; const { boundingVolume } = this; return cullingVolume.computeVisibilityWithPlaneMask(boundingVolume, parentVisibilityPlaneMask); } contentVisibility() { return true; } distanceToTile(frameState) { const boundingVolume = this.boundingVolume; return Math.sqrt(Math.max(boundingVolume.distanceSquaredTo(frameState.camera.position), 0)); } cameraSpaceZDepth({ camera }) { const boundingVolume = this.boundingVolume; scratchVector.subVectors(boundingVolume.center, camera.position); return camera.direction.dot(scratchVector); } insideViewerRequestVolume(frameState) { const viewerRequestVolume = this._viewerRequestVolume; return !viewerRequestVolume || viewerRequestVolume.distanceSquaredTo(frameState.camera.position) <= 0; } updateExpiration() { if (defined$2(this._expireDate) && this.contentReady && !this.hasEmptyContent) { const now = Date.now(); if (Date.lessThan(this._expireDate, now)) { this.contentState = TILE_CONTENT_STATE.EXPIRED; this._expiredContent = this.content; } } } get extras() { return this.header.extras; } _initializeLodMetric(header) { if ('lodMetricType' in header) { this.lodMetricType = header.lodMetricType; } else { this.lodMetricType = this.parent && this.parent.lodMetricType || this.tileset.lodMetricType; console.warn("3D Tile: Required prop lodMetricType is undefined. Using parent lodMetricType"); } if ('lodMetricValue' in header) { this.lodMetricValue = header.lodMetricValue; } else { this.lodMetricValue = this.parent && this.parent.lodMetricValue || this.tileset.lodMetricValue; console.warn('3D Tile: Required prop lodMetricValue is undefined. Using parent lodMetricValue'); } } _initializeTransforms(tileHeader) { this.transform = tileHeader.transform ? new Matrix4$1(tileHeader.transform) : new Matrix4$1(); const parent = this.parent; const tileset = this.tileset; const parentTransform = parent && parent.computedTransform ? parent.computedTransform.clone() : new Matrix4$1().elements; // tileset.modelMatrix.clone(); 改:去掉左乘modelMatrix this.computedTransform = new Matrix4$1(parentTransform).multiplyRight(this.transform); const parentInitialTransform = parent && parent._initialTransform ? parent._initialTransform.clone() : new Matrix4$1(); this._initialTransform = new Matrix4$1(parentInitialTransform).multiplyRight(this.transform); } _initializeBoundingVolumes(tileHeader) { this._contentBoundingVolume = null; this._viewerRequestVolume = null; this._updateBoundingVolume(tileHeader); } _initializeContent(tileHeader) { this.content = { _tileset: this.tileset, _tile: this }; this.hasEmptyContent = true; this.contentState = TILE_CONTENT_STATE.UNLOADED; this.hasTilesetContent = false; if (tileHeader.contentUrl) { this.content = null; this.hasEmptyContent = false; } } _initializeRenderingState(header) { this.depth = header.level || (this.parent ? this.parent.depth + 1 : 0); this._shouldRefine = false; this._distanceToCamera = 0; this._centerZDepth = 0; this._screenSpaceError = 0; this._visibilityPlaneMask = CullingVolume.MASK_INDETERMINATE; this._visible = undefined; this._inRequestVolume = false; this._stackLength = 0; this._selectionDepth = 0; this._frameNumber = 0; this._touchedFrame = 0; this._visitedFrame = 0; this._selectedFrame = 0; this._requestedFrame = 0; this._priority = 0.0; } _getRefine(refine) { return refine || this.parent && this.parent.refine || TILE_REFINEMENT.REPLACE; } _isTileset() { return this.contentUrl.indexOf('.json') !== -1; } _onContentLoaded() { switch (this.content && this.content.type) { case 'vctr': case 'geom': this.tileset._traverser.disableSkipLevelOfDetail = true; break; } if (this._isTileset()) { this.hasTilesetContent = true; } } _updateBoundingVolume(header) { let computedTransform; if(header.id == 'root_0' || !header.id){ console.log('root_0'); } if(!this.tileset.lastRootTransform){//没事过后会因_updateTransform重算 computedTransform = new Matrix4$1(this.tileset.modelMatrix).multiplyRight(this.computedTransform); //add }else { computedTransform = new Matrix4$1(new Matrix4().multiplyMatrices(this.tileset.lastRootTransform, this.getContentTransform()).elements); } this.boundingVolume = createBoundingVolume(header.boundingVolume, computedTransform/* this.computedTransform */, this.boundingVolume); const content = header.content; if (!content) { return; } if (content.boundingVolume) { this._contentBoundingVolume = createBoundingVolume(content.boundingVolume, computedTransform, this._contentBoundingVolume); } if (header.viewerRequestVolume) { this._viewerRequestVolume = createBoundingVolume(header.viewerRequestVolume, computedTransform, this._viewerRequestVolume); } } getBoundUntransformed(){//add 这个会在mesh加载好后才会执行 if(!this.contentTransform){ console.error('?'); } this.getContentTransform(); let computedTransform2 = new Matrix4$1(this.contentTransform.elements); this.boundUntransformed = createBoundingVolume(this.header.boundingVolume, computedTransform2 , this.boundUntransformed);//add return this.boundUntransformed } getContentTransform(force){ //add 获取该tile在创建boundingVolume时需要的matrix,不包含最外层对整体模型的变换. let rtcCenter, contentTransform; if(this.contentTransform && !force){//无需更新 return this.contentTransform.clone() } if(!this.tileset.tileTransInvert){ this.tileset.tileTransInvert = new Matrix4$1(); } this.contentTransform = new Matrix4().fromArray(this.computedTransform).premultiply(new Matrix4().fromArray(this.tileset.tileTransInvert)); if(this.tileContent){ rtcCenter = this.content.rtcCenter; }else { rtcCenter = this.rtcCenterState; } if(!rtcCenter) { let tranM = new Matrix4(); tranM.makeScale(1,1,-1);//大部分是这种情况. 就是因为这个变换才加的这个函数,之前bounding没考虑这个不准。不知道为什么原生的代码不用scaleZ this.contentTransform.premultiply(tranM); } //如果有rtcCenter的话一开始boundingVolume不准?会不会有显示问题? return this.contentTransform.clone() } _updateTransform(parentTransform = new Matrix4$1()) { const computedTransform = parentTransform.clone().multiplyRight(this.transform); const didTransformChange = !computedTransform.equals(this.computedTransform) || !this.tilesetMatrix || !this.tilesetMatrix.equals(this.tileset.modelMatrix); //改 后面加了两个判断,为了在模型移动后更新_updateBoundingVolume if (!didTransformChange) { return; } this.tileset.modelMatrix; this.computedTransform = computedTransform; this.tilesetMatrix = this.tileset.modelMatrix; //add 标记 this._updateBoundingVolume(this.header); } _getLoaderSpecificOptions(loaderId) { switch (loaderId) { case 'i3s': return { ...this.tileset.options.i3s, tile: this.header, tileset: this.tileset.tileset, isTileHeader: false }; case '3d-tiles': case 'cesium-ion': default: return get3dTilesOptions(this.tileset.tileset); } } } class Tileset3DTraverser extends TilesetTraverser { compareDistanceToCamera(a, b) { return b._distanceToCamera === 0 && a._distanceToCamera === 0 ? b._centerZDepth - a._centerZDepth : b._distanceToCamera - a._distanceToCamera; } updateTileVisibility(tile, frameState) { super.updateTileVisibility(tile, frameState); if (!tile.isVisibleAndInRequestVolume) { return; } const hasChildren = tile.children.length > 0; if (tile.hasTilesetContent && hasChildren) { const firstChild = tile.children[0]; this.updateTileVisibility(firstChild, frameState); tile._visible = firstChild._visible; return; } if (this.meetsScreenSpaceErrorEarly(tile, frameState)) { tile._visible = false; return; } const replace = tile.refine === TILE_REFINEMENT.REPLACE; const useOptimization = tile._optimChildrenWithinParent === TILE3D_OPTIMIZATION_HINT.USE_OPTIMIZATION; if (replace && useOptimization && hasChildren) { if (!this.anyChildrenVisible(tile, frameState)) { tile._visible = false; return; } } } meetsScreenSpaceErrorEarly(tile, frameState) { const { parent } = tile; if (!parent || parent.hasTilesetContent || parent.refine !== TILE_REFINEMENT.ADD) { return false; } return !this.shouldRefine(tile, frameState, true); } } const STATUS = { REQUESTED: 'REQUESTED', COMPLETED: 'COMPLETED', ERROR: 'ERROR' }; class I3STileManager { constructor() { _defineProperty(this, "_statusMap", void 0); this._statusMap = {}; } add(request, key, callback, frameState) { if (!this._statusMap[key]) { this._statusMap[key] = { request, callback, key, frameState, status: STATUS.REQUESTED }; request().then(data => { this._statusMap[key].status = STATUS.COMPLETED; this._statusMap[key].callback(data, frameState); }).catch(error => { this._statusMap[key].status = STATUS.ERROR; callback(error); }); } } update(key, frameState) { if (this._statusMap[key]) { this._statusMap[key].frameState = frameState; } } find(key) { return this._statusMap[key]; } } class I3STilesetTraverser extends TilesetTraverser { constructor(options) { super(options); _defineProperty(this, "_tileManager", void 0); this._tileManager = new I3STileManager(); } shouldRefine(tile, frameState) { tile._lodJudge = getLodStatus(tile, frameState); return tile._lodJudge === 'DIG'; } updateChildTiles(tile, frameState) { const children = tile.header.children || []; const childTiles = tile.children; const tileset = tile.tileset; for (const child of children) { const extendedId = "".concat(child.id, "-").concat(frameState.viewport.id); const childTile = childTiles && childTiles.find(t => t.id === extendedId); if (!childTile) { let request = () => this._loadTile(child.id, tileset); const cachedRequest = this._tileManager.find(extendedId); if (!cachedRequest) { if (tileset.tileset.nodePages) { request = () => tileset.tileset.nodePagesTile.formTileFromNodePages(child.id); } this._tileManager.add(request, extendedId, header => this._onTileLoad(header, tile, extendedId), frameState); } else { this._tileManager.update(extendedId, frameState); } } else if (childTile) { this.updateTile(childTile, frameState); } } return false; } async _loadTile(nodeId, tileset) { const { loader } = tileset; const nodeUrl = tileset.getTileUrl("".concat(tileset.url, "/nodes/").concat(nodeId)); const options = { ...tileset.loadOptions, i3s: { ...tileset.loadOptions.i3s, isTileHeader: true, loadContent: false } }; return await load(nodeUrl, loader, options); } _onTileLoad(header, tile, extendedId) { const childTile = new TileHeader(tile.tileset, header, tile, extendedId); tile.children.push(childTile); const frameState = this._tileManager.find(childTile.id).frameState; this.updateTile(childTile, frameState); if (this._frameNumber === frameState.frameNumber) { this.executeTraversal(childTile, frameState); } } } const DEFAULT_PROPS = { description: '', ellipsoid: Ellipsoid.WGS84, modelMatrix: new Matrix4$1(), throttleRequests: true, maxRequests: 64, maximumMemoryUsage: 32, onTileLoad: () => {}, onTileUnload: () => {}, onTileError: () => {}, onTraversalComplete: selectedTiles => selectedTiles, contentLoader: undefined, viewDistanceScale: 1.0, maximumScreenSpaceError: 8, loadTiles: true, updateTransforms: true, viewportTraversersMap: null, loadOptions: { fetch: {} }, attributions: [], basePath: '', i3s: {} }; const TILES_TOTAL = 'Tiles In Tileset(s)'; const TILES_IN_MEMORY = 'Tiles In Memory'; const TILES_IN_VIEW = 'Tiles In View'; const TILES_RENDERABLE = 'Tiles To Render'; const TILES_LOADED = 'Tiles Loaded'; const TILES_LOADING = 'Tiles Loading'; const TILES_UNLOADED = 'Tiles Unloaded'; const TILES_LOAD_FAILED = 'Failed Tile Loads'; const POINTS_COUNT = 'Points'; const TILES_GPU_MEMORY = 'Tile Memory Use'; let tilesetSid = 0;//add class Tileset3D extends EventDispatcher{//xzw add EventDispatcher constructor(json, options) { super(); viewer.visiVertexCount = viewer.visiVertexCount || 0; _defineProperty(this, "options", void 0); _defineProperty(this, "loadOptions", void 0); _defineProperty(this, "type", void 0); _defineProperty(this, "tileset", void 0); _defineProperty(this, "loader", void 0); _defineProperty(this, "url", void 0); _defineProperty(this, "basePath", void 0); _defineProperty(this, "modelMatrix", void 0); _defineProperty(this, "ellipsoid", void 0); _defineProperty(this, "lodMetricType", void 0); _defineProperty(this, "lodMetricValue", void 0); _defineProperty(this, "refine", void 0); _defineProperty(this, "root", void 0); _defineProperty(this, "roots", void 0); _defineProperty(this, "asset", void 0); _defineProperty(this, "description", void 0); _defineProperty(this, "properties", void 0); _defineProperty(this, "extras", void 0); _defineProperty(this, "attributions", void 0); _defineProperty(this, "credits", void 0); _defineProperty(this, "stats", void 0); _defineProperty(this, "traverseCounter", void 0); _defineProperty(this, "geometricError", void 0); _defineProperty(this, "selectedTiles", void 0); _defineProperty(this, "cartographicCenter", void 0); _defineProperty(this, "cartesianCenter", void 0); _defineProperty(this, "zoom", void 0); _defineProperty(this, "boundingVolume", void 0); _defineProperty(this, "gpuMemoryUsageInBytes", void 0); _defineProperty(this, "dynamicScreenSpaceErrorComputedDensity", void 0); _defineProperty(this, "_traverser", void 0); _defineProperty(this, "_cache", void 0); _defineProperty(this, "_requestScheduler", void 0); _defineProperty(this, "_frameNumber", void 0); _defineProperty(this, "_queryParamsString", void 0); _defineProperty(this, "_queryParams", void 0); _defineProperty(this, "_extensionsUsed", void 0); _defineProperty(this, "_tiles", void 0); _defineProperty(this, "_pendingCount", void 0); _defineProperty(this, "lastUpdatedVieports", void 0); _defineProperty(this, "_requestedTiles", void 0); _defineProperty(this, "_emptyTiles", void 0); _defineProperty(this, "frameStateData", void 0); _defineProperty(this, "maximumMemoryUsage", void 0); _defineProperty(this, "visible", true);//add assert$7(json); this.options = { ...DEFAULT_PROPS, ...options }; this.tileset = json; this.loader = json.loader; this.type = json.type; this.url = json.url; this.basePath = json.basePath || dirname(this.url); this.modelMatrix = this.options.modelMatrix; this.ellipsoid = this.options.ellipsoid; this.lodMetricType = json.lodMetricType; this.lodMetricValue = json.lodMetricValue; this.refine = json.root.refine; this.loadOptions = this.options.loadOptions || {}; this.root = null; this.roots = {}; this.cartographicCenter = null; this.cartesianCenter = null; this.zoom = 1; this.boundingVolume = null; this.traverseCounter = 0; this.geometricError = 0; this._traverser = this._initializeTraverser(); this._cache = new TilesetCache(); this._requestScheduler = new RequestScheduler({ throttleRequests: this.options.throttleRequests, maxRequests: this.options.maxRequests }); this._frameNumber = 0; this._pendingCount = 0; this._tiles = {}; this.selectedTiles = []; this._emptyTiles = []; this._requestedTiles = []; this.frameStateData = {}; this.lastUpdatedVieports = null; this._queryParams = {}; this._queryParamsString = ''; this.maximumMemoryUsage = this.options.maximumMemoryUsage || 32; this.gpuMemoryUsageInBytes = 0; this.stats = new Stats$1({ id: this.url }); this._initializeStats(); this._extensionsUsed = undefined; this.dynamicScreenSpaceErrorComputedDensity = 0.0; this.extras = null; this.asset = {}; this.credits = {}; this.description = this.options.description || ''; this._initializeTileSet(json); } destroy() { this._destroy(); } isLoaded() { return this._pendingCount === 0 && this._frameNumber !== 0; } get tiles() { return Object.values(this._tiles); } get frameNumber() { return this._frameNumber; } get queryParams() { if (!this._queryParamsString) { this._queryParamsString = getQueryParamString(this._queryParams); } return this._queryParamsString; } setProps(props) { this.options = { ...this.options, ...props }; } setOptions(options) { this.options = { ...this.options, ...options }; } getTileUrl(tilePath) { const isDataUrl = tilePath.startsWith('data:'); tilePath = Potree.Common.dealURL(tilePath); //add 去除'+' if (isDataUrl) { return tilePath; } return "".concat(tilePath).concat(this.queryParams); } hasExtension(extensionName) { return Boolean(this._extensionsUsed && this._extensionsUsed.indexOf(extensionName) > -1); } update(viewports) { if ('loadTiles' in this.options && !this.options.loadTiles) { return; } if (this.traverseCounter > 0) { return; } if (!viewports && this.lastUpdatedVieports) { viewports = this.lastUpdatedVieports; } else { this.lastUpdatedVieports = viewports; } if (!(viewports instanceof Array)) { viewports = [viewports]; } this._cache.reset(); this._frameNumber++; this.traverseCounter = viewports.length; const viewportsToTraverse = []; for (const viewport of viewports) { const id = viewport.id; if (this._needTraverse(id)) { viewportsToTraverse.push(id); } else { this.traverseCounter--; } } for (const viewport of viewports) { const id = viewport.id; if (!this.roots[id]) { this.roots[id] = this._initializeTileHeaders(this.tileset, null); } if (!viewportsToTraverse.includes(id)) { continue; } const frameState = getFrameState(viewport, this._frameNumber); this._traverser.traverse(this.roots[id], frameState, this.options); } } _needTraverse(viewportId) { let traverserId = viewportId; if (this.options.viewportTraversersMap) { traverserId = this.options.viewportTraversersMap[viewportId]; } if (traverserId !== viewportId) { return false; } return true; } _onTraversalEnd(frameState) { const id = frameState.viewport.id; if (!this.frameStateData[id]) { this.frameStateData[id] = { selectedTiles: [], _requestedTiles: [], _emptyTiles: [] }; } const currentFrameStateData = this.frameStateData[id]; const selectedTiles = Object.values(this._traverser.selectedTiles); currentFrameStateData.selectedTiles = selectedTiles; currentFrameStateData._requestedTiles = Object.values(this._traverser.requestedTiles); currentFrameStateData._emptyTiles = Object.values(this._traverser.emptyTiles); this.traverseCounter--; if (this.traverseCounter > 0) { return; } this._updateTiles(); } _updateTiles() { this.selectedTiles = []; this._requestedTiles = []; this._emptyTiles = []; for (const frameStateKey in this.frameStateData) { const frameStateDataValue = this.frameStateData[frameStateKey]; this.selectedTiles = this.selectedTiles.concat(frameStateDataValue.selectedTiles); this._requestedTiles = this._requestedTiles.concat(frameStateDataValue._requestedTiles); this._emptyTiles = this._emptyTiles.concat(frameStateDataValue._emptyTiles); } this.selectedTiles = this.options.onTraversalComplete(this.selectedTiles); for (const tile of this.selectedTiles) { this._tiles[tile.id] = tile; } this._loadTiles(); this._unloadTiles(); this._updateStats(); } _tilesChanged(oldSelectedTiles, selectedTiles) { if (oldSelectedTiles.length !== selectedTiles.length) { return true; } const set1 = new Set(oldSelectedTiles.map(t => t.id)); const set2 = new Set(selectedTiles.map(t => t.id)); let changed = oldSelectedTiles.filter(x => !set2.has(x.id)).length > 0; changed = changed || selectedTiles.filter(x => !set1.has(x.id)).length > 0; return changed; } _loadTiles() { for (const tile of this._requestedTiles) { if (tile.contentUnloaded) { this._loadTile(tile); } } } _unloadTiles() { this._cache.unloadTiles(this, (tileset, tile) => tileset._unloadTile(tile)); } _updateStats() { let tilesRenderable = 0; let pointsRenderable = 0; for (const tile of this.selectedTiles) { if (tile.contentAvailable && tile.content) { tilesRenderable++; if (tile.content.pointCount) { pointsRenderable += tile.content.pointCount; } } } this.stats.get(TILES_IN_VIEW).count = this.selectedTiles.length; this.stats.get(TILES_RENDERABLE).count = tilesRenderable; this.stats.get(POINTS_COUNT).count = pointsRenderable; } _initializeTileSet(tilesetJson) { this.root = this._initializeTileHeaders(tilesetJson, null); if (this.type === TILESET_TYPE.TILES3D) { this._initializeCesiumTileset(tilesetJson); } if (this.type === TILESET_TYPE.I3S) { this._initializeI3STileset(); } this._calculateViewProps(); } _calculateViewProps() { const root = this.root; assert$7(root); const { center } = root.boundingVolume; if (!center) { console.warn('center was not pre-calculated for the root tile'); this.cartographicCenter = new Vector3$1(); this.zoom = 1; return; } this.cartographicCenter = Ellipsoid.WGS84.cartesianToCartographic(center, new Vector3$1()); this.cartesianCenter = center; this.zoom = getZoomFromBoundingVolume(root.boundingVolume); } _initializeStats() { this.stats.get(TILES_TOTAL); this.stats.get(TILES_LOADING); this.stats.get(TILES_IN_MEMORY); this.stats.get(TILES_IN_VIEW); this.stats.get(TILES_RENDERABLE); this.stats.get(TILES_LOADED); this.stats.get(TILES_UNLOADED); this.stats.get(TILES_LOAD_FAILED); this.stats.get(POINTS_COUNT, 'memory'); this.stats.get(TILES_GPU_MEMORY, 'memory'); } _initializeTileHeaders(tilesetJson, parentTileHeader) { const rootTile = new TileHeader(this, tilesetJson.root, parentTileHeader, parentTileHeader == void 0 && 'root_'+tilesetSid++); if (parentTileHeader) { parentTileHeader.children.push(rootTile); rootTile.depth = parentTileHeader.depth + 1; } if (this.type === TILESET_TYPE.TILES3D) { const stack = []; stack.push(rootTile); while (stack.length > 0) { const tile = stack.pop(); this.stats.get(TILES_TOTAL).incrementCount(); const children = tile.header.children || []; // !zeg改 if(tile.depth < /* this.options. */maxDepth){ for (const childHeader of children) { const childTile = new TileHeader(this, childHeader, tile); tile.children.push(childTile); childTile.depth = tile.depth + 1; stack.push(childTile); } } window.maxTileDepth = Math.max(window.maxTileDepth||0, tile.depth); } } return rootTile; } _initializeTraverser() { let TraverserClass; const type = this.type; switch (type) { case TILESET_TYPE.TILES3D: TraverserClass = Tileset3DTraverser; break; case TILESET_TYPE.I3S: TraverserClass = I3STilesetTraverser; break; default: TraverserClass = TilesetTraverser; } return new TraverserClass({ basePath: this.basePath, onTraversalEnd: this._onTraversalEnd.bind(this) }); } _destroyTileHeaders(parentTile) { this._destroySubtree(parentTile); } async _loadTile(tile) { let loaded; try { this._onStartTileLoading(); loaded = await tile.loadContent(); } catch (error) { this._onTileLoadError(tile, error); } finally { this._onEndTileLoading(tile);// this._onTileLoad(tile, loaded); } } _onTileLoadError(tile, error) { this.stats.get(TILES_LOAD_FAILED).incrementCount(); const message = error.message || error.toString(); const url = tile.url; console.error("A 3D tile failed to load: ".concat(tile.url, " ").concat(message)); this.options.onTileError(tile, message, url); } _onTileLoad(tile, loaded) { if (!loaded) { return; } if (tile && tile.content) { calculateTransformProps(tile, tile.content); } this._addTileToCache(tile); this.options.onTileLoad(tile); } _onStartTileLoading() { this._pendingCount++; this.stats.get(TILES_LOADING).incrementCount(); } _onEndTileLoading(tile) { this._pendingCount--; this.stats.get(TILES_LOADING).decrementCount(); this.dispatchEvent({type:'endTileLoading', tile, loadingCount: this._pendingCount }); // !zeg改 } _addTileToCache(tile) { this._cache.add(this, tile, tileset => tileset._updateCacheStats(tile)); } _updateCacheStats(tile) { this.stats.get(TILES_LOADED).incrementCount(); this.stats.get(TILES_IN_MEMORY).incrementCount(); this.gpuMemoryUsageInBytes += tile.content.byteLength || 0; this.stats.get(TILES_GPU_MEMORY).count = this.gpuMemoryUsageInBytes; } _unloadTile(tile) { this.gpuMemoryUsageInBytes -= tile.content && tile.content.byteLength || 0; this.stats.get(TILES_IN_MEMORY).decrementCount(); this.stats.get(TILES_UNLOADED).incrementCount(); this.stats.get(TILES_GPU_MEMORY).count = this.gpuMemoryUsageInBytes; this.options.onTileUnload(tile); tile.unloadContent(); } _destroy() { const stack = []; if (this.root) { stack.push(this.root); } while (stack.length > 0) { const tile = stack.pop(); for (const child of tile.children) { stack.push(child); } this._destroyTile(tile); } this.root = null; } _destroySubtree(tile) { const root = tile; const stack = []; stack.push(root); while (stack.length > 0) { tile = stack.pop(); for (const child of tile.children) { stack.push(child); } if (tile !== root) { this._destroyTile(tile); } } root.children = []; } _destroyTile(tile) { this._cache.unloadTile(this, tile); this._unloadTile(tile); tile.destroy(); } _initializeCesiumTileset(tilesetJson) { this.asset = tilesetJson.asset; if (!this.asset) { throw new Error('Tileset must have an asset property.'); } if (this.asset.version !== '0.0' && this.asset.version !== '1.0') { throw new Error('The tileset must be 3D Tiles version 0.0 or 1.0.'); } if ('tilesetVersion' in this.asset) { this._queryParams.v = this.asset.tilesetVersion; } this.credits = { attributions: this.options.attributions || [] }; this.description = this.options.description || ''; this.properties = tilesetJson.properties; this.geometricError = tilesetJson.geometricError; this._extensionsUsed = tilesetJson.extensionsUsed; this.extras = tilesetJson.extras; } _initializeI3STileset() { if (this.loadOptions.i3s && 'token' in this.loadOptions.i3s) { this._queryParams.token = this.loadOptions.i3s.token; } } } function getQueryParamString(queryParams) { const queryParamStrings = []; for (const key of Object.keys(queryParams)) { queryParamStrings.push("".concat(key, "=").concat(queryParams[key])); } switch (queryParamStrings.length) { case 0: return ''; case 1: return "?".concat(queryParamStrings[0]); default: return "?".concat(queryParamStrings.join('&')); } } const VERSION$5 = "3.1.4" ; const TILE3D_TYPE = { COMPOSITE: 'cmpt', POINT_CLOUD: 'pnts', BATCHED_3D_MODEL: 'b3dm', INSTANCED_3D_MODEL: 'i3dm', GEOMETRY: 'geom', VECTOR: 'vect', GLTF: 'glTF' }; function getStringFromArrayBuffer(arrayBuffer, byteOffset, byteLength) { assert$7(arrayBuffer instanceof ArrayBuffer); const textDecoder = new TextDecoder('utf8'); const typedArray = new Uint8Array(arrayBuffer, byteOffset, byteLength); const string = textDecoder.decode(typedArray); return string; } function getMagicString$1(arrayBuffer, byteOffset = 0) { const dataView = new DataView(arrayBuffer); return "".concat(String.fromCharCode(dataView.getUint8(byteOffset + 0))).concat(String.fromCharCode(dataView.getUint8(byteOffset + 1))).concat(String.fromCharCode(dataView.getUint8(byteOffset + 2))).concat(String.fromCharCode(dataView.getUint8(byteOffset + 3))); } const VERSION$4 = "3.1.4" ; const DEFAULT_DRACO_OPTIONS = { draco: { decoderType: typeof WebAssembly === 'object' ? 'wasm' : 'js', libraryPath: 'libs/', extraAttributes: {}, attributeNameEntry: undefined } }; const DracoLoader$1 = { name: 'Draco', id: 'draco', module: 'draco', shapes: ['mesh'], version: VERSION$4, worker: true, extensions: ['drc'], mimeTypes: ['application/octet-stream'], binary: true, tests: ['DRACO'], options: DEFAULT_DRACO_OPTIONS }; function getMeshBoundingBox(attributes) { let minX = Infinity; let minY = Infinity; let minZ = Infinity; let maxX = -Infinity; let maxY = -Infinity; let maxZ = -Infinity; const positions = attributes.POSITION ? attributes.POSITION.value : []; const len = positions && positions.length; for (let i = 0; i < len; i += 3) { const x = positions[i]; const y = positions[i + 1]; const z = positions[i + 2]; minX = x < minX ? x : minX; minY = y < minY ? y : minY; minZ = z < minZ ? z : minZ; maxX = x > maxX ? x : maxX; maxY = y > maxY ? y : maxY; maxZ = z > maxZ ? z : maxZ; } return [[minX, minY, minZ], [maxX, maxY, maxZ]]; } function assert$3(condition, message) { if (!condition) { throw new Error(message || 'loader assertion failed.'); } } class Schema { constructor(fields, metadata) { _defineProperty(this, "fields", void 0); _defineProperty(this, "metadata", void 0); assert$3(Array.isArray(fields)); checkNames(fields); this.fields = fields; this.metadata = metadata || new Map(); } compareTo(other) { if (this.metadata !== other.metadata) { return false; } if (this.fields.length !== other.fields.length) { return false; } for (let i = 0; i < this.fields.length; ++i) { if (!this.fields[i].compareTo(other.fields[i])) { return false; } } return true; } select(...columnNames) { const nameMap = Object.create(null); for (const name of columnNames) { nameMap[name] = true; } const selectedFields = this.fields.filter(field => nameMap[field.name]); return new Schema(selectedFields, this.metadata); } selectAt(...columnIndices) { const selectedFields = columnIndices.map(index => this.fields[index]).filter(Boolean); return new Schema(selectedFields, this.metadata); } assign(schemaOrFields) { let fields; let metadata = this.metadata; if (schemaOrFields instanceof Schema) { const otherSchema = schemaOrFields; fields = otherSchema.fields; metadata = mergeMaps(mergeMaps(new Map(), this.metadata), otherSchema.metadata); } else { fields = schemaOrFields; } const fieldMap = Object.create(null); for (const field of this.fields) { fieldMap[field.name] = field; } for (const field of fields) { fieldMap[field.name] = field; } const mergedFields = Object.values(fieldMap); return new Schema(mergedFields, metadata); } } function checkNames(fields) { const usedNames = {}; for (const field of fields) { if (usedNames[field.name]) { console.warn('Schema: duplicated field name', field.name, field); } usedNames[field.name] = true; } } function mergeMaps(m1, m2) { return new Map([...(m1 || new Map()), ...(m2 || new Map())]); } class Field { constructor(name, type, nullable = false, metadata = new Map()) { _defineProperty(this, "name", void 0); _defineProperty(this, "type", void 0); _defineProperty(this, "nullable", void 0); _defineProperty(this, "metadata", void 0); this.name = name; this.type = type; this.nullable = nullable; this.metadata = metadata; } get typeId() { return this.type && this.type.typeId; } clone() { return new Field(this.name, this.type, this.nullable, this.metadata); } compareTo(other) { return this.name === other.name && this.type === other.type && this.nullable === other.nullable && this.metadata === other.metadata; } toString() { return "".concat(this.type).concat(this.nullable ? ', nullable' : '').concat(this.metadata ? ", metadata: ".concat(this.metadata) : ''); } } let Type$1; (function (Type) { Type[Type["NONE"] = 0] = "NONE"; Type[Type["Null"] = 1] = "Null"; Type[Type["Int"] = 2] = "Int"; Type[Type["Float"] = 3] = "Float"; Type[Type["Binary"] = 4] = "Binary"; Type[Type["Utf8"] = 5] = "Utf8"; Type[Type["Bool"] = 6] = "Bool"; Type[Type["Decimal"] = 7] = "Decimal"; Type[Type["Date"] = 8] = "Date"; Type[Type["Time"] = 9] = "Time"; Type[Type["Timestamp"] = 10] = "Timestamp"; Type[Type["Interval"] = 11] = "Interval"; Type[Type["List"] = 12] = "List"; Type[Type["Struct"] = 13] = "Struct"; Type[Type["Union"] = 14] = "Union"; Type[Type["FixedSizeBinary"] = 15] = "FixedSizeBinary"; Type[Type["FixedSizeList"] = 16] = "FixedSizeList"; Type[Type["Map"] = 17] = "Map"; Type[Type["Dictionary"] = -1] = "Dictionary"; Type[Type["Int8"] = -2] = "Int8"; Type[Type["Int16"] = -3] = "Int16"; Type[Type["Int32"] = -4] = "Int32"; Type[Type["Int64"] = -5] = "Int64"; Type[Type["Uint8"] = -6] = "Uint8"; Type[Type["Uint16"] = -7] = "Uint16"; Type[Type["Uint32"] = -8] = "Uint32"; Type[Type["Uint64"] = -9] = "Uint64"; Type[Type["Float16"] = -10] = "Float16"; Type[Type["Float32"] = -11] = "Float32"; Type[Type["Float64"] = -12] = "Float64"; Type[Type["DateDay"] = -13] = "DateDay"; Type[Type["DateMillisecond"] = -14] = "DateMillisecond"; Type[Type["TimestampSecond"] = -15] = "TimestampSecond"; Type[Type["TimestampMillisecond"] = -16] = "TimestampMillisecond"; Type[Type["TimestampMicrosecond"] = -17] = "TimestampMicrosecond"; Type[Type["TimestampNanosecond"] = -18] = "TimestampNanosecond"; Type[Type["TimeSecond"] = -19] = "TimeSecond"; Type[Type["TimeMillisecond"] = -20] = "TimeMillisecond"; Type[Type["TimeMicrosecond"] = -21] = "TimeMicrosecond"; Type[Type["TimeNanosecond"] = -22] = "TimeNanosecond"; Type[Type["DenseUnion"] = -23] = "DenseUnion"; Type[Type["SparseUnion"] = -24] = "SparseUnion"; Type[Type["IntervalDayTime"] = -25] = "IntervalDayTime"; Type[Type["IntervalYearMonth"] = -26] = "IntervalYearMonth"; })(Type$1 || (Type$1 = {})); let _Symbol$toStringTag, _Symbol$toStringTag2, _Symbol$toStringTag7; class DataType { static isNull(x) { return x && x.typeId === Type$1.Null; } static isInt(x) { return x && x.typeId === Type$1.Int; } static isFloat(x) { return x && x.typeId === Type$1.Float; } static isBinary(x) { return x && x.typeId === Type$1.Binary; } static isUtf8(x) { return x && x.typeId === Type$1.Utf8; } static isBool(x) { return x && x.typeId === Type$1.Bool; } static isDecimal(x) { return x && x.typeId === Type$1.Decimal; } static isDate(x) { return x && x.typeId === Type$1.Date; } static isTime(x) { return x && x.typeId === Type$1.Time; } static isTimestamp(x) { return x && x.typeId === Type$1.Timestamp; } static isInterval(x) { return x && x.typeId === Type$1.Interval; } static isList(x) { return x && x.typeId === Type$1.List; } static isStruct(x) { return x && x.typeId === Type$1.Struct; } static isUnion(x) { return x && x.typeId === Type$1.Union; } static isFixedSizeBinary(x) { return x && x.typeId === Type$1.FixedSizeBinary; } static isFixedSizeList(x) { return x && x.typeId === Type$1.FixedSizeList; } static isMap(x) { return x && x.typeId === Type$1.Map; } static isDictionary(x) { return x && x.typeId === Type$1.Dictionary; } get typeId() { return Type$1.NONE; } compareTo(other) { return this === other; } } _Symbol$toStringTag = Symbol.toStringTag; class Int extends DataType { constructor(isSigned, bitWidth) { super(); _defineProperty(this, "isSigned", void 0); _defineProperty(this, "bitWidth", void 0); this.isSigned = isSigned; this.bitWidth = bitWidth; } get typeId() { return Type$1.Int; } get [_Symbol$toStringTag]() { return 'Int'; } toString() { return "".concat(this.isSigned ? 'I' : 'Ui', "nt").concat(this.bitWidth); } } class Int8 extends Int { constructor() { super(true, 8); } } class Int16 extends Int { constructor() { super(true, 16); } } class Int32 extends Int { constructor() { super(true, 32); } } class Uint8 extends Int { constructor() { super(false, 8); } } class Uint16 extends Int { constructor() { super(false, 16); } } class Uint32 extends Int { constructor() { super(false, 32); } } const Precision = { HALF: 16, SINGLE: 32, DOUBLE: 64 }; _Symbol$toStringTag2 = Symbol.toStringTag; class Float extends DataType { constructor(precision) { super(); _defineProperty(this, "precision", void 0); this.precision = precision; } get typeId() { return Type$1.Float; } get [_Symbol$toStringTag2]() { return 'Float'; } toString() { return "Float".concat(this.precision); } } class Float32 extends Float { constructor() { super(Precision.SINGLE); } } class Float64 extends Float { constructor() { super(Precision.DOUBLE); } } _Symbol$toStringTag7 = Symbol.toStringTag; class FixedSizeList extends DataType { constructor(listSize, child) { super(); _defineProperty(this, "listSize", void 0); _defineProperty(this, "children", void 0); this.listSize = listSize; this.children = [child]; } get typeId() { return Type$1.FixedSizeList; } get valueType() { return this.children[0].type; } get valueField() { return this.children[0]; } get [_Symbol$toStringTag7]() { return 'FixedSizeList'; } toString() { return "FixedSizeList[".concat(this.listSize, "]<").concat(this.valueType, ">"); } } function getArrowTypeFromTypedArray(array) { switch (array.constructor) { case Int8Array: return new Int8(); case Uint8Array: return new Uint8(); case Int16Array: return new Int16(); case Uint16Array: return new Uint16(); case Int32Array: return new Int32(); case Uint32Array: return new Uint32(); case Float32Array: return new Float32(); case Float64Array: return new Float64(); default: throw new Error('array type not supported'); } } function deduceMeshField(attributeName, attribute, optionalMetadata) { const type = getArrowTypeFromTypedArray(attribute.value); const metadata = optionalMetadata ? optionalMetadata : makeMeshAttributeMetadata(attribute); const field = new Field(attributeName, new FixedSizeList(attribute.size, new Field('value', type)), false, metadata); return field; } function makeMeshAttributeMetadata(attribute) { const result = new Map(); if ('byteOffset' in attribute) { result.set('byteOffset', attribute.byteOffset.toString(10)); } if ('byteStride' in attribute) { result.set('byteStride', attribute.byteStride.toString(10)); } if ('normalized' in attribute) { result.set('normalized', attribute.normalized.toString()); } return result; } function getDracoSchema(attributes, loaderData, indices) { const metadataMap = makeMetadata(loaderData.metadata); const fields = []; const namedLoaderDataAttributes = transformAttributesLoaderData(loaderData.attributes); for (const attributeName in attributes) { const attribute = attributes[attributeName]; const field = getArrowFieldFromAttribute(attributeName, attribute, namedLoaderDataAttributes[attributeName]); fields.push(field); } if (indices) { const indicesField = getArrowFieldFromAttribute('indices', indices); fields.push(indicesField); } return new Schema(fields, metadataMap); } function transformAttributesLoaderData(loaderData) { const result = {}; for (const key in loaderData) { const dracoAttribute = loaderData[key]; result[dracoAttribute.name || 'undefined'] = dracoAttribute; } return result; } function getArrowFieldFromAttribute(attributeName, attribute, loaderData) { const metadataMap = loaderData ? makeMetadata(loaderData.metadata) : undefined; const field = deduceMeshField(attributeName, attribute, metadataMap); return field; } function makeMetadata(metadata) { const metadataMap = new Map(); for (const key in metadata) { metadataMap.set("".concat(key, ".string"), JSON.stringify(metadata[key])); } return metadataMap; } const DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP = { POSITION: 'POSITION', NORMAL: 'NORMAL', COLOR: 'COLOR_0', TEX_COORD: 'TEXCOORD_0' }; const DRACO_DATA_TYPE_TO_TYPED_ARRAY_MAP = { 1: Int8Array, 2: Uint8Array, 3: Int16Array, 4: Uint16Array, 5: Int32Array, 6: Uint32Array, 9: Float32Array }; const INDEX_ITEM_SIZE = 4; class DracoParser { constructor(draco) { _defineProperty(this, "draco", void 0); _defineProperty(this, "decoder", void 0); _defineProperty(this, "metadataQuerier", void 0); this.draco = draco; this.decoder = new this.draco.Decoder(); this.metadataQuerier = new this.draco.MetadataQuerier(); } destroy() { this.draco.destroy(this.decoder); this.draco.destroy(this.metadataQuerier); } parseSync(arrayBuffer, options = {}) { const buffer = new this.draco.DecoderBuffer(); buffer.Init(new Int8Array(arrayBuffer), arrayBuffer.byteLength); this._disableAttributeTransforms(options); const geometry_type = this.decoder.GetEncodedGeometryType(buffer); const dracoGeometry = geometry_type === this.draco.TRIANGULAR_MESH ? new this.draco.Mesh() : new this.draco.PointCloud(); try { let dracoStatus; switch (geometry_type) { case this.draco.TRIANGULAR_MESH: dracoStatus = this.decoder.DecodeBufferToMesh(buffer, dracoGeometry); break; case this.draco.POINT_CLOUD: dracoStatus = this.decoder.DecodeBufferToPointCloud(buffer, dracoGeometry); break; default: throw new Error('DRACO: Unknown geometry type.'); } if (!dracoStatus.ok() || !dracoGeometry.ptr) { const message = "DRACO decompression failed: ".concat(dracoStatus.error_msg()); throw new Error(message); } const loaderData = this._getDracoLoaderData(dracoGeometry, geometry_type, options); const geometry = this._getMeshData(dracoGeometry, loaderData, options); const boundingBox = getMeshBoundingBox(geometry.attributes); const schema = getDracoSchema(geometry.attributes, loaderData, geometry.indices); const data = { loader: 'draco', loaderData, header: { vertexCount: dracoGeometry.num_points(), boundingBox }, ...geometry, schema }; return data; } finally { this.draco.destroy(buffer); if (dracoGeometry) { this.draco.destroy(dracoGeometry); } } } _getDracoLoaderData(dracoGeometry, geometry_type, options) { const metadata = this._getTopLevelMetadata(dracoGeometry); const attributes = this._getDracoAttributes(dracoGeometry, options); return { geometry_type, num_attributes: dracoGeometry.num_attributes(), num_points: dracoGeometry.num_points(), num_faces: dracoGeometry instanceof this.draco.Mesh ? dracoGeometry.num_faces() : 0, metadata, attributes }; } _getDracoAttributes(dracoGeometry, options) { const dracoAttributes = {}; for (let attributeId = 0; attributeId < dracoGeometry.num_attributes(); attributeId++) { const dracoAttribute = this.decoder.GetAttribute(dracoGeometry, attributeId); const metadata = this._getAttributeMetadata(dracoGeometry, attributeId); dracoAttributes[dracoAttribute.unique_id()] = { unique_id: dracoAttribute.unique_id(), attribute_type: dracoAttribute.attribute_type(), data_type: dracoAttribute.data_type(), num_components: dracoAttribute.num_components(), byte_offset: dracoAttribute.byte_offset(), byte_stride: dracoAttribute.byte_stride(), normalized: dracoAttribute.normalized(), attribute_index: attributeId, metadata }; const quantization = this._getQuantizationTransform(dracoAttribute, options); if (quantization) { dracoAttributes[dracoAttribute.unique_id()].quantization_transform = quantization; } const octahedron = this._getOctahedronTransform(dracoAttribute, options); if (octahedron) { dracoAttributes[dracoAttribute.unique_id()].octahedron_transform = octahedron; } } return dracoAttributes; } _getMeshData(dracoGeometry, loaderData, options) { const attributes = this._getMeshAttributes(loaderData, dracoGeometry, options); const positionAttribute = attributes.POSITION; if (!positionAttribute) { throw new Error('DRACO: No position attribute found.'); } if (dracoGeometry instanceof this.draco.Mesh) { switch (options.topology) { case 'triangle-strip': return { topology: 'triangle-strip', mode: 4, attributes, indices: { value: this._getTriangleStripIndices(dracoGeometry), size: 1 } }; case 'triangle-list': default: return { topology: 'triangle-list', mode: 5, attributes, indices: { value: this._getTriangleListIndices(dracoGeometry), size: 1 } }; } } return { topology: 'point-list', mode: 0, attributes }; } _getMeshAttributes(loaderData, dracoGeometry, options) { const attributes = {}; for (const loaderAttribute of Object.values(loaderData.attributes)) { const attributeName = this._deduceAttributeName(loaderAttribute, options); loaderAttribute.name = attributeName; const { value, size } = this._getAttributeValues(dracoGeometry, loaderAttribute); attributes[attributeName] = { value, size, byteOffset: loaderAttribute.byte_offset, byteStride: loaderAttribute.byte_stride, normalized: loaderAttribute.normalized }; } return attributes; } _getTriangleListIndices(dracoGeometry) { const numFaces = dracoGeometry.num_faces(); const numIndices = numFaces * 3; const byteLength = numIndices * INDEX_ITEM_SIZE; const ptr = this.draco._malloc(byteLength); try { this.decoder.GetTrianglesUInt32Array(dracoGeometry, byteLength, ptr); return new Uint32Array(this.draco.HEAPF32.buffer, ptr, numIndices).slice(); } finally { this.draco._free(ptr); } } _getTriangleStripIndices(dracoGeometry) { const dracoArray = new this.draco.DracoInt32Array(); try { this.decoder.GetTriangleStripsFromMesh(dracoGeometry, dracoArray); return getUint32Array(dracoArray); } finally { this.draco.destroy(dracoArray); } } _getAttributeValues(dracoGeometry, attribute) { const TypedArrayCtor = DRACO_DATA_TYPE_TO_TYPED_ARRAY_MAP[attribute.data_type]; const numComponents = attribute.num_components; const numPoints = dracoGeometry.num_points(); const numValues = numPoints * numComponents; const byteLength = numValues * TypedArrayCtor.BYTES_PER_ELEMENT; const dataType = getDracoDataType(this.draco, TypedArrayCtor); let value; const ptr = this.draco._malloc(byteLength); try { const dracoAttribute = this.decoder.GetAttribute(dracoGeometry, attribute.attribute_index); this.decoder.GetAttributeDataArrayForAllPoints(dracoGeometry, dracoAttribute, dataType, byteLength, ptr); value = new TypedArrayCtor(this.draco.HEAPF32.buffer, ptr, numValues).slice(); } finally { this.draco._free(ptr); } return { value, size: numComponents }; } _deduceAttributeName(attribute, options) { const uniqueId = attribute.unique_id; for (const [attributeName, attributeUniqueId] of Object.entries(options.extraAttributes || {})) { if (attributeUniqueId === uniqueId) { return attributeName; } } const thisAttributeType = attribute.attribute_type; for (const dracoAttributeConstant in DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP) { const attributeType = this.draco[dracoAttributeConstant]; if (attributeType === thisAttributeType) { return DRACO_TO_GLTF_ATTRIBUTE_NAME_MAP[dracoAttributeConstant]; } } const entryName = options.attributeNameEntry || 'name'; if (attribute.metadata[entryName]) { return attribute.metadata[entryName].string; } return "CUSTOM_ATTRIBUTE_".concat(uniqueId); } _getTopLevelMetadata(dracoGeometry) { const dracoMetadata = this.decoder.GetMetadata(dracoGeometry); return this._getDracoMetadata(dracoMetadata); } _getAttributeMetadata(dracoGeometry, attributeId) { const dracoMetadata = this.decoder.GetAttributeMetadata(dracoGeometry, attributeId); return this._getDracoMetadata(dracoMetadata); } _getDracoMetadata(dracoMetadata) { if (!dracoMetadata || !dracoMetadata.ptr) { return {}; } const result = {}; const numEntries = this.metadataQuerier.NumEntries(dracoMetadata); for (let entryIndex = 0; entryIndex < numEntries; entryIndex++) { const entryName = this.metadataQuerier.GetEntryName(dracoMetadata, entryIndex); result[entryName] = this._getDracoMetadataField(dracoMetadata, entryName); } return result; } _getDracoMetadataField(dracoMetadata, entryName) { const dracoArray = new this.draco.DracoInt32Array(); try { this.metadataQuerier.GetIntEntryArray(dracoMetadata, entryName, dracoArray); const intArray = getInt32Array(dracoArray); return { int: this.metadataQuerier.GetIntEntry(dracoMetadata, entryName), string: this.metadataQuerier.GetStringEntry(dracoMetadata, entryName), double: this.metadataQuerier.GetDoubleEntry(dracoMetadata, entryName), intArray }; } finally { this.draco.destroy(dracoArray); } } _disableAttributeTransforms(options) { const { quantizedAttributes = [], octahedronAttributes = [] } = options; const skipAttributes = [...quantizedAttributes, ...octahedronAttributes]; for (const dracoAttributeName of skipAttributes) { this.decoder.SkipAttributeTransform(this.draco[dracoAttributeName]); } } _getQuantizationTransform(dracoAttribute, options) { const { quantizedAttributes = [] } = options; const attribute_type = dracoAttribute.attribute_type(); const skip = quantizedAttributes.map(type => this.decoder[type]).includes(attribute_type); if (skip) { const transform = new this.draco.AttributeQuantizationTransform(); try { if (transform.InitFromAttribute(dracoAttribute)) { return { quantization_bits: transform.quantization_bits(), range: transform.range(), min_values: new Float32Array([1, 2, 3]).map(i => transform.min_value(i)) }; } } finally { this.draco.destroy(transform); } } return null; } _getOctahedronTransform(dracoAttribute, options) { const { octahedronAttributes = [] } = options; const attribute_type = dracoAttribute.attribute_type(); const octahedron = octahedronAttributes.map(type => this.decoder[type]).includes(attribute_type); if (octahedron) { const transform = new this.draco.AttributeQuantizationTransform(); try { if (transform.InitFromAttribute(dracoAttribute)) { return { quantization_bits: transform.quantization_bits() }; } } finally { this.draco.destroy(transform); } } return null; } } function getDracoDataType(draco, attributeType) { switch (attributeType) { case Float32Array: return draco.DT_FLOAT32; case Int8Array: return draco.DT_INT8; case Int16Array: return draco.DT_INT16; case Int32Array: return draco.DT_INT32; case Uint8Array: return draco.DT_UINT8; case Uint16Array: return draco.DT_UINT16; case Uint32Array: return draco.DT_UINT32; default: return draco.DT_INVALID; } } function getInt32Array(dracoArray) { const numValues = dracoArray.size(); const intArray = new Int32Array(numValues); for (let i = 0; i < numValues; i++) { intArray[i] = dracoArray.GetValue(i); } return intArray; } function getUint32Array(dracoArray) { const numValues = dracoArray.size(); const intArray = new Int32Array(numValues); for (let i = 0; i < numValues; i++) { intArray[i] = dracoArray.GetValue(i); } return intArray; } const DRACO_VERSION = '1.4.1'; const DRACO_JS_DECODER_URL = "https://www.gstatic.com/draco/versioned/decoders/".concat(DRACO_VERSION, "/draco_decoder.js"); const DRACO_WASM_WRAPPER_URL = "https://www.gstatic.com/draco/versioned/decoders/".concat(DRACO_VERSION, "/draco_wasm_wrapper.js"); const DRACO_WASM_DECODER_URL = "https://www.gstatic.com/draco/versioned/decoders/".concat(DRACO_VERSION, "/draco_decoder.wasm"); let loadDecoderPromise; async function loadDracoDecoderModule(options) { const modules = options.modules || {}; if (modules.draco3d) { loadDecoderPromise = loadDecoderPromise || modules.draco3d.createDecoderModule({}).then(draco => { return { draco }; }); } else { loadDecoderPromise = loadDecoderPromise || loadDracoDecoder(options); } return await loadDecoderPromise; } async function loadDracoDecoder(options) { let DracoDecoderModule; let wasmBinary; switch (options.draco && options.draco.decoderType) { case 'js': DracoDecoderModule = await loadLibrary(DRACO_JS_DECODER_URL, 'draco', options); break; case 'wasm': default: [DracoDecoderModule, wasmBinary] = await Promise.all([await loadLibrary(DRACO_WASM_WRAPPER_URL, 'draco', options), await loadLibrary(DRACO_WASM_DECODER_URL, 'draco', options)]); } DracoDecoderModule = DracoDecoderModule || globalThis.DracoDecoderModule; return await initializeDracoDecoder(DracoDecoderModule, wasmBinary); } function initializeDracoDecoder(DracoDecoderModule, wasmBinary) { const options = {}; if (wasmBinary) { options.wasmBinary = wasmBinary; } return new Promise(resolve => { DracoDecoderModule({ ...options, onModuleLoaded: draco => resolve({ draco }) }); }); } const DracoLoader = { ...DracoLoader$1, parse: parse$2 }; async function parse$2(arrayBuffer, options) { const { draco } = await loadDracoDecoderModule(options); const dracoParser = new DracoParser(draco); try { return dracoParser.parseSync(arrayBuffer, options === null || options === void 0 ? void 0 : options.draco); } finally { dracoParser.destroy(); } } const GL_PRIMITIVE_MODE = { POINTS: 0x0000, LINES: 0x0001, LINE_LOOP: 0x0002, LINE_STRIP: 0x0003, TRIANGLES: 0x0004, TRIANGLE_STRIP: 0x0005, TRIANGLE_FAN: 0x0006 }; const GL_TYPE = { BYTE: 5120, UNSIGNED_BYTE: 5121, SHORT: 5122, UNSIGNED_SHORT: 5123, INT: 5124, UNSIGNED_INT: 5125, FLOAT: 5126, DOUBLE: 5130 }; const GL$1 = { ...GL_PRIMITIVE_MODE, ...GL_TYPE }; const GL_TYPE_TO_ARRAY_TYPE = { [GL_TYPE.DOUBLE]: Float64Array, [GL_TYPE.FLOAT]: Float32Array, [GL_TYPE.UNSIGNED_SHORT]: Uint16Array, [GL_TYPE.UNSIGNED_INT]: Uint32Array, [GL_TYPE.UNSIGNED_BYTE]: Uint8Array, [GL_TYPE.BYTE]: Int8Array, [GL_TYPE.SHORT]: Int16Array, [GL_TYPE.INT]: Int32Array }; const NAME_TO_GL_TYPE = { DOUBLE: GL_TYPE.DOUBLE, FLOAT: GL_TYPE.FLOAT, UNSIGNED_SHORT: GL_TYPE.UNSIGNED_SHORT, UNSIGNED_INT: GL_TYPE.UNSIGNED_INT, UNSIGNED_BYTE: GL_TYPE.UNSIGNED_BYTE, BYTE: GL_TYPE.BYTE, SHORT: GL_TYPE.SHORT, INT: GL_TYPE.INT }; const ERR_TYPE_CONVERSION = 'Failed to convert GL type'; class GLType { static fromTypedArray(arrayOrType) { arrayOrType = ArrayBuffer.isView(arrayOrType) ? arrayOrType.constructor : arrayOrType; for (const glType in GL_TYPE_TO_ARRAY_TYPE) { const ArrayType = GL_TYPE_TO_ARRAY_TYPE[glType]; if (ArrayType === arrayOrType) { return glType; } } throw new Error(ERR_TYPE_CONVERSION); } static fromName(name) { const glType = NAME_TO_GL_TYPE[name]; if (!glType) { throw new Error(ERR_TYPE_CONVERSION); } return glType; } static getArrayType(glType) { switch (glType) { case GL_TYPE.UNSIGNED_SHORT_5_6_5: case GL_TYPE.UNSIGNED_SHORT_4_4_4_4: case GL_TYPE.UNSIGNED_SHORT_5_5_5_1: return Uint16Array; default: const ArrayType = GL_TYPE_TO_ARRAY_TYPE[glType]; if (!ArrayType) { throw new Error(ERR_TYPE_CONVERSION); } return ArrayType; } } static getByteSize(glType) { const ArrayType = GLType.getArrayType(glType); return ArrayType.BYTES_PER_ELEMENT; } static validate(glType) { return Boolean(GLType.getArrayType(glType)); } static createTypedArray(glType, buffer, byteOffset = 0, length) { if (length === undefined) { length = (buffer.byteLength - byteOffset) / GLType.getByteSize(glType); } const ArrayType = GLType.getArrayType(glType); return new ArrayType(buffer, byteOffset, length); } } function assert$2(condition, message) { if (!condition) { throw new Error("math.gl assertion failed. ".concat(message)); } } function decodeRGB565(rgb565, target = [0, 0, 0]) { const r5 = rgb565 >> 11 & 31; const g6 = rgb565 >> 5 & 63; const b5 = rgb565 & 31; target[0] = r5 << 3; target[1] = g6 << 2; target[2] = b5 << 3; return target; } new Vector2$1(); new Vector3$1(); new Vector2$1(); new Vector2$1(); function fromSNorm(value, rangeMaximum = 255) { return clamp(value, 0.0, rangeMaximum) / rangeMaximum * 2.0 - 1.0; } function signNotZero(value) { return value < 0.0 ? -1.0 : 1.0; } function octDecodeInRange(x, y, rangeMax, result) { assert$2(result); if (x < 0 || x > rangeMax || y < 0 || y > rangeMax) { throw new Error("x and y must be unsigned normalized integers between 0 and ".concat(rangeMax)); } result.x = fromSNorm(x, rangeMax); result.y = fromSNorm(y, rangeMax); result.z = 1.0 - (Math.abs(result.x) + Math.abs(result.y)); if (result.z < 0.0) { const oldVX = result.x; result.x = (1.0 - Math.abs(result.y)) * signNotZero(oldVX); result.y = (1.0 - Math.abs(oldVX)) * signNotZero(result.y); } return result.normalize(); } function octDecode(x, y, result) { return octDecodeInRange(x, y, 255, result); } class Tile3DFeatureTable { constructor(featureTableJson, featureTableBinary) { _defineProperty(this, "json", void 0); _defineProperty(this, "buffer", void 0); _defineProperty(this, "featuresLength", 0); _defineProperty(this, "_cachedTypedArrays", {}); this.json = featureTableJson; this.buffer = featureTableBinary; } getExtension(extensionName) { return this.json.extensions && this.json.extensions[extensionName]; } hasProperty(propertyName) { return Boolean(this.json[propertyName]); } getGlobalProperty(propertyName, componentType = GL$1.UNSIGNED_INT, componentLength = 1) { const jsonValue = this.json[propertyName]; if (jsonValue && Number.isFinite(jsonValue.byteOffset)) { return this._getTypedArrayFromBinary(propertyName, componentType, componentLength, 1, jsonValue.byteOffset); } return jsonValue; } getPropertyArray(propertyName, componentType, componentLength) { const jsonValue = this.json[propertyName]; if (jsonValue && Number.isFinite(jsonValue.byteOffset)) { if ('componentType' in jsonValue) { componentType = GLType.fromName(jsonValue.componentType); } return this._getTypedArrayFromBinary(propertyName, componentType, componentLength, this.featuresLength, jsonValue.byteOffset); } return this._getTypedArrayFromArray(propertyName, componentType, jsonValue); } getProperty(propertyName, componentType, componentLength, featureId, result) { const jsonValue = this.json[propertyName]; if (!jsonValue) { return jsonValue; } const typedArray = this.getPropertyArray(propertyName, componentType, componentLength); if (componentLength === 1) { return typedArray[featureId]; } for (let i = 0; i < componentLength; ++i) { result[i] = typedArray[componentLength * featureId + i]; } return result; } _getTypedArrayFromBinary(propertyName, componentType, componentLength, count, byteOffset) { const cachedTypedArrays = this._cachedTypedArrays; let typedArray = cachedTypedArrays[propertyName]; if (!typedArray) { typedArray = GLType.createTypedArray(componentType, this.buffer.buffer, this.buffer.byteOffset + byteOffset, count * componentLength); cachedTypedArrays[propertyName] = typedArray; } return typedArray; } _getTypedArrayFromArray(propertyName, componentType, array) { const cachedTypedArrays = this._cachedTypedArrays; let typedArray = cachedTypedArrays[propertyName]; if (!typedArray) { typedArray = GLType.createTypedArray(componentType, array); cachedTypedArrays[propertyName] = typedArray; } return typedArray; } } const COMPONENTS_PER_ATTRIBUTE = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 }; const UNPACKER = { SCALAR: (values, i) => values[i], VEC2: (values, i) => [values[2 * i + 0], values[2 * i + 1]], VEC3: (values, i) => [values[3 * i + 0], values[3 * i + 1], values[3 * i + 2]], VEC4: (values, i) => [values[4 * i + 0], values[4 * i + 1], values[4 * i + 2], values[4 * i + 3]], MAT2: (values, i) => [values[4 * i + 0], values[4 * i + 1], values[4 * i + 2], values[4 * i + 3]], MAT3: (values, i) => [values[9 * i + 0], values[9 * i + 1], values[9 * i + 2], values[9 * i + 3], values[9 * i + 4], values[9 * i + 5], values[9 * i + 6], values[9 * i + 7], values[9 * i + 8]], MAT4: (values, i) => [values[16 * i + 0], values[16 * i + 1], values[16 * i + 2], values[16 * i + 3], values[16 * i + 4], values[16 * i + 5], values[16 * i + 6], values[16 * i + 7], values[16 * i + 8], values[16 * i + 9], values[16 * i + 10], values[16 * i + 11], values[16 * i + 12], values[16 * i + 13], values[16 * i + 14], values[16 * i + 15]] }; const PACKER = { SCALAR: (x, values, i) => { values[i] = x; }, VEC2: (x, values, i) => { values[2 * i + 0] = x[0]; values[2 * i + 1] = x[1]; }, VEC3: (x, values, i) => { values[3 * i + 0] = x[0]; values[3 * i + 1] = x[1]; values[3 * i + 2] = x[2]; }, VEC4: (x, values, i) => { values[4 * i + 0] = x[0]; values[4 * i + 1] = x[1]; values[4 * i + 2] = x[2]; values[4 * i + 3] = x[3]; }, MAT2: (x, values, i) => { values[4 * i + 0] = x[0]; values[4 * i + 1] = x[1]; values[4 * i + 2] = x[2]; values[4 * i + 3] = x[3]; }, MAT3: (x, values, i) => { values[9 * i + 0] = x[0]; values[9 * i + 1] = x[1]; values[9 * i + 2] = x[2]; values[9 * i + 3] = x[3]; values[9 * i + 4] = x[4]; values[9 * i + 5] = x[5]; values[9 * i + 6] = x[6]; values[9 * i + 7] = x[7]; values[9 * i + 8] = x[8]; values[9 * i + 9] = x[9]; }, MAT4: (x, values, i) => { values[16 * i + 0] = x[0]; values[16 * i + 1] = x[1]; values[16 * i + 2] = x[2]; values[16 * i + 3] = x[3]; values[16 * i + 4] = x[4]; values[16 * i + 5] = x[5]; values[16 * i + 6] = x[6]; values[16 * i + 7] = x[7]; values[16 * i + 8] = x[8]; values[16 * i + 9] = x[9]; values[16 * i + 10] = x[10]; values[16 * i + 11] = x[11]; values[16 * i + 12] = x[12]; values[16 * i + 13] = x[13]; values[16 * i + 14] = x[14]; values[16 * i + 15] = x[15]; } }; function createTypedArrayFromAccessor(tile3DAccessor, buffer, byteOffset, length) { const { componentType } = tile3DAccessor; assert$7(tile3DAccessor.componentType); const type = typeof componentType === 'string' ? GLType.fromName(componentType) : componentType; const size = COMPONENTS_PER_ATTRIBUTE[tile3DAccessor.type]; const unpacker = UNPACKER[tile3DAccessor.type]; const packer = PACKER[tile3DAccessor.type]; byteOffset += tile3DAccessor.byteOffset; const values = GLType.createTypedArray(type, buffer, byteOffset, size * length); return { values, type, size, unpacker, packer }; } const defined$1 = x => x !== undefined; function initializeHierarchy(batchTable, jsonHeader, binaryBody) { if (!jsonHeader) { return null; } let hierarchy = batchTable.getExtension('3DTILES_batch_table_hierarchy'); const legacyHierarchy = jsonHeader.HIERARCHY; if (legacyHierarchy) { console.warn('3D Tile Parser: HIERARCHY is deprecated. Use 3DTILES_batch_table_hierarchy.'); jsonHeader.extensions = jsonHeader.extensions || {}; jsonHeader.extensions['3DTILES_batch_table_hierarchy'] = legacyHierarchy; hierarchy = legacyHierarchy; } if (!hierarchy) { return null; } return initializeHierarchyValues(hierarchy, binaryBody); } function initializeHierarchyValues(hierarchyJson, binaryBody) { let i; let classId; let binaryAccessor; const instancesLength = hierarchyJson.instancesLength; const classes = hierarchyJson.classes; let classIds = hierarchyJson.classIds; let parentCounts = hierarchyJson.parentCounts; let parentIds = hierarchyJson.parentIds; let parentIdsLength = instancesLength; if (defined$1(classIds.byteOffset)) { classIds.componentType = defaultValue(classIds.componentType, GL.UNSIGNED_SHORT); classIds.type = AttributeType.SCALAR; binaryAccessor = getBinaryAccessor(classIds); classIds = binaryAccessor.createArrayBufferView(binaryBody.buffer, binaryBody.byteOffset + classIds.byteOffset, instancesLength); } let parentIndexes; if (defined$1(parentCounts)) { if (defined$1(parentCounts.byteOffset)) { parentCounts.componentType = defaultValue(parentCounts.componentType, GL.UNSIGNED_SHORT); parentCounts.type = AttributeType.SCALAR; binaryAccessor = getBinaryAccessor(parentCounts); parentCounts = binaryAccessor.createArrayBufferView(binaryBody.buffer, binaryBody.byteOffset + parentCounts.byteOffset, instancesLength); } parentIndexes = new Uint16Array(instancesLength); parentIdsLength = 0; for (i = 0; i < instancesLength; ++i) { parentIndexes[i] = parentIdsLength; parentIdsLength += parentCounts[i]; } } if (defined$1(parentIds) && defined$1(parentIds.byteOffset)) { parentIds.componentType = defaultValue(parentIds.componentType, GL.UNSIGNED_SHORT); parentIds.type = AttributeType.SCALAR; binaryAccessor = getBinaryAccessor(parentIds); parentIds = binaryAccessor.createArrayBufferView(binaryBody.buffer, binaryBody.byteOffset + parentIds.byteOffset, parentIdsLength); } const classesLength = classes.length; for (i = 0; i < classesLength; ++i) { const classInstancesLength = classes[i].length; const properties = classes[i].instances; const binaryProperties = getBinaryProperties(classInstancesLength, properties, binaryBody); classes[i].instances = combine(binaryProperties, properties); } const classCounts = new Array(classesLength).fill(0); const classIndexes = new Uint16Array(instancesLength); for (i = 0; i < instancesLength; ++i) { classId = classIds[i]; classIndexes[i] = classCounts[classId]; ++classCounts[classId]; } const hierarchy = { classes, classIds, classIndexes, parentCounts, parentIndexes, parentIds }; validateHierarchy(hierarchy); return hierarchy; } function traverseHierarchy(hierarchy, instanceIndex, endConditionCallback) { if (!hierarchy) { return; } const parentCounts = hierarchy.parentCounts; const parentIds = hierarchy.parentIds; if (parentIds) { return endConditionCallback(hierarchy, instanceIndex); } if (parentCounts > 0) { return traverseHierarchyMultipleParents(hierarchy, instanceIndex, endConditionCallback); } return traverseHierarchySingleParent(hierarchy, instanceIndex, endConditionCallback); } function traverseHierarchyMultipleParents(hierarchy, instanceIndex, endConditionCallback) { const classIds = hierarchy.classIds; const parentCounts = hierarchy.parentCounts; const parentIds = hierarchy.parentIds; const parentIndexes = hierarchy.parentIndexes; const instancesLength = classIds.length; const visited = scratchVisited; visited.length = Math.max(visited.length, instancesLength); const visitedMarker = ++marker; const stack = scratchStack; stack.length = 0; stack.push(instanceIndex); while (stack.length > 0) { instanceIndex = stack.pop(); if (visited[instanceIndex] === visitedMarker) { continue; } visited[instanceIndex] = visitedMarker; const result = endConditionCallback(hierarchy, instanceIndex); if (defined$1(result)) { return result; } const parentCount = parentCounts[instanceIndex]; const parentIndex = parentIndexes[instanceIndex]; for (let i = 0; i < parentCount; ++i) { const parentId = parentIds[parentIndex + i]; if (parentId !== instanceIndex) { stack.push(parentId); } } } return null; } function traverseHierarchySingleParent(hierarchy, instanceIndex, endConditionCallback) { let hasParent = true; while (hasParent) { const result = endConditionCallback(hierarchy, instanceIndex); if (defined$1(result)) { return result; } const parentId = hierarchy.parentIds[instanceIndex]; hasParent = parentId !== instanceIndex; instanceIndex = parentId; } throw new Error('traverseHierarchySingleParent'); } function validateHierarchy(hierarchy) { const classIds = hierarchy.classIds; const instancesLength = classIds.length; for (let i = 0; i < instancesLength; ++i) { validateInstance(hierarchy, i, stack); } } function validateInstance(hierarchy, instanceIndex, stack) { const parentCounts = hierarchy.parentCounts; const parentIds = hierarchy.parentIds; const parentIndexes = hierarchy.parentIndexes; const classIds = hierarchy.classIds; const instancesLength = classIds.length; if (!defined$1(parentIds)) { return; } assert(instanceIndex < instancesLength, "Parent index ".concat(instanceIndex, " exceeds the total number of instances: ").concat(instancesLength)); assert(stack.indexOf(instanceIndex) === -1, 'Circular dependency detected in the batch table hierarchy.'); stack.push(instanceIndex); const parentCount = defined$1(parentCounts) ? parentCounts[instanceIndex] : 1; const parentIndex = defined$1(parentCounts) ? parentIndexes[instanceIndex] : instanceIndex; for (let i = 0; i < parentCount; ++i) { const parentId = parentIds[parentIndex + i]; if (parentId !== instanceIndex) { validateInstance(hierarchy, parentId, stack); } } stack.pop(instanceIndex); } function defined(x) { return x !== undefined && x !== null; } const clone = (x, y) => x; const IGNORED_PROPERTY_FIELDS = { HIERARCHY: true, extensions: true, extras: true }; class Tile3DBatchTableParser { constructor(json, binary, featureCount, options = {}) { var _this$json; _defineProperty(this, "json", void 0); _defineProperty(this, "binary", void 0); _defineProperty(this, "featureCount", void 0); _defineProperty(this, "_extensions", void 0); _defineProperty(this, "_properties", void 0); _defineProperty(this, "_binaryProperties", void 0); _defineProperty(this, "_hierarchy", void 0); assert$7(featureCount >= 0); this.json = json || {}; this.binary = binary; this.featureCount = featureCount; this._extensions = ((_this$json = this.json) === null || _this$json === void 0 ? void 0 : _this$json.extensions) || {}; this._properties = {}; for (const propertyName in this.json) { if (!IGNORED_PROPERTY_FIELDS[propertyName]) { this._properties[propertyName] = this.json[propertyName]; } } this._binaryProperties = this._initializeBinaryProperties(); if (options['3DTILES_batch_table_hierarchy']) { this._hierarchy = initializeHierarchy(this, this.json, this.binary); } } getExtension(extensionName) { return this.json && this.json.extensions && this.json.extensions[extensionName]; } memorySizeInBytes() { return 0; } isClass(batchId, className) { this._checkBatchId(batchId); assert$7(typeof className === 'string', className); if (this._hierarchy) { const result = traverseHierarchy(this._hierarchy, batchId, (hierarchy, instanceIndex) => { const classId = hierarchy.classIds[instanceIndex]; const instanceClass = hierarchy.classes[classId]; return instanceClass.name === className; }); return defined(result); } return false; } isExactClass(batchId, className) { assert$7(typeof className === 'string', className); return this.getExactClassName(batchId) === className; } getExactClassName(batchId) { this._checkBatchId(batchId); if (this._hierarchy) { const classId = this._hierarchy.classIds[batchId]; const instanceClass = this._hierarchy.classes[classId]; return instanceClass.name; } return undefined; } hasProperty(batchId, name) { this._checkBatchId(batchId); assert$7(typeof name === 'string', name); return defined(this._properties[name]) || this._hasPropertyInHierarchy(batchId, name); } getPropertyNames(batchId, results) { this._checkBatchId(batchId); results = defined(results) ? results : []; results.length = 0; const propertyNames = Object.keys(this._properties); results.push(...propertyNames); if (this._hierarchy) { this._getPropertyNamesInHierarchy(batchId, results); } return results; } getProperty(batchId, name) { this._checkBatchId(batchId); assert$7(typeof name === 'string', name); if (this._binaryProperties) { const binaryProperty = this._binaryProperties[name]; if (defined(binaryProperty)) { return this._getBinaryProperty(binaryProperty, batchId); } } const propertyValues = this._properties[name]; if (defined(propertyValues)) { return clone(propertyValues[batchId]); } if (this._hierarchy) { const hierarchyProperty = this._getHierarchyProperty(batchId, name); if (defined(hierarchyProperty)) { return hierarchyProperty; } } return undefined; } setProperty(batchId, name, value) { const featureCount = this.featureCount; this._checkBatchId(batchId); assert$7(typeof name === 'string', name); if (this._binaryProperties) { const binaryProperty = this._binaryProperties[name]; if (binaryProperty) { this._setBinaryProperty(binaryProperty, batchId, value); return; } } if (this._hierarchy) { if (this._setHierarchyProperty(this, batchId, name, value)) { return; } } let propertyValues = this._properties[name]; if (!defined(propertyValues)) { this._properties[name] = new Array(featureCount); propertyValues = this._properties[name]; } propertyValues[batchId] = clone(value); } _checkBatchId(batchId) { const valid = batchId >= 0 && batchId < this.featureCount; if (!valid) { throw new Error('batchId not in range [0, featureCount - 1].'); } } _getBinaryProperty(binaryProperty, index) { return binaryProperty.unpack(binaryProperty.typedArray, index); } _setBinaryProperty(binaryProperty, index, value) { binaryProperty.pack(value, binaryProperty.typedArray, index); } _initializeBinaryProperties() { let binaryProperties = null; for (const name in this._properties) { const property = this._properties[name]; const binaryProperty = this._initializeBinaryProperty(name, property); if (binaryProperty) { binaryProperties = binaryProperties || {}; binaryProperties[name] = binaryProperty; } } return binaryProperties; } _initializeBinaryProperty(name, property) { if ('byteOffset' in property) { const tile3DAccessor = property; assert$7(this.binary, "Property ".concat(name, " requires a batch table binary.")); assert$7(tile3DAccessor.type, "Property ".concat(name, " requires a type.")); const accessor = createTypedArrayFromAccessor(tile3DAccessor, this.binary.buffer, this.binary.byteOffset | 0, this.featureCount); return { typedArray: accessor.values, componentCount: accessor.size, unpack: accessor.unpacker, pack: accessor.packer }; } return null; } _hasPropertyInHierarchy(batchId, name) { if (!this._hierarchy) { return false; } const result = traverseHierarchy(this._hierarchy, batchId, (hierarchy, instanceIndex) => { const classId = hierarchy.classIds[instanceIndex]; const instances = hierarchy.classes[classId].instances; return defined(instances[name]); }); return defined(result); } _getPropertyNamesInHierarchy(batchId, results) { traverseHierarchy(this._hierarchy, batchId, (hierarchy, instanceIndex) => { const classId = hierarchy.classIds[instanceIndex]; const instances = hierarchy.classes[classId].instances; for (const name in instances) { if (instances.hasOwnProperty(name)) { if (results.indexOf(name) === -1) { results.push(name); } } } }); } _getHierarchyProperty(batchId, name) { return traverseHierarchy(this._hierarchy, batchId, (hierarchy, instanceIndex) => { const classId = hierarchy.classIds[instanceIndex]; const instanceClass = hierarchy.classes[classId]; const indexInClass = hierarchy.classIndexes[instanceIndex]; const propertyValues = instanceClass.instances[name]; if (defined(propertyValues)) { if (defined(propertyValues.typedArray)) { return this._getBinaryProperty(propertyValues, indexInClass); } return clone(propertyValues[indexInClass]); } return null; }); } _setHierarchyProperty(batchTable, batchId, name, value) { const result = traverseHierarchy(this._hierarchy, batchId, (hierarchy, instanceIndex) => { const classId = hierarchy.classIds[instanceIndex]; const instanceClass = hierarchy.classes[classId]; const indexInClass = hierarchy.classIndexes[instanceIndex]; const propertyValues = instanceClass.instances[name]; if (defined(propertyValues)) { assert$7(instanceIndex === batchId, "Inherited property \"".concat(name, "\" is read-only.")); if (defined(propertyValues.typedArray)) { this._setBinaryProperty(propertyValues, indexInClass, value); } else { propertyValues[indexInClass] = clone(value); } return true; } return false; }); return defined(result); } } const SIZEOF_UINT32$1 = 4; function parse3DTileHeaderSync(tile, arrayBuffer, byteOffset = 0) { const view = new DataView(arrayBuffer); tile.magic = view.getUint32(byteOffset, true); byteOffset += SIZEOF_UINT32$1; tile.version = view.getUint32(byteOffset, true); byteOffset += SIZEOF_UINT32$1; tile.byteLength = view.getUint32(byteOffset, true); byteOffset += SIZEOF_UINT32$1; if (tile.version !== 1) { throw new Error("3D Tile Version ".concat(tile.version, " not supported")); } return byteOffset; } const SIZEOF_UINT32 = 4; const DEPRECATION_WARNING = 'b3dm tile in legacy format.'; function parse3DTileTablesHeaderSync(tile, arrayBuffer, byteOffset) { const view = new DataView(arrayBuffer); let batchLength; tile.header = tile.header || {}; let featureTableJsonByteLength = view.getUint32(byteOffset, true); byteOffset += SIZEOF_UINT32; let featureTableBinaryByteLength = view.getUint32(byteOffset, true); byteOffset += SIZEOF_UINT32; let batchTableJsonByteLength = view.getUint32(byteOffset, true); byteOffset += SIZEOF_UINT32; let batchTableBinaryByteLength = view.getUint32(byteOffset, true); byteOffset += SIZEOF_UINT32; if (batchTableJsonByteLength >= 570425344) { byteOffset -= SIZEOF_UINT32 * 2; batchLength = featureTableJsonByteLength; batchTableJsonByteLength = featureTableBinaryByteLength; batchTableBinaryByteLength = 0; featureTableJsonByteLength = 0; featureTableBinaryByteLength = 0; console.warn(DEPRECATION_WARNING); } else if (batchTableBinaryByteLength >= 570425344) { byteOffset -= SIZEOF_UINT32; batchLength = batchTableJsonByteLength; batchTableJsonByteLength = featureTableJsonByteLength; batchTableBinaryByteLength = featureTableBinaryByteLength; featureTableJsonByteLength = 0; featureTableBinaryByteLength = 0; console.warn(DEPRECATION_WARNING); } tile.header.featureTableJsonByteLength = featureTableJsonByteLength; tile.header.featureTableBinaryByteLength = featureTableBinaryByteLength; tile.header.batchTableJsonByteLength = batchTableJsonByteLength; tile.header.batchTableBinaryByteLength = batchTableBinaryByteLength; tile.header.batchLength = batchLength; return byteOffset; } function parse3DTileTablesSync(tile, arrayBuffer, byteOffset, options) { byteOffset = parse3DTileFeatureTable(tile, arrayBuffer, byteOffset); byteOffset = parse3DTileBatchTable(tile, arrayBuffer, byteOffset); return byteOffset; } function parse3DTileFeatureTable(tile, arrayBuffer, byteOffset, options) { const { featureTableJsonByteLength, featureTableBinaryByteLength, batchLength } = tile.header; tile.featureTableJson = { BATCH_LENGTH: batchLength || 0 }; if (featureTableJsonByteLength > 0) { const featureTableString = getStringFromArrayBuffer(arrayBuffer, byteOffset, featureTableJsonByteLength); tile.featureTableJson = JSON.parse(featureTableString); } byteOffset += featureTableJsonByteLength; tile.featureTableBinary = new Uint8Array(arrayBuffer, byteOffset, featureTableBinaryByteLength); byteOffset += featureTableBinaryByteLength; return byteOffset; } function parse3DTileBatchTable(tile, arrayBuffer, byteOffset, options) { const { batchTableJsonByteLength, batchTableBinaryByteLength } = tile.header; if (batchTableJsonByteLength > 0) { const batchTableString = getStringFromArrayBuffer(arrayBuffer, byteOffset, batchTableJsonByteLength); tile.batchTableJson = JSON.parse(batchTableString); byteOffset += batchTableJsonByteLength; if (batchTableBinaryByteLength > 0) { tile.batchTableBinary = new Uint8Array(arrayBuffer, byteOffset, batchTableBinaryByteLength); tile.batchTableBinary = new Uint8Array(tile.batchTableBinary); byteOffset += batchTableBinaryByteLength; } } return byteOffset; } function normalize3DTileColorAttribute(tile, colors, batchTable) { if (!colors && (!tile || !tile.batchIds || !batchTable)) { return null; } const { batchIds, isRGB565, pointCount } = tile; if (batchIds && batchTable) { const colorArray = new Uint8ClampedArray(pointCount * 3); for (let i = 0; i < pointCount; i++) { const batchId = batchIds[i]; const dimensions = batchTable.getProperty(batchId, 'dimensions'); const color = dimensions.map(d => d * 255); colorArray[i * 3] = color[0]; colorArray[i * 3 + 1] = color[1]; colorArray[i * 3 + 2] = color[2]; } return { type: GL$1.UNSIGNED_BYTE, value: colorArray, size: 3, normalized: true }; } if (isRGB565) { const colorArray = new Uint8ClampedArray(pointCount * 3); for (let i = 0; i < pointCount; i++) { const color = decodeRGB565(colors[i]); colorArray[i * 3] = color[0]; colorArray[i * 3 + 1] = color[1]; colorArray[i * 3 + 2] = color[2]; } return { type: GL$1.UNSIGNED_BYTE, value: colorArray, size: 3, normalized: true }; } if (colors && colors.length === pointCount * 3) { return { type: GL$1.UNSIGNED_BYTE, value: colors, size: 3, normalized: true }; } return { type: GL$1.UNSIGNED_BYTE, value: colors, size: 4, normalized: true }; } const scratchNormal = new Vector3$1(); function normalize3DTileNormalAttribute(tile, normals) { if (!normals) { return null; } if (tile.isOctEncoded16P) { const decodedArray = new Float32Array(tile.pointsLength * 3); for (let i = 0; i < tile.pointsLength; i++) { octDecode(normals[i * 2], normals[i * 2 + 1], scratchNormal); scratchNormal.toArray(decodedArray, i * 3); } return { type: GL$1.FLOAT, size: 2, value: decodedArray }; } return { type: GL$1.FLOAT, size: 2, value: normals }; } function normalize3DTilePositionAttribute(tile, positions, options) { if (!tile.isQuantized) { return positions; } if (options['3d-tiles'] && options['3d-tiles'].decodeQuantizedPositions) { tile.isQuantized = false; return decodeQuantizedPositions(tile, positions); } return { type: GL$1.UNSIGNED_SHORT, value: positions, size: 3, normalized: true }; } function decodeQuantizedPositions(tile, positions) { const scratchPosition = new Vector3$1(); const decodedArray = new Float32Array(tile.pointCount * 3); for (let i = 0; i < tile.pointCount; i++) { scratchPosition.set(positions[i * 3], positions[i * 3 + 1], positions[i * 3 + 2]).scale(1 / tile.quantizedRange).multiply(tile.quantizedVolumeScale).add(tile.quantizedVolumeOffset).toArray(decodedArray, i * 3); } return decodedArray; } async function parsePointCloud3DTile(tile, arrayBuffer, byteOffset, options, context) { byteOffset = parse3DTileHeaderSync(tile, arrayBuffer, byteOffset); byteOffset = parse3DTileTablesHeaderSync(tile, arrayBuffer, byteOffset); byteOffset = parse3DTileTablesSync(tile, arrayBuffer, byteOffset); initializeTile(tile); const { featureTable, batchTable } = parsePointCloudTables(tile); await parseDraco(tile, featureTable, batchTable, options, context); parsePositions(tile, featureTable, options); parseColors(tile, featureTable, batchTable); parseNormals(tile, featureTable); return byteOffset; } function initializeTile(tile) { tile.attributes = { positions: null, colors: null, normals: null, batchIds: null }; tile.isQuantized = false; tile.isTranslucent = false; tile.isRGB565 = false; tile.isOctEncoded16P = false; } function parsePointCloudTables(tile) { const featureTable = new Tile3DFeatureTable(tile.featureTableJson, tile.featureTableBinary); const pointsLength = featureTable.getGlobalProperty('POINTS_LENGTH'); if (!Number.isFinite(pointsLength)) { throw new Error('POINTS_LENGTH must be defined'); } featureTable.featuresLength = pointsLength; tile.featuresLength = pointsLength; tile.pointsLength = pointsLength; tile.pointCount = pointsLength; tile.rtcCenter = featureTable.getGlobalProperty('RTC_CENTER', GL$1.FLOAT, 3); const batchTable = parseBatchIds(tile, featureTable); return { featureTable, batchTable }; } function parsePositions(tile, featureTable, options) { if (!tile.attributes.positions) { if (featureTable.hasProperty('POSITION')) { tile.attributes.positions = featureTable.getPropertyArray('POSITION', GL$1.FLOAT, 3); } else if (featureTable.hasProperty('POSITION_QUANTIZED')) { const positions = featureTable.getPropertyArray('POSITION_QUANTIZED', GL$1.UNSIGNED_SHORT, 3); tile.isQuantized = true; tile.quantizedRange = (1 << 16) - 1; tile.quantizedVolumeScale = featureTable.getGlobalProperty('QUANTIZED_VOLUME_SCALE', GL$1.FLOAT, 3); if (!tile.quantizedVolumeScale) { throw new Error('QUANTIZED_VOLUME_SCALE must be defined for quantized positions.'); } tile.quantizedVolumeOffset = featureTable.getGlobalProperty('QUANTIZED_VOLUME_OFFSET', GL$1.FLOAT, 3); if (!tile.quantizedVolumeOffset) { throw new Error('QUANTIZED_VOLUME_OFFSET must be defined for quantized positions.'); } tile.attributes.positions = normalize3DTilePositionAttribute(tile, positions, options); } } if (!tile.attributes.positions) { throw new Error('Either POSITION or POSITION_QUANTIZED must be defined.'); } } function parseColors(tile, featureTable, batchTable) { if (!tile.attributes.colors) { let colors = null; if (featureTable.hasProperty('RGBA')) { colors = featureTable.getPropertyArray('RGBA', GL$1.UNSIGNED_BYTE, 4); tile.isTranslucent = true; } else if (featureTable.hasProperty('RGB')) { colors = featureTable.getPropertyArray('RGB', GL$1.UNSIGNED_BYTE, 3); } else if (featureTable.hasProperty('RGB565')) { colors = featureTable.getPropertyArray('RGB565', GL$1.UNSIGNED_SHORT, 1); tile.isRGB565 = true; } tile.attributes.colors = normalize3DTileColorAttribute(tile, colors, batchTable); } if (featureTable.hasProperty('CONSTANT_RGBA')) { tile.constantRGBA = featureTable.getGlobalProperty('CONSTANT_RGBA', GL$1.UNSIGNED_BYTE, 4); } } function parseNormals(tile, featureTable) { if (!tile.attributes.normals) { let normals = null; if (featureTable.hasProperty('NORMAL')) { normals = featureTable.getPropertyArray('NORMAL', GL$1.FLOAT, 3); } else if (featureTable.hasProperty('NORMAL_OCT16P')) { normals = featureTable.getPropertyArray('NORMAL_OCT16P', GL$1.UNSIGNED_BYTE, 2); tile.isOctEncoded16P = true; } tile.attributes.normals = normalize3DTileNormalAttribute(tile, normals); } } function parseBatchIds(tile, featureTable) { let batchTable = null; if (!tile.batchIds && featureTable.hasProperty('BATCH_ID')) { tile.batchIds = featureTable.getPropertyArray('BATCH_ID', GL$1.UNSIGNED_SHORT, 1); if (tile.batchIds) { const batchFeatureLength = featureTable.getGlobalProperty('BATCH_LENGTH'); if (!batchFeatureLength) { throw new Error('Global property: BATCH_LENGTH must be defined when BATCH_ID is defined.'); } const { batchTableJson, batchTableBinary } = tile; batchTable = new Tile3DBatchTableParser(batchTableJson, batchTableBinary, batchFeatureLength); } } return batchTable; } async function parseDraco(tile, featureTable, batchTable, options, context) { let dracoBuffer; let dracoFeatureTableProperties; let dracoBatchTableProperties; const batchTableDraco = tile.batchTableJson && tile.batchTableJson.extensions && tile.batchTableJson.extensions['3DTILES_draco_point_compression']; if (batchTableDraco) { dracoBatchTableProperties = batchTableDraco.properties; } const featureTableDraco = featureTable.getExtension('3DTILES_draco_point_compression'); if (featureTableDraco) { dracoFeatureTableProperties = featureTableDraco.properties; const dracoByteOffset = featureTableDraco.byteOffset; const dracoByteLength = featureTableDraco.byteLength; if (!dracoFeatureTableProperties || !Number.isFinite(dracoByteOffset) || !dracoByteLength) { throw new Error('Draco properties, byteOffset, and byteLength must be defined'); } dracoBuffer = tile.featureTableBinary.slice(dracoByteOffset, dracoByteOffset + dracoByteLength); tile.hasPositions = Number.isFinite(dracoFeatureTableProperties.POSITION); tile.hasColors = Number.isFinite(dracoFeatureTableProperties.RGB) || Number.isFinite(dracoFeatureTableProperties.RGBA); tile.hasNormals = Number.isFinite(dracoFeatureTableProperties.NORMAL); tile.hasBatchIds = Number.isFinite(dracoFeatureTableProperties.BATCH_ID); tile.isTranslucent = Number.isFinite(dracoFeatureTableProperties.RGBA); } if (!dracoBuffer) { return true; } const dracoData = { buffer: dracoBuffer, properties: { ...dracoFeatureTableProperties, ...dracoBatchTableProperties }, featureTableProperties: dracoFeatureTableProperties, batchTableProperties: dracoBatchTableProperties, dequantizeInShader: false }; return await loadDraco(tile, dracoData, options, context); } async function loadDraco(tile, dracoData, options, context) { const { parse } = context; const dracoOptions = { ...options, draco: { ...options.draco, extraAttributes: dracoData.batchTableProperties || {} } }; delete dracoOptions['3d-tiles']; const data = await parse(dracoData.buffer, DracoLoader, dracoOptions); const decodedPositions = data.attributes.POSITION && data.attributes.POSITION.value; const decodedColors = data.attributes.COLOR_0 && data.attributes.COLOR_0.value; const decodedNormals = data.attributes.NORMAL && data.attributes.NORMAL.value; const decodedBatchIds = data.attributes.BATCH_ID && data.attributes.BATCH_ID.value; const isQuantizedDraco = decodedPositions && data.attributes.POSITION.value.quantization; const isOctEncodedDraco = decodedNormals && data.attributes.NORMAL.value.quantization; if (isQuantizedDraco) { const quantization = data.POSITION.data.quantization; const range = quantization.range; tile.quantizedVolumeScale = new Vector3$1(range, range, range); tile.quantizedVolumeOffset = new Vector3$1(quantization.minValues); tile.quantizedRange = (1 << quantization.quantizationBits) - 1.0; tile.isQuantizedDraco = true; } if (isOctEncodedDraco) { tile.octEncodedRange = (1 << data.NORMAL.data.quantization.quantizationBits) - 1.0; tile.isOctEncodedDraco = true; } const batchTableAttributes = {}; if (dracoData.batchTableProperties) { for (const attributeName of Object.keys(dracoData.batchTableProperties)) { if (data.attributes[attributeName] && data.attributes[attributeName].value) { batchTableAttributes[attributeName.toLowerCase()] = data.attributes[attributeName].value; } } } tile.attributes = { positions: decodedPositions, colors: normalize3DTileColorAttribute(tile, decodedColors, undefined), normals: decodedNormals, batchIds: decodedBatchIds, ...batchTableAttributes }; } const VERSION$3 = "3.1.4" ; const VERSION$2 = "3.1.4" ; const VERSION$1 = "3.1.4" ; const BASIS_CDN_ENCODER_WASM = "https://unpkg.com/@loaders.gl/textures@".concat(VERSION$1, "/dist/libs/basis_encoder.wasm"); const BASIS_CDN_ENCODER_JS = "https://unpkg.com/@loaders.gl/textures@".concat(VERSION$1, "/dist/libs/basis_encoder.js"); let loadBasisTranscoderPromise; async function loadBasisTrascoderModule(options) { const modules = options.modules || {}; if (modules.basis) { return modules.basis; } loadBasisTranscoderPromise = loadBasisTranscoderPromise || loadBasisTrascoder(options); return await loadBasisTranscoderPromise; } async function loadBasisTrascoder(options) { let BASIS = null; let wasmBinary = null; [BASIS, wasmBinary] = await Promise.all([await loadLibrary('basis_transcoder.js', 'textures', options), await loadLibrary('basis_transcoder.wasm', 'textures', options)]); BASIS = BASIS || globalThis.BASIS; return await initializeBasisTrascoderModule(BASIS, wasmBinary); } function initializeBasisTrascoderModule(BasisModule, wasmBinary) { const options = {}; if (wasmBinary) { options.wasmBinary = wasmBinary; } return new Promise(resolve => { BasisModule(options).then(module => { const { BasisFile, initializeBasis } = module; initializeBasis(); resolve({ BasisFile }); }); }); } let loadBasisEncoderPromise; async function loadBasisEncoderModule(options) { const modules = options.modules || {}; if (modules.basisEncoder) { return modules.basisEncoder; } loadBasisEncoderPromise = loadBasisEncoderPromise || loadBasisEncoder(options); return await loadBasisEncoderPromise; } async function loadBasisEncoder(options) { let BASIS_ENCODER = null; let wasmBinary = null; [BASIS_ENCODER, wasmBinary] = await Promise.all([await loadLibrary(BASIS_CDN_ENCODER_JS, 'textures', options), await loadLibrary(BASIS_CDN_ENCODER_WASM, 'textures', options)]); BASIS_ENCODER = BASIS_ENCODER || globalThis.BASIS; return await initializeBasisEncoderModule(BASIS_ENCODER, wasmBinary); } function initializeBasisEncoderModule(BasisEncoderModule, wasmBinary) { const options = {}; if (wasmBinary) { options.wasmBinary = wasmBinary; } return new Promise(resolve => { BasisEncoderModule(options).then(module => { const { BasisFile, KTX2File, initializeBasis, BasisEncoder } = module; initializeBasis(); resolve({ BasisFile, KTX2File, BasisEncoder }); }); }); } const GL_EXTENSIONS_CONSTANTS = { COMPRESSED_RGB_S3TC_DXT1_EXT: 0x83f0, COMPRESSED_RGBA_S3TC_DXT1_EXT: 0x83f1, COMPRESSED_RGBA_S3TC_DXT3_EXT: 0x83f2, COMPRESSED_RGBA_S3TC_DXT5_EXT: 0x83f3, COMPRESSED_R11_EAC: 0x9270, COMPRESSED_SIGNED_R11_EAC: 0x9271, COMPRESSED_RG11_EAC: 0x9272, COMPRESSED_SIGNED_RG11_EAC: 0x9273, COMPRESSED_RGB8_ETC2: 0x9274, COMPRESSED_RGBA8_ETC2_EAC: 0x9275, COMPRESSED_SRGB8_ETC2: 0x9276, COMPRESSED_SRGB8_ALPHA8_ETC2_EAC: 0x9277, COMPRESSED_RGB8_PUNCHTHROUGH_ALPHA1_ETC2: 0x9278, COMPRESSED_SRGB8_PUNCHTHROUGH_ALPHA1_ETC2: 0x9279, COMPRESSED_RGB_PVRTC_4BPPV1_IMG: 0x8c00, COMPRESSED_RGBA_PVRTC_4BPPV1_IMG: 0x8c02, COMPRESSED_RGB_PVRTC_2BPPV1_IMG: 0x8c01, COMPRESSED_RGBA_PVRTC_2BPPV1_IMG: 0x8c03, COMPRESSED_RGB_ETC1_WEBGL: 0x8d64, COMPRESSED_RGB_ATC_WEBGL: 0x8c92, COMPRESSED_RGBA_ATC_EXPLICIT_ALPHA_WEBGL: 0x8c93, COMPRESSED_RGBA_ATC_INTERPOLATED_ALPHA_WEBGL: 0x87ee, COMPRESSED_RGBA_ASTC_4X4_KHR: 0x93b0, COMPRESSED_RGBA_ASTC_5X4_KHR: 0x93b1, COMPRESSED_RGBA_ASTC_5X5_KHR: 0x93b2, COMPRESSED_RGBA_ASTC_6X5_KHR: 0x93b3, COMPRESSED_RGBA_ASTC_6X6_KHR: 0x93b4, COMPRESSED_RGBA_ASTC_8X5_KHR: 0x93b5, COMPRESSED_RGBA_ASTC_8X6_KHR: 0x93b6, COMPRESSED_RGBA_ASTC_8X8_KHR: 0x93b7, COMPRESSED_RGBA_ASTC_10X5_KHR: 0x93b8, COMPRESSED_RGBA_ASTC_10X6_KHR: 0x93b9, COMPRESSED_RGBA_ASTC_10X8_KHR: 0x93ba, COMPRESSED_RGBA_ASTC_10X10_KHR: 0x93bb, COMPRESSED_RGBA_ASTC_12X10_KHR: 0x93bc, COMPRESSED_RGBA_ASTC_12X12_KHR: 0x93bd, COMPRESSED_SRGB8_ALPHA8_ASTC_4X4_KHR: 0x93d0, COMPRESSED_SRGB8_ALPHA8_ASTC_5X4_KHR: 0x93d1, COMPRESSED_SRGB8_ALPHA8_ASTC_5X5_KHR: 0x93d2, COMPRESSED_SRGB8_ALPHA8_ASTC_6X5_KHR: 0x93d3, COMPRESSED_SRGB8_ALPHA8_ASTC_6X6_KHR: 0x93d4, COMPRESSED_SRGB8_ALPHA8_ASTC_8X5_KHR: 0x93d5, COMPRESSED_SRGB8_ALPHA8_ASTC_8X6_KHR: 0x93d6, COMPRESSED_SRGB8_ALPHA8_ASTC_8X8_KHR: 0x93d7, COMPRESSED_SRGB8_ALPHA8_ASTC_10X5_KHR: 0x93d8, COMPRESSED_SRGB8_ALPHA8_ASTC_10X6_KHR: 0x93d9, COMPRESSED_SRGB8_ALPHA8_ASTC_10X8_KHR: 0x93da, COMPRESSED_SRGB8_ALPHA8_ASTC_10X10_KHR: 0x93db, COMPRESSED_SRGB8_ALPHA8_ASTC_12X10_KHR: 0x93dc, COMPRESSED_SRGB8_ALPHA8_ASTC_12X12_KHR: 0x93dd, COMPRESSED_RED_RGTC1_EXT: 0x8dbb, COMPRESSED_SIGNED_RED_RGTC1_EXT: 0x8dbc, COMPRESSED_RED_GREEN_RGTC2_EXT: 0x8dbd, COMPRESSED_SIGNED_RED_GREEN_RGTC2_EXT: 0x8dbe, COMPRESSED_SRGB_S3TC_DXT1_EXT: 0x8c4c, COMPRESSED_SRGB_ALPHA_S3TC_DXT1_EXT: 0x8c4d, COMPRESSED_SRGB_ALPHA_S3TC_DXT3_EXT: 0x8c4e, COMPRESSED_SRGB_ALPHA_S3TC_DXT5_EXT: 0x8c4f }; const BROWSER_PREFIXES = ['', 'WEBKIT_', 'MOZ_']; const WEBGL_EXTENSIONS = { WEBGL_compressed_texture_s3tc: 'dxt', WEBGL_compressed_texture_s3tc_srgb: 'dxt-srgb', WEBGL_compressed_texture_etc1: 'etc1', WEBGL_compressed_texture_etc: 'etc2', WEBGL_compressed_texture_pvrtc: 'pvrtc', WEBGL_compressed_texture_atc: 'atc', WEBGL_compressed_texture_astc: 'astc', EXT_texture_compression_rgtc: 'rgtc' }; let formats = null; function getSupportedGPUTextureFormats(gl) { if (!formats) { gl = gl || getWebGLContext() || undefined; formats = new Set(); for (const prefix of BROWSER_PREFIXES) { for (const extension in WEBGL_EXTENSIONS) { if (gl && gl.getExtension("".concat(prefix).concat(extension))) { const gpuTextureFormat = WEBGL_EXTENSIONS[extension]; formats.add(gpuTextureFormat); } } } } return formats; } function getWebGLContext() { try { const canvas = document.createElement('canvas'); return canvas.getContext('webgl'); } catch (error) { return null; } } var n$2,i$2,s$1,a$2,r$1,o$2,l$2,f$1;!function(t){t[t.NONE=0]="NONE",t[t.BASISLZ=1]="BASISLZ",t[t.ZSTD=2]="ZSTD",t[t.ZLIB=3]="ZLIB";}(n$2||(n$2={})),function(t){t[t.BASICFORMAT=0]="BASICFORMAT";}(i$2||(i$2={})),function(t){t[t.UNSPECIFIED=0]="UNSPECIFIED",t[t.ETC1S=163]="ETC1S",t[t.UASTC=166]="UASTC";}(s$1||(s$1={})),function(t){t[t.UNSPECIFIED=0]="UNSPECIFIED",t[t.SRGB=1]="SRGB";}(a$2||(a$2={})),function(t){t[t.UNSPECIFIED=0]="UNSPECIFIED",t[t.LINEAR=1]="LINEAR",t[t.SRGB=2]="SRGB",t[t.ITU=3]="ITU",t[t.NTSC=4]="NTSC",t[t.SLOG=5]="SLOG",t[t.SLOG2=6]="SLOG2";}(r$1||(r$1={})),function(t){t[t.ALPHA_STRAIGHT=0]="ALPHA_STRAIGHT",t[t.ALPHA_PREMULTIPLIED=1]="ALPHA_PREMULTIPLIED";}(o$2||(o$2={})),function(t){t[t.RGB=0]="RGB",t[t.RRR=3]="RRR",t[t.GGG=4]="GGG",t[t.AAA=15]="AAA";}(l$2||(l$2={})),function(t){t[t.RGB=0]="RGB",t[t.RGBA=3]="RGBA",t[t.RRR=4]="RRR",t[t.RRRG=5]="RRRG";}(f$1||(f$1={})); const KTX2_ID = [0xab, 0x4b, 0x54, 0x58, 0x20, 0x32, 0x30, 0xbb, 0x0d, 0x0a, 0x1a, 0x0a]; function isKTX(data) { const id = new Uint8Array(data); const notKTX = id.byteLength < KTX2_ID.length || id[0] !== KTX2_ID[0] || id[1] !== KTX2_ID[1] || id[2] !== KTX2_ID[2] || id[3] !== KTX2_ID[3] || id[4] !== KTX2_ID[4] || id[5] !== KTX2_ID[5] || id[6] !== KTX2_ID[6] || id[7] !== KTX2_ID[7] || id[8] !== KTX2_ID[8] || id[9] !== KTX2_ID[9] || id[10] !== KTX2_ID[10] || id[11] !== KTX2_ID[11]; return !notKTX; } const OutputFormat = { etc1: { basisFormat: 0, compressed: true, format: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_ETC1_WEBGL }, etc2: { basisFormat: 1, compressed: true }, bc1: { basisFormat: 2, compressed: true, format: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_S3TC_DXT1_EXT }, bc3: { basisFormat: 3, compressed: true, format: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_S3TC_DXT5_EXT }, bc4: { basisFormat: 4, compressed: true }, bc5: { basisFormat: 5, compressed: true }, 'bc7-m6-opaque-only': { basisFormat: 6, compressed: true }, 'bc7-m5': { basisFormat: 7, compressed: true }, 'pvrtc1-4-rgb': { basisFormat: 8, compressed: true, format: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGB_PVRTC_4BPPV1_IMG }, 'pvrtc1-4-rgba': { basisFormat: 9, compressed: true, format: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_PVRTC_4BPPV1_IMG }, 'astc-4x4': { basisFormat: 10, compressed: true, format: GL_EXTENSIONS_CONSTANTS.COMPRESSED_RGBA_ASTC_4X4_KHR }, 'atc-rgb': { basisFormat: 11, compressed: true }, 'atc-rgba-interpolated-alpha': { basisFormat: 12, compressed: true }, rgba32: { basisFormat: 13, compressed: false }, rgb565: { basisFormat: 14, compressed: false }, bgr565: { basisFormat: 15, compressed: false }, rgba4444: { basisFormat: 16, compressed: false } }; async function parseBasis(data, options) { if (options.basis.containerFormat === 'auto') { if (isKTX(data)) { const fileConstructors = await loadBasisEncoderModule(options); return parseKTX2File(fileConstructors.KTX2File, data, options); } const { BasisFile } = await loadBasisTrascoderModule(options); return parseBasisFile(BasisFile, data, options); } switch (options.basis.module) { case 'encoder': const fileConstructors = await loadBasisEncoderModule(options); switch (options.basis.containerFormat) { case 'ktx2': return parseKTX2File(fileConstructors.KTX2File, data, options); case 'basis': default: return parseBasisFile(fileConstructors.BasisFile, data, options); } case 'transcoder': default: const { BasisFile } = await loadBasisTrascoderModule(options); return parseBasisFile(BasisFile, data, options); } } function parseBasisFile(BasisFile, data, options) { const basisFile = new BasisFile(new Uint8Array(data)); try { if (!basisFile.startTranscoding()) { return null; } const imageCount = basisFile.getNumImages(); const images = []; for (let imageIndex = 0; imageIndex < imageCount; imageIndex++) { const levelsCount = basisFile.getNumLevels(imageIndex); const levels = []; for (let levelIndex = 0; levelIndex < levelsCount; levelIndex++) { levels.push(transcodeImage(basisFile, imageIndex, levelIndex, options)); } images.push(levels); } return images; } finally { basisFile.close(); basisFile.delete(); } } function transcodeImage(basisFile, imageIndex, levelIndex, options) { const width = basisFile.getImageWidth(imageIndex, levelIndex); const height = basisFile.getImageHeight(imageIndex, levelIndex); const hasAlpha = basisFile.getHasAlpha(); const { compressed, format, basisFormat } = getBasisOptions(options, hasAlpha); const decodedSize = basisFile.getImageTranscodedSizeInBytes(imageIndex, levelIndex, basisFormat); const decodedData = new Uint8Array(decodedSize); if (!basisFile.transcodeImage(decodedData, imageIndex, levelIndex, basisFormat, 0, 0)) { return null; } return { width, height, data: decodedData, compressed, hasAlpha, format }; } function parseKTX2File(KTX2File, data, options) { const ktx2File = new KTX2File(new Uint8Array(data)); try { if (!ktx2File.startTranscoding()) { return null; } const levelsCount = ktx2File.getLevels(); const levels = []; for (let levelIndex = 0; levelIndex < levelsCount; levelIndex++) { levels.push(transcodeKTX2Image(ktx2File, levelIndex, options)); break; } return levels; } finally { ktx2File.close(); ktx2File.delete(); } } function transcodeKTX2Image(ktx2File, levelIndex, options) { const { alphaFlag, height, width } = ktx2File.getImageLevelInfo(levelIndex, 0, 0); const { compressed, format, basisFormat } = getBasisOptions(options, alphaFlag); const decodedSize = ktx2File.getImageTranscodedSizeInBytes(levelIndex, 0, 0, basisFormat); const decodedData = new Uint8Array(decodedSize); if (!ktx2File.transcodeImage(decodedData, levelIndex, 0, 0, basisFormat, 0, -1, -1)) { return null; } return { width, height, data: decodedData, compressed, alphaFlag, format }; } function getBasisOptions(options, hasAlpha) { let format = options && options.basis && options.basis.format; if (format === 'auto') { format = selectSupportedBasisFormat(); } if (typeof format === 'object') { format = hasAlpha ? format.alpha : format.noAlpha; } format = format.toLowerCase(); return OutputFormat[format]; } function selectSupportedBasisFormat() { const supportedFormats = getSupportedGPUTextureFormats(); if (supportedFormats.has('astc')) { return 'astc-4x4'; } else if (supportedFormats.has('dxt')) { return { alpha: 'bc3', noAlpha: 'bc1' }; } else if (supportedFormats.has('pvrtc')) { return { alpha: 'pvrtc1-4-rgba', noAlpha: 'pvrtc1-4-rgb' }; } else if (supportedFormats.has('etc1')) { return 'etc1'; } else if (supportedFormats.has('etc2')) { return 'etc2'; } return 'rgb565'; } const BasisWorkerLoader = { name: 'Basis', id: 'basis', module: 'textures', version: VERSION$2, worker: true, extensions: ['basis', 'ktx2'], mimeTypes: ['application/octet-stream', 'image/ktx2'], tests: ['sB'], binary: true, options: { basis: { format: 'auto', libraryPath: 'libs/', containerFormat: 'auto', module: 'transcoder' } } }; const BasisLoader = { ...BasisWorkerLoader, parse: parseBasis }; const VERSION = "3.1.4" ; const { _parseImageNode } = globalThis; const IMAGE_SUPPORTED = typeof Image !== 'undefined'; const IMAGE_BITMAP_SUPPORTED = typeof ImageBitmap !== 'undefined'; const NODE_IMAGE_SUPPORTED = Boolean(_parseImageNode); const DATA_SUPPORTED = isBrowser$2 ? true : NODE_IMAGE_SUPPORTED; function isImageTypeSupported(type) { switch (type) { case 'auto': return IMAGE_BITMAP_SUPPORTED || IMAGE_SUPPORTED || DATA_SUPPORTED; case 'imagebitmap': return IMAGE_BITMAP_SUPPORTED; case 'image': return IMAGE_SUPPORTED; case 'data': return DATA_SUPPORTED; default: throw new Error("@loaders.gl/images: image ".concat(type, " not supported in this environment")); } } function getDefaultImageType() { if (IMAGE_BITMAP_SUPPORTED) { return 'imagebitmap'; } if (IMAGE_SUPPORTED) { return 'image'; } if (DATA_SUPPORTED) { return 'data'; } throw new Error('Install \'@loaders.gl/polyfills\' to parse images under Node.js'); } function getImageType(image) { const format = getImageTypeOrNull(image); if (!format) { throw new Error('Not an image'); } return format; } function getImageData(image) { switch (getImageType(image)) { case 'data': return image; case 'image': case 'imagebitmap': const canvas = document.createElement('canvas'); const context = canvas.getContext('2d'); if (!context) { throw new Error('getImageData'); } canvas.width = image.width; canvas.height = image.height; context.drawImage(image, 0, 0); return context.getImageData(0, 0, image.width, image.height); default: throw new Error('getImageData'); } } function getImageTypeOrNull(image) { if (typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap) { return 'imagebitmap'; } if (typeof Image !== 'undefined' && image instanceof Image) { return 'image'; } if (image && typeof image === 'object' && image.data && image.width && image.height) { return 'data'; } return null; } const SVG_DATA_URL_PATTERN = /^data:image\/svg\+xml/; const SVG_URL_PATTERN = /\.svg((\?|#).*)?$/; function isSVG(url) { return url && (SVG_DATA_URL_PATTERN.test(url) || SVG_URL_PATTERN.test(url)); } function getBlobOrSVGDataUrl(arrayBuffer, url) { if (isSVG(url)) { const textDecoder = new TextDecoder(); let xmlText = textDecoder.decode(arrayBuffer); try { if (typeof unescape === 'function' && typeof encodeURIComponent === 'function') { xmlText = unescape(encodeURIComponent(xmlText)); } } catch (error) { throw new Error(error.message); } const src = "data:image/svg+xml;base64,".concat(btoa(xmlText)); return src; } return getBlob(arrayBuffer, url); } function getBlob(arrayBuffer, url) { if (isSVG(url)) { throw new Error('SVG cannot be parsed directly to imagebitmap'); } return new Blob([new Uint8Array(arrayBuffer)]); } async function parseToImage(arrayBuffer, options, url) { const blobOrDataUrl = getBlobOrSVGDataUrl(arrayBuffer, url); const URL = self.URL || self.webkitURL; const objectUrl = typeof blobOrDataUrl !== 'string' && URL.createObjectURL(blobOrDataUrl); try { return await loadToImage(objectUrl || blobOrDataUrl, options); } finally { if (objectUrl) { URL.revokeObjectURL(objectUrl); } } } async function loadToImage(url, options) { const image = new Image(); image.src = url; if (options.image && options.image.decode && image.decode) { await image.decode(); return image; } return await new Promise((resolve, reject) => { try { image.onload = () => resolve(image); image.onerror = err => reject(new Error("Could not load image ".concat(url, ": ").concat(err))); } catch (error) { reject(error); } }); } const EMPTY_OBJECT = {}; let imagebitmapOptionsSupported = true; async function parseToImageBitmap(arrayBuffer, options, url) { let blob; if (isSVG(url)) { const image = await parseToImage(arrayBuffer, options, url); blob = image; } else { blob = getBlob(arrayBuffer, url); } const imagebitmapOptions = options && options.imagebitmap; return await safeCreateImageBitmap(blob, imagebitmapOptions); } async function safeCreateImageBitmap(blob, imagebitmapOptions = null) { if (isEmptyObject(imagebitmapOptions) || !imagebitmapOptionsSupported) { imagebitmapOptions = null; } if (imagebitmapOptions) { try { return await createImageBitmap(blob, imagebitmapOptions); } catch (error) { console.warn(error); imagebitmapOptionsSupported = false; } } return await createImageBitmap(blob); } function isEmptyObject(object) { for (const key in object || EMPTY_OBJECT) { return false; } return true; } const BIG_ENDIAN = false; const LITTLE_ENDIAN = true; function getBinaryImageMetadata(binaryData) { const dataView = toDataView(binaryData); return getPngMetadata(dataView) || getJpegMetadata(dataView) || getGifMetadata(dataView) || getBmpMetadata(dataView); } function getPngMetadata(binaryData) { const dataView = toDataView(binaryData); const isPng = dataView.byteLength >= 24 && dataView.getUint32(0, BIG_ENDIAN) === 0x89504e47; if (!isPng) { return null; } return { mimeType: 'image/png', width: dataView.getUint32(16, BIG_ENDIAN), height: dataView.getUint32(20, BIG_ENDIAN) }; } function getGifMetadata(binaryData) { const dataView = toDataView(binaryData); const isGif = dataView.byteLength >= 10 && dataView.getUint32(0, BIG_ENDIAN) === 0x47494638; if (!isGif) { return null; } return { mimeType: 'image/gif', width: dataView.getUint16(6, LITTLE_ENDIAN), height: dataView.getUint16(8, LITTLE_ENDIAN) }; } function getBmpMetadata(binaryData) { const dataView = toDataView(binaryData); const isBmp = dataView.byteLength >= 14 && dataView.getUint16(0, BIG_ENDIAN) === 0x424d && dataView.getUint32(2, LITTLE_ENDIAN) === dataView.byteLength; if (!isBmp) { return null; } return { mimeType: 'image/bmp', width: dataView.getUint32(18, LITTLE_ENDIAN), height: dataView.getUint32(22, LITTLE_ENDIAN) }; } function getJpegMetadata(binaryData) { const dataView = toDataView(binaryData); const isJpeg = dataView.byteLength >= 3 && dataView.getUint16(0, BIG_ENDIAN) === 0xffd8 && dataView.getUint8(2) === 0xff; if (!isJpeg) { return null; } const { tableMarkers, sofMarkers } = getJpegMarkers(); let i = 2; while (i + 9 < dataView.byteLength) { const marker = dataView.getUint16(i, BIG_ENDIAN); if (sofMarkers.has(marker)) { return { mimeType: 'image/jpeg', height: dataView.getUint16(i + 5, BIG_ENDIAN), width: dataView.getUint16(i + 7, BIG_ENDIAN) }; } if (!tableMarkers.has(marker)) { return null; } i += 2; i += dataView.getUint16(i, BIG_ENDIAN); } return null; } function getJpegMarkers() { const tableMarkers = new Set([0xffdb, 0xffc4, 0xffcc, 0xffdd, 0xfffe]); for (let i = 0xffe0; i < 0xfff0; ++i) { tableMarkers.add(i); } const sofMarkers = new Set([0xffc0, 0xffc1, 0xffc2, 0xffc3, 0xffc5, 0xffc6, 0xffc7, 0xffc9, 0xffca, 0xffcb, 0xffcd, 0xffce, 0xffcf, 0xffde]); return { tableMarkers, sofMarkers }; } function toDataView(data) { if (data instanceof DataView) { return data; } if (ArrayBuffer.isView(data)) { return new DataView(data.buffer); } if (data instanceof ArrayBuffer) { return new DataView(data); } throw new Error('toDataView'); } async function parseToNodeImage(arrayBuffer, options) { const { mimeType } = getBinaryImageMetadata(arrayBuffer) || {}; const _parseImageNode = globalThis._parseImageNode; assert$7(_parseImageNode); return await _parseImageNode(arrayBuffer, mimeType); } async function parseImage(arrayBuffer, options, context) { options = options || {}; const imageOptions = options.image || {}; const imageType = imageOptions.type || 'auto'; const { url } = context || {}; const loadType = getLoadableImageType(imageType); let image; switch (loadType) { case 'imagebitmap': image = await parseToImageBitmap(arrayBuffer, options, url); break; case 'image': image = await parseToImage(arrayBuffer, options, url); break; case 'data': image = await parseToNodeImage(arrayBuffer); break; default: assert$7(false); } if (imageType === 'data') { image = getImageData(image); } return image; } function getLoadableImageType(type) { switch (type) { case 'auto': case 'data': return getDefaultImageType(); default: isImageTypeSupported(type); return type; } } const EXTENSIONS$1 = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'ico', 'svg']; const MIME_TYPES = ['image/png', 'image/jpeg', 'image/gif', 'image/webp', 'image/bmp', 'image/vnd.microsoft.icon', 'image/svg+xml']; const DEFAULT_IMAGE_LOADER_OPTIONS = { image: { type: 'auto', decode: true } }; const ImageLoader$1 = { id: 'image', module: 'images', name: 'Images', version: VERSION, mimeTypes: MIME_TYPES, extensions: EXTENSIONS$1, parse: parseImage, tests: [arrayBuffer => Boolean(getBinaryImageMetadata(new DataView(arrayBuffer)))], options: DEFAULT_IMAGE_LOADER_OPTIONS }; const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif']; const mimeTypeSupported = {}; function _isImageFormatSupported(mimeType) { if (mimeTypeSupported[mimeType] === undefined) { mimeTypeSupported[mimeType] = checkFormatSupport(mimeType); } return mimeTypeSupported[mimeType]; } function checkFormatSupport(mimeType) { switch (mimeType) { case 'image/webp': return checkWebPSupport(); case 'image/svg': return isBrowser$2; default: if (!isBrowser$2) { const { _parseImageNode } = globalThis; return Boolean(_parseImageNode) && NODE_FORMAT_SUPPORT.includes(mimeType); } return true; } } function checkWebPSupport() { if (!isBrowser$2) { return false; } try { const element = document.createElement('canvas'); return element.toDataURL('image/webp').indexOf('data:image/webp') === 0; } catch { return false; } } function assert$1(condition, message) { if (!condition) { throw new Error(message || 'assert failed: gltf'); } } function resolveUrl(url, options) { const absolute = url.startsWith('data:') || url.startsWith('http:') || url.startsWith('https:'); if (absolute) { return url; } const baseUrl = options.baseUri || options.uri; if (!baseUrl) { throw new Error("'baseUri' must be provided to resolve relative url ".concat(url)); } return baseUrl.substr(0, baseUrl.lastIndexOf('/') + 1) + url; } function getTypedArrayForBufferView(json, buffers, bufferViewIndex) { const bufferView = json.bufferViews[bufferViewIndex]; assert$1(bufferView); const bufferIndex = bufferView.buffer; const binChunk = buffers[bufferIndex]; assert$1(binChunk); const byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset; return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength); } const TYPES = ['SCALAR', 'VEC2', 'VEC3', 'VEC4']; const ARRAY_CONSTRUCTOR_TO_WEBGL_CONSTANT = [[Int8Array, 5120], [Uint8Array, 5121], [Int16Array, 5122], [Uint16Array, 5123], [Uint32Array, 5125], [Float32Array, 5126], [Float64Array, 5130]]; const ARRAY_TO_COMPONENT_TYPE = new Map(ARRAY_CONSTRUCTOR_TO_WEBGL_CONSTANT); const ATTRIBUTE_TYPE_TO_COMPONENTS = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 }; const ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE = { 5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4 }; const ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY = { 5120: Int8Array, 5121: Uint8Array, 5122: Int16Array, 5123: Uint16Array, 5125: Uint32Array, 5126: Float32Array }; function getAccessorTypeFromSize(size) { const type = TYPES[size - 1]; return type || TYPES[0]; } function getComponentTypeFromArray(typedArray) { const componentType = ARRAY_TO_COMPONENT_TYPE.get(typedArray.constructor); if (!componentType) { throw new Error('Illegal typed array'); } return componentType; } function getAccessorArrayTypeAndLength(accessor, bufferView) { const ArrayType = ATTRIBUTE_COMPONENT_TYPE_TO_ARRAY[accessor.componentType]; const components = ATTRIBUTE_TYPE_TO_COMPONENTS[accessor.type]; const bytesPerComponent = ATTRIBUTE_COMPONENT_TYPE_TO_BYTE_SIZE[accessor.componentType]; const length = accessor.count * components; const byteLength = accessor.count * components * bytesPerComponent; assert$1(byteLength >= 0 && byteLength <= bufferView.byteLength); return { ArrayType, length, byteLength }; } const DEFAULT_GLTF_JSON = { asset: { version: '2.0', generator: 'loaders.gl' }, buffers: [] }; class GLTFScenegraph { constructor(gltf) { _defineProperty(this, "gltf", void 0); _defineProperty(this, "sourceBuffers", void 0); _defineProperty(this, "byteLength", void 0); this.gltf = gltf || { json: { ...DEFAULT_GLTF_JSON }, buffers: [] }; this.sourceBuffers = []; this.byteLength = 0; if (this.gltf.buffers && this.gltf.buffers[0]) { this.byteLength = this.gltf.buffers[0].byteLength; this.sourceBuffers = [this.gltf.buffers[0]]; } } get json() { return this.gltf.json; } getApplicationData(key) { const data = this.json[key]; return data; } getExtraData(key) { const extras = this.json.extras || {}; return extras[key]; } getExtension(extensionName) { const isExtension = this.getUsedExtensions().find(name => name === extensionName); const extensions = this.json.extensions || {}; return isExtension ? extensions[extensionName] || true : null; } getRequiredExtension(extensionName) { const isRequired = this.getRequiredExtensions().find(name => name === extensionName); return isRequired ? this.getExtension(extensionName) : null; } getRequiredExtensions() { return this.json.extensionsRequired || []; } getUsedExtensions() { return this.json.extensionsUsed || []; } getObjectExtension(object, extensionName) { const extensions = object.extensions || {}; return extensions[extensionName]; } getScene(index) { return this.getObject('scenes', index); } getNode(index) { return this.getObject('nodes', index); } getSkin(index) { return this.getObject('skins', index); } getMesh(index) { return this.getObject('meshes', index); } getMaterial(index) { return this.getObject('materials', index); } getAccessor(index) { return this.getObject('accessors', index); } getTexture(index) { return this.getObject('textures', index); } getSampler(index) { return this.getObject('samplers', index); } getImage(index) { return this.getObject('images', index); } getBufferView(index) { return this.getObject('bufferViews', index); } getBuffer(index) { return this.getObject('buffers', index); } getObject(array, index) { if (typeof index === 'object') { return index; } const object = this.json[array] && this.json[array][index]; if (!object) { throw new Error("glTF file error: Could not find ".concat(array, "[").concat(index, "]")); } return object; } getTypedArrayForBufferView(bufferView) { bufferView = this.getBufferView(bufferView); const bufferIndex = bufferView.buffer; const binChunk = this.gltf.buffers[bufferIndex]; assert$1(binChunk); const byteOffset = (bufferView.byteOffset || 0) + binChunk.byteOffset; return new Uint8Array(binChunk.arrayBuffer, byteOffset, bufferView.byteLength); } getTypedArrayForAccessor(accessor) { accessor = this.getAccessor(accessor); const bufferView = this.getBufferView(accessor.bufferView); const buffer = this.getBuffer(bufferView.buffer); const arrayBuffer = buffer.data; const { ArrayType, length } = getAccessorArrayTypeAndLength(accessor, bufferView); const byteOffset = bufferView.byteOffset + accessor.byteOffset; return new ArrayType(arrayBuffer, byteOffset, length); } getTypedArrayForImageData(image) { image = this.getAccessor(image); const bufferView = this.getBufferView(image.bufferView); const buffer = this.getBuffer(bufferView.buffer); const arrayBuffer = buffer.data; const byteOffset = bufferView.byteOffset || 0; return new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength); } addApplicationData(key, data) { this.json[key] = data; return this; } addExtraData(key, data) { this.json.extras = this.json.extras || {}; this.json.extras[key] = data; return this; } addObjectExtension(object, extensionName, data) { object.extensions = object.extensions || {}; object.extensions[extensionName] = data; this.registerUsedExtension(extensionName); return this; } setObjectExtension(object, extensionName, data) { const extensions = object.extensions || {}; extensions[extensionName] = data; } removeObjectExtension(object, extensionName) { const extensions = object.extensions || {}; const extension = extensions[extensionName]; delete extensions[extensionName]; return extension; } addExtension(extensionName, extensionData = {}) { assert$1(extensionData); this.json.extensions = this.json.extensions || {}; this.json.extensions[extensionName] = extensionData; this.registerUsedExtension(extensionName); return extensionData; } addRequiredExtension(extensionName, extensionData = {}) { assert$1(extensionData); this.addExtension(extensionName, extensionData); this.registerRequiredExtension(extensionName); return extensionData; } registerUsedExtension(extensionName) { this.json.extensionsUsed = this.json.extensionsUsed || []; if (!this.json.extensionsUsed.find(ext => ext === extensionName)) { this.json.extensionsUsed.push(extensionName); } } registerRequiredExtension(extensionName) { this.registerUsedExtension(extensionName); this.json.extensionsRequired = this.json.extensionsRequired || []; if (!this.json.extensionsRequired.find(ext => ext === extensionName)) { this.json.extensionsRequired.push(extensionName); } } removeExtension(extensionName) { if (this.json.extensionsRequired) { this._removeStringFromArray(this.json.extensionsRequired, extensionName); } if (this.json.extensionsUsed) { this._removeStringFromArray(this.json.extensionsUsed, extensionName); } if (this.json.extensions) { delete this.json.extensions[extensionName]; } } setDefaultScene(sceneIndex) { this.json.scene = sceneIndex; } addScene(scene) { const { nodeIndices } = scene; this.json.scenes = this.json.scenes || []; this.json.scenes.push({ nodes: nodeIndices }); return this.json.scenes.length - 1; } addNode(node) { const { meshIndex, matrix } = node; this.json.nodes = this.json.nodes || []; const nodeData = { mesh: meshIndex }; if (matrix) { nodeData.matrix = matrix; } this.json.nodes.push(nodeData); return this.json.nodes.length - 1; } addMesh(mesh) { const { attributes, indices, material, mode = 4 } = mesh; const accessors = this._addAttributes(attributes); const glTFMesh = { primitives: [{ attributes: accessors, mode }] }; if (indices) { const indicesAccessor = this._addIndices(indices); glTFMesh.primitives[0].indices = indicesAccessor; } if (Number.isFinite(material)) { glTFMesh.primitives[0].material = material; } this.json.meshes = this.json.meshes || []; this.json.meshes.push(glTFMesh); return this.json.meshes.length - 1; } addPointCloud(attributes) { const accessorIndices = this._addAttributes(attributes); const glTFMesh = { primitives: [{ attributes: accessorIndices, mode: 0 }] }; this.json.meshes = this.json.meshes || []; this.json.meshes.push(glTFMesh); return this.json.meshes.length - 1; } addImage(imageData, mimeTypeOpt) { const metadata = getBinaryImageMetadata(imageData); const mimeType = mimeTypeOpt || (metadata === null || metadata === void 0 ? void 0 : metadata.mimeType); const bufferViewIndex = this.addBufferView(imageData); const glTFImage = { bufferView: bufferViewIndex, mimeType }; this.json.images = this.json.images || []; this.json.images.push(glTFImage); return this.json.images.length - 1; } addBufferView(buffer) { const byteLength = buffer.byteLength; assert$1(Number.isFinite(byteLength)); this.sourceBuffers = this.sourceBuffers || []; this.sourceBuffers.push(buffer); const glTFBufferView = { buffer: 0, byteOffset: this.byteLength, byteLength }; this.byteLength += padToNBytes(byteLength, 4); this.json.bufferViews = this.json.bufferViews || []; this.json.bufferViews.push(glTFBufferView); return this.json.bufferViews.length - 1; } addAccessor(bufferViewIndex, accessor) { const glTFAccessor = { bufferView: bufferViewIndex, type: getAccessorTypeFromSize(accessor.size), componentType: accessor.componentType, count: accessor.count, max: accessor.max, min: accessor.min }; this.json.accessors = this.json.accessors || []; this.json.accessors.push(glTFAccessor); return this.json.accessors.length - 1; } addBinaryBuffer(sourceBuffer, accessor = { size: 3 }) { const bufferViewIndex = this.addBufferView(sourceBuffer); let minMax = { min: accessor.min, max: accessor.max }; if (!minMax.min || !minMax.max) { minMax = this._getAccessorMinMax(sourceBuffer, accessor.size); } const accessorDefaults = { size: accessor.size, componentType: getComponentTypeFromArray(sourceBuffer), count: Math.round(sourceBuffer.length / accessor.size), min: minMax.min, max: minMax.max }; return this.addAccessor(bufferViewIndex, Object.assign(accessorDefaults, accessor)); } addTexture(texture) { const { imageIndex } = texture; const glTFTexture = { source: imageIndex }; this.json.textures = this.json.textures || []; this.json.textures.push(glTFTexture); return this.json.textures.length - 1; } addMaterial(pbrMaterialInfo) { this.json.materials = this.json.materials || []; this.json.materials.push(pbrMaterialInfo); return this.json.materials.length - 1; } createBinaryChunk() { var _this$json, _this$json$buffers; this.gltf.buffers = []; const totalByteLength = this.byteLength; const arrayBuffer = new ArrayBuffer(totalByteLength); const targetArray = new Uint8Array(arrayBuffer); let dstByteOffset = 0; for (const sourceBuffer of this.sourceBuffers || []) { dstByteOffset = copyToArray(sourceBuffer, targetArray, dstByteOffset); } if ((_this$json = this.json) !== null && _this$json !== void 0 && (_this$json$buffers = _this$json.buffers) !== null && _this$json$buffers !== void 0 && _this$json$buffers[0]) { this.json.buffers[0].byteLength = totalByteLength; } else { this.json.buffers = [{ byteLength: totalByteLength }]; } this.gltf.binary = arrayBuffer; this.sourceBuffers = [arrayBuffer]; } _removeStringFromArray(array, string) { let found = true; while (found) { const index = array.indexOf(string); if (index > -1) { array.splice(index, 1); } else { found = false; } } } _addAttributes(attributes = {}) { const result = {}; for (const attributeKey in attributes) { const attributeData = attributes[attributeKey]; const attrName = this._getGltfAttributeName(attributeKey); const accessor = this.addBinaryBuffer(attributeData.value, attributeData); result[attrName] = accessor; } return result; } _addIndices(indices) { return this.addBinaryBuffer(indices, { size: 1 }); } _getGltfAttributeName(attributeName) { switch (attributeName.toLowerCase()) { case 'position': case 'positions': case 'vertices': return 'POSITION'; case 'normal': case 'normals': return 'NORMAL'; case 'color': case 'colors': return 'COLOR_0'; case 'texcoord': case 'texcoords': return 'TEXCOORD_0'; default: return attributeName; } } _getAccessorMinMax(buffer, size) { const result = { min: null, max: null }; if (buffer.length < size) { return result; } result.min = []; result.max = []; const initValues = buffer.subarray(0, size); for (const value of initValues) { result.min.push(value); result.max.push(value); } for (let index = size; index < buffer.length; index += size) { for (let componentIndex = 0; componentIndex < size; componentIndex++) { result.min[0 + componentIndex] = Math.min(result.min[0 + componentIndex], buffer[index + componentIndex]); result.max[0 + componentIndex] = Math.max(result.max[0 + componentIndex], buffer[index + componentIndex]); } } return result; } } const isWebAssemblySupported = typeof WebAssembly !== 'object'; const wasm_base = 'B9h9z9tFBBBF8fL9gBB9gLaaaaaFa9gEaaaB9gFaFa9gEaaaFaEMcBFFFGGGEIIILF9wFFFLEFBFKNFaFCx/IFMO/LFVK9tv9t9vq95GBt9f9f939h9z9t9f9j9h9s9s9f9jW9vq9zBBp9tv9z9o9v9wW9f9kv9j9v9kv9WvqWv94h919m9mvqBF8Z9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv94h919m9mvqBGy9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv949TvZ91v9u9jvBEn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9P9jWBIi9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9R919hWBLn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9F949wBKI9z9iqlBOc+x8ycGBM/qQFTa8jUUUUBCU/EBlHL8kUUUUBC9+RKGXAGCFJAI9LQBCaRKAE2BBC+gF9HQBALAEAIJHOAGlAGTkUUUBRNCUoBAG9uC/wgBZHKCUGAKCUG9JyRVAECFJRICBRcGXEXAcAF9PQFAVAFAclAcAVJAF9JyRMGXGXAG9FQBAMCbJHKC9wZRSAKCIrCEJCGrRQANCUGJRfCBRbAIRTEXGXAOATlAQ9PQBCBRISEMATAQJRIGXAS9FQBCBRtCBREEXGXAOAIlCi9PQBCBRISLMANCU/CBJAEJRKGXGXGXGXGXATAECKrJ2BBAtCKZrCEZfIBFGEBMAKhB83EBAKCNJhB83EBSEMAKAI2BIAI2BBHmCKrHYAYCE6HYy86BBAKCFJAICIJAYJHY2BBAmCIrCEZHPAPCE6HPy86BBAKCGJAYAPJHY2BBAmCGrCEZHPAPCE6HPy86BBAKCEJAYAPJHY2BBAmCEZHmAmCE6Hmy86BBAKCIJAYAmJHY2BBAI2BFHmCKrHPAPCE6HPy86BBAKCLJAYAPJHY2BBAmCIrCEZHPAPCE6HPy86BBAKCKJAYAPJHY2BBAmCGrCEZHPAPCE6HPy86BBAKCOJAYAPJHY2BBAmCEZHmAmCE6Hmy86BBAKCNJAYAmJHY2BBAI2BGHmCKrHPAPCE6HPy86BBAKCVJAYAPJHY2BBAmCIrCEZHPAPCE6HPy86BBAKCcJAYAPJHY2BBAmCGrCEZHPAPCE6HPy86BBAKCMJAYAPJHY2BBAmCEZHmAmCE6Hmy86BBAKCSJAYAmJHm2BBAI2BEHICKrHYAYCE6HYy86BBAKCQJAmAYJHm2BBAICIrCEZHYAYCE6HYy86BBAKCfJAmAYJHm2BBAICGrCEZHYAYCE6HYy86BBAKCbJAmAYJHK2BBAICEZHIAICE6HIy86BBAKAIJRISGMAKAI2BNAI2BBHmCIrHYAYCb6HYy86BBAKCFJAICNJAYJHY2BBAmCbZHmAmCb6Hmy86BBAKCGJAYAmJHm2BBAI2BFHYCIrHPAPCb6HPy86BBAKCEJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCIJAmAYJHm2BBAI2BGHYCIrHPAPCb6HPy86BBAKCLJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCKJAmAYJHm2BBAI2BEHYCIrHPAPCb6HPy86BBAKCOJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCNJAmAYJHm2BBAI2BIHYCIrHPAPCb6HPy86BBAKCVJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCcJAmAYJHm2BBAI2BLHYCIrHPAPCb6HPy86BBAKCMJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCSJAmAYJHm2BBAI2BKHYCIrHPAPCb6HPy86BBAKCQJAmAPJHm2BBAYCbZHYAYCb6HYy86BBAKCfJAmAYJHm2BBAI2BOHICIrHYAYCb6HYy86BBAKCbJAmAYJHK2BBAICbZHIAICb6HIy86BBAKAIJRISFMAKAI8pBB83BBAKCNJAICNJ8pBB83BBAICTJRIMAtCGJRtAECTJHEAS9JQBMMGXAIQBCBRISEMGXAM9FQBANAbJ2BBRtCBRKAfREEXAEANCU/CBJAKJ2BBHTCFrCBATCFZl9zAtJHt86BBAEAGJREAKCFJHKAM9HQBMMAfCFJRfAIRTAbCFJHbAG9HQBMMABAcAG9sJANCUGJAMAG9sTkUUUBpANANCUGJAMCaJAG9sJAGTkUUUBpMAMCBAIyAcJRcAIQBMC9+RKSFMCBC99AOAIlAGCAAGCA9Ly6yRKMALCU/EBJ8kUUUUBAKM+OmFTa8jUUUUBCoFlHL8kUUUUBC9+RKGXAFCE9uHOCtJAI9LQBCaRKAE2BBHNC/wFZC/gF9HQBANCbZHVCF9LQBALCoBJCgFCUFT+JUUUBpALC84Jha83EBALC8wJha83EBALC8oJha83EBALCAJha83EBALCiJha83EBALCTJha83EBALha83ENALha83EBAEAIJC9wJRcAECFJHNAOJRMGXAF9FQBCQCbAVCF6yRSABRECBRVCBRQCBRfCBRICBRKEXGXAMAcuQBC9+RKSEMGXGXAN2BBHOC/vF9LQBALCoBJAOCIrCa9zAKJCbZCEWJHb8oGIRTAb8oGBRtGXAOCbZHbAS9PQBALAOCa9zAIJCbZCGWJ8oGBAVAbyROAb9FRbGXGXAGCG9HQBABAt87FBABCIJAO87FBABCGJAT87FBSFMAEAtjGBAECNJAOjGBAECIJATjGBMAVAbJRVALCoBJAKCEWJHmAOjGBAmATjGIALAICGWJAOjGBALCoBJAKCFJCbZHKCEWJHTAtjGBATAOjGIAIAbJRIAKCFJRKSGMGXGXAbCb6QBAQAbJAbC989zJCFJRQSFMAM1BBHbCgFZROGXGXAbCa9MQBAMCFJRMSFMAM1BFHbCgBZCOWAOCgBZqROGXAbCa9MQBAMCGJRMSFMAM1BGHbCgBZCfWAOqROGXAbCa9MQBAMCEJRMSFMAM1BEHbCgBZCdWAOqROGXAbCa9MQBAMCIJRMSFMAM2BIC8cWAOqROAMCLJRMMAOCFrCBAOCFZl9zAQJRQMGXGXAGCG9HQBABAt87FBABCIJAQ87FBABCGJAT87FBSFMAEAtjGBAECNJAQjGBAECIJATjGBMALCoBJAKCEWJHOAQjGBAOATjGIALAICGWJAQjGBALCoBJAKCFJCbZHKCEWJHOAtjGBAOAQjGIAICFJRIAKCFJRKSFMGXAOCDF9LQBALAIAcAOCbZJ2BBHbCIrHTlCbZCGWJ8oGBAVCFJHtATyROALAIAblCbZCGWJ8oGBAtAT9FHmJHtAbCbZHTyRbAT9FRTGXGXAGCG9HQBABAV87FBABCIJAb87FBABCGJAO87FBSFMAEAVjGBAECNJAbjGBAECIJAOjGBMALAICGWJAVjGBALCoBJAKCEWJHYAOjGBAYAVjGIALAICFJHICbZCGWJAOjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAIAmJCbZHICGWJAbjGBALCoBJAKCGJCbZHKCEWJHOAVjGBAOAbjGIAKCFJRKAIATJRIAtATJRVSFMAVCBAM2BBHYyHTAOC/+F6HPJROAYCbZRtGXGXAYCIrHmQBAOCFJRbSFMAORbALAIAmlCbZCGWJ8oGBROMGXGXAtQBAbCFJRVSFMAbRVALAIAYlCbZCGWJ8oGBRbMGXGXAP9FQBAMCFJRYSFMAM1BFHYCgFZRTGXGXAYCa9MQBAMCGJRYSFMAM1BGHYCgBZCOWATCgBZqRTGXAYCa9MQBAMCEJRYSFMAM1BEHYCgBZCfWATqRTGXAYCa9MQBAMCIJRYSFMAM1BIHYCgBZCdWATqRTGXAYCa9MQBAMCLJRYSFMAMCKJRYAM2BLC8cWATqRTMATCFrCBATCFZl9zAQJHQRTMGXGXAmCb6QBAYRPSFMAY1BBHMCgFZROGXGXAMCa9MQBAYCFJRPSFMAY1BFHMCgBZCOWAOCgBZqROGXAMCa9MQBAYCGJRPSFMAY1BGHMCgBZCfWAOqROGXAMCa9MQBAYCEJRPSFMAY1BEHMCgBZCdWAOqROGXAMCa9MQBAYCIJRPSFMAYCLJRPAY2BIC8cWAOqROMAOCFrCBAOCFZl9zAQJHQROMGXGXAtCb6QBAPRMSFMAP1BBHMCgFZRbGXGXAMCa9MQBAPCFJRMSFMAP1BFHMCgBZCOWAbCgBZqRbGXAMCa9MQBAPCGJRMSFMAP1BGHMCgBZCfWAbqRbGXAMCa9MQBAPCEJRMSFMAP1BEHMCgBZCdWAbqRbGXAMCa9MQBAPCIJRMSFMAPCLJRMAP2BIC8cWAbqRbMAbCFrCBAbCFZl9zAQJHQRbMGXGXAGCG9HQBABAT87FBABCIJAb87FBABCGJAO87FBSFMAEATjGBAECNJAbjGBAECIJAOjGBMALCoBJAKCEWJHYAOjGBAYATjGIALAICGWJATjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAICFJHICbZCGWJAOjGBALCoBJAKCGJCbZCEWJHOATjGBAOAbjGIALAIAm9FAmCb6qJHICbZCGWJAbjGBAIAt9FAtCb6qJRIAKCEJRKMANCFJRNABCKJRBAECSJREAKCbZRKAICbZRIAfCEJHfAF9JQBMMCBC99AMAc6yRKMALCoFJ8kUUUUBAKM/tIFGa8jUUUUBCTlRLC9+RKGXAFCLJAI9LQBCaRKAE2BBC/+FZC/QF9HQBALhB83ENAECFJRKAEAIJC98JREGXAF9FQBGXAGCG6QBEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMALCNJAICFZCGWqHGAICGrCBAICFrCFZl9zAG8oGBJHIjGBABAIjGBABCIJRBAFCaJHFQBSGMMEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMABAICGrCBAICFrCFZl9zALCNJAICFZCGWqHI8oGBJHG87FBAIAGjGBABCGJRBAFCaJHFQBMMCBC99AKAE6yRKMAKM+lLKFaF99GaG99FaG99GXGXAGCI9HQBAF9FQFEXGXGX9DBBB8/9DBBB+/ABCGJHG1BB+yAB1BBHE+yHI+L+TABCFJHL1BBHK+yHO+L+THN9DBBBB9gHVyAN9DBB/+hANAN+U9DBBBBANAVyHcAc+MHMAECa3yAI+SHIAI+UAcAMAKCa3yAO+SHcAc+U+S+S+R+VHO+U+SHN+L9DBBB9P9d9FQBAN+oRESFMCUUUU94REMAGAE86BBGXGX9DBBB8/9DBBB+/Ac9DBBBB9gyAcAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMALAG86BBGXGX9DBBB8/9DBBB+/AI9DBBBB9gyAIAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMABAG86BBABCIJRBAFCaJHFQBSGMMAF9FQBEXGXGX9DBBB8/9DBBB+/ABCIJHG8uFB+yAB8uFBHE+yHI+L+TABCGJHL8uFBHK+yHO+L+THN9DBBBB9gHVyAN9DB/+g6ANAN+U9DBBBBANAVyHcAc+MHMAECa3yAI+SHIAI+UAcAMAKCa3yAO+SHcAc+U+S+S+R+VHO+U+SHN+L9DBBB9P9d9FQBAN+oRESFMCUUUU94REMAGAE87FBGXGX9DBBB8/9DBBB+/Ac9DBBBB9gyAcAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMALAG87FBGXGX9DBBB8/9DBBB+/AI9DBBBB9gyAIAO+U+SHN+L9DBBB9P9d9FQBAN+oRGSFMCUUUU94RGMABAG87FBABCNJRBAFCaJHFQBMMM/SEIEaE99EaF99GXAF9FQBCBREABRIEXGXGX9D/zI818/AICKJ8uFBHLCEq+y+VHKAI8uFB+y+UHO9DB/+g6+U9DBBB8/9DBBB+/AO9DBBBB9gy+SHN+L9DBBB9P9d9FQBAN+oRVSFMCUUUU94RVMAICIJ8uFBRcAICGJ8uFBRMABALCFJCEZAEqCFWJAV87FBGXGXAKAM+y+UHN9DB/+g6+U9DBBB8/9DBBB+/AN9DBBBB9gy+SHS+L9DBBB9P9d9FQBAS+oRMSFMCUUUU94RMMABALCGJCEZAEqCFWJAM87FBGXGXAKAc+y+UHK9DB/+g6+U9DBBB8/9DBBB+/AK9DBBBB9gy+SHS+L9DBBB9P9d9FQBAS+oRcSFMCUUUU94RcMABALCaJCEZAEqCFWJAc87FBGXGX9DBBU8/AOAO+U+TANAN+U+TAKAK+U+THO9DBBBBAO9DBBBB9gy+R9DB/+g6+U9DBBB8/+SHO+L9DBBB9P9d9FQBAO+oRcSFMCUUUU94RcMABALCEZAEqCFWJAc87FBAICNJRIAECIJREAFCaJHFQBMMM9JBGXAGCGrAF9sHF9FQBEXABAB8oGBHGCNWCN91+yAGCi91CnWCUUU/8EJ+++U84GBABCIJRBAFCaJHFQBMMM9TFEaCBCB8oGUkUUBHFABCEJC98ZJHBjGUkUUBGXGXAB8/BCTWHGuQBCaREABAGlCggEJCTrXBCa6QFMAFREMAEM/lFFFaGXGXAFABqCEZ9FQBABRESFMGXGXAGCT9PQBABRESFMABREEXAEAF8oGBjGBAECIJAFCIJ8oGBjGBAECNJAFCNJ8oGBjGBAECSJAFCSJ8oGBjGBAECTJREAFCTJRFAGC9wJHGCb9LQBMMAGCI9JQBEXAEAF8oGBjGBAFCIJRFAECIJREAGC98JHGCE9LQBMMGXAG9FQBEXAEAF2BB86BBAECFJREAFCFJRFAGCaJHGQBMMABMoFFGaGXGXABCEZ9FQBABRESFMAFCgFZC+BwsN9sRIGXGXAGCT9PQBABRESFMABREEXAEAIjGBAECSJAIjGBAECNJAIjGBAECIJAIjGBAECTJREAGC9wJHGCb9LQBMMAGCI9JQBEXAEAIjGBAECIJREAGC98JHGCE9LQBMMGXAG9FQBEXAEAF86BBAECFJREAGCaJHGQBMMABMMMFBCUNMIT9kBB'; const wasm_simd = 'B9h9z9tFBBBF8dL9gBB9gLaaaaaFa9gEaaaB9gGaaB9gFaFaEQSBBFBFFGEGEGIILF9wFFFLEFBFKNFaFCx/aFMO/LFVK9tv9t9vq95GBt9f9f939h9z9t9f9j9h9s9s9f9jW9vq9zBBp9tv9z9o9v9wW9f9kv9j9v9kv9WvqWv94h919m9mvqBG8Z9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv94h919m9mvqBIy9tv9z9o9v9wW9f9kv9j9v9kv9J9u9kv949TvZ91v9u9jvBLn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9P9jWBKi9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9R919hWBNn9tv9z9o9v9wW9f9kv9j9v9kv69p9sWvq9F949wBcI9z9iqlBMc/j9JSIBTEM9+FLa8jUUUUBCTlRBCBRFEXCBRGCBREEXABCNJAGJAECUaAFAGrCFZHIy86BBAEAIJREAGCFJHGCN9HQBMAFCx+YUUBJAE86BBAFCEWCxkUUBJAB8pEN83EBAFCFJHFCUG9HQBMMkRIbaG97FaK978jUUUUBCU/KBlHL8kUUUUBC9+RKGXAGCFJAI9LQBCaRKAE2BBC+gF9HQBALAEAIJHOAGlAG/8cBBCUoBAG9uC/wgBZHKCUGAKCUG9JyRNAECFJRKCBRVGXEXAVAF9PQFANAFAVlAVANJAF9JyRcGXGXAG9FQBAcCbJHIC9wZHMCE9sRSAMCFWRQAICIrCEJCGrRfCBRbEXAKRTCBRtGXEXGXAOATlAf9PQBCBRKSLMALCU/CBJAtAM9sJRmATAfJRKCBREGXAMCoB9JQBAOAKlC/gB9JQBCBRIEXAmAIJREGXGXGXGXGXATAICKrJ2BBHYCEZfIBFGEBMAECBDtDMIBSEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCIJAnDeBJAeCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCNJAnDeBJAeCx+YUUBJ2BBJRKSFMAEAKDBBBDMIBAKCTJRKMGXGXGXGXGXAYCGrCEZfIBFGEBMAECBDtDMITSEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMITAKCIJAnDeBJAeCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMITAKCNJAnDeBJAeCx+YUUBJ2BBJRKSFMAEAKDBBBDMITAKCTJRKMGXGXGXGXGXAYCIrCEZfIBFGEBMAECBDtDMIASEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIAAKCIJAnDeBJAeCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIAAKCNJAnDeBJAeCx+YUUBJ2BBJRKSFMAEAKDBBBDMIAAKCTJRKMGXGXGXGXGXAYCKrfIBFGEBMAECBDtDMI8wSEMAEAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HYCEWCxkUUBJDBEBAYCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HYCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMI8wAKCIJAnDeBJAYCx+YUUBJ2BBJRKSGMAEAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HYCEWCxkUUBJDBEBAYCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HYCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMI8wAKCNJAnDeBJAYCx+YUUBJ2BBJRKSFMAEAKDBBBDMI8wAKCTJRKMAICoBJREAICUFJAM9LQFAERIAOAKlC/fB9LQBMMGXAEAM9PQBAECErRIEXGXAOAKlCi9PQBCBRKSOMAmAEJRYGXGXGXGXGXATAECKrJ2BBAICKZrCEZfIBFGEBMAYCBDtDMIBSEMAYAKDBBIAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnHPCGD+MFAPDQBTFtGmEYIPLdKeOnC0+G+MiDtD9OHdCEDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCIJAnDeBJAeCx+YUUBJ2BBJRKSGMAYAKDBBNAKDBBBHPCID+MFAPDQBTFtGmEYIPLdKeOnC+P+e+8/4BDtD9OHdCbDbD8jHPD8dBhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBAeCx+YUUBJDBBBHnAnDQBBBBBBBBBBBBBBBBAPD8dFhUg/8/4/w/goB9+h84k7HeCEWCxkUUBJDBEBD9uDQBFGEILKOTtmYPdenDfAdAPD9SDMIBAKCNJAnDeBJAeCx+YUUBJ2BBJRKSFMAYAKDBBBDMIBAKCTJRKMAICGJRIAECTJHEAM9JQBMMGXAK9FQBAKRTAtCFJHtCI6QGSFMMCBRKSEMGXAM9FQBALCUGJAbJREALAbJDBGBRnCBRYEXAEALCU/CBJAYJHIDBIBHdCFD9tAdCFDbHPD9OD9hD9RHdAIAMJDBIBHiCFD9tAiAPD9OD9hD9RHiDQBTFtGmEYIPLdKeOnH8ZAIAQJDBIBHpCFD9tApAPD9OD9hD9RHpAIASJDBIBHyCFD9tAyAPD9OD9hD9RHyDQBTFtGmEYIPLdKeOnH8cDQBFTtGEmYILPdKOenHPAPDQBFGEBFGEBFGEBFGEAnD9uHnDyBjGBAEAGJHIAnAPAPDQILKOILKOILKOILKOD9uHnDyBjGBAIAGJHIAnAPAPDQNVcMNVcMNVcMNVcMD9uHnDyBjGBAIAGJHIAnAPAPDQSQfbSQfbSQfbSQfbD9uHnDyBjGBAIAGJHIAnA8ZA8cDQNVi8ZcMpySQ8c8dfb8e8fHPAPDQBFGEBFGEBFGEBFGED9uHnDyBjGBAIAGJHIAnAPAPDQILKOILKOILKOILKOD9uHnDyBjGBAIAGJHIAnAPAPDQNVcMNVcMNVcMNVcMD9uHnDyBjGBAIAGJHIAnAPAPDQSQfbSQfbSQfbSQfbD9uHnDyBjGBAIAGJHIAnAdAiDQNiV8ZcpMyS8cQ8df8eb8fHdApAyDQNiV8ZcpMyS8cQ8df8eb8fHiDQBFTtGEmYILPdKOenHPAPDQBFGEBFGEBFGEBFGED9uHnDyBjGBAIAGJHIAnAPAPDQILKOILKOILKOILKOD9uHnDyBjGBAIAGJHIAnAPAPDQNVcMNVcMNVcMNVcMD9uHnDyBjGBAIAGJHIAnAPAPDQSQfbSQfbSQfbSQfbD9uHnDyBjGBAIAGJHIAnAdAiDQNVi8ZcMpySQ8c8dfb8e8fHPAPDQBFGEBFGEBFGEBFGED9uHnDyBjGBAIAGJHIAnAPAPDQILKOILKOILKOILKOD9uHnDyBjGBAIAGJHIAnAPAPDQNVcMNVcMNVcMNVcMD9uHnDyBjGBAIAGJHIAnAPAPDQSQfbSQfbSQfbSQfbD9uHnDyBjGBAIAGJREAYCTJHYAM9JQBMMAbCIJHbAG9JQBMMABAVAG9sJALCUGJAcAG9s/8cBBALALCUGJAcCaJAG9sJAG/8cBBMAcCBAKyAVJRVAKQBMC9+RKSFMCBC99AOAKlAGCAAGCA9Ly6yRKMALCU/KBJ8kUUUUBAKMNBT+BUUUBM+KmFTa8jUUUUBCoFlHL8kUUUUBC9+RKGXAFCE9uHOCtJAI9LQBCaRKAE2BBHNC/wFZC/gF9HQBANCbZHVCF9LQBALCoBJCgFCUF/8MBALC84Jha83EBALC8wJha83EBALC8oJha83EBALCAJha83EBALCiJha83EBALCTJha83EBALha83ENALha83EBAEAIJC9wJRcAECFJHNAOJRMGXAF9FQBCQCbAVCF6yRSABRECBRVCBRQCBRfCBRICBRKEXGXAMAcuQBC9+RKSEMGXGXAN2BBHOC/vF9LQBALCoBJAOCIrCa9zAKJCbZCEWJHb8oGIRTAb8oGBRtGXAOCbZHbAS9PQBALAOCa9zAIJCbZCGWJ8oGBAVAbyROAb9FRbGXGXAGCG9HQBABAt87FBABCIJAO87FBABCGJAT87FBSFMAEAtjGBAECNJAOjGBAECIJATjGBMAVAbJRVALCoBJAKCEWJHmAOjGBAmATjGIALAICGWJAOjGBALCoBJAKCFJCbZHKCEWJHTAtjGBATAOjGIAIAbJRIAKCFJRKSGMGXGXAbCb6QBAQAbJAbC989zJCFJRQSFMAM1BBHbCgFZROGXGXAbCa9MQBAMCFJRMSFMAM1BFHbCgBZCOWAOCgBZqROGXAbCa9MQBAMCGJRMSFMAM1BGHbCgBZCfWAOqROGXAbCa9MQBAMCEJRMSFMAM1BEHbCgBZCdWAOqROGXAbCa9MQBAMCIJRMSFMAM2BIC8cWAOqROAMCLJRMMAOCFrCBAOCFZl9zAQJRQMGXGXAGCG9HQBABAt87FBABCIJAQ87FBABCGJAT87FBSFMAEAtjGBAECNJAQjGBAECIJATjGBMALCoBJAKCEWJHOAQjGBAOATjGIALAICGWJAQjGBALCoBJAKCFJCbZHKCEWJHOAtjGBAOAQjGIAICFJRIAKCFJRKSFMGXAOCDF9LQBALAIAcAOCbZJ2BBHbCIrHTlCbZCGWJ8oGBAVCFJHtATyROALAIAblCbZCGWJ8oGBAtAT9FHmJHtAbCbZHTyRbAT9FRTGXGXAGCG9HQBABAV87FBABCIJAb87FBABCGJAO87FBSFMAEAVjGBAECNJAbjGBAECIJAOjGBMALAICGWJAVjGBALCoBJAKCEWJHYAOjGBAYAVjGIALAICFJHICbZCGWJAOjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAIAmJCbZHICGWJAbjGBALCoBJAKCGJCbZHKCEWJHOAVjGBAOAbjGIAKCFJRKAIATJRIAtATJRVSFMAVCBAM2BBHYyHTAOC/+F6HPJROAYCbZRtGXGXAYCIrHmQBAOCFJRbSFMAORbALAIAmlCbZCGWJ8oGBROMGXGXAtQBAbCFJRVSFMAbRVALAIAYlCbZCGWJ8oGBRbMGXGXAP9FQBAMCFJRYSFMAM1BFHYCgFZRTGXGXAYCa9MQBAMCGJRYSFMAM1BGHYCgBZCOWATCgBZqRTGXAYCa9MQBAMCEJRYSFMAM1BEHYCgBZCfWATqRTGXAYCa9MQBAMCIJRYSFMAM1BIHYCgBZCdWATqRTGXAYCa9MQBAMCLJRYSFMAMCKJRYAM2BLC8cWATqRTMATCFrCBATCFZl9zAQJHQRTMGXGXAmCb6QBAYRPSFMAY1BBHMCgFZROGXGXAMCa9MQBAYCFJRPSFMAY1BFHMCgBZCOWAOCgBZqROGXAMCa9MQBAYCGJRPSFMAY1BGHMCgBZCfWAOqROGXAMCa9MQBAYCEJRPSFMAY1BEHMCgBZCdWAOqROGXAMCa9MQBAYCIJRPSFMAYCLJRPAY2BIC8cWAOqROMAOCFrCBAOCFZl9zAQJHQROMGXGXAtCb6QBAPRMSFMAP1BBHMCgFZRbGXGXAMCa9MQBAPCFJRMSFMAP1BFHMCgBZCOWAbCgBZqRbGXAMCa9MQBAPCGJRMSFMAP1BGHMCgBZCfWAbqRbGXAMCa9MQBAPCEJRMSFMAP1BEHMCgBZCdWAbqRbGXAMCa9MQBAPCIJRMSFMAPCLJRMAP2BIC8cWAbqRbMAbCFrCBAbCFZl9zAQJHQRbMGXGXAGCG9HQBABAT87FBABCIJAb87FBABCGJAO87FBSFMAEATjGBAECNJAbjGBAECIJAOjGBMALCoBJAKCEWJHYAOjGBAYATjGIALAICGWJATjGBALCoBJAKCFJCbZCEWJHYAbjGBAYAOjGIALAICFJHICbZCGWJAOjGBALCoBJAKCGJCbZCEWJHOATjGBAOAbjGIALAIAm9FAmCb6qJHICbZCGWJAbjGBAIAt9FAtCb6qJRIAKCEJRKMANCFJRNABCKJRBAECSJREAKCbZRKAICbZRIAfCEJHfAF9JQBMMCBC99AMAc6yRKMALCoFJ8kUUUUBAKM/tIFGa8jUUUUBCTlRLC9+RKGXAFCLJAI9LQBCaRKAE2BBC/+FZC/QF9HQBALhB83ENAECFJRKAEAIJC98JREGXAF9FQBGXAGCG6QBEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMALCNJAICFZCGWqHGAICGrCBAICFrCFZl9zAG8oGBJHIjGBABAIjGBABCIJRBAFCaJHFQBSGMMEXGXAKAE9JQBC9+bMAK1BBHGCgFZRIGXGXAGCa9MQBAKCFJRKSFMAK1BFHGCgBZCOWAICgBZqRIGXAGCa9MQBAKCGJRKSFMAK1BGHGCgBZCfWAIqRIGXAGCa9MQBAKCEJRKSFMAK1BEHGCgBZCdWAIqRIGXAGCa9MQBAKCIJRKSFMAK2BIC8cWAIqRIAKCLJRKMABAICGrCBAICFrCFZl9zALCNJAICFZCGWqHI8oGBJHG87FBAIAGjGBABCGJRBAFCaJHFQBMMCBC99AKAE6yRKMAKM/xLGEaK978jUUUUBCAlHE8kUUUUBGXGXAGCI9HQBGXAFC98ZHI9FQBABRGCBRLEXAGAGDBBBHKCiD+rFCiD+sFD/6FHOAKCND+rFCiD+sFD/6FAOD/gFAKCTD+rFCiD+sFD/6FHND/gFD/kFD/lFHVCBDtD+2FHcAOCUUUU94DtHMD9OD9RD/kFHO9DBB/+hDYAOAOD/mFAVAVD/mFANAcANAMD9OD9RD/kFHOAOD/mFD/kFD/kFD/jFD/nFHND/mF9DBBX9LDYHcD/kFCgFDtD9OAKCUUU94DtD9OD9QAOAND/mFAcD/kFCND+rFCU/+EDtD9OD9QAVAND/mFAcD/kFCTD+rFCUU/8ODtD9OD9QDMBBAGCTJRGALCIJHLAI9JQBMMAIAF9PQFAEAFCEZHLCGWHGqCBCTAGl/8MBAEABAICGWJHIAG/8cBBGXAL9FQBAEAEDBIBHKCiD+rFCiD+sFD/6FHOAKCND+rFCiD+sFD/6FAOD/gFAKCTD+rFCiD+sFD/6FHND/gFD/kFD/lFHVCBDtD+2FHcAOCUUUU94DtHMD9OD9RD/kFHO9DBB/+hDYAOAOD/mFAVAVD/mFANAcANAMD9OD9RD/kFHOAOD/mFD/kFD/kFD/jFD/nFHND/mF9DBBX9LDYHcD/kFCgFDtD9OAKCUUU94DtD9OD9QAOAND/mFAcD/kFCND+rFCU/+EDtD9OD9QAVAND/mFAcD/kFCTD+rFCUU/8ODtD9OD9QDMIBMAIAEAG/8cBBSFMABAFC98ZHGT+HUUUBAGAF9PQBAEAFCEZHICEWHLJCBCAALl/8MBAEABAGCEWJHGAL/8cBBAEAIT+HUUUBAGAEAL/8cBBMAECAJ8kUUUUBM+yEGGaO97GXAF9FQBCBRGEXABCTJHEAEDBBBHICBDtHLCUU98D8cFCUU98D8cEHKD9OABDBBBHOAIDQILKOSQfbPden8c8d8e8fCggFDtD9OD/6FAOAIDQBFGENVcMTtmYi8ZpyHICTD+sFD/6FHND/gFAICTD+rFCTD+sFD/6FHVD/gFD/kFD/lFHI9DB/+g6DYAVAIALD+2FHLAVCUUUU94DtHcD9OD9RD/kFHVAVD/mFAIAID/mFANALANAcD9OD9RD/kFHIAID/mFD/kFD/kFD/jFD/nFHND/mF9DBBX9LDYHLD/kFCTD+rFAVAND/mFALD/kFCggEDtD9OD9QHVAIAND/mFALD/kFCaDbCBDnGCBDnECBDnKCBDnOCBDncCBDnMCBDnfCBDnbD9OHIDQNVi8ZcMpySQ8c8dfb8e8fD9QDMBBABAOAKD9OAVAIDQBFTtGEmYILPdKOenD9QDMBBABCAJRBAGCIJHGAF9JQBMMM94FEa8jUUUUBCAlHE8kUUUUBABAFC98ZHIT+JUUUBGXAIAF9PQBAEAFCEZHLCEWHFJCBCAAFl/8MBAEABAICEWJHBAF/8cBBAEALT+JUUUBABAEAF/8cBBMAECAJ8kUUUUBM/hEIGaF97FaL978jUUUUBCTlRGGXAF9FQBCBREEXAGABDBBBHIABCTJHLDBBBHKDQILKOSQfbPden8c8d8e8fHOCTD+sFHNCID+rFDMIBAB9DBBU8/DY9D/zI818/DYANCEDtD9QD/6FD/nFHNAIAKDQBFGENVcMTtmYi8ZpyHICTD+rFCTD+sFD/6FD/mFHKAKD/mFANAICTD+sFD/6FD/mFHVAVD/mFANAOCTD+rFCTD+sFD/6FD/mFHOAOD/mFD/kFD/kFD/lFCBDtD+4FD/jF9DB/+g6DYHND/mF9DBBX9LDYHID/kFCggEDtHcD9OAVAND/mFAID/kFCTD+rFD9QHVAOAND/mFAID/kFCTD+rFAKAND/mFAID/kFAcD9OD9QHNDQBFTtGEmYILPdKOenHID8dBAGDBIBDyB+t+J83EBABCNJAID8dFAGDBIBDyF+t+J83EBALAVANDQNVi8ZcMpySQ8c8dfb8e8fHND8dBAGDBIBDyG+t+J83EBABCiJAND8dFAGDBIBDyE+t+J83EBABCAJRBAECIJHEAF9JQBMMM/3FGEaF978jUUUUBCoBlREGXAGCGrAF9sHIC98ZHL9FQBCBRGABRFEXAFAFDBBBHKCND+rFCND+sFD/6FAKCiD+sFCnD+rFCUUU/8EDtD+uFD/mFDMBBAFCTJRFAGCIJHGAL9JQBMMGXALAI9PQBAEAICEZHGCGWHFqCBCoBAFl/8MBAEABALCGWJHLAF/8cBBGXAG9FQBAEAEDBIBHKCND+rFCND+sFD/6FAKCiD+sFCnD+rFCUUU/8EDtD+uFD/mFDMIBMALAEAF/8cBBMM9TFEaCBCB8oGUkUUBHFABCEJC98ZJHBjGUkUUBGXGXAB8/BCTWHGuQBCaREABAGlCggEJCTrXBCa6QFMAFREMAEMMMFBCUNMIT9tBB'; const detector = new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0, 1, 4, 1, 96, 0, 0, 3, 3, 2, 0, 0, 5, 3, 1, 0, 1, 12, 1, 0, 10, 22, 2, 12, 0, 65, 0, 65, 0, 65, 0, 252, 10, 0, 0, 11, 7, 0, 65, 0, 253, 15, 26, 11]); const wasmpack = new Uint8Array([32, 0, 65, 253, 3, 1, 2, 34, 4, 106, 6, 5, 11, 8, 7, 20, 13, 33, 12, 16, 128, 9, 116, 64, 19, 113, 127, 15, 10, 21, 22, 14, 255, 66, 24, 54, 136, 107, 18, 23, 192, 26, 114, 118, 132, 17, 77, 101, 130, 144, 27, 87, 131, 44, 45, 74, 156, 154, 70, 167]); const FILTERS = { 0: '', 1: 'meshopt_decodeFilterOct', 2: 'meshopt_decodeFilterQuat', 3: 'meshopt_decodeFilterExp', NONE: '', OCTAHEDRAL: 'meshopt_decodeFilterOct', QUATERNION: 'meshopt_decodeFilterQuat', EXPONENTIAL: 'meshopt_decodeFilterExp' }; const DECODERS = { 0: 'meshopt_decodeVertexBuffer', 1: 'meshopt_decodeIndexBuffer', 2: 'meshopt_decodeIndexSequence', ATTRIBUTES: 'meshopt_decodeVertexBuffer', TRIANGLES: 'meshopt_decodeIndexBuffer', INDICES: 'meshopt_decodeIndexSequence' }; function isMeshoptSupported() { return isWebAssemblySupported; } async function meshoptDecodeGltfBuffer(target, count, size, source, mode, filter = 'NONE') { const instance = await loadWasmInstance(); decode$5(instance, instance.exports[DECODERS[mode]], target, count, size, source, instance.exports[FILTERS[filter || 'NONE']]); } let wasmPromise; async function loadWasmInstance() { if (!wasmPromise) { wasmPromise = loadWasmModule(); } return wasmPromise; } async function loadWasmModule() { let wasm = wasm_base; if (WebAssembly.validate(detector)) { wasm = wasm_simd; console.log('Warning: meshopt_decoder is using experimental SIMD support'); } const result = await WebAssembly.instantiate(unpack(wasm), {}); await result.instance.exports.__wasm_call_ctors(); return result.instance; } function unpack(data) { const result = new Uint8Array(data.length); for (let i = 0; i < data.length; ++i) { const ch = data.charCodeAt(i); result[i] = ch > 96 ? ch - 71 : ch > 64 ? ch - 65 : ch > 47 ? ch + 4 : ch > 46 ? 63 : 62; } let write = 0; for (let i = 0; i < data.length; ++i) { result[write++] = result[i] < 60 ? wasmpack[result[i]] : (result[i] - 60) * 64 + result[++i]; } return result.buffer.slice(0, write); } function decode$5(instance, fun, target, count, size, source, filter) { const sbrk = instance.exports.sbrk; const count4 = count + 3 & ~3; const tp = sbrk(count4 * size); const sp = sbrk(source.length); const heap = new Uint8Array(instance.exports.memory.buffer); heap.set(source, sp); const res = fun(tp, count, size, sp, source.length); if (res === 0 && filter) { filter(tp, count4, size); } target.set(heap.subarray(tp, tp + count * size)); sbrk(tp - sbrk(0)); if (res !== 0) { throw new Error("Malformed buffer data: ".concat(res)); } } const EXT_MESHOPT_COMPRESSION = 'EXT_meshopt_compression'; const name$6 = EXT_MESHOPT_COMPRESSION; function preprocess$4(gltfData) { const scenegraph = new GLTFScenegraph(gltfData); if (scenegraph.getRequiredExtensions().includes(EXT_MESHOPT_COMPRESSION) && !isMeshoptSupported()) { throw new Error("gltf: Required extension ".concat(EXT_MESHOPT_COMPRESSION, " not supported by browser")); } } async function decode$4(gltfData, options) { var _options$gltf; const scenegraph = new GLTFScenegraph(gltfData); if (!(options !== null && options !== void 0 && (_options$gltf = options.gltf) !== null && _options$gltf !== void 0 && _options$gltf.decompressMeshes)) { return; } const promises = []; for (const bufferViewIndex of gltfData.json.bufferViews || []) { promises.push(decodeMeshoptBufferView(scenegraph, bufferViewIndex)); } await Promise.all(promises); scenegraph.removeExtension(EXT_MESHOPT_COMPRESSION); } async function decodeMeshoptBufferView(scenegraph, bufferView) { const meshoptExtension = scenegraph.getObjectExtension(bufferView, EXT_MESHOPT_COMPRESSION); if (meshoptExtension) { const buffer = bufferView.buffer; const { byteOffset = 0, byteLength = 0, byteStride, count, mode, filter = 'NONE' } = meshoptExtension; const source = new Uint8Array(buffer, byteOffset, byteLength); const result = new ArrayBuffer(count * byteStride); await meshoptDecodeGltfBuffer(new Uint8Array(result), count, byteStride, source, mode, filter); return result; } return null; } var EXT_meshopt_compression = /*#__PURE__*/Object.freeze({ __proto__: null, name: name$6, preprocess: preprocess$4, decode: decode$4 }); const EXT_TEXTURE_WEBP = 'EXT_texture_webp'; const name$5 = EXT_TEXTURE_WEBP; function preprocess$3(gltfData, options) { const scenegraph = new GLTFScenegraph(gltfData); if (!_isImageFormatSupported('image/webp')) { if (scenegraph.getRequiredExtensions().includes(EXT_TEXTURE_WEBP)) { throw new Error("gltf: Required extension ".concat(EXT_TEXTURE_WEBP, " not supported by browser")); } return; } const { json } = scenegraph; for (const texture of json.textures || []) { const extension = scenegraph.getObjectExtension(texture, EXT_TEXTURE_WEBP); if (extension) { texture.source = extension.source; } scenegraph.removeObjectExtension(texture, EXT_TEXTURE_WEBP); } scenegraph.removeExtension(EXT_TEXTURE_WEBP); } var EXT_texture_webp = /*#__PURE__*/Object.freeze({ __proto__: null, name: name$5, preprocess: preprocess$3 }); const KHR_TEXTURE_BASISU = 'KHR_texture_basisu'; const name$4 = KHR_TEXTURE_BASISU; function preprocess$2(gltfData, options) { const scene = new GLTFScenegraph(gltfData); const { json } = scene; for (const texture of json.textures || []) { const extension = scene.getObjectExtension(texture, KHR_TEXTURE_BASISU); if (extension) { texture.source = extension.source; } scene.removeObjectExtension(texture, KHR_TEXTURE_BASISU); } scene.removeExtension(KHR_TEXTURE_BASISU); } var KHR_texture_basisu = /*#__PURE__*/Object.freeze({ __proto__: null, name: name$4, preprocess: preprocess$2 }); function getGLTFAccessors(attributes) { const accessors = {}; for (const name in attributes) { const attribute = attributes[name]; if (name !== 'indices') { const glTFAccessor = getGLTFAccessor(attribute); accessors[name] = glTFAccessor; } } return accessors; } function getGLTFAccessor(attribute) { const { buffer, size, count } = getAccessorData(attribute); const glTFAccessor = { value: buffer, size, byteOffset: 0, count, type: getAccessorTypeFromSize(size), componentType: getComponentTypeFromArray(buffer) }; return glTFAccessor; } function getAccessorData(attribute) { let buffer = attribute; let size = 1; let count = 0; if (attribute && attribute.value) { buffer = attribute.value; size = attribute.size || 1; } if (buffer) { if (!ArrayBuffer.isView(buffer)) { buffer = toTypedArray(buffer, Float32Array); } count = buffer.length / size; } return { buffer, size, count }; } function toTypedArray(array, ArrayType, convertTypedArrays = false) { if (!array) { return null; } if (Array.isArray(array)) { return new ArrayType(array); } if (convertTypedArrays && !(array instanceof ArrayType)) { return new ArrayType(array); } return array; } const KHR_DRACO_MESH_COMPRESSION = 'KHR_draco_mesh_compression'; const name$3 = KHR_DRACO_MESH_COMPRESSION; function preprocess$1(gltfData, options, context) { const scenegraph = new GLTFScenegraph(gltfData); for (const primitive of makeMeshPrimitiveIterator(scenegraph)) { if (scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION)) ; } } async function decode$3(gltfData, options, context) { var _options$gltf; if (!(options !== null && options !== void 0 && (_options$gltf = options.gltf) !== null && _options$gltf !== void 0 && _options$gltf.decompressMeshes)) { return; } const scenegraph = new GLTFScenegraph(gltfData); const promises = []; for (const primitive of makeMeshPrimitiveIterator(scenegraph)) { if (scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION)) { promises.push(decompressPrimitive(scenegraph, primitive, options, context)); } } await Promise.all(promises); scenegraph.removeExtension(KHR_DRACO_MESH_COMPRESSION); } function encode$3(gltfData, options = {}) { const scenegraph = new GLTFScenegraph(gltfData); for (const mesh of scenegraph.json.meshes || []) { compressMesh(mesh); scenegraph.addRequiredExtension(KHR_DRACO_MESH_COMPRESSION); } } async function decompressPrimitive(scenegraph, primitive, options, context) { const dracoExtension = scenegraph.getObjectExtension(primitive, KHR_DRACO_MESH_COMPRESSION); if (!dracoExtension) { return; } const buffer = scenegraph.getTypedArrayForBufferView(dracoExtension.bufferView); const bufferCopy = sliceArrayBuffer(buffer.buffer, buffer.byteOffset); const { parse } = context; const dracoOptions = { ...options }; delete dracoOptions['3d-tiles']; const decodedData = await parse(bufferCopy, DracoLoader, dracoOptions, context); const decodedAttributes = getGLTFAccessors(decodedData.attributes); for (const [attributeName, decodedAttribute] of Object.entries(decodedAttributes)) { if (attributeName in primitive.attributes) { const accessorIndex = primitive.attributes[attributeName]; const accessor = scenegraph.getAccessor(accessorIndex); if (accessor !== null && accessor !== void 0 && accessor.min && accessor !== null && accessor !== void 0 && accessor.max) { decodedAttribute.min = accessor.min; decodedAttribute.max = accessor.max; } } } primitive.attributes = decodedAttributes; if (decodedData.indices) { primitive.indices = getGLTFAccessor(decodedData.indices); } checkPrimitive(primitive); } function compressMesh(attributes, indices, mode = 4, options, context) { var _context$parseSync; if (!options.DracoWriter) { throw new Error('options.gltf.DracoWriter not provided'); } const compressedData = options.DracoWriter.encodeSync({ attributes }); const decodedData = context === null || context === void 0 ? void 0 : (_context$parseSync = context.parseSync) === null || _context$parseSync === void 0 ? void 0 : _context$parseSync.call(context, { attributes }); const fauxAccessors = options._addFauxAttributes(decodedData.attributes); const bufferViewIndex = options.addBufferView(compressedData); const glTFMesh = { primitives: [{ attributes: fauxAccessors, mode, extensions: { [KHR_DRACO_MESH_COMPRESSION]: { bufferView: bufferViewIndex, attributes: fauxAccessors } } }] }; return glTFMesh; } function checkPrimitive(primitive) { if (!primitive.attributes && Object.keys(primitive.attributes).length > 0) { throw new Error('glTF: Empty primitive detected: Draco decompression failure?'); } } function* makeMeshPrimitiveIterator(scenegraph) { for (const mesh of scenegraph.json.meshes || []) { for (const primitive of mesh.primitives) { yield primitive; } } } var KHR_draco_mesh_compression = /*#__PURE__*/Object.freeze({ __proto__: null, name: name$3, preprocess: preprocess$1, decode: decode$3, encode: encode$3 }); const KHR_LIGHTS_PUNCTUAL = 'KHR_lights_punctual'; const name$2 = KHR_LIGHTS_PUNCTUAL; async function decode$2(gltfData) { const gltfScenegraph = new GLTFScenegraph(gltfData); const { json } = gltfScenegraph; const extension = gltfScenegraph.getExtension(KHR_LIGHTS_PUNCTUAL); if (extension) { gltfScenegraph.json.lights = extension.lights; gltfScenegraph.removeExtension(KHR_LIGHTS_PUNCTUAL); } for (const node of json.nodes || []) { const nodeExtension = gltfScenegraph.getObjectExtension(node, KHR_LIGHTS_PUNCTUAL); if (nodeExtension) { node.light = nodeExtension.light; } gltfScenegraph.removeObjectExtension(node, KHR_LIGHTS_PUNCTUAL); } } async function encode$2(gltfData) { const gltfScenegraph = new GLTFScenegraph(gltfData); const { json } = gltfScenegraph; if (json.lights) { const extension = gltfScenegraph.addExtension(KHR_LIGHTS_PUNCTUAL); assert$1(!extension.lights); extension.lights = json.lights; delete json.lights; } if (gltfScenegraph.json.lights) { for (const light of gltfScenegraph.json.lights) { const node = light.node; gltfScenegraph.addObjectExtension(node, KHR_LIGHTS_PUNCTUAL, light); } delete gltfScenegraph.json.lights; } } var KHR_lights_punctual = /*#__PURE__*/Object.freeze({ __proto__: null, name: name$2, decode: decode$2, encode: encode$2 }); const KHR_MATERIALS_UNLIT = 'KHR_materials_unlit'; const name$1 = KHR_MATERIALS_UNLIT; async function decode$1(gltfData) { const gltfScenegraph = new GLTFScenegraph(gltfData); const { json } = gltfScenegraph; gltfScenegraph.removeExtension(KHR_MATERIALS_UNLIT); for (const material of json.materials || []) { const extension = material.extensions && material.extensions.KHR_materials_unlit; if (extension) { material.unlit = true; } gltfScenegraph.removeObjectExtension(material, KHR_MATERIALS_UNLIT); } } function encode$1(gltfData) { const gltfScenegraph = new GLTFScenegraph(gltfData); const { json } = gltfScenegraph; if (gltfScenegraph.materials) { for (const material of json.materials || []) { if (material.unlit) { delete material.unlit; gltfScenegraph.addObjectExtension(material, KHR_MATERIALS_UNLIT, {}); gltfScenegraph.addExtension(KHR_MATERIALS_UNLIT); } } } } var KHR_materials_unlit = /*#__PURE__*/Object.freeze({ __proto__: null, name: name$1, decode: decode$1, encode: encode$1 }); const KHR_TECHNIQUES_WEBGL = 'KHR_techniques_webgl'; const name$7 = KHR_TECHNIQUES_WEBGL; async function decode(gltfData) { const gltfScenegraph = new GLTFScenegraph(gltfData); const { json } = gltfScenegraph; const extension = gltfScenegraph.getExtension(KHR_TECHNIQUES_WEBGL); if (extension) { const techniques = resolveTechniques(extension, gltfScenegraph); for (const material of json.materials || []) { const materialExtension = gltfScenegraph.getObjectExtension(material, KHR_TECHNIQUES_WEBGL); if (materialExtension) { material.technique = Object.assign({}, materialExtension, techniques[materialExtension.technique]); material.technique.values = resolveValues(material.technique, gltfScenegraph); } gltfScenegraph.removeObjectExtension(material, KHR_TECHNIQUES_WEBGL); } gltfScenegraph.removeExtension(KHR_TECHNIQUES_WEBGL); } } async function encode(gltfData, options) {} function resolveTechniques(techniquesExtension, gltfScenegraph) { const { programs = [], shaders = [], techniques = [] } = techniquesExtension; const textDecoder = new TextDecoder(); shaders.forEach(shader => { if (Number.isFinite(shader.bufferView)) { shader.code = textDecoder.decode(gltfScenegraph.getTypedArrayForBufferView(shader.bufferView)); } else { throw new Error('KHR_techniques_webgl: no shader code'); } }); programs.forEach(program => { program.fragmentShader = shaders[program.fragmentShader]; program.vertexShader = shaders[program.vertexShader]; }); techniques.forEach(technique => { technique.program = programs[technique.program]; }); return techniques; } function resolveValues(technique, gltfScenegraph) { const values = Object.assign({}, technique.values); Object.keys(technique.uniforms || {}).forEach(uniform => { if (technique.uniforms[uniform].value && !(uniform in values)) { values[uniform] = technique.uniforms[uniform].value; } }); Object.keys(values).forEach(uniform => { if (typeof values[uniform] === 'object' && values[uniform].index !== undefined) { values[uniform].texture = gltfScenegraph.getTexture(values[uniform].index); } }); return values; } var KHR_techniques_webgl = /*#__PURE__*/Object.freeze({ __proto__: null, name: name$7, decode: decode, encode: encode }); const EXTENSIONS$2 = [EXT_meshopt_compression, EXT_texture_webp, KHR_texture_basisu, KHR_draco_mesh_compression, KHR_lights_punctual, KHR_materials_unlit, KHR_techniques_webgl]; function preprocessExtensions(gltf, options = {}, context) { const extensions = EXTENSIONS$2.filter(extension => useExtension(extension.name, options)); for (const extension of extensions) { var _extension$preprocess; (_extension$preprocess = extension.preprocess) === null || _extension$preprocess === void 0 ? void 0 : _extension$preprocess.call(extension, gltf, options, context); } } async function decodeExtensions(gltf, options = {}, context) { const extensions = EXTENSIONS$2.filter(extension => useExtension(extension.name, options)); for (const extension of extensions) { var _extension$decode; await ((_extension$decode = extension.decode) === null || _extension$decode === void 0 ? void 0 : _extension$decode.call(extension, gltf, options, context)); } } function useExtension(extensionName, options) { var _options$gltf; const excludes = (options === null || options === void 0 ? void 0 : (_options$gltf = options.gltf) === null || _options$gltf === void 0 ? void 0 : _options$gltf.excludeExtensions) || {}; const exclude = extensionName in excludes && !excludes[extensionName]; return !exclude; } const KHR_BINARY_GLTF = 'KHR_binary_glTF'; function preprocess(gltfData) { const gltfScenegraph = new GLTFScenegraph(gltfData); const { json } = gltfScenegraph; for (const image of json.images || []) { const extension = gltfScenegraph.getObjectExtension(image, KHR_BINARY_GLTF); if (extension) { Object.assign(image, extension); } gltfScenegraph.removeObjectExtension(image, KHR_BINARY_GLTF); } if (json.buffers && json.buffers[0]) { delete json.buffers[0].uri; } gltfScenegraph.removeExtension(KHR_BINARY_GLTF); } const GLTF_ARRAYS = { accessors: 'accessor', animations: 'animation', buffers: 'buffer', bufferViews: 'bufferView', images: 'image', materials: 'material', meshes: 'mesh', nodes: 'node', samplers: 'sampler', scenes: 'scene', skins: 'skin', textures: 'texture' }; const GLTF_KEYS = { accessor: 'accessors', animations: 'animation', buffer: 'buffers', bufferView: 'bufferViews', image: 'images', material: 'materials', mesh: 'meshes', node: 'nodes', sampler: 'samplers', scene: 'scenes', skin: 'skins', texture: 'textures' }; class GLTFV1Normalizer { constructor() { _defineProperty(this, "idToIndexMap", { animations: {}, accessors: {}, buffers: {}, bufferViews: {}, images: {}, materials: {}, meshes: {}, nodes: {}, samplers: {}, scenes: {}, skins: {}, textures: {} }); _defineProperty(this, "json", void 0); } normalize(gltf, options) { this.json = gltf.json; const json = gltf.json; switch (json.asset && json.asset.version) { case '2.0': return; case undefined: case '1.0': break; default: console.warn("glTF: Unknown version ".concat(json.asset.version)); return; } if (!options.normalize) { throw new Error('glTF v1 is not supported.'); } console.warn('Converting glTF v1 to glTF v2 format. This is experimental and may fail.'); this._addAsset(json); this._convertTopLevelObjectsToArrays(json); preprocess(gltf); this._convertObjectIdsToArrayIndices(json); this._updateObjects(json); this._updateMaterial(json); } _addAsset(json) { json.asset = json.asset || {}; json.asset.version = '2.0'; json.asset.generator = json.asset.generator || 'Normalized to glTF 2.0 by loaders.gl'; } _convertTopLevelObjectsToArrays(json) { for (const arrayName in GLTF_ARRAYS) { this._convertTopLevelObjectToArray(json, arrayName); } } _convertTopLevelObjectToArray(json, mapName) { const objectMap = json[mapName]; if (!objectMap || Array.isArray(objectMap)) { return; } json[mapName] = []; for (const id in objectMap) { const object = objectMap[id]; object.id = object.id || id; const index = json[mapName].length; json[mapName].push(object); this.idToIndexMap[mapName][id] = index; } } _convertObjectIdsToArrayIndices(json) { for (const arrayName in GLTF_ARRAYS) { this._convertIdsToIndices(json, arrayName); } if ('scene' in json) { json.scene = this._convertIdToIndex(json.scene, 'scene'); } for (const texture of json.textures) { this._convertTextureIds(texture); } for (const mesh of json.meshes) { this._convertMeshIds(mesh); } for (const node of json.nodes) { this._convertNodeIds(node); } for (const node of json.scenes) { this._convertSceneIds(node); } } _convertTextureIds(texture) { if (texture.source) { texture.source = this._convertIdToIndex(texture.source, 'image'); } } _convertMeshIds(mesh) { for (const primitive of mesh.primitives) { const { attributes, indices, material } = primitive; for (const attributeName in attributes) { attributes[attributeName] = this._convertIdToIndex(attributes[attributeName], 'accessor'); } if (indices) { primitive.indices = this._convertIdToIndex(indices, 'accessor'); } if (material) { primitive.material = this._convertIdToIndex(material, 'material'); } } } _convertNodeIds(node) { if (node.children) { node.children = node.children.map(child => this._convertIdToIndex(child, 'node')); } if (node.meshes) { node.meshes = node.meshes.map(mesh => this._convertIdToIndex(mesh, 'mesh')); } } _convertSceneIds(scene) { if (scene.nodes) { scene.nodes = scene.nodes.map(node => this._convertIdToIndex(node, 'node')); } } _convertIdsToIndices(json, topLevelArrayName) { if (!json[topLevelArrayName]) { console.warn("gltf v1: json doesn't contain attribute ".concat(topLevelArrayName)); json[topLevelArrayName] = []; } for (const object of json[topLevelArrayName]) { for (const key in object) { const id = object[key]; const index = this._convertIdToIndex(id, key); object[key] = index; } } } _convertIdToIndex(id, key) { const arrayName = GLTF_KEYS[key]; if (arrayName in this.idToIndexMap) { const index = this.idToIndexMap[arrayName][id]; if (!Number.isFinite(index)) { throw new Error("gltf v1: failed to resolve ".concat(key, " with id ").concat(id)); } return index; } return id; } _updateObjects(json) { for (const buffer of this.json.buffers) { delete buffer.type; } } _updateMaterial(json) { for (const material of json.materials) { material.pbrMetallicRoughness = { baseColorFactor: [1, 1, 1, 1], metallicFactor: 1, roughnessFactor: 1 }; const textureId = material.values && material.values.tex; const textureIndex = json.textures.findIndex(texture => texture.id === textureId); if (textureIndex !== -1) { material.pbrMetallicRoughness.baseColorTexture = { index: textureIndex }; } } } } function normalizeGLTFV1(gltf, options = {}) { return new GLTFV1Normalizer().normalize(gltf, options); } const COMPONENTS = { SCALAR: 1, VEC2: 2, VEC3: 3, VEC4: 4, MAT2: 4, MAT3: 9, MAT4: 16 }; const BYTES = { 5120: 1, 5121: 1, 5122: 2, 5123: 2, 5125: 4, 5126: 4 }; const GL_SAMPLER = { TEXTURE_MAG_FILTER: 0x2800, TEXTURE_MIN_FILTER: 0x2801, TEXTURE_WRAP_S: 0x2802, TEXTURE_WRAP_T: 0x2803, REPEAT: 0x2901, LINEAR: 0x2601, NEAREST_MIPMAP_LINEAR: 0x2702 }; const SAMPLER_PARAMETER_GLTF_TO_GL = { magFilter: GL_SAMPLER.TEXTURE_MAG_FILTER, minFilter: GL_SAMPLER.TEXTURE_MIN_FILTER, wrapS: GL_SAMPLER.TEXTURE_WRAP_S, wrapT: GL_SAMPLER.TEXTURE_WRAP_T }; const DEFAULT_SAMPLER = { [GL_SAMPLER.TEXTURE_MAG_FILTER]: GL_SAMPLER.LINEAR, [GL_SAMPLER.TEXTURE_MIN_FILTER]: GL_SAMPLER.NEAREST_MIPMAP_LINEAR, [GL_SAMPLER.TEXTURE_WRAP_S]: GL_SAMPLER.REPEAT, [GL_SAMPLER.TEXTURE_WRAP_T]: GL_SAMPLER.REPEAT }; function getBytesFromComponentType(componentType) { return BYTES[componentType]; } function getSizeFromAccessorType(type) { return COMPONENTS[type]; } class GLTFPostProcessor { constructor() { _defineProperty(this, "baseUri", ''); _defineProperty(this, "json", {}); _defineProperty(this, "buffers", []); _defineProperty(this, "images", []); } postProcess(gltf, options = {}) { const { json, buffers = [], images = [], baseUri = '' } = gltf; assert$1(json); this.baseUri = baseUri; this.json = json; this.buffers = buffers; this.images = images; this._resolveTree(this.json, options); return this.json; } _resolveTree(json, options = {}) { if (json.bufferViews) { json.bufferViews = json.bufferViews.map((bufView, i) => this._resolveBufferView(bufView, i)); } if (json.images) { json.images = json.images.map((image, i) => this._resolveImage(image, i)); } if (json.samplers) { json.samplers = json.samplers.map((sampler, i) => this._resolveSampler(sampler, i)); } if (json.textures) { json.textures = json.textures.map((texture, i) => this._resolveTexture(texture, i)); } if (json.accessors) { json.accessors = json.accessors.map((accessor, i) => this._resolveAccessor(accessor, i)); } if (json.materials) { json.materials = json.materials.map((material, i) => this._resolveMaterial(material, i)); } if (json.meshes) { json.meshes = json.meshes.map((mesh, i) => this._resolveMesh(mesh, i)); } if (json.nodes) { json.nodes = json.nodes.map((node, i) => this._resolveNode(node, i)); } if (json.skins) { json.skins = json.skins.map((skin, i) => this._resolveSkin(skin, i)); } if (json.scenes) { json.scenes = json.scenes.map((scene, i) => this._resolveScene(scene, i)); } if (json.scene !== undefined) { json.scene = json.scenes[this.json.scene]; } } getScene(index) { return this._get('scenes', index); } getNode(index) { return this._get('nodes', index); } getSkin(index) { return this._get('skins', index); } getMesh(index) { return this._get('meshes', index); } getMaterial(index) { return this._get('materials', index); } getAccessor(index) { return this._get('accessors', index); } getCamera(index) { return null; } getTexture(index) { return this._get('textures', index); } getSampler(index) { return this._get('samplers', index); } getImage(index) { return this._get('images', index); } getBufferView(index) { return this._get('bufferViews', index); } getBuffer(index) { return this._get('buffers', index); } _get(array, index) { if (typeof index === 'object') { return index; } const object = this.json[array] && this.json[array][index]; if (!object) { console.warn("glTF file error: Could not find ".concat(array, "[").concat(index, "]")); } return object; } _resolveScene(scene, index) { scene.id = scene.id || "scene-".concat(index); scene.nodes = (scene.nodes || []).map(node => this.getNode(node)); return scene; } _resolveNode(node, index) { node.id = node.id || "node-".concat(index); if (node.children) { node.children = node.children.map(child => this.getNode(child)); } if (node.mesh !== undefined) { node.mesh = this.getMesh(node.mesh); } else if (node.meshes !== undefined && node.meshes.length) { node.mesh = node.meshes.reduce((accum, meshIndex) => { const mesh = this.getMesh(meshIndex); accum.id = mesh.id; accum.primitives = accum.primitives.concat(mesh.primitives); return accum; }, { primitives: [] }); } if (node.camera !== undefined) { node.camera = this.getCamera(node.camera); } if (node.skin !== undefined) { node.skin = this.getSkin(node.skin); } return node; } _resolveSkin(skin, index) { skin.id = skin.id || "skin-".concat(index); skin.inverseBindMatrices = this.getAccessor(skin.inverseBindMatrices); return skin; } _resolveMesh(mesh, index) { mesh.id = mesh.id || "mesh-".concat(index); if (mesh.primitives) { mesh.primitives = mesh.primitives.map(primitive => { primitive = { ...primitive }; const attributes = primitive.attributes; primitive.attributes = {}; for (const attribute in attributes) { primitive.attributes[attribute] = this.getAccessor(attributes[attribute]); } if (primitive.indices !== undefined) { primitive.indices = this.getAccessor(primitive.indices); } if (primitive.material !== undefined) { primitive.material = this.getMaterial(primitive.material); } return primitive; }); } return mesh; } _resolveMaterial(material, index) { material.id = material.id || "material-".concat(index); if (material.normalTexture) { material.normalTexture = { ...material.normalTexture }; material.normalTexture.texture = this.getTexture(material.normalTexture.index); } if (material.occlusionTexture) { material.occlustionTexture = { ...material.occlustionTexture }; material.occlusionTexture.texture = this.getTexture(material.occlusionTexture.index); } if (material.emissiveTexture) { material.emmisiveTexture = { ...material.emmisiveTexture }; material.emissiveTexture.texture = this.getTexture(material.emissiveTexture.index); } if (!material.emissiveFactor) { material.emissiveFactor = material.emmisiveTexture ? [1, 1, 1] : [0, 0, 0]; } if (material.pbrMetallicRoughness) { material.pbrMetallicRoughness = { ...material.pbrMetallicRoughness }; const mr = material.pbrMetallicRoughness; if (mr.baseColorTexture) { mr.baseColorTexture = { ...mr.baseColorTexture }; mr.baseColorTexture.texture = this.getTexture(mr.baseColorTexture.index); } if (mr.metallicRoughnessTexture) { mr.metallicRoughnessTexture = { ...mr.metallicRoughnessTexture }; mr.metallicRoughnessTexture.texture = this.getTexture(mr.metallicRoughnessTexture.index); } } return material; } _resolveAccessor(accessor, index) { accessor.id = accessor.id || "accessor-".concat(index); if (accessor.bufferView !== undefined) { accessor.bufferView = this.getBufferView(accessor.bufferView); } accessor.bytesPerComponent = getBytesFromComponentType(accessor.componentType); accessor.components = getSizeFromAccessorType(accessor.type); accessor.bytesPerElement = accessor.bytesPerComponent * accessor.components; if (accessor.bufferView) { const buffer = accessor.bufferView.buffer; const { ArrayType, byteLength } = getAccessorArrayTypeAndLength(accessor, accessor.bufferView); const byteOffset = (accessor.bufferView.byteOffset || 0) + (accessor.byteOffset || 0) + buffer.byteOffset; let cutBuffer = buffer.arrayBuffer.slice(byteOffset, byteOffset + byteLength); if (accessor.bufferView.byteStride) { cutBuffer = this._getValueFromInterleavedBuffer(buffer, byteOffset, accessor.bufferView.byteStride, accessor.bytesPerElement, accessor.count); } accessor.value = new ArrayType(cutBuffer); } return accessor; } _getValueFromInterleavedBuffer(buffer, byteOffset, byteStride, bytesPerElement, count) { const result = new Uint8Array(count * bytesPerElement); for (let i = 0; i < count; i++) { const elementOffset = byteOffset + i * byteStride; result.set(new Uint8Array(buffer.arrayBuffer.slice(elementOffset, elementOffset + bytesPerElement)), i * bytesPerElement); } return result.buffer; } _resolveTexture(texture, index) { texture.id = texture.id || "texture-".concat(index); texture.sampler = 'sampler' in texture ? this.getSampler(texture.sampler) : DEFAULT_SAMPLER; texture.source = this.getImage(texture.source); return texture; } _resolveSampler(sampler, index) { sampler.id = sampler.id || "sampler-".concat(index); sampler.parameters = {}; for (const key in sampler) { const glEnum = this._enumSamplerParameter(key); if (glEnum !== undefined) { sampler.parameters[glEnum] = sampler[key]; } } return sampler; } _enumSamplerParameter(key) { return SAMPLER_PARAMETER_GLTF_TO_GL[key]; } _resolveImage(image, index) { image.id = image.id || "image-".concat(index); if (image.bufferView !== undefined) { image.bufferView = this.getBufferView(image.bufferView); } const preloadedImage = this.images[index]; if (preloadedImage) { image.image = preloadedImage; } return image; } _resolveBufferView(bufferView, index) { const bufferIndex = bufferView.buffer; const result = { id: "bufferView-".concat(index), ...bufferView, buffer: this.buffers[bufferIndex] }; const arrayBuffer = this.buffers[bufferIndex].arrayBuffer; let byteOffset = this.buffers[bufferIndex].byteOffset || 0; if ('byteOffset' in bufferView) { byteOffset += bufferView.byteOffset; } result.data = new Uint8Array(arrayBuffer, byteOffset, bufferView.byteLength); return result; } _resolveCamera(camera, index) { camera.id = camera.id || "camera-".concat(index); if (camera.perspective) ; if (camera.orthographic) ; return camera; } } function postProcessGLTF(gltf, options) { return new GLTFPostProcessor().postProcess(gltf, options); } const MAGIC_glTF = 0x676c5446; const GLB_FILE_HEADER_SIZE = 12; const GLB_CHUNK_HEADER_SIZE = 8; const GLB_CHUNK_TYPE_JSON = 0x4e4f534a; const GLB_CHUNK_TYPE_BIN = 0x004e4942; const GLB_CHUNK_TYPE_JSON_XVIZ_DEPRECATED = 0; const GLB_CHUNK_TYPE_BIX_XVIZ_DEPRECATED = 1; const GLB_V1_CONTENT_FORMAT_JSON = 0x0; const LE = true; function getMagicString(dataView, byteOffset = 0) { return "".concat(String.fromCharCode(dataView.getUint8(byteOffset + 0))).concat(String.fromCharCode(dataView.getUint8(byteOffset + 1))).concat(String.fromCharCode(dataView.getUint8(byteOffset + 2))).concat(String.fromCharCode(dataView.getUint8(byteOffset + 3))); } function isGLB(arrayBuffer, byteOffset = 0, options = {}) { const dataView = new DataView(arrayBuffer); const { magic = MAGIC_glTF } = options; const magic1 = dataView.getUint32(byteOffset, false); return magic1 === magic || magic1 === MAGIC_glTF; } function parseGLBSync(glb, arrayBuffer, byteOffset = 0, options = {}) { const dataView = new DataView(arrayBuffer); const type = getMagicString(dataView, byteOffset + 0); const version = dataView.getUint32(byteOffset + 4, LE); const byteLength = dataView.getUint32(byteOffset + 8, LE); Object.assign(glb, { header: { byteOffset, byteLength, hasBinChunk: false }, type, version, json: {}, binChunks: [] }); byteOffset += GLB_FILE_HEADER_SIZE; switch (glb.version) { case 1: return parseGLBV1(glb, dataView, byteOffset); case 2: return parseGLBV2(glb, dataView, byteOffset, options = {}); default: throw new Error("Invalid GLB version ".concat(glb.version, ". Only supports v1 and v2.")); } } function parseGLBV1(glb, dataView, byteOffset) { assert$7(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE); const contentLength = dataView.getUint32(byteOffset + 0, LE); const contentFormat = dataView.getUint32(byteOffset + 4, LE); byteOffset += GLB_CHUNK_HEADER_SIZE; assert$7(contentFormat === GLB_V1_CONTENT_FORMAT_JSON); parseJSONChunk(glb, dataView, byteOffset, contentLength); byteOffset += contentLength; byteOffset += parseBINChunk(glb, dataView, byteOffset, glb.header.byteLength); return byteOffset; } function parseGLBV2(glb, dataView, byteOffset, options) { assert$7(glb.header.byteLength > GLB_FILE_HEADER_SIZE + GLB_CHUNK_HEADER_SIZE); parseGLBChunksSync(glb, dataView, byteOffset, options); return byteOffset + glb.header.byteLength; } function parseGLBChunksSync(glb, dataView, byteOffset, options) { while (byteOffset + 8 <= glb.header.byteLength) { const chunkLength = dataView.getUint32(byteOffset + 0, LE); const chunkFormat = dataView.getUint32(byteOffset + 4, LE); byteOffset += GLB_CHUNK_HEADER_SIZE; switch (chunkFormat) { case GLB_CHUNK_TYPE_JSON: parseJSONChunk(glb, dataView, byteOffset, chunkLength); break; case GLB_CHUNK_TYPE_BIN: parseBINChunk(glb, dataView, byteOffset, chunkLength); break; case GLB_CHUNK_TYPE_JSON_XVIZ_DEPRECATED: if (!options.strict) { parseJSONChunk(glb, dataView, byteOffset, chunkLength); } break; case GLB_CHUNK_TYPE_BIX_XVIZ_DEPRECATED: if (!options.strict) { parseBINChunk(glb, dataView, byteOffset, chunkLength); } break; } byteOffset += padToNBytes(chunkLength, 4); } return byteOffset; } function parseJSONChunk(glb, dataView, byteOffset, chunkLength) { const jsonChunk = new Uint8Array(dataView.buffer, byteOffset, chunkLength); const textDecoder = new TextDecoder('utf8'); const jsonText = textDecoder.decode(jsonChunk); glb.json = JSON.parse(jsonText); return padToNBytes(chunkLength, 4); } function parseBINChunk(glb, dataView, byteOffset, chunkLength) { glb.header.hasBinChunk = true; glb.binChunks.push({ byteOffset, byteLength: chunkLength, arrayBuffer: dataView.buffer }); return padToNBytes(chunkLength, 4); } async function parseGLTF(gltf, arrayBufferOrString, byteOffset = 0, options, context) { var _options$gltf, _options$gltf2, _options$gltf3, _options$gltf4; parseGLTFContainerSync(gltf, arrayBufferOrString, byteOffset, options); normalizeGLTFV1(gltf, { normalize: options === null || options === void 0 ? void 0 : (_options$gltf = options.gltf) === null || _options$gltf === void 0 ? void 0 : _options$gltf.normalize }); preprocessExtensions(gltf, options, context); const promises = []; if (options !== null && options !== void 0 && (_options$gltf2 = options.gltf) !== null && _options$gltf2 !== void 0 && _options$gltf2.loadBuffers && gltf.json.buffers) { await loadBuffers(gltf, options, context); } if (options !== null && options !== void 0 && (_options$gltf3 = options.gltf) !== null && _options$gltf3 !== void 0 && _options$gltf3.loadImages) { const promise = loadImages(gltf, options, context); promises.push(promise); } const promise = decodeExtensions(gltf, options, context); promises.push(promise); await Promise.all(promises); gltf.json.gltfArrayBuffer = arrayBufferOrString; // !zeg add 兼容gltf return options !== null && options !== void 0 && (_options$gltf4 = options.gltf) !== null && _options$gltf4 !== void 0 && _options$gltf4.postProcess ? postProcessGLTF(gltf, options) : gltf; } function parseGLTFContainerSync(gltf, data, byteOffset, options) { if (options.uri) { gltf.baseUri = options.uri; } if (data instanceof ArrayBuffer && !isGLB(data, byteOffset, options)) { const textDecoder = new TextDecoder(); data = textDecoder.decode(data); } if (typeof data === 'string') { gltf.json = parseJSON(data); } else if (data instanceof ArrayBuffer) { const glb = {}; byteOffset = parseGLBSync(glb, data, byteOffset, options.glb); assert$1(glb.type === 'glTF', "Invalid GLB magic string ".concat(glb.type)); gltf._glb = glb; gltf.json = glb.json; } else { assert$1(false, 'GLTF: must be ArrayBuffer or string'); } const buffers = gltf.json.buffers || []; gltf.buffers = new Array(buffers.length).fill(null); if (gltf._glb && gltf._glb.header.hasBinChunk) { const { binChunks } = gltf._glb; gltf.buffers[0] = { arrayBuffer: binChunks[0].arrayBuffer, byteOffset: binChunks[0].byteOffset, byteLength: binChunks[0].byteLength }; } const images = gltf.json.images || []; gltf.images = new Array(images.length).fill({}); } async function loadBuffers(gltf, options, context) { const buffers = gltf.json.buffers || []; for (let i = 0; i < buffers.length; ++i) { const buffer = buffers[i]; if (buffer.uri) { var _context$fetch, _response$arrayBuffer; const { fetch } = context; assert$1(fetch); const uri = resolveUrl(buffer.uri, options); const response = await (context === null || context === void 0 ? void 0 : (_context$fetch = context.fetch) === null || _context$fetch === void 0 ? void 0 : _context$fetch.call(context, uri)); const arrayBuffer = await (response === null || response === void 0 ? void 0 : (_response$arrayBuffer = response.arrayBuffer) === null || _response$arrayBuffer === void 0 ? void 0 : _response$arrayBuffer.call(response)); gltf.buffers[i] = { arrayBuffer, byteOffset: 0, byteLength: arrayBuffer.byteLength }; delete buffer.uri; } } } async function loadImages(gltf, options, context) { const imageIndices = getReferencesImageIndices(gltf); const images = gltf.json.images || []; const promises = []; for (const imageIndex of imageIndices) { promises.push(loadImage(gltf, images[imageIndex], imageIndex, options, context)); } return await Promise.all(promises); } function getReferencesImageIndices(gltf) { const imageIndices = new Set(); const textures = gltf.json.textures || []; for (const texture of textures) { if (texture.source !== undefined) { imageIndices.add(texture.source); } } return Array.from(imageIndices).sort(); } async function loadImage(gltf, image, index, options, context) { const { fetch, parse } = context; let arrayBuffer; if (image.uri) { const uri = resolveUrl(image.uri, options); const response = await fetch(uri); arrayBuffer = await response.arrayBuffer(); } if (Number.isFinite(image.bufferView)) { const array = getTypedArrayForBufferView(gltf.json, gltf.buffers, image.bufferView); arrayBuffer = sliceArrayBuffer(array.buffer, array.byteOffset, array.byteLength); } assert$1(arrayBuffer, 'glTF image has no data'); let parsedImage = await parse(arrayBuffer, [ImageLoader$1, BasisLoader], { mimeType: image.mimeType, basis: options.basis || { format: selectSupportedBasisFormat() } }, context); if (parsedImage && parsedImage[0]) { parsedImage = { compressed: true, mipmaps: false, width: parsedImage[0].width, height: parsedImage[0].height, data: parsedImage }; } gltf.images = gltf.images || []; gltf.images[index] = parsedImage; } const GLTFLoader$1 = { name: 'glTF', id: 'gltf', module: 'gltf', version: VERSION$3, extensions: ['gltf', 'glb'], mimeTypes: ['model/gltf+json', 'model/gltf-binary'], text: true, binary: true, tests: ['glTF'], parse: parse$1, options: { gltf: { normalize: true, loadBuffers: true, loadImages: true, decompressMeshes: true, postProcess: true }, log: console }, deprecatedOptions: { fetchImages: 'gltf.loadImages', createImages: 'gltf.loadImages', decompress: 'gltf.decompressMeshes', postProcess: 'gltf.postProcess', gltf: { decompress: 'gltf.decompressMeshes' } } }; async function parse$1(arrayBuffer, options = {}, context) { options = { ...GLTFLoader$1.options, ...options }; options.gltf = { ...GLTFLoader$1.options.gltf, ...options.gltf }; const { byteOffset = 0 } = options; const gltf = {}; return await parseGLTF(gltf, arrayBuffer, byteOffset, options, context); } const GLTF_FORMAT = { URI: 0, EMBEDDED: 1 }; function parse3DTileGLTFViewSync(tile, arrayBuffer, byteOffset, options) { tile.rotateYtoZ = true; const gltfByteLength = tile.byteOffset + tile.byteLength - byteOffset; if (gltfByteLength === 0) { throw new Error('glTF byte length must be greater than 0.'); } tile.gltfUpAxis = options['3d-tiles'] && options['3d-tiles'].assetGltfUpAxis ? options['3d-tiles'].assetGltfUpAxis : 'Y'; tile.gltfArrayBuffer = sliceArrayBuffer(arrayBuffer, byteOffset, gltfByteLength); tile.gltfByteOffset = 0; tile.gltfByteLength = gltfByteLength; if (byteOffset % 4 === 0) ; else { console.warn("".concat(tile.type, ": embedded glb is not aligned to a 4-byte boundary.")); } return tile.byteOffset + tile.byteLength; } async function extractGLTF(tile, gltfFormat, options, context) { const tile3DOptions = options['3d-tiles'] || {}; extractGLTFBufferOrURL(tile, gltfFormat); if (tile3DOptions.loadGLTF) { const { parse, fetch } = context; if (tile.gltfUrl) { tile.gltfArrayBuffer = await fetch(tile.gltfUrl, options); tile.gltfByteOffset = 0; } if (tile.gltfArrayBuffer) { tile.gltf = await parse(tile.gltfArrayBuffer, GLTFLoader$1, options, context); delete tile.gltfArrayBuffer; delete tile.gltfByteOffset; delete tile.gltfByteLength; } } } function extractGLTFBufferOrURL(tile, gltfFormat, options) { switch (gltfFormat) { case GLTF_FORMAT.URI: const gltfUrlBytes = new Uint8Array(tile.gltfArrayBuffer, tile.gltfByteOffset); const textDecoder = new TextDecoder(); const gltfUrl = textDecoder.decode(gltfUrlBytes); tile.gltfUrl = gltfUrl.replace(/[\s\0]+$/, ''); delete tile.gltfArrayBuffer; delete tile.gltfByteOffset; delete tile.gltfByteLength; break; case GLTF_FORMAT.EMBEDDED: break; default: throw new Error('b3dm: Illegal glTF format field'); } } async function parseBatchedModel3DTile(tile, arrayBuffer, byteOffset, options, context) { var _tile$gltf; byteOffset = parseBatchedModel(tile, arrayBuffer, byteOffset, options); await extractGLTF(tile, GLTF_FORMAT.EMBEDDED, options, context); const extensions = tile === null || tile === void 0 ? void 0 : (_tile$gltf = tile.gltf) === null || _tile$gltf === void 0 ? void 0 : _tile$gltf.extensions; if (extensions && extensions.CESIUM_RTC) { tile.rtcCenter = extensions.CESIUM_RTC.center; } return byteOffset; } function parseBatchedModel(tile, arrayBuffer, byteOffset, options, context) { byteOffset = parse3DTileHeaderSync(tile, arrayBuffer, byteOffset); byteOffset = parse3DTileTablesHeaderSync(tile, arrayBuffer, byteOffset); byteOffset = parse3DTileTablesSync(tile, arrayBuffer, byteOffset); byteOffset = parse3DTileGLTFViewSync(tile, arrayBuffer, byteOffset, options); const featureTable = new Tile3DFeatureTable(tile.featureTableJson, tile.featureTableBinary); tile.rtcCenter = featureTable.getGlobalProperty('RTC_CENTER', GL$1.FLOAT, 3); return byteOffset; } async function parseInstancedModel3DTile(tile, arrayBuffer, byteOffset, options, context) { byteOffset = parseInstancedModel(tile, arrayBuffer, byteOffset, options); await extractGLTF(tile, tile.gltfFormat, options, context); return byteOffset; } function parseInstancedModel(tile, arrayBuffer, byteOffset, options, context) { byteOffset = parse3DTileHeaderSync(tile, arrayBuffer, byteOffset); if (tile.version !== 1) { throw new Error("Instanced 3D Model version ".concat(tile.version, " is not supported")); } byteOffset = parse3DTileTablesHeaderSync(tile, arrayBuffer, byteOffset); const view = new DataView(arrayBuffer); tile.gltfFormat = view.getUint32(byteOffset, true); byteOffset += 4; byteOffset = parse3DTileTablesSync(tile, arrayBuffer, byteOffset); byteOffset = parse3DTileGLTFViewSync(tile, arrayBuffer, byteOffset, options); if (tile.featureTableJsonByteLength === 0) { throw new Error('i3dm parser: featureTableJsonByteLength is zero.'); } const featureTable = new Tile3DFeatureTable(tile.featureTableJson, tile.featureTableBinary); const instancesLength = featureTable.getGlobalProperty('INSTANCES_LENGTH'); featureTable.featuresLength = instancesLength; if (!Number.isFinite(instancesLength)) { throw new Error('i3dm parser: INSTANCES_LENGTH must be defined'); } tile.eastNorthUp = featureTable.getGlobalProperty('EAST_NORTH_UP'); tile.rtcCenter = featureTable.getGlobalProperty('RTC_CENTER', GL$1.FLOAT, 3); const batchTable = new Tile3DBatchTableParser(tile.batchTableJson, tile.batchTableBinary, instancesLength); extractInstancedAttributes(tile, featureTable, batchTable, instancesLength); return byteOffset; } function extractInstancedAttributes(tile, featureTable, batchTable, instancesLength) { const collectionOptions = { instances: new Array(instancesLength), batchTable: tile._batchTable, cull: false, url: undefined, gltf: undefined, basePath: undefined, incrementallyLoadTextures: false, forwardAxis: [1, 0, 0] }; const instances = collectionOptions.instances; const instancePosition = new Vector3$1(); new Vector3$1(); new Vector3$1(); new Vector3$1(); const instanceRotation = new Matrix3$1(); const instanceQuaternion = new Quaternion$1(); const instanceScale = new Vector3$1(); const instanceTranslationRotationScale = {}; const instanceTransform = new Matrix4$1(); const scratch1 = []; const scratch2 = []; const scratchVector1 = new Vector3$1(); const scratchVector2 = new Vector3$1(); for (let i = 0; i < instancesLength; i++) { let position; if (featureTable.hasProperty('POSITION')) { position = featureTable.getProperty('POSITION', GL$1.FLOAT, 3, i, instancePosition); } else if (featureTable.hasProperty('POSITION_QUANTIZED')) { position = featureTable.getProperty('POSITION_QUANTIZED', GL$1.UNSIGNED_SHORT, 3, i, instancePosition); const quantizedVolumeOffset = featureTable.getGlobalProperty('QUANTIZED_VOLUME_OFFSET', GL$1.FLOAT, 3, scratchVector1); if (!quantizedVolumeOffset) { throw new Error('i3dm parser: QUANTIZED_VOLUME_OFFSET must be defined for quantized positions.'); } const quantizedVolumeScale = featureTable.getGlobalProperty('QUANTIZED_VOLUME_SCALE', GL$1.FLOAT, 3, scratchVector2); if (!quantizedVolumeScale) { throw new Error('i3dm parser: QUANTIZED_VOLUME_SCALE must be defined for quantized positions.'); } const MAX_UNSIGNED_SHORT = 65535.0; for (let j = 0; j < 3; j++) { position[j] = position[j] / MAX_UNSIGNED_SHORT * quantizedVolumeScale[j] + quantizedVolumeOffset[j]; } } if (!position) { throw new Error('i3dm: POSITION or POSITION_QUANTIZED must be defined for each instance.'); } instancePosition.copy(position); instanceTranslationRotationScale.translation = instancePosition; tile.normalUp = featureTable.getProperty('NORMAL_UP', GL$1.FLOAT, 3, i, scratch1); tile.normalRight = featureTable.getProperty('NORMAL_RIGHT', GL$1.FLOAT, 3, i, scratch2); if (tile.normalUp) { if (!tile.normalRight) { throw new Error('i3dm: Custom orientation requires both NORMAL_UP and NORMAL_RIGHT.'); } tile.hasCustomOrientation = true; } else { tile.octNormalUp = featureTable.getProperty('NORMAL_UP_OCT32P', GL$1.UNSIGNED_SHORT, 2, scratch1); tile.octNormalRight = featureTable.getProperty('NORMAL_RIGHT_OCT32P', GL$1.UNSIGNED_SHORT, 2, scratch2); if (tile.octNormalUp) { if (!tile.octNormalRight) { throw new Error('i3dm: oct-encoded orientation requires NORMAL_UP_OCT32P and NORMAL_RIGHT_OCT32P'); } throw new Error('i3dm: oct-encoded orientation not implemented'); } else if (tile.eastNorthUp) { Ellipsoid.WGS84.eastNorthUpToFixedFrame(instancePosition, instanceTransform); instanceTransform.getRotationMatrix3(instanceRotation); } else { instanceRotation.identity(); } } instanceQuaternion.fromMatrix3(instanceRotation); instanceTranslationRotationScale.rotation = instanceQuaternion; instanceScale.set(1.0, 1.0, 1.0); const scale = featureTable.getProperty('SCALE', GL$1.FLOAT, 1, i); if (Number.isFinite(scale)) { instanceScale.multiplyByScalar(scale); } const nonUniformScale = featureTable.getProperty('SCALE_NON_UNIFORM', GL$1.FLOAT, 3, i, scratch1); if (nonUniformScale) { instanceScale.scale(nonUniformScale); } instanceTranslationRotationScale.scale = instanceScale; let batchId = featureTable.getProperty('BATCH_ID', GL$1.UNSIGNED_SHORT, 1, i); if (batchId === undefined) { batchId = i; } const rotationMatrix = new Matrix4$1().fromQuaternion(instanceTranslationRotationScale.rotation); instanceTransform.identity(); instanceTransform.translate(instanceTranslationRotationScale.translation); instanceTransform.multiplyRight(rotationMatrix); instanceTransform.scale(instanceTranslationRotationScale.scale); const modelMatrix = instanceTransform.clone(); instances[i] = { modelMatrix, batchId }; } tile.instances = instances; } async function parseComposite3DTile(tile, arrayBuffer, byteOffset, options, context, parse3DTile) { byteOffset = parse3DTileHeaderSync(tile, arrayBuffer, byteOffset); const view = new DataView(arrayBuffer); tile.tilesLength = view.getUint32(byteOffset, true); byteOffset += 4; tile.tiles = []; while (tile.tiles.length < tile.tilesLength && tile.byteLength - byteOffset > 12) { const subtile = {}; tile.tiles.push(subtile); byteOffset = await parse3DTile(arrayBuffer, byteOffset, options, context, subtile); } return byteOffset; } async function parseGltf3DTile(tile, arrayBuffer, options, context) { tile.rotateYtoZ = true; tile.gltfUpAxis = options['3d-tiles'] && options['3d-tiles'].assetGltfUpAxis ? options['3d-tiles'].assetGltfUpAxis : 'Y'; const { parse } = context; tile.gltf = await parse(arrayBuffer, GLTFLoader$1, options, context); } async function parse3DTile(arrayBuffer, byteOffset = 0, options, context, tile = {}) { tile.byteOffset = byteOffset; tile.type = getMagicString$1(arrayBuffer, byteOffset); switch (tile.type) { case TILE3D_TYPE.COMPOSITE: return await parseComposite3DTile(tile, arrayBuffer, byteOffset, options, context, parse3DTile); case TILE3D_TYPE.BATCHED_3D_MODEL: return await parseBatchedModel3DTile(tile, arrayBuffer, byteOffset, options, context); case TILE3D_TYPE.GLTF: return await parseGltf3DTile(tile, arrayBuffer, options, context); case TILE3D_TYPE.INSTANCED_3D_MODEL: return await parseInstancedModel3DTile(tile, arrayBuffer, byteOffset, options, context); case TILE3D_TYPE.POINT_CLOUD: return await parsePointCloud3DTile(tile, arrayBuffer, byteOffset, options, context); default: throw new Error("3DTileLoader: unknown type ".concat(tile.type)); } } const SUBTREE_FILE_MAGIC = 0x74627573; const SUBTREE_FILE_VERSION = 1; async function parse3DTilesSubtree(data) { const magic = new Uint32Array(data.slice(0, 4)); if (magic[0] !== SUBTREE_FILE_MAGIC) { throw new Error('Wrong subtree file magic number'); } const version = new Uint32Array(data.slice(4, 8)); if (version[0] !== SUBTREE_FILE_VERSION) { throw new Error('Wrong subtree file verson, must be 1'); } const jsonByteLength = parseUint64Value(data.slice(8, 16)); const stringAttribute = new Uint8Array(data, 24, jsonByteLength); const textDecoder = new TextDecoder('utf8'); const string = textDecoder.decode(stringAttribute); const subtree = JSON.parse(string); const binaryByteLength = parseUint64Value(data.slice(16, 24)); let internalBinaryBuffer = new ArrayBuffer(0); if (binaryByteLength) { internalBinaryBuffer = data.slice(24 + jsonByteLength); } if ('bufferView' in subtree.tileAvailability) { subtree.tileAvailability.explicitBitstream = await getExplicitBitstream(subtree, 'tileAvailability', internalBinaryBuffer); } if ('bufferView' in subtree.contentAvailability) { subtree.contentAvailability.explicitBitstream = await getExplicitBitstream(subtree, 'contentAvailability', internalBinaryBuffer); } if ('bufferView' in subtree.childSubtreeAvailability) { subtree.childSubtreeAvailability.explicitBitstream = await getExplicitBitstream(subtree, 'childSubtreeAvailability', internalBinaryBuffer); } return subtree; } async function getExplicitBitstream(subtree, name, internalBinaryBuffer) { const bufferViewIndex = subtree[name].bufferView; const bufferView = subtree.bufferViews[bufferViewIndex]; const buffer = subtree.buffers[bufferView.buffer]; if (buffer.uri) { const response = await fetchFile(buffer.uri); const data = await response.arrayBuffer(); return new Uint8Array(data, bufferView.byteOffset, bufferView.byteLength); } return new Uint8Array(internalBinaryBuffer, bufferView.byteOffset, bufferView.byteLength); } function parseUint64Value(buffer) { const dataView = new DataView(buffer); const left = dataView.getUint32(0, true); const right = dataView.getUint32(4, true); return left + 2 ** 32 * right; } const Tile3DSubtreeLoader = { id: '3d-tiles-subtree', name: '3D Tiles Subtree', module: '3d-tiles', version: VERSION$5, extensions: ['subtree'], mimeTypes: ['application/octet-stream'], tests: ['subtree'], parse: parse3DTilesSubtree, options: {} }; const QUADTREE_DEVISION_COUNT = 4; const OCTREE_DEVISION_COUNT = 8; const SUBDIVISION_COUNT_MAP = { QUADTREE: QUADTREE_DEVISION_COUNT, OCTREE: OCTREE_DEVISION_COUNT }; async function parseImplicitTiles(subtree, options, parentData = { mortonIndex: 0, x: 0, y: 0, z: 0 }, childIndex = 0, level = 0, globalData = { level: 0, mortonIndex: 0, x: 0, y: 0, z: 0 }) { const { subdivisionScheme, subtreeLevels, maximumLevel, contentUrlTemplate, subtreesUriTemplate, basePath } = options; const tile = { children: [], lodMetricValue: 0, contentUrl: '' }; const childrenPerTile = SUBDIVISION_COUNT_MAP[subdivisionScheme]; const childX = childIndex & 0b01; const childY = childIndex >> 1 & 0b01; const childZ = childIndex >> 2 & 0b01; const levelOffset = (childrenPerTile ** level - 1) / (childrenPerTile - 1); let childTileMortonIndex = concatBits(parentData.mortonIndex, childIndex); let tileAvailabilityIndex = levelOffset + childTileMortonIndex; let childTileX = concatBits(parentData.x, childX); let childTileY = concatBits(parentData.y, childY); let childTileZ = concatBits(parentData.z, childZ); let isChildSubtreeAvailable = false; if (level + 1 > subtreeLevels) { isChildSubtreeAvailable = getAvailabilityResult(subtree.childSubtreeAvailability, childTileMortonIndex); } const x = concatBits(globalData.x, childTileX); const y = concatBits(globalData.y, childTileY); const z = concatBits(globalData.z, childTileZ); const lev = level + globalData.level; if (isChildSubtreeAvailable) { const subtreePath = "".concat(basePath, "/").concat(subtreesUriTemplate); const childSubtreeUrl = replaceContentUrlTemplate(subtreePath, lev, x, y, z); const childSubtree = await load(childSubtreeUrl, Tile3DSubtreeLoader); subtree = childSubtree; globalData.mortonIndex = childTileMortonIndex; globalData.x = childTileX; globalData.y = childTileY; globalData.z = childTileZ; globalData.level = level; childTileMortonIndex = 0; tileAvailabilityIndex = 0; childTileX = 0; childTileY = 0; childTileZ = 0; level = 0; } const isTileAvailable = getAvailabilityResult(subtree.tileAvailability, tileAvailabilityIndex); if (!isTileAvailable || level > maximumLevel) { return tile; } const isContentAvailable = getAvailabilityResult(subtree.contentAvailability, tileAvailabilityIndex); if (isContentAvailable) { tile.contentUrl = replaceContentUrlTemplate(contentUrlTemplate, lev, x, y, z); } const childTileLevel = level + 1; const pData = { mortonIndex: childTileMortonIndex, x: childTileX, y: childTileY, z: childTileZ }; for (let index = 0; index < childrenPerTile; index++) { const currentTile = await parseImplicitTiles(subtree, options, pData, index, childTileLevel, globalData); if (currentTile.contentUrl || currentTile.children.length) { const globalLevel = lev + 1; const childCoordinates = { childTileX, childTileY, childTileZ }; const formattedTile = formatTileData(currentTile, globalLevel, childCoordinates, options); tile.children.push(formattedTile); } } return tile; } function getAvailabilityResult(availabilityData, index) { if ('constant' in availabilityData) { return Boolean(availabilityData.constant); } if (availabilityData.explicitBitstream) { return getBooleanValueFromBitstream(index, availabilityData.explicitBitstream); } return false; } function formatTileData(tile, level, childCoordinates, options) { const { basePath, refine, getRefine, lodMetricType, getTileType, rootLodMetricValue, rootBoundingVolume } = options; const uri = tile.contentUrl && tile.contentUrl.replace("".concat(basePath, "/"), ''); const lodMetricValue = rootLodMetricValue / 2 ** level; const boundingVolume = calculateBoundingVolumeForChildTile(level, rootBoundingVolume, childCoordinates); return { children: tile.children, contentUrl: tile.contentUrl, content: { uri }, id: tile.contentUrl, refine: getRefine(refine), type: getTileType(tile), lodMetricType, lodMetricValue, boundingVolume }; } function calculateBoundingVolumeForChildTile(level, rootBoundingVolume, childCoordinates) { if (rootBoundingVolume.region) { const { childTileX, childTileY, childTileZ } = childCoordinates; const [west, south, east, north, minimumHeight, maximumHeight] = rootBoundingVolume.region; const boundingVolumesCount = 2 ** level; const sizeX = (east - west) / boundingVolumesCount; const sizeY = (north - south) / boundingVolumesCount; const sizeZ = (maximumHeight - minimumHeight) / boundingVolumesCount; const [childWest, childEast] = [west + sizeX * childTileX, west + sizeX * (childTileX + 1)]; const [childSouth, childNorth] = [south + sizeY * childTileY, south + sizeY * (childTileY + 1)]; const [childMinimumHeight, childMaximumHeight] = [minimumHeight + sizeZ * childTileZ, minimumHeight + sizeZ * (childTileZ + 1)]; return { region: [childWest, childSouth, childEast, childNorth, childMinimumHeight, childMaximumHeight] }; } console.warn('Unsupported bounding volume type: ', rootBoundingVolume); return null; } function concatBits(first, second) { return parseInt(first.toString(2) + second.toString(2), 2); } function replaceContentUrlTemplate(templateUrl, level, x, y, z) { const mapUrl = generateMapUrl({ level, x, y, z }); return templateUrl.replace(/{level}|{x}|{y}|{z}/gi, matched => mapUrl[matched]); } function generateMapUrl(items) { const mapUrl = {}; for (const key in items) { mapUrl["{".concat(key, "}")] = items[key]; } return mapUrl; } function getBooleanValueFromBitstream(availabilityIndex, availabilityBuffer) { const byteIndex = Math.floor(availabilityIndex / 8); const bitIndex = availabilityIndex % 8; const bitValue = availabilityBuffer[byteIndex] >> bitIndex & 1; return bitValue === 1; } function getTileType(tile) { if (!tile.contentUrl) { return TILE_TYPE.EMPTY; } const contentUrl = tile.contentUrl; const fileExtension = contentUrl.split('.').pop(); switch (fileExtension) { case 'pnts': return TILE_TYPE.POINTCLOUD; case 'i3dm': case 'b3dm': case 'glb': case 'gltf': return TILE_TYPE.SCENEGRAPH; default: return fileExtension; } } function getRefine(refine) { switch (refine) { case 'REPLACE': case 'replace': return TILE_REFINEMENT.REPLACE; case 'ADD': case 'add': return TILE_REFINEMENT.ADD; default: return refine; } } function normalizeTileData(tile, options) { if (!tile) { return null; } if (tile.content) { const contentUri = tile.content.uri || tile.content.url; tile.contentUrl = "".concat(options.basePath, "/").concat(contentUri); } tile.id = tile.contentUrl; tile.lodMetricType = LOD_METRIC_TYPE.GEOMETRIC_ERROR; tile.lodMetricValue = tile.geometricError; tile.transformMatrix = tile.transform; tile.type = getTileType(tile); tile.refine = getRefine(tile.refine); return tile; } function normalizeTileHeaders(tileset) { const basePath = tileset.basePath; const root = normalizeTileData(tileset.root, tileset); const stack = []; stack.push(root); while (stack.length > 0) { const tile = stack.pop() || {}; const children = tile.children || []; for (const childHeader of children) { normalizeTileData(childHeader, { basePath }); stack.push(childHeader); } } return root; } async function normalizeImplicitTileHeaders(tileset) { if (!tileset.root) { return null; } const basePath = tileset.basePath; const implicitTilingExtension = tileset.root.extensions['3DTILES_implicit_tiling']; const { subdivisionScheme, maximumLevel, subtreeLevels, subtrees: { uri: subtreesUriTemplate } } = implicitTilingExtension; const subtreeUrl = replaceContentUrlTemplate(subtreesUriTemplate, 0, 0, 0, 0); const rootSubtreeUrl = "".concat(basePath, "/").concat(subtreeUrl); const rootSubtree = await load(rootSubtreeUrl, Tile3DSubtreeLoader); const contentUrlTemplate = "".concat(basePath, "/").concat(tileset.root.content.uri); const refine = tileset.root.refine; const rootLodMetricValue = tileset.root.geometricError; const rootBoundingVolume = tileset.root.boundingVolume; const options = { contentUrlTemplate, subtreesUriTemplate, subdivisionScheme, subtreeLevels, maximumLevel, refine, basePath, lodMetricType: LOD_METRIC_TYPE.GEOMETRIC_ERROR, rootLodMetricValue, rootBoundingVolume, getTileType, getRefine }; return await normalizeImplicitTileData(tileset.root, rootSubtree, options); } async function normalizeImplicitTileData(tile, rootSubtree, options) { if (!tile) { return null; } tile.lodMetricType = LOD_METRIC_TYPE.GEOMETRIC_ERROR; tile.lodMetricValue = tile.geometricError; tile.transformMatrix = tile.transform; const { children, contentUrl } = await parseImplicitTiles(rootSubtree, options); if (contentUrl) { tile.contentUrl = contentUrl; tile.content = { uri: contentUrl.replace("".concat(options.basePath, "/"), '') }; } tile.refine = getRefine(tile.refine); tile.type = getTileType(tile); tile.children = children; tile.id = tile.contentUrl; return tile; } const IMPLICIT_TILING_EXTENSION_NAME = '3DTILES_implicit_tiling'; const Tiles3DLoader = { id: '3d-tiles', name: '3D Tiles', module: '3d-tiles', version: VERSION$5, extensions: ['cmpt', 'pnts', 'b3dm', 'i3dm'], mimeTypes: ['application/octet-stream'], tests: ['cmpt', 'pnts', 'b3dm', 'i3dm'], parse: parse$4, options: { '3d-tiles': { loadGLTF: true, decodeQuantizedPositions: false, isTileset: 'auto', assetGltfUpAxis: null } } }; function getBaseUri(tileset) { return dirname(tileset.url); } async function parseTile(arrayBuffer, options, context) { const tile = { content: { featureIds: null } }; const byteOffset = 0; await parse3DTile(arrayBuffer, byteOffset, options, context, tile.content); return tile.content; } async function parseTileset(data, options, context) { var _tilesetJson$root; const tilesetJson = JSON.parse(new TextDecoder().decode(data)); tilesetJson.loader = options.loader || Tiles3DLoader; tilesetJson.url = context.url; tilesetJson.basePath = getBaseUri(tilesetJson); tilesetJson.root = hasImplicitTilingExtension(tilesetJson) ? await normalizeImplicitTileHeaders(tilesetJson) : normalizeTileHeaders(tilesetJson); tilesetJson.type = TILESET_TYPE.TILES3D; tilesetJson.lodMetricType = LOD_METRIC_TYPE.GEOMETRIC_ERROR; tilesetJson.lodMetricValue = ((_tilesetJson$root = tilesetJson.root) === null || _tilesetJson$root === void 0 ? void 0 : _tilesetJson$root.lodMetricValue) || 0; return tilesetJson; } async function parse$4(data, options, context) { const loaderOptions = options['3d-tiles'] || {}; let isTileset; if (loaderOptions.isTileset === 'auto') { isTileset = context.url && context.url.indexOf('.json') !== -1; } else { isTileset = loaderOptions.isTileset; } if (isTileset) { data = await parseTileset(data, options, context); } else { data = await parseTile(data, options, context); } return data; } function hasImplicitTilingExtension(tilesetJson) { var _tilesetJson$extensio, _tilesetJson$extensio2; return (tilesetJson === null || tilesetJson === void 0 ? void 0 : (_tilesetJson$extensio = tilesetJson.extensionsRequired) === null || _tilesetJson$extensio === void 0 ? void 0 : _tilesetJson$extensio.includes(IMPLICIT_TILING_EXTENSION_NAME)) && (tilesetJson === null || tilesetJson === void 0 ? void 0 : (_tilesetJson$extensio2 = tilesetJson.extensionsUsed) === null || _tilesetJson$extensio2 === void 0 ? void 0 : _tilesetJson$extensio2.includes(IMPLICIT_TILING_EXTENSION_NAME)); } const CESIUM_ION_URL = 'https://api.cesium.com/v1/assets'; async function getIonTilesetMetadata(accessToken, assetId) { if (!assetId) { const assets = await getIonAssets(accessToken); for (const item of assets.items) { if (item.type === '3DTILES') { assetId = item.id; } } } const ionAssetMetadata = await getIonAssetMetadata(accessToken, assetId); const { type, url } = ionAssetMetadata; assert$7(type === '3DTILES' && url); ionAssetMetadata.headers = { Authorization: "Bearer ".concat(ionAssetMetadata.accessToken) }; return ionAssetMetadata; } async function getIonAssets(accessToken) { assert$7(accessToken); const url = CESIUM_ION_URL; const headers = { Authorization: "Bearer ".concat(accessToken) }; const response = await fetchFile(url, { fetch: { headers } }); if (!response.ok) { throw new Error(response.statusText); } return await response.json(); } async function getIonAssetMetadata(accessToken, assetId) { assert$7(accessToken, assetId); const headers = { Authorization: "Bearer ".concat(accessToken) }; const url = "".concat(CESIUM_ION_URL, "/").concat(assetId); let response = await fetchFile("".concat(url), { fetch: { headers } }); if (!response.ok) { throw new Error(response.statusText); } let metadata = await response.json(); response = await fetchFile("".concat(url, "/endpoint"), { fetch: { headers } }); if (!response.ok) { throw new Error(response.statusText); } const tilesetInfo = await response.json(); metadata = { ...metadata, ...tilesetInfo }; return metadata; } async function preload(url, options = {}) { options = options['cesium-ion'] || {}; const { accessToken } = options; let assetId = options.assetId; if (!Number.isFinite(assetId)) { const matched = url.match(/\/([0-9]+)\/tileset.json/); assetId = matched && matched[1]; } return getIonTilesetMetadata(accessToken, assetId); } const CesiumIonLoader = { ...Tiles3DLoader, id: 'cesium-ion', name: 'Cesium Ion', preload, parse: async (data, options, context) => { options = { ...options }; options['3d-tiles'] = options['cesium-ion']; options.loader = CesiumIonLoader; return Tiles3DLoader.parse(data, options, context); }, options: { 'cesium-ion': { ...Tiles3DLoader.options['3d-tiles'], accessToken: null } } }; // From https://github.com/potree/potree/blob/master/src/materials/PointCloudMaterial.js function generateGradientTexture(gradient) { const size = 64; // create canvas const canvas = document.createElement('canvas'); canvas.width = size; canvas.height = size; // get context const context = canvas.getContext('2d'); // draw gradient context.rect(0, 0, size, size); const ctxGradient = context.createLinearGradient(0, 0, size, size); for (let i = 0; i < gradient.length; i++) { const step = gradient[i]; ctxGradient.addColorStop(step[0], '#' + step[1].getHexString()); } context.fillStyle = ctxGradient; context.fill(); //let texture = new THREE.Texture(canvas); const texture = new CanvasTexture(canvas); texture.needsUpdate = true; texture.minFilter = LinearFilter; texture.wrapS = RepeatWrapping; texture.wrapT = RepeatWrapping; texture.repeat.set(2, 2); // textureImage = texture.image; return texture; } function getCameraFrustum(camera) { camera.updateMatrix(); // make sure camera's local matrix is updated camera.updateMatrixWorld(); // make sure camera's world matrix is updated camera.matrixWorldInverse.copy(camera.matrixWorld).invert(); const frustum = new Frustum(); frustum.setFromProjectionMatrix(new Matrix4().multiplyMatrices(camera.projectionMatrix, camera.matrixWorldInverse)); return frustum; } function loadersPlaneToMesh(plane) { const group = new Group(); // Create a basic rectangle geometry from math.gl plane const planeGeometry = new PlaneGeometry(10, 5); // Align the geometry to the plane const coplanarPoint = new Vector3(...plane.projectPointOntoPlane([0, 0, 0])); const normal = new Vector3(plane.normal.x, plane.normal.y, plane.normal.z); const focalPoint = new Vector3().copy(coplanarPoint).add(normal); planeGeometry.lookAt(focalPoint); planeGeometry.translate(coplanarPoint.x, coplanarPoint.y, coplanarPoint.z); // Edges /* const edges = new EdgesGeometry(planeGeometry) var dispPlane = new LineSegments(edges, new LineBasicMaterial({ color: 0x00ffff }))*/ //plane const material = new BasicMaterial({ color: 0x00ffff, side: DoubleSide }); const mesh = new Mesh(planeGeometry, material); const arrowHelper = new ArrowHelper(normal, coplanarPoint, 5, 0xffff00); group.add(arrowHelper); group.add(mesh); return group; } function loadersBoundingBoxToMesh(tile) { // Create a basic rectangle geometry from math.gl half-axes const boundUntransformed = tile.type == 'empty' ? tile.boundingVolume : tile.getBoundUntransformed(); //empty时没创建好,直接给boundingVolume一样 /* let redColor = 0; if (tile.content) { //redColor = Math.min((tile.content.byteLength != void 0 ? tile.content.byteLength : 0) / 500000, 1.0); redColor = Math.min( tile.depth / 10 , 1.0); //改 } */ let hue = 0; if(tile.content){ hue = Math.min( tile.depth / 10 , 1.0); //改 } const boxColor = new THREE.Color().setHSL(hue, 0.9, 0.85); //const boxColor = new Color(redColor, 1.0, 0.0); const boxGeometry = new BoxGeometry(1, 1, 1); const boxTransform = new Matrix4(); if (boundUntransformed.halfAxes) { boxTransform.copy(getMatrix4FromHalfAxes(boundUntransformed.halfAxes)); } else if (boundUntransformed.radius) { boxGeometry.scale(boundUntransformed.radius * 2, boundUntransformed.radius * 2, boundUntransformed.radius * 2); } boxTransform.premultiply((new Matrix4()).setPosition(...boundUntransformed.center)); //add /* if(tile.type != "empty"){ //root单独处理了 boxTransform.premultiply((new Matrix4$1()).makeScale(1,1,-1))//xzw 因为mesh倒转了所以box也 }else{ console.log('empty') } */ boxGeometry.applyMatrix4(boxTransform); const edges = new EdgesGeometry(boxGeometry); const dispPlane = new LineSegments(edges, new LineBasicMaterial({ color: boxColor, transparent:true })); //dispPlane.position.copy(new Vector3$1(...boundingVolume.center)); dispPlane.matrixAutoUpdate = false; //add let label = new Potree.TextSprite({ mat: new THREE.MeshBasicMaterial({transparent:true }), //depthTest:false会导致经常显示不出 backgroundColor: {r: 0, g: 0, b: 0, a:0.1}, textColor: {r: boxColor.r*255, g: boxColor.g*255, b: boxColor.b*255, a:0.9}, fontsize:100, //useDepth : true , renderOrder : 5, text: tile.id.split('/').pop().split('.b3dm')[0], //Tile_+094_-010.b3dm' name:'tile' }); label.addEventListener('mouseover',()=>{ if(label.sprite.material.opacity < 1)return window.hoverTile = tile; console.log('hoverLabel',tile.id, tile._distanceToCamera); }); /* let s = 0.6 label.scale.set(s,s,s) */ let s = tile.tileset.options.maximumScreenSpaceError / 400; label.scale.set(s,s,s); dispPlane.add(label); label.position.set(...boundUntransformed.center); //label.position.z *= -1 /* if(tile.content.byteLength == void 0){ let oldUpdate = dispPlane.updateMatrixWorld.bind(dispPlane) dispPlane.updateMatrixWorld = (a,b)=>{ oldUpdate(a,b) } let oldU = dispPlane.updateMatrix.bind(dispPlane) dispPlane.updateMatrix = (a,b)=>{ oldU(a,b) } } */ tile.volumeBox = dispPlane; return dispPlane; } function getMatrix4FromHalfAxes(halfAxes) { const m = halfAxes; const rotateMatrix = new Matrix4().fromArray([ m[0] * 2, m[1] * 2, m[2] * 2, 0, m[3] * 2, m[4] * 2, m[5] * 2, 0, m[6] * 2, m[7] * 2, m[8] * 2, 0, 0, 0, 0, 1, ]); return rotateMatrix; } /* * from https://github.com/tentone/geo-three * Tree-shaking did not work, probably due to static class methods */ function datumsToSpherical(latitude, longitude) { const EARTH_RADIUS = 6378137; const EARTH_PERIMETER = 2 * Math.PI * EARTH_RADIUS; const EARTH_ORIGIN = EARTH_PERIMETER / 2.0; const x = longitude * EARTH_ORIGIN / 180.0; let y = Math.log(Math.tan((90 + latitude) * Math.PI / 360.0)) / (Math.PI / 180.0); y = y * EARTH_ORIGIN / 180.0; return new Vector2(x, y); } const Gradients$1 = { // From chroma spectral http://gka.github.io/chroma.js/ SPECTRAL: [ [0, new Color(0.3686, 0.3098, 0.6353)], [0.1, new Color(0.1961, 0.5333, 0.7412)], [0.2, new Color(0.4, 0.7608, 0.6471)], [0.3, new Color(0.6706, 0.8667, 0.6431)], [0.4, new Color(0.902, 0.9608, 0.5961)], [0.5, new Color(1.0, 1.0, 0.749)], [0.6, new Color(0.9961, 0.8784, 0.5451)], [0.7, new Color(0.9922, 0.6824, 0.3804)], [0.8, new Color(0.9569, 0.4275, 0.2627)], [0.9, new Color(0.8353, 0.2431, 0.3098)], [1, new Color(0.6196, 0.0039, 0.2588)], ], PLASMA: [ [0.0, new Color(0.241, 0.015, 0.61)], [0.1, new Color(0.387, 0.001, 0.654)], [0.2, new Color(0.524, 0.025, 0.653)], [0.3, new Color(0.651, 0.125, 0.596)], [0.4, new Color(0.752, 0.227, 0.513)], [0.5, new Color(0.837, 0.329, 0.431)], [0.6, new Color(0.907, 0.435, 0.353)], [0.7, new Color(0.963, 0.554, 0.272)], [0.8, new Color(0.992, 0.681, 0.195)], [0.9, new Color(0.987, 0.822, 0.144)], [1.0, new Color(0.94, 0.975, 0.131)], ], YELLOW_GREEN: [ [0, new Color(0.1647, 0.2824, 0.3451)], [0.1, new Color(0.1338, 0.3555, 0.4227)], [0.2, new Color(0.061, 0.4319, 0.4864)], [0.3, new Color(0.0, 0.5099, 0.5319)], [0.4, new Color(0.0, 0.5881, 0.5569)], [0.5, new Color(0.137, 0.665, 0.5614)], [0.6, new Color(0.2906, 0.7395, 0.5477)], [0.7, new Color(0.4453, 0.8099, 0.5201)], [0.8, new Color(0.6102, 0.8748, 0.485)], [0.9, new Color(0.7883, 0.9323, 0.4514)], [1, new Color(0.9804, 0.9804, 0.4314)], ], VIRIDIS: [ [0.0, new Color(0.267, 0.005, 0.329)], [0.1, new Color(0.283, 0.141, 0.458)], [0.2, new Color(0.254, 0.265, 0.53)], [0.3, new Color(0.207, 0.372, 0.553)], [0.4, new Color(0.164, 0.471, 0.558)], [0.5, new Color(0.128, 0.567, 0.551)], [0.6, new Color(0.135, 0.659, 0.518)], [0.7, new Color(0.267, 0.749, 0.441)], [0.8, new Color(0.478, 0.821, 0.318)], [0.9, new Color(0.741, 0.873, 0.15)], [1.0, new Color(0.993, 0.906, 0.144)], ], INFERNO: [ [0.0, new Color(0.077, 0.042, 0.206)], [0.1, new Color(0.225, 0.036, 0.388)], [0.2, new Color(0.373, 0.074, 0.432)], [0.3, new Color(0.522, 0.128, 0.42)], [0.4, new Color(0.665, 0.182, 0.37)], [0.5, new Color(0.797, 0.255, 0.287)], [0.6, new Color(0.902, 0.364, 0.184)], [0.7, new Color(0.969, 0.516, 0.063)], [0.8, new Color(0.988, 0.683, 0.072)], [0.9, new Color(0.961, 0.859, 0.298)], [1.0, new Color(0.988, 0.998, 0.645)], ], GRAYSCALE: [ [0, new Color(0, 0, 0)], [1, new Color(1, 1, 1)], ], // 16 samples of the TURBU color scheme // values taken from: https://gist.github.com/mikhailov-work/ee72ba4191942acecc03fe6da94fc73f // original file licensed under Apache-2.0 TURBO: [ [0.0, new Color(0.18995, 0.07176, 0.23217)], [0.07, new Color(0.25107, 0.25237, 0.63374)], [0.13, new Color(0.27628, 0.42118, 0.89123)], [0.2, new Color(0.25862, 0.57958, 0.99876)], [0.27, new Color(0.15844, 0.73551, 0.92305)], [0.33, new Color(0.09267, 0.86554, 0.7623)], [0.4, new Color(0.19659, 0.94901, 0.59466)], [0.47, new Color(0.42778, 0.99419, 0.38575)], [0.53, new Color(0.64362, 0.98999, 0.23356)], [0.6, new Color(0.80473, 0.92452, 0.20459)], [0.67, new Color(0.93301, 0.81236, 0.22667)], [0.73, new Color(0.99314, 0.67408, 0.20348)], [0.8, new Color(0.9836, 0.49291, 0.12849)], [0.87, new Color(0.92105, 0.31489, 0.05475)], [0.93, new Color(0.81608, 0.18462, 0.01809)], [1.0, new Color(0.66449, 0.08436, 0.00424)], ], RAINBOW: [ [0, new Color(0.278, 0, 0.714)], [1 / 6, new Color(0, 0, 1)], [2 / 6, new Color(0, 1, 1)], [3 / 6, new Color(0, 1, 0)], [4 / 6, new Color(1, 1, 0)], [5 / 6, new Color(1, 0.64, 0)], [1, new Color(1, 0, 0)], ], CONTOUR: [ [0.0, new Color(0, 0, 0)], [0.03, new Color(0, 0, 0)], [0.04, new Color(1, 1, 1)], [1.0, new Color(1, 1, 1)], ], }; const PointCloudFS = ` varying vec3 vColor; uniform float alpha; void main() { if (vColor == vec3(0.0, 0.0, 0.0)) { discard; } else { gl_FragColor = vec4( vColor, alpha); } } `; const PointCloudVS = ` varying vec3 vColor; uniform sampler2D gradient; uniform sampler2D grayscale; attribute float intensity; attribute float classification; uniform vec3 rootCenter; uniform vec3 rootNormal; uniform vec2 elevationRange; uniform int coloring; uniform bool hideGround; uniform float maxIntensity; uniform float intensityContrast; uniform float pointSize; #ifdef USE_COLOR vec3 getRGB() { vec3 rgb = color; return rgb; } #endif vec3 getElevation(){ vec4 world = modelMatrix * vec4( position, 1.0 ); float diff = abs(dot(rootNormal, (vec3(world) - rootCenter))); float w = max(diff - elevationRange.x,0.0) / max(elevationRange.y - elevationRange.x,1.0); vec3 cElevation = texture2D(gradient, vec2(w,1.0-w)).rgb; return cElevation; } vec3 getIntensity(){ // TODO: real contrast enhancement. Check https://github.com/yuki-koyama/enhancer/blob/master/shaders/enhancer.fs float intmod = pow(intensity, intensityContrast); vec3 cIntensity = texture2D(grayscale, vec2(intmod / maxIntensity ,1.0-(intmod / maxIntensity))).rgb; return cIntensity; } vec3 getClassification(){ float classNormalized = classification / 255.0; vec3 cClassification = texture2D(gradient, vec2(classNormalized * 5.0,1.0-classNormalized * 5.0)).rgb; return cClassification; } vec3 getColor(){ vec3 color; if (hideGround && classification == 2.0) { return vec3(0.0, 0.0, 0.0); } if (coloring == 1) { color = getIntensity(); } else if (coloring == 2) { color = getClassification(); } else if (coloring == 3) { color = getElevation(); } #ifdef USE_COLOR else if (coloring == 4) { color = getRGB(); } #endif else { color = vec3(1.0, 1.0, 1.0); } return color; } void main() { vColor = getColor(); gl_PointSize = pointSize; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); } `; /** Types of coloring used when viewing point cloud tiles */ var PointCloudColoring; (function (PointCloudColoring) { PointCloudColoring[PointCloudColoring["Intensity"] = 1] = "Intensity"; PointCloudColoring[PointCloudColoring["Classification"] = 2] = "Classification"; PointCloudColoring[PointCloudColoring["Elevation"] = 3] = "Elevation"; PointCloudColoring[PointCloudColoring["RGB"] = 4] = "RGB"; PointCloudColoring[PointCloudColoring["White"] = 5] = "White"; })(PointCloudColoring || (PointCloudColoring = {})); /** Types of shading used when viewing b3dm (mesh) tiles */ var Shading; (function (Shading) { Shading[Shading["FlatTexture"] = 1] = "FlatTexture"; Shading[Shading["ShadedTexture"] = 2] = "ShadedTexture"; Shading[Shading["ShadedNoTexture"] = 3] = "ShadedNoTexture"; })(Shading || (Shading = {})); var GeoTransform; (function (GeoTransform) { GeoTransform[GeoTransform["Reset"] = 1] = "Reset"; GeoTransform[GeoTransform["Mercator"] = 2] = "Mercator"; GeoTransform[GeoTransform["WGS84Cartesian"] = 3] = "WGS84Cartesian"; })(GeoTransform || (GeoTransform = {})); const gradient = Gradients$1.RAINBOW; const gradientTexture = typeof document != 'undefined' ? generateGradientTexture(gradient) : null; const grayscale = Gradients$1.GRAYSCALE; const grayscaleTexture = typeof document != 'undefined' ? generateGradientTexture(grayscale) : null; const defaultOptions = { throttleRequests: true, maxRequests: 64, updateInterval: 0.1, maxConcurrency: 1, maximumScreenSpaceError: 16, maximumMemoryUsage: 32, viewDistanceScale: 1.0, skipLevelOfDetail: false, updateTransforms: true, shading: Shading.FlatTexture, transparent: false, pointCloudColoring: PointCloudColoring.White, pointSize: 1.0, worker: true, wireframe: false, debug: false, basisTranscoderPath: null, dracoDecoderPath: null, material: null, computeNormals: false, shaderCallback: null, geoTransform: GeoTransform.Reset, preloadTilesCount: null }; /** 3D Tiles Loader */ class Loader3DTiles { /** * Loads a tileset of 3D Tiles according to the given {@link LoaderProps} * @public * * @param props - Properties for this load call {@link LoaderProps}. * @returns An object containing the 3D Model to be added to the scene * and a runtime engine to be updated every frame. */ static load(props) { return __awaiter(this, void 0, void 0, function* () { const options = Object.assign(Object.assign({}, defaultOptions), props.options); const { url } = props; const UPDATE_INTERVAL = options.updateInterval; const MAX_DEPTH_FOR_ORIENTATION = 5; const loadersGLOptions = {}; if (options.cesiumIONToken) { loadersGLOptions['cesium-ion'] = { accessToken: options.cesiumIONToken, }; const metadata = yield CesiumIonLoader.preload(url, loadersGLOptions); loadersGLOptions['fetch'] = { headers: metadata.headers }; } if (props.loadingManager) { props.loadingManager.itemStart(url); } const tilesetJson = yield load(url, Tiles3DLoader, Object.assign({}, loadersGLOptions)); const renderMap = {}; const boxMap = {}; const unloadQueue = []; const root = new Group(); const tileBoxes = new Group(); tileBoxes.matrixAutoUpdate = false;//add if (!options.debug) { tileBoxes.visible = false; }else { options.parent.add(tileBoxes); //add } const pointcloudUniforms = { pointSize: { type: 'f', value: options.pointSize }, gradient: { type: 't', value: gradientTexture }, grayscale: { type: 't', value: grayscaleTexture }, rootCenter: { type: 'vec3', value: new Vector3() }, rootNormal: { type: 'vec3', value: new Vector3() }, coloring: { type: 'i', value: options.pointCloudColoring }, hideGround: { type: 'b', value: true }, elevationRange: { type: 'vec2', value: new Vector2(0, 400) }, maxIntensity: { type: 'f', value: 1.0 }, intensityContrast: { type: 'f', value: 1.0 }, alpha: { type: 'f', value: 1.0 }, }; const pointcloudMaterial = new ShaderMaterial({ uniforms: pointcloudUniforms, vertexShader: PointCloudVS, fragmentShader: PointCloudFS, transparent: options.transparent, vertexColors: true }); let cameraReference = null; let rendererReference = null; const gltfLoader = props.gltfLoader || new GLTFLoader(); //xzw改 let ktx2Loader = undefined; let dracoLoader = undefined; if (options.basisTranscoderPath) { ktx2Loader = new KTX2Loader(); ktx2Loader.detectSupport(props.renderer); ktx2Loader.setTranscoderPath(options.basisTranscoderPath + '/'); ktx2Loader.setWorkerLimit(1); gltfLoader.setKTX2Loader(ktx2Loader); } if (options.dracoDecoderPath) { dracoLoader = new DRACOLoader(); dracoLoader.setDecoderPath(options.dracoDecoderPath + '/'); dracoLoader.setWorkerLimit(options.maxConcurrency); gltfLoader.setDRACOLoader(dracoLoader); } const unlitMaterial = new BasicMaterial({ transparent: options.transparent }); const tileOptions = { maximumMemoryUsage: options.maximumMemoryUsage, maximumScreenSpaceError: options.maximumScreenSpaceError, viewDistanceScale: options.viewDistanceScale, skipLevelOfDetail: options.skipLevelOfDetail, updateTransforms: options.updateTransforms, throttleRequests: options.throttleRequests, maxRequests: options.maxRequests, updateTime: options.updateTime || 0, //add //maxDepth: options.maxDepth || 50, // !zeg改 contentLoader: (tile) => __awaiter(this, void 0, void 0, function* () { let tileContent = null; switch (tile.type) { case TILE_TYPE.POINTCLOUD: { tileContent = createPointNodes(tile, pointcloudMaterial, options, rootTransformInverse); break; } case TILE_TYPE.SCENEGRAPH: case TILE_TYPE.MESH: { tileContent = yield createGLTFNodes(gltfLoader, tile, unlitMaterial, options, rootTransformInverse); break; } } if (tileContent) { tileContent.visible = false; renderMap[tile.id] = tileContent; tileContent.name = tile.id; //add tileContent.tile = tile; //add root.add(renderMap[tile.id]); if (options.debug) { const box = loadersBoundingBoxToMesh(tile); tileBoxes.add(box); boxMap[tile.id] = box; } //xzw : tileset.dispatchEvent({type:'tileLoaded',tileContent}); //每一个tile加载完要更改透明度等 } }), onTileLoad: (tile) => __awaiter(this, void 0, void 0, function* () { if (tileset) { if (!orientationDetected && (tile === null || tile === void 0 ? void 0 : tile.depth) <= MAX_DEPTH_FOR_ORIENTATION) { detectOrientation(tile); } tileset._frameNumber++; tilesetUpdate(tileset, renderMap, rendererReference, cameraReference); } }), onTileUnload: (tile) => { unloadQueue.push(tile); }, onTileError: (tile, message) => { console.error('Tile error', tile.id, message); }, }; const tileset = new Tileset3D(tilesetJson, Object.assign(Object.assign({}, tileOptions), { loadOptions: Object.assign(Object.assign({}, loadersGLOptions), { maxConcurrency: options.maxConcurrency, worker: options.worker, gltf: { loadImages: false, }, '3d-tiles': { loadGLTF: false } }) })); tileset.boxMap = boxMap; //add // transformations const threeMat = new Matrix4(); const tileTransform = new Matrix4(); const rootCenter = new Vector3(); let orientationDetected = false; if (tileset.root.boundingVolume) { if (tileset.root.header.boundingVolume.region) { // TODO: Handle region type bounding volumes // https://github.com/visgl/loaders.gl/issues/1994 console.warn("Cannot apply a model matrix to bounding volumes of type region. Tileset stays in original geo-coordinates."); options.geoTransform = GeoTransform.WGS84Cartesian; } tileTransform.setPosition(tileset.root.boundingVolume.center[0], tileset.root.boundingVolume.center[1], tileset.root.boundingVolume.center[2]); } else { console.warn("Bounding volume not found, no transformations applied"); } if (options.debug) { const box = loadersBoundingBoxToMesh(tileset.root); tileBoxes.add(box); boxMap[tileset.root.id] = box; } let disposeFlag = false; let loadingEnded = false; pointcloudUniforms.rootCenter.value.copy(rootCenter); pointcloudUniforms.rootNormal.value.copy(new Vector3(0, 0, 1).normalize()); // Extra stats tileset.stats.get('Loader concurrency').count = options.maxConcurrency; tileset.stats.get('Maximum SSE').count = options.maximumScreenSpaceError; tileset.stats.get('Maximum mem usage').count = options.maximumMemoryUsage; let timer = 0; let lastCameraTransform = null; let lastCameraAspect = null; const lastCameraPosition = new Vector3(Infinity, Infinity, Infinity); let sseDenominator = null; root.updateMatrixWorld(true); const lastRootTransform = new Matrix4().copy(root.matrixWorld); const rootTransformInverse = new Matrix4().copy(lastRootTransform).invert(); tileset.lastRootTransform = lastRootTransform; //add detectOrientation(tileset.root); updateResetTransform(); if (options.debug) { boxMap[tileset.root.id].applyMatrix4(threeMat); tileBoxes.matrixWorld.copy(root.matrixWorld); } if (options.geoTransform == GeoTransform.Mercator) { const coords = datumsToSpherical(tileset.cartographicCenter[1], tileset.cartographicCenter[0]); rootCenter.set(coords.x, 0, -coords.y); root.position.copy(rootCenter); root.rotation.set(-Math.PI / 2, 0, 0); root.updateMatrixWorld(true); } else if (options.geoTransform == GeoTransform.WGS84Cartesian) { root.applyMatrix4(tileTransform); root.updateMatrixWorld(true); rootCenter.copy(root.position); } function detectOrientation(tile) { if (!tile.boundingVolume.halfAxes) { return; } const halfAxes = tile.boundingVolume.halfAxes; const orientationMatrix = new Matrix4() .extractRotation(getMatrix4FromHalfAxes(halfAxes)) .premultiply(new Matrix4().extractRotation(rootTransformInverse)); const rotation = new Euler().setFromRotationMatrix(orientationMatrix); if (!rotation.equals(new Euler())) { orientationDetected = true; const pos = new Vector3(tileTransform.elements[12], tileTransform.elements[13], tileTransform.elements[14]); tileTransform.extractRotation(orientationMatrix); tileTransform.setPosition(pos); updateResetTransform(); } } function updateResetTransform() { if (options.geoTransform != GeoTransform.WGS84Cartesian) { // Reset the current model matrix and apply our own transformation //threeMat.copy(tileTransform).invert(); //threeMat.premultiply(lastRootTransform); //xzw删 被下面copy这句覆盖了 let tileTransInvert = new Matrix4().copy(tileTransform).invert(); threeMat.copy(lastRootTransform).multiply(tileTransInvert); tileset.modelMatrix = new Matrix4$1(threeMat.toArray()); //console.log('update tileset ModelMatrix', tileset.modelMatrix.elements) tileset.tileTransInvert = tileTransInvert.toArray(); // add for volumebox } } // 更新瓦片显隐和瓦片迭代更新 function tilesetUpdate(tileset, renderMap, renderer, camera) { if (disposeFlag || options.pauseTilesetUpdate) {// !zeg改 pauseTilesetUpdate return } // Assumes camera fov, near and far are not changing if (!sseDenominator || camera.aspect != lastCameraAspect) { const loadersFrustum = new PerspectiveFrustum({ fov: (camera.fov / 180) * Math.PI, aspectRatio: camera.aspect, near: camera.near, far: camera.far, }); sseDenominator = loadersFrustum.sseDenominator; lastCameraAspect = camera.aspect; if(camera.aspect == 0)return//add if (options.debug) { console.log('Updated sse denonimator:', sseDenominator); } } const frustum = getCameraFrustum(camera); const planes = frustum.planes.map((plane) => new Plane$1(plane.normal.toArray(), plane.constant)); const cullingVolume = new CullingVolume(planes); const rendererSize = new Vector2(); renderer.getSize(rendererSize); const frameState = { camera: { position: lastCameraPosition.toArray(), }, height: rendererSize.y, frameNumber: tileset._frameNumber, sseDenominator: sseDenominator, cullingVolume: cullingVolume, viewport: { id: 0, }, }; tileset._cache.reset(); tileset._traverser.traverse(tileset.root, frameState, tileset.options); for (const tile of tileset.tiles) { if (tile.selected) { if (!renderMap[tile.id]) { console.error('TILE SELECTED BUT NOT LOADED!!', tile.id); } else { // Make sure it's visible if(!renderMap[tile.id].visible){ if(viewer.visiVertexCount 0) { const tile = unloadQueue.pop(); if (renderMap[tile.id] && tile.contentState == TILE_CONTENT_STATE.UNLOADED) { //console.log('removevisi', renderMap[tile.id].visible) 如果是true,visiVertexCount要减 root.remove(renderMap[tile.id]); disposeNode(renderMap[tile.id]); delete renderMap[tile.id]; } if (boxMap[tile.id]) { disposeNode(boxMap[tile.id]); tileBoxes.remove(boxMap[tile.id]); delete boxMap[tile.id]; } } const tilesLoaded = tileset.stats.get('Tiles Loaded').count; const tilesLoading = tileset.stats.get('Tiles Loading').count; if (props.onProgress) { props.onProgress(tilesLoaded, tilesLoaded + tilesLoading); } if (props.loadingManager && !loadingEnded) { if (tilesLoading == 0 && (options.preloadTilesCount == null || tilesLoaded >= options.preloadTilesCount)) { loadingEnded = true; props.loadingManager.itemEnd(props.url); } } return frameState; } return { model: root, runtime: { getTileset: () => { return tileset; }, getStats: () => { return tileset.stats; }, showTiles: (visible) => { tileBoxes.visible = visible; }, setWireframe: (wireframe) => { options.wireframe = wireframe; root.traverse((object) => { if (object instanceof Mesh) { object.material.wireframe = wireframe; } }); }, setDebug: (debug) => { options.debug = debug; tileBoxes.visible = debug; }, setShading: (shading) => { options.shading = shading; }, getTileBoxes: () => { return tileBoxes; }, setViewDistanceScale: (scale) => { tileset.options.viewDistanceScale = scale; tileset._frameNumber++; tilesetUpdate(tileset, renderMap, rendererReference, cameraReference); }, setHideGround: (enabled) => { pointcloudUniforms.hideGround.value = enabled; }, setPointCloudColoring: (selection) => { pointcloudUniforms.coloring.value = selection; }, setElevationRange: (range) => { pointcloudUniforms.elevationRange.value.set(range[0], range[1]); }, setMaxIntensity: (intensity) => { pointcloudUniforms.maxIntensity.value = intensity; }, setIntensityContrast: (contrast) => { pointcloudUniforms.intensityContrast.value = contrast; }, setPointAlpha: (alpha) => { pointcloudUniforms.alpha.value = alpha; }, getLatLongHeightFromPosition: (position) => { const cartographicPosition = tileset.ellipsoid.cartesianToCartographic(new Vector3().copy(position).applyMatrix4(new Matrix4().copy(threeMat).invert()).toArray()); return { lat: cartographicPosition[1], long: cartographicPosition[0], height: cartographicPosition[2], }; }, getPositionFromLatLongHeight: (coord) => { const cartesianPosition = tileset.ellipsoid.cartographicToCartesian([coord.long, coord.lat, coord.height]); return new Vector3(...cartesianPosition).applyMatrix4(threeMat); }, getCameraFrustum: (camera) => { const frustum = getCameraFrustum(camera); const meshes = frustum.planes .map((plane) => new Plane$1(plane.normal.toArray(), plane.constant)) .map((loadersPlane) => loadersPlaneToMesh(loadersPlane)); const model = new Group(); for (const mesh of meshes) model.add(mesh); return model; }, update: function (dt, renderer, camera, ifForce) { cameraReference = camera; rendererReference = renderer; timer += dt; if(!ifForce) ifForce = tileset.nextForceUpdate; tileset.nextForceUpdate = false; if(tileset.needRenderNext){//必须在下一帧渲染刷新否则无法显示 viewer.dispatchEvent('content_changed'); } tileset.needRenderNext = ifForce; if (tileset && (timer >= UPDATE_INTERVAL || ifForce)) { if (!lastRootTransform.equals(root.matrixWorld)) { timer = 0; lastRootTransform.copy(root.matrixWorld); updateResetTransform(); const rootCenter = new Vector3().setFromMatrixPosition(lastRootTransform); pointcloudUniforms.rootCenter.value.copy(rootCenter); pointcloudUniforms.rootNormal.value.copy(new Vector3(0, 0, 1).applyMatrix4(lastRootTransform).normalize()); rootTransformInverse.copy(lastRootTransform).invert(); if (options.debug) { /* console.log('move', tileset.root.id, boxMap[tileset.root.id].matrix.elements, boxMap[tileset.root.id].matrixWorld.elements) boxMap[tileset.root.id].matrix.copy(root.matrixWorld); boxMap[tileset.root.id].matrixWorld.copy(root.matrixWorld); */ //boxMap[tileset.root.id].applyMatrix4(threeMat); tileBoxes.matrix.copy(root.matrixWorld); //boxMap[tileset.root.id].matrixWorld.copy(threeMat); //boxMap[tileset.root.id].applyMatrix4(lastRootTransform);//boxMap[tileset.root.id].applyMatrix4(lastRootTransform); //boxMap[tileset.root.id].updateWorldMatrix() } } if (this.lastCameraTransform == null) { this.lastCameraTransform = new Matrix4().copy(camera.matrixWorld); } else { const cameraChanged = !camera.matrixWorld.equals(this.lastCameraTransform) || !(camera.aspect == lastCameraAspect); if (cameraChanged || ifForce) { timer = 0; tileset._frameNumber++; camera.getWorldPosition(lastCameraPosition); this.lastCameraTransform.copy(camera.matrixWorld); tilesetUpdate(tileset, renderMap, renderer, camera); } } }/* else{ console.log('11') } */ }, dispose: function () { disposeFlag = true; tileset._destroy(); while (root.children.length > 0) { const obj = root.children[0]; disposeNode(obj); root.remove(obj); } while (tileBoxes.children.length > 0) { const obj = tileBoxes.children[0]; tileBoxes.remove(obj); obj.geometry.dispose(); obj.material.dispose(); } if (ktx2Loader) { ktx2Loader.dispose(); } if (dracoLoader) { dracoLoader.dispose(); } }, limit2lowestDepth: isLowest => {//zeg add 设置是否限制为最低精度tile深度 maxDepth = isLowest ? 1 : 100; //影响到shouldRefine, 检查方法:可以看到当maxDepth降低了之后viewer.objs.children[0].runtime.getTileset().tiles.filter(e=> e.tileContent.visible)变少 if (cameraReference) { tileset._frameNumber++; tilesetUpdate(tileset, renderMap, rendererReference, cameraReference); } }, }, }; }); } } function createGLTFNodes(gltfLoader, tile, unlitMaterial, options, rootTransformInverse) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { var _a; const rotateX = new Matrix4().makeRotationAxis(new Vector3(1, 0, 0), Math.PI / 2); const shouldRotate = ((_a = tile.tileset.asset) === null || _a === void 0 ? void 0 : _a.gltfUpAxis) !== "Z"; // The computed trasnform already contains the root's transform, so we have to invert it //const contentTransform = new Matrix4$1().fromArray(tile.computedTransform).premultiply(tileMatrix)//.premultiply(rootTransformInverse); //xzw 删。原先的会造成移动后tiles错乱 //const contentTransform = new Matrix4$1().fromArray(tile.computedTransform).premultiply(tile.tileset.modelMatrix).premultiply(rootTransformInverse) // 这句同tile.computedTransform左乘tileTransform。 因为tileTransform拿不到所以使用modelMatrix。modelMatrix为tileTransform左乘rootTransform, 所以再左乘rootTransformInverse //改动:contentTransform中的computedTransform 去掉左乘 root.modelMatrixWorld , 因为移动过后这个computedTransform没更新 gltfLoader.parse( tile.content.type == 'glTF' ? tile.content.gltf.gltfArrayBuffer : tile.content.gltfArrayBuffer,//tile.content.gltfArrayBuffer, tile.contentUrl ? tile.contentUrl.substr(0, tile.contentUrl.lastIndexOf('/') + 1) : '', (gltf) => { const tileContent = gltf.scenes[0]; tileContent.vertexCount = 0; //xzw add tile.tileContent = tileContent; //xzw add let needUpdate; if(tile.rtcCenterState != !!tile.content.rtcCenter){ // 等mesh加载好才知道rtcCenter,每个mesh坐标都不一样 needUpdate = true; //console.error('rtcCenter有变化?!') } tile.rtcCenterState = rtcCenterGlobal = !!tile.content.rtcCenter; let contentTransform = tile.getContentTransform(needUpdate); if(tile.content.rtcCenter){//大部分模型无 rtcCenter //console.error('rtcCenter有?!') //似乎bounding中包含这个信息了 所以getContentTransform不含这个 let tranM = new Matrix4(); tranM.makeTranslation(tile.content.rtcCenter[0], tile.content.rtcCenter[1], tile.content.rtcCenter[2]); contentTransform.premultiply(tranM); } if(needUpdate ){ tile._updateBoundingVolume(tile.header); //add 重新更新 } if (shouldRotate) { //大部分模型 无需 Rotate contentTransform.multiply(rotateX); // convert from GLTF Y-up to Z-up } tileContent.applyMatrix4(contentTransform); //'https://testgis.4dage.com/LVBADUI_qp/tileset.json', //村庄 这个案例是 要rotateX 且有rtcCenter的。boundingVolume已经是转换这两者后的值所以getContentTransform不加这个 //tile总共要左乘的矩阵 rotateX -> tileTransInvert * computedTransform -> 对geometry的tranM修改 -> 整个模型的matrixWorld tileContent.traverse((object) => { if (object.type == "Mesh") { const mesh = object; //mesh.tileId = tile.id//add const originalMaterial = mesh.material; const originalMap = originalMaterial.map; if (options.material) { mesh.material = options.material.clone(); originalMaterial.dispose(); } else if (options.shading == Shading.FlatTexture) { mesh.material = unlitMaterial.clone(); originalMaterial.dispose(); } if (options.shading != Shading.ShadedNoTexture) { if (mesh.material.type == "ShaderMaterial" && !(mesh.material instanceof BasicMaterial)) {//改 mesh.material.uniforms.map = { value: originalMap }; } else { mesh.material.map = originalMap; } } else { if (originalMap) { originalMap.dispose(); } mesh.material.map = null; } if (options.shaderCallback) { mesh.onBeforeRender = options.shaderCallback; } mesh.material.wireframe = options.wireframe; if (options.computeNormals) { mesh.geometry.computeVertexNormals(); } //xzw add: tileContent.vertexCount += mesh.geometry.attributes.position.count; //------------------add on 2023.1.16----zeg //mesh.geometry.applyMatrix4(contentTransform) /* if (tile.content.rtcCenter) { // 有些b3dm模型会将坐标写在源码的rtcCenter里 如https://testgis.4dage.com/LVBADUI_qp/tileset.json mesh.geometry.translate(tile.content.rtcCenter[0], tile.content.rtcCenter[1], tile.content.rtcCenter[2]) } else { mesh.geometry.scale(1, 1, -1) // 调整缩放,对应box也要进行变换(scaleZ对应box[2]) } */ //--------------------- } }); resolve(tileContent); }, (e) => { reject(new Error(`error parsing gltf in tile ${tile.id}: ${e}`)); } ); }); }); } function createPointNodes(tile, pointcloudMaterial, options, rootTransformInverse) { const d = { rtc_center: tile.content.rtcCenter, points: tile.content.attributes.positions, intensities: tile.content.attributes.intensity, classifications: tile.content.attributes.classification, rgb: null, rgba: null, }; const { colors } = tile.content.attributes; if (colors && colors.size === 3) { d.rgb = colors.value; } if (colors && colors.size === 4) { d.rgba = colors.value; } const geometry = new BufferGeometry(); geometry.setAttribute('position', new Float32BufferAttribute(d.points, 3)); const contentTransform = new Matrix4().fromArray(tile.computedTransform).premultiply(rootTransformInverse); if (d.rgba) { geometry.setAttribute('color', new Float32BufferAttribute(d.rgba, 4)); } else if (d.rgb) { geometry.setAttribute('color', new Uint8BufferAttribute(d.rgb, 3, true)); } if (d.intensities) { geometry.setAttribute('intensity', // Handles both 16bit or 8bit intensity values new BufferAttribute(d.intensities, 1, true)); } if (d.classifications) { geometry.setAttribute('classification', new Uint8BufferAttribute(d.classifications, 1, false)); } const tileContent = new Points(geometry, options.material || pointcloudMaterial); if (d.rtc_center) { const c = d.rtc_center; contentTransform.multiply(new Matrix4().makeTranslation(c[0], c[1], c[2])); } tileContent.applyMatrix4(contentTransform); return tileContent; } function disposeMaterial(material) { var _a, _b, _c, _d; if ((_a = material === null || material === void 0 ? void 0 : material.uniforms) === null || _a === void 0 ? void 0 : _a.map) { (_c = (_b = material === null || material === void 0 ? void 0 : material.uniforms) === null || _b === void 0 ? void 0 : _b.map.value) === null || _c === void 0 ? void 0 : _c.dispose(); } else if (material.map) { (_d = material.map) === null || _d === void 0 ? void 0 : _d.dispose(); } material.dispose(); } function disposeNode(node) { node.traverse((object) => { if (object.isMesh) { object.geometry.dispose(); if (object.material.isMaterial) { disposeMaterial(object.material); } else { // an array of materials for (const material of object.material) { disposeMaterial(material); } } } }); for (let i = node.children.length - 1; i >= 0; i--) { const obj = node.children[i]; node.remove(obj); } } /* 关键搜寻: Loader3DTiles : const tileset = new Tileset3D tileset.dispatchEvent({type:'tileLoaded',tileContent}) createGLTFNodes(gltfLoader 创建tile executeTraversal(root, frameState) 遍历root lastRootTransform rootTransformInverse tileset._loadTiles 依次加载tiles _getPriority 加载优先级 加了一句 笔记: 主要类: class tileset3D 最外层整体。 获取方法: viewer.objs.children[index].runtime.getTileset()。 .tiles包含所有tiles class tileHeader 也就是tileset里面的一个个tile 。 其上有.tileset tileset里算的tileTransform之后是不会改变的, tile.transform也是,是json里的。 tileset.modelMatrix (通常这个值很大) 会随着位移改变 tile.computedTransform 被我修改了,之前左乘了tileset.modelMatrix,现只包含了transform信息 _updateTransform(parentTransfor 其中有_updateBoundingVolume,这个影响可见性, 在computeVisibilityWithPlaneMask中计算 tileset._cache.trim(), 然后再update //可以将所有不可见的tiles dispose 相机靠近容易缺块的bug暂时修改如下: _visible是由computeVisibilityWithPlaneMask计算得 get isVisibleAndInRequestVolume() { return this._inRequestVolume; //去掉了_visible || } 显示所有tile似乎也不会卡顿. 但好像影响了加载顺序从而变慢了。visiVertexCount过量。 怀疑应该还是文件的bound有问题。 原本会消失的地方虽然不会消失了但一直是模糊的 ========= 访问所有tile: viewer.objs.children[0].runtime.getTileset().tiles 它的mesh: tile.tileContent 或者直接得到所有mesh: viewer.objs.children[0].children tile.depth最低为1 , root是0 */ /** * Description: A THREE loader for PLY ASCII files (known as the Polygon * File Format or the Stanford Triangle Format). * * Limitations: ASCII decoding assumes file is UTF-8. * * Usage: * var loader = new PLYLoader(); * loader.load('./models/ply/ascii/dolphins.ply', function (geometry) { * * scene.add( new THREE.Mesh( geometry ) ); * * } ); * * If the PLY file uses non standard property names, they can be mapped while * loading. For example, the following maps the properties * “diffuse_(red|green|blue)” in the file to standard color names. * * loader.setPropertyNameMapping( { * diffuse_red: 'red', * diffuse_green: 'green', * diffuse_blue: 'blue' * } ); * */ var PLYLoader = function ( manager ) { Loader.call( this, manager ); this.propertyNameMapping = {}; }; PLYLoader.prototype = Object.assign( Object.create( Loader.prototype ), { constructor: PLYLoader, load: function ( url, onLoad, onProgress, onError ) { var scope = this; var loader = new FileLoader( this.manager ); loader.setPath( this.path ); loader.setResponseType( 'arraybuffer' ); loader.setRequestHeader( this.requestHeader ); loader.setWithCredentials( this.withCredentials ); loader.load( url, function ( text ) { try { onLoad( scope.parse( text ) ); } catch ( e ) { if ( onError ) { onError( e ); } else { console.error( e ); } scope.manager.itemError( url ); } }, onProgress, onError ); }, setPropertyNameMapping: function ( mapping ) { this.propertyNameMapping = mapping; }, parse: function ( data ) { function parseHeader( data ) { var patternHeader = /ply([\s\S]*)end_header\r?\n/; var headerText = ''; var headerLength = 0; var result = patternHeader.exec( data ); if ( result !== null ) { headerText = result[ 1 ]; headerLength = new Blob( [ result[ 0 ] ] ).size; } var header = { comments: [], elements: [], headerLength: headerLength, objInfo: '' }; var lines = headerText.split( '\n' ); var currentElement; var lineType, lineValues; function make_ply_element_property( propertValues, propertyNameMapping ) { var property = { type: propertValues[ 0 ] }; if ( property.type === 'list' ) { property.name = propertValues[ 3 ]; property.countType = propertValues[ 1 ]; property.itemType = propertValues[ 2 ]; } else { property.name = propertValues[ 1 ]; } if ( property.name in propertyNameMapping ) { property.name = propertyNameMapping[ property.name ]; } return property; } for ( var i = 0; i < lines.length; i ++ ) { var line = lines[ i ]; line = line.trim(); if ( line === '' ) continue; lineValues = line.split( /\s+/ ); lineType = lineValues.shift(); line = lineValues.join( ' ' ); switch ( lineType ) { case 'format': header.format = lineValues[ 0 ]; header.version = lineValues[ 1 ]; break; case 'comment': header.comments.push( line ); break; case 'element': if ( currentElement !== undefined ) { header.elements.push( currentElement ); } currentElement = {}; currentElement.name = lineValues[ 0 ]; currentElement.count = parseInt( lineValues[ 1 ] ); currentElement.properties = []; break; case 'property': currentElement.properties.push( make_ply_element_property( lineValues, scope.propertyNameMapping ) ); break; case 'obj_info': header.objInfo = line; break; default: console.log( 'unhandled', lineType, lineValues ); } } if ( currentElement !== undefined ) { header.elements.push( currentElement ); } return header; } function parseASCIINumber( n, type ) { switch ( type ) { case 'char': case 'uchar': case 'short': case 'ushort': case 'int': case 'uint': case 'int8': case 'uint8': case 'int16': case 'uint16': case 'int32': case 'uint32': return parseInt( n ); case 'float': case 'double': case 'float32': case 'float64': return parseFloat( n ); } } function parseASCIIElement( properties, line ) { var values = line.split( /\s+/ ); var element = {}; for ( var i = 0; i < properties.length; i ++ ) { if ( properties[ i ].type === 'list' ) { var list = []; var n = parseASCIINumber( values.shift(), properties[ i ].countType ); for ( var j = 0; j < n; j ++ ) { list.push( parseASCIINumber( values.shift(), properties[ i ].itemType ) ); } element[ properties[ i ].name ] = list; } else { element[ properties[ i ].name ] = parseASCIINumber( values.shift(), properties[ i ].type ); } } return element; } function parseASCII( data, header ) { // PLY ascii format specification, as per http://en.wikipedia.org/wiki/PLY_(file_format) var buffer = { indices: [], vertices: [], normals: [], uvs: [], faceVertexUvs: [], colors: [] }; var result; var patternBody = /end_header\s([\s\S]*)$/; var body = ''; if ( ( result = patternBody.exec( data ) ) !== null ) { body = result[ 1 ]; } var lines = body.split( '\n' ); var currentElement = 0; var currentElementCount = 0; for ( var i = 0; i < lines.length; i ++ ) { var line = lines[ i ]; line = line.trim(); if ( line === '' ) { continue; } if ( currentElementCount >= header.elements[ currentElement ].count ) { currentElement ++; currentElementCount = 0; } var element = parseASCIIElement( header.elements[ currentElement ].properties, line ); handleElement( buffer, header.elements[ currentElement ].name, element ); currentElementCount ++; } return postProcess( buffer ); } function postProcess( buffer ) { var geometry = new BufferGeometry(); // mandatory buffer data if ( buffer.indices.length > 0 ) { geometry.setIndex( buffer.indices ); } geometry.setAttribute( 'position', new Float32BufferAttribute( buffer.vertices, 3 ) ); // optional buffer data if ( buffer.normals.length > 0 ) { geometry.setAttribute( 'normal', new Float32BufferAttribute( buffer.normals, 3 ) ); } if ( buffer.uvs.length > 0 ) { geometry.setAttribute( 'uv', new Float32BufferAttribute( buffer.uvs, 2 ) ); } if ( buffer.colors.length > 0 ) { geometry.setAttribute( 'color', new Float32BufferAttribute( buffer.colors, 3 ) ); } if ( buffer.faceVertexUvs.length > 0 ) { geometry = geometry.toNonIndexed(); geometry.setAttribute( 'uv', new Float32BufferAttribute( buffer.faceVertexUvs, 2 ) ); } geometry.computeBoundingSphere(); return geometry; } function handleElement( buffer, elementName, element ) { if ( elementName === 'vertex' ) { buffer.vertices.push( element.x, element.y, element.z ); if ( 'nx' in element && 'ny' in element && 'nz' in element ) { buffer.normals.push( element.nx, element.ny, element.nz ); } if ( 's' in element && 't' in element ) { buffer.uvs.push( element.s, element.t ); } if ( 'red' in element && 'green' in element && 'blue' in element ) { buffer.colors.push( element.red / 255.0, element.green / 255.0, element.blue / 255.0 ); } } else if ( elementName === 'face' ) { var vertex_indices = element.vertex_indices || element.vertex_index; // issue #9338 var texcoord = element.texcoord; if ( vertex_indices.length === 3 ) { buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 2 ] ); if ( texcoord && texcoord.length === 6 ) { buffer.faceVertexUvs.push( texcoord[ 0 ], texcoord[ 1 ] ); buffer.faceVertexUvs.push( texcoord[ 2 ], texcoord[ 3 ] ); buffer.faceVertexUvs.push( texcoord[ 4 ], texcoord[ 5 ] ); } } else if ( vertex_indices.length === 4 ) { buffer.indices.push( vertex_indices[ 0 ], vertex_indices[ 1 ], vertex_indices[ 3 ] ); buffer.indices.push( vertex_indices[ 1 ], vertex_indices[ 2 ], vertex_indices[ 3 ] ); } } } function binaryRead( dataview, at, type, little_endian ) { switch ( type ) { // corespondences for non-specific length types here match rply: case 'int8': case 'char': return [ dataview.getInt8( at ), 1 ]; case 'uint8': case 'uchar': return [ dataview.getUint8( at ), 1 ]; case 'int16': case 'short': return [ dataview.getInt16( at, little_endian ), 2 ]; case 'uint16': case 'ushort': return [ dataview.getUint16( at, little_endian ), 2 ]; case 'int32': case 'int': return [ dataview.getInt32( at, little_endian ), 4 ]; case 'uint32': case 'uint': return [ dataview.getUint32( at, little_endian ), 4 ]; case 'float32': case 'float': return [ dataview.getFloat32( at, little_endian ), 4 ]; case 'float64': case 'double': return [ dataview.getFloat64( at, little_endian ), 8 ]; } } function binaryReadElement( dataview, at, properties, little_endian ) { var element = {}; var result, read = 0; for ( var i = 0; i < properties.length; i ++ ) { if ( properties[ i ].type === 'list' ) { var list = []; result = binaryRead( dataview, at + read, properties[ i ].countType, little_endian ); var n = result[ 0 ]; read += result[ 1 ]; for ( var j = 0; j < n; j ++ ) { result = binaryRead( dataview, at + read, properties[ i ].itemType, little_endian ); list.push( result[ 0 ] ); read += result[ 1 ]; } element[ properties[ i ].name ] = list; } else { result = binaryRead( dataview, at + read, properties[ i ].type, little_endian ); element[ properties[ i ].name ] = result[ 0 ]; read += result[ 1 ]; } } return [ element, read ]; } function parseBinary( data, header ) { var buffer = { indices: [], vertices: [], normals: [], uvs: [], faceVertexUvs: [], colors: [] }; var little_endian = ( header.format === 'binary_little_endian' ); var body = new DataView( data, header.headerLength ); var result, loc = 0; for ( var currentElement = 0; currentElement < header.elements.length; currentElement ++ ) { for ( var currentElementCount = 0; currentElementCount < header.elements[ currentElement ].count; currentElementCount ++ ) { result = binaryReadElement( body, loc, header.elements[ currentElement ].properties, little_endian ); loc += result[ 1 ]; var element = result[ 0 ]; handleElement( buffer, header.elements[ currentElement ].name, element ); } } return postProcess( buffer ); } // var geometry; var scope = this; if ( data instanceof ArrayBuffer ) { var text = LoaderUtils.decodeText( new Uint8Array( data ) ); var header = parseHeader( text ); geometry = header.format === 'ascii' ? parseASCII( text, header ) : parseBinary( data, header ); } else { geometry = parseASCII( data, parseHeader( data ) ); } return geometry; } } ); /** * * Supersample Anti-Aliasing Render Pass * * @author bhouston / http://clara.io/ * * This manual approach to SSAA re-renders the scene ones for each sample with camera jitter and accumulates the results. * * References: https://en.wikipedia.org/wiki/Supersampling * */ //较为原始的一种抗锯齿 (超级采样抗锯齿) let SSAARenderPass = function ( clearColor, clearAlpha ) { Pass.call( this ); //this.scene //= scene; //this.camera = camera; this.sampleLevel = 4; // specified as n, where the number of samples is 2^n, so sampleLevel = 4, is 2^4 samples, 16. this.unbiased = true; // as we need to clear the buffer in this pass, clearColor must be set to something, defaults to black. this.clearColor = ( clearColor !== undefined ) ? clearColor : 0x000000; this.clearAlpha = ( clearAlpha !== undefined ) ? clearAlpha : 0; this.renderUniforms = { bgTex : {value:null}, outlineTex : {value:null}, opacity : {value:1}, }; this.renderMat = new ShaderMaterial({ uniforms: this.renderUniforms, vertexShader: CopyShader.vertexShader, /* fragmentShader: CopyShader.fragmentShader, */ fragmentShader: ` uniform sampler2D bgTex; uniform sampler2D outlineTex; uniform float opacity; varying vec2 vUv; void main() { vec4 color1 = texture2D( bgTex, vUv ); vec4 color2 = texture2D( outlineTex, vUv ); gl_FragColor = opacity * mix(color1, color2, color2.a) ; } `, premultipliedAlpha: true, blending: AdditiveBlending, depthTest: false, depthWrite: false, transparent: true }); this.renderMat2 = new ShaderMaterial({ uniforms: UniformsUtils.clone(CopyShader.uniforms) , vertexShader: CopyShader.vertexShader, fragmentShader:`uniform float opacity; uniform sampler2D tDiffuse; varying vec2 vUv; void main() { vec4 texel = texture2D( tDiffuse, vUv ); if(texel.r == 0.0 && texel.g == 0.0 && texel.b == 0.0){ discard; }else{ gl_FragColor = opacity * texel; } } ` , depthTest: false, depthWrite: false, transparent: true }); //////////////////// /* this.renderMat.blendSrc = THREE.OneFactor //即将写入缓冲区的颜色。 this.renderMat.blendDst = THREE.OneFactor //缓冲区已经存在的颜色 this.renderMat.blendEquation = THREE.AddEquation; this.renderMat.blendEquationAlpha = THREE.AddEquation; this.renderMat.blendDstAlpha = THREE.SrcAlphaFactor this.renderMat.blendSrcAlpha = THREE.SrcAlphaFactor */ this.camera2 = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 ); this.scene2 = new Scene(); this.quad2 = new Mesh( new PlaneBufferGeometry( 2, 2 ), this.renderMat/* this.copyMaterial */ ); this.quad2.frustumCulled = false; // Avoid getting clipped this.scene2.add( this.quad2 ); this.copyPass = new ShaderPass( CopyShader ); this.copyPass.renderToScreen = true; }; SSAARenderPass.prototype = Object.assign( Object.create( Pass.prototype ), { constructor: SSAARenderPass, dispose: function () { if ( this.sampleRenderTarget ) { this.sampleRenderTarget.dispose(); this.sampleRenderTarget = null; } }, setSize: function ( width, height ) { if ( this.sampleRenderTarget ) this.sampleRenderTarget.setSize( width, height ); this.childPass && this.childPass.setSize(width, height); }, addPass: function (pass){ this.childPass = pass; }, render: function (scene, camera, viewports, renderer, writeBuffer, readBuffer, maskActive, renderFun ) { if(this.useCopy ){ scene = this.copyPass.scene; camera = this.copyPass.camera; } if ( ! this.sampleRenderTarget ) { this.sampleRenderTarget = new WebGLRenderTarget( readBuffer.width, readBuffer.height, { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat } ); this.sampleRenderTarget.texture.name = "SSAARenderPass.sample"; } var jitterOffsets = SSAARenderPass.JitterVectors[ Math.max( 0, Math.min( this.sampleLevel, 5 ) ) ]; var autoClear = renderer.autoClear; renderer.autoClear = false; var oldClearColor = renderer.getClearColor(new Color).getHex(); var oldClearAlpha = renderer.getClearAlpha(); renderer.setClearColor( this.clearColor, this.clearAlpha ); var baseSampleWeight = 1.0 / jitterOffsets.length; var roundingRange = 1 / 32; //this.copyUniforms[ "tDiffuse" ].value = this.sampleRenderTarget.texture; let oldTarget = renderer.getRenderTarget(); if(oldTarget){ if(oldTarget.scissorTest){ var width = oldTarget.scissor.w, height = oldTarget.scissor.z; }else { var width = oldTarget.width, height = oldTarget.height; } }else { var width = readBuffer.width, height = readBuffer.height; } // render the scene multiple times, each slightly jitter offset from the last and accumulate the results. let opa = 0; for ( var i = 0; i < jitterOffsets.length; i ++ ) { var jitterOffset = jitterOffsets[ i ]; if ( camera.setViewOffset ) { camera.setViewOffset( width, height, jitterOffset[ 0 ] * 0.0625 , jitterOffset[ 1 ] * 0.0625 , // 0.0625 = 1 / 16 width, height ); } var sampleWeight = baseSampleWeight; if ( this.unbiased ) {//更柔和 var uniformCenteredDistribution = ( - 0.5 + ( i + 0.5 ) / jitterOffsets.length ); sampleWeight += roundingRange * uniformCenteredDistribution; } renderer.setRenderTarget(this.sampleRenderTarget); renderer.clear(); if(this.useCopy){ this.copyPass.render(scene,camera, null,renderer, writeBuffer, readBuffer ); }else { if(renderFun){ renderFun({target : this.sampleRenderTarget}); }else { renderer.render( scene, camera ); } } renderer.setRenderTarget(oldTarget); //--------------------- //获取outline tex let hasOutline = this.childPass && this.childPass.render(scene, camera, renderer, writeBuffer, readBuffer, null, renderFun ); //合成到该材质 this.renderUniforms[ "bgTex" ].value = this.sampleRenderTarget.texture; this.renderUniforms[ "outlineTex" ].value = hasOutline ? readBuffer.texture : null; this.renderUniforms[ "opacity" ].value = sampleWeight; /* console.log('sampleWeight', sampleWeight) opa += sampleWeight */ if(!this.renderToScreen){ renderer.setRenderTarget(writeBuffer); } if(i === 0 ){ renderer.setClearColor( 0x000000, 0 ); //叠加前颜色必须0 renderer.clear(); } renderer.render( this.scene2, this.camera2); // , this.renderToScreen ? null : writeBuffer, ( i === 0 ) if(!this.renderToScreen){ renderer.setRenderTarget(oldTarget); } //if(i==2)break; } //console.log('sum:',opa) if ( camera.clearViewOffset )camera.clearViewOffset(); //renderer.setRenderTarget(readBuffer) //renderer.setClearColor( 0x000000, 0 ); //renderer.clear() /* this.quad2.material = this.renderMat2 this.renderMat2.uniforms.tDiffuse.value = writeBuffer.texture; renderer.render( this.scene2, this.camera2); this.quad2.material = this.renderMat */ //renderer.setRenderTarget(oldTarget) renderer.autoClear = autoClear; renderer.setClearColor( oldClearColor, oldClearAlpha ); /* 试了好几次,测量线的透明度还是还原不了。 clearAlpha十分影响结果。 因为绘制测量线需要背景透明。 或许可以先全部绘制完后,再 copyshader中 抗锯齿? 另外会有黑边。 */ } } ); // These jitter vectors are specified in integers because it is easier. // I am assuming a [-8,8) integer grid, but it needs to be mapped onto [-0.5,0.5) // before being used, thus these integers need to be scaled by 1/16. // // Sample patterns reference: https://msdn.microsoft.com/en-us/library/windows/desktop/ff476218%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396 SSAARenderPass.JitterVectors = [ [ [ 0, 0 ] ], [ [ 4, 4 ], [ - 4, - 4 ] ], [ [ - 2, - 6 ], [ 6, - 2 ], [ - 6, 2 ], [ 2, 6 ] ], [ [ 1, - 3 ], [ - 1, 3 ], [ 5, 1 ], [ - 3, - 5 ], [ - 5, 5 ], [ - 7, - 1 ], [ 3, 7 ], [ 7, - 7 ] ], [ [ 1, 1 ], [ - 1, - 3 ], [ - 3, 2 ], [ 4, - 1 ], [ - 5, - 2 ], [ 2, 5 ], [ 5, 3 ], [ 3, - 5 ], [ - 2, 6 ], [ 0, - 7 ], [ - 4, - 6 ], [ - 6, 4 ], [ - 8, 0 ], [ 7, - 4 ], [ 6, 7 ], [ - 7, - 8 ] ], [ [ - 4, - 7 ], [ - 7, - 5 ], [ - 3, - 5 ], [ - 5, - 4 ], [ - 1, - 4 ], [ - 2, - 2 ], [ - 6, - 1 ], [ - 4, 0 ], [ - 7, 1 ], [ - 1, 2 ], [ - 6, 3 ], [ - 3, 3 ], [ - 7, 6 ], [ - 3, 6 ], [ - 5, 7 ], [ - 1, 7 ], [ 5, - 7 ], [ 1, - 6 ], [ 6, - 5 ], [ 4, - 4 ], [ 2, - 3 ], [ 7, - 2 ], [ 1, - 1 ], [ 4, - 1 ], [ 2, 1 ], [ 6, 2 ], [ 0, 4 ], [ 4, 4 ], [ 2, 5 ], [ 7, 5 ], [ 5, 6 ], [ 3, 7 ] ] ]; class MaskPass extends Pass { constructor( scene, camera ) { super(); this.scene = scene; this.camera = camera; this.clear = true; this.needsSwap = false; this.inverse = false; } render( renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) { const context = renderer.getContext(); const state = renderer.state; // don't update color or depth state.buffers.color.setMask( false ); state.buffers.depth.setMask( false ); // lock buffers state.buffers.color.setLocked( true ); state.buffers.depth.setLocked( true ); // set up stencil let writeValue, clearValue; if ( this.inverse ) { writeValue = 0; clearValue = 1; } else { writeValue = 1; clearValue = 0; } state.buffers.stencil.setTest( true ); state.buffers.stencil.setOp( context.REPLACE, context.REPLACE, context.REPLACE ); state.buffers.stencil.setFunc( context.ALWAYS, writeValue, 0xffffffff ); state.buffers.stencil.setClear( clearValue ); state.buffers.stencil.setLocked( true ); // draw into the stencil buffer renderer.setRenderTarget( readBuffer ); if ( this.clear ) renderer.clear(); renderer.render( this.scene, this.camera ); renderer.setRenderTarget( writeBuffer ); if ( this.clear ) renderer.clear(); renderer.render( this.scene, this.camera ); // unlock color and depth buffer for subsequent rendering state.buffers.color.setLocked( false ); state.buffers.depth.setLocked( false ); // only render where stencil is set to 1 state.buffers.stencil.setLocked( false ); state.buffers.stencil.setFunc( context.EQUAL, 1, 0xffffffff ); // draw if == 1 state.buffers.stencil.setOp( context.KEEP, context.KEEP, context.KEEP ); state.buffers.stencil.setLocked( true ); } } class ClearMaskPass extends Pass { constructor() { super(); this.needsSwap = false; } render( renderer /*, writeBuffer, readBuffer, deltaTime, maskActive */ ) { renderer.state.buffers.stencil.setLocked( false ); renderer.state.buffers.stencil.setTest( false ); } } /** * @author alteredq / http://alteredqualia.com/ */ var EffectComposer = function ( renderer, renderTarget ) { this.renderer = renderer; if ( renderTarget === undefined ) { var parameters = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat, stencilBuffer: false, }; var size = renderer.getDrawingBufferSize(new Vector2); renderTarget = new WebGLRenderTarget( size.width, size.height , parameters ); renderTarget.texture.name = 'EffectComposer.rt1'; } this.renderTarget1 = renderTarget; this.renderTarget2 = renderTarget.clone(); this.renderTarget2.texture.name = 'EffectComposer.rt2'; this.writeBuffer = this.renderTarget1; this.readBuffer = this.renderTarget2; this.passes = []; // dependencies /* if ( THREE.CopyShader === undefined ) { console.error( 'THREE.EffectComposer relies on THREE.CopyShader' ); } if ( THREE.ShaderPass === undefined ) { console.error( 'THREE.EffectComposer relies on THREE.ShaderPass' ); } */ this.copyPass = new ShaderPass( CopyShader ); viewer.addEventListener('resize',(e)=>{ //readTarget的话是渲染一整张的,暂时无法一个个viewport渲染所以不用 this.readTarget || this.setSize(e.viewport.resolution2.x,e.viewport.resolution2.y); //暂时假设composer渲染的viewer的viewports.length == 1 }); }; Object.assign( EffectComposer.prototype, { swapBuffers: function () { var tmp = this.readBuffer; this.readBuffer = this.writeBuffer; this.writeBuffer = tmp; }, addPass: function ( pass ) { this.passes.push( pass ); var size = this.renderer.getDrawingBufferSize(new Vector2); pass.setSize( size.width, size.height ); }, removePass: function(pass){ //add let index = this.passes.indexOf(pass); index > -1 && this.passes.splice(index,1); }, insertPass: function ( pass, index ) { this.passes.splice( index, 0, pass ); }, render: function ( scene, camera, viewports, renderFun ) { var maskActive = false; let passes = this.passes.filter(e=>e.enabled); var pass, i, il = passes.length; let oldTarget = this.renderer.getRenderTarget(); if(this.readTarget && oldTarget){ //add 使用当前renderTarget中的像素 this.setSize(oldTarget.width, oldTarget.height);//所有viewports一起渲染,整个画面 因暂时没办法一个个copy oldTarget.viewport.set(0, 0, oldTarget.width, oldTarget.height); oldTarget.scissorTest = false; this.copyPass.render( scene, camera,viewports, this.renderer, this.readBuffer, oldTarget ); } for ( i = 0; i < il; i ++ ) { pass = passes[ i ]; //if(i == il-1)pass.renderToScreen = true// pass.render( scene, camera, viewports, this.renderer, this.writeBuffer, this.readBuffer, maskActive, renderFun ); if ( pass.needsSwap ) { if ( maskActive ) { var context = this.renderer.context; context.stencilFunc( context.NOTEQUAL, 1, 0xffffffff ); this.copyPass.render(null,null, viewports, this.renderer, this.writeBuffer, this.readBuffer );// delta context.stencilFunc( context.EQUAL, 1, 0xffffffff ); } this.swapBuffers(); } if ( MaskPass !== undefined ) { if ( pass instanceof MaskPass ) { maskActive = true; } else if ( pass instanceof ClearMaskPass ) { maskActive = false; } } } this.renderer.setRenderTarget(oldTarget); //add if(!pass.renderToScreen){ //最后一个如果没有绘制到屏幕or target上 this.copyPass.renderToScreen = true; this.copyPass.render(null,null, viewports,this.renderer, this.writeBuffer, this.readBuffer); } }, reset: function ( renderTarget ) { if ( renderTarget === undefined ) { var size = this.renderer.getDrawingBufferSize(new Vector2); renderTarget = this.renderTarget1.clone(); renderTarget.setSize( size.width, size.height ); } this.renderTarget1.dispose(); this.renderTarget2.dispose(); this.renderTarget1 = renderTarget; this.renderTarget2 = renderTarget.clone(); this.writeBuffer = this.renderTarget1; this.readBuffer = this.renderTarget2; }, setSize: function ( width, height, scaleRatio ) { scaleRatio = scaleRatio || this.scaleRatio || 1; //console.log('setSize', width * scaleRatio, height * scaleRatio) let maxTexWidth = 4096; let w = width * scaleRatio; let h = height * scaleRatio; if(w > maxTexWidth || h > maxTexWidth){ //超出会崩溃 if(w>h){ scaleRatio = maxTexWidth / width; }else { scaleRatio = maxTexWidth / height; } } w = width * scaleRatio; h = height * scaleRatio; this.renderTarget1.setSize( w, h ); this.renderTarget2.setSize( w, h ); for ( var i = 0; i < this.passes.length; i ++ ) { this.passes[ i ].setSize( w, h ); } } } ); /** * @author alteredq / http://alteredqualia.com/ */ class RenderPass extends Pass { constructor( overrideMaterial, clearColor, clearAlpha ) { super(); /* this.scene = scene; this.camera = camera; */ this.overrideMaterial = overrideMaterial; this.clearColor = clearColor; this.clearAlpha = ( clearAlpha !== undefined ) ? clearAlpha : 0; this.clear = true; this.clearDepth = false; this.needsSwap = false; this._oldClearColor = new Color(); } render(scene, camera, viewports,renderer, writeBuffer, readBuffer /*, deltaTime, maskActive */ ) { const oldAutoClear = renderer.autoClear; renderer.autoClear = false; let oldClearAlpha, oldOverrideMaterial; if ( this.overrideMaterial !== undefined ) { oldOverrideMaterial = this.scene.overrideMaterial; scene.overrideMaterial = this.overrideMaterial; } if ( this.clearColor ) { renderer.getClearColor( this._oldClearColor ); oldClearAlpha = renderer.getClearAlpha(); renderer.setClearColor( this.clearColor, this.clearAlpha ); } if ( this.clearDepth ) { renderer.clearDepth(); } let oldTarget = renderer.getRenderTarget(); if(!this.renderToScreen)renderer.setRenderTarget( readBuffer ); // TODO: Avoid using autoClear properties, see https://github.com/mrdoob/three.js/pull/15571#issuecomment-465669600 if ( this.clear ) renderer.clear( renderer.autoClearColor, renderer.autoClearDepth, renderer.autoClearStencil ); renderer.render( scene, camera ); if(!this.renderToScreen)renderer.setRenderTarget( oldTarget ); if ( this.clearColor ) { renderer.setClearColor( this._oldClearColor, oldClearAlpha ); } if ( this.overrideMaterial !== undefined ) { scene.overrideMaterial = oldOverrideMaterial; } renderer.setRenderTarget(oldTarget); renderer.autoClear = oldAutoClear; } } /** * NVIDIA FXAA by Timothy Lottes * https://developer.download.nvidia.com/assets/gamedev/files/sdk/11/FXAA_WhitePaper.pdf * - WebGL port by @supereggbert * http://www.glge.org/demos/fxaa/ * Further improved by Daniel Sturk */ const FXAAShader = { uniforms: { 'tDiffuse': { value: null }, 'resolution': { value: new Vector2( 1 / 1024, 1 / 512 ) } }, vertexShader: /* glsl */` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 ); }`, fragmentShader: ` precision highp float; uniform sampler2D tDiffuse; uniform vec2 resolution; varying vec2 vUv; // FXAA 3.11 implementation by NVIDIA, ported to WebGL by Agost Biro (biro@archilogic.com) //---------------------------------------------------------------------------------- // File: es3-kepler\FXAA\assets\shaders/FXAA_DefaultES.frag // SDK Version: v3.00 // Email: gameworks@nvidia.com // Site: http://developer.nvidia.com/ // // Copyright (c) 2014-2015, NVIDIA CORPORATION. All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions // are met: // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above copyright // notice, this list of conditions and the following disclaimer in the // documentation and/or other materials provided with the distribution. // * Neither the name of NVIDIA CORPORATION nor the names of its // contributors may be used to endorse or promote products derived // from this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY // OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. // //---------------------------------------------------------------------------------- #ifndef FXAA_DISCARD // // Only valid for PC OpenGL currently. // Probably will not work when FXAA_GREEN_AS_LUMA = 1. // // 1 = Use discard on pixels which don't need AA. // For APIs which enable concurrent TEX+ROP from same surface. // 0 = Return unchanged color on pixels which don't need AA. // #define FXAA_DISCARD 0 #endif /*--------------------------------------------------------------------------*/ //第三个参数加入后只能在片元着色器中调用,且只对采样器为mipmap类型纹理时有效。不明白作用??? 但警告说范围在16之内 #define FxaaTexTop(t, p) texture2D(t, p, -15.0); //-100.0); #define FxaaTexOff(t, p, o, r) texture2D(t, p + (o * r), -15.0); //-100.0) /*--------------------------------------------------------------------------*/ #define NUM_SAMPLES 5 // assumes colors have premultipliedAlpha, so that the calculated color contrast is scaled by alpha float contrast( vec4 a, vec4 b ) { vec4 diff = abs( a - b ); return max( max( max( diff.r, diff.g ), diff.b ), diff.a ); } /*============================================================================ FXAA3 QUALITY - PC ============================================================================*/ /*--------------------------------------------------------------------------*/ vec4 FxaaPixelShader( vec2 posM, sampler2D tex, vec2 fxaaQualityRcpFrame, float fxaaQualityEdgeThreshold, float fxaaQualityinvEdgeThreshold ) { vec4 rgbaM = FxaaTexTop(tex, posM); vec4 rgbaS = FxaaTexOff(tex, posM, vec2( 0.0, 1.0), fxaaQualityRcpFrame.xy); vec4 rgbaE = FxaaTexOff(tex, posM, vec2( 1.0, 0.0), fxaaQualityRcpFrame.xy); vec4 rgbaN = FxaaTexOff(tex, posM, vec2( 0.0,-1.0), fxaaQualityRcpFrame.xy); vec4 rgbaW = FxaaTexOff(tex, posM, vec2(-1.0, 0.0), fxaaQualityRcpFrame.xy); // . S . // W M E // . N . bool earlyExit = max( max( max( contrast( rgbaM, rgbaN ), contrast( rgbaM, rgbaS ) ), contrast( rgbaM, rgbaE ) ), contrast( rgbaM, rgbaW ) ) < fxaaQualityEdgeThreshold; // . 0 . // 0 0 0 // . 0 . #if (FXAA_DISCARD == 1) if(earlyExit) FxaaDiscard; #else if(earlyExit) return rgbaM; #endif float contrastN = contrast( rgbaM, rgbaN ); float contrastS = contrast( rgbaM, rgbaS ); float contrastE = contrast( rgbaM, rgbaE ); float contrastW = contrast( rgbaM, rgbaW ); float relativeVContrast = ( contrastN + contrastS ) - ( contrastE + contrastW ); relativeVContrast *= fxaaQualityinvEdgeThreshold; bool horzSpan = relativeVContrast > 0.; // . 1 . // 0 0 0 // . 1 . // 45 deg edge detection and corners of objects, aka V/H contrast is too similar if( abs( relativeVContrast ) < .3 ) { // locate the edge vec2 dirToEdge; dirToEdge.x = contrastE > contrastW ? 1. : -1.; dirToEdge.y = contrastS > contrastN ? 1. : -1.; // . 2 . . 1 . // 1 0 2 ~= 0 0 1 // . 1 . . 0 . // tap 2 pixels and see which ones are "outside" the edge, to // determine if the edge is vertical or horizontal vec4 rgbaAlongH = FxaaTexOff(tex, posM, vec2( dirToEdge.x, -dirToEdge.y ), fxaaQualityRcpFrame.xy); float matchAlongH = contrast( rgbaM, rgbaAlongH ); // . 1 . // 0 0 1 // . 0 H vec4 rgbaAlongV = FxaaTexOff(tex, posM, vec2( -dirToEdge.x, dirToEdge.y ), fxaaQualityRcpFrame.xy); float matchAlongV = contrast( rgbaM, rgbaAlongV ); // V 1 . // 0 0 1 // . 0 . relativeVContrast = matchAlongV - matchAlongH; relativeVContrast *= fxaaQualityinvEdgeThreshold; if( abs( relativeVContrast ) < .3 ) { // 45 deg edge // 1 1 . // 0 0 1 // . 0 1 // do a simple blur return mix( rgbaM, (rgbaN + rgbaS + rgbaE + rgbaW) * .25, .4 ); } horzSpan = relativeVContrast > 0.; } if(!horzSpan) rgbaN = rgbaW; if(!horzSpan) rgbaS = rgbaE; // . 0 . 1 // 1 0 1 -> 0 // . 0 . 1 bool pairN = contrast( rgbaM, rgbaN ) > contrast( rgbaM, rgbaS ); if(!pairN) rgbaN = rgbaS; vec2 offNP; offNP.x = (!horzSpan) ? 0.0 : fxaaQualityRcpFrame.x; offNP.y = ( horzSpan) ? 0.0 : fxaaQualityRcpFrame.y; bool doneN = false; bool doneP = false; float nDist = 0.; float pDist = 0.; vec2 posN = posM; vec2 posP = posM; int iterationsUsed = 0; int iterationsUsedN = 0; int iterationsUsedP = 0; for( int i = 0; i < NUM_SAMPLES; i++ ) { iterationsUsed = i; float increment = float(i + 1); if(!doneN) { nDist += increment; posN = posM + offNP * nDist; vec4 rgbaEndN = FxaaTexTop(tex, posN.xy); doneN = contrast( rgbaEndN, rgbaM ) > contrast( rgbaEndN, rgbaN ); iterationsUsedN = i; } if(!doneP) { pDist += increment; posP = posM - offNP * pDist; vec4 rgbaEndP = FxaaTexTop(tex, posP.xy); doneP = contrast( rgbaEndP, rgbaM ) > contrast( rgbaEndP, rgbaN ); iterationsUsedP = i; } if(doneN || doneP) break; } if ( !doneP && !doneN ) return rgbaM; // failed to find end of edge float dist = min( doneN ? float( iterationsUsedN ) / float( NUM_SAMPLES - 1 ) : 1., doneP ? float( iterationsUsedP ) / float( NUM_SAMPLES - 1 ) : 1. ); // hacky way of reduces blurriness of mostly diagonal edges // but reduces AA quality dist = pow(dist, .5); dist = 1. - dist; return mix( rgbaM, rgbaN, dist * .5 ); } void main() { const float edgeDetectionQuality = 0.1 ;//.05 ; //越高,越保留细节;越低,越平滑 但模糊 const float invEdgeDetectionQuality = 1. / edgeDetectionQuality; gl_FragColor = FxaaPixelShader( vUv, tDiffuse, resolution, edgeDetectionQuality, // [0,1] contrast needed, otherwise early discard invEdgeDetectionQuality ); //单独渲染测量线有黑边,因为透明区域的clearColor是(0,0,0),模糊化的时候采用了这些黑色因子来混色。因为测量线颜色可能多种,也不能直接修改clearColor为其颜色,且还有label。 //故而无法单独渲染测量线,而需要将底图一起渲染。 } ` }; /** * @author spidersharma / http://eduperiment.com/ */ let OutlinePass = function ( selectedObjects ) { /* scene = scene; camera = camera; */ this.selectedObjects = selectedObjects !== undefined ? selectedObjects : []; this.visibleEdgeColor = new Color( 1, 1, 1 ); this.hiddenEdgeColor = new Color( 0.1, 0.04, 0.02 ); this.edgeGlow = 0.0; this.usePatternTexture = false; //this.edgeThickness = 1.0; this.edgeStrength = 50; this.downSampleRatio = 1;//2; // 抗锯齿 值越低renderTarget size越大,抗锯齿越强,线条可越细(或许可以把模糊化去掉?) this.pulsePeriod = 0; this.showHiddenPart = false; //是否判断被遮住的部分,以设置hiddenEdgeColor。一般不需要 Pass.call( this ); this.resolution = new Vector2( 256, 256 ); var pars = { minFilter: LinearFilter, magFilter: LinearFilter, format: RGBAFormat }; var resx = Math.round( this.resolution.x / this.downSampleRatio ); var resy = Math.round( this.resolution.y / this.downSampleRatio ); this.renderTargetMaskBuffer = new WebGLRenderTarget( this.resolution.x, this.resolution.y, pars ); this.renderTargetMaskBuffer.texture.name = "OutlinePass.mask"; this.renderTargetMaskBuffer.texture.generateMipmaps = false; this.depthMaterial = new MeshDepthMaterial(); this.depthMaterial.side = DoubleSide; this.depthMaterial.depthPacking = RGBADepthPacking; this.depthMaterial.blending = NoBlending; this.prepareMaskMaterial = this.getPrepareMaskMaterial(); this.prepareMaskMaterial.side = DoubleSide; this.renderTargetDepthBuffer = new WebGLRenderTarget( this.resolution.x, this.resolution.y, pars ); this.renderTargetDepthBuffer.texture.name = "OutlinePass.depth"; this.renderTargetDepthBuffer.texture.generateMipmaps = false; this.edgeDetectionMaterial = this.getEdgeDetectionMaterial(this.edgeStrength); this.renderTargetEdgeBuffer1 = new WebGLRenderTarget( resx, resy, pars ); this.renderTargetEdgeBuffer1.texture.name = "OutlinePass.edge1"; this.renderTargetEdgeBuffer1.texture.generateMipmaps = false; // Overlay material this.overlayMaterial = this.getOverlayMaterial(); // copy material this.copyUniforms = UniformsUtils.clone( CopyShader.uniforms ); this.copyUniforms[ "opacity" ].value = 1.0; this.materialCopy = new ShaderMaterial( { uniforms: this.copyUniforms, vertexShader: CopyShader.vertexShader, fragmentShader: CopyShader.fragmentShader, blending: NoBlending, depthTest: false, depthWrite: false, transparent: true } ); this.enabled = true; this.needsSwap = false; this.oldClearColor = new Color(); this.oldClearAlpha = 1; this.camera = new OrthographicCamera( - 1, 1, 1, - 1, 0, 1 ); this.scene = new Scene(); this.quad = new Mesh( new PlaneBufferGeometry( 2, 2 ), null ); this.quad.frustumCulled = false; // Avoid getting clipped this.scene.add( this.quad ); this.textureMatrix = new Matrix4(); viewer.addEventListener('viewerResize',(e)=>{ let size = viewer.renderer.getSize(new Vector2); this.renderTargetEdgeBuffer1.setSize(size.x, size.y ); this.renderTargetMaskBuffer.setSize(size.x, size.y); this.resolution.set(size.x, size.y); }); }; OutlinePass.prototype = Object.assign( Object.create( Pass.prototype ), { constructor: OutlinePass, dispose: function () { this.renderTargetMaskBuffer.dispose(); this.renderTargetEdgeBuffer1.dispose(); this.renderTargetDepthBuffer.dispose(); }, replaceDepthToViewZ( camera ) { var type = camera.isPerspectiveCamera ? 'perspective' : 'orthographic'; if(type == this.lastCameraType )return this.lastCameraType = type; this.prepareMaskMaterial.fragmentShader = this.prepareMaskMaterial.fragmentShader.replace( /DEPTH_TO_VIEW_Z/g, type + 'DepthToViewZ' ); this.prepareMaskMaterial.needsUpdate = true; }, setSize: function ( width, height ) { }, changeVisibilityOfSelectedObjects: function ( bVisible ) { function gatherSelectedMeshesCallBack( object ) { /* if ( object.isMesh ) { */ if ( object.isPointcloud || object.isMesh || object.isLine || object.isSprite ) { /* if ( bVisible ) { object.visible = object.userData.oldVisible; delete object.userData.oldVisible; } else { object.userData.oldVisible = object.visible; object.visible = bVisible; } */ Potree.Utils.updateVisible(object, 'overlinePass', bVisible); } } for ( var i = 0; i < this.selectedObjects.length; i ++ ) { var selectedObject = this.selectedObjects[ i ]; selectedObject.traverse( gatherSelectedMeshesCallBack ); } }, changeVisibilityOfNonSelectedObjects: function ( bVisible , scenes) { var selectedMeshes = []; function gatherSelectedMeshesCallBack( object ) { //if ( object.isMesh ) selectedMeshes.push( object ); if ( object.isPointcloud || object.isMesh || object.isLine || object.isSprite ) { selectedMeshes.push( object ); } } for ( var i = 0; i < this.selectedObjects.length; i ++ ) { var selectedObject = this.selectedObjects[ i ]; selectedObject.traverse( gatherSelectedMeshesCallBack ); } function VisibilityChangeCallBack( object ) { if ( object.isPointcloud || object.isMesh || object.isLine || object.isSprite ) { var bFound = false; for ( var i = 0; i < selectedMeshes.length; i ++ ) { var selectedObjectId = selectedMeshes[ i ].id; if ( selectedObjectId === object.id ) { bFound = true; break; } } if ( ! bFound ) { var visibility = object.visible; Potree.Utils.updateVisible(object, 'overlinePass', bVisible); //add //但保不齐在设置为false后,渲染时又true了,所以在其他地方update时设置visible 得用updateVisible if(!bVisible){ object.visible = false; }else { object.visible = !!object.bVisible; } object.bVisible = visibility; //这两种updateVisible 和 visible 设置都不能去掉, 第一块是为了防止有的visible不是通过updateVisible设置的; 第二块是为了防止渲染时updateVisible又修改了visible为true, 另外渲染时 return {stopContinue:true} //for pointcloud } } } scenes.forEach(scene=>scene.traverse( VisibilityChangeCallBack )); }, updateTextureMatrix: function (camera) { this.textureMatrix.set( 0.5, 0.0, 0.0, 0.5, 0.0, 0.5, 0.0, 0.5, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0, 1.0 ); this.textureMatrix.multiply( camera.projectionMatrix ); this.textureMatrix.multiply( camera.matrixWorldInverse ); }, render: function (scenes, camera, viewports, renderer, writeBuffer, readBuffer, maskActive, renderFun ) { if(!(scenes instanceof Array))scenes = [scenes]; if ( this.selectedObjects.length > 0 && this.edgeStrength > 0) { let render2 = (target, dontRenderRtEDL=true)=>{ if(renderFun){ renderFun({target , dontRenderRtEDL }); }else { renderer.setRenderTarget(target); renderer.clear(); scenes.forEach(scene=>renderer.render( scene, camera)); } }; this.oldClearColor.copy( renderer.getClearColor(new Color) ); this.oldClearAlpha = renderer.getClearAlpha(); let oldAutoClear = renderer.autoClear; let oldTarget = renderer.getRenderTarget(); renderer.autoClear = false; if ( maskActive ) renderer.context.disable( renderer.context.STENCIL_TEST ); renderer.setClearColor( 0xffffff, 1 ); let oldBG = viewer.background, oldBgOpacity = viewer.backgroundOpacity; viewer.background = new Color(1,1,1); viewer.backgroundOpacity = 1; if(this.showHiddenPart){ //几乎不会使用 viewports.forEach(e=>{ e.oldBeforeRender = e.beforeRender; e.beforeRender = ()=>{ e.oldBeforeRender && e.oldBeforeRender(); this.replaceDepthToViewZ( e.camera ); }; }); // Make selected objects invisible this.changeVisibilityOfSelectedObjects( false ); scenes.forEach(scene=>{ scene.currentBackground = scene.background; scene.background = null; // 1. Draw Non Selected objects in the depth buffer scene.overrideMaterial = this.depthMaterial; }); render2(this.renderTargetDepthBuffer); // Make selected objects visible this.changeVisibilityOfSelectedObjects( true ); viewports.forEach(e=>{ e.beforeRender = ()=>{ e.oldBeforeRender && e.oldBeforeRender(); // Update Texture Matrix for Depth compare this.updateTextureMatrix(e.camera); this.prepareMaskMaterial.uniforms[ "cameraNearFar" ].value = new Vector2( e.camera.near, e.camera.far ); }; }); }else { //不渲染背景 scenes.forEach(scene=>{ scene.currentBackground = scene.background; scene.background = null; }); } let colorType, colors = new Map() , opas = new Map(); {//绘制选中部分 // Make non selected objects invisible, and draw only the selected objects, by comparing the depth buffer of non selected objects this.changeVisibilityOfNonSelectedObjects( false , scenes); scenes.forEach(scene=>{ scene.overrideMaterial = this.prepareMaskMaterial; }); if(this.showHiddenPart){ this.prepareMaskMaterial.uniforms[ "depthTexture" ].value = this.renderTargetDepthBuffer.texture; this.prepareMaskMaterial.uniforms[ "textureMatrix" ].value = this.textureMatrix; } //因为点云不受prepareMaskMaterial作用,所以手动将他们变为黑色不透明 viewer.scene.pointclouds.forEach(e=>{ //先将点云透明度变为1,因为点云透明度莫名其妙会影响其r值// colorType = e.material.activeAttributeName; e.material.activeAttributeName = 'color'; colors.set(e, e.material.color); e.material.color = '#000000'; opas.set(e, e.material.opacity); e.material.opacity = 1; }); } render2(this.renderTargetMaskBuffer); {//恢复 viewer.scene.pointclouds.forEach(e=>{ e.material.activeAttributeName = colorType; e.material.color = colors.get(e); e.material.opacity = opas.get(e); }); if(this.showHiddenPart){ viewports.forEach((e)=>{e.beforeRender = e.oldBeforeRender;}); } scenes.forEach(scene=>{ scene.overrideMaterial = null; scene.background = scene.currentBackground; }); viewer.background = oldBG; viewer.backgroundOpacity = oldBgOpacity; this.changeVisibilityOfNonSelectedObjects( true , scenes); } //检测边缘并绘制 // 3. Apply Edge Detection Pass this.quad.material = this.edgeDetectionMaterial; this.edgeDetectionMaterial.uniforms[ "maskTexture" ].value = this.renderTargetMaskBuffer.texture;//this.renderTargetMaskDownSampleBuffer.texture; this.edgeDetectionMaterial.uniforms[ "texSize" ].value = new Vector2(this.resolution.x, this.resolution.y );//new THREE.Vector2( this.renderTargetMaskDownSampleBuffer.width, this.renderTargetMaskDownSampleBuffer.height ); //this.edgeDetectionMaterial.uniforms[ "texSize" ].value = new THREE.Vector2(this.renderTargetMaskBuffer.width, this.renderTargetMaskBuffer.height)//new THREE.Vector2( this.renderTargetMaskDownSampleBuffer.width, this.renderTargetMaskDownSampleBuffer.height ); this.edgeDetectionMaterial.uniforms[ "edgeStrength" ].value = this.edgeStrength; this.edgeDetectionMaterial.uniforms[ "visibleEdgeColor" ].value = this.visibleEdgeColor;//this.tempPulseColor1; this.edgeDetectionMaterial.uniforms[ "hiddenEdgeColor" ].value = this.hiddenEdgeColor; //this.tempPulseColor2; let buffer; if ( this.renderToScreen ) { this.quad.material.transparent = true; buffer = null; renderer.setClearColor( this.oldClearColor, this.oldClearAlpha ); render2(null,false); //普通绘制原场景 //绘制到全屏 let renderSize = renderer.getSize(new Vector2()); //是client大小 renderer.setViewport(0, 0, renderSize.x, renderSize.y); //规定视口,影响图形变换(画布的使用范围) renderer.setScissorTest( false ); }else { buffer = readBuffer; renderer.setClearColor( 0x000000, 0 ); renderer.clear(); } renderer.setRenderTarget( buffer ); renderer.render( this.scene, this.camera ); //将边缘覆盖上去 renderer.setRenderTarget(oldTarget); renderer.setClearColor( this.oldClearColor, this.oldClearAlpha ); renderer.autoClear = oldAutoClear; return true } }, getPrepareMaskMaterial: function () { if(this.showHiddenPart){ return new ShaderMaterial( { uniforms: { "depthTexture": { value: null }, "cameraNearFar": { value: new Vector2( 0.5, 0.5 ) }, "textureMatrix": { value: new Matrix4() } }, vertexShader: [ 'varying vec4 projTexCoord;', 'varying vec4 vPosition;', 'uniform mat4 textureMatrix;', 'void main() {', ' vPosition = modelViewMatrix * vec4( position, 1.0 );', ' vec4 worldPosition = modelMatrix * vec4( position, 1.0 );', ' projTexCoord = textureMatrix * worldPosition;', ' gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', '}' ].join( '\n' ), fragmentShader: [ '#include ', 'varying vec4 vPosition;', 'varying vec4 projTexCoord;', 'uniform sampler2D depthTexture;', 'uniform vec2 cameraNearFar;', 'void main() {', ' float depth = unpackRGBAToDepth(texture2DProj( depthTexture, projTexCoord ));', ' float viewZ = - DEPTH_TO_VIEW_Z( depth, cameraNearFar.x, cameraNearFar.y );', ' float depthTest = (-vPosition.z > viewZ) ? 1.0 : 0.0;', //决定是否为hiddenPart ' gl_FragColor = vec4(0.0, depthTest, 1.0, 1.0);', '}' ].join( '\n' ) } );//没有绘制部分的颜色是clearColor,255 }else { return new MeshBasicMaterial({color:'#000000'}) //不检测深度,不判断遮挡 } }, getEdgeDetectionMaterial: function (edgeStrength) { return new ShaderMaterial( { uniforms: { "edgeStrength": { value: edgeStrength }, "maskTexture": { value: null }, "texSize": { value: new Vector2( 10, 10 ) }, "visibleEdgeColor": { value: new Vector3( 1.0, 1.0, 1.0 ) }, "hiddenEdgeColor": { value: new Vector3( 1.0, 1.0, 1.0 ) }, }, vertexShader: "varying vec2 vUv;\n\ void main() {\n\ vUv = uv;\n\ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\ }", /* fragmentShader: "varying vec2 vUv;\ uniform sampler2D maskTexture;\ uniform vec2 texSize;\ uniform vec3 visibleEdgeColor;\ uniform vec3 hiddenEdgeColor;\ \ void main() {\n\ vec2 invSize = 1.0 / texSize;\ vec4 uvOffset = vec4(1.0, 0.0, 0.0, 1.0) * vec4(invSize, invSize);\ vec4 c1 = texture2D( maskTexture, vUv + uvOffset.xy);\ vec4 c2 = texture2D( maskTexture, vUv - uvOffset.xy);\ vec4 c3 = texture2D( maskTexture, vUv + uvOffset.yw);\ vec4 c4 = texture2D( maskTexture, vUv - uvOffset.yw);\ float diff1 = (c1.r - c2.r)*0.5;\ float diff2 = (c3.r - c4.r)*0.5;\ float d = length( vec2(diff1, diff2) );\ float a1 = min(c1.g, c2.g);\ float a2 = min(c3.g, c4.g);\ float visibilityFactor = min(a1, a2);\ vec3 edgeColor = 1.0 - visibilityFactor > 0.001 ? visibleEdgeColor : hiddenEdgeColor;\ gl_FragColor = vec4(edgeColor, 1.0) * vec4(d);\ }" */ fragmentShader: `varying vec2 vUv; uniform sampler2D maskTexture; uniform float edgeStrength; uniform vec2 texSize; uniform vec3 visibleEdgeColor; uniform vec3 hiddenEdgeColor; void main() { const float thickness = 1.0; vec2 invSize = thickness / texSize; vec4 uvOffset = vec4(1.0, 0.0, 0.0, 1.0) * vec4(invSize, invSize); vec4 c1 = texture2D( maskTexture, vUv + uvOffset.xy); vec4 c2 = texture2D( maskTexture, vUv - uvOffset.xy); vec4 c3 = texture2D( maskTexture, vUv + uvOffset.yw); vec4 c4 = texture2D( maskTexture, vUv - uvOffset.yw); float diff1 = (c1.r - c2.r)*0.5; //检测边缘, float diff2 = (c3.r - c4.r)*0.5; float d = length( vec2(diff1, diff2) ) * edgeStrength; float a1 = min(c1.g, c2.g); float a2 = min(c3.g, c4.g); float visibilityFactor = min(a1, a2); //检测深度值,是否被遮挡 vec3 edgeColor = 1.0 - visibilityFactor > 0.001 ? visibleEdgeColor : hiddenEdgeColor; //gl_FragColor = vec4(0.0,1.0,0.0,1.0); gl_FragColor = vec4(edgeColor, 1.0) * vec4(d); }` } ); //为什么vec4(0.0,1.0,1.0,1.0); 显示出来的是rgb(109,255,255) ? 几乎只有绿色通道会影响红色通道 }, getSeperableBlurMaterial: function ( maxRadius ) { return new ShaderMaterial( { defines: { "MAX_RADIUS": maxRadius, }, uniforms: { "colorTexture": { value: null }, "texSize": { value: new Vector2( 0.5, 0.5 ) }, "direction": { value: new Vector2( 0.5, 0.5 ) }, "kernelRadius": { value: 1.0 } }, vertexShader: "varying vec2 vUv;\n\ void main() {\n\ vUv = uv;\n\ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\ }", fragmentShader: "#include \ varying vec2 vUv;\ uniform sampler2D colorTexture;\ uniform vec2 texSize;\ uniform vec2 direction;\ uniform float kernelRadius;\ \ float gaussianPdf(in float x, in float sigma) {\ return 0.39894 * exp( -0.5 * x * x/( sigma * sigma))/sigma;\ }\ void main() {\ vec2 invSize = 1.0 / texSize;\ float weightSum = gaussianPdf(0.0, kernelRadius);\ vec3 diffuseSum = texture2D( colorTexture, vUv).rgb * weightSum;\ vec2 delta = direction * invSize * kernelRadius/float(MAX_RADIUS);\ vec2 uvOffset = delta;\ for( int i = 1; i <= MAX_RADIUS; i ++ ) {\ float w = gaussianPdf(uvOffset.x, kernelRadius);\ vec3 sample1 = texture2D( colorTexture, vUv + uvOffset).rgb;\ vec3 sample2 = texture2D( colorTexture, vUv - uvOffset).rgb;\ diffuseSum += ((sample1 + sample2) * w);\ weightSum += (2.0 * w);\ uvOffset += delta;\ }\ gl_FragColor = vec4(diffuseSum/weightSum, 1.0);\ }" } ); }, getOverlayMaterial: function () {//对边缘线进一步处理,已被废弃 return new ShaderMaterial( { uniforms: { "maskTexture": { value: null }, "edgeTexture1": { value: null }, "edgeTexture2": { value: null }, "patternTexture": { value: null }, "edgeStrength": { value: 1.0 }, "edgeGlow": { value: 1.0 }, "usePatternTexture": { value: 0.0 } }, vertexShader: "varying vec2 vUv;\n\ void main() {\n\ vUv = uv;\n\ gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );\n\ }", /* fragmentShader: "varying vec2 vUv;\ uniform sampler2D maskTexture;\ uniform sampler2D edgeTexture1;\ uniform sampler2D edgeTexture2;\ uniform sampler2D patternTexture;\ uniform float edgeStrength;\ uniform float edgeGlow;\ uniform bool usePatternTexture;\ \ void main() {\ vec4 edgeValue1 = texture2D(edgeTexture1, vUv);\ vec4 edgeValue2 = texture2D(edgeTexture2, vUv);\ vec4 maskColor = texture2D(maskTexture, vUv);\ vec4 patternColor = texture2D(patternTexture, 6.0 * vUv);\ float visibilityFactor = 1.0 - maskColor.g > 0.0 ? 1.0 : 0.5;\ vec4 edgeValue = edgeValue1 + edgeValue2 * edgeGlow;\ vec4 finalColor = edgeStrength * maskColor.r * edgeValue;\ // 删除 * maskColor.r 也就是去掉遮罩,使模型部分也有outline if(usePatternTexture)\ finalColor += + visibilityFactor * (1.0 - maskColor.r) * (1.0 - patternColor.r);\ gl_FragColor = finalColor;\ }", */ fragmentShader: `varying vec2 vUv; uniform sampler2D edgeTexture1; uniform float edgeStrength; void main() { gl_FragColor = edgeStrength * texture2D(edgeTexture1, vUv); }`, blending: AdditiveBlending, depthTest: false, depthWrite: false, transparent: true } ); } } ); OutlinePass.BlurDirectionX = new Vector2( 1.0, 0.0 ); OutlinePass.BlurDirectionY = new Vector2( 0.0, 1.0 ); /* 渲染步骤:(没有showHiddenPart时) 仅绘制选中部分,材质为普通的单色材质,颜色是黑色不透明。未绘制部分的clearColor是白色不透明。 (详见prepareMaskMaterial,输出的r要为0) 然后在上面绘制的图中,根据r的差值,得到边缘线。 正常绘制场景之后,将上面的边缘线覆盖于其上。 (如果showHiddenPart,最开始还要先隐藏选中部分,这是为了得到非选中部分的深度值,然后再绘制选中部分时去比较选中和非选中部分之间的深度差异。以判断是否被遮挡。) */ /** * @author mschuetz / http://mschuetz.at * * adapted from THREE.OrbitControls by * * @author qiao / https://github.com/qiao * @author mrdoob / http://mrdoob.com * @author alteredq / http://alteredqualia.com/ * @author WestLangley / http://github.com/WestLangley * @author erich666 / http://erichaines.com * * * */ let minRadius = 2; class OrbitControls extends EventDispatcher{ constructor(viewer, viewport){ super(); this.viewer = viewer; this.renderer = viewer.renderer; this.scene = null; this.sceneControls = new Scene(); this.rotationSpeed = 3; //旋转速度 viewport = viewport || viewer.viewports[0]; this.setCurrentViewport({hoverViewport:viewport, force:true}); //this.currentViewport = viewport this.fadeFactor = 20; this.yawDelta = 0; this.pitchDelta = 0; this.panDelta = new Vector2(0, 0); this.radiusDelta = 0; this.doubleClockZoomEnabled = true; this.tweens = []; this.dollyStart = new Vector2; this.dollyEnd = new Vector2; this.keys = { FORWARD: ['W'.charCodeAt(0), 38], BACKWARD: ['S'.charCodeAt(0), 40], LEFT: ['A'.charCodeAt(0), 37], RIGHT: ['D'.charCodeAt(0), 39], UP: ['Q'.charCodeAt(0)], DOWN: ['E'.charCodeAt(0)], }; let drag = (e) => { if(!this.enabled)return let viewport = e.dragViewport; if(!viewport /* || viewport.camera.type == "OrthographicCamera" */)return //let camera = viewport.camera if (e.drag.object !== null) { return; } let mode; if(e.isTouch){ if(e.touches.length == 1){ mode = 'rotate'; }else { mode = 'scale-pan'; } }else { mode = e.buttons === Potree.defines.Buttons.LEFT ? 'rotate' : 'pan'; } if (e.drag.startHandled === undefined) { e.drag.startHandled = true; this.dispatchEvent({type: 'start'}); } let ndrag = e.drag.pointerDelta.clone();//.add(new THREE.Vector2(1,1)).multiplyScalar(0.5) ndrag.y *= -1; if (mode == 'rotate') { this.yawDelta += ndrag.x * this.rotationSpeed; this.pitchDelta += ndrag.y * this.rotationSpeed; } else if(mode == 'pan'){ this.panDelta.x += ndrag.x; this.panDelta.y += ndrag.y; }else if(mode == 'scale-pan'){ //add this.dollyEnd.subVectors(e.touches[0].pointer, e.touches[1].pointer); var scale = this.dollyEnd.length() / this.dollyStart.length(); this.dollyStart.copy(this.dollyEnd); this.radiusDelta = (1-scale) * this.currentViewport.view.radius; //------------------------ //平移 let pointer = new Vector2().addVectors(e.touches[0].pointer, e.touches[1].pointer).multiplyScalar(0.5);//两个指头的中心点 let delta = new Vector2().subVectors(pointer, this.lastScalePointer); delta.y *= -1; this.panDelta.add(delta); this.lastScalePointer = pointer.clone(); //console.log('scale ',scale, this.radiusDelta) } this.stopTweens(); }; let drop = e => { if(!this.enabled)return this.dispatchEvent({type: 'end'}); }; let scroll = (e) => { if(!this.enabled)return let resolvedRadius = this.currentViewport.view.radius + this.radiusDelta; if(resolvedRadius < 0.1 && e.delta>0)return; //防止缩放太小,导致很慢 this.radiusDelta += -e.delta * resolvedRadius * 0.1; this.stopTweens(); }; let dblclick = (e) => { if(!this.enabled)return if(this.doubleClockZoomEnabled){ this.zoomToLocation(e.mouse); } }; let previousTouch = null; let touchStart = e => { previousTouch = e; }; let touchEnd = e => { previousTouch = e; }; let touchMove = e => { if(!this.enabled)return if (e.touches.length === 2 && previousTouch.touches.length === 2){ let prev = previousTouch; let curr = e; let prevDX = prev.touches[0].pageX - prev.touches[1].pageX; let prevDY = prev.touches[0].pageY - prev.touches[1].pageY; let prevDist = Math.sqrt(prevDX * prevDX + prevDY * prevDY); let currDX = curr.touches[0].pageX - curr.touches[1].pageX; let currDY = curr.touches[0].pageY - curr.touches[1].pageY; let currDist = Math.sqrt(currDX * currDX + currDY * currDY); let delta = currDist / prevDist; let resolvedRadius = this.currentViewport.view.radius + this.radiusDelta; let newRadius = resolvedRadius / delta; this.radiusDelta = newRadius - resolvedRadius; this.stopTweens(); }else if(e.touches.length === 3 && previousTouch.touches.length === 3){ let prev = previousTouch; let curr = e; let prevMeanX = (prev.touches[0].pageX + prev.touches[1].pageX + prev.touches[2].pageX) / 3; let prevMeanY = (prev.touches[0].pageY + prev.touches[1].pageY + prev.touches[2].pageY) / 3; let currMeanX = (curr.touches[0].pageX + curr.touches[1].pageX + curr.touches[2].pageX) / 3; let currMeanY = (curr.touches[0].pageY + curr.touches[1].pageY + curr.touches[2].pageY) / 3; let delta = { x: (currMeanX - prevMeanX) / this.renderer.domElement.clientWidth, y: (currMeanY - prevMeanY) / this.renderer.domElement.clientHeight }; this.panDelta.x += delta.x; this.panDelta.y += delta.y; this.stopTweens(); } previousTouch = e; }; this.addEventListener('touchstart', touchStart); this.addEventListener('touchend', touchEnd); this.addEventListener('touchmove', touchMove); this.viewer.addEventListener('global_drag', drag); this.viewer.addEventListener('global_drop', drop); this.viewer.addEventListener('global_mousewheel', scroll); this.viewer.addEventListener('global_dblclick', dblclick); this.viewer.addEventListener('global_touchmove', (e)=>{ if(e.touches.length>1){//单指的就触发上一句 //console.log('global_touchmove' ) drag(e); } }); let prepareScale = (e)=>{//触屏的scale this.dollyStart.subVectors(e.touches[0].pointer, e.touches[1].pointer); this.lastScalePointer = new Vector2().addVectors(e.touches[0].pointer, e.touches[1].pointer).multiplyScalar(0.5);//两个指头的中心点 }; this.viewer.addEventListener('global_touchstart', (e)=>{ if(this.enabled && e.touches.length==2){//只监听开头两个指头 prepareScale(e); } }); /* this.viewer.addEventListener('global_touchend', (e)=>{ if(!this.enabled)return if(e.touches.length==1){//停止scale,开始rotate prepareRotate(e) //this.pointerDragStart = null //console.log('只剩一个', e.pointer.toArray()) } }) */ this.viewer.addEventListener('focusOnObject',(o)=>{ if(o.position && o.CamTarget){ let distance = o.position.distanceTo(o.CamTarget); if(distance < minRadius) minRadius = distance * 0.5; //融合页面当focus一个很小的物体时,需要将minRadius也调小 } }); } setScene (scene) { this.scene = scene; } setCurrentViewport(o={}){//add if(!this.enabled && !o.force )return if(o.hoverViewport && this.currentViewport != o.hoverViewport ){ this.currentViewport = o.hoverViewport; } } setEnable(enabled){ this.enabled = enabled; } stop(){ this.yawDelta = 0; this.pitchDelta = 0; this.radiusDelta = 0; this.panDelta.set(0, 0); } /* zoomToLocation(mouse){ if(!this.enabled)return let camera = this.scene.getActiveCamera(); let I = Utils.getMousePointCloudIntersection( null, mouse, this.viewer.inputHandler.pointer, camera, this.viewer, this.scene.pointclouds, {pickClipped: true}); if (I === null) { return; } let targetRadius = 0; { let minimumJumpDistance = 0.2; let domElement = this.renderer.domElement; let ray = Utils.mouseToRay(this.viewer.inputHandler.pointer , camera, domElement.clientWidth, domElement.clientHeight); let nodes = I.pointcloud.nodesOnRay(I.pointcloud.visibleNodes, ray); let lastNode = nodes[nodes.length - 1]; let radius = lastNode.getBoundingSphere(new THREE.Sphere()).radius; targetRadius = Math.min(this.currentViewport.view.radius, radius); targetRadius = Math.max(minimumJumpDistance, targetRadius); } let d = this.currentViewport.view.direction.multiplyScalar(-1); let cameraTargetPosition = new THREE.Vector3().addVectors(I.location, d.multiplyScalar(targetRadius)); // TODO Unused: let controlsTargetPosition = I.location; let animationDuration = 600; let easing = TWEEN.Easing.Quartic.Out; { // animate let value = {x: 0}; let tween = new TWEEN.Tween(value).to({x: 1}, animationDuration); tween.easing(easing); this.tweens.push(tween); let startPos = this.currentViewport.view.position.clone(); let targetPos = cameraTargetPosition.clone(); let startRadius = this.currentViewport.view.radius; let targetRadius = cameraTargetPosition.distanceTo(I.location); tween.onUpdate(() => { let t = value.x; this.currentViewport.view.position.x = (1 - t) * startPos.x + t * targetPos.x; this.currentViewport.view.position.y = (1 - t) * startPos.y + t * targetPos.y; this.currentViewport.view.position.z = (1 - t) * startPos.z + t * targetPos.z; this.currentViewport.view.radius = (1 - t) * startRadius + t * targetRadius; this.viewer.setMoveSpeed(this.currentViewport.view.radius); }); tween.onComplete(() => { this.tweens = this.tweens.filter(e => e !== tween); }); tween.start(); } } */ zoomToLocation(mouse){ let I = viewer.inputHandler.intersect; if(!I)return let object = I.object || I.pointcloud; I = I.location; if(!I || !object)return; let dis = this.currentViewport.view.position.distanceTo(I); let bound = object.boundingBox.clone().applyMatrix4(object.matrixWorld); let size = bound.getSize(new Vector3); let len = size.length(); let distance = MathUtils.clamp(dis, 0.1, Math.max(len * 0.1, 3) ); minRadius = distance; viewer.focusOnObject({ position:I }, 'point', null, {distance}); } stopTweens () { this.tweens.forEach(e => e.stop()); this.tweens = []; } update (delta) { if(!this.enabled)return let view = this.currentViewport.view;//this.currentViewport.view; let camera = this.currentViewport.camera; { // accelerate while input is given let ih = this.viewer.inputHandler; let moveForward = this.keys.FORWARD.some(e => ih.pressedKeys[e]); let moveBackward = this.keys.BACKWARD.some(e => ih.pressedKeys[e]); let moveLeft = this.keys.LEFT.some(e => ih.pressedKeys[e]); let moveRight = this.keys.RIGHT.some(e => ih.pressedKeys[e]); let moveUp = this.keys.UP.some(e => ih.pressedKeys[e]); let moveDown = this.keys.DOWN.some(e => ih.pressedKeys[e]); let moveSpeed = this.currentViewport.getMoveSpeed() / 20; let px = 0 , py = 0, pz = 0; if(moveForward){ py = 1 * moveSpeed; }else if(moveBackward){ py = -1 * moveSpeed; } if(moveLeft){ px = -1 * moveSpeed; }else if(moveRight){ px = 1 * moveSpeed; } if(moveUp){ pz = 1 * moveSpeed; }else if(moveDown){ pz = -1 * moveSpeed; } (px!=0 || py!=0 || pz!=0) && view.translate(px, py, pz, true); } { // apply rotation let progression = Math.min(1, this.fadeFactor * delta); let yaw = view.yaw; let pitch = view.pitch; let pivot = view.getPivot(); yaw -= progression * this.yawDelta; pitch -= progression * this.pitchDelta; view.yaw = yaw; view.pitch = pitch; let V = this.currentViewport.view.direction.multiplyScalar(-view.radius); let position = new Vector3().addVectors(pivot, V); view.position.copy(position); } if(camera.type != 'OrthographicCamera'){ // apply pan /* let progression = Math.min(1, this.fadeFactor * delta); let panDistance = progression * view.radius * 3; */ let panDistance = 2 * view.radius * Math.tan(MathUtils.degToRad(camera.fov / 2));//参照4dkk。 平移target(也就是平移镜头位置),但还是难以保证跟手(navvis也不一定跟手,但是很奇怪在居中时中心点居然是跟手的,可能计算方式不同) //计算了下确实是这么算的。 平移pivot。 let px = -this.panDelta.x * panDistance; let py = this.panDelta.y * panDistance; view.pan(px, py); } { // apply zoom let progression = 1;//Math.min(1, this.fadeFactor * delta); // let radius = view.radius + progression * this.radiusDelta * view.radius * 0.1; let radius = view.radius + progression * this.radiusDelta; let V = view.direction.multiplyScalar(-radius); let position = new Vector3().addVectors(view.getPivot(), V); if(this.constantlyForward) {// 到达中心点后还能继续向前移动,也就是能推进中心点 if(radius < minRadius){ radius = minRadius; } } view.radius = radius; view.position.copy(position); } { let speed = view.radius; this.viewer.setMoveSpeed && this.viewer.setMoveSpeed(speed); } { // decelerate over time /* let progression = Math.min(1, this.fadeFactor * delta); let attenuation = Math.max(0, 1 - this.fadeFactor * delta); this.yawDelta *= attenuation; this.pitchDelta *= attenuation; this.panDelta.multiplyScalar(attenuation); // this.radiusDelta *= attenuation; this.radiusDelta -= progression * this.radiusDelta; */ //取消衰减,直接stop this.stop(); } } }; // Adapted from three.js VRButton class VRButton { constructor(){ this.onStartListeners = []; this.onEndListeners = []; this.element = null; } onStart(callback){ this.onStartListeners.push(callback); } onEnd(callback){ this.onEndListeners.push(callback); } static async createButton( renderer, options ) { if ( options ) { console.error( 'THREE.VRButton: The "options" parameter has been removed. Please set the reference space type via renderer.xr.setReferenceSpaceType() instead.' ); } const button = new VRButton(); const element = document.createElement( 'button' ); button.element = element; function setEnter(){ button.element.innerHTML = `
    ENTER
    VR
    `; } function setExit(){ button.element.innerHTML = `
    EXIT
    VR
    `; } function showEnterVR( /*device*/ ) { let currentSession = null; function onSessionStarted( session ) { session.addEventListener( 'end', onSessionEnded ); for(let listener of button.onStartListeners){ listener(); } renderer.xr.setSession( session ); setExit(); currentSession = session; } function onSessionEnded( /*event*/ ) { currentSession.removeEventListener( 'end', onSessionEnded ); for(let listener of button.onEndListeners){ listener(); } setEnter(); currentSession = null; } // button.element.style.display = ''; button.element.style.cursor = 'pointer'; setEnter(); button.element.onmouseenter = function () { button.element.style.opacity = '1.0'; }; button.element.onmouseleave = function () { button.element.style.opacity = '0.7'; }; button.element.onclick = function () { if ( currentSession === null ) { // WebXR's requestReferenceSpace only works if the corresponding feature // was requested at session creation time. For simplicity, just ask for // the interesting ones as optional features, but be aware that the // requestReferenceSpace call will fail if it turns out to be unavailable. // ('local' is always available for immersive sessions and doesn't need to // be requested separately.) const sessionInit = { optionalFeatures: [ 'local-floor', 'bounded-floor', 'hand-tracking' ] }; navigator.xr.requestSession( 'immersive-vr', sessionInit ).then( onSessionStarted ); } else { currentSession.end(); } }; } function stylizeElement( element ) { element.style.position = 'absolute'; element.style.bottom = '20px'; element.style.padding = '12px 6px'; element.style.border = '1px solid #fff'; element.style.borderRadius = '4px'; element.style.background = 'rgba(0,0,0,0.1)'; element.style.color = '#fff'; element.style.font = 'normal 13px sans-serif'; element.style.textAlign = 'center'; element.style.opacity = '0.7'; element.style.outline = 'none'; element.style.zIndex = '999'; } if ( 'xr' in navigator ) { button.element.id = 'VRButton'; button.element.style.display = 'none'; stylizeElement( button.element ); let supported = await navigator.xr.isSessionSupported( 'immersive-vr' ); if(supported){ showEnterVR(); return button; }else { return null; } } else { if ( window.isSecureContext === false ) { console.log("WEBXR NEEDS HTTPS"); } else { console.log("WEBXR not available"); } return null; } } } !function(e, a) { "object" == typeof exports && "object" == typeof module ? module.exports = a() : "function" == typeof define && define.amd ? define([], a) : "object" == typeof exports ? exports.DxfParser = a() : e.DxfParser = a(); }("undefined" != typeof self ? self : undefined, function() { return function(e) { var a = {}; function t(r) { if (a[r]) return a[r].exports; var n = a[r] = { i: r, l: !1, exports: {} }; return e[r].call(n.exports, n, n.exports, t), n.l = !0, n.exports } return t.m = e, t.c = a, t.d = function(e, a, r) { t.o(e, a) || Object.defineProperty(e, a, { enumerable: !0, get: r }); } , t.r = function(e) { "undefined" != typeof Symbol && Symbol.toStringTag && Object.defineProperty(e, Symbol.toStringTag, { value: "Module" }), Object.defineProperty(e, "__esModule", { value: !0 }); } , t.t = function(e, a) { if (1 & a && (e = t(e)), 8 & a) return e; if (4 & a && "object" == typeof e && e && e.__esModule) return e; var r = Object.create(null); if (t.r(r), Object.defineProperty(r, "default", { enumerable: !0, value: e }), 2 & a && "string" != typeof e) for (var n in e) t.d(r, n, function(a) { return e[a] } .bind(null, n)); return r } , t.n = function(e) { var a = e && e.__esModule ? function() { return e.default } : function() { return e } ; return t.d(a, "a", a), a } , t.o = function(e, a) { return Object.prototype.hasOwnProperty.call(e, a) } , t.p = "", t(t.s = 1) }([function(e, a, t) { var r, n; !function(o, c) { "use strict"; void 0 === (n = "function" == typeof (r = function() { var e = function() {} , a = "undefined" , t = ["trace", "debug", "info", "warn", "error"]; function r(e, a) { var t = e[a]; if ("function" == typeof t.bind) return t.bind(e); try { return Function.prototype.bind.call(t, e) } catch (a) { return function() { return Function.prototype.apply.apply(t, [e, arguments]) } } } function n(a, r) { for (var n = 0; n < t.length; n++) { var o = t[n]; this[o] = n < a ? e : this.methodFactory(o, a, r); } this.log = this.debug; } function o(t, o, c) { return function(t) { "debug" === t && (t = "log"); return typeof console !== a && (void 0 !== console[t] ? r(console, t) : void 0 !== console.log ? r(console, "log") : e) }(t) || function(e, t, r) { return function() { typeof console !== a && (n.call(this, t, r), this[e].apply(this, arguments)); } } .apply(this, arguments) } function c(e, r, c) { var i, u = this, s = "loglevel"; function l() { var e; if (typeof window !== a) { try { e = window.localStorage[s]; } catch (e) {} if (typeof e === a) try { var t = window.document.cookie , r = t.indexOf(encodeURIComponent(s) + "="); -1 !== r && (e = /^([^;]+)/.exec(t.slice(r))[1]); } catch (e) {} return void 0 === u.levels[e] && (e = void 0), e } } e && (s += ":" + e), u.name = e, u.levels = { TRACE: 0, DEBUG: 1, INFO: 2, WARN: 3, ERROR: 4, SILENT: 5 }, u.methodFactory = c || o, u.getLevel = function() { return i } , u.setLevel = function(r, o) { if ("string" == typeof r && void 0 !== u.levels[r.toUpperCase()] && (r = u.levels[r.toUpperCase()]), !("number" == typeof r && r >= 0 && r <= u.levels.SILENT)) throw "log.setLevel() called with invalid level: " + r; if (i = r, !1 !== o && function(e) { var r = (t[e] || "silent").toUpperCase(); if (typeof window === a) return; try { return void (window.localStorage[s] = r) } catch (e) {} try { window.document.cookie = encodeURIComponent(s) + "=" + r + ";"; } catch (e) {} }(r), n.call(u, r, e), typeof console === a && r < u.levels.SILENT) return "No console available for logging" } , u.setDefaultLevel = function(e) { l() || u.setLevel(e, !1); } , u.enableAll = function(e) { u.setLevel(u.levels.TRACE, e); } , u.disableAll = function(e) { u.setLevel(u.levels.SILENT, e); } ; var v = l(); null == v && (v = null == r ? "WARN" : r), u.setLevel(v, !1); } var i = new c , u = {}; i.getLogger = function(e) { if ("string" != typeof e || "" === e) throw new TypeError("You must supply a name when creating a logger."); var a = u[e]; return a || (a = u[e] = new c(e,i.getLevel(),i.methodFactory)), a } ; var s = typeof window !== a ? window.log : void 0; return i.noConflict = function() { return typeof window !== a && window.log === i && (window.log = s), i } , i.getLoggers = function() { return u } , i } ) ? r.call(a, t, a, e) : r) || (e.exports = n); }(); } , function(e, a, t) { "use strict"; function r(e) { this._pointer = 0, this._data = e, this._eof = !1; } function n(e, a) { return e <= 9 ? a : e >= 10 && e <= 59 ? parseFloat(a) : e >= 60 && e <= 99 ? parseInt(a) : e >= 100 && e <= 109 ? a : e >= 110 && e <= 149 ? parseFloat(a) : e >= 160 && e <= 179 ? parseInt(a) : e >= 210 && e <= 239 ? parseFloat(a) : e >= 270 && e <= 289 ? parseInt(a) : e >= 290 && e <= 299 ? function(e) { if ("0" === e) return !1; if ("1" === e) return !0; throw TypeError("String '" + e + "' cannot be cast to Boolean type") }(a) : e >= 300 && e <= 369 ? a : e >= 370 && e <= 389 ? parseInt(a) : e >= 390 && e <= 399 ? a : e >= 400 && e <= 409 ? parseInt(a) : e >= 410 && e <= 419 ? a : e >= 420 && e <= 429 ? parseInt(a) : e >= 430 && e <= 439 ? a : e >= 440 && e <= 459 ? parseInt(a) : e >= 460 && e <= 469 ? parseFloat(a) : e >= 470 && e <= 481 ? a : 999 === e ? a : e >= 1e3 && e <= 1009 ? a : e >= 1010 && e <= 1059 ? parseFloat(a) : e >= 1060 && e <= 1071 ? parseInt(a) : (console.log("WARNING: Group code does not have a defined type: %j", { code: e, value: a }), a) } t.r(a), r.prototype.next = function() { var e; if (!this.hasNext()) throw this._eof ? new Error("Cannot call 'next' after EOF group has been read") : new Error("Unexpected end of input: EOF group not read before end of file. Ended on code " + this._data[this._pointer]); return e = { code: parseInt(this._data[this._pointer]) }, this._pointer++, e.value = n(e.code, this._data[this._pointer].trim()), this._pointer++, 0 === e.code && "EOF" === e.value && (this._eof = !0), this.lastReadGroup = e, e } , r.prototype.peek = function() { if (!this.hasNext()) throw this._eof ? new Error("Cannot call 'next' after EOF group has been read") : new Error("Unexpected end of input: EOF group not read before end of file. Ended on code " + this._data[this._pointer]); var e = { code: parseInt(this._data[this._pointer]) }; return e.value = n(e.code, this._data[this._pointer + 1].trim()), e } , r.prototype.rewind = function(e) { e = e || 1, this._pointer = this._pointer - 2 * e; } , r.prototype.hasNext = function() { return !this._eof && !(this._pointer > this._data.length - 2) } , r.prototype.isEOF = function() { return this._eof } ; var o = [0, 16711680, 16776960, 65280, 65535, 255, 16711935, 16777215, 8421504, 12632256, 16711680, 16744319, 13369344, 13395558, 10027008, 10046540, 8323072, 8339263, 4980736, 4990502, 16727808, 16752511, 13382400, 13401958, 10036736, 10051404, 8331008, 8343359, 4985600, 4992806, 16744192, 16760703, 13395456, 13408614, 10046464, 10056268, 8339200, 8347455, 4990464, 4995366, 16760576, 16768895, 13408512, 13415014, 10056192, 10061132, 8347392, 8351551, 4995328, 4997670, 16776960, 16777087, 13421568, 13421670, 10000384, 10000460, 8355584, 8355647, 5000192, 5000230, 12582656, 14679935, 10079232, 11717734, 7510016, 8755276, 6258432, 7307071, 3755008, 4344870, 8388352, 12582783, 6736896, 10079334, 5019648, 7510092, 4161280, 6258495, 2509824, 3755046, 4194048, 10485631, 3394560, 8375398, 2529280, 6264908, 2064128, 5209919, 1264640, 3099686, 65280, 8388479, 52224, 6736998, 38912, 5019724, 32512, 4161343, 19456, 2509862, 65343, 8388511, 52275, 6737023, 38950, 5019743, 32543, 4161359, 19475, 2509871, 65407, 8388543, 52326, 6737049, 38988, 5019762, 32575, 4161375, 19494, 2509881, 65471, 8388575, 52377, 6737074, 39026, 5019781, 32607, 4161391, 19513, 2509890, 65535, 8388607, 52428, 6737100, 39064, 5019800, 32639, 4161407, 19532, 2509900, 49151, 8380415, 39372, 6730444, 29336, 5014936, 24447, 4157311, 14668, 2507340, 32767, 8372223, 26316, 6724044, 19608, 5010072, 16255, 4153215, 9804, 2505036, 16383, 8364031, 13260, 6717388, 9880, 5005208, 8063, 4149119, 4940, 2502476, 255, 8355839, 204, 6710988, 152, 5000344, 127, 4145023, 76, 2500172, 4129023, 10452991, 3342540, 8349388, 2490520, 6245528, 2031743, 5193599, 1245260, 3089996, 8323327, 12550143, 6684876, 10053324, 4980888, 7490712, 4128895, 6242175, 2490444, 3745356, 12517631, 14647295, 10027212, 11691724, 7471256, 8735896, 6226047, 7290751, 3735628, 4335180, 16711935, 16744447, 13369548, 13395660, 9961624, 9981080, 8323199, 8339327, 4980812, 4990540, 16711871, 16744415, 13369497, 13395634, 9961586, 9981061, 8323167, 8339311, 4980793, 4990530, 16711807, 16744383, 13369446, 13395609, 9961548, 9981042, 8323135, 8339295, 4980774, 4990521, 16711743, 16744351, 13369395, 13395583, 9961510, 9981023, 8323103, 8339279, 4980755, 4990511, 3355443, 5987163, 8684676, 11382189, 14079702, 16777215]; function c(e) { var a = {}; e.rewind(); var t = e.next() , r = t.code; if (a.x = t.value, r += 10, (t = e.next()).code != r) throw new Error("Expected code for point value to be " + r + " but got " + t.code + "."); return a.y = t.value, r += 10, (t = e.next()).code != r ? (e.rewind(), a) : (a.z = t.value, a) } function i(e, a) { switch (a.code) { case 0: e.type = a.value; break; case 5: e.handle = a.value; /* if(e.handle == "2FDDB"){ console.log(1) } */ break; case 6: e.lineType = a.value; break; case 8: e.layer = a.value; break; case 48: e.lineTypeScale = a.value; break; case 60: e.visible = 0 === a.value; break; case 62: e.colorIndex = a.value, e.color = (t = Math.abs(a.value), o[t]); break; case 67: e.inPaperSpace = 0 !== a.value; break; case 100: break; case 330: e.ownerHandle = a.value; break; case 347: e.materialObjectHandle = a.value; break; case 370: e.lineweight = a.value; break; case 420: e.color = a.value; break; case 1e3: e.extendedData = e.extendedData || {}, e.extendedData.customStrings = e.extendedData.customStrings || [], e.extendedData.customStrings.push(a.value); break; case 1001: e.extendedData = e.extendedData || {}, e.extendedData.applicationName = a.value; break; default: return !1 } var t; return !0 } function u() {} function s(e, a) { var t, r = [], n = !1, o = !1; for (t = 0; t <= 4; t++) { for (var c = {}; "EOF" !== a && 0 !== a.code && !o; ) { switch (a.code) { case 10: case 11: case 12: case 13: if (n) { o = !0; continue } c.x = a.value, n = !0; break; case 20: case 21: case 22: case 23: c.y = a.value; break; case 30: case 31: case 32: case 33: c.z = a.value; break; default: return r } a = e.next(); } r.push(c), n = !1, o = !1; } return e.rewind(), r } function l() {} function v() {} function d() {} function b() {} function p() {} function f() {} function k() {} function y() {} function x(e, a) { if (!e || e <= 0) throw Error("n must be greater than 0 verticies"); var t, r = [], n = !1, o = !1, c = a.lastReadGroup; for (t = 0; t < e; t++) { for (var i = {}; "EOF" !== c && 0 !== c.code && !o; ) { switch (c.code) { case 10: if (n) { o = !0; continue } i.x = c.value, n = !0; break; case 20: i.y = c.value; break; case 30: i.z = c.value; break; case 40: i.startWidth = c.value; break; case 41: i.endWidth = c.value; break; case 42: 0 != c.value && (i.bulge = c.value); break; default: return n && r.push(i), r } c = a.next(); } r.push(i), n = !1, o = !1; } return a.rewind(), r } function h() {} function g() {} function E() {} function w() {} function m(e, a) { var t = { type: a.value }; for (a = e.next(); "EOF" != a && 0 != a.code; ) i(t, a), a = e.next(); return t } function P() {} function F() {} function O() {} u.ForEntityName = "3DFACE", u.prototype.parseEntity = function(e, a) { var t = { type: a.value, vertices: [] }; for (a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 70: t.shape = 1 == (1 & a.value), t.hasContinuousLinetypePattern = 128 == (128 & a.value); break; case 10: t.vertices = s(e, a), a = e.lastReadGroup; break; default: i(t, a); } a = e.next(); } return t } , l.ForEntityName = "ARC", l.prototype.parseEntity = function(e, a) { var t; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.center = c(e); break; case 40: t.radius = a.value; break; case 50: t.startAngle = Math.PI / 180 * a.value; break; case 51: t.endAngle = Math.PI / 180 * a.value, t.angleLength = t.endAngle - t.startAngle; break; default: i(t, a); } a = e.next(); } return t } , v.ForEntityName = "ATTDEF", v.prototype.parseEntity = function(e, a) { var t = { type: a.value, scale: 1, textStyle: "STANDARD" }; for (a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 1: t.text = a.value; break; case 2: t.tag = a.value; break; case 3: t.prompt = a.value; break; case 7: t.textStyle = a.value; break; case 10: t.startPoint = c(e); break; case 11: t.endPoint = c(e); break; case 39: t.thickness = a.value; break; case 40: t.textHeight = a.value; break; case 41: t.scale = a.value; break; case 50: t.rotation = a.value; break; case 51: t.obliqueAngle = a.value; break; case 70: t.invisible = !!(1 & a.value), t.constant = !!(2 & a.value), t.verificationRequired = !!(4 & a.value), t.preset = !!(8 & a.value); break; case 71: t.backwards = !!(2 & a.value), t.mirrored = !!(4 & a.value); break; case 72: t.horizontalJustification = a.value; break; case 73: t.fieldLength = a.value; break; case 74: t.verticalJustification = a.value; break; case 100: break; case 210: t.extrusionDirectionX = a.value; break; case 220: t.extrusionDirectionY = a.value; break; case 230: t.extrusionDirectionZ = a.value; break; default: i(t, a); } a = e.next(); } return t } , d.ForEntityName = "CIRCLE", d.prototype.parseEntity = function(e, a) { var t, r; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.center = c(e); break; case 40: t.radius = a.value; break; case 50: t.startAngle = Math.PI / 180 * a.value; break; case 51: (r = Math.PI / 180 * a.value) < t.startAngle ? t.angleLength = r + 2 * Math.PI - t.startAngle : t.angleLength = r - t.startAngle, t.endAngle = r; break; default: i(t, a); } a = e.next(); } return t } , b.ForEntityName = "DIMENSION", b.prototype.parseEntity = function(e, a) { var t; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 2: t.block = a.value; break; case 10: t.anchorPoint = c(e); break; case 11: t.middleOfText = c(e); break; case 12: t.insertionPoint = c(e); break; case 13: t.linearOrAngularPoint1 = c(e); break; case 14: t.linearOrAngularPoint2 = c(e); break; case 15: t.diameterOrRadiusPoint = c(e); break; case 16: t.arcPoint = c(e); break; case 70: t.dimensionType = a.value; break; case 71: t.attachmentPoint = a.value; break; case 42: t.actualMeasurement = a.value; break; case 1: t.text = a.value; break; case 50: t.angle = a.value; break; default: i(t, a); } a = e.next(); } return t } , p.ForEntityName = "ELLIPSE", p.prototype.parseEntity = function(e, a) { var t; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.center = c(e); break; case 11: t.majorAxisEndPoint = c(e); break; case 40: t.axisRatio = a.value; break; case 41: t.startAngle = a.value; break; case 42: t.endAngle = a.value; break; case 2: t.name = a.value; break; default: i(t, a); } a = e.next(); } return t } , f.ForEntityName = "INSERT", f.prototype.parseEntity = function(e, a) { var t; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 2: t.name = a.value; break; case 41: t.xScale = a.value; break; case 42: t.yScale = a.value; break; case 43: t.zScale = a.value; break; case 10: t.position = c(e); break; case 50: t.rotation = a.value; break; case 70: t.columnCount = a.value; break; case 71: t.rowCount = a.value; break; case 44: t.columnSpacing = a.value; break; case 45: t.rowSpacing = a.value; break; case 210: t.extrusionDirection = c(e); break; default: i(t, a); } a = e.next(); } return t } , k.ForEntityName = "LINE", k.prototype.parseEntity = function(e, a) { var t = { type: a.value, vertices: [] }; for (a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.vertices.unshift(c(e)); break; case 11: t.vertices.push(c(e)); break; case 210: t.extrusionDirection = c(e); break; case 100: break; default: i(t, a); } a = e.next(); } return t } , y.ForEntityName = "LWPOLYLINE", y.prototype.parseEntity = function(e, a) { var t = { type: a.value, vertices: [] } , r = 0; for (a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 38: t.elevation = a.value; break; case 39: t.depth = a.value; break; case 70: t.shape = 1 == (1 & a.value), t.hasContinuousLinetypePattern = 128 == (128 & a.value); break; case 90: r = a.value; break; case 10: //t.vertices = x(r, e); //改,因有的文件有点问题,可能版本原因 t.vertices.push(...x(r, e));//有的文件同一个entity需要多次执行到这里,所以不能直接覆盖,要push t.vertices = t.vertices.filter(e=>e.x!=void 0);//而且会出现空的vertix:{},所以排除下 break; case 43: 0 !== a.value && (t.width = a.value); break; case 210: t.extrusionDirectionX = a.value; break; case 220: t.extrusionDirectionY = a.value; break; case 230: t.extrusionDirectionZ = a.value; break; default: i(t, a); } a = e.next(); } return t } , h.ForEntityName = "MTEXT", h.prototype.parseEntity = function(e, a) { var t = { type: a.value }; for (a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 3: case 1: t.text ? t.text += a.value : t.text = a.value; break; case 10: t.position = c(e); break; case 40: t.height = a.value; break; case 41: t.width = a.value; break; case 50: t.rotation = a.value; break; case 71: t.attachmentPoint = a.value; break; case 72: t.drawingDirection = a.value; break; default: i(t, a); } a = e.next(); } return t } , g.ForEntityName = "POINT", g.prototype.parseEntity = function(e, a) { var t; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.position = c(e); break; case 39: t.thickness = a.value; break; case 210: t.extrusionDirection = c(e); break; case 100: break; default: i(t, a); } a = e.next(); } return t } , E.ForEntityName = "VERTEX", E.prototype.parseEntity = function(e, a) { var t = { type: a.value }; for (a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.x = a.value; break; case 20: t.y = a.value; break; case 30: t.z = a.value; break; case 40: case 41: case 42: 0 != a.value && (t.bulge = a.value); break; case 70: t.curveFittingVertex = 0 != (1 & a.value), t.curveFitTangent = 0 != (2 & a.value), t.splineVertex = 0 != (8 & a.value), t.splineControlPoint = 0 != (16 & a.value), t.threeDPolylineVertex = 0 != (32 & a.value), t.threeDPolylineMesh = 0 != (64 & a.value), t.polyfaceMeshVertex = 0 != (128 & a.value); break; case 50: case 71: t.faceA = a.value; break; case 72: t.faceB = a.value; break; case 73: t.faceC = a.value; break; case 74: t.faceD = a.value; break; default: i(t, a); } a = e.next(); } return t } , w.ForEntityName = "POLYLINE", w.prototype.parseEntity = function(e, a) { var t = { type: a.value, vertices: [] }; for (a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: case 20: case 30: case 39: t.thickness = a.value; break; case 40: case 41: break; case 70: t.shape = 0 != (1 & a.value), t.includesCurveFitVertices = 0 != (2 & a.value), t.includesSplineFitVertices = 0 != (4 & a.value), t.is3dPolyline = 0 != (8 & a.value), t.is3dPolygonMesh = 0 != (16 & a.value), t.is3dPolygonMeshClosed = 0 != (32 & a.value), t.isPolyfaceMesh = 0 != (64 & a.value), t.hasContinuousLinetypePattern = 0 != (128 & a.value); break; case 71: case 72: case 73: case 74: case 75: break; case 210: t.extrusionDirection = c(e); break; default: i(t, a); } a = e.next(); } return t.vertices = function(e, a) { var t = new E , r = []; for (; !e.isEOF(); ) if (0 === a.code) if ("VERTEX" === a.value) r.push(t.parseEntity(e, a)), a = e.lastReadGroup; else if ("SEQEND" === a.value) { m(e, a); break } return r }(e, a), t } , P.ForEntityName = "SOLID", P.prototype.parseEntity = function(e, a) { var t; for ((t = { type: a.value }).points = [], a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.points[0] = c(e); break; case 11: t.points[1] = c(e); break; case 12: t.points[2] = c(e); break; case 13: t.points[3] = c(e); break; case 210: t.extrusionDirection = c(e); break; default: i(t, a); } a = e.next(); } return t } , F.ForEntityName = "SPLINE", F.prototype.parseEntity = function(e, a) { var t; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.controlPoints || (t.controlPoints = []), t.controlPoints.push(c(e)); break; case 11: t.fitPoints || (t.fitPoints = []), t.fitPoints.push(c(e)); break; case 12: t.startTangent = c(e); break; case 13: t.endTangent = c(e); break; case 40: t.knotValues || (t.knotValues = []), t.knotValues.push(a.value); break; case 70: 0 != (1 & a.value) && (t.closed = !0), 0 != (2 & a.value) && (t.periodic = !0), 0 != (4 & a.value) && (t.rational = !0), 0 != (8 & a.value) && (t.planar = !0), 0 != (16 & a.value) && (t.planar = !0, t.linear = !0); break; case 71: t.degreeOfSplineCurve = a.value; break; case 72: t.numberOfKnots = a.value; break; case 73: t.numberOfControlPoints = a.value; break; case 74: t.numberOfFitPoints = a.value; break; case 210: t.normalVector = c(e); break; default: i(t, a); } a = e.next(); } return t } , O.ForEntityName = "TEXT", O.prototype.parseEntity = function(e, a) { var t; for (t = { type: a.value }, a = e.next(); "EOF" !== a && 0 !== a.code; ) { switch (a.code) { case 10: t.startPoint = c(e); break; case 11: t.endPoint = c(e); break; case 40: t.textHeight = a.value; break; case 41: t.xScale = a.value; break; case 50: t.rotation = a.value; break; case 1: t.text = a.value; break; case 72: t.halign = a.value; break; case 73: t.valign = a.value; break; default: i(t, a); } a = e.next(); } return t } ; var N = t(0); function T() { var e; this._entityHandlers = {}, (e = this).registerEntityHandler(u), e.registerEntityHandler(l), e.registerEntityHandler(v), e.registerEntityHandler(d), e.registerEntityHandler(b), e.registerEntityHandler(p), e.registerEntityHandler(f), e.registerEntityHandler(k), e.registerEntityHandler(y), e.registerEntityHandler(h), e.registerEntityHandler(g), e.registerEntityHandler(w), e.registerEntityHandler(P), e.registerEntityHandler(F), e.registerEntityHandler(O); } function S(e) { N.debug("unhandled group " + L(e)); } function L(e) { return e.code + ":" + e.value } N.setLevel("error"), T.prototype.parse = function(e, a) { throw new Error("read() not implemented. Use readSync()") } , T.prototype.registerEntityHandler = function(e) { var a = new e; this._entityHandlers[e.ForEntityName] = a; } , T.prototype.parseSync = function(e) { return "string" == typeof e ? this._parse(e) : (console.error("Cannot read dxf source of type `" + typeof e), null) } , T.prototype.parseStream = function(e, a) { var t = "" , r = this; e.on("data", function(e) { t += e; }), e.on("end", function() { try { var e = r._parse(t); } catch (e) { return a(e) } a(null, e); }), e.on("error", function(e) { a(e); }); } , T.prototype._parse = function(e) { var a, t, n = {}, c = 0, i = e.split(/\r\n|\r|\n/g); if (!(a = new r(i)).hasNext()) throw Error("Empty file"); var u = this , s = function(e, a) { return t.code === e && t.value === a } , l = function() { var e = null , r = null , n = {}; for (t = a.next(); ; ) { if (s(0, "ENDSEC")) { e && (n[e] = r); break } 9 === t.code ? (e && (n[e] = r), e = t.value) : 10 === t.code ? r = { x: t.value } : 20 === t.code ? r.y = t.value : 30 === t.code ? r.z = t.value : r = t.value, t = a.next(); } return t = a.next(), n } , v = function() { var e, r = {}; for (t = a.next(); "EOF" !== t.value && !s(0, "ENDSEC"); ) s(0, "BLOCK") ? (N.debug("block {"), e = d(), N.debug("}"), x(e), e.name ? r[e.name] = e : N.error('block with handle "' + e.handle + '" is missing a name.')) : (S(t), t = a.next()); return r } , d = function() { var e = {}; for (t = a.next(); "EOF" !== t.value; ) { switch (t.code) { case 1: e.xrefPath = t.value, t = a.next(); break; case 2: e.name = t.value, t = a.next(); break; case 3: e.name2 = t.value, t = a.next(); break; case 5: e.handle = t.value, t = a.next(); break; case 8: e.layer = t.value, t = a.next(); break; case 10: e.position = y(), t = a.next(); break; case 67: e.paperSpace = !(!t.value || 1 != t.value), t = a.next(); break; case 70: 0 != t.value && (e.type = t.value), t = a.next(); break; case 100: t = a.next(); break; case 330: e.ownerHandle = t.value, t = a.next(); break; case 0: if ("ENDBLK" == t.value) break; e.entities = k(!0); break; default: S(t), t = a.next(); } if (s(0, "ENDBLK")) { t = a.next(); break } } return e } , b = function() { var e = {}; for (t = a.next(); "EOF" !== t.value && !s(0, "ENDSEC"); ) { s(0, "TABLE") ? (t = a.next(), f[t.value] ? (N.debug(t.value + " Table {"), e[f[t.value].tableName] = p(), N.debug("}")) : N.debug("Unhandled Table " + t.value)) : t = a.next(); } return t = a.next(), e }; var p = function() { var e, r = f[t.value], n = {}, o = 0; for (t = a.next(); !s(0, "ENDTAB"); ) switch (t.code) { case 5: n.handle = t.value, t = a.next(); break; case 330: n.ownerHandle = t.value, t = a.next(); break; case 100: "AcDbSymbolTable" === t.value ? t = a.next() : (S(t), t = a.next()); break; case 70: o = t.value, t = a.next(); break; case 0: t.value === r.dxfSymbolName ? n[r.tableRecordsProperty] = r.parseTableRecords() : (S(t), t = a.next()); break; default: S(t), t = a.next(); } var c = n[r.tableRecordsProperty]; return c && (c.constructor === Array ? e = c.length : "object" == typeof c && (e = Object.keys(c).length), o !== e && N.warn("Parsed " + e + " " + r.dxfSymbolName + "'s but expected " + o)), t = a.next(), n } , f = { VPORT: { tableRecordsProperty: "viewPorts", tableName: "viewPort", dxfSymbolName: "VPORT", parseTableRecords: function() { var e = [] , r = {}; for (N.debug("ViewPort {"), t = a.next(); !s(0, "ENDTAB"); ) switch (t.code) { case 2: r.name = t.value, t = a.next(); break; case 10: r.lowerLeftCorner = y(), t = a.next(); break; case 11: r.upperRightCorner = y(), t = a.next(); break; case 12: r.center = y(), t = a.next(); break; case 13: r.snapBasePoint = y(), t = a.next(); break; case 14: r.snapSpacing = y(), t = a.next(); break; case 15: r.gridSpacing = y(), t = a.next(); break; case 16: r.viewDirectionFromTarget = y(), t = a.next(); break; case 17: r.viewTarget = y(), t = a.next(); break; case 42: r.lensLength = t.value, t = a.next(); break; case 43: r.frontClippingPlane = t.value, t = a.next(); break; case 44: r.backClippingPlane = t.value, t = a.next(); break; case 45: r.viewHeight = t.value, t = a.next(); break; case 50: r.snapRotationAngle = t.value, t = a.next(); break; case 51: r.viewTwistAngle = t.value, t = a.next(); break; case 79: r.orthographicType = t.value, t = a.next(); break; case 110: r.ucsOrigin = y(), t = a.next(); break; case 111: r.ucsXAxis = y(), t = a.next(); break; case 112: r.ucsYAxis = y(), t = a.next(); break; case 110: r.ucsOrigin = y(), t = a.next(); break; case 281: r.renderMode = t.value, t = a.next(); break; case 281: r.defaultLightingType = t.value, t = a.next(); break; case 292: r.defaultLightingOn = t.value, t = a.next(); break; case 330: r.ownerHandle = t.value, t = a.next(); break; case 63: case 421: case 431: r.ambientColor = t.value, t = a.next(); break; case 0: "VPORT" === t.value && (N.debug("}"), e.push(r), N.debug("ViewPort {"), r = {}, t = a.next()); break; default: S(t), t = a.next(); } return N.debug("}"), e.push(r), e } }, LTYPE: { tableRecordsProperty: "lineTypes", tableName: "lineType", dxfSymbolName: "LTYPE", parseTableRecords: function() { var e, r, n = {}, o = {}; for (N.debug("LType {"), t = a.next(); !s(0, "ENDTAB"); ) switch (t.code) { case 2: o.name = t.value, e = t.value, t = a.next(); break; case 3: o.description = t.value, t = a.next(); break; case 73: (r = t.value) > 0 && (o.pattern = []), t = a.next(); break; case 40: o.patternLength = t.value, t = a.next(); break; case 49: o.pattern.push(t.value), t = a.next(); break; case 0: N.debug("}"), r > 0 && r !== o.pattern.length && N.warn("lengths do not match on LTYPE pattern"), n[e] = o, o = {}, N.debug("LType {"), t = a.next(); break; default: t = a.next(); } return N.debug("}"), n[e] = o, n } }, LAYER: { tableRecordsProperty: "layers", tableName: "layer", dxfSymbolName: "LAYER", parseTableRecords: function() { var e, r, n = {}, c = {}; for (N.debug("Layer {"), t = a.next(); !s(0, "ENDTAB"); ) switch (t.code) { case 2: c.name = t.value, e = t.value, t = a.next(); break; case 62: c.visible = t.value >= 0, c.colorIndex = Math.abs(t.value), c.color = (r = c.colorIndex, o[r]), t = a.next(); break; case 70: c.frozen = 0 != (1 & t.value) || 0 != (2 & t.value), t = a.next(); break; case 0: "LAYER" === t.value && (N.debug("}"), n[e] = c, N.debug("Layer {"), c = {}, e = void 0, t = a.next()); break; default: S(t), t = a.next(); } return N.debug("}"), n[e] = c, n } } } , k = function(e) { var r = [] , n = e ? "ENDBLK" : "ENDSEC"; for (e || (t = a.next()); ; ) if (0 === t.code) { if (t.value === n) break; var o, c = u._entityHandlers[t.value]; if (null == c) { N.warn("Unhandled entity " + t.value), t = a.next(); continue } N.debug(t.value + " {"), o = c.parseEntity(a, t), t = a.lastReadGroup, N.debug("}"), x(o), r.push(o); } else t = a.next(); return "ENDSEC" == n && (t = a.next()), r } , y = function() { var e = {} , r = t.code; if (e.x = t.value, r += 10, (t = a.next()).code != r) throw new Error("Expected code for point value to be " + r + " but got " + t.code + "."); return e.y = t.value, r += 10, (t = a.next()).code != r ? (a.rewind(), e) : (e.z = t.value, e) } , x = function(e) { if (!e) throw new TypeError("entity cannot be undefined or null"); e.handle || (e.handle = c++); }; return function() { for (t = a.next(); !a.isEOF(); ) if (0 === t.code && "SECTION" === t.value) { if (2 !== (t = a.next()).code) { console.error("Unexpected code %s after 0:SECTION", L(t)), t = a.next(); continue } "HEADER" === t.value ? (N.debug("> HEADER"), n.header = l(), N.debug("<")) : "BLOCKS" === t.value ? (N.debug("> BLOCKS"), n.blocks = v(), N.debug("<")) : "ENTITIES" === t.value ? (N.debug("> ENTITIES"), n.entities = k(!1), N.debug("<")) : "TABLES" === t.value ? (N.debug("> TABLES"), n.tables = b(), N.debug("<")) : "EOF" === t.value ? N.debug("EOF") : N.warn("Skipping section '%s'", t.value); } else t = a.next(); }(), n } ; a.default = T; } ]).default }); class DxfLoader extends EventDispatcher{ constructor(){ super(); this.parser = new Potree.DxfParser; } load(url,done, options={}){ Potree.loadFile(url, {returnText:true} , (data)=>{ var dxfJson = this.parser.parseSync(data); console.log(dxfJson); let model = this.createModel(dxfJson, options); done(model); }); } createModel(data, options={}){ let root = new Object3D; var i, entity, obj, min_x, min_y, min_z, max_x, max_y, max_z; /* var dims = { min: { x: false, y: false, z: false}, max: { x: false, y: false, z: false} }; */ //let bound = THREE.Box3 for(i = 0; i < data.entities.length; i++) { entity = data.entities[i]; obj = drawEntity(entity, data, options.unsupportTypes ); if (obj) { var bbox = new Box3().setFromObject(obj); /* if (bbox.min.x && ((dims.min.x === false) || (dims.min.x > bbox.min.x))) dims.min.x = bbox.min.x; if (bbox.min.y && ((dims.min.y === false) || (dims.min.y > bbox.min.y))) dims.min.y = bbox.min.y; if (bbox.min.z && ((dims.min.z === false) || (dims.min.z > bbox.min.z))) dims.min.z = bbox.min.z; if (bbox.max.x && ((dims.max.x === false) || (dims.max.x < bbox.max.x))) dims.max.x = bbox.max.x; if (bbox.max.y && ((dims.max.y === false) || (dims.max.y < bbox.max.y))) dims.max.y = bbox.max.y; if (bbox.max.z && ((dims.max.z === false) || (dims.max.z < bbox.max.z))) dims.max.z = bbox.max.z; */ root.add(obj); } obj = null; } return root } } function drawEntity(entity, data, unsupportTypes, group) { var mesh; if(unsupportTypes && unsupportTypes.includes(entity.type)){ console.warn('����һ��dxf������Ƶ�type��', entity.type); return //add �����Ƹ��� } if(entity.type === 'CIRCLE' || entity.type === 'ARC') { mesh = drawArc(entity, data, unsupportTypes, group ); } else if(entity.type === 'LWPOLYLINE' || entity.type === 'LINE' || entity.type === 'POLYLINE') { mesh = drawLine(entity, data, unsupportTypes, group ); } else if(entity.type === 'TEXT') {//"MTEXT" text: "{\fKaiTi|b0|i0|c134|p49;�������̨}" mesh = drawText(entity, data, unsupportTypes, group); } else if(entity.type === 'SOLID') { mesh = drawSolid(entity, data); } else if(entity.type === 'POINT') { mesh = drawPoint(entity, data ); } else if(entity.type === 'INSERT') {//Ƕ������group mesh = drawBlock(entity, data, unsupportTypes, group ); } else if(entity.type === 'SPLINE') { mesh = drawSpline(entity, data); } else if(entity.type === 'MTEXT') { mesh = drawMtext(entity, data, unsupportTypes, group ); } else if(entity.type === 'ELLIPSE') { mesh = drawEllipse(entity, data, unsupportTypes, group ); } else if(entity.type === 'DIMENSION') { var dimTypeEnum = entity.dimensionType & 7; if(dimTypeEnum === 0) { mesh = drawDimension(entity, data); } else { console.log("Unsupported Dimension type: " + dimTypeEnum); } } else { console.log("Unsupported Entity Type: " + entity.type); } if(entity.type === 'LWPOLYLINE' || entity.type === 'LINE' || entity.type === 'POLYLINE') { mesh = drawLine(entity, data); } /* if(entity.type === 'INSERT') { mesh = drawBlock(entity, data); //group } */ /* �Ŷ���arc �ƺ�ǽ�ڶ��Dz���insert���ͣ��������� */ mesh && (mesh.dxfInfo = entity); //add return mesh; } function drawEllipse(entity, data) { var color = getColor$2(entity, data); var xrad = Math.sqrt(Math.pow(entity.majorAxisEndPoint.x,2) + Math.pow(entity.majorAxisEndPoint.y,2)); var yrad = xrad*entity.axisRatio; var rotation = Math.atan2(entity.majorAxisEndPoint.y, entity.majorAxisEndPoint.x); var curve = new EllipseCurve( entity.center.x, entity.center.y, xrad, yrad, entity.startAngle, entity.endAngle, false, // Always counterclockwise rotation ); var points = curve.getPoints( 50 ); var geometry = new BufferGeometry().setFromPoints( points ); var material = new LineBasicMaterial( { linewidth: 1, color : color } ); // Create the final object to add to the scene var ellipse = new Line( geometry, material ); return ellipse; } function drawMtext(entity, data) { var color = getColor$2(entity, data); //if (!font) { return console.log('font parameter not set. Ignoring text entity.')} var geometry = new TextGeometry( entity.text, { //font: font, size: entity.height * (4/5), height: 1 }); var material = new MeshBasicMaterial( {color: color} ); var text = new Mesh( geometry, material ); // Measure what we rendered. var measure = new Box3(); measure.setFromObject( text ); var textWidth = measure.max.x - measure.min.x; // If the text ends up being wider than the box, it's supposed // to be multiline. Doing that in threeJS is overkill. if (textWidth > entity.width) { console.log("Can't render this multipline MTEXT entity, sorry.", entity); return undefined; } text.position.z = 0; switch (entity.attachmentPoint) { case 1: // Top Left text.position.x = entity.position.x; text.position.y = entity.position.y - entity.height; break; case 2: // Top Center text.position.x = entity.position.x - textWidth/2; text.position.y = entity.position.y - entity.height; break; case 3: // Top Right text.position.x = entity.position.x - textWidth; text.position.y = entity.position.y - entity.height; break; case 4: // Middle Left text.position.x = entity.position.x; text.position.y = entity.position.y - entity.height/2; break; case 5: // Middle Center text.position.x = entity.position.x - textWidth/2; text.position.y = entity.position.y - entity.height/2; break; case 6: // Middle Right text.position.x = entity.position.x - textWidth; text.position.y = entity.position.y - entity.height/2; break; case 7: // Bottom Left text.position.x = entity.position.x; text.position.y = entity.position.y; break; case 8: // Bottom Center text.position.x = entity.position.x - textWidth/2; text.position.y = entity.position.y; break; case 9: // Bottom Right text.position.x = entity.position.x - textWidth; text.position.y = entity.position.y; break; default: return undefined; }; return text; } function drawSpline(entity, data) { var color = getColor$2(entity, data); var points = entity.controlPoints.map(function(vec) { return new Vector2(vec.x, vec.y); }); var interpolatedPoints = []; var curve; if (entity.degreeOfSplineCurve === 2 || entity.degreeOfSplineCurve === 3) { for(var i = 0; i + 2 < points.length; i = i + 2) { if (entity.degreeOfSplineCurve === 2) { curve = new QuadraticBezierCurve(points[i], points[i + 1], points[i + 2]); } else { curve = new QuadraticBezierCurve3(points[i], points[i + 1], points[i + 2]); } interpolatedPoints.push.apply(interpolatedPoints, curve.getPoints(50)); } } else { curve = new SplineCurve(points); interpolatedPoints = curve.getPoints( 100 ); } var geometry = new BufferGeometry().setFromPoints( interpolatedPoints ); var material = new LineBasicMaterial( { linewidth: 1, color : color } ); var splineObject = new Line( geometry, material ); return splineObject; } function drawLine(entity, data) { var geometry = new Geometry(), color = getColor$2(entity, data), material, lineType, vertex, startPoint, endPoint, bulgeGeometry, bulge, i, line; if (!entity.vertices) return console.log('entity missing vertices.'); // create geometry for(i = 0; i < entity.vertices.length; i++) { if(entity.vertices[i].bulge) { bulge = entity.vertices[i].bulge; startPoint = entity.vertices[i]; endPoint = i + 1 < entity.vertices.length ? entity.vertices[i + 1] : geometry.vertices[0]; bulgeGeometry = new THREEx.BulgeGeometry(startPoint, endPoint, bulge); geometry.vertices.push.apply(geometry.vertices, bulgeGeometry.vertices); } else { vertex = entity.vertices[i]; geometry.vertices.push(new Vector3(vertex.x, vertex.y, 0)); } } if(entity.shape) geometry.vertices.push(geometry.vertices[0]); // set material if(entity.lineType) { lineType = data.tables.lineType.lineTypes[entity.lineType]; } if(lineType && lineType.pattern && lineType.pattern.length !== 0) { material = new LineDashedMaterial({ color: color, gapSize: 4, dashSize: 4}); } else { material = new LineBasicMaterial({ linewidth: 1, color: color }); } // if(lineType && lineType.pattern && lineType.pattern.length !== 0) { // geometry.computeLineDistances(); // // Ugly hack to add diffuse to this. Maybe copy the uniforms object so we // // don't add diffuse to a material. // lineType.material.uniforms.diffuse = { type: 'c', value: new THREE.Color(color) }; // material = new THREE.ShaderMaterial({ // uniforms: lineType.material.uniforms, // vertexShader: lineType.material.vertexShader, // fragmentShader: lineType.material.fragmentShader // }); // }else { // material = new THREE.LineBasicMaterial({ linewidth: 1, color: color }); // } line = new Line(geometry, material); return line; } function drawArc(entity, data) { var startAngle, endAngle; if (entity.type === 'CIRCLE') { startAngle = entity.startAngle || 0; endAngle = startAngle + 2 * Math.PI; } else { startAngle = entity.startAngle; endAngle = entity.endAngle; } var curve = new ArcCurve( 0, 0, entity.radius, startAngle, endAngle); var points = curve.getPoints( 32 ); var geometry = new BufferGeometry().setFromPoints( points ); var material = new LineBasicMaterial({ color: getColor$2(entity, data) }); var arc = new Line(geometry, material); arc.position.x = entity.center.x; arc.position.y = entity.center.y; arc.position.z = entity.center.z; return arc; } function drawSolid(entity, data) { var material, mesh, verts, geometry = new Geometry(); verts = geometry.vertices; verts.push(new Vector3(entity.points[0].x, entity.points[0].y, entity.points[0].z)); verts.push(new Vector3(entity.points[1].x, entity.points[1].y, entity.points[1].z)); verts.push(new Vector3(entity.points[2].x, entity.points[2].y, entity.points[2].z)); verts.push(new Vector3(entity.points[3].x, entity.points[3].y, entity.points[3].z)); // Calculate which direction the points are facing (clockwise or counter-clockwise) var vector1 = new Vector3(); var vector2 = new Vector3(); vector1.subVectors(verts[1], verts[0]); vector2.subVectors(verts[2], verts[0]); vector1.cross(vector2); // If z < 0 then we must draw these in reverse order if(vector1.z < 0) { geometry.faces.push(new Face3(2, 1, 0)); geometry.faces.push(new Face3(2, 3, 1)); } else { geometry.faces.push(new Face3(0, 1, 2)); geometry.faces.push(new Face3(1, 3, 2)); } material = new MeshBasicMaterial({ color: getColor$2(entity, data) }); return new Mesh(geometry, material); } function drawText(entity, data) { var geometry, material, text; //����Ҫfont���ܻ������� /* if(!font) return console.warn('Text is not supported without a Three.js font loaded with THREE.FontLoader! Load a font of your choice and pass this into the constructor. See the sample for this repository or Three.js examples at http://threejs.org/examples/?q=text#webgl_geometry_text for more details.'); */ geometry = new TextGeometry(entity.text, {/* font: font, */ height: 0, size: entity.textHeight || 12 }); if (entity.rotation) { var zRotation = entity.rotation * Math.PI / 180; geometry.rotateZ(zRotation); } material = new MeshBasicMaterial({ color: getColor$2(entity, data) }); text = new Mesh(geometry, material); text.position.x = entity.startPoint.x; text.position.y = entity.startPoint.y; text.position.z = entity.startPoint.z; return text; } function drawPoint(entity, data) { var geometry, material, point; geometry = new Geometry(); geometry.vertices.push(new Vector3(entity.position.x, entity.position.y, entity.position.z)); // TODO: could be more efficient. PointCloud per layer? var numPoints = 1; var color = getColor$2(entity, data); var colors = new Float32Array( numPoints*3 ); colors[0] = color.r; colors[1] = color.g; colors[2] = color.b; geometry.colors = colors; geometry.computeBoundingBox(); material = new PointsMaterial( { size: 0.05, vertexColors: VertexColors } ); point = new Points(geometry, material); //scene.add(point); return point } function drawDimension(entity, data, unsupportTypes, parentGroup) { return drawBlock(entity, data, unsupportTypes, parentGroup, true) //�� /* var block = data.blocks[entity.block]; if (!block || !block.entities) return null; var group = new THREE.Object3D(); // if(entity.anchorPoint) { // group.position.x = entity.anchorPoint.x; // group.position.y = entity.anchorPoint.y; // group.position.z = entity.anchorPoint.z; // } for(var i = 0; i < block.entities.length; i++) { var childEntity = drawEntity(block.entities[i], data, group); if(childEntity && !group.children.includes(childEntity)) group.add(childEntity); } return group; */ } function drawBlock(entity, data, unsupportTypes, parentGroup, isDimension) { var block = data.blocks[entity.name]; if (!block.entities) return null; var group = new Object3D(); parentGroup && parentGroup.add(group);//��ǰ���� Ϊ�˻��matrixWorld if(!isDimension){ if(entity.position) { group.position.x = entity.position.x; group.position.y = entity.position.y; group.position.z = entity.position.z; } if(entity.xScale){ group.scale.x = entity.xScale; } if(entity.yScale){ group.scale.y = entity.yScale; } if(entity.rotation) { group.rotation.z = entity.rotation * Math.PI / 180; } //add: ���������еĻ������������Զ add zScale var flag; if(entity.zScale != void 0 && entity.extrusionDirection){//see (005.dxf�� others/01.dxf) extrusionDirection ={x:0,y:0,z:-1} group.position.x *= Math.sign(entity.zScale); group.scale.x *= Math.sign(entity.zScale); group.rotation.z *= Math.sign(entity.zScale); flag = 1; //�����������extrusionDirection } }else { /* if(entity.anchorPoint) { group.position.x = entity.anchorPoint.x; group.position.y = entity.anchorPoint.y; group.position.z = entity.anchorPoint.z; } */ } //add: group.updateMatrixWorld(); var visiEntities = block.entities.filter(e=>e.visible !== false); //console.log("block�Ŀɼ�children����"+block.entities.length) for(var i = 0; i < visiEntities.length; i++) { var childEntity = drawEntity(visiEntities[i], data, unsupportTypes, group ); if(childEntity && !group.children.includes(childEntity)) group.add(childEntity); } return group; } function getColor$2(entity, data) { var color = 0x000000; //default if(entity.color) color = entity.color; else if(data.tables && data.tables.layer && data.tables.layer.layers[entity.layer]) color = data.tables.layer.layers[entity.layer].color; if(color == null || color === 0xffffff) { color = 0x000000; } return color; } function createLineTypeShaders(data) { var ltype, type; if(!data.tables || !data.tables.lineType) return; var ltypes = data.tables.lineType.lineTypes; for(type in ltypes) { ltype = ltypes[type]; if(!ltype.pattern) continue; ltype.material = createDashedLineShader(ltype.pattern); } } function createDashedLineShader(pattern) { var i, dashedLineShader = {}, totalLength = 0.0; for(i = 0; i < pattern.length; i++) { totalLength += Math.abs(pattern[i]); } dashedLineShader.uniforms = UniformsUtils.merge([ UniformsLib[ 'common' ], UniformsLib[ 'fog' ], { 'pattern': { type: 'fv1', value: pattern }, 'patternLength': { type: 'f', value: totalLength } } ]); dashedLineShader.vertexShader = [ 'attribute float lineDistance;', 'varying float vLineDistance;', ShaderChunk[ 'color_pars_vertex' ], 'void main() {', ShaderChunk[ 'color_vertex' ], 'vLineDistance = lineDistance;', 'gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );', '}' ].join('\n'); dashedLineShader.fragmentShader = [ 'uniform vec3 diffuse;', 'uniform float opacity;', 'uniform float pattern[' + pattern.length + '];', 'uniform float patternLength;', 'varying float vLineDistance;', ShaderChunk[ 'color_pars_fragment' ], ShaderChunk[ 'fog_pars_fragment' ], 'void main() {', 'float pos = mod(vLineDistance, patternLength);', 'for ( int i = 0; i < ' + pattern.length + '; i++ ) {', 'pos = pos - abs(pattern[i]);', 'if( pos < 0.0 ) {', 'if( pattern[i] > 0.0 ) {', 'gl_FragColor = vec4(1.0, 0.0, 0.0, opacity );', 'break;', '}', 'discard;', '}', '}', ShaderChunk[ 'color_fragment' ], ShaderChunk[ 'fog_fragment' ], '}' ].join('\n'); return dashedLineShader; } function findExtents(scene) { for(var child of scene.children) { var minX, maxX, minY, maxY; if(child.position) { minX = Math.min(child.position.x, minX); minY = Math.min(child.position.y, minY); maxX = Math.max(child.position.x, maxX); maxY = Math.max(child.position.y, maxY); } } return { min: { x: minX, y: minY }, max: { x: maxX, y: maxY }}; } // Three.js extension functions. Webpack doesn't seem to like it if we modify the THREE object directly. var THREEx = { Math: {} }; /** * Returns the angle in radians of the vector (p1,p2). In other words, imagine * putting the base of the vector at coordinates (0,0) and finding the angle * from vector (1,0) to (p1,p2). * @param {Object} p1 start point of the vector * @param {Object} p2 end point of the vector * @return {Number} the angle */ THREEx.Math.angle2 = function(p1, p2) { var v1 = new Vector2(p1.x, p1.y); var v2 = new Vector2(p2.x, p2.y); v2.sub(v1); // sets v2 to be our chord v2.normalize(); if(v2.y < 0) return -Math.acos(v2.x); return Math.acos(v2.x); }; THREEx.Math.polar = function(point, distance, angle) { var result = {}; result.x = point.x + distance * Math.cos(angle); result.y = point.y + distance * Math.sin(angle); return result; }; /** * Calculates points for a curve between two points * @param startPoint - the starting point of the curve * @param endPoint - the ending point of the curve * @param bulge - a value indicating how much to curve * @param segments - number of segments between the two given points */ THREEx.BulgeGeometry = function ( startPoint, endPoint, bulge, segments ) { var vertex, i, center, p0, p1, angle, radius, startAngle, thetaAngle; Geometry.call( this ); this.startPoint = p0 = startPoint ? new Vector2(startPoint.x, startPoint.y) : new Vector2(0,0); this.endPoint = p1 = endPoint ? new Vector2(endPoint.x, endPoint.y) : new Vector2(1,0); this.bulge = bulge = bulge || 1; angle = 4 * Math.atan(bulge); radius = p0.distanceTo(p1) / 2 / Math.sin(angle/2); center = THREEx.Math.polar(startPoint, radius, THREEx.Math.angle2(p0,p1) + (Math.PI / 2 - angle/2)); this.segments = segments = segments || Math.max( Math.abs(Math.ceil(angle/(Math.PI/18))), 6); // By default want a segment roughly every 10 degrees startAngle = THREEx.Math.angle2(center, p0); thetaAngle = angle / segments; this.vertices.push(new Vector3(p0.x, p0.y, 0)); for(i = 1; i <= segments - 1; i++) { vertex = THREEx.Math.polar(center, Math.abs(radius), startAngle + thetaAngle * i); this.vertices.push(new Vector3(vertex.x, vertex.y, 0)); } }; THREEx.BulgeGeometry.prototype = Object.create( Geometry.prototype ); /* -----note-------------- */ const manager = new LoadingManager(); let loaders = {}; let mapArea; let shelterHistory = []; Potree.isIframeChild = window.parent!=window; //子页面 /* if(Potree.isIframeChild){ let rootWin = Common.getRootWindow() rootWin && rootWin.viewer.dispatchEvent({type:'createIframe', window}) //给祖先页面发送信息,得不到可能跨域了或无viewer } */ /* window.addEventListener('focus',()=>{ console.log('focus',window.winIndex) }) window.addEventListener('blur',()=>{ console.log('blur',window.winIndex) }) */ class Viewer extends ViewerBase{ constructor(domElement, mapArea_, args = {}){ super(domElement, $.extend(args,{name:'mainViewer', antialias:true, preserveDrawingBuffer:true})); //注:viewer因为要分屏,尤其是四屏,preserveDrawingBuffer需要为true, 否则无法局部clear window.viewer = this; mapArea = mapArea_; if(Potree.settings.editType == "pano" || Potree.settings.editType == "merge"){ this.modules = { Alignment, SiteModel, volumeComputer: new VolumeComputer //暂时 }; Potree.settings.useDepthTex = false; if(Potree.settings.editType == "pano" ){ this.modules.PanoEditor = PanoEditor$1; }else if(Potree.settings.editType == "merge"){ this.modules.MergeEditor = MergeEditor; this.modules.CamAniEditor = CamAniEditor; } }else { this.modules = { Clip, Alignment, SiteModel, RouteGuider : new RouteGuider, clipping: new Clipping, ParticleEditor, CamAniEditor, volumeComputer: new VolumeComputer, MergeEditor }; } { Potree.timeCollect = { //median预置一个中等值,以防止cpu过低的设备首次卡顿 //depthSampler : {minCount:400, median: 1}, //旧的 实时从图片中获取色值的用时,但是现在直接从data中取 depthSamChangeImg:{minCount:200, median: 25} //换图的用时。 需要一开始就start }; for(let i in Potree.timeCollect){ Potree.timeCollect[i].measures = []; Potree.timeCollect[i].sum = 0; } setTimeout(()=>{ for(let i in Potree.timeCollect){ Potree.timeCollect[i].start = true; } /* setTimeout(()=>{ console.log('timeCollect', Potree.timeCollect.depthSampler.median, Potree.timeCollect.depthSampler.ave, Potree.timeCollect.depthSampler.measures.length) },10000) */ },2000); } this.navigateMode = 'free'; // 'panorama'; 'free'自由模式是只显示点云或者未进入到漫游点, this.isEdit = true; this.waitQueue = []; this.unitConvert = new UoMService(); this.visible = true; this.fpVisiDatasets = []; this.atDatasets = []; this.objs = new Object3D; this.testMaxNodeCount = 0; this.tiles3dVisiVCount = 0; //this.lastPos = new THREE.Vector3(Infinity,Infinity,Infinity) //------------- var supportExtFragDepth = !!Potree.Features.EXT_DEPTH.isSupported(this.renderer.getContext()) ;//iphoneX居然不支持 //这意味着边缘增强和测量线遮挡失效 if(!supportExtFragDepth)console.error('ExtFragDepth unsupported! 边缘增强和测量线遮挡失效'); this.guiLoaded = false; this.guiLoadTasks = []; this.onVrListeners = []; this.messages = []; this.elMessages = $(`
    `); $(domElement).append(this.elMessages); this.fakeMeasure = {}; try{ if(!Potree.settings.isOfficial) { // generate missing dom hierarchy if ($(domElement).find('#potree_map').length === 0) { let potreeMap = $(` `); $(domElement).append(potreeMap); } if ($(domElement).find('#potree_description').length === 0) { let potreeDescription = $(`
    `); $(domElement).append(potreeDescription); } if ($(domElement).find('#potree_annotations').length === 0) { let potreeAnnotationContainer = $(`
    `); $(domElement).append(potreeAnnotationContainer); } if ($(domElement).find('#potree_quick_buttons').length === 0) { let potreeMap = $(`
    `); $(domElement).append(potreeMap); } //add { $(domElement).append($("
    ")); if(!mapArea && (Potree.settings.editType != 'merge' /* || Potree.settings.showObjectsOnMap */)){ mapArea = $("
    "); $(domElement).append(mapArea); mapArea = mapArea[0]; } } /* let domRoot = this.renderer.domElement.parentElement; let elAttach = $(""); elAttach.css({ position : "absolute", right : '10%', bottom: '20px', zIndex: "10000", fontSize:'1em', color:"black", background:'rgba(255,255,255,0.8)', }) let state = false elAttach.on("click", () => { window.buttonFunction && window.buttonFunction() }); domRoot.appendChild(elAttach[0]); */ } this.tiles3dMemoryUsage = 0; this.pointCloudLoadedCallback = args.onPointCloudLoaded || function () {}; // if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) { // defaultSettings.navigation = "Orbit"; // } this.server = null; this.fov = 60; this.isFlipYZ = false; this.useDEMCollisions = false; this.generateDEM = false; this.minNodeSize = 30; //允许加载的node的最小可见像素宽度。越大越省性能 this.edlStrength = 1.0; this.edlRadius = 1.4; this.edlOpacity = 1.0; this.useEDL = false; this.description = ""; this.classifications = ClassificationScheme.DEFAULT; this.moveSpeed = 1; this.lengthUnit = LengthUnits.METER; this.lengthUnitDisplay = LengthUnits.METER; this.showBoundingBox = false; this.showAnnotations = true; this.freeze = false; this.elevationGradientRepeat = ElevationGradientRepeat.CLAMP; this.filterReturnNumberRange = [0, 7]; this.filterNumberOfReturnsRange = [0, 7]; this.filterGPSTimeRange = [-Infinity, Infinity]; this.filterPointSourceIDRange = [0, 65535]; this.potreeRenderer = null; this.edlRenderer = null; this.pRenderer = null; this.scene = null; this.sceneVR = null; this.overlay = null; this.overlayCamera = null; this.inputHandler = null; this.controls = null; this.clippingTool = null; this.transformationTool = null; this.navigationCube = null; this.compass = null; this.skybox = null; this.clock = new Clock(); this.background = null; this.buffers = new Map; if(args.noDragAndDrop){ }else { this.initDragAndDrop(); } if(typeof Stats !== "undefined"){ this.stats = new Stats(); this.stats.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom document.body.appendChild( this.stats.dom ); } { this.overlay = new Scene(); this.overlayCamera = new OrthographicCamera( 0, 1, 1, 0, -1000, 1000 ); } this.pRenderer = new Renderer$1(this.renderer); { let near = 2.5; let far = 10.0; let fov = 90; this.shadowTestCam = new PerspectiveCamera(90, 1, near, far); this.shadowTestCam.position.set(3.50, -2.80, 8.561); this.shadowTestCam.lookAt(new Vector3(0, 0, 4.87)); } let scene = new ExtendScene(this.renderer); { // create VR scene this.sceneVR = new Scene(); // let texture = new THREE.TextureLoader().load(`${Potree.resourcePath}/images/vr_controller_help.jpg`); // let plane = new THREE.PlaneBufferGeometry(1, 1, 1, 1); // let infoMaterial = new THREE.MeshBasicMaterial({map: texture}); // let infoNode = new THREE.Mesh(plane, infoMaterial); // infoNode.position.set(-0.5, 1, 0); // infoNode.scale.set(0.4, 0.3, 1); // infoNode.lookAt(0, 1, 0) // this.sceneVR.add(infoNode); // window.infoNode = infoNode; } this.setScene(scene); //add: for 截图时抗锯齿 { /* this.composer = new EffectComposer( this.renderer ); this.ssaaRenderPass = new SSAARenderPass(0x000000, 0); this.composer.addPass( this.ssaaRenderPass ); */ //this.ssaaRenderPass.useCopy = true //this.ssaaRenderPass.renderToScreen = true; //this.ssaaRenderPass.needsSwap = false //见 https://threejs.org/examples/?q=AA#webgl_postprocessing_fxaa 效果和SSAA差不多,都对透明不太友好。 /* let outlinePass = this.outlinePass = new OutlinePass( ); this.ssaaRenderPass.addPass(outlinePass) */ //ssaa有透明度、发黑的问题所以不用了 //-------------------------- this.composer = new EffectComposer( this.renderer ); this.composer.scaleRatio = 4; //将底图和测量线绘制在一张高倍贴图上,for测量线不模糊 this.composer.readTarget = true; //把底图和测量线一起fxaa /* const renderPass = new RenderPass(); //renderPass.clearColor = new THREE.Color( 0,0,0 ); //renderPass.clearAlpha = 0; renderPass.clear = !this.composer.readTarget this.composer.addPass( renderPass ); */ //for 融合页面 let outlinePass = this.outlinePass = new OutlinePass( ); outlinePass.renderToScreen = true; //这样更流畅,不用ssaa了,缺点是outline有锯齿 outlinePass.enabled = false; this.composer.addPass( outlinePass ); outlinePass.edgeStrength = 4; outlinePass.edgeGlow = 0; outlinePass.visibleEdgeColor = new Color("#09a1b3"); /* this.fxaaPass = new ShaderPass( FXAAShader ); this.fxaaPass.readTarget = true //add this.fxaaPass.setSize = function(width, height){ this.material.uniforms[ 'resolution' ].value.x = 1 / ( width ) ; this.material.uniforms[ 'resolution' ].value.y = 1 / ( height ) ; } this.fxaaPass.renderToScreen = true; this.composer.addPass( this.fxaaPass ); */ //抗锯齿截图 效果时而好时而不好,文字比较模糊 //这两个暂时不能一起用。目前刚好不需要一起用 /* this.images360.fastTranMaskPass = new FastTranPass( this.renderer ) this.composer.addPass(this.images360.fastTranMaskPass)//add */ } { this.mainViewport = new Viewport( this.scene.view, this.scene.cameraP, { left:0, bottom:0, width:1, height: 1, name:'MainView' }); this.viewports = [this.mainViewport]; Potree.settings.showCompass && (this.compass = new Compass(Potree.settings.compassDom, this.mainViewport)); //this.axis = new AxisViewer(this.mainViewport, this.renderArea ) this.magnifier = new Magnifier(this); this.reticule = new Reticule(this); this.scene.scene.add(this.magnifier); this.scene.scene.add(this.reticule); if(Potree.settings.editType != "pano" && (Potree.settings.editType != 'merge' /* || Potree.settings.showObjectsOnMap */)){ this.mapViewer = new MapViewer(mapArea/* $('#mapGaode')[0] */); } this.inputHandler = new InputHandler(this, this.scene.scene); this.inputHandler.containsMouse = true;//初始化,使键盘事件在mainViewer有效 //this.inputHandler.setScene(this.scene); //this.inputHandler.addInputListener(this);//add this.clippingTool = new ClippingTool(this); this.transformationTool = new TransformationTool(this); this.navigationCube = new NavigationCube(this); this.navigationCube.visible = false; this.createControls(); this.clippingTool.setScene(this.scene); let onPointcloudAdded = (e) => { if (this.scene.pointclouds.length === 1) { let speed = e.pointcloud.boundingBox.getSize(new Vector3()).length(); speed = speed / 2000; this.setMoveSpeed(speed); } }; let onVolumeRemoved = (e) => { this.inputHandler.deselect(e.volume); }; this.addEventListener('scene_changed', (e) => { this.inputHandler.setScene(e.scene); this.clippingTool.setScene(this.scene); if(!e.scene.hasEventListener("pointcloud_added", onPointcloudAdded)){ e.scene.addEventListener("pointcloud_added", onPointcloudAdded); } if(!e.scene.hasEventListener("volume_removed", onPointcloudAdded)){ e.scene.addEventListener("volume_removed", onVolumeRemoved); } }); this.scene.addEventListener("volume_removed", onVolumeRemoved); this.scene.addEventListener('pointcloud_added', onPointcloudAdded); } { // set defaults this.setFOV(60); this.setEDLEnabled(false); this.setEDLRadius(3); this.setEDLStrength(0.01); this.setEDLOpacity(1.0); this.setPointBudget(1*1000*1000); this.setShowBoundingBox(false); this.setFreeze(false); this.setControls(this.fpControls/* orbitControls */); this.setBackground( new Color(Potree.config.background),1 /* 'gradient' */ ); this.scaleFactor = 1; this.loadSettingsFromURL(); } // start rendering! //if(args.useDefaultRenderLoop === undefined || args.useDefaultRenderLoop === true){ //requestAnimationFrame(this.loop.bind(this)); //} this.renderer.setAnimationLoop(this.loop.bind(this)); this.loadGUI = this.loadGUI.bind(this); this.annotationTool = new AnnotationTool(this); this.measuringTool = new MeasuringTool(this); //this.profileTool = new ProfileTool(this); this.volumeTool = new VolumeTool(this); this.tagTool = new TagTool(this); //----------- CursorDeal.init(this, this.mapViewer ? [this, this.mapViewer] : [this]);//ADD if(Potree.settings.editType == "pano"){ this.modules.PanoEditor.init(); }else if(Potree.settings.editType == "merge"){ this.modules.MergeEditor.init(); }else { this.modules.SiteModel.init(); this.modules.ParticleEditor.init(); } this.modules.Alignment.init(); this.images360 = new Images360(this); this.scene.scene.add(this.objs); loaders = { objLoader : new OBJLoader( manager ), mtlLoader : new MTLLoader( manager ), glbLoader : new GLTFLoader(undefined, this.renderer, Potree.settings.libsUrl ), plyLoader : new PLYLoader( manager ), dxfLoader : new DxfLoader(), shapeLoader: new Potree.ShapefileLoader() }; //add test /* const environment = new RoomEnvironment(); const pmremGenerator = new THREE.PMREMGenerator( this.renderer ); this.scene.scene.environment = pmremGenerator.fromScene( environment ).texture; */ //----------- this.modules.volumeComputer && this.modules.volumeComputer.init(); }catch(e){ this.onCrash(e); } //-----------------------add---------------------------------------------------- /* { let ratio this.addEventListener('resize',(e)=>{ if(ratio != e.deviceRatio){ //因为devicePixelRatio会影响到点云大小,所以改变时计算下点云大小 viewer.scene.pointclouds.forEach(p => { p.changePointSize() }) } ratio = e.deviceRatio }) } */ { let pointDensity = ''; Object.defineProperty(Potree.settings , "pointDensity",{ get: function() { return pointDensity }, set: (density)=>{ if(density){ let config = Potree.config.pointDensity[density]; let pointBudget = config.pointBudget; if(density == 'magnifier'){//尽可能不变pointBudget,否则点云可能会闪烁,因点云被释放又加载,如SS-t-7DUfWAUZ3V pointBudget = Math.max(Potree.pointBudget, Potree.config.pointDensity['magnifier'].pointBudget); }else if(this.magnifier.visible){//放大镜打开时要保证最低点云数量(全景模式没点) pointBudget = Math.max(pointBudget, Potree.config.pointDensity['magnifier'].pointBudget); } viewer.setMinNodeSize(config.minNodeSize || Potree.config.minNodeSize); viewer.setPointBudget(pointBudget ); pointDensity = density; this.setPointLevels(); this.dispatchEvent('pointDensityChanged'); } } }); let UserPointDensity = ''; Object.defineProperty(Potree.settings , "UserPointDensity",{ get: function() { return UserPointDensity }, set: (density)=>{ if(UserPointDensity != density){ if(Potree.settings.displayMode == 'showPointCloud' && this.viewports.length != 4){//漫游模式和四屏时都有自己的pointDensity Potree.settings.pointDensity = density; } UserPointDensity = density; this.dispatchEvent('UserPointDensityChanged'); } } }); } { let cameraFar = Potree.settings.cameraFar; Object.defineProperty(Potree.settings , "cameraFar",{ get: function() { return cameraFar }, set: (far)=>{ if(far != cameraFar){ if(Potree.settings.displayMode != 'showPanos' && !this.fixCamFar){ this.mainViewport.camera.far = far; this.mainViewport.camera.updateProjectionMatrix(); } cameraFar = far; } } }); } /* this.reticule.addEventListener('update',(e)=>{ this.needRender = true if(this.mapViewer && this.mapViewer.attachedToViewer)this.mapViewer.needRender = true //分屏时mapViewer也有reticule }) */ this.reticule.addEventListener('update',(e)=>{ this.reticule.hoverViewport && (this.reticule.hoverViewport.needRender = true); this.lazyRenderViewports(); if(this.mapViewer && this.mapViewer.attachedToViewer && (this.mapViewer.viewports[0].needRender || this.needRender ) ) this.mapViewer.needRender = true; //分屏时mapViewer也有reticule }); this.addEventListener('pointcloud_changed',(e)=>{ if(this.screenshoting){ this.viewports.filter(e=>!e.noPointcloud).forEach(e =>e.needRender = true); } else this.lazyRenderViewports(); }); this.addEventListener('allLoaded', ()=>{ setTimeout(this.testPointcloudsMaxLevel.bind(this), 1000); //延迟一丢丢,等画面出现 this.scene.pointclouds.forEach(pointcloud=>{ pointcloud.addEventListener('isVisible',(e)=>{//是否显示该点的mesh(不显示也能走) //console.log('pointcloud isVisible', this.id, e.visible) if(e.reason != 'displayMode' && e.reason != 'overlinePass'){ this.updateModelBound('visibleChanged'); } this.dispatchEvent('pointcloud_changed'); }); pointcloud.material.addEventListener('material_property_changed',()=>{ this.dispatchEvent('pointcloud_changed'); }); }); window.addEventListener('unfocusPage',(e)=>{ console.log('unfocusPage',document.title); this.setDisplay(false); }); window.addEventListener('focusPage',(e)=>{ console.log('focusPage',document.title); this.setDisplay(true); }); }); if(Potree.settings.editType != 'pano' && Potree.settings.editType != 'merge'){ this.addEventListener('switchFloorplanSelect',(e)=>{//进入平面图设置后 切换选中的数据集 this.selectedFloorplan = e.pointcloud; //绝对显示 this.updateFpVisiDatasets(); let pointclouds; if(e.pointcloud){ pointclouds = [e.pointcloud]; }else if(this.fpVisiDatasets.length){ pointclouds = this.fpVisiDatasets; } pointclouds && this.mapViewer.fitToDatasets(pointclouds); }); this.modules.SiteModel.bus.addEventListener('FloorChange',(e)=>{ this.updateFpVisiDatasets(); this.updatePanosVisibles(e.currentFloor); //问:编辑空间模型时,需不需要改为显示当前选择的楼层。因为若所在楼层和选中的不一致,修改选中楼层的轮廓却改不了marker显示很奇怪,尤其刚好在一个建筑内时。 }); this.mapViewer.mapLayer.addEventListener('floorplanLoaded',()=>{ this.updateCadVisibles(this.fpVisiDatasets, true); //加载完成后重新更新下 }); /* this.modules.Clip.bus.addEventListener('updateSelectedDatasets',()=>{ this.updateFpVisiDatasets() }) */ } { let updated, zoomLevel; let update = (e)=>{ if(e.type == 'updateModelBound' || e.viewport == this.mainViewport && (e.changeInfo.positionChanged || zoomLevel != this.images360.zoomLevel)){ zoomLevel = this.images360.zoomLevel; //对updateMarkerVisibles有影响 //e.changeInfo.positionChanged && shelterHistory.clear() //清空 (e.type == 'updateModelBound' || e.changeInfo.positionChanged) && this.updateDatasetAt(); //更新所在数据集 if(Potree.settings.ifShowMarker && Potree.settings.editType != 'merge'){ Common. intervalTool.isWaiting('updateMarkerVisibles', ()=>{ if(!this.mainViewport.view.isFlying() ){ this.updateMarkerVisibles(); } },500); } } }; this.addEventListener('camera_changed', update); this.addEventListener('updateModelBound', update); this.addEventListener('showMarkerChanged',()=>{ this.updatePanosVisibles(this.modules.SiteModel.currentFloor); this.updateMarkerVisibles(); }); /* if(!Potree.Features.EXT_DEPTH.isSupported()){ this.images360.addEventListener('endChangeMode',(e)=>{ if(e.mode == 'showPanos'){ this.updateMarkerVisibles() } }) */ this.images360.addEventListener('getNeighbourAuto',(e)=>{ if(/* Potree.settings.displayMode == 'showPanos' && */e.panos.includes(this.images360.currentPano)){ Common.intervalTool.isWaiting('updateMarkerVisibles', ()=>{ this.updateMarkerVisibles(); },500); } }); /* } */ } { let interval; document.addEventListener('visibilitychange',(e)=>{ let v = !document.hidden; //console.warn('visibilitychange', v ) this.dispatchEvent({type:'pageVisible', v } ); if(this.screenshoting && !v){//截图过程中离开页面需要照常loop。但是尽量别离开页面,效果只能达到90% interval = setInterval(()=>{ //console.log('force loop', Date.now()) if(this.screenshoting){ //截图完成时如果还没回来就不loop this.loop(Date.now()); } },50); //实际>1000, 浏览器会放慢定时器,放慢多少不确定 }else if(v && interval){ clearInterval(interval); } }); } /* if(!Potree.isIframeChild){ window.winIndex = 0; let index = 1; this.addEventListener('createIframe',(e)=>{//创建了子页面 let child = e.window; child.winIndex = index ++; }) //不知道删除iframe时是否那些模型还在内存里,需要释放吗? 如果要需要加一个事件 } */ /* { let setInteract = ()=>{ this.interacted = true //标记这一帧用户有操作屏幕 } this.addEventListener('global_mousedown', setInteract) this.addEventListener('global_touchmove', setInteract) this.addEventListener('global_mousewheel', setInteract) } {//针对数据集偏离中心很远后产生的精度损失而抖动 this.addEventListener('camera_changed', e => { if(e.viewport == this.mainViewport && (e.changeInfo.positionChanged)){ if(Potree.settings.editType != 'merge' && Potree.settings.editType != 'pano'){ if(!this.mainViewport.view.isFlying() ){ let pos = this.mainViewport.view.position.clone() // this.mainViewport.camera.getWorldPosition(new THREE.Vector3) if(pos.lengthSq() > 25000000){ Common.intervalTool.isWaiting('moveWorldCenter', ()=>{ pos = this.mainViewport.view.position.clone() this.mainViewport.view.translateWorld(-pos.x, -pos.y, -pos.z) this.scene.scene.position.sub(pos) console.log('变位置', pos) this.scene.pointclouds.forEach(cloud=>{ Alignment.setMatrix(cloud) }) },500) } } } } }) }*/ } lazyRenderViewports(updateCount){ //本来要写this.needRender = true的, 但在分四个屏时防止太卡而排队render let viewports = this.viewports; if(viewports.length == 1 || this.needRender){ return this.needRender = true } const maxWaitTime = 200; let now = Date.now(); let maxRenderCount = 1; viewports.forEach(e=>{ if(now - e.lastRenderTime > maxWaitTime){ e.needRender = true; } }); let renderCount = viewports.filter(e=>e.needRender).length; //console.log('renderCount', renderCount) let list = viewports.filter(e=>!e.needRender).sort((a,b)=>a.lastRenderTime - b.lastRenderTime);//没有准备render的,按上次渲染时间排序,作为候补 if(renderCount < maxRenderCount){ //还有名额,补齐 list.slice(0, maxRenderCount - renderCount).forEach(e=>{ e.needRender = true; }); }else if(list[0] && now - list[0].lastRenderTime > maxWaitTime){//名额不足时,考虑候补队列第一个是否超时,超时的话也渲染 list[0].needRender = true; } } ifPointBlockedByIntersect(point , panoId, soon ){//点是否被遮挡 let ifShelter; let now = Date.now(); let extraPanoId = panoId != void 0; if(!this.shelterCount)return let history = shelterHistory.find(e=>e.point.equals(point)); let cameraPos = this.mainViewport.view.position.clone(); if(panoId == void 0){ if(this.images360.isAtPano(0.05)){ panoId = this.images360.currentPano.id; } } if(history){ if(panoId != void 0){ ifShelter = history.panos[panoId]; }else { if(history.notAtPano.cameraPos && history.notAtPano.cameraPos.equals(cameraPos)){ ifShelter = history.notAtPano.ifShelter; } } let index = shelterHistory.indexOf(history); //先取出,稍后放回 shelterHistory.splice(index, 1); }else {//新增 history = {point, panos:{}, notAtPano:{}}; const minCount = 100; if(shelterHistory.length > minCount){//去除最早的 let old; while(old = shelterHistory[0], now - old.lastTime > 1000){//因为不知热点个数,所以需要加上时间限制,超过时间才能删。 if(old == history || shelterHistory.length == minCount)break; shelterHistory.splice(0,1); //console.log('delete') } } } if(ifShelter == void 0){ delete history.waitCompute; if(this.mainViewport.view.isFlying()){ return useLastResult() } if(panoId != void 0){ let pano = this.images360.getPano(panoId); if((soon || this.shelterCount.byTex=0; i--){ let history = shelterHistory[i]; if(history.waitCompute){ if(history.waitCompute.panoId != void 0){ if(!history.waitCompute.forceGet && (history.waitCompute.panoId != this.images360.currentPano.id || !this.images360.isAtPano(0.1))){ delete history.waitCompute; //取消计算 }else { if(this.images360.currentPano.depthTex){ if(byTex >= maxTexCount)break byTex ++; let ifShelter = !!viewer.inputHandler.ifBlockedByIntersect({point:history.point, margin:Potree.config.shelterMargin, useDepthTex:true, viewport:this.mainViewport } ); history.panos[this.images360.currentPano.id] = ifShelter; history.ifShelter = ifShelter; delete history.waitCompute; //console.log('补1', history.point.toArray()) }else { if(this.images360.currentPano.pointcloud.hasDepthTex){ //先等待加载完深度图 }else { waitCloud.push(history); } } } }else { waitCloud.push(history); } } } let maxCloudCount; if(byTex < maxTexCount && waitCloud.length ){ maxCloudCount = this.lastFrameChanged ? Common.getBestCount('shelterMaxCloud', 0, 2, 4, 8 /* ,true */ ) : 5; let waitCloud2 = []; if(maxCloudCount){ for(let i=0; ie.point); let result = Common.batchHandling.getSlice('shelterByCloud', list, {maxUseCount:maxCloudCount,useEquals:true, stopWhenAllUsed:true} ); //iphonex稳定后大概在7-10。 //list.length>0 && console.log('list',list, maxCloudCount) result.list.forEach(e=>{ let history = waitCloud2.find(a=>a.point.equals(e)); let ifShelter = !!viewer.inputHandler.ifBlockedByIntersect({point:history.point, margin: Potree.config.shelterMargin , pickWindowSize:3, viewport:this.mainViewport} ); if(history.waitCompute.cameraPos){ history.notAtPano = {cameraPos: history.waitCompute.cameraPos , ifShelter }; }else { history.panos[this.images360.currentPano.id] = ifShelter; } history.ifShelter = ifShelter; byCloud++; //console.log('补2', history.point.toArray()) delete history.waitCompute; }); } } if(byTex || byCloud){ //console.log('shelterComputed',byTex,byCloud, maxTexCount, maxCloudCount) Common.intervalTool.isWaiting('shelterComputed', ()=>{ //console.log('shelterComputed update') this.dispatchEvent('shelterComputed'); },340); } } updateDatasetAt(force){//更新所在数据集 let fun = ()=>{ let currPos = viewer.mainViewport.view.position; //if(force || !currPos.equals(this.lastPos)){ //this.lastPos.copy(currPos) var at = this.scene.pointclouds.filter(e=> (e.visible || e.unvisibleReasons && e.unvisibleReasons.length == 1 && e.unvisibleReasons[0].reason == 'displayMode') && e.ifContainsPoint(currPos) ); if(Common.getDifferenceSet(at, this.atDatasets).length){ //console.log('atDatasets', at) this.atDatasets = at; if(Potree.settings.editType != 'pano' && Potree.settings.editType != 'merge'){ this.updateFpVisiDatasets(); if(!viewer.modules.SiteModel.currentFloor){ this.updatePanosVisibles(); //所在数据集的点位显示 } } this.dispatchEvent({type:'pointcloudAtChange',pointclouds:at}); } force = false; }; //} if(force)fun(); else Common.intervalTool.isWaiting('atWhichDataset', fun , 300); } updatePanosVisibles(currentFloor){//显示当前楼层的所有panos if(!Potree.settings.ifShowMarker)return let inEntity = currentFloor; //console.error('updatePanosVisibles', currentFloor) viewer.images360.panos.forEach(pano=>{ let visible = inEntity ? inEntity.panos.includes(pano) : this.atDatasets.some(e=>e.panos.includes(pano)); Potree.Utils.updateVisible(pano, 'buildingChange', visible, 2); }); this.dispatchEvent('content_changed'); } //注:非official的没有获取sitemodel的信息所以不执行楼层判断,marker显示不对是正常的 /* 2023.11.24 针对部分场景的楼层很矮,很容易就到楼层外了,且不能修改楼高。如SG-t-9NdCpxrUPLL#/,采取如下措施: 1 如果不在任何一楼层,但在某个数据集中,就显示该数据集所有的marker 2 如果当前楼层无,则显示上一次的currentFloor的marker (目前先使用方案1) */ updateMarkerVisibles(){//限制显示的marker个数,因镜头内marker多的时候可能会卡 if(!Potree.settings.ifShowMarker)return const minRadius = 8 * this.images360.zoomLevel, //当视线垂直于marker时的最小可见距离,此范围内可见的pano绝对可见 maxRadius = 50 * this.images360.zoomLevel, //当视线垂直于marker时的最大可见距离,此范围外绝对不可见 hopeCount = browser.isMobile() ? 8 : 15; //期望达到的真实可见的marker数 let sheltered = (pano)=>{ if(/* Potree.settings.displayMode == 'showPanos' && !Potree.Features.EXT_DEPTH.isSupported() && */this.images360.isAtPano() && !this.mainViewport.view.isFlying()){ return !this.images360.currentPano.neighbours.includes(pano) && this.images360.currentPano != pano //起初因不支持EXT_DEPTH时无法用depthTex遮住marker, 后为了减少绘制,都判断 } }; let panoMap = new Map; //先记录想要设置为可见的 let set = ()=>{//最后确定设置 let count = 0; viewer.images360.panos.forEach(pano=>{ let v = panoMap.get(pano).visible; v && count++; Potree.Utils.updateVisible(pano.marker, 'limitMarkerShow', v ); }); //console.log('updateMarkerVisibles marker显示个数', count) this.dispatchEvent('content_changed'); }; let isWithinDis = (pano,maxDis)=>{//是否marker到相机的距离 没有超出可视距离。可视距离考虑上倾斜角,倾斜越大可视距离越短 let camPos = viewer.mainViewport.camera.position; let o = panoMap.get(pano); o.dis = o.dis || camPos.distanceTo(pano.marker.position); o.sin = o.sin || Math.sqrt(Math.abs(camPos.z - pano.marker.position.z) / o.dis); //和地面夹角的sin。 按公式是不加Math.sqrt的,但是这样大马路上在贴近地面时算出的个数非常少,所以增大点…… return o.dis < maxDis * o.sin }; viewer.images360.panos.forEach(pano=>{//minRadius内的记录为可见 let o = {}; panoMap.set(pano, o); if(pano.visible && !sheltered(pano) && isWithinDis(pano, minRadius)){ o.visible = true; } }); //不超过hopeCount的话,可以直接确定设置 if(viewer.images360.panos.filter(pano=> panoMap.get(pano).visible ).length >= hopeCount)return set() //距离超过maxRadius就绝对不可见 let insideOutCirle = viewer.images360.panos.filter(pano=> pano.visible && !sheltered(pano) && isWithinDis(pano, maxRadius)); if(insideOutCirle.length <= hopeCount){ insideOutCirle.forEach(pano=>panoMap.get(pano).visible = true ); return set() } //数量超过hopeCount时,根据距离排序 insideOutCirle.sort((a,b)=>{return panoMap.get(a).dis - panoMap.get(b).dis }); let slice = insideOutCirle.slice(0,hopeCount); slice.forEach(pano=>panoMap.get(pano).visible = true ); set(); } updateFpVisiDatasets(){ let Clip = this.modules.Clip; let SiteModel = this.modules.SiteModel; let Alignment = this.modules.Alignment; var currentFloor = SiteModel.currentFloor; /* if(Clip.editing){//下载页面已经改为和普通时一样,根据位置判断 this.updateCadVisibles(Clip.selectedDatasets) }else */if(this.selectedFloorplan){//平面图设置中 或 地理设置 let pointclouds = [this.selectedFloorplan]; this.updateCadVisibles(pointclouds); }else if(SiteModel.editing || Alignment.editing){//只显示勾选的,也就是显示的点云的 let pointclouds = this.scene.pointclouds.filter(p => Potree.Utils.getObjVisiByReason(p,'datasetSelection') ); this.updateCadVisibles(pointclouds); //this.updatePanosVisibles(currentFloor/* , pointclouds */) }else { let pointclouds = currentFloor ? this.findPointcloudsAtFloor(currentFloor) : []; if(pointclouds.length == 0){ if(this.focusDatasets){ pointclouds = this.focusDatasets; } } if(pointclouds.length == 0){//如果当前不在任何楼层或楼层中无数据集,就用当前所在数据集 pointclouds = this.atDatasets; } this.updateCadVisibles(pointclouds); //this.updatePanosVisibles(currentFloor/* , pointclouds */) } } /* findPointcloudsAtFloor(entity){//找当前楼层需要显示哪些数据集。 //数据集的belongToEntity 在这个entity内(否则会出现点击数据集飞过去平面图却不显示)。or 如果数据集有漫游点的话,需要包含>20%的漫游点。 (防止重叠体积很大但其实一个漫游点都不包含) //重叠体积>50% 或 包含>50%的漫游点 const ratio1 = 0.2, ratio2 = 0.5, ratio3 = 0.95 var lowScores = [] var pointclouds = viewer.scene.pointclouds.filter(e=>{ let score = 0 if(e.belongToEntity && (e.belongToEntity == entity || e.belongToEntity.buildParent == entity)){//条件1 若该数据集挂载到该楼层 或 该数据集挂载到的房间属于该楼层(这样能显示该层所有房间) return true } if(e.panos.length){//条件2 var insidePanos = e.panos.filter(a=>entity.ifContainsPoint(a.position)); let panoCountRatio = insidePanos.length / e.panos.length if(panoCountRatio > ratio2)return true score += panoCountRatio } //条件3 let volume = entity.intersectPointcloudVolume(e); let volumeRatio = volume / entity.getVolume(true) //注:hole加入计算 if(volumeRatio > ratio3){ //ratio3要高一些,因为点云bounding可能很大,包含很多无点云的空间。即使整个数据集包含entity都不一定看起来在数据集中。(千万要防止两层楼都显示了) return true }else{ score += volumeRatio } lowScores.push({score, pointcloud:e}) }) if(pointclouds.length == 0){//从低分项挑一个出来。 lowScores.sort((a,b)=>{return a.score - b.score}) if(lowScores[0].score > 0.4){ pointclouds = [lowScores[0].pointcloud] } } return pointclouds } */ findPointcloudsAtFloor(entity){//找当前楼层需要显示哪些数据集。 //数据集的belongToEntity 在这个entity内(否则会出现点击数据集飞过去平面图却不显示)。or 如果数据集有漫游点的话,需要包含>20%的漫游点。 (防止重叠体积很大但其实一个漫游点都不包含) //重叠体积>50% 或 包含>50%的漫游点 const ratio1 = 0.2, ratio2 = 0.5, ratio3 = 0.95; var lowScores = []; var pointclouds = viewer.scene.pointclouds.filter(e=>{ let score = 0; if(e.belongToEntity && (e.belongToEntity == entity || e.belongToEntity.buildParent == entity)){//条件1 若该数据集挂载到该楼层 或 该数据集挂载到的房间属于该楼层(这样能显示该层所有房间) return true } if(e.panos.length){//条件2 var insidePanos = e.panos.filter(a=>entity.ifContainsPoint(a.position)); let panoCountRatio = insidePanos.length / e.panos.length; if(panoCountRatio > ratio2)return true score += panoCountRatio * 2; } //条件3 let coverHeightRatio = entity.coverPointcloudHeight(e,true);//重叠高度占楼层高度的比率。 if(coverHeightRatio < 0.2) return let {toEntity,toPointcloud} = entity.intersectPointcloudArea(e,true); //重叠面积占比 let coverAreaRatio = Math.max(toEntity,toPointcloud);//占数据集俯视面积的比率 和 占楼层的面积比率,挑一个大的。 (有的数据集被划分多个楼层,所以每层都应该显示该数据集的图。还有的数据集比较大,可能包含多个建筑,则看占楼层面积) if(coverAreaRatio < 0.2) return score += coverAreaRatio * coverHeightRatio; lowScores.push({score, pointcloud:e}); }); /* if(pointclouds.length == 0){//从低分项挑一个出来。 lowScores.sort((a,b)=>{return a.score - b.score}) if(lowScores[0].score > 0.4){ pointclouds = [lowScores[0].pointcloud] } } */ lowScores.forEach(e=>{//一个楼层里可以包含许多个数据集 调试SG-t-ds27ym7xzjJ if(e.score > 0.5){ pointclouds.push(e.pointcloud); } }); return pointclouds } updateCadVisibles(visiClouds, force){ //console.log('visiClouds',visiClouds) let oldVisi = this.fpVisiDatasets; var visiClouds = this.fpVisiDatasets = visiClouds; if(!force){ var difference = Common.getDifferenceSet(oldVisi , visiClouds); if(difference.length == 0)return } //console.log('visiClouds',visiClouds.map(e=>e.name)) viewer.scene.pointclouds.forEach(pointcloud=>{ var floorplan = viewer.mapViewer.mapLayer.getFloorplan(pointcloud.dataset_id); var visi = visiClouds.includes(pointcloud); if(floorplan){ Potree.Utils.updateVisible(floorplan.objectGroup, 'buildingChange', visi); }/* else if(!visi){ let changeVisi = (e)=>{ Potree.Utils.updateVisible(e.floorplan.objectGroup, 'buildingChange', this.fpVisiDatasets.includes(pointcloud)) viewer.mapViewer.mapLayer.removeEventListener('floorplanLoaded', changeVisi) console.log('updateCadVisibles加载后更改显示',e) } viewer.mapViewer.mapLayer.addEventListener('floorplanLoaded', changeVisi) } */ //已经添加了全局的 floorplanLoaded后会updateCadVisibles,这段就删了 }); viewer.mapViewer.mapLayer.needUpdate = true; //可能需要更新加载的level程度 viewer.mapViewer.needRender = true; //若上句不触发加载也要立即重新绘制 } //促使点云加载出最高级别 testPointcloudsMaxLevel(){ //所有点云都无需testMaxNodeLevel 就停止 let camera_changed, count = 0; let test = (e={})=>{ camera_changed = true; let camera = e.camera || this.scene.getActiveCamera(); Common.intervalTool.isWaiting('testPointcloudsMaxLevel', ()=>{ if(!camera_changed && count>50 || Potree.settings.displayMode == 'showPanos' || this.pauseTestMaxLevel )return //只有当camera_changed后才继续循环, 除了最开始几次需要连续加载下 camera_changed = false; count ++; //console.log('testPointcloudsMaxLevel中', camera.type /* count */) let oldCount = this.testMaxNodeCount; var success = true; viewer.scene.pointclouds.forEach(e=>{ var wait = e.testMaxNodeLevel(camera); if(wait){ success = false; } }); /* if(oldCount<=Potree.config.testNodeCount1 && this.testMaxNodeCount>Potree.config.testNodeCount1 ){//差不多等当前所在数据集nodeMaxLevel加载出来 viewer.scene.pointclouds.forEach(e=>{e.changePointSize()}) //重新更新一下大小。因之前用的是nodeMaxLevelPredict (防止刚开始因nodeMaxLevel没涨完,导致过大的点云突然出现 } */ if(!success)return true //没有全部加载完,继续循环 else { this.removeEventListener('camera_changed',test); console.log('testPointcloudsMaxLevel结束'); } }, count<10 ? 250 : 500); }; this.addEventListener('camera_changed',test); test(); /* 检验: viewer.scene.pointclouds.sort((a,b)=>a.nodeMaxLevelPredict.min - b.nodeMaxLevelPredict.min).forEach(e=>console.log(e.nodeMaxLevel, e.nodeMaxLevelPredict.min)) */ } setPointLevels(){ this.scene.pointclouds.forEach(e=>{ e.setPointLevel(); }); } onCrash(error){ console.error(error); $(this.renderArea).empty(); if ($(this.renderArea).find('#potree_failpage').length === 0) { let elFailPage = $(`

    Potree Encountered An Error

    This may happen if your browser or graphics card is not supported.
    We recommend to use Chrome or Firefox.

    Please also visit webglreport.com and check whether your system supports WebGL.

    If you are already using one of the recommended browsers and WebGL is enabled, consider filing an issue report at github,
    including your operating system, graphics card, browser and browser version, as well as the error message below.
    Please do not report errors on unsupported browsers.

    
    				
    			
    `); let elErrorMessage = elFailPage.find('#potree_error_console'); elErrorMessage.html(error.stack); $(this.renderArea).append(elFailPage); } throw error; } // ------------------------------------------------------------------------------------ // Viewer API // ------------------------------------------------------------------------------------ setScene (scene) { if (scene === this.scene) { return; } let oldScene = this.scene; this.scene = scene; this.dispatchEvent({ type: 'scene_changed', oldScene: oldScene, scene: scene }); { // Annotations $('.annotation').detach(); // for(let annotation of this.scene.annotations){ // this.renderArea.appendChild(annotation.domElement[0]); // } this.scene.annotations.traverse(annotation => { this.renderArea.appendChild(annotation.domElement[0]); }); if (!this.onAnnotationAdded) { this.onAnnotationAdded = e => { // console.log("annotation added: " + e.annotation.title); e.annotation.traverse(node => { $("#potree_annotation_container").append(node.domElement); //this.renderArea.appendChild(node.domElement[0]); node.scene = this.scene; }); }; } if (oldScene) { oldScene.annotations.removeEventListener('annotation_added', this.onAnnotationAdded); } this.scene.annotations.addEventListener('annotation_added', this.onAnnotationAdded); } }; setControls(controls/* , setSpeed */){ if (controls !== this.controls) { if (this.controls) { this.controls.setEnable(false); //this.inputHandler.removeInputListener(this.controls); this.controls.moveSpeed = this.moveSpeed; //记录 (因为orbit的radius很大,转为firstPerson时要缩小) } this.controls = controls; controls.moveSpeed && this.setMoveSpeed(controls.moveSpeed); //add this.controls.setEnable(true); //this.inputHandler.addInputListener(this.controls); } } getControls () { if(this.renderer.xr.isPresenting){ return this.vrControls; }else { return this.controls; } } getMinNodeSize () { return this.minNodeSize; }; setMinNodeSize (value) { if (this.minNodeSize !== value) { this.minNodeSize = value; this.dispatchEvent({'type': 'minnodesize_changed', 'viewer': this}); } }; getBackground () { return this.background; } setBackground(bg, src){ /* if (this.background === bg ) { return; } */ if(bg === "skybox"){ if(!src)src = Potree.resourcePath+'/textures/skybox/xingkong.jpg'; this.skybox = Utils.loadSkybox(src, this.skybox, ()=>{ this.background = bg; }); }else { this.background = bg; } this.backgroundOpacity = 1;//add this.dispatchEvent({'type': 'background_changed', 'viewer': this}); } setDescription (value) { this.description = value; $('#potree_description').html(value); //$('#potree_description').text(value); } getDescription(){ return this.description; } setShowBoundingBox (value) { if (this.showBoundingBox !== value) { this.showBoundingBox = value; this.dispatchEvent({'type': 'show_boundingbox_changed', 'viewer': this}); } }; getShowBoundingBox () { return this.showBoundingBox; }; setMoveSpeed (value) { if (this.getMoveSpeed() !== value) { this.mainViewport.setMoveSpeed(value); this.dispatchEvent({'type': 'move_speed_changed', 'viewer': this, 'speed': value}); } }; getMoveSpeed () { return this.mainViewport.moveSpeed; }; setWeightClassification (w) { for (let i = 0; i < this.scene.pointclouds.length; i++) { this.scene.pointclouds[i].material.weightClassification = w; this.dispatchEvent({'type': 'attribute_weights_changed' + i, 'viewer': this}); } }; setFreeze (value) { value = Boolean(value); if (this.freeze !== value) { this.freeze = value; this.dispatchEvent({'type': 'freeze_changed', 'viewer': this}); } }; getFreeze () { return this.freeze; }; setElevationGradientRepeat(value){ if(this.elevationGradientRepeat !== value){ this.elevationGradientRepeat = value; this.dispatchEvent({ type: "elevation_gradient_repeat_changed", viewer: this}); } } setPointBudget (value) { //pointBudget: 每次刷新显示点数量的最大值。 缓存中的点数量也跟此有关,但大于这个数值。 if (Potree.pointBudget !== value && value) { Potree.pointBudget = parseInt(value); this.dispatchEvent({'type': 'point_budget_changed', 'viewer': this}); } }; getPointBudget () { return Potree.pointBudget; }; setShowAnnotations (value) { if (this.showAnnotations !== value) { this.showAnnotations = value; this.dispatchEvent({'type': 'show_annotations_changed', 'viewer': this}); } } getShowAnnotations () { return this.showAnnotations; } setDEMCollisionsEnabled(value){ if(this.useDEMCollisions !== value){ this.useDEMCollisions = value; this.dispatchEvent({'type': 'use_demcollisions_changed', 'viewer': this}); }; }; getDEMCollisionsEnabled () { return this.useDEMCollisions; }; setEDLEnabled (value) { value = Boolean(value) && Features.SHADER_EDL.isSupported(); if (this.useEDL !== value) { this.useEDL = value; this.dispatchEvent({'type': 'use_edl_changed', 'viewer': this}); this.dispatchEvent('pointcloud_changed'); } }; getEDLEnabled () { return this.useEDL; }; setEDLRadius (value) { if (this.edlRadius !== value) { this.edlRadius = value; this.dispatchEvent({'type': 'edl_radius_changed', 'viewer': this}); this.dispatchEvent('pointcloud_changed'); } }; getEDLRadius () { return this.edlRadius; }; setEDLStrength (value) { if (this.edlStrength !== value) { this.edlStrength = value; this.dispatchEvent({'type': 'edl_strength_changed', 'viewer': this}); this.dispatchEvent('pointcloud_changed'); } }; getEDLStrength () { return this.edlStrength; }; setEDLOpacity (value) { if (this.edlOpacity !== value) { this.edlOpacity = value; this.dispatchEvent({'type': 'edl_opacity_changed', 'viewer': this}); this.dispatchEvent('pointcloud_changed'); } }; getEDLOpacity () { return this.edlOpacity; }; setFOV (value) { if (this.fov !== value) { let oldFov = this.fov; this.fov = value; this.scene.cameraP.fov = this.fov; //add this.scene.cameraP.updateProjectionMatrix(); //add this.dispatchEvent({'type': 'fov_changed', 'viewer': this, oldFov, fov:this.fov}); } }; getFOV () { return this.fov; }; disableAnnotations () { this.scene.annotations.traverse(annotation => { annotation.domElement.css('pointer-events', 'none'); // return annotation.visible; }); }; enableAnnotations () { this.scene.annotations.traverse(annotation => { annotation.domElement.css('pointer-events', 'auto'); // return annotation.visible; }); } setClassifications(classifications){ this.classifications = classifications; this.dispatchEvent({'type': 'classifications_changed', 'viewer': this}); } setClassificationVisibility (key, value) { if (!this.classifications[key]) { this.classifications[key] = {visible: value, name: 'no name'}; this.dispatchEvent({'type': 'classification_visibility_changed', 'viewer': this}); } else if (this.classifications[key].visible !== value) { this.classifications[key].visible = value; this.dispatchEvent({'type': 'classification_visibility_changed', 'viewer': this}); } } toggleAllClassificationsVisibility(){ let numVisible = 0; let numItems = 0; for(const key of Object.keys(this.classifications)){ if(this.classifications[key].visible){ numVisible++; } numItems++; } let visible = true; if(numVisible === numItems){ visible = false; } let somethingChanged = false; for(const key of Object.keys(this.classifications)){ if(this.classifications[key].visible !== visible){ this.classifications[key].visible = visible; somethingChanged = true; } } if(somethingChanged){ this.dispatchEvent({'type': 'classification_visibility_changed', 'viewer': this}); } } setFilterReturnNumberRange(from, to){ this.filterReturnNumberRange = [from, to]; this.dispatchEvent({'type': 'filter_return_number_range_changed', 'viewer': this}); } setFilterNumberOfReturnsRange(from, to){ this.filterNumberOfReturnsRange = [from, to]; this.dispatchEvent({'type': 'filter_number_of_returns_range_changed', 'viewer': this}); } setFilterGPSTimeRange(from, to){ this.filterGPSTimeRange = [from, to]; this.dispatchEvent({'type': 'filter_gps_time_range_changed', 'viewer': this}); } setFilterPointSourceIDRange(from, to){ this.filterPointSourceIDRange = [from, to]; this.dispatchEvent({'type': 'filter_point_source_id_range_changed', 'viewer': this}); } setLengthUnit (value) { switch (value) { case 'm': this.lengthUnit = LengthUnits.METER; this.lengthUnitDisplay = LengthUnits.METER; break; case 'ft': this.lengthUnit = LengthUnits.FEET; this.lengthUnitDisplay = LengthUnits.FEET; break; case 'in': this.lengthUnit = LengthUnits.INCH; this.lengthUnitDisplay = LengthUnits.INCH; break; } this.dispatchEvent({ 'type': 'length_unit_changed', 'viewer': this, value: value}); }; setLengthUnitAndDisplayUnit(lengthUnitValue, lengthUnitDisplayValue) { switch (lengthUnitValue) { case 'm': this.lengthUnit = LengthUnits.METER; break; case 'ft': this.lengthUnit = LengthUnits.FEET; break; case 'in': this.lengthUnit = LengthUnits.INCH; break; } switch (lengthUnitDisplayValue) { case 'm': this.lengthUnitDisplay = LengthUnits.METER; break; case 'ft': this.lengthUnitDisplay = LengthUnits.FEET; break; case 'in': this.lengthUnitDisplay = LengthUnits.INCH; break; } this.dispatchEvent({ 'type': 'length_unit_changed', 'viewer': this, value: lengthUnitValue }); }; zoomTo(node, factor, animationDuration = 0){ let view = this.mainViewport.view; let camera = this.scene.cameraP.clone(); camera.rotation.copy(this.scene.cameraP.rotation); camera.rotation.order = "ZXY"; camera.rotation.x = Math.PI / 2 + view.pitch; camera.rotation.z = view.yaw; camera.updateMatrix(); camera.updateMatrixWorld(); camera.zoomTo(node, factor); let bs; if (node.boundingSphere) { bs = node.boundingSphere; } else if (node.geometry && node.geometry.boundingSphere) { bs = node.geometry.boundingSphere; } else { bs = node.boundingBox.getBoundingSphere(new Sphere()); } bs = bs.clone().applyMatrix4(node.matrixWorld); let startPosition = view.position.clone(); let endPosition = camera.position.clone(); let startTarget = view.getPivot(); let endTarget = bs.center; let startRadius = view.radius; let endRadius = endPosition.distanceTo(endTarget); let easing = TWEEN.Easing.Quartic.Out; { // animate camera position let pos = startPosition.clone(); let tween = new TWEEN.Tween(pos).to(endPosition, animationDuration); tween.easing(easing); tween.onUpdate(() => { view.position.copy(pos); }); tween.start(); } { // animate camera target let target = startTarget.clone(); let tween = new TWEEN.Tween(target).to(endTarget, animationDuration); tween.easing(easing); tween.onUpdate(() => { view.lookAt(target); }); tween.onComplete(() => { view.lookAt(target); this.dispatchEvent({type: 'focusing_finished', target: this}); }); this.dispatchEvent({type: 'focusing_started', target: this}); tween.start(); } }; moveToGpsTimeVicinity(time){ const result = Potree.Utils.findClosestGpsTime(time, viewer); const box = result.node.pointcloud.deepestNodeAt(result.position).getBoundingBox(); const diameter = box.min.distanceTo(box.max); const camera = this.scene.getActiveCamera(); const offset = camera.getWorldDirection(new Vector3()).multiplyScalar(diameter); const newCamPos = result.position.clone().sub(offset); this.scene.view.position.copy(newCamPos); this.scene.view.lookAt(result.position); } showAbout () { $(function () { $('#about-panel').dialog(); }); }; getGpsTimeExtent(){ const range = [Infinity, -Infinity]; for(const pointcloud of this.scene.pointclouds){ const attributes = pointcloud.pcoGeometry.pointAttributes.attributes; const aGpsTime = attributes.find(a => a.name === "gps-time"); if(aGpsTime){ range[0] = Math.min(range[0], aGpsTime.range[0]); range[1] = Math.max(range[1], aGpsTime.range[1]); } } return range; } fitToScreen (factor = 1, animationDuration = 0) { let box = this.getBoundingBox(this.scene.pointclouds); let node = new Object3D(); node.boundingBox = box; this.zoomTo(node, factor, animationDuration); this.controls.stop(); }; toggleNavigationCube() { this.navigationCube.visible = !this.navigationCube.visible; /* this.viewports.push({ name:'navigationCube', left:0, bottom:0, width:0.2, }) */ } /* setView(pos, view) { if(!pos) return; switch(pos) { case "F": this.setFrontView(view); break; case "B": this.setBackView(view); break; case "L": this.setLeftView(view); break; case "R": this.setRightView(view); break; case "U": this.setTopView(view); break; case "D": this.setBottomView(view); break; } } */ setTopView(view){ view = view || this.scene.view; view.setCubeView("top"); this.fitToScreen(); }; setBottomView(){ this.scene.view.yaw = -Math.PI; this.scene.view.pitch = Math.PI / 2; this.fitToScreen(); }; setFrontView(view){ view = view || this.scene.view; view.yaw = 0; view.pitch = 0; this.fitToScreen(); }; setBackView(view){ view = view || this.scene.view; view.yaw = Math.PI; view.pitch = 0; this.fitToScreen(); }; setLeftView(){ this.scene.view.yaw = -Math.PI / 2; this.scene.view.pitch = 0; this.fitToScreen(); }; setRightView () { this.scene.view.yaw = Math.PI / 2; this.scene.view.pitch = 0; this.fitToScreen(); }; flipYZ () { this.isFlipYZ = !this.isFlipYZ; // TODO flipyz console.log('TODO'); } setCameraMode(mode){ this.scene.cameraMode = mode; viewer.mainViewport.camera = mode == CameraMode.PERSPECTIVE ? this.scene.cameraP : this.scene.cameraO; //改 for(let pointcloud of this.scene.pointclouds) { pointcloud.material.useOrthographicCamera = mode == CameraMode.ORTHOGRAPHIC; } } getProjection(){ const pointcloud = this.scene.pointclouds[0]; if(pointcloud){ return pointcloud.projection; }else { return null; } } async loadProject(url,done){ const response = await fetch(url); if(response.ok){ const text = await response.text(); const json = JSON5.parse(text); // const json = JSON.parse(text); if(json.type === "Potree"){ Potree.loadProject(viewer, json, done); } }else { console.warn("未能加载:"+url ); } } saveProject(){ return Potree.saveProject(this); } loadSettingsFromURL(){ if(Utils.getParameterByName("pointSize")){ this.setPointSize(parseFloat(Utils.getParameterByName("pointSize"))); } if(Utils.getParameterByName("FOV")){ this.setFOV(parseFloat(Utils.getParameterByName("FOV"))); } if(Utils.getParameterByName("opacity")){ this.setOpacity(parseFloat(Utils.getParameterByName("opacity"))); } if(Utils.getParameterByName("edlEnabled")){ let enabled = Utils.getParameterByName("edlEnabled") === "true"; this.setEDLEnabled(enabled); } if (Utils.getParameterByName('edlRadius')) { this.setEDLRadius(parseFloat(Utils.getParameterByName('edlRadius'))); } if (Utils.getParameterByName('edlStrength')) { this.setEDLStrength(parseFloat(Utils.getParameterByName('edlStrength'))); } if (Utils.getParameterByName('pointBudget')) { this.setPointBudget(parseFloat(Utils.getParameterByName('pointBudget'))); } if (Utils.getParameterByName('showBoundingBox')) { let enabled = Utils.getParameterByName('showBoundingBox') === 'true'; if (enabled) { this.setShowBoundingBox(true); } else { this.setShowBoundingBox(false); } } if (Utils.getParameterByName('material')) { let material = Utils.getParameterByName('material'); this.setMaterial(material); } if (Utils.getParameterByName('pointSizing')) { let sizing = Utils.getParameterByName('pointSizing'); this.setPointSizing(sizing); } if (Utils.getParameterByName('quality')) { let quality = Utils.getParameterByName('quality'); this.setQuality(quality); } if (Utils.getParameterByName('position')) { let value = Utils.getParameterByName('position'); value = value.replace('[', '').replace(']', ''); let tokens = value.split(';'); let x = parseFloat(tokens[0]); let y = parseFloat(tokens[1]); let z = parseFloat(tokens[2]); this.scene.view.position.set(x, y, z); } if (Utils.getParameterByName('target')) { let value = Utils.getParameterByName('target'); value = value.replace('[', '').replace(']', ''); let tokens = value.split(';'); let x = parseFloat(tokens[0]); let y = parseFloat(tokens[1]); let z = parseFloat(tokens[2]); this.scene.view.lookAt(new Vector3(x, y, z)); } if (Utils.getParameterByName('background')) { let value = Utils.getParameterByName('background'); this.setBackground(value); } // if(Utils.getParameterByName("elevationRange")){ // let value = Utils.getParameterByName("elevationRange"); // value = value.replace("[", "").replace("]", ""); // let tokens = value.split(";"); // let x = parseFloat(tokens[0]); // let y = parseFloat(tokens[1]); // // this.setElevationRange(x, y); // //this.scene.view.target.set(x, y, z); // } }; // ------------------------------------------------------------------------------------ // Viewer Internals // ------------------------------------------------------------------------------------ createControls () { { // create FIRST PERSON CONTROLS this.fpControls = new FirstPersonControls(this, this.mainViewport); this.fpControls.enabled = false; this.fpControls.addEventListener('start', this.disableAnnotations.bind(this)); this.fpControls.addEventListener('end', this.enableAnnotations.bind(this)); /* this.addEventListener("loadPointCloudDone", ()=>{ let boundPlane = new THREE.Box3() boundPlane.expandByPoint(this.bound.boundingBox.min.clone())//最低高度为bound的最低 boundPlane.expandByPoint(this.bound.boundingBox.max.clone().setZ(this.bound.center.z))//最高高度为bound的中心高度 FirstPersonControls.boundPlane = boundPlane FirstPersonControls.standardSpeed = THREE.Math.clamp( Math.sqrt(this.bound.boundSize.length() )/ 100 , 0.02,0.5); //在这个boundPlane中的速度 }) */ } // { // create GEO CONTROLS // this.geoControls = new GeoControls(this.scene.camera, this.renderer.domElement); // this.geoControls.enabled = false; // this.geoControls.addEventListener("start", this.disableAnnotations.bind(this)); // this.geoControls.addEventListener("end", this.enableAnnotations.bind(this)); // this.geoControls.addEventListener("move_speed_changed", (event) => { // this.setMoveSpeed(this.geoControls.moveSpeed); // }); // } { // create ORBIT CONTROLS this.orbitControls = new OrbitControls(this); this.orbitControls.enabled = false; this.orbitControls.addEventListener('start', this.disableAnnotations.bind(this)); this.orbitControls.addEventListener('end', this.enableAnnotations.bind(this)); } /* { // create EARTH CONTROLS this.earthControls = new EarthControls(this); this.earthControls.enabled = false; this.earthControls.addEventListener('start', this.disableAnnotations.bind(this)); this.earthControls.addEventListener('end', this.enableAnnotations.bind(this)); } { // create DEVICE ORIENTATION CONTROLS this.deviceControls = new DeviceOrientationControls(this); this.deviceControls.enabled = false; this.deviceControls.addEventListener('start', this.disableAnnotations.bind(this)); this.deviceControls.addEventListener('end', this.enableAnnotations.bind(this)); } */ /* { // create VR CONTROLS this.vrControls = new VRControls(this); this.vrControls.enabled = false; this.vrControls.addEventListener('start', this.disableAnnotations.bind(this)); this.vrControls.addEventListener('end', this.enableAnnotations.bind(this)); } */ }; toggleSidebar () { let renderArea = $('#potree_render_area'); let isVisible = renderArea.css('left') !== '0px'; if (isVisible) { renderArea.css('left', '0px'); } else { renderArea.css('left', '300px'); } }; toggleMap () { // let map = $('#potree_map'); // map.toggle(100); if (this.mapView) { this.mapView.toggle(); } }; onGUILoaded(callback){ if(this.guiLoaded){ callback(); }else { this.guiLoadTasks.push(callback); } } promiseGuiLoaded(){ return new Promise( resolve => { if(this.guiLoaded){ resolve(); }else { this.guiLoadTasks.push(resolve); } }); } loadGUI(callback){ if(callback){ this.onGUILoaded(callback); } let viewer = this; let sidebarContainer = $('#potree_sidebar_container'); sidebarContainer.load(new URL(Potree.scriptPath + '/' + (Potree.settings.sidebar || 'sidebar1.html')).href, () => { sidebarContainer.css('width', '300px'); sidebarContainer.css('height', '100%'); let imgMenuToggle = document.createElement('img'); imgMenuToggle.src = new URL(Potree.resourcePath + '/icons/menu_button.svg').href; imgMenuToggle.onclick = this.toggleSidebar; imgMenuToggle.classList.add('potree_menu_toggle'); let imgMapToggle = document.createElement('img'); imgMapToggle.src = new URL(Potree.resourcePath + '/icons/map_icon.png').href; imgMapToggle.style.display = 'none'; imgMapToggle.onclick = e => { this.toggleMap(); }; imgMapToggle.id = 'potree_map_toggle'; let elButtons = $("#potree_quick_buttons").get(0); elButtons.append(imgMenuToggle); elButtons.append(imgMapToggle); VRButton.createButton(this.renderer).then(vrButton => { if(vrButton == null){ console.log("VR not supported or active."); return; } this.renderer.xr.enabled = true; let element = vrButton.element; element.style.position = ""; element.style.bottom = ""; element.style.left = ""; element.style.margin = "4px"; element.style.fontSize = "100%"; element.style.width = "2.5em"; element.style.height = "2.5em"; element.style.padding = "0"; element.style.textShadow = "black 2px 2px 2px"; element.style.display = "block"; elButtons.append(element); vrButton.onStart(() => { this.dispatchEvent({type: "vr_start"}); }); vrButton.onEnd(() => { this.dispatchEvent({type: "vr_end"}); }); }); /* this.mapView = new MapView(this); this.mapView.init(); */ i18n.init({ lng: 'en', resGetPath: Potree.resourcePath + '/lang/__lng__/__ns__.json', preload: ['en', 'fr', 'de', 'jp', 'se', 'es', 'zh'], getAsync: true, debug: false }, function (t) { // Start translation once everything is loaded $('body').i18n(); }); $(() => { //initSidebar(this); let sidebar = new Sidebar(this); sidebar.init(); this.sidebar = sidebar; //if (callback) { // $(callback); //} let elProfile = $('
    ').load(new URL(Potree.scriptPath + '/profile.html').href, () => { $(document.body).append(elProfile.children()); this.profileWindow = new ProfileWindow(this); this.profileWindowController = new ProfileWindowController(this); $('#profile_window').draggable({ handle: $('#profile_titlebar'), containment: $(document.body) }); $('#profile_window').resizable({ containment: $(document.body), handles: 'n, e, s, w' }); $(() => { this.guiLoaded = true; for(let task of this.guiLoadTasks){ task(); } }); }); }); }); return this.promiseGuiLoaded(); } setLanguage (lang) { i18n.setLng(lang); $('body').i18n(); } setServer (server) { this.server = server; } initDragAndDrop(){ function allowDrag(e) { e.dataTransfer.dropEffect = 'copy'; e.preventDefault(); } let dropHandler = async (event) => { console.log(event); event.preventDefault(); for(const item of event.dataTransfer.items){ console.log(item); if(item.kind !== "file"){ continue; } const file = item.getAsFile(); const isJson = file.name.toLowerCase().endsWith(".json"); const isGeoPackage = file.name.toLowerCase().endsWith(".gpkg"); if(isJson){ try{ const text = await file.text(); const json = JSON.parse(text); if(json.type === "Potree"){ Potree.loadProject(viewer, json); } }catch(e){ console.error("failed to parse the dropped file as JSON"); console.error(e); } }else if(isGeoPackage){ const hasPointcloud = viewer.scene.pointclouds.length > 0; if(!hasPointcloud){ let msg = "At least one point cloud is needed that specifies the "; msg += "coordinate reference system before loading vector data."; console.error(msg); }else { proj4.defs("WGS84", "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"); proj4.defs("pointcloud", this.getProjection()); let transform = proj4("WGS84", "pointcloud"); const buffer = await file.arrayBuffer(); const params = { transform: transform, source: file.name, }; const geo = await Potree.GeoPackageLoader.loadBuffer(buffer, params); viewer.scene.addGeopackage(geo); } } } }; $("body")[0].addEventListener("dragenter", allowDrag); $("body")[0].addEventListener("dragover", allowDrag); $("body")[0].addEventListener("drop", dropHandler); } updateAnnotations () { if(!this.visibleAnnotations){ this.visibleAnnotations = new Set(); } this.scene.annotations.updateBounds(); this.scene.cameraP.updateMatrixWorld(); this.scene.cameraO.updateMatrixWorld(); let distances = []; let renderAreaSize = this.renderer.getSize(new Vector2()); let viewer = this; let visibleNow = []; this.scene.annotations.traverse(annotation => { if (annotation === this.scene.annotations) { return true; } if (!annotation.visible) { return false; } annotation.scene = this.scene; let element = annotation.domElement; let position = annotation.position.clone(); position.add(annotation.offset); if (!position) { position = annotation.boundingBox.getCenter(new Vector3()); } let distance = viewer.scene.cameraP.position.distanceTo(position); let radius = annotation.boundingBox.getBoundingSphere(new Sphere()).radius; let screenPos = new Vector3(); let screenSize = 0; { // SCREEN POS screenPos.copy(position).project(this.scene.getActiveCamera()); screenPos.x = renderAreaSize.x * (screenPos.x + 1) / 2; screenPos.y = renderAreaSize.y * (1 - (screenPos.y + 1) / 2); // SCREEN SIZE if(viewer.scene.cameraMode == CameraMode.PERSPECTIVE) { let fov = Math.PI * viewer.scene.cameraP.fov / 180; let slope = Math.tan(fov / 2.0); let projFactor = 0.5 * renderAreaSize.y / (slope * distance); screenSize = radius * projFactor; } else { screenSize = Utils.projectedRadiusOrtho(radius, viewer.scene.cameraO.projectionMatrix, renderAreaSize.x, renderAreaSize.y); } } element.css("left", screenPos.x + "px"); element.css("top", screenPos.y + "px"); //element.css("display", "block"); let zIndex = 10000000 - distance * (10000000 / this.scene.cameraP.far); if(annotation.descriptionVisible){ zIndex += 10000000; } element.css("z-index", parseInt(zIndex)); if(annotation.children.length > 0){ let expand = screenSize > annotation.collapseThreshold || annotation.boundingBox.containsPoint(this.scene.getActiveCamera().position); annotation.expand = expand; if (!expand) { //annotation.display = (screenPos.z >= -1 && screenPos.z <= 1); let inFrustum = (screenPos.z >= -1 && screenPos.z <= 1); if(inFrustum){ visibleNow.push(annotation); } } return expand; } else { //annotation.display = (screenPos.z >= -1 && screenPos.z <= 1); let inFrustum = (screenPos.z >= -1 && screenPos.z <= 1); if(inFrustum){ visibleNow.push(annotation); } } }); let notVisibleAnymore = new Set(this.visibleAnnotations); for(let annotation of visibleNow){ annotation.display = true; notVisibleAnymore.delete(annotation); } this.visibleAnnotations = visibleNow; for(let annotation of notVisibleAnymore){ annotation.display = false; } } updateMaterialDefaults(pointcloud){ // PROBLEM STATEMENT: // * [min, max] of intensity, source id, etc. are computed as point clouds are loaded // * the point cloud material won't know the range it should use until some data is loaded // * users can modify the range at runtime, but sensible default ranges should be // applied even if no GUI is present // * display ranges shouldn't suddenly change even if the actual range changes over time. // e.g. the root node has intensity range [1, 478]. One of the descendants increases range to // [0, 2047]. We should not automatically change to the new range because that would result // in sudden and drastic changes of brightness. We should adjust the min/max of the sidebar slider. const material = pointcloud.material; const attIntensity = pointcloud.getAttribute("intensity"); if(attIntensity != null && material.intensityRange[0] === Infinity){ material.intensityRange = [...attIntensity.range]; } // const attIntensity = pointcloud.getAttribute("intensity"); // if(attIntensity && material.intensityRange[0] === Infinity){ // material.intensityRange = [...attIntensity.range]; // } // let attributes = pointcloud.getAttributes(); // for(let attribute of attributes.attributes){ // if(attribute.range){ // let range = [...attribute.range]; // material.computedRange.set(attribute.name, range); // //material.setRange(attribute.name, range); // } // } } update(delta, timestamp){ viewer.addTimeMark('update','start'); TWEEN.update(timestamp); transitions.update(delta);//写在开头,因为这时候最为固定,计时准确 this.dispatchEvent({ type: 'update_start', delta: delta, timestamp: timestamp}); this.updateScreenSize(); //判断是否改变canvas大小 const scene = this.scene; const camera = scene.getActiveCamera(); const visiblePointClouds = this.scene.pointclouds.filter(pc => pc.visible); Potree.pointLoadLimit = Potree.pointBudget * 2; /* const lTarget = camera.position.clone().add(camera.getWorldDirection(new THREE.Vector3()).multiplyScalar(1000)); this.scene.directionalLight.position.copy(camera.position); this.scene.directionalLight.lookAt(lTarget); */ for (let pointcloud of visiblePointClouds) { pointcloud.showBoundingBox = this.showBoundingBox; pointcloud.generateDEM = this.generateDEM; pointcloud.minimumNodePixelSize = this.minNodeSize; let material = pointcloud.material; material.uniforms.uFilterReturnNumberRange.value = this.filterReturnNumberRange; material.uniforms.uFilterNumberOfReturnsRange.value = this.filterNumberOfReturnsRange; material.uniforms.uFilterGPSTimeClipRange.value = this.filterGPSTimeRange; material.uniforms.uFilterPointSourceIDClipRange.value = this.filterPointSourceIDRange; material.classification = this.classifications; material.recomputeClassification(); this.updateMaterialDefaults(pointcloud); } { if(this.showBoundingBox){ let bbRoot = this.scene.scene.getObjectByName("potree_bounding_box_root"); if(!bbRoot){ let node = new Object3D(); node.name = "potree_bounding_box_root"; this.scene.scene.add(node); bbRoot = node; } let visibleBoxes = []; for(let pointcloud of this.scene.pointclouds){ for(let node of pointcloud.visibleNodes.filter(vn => vn.boundingBoxNode !== undefined)){ let box = node.boundingBoxNode; visibleBoxes.push(box); } } bbRoot.children = visibleBoxes; } } if(this.boundNeedUpdate)this.updateModelBound(); //add this.scene.cameraP.fov = this.fov; let controls = this.getControls(); if (controls === this.deviceControls) { this.controls.setScene(scene); this.controls.update(delta); this.scene.cameraP.position.copy(scene.view.position); this.scene.cameraO.position.copy(scene.view.position); } else if (controls !== null) { controls.setScene(scene); controls.update(delta); //更新camera this.viewports.forEach(viewport=>{ if(!viewport.active)return viewport.view.applyToCamera(viewport.camera); }); } this.lastFrameChanged = this.cameraChanged();//判断camera画面是否改变 { // update clip boxes let boxes = []; // volumes with clipping enabled boxes.push(...this.scene.volumes.filter(v => (v.clip && v instanceof BoxVolume$1))); // profile segments for(let profile of this.scene.profiles){ boxes.push(...profile.boxes); } // Needed for .getInverse(), pre-empt a determinant of 0, see #815 / #816 let degenerate = (box) => box.matrixWorld.determinant() !== 0 && box.clip; let clipBoxes = boxes.filter(degenerate).map( box => { box.updateMatrixWorld(); let boxInverse = box.matrixWorld.clone().invert(); //let boxPosition = box.getWorldPosition(new THREE.Vector3()); return {box: box, inverse: boxInverse/* , position: boxPosition */}; }); //改 let bigClipInBox = clipBoxes.find(e=>e.box.clipTask == ClipTask.SHOW_INSIDE_Big && !e.box.highlight);//裁剪下载 when this.modules.Clip.editing let clipBoxes_in = clipBoxes.filter(e=>e.box.clipTask == ClipTask.SHOW_INSIDE && !e.box.highlight); let clipBoxes_out = clipBoxes.filter(e=>e.box.clipTask == ClipTask.SHOW_OUTSIDE && !e.box.highlight); let highlightBoxes = clipBoxes.filter(e=>e.box.highlight ); //let prismPolygons = this.modules.volumeComputer && this.modules.volumeComputer.entered ? viewer.scene.measurements.filter(e=>(e.measureType == 'MulDistance Ring') && !e.isNew) : [] let prismPolygons = this.modules.volumeComputer ? this.modules.volumeComputer.prisms.filter(e=>!e.isNew && e.visible && !e.dontHighlight && (e.baseModel || !e.modelHaventLoad)) : []; // set clip volumes in material for(let pointcloud of visiblePointClouds){ pointcloud.material.setClipBoxes(bigClipInBox, clipBoxes_in, clipBoxes_out, highlightBoxes, prismPolygons); } } { for(let pointcloud of visiblePointClouds){ pointcloud.material.elevationGradientRepeat = this.elevationGradientRepeat; } } { // update navigation cube this.navigationCube.update(camera.rotation); } this.updateAnnotations(); if(this.mapView){ this.mapView.update(delta); if(this.mapView.sceneProjection){ $( "#potree_map_toggle" ).css("display", "block"); } } this.transformationTool.update(); if(Potree.settings.editType != 'pano' && Potree.settings.editType != 'merge'){ this.modules.ParticleEditor.update(delta); } this.mapViewer && this.mapViewer.update(delta); //地图更新 this.dispatchEvent({ type: 'update', delta: delta, timestamp: timestamp}); //在有sidebar时耗高cpu,占本update函数80% viewer.addTimeMark('update','end'); //add ------ this.reticule.updateVisible(); } updateViewPointcloud(camera, areaSize, isViewport){ let result = Potree.updatePointClouds(this.scene.pointclouds, camera, areaSize ); //if(isViewport)return const tStart = performance.now(); const campos = camera.position; let closestImage = Infinity; for(const images of this.scene.orientedImages){ for(const image of images.images){ const distance = image.mesh.position.distanceTo(campos); closestImage = Math.min(closestImage, distance); } } //const tEnd = performance.now(); //改:不根据点云修改视野near far var near = camera.near, far = camera.far; if(!camera.limitFar && result.lowestSpacing !== Infinity){ //let near = result.lowestSpacing * 10.0; let far = -this.getBoundingBox().applyMatrix4(camera.matrixWorldInverse).min.z; far = Math.max(far * 1.5, 10000); //near = Math.min(100.0, Math.max(0.01, near)); //near = Math.min(near, closestImage); far = Math.max(far, near + 10000); /* if(near === Infinity){ near = 0.1; } */ //camera.near = near; //为了其他物体的显示,不修改near camera.far = far; } /* if(this.scene.cameraMode == CameraMode.ORTHOGRAPHIC) {//??? camera.near = -camera.far; } */ if(/* near != camera.near || */far != camera.far){ camera.updateProjectionMatrix(); } //注:pointcloud.visibleNodes会随着near far自动更新 } getPRenderer(){ if(this.useHQ){ if (!this.hqRenderer) { this.hqRenderer = new HQSplatRenderer(this); } this.hqRenderer.useEDL = this.useEDL; return this.hqRenderer; }else { /* if (this.useEDL && Features.SHADER_EDL.isSupported()) { if (!this.edlRenderer) { this.edlRenderer = new EDLRenderer(this); } return this.edlRenderer; } else { if (!this.potreeRenderer) { this.potreeRenderer = new PotreeRenderer(this); } return this.potreeRenderer; } */ if (!this.edlRenderer) { this.edlRenderer = new EDLRenderer(this); } return this.edlRenderer; } } renderVR(){//渲染部分没改完 let renderer = this.renderer; renderer.setClearColor(0x550000, 0); renderer.clear(); let xr = renderer.xr; let dbg = new PerspectiveCamera(); let xrCameras = xr.getCamera(dbg); if(xrCameras.cameras.length !== 2){ return; } let makeCam = this.vrControls.getCamera.bind(this.vrControls); { // clear framebuffer if(viewer.background === "skybox"){ renderer.setClearColor(0xff0000, 1); }else if(viewer.background === "gradient"){ renderer.setClearColor(0x112233, 1); }else if(viewer.background === "black"){ renderer.setClearColor(0x000000, 1); }else if(viewer.background === "white"){ renderer.setClearColor(0xFFFFFF, 1); }else { renderer.setClearColor(0x000000, 0); } renderer.clear(); } // render background if(this.background === "skybox"){ let {skybox} = this; let cam = makeCam(); skybox.camera.rotation.copy(cam.rotation); skybox.camera.fov = cam.fov; skybox.camera.aspect = cam.aspect; // let dbg = new THREE.Object3D(); let dbg = skybox.parent; // dbg.up.set(0, 0, 1); dbg.rotation.x = Math.PI / 2; // skybox.camera.parent = dbg; // dbg.children.push(skybox.camera); dbg.updateMatrix(); dbg.updateMatrixWorld(); skybox.camera.updateMatrix(); skybox.camera.updateMatrixWorld(); skybox.camera.updateProjectionMatrix(); renderer.render(skybox.scene, skybox.camera); // renderer.render(skybox.scene, cam); }else if(this.background === "gradient"){ // renderer.render(this.scene.sceneBG, this.scene.cameraBG); } this.renderer.xr.getSession().updateRenderState({ depthNear: 0.1, depthFar: 10000 }); let cam = null; let view = null; { // render world scene cam = makeCam(); cam.position.z -= 0.8 * cam.scale.x; cam.parent = null; // cam.near = 0.05; cam.near = viewer.scene.getActiveCamera().near; cam.far = viewer.scene.getActiveCamera().far; cam.updateMatrix(); cam.updateMatrixWorld(); this.scene.scene.updateMatrix(); this.scene.scene.updateMatrixWorld(); this.scene.scene.matrixAutoUpdate = false; let camWorld = cam.matrixWorld.clone(); view = camWorld.clone().invert(); this.scene.scene.matrix.copy(view); this.scene.scene.matrixWorld.copy(view); cam.matrix.identity(); cam.matrixWorld.identity(); cam.matrixWorldInverse.identity(); renderer.render(this.scene.scene, cam); this.scene.scene.matrixWorld.identity(); } for(let pointcloud of this.scene.pointclouds){ let viewport = xrCameras.cameras[0].viewport; pointcloud.material.useEDL = false; pointcloud.screenHeight = viewport.height; pointcloud.screenWidth = viewport.width; // automatically switch to paraboloids because they cause far less flickering in VR, // when point sizes are larger than around 2 pixels // if(Features.SHADER_INTERPOLATION.isSupported()){ // pointcloud.material.shape = Potree.PointShape.PARABOLOID; // } } // render point clouds for(let xrCamera of xrCameras.cameras){ let v = xrCamera.viewport; renderer.setViewport(v.x, v.y, v.width, v.height); // xrCamera.fov = 90; { // estimate VR fov let proj = xrCamera.projectionMatrix; let inv = proj.clone().invert(); let p1 = new Vector4(0, 1, -1, 1).applyMatrix4(inv); let rad = p1.y; let fov = 180 * (rad / Math.PI); xrCamera.fov = fov; } for(let pointcloud of this.scene.pointclouds){ const {material} = pointcloud; material.useEDL = false; } let vrWorld = view.clone().invert(); vrWorld.multiply(xrCamera.matrixWorld); let vrView = vrWorld.clone().invert(); this.pRenderer.render(this.scene.scenePointCloud, xrCamera, null, { viewOverride: vrView, }); } { // render VR scene let cam = makeCam(); cam.parent = null; renderer.render(this.sceneVR, cam); } renderer.resetState(); } clear(params={}){ let background = params.background || this.background; let backgroundOpacity = params.backgroundOpacity == void 0 ? this.backgroundOpacity : params.backgroundOpacity;//如果想完全透明,只需要backgroundOpacity为0 let renderer = this.renderer; //let gl = renderer.getContext() if(background instanceof Color){ //add renderer.setClearColor(background, backgroundOpacity); }else if(background === "skybox"){ renderer.setClearColor(0x000000, backgroundOpacity); } else if (background === 'gradient') { renderer.setClearColor(0x000000, backgroundOpacity); } else if (background === 'black') { renderer.setClearColor(0x000000, 1); } else if (background === 'white') { renderer.setClearColor(0xFFFFFF, 1); } else { renderer.setClearColor(background, backgroundOpacity); } params.target || renderer.clear(); } getBuffer(viewport){//根据不同viewport返回rtEDL的texture var buffer = this.buffers.get(viewport); if(!buffer){ buffer = new WebGLRenderTarget(viewport.resolution.x, viewport.resolution.y, { minFilter: NearestFilter, magFilter: NearestFilter, format: RGBAFormat, type: FloatType }); this.buffers.set(viewport, rtEDL); } return buffer } async renderDefault(params_={}){ if(!this.visible ) return let pRenderer = this.getPRenderer(); let viewports = params_.viewports || this.viewports; viewer.addTimeMark('renderDefault','start'); //console.log('render', viewports.map(e=>e.name).join(',')) let renderSize; if(params_.target){ renderSize = new Vector2(params_.target.width, params_.target.height); //是画布大小 //可能需要viewer.setSize }else { renderSize = this.renderer.getSize(new Vector2()); //是client大小 } ///let needsResize = viewports.length > 1 || params_.resize //去掉原因:因为不需要渲染的viewport不在此中所以无法判断几个viewport for(let i=0; i{ if(object.material){ Potree.Utils.updateVisible(object, 'renderOpa', (params.renderBeforeCloud && (object.material.opacity<1 || !object.material.depthTest) || (!params.renderBeforeCloud) && (object.material.opacity==1 && object.material.depthTest))? false:true); //点云之前渲染的话隐藏半透明的, 点云之后渲染的话隐藏不透明的。 depthTest==false的也最后渲染 } });//ground的材质中opacity为1,所以被当做不透明了 } viewer.dispatchEvent({type: "render.begin2" , name:'scene', viewport:params.viewport }); renderer.render(this.scene.scene, camera); if('renderBeforeCloud' in params){ this.scene.scene.traverse((object)=>{ if(object.material){ Potree.Utils.updateVisible(object, 'renderOpa', true); //恢复 } }); } } this.dispatchEvent({type: "render.pass.scene", viewer: viewer}); } renderOverlay2(params){ let renderer = params.renderer || this.renderer; let camera = params.camera ? params.camera : this.scene.getActiveCamera(); //清除深度 !!!! renderer.clearDepth(); if(!params.magnifier){ //测量线 this.dispatchEvent({type: "render.pass.perspective_overlay", camera, screenshot:params.screenshot,viewport:params.viewport, renderer}); if(!params.screenshot && params.viewport.name != "mapViewport" ){ Potree.Utils.setCameraLayers(camera, ['magnifier']); //magnifier 遮住测量线 renderer.render(this.scene.scene, camera); } } if(params.viewport.name != "mapViewport" ) { Potree.Utils.setCameraLayers(camera, ['volume','transformationTool']); //viewer.dispatchEvent({type: "render.begin2" , name:'clip&trans',viewport:params.viewport }) renderer.render(this.clippingTool.sceneVolume, camera); //official 可以删 renderer.render(this.transformationTool.scene, camera); } } setLimitFar(state){//切换是否limitFar viewer.mainViewport.camera.limitFar = !!state; if(state){ viewer.mainViewport.camera.near = 0.02; viewer.mainViewport.camera.far = Potree.settings.displayMode == 'showPanos' ? viewer.farWhenShowPano : Potree.settings.cameraFar; viewer.mainViewport.camera.updateProjectionMatrix(); } } setClipState(state){//有时候需要暂时关闭下clip state = !!state; if(this.clipUnabled == !state)return this.scene.volumes.filter(v=>/* v.clip && */ v instanceof Potree.BoxVolume).map(volume=>{ volume.clip = state; Potree.Utils.updateVisible(volume, 'setClipState', state); }); this.clipUnabled = !state; } /* 大规模WebGL应用引发浏览器崩溃的几种情况及解决办法 https://blog.csdn.net/weixin_30378311/article/details/94846947 */ async render(params={}){//add params viewer.addTimeMark('render','start'); const vrActive = this.renderer.xr.isPresenting; let SiteModel = viewer.modules.SiteModel; if(this.screenshoting && !params.screenshot)return //正在截图 let s = SiteModel.editing && SiteModel.selected && (SiteModel.selected.buildType == 'room' || SiteModel.selected.buildType == 'floor'); //空间模型的房间选中材质是需要depth的,这时候需要绘制两次点云 Potree.settings.pointEnableRT = !this.screenshoting && (this.scene.measurements.filter(e=>e.visible).length > 0 || s ); if(vrActive){ this.renderVR(); }else { let specialRender = !!params.viewports; let viewports = params.viewports || this.viewports; if(!this.needRender){ viewports = viewports.filter(v=>v.needRender); //可以渲染的条件是viewer或viewport的needRender为true } viewports = viewports.filter(v=>v.active); if(viewports.length > 0){ params.viewports = viewports; if(this.outlinePass.selectedObjects.length && this.outlinePass.edgeStrength > 0 && !params.screenshot // || this.images360.fastTranMaskPass.enabled ){ let scenes = this.inputHandler.interactiveScenes.concat(this.scene.scene).concat(viewer.scene.scenePointCloud); this.composer.render(scenes, null, this.viewports, this.renderDefault.bind(this)); }else { await this.renderDefault(params); } } if(!specialRender) this.needRender = false; } viewer.addTimeMark('render','end'); } startScreenshot(info={}, width=800, height=400, compressRatio ){//add //let deferred = info.deferred || $.Deferred(); let getImageDeferred = info.getImageDeferred || $.Deferred(); let finishDeferred = info.finishDeferred || $.Deferred(); let viewerMaster = info.map ? this.mapViewer : this; //截图主体 let useMap = info.map || info.type == 'measure' || info.type.includes('prism2d'); if(Potree.settings.displayMode == 'showPanos' && viewer.mainViewport.view.isFlying('pos')){//如果在飞,飞完再截图 info.getImageDeferred = getImageDeferred , info.finishDeferred = finishDeferred; let f = ()=>{ this.startScreenshot(info, width, height, compressRatio); }; viewer.mainViewport.view.addEventListener('flyingDone', f, {once:true}); return {getImagePromise:getImageDeferred.promise(), finishPromise:finishDeferred.promise()} } var startTime = Date.now(); //抗锯齿待加 1 post处理 2截图大张再抗锯齿缩小 this.pauseTestMaxLevel = true; this.screenshoting = true; console.log('startScreenshot: '+startTime); let updateCamera = ()=>{ viewports.forEach(e=>{ e.view.applyToCamera(e.camera); //因为fly时只更新了view所以要强制更新下camera this.dispatchEvent({ //update map and sprite, mapChanged type: "camera_changed", camera: e.camera, viewport : e, changeInfo:{positionChanged:true,changed:true} }); }); }; let screenshot = async ()=>{ let pose; useMap && (viewer.mapViewer.needRender = true); info.beforeScreenshot && info.beforeScreenshot(); let render = async ()=>{ this.needRender = true; viewports.forEach(e=>e.active = true); if(info.useRenderTarget){ //离屏渲染 有抗锯齿问题、在手机上速度慢 主视图一样会变内容,还是不用这个了 var { dataUrl } = viewerMaster.makeScreenshot( new Vector2(width,height), null, compressRatio ); }else { //直接渲染 会改变canvas大小 let canvas = viewerMaster.renderer.domElement; //canvas.width = width, canvas.height = height //不需要改canvas大小, 只需要 this.renderer.setSize(width, height ); 前面updateScreenSize已经执行 await viewerMaster.render({ screenshot : true, width , height, resize :true, needWaitLoadPoint:!!info.splitRenderInfo, maxTimeForPointLoad:info.maxTimeForPointLoad }); //需要resize /* if(info.type.includes('prism2d')){//o.map要为true viewer.dispatchEvent({type:'resize', viewport:viewer.mapViewer.viewports[0]}) //借用viewer通知lineMaterial resize viewerMaster.renderOverlay() } */ var dataUrl = canvas.toDataURL('image/png',compressRatio); } return dataUrl }; let dataUrl; if(info.splitRenderInfo){//为了防崩溃,分区域批次渲染 let {wc , hc} = info.splitRenderInfo; let camera = viewer.mainViewport.camera; let dataUrls = []; let width_ = Math.ceil(1/wc * width) , height_ = Math.ceil(1/hc * height); //可能加起来比原图大,没事到时候边界会重合 viewer.updateScreenSize({forceUpdateSize:true, width:width_, height:height_, forTarget:info.useRenderTarget}); //更新viewports相机透视 使focusOnObject在此窗口大小下 for(let i=0;i{ oldStates.viewports.forEach(old=>{//恢复相机 var viewport = viewports.find(v=>v.name == old.name); viewport.left = old.left; viewport.bottom = old.bottom; viewport.width = old.width; viewport.height = old.height; viewport.view.copy(old.view); //viewport.view.applyToCamera(viewport.camera); if(viewport.camera.isOrthographicCamera){ viewport.camera.zoom = viewport.view.zoom; } }); viewerMaster.updateScreenSize({forceUpdateSize:true});//更新像素 /* oldStates.viewports.forEach(old=>{//恢复相机 var viewport = [mapViewport, mainViewport].find(v=>v.name == old.name); this.dispatchEvent({ //update map type: "camera_changed", camera: viewport.camera, viewport : viewport }) }) */ updateCamera(); finishDeferred.resolve({dataUrl, pose}); info.map || setTimeout(()=>{ if(!this.screenshoting){ //Potree.settings.pointNoLimit = false Potree.settings.pointDensity = 'high'; //console.warn('恢复pointDensity') /* if(viewer.scene.pointclouds[0].material.oldSize_ ){ viewer.scene.pointclouds[0].material.size = viewer.scene.pointclouds[0].material.oldSize_ viewer.scene.pointclouds[0].material.oldSize_ = null } */ } },500); //延迟:避免连续多次截图时释放点云 this.screenshoting = false; console.log('screenshot done: ' + startTime, 'costTime', (Date.now() - startTime) + 'ms'); }; {//恢复: this.backgroundOpacity = oldStates.bgOpacity; this.background = oldStates.background; this.pauseTestMaxLevel = false; oldStates.displayMode && (Potree.settings.displayMode = oldStates.displayMode); if(info.type == 'measure' || info.type.includes('prism2d')){ this.modules.SiteModel.pauseUpdateEntity = false; this.focusDatasets = null; this.scene.measurements.forEach(e=>Potree.Utils.updateVisible(e, 'screenshot',true)); info.type == 'measure' && info.measurement.setSelected(false, 'screenshot'); } if(info.type.includes('prism2d')){ //viewer.mapViewer.transparentBG = false; (info.prisms || [info.prism]).forEach(prism=>{ prism.changeStyleForScreenshot(false); if(info.type == 'prism2d-single'){ prism.baseModel && (Potree.Utils.updateVisible(prism.baseModel, 'screenshot', false, 3, 'cancel'), Potree.Utils.setObjectLayers(prism.baseModel, 'model')); prism.setEditState(false); } }); } this.images360.panos.forEach(pano=>{ Potree.Utils.updateVisible(pano, 'screenshot', true); }); Potree.Utils.updateVisible(this.reticule, 'screenshot', true); if(useMap){ Potree.Utils.updateVisible(this.mapViewer.cursor, 'screenshot', true); if(oldStates.attachedToViewer != this.mapViewer.attachedToViewer){ this.mapViewer.attachToMainViewer(!!oldStates.attachedToViewer ); } if(oldStates.attachedToViewer && this.mapViewer.splitDir != oldStates.oldSplitDir){ this.mapViewer.changeSplitScreenDir(oldStates.oldSplitDir); } mapViewport.noPointcloud = oldStates.noPointcloud; Potree.Utils.updateVisible(viewer.mapViewer.mapLayer.sceneGroup, 'screenshot-prism', true); } let recover = ()=>{ if(Potree.settings.displayMode == 'showPanos') { viewer.images360.flyToPano({pano:oldStates.pano, duration:0, callback:()=>{ finish(); }}); }else { finish(); } }; if(info.ifGetPose){ Potree.sdk.scene.getPose().done(pose_ =>{ pose = pose_; getImageDeferred.resolve({dataUrl, pose}); recover(); }); }else { recover(); } } };// screenshot end let mapViewport; let mainViewport = this.mainViewport; let viewports = [], oldStates = { viewports:[], pano: Potree.settings.displayMode == 'showPanos' ? viewer.images360.currentPano : null, bgOpacity : this.backgroundOpacity, background: this.background }; if(!info.type.includes('prism2d')){ viewports.push(mainViewport); mainViewport.camera.isOrthographicCamera && (mainViewport.view.zoom = mainViewport.camera.zoom); oldStates.viewports.push(mainViewport.clone()); } if(useMap){ mapViewport = this.mapViewer.viewports[0]; mapViewport.view.zoom = mapViewport.camera.zoom; viewports.push(mapViewport); oldStates.viewports.push(mapViewport.clone()); oldStates.attachedToViewer = this.mapViewer.attachedToViewer; oldStates.noPointcloud = mapViewport.noPointcloud; oldStates.displayMode = Potree.settings.displayMode; Potree.Utils.updateVisible(this.mapViewer.cursor, 'screenshot', false);//令mapCursor不可见 } viewports.forEach(e=>e.active = false); //暂停渲染 if(info.hideMarkers){ this.images360.panos.forEach(pano=>{//令漫游点不可见 Potree.Utils.updateVisible(pano, 'screenshot', false); }); } Potree.Utils.updateVisible(this.reticule, 'screenshot', false);//令reticule不可见 if(info.bgOpacity != void 0){ this.backgroundOpacity = info.bgOpacity; } if(info.background != void 0){ this.background = info.background; } Potree.settings.pointDensity = info.pointDensity || 'screenshot'; let focusDatasets = (measurements)=>{ this.focusDatasets = []; measurements.forEach(measure=>{ measure.points_datasets.forEach(e=>{ let p = viewer.scene.pointclouds.find(a=>a.dataset_id == e); if(p && !this.focusDatasets.includes(p)){ this.focusDatasets.push(p); } }); }); this.updateFpVisiDatasets(); }; if(info.type == 'measure'){//要截图双屏 this.scene.measurements.forEach(e=>Potree.Utils.updateVisible(e,'screenshot',e == info.measurement) ); info.measurement.setSelected(true, 'screenshot'); //因为分屏后位置才最终确定,才能确定是否显示出floorplan所以先分屏 if(Potree.settings.floorplanEnable){ oldStates.oldSplitDir = this.mapViewer.splitDir; this.mapViewer.attachToMainViewer(true, 'measure', 0.5 , {dir:'leftRight'} ); } viewer.updateScreenSize({forceUpdateSize:true, width, height, forTarget:info.useRenderTarget}); //更新viewports相机透视 使focusOnObject在此窗口大小下 let begin = ()=>{ useMap = this.mapViewer.attachedToViewer; updateCamera(); if(useMap){ let waitMap = ()=>{ console.log('waitMap: '+startTime, Date.now(), this.mapViewer.mapLayer.loadingInProgress ); this.mapViewer.waitLoadDone(screenshot.bind(this));//等待地图所有加载完 }; this.waitPointLoad(waitMap,info.maxTimeForPointLoad); }else { this.waitPointLoad(screenshot,info.maxTimeForPointLoad); } }; let {promise} = this.focusOnObject(info.measurement, 'measure', 0, { basePanoSize:1024, gotoBestView:true, //minMapWidth: THREE.Math.clamp(Math.min(viewer.bound.boundSize.x, viewer.bound.boundSize.y) / (20/* this.mapViewer.mapLayer.maps.find(e=>e.name == 'map').disabled ? 6 : 3*/), 2, 25) //有地图的话为了显示出名字需要更大范围 });//注意:不同角度截图 得到三维的会不一样,因为focusOnObject是根据方向的 promise.done(()=>{ //console.log('promise.done') //根据当前位置更新floorplan显示 //console.log('view Pos ', this.mainViewport.view.position.toArray()) this.updateDatasetAt(true); this.modules.SiteModel.updateEntityAt(true, {measure:info.measurement}); //更新测量线所在楼层,展示俯视图 if(!this.modules.SiteModel.inEntity){ focusDatasets([info.measurement]); } this.modules.SiteModel.pauseUpdateEntity = true; //console.log('currentFloor', this.modules.SiteModel.currentFloor, 'currentDataset', this.atDatasets ) let floorplanShowed = this.mapViewer.mapLayer.maps.some(e => e.name.includes('floorplan') && e.objectGroup.visible);//如果没有floorplan展示就不分屏(如果focus时飞出数据集从而不展示怎么办。尤其是俯瞰比较长的线时容易这样,或许要根据其所在数据集强制显示) if(!floorplanShowed && this.mapViewer.attachedToViewer){ this.mapViewer.attachToMainViewer(false); //取消分屏 viewer.updateScreenSize({forceUpdateSize:true, width, height, forTarget:info.useRenderTarget}); //更新viewports相机透视 let {promise} = this.focusOnObject(info.measurement, 'measure', 0, {basePanoSize:1024,gotoBestView:true,} );//因画面比例更改,重新focus promise.done(()=>{ begin(); }); }else { begin(); } }); }else if(info.type.includes('prism2d')){ let points = [], prisms = info.type == 'prism2d-all' ? info.prisms: [info.prism]; this.mapViewer.attachToMainViewer(true, 'screenshot', 1 , {/* dir:'leftRight' */} ); viewer.updateScreenSize({forceUpdateSize:true, width, height, forTarget:info.useRenderTarget}); //更新viewports相机透视 使focusOnObject在此窗口大小下 prisms.forEach(prism=>{ points.push(...prism.points); prism.setSelected(false); prism.changeStyleForScreenshot(true, {hideLabel: info.type != 'prism2d-all', showName: info.type == 'prism2d-all' }); if(info.type == 'prism2d-single') { prism.baseModel && (Potree.Utils.updateVisible(prism.baseModel, 'screenshot', true, 3, 'add'), Potree.Utils.setObjectLayers(prism.baseModel, 'bothMapAndScene')); prism.setEditState(true); } }); this.scene.measurements.forEach(e=>Potree.Utils.updateVisible(e,'screenshot', prisms.includes(e)) ); this.backgroundOpacity = 0; let {promise} = this.focusOnObject({points}, 'prisms2d', 0, { minMapWidth: 0.5 , onlyMap:true, focusBoundCenter:true, boundExpandRatio:1.4 }); promise.done(()=>{ updateCamera(); if(info.type == 'prism2d-all'){//全部 带平面图 focusDatasets(prisms); //更新平面图 可能有漏洞,当不在任何一个数据集内的话? this.mapViewer.waitLoadDone(screenshot.bind(this));//等待地图所有加载完 }else {//single mapViewport.noPointcloud = false; //显示点云,而非平面图 Potree.settings.displayMode = 'showPointCloud'; Potree.Utils.updateVisible(viewer.mapViewer.mapLayer.sceneGroup, 'screenshot-prism', false); this.waitPointLoad(screenshot, 5000); } }); }else {//default let done = ()=>{ updateCamera(); if(info.splitRenderInfo){ screenshot(); }else { this.waitPointLoad(screenshot, info.maxTimeForPointLoad); } }; if(info.focusObjectInfo){ viewer.updateScreenSize({forceUpdateSize:true, width, height, forTarget:info.useRenderTarget}); //更新viewports相机透视 使focusOnObject在此窗口大小下 let {promise} = this.focusOnObject( ...info.focusObjectInfo); promise.done(()=>{ done(); }); }else { done(); } } /* 测量线的截图因为要调用分屏的,会改变画面 但是普通截图的话,不会改变画面 */ return {getImagePromise:getImageDeferred.promise(), finishPromise:finishDeferred.promise()} } async waitPointLoad(done, maxTimeForPointLoad, viewport = viewer.mainViewport, resolution ){ return new Promise((resolve,reject)=>{ let finish; let dealDone = ()=>{ viewer.removeEventListener('update',update); viewer.removeEventListener('overPointBudget',decreaseLevel); finish || done && done(); finish = true; resolve(); }; let decreaseLevel = (e)=>{ //降点云level 但基本降完也不会再加载低等级的点云了,所以是否直接不降?即使质量不平均 if(e.restQueueSize < 10)return ;//差不多完成了 let pointclouds = viewer.scene.pointclouds.filter(e=>e.visibleNodes.length); console.log('准备decreaseLevel, numVisiblePoints', e.numVisiblePoints, '最小numVisiblePoints占比', 1/pointclouds.length ); pointclouds.forEach(pointcloud=>{ let percent = pointcloud.numVisiblePoints / e.numVisiblePoints; //console.log('numVisiblePoints占总比', pointcloud.dataset_id, percent ) if(percent < 1/pointclouds.length)return let old = pointcloud.maxLevel; let levels = pointcloud.visibleNodes.map(e=>e.getLevel()); let actMaxLevel = Math.max.apply(null, levels); //实际加载到的最高的node level pointcloud.maxLevel = actMaxLevel - 1; console.warn(pointcloud.dataset_id,'decreaseLevel,新maxLevel', actMaxLevel - 1, '原maxlevel', old ); }); }; /* if(Potree.settings.displayMode == 'showPointCloud'){ viewer.updateScreenSize({forceUpdateSize:true, width, height, forTarget:info.useRenderTarget})//需要先setSize才能加载范围内的点云 } */ //updateCamera() //viewer.addEventListener('overPointBudget',decreaseLevel) let update = ()=>{ //前提:已经禁止渲染,仅加载点云 let camera = viewport.forViewOffset ? viewport.forViewOffset.camera : viewport.camera; Potree.updatePointClouds(viewer.scene.pointclouds, camera , resolution || viewport.resolution ); }; viewer.addEventListener('update',update); update(); let maxTime = maxTimeForPointLoad || 2000; document.hidden && (maxTime *= 6); //离开页面后会变成1秒1帧 setTimeout(()=>{ if(Potree.pointsLoading && Potree.settings.displayMode == 'showPointCloud'){//如果还在加载 viewer.addEventListener('pointsLoaded',()=>{ //点云加载完时(不一定准确) if(!finish)console.log('加载完毕', ' numVisiblePoints', Potree.numVisiblePoints); dealDone(); },{once:true}); let overTime = ()=>{//超时不候(其实之后等待地图还会再加载几秒) if(document.hidden){ return setTimeout(overTime, maxTime) } if(!finish)console.warn('超时, numVisiblePoints', Potree.numVisiblePoints); dealDone(); }; setTimeout(overTime, maxTime); }else { console.log('已经加载完,无需再加载点云 numVisiblePoints', Potree.numVisiblePoints); dealDone(); } },200);//先加载一段时间 }) } focusOnObject(object, type, duration, o={} ) { //飞向热点、测量线等 。 //console.log('focusOnObject: ', object, type) let deferred = o.deferred || $.Deferred(); let target = new Vector3, //相机focus的位置 position = new Vector3, //相机最终位置 dis; //相机距离目标 duration = duration == void 0 ? 1200 : duration; let camera = o.endCamera || viewer.scene.getActiveCamera(); let cameraPos = camera.position.clone(); let boundSize; if(o.dontChangeCamDir && (o.endYaw == void 0 || o.endPitch == void 0)){ //在俯视时仅靠dir来算不准 o.endYaw = viewer.mainViewport.view.yaw; o.endPitch = viewer.mainViewport.view.pitch; } let moveMap = (done)=>{ if(this.mapViewer && !o.dontMoveMap){ //console.log('mapFocusOn: '+target.toArray()) const minMapWidth = o.minMapWidth || 2; //截图的时候要显示的地图范围较大,为了显示出地区名字 const minBound = new Vector2(minMapWidth,minMapWidth);//针对垂直线,在地图上只有一个点 //原始的bound let boundOri; if(object.points){ boundOri = new Box3(); object.points.forEach(e=>{ boundOri.expandByPoint(e); }); }else { boundOri = object.boundingBox; } if(o.focusBoundCenter){ boundOri.getCenter(target); } let boundSizeOri = boundOri.getSize(new Vector3); let boundSizeMap = boundSizeOri.clone().multiplyScalar(o.boundExpandRatio || math.linearClamp(Math.max(boundSizeOri.x, boundSizeOri.y) , [0.5, 30], [2.5, 1.2]));//为了能同时看清测量线和地图,当测量线较短时,扩大margin,防止地图过度放大 boundSizeMap.x = Math.max(minBound.x, boundSizeMap.x ); boundSizeMap.y = Math.max(minBound.y, boundSizeMap.y ); this.mapViewer.moveTo(target.clone(), boundSizeMap, duration, o.margin, null, done); } }; if(o.onlyMap){ moveMap(()=>{ deferred.resolve(); }); return {promise:deferred.promise()} } let getPosWithFullBound = (points, boundingBox, target, cameraPos )=>{//使boundingBox差不多占满屏幕时的相机到target的距离 // points 和 boundingBox 至少有一个 let scale; if(o.dontChangeCamDir){ var inv = camera.matrixWorldInverse; }else { var cameraTemp = camera.clone(); let view = viewer.mainViewport.view.clone(); view.position.copy(cameraPos); view.lookAt(target); if(o.endPitch != void 0){ view.pitch = o.endPitch; view.yaw = o.endYaw; } view.applyToCamera(cameraTemp); //对镜头的bound var inv = cameraTemp.matrixWorldInverse; } var bound = new Box3(); if(points){//使用points得到的bound更小 //如果points和boundingbox的差别较大,尤其使target和points中心不一致,那么points不一定会刚好在boundingbox内 points.forEach(e=>{ var p = e.clone().applyMatrix4(inv); bound.expandByPoint(p); }); scale = 1.2; }else { bound = boundingBox.applyMatrix4(inv); scale = 1; //0.9; } boundSize = bound.getSize(new Vector3); if(o.boundScale){ scale = o.boundScale; } { boundSize.x *= scale; //稍微放大一些,不然会靠到屏幕边缘 boundSize.y *= scale; let min = 0.0001; boundSize.x = Math.max(min, boundSize.x); boundSize.y = Math.max(min, boundSize.y); } if(camera.type == 'OrthographicCamera'){ dis = boundSize.length() / 2; }else { let aspect = boundSize.x / boundSize.y; if(camera.aspect > aspect){//视野更宽则用bound的纵向来决定 dis = boundSize.y/2/ Math.tan(MathUtils.degToRad(camera.fov / 2)) + boundSize.z/2; }else { let hfov = cameraLight.getHFOVForCamera(camera, true); dis = boundSize.x/2 / Math.tan(hfov / 2) + boundSize.z/2; } dis += camera.near; } dis = Math.max(0.1,dis); //三个顶点以上的由于measure的中心不等于bound的中心,所以点会超出bound外。 且由于视椎近大远小,即使是两个点的,bound居中后线看上去仍旧不居中. //获得相机最佳位置 let dir; if(o.dontChangeCamDir){ dir = viewer.mainViewport.view.direction.negate(); }else { dir = new Vector3().subVectors(cameraPos, target).normalize(); } if(o.dontLookUp && dir.z < 0){ dir.negate(); } position.copy(target).add(dir.multiplyScalar(dis)); if(false){//打开以检查box if(!this.boundBox){//调试 this.boundBox = new Mesh(new BoxGeometry(1,1,1,1)); this.boundBox.material.wireframe = true; this.boundBox.up.set(0,0,1); Potree.Utils.setObjectLayers(this.boundBox,'sceneObjects'); this.scene.scene.add(this.boundBox); } this.boundBox.position.copy(target); this.boundBox.scale.copy(boundSize); this.boundBox.lookAt(position); } return position }; if(this.images360.flying){ let f = ()=>{ this.focusOnObject(object, type, duration, $.extend(o,{deferred})); this.images360.removeEventListener('cameraMoveDone',f); }; this.images360.addEventListener('cameraMoveDone',f); return {promise: deferred.promise() } } if (type == 'measure') { if(object.points.length == 0){ return {promise:deferred.resolve()} } target.copy(object.getCenter()); //试试改变位置(方向),直视测量线。能避免倾斜角度造成的非常不居中、以及看不到面的情况 if(o.gotoBestView){//直接沿着法线方向看 if(object.points.length>2){ let facePlane = object.getFacePlane(target); let normal = facePlane.normal.clone(); if(normal.dot(this.mainViewport.view.direction) > 0){ normal.negate(); //顺着视线 } //normal.z = Math.max(0.2, normal.z)//尽量不要仰视 cameraPos.copy(target).add(normal); }else { let lineDir = new Vector3().subVectors(object.points[0],object.points[1]).normalize(); let facePlane = new Plane().setFromNormalAndCoplanarPoint(lineDir, target); //if(cameraPos.z < target.z + 1)cameraPos.z = target.z + 1//尽量不要仰视 let dir = new Vector3().subVectors(cameraPos,target).normalize(); dir.z = Math.max(dir.z, 0.4); cameraPos.copy(target).add(dir);//尽量不要仰视 let dis = cameraPos.distanceTo(target); dir = lineDir.clone().multiplyScalar(dis); let line = new Line3().set(cameraPos.clone().add(dir), cameraPos.clone().add(dir.negate())); //过相机位置作到面的垂线 facePlane.intersectLine(line, cameraPos ); //得垂足,作为新的相机位置。就能在测量线中间看测量线 } }else { if(object.points.length>2){ let facePlane = object.getFacePlane(target); let normal = facePlane.normal.clone(); let angle = this.mainViewport.view.direction.angleTo(normal); let minDiff = MathUtils.degToRad(60); //console.log('angle',angle) if(angle>minDiff && angleMath.PI-maxDiff){//当几乎正对时就不执行 if(angle>Math.PI/2){ //令dir和lineDir成钝角 lineDir.negate(); } let dir = new Vector3().subVectors(camera.position, target).normalize(); let mid = new Vector3().addVectors(lineDir, dir).normalize(); //中间法向量(如果刚好dir和lineDir反向,那得到的为零向量,就不移动了,但一般不会酱紫吧) let newDir = new Vector3().addVectors(dir, mid); cameraPos.copy(target.clone().add(newDir)); } }else { console.error('measure 没有facePlane points点数还不为2?'); } } //截图时直接飞到最佳视角,也就是平视它所在面。其余时候为了避免镜头旋转过大就算了。 position = getPosWithFullBound(object.points, null, target, cameraPos ); moveMap(); if(Potree.settings.displayMode == 'showPointCloud'){ //点云 let minDis = 0.3; if(0 ){//如果没有被选中,会被遮挡。 2023.11.15 最好展示全局(尤其截图时),虽然被遮挡,但focus的过程是选中状态全显示的,可以看出所在范围。 let checkIntersect = ( )=>{ let intersect = this.inputHandler.ifBlockedByIntersect({point:position, cameraPos: target});// 不一定准确 if(intersect){ let blockCount = 0, unblockCount = 0, visi; for(let i=0;i= 0.5){ visi = false; break } }else { unblockCount ++; if(unblockCount / object.points.length > 0.5){ visi = true; break } } } if(visi == void 0){ visi = unblockCount / object.points.length > 0.5; } let shrink = ()=>{ let dir = new Vector3().subVectors(position, target).normalize().multiplyScalar(intersect.distance); position.copy(target).add(dir); console.log('checkIntersect newPos', position.clone() ); }; if(!visi){//更改位置距离target如果小于最小距离,需要反向。 否则直接缩短距离。 if(intersect.distance < minDis ){ console.log('检测到intersect 反向', intersect.distance ); let position1 = position.clone(); let dir = new Vector3().subVectors(position, target); position.copy(target).sub(dir); let intersect2 = this.inputHandler.ifBlockedByIntersect({point: position, cameraPos:target});// 不一定准确 if(intersect2){ if(intersect2.distance < intersect.distance ){ position.copy(position1);//恢复 } shrink(); } }else { shrink(); } } } }; checkIntersect(); //多折线没有areaPlane 有时候会看向空白区域 - - } }else if(Potree.settings.displayMode == 'showPanos'){//全景 (比较难校准) let target2, dir; if( object.measureType.includes('MulDistance')){//因为该线不闭合,可能看向target的方向会没有线,所以换一个target target2 = object.points[Math.round(object.points.length / 2) ];//直接看向中间点 dir = new Vector3().subVectors(target2, position).normalize(); } let pano = viewer.images360.fitPanoTowardPoint({ /*point : target, //不使用目标点来判断是因为缺少measure角度的信息。比如虽然可以靠近线的中心,但是线朝向屏幕,那几乎就是一个点了。 //bestDistance : dis * 0.5, //乘以小数是为了尽量靠近 boundSphere: boundOri.getBoundingSphere(new THREE.Sphere), */ target, dir, point : position, bestDistance : 0 , checkIntersect: o.checkIntersect, gotoBestView: o.gotoBestView }); let result = {promise:deferred.promise() }; if(pano && pano.msg){ pano = pano.pano; result.msg = pano.msg; } if(pano){ viewer.images360.flyToPano({pano, target : target2 || target, duration, deferred, dontMoveMap:true , basePanoSize:o.basePanoSize});//dontMoveMap不要移动map,否则flytopano会自动在map中focus到漫游点的位置,而非测量线了 if(viewer.images360.currentPano == pano){ let dis1 = viewer.images360.currentPano.position.distanceTo(target); let dis2 = position.distanceTo(target); //console.log('dis1 / dis2',dis1 / dis2, 'dis1-dis2', dis1-dis2) return {msg: (dis1 / dis2 > 1.5 && dis1-dis2>10)? 'tooFar' : 'posNoChange', promise : deferred.promise() } } } return result /* if(viewer.images360.currentPano == pano){ let dis1 = viewer.images360.currentPano.position.distanceTo(target) let dis2 = position.distanceTo(target) //console.log('dis1 / dis2',dis1 / dis2, 'dis1-dis2', dis1-dis2) return {msg: (dis1 / dis2 > 1.5 && dis1-dis2>10)? 'tooFar' : 'posNoChange', promise : deferred.promise() } }else{ return {promise : deferred.promise()} } */ //出现过到达位置后测量线标签闪烁的情况 // 测量线截图 全景 最好能放大. 但要确保该位置放大后图片加载完有点困难 } } else if (type == 'tag' || type == 'point') { //dimension = 1 target.copy(object.position); let bestDistance = o.distance || 3; if(!o.dontMoveMap && this.mapViewer){ //console.log('mapFocusOn: '+target.toArray()) this.mapViewer.moveTo(target.clone(), null, duration); } if(Potree.settings.displayMode == 'showPointCloud'){ if(o.dontChangePos){ position.copy(cameraPos); }else { dis = bestDistance; let dir = o.direction ? o.direction.clone().negate() : this.mainViewport.view.direction.negate();// */new THREE.Vector3().subVectors(camera.position, target).normalize() if(o.dontLookUp && dir.z<0) dir.z *= -1; position.copy(target).add(dir.multiplyScalar(dis)); } if(o.sameFloor){//需要在同一楼层 let atFloor = this.modules.SiteModel.pointInWhichEntity(target, 'floor'); if(atFloor){ let camFloor = this.modules.SiteModel.pointInWhichEntity(position, 'floor'); if(camFloor != atFloor){ let raycaster = new Raycaster(); let origin = target; let dir = new Vector3().subVectors( position, target ).normalize(); raycaster.set(origin, dir); let intersect = Potree.Utils.getIntersect(null, [atFloor.box], null, raycaster); if(intersect){ let dis = MathUtils.clamp(intersect.distance - 0.2, camera.near, intersect.distance); position.addVectors(origin, dir.multiplyScalar(dis)); console.log('移动到楼层'); }else { console.error('?no intersect?'); } } } } if(o.checkIntersect){//识别被点云遮住的话 let intersect; //反向查找从target到相机的第一个intersect intersect = this.inputHandler.ifBlockedByIntersect({point:position, margin:0, cameraPos:target} /* {point:target, margin: 0.2, cameraPos:position} */); if(intersect){ position.copy(intersect.location); console.log('移近'); } } }else if(Potree.settings.displayMode == 'showPanos'){ let pano = viewer.images360.fitPanoTowardPoint({ point : target, dir : this.mainViewport.view.direction, //尽量不改相机方向,避免镜头晃动 checkIntersect: o.checkIntersect, sameFloor:o.sameFloor, bestDistance, maxDis: o.maxDis //越近越好,但不要太近,bestDistance左右差不多 }); let result = {promise:deferred.promise() }; if(pano && pano.msg){ pano = pano.pano; result.msg = pano.msg; } pano && viewer.images360.flyToPano({pano, target, duration, deferred, dontMoveMap:true , basePanoSize:o.basePanoSize }); return result } }else if(object.boundingBox && type == 'boundingBox'){//使屏幕刚好看全boundingBox target = object.boundingBox.getCenter(new Vector3); if(o.dir){ //指定方向 cameraPos.copy(target).sub(o.dir); } position = getPosWithFullBound(object.points, object.boundingBox.clone(), target, cameraPos ); if(Potree.settings.displayMode == 'showPanos'){//全景 (比较难校准) let pano = viewer.images360.fitPanoTowardPoint({ point : position, bestDistance : 0 , }); let result = {promise:deferred.promise() }; if(pano && pano.msg){ pano = pano.pano; result.msg = pano.msg; } pano && viewer.images360.flyToPano({pano, target, duration, deferred, dontMoveMap:true , basePanoSize:o.basePanoSize});//dontMoveMap不要移动map,否则flytopano会自动在map中focus到漫游点的位置,而非测量线了 if(!pano){ console.error('no pano'); } return result //出现过到达位置后测量线标签闪烁的情况 }else { /* if(o.dontChangeCamDir){ target = null } */ moveMap(); } } if(o.startCamera && o.endCamera){ viewer.mainViewport.view.tranCamera(this.mainViewport, { endPosition:position, target , boundSize, callback:()=>{ //console.log('focusOnObjectSuccess: '+object.name, type) deferred.resolve(); }, startCamera:o.startCamera, endCamera:o.endCamera, midCamera:this.scene.cameraBasic, endYaw:o.endYaw, endPitch:o.endPitch }, duration); }else if(camera.type == "OrthographicCamera"){ viewer.mainViewport.view.moveOrthoCamera(this.mainViewport, { endPosition:position, target , boundSize, endYaw:o.endYaw, endPitch:o.endPitch, callback:()=>{ //console.log('focusOnObjectSuccess: '+object.name, type) deferred.resolve(); }, }, duration); }else { viewer.mainViewport.view.setView({position, target, duration, endYaw:o.endYaw, endPitch:o.endPitch, callback:()=>{ //console.log('focusOnObjectSuccess: '+object.name, type) deferred.resolve(); } }); } this.dispatchEvent({type:'focusOnObject', CamTarget:target, position}); //给controls发送信息 return {promise:deferred.promise()} } flyToDataset(o={}){ var pointcloud; if(o instanceof Object3D) pointcloud = o; else if(o.pointcloud) pointcloud = o.pointcloud; else pointcloud = this.scene.pointclouds.find(p => p.dataset_id == o.id); let duration = o.duration == void 0 ? 1000 : o.duration; if(o.focusOnPoint || !pointcloud.panosBound){//focus点云上一点,避免center区域刚好没有点 if(pointcloud.root.geometryNode){ let posArr = pointcloud.root.geometryNode.geometry.attributes.position.array; let count = pointcloud.root.geometryNode.geometry.attributes.position.count; let index = Math.ceil(count / 2); //随便取一个点 let point = new Vector3(posArr[index*3+0],posArr[index*3+1],posArr[index*3+2]); //point.applyMatrix4(pointcloud.root.pointcloud.matrixWorld) point.applyMatrix4(pointcloud.root.sceneNode.matrixWorld); viewer.focusOnObject({position:point},'point',duration,{dontChangeCamDir:true, distance:15}); o.dontMoveMap || viewer.mapViewer.fitToPointcloud(pointcloud, duration); console.log('flyToDataset focusOnPoint done'); return } } var center = pointcloud.bound.getCenter(new Vector3); let position; let getPano = ()=>{//获取离中心最近的pano let request = []; let rank = [ Images360.scoreFunctions.distanceSquared({position: center}) ]; let r = Common.sortByScore(pointcloud.panos, request, rank); if(r.length){ return r[0].item } }; if(Potree.settings.displayMode == 'showPanos'){ let pano = getPano(); if(pano){ if(pano == this.images360.currentPano) return 'posNoChange' this.images360.flyToPano({ pano }); }else return false }else { let target; position = center; if(pointcloud.panosBound){ let panosCenter = pointcloud.panosBound.center; //pano集中的地方,也就是真正有点云的地方 position = panosCenter.clone(); /* let ratio = 0.2 position.z = center.z * ratio + panosCenter.z * (1-ratio) //因为panos一般比较低,为了不让相机朝下时看不到点云,加一丢丢中心高度 */ let pano = getPano(); if(pano){ target = pano.position; //针对像隧道一样的场景, 中心点还是panosCenter都在没有点云的地方,所以还是看向其中一个漫游点好。 position.z = target.z; //水平, 避免朝上或朝下 } } if(this.modules.Clip.editing){ position.z = center.z; //剪裁时在中心高度,因为以点云为重点 this.modules.Clip.bus.dispatchEvent({type:'flyToPos', position, duration }); }else { if(math.closeTo(position, this.images360.position)) return 'posNoChange' viewer.mainViewport.view.setView({position, target, duration }); } o.dontMoveMap || viewer.mapViewer.fitToPointcloud(pointcloud, duration); } return true } findClosestDatasetOnMap(position){//寻找当前平面图上离某点最近的数据集 let pointclouds = viewer.scene.pointclouds.filter(e=>e.visible); const addScore = viewer.bound.boundSize.length(); let r = Potree.Common.sortByScore(pointclouds, [], [(e)=>{//pos2d和bound2d距离排序 let pos3d = position.clone().setZ(e.panosBound ? e.panosBound.center.z : (e.bound.min.z+e.bound.max.z)/2); return - (e.panosBound ? e.panosBound.bounding : e.bound).distanceToPoint(pos3d) },(e)=>{//最有可能的是地图上显示的平面图 return e in viewer.fpVisiDatasets ? addScore : 0 }]); return r[0] && r[0].item } addTimeMark(name, type){ let record = Potree.timeCollect[name]; let needRecord = record && record.start && record.measures.length < record.minCount; if(needRecord || Potree.measureTimings){ performance.mark(name+"-"+type); if(type == 'end'){ let measure = performance.measure(name,name+"-start",name+"-end"); if(needRecord){ record.measures.push( measure.duration ); record.sum += measure.duration; record.ave = record.sum / record.measures.length; record.measures.sort( (a, b) => a - b ); record.median = record.measures[parseInt(record.measures.length / 2)]; } } } } addFakeMeasure(name,duration){//把一些count当做duration来统计 if(!Potree.measureTimings)return if(!this.fakeMeasure[name]){ this.fakeMeasure[name] = []; } let object = { name, duration }; this.fakeMeasure[name].push(object); } resolveTimings(timestamp,log){//打印用时。 注:performance手机的精度只到整数位 。 sidebar中监听update时有高cpu的函数所以不要用demo测 if(!this.toggle){ this.toggle = timestamp; } let duration = timestamp - this.toggle; if(duration > 1000.0){ if(log){ let measures = performance.getEntriesByType("measure"); for(let i in this.fakeMeasure){ measures.push(...this.fakeMeasure[i]); } let names = new Set(); for(let measure of measures){ names.add(measure.name); } let groups = new Map(); for(let name of names){ groups.set(name, { measures: [], sum: 0, n: 0, min: Infinity, max: -Infinity }); } for(let measure of measures){ let group = groups.get(measure.name); group.measures.push(measure); group.sum += measure.duration; group.n++; group.min = Math.min(group.min, measure.duration); group.max = Math.max(group.max, measure.duration); } /* let glQueries = Potree.resolveQueries(this.renderer.getContext()); // resolveQueries 无 for(let [key, value] of glQueries){ let group = { measures: value.map(v => {return {duration: v}}), sum: value.reduce( (a, i) => a + i, 0), n: value.length, min: Math.min(...value), max: Math.max(...value) }; let groupname = `[tq] ${key}`; groups.set(groupname, group); names.add(groupname); } */ for(let [name, group] of groups){ group.mean = group.sum / group.n; /* group.measures.sort( (a, b) => a.duration - b.duration ); if(group.n === 1){ group.median = group.measures[0].duration; }else if(group.n > 1){ group.median = group.measures[parseInt(group.n / 2)].duration; } */ let measures = group.measures.slice(); measures.sort( (a, b) => a.duration - b.duration ); if(group.n === 1){ group.median = measures[0].duration; }else if(group.n > 1){ group.median = measures[parseInt(group.n / 2)].duration; } } let cn = Array.from(names).reduce( (a, i) => Math.max(a, i.length), 0) + 5; let cmin = 6; let cmed = 6; let cmax = 6; let csam = 4; let message = ` ${"NAME".padEnd(cn)} |` + ` ${"MIN".padStart(cmin)} |` + ` ${"MEDIAN".padStart(cmed)} |` + ` ${"MAX".padStart(cmax)} |` + ` ${"AVE".padStart(cmax)} |` + ` ${"SAMPLES".padStart(csam)} \n`; message += ` ${"-".repeat(message.length) }\n`; names = Array.from(names).sort(); for(let name of names){ let group = groups.get(name); let min = group.min.toFixed(3); let median = group.median.toFixed(3); let max = group.max.toFixed(3); let ave = group.mean.toFixed(3); //add let n = group.n; message += ` ${name.padEnd(cn)} |` + ` ${min.padStart(cmin)} |` + ` ${median.padStart(cmed)} |` + ` ${max.padStart(cmax)} |` + ` ${ave.padStart(cmax)} |` + ` ${n.toString().padStart(csam)}\n`; } message += `\n`; console.log(message); } this.fakeMeasure = {}; //clear performance.clearMarks(); performance.clearMeasures(); this.toggle = timestamp; } //注意,console.log本身用时挺高,降4倍时可能占用0.5毫秒,所以不能每帧都打印 } loop(timestamp){ //let startTime = performance.now() //console.log('间隔:' /*, parseInt((startTime - this.lastEndTime)*100 )/100 */) if(this.paused)return if(performance.getEntriesByName("loopWaitNext-start").length)viewer.addTimeMark('loopWaitNext','end'); if(this.stats){ this.stats.begin(); } performance.mark('loop-start') ;// 无论有没有reportTimings都要获取,因为getBestCound需要 this.dispatchEvent('loopStart'); this.interacted = false; this.shelterCount = {byTex:0, byCloud:0, maxByTex: 100, maxByCloud:0 }; //清空 因ifPointBlockedByIntersect可能在任何时候触发,所以需要一开始就定义这个,且每次计算最大可计算次数太麻烦了就定义一个吧。 let deltaTime = this.clock.getDelta(); this.update(deltaTime, timestamp); this.magnifier.render(); this.render(); this.objs.children.forEach(e=>{ if(e.fileType == '3dTiles'){ e.traverse(child=>{ if(child.runtime){ child.runtime.update(deltaTime, this.renderer, this.mainViewport.camera); return {stopContinue:true} } }); } }); // let vrActive = viewer.renderer.xr.isPresenting; // if(vrActive){ // this.update(this.clock.getDelta(), timestamp); // this.render(); // }else{ // this.update(this.clock.getDelta(), timestamp); // this.render(); // } Potree.framenumber++; //------------- this.images360.tileDownloader.update(); this.images360.panoRenderer.update(); this.images360.getNeighbours(this.interacted); this.computeShelter(); //------------- if(this.stats){ this.stats.end(); } viewer.addTimeMark('loop','end'); viewer.addTimeMark('loopWaitNext','start'); this.resolveTimings(timestamp, Potree.measureTimings); //Potree.measureTimings = 1 } postError(content, params = {}){ let message = this.postMessage(content, params); message.element.addClass("potree_message_error"); return message; } postMessage(content, params = {}){ let message = new Message(content); let animationDuration = 100; message.element.css("display", "none"); message.elClose.click( () => { message.element.slideToggle(animationDuration); let index = this.messages.indexOf(message); if(index >= 0){ this.messages.splice(index, 1); } }); this.elMessages.prepend(message.element); message.element.slideToggle(animationDuration); this.messages.push(message); if(params.duration !== undefined){ let fadeDuration = 500; let slideOutDuration = 200; setTimeout(() => { message.element.animate({ opacity: 0 }, fadeDuration); message.element.slideToggle(slideOutDuration); }, params.duration); } return message; } getBoundingBox (pointclouds) { //可以直接返回viewer.bound if(!this.bound){ this.updateModelBound(); } return this.bound.boundingBox.clone()//this.scene.getBoundingBox(pointclouds); }; updateModelBound(reason){ this.boundNeedUpdate = false; this.bound = Utils.computePointcloudsBound(this.scene.pointclouds.filter(pointcloud=> //只求可见 pointcloud.visible || pointcloud.unvisibleReasons && pointcloud.unvisibleReasons.length == 1 && pointcloud.unvisibleReasons[0].reason == 'displayMode' )); if(Potree.settings.boundAddObjs){//加上obj的bound 需要确保都updateMatrixWorld过 this.objs.children.forEach(e=>{ this.bound.boundingBox.union(e.boundingBox.clone().applyMatrix4(e.matrixWorld)); }); this.bound.boundingBox.getSize(this.bound.boundSize); this.bound.boundingBox.getCenter(this.bound.center); } viewer.farWhenShowPano = this.bound.boundSize.length() * 10;//全景漫游时要能看到整个skybox 原本*2的但对于距离特远的数据集需要乘大一些否则会黑面 /* let boundPlane = new THREE.Box3() boundPlane.expandByPoint(this.bound.boundingBox.min.clone())//最低高度为bound的最低 boundPlane.expandByPoint(this.bound.boundingBox.max.clone().setZ(this.bound.center.z))//最高高度为bound的中心高度 FirstPersonControls.boundPlane = boundPlane */ //FirstPersonControls.standardSpeed = THREE.Math.clamp( Math.sqrt(this.bound.boundSize.length() )/ 100 , 0.02,0.5); //在这个boundPlane中的速度 viewer.scene.pointclouds.forEach(e=>{//海拔范围 e.material.heightMin = this.bound.boundingBox.min.z; e.material.heightMax = this.bound.boundingBox.max.z; }); this.dispatchEvent({type:'updateModelBound'}); } waitForLoad(object, isLoadedCallback){//等待加载时显示loading。主要是贴图 this.waitQueue.push({ object, isLoadedCallback, }); //console.warn('waitForLoad',object.id,this.waitQueue.length) 1 === this.waitQueue.length && this.dispatchEvent({type:"loading", show:true}); } ifAllLoaded( ){ if(this.waitQueue.length>0){ this.waitQueue = this.waitQueue.filter(function(e) { return !e.isLoadedCallback() }); } //console.warn('ifAllLoaded', this.waitQueue.length) 0 === this.waitQueue.length && this.dispatchEvent({type:"loading", show:false}); } cancelLoad(object){//add 突然出现还没加载完就被deactivateTiledPano但还在loading的情况,所以加了这个 this.waitQueue = this.waitQueue.filter(function(e) { return e.object != object }); //console.log('cancelLoad', object.id) this.ifAllLoaded(); } setView(o={}){ let callback = ()=>{ if(o.displayMode){ Potree.settings.displayMode = o.displayMode; } if(!o.pano && o.currentPano )this.images360.currentPano = o.currentPano; o.callback && o.callback(); }; if(o.pano != void 0){//pano 权重高于 position this.images360.flyToPano(o); }else { this.mainViewport.view.setView($.extend({},o, {callback})); } } //设置点云为标准模式 setPointStandardMat(state, pointDensity, fitPointsize){ console.log('setPointStandardMat',state); if(state){ if(this.pointStatesBefore){ return console.warn('已设置过pointStatesBefore!') } this.pointStatesBefore = { opacity : new Map(), size: new Map(), density:Potree.settings.pointDensity, useEDL:this.getEDLEnabled(), shape: viewer.scene.pointclouds[0].material.shape }; viewer.scene.pointclouds.forEach(e=>{ this.pointStatesBefore.opacity.set(e, e.temp.pointOpacity); //因为更改pointDensity时会自动变opacity,所以这项最先获取 this.pointStatesBefore.colorType = e.material.activeAttributeName; fitPointsize && this.pointStatesBefore.size.set(e,e.temp.pointSize); //这项不一定有用,因为会被后期覆盖 }); if(pointDensity)Potree.settings.pointDensity = pointDensity; //万一之后切换到全景模式怎么办 if(fitPointsize)Potree.settings.sizeFitToLevel = true; viewer.scene.pointclouds.forEach(e=>{ e.material.activeAttributeName = 'rgba'; e.material.shape = Potree.PointShape['SQUARE']; fitPointsize && e.changePointSize(Potree.config.material.realPointSize, true); e.changePointOpacity(1); }); viewer.setEDLEnabled(false); }else { if(!this.pointStatesBefore){ return console.error('未设置过pointStatesBefore!') } Potree.settings.sizeFitToLevel = false; if(pointDensity)Potree.settings.pointDensity = this.pointStatesBefore.pointDensity; viewer.scene.pointclouds.forEach(e=>{ e.material.activeAttributeName = this.pointStatesBefore.colorType; e.changePointOpacity(this.pointStatesBefore.opacity.get(e)); e.material.shape = this.pointStatesBefore.shape; let size = this.pointStatesBefore.size.get(e); if(size) e.changePointSize(size); }); viewer.setEDLEnabled(this.pointStatesBefore.useEDL); this.pointStatesBefore = null; } } //调试时显示transformControl来调节object transformObject(object){ let seleted = viewer.inputHandler.selection[0]; if(!object){//取消 seleted && viewer.inputHandler.toggleSelection(seleted); return } if(seleted && seleted != object){//要更换,先取消 this.transformObject(null); } if(!object.boundingBox){ object.boundingBox = new Box3(); //任意大小 只是为了显示黄色外框 //??? computeBoundingBox } if(!viewer.inputHandler.selection.includes(object)){ viewer.inputHandler.toggleSelection(object); } } pointInWhichPointcloud(pos){//选择最接近中心的那个 使用boundSphere let result = Common.sortByScore(this.scene.pointclouds,[],[ (pointcloud)=>{ var size = pointcloud.pcoGeometry.tightBoundingBox.getSize(new Vector3); var center = pointcloud.bound.getCenter(new Vector3); var length = size.length() / 2; var dis = pos.distanceTo(center); return length / dis //到数据集中心的距离占数据集大小越小越好 } ]); //若要求更准确的话,可以使用ifContainsPoint判断一下是否在bound中 let r = result[0]; return r && r.score > 1 ? result[0].item : null } /* createRoomEv(){ const environment = new RoomEnvironment(); const pmremGenerator = new THREE.PMREMGenerator( this.renderer ); } */ modelLoaded(object, fileInfo_={}, done){//普通模型加载完以后 object.isModel = true; let boundingBox = new Box3(); if(fileInfo_.parentInfo){ object.name = fileInfo_.name; fileInfo_.parentInfo.loadedCount ++; fileInfo_.parentInfo.modelGroup.add(object); if(fileInfo_.parentInfo.loadedCount == fileInfo_.parentInfo.url.length){ return this.modelLoaded(fileInfo_.parentInfo.modelGroup, fileInfo_.parentInfo, done) }else { return } } object.name = fileInfo_.name != void 0 ? fileInfo_.name : fileInfo_.fileType; object.fileType = fileInfo_.fileType; object.boundingBox = boundingBox; //未乘上matrixWorld的本地boundingBox //object.scale.set(1,1,1);//先获取原始的大小时的boundingBox object.opacity = 1; //初始化 记录 object.updateMatrixWorld(); if(fileInfo_.id != void 0)object.dataset_id = fileInfo_.id; fileInfo_.loadCostTime = Date.now() - fileInfo_.loadStartTime; /* let weight = Math.round((total / 1024 / 1024) * 100) / 100;*/ console.log( '加载完毕:', Common.getNameFromURL(fileInfo_.url), '耗时(ms)', fileInfo_.loadCostTime, /* 模型数据量:' + weight + 'M' */); if(fileInfo_.fileType == '3dTiles'){ let isGroup = !object.runtime; let children = object.runtime ? [object] : object.children; children.forEach(object =>{ let boundingBox_ = new Box3(); let tileset = object.runtime.getTileset(); //TileHeader: tileset.root //参见另一个工程 TileRenderer.js preprocessNode //这个坐标位置几万…… let data = boundingVolume.halfAxes //但这个似乎是premultiply( transform );过后的,可能需还原下 let json = tileset.tileset; let box = json.root.boundingVolume.box; if(box){ let center = new Vector3(box[0],box[1],box[2]); let boundSize = new Vector3( ); // get the extents of the bounds in each axis let vecX = new Vector3( box[ 3 ], box[ 4 ], box[ 5 ] ); let vecY = new Vector3( box[ 6 ], box[ 7 ], box[ 8 ] ); let vecZ = new Vector3( box[ 9 ], box[ 10 ], box[ 11 ] ); const scaleX = vecX.length(); const scaleY = vecY.length(); const scaleZ = vecZ.length(); boundingBox_.min.set( - scaleX, - scaleY, - scaleZ ); boundingBox_.max.set( scaleX, scaleY, scaleZ ); if(isGroup){//如果是多个拼成的,每个都保留原先在parent中的offset。 如果是外层,作为独立个体,不用理会位置信息,直接放中央。 object.position.copy(center); object.position.z *= -1; boundingBox_.translate(object.position); //由于是内层,其位移会改变整体的boundingbox } }else if(json.root.boundingVolume.sphere){ let sphereData = json.root.boundingVolume.sphere; let center = new Vector3(...sphereData); let radius = sphereData[3] / 2; /* let sphere = new THREE.Sphere(center, radius) let box = sphere.getBoundingBox() boundingBox.copy(box) */ boundingBox_.min.set( - radius, - radius, - radius ); boundingBox_.max.set( radius, radius, radius ); }else { return console.error('json boundingVolume 缺少信息') } //中心点居然没用。可能是漏用了什么信息,也许这和LVBADUI_qp是散的有关。 console.log('3d tiles json',json); json.root.refine = 'ADD'; json.refine = 'ADD'; boundingBox.union(boundingBox_); }); }else { Potree.Utils.setObjectLayers(object, Potree.settings.showObjectsOnMap ? 'bothMapAndScene' : 'model'); object.traverse( ( child )=>{ let is = child.isMesh || child instanceof Points || child.isLine; if (is){ child.renderOrder = Potree.config.renderOrders.model; //if(Potree.settings.boundAddObjs){ child.geometry.computeBoundingBox(); //console.log(child.matrixWorld.clone()) boundingBox.union(child.geometry.boundingBox.clone().applyMatrix4(child.matrixWorld)); //但感觉如果最外层object大小不为1,要还原下scale再乘 //}//获取在scale为1时,表现出的大小 //Potree.Utils.makeTexDontResize(child.material.map) //console.log(child.name, 'roughness',child.material.roughness,'metalness',child.material.metalness) if(fileInfo_.unlit && (!(child.material instanceof BasicMaterial) /* || object.fileType == 'glb' */)){ //注释掉是因为已经写入到loader文件里了 //let material = new THREE.MeshBasicMaterial({map:child.material.map}) let material = new BasicMaterial({map : child.material.map, opacity:child.material.opacity, color:child.material.color}); //很奇怪glb的图会使原本的MeshBasicMaterial 会偏暗,所以自己重新写 //child.material.dispose() child.material = material; } if(fileInfo_.useStandandMat && !(child.material instanceof MeshStandardMaterial)){ child.material = new MeshStandardMaterial(); } if(child.material instanceof MeshStandardMaterial){ /* child.material.roughness = 0.7 child.material.metalness = 0.5 */ } //纯色的还是不能用BasicMaterial } } ); } this.objs.add(object); if(fileInfo_.transform){ let setTransfrom = (name)=>{ let value = fileInfo_.transform[name]; if(!value)return if(value instanceof Array){ object[name].fromArray(value); }else { object[name].copy(value); } }; setTransfrom('position'); setTransfrom('rotation'); setTransfrom('scale'); } if(fileInfo_.moveWithPointcloud){ object.updateMatrix(); object.matrixAutoUpdate = false; object.matrix.premultiply(viewer.scene.pointclouds[0].transformMatrix); //默认跟随第一个数据集 object.matrixWorldNeedsUpdate = true; } object.updateMatrixWorld(); MergeEditor.getBoundCenter(object); //初始化 done && done(object, fileInfo_); this.dispatchEvent({type:'modelLoaded',model:object}); //如果需要点击出现transform工具需要它有select事件 如 viewer.objs.children[1].addEventListener('select',()=>{}) } async loadModel(fileInfo, done, onProgress_, onError){ console.log('开始加载', Common.getNameFromURL(fileInfo.url) ); let boundingBox = new Box3(); /* if(!Potree.settings.boundAddObjs){ boundingBox.min.set(-0.5,-0.5,-0.5); boundingBox.max.set(0.5,0.5,0.5) } */ if(fileInfo.objurl){ fileInfo.url = fileInfo.objurl, fileInfo.fileType = 'obj'; //兼容最早的 } if(fileInfo.url instanceof Array){ if(fileInfo.url.length == 1){ fileInfo.url = fileInfo.url[0]; }else { fileInfo.loadedCount = 0; fileInfo.modelGroup = new Object3D; //parentGroup.name = fileInfo.title fileInfo.url.forEach((url,i)=>{ let fileInfoS = Common.CloneObject(fileInfo); fileInfoS.url = url; fileInfoS.name = 'child-'+i; fileInfoS.parentInfo = fileInfo; this.loadModel(fileInfoS, done, onProgress_, onError); }); return } } fileInfo.url = Common.dealURL(fileInfo.url); //去除'+' fileInfo.loadStartTime = Date.now(); //let fileType = fileInfo.tilesUrl ? '3dTiles' : fileInfo.objurl ? 'obj' : 'glb' let loadDone = (object, fileInfo_ )=>{ this.modelLoaded(object, fileInfo_ || fileInfo , done); }; let onProgress = function ( xhr ) { if ( xhr.lengthComputable ) { let percentComplete = xhr.loaded / xhr.total * 100; //console.log( Math.round(percentComplete, 2) + '% downloaded' ); onProgress_ && onProgress_(percentComplete); } }; if(fileInfo.fileType == 'obj'){ //暂时不支持数组 if(fileInfo.mtlurl){ loaders.mtlLoader.load( fileInfo.mtlurl , (materials)=>{ materials.preload(); loaders.objLoader.setMaterials( materials ).load(fileInfo.objurl, (object, total)=>{ loadDone(object/* , total, fileInfo.objurl */); }, onProgress, onError ); } , onProgress, onError ); }else { loaders.objLoader.load(fileInfo.objurl, (object, total)=>{ loadDone(object); }, onProgress, onError); } }else if(fileInfo.fileType == 'glb'){ loaders.glbLoader.unlitMat = true;//!!fileInfo.unlit loaders.glbLoader.load(fileInfo.url, ( gltf, total )=>{ //console.log('loadGLTF', gltf) loadDone(gltf.scene/* , total, fileInfo.url */); }, onProgress, onError); }else if(fileInfo.fileType == 'ply'){ loaders.plyLoader.load( fileInfo.url, (geometry) => { let object; console.log('ply加载完毕', geometry); if(!geometry.index){//点云 object = new Points(geometry, new PointsMaterial({vertexColors:true, size:0.02})); //141M的点云,intersect费时300ms以上 }else {//mesh object = new Mesh(geometry); } loadDone(object); }); }else if(fileInfo.fileType == '3dTiles'){ let result = await Loader3DTiles.load({ url: fileInfo.url, gltfLoader : loaders.glbLoader, //renderer: SceneRenderer.renderer options: { //dracoDecoderPath: '../utils/loaders/DRACOLoader/draco', //basisTranscoderPath: '../utils/loaders/KTX2Loader/basis', maximumScreenSpaceError: fileInfo.maximumScreenSpaceError || 80, //越小越清晰。 如果本身tiles很密很小这个值就不能很大。 //maxDepth: 100, //maximumMemoryUsage: 100, //缓存大小,见tiles3DMaxMemory。单位M(但实际结果是 2.5*maximumMemoryUsage + 750 。超过2G会崩, 所以应该小于540) 若太小,密集的tile反复加载很卡. (任务管理器刷新网页后若内存不掉就要结束进程否则虚高) debug: browser.urlHasValue('tilesBox'), //show box parent: this.scene.scene, is4dkk: fileInfo.is4dkk,//是否是4dkk中的模型. 通常maximumScreenSpaceError需要10 updateTime: fileInfo.updateTime, //加后缀防止缓存 }, }); console.log(result); result.model.runtime = result.runtime; let loaded = false; let tileset = result.runtime.getTileset(); tileset.addEventListener('endTileLoading', function (data) {//Tileset3D if (data.loadingCount == 0 && !loaded) { loaded = true; console.log('loaded!!!!!!!!!!!!!'); } }); tileset.addEventListener('tileLoaded',(e)=>{ //每一个tile加载完要更改透明度 let opacity = result.model.parent == this.objs ? result.model.opacity : result.model.parent.opacity;//最多两层 MergeEditor.changeOpacity(e.tileContent,opacity); //set Layers ? }); { let vi = true; Object.defineProperty( result.model, "visible", { get: function() { return vi }, set: function(v) { vi = v; tileset.visible = v; //同步,使不加载 tileset.nextForceUpdate = true; } }); } loadDone(result.model/* , null, fileInfo.url */); }else if(fileInfo.fileType == 'dxf'){ loaders.dxfLoader.load(fileInfo.url,(object)=>{ loadDone(object); },fileInfo); }else if(fileInfo.fileType == 'shp'){ loaders.shapeLoader.transform = viewer.transform.lonlatToLocal; const shp = await loaders.shapeLoader.load(fileInfo.url); const shpModel = shp.node; loadDone(shpModel); }else if(fileInfo.fileType == '3dgs'){ const gsViewer = new Potree.GaussianSplats3D.Viewer({ rootElement: this.renderArea, threeScene: this.scene.scene, renderer: this.renderer, camera: this.mainViewport.camera, useBuiltInControls: false, //dropInMode: true, // sharedMemoryForWorkers:false //否则 报错 Uncaught (in promise) DOMException: Failed to execute 'postMessage' on 'DedicatedWorkerGlobalScope': SharedArrayBuffer transfer requires self.crossOriginIsolated. }); //let path = Potree.resourcePath+'/models/gaussian/bonsai.ksplat'; gsViewer.addSplatScene(fileInfo.url, { 'streamView': true }) .then(() => { gsViewer.start(); /* gsViewer.splatMesh.rotation.x = -2.365929590263301 gsViewer.splatMesh.position.z = 5 */ gsViewer.splatMesh.updateMatrix(); gsViewer.splatMesh.updateMatrixWorld(); gsViewer.splatMesh.visible = false; //viewer.mainViewport.view.setView({position: new THREE.Vector3(-4.980, -5.3879, 5.4503095), quaternion:new THREE.Quaternion(0.5750,-0.2809,-0.3372,0.6903)}) gsViewer.splatMesh.onSplatTreeReadyCallback = ()=>{ let {sceneMax,sceneMin} = gsViewer.splatMesh.splatTree.subTrees[0]; gsViewer.splatMesh.boundingBox.min.copy(sceneMin); gsViewer.splatMesh.boundingBox.max.copy(sceneMax); loadDone(gsViewer.splatMesh); }; }); //window.gsViewer = gsViewer viewer.addEventListener('render.begin2',(e)=>{ if(e.name == 'scene'){ //gsViewer.selfDrivenUpdate() gsViewer.update(); if(gsViewer.initialized && gsViewer.splatRenderReady){ gsViewer.splatMesh.visible = true; } } }); } } removeModel(model){ this.objs.remove(model); let dispose = (e)=>{ e.geometry && e.geometry.dispose(); e.material && e.material.dispose(); }; model.traverse(e=>{ dispose(e); }); if(Potree.settings.boundAddObjs){ this.updateModelBound(); } } setDisplay(state, cause='setDisplay'){//如果创建了iframe,主页的需要隐藏的话需要释放一些内存出来。iframe关闭前也释放下比较保险 state = !!state; this.objs.children.forEach(e=>{ if(e.fileType == '3dTiles'){ let tileset = e.runtime.getTileset(); Potree.Utils.updateVisible(e, cause, state); if(!state) tileset._cache.trim(); //使下一次update时dispose所有不可见的tiles e.runtime.update(16, this.renderer, this.mainViewport.camera, true); if(state) this.dispatchEvent('content_changed'); } }); if(state){ Potree.pointBudget = 6*1000*1000; //先随便写一个, 随后mergeEditor.updateMemoryUsage }else { Potree.pointBudget = 0; Potree.updatePointClouds(this.scene.pointclouds, this.mainViewport.camera, this.mainViewport.resolution ); this.images360.panoRenderer.disposeIdelTargets(); //如果也能清空当前使用的就好了,但是恢复就需要时间 this.images360.depthSampler.clearTexData(); } this.dispatchEvent({type:'setDisplay',state}); this.paused = !state; } addFire(){ if(Potree.settings.number == 't-CwfhfqJ'){ let position = Potree.Utils.datasetPosTransform({ pointcloud:viewer.scene.pointclouds[0], position: new Vector3(4.4318,-0.580291847759, -0.78), fromDataset:true }); viewer.modules.ParticleEditor.addParticle( { type:'fire', positions:[position], radius:0.42, height:10, }); viewer.modules.ParticleEditor.addParticle( { type:'smoke', positions: [ new Vector3().addVectors(position,new Vector3(0,0,0.3))], positionStyle : 'sphere' , positionRadius : 0.3, sizeTween: [[0, 0.3, 0.9, 1], [0.05, 0.1, 1, 0.8]], opacityBase : 0.2, opacityTween :[ [0, 0.3, 0.7, 0.95, 1], [0, 0.2, 1 , 0.1, 0] ], velocityBase : new Vector3( 0, 0, 1), velocitySpread : new Vector3( 0.2, 0.2, -0.3), accelerationBase : 0.2, accelerationSpread : 0.7, radius:0, //particlesPerSecond : 30, particleDeathAge : 3.0, }); viewer.modules.ParticleEditor.addParticle( { type:'explode', name:'fire splash', position: new Vector3().addVectors(position,new Vector3(0,0,0.3)), size: 0.1, sizeRange: 0.3, sizeTween:[[0, 0.05, 0.3, 0.45], [0, 0.02, 0.1, 0.05] ], opacityTween: [[0, 0.05, 0.3, 0.45], [1, 1, 0.5, 0]] , speed : 1, //sphere speedRange : 4, radius: 0.1, acceleration : 0.3, accelerationRange : 1, particleSpaceTime:0, strength:4, }); } } addVideo(){ if(Potree.settings.number != 'SS-t-P6zBR73Gke')return var geo = new PlaneGeometry(1, 1, 1, 1); var videoInfo = this.videoInfo = [ { id: '40-2', url: 'https://laser-oss.4dkankan.com/testdata/SS-t-P6zBR73Gke/temp/poi/2022/05/10/0aabafee-36b8-455d-9c11-0780bf694786.mp4', rotation:[-1.494468618954883, -1.4987317433158989, -3.061254983446741], position:[ 19.801820617361624, 2.884673619844108, -0.03362305858221648], scale:[3.5741423153151763, 2.8738725275578703, 1], }, { id: 40, /* rotation:[-1.534692822378723, 0.01083403560862361, 3.141535283661569], position:[17.2934294239949861, 2.413510747928117, -0.008057029580231356], */ url: 'https://laser-oss.4dkankan.com/testdata/SS-t-P6zBR73Gke/temp/poi/2022/05/09/7896d6ef-a2d6-4fd7-949c-768782a5b484.mp4', rotation:[-1.5487684197910518, 0.021848470169552752, -3.1387534893955236], position:[17.277316608096, 2.0840432922115846, -0.0931149415437065], scale:[2.0821757723834047, 0.6129478480765236, 1], visibles: [40] }, ]; let add = (info)=>{ var video = $(``)[0]; video.setAttribute("crossOrigin", 'Anonymous'); video.src = info.url || Potree.resourcePath+`/video/${Potree.settings.number}/${info.id}.mp4`; var map = new VideoTexture(video); var plane = this.videoPlane = new Mesh(geo, new MeshBasicMaterial({ color:"#ffffff", transparent: !0, depthTest:false, opacity:0 , //side:2, map })); plane.position.fromArray(info.position); plane.rotation.fromArray(info.rotation); info.scale && plane.scale.fromArray(info.scale); this.scene.scene.add(plane); info.plane = plane; plane.boundingBox = new Box3(new Vector3(0,-0.5,0),new Vector3(1,-0.4,0.2)); video.addEventListener('loadeddata', function(e) { video.play(); if(!info.visibles/* ||!viewer.images360.currentPano || info.visibles.includes(viewer.images360.currentPano.id) */){ plane.material.opacity = 1; } info.scale || plane.scale.set(video.videoWidth/1000,video.videoHeight/1000,1); // 1080 * 1920 console.log('video loadeddata', info.id); }); if(info.visibles){ this.images360.addEventListener('flyToPano' ,(e)=>{//飞之前 if(info.visibles.includes(e.toPano.pano.id)){ //出现 setTimeout(()=>{ plane.visible = true; video.currentTime = 0; video.play(); if(video.paused){ var startPlay = ()=>{ plane.visible && video.play(); this.removeEventListener('global_mousedown', startPlay); }; this.addEventListener('global_mousedown', startPlay); } Potree.settings.zoom.enabled = false; transitions.start(lerp.property(plane.material, "opacity", 1 ) , e.toPano.duration*0.4 , ()=>{ }, 0, easing['easeInOutQuad']); }, e.toPano.duration*0.6); //时间上不能和消失的重叠 延迟 }else { //消失 transitions.start(lerp.property(plane.material, "opacity", 0, ) , e.toPano.duration*0.4, ()=>{ if(!info){ plane.visible = false; video.pause(); Potree.settings.zoom.enabled = true; } }, 0, easing['easeInOutQuad']); } }); } var startPlay = ()=>{ video.play(); //video.pause() //video.currentTime = 0.1; this.removeEventListener('global_mousedown', startPlay); }; this.addEventListener('global_mousedown', startPlay); Potree.settings.isTest && plane.addEventListener('select',(e)=>{console.log(e);}); }; videoInfo.forEach(info=>{ add(info); }); /* this.images360.addEventListener('flyToPano' ,(e)=>{//飞之前 if(Potree.settings.displayMode != 'showPanos') return let info = videoInfo[e.toPano.pano.id] if(info ){ //出现 setTimeout(()=>{ plane.visible = true; plane.position.fromArray(info.position) plane.rotation.fromArray(info.rotation) video.src = Potree.resourcePath+`/video/${Potree.settings.number}/${e.toPano.pano.id}.mp4` video.play(); video.currentTime = 0 Potree.settings.zoom.enabled = false transitions.start(lerp.property(plane.material, "opacity", 1 ) , e.toPano.duration*0.4 , ()=>{ }, 0, easing['easeInOutQuad']) }, e.toPano.duration*0.6) //时间上不能和消失的重叠 延迟 } //消失 transitions.start(lerp.property(plane.material, "opacity", 0, ) , e.toPano.duration*0.4, ()=>{ if(!info){ plane.visible = false video.pause() Potree.settings.zoom.enabled = true } }, 0, easing['easeInOutQuad']) }) this.images360.addEventListener('endChangeMode',(e)=>{ //暂时不处理初始加载时就在有视频的点位上的情况 if(e.mode == 'showPanos'){ let info = videoInfo[this.images360.currentPano.id] if(info ){ //出现 plane.visible = true; plane.position.fromArray(info.position) plane.rotation.fromArray(info.rotation) plane.material.opacity = 0 video.src = Potree.resourcePath+`/video/${Potree.settings.number}/${this.images360.currentPano.id}.mp4` video.play(); video.currentTime = 0 Potree.settings.zoom.enabled = false transitions.start(lerp.property(plane.material, "opacity", 1, (e)=>{console.log('fadeIn',e)}) , 300 , ()=>{ }, 0, easing['easeInOutQuad']) } }else{ plane.visible = false; Potree.settings.zoom.enabled = true } }) */ } /* addTube(){//加水管 自动生成 if(Potree.settings.number == 't-8KbK1JjubE'){ let boundingBox = new THREE.Box3() boundingBox.min.set(-1,-1,-1); boundingBox.max.set(1,1,1) let radius = 0.08; let radialSegments = 5 let radSegments = Math.PI*2 / radialSegments var circlePts = [];//横截面 for(let i=0;i{//height:在path之上的高度,负数代表在path之下 var name = 'cylinder'+count var mat = new THREE.MeshStandardMaterial({color, depthTest:false, roughness:0.4,metalness:0.5})  let linePath = path.map(e=>new THREE.Vector3().copy(e).setZ(e.z+height)) let geo = MeshDraw.getExtrudeGeo( circlePts, null,{ extrudePath:linePath, tension:0.2} ) var mesh = new THREE.Mesh(geo,mat); mesh.name = name window[name] = mesh mesh.boundingBox = boundingBox mesh.matrixAutoUpdate = false mesh.matrix.copy(viewer.scene.pointclouds[0].transformMatrix) mesh.matrixWorldNeedsUpdate = true this.scene.scene.add(mesh); count ++ } let linePath, height //地上管子 黄色 linePath = [{"x":-109.83,"y":-68.33,"z":-7.52},{"x":-95.17,"y":-59.3,"z":-7.38}, {"x":-38.75,"y":-24.01,"z":-6.01},{"x":0.5,"y":0.19,"z":-3.89},{"x":39.29,"y":24.41,"z":-1.31} ,{"x":43.58,"y":27.7,"z":-0.97},{"x":40.22,"y":35.37,"z":-0.67}// 拐弯向右 , {"x":39.18,"y":36.71,"z":0.35},{"x":38.69,"y":36.04,"z":18.04} // 拐弯向上 ] height = radius + 0.05; addMesh('#b86', linePath, height) //地下管子 藍色 linePath = [{"x":-108.24,"y":-70.61,"z":-7.52}, {"x":-57.8,"y":-39.31,"z":-6.72},{"x":-18.8,"y":-15.35,"z":-5.01},{"x":55.87,"y":31.67,"z":-0.04},{"x":110.53,"y":66.48,"z":5.14} ] height = -0.5; addMesh('#48a', linePath, height) } } */ addTube(datas){//加水管 自动生成 for(let number in datas){ if(number == Potree.settings.number){ let data = datas[number]; let boundingBox = new Box3(); boundingBox.min.set(-1,-1,-1); boundingBox.max.set(1,1,1); var index = 0; var addMesh = ({color, path, height,fromDataset, radius=0.08, datasetId=Potree.settings.originDatasetId, spaceDis, tension=0.1, visiEntity}={})=>{//height:在path之上的高度,负数代表在path之下 var name = 'tube_'+index; let radialSegments = 30;//THREE.Math.clamp( Math.round(radius * 800), 10, 40) let radSegments = Math.PI*2 / radialSegments; var circlePts = [];//横截面 for(let i=0;i{ e instanceof Array && (e = new Vector3().fromArray(e)); let pos; if(fromDataset){ //获取:JSON.stringify(viewer.scene.measurements[0].dataset_points.map(e=>Potree.math.toPrecision(e.toArray(),3))) pos = Potree.Utils.datasetPosTransform({ fromDataset: true, position:e, datasetId}); }else { pos = new Vector3().copy(e); } return pos.setZ(pos.z+height) }); console.log(linePath); let geo = MeshDraw.getExtrudeGeo( circlePts, null,{ extrudePath:linePath, tension, spaceDis} ); var mesh = new Mesh(geo,mat); mesh.name = name; window[name] = mesh; mesh.boundingBox = boundingBox; mesh.matrixAutoUpdate = false; fromDataset || mesh.matrix.copy(viewer.scene.pointclouds[0].transformMatrix); mesh.matrixWorldNeedsUpdate = true; this.scene.scene.add(mesh); if(visiEntity){ this.modules.SiteModel.bus.addEventListener('buildingChange',(e)=>{ Potree.Utils.updateVisible(mesh,'isInEntity', e.entity && e.entity.name == visiEntity); }); } }; while(index < data.length){ addMesh(data[index]); index++; } } } /* 可能用到的 viewer.scene.measurements.forEach(e=>e.edgeLabels.forEach(e=>Potree.Utils.updateVisible(e,'f',false))) JSON.stringify(viewer.scene.measurements.find(e=>e.visible).dataset_points.map(e=>Potree.math.toPrecision(e.toArray(),3))) */ } addSprite(e){//api let sprite; if(e.text != void 0){ sprite = new TextSprite(e ); }else { let map = texLoader.load(src); e.map = map; sprite = new Sprite(e ); } return sprite } }; //------ CLIP 默认clipTask都是clipInside ---------------------- /* 并集相当于加法,交集相当于乘法。 所有结果都能展开成多个乘积相加。 假设有4个clipBoxes,ABCD, 如果是 A*B + C*D ,那么这是最终结果。 如果是 (A+B)*(C+D) = A*C+A*D+B*C+B*D */ /* let Clips = { boxes : [], unionGroups : [], //二维数组。最外层要求并集,里层要求交集(如果只有一个元素就是本身)。总结起来就是要求一堆交集的并集 shaderParams:{}, needsUpdate : true, addClip(box, clipMethod){ //不允许重复 if(this.boxes.includes(box)){ return console.warn('addClip重复添加了box',box) } boxes.push(box) if(clipMethod == 'any'){//并 this.unionGroups.push([box]) }else if(clipMethod == 'all'){//交 this.unionGroups.forEach(mixGroup=>mixGroup.push(box)) } this.needsUpdate = true }, removeClip(box){ if(!this.boxes.includes(box)){ return console.warn('removeClip没有找到该box',box) } var newGroups = []; this.unionGroups.forEach(mixGroup=>{ if(mixGroup.length == 1 && mixGroup[0] == box)return;//直接删除 newGroups.push(mixGroup.filter(e=>e!=box)); }) this.unionGroups = newGroups; this.needsUpdate = true } , clearClip(){ this.boxes = []; this.unionGroups = [] this.needsUpdate = true } , updateShaderParams(){//没写完 - - 见 pointcloud clip.vs //uniform mat4 clipBoxes[num_clipboxes]; //uniform int clipBoxGroupCount; //uniform int mixClipIndices[clipboxGroupItemCount]; //把所有的要求都直接放到数组内 //这里需要转为Float32Array..? 参考material.setClipBoxes let everyClipGroupCount = this.unionGroups.map(e=>e.length) let mixClipIndices = [] this.unionGroups.forEach(e=>{ mixClipIndices.push(...e) }) this.shaderParams = { num_clipboxes : this.boxes.length, clipBoxGroupCount : this.unionGroups.length, everyClipGroupCount, clipBoxIndexCount: mixClipIndices.length, mixClipIndices } } , getShaderParams(){//每次要传递参数到shader中,执行这个就好 if(this.needsUpdate){ this.updateShaderParams() } return this.shaderParams } } */ /** * @author mschuetz / http://mschuetz.at * * adapted from THREE.OrbitControls by * * @author qiao / https://github.com/qiao * @author mrdoob / http://mrdoob.com * @author alteredq / http://alteredqualia.com/ * @author WestLangley / http://github.com/WestLangley * @author erich666 / http://erichaines.com * * * */ class OrbitControls$1 extends EventDispatcher$1{ constructor(viewer){ super(); this.viewer = viewer; this.renderer = viewer.renderer; this.scene = null; this.sceneControls = new Scene(); this.rotationSpeed = 5; this.fadeFactor = 20; this.yawDelta = 0; this.pitchDelta = 0; this.panDelta = new Vector2(0, 0); this.radiusDelta = 0; this.doubleClockZoomEnabled = true; this.tweens = []; let drag = (e) => { if (e.drag.object !== null) { return; } if (e.drag.startHandled === undefined) { e.drag.startHandled = true; this.dispatchEvent({type: 'start'}); } let ndrag = { x: e.drag.lastDrag.x / this.renderer.domElement.clientWidth, y: e.drag.lastDrag.y / this.renderer.domElement.clientHeight }; if (e.drag.mouse === MOUSE$1.LEFT) { this.yawDelta += ndrag.x * this.rotationSpeed; this.pitchDelta += ndrag.y * this.rotationSpeed; this.stopTweens(); } else if (e.drag.mouse === MOUSE$1.RIGHT) { this.panDelta.x += ndrag.x; this.panDelta.y += ndrag.y; this.stopTweens(); } }; let drop = e => { this.dispatchEvent({type: 'end'}); }; let scroll = (e) => { let resolvedRadius = this.scene.view.radius + this.radiusDelta; this.radiusDelta += -e.delta * resolvedRadius * 0.1; this.stopTweens(); }; let dblclick = (e) => { if(this.doubleClockZoomEnabled){ this.zoomToLocation(e.mouse); } }; let previousTouch = null; let touchStart = e => { previousTouch = e; }; let touchEnd = e => { previousTouch = e; }; let touchMove = e => { if (e.touches.length === 2 && previousTouch.touches.length === 2){ let prev = previousTouch; let curr = e; let prevDX = prev.touches[0].pageX - prev.touches[1].pageX; let prevDY = prev.touches[0].pageY - prev.touches[1].pageY; let prevDist = Math.sqrt(prevDX * prevDX + prevDY * prevDY); let currDX = curr.touches[0].pageX - curr.touches[1].pageX; let currDY = curr.touches[0].pageY - curr.touches[1].pageY; let currDist = Math.sqrt(currDX * currDX + currDY * currDY); let delta = currDist / prevDist; let resolvedRadius = this.scene.view.radius + this.radiusDelta; let newRadius = resolvedRadius / delta; this.radiusDelta = newRadius - resolvedRadius; this.stopTweens(); }else if(e.touches.length === 3 && previousTouch.touches.length === 3){ let prev = previousTouch; let curr = e; let prevMeanX = (prev.touches[0].pageX + prev.touches[1].pageX + prev.touches[2].pageX) / 3; let prevMeanY = (prev.touches[0].pageY + prev.touches[1].pageY + prev.touches[2].pageY) / 3; let currMeanX = (curr.touches[0].pageX + curr.touches[1].pageX + curr.touches[2].pageX) / 3; let currMeanY = (curr.touches[0].pageY + curr.touches[1].pageY + curr.touches[2].pageY) / 3; let delta = { x: (currMeanX - prevMeanX) / this.renderer.domElement.clientWidth, y: (currMeanY - prevMeanY) / this.renderer.domElement.clientHeight }; this.panDelta.x += delta.x; this.panDelta.y += delta.y; this.stopTweens(); } previousTouch = e; }; this.addEventListener('touchstart', touchStart); this.addEventListener('touchend', touchEnd); this.addEventListener('touchmove', touchMove); this.addEventListener('drag', drag); this.addEventListener('drop', drop); this.addEventListener('mousewheel', scroll); this.addEventListener('dblclick', dblclick); } setScene (scene) { this.scene = scene; } stop(){ this.yawDelta = 0; this.pitchDelta = 0; this.radiusDelta = 0; this.panDelta.set(0, 0); } zoomToLocation(mouse){ let camera = this.scene.getActiveCamera(); let I = Utils.getMousePointCloudIntersection( mouse, camera, this.viewer, this.scene.pointclouds, {pickClipped: true}); if (I === null) { return; } let targetRadius = 0; { let minimumJumpDistance = 0.2; let domElement = this.renderer.domElement; let ray = Utils.mouseToRay(mouse, camera, domElement.clientWidth, domElement.clientHeight); let nodes = I.pointcloud.nodesOnRay(I.pointcloud.visibleNodes, ray); let lastNode = nodes[nodes.length - 1]; let radius = lastNode.getBoundingSphere(new Sphere()).radius; targetRadius = Math.min(this.scene.view.radius, radius); targetRadius = Math.max(minimumJumpDistance, targetRadius); } let d = this.scene.view.direction.multiplyScalar(-1); let cameraTargetPosition = new Vector3().addVectors(I.location, d.multiplyScalar(targetRadius)); // TODO Unused: let controlsTargetPosition = I.location; let animationDuration = 600; let easing = TWEEN.Easing.Quartic.Out; { // animate let value = {x: 0}; let tween = new TWEEN.Tween(value).to({x: 1}, animationDuration); tween.easing(easing); this.tweens.push(tween); let startPos = this.scene.view.position.clone(); let targetPos = cameraTargetPosition.clone(); let startRadius = this.scene.view.radius; let targetRadius = cameraTargetPosition.distanceTo(I.location); tween.onUpdate(() => { let t = value.x; this.scene.view.position.x = (1 - t) * startPos.x + t * targetPos.x; this.scene.view.position.y = (1 - t) * startPos.y + t * targetPos.y; this.scene.view.position.z = (1 - t) * startPos.z + t * targetPos.z; this.scene.view.radius = (1 - t) * startRadius + t * targetRadius; this.viewer.setMoveSpeed(this.scene.view.radius); }); tween.onComplete(() => { this.tweens = this.tweens.filter(e => e !== tween); }); tween.start(); } } stopTweens () { this.tweens.forEach(e => e.stop()); this.tweens = []; } update (delta) { let view = this.scene.view; { // apply rotation let progression = Math.min(1, this.fadeFactor * delta); let yaw = view.yaw; let pitch = view.pitch; let pivot = view.getPivot(); yaw -= progression * this.yawDelta; pitch -= progression * this.pitchDelta; view.yaw = yaw; view.pitch = pitch; let V = this.scene.view.direction.multiplyScalar(-view.radius); let position = new Vector3().addVectors(pivot, V); view.position.copy(position); } { // apply pan let progression = Math.min(1, this.fadeFactor * delta); let panDistance = progression * view.radius * 3; let px = -this.panDelta.x * panDistance; let py = this.panDelta.y * panDistance; view.pan(px, py); } { // apply zoom let progression = Math.min(1, this.fadeFactor * delta); // let radius = view.radius + progression * this.radiusDelta * view.radius * 0.1; let radius = view.radius + progression * this.radiusDelta; let V = view.direction.multiplyScalar(-radius); let position = new Vector3().addVectors(view.getPivot(), V); view.radius = radius; view.position.copy(position); } { let speed = view.radius; this.viewer.setMoveSpeed(speed); } { // decelerate over time let progression = Math.min(1, this.fadeFactor * delta); let attenuation = Math.max(0, 1 - this.fadeFactor * delta); this.yawDelta *= attenuation; this.pitchDelta *= attenuation; this.panDelta.multiplyScalar(attenuation); // this.radiusDelta *= attenuation; this.radiusDelta -= progression * this.radiusDelta; } } }; /** * @author mschuetz / http://mschuetz.at * * adapted from THREE.OrbitControls by * * @author qiao / https://github.com/qiao * @author mrdoob / http://mrdoob.com * @author alteredq / http://alteredqualia.com/ * @author WestLangley / http://github.com/WestLangley * @author erich666 / http://erichaines.com * * * */ class FirstPersonControls$1 extends EventDispatcher$1 {//old constructor (viewer) { super(); this.viewer = viewer; this.renderer = viewer.renderer; this.scene = null; this.sceneControls = new Scene(); this.rotationSpeed = 200; this.moveSpeed = 10; this.lockElevation = false; this.keys = { FORWARD: ['W'.charCodeAt(0), 38], BACKWARD: ['S'.charCodeAt(0), 40], LEFT: ['A'.charCodeAt(0), 37], RIGHT: ['D'.charCodeAt(0), 39], UP: ['R'.charCodeAt(0), 33], DOWN: ['F'.charCodeAt(0), 34] }; this.fadeFactor = 50; this.yawDelta = 0; this.pitchDelta = 0; this.translationDelta = new Vector3(0, 0, 0); this.translationWorldDelta = new Vector3(0, 0, 0); this.tweens = []; let drag = (e) => { if (e.drag.object !== null) { return; } if (e.drag.startHandled === undefined) { e.drag.startHandled = true; this.dispatchEvent({type: 'start'}); } let moveSpeed = this.viewer.getMoveSpeed(); let ndrag = { x: e.drag.lastDrag.x / this.renderer.domElement.clientWidth, y: e.drag.lastDrag.y / this.renderer.domElement.clientHeight }; if (e.drag.mouse === MOUSE$1.LEFT) { this.yawDelta += ndrag.x * this.rotationSpeed; this.pitchDelta += ndrag.y * this.rotationSpeed; } else if (e.drag.mouse === MOUSE$1.RIGHT) { this.translationDelta.x -= ndrag.x * moveSpeed * 100; this.translationDelta.z += ndrag.y * moveSpeed * 100; } }; let drop = e => { this.dispatchEvent({type: 'end'}); }; let scroll = (e) => { let speed = this.viewer.getMoveSpeed(); if (e.delta < 0) { speed = speed * 0.9; } else if (e.delta > 0) { speed = speed / 0.9; } speed = Math.max(speed, 0.1); this.viewer.setMoveSpeed(speed); }; let dblclick = (e) => { this.zoomToLocation(e.mouse); }; this.addEventListener('drag', drag); this.addEventListener('drop', drop); this.addEventListener('mousewheel', scroll); this.addEventListener('dblclick', dblclick); } setScene (scene) { this.scene = scene; } stop(){ this.yawDelta = 0; this.pitchDelta = 0; this.translationDelta.set(0, 0, 0); } zoomToLocation(mouse){ let camera = this.scene.getActiveCamera(); let I = Utils.getMousePointCloudIntersection( mouse, camera, this.viewer, this.scene.pointclouds); if (I === null) { return; } let targetRadius = 0; { let minimumJumpDistance = 0.2; let domElement = this.renderer.domElement; let ray = Utils.mouseToRay(mouse, camera, domElement.clientWidth, domElement.clientHeight); let nodes = I.pointcloud.nodesOnRay(I.pointcloud.visibleNodes, ray); let lastNode = nodes[nodes.length - 1]; let radius = lastNode.getBoundingSphere(new Sphere()).radius; targetRadius = Math.min(this.scene.view.radius, radius); targetRadius = Math.max(minimumJumpDistance, targetRadius); } let d = this.scene.view.direction.multiplyScalar(-1); let cameraTargetPosition = new Vector3().addVectors(I.location, d.multiplyScalar(targetRadius)); // TODO Unused: let controlsTargetPosition = I.location; let animationDuration = 600; let easing = TWEEN.Easing.Quartic.Out; { // animate let value = {x: 0}; let tween = new TWEEN.Tween(value).to({x: 1}, animationDuration); tween.easing(easing); this.tweens.push(tween); let startPos = this.scene.view.position.clone(); let targetPos = cameraTargetPosition.clone(); let startRadius = this.scene.view.radius; let targetRadius = cameraTargetPosition.distanceTo(I.location); tween.onUpdate(() => { let t = value.x; this.scene.view.position.x = (1 - t) * startPos.x + t * targetPos.x; this.scene.view.position.y = (1 - t) * startPos.y + t * targetPos.y; this.scene.view.position.z = (1 - t) * startPos.z + t * targetPos.z; this.scene.view.radius = (1 - t) * startRadius + t * targetRadius; this.viewer.setMoveSpeed(this.scene.view.radius / 2.5); }); tween.onComplete(() => { this.tweens = this.tweens.filter(e => e !== tween); }); tween.start(); } } update (delta) { let view = this.scene.view; { // cancel move animations on user input let changes = [ this.yawDelta, this.pitchDelta, this.translationDelta.length(), this.translationWorldDelta.length() ]; let changeHappens = changes.some(e => Math.abs(e) > 0.001); if (changeHappens && this.tweens.length > 0) { this.tweens.forEach(e => e.stop()); this.tweens = []; } } { // accelerate while input is given let ih = this.viewer.inputHandler; let moveForward = this.keys.FORWARD.some(e => ih.pressedKeys[e]); let moveBackward = this.keys.BACKWARD.some(e => ih.pressedKeys[e]); let moveLeft = this.keys.LEFT.some(e => ih.pressedKeys[e]); let moveRight = this.keys.RIGHT.some(e => ih.pressedKeys[e]); let moveUp = this.keys.UP.some(e => ih.pressedKeys[e]); let moveDown = this.keys.DOWN.some(e => ih.pressedKeys[e]); if(this.lockElevation){ let dir = view.direction; dir.z = 0; dir.normalize(); if (moveForward && moveBackward) { this.translationWorldDelta.set(0, 0, 0); } else if (moveForward) { this.translationWorldDelta.copy(dir.multiplyScalar(this.viewer.getMoveSpeed())); } else if (moveBackward) { this.translationWorldDelta.copy(dir.multiplyScalar(-this.viewer.getMoveSpeed())); } }else { if (moveForward && moveBackward) { this.translationDelta.y = 0; } else if (moveForward) { this.translationDelta.y = this.viewer.getMoveSpeed(); } else if (moveBackward) { this.translationDelta.y = -this.viewer.getMoveSpeed(); } } if (moveLeft && moveRight) { this.translationDelta.x = 0; } else if (moveLeft) { this.translationDelta.x = -this.viewer.getMoveSpeed(); } else if (moveRight) { this.translationDelta.x = this.viewer.getMoveSpeed(); } if (moveUp && moveDown) { this.translationWorldDelta.z = 0; } else if (moveUp) { this.translationWorldDelta.z = this.viewer.getMoveSpeed(); } else if (moveDown) { this.translationWorldDelta.z = -this.viewer.getMoveSpeed(); } } { // apply rotation let yaw = view.yaw; let pitch = view.pitch; yaw -= this.yawDelta * delta; pitch -= this.pitchDelta * delta; view.yaw = yaw; view.pitch = pitch; } { // apply translation view.translate( this.translationDelta.x * delta, this.translationDelta.y * delta, this.translationDelta.z * delta ); view.translateWorld( this.translationWorldDelta.x * delta, this.translationWorldDelta.y * delta, this.translationWorldDelta.z * delta ); } { // set view target according to speed view.radius = 3 * this.viewer.getMoveSpeed(); } { // decelerate over time let attenuation = Math.max(0, 1 - this.fadeFactor * delta); this.yawDelta *= attenuation; this.pitchDelta *= attenuation; this.translationDelta.multiplyScalar(attenuation); this.translationWorldDelta.multiplyScalar(attenuation); } } }; OrthographicCamera.prototype.zoomTo = function( node, factor = 1){ if ( !node.geometry && !node.boundingBox) { return; } // TODO //let minWS = new THREE.Vector4(node.boundingBox.min.x, node.boundingBox.min.y, node.boundingBox.min.z, 1); //let minVS = minWS.applyMatrix4(this.matrixWorldInverse); //let right = node.boundingBox.max.x; //let bottom = node.boundingBox.min.y; //let top = node.boundingBox.max.y; this.updateProjectionMatrix(); }; PerspectiveCamera.prototype.zoomTo = function (node, factor) { if (!node.geometry && !node.boundingSphere && !node.boundingBox) { return; } if (node.geometry && node.geometry.boundingSphere === null) { node.geometry.computeBoundingSphere(); } node.updateMatrixWorld(); let bs; if (node.boundingSphere) { bs = node.boundingSphere; } else if (node.geometry && node.geometry.boundingSphere) { bs = node.geometry.boundingSphere; } else { bs = node.boundingBox.getBoundingSphere(new Sphere()); } let _factor = factor || 1; bs = bs.clone().applyMatrix4(node.matrixWorld); let radius = bs.radius; let fovr = this.fov * Math.PI / 180; if (this.aspect < 1) { fovr = fovr * this.aspect; } let distanceFactor = Math.abs(radius / Math.sin(fovr / 2)) * _factor; let offset = this.getWorldDirection(new Vector3()).multiplyScalar(-distanceFactor); this.position.copy(bs.center.clone().add(offset)); }; Ray.prototype.distanceToPlaneWithNegative = function (plane) { let denominator = plane.normal.dot(this.direction); if (denominator === 0) { // line is coplanar, return origin if (plane.distanceToPoint(this.origin) === 0) { return 0; } // Null is preferable to undefined since undefined means.... it is undefined return null; } let t = -(this.origin.dot(plane.normal) + plane.constant) / denominator; return t; }; const workerPool = new WorkerPool(); const version = { major: 1, minor: 8, suffix: '.0' }; let lru = new LRU(); console.log('Potree ' + version.major + '.' + version.minor + version.suffix); let pointBudget = 1 * 1000 * 1000; let framenumber = 0; let numNodesLoading = 0; let maxNodesLoading = 6;//4; const debug = {}; exports.scriptPath = ""; if (document.currentScript && document.currentScript.src) { exports.scriptPath = new URL(document.currentScript.src + '/..').href; if (exports.scriptPath.slice(-1) === '/') { exports.scriptPath = exports.scriptPath.slice(0, -1); } } else if(({ url: (typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('potree.js', document.baseURI).href)) })){ exports.scriptPath = new URL((typeof document === 'undefined' ? new (require('u' + 'rl').URL)('file:' + __filename).href : (document.currentScript && document.currentScript.src || new URL('potree.js', document.baseURI).href)) + "/..").href; if (exports.scriptPath.slice(-1) === '/') { exports.scriptPath = exports.scriptPath.slice(0, -1); } }else { console.error('Potree was unable to find its script path using document.currentScript. Is Potree included with a script tag? Does your browser support this function?'); } let resourcePath = exports.scriptPath + '/resources'; //add: async function loadFile(path, params , callback, onError){ params = params || {}; let fetchMethod = params.fetchMethod || 'get'; delete params.fetchMethod; if(Potree.fileServer){ Potree.fileServer[fetchMethod](path, { params }).then(data=>{ if(data.data)data = data.data; if(data.data)data = data.data; //融合页面getdataset需要查找两次data callback && callback(data); }).catch(onError); }else { try{ if(Object.keys(params).length > 0){ path+='?'; let index = 0; for(let i in params){ if(index>0) path += '&'; path+=i; path+='='; path+=params[i]; index ++; } } let info={}; if(fetchMethod == 'post')info.method = 'POST'; let response = await fetch(path, info); let text = await response.text(); var data = params.returnText ? text : JSON.parse(text); if(data.data) data = data.data; callback && callback(data); return data }catch(e){ console.log('loadFile出错', e); onError && onError(e); } } //查询: http://192.168.0.26:8080/doc.html#/default/filter-%E6%BC%AB%E6%B8%B8%E7%82%B9/filterUsingGET } async function loadDatasets(callback,sceneCode,onError){//之后直接把path写进来 let path; sceneCode = sceneCode || Potree.settings.number; if(Potree.fileServer){ path = `/laser/dataset/${sceneCode}/getDataSet`; }else { //path = `${Potree.settings.urls.prefix2}/indoor/${Potree.settings.number}/api/datasets` //现在只能加载得了本地的了 path = `${Potree.settings.urls.prefix}/laser/dataset/${sceneCode}/getDataSet`; //path = `${Potree.scriptPath}/data/${sceneCode}/getDataSet.json` } return loadFile(path, null, callback,onError) } //目前上传平面图后如果不点击保存按钮,数据还是旧的不生效 async function loadMapEntity(datasetId, force){ if(!Potree.settings.floorplanEnable && !force && Potree.fileServer )return /* 等待平面图类型定义好会加载 */ let loaded = 0; let needLoads = datasetId == 'all' ? viewer.scene.pointclouds.map(e=>e.dataset_id) : [datasetId]; let callback = (dataset_id, floorplanType, data )=>{ if(!data || data.length == 0)return console.log('平面图没有数据', dataset_id, floorplanType ) //要防止旧的比新的先获取到导致覆盖新的,因为两种type随时可能切换 if(floorplanType != Potree.settings.floorplanType[dataset_id]) return //如果请求的floorplanType不是当前最新的floorplanType就返回 var map = viewer.mapViewer.mapLayer.maps.find(e => e.name == 'floorplan_'+ dataset_id); if(map){ viewer.mapViewer.mapLayer.removeMap(map); } var mapNew = viewer.mapViewer.mapLayer.addMapEntity(data.data || data, dataset_id); if(map){ mapNew.visibleReasons = map.visibleReasons; mapNew.unvisibleReasons = map.unvisibleReasons; } loaded ++; }; needLoads.forEach(dataset_id=>{ let floorplanType = Potree.settings.floorplanType[dataset_id], prefix = ''; if(!Potree.fileServer){ prefix = Potree.settings.urls.prefix; } if(!floorplanType)return var path; /* if(Potree.fileServer){ path = `/laser/tiledMap/${Potree.settings.number}/tiledMap/${floorplanType}/${dataset_id}` }else{ path = `${Potree.settings.urls.prefix2}/indoor/${Potree.settings.number}/api/tiled_maps` } */ path = `${prefix}/laser/tiledMap/${Potree.settings.number}/tiledMap/${floorplanType}/${dataset_id}`; Potree.settings.floorplanRequests[dataset_id] = true; //开始加载了 return loadFile(path, null, callback.bind(this, dataset_id, floorplanType) ) }); } async function loadPanos(datasetId, callback){ var path; //let query = `?datasetId=${datasetId}` //`?lat=${center.lat}&lon=${center.lon}&radius=200000` if(Potree.fileServer){ path = `/laser/filter/${Potree.settings.number}/query`; }else { //path = `${Potree.settings.urls.prefix2}/indoor/${Potree.settings.number}/api/images/filter` + query //path = `${Potree.scriptPath}/data/${Potree.settings.number}/panos-${datasetId}.json` path = `${Potree.settings.urls.prefix}/laser/filter/${Potree.settings.number}/query`; } return loadFile(path, {datasetId:datasetId}, callback) } async function loadPanosInfo(callback){ var path; if(Potree.fileServer){ }else { path = `${Potree.scriptPath}/data/panoEdit/vision_edit.txt`; } return loadFile(path, null, callback) } async function loadImgVersion( callback){ var path; //let query = `?datasetId=${datasetId}` //`?lat=${center.lat}&lon=${center.lon}&radius=200000` /* if(Potree.fileServer){ path = `/laser/filter/${Potree.settings.number}/query` }else{ */ path = `/laser/init/getSceneNumVersion/${Potree.settings.number}`; //api/laser/init //} return loadFile(path, {fetchMethod:'post'}, callback, callback) } //site_model /* { "area": 2503.30551910935, "attributes": {}, "center": [ 113.59568277455075, 22.366566635195288, 12.78751625 ], "children": [], "geometry_hash": 1891071345, "id": 10, "name": "港湾一号", "parentId": null, "polygon": { "coordinates": [ [ [ 113.59590810534583, 22.36679132753878 ], [ 113.59590810534583, 22.366807172528629 ], [ 113.59545610274934, 22.366807172528629 ], [ 113.59545610274934, 22.36679132753878 ] ] ], "type": "Polygon" }, "type": "BUILDING", "volume": null, "z_max": null, "z_min": null } */ function Log$1(){ let args = Array.from(arguments); let params = args[args.length-1]; if(params && params.font) {params = params.font, args.pop();} else params = {}; let str = '', color = params.color || '#13f', fontSize = params.fontSize || 12; args.forEach((e,i)=>{ i > 0 && (str += ' , ' ); /* if(params.toFixed && typeof e == 'number'){ e = e.toFixed(params.toFixed) } */ if(params.toFixed ){ e = Potree.math.toPrecision(e, params.toFixed); } str += e; //object可以JSON.stringify,但不是所有都行 }); console.warn(`%c${str}`, `color:${color};font-size:${fontSize}px`); } function loadPointCloud$1(path, name, sceneCode, timeStamp, callback, onError){ let loaded = function(e){ e.pointcloud.name = name; e.pointcloud.sceneCode = sceneCode; //对应4dkk的场景码 callback(e); }; let promise = new Promise( resolve => { // load pointcloud if (!path){ // TODO: callback? comment? Hello? Bueller? Anyone? } else if (path.indexOf('ept.json') > 0) { EptLoader.load(path, function(geometry) { if (!geometry) { console.error(new Error(`failed to load point cloud from URL: ${path}`)); } else { let pointcloud = new ExtendPointCloudOctree(geometry); //loaded(pointcloud); resolve({type: 'pointcloud_loaded', pointcloud: pointcloud}); } }); } else if (path.indexOf('cloud.js') > 0) { POCLoader.load(path, timeStamp, function (geometry) { if (!geometry) { //callback({type: 'loading_failed'}); console.error(new Error(`failed to load point cloud from URL: ${path}`)); onError && onError(); } else { let pointcloud = new ExtendPointCloudOctree(geometry); // loaded(pointcloud); resolve({type: 'pointcloud_loaded', pointcloud: pointcloud}); } }); }/* else if (path.indexOf('metadata.json') > 0) { //部分浏览器(如uc)不支持NodeLoader中的1n的大数据写法 Potree.OctreeLoader.load(path).then(e => { let geometry = e.geometry; if(!geometry){ console.error(new Error(`failed to load point cloud from URL: ${path}`)); }else{ let pointcloud = new ExtendPointCloudOctree(geometry); let aPosition = pointcloud.getAttribute("position"); let material = pointcloud.material; material.elevationRange = [ aPosition.range[0][2], aPosition.range[1][2], ]; // loaded(pointcloud); resolve({type: 'pointcloud_loaded', pointcloud: pointcloud}); } }); OctreeLoader.load(path, function (geometry) { if (!geometry) { //callback({type: 'loading_failed'}); console.error(new Error(`failed to load point cloud from URL: ${path}`)); } else { let pointcloud = new ExtendPointCloudOctree(geometry); // loaded(pointcloud); resolve({type: 'pointcloud_loaded', pointcloud: pointcloud}); } }); } */else if (path.indexOf('.vpc') > 0) { PointCloudArena4DGeometry.load(path, function (geometry) { if (!geometry) { //callback({type: 'loading_failed'}); console.error(new Error(`failed to load point cloud from URL: ${path}`)); } else { let pointcloud = new PointCloudArena4D(geometry); // loaded(pointcloud); resolve({type: 'pointcloud_loaded', pointcloud: pointcloud}); } }); } else { //callback({'type': 'loading_failed'}); console.error(new Error(`failed to load point cloud from URL: ${path}`)); } }); if(callback){ promise.then(pointcloud => { loaded(pointcloud); }); }else { return promise; } }; // add selectgroup (function($){ $.fn.extend({ selectgroup: function(args = {}){ let elGroup = $(this); let rootID = elGroup.prop("id"); let groupID = `${rootID}`; let groupTitle = (args.title !== undefined) ? args.title : ""; let elButtons = []; elGroup.find("option").each((index, value) => { let buttonID = $(value).prop("id"); let label = $(value).html(); let optionValue = $(value).prop("value"); let elButton = $(` `); let elLabel = elButton.find("label"); let elInput = elButton.find("input"); elInput.change( () => { elGroup.find("label").removeClass("ui-state-active"); elGroup.find("label").addClass("ui-state-default"); if(elInput.is(":checked")){ elLabel.addClass("ui-state-active"); }else { //elLabel.addClass("ui-state-default"); } }); elButtons.push(elButton); }); let elFieldset = $(`
    ${groupTitle}
    `); let elButtonContainer = elFieldset.find("span"); for(let elButton of elButtons){ elButtonContainer.append(elButton); } elButtonContainer.find("label").each( (index, value) => { $(value).css("margin", "0px"); $(value).css("border-radius", "0px"); $(value).css("border", "1px solid black"); $(value).css("border-left", "none"); }); elButtonContainer.find("label:first").each( (index, value) => { $(value).css("border-radius", "4px 0px 0px 4px"); }); elButtonContainer.find("label:last").each( (index, value) => { $(value).css("border-radius", "0px 4px 4px 0px"); $(value).css("border-left", "none"); }); elGroup.empty(); elGroup.append(elFieldset); } }); })(jQuery); //在这之后export的内容才赋值到Potree中 exports.Action = Action; exports.Annotation = Annotation; exports.Box3Helper = Box3Helper$1; exports.BoxVolume = BoxVolume$1; exports.CameraAnimation = CameraAnimation$1; exports.CameraMode = CameraMode; exports.ClassificationScheme = ClassificationScheme; exports.ClipMethod = ClipMethod; exports.ClipTask = ClipTask; exports.ClipVolume = ClipVolume; exports.ClippingTool = ClippingTool; exports.ElevationGradientRepeat = ElevationGradientRepeat; exports.Enum = Enum; exports.EnumItem = EnumItem; exports.EptBinaryLoader = EptBinaryLoader; exports.EptKey = EptKey; exports.EptLaszipLoader = EptLaszipLoader; exports.EptLazBatcher = EptLazBatcher; exports.EptLoader = EptLoader; exports.EptZstandardLoader = EptZstandardLoader; exports.EventDispatcher = EventDispatcher$1; exports.ExtendPointCloudMaterial = ExtendPointCloudMaterial; exports.ExtendPointCloudOctree = ExtendPointCloudOctree; exports.ExtendScene = ExtendScene; exports.EyeDomeLightingMaterial = EyeDomeLightingMaterial; exports.Features = Features; exports.FirstPersonControls = FirstPersonControls$1; exports.GeoPackageLoader = GeoPackageLoader; exports.Geopackage = Geopackage$1; exports.Gradients = Gradients; exports.HierarchicalSlider = HierarchicalSlider; exports.Images360 = Images360; exports.KeyCodes = KeyCodes; exports.LRU = LRU; exports.LRUItem = LRUItem; exports.LengthUnits = LengthUnits; exports.LineDraw = LineDraw; exports.Log = Log$1; exports.MOUSE = MOUSE$1; exports.MapViewer = MapViewer; exports.Measure = Measure$1; exports.MeasuringTool = MeasuringTool; exports.MeshDraw = MeshDraw; exports.Message = Message; exports.NodeLoader = NodeLoader; exports.NormalizationEDLMaterial = NormalizationEDLMaterial; exports.NormalizationMaterial = NormalizationMaterial; exports.OctreeLoader = OctreeLoader; exports.OrbitControls = OrbitControls$1; exports.OrientedImage = OrientedImage; exports.OrientedImageLoader = OrientedImageLoader; exports.OrientedImages = OrientedImages; exports.POCLoader = POCLoader; exports.PointAttribute = PointAttribute; exports.PointAttributeTypes = PointAttributeTypes; exports.PointAttributes = PointAttributes; exports.PointCloudEptGeometry = PointCloudEptGeometry; exports.PointCloudEptGeometryNode = PointCloudEptGeometryNode; exports.PointCloudOctreeGeometry = PointCloudOctreeGeometry; exports.PointCloudOctreeGeometryNode = PointCloudOctreeGeometryNode; exports.PointCloudSM = PointCloudSM; exports.PointCloudTree = PointCloudTree; exports.PointCloudTreeNode = PointCloudTreeNode; exports.PointShape = PointShape$1; exports.PointSizeType = PointSizeType; exports.Points = Points$1; exports.PolygonClipVolume = PolygonClipVolume; exports.Profile = Profile; exports.ProfileData = ProfileData; exports.ProfileRequest = ProfileRequest; exports.ProfileTool = ProfileTool; exports.Renderer = Renderer; exports.ScreenBoxSelectTool = ScreenBoxSelectTool; exports.ShapefileLoader = ShapefileLoader; exports.SphereVolume = SphereVolume$2; exports.SpotLightHelper = SpotLightHelper$1; exports.TextSprite = TextSprite$2; exports.TransformationTool = TransformationTool; exports.TreeType = TreeType; exports.Utils = Utils; exports.Version = Version; exports.Viewer = Viewer; exports.Volume = Volume$1; exports.VolumeTool = VolumeTool; exports.WorkerPool = WorkerPool; exports.XHRFactory = XHRFactory; exports.config = config$1; exports.debug = debug; exports.framenumber = framenumber; exports.loadDatasets = loadDatasets; exports.loadFile = loadFile; exports.loadImgVersion = loadImgVersion; exports.loadMapEntity = loadMapEntity; exports.loadPanos = loadPanos; exports.loadPanosInfo = loadPanosInfo; exports.loadPointCloud = loadPointCloud$1; exports.loadProject = loadProject; exports.lru = lru; exports.maxNodesLoading = maxNodesLoading; exports.mergeEditStart = mergeEditStart; exports.numNodesLoading = numNodesLoading; exports.panoEditStart = panoEditStart; exports.pointBudget = pointBudget; exports.resourcePath = resourcePath; exports.saveProject = saveProject; exports.settings = settings; exports.start = start; exports.version = version; exports.workerPool = workerPool; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=potree.js.map