Notebook - Welcome to Notebook

Contact/Report Bugs
You can contact me at: bkenwright@xbdev.net












WebGPU Example Cube Texture Rotation transform camera no lighting 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 width 400 canvas height 400 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 Set up WebGPU texture image const img document createElement img img src https notebook xbdev net var images food jpg await Promise all img decode const presentationFormat context getPreferredFormat adapter context configure device format presentationFormat Create texture sampler and load data const imageCanvas document createElement canvas imageCanvas width img width imageCanvas height img height const imageCanvasContext imageCanvas getContext 2d imageCanvasContext drawImage img 0 0 img width img height const imageData imageCanvasContext getImageData 0 0 img width img height let textureData null const rowPitch Math ceil img width 4 256 256 if rowPitch img width 4 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 const sampler device createSampler minFilter linear magFilter linear const texture device createTexture size img width img height 1 format rgba8unorm usage 0x4 0x2 GPUTextureUsage COPY DST GPUTextureUsage TEXTURE BINDING if typeof device queue writeTexture function console log device queue writeTexture supported device queue writeTexture texture textureData bytesPerRow img width 4 img width img height 1 else 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 1 device queue submit textureLoadEncoder finish Create vertex buffers and load data const cubeVertexSize 4 10 Byte size of one cube vertex const cubePositionOffset 0 const cubeColorOffset 4 4 Byte offset of cube vertex color attribute const cubeUVOffset 4 8 const cubeVertexCount 36 prettier ignore const cubeVertexArray new Float32Array float4 position float4 color float2 uv 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 0 0 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 0 0 1 0 0 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 0 1 0 1 1 0 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 0 1 0 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 1 1 0 0 0 1 1 0 1 1 1 1 0 0 1 1 1 1 1 1 1 1 0 1 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 0 0 1 1 0 0 1 1 1 1 0 0 1 1 0 0 1 1 1 1 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 0 0 1 0 1 1 1 1 1 0 1 0 1 0 0 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 0 1 1 1 1 1 1 1 0 1 0 1 0 0 const numVertices cubeVertexArray length 10 const positionUVBuffer device createBuffer size cubeVertexArray byteLength usage GPUBufferUsage VERTEX GPUBufferUsage COPY DST device queue writeBuffer positionUVBuffer 0 cubeVertexArray Uniform data 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 3 0 0 0 0 1 0 out eye center up mat4 multiply viewProjectionMatrix projectionMatrix viewMatrix Create uniform buffers and binding layout const vertexUniformBuffer device createBuffer size 64 usage GPUBufferUsage UNIFORM GPUBufferUsage COPY DST device queue writeBuffer vertexUniformBuffer 0 viewProjectionMatrix 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 const sceneUniformBindGroup device createBindGroup layout sceneUniformBindGroupLayout entries binding 0 resource buffer vertexUniformBuffer binding 1 resource sampler binding 2 resource texture createView Create render pipeline let basicVertWGSL block struct Uniforms 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 fragPosition vec4 f32 stage vertex fn main location 0 position vec4 f32 location 1 uv vec2 f32 VertexOutput var output VertexOutput output Position uniforms modelViewProjectionMatrix position output fragUV uv output fragPosition 0 5 position vec4 f32 1 0 1 0 1 0 1 0 return output let basicPixelWGSL group 0 binding 1 var mySampler sampler group 0 binding 2 var myTexture texture 2d f32 stage fragment fn main location 0 fragUV vec2 f32 location 1 fragPosition vec4 f32 location 0 vec4 f32 basic textured return textureSample myTexture mySampler fragUV if you want a retro color mixing in position return textureSample myTexture mySampler fragUV fragPosition if you want a constant color for the shape 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 cubeVertexSize attributes position shaderLocation 0 offset cubePositionOffset format float32x4 uv shaderLocation 1 offset cubeUVOffset format float32x2 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 context getCurrentTexture createView loadValue 0 1 1 1 clear screen to green storeOp store depthStencilAttachment view depthTexture createView depthLoadValue 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 mat4 multiply xform viewMatrix xform mat4 multiply xform projectionMatrix xform rotation 0 0 01 rotation 2 0 005 xformMatrix modelMatrix null rotation null device queue writeBuffer vertexUniformBuffer 0 modelMatrix Swap framebuffer renderPassDescription colorAttachments 0 view context getCurrentTexture createView const commandEncoder device createCommandEncoder const renderPass commandEncoder beginRenderPass renderPassDescription renderPass setPipeline pipeline First argument here refers to array index in pipeline vertexState vertexBuffers renderPass setVertexBuffer 0 positionUVBuffer First argument here refers to array index in pipeline layout bindGroupLayouts renderPass setBindGroup 0 sceneUniformBindGroup renderPass draw numVertices 1 0 0 renderPass endPass device queue submit commandEncoder finish requestAnimationFrame draw draw

