Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <canvas id="canvas" width="512" height="512"></canvas>
- <script type="text/javascript">
- function init() {
- function frac(x, y, z) {
- function fraccore(x)
- {
- ox = x;
- var scale = 1;
- for(i=0; i<6; i++)
- {
- x*=3;
- scale*=3;
- flx = Math.floor(x);
- x -= flx;
- if(!( flx==0 || flx==2 ))
- {
- if(x<1.5)
- {
- ox -= 1/scale;
- }else
- {
- ox += 1/scale;
- }
- }
- }
- return ox;
- }
- x = fraccore(x)-0.5;
- y = fraccore(y)-0.5;
- z = fraccore(z)-0.5;
- return [x*4*x*x, y*4*y*y, z*4*z*z+1.5, (z*4*z*z+0.5)*255];
- }
- var canvas=document.getElementById("canvas"),
- context=canvas.getContext("2d"),
- imageData=context.getImageData(0, 0, 512, 512),
- buffer=imageData.data,
- zbuffer=[512*512],
- interval,
- steps=0,
- render=function () {
- var point, i, x, y, z, xp, yp, pos;
- for (i=0; i<50000; i++) {
- x=Math.random();
- y=Math.random();
- z=Math.random();
- point = frac(x, y, z);
- xp=
- Math.floor(
- ((point[0])*512)/point[2])+
- 256;
- yp=
- Math.floor(
- ((point[1])*512)/point[2])+
- 256;
- if (
- (xp>=0)&&
- (xp<512)&&
- (yp>=0)&&
- (yp<512)&&
- (point[2]<zbuffer[pos=yp*512+xp]))
- {
- zbuffer[pos]=point[2];
- buffer[pos*4+1]=point[3];
- buffer[pos*4+3]=255;
- }
- }
- context.putImageData(imageData, 0, 0);
- steps++;
- if (steps>1024) {
- window.clearInterval(interval);
- }
- };
- for (i=0; i<512*512; i++) {
- zbuffer[i]=10000000000;
- }
- interval=window.setInterval(render, 0);
- }
- init();
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement