webgpu_lights_pointlights.html 7.0 KB

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