-
Notifications
You must be signed in to change notification settings - Fork 3
/
lmaterials.cpp
387 lines (297 loc) · 8.01 KB
/
lmaterials.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
#include "lmaterials.h"
#define LM_STRING_TRANSPARENT "_T_"
namespace LM
{
void LMaterial::Destroy()
{
if (pAlbedo)
{
memdelete(pAlbedo);
pAlbedo = 0;
}
}
/////////////////////////////////
LMaterials::LMaterials()
{
m_uiMaxMaterialSize = 256;
}
LMaterials::~LMaterials()
{
Reset();
}
void LMaterials::Reset()
{
for (int n=0; n<m_Materials.size(); n++)
{
m_Materials[n].Destroy();
}
m_Materials.clear(true);
}
void LMaterials::AdjustMaterials(float emission_density)
{
if (emission_density == 0.0f)
return;
float emission_multiplier = 1.0f / emission_density;
for (int n=0; n<m_Materials.size(); n++)
{
LMaterial &mat = m_Materials[n];
if (mat.m_bEmitter)
{
mat.m_Power_Emission *= emission_multiplier;
mat.m_Col_Emission *= emission_multiplier;
}
}
}
int LMaterials::FindOrCreateMaterial(const MeshInstance &mi, Ref<Mesh> rmesh, int surf_id)
{
Ref<Material> src_material;
// mesh instance has the material?
src_material = mi.get_surface_material(surf_id);
if (src_material.ptr())
{
//mi.set_surface_material(0, mat);
}
else
{
// mesh has the material?
src_material = rmesh->surface_get_material(surf_id);
//mi.set_surface_material(0, smat);
}
// Ref<Material> src_material = rmesh->surface_get_material(surf_id);
const Material * pSrcMaterial = src_material.ptr();
if (!pSrcMaterial)
return 0;
// already exists?
for (int n=0; n<m_Materials.size(); n++)
{
if (m_Materials[n].pGodotMaterial == pSrcMaterial)
return n+1;
}
// doesn't exist create a new material
LMaterial * pMat = m_Materials.request();
pMat->Create();
pMat->pGodotMaterial = pSrcMaterial;
// spatial material?
Ref<SpatialMaterial> spatial_mat = src_material;
Ref<Texture> albedo_tex;
Color albedo = Color(1, 1, 1, 1);
float emission = 0.0f;
Color emission_color(1, 1, 1, 1);
if (spatial_mat.is_valid())
{
albedo_tex = spatial_mat->get_texture(SpatialMaterial::TEXTURE_ALBEDO);
albedo = spatial_mat->get_albedo();
emission = spatial_mat->get_emission_energy();
emission_color = spatial_mat->get_emission();
}
else
{
// shader material?
Variant shader_tex = FindCustom_AlbedoTex(src_material);
albedo_tex = shader_tex;
// check the name of the material to allow emission
String szMat = src_material->get_path();
if (szMat.find(LM_STRING_TRANSPARENT) != -1)
{
pMat->m_bTransparent = true;
}
FindCustom_ShaderParams(src_material, emission, emission_color);
} // not spatial mat
// emission
if (emission > 0.0f)
{
pMat->m_bEmitter = true;
pMat->m_Power_Emission = emission / 1000.0f; // some constant to be comparable to lights
pMat->m_Col_Emission = emission_color * pMat->m_Power_Emission;
// apply a modifier for the emission density. As the number of samples go up, the power per sample
// must reduce in order to prevent brightness changing.
}
Ref<Image> img_albedo;
if (albedo_tex.is_valid())
{
img_albedo = albedo_tex->get_data();
pMat->pAlbedo = _get_bake_texture(img_albedo, albedo, Color(0, 0, 0)); // albedo texture, color is multiplicative
//albedo_texture = _get_bake_texture(img_albedo, size, mat->get_albedo(), Color(0, 0, 0)); // albedo texture, color is multiplicative
} else
{
//albedo_texture = _get_bake_texture(img_albedo, size, Color(1, 1, 1), mat->get_albedo()); // no albedo texture, color is additive
}
// emission?
// returns the new material ID plus 1
return m_Materials.size();
}
void LMaterials::FindCustom_ShaderParams(Ref<Material> src_material, float &emission, Color &emission_color)
{
// defaults
emission = 0.0f;
emission_color = Color(1, 1, 1, 1);
Ref<ShaderMaterial> shader_mat = src_material;
if (!shader_mat.is_valid())
return;
Variant p_emission = shader_mat->get_shader_param("emission");
if (p_emission)
emission = p_emission;
Variant p_emission_color = shader_mat->get_shader_param("emission_color");
if (p_emission_color)
emission_color = p_emission_color;
}
Variant LMaterials::FindCustom_AlbedoTex(Ref<Material> src_material)
{
Ref<ShaderMaterial> shader_mat = src_material;
if (!shader_mat.is_valid())
return Variant::NIL;
// get the shader
Ref<Shader> shader = shader_mat->get_shader();
if (!shader.is_valid())
return Variant::NIL;
// first - is there a named albedo texture?
Variant named_param = shader_mat->get_shader_param("texture_albedo");
// if (named_param)
// return named_param;
return named_param;
/*
// find the most likely albedo texture
List<PropertyInfo> plist;
shader->get_param_list(&plist);
String sz_first_obj_param;
for (List<PropertyInfo>::Element *E = plist.front(); E; E = E->next()) {
String szName = E->get().name;
Variant::Type t = E->get().type;
// print_line("shader param : " + szName);
// print_line("shader type : " + String(Variant(t)));
if (t == Variant::OBJECT)
{
sz_first_obj_param = szName;
break;
}
//r_options->push_back(quote_style + E->get().name.replace_first("shader_param/", "") + quote_style);
}
if (sz_first_obj_param == "")
return Variant::NIL;
StringName pr = shader->remap_param(sz_first_obj_param);
if (!pr) {
String n = sz_first_obj_param;
if (n.find("param/") == 0) { //backwards compatibility
pr = n.substr(6, n.length());
}
if (n.find("shader_param/") == 0) { //backwards compatibility
pr = n.replace_first("shader_param/", "");
}
}
if (!pr)
return Variant::NIL;
Variant param = shader_mat->get_shader_param(pr);
print_line("\tparam is " + String(param));
return param;
*/
}
LTexture * LMaterials::_make_dummy_texture(LTexture * pLTexture, Color col)
{
pLTexture->colors.resize(1);
pLTexture->colors.set(0, col);
pLTexture->width = 1;
pLTexture->height = 1;
return pLTexture;
}
LTexture * LMaterials::_get_bake_texture(Ref<Image> p_image, const Color &p_color_mul, const Color &p_color_add)
{
LTexture * lt = memnew(LTexture);
// no image exists, use dummy texture
if (p_image.is_null() || p_image->empty())
{
return _make_dummy_texture(lt, p_color_mul);
}
p_image = p_image->duplicate();
if (p_image->is_compressed()) {
Error err = p_image->decompress();
if (err != OK)
{
// could not decompress
WARN_PRINT("LMaterials::_get_bake_texture : could not decompress texture");
return _make_dummy_texture(lt, p_color_mul);
}
}
p_image->convert(Image::FORMAT_RGBA8);
// downsize if necessary
int w = p_image->get_width();
int h = p_image->get_height();
bool bResize = false;
while (true)
{
if ((w > m_uiMaxMaterialSize) || (h > m_uiMaxMaterialSize))
{
w /= 2;
h /= 2;
bResize = true;
}
else
{
w = MAX(w, 1);
h = MAX(h, 1);
break;
}
}
if (bResize)
p_image->resize(w, h, Image::INTERPOLATE_CUBIC);
w = p_image->get_width();
h = p_image->get_height();
int size = w * h;
lt->width = w;
lt->height = h;
lt->colors.resize(size);
PoolVector<uint8_t>::Read r = p_image->get_data().read();
for (int i = 0; i<size; i++)
{
Color c;
c.r = (r[i * 4 + 0] / 255.0) * p_color_mul.r + p_color_add.r;
c.g = (r[i * 4 + 1] / 255.0) * p_color_mul.g + p_color_add.g;
c.b = (r[i * 4 + 2] / 255.0) * p_color_mul.b + p_color_add.b;
// srgb to linear?
c.a = r[i * 4 + 3] / 255.0;
lt->colors.set(i, c);
}
return lt;
}
void LTexture::Sample(const Vector2 &uv, Color &col) const
{
// mod to surface (tiling)
float x = fmodf(uv.x, 1.0f);
float y = fmodf(uv.y, 1.0f);
// we need these because fmod can produce negative results
if (x < 0.0f)
x = 1.0f + x;
if (y < 0.0f)
y = 1.0f + y;
x *= width;
y *= height;
// no filtering as yet
int tx = x;
int ty = y;
tx = MIN(tx, width-1);
ty = MIN(ty, height-1);
int i = (ty * width) + tx;
col = colors[i];
}
bool LMaterials::FindColors(int mat_id, const Vector2 &uv, Color &albedo, bool &bTransparent)
{
// mat_id is plus one
if (!mat_id)
{
albedo = Color(1, 1, 1, 1);
bTransparent = false;
return false;
}
mat_id--;
const LMaterial &mat = m_Materials[mat_id];
// return whether transparent
bTransparent = mat.m_bTransparent;
if (!mat.pAlbedo)
{
albedo = Color(1, 1, 1, 1);
return false;
}
const LTexture &tex = *mat.pAlbedo;
tex.Sample(uv, albedo);
return true;
}
} // namespace