Skip to content

Instantly share code, notes, and snippets.

@hjanetzek
Last active November 7, 2016 09:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjanetzek/f06f6359d6ba240669ce85a359bfe682 to your computer and use it in GitHub Desktop.
Save hjanetzek/f06f6359d6ba240669ce85a359bfe682 to your computer and use it in GitHub Desktop.
TANGRAM shaderProgram.cpp:145: >>> VERTEX {style:heightglowline}
#define TANGRAM_EPSILON 0.00001
#define TANGRAM_WORLD_POSITION_WRAP 100000.
#define TANGRAM_DEPTH_DELTA 0.000031
#define TANGRAM_VERTEX_SHADER
#pragma tangram: extensions
#ifdef GL_ES
precision highp float;
#endif
#pragma tangram: defines
#undef STYLE
#define STYLE heightglowline
#define TANGRAM_LIGHTING_VERTEX
#define TANGRAM_MATERIAL_DIFFUSE
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_proj;
uniform mat3 u_normal_matrix;
uniform vec4 u_tile_origin;
uniform vec3 u_map_position;
uniform vec2 u_resolution;
uniform float u_time;
uniform float u_meters_per_pixel;
uniform float u_device_pixel_ratio;
uniform float u_proxy_depth;
#pragma tangram: uniforms
attribute vec4 a_position;
attribute vec4 a_color;
attribute vec4 a_extrude;
#ifdef TANGRAM_USE_TEX_COORDS
attribute vec2 a_texcoord;
varying vec2 v_texcoord;
#endif
#ifdef TANGRAM_FEATURE_SELECTION
// Make sure lighting is a no-op for feature selection pass
#undef TANGRAM_LIGHTING_VERTEX
attribute vec4 a_selection_color;
varying vec4 v_selection_color;
#endif
varying vec4 v_world_position;
varying vec4 v_position;
varying vec4 v_color;
varying vec3 v_normal;
#ifdef TANGRAM_LIGHTING_VERTEX
varying vec4 v_lighting;
#endif
#define UNPACK_POSITION(x) (x / 8192.0)
#define UNPACK_EXTRUSION(x) (x / 4096.0)
#define UNPACK_ORDER(x) (x / 2.0)
#define UNPACK_TEXCOORD(x) (x / 8192.0)
vec4 modelPosition() {
return vec4(UNPACK_POSITION(a_position.xyz) * exp2(u_tile_origin.z - u_tile_origin.w), 1.0);
}
vec4 worldPosition() {
return v_world_position;
}
vec3 worldNormal() {
return vec3(0.0, 0.0, 1.0);
}
vec4 modelPositionBaseZoom() {
return vec4(UNPACK_POSITION(a_position.xyz), 1.0);
}
#pragma tangram: material
/*
Defines globals:
material
light_accumulator_*
*/
#ifdef TANGRAM_WEBGL
#define TANGRAM_SKEW u_vanishing_point
#else
#define TANGRAM_SKEW vec2(0.0)
#endif
// MATERIALS
//
struct Material {
#ifdef TANGRAM_MATERIAL_EMISSION
vec4 emission;
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
vec3 emissionScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT
vec4 ambient;
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
vec3 ambientScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE
vec4 diffuse;
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
vec3 diffuseScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR
vec4 specular;
float shininess;
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
vec3 specularScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
vec3 normalScale;
vec3 normalAmount;
#endif
};
// Note: uniform is copied to a global instance to allow modification
uniform Material u_material;
Material material;
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
uniform sampler2D u_material_emission_texture;
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
uniform sampler2D u_material_ambient_texture;
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
uniform sampler2D u_material_diffuse_texture;
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
uniform sampler2D u_material_specular_texture;
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
uniform sampler2D u_material_normal_texture;
#endif
// Global light accumulators for each property
vec4 light_accumulator_ambient = vec4(0.0);
vec4 light_accumulator_diffuse = vec4(0.0);
#ifdef TANGRAM_MATERIAL_SPECULAR
vec4 light_accumulator_specular = vec4(0.0);
#endif
#ifdef TANGRAM_MATERIAL_TEXTURE_SPHEREMAP
vec4 getSphereMap (in sampler2D _tex, in vec3 _eyeToPoint, in vec3 _normal, in vec2 _skew) {
vec3 eye = normalize(_eyeToPoint);
eye.xy -= _skew;
eye = normalize(eye);
vec3 r = reflect(eye, _normal);
r.z += 1.0;
float m = 2. * length(r);
vec2 uv = r.xy / m + .5;
return texture2D(_tex, uv);
}
#endif
#ifdef TANGRAM_MATERIAL_TEXTURE_TRIPLANAR
vec3 getTriPlanarBlend (in vec3 _normal) {
vec3 blending = abs(_normal);
blending = normalize(max(blending, 0.00001));
float b = (blending.x + blending.y + blending.z);
return blending / b;
}
vec4 getTriPlanar (in sampler2D _tex, in vec3 _pos, in vec3 _normal, in vec3 _scale) {
vec3 blending = getTriPlanarBlend(_normal);
vec4 xaxis = texture2D(_tex, fract(_pos.yz * _scale.x));
vec4 yaxis = texture2D(_tex, fract(_pos.xz * _scale.y));
vec4 zaxis = texture2D(_tex, fract(_pos.xy * _scale.z));
return xaxis * blending.x + yaxis * blending.y + zaxis * blending.z;
}
#endif
#ifdef TANGRAM_MATERIAL_TEXTURE_PLANAR
vec4 getPlanar (in sampler2D _tex, in vec3 _pos, in vec2 _scale) {
return texture2D( _tex, fract(_pos.xy * _scale.x) );
}
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
void calculateNormal (inout vec3 _normal) {
// Get NORMALMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_UV
_normal += texture2D(u_material_normal_texture, fract(v_texcoord*material.normalScale.xy)).rgb*2.0-1.0;
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_PLANAR
vec3 normalTex = getPlanar(u_material_normal_texture, v_world_position.xyz, material.normalScale.xy).rgb*2.0-1.0;
_normal += normalTex;
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_TRIPLANAR
vec3 normalTex = getTriPlanar(u_material_normal_texture, v_world_position.xyz, _normal, material.normalScale).rgb*2.0-1.0;
_normal += normalTex;
#endif
_normal = normalize(_normal);
}
#endif
#if (defined(TANGRAM_VERTEX_SHADER) && defined(TANGRAM_LIGHTING_VERTEX)) || (defined(TANGRAM_FRAGMENT_SHADER) && defined(TANGRAM_LIGHTING_FRAGMENT))
void calculateMaterial (in vec3 _eyeToPoint, inout vec3 _normal) {
// get EMISSION TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_UV
material.emission *= texture2D(u_material_emission_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_PLANAR
material.emission *= getPlanar(u_material_emission_texture, v_world_position.xyz, material.emissionScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_TRIPLANAR
material.emission *= getTriPlanar(u_material_emission_texture, v_world_position.xyz, _normal, material.emissionScale);
#endif
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_SPHEREMAP
material.emission *= getSphereMap(u_material_emission_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
// get AMBIENT TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_UV
material.ambient *= texture2D(u_material_ambient_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_PLANAR
material.ambient *= getPlanar(u_material_ambient_texture, v_world_position.xyz, material.ambientScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_TRIPLANAR
material.ambient *= getTriPlanar(u_material_ambient_texture, v_world_position.xyz, _normal, material.ambientScale);
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_SPHEREMAP
material.ambient *= getSphereMap(u_material_ambient_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
// get DIFFUSE TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_UV
material.diffuse *= texture2D(u_material_diffuse_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_PLANAR
material.diffuse *= getPlanar(u_material_diffuse_texture, v_world_position.xyz, material.diffuseScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_TRIPLANAR
material.diffuse *= getTriPlanar(u_material_diffuse_texture, v_world_position.xyz, _normal, material.diffuseScale);
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_SPHEREMAP
material.diffuse *= getSphereMap(u_material_diffuse_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
// get SPECULAR TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_UV
material.specular *= texture2D(u_material_specular_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_PLANAR
material.specular *= getPlanar(u_material_specular_texture, v_world_position.xyz, material.specularScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_TRIPLANAR
material.specular *= getTriPlanar(u_material_specular_texture, v_world_position.xyz, _normal, material.specularScale);
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_SPHEREMAP
material.specular *= getSphereMap(u_material_specular_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
}
#endif
#pragma tangram: lighting
/*
Expected globals:
material
light_accumulator_*
*/
struct DirectionalLight {
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 direction;
};
void calculateLight(in DirectionalLight _light, in vec3 _eyeToPoint, in vec3 _normal) {
light_accumulator_ambient += _light.ambient;
float nDotVP = clamp(dot(_normal, -_light.direction), 0.0, 1.0);
#ifdef TANGRAM_MATERIAL_DIFFUSE
light_accumulator_diffuse += _light.diffuse * nDotVP;
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR
float pf = 0.0;
if (nDotVP > 0.0) {
vec3 reflectVector = reflect(_light.direction, _normal);
float eyeDotR = max(dot(normalize(_eyeToPoint), reflectVector), 0.0);
pf = pow(eyeDotR, material.shininess);
}
light_accumulator_specular += _light.specular * pf;
#endif
}
uniform DirectionalLight u_light1;
DirectionalLight light1;
#if (defined(TANGRAM_VERTEX_SHADER) && defined(TANGRAM_LIGHTING_VERTEX)) || (defined(TANGRAM_FRAGMENT_SHADER) && defined(TANGRAM_LIGHTING_FRAGMENT))
vec4 calculateLighting(in vec3 _eyeToPoint, in vec3 _normal, in vec4 _color) {
// Do initial material calculations over normal, emission, ambient, diffuse and specular values
calculateMaterial(_eyeToPoint,_normal);
// Unroll the loop of individual lights to calculate
#pragma tangram: lights_to_compute
calculateLight(light1, _eyeToPoint, _normal);
// Final light intensity calculation
vec4 color = vec4(0.0);
#ifdef TANGRAM_MATERIAL_EMISSION
color = material.emission;
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT
color += light_accumulator_ambient * _color * material.ambient;
#else
#ifdef TANGRAM_MATERIAL_DIFFUSE
color += light_accumulator_ambient * _color * material.diffuse;
#endif
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE
color += light_accumulator_diffuse * _color * material.diffuse;
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR
color += light_accumulator_specular * material.specular;
#endif
// Clamp final color
color = clamp(color, 0.0, 1.0);
return color;
}
#endif
#pragma tangram: global
#pragma tangram: raster
#ifdef TANGRAM_MODEL_POSITION_BASE_ZOOM_VARYING
varying vec4 v_modelpos_base_zoom;
#endif
void main() {
vec4 position = vec4(UNPACK_POSITION(a_position.xyz), 1.0);
#ifdef TANGRAM_FEATURE_SELECTION
v_selection_color = a_selection_color;
// Skip non-selectable meshes
if (v_selection_color == vec4(0.0)) {
gl_Position = vec4(0.0);
return;
}
#else
// Initialize globals
#pragma tangram: setup
material = u_material;
light1 = u_light1;
#endif
v_color = a_color;
#ifdef TANGRAM_USE_TEX_COORDS
v_texcoord = UNPACK_TEXCOORD(a_texcoord);
#endif
#ifdef TANGRAM_MODEL_POSITION_BASE_ZOOM_VARYING
v_modelpos_base_zoom = modelPositionBaseZoom();
#endif
v_normal = u_normal_matrix * vec3(0.,0.,1.);
{
vec4 extrude = UNPACK_EXTRUSION(a_extrude);
float width = extrude.z;
float dwdz = extrude.w;
float dz = u_map_position.z - u_tile_origin.z;
// Interpolate between zoom levels
width += dwdz * clamp(dz, 0.0, 1.0);
// Scale pixel dimensions to be consistent in screen space
// and adjust scale for overzooming.
width *= exp2(-dz + (u_tile_origin.w - u_tile_origin.z));
// Modify line width in model space before extrusion
#pragma tangram: width
#ifdef TANGRAM_USE_TEX_COORDS
v_texcoord.y /= 2. * extrude.z;
#endif
position.xy += extrude.xy * width;
}
// Transform position into meters relative to map center
position = u_model * position;
// World coordinates for 3d procedural textures
vec4 local_origin = vec4(u_map_position.xy, 0., 0.);
#ifdef TANGRAM_WORLD_POSITION_WRAP
local_origin = mod(local_origin, TANGRAM_WORLD_POSITION_WRAP);
#endif
v_world_position = position + local_origin;
// Modify position before lighting and camera projection
#pragma tangram: position
// Set position varying to the camera-space vertex position
v_position = u_view * position;
#ifdef TANGRAM_LIGHTING_VERTEX
// Modify normal before lighting
vec3 normal = v_normal;
#pragma tangram: normal
v_lighting = calculateLighting(v_position.xyz, normal, vec4(1.));
#endif
gl_Position = u_proj * v_position;
// Proxy tiles are placed deeper in the depth buffer than non-proxy tiles
gl_Position.z += TANGRAM_DEPTH_DELTA * gl_Position.w * u_proxy_depth;
#ifdef TANGRAM_DEPTH_DELTA
float layer = UNPACK_ORDER(a_position.w);
gl_Position.z -= layer * TANGRAM_DEPTH_DELTA * gl_Position.w;
#endif
}
<<<<<<
TANGRAM shaderProgram.cpp:146: >>> FRAGMENT {style:heightglowline}
#define TANGRAM_EPSILON 0.00001
#define TANGRAM_WORLD_POSITION_WRAP 100000.
#define TANGRAM_FRAGMENT_SHADER
#pragma tangram: extensions
#ifdef GL_ES
precision highp float;
#endif
#pragma tangram: defines
#undef STYLE
#define STYLE heightglowline
#define TANGRAM_LIGHTING_VERTEX
#define TANGRAM_MATERIAL_DIFFUSE
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_proj;
uniform mat3 u_normal_matrix;
uniform mat3 u_inverse_normal_matrix;
uniform vec4 u_tile_origin;
uniform vec3 u_map_position;
uniform vec2 u_resolution;
uniform float u_time;
uniform float u_meters_per_pixel;
uniform float u_device_pixel_ratio;
uniform float u_texture_ratio;
uniform sampler2D u_texture;
#pragma tangram: uniforms
varying vec4 v_world_position;
varying vec4 v_position;
varying vec4 v_color;
varying vec3 v_normal;
#ifdef TANGRAM_USE_TEX_COORDS
varying vec2 v_texcoord;
#endif
#ifdef TANGRAM_LIGHTING_VERTEX
varying vec4 v_lighting;
#endif
vec4 worldPosition() {
return v_world_position;
}
vec3 worldNormal() {
return normalize(u_inverse_normal_matrix * v_normal);
}
#pragma tangram: material
/*
Defines globals:
material
light_accumulator_*
*/
#ifdef TANGRAM_WEBGL
#define TANGRAM_SKEW u_vanishing_point
#else
#define TANGRAM_SKEW vec2(0.0)
#endif
// MATERIALS
//
struct Material {
#ifdef TANGRAM_MATERIAL_EMISSION
vec4 emission;
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
vec3 emissionScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT
vec4 ambient;
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
vec3 ambientScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE
vec4 diffuse;
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
vec3 diffuseScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR
vec4 specular;
float shininess;
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
vec3 specularScale;
#endif
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
vec3 normalScale;
vec3 normalAmount;
#endif
};
// Note: uniform is copied to a global instance to allow modification
uniform Material u_material;
Material material;
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
uniform sampler2D u_material_emission_texture;
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
uniform sampler2D u_material_ambient_texture;
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
uniform sampler2D u_material_diffuse_texture;
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
uniform sampler2D u_material_specular_texture;
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
uniform sampler2D u_material_normal_texture;
#endif
// Global light accumulators for each property
vec4 light_accumulator_ambient = vec4(0.0);
vec4 light_accumulator_diffuse = vec4(0.0);
#ifdef TANGRAM_MATERIAL_SPECULAR
vec4 light_accumulator_specular = vec4(0.0);
#endif
#ifdef TANGRAM_MATERIAL_TEXTURE_SPHEREMAP
vec4 getSphereMap (in sampler2D _tex, in vec3 _eyeToPoint, in vec3 _normal, in vec2 _skew) {
vec3 eye = normalize(_eyeToPoint);
eye.xy -= _skew;
eye = normalize(eye);
vec3 r = reflect(eye, _normal);
r.z += 1.0;
float m = 2. * length(r);
vec2 uv = r.xy / m + .5;
return texture2D(_tex, uv);
}
#endif
#ifdef TANGRAM_MATERIAL_TEXTURE_TRIPLANAR
vec3 getTriPlanarBlend (in vec3 _normal) {
vec3 blending = abs(_normal);
blending = normalize(max(blending, 0.00001));
float b = (blending.x + blending.y + blending.z);
return blending / b;
}
vec4 getTriPlanar (in sampler2D _tex, in vec3 _pos, in vec3 _normal, in vec3 _scale) {
vec3 blending = getTriPlanarBlend(_normal);
vec4 xaxis = texture2D(_tex, fract(_pos.yz * _scale.x));
vec4 yaxis = texture2D(_tex, fract(_pos.xz * _scale.y));
vec4 zaxis = texture2D(_tex, fract(_pos.xy * _scale.z));
return xaxis * blending.x + yaxis * blending.y + zaxis * blending.z;
}
#endif
#ifdef TANGRAM_MATERIAL_TEXTURE_PLANAR
vec4 getPlanar (in sampler2D _tex, in vec3 _pos, in vec2 _scale) {
return texture2D( _tex, fract(_pos.xy * _scale.x) );
}
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
void calculateNormal (inout vec3 _normal) {
// Get NORMALMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_UV
_normal += texture2D(u_material_normal_texture, fract(v_texcoord*material.normalScale.xy)).rgb*2.0-1.0;
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_PLANAR
vec3 normalTex = getPlanar(u_material_normal_texture, v_world_position.xyz, material.normalScale.xy).rgb*2.0-1.0;
_normal += normalTex;
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE_TRIPLANAR
vec3 normalTex = getTriPlanar(u_material_normal_texture, v_world_position.xyz, _normal, material.normalScale).rgb*2.0-1.0;
_normal += normalTex;
#endif
_normal = normalize(_normal);
}
#endif
#if (defined(TANGRAM_VERTEX_SHADER) && defined(TANGRAM_LIGHTING_VERTEX)) || (defined(TANGRAM_FRAGMENT_SHADER) && defined(TANGRAM_LIGHTING_FRAGMENT))
void calculateMaterial (in vec3 _eyeToPoint, inout vec3 _normal) {
// get EMISSION TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_UV
material.emission *= texture2D(u_material_emission_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_PLANAR
material.emission *= getPlanar(u_material_emission_texture, v_world_position.xyz, material.emissionScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_TRIPLANAR
material.emission *= getTriPlanar(u_material_emission_texture, v_world_position.xyz, _normal, material.emissionScale);
#endif
#ifdef TANGRAM_MATERIAL_EMISSION_TEXTURE_SPHEREMAP
material.emission *= getSphereMap(u_material_emission_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
// get AMBIENT TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_UV
material.ambient *= texture2D(u_material_ambient_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_PLANAR
material.ambient *= getPlanar(u_material_ambient_texture, v_world_position.xyz, material.ambientScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_TRIPLANAR
material.ambient *= getTriPlanar(u_material_ambient_texture, v_world_position.xyz, _normal, material.ambientScale);
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT_TEXTURE_SPHEREMAP
material.ambient *= getSphereMap(u_material_ambient_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
// get DIFFUSE TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_UV
material.diffuse *= texture2D(u_material_diffuse_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_PLANAR
material.diffuse *= getPlanar(u_material_diffuse_texture, v_world_position.xyz, material.diffuseScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_TRIPLANAR
material.diffuse *= getTriPlanar(u_material_diffuse_texture, v_world_position.xyz, _normal, material.diffuseScale);
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE_TEXTURE_SPHEREMAP
material.diffuse *= getSphereMap(u_material_diffuse_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
// get SPECULAR TEXTUREMAP
//------------------------------------------------
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_UV
material.specular *= texture2D(u_material_specular_texture,v_texcoord);
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_PLANAR
material.specular *= getPlanar(u_material_specular_texture, v_world_position.xyz, material.specularScale.xy);
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_TRIPLANAR
material.specular *= getTriPlanar(u_material_specular_texture, v_world_position.xyz, _normal, material.specularScale);
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR_TEXTURE_SPHEREMAP
material.specular *= getSphereMap(u_material_specular_texture, _eyeToPoint, _normal, TANGRAM_SKEW);
#endif
#endif
}
#endif
#pragma tangram: lighting
/*
Expected globals:
material
light_accumulator_*
*/
struct DirectionalLight {
vec4 ambient;
vec4 diffuse;
vec4 specular;
vec3 direction;
};
void calculateLight(in DirectionalLight _light, in vec3 _eyeToPoint, in vec3 _normal) {
light_accumulator_ambient += _light.ambient;
float nDotVP = clamp(dot(_normal, -_light.direction), 0.0, 1.0);
#ifdef TANGRAM_MATERIAL_DIFFUSE
light_accumulator_diffuse += _light.diffuse * nDotVP;
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR
float pf = 0.0;
if (nDotVP > 0.0) {
vec3 reflectVector = reflect(_light.direction, _normal);
float eyeDotR = max(dot(normalize(_eyeToPoint), reflectVector), 0.0);
pf = pow(eyeDotR, material.shininess);
}
light_accumulator_specular += _light.specular * pf;
#endif
}
uniform DirectionalLight u_light1;
DirectionalLight light1;
#if (defined(TANGRAM_VERTEX_SHADER) && defined(TANGRAM_LIGHTING_VERTEX)) || (defined(TANGRAM_FRAGMENT_SHADER) && defined(TANGRAM_LIGHTING_FRAGMENT))
vec4 calculateLighting(in vec3 _eyeToPoint, in vec3 _normal, in vec4 _color) {
// Do initial material calculations over normal, emission, ambient, diffuse and specular values
calculateMaterial(_eyeToPoint,_normal);
// Unroll the loop of individual lights to calculate
#pragma tangram: lights_to_compute
calculateLight(light1, _eyeToPoint, _normal);
// Final light intensity calculation
vec4 color = vec4(0.0);
#ifdef TANGRAM_MATERIAL_EMISSION
color = material.emission;
#endif
#ifdef TANGRAM_MATERIAL_AMBIENT
color += light_accumulator_ambient * _color * material.ambient;
#else
#ifdef TANGRAM_MATERIAL_DIFFUSE
color += light_accumulator_ambient * _color * material.diffuse;
#endif
#endif
#ifdef TANGRAM_MATERIAL_DIFFUSE
color += light_accumulator_diffuse * _color * material.diffuse;
#endif
#ifdef TANGRAM_MATERIAL_SPECULAR
color += light_accumulator_specular * material.specular;
#endif
// Clamp final color
color = clamp(color, 0.0, 1.0);
return color;
}
#endif
#pragma tangram: global
#pragma tangram: raster
#ifdef TANGRAM_MODEL_POSITION_BASE_ZOOM_VARYING
varying vec4 v_modelpos_base_zoom;
#endif
void main(void) {
// Initialize globals
#pragma tangram: setup
material = u_material;
light1 = u_light1;
vec4 color = v_color;
vec3 normal = v_normal;
#ifdef TANGRAM_RASTER_TEXTURE_COLOR
color *= sampleRaster(0);
#endif
#ifdef TANGRAM_LINE_TEXTURE
vec2 line_st = vec2(v_texcoord.x, fract(v_texcoord.y / u_texture_ratio));
vec4 line_color = texture2D(u_texture, line_st);
if (line_color.a < TANGRAM_ALPHA_TEST) {
#ifdef TANGRAM_LINE_BACKGROUND_COLOR
color.rgb = TANGRAM_LINE_BACKGROUND_COLOR;
#elif !defined(TANGRAM_BLEND_OVERLAY) && !defined(TANGRAM_BLEND_INLAY)
discard;
#else
color.a = 0.0;
#endif
} else {
color *= line_color;
}
#endif
#ifdef TANGRAM_RASTER_TEXTURE_NORMAL
normal = normalize(sampleRaster(0).rgb * 2.0 - 1.0);
#endif
#ifdef TANGRAM_MATERIAL_NORMAL_TEXTURE
calculateNormal(normal);
#endif
// Modify normal before lighting if not already modified in vertex shader
#if !defined(TANGRAM_LIGHTING_VERTEX)
#pragma tangram: normal
#endif
// Modify color before lighting is applied
#pragma tangram: color
color.rgb += vec3(worldPosition().z / 800.);
#if defined(TANGRAM_LIGHTING_FRAGMENT)
color = calculateLighting(v_position.xyz, normal, color);
#elif defined(TANGRAM_LIGHTING_VERTEX)
color *= v_lighting;
#endif
// Modify color after lighting (filter-like effects that don't require a additional render passes)
#pragma tangram: filter
gl_FragColor = color;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment