Skip to content

Instantly share code, notes, and snippets.

@mlfarrell
Last active November 13, 2019 23:38
Show Gist options
  • Save mlfarrell/9788522b189c42e7c874a164cd89db45 to your computer and use it in GitHub Desktop.
Save mlfarrell/9788522b189c42e7c874a164cd89db45 to your computer and use it in GitHub Desktop.
#version 450
#define VGL_VULKAN
#extension GL_ARB_separate_shader_objects : enable
#ifdef GL_ES
precision highp float;
#else
#define highp
#define lowp
#define mediump
#endif
#if (__VERSION__ < 450)
in highp vec4 position;
in mediump vec3 normal;
in mediump vec2 texcoord0;
#ifdef TEXTURE
out mediump vec2 va_texcoord;
#endif
out vec4 shadowCoord;
#else
layout(location = 0) in vec4 position;
layout(location = 1) in vec3 normal;
layout(location = 4) in vec2 texcoord0;
layout(location = 0) out OutBlock
{
vec3 va_normal;
vec4 ec_pos;
vec4 shadowCoord;
#ifdef TEXTURE
vec2 va_texcoord;
#endif
};
#endif
//Material colors
struct Material
{
//lowp vec4 ambient, diffuse, specular, emissive;
lowp float alpha;
mediump float shininess;
};
//Lights
struct Light
{
mediump vec4 position;
lowp vec4 ambient, diffuse, specular;
mediump vec3 spotDirection;
float spotExponent, spotCosCutoff;
float constantAttenuation, linearAttenuation, quadraticAttenuation;
};
#if (__VERSION__ < 450)
uniform mat4 biasShadowMatrix;
uniform highp mat4 projectionMatrix;
uniform highp mat4 viewMatrix;
uniform mediump vec2 textureScale;
uniform mediump mat3 normalMatrix;
#else
layout(std140, set = 0, binding = 0) uniform UniformBufferObject
{
mat4 biasShadowMatrix;
highp mat4 projectionMatrix;
highp mat4 viewMatrix;
mediump vec2 textureScale;
mediump mat3 normalMatrix;
vec4 lightModelProductSceneColor;
Material material;
Light lights[4];
bool disableShadows;
float shadowBias;
};
#endif
const int MAX_TRANSFORMS = 512;
#ifdef VGL_VULKAN
#define gl_InstanceID gl_InstanceIndex
layout (std140, set = 1, binding = 16) uniform InstancedTransforms
{
mat4 transforms[MAX_TRANSFORMS];
};
#else
layout (std140) uniform InstancedTransforms
{
mat4 transforms[MAX_TRANSFORMS];
};
#endif
void main()
{
#ifdef TEXTURE
//Pass transformed texcoord.
va_texcoord = texcoord0*textureScale;
#endif
mat4 modelMatrix = transforms[gl_InstanceIndex];
mat4 modelViewMatrix = viewMatrix * modelMatrix;
mat3 normalMatrix = mat3(transpose(inverse(modelViewMatrix)));
//Compute transformed normal
vec3 eyeNormal = normalize(normalMatrix * normal);
va_normal = eyeNormal;
//Compute ec vert & shadow position
ec_pos = (modelViewMatrix * position);
shadowCoord = biasShadowMatrix * modelMatrix * position;
gl_Position = projectionMatrix * modelViewMatrix * position;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment