Hi all, I’m having some problems when I try to send a bunch of vec2 values to the fragment shader. I need to send multiple vec2 values, each vec2 value defines a position of some character.
this.material.setParameter('positions[0]', [playerPos.x, playerPos.z]);
this.material.setParameter('positions[1]', [playerPos.x + 1.0, playerPos.z + 1.0]);
My shader:
uniform vec2 positions[2]; // trying to create an array of vec2 value with capacity 2
This works:
getSelectedCharPosition(positions[0]);
But this does not work:
getSelectedCharPosition(positions[1]);
Any ideas?
Hi @Ertugrul_Cetin ,
Yes, it requires flattening and passing your whole array at once. Check this post for instructions, and also check the WebGL 1 vs 2 compatibility follow up at the end:
I apologize for reviving an older thread, I had trouble myself sending an array to a shader. I was able to resolve my issue, posting my findings since there were a lot unanswered threads on the subject, in case it’s of help (@mvaligursky do step in if I got something wrong).
To make sure an array uniform is created:
You need to add [0] to your uniform name, at the end.
Passing Float32Array and dynamic JS arrays is supported. I think the exact length of the array is what specifies the type of …
2 Likes
Here’s an example, search for ‘tints’ in it:
import * as pc from '../../../../';
class GrabPassExample {
static CATEGORY = 'Graphics';
static NAME = 'Grab Pass';
static FILES = {
'shader.vert': /* glsl */`
attribute vec3 vertex_position;
attribute vec2 vertex_texCoord0;
uniform mat4 matrix_model;
uniform mat4 matrix_viewProjection;
varying vec2 texCoord;
void main(void)
{
// project the position
vec4 pos = matrix_model * vec4(vertex_position, 1.0);
gl_Position = matrix_viewProjection * pos;
This file has been truncated. show original
3 Likes