Hi. I am attempting to create a realtime lighting solution for my game. So far, I have been able to identify all the edges of my objects and have stored them in a list. I am using vertex buffers to store primitives that represent the shadows cast by these edges, but none appear to be drawn to the screen. It is partially following the Realtime 2D Lighting in Gamemaker Studio yoyogames blog post from a few years back.
EDIT: I have another object drawing the edges on screen for debugging purposes. I know they are correct.
This is the code for the light object:
Create:
Draw:
ProjectShadow script function:
Thank you in advance.
EDIT: I have another object drawing the edges on screen for debugging purposes. I know they are correct.
This is the code for the light object:
Create:
GML:
/// @desc
vertex_format_begin();
vertex_format_add_position();
vertex_format_add_color();
vFormat = vertex_format_end();
vBuffer = vertex_create_buffer();
lx = x;
ly = y;
GML:
/// @desc
vertex_begin(vBuffer,vFormat);
for(var i=0;i<ds_list_size(obj_lightingManager.edges);i++) {
ProjectShadow(vBuffer, obj_lightingManager.edges[| i],lx,ly);
}
vertex_end(vBuffer);
vertex_submit(vBuffer,pr_trianglelist,-1);
GML:
function ProjectShadow(_vBuffer, _edge, _Lx, _Ly) {
var _Ax = _edge[0,0];
var _Ay = _edge[0,1];
var _Bx = _edge[0,0];
var _By = _edge[0,1];
// shadows are infinite - almost, just enough to go off screen
var SHADOW_LENGTH = 20000;
var Adx,Ady,Bdx,Bdy,len
// get unit length to point 1
Adx = _Ax-_Lx;
Ady = _Ay-_Ly;
len = (1.0*SHADOW_LENGTH)/sqrt( (Adx*Adx)+(Ady*Ady) ); // unit length scaler * Shadow length
Adx = _Ax + Adx * len;
Ady = _Ay + Ady * len;
// get unit length to point 2
Bdx = _Bx-_Lx;
Bdy = _By-_Ly;
len = (1.0*SHADOW_LENGTH) / sqrt( (Bdx*Bdx)+(Bdy*Bdy) ); // unit length scaler * Shadow length
Bdx = _Bx + Bdx * len;
Bdy = _By + Bdy * len;
// now build a quad
vertex_position(_vBuffer, _Ax,_Ay);
vertex_argb(_vBuffer, $ff000000);
vertex_position(_vBuffer, _Bx,_By);
vertex_argb(_vBuffer, $ff000000);
vertex_position(_vBuffer, Adx,Ady);
vertex_argb(_vBuffer, $ff000000);
vertex_position(_vBuffer, _Bx,_By);
vertex_argb(_vBuffer, $ff000000);
vertex_position(_vBuffer, Adx,Ady);
vertex_argb(_vBuffer, $ff000000);
vertex_position(_vBuffer, Bdx,Bdy);
vertex_argb(_vBuffer, $ff000000);
}
Last edited: