12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571 |
- /*************************************************************************/
- /* primitive_meshes.cpp */
- /*************************************************************************/
- /* This file is part of: */
- /* GODOT ENGINE */
- /* https://godotengine.org */
- /*************************************************************************/
- /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
- /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
- /* */
- /* 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. */
- /*************************************************************************/
- #include "primitive_meshes.h"
- #include "servers/visual_server.h"
- /**
- PrimitiveMesh
- */
- void PrimitiveMesh::_update() const {
- Array arr;
- arr.resize(VS::ARRAY_MAX);
- _create_mesh_array(arr);
- PoolVector<Vector3> points = arr[VS::ARRAY_VERTEX];
- aabb = AABB();
- int pc = points.size();
- ERR_FAIL_COND(pc == 0);
- {
- PoolVector<Vector3>::Read r = points.read();
- for (int i = 0; i < pc; i++) {
- if (i == 0)
- aabb.position = r[i];
- else
- aabb.expand_to(r[i]);
- }
- }
- if (flip_faces) {
- PoolVector<Vector3> normals = arr[VS::ARRAY_NORMAL];
- PoolVector<int> indices = arr[VS::ARRAY_INDEX];
- if (normals.size() && indices.size()) {
- {
- int nc = normals.size();
- PoolVector<Vector3>::Write w = normals.write();
- for (int i = 0; i < nc; i++) {
- w[i] = -w[i];
- }
- }
- {
- int ic = indices.size();
- PoolVector<int>::Write w = indices.write();
- for (int i = 0; i < ic; i += 3) {
- SWAP(w[i + 0], w[i + 1]);
- }
- }
- arr[VS::ARRAY_NORMAL] = normals;
- arr[VS::ARRAY_INDEX] = indices;
- }
- }
- // in with the new
- VisualServer::get_singleton()->mesh_clear(mesh);
- VisualServer::get_singleton()->mesh_add_surface_from_arrays(mesh, (VisualServer::PrimitiveType)primitive_type, arr);
- VisualServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
- pending_request = false;
- clear_cache();
- const_cast<PrimitiveMesh *>(this)->emit_changed();
- }
- void PrimitiveMesh::_request_update() {
- if (pending_request)
- return;
- _update();
- }
- int PrimitiveMesh::get_surface_count() const {
- if (pending_request) {
- _update();
- }
- return 1;
- }
- int PrimitiveMesh::surface_get_array_len(int p_idx) const {
- ERR_FAIL_INDEX_V(p_idx, 1, -1);
- if (pending_request) {
- _update();
- }
- return VisualServer::get_singleton()->mesh_surface_get_array_len(mesh, 0);
- }
- int PrimitiveMesh::surface_get_array_index_len(int p_idx) const {
- ERR_FAIL_INDEX_V(p_idx, 1, -1);
- if (pending_request) {
- _update();
- }
- return VisualServer::get_singleton()->mesh_surface_get_array_index_len(mesh, 0);
- }
- Array PrimitiveMesh::surface_get_arrays(int p_surface) const {
- ERR_FAIL_INDEX_V(p_surface, 1, Array());
- if (pending_request) {
- _update();
- }
- return VisualServer::get_singleton()->mesh_surface_get_arrays(mesh, 0);
- }
- Array PrimitiveMesh::surface_get_blend_shape_arrays(int p_surface) const {
- ERR_FAIL_INDEX_V(p_surface, 1, Array());
- if (pending_request) {
- _update();
- }
- return Array();
- }
- uint32_t PrimitiveMesh::surface_get_format(int p_idx) const {
- ERR_FAIL_INDEX_V(p_idx, 1, 0);
- if (pending_request) {
- _update();
- }
- return VisualServer::get_singleton()->mesh_surface_get_format(mesh, 0);
- }
- Mesh::PrimitiveType PrimitiveMesh::surface_get_primitive_type(int p_idx) const {
- return primitive_type;
- }
- Ref<Material> PrimitiveMesh::surface_get_material(int p_idx) const {
- ERR_FAIL_INDEX_V(p_idx, 1, NULL);
- return material;
- }
- int PrimitiveMesh::get_blend_shape_count() const {
- return 0;
- }
- StringName PrimitiveMesh::get_blend_shape_name(int p_index) const {
- return StringName();
- }
- AABB PrimitiveMesh::get_aabb() const {
- if (pending_request) {
- _update();
- }
- return aabb;
- }
- RID PrimitiveMesh::get_rid() const {
- if (pending_request) {
- _update();
- }
- return mesh;
- }
- void PrimitiveMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("_update"), &PrimitiveMesh::_update);
- ClassDB::bind_method(D_METHOD("set_material", "material"), &PrimitiveMesh::set_material);
- ClassDB::bind_method(D_METHOD("get_material"), &PrimitiveMesh::get_material);
- ClassDB::bind_method(D_METHOD("get_mesh_arrays"), &PrimitiveMesh::get_mesh_arrays);
- ClassDB::bind_method(D_METHOD("set_custom_aabb", "aabb"), &PrimitiveMesh::set_custom_aabb);
- ClassDB::bind_method(D_METHOD("get_custom_aabb"), &PrimitiveMesh::get_custom_aabb);
- ClassDB::bind_method(D_METHOD("set_flip_faces", "flip_faces"), &PrimitiveMesh::set_flip_faces);
- ClassDB::bind_method(D_METHOD("get_flip_faces"), &PrimitiveMesh::get_flip_faces);
- ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "material", PROPERTY_HINT_RESOURCE_TYPE, "SpatialMaterial,ShaderMaterial"), "set_material", "get_material");
- ADD_PROPERTY(PropertyInfo(Variant::AABB, "custom_aabb", PROPERTY_HINT_NONE, ""), "set_custom_aabb", "get_custom_aabb");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "flip_faces"), "set_flip_faces", "get_flip_faces");
- }
- void PrimitiveMesh::set_material(const Ref<Material> &p_material) {
- material = p_material;
- if (!pending_request) {
- // just apply it, else it'll happen when _update is called.
- VisualServer::get_singleton()->mesh_surface_set_material(mesh, 0, material.is_null() ? RID() : material->get_rid());
- _change_notify();
- emit_changed();
- };
- }
- Ref<Material> PrimitiveMesh::get_material() const {
- return material;
- }
- Array PrimitiveMesh::get_mesh_arrays() const {
- return surface_get_arrays(0);
- }
- void PrimitiveMesh::set_custom_aabb(const AABB &p_custom) {
- custom_aabb = p_custom;
- VS::get_singleton()->mesh_set_custom_aabb(mesh, custom_aabb);
- emit_changed();
- }
- AABB PrimitiveMesh::get_custom_aabb() const {
- return custom_aabb;
- }
- void PrimitiveMesh::set_flip_faces(bool p_enable) {
- flip_faces = p_enable;
- _request_update();
- }
- bool PrimitiveMesh::get_flip_faces() const {
- return flip_faces;
- }
- PrimitiveMesh::PrimitiveMesh() {
- flip_faces = false;
- // defaults
- mesh = VisualServer::get_singleton()->mesh_create();
- // assume primitive triangles as the type, correct for all but one and it will change this :)
- primitive_type = Mesh::PRIMITIVE_TRIANGLES;
- // make sure we do an update after we've finished constructing our object
- pending_request = true;
- }
- PrimitiveMesh::~PrimitiveMesh() {
- VisualServer::get_singleton()->free(mesh);
- }
- /**
- CapsuleMesh
- */
- void CapsuleMesh::_create_mesh_array(Array &p_arr) const {
- int i, j, prevrow, thisrow, point;
- float x, y, z, u, v, w;
- float onethird = 1.0 / 3.0;
- float twothirds = 2.0 / 3.0;
- // 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;
- #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 */
- thisrow = 0;
- prevrow = 0;
- for (j = 0; j <= (rings + 1); j++) {
- v = j;
- v /= (rings + 1);
- w = sin(0.5 * Math_PI * v);
- z = radius * cos(0.5 * Math_PI * v);
- for (i = 0; i <= radial_segments; i++) {
- u = i;
- u /= radial_segments;
- x = sin(u * (Math_PI * 2.0));
- y = -cos(u * (Math_PI * 2.0));
- Vector3 p = Vector3(x * radius * w, y * radius * w, z);
- points.push_back(p + Vector3(0.0, 0.0, 0.5 * mid_height));
- normals.push_back(p.normalized());
- ADD_TANGENT(-y, x, 0.0, 1.0)
- uvs.push_back(Vector2(u, v * onethird));
- point++;
- if (i > 0 && j > 0) {
- indices.push_back(prevrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i);
- indices.push_back(thisrow + i - 1);
- };
- };
- prevrow = thisrow;
- thisrow = point;
- };
- /* cylinder */
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (rings + 1); j++) {
- v = j;
- v /= (rings + 1);
- z = mid_height * v;
- z = (mid_height * 0.5) - z;
- for (i = 0; i <= radial_segments; i++) {
- u = i;
- u /= radial_segments;
- x = sin(u * (Math_PI * 2.0));
- y = -cos(u * (Math_PI * 2.0));
- Vector3 p = Vector3(x * radius, y * radius, z);
- points.push_back(p);
- normals.push_back(Vector3(x, y, 0.0));
- ADD_TANGENT(-y, x, 0.0, 1.0)
- uvs.push_back(Vector2(u, onethird + (v * onethird)));
- point++;
- if (i > 0 && j > 0) {
- indices.push_back(prevrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i);
- indices.push_back(thisrow + i - 1);
- };
- };
- prevrow = thisrow;
- thisrow = point;
- };
- /* bottom hemisphere */
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (rings + 1); j++) {
- v = j;
- v /= (rings + 1);
- v += 1.0;
- w = sin(0.5 * Math_PI * v);
- z = radius * cos(0.5 * Math_PI * v);
- for (i = 0; i <= radial_segments; i++) {
- float u2 = i;
- u2 /= radial_segments;
- x = sin(u2 * (Math_PI * 2.0));
- y = -cos(u2 * (Math_PI * 2.0));
- Vector3 p = Vector3(x * radius * w, y * radius * w, z);
- points.push_back(p + Vector3(0.0, 0.0, -0.5 * mid_height));
- normals.push_back(p.normalized());
- ADD_TANGENT(-y, x, 0.0, 1.0)
- uvs.push_back(Vector2(u2, twothirds + ((v - 1.0) * onethird)));
- point++;
- if (i > 0 && j > 0) {
- indices.push_back(prevrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i);
- indices.push_back(thisrow + i - 1);
- };
- };
- prevrow = thisrow;
- thisrow = point;
- };
- p_arr[VS::ARRAY_VERTEX] = points;
- p_arr[VS::ARRAY_NORMAL] = normals;
- p_arr[VS::ARRAY_TANGENT] = tangents;
- p_arr[VS::ARRAY_TEX_UV] = uvs;
- p_arr[VS::ARRAY_INDEX] = indices;
- }
- void CapsuleMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_radius", "radius"), &CapsuleMesh::set_radius);
- ClassDB::bind_method(D_METHOD("get_radius"), &CapsuleMesh::get_radius);
- ClassDB::bind_method(D_METHOD("set_mid_height", "mid_height"), &CapsuleMesh::set_mid_height);
- ClassDB::bind_method(D_METHOD("get_mid_height"), &CapsuleMesh::get_mid_height);
- ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CapsuleMesh::set_radial_segments);
- ClassDB::bind_method(D_METHOD("get_radial_segments"), &CapsuleMesh::get_radial_segments);
- ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CapsuleMesh::set_rings);
- ClassDB::bind_method(D_METHOD("get_rings"), &CapsuleMesh::get_rings);
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_radius", "get_radius");
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "mid_height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_mid_height", "get_mid_height");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
- }
- void CapsuleMesh::set_radius(const float p_radius) {
- radius = p_radius;
- _request_update();
- }
- float CapsuleMesh::get_radius() const {
- return radius;
- }
- void CapsuleMesh::set_mid_height(const float p_mid_height) {
- mid_height = p_mid_height;
- _request_update();
- }
- float CapsuleMesh::get_mid_height() const {
- return mid_height;
- }
- void CapsuleMesh::set_radial_segments(const int p_segments) {
- radial_segments = p_segments > 4 ? p_segments : 4;
- _request_update();
- }
- int CapsuleMesh::get_radial_segments() const {
- return radial_segments;
- }
- void CapsuleMesh::set_rings(const int p_rings) {
- rings = p_rings > 1 ? p_rings : 1;
- _request_update();
- }
- int CapsuleMesh::get_rings() const {
- return rings;
- }
- CapsuleMesh::CapsuleMesh() {
- // defaults
- radius = 1.0;
- mid_height = 1.0;
- radial_segments = 64;
- rings = 8;
- }
- /**
- CubeMesh
- */
- void CubeMesh::_create_mesh_array(Array &p_arr) const {
- int i, j, prevrow, thisrow, point;
- float x, y, z;
- float onethird = 1.0 / 3.0;
- float twothirds = 2.0 / 3.0;
- Vector3 start_pos = size * -0.5;
- // set our bounding box
- PoolVector<Vector3> points;
- PoolVector<Vector3> normals;
- PoolVector<float> tangents;
- PoolVector<Vector2> uvs;
- PoolVector<int> indices;
- 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
- y = start_pos.y;
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= subdivide_h + 1; j++) {
- x = start_pos.x;
- for (i = 0; i <= subdivide_w + 1; i++) {
- float u = i;
- float v = j;
- u /= (3.0 * (subdivide_w + 1.0));
- v /= (2.0 * (subdivide_h + 1.0));
- // front
- points.push_back(Vector3(x, -y, -start_pos.z)); // double negative on the Z!
- normals.push_back(Vector3(0.0, 0.0, 1.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(u, v));
- point++;
- // back
- points.push_back(Vector3(-x, -y, start_pos.z));
- normals.push_back(Vector3(0.0, 0.0, -1.0));
- ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(twothirds + u, v));
- point++;
- if (i > 0 && j > 0) {
- int i2 = i * 2;
- // front
- indices.push_back(prevrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2);
- indices.push_back(thisrow + i2 - 2);
- // back
- indices.push_back(prevrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- };
- x += size.x / (subdivide_w + 1.0);
- };
- y += size.y / (subdivide_h + 1.0);
- prevrow = thisrow;
- thisrow = point;
- };
- // left + right
- y = start_pos.y;
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (subdivide_h + 1); j++) {
- z = start_pos.z;
- for (i = 0; i <= (subdivide_d + 1); i++) {
- float u = i;
- float v = j;
- u /= (3.0 * (subdivide_d + 1.0));
- v /= (2.0 * (subdivide_h + 1.0));
- // right
- points.push_back(Vector3(-start_pos.x, -y, -z));
- normals.push_back(Vector3(1.0, 0.0, 0.0));
- ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
- uvs.push_back(Vector2(onethird + u, v));
- point++;
- // left
- points.push_back(Vector3(start_pos.x, -y, z));
- normals.push_back(Vector3(-1.0, 0.0, 0.0));
- ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
- uvs.push_back(Vector2(u, 0.5 + v));
- point++;
- if (i > 0 && j > 0) {
- int i2 = i * 2;
- // right
- indices.push_back(prevrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2);
- indices.push_back(thisrow + i2 - 2);
- // left
- indices.push_back(prevrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- };
- z += size.z / (subdivide_d + 1.0);
- };
- y += size.y / (subdivide_h + 1.0);
- prevrow = thisrow;
- thisrow = point;
- };
- // top + bottom
- z = start_pos.z;
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (subdivide_d + 1); j++) {
- x = start_pos.x;
- for (i = 0; i <= (subdivide_w + 1); i++) {
- float u = i;
- float v = j;
- u /= (3.0 * (subdivide_w + 1.0));
- v /= (2.0 * (subdivide_d + 1.0));
- // top
- points.push_back(Vector3(-x, -start_pos.y, -z));
- normals.push_back(Vector3(0.0, 1.0, 0.0));
- ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(onethird + u, 0.5 + v));
- point++;
- // bottom
- points.push_back(Vector3(x, start_pos.y, -z));
- normals.push_back(Vector3(0.0, -1.0, 0.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(twothirds + u, 0.5 + v));
- point++;
- if (i > 0 && j > 0) {
- int i2 = i * 2;
- // top
- indices.push_back(prevrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2);
- indices.push_back(thisrow + i2 - 2);
- // bottom
- indices.push_back(prevrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- };
- x += size.x / (subdivide_w + 1.0);
- };
- z += size.z / (subdivide_d + 1.0);
- prevrow = thisrow;
- thisrow = point;
- };
- p_arr[VS::ARRAY_VERTEX] = points;
- p_arr[VS::ARRAY_NORMAL] = normals;
- p_arr[VS::ARRAY_TANGENT] = tangents;
- p_arr[VS::ARRAY_TEX_UV] = uvs;
- p_arr[VS::ARRAY_INDEX] = indices;
- }
- void CubeMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_size", "size"), &CubeMesh::set_size);
- ClassDB::bind_method(D_METHOD("get_size"), &CubeMesh::get_size);
- ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &CubeMesh::set_subdivide_width);
- ClassDB::bind_method(D_METHOD("get_subdivide_width"), &CubeMesh::get_subdivide_width);
- ClassDB::bind_method(D_METHOD("set_subdivide_height", "divisions"), &CubeMesh::set_subdivide_height);
- ClassDB::bind_method(D_METHOD("get_subdivide_height"), &CubeMesh::get_subdivide_height);
- ClassDB::bind_method(D_METHOD("set_subdivide_depth", "divisions"), &CubeMesh::set_subdivide_depth);
- ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &CubeMesh::get_subdivide_depth);
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
- }
- void CubeMesh::set_size(const Vector3 &p_size) {
- size = p_size;
- _request_update();
- }
- Vector3 CubeMesh::get_size() const {
- return size;
- }
- void CubeMesh::set_subdivide_width(const int p_divisions) {
- subdivide_w = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int CubeMesh::get_subdivide_width() const {
- return subdivide_w;
- }
- void CubeMesh::set_subdivide_height(const int p_divisions) {
- subdivide_h = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int CubeMesh::get_subdivide_height() const {
- return subdivide_h;
- }
- void CubeMesh::set_subdivide_depth(const int p_divisions) {
- subdivide_d = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int CubeMesh::get_subdivide_depth() const {
- return subdivide_d;
- }
- CubeMesh::CubeMesh() {
- // defaults
- size = Vector3(2.0, 2.0, 2.0);
- subdivide_w = 0;
- subdivide_h = 0;
- subdivide_d = 0;
- }
- /**
- CylinderMesh
- */
- void CylinderMesh::_create_mesh_array(Array &p_arr) const {
- int i, j, prevrow, thisrow, point;
- float x, y, z, u, v, radius;
- radius = bottom_radius > top_radius ? bottom_radius : top_radius;
- PoolVector<Vector3> points;
- PoolVector<Vector3> normals;
- PoolVector<float> tangents;
- PoolVector<Vector2> uvs;
- PoolVector<int> indices;
- 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;
- prevrow = 0;
- for (j = 0; j <= (rings + 1); j++) {
- v = j;
- v /= (rings + 1);
- radius = top_radius + ((bottom_radius - top_radius) * v);
- y = height * v;
- y = (height * 0.5) - y;
- for (i = 0; i <= radial_segments; i++) {
- u = i;
- u /= radial_segments;
- x = sin(u * (Math_PI * 2.0));
- z = cos(u * (Math_PI * 2.0));
- Vector3 p = Vector3(x * radius, y, z * radius);
- points.push_back(p);
- normals.push_back(Vector3(x, 0.0, z));
- ADD_TANGENT(z, 0.0, -x, 1.0)
- uvs.push_back(Vector2(u, v * 0.5));
- point++;
- if (i > 0 && j > 0) {
- indices.push_back(prevrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i);
- indices.push_back(thisrow + i - 1);
- };
- };
- prevrow = thisrow;
- thisrow = point;
- };
- // add top
- if (top_radius > 0.0) {
- y = height * 0.5;
- thisrow = point;
- points.push_back(Vector3(0.0, y, 0.0));
- normals.push_back(Vector3(0.0, 1.0, 0.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
- uvs.push_back(Vector2(0.25, 0.75));
- point++;
- for (i = 0; i <= radial_segments; i++) {
- float r = i;
- r /= radial_segments;
- x = sin(r * (Math_PI * 2.0));
- z = cos(r * (Math_PI * 2.0));
- u = ((x + 1.0) * 0.25);
- v = 0.5 + ((z + 1.0) * 0.25);
- Vector3 p = Vector3(x * top_radius, y, z * top_radius);
- points.push_back(p);
- normals.push_back(Vector3(0.0, 1.0, 0.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
- uvs.push_back(Vector2(u, v));
- point++;
- if (i > 0) {
- indices.push_back(thisrow);
- indices.push_back(point - 1);
- indices.push_back(point - 2);
- };
- };
- };
- // add bottom
- if (bottom_radius > 0.0) {
- y = height * -0.5;
- thisrow = point;
- points.push_back(Vector3(0.0, y, 0.0));
- normals.push_back(Vector3(0.0, -1.0, 0.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
- uvs.push_back(Vector2(0.75, 0.75));
- point++;
- for (i = 0; i <= radial_segments; i++) {
- float r = i;
- r /= radial_segments;
- x = sin(r * (Math_PI * 2.0));
- z = cos(r * (Math_PI * 2.0));
- u = 0.5 + ((x + 1.0) * 0.25);
- v = 1.0 - ((z + 1.0) * 0.25);
- Vector3 p = Vector3(x * bottom_radius, y, z * bottom_radius);
- points.push_back(p);
- normals.push_back(Vector3(0.0, -1.0, 0.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0)
- uvs.push_back(Vector2(u, v));
- point++;
- if (i > 0) {
- indices.push_back(thisrow);
- indices.push_back(point - 2);
- indices.push_back(point - 1);
- };
- };
- };
- p_arr[VS::ARRAY_VERTEX] = points;
- p_arr[VS::ARRAY_NORMAL] = normals;
- p_arr[VS::ARRAY_TANGENT] = tangents;
- p_arr[VS::ARRAY_TEX_UV] = uvs;
- p_arr[VS::ARRAY_INDEX] = indices;
- }
- void CylinderMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_top_radius", "radius"), &CylinderMesh::set_top_radius);
- ClassDB::bind_method(D_METHOD("get_top_radius"), &CylinderMesh::get_top_radius);
- ClassDB::bind_method(D_METHOD("set_bottom_radius", "radius"), &CylinderMesh::set_bottom_radius);
- ClassDB::bind_method(D_METHOD("get_bottom_radius"), &CylinderMesh::get_bottom_radius);
- ClassDB::bind_method(D_METHOD("set_height", "height"), &CylinderMesh::set_height);
- ClassDB::bind_method(D_METHOD("get_height"), &CylinderMesh::get_height);
- ClassDB::bind_method(D_METHOD("set_radial_segments", "segments"), &CylinderMesh::set_radial_segments);
- ClassDB::bind_method(D_METHOD("get_radial_segments"), &CylinderMesh::get_radial_segments);
- ClassDB::bind_method(D_METHOD("set_rings", "rings"), &CylinderMesh::set_rings);
- ClassDB::bind_method(D_METHOD("get_rings"), &CylinderMesh::get_rings);
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "top_radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_top_radius", "get_top_radius");
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "bottom_radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_bottom_radius", "get_bottom_radius");
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_height", "get_height");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
- }
- void CylinderMesh::set_top_radius(const float p_radius) {
- top_radius = p_radius;
- _request_update();
- }
- float CylinderMesh::get_top_radius() const {
- return top_radius;
- }
- void CylinderMesh::set_bottom_radius(const float p_radius) {
- bottom_radius = p_radius;
- _request_update();
- }
- float CylinderMesh::get_bottom_radius() const {
- return bottom_radius;
- }
- void CylinderMesh::set_height(const float p_height) {
- height = p_height;
- _request_update();
- }
- float CylinderMesh::get_height() const {
- return height;
- }
- void CylinderMesh::set_radial_segments(const int p_segments) {
- radial_segments = p_segments > 4 ? p_segments : 4;
- _request_update();
- }
- int CylinderMesh::get_radial_segments() const {
- return radial_segments;
- }
- void CylinderMesh::set_rings(const int p_rings) {
- rings = p_rings > 0 ? p_rings : 0;
- _request_update();
- }
- int CylinderMesh::get_rings() const {
- return rings;
- }
- CylinderMesh::CylinderMesh() {
- // defaults
- top_radius = 1.0;
- bottom_radius = 1.0;
- height = 2.0;
- radial_segments = 64;
- rings = 4;
- }
- /**
- PlaneMesh
- */
- void PlaneMesh::_create_mesh_array(Array &p_arr) const {
- int i, j, prevrow, thisrow, point;
- float x, z;
- Size2 start_pos = size * -0.5;
- PoolVector<Vector3> points;
- PoolVector<Vector3> normals;
- PoolVector<float> tangents;
- PoolVector<Vector2> uvs;
- PoolVector<int> indices;
- 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 */
- z = start_pos.y;
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (subdivide_d + 1); j++) {
- x = start_pos.x;
- for (i = 0; i <= (subdivide_w + 1); i++) {
- float u = i;
- float v = j;
- u /= (subdivide_w + 1.0);
- v /= (subdivide_d + 1.0);
- points.push_back(Vector3(-x, 0.0, -z));
- normals.push_back(Vector3(0.0, 1.0, 0.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(1.0 - u, 1.0 - v)); /* 1.0 - uv to match orientation with Quad */
- point++;
- if (i > 0 && j > 0) {
- indices.push_back(prevrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i);
- indices.push_back(thisrow + i - 1);
- };
- x += size.x / (subdivide_w + 1.0);
- };
- z += size.y / (subdivide_d + 1.0);
- prevrow = thisrow;
- thisrow = point;
- };
- p_arr[VS::ARRAY_VERTEX] = points;
- p_arr[VS::ARRAY_NORMAL] = normals;
- p_arr[VS::ARRAY_TANGENT] = tangents;
- p_arr[VS::ARRAY_TEX_UV] = uvs;
- p_arr[VS::ARRAY_INDEX] = indices;
- }
- void PlaneMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_size", "size"), &PlaneMesh::set_size);
- ClassDB::bind_method(D_METHOD("get_size"), &PlaneMesh::get_size);
- ClassDB::bind_method(D_METHOD("set_subdivide_width", "subdivide"), &PlaneMesh::set_subdivide_width);
- ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PlaneMesh::get_subdivide_width);
- ClassDB::bind_method(D_METHOD("set_subdivide_depth", "subdivide"), &PlaneMesh::set_subdivide_depth);
- ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PlaneMesh::get_subdivide_depth);
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
- }
- void PlaneMesh::set_size(const Size2 &p_size) {
- size = p_size;
- _request_update();
- }
- Size2 PlaneMesh::get_size() const {
- return size;
- }
- void PlaneMesh::set_subdivide_width(const int p_divisions) {
- subdivide_w = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int PlaneMesh::get_subdivide_width() const {
- return subdivide_w;
- }
- void PlaneMesh::set_subdivide_depth(const int p_divisions) {
- subdivide_d = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int PlaneMesh::get_subdivide_depth() const {
- return subdivide_d;
- }
- PlaneMesh::PlaneMesh() {
- // defaults
- size = Size2(2.0, 2.0);
- subdivide_w = 0;
- subdivide_d = 0;
- }
- /**
- PrismMesh
- */
- void PrismMesh::_create_mesh_array(Array &p_arr) const {
- int i, j, prevrow, thisrow, point;
- float x, y, z;
- float onethird = 1.0 / 3.0;
- float twothirds = 2.0 / 3.0;
- Vector3 start_pos = size * -0.5;
- // set our bounding box
- PoolVector<Vector3> points;
- PoolVector<Vector3> normals;
- PoolVector<float> tangents;
- PoolVector<Vector2> uvs;
- PoolVector<int> indices;
- 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 */
- y = start_pos.y;
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (subdivide_h + 1); j++) {
- float scale = (y - start_pos.y) / size.y;
- float scaled_size_x = size.x * scale;
- float start_x = start_pos.x + (1.0 - scale) * size.x * left_to_right;
- float offset_front = (1.0 - scale) * onethird * left_to_right;
- float offset_back = (1.0 - scale) * onethird * (1.0 - left_to_right);
- x = 0.0;
- for (i = 0; i <= (subdivide_w + 1); i++) {
- float u = i;
- float v = j;
- u /= (3.0 * (subdivide_w + 1.0));
- v /= (2.0 * (subdivide_h + 1.0));
- u *= scale;
- /* front */
- points.push_back(Vector3(start_x + x, -y, -start_pos.z)); // double negative on the Z!
- normals.push_back(Vector3(0.0, 0.0, 1.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(offset_front + u, v));
- point++;
- /* back */
- points.push_back(Vector3(start_x + scaled_size_x - x, -y, start_pos.z));
- normals.push_back(Vector3(0.0, 0.0, -1.0));
- ADD_TANGENT(-1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(twothirds + offset_back + u, v));
- point++;
- if (i > 0 && j == 1) {
- int i2 = i * 2;
- /* front */
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2);
- indices.push_back(thisrow + i2 - 2);
- /* back */
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- } else if (i > 0 && j > 0) {
- int i2 = i * 2;
- /* front */
- indices.push_back(prevrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2);
- indices.push_back(thisrow + i2 - 2);
- /* back */
- indices.push_back(prevrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- };
- x += scale * size.x / (subdivide_w + 1.0);
- };
- y += size.y / (subdivide_h + 1.0);
- prevrow = thisrow;
- thisrow = point;
- };
- /* left + right */
- Vector3 normal_left, normal_right;
- normal_left = Vector3(-size.y, size.x * left_to_right, 0.0);
- normal_right = Vector3(size.y, size.x * left_to_right, 0.0);
- normal_left.normalize();
- normal_right.normalize();
- y = start_pos.y;
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (subdivide_h + 1); j++) {
- float left, right;
- float scale = (y - start_pos.y) / size.y;
- left = start_pos.x + (size.x * (1.0 - scale) * left_to_right);
- right = left + (size.x * scale);
- z = start_pos.z;
- for (i = 0; i <= (subdivide_d + 1); i++) {
- float u = i;
- float v = j;
- u /= (3.0 * (subdivide_d + 1.0));
- v /= (2.0 * (subdivide_h + 1.0));
- /* right */
- points.push_back(Vector3(right, -y, -z));
- normals.push_back(normal_right);
- ADD_TANGENT(0.0, 0.0, -1.0, 1.0);
- uvs.push_back(Vector2(onethird + u, v));
- point++;
- /* left */
- points.push_back(Vector3(left, -y, z));
- normals.push_back(normal_left);
- ADD_TANGENT(0.0, 0.0, 1.0, 1.0);
- uvs.push_back(Vector2(u, 0.5 + v));
- point++;
- if (i > 0 && j > 0) {
- int i2 = i * 2;
- /* right */
- indices.push_back(prevrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2 - 2);
- indices.push_back(prevrow + i2);
- indices.push_back(thisrow + i2);
- indices.push_back(thisrow + i2 - 2);
- /* left */
- indices.push_back(prevrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- indices.push_back(prevrow + i2 + 1);
- indices.push_back(thisrow + i2 + 1);
- indices.push_back(thisrow + i2 - 1);
- };
- z += size.z / (subdivide_d + 1.0);
- };
- y += size.y / (subdivide_h + 1.0);
- prevrow = thisrow;
- thisrow = point;
- };
- /* bottom */
- z = start_pos.z;
- thisrow = point;
- prevrow = 0;
- for (j = 0; j <= (subdivide_d + 1); j++) {
- x = start_pos.x;
- for (i = 0; i <= (subdivide_w + 1); i++) {
- float u = i;
- float v = j;
- u /= (3.0 * (subdivide_w + 1.0));
- v /= (2.0 * (subdivide_d + 1.0));
- /* bottom */
- points.push_back(Vector3(x, start_pos.y, -z));
- normals.push_back(Vector3(0.0, -1.0, 0.0));
- ADD_TANGENT(1.0, 0.0, 0.0, 1.0);
- uvs.push_back(Vector2(twothirds + u, 0.5 + v));
- point++;
- if (i > 0 && j > 0) {
- /* bottom */
- indices.push_back(prevrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i);
- indices.push_back(thisrow + i - 1);
- };
- x += size.x / (subdivide_w + 1.0);
- };
- z += size.z / (subdivide_d + 1.0);
- prevrow = thisrow;
- thisrow = point;
- };
- p_arr[VS::ARRAY_VERTEX] = points;
- p_arr[VS::ARRAY_NORMAL] = normals;
- p_arr[VS::ARRAY_TANGENT] = tangents;
- p_arr[VS::ARRAY_TEX_UV] = uvs;
- p_arr[VS::ARRAY_INDEX] = indices;
- }
- void PrismMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_left_to_right", "left_to_right"), &PrismMesh::set_left_to_right);
- ClassDB::bind_method(D_METHOD("get_left_to_right"), &PrismMesh::get_left_to_right);
- ClassDB::bind_method(D_METHOD("set_size", "size"), &PrismMesh::set_size);
- ClassDB::bind_method(D_METHOD("get_size"), &PrismMesh::get_size);
- ClassDB::bind_method(D_METHOD("set_subdivide_width", "segments"), &PrismMesh::set_subdivide_width);
- ClassDB::bind_method(D_METHOD("get_subdivide_width"), &PrismMesh::get_subdivide_width);
- ClassDB::bind_method(D_METHOD("set_subdivide_height", "segments"), &PrismMesh::set_subdivide_height);
- ClassDB::bind_method(D_METHOD("get_subdivide_height"), &PrismMesh::get_subdivide_height);
- ClassDB::bind_method(D_METHOD("set_subdivide_depth", "segments"), &PrismMesh::set_subdivide_depth);
- ClassDB::bind_method(D_METHOD("get_subdivide_depth"), &PrismMesh::get_subdivide_depth);
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "left_to_right", PROPERTY_HINT_RANGE, "-2.0,2.0,0.1"), "set_left_to_right", "get_left_to_right");
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "size"), "set_size", "get_size");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_width", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_width", "get_subdivide_width");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_height", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_height", "get_subdivide_height");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "subdivide_depth", PROPERTY_HINT_RANGE, "0,100,1,or_greater"), "set_subdivide_depth", "get_subdivide_depth");
- }
- void PrismMesh::set_left_to_right(const float p_left_to_right) {
- left_to_right = p_left_to_right;
- _request_update();
- }
- float PrismMesh::get_left_to_right() const {
- return left_to_right;
- }
- void PrismMesh::set_size(const Vector3 &p_size) {
- size = p_size;
- _request_update();
- }
- Vector3 PrismMesh::get_size() const {
- return size;
- }
- void PrismMesh::set_subdivide_width(const int p_divisions) {
- subdivide_w = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int PrismMesh::get_subdivide_width() const {
- return subdivide_w;
- }
- void PrismMesh::set_subdivide_height(const int p_divisions) {
- subdivide_h = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int PrismMesh::get_subdivide_height() const {
- return subdivide_h;
- }
- void PrismMesh::set_subdivide_depth(const int p_divisions) {
- subdivide_d = p_divisions > 0 ? p_divisions : 0;
- _request_update();
- }
- int PrismMesh::get_subdivide_depth() const {
- return subdivide_d;
- }
- PrismMesh::PrismMesh() {
- // defaults
- left_to_right = 0.5;
- size = Vector3(2.0, 2.0, 2.0);
- subdivide_w = 0;
- subdivide_h = 0;
- subdivide_d = 0;
- }
- /**
- QuadMesh
- */
- void QuadMesh::_create_mesh_array(Array &p_arr) const {
- PoolVector<Vector3> faces;
- 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);
- Vector3 quad_faces[4] = {
- Vector3(-_size.x, -_size.y, 0),
- Vector3(-_size.x, _size.y, 0),
- Vector3(_size.x, _size.y, 0),
- Vector3(_size.x, -_size.y, 0),
- };
- static const int indices[6] = {
- 0, 1, 2,
- 0, 2, 3
- };
- for (int i = 0; i < 6; i++) {
- int j = indices[i];
- faces.set(i, quad_faces[j]);
- normals.set(i, Vector3(0, 0, 1));
- tangents.set(i * 4 + 0, 1.0);
- 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] = {
- Vector2(0, 1),
- Vector2(0, 0),
- Vector2(1, 0),
- Vector2(1, 1),
- };
- uvs.set(i, quad_uv[j]);
- }
- p_arr[VS::ARRAY_VERTEX] = faces;
- p_arr[VS::ARRAY_NORMAL] = normals;
- p_arr[VS::ARRAY_TANGENT] = tangents;
- p_arr[VS::ARRAY_TEX_UV] = uvs;
- }
- void QuadMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_size", "size"), &QuadMesh::set_size);
- ClassDB::bind_method(D_METHOD("get_size"), &QuadMesh::get_size);
- ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "size"), "set_size", "get_size");
- }
- QuadMesh::QuadMesh() {
- primitive_type = PRIMITIVE_TRIANGLES;
- size = Size2(1.0, 1.0);
- }
- void QuadMesh::set_size(const Size2 &p_size) {
- size = p_size;
- _request_update();
- }
- Size2 QuadMesh::get_size() const {
- return size;
- }
- /**
- SphereMesh
- */
- void SphereMesh::_create_mesh_array(Array &p_arr) const {
- int i, j, prevrow, thisrow, point;
- float x, y, z;
- // set our bounding box
- PoolVector<Vector3> points;
- PoolVector<Vector3> normals;
- PoolVector<float> tangents;
- PoolVector<Vector2> uvs;
- PoolVector<int> indices;
- 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;
- prevrow = 0;
- for (j = 0; j <= (rings + 1); j++) {
- float v = j;
- float w;
- v /= (rings + 1);
- w = sin(Math_PI * v);
- y = height * (is_hemisphere ? 1.0 : 0.5) * cos(Math_PI * v);
- for (i = 0; i <= radial_segments; i++) {
- float u = i;
- u /= radial_segments;
- x = sin(u * (Math_PI * 2.0));
- z = cos(u * (Math_PI * 2.0));
- if (is_hemisphere && y < 0.0) {
- points.push_back(Vector3(x * radius * w, 0.0, z * radius * w));
- normals.push_back(Vector3(0.0, -1.0, 0.0));
- } else {
- Vector3 p = Vector3(x * radius * w, y, z * radius * w);
- points.push_back(p);
- normals.push_back(p.normalized());
- };
- ADD_TANGENT(z, 0.0, -x, 1.0)
- uvs.push_back(Vector2(u, v));
- point++;
- if (i > 0 && j > 0) {
- indices.push_back(prevrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i - 1);
- indices.push_back(prevrow + i);
- indices.push_back(thisrow + i);
- indices.push_back(thisrow + i - 1);
- };
- };
- prevrow = thisrow;
- thisrow = point;
- };
- p_arr[VS::ARRAY_VERTEX] = points;
- p_arr[VS::ARRAY_NORMAL] = normals;
- p_arr[VS::ARRAY_TANGENT] = tangents;
- p_arr[VS::ARRAY_TEX_UV] = uvs;
- p_arr[VS::ARRAY_INDEX] = indices;
- }
- void SphereMesh::_bind_methods() {
- ClassDB::bind_method(D_METHOD("set_radius", "radius"), &SphereMesh::set_radius);
- ClassDB::bind_method(D_METHOD("get_radius"), &SphereMesh::get_radius);
- ClassDB::bind_method(D_METHOD("set_height", "height"), &SphereMesh::set_height);
- ClassDB::bind_method(D_METHOD("get_height"), &SphereMesh::get_height);
- ClassDB::bind_method(D_METHOD("set_radial_segments", "radial_segments"), &SphereMesh::set_radial_segments);
- ClassDB::bind_method(D_METHOD("get_radial_segments"), &SphereMesh::get_radial_segments);
- ClassDB::bind_method(D_METHOD("set_rings", "rings"), &SphereMesh::set_rings);
- ClassDB::bind_method(D_METHOD("get_rings"), &SphereMesh::get_rings);
- ClassDB::bind_method(D_METHOD("set_is_hemisphere", "is_hemisphere"), &SphereMesh::set_is_hemisphere);
- ClassDB::bind_method(D_METHOD("get_is_hemisphere"), &SphereMesh::get_is_hemisphere);
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "radius", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_radius", "get_radius");
- ADD_PROPERTY(PropertyInfo(Variant::REAL, "height", PROPERTY_HINT_RANGE, "0.001,100.0,0.001,or_greater"), "set_height", "get_height");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "radial_segments", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_radial_segments", "get_radial_segments");
- ADD_PROPERTY(PropertyInfo(Variant::INT, "rings", PROPERTY_HINT_RANGE, "1,100,1,or_greater"), "set_rings", "get_rings");
- ADD_PROPERTY(PropertyInfo(Variant::BOOL, "is_hemisphere"), "set_is_hemisphere", "get_is_hemisphere");
- }
- void SphereMesh::set_radius(const float p_radius) {
- radius = p_radius;
- _request_update();
- }
- float SphereMesh::get_radius() const {
- return radius;
- }
- void SphereMesh::set_height(const float p_height) {
- height = p_height;
- _request_update();
- }
- float SphereMesh::get_height() const {
- return height;
- }
- void SphereMesh::set_radial_segments(const int p_radial_segments) {
- radial_segments = p_radial_segments > 4 ? p_radial_segments : 4;
- _request_update();
- }
- int SphereMesh::get_radial_segments() const {
- return radial_segments;
- }
- void SphereMesh::set_rings(const int p_rings) {
- rings = p_rings > 1 ? p_rings : 1;
- _request_update();
- }
- int SphereMesh::get_rings() const {
- return rings;
- }
- void SphereMesh::set_is_hemisphere(const bool p_is_hemisphere) {
- is_hemisphere = p_is_hemisphere;
- _request_update();
- }
- bool SphereMesh::get_is_hemisphere() const {
- return is_hemisphere;
- }
- SphereMesh::SphereMesh() {
- // defaults
- radius = 1.0;
- height = 2.0;
- radial_segments = 64;
- rings = 32;
- is_hemisphere = false;
- }
|