@galacean/engine-core 1.0.0-beta.14 → 1.0.0-beta.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/module.js CHANGED
@@ -3000,15 +3000,24 @@ var GLCapabilityType;
3000
3000
  }
3001
3001
  var _proto = DisorderedArray.prototype;
3002
3002
  _proto.add = function add(element) {
3003
- if (this.length === this._elements.length) this._elements.push(element);
3004
- else this._elements[this.length] = element;
3003
+ if (this.length === this._elements.length) {
3004
+ this._elements.push(element);
3005
+ } else {
3006
+ this._elements[this.length] = element;
3007
+ }
3005
3008
  this.length++;
3006
3009
  };
3007
3010
  _proto.delete = function _delete(element) {
3008
- //TODO: It can be optimized for custom binary search and other algorithms, currently this._elements>=this.length wastes performance.
3011
+ // @todo: It can be optimized for custom binary search and other algorithms, currently this._elements>=this.length wastes performance.
3009
3012
  var index = this._elements.indexOf(element);
3010
3013
  this.deleteByIndex(index);
3011
3014
  };
3015
+ _proto.set = function set(index, element) {
3016
+ if (index >= this.length) {
3017
+ throw "Index is out of range.";
3018
+ }
3019
+ this._elements[index] = element;
3020
+ };
3012
3021
  _proto.get = function get(index) {
3013
3022
  if (index >= this.length) {
3014
3023
  throw "Index is out of range.";
@@ -3016,9 +3025,9 @@ var GLCapabilityType;
3016
3025
  return this._elements[index];
3017
3026
  };
3018
3027
  /**
3019
- *
3020
- * @param index
3021
- * @returns The replaced item is used to reset its index.
3028
+ * Delete the element at the specified index.
3029
+ * @param index - The index of the element to be deleted
3030
+ * @returns The replaced item is used to reset its index
3022
3031
  */ _proto.deleteByIndex = function deleteByIndex(index) {
3023
3032
  var elements = this._elements;
3024
3033
  var end = null;
@@ -8386,31 +8395,18 @@ __decorate([
8386
8395
  };
8387
8396
  /**
8388
8397
  * @internal
8389
- */ _proto._getSunLightIndex = function _getSunLightIndex() {
8398
+ */ _proto._updateSunLightIndex = function _updateSunLightIndex() {
8390
8399
  var directLights = this._directLights;
8391
- var sunLightIndex = -1;
8392
- var maxIntensity = Number.NEGATIVE_INFINITY;
8393
- var hasShadowLight = false;
8394
- for(var i = 0, n = directLights.length; i < n; i++){
8395
- var directLight = directLights.get(i);
8396
- if (directLight.shadowType !== ShadowType.None && !hasShadowLight) {
8397
- maxIntensity = Number.NEGATIVE_INFINITY;
8398
- hasShadowLight = true;
8399
- }
8400
- var intensity = directLight.intensity * directLight.color.getBrightness();
8401
- if (hasShadowLight) {
8402
- if (directLight.shadowType !== ShadowType.None && maxIntensity < intensity) {
8403
- maxIntensity = intensity;
8404
- sunLightIndex = i;
8405
- }
8406
- } else {
8407
- if (maxIntensity < intensity) {
8408
- maxIntensity = intensity;
8409
- sunLightIndex = i;
8410
- }
8411
- }
8400
+ var index = this._getSunLightIndex();
8401
+ // -1 means no sun light, 0 means the first direct light already is sun light
8402
+ if (index > 0) {
8403
+ var firstLight = directLights.get(0);
8404
+ var sunLight = directLights.get(index);
8405
+ directLights.set(0, sunLight);
8406
+ directLights.set(index, firstLight);
8407
+ sunLight._lightIndex = 0;
8408
+ firstLight._lightIndex = index;
8412
8409
  }
8413
- return sunLightIndex;
8414
8410
  };
8415
8411
  /**
8416
8412
  * @internal
@@ -8459,6 +8455,32 @@ __decorate([
8459
8455
  this._pointLights.garbageCollection();
8460
8456
  this._directLights.garbageCollection();
8461
8457
  };
8458
+ _proto._getSunLightIndex = function _getSunLightIndex() {
8459
+ var directLights = this._directLights;
8460
+ var sunLightIndex = -1;
8461
+ var maxIntensity = Number.NEGATIVE_INFINITY;
8462
+ var hasShadowLight = false;
8463
+ for(var i = 0, n = directLights.length; i < n; i++){
8464
+ var directLight = directLights.get(i);
8465
+ if (directLight.shadowType !== ShadowType.None && !hasShadowLight) {
8466
+ maxIntensity = Number.NEGATIVE_INFINITY;
8467
+ hasShadowLight = true;
8468
+ }
8469
+ var intensity = directLight.intensity * directLight.color.getBrightness();
8470
+ if (hasShadowLight) {
8471
+ if (directLight.shadowType !== ShadowType.None && maxIntensity < intensity) {
8472
+ maxIntensity = intensity;
8473
+ sunLightIndex = i;
8474
+ }
8475
+ } else {
8476
+ if (maxIntensity < intensity) {
8477
+ maxIntensity = intensity;
8478
+ sunLightIndex = i;
8479
+ }
8480
+ }
8481
+ }
8482
+ return sunLightIndex;
8483
+ };
8462
8484
  return LightManager;
8463
8485
  }();
8464
8486
 
@@ -9477,6 +9499,7 @@ __decorate([
9477
9499
  /**
9478
9500
  * @override
9479
9501
  */ _proto._onDestroy = function _onDestroy() {
9502
+ ReferResource.prototype._onDestroy.call(this);
9480
9503
  this._shader = null;
9481
9504
  this._shaderData = null;
9482
9505
  this._renderStates.length = 0;
@@ -9668,7 +9691,7 @@ var begin_mobile_frag = "#define GLSLIFY 1\nvec4 ambient=vec4(0.0);vec4 emission
9668
9691
 
9669
9692
  var begin_viewdir_frag = "#define GLSLIFY 1\n#ifdef MATERIAL_NEED_WORLD_POS\nvec3 V=normalize(camera_Position-v_pos);\n#endif\n"; // eslint-disable-line
9670
9693
 
9671
- var mobile_blinnphong_frag = "#define GLSLIFY 1\n#ifdef MATERIAL_HAS_NORMALTEXTURE\nmat3 tbn=getTBN(gl_FrontFacing);vec3 N=getNormalByNormalTexture(tbn,material_NormalTexture,material_NormalIntensity,v_uv,gl_FrontFacing);\n#else\nvec3 N=getNormal(gl_FrontFacing);\n#endif\nvec3 lightDiffuse=vec3(0.0,0.0,0.0);vec3 lightSpecular=vec3(0.0,0.0,0.0);float shadowAttenuation=1.0;\n#ifdef SCENE_DIRECT_LIGHT_COUNT\nshadowAttenuation=1.0;\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nshadowAttenuation*=sampleShadowMap();int sunIndex=int(scene_ShadowInfo.z);\n#endif\nDirectLight directionalLight;for(int i=0;i<SCENE_DIRECT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_DirectLightCullingMask[i]))continue;directionalLight.color=scene_DirectLightColor[i];\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nif(i==sunIndex){directionalLight.color*=shadowAttenuation;}\n#endif\ndirectionalLight.direction=scene_DirectLightDirection[i];float d=max(dot(N,-directionalLight.direction),0.0);lightDiffuse+=directionalLight.color*d;vec3 halfDir=normalize(V-directionalLight.direction);float s=pow(clamp(dot(N,halfDir),0.0,1.0),material_Shininess);lightSpecular+=directionalLight.color*s;}\n#endif\n#ifdef SCENE_POINT_LIGHT_COUNT\nPointLight pointLight;for(int i=0;i<SCENE_POINT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_PointLightCullingMask[i]))continue;pointLight.color=scene_PointLightColor[i];pointLight.position=scene_PointLightPosition[i];pointLight.distance=scene_PointLightDistance[i];vec3 direction=v_pos-pointLight.position;float dist=length(direction);direction/=dist;float decay=clamp(1.0-pow(dist/pointLight.distance,4.0),0.0,1.0);float d=max(dot(N,-direction),0.0)*decay;lightDiffuse+=pointLight.color*d;vec3 halfDir=normalize(V-direction);float s=pow(clamp(dot(N,halfDir),0.0,1.0),material_Shininess)*decay;lightSpecular+=pointLight.color*s;}\n#endif\n#ifdef SCENE_SPOT_LIGHT_COUNT\nSpotLight spotLight;for(int i=0;i<SCENE_SPOT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_SpotLightCullingMask[i]))continue;spotLight.color=scene_SpotLightColor[i];spotLight.position=scene_SpotLightPosition[i];spotLight.direction=scene_SpotLightDirection[i];spotLight.distance=scene_SpotLightDistance[i];spotLight.angleCos=scene_SpotLightAngleCos[i];spotLight.penumbraCos=scene_SpotLightPenumbraCos[i];vec3 direction=spotLight.position-v_pos;float lightDistance=length(direction);direction/=lightDistance;float angleCos=dot(direction,-spotLight.direction);float decay=clamp(1.0-pow(lightDistance/spotLight.distance,4.0),0.0,1.0);float spotEffect=smoothstep(spotLight.penumbraCos,spotLight.angleCos,angleCos);float decayTotal=decay*spotEffect;float d=max(dot(N,direction),0.0)*decayTotal;lightDiffuse+=spotLight.color*d;vec3 halfDir=normalize(V+direction);float s=pow(clamp(dot(N,halfDir),0.0,1.0),material_Shininess)*decayTotal;lightSpecular+=spotLight.color*s;}\n#endif\ndiffuse*=vec4(lightDiffuse,1.0);specular*=vec4(lightSpecular,1.0);\n#ifdef MATERIAL_IS_ALPHA_CUTOFF\nif(diffuse.a<material_AlphaCutoff){discard;}\n#endif\n"; // eslint-disable-line
9694
+ var mobile_blinnphong_frag = "#define GLSLIFY 1\n#ifdef MATERIAL_HAS_NORMALTEXTURE\nmat3 tbn=getTBN(gl_FrontFacing);vec3 N=getNormalByNormalTexture(tbn,material_NormalTexture,material_NormalIntensity,v_uv,gl_FrontFacing);\n#else\nvec3 N=getNormal(gl_FrontFacing);\n#endif\nvec3 lightDiffuse=vec3(0.0,0.0,0.0);vec3 lightSpecular=vec3(0.0,0.0,0.0);float shadowAttenuation=1.0;\n#ifdef SCENE_DIRECT_LIGHT_COUNT\nshadowAttenuation=1.0;\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nshadowAttenuation*=sampleShadowMap();\n#endif\nDirectLight directionalLight;for(int i=0;i<SCENE_DIRECT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_DirectLightCullingMask[i]))continue;directionalLight.color=scene_DirectLightColor[i];\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nif(i==0){directionalLight.color*=shadowAttenuation;}\n#endif\ndirectionalLight.direction=scene_DirectLightDirection[i];float d=max(dot(N,-directionalLight.direction),0.0);lightDiffuse+=directionalLight.color*d;vec3 halfDir=normalize(V-directionalLight.direction);float s=pow(clamp(dot(N,halfDir),0.0,1.0),material_Shininess);lightSpecular+=directionalLight.color*s;}\n#endif\n#ifdef SCENE_POINT_LIGHT_COUNT\nPointLight pointLight;for(int i=0;i<SCENE_POINT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_PointLightCullingMask[i]))continue;pointLight.color=scene_PointLightColor[i];pointLight.position=scene_PointLightPosition[i];pointLight.distance=scene_PointLightDistance[i];vec3 direction=v_pos-pointLight.position;float dist=length(direction);direction/=dist;float decay=clamp(1.0-pow(dist/pointLight.distance,4.0),0.0,1.0);float d=max(dot(N,-direction),0.0)*decay;lightDiffuse+=pointLight.color*d;vec3 halfDir=normalize(V-direction);float s=pow(clamp(dot(N,halfDir),0.0,1.0),material_Shininess)*decay;lightSpecular+=pointLight.color*s;}\n#endif\n#ifdef SCENE_SPOT_LIGHT_COUNT\nSpotLight spotLight;for(int i=0;i<SCENE_SPOT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_SpotLightCullingMask[i]))continue;spotLight.color=scene_SpotLightColor[i];spotLight.position=scene_SpotLightPosition[i];spotLight.direction=scene_SpotLightDirection[i];spotLight.distance=scene_SpotLightDistance[i];spotLight.angleCos=scene_SpotLightAngleCos[i];spotLight.penumbraCos=scene_SpotLightPenumbraCos[i];vec3 direction=spotLight.position-v_pos;float lightDistance=length(direction);direction/=lightDistance;float angleCos=dot(direction,-spotLight.direction);float decay=clamp(1.0-pow(lightDistance/spotLight.distance,4.0),0.0,1.0);float spotEffect=smoothstep(spotLight.penumbraCos,spotLight.angleCos,angleCos);float decayTotal=decay*spotEffect;float d=max(dot(N,direction),0.0)*decayTotal;lightDiffuse+=spotLight.color*d;vec3 halfDir=normalize(V+direction);float s=pow(clamp(dot(N,halfDir),0.0,1.0),material_Shininess)*decayTotal;lightSpecular+=spotLight.color*s;}\n#endif\ndiffuse*=vec4(lightDiffuse,1.0);specular*=vec4(lightSpecular,1.0);\n#ifdef MATERIAL_IS_ALPHA_CUTOFF\nif(diffuse.a<material_AlphaCutoff){discard;}\n#endif\n"; // eslint-disable-line
9672
9695
 
9673
9696
  var noise_cellular = "#define GLSLIFY 1\n#include <noise_cellular_2D>\n#include <noise_cellular_3D>\n#include <noise_cellular_2x2>\n#include <noise_cellular_2x2x2>\n"; // eslint-disable-line
9674
9697
 
@@ -9708,7 +9731,7 @@ var pbr_helper = "#define GLSLIFY 1\n#include <normal_get>\nfloat computeSpecula
9708
9731
 
9709
9732
  var brdf = "#define GLSLIFY 1\nfloat F_Schlick(float dotLH){return 0.04+0.96*(pow(1.0-dotLH,5.0));}vec3 F_Schlick(vec3 specularColor,float dotLH){float fresnel=exp2((-5.55473*dotLH-6.98316)*dotLH);return(1.0-specularColor)*fresnel+specularColor;}float G_GGX_SmithCorrelated(float alpha,float dotNL,float dotNV){float a2=pow2(alpha);float gv=dotNL*sqrt(a2+(1.0-a2)*pow2(dotNV));float gl=dotNV*sqrt(a2+(1.0-a2)*pow2(dotNL));return 0.5/max(gv+gl,EPSILON);}float D_GGX(float alpha,float dotNH){float a2=pow2(alpha);float denom=pow2(dotNH)*(a2-1.0)+1.0;return RECIPROCAL_PI*a2/pow2(denom);}vec3 BRDF_Specular_GGX(vec3 incidentDirection,vec3 viewDir,vec3 normal,vec3 specularColor,float roughness){float alpha=pow2(roughness);vec3 halfDir=normalize(incidentDirection+viewDir);float dotNL=saturate(dot(normal,incidentDirection));float dotNV=saturate(dot(normal,viewDir));float dotNH=saturate(dot(normal,halfDir));float dotLH=saturate(dot(incidentDirection,halfDir));vec3 F=F_Schlick(specularColor,dotLH);float G=G_GGX_SmithCorrelated(alpha,dotNL,dotNV);float D=D_GGX(alpha,dotNH);return F*(G*D);}vec3 BRDF_Diffuse_Lambert(vec3 diffuseColor){return RECIPROCAL_PI*diffuseColor;}"; // eslint-disable-line
9710
9733
 
9711
- var direct_irradiance_frag_define = "#define GLSLIFY 1\n#include <ShadowFragmentDeclaration>\nvoid addDirectRadiance(vec3 incidentDirection,vec3 color,Geometry geometry,Material material,inout ReflectedLight reflectedLight){float attenuation=1.0;\n#ifdef MATERIAL_ENABLE_CLEAR_COAT\nfloat clearCoatDotNL=saturate(dot(geometry.clearCoatNormal,incidentDirection));vec3 clearCoatIrradiance=clearCoatDotNL*color;reflectedLight.directSpecular+=material.clearCoat*clearCoatIrradiance*BRDF_Specular_GGX(incidentDirection,geometry.viewDir,geometry.clearCoatNormal,vec3(0.04),material.clearCoatRoughness);attenuation-=material.clearCoat*F_Schlick(geometry.clearCoatDotNV);\n#endif\nfloat dotNL=saturate(dot(geometry.normal,incidentDirection));vec3 irradiance=dotNL*color*PI;reflectedLight.directSpecular+=attenuation*irradiance*BRDF_Specular_GGX(incidentDirection,geometry.viewDir,geometry.normal,material.specularColor,material.roughness);reflectedLight.directDiffuse+=attenuation*irradiance*BRDF_Diffuse_Lambert(material.diffuseColor);}\n#ifdef SCENE_DIRECT_LIGHT_COUNT\nvoid addDirectionalDirectLightRadiance(DirectLight directionalLight,Geometry geometry,Material material,inout ReflectedLight reflectedLight){vec3 color=directionalLight.color;vec3 direction=-directionalLight.direction;addDirectRadiance(direction,color,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_POINT_LIGHT_COUNT\nvoid addPointDirectLightRadiance(PointLight pointLight,Geometry geometry,Material material,inout ReflectedLight reflectedLight){vec3 lVector=pointLight.position-geometry.position;vec3 direction=normalize(lVector);float lightDistance=length(lVector);vec3 color=pointLight.color;color*=clamp(1.0-pow(lightDistance/pointLight.distance,4.0),0.0,1.0);addDirectRadiance(direction,color,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_SPOT_LIGHT_COUNT\nvoid addSpotDirectLightRadiance(SpotLight spotLight,Geometry geometry,Material material,inout ReflectedLight reflectedLight){vec3 lVector=spotLight.position-geometry.position;vec3 direction=normalize(lVector);float lightDistance=length(lVector);float angleCos=dot(direction,-spotLight.direction);float spotEffect=smoothstep(spotLight.penumbraCos,spotLight.angleCos,angleCos);float decayEffect=clamp(1.0-pow(lightDistance/spotLight.distance,4.0),0.0,1.0);vec3 color=spotLight.color;color*=spotEffect*decayEffect;addDirectRadiance(direction,color,geometry,material,reflectedLight);}\n#endif\nvoid addTotalDirectRadiance(Geometry geometry,Material material,inout ReflectedLight reflectedLight){float shadowAttenuation=1.0;\n#ifdef SCENE_DIRECT_LIGHT_COUNT\nshadowAttenuation=1.0;\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nshadowAttenuation*=sampleShadowMap();int sunIndex=int(scene_ShadowInfo.z);\n#endif\nDirectLight directionalLight;for(int i=0;i<SCENE_DIRECT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_DirectLightCullingMask[i]))continue;directionalLight.color=scene_DirectLightColor[i];\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nif(i==sunIndex){directionalLight.color*=shadowAttenuation;}\n#endif\ndirectionalLight.direction=scene_DirectLightDirection[i];addDirectionalDirectLightRadiance(directionalLight,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_POINT_LIGHT_COUNT\nPointLight pointLight;for(int i=0;i<SCENE_POINT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_PointLightCullingMask[i]))continue;pointLight.color=scene_PointLightColor[i];pointLight.position=scene_PointLightPosition[i];pointLight.distance=scene_PointLightDistance[i];addPointDirectLightRadiance(pointLight,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_SPOT_LIGHT_COUNT\nSpotLight spotLight;for(int i=0;i<SCENE_SPOT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_SpotLightCullingMask[i]))continue;spotLight.color=scene_SpotLightColor[i];spotLight.position=scene_SpotLightPosition[i];spotLight.direction=scene_SpotLightDirection[i];spotLight.distance=scene_SpotLightDistance[i];spotLight.angleCos=scene_SpotLightAngleCos[i];spotLight.penumbraCos=scene_SpotLightPenumbraCos[i];addSpotDirectLightRadiance(spotLight,geometry,material,reflectedLight);}\n#endif\n}"; // eslint-disable-line
9734
+ var direct_irradiance_frag_define = "#define GLSLIFY 1\n#include <ShadowFragmentDeclaration>\nvoid addDirectRadiance(vec3 incidentDirection,vec3 color,Geometry geometry,Material material,inout ReflectedLight reflectedLight){float attenuation=1.0;\n#ifdef MATERIAL_ENABLE_CLEAR_COAT\nfloat clearCoatDotNL=saturate(dot(geometry.clearCoatNormal,incidentDirection));vec3 clearCoatIrradiance=clearCoatDotNL*color;reflectedLight.directSpecular+=material.clearCoat*clearCoatIrradiance*BRDF_Specular_GGX(incidentDirection,geometry.viewDir,geometry.clearCoatNormal,vec3(0.04),material.clearCoatRoughness);attenuation-=material.clearCoat*F_Schlick(geometry.clearCoatDotNV);\n#endif\nfloat dotNL=saturate(dot(geometry.normal,incidentDirection));vec3 irradiance=dotNL*color*PI;reflectedLight.directSpecular+=attenuation*irradiance*BRDF_Specular_GGX(incidentDirection,geometry.viewDir,geometry.normal,material.specularColor,material.roughness);reflectedLight.directDiffuse+=attenuation*irradiance*BRDF_Diffuse_Lambert(material.diffuseColor);}\n#ifdef SCENE_DIRECT_LIGHT_COUNT\nvoid addDirectionalDirectLightRadiance(DirectLight directionalLight,Geometry geometry,Material material,inout ReflectedLight reflectedLight){vec3 color=directionalLight.color;vec3 direction=-directionalLight.direction;addDirectRadiance(direction,color,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_POINT_LIGHT_COUNT\nvoid addPointDirectLightRadiance(PointLight pointLight,Geometry geometry,Material material,inout ReflectedLight reflectedLight){vec3 lVector=pointLight.position-geometry.position;vec3 direction=normalize(lVector);float lightDistance=length(lVector);vec3 color=pointLight.color;color*=clamp(1.0-pow(lightDistance/pointLight.distance,4.0),0.0,1.0);addDirectRadiance(direction,color,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_SPOT_LIGHT_COUNT\nvoid addSpotDirectLightRadiance(SpotLight spotLight,Geometry geometry,Material material,inout ReflectedLight reflectedLight){vec3 lVector=spotLight.position-geometry.position;vec3 direction=normalize(lVector);float lightDistance=length(lVector);float angleCos=dot(direction,-spotLight.direction);float spotEffect=smoothstep(spotLight.penumbraCos,spotLight.angleCos,angleCos);float decayEffect=clamp(1.0-pow(lightDistance/spotLight.distance,4.0),0.0,1.0);vec3 color=spotLight.color;color*=spotEffect*decayEffect;addDirectRadiance(direction,color,geometry,material,reflectedLight);}\n#endif\nvoid addTotalDirectRadiance(Geometry geometry,Material material,inout ReflectedLight reflectedLight){float shadowAttenuation=1.0;\n#ifdef SCENE_DIRECT_LIGHT_COUNT\nshadowAttenuation=1.0;\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nshadowAttenuation*=sampleShadowMap();\n#endif\nDirectLight directionalLight;for(int i=0;i<SCENE_DIRECT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_DirectLightCullingMask[i]))continue;directionalLight.color=scene_DirectLightColor[i];\n#ifdef SCENE_IS_CALCULATE_SHADOWS\nif(i==0){directionalLight.color*=shadowAttenuation;}\n#endif\ndirectionalLight.direction=scene_DirectLightDirection[i];addDirectionalDirectLightRadiance(directionalLight,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_POINT_LIGHT_COUNT\nPointLight pointLight;for(int i=0;i<SCENE_POINT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_PointLightCullingMask[i]))continue;pointLight.color=scene_PointLightColor[i];pointLight.position=scene_PointLightPosition[i];pointLight.distance=scene_PointLightDistance[i];addPointDirectLightRadiance(pointLight,geometry,material,reflectedLight);}\n#endif\n#ifdef SCENE_SPOT_LIGHT_COUNT\nSpotLight spotLight;for(int i=0;i<SCENE_SPOT_LIGHT_COUNT;i++){if(isRendererCulledByLight(renderer_Layer.xy,scene_SpotLightCullingMask[i]))continue;spotLight.color=scene_SpotLightColor[i];spotLight.position=scene_SpotLightPosition[i];spotLight.direction=scene_SpotLightDirection[i];spotLight.distance=scene_SpotLightDistance[i];spotLight.angleCos=scene_SpotLightAngleCos[i];spotLight.penumbraCos=scene_SpotLightPenumbraCos[i];addSpotDirectLightRadiance(spotLight,geometry,material,reflectedLight);}\n#endif\n}"; // eslint-disable-line
9712
9735
 
9713
9736
  var ibl_frag_define = "#define GLSLIFY 1\nvec3 getLightProbeIrradiance(vec3 sh[9],vec3 normal){normal.x=-normal.x;vec3 result=sh[0]+sh[1]*(normal.y)+sh[2]*(normal.z)+sh[3]*(normal.x)+sh[4]*(normal.y*normal.x)+sh[5]*(normal.y*normal.z)+sh[6]*(3.0*normal.z*normal.z-1.0)+sh[7]*(normal.z*normal.x)+sh[8]*(normal.x*normal.x-normal.y*normal.y);return max(result,vec3(0.0));}vec3 envBRDFApprox(vec3 specularColor,float roughness,float dotNV){const vec4 c0=vec4(-1,-0.0275,-0.572,0.022);const vec4 c1=vec4(1,0.0425,1.04,-0.04);vec4 r=roughness*c0+c1;float a004=min(r.x*r.x,exp2(-9.28*dotNV))*r.x+r.y;vec2 AB=vec2(-1.04,1.04)*a004+r.zw;return specularColor*AB.x+AB.y;}float getSpecularMIPLevel(float roughness,int maxMIPLevel){return roughness*float(maxMIPLevel);}vec3 getLightProbeRadiance(vec3 viewDir,vec3 normal,float roughness,int maxMIPLevel,float specularIntensity){\n#ifndef SCENE_USE_SPECULAR_ENV\nreturn vec3(0);\n#else\nvec3 reflectVec=reflect(-viewDir,normal);reflectVec.x=-reflectVec.x;float specularMIPLevel=getSpecularMIPLevel(roughness,maxMIPLevel);\n#ifdef HAS_TEX_LOD\nvec4 envMapColor=textureCubeLodEXT(scene_EnvSpecularSampler,reflectVec,specularMIPLevel);\n#else\nvec4 envMapColor=textureCube(scene_EnvSpecularSampler,reflectVec,specularMIPLevel);\n#endif\n#ifdef SCENE_IS_DECODE_ENV_RGBM\nenvMapColor.rgb=RGBMToLinear(envMapColor,5.0).rgb;\n#ifdef ENGINE_IS_COLORSPACE_GAMMA\nenvMapColor=linearToGamma(envMapColor);\n#endif\n#else\n#ifndef ENGINE_IS_COLORSPACE_GAMMA\nenvMapColor=gammaToLinear(envMapColor);\n#endif\n#endif\nreturn envMapColor.rgb*specularIntensity;\n#endif\n}"; // eslint-disable-line
9714
9737
 
@@ -15442,10 +15465,10 @@ var Basic2DBatcher = /*#__PURE__*/ function() {
15442
15465
  var vertexElements = [];
15443
15466
  var vertexStride = this.createVertexElements(vertexElements);
15444
15467
  // vertices
15445
- var vertexBuffer = this._vertexBuffers[index] = new Buffer(engine, BufferBindFlag.VertexBuffer, MAX_VERTEX_COUNT * 4 * vertexStride, BufferUsage.Dynamic);
15468
+ var vertexBuffer = this._vertexBuffers[index] = new Buffer(engine, BufferBindFlag.VertexBuffer, MAX_VERTEX_COUNT * vertexStride, BufferUsage.Dynamic);
15446
15469
  vertexBuffer.isGCIgnored = true;
15447
15470
  // indices
15448
- var indiceBuffer = this._indiceBuffers[index] = new Buffer(engine, BufferBindFlag.IndexBuffer, MAX_VERTEX_COUNT * 2 * 3, BufferUsage.Dynamic);
15471
+ var indiceBuffer = this._indiceBuffers[index] = new Buffer(engine, BufferBindFlag.IndexBuffer, MAX_VERTEX_COUNT * 6, BufferUsage.Dynamic);
15449
15472
  indiceBuffer.isGCIgnored = true;
15450
15473
  mesh.setVertexBufferBinding(vertexBuffer, vertexStride);
15451
15474
  mesh.setIndexBufferBinding(indiceBuffer, IndexFormat.UInt16);
@@ -15995,20 +16018,25 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
15995
16018
 
15996
16019
  /**
15997
16020
  * Ambient light.
15998
- */ var AmbientLight = /*#__PURE__*/ function() {
15999
- function AmbientLight() {
16000
- this._diffuseSolidColor = new Color$1(0.212, 0.227, 0.259);
16001
- this._diffuseIntensity = 1.0;
16002
- this._specularIntensity = 1.0;
16003
- this._diffuseMode = DiffuseMode.SolidColor;
16004
- this._shArray = new Float32Array(27);
16005
- this._scenes = [];
16006
- this._specularTextureDecodeRGBM = false;
16021
+ */ var AmbientLight = /*#__PURE__*/ function(ReferResource) {
16022
+ _inherits(AmbientLight, ReferResource);
16023
+ function AmbientLight(engine) {
16024
+ var _this;
16025
+ _this = ReferResource.call(this, engine) || this;
16026
+ _this._diffuseSolidColor = new Color$1(0.212, 0.227, 0.259);
16027
+ _this._diffuseIntensity = 1.0;
16028
+ _this._specularIntensity = 1.0;
16029
+ _this._diffuseMode = DiffuseMode.SolidColor;
16030
+ _this._shArray = new Float32Array(27);
16031
+ _this._scenes = [];
16032
+ _this._specularTextureDecodeRGBM = false;
16033
+ return _this;
16007
16034
  }
16008
16035
  var _proto = AmbientLight.prototype;
16009
16036
  /**
16010
16037
  * @internal
16011
16038
  */ _proto._addToScene = function _addToScene(scene) {
16039
+ this._addReferCount(1);
16012
16040
  this._scenes.push(scene);
16013
16041
  var shaderData = scene.shaderData;
16014
16042
  shaderData.setColor(AmbientLight._diffuseColorProperty, this._diffuseSolidColor);
@@ -16022,9 +16050,13 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
16022
16050
  /**
16023
16051
  * @internal
16024
16052
  */ _proto._removeFromScene = function _removeFromScene(scene) {
16053
+ this._addReferCount(-1);
16025
16054
  var scenes = this._scenes;
16026
16055
  var index = scenes.indexOf(scene);
16027
16056
  scenes.splice(index, 1);
16057
+ var shaderData = scene.shaderData;
16058
+ shaderData.setTexture(AmbientLight._specularTextureProperty, null);
16059
+ shaderData.disableMacro(AmbientLight._specularMacro);
16028
16060
  };
16029
16061
  _proto._setDiffuseMode = function _setDiffuseMode(sceneShaderData) {
16030
16062
  if (this._diffuseMode === DiffuseMode.SphericalHarmonics) {
@@ -16212,7 +16244,7 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
16212
16244
  }
16213
16245
  ]);
16214
16246
  return AmbientLight;
16215
- }();
16247
+ }(ReferResource);
16216
16248
  (function() {
16217
16249
  AmbientLight._shMacro = ShaderMacro.getByName("SCENE_USE_SH");
16218
16250
  })();
@@ -16269,7 +16301,7 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
16269
16301
  _this.name = name || "";
16270
16302
  var shaderData = _this.shaderData;
16271
16303
  shaderData._addReferCount(1);
16272
- _this.ambientLight = new AmbientLight();
16304
+ _this.ambientLight = new AmbientLight(engine);
16273
16305
  engine.sceneManager._allScenes.push(_assert_this_initialized(_this));
16274
16306
  shaderData.enableMacro("SCENE_FOG_MODE", _this._fogMode.toString());
16275
16307
  shaderData.enableMacro("SCENE_SHADOW_CASCADED_COUNT", _this.shadowCascades.toString());
@@ -16377,6 +16409,7 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
16377
16409
  if (this._destroyed) {
16378
16410
  return;
16379
16411
  }
16412
+ EngineObject.prototype.destroy.call(this);
16380
16413
  this._destroy();
16381
16414
  var allScenes = this.engine.sceneManager._allScenes;
16382
16415
  allScenes.splice(allScenes.indexOf(this), 1);
@@ -16419,9 +16452,9 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
16419
16452
  var lightManager = engine._lightManager;
16420
16453
  engine.time._updateSceneShaderData(shaderData);
16421
16454
  lightManager._updateShaderData(this.shaderData);
16422
- var sunLightIndex = lightManager._getSunLightIndex();
16423
- if (sunLightIndex !== -1) {
16424
- var sunlight = lightManager._directLights.get(sunLightIndex);
16455
+ lightManager._updateSunLightIndex();
16456
+ if (lightManager._directLights.length > 0) {
16457
+ var sunlight = lightManager._directLights.get(0);
16425
16458
  shaderData.setColor(Scene._sunlightColorProperty, sunlight._getLightIntensityColor());
16426
16459
  shaderData.setVector3(Scene._sunlightDirectionProperty, sunlight.direction);
16427
16460
  this._sunLight = sunlight;
@@ -16456,6 +16489,7 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
16456
16489
  }
16457
16490
  this._activeCameras.length = 0;
16458
16491
  this.background.destroy();
16492
+ this._ambientLight && this._ambientLight._removeFromScene(this);
16459
16493
  this.shaderData._addReferCount(-1);
16460
16494
  };
16461
16495
  _proto._addToRootEntityList = function _addToRootEntityList(index, rootEntity) {
@@ -16683,10 +16717,9 @@ var TextRenderData = /*#__PURE__*/ function(RenderData) {
16683
16717
  * @internal
16684
16718
  */ _proto._destroyAllScene = function _destroyAllScene() {
16685
16719
  var allScenes = this._allScenes;
16686
- for(var i = 0, n = allScenes.length; i < n; i++){
16687
- allScenes[i]._destroy();
16720
+ while(allScenes[0]){
16721
+ allScenes[0].destroy();
16688
16722
  }
16689
- allScenes.length = 0;
16690
16723
  };
16691
16724
  _create_class(SceneManager, [
16692
16725
  {
@@ -18459,9 +18492,8 @@ var /**
18459
18492
  var lightUp = this._lightUp;
18460
18493
  var lightSide = this._lightSide;
18461
18494
  var lightForward = shadowSliceData.virtualCamera.forward;
18462
- var sunLightIndex = engine._lightManager._getSunLightIndex();
18463
- if (sunLightIndex !== -1) {
18464
- var light = camera.scene._sunLight;
18495
+ var light = camera.scene._sunLight;
18496
+ if (light) {
18465
18497
  var shadowFar = Math.min(camera.scene.shadowDistance, camera.farClipPlane);
18466
18498
  this._getCascadesSplitDistance(shadowFar);
18467
18499
  // prepare render target
@@ -18474,7 +18506,7 @@ var /**
18474
18506
  rhi.clearRenderTarget(engine, CameraClearFlags.All, CascadedShadowCasterPass._clearColor);
18475
18507
  }
18476
18508
  this._shadowInfos.x = light.shadowStrength;
18477
- this._shadowInfos.z = sunLightIndex;
18509
+ this._shadowInfos.z = 0; // @todo: sun light index always 0
18478
18510
  // prepare light and camera direction
18479
18511
  Matrix.rotationQuaternion(light.entity.transform.worldRotationQuaternion, lightWorld);
18480
18512
  lightSide.set(lightWorldE[0], lightWorldE[1], lightWorldE[2]);
@@ -20809,6 +20841,15 @@ var TextVerticalAlignment;
20809
20841
  _this._dirtyUpdateFlag = 0x7;
20810
20842
  /** @internal */ _this._updateFlagManager = new UpdateFlagManager();
20811
20843
  _this._texture = texture;
20844
+ _this._onRegionChange = _this._onRegionChange.bind(_assert_this_initialized(_this));
20845
+ _this._onPivotChange = _this._onPivotChange.bind(_assert_this_initialized(_this));
20846
+ _this._onBorderChange = _this._onBorderChange.bind(_assert_this_initialized(_this));
20847
+ // @ts-ignore
20848
+ _this._region._onValueChanged = _this._onRegionChange;
20849
+ // @ts-ignore
20850
+ _this._pivot._onValueChanged = _this._onPivotChange;
20851
+ // @ts-ignore
20852
+ _this._border._onValueChanged = _this._onBorderChange;
20812
20853
  region && _this._region.copyFrom(region);
20813
20854
  pivot && _this._pivot.copyFrom(pivot);
20814
20855
  border && _this._border.copyFrom(border);
@@ -20948,6 +20989,34 @@ var TextVerticalAlignment;
20948
20989
  }
20949
20990
  this._updateFlagManager.dispatch(type);
20950
20991
  };
20992
+ _proto._onRegionChange = function _onRegionChange() {
20993
+ var _this = this, region = _this._region;
20994
+ // @ts-ignore
20995
+ region._onValueChanged = null;
20996
+ var x = MathUtil$1.clamp(region.x, 0, 1);
20997
+ var y = MathUtil$1.clamp(region.y, 0, 1);
20998
+ region.set(x, y, MathUtil$1.clamp(region.width, 0, 1 - x), MathUtil$1.clamp(region.height, 0, 1 - y));
20999
+ this._dispatchSpriteChange(SpriteModifyFlags.region);
21000
+ if (this._customWidth === undefined || this._customHeight === undefined) {
21001
+ this._dispatchSpriteChange(SpriteModifyFlags.size);
21002
+ }
21003
+ // @ts-ignore
21004
+ region._onValueChanged = this._onRegionChange;
21005
+ };
21006
+ _proto._onPivotChange = function _onPivotChange() {
21007
+ this._dispatchSpriteChange(SpriteModifyFlags.pivot);
21008
+ };
21009
+ _proto._onBorderChange = function _onBorderChange() {
21010
+ var _this = this, border = _this._border;
21011
+ // @ts-ignore
21012
+ border._onValueChanged = null;
21013
+ var x = MathUtil$1.clamp(border.x, 0, 1);
21014
+ var y = MathUtil$1.clamp(border.y, 0, 1);
21015
+ border.set(x, y, MathUtil$1.clamp(border.z, 0, 1 - x), MathUtil$1.clamp(border.w, 0, 1 - y));
21016
+ this._dispatchSpriteChange(SpriteModifyFlags.border);
21017
+ // @ts-ignore
21018
+ border._onValueChanged = this._onBorderChange;
21019
+ };
20951
21020
  _create_class(Sprite, [
20952
21021
  {
20953
21022
  key: "texture",
@@ -21067,14 +21136,7 @@ var TextVerticalAlignment;
21067
21136
  return this._region;
21068
21137
  },
21069
21138
  set: function set(value) {
21070
- var region = this._region;
21071
- var x = MathUtil$1.clamp(value.x, 0, 1);
21072
- var y = MathUtil$1.clamp(value.y, 0, 1);
21073
- region.set(x, y, MathUtil$1.clamp(value.width, 0, 1 - x), MathUtil$1.clamp(value.height, 0, 1 - y));
21074
- this._dispatchSpriteChange(SpriteModifyFlags.region);
21075
- if (this._customWidth === undefined || this._customHeight === undefined) {
21076
- this._dispatchSpriteChange(SpriteModifyFlags.size);
21077
- }
21139
+ this._region !== value && this._region.copyFrom(value);
21078
21140
  }
21079
21141
  },
21080
21142
  {
@@ -21086,16 +21148,7 @@ var TextVerticalAlignment;
21086
21148
  return this._pivot;
21087
21149
  },
21088
21150
  set: function set(value) {
21089
- var pivot = this._pivot;
21090
- if (pivot === value) {
21091
- this._dispatchSpriteChange(SpriteModifyFlags.pivot);
21092
- } else {
21093
- var x = value.x, y = value.y;
21094
- if (pivot.x !== x || pivot.y !== y) {
21095
- pivot.set(x, y);
21096
- this._dispatchSpriteChange(SpriteModifyFlags.pivot);
21097
- }
21098
- }
21151
+ this._pivot !== value && this._pivot.copyFrom(value);
21099
21152
  }
21100
21153
  },
21101
21154
  {
@@ -21110,11 +21163,7 @@ var TextVerticalAlignment;
21110
21163
  return this._border;
21111
21164
  },
21112
21165
  set: function set(value) {
21113
- var border = this._border;
21114
- var x = MathUtil$1.clamp(value.x, 0, 1);
21115
- var y = MathUtil$1.clamp(value.y, 0, 1);
21116
- border.set(x, y, MathUtil$1.clamp(value.z, 0, 1 - x), MathUtil$1.clamp(value.w, 0, 1 - y));
21117
- this._dispatchSpriteChange(SpriteModifyFlags.border);
21166
+ this._border !== value && this._border.copyFrom(value);
21118
21167
  }
21119
21168
  }
21120
21169
  ]);