Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <body>

    <div id="container"></div>


  <select class="dropdown" id ="ddl" name="ddl" onmousedown="this.value='';" onchange="dropDownChange(this.value);">
  <option value='fragmentShader0'>0 - Basic shader</option>
    <option value='fragmentShader1'>1 - Simple 2D noise</option>
    <option value='fragmentShader2'>2 -  Distance fields</option>
    <option value='fragmentShader3'>3 - Distance fields + position</option>
    <option value='fragmentShader4'>4 - Cellular noise</option>
    <option value='fragmentShader5'>5 -Polygon Cellular Noise</option>
    <option value='fragmentShader6'>6 - Fractional Brownian Motion</option>
    <option value='fragmentShader7'>7 - Clouds</option>

  </select>

    <script id="vertexShader" type="x-shader/x-vertex">
        void main() {
            gl_Position = vec4( position, 1.0 );
        }
    </script>
  
  
<script id="fragmentShader0" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main() {
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
    gl_FragColor=vec4(st.x,st.y,0.0,1.0);
}
</script>

<script id="fragmentShader1" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define NUM_NOISE_OCTAVES 5

//Random functions are just random math functions 
float random (vec2 st){
    return fract(sin (dot( st.xy, vec2(6424.4347345235, 3434.52347435))) * 3403342.5346453 );
}

float hash(vec2 p) { return fract(1e4 * sin(17.0 * p.x + p.y * 0.1) * (0.1 + abs(sin(p.y * 13.0 + p.x)))); }


//Noise is basically a random number generator, except it is more consistent
float noise (vec2 st){
 vec2 i = floor(st);
 vec2 f = fract(st);

	// Four corners in 2D of a tile
	float a = hash(i);
    float b = hash(i + vec2(1.0, 0.0));
    float c = hash(i + vec2(0.0, 1.0));
    float d = hash(i + vec2(1.0, 1.0));
    //vec2 u = f * f * (3.0 - 2.0 * f);
    vec2 u = smoothstep(0., 1., f);
    
    // Simple 2D lerp using smoothstep envelope between the values.
	 return mix(mix(a, b, u.x),
				mix(c, d, u.x),
				u.y);

}

void main(){
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
    vec3 color = vec3(0.0);  
    st *=  10.;
    st += vec2(u_time, 0.);
   
    color = vec3(noise(st));

    gl_FragColor = vec4(color,1.0);
}
</script>



  
<script id="fragmentShader2" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main(){
     vec2 st = gl_FragCoord.xy/u_resolution.xy;
   // st.x *= u_resolution.x/u_resolution.y;

    vec3 color = vec3(.0);

    // Cell positions
    vec2 point[5];
    point[0] = vec2(0.83,0.75);
    point[1] = vec2(0.60,0.07);
    point[2] = vec2(0.28,0.64);
    point[3] =  vec2(0.31,0.26);
    point[4] = u_mouse/u_resolution;
    
    float m_dist = 1.;  // minimun distance

    // Iterate through the points positions
    for (int i = 0; i < 5; i++) {
        float dist = distance(st, point[i]);
        
        // Keep the closer distance
        m_dist = min(m_dist, dist);
    }
    
    // Draw the min distance (distance field)
    color += m_dist;

    // Show isolines
    // color -= step(.7,abs(sin(50.0*m_dist)))*.3;
    
    gl_FragColor = vec4(color,1.0);
    
}
</script>
  
  
  
<script id="fragmentShader3" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

vec2 random2( vec2 p ) {
    return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
}


void main(){
     vec2 st = gl_FragCoord.xy/u_resolution.xy;
   // st.x *= u_resolution.x/u_resolution.y;
    
    //Scale
    st *= 3.;
   
    // Tile the space
    vec2 i_st = floor(st);
    vec2 f_st = fract(st);

    float m_dist = 1.;  // minimun distance
    
    for (int y= -1; y <= 1; y++) {
        for (int x= -1; x <= 1; x++) {
            // Neighbor place in the grid
            vec2 neighbor = vec2(float(x),float(y));
            
            // Random position from current + neighbor place in the grid
            vec2 point = random2(i_st + neighbor);

			      // Animate the point
            point = 0.5 + 0.5*sin(u_time + 6.2831*point);
            
            vec2 finalPoint = neighbor + point;
            m_dist = min (m_dist, distance (finalPoint, f_st));
        }
    }

    vec3 color = vec3(.0,0.,0.);
    color += m_dist;
    
     // Draw grid
    color.r += step(.98, f_st.x) + step(.98, f_st.y);
    
    // Show isolines
    // color -= step(.7,abs(sin(27.0*m_dist)))*.5; 
    
    gl_FragColor = vec4(color,1.0);
}
</script>
  
  <script id="fragmentShader4" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

