98 lines
2.3 KiB
TypeScript
98 lines
2.3 KiB
TypeScript
import vsSource from './shaders/shader.vert';
|
|
import texture from './shaders/texture.frag';
|
|
|
|
import {loadObjBuffers} from './objutils';
|
|
import {initShaderProgram} from './shaders';
|
|
import {drawScene} from './draw';
|
|
import {initCache, initParams, setProgramInfo} from './init';
|
|
|
|
import {uiUpdateParams,
|
|
uiUpdateTexture,
|
|
uiUpdateObject,
|
|
uiUpdateShader} from './uijquery';
|
|
import {uiUpdateScene} from './uiscene';
|
|
import {initUX} from './ux';
|
|
import {updateCamera} from './camera';
|
|
|
|
if (window.innerWidth < 1900) {
|
|
$('#screen-warning').append(`
|
|
<div class='display-4'>This app works best on 1920x1080 screens !</div>
|
|
`);
|
|
}
|
|
main();
|
|
|
|
/**
|
|
* The program purpose is encapsulated in a main function
|
|
*/
|
|
async function main() {
|
|
const context: any = {
|
|
gl: null,
|
|
obj: null,
|
|
texture: null,
|
|
params: null,
|
|
buffers: null,
|
|
programInfo: null,
|
|
shaderProgram: null,
|
|
shaders: {
|
|
frag: null,
|
|
vert: null,
|
|
},
|
|
cache: null,
|
|
scene: [],
|
|
};
|
|
const canvas: any = document.querySelector('#glCanvas')!;
|
|
context.gl = canvas.getContext('webgl2');
|
|
|
|
if (context.gl == null) {
|
|
canvas.parentNode.removeChild(canvas);
|
|
document.getElementById('root')!.insertAdjacentHTML('beforeend',
|
|
`<p>Unable to initialize WebGL. Your browser or machine may not
|
|
support it.</p>`);
|
|
}
|
|
|
|
initParams(context);
|
|
await initCache(context);
|
|
context.buffers = loadObjBuffers(context,
|
|
context.buffers,
|
|
context.cache.objs.racer);
|
|
context.shaderProgram = initShaderProgram(context,
|
|
context.shaderProgram,
|
|
context.shaders.frag,
|
|
context.shaders.vert,
|
|
vsSource,
|
|
texture);
|
|
context.programInfo = setProgramInfo(context, context.shaderProgram);
|
|
let then = 0;
|
|
let changed = false;
|
|
|
|
/**
|
|
* Draws the scene repeatedly
|
|
* @param {number} now the current time
|
|
*/
|
|
function render(now: any) {
|
|
now *= 0.001;
|
|
const deltaTime = now - then;
|
|
context.params.squareRotation += deltaTime * context.params.rotSpeed;
|
|
if (now >= 1 && changed == false) {
|
|
changed = true;
|
|
}
|
|
then = now;
|
|
updateCamera(context);
|
|
drawScene(context.gl,
|
|
context.programInfo,
|
|
context.buffers,
|
|
context.params,
|
|
context.texture,
|
|
context.scene,
|
|
now);
|
|
requestAnimationFrame(render);
|
|
}
|
|
uiUpdateParams(context.params);
|
|
uiUpdateTexture(context);
|
|
uiUpdateObject(context);
|
|
uiUpdateShader(context);
|
|
uiUpdateScene(context);
|
|
initUX(context, canvas);
|
|
requestAnimationFrame(render);
|
|
}
|