webgl_lights_rectarealight_map.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - lights - rect area light</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> - RectAreaLight with texture map<br/>
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/"
  18. }
  19. }
  20. </script>
  21. <script type="module">
  22. import * as THREE from 'three';
  23. import Stats from 'three/addons/libs/stats.module.js';
  24. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
  27. import { DRACOLoader } from 'three/addons/loaders/DRACOLoader.js';
  28. import { RectAreaLightHelper } from 'three/addons/helpers/RectAreaLightHelper.js';
  29. import { RectAreaLightUniformsLib } from 'three/addons/lights/RectAreaLightUniformsLib.js';
  30. let renderer, scene, camera, controls;
  31. let stats, carModel, bodyMaterial, glassMaterial;
  32. init();
  33. function init() {
  34. renderer = new THREE.WebGLRenderer( { antialias: true, outputBufferType: THREE.HalfFloatType } );
  35. renderer.setPixelRatio( window.devicePixelRatio );
  36. renderer.setSize( window.innerWidth, window.innerHeight );
  37. renderer.setAnimationLoop( animation );
  38. renderer.toneMapping = THREE.NeutralTonemapping;
  39. document.body.appendChild( renderer.domElement );
  40. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
  41. camera.position.set( - 15, 5, - 5 );
  42. scene = new THREE.Scene();
  43. RectAreaLightUniformsLib.init();
  44. // Create video element for center light
  45. const video = document.createElement( 'video' );
  46. video.crossOrigin = 'anonymous';
  47. video.src = 'textures/veo-oledtv.mp4';
  48. video.loop = true;
  49. video.muted = true;
  50. video.playsInline = true;
  51. video.autoplay = true;
  52. video.play();
  53. const videoTexture = new THREE.VideoTexture( video );
  54. videoTexture.colorSpace = THREE.SRGBColorSpace;
  55. const rectLight = new THREE.RectAreaLight( 0xffffff, 5, 12, 8 );
  56. rectLight.map = videoTexture;
  57. // rectLight.map = new THREE.TextureLoader().load( 'textures/uv_grid_opengl.jpg' );
  58. rectLight.map.generateMipmaps = true;
  59. rectLight.map.minFilter = THREE.LinearMipmapLinearFilter;
  60. rectLight.position.set( 0, 5, 5 );
  61. scene.add( rectLight );
  62. scene.add( new RectAreaLightHelper( rectLight ) );
  63. const geoFloor = new THREE.BoxGeometry( 2000, 0.1, 2000 );
  64. const matStdFloor = new THREE.MeshStandardMaterial( { color: 0x444444 } );
  65. matStdFloor.roughnessMap = createCheckerTexture( 400 );
  66. const mshStdFloor = new THREE.Mesh( geoFloor, matStdFloor );
  67. scene.add( mshStdFloor );
  68. // Car shadow (AO plane)
  69. const aoTexture = new THREE.TextureLoader().load( 'models/gltf/ferrari_ao.png' );
  70. const shadowPlane = new THREE.Mesh(
  71. new THREE.PlaneGeometry( 256, 512 ),
  72. new THREE.MeshBasicMaterial( {
  73. map: aoTexture,
  74. blending: THREE.MultiplyBlending,
  75. depthWrite: false,
  76. transparent: true,
  77. premultipliedAlpha: true
  78. } )
  79. );
  80. shadowPlane.rotation.x = - Math.PI / 2;
  81. shadowPlane.rotation.z = Math.PI / 2;
  82. shadowPlane.position.y = 0.06;
  83. shadowPlane.scale.setScalar( 0.031 );
  84. scene.add( shadowPlane );
  85. // Load car model
  86. bodyMaterial = new THREE.MeshStandardMaterial( {
  87. color: 0xff4400,
  88. roughness: 0.2,
  89. metalness: 0
  90. } );
  91. glassMaterial = new THREE.MeshStandardMaterial( {
  92. color: 0xffffff,
  93. roughness: 0,
  94. metalness: 0,
  95. transparent: true,
  96. premultipliedAlpha: true,
  97. opacity: 0.3
  98. } );
  99. const dracoLoader = new DRACOLoader();
  100. dracoLoader.setDecoderPath( 'jsm/libs/draco/gltf/' );
  101. const loader = new GLTFLoader();
  102. loader.setDRACOLoader( dracoLoader );
  103. loader.load( 'models/gltf/ferrari.glb', function ( gltf ) {
  104. carModel = gltf.scene;
  105. carModel.rotation.y = Math.PI / 2;
  106. carModel.scale.setScalar( 3 );
  107. carModel.position.set( 0, 0, 0 );
  108. carModel.traverse( function ( child ) {
  109. if ( child.isMesh ) {
  110. // Replace car body material
  111. if ( child.name === 'body' ) {
  112. child.material = bodyMaterial;
  113. }
  114. // Replace glass material with transmissive material
  115. if ( child.name === 'glass' ) {
  116. child.material = glassMaterial;
  117. }
  118. }
  119. } );
  120. scene.add( carModel );
  121. } );
  122. controls = new OrbitControls( camera, renderer.domElement );
  123. controls.target.set( 0, 2, 3 );
  124. controls.enableDamping = true;
  125. controls.update();
  126. //
  127. window.addEventListener( 'resize', onWindowResize );
  128. stats = new Stats();
  129. document.body.appendChild( stats.dom );
  130. const gui = new GUI();
  131. gui.add( rectLight, 'intensity', 0, 20 ).name( 'Light Intensity' );
  132. gui.add( matStdFloor, 'roughness', 0, 1 ).name( 'Floor Roughness' );
  133. gui.add( matStdFloor, 'metalness', 0, 1 ).name( 'Floor Metalness' );
  134. gui.add( bodyMaterial, 'roughness', 0, 1 ).name( 'Car Roughness' );
  135. gui.add( bodyMaterial, 'metalness', 0, 1 ).name( 'Car Metalness' );
  136. gui.add( glassMaterial, 'opacity', 0, 1 ).name( 'Glass Opacity' );
  137. gui.add( glassMaterial, 'roughness', 0, 1 ).name( 'Glass Roughness' );
  138. }
  139. function createCheckerTexture( repeat = 1 ) {
  140. const canvas = document.createElement( 'canvas' );
  141. canvas.width = 2;
  142. canvas.height = 2;
  143. const ctx = canvas.getContext( '2d' );
  144. ctx.fillStyle = '#000';
  145. ctx.fillRect( 0, 0, 2, 2 );
  146. ctx.fillStyle = '#fff';
  147. ctx.fillRect( 0, 0, 1, 1 );
  148. ctx.fillRect( 1, 1, 1, 1 );
  149. const texture = new THREE.CanvasTexture( canvas );
  150. texture.repeat.set( repeat, repeat );
  151. texture.magFilter = THREE.NearestFilter;
  152. texture.wrapS = THREE.RepeatWrapping;
  153. texture.wrapT = THREE.RepeatWrapping;
  154. return texture;
  155. }
  156. function onWindowResize() {
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. camera.aspect = ( window.innerWidth / window.innerHeight );
  159. camera.updateProjectionMatrix();
  160. }
  161. function animation() {
  162. controls.update();
  163. renderer.render( scene, camera );
  164. stats.update();
  165. }
  166. </script>
  167. </body>
  168. </html>
粤ICP备19079148号