1
0
Fork 0
This repository has been archived on 2024-01-06. You can view files and clone it, but cannot push or open issues or pull requests.
NUIT_INFO_2_DECEMBRE_2016/scripts/demo.js

198 lines
6.6 KiB
JavaScript
Raw Normal View History

2016-12-01 21:02:00 +01:00
'use strict';
function runDemo(canvasId) {
var canvas = document.getElementById(canvasId);
var engine = new BABYLON.Engine(canvas, true);
2016-12-01 22:31:33 +01:00
2016-12-01 21:06:23 +01:00
// Cr<43>ation de la sc<73>ne
2016-12-01 21:02:00 +01:00
var scene = new BABYLON.Scene(engine);
scene.gravity = new BABYLON.Vector3(0, -9.81, 0);
scene.collisionsEnabled = true;
2016-12-01 22:31:33 +01:00
2016-12-01 21:06:23 +01:00
// Ajout d'une cam<61>ra et de son contr<74>leur
2016-12-01 21:02:00 +01:00
var camera = new BABYLON.FreeCamera("MainCamera", new BABYLON.Vector3(0, 2.5, 5), scene);
camera.applyGravity = true;
camera.checkCollisions = true;
2016-12-01 22:31:33 +01:00
2016-12-01 21:02:00 +01:00
camera.speed = 0.5;
camera.angularSensibility = 1000;
2016-12-01 22:31:33 +01:00
2016-12-01 21:02:00 +01:00
camera.keysUp = [90]; // Touche Z
camera.keysDown = [83]; // Touche S
camera.keysLeft = [81]; // Touche Q
camera.keysRight = [68]; // Touche D;
scene.activeCamera.attachControl(canvas);
2016-12-01 21:06:23 +01:00
// Ajout d'une lumi<6D>re
2016-12-01 21:02:00 +01:00
var light = new BABYLON.PointLight("DirLight", new BABYLON.Vector3(0, 10, 0), scene);
light.diffuse = new BABYLON.Color3(1, 1, 1);
light.specular = new BABYLON.Color3(0.6, 0.6, 0.6);
light.intensity = 1.5;
2016-12-01 22:31:33 +01:00
2016-12-01 22:53:30 +01:00
var light2 = new BABYLON.PointLight("DirLight", new BABYLON.Vector3(0, 10, 80), scene);
light2.diffuse = new BABYLON.Color3(1, 1, 1);
light2.specular = new BABYLON.Color3(0.6, 0.6, 0.6);
light2.intensity = 1.5;
2016-12-01 21:02:00 +01:00
// On ajoute une skybox
createSkybox(scene);
2016-12-01 22:31:33 +01:00
2016-12-01 21:06:23 +01:00
// Enfin la sc<73>ne de d<>mo
2016-12-01 21:02:00 +01:00
createDemoScene(scene);
2016-12-01 22:31:33 +01:00
2016-12-01 21:02:00 +01:00
// Lancement de la boucle principale
engine.runRenderLoop(function() {
scene.render();
});
}
function createSkybox(scene) {
2016-12-01 21:06:23 +01:00
// Cr<43>ation d'une material
2016-12-01 21:02:00 +01:00
var sMaterial = new BABYLON.StandardMaterial("skyboxMaterial", scene);
sMaterial.backFaceCulling = false;
sMaterial.reflectionTexture = new BABYLON.CubeTexture("images/skybox/skybox", scene);
sMaterial.reflectionTexture.coordinatesMode = BABYLON.Texture.SKYBOX_MODE;
2016-12-01 22:31:33 +01:00
2016-12-01 21:06:23 +01:00
// Cr<43>ation d'un cube avec la material adapt<70>e
2016-12-01 21:02:00 +01:00
var skybox = BABYLON.Mesh.CreateBox("skybox", 250, scene);
skybox.material = sMaterial;
}
function createDemoScene(scene) {
2016-12-01 21:06:23 +01:00
// Cr<43>ation d'un sol
2016-12-01 22:30:23 +01:00
/*---------------------SALLE 1 -------------------------------*/
var groundSalle1 = BABYLON.Mesh.CreatePlane("groundSalle1", 50, scene);
groundSalle1.rotation.x = Math.PI / 2;
groundSalle1.material = new BABYLON.StandardMaterial("gMaterial", scene);
groundSalle1.material.diffuseTexture = new BABYLON.Texture("images/ground.png", scene);
groundSalle1.checkCollisions = true;
var mur1Salle1 = BABYLON.Mesh.CreatePlane("mur1Salle1", 50, scene);
mur1Salle1.rotation.y = Math.PI / 2;
mur1Salle1.position.y = 20;
mur1Salle1.position.x = 25;
mur1Salle1.visibility = 0;
mur1Salle1.checkCollisions = true;
var mur2Salle1 = BABYLON.Mesh.CreatePlane("mur2Salle1", 50, scene);
mur2Salle1.rotation.y = -(Math.PI / 2);
mur2Salle1.position.y = 20;
mur2Salle1.position.x = -25;
mur2Salle1.visibility = 0;
mur2Salle1.checkCollisions = true;
var mur31Salle1 = BABYLON.Mesh.CreatePlane("mur31Salle1", 20, scene);
mur31Salle1.rotation.z = Math.PI / 2;
mur31Salle1.position.z = 25;
mur31Salle1.position.x = -15;
mur31Salle1.visibility = 0;
mur31Salle1.checkCollisions = true;
var mur32Salle1 = BABYLON.Mesh.CreatePlane("mur32Salle1", 20, scene);
mur32Salle1.rotation.z = Math.PI / 2;
mur32Salle1.position.z = 25;
mur32Salle1.position.x = 15;
mur32Salle1.visibility = 0;
mur32Salle1.checkCollisions = true;
var mur4Salle1 = BABYLON.Mesh.CreatePlane("mur4Salle1", 50, scene);
mur4Salle1.rotation.x = Math.PI;
mur4Salle1.position.z = -25;
mur4Salle1.visibility = 0;
mur4Salle1.checkCollisions = true;
/*---------------------COULOIR 1 -------------------------------*/
var groundCouloir1 = BABYLON.Mesh.CreateGround("groundCouloir1", 10, 20, 2, scene);
groundCouloir1.material = new BABYLON.StandardMaterial("gMaterial", scene);
groundCouloir1.material.diffuseTexture = new BABYLON.Texture("images/ground.png", scene);
groundCouloir1.position.z = 35;
groundCouloir1.checkCollisions = true;
var mur1Couloir1 = BABYLON.Mesh.CreatePlane("mur1Couloir1", 20, scene);
mur1Couloir1.rotation.y = Math.PI / 2;
mur1Couloir1.position.z = 35;
mur1Couloir1.position.x = 5;
mur1Couloir1.visibility = 0;
mur1Couloir1.checkCollisions = true;
var mur2Couloir1 = BABYLON.Mesh.CreatePlane("mur2Couloir1", 20, scene);
mur2Couloir1.rotation.y = -(Math.PI / 2);
mur2Couloir1.position.z = 35;
mur2Couloir1.position.x = -5;
mur2Couloir1.visibility = 0;
mur2Couloir1.checkCollisions = true;
2016-12-01 22:31:33 +01:00
2016-12-01 22:53:30 +01:00
/*-----------------SALLE 2----------------------------------*/
var groundSalle2 = BABYLON.Mesh.CreatePlane("groundSalle2", 50, scene);
groundSalle2.rotation.x = Math.PI / 2;
groundSalle2.position.z = 70;
groundSalle2.material = new BABYLON.StandardMaterial("gMaterial", scene);
groundSalle2.material.diffuseTexture = new BABYLON.Texture("images/ground.png", scene);
groundSalle2.checkCollisions = true;
var mur11Salle2 = BABYLON.Mesh.CreatePlane("mur11Salle2", 20, scene);
mur11Salle2.rotation.x = Math.PI;
mur11Salle2.position.z = 45;
mur11Salle2.position.x = 15;
mur11Salle2.visibility = 0;
mur11Salle2.checkCollisions = true;
var mur12Salle2 = BABYLON.Mesh.CreatePlane("mur12Salle2", 20, scene);
mur12Salle2.rotation.x = Math.PI;
mur12Salle2.position.z = 45;
mur12Salle2.position.x = -15;
mur12Salle2.visibility = 0;
mur12Salle2.checkCollisions = true;
var mur2Salle2 = BABYLON.Mesh.CreatePlane("mur2Salle2", 50, scene);
mur2Salle2.rotation.y = -(Math.PI / 2);
mur2Salle2.position.y = 20;
mur2Salle2.position.x = -25;
mur2Salle2.position.z = 70;
mur2Salle2.visibility = 0;
mur2Salle2.checkCollisions = true;
var mur31Salle1 = BABYLON.Mesh.CreatePlane("mur31Salle1", 20, scene);
mur31Salle1.rotation.z = Math.PI / 2;
mur31Salle1.position.z = 95;
mur31Salle1.position.x = -15;
mur31Salle1.visibility = 0;
mur31Salle1.checkCollisions = true;
var mur32Salle1 = BABYLON.Mesh.CreatePlane("mur32Salle1", 20, scene);
mur32Salle1.rotation.z = Math.PI / 2;
mur32Salle1.position.z = 95;
mur32Salle1.position.x = 15;
mur32Salle1.visibility = 0;
mur32Salle1.checkCollisions = true;
var mur4Salle2 = BABYLON.Mesh.CreatePlane("mur4Salle2", 50, scene);
mur4Salle2.rotation.y = Math.PI / 2;
mur4Salle2.position.y = 20;
mur4Salle2.position.x = 25;
mur4Salle2.position.z = 70;
mur4Salle2.visibility = 0;
mur4Salle2.checkCollisions = true;
2016-12-01 21:02:00 +01:00
// Et quelques cubes...
var boxMaterial = new BABYLON.StandardMaterial("bMaterial", scene);
boxMaterial.diffuseTexture = new BABYLON.Texture("images/box.png", scene);
2016-12-01 22:31:33 +01:00
2016-12-01 21:02:00 +01:00
var positions = [
{ x: -15, z: 15 },
{ x: -15, z: -15 },
{ x: 15, z: 15 },
{ x: 15, z: -15 }
];
2016-12-01 22:31:33 +01:00
2016-12-01 21:02:00 +01:00
var cubeSize = 2.5;
2016-12-01 22:31:33 +01:00
2016-12-01 21:02:00 +01:00
for (var i = 0; i < 4; i++) {
var box = BABYLON.Mesh.CreateBox("box1", cubeSize, scene);
box.position = new BABYLON.Vector3(positions[i].x, cubeSize / 2, positions[i].z);
box.material = boxMaterial;
box.checkCollisions = true;
}
2016-12-01 22:31:33 +01:00
}