Path: blob/main/projects/HexGL/bkcore/threejs/Shaders.js
4627 views
/**1* @author Thibaut Despoulain / http://bkcore.com2* @author alteredq / http://alteredqualia.com/3* @author mr.doob / http://mrdoob.com/4*/5var bkcore = bkcore || {};6bkcore.threejs = bkcore.threejs || {};78bkcore.threejs.Shaders =9{10'additive' : {11uniforms: {12tDiffuse: { type: "t", value: 0, texture: null },13tAdd: { type: "t", value: 1, texture: null },14fCoeff: { type: "f", value: 1.0 }15},1617vertexShader: [18"varying vec2 vUv;",1920"void main() {",2122"vUv = uv;",23"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",2425"}"26].join("\n"),2728fragmentShader: [29"uniform sampler2D tDiffuse;",30"uniform sampler2D tAdd;",31"uniform float fCoeff;",3233"varying vec2 vUv;",3435"void main() {",3637"vec4 texel = texture2D( tDiffuse, vUv );",38"vec4 add = texture2D( tAdd, vUv );",39"gl_FragColor = texel + add * fCoeff * add.a;",4041"}"42].join("\n")43},4445/* ------------------------------------------------------------------------------------------------46// Hexagonal Vignette shader47// by BKcore.com48------------------------------------------------------------------------------------------------ */4950'hexvignette': {5152uniforms: {5354tDiffuse: { type: "t", value: 0, texture: null },55tHex: {type: "t", value: 1, texture: null},56size: {type: "f", value: 512.0},57rx: {type: "f", value: 1024.0},58ry: {type: "f", value: 768.0},59color: {type: "c", value: new THREE.Color(0x458ab1)}6061},6263vertexShader: [6465"varying vec2 vUv;",6667"void main() {",6869"vUv = uv;",70"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",7172"}"7374].join("\n"),7576fragmentShader: [7778"uniform float size;",79"uniform float rx;",80"uniform float ry;",8182"uniform vec3 color;",8384"uniform sampler2D tDiffuse;",85"uniform sampler2D tHex;",8687"varying vec2 vUv;",8889"void main() {",9091"vec4 vcolor = vec4(color,1.0);",9293"vec2 hexuv;",94"hexuv.x = mod(vUv.x * rx, size) / size;",95"hexuv.y = mod(vUv.y * ry, size) / size;",96"vec4 hex = texture2D( tHex, hexuv );",9798"float tolerance = 0.2;",99"float vignette_size = 0.6;",100"vec2 powers = pow(abs(vec2(vUv.x - 0.5,vUv.y - 0.5)),vec2(2.0));",101"float radiusSqrd = vignette_size*vignette_size;",102"float gradient = smoothstep(radiusSqrd-tolerance, radiusSqrd+tolerance, powers.x+powers.y);",103104"vec2 uv = ( vUv - vec2( 0.5 ) );",105106"vec2 sample = uv * gradient * 0.5 * (1.0-hex.r);",107108"vec4 texel = texture2D( tDiffuse, vUv-sample );",109"gl_FragColor = (((1.0-hex.r)*vcolor) * 0.5 * gradient) + vec4( mix( texel.rgb, vcolor.xyz*0.7, dot( uv, uv ) ), texel.a );",110111"}"112113].join("\n")114115},116117/* -------------------------------------------------------------------------118// Normal map shader (perpixel)119// - Blinn-Phong120// - normal + diffuse + specular + AO + displacement + reflection + shadow maps121// - PER-PIXEL point and directional lights (use with "lights: true" material option)122// - modified by BKcore123------------------------------------------------------------------------- */124125'normal' : {126127uniforms: THREE.UniformsUtils.merge( [128129THREE.UniformsLib[ "fog" ],130THREE.UniformsLib[ "lights" ],131THREE.UniformsLib[ "shadowmap" ],132133{134135"enableAO" : { type: "i", value: 0 },136"enableDiffuse" : { type: "i", value: 0 },137"enableSpecular" : { type: "i", value: 0 },138"enableReflection": { type: "i", value: 0 },139140"tDiffuse" : { type: "t", value: 0, texture: null },141"tCube" : { type: "t", value: 1, texture: null },142"tNormal" : { type: "t", value: 2, texture: null },143"tSpecular" : { type: "t", value: 3, texture: null },144"tAO" : { type: "t", value: 4, texture: null },145"tDisplacement": { type: "t", value: 5, texture: null },146147"uNormalScale": { type: "f", value: 1.0 },148149"uDisplacementBias": { type: "f", value: 0.0 },150"uDisplacementScale": { type: "f", value: 1.0 },151152"uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },153"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },154"uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },155"uShininess": { type: "f", value: 30 },156"uOpacity": { type: "f", value: 1 },157158"uReflectivity": { type: "f", value: 0.5 },159160"uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },161"uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },162163"wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }164165}166167] ),168169fragmentShader: [170171"uniform vec3 uAmbientColor;",172"uniform vec3 uDiffuseColor;",173"uniform vec3 uSpecularColor;",174"uniform float uShininess;",175"uniform float uOpacity;",176177"uniform bool enableDiffuse;",178"uniform bool enableSpecular;",179"uniform bool enableAO;",180"uniform bool enableReflection;",181182"uniform sampler2D tDiffuse;",183"uniform sampler2D tNormal;",184"uniform sampler2D tSpecular;",185"uniform sampler2D tAO;",186187"uniform samplerCube tCube;",188189"uniform float uNormalScale;",190"uniform float uReflectivity;",191192"varying vec3 vTangent;",193"varying vec3 vBinormal;",194"varying vec3 vNormal;",195"varying vec2 vUv;",196197"uniform vec3 ambientLightColor;",198199"#if MAX_DIR_LIGHTS > 0",200"uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",201"uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",202"#endif",203204"#if MAX_POINT_LIGHTS > 0",205"uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",206"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",207"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",208"#endif",209210"#ifdef WRAP_AROUND",211"uniform vec3 wrapRGB;",212"#endif",213214"varying vec3 vViewPosition;",215216THREE.ShaderChunk[ "shadowmap_pars_fragment" ],217THREE.ShaderChunk[ "fog_pars_fragment" ],218219"void main() {",220221"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",222223"vec3 specularTex = vec3( 1.0 );",224225"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",226"normalTex.xy *= uNormalScale;",227"normalTex = normalize( normalTex );",228229"if( enableDiffuse ) {",230231"#ifdef GAMMA_INPUT",232233"vec4 texelColor = texture2D( tDiffuse, vUv );",234"texelColor.xyz *= texelColor.xyz;",235236"gl_FragColor = gl_FragColor * texelColor;",237238"#else",239240"gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",241242"#endif",243244"}",245246"if( enableAO ) {",247248"#ifdef GAMMA_INPUT",249250"vec4 aoColor = texture2D( tAO, vUv );",251"aoColor.xyz *= aoColor.xyz;",252253"gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",254255"#else",256257"gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",258259"#endif",260261"}",262263"if( enableSpecular )",264"specularTex = texture2D( tSpecular, vUv ).xyz;",265266"mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",267"vec3 finalNormal = tsb * normalTex;",268269"vec3 normal = normalize( finalNormal );",270"vec3 viewPosition = normalize( vViewPosition );",271272"#ifdef DOUBLE_SIDED",273274"normal = normal * ( -1.0 + 2.0 * float( gl_FrontFacing ) );",275276"#endif",277278// point lights279280"#if MAX_POINT_LIGHTS > 0",281282"vec3 pointDiffuse = vec3( 0.0 );",283"vec3 pointSpecular = vec3( 0.0 );",284285"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",286287"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",288"vec3 pointVector = lPosition.xyz + vViewPosition.xyz;",289290"float pointDistance = 1.0;",291"if ( pointLightDistance[ i ] > 0.0 )",292"pointDistance = 1.0 - min( ( length( pointVector ) / pointLightDistance[ i ] ), 1.0 );",293294"pointVector = normalize( pointVector );",295296// diffuse297298"#ifdef WRAP_AROUND",299300"float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",301"float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",302303"vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",304305"#else",306307"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",308309"#endif",310311"pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",312313// specular314315"vec3 pointHalfVector = normalize( pointVector + viewPosition );",316"float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",317"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",318319"#ifdef PHYSICALLY_BASED_SHADING",320321// 2.0 => 2.0001 is hack to work around ANGLE bug322323"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",324325"vec3 schlick = specularTex + vec3( 1.0 - specularTex ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",326"pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",327328"#else",329330"pointSpecular += pointDistance * pointLightColor[ i ] * specularTex * pointSpecularWeight * pointDiffuseWeight;",331332"#endif",333334"}",335336"#endif",337338// directional lights339340"#if MAX_DIR_LIGHTS > 0",341342"vec3 dirDiffuse = vec3( 0.0 );",343"vec3 dirSpecular = vec3( 0.0 );",344345"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",346347"vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",348"vec3 dirVector = normalize( lDirection.xyz );",349350// diffuse351352"#ifdef WRAP_AROUND",353354"float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",355"float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",356357"vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",358359"#else",360361"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",362363"#endif",364365"dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",366367// specular368369"vec3 dirHalfVector = normalize( dirVector + viewPosition );",370"float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",371"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",372373"#ifdef PHYSICALLY_BASED_SHADING",374375// 2.0 => 2.0001 is hack to work around ANGLE bug376377"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",378379"vec3 schlick = specularTex + vec3( 1.0 - specularTex ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",380"dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",381382"#else",383384"dirSpecular += directionalLightColor[ i ] * specularTex * dirSpecularWeight * dirDiffuseWeight;",385386"#endif",387388"}",389390"#endif",391392// all lights contribution summation393394"vec3 totalDiffuse = vec3( 0.0 );",395"vec3 totalSpecular = vec3( 0.0 );",396397"#if MAX_DIR_LIGHTS > 0",398399"totalDiffuse += dirDiffuse;",400"totalSpecular += dirSpecular;",401402"#endif",403404"#if MAX_POINT_LIGHTS > 0",405406"totalDiffuse += pointDiffuse;",407"totalSpecular += pointSpecular;",408409"#endif",410411"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",412413"if ( enableReflection ) {",414415"#ifdef DOUBLE_SIDED",416417"float flipNormal = ( -1.0 + 2.0 * float( gl_FrontFacing ) );",418419"#else",420421"float flipNormal = 1.0;",422423"#endif",424425"vec3 wPos = cameraPosition - vViewPosition;",426"vec3 vReflect = reflect( normalize( wPos ), normal );",427428"vec4 cubeColor = textureCube( tCube, flipNormal*vec3( -vReflect.x, vReflect.yz ) );",429430"#ifdef GAMMA_INPUT",431432"cubeColor.xyz *= cubeColor.xyz;",433434"#endif",435436"gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",437438"}",439440THREE.ShaderChunk[ "shadowmap_fragment" ],441THREE.ShaderChunk[ "linear_to_gamma_fragment" ],442THREE.ShaderChunk[ "fog_fragment" ],443444"}"445446].join("\n"),447448vertexShader: [449450"attribute vec4 tangent;",451452"uniform vec2 uOffset;",453"uniform vec2 uRepeat;",454455"#ifdef VERTEX_TEXTURES",456457"uniform sampler2D tDisplacement;",458"uniform float uDisplacementScale;",459"uniform float uDisplacementBias;",460461"#endif",462463"varying vec3 vTangent;",464"varying vec3 vBinormal;",465"varying vec3 vNormal;",466"varying vec2 vUv;",467468"varying vec3 vViewPosition;",469470THREE.ShaderChunk[ "shadowmap_pars_vertex" ],471472"void main() {",473474"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",475476"vViewPosition = -mvPosition.xyz;",477478// normal, tangent and binormal vectors479480"vNormal = normalMatrix * normal;",481"vTangent = normalMatrix * tangent.xyz;",482"vBinormal = cross( vNormal, vTangent ) * tangent.w;",483484"vUv = uv * uRepeat + uOffset;",485486// displacement mapping487488"#ifdef VERTEX_TEXTURES",489490"vec3 dv = texture2D( tDisplacement, uv ).xyz;",491"float df = uDisplacementScale * dv.x + uDisplacementBias;",492"vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",493"gl_Position = projectionMatrix * displacedPosition;",494495"#else",496497"gl_Position = projectionMatrix * mvPosition;",498499"#endif",500501THREE.ShaderChunk[ "shadowmap_vertex" ],502503"}"504505].join("\n")506507},508509/* -------------------------------------------------------------------------510// Normal map shader511// - Blinn-Phong512// - normal + diffuse + specular + AO + displacement + reflection + shadow maps513// - PER-VERTEX point and directional lights (use with "lights: true" material option)514------------------------------------------------------------------------- */515516'normalV' : {517518uniforms: THREE.UniformsUtils.merge( [519520THREE.UniformsLib[ "fog" ],521THREE.UniformsLib[ "lights" ],522THREE.UniformsLib[ "shadowmap" ],523524{525526"enableAO" : { type: "i", value: 0 },527"enableDiffuse" : { type: "i", value: 0 },528"enableSpecular" : { type: "i", value: 0 },529"enableReflection": { type: "i", value: 0 },530531"tDiffuse" : { type: "t", value: 0, texture: null },532"tCube" : { type: "t", value: 1, texture: null },533"tNormal" : { type: "t", value: 2, texture: null },534"tSpecular" : { type: "t", value: 3, texture: null },535"tAO" : { type: "t", value: 4, texture: null },536"tDisplacement": { type: "t", value: 5, texture: null },537538"uNormalScale": { type: "f", value: 1.0 },539540"uDisplacementBias": { type: "f", value: 0.0 },541"uDisplacementScale": { type: "f", value: 1.0 },542543"uDiffuseColor": { type: "c", value: new THREE.Color( 0xffffff ) },544"uSpecularColor": { type: "c", value: new THREE.Color( 0x111111 ) },545"uAmbientColor": { type: "c", value: new THREE.Color( 0xffffff ) },546"uShininess": { type: "f", value: 30 },547"uOpacity": { type: "f", value: 1 },548549"uReflectivity": { type: "f", value: 0.5 },550551"uOffset" : { type: "v2", value: new THREE.Vector2( 0, 0 ) },552"uRepeat" : { type: "v2", value: new THREE.Vector2( 1, 1 ) },553554"wrapRGB" : { type: "v3", value: new THREE.Vector3( 1, 1, 1 ) }555556}557558] ),559560fragmentShader: [561562"uniform vec3 uAmbientColor;",563"uniform vec3 uDiffuseColor;",564"uniform vec3 uSpecularColor;",565"uniform float uShininess;",566"uniform float uOpacity;",567568"uniform bool enableDiffuse;",569"uniform bool enableSpecular;",570"uniform bool enableAO;",571"uniform bool enableReflection;",572573"uniform sampler2D tDiffuse;",574"uniform sampler2D tNormal;",575"uniform sampler2D tSpecular;",576"uniform sampler2D tAO;",577578"uniform samplerCube tCube;",579580"uniform float uNormalScale;",581"uniform float uReflectivity;",582583"varying vec3 vTangent;",584"varying vec3 vBinormal;",585"varying vec3 vNormal;",586"varying vec2 vUv;",587588"uniform vec3 ambientLightColor;",589590"#if MAX_DIR_LIGHTS > 0",591"uniform vec3 directionalLightColor[ MAX_DIR_LIGHTS ];",592"uniform vec3 directionalLightDirection[ MAX_DIR_LIGHTS ];",593"#endif",594595"#if MAX_POINT_LIGHTS > 0",596"uniform vec3 pointLightColor[ MAX_POINT_LIGHTS ];",597"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",598"#endif",599600"#ifdef WRAP_AROUND",601"uniform vec3 wrapRGB;",602"#endif",603604"varying vec3 vViewPosition;",605606THREE.ShaderChunk[ "shadowmap_pars_fragment" ],607THREE.ShaderChunk[ "fog_pars_fragment" ],608609"void main() {",610611"gl_FragColor = vec4( vec3( 1.0 ), uOpacity );",612613"vec3 specularTex = vec3( 1.0 );",614615"vec3 normalTex = texture2D( tNormal, vUv ).xyz * 2.0 - 1.0;",616"normalTex.xy *= uNormalScale;",617"normalTex = normalize( normalTex );",618619"if( enableDiffuse ) {",620621"#ifdef GAMMA_INPUT",622623"vec4 texelColor = texture2D( tDiffuse, vUv );",624"texelColor.xyz *= texelColor.xyz;",625626"gl_FragColor = gl_FragColor * texelColor;",627628"#else",629630"gl_FragColor = gl_FragColor * texture2D( tDiffuse, vUv );",631632"#endif",633634"}",635636"if( enableAO ) {",637638"#ifdef GAMMA_INPUT",639640"vec4 aoColor = texture2D( tAO, vUv );",641"aoColor.xyz *= aoColor.xyz;",642643"gl_FragColor.xyz = gl_FragColor.xyz * aoColor.xyz;",644645"#else",646647"gl_FragColor.xyz = gl_FragColor.xyz * texture2D( tAO, vUv ).xyz;",648649"#endif",650651"}",652653"if( enableSpecular )",654"specularTex = texture2D( tSpecular, vUv ).xyz;",655656"mat3 tsb = mat3( normalize( vTangent ), normalize( vBinormal ), normalize( vNormal ) );",657"vec3 finalNormal = tsb * normalTex;",658659"vec3 normal = normalize( finalNormal );",660"vec3 viewPosition = normalize( vViewPosition );",661662// point lights663664"#if MAX_POINT_LIGHTS > 0",665666"vec3 pointDiffuse = vec3( 0.0 );",667"vec3 pointSpecular = vec3( 0.0 );",668669"for ( int i = 0; i < MAX_POINT_LIGHTS; i ++ ) {",670671"vec3 pointVector = normalize( vPointLight[ i ].xyz );",672"float pointDistance = vPointLight[ i ].w;",673674// diffuse675676"#ifdef WRAP_AROUND",677678"float pointDiffuseWeightFull = max( dot( normal, pointVector ), 0.0 );",679"float pointDiffuseWeightHalf = max( 0.5 * dot( normal, pointVector ) + 0.5, 0.0 );",680681"vec3 pointDiffuseWeight = mix( vec3 ( pointDiffuseWeightFull ), vec3( pointDiffuseWeightHalf ), wrapRGB );",682683"#else",684685"float pointDiffuseWeight = max( dot( normal, pointVector ), 0.0 );",686687"#endif",688689"pointDiffuse += pointDistance * pointLightColor[ i ] * uDiffuseColor * pointDiffuseWeight;",690691// specular692693"vec3 pointHalfVector = normalize( pointVector + viewPosition );",694"float pointDotNormalHalf = max( dot( normal, pointHalfVector ), 0.0 );",695"float pointSpecularWeight = specularTex.r * max( pow( pointDotNormalHalf, uShininess ), 0.0 );",696697"#ifdef PHYSICALLY_BASED_SHADING",698699// 2.0 => 2.0001 is hack to work around ANGLE bug700701"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",702703"vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( pointVector, pointHalfVector ), 5.0 );",704"pointSpecular += schlick * pointLightColor[ i ] * pointSpecularWeight * pointDiffuseWeight * pointDistance * specularNormalization;",705706"#else",707708"pointSpecular += pointDistance * pointLightColor[ i ] * uSpecularColor * pointSpecularWeight * pointDiffuseWeight;",709710"#endif",711712"}",713714"#endif",715716// directional lights717718"#if MAX_DIR_LIGHTS > 0",719720"vec3 dirDiffuse = vec3( 0.0 );",721"vec3 dirSpecular = vec3( 0.0 );",722723"for( int i = 0; i < MAX_DIR_LIGHTS; i++ ) {",724725"vec4 lDirection = viewMatrix * vec4( directionalLightDirection[ i ], 0.0 );",726"vec3 dirVector = normalize( lDirection.xyz );",727728// diffuse729730"#ifdef WRAP_AROUND",731732"float directionalLightWeightingFull = max( dot( normal, dirVector ), 0.0 );",733"float directionalLightWeightingHalf = max( 0.5 * dot( normal, dirVector ) + 0.5, 0.0 );",734735"vec3 dirDiffuseWeight = mix( vec3( directionalLightWeightingFull ), vec3( directionalLightWeightingHalf ), wrapRGB );",736737"#else",738739"float dirDiffuseWeight = max( dot( normal, dirVector ), 0.0 );",740741"#endif",742743"dirDiffuse += directionalLightColor[ i ] * uDiffuseColor * dirDiffuseWeight;",744745// specular746747"vec3 dirHalfVector = normalize( dirVector + viewPosition );",748"float dirDotNormalHalf = max( dot( normal, dirHalfVector ), 0.0 );",749"float dirSpecularWeight = specularTex.r * max( pow( dirDotNormalHalf, uShininess ), 0.0 );",750751"#ifdef PHYSICALLY_BASED_SHADING",752753// 2.0 => 2.0001 is hack to work around ANGLE bug754755"float specularNormalization = ( uShininess + 2.0001 ) / 8.0;",756757"vec3 schlick = uSpecularColor + vec3( 1.0 - uSpecularColor ) * pow( 1.0 - dot( dirVector, dirHalfVector ), 5.0 );",758"dirSpecular += schlick * directionalLightColor[ i ] * dirSpecularWeight * dirDiffuseWeight * specularNormalization;",759760"#else",761762"dirSpecular += directionalLightColor[ i ] * uSpecularColor * dirSpecularWeight * dirDiffuseWeight;",763764"#endif",765766"}",767768"#endif",769770// all lights contribution summation771772"vec3 totalDiffuse = vec3( 0.0 );",773"vec3 totalSpecular = vec3( 0.0 );",774775"#if MAX_DIR_LIGHTS > 0",776777"totalDiffuse += dirDiffuse;",778"totalSpecular += dirSpecular;",779780"#endif",781782"#if MAX_POINT_LIGHTS > 0",783784"totalDiffuse += pointDiffuse;",785"totalSpecular += pointSpecular;",786787"#endif",788789"gl_FragColor.xyz = gl_FragColor.xyz * ( totalDiffuse + ambientLightColor * uAmbientColor) + totalSpecular;",790791"if ( enableReflection ) {",792793"vec3 wPos = cameraPosition - vViewPosition;",794"vec3 vReflect = reflect( normalize( wPos ), normal );",795796"vec4 cubeColor = textureCube( tCube, vec3( -vReflect.x, vReflect.yz ) );",797798"#ifdef GAMMA_INPUT",799800"cubeColor.xyz *= cubeColor.xyz;",801802"#endif",803804"gl_FragColor.xyz = mix( gl_FragColor.xyz, cubeColor.xyz, specularTex.r * uReflectivity );",805806"}",807808THREE.ShaderChunk[ "shadowmap_fragment" ],809THREE.ShaderChunk[ "linear_to_gamma_fragment" ],810THREE.ShaderChunk[ "fog_fragment" ],811812"}"813814].join("\n"),815816vertexShader: [817818"attribute vec4 tangent;",819820"uniform vec2 uOffset;",821"uniform vec2 uRepeat;",822823"#ifdef VERTEX_TEXTURES",824825"uniform sampler2D tDisplacement;",826"uniform float uDisplacementScale;",827"uniform float uDisplacementBias;",828829"#endif",830831"varying vec3 vTangent;",832"varying vec3 vBinormal;",833"varying vec3 vNormal;",834"varying vec2 vUv;",835836"#if MAX_POINT_LIGHTS > 0",837838"uniform vec3 pointLightPosition[ MAX_POINT_LIGHTS ];",839"uniform float pointLightDistance[ MAX_POINT_LIGHTS ];",840841"varying vec4 vPointLight[ MAX_POINT_LIGHTS ];",842843"#endif",844845"varying vec3 vViewPosition;",846847THREE.ShaderChunk[ "shadowmap_pars_vertex" ],848849"void main() {",850851"vec4 mvPosition = modelViewMatrix * vec4( position, 1.0 );",852853"vViewPosition = -mvPosition.xyz;",854855// normal, tangent and binormal vectors856857"vNormal = normalMatrix * normal;",858"vTangent = normalMatrix * tangent.xyz;",859"vBinormal = cross( vNormal, vTangent ) * tangent.w;",860861"vUv = uv * uRepeat + uOffset;",862863// point lights864865"#if MAX_POINT_LIGHTS > 0",866867"for( int i = 0; i < MAX_POINT_LIGHTS; i++ ) {",868869"vec4 lPosition = viewMatrix * vec4( pointLightPosition[ i ], 1.0 );",870"vec3 lVector = lPosition.xyz - mvPosition.xyz;",871872"float lDistance = 1.0;",873"if ( pointLightDistance[ i ] > 0.0 )",874"lDistance = 1.0 - min( ( length( lVector ) / pointLightDistance[ i ] ), 1.0 );",875876"lVector = normalize( lVector );",877878"vPointLight[ i ] = vec4( lVector, lDistance );",879880"}",881882"#endif",883884// displacement mapping885886"#ifdef VERTEX_TEXTURES",887888"vec3 dv = texture2D( tDisplacement, uv ).xyz;",889"float df = uDisplacementScale * dv.x + uDisplacementBias;",890"vec4 displacedPosition = vec4( normalize( vNormal.xyz ) * df, 0.0 ) + mvPosition;",891"gl_Position = projectionMatrix * displacedPosition;",892893"#else",894895"gl_Position = projectionMatrix * mvPosition;",896897"#endif",898899THREE.ShaderChunk[ "shadowmap_vertex" ],900901"}"902903].join("\n")904905},906907/* -------------------------------------------------------------------------908// Cube map shader909------------------------------------------------------------------------- */910911'cube': {912913uniforms: { "tCube": { type: "t", value: 1, texture: null },914"tFlip": { type: "f", value: -1 } },915916vertexShader: [917918"varying vec3 vViewPosition;",919920"void main() {",921922"vec4 mPosition = objectMatrix * vec4( position, 1.0 );",923"vViewPosition = cameraPosition - mPosition.xyz;",924925"gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );",926927"}"928929].join("\n"),930931fragmentShader: [932933"uniform samplerCube tCube;",934"uniform float tFlip;",935936"varying vec3 vViewPosition;",937938"void main() {",939940"vec3 wPos = cameraPosition - vViewPosition;",941"gl_FragColor = textureCube( tCube, vec3( tFlip * wPos.x, wPos.yz ) );",942943"}"944945].join("\n")946947}948949};950951