Documented code

This commit is contained in:
Ronan 2021-08-25 22:54:50 +02:00
parent 0878258425
commit 4d9a5c1ca3
2 changed files with 77 additions and 28 deletions

View file

@ -12,10 +12,6 @@ import {
import { import {
GLTFLoader GLTFLoader
} from 'https://cdn.jsdelivr.net/npm/three@0.119.1/examples/jsm/loaders/GLTFLoader.js'; } from 'https://cdn.jsdelivr.net/npm/three@0.119.1/examples/jsm/loaders/GLTFLoader.js';
import {
RGBELoader
} from 'https://cdn.jsdelivr.net/npm/three@0.119.1/examples/jsm/loaders/RGBELoader.js';
var container, stats, controls; var container, stats, controls;
var camera, scene, renderer; var camera, scene, renderer;
@ -27,9 +23,16 @@ render();
var height, width; var height, width;
/**
* Initializes the 3D plan
* Creates and loads every needed things
*/
function init() { function init() {
//
// Creates HTML
//
container = document.createElement('div'); container = document.createElement('div');
container.id = 'map3d'; container.id = 'map3d';
@ -37,14 +40,18 @@ function init() {
width = document.querySelector('#maps').clientWidth; width = document.querySelector('#maps').clientWidth;
var svg = document.querySelector('#maps #map'); var svg = document.querySelector('#maps #map');
document.querySelector('#maps').insertBefore(container, svg); document.querySelector('#maps').insertBefore(container, svg);
//
// Creates cameras and scene
//
camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.75, 20000); camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.75, 20000);
camera.position.set(500,1500,500); camera.position.set(500,1500,500);
scene = new THREE.Scene(); scene = new THREE.Scene();
//
// LIGHTS // LIGHTS
//
let sol = new THREE.AmbientLight(0x404040, 1.0); let sol = new THREE.AmbientLight(0x404040, 1.0);
scene.add(sol); scene.add(sol);
@ -58,16 +65,23 @@ function init() {
//scene.background = new THREE.Color( 0xff0000 ); //scene.background = new THREE.Color( 0xff0000 );
raycaster = new THREE.Raycaster(); raycaster = new THREE.Raycaster();
mouse = new THREE.Vector2() mouse = new THREE.Vector2()
//
// Loading screen // Loading screen
//
const loadingManager = new THREE.LoadingManager( () => { const loadingManager = new THREE.LoadingManager( () => {
const loadingScreen = document.getElementById('loading-screen'); const loadingScreen = document.getElementById('loading-screen');
loadingScreen.classList.add('fade-out'); loadingScreen.classList.add('fade-out');
loadingScreen.addEventListener('transitionend', onTransitionEnd); loadingScreen.addEventListener('transitionend', onTransitionEnd);
}); });
//
// Load the 3D model
//
var loader = new GLTFLoader(loadingManager); var loader = new GLTFLoader(loadingManager);
loader.load('assets/images/map3D.glb', function(gltf) { loader.load('assets/images/map3D.glb', function(gltf) {
@ -85,6 +99,9 @@ function init() {
//
// RENDERER
//
renderer = new THREE.WebGLRenderer({ renderer = new THREE.WebGLRenderer({
antialias: true, antialias: true,
}); });
@ -100,6 +117,9 @@ function init() {
var pmremGenerator = new THREE.PMREMGenerator(renderer); var pmremGenerator = new THREE.PMREMGenerator(renderer);
pmremGenerator.compileEquirectangularShader(); pmremGenerator.compileEquirectangularShader();
//
// CONTROLS
//
controls = new OrbitControls(camera, renderer.domElement); controls = new OrbitControls(camera, renderer.domElement);
controls.addEventListener('change', render); // use if there is no animation loop controls.addEventListener('change', render); // use if there is no animation loop
controls.minDistance = 0; controls.minDistance = 0;
@ -109,7 +129,9 @@ function init() {
controls.maxPolarAngle = Math.PI/2.05; controls.maxPolarAngle = Math.PI/2.05;
controls.update(); controls.update();
//
// Load Light // Load Light
//
var ambientLight = new THREE.AmbientLight( 0xcccccc ); var ambientLight = new THREE.AmbientLight( 0xcccccc );
scene.add( ambientLight ); scene.add( ambientLight );
@ -117,17 +139,22 @@ function init() {
directionalLight.position.set( 0, 1, 1 ).normalize(); directionalLight.position.set( 0, 1, 1 ).normalize();
scene.add( directionalLight ); scene.add( directionalLight );
//
// EVENTS
//
window.addEventListener('resize', onWindowResize, false); window.addEventListener('resize', onWindowResize, false);
renderer.domElement.addEventListener('click', onClick, false); // Mouse renderer.domElement.addEventListener('click', onClick, false); // Mouse
//renderer.domElement.addEventListener('mousemove', onMouseOver,false); //renderer.domElement.addEventListener('mousemove', onMouseOver,false);
renderer.domElement.addEventListener('touchend', onTouchEnd, false); // Smartphone renderer.domElement.addEventListener('touchend', onTouchEnd, false); // Smartphone
} }
// Return all the selectors in the database /**
* Get all the selectors (buildings identifiers)
* @returns Array with all the selectors
*/
function getSelectors() { function getSelectors() {
let info = {}; let info = {};
let object = { let object = {
@ -160,9 +187,8 @@ function handleClickOnBuilding(x,y) {
// If we clicked on a building // If we clicked on a building
if (intersects.length > 0) { if (intersects.length > 0) {
// console.log(intersects);
//console.log( intersects[0].object.name.toString()); var selector = intersects[0].object.name.toString().toLowerCase(); // Name of the building we clicked on
var selector = intersects[0].object.name.toString().toLowerCase();
// Wait for getSelectors() to be done // Wait for getSelectors() to be done
// If we do not wait, everything will be executed before checking what is inside the database // If we do not wait, everything will be executed before checking what is inside the database
@ -203,9 +229,9 @@ function handleClickOnBuilding(x,y) {
} }
} }
// /**
// When the user clicks on a building with a mouse * Get the position where the user clicked (mouse) on a building and process it
// */
function onClick() { function onClick() {
event.preventDefault(); event.preventDefault();
@ -221,9 +247,9 @@ function onClick() {
} }
// /**
// When the user clicks on a building on a smartphone * Get the position where the user clicked (smartphone) on a building and process it
// */
function onTouchEnd() { function onTouchEnd() {
var clientX, clientY; var clientX, clientY;
@ -237,6 +263,11 @@ function onTouchEnd() {
handleClickOnBuilding(mouse.x, mouse.y); handleClickOnBuilding(mouse.x, mouse.y);
} }
/**
* Process something when the user moved the mouse over a building
* @todo add text over the building
*/
function onMouseOver() { function onMouseOver() {
event.preventDefault(); event.preventDefault();
@ -313,9 +344,10 @@ function makeLabelCanvas(baseWidth, size, name) {
return ctx.canvas; return ctx.canvas;
} }
//
// Auto-resize the canvas /**
// * Auto-resizes the canvas when window size is updated
*/
function onWindowResize() { function onWindowResize() {
height = document.querySelector('#main-content .inner').clientHeight; height = document.querySelector('#main-content .inner').clientHeight;
@ -323,19 +355,22 @@ function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight; camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix(); camera.updateProjectionMatrix();
renderer.setSize(width * 0.9, window.innerHeight * 0.75); renderer.setSize(width * 0.9, window.innerHeight * 0.75); // 0.9 and 0.75 so it looks comfortable on the screen
render(); render();
} }
/**
* Render the scene
*/
function render() { function render() {
renderer.render(scene, camera); renderer.render(scene, camera);
} }
/* /*
* When the model finished loading * Remove the loader when the model loaded
*/ */
function onTransitionEnd( event) { function onTransitionEnd( event) {
event.target.remove(); event.target.remove();

View file

@ -107,25 +107,39 @@ class Dao
} }
} }
/**
* Add a building in the database
* @param String $title = Name of the building displayed
* @param String $description = Description of the building
* @param String $selector = Identifier of the building (unique)
* @return Mixed = if error : false
* else : Array of the row added as an array indexed by column name
*/
public function create_building($title, $description, $selector) { public function create_building($title, $description, $selector) {
$sql = 'INSERT INTO map_insa (title, description, selector) VALUES(:title, :description, :selector)'; $sql = 'INSERT INTO map_insa (title, description, selector) VALUES(:title, :description, :selector)';
//var_dump($title, $description, $selector);
$query = $this->conn->prepare($sql); $query = $this->conn->prepare($sql);
$query->execute(array( $query->execute(array(
':title' => $title, ':title' => $title,
':description' => $description, ':description' => $description,
':selector' => $selector, ':selector' => $selector,
)); ));
//var_dump($query->errorInfo());
return $query->fetch(PDO::FETCH_ASSOC); return $query->fetch(PDO::FETCH_ASSOC);
} }
/**
* Remove a building in the database
* @param String $selector = Identifier of the building (unique)
* @return Mixed = if error : false
* else : Array with the selector used to remove from the database
*/
public function delete_building($selector) { public function delete_building($selector) {
$sql = 'DELETE FROM map_insa WHERE selector=?'; $sql = 'DELETE FROM map_insa WHERE selector=?';
//var_dump($selector);
$query = $this->conn->prepare($sql); $query = $this->conn->prepare($sql);
$query->execute([$selector]); $query->execute([$selector]);
//var_dump($query->errorInfo());
return $query->fetch(PDO::FETCH_ASSOC); return $query->fetch(PDO::FETCH_ASSOC);
} }
} }