使用unity制作vr_这是使用Unity在10分钟内制作360 VR应用程序的方法

本文介绍了如何使用Unity在10分钟内快速构建一个360度VR应用程序,适用于Android和Google Cardboard。通过创建球体、翻转法线、投影360视频、设置Google Cardboard和在Android设备上运行,即使没有太多编程经验也能轻松上手。
摘要由CSDN通过智能技术生成

使用unity制作vr

by Adriana Vecchioli

由Adriana Vecchioli

这是使用Unity在10分钟内制作360 VR应用程序的方法 (Here’s how you can make a 360 VR app in 10 minutes with Unity)

Virtual Reality (VR) is exciting. It’s also the New Frontier of app development.

虚拟现实(VR)令人兴奋。 它也是应用程序开发的新领域。

VR is poised to give birth to new forms of storytelling and emotionally powerful experiences. Yet making VR is perceived as intimidating: it’s expensive and requires both special hardware and skills.

虚拟现实有望催生新的叙事形式和强大的情感体验。 然而,制作VR被认为是令人生畏的:它昂贵且需要特殊的硬件和技能。

But that’s changing, as intuitive tools and affordable hardware are making VR development accessible. This tutorial will show you how to build a 360 video app on Android and Google Cardboard in just a few minutes. And barely any coding required ;)

但这正在发生变化,因为直观的工具和价格合理的硬件使VR开发变得可访问。 本教程将向您展示如何在短短几分钟内在Android和Google Cardboard上构建360视频应用。 而且几乎不需要任何编码;)

VR development should not be a barrier to bringing your ideas to life. Let’s get started:

VR开发不应成为实现您的想法的障碍。 让我们开始吧:

你需要什么 (What you need)

Here’s our grocery list:

这是我们的购物清单:

? An Android phone with a gyroscope to sense head movements, running on KitKat or newer OS.

? 一个A ndroid手机 瓦特 i个陀螺仪来检测头的动作,在奇巧或较新的操作系统上运行。

? A Cardboard headset. If you don’t own one, you can find many on Amazon for less than 10 dollars. This one is my favorite.

? AC ardboard耳机。 如果您不拥有它,则可以在亚马逊上找到少于10美元的商品。 是我的最爱。

? Unity3D, a cross-platform game engine, that you need to install on your computer, version 5.6 or newer. We will use this software to build our whole project.

? 您需要在5.6版或更高版本的计算机上安装跨平台游戏引擎U nity3D 。 我们将使用该软件来构建我们的整个项目。

? The GoogleVR SDK for Unity, which you can download beforehand.

适用于UnityG oogleVR SDK,您可以事先下载。

? A 360 video. Shoot one with a 360 camera (here’s one you can plug into your phone) or find one online.

? 3 60个视频。 使用360相机拍摄一幅( 您可以将其插入手机)或在线查找一幅。

我们如何构建此应用程序? (How are we building this app?)

Unlike regular video that has a rectangular frame, 360 video has the shape of a sphere. So, we first need to create a spherical screen to project our 360 video onto. The player (or viewer) will be located inside this sphere and will be able to watch the video in any direction.

与具有矩形框架的常规视频不同,360视频具有球形形状。 因此,我们首先需要创建一个球形屏幕以将我们的360视频投影到该屏幕上。 播放器(或观看者)将位于该球体内,并且可以从任何方向观看视频。

The steps below should make you feel empowered to make your own changes, by explaining how it all works under the hood. For step-by-step instructions, refer to the video

下面的步骤将通过解释所有操作原理,使您有能力进行自己的更改。 有关分步说明,请参阅视频

步骤1:建立一个球体? (Step 1: Build a Sphere ?)

First, let’s open a new Unity Project if you are starting from scratch (or a new Scene if you want to integrate the 360 video player in an existing project.) Think of a Scene as one level of a video game, and a Project as a full game.

首先,从头开始创建一个新的Unity项目(如果要将360视频播放器集成到现有项目中,则打开一个新场景。)将场景视为视频游戏的一个级别,将项目视为完整的游戏。

