






















(function () {
  'use strict';

  if (typeof THREE === 'undefined' || !THREE.ShaderChunk) return;

  var src = THREE.ShaderChunk.lights_fragment_begin;
  var applied = 0, missed = [];

  
  var COPIES = [
    ['pointLight = pointLights[ i ];',
     ['position', 'color', 'distance', 'decay'], 'pointLight', 'pointLights'],
    ['spotLight = spotLights[ i ];',
     ['position', 'direction', 'color', 'distance', 'decay', 'coneCos', 'penumbraCos'],
     'spotLight', 'spotLights'],
    ['directionalLight = directionalLights[ i ];',
     ['direction', 'color'], 'directionalLight', 'directionalLights'],
    ['rectAreaLight = rectAreaLights[ i ];',
     ['color', 'position', 'halfWidth', 'halfHeight'], 'rectAreaLight', 'rectAreaLights']
  ];

  COPIES.forEach(function (c) {
    var stmt = c[0], members = c[1], local = c[2], arr = c[3];
    if (src.indexOf(stmt) === -1) { missed.push(stmt); return; }
    var expanded = members.map(function (m) {
      return local + '.' + m + ' = ' + arr + '[ i ].' + m + ';';
    }).join('\n\t\t');
    src = src.split(stmt).join(expanded);
    applied++;
  });

  

  var HEMI_OLD = 'irradiance += getHemisphereLightIrradiance( hemisphereLights[ i ], geometry.normal );';
  var HEMI_NEW = 'hemisphereLight.direction = hemisphereLights[ i ].direction;\n' +
                 '\t\t\themisphereLight.skyColor = hemisphereLights[ i ].skyColor;\n' +
                 '\t\t\themisphereLight.groundColor = hemisphereLights[ i ].groundColor;\n' +
                 '\t\t\tirradiance += getHemisphereLightIrradiance( hemisphereLight, geometry.normal );';
  if (src.indexOf(HEMI_OLD) !== -1) {
    src = src.split(HEMI_OLD).join(HEMI_NEW);
    
    src = src.replace('\t#if ( NUM_HEMI_LIGHTS > 0 )\n',
                      '\t#if ( NUM_HEMI_LIGHTS > 0 )\n\t\tHemisphereLight hemisphereLight;\n');
    applied++;
  } else {
    missed.push(HEMI_OLD);
  }

  THREE.ShaderChunk.lights_fragment_begin = src;

  if (missed.length) {
    
    console.warn('[gl_compat] three r' + THREE.REVISION +
                 ' 的 lights_fragment_begin 与补丁不匹配，' + missed.length +
                 ' 处未替换（已应用 ' + applied + ' 处）：' + missed.join(' | '));
  }
})();
