Array Uniforms, as I understand it, are slow and need to be updated every draw call.
I feed my instances the index of the nearest world point. In the shader I use this index to get world information about the corresponding point.
Since I misuse column 3 of the transformation matrix, I only have four floats so I don’t want to use two floats for an index. This means that with a 2D texture I would have to split the ID into x and y before sampling the texture. And that 1 milion instances x 40 vertices.
And I think it would be cleaner since in fact my data is just not organized in a grid.
But if I am mistaking, and arrays are as fast as textures and can handle over 200K of information, that would be great.
…but I’m also not sure why you need special 1DTexture support over just sending in a 2D texture with no width. I would be very much surprised if the GPU weren’t already treating 1D textures that way.
sampler2D(myTexture, vec2(0, myIndex));
This video shows a test. When nobody is walking in it, the grass is red. When a guy walks around in it, it turns green and when he goes elsewhere it turns red again. This shows that the WorldInfo-texture idea works.
Some elements I put together:
First of all I created a texture with an ImageRaster to draw directly on it. every pixel corresponds with one of the 40K vertices of the globe mesh. The red chanel is used to store the height, green is for guys walking around.
I created grass using instancing. For every grass patch there are 20 floats of information. 16 for the transformation matrix and the normal-rotation and for spots. OpenGL only supports 16 elements per attribute array. But since these elements can be of any type, using vec4’s allows you to pass 4x16=64 floats - and using mat4’s would give you even more.
Each grass patch needs the following extra information in order for this idea to work:
2 uv coords for wind location
2 uv coords of the for worldInfo-lookup
distance to the nearest world-point
direction of the nearest world-point
I combine both uv-pairs: the wind location is in the fraction, the world-lookup is in the integers. Using the wind-UV’s is easy: they don’t need any work since they will wrap anyway. The worldlookup-UV’s need some work.
// GRASS PATCHES
uniform vec2 m_windDirection; //Direction of the wind
uniform float m_windSpeed; //Speed of the wind, not implemented
uniform float m_time; //Game-time updated every cycle
uniform sampler2D m_windDistortionMap; //2 channel distortion noise to read displacement
uniform sampler2D m_worldInfo;
//uniform float m_worldInfoMovers;
varying vec4 worldTest;
vec4 modelSpacePos = vec4(inPosition, 1.0);
vec3 modelSpaceNorm = inNormal;
float x = floor(uv.x)/256;
float y = floor(uv.y)/256;
vec4 wInfo=texture2D( m_worldInfo , vec2(x,y) );
This is the relevant part of the vert-shader code. There are also some minor changes to Instancing.glsllib.
Now I need some clever shader code to make the patched bend to the ground corresponding to the ‘presure’. Pressure is a world info value between 0 and 1.