can you code a 3JS page, that uses the ascii affect, exactly like this one…
- User
can you code a 3JS page, that uses the ascii affect, exactly like this one https://threejs.org/examples/webgl_effects_ascii.html
- 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:
-
Create a new directory on your computer to store the project files.
-
Save the code above into a file named
index.htmlwithin that directory. -
Run the project:
-
Open
index.htmlin 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:8000in your browser.
- Python 3:
-
Explanation:
-
Importing Modules:
The script uses ES6 module imports to include Three.js,
OrbitControls, andAsciiEffectdirectly 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
WebGLRendereris created. - The
AsciiEffectis instantiated with custom characters for rendering and set to invert the brightness for better visibility.
effect = new AsciiEffect(renderer, ' .:-=+*#%@', { invert: true }); - A standard
-
Scene Setup:
- A
PerspectiveCamerais positioned along the z-axis. - A
Sceneis created, and aMeshwith aTorusKnotGeometryandMeshNormalMaterialis added. - The mesh rotates over time to create an animation.
- A
-
Controls:
The
OrbitControlsallow for interactive rotation of the camera using mouse movement. -
Animation Loop:
The
animatefunction 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
pretag 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 inAsciiEffectflips 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
TorusKnotGeometrywith other geometries likeBoxGeometry,SphereGeometry, etc. -
Materials:
Experiment with different materials for varied visual effects.
-
Character Set:
Modify the string in
AsciiEffectto use different characters.
References:
Feel free to ask if you need further assistance or have questions about customizing the effect!
-