我有一个带有纹理的大型2D平面的应用程序,但我遇到了一些闪烁的问题。
这里可以看到闪烁的问题(飞机没有纹理):EXAMPLE CODE
此深度缓冲区(z缓冲区)精度问题还是别的什么?
解决方案是缩小一切,而不是允许大型飞机/物体吗?什么是最佳做法?
我知道z near和far也影响精度,因此将z设置为接近1000会修复此示例中的闪烁。
代码:
/*
* Scale = 1, no flickering. Scale = 1000, flickering.
*/
var scale = 1000;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1000, 1000000);
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 100 * scale;
var renderer = new THREE.WebGLRenderer({
antialias: false
});
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
var light = new THREE.AmbientLight(0xFFFFFF);
scene.add(light);
var material = new THREE.MeshBasicMaterial({
transparent: false,
side: THREE.DoubleSide,
fog: false,
color: 0xFFFF00,
opacity: 1.0
});
var cubeGeometry = new THREE.PlaneGeometry(50 * scale, 50 * scale, 1, 1);
var cubeMesh = new THREE.Mesh(cubeGeometry, material);
cubeMesh.position.set(0, 0, 1 * scale);
scene.add(cubeMesh);
var material = new THREE.MeshBasicMaterial({
transparent: false,
side: THREE.DoubleSide,
fog: false,
color: 0xFF0000,
opacity: 1.0
});
var cubeGeometry = new THREE.PlaneGeometry(100 * scale, 100 * scale, 1, 1);
var cubeMesh = new THREE.Mesh(cubeGeometry, material);
scene.add(cubeMesh);
function render() {
var time = Date.now() * 0.5;
camera.position.x = Math.sin(time / 1000) * 150 * scale;
camera.position.y = 0;
camera.position.z = Math.cos(time / 1000) * 150 * scale;
camera.lookAt(scene.position);
renderer.render(scene, camera);
requestAnimationFrame(render);
}
render();
答案 0 :(得分:35)
你所看到的是GPU精度不高,因为你使用的范围太大了。通过将摄像机的near
平面设置为100
(而不是1
),闪烁消失了。
答案 1 :(得分:1)
尝试使用此代码以查看其是否有效:
cubeMesh.position.set(0, 0, 1);