【问题标题】:Is it possible to make only specific parts of a GLTF model clickable using AFrame?是否可以使用 AFrame 仅使 GLTF 模型的特定部分可点击?
【发布时间】:2021-05-06 07:37:54
【问题描述】:

我正在 AFrame 中加载 GLTF 模型并使用 AFrame 组件遍历其节点。我只想使该模型的某些部分可点击,即当用户将鼠标悬停在这些部分上时,光标将是指针,而对于所有其他部分,它将是默认光标。 这可以使用 HTML 或 Aframe 吗?

【问题讨论】:

    标签: html cursor aframe gltf


    【解决方案1】:

    文档是您的朋友! cursorraycaster 组件都暴露了相交对象(不仅是 HTML 元素,还有底层网格)。

    所以你可以创建一个custom component,它将

    • 检查鼠标是否悬停在模型上
    • 跟踪相交的网格并应用您的逻辑

    在这个例子中检查一下(我希望得到广泛的评论):

    <script src="https://aframe.io/releases/1.1.0/aframe.min.js"></script>
    <script>
      AFRAME.registerComponent("foo", {
        init: function() {
          // check when the cursor is intersecting with this entity
          this.el.addEventListener("raycaster-intersected", evt => {
            this.intersection = evt.detail;
          });
    
          // check when the intersection is cleared
          this.el.addEventListener("raycaster-intersected-cleared", evt => {
            this.intersection = null;
          });
        },
        tick: (function() {
          // helper variable to keep track of which element is under the mouse
          var currentMesh = null;
    
          return function() {
            // if there is no intersection, return
            if (!this.intersection) {
              if (currentMesh) {
                // remove highlight if a mesh was highlighted
                this.highlight(currentMesh, false);
                currentMesh = false;
              }
              return;
            }
            // grab the current intersected object
            var intersection = this.intersection.getIntersection(this.el);
            if (!intersection) return;
            const obj = intersection.object;
            // if nothing was cached, highlight this mesh
            if (!currentMesh) {
              currentMesh = obj;
              this.highlight(currentMesh, true);
            }
            // if a mesh was highlighted, clear the old one, and highlight the new one
            if (obj.uuid !== currentMesh.uuid) {
              this.highlight(currentMesh, false);
              currentMesh = obj;
              this.highlight(currentMesh, true);
            }
          };
        })(),
        highlight: function(mesh, highlight) {
          var color = highlight ? 0xffff00 : 0x000000;
          mesh.material.emissive.setHex(color);
          mesh.material.needsUpdate = true;
        }
      });
    </script>
    </head>
    
    <body>
      <div style="z-index: 99999; position: fixed; top: 5%; text-align: center">
        <p>
          Model by <a href="https://poly.google.com/view/62zn39CRkbG">google</a>
        </p>
      </div>
      <a-scene background="color: #FAFAFA" cursor="rayOrigin: mouse">
        <a-assets>
          <a-asset-item id="model" src="https://cdn.glitch.com/0c08e4fb-3f49-4a20-bb84-a2eceb70bca4%2FWall_Art_Classical_01.gltf?v=1612251538844"></a-asset-item>
        </a-assets>
    
        <a-entity position="0 1 -3" scale="0.1 0.1 0.1" gltf-model="#model" foo></a-entity>
      </a-scene>

    【讨论】:

    • 如果您需要澄清,请随时发表评论
    猜你喜欢
    • 1970-01-01
    • 2021-08-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-14
    • 1970-01-01
    相关资源
    最近更新 更多