@ -1,76 +1,19 @@
@@ -1,76 +1,19 @@
function push _point ( s , x , y , ax , ay , bx , by , thickness , r , g , b , stroke _id ) {
ser _f32 ( s , x ) ;
ser _f32 ( s , y ) ;
ser _f32 ( s , thickness ) ;
ser _f32 ( s , ax ) ;
ser _f32 ( s , ay ) ;
ser _f32 ( s , bx ) ;
ser _f32 ( s , by ) ;
ser _u8 ( s , r ) ;
ser _u8 ( s , g ) ;
ser _u8 ( s , b ) ;
ser _align ( s , 4 ) ;
ser _u32 ( s , stroke _id ) ;
}
function push _quad ( s , p1x , p1y , p2x , p2y , p3x , p3y , p4x , p4y , ax , ay , bx , by , thickness , r , g , b , stroke _id ) {
push _point ( s , p1x , p1y , ax , ay , bx , by , thickness , r , g , b , stroke _id ) ;
push _point ( s , p2x , p2y , ax , ay , bx , by , thickness , r , g , b , stroke _id ) ;
push _point ( s , p3x , p3y , ax , ay , bx , by , thickness , r , g , b , stroke _id ) ;
push _point ( s , p4x , p4y , ax , ay , bx , by , thickness , r , g , b , stroke _id ) ;
}
function push _stroke ( s , stroke , stroke _index ) {
const stroke _width = stroke . width ;
const points = stroke . points ;
const color _u32 = stroke . color ;
const radius = stroke _width / 2 ;
if ( points . length < 2 ) {
return ;
}
const r = ( color _u32 >> 16 ) & 0xFF ;
const g = ( color _u32 >> 8 ) & 0xFF ;
const b = color _u32 & 0xFF ;
for ( let i = 0 ; i < points . length - 1 ; ++ i ) {
const from = points [ i ] ;
const to = points [ i + 1 ] ;
const dir _x = to . x - from . x ;
const dir _y = to . y - from . y ;
const len = Math . sqrt ( dir _x * dir _x + dir _y * dir _y ) ;
const dir1 _x = dir _x / len ;
const dir1 _y = dir _y / len ;
const up _x = dir _y / len ;
const up _y = - dir _x / len ;
let p1 _x = from . x + ( up _x - dir1 _x ) * radius ;
let p1 _y = from . y + ( up _y - dir1 _y ) * radius ;
let p2 _x = to . x + ( up _x + dir1 _x ) * radius ;
let p2 _y = to . y + ( up _y + dir1 _y ) * radius ;
let p3 _x = from . x + ( - up _x - dir1 _x ) * radius ;
let p3 _y = from . y + ( - up _y - dir1 _y ) * radius ;
let p4 _x = to . x + ( - up _x + dir1 _x ) * radius ;
let p4 _y = to . y + ( - up _y + dir1 _y ) * radius ;
push _quad ( s ,
p1 _x , p1 _y ,
p2 _x , p2 _y ,
p3 _x , p3 _y ,
p4 _x , p4 _y ,
from . x , from . y ,
to . x , to . y ,
stroke _width ,
r , g , b ,
stroke _index
) ;
ser _f32 ( s , from . x ) ;
ser _f32 ( s , from . y ) ;
ser _f32 ( s , to . x ) ;
ser _f32 ( s , to . y ) ;
ser _u32 ( s , stroke _index ) ;
}
}
@ -83,7 +26,7 @@ function geometry_prepare_stroke(state) {
@@ -83,7 +26,7 @@ function geometry_prepare_stroke(state) {
return null ;
}
const points = process _stroke ( state , state . players [ state . me ] . points ) ;
const points = process _stroke ( state , state . canvas . zoom , state . players [ state . me ] . points ) ;
return {
'color' : state . players [ state . me ] . color ,
@ -99,23 +42,10 @@ function geometry_add_stroke(state, context, stroke, stroke_index, skip_bvh = fa
@@ -99,23 +42,10 @@ function geometry_add_stroke(state, context, stroke, stroke_index, skip_bvh = fa
stroke . index = state . events . length ;
for ( let i = 0 ; i < config . lod _levels ; ++ i ) {
// TODO: just pass zoom to process_stroke ?
const saved _zoom = state . canvas . zoom ;
state . canvas . zoom = Math . pow ( 0.5 , i ) ;
const points = ( i > 0 ? process _stroke ( state , stroke . points ) : stroke . points ) ;
state . canvas . zoom = saved _zoom ;
const vertex _serializer = context . lods [ i ] . vertices = ser _ensure _by ( context . lods [ i ] . vertices , points . length * 4 * config . bytes _per _point ) ;
/ *
event . index = state . events . length ;
event . starting _index = state . starting _index ;
if ( event . points . length > 1 ) {
state . starting _index += ( event . points . length - 1 ) * 4 ;
}
const lod = context . lods [ i ] ;
state . total _points += event . points . length ;
* /
const points = ( i > 0 ? process _stroke ( state , lod . max _zoom , stroke . points ) : stroke . points ) ;
const segment _serializer = lod . segments = ser _ensure _by ( lod . segments , ( points . length - 1 ) * config . bytes _per _quad ) ;
let starting _index = 0 ;
@ -133,14 +63,26 @@ function geometry_add_stroke(state, context, stroke, stroke_index, skip_bvh = fa
@@ -133,14 +63,26 @@ function geometry_add_stroke(state, context, stroke, stroke_index, skip_bvh = fa
context . lods [ i ] . total _points += points . length ;
push _stroke ( vertex _serializer , stroke . lods [ stroke . lods . length - 1 ] , stroke _index ) ;
push _stroke ( segment _serializer , stroke . lods [ stroke . lods . length - 1 ] , stroke _index ) ;
if ( i === 0 ) {
stroke . bbox = stroke _bbox ( stroke ) ;
stroke . area = ( stroke . bbox . x2 - stroke . bbox . x1 ) * ( stroke . bbox . y2 - stroke . bbox . y1 ) ;
}
}
context . stroke _data = ser _ensure _by ( context . stroke _data , config . bytes _per _stroke ) ;
const color _u32 = stroke . color ;
const r = ( color _u32 >> 16 ) & 0xFF ;
const g = ( color _u32 >> 8 ) & 0xFF ;
const b = color _u32 & 0xFF ;
ser _u16 ( context . stroke _data , r ) ;
ser _u16 ( context . stroke _data , g ) ;
ser _u16 ( context . stroke _data , b ) ;
ser _u16 ( context . stroke _data , stroke . width ) ;
if ( ! skip _bvh ) bvh _add _stroke ( state . bvh , stroke _index , stroke ) ;
}