void main(){
     vec2 st = gl_FragCoord.xy/u_resolution.xy;
   // st.x *= u_resolution.x/u_resolution.y;

    vec3 color = vec3(.0);

    // Cell positions
    vec2 point[5];
    point[0] = vec2(0.83,0.75);
    point[1] = vec2(0.60,0.07);
    point[2] = vec2(0.28,0.64);
    point[3] =  vec2(0.31,0.26);
    point[4] = u_mouse/u_resolution;
    
    float m_dist = 1.;  // minimun distance
    vec2 m_point; //minimum position

    // Iterate through the points positions
    for (int i = 0; i < 5; i++) {
        float dist = distance(st, point[i]);
        if ( dist < m_dist ) {
            // Keep the closer distance
            m_dist = dist;

            // Kepp the position of the closer point
            m_point = point[i];
        }
    }
    
    // Draw the min distance (distance field)
    color += m_dist * 2.0;
    
     // tint acording the closest point position
    color.rg = m_point;

    // Show isolines
    // color -= step(.7,abs(sin(50.0*m_dist)))*.3;
    // Show isolines
    color -= abs(sin(80.0*m_dist))*0.07;
    
    // Draw point center
    color += 1.-step(.02, m_dist);
    
    gl_FragColor = vec4(color,1.0);
    
}
</script>

  
  <script id="fragmentShader5" type="x-shader/x-fragment">

uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;

vec2 random2( vec2 p ) {
    return fract(sin(vec2(dot(p,vec2(127.1,311.7)),dot(p,vec2(269.5,183.3))))*43758.5453);
}

void main() {
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
    st.x *= u_resolution.x/u_resolution.y;
    vec3 color = vec3(.0);
    
    // Scale 
    st *= 5.;
    
    // Tile the space
    vec2 i_st = floor(st);
    vec2 f_st = fract(st);

    float m_dist = 10.;  // minimun distance
    vec2 m_point;        // minimum point
    
    for (int x=-1; x<=1; x++ ) {
        for (int y=-1; y<=1; y++ ) {
            vec2 neighbor = vec2(float(x),float(y));
            vec2 point = random2(i_st + neighbor);
            point = 0.5 + 0.5*sin(u_time + 6.2831*point);
            vec2 finalPoint = neighbor + point;
            float dist = distance (finalPoint, f_st);

            if( dist < m_dist ) {
                m_dist = dist;
                m_point = point;
            }
        }
    }

    // Assign a color using the closest point position
    color += vec3(0.5) + dot(m_point,vec2(.2,.2));
 
    gl_FragColor = vec4(color,1.0);
}
</script>
  
  <script id="fragmentShader6" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define NUM_NOISE_OCTAVES 5

float random (vec2 st){
    return fract(sin (dot( st.xy, vec2(6424.4347345235, 3434.52347435))) * 3403342.5346453 );
}

float hash(vec2 p) { return fract(1e4 * sin(17.0 * p.x + p.y * 0.1) * (0.1 + abs(sin(p.y * 13.0 + p.x)))); }


float noise (vec2 st){
 vec2 i = floor(st);
 vec2 f = fract(st);

	// Four corners in 2D of a tile
	float a = hash(i);
  float b = hash(i + vec2(1.0, 0.0));
  float c = hash(i + vec2(0.0, 1.0));
  float d = hash(i + vec2(1.0, 1.0));
  
  //vec2 u = f * f * (3.0 - 2.0 * f);
  vec2 u = smoothstep(0., 1., f);
    
    // Simple 2D lerp using smoothstep envelope between the values.
return mix(mix(a, b, u.x),
				mix(c, d, u.x),
				u.y);

}

//Fractional Brownian Motion:
//https://code.google.com/archive/p/fractalterraingeneration/wikis/Fractional_Brownian_Motion.wiki
float fbm(vec2 st) {
	float value = 0.1;
  float amplitude = .5;
  float frequency = 0.;
  
  float lacunarity = 2.; //Increment in random, but regular steps
  float gain = 0.5; //makes value go back down to below 1
  
  for (int i = 0; i < NUM_NOISE_OCTAVES; i++){
      value += amplitude * noise(st);
       
      st *= 2.;  
      amplitude *= .5; 
  }
  return value;
}

void main(){
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
    vec3 color = vec3(0.0);  
    st *=  10.;
    st += vec2(u_time, 0.);
   
    color = vec3(fbm(st));

    gl_FragColor = vec4(color,1.0);
}

  </script>
  
  
  
   
  <script id="fragmentShader7" type="x-shader/x-fragment">
uniform vec2 u_resolution;
uniform vec2 u_mouse;
uniform float u_time;
#define NUM_NOISE_OCTAVES 5


float random (in vec2 _st) { 
    return fract(sin(dot(_st.xy,
                         vec2(12.9898,78.233)))* 
        43758.5453123);
}

