2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* baked_light_instance.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* http://www.godotengine.org */
|
|
|
|
/*************************************************************************/
|
2017-01-01 21:01:57 +00:00
|
|
|
/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2014-06-11 13:41:03 +00:00
|
|
|
#include "baked_light_instance.h"
|
2016-12-20 03:21:07 +00:00
|
|
|
#include "light.h"
|
|
|
|
#include "math.h"
|
2017-03-05 15:44:50 +00:00
|
|
|
#include "mesh_instance.h"
|
|
|
|
#include "scene/scene_string_names.h"
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define FINDMINMAX(x0, x1, x2, min, max) \
|
|
|
|
min = max = x0; \
|
|
|
|
if (x1 < min) min = x1; \
|
|
|
|
if (x1 > max) max = x1; \
|
|
|
|
if (x2 < min) min = x2; \
|
|
|
|
if (x2 > max) max = x2;
|
|
|
|
|
|
|
|
static bool planeBoxOverlap(Vector3 normal, float d, Vector3 maxbox) {
|
|
|
|
int q;
|
|
|
|
Vector3 vmin, vmax;
|
|
|
|
for (q = 0; q <= 2; q++) {
|
|
|
|
if (normal[q] > 0.0f) {
|
|
|
|
vmin[q] = -maxbox[q];
|
|
|
|
vmax[q] = maxbox[q];
|
|
|
|
} else {
|
|
|
|
vmin[q] = maxbox[q];
|
|
|
|
vmax[q] = -maxbox[q];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (normal.dot(vmin) + d > 0.0f) return false;
|
|
|
|
if (normal.dot(vmax) + d >= 0.0f) return true;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
/*======================== X-tests ========================*/
|
2017-03-05 15:44:50 +00:00
|
|
|
#define AXISTEST_X01(a, b, fa, fb) \
|
|
|
|
p0 = a * v0.y - b * v0.z; \
|
|
|
|
p2 = a * v2.y - b * v2.z; \
|
|
|
|
if (p0 < p2) { \
|
|
|
|
min = p0; \
|
|
|
|
max = p2; \
|
|
|
|
} else { \
|
|
|
|
min = p2; \
|
|
|
|
max = p0; \
|
|
|
|
} \
|
|
|
|
rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
|
|
|
|
if (min > rad || max < -rad) return false;
|
|
|
|
|
|
|
|
#define AXISTEST_X2(a, b, fa, fb) \
|
|
|
|
p0 = a * v0.y - b * v0.z; \
|
|
|
|
p1 = a * v1.y - b * v1.z; \
|
|
|
|
if (p0 < p1) { \
|
|
|
|
min = p0; \
|
|
|
|
max = p1; \
|
|
|
|
} else { \
|
|
|
|
min = p1; \
|
|
|
|
max = p0; \
|
|
|
|
} \
|
|
|
|
rad = fa * boxhalfsize.y + fb * boxhalfsize.z; \
|
|
|
|
if (min > rad || max < -rad) return false;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
/*======================== Y-tests ========================*/
|
2017-03-05 15:44:50 +00:00
|
|
|
#define AXISTEST_Y02(a, b, fa, fb) \
|
|
|
|
p0 = -a * v0.x + b * v0.z; \
|
|
|
|
p2 = -a * v2.x + b * v2.z; \
|
|
|
|
if (p0 < p2) { \
|
|
|
|
min = p0; \
|
|
|
|
max = p2; \
|
|
|
|
} else { \
|
|
|
|
min = p2; \
|
|
|
|
max = p0; \
|
|
|
|
} \
|
|
|
|
rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
|
|
|
|
if (min > rad || max < -rad) return false;
|
|
|
|
|
|
|
|
#define AXISTEST_Y1(a, b, fa, fb) \
|
|
|
|
p0 = -a * v0.x + b * v0.z; \
|
|
|
|
p1 = -a * v1.x + b * v1.z; \
|
|
|
|
if (p0 < p1) { \
|
|
|
|
min = p0; \
|
|
|
|
max = p1; \
|
|
|
|
} else { \
|
|
|
|
min = p1; \
|
|
|
|
max = p0; \
|
|
|
|
} \
|
|
|
|
rad = fa * boxhalfsize.x + fb * boxhalfsize.z; \
|
|
|
|
if (min > rad || max < -rad) return false;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
/*======================== Z-tests ========================*/
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
#define AXISTEST_Z12(a, b, fa, fb) \
|
|
|
|
p1 = a * v1.x - b * v1.y; \
|
|
|
|
p2 = a * v2.x - b * v2.y; \
|
|
|
|
if (p2 < p1) { \
|
|
|
|
min = p2; \
|
|
|
|
max = p1; \
|
|
|
|
} else { \
|
|
|
|
min = p1; \
|
|
|
|
max = p2; \
|
|
|
|
} \
|
|
|
|
rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
|
|
|
|
if (min > rad || max < -rad) return false;
|
|
|
|
|
|
|
|
#define AXISTEST_Z0(a, b, fa, fb) \
|
|
|
|
p0 = a * v0.x - b * v0.y; \
|
|
|
|
p1 = a * v1.x - b * v1.y; \
|
|
|
|
if (p0 < p1) { \
|
|
|
|
min = p0; \
|
|
|
|
max = p1; \
|
|
|
|
} else { \
|
|
|
|
min = p1; \
|
|
|
|
max = p0; \
|
|
|
|
} \
|
|
|
|
rad = fa * boxhalfsize.x + fb * boxhalfsize.y; \
|
|
|
|
if (min > rad || max < -rad) return false;
|
|
|
|
|
|
|
|
static bool fast_tri_box_overlap(const Vector3 &boxcenter, const Vector3 boxhalfsize, const Vector3 *triverts) {
|
|
|
|
|
|
|
|
/* use separating axis theorem to test overlap between triangle and box */
|
|
|
|
/* need to test for overlap in these directions: */
|
|
|
|
/* 1) the {x,y,z}-directions (actually, since we use the AABB of the triangle */
|
|
|
|
/* we do not even need to test these) */
|
|
|
|
/* 2) normal of the triangle */
|
|
|
|
/* 3) crossproduct(edge from tri, {x,y,z}-directin) */
|
|
|
|
/* this gives 3x3=9 more tests */
|
|
|
|
Vector3 v0, v1, v2;
|
|
|
|
float min, max, d, p0, p1, p2, rad, fex, fey, fez;
|
|
|
|
Vector3 normal, e0, e1, e2;
|
|
|
|
|
|
|
|
/* This is the fastest branch on Sun */
|
|
|
|
/* move everything so that the boxcenter is in (0,0,0) */
|
|
|
|
|
|
|
|
v0 = triverts[0] - boxcenter;
|
|
|
|
v1 = triverts[1] - boxcenter;
|
|
|
|
v2 = triverts[2] - boxcenter;
|
|
|
|
|
|
|
|
/* compute triangle edges */
|
|
|
|
e0 = v1 - v0; /* tri edge 0 */
|
|
|
|
e1 = v2 - v1; /* tri edge 1 */
|
|
|
|
e2 = v0 - v2; /* tri edge 2 */
|
|
|
|
|
|
|
|
/* Bullet 3: */
|
|
|
|
/* test the 9 tests first (this was faster) */
|
|
|
|
fex = Math::abs(e0.x);
|
|
|
|
fey = Math::abs(e0.y);
|
|
|
|
fez = Math::abs(e0.z);
|
|
|
|
AXISTEST_X01(e0.z, e0.y, fez, fey);
|
|
|
|
AXISTEST_Y02(e0.z, e0.x, fez, fex);
|
|
|
|
AXISTEST_Z12(e0.y, e0.x, fey, fex);
|
|
|
|
|
|
|
|
fex = Math::abs(e1.x);
|
|
|
|
fey = Math::abs(e1.y);
|
|
|
|
fez = Math::abs(e1.z);
|
|
|
|
AXISTEST_X01(e1.z, e1.y, fez, fey);
|
|
|
|
AXISTEST_Y02(e1.z, e1.x, fez, fex);
|
|
|
|
AXISTEST_Z0(e1.y, e1.x, fey, fex);
|
|
|
|
|
|
|
|
fex = Math::abs(e2.x);
|
|
|
|
fey = Math::abs(e2.y);
|
|
|
|
fez = Math::abs(e2.z);
|
|
|
|
AXISTEST_X2(e2.z, e2.y, fez, fey);
|
|
|
|
AXISTEST_Y1(e2.z, e2.x, fez, fex);
|
|
|
|
AXISTEST_Z12(e2.y, e2.x, fey, fex);
|
|
|
|
|
|
|
|
/* Bullet 1: */
|
|
|
|
/* first test overlap in the {x,y,z}-directions */
|
|
|
|
/* find min, max of the triangle each direction, and test for overlap in */
|
|
|
|
/* that direction -- this is equivalent to testing a minimal AABB around */
|
|
|
|
/* the triangle against the AABB */
|
|
|
|
|
|
|
|
/* test in X-direction */
|
|
|
|
FINDMINMAX(v0.x, v1.x, v2.x, min, max);
|
|
|
|
if (min > boxhalfsize.x || max < -boxhalfsize.x) return false;
|
|
|
|
|
|
|
|
/* test in Y-direction */
|
|
|
|
FINDMINMAX(v0.y, v1.y, v2.y, min, max);
|
|
|
|
if (min > boxhalfsize.y || max < -boxhalfsize.y) return false;
|
|
|
|
|
|
|
|
/* test in Z-direction */
|
|
|
|
FINDMINMAX(v0.z, v1.z, v2.z, min, max);
|
|
|
|
if (min > boxhalfsize.z || max < -boxhalfsize.z) return false;
|
|
|
|
|
|
|
|
/* Bullet 2: */
|
|
|
|
/* test if the box intersects the plane of the triangle */
|
|
|
|
/* compute plane equation of triangle: normal*x+d=0 */
|
|
|
|
normal = e0.cross(e1);
|
|
|
|
d = -normal.dot(v0); /* plane eq: normal.x+d=0 */
|
|
|
|
if (!planeBoxOverlap(normal, d, boxhalfsize)) return false;
|
|
|
|
|
|
|
|
return true; /* box and triangle overlaps */
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector<Color> BakedLight::_get_bake_texture(Image &p_image, const Color &p_color) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector<Color> ret;
|
|
|
|
|
|
|
|
if (p_image.empty()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ret.resize(bake_texture_size * bake_texture_size);
|
|
|
|
for (int i = 0; i < bake_texture_size * bake_texture_size; i++) {
|
|
|
|
ret[i] = p_color;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
p_image.convert(Image::FORMAT_RGBA8);
|
2017-03-05 15:44:50 +00:00
|
|
|
p_image.resize(bake_texture_size, bake_texture_size, Image::INTERPOLATE_CUBIC);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<uint8_t>::Read r = p_image.get_data().read();
|
2017-03-05 15:44:50 +00:00
|
|
|
ret.resize(bake_texture_size * bake_texture_size);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < bake_texture_size * bake_texture_size; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
Color c;
|
2017-03-05 15:44:50 +00:00
|
|
|
c.r = r[i * 4 + 0] / 255.0;
|
|
|
|
c.g = r[i * 4 + 1] / 255.0;
|
|
|
|
c.b = r[i * 4 + 2] / 255.0;
|
|
|
|
c.a = r[i * 4 + 3] / 255.0;
|
|
|
|
ret[i] = c;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
BakedLight::MaterialCache BakedLight::_get_material_cache(Ref<Material> p_material) {
|
|
|
|
|
|
|
|
//this way of obtaining materials is inaccurate and also does not support some compressed formats very well
|
|
|
|
Ref<FixedSpatialMaterial> mat = p_material;
|
|
|
|
|
|
|
|
Ref<Material> material = mat; //hack for now
|
|
|
|
|
|
|
|
if (material_cache.has(material)) {
|
|
|
|
return material_cache[material];
|
|
|
|
}
|
|
|
|
|
|
|
|
MaterialCache mc;
|
|
|
|
|
|
|
|
if (mat.is_valid()) {
|
|
|
|
|
|
|
|
Ref<ImageTexture> albedo_tex = mat->get_texture(FixedSpatialMaterial::TEXTURE_ALBEDO);
|
|
|
|
|
|
|
|
Image img_albedo;
|
|
|
|
if (albedo_tex.is_valid()) {
|
|
|
|
|
|
|
|
img_albedo = albedo_tex->get_data();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mc.albedo = _get_bake_texture(img_albedo, mat->get_albedo());
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Ref<ImageTexture> emission_tex = mat->get_texture(FixedSpatialMaterial::TEXTURE_EMISSION);
|
|
|
|
|
|
|
|
Color emission_col = mat->get_emission();
|
2017-03-05 15:44:50 +00:00
|
|
|
emission_col.r *= mat->get_emission_energy();
|
|
|
|
emission_col.g *= mat->get_emission_energy();
|
|
|
|
emission_col.b *= mat->get_emission_energy();
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Image img_emission;
|
|
|
|
|
|
|
|
if (emission_tex.is_valid()) {
|
|
|
|
|
|
|
|
img_emission = emission_tex->get_data();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mc.emission = _get_bake_texture(img_emission, emission_col);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
Image empty;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mc.albedo = _get_bake_texture(empty, Color(0.7, 0.7, 0.7));
|
|
|
|
mc.emission = _get_bake_texture(empty, Color(0, 0, 0));
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
material_cache[p_material] = mc;
|
2016-12-20 03:21:07 +00:00
|
|
|
return mc;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
static _FORCE_INLINE_ Vector2 get_uv(const Vector3 &p_pos, const Vector3 *p_vtx, const Vector2 *p_uv) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_pos.distance_squared_to(p_vtx[0]) < CMP_EPSILON2)
|
2016-12-20 03:21:07 +00:00
|
|
|
return p_uv[0];
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_pos.distance_squared_to(p_vtx[1]) < CMP_EPSILON2)
|
2016-12-20 03:21:07 +00:00
|
|
|
return p_uv[1];
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_pos.distance_squared_to(p_vtx[2]) < CMP_EPSILON2)
|
2016-12-20 03:21:07 +00:00
|
|
|
return p_uv[2];
|
|
|
|
|
|
|
|
Vector3 v0 = p_vtx[1] - p_vtx[0];
|
|
|
|
Vector3 v1 = p_vtx[2] - p_vtx[0];
|
|
|
|
Vector3 v2 = p_pos - p_vtx[0];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float d00 = v0.dot(v0);
|
|
|
|
float d01 = v0.dot(v1);
|
|
|
|
float d11 = v1.dot(v1);
|
|
|
|
float d20 = v2.dot(v0);
|
|
|
|
float d21 = v2.dot(v1);
|
2016-12-20 03:21:07 +00:00
|
|
|
float denom = (d00 * d11 - d01 * d01);
|
2017-03-05 15:44:50 +00:00
|
|
|
if (denom == 0)
|
2016-12-20 03:21:07 +00:00
|
|
|
return p_uv[0];
|
|
|
|
float v = (d11 * d20 - d01 * d21) / denom;
|
|
|
|
float w = (d00 * d21 - d01 * d20) / denom;
|
|
|
|
float u = 1.0f - v - w;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return p_uv[0] * u + p_uv[1] * v + p_uv[2] * w;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_plot_face(int p_idx, int p_level, const Vector3 *p_vtx, const Vector2 *p_uv, const MaterialCache &p_material, const Rect3 &p_aabb) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_level == cell_subdiv - 1) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//plot the face by guessing it's albedo and emission value
|
|
|
|
|
|
|
|
//find best axis to map to, for scanning values
|
|
|
|
int closest_axis;
|
|
|
|
float closest_dot;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 normal = Plane(p_vtx[0], p_vtx[1], p_vtx[2]).normal;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 axis;
|
2017-03-05 15:44:50 +00:00
|
|
|
axis[i] = 1.0;
|
|
|
|
float dot = ABS(normal.dot(axis));
|
|
|
|
if (i == 0 || dot > closest_dot) {
|
|
|
|
closest_axis = i;
|
|
|
|
closest_dot = dot;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 axis;
|
2017-03-05 15:44:50 +00:00
|
|
|
axis[closest_axis] = 1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
Vector3 t1;
|
2017-03-05 15:44:50 +00:00
|
|
|
t1[(closest_axis + 1) % 3] = 1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
Vector3 t2;
|
2017-03-05 15:44:50 +00:00
|
|
|
t2[(closest_axis + 2) % 3] = 1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
t1 *= p_aabb.size[(closest_axis + 1) % 3] / float(color_scan_cell_width);
|
|
|
|
t2 *= p_aabb.size[(closest_axis + 2) % 3] / float(color_scan_cell_width);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Color albedo_accum;
|
|
|
|
Color emission_accum;
|
2017-03-05 15:44:50 +00:00
|
|
|
float alpha = 0.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//map to a grid average in the best axis for this face
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < color_scan_cell_width; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 ofs_i = float(i) * t1;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < color_scan_cell_width; j++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 ofs_j = float(j) * t2;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 from = p_aabb.pos + ofs_i + ofs_j;
|
2016-12-20 03:21:07 +00:00
|
|
|
Vector3 to = from + t1 + t2 + axis * p_aabb.size[closest_axis];
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 half = (to - from) * 0.5;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//is in this cell?
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!fast_tri_box_overlap(from + half, half, p_vtx)) {
|
2016-12-20 03:21:07 +00:00
|
|
|
continue; //face does not span this cell
|
|
|
|
}
|
|
|
|
|
|
|
|
//go from -size to +size*2 to avoid skipping collisions
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 ray_from = from + (t1 + t2) * 0.5 - axis * p_aabb.size[closest_axis];
|
|
|
|
Vector3 ray_to = ray_from + axis * p_aabb.size[closest_axis] * 2;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 intersection;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!Geometry::ray_intersects_triangle(ray_from, ray_to, p_vtx[0], p_vtx[1], p_vtx[2], &intersection)) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//no intersect? look in edges
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float closest_dist = 1e20;
|
|
|
|
for (int j = 0; j < 3; j++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
Vector3 c;
|
|
|
|
Vector3 inters;
|
2017-03-05 15:44:50 +00:00
|
|
|
Geometry::get_closest_points_between_segments(p_vtx[j], p_vtx[(j + 1) % 3], ray_from, ray_to, inters, c);
|
|
|
|
float d = c.distance_to(intersection);
|
|
|
|
if (j == 0 || d < closest_dist) {
|
|
|
|
closest_dist = d;
|
|
|
|
intersection = inters;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 uv = get_uv(intersection, p_vtx, p_uv);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int uv_x = CLAMP(Math::fposmod(uv.x, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
|
|
|
|
int uv_y = CLAMP(Math::fposmod(uv.y, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = uv_y * bake_texture_size + uv_x;
|
|
|
|
albedo_accum.r += p_material.albedo[ofs].r;
|
|
|
|
albedo_accum.g += p_material.albedo[ofs].g;
|
|
|
|
albedo_accum.b += p_material.albedo[ofs].b;
|
|
|
|
albedo_accum.a += p_material.albedo[ofs].a;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
emission_accum.r += p_material.emission[ofs].r;
|
|
|
|
emission_accum.g += p_material.emission[ofs].g;
|
|
|
|
emission_accum.b += p_material.emission[ofs].b;
|
|
|
|
alpha += 1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (alpha == 0) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//could not in any way get texture information.. so use closest point to center
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Face3 f(p_vtx[0], p_vtx[1], p_vtx[2]);
|
|
|
|
Vector3 inters = f.get_closest_point_to(p_aabb.pos + p_aabb.size * 0.5);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector2 uv = get_uv(inters, p_vtx, p_uv);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int uv_x = CLAMP(Math::fposmod(uv.x, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
|
|
|
|
int uv_y = CLAMP(Math::fposmod(uv.y, 1.0f) * bake_texture_size, 0, bake_texture_size - 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = uv_y * bake_texture_size + uv_x;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
alpha = 1.0 / (color_scan_cell_width * color_scan_cell_width);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
albedo_accum.r = p_material.albedo[ofs].r * alpha;
|
|
|
|
albedo_accum.g = p_material.albedo[ofs].g * alpha;
|
|
|
|
albedo_accum.b = p_material.albedo[ofs].b * alpha;
|
|
|
|
albedo_accum.a = p_material.albedo[ofs].a * alpha;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
emission_accum.r = p_material.emission[ofs].r * alpha;
|
|
|
|
emission_accum.g = p_material.emission[ofs].g * alpha;
|
|
|
|
emission_accum.b = p_material.emission[ofs].b * alpha;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
zero_alphas++;
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float accdiv = 1.0 / (color_scan_cell_width * color_scan_cell_width);
|
|
|
|
alpha *= accdiv;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
albedo_accum.r *= accdiv;
|
|
|
|
albedo_accum.g *= accdiv;
|
|
|
|
albedo_accum.b *= accdiv;
|
|
|
|
albedo_accum.a *= accdiv;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
emission_accum.r *= accdiv;
|
|
|
|
emission_accum.g *= accdiv;
|
|
|
|
emission_accum.b *= accdiv;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//put this temporarily here, corrected in a later step
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].albedo[0] += albedo_accum.r;
|
|
|
|
bake_cells_write[p_idx].albedo[1] += albedo_accum.g;
|
|
|
|
bake_cells_write[p_idx].albedo[2] += albedo_accum.b;
|
|
|
|
bake_cells_write[p_idx].light[0] += emission_accum.r;
|
|
|
|
bake_cells_write[p_idx].light[1] += emission_accum.g;
|
|
|
|
bake_cells_write[p_idx].light[2] += emission_accum.b;
|
|
|
|
bake_cells_write[p_idx].alpha += alpha;
|
|
|
|
|
|
|
|
static const Vector3 side_normals[6] = {
|
2016-12-20 03:21:07 +00:00
|
|
|
Vector3(-1, 0, 0),
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3(1, 0, 0),
|
|
|
|
Vector3(0, -1, 0),
|
|
|
|
Vector3(0, 1, 0),
|
|
|
|
Vector3(0, 0, -1),
|
|
|
|
Vector3(0, 0, 1),
|
2016-12-20 03:21:07 +00:00
|
|
|
};
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 6; i++) {
|
|
|
|
if (normal.dot(side_normals[i]) > CMP_EPSILON) {
|
|
|
|
bake_cells_write[p_idx].used_sides |= (1 << i);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
//go down
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Rect3 aabb = p_aabb;
|
|
|
|
aabb.size *= 0.5;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i & 1)
|
|
|
|
aabb.pos.x += aabb.size.x;
|
|
|
|
if (i & 2)
|
|
|
|
aabb.pos.y += aabb.size.y;
|
|
|
|
if (i & 4)
|
|
|
|
aabb.pos.z += aabb.size.z;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
{
|
2017-03-05 15:44:50 +00:00
|
|
|
Rect3 test_aabb = aabb;
|
2016-12-20 03:21:07 +00:00
|
|
|
//test_aabb.grow_by(test_aabb.get_longest_axis_size()*0.05); //grow a bit to avoid numerical error in real-time
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 qsize = test_aabb.size * 0.5; //quarter size, for fast aabb test
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!fast_tri_box_overlap(test_aabb.pos + qsize, qsize, p_vtx)) {
|
|
|
|
//if (!Face3(p_vtx[0],p_vtx[1],p_vtx[2]).intersects_aabb2(aabb)) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//does not fit in child, go on
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (bake_cells_write[p_idx].childs[i] == CHILD_EMPTY) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//sub cell must be created
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (bake_cells_used == (1 << bake_cells_alloc)) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//exhausted cells, creating more space
|
|
|
|
bake_cells_alloc++;
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write = PoolVector<BakeCell>::Write();
|
|
|
|
bake_cells.resize(1 << bake_cells_alloc);
|
|
|
|
bake_cells_write = bake_cells.write();
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].childs[i] = bake_cells_used;
|
|
|
|
bake_cells_level_used[p_level + 1]++;
|
2016-12-20 03:21:07 +00:00
|
|
|
bake_cells_used++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_plot_face(bake_cells_write[p_idx].childs[i], p_level + 1, p_vtx, p_uv, p_material, aabb);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_fixup_plot(int p_idx, int p_level, int p_x, int p_y, int p_z) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_level == cell_subdiv - 1) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
float alpha = bake_cells_write[p_idx].alpha;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].albedo[0] /= alpha;
|
|
|
|
bake_cells_write[p_idx].albedo[1] /= alpha;
|
|
|
|
bake_cells_write[p_idx].albedo[2] /= alpha;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//transfer emission to light
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].light[0] /= alpha;
|
|
|
|
bake_cells_write[p_idx].light[1] /= alpha;
|
|
|
|
bake_cells_write[p_idx].light[2] /= alpha;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].alpha = 1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//remove neighbours from used sides
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int n = 0; n < 6; n++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs[3] = { 0, 0, 0 };
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ofs[n / 2] = (n & 1) ? 1 : -1;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//convert to x,y,z on this level
|
2017-03-05 15:44:50 +00:00
|
|
|
int x = p_x;
|
|
|
|
int y = p_y;
|
|
|
|
int z = p_z;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
x += ofs[0];
|
|
|
|
y += ofs[1];
|
|
|
|
z += ofs[2];
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs_x = 0;
|
|
|
|
int ofs_y = 0;
|
|
|
|
int ofs_z = 0;
|
|
|
|
int size = 1 << p_level;
|
|
|
|
int half = size / 2;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (x < 0 || x >= size || y < 0 || y >= size || z < 0 || z >= size) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//neighbour is out, can't use it
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].used_sides &= ~(1 << uint32_t(n));
|
2016-12-20 03:21:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t neighbour = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < cell_subdiv - 1; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
BakeCell *bc = &bake_cells_write[neighbour];
|
|
|
|
|
|
|
|
int child = 0;
|
|
|
|
if (x >= ofs_x + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 1;
|
|
|
|
ofs_x += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
if (y >= ofs_y + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 2;
|
|
|
|
ofs_y += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
if (z >= ofs_z + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 4;
|
|
|
|
ofs_z += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
neighbour = bc->childs[child];
|
2017-03-05 15:44:50 +00:00
|
|
|
if (neighbour == CHILD_EMPTY) {
|
2016-12-20 03:21:07 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
half >>= 1;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (neighbour != CHILD_EMPTY) {
|
|
|
|
bake_cells_write[p_idx].used_sides &= ~(1 << uint32_t(n));
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
//go down
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float alpha_average = 0;
|
|
|
|
int half = cells_per_axis >> (p_level + 1);
|
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
uint32_t child = bake_cells_write[p_idx].childs[i];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (child == CHILD_EMPTY)
|
2016-12-20 03:21:07 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int nx = p_x;
|
|
|
|
int ny = p_y;
|
|
|
|
int nz = p_z;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i & 1)
|
|
|
|
nx += half;
|
|
|
|
if (i & 2)
|
|
|
|
ny += half;
|
|
|
|
if (i & 4)
|
|
|
|
nz += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_fixup_plot(child, p_level + 1, nx, ny, nz);
|
|
|
|
alpha_average += bake_cells_write[child].alpha;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].alpha = alpha_average / 8.0;
|
|
|
|
bake_cells_write[p_idx].light[0] = 0;
|
|
|
|
bake_cells_write[p_idx].light[1] = 0;
|
|
|
|
bake_cells_write[p_idx].light[2] = 0;
|
|
|
|
bake_cells_write[p_idx].albedo[0] = 0;
|
|
|
|
bake_cells_write[p_idx].albedo[1] = 0;
|
|
|
|
bake_cells_write[p_idx].albedo[2] = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//clean up light
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].light_pass = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
//find neighbours
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_bake_add_mesh(const Transform &p_xform, Ref<Mesh> &p_mesh) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_mesh->get_surface_count(); i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
|
2016-12-20 03:21:07 +00:00
|
|
|
continue; //only triangles
|
|
|
|
|
|
|
|
MaterialCache material = _get_material_cache(p_mesh->surface_get_material(i));
|
|
|
|
|
|
|
|
Array a = p_mesh->surface_get_arrays(i);
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
|
2017-03-05 15:44:50 +00:00
|
|
|
PoolVector<Vector3>::Read vr = vertices.read();
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Vector2> uv = a[Mesh::ARRAY_TEX_UV];
|
|
|
|
PoolVector<Vector2>::Read uvr;
|
|
|
|
PoolVector<int> index = a[Mesh::ARRAY_INDEX];
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool read_uv = false;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
if (uv.size()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uvr = uv.read();
|
|
|
|
read_uv = true;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (index.size()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int facecount = index.size() / 3;
|
|
|
|
PoolVector<int>::Read ir = index.read();
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < facecount; j++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 vtxs[3];
|
|
|
|
Vector2 uvs[3];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
vtxs[k] = p_xform.xform(vr[ir[j * 3 + k]]);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (read_uv) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
uvs[k] = uvr[ir[j * 3 + k]];
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//plot face
|
2017-03-05 15:44:50 +00:00
|
|
|
_plot_face(0, 0, vtxs, uvs, material, bounds);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int facecount = vertices.size() / 3;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < facecount; j++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 vtxs[3];
|
|
|
|
Vector2 uvs[3];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
vtxs[k] = p_xform.xform(vr[j * 3 + k]);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (read_uv) {
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
|
|
|
uvs[k] = uvr[j * 3 + k];
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//plot face
|
2017-03-05 15:44:50 +00:00
|
|
|
_plot_face(0, 0, vtxs, uvs, material, bounds);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_bake_add_to_aabb(const Transform &p_xform, Ref<Mesh> &p_mesh, bool &first) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < p_mesh->get_surface_count(); i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_mesh->surface_get_primitive_type(i) != Mesh::PRIMITIVE_TRIANGLES)
|
2016-12-20 03:21:07 +00:00
|
|
|
continue; //only triangles
|
|
|
|
|
|
|
|
Array a = p_mesh->surface_get_arrays(i);
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Vector3> vertices = a[Mesh::ARRAY_VERTEX];
|
2016-12-20 03:21:07 +00:00
|
|
|
int vc = vertices.size();
|
2017-03-05 15:44:50 +00:00
|
|
|
PoolVector<Vector3>::Read vr = vertices.read();
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
if (first) {
|
2017-03-05 15:44:50 +00:00
|
|
|
bounds.pos = p_xform.xform(vr[0]);
|
|
|
|
first = false;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < vc; j++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
bounds.expand_to(p_xform.xform(vr[j]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLight::bake() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_alloc = 16;
|
|
|
|
bake_cells.resize(1 << bake_cells_alloc);
|
|
|
|
bake_cells_used = 1;
|
|
|
|
cells_per_axis = (1 << (cell_subdiv - 1));
|
|
|
|
zero_alphas = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool aabb_first = true;
|
2016-12-20 03:21:07 +00:00
|
|
|
print_line("Generating AABB");
|
|
|
|
|
|
|
|
bake_cells_level_used.resize(cell_subdiv);
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < cell_subdiv; i++) {
|
|
|
|
bake_cells_level_used[i] = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int count = 0;
|
|
|
|
for (Set<GeometryInstance *>::Element *E = geometries.front(); E; E = E->next()) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("aabb geom " + itos(count) + "/" + itos(geometries.size()));
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
GeometryInstance *geom = E->get();
|
|
|
|
|
|
|
|
if (geom->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
MeshInstance *mesh_instance = geom->cast_to<MeshInstance>();
|
|
|
|
Ref<Mesh> mesh = mesh_instance->get_mesh();
|
|
|
|
if (mesh.is_valid()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_bake_add_to_aabb(geom->get_relative_transform(this), mesh, aabb_first);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("AABB: " + bounds);
|
2016-12-20 03:21:07 +00:00
|
|
|
ERR_FAIL_COND(aabb_first);
|
|
|
|
|
|
|
|
bake_cells_write = bake_cells.write();
|
2017-03-05 15:44:50 +00:00
|
|
|
count = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Set<GeometryInstance *>::Element *E = geometries.front(); E; E = E->next()) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
GeometryInstance *geom = E->get();
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("plot geom " + itos(count) + "/" + itos(geometries.size()));
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
if (geom->cast_to<MeshInstance>()) {
|
|
|
|
|
|
|
|
MeshInstance *mesh_instance = geom->cast_to<MeshInstance>();
|
|
|
|
Ref<Mesh> mesh = mesh_instance->get_mesh();
|
|
|
|
if (mesh.is_valid()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_bake_add_mesh(geom->get_relative_transform(this), mesh);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_fixup_plot(0, 0, 0, 0, 0);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write = PoolVector<BakeCell>::Write();
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
bake_cells.resize(bake_cells_used);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("total bake cells used: " + itos(bake_cells_used));
|
|
|
|
for (int i = 0; i < cell_subdiv; i++) {
|
|
|
|
print_line("level " + itos(i) + ": " + itos(bake_cells_level_used[i]));
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("zero alphas: " + itos(zero_alphas));
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_bake_directional(int p_idx, int p_level, int p_x, int p_y, int p_z, const Vector3 &p_dir, const Color &p_color, int p_sign) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_level == cell_subdiv - 1) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 end;
|
2017-03-05 15:44:50 +00:00
|
|
|
end.x = float(p_x + 0.5) / cells_per_axis;
|
|
|
|
end.y = float(p_y + 0.5) / cells_per_axis;
|
|
|
|
end.z = float(p_z + 0.5) / cells_per_axis;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
end = bounds.pos + bounds.size * end;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float max_ray_len = (bounds.size).length() * 1.2;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 begin = end + max_ray_len * -p_dir;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//clip begin
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (ABS(p_dir[i]) < CMP_EPSILON) {
|
2016-12-20 03:21:07 +00:00
|
|
|
continue; // parallel to axis, don't clip
|
|
|
|
}
|
|
|
|
|
|
|
|
Plane p;
|
2017-03-05 15:44:50 +00:00
|
|
|
p.normal[i] = 1.0;
|
|
|
|
p.d = bounds.pos[i];
|
|
|
|
if (p_dir[i] < 0) {
|
|
|
|
p.d += bounds.size[i];
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector3 inters;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p.intersects_segment(end, begin, &inters)) {
|
|
|
|
begin = inters;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = _plot_ray(begin, end);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (idx >= 0 && light_pass != bake_cells_write[idx].light_pass) {
|
2016-12-20 03:21:07 +00:00
|
|
|
//hit something, add or remove light to it
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color albedo = Color(bake_cells_write[idx].albedo[0], bake_cells_write[idx].albedo[1], bake_cells_write[idx].albedo[2]);
|
|
|
|
bake_cells_write[idx].light[0] += albedo.r * p_color.r * p_sign;
|
|
|
|
bake_cells_write[idx].light[1] += albedo.g * p_color.g * p_sign;
|
|
|
|
bake_cells_write[idx].light[2] += albedo.b * p_color.b * p_sign;
|
|
|
|
bake_cells_write[idx].light_pass = light_pass;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int half = cells_per_axis >> (p_level + 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//go down
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
uint32_t child = bake_cells_write[p_idx].childs[i];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (child == CHILD_EMPTY)
|
2016-12-20 03:21:07 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int nx = p_x;
|
|
|
|
int ny = p_y;
|
|
|
|
int nz = p_z;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i & 1)
|
|
|
|
nx += half;
|
|
|
|
if (i & 2)
|
|
|
|
ny += half;
|
|
|
|
if (i & 4)
|
|
|
|
nz += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_bake_directional(child, p_level + 1, nx, ny, nz, p_dir, p_color, p_sign);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_bake_light(Light *p_light) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
if (p_light->cast_to<DirectionalLight>()) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
DirectionalLight *dl = p_light->cast_to<DirectionalLight>();
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Transform rel_xf = dl->get_relative_transform(this);
|
|
|
|
|
|
|
|
Vector3 light_dir = -rel_xf.basis.get_axis(2);
|
|
|
|
|
|
|
|
Color color = dl->get_color();
|
2017-01-14 17:03:38 +00:00
|
|
|
float nrg = dl->get_param(Light::PARAM_ENERGY);
|
2017-03-05 15:44:50 +00:00
|
|
|
color.r *= nrg;
|
|
|
|
color.g *= nrg;
|
|
|
|
color.b *= nrg;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
light_pass++;
|
2017-03-05 15:44:50 +00:00
|
|
|
_bake_directional(0, 0, 0, 0, 0, light_dir, color, 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_upscale_light(int p_idx, int p_level) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//go down
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float light_accum[3] = { 0, 0, 0 };
|
|
|
|
float alpha_accum = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool check_children = p_level < (cell_subdiv - 2);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
uint32_t child = bake_cells_write[p_idx].childs[i];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (child == CHILD_EMPTY)
|
2016-12-20 03:21:07 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (check_children) {
|
2017-03-05 15:44:50 +00:00
|
|
|
_upscale_light(child, p_level + 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
light_accum[0] += bake_cells_write[child].light[0];
|
|
|
|
light_accum[1] += bake_cells_write[child].light[1];
|
|
|
|
light_accum[2] += bake_cells_write[child].light[2];
|
|
|
|
alpha_accum += bake_cells_write[child].alpha;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].light[0] = light_accum[0] / 8.0;
|
|
|
|
bake_cells_write[p_idx].light[1] = light_accum[1] / 8.0;
|
|
|
|
bake_cells_write[p_idx].light[2] = light_accum[2] / 8.0;
|
|
|
|
bake_cells_write[p_idx].alpha = alpha_accum / 8.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLight::bake_lights() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(bake_cells.size() == 0);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
bake_cells_write = bake_cells.write();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (Set<Light *>::Element *E = lights.front(); E; E = E->next()) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
_bake_light(E->get());
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_upscale_light(0, 0);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write = PoolVector<BakeCell>::Write();
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color BakedLight::_cone_trace(const Vector3 &p_from, const Vector3 &p_dir, float p_half_angle) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color color(0, 0, 0, 0);
|
|
|
|
float tha = Math::tan(p_half_angle); //tan half angle
|
|
|
|
Vector3 from = (p_from - bounds.pos) / bounds.size; //convert to 0..1
|
|
|
|
from /= cells_per_axis; //convert to voxels of size 1
|
|
|
|
Vector3 dir = (p_dir / bounds.size).normalized();
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float max_dist = Vector3(cells_per_axis, cells_per_axis, cells_per_axis).length();
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
float dist = 1.0;
|
|
|
|
// self occlusion in flat surfaces
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
float alpha = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
while (dist < max_dist && alpha < 0.95) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
#if 0
|
|
|
|
// smallest sample diameter possible is the voxel size
|
|
|
|
float diameter = MAX(1.0, 2.0 * tha * dist);
|
|
|
|
float lod = log2(diameter);
|
|
|
|
|
|
|
|
Vector3 sample_pos = from + dist * dir;
|
|
|
|
|
|
|
|
|
|
|
|
Color samples_base[2][8]={{Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0)},
|
|
|
|
{Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0),Color(0,0,0,0)}};
|
|
|
|
|
|
|
|
float levelf = Math::fposmod(lod,1.0);
|
|
|
|
float fx = Math::fposmod(sample_pos.x,1.0);
|
|
|
|
float fy = Math::fposmod(sample_pos.y,1.0);
|
|
|
|
float fz = Math::fposmod(sample_pos.z,1.0);
|
|
|
|
|
|
|
|
for(int l=0;l<2;l++){
|
|
|
|
|
|
|
|
int bx = Math::floor(sample_pos.x);
|
|
|
|
int by = Math::floor(sample_pos.y);
|
|
|
|
int bz = Math::floor(sample_pos.z);
|
|
|
|
|
|
|
|
int lodn=int(Math::floor(lod))-l;
|
|
|
|
|
|
|
|
bx>>=lodn;
|
|
|
|
by>>=lodn;
|
|
|
|
bz>>=lodn;
|
|
|
|
|
|
|
|
int limit = MAX(0,cell_subdiv-lodn-1);
|
|
|
|
|
|
|
|
for(int c=0;c<8;c++) {
|
|
|
|
|
|
|
|
int x = bx;
|
|
|
|
int y = by;
|
|
|
|
int z = bz;
|
|
|
|
|
|
|
|
if (c&1) {
|
|
|
|
x+=1;
|
|
|
|
}
|
|
|
|
if (c&2) {
|
|
|
|
y+=1;
|
|
|
|
}
|
|
|
|
if (c&4) {
|
|
|
|
z+=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ofs_x=0;
|
|
|
|
int ofs_y=0;
|
|
|
|
int ofs_z=0;
|
|
|
|
int size = cells_per_axis>>lodn;
|
|
|
|
int half=size/2;
|
|
|
|
|
|
|
|
bool outside=x<0 || x>=size || y<0 || y>=size || z<0 || z>=size;
|
|
|
|
|
|
|
|
if (outside)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t cell=0;
|
|
|
|
|
|
|
|
for(int i=0;i<limit;i++) {
|
|
|
|
|
|
|
|
BakeCell *bc = &bake_cells_write[cell];
|
|
|
|
|
|
|
|
int child = 0;
|
|
|
|
if (x >= ofs_x + half) {
|
|
|
|
child|=1;
|
|
|
|
ofs_x+=half;
|
|
|
|
}
|
|
|
|
if (y >= ofs_y + half) {
|
|
|
|
child|=2;
|
|
|
|
ofs_y+=half;
|
|
|
|
}
|
|
|
|
if (z >= ofs_z + half) {
|
|
|
|
child|=4;
|
|
|
|
ofs_z+=half;
|
|
|
|
}
|
|
|
|
|
|
|
|
cell = bc->childs[child];
|
|
|
|
if (cell==CHILD_EMPTY)
|
|
|
|
break;
|
|
|
|
|
|
|
|
half>>=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cell!=CHILD_EMPTY) {
|
|
|
|
|
|
|
|
samples_base[l][c].r=bake_cells_write[cell].light[0];
|
|
|
|
samples_base[l][c].g=bake_cells_write[cell].light[1];
|
|
|
|
samples_base[l][c].b=bake_cells_write[cell].light[2];
|
|
|
|
samples_base[l][c].a=bake_cells_write[cell].alpha;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Color m0x0 = samples_base[0][0].linear_interpolate(samples_base[0][1],fx);
|
|
|
|
Color m0x1 = samples_base[0][2].linear_interpolate(samples_base[0][3],fx);
|
|
|
|
Color m0y0 = m0x0.linear_interpolate(m0x1,fy);
|
|
|
|
m0x0 = samples_base[0][4].linear_interpolate(samples_base[0][5],fx);
|
|
|
|
m0x1 = samples_base[0][6].linear_interpolate(samples_base[0][7],fx);
|
|
|
|
Color m0y1 = m0x0.linear_interpolate(m0x1,fy);
|
|
|
|
Color m0z = m0y0.linear_interpolate(m0y1,fz);
|
|
|
|
|
|
|
|
Color m1x0 = samples_base[1][0].linear_interpolate(samples_base[1][1],fx);
|
|
|
|
Color m1x1 = samples_base[1][2].linear_interpolate(samples_base[1][3],fx);
|
|
|
|
Color m1y0 = m1x0.linear_interpolate(m1x1,fy);
|
|
|
|
m1x0 = samples_base[1][4].linear_interpolate(samples_base[1][5],fx);
|
|
|
|
m1x1 = samples_base[1][6].linear_interpolate(samples_base[1][7],fx);
|
|
|
|
Color m1y1 = m1x0.linear_interpolate(m1x1,fy);
|
|
|
|
Color m1z = m1y0.linear_interpolate(m1y1,fz);
|
|
|
|
|
|
|
|
Color m = m0z.linear_interpolate(m1z,levelf);
|
|
|
|
#else
|
|
|
|
float diameter = 1.0;
|
|
|
|
Vector3 sample_pos = from + dist * dir;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color m(0, 0, 0, 0);
|
2016-12-20 03:21:07 +00:00
|
|
|
{
|
|
|
|
int x = Math::floor(sample_pos.x);
|
|
|
|
int y = Math::floor(sample_pos.y);
|
|
|
|
int z = Math::floor(sample_pos.z);
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs_x = 0;
|
|
|
|
int ofs_y = 0;
|
|
|
|
int ofs_z = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
int size = cells_per_axis;
|
2017-03-05 15:44:50 +00:00
|
|
|
int half = size / 2;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bool outside = x < 0 || x >= size || y < 0 || y >= size || z < 0 || z >= size;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
if (!outside) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t cell = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < cell_subdiv - 1; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
BakeCell *bc = &bake_cells_write[cell];
|
|
|
|
|
|
|
|
int child = 0;
|
|
|
|
if (x >= ofs_x + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 1;
|
|
|
|
ofs_x += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
if (y >= ofs_y + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 2;
|
|
|
|
ofs_y += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
if (z >= ofs_z + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 4;
|
|
|
|
ofs_z += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cell = bc->childs[child];
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cell == CHILD_EMPTY)
|
2016-12-20 03:21:07 +00:00
|
|
|
break;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
half >>= 1;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cell != CHILD_EMPTY) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
m.r = bake_cells_write[cell].light[0];
|
|
|
|
m.g = bake_cells_write[cell].light[1];
|
|
|
|
m.b = bake_cells_write[cell].light[2];
|
|
|
|
m.a = bake_cells_write[cell].alpha;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
// front-to-back compositing
|
|
|
|
float a = (1.0 - alpha);
|
|
|
|
color.r += a * m.r;
|
|
|
|
color.g += a * m.g;
|
|
|
|
color.b += a * m.b;
|
|
|
|
alpha += a * m.a;
|
|
|
|
//occlusion += a * voxelColor.a;
|
|
|
|
//occlusion += (a * voxelColor.a) / (1.0 + 0.03 * diameter);
|
|
|
|
dist += diameter * 0.5; // smoother
|
|
|
|
//dist += diameter; // faster but misses more voxels
|
|
|
|
}
|
|
|
|
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_bake_radiance(int p_idx, int p_level, int p_x, int p_y, int p_z) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_level == cell_subdiv - 1) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
const int NUM_CONES = 6;
|
|
|
|
Vector3 cone_directions[6] = {
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3(1, 0, 0),
|
|
|
|
Vector3(0.5, 0.866025, 0),
|
|
|
|
Vector3(0.5, 0.267617, 0.823639),
|
|
|
|
Vector3(0.5, -0.700629, 0.509037),
|
|
|
|
Vector3(0.5, -0.700629, -0.509037),
|
|
|
|
Vector3(0.5, 0.267617, -0.823639)
|
|
|
|
};
|
|
|
|
float coneWeights[6] = { 0.25, 0.15, 0.15, 0.15, 0.15, 0.15 };
|
|
|
|
|
|
|
|
Vector3 pos = (Vector3(p_x, p_y, p_z) / float(cells_per_axis)) * bounds.size + bounds.pos;
|
|
|
|
Vector3 voxel_size = bounds.size / float(cells_per_axis);
|
|
|
|
pos += voxel_size * 0.5;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Color accum;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write[p_idx].light[0] = 0;
|
|
|
|
bake_cells_write[p_idx].light[1] = 0;
|
|
|
|
bake_cells_write[p_idx].light[2] = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int freepix = 0;
|
|
|
|
for (int i = 0; i < 6; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (!(bake_cells_write[p_idx].used_sides & (1 << i)))
|
2016-12-20 03:21:07 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((i & 1) == 0)
|
|
|
|
bake_cells_write[p_idx].light[i / 2] = 1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
freepix++;
|
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs = i / 2;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 dir;
|
2017-03-05 15:44:50 +00:00
|
|
|
if ((i & 1) == 0)
|
|
|
|
dir[ofs] = 1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
dir[ofs] = -1.0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < 1; j++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 cone_dir;
|
2017-03-05 15:44:50 +00:00
|
|
|
cone_dir.x = cone_directions[j][(ofs + 0) % 3];
|
|
|
|
cone_dir.y = cone_directions[j][(ofs + 1) % 3];
|
|
|
|
cone_dir.z = cone_directions[j][(ofs + 2) % 3];
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
cone_dir[ofs] *= dir[ofs];
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Color res = _cone_trace(pos + dir * voxel_size, cone_dir, Math::deg2rad(29.9849));
|
|
|
|
accum.r += res.r; //*coneWeights[j];
|
|
|
|
accum.g += res.g; //*coneWeights[j];
|
|
|
|
accum.b += res.b; //*coneWeights[j];
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
#if 0
|
2016-12-20 03:21:07 +00:00
|
|
|
if (freepix==0) {
|
|
|
|
bake_cells_write[p_idx].light[0]=0;
|
|
|
|
bake_cells_write[p_idx].light[1]=0;
|
|
|
|
bake_cells_write[p_idx].light[2]=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freepix==1) {
|
|
|
|
bake_cells_write[p_idx].light[0]=1;
|
|
|
|
bake_cells_write[p_idx].light[1]=0;
|
|
|
|
bake_cells_write[p_idx].light[2]=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freepix==2) {
|
|
|
|
bake_cells_write[p_idx].light[0]=0;
|
|
|
|
bake_cells_write[p_idx].light[1]=1;
|
|
|
|
bake_cells_write[p_idx].light[2]=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freepix==3) {
|
|
|
|
bake_cells_write[p_idx].light[0]=1;
|
|
|
|
bake_cells_write[p_idx].light[1]=1;
|
|
|
|
bake_cells_write[p_idx].light[2]=0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freepix==4) {
|
|
|
|
bake_cells_write[p_idx].light[0]=0;
|
|
|
|
bake_cells_write[p_idx].light[1]=0;
|
|
|
|
bake_cells_write[p_idx].light[2]=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freepix==5) {
|
|
|
|
bake_cells_write[p_idx].light[0]=1;
|
|
|
|
bake_cells_write[p_idx].light[1]=0;
|
|
|
|
bake_cells_write[p_idx].light[2]=1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freepix==6) {
|
|
|
|
bake_cells_write[p_idx].light[0]=0;
|
|
|
|
bake_cells_write[p_idx].light[0]=1;
|
|
|
|
bake_cells_write[p_idx].light[0]=1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
//bake_cells_write[p_idx].radiance[0]=accum.r;
|
|
|
|
//bake_cells_write[p_idx].radiance[1]=accum.g;
|
|
|
|
//bake_cells_write[p_idx].radiance[2]=accum.b;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int half = cells_per_axis >> (p_level + 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
//go down
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
uint32_t child = bake_cells_write[p_idx].childs[i];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (child == CHILD_EMPTY)
|
2016-12-20 03:21:07 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int nx = p_x;
|
|
|
|
int ny = p_y;
|
|
|
|
int nz = p_z;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i & 1)
|
|
|
|
nx += half;
|
|
|
|
if (i & 2)
|
|
|
|
ny += half;
|
|
|
|
if (i & 4)
|
|
|
|
nz += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_bake_radiance(child, p_level + 1, nx, ny, nz);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLight::bake_radiance() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ERR_FAIL_COND(bake_cells.size() == 0);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
bake_cells_write = bake_cells.write();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_bake_radiance(0, 0, 0, 0, 0);
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
bake_cells_write = PoolVector<BakeCell>::Write();
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
int BakedLight::_find_cell(int x, int y, int z) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
uint32_t cell = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int ofs_x = 0;
|
|
|
|
int ofs_y = 0;
|
|
|
|
int ofs_z = 0;
|
2016-12-20 03:21:07 +00:00
|
|
|
int size = cells_per_axis;
|
2017-03-05 15:44:50 +00:00
|
|
|
int half = size / 2;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (x < 0 || x >= size)
|
2016-12-20 03:21:07 +00:00
|
|
|
return -1;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (y < 0 || y >= size)
|
2016-12-20 03:21:07 +00:00
|
|
|
return -1;
|
2017-03-05 15:44:50 +00:00
|
|
|
if (z < 0 || z >= size)
|
2016-12-20 03:21:07 +00:00
|
|
|
return -1;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < cell_subdiv - 1; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
BakeCell *bc = &bake_cells_write[cell];
|
|
|
|
|
|
|
|
int child = 0;
|
|
|
|
if (x >= ofs_x + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 1;
|
|
|
|
ofs_x += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
if (y >= ofs_y + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 2;
|
|
|
|
ofs_y += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
if (z >= ofs_z + half) {
|
2017-03-05 15:44:50 +00:00
|
|
|
child |= 4;
|
|
|
|
ofs_z += half;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
cell = bc->childs[child];
|
2017-03-05 15:44:50 +00:00
|
|
|
if (cell == CHILD_EMPTY)
|
2016-12-20 03:21:07 +00:00
|
|
|
return -1;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
half >>= 1;
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return cell;
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int BakedLight::_plot_ray(const Vector3 &p_from, const Vector3 &p_to) {
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
Vector3 from = (p_from - bounds.pos) / bounds.size;
|
|
|
|
Vector3 to = (p_to - bounds.pos) / bounds.size;
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int x1 = Math::floor(from.x * cells_per_axis);
|
|
|
|
int y1 = Math::floor(from.y * cells_per_axis);
|
|
|
|
int z1 = Math::floor(from.z * cells_per_axis);
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int x2 = Math::floor(to.x * cells_per_axis);
|
|
|
|
int y2 = Math::floor(to.y * cells_per_axis);
|
|
|
|
int z2 = Math::floor(to.z * cells_per_axis);
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
int i, dx, dy, dz, l, m, n, x_inc, y_inc, z_inc, err_1, err_2, dx2, dy2, dz2;
|
|
|
|
int point[3];
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
point[0] = x1;
|
|
|
|
point[1] = y1;
|
|
|
|
point[2] = z1;
|
|
|
|
dx = x2 - x1;
|
|
|
|
dy = y2 - y1;
|
|
|
|
dz = z2 - z1;
|
|
|
|
x_inc = (dx < 0) ? -1 : 1;
|
|
|
|
l = ABS(dx);
|
|
|
|
y_inc = (dy < 0) ? -1 : 1;
|
|
|
|
m = ABS(dy);
|
|
|
|
z_inc = (dz < 0) ? -1 : 1;
|
|
|
|
n = ABS(dz);
|
|
|
|
dx2 = l << 1;
|
|
|
|
dy2 = m << 1;
|
|
|
|
dz2 = n << 1;
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
if ((l >= m) && (l >= n)) {
|
|
|
|
err_1 = dy2 - l;
|
|
|
|
err_2 = dz2 - l;
|
|
|
|
for (i = 0; i < l; i++) {
|
2017-03-05 15:44:50 +00:00
|
|
|
int cell = _find_cell(point[0], point[1], point[2]);
|
|
|
|
if (cell >= 0)
|
2016-12-20 03:21:07 +00:00
|
|
|
return cell;
|
|
|
|
|
|
|
|
if (err_1 > 0) {
|
|
|
|
point[1] += y_inc;
|
|
|
|
err_1 -= dx2;
|
|
|
|
}
|
|
|
|
if (err_2 > 0) {
|
|
|
|
point[2] += z_inc;
|
|
|
|
err_2 -= dx2;
|
|
|
|
}
|
|
|
|
err_1 += dy2;
|
|
|
|
err_2 += dz2;
|
|
|
|
point[0] += x_inc;
|
|
|
|
}
|
|
|
|
} else if ((m >= l) && (m >= n)) {
|
|
|
|
err_1 = dx2 - m;
|
|
|
|
err_2 = dz2 - m;
|
|
|
|
for (i = 0; i < m; i++) {
|
2017-03-05 15:44:50 +00:00
|
|
|
int cell = _find_cell(point[0], point[1], point[2]);
|
|
|
|
if (cell >= 0)
|
2016-12-20 03:21:07 +00:00
|
|
|
return cell;
|
|
|
|
if (err_1 > 0) {
|
|
|
|
point[0] += x_inc;
|
|
|
|
err_1 -= dy2;
|
|
|
|
}
|
|
|
|
if (err_2 > 0) {
|
|
|
|
point[2] += z_inc;
|
|
|
|
err_2 -= dy2;
|
|
|
|
}
|
|
|
|
err_1 += dx2;
|
|
|
|
err_2 += dz2;
|
|
|
|
point[1] += y_inc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err_1 = dy2 - n;
|
|
|
|
err_2 = dx2 - n;
|
|
|
|
for (i = 0; i < n; i++) {
|
2017-03-05 15:44:50 +00:00
|
|
|
int cell = _find_cell(point[0], point[1], point[2]);
|
|
|
|
if (cell >= 0)
|
2016-12-20 03:21:07 +00:00
|
|
|
return cell;
|
|
|
|
|
|
|
|
if (err_1 > 0) {
|
|
|
|
point[1] += y_inc;
|
|
|
|
err_1 -= dz2;
|
|
|
|
}
|
|
|
|
if (err_2 > 0) {
|
|
|
|
point[0] += x_inc;
|
|
|
|
err_2 -= dz2;
|
|
|
|
}
|
|
|
|
err_1 += dy2;
|
|
|
|
err_2 += dx2;
|
|
|
|
point[2] += z_inc;
|
|
|
|
}
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
return _find_cell(point[0], point[1], point[2]);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
2016-07-26 02:45:20 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
void BakedLight::set_cell_subdiv(int p_subdiv) {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
cell_subdiv = p_subdiv;
|
2016-07-26 02:45:20 +00:00
|
|
|
|
2017-01-14 11:26:56 +00:00
|
|
|
//VS::get_singleton()->baked_light_set_subdivision(baked_light,p_subdiv);
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
int BakedLight::get_cell_subdiv() const {
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
return cell_subdiv;
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-11 03:52:51 +00:00
|
|
|
Rect3 BakedLight::get_aabb() const {
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
return Rect3(Vector3(0, 0, 0), Vector3(1, 1, 1));
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Face3> BakedLight::get_faces(uint32_t p_usage_flags) const {
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
return PoolVector<Face3>();
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
String BakedLight::get_configuration_warning() const {
|
2016-07-26 02:45:20 +00:00
|
|
|
return String();
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
void BakedLight::_debug_mesh(int p_idx, int p_level, const Rect3 &p_aabb, DebugMode p_mode, Ref<MultiMesh> &p_multimesh, int &idx) {
|
2016-07-26 02:45:20 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (p_level == cell_subdiv - 1) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Vector3 center = p_aabb.pos + p_aabb.size * 0.5;
|
2016-12-20 03:21:07 +00:00
|
|
|
Transform xform;
|
2017-03-05 15:44:50 +00:00
|
|
|
xform.origin = center;
|
|
|
|
xform.basis.scale(p_aabb.size * 0.5);
|
|
|
|
p_multimesh->set_instance_transform(idx, xform);
|
2016-12-20 03:21:07 +00:00
|
|
|
Color col;
|
2017-03-05 15:44:50 +00:00
|
|
|
switch (p_mode) {
|
2016-12-20 03:21:07 +00:00
|
|
|
case DEBUG_ALBEDO: {
|
2017-03-05 15:44:50 +00:00
|
|
|
col = Color(bake_cells_write[p_idx].albedo[0], bake_cells_write[p_idx].albedo[1], bake_cells_write[p_idx].albedo[2]);
|
2016-12-20 03:21:07 +00:00
|
|
|
} break;
|
|
|
|
case DEBUG_LIGHT: {
|
2017-03-05 15:44:50 +00:00
|
|
|
col = Color(bake_cells_write[p_idx].light[0], bake_cells_write[p_idx].light[1], bake_cells_write[p_idx].light[2]);
|
|
|
|
Color colr = Color(bake_cells_write[p_idx].radiance[0], bake_cells_write[p_idx].radiance[1], bake_cells_write[p_idx].radiance[2]);
|
|
|
|
col.r += colr.r;
|
|
|
|
col.g += colr.g;
|
|
|
|
col.b += colr.b;
|
2016-12-20 03:21:07 +00:00
|
|
|
} break;
|
|
|
|
}
|
2017-03-05 15:44:50 +00:00
|
|
|
p_multimesh->set_instance_color(idx, col);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
idx++;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 8; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (bake_cells_write[p_idx].childs[i] == CHILD_EMPTY)
|
2016-12-20 03:21:07 +00:00
|
|
|
continue;
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
Rect3 aabb = p_aabb;
|
|
|
|
aabb.size *= 0.5;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i & 1)
|
|
|
|
aabb.pos.x += aabb.size.x;
|
|
|
|
if (i & 2)
|
|
|
|
aabb.pos.y += aabb.size.y;
|
|
|
|
if (i & 4)
|
|
|
|
aabb.pos.z += aabb.size.z;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
_debug_mesh(bake_cells_write[p_idx].childs[i], p_level + 1, aabb, p_mode, p_multimesh, idx);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLight::create_debug_mesh(DebugMode p_mode) {
|
|
|
|
|
|
|
|
Ref<MultiMesh> mm;
|
|
|
|
mm.instance();
|
|
|
|
|
|
|
|
mm->set_transform_format(MultiMesh::TRANSFORM_3D);
|
|
|
|
mm->set_color_format(MultiMesh::COLOR_8BIT);
|
2017-03-05 15:44:50 +00:00
|
|
|
mm->set_instance_count(bake_cells_level_used[cell_subdiv - 1]);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Ref<Mesh> mesh;
|
|
|
|
mesh.instance();
|
|
|
|
|
|
|
|
{
|
|
|
|
Array arr;
|
|
|
|
arr.resize(Mesh::ARRAY_MAX);
|
|
|
|
|
2017-01-07 21:25:37 +00:00
|
|
|
PoolVector<Vector3> vertices;
|
|
|
|
PoolVector<Color> colors;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
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++;
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int i = 0; i < 6; i++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
Vector3 face_points[4];
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int j = 0; j < 4; j++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
|
|
|
float v[3];
|
2017-03-05 15:44:50 +00:00
|
|
|
v[0] = 1.0;
|
|
|
|
v[1] = 1 - 2 * ((j >> 1) & 1);
|
|
|
|
v[2] = v[1] * (1 - 2 * (j & 1));
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
for (int k = 0; k < 3; k++) {
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
if (i < 3)
|
|
|
|
face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
else
|
2017-03-05 15:44:50 +00:00
|
|
|
face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
//tri 1
|
2016-12-20 03:21:07 +00:00
|
|
|
ADD_VTX(0);
|
|
|
|
ADD_VTX(1);
|
|
|
|
ADD_VTX(2);
|
2017-03-05 15:44:50 +00:00
|
|
|
//tri 2
|
2016-12-20 03:21:07 +00:00
|
|
|
ADD_VTX(2);
|
|
|
|
ADD_VTX(3);
|
|
|
|
ADD_VTX(0);
|
|
|
|
}
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
arr[Mesh::ARRAY_VERTEX] = vertices;
|
|
|
|
arr[Mesh::ARRAY_COLOR] = colors;
|
|
|
|
mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arr);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Ref<FixedSpatialMaterial> fsm;
|
|
|
|
fsm.instance();
|
2017-03-05 15:44:50 +00:00
|
|
|
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));
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
mesh->surface_set_material(0, fsm);
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mm->set_mesh(mesh);
|
|
|
|
|
|
|
|
bake_cells_write = bake_cells.write();
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
int idx = 0;
|
|
|
|
_debug_mesh(0, 0, bounds, p_mode, mm, idx);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
print_line("written: " + itos(idx) + " total: " + itos(bake_cells_level_used[cell_subdiv - 1]));
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
MultiMeshInstance *mmi = memnew(MultiMeshInstance);
|
2016-12-20 03:21:07 +00:00
|
|
|
mmi->set_multimesh(mm);
|
|
|
|
add_child(mmi);
|
2017-01-10 10:08:40 +00:00
|
|
|
#ifdef TOOLS_ENABLED
|
2017-03-05 15:44:50 +00:00
|
|
|
if (get_tree()->get_edited_scene_root() == this) {
|
2016-12-20 03:21:07 +00:00
|
|
|
mmi->set_owner(this);
|
|
|
|
} else {
|
|
|
|
mmi->set_owner(get_owner());
|
|
|
|
}
|
2017-01-10 10:08:40 +00:00
|
|
|
#else
|
|
|
|
mmi->set_owner(get_owner());
|
|
|
|
#endif
|
2016-12-20 03:21:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLight::_debug_mesh_albedo() {
|
|
|
|
create_debug_mesh(DEBUG_ALBEDO);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLight::_debug_mesh_light() {
|
|
|
|
create_debug_mesh(DEBUG_LIGHT);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLight::_bind_methods() {
|
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_cell_subdiv", "steps"), &BakedLight::set_cell_subdiv);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_cell_subdiv"), &BakedLight::get_cell_subdiv);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("bake"), &BakedLight::bake);
|
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("bake"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("bake_lights"), &BakedLight::bake_lights);
|
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("bake_lights"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("bake_radiance"), &BakedLight::bake_radiance);
|
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("bake_radiance"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_albedo"), &BakedLight::_debug_mesh_albedo);
|
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("debug_mesh_albedo"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("debug_mesh_light"), &BakedLight::_debug_mesh_light);
|
|
|
|
ClassDB::set_method_flags(get_class_static(), _scs_create("debug_mesh_light"), METHOD_FLAGS_DEFAULT | METHOD_FLAG_EDITOR);
|
2016-12-20 03:21:07 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_subdiv"), "set_cell_subdiv", "get_cell_subdiv");
|
|
|
|
ADD_SIGNAL(MethodInfo("baked_light_changed"));
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
BakedLight::BakedLight() {
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-01-14 11:26:56 +00:00
|
|
|
//baked_light=VisualServer::get_singleton()->baked_light_create();
|
2017-03-05 15:44:50 +00:00
|
|
|
VS::get_singleton()->instance_set_base(get_instance(), baked_light);
|
2014-06-11 13:41:03 +00:00
|
|
|
|
2017-03-05 15:44:50 +00:00
|
|
|
cell_subdiv = 8;
|
|
|
|
bake_texture_size = 128;
|
|
|
|
color_scan_cell_width = 8;
|
|
|
|
light_pass = 0;
|
2014-06-11 13:41:03 +00:00
|
|
|
}
|
2014-10-28 01:54:32 +00:00
|
|
|
|
2016-12-20 03:21:07 +00:00
|
|
|
BakedLight::~BakedLight() {
|
|
|
|
|
|
|
|
VS::get_singleton()->free(baked_light);
|
|
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////
|
|
|
|
|
|
|
|
#if 0
|
2014-10-28 01:54:32 +00:00
|
|
|
void BakedLightSampler::set_param(Param p_param,float p_value) {
|
|
|
|
ERR_FAIL_INDEX(p_param,PARAM_MAX);
|
|
|
|
params[p_param]=p_value;
|
|
|
|
VS::get_singleton()->baked_light_sampler_set_param(base,VS::BakedLightSamplerParam(p_param),p_value);
|
|
|
|
}
|
|
|
|
|
|
|
|
float BakedLightSampler::get_param(Param p_param) const{
|
|
|
|
|
|
|
|
ERR_FAIL_INDEX_V(p_param,PARAM_MAX,0);
|
|
|
|
return params[p_param];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLightSampler::set_resolution(int p_resolution){
|
|
|
|
|
2015-10-17 13:29:54 +00:00
|
|
|
ERR_FAIL_COND(p_resolution<4 || p_resolution>32);
|
2014-10-28 01:54:32 +00:00
|
|
|
resolution=p_resolution;
|
|
|
|
VS::get_singleton()->baked_light_sampler_set_resolution(base,resolution);
|
|
|
|
}
|
|
|
|
int BakedLightSampler::get_resolution() const {
|
|
|
|
|
|
|
|
return resolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
AABB BakedLightSampler::get_aabb() const {
|
|
|
|
|
|
|
|
float r = get_param(PARAM_RADIUS);
|
|
|
|
return AABB( Vector3(-r,-r,-r),Vector3(r*2,r*2,r*2));
|
|
|
|
}
|
|
|
|
DVector<Face3> BakedLightSampler::get_faces(uint32_t p_usage_flags) const {
|
|
|
|
return DVector<Face3>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void BakedLightSampler::_bind_methods() {
|
|
|
|
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_param","param","value"),&BakedLightSampler::set_param);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_param","param"),&BakedLightSampler::get_param);
|
2014-10-28 01:54:32 +00:00
|
|
|
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("set_resolution","resolution"),&BakedLightSampler::set_resolution);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_resolution"),&BakedLightSampler::get_resolution);
|
2014-10-28 01:54:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
BIND_CONSTANT( PARAM_RADIUS );
|
|
|
|
BIND_CONSTANT( PARAM_STRENGTH );
|
|
|
|
BIND_CONSTANT( PARAM_ATTENUATION );
|
|
|
|
BIND_CONSTANT( PARAM_DETAIL_RATIO );
|
|
|
|
BIND_CONSTANT( PARAM_MAX );
|
|
|
|
|
2017-02-12 00:11:37 +00:00
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::REAL,"params/radius",PROPERTY_HINT_RANGE,"0.01,1024,0.01"),"set_param","get_param",PARAM_RADIUS);
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::REAL,"params/strength",PROPERTY_HINT_RANGE,"0.01,16,0.01"),"set_param","get_param",PARAM_STRENGTH);
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::REAL,"params/attenuation",PROPERTY_HINT_EXP_EASING),"set_param","get_param",PARAM_ATTENUATION);
|
|
|
|
ADD_PROPERTYI( PropertyInfo(Variant::REAL,"params/detail_ratio",PROPERTY_HINT_RANGE,"0.01,1.0,0.01"),"set_param","get_param",PARAM_DETAIL_RATIO);
|
|
|
|
//ADD_PROPERTYI( PropertyInfo(Variant::REAL,"params/detail_ratio",PROPERTY_HINT_RANGE,"0,20,1"),"set_param","get_param",PARAM_DETAIL_RATIO);
|
|
|
|
ADD_PROPERTY( PropertyInfo(Variant::REAL,"params/resolution",PROPERTY_HINT_RANGE,"4,32,1"),"set_resolution","get_resolution");
|
2014-10-28 01:54:32 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BakedLightSampler::BakedLightSampler() {
|
|
|
|
|
|
|
|
base = VS::get_singleton()->baked_light_sampler_create();
|
|
|
|
set_base(base);
|
|
|
|
|
|
|
|
params[PARAM_RADIUS]=1.0;
|
|
|
|
params[PARAM_STRENGTH]=1.0;
|
|
|
|
params[PARAM_ATTENUATION]=1.0;
|
|
|
|
params[PARAM_DETAIL_RATIO]=0.1;
|
|
|
|
resolution=16;
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
BakedLightSampler::~BakedLightSampler(){
|
|
|
|
|
|
|
|
VS::get_singleton()->free(base);
|
|
|
|
}
|
2016-10-03 19:33:42 +00:00
|
|
|
#endif
|