Fireplace in WebVR tutorial – Using Objects and Shaders in WebVR
Fireplace in VR – Using Shaders and Objects
To create our fireplace in virtual reality, we’ll divide our project into three sections.
- Adding 360 photo
- Adding fireplace object
- Adding fire using
ShaderMaterial
This is part of #DaysInVR series. View All VR Projects. Yesterday, we saw how to create solar system in VR. Today we’ll see how to create fireplace in VR. We’ll make use of shaders to create the fire and place it inside our fireplace object.
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 360 photo in WebVR
Adding 360 photo is pretty straight forward. We’ll use SphereGeometry
and MeshBasicMaterial
add 360 photo to our scene.
1 2 3 4 5 6 7 |
var panoTexture = new THREE.TextureLoader().load('Hall.jpg'); var panoGeometry = new THREE.SphereGeometry(500, 32, 32); var panoMaterial = new THREE.MeshBasicMaterial({ map: panoTexture, side: THREE.DoubleSide }); var pano = new THREE.Mesh(panoGeometry, panoMaterial); scene.add(pano); |
Adding object in WebVR
Now we have to add our fireplace object in the desired place. I used an object from tf3dm for this demo. I’m using JSONLoader
to load the object in JSON
format.
1 2 3 4 5 6 7 8 9 |
var loader = new THREE.AssimpJSONLoader(); loader.load('fireplace.json', function(object) { object.scale.multiplyScalar(0.4); object.position.set(20, controls.userHeight - 40, 25); object.rotateZ(Math.PI/180 * -120) scene.add(object); }); |
Using object.position
we have to set the position of the fireplace object to where we need.
Adding ShaderMaterial in WebVR
We’ll add our fire to our fireplace now. The fire is created by using fire shader. We’ll use ShaderMaterial
to create the material using the shader.
1 2 3 4 5 6 7 8 9 10 11 |
var fireMaterial = new THREE.ShaderMaterial({ defines : THREE.FireShader.defines, uniforms : THREE.UniformsUtils.clone( THREE.FireShader.uniforms ), vertexShader : THREE.FireShader.vertexShader, fragmentShader : THREE.FireShader.fragmentShader, transparent : true, depthWrite : false, depthTest : false }); |
Then we position the fire in our desired position, by adjusting the x,y,z
positions and add it to the scene.
1 2 3 4 5 6 7 |
// add fire var fireTexture = new THREE.TextureLoader().load('Fire.png'); var fire = new THREE.Fire(fireTexture); fire.position.set(3.5, controls.userHeight - 5, 5); scene.add(fire); |
Once the three components are added to the scene, you’ll be able to view a crackling fireplace in the scene you built.
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