Created
March 29, 2021 23:46
-
-
Save DragoniteSpam/43e5908557e80c0686f09d82cb832b18 to your computer and use it in GitHub Desktop.
convert a game maker tilemap to a vertex buffer
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
function tilemap_to_vertex_buffer(tilemap, format) { | |
// vertex format: position, normal, texture | |
var vbuff = vertex_create_buffer(); | |
vertex_begin(vbuff, format); | |
var ts_tileset = tilemap_get_tileset(tilemap); | |
var ts_tex = tileset_get_texture(ts_tileset); | |
var ts_uvs = tileset_get_uvs(ts_tileset); | |
var ts_tex_width = texture_get_texel_width(ts_tex); | |
var ts_tex_height = texture_get_texel_height(ts_tex); | |
var ts_tile_width = tilemap_get_tile_width(tilemap); | |
var ts_tile_height = tilemap_get_tile_height(tilemap); | |
var ts_tex_tile_width = ts_tile_width * ts_tex_width; | |
var ts_tex_tile_height = ts_tile_height * ts_tex_height; | |
var ts_horizontal_count = (ts_uvs[2] - ts_uvs[0]) / ts_tex_tile_width; | |
for (var i = 0; i < 10; i++) { | |
for (var j = 0; j < 10; j++) { | |
var index = tile_get_index(tilemap_get(tilemap, i, j)); | |
var index_x = index % ts_horizontal_count; | |
var index_y = index div ts_horizontal_count; | |
var p1u = ts_uvs[0] + ts_tex_tile_width * index_x; | |
var p1v = ts_uvs[1] + ts_tex_tile_height * index_y; | |
var p2u = ts_uvs[0] + ts_tex_tile_width * (index_x + 1); | |
var p2v = ts_uvs[1] + ts_tex_tile_height * index_y; | |
var p3u = ts_uvs[0] + ts_tex_tile_width * (index_x + 1); | |
var p3v = ts_uvs[1] + ts_tex_tile_height * (index_y + 1); | |
var p4u = ts_uvs[0] + ts_tex_tile_width * index_x; | |
var p4v = ts_uvs[1] + ts_tex_tile_height * (index_y + 1); | |
vertex_position_3d(vbuff, 16 * i, 16 * j, 0); | |
vertex_normal(vbuff, 0, 0, 1); | |
vertex_texcoord(vbuff, p1u, p1v); | |
vertex_position_3d(vbuff, 16 * (i + 1), 16 * j, 0); | |
vertex_normal(vbuff, 0, 0, 1); | |
vertex_texcoord(vbuff, p2u, p2v); | |
vertex_position_3d(vbuff, 16 * (i + 1), 16 * (j + 1), 0); | |
vertex_normal(vbuff, 0, 0, 1); | |
vertex_texcoord(vbuff, p3u, p3v); | |
vertex_position_3d(vbuff, 16 * (i + 1), 16 * (j + 1), 0); | |
vertex_normal(vbuff, 0, 0, 1); | |
vertex_texcoord(vbuff, p3u, p3v); | |
vertex_position_3d(vbuff, 16 * i, 16 * (j + 1), 0); | |
vertex_normal(vbuff, 0, 0, 1); | |
vertex_texcoord(vbuff, p4u, p4v); | |
vertex_position_3d(vbuff, 16 * i, 16 * j, 0); | |
vertex_normal(vbuff, 0, 0, 1); | |
vertex_texcoord(vbuff, p1u, p1v); | |
} | |
} | |
vertex_end(vbuff); | |
vertex_freeze(vbuff); | |
layer_destroy(tilemap); | |
return vbuff; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment