Unable to assign a shader to an element using A-Frame/THREE.js

Currently experimenting with creating a shader, I am attempting to apply it to an object upon loading using the code snippet below

AFRAME.registerShader('my-simple-shader', {
    schema: {
        color: {type: 'color', is: 'uniform', default: 'red'}
    },
    raw: false,
    fragmentShader: `
        uniform vec3 color;

        void main(){
            gl_FragColor = vec4(color, 1.0);
        }
    `
});

AFRAME.registerSystem('shader-preview-loader', {
    init: function(){
        this.el.sceneEl.addEventListener("scene-loaded", ()=>{
            let perry = document.querySelector('.perry-the-planeglb');
            perry.setAttribute('material', {shader: 'my-simple-shader'});
        })
    }
})

The custom scene-loaded event functions properly, ensuring all scene objects are loaded before any operations occur.

New to Aframe, I've been advised by the documentation to embed shaders in HTML markup, but due to using Mozilla Hubs, this is not feasible for me.

If anyone has insights on how to dynamically assign a new shader material through Javascript, please share them

Answer №1

It seems like everything is going well, except for one line:

perry.setAttribute('material', {shader: 'my-simple-shader'});

You're providing an object as a parameter when only a string is required.

The correct format should be:

perry.setAttribute('material', 'shader: my-simple-shader');

Ensure that the shader is registered before initialization based on your code, just to double-check.

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

Entering the ajax success handler

I have created this code snippet and I'm looking for a way to pass the exact 'this' value (since there are multiple items with this class) into the ajax success function. Any suggestions on how to achieve that? $(document).on('click&ap ...

The AJAX comments triggered an Uncaught SyntaxError due to an unexpected identifier

Can someone assist me in identifying the issue? I keep receiving an Unexpected identifier error when attempting to use ajax for sending comments. Code: function funcSuccess(data) { $("#comment_ajax").text(data); } function funcBefore() { $("#comme ...

Tips for aligning text in MUI Breadcrumbs

I am currently utilizing MUI Breadcrumb within my code and I am seeking a solution to center the content within the Breadcrumb. Below is the code snippet that I have attempted: https://i.stack.imgur.com/7zb1H.png <BreadcrumbStyle style={{marginTop:30}} ...

Could you provide some guidance on how to properly test this AngularJS code using Jasmine?

Here is a simple function I have: wpApp = angular.module('wpApp', ['ngRoute']); wpApp.controller("ctrlr", function($scope) { $scope.message = 'This is the page'; }); I am attempting to test it using Jasmine with this sp ...

press a button and choose an option from a dropdown menu on a website

Visiting a webpage within an iframe without scripting access at: This page is enclosed in another page located at: . My goal is to automatically click the "Schedule Now" button when accessing the docmein.com page. Once the "Request New Appointment" popu ...

BackboneJs error: Cannot call undefined as a function

Having encountered a persistent issue with no resolved solutions on Stack Overflow, I am revisiting this problem. For reference: Uncaught TypeError: undefined is not a function rails3/backbone/js Currently working on my first app using backBoneJs and fol ...

Getting the value of a session variable in JavaScript from a PHP file

Despite the numerous inquiries on this topic, I am still struggling to comprehend it. Scenario: An image with a hyperlink When the image is clicked: Verify if session exists If session exists, open the link If session does not exist, display the login ...

Are the intervals constantly shifting?

I have encountered an issue in my code where if an element is not currently doing something (specifically, "playing"), then I initiate the playing sequence. However, if it is already playing, I stop the action. While I have successfully implemented the sta ...

Trigger file upload window to open upon clicking a div using jQuery

I utilize (CSS 2.1 and jQuery) to customize file inputs. Everything is working well up until now. Check out this example: File Input Demo If you are using Firefox, everything is functioning properly. However, with Chrome, there seems to be an issue se ...

Setting a class for a specific date on a jQuery UI calendar using the MultipleDates plugin

I recently encountered an issue with the Multiple Dates picker for jQuery UI date picker. It seems that the developer who created it has not updated it in quite some time, resulting in pre-highlighting dates no longer functioning properly. To address this ...

Steps to ensure my collapsible is open from the start

Is there a way to have the collapsible content open by default without much hassle? I'm a bit lost here and could use some guidance. I must confess, I'm not very familiar with this, so hopefully, it's not too complicated. While my question ...

The new URL identifier is malfunctioning, as well as the local storage cache in Internet Explorer

I'm experiencing an issue with the new URL method ('id') in Internet Explorer. Below is the code snippet that I am using: $("#content .left").hide(); $('#'+activeTab).show(); $('#side-menu-parent li a').click(f ...

Vanilla JS causing event to fire repeatedly

My code is written in Vanilla JS and I've encountered an issue with dynamically generated content. Each click on the dynamic content returns a different value than expected: First click: 1 Second click: 3 Third click: 6 Fourth click: 10 But it sh ...

Checking if multiple text fields with identical names are valid using BootstrapValidator

I'm facing an issue where I am unable to validate multiple text fields with the same name. The validation only works for the first input text field, but not for the rest. <form id = "frm_org_id"> <input type="text" name="email_address[]" pla ...

Sequelize: Establishing association upon saving without the need to retrieve another object

Suppose I have two tables, parent and child, where the parent has multiple children. They are properly mapped in Sequelize. Now, I need to add a new child to an existing parent. However, I do not have access to the parent instance, only its id. I am aw ...

Enhancing user experience by implementing AJAX in a contact form to eliminate the need for page

I have read numerous questions on this topic and have compiled the code I currently have from various responses. Unfortunately, despite my efforts, I am unable to make it work and I cannot identify the reason behind this issue. Below is the HTML form str ...

Incorporating new functionality into the current .js file to automatically collapse the navbar when clicking outside

Previously, I utilized .js for my sticky navbar in Bootstrap 4.1.3 and it worked perfectly. However, I attempted to add a function in the script to collapse the navbar on mobile devices when clicking outside the menu, but it wasn't successful. Here i ...

"JIRA post functionality not functioning correctly on Internet Explorer and Firefox due to jQuery compatibility

I am facing an issue while trying to submit data to JIRA. It seems to be working fine in Chrome but not in IE/FF. I suspect there is something wrong with the ajax call setup, but I'm not entirely sure what needs to be done to fix it. $(&apos ...

How to Insert JSON into React Component's Attribute?

I am struggling with setting the value of a React component using JSON in the attribute. I want to concatenate a letter or word, but it doesn't seem to work. Is there a correct way to combine strings within a Component's attribute? In this case, ...

Is there a way to retrieve the text content from a span element using JavaScript?

Revised for better organization and explanation of the question Hello, I am new to web programming so please bear with me. My question is about extracting customer information from the following code snippet: <span class="tag-element-content" ...