Solar System in WebVR tutorial – Using objects in WebVR to create VR Experience
Solar System in VR
Our solar system is created of multiple components.
- Adding photo background in VR
- Adding objects in VR
- Adding text in VR
This is part of #DaysInVR series. View All VR Projects. Yesterday we created where’s waldo in VR. Today we’re going to make solar system in VR and learn how.
Join 6000+ Students
Upgrade your programming skills to include Virtual Reality. A premium step-by-step training course to get you building real world Virtual Reality apps and website components. You’ll have access to 40+ WebVR & Unity lessons along with their source code.
Start Learning Now
Adding galaxy photo background
Adding galaxy is the simplest step and if you’ve followed our other tutorials like 360 photos in VR, then this should be easy for you. Create a SphereGeometry
and create a material with the galaxy texture.
1 2 3 4 5 6 7 8 9 |
var material = new THREE.MeshBasicMaterial({ map: textureLoader.load('milky-way.jpg'), side: THREE.DoubleSide }); var milkyWay = new THREE.Mesh(new THREE.SphereGeometry(milkyWaySize, 35, 35), material); scene.add(milkyWay); |
Adding planet objects to our scene
We have to add our planets as objects in WebVR. We’ll be using SphereGeometry
for our planets. The procedure is same for all the planets that we have to add.
For our example, we’ll see how to add Mercury. We’ll learn how to add models in WebVR.
1 2 3 4 5 6 |
var mercurySize = 1.2; var mercuryOrbitRadius = sunSize + (AU * 0.4); var mercuryOrbitAngle = getRandomArbitrary(0, 360); var mercuryOrbitSpeed = 0.8; var mercuryRotateSpeed = 0.05; |
We’ll have to use MeshLambertMaterial
and we’ll use AmbientLight
to view the scene.
1 2 3 4 5 6 7 8 |
var material = new THREE.MeshLambertMaterial({ map: textureLoader.load('mercury.jpg'), shading: THREE.SmoothShading }); var mercury = new THREE.Mesh(new THREE.SphereGeometry(mercurySize, 15, 15), material); scene.add(mercury); |
We’ll follow the same method to each planet into the scene.
Adding text in WebVR
To add the text next to the planet, we’ll use TextGeometry
to add text to our scene.
1 2 3 4 5 6 7 8 9 10 11 |
var textMaterial = new THREE.MeshLambertMaterial({ color: 0xffffff }); var fontLoader = new THREE.FontLoader().load('helvetiker_regular.typeface.json', function(font) { var mercuryTextGeometry = new THREE.TextGeometry('mercury', { font: font, size: 7, bevelEnabled: false, height: 1 }); }); |
We’ll be using a FontLoader
to load the font necessary to display our text. We have to provide our font options and text to TextGeometry
. We’ll add the mesh to our scene.
Moving our planets
Now we have to rotate the planets around us. Let’s add the following code in our animate()
function.
1 2 3 4 5 6 7 8 9 10 11 |
mercuryOrbitAngle -= mercuryOrbitSpeed; var radians = mercuryOrbitAngle * Math.PI / 180; mercury.position.x = Math.cos(radians) * mercuryOrbitRadius; mercury.position.z = Math.sin(radians) * mercuryOrbitRadius; mercury.rotation.y += mercuryRotateSpeed; mercuryText.position.x = mercury.position.x + 10; mercuryText.position.y = mercury.position.y; mercuryText.position.z = mercury.position.z; mercuryText.needsUpdate = true; |
The position of textMesh
needs to be updated so that they move along with the planet. A slight offset is given to the position of the text so that they don’t overlap with the planet.
Create your version of solar system in VR. Share a link to your creation in the comments below.
Download Source Code
Kickstart VR development by downloading source code for this project. You’ll have instant access to source code and asset files. You can use them in your personal or commercial projects.
Leave a Reply