Virtual Reality Image Gallery With Lighting Effects Tutorial
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
This is part of #DaysInVR series. View All VR Projects
Yesterday, we learnt how to create raining effect in VR on top of photos or videos along with sound.
VR Image Gallery
In this tutorial, we’ll see how to create VR image gallery with lighting effects which you can customize.
For this to work, we have to add two items to our VR scene
. We have to add our photos and we have add our lighting.
Adding photos in VR
We’re going to use BoxGeometry
and MeshLambertMaterial
to create our scene. We have used
MeshLambertMaterial
in our last tutorial to create a rainy VR scene. This type of material will reflect light. We’ll use this property to create some cool light effects later.
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 29 |
var geometry = new THREE.BoxGeometry(562, 562, 562, 1, 1, 1); for (var i = 0; i< images.length; i++){ textures.push(getTexture(images[i])) materials[i] = new THREE.MeshLambertMaterial({ map: textures[i], side: THREE.DoubleSide}); } function getTexture(imgUrl) { var texture = new THREE.Texture(); var imgObj = new Image(); imgObj.onload = function() { canvas = document.createElement('canvas'); context = canvas.getContext('2d'); canvas.height=imgObj.height + 1000; canvas.width= imgObj.width + 1000; context.drawImage(imgObj, (canvas.width - this.width) * 0.5, (canvas.height - this.height) * 0.5); texture.image = canvas; texture.minFilter = THREE.LinearFilter; texture.wrapS = THREE.RepeatWrapping; texture.needsUpdate = true; texture.repeat.x = - 1; } imgObj.crossOrigin = 'anonymous'; imgObj.src = imgUrl; return texture; } |
Adding light to our scene
Next, we have to add light to our scene. If you ran the project without adding any light to the scene, you’ll notice nothing can be seen. This is because, MeshLambertMaterial
will only be visible with light. Try changing the material to MeshBasicMaterial
and you can see the images without adding any light. But you can’t add any light effects either, because it doesn’t react to light.
1 2 3 4 5 6 7 8 9 10 11 |
var light1 = new THREE.PointLight( 0xff0040 ); light1.position.set(0, 0, 0); scene.add( light1 ); var light2 = new THREE.PointLight( 0x0040ff); light2.position.set(0, 0, 0); scene.add( light2 ); var light3 = new THREE.PointLight( 0x80ff80); light3.position.set(0, 0, 0); scene.add( light3 ); |
We’re using PointLight
to illuminate our scene. PointLight
will emit light in all directions. As you can see from the code, we’re adding 3 PointLights
with different colors to our scene.
For adding some color changing effects, we’re going to change the intensity of some lights along with time.
1 2 3 4 5 |
var time = Date.now() * 0.0007; light1.intensity = Math.sin( time * 0.7 ); light2.intensity = Math.cos( time * 0.8 ); |
The above code has to be added to your animate()
function. When it is run, light1
and light2
intensity will be changing along a sine curve. The combined effect of all the lights gives us the cool effect we see in the demo. You can try changing the values and see what effects you can come up with.
Its your turn
Try adding your own pictures or pull images from Twitter and Instagram and make a VR image gallery. Add effects of your own with lights. 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