Jump to content

Recommended Posts

Hi guys,


I wanted to share with you a custom library I made, that was very helpful on my project. It implements the basic constructive operations on meshes, like union, intersection, subtraction etc. In 3 words, it's called "constructive solid geometry". The library is an adaptation from csg.js (http://evanw.github.com/csg.js) for BabylonJS meshes.




You will find this lib useful if you meet the need to dynamically build your own geometry with basic primitives. For instance you can carve cylindric holes in bigger cylinders to create pipes (wink wink to all old-school Half-Life mappers down there :)).


Anyhow, you can find the repository over here : https://github.com/CraigFeldspar/BabylonCSG.

I'm still testing it, so feel free to signal any bug you would find..


Cheers !


Link to comment
Share on other sites

Thanks for your feedback !


@davrous : No demo atm, but i'm gonna set up a jsFiddle with some tweak possibilities when i get a little free time.


@deltakosh : My pleasure :) I wasn't sure if you wanted this module to be part of Babylon, that's the reason why I created a repo. I'm still working on making it work with MultiMaterials though, the PR will follow just right after.


@AndyBeaulieu : I did not think about that for now, but I guess if you apply the exact same operations to Cannon.js solid primitives, you could get some decent result. Try to convert Cannon.js objects to CSG polygons and see what happens ! Looking forward to it :)

Link to comment
Share on other sites



I've just integrated your great code. My next ask would be: Could you create a small demo like this ones that showcases features of CSG ?

var CreateFogScene = function(engine) {    var scene = new BABYLON.Scene(engine);    var camera = new BABYLON.FreeCamera("Camera", new BABYLON.Vector3(0, 0, -20), scene);    var light = new BABYLON.PointLight("Omni", new BABYLON.Vector3(20, 100, 2), scene);    var sphere0 = BABYLON.Mesh.CreateSphere("Sphere0", 16, 3, scene);    var sphere1 = BABYLON.Mesh.CreateSphere("Sphere1", 16, 3, scene);    var sphere2 = BABYLON.Mesh.CreateSphere("Sphere2", 16, 3, scene);    var material0 = new BABYLON.StandardMaterial("mat0", scene);    material0.diffuseColor = new BABYLON.Color3(1, 0, 0);    sphere0.material = material0;    sphere0.position = new BABYLON.Vector3(-10, 0, 0);    var material1 = new BABYLON.StandardMaterial("mat1", scene);    material1.diffuseColor = new BABYLON.Color3(1, 1, 0);    sphere1.material = material1;    var material2 = new BABYLON.StandardMaterial("mat2", scene);    material2.diffuseColor = new BABYLON.Color3(1, 0, 1);    sphere2.material = material2;    sphere2.position = new BABYLON.Vector3(10, 0, 0);    sphere1.convertToFlatShadedMesh();        camera.setTarget(new BABYLON.Vector3(0, 0, 0));        // Fog    scene.fogMode = BABYLON.Scene.FOGMODE_EXP;    scene.fogDensity = 0.1;    // Animations    var alpha = 0;    scene.registerBeforeRender(function () {        sphere0.position.z = 4 * Math.cos(alpha);        sphere1.position.z = 4 * Math.sin(alpha);        sphere2.position.z = 4 * Math.cos(alpha);        alpha += 0.1;    });    return scene;};
Link to comment
Share on other sites

  • 6 months later...
  • 1 year later...
  • 2 years later...
59 minutes ago, BartW said:

This is awesome! I guess when you want to animate an object intersecting another object, the mesh needs to be regenerated each frame?

Not necessarily, though it does depend on what you wish to achieve. If you want one mesh to move through another creating a hole in the mesh passed through then as in the following playground  you can create the  before and after meshes and swap them at the appropriate time https://www.babylonjs-playground.com/#FPIYDA

Link to comment
Share on other sites

So you need dynamic subtraction of multiple meshes. Have you tried CSG with invisible meshes? That way you might get way with just disposing of and re-creating the CSG and mesh from CSG each frame. By the way personally I would ask a new question rather than adding to this topic which is a really a demo thread. I would show the video and ask the question anew. Up to you of course. ?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...