Skip to content

Change camera target by tapping on the model #512

Closed

Description

@cdata

In #288 , we introduced new, basic functionality for controlling the camera. After implementing #453 we will have the notion of a configurable "camera target" built upon the foundation of #288.

In discussing #447 and in other channels, it was suggested that one mechanism for controlling the camera target could be to do a raycast based on pointer input. If the ray intersects with the model, the camera target is moved to the point of intersection.

This mechanism would allow users to focus on points of interest in a model without needing to pan, and without the risk of relocating somewhere far away from the model.

Activity

  1. lgasnier commented on Jul 28, 2020

    @lgasnier

    It's an old issue and since then, hotspot features and some javascript code can be used to move the rotation center.
    A double click at any point on the 3D model will move it to this point.
    A double click outside the model restores the default rotation center.

    So we can focus on the clicked point and then zoom on it (useful for example to see details of the top or the bottom of a tall 3D model).

    • html
    <model-viewer
    	id="orbit-demo"
    	ondblclick="changePivotpoint(event)"
    	src="../models/astronaut.glb"
    	alt="Astronaut"
    	camera-controls
    >
    
    <img slot="hotspot-pivotpoint"
    	class="pivotpoint"
    	id="pivotpoint"
    	src="../resources/pivotpoint.svg"
    	data-position="0 0 0"
    	data-normal="0 0 1"
    >
    
    • css for hotspot-pivotpoint
    .pivotpoint{
    display: none;
    --min-hotspot-opacity: 0;
    height: 2em;
    }
    
    • javascript
    function changePivotpoint(event) {
      var oX = event.offsetX;
      var oY = event.offsetY;
      var model = document.querySelector('#orbit-demo');
      var posandnormal = model.positionAndNormalFromPoint(oX,oY);
      if (posandnormal != null) {
    	  var position = posandnormal.position
    	  var normal = posandnormal.normal;
    	  var strPosition = position.x + " " + position.y + " " + position.z;
    	  var cameraTarget = position.x + "m " + position.y + "m " + position.z + "m";
    	  var strNormal = normal.x + " " + normal.y + " " + normal.z;
    	  // var strPosandnormal = JSON.stringify(posandnormal);
    	  model.updateHotspot({"name": "hotspot-pivotpoint", "position": strPosition, "normal": strNormal});
    	  model.cameraTarget = cameraTarget;
    	  document.getElementById("pivotpoint").style.display = "block";
      }
      else
      {
    	  model.cameraTarget = cameraTarget;
    	  document.getElementById("pivotpoint").style.display = "none";
      }
    }
    
    • pivotpoint.svg
    <svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
    <path style="fill:#FF0000;"
    	d="m12 16 c-2.206 0 -4 -1.794 -4 -4
    	s1.794 -4 4 -4 4 1.794 4 4 -1.794 4 -4 4z
    	m0 -6.5 c-1.378 0 -2.5 1.122 -2.5 2.5
    	s1.122 2.5 2.5 2.5 2.5 -1.122 2.5 -2.5 -1.122 -2.5 -2.5 -2.5z"/>
    <path style="fill:#FF0000;"
    	d="m12 13
    	c-.551 0 -1 -.449 -1 -1
    	s.449 -1 1 -1 1 .449 1 1 -.449 1 -1 1z"/>
    </svg>
    
  2. elalish commented on Jul 28, 2020

    @elalish
    Contributor

    @lgasnier Precisely! Thanks for posting this demonstration.

  3. elalish commented on Mar 30, 2021

    @elalish
    Contributor

    We have a panning example now demonstrating this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    arc: interactiontype: feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions