@xviewer.js/core 1.0.4-alpha.8 → 1.0.4

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/main.cjs CHANGED
@@ -3743,7 +3743,9 @@ uniform float roughness;
3743
3743
  uniform float metalness;
3744
3744
  uniform sampler2D map;
3745
3745
  uniform sampler2D normalMap;
3746
+ uniform vec4 normalScaleBias;
3746
3747
  uniform sampler2D roughnessMap;
3748
+ uniform vec4 roughnessScaleBias;
3747
3749
  uniform sampler2D aoMap;
3748
3750
  uniform float aoMapIntensity;
3749
3751
  uniform sampler2D lightMap;
@@ -3781,7 +3783,7 @@ void main() {
3781
3783
  vec2 reflectUv = coord.xy;
3782
3784
 
3783
3785
  #ifdef USE_NORMALMAP
3784
- vec4 texelNormal = texture2D(normalMap, UV_NORMAL);
3786
+ vec4 texelNormal = texture2D(normalMap, UV_NORMAL * normalScaleBias.xy + normalScaleBias.zw);
3785
3787
  vec3 normal = normalize(vec3(texelNormal.r * 2.0 - 1.0, texelNormal.b, texelNormal.g * 2.0 - 1.0));
3786
3788
  reflectUv += coord.z * normal.xz * 0.05;
3787
3789
  #endif
@@ -3808,7 +3810,7 @@ void main() {
3808
3810
  float roughnessFactor = roughness;
3809
3811
 
3810
3812
  #ifdef USE_ROUGHNESSMAP
3811
- roughnessFactor *= texture2D(roughnessMap, UV_ROUGHNESS).g * roughness;
3813
+ roughnessFactor *= texture2D(roughnessMap, UV_ROUGHNESS * roughnessScaleBias.xy + roughnessScaleBias.zw).g * roughness;
3812
3814
  #endif
3813
3815
 
3814
3816
  computeMultiscattering( geometryNormal, geometryViewDir, specularColor, specularF90, roughnessFactor, singleScattering, multiScattering );
@@ -3820,6 +3822,8 @@ void main() {
3820
3822
 
3821
3823
  #ifdef USE_LIGHTMAP
3822
3824
  irradiance += texture2D(lightMap, UV_LIGHTMAP).rgb * lightMapIntensity;
3825
+ #else
3826
+ irradiance += vec3(1.);
3823
3827
  #endif
3824
3828
 
3825
3829
  float lod = roughnessFactor * (1.7 - 0.7 * roughnessFactor) * 6.;
@@ -3906,12 +3910,24 @@ class ReflectorMaterial extends THREE.ShaderMaterial {
3906
3910
  set roughnessMap(v) {
3907
3911
  this.uniforms.roughnessMap.value = v;
3908
3912
  }
3913
+ get roughnessScaleBias() {
3914
+ return this.uniforms.roughnessScaleBias.value;
3915
+ }
3916
+ set roughnessScaleBias(v) {
3917
+ this.uniforms.roughnessScaleBias.value.copy(v);
3918
+ }
3909
3919
  get normalMap() {
3910
3920
  return this.uniforms.normalMap.value;
3911
3921
  }
3912
3922
  set normalMap(v) {
3913
3923
  this.uniforms.normalMap.value = v;
3914
3924
  }
3925
+ get normalScaleBias() {
3926
+ return this.uniforms.normalScaleBias.value;
3927
+ }
3928
+ set normalScaleBias(v) {
3929
+ this.uniforms.normalScaleBias.value.copy(v);
3930
+ }
3915
3931
  get aoMap() {
3916
3932
  return this.uniforms.aoMap.value;
3917
3933
  }
@@ -3976,9 +3992,15 @@ class ReflectorMaterial extends THREE.ShaderMaterial {
3976
3992
  roughnessMap: {
3977
3993
  value: null
3978
3994
  },
3995
+ roughnessScaleBias: {
3996
+ value: new THREE.Vector4(1, 1, 0, 0)
3997
+ },
3979
3998
  normalMap: {
3980
3999
  value: null
3981
4000
  },
4001
+ normalScaleBias: {
4002
+ value: new THREE.Vector4(1, 1, 0, 0)
4003
+ },
3982
4004
  aoMap: {
3983
4005
  value: null
3984
4006
  },
@@ -4024,9 +4046,15 @@ __decorate([
4024
4046
  __decorate([
4025
4047
  property
4026
4048
  ], ReflectorMaterial.prototype, "roughnessMap", null);
4049
+ __decorate([
4050
+ property
4051
+ ], ReflectorMaterial.prototype, "roughnessScaleBias", null);
4027
4052
  __decorate([
4028
4053
  property
4029
4054
  ], ReflectorMaterial.prototype, "normalMap", null);
4055
+ __decorate([
4056
+ property
4057
+ ], ReflectorMaterial.prototype, "normalScaleBias", null);
4030
4058
  __decorate([
4031
4059
  property
4032
4060
  ], ReflectorMaterial.prototype, "aoMap", null);
@@ -4343,10 +4371,13 @@ class TaskManager {
4343
4371
  destroy() {
4344
4372
  this._tasks = [];
4345
4373
  this._taskIndex = 0;
4346
- clearInterval(this._interval);
4347
4374
  }
4348
4375
  add(task) {
4349
4376
  this._tasks.push(task);
4377
+ if (this._onStartCalled === false) {
4378
+ this._onStartCalled = true;
4379
+ this.onStart && this.onStart();
4380
+ }
4350
4381
  }
4351
4382
  update() {
4352
4383
  let task = this._tasks[this._taskIndex];
@@ -4358,13 +4389,9 @@ class TaskManager {
4358
4389
  this.onError && this.onError(task);
4359
4390
  }
4360
4391
  ++this._taskIndex;
4361
- if (this._onstartCalled === false) {
4362
- this._onstartCalled = true;
4363
- this.onStart && this.onStart();
4364
- }
4365
4392
  this.onProgress && this.onProgress(task, this._taskIndex, this._tasks.length);
4366
4393
  if (this._taskIndex === this._tasks.length) {
4367
- this._onstartCalled = false;
4394
+ this._onStartCalled = false;
4368
4395
  this.onComplete && this.onComplete();
4369
4396
  }
4370
4397
  }
@@ -4375,9 +4402,7 @@ class TaskManager {
4375
4402
  this.onError = onError;
4376
4403
  this._tasks = [];
4377
4404
  this._taskIndex = 0;
4378
- this._interval = null;
4379
- this._onstartCalled = false;
4380
- this._interval = setInterval(()=>this.update());
4405
+ this._onStartCalled = false;
4381
4406
  }
4382
4407
  }
4383
4408
 
@@ -4530,10 +4555,11 @@ ResourceManager._texSettingKeys = [
4530
4555
  ];
4531
4556
 
4532
4557
  class Task {
4533
- constructor(excute, name = "task" + this.instanceId){
4534
- this.excute = excute;
4535
- this.name = name;
4558
+ constructor(excute, name){
4536
4559
  this.instanceId = Task._instanceCount++;
4560
+ this.name = "task" + this.instanceId;
4561
+ this.excute = excute;
4562
+ this.name = name || this.name;
4537
4563
  }
4538
4564
  }
4539
4565
  Task._instanceCount = 0;
@@ -4724,16 +4750,22 @@ class Viewer extends EventEmitter {
4724
4750
  }
4725
4751
  }
4726
4752
  loop(dt) {
4727
- dt = Math.min(dt, 0.067);
4728
- this._renderer.info.reset();
4729
- this._componentManager.update(dt);
4730
- this._componentManager.render(dt);
4753
+ this._taskManager.update();
4754
+ if (!this._loading && !this._tasking && this._running > 0) {
4755
+ this._running--;
4756
+ }
4757
+ if (this._running <= 0) {
4758
+ dt = Math.min(dt, 0.067);
4759
+ this._renderer.info.reset();
4760
+ this._componentManager.update(dt);
4761
+ this._componentManager.render(dt);
4762
+ }
4731
4763
  }
4732
4764
  start() {
4733
- if (this._running == false) {
4734
- this._running = true;
4765
+ if (this._active == false) {
4766
+ this._active = true;
4735
4767
  const frameCallback = (time)=>{
4736
- if (this._running) {
4768
+ if (this._active) {
4737
4769
  this._frame(time * 0.001);
4738
4770
  }
4739
4771
  };
@@ -4742,7 +4774,8 @@ class Viewer extends EventEmitter {
4742
4774
  return this;
4743
4775
  }
4744
4776
  stop() {
4745
- this._running = false;
4777
+ this._active = false;
4778
+ this._running = 4;
4746
4779
  this._time = this._lastTime = 0;
4747
4780
  return this;
4748
4781
  }
@@ -5092,7 +5125,8 @@ class Viewer extends EventEmitter {
5092
5125
  width: 1,
5093
5126
  height: 1,
5094
5127
  factor: 1
5095
- }, this._running = false, this._rootRotated = false, this._time = 0, this._lastTime = 0, this._lastFrameTime = 0, this._targetFrameRate = -1, this._fixedFrameTime = false, this._windowSize = ()=>[
5128
+ }, this._active = false, this._loading = false, this._tasking = false, this._running = 4 //延迟4帧执行
5129
+ , this._rootRotated = false, this._time = 0, this._lastTime = 0, this._lastFrameTime = 0, this._targetFrameRate = -1, this._fixedFrameTime = false, this._windowSize = ()=>[
5096
5130
  window.innerWidth,
5097
5131
  window.innerHeight
5098
5132
  ], this._orientation = Orientation.AUTO;
@@ -5124,21 +5158,14 @@ class Viewer extends EventEmitter {
5124
5158
  this._renderer.sortObjects = sortObjects;
5125
5159
  this._targetFrameRate = targetFrameRate;
5126
5160
  this._windowSize = typeof resize === "function" ? resize : resize === ResizeMode.AUTO ? this._windowSize : null;
5127
- const states = {
5128
- tasking: false,
5129
- loading: false};
5130
- const start = ()=>{
5131
- !states.tasking && !states.loading && this.start();
5132
- };
5133
5161
  const complete = (set, cb)=>{
5134
5162
  set();
5135
5163
  cb && cb();
5136
- !this._running && setTimeout(start);
5137
5164
  };
5138
- this._taskManager = new TaskManager(()=>complete(()=>states.tasking = false, tasker.onComplete), tasker.onProgress, tasker.onError);
5139
- this._taskManager.onStart = ()=>states.tasking = true;
5140
- this._loadingManager = new THREE.LoadingManager(()=>complete(()=>states.loading = false, loader.onComplete), loader.onProgress, loader.onError);
5141
- this._loadingManager.onStart = ()=>states.loading = true;
5165
+ this._taskManager = new TaskManager(()=>complete(()=>this._tasking = false, tasker.onComplete), tasker.onProgress, tasker.onError);
5166
+ this._taskManager.onStart = ()=>this._tasking = true;
5167
+ this._loadingManager = new THREE.LoadingManager(()=>complete(()=>this._loading = false, loader.onComplete), loader.onProgress, loader.onError);
5168
+ this._loadingManager.onStart = ()=>this._loading = true;
5142
5169
  this._resourceManager = new ResourceManager(this);
5143
5170
  this._componentManager = new ComponentManager(this);
5144
5171
  this._resourceOptions = {
@@ -5165,7 +5192,7 @@ class Viewer extends EventEmitter {
5165
5192
  setTimeout(()=>{
5166
5193
  this.rotate();
5167
5194
  this.resize();
5168
- start();
5195
+ this.start();
5169
5196
  });
5170
5197
  }
5171
5198
  }
@@ -5889,10 +5916,698 @@ function getFilesFromItemList(items, onDone) {
5889
5916
  }
5890
5917
  }
5891
5918
 
5919
+ const newline = /\n/;
5920
+ const newlineChar = '\n';
5921
+ const whitespace = /\s/;
5922
+ function wordwrap(text, opt) {
5923
+ opt = opt || {};
5924
+ //zero width results in nothing visible
5925
+ if (opt.width === 0 && opt.mode !== 'nowrap') return [];
5926
+ text = text || '';
5927
+ const width = typeof opt.width === 'number' ? opt.width : Number.MAX_VALUE;
5928
+ const start = Math.max(0, opt.start || 0);
5929
+ const end = typeof opt.end === 'number' ? opt.end : text.length;
5930
+ const mode = opt.mode;
5931
+ const letterSpacing = opt.letterSpacing || 0;
5932
+ const measure = opt.measure || monospace;
5933
+ if (mode === 'pre') return pre(measure, text, start, end, width, letterSpacing);
5934
+ else return greedy(measure, text, start, end, width, mode, letterSpacing);
5935
+ }
5936
+ function idxOf(text, chr, start, end) {
5937
+ var idx = text.indexOf(chr, start);
5938
+ if (idx === -1 || idx > end) return end;
5939
+ return idx;
5940
+ }
5941
+ function isWhitespace(chr) {
5942
+ return whitespace.test(chr);
5943
+ }
5944
+ function pre(measure, text, start, end, width, letterSpacing) {
5945
+ var lines = [];
5946
+ var lineStart = start;
5947
+ for(var i = start; i < end && i < text.length; i++){
5948
+ var chr = text.charAt(i);
5949
+ var isNewline = newline.test(chr);
5950
+ //If we've reached a newline, then step down a line
5951
+ //Or if we've reached the EOF
5952
+ if (isNewline || i === end - 1) {
5953
+ var lineEnd = isNewline ? i : i + 1;
5954
+ var measured = measure(text, lineStart, lineEnd, width, letterSpacing);
5955
+ lines.push(measured);
5956
+ lineStart = i + 1;
5957
+ }
5958
+ }
5959
+ return lines;
5960
+ }
5961
+ function greedy(measure, text, start, end, width, mode, letterSpacing) {
5962
+ //A greedy word wrapper based on LibGDX algorithm
5963
+ //https://github.com/libgdx/libgdx/blob/master/gdx/src/com/badlogic/gdx/graphics/g2d/BitmapFontCache.java
5964
+ var lines = [];
5965
+ var testWidth = width;
5966
+ //if 'nowrap' is specified, we only wrap on newline chars
5967
+ if (mode === 'nowrap') testWidth = Number.MAX_VALUE;
5968
+ while(start < end && start < text.length){
5969
+ //get next newline position
5970
+ var newLine = idxOf(text, newlineChar, start, end);
5971
+ //eat whitespace at start of line
5972
+ while(start < newLine){
5973
+ if (!isWhitespace(text.charAt(start))) break;
5974
+ start++;
5975
+ }
5976
+ //determine visible # of glyphs for the available width
5977
+ var measured = measure(text, start, newLine, testWidth, letterSpacing);
5978
+ var lineEnd = start + (measured.end - measured.start);
5979
+ var nextStart = lineEnd + newlineChar.length;
5980
+ //if we had to cut the line before the next newline...
5981
+ if (lineEnd < newLine) {
5982
+ //find char to break on
5983
+ while(lineEnd > start){
5984
+ if (isWhitespace(text.charAt(lineEnd))) break;
5985
+ lineEnd--;
5986
+ }
5987
+ if (lineEnd === start) {
5988
+ if (nextStart > start + newlineChar.length) nextStart--;
5989
+ lineEnd = nextStart; // If no characters to break, show all.
5990
+ } else {
5991
+ nextStart = lineEnd;
5992
+ //eat whitespace at end of line
5993
+ while(lineEnd > start){
5994
+ if (!isWhitespace(text.charAt(lineEnd - newlineChar.length))) break;
5995
+ lineEnd--;
5996
+ }
5997
+ }
5998
+ }
5999
+ if (lineEnd >= start) {
6000
+ var result = measure(text, start, lineEnd, testWidth);
6001
+ lines.push(result);
6002
+ }
6003
+ start = nextStart;
6004
+ }
6005
+ return lines;
6006
+ }
6007
+ //determines the visible number of glyphs within a given width
6008
+ function monospace(text, start, end, width) {
6009
+ var glyphs = Math.min(width, end - start);
6010
+ return {
6011
+ start: start,
6012
+ end: start + glyphs
6013
+ };
6014
+ }
6015
+
6016
+ const X_HEIGHTS = [
6017
+ 'x',
6018
+ 'e',
6019
+ 'a',
6020
+ 'o',
6021
+ 'n',
6022
+ 's',
6023
+ 'r',
6024
+ 'c',
6025
+ 'u',
6026
+ 'm',
6027
+ 'v',
6028
+ 'w',
6029
+ 'z'
6030
+ ];
6031
+ const M_WIDTHS = [
6032
+ 'm',
6033
+ 'w'
6034
+ ];
6035
+ const CAP_HEIGHTS = [
6036
+ 'H',
6037
+ 'I',
6038
+ 'N',
6039
+ 'E',
6040
+ 'F',
6041
+ 'K',
6042
+ 'L',
6043
+ 'T',
6044
+ 'U',
6045
+ 'V',
6046
+ 'W',
6047
+ 'X',
6048
+ 'Y',
6049
+ 'Z'
6050
+ ];
6051
+ const TAB_ID = '\t'.charCodeAt(0);
6052
+ const SPACE_ID = ' '.charCodeAt(0);
6053
+ const ALIGN_LEFT = 0, ALIGN_CENTER = 1, ALIGN_RIGHT = 2;
6054
+ function findChar(array, value, start) {
6055
+ start = start || 0;
6056
+ for(let i = start; i < array.length; i++){
6057
+ if (array[i].id === value) {
6058
+ return i;
6059
+ }
6060
+ }
6061
+ return -1;
6062
+ }
6063
+ function getGlyphById(font, id) {
6064
+ if (!font.chars || font.chars.length === 0) return null;
6065
+ let glyphIdx = findChar(font.chars, id);
6066
+ if (glyphIdx >= 0) return font.chars[glyphIdx];
6067
+ return null;
6068
+ }
6069
+ function getXHeight(font) {
6070
+ for(let i = 0; i < X_HEIGHTS.length; i++){
6071
+ let id = X_HEIGHTS[i].charCodeAt(0);
6072
+ let idx = findChar(font.chars, id);
6073
+ if (idx >= 0) return font.chars[idx].height;
6074
+ }
6075
+ return 0;
6076
+ }
6077
+ function getMGlyph(font) {
6078
+ for(let i = 0; i < M_WIDTHS.length; i++){
6079
+ let id = M_WIDTHS[i].charCodeAt(0);
6080
+ let idx = findChar(font.chars, id);
6081
+ if (idx >= 0) return font.chars[idx];
6082
+ }
6083
+ return 0;
6084
+ }
6085
+ function getCapHeight(font) {
6086
+ for(let i = 0; i < CAP_HEIGHTS.length; i++){
6087
+ let id = CAP_HEIGHTS[i].charCodeAt(0);
6088
+ let idx = findChar(font.chars, id);
6089
+ if (idx >= 0) return font.chars[idx].height;
6090
+ }
6091
+ return 0;
6092
+ }
6093
+ function getKerning(font, left, right) {
6094
+ if (!font.kernings || font.kernings.length === 0) return 0;
6095
+ let table = font.kernings;
6096
+ for(let i = 0; i < table.length; i++){
6097
+ let kern = table[i];
6098
+ if (kern.first === left && kern.second === right) return kern.amount;
6099
+ }
6100
+ return 0;
6101
+ }
6102
+ function getAlignType(align) {
6103
+ if (align === 'center') return ALIGN_CENTER;
6104
+ else if (align === 'right') return ALIGN_RIGHT;
6105
+ return ALIGN_LEFT;
6106
+ }
6107
+ class BMFontTextLayout {
6108
+ get width() {
6109
+ return this._width;
6110
+ }
6111
+ get height() {
6112
+ return this._height;
6113
+ }
6114
+ get descender() {
6115
+ return this._descender;
6116
+ }
6117
+ get ascender() {
6118
+ return this._ascender;
6119
+ }
6120
+ get xHeight() {
6121
+ return this._xHeight;
6122
+ }
6123
+ get baseline() {
6124
+ return this._baseline;
6125
+ }
6126
+ get capHeight() {
6127
+ return this._capHeight;
6128
+ }
6129
+ get lineHeight() {
6130
+ return this._lineHeight;
6131
+ }
6132
+ get glyphs() {
6133
+ return this._glyphs;
6134
+ }
6135
+ get linesTotal() {
6136
+ return this._linesTotal;
6137
+ }
6138
+ update(font, text, setting) {
6139
+ setting = Object.assign({
6140
+ tabSize: 4,
6141
+ width: 0,
6142
+ letterSpacing: 0,
6143
+ mode: "nowrap",
6144
+ align: "left"
6145
+ }, setting);
6146
+ this._font = font;
6147
+ const measure = this._computeMetrics.bind(this);
6148
+ const lines = wordwrap(text, {
6149
+ measure,
6150
+ ...setting
6151
+ });
6152
+ const minWidth = setting.width || 0;
6153
+ const glyphs = this._glyphs;
6154
+ const maxLineWidth = lines.reduce((prev, line)=>Math.max(prev, line.width, minWidth), 0);
6155
+ var _setting_lineHeight;
6156
+ //the pen position
6157
+ const lineHeight = (_setting_lineHeight = setting.lineHeight) != null ? _setting_lineHeight : font.common.lineHeight;
6158
+ const baseline = font.common.base;
6159
+ const descender = lineHeight - baseline;
6160
+ const letterSpacing = setting.letterSpacing || 0;
6161
+ const height = lineHeight * lines.length - descender;
6162
+ const align = getAlignType(setting.align);
6163
+ var _setting_anchor;
6164
+ const anchor = (_setting_anchor = setting.anchor) != null ? _setting_anchor : [
6165
+ 0.5,
6166
+ 0.5
6167
+ ];
6168
+ this._setupSpaceGlyphs(font, setting);
6169
+ //the metrics for this text layout
6170
+ this._width = maxLineWidth;
6171
+ this._height = height;
6172
+ this._descender = descender;
6173
+ this._baseline = baseline;
6174
+ this._xHeight = getXHeight(font);
6175
+ this._capHeight = getCapHeight(font);
6176
+ this._lineHeight = lineHeight;
6177
+ this._ascender = baseline - this._xHeight; //lineHeight - descender - this._xHeight
6178
+ const anchorOffset = [
6179
+ -maxLineWidth * anchor[0],
6180
+ 2 * lineHeight * anchor[1] - baseline
6181
+ ];
6182
+ let x = 0, y = 0;
6183
+ //draw text along baseline
6184
+ y -= height;
6185
+ glyphs.length = 0;
6186
+ for(let k = 0; k < lines.length; k++){
6187
+ const line = lines[k];
6188
+ let start = line.start;
6189
+ let end = line.end;
6190
+ let lineWidth = line.width;
6191
+ let lastGlyph;
6192
+ //for each glyph in that line...
6193
+ for(let i = start; i < end; i++){
6194
+ let id = text.charCodeAt(i);
6195
+ let glyph = this._getGlyph(font, id);
6196
+ if (glyph) {
6197
+ if (lastGlyph) x += getKerning(font, lastGlyph.id, glyph.id);
6198
+ let tx = x;
6199
+ if (align === ALIGN_CENTER) tx += (maxLineWidth - lineWidth) / 2;
6200
+ else if (align === ALIGN_RIGHT) tx += maxLineWidth - lineWidth;
6201
+ glyphs.push({
6202
+ position: [
6203
+ tx + anchorOffset[0],
6204
+ y + anchorOffset[1]
6205
+ ],
6206
+ data: glyph,
6207
+ index: i,
6208
+ line: k
6209
+ });
6210
+ //move pen forward
6211
+ x += glyph.xadvance + letterSpacing;
6212
+ lastGlyph = glyph;
6213
+ }
6214
+ }
6215
+ //next line down
6216
+ y += lineHeight;
6217
+ x = 0;
6218
+ }
6219
+ this._linesTotal = lines.length;
6220
+ }
6221
+ _setupSpaceGlyphs(font, setting) {
6222
+ //These are fallbacks, when the font doesn't include
6223
+ //' ' or '\t' glyphs
6224
+ if (!font.chars || font.chars.length === 0) return;
6225
+ //try to get space glyph
6226
+ //then fall back to the 'm' or 'w' glyphs
6227
+ //then fall back to the first glyph available
6228
+ const space = Object.assign({}, getGlyphById(font, SPACE_ID) || getMGlyph(font) || font.chars[0]);
6229
+ //and create a fallback for tab
6230
+ const tabWidth = setting.tabSize * space.xadvance;
6231
+ this._fallbackSpaceGlyph = space;
6232
+ this._fallbackTabGlyph = Object.assign(space, {
6233
+ x: 0,
6234
+ y: 0,
6235
+ xadvance: tabWidth,
6236
+ id: TAB_ID,
6237
+ xoffset: 0,
6238
+ yoffset: 0,
6239
+ width: 0,
6240
+ height: 0
6241
+ });
6242
+ }
6243
+ _getGlyph(font, id) {
6244
+ let glyph = getGlyphById(font, id);
6245
+ if (glyph) return glyph;
6246
+ else if (id === TAB_ID) return this._fallbackTabGlyph;
6247
+ else if (id === SPACE_ID) return this._fallbackSpaceGlyph;
6248
+ return null;
6249
+ }
6250
+ _computeMetrics(text, start, end, width, letterSpacing = 0) {
6251
+ let font = this._font;
6252
+ let curPen = 0;
6253
+ let curWidth = 0;
6254
+ let count = 0;
6255
+ let lastGlyph;
6256
+ if (!font.chars || font.chars.length === 0) {
6257
+ return {
6258
+ start: start,
6259
+ end: start,
6260
+ width: 0
6261
+ };
6262
+ }
6263
+ end = Math.min(text.length, end);
6264
+ for(let i = start; i < end; i++){
6265
+ let id = text.charCodeAt(i);
6266
+ let glyph = this._getGlyph(font, id);
6267
+ if (glyph) {
6268
+ //move pen forward
6269
+ glyph.xoffset;
6270
+ let kern = lastGlyph ? getKerning(font, lastGlyph.id, glyph.id) : 0;
6271
+ curPen += kern;
6272
+ let nextPen = curPen + glyph.xadvance + letterSpacing;
6273
+ let nextWidth = curPen + glyph.width;
6274
+ //we've hit our limit; we can't move onto the next glyph
6275
+ if (nextWidth >= width || nextPen >= width) break;
6276
+ //otherwise continue along our line
6277
+ curPen = nextPen;
6278
+ curWidth = nextWidth;
6279
+ lastGlyph = glyph;
6280
+ }
6281
+ count++;
6282
+ }
6283
+ //make sure rightmost edge lines up with rendered glyphs
6284
+ if (lastGlyph) curWidth += lastGlyph.xoffset;
6285
+ return {
6286
+ start: start,
6287
+ end: start + count,
6288
+ width: curWidth
6289
+ };
6290
+ }
6291
+ constructor(){
6292
+ this._width = 0;
6293
+ this._height = 0;
6294
+ this._descender = 0;
6295
+ this._ascender = 0;
6296
+ this._xHeight = 0;
6297
+ this._baseline = 0;
6298
+ this._capHeight = 0;
6299
+ this._lineHeight = 0;
6300
+ this._linesTotal = 0;
6301
+ this._glyphs = [];
6302
+ this._fallbackSpaceGlyph = null;
6303
+ this._fallbackTabGlyph = null;
6304
+ this._font = null;
6305
+ }
6306
+ }
6307
+
6308
+ const itemSize = 2;
6309
+ const box = {
6310
+ min: [
6311
+ 0,
6312
+ 0
6313
+ ],
6314
+ max: [
6315
+ 0,
6316
+ 0
6317
+ ]
6318
+ };
6319
+ function bounds(positions) {
6320
+ const count = positions.length / itemSize;
6321
+ box.min[0] = positions[0];
6322
+ box.min[1] = positions[1];
6323
+ box.max[0] = positions[0];
6324
+ box.max[1] = positions[1];
6325
+ for(let i = 0; i < count; i++){
6326
+ const x = positions[i * itemSize + 0];
6327
+ const y = positions[i * itemSize + 1];
6328
+ box.min[0] = Math.min(x, box.min[0]);
6329
+ box.min[1] = Math.min(y, box.min[1]);
6330
+ box.max[0] = Math.max(x, box.max[0]);
6331
+ box.max[1] = Math.max(y, box.max[1]);
6332
+ }
6333
+ }
6334
+ function computeBox(positions, output) {
6335
+ bounds(positions);
6336
+ output.min.set(box.min[0], box.min[1], 0);
6337
+ output.max.set(box.max[0], box.max[1], 0);
6338
+ }
6339
+ function computeSphere(positions, output) {
6340
+ bounds(positions);
6341
+ const minX = box.min[0];
6342
+ const minY = box.min[1];
6343
+ const maxX = box.max[0];
6344
+ const maxY = box.max[1];
6345
+ const width = maxX - minX;
6346
+ const height = maxY - minY;
6347
+ const length = Math.sqrt(width * width + height * height);
6348
+ output.center.set(minX + width / 2, minY + height / 2, 0);
6349
+ output.radius = length / 2;
6350
+ }
6351
+
6352
+ class BMFontTextGeometry extends THREE.BufferGeometry {
6353
+ update(fontAtlas, text, setting) {
6354
+ this.layout.update(fontAtlas.info, text, setting);
6355
+ const { indice, position, uv } = fontAtlas.build(this.layout, setting);
6356
+ this.setIndex(new THREE.BufferAttribute(indice, 1));
6357
+ this.setAttribute("position", new THREE.BufferAttribute(position, 2));
6358
+ this.setAttribute("uv", new THREE.BufferAttribute(uv, 2));
6359
+ }
6360
+ computeBoundingSphere() {
6361
+ if (this.boundingSphere === null) {
6362
+ this.boundingSphere = new THREE.Sphere();
6363
+ }
6364
+ const positions = this.attributes.position.array;
6365
+ const itemSize = this.attributes.position.itemSize;
6366
+ if (!positions || !itemSize || positions.length < 2) {
6367
+ this.boundingSphere.radius = 0;
6368
+ this.boundingSphere.center.set(0, 0, 0);
6369
+ return;
6370
+ }
6371
+ computeSphere(positions, this.boundingSphere);
6372
+ if (isNaN(this.boundingSphere.radius)) {
6373
+ console.error('THREE.BufferGeometry.computeBoundingSphere(): ' + 'Computed radius is NaN. The ' + '"position" attribute is likely to have NaN values.');
6374
+ }
6375
+ }
6376
+ computeBoundingBox() {
6377
+ if (this.boundingBox === null) {
6378
+ this.boundingBox = new THREE.Box3();
6379
+ }
6380
+ const bbox = this.boundingBox;
6381
+ const positions = this.attributes.position["array"];
6382
+ const itemSize = this.attributes.position.itemSize;
6383
+ if (!positions || !itemSize || positions.length < 2) {
6384
+ bbox.makeEmpty();
6385
+ return;
6386
+ }
6387
+ computeBox(positions, bbox);
6388
+ }
6389
+ constructor(...args){
6390
+ super(...args), this.layout = new BMFontTextLayout();
6391
+ }
6392
+ }
6393
+
6394
+ class Label extends Component {
6395
+ get text() {
6396
+ return this._text;
6397
+ }
6398
+ set text(v) {
6399
+ if (this._text !== v) {
6400
+ this._text = v;
6401
+ this.needsUpdate = true;
6402
+ }
6403
+ }
6404
+ get font() {
6405
+ return this._font;
6406
+ }
6407
+ set font(v) {
6408
+ if (this._font !== v) {
6409
+ this._font = v;
6410
+ this.needsUpdate = true;
6411
+ }
6412
+ }
6413
+ get setting() {
6414
+ return this._setting;
6415
+ }
6416
+ set setting(v) {
6417
+ if (JSON.stringify(this._setting) !== JSON.stringify(v)) {
6418
+ this._setting = v;
6419
+ this.needsUpdate = true;
6420
+ }
6421
+ }
6422
+ get layout() {
6423
+ return this.node.geometry.layout;
6424
+ }
6425
+ forceUpdate() {
6426
+ this.node.geometry.update(this.font, this.text, this.setting);
6427
+ }
6428
+ update(dt) {
6429
+ if (this.needsUpdate) {
6430
+ this.needsUpdate = false;
6431
+ this.forceUpdate();
6432
+ }
6433
+ }
6434
+ constructor({ text = "", font, material, ...setting }){
6435
+ super(), this.needsUpdate = false, this._text = "", this._font = null, this._setting = null;
6436
+ this.text = text;
6437
+ this.font = font;
6438
+ this.setting = setting;
6439
+ this.node = new THREE.Mesh(new BMFontTextGeometry(), material || font.material);
6440
+ this.node.geometry.update(this.font, this.text, this.setting);
6441
+ }
6442
+ }
6443
+
6444
+ const vert_MSDF = `
6445
+ varying vec2 v_uv;
6446
+
6447
+ void main() {
6448
+ gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0);
6449
+ v_uv = uv;
6450
+ }
6451
+ `;
6452
+ const frag_MSDF = `
6453
+ uniform sampler2D u_fontTexture;
6454
+ varying vec2 v_uv;
6455
+
6456
+ uniform vec3 u_color;
6457
+ uniform float u_opacity;
6458
+ uniform vec3 u_strokeColor;
6459
+ uniform float u_strokeWidth;
6460
+ uniform vec3 u_shadowColor;
6461
+ uniform float u_shadowBlur;
6462
+ uniform vec2 u_shadowOffset;
6463
+ uniform float u_weight;
6464
+
6465
+ float median(in float r, in float g, in float b) {
6466
+ return max(min(r, g), min(max(r, g), b));
6467
+ }
6468
+
6469
+ float signedDistance(in vec2 uv) {
6470
+ vec4 texel = texture2D(u_fontTexture, uv);
6471
+ return median(texel.r, texel.g, texel.b) - 0.5;
6472
+ }
6473
+
6474
+ void main() {
6475
+ vec4 color = vec4(u_color, 1.);
6476
+ float d = signedDistance(v_uv) + u_weight;
6477
+ float w = fwidth(d);
6478
+
6479
+ vec4 stroke = vec4(u_strokeColor, smoothstep(-w, w, d));
6480
+ color.a *= smoothstep(-w, w, d - u_strokeWidth);
6481
+ color = mix(stroke, color, color.a);
6482
+
6483
+ float sd = signedDistance(v_uv + u_shadowOffset) + u_weight;
6484
+ vec4 shadow = vec4(u_shadowColor, smoothstep(-w - u_shadowBlur, w + u_shadowBlur, sd));
6485
+ color = mix(shadow, color, color.a);
6486
+
6487
+ color.a *= u_opacity;
6488
+ gl_FragColor = color;
6489
+ }
6490
+ `;
6491
+ class BMFontAtlas {
6492
+ build(layout, setting) {
6493
+ const info = this.info;
6494
+ const texWidth = info.common.scaleW;
6495
+ const texHeight = info.common.scaleH;
6496
+ const glyphs = layout.glyphs.filter((glyph)=>glyph.data.width * glyph.data.height > 0);
6497
+ const flipY = setting.flipY !== false;
6498
+ const position = new Float32Array(glyphs.length * 8);
6499
+ const uv = new Float32Array(glyphs.length * 8);
6500
+ const indice = new Uint16Array(glyphs.length * 6);
6501
+ for(let k = 0, i0 = 0, i1 = 0, i2 = 0, i = 0; i < glyphs.length; i++, k += 4){
6502
+ const glyph = glyphs[i];
6503
+ const bitmap = glyph.data;
6504
+ // bottom left position
6505
+ let x = glyph.position[0] + bitmap.xoffset;
6506
+ let y = glyph.position[1] + bitmap.yoffset;
6507
+ // quad size
6508
+ let w = bitmap.width;
6509
+ let h = bitmap.height;
6510
+ // BL
6511
+ position[i0++] = x;
6512
+ position[i0++] = y;
6513
+ // BR
6514
+ position[i0++] = x + w;
6515
+ position[i0++] = y;
6516
+ // TR
6517
+ position[i0++] = x + w;
6518
+ position[i0++] = y + h;
6519
+ // TL
6520
+ position[i0++] = x;
6521
+ position[i0++] = y + h;
6522
+ let bw = bitmap.x + bitmap.width;
6523
+ let bh = bitmap.y + bitmap.height;
6524
+ // top left position
6525
+ let u0 = bitmap.x / texWidth;
6526
+ let v0 = bitmap.y / texHeight;
6527
+ let u1 = bw / texWidth;
6528
+ let v1 = bh / texHeight;
6529
+ if (flipY) {
6530
+ v0 = 1 - v0;
6531
+ v1 = 1 - v1;
6532
+ }
6533
+ // BL
6534
+ uv[i1++] = u0;
6535
+ uv[i1++] = v0;
6536
+ // BR
6537
+ uv[i1++] = u1;
6538
+ uv[i1++] = v0;
6539
+ // TR
6540
+ uv[i1++] = u1;
6541
+ uv[i1++] = v1;
6542
+ // TL
6543
+ uv[i1++] = u0;
6544
+ uv[i1++] = v1;
6545
+ indice[i2++] = k + 0;
6546
+ indice[i2++] = k + 1;
6547
+ indice[i2++] = k + 2;
6548
+ indice[i2++] = k + 0;
6549
+ indice[i2++] = k + 2;
6550
+ indice[i2++] = k + 3;
6551
+ }
6552
+ return {
6553
+ position,
6554
+ uv,
6555
+ indice
6556
+ };
6557
+ }
6558
+ constructor({ info, texture, uniforms, ...props }){
6559
+ this.uniforms = {
6560
+ u_color: {
6561
+ value: new THREE.Color(0xffffff)
6562
+ },
6563
+ u_opacity: {
6564
+ value: 1
6565
+ },
6566
+ u_weight: {
6567
+ value: 0.2
6568
+ },
6569
+ u_strokeColor: {
6570
+ value: new THREE.Color(0xff0000)
6571
+ },
6572
+ u_strokeWidth: {
6573
+ value: 0
6574
+ },
6575
+ u_shadowColor: {
6576
+ value: new THREE.Color(0xff0000)
6577
+ },
6578
+ u_shadowBlur: {
6579
+ value: 0
6580
+ },
6581
+ u_shadowOffset: {
6582
+ value: new THREE.Vector2()
6583
+ },
6584
+ u_fontTexture: {
6585
+ value: null
6586
+ }
6587
+ };
6588
+ this.info = info;
6589
+ this.uniforms.u_fontTexture.value = texture;
6590
+ this.material = new THREE.ShaderMaterial(Object.assign({
6591
+ vertexShader: vert_MSDF,
6592
+ fragmentShader: frag_MSDF,
6593
+ transparent: true,
6594
+ depthWrite: false,
6595
+ blending: THREE.NormalBlending,
6596
+ uniforms: {
6597
+ ...this.uniforms,
6598
+ ...uniforms
6599
+ }
6600
+ }, props));
6601
+ }
6602
+ }
6603
+
5892
6604
  exports.AccumulativeShadows = AccumulativeShadows;
5893
6605
  exports.Animation = Animation;
5894
6606
  exports.AnimationCurve = AnimationCurve;
5895
6607
  exports.BINLoader = BINLoader;
6608
+ exports.BMFontAtlas = BMFontAtlas;
6609
+ exports.BMFontTextGeometry = BMFontTextGeometry;
6610
+ exports.BMFontTextLayout = BMFontTextLayout;
5896
6611
  exports.Box = Box;
5897
6612
  exports.BoxProjection = BoxProjection;
5898
6613
  exports.Center = Center;
@@ -5913,6 +6628,7 @@ exports.GLTFLoader = GLTFLoader;
5913
6628
  exports.HDRLoader = HDRLoader;
5914
6629
  exports.JSONLoader = JSONLoader;
5915
6630
  exports.KTX2Loader = KTX2Loader;
6631
+ exports.Label = Label;
5916
6632
  exports.Loader = Loader;
5917
6633
  exports.Logger = Logger;
5918
6634
  exports.ObjectInstance = ObjectInstance;