/
MainScene.ts
213 lines (189 loc) · 7.52 KB
/
MainScene.ts
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @author Masaru Yamagishi <yamagishi.iloop@gmail.com>
* @license Apache-2.0
*/
// import { RayHelper } from '@babylonjs/core/Debug/rayHelper'
import { AdvancedDynamicTexture } from '@babylonjs/gui/2D/advancedDynamicTexture';
import { Camera } from '@babylonjs/core/Cameras/camera';
import { CapsuleBuilder } from '@babylonjs/core/Meshes/Builders/capsuleBuilder';
import { CascadedShadowGenerator } from '@babylonjs/core/Lights/Shadows/cascadedShadowGenerator';
import { Color3 } from '@babylonjs/core/Maths/math.color';
import { DirectionalLight } from '@babylonjs/core/Lights/directionalLight';
import { Engine } from '@babylonjs/core/Engines/engine';
import { FreeCamera } from '@babylonjs/core/Cameras/freeCamera';
import { Ray } from '@babylonjs/core/Culling/ray';
import { Rectangle } from '@babylonjs/gui/2D/controls/rectangle';
import { Scene, SceneOptions } from '@babylonjs/core/scene';
import { ShadowGenerator } from '@babylonjs/core/Lights/Shadows/shadowGenerator';
import { Sound } from '@babylonjs/core/Audio/sound';
import { SSAORenderingPipeline } from '@babylonjs/core/PostProcesses/RenderPipeline/Pipelines/ssaoRenderingPipeline';
import { StandardMaterial } from '@babylonjs/core/Materials/standardMaterial';
import { Vector3 } from '@babylonjs/core/Maths/math.vector';
import { aroundWall } from './MainScene/Stage/aroundWall';
import { ground } from './MainScene/Stage/ground';
import { houses } from './MainScene/Stage/houses';
import { mainLight } from './MainScene/Stage/mainLight';
import { obstacle } from './MainScene/Stage/obstacle';
import { skybox } from './MainScene/Stage/skybox';
import { ShooterCameraDashInput } from './MainScene/ShooterCameraDashInput';
import { SceneInterface } from './SceneInterface';
import { ContextHolder } from './ContextHolder';
// side-effects
import '@babylonjs/core/Audio/audioSceneComponent';
import '@babylonjs/core/Collisions/collisionCoordinator';
import '@babylonjs/core/Lights/Shadows/shadowGeneratorSceneComponent';
import '@babylonjs/core/Loading/Plugins/babylonFileLoader';
import '@babylonjs/core/Rendering/depthRendererSceneComponent';
// @see https://vitejs.dev/guide/assets.html#importing-asset-as-url
// @see https://www.videvo.net/sound-effect/gun-shot-single-shot-in-pe1097906/246309/
import gunfireSoundURL from './assets/gunfire.mp3?url';
/**
* Main in-game scene
*/
export class MainScene implements SceneInterface {
private readonly engine: Engine;
private readonly scene: Scene;
private readonly camera: Camera;
private readonly mainLight: DirectionalLight;
private readonly shadowGenerator: ShadowGenerator;
private gunfireSound?: Sound;
/**
* Constructor
*/
constructor(contextHolder: ContextHolder, sceneOptions?: SceneOptions) {
this.engine = contextHolder.engine;
const canvas = this.engine.getRenderingCanvas();
if (!canvas) {
throw new Error('Unknown canvas element');
}
this.scene = new Scene(this.engine, sceneOptions);
this.camera = setUpCamera(canvas, this.scene);
this.mainLight = mainLight(this.scene);
this.shadowGenerator = new CascadedShadowGenerator(2048, this.mainLight);
setUpCrossHair();
new SSAORenderingPipeline(`ssaoPipeline`, this.scene, 0.75, [this.camera]);
}
/**
* Start main loop
*/
public async start(): Promise<void> {
skybox(this.scene);
ground(this.scene);
aroundWall(this.scene);
obstacle(this.scene);
this.gunfireSound = await new Promise((resolve: (value: Sound) => void) => {
const sound: Sound = new Sound(`Gunfire`, gunfireSoundURL, this.scene, () => resolve(sound));
});
await houses(this.scene, this.shadowGenerator);
await loadMobs(this.scene, this.shadowGenerator);
this.scene.activeCamera = this.camera;
document.addEventListener('click', this.onMouseClick);
this.engine.runRenderLoop(() => {
this.scene.render();
});
}
public async dispose(): Promise<void> {
this.scene.dispose();
}
public async render(): Promise<void> {
this.scene.render();
}
/**
* Execute when mouse has clicked inside game
*/
private readonly onMouseClick = (evt: MouseEvent): void => {
if (!this.engine.isPointerLock) {
this.engine.enterPointerlock();
}
// button: 0 equals primary
if (evt.button !== 0) {
return;
}
const origin = this.camera.globalPosition.clone();
const forward = this.camera.getDirection(Vector3.Forward());
const ray = new Ray(origin, forward, 200);
// const rayHelper = new RayHelper(ray);
// rayHelper.show(this.scene);
if (this.gunfireSound) {
this.gunfireSound.play();
}
const hit = this.scene.pickWithRay(ray, (mesh) => {
return mesh.name.match(/^Mob.+/) !== null;
});
if (hit && hit.pickedMesh) {
hit.pickedMesh.dispose();
}
};
}
/**
* Creates main camera
*
* @param canvas Target CanvasElement
* @param scene Target Scene
*/
function setUpCamera(canvas: HTMLCanvasElement, scene: Scene): Camera {
const initialPotision = new Vector3(Math.random() * 200 - 100, 2, Math.random() * 200 - 100);
const camera = new FreeCamera(`MainCamera`, initialPotision, scene);
camera.setTarget(Vector3.Zero());
camera.inputs.add(new ShooterCameraDashInput(camera));
camera.attachControl(canvas, true);
camera.applyGravity = true;
camera.ellipsoid = new Vector3(1.2, 1.2, 1.2);
camera.checkCollisions = true;
return camera;
}
/**
* Creates center crosshair
*/
function setUpCrossHair(): AdvancedDynamicTexture {
const tex = AdvancedDynamicTexture.CreateFullscreenUI('FullscreenUI');
const xRect = new Rectangle('xRect');
xRect.width = '20px';
xRect.height = '2px';
xRect.color = 'White';
xRect.background = 'White';
tex.addControl(xRect);
const yRect = new Rectangle('yRect');
yRect.width = '2px';
yRect.height = '20px';
yRect.color = 'White';
yRect.background = 'White';
tex.addControl(yRect);
return tex;
}
/**
* Generate mobs
*
* @param scene Target Scene
* @param shadowGenerator Shadow Generator
*/
async function loadMobs(scene: Scene, shadowGenerator: ShadowGenerator): Promise<void> {
const capsuleBase = CapsuleBuilder.CreateCapsule(
`mobBase`,
{
height: 4,
capSubdivisions: 6,
radius: 1,
subdivisions: 4,
tessellation: 16,
},
scene
);
capsuleBase.position.y = 1;
const material = new StandardMaterial(`MobMaterialBase`, scene);
capsuleBase.material = material;
for (let index = 0; index < 100; index++) {
const capsule = capsuleBase.clone(`Mob${index}`, null);
capsule.material = material.clone(`MobMaterial${index}`);
(capsule.material as StandardMaterial).diffuseColor = new Color3(Math.random(), Math.random(), Math.random());
shadowGenerator.addShadowCaster(capsule);
capsule.position = new Vector3(Math.random() * 200 - 100, 2, Math.random() * 200 - 100);
capsule.rotate(Vector3.Up(), Math.random() * Math.PI * 4);
scene.onBeforeRenderObservable.add(() => {
capsule.rotate(Vector3.Up(), Math.random() * 0.5 - 0.25);
const delta = scene.getEngine().getDeltaTime() / 100;
capsule.moveWithCollisions(capsule.forward.multiplyByFloats(delta, delta, delta));
});
}
capsuleBase.isVisible = false;
}