1
0

webgpu_postprocessing_outline.html 7.9 KB

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