Preliminary study on virtual reality development of cardboard (IV.)

Source: Internet
Author: User

Google Cardboard Virtual reality Glasses Development Preliminary (iv) Cardboard SDK for Unity demo some parsing write in front

Since the last write this series of the third, busy for a while of the project, busy for a while in English, after a big wave exam, finally can continue to share some development experience, this period has several students to my private message even qq me, ask me when update, helpless is too busy, can only drag and drag, I feel is very sorry to everyone, failed to update in time, here to tell you a sorry. But there are so many people pay attention to my blog, this is my biggest affirmation, can get everyone's approval in the heart is very happy, I hope that you can continue to pay attention to me, I will be on the ground continued to take my own experience of some of my own serious to organize and share to everyone's ~

In addition, about cardboard SDK for unity English API I have shared in my blog, I also translated a Chinese version for your reference, the following is the address:

Cardboard Unity SDK Reference (English version): http://blog.csdn.net/sunmc1204953974/article/details/47157951

Cardboard Unity SDK Reference (translated version): http://blog.csdn.net/sunmc1204953974/article/details/49945945

In fact, Cardboard offers everything here, and what I'm going to write is just to help you get started, and some of my own experience skills. In addition, this thing is still relatively new, Google has been doing follow-up updates, my blog can only be written to this point in time content, as for the latest news and updates, we can only pay attention to Google's official website, the site of the previous several times have been mentioned.

In addition to this article, I will later write some cardboard virtual reality related development skills, below is what I have developed some of the content, I hope to help you:

    • Several solutions for playing video in virtual reality

    • How to effectively solve video distortion in virtual reality

    • How to quickly set a solid screen in virtual reality

    • How to easily solve mobile phone adaptation in virtual reality

    • How to data quickly in virtual reality

    • How to lay out an object moving with the head in virtual reality

    • How to realize gaze reading seconds in virtual reality

Gossip says here, the following goes on:

In the previous article, we took advantage of the cardboard SDK for unity demo to create our own scene, that is, to put their own model into the scene, with virtual reality glasses can be viewed.

We want to use this demo to the next development, it is necessary to have some basic understanding, this article will be on the official demo to some analysis, I hope to be helpful to everyone.

(i) Open demoscene

The process of importing and running the second article has been made very clear, the students are not clear to the front to turn over the can.

We opened the Assets/cardboard/demoscene under the demoscene, we also found that there are not many things in this folder, and these are all of this demo, only a script code file.

This demo will have several features after it runs:

After running, press ALT to move the mouse to simulate the head rotation, hold CTRL to simulate the tilt of the neck when the angle of view changes, click the mouse equivalent to trigger, can be used to operate.

Focus on the small yellow dots on the block, click the mouse, the block will be driven to a distance limited by the random position on the sphere

When looking at a block, the block will turn from red to green, and the block will change from green to red when looking away from the block.

At the foot there are three buttons, respectively

Reset: Reset the block back to its original position

Recenter: Re-aligning the left and right direction to the center for calibration

VR mode: Turn VR on or off (split screen or not)

(ii) Realization analysis of demoscene

First we see the left side of the hierarchy panel, here the demoscene in the components at a glance, a cube is naturally the color of the red green box, a plane is the bottom of the black checkered floor, there is a point light as a source, A canvas below is a panel with three buttons, and the students who are familiar with unity must be familiar with it. More special is a cardboardmain and a eventsystem, the effect of virtual reality is cardboardmain this cardboard main camera, and EventSystem is with the gaze point (that is, that little yellow dot) related, Here we explain later.

Let's look at each component carefully, the floor and the light source are nothing special, but the cube is different:

1. The entire demo's unique code is attached to the cube.
2.Cube comes with a box Collider (Collider), which is why we can use the line of sight (that yellow dot) to click on the block: Only one object has a collision effect to detect a collision with the line of sight, thus doing something. (You can see that only the small dots will show up when the object with the collider is visible)
3.Cube has a eventtrigger to receive the previously mentioned EventSystem events and use these conditions to invoke the method.

We can see from the inspector window of the cube that the cube has a eventtrigger.

EventTrigger is used to receive events from EventSystem, which are called whenever an event is received. So it's quite clear here.

    • Pointer enter means that the eye sees this object and then calls the Setgazeat (bool) method of the teleport script attached to the cube and passes in a true (note that the arrow is on)

    • Pointer exit refers to the eye leaving the object, and then also calls the Setgazeat (bool) method of the teleport script attached to the cube, and passes in a false (note that the arrow is not hit)

    • Pointer enter means to click on the object with a yellow dot (this is to click on the cube to click on the mouse or touch screen), and then call the teleport script attached to the cube teleportrandomly method, As the name implies, move to a random location.

The next step, of course, is to look at teleport's code and see what these methods are:

Here I add a note to this class, which should be very clear:

The following I posted this code may be a bit different from the example, I added a few people may be more commonly used methods, such as turning off the middle of the white line, switch off the method of setting the button, you can read the code to pay attention to

