Vectors are sometimes applied differently when rendering in sceneform

81 views Asked by At

I'm trying to render with an array of the same vector, but sometimes when I load an object I have a problem where the camera vector changes with the object I load into viewRenderable. What could be the cause?

error1

error2

These two pictures are the same rendering, with the same vector.

Currently, I'm mixing MaterialFactory and viewrenderable. Lines use MaterialFactory, texts use viewrenderables.

This is part of my code. The center vector and camera vector are implemented as global variables.

class RenderingViewHolder(context: Context, type: Int, roomBean: RoomBean) {


init {

    findView()

    doorHeight = 15f
    windowHeight = 10f

    initPointList()
    initCallback()

    maxLength = LocationUtil.longLength(pointList, roomBean.height)

    initVectorList(segmentList)
    initPartVectorList(roomBean.wallObjectList)

    initCenterVector(vectorList1)
    setCameraPosition(cameraPosition)

    initSceneView()

    startRendering()

}

private fun startRendering() {
    DlogUtil.d(TAG, "음??????")


            draw3Droom()
            draw3Dpart()


    setTransformableNode()
}

private fun initCenterVector(vectorList: List<List<Vector3>>) {

    var cameraX = 0f
    var cameraY = 0f
    var cameraZ = 0f
    var maxPosition = 0f

    for (i in vectorList.indices) {

        cameraX += vectorList[i][0].x
        cameraY += vectorList[i][0].y
        cameraZ += vectorList[i][0].z

 
    }

    cameraX /= vectorList.size * 2
    cameraY /= vectorList.size * 2
    cameraZ /= vectorList.size * 2

    centerPosition = Vector3(cameraX, cameraY, cameraZ)

    
    cameraPosition =
        if (cameraZ <= 0) {
            Vector3(
                cameraX, cameraY,
                1.5f * maxPosition
            )
        } else {

            Vector3(
                cameraX, cameraY,
                cameraZ + 1.5f * maxPosition
            )
        }

}

This is the utility's code to render the object.

@RequiresApi(Build.VERSION_CODES.N)
fun drawCylinderLine(
    context: Context,
    lineColor: Color,
    radius: Float,
    length: Float,
    parentNode: TransformableNode,
    from: Vector3,
    to: Vector3
) {
    // 1. make a material by the color
    MaterialFactory.makeOpaqueWithColor(context, lineColor)
        .thenAccept { material: Material? ->
            // 2. make a model by the material
            val model = ShapeFactory.makeCylinder(
                radius, length,
                Vector3(0f, 0f, 0f), material
            )


            val light = Light.builder(Light.Type.FOCUSED_SPOTLIGHT)
                .setShadowCastingEnabled(false)
                .setIntensity(0f)
                .build()

            // 3. make node
            val node = Node()
            node.renderable = model

            node.setParent(parentNode)
            node.light = light
            node.worldPosition = Vector3.add(to, from).scaled(.5f);

            //4. set rotation
            val difference = Vector3.subtract(to, from)
            val directionFromTopToBottom = difference.normalized()
            val rotationFromAToB =
                Quaternion.lookRotation(
                    directionFromTopToBottom,
                    Vector3.up()
                )
            node.worldRotation = Quaternion.multiply(
                rotationFromAToB,
                Quaternion.axisAngle(Vector3(1.0f, 0.0f, 0.0f), 90f)
            )
        }
}

@RequiresApi(Build.VERSION_CODES.N)
fun drawTextView(
    context: Context,
    height: Float,
    text: String,
    parentNode: TransformableNode,
    from: Vector3,
    to: Vector3,
    direction: Constants.Direction
) {
    ViewRenderable.builder()
        .setView(context, R.layout.view_sceneview_length)
        .build()
        .thenAccept {
            val indicatorModel = Node()
            indicatorModel.setParent(parentNode)
            indicatorModel.renderable = it
            indicatorModel.worldPosition = Vector3(
                (from.x + to.x) / 2,
                ((from.y + to.y) / 2 + height),
                (from.z + to.z) / 2
            )

            var textView: TextView = it.view.findViewById(R.id.textViewX)

            var linearLayout: LinearLayout =
                it.view.findViewById(R.id.linearLayout)
            var layoutParam: LinearLayout.LayoutParams =
                LinearLayout.LayoutParams(250, 70)

            linearLayout.layoutParams = layoutParam
            textView.text = text

            //4. set rotation
            val difference = Vector3.subtract(to, from)
            val directionFromTopToBottom = difference.normalized()

            when (direction) {

                Constants.Direction.Horizontal -> {

                    val rotationFromAToB =
                        Quaternion.lookRotation(
                            directionFromTopToBottom,
                            Vector3.up()
                        )

                    indicatorModel.worldRotation = Quaternion.multiply(
                        rotationFromAToB,
                        Quaternion.axisAngle(Vector3(0.0f, 1.0f, 0.0f), 90f)
                    )

                }

                Constants.Direction.Vertical -> {

                    val rotationFromAToB =
                        Quaternion.lookRotation(
                            Vector3(0f, 0f, 0f),
                            Vector3.up()
                        )

                    indicatorModel.worldRotation = Quaternion.multiply(
                        rotationFromAToB,
                        Quaternion.axisAngle(Vector3(0.0f, 0.0f, 1.0f), 270f)
                    )

                }

                Constants.Direction.FLOOR -> {

                    when {
                        to.z > from.z -> {

                            var rotationFromAToB = Quaternion.lookRotation(
                                directionFromTopToBottom,
                                Vector3.left()
                            )

                            indicatorModel.worldRotation = Quaternion.multiply(
                                rotationFromAToB,
                                Quaternion.axisAngle(Vector3(0f, 1f, 0f), 90f)
                            )
                        }
                        to.z < from.z -> {
                            var rotationFromAToB = Quaternion.lookRotation(
                                directionFromTopToBottom,
                                Vector3.right()
                            )

                            indicatorModel.worldRotation = Quaternion.multiply(
                                rotationFromAToB,
                                Quaternion.axisAngle(Vector3(0f, 1f, 0f), 90f)
                            )

                        }
                        else -> {
                            var rotationFromAToB = Quaternion.lookRotation(
                                Vector3(0f, 0f, 0f),
                                Vector3.zero()
                            )

                            indicatorModel.worldRotation = Quaternion.multiply(
                                rotationFromAToB,
                                Quaternion.axisAngle(Vector3(1f, 0f, 0f), 90f)
                            )
                        }

                    }


                }
            }
        }
}
1

There are 1 answers

0
Mary Kim On

This is simple reason.

This is a problem that occurs when the creation time of the parentsNode position is slower than the completion time of the ShareFactory rendering. So far I've put this behind the rendering code, and it caused the error.

Because viewRenderable has a slower rendering generation time than ShapeFactory, rendering was created according to the slower worldPosition.

So, I made the following changes.

...

    setCameraPosition(cameraPosition)

    initSceneView()
    initWorldPosition()

    when (type) {

        TYPE_3D -> {
            draw3Droom()
            draw3Dpart()
        }

        TYPE_FLOOR -> {

            //핀치 줌 막는 변수
            isFloor = true

...

private fun initWorldPosition() {
    parentsTransformableNode.worldPosition = centerPosition
    //transformableNode.worldPosition = centerPosition
}