Questions tagged [three.js]

Three.js is a lightweight cross-browser JavaScript library/API used to create and display animated 3D computer graphics on a Web browser. Three.js scripts may be used in conjunction with the HTML5 canvas element, SVG or WebGL.

Three.js allows the creation of GPU-accelerated 3D animations using the JavaScript language as part of a website without relying on proprietary browser plugins. This is possible thanks to the advent of WebGL.

Features

Three.js includes the following features:

  • Renderers: WebGL, WebGPU (experimental), SVG, CSS2D and CSS3D renderers.
  • Effects: Anaglyph, ASCII, outline, parallax barrier, stereo and peppers ghost.
  • Scenes: add and remove objects at run-time, fog, background, environment
  • Cameras: perspective, orthographic and stereo
  • Controllers: orbit, trackball, FPS, fly and more
  • Animation: armatures, forward kinematics, inverse kinematics, morph and keyframe
  • Lights: ambient, hemisphere, area, directional, point, spot lights and light probes
  • Shadows: cast and receive
  • Materials: PBR, Phong, Lambert, Toon, Matcap, Unlit and more
  • Shaders: access to full OpenGL Shading Language (GLSL)
  • Capabilities: lens flare, depth pass and extensive post-processing library
  • Objects: meshes, instanced mesh, point clouds, lines, sprites, ribbons, bones and more
  • Geometry: plane, cube, sphere, torus, 3D text and more
  • Modifiers: curve, edge split, simplify and tessellate
  • Data loaders: binary, image, JSON and scene
  • Utilities: full set of time and 3D math functions
  • including frustum, matrix, quaternion and more
  • Support: API documentation, public forum, chat and wiki in full operation
  • Examples: Over 350 files of coding examples plus fonts, models, textures, sounds and other support files
  • Debugging: Stats.js, Three.js Developer Tools

Three.js runs in all browsers supported by WebGL.

It was first released by Ricardo Cabello (mrdoob) to GitHub in April 2010 and is made available under the MIT license.

Example Code:

Renders an animated cube.

<html>
  <head>
    <title>My first Three.js app</title>
    <style>
      body {
        margin: 0;
      }
    </style>
  </head>
  <body>
    <script src="three.min.js"></script>
    <script>
      let camera, scene, renderer;
      let geometry, material, mesh;

      init();
      animate();

      function init() {

        camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 10 );
        camera.position.z = 1;

        scene = new THREE.Scene();

        geometry = new THREE.BoxGeometry( 0.2, 0.2, 0.2 );
        material = new THREE.MeshNormalMaterial();

        mesh = new THREE.Mesh( geometry, material );
        scene.add( mesh );

        renderer = new THREE.WebGLRenderer( { antialias: true } );
        renderer.setSize( window.innerWidth, window.innerHeight );
        document.body.appendChild( renderer.domElement );
      }

      function animate() {

        requestAnimationFrame( animate );

        mesh.rotation.x += 0.01;
        mesh.rotation.y += 0.02;

        renderer.render( scene, camera );
      }

    </script>
  </body>
</html>

Useful links:

16 questions
8
votes
2 answers

How can I offset/shrink a triangular polygon in GLSL?

I need to offset all (blue) triangles, each independently of the others, using the vertex-shader. In order to manipulate the triangle as a whole, I've created custom (vec3) attributes for each vertex (red) representing the leftward (purple) and…
Jack
  • 341
  • 1
  • 11
4
votes
1 answer

Which provides better intuition: THREE.Geometry or THREE.BufferGeometry?

THREE.js recently dropped support for THREE.Geometry in favor of exclusively THREE.BufferGeometry. I'm trying to decide which paradigm to teach in my computer graphics course to best provide students with an intuition for the field (I don't mind…
TomKern
  • 251
  • 1
  • 4
3
votes
1 answer

How to apply wire texture for realistic rendering of embroidery?

