static inline void _plot_face(uint8_t*** p_cell_status,int x,int y,int z,int len_x,int len_y,int len_z,const Vector3& voxelsize,const Face3& p_face) { AABB aabb( Vector3(x,y,z),Vector3(len_x,len_y,len_z)); aabb.pos=aabb.pos*voxelsize; aabb.size=aabb.size*voxelsize; if (!p_face.intersects_aabb(aabb)) return; if (len_x==1 && len_y==1 && len_z==1) { p_cell_status[x][y][z]=_CELL_SOLID; return; } int div_x=len_x>1?2:1; int div_y=len_y>1?2:1; int div_z=len_z>1?2:1; #define _SPLIT(m_i,m_div,m_v,m_len_v,m_new_v,m_new_len_v)\ if (m_div==1) {\ m_new_v=m_v;\ m_new_len_v=1; \ } else if (m_i==0) {\ m_new_v=m_v;\ m_new_len_v=m_len_v/2;\ } else {\ m_new_v=m_v+m_len_v/2;\ m_new_len_v=m_len_v-m_len_v/2; \ } int new_x; int new_len_x; int new_y; int new_len_y; int new_z; int new_len_z; for (int i=0;i<div_x;i++) { _SPLIT(i,div_x,x,len_x,new_x,new_len_x); for (int j=0;j<div_y;j++) { _SPLIT(j,div_y,y,len_y,new_y,new_len_y); for (int k=0;k<div_z;k++) { _SPLIT(k,div_z,z,len_z,new_z,new_len_z); _plot_face(p_cell_status,new_x,new_y,new_z,new_len_x,new_len_y,new_len_z,voxelsize,p_face); } } } }
void BakedLight::_plot_face(int p_idx, int p_level, const Vector3 *p_vtx, const Vector2* p_uv, const MaterialCache& p_material, const Rect3 &p_aabb) { if (p_level==cell_subdiv-1) { //plot the face by guessing it's albedo and emission value //find best axis to map to, for scanning values int closest_axis; float closest_dot; Vector3 normal = Plane(p_vtx[0],p_vtx[1],p_vtx[2]).normal; for(int i=0;i<3;i++) { Vector3 axis; axis[i]=1.0; float dot=ABS(normal.dot(axis)); if (i==0 || dot>closest_dot) { closest_axis=i; closest_dot=dot; } } Vector3 axis; axis[closest_axis]=1.0; Vector3 t1; t1[(closest_axis+1)%3]=1.0; Vector3 t2; t2[(closest_axis+2)%3]=1.0; t1*=p_aabb.size[(closest_axis+1)%3]/float(color_scan_cell_width); t2*=p_aabb.size[(closest_axis+2)%3]/float(color_scan_cell_width); Color albedo_accum; Color emission_accum; float alpha=0.0; //map to a grid average in the best axis for this face for(int i=0;i<color_scan_cell_width;i++) { Vector3 ofs_i=float(i)*t1; for(int j=0;j<color_scan_cell_width;j++) { Vector3 ofs_j=float(j)*t2; Vector3 from = p_aabb.pos+ofs_i+ofs_j; Vector3 to = from + t1 + t2 + axis * p_aabb.size[closest_axis]; Vector3 half = (to-from)*0.5; //is in this cell? if (!fast_tri_box_overlap(from+half,half,p_vtx)) { continue; //face does not span this cell } //go from -size to +size*2 to avoid skipping collisions Vector3 ray_from = from + (t1+t2)*0.5 - axis * p_aabb.size[closest_axis]; Vector3 ray_to = ray_from + axis * p_aabb.size[closest_axis]*2; Vector3 intersection; if (!Geometry::ray_intersects_triangle(ray_from,ray_to,p_vtx[0],p_vtx[1],p_vtx[2],&intersection)) { //no intersect? look in edges float closest_dist=1e20; for(int j=0;j<3;j++) { Vector3 c; Vector3 inters; Geometry::get_closest_points_between_segments(p_vtx[j],p_vtx[(j+1)%3],ray_from,ray_to,inters,c); float d=c.distance_to(intersection); if (j==0 || d<closest_dist) { closest_dist=d; intersection=inters; } } } Vector2 uv = get_uv(intersection,p_vtx,p_uv); int uv_x = CLAMP(Math::fposmod(uv.x,1.0)*bake_texture_size,0,bake_texture_size-1); int uv_y = CLAMP(Math::fposmod(uv.y,1.0)*bake_texture_size,0,bake_texture_size-1); int ofs = uv_y*bake_texture_size+uv_x; albedo_accum.r+=p_material.albedo[ofs].r; albedo_accum.g+=p_material.albedo[ofs].g; albedo_accum.b+=p_material.albedo[ofs].b; albedo_accum.a+=p_material.albedo[ofs].a; emission_accum.r+=p_material.emission[ofs].r; emission_accum.g+=p_material.emission[ofs].g; emission_accum.b+=p_material.emission[ofs].b; alpha+=1.0; } } if (alpha==0) { //could not in any way get texture information.. so use closest point to center Face3 f( p_vtx[0],p_vtx[1],p_vtx[2]); Vector3 inters = f.get_closest_point_to(p_aabb.pos+p_aabb.size*0.5); Vector2 uv = get_uv(inters,p_vtx,p_uv); int uv_x = CLAMP(Math::fposmod(uv.x,1.0)*bake_texture_size,0,bake_texture_size-1); int uv_y = CLAMP(Math::fposmod(uv.y,1.0)*bake_texture_size,0,bake_texture_size-1); int ofs = uv_y*bake_texture_size+uv_x; alpha = 1.0/(color_scan_cell_width*color_scan_cell_width); albedo_accum.r=p_material.albedo[ofs].r*alpha; albedo_accum.g=p_material.albedo[ofs].g*alpha; albedo_accum.b=p_material.albedo[ofs].b*alpha; albedo_accum.a=p_material.albedo[ofs].a*alpha; emission_accum.r=p_material.emission[ofs].r*alpha; emission_accum.g=p_material.emission[ofs].g*alpha; emission_accum.b=p_material.emission[ofs].b*alpha; zero_alphas++; } else { float accdiv = 1.0/(color_scan_cell_width*color_scan_cell_width); alpha*=accdiv; albedo_accum.r*=accdiv; albedo_accum.g*=accdiv; albedo_accum.b*=accdiv; albedo_accum.a*=accdiv; emission_accum.r*=accdiv; emission_accum.g*=accdiv; emission_accum.b*=accdiv; } //put this temporarily here, corrected in a later step bake_cells_write[p_idx].albedo[0]+=albedo_accum.r; bake_cells_write[p_idx].albedo[1]+=albedo_accum.g; bake_cells_write[p_idx].albedo[2]+=albedo_accum.b; bake_cells_write[p_idx].light[0]+=emission_accum.r; bake_cells_write[p_idx].light[1]+=emission_accum.g; bake_cells_write[p_idx].light[2]+=emission_accum.b; bake_cells_write[p_idx].alpha+=alpha; static const Vector3 side_normals[6]={ Vector3(-1, 0, 0), Vector3( 1, 0, 0), Vector3( 0,-1, 0), Vector3( 0, 1, 0), Vector3( 0, 0,-1), Vector3( 0, 0, 1), }; for(int i=0;i<6;i++) { if (normal.dot(side_normals[i])>CMP_EPSILON) { bake_cells_write[p_idx].used_sides|=(1<<i); } } } else { //go down for(int i=0;i<8;i++) { Rect3 aabb=p_aabb; aabb.size*=0.5; if (i&1) aabb.pos.x+=aabb.size.x; if (i&2) aabb.pos.y+=aabb.size.y; if (i&4) aabb.pos.z+=aabb.size.z; { Rect3 test_aabb=aabb; //test_aabb.grow_by(test_aabb.get_longest_axis_size()*0.05); //grow a bit to avoid numerical error in real-time Vector3 qsize = test_aabb.size*0.5; //quarter size, for fast aabb test if (!fast_tri_box_overlap(test_aabb.pos+qsize,qsize,p_vtx)) { //if (!Face3(p_vtx[0],p_vtx[1],p_vtx[2]).intersects_aabb2(aabb)) { //does not fit in child, go on continue; } } if (bake_cells_write[p_idx].childs[i]==CHILD_EMPTY) { //sub cell must be created if (bake_cells_used==(1<<bake_cells_alloc)) { //exhausted cells, creating more space bake_cells_alloc++; bake_cells_write=PoolVector<BakeCell>::Write(); bake_cells.resize(1<<bake_cells_alloc); bake_cells_write=bake_cells.write(); } bake_cells_write[p_idx].childs[i]=bake_cells_used; bake_cells_level_used[p_level+1]++; bake_cells_used++; } _plot_face(bake_cells_write[p_idx].childs[i],p_level+1,p_vtx,p_uv,p_material,aabb); } } }
PoolVector<Face3> Geometry::wrap_geometry(PoolVector<Face3> p_array, real_t *p_error) { #define _MIN_SIZE 1.0 #define _MAX_LENGTH 20 int face_count = p_array.size(); PoolVector<Face3>::Read facesr = p_array.read(); const Face3 *faces = facesr.ptr(); Rect3 global_aabb; for (int i = 0; i < face_count; i++) { if (i == 0) { global_aabb = faces[i].get_aabb(); } else { global_aabb.merge_with(faces[i].get_aabb()); } } global_aabb.grow_by(0.01); // avoid numerical error // determine amount of cells in grid axis int div_x, div_y, div_z; if (global_aabb.size.x / _MIN_SIZE < _MAX_LENGTH) div_x = (int)(global_aabb.size.x / _MIN_SIZE) + 1; else div_x = _MAX_LENGTH; if (global_aabb.size.y / _MIN_SIZE < _MAX_LENGTH) div_y = (int)(global_aabb.size.y / _MIN_SIZE) + 1; else div_y = _MAX_LENGTH; if (global_aabb.size.z / _MIN_SIZE < _MAX_LENGTH) div_z = (int)(global_aabb.size.z / _MIN_SIZE) + 1; else div_z = _MAX_LENGTH; Vector3 voxelsize = global_aabb.size; voxelsize.x /= div_x; voxelsize.y /= div_y; voxelsize.z /= div_z; // create and initialize cells to zero //print_line("Wrapper: Initializing Cells"); uint8_t ***cell_status = memnew_arr(uint8_t **, div_x); for (int i = 0; i < div_x; i++) { cell_status[i] = memnew_arr(uint8_t *, div_y); for (int j = 0; j < div_y; j++) { cell_status[i][j] = memnew_arr(uint8_t, div_z); for (int k = 0; k < div_z; k++) { cell_status[i][j][k] = 0; } } } // plot faces into cells //print_line("Wrapper (1/6): Plotting Faces"); for (int i = 0; i < face_count; i++) { Face3 f = faces[i]; for (int j = 0; j < 3; j++) { f.vertex[j] -= global_aabb.pos; } _plot_face(cell_status, 0, 0, 0, div_x, div_y, div_z, voxelsize, f); } // determine which cells connect to the outside by traversing the outside and recursively flood-fill marking //print_line("Wrapper (2/6): Flood Filling"); for (int i = 0; i < div_x; i++) { for (int j = 0; j < div_y; j++) { _mark_outside(cell_status, i, j, 0, div_x, div_y, div_z); _mark_outside(cell_status, i, j, div_z - 1, div_x, div_y, div_z); } } for (int i = 0; i < div_z; i++) { for (int j = 0; j < div_y; j++) { _mark_outside(cell_status, 0, j, i, div_x, div_y, div_z); _mark_outside(cell_status, div_x - 1, j, i, div_x, div_y, div_z); } } for (int i = 0; i < div_x; i++) { for (int j = 0; j < div_z; j++) { _mark_outside(cell_status, i, 0, j, div_x, div_y, div_z); _mark_outside(cell_status, i, div_y - 1, j, div_x, div_y, div_z); } } // build faces for the inside-outside cell divisors //print_line("Wrapper (3/6): Building Faces"); PoolVector<Face3> wrapped_faces; for (int i = 0; i < div_x; i++) { for (int j = 0; j < div_y; j++) { for (int k = 0; k < div_z; k++) { _build_faces(cell_status, i, j, k, div_x, div_y, div_z, wrapped_faces); } } } //print_line("Wrapper (4/6): Transforming Back Vertices"); // transform face vertices to global coords int wrapped_faces_count = wrapped_faces.size(); PoolVector<Face3>::Write wrapped_facesw = wrapped_faces.write(); Face3 *wrapped_faces_ptr = wrapped_facesw.ptr(); for (int i = 0; i < wrapped_faces_count; i++) { for (int j = 0; j < 3; j++) { Vector3 &v = wrapped_faces_ptr[i].vertex[j]; v = v * voxelsize; v += global_aabb.pos; } } // clean up grid //print_line("Wrapper (5/6): Grid Cleanup"); for (int i = 0; i < div_x; i++) { for (int j = 0; j < div_y; j++) { memdelete_arr(cell_status[i][j]); } memdelete_arr(cell_status[i]); } memdelete_arr(cell_status); if (p_error) *p_error = voxelsize.length(); //print_line("Wrapper (6/6): Finished."); return wrapped_faces; }
void GIProbe::_plot_mesh(const Transform& p_xform, Ref<Mesh>& p_mesh, Baker *p_baker, const Vector<Ref<Material> > &p_materials, const Ref<Material> &p_override_material) { for(int i=0;i<p_mesh->get_surface_count();i++) { if (p_mesh->surface_get_primitive_type(i)!=Mesh::PRIMITIVE_TRIANGLES) continue; //only triangles Ref<Material> src_material; if (p_override_material.is_valid()) { src_material=p_override_material; } else if (i<p_materials.size() && p_materials[i].is_valid()) { src_material=p_materials[i]; } else { src_material=p_mesh->surface_get_material(i); } Baker::MaterialCache material = _get_material_cache(src_material,p_baker); Array a = p_mesh->surface_get_arrays(i); PoolVector<Vector3> vertices = a[Mesh::ARRAY_VERTEX]; PoolVector<Vector3>::Read vr=vertices.read(); PoolVector<Vector2> uv = a[Mesh::ARRAY_TEX_UV]; PoolVector<Vector2>::Read uvr; PoolVector<int> index = a[Mesh::ARRAY_INDEX]; bool read_uv=false; if (uv.size()) { uvr=uv.read(); read_uv=true; } if (index.size()) { int facecount = index.size()/3; PoolVector<int>::Read ir=index.read(); for(int j=0;j<facecount;j++) { Vector3 vtxs[3]; Vector2 uvs[3]; for(int k=0;k<3;k++) { vtxs[k]=p_xform.xform(vr[ir[j*3+k]]); } if (read_uv) { for(int k=0;k<3;k++) { uvs[k]=uvr[ir[j*3+k]]; } } //test against original bounds if (!fast_tri_box_overlap(-extents,extents*2,vtxs)) continue; //plot _plot_face(0,0,0,0,0,vtxs,uvs,material,p_baker->po2_bounds,p_baker); } } else { int facecount = vertices.size()/3; for(int j=0;j<facecount;j++) { Vector3 vtxs[3]; Vector2 uvs[3]; for(int k=0;k<3;k++) { vtxs[k]=p_xform.xform(vr[j*3+k]); } if (read_uv) { for(int k=0;k<3;k++) { uvs[k]=uvr[j*3+k]; } } //test against original bounds if (!fast_tri_box_overlap(-extents,extents*2,vtxs)) continue; //plot face _plot_face(0,0,0,0,0,vtxs,uvs,material,p_baker->po2_bounds,p_baker); } } } }
void GIProbe::_plot_face(int p_idx, int p_level,int p_x,int p_y,int p_z, const Vector3 *p_vtx, const Vector2* p_uv, const Baker::MaterialCache& p_material, const Rect3 &p_aabb,Baker *p_baker) { if (p_level==p_baker->cell_subdiv-1) { //plot the face by guessing it's albedo and emission value //find best axis to map to, for scanning values int closest_axis; float closest_dot; Vector3 normal = Plane(p_vtx[0],p_vtx[1],p_vtx[2]).normal; for(int i=0;i<3;i++) { Vector3 axis; axis[i]=1.0; float dot=ABS(normal.dot(axis)); if (i==0 || dot>closest_dot) { closest_axis=i; closest_dot=dot; } } Vector3 axis; axis[closest_axis]=1.0; Vector3 t1; t1[(closest_axis+1)%3]=1.0; Vector3 t2; t2[(closest_axis+2)%3]=1.0; t1*=p_aabb.size[(closest_axis+1)%3]/float(color_scan_cell_width); t2*=p_aabb.size[(closest_axis+2)%3]/float(color_scan_cell_width); Color albedo_accum; Color emission_accum; Vector3 normal_accum; float alpha=0.0; //map to a grid average in the best axis for this face for(int i=0;i<color_scan_cell_width;i++) { Vector3 ofs_i=float(i)*t1; for(int j=0;j<color_scan_cell_width;j++) { Vector3 ofs_j=float(j)*t2; Vector3 from = p_aabb.pos+ofs_i+ofs_j; Vector3 to = from + t1 + t2 + axis * p_aabb.size[closest_axis]; Vector3 half = (to-from)*0.5; //is in this cell? if (!fast_tri_box_overlap(from+half,half,p_vtx)) { continue; //face does not span this cell } //go from -size to +size*2 to avoid skipping collisions Vector3 ray_from = from + (t1+t2)*0.5 - axis * p_aabb.size[closest_axis]; Vector3 ray_to = ray_from + axis * p_aabb.size[closest_axis]*2; Vector3 intersection; if (!Geometry::ray_intersects_triangle(ray_from,ray_to,p_vtx[0],p_vtx[1],p_vtx[2],&intersection)) { //no intersect? look in edges float closest_dist=1e20; for(int j=0;j<3;j++) { Vector3 c; Vector3 inters; Geometry::get_closest_points_between_segments(p_vtx[j],p_vtx[(j+1)%3],ray_from,ray_to,inters,c); float d=c.distance_to(intersection); if (j==0 || d<closest_dist) { closest_dist=d; intersection=inters; } } } Vector2 uv = get_uv(intersection,p_vtx,p_uv); int uv_x = CLAMP(Math::fposmod(uv.x,1.0f)*bake_texture_size,0,bake_texture_size-1); int uv_y = CLAMP(Math::fposmod(uv.y,1.0f)*bake_texture_size,0,bake_texture_size-1); int ofs = uv_y*bake_texture_size+uv_x; albedo_accum.r+=p_material.albedo[ofs].r; albedo_accum.g+=p_material.albedo[ofs].g; albedo_accum.b+=p_material.albedo[ofs].b; albedo_accum.a+=p_material.albedo[ofs].a; emission_accum.r+=p_material.emission[ofs].r; emission_accum.g+=p_material.emission[ofs].g; emission_accum.b+=p_material.emission[ofs].b; normal_accum+=normal; alpha+=1.0; } } if (alpha==0) { //could not in any way get texture information.. so use closest point to center Face3 f( p_vtx[0],p_vtx[1],p_vtx[2]); Vector3 inters = f.get_closest_point_to(p_aabb.pos+p_aabb.size*0.5); Vector2 uv = get_uv(inters,p_vtx,p_uv); int uv_x = CLAMP(Math::fposmod(uv.x,1.0f)*bake_texture_size,0,bake_texture_size-1); int uv_y = CLAMP(Math::fposmod(uv.y,1.0f)*bake_texture_size,0,bake_texture_size-1); int ofs = uv_y*bake_texture_size+uv_x; alpha = 1.0/(color_scan_cell_width*color_scan_cell_width); albedo_accum.r=p_material.albedo[ofs].r*alpha; albedo_accum.g=p_material.albedo[ofs].g*alpha; albedo_accum.b=p_material.albedo[ofs].b*alpha; albedo_accum.a=p_material.albedo[ofs].a*alpha; emission_accum.r=p_material.emission[ofs].r*alpha; emission_accum.g=p_material.emission[ofs].g*alpha; emission_accum.b=p_material.emission[ofs].b*alpha; normal_accum*=alpha; } else { float accdiv = 1.0/(color_scan_cell_width*color_scan_cell_width); alpha*=accdiv; albedo_accum.r*=accdiv; albedo_accum.g*=accdiv; albedo_accum.b*=accdiv; albedo_accum.a*=accdiv; emission_accum.r*=accdiv; emission_accum.g*=accdiv; emission_accum.b*=accdiv; normal_accum*=accdiv; } //put this temporarily here, corrected in a later step p_baker->bake_cells[p_idx].albedo[0]+=albedo_accum.r; p_baker->bake_cells[p_idx].albedo[1]+=albedo_accum.g; p_baker->bake_cells[p_idx].albedo[2]+=albedo_accum.b; p_baker->bake_cells[p_idx].emission[0]+=emission_accum.r; p_baker->bake_cells[p_idx].emission[1]+=emission_accum.g; p_baker->bake_cells[p_idx].emission[2]+=emission_accum.b; p_baker->bake_cells[p_idx].normal[0]+=normal_accum.x; p_baker->bake_cells[p_idx].normal[1]+=normal_accum.y; p_baker->bake_cells[p_idx].normal[2]+=normal_accum.z; p_baker->bake_cells[p_idx].alpha+=alpha; static const Vector3 side_normals[6]={ Vector3(-1, 0, 0), Vector3( 1, 0, 0), Vector3( 0,-1, 0), Vector3( 0, 1, 0), Vector3( 0, 0,-1), Vector3( 0, 0, 1), }; /* for(int i=0;i<6;i++) { if (normal.dot(side_normals[i])>CMP_EPSILON) { p_baker->bake_cells[p_idx].used_sides|=(1<<i); } }*/ } else { //go down int half = (1<<(p_baker->cell_subdiv-1)) >> (p_level+1); for(int i=0;i<8;i++) { Rect3 aabb=p_aabb; aabb.size*=0.5; int nx=p_x; int ny=p_y; int nz=p_z; if (i&1) { aabb.pos.x+=aabb.size.x; nx+=half; } if (i&2) { aabb.pos.y+=aabb.size.y; ny+=half; } if (i&4) { aabb.pos.z+=aabb.size.z; nz+=half; } //make sure to not plot beyond limits if (nx<0 || nx>=p_baker->axis_cell_size[0] || ny<0 || ny>=p_baker->axis_cell_size[1] || nz<0 || nz>=p_baker->axis_cell_size[2]) continue; { Rect3 test_aabb=aabb; //test_aabb.grow_by(test_aabb.get_longest_axis_size()*0.05); //grow a bit to avoid numerical error in real-time Vector3 qsize = test_aabb.size*0.5; //quarter size, for fast aabb test if (!fast_tri_box_overlap(test_aabb.pos+qsize,qsize,p_vtx)) { //if (!Face3(p_vtx[0],p_vtx[1],p_vtx[2]).intersects_aabb2(aabb)) { //does not fit in child, go on continue; } } if (p_baker->bake_cells[p_idx].childs[i]==Baker::CHILD_EMPTY) { //sub cell must be created uint32_t child_idx = p_baker->bake_cells.size(); p_baker->bake_cells[p_idx].childs[i]=child_idx; p_baker->bake_cells.resize( p_baker->bake_cells.size() + 1); p_baker->bake_cells[child_idx].level=p_level+1; } _plot_face(p_baker->bake_cells[p_idx].childs[i],p_level+1,nx,ny,nz,p_vtx,p_uv,p_material,aabb,p_baker); } } }