Permalink
Browse files

Avoid creating new Matrix4 every frame

  • Loading branch information...
1 parent c02d751 commit 712c324ca3df8801627b79a3aa375f597ca4f7bb @vilcans committed Apr 22, 2012
Showing with 17 additions and 6 deletions.
  1. +17 −6 src/graphics.coffee
View
23 src/graphics.coffee
@@ -12,6 +12,8 @@ class window.Graphics
parentElement.clientWidth, parentElement.clientHeight)
@renderer.setSize @dimensions.x, @dimensions.y
+ @cameraMatrices = new CameraMatrices()
+
@stats = new Stats()
loadAssets: (onFinished) ->
@@ -129,12 +131,21 @@ class window.Graphics
@stats.update()
setCamera: (latitude, longitude, altitude) ->
- rotationY = new Matrix4().setRotationY(longitude)
- rotationX = new Matrix4().setRotationX(-latitude)
- translation = new Matrix4().setTranslation(0, 0, altitude + 1)
-
- matrix = new Matrix4().multiply(rotationY, rotationX).multiplySelf(translation)
# Funny, Object3D doesn't have a way to just SET the matrix(?)
@camera.matrix.identity()
- @camera.applyMatrix(matrix)
+ @camera.applyMatrix(@cameraMatrices.get(latitude, longitude, altitude))
+
+class CameraMatrices
+ constructor: ->
+ @rotationY = new Matrix4()
+ @rotationX = new Matrix4()
+ @translation = new Matrix4()
+ @matrix = new Matrix4()
+
+ get: (latitude, longitude, altitude) ->
+ @rotationY.setRotationY(longitude)
+ @rotationX.setRotationX(-latitude)
+ @translation.setTranslation(0, 0, altitude + 1)
+ @matrix.multiply(@rotationY, @rotationX).multiplySelf(@translation)
+ return @matrix

0 comments on commit 712c324

Please sign in to comment.