9Gamburger0
Ученик
(100),
на голосовании
11 месяцев назад
Как сделать чтобы Икосаэдр отображался в HTML и я мог его вращать, а то у меня он не показывается может ошибка в коде? вот части кода: 1. <button id="toggleButton" onclick="toggleObject()">Переключить объект</button> <script src= <script> const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000); const renderer = new THREE.WebGLRenderer(); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement);
2. let cube = createCube(); scene.add(cube);
const light = new THREE.PointLight(0xffffff, 1); light.position.set(2, 0, 5); scene.add(light);
3. function createCube() { const geometry = new THREE.BoxGeometry(); const material = new THREE.MeshStandardMaterial({ color: "#7442c8" }); return new THREE.Mesh(geometry, material); }
function createSpikyIcosahedron() { const geometry = new THREE.IcosahedronGeometry(1, 0); // Используем IcosahedronGeometry geometry.vertices.forEach((vertex) => { vertex.normalize().multiplyScalar(0.5); // Уменьшаем размер икосаэдра }); const material = new THREE.MeshStandardMaterial({ color: "#7442c8" }); const spikyIcosahedron = new THREE.Mesh(geometry, material); return spikyIcosahedron; }
4. function animate() { requestAnimationFrame(animate);
if (cube) { cube.rotation.x += rotateX; cube.rotation.y += rotateY; }
вот части кода:
1. <button id="toggleButton" onclick="toggleObject()">Переключить объект</button>
<script src=
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
2. let cube = createCube();
scene.add(cube);
const light = new THREE.PointLight(0xffffff, 1);
light.position.set(2, 0, 5);
scene.add(light);
3. function createCube() {
const geometry = new THREE.BoxGeometry();
const material = new THREE.MeshStandardMaterial({ color: "#7442c8" });
return new THREE.Mesh(geometry, material);
}
function createSpikyIcosahedron() {
const geometry = new THREE.IcosahedronGeometry(1, 0); // Используем IcosahedronGeometry
geometry.vertices.forEach((vertex) => {
vertex.normalize().multiplyScalar(0.5); // Уменьшаем размер икосаэдра
});
const material = new THREE.MeshStandardMaterial({ color: "#7442c8" });
const spikyIcosahedron = new THREE.Mesh(geometry, material);
return spikyIcosahedron;
}
4. function animate() {
requestAnimationFrame(animate);
if (cube) {
cube.rotation.x += rotateX;
cube.rotation.y += rotateY;
}
light.position.x += lightX;
light.position.y += lightY;
if (icosahedron) {
icosahedron.rotation.x += rotateX;
icosahedron.rotation.y += rotateY;
}
renderer.render(scene, camera);
}
animate();
document.addEventListener('keydown', (event) => {
switch (event.code) {
case 'ArrowLeft':
rotateY = -0.05;
break;
case 'ArrowRight':
rotateY = 0.05;
break;
case 'ArrowUp':
rotateX = -0.05;
break;
case 'ArrowDown':
rotateX = 0.05;
break;
case 'KeyW':
lightY = 0.15;
break;
case 'KeyS':
lightY = -0.15;
break;
case 'KeyA':
lightX = -0.15;
break;
case 'KeyD':
lightX = 0.15;
break;
default:
break;
}
});
5. function toggleObject() {
if (cube !== null) {
scene.remove(cube);
cube = null;
icosahedron = createSpikyIcosahedron();
scene.add(icosahedron);
} else {
scene.remove(icosahedron);
icosahedron = null;
cube = createCube();
scene.add(cube);
}
}
</script>
</body>
</html>