@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.
@@ -36933,6 +36933,8 @@ else if (typeof exports === 'object')
36933
36933
  matLibs;
36934
36934
  geometrys;
36935
36935
  activeGeo;
36936
+ currentObjectName;
36937
+ currentMaterialName;
36936
36938
  facesMaterialsIndex;
36937
36939
  mtl;
36938
36940
  mtlUrl;
@@ -36941,26 +36943,35 @@ else if (typeof exports === 'object')
36941
36943
  this.source_normals = [];
36942
36944
  this.source_tangents = [];
36943
36945
  this.source_textureCoords = [];
36946
+ this.currentObjectName = "default";
36947
+ this.currentMaterialName = "";
36944
36948
  this.matLibs = {};
36945
36949
  this.geometrys = {};
36950
+ this.activeGeo = void 0;
36946
36951
  this.textData = obj;
36947
36952
  await Promise.all([this.parserOBJ(), this.loadMTL()]);
36948
36953
  this.parser_mesh();
36949
36954
  return `null`;
36950
36955
  }
36951
36956
  applyVector2(fi, sourceData, destData) {
36952
- if (sourceData[fi] && sourceData[fi].length > 0) {
36957
+ if (fi >= 0 && sourceData[fi] && sourceData[fi].length > 0) {
36953
36958
  destData.push(sourceData[fi][0]);
36954
- destData.push(sourceData[fi][1]);
36959
+ destData.push(-sourceData[fi][1]);
36955
36960
  } else {
36956
36961
  destData.push(0);
36957
36962
  destData.push(0);
36958
36963
  }
36959
36964
  }
36960
36965
  applyVector3(fi, sourceData, destData) {
36961
- destData.push(sourceData[fi][0]);
36962
- destData.push(sourceData[fi][1]);
36963
- destData.push(sourceData[fi][2]);
36966
+ if (fi >= 0 && sourceData[fi] && sourceData[fi].length > 0) {
36967
+ destData.push(sourceData[fi][0]);
36968
+ destData.push(sourceData[fi][1]);
36969
+ destData.push(sourceData[fi][2]);
36970
+ } else {
36971
+ destData.push(0);
36972
+ destData.push(0);
36973
+ destData.push(0);
36974
+ }
36964
36975
  }
36965
36976
  applyVector4(fi, sourceData, destData) {
36966
36977
  destData.push(sourceData[fi][0]);
@@ -36968,12 +36979,76 @@ else if (typeof exports === 'object')
36968
36979
  destData.push(sourceData[fi][2]);
36969
36980
  destData.push(sourceData[fi][3]);
36970
36981
  }
36982
+ /**
36983
+ * Parse UV index with support for negative indices (OBJ format)
36984
+ * Similar to Three.js parseUVIndex method
36985
+ */
36986
+ parseUVIndex(value, uvCount) {
36987
+ const index = parseInt(value, 10);
36988
+ if (index >= 0) {
36989
+ return index - 1;
36990
+ } else {
36991
+ return uvCount + index;
36992
+ }
36993
+ }
36994
+ /**
36995
+ * Parse vertex index with support for negative indices (OBJ format)
36996
+ */
36997
+ parseVertexIndex(value, vertexCount) {
36998
+ const index = parseInt(value, 10);
36999
+ if (index >= 0) {
37000
+ return index - 1;
37001
+ } else {
37002
+ return vertexCount + index;
37003
+ }
37004
+ }
37005
+ /**
37006
+ * Parse normal index with support for negative indices (OBJ format)
37007
+ */
37008
+ parseNormalIndex(value, normalCount) {
37009
+ const index = parseInt(value, 10);
37010
+ if (index >= 0) {
37011
+ return index - 1;
37012
+ } else {
37013
+ return normalCount + index;
37014
+ }
37015
+ }
37016
+ /**
37017
+ * Calculate face normal from three vertices using cross product
37018
+ * Similar to Three.js addFaceNormal method
37019
+ */
37020
+ calculateFaceNormal(v0, v1, v2) {
37021
+ const edge1 = [
37022
+ v1[0] - v0[0],
37023
+ v1[1] - v0[1],
37024
+ v1[2] - v0[2]
37025
+ ];
37026
+ const edge2 = [
37027
+ v2[0] - v0[0],
37028
+ v2[1] - v0[1],
37029
+ v2[2] - v0[2]
37030
+ ];
37031
+ const normal = [
37032
+ edge1[1] * edge2[2] - edge1[2] * edge2[1],
37033
+ edge1[2] * edge2[0] - edge1[0] * edge2[2],
37034
+ edge1[0] * edge2[1] - edge1[1] * edge2[0]
37035
+ ];
37036
+ const length = Math.sqrt(
37037
+ normal[0] * normal[0] + normal[1] * normal[1] + normal[2] * normal[2]
37038
+ );
37039
+ if (length > 0) {
37040
+ normal[0] /= length;
37041
+ normal[1] /= length;
37042
+ normal[2] /= length;
37043
+ }
37044
+ return normal;
37045
+ }
36971
37046
  async loadMTL() {
36972
37047
  let fileLoad = new FileLoader();
36973
37048
  let sourceData = await fileLoad.loadTxt(this.baseUrl + this.mtlUrl);
36974
37049
  let sourceStr = sourceData[`data`];
36975
37050
  let mat;
36976
- let str = sourceStr.split("\r\n");
37051
+ let str = sourceStr.split(/\r?\n/);
36977
37052
  for (let i = 0; i < str.length; i++) {
36978
37053
  let line = str[i];
36979
37054
  var commentStart = line.indexOf("#");
@@ -37024,20 +37099,41 @@ else if (typeof exports === 'object')
37024
37099
  }
37025
37100
  async load_textures() {
37026
37101
  }
37102
+ getGeometryKey(objectName, materialName) {
37103
+ const objName = (objectName ?? this.currentObjectName ?? "default") || "default";
37104
+ const matName = materialName ?? this.currentMaterialName ?? "";
37105
+ const matKey = matName.length > 0 ? matName : "default";
37106
+ return `${objName}::${matKey}`;
37107
+ }
37108
+ ensureActiveGeo(objectName, materialName) {
37109
+ const objName = (objectName ?? this.currentObjectName ?? "default") || "default";
37110
+ const matName = materialName ?? this.currentMaterialName ?? "";
37111
+ const geoKey = this.getGeometryKey(objName, matName);
37112
+ if (!this.geometrys[geoKey]) {
37113
+ this.geometrys[geoKey] = {
37114
+ type: objName,
37115
+ name: geoKey,
37116
+ source_mat: matName,
37117
+ source_faces: []
37118
+ };
37119
+ } else if (this.geometrys[geoKey].source_mat !== matName) {
37120
+ this.geometrys[geoKey].source_mat = matName;
37121
+ }
37122
+ this.activeGeo = this.geometrys[geoKey];
37123
+ }
37027
37124
  parserLine(line) {
37028
37125
  var commentStart = line.indexOf("#");
37029
37126
  if (commentStart != -1) {
37030
37127
  if (line.indexOf(`# object`) != -1) {
37031
- var splitedLine = line.split(/\s+/);
37032
- let type = splitedLine[1];
37033
- let geoName = splitedLine[2];
37034
- this.activeGeo = {
37035
- type,
37036
- name: geoName[1],
37037
- source_mat: ``,
37038
- source_faces: []
37039
- };
37040
- this.geometrys[geoName] = this.activeGeo;
37128
+ const commentParts = line.split(/\s+/);
37129
+ const type = commentParts[1] || "default";
37130
+ const geoName = commentParts[2] || "default";
37131
+ this.currentObjectName = geoName;
37132
+ this.activeGeo = void 0;
37133
+ this.ensureActiveGeo(geoName, this.currentMaterialName);
37134
+ if (this.activeGeo) {
37135
+ this.activeGeo.type = type;
37136
+ }
37041
37137
  }
37042
37138
  line = line.substring(0, commentStart);
37043
37139
  }
@@ -37072,33 +37168,30 @@ else if (typeof exports === 'object')
37072
37168
  normal: []
37073
37169
  };
37074
37170
  for (var i = 1; i < splitedLine.length; ++i) {
37075
- var dIndex = splitedLine[i].indexOf("//");
37076
- var splitedFaceIndices = splitedLine[i].split(/\W+/);
37077
- if (dIndex > 0) {
37078
- face.indices.push(splitedFaceIndices[0]);
37079
- face.normal.push(splitedFaceIndices[1]);
37080
- } else {
37081
- if (splitedFaceIndices.length === 1) {
37082
- face.indices.push(splitedFaceIndices[0]);
37083
- } else if (splitedFaceIndices.length === 2) {
37084
- face.indices.push(splitedFaceIndices[0]);
37085
- face.texture.push(splitedFaceIndices[1]);
37086
- } else if (splitedFaceIndices.length === 3) {
37087
- face.indices.push(splitedFaceIndices[0]);
37088
- face.texture.push(splitedFaceIndices[1]);
37089
- face.normal.push(splitedFaceIndices[2]);
37090
- }
37171
+ var vertexStr = splitedLine[i];
37172
+ if (vertexStr.length === 0) continue;
37173
+ var vertexParts = vertexStr.split("/");
37174
+ var positionIndex = vertexParts[0] || "";
37175
+ var textureIndex = vertexParts.length >= 2 ? vertexParts[1] || "" : "";
37176
+ var normalIndex = vertexParts.length >= 3 ? vertexParts[2] || "" : "";
37177
+ if (positionIndex.length === 0) {
37178
+ continue;
37091
37179
  }
37180
+ face.indices.push(positionIndex);
37181
+ face.texture.push(textureIndex);
37182
+ face.normal.push(normalIndex);
37092
37183
  }
37184
+ this.ensureActiveGeo();
37093
37185
  this.activeGeo.source_faces.push(face);
37094
37186
  } else if (splitedLine[0] === "usemtl") {
37095
- this.activeGeo.source_mat = splitedLine[1];
37187
+ this.currentMaterialName = splitedLine[1] || "";
37188
+ this.ensureActiveGeo(this.currentObjectName, this.currentMaterialName);
37096
37189
  } else if (splitedLine[0] === `mtllib`) {
37097
37190
  this.mtlUrl = splitedLine[1];
37098
37191
  }
37099
37192
  }
37100
37193
  async parserOBJ() {
37101
- let str = this.textData.split("\r\n");
37194
+ let str = this.textData.split(/\r?\n/);
37102
37195
  for (let i = 0; i < str.length; i++) {
37103
37196
  const element = str[i];
37104
37197
  this.parserLine(element);
@@ -37115,44 +37208,138 @@ else if (typeof exports === 'object')
37115
37208
  geoData.uv_arr = [];
37116
37209
  geoData.indeice_arr = [];
37117
37210
  let index = 0;
37211
+ const vertexCount = this.source_vertices.length;
37212
+ const normalCount = this.source_normals.length;
37213
+ const uvCount = this.source_textureCoords.length;
37118
37214
  for (let i = 0; i < geoData.source_faces.length; i++) {
37119
37215
  const face = geoData.source_faces[i];
37120
- let f0 = parseInt(face.indices[0]) - 1;
37121
- let f1 = parseInt(face.indices[1]) - 1;
37122
- let f2 = parseInt(face.indices[2]) - 1;
37123
- let n0 = parseInt(face.normal[0]) - 1;
37124
- let n1 = parseInt(face.normal[1]) - 1;
37125
- let n2 = parseInt(face.normal[2]) - 1;
37126
- let u0 = parseInt(face.texture[0]) - 1;
37127
- let u1 = parseInt(face.texture[1]) - 1;
37128
- let u2 = parseInt(face.texture[2]) - 1;
37216
+ let f0 = this.parseVertexIndex(face.indices[0], vertexCount);
37217
+ let f1 = this.parseVertexIndex(face.indices[1], vertexCount);
37218
+ let f2 = this.parseVertexIndex(face.indices[2], vertexCount);
37219
+ const getNormalIndex = (value) => {
37220
+ if (!value || value.length === 0) {
37221
+ return -1;
37222
+ }
37223
+ const parsed = this.parseNormalIndex(value, normalCount);
37224
+ return Number.isFinite(parsed) && parsed >= 0 && parsed < normalCount ? parsed : -1;
37225
+ };
37226
+ const getUVIndex = (value) => {
37227
+ if (!value || value.length === 0) {
37228
+ return -1;
37229
+ }
37230
+ const parsed = this.parseUVIndex(value, uvCount);
37231
+ return Number.isFinite(parsed) && parsed >= 0 && parsed < uvCount ? parsed : -1;
37232
+ };
37233
+ let n0 = getNormalIndex(face.normal[0]);
37234
+ let n1 = getNormalIndex(face.normal[1]);
37235
+ let n2 = getNormalIndex(face.normal[2]);
37236
+ const needsFaceNormal = n0 < 0 || n1 < 0 || n2 < 0;
37237
+ let faceNormal = null;
37238
+ if (needsFaceNormal && f0 >= 0 && f1 >= 0 && f2 >= 0 && this.source_vertices[f0] && this.source_vertices[f1] && this.source_vertices[f2]) {
37239
+ faceNormal = this.calculateFaceNormal(
37240
+ this.source_vertices[f0],
37241
+ this.source_vertices[f1],
37242
+ this.source_vertices[f2]
37243
+ );
37244
+ }
37245
+ let u0 = getUVIndex(face.texture[0]);
37246
+ let u1 = getUVIndex(face.texture[1]);
37247
+ let u2 = getUVIndex(face.texture[2]);
37129
37248
  this.applyVector3(f0, this.source_vertices, geoData.vertex_arr);
37130
- this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37131
- this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37249
+ if (n0 >= 0) {
37250
+ this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37251
+ } else if (faceNormal) {
37252
+ geoData.normal_arr.push(faceNormal[0], faceNormal[1], faceNormal[2]);
37253
+ } else {
37254
+ geoData.normal_arr.push(0, 0, 0);
37255
+ }
37256
+ if (u0 >= 0) {
37257
+ this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37258
+ } else {
37259
+ geoData.uv_arr.push(0, 0);
37260
+ }
37132
37261
  geoData.indeice_arr[index] = index++;
37133
37262
  this.applyVector3(f1, this.source_vertices, geoData.vertex_arr);
37134
- this.applyVector3(n1, this.source_normals, geoData.normal_arr);
37135
- this.applyVector2(u1, this.source_textureCoords, geoData.uv_arr);
37263
+ if (n1 >= 0) {
37264
+ this.applyVector3(n1, this.source_normals, geoData.normal_arr);
37265
+ } else if (faceNormal) {
37266
+ geoData.normal_arr.push(faceNormal[0], faceNormal[1], faceNormal[2]);
37267
+ } else {
37268
+ geoData.normal_arr.push(0, 0, 0);
37269
+ }
37270
+ if (u1 >= 0) {
37271
+ this.applyVector2(u1, this.source_textureCoords, geoData.uv_arr);
37272
+ } else {
37273
+ geoData.uv_arr.push(0, 0);
37274
+ }
37136
37275
  geoData.indeice_arr[index] = index++;
37137
37276
  this.applyVector3(f2, this.source_vertices, geoData.vertex_arr);
37138
- this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37139
- this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37277
+ if (n2 >= 0) {
37278
+ this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37279
+ } else if (faceNormal) {
37280
+ geoData.normal_arr.push(faceNormal[0], faceNormal[1], faceNormal[2]);
37281
+ } else {
37282
+ geoData.normal_arr.push(0, 0, 0);
37283
+ }
37284
+ if (u2 >= 0) {
37285
+ this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37286
+ } else {
37287
+ geoData.uv_arr.push(0, 0);
37288
+ }
37140
37289
  geoData.indeice_arr[index] = index++;
37141
37290
  if (face.indices.length > 3) {
37142
- let f3 = parseInt(face.indices[3]) - 1;
37143
- let n3 = parseInt(face.normal[3]) - 1;
37144
- let u3 = parseInt(face.texture[3]) - 1;
37291
+ let f3 = this.parseVertexIndex(face.indices[3], vertexCount);
37292
+ let n3 = getNormalIndex(face.normal[3]);
37293
+ let u3 = getUVIndex(face.texture[3]);
37294
+ let faceNormal2 = null;
37295
+ 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]) {
37296
+ faceNormal2 = this.calculateFaceNormal(
37297
+ this.source_vertices[f0],
37298
+ this.source_vertices[f2],
37299
+ this.source_vertices[f3]
37300
+ );
37301
+ }
37145
37302
  this.applyVector3(f0, this.source_vertices, geoData.vertex_arr);
37146
- this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37147
- this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37303
+ if (n0 >= 0) {
37304
+ this.applyVector3(n0, this.source_normals, geoData.normal_arr);
37305
+ } else if (faceNormal2) {
37306
+ geoData.normal_arr.push(faceNormal2[0], faceNormal2[1], faceNormal2[2]);
37307
+ } else {
37308
+ geoData.normal_arr.push(0, 0, 0);
37309
+ }
37310
+ if (u0 >= 0) {
37311
+ this.applyVector2(u0, this.source_textureCoords, geoData.uv_arr);
37312
+ } else {
37313
+ geoData.uv_arr.push(0, 0);
37314
+ }
37148
37315
  geoData.indeice_arr[index] = index++;
37149
37316
  this.applyVector3(f2, this.source_vertices, geoData.vertex_arr);
37150
- this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37151
- this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37317
+ if (n2 >= 0) {
37318
+ this.applyVector3(n2, this.source_normals, geoData.normal_arr);
37319
+ } else if (faceNormal2) {
37320
+ geoData.normal_arr.push(faceNormal2[0], faceNormal2[1], faceNormal2[2]);
37321
+ } else {
37322
+ geoData.normal_arr.push(0, 0, 0);
37323
+ }
37324
+ if (u2 >= 0) {
37325
+ this.applyVector2(u2, this.source_textureCoords, geoData.uv_arr);
37326
+ } else {
37327
+ geoData.uv_arr.push(0, 0);
37328
+ }
37152
37329
  geoData.indeice_arr[index] = index++;
37153
37330
  this.applyVector3(f3, this.source_vertices, geoData.vertex_arr);
37154
- this.applyVector3(n3, this.source_normals, geoData.normal_arr);
37155
- this.applyVector2(u3, this.source_textureCoords, geoData.uv_arr);
37331
+ if (n3 >= 0) {
37332
+ this.applyVector3(n3, this.source_normals, geoData.normal_arr);
37333
+ } else if (faceNormal2) {
37334
+ geoData.normal_arr.push(faceNormal2[0], faceNormal2[1], faceNormal2[2]);
37335
+ } else {
37336
+ geoData.normal_arr.push(0, 0, 0);
37337
+ }
37338
+ if (u3 >= 0) {
37339
+ this.applyVector2(u3, this.source_textureCoords, geoData.uv_arr);
37340
+ } else {
37341
+ geoData.uv_arr.push(0, 0);
37342
+ }
37156
37343
  geoData.indeice_arr[index] = index++;
37157
37344
  }
37158
37345
  }
@@ -37184,10 +37371,17 @@ else if (typeof exports === 'object')
37184
37371
  topology: 0
37185
37372
  });
