Spaces:
Running
Running
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8" /> | |
| <meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
| <title>Babylon.js 3D VR Game</title> | |
| <style> | |
| body, html { margin: 0; padding: 0; overflow: hidden; font-family: 'Trebuchet MS', serif; } | |
| canvas { width: 100%; height: 100%; display: block; } | |
| #chatUI { position: absolute; top: 10px; right: 10px; width: 300px; } | |
| #chatLog { width: 100%; height: 400px; background-color: rgba(0, 0, 0, 0.7); padding: 10px; color: #fff; overflow-y: auto; border: 2px solid #4e342e; box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.9); } | |
| #chatInput { width: 100%; height: 40px; margin-top: 10px; background-color: rgba(255, 255, 255, 0.4); border: 2px solid #4e342e; } | |
| #sendButton { width: 100%; height: 40px; margin-top: 10px; background: linear-gradient(to bottom, #957d5f, #6c543e); color: #e0d7c5; border: none; cursor: pointer; } | |
| </style> | |
| </head> | |
| <body> | |
| <canvas id="renderCanvas"></canvas> | |
| <div id="chatUI"> | |
| <div id="chatLog"></div> | |
| <input type="text" id="chatInput" placeholder="Type your message here..." /> | |
| <button id="sendButton">Send</button> | |
| </div> | |
| <script src="https://cdn.babylonjs.com/babylon.js"></script> | |
| <script src="https://cdn.babylonjs.com/loaders/babylon.glTF2FileLoader.min.js"></script> | |
| <script src="https://unpkg.com/@huggingface/transformers"></script> | |
| <script> | |
| // Setup Babylon.js | |
| const canvas = document.getElementById("renderCanvas"); | |
| const engine = new BABYLON.Engine(canvas, true); | |
| const createScene = function() { | |
| const scene = new BABYLON.Scene(engine); | |
| // Camera | |
| const camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 10, BABYLON.Vector3.Zero(), scene); | |
| camera.attachControl(canvas, true); | |
| // Light | |
| const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene); | |
| // Ground | |
| const ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 10, height: 10 }, scene); | |
| // Player and NPCs | |
| const player = BABYLON.MeshBuilder.CreateBox("player", { size: 1 }, scene); | |
| player.position.y = 0.5; | |
| const npc1 = BABYLON.MeshBuilder.CreateSphere("npc1", { diameter: 1 }, scene); | |
| npc1.position.x = 3; | |
| npc1.position.y = 0.5; | |
| // Basic movement | |
| window.addEventListener("keydown", (event) => { | |
| switch (event.key) { | |
| case "ArrowUp": | |
| player.position.z -= 0.1; | |
| break; | |
| case "ArrowDown": | |
| player.position.z += 0.1; | |
| break; | |
| case "ArrowLeft": | |
| player.position.x -= 0.1; | |
| break; | |
| case "ArrowRight": | |
| player.position.x += 0.1; | |
| break; | |
| } | |
| checkCollision(player, npc1); | |
| }); | |
| function checkCollision(mesh1, mesh2) { | |
| if (mesh1.intersectsMesh(mesh2)) { | |
| openChat("npc1", "NPC-1", "Hello! How can I assist you today?"); | |
| } | |
| } | |
| return scene; | |
| }; | |
| const scene = createScene(); | |
| engine.runRenderLoop(function() { | |
| scene.render(); | |
| }); | |
| window.addEventListener("resize", function() { | |
| engine.resize(); | |
| }); | |
| async function createXRExperience() { | |
| try { | |
| const xrHelper = await scene.createDefaultXRExperienceAsync({ | |
| floorMeshes: [scene.getMeshByName("ground")], | |
| // Do not explicitly disable all optional features; let the default setup run | |
| disableDefaultUI: false // Keep the default UI enabled to ensure the VR button works | |
| }); | |
| // After initialization, specifically disable hand tracking if it was included | |
| const featuresManager = xrHelper.baseExperience.featuresManager; | |
| // This explicitly disables hand tracking if it was somehow included by default | |
| featuresManager.disableFeature(BABYLON.WebXRFeatureName.HAND_TRACKING); | |
| // Set up interactions | |
| setupControllerInteractions(xrHelper); | |
| scene.activeCamera = xrHelper.baseExperience.camera; | |
| } catch (err) { | |
| console.error('Failed to start VR session:', err); | |
| } | |
| } | |
| // Set up VR button and initialize VR experience on click | |
| const vrButton = document.createElement("button"); | |
| vrButton.textContent = "Enter VR"; | |
| vrButton.style.position = "absolute"; | |
| vrButton.style.bottom = "10px"; | |
| vrButton.style.left = "10px"; | |
| vrButton.style.width = "200px"; | |
| vrButton.style.height = "40px"; | |
| vrButton.style.background = "linear-gradient(to bottom, #6c543e, #4e342e)"; | |
| vrButton.style.color = "#e0d7c5"; | |
| vrButton.style.border = "none"; | |
| vrButton.style.cursor = "pointer"; | |
| document.body.appendChild(vrButton); | |
| vrButton.addEventListener("click", createXRExperience); | |
| function setupControllerInteractions(xrHelper) { | |
| xrHelper.input.onControllerAddedObservable.add(controller => { | |
| controller.onMotionControllerInitObservable.add(motionController => { | |
| const thumbstick = motionController.getComponent("xr-standard-thumbstick"); | |
| if (thumbstick) { | |
| thumbstick.onAxisValueChangedObservable.add((axisValues) => { | |
| const player = scene.getMeshByName("player"); | |
| // Map thumbstick input to movement | |
| const speed = 0.05; // Adjust this value for faster/slower movement | |
| player.position.x += axisValues.x * speed; // Horizontal movement (left-right) | |
| player.position.z += axisValues.y * speed; // Vertical movement (forward-backward) | |
| }); | |
| } | |
| }); | |
| }); | |
| } | |
| // Chat System with Transformers.js | |
| async function setupChat() { | |
| try { | |
| const model = await transformers.AutoModelForCausalLM.from_pretrained('mistralai/Mistral'); | |
| const tokenizer = await transformers.AutoTokenizer.from_pretrained('mistralai/Mistral'); | |
| return { model, tokenizer }; | |
| } catch (error) { | |
| console.error('Failed to load the chat model or tokenizer:', error); | |
| alert('Chat system is currently unavailable. Please try again later.'); | |
| return null; | |
| } | |
| } | |
| let chatSetup = setupChat(); | |
| document.getElementById("sendButton").addEventListener("click", async () => { | |
| const inputField = document.getElementById("chatInput"); | |
| const chatLog = document.getElementById("chatLog"); | |
| const message = inputField.value; | |
| if (message.trim() === "" || !chatSetup) return; | |
| chatLog.innerHTML += `<p>Player: ${message}</p>`; | |
| inputField.value = ""; | |
| const { model, tokenizer } = await chatSetup; | |
| const inputs = tokenizer(message, { return_tensors: "pt" }); | |
| const response = await model.generate(inputs.input_ids, { max_length: 50 }); | |
| const output = tokenizer.decode(response[0], { skip_special_tokens: true }); | |
| chatLog.innerHTML += `<p>NPC: ${output}</p>`; | |
| chatLog.scrollTop = chatLog.scrollHeight; | |
| // Limit chat log entries to 50 | |
| if (chatLog.childElementCount > 50) { | |
| chatLog.removeChild(chatLog.firstChild); | |
| } | |
| }); | |
| function openChat(npcId, npcName, greeting) { | |
| const chatLog = document.getElementById("chatLog"); | |
| chatLog.innerHTML += `<p>${npcName}: ${greeting}</p>`; | |
| } | |
| </script> | |
| </body> | |
| </html> | |