Skip to content

Commit d71fc38

Browse files
committed
Examples: Improved grid in materialx example.
1 parent 10a2144 commit d71fc38

File tree

2 files changed

+14
-13
lines changed

2 files changed

+14
-13
lines changed
-16.2 KB
Loading

examples/webgpu_loader_materialx.html

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
import * as THREE from 'three/webgpu';
3434

35-
import { Fn, length, fract, vec4, positionWorld, smoothstep, max, abs, float, cameraPosition, clamp } from 'three/tsl';
35+
import { Fn, length, fract, vec4, positionWorld, smoothstep, max, abs, float, fwidth } from 'three/tsl';
3636

3737
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
3838

@@ -119,22 +119,23 @@
119119

120120
// Ground plane
121121

122-
const material = new THREE.MeshStandardNodeMaterial();
122+
const material = new THREE.MeshBasicNodeMaterial();
123123

124124
const gridXZ = Fn( ( [ gridSize = float( 1.0 ), dotWidth = float( 0.1 ), lineWidth = float( 0.02 ) ] ) => {
125125

126126
const worldPos = positionWorld;
127-
const grid = fract( worldPos.xz.div( gridSize ) );
127+
const coord = worldPos.xz.div( gridSize );
128+
const grid = fract( coord );
128129

129-
// Distance-based antialiasing
130-
const distToCamera = length( worldPos.sub( cameraPosition ) );
131-
const smoothing = clamp( distToCamera.div( 100.0 ), 0.01, 0.02 );
130+
// Screen-space derivative for automatic antialiasing
131+
const fw = fwidth( coord );
132+
const smoothing = max( fw.x, fw.y ).mul( 0.5 );
132133

133-
// Create dots at cell centers
134-
const dotDist = length( grid.sub( 0.5 ) );
135-
const dots = smoothstep( dotWidth.add( smoothing ), dotWidth.sub( smoothing ), dotDist );
134+
// Create squares at cell centers
135+
const squareDist = max( abs( grid.x.sub( 0.5 ) ), abs( grid.y.sub( 0.5 ) ) );
136+
const dots = smoothstep( dotWidth.add( smoothing ), dotWidth.sub( smoothing ), squareDist );
136137

137-
// Create grid lines
138+
// Create grid lines with derivative-based antialiasing
138139
const lineX = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.x.sub( 0.5 ) ) );
139140
const lineZ = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.y.sub( 0.5 ) ) );
140141
const lines = max( lineX, lineZ );
@@ -150,15 +151,15 @@
150151
} );
151152

152153
// Create grid pattern
153-
const gridPattern = gridXZ( 1.0, 0.04, 0.01 );
154+
const gridPattern = gridXZ( 1.0, 0.03, 0.005 );
154155
const baseColor = vec4( 1.0, 1.0, 1.0, 0.0 );
155-
const gridColor = vec4( 0.2, 0.2, 0.2, 1.0 );
156+
const gridColor = vec4( 0.5, 0.5, 0.5, 1.0 );
156157

157158
// Mix base color with grid lines
158159
material.colorNode = gridPattern.mix( baseColor, gridColor ).mul( radialGradient( 30.0, 20.0 ) );
159160
material.transparent = true;
160161

161-
const plane = new THREE.Mesh( new THREE.CircleGeometry( 50 ), material );
162+
const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), material );
162163
plane.rotation.x = - Math.PI / 2;
163164
plane.renderOrder = - 1;
164165
scene.add( plane );

0 commit comments

Comments
 (0)