当前位置:网站首页>Three Solution to the problem of inaccuracy in radiographic testing under the condition of non full screen canvas of JS

Three Solution to the problem of inaccuracy in radiographic testing under the condition of non full screen canvas of JS

2022-06-24 00:01:00 Zmikoo's growth path

The radiographic detection function in the case of full screen is generally written as follows :

function onTouchstart(event) {
    var touch = event.touches[0];
    if (!touch) return;
    let mousePosition = new THREE.Vector2();
    mousePosition.x = (touch.x / wx.getSystemInfoSync().windowWidth) * 2 - 1;
    mousePosition.y = -(touch.y / wx.getSystemInfoSync().windowHeight) * 2 + 1;
    var raycaster = new THREE.Raycaster();
    raycaster.setFromCamera(mousePosition, camera);
    let intersects = raycaster.intersectObjects(scene.children,true);
    if (intersects.length > 0) {
        console.log(" detected  ==", intersects[0].object);
        if (INTERSECTED != intersects[0].object) {
            if (INTERSECTED) INTERSECTED = intersects[0].object;
            return intersects[0].object;
        }
    } else {
        if (INTERSECTED)
            INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);

        INTERSECTED = null;
    }
}

When canvas Non full screen writing :

var canvasInfo;
 let query = wx.createSelectorQuery();
 query.select('#webgl').boundingClientRect((res) => {
    canvasInfo = res;
  }).exec();
function onTouchstart(event) {
  var touch = event.touches[0];
  if (!touch) return;
  let mousePosition = new THREE.Vector2();
  mousePosition.x = (touch.x / canvasInfo.width) * 2 - 1;
  mousePosition.y = -(touch.y / canvasInfo.height) * 2 + 1;
  var raycaster = new THREE.Raycaster();
  raycaster.setFromCamera(mousePosition, camera);
  let intersects = raycaster.intersectObjects(scene.children, true);
  if (intersects.length > 0) {
      console.log(" detected  ==", intersects[0].object.userData.name);
      if (INTERSECTED != intersects[0].object) {
          if (INTERSECTED) INTERSECTED = intersects[0].object;
          return intersects[0].object;
      }
  } else {
      if (INTERSECTED)
          INTERSECTED.material.emissive.setHex(INTERSECTED.currentHex);
      INTERSECTED = null;
  }
}
原网站

版权声明
本文为[Zmikoo's growth path]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206232121117618.html