javascript - 如何在 three.js 轴上旋转 3D 对象?

标签 javascript coffeescript three.js

我对 three.js 中的旋转有很大的疑问 我想在我的一个游戏中旋转我的 3D 立方体。

//init
geometry = new THREE.CubeGeometry grid, grid, grid
material = new THREE.MeshLambertMaterial {color:0xFFFFFF * Math.random(), shading:THREE.FlatShading, overdraw:true, transparent: true, opacity:0.8}
for i in [1...@shape.length]
    othergeo = new THREE.Mesh new THREE.CubeGeometry(grid, grid, grid)
    othergeo.position.x = grid * @shape[i][0]
    othergeo.position.y = grid * @shape[i][1]
    THREE.GeometryUtils.merge geometry, othergeo
@mesh = new THREE.Mesh geometry, material

//rotate
@mesh.rotation.y += y * Math.PI / 180
@mesh.rotation.x += x * Math.PI / 180
@mesh.rotation.z += z * Math.PI / 180

和 (x, y, z) 可能是 (1, 0, 0)

然后立方体可以旋转,但问题是立方体绕着自己的轴旋转,所以旋转后,它不能像预期的那样旋转。

我找到页面 How to rotate a Three.js Vector3 around an axis? , 但它只是让一个 Vector3 点绕世界轴旋转?

并且我尝试使用 ma​​trixRotationWorld 作为

@mesh.matrixRotationWorld.x += x * Math.PI / 180
@mesh.matrixRotationWorld.y += y * Math.PI / 180
@mesh.matrixRotationWorld.z += z * Math.PI / 180

但是不管用,我不知道是我用错了还是有其他方法..

那么,如何让3D立方体绕世界轴旋转呢???

最佳答案

从 r59 版本开始,three.js 提供了这三个 functions围绕对象轴旋转对象。

object.rotateX(angle);
object.rotateY(angle);
object.rotateZ(angle);

关于javascript - 如何在 three.js 轴上旋转 3D 对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11060734/

相关文章:

javascript - React Native - 在 for 循环中一个接一个地打开多个模态

javascript - .tolowercase() 只有文本,URL 除外?

javascript - Django如何正确提交表单并处理请求?

javascript - 在 CoffeeScript 中将参数传递给带有 `myarguments...` 的函数时出现问题

javascript - 如何确保事件在适当的 DIV 上触发?

javascript - 发出 JSON RPC API 的 GET 请求而不是 POST 请求

backbone.js - 将 Backbone 集合与服务器响应合并

javascript - 将外部 CSS 作为内联样式应用于 HTML 节点(vanilla Javascript/Coffeescript)

javascript - 我无法让透视相机跟随玩家。相机固定在世界原点

javascript - 向场景添加光没有效果