2
0

lightmap_raycaster.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*************************************************************************/
  2. /* lightmap_raycaster.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #ifdef TOOLS_ENABLED
  31. #include "lightmap_raycaster.h"
  32. // From Embree.
  33. #include <math/vec2.h>
  34. #include <math/vec3.h>
  35. #include <pmmintrin.h>
  36. using namespace embree;
  37. LightmapRaycaster *LightmapRaycasterEmbree::create_embree_raycaster() {
  38. return memnew(LightmapRaycasterEmbree);
  39. }
  40. void LightmapRaycasterEmbree::make_default_raycaster() {
  41. create_function = create_embree_raycaster;
  42. }
  43. void LightmapRaycasterEmbree::filter_function(const struct RTCFilterFunctionNArguments *p_args) {
  44. RTCHit *hit = (RTCHit *)p_args->hit;
  45. unsigned int geomID = hit->geomID;
  46. float u = hit->u;
  47. float v = hit->v;
  48. LightmapRaycasterEmbree *scene = (LightmapRaycasterEmbree *)p_args->geometryUserPtr;
  49. RTCGeometry geom = rtcGetGeometry(scene->embree_scene, geomID);
  50. rtcInterpolate0(geom, hit->primID, hit->u, hit->v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, &hit->u, 2);
  51. if (scene->alpha_textures.has(geomID)) {
  52. const AlphaTextureData &alpha_texture = scene->alpha_textures[geomID];
  53. if (alpha_texture.sample(hit->u, hit->v) < 128) {
  54. p_args->valid[0] = 0;
  55. return;
  56. }
  57. }
  58. rtcInterpolate0(geom, hit->primID, u, v, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, &hit->Ng_x, 3);
  59. }
  60. bool LightmapRaycasterEmbree::intersect(Ray &r_ray) {
  61. RTCIntersectContext context;
  62. rtcInitIntersectContext(&context);
  63. rtcIntersect1(embree_scene, &context, (RTCRayHit *)&r_ray);
  64. return r_ray.geomID != RTC_INVALID_GEOMETRY_ID;
  65. }
  66. void LightmapRaycasterEmbree::intersect(Vector<Ray> &r_rays) {
  67. Ray *rays = r_rays.ptrw();
  68. for (int i = 0; i < r_rays.size(); ++i) {
  69. intersect(rays[i]);
  70. }
  71. }
  72. void LightmapRaycasterEmbree::set_mesh_alpha_texture(Ref<Image> p_alpha_texture, unsigned int p_id) {
  73. if (p_alpha_texture.is_valid() && p_alpha_texture->get_size() != Vector2i()) {
  74. AlphaTextureData tex;
  75. tex.size = p_alpha_texture->get_size();
  76. tex.data = p_alpha_texture->get_data();
  77. alpha_textures.insert(p_id, tex);
  78. }
  79. }
  80. float blerp(float c00, float c10, float c01, float c11, float tx, float ty) {
  81. return Math::lerp(Math::lerp(c00, c10, tx), Math::lerp(c01, c11, tx), ty);
  82. }
  83. uint8_t LightmapRaycasterEmbree::AlphaTextureData::sample(float u, float v) const {
  84. float x = u * size.x;
  85. float y = v * size.y;
  86. int xi = (int)x;
  87. int yi = (int)y;
  88. uint8_t texels[4];
  89. for (int i = 0; i < 4; ++i) {
  90. int sample_x = CLAMP(xi + i % 2, 0, size.x - 1);
  91. int sample_y = CLAMP(yi + i / 2, 0, size.y - 1);
  92. texels[i] = data[sample_y * size.x + sample_x];
  93. }
  94. return Math::round(blerp(texels[0], texels[1], texels[2], texels[3], x - xi, y - yi));
  95. }
  96. void LightmapRaycasterEmbree::add_mesh(const Vector<Vector3> &p_vertices, const Vector<Vector3> &p_normals, const Vector<Vector2> &p_uv2s, unsigned int p_id) {
  97. RTCGeometry embree_mesh = rtcNewGeometry(embree_device, RTC_GEOMETRY_TYPE_TRIANGLE);
  98. rtcSetGeometryVertexAttributeCount(embree_mesh, 2);
  99. int vertex_count = p_vertices.size();
  100. ERR_FAIL_COND(vertex_count % 3 != 0);
  101. ERR_FAIL_COND(vertex_count != p_uv2s.size());
  102. Vec3fa *embree_vertices = (Vec3fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX, 0, RTC_FORMAT_FLOAT3, sizeof(Vec3fa), vertex_count);
  103. Vec2fa *embree_light_uvs = (Vec2fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 0, RTC_FORMAT_FLOAT2, sizeof(Vec2fa), vertex_count);
  104. uint32_t *embree_triangles = (uint32_t *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_INDEX, 0, RTC_FORMAT_UINT3, sizeof(uint32_t) * 3, vertex_count / 3);
  105. Vec3fa *embree_normals = nullptr;
  106. if (!p_normals.is_empty()) {
  107. embree_normals = (Vec3fa *)rtcSetNewGeometryBuffer(embree_mesh, RTC_BUFFER_TYPE_VERTEX_ATTRIBUTE, 1, RTC_FORMAT_FLOAT3, sizeof(Vec3fa), vertex_count);
  108. }
  109. for (int i = 0; i < vertex_count; i++) {
  110. embree_vertices[i] = Vec3fa(p_vertices[i].x, p_vertices[i].y, p_vertices[i].z);
  111. embree_light_uvs[i] = Vec2fa(p_uv2s[i].x, p_uv2s[i].y);
  112. if (embree_normals != nullptr) {
  113. embree_normals[i] = Vec3fa(p_normals[i].x, p_normals[i].y, p_normals[i].z);
  114. }
  115. embree_triangles[i] = i;
  116. }
  117. rtcCommitGeometry(embree_mesh);
  118. rtcSetGeometryIntersectFilterFunction(embree_mesh, filter_function);
  119. rtcSetGeometryUserData(embree_mesh, this);
  120. rtcAttachGeometryByID(embree_scene, embree_mesh, p_id);
  121. rtcReleaseGeometry(embree_mesh);
  122. }
  123. void LightmapRaycasterEmbree::commit() {
  124. rtcCommitScene(embree_scene);
  125. }
  126. void LightmapRaycasterEmbree::set_mesh_filter(const Set<int> &p_mesh_ids) {
  127. for (Set<int>::Element *E = p_mesh_ids.front(); E; E = E->next()) {
  128. rtcDisableGeometry(rtcGetGeometry(embree_scene, E->get()));
  129. }
  130. rtcCommitScene(embree_scene);
  131. filter_meshes = p_mesh_ids;
  132. }
  133. void LightmapRaycasterEmbree::clear_mesh_filter() {
  134. for (Set<int>::Element *E = filter_meshes.front(); E; E = E->next()) {
  135. rtcEnableGeometry(rtcGetGeometry(embree_scene, E->get()));
  136. }
  137. rtcCommitScene(embree_scene);
  138. filter_meshes.clear();
  139. }
  140. void embree_error_handler(void *p_user_data, RTCError p_code, const char *p_str) {
  141. print_error("Embree error: " + String(p_str));
  142. }
  143. LightmapRaycasterEmbree::LightmapRaycasterEmbree() {
  144. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
  145. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_ON);
  146. embree_device = rtcNewDevice(nullptr);
  147. rtcSetDeviceErrorFunction(embree_device, &embree_error_handler, nullptr);
  148. embree_scene = rtcNewScene(embree_device);
  149. }
  150. LightmapRaycasterEmbree::~LightmapRaycasterEmbree() {
  151. _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_OFF);
  152. _MM_SET_DENORMALS_ZERO_MODE(_MM_DENORMALS_ZERO_OFF);
  153. if (embree_scene != nullptr) {
  154. rtcReleaseScene(embree_scene);
  155. }
  156. if (embree_device != nullptr) {
  157. rtcReleaseDevice(embree_device);
  158. }
  159. }
  160. #endif