var createScene = function () {
var scene = new BABYLON.Scene(engine);
var light = new BABYLON.DirectionalLight(“dir01”, new BABYLON.Vector3(0, 20, -0.3), scene);
light.intensity = 0.1;
var camera = new BABYLON.FreeCamera(“Camera”, new BABYLON.Vector3(0, 12.5, -20), scene);
camera.speed = 0.4;
camera.attachControl(canvas, true);
light.position = new BABYLON.Vector3(20, 60, 30);
scene.ambientColor = BABYLON.Color3.FromInts(10, 30, 10);
scene.clearColor = BABYLON.Color3.FromInts(0, 0, 0);
scene.gravity = new BABYLON.Vector3(0, -0.5, 0);
var gunshot = new BABYLON.Sound("gunshot", "sounds/gunshot.wav", scene);
var music1 = new BABYLON.Sound(
“gunshot1”,
“sounds/gunshot.wav”,
scene,
soundReady,
{ loop: false }
);
var music2 = new BABYLON.Sound(
“gunshot2”,
“sounds/gunshot.wav”,
scene,
soundReady,
{ loop: false }
);
var music3 = new BABYLON.Sound(
“Cellolong”,
“sounds/cellolong.wav”,
scene,
soundReady,
{ loop: false }
);
var soundsReady = 0;
function soundReady() {
soundsReady++;
if (soundsReady === 3) {
music1.play();
music2.play(5);
music3.play(0.75);
}
}
// Fog
scene.fogMode = BABYLON.Scene.FOGMODE_EXP;
scene.fogDensity = 0.09;
scene.fogColor = scene.clearColor;
// Skybox
var skybox = BABYLON.Mesh.CreateBox("skyBox", 150.0, scene);
var skyboxMaterial = new BABYLON.StandardMaterial("skyBox", scene);
skyboxMaterial.backFaceCulling = false;
skyboxMaterial.reflectionTexture = new BABYLON.CubeTexture("textures/skybox", scene);
skyboxMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
skyboxMaterial.diffuseColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
skyboxMaterial.disableLighting = true;
skybox.material = skyboxMaterial;
skybox.infiniteDistance = true;
// Ground
var ground = BABYLON.Mesh.CreateGroundFromHeightMap(“ground”, “textures/heightMap.png”, 10000, 10000, 100, -40, 50, scene, false);
var groundMaterial = new BABYLON.StandardMaterial(“ground”, scene);
groundMaterial.diffuseTexture = new BABYLON.Texture(“textures/ground.jpg”, scene);
groundMaterial.diffuseTexture.uScale = 1000;
groundMaterial.diffuseTexture.vScale = 1000;
groundMaterial.specularColor = new BABYLON.Color3(0, 0, 0);
groundMaterial.emissiveColor = new BABYLON.Color3(0.3, 0.3, 0.3);
ground.material = groundMaterial;
ground.receiveShadows = true;
ground.checkCollisions = true;
ground.onReady = function () {
ground.optimize(10);
// Shadows
var shadowGenerator = new BABYLON.ShadowGenerator(1024, light);
// Trees
BABYLON.SceneLoader.ImportMesh("", "//www.babylonjs.com/assets/Tree/", "tree.babylon", scene, function (newMeshes) {
newMeshes[0].material.opacityTexture = null;
newMeshes[0].material.backFaceCulling = false;
newMeshes[0].isVisible = false;
newMeshes[0].position.y = ground.getHeightAtCoordinates(0, 0); // Getting height from ground object
shadowGenerator.getShadowMap().renderList.push(newMeshes[0]);
var range = 10000;
var count = 10000;
for (var index = 0; index < count; index++) {
var newInstance = newMeshes[0].createInstance("i" + index);
var x = range / 2 - Math.random() * range;
var z = range / 2 - Math.random() * range;
var y = ground.getHeightAtCoordinates(x, z); // Getting height from ground object
newInstance.position = new BABYLON.Vector3(x, y, z);
newInstance.rotate(BABYLON.Axis.Y, Math.random() * Math.PI * 2, BABYLON.Space.WORLD);
var scale = 0.5 + Math.random() * 2;
newInstance.scaling.addInPlace(new BABYLON.Vector3(scale, scale, scale));
shadowGenerator.getShadowMap().renderList.push(newInstance);
}
shadowGenerator.getShadowMap().refreshRate = 0; // We need to compute it just once
shadowGenerator.usePoissonSampling = true;
// 87 corresponds to 'w'
// 83 corresponds to ‘s’
// 65 corresponds to ‘a’
// 68 corresponds to ‘d’
// See more JavaScript KeyCodes here: https://keycode.info/
camera.keysUp.push(87);
camera.keysDown.push(83);
camera.keysLeft.push(65);
camera.keysRight.push(68);
camera.speed = 3;
camera.fov = 0.8;
camera.inertia = 0;
let canvas = scene.getEngine().getRenderingCanvas();
canvas.addEventListener(“click”, event => {
canvas.requestPointerLock = canvas.requestPointerLock || canvas.msRequestPointerLock || canvas.mozRequestPointerLock || canvas.webkitRequestPointerLock;
if(canvas.requestPointerLock) {
canvas.requestPointerLock();
}
}, false)
// Apply material to a box
var box1 = BABYLON.MeshBuilder.CreateBox("box", {}, scene);
box1.position = new BABYLON.Vector3(5, 0, -10);
box1.scaling.x = 9;
box1.scaling.y = 9;
box1.scaling.z = 9;
// Collisions
camera.checkCollisions = true;
camera.applyGravity = true;
box1.checkCollisions = true;
});
}
return scene;
};