Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE HTML><html><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
- <title>Chrome,Opera,are better for Webgl</title><style>body{margin: 0px;overflow: hidden;}</style></head> <body><canvas id="can"></canvas><div id="i"></div><script id="vs"type="x-shader/vertex">attribute vec3 position;void main(){gl_Position=vec4(position,1.0);}</script><script id="fs"type="x-shader/fragment">
- //the interesting code segment that sets the scene starts at line 33:
- //@Ollj made this;
- //...tiny Offline HTML5 WebGl|GLES canvas Boilerplate ShaderToy.com GlslSandbox GLSL Vertex Shader Test
- //...to more easily code GlGl offline, without any external libraries as single .html file, best with Chrome.
- //The methods are;
- //...commonly used to visualize linear algebra, geometry or raymarching/sphere-tracking signed distance functions.
- //The error message line numbers are;
- //... off by +1 line, that line is the html header above
- //... and even though error messages are minimalistic glsl, ollj saw other boilerplates handling errors much worse.
- //the IO functions of the Boilerplate;
- //...is still pretty messy, handlers and variables exist but are not utilized well. mouse "works"
- //...no keyboard input in here, as I saw no use for it, not yet.
- #ifdef GL_ES
- precision highp float;
- #endif
- //#extension GL_OES_standard_derivatives : enable
- //"GL_OES_standard_derivatives" is used a lot in shadertoy but it just doesn't work too well for many environments.
- ///---start of input values, constant and identical for each pixel, set by "Boilerplate" code segment FAR below---
- //uniforms have duplicate aliases for compatibility to copy paste scene-code from different sources.
- uniform vec2 res;////number of screenspace pixel.xy
- uniform vec2 iResolution;//<-res
- uniform vec2 resolution; //<-res
- uniform float time;//increments over time for animation
- uniform float iGlobalTime;//<-time
- uniform vec2 mouse;//mouse position.xy
- uniform vec2 iMouse;//<-mouse, some applications insist on a vec4 mouse.
- //no support for images yet.
- #ifdef GL_ES
- precision mediump float;
- #endif
- ////---end of input values, constant and identical for each pixel, set by "Boilerplate" code segment FAR below---
- ////------start of variable scene code---
- // Voronoi
- // By: Brandon Fogerty
- // bfogerty at gmail.com
- // xdpixel.com
- //@ollj modified it to use as simple "random level generator" by making the following values constant or seeded:
- //- mx mouse.x //set voronoi-zoom -> larger regions
- #define mx mouse.x
- //- my mouse.y //set voronoi-seed within randomVec2() -> different level.
- #define my mouse.y
- //- tilesize //set tile size. application dependent //larger tilesize makes larger more blocky image.
- #define tilesize 10.0
- //- darkness //set brightness-threshold that turns flattened grey-scale image to monochrome image.
- // //larger darkness makes more cells black, -0.5->0%; 0.5->50%; 1.5=100%
- #define darkness 0.7
- //- t time //Brownian-like motion of voronoi-cells.
- #define t time
- //below lines 62 and 68 are optional masks. remove them to visualize the underlining subroutines.
- vec2 randomVec2(vec2 p){mat2 m=mat2(15.27,47.63,99.41,88.98);return fract(sin(m*p*my)*46839.32);}
- vec2 voronoi(in vec2 p){vec2 g=floor(p);vec2 f=fract(p);vec3 res=vec3(100.0,0.0,0.0);
- for(int y=-1;y<=1;++y){for(int x=-1;x<=1;++x){
- vec2 l=vec2(x,y);vec2 o=randomVec2(l+g);
- float d=distance(l+(sin(t*o)*0.5+0.5),f);
- if(d<res.x){res=vec3(d,o);}}}return vec2(res.yz);}
- void main(void){vec4 p=gl_FragCoord;
- //below line is optional, makes scene blocky by applying [tilesize] as voxel size.
- p.x-=mod(p.x,tilesize);p.y-=mod(p.y,tilesize);//doing mod() per dimension makes it more backwards compatible.
- vec2 v=((p.xy/ resolution.xy)*2.0-1.0)*res.x/res.y;vec2 c=voronoi(v*mx*10.);
- vec3 f=cos(c.x*10.+vec3(1.,2.,3.))+.6;//3 color dimensions
- //vec3 f=cos(c.x*10.+vec3(1.,0.,0.));//1 color dimension
- //below line is optional, increases contrast to monochrome
- if(f.x>darkness)f=vec3(1,1,1);else f=vec3(0,0,0);//increase contrast to moochrome
- gl_FragColor=vec4(f,1.0);}
- ///-------end of variable scene code-----
- ///-------end of variable scene code-----
- //below is a wrapper to bridge from common other boilerplates to Shadertoy namespace
- //void main(void){vec4 c=vec4(0.0);mainImage(c,gl_FragCoord.xy);c.w=1.0;gl_FragColor=c;}
- //if (you get the error message "no main() function") just uncomment the line above.
- </script><script>//crossBrowser.requestAnimationFrame by paulirish.com/2011/requestanimationframe-for-smart-animating
- //start of Boilerplate
- window.requestAnimationFrame=window.requestAnimationFrame||(function(){return window.webkitRequestAnimationFrame|| window.mozRequestAnimationFrame||window.oRequestAnimationFrame||window.msRequestAnimationFrame||function(callback,element){window.setTimeout(callback,50/3);};})();
- var c,gl,b,v,f,p,o,mx,my//lots of var's needed to initialize glsl and to bridge a html5 canvas and glsl.
- var lastMouseX = null;var lastMouseY=null;var mouseDown=false;
- var mdX=null;var mdY=null;//mouse speed as derivation of mouse positions over time.
- function handleMouseDown(event){mouseDown = true;
- //lastMouseX=event.clientX;
- //lastMouseY=event.clientY;
- mx=event.clientX*0.001;my=event.clientY*0.001;
- }function handleMouseUp(event){mouseDown=false;
- }function handleMouseMove(event){
- //if (!mouseDown){return;}
- var newX=event.clientX*0.001;var newY=event.clientY*0.001;
- //var deltaX = newX - lastMouseX;
- //var newRotationMatrix = mat4.create();
- //mat4.identity(newRotationMatrix);
- //mat4.rotate(newRotationMatrix, degToRad(deltaX / 10), [0, 1, 0]);
- // var deltaY = newY - lastMouseY;
- // mat4.rotate(newRotationMatrix, degToRad(deltaY / 10), [1, 0, 0]);
- // mat4.multiply(newRotationMatrix, moonRotationMatrix, moonRotationMatrix);
- //lastMouseX=newX;
- //lastMouseY=newY;
- mx=newX;my=newY;}//making a compatible UI, something I don't want to spend much time with for now.
- par={start_time:new Date().getTime(),time:0,screenWidth:0,screenHeight:0,screenRatio:0};init();animate();
- function init(){v=document.getElementById('vs').textContent;f=document.getElementById('fs').textContent;
- c=document.querySelector('canvas');
- c.onmousedown =handleMouseDown;
- document.onmouseup =handleMouseUp;
- document.onmousemove=handleMouseMove;
- try{gl=c.getContext('experimental-webgl');}catch(error){}if(!gl){throw "webgl.context.create.fail";}
- b=gl.createBuffer();gl.bindBuffer(gl.ARRAY_BUFFER,b );
- gl.bufferData(gl.ARRAY_BUFFER,new Float32Array([-1.0,-1.0,1.0,-1.0,-1.0,1.0,1.0,-1.0,1.0,1.0,-1.0,1.0]), gl.STATIC_DRAW);p=createProgram(v,f);onWindowResize();window.addEventListener('resize',onWindowResize,false);
- }function createProgram(v,f){var p=gl.createProgram();var vs=createShader(v,gl.VERTEX_SHADER);
- var fs=createShader('#ifdef GL_ES\nprecision highp float;\n#endif\n\n'+f,gl.FRAGMENT_SHADER);
- if(vs==null||fs==null)return null;
- gl.attachShader(p,vs);gl.attachShader(p,fs);gl.deleteShader(vs);gl.deleteShader(fs);gl.linkProgram(p);
- if(!gl.getProgramParameter(p,gl.LINK_STATUS)){alert("ERROR:\n"+"VALIDATE_STATUS: "+
- gl.getProgramParameter(p,gl.VALIDATE_STATUS)+"\n"+"ERROR: "+gl.getError()+"\n\n"+"-Vertex Shader-\n"+v+
- "\n\n"+"-Fragment Shader-\n"+f);return null;}return p;
- }function createShader(src,type){var s=gl.createShader(type);gl.shaderSource(s,src);gl.compileShader(s);
- if(!gl.getShaderParameter(s,gl.COMPILE_STATUS)){
- alert((type==gl.VERTEX_SHADER?"VERTEX":"FRAGMENT")+" SHADER:\n"+gl.getShaderInfoLog(s));return null;}return s;
- }function onWindowResize(event){c.width =window.innerWidth;c.height=window.innerHeight;
- par.screenWidth =c.width;par.screenHeight=c.height;gl.viewport(0,0,c.width,c.height);
- //document.onmousemove=hmm;
- }function animate(){requestAnimationFrame(animate);render();
- }function hmm(event){//event.clientX; .Y <- mouse when klicked on an element.
- //var r=c.getBoundingClientRect();
- //mx=event.clientX;//-(r.left+r.width *0.5);
- //my=event.clientY;//-(r.top +r.height*0.5);
- //mdX=mx*0.00005;mdY=my*0.00005;
- }function render(){if(!p)return;par.time=new Date().getTime()-par.start_time;
- //if(mx<0.1)mx=0.1;//lastMouseX;
- //if(mx>1.0)mx=1.0;
- //my=0.0;//lastMouseY;//min(max(my,1.0),0.0);
- //setting uniforms of glsl. this is the bridge between html5-canvas and glsl, .js var's set glsl constants:
- gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT);gl.useProgram(p);
- gl.uniform1f(gl.getUniformLocation(p,'iGlobalTime'),par.time/1000.0);
- gl.uniform1f(gl.getUniformLocation(p,'time'),par.time/1000.0);
- gl.uniform2f(gl.getUniformLocation(p,'res'),par.screenWidth,par.screenHeight);
- gl.uniform2f(gl.getUniformLocation(p,'resolution'),par.screenWidth,par.screenHeight);
- gl.uniform2f(gl.getUniformLocation(p,'iResolution'),par.screenWidth,par.screenHeight);
- gl.uniform2f(gl.getUniformLocation(p,'mouse' ),mx*1.02,(1.0-my*2.0));
- gl.uniform2f(gl.getUniformLocation(p,'iMouse'),mx*1.02,(1.0-my*2.0));
- gl.bindBuffer(gl.ARRAY_BUFFER,b);//Render
- gl.vertexAttribPointer(o,2,gl.FLOAT,false,0,0);gl.enableVertexAttribArray(o);gl.drawArrays(gl.TRIANGLES,0,6);gl.disableVertexAttribArray(o);}</script></body></html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement