Script when the camera is close to an object

Hi @Cedric_atlantis

The ‘mesh’ position is always 0,0,0. But you can access it’s center position by accessing the bounding box parameters that define mesh extents within the world space. So within your script you could add additional variable ‘meshCenter’ like this:

if (nodes.length > 0) {
      const mesh = nodes[0].mesh;
      const meshCenter = mesh.geometry.boundingSphere.center;
      const distance = cameraPosition.distanceTo(meshCenter);

Let me know if this helps!

Best

hello and thank you for the answer!

I added “if” in my script but nothing the switchObject does not activate when I approach my Cube object…

<script>
  const viewer = WALK.getViewer();

  let extension = null;
  let cameraWithinDistance = false;

  function triggerExtension() {
    if (extension) {
      extension.activate();
    }
  }

  function checkProximity() {
    const cameraPosition = viewer.getCameraPosition();
    const nodes = viewer.findNodesOfType('Cube');
    if (nodes.length > 0) {
      const mesh = nodes[0].mesh;
      const distance = cameraPosition.distanceTo(mesh.position);

      if (distance < 2 && !cameraWithinDistance) {
        cameraWithinDistance = true;
        triggerExtension();
      }

      if (distance >= 2 && cameraWithinDistance) {
        cameraWithinDistance = false;
      }
    }

    requestAnimationFrame(checkProximity);
  }
  
  if (nodes.length > 0) {
      const mesh = nodes[0].mesh;
      const meshCenter = mesh.geometry.boundingSphere.center;
      const distance = cameraPosition.distanceTo(meshCenter);

  function onSceneLoadComplete() {
    extension = viewer.getExtension('SwitchObjects', '1');
    requestAnimationFrame(checkProximity);
  }

  viewer.onSceneLoadComplete(onSceneLoadComplete);
</script>

Hi @Cedric_atlantis

Sorry for the confusion, here is full script with the change (at the first if block):

<script>
  const viewer = WALK.getViewer();

  let extension = null;
  let cameraWithinDistance = false;

  function triggerExtension() {
    if (extension) {
      extension.activate();
    }
  }

  function checkProximity() {
    const cameraPosition = viewer.getCameraPosition();
    const nodes = viewer.findNodesOfType('Cube');
    if (nodes.length > 0) {
      const mesh = nodes[0].mesh;
      const meshCenter = mesh.geometry.boundingSphere.center;
      const distance = cameraPosition.distanceTo(meshCenter);

      if (distance < 2 && !cameraWithinDistance) {
        cameraWithinDistance = true;
        triggerExtension();
      }

      if (distance >= 2 && cameraWithinDistance) {
        cameraWithinDistance = false;
      }
    }

    requestAnimationFrame(checkProximity);
  }

  function onSceneLoadComplete() {
    extension = viewer.getExtension('SwitchObjects', '1');
    requestAnimationFrame(checkProximity);
  }

  viewer.onSceneLoadComplete(onSceneLoadComplete);
</script>

This script does not work either. The ‘Switch Object’ extension does not activate when I approach the “Cube” object

Hi @Cedric_atlantis

Hard to tell what could be the reason. If you could send me your scene via email (or link to it) I could take a look!

Best!

Hi @Cedric_atlantis

After looking at your scene and your code I have noticed that you are using “extension.activate()” which is not correct. Proper extension activation is made by using “extension.trigger()”. Then your script should work:

<script>
  const viewer = WALK.getViewer();

  let extension = null;
  let cameraWithinDistance = false;

  function triggerExtension() {
    if (extension) {
      extension.trigger();
    }
  }

  function checkProximity() {
    const cameraPosition = viewer.getCameraPosition();
    const nodes = viewer.findNodesOfType('Cube');
    if (nodes.length > 0) {
      const mesh = nodes[0].mesh;
      const meshCenter = mesh.geometry.boundingSphere.center;
      const distance = cameraPosition.distanceTo(meshCenter);

      if (distance < 2 && !cameraWithinDistance) {
        cameraWithinDistance = true;
        triggerExtension();
      }

      if (distance >= 2 && cameraWithinDistance) {
        cameraWithinDistance = false;
        // Uncomment line below to triggerExtension again after moving away.
        // triggerExtension();
      }
    }

    requestAnimationFrame(checkProximity);
  }

  function onSceneLoadComplete() {
    extension = viewer.getExtension('SwitchObjects', '1');
    requestAnimationFrame(checkProximity);
  }

  viewer.onSceneLoadComplete(onSceneLoadComplete);
</script>
1 Like

image

I also can’t see the camera co-ordinates?

Sorry, this feature is not yet available, but it will be available later today when Shapespark 2.8 comes out.:slight_smile:

Well that’s just not good enough!! :joy::joy:

Yes I saw it in the browser editor so figured it was just a matter of time, awesome :clap: