WebGPU Cube Map texture_cube normals var script document createElement script script type text javascript script async false script src https cdnjs cloudflare com ajax libs gl matrix 2 6 0 gl matrix min js document head appendChild script const canvas document createElement canvas document body appendChild canvas canvas style border 1px solid blue canvas width 512 canvas height 512 console log canvas width console log canvas height const gpu navigator gpu console log gpu const adapter await gpu requestAdapter const device await adapter requestDevice const context canvas getContext webgpu const devicePixelRatio 1 1 const presentationSize canvas width canvas clientWidth devicePixelRatio canvas height canvas clientHeight devicePixelRatio const presentationFormat navigator gpu getPreferredCanvasFormat context getPreferredFormat adapter no longer supported const presentationFormat navigator gpu getPreferredCanvasFormat context getPreferredFormat adapter no longer supported context configure device format presentationFormat console log presentationFormat presentationFormat texture image let imgs https notebook xbdev net var images test512 png https notebook xbdev net var images test512 png https notebook xbdev net var images test512 png https notebook xbdev net var images test512 png https notebook xbdev net var images test512 png https notebook xbdev net var images test512 png const textureSize width image width height image height depth 1 const texture device createTexture size textureSize dimension 2d format rgba8unorm usage GPUTextureUsage COPY_DST GPUTextureUsage RENDER_ATTACHMENT GPUTextureUsage SAMPLED const cubeTexture device createTexture size 512 512 6 dimension cube 3d format presentationFormat bgra8unorm rgba8unorm usage 0x4 0x2 GPUTextureUsage COPY_DST GPUTextureUsage TEXTURE_BINDING usage GPUTextureUsage COPY_DST GPUTextureUsage RENDER_ATTACHMENT let textureData new Uint8Array 6 512 512 4 for let i 0 i imgs length i const img document createElement img img src imgs i await img decode console log img width img width const imageCanvas document createElement canvas imageCanvas width 512 img width imageCanvas height 512 img height const imageCanvasContext imageCanvas getContext 2d imageCanvasContext drawImage img 0 0 imageCanvas width imageCanvas height const imageData imageCanvasContext getImageData 0 0 imageCanvas width imageCanvas height for let x 0 x 512 512 4 x textureData 512 512 4 i x imageData data x let textureData null const rowPitch Math ceil img width 4 256 256 console log rowPitch rowPitch if rowPitch img width console log easy textureData imageData data else textureData new Uint8Array rowPitch img height for let y 0 y img height y for let x 0 x img width x let i x 4 y rowPitch textureData i imageData data i textureData i 1 imageData data i 1 textureData i 2 imageData data i 2 textureData i 3 imageData data i 3 console log textureData byteLength textureData byteLength let textureData new Uint8Array 6 512 512 4 for let y 0 y 512 6 y for let x 0 x 512 x let i x 4 y 512 textureData i 220 imageData data i textureData i 1 0 imageData data i 1 textureData i 2 0 imageData data i 2 textureData i 3 255 imageData data i 3 for let k 0 k 6 512 512 4 4 k textureData k 4 0 120 textureData k 4 1 0 textureData k 4 2 120 textureData k 4 3 255 device queue writeTexture texture cubeTexture textureData offset 0 512 512 4 0 1048576 img width img height 4 i bytesPerRow 512 4 img width 4 rowsPerImage 512 img height 512 512 6 NOTE Fallback if Queue writeTexture isn t implemented const textureDataBuffer device createBuffer size textureData byteLength usage GPUBufferUsage COPY_DST GPUBufferUsage COPY_SRC device queue writeBuffer textureDataBuffer 0 textureData const textureLoadEncoder device createCommandEncoder textureLoadEncoder copyBufferToTexture buffer textureDataBuffer bytesPerRow img width 4 imageHeight img height texture img width img height 6 device queue submit textureLoadEncoder finish bytesPerRow of type GPUSize32 The stride in bytes between the beginning of each block row and the subsequent block row Number of block rows per single image of the texture rowsPerImage x bytesPerRow is the stride in bytes between the beginning of each image of data and the subsequent image GPUTextureView let cubeView cubeTexture createView format presentationFormat dimension 3d dimension cube aspect all mipLevelCount 0 arrayLayerCount 6 console log textureView cubeView const cubeSampler device createSampler minFilter linear magFilter linear Create vertex buffers and load data function createCube options options options let dimensions options dimensions 1 1 1 let position options position dimensions 0 2 dimensions 1 2 dimensions 2 2 let x position 0 let y position 1 let z position 2 let width dimensions 0 let height dimensions 1 let depth dimensions 2 let fbl x x y y z z depth let fbr x x width y y z z depth let ftl x x y y height z z depth let ftr x x width y y height z z depth let bbl x x y y z z let bbr x x width y y z z let btl x x y y height z z let btr x x width y y height z z let positions new Float32Array front fbl x fbl y fbl z fbr x fbr y fbr z ftl x ftl y ftl z ftl x ftl y ftl z fbr x fbr y fbr z ftr x ftr y ftr z right fbr x fbr y fbr z bbr x bbr y bbr z ftr x ftr y ftr z ftr x ftr y ftr z bbr x bbr y bbr z btr x btr y btr z back fbr x bbr y bbr z bbl x bbl y bbl z btr x btr y btr z btr x btr y btr z bbl x bbl y bbl z btl x btl y btl z left bbl x bbl y bbl z fbl x fbl y fbl z btl x btl y btl z btl x btl y btl z fbl x fbl y fbl z ftl x ftl y ftl z top ftl x ftl y ftl z ftr x ftr y ftr z btl x btl y btl z btl x btl y btl z ftr x ftr y ftr z btr x btr y btr z bottom bbl x bbl y bbl z bbr x bbr y bbr z fbl x fbl y fbl z fbl x fbl y fbl z bbr x bbr y bbr z fbr x fbr y fbr z let uvs new Float32Array front 0 0 1 0 0 1 0 1 1 0 1 1 right 0 0 1 0 0 1 0 1 1 0 1 1 back 0 0 1 0 0 1 0 1 1 0 1 1 left 0 0 1 0 0 1 0 1 1 0 1 1 top 0 0 1 0 0 1 0 1 1 0 1 1 bottom 0 0 1 0 0 1 0 1 1 0 1 1 let normals new Float32Array front 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 right 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 back 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 left 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 top 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 bottom 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 0 1 0 return positions normals uvs const cubeData createCube const numVertices cubeData positions length 3 const positionBuffer device createBuffer size cubeData positions byteLength usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST const normalBuffer device createBuffer size cubeData normals byteLength usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST const uvBuffer device createBuffer size cubeData uvs byteLength usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST device queue writeBuffer positionBuffer 0 cubeData positions device queue writeBuffer normalBuffer 0 cubeData normals device queue writeBuffer uvBuffer 0 cubeData uvs Uniform data const eyePosition new Float32Array 1 1 1 const lightPosition new Float32Array 1 1 1 const projectionMatrix mat4 create const viewMatrix mat4 create const modelMatrix mat4 create const viewProjectionMatrix mat4 create const mvpMatrix mat4 create const rotation 0 0 0 mat4 perspective projectionMatrix Math PI 2 canvas width canvas height 0 1 10 0 mat4 lookAt viewMatrix 1 1 1 0 0 0 0 1 0 mat4 multiply viewProjectionMatrix projectionMatrix viewMatrix Create uniform buffers and binding layout const vertexUniformBuffer device createBuffer size 128 usage GPUBufferUsage UNIFORM GPUBufferUsage COPY_DST const fragmentUniformBuffer device createBuffer size 32 usage GPUBufferUsage UNIFORM GPUBufferUsage COPY_DST device queue writeBuffer vertexUniformBuffer 0 modelMatrix device queue writeBuffer vertexUniformBuffer 64 viewProjectionMatrix device queue writeBuffer fragmentUniformBuffer 0 eyePosition device queue writeBuffer fragmentUniformBuffer 16 lightPosition const sceneUniformBindGroupLayout device createBindGroupLayout entries binding 0 visibility GPUShaderStage VERTEX buffer type uniform binding 1 visibility GPUShaderStage FRAGMENT buffer type uniform binding 2 visibility GPUShaderStage FRAGMENT sampler type filtering type non filtering binding 3 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension cube const sceneUniformBindGroup device createBindGroup layout sceneUniformBindGroupLayout entries binding 0 resource buffer vertexUniformBuffer binding 1 resource buffer fragmentUniformBuffer binding 2 resource cubeSampler binding 3 resource cubeView cubeTexture createView Create render pipeline let basicVertWGSL struct Uniforms worldMatrix mat4x4 f32 modelViewProjectionMatrix mat4x4 f32 binding 0 group 0 var uniform uniforms Uniforms struct VertexOutput builtin position Position vec4 f32 location 0 fragUV vec2 f32 location 1 norm vec3 f32 location 2 fragPosition vec4 f32 vertex fn main location 0 position vec4 f32 location 1 norm vec3 f32 location 2 uv vec2 f32 VertexOutput var output VertexOutput output Position uniforms modelViewProjectionMatrix position output Position uniforms modelViewProjectionMatrix uniforms worldMatrix position output norm vec3 f32 uniforms worldMatrix vec4 f32 norm 1 0 xyz output fragUV uv output fragPosition uniforms worldMatrix position 0 5 position vec4 f32 1 0 1 0 1 0 1 0 return output let basicPixelWGSL struct Uniforms eyePosition vec3 f32 lightPosition vec3 f32 group 0 binding 1 var uniform uniforms Uniforms group 0 binding 2 var mySampler sampler group 0 binding 3 var myTexture texture_cube f32 group 0 binding 3 var myTexture texture_cube f32 fragment fn main location 0 fragUV vec2 f32 location 1 norm vec3 f32 location 2 fragPosition vec4 f32 location 0 vec4 f32 let col vec4 f32 1 0 1 0 1 0 1 0 textureSample myTexture mySampler fragUV return textureSample myTexture mySampler vec3 f32 0 0 0 0 0 9 return vec4 f32 1 0 0 0 1 0 1 0 let sdir normalize vec3 f32 0 0 0 0 0 0 fragPosition xyz return textureSample myTexture mySampler sdir return textureSample myTexture mySampler norm let col textureSample myTexture mySampler norm let ldir uniforms eyePosition fragPosition xyz let pdir norm let ilum col dot ldir pdir return ilum return textureSample myTexture mySampler fragUV fragPosition return vec4 f32 1 0 0 0 1 0 1 0 const pipeline device createRenderPipeline layout device createPipelineLayout bindGroupLayouts sceneUniformBindGroupLayout vertex module device createShaderModule code basicVertWGSL entryPoint main buffers arrayStride 12 attributes shaderLocation 0 format float32x3 offset 0 arrayStride 12 attributes shaderLocation 1 format float32x3 offset 0 arrayStride 8 attributes shaderLocation 2 format float32x2 offset 0 fragment module device createShaderModule code basicPixelWGSL entryPoint main targets format presentationFormat primitive topology triangle list cullMode back depthStencil format depth24plus depthWriteEnabled true depthCompare less Render pass description const depthTexture device createTexture size canvas width canvas height 1 format depth24plus usage GPUTextureUsage RENDER_ATTACHMENT const renderPassDescription colorAttachments view null assign later context getCurrentTexture createView loadOp clear clearValue 0 1 0 1 storeOp store depthStencilAttachment view depthTexture createView depthLoadOp clear depthClearValue 1 depthStoreOp store stencilLoadValue 0 stencilStoreOp store let translateMat mat4 create let rotateXMat mat4 create let rotateYMat mat4 create let rotateZMat mat4 create let scaleMat mat4 create function draw Update uniform buffer function xformMatrix xform translate rotate scale translate translate 0 0 0 rotate rotate 0 0 0 scale scale 1 1 1 mat4 fromTranslation translateMat translate mat4 fromXRotation rotateXMat rotate 0 mat4 fromYRotation rotateYMat rotate 1 mat4 fromZRotation rotateZMat rotate 2 mat4 fromScaling scaleMat scale mat4 multiply xform rotateXMat scaleMat mat4 multiply xform rotateYMat xform mat4 multiply xform rotateZMat xform mat4 multiply xform translateMat xform rotation 0 0 01 rotation 2 0 005 xformMatrix modelMatrix null rotation null device queue writeBuffer vertexUniformBuffer 0 modelMatrix pipeline framebuffer renderPassDescription colorAttachments 0 view context getCurrentTexture createView const commandEncoder device createCommandEncoder const renderPass commandEncoder beginRenderPass renderPassDescription renderPass setPipeline pipeline renderPass setVertexBuffer 0 positionBuffer renderPass setVertexBuffer 1 normalBuffer renderPass setVertexBuffer 2 uvBuffer renderPass setBindGroup 0 sceneUniformBindGroup renderPass draw numVertices 1 0 0 renderPass end device queue submit commandEncoder finish requestAnimationFrame draw draw console log ready