Конечно! Вот пример простейшей сцены с использованием библиотеки Three.js, создающей небольшую комнату с кубом возле стены:
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Three.js Room with Cube</title>
<style>
body { margin: 0; }
canvas { display: block; }
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
// Создаем сцену
const scene = new THREE.Scene();
// Создаем камеру
const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// Создаем рендерер
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// Создаем стены
const wallGeometry = new THREE.PlaneGeometry(10, 10);
const wallMaterial = new THREE.MeshBasicMaterial({color: 0xaaaaaa, side: THREE.DoubleSide});
// Левая стена
const leftWall = new THREE.Mesh(wallGeometry, wallMaterial);
leftWall.rotation.y = Math.PI / 2;
leftWall.position.x = -5;
scene.add(leftWall);
// Правая стена
const rightWall = new THREE.Mesh(wallGeometry, wallMaterial);
rightWall.rotation.y = -Math.PI / 2;
rightWall.position.x = 5;
scene.add(rightWall);
// Задняя стена
const backWall = new THREE.Mesh(wallGeometry, wallMaterial);
backWall.position.z = -5;
scene.add(backWall);
// Создаем пол
const floor = new THREE.Mesh(wallGeometry, wallMaterial);
floor.rotation.x = -Math.PI / 2;
floor.position.y = -5;
scene.add(floor);
// Создаем куб
const cubeGeometry = new THREE.BoxGeometry();
const cubeMaterial = new THREE.MeshBasicMaterial({color: 0x0077ff});
const cube = new THREE.Mesh(cubeGeometry, cubeMaterial);
cube.position.set(1, -4.5, -4); // Позиция куба рядом со стеной
scene.add(cube);
// Функция анимации
function animate() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
}
// Запуск анимации
animate();
// Обработка изменения размера окна
window.addEventListener('resize', function() {
const width = window.innerWidth;
const height = window.innerHeight;
renderer.setSize(width, height);
camera.aspect = width / height;
camera.updateProjectionMatrix();
});
</script>
</body>
</html>
Этот пример создает комнату с тремя стенами и полом, а также куб, расположенный рядом с одной из стен. Вы можете открыть этот код в браузере, и он отобразит простую трехмерную сцену.