Creating a WEBVR Environment
We will use the following tags
- <a-scene>: The <a-scene> element represents a scene. All entities are contained within the scene, which is the global root object.
- <a-assets>: A-Frame has an asset management system that allows us to place our assets in one place and to preload and cache assets for better performance.
- <a-plane>: The plane primitive creates flat surfaces using the geometry component with the type set to plane.
- <a-sky>: The sky primitive adds a background color or 360° image to a scene. A sky is a large sphere with a color or texture mapped to the inside.
- Link to the latest AFRME library: https://aframe.io/releases/1.3.0/aframe.min.js”
The Code
<!DOCTYPE html>
<a-assets>
<img id=”sunny-sky”src=”images/nsky.jpeg”>
</a-assets>
<a-plane src=”#floor”repeat=”100 100″rotation=”-90 0 0″scale=”100 100″></a-plane>
<a-sky src=”#sunny-sky”rotation=”0 64 0″></a-sky>
</a-scene>
Adding Primitive Shapes to a VR Enviornment
We will use the following tags
- <a-scene>: The <a-scene> element represents a scene. All entities are contained within the scene, which is the global root object.
- <a-assets>: A-Frame has an asset management system that allows us to place our assets in one place and to preload and cache assets for better performance.
- <a-plane>: The plane primitive creates flat surfaces using the geometry component with the type set to plane.
- <a-sky>: The sky primitive adds a background color or 360° image to a scene. A sky is a large sphere with a color or texture mapped to the inside.
- <a-box> To create a cube and apply texture
- <a-sphere> To create a sphere and add texture
- Link to the latest AFRME library: https://aframe.io/releases/1.3.0/aframe.min.js”
Positioning in WEBVR
A-Frame uses a right-handed coordinate system
Position values represent the 3D axes.
X – Y – Z
Code
Example
WEBVR Physics (Gravity)
In this tutorial, I would like to share how to make use of physics in WEBVR simulating earth’s gravity.
In this tutorial, we will make use of:
AFrame and Aframe Physics Library:
- https://aframe.io/releases/1.0.4/aframe.min.js
- https://unpkg.com/aframe-physics-system@1.4.3/dist/aframe-physics-system.min.js
-
restitution element
- Position and rotation attributes
- Applying physics elements such as Dynamic bode and static body
Explanation Dynamic, Static Body an Restituition
- dynamic-body: A freely-moving object. Dynamic bodies have mass, collide with other objects, bounce or slow during collisions, and fall if gravity is enabled.
- static-body: A fixed-position or animated object. Other objects may collide with static bodies, but static bodies themselves are unaffected by gravity and collisions.
- The restitution is the bounciness value of the objects that fall on the floor
CODE
Positioning
In this part, we will learn how to position shapes, point of view and make use of light. New elements/properties that we will be using are:
- <a-entity>
- <a-light> Properties: type – look-at
Code:
Physics in Aframe
In this example of Aframe physics, we will look at bodies that are both still and moving. In Aframe physics, a “static body” is a solid object that other objects can’t pass through. When an object is a “dynamic body” it can be moved, grabbed, and thrown away.
In this exercise we will use the following libraries:
- <script src=”https://unpkg.com/aframe-physics-system@1.4.3/dist/aframe-physics-system.min.js”></script>
- <script src=”https://unpkg.com/super-hands@2.0.2/dist/super-hands.min.js”></script>
- <script src=”https://cdn.rawgit.com/donmccurdy/aframe-extras/v3.13.1/dist/aframe-extras.min.js”></script>
Library 1 is for the use of Physics such as dynamic-body, static-body and settings for gravity
Library 2 is to create a cursor for interaction
Library 3 is the main Aframe library
This exercise will show 3 shapes: a blue cube on the right, a red cube on the left, and a green sphere in the middle. The blue cube is set to be a “static body.” This means that anything that hits it won’t go through, and the shape won’t move. Since the red cube is set to have a “dynamic body,” any object that hits it will not go through but will cause it to move. The green sphere is set to be grabbable and have a dynamic body. This means that you can grab it with the cursor and move it around. Use the code below to try it out.
Code:
- <!DOCTYPE html>
- <html lang=”en”>
- <head>
- <meta charset=”UTF-8″>
- <meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
- <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
- <title>Document</title>
- <script src=”https://aframe.io/releases/1.0.4/aframe.min.js”></script>
- <script src=”https://unpkg.com/aframe-physics-system@1.4.3/dist/aframe-physics-system.min.js”></script>
- <script src=”https://unpkg.com/super-hands@2.0.2/dist/super-hands.min.js”></script>
- <script src=”https://cdn.rawgit.com/donmccurdy/aframe-extras/v3.13.1/dist/aframe-extras.min.js”></script>
- </head>
- <body>
- <a-scene physics=”gravity: -9.8″>
- <a-assets>
- <img id=”ground” src=”images/floor.jpg”>
- </a-assets>
- <a-entity>
- <a-camera progressive-controls position=”0 0 5″></a-camera>
- </a-entity>
- <!–Ground–>
- <a-plane src=”#ground” scale=”50 50 1″ repeat=”5 5 1″ rotation=”-90 0 0″ static-body></a-plane>
- <!–Red Box –>
- <a-box color=”red” position=”-3.5 4 -4″ dynamic-body></a-box>
- <!–Blue Box–>
- <a-box color=”blue” position=”3.5 0.5 -4″ static-body=”shape: sphere; sphereRadius:1″></a-box>
- <!–Green Sphere–>
- <a-sphere color=”green” radius=”0.5″ position=”-1 4 -4″ grabbable dynamic-body></a-sphere>
- <a-plane color=”green” rotation=”-90 0 0″ position=”-1 4 0″ static-body></a-plane>
- </a-scene>
- </body>
- </html>