Added NodeJS Demo.
This commit is contained in:
97
demos/particleHeader/GpuWorker.js
Normal file
97
demos/particleHeader/GpuWorker.js
Normal file
@@ -0,0 +1,97 @@
|
||||
// worker.js
|
||||
|
||||
import { ParticleSimulation } from "./demo.js";
|
||||
|
||||
|
||||
var particleSimulation = new ParticleSimulation();
|
||||
|
||||
export class Controller {
|
||||
|
||||
setup( request ) {
|
||||
|
||||
var canvas = request.canvas;
|
||||
|
||||
console.log( canvas );
|
||||
|
||||
particleSimulation.setup( canvas, request.width, request.height );
|
||||
|
||||
//self.postMessage({ type: "pong", id: request.id });
|
||||
}
|
||||
|
||||
resetParticlePositions() {
|
||||
|
||||
particleSimulation.resetParticlePositions();
|
||||
|
||||
}
|
||||
|
||||
mousemove( request ) {
|
||||
|
||||
particleSimulation.eventManager.mousemove( request );
|
||||
|
||||
}
|
||||
|
||||
mousedown( request ) {
|
||||
|
||||
console.log("onMouseDown");
|
||||
|
||||
particleSimulation.eventManager.mousedown( request );
|
||||
|
||||
}
|
||||
|
||||
mouseup( request ) {
|
||||
|
||||
particleSimulation.eventManager.mouseup( request );
|
||||
|
||||
}
|
||||
|
||||
mouseleave( request ) {
|
||||
|
||||
particleSimulation.eventManager.mouseleave( request );
|
||||
|
||||
}
|
||||
|
||||
wheel( request ) {
|
||||
|
||||
particleSimulation.eventManager.wheel( request );
|
||||
|
||||
}
|
||||
|
||||
resize( request ) {
|
||||
|
||||
particleSimulation.eventManager.resize( request );
|
||||
|
||||
}
|
||||
|
||||
resetParticlePositions() {
|
||||
|
||||
particleSimulation.resetParticlePositions( );
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
const controller = new Controller();
|
||||
|
||||
self.onmessage = function (event) {
|
||||
|
||||
const data = event.data;
|
||||
|
||||
if (typeof data !== "object" || typeof data.method !== "string") {
|
||||
console.warn("Invalid request received:", data);
|
||||
return;
|
||||
}
|
||||
|
||||
// Optional: wrap the data into a Request instance (if you need its methods)
|
||||
// const request = new Request(data.method, data.payload);
|
||||
|
||||
// Or just use plain data object
|
||||
const request = data;
|
||||
|
||||
const methodName = request.method;
|
||||
|
||||
if (typeof controller[methodName] !== "function") {
|
||||
console.warn("No method found for:", request.method);
|
||||
return;
|
||||
}
|
||||
|
||||
controller[methodName](request);
|
||||
};
|
||||
431
demos/particleHeader/demo.js
Normal file
431
demos/particleHeader/demo.js
Normal file
@@ -0,0 +1,431 @@
|
||||
|
||||
import Shader from "../../framework/WebGpu.js"
|
||||
|
||||
import Matrix4 from "../../framework/Matrix4.js"
|
||||
|
||||
import Vector3 from "../../framework/Vector3.js"
|
||||
|
||||
import Camera from "../../framework/Camera.js";
|
||||
|
||||
import EventManager from "../../framework/eventManager.js";
|
||||
|
||||
import ShaderInpector from "../../framework/ShaderInpector.js";
|
||||
|
||||
|
||||
export class ParticleSimulation {
|
||||
|
||||
canvas;
|
||||
|
||||
device;
|
||||
|
||||
camera;
|
||||
|
||||
useLocalSort = true;
|
||||
|
||||
eventManager = new EventManager();
|
||||
|
||||
frameCount = 0;
|
||||
|
||||
setCanvas( canvas ) {
|
||||
|
||||
this.canvas = canvas;
|
||||
|
||||
this.eventManager.setCanvas( canvas );
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
createTextureFromImageBitmap( device, imageBitmap ) {
|
||||
|
||||
const texture = device.createTexture( {
|
||||
|
||||
size: [ imageBitmap.width, imageBitmap.height, 1 ],
|
||||
|
||||
format: 'rgba8unorm',
|
||||
|
||||
usage: GPUTextureUsage.TEXTURE_BINDING | GPUTextureUsage.COPY_DST | GPUTextureUsage.RENDER_ATTACHMENT
|
||||
|
||||
} );
|
||||
|
||||
device.queue.copyExternalImageToTexture(
|
||||
|
||||
{ source: imageBitmap },
|
||||
|
||||
{ texture: texture },
|
||||
|
||||
[ imageBitmap.width, imageBitmap.height, 1 ]
|
||||
|
||||
);
|
||||
|
||||
return texture;
|
||||
|
||||
}
|
||||
|
||||
async loadImageBitmap(url) {
|
||||
|
||||
const response = await fetch( url );
|
||||
|
||||
const blob = await response.blob();
|
||||
|
||||
const imageBitmap = await createImageBitmap( blob );
|
||||
|
||||
return imageBitmap;
|
||||
}
|
||||
|
||||
|
||||
async loadTexture( url ) {
|
||||
|
||||
const imageBitmap = await this.loadImageBitmap( url );
|
||||
|
||||
const texture = this.createTextureFromImageBitmap( this.device, imageBitmap );
|
||||
|
||||
return texture;
|
||||
|
||||
}
|
||||
|
||||
createPlane(width, height, repeatU, repeatV) {
|
||||
|
||||
const vertices = new Float32Array( 18 ); // 6 vertices (2 triangles) * 3 coords
|
||||
const normals = new Float32Array( 18 ); // same count as vertices
|
||||
const uvs = new Float32Array( 12 ); // 6 vertices * 2 coords
|
||||
|
||||
// Positions (two triangles forming a plane on XY plane at z=0)
|
||||
// Large plane from (-width/2, -height/2) to (width/2, height/2)
|
||||
vertices.set([
|
||||
-width / 2, -height / 2, 0,
|
||||
width / 2, -height / 2, 0,
|
||||
-width / 2, height / 2, 0,
|
||||
|
||||
-width / 2, height / 2, 0,
|
||||
width / 2, -height / 2, 0,
|
||||
width / 2, height / 2, 0
|
||||
]);
|
||||
|
||||
// Normals all pointing +Z
|
||||
for (let i = 0; i < 6; i++) {
|
||||
normals[i * 3 + 0] = 0;
|
||||
normals[i * 3 + 1] = 0;
|
||||
normals[i * 3 + 2] = 1;
|
||||
}
|
||||
|
||||
// UVs scaled by repeatU, repeatV to repeat texture over the plane
|
||||
uvs.set([
|
||||
0, 0,
|
||||
repeatU, 0,
|
||||
0, repeatV,
|
||||
|
||||
0, repeatV,
|
||||
repeatU, 0,
|
||||
repeatU, repeatV
|
||||
]);
|
||||
|
||||
return { vertices, normals, uvs };
|
||||
|
||||
}
|
||||
|
||||
async getParticlePositionsAndIndicesFromDiv(div, canvasSize = 256) {
|
||||
|
||||
const rect = div.getBoundingClientRect();
|
||||
console.log(rect.width, rect.height);
|
||||
|
||||
|
||||
const divWidth = rect.width;
|
||||
const divHeight = rect.height;
|
||||
|
||||
|
||||
const canvas = await html2canvas(div, {
|
||||
backgroundColor: null,
|
||||
width: divWidth,
|
||||
height: divHeight,
|
||||
scale: .6,
|
||||
useCORS: true
|
||||
});
|
||||
|
||||
const ctx = canvas.getContext("2d");
|
||||
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height );
|
||||
const data = imageData.data;
|
||||
console.log(imageData);
|
||||
const positions = [];
|
||||
|
||||
const samplesPerPixel = 8;
|
||||
const colors = [];
|
||||
|
||||
for (let y = 0; y < canvas.height; y++) {
|
||||
for (let x = 0; x < canvas.width; x++) {
|
||||
|
||||
const i = (y * canvas.width + x) * 4;
|
||||
|
||||
const r = data[i + 0];
|
||||
const g = data[i + 1];
|
||||
const b = data[i + 2];
|
||||
const a = data[i + 3];
|
||||
|
||||
|
||||
if (a > 10) {
|
||||
for (let s = 0; s < samplesPerPixel; s++) {
|
||||
|
||||
const u = (x + Math.random()) / (canvas.width - 1);
|
||||
const v = (y + Math.random()) / (canvas.height - 1);
|
||||
|
||||
const clipX = u * 2 - 1;
|
||||
const clipY = 1 - v * 2;
|
||||
|
||||
positions.push(clipX, clipY, 0, 1);
|
||||
colors.push(r / 255, g / 255, b / 255, a / 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
console.log("positions", positions.length);
|
||||
|
||||
const positionsArray = new Float32Array(positions);
|
||||
|
||||
const indexCount = positionsArray.length / 4;
|
||||
const indices = new Uint32Array(indexCount);
|
||||
|
||||
for (let i = 0; i < indexCount; i++) {
|
||||
indices[i] = i;
|
||||
}
|
||||
|
||||
return {
|
||||
positions: positionsArray,
|
||||
indices: indices,
|
||||
colors: new Float32Array(colors)
|
||||
};
|
||||
}
|
||||
|
||||
async setup( offscreenCanvas, width, height ) {
|
||||
|
||||
offscreenCanvas.width = width;
|
||||
|
||||
offscreenCanvas.height = height;
|
||||
|
||||
this.canvas = offscreenCanvas;
|
||||
|
||||
const context = offscreenCanvas.getContext("webgpu");
|
||||
|
||||
this.camera = new Camera( [0, 0, 1115], [0, -.3, 0], [0, 1, 0] );
|
||||
|
||||
this.camera.distance = 2
|
||||
|
||||
this.eventManager.setup( offscreenCanvas, this.camera );
|
||||
|
||||
|
||||
const adapter = await self.navigator.gpu.requestAdapter();
|
||||
|
||||
if ( !adapter ) {
|
||||
|
||||
throw new Error("Failed to get GPU adapter");
|
||||
|
||||
}
|
||||
|
||||
const presentationFormat = navigator.gpu.getPreferredCanvasFormat();
|
||||
|
||||
this.device = await adapter.requestDevice();
|
||||
|
||||
|
||||
|
||||
context.configure({
|
||||
device: this.device,
|
||||
format: presentationFormat,
|
||||
alphaMode: "opaque"
|
||||
});
|
||||
|
||||
|
||||
//var model = await this.loadJSON("demo.json");
|
||||
|
||||
//var mesh = model.meshes[0];
|
||||
var div = document.querySelector("#particleSource");
|
||||
|
||||
|
||||
const { positions, indices, colors } = await this.getParticlePositionsAndIndicesFromDiv( div );
|
||||
|
||||
|
||||
const rect = div.getBoundingClientRect();
|
||||
|
||||
const divWidth = rect.width;
|
||||
const divHeight = rect.height;
|
||||
|
||||
const aspectRatio = divWidth / divHeight;
|
||||
|
||||
|
||||
|
||||
this.initiateParticlesShader = new Shader( this.device );
|
||||
|
||||
|
||||
|
||||
|
||||
await this.initiateParticlesShader.setup( "../../shaders/initiateParticles.wgsl");
|
||||
|
||||
//this.initiateParticlesShader.setVariable( "aspectRatio", aspectRatio );
|
||||
|
||||
this.initiateParticlesShader.setVariable( "initiationPositions", positions );
|
||||
|
||||
this.initiateParticlesShader.setVariable( "positions", positions );
|
||||
|
||||
|
||||
|
||||
this.simpleGravityShader = new Shader( this.device );
|
||||
|
||||
await this.simpleGravityShader.setup( "../../shaders/simpleGravity.wgsl");
|
||||
|
||||
this.simpleGravityShader.setBuffer( "velocities", this.initiateParticlesShader.getBuffer("velocities") );
|
||||
|
||||
this.simpleGravityShader.setBuffer( "positions", this.initiateParticlesShader.getBuffer("positions") );
|
||||
|
||||
|
||||
this.simpleGravityShader.setVariable( "aspectRatio", aspectRatio );
|
||||
|
||||
//this.simpleGravityShader.setVariable( "gravity", [0, -7, 0] );
|
||||
this.simpleGravityShader.setVariable( "hoverRadius", .2 );
|
||||
|
||||
|
||||
|
||||
|
||||
this.renderShader = new Shader( this.device );
|
||||
|
||||
this.renderShader.setCanvas( this.canvas );
|
||||
|
||||
this.renderShader.topology = "point-list";
|
||||
|
||||
|
||||
|
||||
console.log("vertices", positions);
|
||||
|
||||
|
||||
this.vertexCount = positions.length / 4;
|
||||
|
||||
|
||||
//this.renderShader.setAttribute( "normal", mesh.normals );
|
||||
|
||||
this.renderShader.setAttribute( "indices",indices );
|
||||
|
||||
this.renderShader.setCanvas( this.canvas );
|
||||
|
||||
this.renderShader.topology = "point-list";
|
||||
|
||||
await this.renderShader.setup( "../../shaders/particle-header.wgsl");
|
||||
|
||||
this.renderShader.setVariable( "hoverRadius", .2 );
|
||||
|
||||
this.renderShader.setBuffer( "positions", this.simpleGravityShader.getBuffer("positions") );
|
||||
|
||||
//this.renderShader.setVariable( "positions", positions );
|
||||
|
||||
this.renderShader.setVariable( "colors", colors );
|
||||
|
||||
this.renderShader.setVariable( "aspectRatio", aspectRatio );
|
||||
|
||||
|
||||
div.addEventListener("mousemove", function(event) {
|
||||
const rect = div.getBoundingClientRect();
|
||||
|
||||
const mouseX = event.clientX - rect.left;
|
||||
const mouseY = event.clientY - rect.top;
|
||||
|
||||
const aspectRatio = rect.width / rect.height;
|
||||
|
||||
// Normalize to 0..1
|
||||
const normX = mouseX / rect.width;
|
||||
const normY = mouseY / rect.height;
|
||||
|
||||
// Map to clip space coordinates
|
||||
const clipX = normX * 2 * aspectRatio - aspectRatio; // -aspectRatio to +aspectRatio
|
||||
const clipY = 1 - normY * 2; // +1 to -1 (top to bottom)
|
||||
|
||||
// Update uniform buffer for mousePos
|
||||
this.renderShader.setVariable("mousePos", [clipX, clipY]);
|
||||
this.simpleGravityShader.setVariable("mousePos", [clipX, clipY]);
|
||||
|
||||
}.bind(this));
|
||||
|
||||
|
||||
var texture = await this.loadTexture("./textures/defaultnouvs.png");
|
||||
|
||||
const sampler = this.device.createSampler({
|
||||
minFilter: 'linear',
|
||||
magFilter: 'linear',
|
||||
mipmapFilter: 'linear',
|
||||
addressModeU: 'repeat',
|
||||
addressModeV: 'repeat',
|
||||
});
|
||||
|
||||
//this.renderShader.setVariable( "mySampler", sampler );
|
||||
|
||||
//this.renderShader.setVariable( "myTexture", texture );
|
||||
|
||||
const workgroupSize = 64;
|
||||
|
||||
const particleCount = positions.length / 4;
|
||||
this.dispatchCount = Math.ceil(particleCount / workgroupSize);
|
||||
|
||||
if( this.dispatchCount * workgroupSize <= particleCount ) {
|
||||
|
||||
console.error( "this.dispatchCount is to small", this.dispatchCount);
|
||||
|
||||
}
|
||||
this.initiateParticlesShader.execute( this.dispatchCount );
|
||||
|
||||
this.render();
|
||||
|
||||
}
|
||||
|
||||
updateTimeDelta() {
|
||||
|
||||
const now = performance.now();
|
||||
|
||||
this.deltaTimeValue = ( now - this.lastFrameTime ) / 1000;
|
||||
|
||||
this.lastFrameTime = now;
|
||||
|
||||
}
|
||||
|
||||
async render() {
|
||||
|
||||
this.updateTimeDelta();
|
||||
|
||||
const viewMatrixData = this.camera.getViewMatrix();
|
||||
|
||||
const projectionMatrixData = Matrix4.createProjectionMatrix( this.camera, this.canvas )
|
||||
|
||||
const viewProjectionMatrix = Matrix4.multiply( projectionMatrixData, viewMatrixData );
|
||||
|
||||
const cameraWorldMatrix = Matrix4.invert( viewMatrixData );
|
||||
|
||||
const cameraPosition = Matrix4.getColumn( cameraWorldMatrix, 3 );
|
||||
|
||||
this.renderShader.setVariable( "viewProjectionMatrix", viewProjectionMatrix );
|
||||
|
||||
//this.renderShader.setVariable( "time", this.lastFrameTime*.001 );
|
||||
|
||||
|
||||
this.simpleGravityShader.setVariable("deltaTime", this.deltaTimeValue);
|
||||
|
||||
this.simpleGravityShader.execute( this.dispatchCount );
|
||||
|
||||
this.renderShader.renderToCanvas( this.vertexCount , 1, 0 );
|
||||
|
||||
this.frameCount++;
|
||||
|
||||
requestAnimationFrame( this.render.bind( this ) );
|
||||
|
||||
}
|
||||
|
||||
async loadJSON( pathName ) {
|
||||
|
||||
const response = await fetch( pathName );
|
||||
|
||||
if ( !response.ok ){
|
||||
|
||||
throw new Error( `Failed to load shader: ${ pathName }` );
|
||||
|
||||
}
|
||||
|
||||
return await response.json();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
195
demos/particleHeader/index.html
Normal file
195
demos/particleHeader/index.html
Normal file
@@ -0,0 +1,195 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<title>10.000 Gpu Sphere Collision Demo.</title>
|
||||
</head>
|
||||
<base href="particleHeader/" />
|
||||
<link rel="stylesheet" href="./style/main.css" >
|
||||
|
||||
<script type="module">
|
||||
|
||||
import { ParticleSimulation } from "./demo.js";
|
||||
|
||||
|
||||
var particleSimulation = new ParticleSimulation();
|
||||
|
||||
|
||||
var useWebWorker = false;
|
||||
|
||||
const canvas = document.querySelector(".mainCanvas");
|
||||
|
||||
particleSimulation.setCanvas( canvas );
|
||||
|
||||
resizeCanvasToDisplaySize( canvas );
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
var worker;
|
||||
|
||||
if ( !useWebWorker ) {
|
||||
|
||||
await particleSimulation.setup( canvas, canvas.width, canvas.height, true );
|
||||
|
||||
console.log("document.bufferMap", document.bufferMap);
|
||||
|
||||
} else if ( useWebWorker ) {
|
||||
|
||||
worker = new Worker("../../framework/GpuWorker.js", { type: "module" });
|
||||
|
||||
worker.onmessage = function ( event ) {
|
||||
|
||||
console.log("From worker:", event.data);
|
||||
|
||||
};
|
||||
|
||||
const offscreen = canvas.transferControlToOffscreen();
|
||||
|
||||
worker.addEventListener("error", function ( event ) {
|
||||
|
||||
console.error( "Worker failed:",
|
||||
event.message, "at",
|
||||
event.filename + ":" +
|
||||
event.lineno + ":" +
|
||||
event.colno );
|
||||
|
||||
});
|
||||
|
||||
var request = {
|
||||
method: "setup",
|
||||
canvas: offscreen,
|
||||
width: canvas.width,
|
||||
height: canvas.height
|
||||
};
|
||||
|
||||
worker.postMessage( request, [offscreen] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
var events = new Array( "mousemove", "mousedown", "mouseup", "onwheel", "wheel" );
|
||||
|
||||
for ( var i = 0; i < events.length; i++ ) {
|
||||
|
||||
let eventName = events[i];
|
||||
|
||||
console.log("createEvent", eventName);
|
||||
|
||||
canvas.addEventListener( eventName, function ( event ) {
|
||||
|
||||
event.preventDefault();
|
||||
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
|
||||
const x = event.clientX - rect.left;
|
||||
|
||||
const y = event.clientY - rect.top;
|
||||
|
||||
var request = {
|
||||
method: eventName,
|
||||
clientX: x,
|
||||
clientY: y,
|
||||
deltaY: event.deltaY
|
||||
};
|
||||
|
||||
if ( useWebWorker ) {
|
||||
|
||||
worker.postMessage( request );
|
||||
|
||||
} else {
|
||||
|
||||
particleSimulation.eventManager[ eventName ]( request );
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
document.querySelector("#resetParticlesButton").addEventListener("click", function () {
|
||||
|
||||
var request = {
|
||||
method: "resetParticlePositions"
|
||||
};
|
||||
|
||||
if ( useWebWorker ) {
|
||||
|
||||
worker.postMessage( request );
|
||||
|
||||
} else {
|
||||
|
||||
particleSimulation.resetParticlePositions();
|
||||
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
function resizeCanvasToDisplaySize( canvas ) {
|
||||
|
||||
const width = window.innerWidth;
|
||||
const height = window.innerHeight;
|
||||
|
||||
var request = {
|
||||
method: "resize",
|
||||
width: width,
|
||||
height: height
|
||||
};
|
||||
|
||||
if ( useWebWorker ) {
|
||||
|
||||
worker.postMessage( request );
|
||||
|
||||
} else {
|
||||
|
||||
particleSimulation.eventManager.resize( request );
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
window.addEventListener( "resize", function () {
|
||||
|
||||
resizeCanvasToDisplaySize( canvas );
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
</script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js"></script>
|
||||
|
||||
<body>
|
||||
<div id="controlPanel">
|
||||
<div class="inputRow">
|
||||
<button id="resetParticlesButton">Reset Spheres</button>
|
||||
</div>
|
||||
</div><div id="particleSource" xmlns="http://www.w3.org/1999/xhtml" style="
|
||||
width: 882px;
|
||||
height: 67px;
|
||||
background: linear-gradient(135deg, #ff6600, #ffcc00);
|
||||
color: white;
|
||||
font-family: Arial, sans-serif;
|
||||
font-size: 24px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
text-align: center;
|
||||
border-radius: 20px;
|
||||
box-shadow: 0 0 20px rgba(0,0,0,0.3);
|
||||
">
|
||||
Hello GPU!
|
||||
</div>
|
||||
<canvas class="mainCanvas" width="1000" height="1000"></canvas>
|
||||
</body>
|
||||
</html>
|
||||
114
demos/particleHeader/style/main.css
Normal file
114
demos/particleHeader/style/main.css
Normal file
@@ -0,0 +1,114 @@
|
||||
html, body {
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
height: 100%;
|
||||
overflow: hidden;
|
||||
background:#111111;
|
||||
}
|
||||
|
||||
canvas {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
display: block;
|
||||
padding: 0;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
#controlPanel {
|
||||
position: absolute;
|
||||
top: 10px;
|
||||
left: 10px;
|
||||
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
padding: 20px;
|
||||
background: rgba(30, 30, 30, 0.6);
|
||||
backdrop-filter: blur(18px);
|
||||
-webkit-backdrop-filter: blur(18px);
|
||||
border-radius: 14px;
|
||||
box-shadow:
|
||||
0 4px 12px rgba(0, 0, 0, 0.5),
|
||||
0 0 0 1px rgba(255, 255, 255, 0.05);
|
||||
z-index: 1000;
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
.inputRow {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.inputRow label {
|
||||
color: #ccc;
|
||||
font-size: 14px;
|
||||
white-space: nowrap;
|
||||
width: 70px;
|
||||
}
|
||||
|
||||
.inputRow button,
|
||||
.inputRow input {
|
||||
flex-grow: 1;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #f0f0f5;
|
||||
background: rgba(28, 28, 30, 0.9);
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
padding: 8px 10px;
|
||||
cursor: pointer;
|
||||
box-shadow:
|
||||
0 1px 3px rgba(0, 0, 0, 0.4),
|
||||
0 0 6px rgba(28, 28, 30, 0.5);
|
||||
transition: background-color 0.2s ease, box-shadow 0.2s ease;
|
||||
}
|
||||
|
||||
.inputRow button:hover {
|
||||
background: rgba(40, 40, 44, 0.95);
|
||||
}
|
||||
|
||||
.inputRow input {
|
||||
background: rgba(20, 20, 20, 0.8);
|
||||
border: 1px solid rgba(255, 255, 255, 0.1);
|
||||
outline: none;
|
||||
box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.6);
|
||||
}
|
||||
|
||||
.inputRow select {
|
||||
flex-grow: 1;
|
||||
font-size: 14px;
|
||||
font-weight: 600;
|
||||
color: #f0f0f5;
|
||||
background: rgba(28, 28, 30, 0.9);
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
padding: 8px 10px;
|
||||
cursor: pointer;
|
||||
box-shadow:
|
||||
0 1px 3px rgba(0, 0, 0, 0.4),
|
||||
0 0 6px rgba(28, 28, 30, 0.5);
|
||||
transition: background-color 0.2s ease, box-shadow 0.2s ease;
|
||||
appearance: none; /* Remove default arrow */
|
||||
-webkit-appearance: none;
|
||||
-moz-appearance: none;
|
||||
background-image:
|
||||
linear-gradient(45deg, transparent 50%, #f0f0f5 50%),
|
||||
linear-gradient(135deg, #f0f0f5 50%, transparent 50%);
|
||||
background-position:
|
||||
calc(100% - 20px) calc(50% - 3px),
|
||||
calc(100% - 15px) calc(50% - 3px);
|
||||
background-size: 5px 5px;
|
||||
background-repeat: no-repeat;
|
||||
}
|
||||
|
||||
.inputRow select:hover {
|
||||
background: rgba(40, 40, 44, 0.95);
|
||||
}
|
||||
|
||||
.inputRow option {
|
||||
background: rgba(28, 28, 30, 0.95);
|
||||
color: #f0f0f5;
|
||||
}
|
||||
BIN
demos/particleHeader/textures/0_floorTiles_ddn.png
Executable file
BIN
demos/particleHeader/textures/0_floorTiles_ddn.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 447 KiB |
BIN
demos/particleHeader/textures/0_floorTiles_diff.png
Executable file
BIN
demos/particleHeader/textures/0_floorTiles_diff.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 1.2 MiB |
BIN
demos/particleHeader/textures/defaultnouvs.png
Executable file
BIN
demos/particleHeader/textures/defaultnouvs.png
Executable file
Binary file not shown.
|
After Width: | Height: | Size: 5.0 KiB |
114
demos/particleHeader/webgpu_framework.md
Normal file
114
demos/particleHeader/webgpu_framework.md
Normal file
@@ -0,0 +1,114 @@
|
||||
# Simplifying WebGPU with This Framework
|
||||
|
||||
WebGPU’s native API is complex and verbose. This framework reduces that complexity by providing simple classes and methods to manage shaders, buffers, and execution. Instead of handling low-level GPU commands, you focus on your logic.
|
||||
|
||||
## 1. Loading and Using a Shader
|
||||
|
||||
**Without Framework:**
|
||||
|
||||
```
|
||||
Manual setup of device, pipeline, bind groups, etc. Very verbose and error-prone
|
||||
const shaderModule = device.createShaderModule({ code: wgslSource });
|
||||
const pipeline = device.createComputePipeline({ ... });
|
||||
// Setup bind groups and command encoder manually
|
||||
```
|
||||
|
||||
**With Framework:**
|
||||
|
||||
```
|
||||
const shader = new Shader( "myComputeShader.wgsl" );
|
||||
|
||||
await shader.compile();
|
||||
|
||||
shader.setVariable( "someUniform", 42 );
|
||||
|
||||
shader.execute( 64 ); // runs compute with 64 workgroups
|
||||
```
|
||||
|
||||
The framework loads, compiles, sets uniforms, and dispatches the compute shader with simple method calls.
|
||||
|
||||
## 2. Setting Buffers
|
||||
|
||||
**Without Framework:**
|
||||
|
||||
```
|
||||
Create GPU buffer, write data, create bind group manually
|
||||
```
|
||||
|
||||
**With Framework:**
|
||||
|
||||
```
|
||||
const dataBuffer = gpu.createBuffer( new Float32Array([1, 2, 3]) );
|
||||
|
||||
shader.setBuffer( "inputBuffer", dataBuffer );
|
||||
```
|
||||
|
||||
Buffers are bound by name with a simple call.
|
||||
|
||||
## 3. Rendering to Canvas
|
||||
|
||||
**Without Framework:**
|
||||
|
||||
```
|
||||
Create render pipeline, set vertex buffers, encode commands manually
|
||||
```
|
||||
|
||||
**With Framework:**
|
||||
|
||||
```
|
||||
shader.setCanvas( canvasElement );
|
||||
|
||||
shader.setAttributes( vertexData );
|
||||
|
||||
shader.execute();
|
||||
```
|
||||
|
||||
The framework handles pipeline creation, drawing commands, and presentation automatically.
|
||||
|
||||
## 4. Camera and Matrix Utilities
|
||||
|
||||
**Example:**
|
||||
|
||||
```
|
||||
const camera = new Camera();
|
||||
|
||||
camera.position.set( 0, 1, 5 );
|
||||
|
||||
camera.updateViewMatrix();
|
||||
|
||||
shader.setVariable( "viewMatrix", camera.viewMatrix );
|
||||
```
|
||||
|
||||
The framework provides built-in classes for common math tasks.
|
||||
|
||||
## 5. Event Handling
|
||||
|
||||
**Example:**
|
||||
|
||||
```
|
||||
const events = new EventManager( canvasElement );
|
||||
|
||||
events.on( "mouseMove", (event) => {
|
||||
camera.rotate( event.deltaX, event.deltaY );
|
||||
shader.setVariable( "viewMatrix", camera.viewMatrix );
|
||||
shader.execute();
|
||||
});
|
||||
```
|
||||
|
||||
Separates input handling cleanly from GPU logic.
|
||||
|
||||
## Summary Table
|
||||
|
||||
| Task | WebGPU Native API | This Framework |
|
||||
| --- | --- | --- |
|
||||
| Load and compile shader | Many steps, manual setup | One line with `new Shader()` + `compile()` |
|
||||
| Set uniforms | Define bind groups and layouts | Simple `setVariable()` calls |
|
||||
| Bind buffers | Manual buffer creation and binding | `setBuffer()` by name |
|
||||
| Execute compute | Command encoder and dispatch calls | `execute(workgroupCount)` method |
|
||||
| Render to canvas | Complex pipeline and draw calls | `setCanvas()`, `setAttributes()`, `execute()` |
|
||||
| Handle math and camera | External libs or manual math | Built-in Matrix4, Camera classes |
|
||||
| Input handling | Manual event listeners | `EventManager` handles input cleanly |
|
||||
|
||||
---
|
||||
|
||||
This framework hides the low-level complexity behind a clean, simple API that accelerates WebGPU development and makes GPU programming accessible and maintainable.
|
||||
Reference in New Issue
Block a user