This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun Int.getTransform(): Mat4 { | |
val tm = modelViewer.engine.transformManager | |
return Mat4.of(*tm.getTransform(tm.getInstance(this), null)) | |
} | |
private fun Int.setTransform(mat: Mat4) { | |
val tm = modelViewer.engine.transformManager | |
tm.setTransform(tm.getInstance(this), mat.toFloatArray()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Reset the root transform, then rotate it around the Z axis. | |
modelViewer.asset?.apply { | |
modelViewer.transformToUnitCube() | |
val rootTransform = this.root.getTransform() | |
val degrees = 20f * seconds.toFloat() | |
val zAxis = Float3(0f, 0f, 1f) | |
this.root.setTransform(rootTransform * rotation(zAxis, degrees)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val frameCallback = object : Choreographer.FrameCallback { | |
private val startTime = System.nanoTime() | |
override fun doFrame(currentTime: Long) { | |
val seconds = (currentTime - startTime).toDouble() / 1_000_000_000 | |
choreographer.postFrameCallback(this) | |
modelViewer.animator?.apply { | |
if (animationCount > 0) { | |
applyAnimation(0, seconds.toFloat()) | |
} | |
updateBoneMatrices() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
// ... | |
//loadGlb("DamagedHelmet") | |
loadGltf("BusterDrone") | |
loadEnvironment("venetian_crossroads_2k") | |
} | |
private fun loadGltf(name: String) { | |
val buffer = readAsset("models/${name}.gltf") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
// ... | |
loadGlb("DamagedHelmet") | |
loadEnvironment("venetian_crossroads_2k") | |
} | |
private fun loadEnvironment(ibl: String) { | |
// Create the indirect light source and add it to the scene. | |
var buffer = readAsset("envs/$ibl/${ibl}_ibl.ktx") | |
KtxLoader.createIndirectLight(modelViewer.engine, buffer).apply { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
// ... | |
loadGlb("DamagedHelmet") | |
modelViewer.scene.skybox = Skybox.Builder().build(modelViewer.engine) | |
} | |
private fun loadGlb(name: String) { | |
val buffer = readAsset("models/${name}.glb") | |
modelViewer.loadModelGlb(buffer) | |
modelViewer.transformToUnitCube() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private val frameCallback = object : Choreographer.FrameCallback { | |
override fun doFrame(currentTime: Long) { | |
choreographer.postFrameCallback(this) | |
modelViewer.render(currentTime) | |
} | |
} | |
override fun onResume() { | |
super.onResume() | |
choreographer.postFrameCallback(frameCallback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private lateinit var surfaceView: SurfaceView | |
private lateinit var choreographer: Choreographer | |
private lateinit var modelViewer: ModelViewer | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
surfaceView = SurfaceView(this).apply { setContentView(this) } | |
choreographer = Choreographer.getInstance() | |
modelViewer = ModelViewer(surfaceView) | |
surfaceView.setOnTouchListener(modelViewer) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
implementation 'com.google.android.filament:filament-android:1.6.0' | |
implementation 'com.google.android.filament:filament-utils-android:1.6.0' | |
implementation 'com.google.android.filament:gltfio-android:1.6.0' | |
// ... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.android.filament.utils.* | |
class MainActivity : Activity() { | |
companion object { | |
init { Utils.init() } | |
} | |
// ... |