Code Monkey home page Code Monkey logo

babyloncsg's Introduction

BabylonCSG

Constructive Solid Geometry in BABYLON.js, based on csg.js

Constructive Solid Geometry (CSG) is a modeling technique that uses Boolean operations like union and intersection to combine 3D solids. This library implements CSG operations on meshes elegantly and concisely using BSP trees, and is meant to serve as an easily understandable implementation of the algorithm. All edge cases involving overlapping coplanar polygons in both solids are correctly handled.

This library is an adaptation for BABYLON.js meshes, original documentation on csg.js can be found here : https://github.com/evanw/csg.js/

#Example

var a = BABYLON.Mesh.CreateBox("box", 500, scene);
var b = BABYLON.Mesh.CreateBox("box", 500, scene);

a.position.y += 500;
b.position.y += 250;
b.rotation.y += Math.PI/8;

var aCSG = BABYLON.CSG.FromMesh(a);
var bCSG = BABYLON.CSG.FromMesh(b);

var subCSG = bCSG.subtract(aCSG);

// Disposing original meshes since we don't want to see them on the scene
a.dispose();
b.dispose();

subCSG.toMesh("csg", new BABYLON.StandardMaterial("mat", scene), scene);

#Multi-Material You may want to keep one subMesh for each mesh you used CSG on. This library allows you to do so :

var a = BABYLON.Mesh.CreateBox("box", 500, scene);
var b = BABYLON.Mesh.CreateBox("box", 500, scene);

a.position.y += 500;
b.position.y += 250;
b.rotation.y += Math.PI/8;

var aCSG = BABYLON.CSG.FromMesh(a);
var bCSG = BABYLON.CSG.FromMesh(b);

var subCSG = bCSG.subtract(aCSG);

// Disposing original meshes since we don't want to see them on the scene
a.dispose();
b.dispose();

// Set up a MultiMaterial
var multiMat = new BABYLON.MultiMaterial("multiMat", scene);
var mat0 = new BABYLON.StandardMaterial("mat0", scene);
var mat1 = new BABYLON.StandardMaterial("mat1", scene);

mat0.diffuseColor.copyFromFloats(0.8, 0.2, 0.2);
mat1.diffuseColor.copyFromFloats(0.2, 0.8, 0.2);    

// Submeshes are built in order : mat0 will be for the first cube, and mat1 for the second
multiMat.subMaterials.push(mat0, mat1);

// Last parameter to true means you want to build 1 subMesh for each mesh involved
subCSG.toMesh("csg", multiMat, scene, true);

babyloncsg's People

Contributors

craigfeldspar avatar

Stargazers

jumpjack avatar Ethan Sherbondy avatar Joseph Hancock avatar PPL avatar sheen avatar Lee.Lee avatar Roberto Capuano avatar MJimRenman avatar Gordon L. Hempton avatar  avatar  avatar DaShun avatar Ergin Akin avatar Terran avatar Mike Lyons avatar Angel avatar Ranveer Aggarwal avatar Alberto Piras avatar Alexandre Nicastro avatar Claudel Sylvain avatar Andy Shora avatar  avatar Jonathan Lussier avatar Avin Lambrero avatar Adam Bowman avatar Hugo Esquibet avatar Denis Stoyanov avatar  avatar Côme avatar François Deléglise avatar

Watchers

Côme avatar James Cloos avatar  avatar Jerry Richards avatar François Deléglise avatar  avatar

babyloncsg's Issues

Substract between sphere and box seems not to work

Hello I have this code:

var scene = new BABYLON.Scene(engine);
    var light = new BABYLON.DirectionalLight("dir01", new BABYLON.Vector3(0, -0.5, -1.0), scene);
    var camera = new BABYLON.ArcRotateCamera("Camera", 0, 0, 10, new BABYLON.Vector3(0, 0, 0), scene);
    camera.setPosition(new BABYLON.Vector3(10, 10, 10));
    light.position = new BABYLON.Vector3(20, 150, 70);
    camera.minZ = 10.0;

    scene.ambientColor = new BABYLON.Color3(0.3, 0.3, 0.3);

    var a = BABYLON.Mesh.CreateSphere("sphere", 16, 4, scene);
    var b = BABYLON.Mesh.CreateBox("box", 4, scene);

    a.position.y += 5;
    b.position.y += 2.5;

    var aCSG = BABYLON.CSG.FromMesh(a);
    var bCSG = BABYLON.CSG.FromMesh(b);

    // Disposing original meshes since we don't want to see them on the scene
   //   a.dispose();
   // b.dispose();

    // Set up a MultiMaterial
    var multiMat = new BABYLON.MultiMaterial("multiMat", scene);
    var mat0 = new BABYLON.StandardMaterial("mat0", scene);
    var mat1 = new BABYLON.StandardMaterial("mat1", scene);

    mat0.diffuseColor.copyFromFloats(0.8, 0.2, 0.2);
    mat0.backFaceCulling = false;

    mat1.diffuseColor.copyFromFloats(0.2, 0.8, 0.2);
    mat1.backFaceCulling = false;

    // Submeshes are built in order : mat0 will be for the first cube, and mat1 for the second
    multiMat.subMaterials.push(mat0, mat1);

    // Last parameter to true means you want to build 1 subMesh for each mesh involved
    var subCSG = bCSG.subtract(aCSG);
    var newMesh = subCSG.toMesh("csg", multiMat, scene, true);
    newMesh.position = new BABYLON.Vector3(-10, 0, 0);

    subCSG = aCSG.subtract(bCSG);
    newMesh = subCSG.toMesh("csg2", multiMat, scene, true);
    newMesh.position = new BABYLON.Vector3(10, 0, 0);

    subCSG = aCSG.intersect(bCSG);
    newMesh = subCSG.toMesh("csg3", multiMat, scene, true);
    newMesh.position = new BABYLON.Vector3(0, 0, 10);

    return scene;

and if you try it you will see that box.subtract(shpere) is not producing the right result :(

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.