                         fontface:o.fontface
                         constant_size:o.constant_size
                 when 'index_face_set'
-                    add_obj_to_scene(o)
+                    @add_obj(o)
                     if o.mesh and not o.wireframe  # draw a wireframe mesh on top of the surface we just drew.
                         o.color='#000000'
                         o.wireframe = o.mesh
-                        add_obj_to_scene(o)
+                        @add_obje(o)
                 when 'line'
                     delete o.type
                     @add_line(o)

            @controls.target = @_center
        @render_scene(true)

    animate: (opts={}) =>
        opts = defaults opts,
            fps  : undefined
            stop : false
            mouseover : true
        #console.log('anim', @opts.element.length, @opts.element.is(":visible"))

        if not @opts.element.is(":visible")
            # check again after a delay
            setTimeout((() => @animate(opts)), 1500)
            return

        if opts.stop
            @_stop_animating = true
            # so next time around will start
            return
        if @_stop_animating
            @_stop_animating = false
            return
        f = () =>
            requestAnimationFrame((()=>@animate(opts)))
        if opts.fps? and opts.fps
            setTimeout(f , 1000/opts.fps)
        else
            f()
        if opts.mouseover and (not document.hasFocus() or not @opts.element.is(":hover"))
            return
        @render_scene()

    render_scene: (force=false) =>
        #console.log('render', @opts.element.length)
        if @controls?
            @controls?.update()
        else
            if @opts.trackball
                @set_trackball_controls()

        pos = @camera.position
        if not @_last_pos?
            new_pos = true
            @_last_pos = pos.clone()
        else if @_last_pos.distanceToSquared(pos) > .05
            new_pos = true
            @_last_pos.copy(pos)
        else
            new_pos = false

        if not new_pos and not force
            return

        # rescale all text in scene
        if (new_pos or force) and @_center?
            s = @camera.position.distanceTo(@_center) / 3
            if @_text?
                for sprite in @_text
                    sprite.scale.set(s,s,s)
            if @_frame_labels?
                for sprite in @_frame_labels
                    sprite.scale.set(s,s,s)

        @renderer.render(@scene, @camera)

    add_3dgraphics_obj: (opts) =>
        opts = defaults opts,
            obj       : required
            wireframe : false

        add_obj_to_scene = (myobj)=>
            vertices = myobj.vertex_geometry
            for objects in [0..myobj.face_geometry.length-1]
                face3 = myobj.face_geometry[objects].face3
                face4 = myobj.face_geometry[objects].face4
                face5 = myobj.face_geometry[objects].face5
                geometry = new THREE.Geometry()
                geometry.vertices.push(new THREE.Vector3(vertices[i],
                vertices[i+1],vertices[i+2])) for i in [0..(vertices.length-1)] by 3
                geometry.faces.push(new THREE.Face4(face4[k]-1,face4[k+1]-1,face4[k+2]-1,
                face4[k+3]-1)) for k in [0..(face4.length-1)] by 4
                geometry.faces.push(new THREE.Face3(face3[k]-1,face3[k+1]-1,face3[k+2]-1)) for k in [0..(face3.length-1)] by 3
                geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+1]-1,face5[k+2]-1,
                face5[k+4]-1)) + geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+1]-1,face5[k+2]-1,
                face5[k+3]-1)) + geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+1]-1,face5[k+2]-1,
                face5[k+4]-1)) + geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+2]-1,face5[k+3]-1,
                face5[k+4]-1)) + geometry.faces.push(new THREE.Face4(face5[k+1]-1,face5[k+2]-1,face5[k+3]-1,
                face5[k+4]-1))for k in [0..(face5.length-1)] by 5
                geometry.mergeVertices()
                geometry.computeCentroids()
                geometry.computeFaceNormals()
                #geometry.computeVertexNormals()
                geometry.computeBoundingSphere()
                #finding material key(mk)
                name = myobj.face_geometry[objects].material_name
                mk = 0
                for item in [0..myobj.material.length-1]
                    if name == myobj.material[item].name
                        mk = item
                        break

                if opts.wireframe or myobj.wireframe
                    if myobj.color
                        color = myobj.color
                    else
                        c = myobj.material[mk].color
                        color = "rgb(#{c[0]*255},#{c[1]*255},#{c[2]*255})"
                    if typeof myobj.wireframe == 'number'
                        line_width = myobj.wireframe
                    else if typeof opts.wireframe == 'number'
                        line_width = opts.wireframe
                    else
                        line_width = 1

                    material = new THREE.MeshBasicMaterial
                        wireframe          : true
                        color              : color
                        wireframeLinewidth : line_width
                else if not myobj.material[mk]?
                    console.log("BUG -- couldn't get material for ", myobj)
                    material = new THREE.MeshBasicMaterial
                        wireframe : false
                        color     : "#000000"
                else
                    material =  new THREE.MeshPhongMaterial
                        shininess   : "1"
                        ambient     : 0x0ffff
                        wireframe   : false
                        transparent : myobj.material[mk].opacity < 1

                    material.color.setRGB(myobj.material[mk].color[0],
                                                myobj.material[mk].color[1],myobj.material[mk].color[2])
                    material.ambient.setRGB(myobj.material[mk].ambient[mk],
                                                  myobj.material[mk].ambient[1],myobj.material[0].ambient[2])
                    material.specular.setRGB(myobj.material[mk].specular[0],
                                                   myobj.material[mk].specular[1],myobj.material[mk].specular[2])
                    material.opacity = myobj.material[mk].opacity

                mesh = new THREE.Mesh(geometry, material)
                mesh.position.set(0,0,0)
                @scene.add(mesh)

        for o in opts.obj
            switch o.type


        @scene.add(particle)

    add_obj: (myobj)=>
        vertices = myobj.vertex_geometry
        for objects in [0..myobj.face_geometry.length-1]
            face3 = myobj.face_geometry[objects].face3
            face4 = myobj.face_geometry[objects].face4
            face5 = myobj.face_geometry[objects].face5
            geometry = new THREE.Geometry()
            geometry.vertices.push(new THREE.Vector3(vertices[i],
            vertices[i+1],vertices[i+2])) for i in [0..(vertices.length-1)] by 3
            geometry.faces.push(new THREE.Face4(face4[k]-1,face4[k+1]-1,face4[k+2]-1,
            face4[k+3]-1)) for k in [0..(face4.length-1)] by 4
            geometry.faces.push(new THREE.Face3(face3[k]-1,face3[k+1]-1,face3[k+2]-1)) for k in [0..(face3.length-1)] by 3
            geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+1]-1,face5[k+2]-1,
            face5[k+4]-1)) + geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+1]-1,face5[k+2]-1,
            face5[k+3]-1)) + geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+1]-1,face5[k+2]-1,
            face5[k+4]-1)) + geometry.faces.push(new THREE.Face4(face5[k]-1,face5[k+2]-1,face5[k+3]-1,
            face5[k+4]-1)) + geometry.faces.push(new THREE.Face4(face5[k+1]-1,face5[k+2]-1,face5[k+3]-1,
            face5[k+4]-1))for k in [0..(face5.length-1)] by 5
            geometry.mergeVertices()
            geometry.computeCentroids()
            geometry.computeFaceNormals()
            #geometry.computeVertexNormals()
            geometry.computeBoundingSphere()
            #finding material key(mk)
            name = myobj.face_geometry[objects].material_name
            mk = 0
            for item in [0..myobj.material.length-1]
                if name == myobj.material[item].name
                    mk = item
                    break

            if opts.wireframe or myobj.wireframe
                if myobj.color
                    color = myobj.color
                else
                    c = myobj.material[mk].color
                    color = "rgb(#{c[0]*255},#{c[1]*255},#{c[2]*255})"
                if typeof myobj.wireframe == 'number'
                    line_width = myobj.wireframe
                else if typeof opts.wireframe == 'number'
                    line_width = opts.wireframe
                else
                    line_width = 1

                material = new THREE.MeshBasicMaterial
                    wireframe          : true
                    color              : color
                    wireframeLinewidth : line_width
            else if not myobj.material[mk]?
                console.log("BUG -- couldn't get material for ", myobj)
                material = new THREE.MeshBasicMaterial
                    wireframe : false
                    color     : "#000000"
            else
                material =  new THREE.MeshPhongMaterial
                    shininess   : "1"
                    ambient     : 0x0ffff
                    wireframe   : false
                    transparent : myobj.material[mk].opacity < 1

                material.color.setRGB(myobj.material[mk].color[0],
                                            myobj.material[mk].color[1],myobj.material[mk].color[2])
                material.ambient.setRGB(myobj.material[mk].ambient[mk],
                                              myobj.material[mk].ambient[1],myobj.material[0].ambient[2])
                material.specular.setRGB(myobj.material[mk].specular[0],
                                               myobj.material[mk].specular[1],myobj.material[mk].specular[2])
                material.opacity = myobj.material[mk].opacity

            mesh = new THREE.Mesh(geometry, material)
            mesh.position.set(0,0,0)
            @scene.add(mesh)

    # always call this after adding things to the scene to make sure track
    # controls are sorted out, etc.   Set draw:false, if you don't want to
    # actually *see* a frame.
