Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <!-- Shader Session with cacheflowe | 2022
 *
 * cacheflowe: 
 * https://cacheflowe.com
 * https://www.instagram.com/cacheflowe/
 *
 * We're curiouslyminded:
 * https://www.curiouslyminded.xyz
 * https://www.twitch.tv/curiouslyminded
 * https://www.youtube.com/curiouslyminded -->

<div id="shadercollab"></div>

<script id="vertex" type="x-shader/x-vertex">
	void main() { gl_Position = vec4(position, 1.0); }
</script>

<script id="fragment" type="x-shader/x-fragment">
precision highp float;

uniform vec2 u_resolution;
uniform float u_time;

////////////////////////
// helpers
////////////////////////

#define PI     3.14159265358
#define TWO_PI 6.28318530718

vec2 rotateCoord(vec2 uv, float rads) {
    uv *= mat2(cos(rads), sin(rads), -sin(rads), cos(rads));
    return uv;
}

float saw(float rads) {
    rads += PI * 0.5;
    float percent = fract(rads/PI);
    float dir = sign(sin(rads));
    return dir * (2. * percent  - 1.);
}

float oscBetween(float low, float high, float time, float offset) {
  float range = abs(high - low);
  float halfRange = range / 2.;
  float midPoint = low + halfRange;
  return midPoint + halfRange * sin(offset + time);
}

////////////////////////
// patterns
////////////////////////

vec3 drawChevronStripes(vec2 uv) {
    // rotate
    float rotate = oscBetween(-1., 1., u_time/2., 0.);
    uv = rotateCoord(uv, rotate * -1.);
    uv.y *= u_resolution.y / u_resolution.x;
    // build params
    float altTime = u_time * 0.5;
    float chevronAmp = 0.06;
    float freqAmp = oscBetween(0., 1., u_time/2., 0.);
    float freq = 10. + freqAmp * 20.;
    float zoom = oscBetween(0., 1., u_time/3., PI);
    float numLines = 20. + zoom * 100.;
    float x = uv.x;
    // lerp between saw & sin
    float sawWaveDisp = saw(x * freq);
    float sinWaveDisp = sin(x * freq);
    uv.y += chevronAmp * mix(sawWaveDisp, sinWaveDisp, 0.5 + 0.5 * sin(altTime));
    float col = 0.5 + 0.5 * sin(uv.y * numLines);
    return vec3(0.0, col, 0.0);
}

vec3 drawWarpVortex(vec2 uv) {
    float rotate = oscBetween(-1., 1., u_time/3., 0.);
    float altTime = u_time * 0.05;
    float rads = atan(uv.x, uv.y) + rotate; 
    float zoom = oscBetween(0.3, 1., u_time/3., PI);
    float dist = length(uv) * zoom;
    float spinAmp = oscBetween(-2., 2., u_time/4., 0.);
    float spinFreq = oscBetween(0.3, 5., u_time/3., PI);;
    rads += sin(altTime + dist * spinFreq) * spinAmp * (1. - dist/8.);
    float radialStripes = 24.;
    float col = 0.5 + 0.5 * sin(rads * radialStripes);
    return vec3(col);
}
	
vec3 newPattern(vec2 uv) {	
	float rotate = oscBetween(-1., 1., u_time/2., 0.);
    uv = rotateCoord(uv, rotate * -1.);
    uv.y *= u_resolution.y / u_resolution.x;

	
	float stripes = fract(
		uv.y * 5.0 + u_time
	);
	
	return vec3(stripes, 0., 0.);
}

////////////////////////
// main! combine the patterns
////////////////////////

void main()
{
    // Centered pixel coordinates
    //c2 uv =  (-iResolution.xy + 2.0*fragCoord)/iResolution.y;
	vec2 uv = (gl_FragCoord.xy - (u_resolution.xy * .5)) / u_resolution.yy;
	uv *= 3.; // zoom a bit
	
    // oscillated pattern mix
    float drawFunc1Mix = oscBetween(0.5, 1., u_time/3., 0.);
    float drawFunc2Mix = oscBetween(0.5, 1., u_time/2., PI/2.);
	float drawFunc3Mix = oscBetween(0.5, 1., u_time/1., 0.);

    // sum of patterns
    vec3 col = vec3(0.);
    col += drawFunc1Mix * drawChevronStripes(uv);
    col += drawFunc2Mix * drawWarpVortex(uv);
	col += drawFunc3Mix * newPattern(uv);
    
    // test individual patterns
    
	//col = newPattern(uv);

    // "threshold" combine patterns & output
    col = smoothstep(0.45, 0.55, col);
    gl_FragColor = vec4(col, 1.0);
}
</script>
              
            
!

CSS

              
                $c1: #000; // black
$c2: #fff; // white
$c3: #811121; // dark red
$c4: yellow;

* {
	user-select: none;
}

body {
	height: 100vh;
	background-color: $c1;
	margin: 0;
	padding: 0;
	overflow: hidden;
	position: relative;
	
	&.is-debugging {
		overflow: scroll;
	}
}

.debug-drawer {
	width: auto;
	position: absolute;
	top: 0;
	left: 0;
	border: 4px solid gray;
	
	pre {
		background-color: $c1; // black
		color: $c2; // white
		padding: 5px;
		margin: 0;
		
		.error-line {
			background: $c3;
		}
		
		.error {
			color: $c4;
			font-style: italic;
		}
	}
}
              
            
!

JS

              
                /* 
 * SHADER SESSION WITH CACHEFLOWE
 * NOV 2022
 *
 * cacheflowe: 
 * https://cacheflowe.com
 * https://www.instagram.com/cacheflowe/
 *
 * We're curiouslyminded:
 * https://www.curiouslyminded.xyz
 * https://www.twitch.tv/curiouslyminded
 * https://www.youtube.com/curiouslyminded
 *
 *
 * GLSL Debugger in JS by Sean Zellmer:
 * https://twitter.com/lejeunerenard
 *
 */

let camera, scene, renderer, clock;
let uniforms;

function shaderErrorView (errors, code) {
	// Alter code to display error
	const codeLines = code.trim().split('\n');
	const maxGutter = Math.floor(Math.log10(codeLines.length));
	const errorLines = codeLines.map(function (line, i) {
		i += 1; // Adjust for 1-indexed errors
		
		const lineNumWidth = Math.floor(Math.log10(i));
		const gutter = ' '.repeat(maxGutter - lineNumWidth) + i;
		
		// Error lineNumber is based on non-trimmed code which includes an empty first line
		const lineErrors = errors.filter((error) => error.lineNumber - 1 === i);
		const lineClass = lineErrors.length ? 'error-line' : '';
		let errorMessages = lineErrors.map((e) => '<span class="error">' + e.message + '</span>').join(', ');
		if (errorMessages !== '') errorMessages = ' ' + errorMessages;
		
		return `<span class="${lineClass}">${gutter}| ${line}${errorMessages}</span>`;
	});
	
	const container = document.createElement('div');
	container.classList.add('debug-drawer');
	document.body.classList.add('is-debugging');
	
	const pre = document.createElement('pre');
	const codeContainer = document.createElement('code');
	codeContainer.innerHTML = errorLines.join('\n');

	pre.appendChild(codeContainer);
	container.appendChild(pre);
	
	return container;
}

function checkForShaderErrors (renderer) {
	var errors = [];
	const currentScript = 'fragmentShader';
	
	var programs = renderer.info.programs;

	valid = true;
	var parseMessage = /^(?:ERROR|WARNING): \d+:(\d+): (.*)/gm; // Fixed threejs regex by adding `m` flag

	for (var i = 0, n = programs.length; i !== n; ++i) {
		var diagnostics = programs[ i ].diagnostics;

		if ( diagnostics === undefined ) continue;
		if ( ! diagnostics.runnable ) valid = false;

		var shaderInfo = diagnostics[ currentScript ];
		var lineOffset = shaderInfo.prefix.split( /\r\n|\r|\n/ ).length;

		while(true) {
			var parseResult = parseMessage.exec( shaderInfo.log );
			if ( parseResult === null ) break;

			errors.push({
				lineNumber: parseResult[ 1 ] - lineOffset,
				message: parseResult[ 2 ]
			});
		} // messages

		break;
	} // programs
	
	return errors;
}

function init() {
	const container = document.getElementById("shadercollab");

	clock = new THREE.Clock();
	camera = new THREE.Camera();
	camera.position.z = 1;

	scene = new THREE.Scene();

	const geometry = new THREE.PlaneBufferGeometry(2, 2);

	uniforms = {
		u_time: { type: "f", value: 1.0 },
		u_resolution: { type: "v2", value: new THREE.Vector2() },
		u_mouse: { type: "v2", value: new THREE.Vector2() },
	};

	const material = new THREE.ShaderMaterial({
		uniforms,
		vertexShader: document.getElementById("vertex").textContent,
		fragmentShader: document.getElementById("fragment").textContent
	});

	const mesh = new THREE.Mesh(geometry, material);
	scene.add(mesh);

	renderer = new THREE.WebGLRenderer();
	renderer.setPixelRatio(window.devicePixelRatio);

	container.appendChild(renderer.domElement);
	
	onWindowResize();
	window.addEventListener("resize", onWindowResize);
}

function onWindowResize() {
	renderer.setSize(window.innerWidth, window.innerHeight);
	uniforms.u_resolution.value.x = renderer.domElement.width;
	uniforms.u_resolution.value.y = renderer.domElement.height;
}

let runOnce = false;
function render() {
	uniforms.u_time.value = clock.getElapsedTime();
	renderer.render(scene, camera);
	
	if (!runOnce) {
		const errors = checkForShaderErrors(renderer);
		if (errors.length) {
			const overlay = shaderErrorView(errors, document.getElementById("fragment").textContent);
			document.body.appendChild(overlay);
		}
		runOnce = true;
	}
}

function animate() {
	render();
	requestAnimationFrame(animate);
}

init();
animate();

document.onmousemove = function (e) {
	uniforms.u_mouse.value.x = e.pageX;
	uniforms.u_mouse.value.y = e.pageY;
};
              
            
!
999px

Console