Implemented image generation for PaintCurve2D.

This commit is contained in:
Relintai 2023-08-26 21:55:50 +02:00
parent cfa067fa56
commit e7eb415910
2 changed files with 248 additions and 220 deletions

View File

@ -308,27 +308,316 @@ Ref<Image> PaintCurve2D::_get_rendered_image() {
return Ref<Image>();
}
/*
if (polygon.size() < 3) {
if (!curve.is_valid()) {
return Ref<Image>();
}
if (curve->get_point_count() < 2) {
return Ref<Image>();
}
_rendered_image.instance();
_rendered_image->create(_size.x, _size.y, false, Image::FORMAT_RGBA8);
Vector<Vector2> points;
Vector<Vector2> uvs;
Vector<Color> colors;
Vector<int> indices;
_prepare_render_data(points, uvs, colors, indices);
if (_outline_enabled) {
_prepare_render_data_outline(points, uvs, colors, indices);
if (indices.size() == 0) {
return Ref<Image>();
_image_render_triangles(points, uvs, colors, indices, _outline_texture);
points.clear();
uvs.clear();
colors.clear();
indices.clear();
}
if (_fill_enabled) {
_prepare_render_data_fill(points, uvs, colors, indices);
_image_render_triangles(points, uvs, colors, indices, _fill_texture);
}
return _rendered_image;
}
PoolVector2Array PaintCurve2D::generate_uvs(const Vector<Vector2> &p_points) {
PoolVector2Array uvs;
int len = p_points.size();
if (len == 0) {
return uvs;
}
const Vector2 *pr = p_points.ptr();
Rect2 bounds = Rect2(pr[0], Vector2());
for (int i = 1; i < len; i++) {
Vector2 e = pr[i];
bounds.expand_to(e);
}
uvs.resize(len);
PoolVector2Array::Write uvr = uvs.write();
Vector2 *uvp = uvr.ptr();
for (int i = 0; i < len; i++) {
Vector2 e = pr[i];
e -= bounds.position;
e /= bounds.size;
uvp[i] = e;
}
return uvs;
}
PoolVector2Array PaintCurve2D::generate_uvs(const Vector<Vector2> &p_points, const Rect2 &p_uv_rect) {
PoolVector2Array uvs;
int len = p_points.size();
if (len == 0) {
return uvs;
}
const Vector2 *pr = p_points.ptr();
Rect2 bounds = Rect2(pr[0], Vector2());
for (int i = 1; i < len; i++) {
Vector2 e = pr[i];
bounds.expand_to(e);
}
uvs.resize(len);
PoolVector2Array::Write uvr = uvs.write();
Vector2 *uvp = uvr.ptr();
for (int i = 0; i < len; i++) {
Vector2 e = pr[i];
e -= bounds.position;
e /= bounds.size;
e *= p_uv_rect.size;
e += p_uv_rect.position;
uvp[i] = e;
}
return uvs;
}
void PaintCurve2D::generate_polyline_mesh(const Vector<Point2> &p_points, float p_width, Vector<Vector2> &r_triangles, Vector<int> &r_indices) {
Vector2 prev_t;
if (p_points.size() < 2) {
return;
}
r_triangles.resize(p_points.size() * 2);
r_indices.resize(p_points.size() * 6);
for (int i = 0; i < p_points.size(); i++) {
Vector2 t;
if (i == p_points.size() - 1) {
t = prev_t;
} else {
t = (p_points[i + 1] - p_points[i]).normalized().tangent();
if (i == 0) {
prev_t = t;
}
}
Vector2 tangent = ((t + prev_t).normalized()) * p_width * 0.5;
r_triangles.write[i * 2 + 0] = p_points[i] + tangent;
r_triangles.write[i * 2 + 1] = p_points[i] - tangent;
if (i != 0) {
int indx = (i - 1) * 2;
int iindx = (i - 1) * 6;
r_indices.write[iindx] = indx;
r_indices.write[iindx + 1] = indx + 1;
r_indices.write[iindx + 2] = indx + 3;
r_indices.write[iindx + 3] = indx;
r_indices.write[iindx + 4] = indx + 2;
r_indices.write[iindx + 5] = indx + 3;
}
prev_t = t;
}
}
void PaintCurve2D::_prepare_render_data_fill(Vector<Vector2> &r_points, Vector<Vector2> &r_uvs, Vector<Color> &r_colors, Vector<int> &r_indices) {
if (!curve.is_valid()) {
return;
}
int curve_len = curve->get_point_count();
if (curve_len < 2) {
return;
}
PoolVector2Array polygon = curve->tessellate();
int len = polygon.size();
r_points.resize(len);
{
PoolVector<Vector2>::Read polyr = polygon.read();
for (int i = 0; i < len; i++) {
r_points.write[i] = polyr[i];
}
}
if (_fill_invert) {
Rect2 bounds;
int highest_idx = -1;
float highest_y = -1e20;
float sum = 0;
for (int i = 0; i < len; i++) {
if (i == 0) {
bounds.position = r_points[i];
} else {
bounds.expand_to(r_points[i]);
}
if (r_points[i].y > highest_y) {
highest_idx = i;
highest_y = r_points[i].y;
}
int ni = (i + 1) % len;
sum += (r_points[ni].x - r_points[i].x) * (r_points[ni].y + r_points[i].y);
}
bounds = bounds.grow(_fill_invert_border);
Vector2 ep[7] = {
Vector2(r_points[highest_idx].x, r_points[highest_idx].y + _fill_invert_border),
Vector2(bounds.position + bounds.size),
Vector2(bounds.position + Vector2(bounds.size.x, 0)),
Vector2(bounds.position),
Vector2(bounds.position + Vector2(0, bounds.size.y)),
Vector2(r_points[highest_idx].x - CMP_EPSILON, r_points[highest_idx].y + _fill_invert_border),
Vector2(r_points[highest_idx].x - CMP_EPSILON, r_points[highest_idx].y),
};
if (sum > 0) {
SWAP(ep[1], ep[4]);
SWAP(ep[2], ep[3]);
SWAP(ep[5], ep[0]);
SWAP(ep[6], r_points.write[highest_idx]);
}
r_points.resize(r_points.size() + 7);
for (int i = r_points.size() - 1; i >= highest_idx + 7; i--) {
r_points.write[i] = r_points[i - 7];
}
for (int i = 0; i < 7; i++) {
r_points.write[highest_idx + i + 1] = ep[i];
}
len = r_points.size();
}
if (_fill_texture.is_valid()) {
Transform2D texmat(_fill_tex_rot, _fill_tex_ofs);
texmat.scale(_fill_tex_scale);
//Size2 tex_size = _fill_texture->get_size();
PoolVector2Array uv = generate_uvs(r_points);
r_uvs.resize(len);
PoolVector<Vector2>::Read uvr = uv.read();
for (int i = 0; i < len; i++) {
r_uvs.write[i] = texmat.xform(uvr[i]);
}
}
r_colors.push_back(_fill_color);
r_indices = Geometry::triangulate_polygon(r_points);
}
void PaintCurve2D::_prepare_render_data_outline(Vector<Vector2> &r_points, Vector<Vector2> &r_uvs, Vector<Color> &r_colors, Vector<int> &r_indices) {
if (!curve.is_valid()) {
return;
}
if (_outline_width <= 0) {
return;
}
int curve_len = curve->get_point_count();
if (curve_len < 2) {
return;
}
PoolVector2Array polygon = curve->tessellate();
int len = polygon.size();
Vector<Vector2> curve_points;
curve_points.resize(len);
{
PoolVector<Vector2>::Read polyr = polygon.read();
for (int i = 0; i < len; i++) {
curve_points.write[i] = polyr[i];
}
}
generate_polyline_mesh(curve_points, _outline_width, r_points, r_indices);
len = r_points.size();
if (_outline_texture.is_valid()) {
Transform2D texmat(_outline_tex_rot, _outline_tex_ofs);
texmat.scale(_outline_tex_scale);
//Size2 tex_size = _outline_texture->get_size();
PoolVector2Array uv = generate_uvs(r_points);
r_uvs.resize(len);
PoolVector<Vector2>::Read uvr = uv.read();
for (int i = 0; i < len; i++) {
r_uvs.write[i] = texmat.xform(uvr[i]);
}
}
r_colors.push_back(_outline_color);
}
void PaintCurve2D::_image_render_triangles(const Vector<Vector2> &p_points, const Vector<Vector2> &p_uvs, const Vector<Color> &p_colors, const Vector<int> &p_indices, const Ref<Texture> &p_texture) {
if (p_indices.size() == 0) {
return;
}
Ref<Image> texture_image;
Vector2 texture_image_size;
if (uvs.size() > 0 && texture.is_valid()) {
texture_image = texture->get_data();
if (p_uvs.size() > 0 && p_texture.is_valid()) {
texture_image = p_texture->get_data();
}
bool use_uvs = texture_image.is_valid();
@ -337,9 +626,6 @@ Ref<Image> PaintCurve2D::_get_rendered_image() {
texture_image_size = texture_image->get_size();
}
_rendered_image.instance();
_rendered_image->create(_size.x, _size.y, false, Image::FORMAT_RGBA8);
_rendered_image->lock();
if (use_uvs) {
@ -350,29 +636,31 @@ Ref<Image> PaintCurve2D::_get_rendered_image() {
Vector2 cuvs[3];
Color ccolors[3];
if (colors.size() == 1) {
if (p_colors.size() == 1) {
for (int j = 0; j < 3; ++j) {
ccolors[j] = colors[0];
ccolors[j] = p_colors[0];
}
}
//Note: Don't worry about the Node's Transform here, that will get applied automatically in the caller
for (int index = 0; index < indices.size(); index += 3) {
for (int index = 0; index < p_indices.size(); index += 3) {
// Rasterize triangle
// Based on https://www.youtube.com/watch?v=PahbNFypubE
for (int i = 0; i < 3; ++i) {
int cind = indices[index + i];
int cind = p_indices[index + i];
cpoints[i] = points[cind].round();
ERR_FAIL_INDEX(cind, p_points.size());
if (colors.size() > 1) {
ccolors[i] = colors[cind];
cpoints[i] = p_points[cind].round();
if (p_colors.size() > 1) {
ccolors[i] = p_colors[cind];
}
if (use_uvs) {
cuvs[i] = uvs[cind];
cuvs[i] = p_uvs[cind];
}
}
@ -535,268 +823,6 @@ Ref<Image> PaintCurve2D::_get_rendered_image() {
}
_rendered_image->unlock();
*/
return _rendered_image;
}
PoolVector2Array PaintCurve2D::generate_uvs(const Vector<Vector2> &p_points) {
PoolVector2Array uvs;
int len = p_points.size();
if (len == 0) {
return uvs;
}
const Vector2 *pr = p_points.ptr();
Rect2 bounds = Rect2(pr[0], Vector2());
for (int i = 1; i < len; i++) {
Vector2 e = pr[i];
bounds.expand_to(e);
}
uvs.resize(len);
PoolVector2Array::Write uvr = uvs.write();
Vector2 *uvp = uvr.ptr();
for (int i = 0; i < len; i++) {
Vector2 e = pr[i];
e -= bounds.position;
e /= bounds.size;
uvp[i] = e;
}
return uvs;
}
PoolVector2Array PaintCurve2D::generate_uvs(const Vector<Vector2> &p_points, const Rect2 &p_uv_rect) {
PoolVector2Array uvs;
int len = p_points.size();
if (len == 0) {
return uvs;
}
const Vector2 *pr = p_points.ptr();
Rect2 bounds = Rect2(pr[0], Vector2());
for (int i = 1; i < len; i++) {
Vector2 e = pr[i];
bounds.expand_to(e);
}
uvs.resize(len);
PoolVector2Array::Write uvr = uvs.write();
Vector2 *uvp = uvr.ptr();
for (int i = 0; i < len; i++) {
Vector2 e = pr[i];
e -= bounds.position;
e /= bounds.size;
e *= p_uv_rect.size;
e += p_uv_rect.position;
uvp[i] = e;
}
return uvs;
}
void PaintCurve2D::generate_polyline_mesh(const Vector<Point2> &p_points, float p_width, Vector<Vector2> &r_triangles, Vector<int> &r_indices) {
Vector2 prev_t;
r_triangles.resize(p_points.size() * 2);
r_indices.resize(p_points.size() * 6);
for (int i = 0; i < p_points.size(); i++) {
Vector2 t;
if (i == p_points.size() - 1) {
t = prev_t;
} else {
t = (p_points[i + 1] - p_points[i]).normalized().tangent();
if (i == 0) {
prev_t = t;
}
}
Vector2 tangent = ((t + prev_t).normalized()) * p_width * 0.5;
r_triangles.write[i * 2 + 0] = p_points[i] + tangent;
r_triangles.write[i * 2 + 1] = p_points[i] - tangent;
if (i != 0) {
int indx = (i - 1) * 2;
int iindx = (i - 1) * 6;
r_indices.write[iindx] = indx;
r_indices.write[iindx + 1] = indx + 1;
r_indices.write[iindx + 2] = indx + 3;
r_indices.write[iindx + 3] = indx + 0;
r_indices.write[iindx + 4] = indx + 2;
r_indices.write[iindx + 5] = indx + 3;
}
prev_t = t;
}
}
void PaintCurve2D::_prepare_render_data_fill(Vector<Vector2> &r_points, Vector<Vector2> &r_uvs, Vector<Color> &r_colors, Vector<int> &r_indices) {
if (!curve.is_valid()) {
return;
}
int curve_len = curve->get_point_count();
if (curve_len < 2) {
return;
}
PoolVector2Array polygon = curve->tessellate();
int len = polygon.size();
r_points.resize(len);
{
PoolVector<Vector2>::Read polyr = polygon.read();
for (int i = 0; i < len; i++) {
r_points.write[i] = polyr[i];
}
}
if (_fill_invert) {
Rect2 bounds;
int highest_idx = -1;
float highest_y = -1e20;
float sum = 0;
for (int i = 0; i < len; i++) {
if (i == 0) {
bounds.position = r_points[i];
} else {
bounds.expand_to(r_points[i]);
}
if (r_points[i].y > highest_y) {
highest_idx = i;
highest_y = r_points[i].y;
}
int ni = (i + 1) % len;
sum += (r_points[ni].x - r_points[i].x) * (r_points[ni].y + r_points[i].y);
}
bounds = bounds.grow(_fill_invert_border);
Vector2 ep[7] = {
Vector2(r_points[highest_idx].x, r_points[highest_idx].y + _fill_invert_border),
Vector2(bounds.position + bounds.size),
Vector2(bounds.position + Vector2(bounds.size.x, 0)),
Vector2(bounds.position),
Vector2(bounds.position + Vector2(0, bounds.size.y)),
Vector2(r_points[highest_idx].x - CMP_EPSILON, r_points[highest_idx].y + _fill_invert_border),
Vector2(r_points[highest_idx].x - CMP_EPSILON, r_points[highest_idx].y),
};
if (sum > 0) {
SWAP(ep[1], ep[4]);
SWAP(ep[2], ep[3]);
SWAP(ep[5], ep[0]);
SWAP(ep[6], r_points.write[highest_idx]);
}
r_points.resize(r_points.size() + 7);
for (int i = r_points.size() - 1; i >= highest_idx + 7; i--) {
r_points.write[i] = r_points[i - 7];
}
for (int i = 0; i < 7; i++) {
r_points.write[highest_idx + i + 1] = ep[i];
}
len = r_points.size();
}
if (_fill_texture.is_valid()) {
Transform2D texmat(_fill_tex_rot, _fill_tex_ofs);
texmat.scale(_fill_tex_scale);
//Size2 tex_size = _fill_texture->get_size();
PoolVector2Array uv = generate_uvs(r_points);
r_uvs.resize(len);
PoolVector<Vector2>::Read uvr = uv.read();
for (int i = 0; i < len; i++) {
r_uvs.write[i] = texmat.xform(uvr[i]);
}
}
r_colors.push_back(_fill_color);
r_indices = Geometry::triangulate_polygon(r_points);
}
void PaintCurve2D::_prepare_render_data_outline(Vector<Vector2> &r_points, Vector<Vector2> &r_uvs, Vector<Color> &r_colors, Vector<int> &r_indices) {
if (!curve.is_valid()) {
return;
}
if (_outline_width <= 0) {
return;
}
int curve_len = curve->get_point_count();
if (curve_len < 2) {
return;
}
PoolVector2Array polygon = curve->tessellate();
int len = polygon.size();
Vector<Vector2> curve_points;
curve_points.resize(len);
{
PoolVector<Vector2>::Read polyr = polygon.read();
for (int i = 0; i < len; i++) {
curve_points.write[i] = polyr[i];
}
}
generate_polyline_mesh(curve_points, _outline_width, r_points, r_indices);
len = r_points.size();
if (_outline_texture.is_valid()) {
Transform2D texmat(_outline_tex_rot, _outline_tex_ofs);
texmat.scale(_outline_tex_scale);
//Size2 tex_size = _outline_texture->get_size();
PoolVector2Array uv = generate_uvs(r_points);
r_uvs.resize(len);
PoolVector<Vector2>::Read uvr = uv.read();
for (int i = 0; i < len; i++) {
r_uvs.write[i] = texmat.xform(uvr[i]);
}
}
r_colors.push_back(_outline_color);
}
void PaintCurve2D::_bind_methods() {

View File

@ -113,6 +113,8 @@ protected:
void _prepare_render_data_fill(Vector<Vector2> &r_points, Vector<Vector2> &r_uvs, Vector<Color> &r_colors, Vector<int> &r_indices);
void _prepare_render_data_outline(Vector<Vector2> &r_points, Vector<Vector2> &r_uvs, Vector<Color> &r_colors, Vector<int> &r_indices);
void _image_render_triangles(const Vector<Vector2> &p_points, const Vector<Vector2> &p_uvs, const Vector<Color> &p_colors, const Vector<int> &p_indices, const Ref<Texture> &p_texture);
void _notification(int p_what);
static void _bind_methods();