Guest User

Untitled

a guest
Feb 18th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // MAT.__webglShader.vertexShader
  2.  
  3. #define LAMBERT
  4. varying vec3 vLightFront;
  5. #ifdef DOUBLE_SIDED
  6.     varying vec3 vLightBack;
  7. #endif
  8. #define PI 3.14159
  9. #define PI2 6.28318
  10. #define RECIPROCAL_PI 0.31830988618
  11. #define RECIPROCAL_PI2 0.15915494
  12. #define LOG2 1.442695
  13. #define EPSILON 1e-6
  14. #define saturate(a) clamp( a, 0.0, 1.0 )
  15. #define whiteCompliment(a) ( 1.0 - saturate( a ) )
  16. float square( const in float x ) { return x*x; }
  17. float average( const in vec3 color ) { return dot( color, vec3( 0.3333 ) ); }
  18. struct IncidentLight {
  19.     vec3 color;
  20.     vec3 direction;
  21.     bool visible;
  22. };
  23. struct ReflectedLight {
  24.     vec3 directDiffuse;
  25.     vec3 directSpecular;
  26.     vec3 indirectDiffuse;
  27.     vec3 indirectSpecular;
  28. };
  29. struct GeometricContext {
  30.     vec3 position;
  31.     vec3 normal;
  32.     vec3 viewDir;
  33. };
  34. vec3 transformDirection( in vec3 dir, in mat4 matrix ) {
  35.     return normalize( ( matrix * vec4( dir, 0.0 ) ).xyz );
  36. }
  37. vec3 inverseTransformDirection( in vec3 dir, in mat4 matrix ) {
  38.     return normalize( ( vec4( dir, 0.0 ) * matrix ).xyz );
  39. }
  40. vec3 projectOnPlane(in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
  41.     float distance = dot( planeNormal, point - pointOnPlane );
  42.     return - distance * planeNormal + point;
  43. }
  44. float sideOfPlane( in vec3 point, in vec3 pointOnPlane, in vec3 planeNormal ) {
  45.     return sign( dot( point - pointOnPlane, planeNormal ) );
  46. }
  47. vec3 linePlaneIntersect( in vec3 pointOnLine, in vec3 lineDirection, in vec3 pointOnPlane, in vec3 planeNormal ) {
  48.     return lineDirection * ( dot( planeNormal, pointOnPlane - pointOnLine ) / dot( planeNormal, lineDirection ) ) + pointOnLine;
  49. }
  50. vec3 inputToLinear( in vec3 a ) {
  51.     #ifdef GAMMA_INPUT
  52.         return pow( a, vec3( float( GAMMA_FACTOR ) ) );
  53.     #else
  54.         return a;
  55.     #endif
  56. }
  57. vec3 linearToOutput( in vec3 a ) {
  58.     #ifdef GAMMA_OUTPUT
  59.         return pow( a, vec3( 1.0 / float( GAMMA_FACTOR ) ) );
  60.     #else
  61.         return a;
  62.     #endif
  63. }
  64.  
  65. #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_METALNESSMAP )
  66.     varying vec2 vUv;
  67.     uniform vec4 offsetRepeat;
  68. #endif
  69.  
  70. #if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
  71.     attribute vec2 uv2;
  72.     varying vec2 vUv2;
  73. #endif
  74. #if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) && ! defined( STANDARD )
  75.     varying vec3 vReflect;
  76.     uniform float refractionRatio;
  77. #endif
  78.  
  79. bool testLightInRange( const in float lightDistance, const in float cutoffDistance ) {
  80.     return any( bvec2( cutoffDistance == 0.0, lightDistance < cutoffDistance ) );
  81. }
  82. float calcLightAttenuation( const in float lightDistance, const in float cutoffDistance, const in float decayExponent ) {
  83.     if ( decayExponent > 0.0 ) {
  84.       return pow( saturate( -lightDistance / cutoffDistance + 1.0 ), decayExponent );
  85.     }
  86.     return 1.0;
  87. }
  88. vec3 BRDF_Diffuse_Lambert( const in vec3 diffuseColor ) {
  89.     return RECIPROCAL_PI * diffuseColor;
  90. }
  91. vec3 F_Schlick( const in vec3 specularColor, const in float dotLH ) {
  92.     float fresnel = exp2( ( -5.55473 * dotLH - 6.98316 ) * dotLH );
  93.     return ( 1.0 - specularColor ) * fresnel + specularColor;
  94. }
  95. float G_GGX_Smith( const in float alpha, const in float dotNL, const in float dotNV ) {
  96.     float a2 = alpha * alpha;
  97.     float gl = dotNL + pow( a2 + ( 1.0 - a2 ) * dotNL * dotNL, 0.5 );
  98.     float gv = dotNV + pow( a2 + ( 1.0 - a2 ) * dotNV * dotNV, 0.5 );
  99.     return 1.0 / ( gl * gv );
  100. }
  101. float D_GGX( const in float alpha, const in float dotNH ) {
  102.     float a2 = alpha * alpha;
  103.     float denom = dotNH * dotNH * ( a2 - 1.0 ) + 1.0;
  104.     return RECIPROCAL_PI * a2 / ( denom * denom );
  105. }
  106. vec3 BRDF_Specular_GGX( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
  107.     float alpha = roughness * roughness;
  108.     vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
  109.     float dotNL = saturate( dot( geometry.normal, incidentLight.direction ) );
  110.     float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
  111.     float dotNH = saturate( dot( geometry.normal, halfDir ) );
  112.     float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
  113.     vec3 F = F_Schlick( specularColor, dotLH );
  114.     float G = G_GGX_Smith( alpha, dotNL, dotNV );
  115.     float D = D_GGX( alpha, dotNH );
  116.     return F * ( G * D );
  117. }
  118. vec3 BRDF_Specular_GGX_Environment( const in GeometricContext geometry, const in vec3 specularColor, const in float roughness ) {
  119.     float dotNV = saturate( dot( geometry.normal, geometry.viewDir ) );
  120.     const vec4 c0 = vec4( - 1, - 0.0275, - 0.572, 0.022 );
  121.     const vec4 c1 = vec4( 1, 0.0425, 1.04, - 0.04 );
  122.     vec4 r = roughness * c0 + c1;
  123.     float a004 = min( r.x * r.x, exp2( - 9.28 * dotNV ) ) * r.x + r.y;
  124.     vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;
  125.     return specularColor * AB.x + AB.y;
  126. }
  127. float G_BlinnPhong_Implicit( ) {
  128.     return 0.25;
  129. }
  130. float D_BlinnPhong( const in float shininess, const in float dotNH ) {
  131.     return RECIPROCAL_PI * ( shininess * 0.5 + 1.0 ) * pow( dotNH, shininess );
  132. }
  133. vec3 BRDF_Specular_BlinnPhong( const in IncidentLight incidentLight, const in GeometricContext geometry, const in vec3 specularColor, const in float shininess ) {
  134.     vec3 halfDir = normalize( incidentLight.direction + geometry.viewDir );
  135.     float dotNH = saturate( dot( geometry.normal, halfDir ) );
  136.     float dotLH = saturate( dot( incidentLight.direction, halfDir ) );
  137.     vec3 F = F_Schlick( specularColor, dotLH );
  138.     float G = G_BlinnPhong_Implicit( );
  139.     float D = D_BlinnPhong( shininess, dotNH );
  140.     return F * ( G * D );
  141. }
  142. float GGXRoughnessToBlinnExponent( const in float ggxRoughness ) {
  143.     return ( 2.0 / square( ggxRoughness + 0.0001 ) - 2.0 );
  144. }
  145.  
  146. #if NUM_DIR_LIGHTS > 0
  147.     struct DirectionalLight {
  148.         vec3 direction;
  149.         vec3 color;
  150.         int shadow;
  151.         float shadowBias;
  152.         float shadowRadius;
  153.         vec2 shadowMapSize;
  154.     };
  155.     uniform DirectionalLight directionalLights[ NUM_DIR_LIGHTS ];
  156.     IncidentLight getDirectionalDirectLight( const in DirectionalLight directionalLight, const in GeometricContext geometry ) {
  157.         IncidentLight directLight;
  158.         directLight.color = directionalLight.color;
  159.         directLight.direction = directionalLight.direction;
  160.         directLight.visible = true;
  161.         return directLight;
  162.     }
  163. #endif
  164. #if NUM_POINT_LIGHTS > 0
  165.     struct PointLight {
  166.         vec3 position;
  167.         vec3 color;
  168.         float distance;
  169.         float decay;
  170.         int shadow;
  171.         float shadowBias;
  172.         float shadowRadius;
  173.         vec2 shadowMapSize;
  174.     };
  175.     uniform PointLight pointLights[ NUM_POINT_LIGHTS ];
  176.     IncidentLight getPointDirectLight( const in PointLight pointLight, const in GeometricContext geometry ) {
  177.         IncidentLight directLight;
  178.         vec3 lVector = pointLight.position - geometry.position;
  179.         directLight.direction = normalize( lVector );
  180.         float lightDistance = length( lVector );
  181.         if ( testLightInRange( lightDistance, pointLight.distance ) ) {
  182.             directLight.color = pointLight.color;
  183.             directLight.color *= calcLightAttenuation( lightDistance, pointLight.distance, pointLight.decay );
  184.             directLight.visible = true;
  185.         } else {
  186.             directLight.color = vec3( 0.0 );
  187.             directLight.visible = false;
  188.         }
  189.         return directLight;
  190.     }
  191. #endif
  192. #if NUM_SPOT_LIGHTS > 0
  193.     struct SpotLight {
  194.         vec3 position;
  195.         vec3 direction;
  196.         vec3 color;
  197.         float distance;
  198.         float decay;
  199.         float angleCos;
  200.         float penumbra;
  201.         int shadow;
  202.         float shadowBias;
  203.         float shadowRadius;
  204.         vec2 shadowMapSize;
  205.     };
  206.     uniform SpotLight spotLights[ NUM_SPOT_LIGHTS ];
  207.     IncidentLight getSpotDirectLight( const in SpotLight spotLight, const in GeometricContext geometry ) {
  208.         IncidentLight directLight;
  209.         vec3 lVector = spotLight.position - geometry.position;
  210.         directLight.direction = normalize( lVector );
  211.         float lightDistance = length( lVector );
  212.         float spotEffect = dot( directLight.direction, spotLight.direction );
  213.         if ( all( bvec2( spotEffect > spotLight.angleCos, testLightInRange( lightDistance, spotLight.distance ) ) ) ) {
  214.             float spotEffect = dot( spotLight.direction, directLight.direction );
  215.             spotEffect *= clamp( ( spotEffect - spotLight.angleCos ) / spotLight.penumbra, 0.0, 1.0 );
  216.             directLight.color = spotLight.color;
  217.             directLight.color *= ( spotEffect * calcLightAttenuation( lightDistance, spotLight.distance, spotLight.decay ) );
  218.             directLight.visible = true;
  219.         } else {
  220.             directLight.color = vec3( 0.0 );
  221.             directLight.visible = false;
  222.         }
  223.         return directLight;
  224.     }
  225. #endif
  226. #if NUM_HEMI_LIGHTS > 0
  227.     struct HemisphereLight {
  228.         vec3 direction;
  229.         vec3 skyColor;
  230.         vec3 groundColor;
  231.     };
  232.     uniform HemisphereLight hemisphereLights[ NUM_HEMI_LIGHTS ];
  233.     vec3 getHemisphereLightIrradiance( const in HemisphereLight hemiLight, const in GeometricContext geometry ) {
  234.         float dotNL = dot( geometry.normal, hemiLight.direction );
  235.         float hemiDiffuseWeight = 0.5 * dotNL + 0.5;
  236.         return PI * mix( hemiLight.groundColor, hemiLight.skyColor, hemiDiffuseWeight );
  237.     }
  238. #endif
  239. #if defined( USE_ENVMAP ) && defined( STANDARD )
  240.     vec3 getLightProbeIndirectIrradiance( const in GeometricContext geometry, const in int maxMIPLevel ) {
  241.         #ifdef DOUBLE_SIDED
  242.             float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  243.         #else
  244.             float flipNormal = 1.0;
  245.         #endif
  246.         vec3 worldNormal = inverseTransformDirection( geometry.normal, viewMatrix );
  247.         #ifdef ENVMAP_TYPE_CUBE
  248.             vec3 queryVec = flipNormal * vec3( flipEnvMap * worldNormal.x, worldNormal.yz );
  249.             #ifdef TEXTURE_LOD_EXT
  250.                 vec4 envMapColor = textureCubeLodEXT( envMap, queryVec, float( maxMIPLevel ) );
  251.             #else
  252.                 vec4 envMapColor = textureCube( envMap, queryVec, float( maxMIPLevel ) );
  253.             #endif
  254.         #else
  255.             vec3 envMapColor = vec3( 0.0 );
  256.         #endif
  257.         envMapColor.rgb = inputToLinear( envMapColor.rgb );
  258.         return PI * envMapColor.rgb * envMapIntensity;
  259.     }
  260.     float getSpecularMIPLevel( const in float blinnShininessExponent, const in int maxMIPLevel ) {
  261.         float maxMIPLevelScalar = float( maxMIPLevel );
  262.         float desiredMIPLevel = maxMIPLevelScalar - 0.79248 - 0.5 * log2( square( blinnShininessExponent ) + 1.0 );
  263.         return clamp( desiredMIPLevel, 0.0, maxMIPLevelScalar );
  264.     }
  265.     vec3 getLightProbeIndirectRadiance( const in GeometricContext geometry, const in float blinnShininessExponent, const in int maxMIPLevel ) {
  266.         #ifdef ENVMAP_MODE_REFLECTION
  267.             vec3 reflectVec = reflect( -geometry.viewDir, geometry.normal );
  268.         #else
  269.             vec3 reflectVec = refract( -geometry.viewDir, geometry.normal, refractionRatio );
  270.         #endif
  271.         #ifdef DOUBLE_SIDED
  272.             float flipNormal = ( float( gl_FrontFacing ) * 2.0 - 1.0 );
  273.         #else
  274.             float flipNormal = 1.0;
  275.         #endif
  276.         reflectVec = inverseTransformDirection( reflectVec, viewMatrix );
  277.         float specularMIPLevel = getSpecularMIPLevel( blinnShininessExponent, maxMIPLevel );
  278.         #ifdef ENVMAP_TYPE_CUBE
  279.             vec3 queryReflectVec = flipNormal * vec3( flipEnvMap * reflectVec.x, reflectVec.yz );
  280.             #ifdef TEXTURE_LOD_EXT
  281.                 vec4 envMapColor = textureCubeLodEXT( envMap, queryReflectVec, specularMIPLevel );
  282.             #else
  283.                 vec4 envMapColor = textureCube( envMap, queryReflectVec, specularMIPLevel );
  284.             #endif
  285.         #elif defined( ENVMAP_TYPE_EQUIREC )
  286.             vec2 sampleUV;
  287.             sampleUV.y = saturate( flipNormal * reflectVec.y * 0.5 + 0.5 );
  288.             sampleUV.x = atan( flipNormal * reflectVec.z, flipNormal * reflectVec.x ) * RECIPROCAL_PI2 + 0.5;
  289.             #ifdef TEXTURE_LOD_EXT
  290.                 vec4 envMapColor = texture2DLodEXT( envMap, sampleUV, specularMIPLevel );
  291.             #else
  292.                 vec4 envMapColor = texture2D( envMap, sampleUV, specularMIPLevel );
  293.             #endif
  294.         #elif defined( ENVMAP_TYPE_SPHERE )
  295.             vec3 reflectView = flipNormal * normalize((viewMatrix * vec4( reflectVec, 0.0 )).xyz + vec3(0.0,0.0,1.0));
  296.             #ifdef TEXTURE_LOD_EXT
  297.                 vec4 envMapColor = texture2DLodEXT( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  298.             #else
  299.                 vec4 envMapColor = texture2D( envMap, reflectView.xy * 0.5 + 0.5, specularMIPLevel );
  300.             #endif
  301.         #endif
  302.         envMapColor.rgb = inputToLinear( envMapColor.rgb );
  303.         return envMapColor.rgb * envMapIntensity;
  304.     }
  305. #endif
  306.  
  307. #ifdef USE_COLOR
  308.     varying vec3 vColor;
  309. #endif
  310. #ifdef USE_MORPHTARGETS
  311.     #ifndef USE_MORPHNORMALS
  312.     uniform float morphTargetInfluences[ 8 ];
  313.     #else
  314.     uniform float morphTargetInfluences[ 4 ];
  315.     #endif
  316. #endif
  317. #ifdef USE_SKINNING
  318.     uniform mat4 bindMatrix;
  319.     uniform mat4 bindMatrixInverse;
  320.     #ifdef BONE_TEXTURE
  321.         uniform sampler2D boneTexture;
  322.         uniform int boneTextureWidth;
  323.         uniform int boneTextureHeight;
  324.         mat4 getBoneMatrix( const in float i ) {
  325.             float j = i * 4.0;
  326.             float x = mod( j, float( boneTextureWidth ) );
  327.             float y = floor( j / float( boneTextureWidth ) );
  328.             float dx = 1.0 / float( boneTextureWidth );
  329.             float dy = 1.0 / float( boneTextureHeight );
  330.             y = dy * ( y + 0.5 );
  331.             vec4 v1 = texture2D( boneTexture, vec2( dx * ( x + 0.5 ), y ) );
  332.             vec4 v2 = texture2D( boneTexture, vec2( dx * ( x + 1.5 ), y ) );
  333.             vec4 v3 = texture2D( boneTexture, vec2( dx * ( x + 2.5 ), y ) );
  334.             vec4 v4 = texture2D( boneTexture, vec2( dx * ( x + 3.5 ), y ) );
  335.             mat4 bone = mat4( v1, v2, v3, v4 );
  336.             return bone;
  337.         }
  338.     #else
  339.         uniform mat4 boneGlobalMatrices[ MAX_BONES ];
  340.         mat4 getBoneMatrix( const in float i ) {
  341.             mat4 bone = boneGlobalMatrices[ int(i) ];
  342.             return bone;
  343.         }
  344.     #endif
  345. #endif
  346.  
  347. #ifdef USE_SHADOWMAP
  348.     #if NUM_DIR_LIGHTS > 0
  349.         uniform mat4 directionalShadowMatrix[ NUM_DIR_LIGHTS ];
  350.         varying vec4 vDirectionalShadowCoord[ NUM_DIR_LIGHTS ];
  351.     #endif
  352.     #if NUM_SPOT_LIGHTS > 0
  353.         uniform mat4 spotShadowMatrix[ NUM_SPOT_LIGHTS ];
  354.         varying vec4 vSpotShadowCoord[ NUM_SPOT_LIGHTS ];
  355.     #endif
  356.     #if NUM_POINT_LIGHTS > 0
  357.         uniform mat4 pointShadowMatrix[ NUM_POINT_LIGHTS ];
  358.         varying vec4 vPointShadowCoord[ NUM_POINT_LIGHTS ];
  359.     #endif
  360. #endif
  361.  
  362. #ifdef USE_LOGDEPTHBUF
  363.     #ifdef USE_LOGDEPTHBUF_EXT
  364.         varying float vFragDepth;
  365.     #endif
  366.     uniform float logDepthBufFC;
  367. #endif
  368. void main() {
  369. #if defined( USE_MAP ) || defined( USE_BUMPMAP ) || defined( USE_NORMALMAP ) || defined( USE_SPECULARMAP ) || defined( USE_ALPHAMAP ) || defined( USE_EMISSIVEMAP ) || defined( USE_ROUGHNESSMAP ) || defined( USE_METALNESSMAP )
  370.     vUv = uv * offsetRepeat.zw + offsetRepeat.xy;
  371. #endif
  372. #if defined( USE_LIGHTMAP ) || defined( USE_AOMAP )
  373.     vUv2 = uv2;
  374. #endif
  375. #ifdef USE_COLOR
  376.     vColor.xyz = color.xyz;
  377. #endif
  378.  
  379. vec3 objectNormal = vec3( normal );
  380.  
  381. #ifdef USE_MORPHNORMALS
  382.     objectNormal += ( morphNormal0 - normal ) * morphTargetInfluences[ 0 ];
  383.     objectNormal += ( morphNormal1 - normal ) * morphTargetInfluences[ 1 ];
  384.     objectNormal += ( morphNormal2 - normal ) * morphTargetInfluences[ 2 ];
  385.     objectNormal += ( morphNormal3 - normal ) * morphTargetInfluences[ 3 ];
  386. #endif
  387.  
  388. #ifdef USE_SKINNING
  389.     mat4 boneMatX = getBoneMatrix( skinIndex.x );
  390.     mat4 boneMatY = getBoneMatrix( skinIndex.y );
  391.     mat4 boneMatZ = getBoneMatrix( skinIndex.z );
  392.     mat4 boneMatW = getBoneMatrix( skinIndex.w );
  393. #endif
  394. #ifdef USE_SKINNING
  395.     mat4 skinMatrix = mat4( 0.0 );
  396.     skinMatrix += skinWeight.x * boneMatX;
  397.     skinMatrix += skinWeight.y * boneMatY;
  398.     skinMatrix += skinWeight.z * boneMatZ;
  399.     skinMatrix += skinWeight.w * boneMatW;
  400.     skinMatrix  = bindMatrixInverse * skinMatrix * bindMatrix;
  401.     objectNormal = vec4( skinMatrix * vec4( objectNormal, 0.0 ) ).xyz;
  402. #endif
  403.  
  404. #ifdef FLIP_SIDED
  405.     objectNormal = -objectNormal;
  406. #endif
  407. vec3 transformedNormal = normalMatrix * objectNormal;
  408.  
  409.  
  410. vec3 transformed = vec3( position );
  411.  
  412. #ifdef USE_MORPHTARGETS
  413.     transformed += ( morphTarget0 - position ) * morphTargetInfluences[ 0 ];
  414.     transformed += ( morphTarget1 - position ) * morphTargetInfluences[ 1 ];
  415.     transformed += ( morphTarget2 - position ) * morphTargetInfluences[ 2 ];
  416.     transformed += ( morphTarget3 - position ) * morphTargetInfluences[ 3 ];
  417.     #ifndef USE_MORPHNORMALS
  418.     transformed += ( morphTarget4 - position ) * morphTargetInfluences[ 4 ];
  419.     transformed += ( morphTarget5 - position ) * morphTargetInfluences[ 5 ];
  420.     transformed += ( morphTarget6 - position ) * morphTargetInfluences[ 6 ];
  421.     transformed += ( morphTarget7 - position ) * morphTargetInfluences[ 7 ];
  422.     #endif
  423. #endif
  424.  
  425. #ifdef USE_SKINNING
  426.     vec4 skinVertex = bindMatrix * vec4( transformed, 1.0 );
  427.     vec4 skinned = vec4( 0.0 );
  428.     skinned += boneMatX * skinVertex * skinWeight.x;
  429.     skinned += boneMatY * skinVertex * skinWeight.y;
  430.     skinned += boneMatZ * skinVertex * skinWeight.z;
  431.     skinned += boneMatW * skinVertex * skinWeight.w;
  432.     skinned  = bindMatrixInverse * skinned;
  433. #endif
  434.  
  435. #ifdef USE_SKINNING
  436.     vec4 mvPosition = modelViewMatrix * skinned;
  437. #else
  438.     vec4 mvPosition = modelViewMatrix * vec4( transformed, 1.0 );
  439. #endif
  440. gl_Position = projectionMatrix * mvPosition;
  441.  
  442. #ifdef USE_LOGDEPTHBUF
  443.     gl_Position.z = log2(max( EPSILON, gl_Position.w + 1.0 )) * logDepthBufFC;
  444.     #ifdef USE_LOGDEPTHBUF_EXT
  445.         vFragDepth = 1.0 + gl_Position.w;
  446.     #else
  447.         gl_Position.z = (gl_Position.z - 1.0) * gl_Position.w;
  448.     #endif
  449. #endif
  450.  
  451. #if defined( USE_ENVMAP ) || defined( PHONG ) || defined( STANDARD ) || defined( LAMBERT ) || defined ( USE_SHADOWMAP )
  452.     #ifdef USE_SKINNING
  453.         vec4 worldPosition = modelMatrix * skinned;
  454.     #else
  455.         vec4 worldPosition = modelMatrix * vec4( transformed, 1.0 );
  456.     #endif
  457. #endif
  458.  
  459. #if defined( USE_ENVMAP ) && ! defined( USE_BUMPMAP ) && ! defined( USE_NORMALMAP ) && ! defined( PHONG ) && ! defined( STANDARD )
  460.     vec3 cameraToVertex = normalize( worldPosition.xyz - cameraPosition );
  461.     vec3 worldNormal = inverseTransformDirection( transformedNormal, viewMatrix );
  462.     #ifdef ENVMAP_MODE_REFLECTION
  463.         vReflect = reflect( cameraToVertex, worldNormal );
  464.     #else
  465.         vReflect = refract( cameraToVertex, worldNormal, refractionRatio );
  466.     #endif
  467. #endif
  468.  
  469. vec3 diffuse = vec3( 1.0 );
  470. GeometricContext geometry;
  471. geometry.position = mvPosition.xyz;
  472. geometry.normal = normalize( transformedNormal );
  473. geometry.viewDir = normalize( -mvPosition.xyz );
  474. GeometricContext backGeometry;
  475. backGeometry.position = geometry.position;
  476. backGeometry.normal = -geometry.normal;
  477. backGeometry.viewDir = geometry.viewDir;
  478. vLightFront = vec3( 0.0 );
  479. #ifdef DOUBLE_SIDED
  480.     vLightBack = vec3( 0.0 );
  481. #endif
  482. IncidentLight directLight;
  483. float dotNL;
  484. vec3 directLightColor_Diffuse;
  485. #if NUM_POINT_LIGHTS > 0
  486.     for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  487.         directLight = getPointDirectLight( pointLights[ i ], geometry );
  488.         dotNL = dot( geometry.normal, directLight.direction );
  489.         directLightColor_Diffuse = PI * directLight.color;
  490.         vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
  491.         #ifdef DOUBLE_SIDED
  492.             vLightBack += saturate( -dotNL ) * directLightColor_Diffuse;
  493.         #endif
  494.     }
  495. #endif
  496. #if NUM_SPOT_LIGHTS > 0
  497.     for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  498.         directLight = getSpotDirectLight( spotLights[ i ], geometry );
  499.         dotNL = dot( geometry.normal, directLight.direction );
  500.         directLightColor_Diffuse = PI * directLight.color;
  501.         vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
  502.         #ifdef DOUBLE_SIDED
  503.             vLightBack += saturate( -dotNL ) * directLightColor_Diffuse;
  504.         #endif
  505.     }
  506. #endif
  507. #if NUM_DIR_LIGHTS > 0
  508.     for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  509.         directLight = getDirectionalDirectLight( directionalLights[ i ], geometry );
  510.         dotNL = dot( geometry.normal, directLight.direction );
  511.         directLightColor_Diffuse = PI * directLight.color;
  512.         vLightFront += saturate( dotNL ) * directLightColor_Diffuse;
  513.         #ifdef DOUBLE_SIDED
  514.             vLightBack += saturate( -dotNL ) * directLightColor_Diffuse;
  515.         #endif
  516.     }
  517. #endif
  518. #if NUM_HEMI_LIGHTS > 0
  519.     for ( int i = 0; i < NUM_HEMI_LIGHTS; i ++ ) {
  520.         vLightFront += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry );
  521.         #ifdef DOUBLE_SIDED
  522.             vLightBack += getHemisphereLightIrradiance( hemisphereLights[ i ], backGeometry );
  523.         #endif
  524.     }
  525. #endif
  526.  
  527. #ifdef USE_SHADOWMAP
  528.     #if NUM_DIR_LIGHTS > 0
  529.     for ( int i = 0; i < NUM_DIR_LIGHTS; i ++ ) {
  530.         vDirectionalShadowCoord[ i ] = directionalShadowMatrix[ i ] * worldPosition;
  531.     }
  532.     #endif
  533.     #if NUM_SPOT_LIGHTS > 0
  534.     for ( int i = 0; i < NUM_SPOT_LIGHTS; i ++ ) {
  535.         vSpotShadowCoord[ i ] = spotShadowMatrix[ i ] * worldPosition;
  536.     }
  537.     #endif
  538.     #if NUM_POINT_LIGHTS > 0
  539.     for ( int i = 0; i < NUM_POINT_LIGHTS; i ++ ) {
  540.         vPointShadowCoord[ i ] = pointShadowMatrix[ i ] * worldPosition;
  541.     }
  542.     #endif
  543. #endif
  544.  
  545. }
Add Comment
Please, Sign In to add comment