WebGPU Example Parallax Basic Parallax Parallax Steep Parallax Steep Binary Search Parallax Occlusion use strict document body style height 600px const cubev p 1 0 1 0 1 0 u 0 0 1 0 c 1 0 0 0 0 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 1 0 1 0 c 1 0 0 0 0 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 0 0 0 0 c 1 0 0 0 0 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 1 0 0 0 c 1 0 0 0 0 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 1 0 0 0 c 0 0 1 0 0 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 0 0 0 0 c 0 0 1 0 0 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 1 0 1 0 c 0 0 1 0 0 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 0 0 1 0 c 0 0 1 0 0 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 1 0 0 0 c 0 0 0 0 1 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 0 0 0 0 c 0 0 0 0 1 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 1 0 1 0 c 0 0 0 0 1 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 0 0 1 0 c 0 0 0 0 1 0 n 0 0 0 0 1 0 p 1 0 1 0 1 0 u 1 0 0 0 c 1 0 0 0 1 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 0 0 0 0 c 1 0 0 0 1 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 1 0 1 0 c 1 0 0 0 1 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 0 0 1 0 c 1 0 0 0 1 0 n 1 0 0 0 0 0 p 1 0 1 0 1 0 u 0 0 1 0 c 1 0 1 0 0 0 n 0 0 1 0 0 0 p 1 0 1 0 1 0 u 1 0 1 0 c 1 0 1 0 0 0 n 0 0 1 0 0 0 p 1 0 1 0 1 0 u 0 0 0 0 c 1 0 1 0 0 0 n 0 0 1 0 0 0 p 1 0 1 0 1 0 u 1 0 0 0 c 1 0 1 0 0 0 n 0 0 1 0 0 0 p 1 0 1 0 1 0 u 0 0 1 0 c 1 0 1 0 1 0 n 0 0 1 0 0 0 p 1 0 1 0 1 0 u 1 0 1 0 c 1 0 1 0 1 0 n 0 0 1 0 0 0 p 1 0 1 0 1 0 u 0 0 0 0 c 1 0 1 0 1 0 n 0 0 1 0 0 0 p 1 0 1 0 1 0 u 1 0 0 0 c 1 0 1 0 1 0 n 0 0 1 0 0 0 const cubef 0 1 3 0 3 2 Face front 4 5 7 4 7 6 Face right 8 9 11 8 11 10 12 13 15 12 15 14 16 17 19 16 19 18 20 21 23 20 23 22 calculate tangent vector for uv coordinates calculate bi tangent using normal for let i 0 i cubef length i 3 let v0 cubev cubef i 0 let v1 cubev cubef i 1 let v2 cubev cubef i 2 let edge0 v1 p 0 v0 p 0 v1 p 1 v0 p 1 v1 p 2 v0 p 2 let edge1 v2 p 0 v0 p 0 v2 p 1 v0 p 1 v2 p 2 v0 p 2 let duv0 v1 u 0 v0 u 0 v1 u 1 v0 u 1 v1 u 2 v0 u 2 let duv1 v2 u 0 v0 u 0 v2 u 1 v0 u 1 v2 u 2 v0 u 2 var f 1 0 duv0 0 duv1 1 duv1 0 duv0 1 let tangentx f duv1 1 edge0 0 duv0 1 edge1 0 let tangenty f duv1 1 edge0 1 duv0 1 edge1 1 let tangentz f duv1 1 edge0 2 duv0 1 edge1 2 cubev cubef i 0 t tangentx tangenty tangentz cubev cubef i 1 t tangentx tangenty tangentz cubev cubef i 2 t tangentx tangenty tangentz console log vertices cubev length console log faces cubef length get some bounds for the model doesn t go off screen let min x 10000 y 10000 z 10000 let max x 10000 y 10000 z 10000 for let i 0 i cubev length i min x Math min min x cubev i p 0 min y Math min min y cubev i p 1 min z Math min min z cubev i p 2 max x Math max max x cubev i p 0 max y Math max max y cubev i p 1 max z Math max max z cubev i p 0 let delta x max x min x y max y min y z max z min z let middle x min x delta x 0 5 y min y delta y 0 5 z min z delta z 0 5 let dist Math sqrt delta x delta x delta y delta y delta z delta z console log model size dist console log min min x min y min z console log max max x max y max z console log middle middle x middle y middle z 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 let canvas document createElement canvas document body appendChild canvas canvas width 600 canvas height 600 const adapter await navigator gpu requestAdapter const device await adapter requestDevice const context canvas getContext webgpu const presentationSize canvas width window devicePixelRatio canvas height window devicePixelRatio const presentationFormat context getPreferredFormat adapter let translateMat mat4 create let rotateXMat mat4 create let rotateYMat mat4 create let rotateZMat mat4 create let scaleMat mat4 create const rotation 0 0 0 const modelMatrix mat4 create const viewMatrix mat4 create const projectionMatrix mat4 create const parallaxValue new Float32Array 0 5 const const1Value new Float32Array 0 02 const const2Value new Float32Array 0 06 const parallaxType new Float32Array 0 0 const uniformBuffer device createBuffer size 64 3 64 192 3 x mat4x4 3x64 192 usage GPUBufferUsage UNIFORM GPUBufferUsage COPY_DST mat4 perspective projectionMatrix Math PI 2 canvas width canvas height 0 001 1500 0 mat4 lookAt viewMatrix 0 0 0 8 dist 0 0 0 0 1 0 device queue writeBuffer uniformBuffer 0 modelMatrix device queue writeBuffer uniformBuffer 64 viewMatrix device queue writeBuffer uniformBuffer 128 projectionMatrix device queue writeBuffer uniformBuffer 192 parallaxValue device queue writeBuffer uniformBuffer 196 const1Value device queue writeBuffer uniformBuffer 200 const2Value device queue writeBuffer uniformBuffer 204 parallaxType async function setupTexture fileName const img document createElement img img src fileName https notebook xbdev net var images test512 png await img decode const basicTexture device createTexture size img width img height 1 format rgba8unorm presentationFormat bgra8unorm rgba8unorm usage 0x2 0x4 GPUTextureUsage COPY_DST GPUTextureUsage TEXTURE_BINDING console log presentationFormat presentationFormat bgra8unorm const imageCanvas document createElement canvas imageCanvas width img width imageCanvas height 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 let textureData new Uint8Array img width img height 4 for let x 0 x img width img height 4 x textureData x imageData data x await device queue writeTexture texture basicTexture textureData offset 0 bytesPerRow img width 4 rowsPerImage img height img width img height 1 return basicTexture end setupTexture let textureSampler device createSampler minFilter linear magFilter linear var texturesExamples https notebook xbdev net var images wall1 png https notebook xbdev net var images wall1Height png https notebook xbdev net var images wall1Height png https notebook xbdev net var images toybox png https notebook xbdev net var images toyboxHeight png https notebook xbdev net var images toyboxNormal png https notebook xbdev net var images bricks png https notebook xbdev net var images bricksHeight png https notebook xbdev net var images bricksNormal png let textures for let i 0 i texturesExamples length i let texture0 await setupTexture texturesExamples i 0 let colorTexture texture0 0 let texture1 await setupTexture texturesExamples i 1 let heightTexture texture1 0 let texture2 await setupTexture texturesExamples i 2 let normalTexture texture2 0 textures push c colorTexture h heightTexture n normalTexture 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 binding 3 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension 2d binding 4 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension 2d binding 5 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension 2d var bgDescriptor GPUBindGroupDescriptor layout sceneUniformBindGroupLayout entries binding 0 resource buffer uniformBuffer binding 1 resource buffer uniformBuffer binding 2 resource textureSampler binding 3 resource textures 0 c createView binding 4 resource textures 0 h createView binding 5 resource textures 0 n createView var sceneUniformBindGroup device createBindGroup bgDescriptor context configure device device format presentationFormat size presentationSize const vertWGSL block struct Uniforms matrixModel mat4x4 f32 matrixView mat4x4 f32 matrixProjection mat4x4 f32 parallaxValue f32 const1 f32 const2 f32 parallaxType f32 binding 0 group 0 var uniform uniforms Uniforms struct VSOut builtin position Position vec4 f32 location 0 verPos vec3 f32 location 1 tcoord vec2 f32 location 2 normal vec3 f32 location 3 tangent vec3 f32 location 4 color vec3 f32 location 5 eyePos vec3 f32 stage vertex fn main location 0 inPos vec3 f32 location 1 tcoord vec2 f32 location 2 normal vec3 f32 location 3 tangent vec3 f32 location 4 color vec3 f32 VSOut var MVP uniforms matrixProjection uniforms matrixView uniforms matrixModel var MV uniforms matrixView uniforms matrixModel var vsOut VSOut vsOut Position MVP vec4 f32 inPos 1 0 vsOut verPos MV vec4 f32 inPos 1 0 xyz vsOut normal MV vec4 f32 normal 0 0 xyz vsOut tangent MV vec4 f32 tangent 0 0 xyz vsOut tcoord tcoord vsOut color color vsOut eyePos vec3 f32 uniforms matrixView 3 1 uniforms matrixView 3 2 uniforms matrixView 3 3 return vsOut const fragWGSL block struct Uniforms matrixModel mat4x4 f32 matrixView mat4x4 f32 matrixProjection mat4x4 f32 parallaxValue f32 const1 f32 const2 f32 parallaxType f32 binding 1 group 0 var uniform uniforms Uniforms group 0 binding 2 var mySampler sampler group 0 binding 3 var myTexture0 texture_2d f32 group 0 binding 4 var myTexture1 texture_2d f32 group 0 binding 5 var myTexture2 texture_2d f32 fn ParallaxMapping texCoords vec2 f32 viewDir vec3 f32 vec2 f32 let height textureSample myTexture1 mySampler texCoords x uniforms const1 let p viewDir xy viewDir z height uniforms parallaxValue return texCoords p fn ParallaxMappingSteep texCoords vec2 f32 viewDir vec3 f32 vec2 f32 let numSteps 20 0 var dir viewDir xy uniforms parallaxValue uniforms const1 var currTexCoords texCoords var currDepth textureSample myTexture1 mySampler currTexCoords x var stepSize 1 0 for var i 0 0 i numSteps i i 1 0 let stepDepth i numSteps 0 0 1 0 let texDepth textureSample myTexture1 mySampler currTexCoords x if texDepth stepDepth break currTexCoords currTexCoords dir stepSize return currTexCoords Binary Search enhancement to Steep doesn t just break out but goes back and halves the search step size goes further and further until maximum iterations has been reached fn ParallaxMappingSteepBS texCoords vec2 f32 viewDir vec3 f32 vec2 f32 let numSteps 20 0 var dir viewDir xy uniforms parallaxValue uniforms const1 var currTexCoords texCoords var currDepth textureSample myTexture1 mySampler currTexCoords x var stepSize 1 0 var i 0 0 var stepDepth 0 0 for var k 0 0 k numSteps k k 1 0 stepDepth stepDepth 1 0 numSteps stepSize 0 0 1 0 let texDepth textureSample myTexture1 mySampler currTexCoords x if texDepth stepDepth currTexCoords currTexCoords dir stepSize stepDepth stepDepth 1 0 numSteps stepSize i i 1 0 stepSize stepSize 0 5 currTexCoords currTexCoords dir stepSize i i 1 0 return currTexCoords fn ParallaxMappingOcclusion texCoords vec2 f32 viewDir vec3 f32 vec2 f32 let numLayers 20 0 let layerDepth 1 0 numLayers var currentLayerDepth 0 0 let P viewDir xy uniforms parallaxValue uniforms const1 let deltaTexCoords P numLayers var currentTexCoords texCoords var currentDepthMapValue textureSample myTexture1 mySampler currentTexCoords x while currentLayerDepth currentDepthMapValue for var i 0 0 i numLayers i i 1 0 if currentLayerDepth currentDepthMapValue break currentTexCoords currentTexCoords deltaTexCoords currentDepthMapValue textureSample myTexture1 mySampler currentTexCoords x currentLayerDepth currentLayerDepth layerDepth var prevTexCoords currentTexCoords deltaTexCoords let afterDepth currentDepthMapValue currentLayerDepth let beforeDepth textureSample myTexture1 mySampler prevTexCoords x currentLayerDepth layerDepth let weight afterDepth afterDepth beforeDepth let finalTexCoords prevTexCoords weight currentTexCoords 1 0 weight return finalTexCoords stage fragment fn main location 0 verPos vec3 f32 location 1 tcoord vec2 f32 location 2 normal vec3 f32 location 3 tangent vec3 f32 location 4 color vec3 f32 location 5 eyePos vec3 f32 location 0 vec4 f32 let parallaxValue uniforms parallaxValue 0 5 let const1 uniforms const1 0 02 let const2 uniforms const2 0 06 let parallaxType uniforms parallaxType var bt cross normalize normal normalize tangent let tbn mat3x3 f32 normalize tangent normalize bt normalize normal var eyePos2 vec3 f32 0 0 0 0 0 8 3 464101 var EyeT tbn normalize eyePos2 verPos var texCoords tcoord if parallaxType 0 0 texCoords ParallaxMapping tcoord EyeT if parallaxType 1 0 texCoords ParallaxMappingSteep tcoord EyeT if parallaxType 2 0 texCoords ParallaxMappingSteepBS tcoord EyeT if parallaxType 3 0 texCoords ParallaxMappingOcclusion tcoord EyeT var tnormal textureSample myTexture2 mySampler texCoords xyz tnormal normalize tnormal 2 0 1 0 let light clamp dot vec3 f32 0 0 0 0 1 0 tnormal 0 5 1 0 var fragColor textureSample myTexture0 mySampler texCoords light fragColor a 1 0 return fragColor var positionBuffer null var colorBuffer null var tcoordsBuffer null var normalBuffer null var tangentBuffer null var indexBuffer null console log creating buffers let arrays v u n f t c for let i 0 i cubev length i arrays v push cubev i p 0 arrays v push cubev i p 1 arrays v push cubev i p 2 arrays c push cubev i c 0 arrays c push cubev i c 1 arrays c push cubev i c 2 arrays u push cubev i u 0 arrays u push cubev i u 1 arrays n push cubev i n 0 arrays n push cubev i n 1 arrays n push cubev i n 2 arrays t push cubev i t 0 arrays t push cubev i t 1 arrays t push cubev i t 2 for let i 0 i cubef length i arrays f push cubef i let positions new Float32Array arrays v let colors new Float32Array arrays c let tcoords new Float32Array arrays u let normals new Float32Array arrays n let tangents new Float32Array arrays t let indices new Uint32Array arrays f positionBuffer device createBuffer size positions byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST colorBuffer device createBuffer size colors byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST tcoordBuffer device createBuffer size tcoords byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST normalBuffer device createBuffer size normals byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST tangentBuffer device createBuffer size tangents byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST indexBuffer device createBuffer size indices byteLength 3 3 usage GPUBufferUsage INDEX GPUBufferUsage COPY_DST device queue writeBuffer positionBuffer 0 positions device queue writeBuffer colorBuffer 0 colors device queue writeBuffer tcoordBuffer 0 tcoords device queue writeBuffer normalBuffer 0 normals device queue writeBuffer tangentBuffer 0 tangents device queue writeBuffer indexBuffer 0 indices const pipeline device createRenderPipeline layout device createPipelineLayout bindGroupLayouts sceneUniformBindGroupLayout vertex module device createShaderModule code vertWGSL entryPoint main buffers arrayStride 12 attributes shaderLocation 0 format float32x3 offset 0 arrayStride 8 attributes shaderLocation 1 format float32x2 offset 0 arrayStride 12 attributes shaderLocation 2 format float32x3 offset 0 arrayStride 12 attributes shaderLocation 3 format float32x3 offset 0 arrayStride 12 attributes shaderLocation 4 format float32x3 offset 0 fragment module device createShaderModule code fragWGSL entryPoint main targets format presentationFormat primitive topology triangle list frontFace ccw cullMode back depthStencil format depth24plus depthWriteEnabled true depthCompare less const depthTexture device createTexture size canvas width canvas height 1 format depth24plus usage GPUTextureUsage RENDER_ATTACHMENT const renderPassDescription colorAttachments view context getCurrentTexture createView loadValue 0 0 0 8 0 8 1 clear screen storeOp store depthStencilAttachment view depthTexture createView depthLoadValue 1 depthStoreOp store stencilLoadValue 0 stencilStoreOp store rotation 0 0 7 function frame rotation 1 0 001 rotation 0 0 0023 let translate middle x middle y middle z mat4 fromTranslation translateMat translate mat4 fromXRotation rotateXMat rotation 0 mat4 fromYRotation rotateYMat rotation 1 mat4 fromZRotation rotateZMat rotation 2 mat4 multiply modelMatrix rotateYMat rotateZMat mat4 multiply modelMatrix modelMatrix rotateXMat mat4 multiply modelMatrix modelMatrix translateMat device queue writeBuffer uniformBuffer 0 modelMatrix device queue writeBuffer uniformBuffer 192 parallaxValue device queue writeBuffer uniformBuffer 196 const1Value device queue writeBuffer uniformBuffer 200 const2Value device queue writeBuffer uniformBuffer 204 parallaxType 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 tcoordBuffer renderPass setVertexBuffer 2 normalBuffer renderPass setVertexBuffer 3 tangentBuffer renderPass setVertexBuffer 4 colorBuffer renderPass setIndexBuffer indexBuffer uint32 renderPass setBindGroup 0 sceneUniformBindGroup renderPass drawIndexed cubef length 1 renderPass endPass device queue submit commandEncoder finish requestAnimationFrame frame frame let rstart x rotation 0 y rotation 1 z rotation 2 let mstart x 0 y 0 let dragging false document onmousedown function e if e button 2 return dragging true mstart x e pageX mstart y e pageY rstart x rotation 0 y rotation 1 z rotation 2 document onmouseup function e dragging false document onmousemove function e if dragging return let s 0 01 rotation 0 rstart x mstart y e pageY s rotation 1 rstart y mstart x e pageX s let var2 document createElement div document body appendChild var2 var2 style position absolute var2 style left 30px var2 style top 10px var2 style width 300px var2 innerHTML fieldset id var2 legend Select type legend label for radio1 Rocks label input type radio value 0 name myval2 id radio1 checked checked label for radio2 Toy label input type radio value 1 name myval2 id radio2 label for radio 3 Brick label input type radio value 2 name myval2 id radio3 fieldset let var3 document createElement div document body appendChild var3 var3 style position absolute var3 style left 30px var3 style bottom 10px var3 style width 300px var3 innerHTML fieldset id var3 legend Select type legend label for radio1 Basic label input type radio value 0 name myval3 id radio1 checked checked label for radio2 Steep label input type radio value 1 name myval3 id radio2 label for radio3 SteepBS label input type radio value 2 name myval3 id radio3 label for radio4 Occlusion label input type radio value 3 name myval3 id radio4 fieldset let var1 document createElement div document body appendChild var1 var1 id var1 var1 style position absolute var1 style left 35px var1 style top 75px var1 style width 200px let const1 document createElement div document body appendChild const1 const1 id const1 const1 style position absolute const1 style left 35px const1 style top 95px const1 style width 200px let const2 document createElement div document body appendChild const2 const2 id const2 const2 style position absolute const2 style left 35px const2 style top 115px const2 style width 200px let link document createElement link link href https code jquery com ui 1 10 4 themes ui lightness jquery ui css link rel stylesheet link async false document body appendChild link let script1 document createElement script script1 src https code jquery com jquery 1 10 2 js script1 type text javascript script1 async false document body appendChild script1 let script2 document createElement script script2 src https code jquery com ui 1 10 4 jquery ui js script2 type text javascript script2 async false document body appendChild script2 document body oncontextmenu function return false script2 onload function var1 slider values 0 5 min 0 0 max 2 0 step 0 01 slide function event ui ui handle text ui value parallaxValue 0 parseFloat ui value const1 slider values const1Value 0 min 0 5 max 0 5 step 0 01 slide function event ui ui handle text ui value const1Value 0 parseFloat ui value const2 slider values const2Value 0 min 0 5 max 0 5 step 0 01 slide function event ui ui handle text ui value const2Value 0 parseFloat ui value ui slider handle each function this text this parent slider values 0 var2 change function let selectedValue input name myval2 checked val let indx parseFloat selectedValue bgDescriptor entries 3 resource textures indx c createView bgDescriptor entries 4 resource textures indx h createView bgDescriptor entries 5 resource textures indx n createView sceneUniformBindGroup device createBindGroup bgDescriptor var3 change function let selectedValue input name myval3 checked val let indx parseFloat selectedValue parallaxType 0 indx end onload console log ready console log right click and drag to rotate shape
ample myTexture1 mySampler currTexCoords x var stepSize 1 0 var i 0 0 var stepDepth 0 0 for var k 0 0 k numSteps k k 1 0 stepDepth stepDepth 1 0 numSteps stepSize 0 0 1 0 let texDepth textureSample myTexture1 mySampler currTexCoords x if texDepth stepDepth currTexCoords currTexCoords dir stepSize stepDepth stepDepth 1 0 numSteps stepSize i i 1 0 stepSize stepSize 0 5 currTexCoords currTexCoords dir stepSize i i 1 0 return currTexCoords fn ParallaxMappingOcclusion texCoords vec2 f32 viewDir vec3 f32 vec2 f32 let numLayers 20 0 let layerDepth 1 0 numLayers var currentLayerDepth 0 0 let P viewDir xy uniforms parallaxValue uniforms const1 let deltaTexCoords P numLayers var currentTexCoords texCoords var currentDepthMapValue textureSample myTexture1 mySampler currentTexCoords x while currentLayerDepth currentDepthMapValue for var i 0 0 i numLayers i i 1 0 if currentLayerDepth currentDepthMapValue break currentTexCoords currentTexCoords deltaTexCoords currentDepthMapValue textureSample myTexture1 mySampler currentTexCoords x currentLayerDepth currentLayerDepth layerDepth var prevTexCoords currentTexCoords deltaTexCoords let afterDepth currentDepthMapValue currentLayerDepth let beforeDepth textureSample myTexture1 mySampler prevTexCoords x currentLayerDepth layerDepth let weight afterDepth afterDepth beforeDepth let finalTexCoords prevTexCoords weight currentTexCoords 1 0 weight return finalTexCoords stage fragment fn main location 0 verPos vec3 f32 location 1 tcoord vec2 f32 location 2 normal vec3 f32 location 3 tangent vec3 f32 location 4 color vec3 f32 location 5 eyePos vec3 f32 location 0 vec4 f32 let parallaxValue uniforms parallaxValue 0 5 let const1 uniforms const1 0 02 let const2 uniforms const2 0 06 let parallaxType uniforms parallaxType var bt cross normalize normal normalize tangent let tbn mat3x3 f32 normalize tangent normalize bt normalize normal var eyePos2 vec3 f32 0 0 0 0 0 8 3 464101 var EyeT tbn normalize eyePos2 verPos var texCoords tcoord if parallaxType 0 0 texCoords ParallaxMapping tcoord EyeT if parallaxType 1 0 texCoords ParallaxMappingSteep tcoord EyeT if parallaxType 2 0 texCoords ParallaxMappingSteepBS tcoord EyeT if parallaxType 3 0 texCoords ParallaxMappingOcclusion tcoord EyeT var tnormal textureSample myTexture2 mySampler texCoords xyz tnormal normalize tnormal 2 0 1 0 let light clamp dot vec3 f32 0 0 0 0 1 0 tnormal 0 5 1 0 var fragColor textureSample myTexture0 mySampler texCoords light fragColor a 1 0 return fragColor var positionBuffer null var colorBuffer null var tcoordsBuffer null var normalBuffer null var tangentBuffer null var indexBuffer null console log creating buffers let arrays v u n f t c for let i 0 i cubev length i arrays v push cubev i p 0 arrays v push cubev i p 1 arrays v push cubev i p 2 arrays c push cubev i c 0 arrays c push cubev i c 1 arrays c push cubev i c 2 arrays u push cubev i u 0 arrays u push cubev i u 1 arrays n push cubev i n 0 arrays n push cubev i n 1 arrays n push cubev i n 2 arrays t push cubev i t 0 arrays t push cubev i t 1 arrays t push cubev i t 2 for let i 0 i cubef length i arrays f push cubef i let positions new Float32Array arrays v let colors new Float32Array arrays c let tcoords new Float32Array arrays u let normals new Float32Array arrays n let tangents new Float32Array arrays t let indices new Uint32Array arrays f positionBuffer device createBuffer size positions byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST colorBuffer device createBuffer size colors byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST tcoordBuffer device createBuffer size tcoords byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST normalBuffer device createBuffer size normals byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST tangentBuffer device createBuffer size tangents byteLength 3 3 usage GPUBufferUsage VERTEX GPUBufferUsage COPY_DST indexBuffer device createBuffer size indices byteLength 3 3 usage GPUBufferUsage INDEX GPUBufferUsage COPY_DST device queue writeBuffer positionBuffer 0 positions device queue writeBuffer colorBuffer 0 colors device queue writeBuffer tcoordBuffer 0 tcoords device queue writeBuffer normalBuffer 0 normals device queue writeBuffer tangentBuffer 0 tangents device queue writeBuffer indexBuffer 0 indices const pipeline device createRenderPipeline layout device createPipelineLayout bindGroupLayouts sceneUniformBindGroupLayout vertex module device createShaderModule code vertWGSL entryPoint main buffers arrayStride 12 attributes shaderLocation 0 format float32x3 offset 0 arrayStride 8 attributes shaderLocation 1 format float32x2 offset 0 arrayStride 12 attributes shaderLocation 2 format float32x3 offset 0 arrayStride 12 attributes shaderLocation 3 format float32x3 offset 0 arrayStride 12 attributes shaderLocation 4 format float32x3 offset 0 fragment module device createShaderModule code fragWGSL entryPoint main targets format presentationFormat primitive topology triangle list frontFace ccw cullMode back depthStencil format depth24plus depthWriteEnabled true depthCompare less const depthTexture device createTexture size canvas width canvas height 1 format depth24plus usage GPUTextureUsage RENDER_ATTACHMENT const renderPassDescription colorAttachments view context getCurrentTexture createView loadValue 0 0 0 8 0 8 1 clear screen storeOp store depthStencilAttachment view depthTexture createView depthLoadValue 1 depthStoreOp store stencilLoadValue 0 stencilStoreOp store rotation 0 0 7 function frame rotation 1 0 001 rotation 0 0 0023 let translate middle x middle y middle z mat4 fromTranslation translateMat translate mat4 fromXRotation rotateXMat rotation 0 mat4 fromYRotation rotateYMat rotation 1 mat4 fromZRotation rotateZMat rotation 2 mat4 multiply modelMatrix rotateYMat rotateZMat mat4 multiply modelMatrix modelMatrix rotateXMat mat4 multiply modelMatrix modelMatrix translateMat device queue writeBuffer uniformBuffer 0 modelMatrix device queue writeBuffer uniformBuffer 192 parallaxValue device queue writeBuffer uniformBuffer 196 const1Value device queue writeBuffer uniformBuffer 200 const2Value device queue writeBuffer uniformBuffer 204 parallaxType 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 tcoordBuffer renderPass setVertexBuffer 2 normalBuffer renderPass setVertexBuffer 3 tangentBuffer renderPass setVertexBuffer 4 colorBuffer renderPass setIndexBuffer indexBuffer uint32 renderPass setBindGroup 0 sceneUniformBindGroup renderPass drawIndexed cubef length 1 renderPass endPass device queue submit commandEncoder finish requestAnimationFrame frame frame let rstart x rotation 0 y rotation 1 z rotation 2 let mstart x 0 y 0 let dragging false document onmousedown function e if e button 2 return dragging true mstart x e pageX mstart y e pageY rstart x rotation 0 y rotation 1 z rotation 2 document onmouseup function e dragging false document onmousemove function e if dragging return let s 0 01 rotation 0 rstart x mstart y e pageY s rotation 1 rstart y mstart x e pageX s let var2 document createElement div document body appendChild var2 var2 style position absolute var2 style left 30px var2 style top 10px var2 style width 300px var2 innerHTML fieldset id var2 legend Select type legend label for radio1 Rocks label input type radio value 0 name myval2 id radio1 checked checked label for radio2 Toy label input type radio value 1 name myval2 id radio2 label for radio 3 Brick label input type radio value 2 name myval2 id radio3 fieldset let var3 document createElement div document body appendChild var3 var3 style position absolute var3 style left 30px var3 style bottom 10px var3 style width 300px var3 innerHTML fieldset id var3 legend Select type legend label for radio1 Basic label input type radio value 0 name myval3 id radio1 checked checked label for radio2 Steep label input type radio value 1 name myval3 id radio2 label for radio3 SteepBS label input type radio value 2 name myval3 id radio3 label for radio4 Occlusion label input type radio value 3 name myval3 id radio4 fieldset let var1 document createElement div document body appendChild var1 var1 id var1 var1 style position absolute var1 style left 35px var1 style top 75px var1 style width 200px let const1 document createElement div document body appendChild const1 const1 id const1 const1 style position absolute const1 style left 35px const1 style top 95px const1 style width 200px let const2 document createElement div document body appendChild const2 const2 id const2 const2 style position absolute const2 style left 35px const2 style top 115px const2 style width 200px let link document createElement link link href https code jquery com ui 1 10 4 themes ui lightness jquery ui css link rel stylesheet link async false document body appendChild link let script1 document createElement script script1 src https code jquery com jquery 1 10 2 js script1 type text javascript script1 async false document body appendChild script1 let script2 document createElement script script2 src https code jquery com ui 1 10 4 jquery ui js script2 type text javascript script2 async false document body appendChild script2 document body oncontextmenu function return false script2 onload function var1 slider values 0 5 min 0 0 max 2 0 step 0 01 slide function event ui ui handle text ui value parallaxValue 0 parseFloat ui value const1 slider values const1Value 0 min 0 5 max 0 5 step 0 01 slide function event ui ui handle text ui value const1Value 0 parseFloat ui value const2 slider values const2Value 0 min 0 5 max 0 5 step 0 01 slide function event ui ui handle text ui value const2Value 0 parseFloat ui value ui slider handle each function this text this parent slider values 0 var2 change function let selectedValue input name myval2 checked val let indx parseFloat selectedValue bgDescriptor entries 3 resource textures indx c createView bgDescriptor entries 4 resource textures indx h createView bgDescriptor entries 5 resource textures indx n createView sceneUniformBindGroup device createBindGroup bgDescriptor var3 change function let selectedValue input name myval3 checked val let indx parseFloat selectedValue parallaxType 0 indx end onload console log ready console log right click and drag to rotate shape