matrix-engine-wgpu 1.3.17 → 1.3.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.codesandbox/tasks.json +46 -0
- package/.devcontainer/devcontainer.json +22 -0
- package/.github/dependabot.yml +12 -0
- package/REFERENCE.md +59 -0
- package/app-worker.js +45 -0
- package/dev.md +541 -0
- package/empty.js +17 -0
- package/examples/camera-texture.js +60 -0
- package/examples/games/jamb/html-content.js +128 -0
- package/examples/games/jamb/jamb.js +1341 -0
- package/examples/games/jamb/readme.md +3 -0
- package/examples/load-jamb.js +6 -0
- package/examples/load-obj-file.js +95 -0
- package/examples/load-objs-sequence.js +68 -0
- package/examples/unlit-textures.js +31 -0
- package/examples/video-texture.js +61 -0
- package/examples.js +73 -0
- package/main.js +635 -0
- package/non-project-files/cubebuffer-example.js +51 -0
- package/non-project-files/dev.txt +21 -0
- package/non-project-files/image1.png +0 -0
- package/non-project-files/image6.png +0 -0
- package/package.json +1 -4
- package/public/ammojs/ammo.js +957 -0
- package/public/ammojs/ammo.wasm.js +921 -0
- package/public/ammojs/ammo.wasm.wasm +0 -0
- package/public/app-worker.js +47 -0
- package/public/app.js +12744 -0
- package/public/css/style.css +711 -0
- package/public/empty.html +25 -0
- package/public/empty.js +10453 -0
- package/public/examples.html +30 -0
- package/public/examples.js +11217 -0
- package/public/index.html +20 -0
- package/public/manifest copy.web +35 -0
- package/public/manifest.web +25 -0
- package/public/res/audios/block.mp3 +0 -0
- package/public/res/audios/dice-roll.mp3 +0 -0
- package/public/res/audios/dice1.mp3 +0 -0
- package/public/res/audios/dice2.mp3 +0 -0
- package/public/res/audios/kenney/Kenney.url +2 -0
- package/public/res/audios/kenney/License.txt +22 -0
- package/public/res/audios/kenney/Patreon.url +2 -0
- package/public/res/audios/kenney/audios/back_001.ogg +0 -0
- package/public/res/audios/kenney/audios/back_002.ogg +0 -0
- package/public/res/audios/kenney/audios/back_003.ogg +0 -0
- package/public/res/audios/kenney/audios/back_004.ogg +0 -0
- package/public/res/audios/kenney/audios/bong_001.ogg +0 -0
- package/public/res/audios/kenney/audios/click_001.ogg +0 -0
- package/public/res/audios/kenney/audios/click_002.ogg +0 -0
- package/public/res/audios/kenney/audios/click_003.ogg +0 -0
- package/public/res/audios/kenney/audios/click_004.ogg +0 -0
- package/public/res/audios/kenney/audios/click_005.ogg +0 -0
- package/public/res/audios/kenney/audios/close_001.ogg +0 -0
- package/public/res/audios/kenney/audios/close_002.ogg +0 -0
- package/public/res/audios/kenney/audios/close_003.ogg +0 -0
- package/public/res/audios/kenney/audios/close_004.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_001.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_002.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_003.ogg +0 -0
- package/public/res/audios/kenney/audios/confirmation_004.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_001.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_002.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_003.ogg +0 -0
- package/public/res/audios/kenney/audios/drop_004.ogg +0 -0
- package/public/res/audios/kenney/audios/error_001.ogg +0 -0
- package/public/res/audios/kenney/audios/error_002.ogg +0 -0
- package/public/res/audios/kenney/audios/error_003.ogg +0 -0
- package/public/res/audios/kenney/audios/error_004.ogg +0 -0
- package/public/res/audios/kenney/audios/error_005.ogg +0 -0
- package/public/res/audios/kenney/audios/error_006.ogg +0 -0
- package/public/res/audios/kenney/audios/error_007.ogg +0 -0
- package/public/res/audios/kenney/audios/error_008.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_001.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_002.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_003.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_004.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_005.ogg +0 -0
- package/public/res/audios/kenney/audios/glass_006.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_001.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_002.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_003.ogg +0 -0
- package/public/res/audios/kenney/audios/glitch_004.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_001.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_002.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_003.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_004.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_005.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_006.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_007.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_008.ogg +0 -0
- package/public/res/audios/kenney/audios/maximize_009.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_001.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_002.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_003.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_004.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_005.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_006.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_007.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_008.ogg +0 -0
- package/public/res/audios/kenney/audios/minimize_009.ogg +0 -0
- package/public/res/audios/kenney/audios/open_001.ogg +0 -0
- package/public/res/audios/kenney/audios/open_002.ogg +0 -0
- package/public/res/audios/kenney/audios/open_003.ogg +0 -0
- package/public/res/audios/kenney/audios/open_004.ogg +0 -0
- package/public/res/audios/kenney/audios/pluck_001.ogg +0 -0
- package/public/res/audios/kenney/audios/pluck_002.ogg +0 -0
- package/public/res/audios/kenney/audios/question_001.ogg +0 -0
- package/public/res/audios/kenney/audios/question_002.ogg +0 -0
- package/public/res/audios/kenney/audios/question_003.ogg +0 -0
- package/public/res/audios/kenney/audios/question_004.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_001.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_002.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_003.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_004.ogg +0 -0
- package/public/res/audios/kenney/audios/scratch_005.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_001.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_002.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_003.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_004.ogg +0 -0
- package/public/res/audios/kenney/audios/scroll_005.ogg +0 -0
- package/public/res/audios/kenney/audios/select_001.ogg +0 -0
- package/public/res/audios/kenney/audios/select_002.ogg +0 -0
- package/public/res/audios/kenney/audios/select_003.ogg +0 -0
- package/public/res/audios/kenney/audios/select_004.ogg +0 -0
- package/public/res/audios/kenney/audios/select_005.ogg +0 -0
- package/public/res/audios/kenney/audios/select_006.ogg +0 -0
- package/public/res/audios/kenney/audios/select_007.ogg +0 -0
- package/public/res/audios/kenney/audios/select_008.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_001.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_002.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_003.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_004.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_005.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_006.ogg +0 -0
- package/public/res/audios/kenney/audios/switch_007.ogg +0 -0
- package/public/res/audios/kenney/audios/tick_001.ogg +0 -0
- package/public/res/audios/kenney/audios/tick_002.ogg +0 -0
- package/public/res/audios/kenney/audios/tick_004.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_001.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_002.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_003.ogg +0 -0
- package/public/res/audios/kenney/audios/toggle_004.ogg +0 -0
- package/public/res/audios/start.mp3 +0 -0
- package/public/res/audios/toggle_002.mp3 +0 -0
- package/public/res/fonts/Accuratist.ttf +0 -0
- package/public/res/fonts/Closeness.ttf +0 -0
- package/public/res/fonts/WARGAMES.TTF +0 -0
- package/public/res/fonts/readme.txt +5 -0
- package/public/res/fonts/stormfaze.ttf +0 -0
- package/public/res/icons/512.png +0 -0
- package/public/res/icons/webgpu-horizontal.svg +45 -0
- package/public/res/meshes/blender/cube.blend +0 -0
- package/public/res/meshes/blender/cube.blend1 +0 -0
- package/public/res/meshes/blender/cube.mtl +12 -0
- package/public/res/meshes/blender/cube.obj +46 -0
- package/public/res/meshes/blender/cube.png +0 -0
- package/public/res/meshes/blender/cubeSmartUV.blend +0 -0
- package/public/res/meshes/blender/cubeSmartUV.mtl +12 -0
- package/public/res/meshes/blender/cubeSmartUV.obj +46 -0
- package/public/res/meshes/blender/lopta.mtl +10 -0
- package/public/res/meshes/blender/lopta.obj +3402 -0
- package/public/res/meshes/blender/piramyd.blend +0 -0
- package/public/res/meshes/blender/piramyd.blend1 +0 -0
- package/public/res/meshes/blender/piramyd.js +42 -0
- package/public/res/meshes/blender/piramyd.mtl +10 -0
- package/public/res/meshes/blender/piramyd.obj +18696 -0
- package/public/res/meshes/blender/piramyd1.js +42 -0
- package/public/res/meshes/blender/sphepe.blend +0 -0
- package/public/res/meshes/blender/sphepe.blend1 +0 -0
- package/public/res/meshes/blender/sphere.mtl +10 -0
- package/public/res/meshes/blender/sphere.obj +3402 -0
- package/public/res/meshes/blender/welcomeTextblend.blend +0 -0
- package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
- package/public/res/meshes/jamb/bg.blend +0 -0
- package/public/res/meshes/jamb/bg.blend1 +0 -0
- package/public/res/meshes/jamb/bg.mtl +12 -0
- package/public/res/meshes/jamb/bg.obj +17 -0
- package/public/res/meshes/jamb/bg.png +0 -0
- package/public/res/meshes/jamb/dice-default.png +0 -0
- package/public/res/meshes/jamb/dice-mark.png +0 -0
- package/public/res/meshes/jamb/dice.mtl +12 -0
- package/public/res/meshes/jamb/dice.obj +40 -0
- package/public/res/meshes/jamb/dice.png +0 -0
- package/public/res/meshes/jamb/jamb-title.mtl +12 -0
- package/public/res/meshes/jamb/jamb-title.obj +26008 -0
- package/public/res/meshes/jamb/jamb.blend +0 -0
- package/public/res/meshes/jamb/jamb.blend1 +0 -0
- package/public/res/meshes/jamb/logo.png +0 -0
- package/public/res/meshes/jamb/nidzaDice.blend +0 -0
- package/public/res/meshes/jamb/nidzaDice.blend1 +0 -0
- package/public/res/meshes/jamb/pile.blend +0 -0
- package/public/res/meshes/jamb/simpleCube.blend +0 -0
- package/public/res/meshes/jamb/simpleCube.blend1 +0 -0
- package/public/res/meshes/jamb/sounds/roll1.wav +0 -0
- package/public/res/meshes/jamb/text.png +0 -0
- package/public/res/meshes/obj/armor.obj +319 -0
- package/public/res/meshes/obj/armor.png +0 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000001.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000002.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000003.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000004.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000005.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000006.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000007.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000008.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000009.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000010.obj +23260 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000011.obj +23262 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000012.obj +23262 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000013.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000014.obj +23262 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000015.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000016.obj +23264 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000017.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000018.obj +23261 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000019.obj +23263 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.mtl +22 -0
- package/public/res/meshes/objs-sequence/swat-walk-pistol_000020.obj +23261 -0
- package/public/res/meshes/shapes/star1.obj +60 -0
- package/public/res/multilang/en.json +39 -0
- package/public/res/multilang/sr.json +39 -0
- package/public/res/textures/default.png +0 -0
- package/public/res/textures/rust.jpg +0 -0
- package/public/res/textures/tex1.jpg +0 -0
- package/public/res/videos/readme.txt +2 -0
- package/public/res/videos/tunel.mp4 +0 -0
- package/public/test.html +636 -0
- package/public/three-test.js +165 -0
- package/public/worker.html +25 -0
- package/src/engine/ball.js +482 -0
- package/src/engine/cube.js +496 -0
- package/src/engine/engine.js +404 -0
- package/src/engine/final/adaptJSON1.js +53 -0
- package/src/engine/final/utils2.js +63 -0
- package/src/engine/lights.js +153 -0
- package/src/engine/loader-obj.js +473 -0
- package/src/engine/materials.js +295 -0
- package/src/engine/matrix-class.js +252 -0
- package/src/engine/mesh-obj.js +574 -0
- package/src/engine/raycast.js +218 -0
- package/src/engine/utils.js +881 -0
- package/src/libs/mat.js +0 -0
- package/src/multilang/lang.js +35 -0
- package/src/physics/matrix-ammo.js +363 -0
- package/src/shaders/fragment.video.wgsl.js +83 -0
- package/src/shaders/fragment.wgsl.js +75 -0
- package/src/shaders/shaders.js +51 -0
- package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-fs.glsl +56 -0
- package/src/shaders/standard-matrix-engine-shaders/standard-matrix-engine-vs.glsl +75 -0
- package/src/shaders/vertex.wgsl.js +54 -0
- package/src/shaders/vertexShadow.wgsl.js +20 -0
- package/src/sounds/sounds.js +69 -0
- package/src/world.js +474 -0
|
@@ -0,0 +1,482 @@
|
|
|
1
|
+
import {UNLIT_SHADER} from "../shaders/shaders";
|
|
2
|
+
import {mat4, vec3} from 'wgpu-matrix';
|
|
3
|
+
import {Position, Rotation} from "./matrix-class";
|
|
4
|
+
import {createInputHandler} from "./engine";
|
|
5
|
+
|
|
6
|
+
export default class MEBall {
|
|
7
|
+
|
|
8
|
+
constructor(canvas, device, context, o) {
|
|
9
|
+
this.context = context;
|
|
10
|
+
this.device = device;
|
|
11
|
+
|
|
12
|
+
// The input handler
|
|
13
|
+
this.inputHandler = createInputHandler(window, canvas);
|
|
14
|
+
this.cameras = o.cameras;
|
|
15
|
+
this.scale = o.scale;
|
|
16
|
+
console.log('passed : o.mainCameraParams.responseCoef ', o.mainCameraParams.responseCoef)
|
|
17
|
+
this.mainCameraParams = {
|
|
18
|
+
type: o.mainCameraParams.type,
|
|
19
|
+
responseCoef: o.mainCameraParams.responseCoef
|
|
20
|
+
} // | WASD 'arcball' };
|
|
21
|
+
|
|
22
|
+
this.lastFrameMS = 0;
|
|
23
|
+
|
|
24
|
+
this.entityArgPass = o.entityArgPass;
|
|
25
|
+
|
|
26
|
+
this.SphereLayout = {
|
|
27
|
+
vertexStride: 8 * 4,
|
|
28
|
+
positionsOffset: 0,
|
|
29
|
+
normalOffset: 3 * 4,
|
|
30
|
+
uvOffset: 6 * 4,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
if(typeof o.raycast === 'undefined') {
|
|
34
|
+
this.raycast = {
|
|
35
|
+
enabled: false,
|
|
36
|
+
radius: 2
|
|
37
|
+
};
|
|
38
|
+
} else {
|
|
39
|
+
this.raycast = o.raycast;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
this.texturesPaths = [];
|
|
43
|
+
|
|
44
|
+
o.texturesPaths.forEach((t) => {
|
|
45
|
+
this.texturesPaths.push(t)
|
|
46
|
+
})
|
|
47
|
+
|
|
48
|
+
this.position = new Position(o.position.x, o.position.y, o.position.z)
|
|
49
|
+
this.rotation = new Rotation(o.rotation.x, o.rotation.y, o.rotation.z);
|
|
50
|
+
this.rotation.rotationSpeed.x = o.rotationSpeed.x;
|
|
51
|
+
this.rotation.rotationSpeed.y = o.rotationSpeed.y;
|
|
52
|
+
this.rotation.rotationSpeed.z = o.rotationSpeed.z;
|
|
53
|
+
|
|
54
|
+
this.shaderModule = device.createShaderModule({code: UNLIT_SHADER});
|
|
55
|
+
this.presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
|
56
|
+
|
|
57
|
+
this.pipeline = device.createRenderPipeline({
|
|
58
|
+
layout: 'auto',
|
|
59
|
+
vertex: {
|
|
60
|
+
module: this.shaderModule,
|
|
61
|
+
entryPoint: 'vertexMain',
|
|
62
|
+
buffers: [
|
|
63
|
+
{
|
|
64
|
+
arrayStride: this.SphereLayout.vertexStride,
|
|
65
|
+
attributes: [
|
|
66
|
+
{
|
|
67
|
+
// position
|
|
68
|
+
shaderLocation: 0,
|
|
69
|
+
offset: this.SphereLayout.positionsOffset,
|
|
70
|
+
format: 'float32x3',
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
// normal
|
|
74
|
+
shaderLocation: 1,
|
|
75
|
+
offset: this.SphereLayout.normalOffset,
|
|
76
|
+
format: 'float32x3',
|
|
77
|
+
},
|
|
78
|
+
{
|
|
79
|
+
// uv
|
|
80
|
+
shaderLocation: 2,
|
|
81
|
+
offset: this.SphereLayout.uvOffset,
|
|
82
|
+
format: 'float32x2',
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
fragment: {
|
|
89
|
+
module: this.shaderModule,
|
|
90
|
+
entryPoint: 'fragmentMain',
|
|
91
|
+
targets: [
|
|
92
|
+
{
|
|
93
|
+
format: this.presentationFormat,
|
|
94
|
+
},
|
|
95
|
+
],
|
|
96
|
+
},
|
|
97
|
+
primitive: {
|
|
98
|
+
topology: 'triangle-list',
|
|
99
|
+
|
|
100
|
+
// Backface culling since the sphere is solid piece of geometry.
|
|
101
|
+
// Faces pointing away from the camera will be occluded by faces
|
|
102
|
+
// pointing toward the camera.
|
|
103
|
+
cullMode: 'back',
|
|
104
|
+
},
|
|
105
|
+
|
|
106
|
+
// Enable depth testing so that the fragment closest to the camera
|
|
107
|
+
// is rendered in front.
|
|
108
|
+
depthStencil: {
|
|
109
|
+
depthWriteEnabled: true,
|
|
110
|
+
depthCompare: 'less',
|
|
111
|
+
format: 'depth24plus',
|
|
112
|
+
},
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
this.depthTexture = device.createTexture({
|
|
116
|
+
size: [canvas.width, canvas.height],
|
|
117
|
+
format: 'depth24plus',
|
|
118
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
this.uniformBufferSize = 4 * 16; // 4x4 matrix
|
|
122
|
+
this.uniformBuffer = device.createBuffer({
|
|
123
|
+
size: this.uniformBufferSize,
|
|
124
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
// Fetch the images and upload them into a GPUTexture.
|
|
128
|
+
this.texture0 = null;
|
|
129
|
+
this.moonTexture = null;
|
|
130
|
+
|
|
131
|
+
this.settings = {
|
|
132
|
+
useRenderBundles: true,
|
|
133
|
+
asteroidCount: 15,
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
this.loadTex0(this.texturesPaths, device).then(() => {
|
|
137
|
+
this.loadTex1(this.texturesPaths, device).then(() => {
|
|
138
|
+
this.sampler = device.createSampler({
|
|
139
|
+
magFilter: 'linear',
|
|
140
|
+
minFilter: 'linear',
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
this.transform = mat4.create();
|
|
144
|
+
mat4.identity(this.transform);
|
|
145
|
+
|
|
146
|
+
// Create one large central planet surrounded by a large ring of asteroids
|
|
147
|
+
this.planet = this.createGeometry(this.scale);
|
|
148
|
+
this.planet.bindGroup = this.createSphereBindGroup(this.texture0, this.transform);
|
|
149
|
+
|
|
150
|
+
var asteroids = [
|
|
151
|
+
this.createGeometry(12, 8, 6, 0.15),
|
|
152
|
+
];
|
|
153
|
+
|
|
154
|
+
this.renderables = [this.planet];
|
|
155
|
+
|
|
156
|
+
// this.ensureEnoughAsteroids(asteroids, this.transform);
|
|
157
|
+
this.renderPassDescriptor = {
|
|
158
|
+
colorAttachments: [
|
|
159
|
+
{
|
|
160
|
+
view: undefined,
|
|
161
|
+
clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
|
|
162
|
+
loadOp: this.entityArgPass.loadOp,
|
|
163
|
+
storeOp: this.entityArgPass.storeOp,
|
|
164
|
+
},
|
|
165
|
+
],
|
|
166
|
+
depthStencilAttachment: {
|
|
167
|
+
view: this.depthTexture.createView(),
|
|
168
|
+
depthClearValue: 1.0,
|
|
169
|
+
depthLoadOp: this.entityArgPass.depthLoadOp,
|
|
170
|
+
depthStoreOp: this.entityArgPass.depthStoreOp,
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const aspect = canvas.width / canvas.height;
|
|
175
|
+
this.projectionMatrix = mat4.perspective((2 * Math.PI) / 5, aspect, 1, 100.0);
|
|
176
|
+
this.modelViewProjectionMatrix = mat4.create();
|
|
177
|
+
|
|
178
|
+
this.frameBindGroup = device.createBindGroup({
|
|
179
|
+
layout: this.pipeline.getBindGroupLayout(0),
|
|
180
|
+
entries: [
|
|
181
|
+
{
|
|
182
|
+
binding: 0,
|
|
183
|
+
resource: {
|
|
184
|
+
buffer: this.uniformBuffer,
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
],
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
// The render bundle can be encoded once and re-used as many times as needed.
|
|
192
|
+
// Because it encodes all of the commands needed to render at the GPU level,
|
|
193
|
+
// those commands will not need to execute the associated JavaScript code upon
|
|
194
|
+
// execution or be re-validated, which can represent a significant time savings.
|
|
195
|
+
//
|
|
196
|
+
// However, because render bundles are immutable once created, they are only
|
|
197
|
+
// appropriate for rendering content where the same commands will be executed
|
|
198
|
+
// every time, with the only changes being the contents of the buffers and
|
|
199
|
+
// textures used. Cases where the executed commands differ from frame-to-frame,
|
|
200
|
+
// such as when using frustrum or occlusion culling, will not benefit from
|
|
201
|
+
// using render bundles as much.
|
|
202
|
+
this.renderBundle;
|
|
203
|
+
this.updateRenderBundle();
|
|
204
|
+
|
|
205
|
+
})
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
ensureEnoughAsteroids(asteroids, transform) {
|
|
211
|
+
for(let i = this.renderables.length;i <= this.settings.asteroidCount;++i) {
|
|
212
|
+
// Place copies of the asteroid in a ring.
|
|
213
|
+
const radius = Math.random() * 1.7 + 1.25;
|
|
214
|
+
const angle = Math.random() * Math.PI * 2;
|
|
215
|
+
const x = Math.sin(angle) * radius;
|
|
216
|
+
const y = (Math.random() - 0.5) * 0.015;
|
|
217
|
+
const z = Math.cos(angle) * radius;
|
|
218
|
+
|
|
219
|
+
mat4.identity(transform);
|
|
220
|
+
mat4.translate(transform, [x, y, z], transform);
|
|
221
|
+
mat4.rotateX(transform, Math.random() * Math.PI, transform);
|
|
222
|
+
mat4.rotateY(transform, Math.random() * Math.PI, transform);
|
|
223
|
+
this.renderables.push({
|
|
224
|
+
...asteroids[i % asteroids.length],
|
|
225
|
+
bindGroup: this.createSphereBindGroup(this.moonTexture, transform),
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
updateRenderBundle() {
|
|
231
|
+
console.log('updateRenderBundle')
|
|
232
|
+
const renderBundleEncoder = this.device.createRenderBundleEncoder({
|
|
233
|
+
colorFormats: [this.presentationFormat],
|
|
234
|
+
depthStencilFormat: 'depth24plus',
|
|
235
|
+
});
|
|
236
|
+
this.renderScene(renderBundleEncoder);
|
|
237
|
+
this.renderBundle = renderBundleEncoder.finish();
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
createGeometry(radius, widthSegments = 8, heightSegments = 4, randomness = 0) {
|
|
241
|
+
|
|
242
|
+
const sphereMesh = this.createSphereMesh(radius, widthSegments, heightSegments, randomness);
|
|
243
|
+
// Create a vertex buffer from the sphere data.
|
|
244
|
+
const vertices = this.device.createBuffer({
|
|
245
|
+
size: sphereMesh.vertices.byteLength,
|
|
246
|
+
usage: GPUBufferUsage.VERTEX,
|
|
247
|
+
mappedAtCreation: true,
|
|
248
|
+
});
|
|
249
|
+
new Float32Array(vertices.getMappedRange()).set(sphereMesh.vertices);
|
|
250
|
+
vertices.unmap();
|
|
251
|
+
|
|
252
|
+
const indices = this.device.createBuffer({
|
|
253
|
+
size: sphereMesh.indices.byteLength,
|
|
254
|
+
usage: GPUBufferUsage.INDEX,
|
|
255
|
+
mappedAtCreation: true,
|
|
256
|
+
});
|
|
257
|
+
new Uint16Array(indices.getMappedRange()).set(sphereMesh.indices);
|
|
258
|
+
indices.unmap();
|
|
259
|
+
|
|
260
|
+
return {
|
|
261
|
+
vertices,
|
|
262
|
+
indices,
|
|
263
|
+
indexCount: sphereMesh.indices.length,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
createSphereBindGroup(texture, transform) {
|
|
268
|
+
|
|
269
|
+
const uniformBufferSize = 4 * 16; // 4x4 matrix
|
|
270
|
+
const uniformBuffer = this.device.createBuffer({
|
|
271
|
+
size: uniformBufferSize,
|
|
272
|
+
usage: GPUBufferUsage.UNIFORM | GPUBufferUsage.COPY_DST,
|
|
273
|
+
mappedAtCreation: true,
|
|
274
|
+
});
|
|
275
|
+
new Float32Array(uniformBuffer.getMappedRange()).set(transform);
|
|
276
|
+
uniformBuffer.unmap();
|
|
277
|
+
|
|
278
|
+
const bindGroup = this.device.createBindGroup({
|
|
279
|
+
layout: this.pipeline.getBindGroupLayout(1),
|
|
280
|
+
entries: [
|
|
281
|
+
{
|
|
282
|
+
binding: 0,
|
|
283
|
+
resource: {
|
|
284
|
+
buffer: uniformBuffer,
|
|
285
|
+
},
|
|
286
|
+
},
|
|
287
|
+
{
|
|
288
|
+
binding: 1,
|
|
289
|
+
resource: this.sampler,
|
|
290
|
+
},
|
|
291
|
+
{
|
|
292
|
+
binding: 2,
|
|
293
|
+
resource: texture.createView(),
|
|
294
|
+
},
|
|
295
|
+
],
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
return bindGroup;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
getTransformationMatrix(pos) {
|
|
302
|
+
// const viewMatrix = mat4.identity();
|
|
303
|
+
const now = Date.now();
|
|
304
|
+
const deltaTime = (now - this.lastFrameMS) / this.mainCameraParams.responseCoef;
|
|
305
|
+
this.lastFrameMS = now;
|
|
306
|
+
|
|
307
|
+
// const viewMatrix = mat4.identity(); ORI
|
|
308
|
+
const camera = this.cameras[this.mainCameraParams.type];
|
|
309
|
+
const viewMatrix = camera.update(deltaTime, this.inputHandler());
|
|
310
|
+
|
|
311
|
+
mat4.translate(viewMatrix, vec3.fromValues(pos.x, pos.y, pos.z), viewMatrix);
|
|
312
|
+
|
|
313
|
+
mat4.rotateX(viewMatrix, Math.PI * this.rotation.getRotX(), viewMatrix);
|
|
314
|
+
mat4.rotateY(viewMatrix, Math.PI * this.rotation.getRotY(), viewMatrix);
|
|
315
|
+
mat4.rotateZ(viewMatrix, Math.PI * this.rotation.getRotZ(), viewMatrix);
|
|
316
|
+
|
|
317
|
+
mat4.multiply(this.projectionMatrix, viewMatrix, this.modelViewProjectionMatrix);
|
|
318
|
+
return this.modelViewProjectionMatrix;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
async loadTex1(texPaths, device) {
|
|
322
|
+
return new Promise(async (resolve) => {
|
|
323
|
+
const response = await fetch(texPaths[0]);
|
|
324
|
+
const imageBitmap = await createImageBitmap(await response.blob());
|
|
325
|
+
|
|
326
|
+
this.moonTexture = device.createTexture({
|
|
327
|
+
size: [imageBitmap.width, imageBitmap.height, 1],
|
|
328
|
+
format: 'rgba8unorm',
|
|
329
|
+
usage:
|
|
330
|
+
GPUTextureUsage.TEXTURE_BINDING |
|
|
331
|
+
GPUTextureUsage.COPY_DST |
|
|
332
|
+
GPUTextureUsage.RENDER_ATTACHMENT,
|
|
333
|
+
});
|
|
334
|
+
var moonTexture = this.moonTexture
|
|
335
|
+
device.queue.copyExternalImageToTexture(
|
|
336
|
+
{source: imageBitmap},
|
|
337
|
+
{texture: moonTexture},
|
|
338
|
+
[imageBitmap.width, imageBitmap.height]
|
|
339
|
+
);
|
|
340
|
+
resolve()
|
|
341
|
+
})
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
async loadTex0(paths, device) {
|
|
345
|
+
return new Promise(async (resolve) => {
|
|
346
|
+
const response = await fetch(paths[0]);
|
|
347
|
+
const imageBitmap = await createImageBitmap(await response.blob());
|
|
348
|
+
console.log('loadTex0 WHAT IS THIS -> ', this)
|
|
349
|
+
this.texture0 = device.createTexture({
|
|
350
|
+
size: [imageBitmap.width, imageBitmap.height, 1],
|
|
351
|
+
format: 'rgba8unorm',
|
|
352
|
+
usage:
|
|
353
|
+
GPUTextureUsage.TEXTURE_BINDING |
|
|
354
|
+
GPUTextureUsage.COPY_DST |
|
|
355
|
+
GPUTextureUsage.RENDER_ATTACHMENT,
|
|
356
|
+
});
|
|
357
|
+
var texture0 = this.texture0
|
|
358
|
+
device.queue.copyExternalImageToTexture(
|
|
359
|
+
{source: imageBitmap},
|
|
360
|
+
{texture: texture0},
|
|
361
|
+
[imageBitmap.width, imageBitmap.height]
|
|
362
|
+
);
|
|
363
|
+
resolve()
|
|
364
|
+
})
|
|
365
|
+
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
createSphereMesh(radius, widthSegments = 3, heightSegments = 3, randomness = 0) {
|
|
369
|
+
const vertices = [];
|
|
370
|
+
const indices = [];
|
|
371
|
+
|
|
372
|
+
widthSegments = Math.max(3, Math.floor(widthSegments));
|
|
373
|
+
heightSegments = Math.max(2, Math.floor(heightSegments));
|
|
374
|
+
|
|
375
|
+
const firstVertex = vec3.create();
|
|
376
|
+
const vertex = vec3.create();
|
|
377
|
+
const normal = vec3.create();
|
|
378
|
+
|
|
379
|
+
let index = 0;
|
|
380
|
+
const grid = [];
|
|
381
|
+
|
|
382
|
+
// generate vertices, normals and uvs
|
|
383
|
+
for(let iy = 0;iy <= heightSegments;iy++) {
|
|
384
|
+
const verticesRow = [];
|
|
385
|
+
const v = iy / heightSegments;
|
|
386
|
+
// special case for the poles
|
|
387
|
+
let uOffset = 0;
|
|
388
|
+
if(iy === 0) {
|
|
389
|
+
uOffset = 0.5 / widthSegments;
|
|
390
|
+
} else if(iy === heightSegments) {
|
|
391
|
+
uOffset = -0.5 / widthSegments;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
for(let ix = 0;ix <= widthSegments;ix++) {
|
|
395
|
+
const u = ix / widthSegments;
|
|
396
|
+
// Poles should just use the same position all the way around.
|
|
397
|
+
if(ix == widthSegments) {
|
|
398
|
+
vec3.copy(firstVertex, vertex);
|
|
399
|
+
} else if(ix == 0 || (iy != 0 && iy !== heightSegments)) {
|
|
400
|
+
const rr = radius + (Math.random() - 0.5) * 2 * randomness * radius;
|
|
401
|
+
// vertex
|
|
402
|
+
vertex[0] = -rr * Math.cos(u * Math.PI * 2) * Math.sin(v * Math.PI);
|
|
403
|
+
vertex[1] = rr * Math.cos(v * Math.PI);
|
|
404
|
+
vertex[2] = rr * Math.sin(u * Math.PI * 2) * Math.sin(v * Math.PI);
|
|
405
|
+
if(ix == 0) {
|
|
406
|
+
vec3.copy(vertex, firstVertex);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
vertices.push(...vertex);
|
|
410
|
+
|
|
411
|
+
// normal
|
|
412
|
+
vec3.copy(vertex, normal);
|
|
413
|
+
vec3.normalize(normal, normal);
|
|
414
|
+
vertices.push(...normal);
|
|
415
|
+
// uv
|
|
416
|
+
vertices.push(u + uOffset, 1 - v);
|
|
417
|
+
verticesRow.push(index++);
|
|
418
|
+
}
|
|
419
|
+
grid.push(verticesRow);
|
|
420
|
+
}
|
|
421
|
+
// indices
|
|
422
|
+
for(let iy = 0;iy < heightSegments;iy++) {
|
|
423
|
+
for(let ix = 0;ix < widthSegments;ix++) {
|
|
424
|
+
const a = grid[iy][ix + 1];
|
|
425
|
+
const b = grid[iy][ix];
|
|
426
|
+
const c = grid[iy + 1][ix];
|
|
427
|
+
const d = grid[iy + 1][ix + 1];
|
|
428
|
+
|
|
429
|
+
if(iy !== 0) indices.push(a, b, d);
|
|
430
|
+
if(iy !== heightSegments - 1) indices.push(b, c, d);
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
return {
|
|
435
|
+
vertices: new Float32Array(vertices),
|
|
436
|
+
indices: new Uint16Array(indices),
|
|
437
|
+
};
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
// Render bundles function as partial, limited render passes, so we can use the
|
|
441
|
+
// same code both to render the scene normally and to build the render bundle.
|
|
442
|
+
renderScene(passEncoder) {
|
|
443
|
+
if(typeof this.renderables === 'undefined') return;
|
|
444
|
+
passEncoder.setPipeline(this.pipeline);
|
|
445
|
+
passEncoder.setBindGroup(0, this.frameBindGroup);
|
|
446
|
+
// Loop through every renderable object and draw them individually.
|
|
447
|
+
// (Because many of these meshes are repeated, with only the transforms
|
|
448
|
+
// differing, instancing would be highly effective here. This sample
|
|
449
|
+
// intentionally avoids using instancing in order to emulate a more complex
|
|
450
|
+
// scene, which helps demonstrate the potential time savings a render bundle
|
|
451
|
+
// can provide.)
|
|
452
|
+
let count = 0;
|
|
453
|
+
for(const renderable of this.renderables) {
|
|
454
|
+
passEncoder.setBindGroup(1, renderable.bindGroup);
|
|
455
|
+
passEncoder.setVertexBuffer(0, renderable.vertices);
|
|
456
|
+
passEncoder.setIndexBuffer(renderable.indices, 'uint16');
|
|
457
|
+
passEncoder.drawIndexed(renderable.indexCount);
|
|
458
|
+
|
|
459
|
+
if(++count > this.settings.asteroidCount) {
|
|
460
|
+
break;
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
draw = () => {
|
|
466
|
+
if(this.moonTexture == null) {
|
|
467
|
+
// console.log('not ready')
|
|
468
|
+
return;
|
|
469
|
+
}
|
|
470
|
+
const transformationMatrix = this.getTransformationMatrix(this.position);
|
|
471
|
+
this.device.queue.writeBuffer(
|
|
472
|
+
this.uniformBuffer,
|
|
473
|
+
0,
|
|
474
|
+
transformationMatrix.buffer,
|
|
475
|
+
transformationMatrix.byteOffset,
|
|
476
|
+
transformationMatrix.byteLength
|
|
477
|
+
);
|
|
478
|
+
this.renderPassDescriptor.colorAttachments[0].view = this.context
|
|
479
|
+
.getCurrentTexture()
|
|
480
|
+
.createView();
|
|
481
|
+
}
|
|
482
|
+
}
|