Virtual tour in WebVR tutorial using Hotspots
Virtual tour in WebVR using hotspots
In this tutorial, we’ll use Raycaster
, Clock
and SphereGeometry
to create a hotspots and virtual tour in VR. Once you select the hotspot, based on the time you set, you can navigate to a different panorama.
This is part of #DaysInVR series. View All VR Projects. Yesterday, we learnt how to use raycasting in WebVR to select objects. Today, we’ll use similar principles to create virtual tour in webvr
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 panoramas in VR
We’ll add one panorama to our default scene and set material.map.needsUpdate = true;
so that we can update the texture later.
1 2 3 4 5 6 7 8 9 10 11 |
var geometry = new THREE.SphereGeometry(500, 60, 40); var loader = new THREE.TextureLoader(); var texture = loader.load('/common/pano.jpg'); var material = new THREE.MeshBasicMaterial({ map: texture, side: THREE.DoubleSide }); material.map.needsUpdate = true; var mesh = new THREE.Mesh(geometry, material); mesh.position.set(0, controls.userHeight, -5); scene.add(mesh); |
Adding raycaster in VR
We’ll use raycaster that move along with the camera as our cursor for selecting the hotspot.
1 2 3 4 5 |
var raycaster = new THREE.Raycaster(); var arrow = new THREE.ArrowHelper( raycaster.ray.direction, raycaster.ray.origin, 100, Math.random() * 0xffffff ); scene.add( arrow ); |
We also need to add the following in the animate
function so that the raycaster
updates with camera movement.
1 2 3 4 5 6 7 |
// update the position of arrow arrow.setDirection(raycaster.ray.direction); // update the raycaster raycaster.set(camera.getWorldPosition(), camera.getWorldDirection()); |
Adding hotspots in WebVR
To add hotspots, we’ll use SphereGeometry
and then use Raycaster
to select the hotspots.
1 2 3 4 5 6 7 8 |
var sphereGeometry = new THREE.SphereGeometry( .3, 32, 32 ); var sphereMaterial = new THREE.MeshBasicMaterial({color: 0xffff00, side: THREE.DoubleSide}); var sphere = new THREE.Mesh( sphereGeometry, sphereMaterial ); sphere.name = 'hotspot1'; sphere.position.set(-1, controls.userHeight, -5); scene.add(sphere); |
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 27 28 |
// intersect with all scene meshes. var intersects = raycaster.intersectObjects(hotspots); if (intersects.length > 0) { if (!clock.running) clock.start(); var scale = Math.abs(Math.sin(delta/5000*Math.PI)) * 2; if (scale < 1) { scale = 1; } intersects[1].object.scale.set(scale, scale, scale); if (updateTexture < 1) { if (clock.getElapsedTime() > 2) { updateTexture = 1; var newTexture = loader.load(panoList[Math.floor(Math.random()*panoList.length)]); material.map = newTexture; sphere.position.x = Math.floor(Math.random() * 7) + 1 sphere.position.y = Math.floor(Math.random() * 7) + 0 } } } else { clock.stop(); updateTexture = 0; for (var i = 0; i < hotspots.length; i ++) { hotspots[i].scale.set(1, 1, 1); } } |
Once our raycaster intersects with the hotspot, we’ll start a scaling animation so that we know it’s being interacted with. A clock is also started, so that, once it hits 2 seconds, we’ll load our new texture. Then the new texture is applied to the sphere
material.
The position of the hotspot is randomly changed for this demo. And a new texture is randomly selected each time you try to navigate through the hotspot.
Its your turn
Try adding more panos and textures, and add more hotspots. Create your own virtual tour in WebVR. Don’t forget to share your 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