Afterwards, add a sphere object in the Scene, placed at its center (Position = 0, 0, 0), with a radius of 50 (Scale = 50, 50, 50). The Camera’s position should also be set to 0, 0, 0. The Camera is the player/viewer’s eyes so we want it at the center of the Sphere. Placing it elsewhere would make the video look distorted.

然后,在场景中添加一个球体对象,将其放置在其中心( Position = 0,0,0 ),其半径为50( Scale = 50,50,50 )。 摄像机的位置也应设置为0、0、0。摄像机是播放器/查看器的眼睛,因此我们希望它位于球体的中心。 将其放置在其他位置会使视频看起来失真。

Once the Camera is placed inside the Sphere, the latter is no longer visible in the Scene. Don’t worry, there is an explanation for that! Indeed, most game engines do not, by default, render by default the inner side of 3D objects. This is because we rarely need to see them, it would be a waste of resource to render them. We’ll fix that next.

将摄影机放置在球体内后,球在场景中不再可见。 不用担心,有一个解释! 实际上,默认情况下,大多数游戏引擎不会默认渲染3D对象的内侧。 这是因为我们很少需要看到它们,渲染它们会浪费资源。 接下来,我们将修复该问题。

第2步:翻转球体的法线? (Step 2: Flip the Sphere’s Normals ?)

In our case, we do need to see our Sphere from the inside. That’s the whole point of the app, so we are going to turn it inside-out.

在我们的案例中,我们确实需要从内部查看Sphere。 这就是应用程序的全部重点,因此我们将由内而外进行转换。

In Unity, spheres are not actually spheres (what? We’ve been lied to all along!), they are polygons made with thousands of teeny, tiny facets. The external sides of the facets are visible, but not the internal ones. For that reason, we are going to make a program to flip these little facets like pancakes.

在Unity中,球体实际上不是球体(是什么?一直以来都是撒谎!),它们是由成千上万的微小面组成的多边形。 刻面的外部可见,但内部不可见。 因此,我们将编写一个程序来翻转这些薄煎饼等小面。

In 3D geometry, we call this transformation « reversing normals » or « flipping normals ».

在3D几何中,我们将此转换称为“反转法线”或“翻转法线”。

We’ll use a program called a Shader, that we’ll apply to the Material of the Sphere. Materials control the appearance of objects in Unity. Shaders are small scripts that calculate the color of each pixel rendered, based on lighting and information pulled from their Materials.

我们将使用一个名为Shader的程序,该程序将应用于球体的材质 。 材质控制Unity中对象的外观。 着色器是一些小的脚本,它们根据照明和从其“材质”中提取的信息来计算每个渲染像素的颜色。

Hence create a new Material for the Sphere, then a new Shader applied to this Material. We need to write custom code for the Shader… but have no fear, you can copy-paste the code below:

因此,为球体创建一个新的材质,然后将一个新的着色器应用于该材质。 我们需要为Shader编写自定义代码……但不用担心,您可以复制粘贴以下代码:

This little Shader is going to turn each pixel of the sphere inside out. Now our Sphere appears like a big white ball, viewed from inside, within our Scene. The next step is to turn this white sphere into a video player.

这个小着色器将把球的每个像素都翻过来。 现在,我们的球体看起来像一个大白球,从内部在我们的场景中查看。 下一步是将白色球体变成视频播放器。

步骤3:将您的360视频投影到Sphere中? (Step 3: Project your 360 video inside the Sphere ?)

Here you need to have a 360 mp4 video on hand. Import it in the project, then drag it onto the Sphere. And that’s when the magic happens: a ‘Video Player’ component appears and boom, the video is ready to play.

在这里,您需要准备一个360 mp4的视频。 将其导入项目中,然后将其拖动到Sphere上。 那就是魔术发生的时候:“视频播放器”组件出现并且轰动,视频可以播放了。

You can play with the settings like loops and audio. It also supports streaming!

您可以播放诸如循环和音频之类的设置。 它还支持流!

