Skip to content

Instantly share code, notes, and snippets.

@gsabran
Created August 15, 2016 00:15
Show Gist options
  • Save gsabran/d705d1a305908156537d2a9418c67a22 to your computer and use it in GitHub Desktop.
Save gsabran/d705d1a305908156537d2a9418c67a22 to your computer and use it in GitHub Desktop.
uniform mat4 u_modelViewProjectionTransform;
#define USE_TEXCOORD
#define USE_GEOMETRY_MODIFIER
#define USE_DIFFUSETEXCOORD 2
#define USE_DIFFUSE 2
#define USE_DIFFUSE_MAP 2
#define NEED_IN_TEXCOORD0
// In model space, must stay in it
struct SCNShaderGeometry
{
vec4 position;
vec3 normal;
vec4 tangent;
vec4 color;
vec2 texcoords[8]; // MAX_UV
} _geometry;
struct SCNShaderSurface
{
vec3 view; // in view space
vec3 position; // in view space
vec3 normal; // in view space
vec3 tangent; // in view space
vec3 bitangent; // in view space
float shininess;
float fresnel;
float ambientOcclusion;
vec3 _normalTS; // UNDOCUMENTED in tangent space
vec4 diffuse;
vec2 diffuseTexcoord;
} _surface;
uniform mat4 u_diffuseTextureMatrix;
varying vec2 v_texcoord0;
#define kSCNTexcoordCount 1
#ifdef USE_SKINNING
uniform vec4 u_skinningJointMatrices[60];
attribute vec4 a_skinningWeights;
attribute vec4 a_skinningJoints;
#endif
// Attributes
attribute vec4 a_position;
#ifdef USE_NORMAL
attribute vec3 a_normal;
#endif
#if defined(USE_TANGENT) || defined(USE_BITANGENT)
attribute vec4 a_tangent;
#endif
#ifdef NEED_IN_TEXCOORD0
attribute vec2 a_texCoord0;
#endif
#ifdef NEED_IN_TEXCOORD1
attribute vec2 a_texCoord1;
#endif
#ifdef NEED_IN_TEXCOORD2
attribute vec2 a_texCoord2;
#endif
#ifdef NEED_IN_TEXCOORD3
attribute vec2 a_texCoord3;
#endif
#ifdef NEED_IN_TEXCOORD4
attribute vec2 a_texCoord4;
#endif
#ifdef NEED_IN_TEXCOORD5
attribute vec2 a_texCoord5;
#endif
#ifdef NEED_IN_TEXCOORD6
attribute vec2 a_texCoord6;
#endif
#ifdef NEED_IN_TEXCOORD7
attribute vec2 a_texCoord7;
#endif
#ifdef USE_VERTEX_COLOR
attribute vec4 a_color;
varying vec4 v_vertexColor;
#endif
// Varyings
#ifdef USE_PER_VERTEX_LIGHTING
varying vec3 v_diffuse;
#ifdef USE_SPECULAR
varying vec3 v_specular;
#endif
#endif
#if defined(USE_POSITION) || defined(USE_VIEW)
varying vec3 v_position;
#endif
#ifdef USE_NORMAL
varying vec3 v_normal;
#endif
#ifdef USE_TANGENT
varying vec3 v_tangent;
#endif
#ifdef USE_BITANGENT
varying vec3 v_bitangent;
#endif
#if defined(USE_SPECULAR) && defined(USE_PER_VERTEX_LIGHTING)
uniform float u_materialShininess;
#endif
void main(void)
{
_geometry.position = a_position;
#ifdef USE_NORMAL
_geometry.normal = a_normal;
#endif
#if defined(USE_TANGENT) || defined(USE_BITANGENT)
_geometry.tangent = a_tangent;
#endif
#ifdef NEED_IN_TEXCOORD0
_geometry.texcoords[0] = a_texCoord0;
#endif
#ifdef NEED_IN_TEXCOORD1
_geometry.texcoords[1] = a_texCoord1;
#endif
#ifdef NEED_IN_TEXCOORD2
_geometry.texcoords[2] = a_texCoord2;
#endif
#ifdef NEED_IN_TEXCOORD3
_geometry.texcoords[3] = a_texCoord3;
#endif
#ifdef NEED_IN_TEXCOORD4
_geometry.texcoords[4] = a_texCoord4;
#endif
#ifdef NEED_IN_TEXCOORD5
_geometry.texcoords[5] = a_texCoord5;
#endif
#ifdef NEED_IN_TEXCOORD6
_geometry.texcoords[6] = a_texCoord6;
#endif
#ifdef NEED_IN_TEXCOORD7
_geometry.texcoords[7] = a_texCoord7;
#endif
#ifdef USE_VERTEX_COLOR
_geometry.color = a_color;
#endif
#ifdef USE_SKINNING
{
vec3 pos = vec3(0.);
#ifdef USE_NORMAL
vec3 nrm = vec3(0.);
#endif
#if defined(USE_TANGENT) || defined(USE_BITANGENT)
vec3 tgt = vec3(0.);
#endif
for (int i = 0; i < MAX_BONE_INFLUENCES; ++i) {
#if MAX_BONE_INFLUENCES == 1
float weight = 1.0;
#else
float weight = a_skinningWeights[i];
#endif
int idx = int(a_skinningJoints[i]) * 3;
mat4 jointMatrix = mat4(u_skinningJointMatrices[idx], u_skinningJointMatrices[idx+1], u_skinningJointMatrices[idx+2], vec4(0., 0., 0., 1.));
pos += (_geometry.position * jointMatrix).xyz * weight;
#ifdef USE_NORMAL
nrm += _geometry.normal * mat3(jointMatrix) * weight;
#endif
#if defined(USE_TANGENT) || defined(USE_BITANGENT)
tgt += _geometry.tangent.xyz * mat3(jointMatrix) * weight;
#endif
}
_geometry.position.xyz = pos;
#ifdef USE_NORMAL
_geometry.normal = nrm;
#endif
#if defined(USE_TANGENT) || defined(USE_BITANGENT)
_geometry.tangent.xyz = tgt;
#endif
}
#endif
#ifdef USE_GEOMETRY_MODIFIER
// DoGeometryModifier START
// This is the geometry entry point
a = b;
// DoGeometryModifier END
#endif
// Transform the geometry elements in view space
#ifdef USE_POSITION
_surface.position = (u_modelViewTransform * _geometry.position).xyz;
#endif
#ifdef USE_NORMAL
_surface.normal = normalize(mat3(u_normalTransform) * _geometry.normal);
#endif
#if defined(USE_TANGENT) || defined(USE_BITANGENT)
_surface.tangent = normalize(mat3(u_normalTransform) * _geometry.tangent.xyz);
_surface.bitangent = /*_geometry.tangent.w **/ cross(_surface.tangent, _surface.normal); // no need to renormalize since tangent and normal should be orthogonal
_surface.bitangent = normalize(cross(_surface.normal,_surface.tangent));
#endif
make USE_VIEW a mask
#ifdef USE_VIEW
_surface.view = normalize(-_surface.position);
#endif
#ifdef USE_PER_VERTEX_LIGHTING
_lightingContribution.diffuse = vec3(0.);
#ifdef USE_SPECULAR
_lightingContribution.specular = vec3(0.);
_surface.shininess = u_materialShininess;
#endif
// Lighting
__DoLighting__
v_diffuse = _lightingContribution.diffuse;
#ifdef USE_SPECULAR
v_specular = _lightingContribution.specular;
#endif
#endif
#if defined(USE_POSITION) && (USE_POSITION == 2)
v_position = _surface.position;
#endif
#if defined(USE_NORMAL) && (USE_NORMAL == 2)
v_normal = _surface.normal;
#endif
#if defined(USE_TANGENT) && (USE_TANGENT == 2)
v_tangent = _surface.tangent;
#endif
#if defined(USE_BITANGENT) && (USE_BITANGENT == 2)
v_bitangent = _surface.bitangent;
#endif
#ifdef USE_VERTEX_COLOR
v_vertexColor = _geometry.color;
#endif
#ifdef USE_TEXCOORD
v_texcoord0 = (u_diffuseTextureMatrix * vec4(_geometry.texcoords[0], 0., 1.)).xy;
#endif
// this means that the geometry are still in model space
#if defined(SEPARATE_PROJECTION) && SEPARATE_PROJECTION
gl_Position = u_projectionTransform * vec4(_surface.position, 1.);
#else
gl_Position = u_modelViewProjectionTransform * _geometry.position;
#endif
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment