GM 3D lighting

Posted by death on Dec. 15, 2011, 8:52 p.m.

I have always had trouble trying to get the lighting in GM to look playable… and because i always failed to get any good results i have always attempted to make my own simply solutions to these problems. here's a screenshot of what i'm talking about:

this is a little terrain generator i'm working on for my 3drpg. (been going for over a year now and still rewriting everything almost monthly lol)

i've had some good lighting systems but this is the first time i'm using slopy terrains and what not so i had to make a new shading method. it's very simple, thus it's not the most realistic but you gotta admit, it looks loads better than GM lighting.

If anyone has any tips for 3D lighting in Game Maker, i would really appreciate it =)

<p.s. just how is the directional lighting supposed to work? i either get it completely lite or completely black no matter what i try >

Comments

Gordy 12 years, 11 months ago

i'll assume your setting the darkness of a vertex according to how high it is base 0?

try setting the darkness to its height relative to its neighbors, i've done it before and it looks alright.

also, adding a bit of darkness based on its height to a single neighbor will give it a "directional light" feel.

fake ray casting can be done the same way, assuming your vertices are evenly spaced on the x,y axis, you can check if a vertex is higher or lower than line of vertices in a single direction.

overall looks good.

m3k3 12 years, 11 months ago

Read about (if possible in this order) surface normals, diffuse lighting, gouraud lighting model. Done? Now you know what lights do and how they work in game maker.

Edit: your directional light looks like crap because your terrain lacks normal information.

What you'd want for a terrain is a light gray/yellowish direction light and a dark blue ambient light.

Gordy 12 years, 11 months ago

it's unfortunate that if you want to control the lighting in gamemaker yourself, you have to do it on a per triangle, or per vertex basis, which in the end, is quite slow; and as m3k3 points out, there's a lot of math involved.

however, it is possible in small scale.

this is what the last method i suggested can look like: (comes from something i made a while ago)

screenshot

it's a pretty neat effect.

m3k3 12 years, 11 months ago

You only have to do the per vertex thing once and store it as a display list. From there on it's just 1 call from game maker and your gpu does the rest. Result is just as fast as in any 3d app.

Yeah in 3d there is a lot more math involved. That's true. Calculating normals in game maker would be a pain. For terrain you can approximate by using the height difference between neighboring vertexes. This results in a much simpler formula. Something like this with aa few tweaks might work: dx=(height(x2,y1)-height(x1,y1))/abs(x2-x1); normal_z=1-(dx+dy)*0.5; normal_x=dx; normal_y=dy //dy is same as dx just with y instead; xy plane would be a flat terran; positive z is the height; height(x,y) is a height function that returns the height at x,y point. (or just use a 2d array)

Gordy 12 years, 11 months ago

ahh yes, or saving it as a model and drawing it would also increase speed tremendously.

if the height of the terrain, and the shadow "cast" upon it is never going to change, this would work very well.

my statement about the speed only comes into concern if the terrain is too complex and you have to draw it all, or if you want the shadows to be realtime.

death 12 years, 11 months ago

@Gordy: hmm that does look quite nice. I have defined the normals but i didn't define them any different than the usual flat values. unfortunately i have never been in a good math class that would deal with 3d geometry. (im a highschool drop out) however i am in college and my degree requires some advanced math courses so hopefully in the future i'll know what the hell your talking about xD

Gordy 12 years, 11 months ago

haha, well, if you not looking for anything crazy advanced, like raytracing, then math really isn't necessary to get the effect.

(was trying to write some pseudo code for demonstration, but gave up and just made it)

just wrote this up; it's very primitive, but i hope it gives you an idea of what i was talking about.

linky

edit: this is the gmk from the screenshot i made earlier. (WARNING: this is not optimized, and was never meant to be public, so, i apologize if you puke after seeing my code) (arrow keys to move (it will error if you go off the terrain)) (also, it freezes for a few seconds when it starts up, so, give it time)

linky2

also, i'm a high school dropout as well (for the longest time, i hated math; only now starting to get into it for 3d reasons), and hearing that you're in college gives me hope if my game career doesn't work out.

death 12 years, 11 months ago

thanks for the links, i'll take all the help i can get. i'll check em out when i can :D