How to determine if plane is in Three.js camera Frustum

前端 未结 2 1171
独厮守ぢ
独厮守ぢ 2020-12-05 16:24

I am having a hard time making a function that returns true if a plane is inside of the camera\'s view frustrum. I found this post, on github, but the recipe always returns

相关标签:
2条回答
  • 2020-12-05 16:45

    Maybe the matrices are not updated?

    camera.updateMatrix(); // make sure camera's local matrix is updated
    camera.updateMatrixWorld(); // make sure camera's world matrix is updated
    camera.matrixWorldInverse.getInverse( camera.matrixWorld );
    
    plane.updateMatrix(); // make sure plane's local matrix is updated
    plane.updateMatrixWorld(); // make sure plane's world matrix is updated
    
    var frustum = new THREE.Frustum();
    frustum.setFromMatrix( new THREE.Matrix4().multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse ) );
    alert( frustum.contains( plane ) );
    
    0 讨论(0)
  • 2020-12-05 17:00

    Correct me if I'm wrong, but why to calculate frustum second time?

    I'm working on a complex project and the accepted answer is not the best solution for me. I see no point of recalculating what aleady was calculated. For my purpose I modified my copy of three.js by raising a flag if something was detected in frustum or not. Later just check on your object if object.inFrustum === true

    Below following line

    if ( webglObjects && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
    

    add

    object.inFrustum = true;
    

    also where the if block ends add an oposite flag

    else {
        object.inFrustum = false;
    }
    

    My final result in r70 which works perfectly:

    if ( webglObjects && ( object.frustumCulled === false || _frustum.intersectsObject( object ) === true ) ) {
    
        object.inFrustum = true; // The line to add
    
        for ( var i = 0, l = webglObjects.length; i < l; i ++ ) {
    
            var webglObject = webglObjects[i];
    
            unrollBufferMaterial( webglObject );
    
            webglObject.render = true;
    
            if ( _this.sortObjects === true ) {
    
                _vector3.setFromMatrixPosition( object.matrixWorld );
                _vector3.applyProjection( _projScreenMatrix );
    
                webglObject.z = _vector3.z;
    
            }
    
        }
    
    } else { // Create second condition like that
        object.inFrustum = false;
    }
    
    0 讨论(0)
提交回复
热议问题