Skip to content

Commit 5cde3c7

Browse files
committed
Examples: Improved grid in webgpu_loader_materialx.
1 parent d8e70a4 commit 5cde3c7

2 files changed

Lines changed: 15 additions & 19 deletions

File tree

742 Bytes
Loading

examples/webgpu_loader_materialx.html

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040

4141
import * as THREE from 'three/webgpu';
4242

43-
import { Fn, length, fract, vec4, positionWorld, smoothstep, max, abs, float, fwidth } from 'three/tsl';
43+
import { Fn, abs, fract, fwidth, length, max, saturate, smoothstep, vec4, positionWorld, float } from 'three/tsl';
4444

4545
import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
4646

@@ -130,41 +130,37 @@
130130

131131
const material = new THREE.MeshBasicNodeMaterial();
132132

133-
const gridXZ = Fn( ( [ gridSize = float( 1.0 ), dotWidth = float( 0.1 ), lineWidth = float( 0.02 ) ] ) => {
133+
const grid = Fn( ( [ coord, lineWidth = float( 0.01 ), dotSize = float( 0.03 ) ] ) => {
134134

135-
const coord = positionWorld.xz.div( gridSize );
136-
const grid = fract( coord );
135+
// https://bgolus.medium.com/the-best-darn-grid-shader-yet-727f9278b9d8
137136

138-
// Screen-space derivative for automatic antialiasing
137+
const g = fract( coord );
139138
const fw = fwidth( coord );
140-
const smoothing = max( fw.x, fw.y ).mul( 0.5 );
139+
const gx = abs( g.x.sub( 0.5 ) );
140+
const gy = abs( g.y.sub( 0.5 ) );
141141

142-
// Create squares at cell centers
143-
const squareDist = max( abs( grid.x.sub( 0.5 ) ), abs( grid.y.sub( 0.5 ) ) );
144-
const dots = smoothstep( dotWidth.add( smoothing ), dotWidth.sub( smoothing ), squareDist );
142+
const lineX = saturate( lineWidth.sub( gx ).div( fw.x ).add( 0.5 ) );
143+
const lineY = saturate( lineWidth.sub( gy ).div( fw.y ).add( 0.5 ) );
144+
const lines = max( lineX, lineY );
145145

146-
// Create grid lines
147-
const lineX = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.x.sub( 0.5 ) ) );
148-
const lineZ = smoothstep( lineWidth.add( smoothing ), lineWidth.sub( smoothing ), abs( grid.y.sub( 0.5 ) ) );
149-
const lines = max( lineX, lineZ );
146+
const squareDist = max( gx, gy );
147+
const aa = max( fw.x, fw.y );
148+
const dots = smoothstep( dotSize.add( aa ), dotSize.sub( aa ), squareDist );
150149

151150
return max( dots, lines );
152151

153152
} );
154153

155-
const radialGradient = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
154+
const fade = Fn( ( [ radius = float( 10.0 ), falloff = float( 1.0 ) ] ) => {
156155

157156
return smoothstep( radius, radius.sub( falloff ), length( positionWorld ) );
158157

159158
} );
160159

161-
// Create grid pattern
162-
const gridPattern = gridXZ( 1.0, 0.03, 0.005 );
163-
const baseColor = vec4( 1.0, 1.0, 1.0, 0.0 );
164160
const gridColor = vec4( 0.5, 0.5, 0.5, 1.0 );
161+
const baseColor = vec4( 1.0, 1.0, 1.0, 0.0 );
165162

166-
// Mix base color with grid lines
167-
material.colorNode = gridPattern.mix( baseColor, gridColor ).mul( radialGradient( 30.0, 20.0 ) );
163+
material.colorNode = grid( positionWorld.xz, 0.007, 0.03 ).mix( baseColor, gridColor ).mul( fade( 30.0, 20.0 ) );
168164
material.transparent = true;
169165

170166
const plane = new THREE.Mesh( new THREE.CircleGeometry( 40 ), material );

0 commit comments

Comments
 (0)