简体   繁体   English

拉贾瓦利3d:绕枢轴点旋转

[英]Rajawali 3d: Rotate around a pivot point

I have been using the StreamingTexture to show a video on screen with alpha channel. 我一直在使用StreamingTexture在具有Alpha通道的屏幕上显示视频。 The top half of the video has actual content and bottom half contains the alpha channel. 视频的上半部分包含实际内容,下半部分包含Alpha频道。

plane = new Plane(1, 2, 1, 1);//Plane height is 2 times the plane width due to the video size.
try {
        mMediaPlayer = new MediaPlayer();
        mMediaPlayer.setLooping(true);
        Uri videoUrl = Uri.parse("android.resource://" + getContext().getPackageName() + "/"
                + R.raw.angel);
        mMediaPlayer.setDataSource(getContext(), Uri.parse(videoUrl.toString()));
        mMediaPlayer.prepareAsync();    //prepare the player (asynchronous)
        mMediaPlayer.setOnPreparedListener(mp -> {
            mp.start(); //start the player only when it is prepared
        });
    } catch (IOException e) {
        e.printStackTrace();
    }


    // create texture from media player video
    mVideoTexture = new StreamingTexture("video", mMediaPlayer);

    // set material with video texture
    Material material =
            new Material(new VertexShader(R.raw.custom_vertix_shader),
                    new FragmentShader(R.raw.custom_fragment_shader));
    material.setColorInfluence(0f);

    try {
        material.addTexture(mVideoTexture);
    } catch (ATexture.TextureException e) {
        e.printStackTrace();
    }
    plane.setMaterial(material);
    getCurrentScene().addChild(plane);

Now when I rotate this plane using 现在当我使用

plane.setRotation(Vector3.Axis.Z, angle); 

The rectangle plane size (1, 2) rotates from center(0.5, 1) as it is supposed to but because the video shows only on the top half so it looks weird. 矩形平面尺寸(1、2)从中心(0.5、1)旋转,这是应该的,但是由于视频仅在上半部分显示,因此看起来很奇怪。 It looks like the video is rotating from bottom half. 看起来视频从下半部分开始旋转。

Solution Options: 解决方案选项:

  1. Rotate it from (0.5, 0.5) instead of (0.5, 1) but there is no method to do so. 从(0.5,0.5)而不是(0.5,1)旋转,但是没有方法可以这样做。

  2. Set the size of the plane to 1,1 and clip the bottom half of the video, there is no method to do so either. 将平面的大小设置为1,1,然后裁剪视频的下半部分,也没有方法。

Please suggest what other options I can go for or if there is a solution using above options. 请提出我可以选择的其他选项,或者是否有使用上述选项的解决方案。

I implemented this using a second plane which is exactly like the first plane but doesn't rotate. 我使用与第二个平面完全相同但不会旋转的第二个平面实现了此功能。 Then I used the second plane's coordinates to calculate the correct position values after its rotation. 然后,我使用第二个平面的坐标来计算旋转后的正确位置值。

private void setPlanePosition() {

    double radius = (0.5 * plane2.getScaleY());
    double circleCenterX = plane2.getX();
    double circleCenterY = plane2.getY() - radius;

    double xNewValue = (plane2.getX()) - radius * Math.sin(Math.toRadians(angle));
    double yNewValue = plane2.getY() - radius * Math.cos(Math.toRadians(angle)) + radius;
    plane.setPosition(xNewValue, yNewValue, -10);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM