Author Topic: Cloud Generation  (Read 2022 times)

JackOatley

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 731
  • Nobody
    • View Profile
Cloud Generation
« on: December 23, 2013, 11:32:49 AM »
So I thought I'd share the cloud generation from the new game, it's pretty simple so if you wanna put it in Legacy you can! :) Will update this if I've left anything important out, but this whole code should give you a cloud model!


SETUP

So first, we set up a heightmap to generate the clouds shape:
Code: [Select]
// Set cloud dimensions
width  = irandom_range(24, 64);
height = irandom_range(24, 64);
deep   = irandom_range(8, 16);

// Create heightmap for creating random cloud
heightmap = ds_grid_create(width, height);
ds_grid_clear(heightmap, 0);

// Randomly add circles with increasing radii
repeat (irandom_range(3, 20))
    {
    xx = irandom_range(3, width-4);
    yy = irandom_range(3, height-4);
    for (n=0; n<irandom_range(1, deep-1); n++)
        {
        ds_grid_add_disk(heightmap, xx, yy, n, 1);
        xx += irandom_range(-1, 1)
        yy += irandom_range(-1, 1)
        }
    }


MODELLING

And then we create the mesh. So this is written in GM: Studio, for porting it to 8.1; buffer is basically a d3d_model. We model it as a pr_trianglelist. It's practically the same with a little translation:

Code: [Select]
vertex_position_3d(buffer, xx, yy, topZ);
vertex_colour(buffer, $ffffff, 1.0);
can be translated to;
Code: [Select]
d3d_model_vertex_color(buffer, xx, yy, topZ, $ffffff, 1.0);
Modelling code:
Code: [Select]
// Create mesh from heightmap
buffer = vertex_create_buffer();
vertex_begin(buffer, global.cloudFormat);
polys = 0;

for (xx=0; xx<width; xx++)
    {
    for (yy=0; yy<height; yy++)
        {
        value = ds_grid_get(heightmap, xx, yy);
        bottomZ = round((deep/2)-min(deep-1, value)/4);
        topZ = round((deep/2)+min(deep-1, value)/2);
           
        if (topZ - bottomZ >= 1)
            {
            // Top
            vertex_position_3d(buffer, xx, yy, topZ);  vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx, yy+1, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx, yy+1, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, topZ); vertex_colour(buffer, $ffffff, 1.0);
           
            // Bottom
            vertex_position_3d(buffer, xx, yy, bottomZ);  vertex_colour(buffer, $bbbbbb, 1.0);
            vertex_position_3d(buffer, xx, yy+1, bottomZ); vertex_colour(buffer, $bbbbbb, 1.0);
            vertex_position_3d(buffer, xx+1, yy, bottomZ); vertex_colour(buffer, $bbbbbb, 1.0);
            vertex_position_3d(buffer, xx+1, yy, bottomZ); vertex_colour(buffer, $bbbbbb, 1.0);
            vertex_position_3d(buffer, xx, yy+1, bottomZ); vertex_colour(buffer, $bbbbbb, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, bottomZ); vertex_colour(buffer, $bbbbbb, 1.0);
           
            // Sides
            vertex_position_3d(buffer, xx+1, yy, topZ);  vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy, bottomZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy, bottomZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, bottomZ); vertex_colour(buffer, $ffffff, 1.0);
           
            vertex_position_3d(buffer, xx, yy+1, topZ);  vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx, yy+1, bottomZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx, yy+1, bottomZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, topZ); vertex_colour(buffer, $ffffff, 1.0);
            vertex_position_3d(buffer, xx+1, yy+1, bottomZ); vertex_colour(buffer, $ffffff, 1.0);
           
            vertex_position_3d(buffer, xx, yy, topZ);  vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx, yy+1, topZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx, yy, bottomZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx, yy, bottomZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx, yy+1, topZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx, yy+1, bottomZ); vertex_colour(buffer, $dddddd, 1.0);
           
            vertex_position_3d(buffer, xx, yy, topZ);  vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx, yy, bottomZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx+1, yy, topZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx+1, yy, topZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx, yy, bottomZ); vertex_colour(buffer, $dddddd, 1.0);
            vertex_position_3d(buffer, xx+1, yy, bottomZ); vertex_colour(buffer, $dddddd, 1.0);
           
            polys += 12;
            }
        }
    }
   
vertex_end(buffer);


FINISH UP

Now we check that polys were actually added, if not then we destroy the cloud. You can remove the freeze bit, in Studio this gives the buffer to the graphics card making it draw super fast. But at this point, your new cloud should be ready to draw!!! :)
Code: [Select]
// Free up the data
ds_grid_destroy(heightmap);

// Check the buffer isn't empty, if it is then destroy it, else freeze it
if (polys == 0)
    {
    vertex_delete_buffer(buffer);
    instance_destroy();
    }
else
    {
    vertex_freeze(buffer);
    }
« Last Edit: December 23, 2013, 11:34:23 AM by JackOatley »

trg601

  • Hero Member
  • *****
  • Posts: 841
  • Just a guy
    • View Profile
    • Mutant Brain Games
Re: Cloud Generation
« Reply #1 on: December 23, 2013, 03:15:22 PM »
You might see that in the next version of 1.5 :D
I've made some stuff for terrablox, but I'm too lazy to link to much of it.
Terrablox Solder
Join the Terrablox Chatroom!

Terrabloxian

  • Hero Member
  • *****
  • Posts: 633
  • Drop it like it's hot
    • View Profile
Re: Cloud Generation
« Reply #2 on: December 23, 2013, 04:16:52 PM »
Awesome! Idk about addin this to 1.5.2, but 1.6 I think should definately have this! Thanks Mr. Oatley!
(Only some will know what I'm talking about)
Say nope to dope!

Guma

  • Global Moderator
  • Sr. Member
  • *****
  • Posts: 293
  • [INSERT SOME "INTELLIGENT" QUOTE HERE]
    • View Profile
Re: Cloud Generation
« Reply #3 on: December 23, 2013, 06:13:21 PM »
Agree with Terrabloxian.


That's useful Jack!
Thanks ;)


I might try modding it a little so it doesn't look as similar to the new Terrablox, people might get confused lol! :D
> learning web database programming D:

trg601

  • Hero Member
  • *****
  • Posts: 841
  • Just a guy
    • View Profile
    • Mutant Brain Games
Re: Cloud Generation
« Reply #4 on: December 24, 2013, 09:19:13 PM »
I might try modding it a little so it doesn't look as similar to the new Terrablox, people might get confused lol! :D
That would be nice,
Well, we would also need random lightning strikes when we do that...

AND SHARKNADOS!
I've made some stuff for terrablox, but I'm too lazy to link to much of it.
Terrablox Solder
Join the Terrablox Chatroom!

Terrabloxian

  • Hero Member
  • *****
  • Posts: 633
  • Drop it like it's hot
    • View Profile
Re: Cloud Generation
« Reply #5 on: December 24, 2013, 09:25:03 PM »
I might try modding it a little so it doesn't look as similar to the new Terrablox, people might get confused lol! :D
That would be nice,
Well, we would also need random lightning strikes when we do that...

AND SHARKNADOS!

lol wtf. Well It'll be good for Guma do do something with legacy again. Perhaps you can be the one to implement this?
Say nope to dope!