-
Notifications
You must be signed in to change notification settings - Fork 2
/
3d-pacman.js
134 lines (116 loc) · 3.4 KB
/
3d-pacman.js
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
/**
* 3D Pacman
* 2021-2 Computer Graphics Term Project
* Dept. of Software, Gachon Univ.
*/
import * as THREE from 'https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/optimized/three.js';
import { OrbitControls } from 'https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/unoptimized/examples/jsm/controls/OrbitControls.js';
import { VRButton } from 'https://cdn.skypack.dev/pin/[email protected]/mode=imports,min/unoptimized/examples/jsm/webxr/VRButton.js'
import TWEEN from 'https://cdn.jsdelivr.net/npm/@tweenjs/[email protected]/dist/tween.esm.js';
import * as Utils from './js/utils.js';
import CannonDebugRenderer from './js/CannonDebugRenderer.js';
import { initNaturalMap } from './js/maps/natural.js';
import * as Loading from './js/loading.js';
import { initGachonMap } from './js/maps/gachon.js';
/* 필수 Variable */
var world, canvas, camera, scene, renderer;
var debug;
var controls;
/* VR */
export let isVRMode = false;
/**
* Window OnLoad Event
*/
window.onload = function() {
Loading.initLoading();
initThreeJS();
initVR();
initCannon();
initObject();
renderer.setAnimationLoop(animate);
}
/**
* Initilaizing Three.js
*/
function initThreeJS() {
canvas = document.getElementById("gl-canvas");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
window.addEventListener( 'resize', onWindowResize, false );
renderer = new THREE.WebGLRenderer({ canvas });
renderer.setSize(canvas.width, canvas.height);
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 75, canvas.width / canvas.height, 1, 15000);
camera.position.y = 0;
camera.position.z = 0;
scene.add( camera );
controls = new OrbitControls(camera, renderer.domElement);
controls.enablePan = false; //우클릭 이동 방지
//y축 움직임 제한
}
/**
* Initializing CANNON.js
*/
function initCannon() {
world = new CANNON.World();
world.gravity.set(0, 0, 0);
world.broadphase = new CANNON.NaiveBroadphase();
world.solver.iterations = 10;
debug = new CannonDebugRenderer(scene, world);
}
/**
* Initializing Object
*/
function initObject() {
// Stage Set
Utils.updateStage(1);
// 맵 생성
initNaturalMap(scene, world, controls, camera);
}
/**
* Window Resize CallBack Function
*/
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
function initVR() {
document.body.appendChild(VRButton.createButton(renderer));
const vrEnableButton = document.getElementById("VRButton");
renderer.xr.enabled = true;
vrEnableButton.addEventListener("click", function() {
if(vrEnableButton.innerHTML != "VR NOT SUPPORTED") {
if(!isVRMode) {
isVRMode = true;
}
else {
isVRMode = false;
}
console.log("VR Enable : " + isVRMode);
}
else {
console.log("VR Not Supported");
}
});
}
export function clearAll() {
for (var i = scene.children.length - 1; i >= 0; i--) {
var obj = scene.children[i];
if(obj.type.includes('Light') || obj.type.includes('Camera')) continue;
scene.remove(obj);
}
for (var i = world.bodies.length - 1; i >= 0; i--) {
var body = world.bodies[i];
world.removeBody(body);
}
}
/**
* Animate
*/
function animate() {
Utils.updatePhysics(scene, world, camera, controls);
TWEEN.update();
if(Utils.developerMode) debug.update();
renderer.render(scene, camera);
}