-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteractnpc.html
192 lines (166 loc) · 5.08 KB
/
interactnpc.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Three.js Interaction Example</title>
<style>
body {
margin: 0;
}
canvas {
display: block;
}
</style>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
let camera, scene, renderer;
let player, npc, questionMark;
let playerSpeed = 0.12;
let npcSpeed = 0.35;
let npcDirection = 0;
let npcBaseY = 0;
let npcWalkTimer = 0.2;
function createCharacter(bodyTextureUrl, headTextureUrl) {
const characterGroup = new THREE.Group();
const bodyMaterial = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load(bodyTextureUrl),
});
const headMaterial = new THREE.MeshBasicMaterial({
map: new THREE.TextureLoader().load(headTextureUrl),
});
const head = new THREE.Mesh(
new THREE.SphereGeometry(0.2, 16, 16),
headMaterial
);
head.position.set(0, 0.6, 0);
characterGroup.add(head);
const body = new THREE.Mesh(
new THREE.CylinderGeometry(0.2, 0.2, 0.3, 16),
bodyMaterial
);
body.position.set(0, 0.2, 0);
characterGroup.add(body);
const leg1 = new THREE.Mesh(
new THREE.CylinderGeometry(0.1, 0.1, 0.4, 16),
bodyMaterial
);
leg1.position.set(-0.15, 0, 0);
characterGroup.add(leg1);
const leg2 = leg1.clone();
leg2.position.set(0.15, 0, 0);
characterGroup.add(leg2);
return characterGroup;
}
function init() {
camera = new THREE.PerspectiveCamera(
75,
window.innerWidth / window.innerHeight,
0.1,
1000
);
camera.position.set(0, 1, 5);
scene = new THREE.Scene();
player = createCharacter(
"playerBodyTexture.jpg",
"playerHeadTexture.jpg"
);
scene.add(player);
npc = createCharacter("npcBodyTexture.jpg", "npcHeadTexture.jpg");
npc.position.set(2, npcBaseY, 0);
scene.add(npc);
const fontLoader = new THREE.FontLoader();
fontLoader.load(
"https://threejs.org/examples/fonts/helvetiker_regular.typeface.json",
function (font) {
const textGeometry = new THREE.TextGeometry("τι θες ?", {
font: font,
size: 0.2,
height: 0.02,
});
const textMaterial = new THREE.MeshBasicMaterial({
color: 0xff0000,
});
questionMark = new THREE.Mesh(textGeometry, textMaterial);
questionMark.position.set(2, 1.3, 0);
questionMark.visible = false;
scene.add(questionMark);
const backgroundTexture = new THREE.TextureLoader().load("sea.jpg");
scene.background = backgroundTexture;
}
);
const terrainTexture = new THREE.TextureLoader().load(
"terraintexture.jpg"
);
const terrainMaterial = new THREE.MeshStandardMaterial({
map: terrainTexture,
});
terrainTexture.wrapS = THREE.RepeatWrapping;
terrainTexture.wrapT = THREE.RepeatWrapping;
terrainTexture.repeat.set(10, 10);
const terrain = new THREE.Mesh(
new THREE.PlaneGeometry(10, 10, 10, 10),
terrainMaterial
);
terrain.rotation.x = -Math.PI / 2;
scene.add(terrain);
const light = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(light);
renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
window.addEventListener("keydown", handleKeyDown);
animate();
}
function animate() {
requestAnimationFrame(animate);
questionMark.rotation.y += 0.05;
const leg1 = npc.children[2];
const leg2 = npc.children[3];
const distance = player.position.distanceTo(npc.position);
if (distance < 0.5) {
questionMark.visible = true;
} else {
questionMark.visible = false;
npcWalkTimer += npcSpeed;
leg1.rotation.z = Math.sin(npcWalkTimer) * 0.5;
leg2.rotation.z = -Math.sin(npcWalkTimer) * 0.5;
leg1.position.y = -0.2 + Math.abs(Math.sin(npcWalkTimer)) * 0.1;
leg2.position.y =
-0.2 + Math.abs(Math.sin(npcWalkTimer + Math.PI)) * 0.1;
npcDirection += 0.01;
npc.position.x = 2 * Math.cos(npcDirection);
npc.position.z = 2 * Math.sin(npcDirection);
npc.rotation.y =
Math.atan2(npc.position.z - 0, npc.position.x - 2) + Math.PI / 2;
}
renderer.render(scene, camera);
}
function handleKeyDown(event) {
switch (event.code) {
case "ArrowUp":
player.position.z -= playerSpeed;
break;
case "ArrowDown":
player.position.z += playerSpeed;
break;
case "ArrowLeft":
player.position.x -= playerSpeed;
break;
case "ArrowRight":
player.position.x += playerSpeed;
break;
case "KeyA":
player.rotation.y += 0.1;
break;
case "KeyD":
player.rotation.y -= 0.1;
break;
}
}
init();
</script>
</body>
</html>