1
0

webgpu_materials_retroreflective.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - materials - retroreflective</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <meta property="og:title" content="three.js webgpu - materials - retroreflective">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_materials_retroreflective.html">
  10. <link type="text/css" rel="stylesheet" href="example.css">
  11. </head>
  12. <body>
  13. <div id="info">
  14. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  15. <div class="title-wrapper">
  16. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Retroreflective Materials</span>
  17. </div>
  18. <small>
  19. Traffic cones with retroreflective bands and a camera-mounted light demonstrating the <a href="https://jcgt.org/published/0015/01/04/" target="_blank" rel="noopener">Minimal Retroreflective Microfacet Model</a>. By <a href="https://ben3d.ca" target="_blank" rel="noopener">Ben Houston</a>.
  20. </small>
  21. </div>
  22. <script type="importmap">
  23. {
  24. "imports": {
  25. "three": "../build/three.webgpu.js",
  26. "three/webgpu": "../build/three.webgpu.js",
  27. "three/tsl": "../build/three.tsl.js",
  28. "three/addons/": "./jsm/"
  29. }
  30. }
  31. </script>
  32. <script type="module">
  33. import * as THREE from 'three/webgpu';
  34. import { color, mix, mx_noise_float, pass, positionLocal, positionWorld, materialColor, materialRoughness, reflector, transformNormalToView, vec2, vec3 } from 'three/tsl';
  35. import { bloom } from 'three/addons/tsl/display/BloomNode.js';
  36. import { hashBlur } from 'three/addons/tsl/display/hashBlur.js';
  37. import { Inspector } from 'three/addons/inspector/Inspector.js';
  38. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  39. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  40. let camera, scene, renderer, renderPipeline, controls;
  41. let frontLight, reflectedFlashLight, bandMaterial;
  42. const params = {
  43. enabled: true,
  44. amount: 1.0,
  45. flashLight: 5.0
  46. };
  47. init();
  48. async function init() {
  49. if ( WebGPU.isAvailable() === false ) {
  50. document.body.appendChild( WebGPU.getErrorMessage() );
  51. throw new Error( 'No WebGPU support' );
  52. }
  53. const container = document.createElement( 'div' );
  54. document.body.appendChild( container );
  55. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  56. camera.position.set( 2.3, 1.32, 3.25 );
  57. scene = new THREE.Scene();
  58. scene.background = new THREE.Color( 0x0f131a );
  59. scene.fog = new THREE.Fog( 0x0f131a, 4, 18 );
  60. scene.add( camera );
  61. // camera-mounted LED flashlight (~6500K = sRGB white)
  62. frontLight = new THREE.PointLight( 0xffffff, params.flashLight, 12 );
  63. camera.add( frontLight );
  64. // the flashlight's mirror image below the ground: it stands in for the
  65. // puddle-bounced beam, so the retro bands also light up in the reflection
  66. reflectedFlashLight = new THREE.PointLight( 0xffffff, params.flashLight * 0.35, 12 );
  67. scene.add( reflectedFlashLight );
  68. createFloor();
  69. createCones();
  70. renderer = new THREE.WebGPURenderer( { antialias: true } );
  71. renderer.toneMapping = THREE.ACESFilmicToneMapping;
  72. renderer.setPixelRatio( window.devicePixelRatio );
  73. renderer.setSize( window.innerWidth, window.innerHeight );
  74. renderer.setAnimationLoop( render );
  75. renderer.inspector = new Inspector();
  76. container.appendChild( renderer.domElement );
  77. await renderer.init();
  78. const pmremGenerator = new THREE.PMREMGenerator( renderer );
  79. scene.environment = pmremGenerator.fromScene( createNightEnvironment(), 0.02 ).texture;
  80. renderPipeline = new THREE.RenderPipeline( renderer );
  81. const scenePass = pass( scene, camera );
  82. const scenePassColor = scenePass.getTextureNode();
  83. renderPipeline.outputNode = scenePassColor.add( bloom( scenePassColor, 0.15, 0, 0 ) );
  84. controls = new OrbitControls( camera, renderer.domElement );
  85. controls.target.set( 0, 0.33, 0 );
  86. controls.maxPolarAngle = Math.PI / 2 - 0.05; // keep the camera above the ground
  87. controls.enableDamping = true;
  88. controls.update();
  89. const gui = renderer.inspector.createParameters( 'Settings' );
  90. gui.add( params, 'enabled' ).onChange( updateMaterial );
  91. gui.add( params, 'amount', 0, 1, 0.01 ).onChange( updateMaterial );
  92. gui.add( params, 'flashLight', 0, 30, 0.1 ).name( 'flash light' ).onChange( ( value ) => frontLight.intensity = value );
  93. window.addEventListener( 'resize', onWindowResize );
  94. updateMaterial();
  95. }
  96. function createNightEnvironment() {
  97. const environment = new THREE.Scene();
  98. // sky: faint city glow at the horizon fading to a dark zenith
  99. const skyMaterial = new THREE.MeshBasicNodeMaterial( { side: THREE.BackSide } );
  100. skyMaterial.colorNode = mix( color( 0x05070c ), color( 0x2a3148 ), positionLocal.normalize().y.abs().oneMinus().pow( 4 ) );
  101. environment.add( new THREE.Mesh( new THREE.SphereGeometry( 10 ), skyMaterial ) );
  102. // distant buildings with sparse lit windows
  103. const buildingMaterial = new THREE.MeshBasicNodeMaterial();
  104. const cell = vec2( positionWorld.x.add( positionWorld.z.mul( 1.7 ) ).mul( 3 ), positionWorld.y.mul( 2.5 ) );
  105. const cellUV = cell.fract();
  106. const window_ = cellUV.x.smoothstep( 0.15, 0.3 ).mul( cellUV.x.smoothstep( 0.7, 0.85 ).oneMinus() ).mul( cellUV.y.smoothstep( 0.2, 0.35 ) ).mul( cellUV.y.smoothstep( 0.65, 0.8 ).oneMinus() );
  107. const lit = mx_noise_float( cell.floor().mul( 0.731 ).add( 0.37 ) ).smoothstep( 0.0, 0.1 );
  108. const windowColor = mix( color( 0x88a8ff ), color( 0xffc16b ), mx_noise_float( cell.floor().mul( 0.317 ) ).add( 1 ).mul( 0.5 ) );
  109. buildingMaterial.colorNode = mix( color( 0x05060a ), windowColor.mul( 12 ), window_.mul( lit ) );
  110. for ( let i = 0; i < 8; i ++ ) {
  111. const angle = ( i / 8 ) * Math.PI * 2 + 0.35;
  112. const height = 2.5 + ( ( i * 2.7 ) % 3.5 );
  113. const building = new THREE.Mesh( new THREE.BoxGeometry( 2, height, 1 ), buildingMaterial );
  114. building.position.set( Math.cos( angle ) * 9, height / 2 - 0.5, Math.sin( angle ) * 9 );
  115. building.lookAt( 0, height / 2 - 0.5, 0 );
  116. environment.add( building );
  117. }
  118. // street lamps: sodium and LED
  119. const warmLampMaterial = new THREE.MeshBasicMaterial();
  120. warmLampMaterial.color.setRGB( 18, 11, 4 );
  121. const coolLampMaterial = new THREE.MeshBasicMaterial();
  122. coolLampMaterial.color.setRGB( 7, 9, 13 );
  123. for ( let i = 0; i < 6; i ++ ) {
  124. const angle = ( i / 6 ) * Math.PI * 2 + 0.7;
  125. const lamp = new THREE.Mesh( new THREE.SphereGeometry( 0.35 ), i % 3 === 2 ? coolLampMaterial : warmLampMaterial );
  126. lamp.position.set( Math.cos( angle ) * 8, 2.5 + ( i % 3 ), Math.sin( angle ) * 8 );
  127. environment.add( lamp );
  128. }
  129. // moon
  130. const moonMaterial = new THREE.MeshBasicMaterial();
  131. moonMaterial.color.setRGB( 2.5, 3, 4 );
  132. const moon = new THREE.Mesh( new THREE.SphereGeometry( 0.6 ), moonMaterial );
  133. moon.position.set( 4, 7, - 5 );
  134. environment.add( moon );
  135. return environment;
  136. }
  137. function createFloor() {
  138. const material = new THREE.MeshStandardNodeMaterial( {
  139. color: 0x1b1e23,
  140. roughness: 0.6
  141. } );
  142. const reflection = reflector( { resolutionScale: 0.5 } );
  143. reflection.target.rotateX( - Math.PI / 2 );
  144. scene.add( reflection.target );
  145. const position = positionWorld.xz;
  146. // fade procedural detail as its period approaches the pixel footprint,
  147. // which grows anisotropically at grazing angles and with distance
  148. const bandlimit = ( p ) => p.fwidth().length().smoothstep( 0.5, 1.5 ).oneMinus();
  149. // asphalt: tonal grain and broad worn patches
  150. const grain = mx_noise_float( position.mul( 60 ) ).mul( bandlimit( position.mul( 60 ) ) ).mul( 0.2 );
  151. const patches = mx_noise_float( position.mul( 1.5 ) ).mul( 0.08 );
  152. const asphalt = materialColor.mul( grain.add( patches ).add( 1 ) );
  153. // puddles: flat, dark and mirror-like
  154. const puddle = mx_noise_float( position.mul( 0.45 ).add( 6.27 ) ).smoothstep( 0.25, 0.45 );
  155. // bump: heightfield normal from finite differences of a fine noise, flattened in puddles
  156. const bumpNoise = ( p ) => mx_noise_float( p.mul( 180 ) );
  157. const e = 0.002;
  158. const strength = puddle.oneMinus().mul( bandlimit( position.mul( 180 ) ) ).mul( 0.0015 );
  159. const h = bumpNoise( position );
  160. const hx = bumpNoise( position.add( vec2( e, 0 ) ) );
  161. const hz = bumpNoise( position.add( vec2( 0, e ) ) );
  162. const bump = vec3( h.sub( hx ).mul( strength ), hz.sub( h ).mul( strength ), e ).normalize();
  163. material.normalNode = transformNormalToView( bump );
  164. // the surface relief wobbles the reflection; puddles stay calm
  165. reflection.uvNode = reflection.uvNode.add( bump.xy.mul( 0.03 ) );
  166. material.colorNode = mix( asphalt, asphalt.mul( 0.3 ), puddle );
  167. material.roughnessNode = mix( mx_noise_float( position.mul( 50 ) ).mul( bandlimit( position.mul( 50 ) ) ).mul( 0.15 ).add( materialRoughness ), 0.05, puddle );
  168. // rain-soaked: the whole surface reflects, streaky on wet asphalt and sharpest in the puddles
  169. const wetness = mix( 0.15, 0.7, puddle );
  170. const reflectionBlur = mix( 0.08, 0.005, puddle );
  171. material.emissiveNode = hashBlur( reflection, reflectionBlur, { repeats: 32 } ).rgb.mul( wetness );
  172. const floor = new THREE.Mesh( new THREE.PlaneGeometry( 40, 40 ), material );
  173. floor.rotation.x = - Math.PI / 2;
  174. scene.add( floor );
  175. }
  176. function createCones() {
  177. const orangeMaterial = new THREE.MeshPhysicalNodeMaterial( {
  178. color: 0xff6a00,
  179. roughness: 0.45
  180. } );
  181. // worn plastic: uncorrelated noise on roughness and tone
  182. orangeMaterial.roughnessNode = mx_noise_float( positionLocal.mul( 30 ) ).mul( 0.2 ).add( materialRoughness );
  183. orangeMaterial.colorNode = materialColor.mul( mx_noise_float( positionLocal.add( 7.3 ).mul( 20 ) ).mul( 0.1 ).add( 1 ) );
  184. bandMaterial = new THREE.MeshPhysicalNodeMaterial( {
  185. color: 0xf7f0d6,
  186. roughness: 0.22,
  187. retroreflective: params.amount
  188. } );
  189. const count = 5;
  190. const radius = 1.1;
  191. for ( let i = 0; i < count; i ++ ) {
  192. const cone = createCone( orangeMaterial, bandMaterial );
  193. const angle = ( i / count ) * Math.PI * 2;
  194. cone.position.set( Math.cos( angle ) * radius, 0, Math.sin( angle ) * radius );
  195. scene.add( cone );
  196. }
  197. }
  198. function createCone( orangeMaterial, bandMaterial ) {
  199. const cone = new THREE.Group();
  200. const base = new THREE.Mesh(
  201. createBaseGeometry( 0.42, 0.04, 0.08 ),
  202. orangeMaterial
  203. );
  204. cone.add( base );
  205. const bodyHeight = 0.71;
  206. const bodyBottom = 0.04;
  207. const bodyRadiusBottom = 0.17;
  208. const bodyRadiusTop = 0.035;
  209. const body = new THREE.Mesh(
  210. new THREE.CylinderGeometry( bodyRadiusTop, bodyRadiusBottom, bodyHeight, 96, 1, false ),
  211. orangeMaterial
  212. );
  213. body.position.y = bodyBottom + bodyHeight * 0.5;
  214. cone.add( body );
  215. const bands = [
  216. { center: bodyBottom + bodyHeight * 0.6, height: 0.10 },
  217. { center: bodyBottom + bodyHeight * 0.4, height: 0.06 }
  218. ];
  219. for ( const { center, height } of bands ) {
  220. const bandRadiusBottom = radiusAtHeight( center - height * 0.5, bodyBottom, bodyHeight, bodyRadiusBottom, bodyRadiusTop ) * 1.01;
  221. const bandRadiusTop = radiusAtHeight( center + height * 0.5, bodyBottom, bodyHeight, bodyRadiusBottom, bodyRadiusTop ) * 1.01;
  222. const band = new THREE.Mesh(
  223. new THREE.CylinderGeometry( bandRadiusTop, bandRadiusBottom, height, 96, 1, true ),
  224. bandMaterial
  225. );
  226. band.position.y = center;
  227. cone.add( band );
  228. }
  229. return cone;
  230. }
  231. function radiusAtHeight( y, bodyBottom, bodyHeight, radiusBottom, radiusTop ) {
  232. const t = THREE.MathUtils.clamp( ( y - bodyBottom ) / bodyHeight, 0, 1 );
  233. return THREE.MathUtils.lerp( radiusBottom, radiusTop, t );
  234. }
  235. function createBaseGeometry( size, thickness, radius ) {
  236. const half = size / 2;
  237. const shape = new THREE.Shape();
  238. shape.moveTo( - half + radius, - half );
  239. shape.lineTo( half - radius, - half );
  240. shape.absarc( half - radius, - half + radius, radius, - Math.PI / 2, 0 );
  241. shape.lineTo( half, half - radius );
  242. shape.absarc( half - radius, half - radius, radius, 0, Math.PI / 2 );
  243. shape.lineTo( - half + radius, half );
  244. shape.absarc( - half + radius, half - radius, radius, Math.PI / 2, Math.PI );
  245. shape.lineTo( - half, - half + radius );
  246. shape.absarc( - half + radius, - half + radius, radius, Math.PI, Math.PI * 1.5 );
  247. const geometry = new THREE.ExtrudeGeometry( shape, { depth: thickness, bevelEnabled: false } );
  248. geometry.rotateX( - Math.PI / 2 );
  249. return geometry;
  250. }
  251. function updateMaterial() {
  252. bandMaterial.retroreflective = params.enabled ? params.amount : 0;
  253. }
  254. function onWindowResize() {
  255. camera.aspect = window.innerWidth / window.innerHeight;
  256. camera.updateProjectionMatrix();
  257. renderer.setSize( window.innerWidth, window.innerHeight );
  258. }
  259. function render() {
  260. controls.update();
  261. reflectedFlashLight.position.set( camera.position.x, - camera.position.y, camera.position.z );
  262. reflectedFlashLight.intensity = frontLight.intensity * 0.35;
  263. renderPipeline.render();
  264. }
  265. </script>
  266. </body>
  267. </html>
粤ICP备19079148号