class Scene: @JSVar('camera', 'scene', 'renderer', 'jQuery') def __init__(self, divSelector = '#container',**kwargs): self.clear(divSelector) self.objects = [] renderer = JCS('THREE.CanvasRenderer') scene = JCS('THREE.Scene') camera = JCS('THREE.PerspectiveCamera', kwargs.get('view_angle', 45), kwargs.get('aspect',1), kwargs.get('near', 0.1), kwargs.get('far2', 10000)) camera.position.z = 300 renderer.setSize(js(kwargs.get('width', 500)), js(kwargs.get('height', 500))) jQuery(js(divSelector)).append(renderer.domElement) self.renderer = renderer self.scene = scene self.camera = camera @JSVar('jQuery', 'window') def clear(self, divSelector): if window.animationFrame: cancelAnimationFrame(window.animationFrame) jQuery(js(divSelector)).html('') @JSVar('scene') def add(self, o): scene = js(self.scene) scene.add(js(o.mesh)) self.objects.append(o) @JSVar('renderer') def draw(self): renderer = js(self.renderer) renderer.render(js(self.scene), js(self.camera)) @JSVar('Date.now', 'mesh','renderer','scene','camera','window') def animate(self): window.animationFrame = requestAnimationFrame(self.animate) [o.step() for o in self.objects] self.draw() class Ball: @JSVar('mesh') def __init__(self, **kwargs): self.mesh = self.makemesh(kwargs.get('color', 0xffffff), kwargs.get('radius', 10)) self.rotateX = kwargs.get('rotateX', 0) self.rotateY = kwargs.get('rotateY', 0) self.x = kwargs.get('x', 0) self.y = kwargs.get('y', 0) self.stepCall = kwargs.get('stepCall') self.step() @JSVar('mesh', 'Date.now') def step(self): if self.stepCall: self.stepCall(self) mesh = js(self.mesh) mesh.rotation.x = Date.now() * js(self.rotateX) mesh.rotation.y = Date.now() * js(self.rotateY) mesh.position.x = js(self.x) mesh.position.y = js(self.y) def makemesh(color, radius): geometry = JCS('THREE.IcosahedronGeometry', radius, 1) material = JCS('THREE.MeshBasicMaterial', js({ 'color': color, 'wireframe': true, 'wireframeLinewidth': 1 })) return JCS('THREE.Mesh', geometry, material) s = Scene(width=300, height=300, view_angle=45) for x in range(3): for y in range(3): color = x%3 * 0x333322 + y%3 * 0x332233 + 0x003333 R = 20 s.add(Ball(color=color, x = x * R * 2.1 - 50, y = y * R * 2.1 - 50, rotateX = x * 0.002, rotateY = y * 0.002, radius = R)) s.animate()