const vertexUniformBuffer device createBuffer size 64 usage GPUBufferUsage UNIFORM GPUBufferUsage COPY DST device queue writeBuffer vertexUniformBuffer 0 viewProjectionMatrix 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 const sceneUniformBindGroup device createBindGroup layout sceneUniformBindGroupLayout entries binding 0 resource buffer vertexUniformBuffer binding 1 resource sampler binding 2 resource texture createView Create render pipeline let basicVertWGSL block struct Uniforms 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 fragPosition vec4 f32 stage vertex fn main location 0 position vec4 f32 location 1 uv vec2 f32 VertexOutput var output VertexOutput output Position uniforms modelViewProjectionMatrix position output fragUV uv output fragPosition 0 5 position vec4 f32 1 0 1 0 1 0 1 0 return output let basicPixelWGSL group 0 binding 1 var mySampler sampler group 0 binding 2 var myTexture texture 2d f32 stage fragment fn main location 0 fragUV vec2 f32 location 1 fragPosition vec4 f32 location 0 vec4 f32 basic textured return textureSample myTexture mySampler fragUV if you want a retro color mixing in position return textureSample myTexture mySampler fragUV fragPosition if you want a constant color for the shape 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 cubeVertexSize attributes position shaderLocation 0 offset cubePositionOffset format float32x4 uv shaderLocation 1 offset cubeUVOffset format float32x2 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 context getCurrentTexture createView loadValue 0 1 1 1 clear screen to green storeOp store depthStencilAttachment view depthTexture createView depthLoadValue 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 mat4 multiply xform viewMatrix xform mat4 multiply xform projectionMatrix xform rotation 0 0 01 rotation 2 0 005 xformMatrix modelMatrix null rotation null device queue writeBuffer vertexUniformBuffer 0 modelMatrix Swap framebuffer renderPassDescription colorAttachments 0 view context getCurrentTexture createView const commandEncoder device createCommandEncoder const renderPass commandEncoder beginRenderPass renderPassDescription renderPass setPipeline pipeline First argument here refers to array index in pipeline vertexState vertexBuffers renderPass setVertexBuffer 0 positionUVBuffer First argument here refers to array index in pipeline layout bindGroupLayouts renderPass setBindGroup 0 sceneUniformBindGroup renderPass draw numVertices 1 0 0 renderPass endPass device queue submit commandEncoder finish requestAnimationFrame draw draw

3dplot
a4print
about
acejs
acejs2
acejs3
aessecurity
angularjs
animbackgroundimage
aseformat
assert
asteroidsjs
backgrounds01
backgrounds02
backgrounds03
barnsleyfern
base26
base64
bib
binary
bodypix
bouncy
box2dweb
breakoutjs
browserversion
buslanes
busybutton
bvhreader
calendar
canvas
canvas2
canvas3
canvasmandelbrot
canvasmandelbrot2
canvasnumbers
canvaszoom
capsule
changingimages
chatgptusingopenai
chatgptusingopenai2
chatgptusingopenai3
checkboxtoggle
chinesetiles
classes
classfeatures
clipboardbutton
clonenode
codedropdown
codemirror
codemirror2
collada
colorpick
columnresizer
contextmenu
convnet
cookiebanner
countdown
countdown2
countdown3
crop
css3dbarchart
css3dbarchart2
css3dbook
css3dscene
csscube
csscube2
csscube3
csscubevideos
cssfilelist
csshas
csspulse
cssresizeaspect
cssspin
csszooming
csvtoarray
customcheckbox
d3datamap
d3js
d3js10
d3js11
d3js2
d3js3
d3js4
d3js5
d3js6
d3js7
d3js8
d3js9
d3jsanimatedgrid
d3jsarctransition
d3jsarctransition2
d3jsaxis
d3jsaxischanging
d3jsbars
d3jsbrushing
d3jsbuslanes
d3jsbuslanes2
d3jscalendar
d3jscheat
d3jsclock
d3jscloudmap
d3jscogs
d3jscolors
d3jscovid
d3jscovid2
d3jscovid3
d3jsdashboard
d3jsdashboard2
d3jsdashboard3
d3jsdatakeyfunction
d3jsdensity
d3jsdragresizing
d3jsdragresizing2
d3jseach
d3jsease
d3jsevents
d3jsflower
d3jsforcegroups
d3jsforces
d3jsforces2
d3jsfractaltree
d3jsgeo
d3jsgroupbars
d3jsgroups
d3jsheatmap
d3jshex
d3jshierarchies
d3jshierarchies2
d3jshistogram
d3jshistogram2
d3jshistogram3
d3jshistogram4
d3jsinterpolate
d3jsjoin
d3jskmean
d3jskmean2
d3jsline
d3jsline2
d3jsline3
d3jsline4
d3jslinetransition
d3jslinetransition0
d3jslinetransition2
d3jsmaplocations
d3jsmaps
d3jsmaps2
d3jsmaps3
d3jsmisc
d3jsmisc2
d3jsmodule
d3jsmodulecolor
d3jsmultistyles
d3jsnobel
d3jsoverlappinggraphs
d3jspanel
d3jspie
d3jspieinterpolate
d3jssankey
d3jssankey2
d3jsscatter
d3jsshapes
d3jsslider
d3jsspending
d3jsspending2
d3jsspiralplot
d3jsspirograph
d3jssquare
d3jsstack
d3jsstackedbar
d3jsstackedbar2
d3jssunburst
d3jssunmoon
d3jssvglines
d3jssymbols
d3jstimelines
d3jsuk
d3jsvoronoi
d3scatterplot
d3timeline
d3timeline2
datalist
datamuse
date
dblclickhighlight
deviceorientation
dictionaryapi
dockermenu
doodlepad
downloadgif
dragdroplistitems
dragrotateresizediv
dragrotateresizediv2
dragrotateresizediv3
dragrotateresizediv4
dragrotateresizefontsize
dragselectbrush
drawlinesdiv
dropdown
dualquaternionimages
dynamicgrid
easefunctions
easeinterpolate3dplots
echart
echart2
echart3
encapsulation
epubviewer
errorstack
excalidraw
excalidraw2
excalidraw3
excalidraw5
expandable
faker
fetchplus
fileupload
fixedtopbar
fonts
fonts2
footerbar
fractalmaze
fractalmaze2
fractalnoiseimage
fractals
fractals2
fractaltree
freesvg
fresnel
froggerjs
gantt
gifgiphyapi
gifhex
gltffromscratch
gradients
griditems
griditems2
griditems3
griditems4
gridworms
heat
hexview
hexview2
highlight
icons
icons2
iframes
ik
imagetracertosvg
imgur
inputfile
invadersjs
ipynb
ipynb2
ipynb3
ipynb4
isbn13
isbn2
jpghex
jquery
jquery2
jqueryui
jqueryui2
jsdraganddrop
jslint
jsobfuscate
jsraytracer
jstree
jstree2
jszip
jszipimages
jszipread
keyframes
l2dwidget
lda
leftmenu
less
less2
lineargradientimage
linenumbers
loadimagefromfile
makepdf
maps
markdown
markdown2
markdownalerts
markdownalerts2
markdownbookmarks
markovimage
markovpixelblocks
mathjax
matrices
matsandvects
mazegamejs
md2tex
metrotiles
metrowindows
milestones
misc
misc2
modules
myipdetails
neataptic
networkstructures
networkstructures2
number
obj
objtojson
openaiimages
opencv
opencv2
opencv3
opencv4
opencv5
outline
p2
p5fractalleaf
p5fractalshape
p5js
p5js2
p5js3
p5jsanimatedcover
p5mengercube
p5snowflakes
palindrome
panel
parallax
paste
paste2
pasteimgfromurl
pdfjs
pdfjs2
pdfkit
pdfkit2
pdfkit3
pdfkit4
pdfkit5
pdfkit6
pdfmake
pdfmake2
pdfmake3
pdfmake4
pdfmake5
pdfmake6
perlin
perlin2
perlin3
perspective
pexels
playground
plotly
plotlynoise
plotlyranddist
plyloader
plyloader2
pngtxtencoder
pongjs
pptxgenjs
prettycode
prism
prn
problems
progress
pseudorandom
px2svg
python
quotes
racergame
random
randomprofilepatterns
randomstring
rating
rayambient
raymonte
raymonteprogressive
raymonteprogressive2
raymontewarmstart
reexpcross
reexpcross2
regex
regexbib
regexpfixbib
regexpmultiline
repeatwordsregexp
resizabletable
resizabletable2
revealjs
revealjs2
revealjsmulti
ritalanguage
ritalanguage2
ritalanguage3
rotateimg
rough
rsapublicprivatekeys
rss
rss2
sankey
scrappingsvg
scrolltext
scrolltext2
scrollwidth
sdfboxinboxtwist
sdfhollowbox
setintervalexception
shareurl
shuffle
sidecomment
similarity
simplehighlighter
simpleplatformgamejs
sinecanvas
sliderpopout
slides
smileys
snowfall
snowman
sound
soundsignal
sphererayintersection
springs
sqljs
steganography
stereogram
stringmatching
svg
svgchaos
svgdragresize
svgdragresize2
svgdragresize3
svgdragrotate
svgdrawing
svglines
svglines2
svglines3
svglines4
svglines5
svglinesmandelbrot
svgpathsdragrotate
svgpathsdragrotateresize
svgpie
svgpie2
svgpie3
svgpiepath
svgpiepath2
svgrandomfaces
symbols
synaptic
synaptic2
synonyms
tablerotatecells
tablerotatecells2
tablerotatecells3
tablerotatecells3b
tablerotatecells4
tables
tablezebra
tabularjs
tabularjs2
tabulatordownload
tagcanvas
tensorflowgan
tensorflowjs
tensorflowjsbasic
tensorflowjscnn
tensorflowjssinewave
tensorflowjssound
tensorflowmobilenet
tetrahedronfractal
tetrahedronfractalfolding
tetris
textarea
textareaauto
textareadiv
textareadiv2
textmaskimage
theirorthere
thesaurus
threejs
threejs2
threejs3
threejs4
threejsgltf
threejstokyo
tiles
toaster
tooltip
transition
transitionexpandabledropdown
treeview
treeview2
tricks
tshirt
tshirt2
tshirt3
turningpages
unsplash
urlblob
urlblob2
userdefinepoints
vector
videos
videos2
visualsort
vue
w2ui
w2uientertextdialog
webcam
webgl
webgl2
webgl3
webgl4
webgl5
webglbasic1
webglbasic2
webglcube
webglfov
webglfrustum
webgljson
webglleaves
webgllighting
webglorthographic
webglpoints1
webglpoints2
webglpoints3
webglsquare
webgltexture1
webgltexture2
webgltexture3
webgltransforms
webgltriangle
webgpu
webgpu10
webgpu11
webgpu12
webgpu13
webgpu14
webgpu15
webgpu16
webgpu17
webgpu2
webgpu3
webgpu4
webgpu5
webgpu6
webgpu7
webgpu8
webgpu9
webgpubars
webgpubuffers
webgpubuffers2
webgpucellnoise
webgpuclouds
webgpuclydescope
webgpucompute
webgpucubemap
webgpucubemap2
webgpudeferred
webgpudepth
webgpudof
webgpudrops
webgpuetha
webgpufire
webgpufractalcubes
webgpuglassrain
webgpugltf
webgpugltf2
webgpugrass
webgpugrid
webgpukernel
webgpukleinian
webgpulabupdates
webgpulighting
webgpumandelbrot
webgpumeta3d
webgpumetaballs
webgpumouse
webgpunoise
webgpunormalmapping
webgpuobj
webgpuparallax
webgpuparallax2
webgpuparallax3
webgpuparallaxshadow
webgpuparallaxshadow2
webgpupixel
webgpuquad
webgpuray1
webgpuraytracing
webgpuraytracing2
webgpushadowmaps
webgpushadowmaps2
webgpusierpinski2d
webgpusierpinski3d
webgpusinusoid
webgpussao
webgpustadiumobj
webgpuswirl
webgputestpipe3
webgputoon
webgputopology
webgputt
webgpuvolcloud
webgpuwater
webgpuwireframe
webgpuwireframe2
webpcanvas
webworkers
webxr
webxr2
wiggly
wikipedia