Skip to content

Instantly share code, notes, and snippets.

@mlfarrell
Last active June 16, 2023 19:55
Show Gist options
  • Save mlfarrell/c6e48dce84a0a141a04f6e9225f0aae7 to your computer and use it in GitHub Desktop.
Save mlfarrell/c6e48dce84a0a141a04f6e9225f0aae7 to your computer and use it in GitHub Desktop.
//vs
#ifdef GL_ES
//precision mediump float;
#else
#define highp
#define lowp
#define mediump
#endif
#if (defined GL_ES && __VERSION__ < 300)
attribute highp vec4 position;
attribute mediump vec3 normal;
attribute mediump vec2 texcoord0;
attribute mediump vec3 tangent;
varying mediump mat3 localSurface2World;
#ifdef TEXTURE
varying mediump vec2 va_texcoord;
#endif
varying mediump vec2 va_texcoord_bump;
varying vec4 va_position;
#elif (__VERSION__ < 450)
in highp vec4 position;
in mediump vec3 normal;
in mediump vec3 tangent;
in mediump vec2 texcoord0;
out mediump mat3 localSurface2World;
#ifdef SKELETAL_ANIMATION
layout(location = 5) in ivec4 boneIDs;
layout(location = 6) in vec4 weights;
#endif
#ifdef TEXTURE
out mediump vec2 va_texcoord;
#endif
out mediump vec2 va_texcoord_bump;
out vec4 va_position;
#else
layout(location = 0) in highp vec4 position;
layout(location = 1) in mediump vec3 normal;
layout(location = 2) in mediump vec3 tangent;
layout(location = 4) in mediump vec2 texcoord0;
#ifdef SKELETAL_ANIMATION
layout(location = 5) in ivec4 boneIDs;
layout(location = 6) in vec4 weights;
#endif
layout(location = 0) out OutBlock
{
mediump mat3 localSurface2World;
mediump vec2 va_texcoord_bump;
vec4 va_position;
#ifdef TEXTURE
mediump vec2 va_texcoord;
#endif
};
#endif
//Material colors
struct Material
{
lowp float alpha;
mediump float shininess;
};
//Lights
struct Light
{
mediump vec4 worldPosition;
lowp vec4 ambient, diffuse, specular;
mediump vec3 worldSpotDirection;
float spotExponent, spotCosCutoff;
float constantAttenuation, linearAttenuation, quadraticAttenuation;
};
#if (__VERSION__ < 450)
uniform highp mat4 modelViewProjectionMatrix;
uniform mat4 modelMatrix;
uniform mat4 modelMatrixInverse;
uniform mediump vec2 textureScale;
uniform mediump vec2 bumpScale;
uniform Material material;
#else
//putting all of these in one ubo for now, allowing for vulkan optimizations in the future
layout(std140, set = 0, binding = 0) uniform UniformBufferObject
{
highp mat4 modelViewProjectionMatrix;
highp mat4 modelMatrix;
highp mat4 modelMatrixInverse;
vec3 cameraPosition;
mediump vec2 textureScale;
mediump vec2 bumpScale;
vec4 lightModelProductSceneColor;
Material material;
Light lights[8];
};
#endif
#ifdef SKELETAL_ANIMATION
const int MAX_BONES = 256; //GL implementation min
#if (__VERSION__ < 450)
layout (std140) uniform SkeletalAnimBones
{
mat4 bones[MAX_BONES];
};
#else
layout (std140, set = 1, binding = 16) uniform SkeletalAnimBones
{
mat4 bones[MAX_BONES];
};
#endif
#endif
void main()
{
#ifdef TEXTURE
//Pass transformed texcoord.
va_texcoord = texcoord0*textureScale;
#endif
va_texcoord_bump = texcoord0*bumpScale;
#ifdef SKELETAL_ANIMATION
mat4 boneTransform = bones[boneIDs[0]] * weights[0];
boneTransform += bones[boneIDs[1]] * weights[1];
boneTransform += bones[boneIDs[2]] * weights[2];
boneTransform += bones[boneIDs[3]] * weights[3];
mat3 boneTransform3x3 = mat3(boneTransform);
localSurface2World[0] = normalize(mat3(modelMatrix) * boneTransform3x3 * tangent);
localSurface2World[2] = normalize(normal * inverse(boneTransform3x3) * mat3(modelMatrixInverse));
localSurface2World[1] = vec3(normalize(cross(localSurface2World[2], localSurface2World[0])));
vec4 pos = boneTransform * position;
va_position = modelMatrix * pos;
gl_Position = modelViewProjectionMatrix * pos;
#else
localSurface2World[0] = normalize(vec3(modelMatrix * vec4(tangent, 0.0)));
localSurface2World[2] = normalize(vec3(vec4(normal, 0.0) * modelMatrixInverse));
localSurface2World[1] = normalize(cross(localSurface2World[2],
localSurface2World[0]));
va_position = modelMatrix * position;
//Pass GL-trasnformed to vertex down pipeline
gl_Position = modelViewProjectionMatrix * position;
#endif
}
//fs
#ifdef GL_ES
precision mediump float;
#else
#define highp
#define lowp
#define mediump
#endif
#if (defined GL_ES && __VERSION__ < 300)
#ifdef TEXTURE
varying mediump vec2 va_texcoord;
#endif
varying mediump vec2 va_texcoord_bump;
varying vec4 va_position;
varying mediump mat3 localSurface2World;
#define texture texture2D
#define fragColor gl_FragColor
#elif (__VERSION__ < 450)
#ifdef TEXTURE
in mediump vec2 va_texcoord;
#endif
in mediump vec2 va_texcoord_bump;
in vec4 va_position;
in mediump mat3 localSurface2World;
out vec4 fragColor;
#else
layout(location = 0) in InBlock
{
mediump mat3 localSurface2World;
mediump vec2 va_texcoord_bump;
vec4 va_position;
#ifdef TEXTURE
mediump vec2 va_texcoord;
#endif
};
layout(location = 0) out vec4 fragColor;
#endif
//Prototypes
vec4 computeLight(in mediump vec3 normal, in mediump vec4 position, in lowp float alphaFade, out lowp vec4 otherSideColor, out lowp vec4 secondaryHighlight);
//Material colors
struct Material
{
lowp float alpha;
mediump float shininess;
};
//Lights
struct Light
{
mediump vec4 worldPosition;
lowp vec4 ambient, diffuse, specular;
mediump vec3 worldSpotDirection;
float spotExponent, spotCosCutoff;
float constantAttenuation, linearAttenuation, quadraticAttenuation;
};
#if (__VERSION__ < 450)
uniform Light lights[8];
uniform vec4 lightModelProductSceneColor;
uniform vec4 solidColor;
uniform vec3 cameraPosition;
uniform highp mat4 modelViewProjectionMatrix;
uniform mediump mat4 modelViewMatrix;
uniform mediump vec2 textureScale;
uniform Material material;
#ifdef TEXTURE
uniform lowp sampler2D texture0;
#endif
uniform sampler2D bumpTexture;
#else
layout(std140, set = 0, binding = 0) uniform UniformBufferObject
{
highp mat4 modelViewProjectionMatrix;
highp mat4 modelMatrix;
highp mat4 modelMatrixInverse;
vec3 cameraPosition;
mediump vec2 textureScale;
mediump vec2 bumpScale;
vec4 lightModelProductSceneColor;
Material material;
Light lights[8];
};
#ifdef TEXTURE
layout(set = 1, binding = 0) uniform lowp sampler2D texture0;
#endif
layout(set = 1, binding = 1) uniform sampler2D bumpTexture;
#endif
lowp vec4 ambient;
lowp vec4 diffuse, diffuseBack = vec4(0.0);
lowp vec4 specular, specularBack = vec4(0.0);
lowp vec4 lightColor;
lowp vec4 lightColorOtherSide;
void main()
{
lowp vec4 highlight = vec4(0.0);
#ifdef TEXTURE
vec4 color = texture(texture0, va_texcoord);
#else
vec4 color = vec4(1.0);
#endif
vec4 encodedNormal = texture(bumpTexture, va_texcoord_bump);
vec3 localCoords = 2.0 * encodedNormal.rgb - vec3(1.0);
vec3 eyeNormal = normalize(localSurface2World * localCoords);
vec4 lightColor = computeLight(eyeNormal, va_position, 1.0, lightColorOtherSide, highlight);
if(gl_FrontFacing==false)
lightColor = lightColorOtherSide;
color *= lightColor;
color.a = material.alpha;
color += highlight;
//color = clamp(color, 0.0, 1.0);
fragColor = color;
}
//bump_light.fs
//Material colors
//struct Material
//{
// vec4 ambient, diffuse, specular, emissive;
// float shininess;
//};
//uniform Material material;
//uniform vec4 lightModelProductSceneColor;
//
//vec4 ambient;
//vec4 diffuse;
//vec4 specular;
//
////Prototypes
//void pointLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3);
//void directionalLight(in int i, in vec3 normal, in vec3 ec_pos);
//void infiniteSpotLight(in int i, in vec3 normal, in vec3 ec_pos);
//void spotLight(in int i, in vec3 normal, in vec3 eye, in vec3 ecPosition3);
vec4 computeLight(in mediump vec3 normal, in mediump vec4 position, in lowp float alphaFade, out lowp vec4 otherSideColor, out lowp vec4 secondaryHighlight)
{
vec3 viewDirection = normalize(cameraPosition - vec3(position));
vec3 position3 = vec3(position);
lowp vec4 color, ret;
// Clear the light intensity accumulators
ambient = vec4(0.0);
diffuse = vec4(0.0);
specular = vec4(0.0);
//directionalLight(0, normal);
//pointLight(0, normal, eye, ecPosition3);
//final surface color computation
color = (lightModelProductSceneColor) + ambient + diffuse;
secondaryHighlight = vec4(specular.xyz, 0.0);
otherSideColor = (lightModelProductSceneColor) + ambient + specularBack + diffuseBack;
color = clamp(color, 0.0, 1.0);
ret = color;
//ret.a *= alphaFade;
//secondaryHighlight.a *= alphaFade;
return ret;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment