1/**************************************************************************/
2/* register_types.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
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
31#include "register_types.h"
32
33#include "core/crypto/crypto_core.h"
34
35#include <xatlas.h>
36
37extern bool (*array_mesh_lightmap_unwrap_callback)(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y);
38
39bool xatlas_mesh_lightmap_unwrap_callback(float p_texel_size, const float *p_vertices, const float *p_normals, int p_vertex_count, const int *p_indices, int p_index_count, const uint8_t *p_cache_data, bool *r_use_cache, uint8_t **r_mesh_cache, int *r_mesh_cache_size, float **r_uv, int **r_vertex, int *r_vertex_count, int **r_index, int *r_index_count, int *r_size_hint_x, int *r_size_hint_y) {
40 CryptoCore::MD5Context ctx;
41 ctx.start();
42
43 ctx.update((unsigned char *)&p_texel_size, sizeof(float));
44 ctx.update((unsigned char *)p_indices, sizeof(int) * p_index_count);
45 ctx.update((unsigned char *)p_vertices, sizeof(float) * p_vertex_count * 3);
46 ctx.update((unsigned char *)p_normals, sizeof(float) * p_vertex_count * 3);
47
48 unsigned char hash[16];
49 ctx.finish(hash);
50
51 bool cached = false;
52 unsigned int cache_idx = 0;
53
54 *r_mesh_cache = nullptr;
55 *r_mesh_cache_size = 0;
56
57 if (p_cache_data) {
58 //Check if hash is in cache data
59 int *cache_data = (int *)p_cache_data;
60 int n_entries = cache_data[0];
61 unsigned int read_idx = 1;
62 for (int i = 0; i < n_entries; ++i) {
63 if (memcmp(&cache_data[read_idx], hash, 16) == 0) {
64 cached = true;
65 cache_idx = read_idx;
66 break;
67 }
68
69 read_idx += 4; // hash
70 read_idx += 2; // size hint
71
72 int vertex_count = cache_data[read_idx];
73 read_idx += 1; // vertex count
74 read_idx += vertex_count; // vertex
75 read_idx += vertex_count * 2; // uvs
76
77 int index_count = cache_data[read_idx];
78 read_idx += 1; // index count
79 read_idx += index_count; // indices
80 }
81 }
82
83 if (cached) {
84 int *cache_data = (int *)p_cache_data;
85
86 cache_idx += 4;
87
88 // Load size
89 *r_size_hint_x = cache_data[cache_idx];
90 *r_size_hint_y = cache_data[cache_idx + 1];
91 cache_idx += 2;
92
93 // Load vertices
94 *r_vertex_count = cache_data[cache_idx];
95 cache_idx++;
96 *r_vertex = &cache_data[cache_idx];
97 cache_idx += *r_vertex_count;
98
99 // Load UVs
100 *r_uv = (float *)&cache_data[cache_idx];
101 cache_idx += *r_vertex_count * 2;
102
103 // Load indices
104 *r_index_count = cache_data[cache_idx];
105 cache_idx++;
106 *r_index = &cache_data[cache_idx];
107 } else {
108 // set up input mesh
109 xatlas::MeshDecl input_mesh;
110 input_mesh.indexData = p_indices;
111 input_mesh.indexCount = p_index_count;
112 input_mesh.indexFormat = xatlas::IndexFormat::UInt32;
113
114 input_mesh.vertexCount = p_vertex_count;
115 input_mesh.vertexPositionData = p_vertices;
116 input_mesh.vertexPositionStride = sizeof(float) * 3;
117 input_mesh.vertexNormalData = p_normals;
118 input_mesh.vertexNormalStride = sizeof(uint32_t) * 3;
119 input_mesh.vertexUvData = nullptr;
120 input_mesh.vertexUvStride = 0;
121
122 xatlas::ChartOptions chart_options;
123 chart_options.fixWinding = true;
124
125 ERR_FAIL_COND_V_MSG(p_texel_size <= 0.0f, false, "Texel size must be greater than 0.");
126
127 xatlas::PackOptions pack_options;
128 pack_options.padding = 1;
129 pack_options.maxChartSize = 4094; // Lightmap atlassing needs 2 for padding between meshes, so 4096-2
130 pack_options.blockAlign = true;
131 pack_options.texelsPerUnit = 1.0 / p_texel_size;
132
133 xatlas::Atlas *atlas = xatlas::Create();
134
135 xatlas::AddMeshError err = xatlas::AddMesh(atlas, input_mesh, 1);
136 ERR_FAIL_COND_V_MSG(err != xatlas::AddMeshError::Success, false, xatlas::StringForEnum(err));
137
138 xatlas::Generate(atlas, chart_options, pack_options);
139
140 *r_size_hint_x = atlas->width;
141 *r_size_hint_y = atlas->height;
142
143 float w = *r_size_hint_x;
144 float h = *r_size_hint_y;
145
146 if (w == 0 || h == 0) {
147 xatlas::Destroy(atlas);
148 return false; //could not bake because there is no area
149 }
150
151 const xatlas::Mesh &output = atlas->meshes[0];
152
153 *r_vertex = (int *)memalloc(sizeof(int) * output.vertexCount);
154 ERR_FAIL_NULL_V_MSG(*r_vertex, false, "Out of memory.");
155 *r_uv = (float *)memalloc(sizeof(float) * output.vertexCount * 2);
156 ERR_FAIL_NULL_V_MSG(*r_uv, false, "Out of memory.");
157 *r_index = (int *)memalloc(sizeof(int) * output.indexCount);
158 ERR_FAIL_NULL_V_MSG(*r_index, false, "Out of memory.");
159
160 float max_x = 0;
161 float max_y = 0;
162 for (uint32_t i = 0; i < output.vertexCount; i++) {
163 (*r_vertex)[i] = output.vertexArray[i].xref;
164 (*r_uv)[i * 2 + 0] = output.vertexArray[i].uv[0] / w;
165 (*r_uv)[i * 2 + 1] = output.vertexArray[i].uv[1] / h;
166 max_x = MAX(max_x, output.vertexArray[i].uv[0]);
167 max_y = MAX(max_y, output.vertexArray[i].uv[1]);
168 }
169
170 *r_vertex_count = output.vertexCount;
171
172 for (uint32_t i = 0; i < output.indexCount; i++) {
173 (*r_index)[i] = output.indexArray[i];
174 }
175
176 *r_index_count = output.indexCount;
177
178 xatlas::Destroy(atlas);
179 }
180
181 if (*r_use_cache) {
182 // Build cache data for current mesh
183
184 unsigned int new_cache_size = 4 + 2 + 1 + *r_vertex_count + (*r_vertex_count * 2) + 1 + *r_index_count; // hash + size hint + vertex_count + vertices + uvs + index_count + indices
185 new_cache_size *= sizeof(int);
186 int *new_cache_data = (int *)memalloc(new_cache_size);
187 unsigned int new_cache_idx = 0;
188
189 // hash
190 memcpy(&new_cache_data[new_cache_idx], hash, 16);
191 new_cache_idx += 4;
192
193 // size hint
194 new_cache_data[new_cache_idx] = *r_size_hint_x;
195 new_cache_data[new_cache_idx + 1] = *r_size_hint_y;
196 new_cache_idx += 2;
197
198 // vertex count
199 new_cache_data[new_cache_idx] = *r_vertex_count;
200 new_cache_idx++;
201
202 // vertices
203 memcpy(&new_cache_data[new_cache_idx], *r_vertex, sizeof(int) * (*r_vertex_count));
204 new_cache_idx += *r_vertex_count;
205
206 // uvs
207 memcpy(&new_cache_data[new_cache_idx], *r_uv, sizeof(float) * (*r_vertex_count) * 2);
208 new_cache_idx += *r_vertex_count * 2;
209
210 // index count
211 new_cache_data[new_cache_idx] = *r_index_count;
212 new_cache_idx++;
213
214 // indices
215 memcpy(&new_cache_data[new_cache_idx], *r_index, sizeof(int) * (*r_index_count));
216
217 // Return cache data to the caller
218 *r_mesh_cache = (uint8_t *)new_cache_data;
219 *r_mesh_cache_size = new_cache_size;
220 }
221
222 *r_use_cache = cached; // Return whether cache was used.
223
224 return true;
225}
226
227void initialize_xatlas_unwrap_module(ModuleInitializationLevel p_level) {
228 if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
229 return;
230 }
231
232 array_mesh_lightmap_unwrap_callback = xatlas_mesh_lightmap_unwrap_callback;
233}
234
235void uninitialize_xatlas_unwrap_module(ModuleInitializationLevel p_level) {
236 if (p_level != MODULE_INITIALIZATION_LEVEL_SCENE) {
237 return;
238 }
239}
240