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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
// Skybox setup
|
39 |
+
scene.environmentTexture = new BABYLON.CubeTexture("sky.jpg", scene);
|
40 |
+
|
41 |
+
// Camera
|
42 |
+
const camera = new BABYLON.ArcRotateCamera("Camera", Math.PI / 2, Math.PI / 2, 100, BABYLON.Vector3.Zero(), scene);
|
43 |
+
camera.attachControl(canvas, true);
|
44 |
+
|
45 |
+
// Light
|
46 |
+
const light = new BABYLON.HemisphericLight("light", new BABYLON.Vector3(1, 1, 0), scene);
|
47 |
+
|
48 |
+
// Load maps
|
49 |
+
const mapMatrix = generateRandomMap();
|
50 |
+
const mapWidth = mapMatrix[0].length;
|
51 |
+
const mapHeight = mapMatrix.length;
|
52 |
+
|
53 |
+
// Ground setup with the same size as the map
|
54 |
+
const ground = BABYLON.MeshBuilder.CreateGround("ground", { width: mapWidth, height: mapHeight }, scene);
|
55 |
+
ground.material = new BABYLON.StandardMaterial("groundMat", scene);
|
56 |
+
ground.material.diffuseTexture = new BABYLON.Texture("sky.jpg", scene);
|
57 |
+
|
58 |
+
// Position ground underneath the map
|
59 |
+
ground.position.x = mapWidth / 2 - 0.5;
|
60 |
+
ground.position.z = mapHeight / 2 - 0.5;
|
61 |
+
|
62 |
+
// Create map
|
63 |
+
createMapFromMatrix(mapMatrix, scene);
|
64 |
+
|
65 |
+
// Player
|
66 |
+
const startPosition = getStartPosition(mapMatrix);
|
67 |
+
const player = BABYLON.MeshBuilder.CreateBox("player", { size: 1 }, scene);
|
68 |
+
player.position = new BABYLON.Vector3(startPosition.x, 0.5, startPosition.z);
|
69 |
+
player.material = new BABYLON.StandardMaterial("playerMat", scene);
|
70 |
+
player.material.diffuseTexture = new BABYLON.Texture("sky.jpg", scene);
|
71 |
+
|
72 |
+
// NPC - Red Sphere
|
73 |
+
const npcPosition = getRandomFreePosition(mapMatrix);
|
74 |
+
const npc = BABYLON.MeshBuilder.CreateSphere("npc", { diameter: 1 }, scene);
|
75 |
+
npc.position = new BABYLON.Vector3(npcPosition.x, 0.5, npcPosition.z);
|
76 |
+
npc.material = new BABYLON.StandardMaterial("npcMat", scene);
|
77 |
+
npc.material.diffuseColor = new BABYLON.Color3(1, 0, 0); // Red color
|
78 |
+
|
79 |
+
// Basic movement
|
80 |
+
window.addEventListener("keydown", (event) => {
|
81 |
+
switch (event.key) {
|
82 |
+
case "ArrowUp":
|
83 |
+
player.position.z -= 0.1;
|
84 |
+
break;
|
85 |
+
case "ArrowDown":
|
86 |
+
player.position.z += 0.1;
|
87 |
+
break;
|
88 |
+
case "ArrowLeft":
|
89 |
+
player.position.x -= 0.1;
|
90 |
+
break;
|
91 |
+
case "ArrowRight":
|
92 |
+
player.position.x += 0.1;
|
93 |
+
break;
|
94 |
+
}
|
95 |
+
});
|
96 |
+
|
97 |
+
return scene;
|
98 |
+
};
|
99 |
+
|
100 |
+
function generateRandomMap() {
|
101 |
+
const size = 100;
|
102 |
+
const map = [];
|
103 |
+
for (let y = 0; y < size; y++) {
|
104 |
+
const row = [];
|
105 |
+
for (let x = 0; x < size; x++) {
|
106 |
+
row.push(Math.random() > 0.8 ? 1 : 0); // 1 for wall, 0 for empty
|
107 |
+
}
|
108 |
+
map.push(row);
|
109 |
+
}
|
110 |
+
return map;
|
111 |
+
}
|
112 |
+
|
113 |
+
function createMapFromMatrix(matrix, scene) {
|
114 |
+
const wallMaterial = new BABYLON.StandardMaterial("wallMat", scene);
|
115 |
+
wallMaterial.diffuseTexture = new BABYLON.Texture("sky.jpg", scene);
|
116 |
+
|
117 |
+
matrix.forEach((row, y) => {
|
118 |
+
row.forEach((value, x) => {
|
119 |
+
if (value === 1) {
|
120 |
+
const wall = BABYLON.MeshBuilder.CreateBox("wall_" + x + "_" + y, {
|
121 |
+
width: 1,
|
122 |
+
height: 2,
|
123 |
+
depth: 1
|
124 |
+
}, scene);
|
125 |
+
wall.position = new BABYLON.Vector3(x, 1, y);
|
126 |
+
wall.material = wallMaterial;
|
127 |
+
}
|
128 |
+
});
|
129 |
+
});
|
130 |
+
}
|
131 |
+
|
132 |
+
function getStartPosition(matrix) {
|
133 |
+
for (let y = 0; y < matrix.length; y++) {
|
134 |
+
for (let x = 0; x < matrix[y].length; x++) {
|
135 |
+
if (matrix[y][x] === 0) {
|
136 |
+
return { x, z: y }; // Starting position
|
137 |
+
}
|
138 |
+
}
|
139 |
+
}
|
140 |
+
return { x: 0, z: 0 }; // Default position if none found
|
141 |
+
}
|
142 |
+
|
143 |
+
function getRandomFreePosition(matrix) {
|
144 |
+
let x, z;
|
145 |
+
do {
|
146 |
+
x = Math.floor(Math.random() * matrix[0].length);
|
147 |
+
z = Math.floor(Math.random() * matrix.length);
|
148 |
+
} while (matrix[z][x] !== 0); // Ensure the position is free (no wall)
|
149 |
+
return { x, z };
|
150 |
+
}
|
151 |
+
|
152 |
+
const scene = createScene();
|
153 |
+
engine.runRenderLoop(() => {
|
154 |
+
scene.render();
|
155 |
+
});
|
156 |
+
|
157 |
+
window.addEventListener("resize", () => {
|
158 |
+
engine.resize();
|
159 |
+
});
|
160 |
+
|
161 |
+
async function createXRExperience() {
|
162 |
+
try {
|
163 |
+
const xrHelper = await scene.createDefaultXRExperienceAsync({
|
164 |
+
floorMeshes: [scene.getMeshByName("ground")],
|
165 |
+
disableDefaultUI: false // Keep the default UI enabled to ensure the VR button works
|
166 |
+
});
|
167 |
+
|
168 |
+
// After initialization, specifically disable hand tracking if it was included
|
169 |
+
const featuresManager = xrHelper.baseExperience.featuresManager;
|
170 |
+
featuresManager.disableFeature(BABYLON.WebXRFeatureName.HAND_TRACKING);
|
171 |
+
|
172 |
+
// Set up interactions
|
173 |
+
setupControllerInteractions(xrHelper);
|
174 |
+
scene.activeCamera = xrHelper.baseExperience.camera;
|
175 |
+
|
176 |
+
} catch (err) {
|
177 |
+
console.error('Failed to start VR session:', err);
|
178 |
+
}
|
179 |
+
}
|
180 |
+
|
181 |
+
// Set up VR button and initialize VR experience on click
|
182 |
+
const vrButton = document.createElement("button");
|
183 |
+
vrButton.textContent = "Enter VR";
|
184 |
+
vrButton.style.position = "absolute";
|
185 |
+
vrButton.style.bottom = "10px";
|
186 |
+
vrButton.style.left = "10px";
|
187 |
+
vrButton.style.width = "200px";
|
188 |
+
vrButton.style.height = "40px";
|
189 |
+
vrButton.style.background = "linear-gradient(to bottom, #6c543e, #4e342e)";
|
190 |
+
vrButton.style.color = "#e0d7c5";
|
191 |
+
vrButton.style.border = "none";
|
192 |
+
vrButton.style.cursor = "pointer";
|
193 |
+
document.body.appendChild(vrButton);
|
194 |
+
|
195 |
+
vrButton.addEventListener("click", createXRExperience);
|
196 |
+
|
197 |
+
function setupControllerInteractions(xrHelper) {
|
198 |
+
xrHelper.input.onControllerAddedObservable.add(controller => {
|
199 |
+
controller.onMotionControllerInitObservable.add(motionController => {
|
200 |
+
const xrRay = motionController.getComponent("xr-standard-trigger");
|
201 |
+
|
202 |
+
if (xrRay) {
|
203 |
+
let isProcessing = false; // Debounce flag
|
204 |
+
|
205 |
+
xrRay.onButtonStateChangedObservable.add(() => {
|
206 |
+
if (xrRay.pressed && !isProcessing) {
|
207 |
+
isProcessing = true;
|
208 |
+
|
209 |
+
// Use a try-catch block to handle any unexpected errors
|
210 |
+
try {
|
211 |
+
const pickResult = scene.pickWithRay(controller.pointerRay);
|
212 |
+
|
213 |
+
if (pickResult.hit && pickResult.pickedMesh) {
|
214 |
+
if (pickResult.pickedMesh.name === "npc") {
|
215 |
+
alert("You win!"); // Alert the player that they have won
|
216 |
+
}
|
217 |
+
}
|
218 |
+
} catch (err) {
|
219 |
+
console.error('Error during ray interaction:', err);
|
220 |
+
} finally {
|
221 |
+
// Allow further interactions after a short delay
|
222 |
+
setTimeout(() => {
|
223 |
+
isProcessing = false;
|
224 |
+
}, 200); // Adjust delay as necessary
|
225 |
+
}
|
226 |
+
}
|
227 |
+
});
|
228 |
+
}
|
229 |
+
});
|
230 |
+
});
|
231 |
+
}
|
232 |
+
|
233 |
+
// Ensure the camera controls are reset when exiting VR mode
|
234 |
+
window.addEventListener("resize", () => {
|
235 |
+
engine.resize();
|
236 |
+
});
|
237 |
+
|
238 |
+
async function setupChat() {
|
239 |
+
try {
|
240 |
+
const model = await transformers.AutoModelForCausalLM.from_pretrained('mistralai/Mistral');
|
241 |
+
const tokenizer = await transformers.AutoTokenizer.from_pretrained('mistralai/Mistral');
|
242 |
+
return { model, tokenizer };
|
243 |
+
} catch (error) {
|
244 |
+
console.error('Failed to load the chat model or tokenizer:', error);
|
245 |
+
alert('Chat system is currently unavailable. Please try again later.');
|
246 |
+
return null;
|
247 |
+
}
|
248 |
+
}
|
249 |
+
|
250 |
+
let chatSetup = setupChat();
|
251 |
+
</script>
|
252 |
+
</body>
|
253 |
+
|
254 |
+
</html>
|