使用VideoPlayer播放360立体视频 [英] Play 360 Stereoscopic video with VideoPlayer

查看:126
本文介绍了使用VideoPlayer播放360立体视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android的Unity中的虚拟现实中播放立体声360度视频.到目前为止,我一直在做一些研究,我有两个左右眼的摄像机,每个摄像机都围绕着一个球体.我还需要一个自定义着色器,以使图像在球体内渲染.通过将y-tiling设置为0.5,我将图像的上半部分显示在一个球体上,将y-tiling设置为0.5,y-offset为0.5,将下半部分显示在另一个球体上. 以此,我可以显示已经正确的3D 360度图像.整个想法来自本教程.

I want to play a stereo 360 degree video in virtual reality in Unity on an Android. So far I have been doing some research and I have two cameras for the right and left eye with each a sphere around them. I also need a custom shader to make the image render on the inside of the sphere. I have the upper half of the image showing on one sphere by setting the y-tiling to 0.5 and the lower half shows on the other sphere with y-tiling 0.5 and y-offset 0.5. With this I can show a 3D 360 degree image already correct. The whole idea is from this tutorial.

现在用于视频,我需要控制视频速度,所以事实证明我需要新Unity 5.6中的VideoPlayer测试版.现在,到目前为止,我的设置要求视频播放器同时在两个球体上播放视频,其中一个球体在上方播放(一只眼睛),另一个球体在下方播放(另一只眼睛).

Now for video, I need control over the Video speed so it turned out I need the VideoPlayer from the new Unity 5.6 beta. Now my setup so far would require the Video Player to play the video on both spheres with one sphere playing the upper part (one eye) and the other video playing the lower part (other eye).

这是我的问题:我不知道如何让视频播放器在两种不同的材质上播放同一视频(因为它们的拼贴值不同).有办法吗?

Here is my problem: I don't know how to get the video Player to play the same video on two different materials (since they have different tiling values). Is there a way to do that?

我暗示我可以使用相同的材​​质并通过UV实现平铺效果,但是我不知道它是如何工作的,我什至没有让视频播放器使用两者的材质相同.我有一个此处的屏幕截图.右球体仅具有材质videoMaterial.无需平铺,因为我必须通过紫外线进行此操作.

I got a hint that I could use the same material and achieve the tiling effect via UV, but I don't know how that works and I haven't even got the video player to play the video on two objects using the same material on both of them. I have a screenshot of that here. The Right sphere just has the material videoMaterial. No tiling since I'd have to do that via UV.

走哪条路,怎么做?我在这里的路对吗?

Which way to go and how to do it? Am I on the right way here?

推荐答案

我在这里正确吗?

Am I on the right way here?

几乎,但是您当前正在使用RendererMaterial而不是RenderTextureMaterial.

Almost but you are currently using Renderer and Material instead of RenderTexture and Material.

走哪条路,怎么做?

Which way to go and how to do it?

您需要为此使用RenderTexture.基本上,您将视频渲染为RenderTexture,然后将该纹理分配给两个球体的材质.

You need to use RenderTexture for this. Basically, you render the Video to RenderTexture then you assign that Texture to the material of both Spheres.

1 .创建一个RenderTexture并将其分配给VideoPlayer.

1.Create a RenderTexture and assign it to the VideoPlayer.

2 .为球体创建两种材料.

2.Create two materials for the spheres.

3 .将VideoPlayer.renderMode设置为VideoRenderMode.RenderTexture;

4 .将两个球体的纹理设置为RenderTexture

4.Set the Texture of both Spheres to the Texture from the RenderTexture

5 .准备并播放视频.

下面的代码正是在做那件事.它应该开箱即用.您唯一需要做的就是根据需要修改每种材质的平铺和偏移.

The code below is doing that exact thing. It should work out of the box. The only thing you need to do is to modify the tiling and offset of each material to your needs.

您还应该注释掉:

leftSphere = createSphere("LeftEye", new Vector3(-5f, 0f, 0f), new Vector3(4f, 4f, 4f));
rightSphere = createSphere("RightEye", new Vector3(5f, 0f, 0f), new Vector3(4f, 4f, 4f));

