oprav kod, aby byl funkcni <!DOCTYPE html> <html lang="cs"> <head> <meta…
- User
oprav kod, aby byl funkcni
code - Assistant
oprav kod, aby byl funkcni
<script>
let scene, camera, renderer, controls;
let circles = [], points = [], selectedPoints = [0, 0, 0];
let shapes = [], fillShapes = false;
let pointCount = 10;
function init() {
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(75, (window.innerWidth - 400) / window.innerHeight, 0.1, 1000);
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth - 400, window.innerHeight);
document.getElementById('scene').appendChild(renderer.domElement);
// Orbit controls pro otáčení scény
controls = new THREE.OrbitControls(camera, renderer.domElement);
// Inicializace kružnic
for (let i = 0; i < 3; i++) {
let geometry = new THREE.TorusGeometry(5, 0.1, 16, 100);
let material = new THREE.MeshBasicMaterial({ color: 0xffffff });
let torus = new THREE.Mesh(geometry, material);
circles.push(torus);
scene.add(torus);
}
circles[0].rotation.x = Math.PI / 2;
circles[1].rotation.y = Math.PI / 2;
camera.position.z = 15;
updatePointCount();
animate();
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
function updatePointCount() {
pointCount = parseInt(document.getElementById('pointCount').value);
for (let circle of circles) {
scene.remove(circle);
}
circles = [];
points = [];
for (let i = 0; i < 3; i++) {
let geometry = new THREE.TorusGeometry(5, 0.1, 16, 100);
let material = new THREE.MeshBasicMaterial({ color: 0xffffff });
let torus = new THREE.Mesh(geometry, material);
circles.push(torus);
scene.add(torus);
let circlePoints = [];
for (let j = 0; j < pointCount; j++) {
let angle = (j / pointCount) * Math.PI * 2;
let x = 5 * Math.cos(angle);
let y = 5 * Math.sin(angle);
let pointGeometry = new THREE.SphereGeometry(0.1);
let pointMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
let point = new THREE.Mesh(pointGeometry, pointMaterial);
if (i === 0) {
point.position.set(x, y, 0);
} else if (i === 1) {
point.position.set(x, 0, y);
} else {
point.position.set(0, x, y);
}
torus.add(point);
circlePoints.push(point);
}
points.push(circlePoints);
}
circles[0].rotation.x = Math.PI / 2;
circles[1].rotation.y = Math.PI / 2;
}
function selectCircle(index) {
selectedPoints[index] = (selectedPoints[index] + 1) % pointCount;
updatePointHighlight();
}
function updatePointHighlight() {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < pointCount; j++) {
points[i][j].material.color.setHex(j === selectedPoints[i] ? 0x00ff00 : 0xff0000);
}
}
}
function drawShape() {
// Odstranění předchozích tvarů
for (let shape of shapes) {
scene.remove(shape);
}
shapes = [];
let selectedPositions = selectedPoints.map((pointIndex, circleIndex) =>
points[circleIndex][pointIndex].getWorldPosition(new THREE.Vector3())
);
if (selectedPositions.every(pos => pos.distanceTo(selectedPositions[0]) > 0.1)) {
if (selectedPositions.length === 3) {
// Kreslení trojúhelníku
let geometry = new THREE.BufferGeometry().setFromPoints(selectedPositions);
let material = fillShapes
? new THREE.MeshBasicMaterial({ color: 0x00ff00, side: THREE.DoubleSide, transparent: true, opacity: 0.5 })
: new THREE.LineBasicMaterial({ color: 0x00ff00 });
let shape = fillShapes
? new THREE.Mesh(geometry, material)
: new THREE.Line(geometry, material);
scene.add(shape);
shapes.push(shape);
} else {
// Kreslení úsečky
let geometry = new THREE.BufferGeometry().setFromPoints(selectedPositions);
let material = new THREE.LineBasicMaterial({ color: 0x00ff00 });
let line = new THREE.Line(geometry, material);
scene.add(line);
shapes.push(line);
}
}
}
function toggleFill() {
fillShapes = !fillShapes;
drawShape();
}
function moveCircle(index) {
let circle = circles[index];
circle.rotation.z += (2 * Math.PI) / pointCount;
updatePointHighlight();
}
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
camera.aspect = (window.innerWidth - 400) / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth - 400, window.innerHeight);
}
init();
</script>
Ahoj! Rád ti pomohu opravit tvůj kód, aby správně fungoval. Hlavním problémem ve tvém kódu je chybějící zahrnutí OrbitControls, které jsou nezbytné pro ovládání kamery pomocí myši. Kromě toho jsem upravil několik dalších částí kódu, aby vše fungovalo hladce.
Níže je upravený a funkční kód:
<!DOCTYPE html>
<html lang="cs">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Geometrický Program</title>
<!-- Three.js knihovna -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<!-- OrbitControls -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/examples/js/controls/OrbitControls.js"></script>
<!-- dat.gui knihovna (není použita, můžeš ji odstranit pokud nechceš) -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.min.js"></script>
<style>
body { margin: 0; overflow: hidden; display: flex; font-family: Arial, sans-serif; height: 100vh; }
#controls, #controls-right { width: 200px; padding: 10px; background-color: #f0f0f0; box-sizing: border-box; }
#controls { border-right: 1px solid #ccc; }
#controls-right { border-left: 1px solid #ccc; }
#scene { flex-grow: 1; }
button { display: block; margin: 5px 0; width: 100%; padding: 10px; }
input { width: 100%; margin-bottom: 10px; padding: 5px; box-sizing: border-box; }
</style>
</head>
<body>
<div id="controls">
<label for="pointCount">Počet bodů:</label>
<input type="number" id="pointCount" min="1" value="10">
<button onclick="updatePointCount()">Nastavit body</button>
<button onclick="selectCircle(0)">Kružnice 1</button>
<button onclick="selectCircle(1)">Kružnice 2</button>
<button onclick="selectCircle(2)">Kružnice 3</button>
<button onclick="drawShape()">Zakreslit</button>
<button onclick="toggleFill()">Přepnout výplň</button>
</div>
<div id="scene"></div>
<div id="controls-right">
<button onclick="moveCircle(0)">Posunout kružnici 1</button>
<button onclick="moveCircle(1)">Posunout kružnici 2</button>
<button onclick="moveCircle(2)">Posunout kružnici 3</button>
</div>
<script>
let scene, camera, renderer, controls;
let circles = [], points = [], selectedPoints = [0, 0, 0];
let shapes = [], fillShapes = false;
let pointCount = 10;
function init() {
// Inicializace scény
scene = new THREE.Scene();
// Inicializace kamery
const aspect = (window.innerWidth - 400) / window.innerHeight;
camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
// Inicializace rendereru
renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(window.innerWidth - 400, window.innerHeight);
document.getElementById('scene').appendChild(renderer.domElement);
// Inicializace OrbitControls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enableDamping = true; // Pro hladší ovládání
// Inicializace kružnic
createCircles();
// Nastavení pozice kamery
camera.position.set(15, 15, 15);
camera.lookAt(0, 0, 0);
// Přidání světla pro lepší viditelnost (volitelné)
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);
// Spuštění animace
animate();
}
function createCircles() {
// Odstranění existujících kružnic z scény
for (let circle of circles) {
scene.remove(circle);
}
circles = [];
points = [];
for (let i = 0; i < 3; i++) {
let geometry = new THREE.TorusGeometry(5, 0.1, 16, 100);
let material = new THREE.MeshBasicMaterial({ color: 0xffffff });
let torus = new THREE.Mesh(geometry, material);
circles.push(torus);
scene.add(torus);
// Rotace kružnic pro různé orientace
if (i === 0) {
torus.rotation.x = Math.PI / 2;
} else if (i === 1) {
torus.rotation.y = Math.PI / 2;
}
// Vytvoření bodů na kružnicích
let circlePoints = [];
for (let j = 0; j < pointCount; j++) {
let angle = (j / pointCount) * Math.PI * 2;
let x = 5 * Math.cos(angle);
let y = 5 * Math.sin(angle);
let pointGeometry = new THREE.SphereGeometry(0.1, 16, 16);
let pointMaterial = new THREE.MeshBasicMaterial({ color: 0xff0000 });
let point = new THREE.Mesh(pointGeometry, pointMaterial);
if (i === 0) {
point.position.set(x, y, 0);
} else if (i === 1) {
point.position.set(x, 0, y);
} else {
point.position.set(0, x, y);
}
torus.add(point);
circlePoints.push(point);
}
points.push(circlePoints);
}
}
function animate() {
requestAnimationFrame(animate);
controls.update();
renderer.render(scene, camera);
}
function updatePointCount() {
pointCount = parseInt(document.getElementById('pointCount').value);
createCircles();
updatePointHighlight();
clearShapes();
}
function selectCircle(index) {
selectedPoints[index] = (selectedPoints[index] + 1) % pointCount;
updatePointHighlight();
}
function updatePointHighlight() {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < pointCount; j++) {
points[i][j].material.color.setHex(j === selectedPoints[i] ? 0x00ff00 : 0xff0000);
}
}
}
function drawShape() {
clearShapes();
let selectedPositions = selectedPoints.map((pointIndex, circleIndex) =>
points[circleIndex][pointIndex].getWorldPosition(new THREE.Vector3())
);
// Kontrola, zda jsou všechny body jedinečné
if (selectedPositions.every((pos, _, arr) =>
pos.distanceTo(arr[0]) > 0.1 || pos.equals(arr[0])
)) {
if (selectedPositions.length === 3) {
// Kreslení trojúhelníku
let geometry = new THREE.BufferGeometry();
const vertices = new Float32Array([
selectedPositions[0].x, selectedPositions[0].y, selectedPositions[0].z,
selectedPositions[1].x, selectedPositions[1].y, selectedPositions[1].z,
selectedPositions[2].x, selectedPositions[2].y, selectedPositions[2].z
]);
geometry.setAttribute('position', new THREE.BufferAttribute(vertices, 3));
if (fillShapes) {
geometry.setIndex([0, 1, 2]);
geometry.computeVertexNormals();
let material = new THREE.MeshBasicMaterial({ color: 0x00ff00, side: THREE.DoubleSide, transparent: true, opacity: 0.5 });
let mesh = new THREE.Mesh(geometry, material);
scene.add(mesh);
shapes.push(mesh);
} else {
let material = new THREE.LineBasicMaterial({ color: 0x00ff00 });
let line = new THREE.LineLoop(geometry, material);
scene.add(line);
shapes.push(line);
}
} else if (selectedPositions.length === 2) {
// Kreslení úsečky
let geometry = new THREE.BufferGeometry().setFromPoints(selectedPositions);
let material = new THREE.LineBasicMaterial({ color: 0x00ff00 });
let line = new THREE.Line(geometry, material);
scene.add(line);
shapes.push(line);
}
} else {
alert("Vyberte unikátní body pro kreslení tvaru.");
}
}
function toggleFill() {
fillShapes = !fillShapes;
drawShape();
}
function moveCircle(index) {
let circle = circles[index];
circle.rotation.z += (2 * Math.PI) / pointCount;
updatePointHighlight();
drawShape();
}
function clearShapes() {
for (let shape of shapes) {
scene.remove(shape);
}
shapes = [];
}
window.addEventListener('resize', onWindowResize, false);
function onWindowResize() {
const width = window.innerWidth - 400;
const height = window.innerHeight;
camera.aspect = width / height;
camera.updateProjectionMatrix();
renderer.setSize(width, height);
}
init();
</script>
</body>
</html>
Zahrnutí OrbitControls:
<script> tag pro OrbitControls z CDN, aby bylo možné ovládat kameru pomocí myši.<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/examples/js/controls/OrbitControls.js"></script>
Vylepšení stylů:
box-sizing: border-box; pro lepší manipulaci s paddingem a šířkou.Inicializace OrbitControls s enableDamping:
.enableDamping = true pro hladší ovládání scény.controls.enableDamping = true;
Optimalizace funkce drawShape:
Přidání funkce clearShapes:
Vylepšení responzivního chování:
Přidání světla (volitelné):
const ambientLight = new THREE.AmbientLight(0xffffff, 0.8);
scene.add(ambientLight);
const directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(10, 10, 10);
scene.add(directionalLight);
Odstranění nepoužívaných knihoven:
dat.gui, můžeš odstranit <script> tag pro tuto knihovnu.Validace vstupů:
Optimalizace výkonu:
Doufám, že tyto úpravy pomohou a tvůj 3D geometrický program bude fungovat podle očekávání! Pokud máš další otázky nebo potřebuješ další úpravy, neváhej se zeptat.
Not yet included in a published release.