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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
    {
		"imports":
        {
            "three": "https://unpkg.com/three@0.138.0/build/three.module",
            "three/examples/jsm/": "https://unpkg.com/three@0.138.0/examples/jsm/"
		}
	}
</script>
              
            
!

CSS

              
                body,
canvas {
    margin: 0;
    padding: 0;
}

              
            
!

JS

              
                /*
 * This codepen sample is mainly to demonstrate the transcoding from ETC1s to DXT takes noticeably longer than other formats
 * See the printed info in console to find out which formats are supported
 */

import * as THREE from "three";
import { KTX2Loader } from "three/examples/jsm/loaders/KTX2Loader";

const formatStrings = {
    [THREE.RGBAFormat]: "RGBA32",
    [THREE.RGBA_BPTC_Format]: "RGBA_BPTC",
    [THREE.RGBA_ASTC_4x4_Format]: "RGBA_ASTC_4x4",
    [THREE.RGB_S3TC_DXT1_Format]: "RGB_S3TC_DXT1",
    [THREE.RGBA_S3TC_DXT5_Format]: "RGBA_S3TC_DXT5",
    [THREE.RGB_PVRTC_4BPPV1_Format]: "RGB_PVRTC_4BPPV1",
    [THREE.RGBA_PVRTC_4BPPV1_Format]: "RGBA_PVRTC_4BPPV1",
    [THREE.RGB_ETC1_Format]: "RGB_ETC1",
    [THREE.RGB_ETC2_Format]: "RGB_ETC2",
    [THREE.RGBA_ETC2_EAC_Format]: "RGB_ETC2_EAC"
};

const ktxURL = "https://avseoul.net/test.ktx2";
const jpgURL = "https://avseoul.net/test.jpg";
const textureLoader = new THREE.TextureLoader();
const ktx2Loader = new KTX2Loader();
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-0.5, 0.5, 0.5, -0.5, 1, 10);
camera.position.z = 5;

const init = async function () {
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.setPixelRatio(window.devicePixelRatio);
    document.body.appendChild(renderer.domElement);

    ktx2Loader
        .setTranscoderPath(
            "https://unpkg.com/three@0.138.0/examples/js/libs/basis/"
        )
        .detectSupport(renderer);

    // Print out which compression formats that your platform supports
    console.log(ktx2Loader.workerConfig);

    // Init transcoder upfront
    await ktx2Loader.init();

    // Start to load jpg
    // *Noted that the actual downloading of image happens only once for the first loading since it is cached by the loader. Although gl texture creation is per textures
    const jpgLoadStart = performance.now();
    {
        const tasks = [];
        for (let i = 0; i < 10; i++) {
            // Promises are handling by Promise.all later for parallel processings, threejs loaders support multi threading by web workers
            const task = textureLoader.loadAsync(jpgURL);
            tasks.push(task);
        }

        await Promise.all(tasks).then((textures) => {
            textures.forEach((texture) => {
                addTexture(texture);
            });
        });
    }
    const jpgLoadTime = Math.trunc(performance.now() - jpgLoadStart);
    console.log(`jpg loaded: ${jpgLoadTime}ms`);

    // Compile jpg scene, to compare uploading time
    const jpgCompileStart = performance.now();
    renderer.render(scene, camera);
    const jpgCompileTime = Math.trunc(performance.now() - jpgCompileStart);
    console.log(`jpg uploaded: ${jpgCompileTime}ms`);

    // Start to load ktx2
    // *Noted that the actual downloading of image happens only once for the first loading since it is cached by the loader. Although gl texture creation is per texture.
    // *Also, unlike .jpg, transcoding from ETC1s to any supported compression format by the plaform, is per texture as well.
    const ktxLoadStart = performance.now();
    let format;
    {
        const tasks = [];
        for (let i = 0; i < 10; i++) {
            // Promises are handling by Promise.all later for parallel processings, threejs loaders support multi threading by web workers
            const task = ktx2Loader.loadAsync(ktxURL);
            tasks.push(task);
        }

        await Promise.all(tasks).then((textures) => {
            textures.forEach((texture) => {
                addTexture(texture);
                format = texture.format;
            });
        });
    }
    const ktxLoadTime = Math.trunc(performance.now() - ktxLoadStart);
    console.log(`ktx2 loaded: ${ktxLoadTime}ms`);

    // Compile ktx scene, to compare uploading time
    const ktxCompileStart = performance.now();
    renderer.render(scene, camera);
    const ktxCompileTime = Math.trunc(performance.now() - ktxCompileStart);
    console.log(`ktx2 uploaded: ${ktxCompileTime}ms`);

    // Print out which compression format that ETC1s is trancoded to
    console.info(`ETC1S transcoded to: ${formatStrings[format]}`);

    ktx2Loader.dispose();
};

let ti = 0;
const addTexture = function (texture) {
    const sprite = new THREE.Sprite(
        new THREE.SpriteMaterial({
            map: texture,
            depthWrite: false,
            depthTest: false,
            transparent: false,
            depthFunc: THREE.NeverDepth
        })
    );
    const numItemsInRow = 5;
    const scale = 1 / numItemsInRow;
    const xId = ti % numItemsInRow;
    const yId = Math.floor(ti / numItemsInRow);
    const halfScale = scale * 0.5;
    const x = -scale * xId + 0.5 - halfScale;
    const y = -0.5 + halfScale + yId * scale;
    sprite.position.set(x, y, 0);
    sprite.scale.set(scale, scale, scale);

    scene.add(sprite);

    ti++;
};

init();

              
            
!
999px

Console