@@ -3,7 +3,7 @@ import { BitByBitBase } from "@bitbybit-dev/threejs";
3
3
import { OccStateEnum } from '@bitbybit-dev/occt-worker' ;
4
4
import { Inputs } from '@bitbybit-dev/threejs' ;
5
5
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls' ;
6
- import { Color , DirectionalLight , Fog , Group , HemisphereLight , Mesh , MeshPhongMaterial , PerspectiveCamera , PlaneGeometry , Scene , Vector3 , VSMShadowMap , WebGLRenderer } from 'three' ;
6
+ import { Color , DirectionalLight , Fog , Group , HemisphereLight , Mesh , MeshPhongMaterial , MeshPhysicalMaterial , PerspectiveCamera , PlaneGeometry , Scene , Vector3 , VSMShadowMap , WebGLRenderer } from 'three' ;
7
7
import { GUI } from 'lil-gui' ;
8
8
9
9
function component ( ) {
@@ -23,6 +23,9 @@ function component() {
23
23
) ;
24
24
}
25
25
26
+ let mouseX = 0 ;
27
+ let mouseY = 0 ;
28
+
26
29
const hideSpinner = ( ) => {
27
30
const el = document . getElementById ( 'spinner' ) ;
28
31
if ( el ) {
@@ -37,14 +40,18 @@ function component() {
37
40
38
41
type Model = {
39
42
numberOfRays : number ,
40
- nrSubdivisions : number ,
43
+ widening : number ,
41
44
color : string ,
45
+ sourceCode : ( ) => void
42
46
}
43
47
44
48
const model = {
45
49
numberOfRays : 200 ,
46
- nrSubdivisions : 1 ,
47
- color : '#0f0f0f'
50
+ widening : 3 ,
51
+ color : '#be8250' ,
52
+ sourceCode : ( ) => {
53
+ window . open ( "https://github.com/bitbybit-dev/app-examples/blob/main/webpack/threejs/src/code/homepage.ts" , "_blank" ) ;
54
+ }
48
55
} as Model ;
49
56
50
57
let shapesToClean : Inputs . OCCT . TopoDSShapePointer [ ] = [ ] ;
@@ -54,7 +61,8 @@ function component() {
54
61
55
62
const rotateGroup = ( ) => {
56
63
if ( current . group ) {
57
- current . group . rotation . y += 0.005 ;
64
+ current . group . rotation . y -= ( ( window . innerWidth / 2 ) - mouseX ) / 300000 ;
65
+ current . group . rotation . z -= ( ( window . innerHeight / 2 ) - mouseY ) / 300000 ;
58
66
}
59
67
}
60
68
@@ -68,8 +76,8 @@ function component() {
68
76
const spiralOptions = new Inputs . Point . SpiralDto ( ) ;
69
77
spiralOptions . phi = 0.9 ;
70
78
spiralOptions . numberPoints = model . numberOfRays ;
71
- spiralOptions . widening = 3 ;
72
- spiralOptions . radius = 6 ;
79
+ spiralOptions . widening = model . widening ;
80
+ spiralOptions . radius = 10 ;
73
81
spiralOptions . factor = 1 ;
74
82
const spiralPoints = bitbybit . point . spiral ( spiralOptions )
75
83
@@ -84,7 +92,7 @@ function component() {
84
92
85
93
const pointsAtParam = bitbybit . verb . curve . pointsAtParam ( {
86
94
curves : rays ,
87
- parameter : 0.4
95
+ parameter : 0.3
88
96
} ) ;
89
97
90
98
const spiralingLineCurves = bitbybit . line . convertLinesToNurbsCurves ( {
@@ -101,16 +109,16 @@ function component() {
101
109
keepRemainder : false
102
110
} ) ;
103
111
104
- const curveScalingFactor = 1 ;
112
+ let curveScalingFactor = 1 ;
105
113
const curveScalingFactorCenter = 1 ;
106
114
107
115
const wiresSegmented : Promise < Inputs . OCCT . TopoDSWirePointer [ ] > [ ] = [ ] ;
108
- segmentedLineCurves . forEach ( ( segmentedLineCurve ) => {
116
+ segmentedLineCurves . reverse ( ) . forEach ( ( segmentedLineCurve , i : number ) => {
109
117
const wires : Promise < Inputs . OCCT . TopoDSWirePointer > [ ] = [ ] ;
110
118
segmentedLineCurve . forEach ( ( lineCurve : any ) => {
111
- // lineCurve = bitbybit.verb.curve.reverse({
112
- // curve: lineCurve
113
- // });
119
+
120
+ curveScalingFactor += ( 10 / model . numberOfRays ) ;
121
+
114
122
const pt1 = bitbybit . verb . curve . startPoint ( {
115
123
curve : lineCurve
116
124
} ) ;
@@ -132,25 +140,25 @@ function component() {
132
140
parameter : 0.5 ,
133
141
} )
134
142
] ,
135
- translation : [ 0 , 0 , 0.2 * curveScalingFactor ]
143
+ translation : [ 0 , 0 , i % 3 ? 0.3 : 0.4 ]
136
144
} ) [ 0 ] ;
137
145
const pt4 = bitbybit . point . translatePoints ( {
138
146
points : [
139
147
bitbybit . verb . curve . pointAtParam ( {
140
148
curve : lineCurve ,
141
- parameter : 0.99 ,
149
+ parameter : i % 3 ? 0.99 : 1.1
142
150
} )
143
151
] ,
144
- translation : [ 0 , 0 , 0.2 * curveScalingFactor ]
152
+ translation : [ 0 , 0 , i % 3 ? 0.15 : 0.05 ]
145
153
} ) [ 0 ] ;
146
154
const pt5 = bitbybit . point . translatePoints ( {
147
155
points : [
148
156
bitbybit . verb . curve . pointAtParam ( {
149
157
curve : lineCurve ,
150
- parameter : 0.99 ,
158
+ parameter : i % 3 ? 0.99 : 1.1
151
159
} )
152
160
] ,
153
- translation : [ 0 , 0 , - 0.2 * curveScalingFactor ]
161
+ translation : [ 0 , 0 , i % 3 ? - 0.15 : - 0.05 ]
154
162
} ) [ 0 ] ;
155
163
const pt6 = bitbybit . point . translatePoints ( {
156
164
points : [
@@ -159,7 +167,7 @@ function component() {
159
167
parameter : 0.5 ,
160
168
} )
161
169
] ,
162
- translation : [ 0 , 0 , - 0.2 * curveScalingFactor ]
170
+ translation : [ 0 , 0 , i % 3 ? - 0.3 : - 0.4 ]
163
171
} ) [ 0 ] ;
164
172
const pt7 = bitbybit . point . translatePoints ( {
165
173
points : [
@@ -183,7 +191,7 @@ function component() {
183
191
184
192
const segmentedWires = await Promise . all ( wiresSegmented ) ;
185
193
const lofts : Promise < Inputs . OCCT . TopoDSWirePointer > [ ] = [ ] ;
186
- segmentedWires . forEach ( ( wires , i ) => {
194
+ segmentedWires . reverse ( ) . forEach ( ( wires , i ) => {
187
195
if ( i > 10 ) {
188
196
const loft = bitbybit . occt . operations . loft ( {
189
197
shapes : wires ,
@@ -194,86 +202,51 @@ function component() {
194
202
} ) ;
195
203
196
204
const res = await Promise . all ( lofts ) ;
197
- // const thicks: Promise<Inputs.OCCT.TopoDSShapePointer>[] = [];
198
-
199
- // res.forEach((r, i) => {
200
- // if (i > 10) {
201
- // const thick = bitbybit.occt.fillets.filletEdges({
202
- // shape: r,
203
- // radius: 0.005,
204
- // });
205
- // thicks.push(thick);
206
- // }
207
- // });
208
- // const thicksRes = await Promise.all(thicks);
205
+
206
+ window . addEventListener ( "mousemove" , ( event ) => {
207
+ mouseX = event . clientX ;
208
+ mouseY = event . clientY ;
209
+ } ) ;
210
+
209
211
finalShape = await bitbybit . occt . shapes . compound . makeCompound ( {
210
212
shapes : res ,
211
213
} ) ;
212
214
213
215
const drawOptions = new Inputs . Draw . DrawOcctShapeOptions ( ) ;
214
- const mat = new MeshPhongMaterial ( { color : new Color ( model . color ) } ) ;
216
+ const mat = new MeshPhysicalMaterial ( { color : new Color ( model . color ) } ) ;
215
217
mat . polygonOffset = true ;
216
218
mat . polygonOffsetFactor = 1 ;
217
219
mat . polygonOffsetUnits = 2 ;
218
220
mat . side = 2 ;
221
+ mat . metalness = 0.5 ;
222
+ mat . roughness = 0.9 ;
223
+ mat . specularIntensity = 1 ;
219
224
drawOptions . faceMaterial = mat ;
220
225
drawOptions . edgeColour = "#000000" ;
221
-
226
+
222
227
const group = await bitbybit . draw . drawAnyAsync ( {
223
228
entity : finalShape ,
224
229
options : drawOptions
225
230
} )
226
231
227
- // const sphereManifold = await bitbybit.manifold.manifold.shapes.sphere({
228
- // radius: model.numberOfRays,
229
- // circularSegments: 32,
230
- // });
231
-
232
- // // max has small tolerance so that strict steps would fit the interval till last item
233
- // const span = bitbybit.vector.span({
234
- // step: model.numberOfRays * 2 / (model.nrSubdivisions + 1),
235
- // min: -model.numberOfRays,
236
- // max: model.numberOfRays + 0.000001,
237
- // });
238
-
239
- // const slicedManifolds = await manifold.booleans.splitByPlaneOnOffsets({
240
- // manifold: sphereManifold,
241
- // normal: [1, 1, 0.3],
242
- // originOffsets: span
243
- // });
244
-
245
- // const spanTranslations = bitbybit.vector.span({
246
- // step: model.numberOfRays * 4 / slicedManifolds.length,
247
- // min: 0,
248
- // max: model.numberOfRays * 4 + 1,
249
- // });
250
-
251
- // const translatedManifoldPromises: Promise<Inputs.Manifold.ManifoldPointer>[] = [];
252
- // slicedManifolds.forEach((s, i) => {
253
- // const translated = manifold.transforms.translate({
254
- // manifold: s,
255
- // vector: [0, spanTranslations[i], 0]
256
- // });
257
- // translatedManifoldPromises.push(translated);
258
- // });
259
-
260
- // const translatedManifolds = await Promise.all(translatedManifoldPromises);
261
- // finalManifold = await manifold.operations.compose({
262
- // manifolds: translatedManifolds
263
- // });
264
-
265
232
shapesToClean = [ ] ;
266
-
267
- // const options = new Inputs.Draw.DrawManifoldOrCrossSectionOptions();
268
- // drawOptions.faceColour = "#6600ff";
269
- // const group = await bitbybit.draw.drawAnyAsync({ entity: finalShape, options: drawOptions });
270
-
271
233
if ( group ) {
272
234
group . children [ 0 ] . children . forEach ( ( child ) => {
273
235
child . castShadow = true ;
274
236
child . receiveShadow = true ;
275
237
} ) ;
276
238
}
239
+
240
+ if ( current . group ) {
241
+ group . rotation . x = current . group . rotation . x ;
242
+ group . rotation . y = current . group . rotation . y ;
243
+ group . rotation . z = current . group . rotation . z ;
244
+
245
+ current . group . traverse ( ( obj ) => {
246
+ scene ?. remove ( obj ) ;
247
+ } ) ;
248
+ }
249
+
277
250
current . group = group ;
278
251
279
252
}
@@ -282,9 +255,7 @@ function component() {
282
255
const updateShape = async ( ) => {
283
256
showSpinner ( ) ;
284
257
disableGUI ( ) ;
285
- current . group ?. traverse ( ( obj ) => {
286
- scene ?. remove ( obj ) ;
287
- } ) ;
258
+
288
259
await createShape ( bitbybit , scene ) ;
289
260
enableGUI ( ) ;
290
261
hideSpinner ( ) ;
@@ -315,10 +286,10 @@ function component() {
315
286
}
316
287
317
288
const controls = new OrbitControls ( camera , renderer . domElement ) ;
318
- camera . position . set ( 6 , 1 , 6 ) ;
289
+ camera . position . set ( 10 , 1 , 10 ) ;
319
290
320
291
controls . update ( ) ;
321
- controls . target = new Vector3 ( 0 , 1 , 0 ) ;
292
+ controls . target = new Vector3 ( - 55 , 1 , 0 ) ;
322
293
controls . enableDamping = true ;
323
294
controls . dampingFactor = 0.1
324
295
controls . zoomSpeed = 0.1 ;
@@ -335,8 +306,8 @@ function component() {
335
306
336
307
// renderer.setClearColor(new Color(0x1a1c1f), 1);
337
308
338
- const dirLight = new DirectionalLight ( 0xffffff , 30 ) ;
339
- dirLight . position . set ( - 30 , - 50 , - 30 ) ;
309
+ const dirLight = new DirectionalLight ( 0x3333ff , 30 ) ;
310
+ dirLight . position . set ( 30 , - 50 , 30 ) ;
340
311
dirLight . castShadow = true ;
341
312
dirLight . shadow . camera . near = 0 ;
342
313
dirLight . shadow . camera . far = 200 ;
@@ -345,16 +316,16 @@ function component() {
345
316
dirLight . shadow . camera . left = - dist ;
346
317
dirLight . shadow . camera . top = dist ;
347
318
dirLight . shadow . camera . bottom = - dist ;
348
- dirLight . shadow . mapSize . width = 3000 ;
349
- dirLight . shadow . mapSize . height = 3000 ;
319
+ dirLight . shadow . mapSize . width = 2000 ;
320
+ dirLight . shadow . mapSize . height = 2000 ;
350
321
dirLight . shadow . blurSamples = 8 ;
351
322
dirLight . shadow . radius = 2 ;
352
323
dirLight . shadow . bias = - 0.0005 ;
353
324
354
325
scene . add ( dirLight ) ;
355
326
356
327
const material = new MeshPhongMaterial ( { color : 0x3300ff } )
357
- material . shininess = 0 ;
328
+ material . shininess = 0.3 ;
358
329
material . specular = new Color ( 0x222222 ) ;
359
330
360
331
bitbybit . occtWorkerManager . occWorkerState$ . subscribe ( async s => {
@@ -397,10 +368,10 @@ function component() {
397
368
} ) ;
398
369
399
370
gui
400
- . add ( model , "nrSubdivisions " , 1 , 32 , 1 )
401
- . name ( "Nr Subdivisions " )
371
+ . add ( model , "widening " , 2 , 5 , 0. 1)
372
+ . name ( "Widening " )
402
373
. onFinishChange ( ( value : number ) => {
403
- model . nrSubdivisions = value ;
374
+ model . widening = value ;
404
375
updateShape ( ) ;
405
376
} ) ;
406
377
@@ -416,6 +387,8 @@ function component() {
416
387
} ) ;
417
388
}
418
389
} )
390
+
391
+ const loadButton = gui . add ( model , "sourceCode" ) ;
419
392
}
420
393
421
394
init ( ) ;
0 commit comments