Skip to content

Instantly share code, notes, and snippets.

@RaheelYawar
Created October 4, 2018 16:30
Show Gist options
  • Save RaheelYawar/c1225e367c79b552910713e55aa89b18 to your computer and use it in GitHub Desktop.
Save RaheelYawar/c1225e367c79b552910713e55aa89b18 to your computer and use it in GitHub Desktop.
Three.js (GLSL) Selective Tint Shader
// THREEjs build-in uniforms and attributes (Fragment)
// uniform mat4 viewMatrix - camera.matrixWorldInverse
// uniform vec3 cameraPosition - camera position in world space
varying vec2 vUv;
varying vec3 vTintColor;
uniform sampler2D albedo;
uniform sampler2D tintMask;
void main() {
vec4 maskTexel = texture2D(tintMask, vUv);
vec4 albedoTexel = texture2D(albedo, vUv);
if (maskTexel.r == 1.0 && maskTexel.g == 1.0 && maskTexel.b == 1.0) {
gl_FragColor = vec4(mix(vTintColor, albedoTexel.rgb, 0.5), 1.0);
} else {
gl_FragColor = albedoTexel;
}
}
// THREEjs build-in uniforms and attributes (Vertex)
// uniform mat4 modelMatrix - object.matrixWorld
// uniform mat4 modelViewMatrix - camera.matrixWorldInverse * object.matrixWorld
// uniform mat4 projectionMatrix - camera.projectionMatrix
// uniform mat4 viewMatrix - camera.matrixWorldInverse
// uniform mat3 normalMatrix - inverse transpose of modelViewMatrix
// uniform vec3 cameraPosition - camera position in world space
// attribute vec3 position - vertex position
// attribute vec3 normal - vertex normal
// attribute vec2 uv - uv
// attribute vec2 uv2 - uv2
uniform vec3 tintColor;
varying vec2 vUv;
varying vec3 vTintColor;
void main() {
vUv = uv;
vTintColor = tintColor;
gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment