HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js"></script>
<script id="vertexShader" type="x-shader/x-vertex">
void main() {
gl_Position = vec4( position, 1.0 );
}
</script>
<script id="fragmentShader" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
uniform sampler2D u_noise;
uniform sampler2D u_environment;
vec2 movement;
float scale = 5.;
vec2 hash2(vec2 p)
{
vec2 o = texture2D( u_noise, (p+0.5)/256.0, -100.0 ).xy;
return o;
}
void grid(vec2 uv, inout vec3 colour, vec3 gridcolour, vec2 size, float linewidth) {
vec2 grid = (fract(uv/size+.5)-.5)*size;
grid = abs(grid);
float gridlines = smoothstep(linewidth, linewidth + 0.005, min(grid.x, grid.y));
colour = mix(gridcolour, colour, gridlines);
}
float voronoi(vec2 uv, inout vec2 n_point, inout vec2 s_n_point, inout float s_dist) {
float dist = 4.;
s_dist = 4.;
float s_result = 0.;
vec2 grid_id = floor(uv);
vec2 grid_uv = fract(uv);
float exponent = clamp(abs(u_mouse.y * 50.), 7., 50.);
float result = 0.;
for(float j = -1.; j < 2.; j++) {
for(float i = -1.; i < 2.; i++) {
vec2 offset = vec2(i, j);
vec2 grid_test_id = grid_id + offset;
vec2 rand = hash2(grid_test_id);
vec2 point_pos = offset + rand - grid_uv;
// The following adds some random animation to the particles
rand = hash2(grid_test_id + 1000.);
rand = 0.5 + 0.4*sin((u_time) + 6.2831*rand);
point_pos = offset + rand - grid_uv;
// float len = length(point_pos); // the length gives us a more euclidian (conic) length
float len = dot(point_pos, point_pos); // The float gives us a more rounded distance
// float len = abs(point_pos.x)+abs(point_pos.y); // manhatten distance
result += exp( -exponent*len ); // To soften the effect, use this. You'll also need to return the log result, commented out below
if(len < dist) {
s_dist = dist;
dist = len;
s_n_point = n_point;
n_point = point_pos;
} else if (len < s_dist) {
s_dist = len;
s_n_point = point_pos;
}
}
}
return -(1.0/exponent)*log( result );
return dist;
}
// Naive environment mapping. Pass the reflected vector and pull back the texture position for that ray.
vec3 envMap(vec3 rd, vec3 sn){
rd.xy -= u_time*.2; // This just sort of compensates for the camera movement
// rd.xy -= movement;
rd /= scale; // scale the whole thing down a but from the scaled UVs
vec3 col = texture2D(u_environment, rd.xy - .5, 100.).rgb;
col *= normalize(col);
// col *= vec3(1., 1., 1.2);
// vec3 col = vec3(hash2(rd.xy).y * .5 + .5);
return col;
}
float bumpMap(vec2 uv, inout vec2 q, inout vec2 r, inout float s_dist) {
float vor = voronoi(uv, q, r, s_dist);
// return dot(r, r);
// return s_dist * vor;
return abs(vor - 1.) * ((u_mouse.x + .5) * 3. + 1.);
}
vec4 renderPass(vec2 uv, vec2 uvoffset) {
vec3 surfacePos = vec3(uv, 0.0);
vec3 ray = normalize(vec3(uv - movement, 1.));
// vec3 lightPos = vec3(cos(u_time / 2.) * 2., sin(u_time / 2.) * 2., -3.);
vec3 lightPos = vec3(0., 0., -3.) + vec3(movement, 0.);
vec3 normal = vec3(0., 0., -1);
vec2 sampleDistance = vec2(1. / u_resolution.x, 0.);
vec2 q = vec2(0.,0.);
vec2 r = vec2(0.,0.);
float s_dist = 4.;
float fx = bumpMap(surfacePos.xy-sampleDistance.xy + uvoffset, q, r, s_dist);
float fy = bumpMap(surfacePos.xy-sampleDistance.yx + uvoffset, q, r, s_dist);
s_dist = 4.;
float f = bumpMap(surfacePos.xy + uvoffset, q, r, s_dist);
fx = (fx-f)/sampleDistance.x;
fy = (fy-f)/sampleDistance.x;
normal = normalize( normal + vec3(fx, fy, 0) * 0.2 );
vec3 lightV = lightPos - surfacePos;
float lightDist = max(length(lightV), 0.001);
lightV /= lightDist;
vec3 lightColour = vec3(.8, .8, 1.);
float shininess = 1.;
float brightness = 4.;
float roughness = 2.;
float falloff = 0.6;
float attenuation = 1./(1.0 + lightDist*lightDist*falloff);
float diffuse = max(dot(normal, lightV), 0.);
float specular = pow(max(dot( reflect(-lightV, normal), -ray), 0.), 50.) * shininess;
// specular *= sqrt(roughness);
vec2 _q = abs(normalize(q));
// vec3 tex = texture2D(u_environment, (reflect(vec3(uv, -1.), normal)).xy ).rgb;
vec3 reflect_ray = reflect(vec3(uv - movement, 1.), normal * 1.);
// The reflect ray is the ray wwe use to determine the reflection.
// We use the UV less the movement (to account for "environment") to the surface normal
vec3 tex = envMap(reflect_ray, normal); // Fake environment mapping.
// vec3 texCol = (vec3(s_dist, f, .5)) * brightness;
vec3 texCol = tex * brightness;
vec3 colour = (texCol * (diffuse*vec3(1, .97, .92)*2. + 0.5) + lightColour*specular * f * 2.)*attenuation*1.5;
// colour += texCol * .2;
return vec4(colour, 1.);
}
void main() {
vec2 uv = (gl_FragCoord.xy - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
uv *= scale;
movement = vec2(u_time, 0.);
uv += movement;
// vec4 render = renderPass(uv, vec2(cos(u_time), sin(u_time)));
vec4 render = renderPass(uv, vec2(0.));
// grid(uv, colour, vec3(1.), vec2(1.), .005);
gl_FragColor = render;
}
</script>
<div id="container" touch-action="none"></div>
body {
margin: 0;
padding: 0;
}
#container {
position: fixed;
touch-action: none;
}
/*
Most of the stuff in here is just bootstrapping. Essentially it's just
setting ThreeJS up so that it renders a flat surface upon which to draw
the shader. The only thing to see here really is the uniforms sent to
the shader. Apart from that all of the magic happens in the HTML view
under the fragment shader.
*/
let container;
let camera, scene, renderer;
let uniforms;
let loader=new THREE.TextureLoader();
let texture, environment;
loader.setCrossOrigin("anonymous");
loader.load(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/noise.png',
function texture_load(tex) {
texture = tex;
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.minFilter = THREE.LinearFilter;
loader.load(
'https://s3-us-west-2.amazonaws.com/s.cdpn.io/982762/env_lat-lon.png',
function environment_load(tex) {
environment = tex;
environment.wrapS = THREE.RepeatWrapping;
environment.wrapT = THREE.RepeatWrapping;
environment.minFilter = THREE.LinearFilter;
init();
animate();
}
);
}
);
function init() {
container = document.getElementById( 'container' );
camera = new THREE.Camera();
camera.position.z = 1;
scene = new THREE.Scene();
var geometry = new THREE.PlaneBufferGeometry( 2, 2 );
uniforms = {
u_time: { type: "f", value: 1.0 },
u_resolution: { type: "v2", value: new THREE.Vector2() },
u_noise: { type: "t", value: texture },
u_environment: { type: "t", value: environment },
u_mouse: { type: "v2", value: new THREE.Vector2() }
};
var material = new THREE.ShaderMaterial( {
uniforms: uniforms,
vertexShader: document.getElementById( 'vertexShader' ).textContent,
fragmentShader: document.getElementById( 'fragmentShader' ).textContent
} );
material.extensions.derivatives = true;
var mesh = new THREE.Mesh( geometry, material );
scene.add( mesh );
renderer = new THREE.WebGLRenderer();
renderer.setPixelRatio( 1 );
container.appendChild( renderer.domElement );
onWindowResize();
window.addEventListener( 'resize', onWindowResize, false );
document.addEventListener('pointermove', (e)=> {
let ratio = window.innerHeight / window.innerWidth;
uniforms.u_mouse.value.x = (e.pageX - window.innerWidth / 2) / window.innerWidth / ratio;
uniforms.u_mouse.value.y = (e.pageY - window.innerHeight / 2) / window.innerHeight * -1;
e.preventDefault();
});
}
function onWindowResize( event ) {
renderer.setSize( window.innerWidth, window.innerHeight );
uniforms.u_resolution.value.x = renderer.domElement.width;
uniforms.u_resolution.value.y = renderer.domElement.height;
}
function animate() {
requestAnimationFrame( animate );
render();
}
function render() {
uniforms.u_time.value += 0.01;
renderer.render( scene, camera );
}
Also see: Tab Triggers