11import DepthMode from '../gl/depth_mode' ;
22import StencilMode from '../gl/stencil_mode' ;
33import CullFaceMode from '../gl/cull_face_mode' ;
4- import { collisionUniformValues , collisionCircleUniformValues } from './program/collision_program' ;
4+ import { collisionUniformValues , collisionCircleUniformValues , type CollisionDebugDefinesType } from './program/collision_program' ;
55import { QuadTriangleArray , CollisionCircleLayoutArray } from '../data/array_types' ;
66import { collisionCircleLayout } from '../data/bucket/symbol_attributes' ;
77import SegmentVector from '../data/segment' ;
88import { mat4 } from 'gl-matrix' ;
99import { getCollisionDebugTileProjectionMatrix } from '../geo/projection/projection_util' ;
10+ import { globeToMercatorTransition } from '../geo/projection/globe_util' ;
11+ import { mercatorXfromLng , mercatorYfromLat } from '../geo/mercator_coordinate' ;
1012
1113import type VertexBuffer from '../gl/vertex_buffer' ;
1214import type IndexBuffer from '../gl/index_buffer' ;
1315import type Painter from './painter' ;
1416import type SourceCache from '../source/source_cache' ;
15- import type { TypedStyleLayer } from '../style/style_layer/typed_style_layer' ;
1617import type { OverscaledTileID } from '../source/tile_id' ;
1718import type SymbolBucket from '../data/bucket/symbol_bucket' ;
1819import type Projection from '../geo/projection/projection' ;
20+ import type SymbolStyleLayer from '../style/style_layer/symbol_style_layer' ;
1921
2022export default drawCollisionDebug ;
2123
@@ -29,11 +31,20 @@ type TileBatch = {
2931
3032let quadTriangles : QuadTriangleArray | null | undefined ;
3133
32- function drawCollisionDebug ( painter : Painter , sourceCache : SourceCache , layer : TypedStyleLayer , coords : Array < OverscaledTileID > , translate : [ number , number ] , translateAnchor : 'map' | 'viewport' , isText : boolean ) {
34+ function drawCollisionDebug ( painter : Painter , sourceCache : SourceCache , layer : SymbolStyleLayer , coords : Array < OverscaledTileID > , translate : [ number , number ] , translateAnchor : 'map' | 'viewport' , isText : boolean ) {
3335 const context = painter . context ;
3436 const gl = context . gl ;
3537 const tr = painter . transform ;
36- const program = painter . getOrCreateProgram ( 'collisionBox' ) ;
38+ const mercatorCenter : [ number , number ] = [
39+ mercatorXfromLng ( tr . center . lng ) ,
40+ mercatorYfromLat ( tr . center . lat )
41+ ] ;
42+ const symbolPlacement = layer . layout . get ( 'symbol-placement' ) ;
43+ const textVariableAnchor = layer . layout . get ( 'text-variable-anchor' ) ;
44+ const iconRotateWithMap = layer . layout . get ( 'icon-rotation-alignment' ) === 'map' ;
45+ const textRotateWithMap = layer . layout . get ( 'text-rotation-alignment' ) === 'map' ;
46+
47+ const alongLine = symbolPlacement !== 'point' ;
3748 const tileBatches : Array < TileBatch > = [ ] ;
3849 let circleCount = 0 ;
3950 let circleOffset = 0 ;
@@ -44,7 +55,26 @@ function drawCollisionDebug(painter: Painter, sourceCache: SourceCache, layer: T
4455 const bucket = tile . getBucket ( layer ) as SymbolBucket ;
4556 if ( ! bucket ) continue ;
4657
58+ const invMatrix = bucket . getProjection ( ) . createInversionMatrix ( tr , coord . canonical ) ;
59+ const defines : CollisionDebugDefinesType [ ] = [ ] ;
4760 const tileMatrix = getCollisionDebugTileProjectionMatrix ( coord , bucket , tr ) ;
61+ const isIconPlacedAlongLine = ! isText && iconRotateWithMap && alongLine ;
62+ const isTextPlacedAlongLine = isText && textRotateWithMap && alongLine ;
63+ const hasVariableAnchors = textVariableAnchor && bucket . hasTextData ( ) ;
64+ const updateIconTextFit = bucket . hasIconTextFit ( ) && hasVariableAnchors && bucket . hasIconData ( ) ;
65+ const projectedPosOnLabelSpace = isIconPlacedAlongLine || isTextPlacedAlongLine || ( isText && hasVariableAnchors ) || updateIconTextFit ;
66+ const bucketIsGlobeProjection = bucket . projection . name === 'globe' ;
67+ const globeToMercator = bucketIsGlobeProjection ? globeToMercatorTransition ( tr . zoom ) : 0.0 ;
68+
69+ if ( bucketIsGlobeProjection ) {
70+ defines . push ( 'PROJECTION_GLOBE_VIEW' ) ;
71+
72+ if ( projectedPosOnLabelSpace ) {
73+ defines . push ( 'PROJECTED_POS_ON_VIEWPORT' ) ;
74+ }
75+ }
76+
77+ const program = painter . getOrCreateProgram ( 'collisionBox' , { defines} ) ;
4878
4979 let posMatrix = tileMatrix ;
5080 if ( translate [ 0 ] !== 0 || translate [ 1 ] !== 0 ) {
@@ -76,11 +106,12 @@ function drawCollisionDebug(painter: Painter, sourceCache: SourceCache, layer: T
76106 }
77107 if ( ! buffers ) continue ;
78108 if ( painter . terrain ) painter . terrain . setupElevationDraw ( tile , program ) ;
109+ const tileId : [ number , number , number ] = bucketIsGlobeProjection ? [ coord . canonical . x , coord . canonical . y , 1 << coord . canonical . z ] : [ 0 , 0 , 0 ] ;
79110 program . draw ( painter , gl . LINES ,
80111 DepthMode . disabled , StencilMode . disabled ,
81112 painter . colorModeForRenderPass ( ) ,
82113 CullFaceMode . disabled ,
83- collisionUniformValues ( posMatrix , tr , tile , bucket . getProjection ( ) ) ,
114+ collisionUniformValues ( posMatrix , invMatrix , tr , globeToMercator , mercatorCenter , tile , tileId , bucket . getProjection ( ) ) ,
84115 layer . id , buffers . layoutVertexBuffer , buffers . indexBuffer ,
85116 buffers . segments , null , tr . zoom , null ,
86117 [ buffers . collisionVertexBuffer , buffers . collisionVertexBufferExt ] ) ;
0 commit comments