@@ -5,17 +5,14 @@ const walnutInit = require('@bluelightning32/walnut')
5
5
6
6
const PRECISION = - 10
7
7
8
- console . log ( 'jscad walnut imported' )
9
-
10
8
function init ( wasmPath , callback ) {
11
9
if ( module . exports . walnut !== null ) {
12
10
console . log ( 'walnut already initialized' )
13
11
callback ( )
14
12
return
15
13
}
16
- console . log ( 'walnut init starting ' )
14
+ console . log ( 'walnut initializing ' )
17
15
function findWasm ( file , scriptDirectory ) {
18
- console . log ( 'findWasm' , wasmPath )
19
16
return wasmPath
20
17
}
21
18
@@ -46,7 +43,7 @@ function toWalnut(obj){
46
43
obj . polygons . forEach ( p => vertexCount += p . length )
47
44
let mesh = module . exports . walnut . _AllocateMesh ( vertexCount )
48
45
49
- polyBuffer = w_AllocateFloatVertexArray ( 4096 )
46
+ const polyBuffer = w_AllocateFloatVertexArray ( 4096 )
50
47
tmpBuffer = module . exports . walnut . _AllocateTempVertexBuffer ( )
51
48
52
49
obj . polygons . forEach ( p => {
@@ -73,6 +70,9 @@ function toWalnutTransformed(obj){
73
70
obj . polygons . forEach ( p => vertexCount += p . length )
74
71
let mesh = module . exports . walnut . _AllocateMesh ( vertexCount )
75
72
73
+ const polyBuffer = w_AllocateFloatVertexArray ( 4096 )
74
+ tmpBuffer = module . exports . walnut . _AllocateTempVertexBuffer ( )
75
+
76
76
let v = [ 0 , 0 , 0 ]
77
77
obj . polygons . forEach ( p => {
78
78
if ( p . vertices ) p = p . vertices // jscad format
@@ -84,52 +84,75 @@ function toWalnutTransformed(obj){
84
84
} )
85
85
module . exports . walnut . _AddFloatPolygonToMesh ( count / 3 , polyBuffer . vertexPointer , tmpBuffer , mesh , PRECISION )
86
86
} )
87
+
88
+ module . exports . walnut . _FreeTempVertexBuffer ( tmpBuffer )
89
+ polyBuffer . free ( )
90
+
87
91
mesh . vertexCount = vertexCount
88
92
return mesh
89
93
}
90
94
91
95
function toGeom ( mesh ) {
92
- let triangleCount = module . exports . walnut . _GetTriangleCountInMesh ( mesh )
93
- let vertexCount = triangleCount * 3
94
- let vertexPointer = module . exports . walnut . _AllocateFloatVertexArray ( vertexCount )
95
- module . exports . walnut . _GetFloatTrianglesFromMesh ( mesh , vertexPointer )
96
-
97
- let vertices = new Float32Array ( vertexCount * 3 )
98
- vertices . set ( module . exports . walnut . HEAPF32 . subarray ( vertexPointer / 4 , vertexPointer / 4 + vertexCount * 3 ) )
99
- let indices = new Uint16Array ( vertexCount * 3 )
100
- for ( let i = 0 ; i < vertexCount ; ++ i ) {
101
- indices [ i ] = i
102
- }
103
- module . exports . walnut . _FreeFloatVertexArray ( vertexPointer )
104
- return { indices, vertices, type : 'mesh' }
96
+ const combined = module . exports . walnut . _GetDoublePolygonArrayFromMesh ( mesh , 0 )
97
+
98
+ const combinedFields = module . exports . walnut . HEAPU32 . subarray ( combined / 4 , combined / 4 + 4 )
99
+ const polygonCount = combinedFields [ 0 ]
100
+ const planes = module . exports . walnut . HEAPF64 . subarray ( combinedFields [ 1 ] / 8 , combinedFields [ 1 ] / 8 + polygonCount * 4 )
101
+ const vertexCounts = module . exports . walnut . HEAPU32 . subarray ( combinedFields [ 2 ] / 4 , combinedFields [ 2 ] / 4 + polygonCount )
102
+
103
+ let totalVertexCount = 0
104
+ for ( let i = 0 ; i < polygonCount ; ++ i ) {
105
+ totalVertexCount += vertexCounts [ i ]
106
+ }
107
+
108
+ const vertices = module . exports . walnut . HEAPF64 . subarray ( combinedFields [ 3 ] / 8 , combinedFields [ 3 ] / 8 + 3 * totalVertexCount )
109
+
110
+ const polygons = new Array ( polygonCount )
111
+ let vertexIndex = 0
112
+ for ( let i = 0 ; i < polygonCount ; ++ i ) {
113
+ const outputVertices = new Array ( vertexCounts [ i ] )
114
+ for ( let j = 0 ; j < vertexCounts [ i ] ; ++ j , vertexIndex += 3 ) {
115
+ outputVertices [ j ] = [
116
+ vertices [ vertexIndex + 0 ] ,
117
+ vertices [ vertexIndex + 1 ] ,
118
+ vertices [ vertexIndex + 2 ] ,
119
+ ]
120
+ }
121
+ const plane = [
122
+ planes [ i * 4 + 0 ] ,
123
+ planes [ i * 4 + 1 ] ,
124
+ planes [ i * 4 + 2 ] ,
125
+ planes [ i * 4 + 3 ]
126
+ ]
127
+ polygons [ i ] = {
128
+ vertices : outputVertices ,
129
+ plane : plane
130
+ }
131
+ }
132
+ identity = new Array ( 16 )
133
+ mat4 . identity ( identity )
134
+ return {
135
+ polygons : polygons ,
136
+ transforms : identity
137
+ }
105
138
}
106
139
107
140
function intersect ( geom1 , geom2 ) {
108
- console . log ( 'intersect' , geom1 , geom2 ) ;
109
- let time = Date . now ( )
110
141
let wMesh1 = toWalnut ( geom1 )
111
- console . log ( 'Mesh1 transfered to walnut ' , Date . now ( ) - time ) ;
112
- time = Date . now ( )
113
142
let wMesh2 = toWalnut ( geom2 )
114
- console . log ( 'Mesh2 transfered to walnut ' , Date . now ( ) - time ) ;
115
143
116
144
let wResult = module . exports . walnut . _AllocateMesh ( wMesh1 . vertexCount )
117
145
118
146
let filterSuccess = module . exports . walnut . _IntersectMeshes ( wMesh1 , wMesh2 , wResult )
119
- console . log ( 'intersect ' , Date . now ( ) - time ) ;
120
- time = Date . now ( )
121
147
122
148
let result = toGeom ( wResult )
123
149
124
150
module . exports . walnut . _FreeMesh ( wMesh1 )
125
151
module . exports . walnut . _FreeMesh ( wMesh2 )
126
152
module . exports . walnut . _FreeMesh ( wResult )
127
153
128
- result . color = geom1 . color
129
154
result . transforms = Array ( 16 )
130
155
mat4 . identity ( result . transforms )
131
- console . log ( 'filter result' , filterSuccess , result )
132
- console . log ( 'toGeom ' , Date . now ( ) - time ) ;
133
156
return result
134
157
}
135
158
0 commit comments