diff --git a/COGNET/main.js b/COGNET/main.js index 379f630e740261345ab64830ce91b900847dd669..a6e6581ebff3dbb0dd1a263cdc826ced581fb0f3 100644 --- a/COGNET/main.js +++ b/COGNET/main.js @@ -4,43 +4,222 @@ import { OBJLoader } from 'three/addons/loaders/OBJLoader.js'; // Initialisation de la scène, de la caméra et du rendu const scene = new THREE.Scene(); -const renderer = new THREE.WebGLRenderer(); +const renderer = new THREE.WebGLRenderer({ antialias: true }); renderer.setSize(window.innerWidth, window.innerHeight); document.getElementById('webGL').appendChild(renderer.domElement); +// Define shared materials +const woodMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 }); +const wallMaterial = new THREE.MeshStandardMaterial({ color: 0xE8E8E8 }); +const floorMaterial = new THREE.MeshStandardMaterial({ color: 0x808080 }); +const ceilingMaterial = new THREE.MeshStandardMaterial({ color: 0xF5F5F5 }); + let camera; // Ajout des contrôles de la caméra let controls; function init() { - camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 20); - camera.position.z = 1.5; // Rapprochez la caméra + camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100); + camera.position.set(0, 2, 5); // Position the camera to see the room // scene - const ambientLight = new THREE.AmbientLight(0xffffff); + const ambientLight = new THREE.AmbientLight(0xffffff, 0.5); scene.add(ambientLight); const pointLight = new THREE.PointLight(0xffffff, 1); - camera.add(pointLight); + pointLight.position.set(0, 3, 0); // Light from ceiling + scene.add(pointLight); scene.add(camera); // renderer - renderer.setPixelRatio(window.devicePixelRatio); + renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)); renderer.setSize(window.innerWidth, window.innerHeight); renderer.setAnimationLoop(animate); // controls controls = new OrbitControls(camera, renderer.domElement); controls.minDistance = 1; - controls.maxDistance = 3; + controls.maxDistance = 10; + controls.enableDamping = true; + controls.dampingFactor = 0.05; // resize event window.addEventListener('resize', onWindowResize); + // Create the room + createRoom(); + + // Create the table + createTable(); + // Load the violin object with textures ObjectWithTextures(); } +function createRoom() { + // Load the stone texture + const textureLoader = new THREE.TextureLoader(); + const stoneTexture = textureLoader.load('textures/ft_stone01_n.png'); + + // Define shared materials - update floor to be white and ceiling to be light gray + const woodMaterial = new THREE.MeshStandardMaterial({ color: 0x8B4513 }); + const floorMaterial = new THREE.MeshStandardMaterial({ color: 0xFFFFFF }); // White floor + const ceilingMaterial = new THREE.MeshStandardMaterial({ color: 0xF5F5F5 }); + + + // Apply texture to the wall material with lighter gray color + const wallMaterial = new THREE.MeshStandardMaterial({ + color: 0xBBBBBB, // Lighter gray color (changed from 0x808080) + normalMap: stoneTexture, + normalScale: new THREE.Vector2(0.5, 0.5), + roughness: 0.7, + metalness: 0.2 + }); + + // Floor + const floorGeometry = new THREE.PlaneGeometry(10, 10); + const floor = new THREE.Mesh(floorGeometry, floorMaterial); + floor.rotation.x = -Math.PI / 2; + floor.position.y = -1; + scene.add(floor); + + // Ceiling + const ceilingGeometry = new THREE.PlaneGeometry(10, 10); + const ceiling = new THREE.Mesh(ceilingGeometry, ceilingMaterial); + ceiling.rotation.x = Math.PI / 2; + ceiling.position.y = 4; + scene.add(ceiling); + + // Wall 1 (back) + const wall1Geometry = new THREE.PlaneGeometry(10, 5); + const wall1 = new THREE.Mesh(wall1Geometry, wallMaterial); + wall1.position.set(0, 1.5, -5); + scene.add(wall1); + + // Wall 2 (right) - with window + // Instead of a full wall, create wall segments around the window + + // Top segment of wall 2 + const wall2TopGeometry = new THREE.PlaneGeometry(10, 1); + const wall2Top = new THREE.Mesh(wall2TopGeometry, wallMaterial); + wall2Top.position.set(5, 3.5, 0); + wall2Top.rotation.y = -Math.PI / 2; + scene.add(wall2Top); + + // Bottom segment of wall 2 + const wall2BottomGeometry = new THREE.PlaneGeometry(10, 2); // Increased height from 1 to 2 + const wall2Bottom = new THREE.Mesh(wall2BottomGeometry, wallMaterial); + wall2Bottom.position.set(5, 0, 0); // Lowered position from y: 0.5 to y: 0 + wall2Bottom.rotation.y = -Math.PI / 2; + scene.add(wall2Bottom); + + // Left segment of wall 2 + const wall2LeftGeometry = new THREE.PlaneGeometry(10 - 3, 2); + const wall2Left = new THREE.Mesh(wall2LeftGeometry, wallMaterial); + wall2Left.position.set(5, 2, -3.5 - 1.5); + wall2Left.rotation.y = -Math.PI / 2; + scene.add(wall2Left); + + // Right segment of wall 2 + const wall2RightGeometry = new THREE.PlaneGeometry(10 - 3, 2); + const wall2Right = new THREE.Mesh(wall2RightGeometry, wallMaterial); + wall2Right.position.set(5, 2, 3.5 + 1.5); + wall2Right.rotation.y = -Math.PI / 2; + scene.add(wall2Right); + + // Window pane + const windowGeometry = new THREE.PlaneGeometry(3, 2); + const windowMaterial = new THREE.MeshStandardMaterial({ + color: 0x87CEEB, + transparent: true, + opacity: 0.7 + }); + const windowMesh = new THREE.Mesh(windowGeometry, windowMaterial); + windowMesh.position.set(5.05, 2, 0); + windowMesh.rotation.y = -Math.PI / 2; + scene.add(windowMesh); + + // Window frame - Fix orientation issue + const frameTop = new THREE.Mesh( + new THREE.BoxGeometry(0.1, 0.1, 3.2), // Fixed orientation (width, height, depth) + woodMaterial + ); + frameTop.position.set(5.05, 3.05, 0); + scene.add(frameTop); + + const frameBottom = new THREE.Mesh( + new THREE.BoxGeometry(0.1, 0.1, 3.2), // Fixed orientation (width, height, depth) + woodMaterial + ); + frameBottom.position.set(5.05, 0.95, 0); + scene.add(frameBottom); + + const frameLeft = new THREE.Mesh( + new THREE.BoxGeometry(0.1, 2.2, 0.1), + woodMaterial + ); + frameLeft.position.set(5.05, 2, -1.55); + scene.add(frameLeft); + + const frameRight = new THREE.Mesh( + new THREE.BoxGeometry(0.1, 2.2, 0.1), + woodMaterial + ); + frameRight.position.set(5.05, 2, 1.55); + scene.add(frameRight); + + // Wall 3 (front) - with door + const wall3Geometry = new THREE.PlaneGeometry(10, 5); + const wall3 = new THREE.Mesh(wall3Geometry, wallMaterial); + wall3.position.set(0, 1.5, 5); + wall3.rotation.y = Math.PI; + scene.add(wall3); + + // Door hole (cut in wall3) + const doorGeometry = new THREE.BoxGeometry(2, 3, 0.3); + const door = new THREE.Mesh(doorGeometry, woodMaterial); + door.position.set(2, 0.5, 4.9); + scene.add(door); + + // Wall 4 (left) + const wall4Geometry = new THREE.PlaneGeometry(10, 5); + const wall4 = new THREE.Mesh(wall4Geometry, wallMaterial); + wall4.position.set(-5, 1.5, 0); + wall4.rotation.y = Math.PI / 2; + scene.add(wall4); +} + +function createTable() { + // Table top + const tableTopGeometry = new THREE.BoxGeometry(3, 0.1, 2); + const tableTop = new THREE.Mesh(tableTopGeometry, woodMaterial); + tableTop.position.set(0, 0, 0); + scene.add(tableTop); + + // Table legs geometry (shared) + const legGeometry = new THREE.BoxGeometry(0.1, 1, 0.1); + + // Leg 1 (front left) + const leg1 = new THREE.Mesh(legGeometry, woodMaterial); + leg1.position.set(-1.4, -0.5, 0.9); + scene.add(leg1); + + // Leg 2 (front right) + const leg2 = new THREE.Mesh(legGeometry, woodMaterial); + leg2.position.set(1.4, -0.5, 0.9); + scene.add(leg2); + + // Leg 3 (back left) + const leg3 = new THREE.Mesh(legGeometry, woodMaterial); + leg3.position.set(-1.4, -0.5, -0.9); + scene.add(leg3); + + // Leg 4 (back right) + const leg4 = new THREE.Mesh(legGeometry, woodMaterial); + leg4.position.set(1.4, -0.5, -0.9); + scene.add(leg4); +} + // Fonction pour charger l'objet avec des textures function ObjectWithTextures() { const textureLoader = new THREE.TextureLoader(); @@ -58,15 +237,15 @@ function ObjectWithTextures() { child.material.needsUpdate = true; } }); - object.position.set(0, 0, -1); // Position the object in front of the camera - object.scale.setScalar(0.05); // Augmentez l'échelle de l'objet + object.position.set(0, 0.15, 0); // Raised position slightly to avoid clipping with table + object.rotation.x = Math.PI / 2; // Lay the violin flat on the table + object.scale.setScalar(0.1); // Doubled the scale from 0.05 to 0.1 scene.add(object); }); } // Fonction d'animation function animate() { - requestAnimationFrame(animate); controls.update(); renderer.render(scene, camera); } @@ -77,5 +256,4 @@ function onWindowResize() { renderer.setSize(window.innerWidth, window.innerHeight); } -init(); -animate(); \ No newline at end of file +init(); \ No newline at end of file diff --git a/COGNET/textures/ft_stone01_n.png b/COGNET/textures/ft_stone01_n.png new file mode 100644 index 0000000000000000000000000000000000000000..f7581d42aa5eea4c4ac8951076fc1cb91a83c564 Binary files /dev/null and b/COGNET/textures/ft_stone01_n.png differ