37186
37373
  let mat = new LitMaterial();
37187
- let matData = this.matLibs[geoData.source_mat];
37188
- mat.baseMap = Engine3D.res.getTexture(
37189
- StringUtil.normalizePath(this.baseUrl + matData.map_Kd)
37190
- );
37374
+ const matName = geoData.source_mat;
37375
+ const matData = matName ? this.matLibs[matName] : void 0;
37376
+ if (matData && matData.map_Kd) {
37377
+ const texturePath = StringUtil.normalizePath(
37378
+ this.baseUrl + matData.map_Kd
37379
+ );
37380
+ const texture = Engine3D.res.getTexture(texturePath);
37381
+ if (texture) {
37382
+ mat.baseMap = texture;
37383
+ }
37384
+ }
37191
37385
  let obj = new exports.Object3D();
37192
37386
  let mr = obj.addComponent(exports.MeshRenderer);
37193
37387
  mr.geometry = geo;
@@ -41064,7 +41258,7 @@ else if (typeof exports === 'object')
41064
41258
  }
41065
41259
  }
41066
41260
 
41067
- const version = "1.0.18";
41261
+ const version = "1.0.19";
41068
41262
 
41069
41263
  class Engine3D {
41070
41264
  /**
@@ -61389,12 +61583,16 @@ fn frag(){
61389
61583
  }
61390
61584
 
61391
61585
  class BoundingVolume {
61586
+ static s_tmpMatrix = null;
61392
61587
  _type;
61393
61588
  _data;
61394
61589
  _box;
61395
61590
  _sphere;
61396
61591
  _matrix;
61397
61592
  constructor(data) {
61593
+ if (!BoundingVolume.s_tmpMatrix) {
61594
+ BoundingVolume.s_tmpMatrix = new Matrix4();
61595
+ }
61398
61596
  this._data = data;
61399
61597
  if (data.box) {
61400
61598
  this._type = "box";
@@ -61518,7 +61716,8 @@ fn frag(){
61518
61716
  }
61519
61717
  return target;
61520
61718
  } else if (this._box) {
61521
- const worldMatrix = new Matrix4();
61719
+ const worldMatrix = BoundingVolume.s_tmpMatrix;
61720
+ worldMatrix.identity();
61522
61721
  if (this._matrix) {
61523
61722
  if (parentTransform) {
61524
61723
  worldMatrix.multiplyMatrices(parentTransform, this._matrix);
@@ -47,6 +47,8 @@ export declare class OBJParser extends ParserBase {
47
47
  [name: string]: GeometryData;
48
48
  };
49
49
  private activeGeo;
50
+ private currentObjectName;
51
+ private currentMaterialName;
50
52
  facesMaterialsIndex: {
51
53
  materialName: string;
52
54
  materialStartIndex: number;
@@ -57,8 +59,28 @@ export declare class OBJParser extends ParserBase {
57
59
  private applyVector2;
58
60
  private applyVector3;
59
61
  private applyVector4;
62
+ /**
63
+ * Parse UV index with support for negative indices (OBJ format)
64
+ * Similar to Three.js parseUVIndex method
65
+ */
66
+ private parseUVIndex;
67
+ /**
68
+ * Parse vertex index with support for negative indices (OBJ format)
69
+ */
70
+ private parseVertexIndex;
71
+ /**
72
+ * Parse normal index with support for negative indices (OBJ format)
73
+ */
74
+ private parseNormalIndex;
75
+ /**
76
+ * Calculate face normal from three vertices using cross product
77
+ * Similar to Three.js addFaceNormal method
78
+ */
79
+ private calculateFaceNormal;
60
80
  private loadMTL;
61
81
  private load_textures;
82
+ private getGeometryKey;
83
+ private ensureActiveGeo;
62
84
  private parserLine;
63
85
  private parserOBJ;
64
86
  private parser_mesh;
@@ -6,6 +6,7 @@ import { Frustum } from '../../../../core/bound/Frustum';
6
6
  import { BoundingVolumeData } from './TileSet';
7
7
  export type BoundingVolumeType = 'box' | 'sphere' | 'region';
8
8
  export declare class BoundingVolume {
9
+ static s_tmpMatrix: Matrix4;
9
10
  private _type;
10
11
  private _data;
11
12
  private _box?;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rings-webgpu/core",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "Rings webgpu Engine",
5
5
  "main": "index.js",
6
6
  "exports": {