Shopping in VR – Using Objects and lights in WebVR
Shopping in VR
In this tutorial, we’ll see how to use objects and lights in WebVR to create a shopping experience in VR. We’re using objects from tf3dm.com and PointLight
, AmbientLight
and SpotLight
to light our models. The item name and price will be displayed along with the object using TextGeometry
.
This is part of #DaysInVR series. View All VR Projects. Yesterday we learnt how to use SpotLight
and
PositionalAudio in WebVR to create a nightclub effect.
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 objects in WebVR
For the purposes of the demo, we’ll show three women’s wear items that users can checkout to buy. We’re using OBJLoader
and MTLLoader to load our object. We’ll then place the 3 objects around our camera. Our camera is placed at the center.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
var object1; var mtlLoader = new THREE.MTLLoader(); mtlLoader.setPath('/projects/day16/dancer07/'); mtlLoader.setTexturePath('/projects/day16/dancer07/'); mtlLoader.load('dancer07.mtl', function(materials) { materials.preload(); var objLoader = new THREE.OBJLoader(); objLoader.setMaterials(materials); objLoader.setPath('/projects/day16/dancer07/'); objLoader.load('dancer07.obj', function(object) { object.position.y = -3; object.position.z = -10; scene.add(object); object1 = object; spotLight1.target = object; scene.add(spotLight1); }); }); |
Since there is no light added to the Scene
yet, the objects won’t be visible. We’ll now add lights to the Scene
.
To rotate the object, we need to add the following in our animate()
function. This will keep the item spinning in the y
axis.
1 2 3 4 5 6 |
if (object1 && object1.rotation) { object1.rotation.y = Math.cos( timer ) * 3; object1.updateMatrix(); } |
Adding lights in WebVR
We’re going to add three different lights to create a unique ambience.
- Ambient light
- Point light
- Spot light
Ambient light is used to light all the objects evenly. Point light acts like a light bulb and spreads light in all directions from a point. Spot light spreads light in given direction. We can specify the object as the direction target for the SpotLight and it’ll shine in that direction.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function createSpotlight(color) { var obj = new THREE.SpotLight(color, 2); obj.castShadow = true; obj.angle = Math.PI/5; obj.intensity = 2; obj.penumbra = 0.2; obj.decay = 2; obj.distance = 100; obj.shadow.mapSize.width = 1024; obj.shadow.mapSize.height = 1024; return obj; } var spotLight1 = createSpotlight(0xD8D862); |
Once the objects are present, we’ll add our text next to the objects. In this demo, we’re adding the title of the item and the price next to the object.
Adding text in WebVR
To add text, we’ll use TextGeometry
. Once we create our text mesh, we’ll position it next to the object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
var fontLoader = new THREE.FontLoader().load('helvetiker_regular.typeface.json', function(font) { // title of first item var name1Geo = new THREE.TextGeometry('Dress', { font: font, size: 0.3, bevelEnabled: false, height: 0 }); name1 = new THREE.Mesh(name1Geo, textMaterial); name1.position.set(1, 2, -10); scene.add(name1); // price of first item var price1Geo = new THREE.TextGeometry('$20', { font: font, size: 0.4, bevelEnabled: false, height: 0.1 }); price1 = new THREE.Mesh(price1Geo, textMaterial); price1.position.set(1, 1.3, -10); scene.add(price1); }); |
Once you have added all the elements, you should be able to see the items and pricing next to it.
Its your turn
Add more objects and create even more extensive collection of items for shopping. Don’t forget to share 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