Last active
August 6, 2018 12:11
-
-
Save diska/95f850c6e010cf86abb90b5c9a330b1f to your computer and use it in GitHub Desktop.
Framebufferにデータ書いてWebGLProgram使わずに描画も一応できるよ。というコード。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <canvas id="CNVS" width="256" height="256"></canvas> | |
| <script> | |
| var cx=CNVS.getContext("webgl");cx.enable(0x0c11); | |
| var data=new Uint8Array(256*256*4),fbuf=new Uint8Array(256*256*4); | |
| for(j=0;j<256;j++)for(i=0;i<256;i++){ | |
| data[(j*256+i)*4+0]=i; | |
| data[(j*256+i)*4+1]=j; | |
| data[(j*256+i)*4+2]=(i*i+j*j<65536)?0xff:0; | |
| data[(j*256+i)*4+3]=0xff; | |
| } | |
| var tx=cx.createTexture(); cx.bindTexture(cx.TEXTURE_2D, tx); | |
| cx.texImage2D(cx.TEXTURE_2D,0,cx.RGBA,256,256,0,cx.RGBA,cx.UNSIGNED_BYTE,data); | |
| var fb=cx.createFramebuffer();cx.bindFramebuffer(cx.FRAMEBUFFER,fb); | |
| cx.framebufferTexture2D(cx.FRAMEBUFFER, cx.COLOR_ATTACHMENT0,cx.TEXTURE_2D,tx,0); | |
| cx.readPixels(0,0, 256,256,cx.RGBA,cx.UNSIGNED_BYTE,fbuf); | |
| cx.bindFramebuffer(cx.FRAMEBUFFER,null); | |
| var r,g,b; | |
| for(j=0;j<256;j++)for(i=0;i<256;i++){ | |
| cx.scissor(i,j,1,1); | |
| r=fbuf[(j*256+i)*4+0]/256; | |
| g=fbuf[(j*256+i)*4+1]/256; | |
| b=fbuf[(j*256+i)*4+2]/256; | |
| a=fbuf[(j*256+i)*4+3]/256; | |
| cx.clearColor(r,g,b,a);cx.clear(0x4000); | |
| } | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment