Where’s waldo in WebVR tutorial
Where’s Waldo in VR
Today, we’re going to create Where’s Waldo in VR. We’ll hide waldo somewhere in our scene. We’ll have to look around to find him.
This is part of #DaysInVR series. View All VR Projects
Yesterday, we saw how to create night sky in VR. Today we’re on to day 9 of 30 VR projects in 30 days.
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 the panorama in VR
We’ll add a basic panorama to our scene. We’ll hide waldo in the pano. Adding the pano is similar to what we learnt from 360 photos in VR tutorial.
1 2 3 4 |
var texture = new THREE.TextureLoader().load('/common/pano_waldo.jpg'); var material = new THREE.MeshPhongMaterial({ map: texture, side: THREE.DoubleSide, color: 0xffffff }); |
We’re using MeshPhongMaterial
instead of MeshBasicMaterial
because of the way it reacts to light.
If you try viewing the scene now, you won’t be able to see anything since there is no light in the scene yet. MeshPhongMaterial
only works with a light in the scene.
Adding spotlight to the scene
We’ll use a spotlight to create a flashlight like effect so that only a small portion of the scene is visible.
1 2 3 4 5 6 7 8 9 10 11 12 |
var spotLight = new THREE.SpotLight(0xffffff, 1); spotLight.position.set( 0, 1, -20 ); spotLight.castShadow = true; spotLight.angle = Math.PI / 40; spotLight.penumbra = 0.05; spotLight.decay = 2; spotLight.distance = 200; spotLight.shadow.camera.near = 1; spotLight.shadow.camera.far = 200; scene.add(spotLight); |
Now we have a SpotLight
and you should be seeing a small circle that is visible.
You can add an AmbientLight
if you want to illuminate the entire scene.
1 2 3 4 |
var ambientLight = new THREE.AmbientLight(0xffffff, 0.3); scene.add(ambientLight); |
Moving spotlight with camera
Now we have to move the spotlight in the direction we’re looking. To do this, we’ll create an object
and keep the object in front of the camera. Then we’ll use the object's
position to keep the spotlight in the same direction as the camera.
1 2 3 4 5 6 |
var pivot = new THREE.Object3D(); scene.add(pivot); pivot.add(spotLight) |
We have to add the following code to animate
function, so that the direction of spotlight changes with the camera.
1 2 3 4 5 6 |
var direction = zaxis.clone(); direction.applyQuaternion(camera.quaternion); pivot.quaternion.setFromUnitVectors(zaxis, direction); pivot.position.copy(camera.position); |
Its your turn
Create your version of where’s waldo. Create a different scene with waldo in a different position. Play around with spotlight options and see how you can increase or decrease the difficulty of the game.
Share your creations or ask any questions, if any, 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