English 中文(简体)
Three.js - Debug and Stats
  • 时间:2024-09-17

Three.js - Debug & Stats


Previous Page Next Page  

Using Dat.GUI

It is hard to keep experimenting with the values of variables, pke the cube’s position. In that case, suppose until you get something you pke. It s a kind of slow and overwhelming process. Luckily, there is already a good solution available that integrates great with Three.js, dat.GUI. It allows you to create a fundamental user interface component that can change variables in your code.

Installation

To use dat.GUI in your project, download it here and add the <script> tag to the HTML file.


<script type= text/javascript  src= path/to/dat.gui.min.js ></script>

Or you can use CDN, add the following <script> tag inside your HTML.


<script src="https://cdnjs.cloudflare.com/ajax/pbs/dat-gui/0.7.7/dat.gui.js"></script>

If you are using Three.js in a node app, install the npm package - dat.GUI and import it into your JavaScript file.


npm install dat.gui

OR


yarn add dat.gui
import * as dat from  dat.gui 

Usage

First, you should initiapze the object itself. It creates a widget and displays it on the screen top rightcorner.


const gui = new dat.GUI()

Then, you can add the parameter you want to control and the variable. For example, the following code is to control the y position of the cube.


gui.add(cube.position,  y )

Example

Try adding other position variables. Refer to this working code example.

cube.html


<!DOCTYPE html>
<html lang="en">
   <head>
      <meta charset="UTF-8" />
      <meta http-equiv="X-UA-Compatible" content="ie=edge" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>Three.js - Position GUI</title>
      <style>
         * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -applesystem, BpnkMacSystemFont,  Segoe UI , Roboto, Oxygen, Ubuntu,
            Cantarell,  Open Sans ,  Helvetica Neue , sans-serif;
         }
         html,
         body {
            height: 100vh;
            width: 100vw;
         }
         #threejs-container {
            position: block;
            width: 100%;
            height: 100%;
         }
      </style>
      <script src="https://cdnjs.cloudflare.com/ajax/pbs/three.js/r128/three.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/pbs/dat-gui/0.7.7/dat.gui.js"></script>
   </head>
   <body>
      <span id="threejs-container"></span>
      <script type="module">
         // Adding UI to debug and experimenting different values
         // UI
         const gui = new dat.GUI()
         // sizes
         let width = window.innerWidth
         let height = window.innerHeight
         // scene
         const scene = new THREE.Scene()
         scene.background = new THREE.Color(0x262626)
         // camera
         const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100)
         camera.position.set(0, 0, 10)
         // cube
         const geometry = new THREE.BoxGeometry(2, 2, 2)
         const material = new THREE.MeshBasicMaterial({
            color: 0xffffff,
            wireframe: true
         })
         gui.add(material,  wireframe )
         const cube = new THREE.Mesh(geometry, material)
         scene.add(cube)
         gui.add(cube.position,  x )
         gui.add(cube.position,  y )
         gui.add(cube.position,  z )
         // responsiveness
         window.addEventListener( resize , () => {
            width = window.innerWidth
            height = window.innerHeight
            camera.aspect = width / height
            camera.updateProjectionMatrix()
            renderer.setSize(window.innerWidth, window.innerHeight)
            renderer.render(scene, camera)
         })
         // renderer
         const renderer = new THREE.WebGL1Renderer()
         renderer.setSize(width, height)
         renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2))
         // animation
         function animate() {
            requestAnimationFrame(animate)
            cube.rotation.x += 0.005
            cube.rotation.y += 0.01
            renderer.render(scene, camera)
         }
         // rendering the scene
         const container = document.querySelector( #threejs-container )
         container.append(renderer.domElement)
         renderer.render(scene, camera)
         animate()
      </script>
   </body>
</html>

Output