Scenario:
In my scene, I implemented a vertex shader that positions the grid plane along the xz axis at the camera position. Therefore, if the camera moves, the grid plane moves with it. This leads to a visual effect that when moving the camera, the planar mesh remains stationary. This seems to work correctly.
Problem:
If I move the camera (and therefore the flat mesh) to a certain extent, the mesh suddenly disappears.
I realized that, apparently, there is a connection between extinction and the size of the plane, i.e. The larger the plane, the more I can move the camera until the grid plane disappears.
Also, in my test scene, a flat grid disappears only when moving along the negative x axis, positive x axis, or negative z axis. It does not disappear when moving along the positive z axis.
I suppose this has something to do with some kind of cliff, but it may be wrong. Recalculating the bounding box of a flat grid had no effect.
Any ideas?
Greetings
Violin:
I created a fiddle that shows the problem: http://jsfiddle.net/p8wZ6/10/
In the violin, I added an extra box mesh to better visualize that the camera is actually moving.
- To change the axis the camera moves (negative z axis by default) (not), comment out the corresponding line of code in the tick method.
- To resize the plane, change the size value in the createPlane method.
Source Font:
<script id="vertexShader" type="x-shader/x-vertex"> void main() { vec4 pos = vec4( position, 1.0 ); vec4 wPos = modelMatrix * pos; wPos.x += cameraPosition.x; wPos.z += cameraPosition.z; </script> <script id="fragmentShader" type="x-shader/x-fragment"> void main() { gl_FragColor.rgb = vec3(0.7, 0.7, 0.7); gl_FragColor.a = 1.0; } </script>
Download source package js:
var scene; var camera; var light; var renderer; var controls; var onTick; var planeMesh; var boxMesh; var heightmap; var clock; function createPlane(){ // disappearance seems related to size of geometry. // the larger the longer it takes until disappearance. var size = 20; var geom = new THREE.PlaneGeometry(size, size, 20, 20); return geom; } function createBox(){ var geom = new THREE.CubeGeometry(2, 2, 4); return geom; } function createMesh(){ // plane var geom = createPlane(); var shaderMaterial = new THREE.ShaderMaterial({ vertexShader: document.getElementById( 'vertexShader' ).textContent, fragmentShader: document.getElementById( 'fragmentShader' ).textContent, side: THREE.DoubleSide, wireframe: true }); planeMesh = new THREE.Mesh(geom, shaderMaterial); var axis = new THREE.AxisHelper(4); planeMesh.rotation.x = -90 * (Math.PI / 180); planeMesh.add(axis); scene.add(planeMesh); // box geom = createBox(); var material = new THREE.MeshBasicMaterial( { color: 0xff00ff, }); boxMesh = new THREE.Mesh(geom, material); boxMesh.position.x = 5; boxMesh.position.z = -15; axis = new THREE.AxisHelper(4); boxMesh.add(axis); scene.add(boxMesh); } function startRendering(){ onTick(); }; function onTick(){ // move camera // causes disappearance // neg. z camera.position.z -= .1; // pos. x // camera.position.x += .1; // neg. x // camera.position.x -= .1; // causes no disappearance // pos. z // camera.position.z += .1; requestAnimationFrame(onTick); //controls.update(clock.getDelta()); renderer.render(scene, camera); } function init(){ renderer = new THREE.WebGLRenderer(); renderer.setClearColor( 0xffffff, 1 ); renderer.setSize(window.innerWidth, window.innerHeight); document.body.appendChild(renderer.domElement); scene = new THREE.Scene(); scene.add(new THREE.AxisHelper(4)); camera = new THREE.PerspectiveCamera(65, window.innerWidth / window.innerHeight, 0.1, 1000); camera.position.set(0, 1, 0); light = new THREE.DirectionalLight(0xffffff, 1); light.shadowCameraVisible = true; light.position.set(0, 0, 100); scene.add(light); //clock = new THREE.Clock(); //controls = new THREE.FirstPersonControls(camera); //controls.movementSpeed = 20; //controls.lookSpeed = .1; } init(); createMesh(); startRendering();