float noise (vec2 st){
 vec2 i = floor(st);
 vec2 f = fract(st);

	// Four corners in 2D of a tile
	float a = random(i);
  float b = random(i + vec2(1.0, 0.0));
  float c = random(i + vec2(0.0, 1.0));
  float d = random(i + vec2(1.0, 1.0));
  
  //vec2 u = f * f * (3.0 - 2.0 * f);
  vec2 u = smoothstep(0., 1., f);
    
    // Simple 2D lerp using smoothstep envelope between the values.
return mix(mix(a, b, u.x),
				mix(c, d, u.x),
				u.y);

}

//Fractional Brownian Motion:
//https://code.google.com/archive/p/fractalterraingeneration/wikis/Fractional_Brownian_Motion.wiki
float fbm(vec2 st) {
	float value = 0.1;
  float amplitude = .5;
  float frequency = 0.;
  
  float lacunarity = 2.; //Increment in random, but regular steps
  float gain = 0.5; //makes value go back down to below 1
  
  vec2 shift = vec2(100.);
  mat2 rot = mat2(cos(0.5), sin(0.5), -sin(0.5), cos(0.5)); //removes axial bias
  
  
  for (int i = 0; i < NUM_NOISE_OCTAVES; i++){
      value += amplitude * noise(st);
      st = rot * st * 2. + shift;  
      amplitude *= .5; 
  }
  return value;
}

void main(){
    vec2 st = gl_FragCoord.xy/u_resolution.xy;
    
    vec3 color = vec3(0.0);  
    st *= 3.;
    
    vec2 q = vec2(0.);
    q.x = fbm( st );
    q.y = fbm( st + vec2(1.0));
    
    vec2 r = vec2(0.);
    r.x = fbm( st + 1.0*q + vec2(1.7,9.2)+ 0.15*u_time );
    r.y = fbm( st + 1.0*q + vec2(8.3,2.8)+ 0.126*u_time);
   
    float f = fbm(st + r);
   
   //Tint based on clamp f^2
    color = mix(vec3(0.101961,0.619608,0.666667),
                vec3(0.666667,0.666667,0.498039),
                clamp((f*f)*4.0,0.0,1.0));
   
   //Tint based on length of q
    color = mix(color,
                vec3(0,0,0.164706),
                clamp(length(q),0.0,1.0));
    
    //More powerful tint, with rotation and stuff
    color = mix(color,
                vec3(0.666667,1,1),
                clamp(length(r.x),0.0,1.0));
    
    //f^3 + 0.6f^2 + 0.5f 
    gl_FragColor = vec4((f*f*f+.6*f*f+.5*f)*color,1.);
}

  </script>
              
            
!

CSS

              
                body {
  			background-color: #ffffff;
				margin: 0;
				overflow: hidden;

}

.dropdown{
    position: absolute;
    left: 0px;
    top: 0px;
    z-index: 1;
}
              
            
!

JS

              
                //This is a project for personal educational purposes only
//Most shaders can be found at https://thebookofshaders.com/

var container;
var camera, scene, renderer;
var uniforms;
var mesh;

var shaderName = "fragmentShader7"

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);
  
  
  //Texture loading helper:
  var textureLoader = new THREE.TextureLoader();
  textureLoader.crossOrigin = "anonymous";
  var stonetexture = textureLoader.load("https://cdn.rawgit.com/antu3199/WebAssets/aceb48c0/textures/stonePattern.jpg");
  stonetexture.wrapS = THREE.RepeatWrapping;
  stonetexture.wrapT = THREE.RepeatWrapping;
  
  var normaltexture = textureLoader.load("https://cdn.rawgit.com/antu3199/WebAssets/aceb48c0/textures/normal2.jpg");
  normaltexture.wrapS = THREE.RepeatWrapping;
  normaltexture.wrapT = THREE.RepeatWrapping;
  
  var pikachutexture = textureLoader.load("https://cdn.rawgit.com/antu3199/WebAssets/aceb48c0/textures/pikachu.png");

  

  uniforms = {
    u_time: { type: "f", value: 1.0 },
    u_resolution: { type: "v2", value: new THREE.Vector2() },
    u_mouse: { type: "v2", value: new THREE.Vector2() },
    texture1 : { type: "t", value: stonetexture},
    texture2 : { type: "t", value: normaltexture},
    texture3 : { type: "t", value: pikachutexture}
  };

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

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

  renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
  renderer.setPixelRatio(window.devicePixelRatio);

  container.appendChild(renderer.domElement);

              onWindowResize();
            window.addEventListener( 'resize', onWindowResize, false );

            document.onmousemove = function(e){
              uniforms.u_mouse.value.x = e.pageX
              uniforms.u_mouse.value.y = renderer.domElement.height - e.pageY
            }
        }

        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.02;
            renderer.render( scene, camera );
        }

function dropDownChange(value) {
  mesh.material = new THREE.ShaderMaterial({
    uniforms: uniforms,
    vertexShader: document.getElementById("vertexShader").textContent,
    fragmentShader: document.getElementById(value).textContent
  });

}

              
            
!
999px

Console