ThreeJS Compatibiliy Check and Running Locally

Even though this is becoming less of an issue, some devices or browsers may still be incompatible with WebGL. The method below enables you to check if it is supported and display a message to the user if it is not.

Before attempting to render anything, add https://github.com/mrdoob/three.js/blob/master/examples/jsm/WebGL.js to your javascript and run the following.

if ( WEBGL.isWebGLAvailable() ) {


	// Initiate function or other initializations here
	animate();


} else {


	const warning = WEBGL.getWebGLErrorMessage();
	document.getElementById( 'container' ).appendChild( warning );


}

If you only use procedural geometries and do not load any textures, webpages should work directly from the file system; simply double-click on an HTML file in a file manager and it should appear working in the browser (you'll see file:/yourFile.html in the address bar).

Content Loaded from External Files

Loading models or textures from external files will fail with a security exception due to browsers' same origin policy security restrictions. Loading from a file system will succeed with a security exception.

There are two options for dealing with this:

  • In a browser, change the security settings for local files. This allows you to access your page using the following URL: file:/yourFile.html.
  • Files from a local web server can be executed. This allows you to access your page by typing http://localhost/yourFile.html.

If you choose option 1, be aware that you may expose yourself to vulnerabilities if you use the same browser for regular web browsing. To be safe, you should create a separate browser profile / shortcut for local development. Let's go over each option one at a time.

Run a Local Server

Many programming languages include basic HTTP servers. They are not as feature-rich as production servers like Apache or NGINX, but they should suffice for testing your three.js application.

Node.js five-server

Development server with live reload capability. To install:

# Remove live-server (if you have it)
npm -g rm live-server


# Install five-server
npm -g i five-server


# Update five-server (from time to time)
npm -g i five-server@latest
To run (from your local directory):


five-server . -p 8000

Node.js http-server

Node.js has a simple HTTP server package. To install:

npm install http-server -g

To run (from your local directory):

http-server . -p 8000

Python server

If you have Python installed, it should be enough to run this from a command line (from your working directory):

//Python 2.x
python -m SimpleHTTPServer
//Python 3.x
python -m http.server

This will serve files from the current directory at localhost under port 8000, i.e in the address bar type:

http://localhost:8000/

Ruby server

If you have Ruby installed, you can get the same result running this instead:

ruby -r webrick -e "s = WEBrick::HTTPServer.new(:Port => 8000, :DocumentRoot => Dir.pwd); trap('INT') { s.shutdown }; s.start"

PHP server

PHP also has a built-in web server, starting with php 5.4.0:

php -S localhost:8000

Lighttpd

Lighttpd is a very small general-purpose webserver. We'll go over how to install it on OSX with HomeBrew in this section. Lighttpd, unlike the other servers discussed here, is a full-fledged production-ready server.

1. Install it via homebrew

brew install lighttpd

2. Create a configuration file called lighttpd.conf in the directory where you want to run your webserver. There is a sample here.

3. In the conf file, change the server.document-root to the directory you want to serve files from.

4. Start it with

lighttpd -f lighttpd.conf

5. Navigate to http://localhost:3000/ and it will serve static files from the directory you chose.

IIS

If you use Microsoft IIS as your web server. Before loading, please add a MIME type setting for the.fbx extension.

File name extension: fbx        
MIME Type: text/plain

By default, IIS blocks .fbx, .obj files downloads. You have to configure IIS to enable these kinds of files can be downloaded.


Related Topics

ThreeJS Disposing an Object

The disposal of unwanted library entities is a crucial component of improving performance and avoiding memory leaks in your programme. When you create a three.js type instance, you set aside...

4 minutes read.

ThreeJS Scenes

Fog This class contains the parameters that define linear fog, i.e., that grows linearly denser with the distance. Constructor Fog( color : Integer, near : Float, far : Float ) The color parameter is...

3 minutes read.

ThreeJS BufferAttribute

This class stores data for an attribute associated with a BufferGeometry (such as vertex positions, face indices, normals, colours, UVs, and any custom attributes), allowing for more efficient data passing...

4 minutes read.

ThreeJS Raycaster

This class is intended to help with raycasting. Raycasting is used for a variety of purposes, including mouse picking (determining which objects in 3D space the mouse is over). Code Example const...

4 minutes read.

ThreeJS Updating Things

If an item has been added to the scene with the specified code, it will immediately update its matrices. const object = new THREE.Object3D(); scene.add( object ); or, if they are the child...

4 minutes read.

ThreeJS Cameras

ArrayCamera ArrayCamera can be used to render a scene using a preset group of cameras quickly. This is an important feature of VR scene rendering performance. An array of sub cameras is...

6 minutes read.

ThreeJS Loaders 2

ImageBitmapLoader A loader that converts an image to an ImageBitmap. An ImageBitmap provides an asynchronous and resource-efficient path for preparing textures for WebGL rendering. ImageBitmapLoader, unlike FileLoader, does not prevent multiple concurrent...

8 minutes read.

ThreeJS Shadows

LightShadow Serves as a base class for the other shadow classes. Constructor LightShadow( camera : Camera ) camera - the light's view of the world. Create a new LightShadow. This is not intended to be...

7 minutes read.

ThreeJS Uniform

Uniforms are global GLSL variables. They are passed to shader programs. Code Example When declaring a uniform of a ShaderMaterial, it is declared by value or by object. uniforms: { time: { value: 1.0...

2 minutes read.

ThreeJS Curve

An abstract base class for creating a Curve object with interpolation methods. CurvePath is an array of Curves. Constructor Curve() This constructor creates a new Curve. Properties .arcLengthDivisions : Integer This value determines the amount of...

13 minutes read.

ThreeJS Objects

Bone A bone that is part of a Skeleton. The skeleton in turn is used by the SkinnedMesh. Bones are almost identical to a blank Object3D. Code Example const root = new THREE.Bone(); const...

12 minutes read.

ThreeJS Keyframe Tracks and Animation

A KeyframeTrack is a timed series of keyframes that are made up of lists of times and related values and are used to animate an object's unique property. BooleanKeyframeTrack A Track of...

3 minutes read.

ThreeJS BufferGeometry

A mesh, line, or point geometry representation. Included within buffers are vertex positions, face indices, normals, colours, UVs, and custom attributes, lowering the cost of passing all of this data...

5 minutes read.

ThreeJS Keyframe Track

A KeyframeTrack is a timed series of keyframes that are made up of lists of times and related values and are used to animate an object's unique property. The "Animation System"...

4 minutes read.

ThreeJS Matrix Transformations

Three.js encodes 3D transformations using matrices for translations (position), rotations, and scaling. Every Object3D instance has a matrix that records the location, rotation, and scale of the object. This page...

2 minutes read.

ThreeJS AnimationClip

An AnimationClip is a group of keyframe tracks that may be reused to depict an animation. The "Animation System" page in the "Next Steps" part of the handbook provides an overview...

2 minutes read.

ThreeJS Material

Abstract base class for materials. Materials describe the appearance of objects. They are defined in a (mostly) renderer-independent way, so you don't have to rewrite materials if you decide to use...

6 minutes read.

ThreeJS Object3D

This is the base class for most three.js objects, and it provides a set of properties and methods for manipulating objects in 3D space. It should be noted that this can...

9 minutes read.

ThreeJS Property Binding and Mixer

PropertyBinding Internally, this holds a reference to real property in the scene graph. Constructor PropertyBinding( rootNode : Object3D, path, parsedPath ) -- rootNode: -- path -- parsedPath (optional) Properties The properties are as follows: .path : Number.parsedPath...

2 minutes read.

ThreeJS Lines and Texts

Drawing Lines Let's say you want to draw a line or a circle, not a wireframe Mesh. First, we need to set up the renderer, scene, and camera. Here is the...

3 minutes read.