import Engine from './engine' import Camera from './engine/camera' import Resource from './engine/resource' import loadMesh from './engine/mesh/loader' import { Environment } from './engine/environment' import { LODTerrain } from './engine/components/terrain/lod' import { Skybox } from './engine/components/skybox' import { WaterTile } from './engine/components/water' import { SimplexHeightMap } from './engine/components/terrain/heightmap' import { Material, Texture } from './engine/mesh/material' import { GUIRenderer, GUIImage, Dim4 } from './engine/gui' import { FontRenderer, GUIText, Font } from './engine/gui/font' let game = Engine let env = new Environment() let gui = new GUIRenderer() let fnt = new FontRenderer() async function pipeline () { let entity = await loadMesh(game.gl, 'test') let shader = await game.shaders.createShaderFromFiles(game.gl, 'basic', false) let terrainShader = await game.shaders.createShaderFromFiles(game.gl, 'terrain', false) let skyboxShader = await game.shaders.createShaderFromFiles(game.gl, 'skybox', false) let waterShader = await game.shaders.createShaderFromFiles(game.gl, 'water', false) entity.setRotation([0.0, 0.0, -90.0]) let water = new WaterTile([100.0, 0.0, 100.0], 100.0) water.initialize(game.gl) await water.useDUDV(game.gl, 'dudv') await water.useNormalMap(game.gl, 'normalmap') let arialFont = await Font.fromFile('arial') await arialFont.loadTextures(game.gl) // Initialize GUI await gui.initialize(game) await fnt.initialize(game) let itms = [ new GUIImage(await Texture.createTexture2D(game.gl, await Resource.loadImage('noisy.png'), false, game.gl.LINEAR), new Dim4(-0.9, 0.0, 0.9, 0.0), new Dim4(0.1, 0.0, 0.1, 0.0)) ] // Nesting test itms[0].addChild(new GUIText('this is example text!\nmulti line!', arialFont, 1.5, new Dim4(0.1, 0.0, -0.1, 0.0), new Dim4(1.0, 0.0, 0.3, 0.0), false)) itms[0].children[0].color = [1.0, 0.0, 0.2] itms[0].updateTransform() // Create a height map based on OpenSimplex noise let hmap = new SimplexHeightMap(1, 1, 256, 50) // Create a terrain instance let terrain = new LODTerrain([0.0, 0.0, 0.0], 1024, 1024, 850, 4) // Terrain material let material = new Material(['grass-1024.jpg']) await material.loadTextures(game.gl) // test code for (let i in entity.children) { entity.children[i].mesh.material = material } // Set generator and material for terrain terrain.setGenerator(hmap) terrain.setMaterial(material) // Create and initialize the camera let cam = new Camera([-32.0, 100.0, -32.0], [0.8, -0.6, 0.0]) cam.updateProjection(game.gl) // Create skybox let skybox = new Skybox('skybox', cam.farPlane / 2) // Load textures and generate a mesh await skybox.initialize(game.gl) itms.push(new GUIImage(water.fbos.reflectionTexture, new Dim4(-0.9, 0.0, 0.9, 0.0), new Dim4(0.1, 0.0, 0.1, 0.0))) itms.push(new GUIImage(water.fbos.refractionTexture, new Dim4(-0.3, 0.0, 0.9, 0.0), new Dim4(0.1, 0.0, 0.1, 0.0))) // Update function for camera and terrain game.addUpdateFunction(function (dt) { if (game.input.isDown('w')) { cam.processKeyboard(0, dt) } else if (game.input.isDown('s')) { cam.processKeyboard(1, dt) } if (game.input.isDown('a')) { cam.processKeyboard(2, dt) } else if (game.input.isDown('d')) { cam.processKeyboard(3, dt) } // Panning if (game.input.mouseMoved && game.input.mouse.btn0) { cam.processMouseMove(game.input.mouseOffset) } // Update detail levels terrain.update(game.gl, cam) terrain.updateLODMesh(game.gl) }) function drawEverything (gl) { game.prepare() // Use terrain shader terrainShader.use(gl) // Set environment variables in shader env.draw(gl, terrainShader) // Set the viewport uniforms cam.draw(gl, terrainShader) // Draw terrain terrain.draw(gl, terrainShader) shader.use(gl) cam.draw(gl, shader) entity.draw(gl, shader) // Draw the skybox skyboxShader.use(gl) skybox.draw(gl, skyboxShader, cam) } // Render function for the triangle game.addRenderFunction(function (gl) { water.reflect(gl, cam, drawEverything) water.refract(gl, cam, drawEverything) drawEverything(gl) waterShader.use(gl) cam.draw(gl, waterShader) water.draw(gl, waterShader, cam, env.sun) // Draw GUIs gui.draw(gl, cam, itms) fnt.draw(gl, cam, itms) }) game.startGameLoop() } // Start the game, catch any errors thrown pipeline().catch(function (e) { console.error(e) })