Here's a step-by-step tutorial using ARCore and Sceneform (community-maintained) to create an AR block placement app in Kotlin:
1. Setup Dependencies
app/build.gradle:
android {
compileSdkVersion 34
defaultConfig {
minSdkVersion 24 // ARCore requires min 24
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
// ARCore
implementation 'com.google.ar:core:1.40.0'
// Sceneform (community version)
implementation 'io.github.sceneview:arsceneview:2.0.0'
}
AndroidManifest.xml:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.ar" android:required="true" />
<application>
<meta-data android:name="com.google.ar.core" android:value="required" />
</application>
2. Main Activity (MainActivity.kt)
import android.os.Bundle
import android.view.MotionEvent
import androidx.appcompat.app.AppCompatActivity
import com.google.ar.core.HitResult
import com.google.ar.core.Plane
import io.github.sceneview.ar.ARSceneView
import io.github.sceneview.ar.node.AnchorNode
import io.github.sceneview.math.Position
import io.github.sceneview.node.CubeNode
class MainActivity : AppCompatActivity() {
private lateinit var sceneView: ARSceneView
private var ghostBlock: CubeNode? = null
private var placedBlock: AnchorNode? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
sceneView = findViewById(R.id.sceneView)
setupARSession()
createGhostBlock()
}
private fun setupARSession() {
sceneView.apply {
planeRenderer.isVisible = true // Show detected planes
onTouch = { motionEvent ->
when (motionEvent.action) {
MotionEvent.ACTION_UP -> placeBlock()
}
true
}
}
}
private fun createGhostBlock() {
// Create semi-transparent red block
ghostBlock = CubeNode(
size = Position(0.2f, 0.2f, 0.2f), // 20cm cube
center = Position(0.0f, 0.1f, 0.0f), // Center above ground
materialColor = Color(1.0f, 0.0f, 0.0f, 0.5f) // Red with transparency
).apply {
isEditable = false // Not selectable
isSelectable = false
}
sceneView.addChild(ghostBlock!!)
}
private fun placeBlock() {
// Remove existing block
placedBlock?.let {
sceneView.removeChild(it)
it.anchor?.detach()
}
// Get center screen position
val screenCenter = sceneView.width / 2f to sceneView.height / 2f
// Raycast from center
sceneView.arFrame?.let { frame ->
frame.hitTest(screenCenter.first, screenCenter.second)
.filter { hitResult ->
hitResult.trackable is Plane &&
(hitResult.trackable as Plane).isPoseInPolygon
}
.firstOrNull()
?.let { hitResult ->
placeAnchor(hitResult)
}
}
}
private fun placeAnchor(hitResult: HitResult) {
placedBlock = AnchorNode(sceneView.engine, hitResult.createAnchor()).apply {
// Add solid block
addChildNode(CubeNode(
size = Position(0.2f, 0.2f, 0.2f),
center = Position(0.0f, 0.1f, 0.0f),
materialColor = Color.RED
))
sceneView.addChild(this)
}
}
override fun onResume() {
super.onResume()
sceneView.resume()
}
override fun onPause() {
super.onPause()
sceneView.pause()
}
}
3. Layout (activity_main.xml)
<io.github.sceneview.ar.ARSceneView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sceneView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"/>
4. Update Ghost Block Position
Add this to your onCreate after createGhostBlock():
sceneView.onArFrame = { frame ->
val screenCenter = sceneView.width / 2f to sceneView.height / 2f
frame.hitTest(screenCenter.first, screenCenter.second)
.filter { hitResult ->
hitResult.trackable is Plane &&
(hitResult.trackable as Plane).isPoseInPolygon
}
.firstOrNull()
?.let { hitResult ->
ghostBlock?.position = hitResult.pose.position
ghostBlock?.rotation = hitResult.pose.rotation
}
}
Key Features Implemented:
-
Ghost Block:
- Semi-transparent red cube
- Follows center screen position
- Snaps to detected planes
-
Block Placement:
- Tap anywhere to place solid block
- Anchored to real-world position
- Replaces previous blocks
-
Plane Detection:
- Visualizes detected surfaces
- Blocks only placed on valid planes
Testing Instructions:
- Requires ARCore-supported device (check official list)
- Move device slowly to detect surfaces
- Look at target position until ghost appears
- Tap screen to place solid block
Troubleshooting:
- If no planes detected:
- Ensure good lighting
- Move device side-to-side
- Clean camera lens
- If app crashes:
- Check ARCore installation in Google Play Services
- Verify camera permissions
This implementation provides a foundation for AR object placement. You can enhance it by:
- Adding different shapes/models
- Implementing rotation/scale gestures
- Adding physics with Bullet physics library
- Persisting anchors with Cloud Anchors
The community-maintained Sceneform handles:
- 3D rendering
- AR session management
- Plane visualization
- Anchor handling
- Camera lifecycle
Sceneview GitHub | ARCore Documentation