然后使用从任何3D应用程序导入的Sphere.该行代码仅用于测试目的,并且使用Unity的球体播放视频不是一个好主意,因为这些球体没有足够的细节来使视频流畅.

then use a Sphere imported from any 3D application. That line of code is only there for testing purposes and it's not a good idea to play video with Unity's sphere because the spheres don't have enough details to make the video smooth.

using UnityEngine;
using UnityEngine.Video;

public class StereoscopicVideoPlayer : MonoBehaviour
{
    RenderTexture renderTexture;

    Material leftSphereMat;
    Material rightSphereMat;

    public GameObject leftSphere;
    public GameObject rightSphere;

    private VideoPlayer videoPlayer;

    //Audio
    private AudioSource audioSource;

    void Start()
    {
        //Create Render Texture
        renderTexture = createRenderTexture();

        //Create Left and Right Sphere Materials
        leftSphereMat = createMaterial();
        rightSphereMat = createMaterial();

        //Create the Left and Right Sphere Spheres
        leftSphere = createSphere("LeftEye", new Vector3(-5f, 0f, 0f), new Vector3(4f, 4f, 4f));
        rightSphere = createSphere("RightEye", new Vector3(5f, 0f, 0f), new Vector3(4f, 4f, 4f));

        //Assign material to the Spheres
        leftSphere.GetComponent<MeshRenderer>().material = leftSphereMat;
        rightSphere.GetComponent<MeshRenderer>().material = rightSphereMat;

        //Add VideoPlayer to the GameObject
        videoPlayer = gameObject.AddComponent<VideoPlayer>();

        //Add AudioSource
        audioSource = gameObject.AddComponent<AudioSource>();

        //Disable Play on Awake for both Video and Audio
        videoPlayer.playOnAwake = false;
        audioSource.playOnAwake = false;

        // We want to play from url
        videoPlayer.source = VideoSource.Url;
        videoPlayer.url = "http://www.quirksmode.org/html5/videos/big_buck_bunny.mp4";

        //Set Audio Output to AudioSource
        videoPlayer.audioOutputMode = VideoAudioOutputMode.AudioSource;

        //Assign the Audio from Video to AudioSource to be played
        videoPlayer.EnableAudioTrack(0, true);
        videoPlayer.SetTargetAudioSource(0, audioSource);

        //Set the mode of output to be RenderTexture
        videoPlayer.renderMode = VideoRenderMode.RenderTexture;

        //Set the RenderTexture to store the images to
        videoPlayer.targetTexture = renderTexture;

        //Set the Texture of both Spheres to the Texture from the RenderTexture
        assignTextureToSphere();

        //Prepare Video to prevent Buffering
        videoPlayer.Prepare();

        //Subscribe to prepareCompleted event
        videoPlayer.prepareCompleted += OnVideoPrepared;
    }


    RenderTexture createRenderTexture()
    {

        RenderTexture rd = new RenderTexture(1024, 1024, 16, RenderTextureFormat.ARGB32);
        rd.Create();
        return rd;
    }

    Material createMaterial()
    {
        return new Material(Shader.Find("Specular"));
    }

    void assignTextureToSphere()
    {
        //Set the Texture of both Spheres to the Texture from the RenderTexture
        leftSphereMat.mainTexture = renderTexture;
        rightSphereMat.mainTexture = renderTexture;
    }

    GameObject createSphere(string name, Vector3 spherePos, Vector3 sphereScale)
    {
        GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
        sphere.transform.position = spherePos;
        sphere.transform.localScale = sphereScale;
        sphere.name = name;
        return sphere;
    }

    void OnVideoPrepared(VideoPlayer source)
    {
        Debug.Log("Done Preparing Video");

        //Play Video
        videoPlayer.Play();

        //Play Sound
        audioSource.Play();

        //Change Play Speed
        if (videoPlayer.canSetPlaybackSpeed)
        {
            videoPlayer.playbackSpeed = 1f;
        }
    }
}

还有 Unity教程如何使用特殊的着色器执行此操作,但这对我和其他一些人无效.我建议您使用上述方法,直到将VR支持添加到VideoPlayer API中为止.

There is also Unity tutorial on how to do this with a special shader but this does not work for me and some other people. I suggest you use the method above until VR support is added to the VideoPlayer API.

这篇关于使用VideoPlayer播放360立体视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