我的目标是使用brython调用的THREE.js在WebGL中绘制线条。我有两个大概相同的脚本:
Brython版本: https://codepen.io/dgront/pen/yqJejO
from browser import window, document, alert
renderer = None
scene = None
camera = None
def init() :
global renderer, scene, camera
THREE = window.THREE
renderer = THREE.WebGLRenderer.new( { "alpha": 1, "antialias": True, "clearColor": "#ffffff" } )
renderer.setSize( window.innerWidth, window.innerHeight )
document.body.appendChild( renderer.domElement )
scene = THREE.Scene.new()
camera = THREE.PerspectiveCamera.new( 40, window.innerWidth / window.innerHeight, 1, 1000 )
camera.position.set( 5, 5, 5 )
material = THREE.LineBasicMaterial.new({ "color": "#0077ff" })
geometry = THREE.Geometry.new()
geometry.vertices.append( THREE.Vector3.new( 0, 0, 0) )
geometry.vertices.append( THREE.Vector3.new( 1, 0.5, 0) )
line = THREE.Line.new( geometry, material )
scene.add( line )
def animate(ev) :
window.requestAnimationFrame( animate )
renderer.render(scene, camera)
init()
animate(0)
和Javascript版本: https://codepen.io/dgront/pen/WKxrJN
init();
animate();
var renderer, scene, camera, controls;
var geometry, material, line, vertices, last;
function init() {
renderer = new THREE.WebGLRenderer( { alpha: 1, antialias: true, clearColor: 0xffffff } );
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.set( 5, 5, 5 );
controls = new THREE.OrbitControls( camera, renderer.domElement );
material = new THREE.LineBasicMaterial({ color: 0x0077ff });
geometry = new THREE.Geometry();
geometry.vertices.push( new THREE.Vector3( 0, 0, 0) );
geometry.vertices.push( new THREE.Vector3( 1, 0.5, 0) );
line = new THREE.Line( geometry, material )
scene.add( line );
}
function animate() {
requestAnimationFrame( animate );
renderer.render(scene, camera);
controls.update();
}
两者都应该做完全相同的事情,并且两者都可以工作,但是在Brython版本中看不到线条。
让我感到困惑的另一件事是,当我从JS脚本中删除OrbitControls
对象时,该行也消失了(一个人可以在Codepen上注释以下两行):
controls = new THREE.OrbitControls( camera, renderer.domElement );
controls.update();