webgpu_materials_retroreflective.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 cone with a retroreflective band and a front-facing 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 { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  35. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  36. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  37. let camera, scene, renderer;
  38. let frontLight, bandMaterial;
  39. const params = {
  40. retroreflectivity: true,
  41. retroreflective: 1.0,
  42. frontLightIntensity: 5.0
  43. };
  44. init();
  45. function init() {
  46. if ( WebGPU.isAvailable() === false ) {
  47. document.body.appendChild( WebGPU.getErrorMessage() );
  48. throw new Error( 'No WebGPU support' );
  49. }
  50. const container = document.createElement( 'div' );
  51. document.body.appendChild( container );
  52. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 0.1, 100 );
  53. camera.position.set( 4, 2.5, 6 );
  54. scene = new THREE.Scene();
  55. scene.background = new THREE.Color( 0x11151d );
  56. scene.add( camera );
  57. scene.add( new THREE.HemisphereLight( 0xffffff, 0x223344, 0.8 ) );
  58. frontLight = new THREE.PointLight( 0xffffff, params.frontLightIntensity, 18, 1.4 );
  59. camera.add( frontLight );
  60. createCone();
  61. const grid = new THREE.GridHelper( 8, 16, 0x344055, 0x202838 );
  62. scene.add( grid );
  63. renderer = new THREE.WebGPURenderer( { antialias: true } );
  64. renderer.setPixelRatio( window.devicePixelRatio );
  65. renderer.setSize( window.innerWidth, window.innerHeight );
  66. renderer.setAnimationLoop( render );
  67. container.appendChild( renderer.domElement );
  68. const controls = new OrbitControls( camera, renderer.domElement );
  69. controls.target.set( 0, 1.5, 0 );
  70. controls.update();
  71. const gui = new GUI();
  72. gui.add( params, 'retroreflectivity' ).name( 'retroreflectivity' ).onChange( updateMaterial );
  73. gui.add( params, 'retroreflective', 0, 1, 0.01 ).name( 'retroreflective' ).onChange( updateMaterial );
  74. gui.add( params, 'frontLightIntensity', 0, 30, 0.1 ).name( 'front light intensity' ).onChange( updateMaterial );
  75. window.addEventListener( 'resize', onWindowResize );
  76. updateMaterial();
  77. }
  78. function createCone() {
  79. const cone = new THREE.Group();
  80. scene.add( cone );
  81. const orangeMaterial = new THREE.MeshPhysicalNodeMaterial( {
  82. color: 0xff6a00,
  83. roughness: 0.45,
  84. metalness: 0.0
  85. } );
  86. bandMaterial = new THREE.MeshPhysicalNodeMaterial( {
  87. color: 0xf7f0d6,
  88. roughness: 0.22,
  89. metalness: 0.0,
  90. retroreflective: params.retroreflective
  91. } );
  92. const base = new THREE.Mesh(
  93. new THREE.BoxGeometry( 2.6, 0.24, 2.6 ),
  94. orangeMaterial
  95. );
  96. base.position.y = 0.12;
  97. cone.add( base );
  98. const bodyHeight = 3.2;
  99. const bodyBottom = 0.24;
  100. const bodyRadiusBottom = 1.0;
  101. const bodyRadiusTop = 0.18;
  102. const body = new THREE.Mesh(
  103. new THREE.CylinderGeometry( bodyRadiusTop, bodyRadiusBottom, bodyHeight, 96, 1, false ),
  104. orangeMaterial
  105. );
  106. body.position.y = bodyBottom + bodyHeight * 0.5;
  107. cone.add( body );
  108. const bandHeight = 0.48;
  109. const bandCenter = bodyBottom + bodyHeight * 0.52;
  110. const bandBottom = bandCenter - bandHeight * 0.5;
  111. const bandTop = bandCenter + bandHeight * 0.5;
  112. const bandRadiusBottom = radiusAtHeight( bandBottom, bodyBottom, bodyHeight, bodyRadiusBottom, bodyRadiusTop ) * 1.01;
  113. const bandRadiusTop = radiusAtHeight( bandTop, bodyBottom, bodyHeight, bodyRadiusBottom, bodyRadiusTop ) * 1.01;
  114. const band = new THREE.Mesh(
  115. new THREE.CylinderGeometry( bandRadiusTop, bandRadiusBottom, bandHeight, 96, 1, true ),
  116. bandMaterial
  117. );
  118. band.position.y = bandCenter;
  119. cone.add( band );
  120. }
  121. function radiusAtHeight( y, bodyBottom, bodyHeight, radiusBottom, radiusTop ) {
  122. const t = THREE.MathUtils.clamp( ( y - bodyBottom ) / bodyHeight, 0, 1 );
  123. return THREE.MathUtils.lerp( radiusBottom, radiusTop, t );
  124. }
  125. function updateMaterial() {
  126. bandMaterial.retroreflective = params.retroreflectivity ? params.retroreflective : 0;
  127. frontLight.intensity = params.frontLightIntensity;
  128. }
  129. function onWindowResize() {
  130. camera.aspect = window.innerWidth / window.innerHeight;
  131. camera.updateProjectionMatrix();
  132. renderer.setSize( window.innerWidth, window.innerHeight );
  133. }
  134. function render() {
  135. renderer.render( scene, camera );
  136. }
  137. </script>
  138. </body>
  139. </html>
粤ICP备19079148号