File size: 3,823 Bytes
d5ae462 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | document.addEventListener('DOMContentLoaded', function() {
// Initialisation de la scène Three.js
const scene = new THREE.Scene();
scene.background = new THREE.Color(0xe9e9e9);
// Configuration du rendu
const renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setPixelRatio(window.devicePixelRatio);
renderer.setSize(window.innerWidth - 550, window.innerHeight - 60);
document.getElementById('render-container').appendChild(renderer.domElement);
// Configuration de la caméra
const camera = new THREE.PerspectiveCamera(75, (window.innerWidth - 550) / (window.innerHeight - 60), 0.1, 1000);
camera.position.set(5, 5, 5);
// Contrôles de la caméra
const controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true;
controls.dampingFactor = 0.05;
// Lumière
const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
directionalLight.position.set(1, 1, 1);
scene.add(directionalLight);
// Grille
const gridHelper = new THREE.GridHelper(20, 20);
scene.add(gridHelper);
// Axes
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);
// Animation
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
animate();
// Redimensionnement
window.addEventListener('resize', function() {
camera.aspect = (window.innerWidth - 550) / (window.innerHeight - 60);
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth - 550, window.innerHeight - 60);
});
// Gestion des boutons de formes
document.querySelectorAll('.shape-btn').forEach(button => {
button.addEventListener('click', function() {
const shapeType = this.getAttribute('data-shape');
let geometry;
switch(shapeType) {
case 'cube':
geometry = new THREE.BoxGeometry(1, 1, 1);
break;
case 'sphere':
geometry = new THREE.SphereGeometry(0.5, 32, 32);
break;
case 'cylinder':
geometry = new THREE.CylinderGeometry(0.5, 0.5, 1, 32);
break;
case 'cone':
geometry = new THREE.ConeGeometry(0.5, 1, 32);
break;
}
const material = new THREE.MeshStandardMaterial({
color: Math.random() * 0xffffff,
metalness: 0.2,
roughness: 0.7
});
const mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
// Ajout à l'historique
addToHistory(`Création: ${shapeType}`);
});
});
// Fonction pour ajouter à l'historique
function addToHistory(action) {
const historyList = document.getElementById('history-list');
const li = document.createElement('li');
li.textContent = action;
historyList.appendChild(li);
}
// Bouton Nouveau
document.getElementById('new-model').addEventListener('click', function() {
// Suppression de tous les objets sauf les aides
scene.children.forEach(object => {
if (!(object instanceof THREE.GridHelper) &&
!(object instanceof THREE.AxesHelper) &&
!(object instanceof THREE.Light)) {
scene.remove(object);
}
});
addToHistory('Nouvelle scène créée');
});
}); |