Notebook - Welcome to Notebook

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












Fun with ray tracing Shell based local ambient occlusion approximation Instead of a single shell hemisphere use multiple hemispheres to provide a better approximation as the hemispheres grow in radius their contribution is scaled function validf val if typeof val undefined throw Error invalid value if isNaN val throw Error NaN value if val 9999999 throw Error number warning range if val 9999999 throw Error number warning range function validv val if typeof val undefined throw Error invalid value validf val x validf val y validf val z function vec3 x y z if typeof x undefined x 0 if typeof y undefined y 0 if typeof z undefined z 0 if typeof x number throw Error invalid value not a number if typeof y number throw Error invalid value not a number if typeof z number throw Error invalid value not a number this x x this y y this z z validv this return this vec3 add function v0 v1 return new vec3 v0 x v1 x v0 y v1 y v0 z v1 z vec3 add3 function v0 v1 v2 validv v0 validv v1 validv v2 return vec3 add v0 vec3 add v1 v2 vec3 sub function v0 v1 return new vec3 v0 x v1 x v0 y v1 y v0 z v1 z vec3 scale function v0 s return new vec3 v0 x s v0 y s v0 z s vec3 mult function v0 v1 return new vec3 v0 x v1 x v0 y v1 y v0 z v1 z vec3 cross function v0 v1 return v0 x v1 y v0 y v1 x vec3 cross function v0 v1 return new vec3 v0 y v1 z v0 z v1 y v0 z v1 x v0 x v1 z v0 x v1 y v0 y v1 x vec3 dot function v0 v1 return v0 x v1 x v0 y v1 y v0 z v1 z note you can t use length word vec3 dist function v0 return Math sqrt v0 x v0 x v0 y v0 y v0 z v0 z vec3 norm function v0 let ln Math sqrt v0 x v0 x v0 y v0 y v0 z v0 z validf ln return vec3 scale v0 1 0 ln function Ray o d this o o this d d validv this o validv this d const REFL DIFF 0 SPEC 1 REFR 2 material types function Sphere r p e c refl this r r radius this p p position this e e emission this c c color this refl refl reflection type DIFFuse SPECular REFRactive this intersect function ray returns distance 0 if nohit let op vec3 sub this p ray o Solve t 2 d d 2 t o p d o p o p R 2 0 let eps 1e 4 let b vec3 dot op ray d let det b b vec3 dot op op this r this r if det 0 return 0 else det Math sqrt det let t b det return t eps t t b det eps t 0 const spheres Scene radius position emission color material new Sphere 1e5 new vec3 1e5 1 40 8 81 6 new vec3 new vec3 1 0 25 25 REFL DIFF Left new Sphere 1e5 new vec3 1e5 99 40 8 81 6 new vec3 new vec3 25 1 0 75 REFL DIFF Rght new Sphere 1e5 new vec3 50 40 8 1e5 new vec3 new vec3 25 25 1 0 REFL DIFF Back new Sphere 1e5 new vec3 50 40 8 1e5 170 new vec3 new vec3 REFL DIFF Frnt new Sphere 1e5 new vec3 50 1e5 81 6 new vec3 new vec3 1 0 1 0 25 REFL DIFF Botm new Sphere 1e5 new vec3 50 1e5 81 6 81 6 new vec3 new vec3 0 25 1 0 1 0 REFL DIFF Top new Sphere 16 5 new vec3 27 16 5 47 new vec3 new vec3 0 99 0 0 99 REFL SPEC Mirr new Sphere 16 5 new vec3 73 16 5 78 new vec3 new vec3 0 99 0 99 0 99 REFL REFR Glas new Sphere 600 new vec3 50 681 6 27 81 6 new vec3 12 12 12 new vec3 REFL DIFF Lite function clamp x return x 0 0 x 1 1 x function toInt x return Math round Math pow clamp x 1 2 2 255 5 function intersect o o ray t id const n spheres length let inf 1e20 o t inf for let i n i let d spheres i intersect o ray if d d o t o t d o id i return o t inf function erand48 return Math random const M PI Math PI const cos Math cos const sin Math sin const sqrt Math sqrt const fabs Math abs function trace ray depth Xi const Ray r int depth unsigned short Xi let t distance to intersection let id 0 id of intersected object let o ray ray t t id id if intersect o return new vec3 if miss return black id o id t o t const obj spheres id the hit object const x vec3 add ray o vec3 scale ray d t validv x const n vec3 norm vec3 sub x obj p validv n let nl vec3 dot n ray d 0 vec3 scale n 1 0 vec3 scale n 1 validv nl if depth 3 return obj e let f new vec3 obj c x obj c y obj c z validv f let p f x f y f x f z f x f y f z f y f z max refl shell approximation method for calculationg local ambient occlusion if obj refl REFL DIFF hemispheres 10 100 1000 let light 0 0 let hs 10 100 1000 let hf 0 1 0 5 1 0 for let h 0 h hs length h local ambient occlusion let nr 5 let sum 0 0 let delt hs h let dr 0 5 for let k 0 k nr k randomize nl hemisphere let nh vec3 add nl new vec3 Math random 2 1 dr Math random 2 1 dr Math random 2 1 dr nh vec3 norm nh let ray new Ray x nh let o ray ray t 0 id 0 if intersect o o t 1 0 if o t delt o t delt if o t 0 0 o t 0 0 sum o t sum nr sum 1 0 delt hf h light sum end hr return vec3 scale obj c light Full Monte carlo calculation global illumiation if obj refl REFL DIFF Ideal DIFFUSE reflection let r1 2 M PI erand48 Xi let r2 erand48 Xi let r2s sqrt r2 let w nl let refv fabs w x 1 new vec3 0 1 new vec3 1 let u vec3 cross refv w u vec3 norm u let v vec3 cross w u validv v let aa11 vec3 scale u cos r1 r2s validv aa11 let aa22 vec3 scale v sin r1 r2s validv aa22 let aa33 vec3 scale w sqrt 1 r2 validv aa33 let aabb vec3 add3 aa11 aa22 aa33 validv aabb let d vec3 norm aabb validv d let tt trace new Ray x d depth Xi validv tt let res0 vec3 add obj e vec3 mult f tt console log res0 res0 return res0 Reflection calculation shiny surfaces reflect what they re looking at else if obj refl REFL SPEC Ideal SPECULAR reflection let gg vec3 sub ray d vec3 scale n 2 vec3 dot n ray d let tt trace new Ray x gg depth Xi let res2 vec3 add obj e vec3 mult f tt console log res2 res2 validv res2 return res2 function main let w 256 256 1024 let h 256 256 768 let samps 4 0 samples let cam new Ray new vec3 50 52 295 6 vec3 norm new vec3 0 0 042612 1 cam pos dir let cx new vec3 w 5135 h let cy vec3 scale vec3 norm vec3 cross cx cam d 0 5135 let c Array from Array w h map return vec3 for let y 0 y h y Loop over image rows fprintf stderr rRendering d spp 5 2f samps 4 100 y h 1 for let x 0 x w x Loop cols let Xi new Array 3 0 0 y y y for let sy 0 sy 2 sy 2x2 subpixel rows let i h y 1 w x for let sx 0 sx 2 sx 2x2 subpixel cols let r new vec3 for let s 0 s samps s let r1 2 erand48 Xi dx r1 1 sqrt r1 1 1 sqrt 2 r1 let r2 2 erand48 Xi dy r2 1 sqrt r2 1 1 sqrt 2 r2 dx 0 5 dy 0 5 let d vec3 add3 vec3 scale cx sx 5 dx 2 x w 5 vec3 scale cy sy 5 dy 2 y h 5 cam d let cd2 vec3 add cam o vec3 scale d 140 let rr2 trace new Ray cd2 vec3 norm d 0 Xi c i vec3 add rr2 c i let t2 vec3 scale rr2 1 0 samps r vec3 add r t2 s Camera rays are pushed forward to start in interior c i vec3 add c i vec3 scale vec3 clamp r x clamp r y clamp r z 0 25 return return sx sy x y let canvas document createElement canvas document body appendChild canvas canvas width w canvas height h let context2d canvas getContext 2d let image data context2d getImageData 0 0 w h raw data image data data for let x 0 x w x for let y 0 y h y let i x y w image data data i 4 0 toInt c i x image data data i 4 1 toInt c i y image data data i 4 2 toInt c i z image data data i 4 3 255 context2d putImageData image data 0 0 console log finished render main console log ready

