嗨,我在使用 webgl 和使用 requestAnimationFrame 时遇到问题,如果我继续调试动画很好,但只要我让脚本自由运行,我就会从浏览器收到一个无响应的脚本错误
这是我的html:
<!DOCTYPE html>
<html>
<head>
<title>Earth to WebGL</title>
<script type="text/javascript" src="../Rec/three.min.js"></script>
<script type="text/javascript" src="../Rec/jquery-1.6.4.js"></script>
<script type="text/javascript" src="../Rec/RequestAnimationFrame.js"></script>
<script type="text/javascript" src="ex_loop.js"></script>
<script type="text/javascript" src="Earth.js"></script>
<script>
var loopProg = null;
var renderer = null;
var scene = null;
var camera = null;
var mesh = null;
var earth = null;
$(document).ready(
function() {
var container = document.getElementById("container");
renderer = new THREE.WebGLRenderer({ antialias: true } );
renderer.setSize(container.offsetWidth,container.offsetHeight);
container.appendChild(renderer.domElement)
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera( 45,
container.offsetWidth / container.offsetHeight, 1, 4000 );
earth = new Earth();
scene.add(earth.getEarth);
camera.position.set( 0, 0, 3 );
loopProg = new loopProgram();
loopProg.add(function(){earth.update()});
loopProg.add(function(){renderer.render( scene, camera );});
loopProg.solarLoop();
}
);
</script>
我的地球脚本文件
function Earth()
{
this.getEarth = init();
function init()
{
var map = {map:THREE.ImageUtils.loadTexture("images/earth_surface_2048.jpg")};
var material = new THREE.MeshBasicMaterial(map);
var geometry = new THREE.SphereGeometry(1,32,32);
return new THREE.Mesh(geometry, material);
}
this.update = function()
{
this.getEarth.rotation.x += .01;
}
return this;
}
和循环代码:
function loopProgram()
{
this.functionsToRun = new Array();
this.solarLoop= function()
{
jQuery.each(this.functionsToRun, function(index,value)
{
value ? value() : null;
});
var loopRef = this;
requestAnimationFrame(loopRef.solarLoop());
}
this.add = function(func)
{
this.functionsToRun[this.functionsToRun.length] = func;
}
}