Three.js:创建一个直角三棱柱

时间:2014-11-28 17:47:07

标签: javascript math three.js

我正在尝试制作一个直角三角棱镜。

到目前为止,这是我的代码:

var triangleGeometry = new THREE.Geometry(); 
triangleGeometry.vertices.push(new THREE.Vector3(-1.0,  1.5, 0.95));  
triangleGeometry.vertices.push(new THREE.Vector3(-1.0, -1.5, 0.95)); 
triangleGeometry.vertices.push(new THREE.Vector3( 1.0, -1.5, 0.95));
triangleGeometry.vertices.push(new THREE.Vector3(-1.0,  1.5, 1.2));  
triangleGeometry.vertices.push(new THREE.Vector3(-1.0, -1.5, 1.2)); 
triangleGeometry.vertices.push(new THREE.Vector3( 1.0, -1.5, 1.2));

triangleGeometry.faces.push(new THREE.Face3(0, 1, 2));
triangleGeometry.faces.push(new THREE.Face3(3, 4, 5));
// Points 1,4,3 and 6 form a rectangle which I'm trying to construct using triangles 0,2,5 and 0,3,5
triangleGeometry.faces.push(new THREE.Face3(0, 2, 5));
triangleGeometry.faces.push(new THREE.Face3(0, 3, 5));

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

var triangleMesh = new THREE.Mesh(triangleGeometry, triangleMaterial); 
triangleMesh.position.set(1, 0.0, 0.0); 

scene.add(triangleMesh); 

我能够得到我想要的东西,但我想知道是否有任何其他好的解决方案来获得正确的三角形棱镜。 enter image description here

2 个答案:

答案 0 :(得分:7)

创建课程

PrismGeometry = function ( vertices, height ) {

    var Shape = new THREE.Shape();

    ( function f( ctx ) {

        ctx.moveTo( vertices[0].x, vertices[0].y );
        for (var i=1; i < vertices.length; i++) {
            ctx.lineTo( vertices[i].x, vertices[i].y );
        }
        ctx.lineTo( vertices[0].x, vertices[0].y );

    } )( Shape );

    var settings = { };
    settings.amount = height;
    settings.bevelEnabled = false;
    THREE.ExtrudeGeometry.call( this, Shape, settings );

};

PrismGeometry.prototype = Object.create( THREE.ExtrudeGeometry.prototype );

示例使用

var A = new THREE.Vector2( 0, 0 );
var B = new THREE.Vector2( 30, 10 );
var C = new THREE.Vector2( 20, 50 );

var height = 12;                   
var geometry = new PrismGeometry( [ A, B, C ], height ); 

var material = new THREE.MeshPhongMaterial( { color: 0x00b2fc, specular: 0x00ffff, shininess: 20 } );

var prism1 = new THREE.Mesh( geometry, material );
prism1.rotation.x = -Math.PI  /  2;

scene.add( prism1 );

示例here

答案 1 :(得分:0)

我喜欢@Almaz Vildanov的解决方案。效果很好。由于我使用打字稿,因此将类定义移植到.ts:

import {ExtrudeGeometry, Shape, Vector2} from "three";

class PrismGeometry extends ExtrudeGeometry {
  constructor(vertices: Vector2[], height) {
    super(new Shape(vertices), {depth: height, bevelEnabled: false});
  }
}

示例用法(如@Almaz Vildanov的原始著作,但由于进口而可能省略三项):

var A = new Vector2( 0, 0 );
var B = new Vector2( 30, 10 );
var C = new Vector2( 20, 50 );

var height = 12;                   
var geometry = new PrismGeometry( [ A, B, C ], height ); 

var material = new MeshPhongMaterial( { color: 0x00b2fc, specular: 0x00ffff, shininess: 20 } );

var prism1 = new Mesh( geometry, material );
prism1.rotation.x = -Math.PI  /  2;

scene.add( prism1 );