我正在尝试使用 Three.js 库将 WebGL 图像渲染到画布,然后在渲染后在该画布上使用 toDataURL() 方法。当我检查来自 toDataURL 的字符串时,我得到的只是一个小球体,它是我场景中的光源,渲染的 3D 模型不会出现。代码主要取自 Three.js 网站上的一个示例。有没有办法在整个渲染实际完成后使用 toDataURL ?
<script type="text/javascript">
var canvas = document.getElementById("drawing");
var hash;
var SCREEN_WIDTH = canvas.width;
var SCREEN_HEIGHT = canvas.height;
var container, stats;
var camera, scene, canvasRenderer, webglRenderer;
var loader;
var mesh, zmesh, lightMesh;
var directionalLight, pointLight;
var render_canvas = 1, render_gl = 1;
var has_gl = 0;
render_canvas = !has_gl;
function addMesh(geometry, scale, x, y, z, rx, ry, rz, material) {
mesh = new THREE.Mesh(geometry, material);
mesh.scale.set(scale, scale, scale);
mesh.position.set(x, y, z);
mesh.rotation.set(rx, ry, rz);
scene.add(mesh);
}
function init() {
camera = new THREE.PerspectiveCamera(50, SCREEN_WIDTH
/ SCREEN_HEIGHT, 1, 100000);
camera.position.z = 1500;
scene = new THREE.Scene();
// LIGHTS
var ambient = new THREE.AmbientLight(0x101010);
scene.add(ambient);
directionalLight = new THREE.DirectionalLight(0xffffff, 0.5);
directionalLight.position.set(1, 1, 2).normalize();
scene.add(directionalLight);
pointLight = new THREE.PointLight(0xffaa00);
pointLight.position.set(0, 0, 0);
scene.add(pointLight);
// light representation
sphere = new THREE.SphereGeometry(100, 16, 8, 1);
lightMesh = new THREE.Mesh(sphere, new THREE.MeshBasicMaterial({
color : 0xffaa00
}));
lightMesh.scale.set(0.05, 0.05, 0.05);
lightMesh.position = pointLight.position;
scene.add(lightMesh);
if (render_gl) {
try {
webglRenderer = new THREE.WebGLRenderer({
canvas : canvas,
antialias : true,
alpha : true
});
webglRenderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
has_gl = 1;
} catch (e) {
alert("browser Doesn't support webGL");
return;
}
}
loader = new THREE.BinaryLoader(true);
loader.load('/static/lucy/Lucy100k_bin.js', function(geometry,
materials) {
addMesh(geometry, 0.75, 900, 0, 0, 0, 0, 0,
new THREE.MeshPhongMaterial({
ambient : 0x030303,
color : 0x030303,
specular : 0x990000,
shininess : 30
}));
addMesh(geometry, 0.75, 300, 0, 0, 0, 0, 0,
new THREE.MeshFaceMaterial(materials));
addMesh(geometry, 0.75, -300, 0, 0, 0, 0, 0,
new THREE.MeshPhongMaterial({
ambient : 0x030303,
color : 0x111111,
specular : 0xffaa00,
shininess : 10
}));
addMesh(geometry, 0.75, -900, 0, 0, 0, 0, 0,
new THREE.MeshPhongMaterial({
ambient : 0x030303,
color : 0x555555,
specular : 0x666666,
shininess : 10
}));
});
}
function animate() {
requestAnimationFrame(animate);
webglRenderer.render(scene, camera);
}
init();
animate();
var dataurl = canvas.toDataURL();
</script>