20

When I run my javascript code. I get the following error "Uncaught ReferenceError: THREE is not defined". The line mentioned is:

var renderer = new THREE.WebGLRenderer();

// I have attached the three.js library in the script tag. I don't know what seems to be problem.

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera(45, window.innerWidth/window.innerHeight, 0.1, 1000);
camera.position.set = (0, 0, 10);
camera.lookAt(camera.position);
scene.add(camera);

var geometry = new THREE.Geometry();
geometry.vertices.push(new THREE.Vector3(0.0, 1.0, 0.0));
geometry.vertices.push(new THREE.Vector3(-1.0, -1.0, 0.0));
geometry.vertices.push(new THREE.Vector3(1.0, -1.0, 0.0));
geometry.faces.push(new THREE.Face3(0, 1, 2));

var material = new THREE.BasicMeshMaterial({
    color: 0xFFFFFF,
    side: THREE.DoubleSide
});

var mesh = new THREE.Mesh(geometry, material);
mesh.position.set(-1.5, 0.0, 4.0);
scene.add(mesh);

function render() {
    renderer.render(scene, camera);
}

render();
0

6 Answers 6

17

You need to include three.js before

<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
Sign up to request clarification or add additional context in comments.

3 Comments

after adding the about script src, I am getting this error now! "Uncaught TypeError: THREE.BasicMeshMaterial is not a constructor"
it should be THREE.MeshBasicMaterial()
still getting the error
2

I had similar problem, specifying character encoding in the header worked for me

<head>
	<title>Cube by Charden</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
	.
  .
  .
  .
  
</head>

Comments

2

my mistake was including three.js file from /src/Three.js folder. make sure to use ./build/three.js

Comments

1

it's hard to pinpoint the error since you only provided the code where you try using Three.js but what you do not show is how you load the module and in what order are all the other initialization scripts executed.So I strongly recommend sharing a simple example either through jsfiddle or use the Code Snippet feature in your main post.)

To ensure the three.min.js is loaded:

  • check your browser's console for any errors
  • check the network section in your browser's console, to see if the three.min.js is listen there.

index.html

<html>
<head>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r79/three.min.js"></script>
</head>
  <body>
  </body>
 </html>
<script>
  var container = document.createElement( 'div' );
  document.body.appendChild( container );
  scene = new THREE.Scene();
    ....
</script>

Comments

1

For anyone checking on this, I had the same problem. Realised I messed up by putting <script src="./libs/three.js"></script> after the script that actually uses three in my html file.

Comments

-1

Effectively check the path of the file three.js in my case it's in a carp

2 Comments

Can you update your answer in English? It would be helpful to those who don't understand Spanish.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.