sceneform-android icon indicating copy to clipboard operation
sceneform-android copied to clipboard

Texture Apply to 3D Model/Change SceneView Background

Open AtulLovoj opened this issue 8 months ago • 0 comments

While I have Successfully loaded the 3D Model from the Network url on the SceneView But I want to apply Some Physical Material to the Model with Image Texture with Some rotation and repetition

I am unable to figure out to how go about applying the texture

also Is there Any Way to clear the Background of the SceneView

package com.atul.threejs

import android.graphics.BitmapFactory import android.graphics.Color.* import android.graphics.drawable.ColorDrawable import android.os.Bundle import android.view.View import androidx.core.view.isGone import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import com.google.android.filament.Texture import io.github.sceneview.SceneView import io.github.sceneview.texture.ImageTexture import io.github.sceneview.math.Position import io.github.sceneview.node.ModelNode import kotlinx.coroutines.* import java.net.HttpURLConnection import java.net.URL

class MainFragment : Fragment(R.layout.fragment_main) {

private lateinit var sceneView: SceneView
private lateinit var loadingView: View

private var TRENCHCOAT_GLB = "https://xyz.com/website/TrenchCoat.glb"
private var TRENCHCOAT_TEXTURE = "https://xyz.com/fabImage/1741787357123.jpg"

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    sceneView = view.findViewById(R.id.sceneView)
    loadingView = view.findViewById(R.id.loadingView)

    viewLifecycleOwner.lifecycleScope.launch {

        sceneView.post {
            sceneView.cameraNode.apply {
                setProjection(fovInDegrees = 50.0)
            }


        }

        val modelNode = loadModel()

        modelNode?.let {
            sceneView.addChildNode(it)
        }

        loadingView.isGone = true
    }
}

private suspend fun loadModel(): ModelNode? {
    try {
        loadingView.isGone = false

        // Using SceneView's built-in network loader
        val modelInstance = sceneView.modelLoader.loadModelInstance(TRENCHCOAT_GLB)

        return modelInstance?.let {
            ModelNode(
                modelInstance = it,
                scaleToUnits = 0.5f
            ).apply {

                position = Position(y = -0.5f)
            }
        }
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    } finally {
        loadingView.isGone = true
    }
}

private suspend fun getTexture(): Texture? {
    return withContext(Dispatchers.IO) {
        try {
            val url = URL(TRENCHCOAT_TEXTURE)
            val connection = url.openConnection() as HttpURLConnection
            connection.doInput = true
            connection.connect()
            val bitmap = BitmapFactory.decodeStream(connection.inputStream)

            bitmap?.let {
                ImageTexture.Builder()
                    .bitmap(it)
                    .build(sceneView.engine)
            }
        } catch (e: Exception) {
            e.printStackTrace()
            null
        }
    }
}

}

AtulLovoj avatar Apr 11 '25 12:04 AtulLovoj