Instancing
val erythrocyte = Mesh()
erythrocyte.readFromOBJ(Mesh::class.java.getResource("models/erythrocyte.obj").file)
erythrocyte.material = ShaderMaterial.fromFiles("DefaultDeferredInstanced.vert", "DefaultDeferred.frag")
erythrocyte.material.ambient = GLVector(0.1f, 0.0f, 0.0f)
erythrocyte.material.diffuse = GLVector(0.9f, 0.0f, 0.02f)
erythrocyte.material.specular = GLVector(0.05f, 0f, 0f)
erythrocyte.material.metallic = 0.01f
erythrocyte.material.roughness = 0.9f
erythrocyte.name = "Erythrocyte_Master"
erythrocyte.instancedProperties["ModelMatrix"] = { erythrocyte.model }
scene.addChild(erythrocyte)var instancedProperties = LinkedHashMap<String, () -> Any>()//This is the node we use to store our instances of the object
val container = Node("Cell Container")
val erythrocytes = (0 until 40).map {
val v = Mesh()
v.name = "erythrocyte_$it"
v.instancedProperties["ModelMatrix"] = { v.world }
v.metadata["axis"] = GLVector(sin(0.1 * it).toFloat(), -cos(0.1 * it).toFloat(), sin(1.0f*it)*cos(1.0f*it)).normalized
v.parent = container
//This part is only important for visualization; it makes the cells floating in space
val scale = Random.randomFromRange(0.5f, 1.2f)
v.scale = GLVector(scale, scale, scale)
v.position = Random.randomVectorFromRange(3, -positionRange, positionRange)
v.rotation.setFromEuler(
Random.randomFromRange(0.01f, 0.9f),
Random.randomFromRange(0.01f, 0.9f),
Random.randomFromRange(0.01f, 0.9f)
)
v
}
// Here comes the important part: we add all the objects created to the
// instances list of the master erythrocyte.
erythrocyte.instances.addAll(erythrocytes)Last updated
Was this helpful?