webgpu_lights_pointlights.html 6.9 KB

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