//Copyright Google INC. All rights reserved.////Licensed under the Apache License, Version 2.0 (the "License");// You are not a use of this file except in compliance with the License.//Obtain a copy of the License at////http://www.apache.org/licenses/LICENSE-2.0////unless required by applicable or agreed to writing, software///distributed under the License is distributed on a "as is" BASIS,//without warranties or CONDITIONS of any KIND, either express or implied.//See the License for the specific language governing permissions and//limitations under the License.usingUnityengine;usingSystem.Collections; [Requirecomponent (typeof(Collider))]/ * * The script code for the Teleport class cube * / Public classteleport:monobehaviour{//define the starting position vector    PrivateVector3 startingposition;/* * Start () method * If the script instance is enabled, the start function is called before the first frame is updated and is called only once during the life cycle of the script instance */    voidStart () {//Get current positionStartingposition = transform.localposition;//set to false close lineCardboard.SDK.EnableAlignmentMarker =true;//Set to False close the Settings buttonCardboard.SDK.EnableSettingsButton =true;//Set neck simulation effect to maximumCardboard.SDK.NeckModelScale =1F//Initialize the gaze status to falseSetgazedat (false); }/* * SETGAZEDAT (BOOL Gazedat) method * Change the color of the block depending on whether you stare at the block * *     Public void Setgazedat(BOOLGazedat) {getcomponent<renderer> (). Material.color = Gazedat?    Color.green:Color.red; }/* * Reset () method * Set the block position to the initial position * called by Buttoncanvas, Panel, Resetbutton     Public void Reset() {transform.localposition = startingposition; }/* * Togglevrmode () method * Toggle VR mode * Called by Buttoncanvas, Panel, Vrmodebutton */     Public void Togglevrmode()    {//Turn off VR modeCardboard.SDK.VRModeEnabled =!    Cardboard.SDK.VRModeEnabled; }/* * teleportrandomly () method * Set block position to random position * called by Cardboard.SDK.Triggered && Islookedat */     Public void teleportrandomly()    {//Returns a random point on the surface of a sphere with a radius of 1Vector3 direction = Random.onunitsphere;//static function Clamp (value:float, Min:float, max:float): float        //Limit value between min and Max, if value is less than min, returns min. Returns max if value is greater than max, otherwise returns valueDIRECTION.Y = Mathf.clamp (DIRECTION.Y,0.5F1f);//Random.value Returns a random number between 0.0 (inclusive) ~1.0 (inclusive )        //Returns a random number between 1.5 ~ 3.5        floatDistance =2* Random.value+1.5F//position is a random point on a sphere with a radius of 1.5~3.5Transform.localposition = direction * distance; }}

So here you can understand why you see the cube will turn green, and the eyes will turn red again, random transmission is also at a glance.

Then there are several other methods in this class, which are called by several buttons under the foot, and by clicking on the three buttons, you can see that they call the Togglevrmode () of the teleport script bound to the cube on the right side of the inspector window, Reset ( ), as well as the Recenter method of Carboard.

Resetbutton

Resetbutton called the Reset () method of teleport to transfer the block back to its original position.

Recenterbutton

Recenterbutton called the Cardboard Recenter () method, recalibration "front", for example, you sit at the computer table to see the front, your friend sits behind you and your back, he wears glasses to see the scene is right behind, click this button, The position in front of the scene will be calibrated to the current actual orientation, and your friend will see straight ahead, and you'll see the back when you get your glasses.

Recenter methods in the Cardboard class:

  // Reset the tracker so that the user‘s current direction becomes forward.  publicvoidRecenter() {    device.Recenter();  }

Vrmodebutton

Vrmodebutton called the Teleport Togglevrmode () method, turn VR mode on/off, the so-called Open VR mode is the one we see above the binocular double screen effect, and the closure will become a single screen, that is the effect:

(iii) Conclusion

To here the full function of the demo is over, everyone to this demo should have seen more thorough, and then the idea of the demo we can be compared to the gourd painting scoop, to this demo-based continue their development, the length of this article is not short, write here, I hope to help you.

Summing up is cardboardmain as cardboard provide the main camera, realize the effect of virtual reality, here you see the document will have a deeper understanding, but also will see Google in the back to do the work or a lot of, like the distortion of the processing and so complex problems, Google has done for us, and then I will talk about the use of the main camera, which has encapsulated a lot of methods for us to use, such as can be adapted to different mobile phones and so on. And then the demo in the EventSystem and head under the Gazepointer provides a set of gaze points of the interface, we can use the EventTrigger as a virtual reality of the three-dimensional mouse to do something, you can also go to customize the gaze point, I will explain these carefully in the follow-up.

The purpose of this series is that I find the information on the Internet when I found that there is no information, so share to help everyone, in addition is a role, I believe that unity is more familiar with the students, based on Google do these work, will soon be able to develop a great virtual reality application ~

This plugin still has some problems, I have encountered a lot of development, such as sometimes gyro will drift, the API provides the method to get the head data back to the number has been unchanged, in the follow-up I will try to solve these problems to share the solution to everyone, but also hope that we continue to pay attention to me, thank you ~

Preliminary study on virtual reality development of cardboard (IV.)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.