Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
ThreeJS basic scene
Although Three.js creates scene, camera and light as well as the object, the code syntax is little different.
AFrame declarative way to create a 3D scene
AFrame, simplifies creating a 3D scene by giving us a way to define our scene as html elements. Under the hood AFrame uses Three.js to do the same thing but allows you to change elements through attributes.
With a frame you can create a scene using <a-scene> html tag and nest the objects tags inside the scene. As a person who is used to working with Canvas and JavaScript, this is confusing to me but I can see why it is easier for some.
Good news is, you can use Three.js to further create interactions for your AFrame scenes.
Basic Scene with a cube
We will create our BabylonJS scene on Babylon Playground. Find out more about the editor on What is playground? chapter.
We will implement the 3D concepts we discussed in How to create a basic 3D scene? chapter.
To create a basic BabylonJS scene, we initialize a scene, create camera, light and a mesh and return the scene object.
Scene object gets engine as the input argument. You don't have to worry about the engine when you are working on the playground but when you are working on your local code sample, we will create the engine as well.
Arc Rotate Camera acts like a satellite in orbit around a target and always points towards the target position. In our case, it is pointing towards the box. Where ever you move your camera, it will still point to towards the box.
Arc Rotate Camera parameters are: name you want to give to your camera, alpha(radians) the longitudinal rotation, beta beta (radians) the latitudinal rotation, radius(the distance from the target position), target position(center where the box will be created as a default), scene(optional argument). In this code example scene is not given as an argument and defaults to the scene object on the playground.
Setting beta to 0 or PI can, for technical reasons, cause problems.
A hemispheric light is an easy way to simulate an ambient environment light. In our case, we are doing the bare minimum, just giving a name to the light and set it's location.
Setting y location to 1 while our main object is in the center (0,0,0) location will move the light above the object.
We are creating a Box Mesh by calling BABYLON.MeshBuilder.CreateBox method with the minimum requirements, a name and an options empty object.
Continue exploring how to create a 3D scene with other libraries
Keep working with BabylonJS and dive deeper into WebXR on the Babylon.js chapter:
Read more on BabylonJS documentation:
Browse community BabylonJS demos:
Checkout the example below to see how bump map creates more realistic results.
To see how to map real time tweet location data to your globe using web sockets, check out Tweet Migration project.
Lights can change our scenes drastically. Understanding lighting can help you create the effect you intend.
To create a basic scene we need to initialize a scene object. You can think of a scene as a stage that will hold your production. Everything that will be visible to the viewer will be added to the scene.
We need to add a camera object to the scene that will be our viewers perspective. Anything on the scene but not in the view of the camera won't be visible on your canvas.
We also need a light source for our objects to be visible.
Finally, we will create a basic shape(mesh), box.
Let's create the basic scene using different libraries.