Jump to content


Photo

3D Starfield


  • Please log in to reply
6 replies to this topic

#1 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 12 April 2012 - 10:19 AM

Hey all,

I found this example, and it created a very nice star field. Unfortunately, it used objects which isn't terribly efficient. So, I modified the example in order for it to use an array to draw sprites. The resulting speed increase was roughly 40-60 fps. I then modified it further to separate idle and moving stars. The resulting speed increase from that was roughly 10 fps.

However, for use in the game, at a room speed of 1000; using the required number of stars, I get a fps of about 400. So, I'm hoping someone could help me optimise the current system, or provide a faster alternative.

The current system is...

Create Event:
/*
**  Star Array...
**
**  star[x,y]
**    y...
**      0 - x
**      1 - y
**      2 - amounttomove
**      3 - alpha
*/
numbOfIdleStars=0;
numbOfStars=0;

repeat(50) {
    //create star...
    idleStars[numbOfIdleStars,0]=random(view_wview[0]);
    idleStars[numbOfIdleStars,1]=random(view_hview[0]);
    idleStars[numbOfIdleStars,2]=random(0.8)+0.2;
    numbOfIdleStars+=1;
}

repeat(300) {
    //create star...
    stars[numbOfStars,0]=random(room_width);
    stars[numbOfStars,1]=random(room_height);
    stars[numbOfStars,2]=random(3000);
    stars[numbOfStars,3]=random(0.6)+0.4;
    numbOfStars+=1;
}

Draw Event:
//draw stars...
var i,xx,yy,sx,sy;
i=0;
xx=view_xview[0]/room_width;
yy=view_yview[0]/room_height;

repeat (numbOfIdleStars) {
    draw_sprite_ext(sprStar,0,view_xview[0]+idleStars[i,0],view_yview[0]+idleStars[i,1],1,1,0,c_white,idleStars[i,2]);
    i+=1;
}
i=0;
repeat (numbOfStars) {
    draw_sprite_ext(sprStar,0,view_xview[0]+stars[i,0]-(stars[i,2]*xx),view_yview[0]+stars[i,1]-(stars[i,2]*yy),1,1,0,c_white,stars[i,3]);
    i+=1;
}

Thanks to any who can help.

Edited by MasterOfKings, 12 April 2012 - 02:36 PM.

  • 0

#2 TheSnidr

TheSnidr

    That guy

  • Global Moderators
  • 2438 posts
  • Version:GM:Studio

Posted 12 April 2012 - 11:09 AM

How about, instead of drawing individual stars, drawing layers of stars?
  • 0

#3 Phantom107

Phantom107

    Engineer

  • GMC Member
  • 2595 posts
  • Version:GM:Studio

Posted 12 April 2012 - 11:12 AM

What I'm going to propose will only allow "stars" of 1*1 px, but it is blazingly fast, and most likely the superior method to achieve this.

Create some 3d models that each have a pointlist primitive. You can draw these in a 2D game just fine, even without calling any projection code. There is no need to call d3d_start, 3D models will work just fine as GM is running in 3D mode under the hood anyway.

This has a lot of benefits:
- The dots renders insanely fast because they're in contained in a primitive. This means extremely few draw calls, and no extreme drawing loops every frame!
- You can have multiple models and mess with their alpha on the fly... this means you can make the starfield 'glitter' a bit.

Here is some code I've typed up for a static starfield. You're smart enough to make this dynamic, so I'll just put down the basics.

Start a new GMK, put an object in a blank room. Put room size to 800 x 600 or something.

Create event:

starfield = d3d_model_create();
d3d_model_primitive_begin(starfield, pr_pointlist);

// Create one thousand stars
repeat 1000
begin
    d3d_model_vertex_color(starfield, random(room_width), random(room_height), 0, c_white, 0.2+random(0.8));
end;

d3d_model_primitive_end(starfield);

Draw event:

draw_clear(0);
draw_set_color(c_white);
d3d_model_draw(starfield, 0, 0, 0, -1);

Edit, added screenshot:

Posted Image

Edited by Phantom107, 12 April 2012 - 11:15 AM.

  • 1

#4 Tepi

Tepi

    GMC Member

  • Global Moderators
  • 4201 posts
  • Version:GM8.1

Posted 12 April 2012 - 02:44 PM

Here is an example for creating the stars as triangles of different size. You can even generate them based on real star sky data (positions, sizes, colors), and add mipmapping to make them appear realistically even when zoomed in (yes, I made an unreleased night sky simulator years ago using this method).

You might think this would be slow, but in fact it is surprisingly fast.
  • 1

#5 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 12 April 2012 - 03:21 PM

How about, instead of drawing individual stars, drawing layers of stars?

By "layers", what do you mean exactly?


What I'm going to propose will only allow "stars" of 1*1 px, but it is blazingly fast, and most likely the superior method to achieve this.

Create some 3d models that each have a pointlist primitive. You can draw these in a 2D game just fine, even without calling any projection code. There is no need to call d3d_start, 3D models will work just fine as GM is running in 3D mode under the hood anyway.

This has a lot of benefits:
- The dots renders insanely fast because they're in contained in a primitive. This means extremely few draw calls, and no extreme drawing loops every frame!
- You can have multiple models and mess with their alpha on the fly... this means you can make the starfield 'glitter' a bit.

Here is some code I've typed up for a static starfield. You're smart enough to make this dynamic, so I'll just put down the basics.

Start a new GMK, put an object in a blank room. Put room size to 800 x 600 or something.

Create event:

starfield = d3d_model_create();
d3d_model_primitive_begin(starfield, pr_pointlist);

// Create one thousand stars
repeat 1000
begin
    d3d_model_vertex_color(starfield, random(room_width), random(room_height), 0, c_white, 0.2+random(0.8));
end;

d3d_model_primitive_end(starfield);

Draw event:

draw_clear(0);
draw_set_color(c_white);
d3d_model_draw(starfield, 0, 0, 0, -1);

<omitted screenshot>

Thanks. I added in the static starfield, and set up the dynamic one. Now, the fps is roughly 600 (+200).
Thanks a lot. :D

Edited by MasterOfKings, 12 April 2012 - 03:21 PM.

  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14391 posts
  • Version:GM:Studio

Posted 12 April 2012 - 05:05 PM

3d models. Here
http://gmc.yoyogames...howtopic=450070
  • 1

#7 MasterOfKings

MasterOfKings

    The True Master

  • GMC Member
  • 4888 posts
  • Version:GM8

Posted 25 April 2012 - 01:58 PM

Tepi, I'm sorry, I didn't even notice your reply.

I've had a play around with it, and it both looks good, and performances amazingly. Thanks a lot, guys. Especially icuurd.

PS: +1 for all of you. Including Phantom.

Edited by MasterOfKings, 25 April 2012 - 01:59 PM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users