     # On the server we need to manually request pixel maps.
     spriteData.requirePixelMaps() if Meteor.isServer
 
-    palette = spriteData.customPalette or LOI.Assets.Palette.documents.findOne spriteData.palette?._id
+    palette = @palette()
 
     # Build a new canvas if needed.
     unless @_canvas?.width is spriteData.bounds.width and @_canvas?.height is spriteData.bounds.height


class LOI.Assets.Engine.Sprite
  constructor: (@options) ->
    @ready = new ComputedField =>
      return unless spriteData = @options.spriteData()
      return unless spriteData.layers?.length and spriteData.bounds
      return unless spriteData.customPalette or LOI.Assets.Palette.documents.findOne(spriteData.palette?._id) or @options.visualizeNormals?()

      true

  drawToContext: (context, renderOptions = {}) ->
    # HACK: Request sprite data already at the top since otherwise ready sometimes doesn't get recomputed in time.

              smoothFactor = paletteColor.reflection.smoothFactor or 0

              # Average the normal.
              @_averageNormal.set 0, 0, 0

              for offsetX in [-smoothFactor..smoothFactor]
                for offsetY in [-smoothFactor..smoothFactor]
                  if sampleNormal = layer._pixelMap[pixel.x + offsetX]?[pixel.y + offsetY]?.normal
                    @_averageNormal.add sampleNormal

              @_averageNormal.normalize()
              @_averageNormal.x *= -1 if flippedHorizontal

              averageNormalLightProduct = THREE.Math.clamp @_averageNormal.dot(inverseLightDirection), 0, 1

              # Calculate the perfectly reflected ray.
              reflection = @_averageNormal.clone().multiplyScalar(2 * averageNormalLightProduct).sub inverseLightDirection

              # We assume the inverse view direction to be (0, 0, 1).
              # Dot product is then the equivalent of the z coordinate.
              reflectionViewProduct = THREE.Math.clamp reflection.z, 0, 1

              lightFactor = Math.pow(reflectionViewProduct, shininess) * lightSpecularCoefficient * materialSpecularCoefficient
              @_lightColor.set(0xffffff).multiplyScalar lightFactor

              shadedColor.add @_lightColor

            if palette
              # Find the nearest color from the palette to represent the shaded color.
