Skip to content

Instantly share code, notes, and snippets.

View diska's full-sized avatar

diska

View GitHub Profile
@diska
diska / TextureCube2.html
Last active November 7, 2020 17:28
textureCubeを使って意図したように動いてるけど複雑すぎて既に自分では読めないWebGLコード。
<style>canvas{background-color:blue};</style>
<canvas width="256" height="256"></canvas><hr/>
<script>"use strict";
let cv0=document.createElement("canvas"); cv0.width=32;cv0.height=32;
let cc=cv0.getContext("2d"); cc.font="24px serif";
let cx=document.querySelector("canvas").getContext("webgl");
const vsrc=`attribute vec4 p;varying vec3 eye;
void main(){gl_Position=p;gl_Position.xy=2.*p.xy-1.;eye=vec3(2.*p.xy-1.,.8);}`;
const fsrc=`precision highp float;
uniform samplerCube tx6;uniform mat3 m3;varying vec3 eye;
@diska
diska / TextureCube.html
Created November 6, 2020 00:33
「textureCubeが初めて使えた」という以外の意味はないWebGLコード。
<canvas width="256" height="256"></canvas><hr/>
<script>"use strict";
let data=new Uint8Array(64*64*4);
for(let i=0; i<data.length; i++)data[i]=0xff*Math.random();
const vsrc=`attribute vec4 p;void main(){gl_PointSize=256.;gl_Position=p;}`;
const fsrc=`precision highp float;uniform samplerCube tx6;uniform float th;
void main(){
vec3 eye=vec3(2.*gl_PointCoord.xy-1.,1); eye.xy+=vec2(cos(th),sin(th));
gl_FragColor=textureCube(tx6,normalize(eye));
}`;
@diska
diska / twovids.html
Created October 28, 2020 18:03
video要素2つ読んでcanvasに並べるかなり最低限のWebGLサンプルコード。要mp4ファイル。
<style>canvas,textarea,video{background-color: bisque;}</style>
<canvas width="512" height="512"></canvas><hr/>
<textarea id="LOG" cols="30" rows="10"></textarea>
<video id="VD0" src="" loop autoplay muted hidden></video>
<video id="VD1" src="" loop autoplay muted hidden></video>
<script>"use strict";
let vsrc=`attribute vec4 p;void main(){gl_PointSize=512.;gl_Position=p;}`;
let fsrc=`precision highp float;uniform sampler2D tx;
void main(){gl_FragColor=vec4(texture2D(tx,gl_PointCoord).rgb,1);}`;
let cx=document.querySelector("canvas").getContext("webgl");
@diska
diska / WebGLFramebuffer.html
Created October 25, 2020 16:51
WebGLFramebufferを使うコードの書き方の練習。
<style>canvas,textarea{background-color:bisque;}</style>
<canvas width="256" height="256"></canvas>
<textarea id="LOG" cols="30" rows="10"></textarea>
<script>"use strict";
let vsrc0=`attribute vec4 p;void main(){gl_PointSize=250.;gl_Position=p;}`;
let fsrc0=`uniform sampler2D tx;void main(){gl_FragColor=texture2D(tx,gl_PointCoord*3.);}`;
let vsrc1=`attribute vec4 p;void main(){gl_PointSize=128.;gl_Position=p;}`;
let fsrc1=`void main(){gl_FragColor=(length(gl_PointCoord-.5)<.3)?vec4(1,0,0,1):vec4(1);}`;
let cx=document.querySelector("canvas").getContext("webgl");
let fb=getFb(256,256);
@diska
diska / webglshortsample.html
Created October 25, 2020 00:17
短くて整理されて濃いけど詰め込み過ぎてよくわからないWebGL入門コード。
<style>canvas,textarea{background-color: bisque;}</style>
<canvas width="128" height="128"></canvas><hr/>
<textarea id="LOG" cols="48" rows="16" hidden></textarea>
<script>"use strict";
let vsrc=`attribute vec4 p;void main(){gl_PointSize=64.;gl_Position=p;}`;
let fsrc=`void main(){
if(length(gl_PointCoord.xy-vec2(.5))>.5)discard;
gl_FragColor=vec4(gl_PointCoord.xy,0,1);}`;
let cx=document.querySelector("canvas").getContext("webgl");
try{ cx.useProgram(getPg(vsrc,fsrc));requestAnimationFrame(draw);
@diska
diska / cube.html
Last active October 22, 2020 16:43
WebGLで四角いのが何となく回るだけ。
<style>canvas,textarea{background-color: bisque;}</style>
<canvas width="256" height="256"></canvas><hr/>
<textarea name="" id="LOG" cols="40" rows="12"></textarea>
<script>"use strict";
let vsrc=`attribute vec4 p;
uniform mat3 m3;
varying vec3 vPos,vDep;
void main(){
vPos=p.xyz;
vec4 p2=vec4(p.xyz*m3,1);
@diska
diska / hellowebgl.html
Created October 2, 2020 05:38
WebGLでHELLOと表示するためなら手段を選ばないコード。
<canvas></canvas>
<script>
const hello=`
#...#.#####.#####.#####..###..
#...#.#.....#.....#.....#...#.
#####.####..#.....#.....#...#.
#...#.#.....#.....#.....#...#.
#...#.#####.#.....#......###..`;
let cx=document.querySelector("canvas").getContext("webgl");
cx.clearColor(0,0.3,0,1); cx.clear(0x4000);
@diska
diska / cylinder.html
Created August 2, 2020 03:30
変換行列を理解してない学習段階で書いた円筒のWebGLコード。
<style>canvas{background-color: cornflowerblue;}</style>
<canvas width="512" height="512"></canvas>
<script>
const opt={};
let cx=document.querySelector("canvas").getContext("webgl",opt);
let pg=cx.createProgram();
let vs=cx.createShader(cx.VERTEX_SHADER); cx.attachShader(pg,vs);
let fs=cx.createShader(cx.FRAGMENT_SHADER); cx.attachShader(pg,fs);
let vsrc=`attribute vec4 p;uniform mat4 m4;
varying float c;
@diska
diska / neko.html
Last active July 14, 2020 02:29
WebGLと深度処理の復習。参考: https://scratch.mit.edu/projects/407749635/
<style>canvas{background-color: black;}</style>
<canvas width="512" height="512"></canvas>
<script>"use strict";
const vsrc=`attribute vec4 p;
uniform mat4 umat; uniform sampler2D tex0;
varying vec4 vp;
void main(){
gl_PointSize=3.;
vp=p, vp.z=texture2D(tex0,p.xy*0.5+0.5).z;
gl_Position=umat*vp*0.7;gl_Position.w=1.;
@diska
diska / yashed.html
Created May 20, 2020 01:05
minimal Shader Editor with a uniform.
<style>textarea{background-color: bisque;}</style>
<canvas width="256" height="256"></canvas><hr/>
<textarea id="VS" cols="36" rows="8"></textarea>
<textarea id="FS" cols="36" rows="8"></textarea>
<textarea id="LG" cols="36" rows="8"></textarea>
<script>
VS.value=`attribute vec4 p;\nvoid main(){
gl_PointSize=200.;\n gl_Position=p;\n}`;
FS.value=`precision highp float;\nuniform vec4 u;\nvoid main(){
gl_FragColor=vec4(1);\n gl_FragColor.rg=gl_PointCoord.xy;