Notebook - Welcome to Notebook

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












WebGPU Example Mixing compute shader and renderer flocking example let canvasRef current null var updateWGSL struct Particle pos vec2 f32 vel vec2 f32 block struct SimParams deltaT f32 rule1Distance f32 rule2Distance f32 rule3Distance f32 rule1Scale f32 rule2Scale f32 rule3Scale f32 block struct Particles particles stride 16 array Particle binding 0 group 0 var uniform params SimParams binding 1 group 0 var storage read particlesA Particles binding 2 group 0 var storage read write particlesB Particles stage compute workgroup size 64 fn main builtin global invocation id GlobalInvocationID vec3 u32 var index u32 GlobalInvocationID x var vPos particlesA particles index pos var vVel particlesA particles index vel var cMass vec2 f32 0 0 0 0 var cVel vec2 f32 0 0 0 0 var colVel vec2 f32 0 0 0 0 var cMassCount u32 0u var cVelCount u32 0u var pos vec2 f32 var vel vec2 f32 for var i u32 0u i arrayLength particlesA particles i i 1u if i index continue pos particlesA particles i pos xy vel particlesA particles i vel xy if distance pos vPos params rule1Distance cMass cMass pos cMassCount cMassCount 1u if distance pos vPos params rule2Distance colVel colVel pos vPos if distance pos vPos params rule3Distance cVel cVel vel cVelCount cVelCount 1u if cMassCount 0u var temp f32 cMassCount cMass cMass vec2 f32 temp temp vPos if cVelCount 0u var temp f32 cVelCount cVel cVel vec2 f32 temp temp vVel vVel cMass params rule1Scale colVel params rule2Scale cVel params rule3Scale clamp velocity for a more pleasing simulation vVel normalize vVel clamp length vVel 0 0 0 1 kinematic update vPos vPos vVel params deltaT Wrap around boundary if vPos x 1 0 vPos x 1 0 if vPos x 1 0 vPos x 1 0 if vPos y 1 0 vPos y 1 0 if vPos y 1 0 vPos y 1 0 Write back particlesB particles index pos vPos particlesB particles index vel vVel var spriteWGSL stage vertex fn vert main location 0 a particlePos vec2 f32 location 1 a particleVel vec2 f32 location 2 a pos vec2 f32 builtin position vec4 f32 let angle atan2 a particleVel x a particleVel y let pos vec2 f32 a pos x cos angle a pos y sin angle a pos x sin angle a pos y cos angle return vec4 f32 pos a particlePos 0 0 1 0 stage fragment fn frag main location 0 vec4 f32 return vec4 f32 1 0 1 0 1 0 1 0 canvasRef current document createElement canvas document body appendChild canvasRef current canvasRef current width 400 canvasRef current height 400 console log canvasRef current width console log canvasRef current height const adapter await navigator gpu requestAdapter const device await adapter requestDevice const context canvasRef current getContext webgpu const devicePixelRatio window devicePixelRatio 1 const presentationSize canvasRef current clientWidth devicePixelRatio canvasRef current clientHeight devicePixelRatio const presentationFormat context getPreferredFormat adapter context configure device format presentationFormat size presentationSize const spriteShaderModule device createShaderModule code spriteWGSL const renderPipeline device createRenderPipeline vertex module spriteShaderModule entryPoint vert main buffers instanced particles buffer arrayStride 4 4 stepMode instance attributes instance position shaderLocation 0 offset 0 format float32x2 instance velocity shaderLocation 1 offset 2 4 format float32x2 vertex buffer arrayStride 2 4 stepMode vertex attributes vertex positions shaderLocation 2 offset 0 format float32x2 fragment module spriteShaderModule entryPoint frag main targets format presentationFormat primitive topology triangle list const computePipeline device createComputePipeline compute module device createShaderModule code updateWGSL entryPoint main const renderPassDescriptor GPURenderPassDescriptor colorAttachments view undefined Assigned later loadValue r 0 0 g 0 0 b 0 0 a 1 0 storeOp store prettier ignore const vertexBufferData new Float32Array 0 01 0 02 0 01 0 02 0 0 0 02 const spriteVertexBuffer device createBuffer size vertexBufferData byteLength usage GPUBufferUsage VERTEX mappedAtCreation true new Float32Array spriteVertexBuffer getMappedRange set vertexBufferData spriteVertexBuffer unmap const simParams deltaT 0 04 rule1Distance 0 1 rule2Distance 0 025 rule3Distance 0 025 rule1Scale 0 02 rule2Scale 0 05 rule3Scale 0 005 const simParamBufferSize 7 Float32Array BYTES PER ELEMENT const simParamBuffer device createBuffer size simParamBufferSize usage GPUBufferUsage UNIFORM GPUBufferUsage COPY DST function updateSimParams device queue writeBuffer simParamBuffer 0 new Float32Array simParams deltaT simParams rule1Distance simParams rule2Distance simParams rule3Distance simParams rule1Scale simParams rule2Scale simParams rule3Scale updateSimParams const numParticles 1500 const initialParticleData new Float32Array numParticles 4 for let i 0 i numParticles i initialParticleData 4 i 0 2 Math random 0 5 initialParticleData 4 i 1 2 Math random 0 5 initialParticleData 4 i 2 2 Math random 0 5 0 1 initialParticleData 4 i 3 2 Math random 0 5 0 1 const particleBuffers new Array 2 GPUBuffer const particleBindGroups new Array 2 GPUBindGroup for let i 0 i 2 i particleBuffers i device createBuffer size initialParticleData byteLength usage GPUBufferUsage VERTEX GPUBufferUsage STORAGE mappedAtCreation true new Float32Array particleBuffers i getMappedRange set initialParticleData particleBuffers i unmap for let i 0 i 2 i particleBindGroups i device createBindGroup layout computePipeline getBindGroupLayout 0 entries binding 0 resource buffer simParamBuffer binding 1 resource buffer particleBuffers i offset 0 size initialParticleData byteLength binding 2 resource buffer particleBuffers i 1 2 offset 0 size initialParticleData byteLength let t 0 function frame Sample is no longer the active page if canvasRef current return renderPassDescriptor colorAttachments 0 view context getCurrentTexture createView const commandEncoder device createCommandEncoder const passEncoder commandEncoder beginComputePass passEncoder setPipeline computePipeline passEncoder setBindGroup 0 particleBindGroups t 2 passEncoder dispatch Math ceil numParticles 64 passEncoder endPass const passEncoder commandEncoder beginRenderPass renderPassDescriptor passEncoder setPipeline renderPipeline passEncoder setVertexBuffer 0 particleBuffers t 1 2 passEncoder setVertexBuffer 1 spriteVertexBuffer passEncoder draw 3 numParticles 0 0 passEncoder endPass device queue submit commandEncoder finish t requestAnimationFrame frame frame setInterval frame 200 console log ready

