Scale size in vertex Shader

hello! its possible to scale a model into the vertexShader? can i get the data of the size sending via sphere(x) ?

#version 150

#define PROCESSING_COLOR_SHADER

#ifdef GL_ES
precision mediump float;
#endif

uniform mat4 transform;


uniform mat3 normalMatrix;


   in vec4 position;
   in vec2 texCoord;

   in vec3 color;
   out vec3 Color;
   out vec2 TexCoord;


 	void main() {

    Color = color;
    TexCoord = texCoord;
 		gl_Position = transform * position;

 	}

Hi! You can’t get the size because the vertices are already scaled before being sent to the shader. But you can set the size in a uniform, for example shader.set("sphereSize", 100); in case you need that value. Or you could calculate the distance from each vertex to the center of the sphere in the shader, not sure if that’s of any use.

But what do you want to actually achieve?

Also, it might be easier to send always a sphere of size 1, and then scale it in the shader by multiplying the vertices by a uniform (sphereSize ?). You can set that size before drawing each sphere…

Finally, if you use PShape the performance will be better, because you would avoid sending all the vertices of the sphere on each animation frame, and the shape would be maintained in the GPU.

PShape sph;
...
sph = createShape(SPHERE, 1);
...
translate(...);
shader.set("sphereSize", 100); // needs to be implemented in the shader
shape(sph);
1 Like

I can send it in a uniform, is true, but if ive an arrays of spheres? the uniforms works for all the shapes, and now we need to set a custom attribute (the post before :grinning:), let say that ive the scale factor in vertex shader. in what value i need multyply to scale a shape?

and how can i set manually the size and number of vertices of the sph Shape?

If you have an array of spheres, then you update the uniform before drawing each one.
The number of vertices will depend on sphereDetail().

I can only help with specific code in a few days, sorry, going to be offline…