Trouble encountered with attributes object in RawShaderMaterial

I am struggling to generate my own content with threejs' RawShaderMaterial class. Here is what I have so far:

var geometry = new THREE.RingGeometry(/* params */);
//geometry.vertices.length = 441;
var vertexFooAttribs = [/* 441 instances of THREE.Vector3() */];
var material = new THREE.RawShaderMaterial({
    uniforms: THREE.UniformsUtils.merge([
        THREE.UniformsLib["lights"]
    ]),
    attributes: {
        foo: {type: "v3", value: vertexFooAttribs}
    },
    vertexShader: document.getElementById("vshader").text,
    fragmentShader: document.getElementById("fshader").text,
    side: THREE.BackSide,
    lights: true,
    vertexColors: THREE.VertexColors
});

The code for vshader is as follows:

<script id="vshader" type="shader">
uniform mat4 modelMatrix;
uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat3 normalMatrix;
uniform vec3 cameraPosition;
uniform vec3 pointLightPosition;
uniform vec3 color;

attribute vec3 position;
attribute vec3 normal;
attribute vec3 foo;

varying vec3 vColor;

void main() {
    //operations involving 'foo' attribute...
    vColor = resultOfComputation;
    gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
</script>

And the code for fshader is simply:

<script id="fshader" type="shader">
precision highp float;

varying vec3 vColor;

void main(void) {
    gl_FragColor = vec4(vColor, 1.0);
}
</script>

However, when I execute it, I get the following error message (256 times, fortunately at the limit):

[.WebGLRenderingContext]GL ERROR :GL_INVALID_OPERATION : glDrawElements:
attempt to access out of range vertices in attribute 2

I believe that if we start counting from 0, "attribute 2" refers to my foo attribute specifically.
I tried following the documentation provided by three.js but cannot identify where I might be going wrong. Unfortunately, I cannot share a jsfiddle at this moment. I hoped that perhaps someone familiar with Three.js/WebGL could quickly spot the issue. Thank you.

Answer №1

I have experience with ShaderMaterial but not RawShaderMaterial. Before diving into RawShaderMaterial, consider if ShaderMaterial might suit your needs better. It seems like RawShaderMaterial is geared towards specialized buffered geometry scenarios that require additional setup, such as defining position attributes in the geometry. In contrast, ShaderMaterial could be a more straightforward option for quickly implementing a custom shader.

For a similar example using a custom attribute with ShaderMaterial, check out this link: https://github.com/mrdoob/three.js/blob/master/examples/webgl_custom_attributes.html

If you're curious about RawShaderMaterial and how it handles position attributes, take a look at this example: https://github.com/mrdoob/three.js/blob/master/examples/webgl_buffergeometry_rawshader.html

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

Retrieve JSON information from the driver's console using Selenium with Python

I am trying to retrieve JSON data from the console, which is shown in this image. Here are the codes I have used so far: j = driver.execute_script(" return document.getElementsByClassName('form-group text required assessment_questions_choices_text ...

Currently, there is a requirement to include past build outcomes in the HTML test report within the playwright

Is there a way to display the previous build status of each test case for every test case? I have been attempting to use test.info() in playwright, but it seems inaccessible from onTestEnd. One option could be to retrieve the previous build data from Jenki ...

Retrieve the prior position using the AngularJS ui-router

Utilizing fromState and fromParams within the $stateChangeSuccess event allows us to access all details regarding the previous location. This concept is elaborated in the following discussion: Angular - ui-router get previous state $rootScope.$on('$s ...

Create a Promise that guarantees to reject with an error

I am relatively new to utilizing promises, as I typically rely on traditional callbacks. The code snippet below is from an Angular Service, but the framework doesn't play a significant role in this context. What really matters is how to generate a pro ...

What prevents me from initializing a blank Map or Set using Object.create?

Unfortunately, the code snippet provided is not functioning as expected. Here's what I attempted: const x = Object.create(Set.prototype) x.has(1) const y = Object.create(Map.prototype) y.get(1) I wanted a method to generate empty objects rega ...

Transfer data via ajax to the controller

I need assistance with storing a file in my database using Tabulator without having a form already created. I am currently creating a simple input element like this: var editor = document.createElement("input");. After clicking the file, it trigg ...

What could be the reason for encountering an undefined variable when utilizing classes?

I am just beginning to learn JavaScript and I have a question. On line 7 of Index.js, why is the value of this.main undefined? Main.js class Main { constructor(hostname, port) { this.hostname = hostname; this.port = port; } ...

Display identical text using JavaScript filter

My search filter highlight is currently displaying [object Object] instead of <mark>match values</mark> when replacing the values. This is the code I am using: this.countries.response.filter((val) => { const position = val.value.toLowerCa ...

Error in Node Express server: Status code 0 is not valid

As a beginner node.js/js programmer, I am encountering an error code while trying to make a POST request on my application. The error message reads as follows: Error: [20:22:28] [nodemon] starting `node app.js` Running server on 3000 Mon, 27 Jun 2016 19:2 ...

Tips for iterating through data in JSON format and displaying it in a Codeigniter 4 view using foreach

As a newcomer to JSON, I have a question - how can I iterate through JSON data (which includes object data and object array data) using jQuery/javascript that is retrieved from an AJAX response? To illustrate, here is an example of the JSON data: { "p ...

Issue with the functionality of socket.io callback functions

Previously, I used to utilize the socket.io emit callback in the following manner: On the client side: mysocket.emit('helloworld', 'helloworld', function(param){ console.log(param); }); On the server side: var server = r ...

Issue with AJAX POST request, values not being passed to variables despite being displayed in the echo output

I am facing an issue with my Ajax post request, where the server seems to be receiving two date variables as empty even though they are sending correctly based on what I can see in the echo statement. The gettype function is returning NULL when trying to c ...

Sequence of successive jQuery requests

Currently, I have a series of ajax calls that must be executed sequentially, without the possibility of being performed in parallel. The function performSeriesofCalls essentially makes ajax calls in the specific order of Call1 -> Call2 -> Call3 -> ...

Exploring the contrast between router.pathname and router.route within Next.js

Essentially, my goal is to utilize the NextJS router to access the page url by doing the following: import { useRouter } from "next/router"; const SomeComp = props => { const router = useRouter(); } Yet, when I console.log() the propertie ...

Creating responsive list items using Bootstrap 4 and Flexbox: Adjusting the width of <li> elements to fit within containers

Struggling with expanding li elements to match the container width? Despite tweaking individual widths and Flexbox properties, the desired outcome remains elusive. How can the width of each li be adjusted to align with the container, mirroring the dimensio ...

Using EJS to Render a Function Expression?

Has anyone been able to successfully render a function call in express using EJS? Here's what I've tried so far: res.render("page", { test: test() }); Can someone confirm if this is possible, or provide guidance on how to call a function fr ...

Discovering intersecting elements through the raycaster-intersection event in Aframe

My right laser controller is equipped with a raycaster-intersection event listener setup as shown below: <a-entity id="rightController" laser-controls="hand: right" raycaster="objects: .collidable; far: 20"></a-entity> ... rightController.ad ...

Retrieving images from GridFs and displaying them in an Angular application

I have been working on storing images from my angular app in MongoDB using GridFs. However, I am having trouble retrieving the images from the database to the app. I am using a custom objectId for the query. UPDATE After some changes, I managed to get t ...

What is the best way to iteratively fade in data from a MySQL database one by one?

How can I load data from a MySQL database and display it one by one with a fade-in effect? Let's say I have a total of 40 images stored in the database. First, I want to display each image like this: <li class="img" style=" list-style: none; flo ...

The debate between utilizing buttons and links in a single-page application like React

When developing a SPA application, how should I decide between using buttons or links to enhance user experience? I typically use React for my applications with specific guidelines, but I am unsure about which approach is best. Buttons: For performing ...