attributes vertex positions shaderLocation 2 offset 0 format float32x2 fragment module spriteShaderModule entryPoint frag main targets format presentationFormat primitive topology triangle list const computePipeline device createComputePipeline compute module device createShaderModule code updateWGSL entryPoint main const renderPassDescriptor GPURenderPassDescriptor colorAttachments view undefined Assigned later loadValue r 0 0 g 0 0 b 0 0 a 1 0 storeOp store prettier ignore const vertexBufferData new Float32Array 0 01 0 02 0 01 0 02 0 0 0 02 const spriteVertexBuffer device createBuffer size vertexBufferData byteLength usage GPUBufferUsage VERTEX mappedAtCreation true new Float32Array spriteVertexBuffer getMappedRange set vertexBufferData spriteVertexBuffer unmap const simParams deltaT 0 04 rule1Distance 0 1 rule2Distance 0 025 rule3Distance 0 025 rule1Scale 0 02 rule2Scale 0 05 rule3Scale 0 005 const simParamBufferSize 7 Float32Array BYTES PER ELEMENT const simParamBuffer device createBuffer size simParamBufferSize usage GPUBufferUsage UNIFORM GPUBufferUsage COPY DST function updateSimParams device queue writeBuffer simParamBuffer 0 new Float32Array simParams deltaT simParams rule1Distance simParams rule2Distance simParams rule3Distance simParams rule1Scale simParams rule2Scale simParams rule3Scale updateSimParams const numParticles 1500 const initialParticleData new Float32Array numParticles 4 for let i 0 i numParticles i initialParticleData 4 i 0 2 Math random 0 5 initialParticleData 4 i 1 2 Math random 0 5 initialParticleData 4 i 2 2 Math random 0 5 0 1 initialParticleData 4 i 3 2 Math random 0 5 0 1 const particleBuffers new Array 2 GPUBuffer const particleBindGroups new Array 2 GPUBindGroup for let i 0 i 2 i particleBuffers i device createBuffer size initialParticleData byteLength usage GPUBufferUsage VERTEX GPUBufferUsage STORAGE mappedAtCreation true new Float32Array particleBuffers i getMappedRange set initialParticleData particleBuffers i unmap for let i 0 i 2 i particleBindGroups i device createBindGroup layout computePipeline getBindGroupLayout 0 entries binding 0 resource buffer simParamBuffer binding 1 resource buffer particleBuffers i offset 0 size initialParticleData byteLength binding 2 resource buffer particleBuffers i 1 2 offset 0 size initialParticleData byteLength let t 0 function frame Sample is no longer the active page if canvasRef current return renderPassDescriptor colorAttachments 0 view context getCurrentTexture createView const commandEncoder device createCommandEncoder const passEncoder commandEncoder beginComputePass passEncoder setPipeline computePipeline passEncoder setBindGroup 0 particleBindGroups t 2 passEncoder dispatch Math ceil numParticles 64 passEncoder endPass const passEncoder commandEncoder beginRenderPass renderPassDescriptor passEncoder setPipeline renderPipeline passEncoder setVertexBuffer 0 particleBuffers t 1 2 passEncoder setVertexBuffer 1 spriteVertexBuffer passEncoder draw 3 numParticles 0 0 passEncoder endPass device queue submit commandEncoder finish t requestAnimationFrame frame frame setInterval frame 200 console log ready

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
candycrush
candycrush2
canvas
canvas2
canvas3
canvasmandelbrot
canvasmandelbrot2
canvasnumbers
canvaszoom
capsule
changingimages
chaosgame
chaosrandom
chaosrandomhisto
chaosrandomhisto2
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
curleffect
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
fluiddynamics
fluiddynamics2
fluiddynamics3
fluidsmokedynamics
fluidsmokedynamics2
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
jsfire
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
minkowski2dboxes
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
randomcalcpie
randomgenerator
randomprofilepatterns
randomsinhistogram
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