webgpu_lights_pointlights.html 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. timer = new THREE.Timer();
  43. timer.connect( document );
  44. // model
  45. const loader = new OBJLoader();
  46. loader.load( 'models/obj/walt/WaltHead.obj', function ( obj ) {
  47. const mesh = obj.children[ 0 ];
  48. mesh.geometry = createGeometry( mesh.geometry );
  49. mesh.material = createMaterial();
  50. mesh.scale.multiplyScalar( 0.8 );
  51. mesh.position.y = - 30;
  52. scene.add( mesh );
  53. } );
  54. const sphere = new THREE.SphereGeometry( 0.5, 16, 8 );
  55. // lights
  56. light1 = new THREE.PointLight( 0xff0040, 2000 );
  57. light1.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0xff0040 } ) ) );
  58. scene.add( light1 );
  59. light2 = new THREE.PointLight( 0x0040ff, 2000 );
  60. light2.add( new THREE.Mesh( sphere, new THREE.MeshBasicMaterial( { color: 0x0040ff } ) ) );
  61. scene.add( light2 );
  62. scene.add( new THREE.AmbientLight( 0xaaaaaa, 0.1 ) );
  63. // renderer
  64. renderer = new THREE.WebGPURenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. renderer.setAnimationLoop( animate );
  68. document.body.appendChild( renderer.domElement );
  69. controls = new OrbitControls( camera, renderer.domElement );
  70. controls.enableDamping = true;
  71. window.addEventListener( 'resize', onWindowResize );
  72. }
  73. function onWindowResize() {
  74. camera.aspect = window.innerWidth / window.innerHeight;
  75. camera.updateProjectionMatrix();
  76. renderer.setSize( window.innerWidth, window.innerHeight );
  77. }
  78. function animate() {
  79. timer.update();
  80. const time = timer.getElapsed() * 0.5;
  81. controls.update();
  82. light1.position.x = Math.sin( time ) * 20;
  83. light1.position.y = Math.cos( time * 0.75 ) * - 30;
  84. light1.position.z = Math.cos( time * 0.5 ) * 20;
  85. light2.position.x = Math.cos( time * 0.5 ) * 20;
  86. light2.position.y = Math.sin( time * 0.75 ) * - 30;
  87. light2.position.z = Math.sin( time ) * 20;
  88. renderer.render( scene, camera );
  89. }
  90. // helpers
  91. function createMaterial() {
  92. const material = new THREE.MeshPhongNodeMaterial();
  93. const seedAttribute = attribute( 'seed' );
  94. const displaceNormalAttribute = attribute( 'displaceNormal' );
  95. const localTime = attribute( 'time' ).add( time );
  96. const effector1 = uniform( light1.position ).toVar();
  97. const effector2 = uniform( light2.position ).toVar();
  98. const distance1 = distance( positionLocal, modelWorldMatrixInverse.mul( effector1 ) );
  99. const distance2 = distance( positionLocal, modelWorldMatrixInverse.mul( effector2 ) );
  100. const invDistance1 = max( 0.0, float( 20.0 ).sub( distance1 ) ).div( 2.0 );
  101. const invDistance2 = max( 0.0, float( 20.0 ).sub( distance2 ) ).div( 2.0 );
  102. const s = abs( sin( localTime.mul( 2 ).add( seedAttribute ) ).mul( 0.5 ) ).add( invDistance1 ).add( invDistance2 );
  103. material.positionNode = positionLocal.add( displaceNormalAttribute.mul( s ) );
  104. return material;
  105. }
  106. function createGeometry( geometry ) {
  107. const positionAttribute = geometry.getAttribute( 'position' );
  108. const v0 = new THREE.Vector3();
  109. const v1 = new THREE.Vector3();
  110. const v2 = new THREE.Vector3();
  111. const v3 = new THREE.Vector3();
  112. const n = new THREE.Vector3();
  113. const plane = new THREE.Plane();
  114. const vertices = [];
  115. const times = [];
  116. const seeds = [];
  117. const displaceNormal = [];
  118. for ( let i = 0; i < positionAttribute.count; i += 3 ) {
  119. v0.fromBufferAttribute( positionAttribute, i );
  120. v1.fromBufferAttribute( positionAttribute, i + 1 );
  121. v2.fromBufferAttribute( positionAttribute, i + 2 );
  122. plane.setFromCoplanarPoints( v0, v1, v2 );
  123. v3.copy( v0 ).add( v1 ).add( v2 ).divideScalar( 3 ); // compute center
  124. v3.add( n.copy( plane.normal ).multiplyScalar( - 1 ) ); // displace center inwards
  125. // generate tetrahedron for each triangle
  126. vertices.push( v0.x, v0.y, v0.z, v1.x, v1.y, v1.z, v2.x, v2.y, v2.z );
  127. vertices.push( v3.x, v3.y, v3.z, v1.x, v1.y, v1.z, v0.x, v0.y, v0.z );
  128. vertices.push( v3.x, v3.y, v3.z, v2.x, v2.y, v2.z, v1.x, v1.y, v1.z );
  129. vertices.push( v3.x, v3.y, v3.z, v0.x, v0.y, v0.z, v2.x, v2.y, v2.z );
  130. const t = Math.random();
  131. const s = Math.random();
  132. n.copy( plane.normal );
  133. times.push( t, t, t ); times.push( t, t, t ); times.push( t, t, t ); times.push( t, t, t );
  134. seeds.push( s, s, s ); seeds.push( s, s, s ); seeds.push( s, s, s ); seeds.push( s, s, s );
  135. displaceNormal.push( n.x, n.y, n.z, n.x, n.y, n.z, n.x, n.y, n.z );
  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. }
  140. const newGeometry = new THREE.BufferGeometry();
  141. newGeometry.setAttribute( 'position', new THREE.Float32BufferAttribute( vertices, 3 ) );
  142. newGeometry.setAttribute( 'time', new THREE.Float32BufferAttribute( times, 1 ) );
  143. newGeometry.setAttribute( 'seed', new THREE.Float32BufferAttribute( seeds, 1 ) );
  144. newGeometry.setAttribute( 'displaceNormal', new THREE.Float32BufferAttribute( displaceNormal, 3 ) );
  145. newGeometry.computeVertexNormals();
  146. return newGeometry;
  147. }
  148. </script>
  149. </body>
  150. </html>
粤ICP备19079148号