2018-11-25 17:44:32 +00:00
|
|
|
import Engine from './engine'
|
|
|
|
import Camera from './engine/camera'
|
2019-12-28 20:51:31 +00:00
|
|
|
import Resource from './engine/resource'
|
2018-11-27 19:10:53 +00:00
|
|
|
import loadMesh from './engine/mesh/loader'
|
2018-11-25 17:44:32 +00:00
|
|
|
|
|
|
|
import { Environment } from './engine/environment'
|
2019-12-28 15:57:19 +00:00
|
|
|
import { LODTerrain } from './engine/components/terrain/lod'
|
2019-12-28 20:51:31 +00:00
|
|
|
import { Skybox } from './engine/components/skybox'
|
2019-12-29 15:18:37 +00:00
|
|
|
import { WaterTile } from './engine/components/water'
|
2018-11-25 17:44:32 +00:00
|
|
|
import { SimplexHeightMap } from './engine/components/terrain/heightmap'
|
2019-12-28 20:51:31 +00:00
|
|
|
import { Material, Texture } from './engine/mesh/material'
|
|
|
|
import { GUIRenderer, GUIImage, Dim4 } from './engine/gui'
|
2019-12-29 02:20:57 +00:00
|
|
|
import { FontRenderer, GUIText, Font } from './engine/gui/font'
|
2018-11-25 17:44:32 +00:00
|
|
|
|
2019-12-29 12:58:53 +00:00
|
|
|
let game = Engine
|
2018-11-25 17:44:32 +00:00
|
|
|
let env = new Environment()
|
2019-12-28 20:51:31 +00:00
|
|
|
let gui = new GUIRenderer()
|
2019-12-29 02:20:57 +00:00
|
|
|
let fnt = new FontRenderer()
|
2018-11-25 17:44:32 +00:00
|
|
|
|
|
|
|
async function pipeline () {
|
2019-02-12 10:03:18 +00:00
|
|
|
let entity = await loadMesh(game.gl, 'test')
|
2018-11-26 15:37:25 +00:00
|
|
|
let shader = await game.shaders.createShaderFromFiles(game.gl, 'basic', false)
|
2018-11-25 17:44:32 +00:00
|
|
|
let terrainShader = await game.shaders.createShaderFromFiles(game.gl, 'terrain', false)
|
2019-12-28 20:51:31 +00:00
|
|
|
let skyboxShader = await game.shaders.createShaderFromFiles(game.gl, 'skybox', false)
|
2019-12-29 15:18:37 +00:00
|
|
|
let waterShader = await game.shaders.createShaderFromFiles(game.gl, 'water', false)
|
2018-11-25 17:44:32 +00:00
|
|
|
|
2019-02-12 10:03:18 +00:00
|
|
|
entity.setRotation([0.0, 0.0, -90.0])
|
2018-11-27 19:10:53 +00:00
|
|
|
|
2019-12-29 15:18:37 +00:00
|
|
|
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')
|
2019-12-29 12:58:53 +00:00
|
|
|
|
2019-12-29 02:20:57 +00:00
|
|
|
let arialFont = await Font.fromFile('arial')
|
|
|
|
await arialFont.loadTextures(game.gl)
|
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
// Initialize GUI
|
|
|
|
await gui.initialize(game)
|
2019-12-29 02:20:57 +00:00
|
|
|
await fnt.initialize(game)
|
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
let itms = [
|
|
|
|
new GUIImage(await Texture.createTexture2D(game.gl, await Resource.loadImage('noisy.png'), false, game.gl.LINEAR),
|
2019-12-29 12:58:53 +00:00
|
|
|
new Dim4(-0.9, 0.0, 0.9, 0.0), new Dim4(0.1, 0.0, 0.1, 0.0))
|
2019-12-28 20:51:31 +00:00
|
|
|
]
|
2019-12-29 12:58:53 +00:00
|
|
|
// 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()
|
2019-12-28 20:51:31 +00:00
|
|
|
|
2018-11-25 17:44:32 +00:00
|
|
|
// Create a height map based on OpenSimplex noise
|
|
|
|
let hmap = new SimplexHeightMap(1, 1, 256, 50)
|
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
// Create a terrain instance
|
2019-12-28 15:57:19 +00:00
|
|
|
let terrain = new LODTerrain([0.0, 0.0, 0.0], 1024, 1024, 850, 4)
|
2018-11-25 17:44:32 +00:00
|
|
|
|
|
|
|
// Terrain material
|
2019-12-28 20:51:31 +00:00
|
|
|
let material = new Material(['grass-1024.jpg'])
|
2018-11-25 17:44:32 +00:00
|
|
|
await material.loadTextures(game.gl)
|
2019-12-28 15:57:19 +00:00
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
// test code
|
|
|
|
for (let i in entity.children) {
|
|
|
|
entity.children[i].mesh.material = material
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set generator and material for terrain
|
2019-12-28 15:57:19 +00:00
|
|
|
terrain.setGenerator(hmap)
|
2018-11-25 17:44:32 +00:00
|
|
|
terrain.setMaterial(material)
|
|
|
|
|
|
|
|
// Create and initialize the camera
|
2019-12-29 12:58:53 +00:00
|
|
|
let cam = new Camera([-32.0, 100.0, -32.0], [0.8, -0.6, 0.0])
|
2018-11-25 17:44:32 +00:00
|
|
|
cam.updateProjection(game.gl)
|
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
// Create skybox
|
|
|
|
let skybox = new Skybox('skybox', cam.farPlane / 2)
|
|
|
|
|
|
|
|
// Load textures and generate a mesh
|
|
|
|
await skybox.initialize(game.gl)
|
|
|
|
|
2019-12-29 15:18:37 +00:00
|
|
|
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)))
|
2019-12-29 12:58:53 +00:00
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
// Update function for camera and terrain
|
2018-11-25 17:44:32 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
// Panning
|
2018-11-25 17:44:32 +00:00
|
|
|
if (game.input.mouseMoved && game.input.mouse.btn0) {
|
|
|
|
cam.processMouseMove(game.input.mouseOffset)
|
|
|
|
}
|
2019-02-12 10:03:18 +00:00
|
|
|
|
2019-12-28 16:35:25 +00:00
|
|
|
// Update detail levels
|
|
|
|
terrain.update(game.gl, cam)
|
|
|
|
terrain.updateLODMesh(game.gl)
|
2018-11-25 17:44:32 +00:00
|
|
|
})
|
|
|
|
|
2019-12-29 12:58:53 +00:00
|
|
|
function drawEverything (gl) {
|
|
|
|
game.prepare()
|
|
|
|
|
2018-11-25 17:44:32 +00:00
|
|
|
// 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)
|
2019-12-29 12:58:53 +00:00
|
|
|
|
2019-12-29 15:18:37 +00:00
|
|
|
shader.use(gl)
|
|
|
|
cam.draw(gl, shader)
|
|
|
|
entity.draw(gl, shader)
|
|
|
|
|
2019-12-28 20:51:31 +00:00
|
|
|
// Draw the skybox
|
|
|
|
skyboxShader.use(gl)
|
|
|
|
skybox.draw(gl, skyboxShader, cam)
|
2019-12-29 12:58:53 +00:00
|
|
|
}
|
2019-12-28 20:51:31 +00:00
|
|
|
|
2019-12-29 12:58:53 +00:00
|
|
|
// Render function for the triangle
|
2019-12-28 20:51:31 +00:00
|
|
|
game.addRenderFunction(function (gl) {
|
2019-12-29 15:18:37 +00:00
|
|
|
water.reflect(gl, cam, drawEverything)
|
|
|
|
water.refract(gl, cam, drawEverything)
|
2019-12-29 12:58:53 +00:00
|
|
|
|
|
|
|
drawEverything(gl)
|
|
|
|
|
2019-12-29 15:18:37 +00:00
|
|
|
waterShader.use(gl)
|
|
|
|
cam.draw(gl, waterShader)
|
|
|
|
water.draw(gl, waterShader, cam, env.sun)
|
2019-12-29 12:58:53 +00:00
|
|
|
|
|
|
|
// Draw GUIs
|
|
|
|
gui.draw(gl, cam, itms)
|
|
|
|
fnt.draw(gl, cam, itms)
|
2018-11-25 17:44:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
game.startGameLoop()
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:03:18 +00:00
|
|
|
// Start the game, catch any errors thrown
|
2018-11-25 17:44:32 +00:00
|
|
|
pipeline().catch(function (e) {
|
|
|
|
console.error(e)
|
|
|
|
})
|