webgpu_lights_phong.html 5.9 KB

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