webgpu_lights_phong.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - phong lighting model</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>Phong</span>
  14. </div>
  15. <small>
  16. <b style="color:red">Left: Red lights</b> - <b>Center: All lights</b> - <b style="color:blue">Right: blue light</b>
  17. </small>
  18. </div>
  19. <script type="importmap">
  20. {
  21. "imports": {
  22. "three": "../build/three.webgpu.js",
  23. "three/webgpu": "../build/three.webgpu.js",
  24. "three/tsl": "../build/three.tsl.js",
  25. "three/addons/": "./jsm/"
  26. }
  27. }
  28. </script>
  29. <script type="module">
  30. import * as THREE from 'three/webgpu';
  31. import { color, fog, rangeFogFactor, checker, uv, mix, texture, lights, normalMap } from 'three/tsl';
  32. import { Inspector } from 'three/addons/inspector/Inspector.js';
  33. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  34. import { TeapotGeometry } from 'three/addons/geometries/TeapotGeometry.js';
  35. let camera, scene, renderer,
  36. light1, light2, light3, light4,
  37. controls;
  38. init();
  39. function init() {
  40. camera = new THREE.PerspectiveCamera( 50, window.innerWidth / window.innerHeight, 0.01, 100 );
  41. camera.position.z = 7;
  42. scene = new THREE.Scene();
  43. scene.fogNode = fog( color( 0xFF00FF ), rangeFogFactor( 12, 30 ) );
  44. const sphereGeometry = new THREE.SphereGeometry( 0.1, 16, 8 );
  45. // textures
  46. const textureLoader = new THREE.TextureLoader();
  47. const normalMapTexture = textureLoader.load( './textures/water/Water_1_M_Normal.jpg' );
  48. normalMapTexture.wrapS = THREE.RepeatWrapping;
  49. normalMapTexture.wrapT = THREE.RepeatWrapping;
  50. const alphaTexture = textureLoader.load( './textures/roughness_map.jpg' );
  51. alphaTexture.wrapS = THREE.RepeatWrapping;
  52. alphaTexture.wrapT = THREE.RepeatWrapping;
  53. // lights
  54. const addLight = ( hexColor, power = 1700, distance = 100 ) => {
  55. const material = new THREE.MeshPhongNodeMaterial();
  56. material.colorNode = color( hexColor );
  57. material.lights = false;
  58. const mesh = new THREE.Mesh( sphereGeometry, material );
  59. const light = new THREE.PointLight( hexColor, 1, distance );
  60. light.power = power;
  61. light.add( mesh );
  62. scene.add( light );
  63. return light;
  64. };
  65. light1 = addLight( 0x0040ff );
  66. light2 = addLight( 0xffffff );
  67. light3 = addLight( 0x80ff80 );
  68. light4 = addLight( 0xffaa00 );
  69. // light nodes ( selective lights )
  70. const blueLightsNode = lights( [ light1 ] );
  71. const whiteLightsNode = lights( [ light2 ] );
  72. // models
  73. const geometryTeapot = new TeapotGeometry( .8, 18 );
  74. const leftObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongNodeMaterial( { color: 0x555555 } ) );
  75. leftObject.material.lightsNode = blueLightsNode;
  76. leftObject.material.specularNode = texture( alphaTexture );
  77. leftObject.position.x = - 3;
  78. scene.add( leftObject );
  79. const centerObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongNodeMaterial( { color: 0x555555 } ) );
  80. centerObject.material.normalNode = normalMap( texture( normalMapTexture ) );
  81. centerObject.material.shininess = 80;
  82. scene.add( centerObject );
  83. const rightObject = new THREE.Mesh( geometryTeapot, new THREE.MeshPhongNodeMaterial( { color: 0x555555 } ) );
  84. rightObject.material.lightsNode = whiteLightsNode;
  85. //rightObject.material.specular.setHex( 0xFF00FF );
  86. rightObject.material.specularNode = mix( color( 0x0000FF ), color( 0xFF0000 ), checker( uv().mul( 5 ) ) );
  87. rightObject.material.shininess = 90;
  88. rightObject.position.x = 3;
  89. scene.add( rightObject );
  90. leftObject.rotation.y = centerObject.rotation.y = rightObject.rotation.y = Math.PI * - 0.5;
  91. leftObject.position.y = centerObject.position.y = rightObject.position.y = - 1;
  92. // renderer
  93. renderer = new THREE.WebGPURenderer( { antialias: true } );
  94. renderer.setPixelRatio( window.devicePixelRatio );
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. renderer.setAnimationLoop( animate );
  97. renderer.inspector = new Inspector();
  98. document.body.appendChild( renderer.domElement );
  99. // controls
  100. controls = new OrbitControls( camera, renderer.domElement );
  101. controls.minDistance = 3;
  102. controls.maxDistance = 25;
  103. //
  104. window.addEventListener( 'resize', onWindowResize );
  105. }
  106. function onWindowResize() {
  107. camera.aspect = window.innerWidth / window.innerHeight;
  108. camera.updateProjectionMatrix();
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. }
  111. function animate() {
  112. const time = performance.now() / 1000;
  113. const lightTime = time * 0.5;
  114. light1.position.x = Math.sin( lightTime * 0.7 ) * 3;
  115. light1.position.y = Math.cos( lightTime * 0.5 ) * 4;
  116. light1.position.z = Math.cos( lightTime * 0.3 ) * 3;
  117. light2.position.x = Math.cos( lightTime * 0.3 ) * 3;
  118. light2.position.y = Math.sin( lightTime * 0.5 ) * 4;
  119. light2.position.z = Math.sin( lightTime * 0.7 ) * 3;
  120. light3.position.x = Math.sin( lightTime * 0.7 ) * 3;
  121. light3.position.y = Math.cos( lightTime * 0.3 ) * 4;
  122. light3.position.z = Math.sin( lightTime * 0.5 ) * 3;
  123. light4.position.x = Math.sin( lightTime * 0.3 ) * 3;
  124. light4.position.y = Math.cos( lightTime * 0.7 ) * 4;
  125. light4.position.z = Math.sin( lightTime * 0.5 ) * 3;
  126. renderer.render( scene, camera );
  127. }
  128. </script>
  129. </body>
  130. </html>
粤ICP备19079148号