Allows creation of instances of meshes and drawing all of them with single draw call. Instances can have their own transformation data (location, rotation and scale).
62 lines
1.5 KiB
GLSL
62 lines
1.5 KiB
GLSL
#version 450
|
|
|
|
layout(binding = 0) uniform UniformBufferObject {
|
|
mat4 model;
|
|
mat4 view;
|
|
mat4 proj;
|
|
} ubo;
|
|
|
|
layout(location = 0) in vec3 inPosition;
|
|
layout(location = 1) in vec3 inColor;
|
|
|
|
layout(location = 2) in vec3 instancePosition;
|
|
layout(location = 3) in vec3 instanceRotation;
|
|
layout(location = 4) in float instanceScale;
|
|
|
|
layout(location = 0) out vec3 fragColor;
|
|
|
|
void main()
|
|
{
|
|
mat4 gRotMat;
|
|
mat3 mx, my, mz;
|
|
|
|
// rotate around x
|
|
float s = sin(instanceRotation.x + 0);
|
|
float c = cos(instanceRotation.x + 0);
|
|
|
|
mx[0] = vec3(1, 0.0, 0.0);
|
|
mx[1] = vec3(0, c, -s);
|
|
mx[2] = vec3(0.0, s, c);
|
|
|
|
// rotate around y
|
|
s = sin(instanceRotation.y + 0);
|
|
c = cos(instanceRotation.y + 0);
|
|
|
|
my[0] = vec3(c, 0.0, s);
|
|
my[1] = vec3(0.0, 1.0, 0.0);
|
|
my[2] = vec3(-s, 0.0, c);
|
|
|
|
// rot around z
|
|
s = sin(instanceRotation.z + 0);
|
|
c = cos(instanceRotation.z + 0);
|
|
|
|
mz[0] = vec3(c, -s, 0.0);
|
|
mz[1] = vec3(s, c, 0.0);
|
|
mz[2] = vec3(0.0, 0.0, 1);
|
|
|
|
mat3 rotMat = mz * my * mx;
|
|
|
|
s = sin(instanceRotation.y + 0);
|
|
c = cos(instanceRotation.y + 0);
|
|
gRotMat[0] = vec4(c, 0.0, s, 0.0);
|
|
gRotMat[1] = vec4(0.0, 1.0, 0.0, 0.0);
|
|
gRotMat[2] = vec4(-s, 0.0, c, 0.0);
|
|
gRotMat[3] = vec4(0.0, 0.0, 0.0, 1.0);
|
|
|
|
vec4 locPos = vec4(inPosition.xyz * rotMat, 1.0);
|
|
vec4 pos = vec4((locPos.xyz * instanceScale) + instancePosition, 1.0);
|
|
|
|
gl_Position = ubo.proj * ubo.view * ubo.model * pos;
|
|
|
|
fragColor = inColor;
|
|
} |