@rings-webgpu/core 1.0.18 → 1.0.20

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.
@@ -36926,6 +36926,8 @@ class OBJParser extends ParserBase {
36926
36926
  matLibs;
36927
36927
  geometrys;
36928
36928
  activeGeo;
36929
+ currentObjectName;
36930
+ currentMaterialName;
36929
36931
  facesMaterialsIndex;
36930
36932
  mtl;
36931
36933
  mtlUrl;
@@ -36934,26 +36936,35 @@ class OBJParser extends ParserBase {
36934
36936
  this.source_normals = [];
36935
36937
  this.source_tangents = [];
36936
36938
  this.source_textureCoords = [];
36939
+ this.currentObjectName = "default";
36940
+ this.currentMaterialName = "";
36937
36941
  this.matLibs = {};
36938
36942
  this.geometrys = {};
36943
+ this.activeGeo = void 0;
36939
36944
  this.textData = obj;
36940
36945
  await Promise.all([this.parserOBJ(), this.loadMTL()]);
36941
36946
  this.parser_mesh();
36942
36947
  return `null`;
36943
36948
  }
36944
36949
  applyVector2(fi, sourceData, destData) {
36945
- if (sourceData[fi] && sourceData[fi].length > 0) {
36950
+ if (fi >= 0 && sourceData[fi] && sourceData[fi].length > 0) {
36946
36951
  destData.push(sourceData[fi][0]);
36947
- destData.push(sourceData[fi][1]);
36952
+ destData.push(-sourceData[fi][1]);
36948
36953
  } else {
36949
36954
  destData.push(0);
36950
36955
  destData.push(0);
36951
36956
  }
36952
36957
  }
36953
36958
  applyVector3(fi, sourceData, destData) {
36954
- destData.push(sourceData[fi][0]);
36955
- destData.push(sourceData[fi][1]);
36956
- destData.push(sourceData[fi][2]);
36959
+ if (fi >= 0 && sourceData[fi] && sourceData[fi].length > 0) {
36960
+ destData.push(sourceData[fi][0]);
36961
+ destData.push(sourceData[fi][1]);
36962
+ destData.push(sourceData[fi][2]);
36963
+ } else {
36964
+ destData.push(0);
36965
+ destData.push(0);
36966
+ destData.push(0);
36967
+ }
36957
36968
  }
36958
36969
  applyVector4(fi, sourceData, destData) {
36959
36970
  destData.push(sourceData[fi][0]);
@@ -36961,12 +36972,76 @@ class OBJParser extends ParserBase {
36961
36972
  destData.push(sourceData[fi][2]);
36962
36973
  destData.push(sourceData[fi][3]);
36963
36974
  }
36975
+ /**
36976
+ * Parse UV index with support for negative indices (OBJ format)
36977
+ * Similar to Three.js parseUVIndex method
36978
+ */
36979
+ parseUVIndex(value, uvCount) {
36980
+ const index = parseInt(value, 10);
36981
+ if (index >= 0) {
36982
+ return index - 1;
36983
+ } else {
36984
+ return uvCount + index;
36985
+ }
36986
+ }
36987
+ /**
36988
+ * Parse vertex index with support for negative indices (OBJ format)
36989
+ */
36990
+ parseVertexIndex(value, vertexCount) {
36991
+ const index = parseInt(value, 10);
36992
+ if (index >= 0) {
36993
+ return index - 1;
36994
+ } else {
36995
+ return vertexCount + index;
36996
+ }
36997
+ }
36998
+ /**
36999
+ * Parse normal index with support for negative indices (OBJ format)
37000
+ */
37001
+ parseNormalIndex(value, normalCount) {
37002
+ const index = parseInt(value, 10);
37003
+ if (index >= 0) {
37004
+ return index - 1;
37005
+ } else {
37006
+ return normalCount + index;
37007
+ }
37008
+ }
37009
+ /**
37010
+ * Calculate face normal from three vertices using cross product
37011
+ * Similar to Three.js addFaceNormal method
37012
+ */
37013
+ calculateFaceNormal(v0, v1, v2) {
37014
+ const edge1 = [
37015
+ v1[0] - v0[0],
37016
+ v1[1] - v0[1],
37017
+ v1[2] - v0[2]
37018
+ ];
37019
+ const edge2 = [
37020
+ v2[0] - v0[0],
37021
+ v2[1] - v0[1],
37022
+ v2[2] - v0[2]
37023
+ ];
37024
+ const normal = [
37025
+ edge1[1] * edge2[2] - edge1[2] * edge2[1],
37026
+ edge1[2] * edge2[0] - edge1[0] * edge2[2],
37027
+ edge1[0] * edge2[1] - edge1[1] * edge2[0]
37028
+ ];
37029
+ const length = Math.sqrt(
37030
+ normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]
37031
+ );
37032
+ if (length > 0) {
37033
+ normal[0] /= length;
37034
+ normal[1] /= length;
37035
+ normal[2] /= length;
37036
+ }
37037
+ return normal;
37038
+ }
36964
37039
  async loadMTL() {
36965
37040
  let fileLoad = new FileLoader();
36966
37041
  let sourceData = await fileLoad.loadTxt(this.baseUrl + this.mtlUrl);
36967
37042
  let sourceStr = sourceData[`data`];
36968
37043
  let mat;
36969
- let str = sourceStr.split("\r\n");
37044
+ let str = sourceStr.split(/\r?\n/);
36970
37045
  for (let i = 0; i < str.length; i++) {
36971
37046
  let line = str[i];
36972
37047
  var commentStart = line.indexOf("#");
@@ -37017,20 +37092,41 @@ class OBJParser extends ParserBase {
37017
37092
  }
37018
37093
  async load_textures() {
37019
37094
  }
37095
+ getGeometryKey(objectName, materialName) {
37096
+ const objName = (objectName ?? this.currentObjectName ?? "default") || "default";
37097
+ const matName = materialName ?? this.currentMaterialName ?? "";
37098
+ const matKey = matName.length > 0 ? matName : "default";
37099
+ return `${objName}::${matKey}`;
37100
+ }
37101
+ ensureActiveGeo(objectName, materialName) {
37102
+ const objName = (objectName ?? this.currentObjectName ?? "default") || "default";
37103
+ const matName = materialName ?? this.currentMaterialName ?? "";
37104
+ const geoKey = this.getGeometryKey(objName, matName);
37105
+ if (!this.geometrys[geoKey]) {
37106
+ this.geometrys[geoKey] = {
37107
+ type: objName,
37108
+ name: geoKey,
37109
+ source_mat: matName,
37110
+ source_faces: []
37111
+ };
37112
+ } else if (this.geometrys[geoKey].source_mat !== matName) {
37113
+ this.geometrys[geoKey].source_mat = matName;
37114
+ }
37115
+ this.activeGeo = this.geometrys[geoKey];
37116
+ }
37020
37117
  parserLine(line) {
37021
37118
  var commentStart = line.indexOf("#");
37022
37119
  if (commentStart != -1) {
37023
37120
  if (line.indexOf(`# object`) != -1) {
37024
- var splitedLine = line.split(/\s+/);
37025
- let type = splitedLine[1];
37026
- let geoName = splitedLine[2];
37027
- this.activeGeo = {
37028
- type,
37029
- name: geoName[1],
37030
- source_mat: ``,
37031
- source_faces: []
37032
- };
37033
- this.geometrys[geoName] = this.activeGeo;
37121
+ const commentParts = line.split(/\s+/);
37122
+ const type = commentParts[1] || "default";
37123
+ const geoName = commentParts[2] || "default";
37124
+ this.currentObjectName = geoName;
37125
+ this.activeGeo = void 0;
37126
+ this.ensureActiveGeo(geoName, this.currentMaterialName);
37127
+ if (this.activeGeo) {
37128
+ this.activeGeo.type = type;
37129
+ }
37034
37130
  }
37035
37131
  line = line.substring(0, commentStart);
37036
37132
  }
@@ -37065,33 +37161,30 @@ class OBJParser extends ParserBase {
37065
37161
  normal: []
37066
37162
  };
37067
37163
  for (var i = 1; i < splitedLine.length; ++i) {
37068
- var dIndex = splitedLine[i].indexOf("//");
37069
- var splitedFaceIndices = splitedLine[i].split(/\W+/);
37070
- if (dIndex > 0) {
37071
- face.indices.push(splitedFaceIndices[0]);
37072
- face.normal.push(splitedFaceIndices[1]);
37073
- } else {
37074
- if (splitedFaceIndices.length === 1) {
37075
- face.indices.push(splitedFaceIndices[0]);
37076
- } else if (splitedFaceIndices.length === 2) {
37077
- face.indices.push(splitedFaceIndices[0]);
37078
- face.texture.push(splitedFaceIndices[1]);
37079
- } else if (splitedFaceIndices.length === 3) {
37080
- face.indices.push(splitedFaceIndices[0]);
37081
- face.texture.push(splitedFaceIndices[1]);
37082
- face.normal.push(splitedFaceIndices[2]);
37083
- }
37164
+ var vertexStr = splitedLine[i];
37165
+ if (vertexStr.length === 0) continue;
37166
+ var vertexParts = vertexStr.split("/");
37167
+ var positionIndex = vertexParts[0] || "";
37168
+ var textureIndex = vertexParts.length >= 2 ? vertexParts[1] || "" : "";
37169
+ var normalIndex = vertexParts.length >= 3 ? vertexParts[2] || "" : "";
37170
+ if (positionIndex.length === 0) {
37171
+ continue;
37084
37172
  }
37173
+ face.indices.push(positionIndex);
37174
+ face.texture.push(textureIndex);
37175
+ face.normal.push(normalIndex);
37085
37176
  }
37177
+ this.ensureActiveGeo();
37086
37178
  this.activeGeo.source_faces.push(face);
37087
37179
  } else if (splitedLine[0] === "usemtl") {
37088
- this.activeGeo.source_mat = splitedLine[1];
37180
+ this.currentMaterialName = splitedLine[1] || "";
37181
+ this.ensureActiveGeo(this.currentObjectName, this.currentMaterialName);
37089
37182
  } else if (splitedLine[0] === `mtllib`) {
37090
37183
  this.mtlUrl = splitedLine[1];
37091
37184
  }
37092
37185
  }
37093
37186
  async parserOBJ() {
37094
- let str = this.textData.split("\r\n");
37187
+ let str = this.textData.split(/\r?\n/);
37095
37188
  for (let i = 0; i < str.length; i++) {
37096
37189
  const element = str[i];
37097
37190
  this.parserLine(element);
@@ -37108,44 +37201,138 @@ class OBJParser extends ParserBase {
37108
37201
  geoData.uv_arr = [];
37109
37202
  geoData.indeice_arr = [];
37110
37203
  let index = 0;
37204
+ const vertexCount = this.source_vertices.length;
37205
+ const normalCount = this.source_normals.length;
37206
+ const uvCount = this.source_textureCoords.length;
37111
37207
  for (let i = 0; i < geoData.source_faces.length; i++) {
37112
37208
  const face = geoData.source_faces[i];
37113
- let f0 = parseInt(face.indices[0]) - 1;
37114
- let f1 = parseInt(face.indices[1]) - 1;
37115
- let f2 = parseInt(face.indices[2]) - 1;
37116
- let n0 = parseInt(face.normal[0]) - 1;
37117
- let n1 = parseInt(face.normal[1]) - 1;
37118
- let n2 = parseInt(face.normal[2]) - 1;
37119
- let u0 = parseInt(face.texture[0]) - 1;
37120
- let u1 = parseInt(face.texture[1]) - 1;
37121
- let u2 = parseInt(face.texture[2]) - 1;
37209
+ let f0 = this.parseVertexIndex(face.indices[0], vertexCount);
37210
+ let f1 = this.parseVertexIndex(face.indices[1], vertexCount);
37211
+ let f2 = this.parseVertexIndex(face.indices[2], vertexCount);
37212
+ const getNormalIndex = (value) => {
37213
+ if (!value || value.length === 0) {
37214
+ return -1;
37215
+ }
37216
+ const parsed = this.parseNormalIndex(value, normalCount);
37217
+ return Number.isFinite(parsed) && parsed >= 0 && parsed < normalCount ? parsed : -1;
37218
+ };
37219
+ const getUVIndex = (value) => {
37220
+ if (!value || value.length === 0) {
37221
+ return -1;
37222
+ }
37223
+ const parsed = this.parseUVIndex(value, uvCount);
37224
+ return Number.isFinite(parsed) && parsed >= 0 && parsed < uvCount ? parsed : -1;
37225
+ };
37226
+ let n0 = getNormalIndex(face.normal[0]);
37227
+ let n1 = getNormalIndex(face.normal[1]);
37228
+ let n2 = getNormalIndex(face.normal[2]);
37229
+ const needsFaceNormal = n0 < 0 || n1 < 0 || n2 < 0;
37230
+ let faceNormal = null;
37231
+ if (needsFaceNormal && f0 >= 0 && f1 >= 0 && f2 >= 0 && this.source_vertices[f0] && this.source_vertices[f1] && this.source_vertices[f2]) {
37232
+ faceNormal = this.calculateFaceNormal(
37233
+ this.source_vertices[f0],
37234
+ this.source_vertices[f1],
37235
+ this.source_vertices[f2]
37236
+ );
37237
+ }
37238
+ let u0 = getUVIndex(face.texture[0]);
37239
+ let u1 = getUVIndex(face.texture[1]);
37240
+ let u2 = getUVIndex(face.texture[2]);
37122
37241
  this.applyVector3(f0, this.source_vertices, geoData.vertex_arr);
37123
- this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37124
- this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37242
+ if (n0 >= 0) {
37243
+ this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37244
+ } else if (faceNormal) {
37245
+ geoData.normal_arr.push(faceNormal[0], faceNormal[1], faceNormal[2]);
37246
+ } else {
37247
+ geoData.normal_arr.push(0, 0, 0);
37248
+ }
37249
+ if (u0 >= 0) {
37250
+ this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37251
+ } else {
37252
+ geoData.uv_arr.push(0, 0);
37253
+ }
37125
37254
  geoData.indeice_arr[index] = index++;
37126
37255
  this.applyVector3(f1, this.source_vertices, geoData.vertex_arr);
37127
- this.applyVector3(n1, this.source_normals, geoData.normal_arr);
37128
- this.applyVector2(u1, this.source_textureCoords, geoData.uv_arr);
37256
+ if (n1 >= 0) {
37257
+ this.applyVector3(n1, this.source_normals, geoData.normal_arr);
37258
+ } else if (faceNormal) {
37259
+ geoData.normal_arr.push(faceNormal[0], faceNormal[1], faceNormal[2]);
37260
+ } else {
37261
+ geoData.normal_arr.push(0, 0, 0);
37262
+ }
37263
+ if (u1 >= 0) {
37264
+ this.applyVector2(u1, this.source_textureCoords, geoData.uv_arr);
37265
+ } else {
37266
+ geoData.uv_arr.push(0, 0);
37267
+ }
37129
37268
  geoData.indeice_arr[index] = index++;
37130
37269
  this.applyVector3(f2, this.source_vertices, geoData.vertex_arr);
37131
- this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37132
- this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37270
+ if (n2 >= 0) {
37271
+ this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37272
+ } else if (faceNormal) {
37273
+ geoData.normal_arr.push(faceNormal[0], faceNormal[1], faceNormal[2]);
37274
+ } else {
37275
+ geoData.normal_arr.push(0, 0, 0);
37276
+ }
37277
+ if (u2 >= 0) {
37278
+ this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37279
+ } else {
37280
+ geoData.uv_arr.push(0, 0);
37281
+ }
37133
37282
  geoData.indeice_arr[index] = index++;
37134
37283
  if (face.indices.length > 3) {
37135
- let f3 = parseInt(face.indices[3]) - 1;
37136
- let n3 = parseInt(face.normal[3]) - 1;
37137
- let u3 = parseInt(face.texture[3]) - 1;
37284
+ let f3 = this.parseVertexIndex(face.indices[3], vertexCount);
37285
+ let n3 = getNormalIndex(face.normal[3]);
37286
+ let u3 = getUVIndex(face.texture[3]);
37287
+ let faceNormal2 = null;
37288
+ if ((n0 < 0 || n2 < 0 || n3 < 0) && f0 >= 0 && f2 >= 0 && f3 >= 0 && this.source_vertices[f0] && this.source_vertices[f2] && this.source_vertices[f3]) {
37289
+ faceNormal2 = this.calculateFaceNormal(
37290
+ this.source_vertices[f0],
37291
+ this.source_vertices[f2],
37292
+ this.source_vertices[f3]
37293
+ );
37294
+ }
37138
37295
  this.applyVector3(f0, this.source_vertices, geoData.vertex_arr);
37139
- this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37140
- this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37296
+ if (n0 >= 0) {
37297
+ this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37298
+ } else if (faceNormal2) {
37299
+ geoData.normal_arr.push(faceNormal2[0], faceNormal2[1], faceNormal2[2]);
37300
+ } else {
37301
+ geoData.normal_arr.push(0, 0, 0);
37302
+ }
37303
+ if (u0 >= 0) {
37304
+ this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37305
+ } else {
37306
+ geoData.uv_arr.push(0, 0);
37307
+ }
37141
37308
  geoData.indeice_arr[index] = index++;
37142
37309
  this.applyVector3(f2, this.source_vertices, geoData.vertex_arr);
37143
- this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37144
- this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37310
+ if (n2 >= 0) {
37311
+ this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37312
+ } else if (faceNormal2) {
37313
+ geoData.normal_arr.push(faceNormal2[0], faceNormal2[1], faceNormal2[2]);
37314
+ } else {
37315
+ geoData.normal_arr.push(0, 0, 0);
37316
+ }
37317
+ if (u2 >= 0) {
37318
+ this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37319
+ } else {
37320
+ geoData.uv_arr.push(0, 0);
37321
+ }
37145
37322
  geoData.indeice_arr[index] = index++;
37146
37323
  this.applyVector3(f3, this.source_vertices, geoData.vertex_arr);
37147
- this.applyVector3(n3, this.source_normals, geoData.normal_arr);
37148
- this.applyVector2(u3, this.source_textureCoords, geoData.uv_arr);
37324
+ if (n3 >= 0) {
37325
+ this.applyVector3(n3, this.source_normals, geoData.normal_arr);
37326
+ } else if (faceNormal2) {
37327
+ geoData.normal_arr.push(faceNormal2[0], faceNormal2[1], faceNormal2[2]);
37328
+ } else {
37329
+ geoData.normal_arr.push(0, 0, 0);
37330
+ }
37331
+ if (u3 >= 0) {
37332
+ this.applyVector2(u3, this.source_textureCoords, geoData.uv_arr);
37333
+ } else {
37334
+ geoData.uv_arr.push(0, 0);
37335
+ }
37149
37336
  geoData.indeice_arr[index] = index++;
37150
37337
  }
37151
37338
  }
@@ -37177,10 +37364,17 @@ class OBJParser extends ParserBase {
37177
37364
  topology: 0
37178
37365
  });
37179
37366
  let mat = new LitMaterial();
37180
- let matData = this.matLibs[geoData.source_mat];
37181
- mat.baseMap = Engine3D.res.getTexture(
37182
- StringUtil.normalizePath(this.baseUrl + matData.map_Kd)
37183
- );
37367
+ const matName = geoData.source_mat;
37368
+ const matData = matName ? this.matLibs[matName] : void 0;
37369
+ if (matData && matData.map_Kd) {
37370
+ const texturePath = StringUtil.normalizePath(
37371
+ this.baseUrl + matData.map_Kd
37372
+ );
37373
+ const texture = Engine3D.res.getTexture(texturePath);
37374
+ if (texture) {
37375
+ mat.baseMap = texture;
37376
+ }
37377
+ }
37184
37378
  let obj = new Object3D();
37185
37379
  let mr = obj.addComponent(MeshRenderer);
37186
37380
  mr.geometry = geo;
@@ -41057,7 +41251,7 @@ class PostProcessingComponent extends ComponentBase {
41057
41251
  }
41058
41252
  }
41059
41253
 
41060
- const version = "1.0.18";
41254
+ const version = "1.0.19";
41061
41255
 
41062
41256
  class Engine3D {
41063
41257
  /**
@@ -61382,12 +61576,16 @@ function toggleTiles(tile, renderer) {
61382
61576
  }
61383
61577
 
61384
61578
  class BoundingVolume {
61579
+ static s_tmpMatrix = null;
61385
61580
  _type;
61386
61581
  _data;
61387
61582
  _box;
61388
61583
  _sphere;
61389
61584
  _matrix;
61390
61585
  constructor(data) {
61586
+ if (!BoundingVolume.s_tmpMatrix) {
61587
+ BoundingVolume.s_tmpMatrix = new Matrix4();
61588
+ }
61391
61589
  this._data = data;
61392
61590
  if (data.box) {
61393
61591
  this._type = "box";
@@ -61511,7 +61709,8 @@ class BoundingVolume {
61511
61709
  }
61512
61710
  return target;
61513
61711
  } else if (this._box) {
61514
- const worldMatrix = new Matrix4();
61712
+ const worldMatrix = BoundingVolume.s_tmpMatrix;
61713
+ worldMatrix.identity();
61515
61714
  if (this._matrix) {
61516
61715
  if (parentTransform) {
61517
61716
  worldMatrix.multiplyMatrices(parentTransform, this._matrix);