Ported the remaining MeshUtils methods.

This commit is contained in:
Relintai 2024-01-13 14:39:12 +01:00
parent 5cfffb109d
commit 9ce93f849c
4 changed files with 450 additions and 380 deletions

View File

@ -34,7 +34,11 @@ void GameScene::input_event(const Ref<InputEvent> &event) {
if (k->get_physical_scancode() == KEY_SPACE) { if (k->get_physical_scancode() == KEY_SPACE) {
if (pressed) { if (pressed) {
immediate = !immediate; ++render_type;
if (render_type >= RENDER_TYPE_MAX) {
render_type = 0;
}
} }
} }
@ -75,10 +79,141 @@ void GameScene::update(float delta) {
} }
void GameScene::render() { void GameScene::render() {
if (!immediate) { static float rotmi = 0;
if (render_type == 0) {
render_obj(); render_obj();
} else { } else if (render_type == 1) {
render_immediate(); render_immediate();
} else if (render_type == 2) {
_mesh_utils_test->clear();
MeshUtils::create_simple_test_cone(_mesh_utils_test);
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 3) {
_mesh_utils_test->clear();
MeshUtils::create_capsule(_mesh_utils_test, 0.5, 0.5);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 4) {
_mesh_utils_test->clear();
MeshUtils::create_cube(_mesh_utils_test, Vector3(0.5, 0.5, 0.5));
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 5) {
_mesh_utils_test->clear();
MeshUtils::create_cylinder(_mesh_utils_test, 0.2, 0.5, 1);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 6) {
_mesh_utils_test->clear();
MeshUtils::create_plane(_mesh_utils_test);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 7) {
_mesh_utils_test->clear();
MeshUtils::create_prism(_mesh_utils_test);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 8) {
_mesh_utils_test->clear();
MeshUtils::create_quad(_mesh_utils_test);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 9) {
_mesh_utils_test->clear();
MeshUtils::create_quad_with_indices(_mesh_utils_test);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 10) {
_mesh_utils_test->clear();
MeshUtils::create_sphere(_mesh_utils_test, 0.5, 0.5);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} else if (render_type == 11) {
_mesh_utils_test->clear();
MeshUtils::create_point(_mesh_utils_test);
_mesh_utils_test->fill_colors_random();
_mesh_utils_test->upload();
camera->bind();
_mesh_utils_test_mi->transform.basis = Basis(Vector3(1, 0, 0), rotmi);
rotmi += 0.01;
_mesh_utils_test_mi->render();
_mesh_utils_test_mi->render();
} }
} }
void GameScene::render_immediate() { void GameScene::render_immediate() {
@ -137,11 +272,11 @@ void GameScene::render_obj() {
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
static float rot = 0; //static float rot = 0;
Transform t = camera->get_camera_transform(); //Transform t = camera->get_camera_transform();
t.basis = Basis(Vector3(0, 1, 0), rot); //t.basis = Basis(Vector3(0, 1, 0), rot);
camera->set_camera_transform(t); //camera->set_camera_transform(t);
rot += 0.01; //rot += 0.01;
Ref<Image> d = texture->get_data(); Ref<Image> d = texture->get_data();
texture->create_from_image(d); texture->create_from_image(d);
@ -166,7 +301,7 @@ void GameScene::render_obj() {
} }
GameScene::GameScene() { GameScene::GameScene() {
immediate = false; render_type = 0;
left = false; left = false;
right = false; right = false;
@ -273,7 +408,7 @@ GameScene::GameScene() {
//mesh->clear(); //mesh->clear();
MeshUtils::create_cone(mesh); MeshUtils::create_simple_test_cone(mesh);
mesh->upload(); mesh->upload();
mi = memnew(MeshInstance3D()); mi = memnew(MeshInstance3D());
@ -328,7 +463,13 @@ GameScene::GameScene() {
_text_2d->update(); _text_2d->update();
_text_2d->transform.set_origin(Vector2(1200, 250)); _text_2d->transform.set_origin(Vector2(1200, 250));
_mesh_utils_test.instance();
Renderer::initialize(); Renderer::initialize();
_mesh_utils_test_mi = memnew(MeshInstance3D());
_mesh_utils_test_mi->material = color_material;
_mesh_utils_test_mi->mesh = _mesh_utils_test;
} }
GameScene::~GameScene() { GameScene::~GameScene() {

View File

@ -23,6 +23,10 @@ class GameScene : public Scene {
SFW_OBJECT(GameScene, Scene); SFW_OBJECT(GameScene, Scene);
public: public:
enum RenderTypes {
RENDER_TYPE_MAX = 12
};
virtual void input_event(const Ref<InputEvent> &event); virtual void input_event(const Ref<InputEvent> &event);
virtual void update(float delta); virtual void update(float delta);
virtual void render(); virtual void render();
@ -32,7 +36,7 @@ public:
GameScene(); GameScene();
~GameScene(); ~GameScene();
bool immediate; int render_type;
bool left; bool left;
bool right; bool right;
@ -62,6 +66,9 @@ public:
Text2D *_text_2d; Text2D *_text_2d;
Ref<Mesh> _mesh_utils_test;
MeshInstance3D *_mesh_utils_test_mi;
//ColoredMaterial *cmaterial; //ColoredMaterial *cmaterial;
}; };

View File

@ -7,12 +7,10 @@
#include "render_core/mesh_utils.h" #include "render_core/mesh_utils.h"
//--STRIP //--STRIP
void MeshUtils::create_cone(Ref<Mesh> mesh) { void MeshUtils::create_simple_test_cone(Ref<Mesh> mesh) {
if (!mesh.is_valid()) { ERR_FAIL_COND(!mesh.is_valid());
return;
}
uint32_t vc = mesh->vertices.size(); uint32_t vc = mesh->get_vertex_count();
//eleje //eleje
mesh->add_color(1, 0, 0); mesh->add_color(1, 0, 0);
@ -80,10 +78,13 @@ void MeshUtils::create_cone(Ref<Mesh> mesh) {
mesh->add_triangle(13 + vc, 12 + vc, 15 + vc); mesh->add_triangle(13 + vc, 12 + vc, 15 + vc);
} }
/* void MeshUtils::create_capsule(Ref<Mesh> mesh, const float radius, const float mid_height, const int p_radial_segments, const int p_rings) {
void MeshUtils::create_capsule(Array &p_arr, const float radius, const float mid_height, const int radial_segments, const int rings) { ERR_FAIL_COND(!mesh.is_valid());
//radial_segments = p_segments > 4 ? p_segments : 4;
//rings = p_rings > 1 ? p_rings : 1; int msi = mesh->get_vertex_count();
int radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
int rings = p_rings > 1 ? p_rings : 1;
int i, j, prevrow, thisrow, point; int i, j, prevrow, thisrow, point;
float x, y, z, u, v, w; float x, y, z, u, v, w;
@ -92,19 +93,8 @@ void MeshUtils::create_capsule(Array &p_arr, const float radius, const float mid
// note, this has been aligned with our collision shape but I've left the descriptions as top/middle/bottom // note, this has been aligned with our collision shape but I've left the descriptions as top/middle/bottom
PoolVector<Vector3> points;
PoolVector<Vector3> normals;
PoolVector<float> tangents;
PoolVector<Vector2> uvs;
PoolVector<int> indices;
point = 0; point = 0;
#define ADD_TANGENT(m_x, m_y, m_z, m_d) \
tangents.push_back(m_x); \
tangents.push_back(m_y); \
tangents.push_back(m_z); \
tangents.push_back(m_d);
// top hemisphere // top hemisphere
thisrow = 0; thisrow = 0;
prevrow = 0; prevrow = 0;
@ -123,20 +113,20 @@ void MeshUtils::create_capsule(Array &p_arr, const float radius, const float mid
y = -cos(u * (Math_PI * 2.0)); y = -cos(u * (Math_PI * 2.0));
Vector3 p = Vector3(x * radius * w, y * radius * w, z); Vector3 p = Vector3(x * radius * w, y * radius * w, z);
points.push_back(p + Vector3(0.0, 0.0, 0.5 * mid_height)); mesh->add_vertex3(p + Vector3(0.0, 0.0, 0.5 * mid_height));
normals.push_back(p.normalized()); mesh->add_normal(p.normalized());
ADD_TANGENT(-y, x, 0.0, 1.0) mesh->add_uv(Vector2(u, v * onethird));
uvs.push_back(Vector2(u, v * onethird));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
indices.push_back(prevrow + i - 1); mesh->add_index(msi + prevrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i); mesh->add_index(msi + thisrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
}; };
}; };
@ -162,20 +152,19 @@ void MeshUtils::create_capsule(Array &p_arr, const float radius, const float mid
y = -cos(u * (Math_PI * 2.0)); y = -cos(u * (Math_PI * 2.0));
Vector3 p = Vector3(x * radius, y * radius, z); Vector3 p = Vector3(x * radius, y * radius, z);
points.push_back(p); mesh->add_vertex3(p);
normals.push_back(Vector3(x, y, 0.0)); mesh->add_normal(Vector3(x, y, 0.0));
ADD_TANGENT(-y, x, 0.0, 1.0) mesh->add_uv(Vector2(u, onethird + (v * onethird)));
uvs.push_back(Vector2(u, onethird + (v * onethird)));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
indices.push_back(prevrow + i - 1); mesh->add_index(msi + prevrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i); mesh->add_index(msi + thisrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
}; };
}; };
@ -202,37 +191,34 @@ void MeshUtils::create_capsule(Array &p_arr, const float radius, const float mid
y = -cos(u2 * (Math_PI * 2.0)); y = -cos(u2 * (Math_PI * 2.0));
Vector3 p = Vector3(x * radius * w, y * radius * w, z); Vector3 p = Vector3(x * radius * w, y * radius * w, z);
points.push_back(p + Vector3(0.0, 0.0, -0.5 * mid_height)); mesh->add_vertex3(p + Vector3(0.0, 0.0, -0.5 * mid_height));
normals.push_back(p.normalized()); mesh->add_normal(p.normalized());
ADD_TANGENT(-y, x, 0.0, 1.0) mesh->add_uv(Vector2(u2, twothirds + ((v - 1.0) * onethird)));
uvs.push_back(Vector2(u2, twothirds + ((v - 1.0) * onethird)));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
indices.push_back(prevrow + i - 1); mesh->add_index(msi + prevrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i); mesh->add_index(msi + thisrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
}; };
}; };
prevrow = thisrow; prevrow = thisrow;
thisrow = point; thisrow = point;
}; };
p_arr[RS::ARRAY_VERTEX] = points;
p_arr[RS::ARRAY_NORMAL] = normals;
p_arr[RS::ARRAY_TANGENT] = tangents;
p_arr[RS::ARRAY_TEX_UV] = uvs;
p_arr[RS::ARRAY_INDEX] = indices;
} }
void MeshUtils::create_cube(Array &p_arr, const Vector3 size, const int subdivide_w, const int subdivide_h, const int subdivide_d) { void MeshUtils::create_cube(Ref<Mesh> mesh, const Vector3 size, const int p_subdivide_w, const int subdivide_h, const int p_subdivide_d) {
//subdivide_w = p_divisions > 0 ? p_divisions : 0; ERR_FAIL_COND(!mesh.is_valid());
//subdivide_d = p_divisions > 0 ? p_divisions : 0;
int msi = mesh->get_vertex_count();
int subdivide_w = p_subdivide_w > 0 ? p_subdivide_w : 0;
int subdivide_d = p_subdivide_d > 0 ? p_subdivide_d : 0;
int i, j, prevrow, thisrow, point; int i, j, prevrow, thisrow, point;
float x, y, z; float x, y, z;
@ -243,19 +229,8 @@ void MeshUtils::create_cube(Array &p_arr, const Vector3 size, const int subdivid
// set our bounding box // set our bounding box
PoolVector<Vector3> points;
PoolVector<Vector3> normals;
PoolVector<float> tangents;
PoolVector<Vector2> uvs;
PoolVector<int> indices;
point = 0; point = 0;
#define ADD_TANGENT(m_x, m_y, m_z, m_d) \
tangents.push_back(m_x); \
tangents.push_back(m_y); \
tangents.push_back(m_z); \
tangents.push_back(m_d);
// front + back // front + back
y = start_pos.y; y = start_pos.y;
thisrow = point; thisrow = point;
@ -269,37 +244,35 @@ void MeshUtils::create_cube(Array &p_arr, const Vector3 size, const int subdivid
v /= (2.0 * (subdivide_h + 1.0)); v /= (2.0 * (subdivide_h + 1.0));
// front // front
points.push_back(Vector3(x, -y, -start_pos.z)); // double negative on the Z! mesh->add_vertex3(Vector3(x, -y, -start_pos.z)); // double negative on the Z!
normals.push_back(Vector3(0.0, 0.0, 1.0)); mesh->add_normal(Vector3(0.0, 0.0, 1.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(u, v));
uvs.push_back(Vector2(u, v));
point++; point++;
// back // back
points.push_back(Vector3(-x, -y, start_pos.z)); mesh->add_vertex3(Vector3(-x, -y, start_pos.z));
normals.push_back(Vector3(0.0, 0.0, -1.0)); mesh->add_normal(Vector3(0.0, 0.0, -1.0));
ADD_TANGENT(-1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(twothirds + u, v));
uvs.push_back(Vector2(twothirds + u, v));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
int i2 = i * 2; int i2 = i * 2;
// front // front
indices.push_back(prevrow + i2 - 2); mesh->add_index(msi + prevrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2); mesh->add_index(msi + thisrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
// back // back
indices.push_back(prevrow + i2 - 1); mesh->add_index(msi + prevrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 + 1); mesh->add_index(msi + thisrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
}; };
x += size.x / (subdivide_w + 1.0); x += size.x / (subdivide_w + 1.0);
@ -323,37 +296,35 @@ void MeshUtils::create_cube(Array &p_arr, const Vector3 size, const int subdivid
v /= (2.0 * (subdivide_h + 1.0)); v /= (2.0 * (subdivide_h + 1.0));
// right // right
points.push_back(Vector3(-start_pos.x, -y, -z)); mesh->add_vertex3(Vector3(-start_pos.x, -y, -z));
normals.push_back(Vector3(1.0, 0.0, 0.0)); mesh->add_normal(Vector3(1.0, 0.0, 0.0));
ADD_TANGENT(0.0, 0.0, -1.0, 1.0); mesh->add_uv(Vector2(onethird + u, v));
uvs.push_back(Vector2(onethird + u, v));
point++; point++;
// left // left
points.push_back(Vector3(start_pos.x, -y, z)); mesh->add_vertex3(Vector3(start_pos.x, -y, z));
normals.push_back(Vector3(-1.0, 0.0, 0.0)); mesh->add_normal(Vector3(-1.0, 0.0, 0.0));
ADD_TANGENT(0.0, 0.0, 1.0, 1.0); mesh->add_uv(Vector2(u, 0.5 + v));
uvs.push_back(Vector2(u, 0.5 + v));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
int i2 = i * 2; int i2 = i * 2;
// right // right
indices.push_back(prevrow + i2 - 2); mesh->add_index(msi + prevrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2); mesh->add_index(msi + thisrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
// left // left
indices.push_back(prevrow + i2 - 1); mesh->add_index(msi + prevrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 + 1); mesh->add_index(msi + thisrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
}; };
z += size.z / (subdivide_d + 1.0); z += size.z / (subdivide_d + 1.0);
@ -377,37 +348,35 @@ void MeshUtils::create_cube(Array &p_arr, const Vector3 size, const int subdivid
v /= (2.0 * (subdivide_d + 1.0)); v /= (2.0 * (subdivide_d + 1.0));
// top // top
points.push_back(Vector3(-x, -start_pos.y, -z)); mesh->add_vertex3(Vector3(-x, -start_pos.y, -z));
normals.push_back(Vector3(0.0, 1.0, 0.0)); mesh->add_normal(Vector3(0.0, 1.0, 0.0));
ADD_TANGENT(-1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(onethird + u, 0.5 + v));
uvs.push_back(Vector2(onethird + u, 0.5 + v));
point++; point++;
// bottom // bottom
points.push_back(Vector3(x, start_pos.y, -z)); mesh->add_vertex3(Vector3(x, start_pos.y, -z));
normals.push_back(Vector3(0.0, -1.0, 0.0)); mesh->add_normal(Vector3(0.0, -1.0, 0.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(twothirds + u, 0.5 + v));
uvs.push_back(Vector2(twothirds + u, 0.5 + v));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
int i2 = i * 2; int i2 = i * 2;
// top // top
indices.push_back(prevrow + i2 - 2); mesh->add_index(msi + prevrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2); mesh->add_index(msi + thisrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
// bottom // bottom
indices.push_back(prevrow + i2 - 1); mesh->add_index(msi + prevrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 + 1); mesh->add_index(msi + thisrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
}; };
x += size.x / (subdivide_w + 1.0); x += size.x / (subdivide_w + 1.0);
@ -417,34 +386,21 @@ void MeshUtils::create_cube(Array &p_arr, const Vector3 size, const int subdivid
prevrow = thisrow; prevrow = thisrow;
thisrow = point; thisrow = point;
}; };
p_arr[RS::ARRAY_VERTEX] = points;
p_arr[RS::ARRAY_NORMAL] = normals;
p_arr[RS::ARRAY_TANGENT] = tangents;
p_arr[RS::ARRAY_TEX_UV] = uvs;
p_arr[RS::ARRAY_INDEX] = indices;
} }
void MeshUtils::create_cylinder(Array &p_arr, float top_radius, float bottom_radius, float height, int radial_segments, int rings) { void MeshUtils::create_cylinder(Ref<Mesh> mesh, float top_radius, float bottom_radius, float height, int p_radial_segments, int p_rings) {
//radial_segments = p_segments > 4 ? p_segments : 4; ERR_FAIL_COND(!mesh.is_valid());
//rings = p_rings > 0 ? p_rings : 0;
int msi = mesh->get_vertex_count();
int radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
int rings = p_rings > 0 ? p_rings : 0;
int i, j, prevrow, thisrow, point; int i, j, prevrow, thisrow, point;
float x, y, z, u, v, radius; float x, y, z, u, v, radius;
PoolVector<Vector3> points;
PoolVector<Vector3> normals;
PoolVector<float> tangents;
PoolVector<Vector2> uvs;
PoolVector<int> indices;
point = 0; point = 0;
#define ADD_TANGENT(m_x, m_y, m_z, m_d) \
tangents.push_back(m_x); \
tangents.push_back(m_y); \
tangents.push_back(m_z); \
tangents.push_back(m_d);
thisrow = 0; thisrow = 0;
prevrow = 0; prevrow = 0;
const real_t side_normal_y = (bottom_radius - top_radius) / height; const real_t side_normal_y = (bottom_radius - top_radius) / height;
@ -465,20 +421,19 @@ void MeshUtils::create_cylinder(Array &p_arr, float top_radius, float bottom_rad
z = cos(u * (Math_PI * 2.0)); z = cos(u * (Math_PI * 2.0));
Vector3 p = Vector3(x * radius, y, z * radius); Vector3 p = Vector3(x * radius, y, z * radius);
points.push_back(p); mesh->add_vertex3(p);
normals.push_back(Vector3(x, side_normal_y, z).normalized()); mesh->add_normal(Vector3(x, side_normal_y, z).normalized());
ADD_TANGENT(z, 0.0, -x, 1.0) mesh->add_uv(Vector2(u, v * 0.5));
uvs.push_back(Vector2(u, v * 0.5));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
indices.push_back(prevrow + i - 1); mesh->add_index(msi + prevrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i); mesh->add_index(msi + thisrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
}; };
}; };
@ -491,10 +446,9 @@ void MeshUtils::create_cylinder(Array &p_arr, float top_radius, float bottom_rad
y = height * 0.5; y = height * 0.5;
thisrow = point; thisrow = point;
points.push_back(Vector3(0.0, y, 0.0)); mesh->add_vertex3(Vector3(0.0, y, 0.0));
normals.push_back(Vector3(0.0, 1.0, 0.0)); mesh->add_normal(Vector3(0.0, 1.0, 0.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0) mesh->add_uv(Vector2(0.25, 0.75));
uvs.push_back(Vector2(0.25, 0.75));
point++; point++;
for (i = 0; i <= radial_segments; i++) { for (i = 0; i <= radial_segments; i++) {
@ -508,16 +462,15 @@ void MeshUtils::create_cylinder(Array &p_arr, float top_radius, float bottom_rad
v = 0.5 + ((z + 1.0) * 0.25); v = 0.5 + ((z + 1.0) * 0.25);
Vector3 p = Vector3(x * top_radius, y, z * top_radius); Vector3 p = Vector3(x * top_radius, y, z * top_radius);
points.push_back(p); mesh->add_vertex3(p);
normals.push_back(Vector3(0.0, 1.0, 0.0)); mesh->add_normal(Vector3(0.0, 1.0, 0.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0) mesh->add_uv(Vector2(u, v));
uvs.push_back(Vector2(u, v));
point++; point++;
if (i > 0) { if (i > 0) {
indices.push_back(thisrow); mesh->add_index(msi + thisrow);
indices.push_back(point - 1); mesh->add_index(msi + point - 1);
indices.push_back(point - 2); mesh->add_index(msi + point - 2);
}; };
}; };
}; };
@ -527,10 +480,9 @@ void MeshUtils::create_cylinder(Array &p_arr, float top_radius, float bottom_rad
y = height * -0.5; y = height * -0.5;
thisrow = point; thisrow = point;
points.push_back(Vector3(0.0, y, 0.0)); mesh->add_vertex3(Vector3(0.0, y, 0.0));
normals.push_back(Vector3(0.0, -1.0, 0.0)); mesh->add_normal(Vector3(0.0, -1.0, 0.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0) mesh->add_uv(Vector2(0.75, 0.75));
uvs.push_back(Vector2(0.75, 0.75));
point++; point++;
for (i = 0; i <= radial_segments; i++) { for (i = 0; i <= radial_segments; i++) {
@ -544,49 +496,35 @@ void MeshUtils::create_cylinder(Array &p_arr, float top_radius, float bottom_rad
v = 1.0 - ((z + 1.0) * 0.25); v = 1.0 - ((z + 1.0) * 0.25);
Vector3 p = Vector3(x * bottom_radius, y, z * bottom_radius); Vector3 p = Vector3(x * bottom_radius, y, z * bottom_radius);
points.push_back(p); mesh->add_vertex3(p);
normals.push_back(Vector3(0.0, -1.0, 0.0)); mesh->add_normal(Vector3(0.0, -1.0, 0.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0) mesh->add_uv(Vector2(u, v));
uvs.push_back(Vector2(u, v));
point++; point++;
if (i > 0) { if (i > 0) {
indices.push_back(thisrow); mesh->add_index(msi + thisrow);
indices.push_back(point - 2); mesh->add_index(msi + point - 2);
indices.push_back(point - 1); mesh->add_index(msi + point - 1);
}; };
}; };
}; };
p_arr[RS::ARRAY_VERTEX] = points;
p_arr[RS::ARRAY_NORMAL] = normals;
p_arr[RS::ARRAY_TANGENT] = tangents;
p_arr[RS::ARRAY_TEX_UV] = uvs;
p_arr[RS::ARRAY_INDEX] = indices;
} }
void MeshUtils::create_plane(Array &p_arr, Size2 size, int subdivide_w, int subdivide_d, Vector3 center_offset) { void MeshUtils::create_plane(Ref<Mesh> mesh, Size2 size, int p_subdivide_w, int p_subdivide_d, Vector3 center_offset) {
//subdivide_w = p_divisions > 0 ? p_divisions : 0; ERR_FAIL_COND(!mesh.is_valid());
//subdivide_d = p_divisions > 0 ? p_divisions : 0;
int msi = mesh->get_vertex_count();
int subdivide_w = p_subdivide_w > 0 ? p_subdivide_w : 0;
int subdivide_d = p_subdivide_d > 0 ? p_subdivide_d : 0;
int i, j, prevrow, thisrow, point; int i, j, prevrow, thisrow, point;
float x, z; float x, z;
Size2 start_pos = size * -0.5; Size2 start_pos = size * -0.5;
PoolVector<Vector3> points;
PoolVector<Vector3> normals;
PoolVector<float> tangents;
PoolVector<Vector2> uvs;
PoolVector<int> indices;
point = 0; point = 0;
#define ADD_TANGENT(m_x, m_y, m_z, m_d) \
tangents.push_back(m_x); \
tangents.push_back(m_y); \
tangents.push_back(m_z); \
tangents.push_back(m_d);
// top + bottom // top + bottom
z = start_pos.y; z = start_pos.y;
thisrow = point; thisrow = point;
@ -599,19 +537,18 @@ void MeshUtils::create_plane(Array &p_arr, Size2 size, int subdivide_w, int subd
u /= (subdivide_w + 1.0); u /= (subdivide_w + 1.0);
v /= (subdivide_d + 1.0); v /= (subdivide_d + 1.0);
points.push_back(Vector3(-x, 0.0, -z) + center_offset); mesh->add_vertex3(Vector3(-x, 0.0, -z) + center_offset);
normals.push_back(Vector3(0.0, 1.0, 0.0)); mesh->add_normal(Vector3(0.0, 1.0, 0.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(1.0 - u, 1.0 - v)); // 1.0 - uv to match orientation with Quad
uvs.push_back(Vector2(1.0 - u, 1.0 - v)); // 1.0 - uv to match orientation with Quad
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
indices.push_back(prevrow + i - 1); mesh->add_index(msi + prevrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i); mesh->add_index(msi + thisrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
}; };
x += size.x / (subdivide_w + 1.0); x += size.x / (subdivide_w + 1.0);
@ -621,18 +558,16 @@ void MeshUtils::create_plane(Array &p_arr, Size2 size, int subdivide_w, int subd
prevrow = thisrow; prevrow = thisrow;
thisrow = point; thisrow = point;
}; };
p_arr[RS::ARRAY_VERTEX] = points;
p_arr[RS::ARRAY_NORMAL] = normals;
p_arr[RS::ARRAY_TANGENT] = tangents;
p_arr[RS::ARRAY_TEX_UV] = uvs;
p_arr[RS::ARRAY_INDEX] = indices;
} }
void MeshUtils::create_prism(Array &p_arr, float left_to_right, Vector3 size, int subdivide_w, int subdivide_h, int subdivide_d) { void MeshUtils::create_prism(Ref<Mesh> mesh, float left_to_right, Vector3 size, int p_subdivide_w, int p_subdivide_h, int p_subdivide_d) {
//subdivide_w = p_divisions > 0 ? p_divisions : 0; ERR_FAIL_COND(!mesh.is_valid());
//subdivide_h = p_divisions > 0 ? p_divisions : 0;
//subdivide_d = p_divisions > 0 ? p_divisions : 0; int msi = mesh->get_vertex_count();
int subdivide_w = p_subdivide_w > 0 ? p_subdivide_w : 0;
int subdivide_h = p_subdivide_h > 0 ? p_subdivide_h : 0;
int subdivide_d = p_subdivide_d > 0 ? p_subdivide_d : 0;
int i, j, prevrow, thisrow, point; int i, j, prevrow, thisrow, point;
float x, y, z; float x, y, z;
@ -643,20 +578,9 @@ void MeshUtils::create_prism(Array &p_arr, float left_to_right, Vector3 size, in
// set our bounding box // set our bounding box
PoolVector<Vector3> points;
PoolVector<Vector3> normals;
PoolVector<float> tangents;
PoolVector<Vector2> uvs;
PoolVector<int> indices;
point = 0; point = 0;
#define ADD_TANGENT(m_x, m_y, m_z, m_d) \ // front + back
tangents.push_back(m_x); \
tangents.push_back(m_y); \
tangents.push_back(m_z); \
tangents.push_back(m_d);
// front + back
y = start_pos.y; y = start_pos.y;
thisrow = point; thisrow = point;
prevrow = 0; prevrow = 0;
@ -677,49 +601,47 @@ void MeshUtils::create_prism(Array &p_arr, float left_to_right, Vector3 size, in
u *= scale; u *= scale;
// front // front
points.push_back(Vector3(start_x + x, -y, -start_pos.z)); // double negative on the Z! mesh->add_vertex3(Vector3(start_x + x, -y, -start_pos.z)); // double negative on the Z!
normals.push_back(Vector3(0.0, 0.0, 1.0)); mesh->add_normal(Vector3(0.0, 0.0, 1.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(offset_front + u, v));
uvs.push_back(Vector2(offset_front + u, v));
point++; point++;
// back // back
points.push_back(Vector3(start_x + scaled_size_x - x, -y, start_pos.z)); mesh->add_vertex3(Vector3(start_x + scaled_size_x - x, -y, start_pos.z));
normals.push_back(Vector3(0.0, 0.0, -1.0)); mesh->add_normal(Vector3(0.0, 0.0, -1.0));
ADD_TANGENT(-1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(twothirds + offset_back + u, v));
uvs.push_back(Vector2(twothirds + offset_back + u, v));
point++; point++;
if (i > 0 && j == 1) { if (i > 0 && j == 1) {
int i2 = i * 2; int i2 = i * 2;
// front // front
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2); mesh->add_index(msi + thisrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
// back // back
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 + 1); mesh->add_index(msi + thisrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
} else if (i > 0 && j > 0) { } else if (i > 0 && j > 0) {
int i2 = i * 2; int i2 = i * 2;
// front // front
indices.push_back(prevrow + i2 - 2); mesh->add_index(msi + prevrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2); mesh->add_index(msi + thisrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
// back // back
indices.push_back(prevrow + i2 - 1); mesh->add_index(msi + prevrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 + 1); mesh->add_index(msi + thisrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
}; };
x += scale * size.x / (subdivide_w + 1.0); x += scale * size.x / (subdivide_w + 1.0);
@ -756,37 +678,35 @@ void MeshUtils::create_prism(Array &p_arr, float left_to_right, Vector3 size, in
v /= (2.0 * (subdivide_h + 1.0)); v /= (2.0 * (subdivide_h + 1.0));
// right // right
points.push_back(Vector3(right, -y, -z)); mesh->add_vertex3(Vector3(right, -y, -z));
normals.push_back(normal_right); mesh->add_normal(normal_right);
ADD_TANGENT(0.0, 0.0, -1.0, 1.0); mesh->add_uv(Vector2(onethird + u, v));
uvs.push_back(Vector2(onethird + u, v));
point++; point++;
// left // left
points.push_back(Vector3(left, -y, z)); mesh->add_vertex3(Vector3(left, -y, z));
normals.push_back(normal_left); mesh->add_normal(normal_left);
ADD_TANGENT(0.0, 0.0, 1.0, 1.0); mesh->add_uv(Vector2(u, 0.5 + v));
uvs.push_back(Vector2(u, 0.5 + v));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
int i2 = i * 2; int i2 = i * 2;
// right // right
indices.push_back(prevrow + i2 - 2); mesh->add_index(msi + prevrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
indices.push_back(prevrow + i2); mesh->add_index(msi + prevrow + i2);
indices.push_back(thisrow + i2); mesh->add_index(msi + thisrow + i2);
indices.push_back(thisrow + i2 - 2); mesh->add_index(msi + thisrow + i2 - 2);
// left // left
indices.push_back(prevrow + i2 - 1); mesh->add_index(msi + prevrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
indices.push_back(prevrow + i2 + 1); mesh->add_index(msi + prevrow + i2 + 1);
indices.push_back(thisrow + i2 + 1); mesh->add_index(msi + thisrow + i2 + 1);
indices.push_back(thisrow + i2 - 1); mesh->add_index(msi + thisrow + i2 - 1);
}; };
z += size.z / (subdivide_d + 1.0); z += size.z / (subdivide_d + 1.0);
@ -810,20 +730,19 @@ void MeshUtils::create_prism(Array &p_arr, float left_to_right, Vector3 size, in
v /= (2.0 * (subdivide_d + 1.0)); v /= (2.0 * (subdivide_d + 1.0));
// bottom // bottom
points.push_back(Vector3(x, start_pos.y, -z)); mesh->add_vertex3(Vector3(x, start_pos.y, -z));
normals.push_back(Vector3(0.0, -1.0, 0.0)); mesh->add_normal(Vector3(0.0, -1.0, 0.0));
ADD_TANGENT(1.0, 0.0, 0.0, 1.0); mesh->add_uv(Vector2(twothirds + u, 0.5 + v));
uvs.push_back(Vector2(twothirds + u, 0.5 + v));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
// bottom // bottom
indices.push_back(prevrow + i - 1); mesh->add_index(msi + prevrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i); mesh->add_index(msi + thisrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
}; };
x += size.x / (subdivide_w + 1.0); x += size.x / (subdivide_w + 1.0);
@ -833,24 +752,10 @@ void MeshUtils::create_prism(Array &p_arr, float left_to_right, Vector3 size, in
prevrow = thisrow; prevrow = thisrow;
thisrow = point; thisrow = point;
}; };
p_arr[RS::ARRAY_VERTEX] = points;
p_arr[RS::ARRAY_NORMAL] = normals;
p_arr[RS::ARRAY_TANGENT] = tangents;
p_arr[RS::ARRAY_TEX_UV] = uvs;
p_arr[RS::ARRAY_INDEX] = indices;
} }
void MeshUtils::create_quad(Array &p_arr, Size2 size, Vector3 center_offset) { void MeshUtils::create_quad(Ref<Mesh> mesh, Size2 size, Vector3 center_offset) {
PoolVector<Vector3> faces; ERR_FAIL_COND(!mesh.is_valid());
PoolVector<Vector3> normals;
PoolVector<float> tangents;
PoolVector<Vector2> uvs;
faces.resize(6);
normals.resize(6);
tangents.resize(6 * 4);
uvs.resize(6);
Vector2 _size = Vector2(size.x / 2.0f, size.y / 2.0f); Vector2 _size = Vector2(size.x / 2.0f, size.y / 2.0f);
@ -868,12 +773,9 @@ void MeshUtils::create_quad(Array &p_arr, Size2 size, Vector3 center_offset) {
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
int j = indices[i]; int j = indices[i];
faces.set(i, quad_faces[j]);
normals.set(i, Vector3(0, 0, 1)); mesh->add_vertex3(quad_faces[j]);
tangents.set(i * 4 + 0, 1.0); mesh->add_normal(Vector3(0, 0, 1));
tangents.set(i * 4 + 1, 0.0);
tangents.set(i * 4 + 2, 0.0);
tangents.set(i * 4 + 3, 1.0);
static const Vector2 quad_uv[4] = { static const Vector2 quad_uv[4] = {
Vector2(0, 1), Vector2(0, 1),
@ -882,18 +784,43 @@ void MeshUtils::create_quad(Array &p_arr, Size2 size, Vector3 center_offset) {
Vector2(1, 1), Vector2(1, 1),
}; };
uvs.set(i, quad_uv[j]); mesh->add_uv(quad_uv[j]);
} }
p_arr[RS::ARRAY_VERTEX] = faces;
p_arr[RS::ARRAY_NORMAL] = normals;
p_arr[RS::ARRAY_TANGENT] = tangents;
p_arr[RS::ARRAY_TEX_UV] = uvs;
} }
void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int radial_segments, int rings, bool is_hemisphere) { void MeshUtils::create_quad_with_indices(Ref<Mesh> mesh, Size2 size, Vector3 center_offset) {
//radial_segments = p_radial_segments > 4 ? p_radial_segments : 4; ERR_FAIL_COND(!mesh.is_valid());
//rings = p_rings > 1 ? p_rings : 1;
int msi = mesh->get_vertex_count();
Vector2 _size = Vector2(size.x / 2.0f, size.y / 2.0f);
mesh->add_vertex3(Vector3(-_size.x, -_size.y, 0) + center_offset);
mesh->add_vertex3(Vector3(-_size.x, _size.y, 0) + center_offset);
mesh->add_vertex3(Vector3(_size.x, _size.y, 0) + center_offset);
mesh->add_vertex3(Vector3(_size.x, -_size.y, 0) + center_offset);
mesh->add_normal(Vector3(0, 0, 1));
mesh->add_normal(Vector3(0, 0, 1));
mesh->add_normal(Vector3(0, 0, 1));
mesh->add_normal(Vector3(0, 0, 1));
mesh->add_uv(Vector2(0, 1));
mesh->add_uv(Vector2(0, 0));
mesh->add_uv(Vector2(1, 0));
mesh->add_uv(Vector2(1, 1));
mesh->add_triangle(msi, msi + 1, msi + 2);
mesh->add_triangle(msi, msi + 2, msi + 3);
}
void MeshUtils::create_sphere(Ref<Mesh> mesh, float radius, float height, int p_radial_segments, int p_rings, bool is_hemisphere) {
ERR_FAIL_COND(!mesh.is_valid());
int msi = mesh->get_vertex_count();
int radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
int rings = p_rings > 1 ? p_rings : 1;
int i, j, prevrow, thisrow, point; int i, j, prevrow, thisrow, point;
float x, y, z; float x, y, z;
@ -902,19 +829,8 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
// set our bounding box // set our bounding box
PoolVector<Vector3> points;
PoolVector<Vector3> normals;
PoolVector<float> tangents;
PoolVector<Vector2> uvs;
PoolVector<int> indices;
point = 0; point = 0;
#define ADD_TANGENT(m_x, m_y, m_z, m_d) \
tangents.push_back(m_x); \
tangents.push_back(m_y); \
tangents.push_back(m_z); \
tangents.push_back(m_d);
thisrow = 0; thisrow = 0;
prevrow = 0; prevrow = 0;
for (j = 0; j <= (rings + 1); j++) { for (j = 0; j <= (rings + 1); j++) {
@ -933,46 +849,54 @@ void SphereMesh::create_mesh_array(Array &p_arr, float radius, float height, int
z = cos(u * (Math_PI * 2.0)); z = cos(u * (Math_PI * 2.0));
if (is_hemisphere && y < 0.0) { if (is_hemisphere && y < 0.0) {
points.push_back(Vector3(x * radius * w, 0.0, z * radius * w)); mesh->add_vertex3(Vector3(x * radius * w, 0.0, z * radius * w));
normals.push_back(Vector3(0.0, -1.0, 0.0)); mesh->add_normal(Vector3(0.0, -1.0, 0.0));
} else { } else {
Vector3 p = Vector3(x * radius * w, y, z * radius * w); Vector3 p = Vector3(x * radius * w, y, z * radius * w);
points.push_back(p); mesh->add_vertex3(p);
Vector3 normal = Vector3(x * w * scale, radius * (y / scale), z * w * scale); Vector3 normal = Vector3(x * w * scale, radius * (y / scale), z * w * scale);
normals.push_back(normal.normalized()); mesh->add_normal(normal.normalized());
}; };
ADD_TANGENT(z, 0.0, -x, 1.0)
uvs.push_back(Vector2(u, v)); mesh->add_uv(Vector2(u, v));
point++; point++;
if (i > 0 && j > 0) { if (i > 0 && j > 0) {
indices.push_back(prevrow + i - 1); mesh->add_index(msi + prevrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
indices.push_back(prevrow + i); mesh->add_index(msi + prevrow + i);
indices.push_back(thisrow + i); mesh->add_index(msi + thisrow + i);
indices.push_back(thisrow + i - 1); mesh->add_index(msi + thisrow + i - 1);
}; };
}; };
prevrow = thisrow; prevrow = thisrow;
thisrow = point; thisrow = point;
}; };
p_arr[RS::ARRAY_VERTEX] = points;
p_arr[RS::ARRAY_NORMAL] = normals;
p_arr[RS::ARRAY_TANGENT] = tangents;
p_arr[RS::ARRAY_TEX_UV] = uvs;
p_arr[RS::ARRAY_INDEX] = indices;
} }
void MeshUtils::create_point(Array &p_arr) { void MeshUtils::create_point(Ref<Mesh> mesh) {
PoolVector<Vector3> faces; ERR_FAIL_COND(!mesh.is_valid());
faces.resize(1);
faces.set(0, Vector3(0.0, 0.0, 0.0));
p_arr[RS::ARRAY_VERTEX] = faces; //TODO This should set mesh type instead
int msi = mesh->get_vertex_count();
Vector2 _size = Vector2(0.01, 0.01);
mesh->add_vertex3(Vector3(0, -_size.y, 0));
mesh->add_vertex3(Vector3(-_size.x, _size.y, 0));
mesh->add_vertex3(Vector3(_size.x, _size.y, 0));
mesh->add_normal(Vector3(0, 0, 1));
mesh->add_normal(Vector3(0, 0, 1));
mesh->add_normal(Vector3(0, 0, 1));
mesh->add_uv(Vector2(0.5, 1));
mesh->add_uv(Vector2(0, 0));
mesh->add_uv(Vector2(1, 0));
mesh->add_triangle(msi, msi + 1, msi + 2);
} }
*/

View File

@ -7,18 +7,16 @@
class MeshUtils { class MeshUtils {
public: public:
static void create_cone(Ref<Mesh> mesh); static void create_simple_test_cone(Ref<Mesh> mesh);
static void create_capsule(Ref<Mesh> mesh, float radius, float mid_height, int radial_segments = 64, int rings = 8);
/* static void create_cube(Ref<Mesh> mesh, Vector3 size, int subdivide_w = 0, int subdivide_h = 0, int subdivide_d = 0);
static void create_capsule(Array &p_arr, float radius, float mid_height, int radial_segments = 64, int rings = 8); static void create_cylinder(Ref<Mesh> mesh, float top_radius, float bottom_radius, float height, int radial_segments = 64, int rings = 4);
static void create_cube(Array &p_arr, Vector3 size, int subdivide_w = 0, int subdivide_h = 0, int subdivide_d = 0); static void create_plane(Ref<Mesh> mesh, Size2 size = Size2(2.0, 2.0), int subdivide_w = 0, int subdivide_d = 0, Vector3 center_offset = Vector3(0.0, 0.0, 0.0));
static void create_cylinder(Array &p_arr, float top_radius, float bottom_radius, float height, int radial_segments = 64, int rings = 4); static void create_prism(Ref<Mesh> mesh, float left_to_right = 0.5, Vector3 size = Vector3(2.0, 2.0, 2.0), int subdivide_w = 0, int subdivide_h = 0, int subdivide_d = 0);
static void create_plane(Array &p_arr, Size2 size = Size2(2.0, 2.0), int subdivide_w = 0, int subdivide_d = 0, Vector3 center_offset = Vector3(0.0, 0.0, 0.0)); static void create_quad(Ref<Mesh> mesh, Size2 size = Size2(1.0, 1.0), Vector3 center_offset = Vector3(0.0, 0.0, 0.0));
static void create_prism(Array &p_arr, float left_to_right = 0.5, Vector3 size = Vector3(2.0, 2.0, 2.0), int subdivide_w = 0, int subdivide_h = 0, int subdivide_d = 0); static void create_quad_with_indices(Ref<Mesh> mesh, Size2 size = Size2(1.0, 1.0), Vector3 center_offset = Vector3(0.0, 0.0, 0.0));
static void create_quad(Array &p_arr, Size2 size = Size2(1.0, 1.0), Vector3 center_offset = Vector3(0.0, 0.0, 0.0)); static void create_sphere(Ref<Mesh> mesh, float radius, float height, int radial_segments = 64, int rings = 32, bool is_hemisphere = false);
static void create_sphere(Array &p_arr, float radius, float height, int radial_segments = 64, int rings = 32, bool is_hemisphere = false); static void create_point(Ref<Mesh> mesh);
static void create_point(Array &p_arr);
*/
}; };
#endif #endif