简体   繁体   English

在框架中条件地渲染组件

[英]conditonally render components in aframe

I have an aframe scene with three icosahedrons, a complex particle system and a cursor that fuses on objects in the scene. 我有一个具有三个二十面体的复杂场景,一个复杂的粒子系统和一个融合在场景中对象上的光标。 With the particles visible, the scene runs too slow, because the cursor tries to fuse on every particle. 在可见粒子的情况下,场景运行太慢,因为光标试图在每个粒子上融合。 I just need it to fuse on the three icosahedrons. 我只需要它融合在三个二十面体上。

So, I'm trying to do one of 2 things: 因此,我正在尝试执行以下两项操作之一:

  • Only tell the cursor to fuse on the icosahedrons (if it helps performance, might not) 只告诉光标在二十面体上融合(如果它有助于提高性能,则可能不会)
  • Only show the particle system after all icosahedrons have been fused / clicked. 仅在所有二十面体都已融合/单击后,才显示粒子系统。

I don't currently know how to do either of these two things. 我目前不知道该如何做这两件事。 Here's my scene: 这是我的场景:

  <a-scene xrweb xrextras-tap-recenter xrextras-almost-there xrextras-loading xrextras-runtime-error>

    <a-camera position="0 8 2">
      <a-entity position="0 0 -3" geometry="primitive: ring; radiusInner: 0.25; radiusOuter: 0.3;" color="#CCCCCC"
        material="shader: flat; opacity: 0.7" cursor="maxDistance: 10; fuse: true" events-cursor>
        <a-animation begin="fusing" easing="ease-in" attribute="scale" fill="forwards" from="1 1 1" to="0.5 0.5 0.5"
          dur="1500"></a-animation>
          <a-animation begin="mouseleave" easing="ease-in" attribute="scale" fill="forwards" from="0.8 0.8 0.8" to="1 1 1"
          dur="500"></a-animation>
        </a-animation>
      </a-entity>
    </a-camera>
    <!-- should only render this particle system after icosahedrons have been clicked -->
    <a-entity position="0 2.25 -15"
      particle-system="preset: dust; particleCount: 500; type: 2; rotationAngleSpread: .05; texture: ./images/debris.png; velocityValue: .5 0.15 .5;"
    >

    <a-entity rotation="0 0 0" animation="property: rotation; to: 360 0 0; loop: true; dur: 10000; easing: linear">
      <a-icosahedron position="0 1 -4" radius="1.25" material="roughness: 0.8; metalness: 0.2; color: #D65C66;"
        animation="property: rotation; to: 0 360 0; loop: true; dur: 10000; easing: linear" id="redOrb" events-red>
      </a-icosahedron>
    </a-entity>

    <!--- 3 of these, hiding code for brevity-->

  </a-scene>

And here's the javascript that handles whether an icosahedron has been fused / clicked: 这是处理是否已融合/单击二十面体的javascript:

AFRAME.registerComponent('events-red', {
  init: function () {

    el.addEventListener('click', function(){
      redClicked = true;
      //when all 3 have been clicked, hide them, and show the particle system.
    })
  }
});

I tried this, and it doesn't work (function fires under the right conditionals, but nothing shows up on screen): 我尝试了此操作,但它不起作用(在正确的条件下会触发功能,但屏幕上未显示任何内容):

addParticleSystem = function(){
      let particleSystem = document.createElement('a-entity');
      particleSystem.setAttrbute('position','0 2.25 -15');
      particleSystem.setAttribute('particle-system',"preset: dust; particleCount: 500; type: 2; rotationAngleSpread: .05; texture: ./images/debris.png; velocityValue: .5 0.15 .5;");
      document.querySelector('a-scene').appendChild(particleSystem);
    }

When you want to select some objects to be fused (or clicked) and exclude others, you do this with the raycaster component, which works with the cursor (or can work without it, but for fusing, use with the cursor component). 当您想要选择一些要融合(或单击)的对象并排除其他对象时,可以使用与光标一起工作的raycaster组件(或可以不使用它,但可以融合,但可以与光标组件一起使用)。

<a-entity cursor raycaster="far: 20; interval: 1000; objects: .clickable"></a-entity>

Then on every entity that you want to include in the raycasting, add the attribute class="clickable" 然后,在要包含在光线投射中的每个实体上,添加属性class =“ clickable”

<a-box id="redBox" class="clickable" color="red"></a-box>

Here is the documentation on raycasting and cursors: 这是有关射线投射和光标的文档:

https://aframe.io/docs/0.9.0/components/cursor.html#fuse-based-cursor https://aframe.io/docs/0.9.0/components/cursor.html#fuse-based-cursor

I am very surprised that particles would participate in raycasting, as they are gpu instances only. 我很惊讶粒子会参与光线投射,因为它们只是gpu实例。 They are not actually 3D entities in the scene, but generated and rendered as geometry shaders on the graphics card. 它们实际上不是场景中的3D实体,而是在图形卡上生成并渲染为几何着色器。 Ray casting occurs before all the geometry is sent to the graphics card. 在将所有几何图形发送到图形卡之前进行射线投射。 Have you tested turning the raycasting on and off while particles are visible to see if that is causing the slowdown? 您是否测试过在可见粒子的情况下打开和关闭射线广播,以查看是否导致速度降低?

Your code does not show the cursor or raycasting so I don't know if you are filtering entities, but it does not appear that you are using the class filter on your entities, so it appears that you are not filtering the raycaster. 您的代码不显示光标或光线投射,因此我不知道您是否在过滤实体,但似乎没有在实体上使用类过滤器,因此似乎没有在过滤光线投射器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM