Three.js – Hello Cube App ”; Previous Next Like any other programming language, let”s start learning Three.js by creating “Hello cube!” app. The HTML <!DOCTYPE html> <html> <head> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <meta charset=”UTF-8″ /> <title>Three.js – Hello cube</title> <style> /* Our CSS goes here */ </style> <script src=”https://cdnjs.cloudflare.com/ajax/libs/three.js/r127/three.min.js”></script> </head> <body> <div id=”threejs-container”> <!– Our output to be rendered here → </div> <script type=”module”> // our JavaScript code goes here </script> </body> </html> As you can see, it”s just a simple HTML file with Three.js CDN. The CSS <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, Roboto, Oxygen, Ubuntu, Cantarell, “Open Sans”, “Helvetica Neue”, sans-serif; } html, body { height: 100vh; width: 100vw; } #threejs-container{ position: block; width: 100%; height: 100%; } </style> The above CSS is just the basic styling of the HTML page. The threejs-container takes up the whole screen. The JavaScript This is where our three.js app comes into life. The code below renders a single cube in the middle of the screen. All these codes will go into the empty <script> tag in the HTML. const width = window.innerWidth const height = window.innerHeight // Scene const scene = new THREE.Scene() scene.background = new THREE.Color(”#00b140”) // Camera const fov = 45 // AKA Field of View const aspect = window.innerWidth / window.innerHeight const near = 0.1 // the near clipping plane const far = 100 // the far clipping plane const camera = new PerspectiveCamera(fov, aspect, near, far) camera.position.set(0, 0, 10) // Renderer const renderer = new THREE.WebGLRenderer() renderer.setSize(window.innerWidth, window.innerHeight) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // Creating a cube const geometry = new THREE.BoxGeometry(2, 2, 2) const material = new THREE.MeshBasicMaterial({ wireframe: true }) const cube = new THREE.Mesh(geometry, material) scene.add(cube) // Rendering the scene const container = document.querySelector(”#threejs-container”) container.append(renderer.domElement) renderer.render(scene, camera) Let”s discuss the code one step at a time, and then you can get more information about each element in the upcoming chapters. The first thing we need to do is to create a scene, a camera, and a renderer. These are the essential components that make up every Three.js app. The Scene const scene = new THREE.Scene() scene.background = new THREE.Color(”#262626”) The scene serves as the container for everything we can see on the screen, without a THREE.Scene object, Three.js cannot render anything. The background color is dark gray so that we can see the cube. The Camera const camera = new PerspectiveCamera(fov, aspect, near, far) camera.position.set(0, 0, 10) The camera object defines what we’ll see when we render a scene. There are not many but different types of cameras, but for this example, you’ll use a PerspectiveCamera, which matches the way our eyes see the world. The Renderer const renderer = new THREE.WebGLRenderer() renderer.setSize(window.innerWidth, window.innerHeight) The renderer object is responsible for calculating what the scene looks like in the browser, based on the camera. There are different types of renderers, but we mainly use WebGLRenderer since most browsers support WebGL. In addition to creating the renderer instance, we also need to set the size at which we want it to render our app. It”s a good idea to use the width and height of the area we want to fill with our app The Cube- in this case, the width and height of the browser window. The Cube const geometry = new THREE.BoxGeometry(2, 2, 2) const material = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true, }) const cube = new THREE.Mesh(geometry, material) scene.add(cube) The above code creates a simple cube at the center of the screen. We can make any object using THREE.Mesh. The Mesh takes two objects, geometry and material. The geometry of a mesh defines its shape, and materials determine the surface properties of objects. To create a cube, we need BoxGeometry and a primary material (MeshBasicMaterial) with the color 0xffffff. If the wireframe property is set to true, it tells Three.js to show us a wireframe and not a solid object. Rendering the Scene const container = document.querySelector(”#threejs-container”) container.append(renderer.domElement) renderer.render(scene, camera) Example Last but not least, we add the renderer element to our HTML document. The renderer uses an <canvas> element to display the scene to us. In this case, the renderer appends the <canvas> element to the reference container in the HTML. hello-cube-app.html Live Demo <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta http-equiv=”X-UA-Compatible” content=”ie=edge” /> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ /> <title>Three.js – Hello cube</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: -applesystem, BlinkMacSystemFont, ”Segoe UI”, Roboto, Oxygen, Ubuntu, Cantarell, ”Open Sans”, ”Helvetica Neue”, sans-serif; } html, body { height: 100vh; overflow: hidden; width: 100vw; } #threejs-container { position: block; width: 100%; height: 100%; } </style> <script src=”https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js”></script> </head> <body> <div id=”threejs-container”></div> <script type=”module”> // Hello Cube App // Your first Three.js application // sizes const width = window.innerWidth const height = window.innerHeight // scene const scene = new THREE.Scene() scene.background = new THREE.Color(0x262626) // camera const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 100) camera.position.set(0, 0, 10) // cube const geometry = new THREE.BoxGeometry(2, 2, 2) const material = new THREE.MeshBasicMaterial({ color: 0xffffff, wireframe: true }) const cube = new THREE.Mesh(geometry, material) scene.add(cube) // renderer const renderer = new THREE.WebGL1Renderer() renderer.setSize(width, height) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // rendering the scene const container = document.querySelector(”#threejs-container”) container.append(renderer.domElement) renderer.render(scene, camera) </script> </body> </html> Output The output looks like this if everything is working correctly. Play around with the code to get a better understanding of how it works. You have now completed creating your first three.js application. Let”s go ahead and add more beauty to the app. Print Page Previous Next Advertisements ”;
Category: threejs
Three.js – Quick Guide
Three.js – Quick Guide ”; Previous Next Three.js – Introduction All modern browsers became more powerful and more accessible directly using JavaScript. They have adopted WebGL (Web Graphics Library), a JavaScript API, which allows you to render highperformance interactive 3D and 2D graphics within any compatible web browser using the capabilities of the GPU (Graphics Processing Unit). But WebGL is a very low-level system that only draws basic objects like point, square, and line. However, programming WebGL directly from JavaScript is a very complex and verbose process. You need to know the inner details of WebGL and learn a complex shader language to get the most out of WebGL. Here comes Three.js to make your life easy. What is Three.js? Three.js is an open-source, lightweight, cross-browser, general-purpose JavaScript library. Three.js uses WebGL behind the scenes, so you can use it to render Graphics on an HTML <canvas> element in the browser. Since Three.js uses JavaScript, you can interact with other web page elements, add animations and interactions, and even create a game with some logic. Why use Three.js? The following features make Three.js an excellent library to use. You can create complex 3D graphics by just using JavaScript. You can create Virtual Reality (VR) and Augmented Reality (AR) scenes inside the browser. Since it uses WebGL, it has cross-browser support. Many browsers support it. You can add various materials, textures and animate 3D objects. You can also load and work on objects from other 3D modeling software. With a couple of lines of JavaScript and simple logic, you can create anything, from highperformance interactive 3D models to photorealistic real-time scenes. These are some excellent websites created using Three.js− Moments of happiness Garden Eight Interland Under Neon Lights You can find many other examples on the official website of three.js Browser Support All modern browsers on desktop, as well as on mobile, currently support WebGL. The only browser where you have to take care of is the mobile Opera Mini browser. For IE 10 and older,there is the IEWebGL plugin, which you can get from https://github.com/iewebgl/iewebgl./ You can find detailed information about the WebGL browser support here. Once you understand what Three.js is, you can continue to the next chapter about setting up a project to start working with Three.js. Three.js – Installation There are many ways to include Three.js in your project. You can use any of these following methods to get started using Three.js. Then open your favorite code editor and get going. Download the complete Three.js project Download the complete Three.js project into your system. You can download it here or from GitHub. Extract the three.js-master.zip file and look inside the build folder. You can find two three.js, three.min.js, which is just a minified version. Add any of these two files into your project folder and link them to your HTML file. Now you are good to use Three.js in your project. Note − We recommend using the minified version as it loads faster. Insert the following <script> tag into the <head> element of your HTML with a path to the threejs.min.js file. <script src=”/path/to/threejs.min.js”></script> Use CDN links You can link the files from a CDN (Content Delivery Network), a remote site dedicated to hosting files so that you can use them online. You can use any of these websites − cdnjs.com jsdelivr.com Insert any of the following <script> tags into the <head> element of your HTML. <script src=”https://cdnjs.cloudflare.com/ajax/libs/three.js/r127/three.min.js”></script> or <script src=”https://cdn.jsdelivr.net/npm/[email protected]/build/three.min.js”></script> Install the package of Three.js Three.js is also available as a package on NPM.If you have Node.js set up on your computer, you can install it using npm or yarn. npm install three or yarn add three Then, you can import Three.js from the three.module.js file into your JavaScript file import * as THREE from ”three” You can use Three.js along with any JavaScript framework like React, Angular, Vue. Once you finish setting up your project, let”s start creating. Three.js – Hello Cube App Like any other programming language, let”s start learning Three.js by creating “Hello cube!” app. The HTML <!DOCTYPE html> <html> <head> <meta name=”viewport” content=”width=device-width, initial-scale=1″ /> <meta charset=”UTF-8″ /> <title>Three.js – Hello cube</title> <style> /* Our CSS goes here */ </style> <script src=”https://cdnjs.cloudflare.com/ajax/libs/three.js/r127/three.min.js”></script> </head> <body> <div id=”threejs-container”> <!– Our output to be rendered here → </div> <script type=”module”> // our JavaScript code goes here </script> </body> </html> As you can see, it”s just a simple HTML file with Three.js CDN. The CSS <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: -apple-system, BlinkMacSystemFont, “Segoe UI”, Roboto, Oxygen, Ubuntu, Cantarell, “Open Sans”, “Helvetica Neue”, sans-serif; } html, body { height: 100vh; width: 100vw; } #threejs-container{ position: block; width: 100%; height: 100%; } </style> The above CSS is just the basic styling of the HTML page. The threejs-container takes up the whole screen. The JavaScript This is where our three.js app comes into life. The code below renders a single cube in the middle of the screen. All these codes will go into the empty <script> tag in the HTML. const width = window.innerWidth const height = window.innerHeight // Scene const scene = new THREE.Scene() scene.background = new THREE.Color(”#00b140”) // Camera const fov = 45 // AKA Field of View const aspect = window.innerWidth / window.innerHeight const near = 0.1 // the near clipping plane const far = 100 // the far clipping plane const camera = new PerspectiveCamera(fov, aspect, near, far) camera.position.set(0, 0, 10) // Renderer const renderer = new THREE.WebGLRenderer() renderer.setSize(window.innerWidth, window.innerHeight) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // Creating a cube const geometry = new THREE.BoxGeometry(2, 2,
Three.js – Lights & Shadows
Three.js – Lights & Shadows ”; Previous Next Lights make the objects visible, similarly, in Three.js THREE.Light lights up the scene and makes some things visible. Not all materials are affected by lighting. The MeshBasicMaterial and MeshNormalMaterial are self-illuminating, so they don”t need lighting to be visible within a scene. However, most of the other materials do, the MeshLambertMaterial, MeshPhongMaterial, MeshStandardMaterial, MeshPhysicalMaterial, and MeshToonMaterial. We”ll discuss more materials in further chapters. In this chapter, we”ll focus on different types of lights in Three.js. Every light has color and intensity properties. color − (optional) hexadecimal color of the light. Default is 0xffffff (white). intensity − (optional) numeric value of the light”s strength/intensity. Default is 1. Casting Shadows The light that is coming from a specific direction can cast shadows. First, we should make the scene ready for casting shadows. Step − 1 We should first tell the renderer that we want to enable shadows. Casting shadows is an expensive operation. WebGLRenderer only supports this functionality. It uses Shadow mapping, a technique specific to WebGL, performed directly on the GPU. renderer.shadowMapEnabled = true The above line of code tells the renderer to cast shadows in the scene. Note − Three.js, by default, uses shadow maps. Shadow map works for light that casts shadows. The scene renders all objects marked to cast shadows from the point of view of the light. If your shadow looks a bit blocky around its edges, it means the shadow map is too small. To increase the shadow map size, you can define shadowMapHeight and shadowMapWidht properties for the light. Alternatively, you can also try to change the shadowMapType property of WebGLRenderer. You can set this to THREE.BasicShadowMap, THREE.PCFShadowMap, or THREE.PCFSoftShadowMap. // to antialias the shadow renderer.shadowMapType = THREE.PCFSoftShadowMap // or directionalLight.shadowMapWidth = 2048 directionalLight.shadowMapHeight = 2048 Step − 2 You should configure objects to cast shadows. You can inform Three.js which objects can cast shadows and which objects can receive shadows. object.castShadow = true object.recieveShadow = true Step − 3 All the above steps are the same for every light. The next step is to set up the shadow-related properties. light.castShadow = true light.shadow.camera.near = 10 light.shadow.camera.far = 100 light.shadow.camera.left = -50 light.shadow.camera.right = 50 light.shadow.camera.top = 50 light.shadow.camera.bottom = -50 The first property, castShadow, tells Three.js that this light casts shadows. As casting shadows is an expensive operation, we need to define the area where shadows can appear. You can do it with the shadow.camera.near, shadow.camera.far, and shadow.camera.left, etc. properties. With the above properties, we create a box-like area where Three.js render shadows. Example Explore more in this example. directional.html <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta http-equiv=”X-UA-Compatible” content=”ie=edge” /> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ /> <title>Three.js – Directional Light</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: -applesystem, BlinkMacSystemFont, ”Segoe UI”, Roboto, Oxygen, Ubuntu, Cantarell, ”Open Sans”, ”Helvetica Neue”, sans-serif; } html, body { height: 100vh; width: 100vw; } #threejs-container { position: block; width: 100%; height: 100%; } </style> <script src=”https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js”></script> <script src=”https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.js”></script> </head> <body> <div id=”container”></div> <script type=”module”> // Adding directional light to the scene // The lights falls from the light only in one direction. // You can see the position of light using helpers provided in Three.j s for debugging purposes // GUI const gui = new dat.GUI() // sizes let width = window.innerWidth let height = window.innerHeight // scene const scene = new THREE.Scene() scene.background = new THREE.Color(0x262626) // camera const camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 1000) camera.position.set(0, 0, 10) const camFolder = gui.addFolder(”Camera”) camFolder.add(camera.position, ”z”, 10, 80, 1) camFolder.open() // lights const ambientLight = new THREE.AmbientLight(0xffffff, 0.5) scene.add(ambientLight) const light = new THREE.DirectionalLight() light.position.set(2.5, 2, 2) light.castShadow = true light.shadow.mapSize.width = 512 light.shadow.mapSize.height = 512 light.shadow.camera.near = 0.5 light.shadow.camera.far = 100 scene.add(light) const helper = new THREE.DirectionalLightHelper(light) scene.add(helper) // light controls const lightColor = { color: light.color.getHex() } const lightFolder = gui.addFolder(”Directional Light”) lightFolder.addColor(lightColor, ”color”).onChange(() => { light.color.set(lightColor.color) }) lightFolder.add(light, ”intensity”, 0, 1, 0.01) lightFolder.open() const directionalLightFolder = gui.addFolder(”Position of Light”) directionalLightFolder.add(light.position, ”x”, -10, 10, 0.1) directionalLightFolder.add(light.position, ”y”, -10, 10, 0.1) directionalLightFolder.add(light.position, ”z”, -10, 10, 0.1) directionalLightFolder.open() // plane const planeGeometry = new THREE.PlaneGeometry(100, 20) const plane = new THREE.Mesh(planeGeometry, new THREE.MeshPhongMateria l({ color: 0xffffff })) plane.rotateX(-Math.PI / 2) plane.position.y = -1.75 plane.receiveShadow = true scene.add(plane) // cube const geometry = new THREE.BoxGeometry(2, 2, 2) const material = new THREE.MeshStandardMaterial({ color: 0x87ceeb }) const materialFolder = gui.addFolder(”Material”) materialFolder.add(material, ”wireframe”) materialFolder.open() const cube = new THREE.Mesh(geometry, material) cube.position.set(0, 0.5, 0) cube.castShadow = true cube.receiveShadow = true scene.add(cube) // responsiveness window.addEventListener(”resize”, () => { width = window.innerWidth height = window.innerHeight camera.aspect = width / height camera.updateProjectionMatrix() renderer.setSize(window.innerWidth, window.innerHeight) renderer.render(scene, camera) }) // renderer const renderer = new THREE.WebGL1Renderer() renderer.setSize(window.innerWidth, window.innerHeight) renderer.shadowMap.enabled = true renderer.shadowMap.type = THREE.PCFSoftShadowMap renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // animation function animate() { requestAnimationFrame(animate) cube.rotation.x += 0.005 cube.rotation.y += 0.01 renderer.render(scene, camera) } // rendering the scene const container = document.querySelector(”#container”) container.append(renderer.domElement) renderer.render(scene, camera) animate() </script> </body> </html> Output Sr.No Lights & Description 1 Ambient Light It is the most basic light, which illuminates the whole scene equally. 2 Directional Light Directional light comes from a specific point and is emitted directly from far away to the target. 3 Spotlight It is another kind of light that comes from a specific direction in the shape of the cone. 4 Point Light The point light is a light source that emits light in all directions from a single point. 5 Hemisphere Light It is a special light for creating natural lighting. Print Page
Three.js – Textures
Three.js – Textures ”; Previous Next The texture is an image or color added to the material to give more detail or beauty. The texture is an essential topic in Three.js. In this section, we”ll see how to apply a basic texture to our material. Basic Texture First, you should create a loader. Three.js has a built-in function TextureLoader() to load textures into your Three.js project. Then you can load any texture or image by specifying its path in the load() function. const loader = new THREE.TextureLoader() texture.load(”/path/to/the/image”) Then, set the map property of the material to this texture. That”s it; you applied a texture to the plane geometry. Textures have settings for repeating, offsetting, and rotating a texture. By default, textures in three.js do not repeat. There are two properties, wrapS for horizontal wrapping and wrapT for vertical wrapping to set whether a texture repeats. And set the repeating mode to THREE.ReaptWrapping. texture.wrapS = THREE.RepeatWrapping texture.wrapT = THREE.RepeatWrapping texture.magFilter = THREE.NearestFilter In Three.js, you can choose what happens both when the texture is drawn larger than its original size and what happens when it”s drawn smaller than its original size. For setting the filter, when the texture is larger than its original size, you set texture.magFilter property to either THREE.NearestFilter or THREE.LinearFilter. NearestFilter − This filter uses the color of the nearest texel that it can find. LinearFilter − This filter is more advanced and uses the color values of the four neighboring texels to determine the correct color. And, you can add how many times to repeat the texture. const timesToRepeatHorizontally = 4 const timesToRepeatVertically = 2 texture.repeat.set(timesToRepeatHorizontally, timesToRepeatVertically) Example Check out the following example. texture.html Live Demo <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta http-equiv=”X-UA-Compatible” content=”ie=edge” /> <meta name=”viewport” content=”width=device-width, initial-scale=1.0″ /> <title>Three.js – Checker Board</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; font-family: -applesystem, BlinkMacSystemFont, ”Segoe UI”, Roboto, Oxygen, Ubuntu, Cantarell, ”Open Sans”, ”Helvetica Neue”, sans-serif; } html, body { height: 100vh; width: 100vw; } #threejs-container { position: block; width: 100%; height: 100%; } </style> <script src=”https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js”></script> <script src=”https://cdnjs.cloudflare.com/ajax/libs/dat-gui/0.7.7/dat.gui.js”></script> </head> <body> <div id=”threejs-container”></div> <script type=”module”> // Creating a checker-board using Textures // applying the texture to 2d plane geometry // GUI const gui = new dat.GUI() // sizes let width = window.innerWidth let height = window.innerHeight // scene const scene = new THREE.Scene() scene.background = new THREE.Color(0x262626) // camera const camera = new THREE.PerspectiveCamera(75, width / height, 0.1, 100) camera.position.set(0, 0, 10) const camFolder = gui.addFolder(”Camera”) camFolder.add(camera.position, ”z”).min(10).max(60).step(10) camFolder.open() // Light const ambientLight = new THREE.AmbientLight(0xffffff, 1) scene.add(ambientLight) // texture const planeSize = 10 const loader = new THREE.TextureLoader() const texture = loader.load(” https://cloud-nfpbfxp6x-hack-clubbot.vercel.app/0height.png ”) texture.wrapS = THREE.RepeatWrapping texture.wrapT = THREE.RepeatWrapping texture.magFilter = THREE.NearestFilter const repeats = planeSize / 2 texture.repeat.set(repeats, repeats) class StringToNumberHelper { constructor(obj, prop) { this.obj = obj this.prop = prop } get value() { return this.obj[this.prop] } set value(v) { this.obj[this.prop] = parseFloat(v) } } const wrapModes = { ClampToEdgeWrapping: THREE.ClampToEdgeWrapping, RepeatWrapping: THREE.RepeatWrapping, MirroredRepeatWrapping: THREE.MirroredRepeatWrapping } function updateTexture() { texture.needsUpdate = true } gui .add(new StringToNumberHelper(texture, ”wrapS”), ”value”, wrapModes) .name(”texture.wrapS”) .onChange(updateTexture) gui .add(new StringToNumberHelper(texture, ”wrapT”), ”value”, wrapModes) .name(”texture.wrapT”) .onChange(updateTexture) gui.add(texture.repeat, ”x”, 0, 5, 0.01).name(”texture.repeat.x”) gui.add(texture.repeat, ”y”, 0, 5, 0.01).name(”texture.repeat.y”) // plane for board const geometry = new THREE.PlaneGeometry(planeSize, planeSize) const material = new THREE.MeshPhongMaterial({ map: texture, side: THREE.DoubleSide }) const board = new THREE.Mesh(geometry, material) board.position.set(0, 0, 0) scene.add(board) // responsiveness window.addEventListener(”resize”, () => { width = window.innerWidth height = window.innerHeight camera.aspect = width / height camera.updateProjectionMatrix() renderer.setSize(window.innerWidth, window.innerHeight) renderer.render(scene, camera) }) // renderer const renderer = new THREE.WebGL1Renderer() renderer.setSize(width, height) renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2)) // animation function animate() { requestAnimationFrame(animate) renderer.render(scene, camera) } // rendering the scene const container = document.querySelector(”#threejs-container”) container.append(renderer.domElement) renderer.render(scene, camera) console.log(scene.children) animate() </script> </body> </html> Output Texture Mapping base color map It is the basic colored image you add to the object to the texture. With a base color map we add colors to the surface. const textureMap = new THREE.TextureLoader().load(”/path/to/texture-map”) material.map = textureMap You can add the effect of depth using a bump map or normal map or distance map. bump map A bump map is a grayscale image, where the intensity of each pixel determines the height. You can just set the material bumpMap property to the texture. It adds fine details to the texture. const textureBumpMap = new THREE.TextureLoader().load(”/path/to/bump-map”) material.bumpMap = textureBumpMap Normal Maps A normal map describes the normal vector for each pixel, which should be used to calculate how light affects the material used in the geometry. It creates an illusion of depthness to the flat surface. const textureNormalMap = new THREE.TextureLoader().load(”/path/to/normal-map”) material.normalMap = textureNormalMap Displacement Map While the normal map gives an illusion of depth, we change the model”s shape, with a displacement map based on the information from the texture. const textureDisplacementMap = new THREE.TextureLoader().load( ”/path/to/displacement-map” ) material.displacemetMap = textureDisplacementMap Roughness Map The roughness map defines which areas are rough and that affects the reflection sharpness from the surface. const textureRoughnessMap = new THREE.TextureLoader().load( ”/path/to/roughness-map” ) material.roughnessMap = textureRoughnessMap Ambient Occlusion Map It highlights the shadow areas of the object. It requires a second set of UVs. const textureAmbientOcclusionMap = new THREE.TextureLoader().load( ”/path/to/AmbientOcclusion-map” ) material.aoMap = textureAmbientOcclusionMap // second UV mesh.geometry.attributes.uv2 = mesh.geometry.attributes.uv If you compare the objects with roughness map and ambient occlusion map, you can observe that The shadows are more highlighted after using aoMap. Metalness Map It defines how much the material is like a metal. const textureMetalnessMap = new THREE.TextureLoader().load( ”/path/to/metalness-map” ) material.metalnessMap = textureMetalnessMap Example Now, check out the following example texture-maps.html <!DOCTYPE html> <html lang=”en”> <head> <meta charset=”UTF-8″ /> <meta http-equiv=”X-UA-Compatible” content=”ie=edge”
Three.js – Home
Three.js Tutorial PDF Version Quick Guide Resources Job Search Discussion Three.js is an open-source JavaScript library that you can use to create dynamic and interactive websites with 2D and 3D graphics. With Three.js, you can render 3D graphics directly inside the browser. You can do fantastic stuff using Three.js by adding animations or logic and even turning your website into a game. Ricardo Cabello (or mrdoob in GitHub) released Three.js in 2010 and maintained a great open-source community. Audience This tutorial is for anyone who already knows JavaScript and wants to create 3D graphics that run in any browser. This tutorial makes you comfortable in getting started with Three.js and WebGL. Prerequisites Creating 3D applications that run in a browser falls at the intersection of web development and computer graphics. You don’t need to know anything about computer graphics or advanced math; all that is required is a general understanding of HTML, CSS, and JavaScript. If you are just getting started with JavaScript, I recommend completing this tutorial before proceeding with this one Print Page Previous Next Advertisements ”;