1
0

webgpu_lights_phong.html 5.6 KB

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