简体   繁体   中英

Rajawali 3d: Rotate around a pivot point

I have been using the StreamingTexture to show a video on screen with alpha channel. The top half of the video has actual content and bottom half contains the alpha channel.

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. 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.

  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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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