使用raycaster添加延迟

时间:2017-05-15 09:33:30

标签: javascript three.js

我想,当我的raycaster在2秒内与物体相交时,更新物体的纹理。

所以我尝试使用时钟功能,但不确定如何使用它。

var clock = new THREE.Clock();
clock.autoStart = true;

var intersects = raycaster.intersectObjects( pickable, true );

if ( intersects.length > 0 ) {
    if (!clock.running) clock.start()	
    
    if (clock.getElapsedTime() > 2) {
        if (intersects[0].object.name === "btn_meeting_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00004.jpg");
        }
        if (intersects[0].object.name === "btn_lobby_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00003.jpg");
        }
        if (intersects[0].object.name === "btn_restaurant_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00002.jpg");
        }
        if (intersects[0].object.name === "btn_entrance_01") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00001.jpg");
        }
        if (intersects[0].object.name === "btn_exterior") {
            mesh_sphere.material.map = THREE.ImageUtils.loadTexture( "textures/DIVE_IMMO_00000.jpg");
        };
    }
}

你能告诉我这有什么问题吗?

1 个答案:

答案 0 :(得分:1)

我设法使用布尔值和setTimeout
来做 我认为有一种更好的方法可以做到,但这就是想法 我就这样做了:

var loading = false
var timeout = null

function render() {
  var intersects = raycaster.intersectObjects( pickable, true )

  if ( intersects.length > 0 ) {
    updateTexture()
  } else {
    resetLoader()
  }

  // Render the scene
}

function resetLoader() {
  if( loading ) {
    clearTimeout( timeout )

    loading = false
  }
}

function updateTexture() {
  if( !loading ) {
    timeout = setTimeout( function() {
      // Change your texture
    }, 2000 )

    loading = true
  }
}