Exemplo n.º 1
0
BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, bool p_create_visual_debug) {

	String save_path;

	if (image_path.begins_with("res://")) {
		save_path = image_path;
	} else {
		if (get_filename() != "") {
			save_path = get_filename().get_base_dir();
		} else if (get_owner() && get_owner()->get_filename() != "") {
			save_path = get_owner()->get_filename().get_base_dir();
		}

		if (save_path == "") {
			return BAKE_ERROR_NO_SAVE_PATH;
		}
		if (image_path != "") {
			save_path.plus_file(image_path);
		}
	}
	{
		//check for valid save path
		DirAccessRef d = DirAccess::open(save_path);
		if (!d) {
			ERR_PRINTS("Invalid Save Path: " + save_path);
			return BAKE_ERROR_NO_SAVE_PATH;
		}
	}

	Ref<BakedLightmapData> new_light_data;
	new_light_data.instance();

	VoxelLightBaker baker;

	int bake_subdiv;
	int capture_subdiv;
	AABB bake_bounds;
	{
		bake_bounds = AABB(-extents, extents * 2.0);
		int subdiv = nearest_power_of_2_templated(int(bake_bounds.get_longest_axis_size() / bake_cell_size));
		bake_bounds.size[bake_bounds.get_longest_axis_size()] = subdiv * bake_cell_size;
		bake_subdiv = nearest_shift(subdiv) + 1;

		capture_subdiv = bake_subdiv;
		float css = bake_cell_size;
		while (css < capture_cell_size && capture_subdiv > 2) {
			capture_subdiv--;
			css *= 2.0;
		}
	}

	baker.begin_bake(bake_subdiv, bake_bounds);

	List<PlotMesh> mesh_list;
	List<PlotLight> light_list;

	_find_meshes_and_lights(p_from_node ? p_from_node : get_parent(), mesh_list, light_list);

	if (bake_begin_function) {
		bake_begin_function(mesh_list.size() + light_list.size() + 1 + mesh_list.size() * 100);
	}

	int step = 0;

	int pmc = 0;

	for (List<PlotMesh>::Element *E = mesh_list.front(); E; E = E->next()) {

		if (bake_step_function) {
			bake_step_function(step++, RTR("Plotting Meshes: ") + " (" + itos(pmc + 1) + "/" + itos(mesh_list.size()) + ")");
		}

		pmc++;
		baker.plot_mesh(E->get().local_xform, E->get().mesh, E->get().instance_materials, E->get().override_material);
	}

	pmc = 0;
	baker.begin_bake_light(VoxelLightBaker::BakeQuality(bake_quality), VoxelLightBaker::BakeMode(bake_mode), propagation, energy);

	for (List<PlotLight>::Element *E = light_list.front(); E; E = E->next()) {

		if (bake_step_function) {
			bake_step_function(step++, RTR("Plotting Lights:") + " (" + itos(pmc + 1) + "/" + itos(light_list.size()) + ")");
		}

		pmc++;
		PlotLight pl = E->get();
		switch (pl.light->get_light_type()) {
			case VS::LIGHT_DIRECTIONAL: {
				baker.plot_light_directional(-pl.local_xform.basis.get_axis(2), pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_bake_mode() == Light::BAKE_ALL);
			} break;
			case VS::LIGHT_OMNI: {
				baker.plot_light_omni(pl.local_xform.origin, pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_param(Light::PARAM_RANGE), pl.light->get_param(Light::PARAM_ATTENUATION), pl.light->get_bake_mode() == Light::BAKE_ALL);
			} break;
			case VS::LIGHT_SPOT: {
				baker.plot_light_spot(pl.local_xform.origin, pl.local_xform.basis.get_axis(2), pl.light->get_color(), pl.light->get_param(Light::PARAM_ENERGY), pl.light->get_param(Light::PARAM_INDIRECT_ENERGY), pl.light->get_param(Light::PARAM_RANGE), pl.light->get_param(Light::PARAM_ATTENUATION), pl.light->get_param(Light::PARAM_SPOT_ANGLE), pl.light->get_param(Light::PARAM_SPOT_ATTENUATION), pl.light->get_bake_mode() == Light::BAKE_ALL);

			} break;
		}
	}
	/*if (bake_step_function) {
		bake_step_function(pmc++, RTR("Finishing Plot"));
	}*/

	baker.end_bake();

	Set<String> used_mesh_names;

	pmc = 0;
	for (List<PlotMesh>::Element *E = mesh_list.front(); E; E = E->next()) {

		String mesh_name = E->get().mesh->get_name();
		if (mesh_name == "" || mesh_name.find(":") != -1 || mesh_name.find("/") != -1) {
			mesh_name = "LightMap";
		}

		if (used_mesh_names.has(mesh_name)) {
			int idx = 2;
			String base = mesh_name;
			while (true) {
				mesh_name = base + itos(idx);
				if (!used_mesh_names.has(mesh_name))
					break;
				idx++;
			}
		}
		used_mesh_names.insert(mesh_name);

		pmc++;
		VoxelLightBaker::LightMapData lm;

		Error err;
		if (bake_step_function) {
			BakeTimeData btd;
			btd.text = RTR("Lighting Meshes: ") + mesh_name + " (" + itos(pmc) + "/" + itos(mesh_list.size()) + ")";
			btd.pass = step;
			btd.last_step = 0;
			err = baker.make_lightmap(E->get().local_xform, E->get().mesh, lm, _bake_time, &btd);
			if (err != OK) {
				bake_end_function();
				if (err == ERR_SKIP)
					return BAKE_ERROR_USER_ABORTED;
				return BAKE_ERROR_CANT_CREATE_IMAGE;
			}
			step += 100;
		} else {

			err = baker.make_lightmap(E->get().local_xform, E->get().mesh, lm);
		}

		if (err == OK) {

			Ref<Image> image;
			image.instance();

			uint32_t tex_flags = Texture::FLAGS_DEFAULT;
			if (hdr) {

				//just save a regular image
				PoolVector<uint8_t> data;
				int s = lm.light.size();
				data.resize(lm.light.size() * 2);
				{

					PoolVector<uint8_t>::Write w = data.write();
					PoolVector<float>::Read r = lm.light.read();
					uint16_t *hfw = (uint16_t *)w.ptr();
					for (int i = 0; i < s; i++) {
						hfw[i] = Math::make_half_float(r[i]);
					}
				}

				image->create(lm.width, lm.height, false, Image::FORMAT_RGBH, data);

			} else {

				//just save a regular image
				PoolVector<uint8_t> data;
				int s = lm.light.size();
				data.resize(lm.light.size());
				{

					PoolVector<uint8_t>::Write w = data.write();
					PoolVector<float>::Read r = lm.light.read();
					for (int i = 0; i < s; i += 3) {
						Color c(r[i + 0], r[i + 1], r[i + 2]);
						c = c.to_srgb();
						w[i + 0] = CLAMP(c.r * 255, 0, 255);
						w[i + 1] = CLAMP(c.g * 255, 0, 255);
						w[i + 2] = CLAMP(c.b * 255, 0, 255);
					}
				}

				image->create(lm.width, lm.height, false, Image::FORMAT_RGB8, data);

				//This texture is saved to SRGB for two reasons:
				// 1) first is so it looks better when doing the LINEAR->SRGB conversion (more accurate)
				// 2) So it can be used in the GLES2 backend, which does not support linkear workflow
				tex_flags |= Texture::FLAG_CONVERT_TO_LINEAR;
			}

			Ref<ImageTexture> tex;
			String image_path = save_path.plus_file(mesh_name + ".tex");
			bool set_path = true;
			if (ResourceCache::has(image_path)) {
				tex = Ref<Resource>((Resource *)ResourceCache::get(image_path));
				set_path = false;
			}

			if (!tex.is_valid()) {
				tex.instance();
			}

			tex->create_from_image(image, tex_flags);

			err = ResourceSaver::save(image_path, tex, ResourceSaver::FLAG_CHANGE_PATH);
			if (err != OK) {
				if (bake_end_function) {
					bake_end_function();
				}
				ERR_FAIL_COND_V(err != OK, BAKE_ERROR_CANT_CREATE_IMAGE);
			}

			if (set_path) {
				tex->set_path(image_path);
			}
			new_light_data->add_user(E->get().path, tex, E->get().instance_idx);
		}
	}

	AABB bounds = AABB(-extents, extents * 2);
	new_light_data->set_cell_subdiv(capture_subdiv);
	new_light_data->set_bounds(bounds);
	new_light_data->set_octree(baker.create_capture_octree(capture_subdiv));
	{

		float bake_bound_size = bake_bounds.get_longest_axis_size();
		Transform to_bounds;
		to_bounds.basis.scale(Vector3(bake_bound_size, bake_bound_size, bake_bound_size));
		to_bounds.origin = bounds.position;

		Transform to_grid;
		to_grid.basis.scale(Vector3(1 << (capture_subdiv - 1), 1 << (capture_subdiv - 1), 1 << (capture_subdiv - 1)));

		Transform to_cell_space = to_grid * to_bounds.affine_inverse();
		new_light_data->set_cell_space_transform(to_cell_space);
	}

	if (bake_end_function) {
		bake_end_function();
	}

	//create the data for visual server

	if (p_create_visual_debug) {
		MultiMeshInstance *mmi = memnew(MultiMeshInstance);
		mmi->set_multimesh(baker.create_debug_multimesh(VoxelLightBaker::DEBUG_LIGHT));
		add_child(mmi);
#ifdef TOOLS_ENABLED
		if (get_tree()->get_edited_scene_root() == this) {
			mmi->set_owner(this);
		} else {
			mmi->set_owner(get_owner());
		}
#else
		mmi->set_owner(get_owner());
#endif
	}

	set_light_data(new_light_data);

	return BAKE_ERROR_OK;
}
Exemplo n.º 2
0
void GIProbe::_create_debug_mesh(Baker *p_baker) {

	Ref<MultiMesh> mm;
	mm.instance();

	mm->set_transform_format(MultiMesh::TRANSFORM_3D);
	mm->set_color_format(MultiMesh::COLOR_8BIT);
	print_line("leaf voxels: "+itos(p_baker->leaf_voxel_count));
	mm->set_instance_count(p_baker->leaf_voxel_count);

	Ref<Mesh> mesh;
	mesh.instance();

	{
		Array arr;
		arr.resize(Mesh::ARRAY_MAX);

		PoolVector<Vector3> vertices;
		PoolVector<Color> colors;

		int vtx_idx=0;
	#define ADD_VTX(m_idx);\
		vertices.push_back( face_points[m_idx] );\
		colors.push_back( Color(1,1,1,1) );\
		vtx_idx++;\

		for (int i=0;i<6;i++) {


			Vector3 face_points[4];

			for (int j=0;j<4;j++) {

				float v[3];
				v[0]=1.0;
				v[1]=1-2*((j>>1)&1);
				v[2]=v[1]*(1-2*(j&1));

				for (int k=0;k<3;k++) {

					if (i<3)
						face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
					else
						face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
				}
			}

		//tri 1
			ADD_VTX(0);
			ADD_VTX(1);
			ADD_VTX(2);
		//tri 2
			ADD_VTX(2);
			ADD_VTX(3);
			ADD_VTX(0);

		}


		arr[Mesh::ARRAY_VERTEX]=vertices;
		arr[Mesh::ARRAY_COLOR]=colors;
		mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES,arr);
	}

	{
		Ref<FixedSpatialMaterial> fsm;
		fsm.instance();
		fsm->set_flag(FixedSpatialMaterial::FLAG_SRGB_VERTEX_COLOR,true);
		fsm->set_flag(FixedSpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR,true);
		fsm->set_flag(FixedSpatialMaterial::FLAG_UNSHADED,true);
		fsm->set_albedo(Color(1,1,1,1));

		mesh->surface_set_material(0,fsm);
	}

	mm->set_mesh(mesh);


	int idx=0;
	_debug_mesh(0,0,p_baker->po2_bounds,mm,idx,p_baker);

	MultiMeshInstance *mmi = memnew( MultiMeshInstance );
	mmi->set_multimesh(mm);
	add_child(mmi);
#ifdef TOOLS_ENABLED
	if (get_tree()->get_edited_scene_root()==this){
		mmi->set_owner(this);
	} else {
		mmi->set_owner(get_owner());

	}
#else
	mmi->set_owner(get_owner());
#endif
}