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 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 8 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 async function setupTexture fileName let textureSampler device createSampler minFilter linear magFilter linear 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 presentationFormat bgra8unorm rgba8unorm usage 0x2 0x4 GPUTextureUsage COPY_DST GPUTextureUsage TEXTURE_BINDING 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 textureSampler end setupTexture let texture0 await setupTexture https notebook xbdev net var images wall1 png let colorTexture texture0 0 let colorSampler texture0 1 console log texture0 Object values texture0 let texture1 await setupTexture https notebook xbdev net var images wall1normal png let normalTexture texture1 0 let normalSampler texture1 1 console log texture1 Object values texture1 const sceneUniformBindGroupLayout device createBindGroupLayout entries binding 0 visibility GPUShaderStage VERTEX buffer type uniform binding 1 visibility GPUShaderStage FRAGMENT sampler type filtering binding 2 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension 2d binding 3 visibility GPUShaderStage FRAGMENT texture sampleType float viewDimension 2d const sceneUniformBindGroup device createBindGroup layout sceneUniformBindGroupLayout entries binding 0 resource buffer uniformBuffer binding 1 resource colorSampler binding 2 resource colorTexture createView binding 3 resource normalTexture createView context configure device device format presentationFormat size presentationSize const vertWGSL block struct Uniforms matrixModel mat4x4 f32 matrixView mat4x4 f32 matrixProjection mat4x4 f32 parallaxValue 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 location 6 parallaxValue 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 parallaxValue uniforms parallaxValue vsOut eyePos vec3 f32 uniforms matrixView 3 1 uniforms matrixView 3 2 uniforms matrixView 3 3 return vsOut const fragWGSL group 0 binding 1 var mySampler sampler group 0 binding 2 var myTexture0 texture_2d f32 group 0 binding 3 var myTexture1 texture_2d f32 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 6 parallaxValue f32 location 0 vec4 f32 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 let Height textureSample myTexture1 mySampler tcoord x let Offset Height 0 06 parallaxValue 0 02 parallaxValue let texCoords Offset EyeT xy tcoord if parallaxValue is 0 just use the default color no parallax if parallaxValue 0 0 return textureSample myTexture0 mySampler tcoord let brightness clamp dot vec3 f32 0 0 0 0 1 0 normalize normal 0 1 1 0 return vec4 f32 color brightness 1 0 var fragColor textureSample myTexture0 mySampler texCoords 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 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 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 mstart x 0 y 0 let dragging false document onmousedown function e dragging true mstart x e pageX mstart y e pageY document onmouseup function e dragging false document onmousemove function e if dragging return let s 0 01 rotation 0 mstart y e pageY s rotation 1 mstart x e pageX s console log ready
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 location 6 parallaxValue 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 parallaxValue uniforms parallaxValue vsOut eyePos vec3 f32 uniforms matrixView 3 1 uniforms matrixView 3 2 uniforms matrixView 3 3 return vsOut const fragWGSL group 0 binding 1 var mySampler sampler group 0 binding 2 var myTexture0 texture_2d f32 group 0 binding 3 var myTexture1 texture_2d f32 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 6 parallaxValue f32 location 0 vec4 f32 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 let Height textureSample myTexture1 mySampler tcoord x let Offset Height 0 06 parallaxValue 0 02 parallaxValue let texCoords Offset EyeT xy tcoord if parallaxValue is 0 just use the default color no parallax if parallaxValue 0 0 return textureSample myTexture0 mySampler tcoord let brightness clamp dot vec3 f32 0 0 0 0 1 0 normalize normal 0 1 1 0 return vec4 f32 color brightness 1 0 var fragColor textureSample myTexture0 mySampler texCoords 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 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 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 mstart x 0 y 0 let dragging false document onmousedown function e dragging true mstart x e pageX mstart y e pageY document onmouseup function e dragging false document onmousemove function e if dragging return let s 0 01 rotation 0 mstart y e pageY s rotation 1 mstart x e pageX s console log ready