Skip to content

Commit

Permalink
Pass IDs in to shader as integers
Browse files Browse the repository at this point in the history
  • Loading branch information
thatcomputerguy0101 committed Sep 27, 2024
1 parent 8eed6b3 commit d5cf6c2
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 18 deletions.
26 changes: 15 additions & 11 deletions src/renderer/geometry/shaders/mesh.vert
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ in vec4 row3;

out vec3 pos;

#ifdef USE_NORMALS
#ifdef USE_NORMALS
uniform mat4 normalMatrix;
in vec3 normal;
out vec3 nor;

#ifdef USE_TANGENTS
#ifdef USE_TANGENTS
in vec4 tangent;
out vec3 tang;
out vec3 bitang;
Expand All @@ -36,7 +36,7 @@ out vec3 bitang;
#endif


#ifdef USE_UVS
#ifdef USE_UVS
#ifdef USE_INSTANCE_TEXTURE_TRANSFORMATION
in vec3 tex_transform_row1;
in vec3 tex_transform_row2;
Expand All @@ -45,7 +45,7 @@ in vec2 uv_coordinates;
out vec2 uvs;
#endif

#ifdef USE_VERTEX_COLORS
#ifdef USE_VERTEX_COLORS
in vec4 color;
#endif
#ifdef USE_INSTANCE_COLORS
Expand All @@ -54,6 +54,8 @@ in vec4 instance_color;

out vec4 col;

flat out int instanceID;

void main()
{
// *** POSITION ***
Expand All @@ -73,31 +75,31 @@ void main()
#ifdef PARTICLES
worldPosition.xyz += start_position + start_velocity * time + 0.5 * acceleration * time * time;
#endif
#ifdef USE_INSTANCE_TRANSLATIONS
#ifdef USE_INSTANCE_TRANSLATIONS
worldPosition.xyz += instance_translation;
#endif
gl_Position = viewProjection * worldPosition;

pos = worldPosition.xyz;

// *** NORMAL ***
#ifdef USE_NORMALS
#ifdef USE_NORMALS
#ifdef USE_INSTANCE_TRANSFORMS
mat3 normalMat = mat3(transpose(inverse(local2World)));
#else
mat3 normalMat = mat3(normalMatrix);
#endif
nor = normalize(normalMat * normal);

#ifdef USE_TANGENTS
#ifdef USE_TANGENTS
tang = normalize(normalMat * tangent.xyz);
bitang = normalize(cross(nor, tang) * tangent.w);
#endif

#endif

// *** UV ***
#ifdef USE_UVS
#ifdef USE_UVS
#ifdef USE_INSTANCE_TEXTURE_TRANSFORMATION
mat3 texTransform;
texTransform[0] = vec3(tex_transform_row1.x, tex_transform_row2.x, 0.0);
Expand All @@ -111,7 +113,7 @@ void main()

// *** COLOR ***
col = vec4(1.0);
#ifdef USE_VERTEX_COLORS
#ifdef USE_VERTEX_COLORS
col *= color;
#endif
#ifdef USE_INSTANCE_COLORS
Expand All @@ -120,6 +122,8 @@ void main()

// *** IDs ***
#ifdef USE_INSTANCE_ID
col.x = -float(gl_InstanceID);
instanceID = gl_InstanceID;
#else
instanceID = -1;
#endif
}
}
2 changes: 1 addition & 1 deletion src/renderer/material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub struct FragmentAttributes {
pub uv: bool,
/// Color: `in vec4 col;`
pub color: bool,
/// Instance ID: `in uint instanceID`
/// Instance ID: `in int instanceID`
pub instance_id: bool,
}

Expand Down
3 changes: 2 additions & 1 deletion src/renderer/material/geometry_instance_material.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ impl Material for GeometryInstanceMaterial {

fn fragment_attributes(&self) -> FragmentAttributes {
FragmentAttributes {
position: true,
instance_id: true,
..FragmentAttributes::NONE
}
}

fn use_uniforms(&self, program: &Program, camera: &Camera, _lights: &[&dyn Light]) {
program.use_uniform("id", self.id as f32);
program.use_uniform("id", self.id as i32);
program.use_uniform("eye", camera.position());
}

Expand Down
9 changes: 4 additions & 5 deletions src/renderer/material/shaders/geometry_instance_material.frag
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
uniform float id;
uniform int id;
uniform vec3 eye;

flat in int instanceID;

in vec3 pos;
in vec4 col;

layout (location = 0) out vec4 outColor;

void main()
{
float instanceID = -col.x;

float dist = distance(pos, eye);
outColor = vec4(dist, id, instanceID, 1.0);
outColor = vec4(dist, float(id), float(instanceID), 1.0);
}

0 comments on commit d5cf6c2

Please sign in to comment.