proper mouse camera support
This commit is contained in:
parent
414c3263ea
commit
50a878963c
|
@ -34,10 +34,6 @@ export function initParams(context: any) {
|
||||||
y: 0,
|
y: 0,
|
||||||
z: 0,
|
z: 0,
|
||||||
},
|
},
|
||||||
mousePrev: {
|
|
||||||
x: null,
|
|
||||||
y: null,
|
|
||||||
},
|
|
||||||
keyboard: {
|
keyboard: {
|
||||||
w: false,
|
w: false,
|
||||||
a: false,
|
a: false,
|
||||||
|
|
|
@ -41,7 +41,6 @@ async function main() {
|
||||||
|
|
||||||
const data = await fetchObj('/static/objs/racer.obj');
|
const data = await fetchObj('/static/objs/racer.obj');
|
||||||
initParams(context);
|
initParams(context);
|
||||||
console.log(context.params);
|
|
||||||
updateObj(context, data, true);
|
updateObj(context, data, true);
|
||||||
initShaderProgram(context, vsSource, fsSource4);
|
initShaderProgram(context, vsSource, fsSource4);
|
||||||
setProgramInfo(context);
|
setProgramInfo(context);
|
||||||
|
@ -84,6 +83,6 @@ async function main() {
|
||||||
uiUpdateTexture(context);
|
uiUpdateTexture(context);
|
||||||
uiUpdateObject(context);
|
uiUpdateObject(context);
|
||||||
uiUpdateShader(context);
|
uiUpdateShader(context);
|
||||||
initUX(context);
|
initUX(context, canvas);
|
||||||
requestAnimationFrame(render);
|
requestAnimationFrame(render);
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,23 +3,27 @@ import $ from 'jquery';
|
||||||
/**
|
/**
|
||||||
* UI block to update object
|
* UI block to update object
|
||||||
* @param {any} context the program context
|
* @param {any} context the program context
|
||||||
|
* @param {any} canvas the HTML canvas
|
||||||
*/
|
*/
|
||||||
export function initUX(context: any) {
|
export function initUX(context: any, canvas: any) {
|
||||||
$('#glCanvas').mousemove((event) => {
|
window.addEventListener('mousemove', (e) => {
|
||||||
if (context.params.mousePrev.x == null) {
|
if (document.pointerLockElement === canvas) {
|
||||||
context.params.mousePrev.x = event.pageX;
|
context.params.camRot.x += 0.01 * e.movementY;
|
||||||
context.params.mousePrev.y = event.pageY;
|
context.params.camRot.y += 0.01 * e.movementX;
|
||||||
|
|
||||||
|
context.params.camRot.x = Math.max(-Math.PI * 0.5,
|
||||||
|
Math.min(context.params.camRot.x, Math.PI * 0.5));
|
||||||
|
context.params.camRot.y += context.params.camRot.y > Math.PI ?
|
||||||
|
-Math.PI * 2 : context.params.camRot.y < -Math.PI ?
|
||||||
|
Math.PI * 2 : 0;
|
||||||
}
|
}
|
||||||
context.params.camRot.y +=
|
|
||||||
(context.params.mousePrev.x - event.pageX) / 100;
|
|
||||||
context.params.camRot.x +=
|
|
||||||
(context.params.mousePrev.y - event.pageY) / 100;
|
|
||||||
context.params.mousePrev.x = event.pageX;
|
|
||||||
context.params.mousePrev.y = event.pageY;
|
|
||||||
});
|
});
|
||||||
$('#glCanvas').mouseenter((event) => {
|
$('#glCanvas').click(() => {
|
||||||
context.params.mousePrev.x = event.pageX;
|
if (document.pointerLockElement === canvas) {
|
||||||
context.params.mousePrev.y = event.pageY;
|
document.exitPointerLock();
|
||||||
|
} else {
|
||||||
|
canvas.requestPointerLock();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
$(document).keydown((event) => {
|
$(document).keydown((event) => {
|
||||||
if (event.key == 'Shift') {
|
if (event.key == 'Shift') {
|
||||||
|
|
|
@ -14,6 +14,9 @@
|
||||||
<div id="root">
|
<div id="root">
|
||||||
<canvas id='glCanvas' width='640' height='640'></canvas>
|
<canvas id='glCanvas' width='640' height='640'></canvas>
|
||||||
<div id='ui'>
|
<div id='ui'>
|
||||||
|
<div class='ui-block'>
|
||||||
|
To control the camera with mouse, click in the canvas, to exit, click again.
|
||||||
|
</div>
|
||||||
<div class='ui-block'>
|
<div class='ui-block'>
|
||||||
<div style='display: inline;'>Change shader: </div>
|
<div style='display: inline;'>Change shader: </div>
|
||||||
<button id='s_blackandwhite'>Black & White</button>
|
<button id='s_blackandwhite'>Black & White</button>
|
||||||
|
|
Loading…
Reference in New Issue