Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// @function cube(data, i, j, k, cs)
- /// @description A struct to represent a cube in the noise array
- /// @param data a pointer to the noise array
- /// @param i coords of the cube in the noise array
- /// @param j
- /// @param k
- /// @param cs the size of the cube.
- function cube(data, i, j, k, cs) constructor{
- // this array holds the noise values at each corner of the cube
- values = [data[i ][j ][k ],
- data[i + 1][j ][k ],
- data[i + 1][j + 1][k ],
- data[i ][j + 1][k ],
- data[i ][j ][k + 1],
- data[i + 1][j ][k + 1],
- data[i + 1][j + 1][k + 1],
- data[i ][j + 1][k + 1]];
- // this array holds vectors for each corner of the cube
- points = [new vec3d((i + 0) * cs, (j + 0) * cs, (k + 0) * cs),
- new vec3d((i + 1) * cs, (j + 0) * cs, (k + 0) * cs),
- new vec3d((i + 1) * cs, (j + 1) * cs, (k + 0) * cs),
- new vec3d((i + 0) * cs, (j + 1) * cs, (k + 0) * cs),
- new vec3d((i + 0) * cs, (j + 0) * cs, (k + 1) * cs),
- new vec3d((i + 1) * cs, (j + 0) * cs, (k + 1) * cs),
- new vec3d((i + 1) * cs, (j + 1) * cs, (k + 1) * cs),
- new vec3d((i + 0) * cs, (j + 1) * cs, (k + 1) * cs)];
- }
- /// @function marcher(d, cs, iv, vb, vf)
- /// @description a struct that does the marching cube algorithm
- /// @param d the noise array
- /// @param cs the cube size
- /// @param iv the isovalue
- /// @param vb the vertex buffer
- /// @param the vertex buffer format
- function marcher(d, cs, iv, vb, vf) constructor{
- data = d;
- cube_size = cs;
- isovalue = iv;
- vbuff = vb;
- vform = vf;
- /*
- static cube_corners = [new vec3d(0, 0, 0),
- new vec3d(1, 0, 0),
- new vec3d(1, 1, 0),
- new vec3d(0, 1, 0),
- new vec3d(0, 0, 1),
- new vec3d(1, 0, 1),
- new vec3d(1, 1, 1),
- new vec3d(0, 1, 1)];
- */
- // this table holds hex values for determining which edges of the cube get intersected by triangles
- static edge_table = [0x0 , 0x109, 0x203, 0x30a, 0x406, 0x50f, 0x605, 0x70c,
- 0x80c, 0x905, 0xa0f, 0xb06, 0xc0a, 0xd03, 0xe09, 0xf00,
- 0x190, 0x99 , 0x393, 0x29a, 0x596, 0x49f, 0x795, 0x69c,
- 0x99c, 0x895, 0xb9f, 0xa96, 0xd9a, 0xc93, 0xf99, 0xe90,
- 0x230, 0x339, 0x33 , 0x13a, 0x636, 0x73f, 0x435, 0x53c,
- 0xa3c, 0xb35, 0x83f, 0x936, 0xe3a, 0xf33, 0xc39, 0xd30,
- 0x3a0, 0x2a9, 0x1a3, 0xaa , 0x7a6, 0x6af, 0x5a5, 0x4ac,
- 0xbac, 0xaa5, 0x9af, 0x8a6, 0xfaa, 0xea3, 0xda9, 0xca0,
- 0x460, 0x569, 0x663, 0x76a, 0x66 , 0x16f, 0x265, 0x36c,
- 0xc6c, 0xd65, 0xe6f, 0xf66, 0x86a, 0x963, 0xa69, 0xb60,
- 0x5f0, 0x4f9, 0x7f3, 0x6fa, 0x1f6, 0xff , 0x3f5, 0x2fc,
- 0xdfc, 0xcf5, 0xfff, 0xef6, 0x9fa, 0x8f3, 0xbf9, 0xaf0,
- 0x650, 0x759, 0x453, 0x55a, 0x256, 0x35f, 0x55 , 0x15c,
- 0xe5c, 0xf55, 0xc5f, 0xd56, 0xa5a, 0xb53, 0x859, 0x950,
- 0x7c0, 0x6c9, 0x5c3, 0x4ca, 0x3c6, 0x2cf, 0x1c5, 0xcc ,
- 0xfcc, 0xec5, 0xdcf, 0xcc6, 0xbca, 0xac3, 0x9c9, 0x8c0,
- 0x8c0, 0x9c9, 0xac3, 0xbca, 0xcc6, 0xdcf, 0xec5, 0xfcc,
- 0xcc , 0x1c5, 0x2cf, 0x3c6, 0x4ca, 0x5c3, 0x6c9, 0x7c0,
- 0x950, 0x859, 0xb53, 0xa5a, 0xd56, 0xc5f, 0xf55, 0xe5c,
- 0x15c, 0x55 , 0x35f, 0x256, 0x55a, 0x453, 0x759, 0x650,
- 0xaf0, 0xbf9, 0x8f3, 0x9fa, 0xef6, 0xfff, 0xcf5, 0xdfc,
- 0x2fc, 0x3f5, 0xff , 0x1f6, 0x6fa, 0x7f3, 0x4f9, 0x5f0,
- 0xb60, 0xa69, 0x963, 0x86a, 0xf66, 0xe6f, 0xd65, 0xc6c,
- 0x36c, 0x265, 0x16f, 0x66 , 0x76a, 0x663, 0x569, 0x460,
- 0xca0, 0xda9, 0xea3, 0xfaa, 0x8a6, 0x9af, 0xaa5, 0xbac,
- 0x4ac, 0x5a5, 0x6af, 0x7a6, 0xaa , 0x1a3, 0x2a9, 0x3a0,
- 0xd30, 0xc39, 0xf33, 0xe3a, 0x936, 0x83f, 0xb35, 0xa3c,
- 0x53c, 0x435, 0x73f, 0x636, 0x13a, 0x33 , 0x339, 0x230,
- 0xe90, 0xf99, 0xc93, 0xd9a, 0xa96, 0xb9f, 0x895, 0x99c,
- 0x69c, 0x795, 0x49f, 0x596, 0x29a, 0x393, 0x99 , 0x190,
- 0xf00, 0xe09, 0xd03, 0xc0a, 0xb06, 0xa0f, 0x905, 0x80c,
- 0x70c, 0x605, 0x50f, 0x406, 0x30a, 0x203, 0x109, 0x0 ]
- // this table is for making triangles...
- /// @function get_cube_index(c, isovalue)
- /// @description returns a number that determines what cube configuration is used
- /// @param c the cube.
- /// @param isovalue the isovalue
- static get_cube_index = function(c, isovalue){
- var index = 0;
- if(c.values[0] < isovalue) index |= 1;
- if(c.values[1] < isovalue) index |= 2;
- if(c.values[2] < isovalue) index |= 4;
- if(c.values[3] < isovalue) index |= 8;
- if(c.values[4] < isovalue) index |= 16;
- if(c.values[5] < isovalue) index |= 32;
- if(c.values[6] < isovalue) index |= 64;
- if(c.values[7] < isovalue) index |= 128;
- return index;
- }
- /// @function point_lerp(p1, p2, val1, val2, isoval)
- /// @description does a linear interpolation between 2 points in 3d space
- /// @param p1 the first point
- /// @param p2 the second point
- /// @param val1 the noise value at p1
- /// @param val2 the noise value at p2
- /// @param isoval the isovalue that we interpolate at.
- static point_lerp = function(p1, p2, val1, val2, isoval){
- var lerp_factor = (isoval - val1)/(val2 - val1);
- var temp = p2.subtract(p1);
- temp.multiply_self(lerp_factor);
- temp.add_self(p1);
- return temp;
- // use this line to get a blockier mesh
- //return p1.add(p2).multiply(0.5);
- }
- /// @function triangulate(c, isoval)
- /// @description returns a list of points representing triangles that make up each cube.
- /// @param c the cube to be triangulated
- /// @param isoval the isovalue to make triangles at.
- static triangulate = function(c, isoval){
- var index = get_cube_index(c, isoval);
- var edge_code = edge_table[index];
- var tri_code = tri_table[index];
- // create a list of vertices
- var vert_list = [];
- if(edge_code & 1){ vert_list[ 0] = point_lerp(c.points[0], c.points[1], c.values[0], c.values[1], isoval); }
- if(edge_code & 2){ vert_list[ 1] = point_lerp(c.points[1], c.points[2], c.values[1], c.values[2], isoval); }
- if(edge_code & 4){ vert_list[ 2] = point_lerp(c.points[2], c.points[3], c.values[2], c.values[3], isoval); }
- if(edge_code & 8){ vert_list[ 3] = point_lerp(c.points[3], c.points[0], c.values[3], c.values[0], isoval); }
- if(edge_code & 16){ vert_list[ 4] = point_lerp(c.points[4], c.points[5], c.values[4], c.values[5], isoval); }
- if(edge_code & 32){ vert_list[ 5] = point_lerp(c.points[5], c.points[6], c.values[5], c.values[6], isoval); }
- if(edge_code & 64){ vert_list[ 6] = point_lerp(c.points[6], c.points[7], c.values[6], c.values[7], isoval); }
- if(edge_code & 128){ vert_list[ 7] = point_lerp(c.points[7], c.points[4], c.values[7], c.values[4], isoval); }
- if(edge_code & 256){ vert_list[ 8] = point_lerp(c.points[0], c.points[4], c.values[0], c.values[4], isoval); }
- if(edge_code & 512){ vert_list[ 9] = point_lerp(c.points[1], c.points[5], c.values[1], c.values[5], isoval); }
- if(edge_code & 1024){ vert_list[10] = point_lerp(c.points[2], c.points[6], c.values[2], c.values[6], isoval); }
- if(edge_code & 2048){ vert_list[11] = point_lerp(c.points[3], c.points[7], c.values[3], c.values[7], isoval); }
- // create a list of triangles
- var tri_list = [];
- for(var i = 0; tri_code[i] != -1; i += 3){
- tri_list[i div 3] = [vert_list[tri_code[i]], vert_list[tri_code[i + 1]], vert_list[tri_code[i + 2]]];
- }
- return tri_list;
- }
- /// @function march()
- /// @description run the marching cubes algorithm
- static march = function(){
- // get the dimensions of the noise array
- var cubes_wide = array_length(data) - 1;
- var cubes_long = array_length(data[0]) - 1;
- var cubes_high = array_length(data[0][0]) - 1;
- // start the vertex buffer
- vertex_begin(vbuff, vform);
- // iterate through the noise array
- for(var i = 0; i < cubes_wide; i++){
- for(var j = 0; j < cubes_long; j++){
- for(var k = 0; k < cubes_high; k++){
- // create a cube at the current position in the noise array
- var c = new cube(data, i, j, k, cube_size);
- // get a triangle list for the cube
- var t_list = triangulate(c, isovalue);
- // if there are triangles, add them to the vertex buffer
- if(array_length(t_list) > 0){
- for(var t = 0; t < array_length(t_list); t++){
- vertex_position_3d(vbuff, t_list[t][0].X, t_list[t][0].Y, t_list[t][0].Z);
- vertex_color(vbuff, c_yellow, 1);
- vertex_texcoord(vbuff, 0, 0);
- vertex_position_3d(vbuff, t_list[t][1].X, t_list[t][1].Y, t_list[t][1].Z);
- vertex_color(vbuff, c_red, 1);
- vertex_texcoord(vbuff, 0, 0);
- vertex_position_3d(vbuff, t_list[t][2].X, t_list[t][2].Y, t_list[t][2].Z);
- vertex_color(vbuff, c_green, 1);
- vertex_texcoord(vbuff, 0, 0);
- }
- }
- }
- }
- }
- vertex_end(vbuff);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement