An error occurred while loading the file. Please try again.
index.html 16.45 KiB
<!doctype html>
<html lang="de">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>GeoVis AR Projekt</title>
  <style>
    body {
      margin: 0;
      font-family: Arial, sans-serif;
      background-color: #f0f0f0;
      color: #333;
      display: flex;
      justify-content: center;
      align-items: center;
      height: 100vh;
      background-image: url('https://www.hft-stuttgart.de/fileadmin/Dateien/Hochschule/-_R_Juergen_Pollak_HFT_18.04.18-0091.jpg');
      background-size: cover;
      background-position: center;
    .container {
      text-align: center;
      background-color: rgba(255, 255, 255, 0.9);
      padding: 50px;
      border-radius: 10px;
      color: #333;
      max-width: 80%;
      box-shadow: 0px 0px 15px rgba(0, 0, 0, 0.2);
    h1 {
      font-size: 3em;
      margin-bottom: 20px;
      color: #444;
    p {
      font-size: 1.2em;
      margin-bottom: 30px;
    #menu-bar {
      position: absolute;
      bottom: 0;
      width: 100%;
      height: 80px;
      display: flex;
      justify-content: space-around;
      align-items: center;
      background: #e0e0e0;
      padding: 10px;
      box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1);
      z-index: 10;
    .menu-item:hover {
      background-color: #d6d6d6;
      /* Etwas dunkleres Grau beim Hover */
      transform: scale(1.1);
      /* Leichte Vergrößerung beim Hover */
    .menu-item img {
      width: 50px;
      height: 50px;
    .menu-placeholder {
display: none; position: absolute; bottom: 0; width: 100%; height: 80px; display: flex; justify-content: space-around; align-items: center; background: #e0e0e0; /* Helles Grau */ box-shadow: 0 -2px 5px rgba(0, 0, 0, 0.1); z-index: 15; } .menu-placeholder .menu-item img { width: 50px; height: 50px; } button { background-color: #4CAF50; color: white; font-size: 1em; padding: 10px 20px; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease-in-out, transform 0.2s; box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1); margin: 10px; } button:hover { background-color: #45a049; } button:active { background-color: #387a39; } /* Confirmation Dialog */ #confirmation-dialog, #info-dialog { position: fixed; top: 0; left: 0; width: 100%; height: 100%; display: flex; justify-content: center; align-items: center; z-index: 20; } .dialog-overlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0, 0, 0, 0.5); } .dialog-box { position: relative; background: white; padding: 20px; border-radius: 8px; text-align: center; box-shadow: 0 4px 6px rgba(0, 0, 0, 0.2);
} .dialog-box button { margin: 5px; padding: 10px 20px; font-size: 16px; border: none; border-radius: 5px; cursor: pointer; } .dialog-box button:first-child { background-color: #e74c3c; color: white; } .dialog-box button:last-child { background-color: #3498db; color: white; } #dynamic-menu { position: absolute; bottom: 70px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 20px; padding: 20px; background: rgba(0, 0, 0, 0.8); box-shadow: 0 -2px 6px rgba(0, 0, 0, 0.2); color: white; width: 100%; height: 200px; z-index: 20; overflow-y: auto; } #dynamic-menu input[type="range"] { width: 100%; margin: 10px 0; } </style> <script src="https://unpkg.com/three@0.126.0/build/three.js"></script> <script src="https://unpkg.com/three@0.126.0/examples/js/loaders/GLTFLoader.js"></script> </head> <body> <!-- Standardmenü --> <div id="menu-bar" style="display: none;"> <div class="menu-item" id="edit-section" onclick="showMenu('edit-menu')"> <img src="previewImages/edit-icon.png" alt="Bearbeiten" /> </div> <div class="menu-item" id="add-section" onclick="showMenu('add-menu')"> <img src="previewImages/add-icon.png" alt="Hinzufügen" /> </div> <div class="menu-item" id="options-section" onclick="showMenu('options-menu')"> <img src="previewImages/options-icon.png" alt="Optionen" /> </div> </div> <!-- Hinzufügen-Menü --> <div id="add-menu" class="menu-placeholder" style="display: none;"></div> <!-- Bearbeiten-Menü --> <div id="edit-menu" class="menu-placeholder" style="display: none;"> <div class="menu-item" onclick="openRotationMenu()"> <img src="previewImages/rotate-icon.png" alt="Rotation" /> </div>
<div class="menu-item" onclick="openScaleMenu()"> <img src="previewImages/scale-icon.png" alt="Skalierung" /> </div> <div class="menu-item" onclick="deleteModel()"> <img src="previewImages/delete-icon.png" alt="Löschen" /> </div> <div class="menu-item" onclick="showMenu('menu-bar')"> <img src="previewImages/back-icon.png" alt="Zurück" /> </div> </div> <!-- Optionen-Menü --> <div id="options-menu" class="menu-placeholder" style="display: none;"> <div class="menu-item" onclick="exitAR()"> <img src="previewImages/exit-icon.png" alt="Beenden" /> </div> <div class="menu-item" onclick="showMenu('menu-bar')"> <img src="previewImages/back-icon.png" alt="Zurück" /> </div> </div> <!-- Dynamisches Menü --> <div id="dynamic-menu" style="display: none;"></div> <!-- Bestätigungsdialog --> <div id="confirmation-dialog" style="display: none;"> <div class="dialog-overlay"></div> <div class="dialog-box"> <p>Möchten Sie die AR-Session wirklich verlassen?</p> <button onclick="confirmExit(true)">Ja</button> <button onclick="confirmExit(false)">Nein</button> </div> </div> <!-- Informations-Dialog --> <div id="info-dialog" style="display: none;"> <div class="dialog-overlay"></div> <div class="dialog-box"> <p id="info-text">Hier kommt die Nachricht hin</p> <button onclick="closeInfoDialog()">OK</button> </div> </div> <script> // Variablen let selectedModel = 'robot'; let selectedPlacedModel = null; let currentSession = null; let reticle = null; let scene, camera; let models = { robot: { name: "Roboter", image: "previewImages/robot.png", file: "https://transfer.hft-stuttgart.de/gitlab/geovistoogsi/ar/-/raw/master/public/assets/models/bench_model/scene.gltf", scale: { x: 0.1, y: 0.1, z: 0.1 } }, sunflower: { name: "Sonnenblume", image: "previewImages/sunflower.png", file: "https://transfer.hft-stuttgart.de/gitlab/geovistoogsi/ar/-/raw/master/public/assets/models/trash_model/scene.gltf", scale: { x: 0.04, y: 0.04, z: 0.04 } }, tree: { name: "Baum", image: "previewImages/tree.png", file: "https://raw.githubusercontent.com/KhronosGroup/glTF-Sample-Assets/refs/heads/main/Models/Lantern/glTF/Lantern.gltf", scale: { x: 0.1, y: 0.1, z: 0.1 } }
}; const menus = ['menu-bar', 'add-menu', 'edit-menu', 'options-menu']; window.onload = () => { initializeAddMenu(); }; function initializeAddMenu() { const addMenu = document.getElementById('add-menu'); addMenu.innerHTML = Object.entries(models) .map( ([key, model]) => ` <div class="menu-item" id="${key}-item" onclick="selectModel('${key}')"> <img src="${model.image}" alt="${model.name}" /> </div> ` ) .join('') + ` <div class="menu-item" onclick="showMenu('menu-bar')"> <img src="previewImages/back-icon.png" alt="Zurück" /> </div> `; } function showMenu(menuId) { menus.forEach(id => { document.getElementById(id).style.display = id === menuId ? 'flex' : 'none'; }); closeDynamicMenu(); if (menuId === 'menu-bar') clearSelectedModel(); } function clearSelectedModel() { if (selectedPlacedModel) { selectedPlacedModel.traverse((child) => { if (child.isMesh) { child.material.emissive.setHex(0x000000); // Markierung entfernen } }); selectedPlacedModel = null; // Kein Modell mehr ausgewählt } } function selectModel(modelId) { const model = models[modelId]; if (model && model.file) { loadModel(model.file); showMenu('menu-bar'); } } function loadModel(filePath) { const modelConfig = Object.values(models).find(model => model.file === filePath); const loader = new THREE.GLTFLoader(); loader.load( filePath, (gltf) => { const model = gltf.scene; if (modelConfig && modelConfig.scale) { model.scale.set(modelConfig.scale.x, modelConfig.scale.y, modelConfig.scale.z); } model.position.copy(reticle.position); scene.add(model); selectedPlacedModel = model; highlightSelectedModel(); }, undefined, (error) => {
console.error("Fehler beim Laden des Modells:", error); } ); } function highlightSelectedModel() { if (selectedPlacedModel) { removeHighlightFromAllModels(); selectedPlacedModel.traverse((child) => { if (child.isMesh) { child.material.emissive.setHex(0xff0000); // Rote Hervorhebung } }); } } function removeHighlightFromAllModels() { scene.traverse((child) => { if (child.isMesh && child.material && child.material.emissive) { child.material.emissive.setHex(0x000000); // Markierung entfernen } }); } function removeHighlightFromSelectedModel() { if (selectedPlacedModel) { selectedPlacedModel.traverse((child) => { if (child.isMesh) { child.material.emissive.setHex(0x000000); // Markierung entfernen } }); selectedPlacedModel = null; // Kein Modell mehr ausgewählt } } function selectModelFromScene(event) { const mouse = new THREE.Vector2( (event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1 ); const raycaster = new THREE.Raycaster(); raycaster.setFromCamera(mouse, camera); const intersects = raycaster.intersectObjects(scene.children, true); if (intersects.length > 0) { selectedPlacedModel = intersects[0].object; highlightSelectedModel(); showMenu("edit-menu") } } function openRotationMenu() { if (!selectedPlacedModel) { showInfoDialog("Kein Modell ausgewählt. Bitte wählen Sie ein Modell aus, bevor Sie es bearbeiten."); return; } const dynamicMenu = document.getElementById("dynamic-menu"); dynamicMenu.style.display = "flex"; dynamicMenu.innerHTML = ` <h3>Rotation anpassen</h3> <label>Y-Achse: <input type="range" min="0" max="360" step="10" onchange="updateRotation('y', this.value)"></label> <button onclick="closeDynamicMenu()">Zurück</button> `; } function updateRotation(axis, value) { if (selectedPlacedModel) { const radians = (value / 180) * Math.PI;
selectedPlacedModel.rotation[axis] = radians; } } function openScaleMenu() { if (!selectedPlacedModel) { showInfoDialog("Kein Modell ausgewählt. Bitte wählen Sie ein Modell aus, bevor Sie es bearbeiten."); return; } const dynamicMenu = document.getElementById("dynamic-menu"); dynamicMenu.style.display = "flex"; dynamicMenu.innerHTML = ` <h3>Skalierung anpassen</h3> <label>Größe: <input type="range" min="0.1" max="3" step="0.1" onchange="updateScale(this.value)"></label> <button onclick="closeDynamicMenu()">Zurück</button> `; } function updateScale(value) { if (selectedPlacedModel) { const scale = parseFloat(value); selectedPlacedModel.scale.set(scale, scale, scale); } } function deleteModel() { if (selectedPlacedModel) { scene.remove(selectedPlacedModel); selectedPlacedModel = null; } } function closeDynamicMenu() { const dynamicMenu = document.getElementById("dynamic-menu"); dynamicMenu.style.display = "none"; } function showInfoDialog(message) { const infoDialog = document.getElementById("info-dialog"); const infoText = document.getElementById("info-text"); // Nachricht setzen infoText.textContent = message; // Dialog anzeigen infoDialog.style.display = "flex"; } function closeInfoDialog() { const infoDialog = document.getElementById("info-dialog"); infoDialog.style.display = "none"; } async function activateXR() { const canvas = document.createElement('canvas'); document.body.appendChild(canvas); const gl = canvas.getContext('webgl', { xrCompatible: true }); const renderer = new THREE.WebGLRenderer({ alpha: true, canvas, context: gl }); renderer.autoClear = false; scene = new THREE.Scene(); camera = new THREE.PerspectiveCamera(); camera.matrixAutoUpdate = false; const light = new THREE.DirectionalLight(0xffffff, 1); light.position.set(10, 10, 10); scene.add(light);
const loader = new THREE.GLTFLoader(); loader.load("https://immersive-web.github.io/webxr-samples/media/gltf/reticle/reticle.gltf", (gltf) => { reticle = gltf.scene; reticle.visible = false; scene.add(reticle); }); currentSession = await navigator.xr.requestSession('immersive-ar', { optionalFeatures: ["dom-overlay"], domOverlay: { root: document.body }, requiredFeatures: ['hit-test'] }); currentSession.updateRenderState({ baseLayer: new XRWebGLLayer(currentSession, gl) }); const referenceSpace = await currentSession.requestReferenceSpace('local'); const viewerSpace = await currentSession.requestReferenceSpace('viewer'); const hitTestSource = await currentSession.requestHitTestSource({ space: viewerSpace }); document.getElementById('menu-bar').style.display = 'flex'; canvas.addEventListener("pointerdown", selectModelFromScene); currentSession.requestAnimationFrame(function onXRFrame(time, frame) { currentSession.requestAnimationFrame(onXRFrame); gl.bindFramebuffer(gl.FRAMEBUFFER, currentSession.renderState.baseLayer.framebuffer); const pose = frame.getViewerPose(referenceSpace); if (pose) { const view = pose.views[0]; const viewport = currentSession.renderState.baseLayer.getViewport(view); renderer.setSize(viewport.width, viewport.height); camera.matrix.fromArray(view.transform.matrix); camera.projectionMatrix.fromArray(view.projectionMatrix); camera.updateMatrixWorld(true); const hitTestResults = frame.getHitTestResults(hitTestSource); if (hitTestResults.length > 0) { const hitPose = hitTestResults[0].getPose(referenceSpace); reticle.visible = true; reticle.position.set(hitPose.transform.position.x, hitPose.transform.position.y, hitPose.transform.position.z); reticle.updateMatrixWorld(true); } renderer.render(scene, camera); } }); } function exitAR() { document.getElementById('confirmation-dialog').style.display = 'flex'; } function confirmExit(shouldExit) { if (shouldExit && currentSession) { currentSession.end(); } document.getElementById('confirmation-dialog').style.display = 'none'; } if (navigator.xr) { const startButton = document.createElement('button'); startButton.textContent = 'Start AR'; startButton.style.cssText = "position: fixed; top: 50%; left: 50%; transform: translate(-50%, -50%); padding: 15px; font-size: 18px;"; document.body.appendChild(startButton); startButton.onclick = () => { startButton.remove(); activateXR(); }; } else {
alert('WebXR wird nicht unterstützt.'); } </script> </body> </html>