April 17, 2013 - Tagged as: threejs, coffeescript, en.
I’m using THREE.js for a project I’m working on. This is my first experience programming a 3D environment and I’m having hard times wrapping my head around 3D coordinate system.
So I wrote a helper function that draws axis arrows to the scene center to help me see how to translate/rotate my objects.
= (params = {}) ->
drawAxisHelpers .radius = params.radius || 0.05
params.height = params.height || 2
params.scene = params.scene || scene
params
= new THREE.CylinderGeometry 0, 2 * params.radius, params.height / 5
arrowGeometry
= new THREE.MeshBasicMaterial color: 0xFF0000
xAxisMaterial = new THREE.CylinderGeometry params.radius, params.radius, params.height
xAxisGeometry = new THREE.Mesh xAxisGeometry, xAxisMaterial
xAxisMesh = new THREE.Mesh arrowGeometry, xAxisMaterial
xArrowMesh .add xArrowMesh
xAxisMesh.position.y += params.height / 2
xArrowMesh.rotation.z -= 90 * Math.PI / 180
xAxisMesh.position.x += params.height / 2
xAxisMesh.scene.add xAxisMesh
params
= new THREE.MeshBasicMaterial color: 0x00FF00
yAxisMaterial = new THREE.CylinderGeometry params.radius, params.radius, params.height
yAxisGeometry = new THREE.Mesh yAxisGeometry, yAxisMaterial
yAxisMesh = new THREE.Mesh arrowGeometry, yAxisMaterial
yArrowMesh .add yArrowMesh
yAxisMesh.position.y += params.height / 2
yArrowMesh.position.y += params.height / 2
yAxisMesh.scene.add yAxisMesh
params
= new THREE.MeshBasicMaterial color: 0x0000FF
zAxisMaterial = new THREE.CylinderGeometry params.radius, params.radius, params.height
zAxisGeometry = new THREE.Mesh zAxisGeometry, zAxisMaterial
zAxisMesh = new THREE.Mesh arrowGeometry, zAxisMaterial
zArrowMesh .add zArrowMesh
zAxisMesh.rotation.x += 90 * Math.PI / 180
zAxisMesh.position.y += params.height / 2
zArrowMesh.position.z += params.height / 2
zAxisMesh.scene.add zAxisMesh params
drawAxisHelpers
function takes named parameters, all optional. radius
and height
are for sizes of arrows, and scene
is the scene object to draw arrows. When isn’t provided, a global scene
object is used.
Here’s an example: (you can zoom in/out with mouse scroll, move around with left/right clicks)
In this example I used OrbitAndPanControls
class to enable zooming/moving around with mouse. I copied this class from Udacity’s ‘CS291 - Interactive 3D Rendering’ course’s examples. You can see this post’s source to learn more.