Skip to content

Commit 5e62433

Browse files
authored
Update building-3d-environments-threejs-pt-2.md
1 parent 2891eb0 commit 5e62433

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

en/drafts/originals/building-3d-environments-threejs-pt-2.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -389,21 +389,29 @@ You need to **add** the following code:
389389
jars = new THREE.Group();
390390
scene.add( jars );
391391
392+
// jar/site positions
393+
aibomSite = { x: 0.36, z : -0.01 }
394+
mailuSite = { x: 0.84, z : 0.48 }
395+
louisadeSite = { x: 0.99, z : 0.59 }
396+
adzeraSite = { x: 0.61, z : 0.15 }
397+
dimiriSite = { x: 0.43, z : 0 }
398+
yabobSite = { x: 0.572, z : 0.0396 }
399+
392400
// loading function for Aibom jar model
393401
function onLoadAibom( gltf ) {
394402
aibomM = gltf.scene.children[0];
395403
aibomM.material = new THREE.MeshStandardMaterial();
396404
aibomM.material.color.set(parameters.materialColor);
397405
aibomM.scale.set( piecescale, piecescale, piecescale);
398-
aibomM.position.set( 0.36* ratio, desk + 0.01, -0.01* ratio);
406+
aibomM.position.set( aibomSite.x* ratio, desk + 0.01, aibomSite.z* ratio);
399407
aibomM.userData.planes = aibomG;
400408
jars.add( aibomM);
401409
}
402410
loader.load( 'models/aibom.glb', onLoadAibom, undefined, function ( error ) {console.error( error );} );
403411
```
404412
Save the index.html file and reload the web browser and you should see a model. You will notice that you did not have to add ```aibomM``` to the scene, since you added it to the jars group, which has already been added to the scene.
405413

406-
To avoid repetitive code you will define a function ```createModel()```, and assign the ```onLoadAibom()``` function run this ```createModel()``` function when it loads the model. The function will take 5 arguments: the model filename, the x position, the z position, the model colour and the matching gallery as these vary with the different models.
414+
To avoid repetitive code you will define a function ```createModel()```, and assign the ```onLoadAibom()``` function run this ```createModel()``` function when it loads the model. The function will take 4 arguments: the model filename, position, the model colour and the matching gallery as these vary with the different models.
407415

408416
It may seem confusing to have to have two different functions and it is not essential to understand the following, but it may help if you are trying to write your own code. The `loader.load method` does not expect the function (i.e. ```onLoadAibom```) called after loading to return anything. You will note there is no ```return(x)``` in the ```onLoadAibom``` function. So you have to pass our loaded model to a pre-declared variable (i.e. ```aibomM```).
409417

@@ -421,19 +429,19 @@ loader.load( 'models/aibom.glb', onLoadAibom, undefined, function ( error ) {con
421429
The **lines** of code above should be **changed** to the following:
422430
```
423431
//a function to make the model with the parameter specified
424-
function createModel(gltf, x, z, col, gallery){
432+
function createModel(gltf, site, col, gallery){
425433
const model = gltf.scene.children[0];
426434
model.material = new THREE.MeshStandardMaterial();
427435
model.material.color.set(col);
428436
model.scale.set( piecescale, piecescale, piecescale);
429-
model.position.set( x * ratio, desk + 0.01, z * ratio);
437+
model.position.set( site.x * ratio, desk + 0.01, site.z * ratio);
430438
model.userData.planes = gallery;
431439
return model;
432440
}
433441
434442
//calls the createModel function but still in a separately defined function
435443
function onLoadAibom( gltf ) {
436-
aibomM = createModel(gltf, 0.36, -0.01, parameters.materialColor, aibomG);
444+
aibomM = createModel(gltf, aibomSite, parameters.materialColor, aibomG);
437445
jars.add( aibomM);
438446
}
439447
loader.load( 'models/aibom.glb', onLoadAibom, undefined, function ( error ) {console.error( error );} );
@@ -459,32 +467,32 @@ The **lines** of code above should be **changed** to the following:
459467
// directly has the onLoad function as an anonymous function in the loader.load
460468
// load a jar (filename, load function, function while loading, error function)
461469
loader.load( 'models/aibom.glb', function( gltf ) {
462-
aibomM = createModel(gltf, 0.36, -0.01, parameters.materialColor, aibomG);
470+
aibomM = createModel(gltf, aibomSite, parameters.materialColor, aibomG);
463471
jars.add( aibomM);
464472
}, undefined, function ( error ) {console.error( error );} );
465473
466474
loader.load( 'models/mailu.glb', function( gltf) {
467-
mailuM = createModel(gltf, 0.84, 0.48, parameters.nabColor, mailuG);
475+
mailuM = createModel(gltf, mailuSite, parameters.nabColor, mailuG);
468476
jars.add( mailuM);
469477
}, undefined, function ( error ) { console.error( error );} );
470478
471479
loader.load( 'models/louisade.glb', function( gltf ) {
472-
louisadeM = createModel(gltf, 0.99, 0.59, parameters.ringTopColor, louisadeG);
480+
louisadeM = createModel(gltf, louisadeSite, parameters.ringTopColor, louisadeG);
473481
jars.add(louisadeM);
474482
}, undefined, function ( error ) {console.error( error );} );
475483
476484
loader.load( 'models/adzera.glb', function( gltf ) {
477-
adzeraM = createModel(gltf, 0.61, 0.15, parameters.coilBeatenColor, adzeraG);
485+
adzeraM = createModel(gltf, adzeraSite, parameters.coilBeatenColor, adzeraG);
478486
jars.add( adzeraM);
479487
}, undefined, function ( error ) {console.error( error );} );
480488
481489
loader.load( 'models/dimiri.glb', function( gltf ) {
482-
dimiriM = createModel(gltf, 0.43, 0, parameters.coilColor, dimiriG);
490+
dimiriM = createModel(gltf, dimiriSite, parameters.coilColor, dimiriG);
483491
jars.add( dimiriM);
484492
}, undefined, function ( error ) {console.error( error );} );
485493
486494
loader.load( 'models/yabob.glb', function( gltf ) {
487-
yabobM = createModel(gltf, 0.572, 0.0396, parameters.paddleColor, yabobG);
495+
yabobM = createModel(gltf, yabobSite, parameters.paddleColor, yabobG);
488496
jars.add( yabobM);
489497
}, undefined, function ( error ) {console.error( error );} );
490498

0 commit comments

Comments
 (0)