Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Add 3D objects to a React 360 application

    Tomasz ŁakomyTomasz Łakomy
    reactReact
    ^16.3.2

    React 360 applications are not restricted to 2D panels in space – you can also bring in 3D elements. In this lesson we are going to learn how to import 3D objects using Entity component and how to use Locations to display them in a React 360 application.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Discuss

    Discuss

    Transcript

    Transcript

    Instructor: To import our 3D model in React 360 application, go to components and create a new file that we're going to call arrow.js. Here, import React from React, and we're going to also import asset and view from React 360.

    In order to display 3D models inside of React 360 application, we need to import entity from entity. entity is also part of React 360, but you have to import it in this way. We're going to create a new component. We're going to call it arrow.

    It's going to extend React component, and we're going to render a view and the entity inside of it. entity allows us to display external 3D models. We can either design something, or we can go to a website, such as Google Poly, and download a free model for our application.

    We can download this model in a couple of different formats. React 360 supports both object file and GLTF file. GLTF basically like a JPEG for 3D, and we're going to use this one. I'm going to download it and put it inside of our static assets directory.

    Here, we can see it inside of the static assets directory. To assign this model to the entity component, we need to specify a source. It's going to take an object, GLTF-2. We're going to set it equal to asset arrow.gltf.

    Let me save that, and right now, what we need to do is to register this arrow component. In order to do that, go to index.js, import the arrow component, like we do it with the flag component, and register it over here like this.

    Right now, we need to jump into client.js and render this component somehow. We cannot use a surface, because surfaces are for displaying React components onto flat planes, either on a cylinder or on a flat surface.

    With 3D models, we have to use something different. We have to use a location. First up, we need to import location from React 360 web, and we're going to create a new location. I'm going to call it myNewLocation, and it's going to be a new location.

    Here, we have to set in an area specifying the position of the location within the x, y, and z-axis. I'm going to put it as three meters to the right, zero meters in the y-axis, and minus one meter in the z-axis. This model is going to be displayed three meters to the right and one meter in front of me.

    We're going to render the arrow component to this new location. We're going to do React 360, render to location. Then we're going to create a new route, with the arrow component. I'm not going to set any props, and I'm going to use my new location, like this.

    I'm going to comment out this flag comment, because we are not going to need that. After we save and refresh that, we are not going to be able to see the arrow. We need to shed some light on this situation. There are a couple of different types of lighting we can use in React 360 applications.

    In this case, I'm going to use an ambient light. We're going to import it from ambient light, and I'm going to set the intensity of the light to 1.0We're going to set the color of the light to white. After I save and refresh that, we can see our problem.

    We were displaying the arrow, but it's absolutely massive. We need to scale it down a bit. To change that, we need to use this transform property. I'm going to specify a style. Inside of the style, we're going to have a transform.

    Transform property takes an array. We're going to have scale of 003, because our arrow is really huge. We're going to rotate it in the y direction by 120 degrees, and we're going to rotate it in the x direction by minus 30 degrees.

    After I save and refresh that, we have the desired effect. We have this 3D model of the arrow displayed a couple of meters to our right. The thing is, is that this model is flat right now. The reason it happens is that right now, we are using this ambient light.

    This ambient light has the same intensity in all kinds of direction. In order to have shadows displayed on this arrow model, we need to use some figures. First, import point light from point light. I'm going to copy this ambient light, because point light is similar to ambient light.

    It also has an intensity, also has a color, but I'm going to transform it. I'm going to use style, and I'm going to supply a transform property. It's going to translate, as in move, our light source to 0020. It's going to appear 20 meters behind our back.

    After I save and refresh that, we're going to see that our model looks much better, much more alive, because we are using the point size, so we can see the shadows on this model.