@threepipe/plugin-blend-importer 0.0.6 → 0.0.7

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.
@@ -1 +1 @@
1
- {"version":3,"file":"geometry.d.ts","sourceRoot":"","sources":["../../src/loader/geometry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAC,MAAM,OAAO,CAAA;AAezB,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,kHAgM3D;AASD,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,kHA0E1D"}
1
+ {"version":3,"file":"geometry.d.ts","sourceRoot":"","sources":["../../src/loader/geometry.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,GAAG,EAAC,MAAM,OAAO,CAAA;AAezB,wBAAgB,oBAAoB,CAAC,QAAQ,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,kHAoM3D;AASD,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,kHA6E1D"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@threepipe/plugin-blend-importer",
3
3
  "description": "Basic importer for .blend file",
4
- "version": "0.0.6",
4
+ "version": "0.0.7",
5
5
  "dependencies": {},
6
6
  "exports": {
7
7
  ".": {
@@ -52,8 +52,8 @@ export class BlendLoadPlugin extends BaseImporterPlugin {
52
52
  const objects = await createObjects(blend, ctx)
53
53
  const root = new Object3D()
54
54
  root.add(...objects)
55
- root.userData.autoScaled = true
56
- root.userData.autoCentered = true
55
+ // root.userData.autoScaled = true
56
+ // root.userData.autoCentered = true
57
57
  // console.log(res, blend, root)
58
58
  blend.scene = root
59
59
  return blend
@@ -583,6 +583,15 @@ function worker_code () {
583
583
  function toString (buffer, _in, _out) {
584
584
  return String.fromCharCode.apply(String, new Uint8Array(buffer, _in, _out - _in));
585
585
  }
586
+ function seekCheck (buffer, _in, str) {
587
+ const length = str.length;
588
+ for (let i = 0; i < length; i++) {
589
+ if (buffer.getUint8(_in + i) !== str.charCodeAt(i)) {
590
+ return false;
591
+ }
592
+ }
593
+ return true;
594
+ }
586
595
 
587
596
  // Begin parsing blender __blender_file__
588
597
 
@@ -674,19 +683,16 @@ function worker_code () {
674
683
  curr_count++;
675
684
  }
676
685
 
677
- // todo fix seek logic
678
- // Adjust for 4byte alignment TYPE
679
- if (toString(_data, offset2, offset2 + 4) !== 'TYPE'
680
- ) {
681
- for (let j = 0; j < 8; j++) {
682
- if(toString(_data, offset2, offset2 + 4) === 'TYPE') break
683
- offset2++;
684
- }
685
- // offset2 = (4 - (offset2 % 4)) + offset2;
686
+ // Adjust for alignment TYPE
687
+ for (let j = 0; j < 8; j++) {
688
+ if(seekCheck(data, offset2, 'TYPE')) break
689
+ offset2++;
686
690
  }
687
- if(toString(_data, offset2, offset2 + 4) !== 'TYPE') {
688
- debugger
691
+ if(!seekCheck(data, offset2, 'TYPE')) {
692
+ ERROR = 'Unexpected alignment error in SDNA parsing';
693
+ break
689
694
  }
695
+
690
696
  offset2 += 4;
691
697
 
692
698
  // Number of struct types
@@ -706,18 +712,14 @@ function worker_code () {
706
712
  curr_count++;
707
713
  }
708
714
 
709
- // todo fix seek logic
710
- // Adjust for 4byte alignment
711
- if (toString(_data, offset2, offset2 + 4) !== 'TLEN'
712
- ) {
713
- for (let j = 0; j < 8; j++) {
714
- if(toString(_data, offset2, offset2 + 4) === 'TLEN') break
715
- offset2++;
716
- }
717
- // offset2 = (4 - (offset2 % 4)) + offset2;
715
+ // Adjust for alignment
716
+ for (let j = 0; j < 8; j++) {
717
+ if(seekCheck(data, offset2, 'TLEN')) break
718
+ offset2++;
718
719
  }
719
- if(toString(_data, offset2, offset2 + 4) !== 'TLEN') {
720
- debugger
720
+ if(!seekCheck(data, offset2, 'TLEN')) {
721
+ ERROR = 'Unexpected alignment error in SDNA parsing';
722
+ break
721
723
  }
722
724
  offset2 += 4;
723
725
  curr_count = 0;
@@ -729,18 +731,14 @@ function worker_code () {
729
731
  curr_count++;
730
732
  }
731
733
 
732
- // todo fix seek logic
733
- // Adjust for 4byte alignment STRC
734
- if (toString(_data, offset2, offset2 + 4) !== 'STRC'
735
- ) {
736
- for (let j = 0; j < 8; j++) {
737
- if(toString(_data, offset2, offset2 + 4) === 'STRC') break
738
- offset2++;
739
- }
740
- // offset2 = (4 - (offset2 % 4)) + offset2;
734
+ // Adjust for alignment
735
+ for (let j = 0; j < 8; j++) {
736
+ if(seekCheck(data, offset2, 'STRC')) break
737
+ offset2++;
741
738
  }
742
- if(toString(_data, offset2, offset2 + 4) !== 'STRC') {
743
- debugger
739
+ if(!seekCheck(data, offset2, 'STRC')) {
740
+ ERROR = 'Unexpected alignment error in SDNA parsing';
741
+ break
744
742
  }
745
743
  offset2 += 4;
746
744
 
@@ -24,7 +24,9 @@ export function createBufferGeometry(meshData: any, ctx: Ctx) {
24
24
  // https://github.com/blender/blender/blob/05dcc0377b62d8e026e1901dfbecbd4b06fda0b5/scripts/addons_core/io_scene_gltf2/blender/exp/primitive_extract.py#L19
25
25
 
26
26
  let vertices
27
+ let verticesData
27
28
  let indices
29
+ let indicesData
28
30
 
29
31
  // Extract vertex positions from vdata layers
30
32
  if (meshData.vdata && meshData.vdata.layers && meshData.vdata.totlayer > 0) {
@@ -40,6 +42,7 @@ export function createBufferGeometry(meshData: any, ctx: Ctx) {
40
42
  continue
41
43
  }
42
44
  vertices = layer
45
+ verticesData = data
43
46
  } else {
44
47
  // console.log('unknown vdata', layer)
45
48
  }
@@ -61,6 +64,7 @@ export function createBufferGeometry(meshData: any, ctx: Ctx) {
61
64
  continue
62
65
  }
63
66
  indices = layer
67
+ indicesData = data
64
68
  } else {
65
69
  // console.log('unknown ldata', layer)
66
70
  }
@@ -106,30 +110,30 @@ export function createBufferGeometry(meshData: any, ctx: Ctx) {
106
110
  // return geometry
107
111
  // }
108
112
 
109
- if (vertices?.data && vertices.data.length > 0) {
110
- const positions = new Float32Array(vertices.data.length * 3)
111
- for (let j = 0; j < vertices.data.length; j++) {
112
- const vertex = vertices.data[j]
113
- if (vertex.x !== undefined) {
114
- positions[j * 3] = vertex.x
115
- positions[j * 3 + 1] = vertex.z
116
- positions[j * 3 + 2] = -vertex.y
117
- } else if (vertex.co !== undefined) {
118
- positions[j * 3] = vertex.co[0]
119
- positions[j * 3 + 1] = vertex.co[2]
120
- positions[j * 3 + 2] = -vertex.co[1]
113
+ if (verticesData && verticesData.length > 0) {
114
+ const positions = new Float32Array(verticesData.length * 3)
115
+ for (let j = 0; j < verticesData.length; j++) {
116
+ const {x, y, z, co} = verticesData[j] || {}
117
+ if (x !== undefined) {
118
+ positions[j * 3] = x
119
+ positions[j * 3 + 1] = z
120
+ positions[j * 3 + 2] = -y
121
+ } else if (co !== undefined) {
122
+ positions[j * 3] = co[0]
123
+ positions[j * 3 + 1] = co[2]
124
+ positions[j * 3 + 2] = -co[1]
121
125
  } else {
122
126
  // console.log(bakeGetters(meshData))
123
127
  // const t = [...new Int32Array(vertex.__blender_file__.AB, vertex.__data_address__, 8)]
124
128
  // debugger
125
- console.error('BlendLoader - unknown vertex', vertex)
129
+ console.error('BlendLoader - unknown vertex', verticesData[j])
126
130
  break
127
131
  }
128
132
  }
129
133
  geometry.setAttribute('position', new ctx.BufferAttribute(positions, 3))
130
134
  }
131
135
 
132
- if (indices?.data && indices.data.length > 0 && vertices?.data?.length) {
136
+ if (indicesData && indicesData.length > 0 && verticesData?.length) {
133
137
  const faceSize = meshData.totloop / meshData.totpoly
134
138
  if (faceIndices.length > 0) {
135
139
  // Use face offset indices for variable-sized faces
@@ -149,11 +153,11 @@ export function createBufferGeometry(meshData: any, ctx: Ctx) {
149
153
 
150
154
  if (faceVertCount >= 3) {
151
155
  // todo better Triangulate the face using fan triangulation
152
- const firstVert = indices.data[faceStart].i
156
+ const firstVert = indicesData[faceStart].i
153
157
  for (let k = 1; k < faceVertCount - 1; k++) {
154
158
  indexes[t++] = firstVert
155
- indexes[t++] = indices.data[faceStart + k].i
156
- indexes[t++] = indices.data[faceStart + k + 1].i
159
+ indexes[t++] = indicesData[faceStart + k].i
160
+ indexes[t++] = indicesData[faceStart + k + 1].i
157
161
  }
158
162
  } else {
159
163
  // debugger
@@ -251,24 +255,27 @@ export function createBufferGeometryOld(mesh: any, ctx: Ctx) {
251
255
  index += indexi - 1 + l
252
256
 
253
257
  const loop = loops[index]
254
- const vert = vertices[loop.v]
258
+ const {co, no} = vertices[loop.v] || {}
255
259
  indices[currentIndex] = currentIndex
256
260
 
257
- positions[currentIndex * 3 + 0] = vert.co[0]
258
- positions[currentIndex * 3 + 1] = vert.co[2]
259
- positions[currentIndex * 3 + 2] = -vert.co[1]
261
+ if (co) {
262
+ positions[currentIndex * 3 + 0] = co[0]
263
+ positions[currentIndex * 3 + 1] = co[2]
264
+ positions[currentIndex * 3 + 2] = -co[1]
265
+ }
260
266
 
261
- if (vert.no) {
262
- normals[currentIndex * 3 + 0] = vert.no[0]
263
- normals[currentIndex * 3 + 1] = vert.no[2]
264
- normals[currentIndex * 3 + 2] = -vert.no[1]
267
+ if (no) {
268
+ normals[currentIndex * 3 + 0] = no[0]
269
+ normals[currentIndex * 3 + 1] = no[2]
270
+ normals[currentIndex * 3 + 2] = -no[1]
265
271
  } else {
266
272
  computeNormals = true
267
273
  }
268
274
 
269
275
  if (uv) {
270
- uvs[currentIndex * 2 + 0] = uv[index].uv[0]
271
- uvs[currentIndex * 2 + 1] = uv[index].uv[1]
276
+ const uv1 = uv[index].uv
277
+ uvs[currentIndex * 2 + 0] = uv1[0]
278
+ uvs[currentIndex * 2 + 1] = uv1[1]
272
279
  }
273
280
 
274
281
  currentIndex++