Path: blob/main/projects/HexGL/bkcore/hexgl/tracks/Cityscape.js
4629 views
/*1* HexGL2* @author Thibaut 'BKcore' Despoulain <http://bkcore.com>3* @license This work is licensed under the Creative Commons Attribution-NonCommercial 3.0 Unported License.4* To view a copy of this license, visit http://creativecommons.org/licenses/by-nc/3.0/.5*/67var bkcore = bkcore || {};8bkcore.hexgl = bkcore.hexgl || {};9bkcore.hexgl.tracks = bkcore.hexgl.tracks || {};1011bkcore.hexgl.tracks.Cityscape = {1213lib: null,14materials: {},1516name: "Cityscape",1718checkpoints: {19list: [0,1,2],20start: 0,21last: 222},2324spawn: {25x: -1134*2,26y: 387,27z: -443*228},2930spawnRotation: {31x: 0,32y: 0,33z: 034},3536analyser: null,37pixelRatio: 2048.0 / 6000.0,3839load: function(opts, quality)40{41this.lib = new bkcore.threejs.Loader(opts);4243// desktop + quality low44// OR45// mobile + quality low or mid46if(quality < 2) // LOW47{48this.lib.load({49textures: {50'hex' : "textures/hud/hex.jpg",51'spark' : "textures/particles/spark.png",52'cloud' : "textures/particles/cloud.png",53'ship.feisar.diffuse' : "textures/ships/feisar/diffuse.jpg",54'booster.diffuse' : "textures/ships/feisar/booster/booster.png",55'booster.sprite' : "textures/ships/feisar/booster/boostersprite.jpg",56'track.cityscape.diffuse' : "textures/tracks/cityscape/diffuse.jpg",57'track.cityscape.scrapers1.diffuse' : "textures/tracks/cityscape/scrapers1/diffuse.jpg",58'track.cityscape.scrapers2.diffuse' : "textures/tracks/cityscape/scrapers2/diffuse.jpg",59'track.cityscape.start.diffuse' : "textures/tracks/cityscape/start/diffuse.jpg",60'track.cityscape.start.banner' : "textures/tracks/cityscape/start/start.jpg",61'bonus.base.diffuse' : "textures/bonus/base/diffuse.jpg"62},63texturesCube: {64'skybox.dawnclouds' : "textures/skybox/dawnclouds/%1.jpg"65},66geometries: {67'bonus.base' : "geometries/bonus/base/base.js",68'booster' : "geometries/booster/booster.js",69'ship.feisar' : "geometries/ships/feisar/feisar.js",70'track.cityscape' : "geometries/tracks/cityscape/track.js",71'track.cityscape.scrapers1' : "geometries/tracks/cityscape/scrapers1.js",72'track.cityscape.scrapers2' : "geometries/tracks/cityscape/scrapers2.js",73'track.cityscape.start' : "geometries/tracks/cityscape/start.js",74'track.cityscape.start.banner' : "geometries/tracks/cityscape/startbanner.js",75'track.cityscape.bonus.speed' : "geometries/tracks/cityscape/bonus/speed.js"76},77analysers: {78'track.cityscape.collision' : "textures/tracks/cityscape/collision.png",79'track.cityscape.height' : "textures/tracks/cityscape/height.png"80},81images: {82'hud.bg' : "textures/hud/hud-bg.png",83'hud.speed' : "textures/hud/hud-fg-speed.png",84'hud.shield' : "textures/hud/hud-fg-shield.png"85},86sounds: {87bg: {88src: 'audio/bg.ogg',89loop: true,90usePanner: false91},92crash: {93src: 'audio/crash.ogg',94loop: false,95usePanner: true96},97destroyed: {98src: 'audio/destroyed.ogg',99loop: false,100usePanner: false101},102boost: {103src: 'audio/boost.ogg',104loop: false,105usePanner: true106},107wind: {108src: 'audio/wind.ogg',109loop: true,110usePanner: true111}112}113});114}115// desktop + quality mid or high116// OR117// mobile + quality high118else // HIGH119{console.log('HIGH');120this.lib.load({121textures: {122'hex' : "textures.full/hud/hex.jpg",123'spark' : "textures.full/particles/spark.png",124'cloud' : "textures.full/particles/cloud.png",125'ship.feisar.diffuse' : "textures.full/ships/feisar/diffuse.jpg",126'ship.feisar.specular' : "textures.full/ships/feisar/specular.jpg",127'ship.feisar.normal' : "textures.full/ships/feisar/normal.jpg",128'booster.diffuse' : "textures.full/ships/feisar/booster/booster.png",129'booster.sprite' : "textures.full/ships/feisar/booster/boostersprite.jpg",130'track.cityscape.diffuse' : "textures.full/tracks/cityscape/diffuse.jpg",131'track.cityscape.specular' : "textures.full/tracks/cityscape/specular.jpg",132'track.cityscape.normal' : "textures.full/tracks/cityscape/normal.jpg",133'track.cityscape.scrapers1.diffuse' : "textures.full/tracks/cityscape/scrapers1/diffuse.jpg",134'track.cityscape.scrapers1.specular': "textures.full/tracks/cityscape/scrapers1/specular.jpg",135'track.cityscape.scrapers1.normal' : "textures.full/tracks/cityscape/scrapers1/normal.jpg",136'track.cityscape.scrapers2.diffuse' : "textures.full/tracks/cityscape/scrapers2/diffuse.jpg",137'track.cityscape.scrapers2.specular': "textures.full/tracks/cityscape/scrapers2/specular.jpg",138'track.cityscape.scrapers2.normal' : "textures.full/tracks/cityscape/scrapers2/normal.jpg",139'track.cityscape.start.diffuse' : "textures.full/tracks/cityscape/start/diffuse.jpg",140'track.cityscape.start.specular' : "textures.full/tracks/cityscape/start/specular.jpg",141'track.cityscape.start.normal' : "textures.full/tracks/cityscape/start/normal.jpg",142'track.cityscape.start.banner' : "textures.full/tracks/cityscape/start/start.jpg",143'bonus.base.diffuse' : "textures.full/bonus/base/diffuse.jpg",144'bonus.base.normal' : "textures.full/bonus/base/normal.jpg",145'bonus.base.specular' : "textures.full/bonus/base/specular.jpg"146},147texturesCube: {148'skybox.dawnclouds' : "textures.full/skybox/dawnclouds/%1.jpg"149},150geometries: {151'bonus.base' : "geometries/bonus/base/base.js",152'booster' : "geometries/booster/booster.js",153'ship.feisar' : "geometries/ships/feisar/feisar.js",154'track.cityscape' : "geometries/tracks/cityscape/track.js",155'track.cityscape.scrapers1' : "geometries/tracks/cityscape/scrapers1.js",156'track.cityscape.scrapers2' : "geometries/tracks/cityscape/scrapers2.js",157'track.cityscape.start' : "geometries/tracks/cityscape/start.js",158'track.cityscape.start.banner' : "geometries/tracks/cityscape/startbanner.js",159'track.cityscape.bonus.speed' : "geometries/tracks/cityscape/bonus/speed.js"160},161analysers: {162'track.cityscape.collision' : "textures.full/tracks/cityscape/collision.png",163'track.cityscape.height' : "textures.full/tracks/cityscape/height.png"164},165images: {166'hud.bg' : "textures.full/hud/hud-bg.png",167'hud.speed' : "textures.full/hud/hud-fg-speed.png",168'hud.shield' : "textures.full/hud/hud-fg-shield.png"169},170sounds: {171bg: {172src: 'audio/bg.ogg',173loop: true174},175crash: {176src: 'audio/crash.ogg',177loop: false178},179destroyed: {180src: 'audio/destroyed.ogg',181loop: false182},183boost: {184src: 'audio/boost.ogg',185loop: false186},187wind: {188src: 'audio/wind.ogg',189loop: true190}191}192});193}194},195196buildMaterials: function(quality)197{198// desktop + quality low199// OR200// mobile + quality low or mid201if(quality < 2) // LOW202{203this.materials.track = new THREE.MeshBasicMaterial({204map: this.lib.get("textures", "track.cityscape.diffuse"),205ambient: 0xcccccc206});207208this.materials.bonusBase = new THREE.MeshBasicMaterial({209map: this.lib.get("textures", "bonus.base.diffuse"),210ambient: 0xcccccc211});212213this.materials.bonusSpeed = new THREE.MeshBasicMaterial({214color: 0x0096ff215});216217this.materials.ship = new THREE.MeshBasicMaterial({218map: this.lib.get("textures", "ship.feisar.diffuse"),219ambient: 0xaaaaaa220});221222this.materials.booster = new THREE.MeshBasicMaterial({223map: this.lib.get("textures", "booster.diffuse"),224transparent: true225});226227this.materials.scrapers1 = new THREE.MeshBasicMaterial({228map: this.lib.get("textures", "track.cityscape.scrapers1.diffuse"),229ambient: 0xcccccc230});231232this.materials.scrapers2 = new THREE.MeshBasicMaterial({233map: this.lib.get("textures", "track.cityscape.scrapers2.diffuse"),234ambient: 0xcccccc235});236237this.materials.start = new THREE.MeshBasicMaterial({238map: this.lib.get("textures", "track.cityscape.start.diffuse"),239ambient: 0xcccccc240});241242this.materials.startBanner = new THREE.MeshBasicMaterial({243map: this.lib.get("textures", "track.cityscape.start.banner"),244transparent: false245});246}247// desktop + quality mid or high248// OR249// mobile + quality high250else // HIGH251{252this.materials.track = bkcore.Utils.createNormalMaterial({253diffuse: this.lib.get("textures", "track.cityscape.diffuse"),254specular: this.lib.get("textures", "track.cityscape.specular"),255normal: this.lib.get("textures", "track.cityscape.normal"),256ambient: 0xffffff,257shininess: 42,258metal: true,259perPixel: true260});261262this.materials.bonusBase = bkcore.Utils.createNormalMaterial({263diffuse: this.lib.get("textures", "bonus.base.diffuse"),264specular: this.lib.get("textures", "bonus.base.specular"),265normal: this.lib.get("textures", "bonus.base.normal"),266normalScale: 3.0,267ambient: 0x444444,268shininess: 42,269metal: false,270perPixel: false271});272273this.materials.bonusSpeed = new THREE.MeshBasicMaterial({274color: 0x0096ff275});276277this.materials.ship = bkcore.Utils.createNormalMaterial({278diffuse: this.lib.get("textures", "ship.feisar.diffuse"),279specular: this.lib.get("textures", "ship.feisar.specular"),280normal: this.lib.get("textures", "ship.feisar.normal"),281ambient: 0x444444,282shininess: 42,283metal: true,284perPixel: false285});286287this.materials.booster = new THREE.MeshBasicMaterial({288map: this.lib.get("textures", "booster.diffuse"),289transparent: true290});291292this.materials.scrapers1 = bkcore.Utils.createNormalMaterial({293diffuse: this.lib.get("textures", "track.cityscape.scrapers1.diffuse"),294specular: this.lib.get("textures", "track.cityscape.scrapers1.specular"),295normal: this.lib.get("textures", "track.cityscape.scrapers1.normal"),296cube: this.lib.get("texturesCube", "skybox.dawnclouds"),297reflectivity: 0.8,298ambient: 0x444444,299shininess: 42,300metal: false,301perPixel: false302});303304this.materials.scrapers2 = bkcore.Utils.createNormalMaterial({305diffuse: this.lib.get("textures", "track.cityscape.scrapers2.diffuse"),306specular: this.lib.get("textures", "track.cityscape.scrapers2.specular"),307normal: this.lib.get("textures", "track.cityscape.scrapers2.normal"),308cube: this.lib.get("texturesCube", "skybox.dawnclouds"),309reflectivity: 0.8,310ambient: 0x000000,311shininess: 42,312metal: false,313perPixel: false314});315316this.materials.start = bkcore.Utils.createNormalMaterial({317diffuse: this.lib.get("textures", "track.cityscape.start.diffuse"),318specular: this.lib.get("textures", "track.cityscape.start.specular"),319normal: this.lib.get("textures", "track.cityscape.start.normal"),320ambient: 0xaaaaaa,321shininess: 42,322metal: false,323perPixel: false324});325326this.materials.startBanner = new THREE.MeshBasicMaterial({327map: this.lib.get("textures", "track.cityscape.start.banner"),328transparent: false329});330}331},332333buildScenes: function(ctx, quality)334{335// IMPORTANT336this.analyser = this.lib.get("analysers", "track.cityscape.collision");337338// SKYBOX339var sceneCube = new THREE.Scene();340341var cameraCube = new THREE.PerspectiveCamera( 70, ctx.width / ctx.height, 1, 6000 );342sceneCube.add( cameraCube );343344var skyshader = THREE.ShaderUtils.lib[ "cube" ];345skyshader.uniforms[ "tCube" ].texture = this.lib.get("texturesCube", "skybox.dawnclouds");346347var skymaterial = new THREE.ShaderMaterial(348{349fragmentShader: skyshader.fragmentShader,350vertexShader: skyshader.vertexShader,351uniforms: skyshader.uniforms,352depthWrite: false353});354355var mesh = new THREE.Mesh( new THREE.CubeGeometry( 100, 100, 100 ), skymaterial );356mesh.flipSided = true;357358sceneCube.add(mesh);359360ctx.manager.add("sky", sceneCube, cameraCube);361362var ambient = 0xbbbbbb, diffuse = 0xffffff, specular = 0xffffff, shininess = 42, scale = 23;363364// MAIN SCENE365var camera = new THREE.PerspectiveCamera( 70, ctx.width / ctx.height, 1, 60000 );366367var scene = new THREE.Scene();368scene.add( camera );369scene.add( new THREE.AmbientLight( ambient ) );370371// SUN372var sun = new THREE.DirectionalLight( diffuse, 1.5, 30000 );373sun.position.set( -4000, 1200, 1800 );374sun.lookAt(new THREE.Vector3());375376// desktop + quality mid or high377if(quality > 2)378{379sun.castShadow = true;380sun.shadowCameraNear = 50;381sun.shadowCameraFar = camera.far*2;382sun.shadowCameraRight = 3000;383sun.shadowCameraLeft = -3000;384sun.shadowCameraTop = 3000;385sun.shadowCameraBottom = -3000;386//sun.shadowCameraVisible = true;387sun.shadowBias = 0.0001;388sun.shadowDarkness = 0.7;389sun.shadowMapWidth = 2048;390sun.shadowMapHeight = 2048;391}392scene.add( sun );393394// SHIP395var ship = ctx.createMesh(scene, this.lib.get("geometries", "ship.feisar"), -1134*2, 10, -443*2, this.materials.ship);396397var booster = ctx.createMesh(ship, this.lib.get("geometries", "booster"), 0, 0.665, -3.8, this.materials.booster);398booster.depthWrite = false;399400var boosterSprite = new THREE.Sprite({401map: this.lib.get("textures", "booster.sprite"),402blending: THREE.AdditiveBlending,403useScreenCoordinates: false,404color: 0xffffff405});406boosterSprite.scale.set(0.02, 0.02, 0.02);407boosterSprite.mergeWith3D = false;408booster.add(boosterSprite);409410var boosterLight = new THREE.PointLight(0x00a2ff, 4.0, 60);411boosterLight.position.set(0, 0.665, -4);412413// desktop + quality low, mid or high414// OR415// mobile + quality mid or high416// NB booster is now enabled on desktop + low quality,417// when it wasn't before; this is because this booster setting418// is the only difference between mobile + mid quality419// and desktop + low quality, so I merged them for convenience420if(quality > 0)421ship.add(boosterLight);422423// SHIP CONTROLS424var shipControls = new bkcore.hexgl.ShipControls(ctx);425shipControls.collisionMap = this.lib.get("analysers", "track.cityscape.collision");426shipControls.collisionPixelRatio = 2048.0 / 6000.0;427shipControls.collisionDetection = true;428shipControls.heightMap = this.lib.get("analysers", "track.cityscape.height");;429shipControls.heightPixelRatio = 2048.0 / 6000.0;430shipControls.heightBias = 4.0;431shipControls.heightScale = 10.0;432shipControls.control(ship);433ctx.components.shipControls = shipControls;434ctx.tweakShipControls();435436// SHIP EFFECTS AND PARTICLES437var fxParams = {438scene: scene,439shipControls: shipControls,440booster: booster,441boosterSprite: boosterSprite,442boosterLight: boosterLight,443useParticles: false444};445446// desktop + quality mid or high447if(quality > 2)448{449fxParams.textureCloud = this.lib.get("textures", "cloud");450fxParams.textureSpark = this.lib.get("textures", "spark");451fxParams.useParticles = true;452}453ctx.components.shipEffects = new bkcore.hexgl.ShipEffects(fxParams);454455// TRACK456var track = ctx.createMesh(scene, this.lib.get("geometries", "track.cityscape"), 0, -5, 0, this.materials.track);457var bonusBase = ctx.createMesh(scene, this.lib.get("geometries", "bonus.base"), 0, -5, 0, this.materials.bonusBase);458var bonusSpeed = ctx.createMesh(scene, this.lib.get("geometries", "track.cityscape.bonus.speed"), 0, -5, 0, this.materials.bonusSpeed);459bonusSpeed.receiveShadow = false;460var scrapers1 = ctx.createMesh(scene, this.lib.get("geometries", "track.cityscape.scrapers1"), 0, 0, 0, this.materials.scrapers1);461var scrapers2 = ctx.createMesh(scene, this.lib.get("geometries", "track.cityscape.scrapers2"), 0, 0, 0, this.materials.scrapers2);462var start = ctx.createMesh(scene, this.lib.get("geometries", "track.cityscape.start"), 0, -5, 0, this.materials.start);463var startbanner = ctx.createMesh(scene, this.lib.get("geometries", "track.cityscape.start.banner"), 0, -5, 0, this.materials.startBanner);464startbanner.doubleSided = true;465466// CAMERA467ctx.components.cameraChase = new bkcore.hexgl.CameraChase({468target: ship,469camera: camera,470cameraCube: ctx.manager.get("sky").camera,471lerp: 0.5,472yoffset: 8.0,473zoffset: 10.0,474viewOffset: 10.0475});476477ctx.manager.add("game", scene, camera, function(delta, renderer)478{479if(delta > 25 && this.objects.lowFPS < 1000) this.objects.lowFPS++;480481var dt = delta/16.6;482483this.objects.components.shipControls.update(dt);484485this.objects.components.shipEffects.update(dt);486487this.objects.components.cameraChase.update(dt, this.objects.components.shipControls.getSpeedRatio());488/*this.objects.time += 0.002;489var c = this.objects.components.cameraChase.camera;490c.position.set(491Math.cos(this.objects.time)*15+this.objects.components.shipControls.dummy.position.x,49210+this.objects.components.shipControls.dummy.position.y,493Math.sin(this.objects.time)*15+this.objects.components.shipControls.dummy.position.z494);495c.lookAt(this.objects.components.shipControls.dummy.position);496this.objects.components.cameraChase.cameraCube.rotation.copy(c.rotation);*/497498this.objects.composers.game.render(dt);499if(this.objects.hud) this.objects.hud.update(500this.objects.components.shipControls.getRealSpeed(100),501this.objects.components.shipControls.getRealSpeedRatio(),502this.objects.components.shipControls.getShield(100),503this.objects.components.shipControls.getShieldRatio()504);505if(this.objects.components.shipControls.getShieldRatio() < 0.2)506this.objects.extras.vignetteColor.setHex(0x992020);507else508this.objects.extras.vignetteColor.setHex(0x458ab1);509},510{511components: ctx.components,512composers: ctx.composers,513extras: ctx.extras,514quality: quality,515hud: ctx.hud,516time: 0.0,517lowFPS: 0518});519}520}521522523