Search code examples
three.jselectroncannon.js

I can see lines on a three.js object, even though wireframe is off


https://gyazo.com/7ea04fad47507a06fe7d38afbc58562b I can see these weird white lines on this black box (both black boxes, mind you.) This is very strange and I never specified my code to do this. Is there any way i can turn this off at all? this is very annoying.. My code (it's Three.JS + Cannon.JS example by whoever made it, but its modified):

var world, mass, body, shape, timeStep=1/60,
     camera, scene, renderer, geometry, material, mesh;
  initThree();
  initCannon();
  animate();
  function initCannon() {
      world = new CANNON.World();
      world.gravity.set(0,0,0);
      world.broadphase = new CANNON.NaiveBroadphase();
      world.solver.iterations = 10;
      shape = new CANNON.Box(new CANNON.Vec3(1,1,1));
      mass = 1;
      body = new CANNON.Body({
        mass: 1
      });
      body.addShape(shape);
      body.angularVelocity.set(0,10,0);
      body.angularDamping = 0.5;
      world.addBody(body);
  }
  function initThree() {
      scene = new THREE.Scene();
      camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 100 );
      camera.position.z = 5;
      scene.add( camera );
      geometry = new THREE.BoxGeometry( 2, 2, 2 );
      material = new THREE.MeshPhongMaterial( {shininess:0, color: 0xffffff } );
      mesh = new THREE.Mesh( geometry, material );
      geometry2 = new THREE.BoxGeometry( 1, 1, 1 );
      material2 = new THREE.MeshPhongMaterial( {shininess:0, color: 0xff0000 } );
      mesh2 = new THREE.Mesh( geometry2, material2 );
      mesh2.position.y = 2;
      scene.add( mesh );
      scene.add( mesh2 );
      renderer = new THREE.CanvasRenderer({ alpha:true });
      renderer.setSize( window.innerWidth, window.innerHeight );
      document.body.appendChild( renderer.domElement );
  }
  function animate() {
      requestAnimationFrame( animate );
      updatePhysics();
      render();
  }
  function updatePhysics() {
      // Step the physics world
      world.step(timeStep);
      // Copy coordinates from Cannon.js to Three.js
      mesh.position.copy(body.position);
      mesh.quaternion.copy(body.quaternion);
  }
  function render() {
      renderer.render( scene, camera );
  }

Solution

  • Can you use the WebGLRenderer instead of the CanvasRenderer? Canvas renderer is more like a webGL emulator rather than a renderer. If I switch to WebGL renderer, the lines go away.

    var world, mass, body, shape, timeStep = 1 / 60,
      camera, scene, renderer, geometry, material, mesh,controls;
    initThree();
    initCannon();
    animate();
    
    function initCannon() {
      world = new CANNON.World();
      world.gravity.set(0, 0, 0);
      world.broadphase = new CANNON.NaiveBroadphase();
      world.solver.iterations = 10;
      shape = new CANNON.Box(new CANNON.Vec3(1, 1, 1));
      mass = 1;
      body = new CANNON.Body({
        mass: 1
      });
      body.addShape(shape);
      body.angularVelocity.set(0, 100, 0);
      body.angularDamping = 0.5;
      world.addBody(body);
    }
    
    function initThree() {
      renderer = new THREE.WebGLRenderer({
        alpha: true
      });
      scene = new THREE.Scene();
      camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 1, 100);
      camera.position.z = 10;
      camera.position.y = 1;
    
      scene.add(camera);
    
    
      var light = dirLight = new THREE.DirectionalLight(0xFFFFFF);
      scene.add(light);
    
      //Shadowmapping
      renderer.shadowMap.enabled = true;
      renderer.shadowMap.type = THREE.PCFSoftShadowMap;
      //renderer.shadowMap.bias = 0.1;
      light.position.set(5, 5, 5);
      light.userData.targetOffset = light.position.clone();
      light.castShadow = true;
      light.shadow.mapSize.width = 512;
      light.shadow.mapSize.height = 512;
      light.shadow.camera.near = 0.1;
      light.shadow.camera.far = 100.0
      var shadowAreaLength = 10
      light.shadow.camera.left = light.shadow.camera.bottom = -shadowAreaLength;
      light.shadow.camera.right = light.shadow.camera.top = shadowAreaLength;
    
        controls = new THREE.OrbitControls(camera,renderer.domElement);
        controls.enableDamping = true;
        controls.dampingFactor = 0.5;
        controls.autoRotate = true;
        controls.maxPolarAngle *= 0.5;
        controls.panSpeed *= 0.5;
        controls.rotateSpeed *= 0.25;
       // camera.position.set(0, 0.5, 1.7);
    
    
      geometry = new THREE.BoxGeometry(2, 2, 2);
      material = new THREE.MeshPhongMaterial({
        shininess: 0,
        color: 0x808080
    
      });
      mesh = new THREE.Mesh(geometry, material);
      mesh.castShadow = true;
      geometry2 = new THREE.BoxGeometry(1, 1, 1);
      material2 = new THREE.MeshPhongMaterial({
        shininess: 0,
        color: 0xff0000
      });
      mesh2 = new THREE.Mesh(geometry2, material2);
      mesh2.position.y = 2;
      scene.add(mesh);
      scene.add(mesh2);
    
    
      var gplane =
        new THREE.Mesh(new THREE.PlaneGeometry(100, 100, 10, 10), material);
      scene.add(gplane);
    
      gplane.recieveShadow = true;
    
      gplane.rotation.x += Math.PI * -0.5;
      gplane.position.y -= 2
    
    camera.lookAt(gplane.position);
      renderer.setSize(window.innerWidth, window.innerHeight);
      document.body.appendChild(renderer.domElement);
    }
    
    function animate() {
      requestAnimationFrame(animate);
            controls.update();
      updatePhysics();
      render();
    }
    
    function updatePhysics() {
      // Step the physics world
      world.step(timeStep);
      // Copy coordinates from Cannon.js to Three.js
      mesh.position.copy(body.position);
      mesh.quaternion.copy(body.quaternion);
    }
    
    function render() {
      renderer.render(scene, camera);
    }
    <script src="https://cdnjs.cloudflare.com/ajax/libs/cannon.js/0.6.2/cannon.min.js"></script>
    <script src="https://threejs.org/build/three.min.js"></script>
    <script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/renderers/CanvasRenderer.js"></script>
    <script src="https://cdn.rawgit.com/mrdoob/three.js/master/examples/js/controls/OrbitControls.js"></script>