diff --git a/modules/raycast/lightmap_raycaster.cpp b/modules/raycast/lightmap_raycaster.cpp index 29334b7cb02..fac4385d771 100644 --- a/modules/raycast/lightmap_raycaster.cpp +++ b/modules/raycast/lightmap_raycaster.cpp @@ -30,12 +30,7 @@ #include "lightmap_raycaster.h" -// From Embree. -#include -#include -#include - -using namespace embree; +#include LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() { return memnew(LightmapRaycasterEmbree); @@ -135,23 +130,22 @@ void LightmapRaycasterEmbree::add_mesh(const Vector &p_vertices, const ERR_FAIL_COND(vertex_count % 3 != 0); ERR_FAIL_COND(vertex_count != p_uv2s.size()); + ERR_FAIL_COND(!p_normals.empty() && vertex_count != p_normals.size()); + + Vector3 *embree_vertices = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); + copymem(embree_vertices, p_vertices.ptr(), sizeof(Vector3) * vertex_count); + + Vector2 *embree_light_uvs = (Vector2 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vector2), vertex_count); + copymem(embree_light_uvs, p_uv2s.ptr(), sizeof(Vector2) * vertex_count); - Vec3fa *embree_vertices = (Vec3fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vec3fa), vertex_count); - Vec2fa *embree_light_uvs = (Vec2fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vec2fa), vertex_count); uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3); - - Vec3fa *embree_normals = nullptr; - if (!p_normals.empty()) { - embree_normals = (Vec3fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vec3fa), vertex_count); + for (int i = 0; i < vertex_count; i++) { + embree_triangles[i] = i; } - for (int i = 0; i < vertex_count; i++) { - embree_vertices[i] = Vec3fa(p_vertices[i].x, p_vertices[i].y, p_vertices[i].z); - embree_light_uvs[i] = Vec2fa(p_uv2s[i].x, p_uv2s[i].y); - if (embree_normals != nullptr) { - embree_normals[i] = Vec3fa(p_normals[i].x, p_normals[i].y, p_normals[i].z); - } - embree_triangles[i] = i; + if (!p_normals.empty()) { + Vector3 *embree_normals = (Vector3 *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vector3), vertex_count); + copymem(embree_normals, p_normals.ptr(), sizeof(Vector3) * vertex_count); } rtcCommitGeometry(embree_mesh); diff --git a/scene/3d/baked_lightmap.cpp b/scene/3d/baked_lightmap.cpp index 67c201c3e5d..af044fccdb4 100644 --- a/scene/3d/baked_lightmap.cpp +++ b/scene/3d/baked_lightmap.cpp @@ -810,7 +810,7 @@ BakedLightmap::BakeError BakedLightmap::bake(Node *p_from_node, String p_data_sa } break; case ENVIRONMENT_MODE_CUSTOM_SKY: { if (environment_custom_sky.is_valid()) { - environment_image = _get_irradiance_from_sky(environment_custom_sky, Vector2i(128, 64)); + environment_image = _get_irradiance_from_sky(environment_custom_sky, environment_custom_energy, Vector2i(128, 64)); environment_xform.set_euler(environment_custom_sky_rotation_degrees * Math_PI / 180.0); } @@ -1233,7 +1233,7 @@ void BakedLightmap::_clear_lightmaps() { } } -Ref BakedLightmap::_get_irradiance_from_sky(Ref p_sky, Vector2i p_size) { +Ref BakedLightmap::_get_irradiance_from_sky(Ref p_sky, float p_energy, Vector2i p_size) { if (p_sky.is_null()) { return Ref(); } @@ -1245,7 +1245,7 @@ Ref BakedLightmap::_get_irradiance_from_sky(Ref p_sky, Vector2i p_si } Ref procedural = p_sky; if (procedural.is_valid()) { - sky_image = procedural->get_panorama(); + sky_image = procedural->get_data(); } if (sky_image.is_null()) { @@ -1254,6 +1254,17 @@ Ref BakedLightmap::_get_irradiance_from_sky(Ref p_sky, Vector2i p_si sky_image->convert(Image::FORMAT_RGBF); sky_image->resize(p_size.x, p_size.y, Image::INTERPOLATE_CUBIC); + + if (p_energy != 1.0) { + sky_image->lock(); + for (int i = 0; i < p_size.y; i++) { + for (int j = 0; j < p_size.x; j++) { + sky_image->set_pixel(j, i, sky_image->get_pixel(j, i) * p_energy); + } + } + sky_image->unlock(); + } + return sky_image; } @@ -1261,7 +1272,7 @@ Ref BakedLightmap::_get_irradiance_map(Ref p_env, Vector2i p Environment::BGMode bg_mode = p_env->get_background(); switch (bg_mode) { case Environment::BG_SKY: { - return _get_irradiance_from_sky(p_env->get_sky(), Vector2i(128, 64)); + return _get_irradiance_from_sky(p_env->get_sky(), p_env->get_bg_energy(), Vector2i(128, 64)); } case Environment::BG_CLEAR_COLOR: case Environment::BG_COLOR: { diff --git a/scene/3d/baked_lightmap.h b/scene/3d/baked_lightmap.h index 6f8f0b8f980..09791b5d2ab 100644 --- a/scene/3d/baked_lightmap.h +++ b/scene/3d/baked_lightmap.h @@ -187,7 +187,7 @@ private: void _clear_lightmaps(); void _get_material_images(const MeshesFound &p_found_mesh, Lightmapper::MeshData &r_mesh_data, Vector > &r_albedo_textures, Vector > &r_emission_textures); - Ref _get_irradiance_from_sky(Ref p_sky, Vector2i p_size); + Ref _get_irradiance_from_sky(Ref p_sky, float p_energy, Vector2i p_size); Ref _get_irradiance_map(Ref p_env, Vector2i p_size); void _find_meshes_and_lights(Node *p_at_node, Vector &meshes, Vector &lights); Vector2i _compute_lightmap_size(const MeshesFound &p_mesh); diff --git a/scene/resources/sky.cpp b/scene/resources/sky.cpp index 0db92f21325..0bad4a2ed0c 100644 --- a/scene/resources/sky.cpp +++ b/scene/resources/sky.cpp @@ -390,8 +390,8 @@ ProceduralSky::TextureSize ProceduralSky::get_texture_size() const { return texture_size; } -Ref ProceduralSky::get_panorama() const { - return panorama; +Ref ProceduralSky::get_data() const { + return panorama->duplicate(); } RID ProceduralSky::get_rid() const { diff --git a/scene/resources/sky.h b/scene/resources/sky.h index 5b5e6c70176..0cfd00bcda4 100644 --- a/scene/resources/sky.h +++ b/scene/resources/sky.h @@ -190,7 +190,7 @@ public: void set_texture_size(TextureSize p_size); TextureSize get_texture_size() const; - Ref get_panorama() const; + Ref get_data() const; virtual RID get_rid() const;