Spaces:
Running
Running
Update index.html
Browse files- index.html +117 -18
index.html
CHANGED
@@ -3,27 +3,126 @@
|
|
3 |
|
4 |
<head>
|
5 |
<meta charset="UTF-8" />
|
6 |
-
<link rel="stylesheet" href="style.css" />
|
7 |
-
|
8 |
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
9 |
-
<title>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
</head>
|
11 |
|
12 |
<body>
|
13 |
-
<
|
14 |
-
<
|
15 |
-
<
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
<
|
25 |
-
|
26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
</body>
|
28 |
|
29 |
-
</html>
|
|
|
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 with Transformers.js Chat</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 (Q-learning is complex in a static file, so using basic movement for now)
|
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 |
+
// Chat System with Transformers.js
|
95 |
+
async function setupChat() {
|
96 |
+
const model = await transformers.AutoModelForCausalLM.from_pretrained('mistralai/Mistral');
|
97 |
+
const tokenizer = await transformers.AutoTokenizer.from_pretrained('mistralai/Mistral');
|
98 |
+
return { model, tokenizer };
|
99 |
+
}
|
100 |
+
|
101 |
+
let chatSetup = setupChat();
|
102 |
+
|
103 |
+
document.getElementById("sendButton").addEventListener("click", async () => {
|
104 |
+
const inputField = document.getElementById("chatInput");
|
105 |
+
const chatLog = document.getElementById("chatLog");
|
106 |
+
const message = inputField.value;
|
107 |
+
if (message.trim() === "") return;
|
108 |
+
|
109 |
+
chatLog.innerHTML += `<p>Player: ${message}</p>`;
|
110 |
+
inputField.value = "";
|
111 |
+
|
112 |
+
const { model, tokenizer } = await chatSetup;
|
113 |
+
const inputs = tokenizer(message, { return_tensors: "pt" });
|
114 |
+
const response = await model.generate(inputs.input_ids, { max_length: 50 });
|
115 |
+
const output = tokenizer.decode(response[0], { skip_special_tokens: true });
|
116 |
+
|
117 |
+
chatLog.innerHTML += `<p>NPC: ${output}</p>`;
|
118 |
+
chatLog.scrollTop = chatLog.scrollHeight;
|
119 |
+
});
|
120 |
+
|
121 |
+
function openChat(npcId, npcName, greeting) {
|
122 |
+
const chatLog = document.getElementById("chatLog");
|
123 |
+
chatLog.innerHTML += `<p>${npcName}: ${greeting}</p>`;
|
124 |
+
}
|
125 |
+
</script>
|
126 |
</body>
|
127 |
|
128 |
+
</html>
|