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 type="text/fragment" id="fragShader">
precision highp float;
uniform vec2 u_resolution;
uniform float u_time;
uniform vec2 u_mouse;
uniform sampler2D s_noise;
uniform vec3 u_cp;
uniform sampler2D b_noise;
varying vec2 v_uv;
// Uncomment to get screenspace antialiasing - nice, but slow
// #define SSAA
/* Raymarching constants */
/* --------------------- */
const float MAX_TRACE_DISTANCE = 10.; // max trace distance
const float INTERSECTION_PRECISION = 0.001; // precision of the intersection
const int NUM_OF_TRACE_STEPS = 256; // max number of trace steps
const float STEP_MULTIPLIER = .4; // the step mutliplier - ie, how much further to progress on each step
/* Structures */
/* ---------- */
struct Camera {
vec3 ro;
vec3 rd;
vec3 forward;
vec3 right;
vec3 up;
float FOV;
};
struct Surface {
float len;
vec3 position;
vec3 colour;
float id;
float steps;
float AO;
};
struct Model {
float dist;
vec3 colour;
float id;
};
vec2 toScreenspace(in vec2 p) {
vec2 uv = (p - 0.5 * u_resolution.xy) / min(u_resolution.y, u_resolution.x);
return uv;
}
mat2 R(float a) {
float c = cos(a);
float s = sin(a);
return mat2(c, -s, s, c);
}
float hash13(vec3 p3)
{
p3 = fract(p3 * .1031);
p3 += dot(p3, p3.zyx + 31.32);
return fract((p3.x + p3.y) * p3.z);
}
vec3 hash33(vec3 p3)
{
p3 = fract(p3 * vec3(.1031, .1030, .0973));
p3 += dot(p3, p3.yxz+33.33);
return fract((p3.xxy + p3.yxx)*p3.zyx);
}
// Simplex 3D Noise
// by Ian McEwan, Ashima Arts
//
vec4 permute(vec4 x){return mod(((x*34.0)+1.0)*x, 289.0);}
vec4 taylorInvSqrt(vec4 r){return 1.79284291400159 - 0.85373472095314 * r;}
float snoise(vec3 v){
const vec2 C = vec2(1.0/6.0, 1.0/3.0) ;
const vec4 D = vec4(0.0, 0.5, 1.0, 2.0);
// First corner
vec3 i = floor(v + dot(v, C.yyy) );
vec3 x0 = v - i + dot(i, C.xxx) ;
// Other corners
vec3 g = step(x0.yzx, x0.xyz);
vec3 l = 1.0 - g;
vec3 i1 = min( g.xyz, l.zxy );
vec3 i2 = max( g.xyz, l.zxy );
// x0 = x0 - 0. + 0.0 * C
vec3 x1 = x0 - i1 + 1.0 * C.xxx;
vec3 x2 = x0 - i2 + 2.0 * C.xxx;
vec3 x3 = x0 - 1. + 3.0 * C.xxx;
// Permutations
i = mod(i, 289.0 );
vec4 p = permute( permute( permute(
i.z + vec4(0.0, i1.z, i2.z, 1.0 ))
+ i.y + vec4(0.0, i1.y, i2.y, 1.0 ))
+ i.x + vec4(0.0, i1.x, i2.x, 1.0 ));
// Gradients
// ( N*N points uniformly over a square, mapped onto an octahedron.)
float n_ = 1.0/7.0; // N=7
vec3 ns = n_ * D.wyz - D.xzx;
vec4 j = p - 49.0 * floor(p * ns.z *ns.z); // mod(p,N*N)
vec4 x_ = floor(j * ns.z);
vec4 y_ = floor(j - 7.0 * x_ ); // mod(j,N)
vec4 x = x_ *ns.x + ns.yyyy;
vec4 y = y_ *ns.x + ns.yyyy;
vec4 h = 1.0 - abs(x) - abs(y);
vec4 b0 = vec4( x.xy, y.xy );
vec4 b1 = vec4( x.zw, y.zw );
vec4 s0 = floor(b0)*2.0 + 1.0;
vec4 s1 = floor(b1)*2.0 + 1.0;
vec4 sh = -step(h, vec4(0.0));
vec4 a0 = b0.xzyw + s0.xzyw*sh.xxyy ;
vec4 a1 = b1.xzyw + s1.xzyw*sh.zzww ;
vec3 p0 = vec3(a0.xy,h.x);
vec3 p1 = vec3(a0.zw,h.y);
vec3 p2 = vec3(a1.xy,h.z);
vec3 p3 = vec3(a1.zw,h.w);
//Normalise gradients
vec4 norm = taylorInvSqrt(vec4(dot(p0,p0), dot(p1,p1), dot(p2, p2), dot(p3,p3)));
p0 *= norm.x;
p1 *= norm.y;
p2 *= norm.z;
p3 *= norm.w;
// Mix final noise value
vec4 m = max(0.6 - vec4(dot(x0,x0), dot(x1,x1), dot(x2,x2), dot(x3,x3)), 0.0);
m = m * m;
return 42.0 * dot( m*m, vec4( dot(p0,x0), dot(p1,x1),
dot(p2,x2), dot(p3,x3) ) );
}
// Triplanar blending factor - controls the sharpness of the blending
const float blendSharpness = 2.0;
vec2 triplanarUV(vec3 worldPos, vec3 axis) {
return (worldPos - dot(worldPos, axis) * axis).xy;
}
vec4 triplanarTexture(sampler2D texture, vec3 worldPos, vec3 normal) {
// Compute weights for each axis based on the normal
vec3 weights = pow(abs(normal), vec3(blendSharpness));
weights /= dot(weights, vec3(1.0));
// Sample the texture for each axis
vec4 xTex = texture2D(texture, triplanarUV(worldPos, vec3(1, 0, 0)));
vec4 yTex = texture2D(texture, triplanarUV(worldPos, vec3(0, 1, 0)));
vec4 zTex = texture2D(texture, triplanarUV(worldPos, vec3(0, 0, 1)));
// Blend the textures based on the weights
return xTex * weights.x + yTex * weights.y + zTex * weights.z;
}
vec4 boxmap( in sampler2D s, in vec3 p, in vec3 n, in float k )
{
// project+fetch
vec4 x = texture2D( s, p.yz );
vec4 y = texture2D( s, p.zx );
vec4 z = texture2D( s, p.xy );
// and blend
vec3 m = pow( abs(n), vec3(k) );
return (x*m.x + y*m.y + z*m.z) / (m.x + m.y + m.z);
}
//--------------------------------
// Modelling
//--------------------------------
Model model(vec3 p) {
vec2 n = vec2(length(p.xz) - .35 - (cos(u_time*5.)*.05+.05), p.y);
float a = atan(p.x, p.z), ca=cos(a*3.+u_time*10.);
n *= R(a*1.5+u_time*8.);
n.y = abs(n.y)-.15 ;
float d = length(n)-.08+ca*.03;
vec3 pm = p * mix(1., .8+cos(u_time*3.+a*2.)*.5, sin(u_time*2.)*.5+.5);
vec3 m = mod(pm, .1)-.05;
vec3 id = floor((pm)/.1);
vec3 gid = floor((pm)/.3);
// d = length(vec2(max(0., d-.02), max(0., length(m)-.4)))-.01;
// d = max(d, length(m)-.3)-.1;
d = length(vec2(max(0., d), max(0., length(m)-.035-ca*.01)))-.02+ca*.01;
vec3 colour = vec3(hash33(id).rrb * vec3(1,.5,.1) + vec3(0,.5,.7));
colour *= hash13(gid)+.3;
return Model(d, colour, 1.);
}
Model map( vec3 p ){
return model(p);
}
Surface calcIntersection( in Camera cam ){
float h = INTERSECTION_PRECISION;
float rayDepth = 0.0;
float hitDepth = -1.0;
float id = -1.;
float steps = 0.;
float ao = 0.;
vec3 position;
vec3 colour;
for( int i=0; i< NUM_OF_TRACE_STEPS ; i++ ) {
if( abs(h) < INTERSECTION_PRECISION || rayDepth > MAX_TRACE_DISTANCE ) break;
position = cam.ro+cam.rd*rayDepth;
Model m = map( position );
h = m.dist;
rayDepth += h * STEP_MULTIPLIER;
id = m.id;
steps += 1.;
ao += max(h, 0.);
colour = m.colour;
}
if( rayDepth < MAX_TRACE_DISTANCE ) hitDepth = rayDepth;
if( rayDepth >= MAX_TRACE_DISTANCE ) id = -1.0;
return Surface( hitDepth, position, colour, id, steps, ao );
}
Camera getCamera(in vec2 uv, in vec3 pos, in vec3 target) {
vec3 forward = normalize(target - pos);
vec3 right = normalize(vec3(forward.z, 0., -forward.x));
vec3 up = normalize(cross(forward, right));
float FOV = .6;
return Camera(
pos,
normalize(forward + FOV * uv.x * right + FOV * uv.y * up),
forward,
right,
up,
FOV
);
}
float softshadow( in vec3 ro, in vec3 rd, in float mint, in float tmax ) {
float res = 1.0;
float t = mint;
for( int i=0; i<16; i++ ) {
float h = map( ro + rd*t ).dist;
res = min( res, 8.0*h/t );
t += clamp( h, 0.02, 0.10 );
if( h<0.001 || t>tmax ) break;
}
return clamp( res, 0.0, 1.0 );
}
float calcAO( in vec3 pos, in vec3 nor ) {
float occ = 0.0;
float sca = 1.0;
for( int i=0; i<5; i++ )
{
float hr = 0.01 + 0.12*float(i)/4.0;
vec3 aopos = nor * hr + pos;
float dd = map( aopos ).dist;
occ += -(dd-hr)*sca;
sca *= 0.95;
}
return clamp( 1.0 - 3.0*occ, 0.0, 1.0 );
}
vec3 shade(vec3 col, vec3 pos, vec3 nor, vec3 ref, Camera cam) {
// lighitng
float occ = calcAO( pos, nor );
vec3 lig = normalize( vec3(-0.6, 0.7, -0.3) );
float amb = clamp( 0.5+0.5*nor.y, 0.0, 1.0 );
float dif = clamp( dot( nor, lig ), 0.0, 1.0 );
float bac = clamp( dot( nor, normalize(vec3(-lig.x,0.0,-lig.z))), 0.0, 1.0 )*clamp( 1.0-pos.y,0.0,1.0);
//float dom = smoothstep( -0.1, 0.1, ref.y );
float fre = pow( clamp(1.0+dot(nor,cam.rd),0.0,1.0), 2.0 );
// float spe = pow(clamp( dot( ref, lig ), 0.0, 1.0 ),16.0);
vec3 lin = vec3(0.0);
lin += 1.20*dif*vec3(.85,0.80,0.70);
// lin += 1.20*spe*vec3(1.00,0.85,0.55)*dif;
// lin += 0.80*amb*vec3(0.50,0.70,.80)*occ;
float hem = dot(nor, vec3(.1,1,0)) *.5 + .5;
// lin += mix(vec3(1,0,0), vec3(0,0,1), hem);
lin += mix(vec3(.5, .4, .3), vec3(0.1,.1,.6)*.5, hem);
//lin += 0.30*dom*vec3(0.50,0.70,1.00)*occ;
lin += 0.30*bac*vec3(0.25,0.25,0.25)*occ;
lin += 0.80*fre*vec3(1.30,1.20,1.00)*occ;
col = col*lin;
// col = vec3(fre);
return col;
}
// Calculates the normal by taking a very small distance,
// remapping the function, and getting normal for that
vec3 calcNormal( in vec3 pos ){
vec3 eps = vec3( 0.001, 0.0, 0.0 );
vec3 nor = vec3(
map(pos+eps.xyy).dist - map(pos-eps.xyy).dist,
map(pos+eps.yxy).dist - map(pos-eps.yxy).dist,
map(pos+eps.yyx).dist - map(pos-eps.yyx).dist );
return normalize(nor);
}
vec3 render(Surface surface, Camera cam, vec2 uv) {
vec3 colour = vec3(.04,.045,.05);
colour = vec3(.35, .5, .75);
vec3 colourB = vec3(.9, .85, .8);
vec2 pp = uv;
colour = mix(colourB, colour, pow(length(pp), 2.)/1.5);
if (surface.id == 1.){
vec3 surfaceNormal = calcNormal( surface.position );
vec3 ref = reflect(cam.rd, surfaceNormal);
colour = surfaceNormal;
vec3 pos = surface.position;
vec3 col;
// float r = length(pos);
// vec3 q = vec3(r, atan(pos.x, pos.y), acos(pos.z/r));
// vec2 uv = vec2(q.y/6.28318530718, q.z/3.1415926);
// vec3 col = vec3(uv, 1.);
// col = texture2D(s_noise,uv*10.).rrr*.2+.4;
// col += surface.colour*.5-.25;
// col = triplanarTexture(s_noise, pos*10., surfaceNormal).rrr;
float d = 10.+ smoothstep(2., .01, length(cam.ro - pos))*10.;
col = boxmap(s_noise, pos*d, surfaceNormal, 6.).rrr;
col += surface.colour*.5-.25;
colour = shade(col, pos, surfaceNormal, ref, cam);
}
return colour;
}
void main() {
#ifdef SSAA
vec3 c = vec3(0);
for(int x=0; x<2; x++) {
for(int y=0; y<2; y++) {
vec2 uv = toScreenspace(gl_FragCoord.xy+vec2(x,y)*.5);
Camera cam = getCamera(uv, u_cp * .01, vec3(0));
Surface surface = calcIntersection(cam);
c += render(surface, cam, uv);
}
}
gl_FragColor = vec4(c*.25,1);
#else
vec2 uv = toScreenspace(gl_FragCoord.xy);
// Camera cam = getCamera(uv, vec3(-0.5,.5,-1)*1.7, vec3(0));
Camera cam = getCamera(uv, u_cp * .01, vec3(0));
Surface surface = calcIntersection(cam);
gl_FragColor = vec4(render(surface, cam, uv), 1.);
#endif
}
</script>
body {
background: #333;
color: #fff;
font-family: sans-serif;
}
body,
html {
margin: 0;
overflow: hidden;
padding: 0;
}
canvas { width:100%; height: 100%; }
console.clear();
import { FragmentShader, Texture, Uniform, DollyCamera } from 'https://cdn.skypack.dev/wtc-gl@1.0.0-beta.51';
import { Vec2, Vec3, Mat4 } from "https://cdn.skypack.dev/wtc-math@1.0.17";
const shaderF = document.querySelector('#fragShader').innerText;
const camera = new DollyCamera({},{far:1000});
const cp = new Uniform({
name: "cp",
value: [50,50,100],
kind: "vec3"
})
// Create the fragment shader wrapper
const FSWrapper = new FragmentShader({
fragment: shaderF,
onBeforeRender: (t) => {
camera.update();
cp.value = camera.position.multiplyNew(new Vec3(-1,1,1)).array;
}
});
FSWrapper.playing = false;
const { gl, uniforms } = FSWrapper;
camera.setPosition(100, 100, -200.);
uniforms.u_cp = cp;
// Create the texture
const texture = new Texture(gl, {
wrapS: gl.REPEAT,
wrapT: gl.REPEAT
});
// Load the image into the uniform
const img = new Image();
img.crossOrigin = "anonymous";
img.src = "https://assets.codepen.io/982762/noise.png";
img.onload = () => { FSWrapper.playing = true; (texture.image = img) };
uniforms.s_noise = new Uniform({
name: "noise",
value: texture,
kind: "texture"
});
Also see: Tab Triggers