Interactive 3D Web Experience Tutorial

Creating an Interactive 3D Web Experience with Three.js and Tweakpane

Welcome to this exciting tutorial, where we'll take you through the step-by-step process of creating an advanced, interactive 3D scene using Three.js and Tweakpane. By the end of this guide, you'll have built a sophisticated 3D experience that includes a highly detailed model, realistic lighting, and dynamic user controls.

Step 1: Setting Up the Basic HTML Structure

To start, we need a basic HTML skeleton. This structure provides the foundation for our 3D scene and will include the necessary libraries, CSS styles, and HTML elements.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Advanced 3D Model Interaction</title>
    <!-- Including external libraries -->
    <script src="https://cdn.jsdelivr.net/npm/tweakpane@3.0.0"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/controls/OrbitControls.js"></script>
    <style>
        body { margin: 0; display: flex; }
        canvas { display: block; width: 100%; height: 100vh; }
        #pane { position: absolute; top: 10px; left: 10px; z-index: 10; }
    </style>
</head>
<body>
    <div id="pane"></div>
    <script>
        // We will add the JavaScript code here later
    </script>
</body>
</html>

Step 2: Setting Up the Scene, Camera, and Renderer

Next, we’ll initialize the core components of a 3D scene: the scene itself, the camera, and the renderer.

<script>
    // Set up the scene
    const scene = new THREE.Scene();
    const camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 1000);
    const renderer = new THREE.WebGLRenderer({ antialias: true });
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);
</script>

Step 3: Adding Orbit Controls for Interactive Camera Movement

To allow users to explore the 3D scene interactively, we introduce orbit controls. This enables camera rotation, panning, and zooming.

<script>
    const controls = new THREE.OrbitControls(camera, renderer.domElement);
    controls.enableDamping = true;
    controls.dampingFactor = 0.25;
    controls.enableZoom = true;
</script>

Step 4: Adding an Environment Map for Realistic Reflections

To make the 3D scene more immersive, we add an environment map. This texture will simulate reflections on the model, making it appear more lifelike.

<script>
    const loader = new THREE.CubeTextureLoader();
    const texture = loader.load([
        'https://threejs.org/examples/textures/cube/Park3Med/px.jpg',
        'https://threejs.org/examples/textures/cube/Park3Med/nx.jpg',
        'https://threejs.org/examples/textures/cube/Park3Med/py.jpg',
        'https://threejs.org/examples/textures/cube/Park3Med/ny.jpg',
        'https://threejs.org/examples/textures/cube/Park3Med/pz.jpg',
        'https://threejs.org/examples/textures/cube/Park3Med/nz.jpg'
    ]);
    scene.background = texture;
</script>

Step 5: Adding Lights to Illuminate the Scene

Lighting is crucial for any 3D scene, as it adds depth and realism. We’ll add both ambient and point lighting.

<script>
    const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
    scene.add(ambientLight);

    const pointLight = new THREE.PointLight(0xffffff, 1);
    pointLight.position.set(10, 10, 10);
    scene.add(pointLight);
</script>

Step 6: Loading a 3D Model Using GLTFLoader

To give our scene something to showcase, we’ll load a 3D model. For this example, we’re using a model of a damaged helmet.

<script>
    const gltfLoader = new THREE.GLTFLoader();
    let model;
    gltfLoader.load('https://threejs.org/examples/models/gltf/DamagedHelmet/glTF/DamagedHelmet.gltf', function(gltf) {
        model = gltf.scene;
        model.scale.set(2, 2, 2);
        scene.add(model);
    });
</script>

Step 7: Adding Dynamic Controls Using Tweakpane

To enhance interactivity, we’ll integrate Tweakpane. This tool allows us to create a control panel where users can modify parameters like rotation speed, light intensity, and more.

<script>
    const PARAMS = {
        rotationX: 0.01,
        rotationY: 0.01,
        lightIntensity: 1,
        lightColor: '#ffffff',
        modelScale: 2
    };

    const pane = new Tweakpane.Pane({ container: document.getElementById('pane') });
    pane.addInput(PARAMS, 'rotationX', { min: 0, max: 0.1 });
    pane.addInput(PARAMS, 'rotationY', { min: 0, max: 0.1 });
    pane.addInput(PARAMS, 'lightIntensity', { min: 0, max: 2 }).on('change', (value) => {
        pointLight.intensity = value;
    });
    pane.addInput(PARAMS, 'lightColor').on('change', (value) => {
        pointLight.color.set(value);
    });
    pane.addInput(PARAMS, 'modelScale', { min: 0.5, max: 5 }).on('change', (value) => {
        if (model) {
            model.scale.set(value, value, value);
        }
    });
</script>

Step 8: Animating the Scene

To bring the scene to life, we’ll create an animation loop that continuously updates the model’s rotation and renders the scene.

<script>
    function animate() {
        requestAnimationFrame(animate);
        if (model) {
            model.rotation.x += PARAMS.rotationX;
            model.rotation.y += PARAMS.rotationY;
        }
        controls.update();
        renderer.render(scene, camera);
    }

    animate();
</script>

Step 9: Handling Window Resizing

To ensure the scene remains correctly proportioned when the browser window is resized, we add a resize event listener.

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

Final Thoughts

Congratulations! You've just built a highly interactive 3D experience with advanced controls, realistic lighting, and an environment map, all wrapped up in a beautifully responsive design. Whether you’re showcasing products, creating immersive experiences, or just having fun with 3D graphics, this setup provides a powerful foundation for building more complex and engaging scenes.

By following this guide, you’ve not only learned how to use Three.js and Tweakpane but also how to structure your code effectively and handle user interaction smoothly. Continue exploring the vast possibilities of 3D web graphics, and let your creativity shine!

Enjoy coding and happy building! 🖥️👾

Advanced 3D Web Experience Tutorial by Liber8 🥷💻 | © 2024

Comments

Popular posts from this blog