How to Create a rorating .glb sculpture in a webGL metaverse envoronment using the three.js library.

 Creating a rotating .glb sculpture using Three.js in a webGL metaverse environment involves several steps. First, make sure you have included the Three.js library in your HTML file. You can use the following script as a starting point:


```html

<!DOCTYPE html>

<html lang="en">

<head>

  <meta charset="UTF-8">

  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <title>Three.js Sculpture</title>

  <style>

    body { margin: 0; }

    canvas { display: block; }

  </style>

</head>

<body>

  <script type="module">

    import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r125/build/three.module.js';

    import { GLTFLoader } from 'https://threejsfundamentals.org/threejs/resources/threejs/r125/examples/jsm/loaders/GLTFLoader.js';


    let camera, scene, renderer;

    let sculpture;


    init();

    animate();


    function init() {

      // Create a scene

      scene = new THREE.Scene();


      // Create a camera

      camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

      camera.position.z = 5;


      // Create a renderer

      renderer = new THREE.WebGLRenderer();

      renderer.setSize(window.innerWidth, window.innerHeight);

      document.body.appendChild(renderer.domElement);


      // Load the .glb model

      const loader = new GLTFLoader();

      loader.load('path/to/your/sculpture.glb', (gltf) => {

        sculpture = gltf.scene;

        scene.add(sculpture);

      });


      // Add ambient light

      const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);

      scene.add(ambientLight);


      // Add directional light

      const directionalLight = new THREE.DirectionalLight(0xffffff, 1);

      directionalLight.position.set(1, 1, 1).normalize();

      scene.add(directionalLight);

    }


    function animate() {

      requestAnimationFrame(animate);


      // Rotate the sculpture

      if (sculpture) {

        sculpture.rotation.x += 0.01;

        sculpture.rotation.y += 0.01;

      }


      // Render the scene

      renderer.render(scene, camera);

    }


    // Handle window resize

    window.addEventListener('resize', () => {

      const newWidth = window.innerWidth;

      const newHeight = window.innerHeight;


      camera.aspect = newWidth / newHeight;

      camera.updateProjectionMatrix();


      renderer.setSize(newWidth, newHeight);

    });

  </script>

</body>

</html>

```


This script creates a basic Three.js scene, loads a .glb model, adds lights, and sets up a simple animation to rotate the sculpture. Replace the `'path/to/your/sculpture.glb'` with the actual path to your .glb file.


Remember to include the Three.js library and GLTFLoader from a CDN or your local installation. Adjust the camera position, lighting, and other parameters based on your preferences and the specifics of your model.


Comments

Popular posts from this blog

Creating a GLB sculpture in Three.js and embedding it into a WebGL metaverse environment

JavaScript PROJECT NOTES