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 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

              
                 <head>
    <link href="https://fonts.googleapis.com/css2?family=Rajdhani:wght@500&display=swap" rel="stylesheet">
     <title>Melting Fractal Shader  by makio135 Export to Codepe</title>
    </head>
<body>
    <div class="container"> 
      <div class="container"><canvas id="renderCanvas" class="container" /></div>
      <div class="footer"><a href = "https://hiteshsahu.com/lab">See More in Lab 🧪</a></div> 
     <div class="header"><h3>Melting Fractal Shader  by makio135 Export to Codepen<h2></div>
</div>
</body>
              
            
!

CSS

              
                <link href="https://fonts.googleapis.com/css2?family=Lobster&display=swap" rel="stylesheet">

#renderCanvas {
    position: absolute;
  width: 100%;
  height: 100%;
  touch-action: none;
   left:  0%;
  right: 0%;
  bottom:0%;
  top: 0%;
} 

 html, body {
  overflow: hidden;
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
font-family: 'Rajdhani', sans-serif;
    color: white;
  font-size: large;

}

a:link, a:visited {
  background-color: #f44336;
  color: white;
  padding: 14px 25px;
  text-align: center;
  text-decoration: none;
  display: inline-block;
}

.container {
  position: relative;
  height: 100%;
  width: 100%;
}

.header {
  position: absolute;
  left:0;
  right:0;
  top:0;
  padding-left:10px;
    text-align: center;

}

.footer {
  position: absolute;
  right:0;
  bottom:0;
  margin:5px;
  padding:5px;
}

a:hover, a:active {
  background-color: red;
}

.center {
  position: absolute;
  background-color: #00bfa5;
  left:  0%;
  right: 0%;
  bottom:0%;
  top: 0%;
  margin:10px;
  padding:10px;
}
              
            
!

JS

              
                
/**
 Original Shader: https://www.shadertoy.com/view/wscBWH
by makio135 https://www.shadertoy.com/user/makio135
 */
BABYLON.Effect.ShadersStore["finalCompFragmentShader"] =
  `varying vec2 vUV; 
        uniform sampler2D textureSampler;
        uniform vec3    iMaterialColor;
        uniform vec2    iResolution;
        uniform float   iTime;
        uniform vec3    cameraPosition;
        uniform vec3    cameraTarget;

      #define AA 1
      #define MIN_DIST 0.001
      #define MAX_DIST 50.

      #define PI 3.141592653589793
      #define TAU 6.283185307179586

      float sdBox(vec3 p, vec3 b) {
          vec3 d = abs(p) - b;
          return length(max(d, 0.0))
              + min(max(d.x, max(d.y, d.z)), 0.0); // remove this line for an only partially signed sdf 
      }

      float sdRoundCone(in vec3 p, in float r1, float r2, float h) {
          vec2 q = vec2(length(p.xz), p.y);

          float b = (r1 - r2) / h;
          float a = sqrt(1.0 - b * b);
          float k = dot(q, vec2(-b, a));

          if(k < 0.0) return length(q) - r1;
          if(k > a * h) return length(q - vec2(0.0, h)) - r2;

          return dot(q, vec2(a, b)) - r1;
      }

      float opUnion(float d1, float d2) {
          return min(d1, d2);
      }

      vec2 opUnion(vec2 d1, vec2 d2) {
          return d1.x < d2.x ? d1 : d2;
      }

      vec3 opSymXYZ(vec3 p) {
          p = abs(p);
          return p;
      }

      float easeInOutQuad(float t) {
          if ((t *= 2.0) < 1.0) {
              return 0.5 * t * t;
          } else {
              return -0.5 * ((t - 1.0) * (t - 3.0) - 1.0);
          }
      }

      // from https://github.com/doxas/twigl
      mat3 rotate3D(float angle, vec3 axis){
          vec3 a = normalize(axis);
          float s = sin(angle);
          float c = cos(angle);
          float r = 1.0 - c;
          return mat3(
              a.x * a.x * r + c,
              a.y * a.x * r + a.z * s,
              a.z * a.x * r - a.y * s,
              a.x * a.y * r - a.z * s,
              a.y * a.y * r + c,
              a.z * a.y * r + a.x * s,
              a.x * a.z * r + a.y * s,
              a.y * a.z * r - a.x * s,
              a.z * a.z * r + c
          );
      }

      void mengerFold(inout vec3 z) {
          float a = min(z.x - z.y, 0.0);
          z.x -= a;
          z.y += a;
          a = min(z.x - z.z, 0.0);
          z.x -= a;
          z.z += a;
          a = min(z.y - z.z, 0.0);
          z.y -= a;
          z.z += a;
      }

      void boxFold(inout vec3 z, vec3 r) {
          z.xyz = clamp(z.xyz, -r, r) * 2.0 - z.xyz;
      }


      float glow = 0.;
      vec2 sceneSDF(vec3 p) {
          float t = iTime * .1;
          t = easeInOutQuad(mod(t, 1.));
          vec2 d = vec2(10e5, 0);

          for(int i = 0; i < 5; i++) {
              p = opSymXYZ(p);
              mengerFold(p);
              boxFold(p, vec3(.5));
              p.x -= .2;
              p *= rotate3D(t * TAU, vec3(1, 1, 0));
              p *= .95;
              p *= rotate3D(-t * TAU, vec3(0, 1, 1));

              float dd = sdRoundCone(p, 1.6, .1, 1.6);
              if(i == 3) glow += 0.006 / (0.01 + dd * dd * 5.) / (float(AA * AA) * 10.);
          }
          d = opUnion(d, vec2(sdBox(p, vec3(.8, .1, .1)), 2.));

          return d;
      }


      // Compute camera-to-world transformation.
      mat3 setCamera(in vec3 ro, in vec3 ta, float cr) {
          vec3 cw = normalize(ta - ro);
          vec3 cp = vec3(sin(cr), cos(cr), 0.0);
          vec3 cu = normalize(cross(cw,cp));
          vec3 cv = normalize(cross(cu,cw));
          return mat3(cu, cv, cw);
      }

      // Cast a ray from origin ro in direction rd until it hits an object.
      // Return (t,m) where t is distance traveled along the ray, and m
      // is the material of the object hit.
      vec2 castRay(in vec3 ro, in vec3 rd) {
          float tmin = MIN_DIST;
          float tmax = MAX_DIST;

          #if 0
          // bounding volume
          float tp1 = (0.0 - ro.y) / rd.y; 
          if(tp1 > 0.0) tmax = min(tmax, tp1);
          float tp2 = (1.6 - ro.y) / rd.y; 
          if(tp2 > 0.0) { 
              if(ro.y > 1.6) tmin = max(tmin, tp2);
              else tmax = min(tmax, tp2 );
          }
          #endif

          float t = tmin;
          float m = -1.0;
          for(int i = 0; i < 100; i++) {
              float precis = 0.0005 * t;
              vec2 res = sceneSDF(ro + rd * t);
              if(res.x < precis || t > tmax) break;
              t += res.x;
              m = res.y;
          }

          if(t > tmax) m =- 1.0;
          return vec2(t, m);
      }


      // Cast a shadow ray from origin ro (an object surface) in direction rd
      // to compute soft shadow in that direction. Returns a lower value
      // (darker shadow) when there is more stuff nearby as we step along the shadow ray.
      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 = sceneSDF(ro + rd * t).x;
              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);
      }

      // Compute normal vector to surface at pos, using central differences method?
      vec3 calcNormal(in vec3 pos) {
          // epsilon = a small number
          vec2 e = vec2(1.0, -1.0) * 0.5773 * 0.0005;

          return normalize(
              e.xyy * sceneSDF(pos + e.xyy).x + 
              e.yyx * sceneSDF(pos + e.yyx).x + 
              e.yxy * sceneSDF(pos + e.yxy).x + 
              e.xxx * sceneSDF(pos + e.xxx).x
          );
      }

      // compute ambient occlusion value at given position/normal
      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 = sceneSDF(aopos).x;
              occ += -(dd - hr) * sca;
              sca *= 0.95;
          }
          return clamp(1.0 - 3.0 * occ, 0.0, 1.0);
      }


      vec3 computeColor(vec3 ro, vec3 rd, vec3 pos, float d, float m) {
          vec3 nor = calcNormal(pos);
          vec3 ref = reflect(rd, nor); // reflected ray

          // material
          vec3 col = vec3(iMaterialColor.x,iMaterialColor.y,iMaterialColor.z)/255.;

          // lighting        
          float occ = calcAO(pos, nor); // ambient occlusion
          vec3 lig = normalize(vec3(-0.4, 0.7, -0.6)); // sunlight
          float amb = clamp(0.5 + 0.5 * nor.y, 0.0, 1.0); // ambient light
          float dif = clamp(dot(nor, lig), 0.0, 1.0); // diffuse reflection from sunlight
          // backlight
          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); // dome light
          float fre = pow(clamp(1.0 + dot(nor, rd), 0.0, 1.0), 2.0); // fresnel
          float spe = pow(clamp(dot(ref, lig), 0.0, 1.0), 16.0); // specular reflection

          dif *= softshadow(pos, lig, 0.02, 2.5);
          dom *= softshadow(pos, ref, 0.02, 2.5);

          vec3 lin = vec3(0.0);
          lin += 1.30 * dif * vec3(1.00, 0.80, 0.55);
          lin += 2.00 * spe * vec3(1.00, 0.90, 0.70) * dif;
          lin += 0.40 * amb * vec3(0.40, 0.60, 1.00) * occ;
          lin += 0.50 * dom * vec3(0.40, 0.60, 1.00) * occ;
          lin += 0.50 * bac * vec3(0.25, 0.25, 0.25) * occ;
          lin += 0.25 * fre * vec3(1.00, 1.00, 1.00) * occ;
          col = col * lin;

          return col;
      }


      // Figure out color value when casting ray from origin ro in direction rd.
      vec3 render(in vec3 ro, in vec3 rd) { 
          // cast ray to nearest object
          vec2 res = castRay(ro, rd);
          float distance = res.x; // distance
          float materialID = res.y; // material ID

          vec3 col = vec3(0.6 - length((gl_FragCoord.xy - iResolution.xy / 2.) / iResolution.x));;
              if(materialID > 0.0) {
                  vec3 pos = ro + distance * rd;
                  col = computeColor(ro, rd, pos, distance, materialID);
              }
          return vec3(clamp(col, 0.0, 1.0));
      }
      void init() {}

      vec3 effect(vec3 c) {
          c += glow * vec3(242, 223, 126)/255.;
          return c;
      }


      void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
          // Ray Origin)\t
          vec3 ro = vec3(-5, 2.5, -6) * 2.2 * rotate3D(iTime * .05 * TAU, vec3(0, 1, 0));
          vec3 ta = vec3(0.0);
          // camera-to-world transformation
          mat3 ca = setCamera(ro, ta, 0.0);

          vec3 color = vec3(0.0);

          #if AA>1
          for(int m = 0; m < AA; m++)
              for(int n = 0; n < AA; n++) {
                  // pixel coordinates
                  vec2 o = vec2(float(m), float(n)) / float(AA) - 0.5;
                  vec2 p = (-iResolution.xy + 2.0 * (fragCoord.xy + o)) / iResolution.y;
                  #else
                  vec2 p = (-iResolution.xy + 2.0 * fragCoord.xy) / iResolution.y;
                  #endif

                  // ray direction
                  vec3 rd = ca * normalize(vec3(p.xy, 2.0));

                  // render\t
                  vec3 col = render(ro, rd);

                  color += col;
                  #if AA>1
              }
          color /= float(AA*AA);
          #endif

          color = effect(color);

          fragColor = vec4(color, 1.0);
      }
      void main() 
      {
          mainImage(gl_FragColor, vUV * iResolution.xy);
      }`

  var canvas = document.getElementById("renderCanvas");
 var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true }); 

 var scene = new BABYLON.Scene(engine);
 var camera = new BABYLON.ArcRotateCamera("Camera", -Math.PI / 2, Math.PI / 2, 12, BABYLON.Vector3.Zero(), scene);
        	
