Basic THREE.js scene setup

share link

by vsasikalabe dot icon Updated: Mar 2, 2023

technology logo
technology logo

Solution Kit Solution Kit  

Scenes are used to set up the rendered object using three.js. We can place objects, lights, and cameras. A mesh object is created using geometry and a material. We need a scene, camera, and renderer to display anything with three.js. With a camera, we can render the scene.  


We will assign an object (const scene = new THREE.Scene(); const camera = new THREE.PerspectiveCamera()), if the geometry class for a rectangular cuboid with a given 'width', 'height', and 'depth'. The cuboid is created centered on the origin, with every edge parallel to one of the axes.  

The scene is a holder for everything that we can be able to see. 

  • Initial Setup 
  • Create the Scene 
  • Create the Camera 
  • Create a Visible Object 
  • Create the Renderer 
  • Render the Scene 

Import Classes from three.js 

  • Mesh 
  • MeshBasicMaterial 
  • PerspectiveCamera 
  • Scene 
  • WebGLRenderer 

To set the scene's background color, we'll also use the Color class: 

  • Color 

Animating the cube- cube.rotation.x += 0.01; 

                    cube.rotation.y += 0.01; 

The object will be run every frame (normally 60 times per second), and it gives a nice rotation animation to the cube. While the app runs, anything you want to move, or change must go through the animate loop.  


Here is an example of how to do a basic Three.js scene setup: 

Preview of the output that you will get on running this code from your IDE.

Code

Instructions

Follow the steps carefully to get the output easily.

  1. Download and Install NodeJS and VS Code on your Desktop.
  2. Open the new folder from your IDE and open the terminal,create react app using npx create-react-app filename.
  3. cd filename.Next install npm i three.
  4. Copy the code using the "Copy" button above, and paste it in your App.js file.(remove earlier code from App.js).
  5. Import THREE (import * as THREE from 'three';)
  6. Add export default animate to the end.
  7. From teriminal npm start to run the file.


I hope you found this useful. I have added version information in the following sections.


I found this code snippet by searching for ' Basic THREE.js scene setup' in kandi. You can try any such use case!

Environment Tested

I tested this solution in the following versions. Be mindful of changes when working with other versions.

  1. The solution is created in VS Code 1.73.1 version.
  2. Node Version v18.12.1
  3. react Version 18.2.0
  4. three Version 0.148.0


Using this solution, we are able to do basic THREE.js scene setup with simple steps. This process also facilities an easy way to use, hassle-free method to create a hands-on working version of code which would help us to do basic THREE.js scene setup.

Dependent Libraries

reactby facebook

JavaScript doticonstar image 209050 doticonVersion:v18.2.0doticon
License: Permissive (MIT)

The library for web and native user interfaces

Support
    Quality
      Security
        License
          Reuse

            reactby facebook

            JavaScript doticon star image 209050 doticonVersion:v18.2.0doticon License: Permissive (MIT)

            The library for web and native user interfaces
            Support
              Quality
                Security
                  License
                    Reuse

                      nodeby nodejs

                      JavaScript doticonstar image 95980 doticonVersion:v20.3.0doticon
                      License: Others (Non-SPDX)

                      Node.js JavaScript runtime :sparkles::turtle::rocket::sparkles:

                      Support
                        Quality
                          Security
                            License
                              Reuse

                                nodeby nodejs

                                JavaScript doticon star image 95980 doticonVersion:v20.3.0doticon License: Others (Non-SPDX)

                                Node.js JavaScript runtime :sparkles::turtle::rocket::sparkles:
                                Support
                                  Quality
                                    Security
                                      License
                                        Reuse

                                          If you do not have react and node.js that is required to run this code, you can install it by clicking on the above link .

                                          You can search for any dependent library on kandi like react and node.

                                          Support

                                          1. For any support on kandi solution kits, please use the chat
                                          2. For further learning resources, visit the Open Weaver Community learning page

                                          See similar Kits and Libraries