chandlerprall / ThreeCSG

CSG plugin for Three.js
MIT License
453 stars 152 forks source link

work with obj or stl loaded geometry #25

Open tarasprystavskyj opened 8 years ago

tarasprystavskyj commented 8 years ago

whant to use that lib with obj or stl or other files loaded through three.js.

now lib shows me error "the geometry is not supported"

How can I do it with this lib?

tarasprystavskyj commented 8 years ago

here is script I whant to make working http://188.226.157.20/3d/canvas_page_for_obj_model.html there you can see obj file type

Here is code I need to get working. ` var loader = new THREE.OBJMTLLoader(); loader.load( 'obj/shell_nte3.obj', 'obj/shell_nte3.mtl', function ( object ) { object.children.forEach (function(child) { }); var objectBSP = new ThreeBSP( object ); var materialNormal = new THREE.MeshNormalMaterial();

var sphereGeometry = new THREE.SphereGeometry( 60, 32, 32 );
var sphereMesh = new THREE.Mesh( sphereGeometry );
var sphereBSP = new ThreeBSP( sphereMesh );

var newBSP = sphereBSP.intersect( objectBSP ); //objectBSP //cubeBSP
var newMesh = newBSP.toMesh( materialNormal );
newMesh.position.set(-70, 60, -120);
scene.add( newMesh );

            } );`
tarasprystavskyj commented 8 years ago

solved this isue. Can now provide fix to your lib.

object.traverse( function ( child ) {

                        if ( child.geometry !== undefined ) {
                            console.log( "found object.geometry" );
                            geometry = child.geometry;

                        }

                    } );

and then we can make ThreeBSP object of it objectBSP = new ThreeBSP( geometry );

And whole code will look like this

var loader = new THREE.OBJMTLLoader();
                    loader.load( 'obj/'+filename+'.obj', 'obj/'+filename+'.mtl', function ( object ) {
                    var geometry;
                    var objectBSP;

                    object.traverse( function ( child ) {

                        if ( child.geometry !== undefined ) {
                            console.log( "found object.geometry" );
                            geometry = child.geometry;

                        }

                    } );

                    /*object.children.forEach (function(child) {
                        child.material.specular.setHex (0x595959);
                        child.material.color.setHex (0x959595);
                        child.material.ambient.setHex (0x959595);
                    });*/
                    //object.position.y = 0;
//                  object.position.x = 0;
//                  object.position.z = 0;
                    objects.push( mesh );

    function add_intersect(geometry)
    {
    objectBSP = new ThreeBSP( geometry );                
    var materialNormal = new THREE.MeshNormalMaterial();

    var sphereGeometry = new THREE.SphereGeometry( 15, 15, 15 );
    var sphereMesh = new THREE.Mesh( sphereGeometry );
    var sphereBSP = new ThreeBSP( sphereMesh );

    // Example #4 - Cube intersect Sphere
    //var newBSP = sphereBSP.intersect( objectBSP ); //objectBSP //cubeBSP
    var newMesh = newBSP.toMesh( materialNormal );
    newMesh.position.set(-70, 60, -120);

    //scene.add( sphereMesh  );
    //scene.add( object );
scene.add( newMesh );

    }   

    add_intersect(geometry);
scotmos commented 8 years ago

Does this also work with JSON FIles using THREE.ObjectLoader();?

tarasprystavskyj commented 8 years ago

I suppose yes. Don't remember the details of case now. But as I understand three.js stores all loaded models in object with same semantics.

Besides now I can also say that these lib (threeBSP) is not the slowest way to intersect small file even for production. Compared results with compiled app - openscad.

msahli2 commented 6 years ago

Hi it seems that ThreeCSG does not work with complex geometry loaded with neither ObjectLoader nor PlyLoader ! may I ask you what kind of OBJMTL are loading , and if you have some demo about , because the link above seems to be dead Thanks

liangchaob commented 3 years ago

Does it support stl file?

msahli2 commented 3 years ago

Hi This Works var loader = new THREE.OBJLoader(); loader.load('https://anyfile.obj', function(obj) { obj.traverse( function (child) { if (child instanceof THREE.Mesh) geometry = child.geometry; child.material = Shipmaterial; });

//Geo1 for  CGS  boolean Manipulation   we bave to Convert from BufferGeometry to geometry to allow boolean operations
    var geo = new THREE.Geometry().fromBufferGeometry( geometry );
    Geo1 = new THREE.Mesh( geo,  material1   );     
    Geo1 .rotation.x = -Math.PI/2 ; Geo1 .position.set(-x,0,0);  Geo1.renderOrder = 5;
            Geo1.updateMatrixWorld(true);

           //--------------boolean operations here--------------------
    }
liangchaob commented 3 years ago

Hi This Works var loader = new THREE.OBJLoader(); loader.load('https://anyfile.obj', function(obj) { obj.traverse( function (child) { if (child instanceof THREE.Mesh) geometry = child.geometry; child.material = Shipmaterial; });

//Geo1 for  CGS  boolean Manipulation   we bave to Convert from BufferGeometry to geometry to allow boolean operations
  var geo = new THREE.Geometry().fromBufferGeometry( geometry );
  Geo1 = new THREE.Mesh( geo,  material1   );     
  Geo1 .rotation.x = -Math.PI/2 ; Geo1 .position.set(-x,0,0);  Geo1.renderOrder = 5;
            Geo1.updateMatrixWorld(true);

           //--------------boolean operations here--------------------
    }

Thanks, it Works for the simple geometry! But my Chrome stuck when obj faces too much, how to solve

msahli2 commented 3 years ago

This may work (I hope so)

  1. download Meshlab (it's free)
  2. open anyfile.obj
  3. try to simplify the mesh
  4. save to anyfile2.obj
  5. Run the procedure with the later file

Le mer. 27 janv. 2021 à 01:58, liangchao notifications@github.com a écrit :

Hi This Works var loader = new THREE.OBJLoader(); loader.load('https://anyfile.obj', function(obj) { obj.traverse( function (child) { if (child instanceof THREE.Mesh) geometry = child.geometry; child.material = Shipmaterial; });

//Geo1 for CGS boolean Manipulation we bave to Convert from BufferGeometry to geometry to allow boolean operations var geo = new THREE.Geometry().fromBufferGeometry( geometry ); Geo1 = new THREE.Mesh( geo, material1 );
Geo1 .rotation.x = -Math.PI/2 ; Geo1 .position.set(-x,0,0); Geo1.renderOrder = 5; Geo1.updateMatrixWorld(true);

       //--------------boolean operations here--------------------
}

Thanks, it Works for the simple geometry! But my Chrome stuck when obj faces too much, how to solve

https://camo.githubusercontent.com/c5ecf5e6cae9c7fdb2e4b5028a82b22a35de2f330ea17173192c45b3afdc1a6c/687474703a2f2f697069632e6c69616e676368616f2e736974652f696d6167652e706e67

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/chandlerprall/ThreeCSG/issues/25#issuecomment-767933523, or unsubscribe https://github.com/notifications/unsubscribe-auth/ACW6XDNLQOITPN6ZVG2GPXDS35QKTANCNFSM4B46L7OA .