webgpu_postprocessing_outline.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - post processing - Outline Pass</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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - Outline Pass by <a href="http://eduperiment.com" target="_blank" rel="noopener">Prashant Sharma</a> and <a href="https://clara.io" target="_blank" rel="noopener">Ben Houston</a><br/><br/>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.webgpu.js",
  17. "three/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { pass, uniform, time, oscSine } from 'three/tsl';
  26. import { outline } from 'three/addons/tsl/display/OutlineNode.js';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  29. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  30. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  31. let container, stats;
  32. let camera, scene, renderer, controls;
  33. let postProcessing, outlinePass;
  34. let selectedObjects = [];
  35. const raycaster = new THREE.Raycaster();
  36. const mouse = new THREE.Vector2();
  37. const obj3d = new THREE.Object3D();
  38. const group = new THREE.Group();
  39. init();
  40. function init() {
  41. container = document.createElement( 'div' );
  42. document.body.appendChild( container );
  43. const width = window.innerWidth;
  44. const height = window.innerHeight;
  45. renderer = new THREE.WebGPURenderer();
  46. renderer.shadowMap.enabled = true;
  47. renderer.setPixelRatio( window.devicePixelRatio );
  48. renderer.setSize( width, height );
  49. renderer.setAnimationLoop( animate );
  50. document.body.appendChild( renderer.domElement );
  51. scene = new THREE.Scene();
  52. camera = new THREE.PerspectiveCamera( 45, width / height, 0.1, 100 );
  53. camera.position.set( 0, 0, 8 );
  54. controls = new OrbitControls( camera, renderer.domElement );
  55. controls.minDistance = 5;
  56. controls.maxDistance = 20;
  57. controls.enablePan = false;
  58. controls.enableDamping = true;
  59. controls.dampingFactor = 0.05;
  60. //
  61. scene.add( new THREE.AmbientLight( 0xaaaaaa, 0.6 ) );
  62. const light = new THREE.DirectionalLight( 0xddffdd, 2 );
  63. light.position.set( 5, 5, 5 );
  64. light.castShadow = true;
  65. light.shadow.mapSize.width = 2048;
  66. light.shadow.mapSize.height = 2048;
  67. light.shadow.bias = - 0.005;
  68. const d = 10;
  69. light.shadow.camera.left = - d;
  70. light.shadow.camera.right = d;
  71. light.shadow.camera.top = d;
  72. light.shadow.camera.bottom = - d;
  73. light.shadow.camera.far = 25;
  74. scene.add( light );
  75. // model
  76. const loader = new OBJLoader();
  77. loader.load( 'models/obj/tree.obj', function ( object ) {
  78. let scale = 1.0;
  79. object.traverse( function ( child ) {
  80. if ( child instanceof THREE.Mesh ) {
  81. child.geometry.center();
  82. child.geometry.computeBoundingSphere();
  83. scale = 0.2 * child.geometry.boundingSphere.radius;
  84. const phongMaterial = new THREE.MeshPhongMaterial( { color: 0xffffff, specular: 0x111111, shininess: 5 } );
  85. child.material = phongMaterial;
  86. child.receiveShadow = true;
  87. child.castShadow = true;
  88. }
  89. } );
  90. object.position.y = 1;
  91. object.scale.divideScalar( scale );
  92. obj3d.add( object );
  93. } );
  94. scene.add( group );
  95. group.add( obj3d );
  96. //
  97. const geometry = new THREE.SphereGeometry( 3, 48, 24 );
  98. for ( let i = 0; i < 20; i ++ ) {
  99. const material = new THREE.MeshLambertMaterial();
  100. material.color.setHSL( Math.random(), 1.0, 0.3 );
  101. const mesh = new THREE.Mesh( geometry, material );
  102. mesh.position.x = Math.random() * 4 - 2;
  103. mesh.position.y = Math.random() * 4 - 2;
  104. mesh.position.z = Math.random() * 4 - 2;
  105. mesh.receiveShadow = true;
  106. mesh.castShadow = true;
  107. mesh.scale.multiplyScalar( Math.random() * 0.3 + 0.1 );
  108. group.add( mesh );
  109. }
  110. const floorMaterial = new THREE.MeshLambertMaterial( { side: THREE.DoubleSide } );
  111. const floorGeometry = new THREE.PlaneGeometry( 12, 12 );
  112. const floorMesh = new THREE.Mesh( floorGeometry, floorMaterial );
  113. floorMesh.rotation.x -= Math.PI * 0.5;
  114. floorMesh.position.y -= 1.5;
  115. group.add( floorMesh );
  116. floorMesh.receiveShadow = true;
  117. const torusGeometry = new THREE.TorusGeometry( 1, 0.3, 16, 100 );
  118. const torusMaterial = new THREE.MeshPhongMaterial( { color: 0xffaaff } );
  119. const torus = new THREE.Mesh( torusGeometry, torusMaterial );
  120. torus.position.z = - 4;
  121. group.add( torus );
  122. torus.receiveShadow = true;
  123. torus.castShadow = true;
  124. //
  125. stats = new Stats();
  126. container.appendChild( stats.dom );
  127. // outline pass
  128. const edgeStrength = uniform( 3.0 );
  129. const edgeGlow = uniform( 0.0 );
  130. const edgeThickness = uniform( 1.0 );
  131. const pulsePeriod = uniform( 0 );
  132. const visibleEdgeColor = uniform( new THREE.Color( 0xffffff ) );
  133. const hiddenEdgeColor = uniform( new THREE.Color( 0x4e3636 ) );
  134. outlinePass = outline( scene, camera, {
  135. selectedObjects,
  136. edgeGlow,
  137. edgeThickness
  138. } );
  139. const { visibleEdge, hiddenEdge } = outlinePass;
  140. const period = time.div( pulsePeriod ).mul( 2 );
  141. const osc = oscSine( period ).mul( .5 ).add( .5 ); // osc [ 0.5, 1.0 ]
  142. const outlineColor = visibleEdge.mul( visibleEdgeColor ).add( hiddenEdge.mul( hiddenEdgeColor ) ).mul( edgeStrength );
  143. const outlinePulse = pulsePeriod.greaterThan( 0 ).select( outlineColor.mul( osc ), outlineColor );
  144. // postprocessing
  145. const scenePass = pass( scene, camera );
  146. postProcessing = new THREE.PostProcessing( renderer );
  147. postProcessing.outputNode = outlinePulse.add( scenePass );
  148. // gui
  149. const gui = new GUI( { width: 280 } );
  150. gui.add( edgeStrength, 'value', 0.01, 10 ).name( 'edgeStrength' );
  151. gui.add( edgeGlow, 'value', 0.0, 1 ).name( 'edgeGlow' );
  152. gui.add( edgeThickness, 'value', 1, 4 ).name( 'edgeThickness' );
  153. gui.add( pulsePeriod, 'value', 0.0, 5 ).name( 'pulsePeriod' );
  154. gui.addColor( { color: visibleEdgeColor.value.getHex( THREE.SRGBColorSpace ) }, 'color' ).onChange( ( value ) => {
  155. visibleEdgeColor.value.set( value );
  156. } ).name( 'visibleEdgeColor' );
  157. gui.addColor( { color: hiddenEdgeColor.value.getHex( THREE.SRGBColorSpace ) }, 'color' ).onChange( ( value ) => {
  158. hiddenEdgeColor.value.set( value );
  159. } ).name( 'hiddenEdgeColor' );
  160. //
  161. window.addEventListener( 'resize', onWindowResize );
  162. renderer.domElement.style.touchAction = 'none';
  163. renderer.domElement.addEventListener( 'pointermove', onPointerMove );
  164. function onPointerMove( event ) {
  165. if ( event.isPrimary === false ) return;
  166. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  167. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  168. checkIntersection();
  169. }
  170. function addSelectedObject( object ) {
  171. selectedObjects = [];
  172. selectedObjects.push( object );
  173. }
  174. function checkIntersection() {
  175. raycaster.setFromCamera( mouse, camera );
  176. const intersects = raycaster.intersectObject( scene, true );
  177. if ( intersects.length > 0 ) {
  178. const selectedObject = intersects[ 0 ].object;
  179. addSelectedObject( selectedObject );
  180. outlinePass.selectedObjects = selectedObjects;
  181. } else {
  182. // outlinePass.selectedObjects = [];
  183. }
  184. }
  185. }
  186. function onWindowResize() {
  187. const width = window.innerWidth;
  188. const height = window.innerHeight;
  189. camera.aspect = width / height;
  190. camera.updateProjectionMatrix();
  191. renderer.setSize( width, height );
  192. }
  193. function animate() {
  194. stats.begin();
  195. controls.update();
  196. postProcessing.render();
  197. stats.end();
  198. }
  199. </script>
  200. </body>
  201. </html>
粤ICP备19079148号