Skip to content

Instantly share code, notes, and snippets.

@gsabran
Created August 15, 2016 00:24
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gsabran/4eebefffba78d692fb8fb25889ebd933 to your computer and use it in GitHub Desktop.
Save gsabran/4eebefffba78d692fb8fb25889ebd933 to your computer and use it in GitHub Desktop.
precision highp float;
#define USE_DOUBLE_SIDED
#define USE_TEXCOORD
#define LOCK_AMBIENT_WITH_DIFFUSE
#define USE_DIFFUSE 2
#define USE_SURFACE_MODIFIER
#define USE_DIFFUSETEXCOORD 2
#define USE_DIFFUSE_MAP 2
#define USE_FRAGMENT_MODIFIER
#define DIFFUSE_PREMULTIPLIED
#define NEED_IN_TEXCOORD0
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;
struct SCNShaderLightingContribution {vec3 ambient; vec3 diffuse; vec3 specular; vec3 modulate;} _lightingContribution;
uniform int projectionCode;
uniform float colorModifier;
varying vec2 v_texcoord0;
#ifdef USE_PER_VERTEX_LIGHTING
varying vec3 v_diffuse;
#ifdef USE_SPECULAR
varying vec3 v_specular;
#endif
#endif
#if defined(USE_POSITION) && (USE_POSITION == 2)
varying vec3 v_position;
#endif
#if defined(USE_NORMAL) && (USE_NORMAL == 2)
varying vec3 v_normal;
#endif
#if defined(USE_TANGENT) && (USE_TANGENT == 2)
varying vec3 v_tangent;
#endif
#if defined(USE_BITANGENT) && (USE_BITANGENT == 2)
varying vec3 v_bitangent;
#endif
#ifdef USE_AMBIENT_LIGHTING
uniform vec4 u_ambientLightColor;
#endif
#ifdef USE_DIFFUSE_MAP
uniform sampler2D u_diffuseTexture;
#ifdef USE_DIFFUSE_INTENSITY
uniform float u_diffuseIntensity;
#endif
#elif defined(USE_DIFFUSE_COLOR)
uniform vec4 u_diffuseColor;
#endif
#ifdef USE_NORMAL_MAP
uniform sampler2D u_normalTexture;
#ifdef USE_NORMAL_INTENSITY
uniform float u_normalIntensity;
#endif
#endif
#ifdef USE_SHININESS
uniform float u_materialShininess;
#endif
#ifdef USE_SPECULAR
#ifdef USE_SPECULAR_MAP
uniform sampler2D u_specularTexture;
#ifdef USE_SPECULAR_INTENSITY
uniform float u_specularIntensity;
#endif
#elif defined(USE_SPECULAR_COLOR)
uniform vec4 u_specularColor;
#endif
#endif // USE_SPECULAR
#ifdef USE_AMBIENT_MAP
uniform sampler2D u_ambientTexture;
#ifdef USE_AMBIENT_INTENSITY
uniform float u_ambientIntensity;
#endif
#elif defined(USE_AMBIENT_COLOR)
uniform vec4 u_ambientColor;
#endif
#ifdef USE_REFLECTIVE_MAP
uniform sampler2D u_reflectiveTexture;
#elif defined(USE_REFLECTIVE_COLOR)
uniform vec4 u_reflectiveColor;
#endif
#if defined(USE_REFLECTIVE_CUBEMAP)
uniform samplerCube u_reflectiveTexture;
#endif
#if defined(USE_REFLECTIVE_CUBEMAP)
uniform mat4 u_viewToCubeWorld;
#endif
#ifdef USE_REFLECTIVE_INTENSITY
uniform float u_reflectiveIntensity;
#endif
#ifdef USE_FRESNELexponent
#endif
#ifdef USE_EMISSION_MAP
uniform sampler2D u_emissionTexture;
#ifdef USE_EMISSION_INTENSITY
uniform float u_emissionIntensity;
#endif
#elif defined(USE_EMISSION_COLOR)
uniform vec4 u_emissionColor;
#endif
#ifdef USE_MULTIPLY_MAP
uniform sampler2D u_multiplyTexture;
#ifdef USE_MULTIPLY_INTENSITY
uniform float u_multiplyIntensity;
#endif
#elif defined(USE_MULTIPLY_COLOR)
uniform vec4 u_multiplyColor;
#endif
#ifdef USE_TRANSPARENT_MAP
uniform sampler2D u_transparentTexture;
#ifdef USE_TRANSPARENT_INTENSITY
uniform float u_transparentIntensity;
#endif
#elif defined(USE_TRANSPARENT_COLOR)
uniform vec4 u_transparentColor;
#endif
#ifdef USE_VERTEX_COLOR
varying vec4 v_vertexColor;
#endif
#ifdef USE_NODE_OPACITY // only for RGB_ZERO
uniform float u_nodeOpacity;
#endif
#ifdef USE_TRANSPARENCY // only for RGB_ZERO
uniform float u_transparency;
#endif
#ifdef USE_DOUBLE_SIDED
uniform float u_orientationPreserved;
#endif
#ifdef USE_TIME
uniform float u_time;
#endif
#ifdef USE_FOG
uniform vec4 u_fogColor;exp
#endif
float saturate(float x) {
return clamp(x, 0., 1.);
}
vec2 saturate(vec2 x) {
return clamp(x, vec2(0.), vec2(1.));
}
vec3 saturate(vec3 x) {
return clamp(x, vec3(0.), vec3(1.));
}
vec4 saturate(vec4 x) {
return clamp(x, vec4(0.), vec4(1.));
}
vec4 illuminate(SCNShaderSurface surface, SCNShaderLightingContribution lighting)
{
vec4 color = vec4(0.,0.,0., surface.diffuse.a);
vec3 D = lighting.diffuse;
#ifdef USE_AMBIENT_LIGHTING
#ifdef USE_AMBIENT_AS_AMBIENTOCCLUSION
D += lighting.ambient * surface.ambientOcclusion;
#elif defined(LOCK_AMBIENT_WITH_DIFFUSE)
D += lighting.ambient;
#endif
#endif // defined(USE_AMBIENT_LIGHTING)
#ifdef USE_EMISSION_AS_SELFILLUMINATION
D += surface.emission.rgb;
#endif
// Do we want to clamp there ????
color.rgb = surface.diffuse.rgb * D;
#if 1 // NEW_REFLECTIVE_BEHAVIOR
#ifdef USE_SPECULAR
vec3 S = lighting.specular;
#elif defined(USE_REFLECTIVE)
vec3 S = vec3(0.);
#endif
#ifdef USE_REFLECTIVE
#ifdef USE_AMBIENT_AS_AMBIENTOCCLUSION
S += surface.reflective.rgb * surface.ambientOcclusion;
#else
S += surface.reflective.rgb;
#endif
#endif
#ifdef USE_SPECULAR
S *= surface.specular.rgb;
#endif
#if defined(USE_SPECULAR) || defined(USE_REFLECTIVE)
color.rgb += S;
#endif
#else
#ifdef USE_SPECULAR
color.rgb += surface.specular.rgb * lighting.specular;
#endif
#ifdef USE_REFLECTIVE
color.rgb += surface.reflective.rgb * (lighting.diffuse + lighting.ambient);
#endif
#endif // NEW_REFLECTIVE_BEHAVIOR
#if defined(USE_AMBIENT) && !defined(USE_AMBIENT_AS_AMBIENTOCCLUSION)
color.rgb += surface.ambient.rgb * lighting.ambient;
#endif
#if defined(USE_EMISSION) && !defined(USE_EMISSION_AS_SELFILLUMINATION)
color.rgb += surface.emission.rgb;
#endif
#ifdef USE_MULTIPLY
color.rgb *= surface.multiply.rgb;
#endif
#ifdef USE_MODULATE
color.rgb *= lighting.modulate;
#endif
return color;
}
struct SCNOutput
{
vec4 color;
} _output;
void main(void)
{
#ifdef USE_TEXCOORD
_surface.diffuseTexcoord = v_texcoord0;
#endif
_surface.ambientOcclusion = 1.0;
//ambient
#ifdef USE_AMBIENT_MAP
_surface.ambient = texture2D(u_ambientTexture, _surface.ambientTexcoord);
#ifdef USE_AMBIENT_INTENSITY
#ifdef USE_AMBIENT_AS_AMBIENTOCCLUSION
_surface.ambientOcclusion = mix(1., _surface.ambient.r, u_ambientIntensity);
#else
_surface.ambient *= u_ambientIntensity;
#endif
#endif
#elif defined(USE_AMBIENT_COLOR)
_surface.ambient = u_ambientColor;
#elif defined(USE_AMBIENT)
_surface.ambient = vec4(0.);
#endif
#if defined(USE_AMBIENT) && defined(USE_VERTEX_COLOR)
_surface.ambient *= v_vertexColor;
#endif
//diffuse
#ifdef USE_DIFFUSE_MAP
_surface.diffuse = texture2D(u_diffuseTexture, _surface.diffuseTexcoord);
#ifdef USE_DIFFUSE_INTENSITY
_surface.diffuse.rgb *= u_diffuseIntensity;
#endif
#elif defined(USE_DIFFUSE_COLOR)
_surface.diffuse = u_diffuseColor;
#elif defined(USE_DIFFUSE)
_surface.diffuse = vec4(0.,0.,0.,1.);
#endif
#if defined(USE_DIFFUSE) && defined(USE_VERTEX_COLOR)
_surface.diffuse *= v_vertexColor;
#endif
//specular
#ifdef USE_SPECULAR_MAP
_surface.specular = texture2D(u_specularTexture, _surface.specularTexcoord);
#ifdef USE_SPECULAR_INTENSITY
_surface.specular *= u_specularIntensity;
#endif
#elif defined(USE_SPECULAR_COLOR)
_surface.specular = u_specularColor;
#elif defined(USE_SPECULAR)
_surface.specular = vec4(0.);
#endif
//emission
#ifdef USE_EMISSION_MAP
_surface.emission = texture2D(u_emissionTexture, _surface.emissionTexcoord);
#ifdef USE_EMISSION_INTENSITY
_surface.emission *= u_emissionIntensity;
#endif
#elif defined(USE_EMISSION_COLOR)
_surface.emission = u_emissionColor;
#elif defined(USE_EMISSION)
_surface.emission = vec4(0.);
#endif
//multiply
#ifdef USE_MULTIPLY_MAP
_surface.multiply = texture2D(u_multiplyTexture, _surface.multiplyTexcoord);
#ifdef USE_MULTIPLY_INTENSITY
_surface.multiply = mix(vec4(1.), _surface.multiply, u_multiplyIntensity);
#endif
#elif defined(USE_MULTIPLY_COLOR)
_surface.multiply = u_multiplyColor;
#elif defined(USE_MULTIPLY)
_surface.multiply = vec4(1.);
#endif
//transparent
#ifdef USE_TRANSPARENT_MAP
_surface.transparent = texture2D(u_transparentTexture, _surface.transparentTexcoord);
#ifdef USE_TRANSPARENT_INTENSITY
_surface.transparent *= u_transparentIntensity;
#endif
#elif defined(USE_TRANSPARENT_COLOR)
_surface.transparent = u_transparentColor;
#elif defined(USE_TRANSPARENT)
_surface.transparent = vec4(1.);
#endif
//normal
#if (defined USE_NORMAL) && (USE_NORMAL == 2)
#ifdef USE_DOUBLE_SIDED
_surface.normal = normalize(v_normal.xyz) * u_orientationPreserved * ((float(gl_FrontFacing) * 2.0) - 1.0);
#else
_surface.normal = normalize(v_normal.xyz);
#endif
#endif
#if defined(USE_TANGENT) && (USE_TANGENT == 2)
_surface.tangent = v_tangent;
#endif
#if defined(USE_BITANGENT) && (USE_BITANGENT == 2)
_surface.bitangent = v_bitangent;
#endif
#if (defined USE_POSITION) && (USE_POSITION == 2)
_surface.position = v_position;
#endif
#if (defined USE_VIEW) && (USE_VIEW == 2)
_surface.view = normalize(-v_position);
#endif
#ifdef USE_NORMAL_MAP
mat3 ts2vs = mat3(_surface.tangent, _surface.bitangent, _surface.normal);
_surface._normalTS = texture2D(u_normalTexture, _surface.normalTexcoord).rgb * 2. - 1.;
// _surface.normal.z = 1. - sqrt(_surface.normal.x * _surface.normal.x + _surface.normal.y * _surface.normal.y);
#ifdef USE_NORMAL_INTENSITY
_surface._normalTS = mix(vec3(0., 0., 1.), _surface._normalTS, u_normalIntensity);
#endif
// transform the normal in view space
_surface.normal.rgb = normalize(ts2vs * _surface._normalTS);
#else
_surface._normalTS = vec3(0., 0., 0.);
#endif
//reflective
#ifdef USE_REFLECTIVE_MAP
vec3 refl = reflect( -_surface.view, _surface.normal );
float m = 2.0 * sqrt( refl.x*refl.x + refl.y*refl.y + (refl.z+1.0)*(refl.z+1.0));
_surface.reflective = texture2D(u_reflectiveTexture,vec2(vec2(refl.x,-refl.y) / m) + 0.5) ;
#ifdef USE_REFLECTIVE_INTENSITY
_surface.reflective.rgb *= u_reflectiveIntensity;
#endif
#elif defined(USE_REFLECTIVE_CUBEMAP)
vec3 refl = reflect( _surface.position, _surface.normal );
_surface.reflective = textureCube(u_reflectiveTexture, mat3(u_viewToCubeWorld) * refl); // sample the cube map in world space
#ifdef USE_REFLECTIVE_INTENSITY
_surface.reflective.rgb *= u_reflectiveIntensity;
#endif
#elif defined(USE_REFLECTIVE_COLOR)
_surface.reflective = u_reflectiveColor;
#elif defined(USE_REFLECTIVE)
_surface.reflective = vec4(0.);
#endif
#ifdef USE_FRESNEL
_surface.fresnel = u_fresnel.x + u_fresnel.y * pow(1.0 - clamp(dot(_surface.view, _surface.normal), 0.0, 1.0), u_fresnel.z);
_surface.reflective *= _surface.fresnel;
#endif
#ifdef USE_SHININESS
_surface.shininess = u_materialShininess;
#endif
#ifdef USE_SURFACE_MODIFIER
// DoSurfaceModifier START
// This is the surface entry point
// DoSurfaceModifier END
#endif
// Lighting
#ifdef USE_AMBIENT_LIGHTING
_lightingContribution.ambient = u_ambientLightColor.rgb;
#elif defined(USE_AMBIENT)
_lightingContribution.ambient = vec3(0.);
#endif
#ifdef USE_LIGHTING
#ifdef USE_PER_PIXEL_LIGHTING
_lightingContribution.diffuse = vec3(0.);
#ifdef USE_MODULATE
_lightingContribution.modulate = vec3(1.);
#endif
#ifdef USE_SPECULAR
_lightingContribution.specular = vec3(0.);
#endif
__DoLighting__
#else // USE_PER_PIXEL_LIGHTING
_lightingContribution.diffuse = v_diffuse;
#ifdef USE_SPECULAR
_lightingContribution.specular = v_specular;
#endif
#endif
#ifdef AVOID_OVERLIGHTING
_lightingContribution.diffuse = clamp(_lightingContribution.diffuse, vec3(0.), vec3(1.));
#ifdef USE_SPECULAR
_lightingContribution.specular = clamp(_lightingContribution.specular, vec3(0.), vec3(1.));
#endif // USE_SPECULAR
#endif // AVOID_OVERLIGHTING
#else // USE_LIGHTING
_lightingContribution.diffuse = vec3(1.);
#endif // USE_LIGHTING
// Combine
_output.color = illuminate(_surface, _lightingContribution);
#ifdef USE_FOG
float fogFactor = pow(clamp(length(_surface.position.xyz) * u_fogParameters.x + u_fogParameters.y, 0., u_fogColor.a), u_fogParameters.z);
_output.color.rgb = mix(_output.color.rgb, u_fogColor.rgb * _output.color.a, fogFactor);
#endif
#ifndef DIFFUSE_PREMULTIPLIED
_output.color.rgb *= _surface.diffuse.a;
#endif
#ifdef USE_TRANSPARENT // Either a map or a color
#ifdef USE_TRANSPARENCY
_surface.transparent *= u_transparency;
#endif
#ifdef USE_TRANSPARENCY_RGBZERO
#ifdef USE_NODE_OPACITY
_output.color *= u_nodeOpacity;
#endif
// compute luminance
_surface.transparent.a = (_surface.transparent.r * 0.212671) + (_surface.transparent.g * 0.715160) + (_surface.transparent.b * 0.072169);
_output.color *= (vec4(1.) - _surface.transparent);
#else // ALPHA_ONE
_output.color *= _surface.transparent.a;
#endif
#else
#ifdef USE_TRANSPARENCY // TRANSPARENCY without TRANSPARENT slot (nodeOpacity + diffuse.a)
_output.color *= u_transparency;
#endif
#endif
#ifdef USE_FRAGMENT_MODIFIER
// DoFragmentModifier START
// This is the color entry point
a=b;
// DoFragmentModifier END
#endif
#ifdef USE_DISCARD
if (_output.color.a == 0.) // we could set a different limit here
discard;
#endif
gl_FragColor = _output.color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment