three.js水平PlaneGeometry在x = 0 y = 0 z = 0

时间:2018-05-07 00:39:27

标签: javascript 3d three.js

嗨,我有这个代码,用于绘制下面的图像

window.onload = function()
{
    // init renderer
    var renderer=new THREE.WebGLRenderer();
    canvas_width=side; canvas_height=side;
    renderer.setSize(canvas_width,canvas_height);
    document.body.appendChild(renderer.domElement);

    // init scene and camera
    var scene=new THREE.Scene();
    var camera=new THREE.PerspectiveCamera(90,canvas_width/canvas_height,1,100);
    camera.position.y=5;
    camera.position.z=25;

    var texture = new THREE.TextureLoader().load( 'arrow.png' );
    var img = new THREE.MeshBasicMaterial( { map: texture } );

    // mesh
    mesh = new THREE.Mesh(new THREE.PlaneGeometry(25,25),img);
    mesh.overdraw = true;
    scene.add(mesh);

    // a light
    var light=new THREE.HemisphereLight(0xffffff,0x000000,1.5);
    light.position.set(1,1,1);
    scene.add(light);

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

enter image description here

但是我希望水平而不是垂直绘制PlaneGeometry,而不是用mesh.rotation.x=THREE.Math.degToRad(-90);旋转它,以获得x = 0 y = 0 z = 0: enter image description here

所以用

mesh.rotation.x=THREE.Math.degToRad(-90);

箭头指向下方

和:

mesh.rotation.x=THREE.Math.degToRad(90);

箭头指向上方

你可以帮帮我吗?

1 个答案:

答案 0 :(得分:2)

您可以使用.rotateX()旋转几何体:

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

// init scene and camera
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(90, canvas_width / canvas_height, 1, 100);
camera.position.y = 5;
camera.position.z = 25;

var texture = new THREE.TextureLoader().load('https://threejs.org/examples/textures/UV_Grid_Sm.jpg');
var img = new THREE.MeshBasicMaterial({
  map: texture,
  side: THREE.DoubleSide
});

// mesh
geom = new THREE.PlaneGeometry(25, 25);
geom.rotateX(-Math.PI * 0.5); // this is how you can do it
mesh = new THREE.Mesh(geom, img);
mesh.overdraw = true;
scene.add(mesh);

// a light
var light = new THREE.HemisphereLight(0xffffff, 0x000000, 1.5);
light.position.set(1, 1, 1);
scene.add(light);

// render
requestAnimationFrame(function animate() {
  requestAnimationFrame(animate);
  renderer.render(scene, camera);
})
body {
  overflow: hidden;
  margin: 0;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/92/three.min.js"></script>