Created
September 25, 2020 08:16
-
-
Save q8f13/6da5ad539eab13db0989b4e734ad0070 to your computer and use it in GitHub Desktop.
generate mesh via code in gdscript example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends MeshInstance | |
""" | |
Example: | |
godot arrayMesh generate a box geometry | |
""" | |
var rings = 50 | |
var radial_segments = 50 | |
var radius = 1 | |
export(float) var width_fwd = 1 | |
export(float) var width_back = 1 | |
export(float) var height_fwd = 1 | |
export(float) var height_back = 1 | |
export(float) var depth_left = 0.5 | |
export(float) var depth_right = 0.5 | |
# Called when the node enters the scene tree for the first time. | |
func _ready(): | |
var arr = [] | |
arr.resize(Mesh.ARRAY_MAX) | |
var verts = PoolVector3Array() | |
var uvs = PoolVector2Array() | |
var normals = PoolVector3Array() | |
var indices = PoolIntArray() | |
# generate a sphere | |
""" | |
# Vertex indices. | |
var thisrow = 0 | |
var prevrow = 0 | |
var point = 0 | |
# Loop over rings. | |
for i in range(rings + 1): | |
var v = float(i) / rings | |
var w = sin(PI * v) | |
var y = cos(PI * v) | |
# Loop over segments in ring. | |
for j in range(radial_segments): | |
var u = float(j) / radial_segments | |
var x = sin(u * PI * 2.0) | |
var z = cos(u * PI * 2.0) | |
var vert = Vector3(x * radius * w, y, z * radius * w) | |
verts.append(vert) | |
normals.append(vert.normalized()) | |
uvs.append(Vector2(u, v)) | |
point += 1 | |
# Create triangles in ring using indices. | |
if i > 0 and j > 0: | |
indices.append(prevrow + j - 1) | |
indices.append(prevrow + j) | |
indices.append(thisrow + j - 1) | |
indices.append(prevrow + j) | |
indices.append(thisrow + j) | |
indices.append(thisrow + j - 1) | |
if i > 0: | |
indices.append(prevrow + radial_segments - 1) | |
indices.append(prevrow) | |
indices.append(thisrow + radial_segments - 1) | |
indices.append(prevrow) | |
indices.append(prevrow + radial_segments) | |
indices.append(thisrow + radial_segments - 1) | |
prevrow = thisrow | |
thisrow = point | |
""" | |
var vts = [ | |
Vector3.ZERO, | |
Vector3(width_fwd, 0, 0), | |
Vector3(0, height_fwd, 0), | |
Vector3(width_fwd, height_fwd, 0), | |
Vector3(0, 0, -depth_left), | |
Vector3(width_back, 0, -depth_right), | |
Vector3(0, height_back, -depth_left), | |
Vector3(width_back, height_back, -depth_right) | |
] | |
verts.append_array(vts) | |
var faces = 6 | |
for n in range(0, faces): | |
uvs.append_array([ Vector2.ZERO, Vector2(0, 1), Vector2(1,0), Vector2(0,1), Vector2(1,1), Vector2(1,0) ]) | |
normals.append_array([ | |
Vector3.FORWARD,Vector3.FORWARD,Vector3.FORWARD,Vector3.FORWARD,Vector3.FORWARD,Vector3.FORWARD, | |
-Vector3.RIGHT,-Vector3.RIGHT,-Vector3.RIGHT,-Vector3.RIGHT,-Vector3.RIGHT,-Vector3.RIGHT, | |
Vector3.RIGHT,Vector3.RIGHT,Vector3.RIGHT,Vector3.RIGHT,Vector3.RIGHT,Vector3.RIGHT, | |
Vector3.UP, Vector3.UP, Vector3.UP, Vector3.UP, Vector3.UP, Vector3.UP, | |
-Vector3.UP, -Vector3.UP, -Vector3.UP, -Vector3.UP, -Vector3.UP, -Vector3.UP, | |
-Vector3.FORWARD,-Vector3.FORWARD,-Vector3.FORWARD,-Vector3.FORWARD,-Vector3.FORWARD,-Vector3.FORWARD | |
]) | |
# indices.append_array([0,2,1,2,3,1]) | |
indices.append_array([0,2,1,2,3,1, 4,6,0,6,2,0]) | |
indices.append_array([1,3,5,3,7,5, 2,6,3,6,7,3]) | |
indices.append_array([4,0,5,0,1,5, 5,7,4,7,6,4]) | |
# indices.append_array([0,1,2,3,4,5]) | |
verts.resize(indices.size()) | |
# print('indices: %s, verts: %s' % indices.size(), verts.size()) | |
arr[Mesh.ARRAY_VERTEX] = verts | |
arr[Mesh.ARRAY_TEX_UV] = uvs | |
arr[Mesh.ARRAY_NORMAL] = normals | |
arr[Mesh.ARRAY_INDEX] = indices | |
mesh = ArrayMesh.new() | |
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arr) | |
pass # Replace with function body. | |
# Called every frame. 'delta' is the elapsed time since the previous frame. | |
#func _process(delta): | |
# pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment