Skip to content

Instantly share code, notes, and snippets.

@shopglobal
Created September 27, 2024 17:03
Show Gist options
  • Save shopglobal/fab7d7b632f489c35475790310e3ddd1 to your computer and use it in GitHub Desktop.
Save shopglobal/fab7d7b632f489c35475790310e3ddd1 to your computer and use it in GitHub Desktop.
Three.js neon sphere

To create a neon sphere using Three.js, you can follow these steps:

  1. Setup Three.js environment: Make sure you have included Three.js in your project. You can do this by adding the Three.js script in your HTML or installing it via npm if using a module bundler.

  2. Create a basic scene: We'll set up a scene, camera, renderer, and add a glowing neon material to a sphere.

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Neon Sphere with Three.js</title>
  <style>
    body { margin: 0; }
    canvas { display: block; }
  </style>
</head>
<body>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
  <script>
    // Setup basic scene, camera, and renderer
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    // Create a sphere
    const geometry = new THREE.SphereGeometry(2, 32, 32);

    // Create neon material using a shader or emissive material
    const neonMaterial = new THREE.MeshBasicMaterial({
      color: 0x00ffff, // Neon cyan color
      emissive: 0x00ffff, // Make it glow
      emissiveIntensity: 1,
      wireframe: true, // To give it a cool neon effect
    });

    const sphere = new THREE.Mesh(geometry, neonMaterial);
    scene.add(sphere);

    camera.position.z = 5;

    // Add a subtle animation
    function animate() {
      requestAnimationFrame(animate);
      sphere.rotation.x += 0.01;
      sphere.rotation.y += 0.01;
      renderer.render(scene, camera);
    }

    animate();

    // Handle window resizing
    window.addEventListener('resize', function() {
      renderer.setSize(window.innerWidth, window.innerHeight);
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
    });
  </script>
</body>
</html>

Key Features:

  • Wireframe Sphere: The wireframe mode gives the sphere a neon look.
  • Emissive Material: The emissive property in MeshBasicMaterial simulates a glow effect.
  • Rotation Animation: The sphere rotates along the x and y axes for a dynamic effect.

You can customize the neon color and tweak the glow intensity by adjusting the emissive and emissiveIntensity properties.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment