2017-09-08 125 views
0

我遇到與Three.js和WhitestormJS Raycasting的問題。 也許我並沒有很好地理解這個特定元素的一些基本原理。Raycaster與Three.js和whitestormJS

我想要做的是同步raycaster與我的相機的方向。 所以,一旦它與一個元素相交,這個元素將被添加到相交數組中。

問題是即使我將相機移動到元素方向,我的「相交」數組仍保持爲空。

在codepen:https://codepen.io/paulbonneau/pen/zdVeJx?editors=1111

我的代碼:

const app = new WHS.App([ 
    new WHS.ElementModule({ 
    container: document.getElementById('app') 
    }), 
    new WHS.SceneModule(), 
    new WHS.DefineModule('camera', new WHS.PerspectiveCamera({ 
    position: new THREE.Vector3(0, 6, 18), 
    far: 10000 
    })), 
    new WHS.CameraModule({ 
    position: { 
     y: 2, 
     z: 2, 
     x: 1, 
    }, 

    }), 
    new WHS.RenderingModule({ 
    bgColor: 0x162129, 

    renderer: { 
     antialias: true, 
     shadowmap: { 
     type: THREE.PCFSoftShadowMap 
     } 
    } 
    }, {shadow: true}), 
    new WHS.OrbitControlsModule(


    ), 
    new WHS.ResizeModule() 
]); 

app.modules[5].controls.enableZoom = false 
var camera = app.get('camera'); 
crosshair = new THREE.Vector2(0,0); 
// Rendu de la skybox càd l'environnement dans lequel se déroule le jeu 
var path = "img/skybox/"; 
var format = '.jpg'; 
var urls = [ 

    './skybox/EH_0.front.jpg', 
    './skybox/EH_0.back.jpg' , 
    './skybox/EH_0.top.jpg', 
    './skybox/EH_0.bottom.jpg', 
    './skybox/EH_0.left.jpg', 
    './skybox/EH_0.right.jpg', 
]; 
var reflectionCube = THREE.ImageUtils.loadTextureCube(urls, null); 
reflectionCube.format = THREE.RGBFormat; 
var shader = THREE.ShaderLib[ "cube" ]; 
shader.uniforms[ "tCube" ].value = reflectionCube; 
var material = new THREE.ShaderMaterial({ 
    fragmentShader: shader.fragmentShader, 
    vertexShader: shader.vertexShader, 
    uniforms: shader.uniforms, 
    depthWrite: false, 
    side: THREE.BackSide 
}); 
//End test 

const world = new WHS.Box({ // Create box to contain the 3D space where the game happen 
    geometry: { 
    width: 100, 
    height: 100, 
    depth: 100 
    }, 
    material: material 
}); 


world.addTo(app); 


var material = new THREE.MeshBasicMaterial({ color: 0xffffff, envMap: reflectionCube }); 

var points = []; 
for (var deg = 0; deg <= 180; deg += 6) { 

    var rad = Math.PI * deg/180; 
    var point = new THREE.Vector2((0.72 + .08 * Math.cos(rad)) * Math.sin(rad), - Math.cos(rad)); // the "egg equation" 
    //console.log(point); // x-coord should be greater than zero to avoid degenerate triangles; it is not in this formula. 
    points.push(point); 

} 
const sphere = new WHS.Sphere({ 
    geometry: { 
     radius: 100 
    }, 

    material: new THREE.MeshBasicMaterial({ 
     color: 0xffffff 
    }), 

    position: { 
     y: 1, 
     x: 1, 
     z: 0 

    } 
}); 




for (var i = 0; i < 20; i++) { 

    egg_size = Math.random() * 10-2; 
    egg = new WHS.Lathe({ 
       geometry: { 
        points: points 
       }, 
       material: material, 
       position: { 
        y: Math.random() * 100 - 50, 
        x: Math.random() * 100 - 50 , 
        z: Math.random() * 100 - 50 
       }, 
       rotation: { 
        x: Math.random() * Math.PI/2, 
        y: Math.random() * Math.PI/2, 
        z: Math.random() * Math.PI/2 
       }, 
       scale:{ 
        x : egg_size, 
        z : egg_size, 
        y : egg_size 
       } 
       }); 
    egg.addTo(sphere); 
} 

sphere.addTo(world); 

raycaster = new THREE.Raycaster(camera, camera.position); 
var intersects = raycaster.intersectObjects(app.children); 


new WHS.Loop(() => { 
    raycaster.setFromCamera(camera.position , camera); 

    sphere.rotation.x += 0.01; 
    sphere.rotation.y += 0.01; 
    for (var i = 0; i < sphere.children.length-1; i++) { 
     sphere.children[i].rotation.x += 0.05; 
     sphere.children[i].rotation.y += 0.05; 
     sphere.children[i].rotation.z += 0.05; 
    } 
}).start(app); 


// Start the app 
app.start(); 
+1

那麼,什麼問題?控制檯錯誤?意外的結果? –

+0

對不起,我編輯了我的信息。 –

回答

0

你在錯誤的道路建設raycaster(如R87的)。

raycaster = new THREE.Raycaster(camera, camera.position); 

documentation所示,raycaster構造像這樣:

var raycaster = new THREE.Raycaster(); 

的代碼的其餘部分看上去是正確的,因此我假定這就是問題所在。

這裏是working example

+0

感謝您的回答!我根據它修改了我的代碼,但沒有得到我想要的結果。我製作了一支筆,如果您想觀看,請點擊此處:https://codepen.io/paulbonneau/pen/zdVeJx?editors=1111 –

+0

如果您閱讀控制檯,則會出現「不支持的相機類型」 。 –

+0

我看到了,我試過每種類型的相機,並獲得每次相同的錯誤信息 –

相關問題