1 line
3.2 KiB
JavaScript
1 line
3.2 KiB
JavaScript
import{a as D,b as z} from"../chunk-c8239659df5e5cce.js";class _{S;canvas;_adapter;_device;_context;telemetry;registry={onBeforeUpdate:[],onAfterUpdate:[],onUpdate:[],onStart:[]};constructor(S={}){this.config=S;this.config={fov:45,...S},this.canvas=document.querySelector("canvas"),this.canvas.width=window.innerWidth,this.canvas.height=window.innerHeight,this.telemetry=new z(this),this.init().catch((x)=>{const C=document.querySelector("main");if(C)C.innerHTML="<div><i>your browser didn't let me set up webgpu. firefox nightly or enable <code>dom.webgpu.enable</code>.</i></div>";throw new Error("Unable to initialize WebGPU. Your browser or machine may not support it.",x)})}async init(){if(!navigator.gpu)throw new Error("WebGPU not supported");if(this._adapter=await navigator.gpu.requestAdapter(),!this._adapter)throw new Error("No GPU adapter found");if(this._device=await this.adapter.requestDevice(),!this._device)throw new Error("No GPU device found");this._context=this.canvas.getContext("webgpu"),this.context.configure({device:this.device,format:"bgra8unorm",alphaMode:"premultiplied",...this.config.context})}get context(){if(!this._context)throw new Error("WebGPU context not initialized");return this._context}get adapter(){if(!this._adapter)throw new Error("WebGPU adapter not initialized");return this._adapter}get device(){if(!this._device)throw new Error("WebGPU device not initialized");return this._device}onBeforeUpdate(S){this.registry.onBeforeUpdate.push(S)}onAfterUpdate(S){this.registry.onAfterUpdate.push(S)}onUpdate(S){this.registry.onUpdate.push(S)}onStart(S){this.registry.onStart.push(S)}doUpdate(S){this.registry.onBeforeUpdate.forEach((x)=>x(S,this)),this.registry.onUpdate.forEach((x)=>x(S,this)),this.registry.onAfterUpdate.forEach((x)=>x(S,this))}doStart(S=0){this.registry.onStart.forEach((x)=>x(this,S))}async oneShot(S=0){this.doStart(S),this.doUpdate(S)}start(){this.doStart();const S=(x)=>{this.doUpdate(x),requestAnimationFrame(S)};requestAnimationFrame(S)}}class j extends D{S;x;depthTexture;uniformBuffer;texture;sampler;uniformBindGroup;renderPassDescriptor;constructor(S,x){super(S);this.app=S;this.mesh=x}onStart(){console.log("hello from meshrenderer!"),console.log(`i've got a ${this.mesh.constructor.name}`)}onUpdate(S){}}class k{S;constructor(S){this.config=S}buffer(S){const x=S.device.createBuffer({size:this.config.mesh.byteLength,usage:GPUBufferUsage.VERTEX,mappedAtCreation:!0});return new Float32Array(x.getMappedRange()).set(this.config.mesh),x.unmap(),x}pipeline(S,x,C){const q=x.module(S);return S.device.createRenderPipeline({layout:"auto",vertex:{module:q,entryPoint:"main",buffers:[{arrayStride:4,attributes:[{shaderLocation:0,offset:0,format:"float32x4"},{shaderLocation:2,offset:this.config.positionSize,format:"float32x4"},{shaderLocation:1,offset:this.config.positionSize+this.config.colorSize,format:"float32x4"}]}]},fragment:{module:q,entryPoint:"main",targets:[{format:"rgba8unorm"}]},primitive:{topology:"triangle-list",cullMode:C.cullMode??"back"},depthStencil:C.stencil&&{depthWriteEnabled:!0,depthCompare:"less",format:"depth24plus"}})}}var H=new Float32Array([-1,-1,0,1,1,1,1,1,0,0,1,-1,0,1,1,1,1,1,1,0,-1,1,0,1,1,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1]),E=new k({mesh:H,positionSize:16,colorSize:16,uvSize:8});var F=new _({fov:20}),X=new j(F,E);F.start();
|