0

我正在尝试使用相同向量的数组进行渲染,但有时当我加载一个对象时,我会遇到一个问题,即相机向量会随着我加载到的对象而变化viewRenderable。可能是什么原因?

错误1

错误2

这两张图片是相同的渲染,具有相同的矢量。

目前,我正在混合MaterialFactoryviewrenderable. 行使用MaterialFactory,文本使用viewrenderables

这是我的代码的一部分。中心向量和相机向量被实现为全局变量。

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
            )
        }

}

这是渲染对象的实用程序代码。

@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)
                            )
                        }

                    }


                }
            }
        }
}
4

1 回答 1

0

这是一个简单的原因。

这是当 parentsNode 位置的创建时间慢于 ShareFactory 渲染完成时间时出现的问题。到目前为止,我已经把它放在渲染代码后面,它导致了错误。

因为 viewRenderable 的渲染生成时间比 ShapeFactory 慢,所以渲染是根据较慢的 worldPosition 创建的。

因此,我进行了以下更改。

...

    setCameraPosition(cameraPosition)

    initSceneView()
    initWorldPosition()

    when (type) {

        TYPE_3D -> {
            draw3Droom()
            draw3Dpart()
        }

        TYPE_FLOOR -> {

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

...

private fun initWorldPosition() {
    parentsTransformableNode.worldPosition = centerPosition
    //transformableNode.worldPosition = centerPosition
}
于 2021-05-12T00:32:19.453 回答