Last active
February 2, 2023 19:05
-
-
Save Kequc/464e4a9acc1e1b229c380a17d5487b2f to your computer and use it in GitHub Desktop.
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 Node | |
enum TERRAIN_SHAPE { NONE, FLAT, PEAK, DROP, SLANT } | |
const QUAD_UVS: = PoolVector2Array([Vector2(0, 0), Vector2(1, 0), Vector2(1, 1), Vector2(0, 1)]) |
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 Spatial | |
const MeshLibraryFactory = preload("res://island/mesh-library-factory.gd") | |
onready var grid: GridMap = get_node("grid") | |
func _ready(): | |
grid.mesh_library = MeshLibraryFactory.generate_terrain() | |
grid.set_cell_item(0, 0, 0, CONSTANTS.TERRAIN_SHAPE.FLAT) | |
print(grid.get_used_cells()) | |
# outputs: [(0, 0, 0)] | |
print(grid.mesh_library.get_item_list()) | |
# outputs: [1, 2, 3, 4] |
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 Resource | |
const terrain_mat = preload("res://island-scn/terrain-mat.material") | |
static func generate_terrain() -> MeshLibrary: | |
var mesh_library: = MeshLibrary.new() | |
_add_item(mesh_library, CONSTANTS.TERRAIN_SHAPE.FLAT, _flat_mesh()) | |
_add_item(mesh_library, CONSTANTS.TERRAIN_SHAPE.PEAK, _peak_mesh()) | |
_add_item(mesh_library, CONSTANTS.TERRAIN_SHAPE.DROP, _drop_mesh()) | |
_add_item(mesh_library, CONSTANTS.TERRAIN_SHAPE.SLANT, _slant_mesh()) | |
return mesh_library | |
static func _add_item(mesh_library: MeshLibrary, id: int, mesh: ArrayMesh): | |
mesh_library.create_item(id) | |
mesh_library.set_item_mesh(id, mesh) | |
const FLAT_MESH_VERTS = PoolVector3Array([ | |
Vector3(0, 1, 1), | |
Vector3(1, 1, 1), | |
Vector3(1, 1, 0), | |
Vector3(0, 1, 0), | |
]) | |
static func _flat_mesh() -> ArrayMesh: | |
var mesh: = ArrayMesh.new() | |
_add_surface(mesh, FLAT_MESH_VERTS, PoolIntArray([0, 1, 2])) | |
_add_surface(mesh, FLAT_MESH_VERTS, PoolIntArray([0, 2, 3])) | |
return mesh | |
const PEAK_MESH_VERTS: = PoolVector3Array([ | |
Vector3(0, 0, 1), | |
Vector3(1, 1, 1), | |
Vector3(1, 0, 0), | |
Vector3(0, 0, 0), | |
]) | |
static func _peak_mesh() -> ArrayMesh: | |
var mesh: = ArrayMesh.new() | |
_add_surface(mesh, PEAK_MESH_VERTS, PoolIntArray([0, 1, 2])) | |
return mesh | |
const DROP_MESH_VERTS: = PoolVector3Array([ | |
Vector3(0, 1, 1), | |
Vector3(1, 1, 1), | |
Vector3(1, 1, 0), | |
Vector3(0, 0, 0), | |
]) | |
static func _drop_mesh() -> ArrayMesh: | |
var mesh: = ArrayMesh.new() | |
_add_surface(mesh, DROP_MESH_VERTS, PoolIntArray([0, 1, 2])) | |
_add_surface(mesh, DROP_MESH_VERTS, PoolIntArray([0, 2, 3])) | |
return mesh | |
const SLANT_MESH_VERTS: = PoolVector3Array([ | |
Vector3(0, 0, 1), | |
Vector3(1, 1, 1), | |
Vector3(1, 1, 0), | |
Vector3(0, 0, 0), | |
]) | |
static func _slant_mesh() -> ArrayMesh: | |
var mesh: = ArrayMesh.new() | |
_add_surface(mesh, SLANT_MESH_VERTS, PoolIntArray([0, 1, 2, 0, 2, 3])) | |
return mesh | |
static func _add_surface(mesh: ArrayMesh, vertices: PoolVector3Array, indices: PoolIntArray): | |
var arrays = [] | |
arrays.resize(ArrayMesh.ARRAY_MAX) | |
arrays[ArrayMesh.ARRAY_VERTEX] = vertices | |
arrays[ArrayMesh.ARRAY_TEX_UV] = CONSTANTS.QUAD_UVS | |
arrays[ArrayMesh.ARRAY_INDEX] = indices | |
mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays) | |
mesh.surface_set_material(mesh.get_surface_count() - 1, terrain_mat) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment