dynamically display elements within aframe based on specific conditions

In my aframe scene, there are three icosahedrons, a complex particle system, and a cursor that fuses on objects within the scene. The performance of the scene is affected when the particles are visible because the cursor attempts to fuse with every particle instead of just the icosahedrons.

I am currently looking into two possible solutions:

  • Restricting the cursor to only fuse with the icosahedrons (if it improves performance)
  • Displaying the particle system only after all the icosahedrons have been fused/clicked

Unfortunately, I do not know how to implement either of these options. Below is the code for 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>

    <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>

    <!--- There are 2 more icosahedrons but hidden for brevity --->

    </a-scene>

Furthermore, here is the JavaScript snippet responsible for determining if an icosahedron has been clicked/fused:

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

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

I attempted to use the following function to add the particle system dynamically, but it did not work as expected:

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);
}

Answer №1

When you need to choose specific objects to fuse (or click) while excluding others, the raycaster component comes in handy. It works alongside the cursor (or can function independently, although it is recommended to use it with the cursor when fusing).

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

To ensure that an entity is included in the raycasting, simply add the attribute class="clickable" to it.

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

If you're interested in learning more about raycasting and cursors, check out this documentation:

I find it surprising that particles can be involved in raycasting, considering they are solely GPU instances. These particles aren't actual 3D entities within the scene but are generated and rendered as geometry shaders on the graphics card. Ray casting takes place before all geometries are sent to the graphics card. Have you tried toggling the raycasting feature on and off when particles are visible to see if that might be causing any performance issues?

Based on your code snippet, it doesn't seem like you're utilizing the class filter for filtering entities in the raycaster tool. Make sure to apply the appropriate class filters to properly filter the raycaster.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

It is necessary to render React Native text strings within a text component

Greetings! The React Native code snippet below is responsible for rendering a user interface. However, upon running the code, an error occurred. How can I resolve this issue? The error message indicates that text strings must be rendered within a text comp ...

React - Error: you have a syntax problem because there is an unexpected token <

I am working on a project using Express, pg, and react. However, I have encountered some issues with React. Here is the directory of my project index.js var express = require('express'); var server = express(); var path = require('path&ap ...

Decoding user input parameters within a vue module

It seems like I am hitting a wall when it comes to finding solutions for this particular issue. Currently, I have a component that is supposed to retrieve data from a file and display it. My intention is to only pass the filename to the component so that ...

Encountering a Node.js issue when attempting to properly sanitize data before inserting it into MySQL

This is a snippet of code that I am using to insert data into a MySQL table. Before running the query, I escape the values like so: const mysql = require('mysql'); const R = require('ramda'); class Repository { constructor(connectio ...

Is it better to modify attributes during the ng-repeat cycle or after the cycle ends?

Consider a scenario where a simple directive is defined as follows: app.directive('seo', function() { return { template: '<meta ng-repeat="tag in data" {{tag.attribute}}="{{tag.name}}" content="{{tag.content}}" />', ...

AngularJS module dependencies configuration functions smoothly during initialization

The structure of my modules looks like this: angular.module('mainModule',["cityModule", "countryModule"]); angular.module('mapModule',[]); angular.module('cityModule',["mapModule"]); angular.module('countryModule',[ ...

Can a simple text and javascript be used to create a register/login "database"?

For a school project, I am working on creating a cross-browser login script without using PHP or MySQL. I want the registration process to store usernames and passwords in a plain text file, and the login process to check that file for matches. I acknowled ...

Create a new storefront JavaScript plugin for Shopware 6 to replace the existing one

I am attempting to replace an existing JavaScript plugin in Shopware 6. However, the code within the plugin file does not seem to execute. Here is my main.js: import MyCookiePermissionPlugin from './plugin/my-cookie-permission/my-cookie-permission.pl ...

Is it possible to include number padding in a Bootstrap number input field?

When using an input box like this: <input type="number" class="form-control" value="00001" /> Is there a way to make it so that when the arrow up key is pressed, it changes to 00002 instead of just 2? https://i.sstati ...

Create a jQuery popup form with a variety of interactive buttons

Is it possible to implement a modal form in jQuery that can be triggered by multiple buttons? All the buttons have identical names and IDs. The goal is to display the modal form whenever any button is clicked. For this example, let's consider having ...

Steps to assign the identifier as the current index in the v-for iteration

Struggling to assign the id based on the index of a v-for loop. I attempted: <li v-for="(item, index) in items" v-bind:key="item.id"> <p>{{item.name}}</p> <input id= {{index}} type= "number"> < ...

"Provide information, then open a new webpage by clicking on a button. Perform various actions with the form by using type

Is there a way to quickly submit form entries to my database and then automatically redirect to a new page? My current situation requires that submitted information is stored in the DB before directing users to a thank you page, all in one seamless click! ...

How can I retrieve an image from a library and automatically update the image in SharePoint every 30 seconds?

I have a web part and I have written the code below: However, it is only fetching one image. How can I fetch all images from the library and change the image every 30 seconds using JavaScript or jQuery? public class MSDN : System.Web.UI.WebControls.WebPa ...

Ways to leverage ember.js in a serverless environment

After checking out the example of ember.js on this website (http://todomvc.com/), I decided to clone the project onto my computer. Upon double-clicking the index.html file, the project ran smoothly, just as I had anticipated. However, following the instru ...

Setting up Scss and purgeCss configuration in Next.js custom postCSS configuration: A step-by-step guide

My current project is using Scss in combination with Bootstrap for design. I have implemented purgeCss to remove unused Css, and customized my postcss.config.js file as follows: module.exports = { plugins: [ "postcss-flexbugs-fixes", [ " ...

`In the event that a series of criteria is unfulfilled in a JavaScript forEach() loop`

My IntersectionObserver utilizes a forEach method to invoke certain functions when a condition is met for each item in an array: const sections = document.querySelectorAll("section") function removeHighlight(id) { someElementArray[id].cl ...

What is the process for enabling HLS.js to retrieve data from the server side?

I have successfully implemented a video player using hls.js, and I have some ts files stored in https:/// along with an m3u8 file. To read the content of the m3u8 file, I used PHP to fetch it and sent the data to JavaScript (res["manifest"] = the content ...

Stop automatic resizing on mobile device after postback

Encountered an issue with a website I'm currently developing. It's not causing any problems, but I want to enhance the user experience. My aim is to maintain the zoom levels of mobile devices after a postback. To provide more context, there is a ...

I'm having trouble displaying the content of my list items within the div

In my code, I have a class called "ignicoes" that includes a list as one of its attributes. This list contains other attributes such as dispositivo, latitude, longitude, and more. My goal is to retrieve the contents of this list and display them within my ...

React Router Link Component Causing Page Malfunction

Recently, I delved into a personal project where I explored React and various packages. As I encountered an issue with the Link component in React Router, I tried to find solutions online without any luck. Let me clarify that I followed all installation st ...