fix camera view calculation

This commit is contained in:
John Alanbrook 2024-08-22 13:31:00 -05:00
parent 5690ee7fba
commit 285a227691
3 changed files with 47 additions and 30 deletions

View file

@ -152,6 +152,7 @@ var entity = {
ent.objects = {}; ent.objects = {};
ent.timers = []; ent.timers = [];
ent.ur = {}; ent.ur = {};
ent.urname = text;
}); });
/* /*
if (!text) if (!text)

View file

@ -60,6 +60,29 @@ game.engine_start = function (s) {
Object.readonly(window.__proto__, "enable_clipboard"); Object.readonly(window.__proto__, "enable_clipboard");
Object.readonly(window.__proto__, "high_dpi"); Object.readonly(window.__proto__, "high_dpi");
Object.readonly(window.__proto__, "sample_count"); Object.readonly(window.__proto__, "sample_count");
prosperon.camera = prosperon.make_camera();
var camera = prosperon.camera;
camera.transform.pos = [0,0,-100];
camera.mode = "keep";
camera.break = "fit";
camera.size = game.size;
gamestate.camera = camera;
prosperon.hudcam = prosperon.make_camera();
var hudcam = prosperon.hudcam;
hudcam.near = 0;
hudcam.size = camera.size;
hudcam.mode = "keep";
hudcam.break = "fit";
prosperon.appcam = prosperon.make_camera();
var appcam = prosperon.appcam;
appcam.near = 0;
appcam.size = window.size;
appcam.transform.pos = [window.size.x,window.size.y,-100];
prosperon.screencolor = render.screencolor();
s(); s();
shape.quad = { shape.quad = {
@ -87,28 +110,6 @@ game.engine_start = function (s) {
}; };
render.init(); render.init();
prosperon.camera = prosperon.make_camera();
var camera = prosperon.camera;
camera.transform.pos = [0,0,-100];
camera.mode = "keep";
camera.break = "fit";
camera.size = game.size;
gamestate.camera = camera;
prosperon.hudcam = prosperon.make_camera();
var hudcam = prosperon.hudcam;
hudcam.near = 0;
hudcam.size = camera.size;
hudcam.mode = "keep";
hudcam.break = "fit";
prosperon.appcam = prosperon.make_camera();
var appcam = prosperon.appcam;
appcam.near = 0;
appcam.size = window.size;
appcam.transform.pos = [window.size.x,window.size.y,-100];
prosperon.screencolor = render.screencolor();
}, },
prosperon.process, prosperon.process,
window.size.x, window.size.x,

View file

@ -853,7 +853,7 @@ render.draw = function render_draw(mesh, ssbo, inst = 1)
} }
// Returns an array in the form of [left, bottom, right, top] in pixels of the camera to render to // Returns an array in the form of [left, bottom, right, top] in pixels of the camera to render to
// Camera viewport is [left,bottom,right,top] in relative values // Camera viewport is [left,bottom,width,height] in relative values
function camviewport() function camviewport()
{ {
var aspect = (this.viewport[2]-this.viewport[0])/(this.viewport[3]-this.viewport[1])*window.size.x/window.size.y; var aspect = (this.viewport[2]-this.viewport[0])/(this.viewport[3]-this.viewport[1])*window.size.x/window.size.y;
@ -901,17 +901,32 @@ function camscreen2world(pos)
return view; return view;
} }
// three coordinatse
// world coordinates, the "actual" view relative to the game's universe
// camera coordinates, normalized from 0 to 1 inside of a camera's viewport, bottom left is 0,0, top right is 1,1
// screen coordinates, pixels, 0,0 at the bottom left of the window and [w,h] at the top right of the screen
camscreen2world.doc = "Convert a view position for a camera to world." camscreen2world.doc = "Convert a view position for a camera to world."
// return camera coordinates given a screen position
function screen2cam(pos) function screen2cam(pos)
{ {
var viewport = this.view(); var viewport = this.view();
var width = viewport[2]-viewport[0]; var width = viewport[2];
var height = viewport[3]-viewport[1]; var height = viewport[3];
var left = pos.x-viewport[0]; var viewpos = pos.sub([viewport[0],viewport[1]]);
var bottom = pos.y-viewport[1]; return viewpos.div([width,height]);
var p = [left/width, bottom/height]; }
return p;
function camextents()
{
var half = this.size;//.scale(0.5);
return {
l: this.pos.x-half.x,
r: this.pos.x+half.x,
t:this.pos.y+half.y,
b:this.pos.y-half.y
};
} }
screen2cam.doc = "Convert a screen space position in pixels to a normalized viewport position in a camera." screen2cam.doc = "Convert a screen space position in pixels to a normalized viewport position in a camera."
@ -928,7 +943,7 @@ prosperon.make_camera = function()
cam.mode = "stretch"; cam.mode = "stretch";
cam.screen2world = camscreen2world; cam.screen2world = camscreen2world;
cam.screen2cam = screen2cam; cam.screen2cam = screen2cam;
cam.extents = camextents;
cam.mousepos = function() { return this.screen2world(input.mouse.screenpos()); } cam.mousepos = function() { return this.screen2world(input.mouse.screenpos()); }
cam.view = camviewport; cam.view = camviewport;
cam.offscreen = false; cam.offscreen = false;