Drawing custom shapes in Babylon.js

1.3k views Asked by At

I have been searching all over the web for a way to draw custom 3d shapes in babylon.js. I would be grateful if somebody could provide a working example. For instance, a 3d irregular pentagon, triangle fan, or a wedge.

2

There are 2 answers

0
David Catuhe On BEST ANSWER

you can find a lot of info about parametric shapes in Babylon.js here: http://doc.babylonjs.com/page.php?p=24847

And mainly for the ribbon here: http://doc.babylonjs.com/page.php?p=25088

0
Dr. Aaron Dishno On

Here is a wedge using the ribbon object:

var createScene = function() {

var scene = new BABYLON.Scene(engine);
scene.clearColor = new BABYLON.Color3(0.8, 0.8, 0.8);
var camera = new BABYLON.ArcRotateCamera("Camera", 3 *Math.PI / 2, Math.PI / 2, 20, BABYLON.Vector3.Zero(), scene);
camera.attachControl(canvas, false);

// lights
var light = new BABYLON.HemisphericLight("hemi", new BABYLON.Vector3(0, 1, 0), scene);
light.groundColor = new BABYLON.Color3(0.2, 0.2, 0.5);
light.intensity = 0.6;


var light2 = new BABYLON.PointLight("light2", new BABYLON.Vector3(-20, 0, -20), scene);
light2.diffuse = BABYLON.Color3.White();
light2.specular = BABYLON.Color3.Green();
light2.intensity = 0.6;

// material
var mat = new BABYLON.StandardMaterial("mat1", scene);
mat.alpha = 1.0;
mat.diffuseColor = new BABYLON.Color3(0.5, 0.5, 1.0);
//mat.backFaceCulling = false;
mat.wireframe = true;

 // tubular ribbon
 path1 = [];
 path2 = [];

  path1.push( new BABYLON.Vector3(0, 0, 0) );
  path2.push( new BABYLON.Vector3(0, 2, 0) );
  path1.push( new BABYLON.Vector3(1, 0, 0) );
  path2.push( new BABYLON.Vector3(1, 2, 0) );
  path1.push( new BABYLON.Vector3(0, 0, 1) );
  path2.push( new BABYLON.Vector3(0, 2, 1) );


var ribbon = BABYLON.Mesh.CreateRibbon("ribbon", [path1, path2], false, true, 0, scene);
ribbon.material = mat;

scene.registerBeforeRender(function(){
    light2.position = camera.position;
});
return scene;};