mirror of
https://github.com/DaanVandenBosch/phantasmal-world.git
synced 2025-04-05 07:18:29 +08:00
1 line
8.9 KiB
JavaScript
1 line
8.9 KiB
JavaScript
(window.webpackJsonp=window.webpackJsonp||[]).push([[3],{472:function(e,t,r){"use strict";var i;r.d(t,"d",(function(){return i})),r.d(t,"b",(function(){return n})),r.d(t,"a",(function(){return s})),r.d(t,"c",(function(){return o})),r.d(t,"f",(function(){return a})),r.d(t,"e",(function(){return u})),r.d(t,"g",(function(){return c})),function(e){e[e.PosNorm=0]="PosNorm",e[e.PosTex=1]="PosTex",e[e.PosNormTex=2]="PosNormTex"}(i||(i={}));const n=0,s=1,o=2;function a(e){switch(e){case i.PosNorm:return 24;case i.PosTex:return 16;case i.PosNormTex:return 28}}function u(e){switch(e){case i.PosTex:return-1;case i.PosNorm:case i.PosNormTex:return 12}}function c(e){switch(e){case i.PosNorm:return-1;case i.PosTex:return 12;case i.PosNormTex:return 24}}},496:function(e,t,r){"use strict";var i;r.d(t,"b",(function(){return i})),r.d(t,"a",(function(){return n})),function(e){e[e.RGBA_S3TC_DXT1=0]="RGBA_S3TC_DXT1",e[e.RGBA_S3TC_DXT3=1]="RGBA_S3TC_DXT3"}(i||(i={}));class n{constructor(e,t,r,i,n){this.gfx=e,this.format=t,this.width=r,this.height=i,this.data=n}upload(){null==this.gfx_texture&&(this.gfx_texture=this.gfx.create_texture(this.format,this.width,this.height,this.data))}destroy(){this.gfx.destroy_texture(this.gfx_texture)}}},519:function(e,t,r){"use strict";r.d(t,"a",(function(){return n})),r.d(t,"b",(function(){return s}));var i=r(463);class n{constructor(){this.root_node=new s(void 0,i.a.identity())}traverse(e,t){this.traverse_node(this.root_node,e,t)}traverse_node(e,t,r){const i=t(e,r);for(const r of e.children)this.traverse_node(r,t,i)}}class s{constructor(e,t,...r){this.mesh=e,this.transform=t,this._children=r}get children(){return this._children}add_child(e){this._children.push(e)}clear_children(){this._children.splice(0)}}},638:function(e,t,r){"use strict";r.d(t,"a",(function(){return o}));var i=r(519),n=r(594),s=r(463);class o{constructor(e){this.width=800,this.height=600,this.scene=new i.a,this.canvas_element=document.createElement("canvas"),this.schedule_render=()=>{null==this.animation_frame&&(this.animation_frame=requestAnimationFrame(this.call_render))},this.call_render=()=>{this.animation_frame=void 0,this.render()},this.mousedown=e=>{this.pointer_pos=new s.b(e.clientX,e.clientY),window.addEventListener("mousemove",this.mousemove),window.addEventListener("mouseup",this.mouseup),window.addEventListener("contextmenu",this.contextmenu)},this.mousemove=e=>{const t=new s.b(e.clientX,e.clientY),r=Object(s.g)(t,this.pointer_pos);1===e.buttons?this.camera.pan(-r.x,r.y,0):2===e.buttons&&this.camera.rotate(-r.x/(20*Math.PI),-r.y/(20*Math.PI)),this.pointer_pos=t,this.schedule_render()},this.mouseup=e=>{e.preventDefault(),this.pointer_pos=void 0,window.removeEventListener("mousemove",this.mousemove),window.removeEventListener("mouseup",this.mouseup)},this.wheel=e=>{switch(this.camera.projection){case n.b.Orthographic:e.deltaY<0?this.camera.zoom(1.1):this.camera.zoom(.9);break;case n.b.Perspective:e.deltaY<0?this.camera.pan(0,0,-2):this.camera.pan(0,0,2)}this.schedule_render()},this.contextmenu=e=>{e.preventDefault(),window.removeEventListener("contextmenu",this.contextmenu)},this.canvas_element.width=this.width,this.canvas_element.height=this.height,this.canvas_element.addEventListener("mousedown",this.mousedown),this.canvas_element.addEventListener("wheel",this.wheel,{passive:!0}),this.camera=new n.a(this.width,this.height,e)}dispose(){this.destroy_scene()}set_size(e,t){this.width=e,this.height=t,this.camera.set_viewport(e,t),this.schedule_render()}start_rendering(){this.schedule_render()}stop_rendering(){null!=this.animation_frame&&cancelAnimationFrame(this.animation_frame),this.animation_frame=void 0}destroy_scene(){this.scene.traverse(e=>{var t,r,i;null===(t=e.mesh)||void 0===t||t.destroy(this.gfx),null===(i=null===(r=e.mesh)||void 0===r?void 0:r.texture)||void 0===i||i.destroy(),e.mesh=void 0},void 0),this.scene.root_node.clear_children(),this.scene.root_node.transform=s.a.identity()}}},905:function(e,t,r){"use strict";r.r(t),r.d(t,"WebgpuRenderer",(function(){return l}));var i=r(23),n=r(472),s=r(638),o=r(463),a=r(496);class u{constructor(e,t){this.device=e,this.bind_group_layout=t}create_gfx_mesh(e,t,r,i){const n=this.device.createBuffer({size:64,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM}),s=this.device.createBindGroup({layout:this.bind_group_layout,entries:[{binding:0,resource:{buffer:n}},{binding:1,resource:this.device.createSampler({magFilter:"linear",minFilter:"linear"})},{binding:2,resource:i.gfx_texture.createView()}]}),o=this.device.createBuffer({size:t.byteLength,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.VERTEX});o.setSubData(0,new Uint8Array(t));const a=this.device.createBuffer({size:r.byteLength,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.INDEX});return a.setSubData(0,new Uint16Array(r)),{uniform_buffer:n,bind_group:s,vertex_buffer:o,index_buffer:a}}destroy_gfx_mesh(e){e&&(e.uniform_buffer.destroy(),e.vertex_buffer.destroy(),e.index_buffer.destroy())}create_texture(e,t,r,i){if(e===a.b.RGBA_S3TC_DXT1||e===a.b.RGBA_S3TC_DXT3){const e=new ArrayBuffer(16),n=new Uint32Array(e);n[0]=4294901760,n[1]=4278255360,n[2]=4278190335,n[3]=4278255615,t=2,r=2,i=e}const n=this.device.createTexture({size:{width:t,height:r,depth:1},format:"rgba8unorm",usage:GPUTextureUsage.COPY_DST|GPUTextureUsage.SAMPLED}),s=256*Math.ceil(4*t/256),o=s*r,u=this.device.createBuffer({size:o,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.COPY_SRC});let c;if(o===i.byteLength)c=new Uint8Array(i);else{c=new Uint8Array(o);const e=new Uint8Array(i);let n=0;for(let i=0;i<r;i++)for(let r=0;r<t;r++){const t=4*r+s*i;c[t]=e[n],c[t+1]=e[n+1],c[t+2]=e[n+2],c[t+3]=e[n+3],n+=4}}u.setSubData(0,c);const h=this.device.createCommandEncoder();return h.copyBufferToTexture({buffer:u,bytesPerRow:s,rowsPerImage:0},{texture:n},{width:t,height:r,depth:1}),this.device.defaultQueue.submit([h.finish()]),u.destroy(),n}destroy_texture(e){null==e||e.destroy()}}var c=function(e,t,r,i){return new(r||(r=Promise))((function(n,s){function o(e){try{u(i.next(e))}catch(e){s(e)}}function a(e){try{u(i.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,a)}u((i=i.apply(e,t||[])).next())}))};class h{constructor(e){this.http_client=e}load(e){return c(this,void 0,void 0,(function*(){return new Uint32Array(yield this.http_client.get(`/shaders/${e}.spv`).array_buffer())}))}}var d=function(e,t,r,i){return new(r||(r=Promise))((function(n,s){function o(e){try{u(i.next(e))}catch(e){s(e)}}function a(e){try{u(i.throw(e))}catch(e){s(e)}}function u(e){var t;e.done?n(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(o,a)}u((i=i.apply(e,t||[])).next())}))};const f=i.a.get("core/rendering/webgpu/WebgpuRenderer");class l extends s.a{constructor(e,t){super(e),this.disposed=!1,this.shader_loader=new h(t),this.initialize()}get gfx(){return this.gpu.gfx}initialize(){return d(this,void 0,void 0,(function*(){try{if(null==window.navigator.gpu)return void f.error("WebGPU not supported on this device.");const e=this.canvas_element.getContext("gpupresent");if(null==e)return void f.error("Failed to initialize gpupresent context.");const t=yield window.navigator.gpu.requestAdapter(),r=yield t.requestDevice(),i=yield this.shader_loader.load("vertex_shader.vert"),s=yield this.shader_loader.load("fragment_shader.frag");if(!this.disposed){const t="bgra8unorm",o=e.configureSwapChain({device:r,format:t}),a=r.createBindGroupLayout({entries:[{binding:0,visibility:GPUShaderStage.VERTEX,type:"uniform-buffer"},{binding:1,visibility:GPUShaderStage.FRAGMENT,type:"sampler"},{binding:2,visibility:GPUShaderStage.FRAGMENT,type:"sampled-texture"}]}),c=r.createRenderPipeline({layout:r.createPipelineLayout({bindGroupLayouts:[a]}),vertexStage:{module:r.createShaderModule({code:i}),entryPoint:"main"},fragmentStage:{module:r.createShaderModule({code:s}),entryPoint:"main"},primitiveTopology:"triangle-list",colorStates:[{format:t}],vertexState:{indexFormat:"uint16",vertexBuffers:[{arrayStride:Object(n.f)(n.d.PosTex),stepMode:"vertex",attributes:[{format:"float3",offset:0,shaderLocation:0},{format:"ushort2norm",offset:12,shaderLocation:1}]}]}});this.gpu={gfx:new u(r,a),device:r,swap_chain:o,pipeline:c},this.set_size(this.width,this.height)}}catch(e){f.error("Failed to initialize WebGPU renderer.",e)}}))}dispose(){this.disposed=!0,super.dispose()}set_size(e,t){this.gpu&&(this.canvas_element.width=e,this.canvas_element.height=t),super.set_size(e,t)}render(){if(this.gpu){const{device:e,swap_chain:t,pipeline:r}=this.gpu,i=e.createCommandEncoder(),n=t.getCurrentTexture().createView(),s=i.beginRenderPass({colorAttachments:[{attachment:n,loadValue:{r:.1,g:.1,b:.1,a:1}}]});s.setPipeline(r);const a=Object(o.e)(this.camera.projection_matrix,this.camera.view_matrix);this.scene.traverse((e,t)=>{const r=Object(o.e)(t,e.transform);if(e.mesh){const t=e.mesh.gfx_mesh;t.uniform_buffer.setSubData(0,r.data),s.setBindGroup(0,t.bind_group),s.setVertexBuffer(0,t.vertex_buffer),s.setIndexBuffer(t.index_buffer),s.drawIndexed(e.mesh.index_count,1,0,0,0)}return r},a),s.endPass(),e.defaultQueue.submit([i.finish()])}}}}}]); |