matrix-engine-wgpu 1.0.2 → 1.0.5
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/REFERENCE.md +3 -0
- package/app-worker.js +45 -0
- package/empty.js +12 -0
- package/examples/load-obj-file.js +48 -0
- package/examples/unlit-textures.js +27 -0
- package/examples.js +7 -0
- package/index.js +18 -5
- package/main.js +49 -23
- package/package.json +11 -2
- 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 +2640 -305
- package/public/css/style.css +1 -2
- package/public/empty.html +25 -0
- package/public/empty.js +9107 -0
- package/public/examples.html +25 -0
- package/public/examples.js +9180 -0
- package/public/index.html +1 -1
- 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/welcomeTextblend.blend +0 -0
- package/public/res/meshes/dragon/stanfordDragonData.js +5 -0
- package/public/res/meshes/obj/armor.obj +319 -0
- package/public/res/meshes/obj/armor.png +0 -0
- package/public/worker.html +25 -0
- package/readme.md +130 -48
- package/src/engine/ball.js +26 -10
- package/src/engine/cube.js +100 -81
- package/src/engine/engine.js +466 -4
- package/src/engine/final/adaptJSON1.js +53 -0
- package/src/engine/final/utils2.js +63 -0
- package/src/engine/loader-obj.js +469 -0
- package/src/engine/matrix-class.js +5 -4
- package/src/engine/matrix-mesh.js +49 -0
- package/src/engine/mesh-obj.js +534 -0
- package/src/engine/utils.js +300 -45
- package/src/physics/matrix-ammo.js +119 -0
- package/src/shaders/fragment.wgsl.js +48 -0
- package/src/shaders/shaders.js +4 -124
- package/src/shaders/vertex.wgsl.js +49 -0
- package/src/shaders/vertexShadow.wgsl.js +20 -0
- package/src/world.js +246 -0
- package/src/meWGPU.js +0 -173
package/src/shaders/shaders.js
CHANGED
|
@@ -1,130 +1,9 @@
|
|
|
1
|
-
|
|
2
1
|
/**
|
|
3
2
|
* @description
|
|
4
|
-
*
|
|
3
|
+
* UNIT Texures -
|
|
4
|
+
* Good for performance
|
|
5
5
|
*/
|
|
6
|
-
export const
|
|
7
|
-
worldViewProjection: mat4x4f,
|
|
8
|
-
worldInverseTranspose: mat4x4f,
|
|
9
|
-
};
|
|
10
|
-
@group(0) @binding(0) var<uniform> vsUniforms: VSUniforms;
|
|
11
|
-
|
|
12
|
-
struct MyVSInput {
|
|
13
|
-
@location(0) position: vec4f,
|
|
14
|
-
@location(1) normal: vec3f,
|
|
15
|
-
@location(2) texcoord: vec2f,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
struct MyVSOutput {
|
|
19
|
-
@builtin(position) position: vec4f,
|
|
20
|
-
@location(0) normal: vec3f,
|
|
21
|
-
@location(1) texcoord: vec2f,
|
|
22
|
-
};
|
|
23
|
-
|
|
24
|
-
@vertex
|
|
25
|
-
fn myVSMain(v: MyVSInput) -> MyVSOutput {
|
|
26
|
-
var vsOut: MyVSOutput;
|
|
27
|
-
vsOut.position = vsUniforms.worldViewProjection * v.position;
|
|
28
|
-
vsOut.normal = (vsUniforms.worldInverseTranspose * vec4f(v.normal, 0.0)).xyz;
|
|
29
|
-
vsOut.texcoord = v.texcoord;
|
|
30
|
-
return vsOut;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
struct FSUniforms {
|
|
34
|
-
lightDirection: vec3f,
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
@group(0) @binding(1) var<uniform> fsUniforms: FSUniforms;
|
|
38
|
-
@group(0) @binding(2) var diffuseSampler: sampler;
|
|
39
|
-
@group(0) @binding(3) var diffuseTexture: texture_2d<f32>;
|
|
40
|
-
|
|
41
|
-
@fragment
|
|
42
|
-
fn myFSMain(v: MyVSOutput) -> @location(0) vec4f {
|
|
43
|
-
var diffuseColor = textureSample(diffuseTexture, diffuseSampler, v.texcoord);
|
|
44
|
-
var a_normal = normalize(v.normal);
|
|
45
|
-
var l = dot(a_normal, fsUniforms.lightDirection) * 0.5 + 0.5;
|
|
46
|
-
return vec4f(diffuseColor.rgb * l, diffuseColor.a);
|
|
47
|
-
}
|
|
48
|
-
`;
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* @description
|
|
52
|
-
* For Cube with images
|
|
53
|
-
*/
|
|
54
|
-
export const cubeTexShader = `struct Uniforms {
|
|
55
|
-
matrix: mat4x4f,
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
struct Vertex {
|
|
59
|
-
@location(0) position: vec4f,
|
|
60
|
-
@location(1) texcoord: vec2f,
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
struct VSOutput {
|
|
64
|
-
@builtin(position) position: vec4f,
|
|
65
|
-
@location(0) texcoord: vec2f,
|
|
66
|
-
};
|
|
67
|
-
|
|
68
|
-
@group(0) @binding(0) var<uniform> uni: Uniforms;
|
|
69
|
-
@group(0) @binding(1) var ourSampler: sampler;
|
|
70
|
-
@group(0) @binding(2) var ourTexture: texture_2d<f32>;
|
|
71
|
-
|
|
72
|
-
@vertex fn vs(vert: Vertex) -> VSOutput {
|
|
73
|
-
var vsOut: VSOutput;
|
|
74
|
-
vsOut.position = uni.matrix * vert.position;
|
|
75
|
-
vsOut.texcoord = vert.texcoord;
|
|
76
|
-
return vsOut;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
@fragment fn fs(vsOut: VSOutput) -> @location(0) vec4f {
|
|
80
|
-
return textureSample(ourTexture, ourSampler, vsOut.texcoord);
|
|
81
|
-
}
|
|
82
|
-
`;
|
|
83
|
-
|
|
84
|
-
export const basicVertWGSL = `struct Uniforms {
|
|
85
|
-
modelViewProjectionMatrix : mat4x4<f32>,
|
|
86
|
-
}
|
|
87
|
-
@binding(0) @group(0) var<uniform> uniforms : Uniforms;
|
|
88
|
-
|
|
89
|
-
struct VertexOutput {
|
|
90
|
-
@builtin(position) Position : vec4<f32>,
|
|
91
|
-
@location(0) fragUV : vec2<f32>,
|
|
92
|
-
@location(1) fragPosition: vec4<f32>,
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
@vertex
|
|
96
|
-
fn main(
|
|
97
|
-
@location(0) position : vec4<f32>,
|
|
98
|
-
@location(1) uv : vec2<f32>
|
|
99
|
-
) -> VertexOutput {
|
|
100
|
-
var output : VertexOutput;
|
|
101
|
-
output.Position = uniforms.modelViewProjectionMatrix * position;
|
|
102
|
-
output.fragUV = uv;
|
|
103
|
-
output.fragPosition = 0.5 * (position + vec4(1.0, 1.0, 1.0, 1.0));
|
|
104
|
-
return output;
|
|
105
|
-
}
|
|
106
|
-
`;
|
|
107
|
-
|
|
108
|
-
export const basicFragWGSL = `@group(0) @binding(1) var mySampler: sampler;
|
|
109
|
-
@group(0) @binding(2) var myTexture: texture_2d<f32>;
|
|
110
|
-
|
|
111
|
-
@fragment
|
|
112
|
-
fn main(
|
|
113
|
-
@location(0) fragUV: vec2<f32>,
|
|
114
|
-
@location(1) fragPosition: vec4<f32>
|
|
115
|
-
) -> @location(0) vec4<f32> {
|
|
116
|
-
return textureSample(myTexture, mySampler, fragUV) * fragPosition;
|
|
117
|
-
}
|
|
118
|
-
`;
|
|
119
|
-
export const vertexPositionColorWGSL = `@fragment
|
|
120
|
-
fn main(
|
|
121
|
-
@location(0) fragUV: vec2<f32>,
|
|
122
|
-
@location(1) fragPosition: vec4<f32>
|
|
123
|
-
) -> @location(0) vec4<f32> {
|
|
124
|
-
return fragPosition;
|
|
125
|
-
}`
|
|
126
|
-
|
|
127
|
-
export const BALL_SHADER = `struct Uniforms {
|
|
6
|
+
export const UNLIT_SHADER = `struct Uniforms {
|
|
128
7
|
viewProjectionMatrix : mat4x4f
|
|
129
8
|
}
|
|
130
9
|
@group(0) @binding(0) var<uniform> uniforms : Uniforms;
|
|
@@ -169,3 +48,4 @@ fn fragmentMain(input: VertexOutput) -> @location(0) vec4f {
|
|
|
169
48
|
|
|
170
49
|
return vec4f(textureColor.rgb * lightColor, textureColor.a);
|
|
171
50
|
}`;
|
|
51
|
+
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
export let vertexWGSL = `struct Scene {
|
|
2
|
+
lightViewProjMatrix: mat4x4f,
|
|
3
|
+
cameraViewProjMatrix: mat4x4f,
|
|
4
|
+
lightPos: vec3f,
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
struct Model {
|
|
8
|
+
modelMatrix: mat4x4f,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@group(0) @binding(0) var<uniform> scene : Scene;
|
|
12
|
+
@group(1) @binding(0) var<uniform> model : Model;
|
|
13
|
+
|
|
14
|
+
struct VertexOutput {
|
|
15
|
+
@location(0) shadowPos: vec3f,
|
|
16
|
+
@location(1) fragPos: vec3f,
|
|
17
|
+
@location(2) fragNorm: vec3f,
|
|
18
|
+
@location(3) uv : vec2f,
|
|
19
|
+
|
|
20
|
+
@builtin(position) Position: vec4f,
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
@vertex
|
|
24
|
+
fn main(
|
|
25
|
+
@location(0) position: vec3f,
|
|
26
|
+
@location(1) normal: vec3f,
|
|
27
|
+
@location(2) uv : vec2f
|
|
28
|
+
) -> VertexOutput {
|
|
29
|
+
var output : VertexOutput;
|
|
30
|
+
|
|
31
|
+
// XY is in (-1, 1) space, Z is in (0, 1) space
|
|
32
|
+
let posFromLight = scene.lightViewProjMatrix * model.modelMatrix * vec4(position, 1.0);
|
|
33
|
+
|
|
34
|
+
// Convert XY to (0, 1)
|
|
35
|
+
// Y is flipped because texture coords are Y-down.
|
|
36
|
+
output.shadowPos = vec3(
|
|
37
|
+
posFromLight.xy * vec2(0.5, -0.5) + vec2(0.5),
|
|
38
|
+
posFromLight.z
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
output.Position = scene.cameraViewProjMatrix * model.modelMatrix * vec4(position, 1.0);
|
|
42
|
+
output.fragPos = output.Position.xyz;
|
|
43
|
+
output.fragNorm = normal;
|
|
44
|
+
// nidza
|
|
45
|
+
output.uv = uv;
|
|
46
|
+
|
|
47
|
+
return output;
|
|
48
|
+
}
|
|
49
|
+
`
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export let vertexShadowWGSL = `struct Scene {
|
|
2
|
+
lightViewProjMatrix: mat4x4f,
|
|
3
|
+
cameraViewProjMatrix: mat4x4f,
|
|
4
|
+
lightPos: vec3f,
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
struct Model {
|
|
8
|
+
modelMatrix: mat4x4f,
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
@group(0) @binding(0) var<uniform> scene : Scene;
|
|
12
|
+
@group(1) @binding(0) var<uniform> model : Model;
|
|
13
|
+
|
|
14
|
+
@vertex
|
|
15
|
+
fn main(
|
|
16
|
+
@location(0) position: vec3f
|
|
17
|
+
) -> @builtin(position) vec4f {
|
|
18
|
+
return scene.lightViewProjMatrix * model.modelMatrix * vec4(position, 1.0);
|
|
19
|
+
}
|
|
20
|
+
`
|
package/src/world.js
ADDED
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
import {vec3} from "wgpu-matrix";
|
|
2
|
+
import MEBall from "./engine/ball.js";
|
|
3
|
+
import MECube from './engine/cube.js';
|
|
4
|
+
import {ArcballCamera, WASDCamera} from "./engine/engine.js";
|
|
5
|
+
import MEMeshObj from "./engine/mesh-obj.js";
|
|
6
|
+
import MatrixAmmo from "./physics/matrix-ammo.js";
|
|
7
|
+
// import {scriptManager} from "./engine/utils.js";
|
|
8
|
+
|
|
9
|
+
export default class MatrixEngineWGPU {
|
|
10
|
+
|
|
11
|
+
mainRenderBundle = [];
|
|
12
|
+
rbContainer = [];
|
|
13
|
+
frame = () => {};
|
|
14
|
+
|
|
15
|
+
entityHolder = [];
|
|
16
|
+
|
|
17
|
+
entityArgPass = {
|
|
18
|
+
loadOp: 'clear',
|
|
19
|
+
storeOp: 'store',
|
|
20
|
+
depthLoadOp: 'clear',
|
|
21
|
+
depthStoreOp: 'store'
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
matrixAmmo = new MatrixAmmo();
|
|
25
|
+
|
|
26
|
+
constructor(options, callback) {
|
|
27
|
+
// console.log('typeof options ', typeof options )
|
|
28
|
+
if(typeof options == 'undefined' || typeof options == "function") {
|
|
29
|
+
this.options = {
|
|
30
|
+
useSingleRenderPass: true,
|
|
31
|
+
canvasSize: 'fullscreen',
|
|
32
|
+
mainCameraParams: {
|
|
33
|
+
type: 'WASD',
|
|
34
|
+
responseCoef: 2000
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
callback = options;
|
|
38
|
+
}
|
|
39
|
+
if(typeof options.mainCameraParams === 'undefined') {
|
|
40
|
+
options.mainCameraParams = {
|
|
41
|
+
type: 'WASD',
|
|
42
|
+
responseCoef: 2000
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
this.options = options;
|
|
46
|
+
|
|
47
|
+
this.mainCameraParams = options.mainCameraParams;
|
|
48
|
+
|
|
49
|
+
var canvas = document.createElement('canvas')
|
|
50
|
+
if(this.options.canvasSize == 'fullscreen') {
|
|
51
|
+
canvas.width = window.innerWidth;
|
|
52
|
+
canvas.height = window.innerHeight;
|
|
53
|
+
} else {
|
|
54
|
+
canvas.width = this.options.canvasSize.w;
|
|
55
|
+
canvas.height = this.options.canvasSize.h;
|
|
56
|
+
}
|
|
57
|
+
document.body.append(canvas)
|
|
58
|
+
|
|
59
|
+
const initialCameraPosition = vec3.create(0, 0, 0);
|
|
60
|
+
this.mainCameraParams = {
|
|
61
|
+
type: this.options.mainCameraParams.type,
|
|
62
|
+
responseCoef: this.options.mainCameraParams.responseCoef
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
this.cameras = {
|
|
66
|
+
arcball: new ArcballCamera({position: initialCameraPosition}),
|
|
67
|
+
WASD: new WASDCamera({position: initialCameraPosition}),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
this.init({canvas, callback})
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
init = async ({canvas, callback}) => {
|
|
74
|
+
this.canvas = canvas;
|
|
75
|
+
this.adapter = await navigator.gpu.requestAdapter();
|
|
76
|
+
this.device = await this.adapter.requestDevice();
|
|
77
|
+
this.context = canvas.getContext('webgpu');
|
|
78
|
+
|
|
79
|
+
const devicePixelRatio = window.devicePixelRatio;
|
|
80
|
+
canvas.width = canvas.clientWidth * devicePixelRatio;
|
|
81
|
+
canvas.height = canvas.clientHeight * devicePixelRatio;
|
|
82
|
+
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
|
83
|
+
|
|
84
|
+
this.context.configure({
|
|
85
|
+
device: this.device,
|
|
86
|
+
format: presentationFormat,
|
|
87
|
+
alphaMode: 'premultiplied',
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if(this.options.useSingleRenderPass == true) {
|
|
91
|
+
this.makeDefaultRenderPassDescriptor()
|
|
92
|
+
this.frame = this.frameSinglePass;
|
|
93
|
+
} else {
|
|
94
|
+
// must be
|
|
95
|
+
this.frame = this.frameSinglePass;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
this.run(callback)
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
makeDefaultRenderPassDescriptor = () => {
|
|
102
|
+
|
|
103
|
+
this.depthTexture = this.device.createTexture({
|
|
104
|
+
size: [this.canvas.width, this.canvas.height],
|
|
105
|
+
format: 'depth24plus',
|
|
106
|
+
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
this.renderPassDescriptor = {
|
|
110
|
+
colorAttachments: [
|
|
111
|
+
{
|
|
112
|
+
view: undefined,
|
|
113
|
+
clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
|
|
114
|
+
loadOp: 'clear',
|
|
115
|
+
storeOp: 'store',
|
|
116
|
+
},
|
|
117
|
+
],
|
|
118
|
+
depthStencilAttachment: {
|
|
119
|
+
view: this.depthTexture.createView(),
|
|
120
|
+
depthClearValue: 1.0,
|
|
121
|
+
depthLoadOp: 'clear',
|
|
122
|
+
depthStoreOp: 'store',
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
addCube = (o) => {
|
|
128
|
+
if(typeof o === 'undefined') {
|
|
129
|
+
var o = {
|
|
130
|
+
scale: 1,
|
|
131
|
+
position: {x: 0, y: 0, z: -4},
|
|
132
|
+
texturesPaths: ['./res/textures/default.png'],
|
|
133
|
+
rotation: {x: 0, y: 0, z: 0},
|
|
134
|
+
rotationSpeed: {x: 0, y: 0, z: 0},
|
|
135
|
+
entityArgPass: this.entityArgPass,
|
|
136
|
+
cameras: this.cameras,
|
|
137
|
+
mainCameraParams: this.mainCameraParams
|
|
138
|
+
}
|
|
139
|
+
} else {
|
|
140
|
+
if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
|
|
141
|
+
if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
|
|
142
|
+
if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
|
|
143
|
+
if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
|
|
144
|
+
if(typeof o.scale === 'undefined') {o.scale = 1;}
|
|
145
|
+
if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
|
|
146
|
+
o.entityArgPass = this.entityArgPass;
|
|
147
|
+
o.cameras = this.cameras;
|
|
148
|
+
}
|
|
149
|
+
let myCube1 = new MECube(this.canvas, this.device, this.context, o)
|
|
150
|
+
this.mainRenderBundle.push(myCube1);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
addBall = (o) => {
|
|
154
|
+
if(typeof o === 'undefined') {
|
|
155
|
+
var o = {
|
|
156
|
+
scale: 1,
|
|
157
|
+
position: {x: 0, y: 0, z: -4},
|
|
158
|
+
texturesPaths: ['./res/textures/default.png'],
|
|
159
|
+
rotation: {x: 0, y: 0, z: 0},
|
|
160
|
+
rotationSpeed: {x: 0, y: 0, z: 0},
|
|
161
|
+
entityArgPass: this.entityArgPass,
|
|
162
|
+
cameras: this.cameras,
|
|
163
|
+
mainCameraParams: this.mainCameraParams
|
|
164
|
+
}
|
|
165
|
+
} else {
|
|
166
|
+
if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
|
|
167
|
+
if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
|
|
168
|
+
if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
|
|
169
|
+
if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
|
|
170
|
+
if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
|
|
171
|
+
if(typeof o.scale === 'undefined') {o.scale = 1;}
|
|
172
|
+
o.entityArgPass = this.entityArgPass;
|
|
173
|
+
o.cameras = this.cameras;
|
|
174
|
+
}
|
|
175
|
+
let myBall1 = new MEBall(this.canvas, this.device, this.context, o)
|
|
176
|
+
this.mainRenderBundle.push(myBall1);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
addMeshObj = (o) => {
|
|
180
|
+
if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
|
|
181
|
+
if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
|
|
182
|
+
if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
|
|
183
|
+
if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
|
|
184
|
+
if(typeof o.mainCameraParams === 'undefined') {o.mainCameraParams = this.mainCameraParams}
|
|
185
|
+
if(typeof o.scale === 'undefined') {o.scale = 1;}
|
|
186
|
+
o.entityArgPass = this.entityArgPass;
|
|
187
|
+
o.cameras = this.cameras;
|
|
188
|
+
if(typeof o.name === 'undefined') {o.name = 'random' + Math.random();}
|
|
189
|
+
if(typeof o.mesh === 'undefined') {
|
|
190
|
+
throw console.error('arg mesh is empty...');
|
|
191
|
+
return;
|
|
192
|
+
}
|
|
193
|
+
if(typeof o.physics === 'undefined') {
|
|
194
|
+
o.physics = {
|
|
195
|
+
enabled: false,
|
|
196
|
+
geometry: "Sphere"
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
if(typeof o.physics.enabled === 'undefined') {o.physics.enabled = false}
|
|
200
|
+
if(typeof o.physics.geometry === 'undefined') {o.physics.geometry = "Sphere"}
|
|
201
|
+
// console.log('Mesh procedure', o)
|
|
202
|
+
let myMesh1 = new MEMeshObj(this.canvas, this.device, this.context, o)
|
|
203
|
+
if(o.physics.enabled == true) {
|
|
204
|
+
this.matrixAmmo.addPhysics(myMesh1, o.physics)
|
|
205
|
+
}
|
|
206
|
+
this.mainRenderBundle.push(myMesh1);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
run(callback) {
|
|
210
|
+
setTimeout(() => {requestAnimationFrame(this.frame)}, 30)
|
|
211
|
+
setTimeout(() => {callback()}, 20)
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
frameSinglePass = () => {
|
|
215
|
+
if(typeof this.mainRenderBundle == 'undefined') return;
|
|
216
|
+
let shadowPass = null;
|
|
217
|
+
let renderPass;
|
|
218
|
+
let commandEncoder = this.device.createCommandEncoder();
|
|
219
|
+
|
|
220
|
+
this.mainRenderBundle.forEach((meItem, index) => {
|
|
221
|
+
meItem.position.update();
|
|
222
|
+
})
|
|
223
|
+
|
|
224
|
+
this.mainRenderBundle.forEach((meItem, index) => {
|
|
225
|
+
meItem.draw(commandEncoder);
|
|
226
|
+
shadowPass = commandEncoder.beginRenderPass(meItem.shadowPassDescriptor);
|
|
227
|
+
shadowPass.setPipeline(meItem.shadowPipeline);
|
|
228
|
+
meItem.drawShadows(shadowPass);
|
|
229
|
+
shadowPass.end();
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
this.mainRenderBundle.forEach((meItem, index) => {
|
|
233
|
+
if(index == 0) renderPass = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
|
|
234
|
+
if(index == 1) renderPass.setPipeline(meItem.pipeline);
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
this.mainRenderBundle.forEach((meItem, index) => {
|
|
238
|
+
meItem.drawElements(renderPass);
|
|
239
|
+
})
|
|
240
|
+
|
|
241
|
+
renderPass.end();
|
|
242
|
+
|
|
243
|
+
this.device.queue.submit([commandEncoder.finish()]);
|
|
244
|
+
requestAnimationFrame(this.frame);
|
|
245
|
+
}
|
|
246
|
+
}
|
package/src/meWGPU.js
DELETED
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
import MEBall from "./engine/ball.js";
|
|
2
|
-
import MECube from './engine/cube.js';
|
|
3
|
-
|
|
4
|
-
export default class MatrixEngineWGPU {
|
|
5
|
-
|
|
6
|
-
mainRenderBundle = [];
|
|
7
|
-
rbContainer = [];
|
|
8
|
-
frame = () => {};
|
|
9
|
-
|
|
10
|
-
entityArgPass = {
|
|
11
|
-
loadOp: 'clear',
|
|
12
|
-
storeOp: 'store',
|
|
13
|
-
depthLoadOp: 'clear',
|
|
14
|
-
depthStoreOp: 'store'
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
constructor(options, callback) {
|
|
18
|
-
console.log('typeof options ', typeof options )
|
|
19
|
-
console.log('typeof options ', options )
|
|
20
|
-
if (typeof options == 'undefined' || typeof options == "function") {
|
|
21
|
-
this.options = {
|
|
22
|
-
useSingleRenderPass: true
|
|
23
|
-
}
|
|
24
|
-
callback = options;
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
this.options = options;
|
|
28
|
-
|
|
29
|
-
var canvas = document.createElement('canvas')
|
|
30
|
-
canvas.width = window.innerWidth;
|
|
31
|
-
canvas.height = window.innerHeight;
|
|
32
|
-
document.body.append(canvas)
|
|
33
|
-
this.init({canvas, callback})
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
init = async ({canvas, callback}) => {
|
|
37
|
-
this.canvas = canvas;
|
|
38
|
-
this.adapter = await navigator.gpu.requestAdapter();
|
|
39
|
-
this.device = await this.adapter.requestDevice();
|
|
40
|
-
this.context = canvas.getContext('webgpu');
|
|
41
|
-
|
|
42
|
-
const devicePixelRatio = window.devicePixelRatio;
|
|
43
|
-
canvas.width = canvas.clientWidth * devicePixelRatio;
|
|
44
|
-
canvas.height = canvas.clientHeight * devicePixelRatio;
|
|
45
|
-
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
|
46
|
-
|
|
47
|
-
this.context.configure({
|
|
48
|
-
device: this.device,
|
|
49
|
-
format: presentationFormat,
|
|
50
|
-
alphaMode: 'premultiplied',
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
if(this.options.useSingleRenderPass == true) {
|
|
54
|
-
this.makeDefaultRenderPassDescriptor()
|
|
55
|
-
this.frame = this.frameSinglePass;
|
|
56
|
-
} else {
|
|
57
|
-
// must be
|
|
58
|
-
this.frame = this.framePassPerObject;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
this.run(callback)
|
|
62
|
-
};
|
|
63
|
-
|
|
64
|
-
makeDefaultRenderPassDescriptor = () => {
|
|
65
|
-
|
|
66
|
-
this.depthTexture = this.device.createTexture({
|
|
67
|
-
size: [this.canvas.width, this.canvas.height],
|
|
68
|
-
format: 'depth24plus',
|
|
69
|
-
usage: GPUTextureUsage.RENDER_ATTACHMENT,
|
|
70
|
-
});
|
|
71
|
-
|
|
72
|
-
this.renderPassDescriptor = {
|
|
73
|
-
colorAttachments: [
|
|
74
|
-
{
|
|
75
|
-
view: undefined,
|
|
76
|
-
clearValue: {r: 0.0, g: 0.0, b: 0.0, a: 1.0},
|
|
77
|
-
loadOp: 'clear',
|
|
78
|
-
storeOp: 'store',
|
|
79
|
-
},
|
|
80
|
-
],
|
|
81
|
-
depthStencilAttachment: {
|
|
82
|
-
view: this.depthTexture.createView(),
|
|
83
|
-
depthClearValue: 1.0,
|
|
84
|
-
depthLoadOp: 'clear',
|
|
85
|
-
depthStoreOp: 'store',
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
addCube = (o) => {
|
|
91
|
-
if(typeof o === 'undefined') {
|
|
92
|
-
var o = {
|
|
93
|
-
position: {x: 0, y: 0, z: -4},
|
|
94
|
-
texturesPaths: ['./res/textures/default.png'],
|
|
95
|
-
rotation: {x: 0, y: 0, z: 0},
|
|
96
|
-
rotationSpeed: {x: 0, y: 0, z: 0},
|
|
97
|
-
entityArgPass: this.entityArgPass
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
|
|
101
|
-
if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
|
|
102
|
-
if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
|
|
103
|
-
if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
|
|
104
|
-
o.entityArgPass = this.entityArgPass;
|
|
105
|
-
}
|
|
106
|
-
let myCube1 = new MECube(this.canvas, this.device, this.context, o)
|
|
107
|
-
this.mainRenderBundle.push(myCube1);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
addBall = (o) => {
|
|
111
|
-
if(typeof o === 'undefined') {
|
|
112
|
-
var o = {
|
|
113
|
-
position: {x: 0, y: 0, z: -4},
|
|
114
|
-
texturesPaths: ['./res/textures/default.png'],
|
|
115
|
-
rotation: {x: 0, y: 0, z: 0},
|
|
116
|
-
rotationSpeed: {x: 0, y: 0, z: 0},
|
|
117
|
-
entityArgPass: this.entityArgPass
|
|
118
|
-
}
|
|
119
|
-
} else {
|
|
120
|
-
if(typeof o.position === 'undefined') {o.position = {x: 0, y: 0, z: -4}}
|
|
121
|
-
if(typeof o.rotation === 'undefined') {o.rotation = {x: 0, y: 0, z: 0}}
|
|
122
|
-
if(typeof o.rotationSpeed === 'undefined') {o.rotationSpeed = {x: 0, y: 0, z: 0}}
|
|
123
|
-
if(typeof o.texturesPaths === 'undefined') {o.texturesPaths = ['./res/textures/default.png']}
|
|
124
|
-
o.entityArgPass = this.entityArgPass;
|
|
125
|
-
}
|
|
126
|
-
let myBall1 = new MEBall(this.canvas, this.device, this.context, o)
|
|
127
|
-
this.mainRenderBundle.push(myBall1);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
run(callback) {
|
|
131
|
-
setTimeout(() => {requestAnimationFrame(this.frame)}, 1000)
|
|
132
|
-
setTimeout(() => {callback()}, 10)
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
frameSinglePass = () => {
|
|
136
|
-
console.log('single')
|
|
137
|
-
let commandEncoder = this.device.createCommandEncoder();
|
|
138
|
-
this.rbContainer = [];
|
|
139
|
-
let passEncoder;
|
|
140
|
-
|
|
141
|
-
this.mainRenderBundle.forEach((meItem, index) => {
|
|
142
|
-
meItem.draw();
|
|
143
|
-
this.rbContainer.push(meItem.renderBundle)
|
|
144
|
-
// if(index == 0) passEncoder = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
this.renderPassDescriptor.colorAttachments[0].view = this.context
|
|
148
|
-
.getCurrentTexture()
|
|
149
|
-
.createView();
|
|
150
|
-
|
|
151
|
-
passEncoder = commandEncoder.beginRenderPass(this.renderPassDescriptor);
|
|
152
|
-
passEncoder.executeBundles(this.rbContainer);
|
|
153
|
-
passEncoder.end();
|
|
154
|
-
this.device.queue.submit([commandEncoder.finish()]);
|
|
155
|
-
requestAnimationFrame(this.frame);
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
framePassPerObject = () => {
|
|
159
|
-
console.log('framePassPerObject')
|
|
160
|
-
let commandEncoder = this.device.createCommandEncoder();
|
|
161
|
-
this.rbContainer = [];
|
|
162
|
-
let passEncoder;
|
|
163
|
-
this.mainRenderBundle.forEach((meItem, index) => {
|
|
164
|
-
meItem.draw();
|
|
165
|
-
this.rbContainer.push(meItem.renderBundle)
|
|
166
|
-
passEncoder = commandEncoder.beginRenderPass(meItem.renderPassDescriptor);
|
|
167
|
-
passEncoder.executeBundles(this.rbContainer);
|
|
168
|
-
passEncoder.end();
|
|
169
|
-
})
|
|
170
|
-
this.device.queue.submit([commandEncoder.finish()]);
|
|
171
|
-
requestAnimationFrame(this.frame);
|
|
172
|
-
}
|
|
173
|
-
}
|