I am building an algorithm which takes a bitmap image as input and render the image as an embroidery design. The different steps are : Vectorize the image, I keep only 5 colors (Done) Generate stitches lines (Done) Apply wire embroidery…
pensfute
  • 131
  • 1
2
votes
0 answers

How to add HemisphereLight to a simple threejs scene?

I'm trying to add light and see its changes in a simple scene in threejs but no matter the intensity or the color I set for the light, I see no change in a scene. Actually, if I don't include the light code, nothing changes as well, so why is…
Jonas
  • 21
  • 1
2
votes
1 answer

Finding a texture pixel (X,Y) on a Sphere

I'm using three.js combined with face recognition, I want to rotate a sphere that displays my input video according to the detected eye location, the face recognition gives me a X,Y on the texture mapped to the sphere, what is the right way to find…
nirhere
  • 23
  • 3
1
vote
0 answers

How to use MTLLoader in THREE.js

I'm a student in computer science and I have a project where I need to create a 3d scene with a train. I loaded the model correctly but the texture is a .mtl file. So I looked the official documentation and I saw the MTLLoader. The problem is it's…
Marco87
  • 11
  • 1
1
vote
0 answers

How to hide a part of a geometry dynamically using a shader in three.js

I have a very big geometry in three.js and I want to hide a section of that geometry along an axis (for example every vertex with a Z>N must be hidden). My idea is to create a custom shader in GLSL that does something like: if vertex.z>n…
1
vote
0 answers

How can I see the whole shader's text content, with all the prepended code by Three.js, when using ShaderMaterial or RawShaderMaterial (r148)?

I know that Three.js prepends the shader text you provide with some code that includes definitions etc. I am not a big fan of "magic" and I would like to be able to see the final text content of the shader(s) that Three.js actually uses. I have seen…
Michael Kolesidis
  • 115
  • 1
  • 1
  • 8
1
vote
0 answers

How to make elevated parts of a steep plane seem darker than the lower surface? [GLSL]

I made a plane in THREEjs using Mesh, PlaneGeometry and ShaderMaterial. It's a very simple/basic form. I applied a simple phormula to make the plain more steep. Now I'm trying to make the lower surface darker than the higher surface. Here is what I…
Jonas
  • 111
  • 1
1
vote
0 answers

ThreeJS multicolor material

How can I have a multicolor material in ThreeJS? I'm looking for a way to have a single material and use one or more b/w masks to color the surface of the model with the same amount of colors; the masks will not blend/overlap each others, so no…
fudo
  • 121
  • 3
1
vote
0 answers

ThreeJS OutlinePass error on Vertex shader not compiled

Trying to replicate ThreeJS outline postprocessing example, but I'm facing an error on some uncompiled Vertex shader. I actually just copy/pasted source code with some small adjustment (removed the on-mouse-over detection in favor of an easier…
fudo
  • 121
  • 3
1
vote
0 answers

Blur calculation in shader with fixed CoC

I have just started learning how to use and create shaders in ThreeJS and, while going through some shaders in the example folder, I have come across a bokeh shader. Inside the shader, blur is computed using a formula, but I dont really understand…
1
vote
0 answers

Draw Line on Arbitrary Surface

I'm working on a 3D modeling tool and we use data from OpenLayers to construct a 3D mesh of some land. I want this behavior: you click on the mesh and it puts a point where you clicked on the mesh. Then, after the first point has been made, if you…
0
votes
1 answer

Using depth texture to mask local pixels

I have a scene with camera facing billboards drawn with depth write off so they appear on top of other objects. I would like to draw each sprite so that any obscured pixels are drawn with a very low opacity. So that obscured portions of each sprite…
0
votes
0 answers

Is there a concept for a cross platform GUI?

I have developed a C++ core application, which needs a relatively simple GUI, like a mind map. Now I wish to find a multipurpose solution for the visualization. So far my effort: three.js (webgl) in browser QT (qml) rendering three.js in a…
Andy
  • 9
  • 2
1
2