Home › Forums › Programming › Instancing by JavaScript
Tagged: instancing, javascript, js
- This topic has 9 replies, 6 voices, and was last updated 1 year, 11 months ago by kdv.
-
AuthorPosts
-
2022-01-17 at 10:25 am #48682webCustomer
Hello,
Because at the moment there is no support for instancing geometry with 3dsMax, I’m trying to do this “by hand”.
I managed to instance a geometry and also add it to the scene, but I dont know how to set the coordinates for the instances. At the end I would like to create the instances and set there position to predefined positions of dummy or null objects.
This is what I got so far:
var instanceCount = 100; var baseGeometry = dd_webGL.getObjectByName('FP_Boote_CILNUEVOS_LP_0'); var material = baseGeometry.material; var baseGeometryCloned = baseGeometry.geometry.clone(); var meshInstance = new v3d.InstancedMesh(baseGeometryCloned, material, instanceCount); const matrix = new v3d.Matrix4(); for (let i = 0; i < instanceCount; i++) { randomizeMatrix(matrix); meshInstance.setMatrixAt(i, matrix); meshInstance.scale.set(0.1, 0.1, 0.1); } app.scene.add(meshInstance);
I need to set the Matrix per instance but I havent had any luck with that. With the randomzieMatrix function I can at least see the different instances in the viewport.
Any hints in the right direction would be highly appreciated.
2022-01-17 at 10:46 am #48683webCustomerMade some progress, seems that this would be the right way. But problem is that the position on the instances is not at the same position as the dummies. Its quite off. How can I set the instances to exactly the same position as the dummies in the scene.
var dummies = dd_webGL.retrieveObjectNames(['GROUP', 'GRP_Dummies']); var instanceCount = dummies.length; var baseGeometry = dd_webGL.getObjectByName('FP_Boote_CILNUEVOS_LP_0'); var material = baseGeometry.material; var baseGeometryCloned = baseGeometry.geometry.clone(); var meshInstance = new v3d.InstancedMesh(baseGeometryCloned, material, instanceCount); dummies.forEach((elem, index) => { var tempMatrix = dd_webGL.getObjectByName(elem).matrix; meshInstance.setMatrixAt(index, tempMatrix); meshInstance.scale.set(0.05, 0.05, 0.05); }) app.scene.add(meshInstance);
2022-01-18 at 6:06 am #48699Yuri KovelenovStaffhi!
you might look at this Verge3D plugin https://www.soft8soft.com/topic/gliftek-plugin-pack-object-management/
it has a puzzle for instancing
2022-01-18 at 8:47 am #48705webCustomerI’ve looked at these puzzles, but there is nothing mentioned about instacing. Its just copying/cloning.
2022-01-18 at 2:51 pm #48715webCustomerGetting back here. My code was correct so far, the offset issue was caused by the group the dummy objects were part of.
I always forget that you need to set the pivot of the GRP to 0,0,0 coordinates in 3dsMax. Otherwise you get that offset when using coordinates from other objects.
So this code here is working when you want to create instanced objects which are created and positioned by dummy objects which are part of a group:
var dummies = retrieveObjectNames(['GROUP', 'GRP_Dummies']); var instanceCount = dummies.length; var baseGeometry = getObjectByName('Teapot001'); var material = baseGeometry.material; var baseGeometryCloned = baseGeometry.geometry.clone(); var meshInstance = new v3d.InstancedMesh(baseGeometryCloned, material, instanceCount); meshInstance.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); dummies.forEach((elem, index) => { var dummyObject = getObjectByName(elem); dummyObject.updateMatrix(); meshInstance.setMatrixAt(index, dummyObject.matrix); }) meshInstance.instanceMatrix.needsUpdate = true; app.scene.add(meshInstance);
Teapot001 is the mesh which I would like to copy instanced. And “GRP_Dummies” is just a group with several dummy objects I’ve created in max.
2022-01-19 at 6:37 am #48724Yuri KovelenovStaff2022-01-20 at 2:17 pm #48777AVerge3DerParticipantGetting back here. My code was correct so far, the offset issue was caused by the group the dummy objects were part of.
I always forget that you need to set the pivot of the GRP to 0,0,0 coordinates in 3dsMax. Otherwise you get that offset when using coordinates from other objects.
So this code here is working when you want to create instanced objects which are created and positioned by dummy objects which are part of a group:
var dummies = retrieveObjectNames(['GROUP', 'GRP_Dummies']); var instanceCount = dummies.length; var baseGeometry = getObjectByName('Teapot001'); var material = baseGeometry.material; var baseGeometryCloned = baseGeometry.geometry.clone(); var meshInstance = new v3d.InstancedMesh(baseGeometryCloned, material, instanceCount); meshInstance.instanceMatrix.setUsage( THREE.DynamicDrawUsage ); dummies.forEach((elem, index) => { var dummyObject = getObjectByName(elem); dummyObject.updateMatrix(); meshInstance.setMatrixAt(index, dummyObject.matrix); }) meshInstance.instanceMatrix.needsUpdate = true; app.scene.add(meshInstance);
Teapot001 is the mesh which I would like to copy instanced. And “GRP_Dummies” is just a group with several dummy objects I’ve created in max.
Thank you for sharing the steps in code you took to get this working.
2022-01-22 at 1:26 pm #48800jancCustomerI’m trying to use that script. Where would I paste it to? The “Exec Script” puzzle piece or the projects .js? Do I need to copy helper functions, too?
2023-01-01 at 1:56 pm #59310amirferdosParticipantalso it is my question?
I’m trying to use that script. Where would I paste it to? The “Exec Script” puzzle piece or the projects .js? Do I need to copy helper functions, too?
2023-01-01 at 2:32 pm #59313kdvParticipantCloned meshes share the same geometry and material. In fact, they are already instanced copies of the initial mesh. There is no need to do something else, no need to use the
InstancedMesh
class. See the “Clone Object” demo (82 cubes sharing one geometry buffer and one material).Puzzles and JS coding. Fast and expensive.
If you don’t see the meaning in something it primarily means that you just don’t see it but not the absence of the meaning at all.
-
AuthorPosts
- You must be logged in to reply to this topic.