第4步:设置Google Cardboard? (Step 4: Set up Google Cardboard ?)

In this step, we’ll make the experience really feel immersive. That’s why we want to view it in a VR headset, here a Google Cardboard.

在这一步中,我们将使体验真正让人身临其境。 这就是为什么我们要在VR耳机(这里是Google Cardboard)中查看它的原因。

We are going to create a “stereoscopic” view (the screen will be split in two, with some fisheye effects on both sides — one side for each eye), using the GoogleVR SDK. The fisheye effect on each eye, combined with the distortion of the Cardboard’s plastic lenses, is what gives you the illusion of depth and immersion.

我们将使用GoogleVR SDK创建一个“立体”视图(屏幕将被分成两部分,在两面都有鱼眼效果(每只眼睛的一侧)。 鱼眼对每只眼睛的影响,再加上Cardboard塑料镜片的变形,使您产生深度和沉浸感的错觉。

To add the GoogleVR SDK to our project, download and import the plugin, then we’ll adjust a bunch of Android settings:

要将GoogleVR SDK添加到我们的项目中,下载并导入插件,然后我们将调整一系列Android设置:

  • Go to top bar menu > File > Build Settings. Add your open scene if it isn’t already added, then select Android in the list of supported platforms.

    转到顶部菜单栏>文件>构建设置 。 添加尚未打开的场景,然后在支持的平台列表中选择Android。

  • Click on Switch Platform. It should take a little while the first time you make the switch.

    单击“ 交换平台” 。 第一次切换时,可能需要一些时间。

  • Click on Player Settings. Components appear in the Instructor panel.

    单击播放器设置 。 组件出现在“讲师”面板中。

In the Player Settings’ Instructor, under the ‘Other Settings’ section:

在“播放器设置”讲师的“其他设置”部分下:

  • Check Virtual Reality Supported. Under Virtual Reality SDKs, select the + icon, then select Cardboard to add it to the list.

    检查“支持虚拟现实” 。 在“ 虚拟现实SDK”下 ,选择+图标,然后选择Cardboard将其添加到列表中。

  • Enter a package name into the Bundle Identifier field (for example, com.yourdomain.demo360). It has to be unique and is used to distinguish our app from others in the Google Play store.

    在“ 捆绑包标识符”字段中输入包名称(例如com.yourdomain.demo360 )。 它必须是唯一的,用于将我们的应用与Google Play商店中的其他应用区分开来。

  • Set the Minimum API Level drop-down menu to “Android 4.4 ‘Kit Kat’ (API level 19)”.

    将“ 最低API级别”下拉菜单设置为“ Android 4.4'Kit Kat'(API级别19) ”。

Afterwards, take the ‘GvrViewerMain’ element from the GoogleVR\Prefabs folder in the Project Browser, and drag it into the scene. In the Inspector, give it the same Position as the center of the Sphere — (0, 0, 0).

然后,从项目浏览器的GoogleVR \ Prefabs文件夹中获取“ GvrViewerMain ”元素,并将其拖动到场景中。 在检查器中,为其指定与球体中心相同的位置 —( 0,0,0 )。

The GvrViewerMain prefab controls all VR Mode settings, such as adapting the screen to the Cardboard’s lenses. It also communicates with your phone’s gyroscope to track your head movements. When you turn your head, the Camera and what you see also turn inside the 360 video player.

GvrViewerMain预制件可控制所有VR模式设置,例如使屏幕适应Cardboard的镜头。 它还与手机的陀螺仪通信以跟踪您的头部运动。 当您转动头时,相机和所看到的东西也会在360视频播放器内转动。

Now you can look in all directions when the video is on and the screen is split in two, to accommodate both lenses of the Cardboard.

现在,当视频打开并且屏幕分成两部分时,您可以从各个方向观看,以容纳纸板的两个镜头。

步骤5:在Android上运行该应用程序? (Step 5: Run the app on Android ?)

For our final step, we’ll run the app on an Android phone and share it with friends!

最后一步,我们将在Android手机上运行该应用程序并与朋友分享!

There are two ways to do that:

有两种方法可以做到这一点:

  • Go back to File > Build Settings. You can plug an Android phone with a USB cable to your computer and click on Build & Run. This installs the app straight to your phone.

    返回到“ 文件”>“构建设置”。 您可以将带有USB电缆的Android手机插入计算机,然后单击“构建并运行”。 这会将应用程序直接安装到您的手机上。

  • The other option is to click on Build only. This does not install it on a phone, but instead generates an APK file. You can share the APK by email with anyone who wants to try the masterpiece you just built. They have to double-tap on the APK attachment to install it on their phones.

    另一个选项是单击仅构建 。 这不会将其安装在手机上,而是会生成APK文件。 您可以通过电子邮件与想要尝试刚刚制作的杰作的任何人共享APK。 他们必须点按两次APK附件才能将其安装在手机上。

During the build process, you may be asked to select the root Android SDK folder. If that’s the case, download the Android SDK then select its folder location.

在构建过程中,可能会要求您选择Android SDK根文件夹。 如果是这种情况,请下载Android SDK,然后选择其文件夹位置。

Launch the app, pop your phone into a Cardboard headset, you’re good to go! You can replace the video with anything in 360 format and experience VR 360 immersion at home.

启动应用程序,将手机弹出到Cardboard耳机中,您就可以开始了! 您可以将视频替换为任何360格式的视频,并在家中体验VR 360的沉浸感。

更进一步 (Going further)

Congratulations, you made a 360 video app, and you are one step away from creating a VR video app! While the terms are often used alike, 360 and VR define two different experiences:

恭喜,您制作了360视频应用程序,距创建VR视频应用程序仅一步之遥! 虽然术语经常使用相同,但360和VR定义了两种不同的体验:

  • 360 video is recorded from all angles, with a special camera or an assembly of multiple ones. The user can watch in any direction desired, but there’s no interactivity to the experience.

    使用专用摄像机或多个摄像机的各个角度记录360度视频。 用户可以沿任何方向观看,但是体验没有交互性。
  • VR usually refers to a computer-generated environment in which the user is immersed. It is an interactive experience: the player can move around and control objects, in addition to looking in all directions.

    VR通常是指用户沉浸在其中的计算机生成的环境。 这是一种互动体验:玩家可以四处移动并控制对象,而且可以全方位查看。

Your new app can serve as a starting point to build a richer VR experience. Unity has plenty of features you can leverage, such as adding 3D elements or cool particle effects ✨ to overlay and enhance your video, or throwing in some interactive elements.

您的新应用可以作为构建更丰富的VR体验的起点。 Unity具有许多可以利用的功能,例如添加3D元素或炫酷的粒子效果✨以叠加和增强视频,或添加一些交互式元素。

You can also place a full 3D environment inside the 360 video player and use the latter as a skybox. The user can navigate in the scenery, using this nifty walking script.

您还可以在360视频播放器中放置完整的3D环境,并将其用作天空盒。 用户可以使用此漂亮的行走脚本在风景中导航。

Let your imagination run wild and show me your creations: tweet me @AdrianaVecc or leave a comment.

让您的想象力疯狂奔放,并向我展示您的创作:向我@AdrianaVecc鸣叫或发表评论。

Crafting beautiful VR stories is hard. Building them shouldn’t be.

制作精美的VR故事很难。 建立它们不应该。

If you liked this article, please press the ? below so other people will see it.

如果您喜欢这篇文章,请按 ?。 在下面,这样其他人就会看到它。

Adriana is an artist & product designer who makes VR experiences that build empathy. If you want to bring your VR ideas to life, shoot us an email: hello@vrtiginous.com ?

Adriana是一位艺术家和产品设计师,他通过VR体验建立同情心。 如果您想将您的VR创意变为现实,请向我们发送电子邮件:hello@vrtiginous.com?

翻译自: https://www.freecodecamp.org/news/how-to-make-a-360-vr-app-with-unity-51cbe41ad8f1/

使用unity制作vr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值