Spaces:
Running
Running
Create index.html
Browse files- index.html +201 -0
index.html
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<!DOCTYPE html>
|
2 |
+
<html lang="en">
|
3 |
+
|
4 |
+
<head>
|
5 |
+
<meta charset="UTF-8" />
|
6 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
7 |
+
<title>Babylon.js 3D VR Game</title>
|
8 |
+
<style>
|
9 |
+
body, html { margin: 0; padding: 0; overflow: hidden; font-family: 'Trebuchet MS', serif; }
|
10 |
+
canvas { width: 100%; height: 100%; display: block; }
|
11 |
+
#chatUI { position: absolute; top: 10px; right: 10px; width: 300px; }
|
12 |
+
#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); }
|
13 |
+
#chatInput { width: 100%; height: 40px; margin-top: 10px; background-color: rgba(255, 255, 255, 0.4); border: 2px solid #4e342e; }
|
14 |
+
#sendButton { width: 100%; height: 40px; margin-top: 10px; background: linear-gradient(to bottom, #957d5f, #6c543e); color: #e0d7c5; border: none; cursor: pointer; }
|
15 |
+
</style>
|
16 |
+
</head>
|
17 |
+
|
18 |
+
<body>
|
19 |
+
<canvas id="renderCanvas"></canvas>
|
20 |
+
<div id="chatUI">
|
21 |
+
<div id="chatLog"></div>
|
22 |
+
<input type="text" id="chatInput" placeholder="Type your message here..." />
|
23 |
+
<button id="sendButton">Send</button>
|
24 |
+
</div>
|
25 |
+
|
26 |
+
<script src="https://cdn.babylonjs.com/babylon.js"></script>
|
27 |
+
<script src="https://cdn.babylonjs.com/loaders/babylon.glTF2FileLoader.min.js"></script>
|
28 |
+
<script src="https://unpkg.com/@huggingface/transformers"></script>
|
29 |
+
|
30 |
+
<script>
|
31 |
+
// Setup Babylon.js
|
32 |
+
const canvas = document.getElementById("renderCanvas");
|
33 |
+
const engine = new BABYLON.Engine(canvas, true);
|
34 |
+
|
35 |
+
const createScene = function() {
|
36 |
+
const scene = new BABYLON.Scene(engine);
|
37 |
+
|
38 |
+
// Camera
|
39 |
+
const camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 10, BABYLON.Vector3.Zero(), scene);
|
40 |
+
camera.attachControl(canvas, true);
|
41 |
+
|
42 |
+
// Light
|
43 |
+
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
|
44 |
+
|
45 |
+
// Ground
|
46 |
+
const ground = BABYLON.MeshBuilder.CreateGround("ground", { width: 10, height: 10 }, scene);
|
47 |
+
|
48 |
+
// Player and NPCs
|
49 |
+
const player = BABYLON.MeshBuilder.CreateBox("player", { size: 1 }, scene);
|
50 |
+
player.position.y = 0.5;
|
51 |
+
|
52 |
+
const npc1 = BABYLON.MeshBuilder.CreateSphere("npc1", { diameter: 1 }, scene);
|
53 |
+
npc1.position.x = 3;
|
54 |
+
npc1.position.y = 0.5;
|
55 |
+
|
56 |
+
// Basic movement
|
57 |
+
window.addEventListener("keydown", (event) => {
|
58 |
+
switch (event.key) {
|
59 |
+
case "ArrowUp":
|
60 |
+
player.position.z -= 0.1;
|
61 |
+
break;
|
62 |
+
case "ArrowDown":
|
63 |
+
player.position.z += 0.1;
|
64 |
+
break;
|
65 |
+
case "ArrowLeft":
|
66 |
+
player.position.x -= 0.1;
|
67 |
+
break;
|
68 |
+
case "ArrowRight":
|
69 |
+
player.position.x += 0.1;
|
70 |
+
break;
|
71 |
+
}
|
72 |
+
checkCollision(player, npc1);
|
73 |
+
});
|
74 |
+
|
75 |
+
function checkCollision(mesh1, mesh2) {
|
76 |
+
if (mesh1.intersectsMesh(mesh2)) {
|
77 |
+
openChat("npc1", "NPC-1", "Hello! How can I assist you today?");
|
78 |
+
}
|
79 |
+
}
|
80 |
+
|
81 |
+
return scene;
|
82 |
+
};
|
83 |
+
|
84 |
+
const scene = createScene();
|
85 |
+
|
86 |
+
engine.runRenderLoop(function() {
|
87 |
+
scene.render();
|
88 |
+
});
|
89 |
+
|
90 |
+
window.addEventListener("resize", function() {
|
91 |
+
engine.resize();
|
92 |
+
});
|
93 |
+
|
94 |
+
async function createXRExperience() {
|
95 |
+
try {
|
96 |
+
const xrHelper = await scene.createDefaultXRExperienceAsync({
|
97 |
+
floorMeshes: [scene.getMeshByName("ground")],
|
98 |
+
// Do not explicitly disable all optional features; let the default setup run
|
99 |
+
disableDefaultUI: false // Keep the default UI enabled to ensure the VR button works
|
100 |
+
});
|
101 |
+
|
102 |
+
// After initialization, specifically disable hand tracking if it was included
|
103 |
+
const featuresManager = xrHelper.baseExperience.featuresManager;
|
104 |
+
|
105 |
+
// This explicitly disables hand tracking if it was somehow included by default
|
106 |
+
featuresManager.disableFeature(BABYLON.WebXRFeatureName.HAND_TRACKING);
|
107 |
+
|
108 |
+
// Set up interactions
|
109 |
+
setupControllerInteractions(xrHelper);
|
110 |
+
scene.activeCamera = xrHelper.baseExperience.camera;
|
111 |
+
|
112 |
+
} catch (err) {
|
113 |
+
console.error('Failed to start VR session:', err);
|
114 |
+
}
|
115 |
+
}
|
116 |
+
|
117 |
+
// Set up VR button and initialize VR experience on click
|
118 |
+
const vrButton = document.createElement("button");
|
119 |
+
vrButton.textContent = "Enter VR";
|
120 |
+
vrButton.style.position = "absolute";
|
121 |
+
vrButton.style.bottom = "10px";
|
122 |
+
vrButton.style.left = "10px";
|
123 |
+
vrButton.style.width = "200px";
|
124 |
+
vrButton.style.height = "40px";
|
125 |
+
vrButton.style.background = "linear-gradient(to bottom, #6c543e, #4e342e)";
|
126 |
+
vrButton.style.color = "#e0d7c5";
|
127 |
+
vrButton.style.border = "none";
|
128 |
+
vrButton.style.cursor = "pointer";
|
129 |
+
document.body.appendChild(vrButton);
|
130 |
+
|
131 |
+
vrButton.addEventListener("click", createXRExperience);
|
132 |
+
|
133 |
+
function setupControllerInteractions(xrHelper) {
|
134 |
+
xrHelper.input.onControllerAddedObservable.add(controller => {
|
135 |
+
controller.onMotionControllerInitObservable.add(motionController => {
|
136 |
+
const xrRay = motionController.getComponent("xr-standard-trigger");
|
137 |
+
|
138 |
+
if (xrRay) {
|
139 |
+
xrRay.onButtonStateChangedObservable.add(() => {
|
140 |
+
if (xrRay.changes.pressed) {
|
141 |
+
if (xrRay.pressed) {
|
142 |
+
const pickResult = scene.pickWithRay(controller.pointerRay);
|
143 |
+
if (pickResult.hit && pickResult.pickedMesh) {
|
144 |
+
if (pickResult.pickedMesh.name === "npc1") {
|
145 |
+
openChat("npc1", "NPC-1", "Hello! How can I assist you today?");
|
146 |
+
}
|
147 |
+
}
|
148 |
+
}
|
149 |
+
}
|
150 |
+
});
|
151 |
+
}
|
152 |
+
});
|
153 |
+
});
|
154 |
+
}
|
155 |
+
|
156 |
+
// Chat System with Transformers.js
|
157 |
+
async function setupChat() {
|
158 |
+
try {
|
159 |
+
const model = await transformers.AutoModelForCausalLM.from_pretrained('mistralai/Mistral');
|
160 |
+
const tokenizer = await transformers.AutoTokenizer.from_pretrained('mistralai/Mistral');
|
161 |
+
return { model, tokenizer };
|
162 |
+
} catch (error) {
|
163 |
+
console.error('Failed to load the chat model or tokenizer:', error);
|
164 |
+
alert('Chat system is currently unavailable. Please try again later.');
|
165 |
+
return null;
|
166 |
+
}
|
167 |
+
}
|
168 |
+
|
169 |
+
let chatSetup = setupChat();
|
170 |
+
|
171 |
+
document.getElementById("sendButton").addEventListener("click", async () => {
|
172 |
+
const inputField = document.getElementById("chatInput");
|
173 |
+
const chatLog = document.getElementById("chatLog");
|
174 |
+
const message = inputField.value;
|
175 |
+
if (message.trim() === "" || !chatSetup) return;
|
176 |
+
|
177 |
+
chatLog.innerHTML += `<p>Player: ${message}</p>`;
|
178 |
+
inputField.value = "";
|
179 |
+
|
180 |
+
const { model, tokenizer } = await chatSetup;
|
181 |
+
const inputs = tokenizer(message, { return_tensors: "pt" });
|
182 |
+
const response = await model.generate(inputs.input_ids, { max_length: 50 });
|
183 |
+
const output = tokenizer.decode(response[0], { skip_special_tokens: true });
|
184 |
+
|
185 |
+
chatLog.innerHTML += `<p>NPC: ${output}</p>`;
|
186 |
+
chatLog.scrollTop = chatLog.scrollHeight;
|
187 |
+
|
188 |
+
// Limit chat log entries to 50
|
189 |
+
if (chatLog.childElementCount > 50) {
|
190 |
+
chatLog.removeChild(chatLog.firstChild);
|
191 |
+
}
|
192 |
+
});
|
193 |
+
|
194 |
+
function openChat(npcId, npcName, greeting) {
|
195 |
+
const chatLog = document.getElementById("chatLog");
|
196 |
+
chatLog.innerHTML += `<p>${npcName}: ${greeting}</p>`;
|
197 |
+
}
|
198 |
+
</script>
|
199 |
+
</body>
|
200 |
+
|
201 |
+
</html>
|