Skip to content

Instantly share code, notes, and snippets.

@ThomasGorisse
Last active September 8, 2022 21:11
Show Gist options
  • Select an option

  • Save ThomasGorisse/3e653f264cdc6441b95bf420c83a0a66 to your computer and use it in GitHub Desktop.

Select an option

Save ThomasGorisse/3e653f264cdc6441b95bf420c83a0a66 to your computer and use it in GitHub Desktop.
package io.github.sceneview.node
import androidx.annotation.IntRange
import com.google.android.filament.*
import io.github.sceneview.Entity
import io.github.sceneview.components.HasRenderableComponent
import io.github.sceneview.managers.NodeManager
import io.github.sceneview.mesh.Geometry
import io.github.sceneview.mesh.geometry
/**
* Mesh are bundles of primitives, each of which has its own geometry and material.
*
* All primitives in a particular renderable share a set of rendering attributes, such as whether
* they cast shadows or use vertex skinning. Kotlin usage example:
*
* ```
* val entity = EntityManager.get().create()
*
* RenderableManager.Builder(1)
* .boundingBox(Box(0.0f, 0.0f, 0.0f, 9000.0f, 9000.0f, 9000.0f))
* .geometry(0, RenderableManager.PrimitiveType.TRIANGLES, vb, ib)
* .material(0, material)
* .build(engine, entity)
*
* scene.addEntity(renderable)
* ```
*
* To modify the state of an existing renderable, clients should first use RenderableManager
* to get a temporary handle called an <em>instance</em>. The instance can then be used to get or
* set the renderable's state. Please note that instances are ephemeral; clients should store
* entities, not instances.
*
* @see RenderableManager.Builder
* @see Geometry
*/
class MeshNode(
engine: Engine,
nodeManager: NodeManager,
entity: Entity
) : Node(engine, nodeManager, entity), HasRenderableComponent {
/**
* Creates a renderable component on a new entity.
*
* @param count the number of primitives that will be supplied to the builder
*/
constructor(
engine: Engine,
nodeManager: NodeManager,
@IntRange(from = 1) count: Int = 1,
renderableBuilder: RenderableManager.Builder.() -> Unit
) : this(engine, nodeManager, EntityManager.get().create().apply {
RenderableManager.Builder(count)
.apply(renderableBuilder)
.build(engine, this)
})
constructor(
engine: Engine,
nodeManager: NodeManager,
geometry: Geometry,
materialInstance: MaterialInstance? = null,
renderableBuilder: RenderableManager.Builder.() -> Unit
) : this(engine, nodeManager, geometry.submeshes.size, {
geometry(geometry)
if (materialInstance != null) {
geometry.submeshes.forEachIndexed { index, _ ->
material(index, materialInstance)
}
}
apply(renderableBuilder)
})
}
@grassydragon

Copy link
Copy Markdown

The constructor invocations look a little bit complicated. I think putting each parameter within this(...) on a new line can help with readability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment