Skip to content

Commit 5ee2693

Browse files
committed
Added grain direction and made originOffset a uniform
1 parent 72057d2 commit 5ee2693

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

examples/jsm/materials/ProceduralWood.js

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ const wood = TSL.Fn( ( [
280280
darkGrainColor,
281281
lightGrainColor
282282
] ) => {
283-
283+
284284
const center = woodCenter( p, centerSize );
285285
const mainWarp = spaceWarp( spaceWarp( p, center, largeWarpScale, largeGrainStretch ), smallWarpStrength, smallWarpScale, 0.17 );
286286
const detailWarp = spaceWarp( mainWarp, fineWarpStrength, fineWarpScale, 0.17 );
@@ -296,6 +296,7 @@ const wood = TSL.Fn( ( [
296296
const woodParams = {
297297
teak: {
298298
originOffset: { x: -0.4, y: 0, z: 0 },
299+
grainRotation: { x: 0, y: 0, z: 0 },
299300
centerSize: 1.11, largeWarpScale: 0.32, largeGrainStretch: 0.24, smallWarpStrength: 0.059,
300301
smallWarpScale: 2, fineWarpStrength: 0.006, fineWarpScale: 32.8, ringCount: 34,
301302
ringBias: 0.03, ringSizeVariance: 0.03, ringVarianceScale: 4.4, barkThickness: 0.3,
@@ -304,6 +305,7 @@ const woodParams = {
304305
},
305306
walnut: {
306307
originOffset: { x: -0.4, y: 0, z: 0 },
308+
grainRotation: { x: 0, y: 0, z: 0 },
307309
centerSize: 1.07, largeWarpScale: 0.42, largeGrainStretch: 0.34, smallWarpStrength: 0.016,
308310
smallWarpScale: 10.3, fineWarpStrength: 0.028, fineWarpScale: 12.7, ringCount: 32,
309311
ringBias: 0.08, ringSizeVariance: 0.03, ringVarianceScale: 5.5, barkThickness: 0.98,
@@ -312,6 +314,7 @@ const woodParams = {
312314
},
313315
white_oak: {
314316
originOffset: { x: -0.4, y: 0, z: 0 },
317+
grainRotation: { x: 0, y: 0, z: 0 },
315318
centerSize: 1.23, largeWarpScale: 0.21, largeGrainStretch: 0.21, smallWarpStrength: 0.034,
316319
smallWarpScale: 2.44, fineWarpStrength: 0.01, fineWarpScale: 14.3, ringCount: 34,
317320
ringBias: 0.82, ringSizeVariance: 0.16, ringVarianceScale: 1.4, barkThickness: 0.7,
@@ -320,6 +323,7 @@ const woodParams = {
320323
},
321324
pine: {
322325
originOffset: { x: -0.4, y: 0, z: -0.2 },
326+
grainRotation: { x: 0, y: 0, z: 0 },
323327
centerSize: 1.23, largeWarpScale: 0.21, largeGrainStretch: 0.18, smallWarpStrength: 0.041,
324328
smallWarpScale: 2.44, fineWarpStrength: 0.006, fineWarpScale: 23.2, ringCount: 24,
325329
ringBias: 0.1, ringSizeVariance: 0.07, ringVarianceScale: 5, barkThickness: 0.35,
@@ -328,6 +332,7 @@ const woodParams = {
328332
},
329333
poplar: {
330334
originOffset: { x: -0.4, y: 0, z: 0.2 },
335+
grainRotation: { x: 0, y: 0, z: 0 },
331336
centerSize: 1.43, largeWarpScale: 0.33, largeGrainStretch: 0.18, smallWarpStrength: 0.04,
332337
smallWarpScale: 4.3, fineWarpStrength: 0.004, fineWarpScale: 33.6, ringCount: 37,
333338
ringBias: 0.07, ringSizeVariance: 0.03, ringVarianceScale: 3.8, barkThickness: 0.3,
@@ -336,6 +341,7 @@ const woodParams = {
336341
},
337342
maple: {
338343
originOffset: { x: -0.4, y: 0.3, z: -0.2 },
344+
grainRotation: { x: 0, y: 0, z: 0 },
339345
centerSize: 1.4, largeWarpScale: 0.38, largeGrainStretch: 0.25, smallWarpStrength: 0.067,
340346
smallWarpScale: 2.5, fineWarpStrength: 0.005, fineWarpScale: 33.6, ringCount: 35,
341347
ringBias: 0.1, ringSizeVariance: 0.07, ringVarianceScale: 4.6, barkThickness: 0.61,
@@ -344,6 +350,7 @@ const woodParams = {
344350
},
345351
red_oak: {
346352
originOffset: { x: -0.4, y: 0, z: 0.4 },
353+
grainRotation: { x: 0, y: 0, z: 0 },
347354
centerSize: 1.21, largeWarpScale: 0.24, largeGrainStretch: 0.25, smallWarpStrength: 0.044,
348355
smallWarpScale: 2.54, fineWarpStrength: 0.01, fineWarpScale: 14.5, ringCount: 34,
349356
ringBias: 0.92, ringSizeVariance: 0.03, ringVarianceScale: 5.6, barkThickness: 1.01,
@@ -352,6 +359,7 @@ const woodParams = {
352359
},
353360
cherry: {
354361
originOffset: { x: -0.4, y: 0.3, z: 0 },
362+
grainRotation: { x: 0, y: 0, z: 0 },
355363
centerSize: 1.33, largeWarpScale: 0.11, largeGrainStretch: 0.33, smallWarpStrength: 0.024,
356364
smallWarpScale: 2.48, fineWarpStrength: 0.01, fineWarpScale: 15.3, ringCount: 36,
357365
ringBias: 0.02, ringSizeVariance: 0.04, ringVarianceScale: 6.5, barkThickness: 0.09,
@@ -360,6 +368,7 @@ const woodParams = {
360368
},
361369
cedar: {
362370
originOffset: { x: -0.4, y: 0.1, z: 0.1 },
371+
grainRotation: { x: 0, y: 0, z: 0 },
363372
centerSize: 1.11, largeWarpScale: 0.39, largeGrainStretch: 0.12, smallWarpStrength: 0.061,
364373
smallWarpScale: 1.9, fineWarpStrength: 0.006, fineWarpScale: 4.8, ringCount: 25,
365374
ringBias: 0.01, ringSizeVariance: 0.07, ringVarianceScale: 6.7, barkThickness: 0.1,
@@ -368,6 +377,7 @@ const woodParams = {
368377
},
369378
mahogany: {
370379
originOffset: { x: -0.4, y: 0.2, z: 0 },
380+
grainRotation: { x: 0, y: 0, z: 0 },
371381
centerSize: 1.25, largeWarpScale: 0.26, largeGrainStretch: 0.29, smallWarpStrength: 0.044,
372382
smallWarpScale: 2.54, fineWarpStrength: 0.01, fineWarpScale: 15.3, ringCount: 38,
373383
ringBias: 0.01, ringSizeVariance: 0.33, ringVarianceScale: 1.2, barkThickness: 0.07,
@@ -405,7 +415,7 @@ export function GetWoodPreset( genus, finish ) {
405415

406416
}
407417

408-
return { ...params, genus, finish, clearcoat, clearcoatRoughness, clearcoatDarken };
418+
return { ...params, originOffset: new THREE.Vector3().copy( params.originOffset ), grainRotation: new THREE.Vector3().copy( params.grainRotation ), genus, finish, clearcoat, clearcoatRoughness, clearcoatDarken };
409419

410420
}
411421

@@ -430,9 +440,11 @@ uniforms.cellScale = TSL.uniform( params.cellScale ).onObjectUpdate( ( { materia
430440
uniforms.cellSize = TSL.uniform( params.cellSize ).onObjectUpdate( ( { material } ) => material.cellSize );
431441
uniforms.darkGrainColor = TSL.uniform( new THREE.Color( params.darkGrainColor ) ).onObjectUpdate( ( { material }, self ) => self.value.set( material.darkGrainColor ) );
432442
uniforms.lightGrainColor = TSL.uniform( new THREE.Color( params.lightGrainColor ) ).onObjectUpdate( ( { material }, self ) => self.value.set( material.lightGrainColor ) );
443+
uniforms.originOffset = TSL.uniform( new THREE.Vector3().copy( params.originOffset ) ).onObjectUpdate( ( { material } ) => material.originOffset );
444+
uniforms.grainRotation = TSL.uniform( new THREE.Vector3().copy( params.grainRotation ) ).onObjectUpdate( ( { material } ) => material.grainRotation );
433445

434446
const colorNode = wood(
435-
TSL.positionLocal.add( TSL.vec3( params.originOffset.x, params.originOffset.y, params.originOffset.z ) ),
447+
TSL.rotate( TSL.positionLocal.add( uniforms.originOffset ), uniforms.grainRotation ),
436448
uniforms.centerSize,
437449
uniforms.largeWarpScale,
438450
uniforms.largeGrainStretch,
@@ -453,7 +465,7 @@ const colorNode = wood(
453465
uniforms.lightGrainColor
454466
).mul( params.clearcoatDarken );
455467

456-
export class WoodNodeMaterial extends THREE.MeshPhysicalNodeMaterial {
468+
export class WoodNodeMaterial extends THREE.MeshPhysicalMaterial {
457469

458470
static get type() {
459471

@@ -492,4 +504,3 @@ export class WoodNodeMaterial extends THREE.MeshPhysicalNodeMaterial {
492504
}
493505

494506
}
495-

0 commit comments

Comments
 (0)