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

              
                canvas#elCanvas
#elInfo
  h1 Stencil by #[a(href='//twitter.com/ycwhk') @ycwhk]
  h2 Video by #[a(href='//www.instagram.com/vovaflame') Vova Krasilnikov]
              
            
!

CSS

              
                canvas {
    position:fixed;
    width:100%;
    height:100vh;
    display:block;
}
#elInfo {
    position:fixed;
    z-index:1;
    font:1em/1 monospace;
    padding:2em;
    background:rgba(0,0,0,0.5);
    color:#eee;
    text-shadow:0 0 3px rgba(0,0,0,0.5);
    a { color:inherit; }
    display:grid;
    grid-gap:1em;
}
body {
    background:#000;
}
              
            
!

JS

              
                const engine = new BABYLON.Engine(elCanvas, true, {});
const scene = (() => {

    const scene = new BABYLON.Scene(engine);
    scene.clearColor = new BABYLON.Color4(0, 0, 0, 1);

    const camera0 = new BABYLON.ArcRotateCamera('camera0',
        BABYLON.Tools.ToRadians(-90), BABYLON.Tools.ToRadians(10), 4, null, scene);
    camera0.attachControl(elCanvas);
    camera0.wheelPrecision = 100;

    const light0 = new BABYLON.HemisphericLight('light0', new BABYLON.Vector3(0, 1, -1), scene);
    light0.intensity = 50;


    const SZ_X = 3;
    const SZ_Y = 6;
    const SZ_Z = 3;
    const PTC_W = 0.01;
    const PTC_H = 0.2; 
    const PTC_D = 0.01;
    const VIDEO_RATIO = 640 / 360;
    const VIDEO_URL = 'https://rawcdn.githack.com/ycw/grocery/aa7e3ec2171150cff24bbba511c4499cfac62862/a-medusa-jellyfish-swimming_VovaKrasilnikov.mp4';
    const NB_PARTICLES = 2048;


    // --- video plane --- 
    // always face to camera; draw after rendering particles

    const width = 20;
    const height = width / VIDEO_RATIO;
    const mesh1 = BABYLON.MeshBuilder.CreatePlane('', { width, height });
    mesh1.renderingGroupId = 1;
    const tex1 = new BABYLON.VideoTexture('', VIDEO_URL, scene, true, false);
    tex1.video.muted = true;
    tex1.video.loop = true;
    tex1.video.play();
    const mat1 = new BABYLON.PBRMetallicRoughnessMaterial('', scene);
    mat1.baseTexture = tex1;
    mesh1.material = mat1;
    mesh1.rotation.y = Math.PI;
    mesh1.bakeCurrentTransformIntoVertices();
    mesh1.onBeforeRenderObservable.add(() => {
        mesh1.lookAt(camera0.position);
    });


    // ---- particles ----
    // use instanced (faster init); & similar SPS arch.

    const ps = {};
    const shape = BABYLON.MeshBuilder.CreateBox('', { width: PTC_W, height: PTC_H, depth: PTC_D });
    shape.setEnabled(false);
    ps.particles = [];
    ps.nbParticles = NB_PARTICLES;
    ps.position = new BABYLON.Vector3(0, 0, 0);
    for (let i = 0; i < ps.nbParticles; ++i) {
        const p = shape.createInstance();
        p.renderingGroupId = 0;
        ps.particles.push(p);
    }
    ps.recycleParticle = (p) => {
        const a = Math.random() * Math.PI * 2;
        const r = Math.random() * (SZ_X / 2);
        const x = r * Math.sin(a);
        const y = (SZ_Y / 2 - PTC_H / 2);
        const z = r * Math.cos(a);
        p.position.set(x, y, z);
        p.rotation.y = a;
        p.$vel = new BABYLON.Vector3(
            (2 * (Math.random() - 0.5)) * 0.02, 
            -(0.08 + Math.random() * 0.12), 
            (2 * (Math.random() - 0.5)) * 0.02
        );
    };
    ps.updateParticle = (p) => {
        p.position.addInPlace(ps.position).addInPlace(p.$vel);
        if (p.position.y < -SZ_Y / 2 + PTC_H / 2) {
            ps.recycleParticle(p);
        }
    };
    ps.initParticles = () => {
        for (const p of ps.particles) {
            ps.recycleParticle(p);
        }
    };
    ps.setParticles = () => {
        for (const p of ps.particles) {
            ps.updateParticle(p);
        }
    }
    ps.initParticles();



    // ---- stencil config ---- 
    // must(?) rollback stencil & depth after each rendering group 
    // to make default rendering pipeline work

    engine.setStencilBuffer(true);
    scene.setRenderingAutoClearDepthStencil(0, true);
    scene.setRenderingAutoClearDepthStencil(1, false);
    scene.setRenderingAutoClearDepthStencil(2, false);
    scene.onBeforeRenderingGroupObservable.add((info) => {
        switch (info.renderingGroupId) {
            case 0:
                ps.setParticles();
                engine.setStencilFunction(BABYLON.Engine.ALWAYS);
                engine.setDepthFunction(BABYLON.Engine.LEQUAL);
                engine.setStencilOperationPass(BABYLON.Engine.INCR);
                engine.setDepthWrite(true);
                engine.setStencilMask(0xff);
                break;
            case 1:
                engine.setStencilFunction(BABYLON.Engine.NOTEQUAL);
                engine.setStencilFunctionReference(0);
                engine.setDepthFunction(BABYLON.Engine.ALWAYS);
                engine.setDepthWrite(false);
                engine.setStencilMask(0x00);
                break;
        }
    });

    scene.onAfterRenderingGroupObservable.add(() => {
        engine.setStencilFunction(BABYLON.Engine.ALWAYS);
        //engine.setStencilOperationPass(BABYLON.Engine.KEEP);
        //engine.setStencilFunctionReference(0);
        engine.setDepthFunction(BABYLON.Engine.LEQUAL);
        engine.setDepthWrite(true);
        engine.setStencilMask(0xff);
    })


    // ---- post process -----
    const rp = new BABYLON.DefaultRenderingPipeline('', true, scene, [camera0]);
    rp.bloomEnabled = true;
    rp.bloomScale = 1;
    rp.bloomThreshold = 0.9;
    rp.bloomWeight = 0.3;
    rp.bloomKernel = 32;
    rp.sharpenEnabled = true;

    return scene;
})();
engine.runRenderLoop(() => {
    scene.render();
});
addEventListener('resize', () => {
    engine.resize();
});
              
            
!
999px

Console