webgpu_postprocessing_outline.html 7.6 KB

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