2018-11-25 17:44:32 +00:00
|
|
|
import Engine from './engine'
|
|
|
|
import Camera from './engine/camera'
|
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'
|
2018-11-25 17:44:32 +00:00
|
|
|
import { SimplexHeightMap } from './engine/components/terrain/heightmap'
|
|
|
|
import { Material } from './engine/mesh/material'
|
|
|
|
|
|
|
|
let game = new Engine()
|
|
|
|
let env = new Environment()
|
|
|
|
|
2019-02-26 12:02:12 +00:00
|
|
|
// let t = 0
|
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-02-12 10:03:18 +00:00
|
|
|
entity.setRotation([0.0, 0.0, -90.0])
|
2018-11-27 19:10:53 +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)
|
|
|
|
|
|
|
|
// Create a terrain
|
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
|
|
|
|
let material = new Material()
|
|
|
|
material.textures = ['grass-1024.jpg']
|
|
|
|
await material.loadTextures(game.gl)
|
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-02-26 16:22:33 +00:00
|
|
|
let cam = new Camera([-200.0, 1.0, 0.0])
|
2018-11-25 17:44:32 +00:00
|
|
|
cam.updateProjection(game.gl)
|
|
|
|
|
|
|
|
// Update function for camera
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2019-12-28 15:57:19 +00:00
|
|
|
|
2019-02-12 10:03:18 +00:00
|
|
|
// TESTING: Move model forward
|
2019-02-26 12:02:12 +00:00
|
|
|
// t = t + 0.1
|
|
|
|
// entity.setPosition([t, 0.0, 0.0])
|
2018-11-25 17:44:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
// Render function for the triangle
|
|
|
|
game.addRenderFunction(function (gl) {
|
2018-11-26 15:37:25 +00:00
|
|
|
shader.use(gl)
|
|
|
|
cam.draw(gl, shader)
|
|
|
|
entity.draw(gl, shader)
|
2019-12-01 16:30:03 +00:00
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|