dv n let nl vec3 dot n ray d 0 vec3 scale n 1 0 vec3 scale n 1 validv nl if depth 3 return obj e let f new vec3 obj c x obj c y obj c z validv f let p f x f y f x f z f x f y f z f y f z max refl shell approximation method for calculationg local ambient occlusion if obj refl REFL DIFF hemispheres 10 100 1000 let light 0 0 let hs 10 100 1000 let hf 0 1 0 5 1 0 for let h 0 h hs length h local ambient occlusion let nr 5 let sum 0 0 let delt hs h let dr 0 5 for let k 0 k nr k randomize nl hemisphere let nh vec3 add nl new vec3 Math random 2 1 dr Math random 2 1 dr Math random 2 1 dr nh vec3 norm nh let ray new Ray x nh let o ray ray t 0 id 0 if intersect o o t 1 0 if o t delt o t delt if o t 0 0 o t 0 0 sum o t sum nr sum 1 0 delt hf h light sum end hr return vec3 scale obj c light Full Monte carlo calculation global illumiation if obj refl REFL DIFF Ideal DIFFUSE reflection let r1 2 M PI erand48 Xi let r2 erand48 Xi let r2s sqrt r2 let w nl let refv fabs w x 1 new vec3 0 1 new vec3 1 let u vec3 cross refv w u vec3 norm u let v vec3 cross w u validv v let aa11 vec3 scale u cos r1 r2s validv aa11 let aa22 vec3 scale v sin r1 r2s validv aa22 let aa33 vec3 scale w sqrt 1 r2 validv aa33 let aabb vec3 add3 aa11 aa22 aa33 validv aabb let d vec3 norm aabb validv d let tt trace new Ray x d depth Xi validv tt let res0 vec3 add obj e vec3 mult f tt console log res0 res0 return res0 Reflection calculation shiny surfaces reflect what they re looking at else if obj refl REFL SPEC Ideal SPECULAR reflection let gg vec3 sub ray d vec3 scale n 2 vec3 dot n ray d let tt trace new Ray x gg depth Xi let res2 vec3 add obj e vec3 mult f tt console log res2 res2 validv res2 return res2 function main let w 256 256 1024 let h 256 256 768 let samps 4 0 samples let cam new Ray new vec3 50 52 295 6 vec3 norm new vec3 0 0 042612 1 cam pos dir let cx new vec3 w 5135 h let cy vec3 scale vec3 norm vec3 cross cx cam d 0 5135 let c Array from Array w h map return vec3 for let y 0 y h y Loop over image rows fprintf stderr rRendering d spp 5 2f samps 4 100 y h 1 for let x 0 x w x Loop cols let Xi new Array 3 0 0 y y y for let sy 0 sy 2 sy 2x2 subpixel rows let i h y 1 w x for let sx 0 sx 2 sx 2x2 subpixel cols let r new vec3 for let s 0 s samps s let r1 2 erand48 Xi dx r1 1 sqrt r1 1 1 sqrt 2 r1 let r2 2 erand48 Xi dy r2 1 sqrt r2 1 1 sqrt 2 r2 dx 0 5 dy 0 5 let d vec3 add3 vec3 scale cx sx 5 dx 2 x w 5 vec3 scale cy sy 5 dy 2 y h 5 cam d let cd2 vec3 add cam o vec3 scale d 140 let rr2 trace new Ray cd2 vec3 norm d 0 Xi c i vec3 add rr2 c i let t2 vec3 scale rr2 1 0 samps r vec3 add r t2 s Camera rays are pushed forward to start in interior c i vec3 add c i vec3 scale vec3 clamp r x clamp r y clamp r z 0 25 return return sx sy x y let canvas document createElement canvas document body appendChild canvas canvas width w canvas height h let context2d canvas getContext 2d let image data context2d getImageData 0 0 w h raw data image data data for let x 0 x w x for let y 0 y h y let i x y w image data data i 4 0 toInt c i x image data data i 4 1 toInt c i y image data data i 4 2 toInt c i z image data data i 4 3 255 context2d putImageData image data 0 0 console log finished render main 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
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