We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.

Removing cloned geometry

Home Forums Programming Removing cloned geometry

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #20049
    mugga
    Participant

    I need some help in creating a filter function.

    I’m trying to create a live filter in conjunction with an csv list.
    I got multiple properly named objects in 3d. When I’m activating the filtering, a dummy objects gets cloned and positioned regarding the already “named” placed objects.

    This is working great, but when I’m changing the filter, I want to remove the already cloned objects.

    The only way I would see at the moment, is to read all the objects in the scene and removing the not used anymore ones. But I wondering if there is a better/simpler way.

    I’ve attached the puzzle I’m currently using for the creation of the clones.

    #20051
    jem
    Customer

    @mugga, the easiest way to manage clones is to maintain their names on a list. You can then write a function to delete all or some of the clones.

    There is a problem with your puzzles. You are cloning an object, but you are not recording the cloned object’s name anywhere. Without the clone’s name, you will struggle to delete it.

    The clone object puzzle is a function that returns the name of the new cloned object. I suggest that you assign the result of the “clone object” puzzle to a local variable, add that variable to a list for later use, and then set the cloned objects position.

    You can use a custom JS function to easily delete the clones. See:

    How do I remove objects from scene

    Jeremy Wernick

    #20061

    @jem thanks for helping out!

    Good news: we are going to ship the remove object puzzle with the next release. It won’t do the dispose operation though (for now). It appears, there is no need to dispose geometry and materials for the cloned objects, because these resources are shared with the original object. When we remove and dispose the clones, the engine automatically restores that disposed geometry and materials (from data stored in JavaScript variables) in order to render the original object.

    Chief 3D Verger | LinkedIn | Twitter

    #20068
    mugga
    Participant

    @jem
    Thank you very much for the explanations and examples. That was excactly my problem, I didnt know how to save the names for the clones. But now I got it working.

    Maybe you can help me with another thing. I would now also like to change the material (diffuse color) for every clone based on a calculated rgb value.
    The function for getting the rgb values, should already work, but cant change the color for the individual objects.

    #20095
    jem
    Customer

    @mugga, I think that you will run into issues trying to change the color of each clone independently. Color is an attribute of a material. All clones will have the same material as their source. That is, there will only be one material, not many copies of the same material. If you were to change the color of the material, the color of all of the clones would be affected. You will need a unique material for each clone. If you only had a minimal number of clones, you could create a few materials in Blender ahead of time and apply one of them to each clone. I suspect that you will have far too many clones to make that approach practical. You can create a JavaScript function to make new materials on-the-fly and apply them to the clones as you create the clones. Here is a link that explains some of JS material system if you are interested.
    https://threejsfundamentals.org/threejs/lessons/threejs-materials.html
    https://www.soft8soft.com/docs/manual/en/programmers_guide/Programming-basics.html
    https://www.soft8soft.com/docs/api/en/materials/MeshLambertMaterial.html
    Most three.js examples will work in the Verge3D engine. Just change the name of the library object from “THREE.” to “v3d.”

    Jeremy Wernick

    #20100
    jem
    Customer

    @mugga, the code is simple, so I just wrote the function. The setObjectColor function expects RGB values as ints between 0-255. If you have some other color format, the code is easy to adapt. This code goes in the ExternalInterface section of your main JS file.

    	//Create a material and assign it to an object
    	app.ExternalInterface.setObjectColor = function (object, red, green, blue) {
    		var colorHex = "#";
    		colorHex += toHex(red);
    		colorHex += toHex(green);
    		colorHex += toHex(blue);
    		var material = new v3d.MeshLambertMaterial({ color: colorHex });
    		app.scene.getObjectByName(object).material = material;
    	}
    	 
    	function toHex(rgb){
    		var hexStr = Number(rgb).toString(16);
    			if (hexStr.length < 2) {
    			hexStr = "0" + hexStr;
    		}
    		return hexStr;
    	}
    

    Jeremy Wernick

    #20207
    mugga
    Participant

    @jem
    Awesome, thank you very much for all your help. I could adapt your code and make it work for my usecase.

    Only thing which is quite strange is that when I’m using anything other than “MeshBasicMaterial” the objects turn black. The scene is only lit via envmap/hdri. Do I have to copy the scene envmap to the new material?

    #20280
    jem
    Customer

    @mugga, yes, this makes sense. I think that you will have to use the MeshNodeMaterial if you are only using HDRIs for lighting. Materials exported from Blender or 3ds use MeshNodeMaterial. This material is complex and challenging to create from scratch in JS code, but I do have an easy solution to this issue. Write some JS code to clone the material, change the RGB values of the base color, and then apply the newly cloned material to the newly created cloned object. This technique is simpler to implement than it sounds. I do not know how you set up your material nodes in Blender, but I will make two assumptions. First, the object from which you create the clones uses a Principled BSDF shader. Second, an RGB value node called “RGB” sets the base color in the Principled BSDF shader. I have included a screenshot of this node set up.

    Now, we need to rewrite the custom setObjectColor() function. Here is the new code:

    	//Create a material and assign it to an object
    	app.ExternalInterface.setObjectColor = function (object, red, green, blue) {
    		var newMat = app.scene.getObjectByName(object).material.clone();
    		var rgbIndex = newMat.nodeRGBMap['RGB']; // 'RGB' is the name of an RGB node
    		newMat.nodeRGB[rgbIndex] = new v3d.Vector4(red/255, green/255, blue/255, 1); // new color in RGBA format
    		app.scene.getObjectByName(object).material = newMat;
    	}

    This code is partialy based on the example in the documentation:
    https://www.soft8soft.com/docs/api/en/materials/MeshNodeMaterial.html
    I have attached an updated project (dummy.zip) that has the new JS code and is only illuminated with an HDRI environment.
    Please give this a try and let us all know if it works for you.

    Jeremy Wernick

    #20306
    mugga
    Participant

    Thank you very much for all your help jem. I had to modify it a little bit for my needs, but its working perfect. :good:

    #20307

    thanks for the clear explanation Jem! :good:

    Chief 3D Verger | LinkedIn | Twitter

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.