camera.attachControl(canvas, true);

var colorVector = new BABYLON.Vector3(Math.random(), Math.random(), Math.random())

var postEffect = new BABYLON.PostProcess("finalComp", "finalComp",
        ["iResolution", 
         "iTime", 
         "cameraPosition", 
         "cameraTarget"],
        ["iMaterialColor"],
1.0, camera);

postEffect.onApply = (effect) => {
        effect.setVector2('iResolution', new BABYLON.Vector2(postEffect.width, postEffect.height));
        effect.setFloat('iTime', time)
        effect.setVector3('cameraPosition', scene.activeCamera.position)
        effect.setVector3('cameraTarget', scene.activeCamera.getForwardRay(1).direction)
       effect.setVector3('iMaterialColor', colorVector)
    }

    var time = 0.0
    var rate = 0.01;
scene.registerBeforeRender( ()=> {
        time += scene.getAnimationRatio() * rate;
        //rate+=0.001*Math.sin(time)
        colorVector = new BABYLON.Vector3(
          Math.abs(Math.cos(time) * 255), 
          Math.abs(Math.sin(time) * 255), 
          Math.cos(time) * 255)
    });

engine.runRenderLoop(function () {
  if (scene && scene.activeCamera) {
    scene.render();
  }
});

// Resize
window.addEventListener("resize", function () {
  engine.resize();
});
              
            
!
999px

Console