Three.js - Ring Geometries



The THREE.RingGeometry creates a D disc with a hole in the center. It is very similar to circle geometry.

  • innerRadius − The inner radius of a circle defines the size of the hole in the center. 0 means no hole. The default value is 0.5.

  • outerRadius − The outer radius of a circle defines its size. The default value is 1.

  • thetaSegments − the number of diagonal segments used to create the circle. The default value is 8. The more segments, the smoother circle is.

  • phiSegments − the number of segments used along the length of the ring. The default value is 8.

  • thetaStart − The position from which to start drawing the circle. This value can range from 0 to 2 * PI, and the default value is 0.

  • thetaLength − This property defines to what extent the circle is completed. The default value is 2 * PI.

const ring = new THREE.RingGeometry(
   innerRadius, outerRadius,
   thetaSegments, phiSegments,
   thetaStart, thetaLength
)

Example

Check out the following example.

ring.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 - Ring</title>
      <style>
         * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: -applesystem, BlinkMacSystemFont, '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/libs/three.js/r128/three.min.js"></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.js"></script>
   </head>
   <body>
   <div id="threejs-container"></div>
      <script type="module">
         // Ring geometry
         // a simple 2d ring in Three.js
         // GUI
         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(30, width / height, 0.1, 100)
         camera.position.set(0, 0, 10)
         const camFolder = gui.addFolder('Camera')
         camFolder.add(camera.position, 'z').min(10).max(60).step(10)
         camFolder.open()
         // Light
         const ambientLight = new THREE.AmbientLight(0xffffff, 1)
         scene.add(ambientLight)
         const pointLight = new THREE.PointLight(0xffffff, 0.2)
         pointLight.position.x = 2
         pointLight.position.y = 3
         pointLight.position.z = 4
         scene.add(pointLight)
         // ring
         const geometry = new THREE.RingGeometry()
         const material = new THREE.MeshBasicMaterial({
            color: 0xffffff,
            wireframe: true,
            side: THREE.DoubleSide
         })
         const materialFolder = gui.addFolder('Material')
         materialFolder.add(material, 'wireframe')
         materialFolder.open()
         const ring = new THREE.Mesh(geometry, material)
         scene.add(ring)
         const ringProps = {
         innerRadius: 1,
            outerRadius: 5,
            thetaSegments: 8,
            phiSegments: 8,
            thetaStart: 0,
            thetaLength: 2 * Math.PI
         }
         const props = gui.addFolder('Properties')
         props
            .add(ringProps, 'innerRadius', 1, 50)
            .step(1)
            .onChange(redraw)
            .onFinishChange(() => console.dir(ring.geometry))
         props.add(ringProps, 'outerRadius', 1, 50).step(1).onChange(redraw)
         props.add(ringProps, 'thetaSegments', 1, 50).step(1).onChange(redraw)
         props.add(ringProps, 'phiSegments', 1, 50).step(1).onChange(redraw)
         props.add(ringProps, 'thetaStart', 0, 2 * Math.PI).onChange(redraw)
         props.add(ringProps, 'thetaLength', 0, 2 * Math.PI).onChange(redraw)
         props.open()
         function redraw() {
            let newGeometry = new THREE.RingGeometry(
               ringProps.innerRadius,
               ringProps.outerRadius,
               ringProps.thetaSegments,
               ringProps.phiSegments,
               ringProps.thetaStart,
               ringProps.thetaLength
            )
            ring.geometry.dispose()
            ring.geometry = newGeometry
         }
         // 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)
            ring.rotation.x += 0.005
            ring.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

threejs_geometries.htm
Advertisements