从三个JS中的2D对象绘制三维几何

时间:2013-09-29 07:24:56

标签: javascript html5 3d three.js webgl

我非常喜欢Three.js,我一直在努力实现以下目标:

假设我有一个来自浏览器视口的特定形状的x和y坐标数组。我想在Three.JS中渲染这些形状,以便我可以在z方向上添加高度。

形状本身实际上是一个“建筑物”,其中我有其平面布局坐标的数组,我想通过在Z方向上添加一个高度来对其进行3D化。

我应该使用什么几何/材料/技术来实现这一目标?

1 个答案:

答案 0 :(得分:7)

使用ExtrudeGeometry,您可以将2D元素(坐标)拉伸到Three.js中的3D对象。

此处示例:http://stemkoski.github.io/Three.js/Extrusion.html

此处的文档:http://threejs.org/docs/#Reference/Extras.Geometries/ExtrudeGeometry

示例中的代码:

var starPoints = [];

starPoints.push( new THREE.Vector2 (   0,  50 ) );
starPoints.push( new THREE.Vector2 (  10,  10 ) );
starPoints.push( new THREE.Vector2 (  40,  10 ) );
starPoints.push( new THREE.Vector2 (  20, -10 ) );
starPoints.push( new THREE.Vector2 (  30, -50 ) );
starPoints.push( new THREE.Vector2 (   0, -20 ) );
starPoints.push( new THREE.Vector2 ( -30, -50 ) );
starPoints.push( new THREE.Vector2 ( -20, -10 ) );
starPoints.push( new THREE.Vector2 ( -40,  10 ) );
starPoints.push( new THREE.Vector2 ( -10,  10 ) );

var starShape = new THREE.Shape( starPoints );

var extrusionSettings = {
    size: 30, height: 4, curveSegments: 3,
    bevelThickness: 1, bevelSize: 2, bevelEnabled: false,
    material: 0, extrudeMaterial: 1
};

var starGeometry = new THREE.ExtrudeGeometry( starShape, extrusionSettings );

var materialFront = new THREE.MeshBasicMaterial( { color: 0xffff00 } );
var materialSide = new THREE.MeshBasicMaterial( { color: 0xff8800 } );
var materialArray = [ materialFront, materialSide ];
var starMaterial = new THREE.MeshFaceMaterial(materialArray);

var star = new THREE.Mesh( starGeometry, starMaterial );
star.position.set(0,50,0);
scene.add(star);

// add a wireframe to model
var wireframeTexture = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } ); 
var star = new THREE.Mesh( starGeometry, wireframeTexture );
star.position.set(0,50,0);
scene.add(star);