Skip to content
TheChatArchiveThe Chat Archive
o1-preview-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

can you code a 3JS page, that uses the ascii affect, exactly like this one…

  1. User

    can you code a 3JS page, that uses the ascii affect, exactly like this one https://threejs.org/examples/webgl_effects_ascii.html

  2. Assistant

    Certainly! Below is the complete code for a Three.js page that implements the ASCII effect, similar to the example you provided. This code creates a spinning 3D torus knot rendered as ASCII characters on the web page.

    HTML File (index.html):

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Three.js ASCII Effect Example</title>
        <style>
            body {
                margin: 0;
                background-color: black;
                color: white;
                font-family: monospace;
                overflow: hidden;
            }
            pre {
                line-height: 7px;
                letter-spacing: 1.5px;
                user-select: none;
            }
        </style>
    </head>
    <body>
    
    <script type="module">
    
        import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.155.0/build/three.module.js';
        import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.155.0/examples/jsm/controls/OrbitControls.js';
        import { AsciiEffect } from 'https://cdn.jsdelivr.net/npm/three@0.155.0/examples/jsm/effects/AsciiEffect.js';
    
        let camera, scene, renderer, effect;
        let mesh;
    
        init();
        animate();
    
        function init() {
    
            // Renderer
            renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);
    
            // Camera
            camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 1000);
            camera.position.z = 100;
    
            // Scene
            scene = new THREE.Scene();
    
            // Geometry and Material
            const geometry = new THREE.TorusKnotGeometry(20, 5, 100, 16);
            const material = new THREE.MeshNormalMaterial();
    
            // Mesh
            mesh = new THREE.Mesh(geometry, material);
            scene.add(mesh);
    
            // Effect
            effect = new AsciiEffect(renderer, ' .:-=+*#%@', { invert: true });
            effect.setSize(window.innerWidth, window.innerHeight);
            effect.domElement.style.color = 'white';
            effect.domElement.style.backgroundColor = 'black';
    
            // Append effect's DOM element to body
            document.body.appendChild(effect.domElement);
    
            // Controls
            const controls = new OrbitControls(camera, effect.domElement);
            controls.enablePan = false;
            controls.enableZoom = false;
    
            // Window resize handler
            window.addEventListener('resize', onWindowResize);
    
        }
    
        function onWindowResize() {
    
            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();
    
            renderer.setSize(window.innerWidth, window.innerHeight);
            effect.setSize(window.innerWidth, window.innerHeight);
    
        }
    
        function animate() {
    
            requestAnimationFrame(animate);
    
            // Rotate the mesh
            mesh.rotation.x += 0.01;
            mesh.rotation.y += 0.01;
    
            // Render the scene using the ASCII effect
            effect.render(scene, camera);
    
        }
    
    </script>
    
    </body>
    </html>
    

    Instructions:

    1. Create a new directory on your computer to store the project files.

    2. Save the code above into a file named index.html within that directory.

    3. Run the project:

      • Open index.html in a modern web browser that supports ES6 modules (e.g., Chrome, Firefox).

      • Alternatively, for the best experience, you might want to use a local web server due to module import restrictions. You can run a simple local server using Python:

        • Python 3:
          python -m http.server 8000
          
        • Python 2:
          python -m SimpleHTTPServer 8000
          
        • Then, navigate to http://localhost:8000 in your browser.

    Explanation:

    • Importing Modules:

      The script uses ES6 module imports to include Three.js, OrbitControls, and AsciiEffect directly from a CDN (Content Delivery Network).

      import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.155.0/build/three.module.js';
      import { OrbitControls } from 'https://cdn.jsdelivr.net/npm/three@0.155.0/examples/jsm/controls/OrbitControls.js';
      import { AsciiEffect } from 'https://cdn.jsdelivr.net/npm/three@0.155.0/examples/jsm/effects/AsciiEffect.js';
      
    • Renderer and Effect:

      • A standard WebGLRenderer is created.
      • The AsciiEffect is instantiated with custom characters for rendering and set to invert the brightness for better visibility.
      effect = new AsciiEffect(renderer, ' .:-=+*#%@', { invert: true });
      
    • Scene Setup:

      • A PerspectiveCamera is positioned along the z-axis.
      • A Scene is created, and a Mesh with a TorusKnotGeometry and MeshNormalMaterial is added.
      • The mesh rotates over time to create an animation.
    • Controls:

      The OrbitControls allow for interactive rotation of the camera using mouse movement.

    • Animation Loop:

      The animate function updates the rotation of the mesh and renders the scene using the ASCII effect.

    Styling:

    • The <style> section defines the look of the ASCII art, setting the background to black and the text to white.
    • The pre tag styling adjusts line height and letter spacing for optimal appearance of the ASCII characters.

    Notes:

    • Character Set:

      The set of characters ' .:-=+*#%@' defines the ASCII characters used to represent different brightness levels.

    • Inversion:

      The { invert: true } option in AsciiEffect flips the brightness levels, which can improve visibility depending on the background and text colors.

    • Dependencies:

      Since we are importing modules directly from a CDN, there's no need to install Three.js or other dependencies manually.

    Troubleshooting:

    • Module Support:

      Ensure your browser supports ES6 modules. If you encounter issues, try updating your browser to the latest version.

    • Local File Restrictions:

      Some browsers have restrictions when opening local files with module imports. If you face issues, use a local web server as mentioned above.

    Customization:

    • Geometry:

      You can replace TorusKnotGeometry with other geometries like BoxGeometry, SphereGeometry, etc.

    • Materials:

      Experiment with different materials for varied visual effects.

    • Character Set:

      Modify the string in AsciiEffect to use different characters.

    References:

    Feel free to ask if you need further assistance or have questions about customizing the effect!