BabylonJS - Tube



In this section, we will learn how to create a Tube.

Syntax

Following is the syntax to create a Tube.

var tube = BABYLON.Mesh.CreateTube("tube", [V1, V2, ..., Vn], radius, tesselation, radiusFunction, cap, scene, false, BABYLON.Mesh.DEFAULTSIDE);

Parameters

Consider the following parameters to create a Tube −

  • Name − The name of the tube.

  • Path − The path of the tube.

  • Radius − The radius of the tube.

  • Tessellation − The number of radial segments.

  • RadiusFunction − It is a function which gives radius value. For example, function(i, distance) {}

  • Cap − The tube cap. The values are NO_CAP, CAP_START, CAP_END, CAP_ALL.

  • Scene − The scene to which the tube needs to be attached.

  • Updatable − It is true by default. It is set to false and can be updated later.

  • SideOrientation − The default value is DEFAULTSIDE.

Demo - Tube

<!doctype html>
<html>
   <head>
      <meta charset = "utf-8">
      <title>MDN Games: Babylon.js demo - shapes</title>
      <script src = "babylon.js"></script>
      <style>
         html,body,canvas { margin: 0; padding: 0; width: 100%; height: 100%; font-size: 0; }
      </style>
   </head>

   <body>
      <canvas id = "renderCanvas"></canvas>
      <script type = "text/javascript">
         var canvas = document.getElementById("renderCanvas");
         var engine = new BABYLON.Engine(canvas, true);
         var createScene  = function() {
            var scene = new BABYLON.Scene(engine);
            scene.clearColor = new BABYLON.Color3(0, 1, 0);
            
            var camera = new BABYLON.ArcRotateCamera("Camera", 1, 0.8, 10, new BABYLON.Vector3(0, 0, 0), scene);
            
            var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
            
            var curvePoints =function(l, t) {
               var path = [];
               var step = l / t;
               var a = 5;
               for (var i = -l/4; i < l/4; i += step ) {
                  var t = i  / Math.PI * 4;
                  var x =   Math.sin(t) + i;
                  path.push(new BABYLON.Vector3(x, 0, 0 ));
               }
               return path;
            };

            var curve = curvePoints(10, 50);

            var radiusFunction = function(i, distance) {
               var t = i / Math.PI * 4 / 6;
               var radius =  Math.sin(t) + i / 15;
               return radius;
            }; 
            var tube = BABYLON.Mesh.CreateTube("tube", curve, 2, 30, radiusFunction, 0, scene, false, BABYLON.Mesh.FRONTSIDE);
            scene.activeCamera.attachControl(canvas);
            return scene;
         };
         var scene = createScene();
         engine.runRenderLoop(function() {
            scene.render();
         });
      </script>
   </body>
</html>

Output

Basic Elements Tube
babylonjs_basic_elements.htm
Advertisements