Skip to content

Instantly share code, notes, and snippets.

@diska
Created March 10, 2021 02:16
Show Gist options
  • Save diska/3d968715ed239dfb51d78e2667d4f128 to your computer and use it in GitHub Desktop.
Save diska/3d968715ed239dfb51d78e2667d4f128 to your computer and use it in GitHub Desktop.
WebGL自由研究「scissorで3D」。raw WebGL 3d sample without WebGLProgram.
<canvas width="512" height="512"></canvas>
<script>"use strict";
let opt={preserveDrawingBuffer:true}
let cx=document.querySelector("canvas").getContext("webgl",opt);
function draw(now){
let x,y,z, sec=now/1000;
cx.disable(cx.SCISSOR_TEST);
cx.clearColor(0,0,.6,1); cx.clear(0x4000);
cx.enable(cx.SCISSOR_TEST);
for(let i=0;i<100000; i++){
x=Math.random()*2-1;
y=Math.random()*2-1;
z=Math.random()*2-1;
if(Math.abs(x)<0.5 && Math.abs(y)<0.2 && Math.abs(z)<0.5){
[x,z]=rot(x,z,sec);
[y,z]=rot(y,z,sec/2);
[x,y,z]=prs(x, y, z, .5);
cx.scissor(x*256+256,y*256+256,2,2);
cx.clearColor(1,1,0,1); cx.clear(0x4000);
}
}
requestAnimationFrame(draw);
}; requestAnimationFrame(draw);
function rot(x,y,th){let c=Math.cos(th),s=Math.sin(th);return [x*c-y*s,x*s+y*c]}
function prs(x,y,z,f){let w=1+(z*f);return [x/w, y/w, z/w]}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment