+}
+
+VG_STATIC void world_routes_gen_meshes( world_instance *world, u32 route_id,
+ scene_context *sc,
+ pointcloud_buffer *pcbuf )
+{
+ ent_route *route = mdl_arritm( &world->ent_route, route_id );
+ u8 colour[4];
+ colour[0] = route->colour[0] * 255.0f;
+ colour[1] = route->colour[1] * 255.0f;
+ colour[2] = route->colour[2] * 255.0f;
+ colour[3] = route->colour[3] * 255.0f;
+
+ u32 last_valid = 0;
+
+ for( int i=0; i<route->checkpoints_count; i++ ){
+ int i0 = route->checkpoints_start+i,
+ i1 = route->checkpoints_start+((i+1)%route->checkpoints_count);
+
+ ent_checkpoint *c0 = mdl_arritm(&world->ent_checkpoint, i0),
+ *c1 = mdl_arritm(&world->ent_checkpoint, i1);
+
+ ent_gate *start_gate = mdl_arritm( &world->ent_gate, c0->gate_index );
+ start_gate = mdl_arritm( &world->ent_gate, start_gate->target );
+
+ ent_gate *end_gate = mdl_arritm( &world->ent_gate, c1->gate_index ),
+ *collector = mdl_arritm( &world->ent_gate, end_gate->target );
+
+ v4f p[3];
+
+ v3_add( (v3f){0.0f,0.1f,0.0f}, start_gate->co[0], p[0] );
+ p[0][3] = start_gate->ref_count;
+ p[0][3] -= (float)start_gate->route_count * 0.5f;
+ start_gate->ref_count ++;
+
+ if( !c0->path_count )
+ continue;
+
+ /* this is so that we get nice flow through the gates */
+ v3f temp_alignments[2];
+ ent_gate *both[] = { start_gate, end_gate };
+
+ for( int j=0; j<2; j++ ){
+ int pi = c0->path_start + ((j==1)? c0->path_count-1: 0);
+
+ ent_path_index *index = mdl_arritm( &world->ent_path_index, pi );
+ ent_route_node *rn = mdl_arritm( &world->ent_route_node,
+ index->index );
+ v3f v0;
+ v3_sub( rn->co, both[j]->co[0], v0 );
+ float d = v3_dot( v0, both[j]->to_world[2] );
+
+ v3_muladds( both[j]->co[0], both[j]->to_world[2], d,
+ temp_alignments[j] );
+ v3_add( (v3f){0.0f,0.1f,0.0f}, temp_alignments[j], temp_alignments[j]);
+ }
+
+
+ for( int j=0; j<c0->path_count; j ++ ){
+ ent_path_index *index = mdl_arritm( &world->ent_path_index,
+ c0->path_start+j );
+ ent_route_node *rn = mdl_arritm( &world->ent_route_node,
+ index->index );
+ if( j==0 || j==c0->path_count-1 )
+ if( j == 0 )
+ v3_copy( temp_alignments[0], p[1] );
+ else
+ v3_copy( temp_alignments[1], p[1] );
+ else
+ v3_copy( rn->co, p[1] );
+
+ p[1][3] = rn->ref_count;
+ p[1][3] -= (float)rn->ref_total * 0.5f;
+ rn->ref_count ++;
+
+ if( j+1 < c0->path_count ){
+ index = mdl_arritm( &world->ent_path_index,
+ c0->path_start+j+1 );
+ rn = mdl_arritm( &world->ent_route_node, index->index );
+
+ if( j+1 == c0->path_count-1 )
+ v3_lerp( p[1], temp_alignments[1], 0.5f, p[2] );
+ else
+ v3_lerp( p[1], rn->co, 0.5f, p[2] );
+
+ p[2][3] = rn->ref_count;
+ p[2][3] -= (float)rn->ref_total * 0.5f;
+ }
+ else{
+ v3_copy( end_gate->co[0], p[2] );
+ v3_add( (v3f){0.0f,0.1f,0.0f}, p[2], p[2] );
+ p[2][3] = collector->ref_count;
+
+ if( i == route->checkpoints_count-1)
+ p[2][3] -= 1.0f;
+
+ p[2][3] -= (float)collector->route_count * 0.5f;
+ //collector->ref_count ++;
+ }
+
+ /* p0,p1,p2 bezier patch is complete
+ * --------------------------------------*/
+ v3f surf0, surf2, n0, n2;
+
+ if( bh_closest_point( world->geo_bh, p[0], surf0, 5.0f ) == -1 )
+ v3_add( (v3f){0.0f,-0.1f,0.0f}, p[0], surf0 );
+
+ if( bh_closest_point( world->geo_bh, p[2], surf2, 5.0f ) == -1 )
+ v3_add( (v3f){0.0f,-0.1f,0.0f}, p[2], surf2 );
+
+ v3_sub( surf0, p[0], n0 );
+ v3_sub( surf2, p[2], n2 );
+ v3_normalize( n0 );
+ v3_normalize( n2 );
+
+ world_routes_place_curve( world, route, p, n0, n2, sc, pcbuf );
+
+ /* --- */
+ v4_copy( p[2], p[0] );
+ }
+ }
+
+ scene_copy_slice( sc, &route->sm );
+}
+
+VG_STATIC
+struct world_surface *world_tri_index_surface( world_instance *world,
+ u32 index );
+
+VG_STATIC f64 world_routes_scatter_surface_points( world_instance *world,
+ pointcloud_buffer *pcbuf,
+ f32 rate )
+{
+ static f32 densities[] = {
+ [k_surface_prop_concrete] = 2.0f,
+ [k_surface_prop_grass] = 0.8f,
+ [k_surface_prop_metal] = 1.0f,
+ [k_surface_prop_wood] = 2.5f,
+ [k_surface_prop_tiles] = 4.0f
+ };
+
+ /* calculate total area */
+ f64 total_area = 0.0f;
+ for( u32 i=0; i<world->scene_geo.indice_count/3; i++ ){
+ u32 *tri = &world->scene_geo.arrindices[i*3];
+ struct world_surface *surf = world_tri_index_surface( world, tri[0] );
+
+ if( surf->info.shader == k_shader_boundary ||
+ surf->info.shader == k_shader_invisible ) continue;
+
+ if( !(surf->info.flags & k_material_flag_preview_visibile) ) continue;
+
+ scene_vert *va = &world->scene_geo.arrvertices[tri[0]],
+ *vb = &world->scene_geo.arrvertices[tri[1]],
+ *vc = &world->scene_geo.arrvertices[tri[2]];
+
+ v3f v0, v1, vn;
+ v3_sub( vb->co, va->co, v0 );
+ v3_sub( vc->co, va->co, v1 );
+ v3_cross( v0, v1, vn );
+ if( vn[1] < 0.0f ) continue;
+
+ f32 density = 1.0f;
+ if( surf->info.surface_prop < vg_list_size(densities) )
+ density = densities[surf->info.surface_prop];
+ total_area += v3_length(vn)*0.5f*density;
+ }
+
+ f32 accum = 0.0f;
+
+ u8 colour[] = { 80,80,80,255 };
+ v3f light_dir = {0.3f,0.8f,0.1f};
+ v3_normalize( light_dir );
+
+ v3f inv_ext;
+ v3_sub( pcbuf->boundary[1], pcbuf->boundary[0], inv_ext );
+ v3_div( (v3f){1.0f,1.0f,1.0f}, inv_ext, inv_ext );
+
+ for( u32 i=0; i<world->scene_geo.indice_count/3; i++ ){
+ u32 *tri = &world->scene_geo.arrindices[i*3];
+ struct world_surface *surf = world_tri_index_surface( world, tri[0] );
+
+ if( surf->info.shader == k_shader_boundary ||
+ surf->info.shader == k_shader_invisible ) continue;
+
+ if( !(surf->info.flags & k_material_flag_preview_visibile) ) continue;
+
+ scene_vert *va = &world->scene_geo.arrvertices[tri[0]],
+ *vb = &world->scene_geo.arrvertices[tri[1]],
+ *vc = &world->scene_geo.arrvertices[tri[2]];
+
+ v3f v0, v1, vn;
+ v3_sub( vb->co, va->co, v0 );
+ v3_sub( vc->co, va->co, v1 );
+ v3_cross( v0, v1, vn );
+ if( vn[1] < 0.0f ) continue;
+
+ f32 density = 1.0f;
+ if( surf->info.surface_prop < vg_list_size(densities) )
+ density = densities[surf->info.surface_prop];