Three.js绘制简单的三角形

时间:2013-04-29 13:13:26

标签: javascript three.js

我正在尝试使用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 );

但屏幕上没有任何内容。 但是这些例子正在起作用: http://threejs.org/docs/58/#Manual/Introduction/Creating_a_scene https://github.com/tonylukasavage/jsstl(此处的三角形创建方式与我尝试的方式相同)

你可以帮我找一个问题吗? 谢谢, Ievgeniia

1 个答案:

答案 0 :(得分:9)

这样做,而不是:

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

您是从网上复制过时的代码 - 还是从过时的书中复制?学习使用当前版本库的three.js示例。

three.js r.58