• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Drawing 2D background behind 3D camera setup

RoninKrang

Member
Hello, I am trying draw a 2D background behind everything and then use a 3D camera to display things in a pseudo 2.5D fashion. I am getting this.

1632168865577.png

Code for objCamera3D, If something looks like an undefined variable it's likely a macro I have setup. like camera_width = camera_get_view_width(camera[0]) etc.

// Create Event

GML:
camera_distance = 300;
camera_FOV        = 90;

pause_follow    = false;
follow            = noone;

x_start = x;
y_start = y;

globalvar X_UP; X_UP = 1;
globalvar Y_UP; Y_UP = 1;

gpu_set_ztestenable(true);
gpu_set_alphatestenable(true);
gpu_set_texrepeat(true);

space_x = 0;
space_y = 0;
space_direction = 225;

seed = (irandom(899999) + 100000) / 100000;

cloud_scale = 0.2;

surface_clouds    = noone;
surface_blank    = noone;
// Inherited End Step. This is what my normal objCamera does I use it so I can get the camera width/height etc, supposedly in it's 2D form

GML:
var _window = window_get_size(browser);

// We set the resolution once every time the camera is created or anytime the player resizes the window.

if (update_resolution or (window_has_focus() and (window[w] != _window[w] or window[h] != _window[h]))) {

    update_resolution = false;
   
    // Ensures the window can't go too big or too small while updating the current window width and height
   
    window[w] = max(_window[w], window_min_w);
    window[h] = max(_window[h], window_min_h);
   
    // Set window size if not fullscreen to ensure the width/height will reset to above min/max settings.
   
    if (!window_get_fullscreen()) {
       
        window_set_size(window[w], window[h]);
       
        global.gamedata[? "window width"]    = window[w];
        global.gamedata[? "window height"]    = window[h];
        save
       
        if (!browser) {
       
            window_set_position((display_get_width() - window[w]) / 2, (display_get_height() - window[h]) / 2);
        }
    }
   
    var _div = max(min(window[w] div width, window[h] div height), 1);
       
    var _view_w = round(clamp(window[w] / _div, width, height * (18.5 / 9)));    // 2.055 widscreen cap
    var _view_h = round(clamp(window[h] / _div, height, width / (4 / 3)));        // Adjusts to 1.333 for ipads etc.
   
    surface_resize(application_surface, window[w], window[h]);
    display_set_gui_size(_view_w, _view_h);
   
    camera_set_view_size(view_camera[0], _view_w, _view_h);
}

camera_set_view_pos(
    view_camera[0],
    clamp(x - camera_width / 2, 0, room_width - camera_width),
    clamp(y - camera_height / 2, 0, room_height - camera_height)
);
// Draw Begin Event

GML:
// Background

var _width    = ceil(camera_width * cloud_scale);
var _height    = ceil(camera_height * cloud_scale);

surface_clouds    = surface_sync(surface_clouds, _width, _height);
surface_blank    = surface_sync(surface_blank, _width, _height);

surface_set_target(surface_clouds);

// Background Noise

var shader            = shSpaceBackgroundNoise;
var shader_seed     = shader_get_uniform(shader, "seed");
var shader_position = shader_get_uniform(shader, "pos");

shader_set(shader);

shader_set_uniform_f(shader_seed, seed);
shader_set_uniform_f(shader_position, space_x / (50 * sign(-X_UP)), space_y / (50 * sign(-Y_UP)));

draw_surface(surface_blank, 0, 0);

shader_reset();
   
// Gas Clouds

var shader            = shSpaceClouds;
var shader_seed     = shader_get_uniform(shader, "seed");
var shader_position = shader_get_uniform(shader, "pos");

shader_set(shader);

shader_set_uniform_f(shader_seed, seed);
shader_set_uniform_f(shader_position, space_x / (40 * sign(-X_UP)), space_y / (40 * sign(-Y_UP)));

draw_surface(surface_blank, 0, 0);

shader_reset();

surface_reset_target();

// Draw Clouds

gpu_set_texfilter(true);
draw_surface_stretched(surface_clouds, camera_x, camera_y, camera_width, camera_height);

// Draw Stars

var scale = 2
while(scale > 1.5) {
   
    var _scale_xy = (5 * (scale / 2));
    var _scale_wh = (2 - scale) + 0.25;  
   
    var _scale_size = sprite_w(bgStars) * _scale_wh; // width and height are both 1024
   
    for (var ww = 0; ww < ceil(camera_width div _scale_size) + 3; ww++) {
        for (var hh = 0; hh < ceil(camera_height div _scale_size) + 3; hh++) {
       
            var _left_x = camera_x - _scale_size + ((space_x / _scale_xy) mod _scale_size);
            var _top_y = camera_y - _scale_size - ((space_y / _scale_xy) mod _scale_size);
       
            draw_sprite_ext(bgStars, 0, _left_x + ww * _scale_size, _top_y + hh * _scale_size, _scale_wh, _scale_wh, 0, c_white, 2 / scale);
        }
    }
   
    scale -= 0.2;
}

gpu_set_texfilter(false);

// 3D Camera

var _tilt    = 360 - (90 * 0.25);    // Top Down 3/4 view.
var _angle    = 360 - 45;                // isometric view

var _y_distance = lengthdir_y(camera_distance, _tilt);

var _from_x = x + lengthdir_x(_y_distance, _angle);
var _from_y = y + lengthdir_y(_y_distance, _angle);
var _from_z = ship_floor - sqrt(power(camera_distance, 2) + power(_y_distance, 2));

var _matrix_view = matrix_build_lookat(_from_x, _from_y, _from_z, x, y, ship_floor, X_UP, Y_UP, 0);
var _matrix_proj = matrix_build_projection_perspective_fov(camera_FOV, window[w] / window[h], 0, 20000);

camera_set_view_mat(view_camera[0], _matrix_view);
camera_set_proj_mat(view_camera[0], _matrix_proj);

camera_apply(view_camera[0]);
 
Top