Skip to content

Instantly share code, notes, and snippets.

@AGoblinKing
Created July 31, 2020 01:55
Show Gist options
  • Save AGoblinKing/80d5a0340f0fc6962188c8006997a118 to your computer and use it in GitHub Desktop.
Save AGoblinKing/80d5a0340f0fc6962188c8006997a118 to your computer and use it in GitHub Desktop.
MIT sublicenses.
// The MIT License
// Copyright © 2017 Shuichi Hayashi
// License of original voronoi implementation (https://www.shadertoy.com/view/ldl3W8)
//
// The MIT License
// Copyright © 2013 Inigo Quilez
// Permission is hereby granted, free of charge,to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#define ANIMATE
const float ANIMATION_RADIUS = 0.4; // up to 0.5
const float NUM_CELLS = 10.0;
const float IOR = 1.5;
const float F0 = ((1.0-IOR)/(1.0+IOR)) * ((1.0-IOR)/(1.0+IOR));
const float F90 = 1.0;
const float BASE_SHARPNESS = 1.0;
const float cubemapPower = 15.0;
const float chromaFactor = 6.0;
const float toneFactor = 0.55;
const float voronoiColorFactor = 1.0 / cubemapPower * chromaFactor;
varying vec4 v_color;
varying vec2 v_frag;
varying float v_time;
varying float v_influence;
vec2 hash2( vec2 p )
{
// procedural white noise
return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
}
// from https://www.shadertoy.com/view/ldl3W8 (bit modified to return desired values)
// returns,
// x: distance to nearest neighbor
// y: cell id
// z: distance to border
vec3 voronoi( in vec2 x )
{
vec2 n = floor(x);
vec2 f = fract(x);
//----------------------------------
// first pass: regular voronoi
//----------------------------------
vec2 mg, mr;
float id;
float md = 8.0;
for( int j=-1; j<=1; j++ )
for( int i=-1; i<=1; i++ )
{
vec2 g = vec2(float(i),float(j));
vec2 o = hash2( n + g );
#ifdef ANIMATE
vec2 ao = (1.-ANIMATION_RADIUS) + ANIMATION_RADIUS*sin( v_time + 6.2831*o );
#else
vec2 ao = o;
#endif
vec2 r = g + ao - f;
float d = dot(r,r);
if( d<md )
{
md = d;
mr = r;
mg = g;
id = o.x+o.y;
}
}
//----------------------------------
// second pass: distance to borders
//----------------------------------
md = 8.0;
for( int j=-2; j<=2; j++ )
for( int i=-2; i<=2; i++ )
{
vec2 g = mg + vec2(float(i),float(j));
vec2 o = hash2( n + g );
#ifdef ANIMATE
o = (1.0-ANIMATION_RADIUS) + ANIMATION_RADIUS*sin( v_time + 6.2831*o );
#endif
vec2 r = g + o - f;
if( dot(mr-r,mr-r)>0.00001 )
md = min( md, dot( 0.5*(mr+r), normalize(r-mr) ) );
}
return vec3( length(mr), id, md );
}
void main() {
vec2 p = v_frag.xy/1.5;
// compute voronoi pattern
vec3 c = voronoi(p*NUM_CELLS );
// colorize
vec3 col = 0.5 + 0.5*cos( c.y*6.2831 + vec3(0.0,1.0,2.0) );
col *= voronoiColorFactor;
float thickness = smoothstep(0.0, 0.4, c.z);
thickness = pow((thickness), 0.25);
vec4 res_color = vec4(mix(v_color.rgb, vec3(1.0, 1.0, 1.0), v_influence), v_color.a);
gl_FragColor = vec4(thickness * 10. * col, 1.) * res_color;
}
import vertexShader from './stained_glass.vert'
import fragmentShader from './stained_glass.frag'
AFRAME.registerShader('stainedglass', {
schema: {
timeMsec: { type: 'time', is: 'uniform', default: 1.0 },
color: { type: 'color', is: 'uniform', default: 'white' },
opacity: { type: 'number', is: 'uniform', default: 1.0 },
offset: { type: 'number', is: 'uniform', default: Math.random() },
influence: { type: 'number', is: 'uniform', default: 0.5 },
},
vertexShader,
fragmentShader,
})
uniform float timeMsec;
uniform vec3 color;
uniform float opacity;
uniform float influence;
uniform float offset;
varying vec2 v_frag;
varying float v_time;
varying vec4 v_color;
varying float v_influence;
void main() {
gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );
v_frag = uv;
v_time = timeMsec/1000.0/2. + offset * 100.;
v_color = vec4(color, opacity);
v_influence = influence;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment