webgpu_lights_pointlights.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - point lights</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 - point lights">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_lights_pointlights.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_lights_pointlights.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Point Lights</span>
  18. </div>
  19. <small>
  20. Walt Disney head by <a href="http://web.archive.org/web/20120903131400/http://davidoreilly.com/post/18087489343/disneyhead" target="_blank" rel="noopener">David OReilly</a><br />
  21. Displacement effect by <a href="https://oosmoxiecode.com/archive/js_webgl/stanford_bunny/" target="_blank" rel="noopener">oosmoxiecode</a>
  22. </small>
  23. </div>
  24. <script type="importmap">
  25. {
  26. "imports": {
  27. "three": "../build/three.webgpu.js",
  28. "three/webgpu": "../build/three.webgpu.js",
  29. "three/tsl": "../build/three.tsl.js",
  30. "three/addons/": "./jsm/"
  31. }
  32. }
  33. </script>
  34. <script type="module">
  35. import * as THREE from 'three/webgpu';
  36. import { OBJLoader } from 'three/addons/loaders/OBJLoader.js';
  37. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  38. import { abs, attribute, distance, float, max, modelWorldMatrixInverse, positionLocal, sin, time, uniform } from 'three/tsl';
  39. let camera, scene, timer, renderer, controls;
  40. let light1, light2;
  41. init();
  42. function init() {
  43. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 1, 1000 );
  44. camera.position.z = 100;
  45. scene = new THREE.Scene();
  46. scene.background = new THREE.Color( 0x000000 );
  47. timer = new THREE.Timer();
  48. timer.connect( document );
  49. // model
  50. const loader = new OBJLoader();
  51. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  52. const mesh = obj.children[ 0 ];
  53. mesh.geometry = createGeometry( mesh.geometry );
  54. mesh.material = createMaterial();
  55. mesh.scale.multiplyScalar( 0.8 );
  56. mesh.position.y = - 30;
  57. scene.add( mesh );
  58. } );
  59. const sphere = new THREE.SphereGeometry( 0.5, 16, 8 );
  60. // lights
  61. light1 = new THREE.PointLight( 0xff0040, 2000 );
  62. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  63. scene.add( light1 );
  64. light2 = new THREE.PointLight( 0x0040ff, 2000 );
  65. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  66. scene.add( light2 );
  67. scene.add( new THREE.AmbientLight( 0xaaaaaa, 0.1 ) );
  68. // renderer
  69. renderer = new THREE.WebGPURenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.setAnimationLoop( animate );
  73. document.body.appendChild( renderer.domElement );
  74. controls = new OrbitControls( camera, renderer.domElement );
  75. controls.enableDamping = true;
  76. window.addEventListener( 'resize', onWindowResize );
  77. }
  78. function onWindowResize() {
  79. camera.aspect = window.innerWidth / window.innerHeight;
  80. camera.updateProjectionMatrix();
  81. renderer.setSize( window.innerWidth, window.innerHeight );
  82. }
  83. function animate() {
  84. timer.update();
  85. const time = timer.getElapsed() * 0.5;
  86. controls.update();
  87. light1.position.x = Math.sin( time ) * 20;
  88. light1.position.y = Math.cos( time * 0.75 ) * - 30;
  89. light1.position.z = Math.cos( time * 0.5 ) * 20;
  90. light2.position.x = Math.cos( time * 0.5 ) * 20;
  91. light2.position.y = Math.sin( time * 0.75 ) * - 30;
  92. light2.position.z = Math.sin( time ) * 20;
  93. renderer.render( scene, camera );
  94. }
  95. // helpers
  96. function createMaterial() {
  97. const material = new THREE.MeshPhongNodeMaterial();
  98. const seedAttribute = attribute( 'seed' );
  99. const displaceNormalAttribute = attribute( 'displaceNormal' );
  100. const localTime = attribute( 'time' ).add( time );
  101. const effector1 = uniform( light1.position ).toVar();
  102. const effector2 = uniform( light2.position ).toVar();
  103. const distance1 = distance( positionLocal, modelWorldMatrixInverse.mul( effector1 ) );
  104. const distance2 = distance( positionLocal, modelWorldMatrixInverse.mul( effector2 ) );
  105. const invDistance1 = max( 0.0, float( 20.0 ).sub( distance1 ) ).div( 2.0 );
  106. const invDistance2 = max( 0.0, float( 20.0 ).sub( distance2 ) ).div( 2.0 );
  107. const s = abs( sin( localTime.mul( 2 ).add( seedAttribute ) ).mul( 0.5 ) ).add( invDistance1 ).add( invDistance2 );
  108. material.positionNode = positionLocal.add( displaceNormalAttribute.mul( s ) );
  109. return material;
  110. }
  111. function createGeometry( geometry ) {
  112. const positionAttribute = geometry.getAttribute( 'position' );
  113. const v0 = new THREE.Vector3();
  114. const v1 = new THREE.Vector3();
  115. const v2 = new THREE.Vector3();
  116. const v3 = new THREE.Vector3();
  117. const n = new THREE.Vector3();
  118. const plane = new THREE.Plane();
  119. const vertices = [];
  120. const times = [];
  121. const seeds = [];
  122. const displaceNormal = [];
  123. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  124. v0.fromBufferAttribute( positionAttribute, i );
  125. v1.fromBufferAttribute( positionAttribute, i + 1 );
  126. v2.fromBufferAttribute( positionAttribute, i + 2 );
  127. plane.setFromCoplanarPoints( v0, v1, v2 );
  128. v3.copy( v0 ).add( v1 ).add( v2 ).divideScalar( 3 ); // compute center
  129. v3.add( n.copy( plane.normal ).multiplyScalar( - 1 ) ); // displace center inwards
  130. // generate tetrahedron for each triangle
  131. vertices.push( v0.x, v0.y, v0.z, v1.x, v1.y, v1.z, v2.x, v2.y, v2.z );
  132. vertices.push( v3.x, v3.y, v3.z, v1.x, v1.y, v1.z, v0.x, v0.y, v0.z );
  133. vertices.push( v3.x, v3.y, v3.z, v2.x, v2.y, v2.z, v1.x, v1.y, v1.z );
  134. vertices.push( v3.x, v3.y, v3.z, v0.x, v0.y, v0.z, v2.x, v2.y, v2.z );
  135. const t = Math.random();
  136. const s = Math.random();
  137. n.copy( plane.normal );
  138. times.push( t, t, t ); times.push( t, t, t ); times.push( t, t, t ); times.push( t, t, t );
  139. seeds.push( s, s, s ); seeds.push( s, s, s ); seeds.push( s, s, s ); seeds.push( s, s, s );
  140. displaceNormal.push( n.x, n.y, n.z, n.x, n.y, n.z, n.x, n.y, n.z );
  141. displaceNormal.push( n.x, n.y, n.z, n.x, n.y, n.z, n.x, n.y, n.z );
  142. displaceNormal.push( n.x, n.y, n.z, n.x, n.y, n.z, n.x, n.y, n.z );
  143. displaceNormal.push( n.x, n.y, n.z, n.x, n.y, n.z, n.x, n.y, n.z );
  144. }
  145. const newGeometry = new THREE.BufferGeometry();
  146. newGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  147. newGeometry.setAttribute( 'time', new THREE.Float32BufferAttribute( times, 1 ) );
  148. newGeometry.setAttribute( 'seed', new THREE.Float32BufferAttribute( seeds, 1 ) );
  149. newGeometry.setAttribute( 'displaceNormal', new THREE.Float32BufferAttribute( displaceNormal, 3 ) );
  150. newGeometry.computeVertexNormals();
  151. return newGeometry;
  152. }
  153. </script>
  154. </body>
  155. </html>
粤ICP备19079148号