Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Three.js Draw simple triangle

I'm trying to draw triangle with three.js:

var scene = new THREE.Scene();

var camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 100;
scene.add( camera );

var renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild( renderer.domElement );

var geom = new THREE.Geometry();
var v1 = new THREE.Vector3(0,0,0);
var v2 = new THREE.Vector3(30,0,0);
var v3 = new THREE.Vector3(30,30,0);

console.log(geom.vertices)
geom.vertices.push(new THREE.Vertex(v1));
geom.vertices.push(new THREE.Vertex(v2));
geom.vertices.push(new THREE.Vertex(v3));

geom.faces.push( new THREE.Face3( 0, 1, 2 ) );
geom.computeFaceNormals();

var mesh= new THREE.Mesh( geom, new THREE.MeshNormalMaterial() );
scene.add(mesh);

renderer.render( scene, camera );

But there is nothing on screen. However these examples are working: http://threejs.org/docs/58/#Manual/Introduction/Creating_a_scene https://github.com/tonylukasavage/jsstl (triangles here are created the same way I'm trying to do)

Could you please help me to find a problem? Thanks, Ievgeniia

like image 654
Zheden Avatar asked Apr 29 '13 13:04

Zheden


People also ask

How do you make a triangle in 3 js?

We first build a threejs Geometry object, followed by three points in space ( Vector3 objects). We push these into the array of vertices owned by geom . Then we build a triangular face on these three vertices, mentioning them by their index in the vertices array (0, 1, and 2), and push this face into our geometry.

Can I use three Js with angular?

Digital Infoways can integrate three. js in Angular with their different proven strategies and techniques.

What can you build with three Js?

Three. js is a powerful library for creating three-dimensional models and games. With just a few lines of JavaScript, you can create anything from simple 3D patterns to photorealistic, real-time scenes. You can build simple and complex 3D geometrics, animate and move objects through a lifelike scene, and more.


1 Answers

Do this, instead:

geom.vertices.push( v1 );
geom.vertices.push( v2 );
geom.vertices.push( v3 );

Are you copying outdated code from the net -- or from an outdated book? Learn from the three.js examples that work with the current version of the library.

three.js r.58

like image 77
WestLangley Avatar answered Sep 28 '22 08:09

WestLangley