webgl_test_wide_gamut.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - test - wide gamut</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. <meta property="og:title" content="three.js webgl - test - wide gamut">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_test_wide_gamut.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_test_wide_gamut.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. <style>
  13. .container {
  14. position: absolute;
  15. overflow: hidden;
  16. width: 100%;
  17. height: 100%;
  18. }
  19. /* based on https://github.com/Paul-Browne/image-comparison-slider */
  20. .slider {
  21. position: absolute;
  22. width: 200px;
  23. height: 100%;
  24. top: 0;
  25. left: 0;
  26. z-index: 1;
  27. }
  28. .slider:before,
  29. .slider:after {
  30. position: absolute;
  31. left: 50%;
  32. content: "";
  33. background: #fff;
  34. cursor: grab;
  35. }
  36. .slider:before {
  37. top: 0;
  38. transform: translateX(-50%);
  39. width: 1px;
  40. height: 100%;
  41. }
  42. .slider:after {
  43. top: 50%;
  44. transform: translate(-50%, -50%);
  45. width: 5px;
  46. height: 33%;
  47. border-radius: 5px;
  48. }
  49. .label {
  50. position: fixed;
  51. top: calc(50% - 1em);
  52. height: 2em;
  53. line-height: 2em;
  54. background: rgba(0, 0, 0, 0.5);
  55. margin: 0;
  56. padding: 0.2em 0.5em;
  57. border-radius: 4px;
  58. font-size: 14px;
  59. user-select: none;
  60. -webkit-user-select: none;
  61. }
  62. </style>
  63. </head>
  64. <body>
  65. <div id="info">
  66. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - wide gamut test<br />
  67. </div>
  68. <div class="container">
  69. <div class="slider"></div>
  70. <p class="label" style="left: 1em;">sRGB</p>
  71. <p class="label" style="right: 1em;">Display P3</p>
  72. </div>
  73. <script type="importmap">
  74. {
  75. "imports": {
  76. "three": "../build/three.module.js",
  77. "three/addons/": "./jsm/"
  78. }
  79. }
  80. </script>
  81. <script type="module">
  82. import * as THREE from 'three';
  83. import { DisplayP3ColorSpace, DisplayP3ColorSpaceImpl, LinearDisplayP3ColorSpace, LinearDisplayP3ColorSpaceImpl } from 'three/addons/math/ColorSpaces.js';
  84. import WebGL from 'three/addons/capabilities/WebGL.js';
  85. let container, camera, renderer, loader;
  86. let sceneL, sceneR, textureL, textureR;
  87. let sliderPos = window.innerWidth / 2;
  88. const slider = document.querySelector( '.slider' );
  89. const isP3Context = WebGL.isColorSpaceAvailable( DisplayP3ColorSpace );
  90. THREE.ColorManagement.define( {
  91. [ DisplayP3ColorSpace ]: DisplayP3ColorSpaceImpl,
  92. [ LinearDisplayP3ColorSpace ]: LinearDisplayP3ColorSpaceImpl
  93. } );
  94. if ( isP3Context ) {
  95. THREE.ColorManagement.workingColorSpace = LinearDisplayP3ColorSpace;
  96. }
  97. init();
  98. function init() {
  99. container = document.querySelector( '.container' );
  100. sceneL = new THREE.Scene();
  101. sceneR = new THREE.Scene();
  102. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
  103. camera.position.z = 6;
  104. loader = new THREE.TextureLoader();
  105. initTextures();
  106. initSlider();
  107. renderer = new THREE.WebGLRenderer( { antialias: true } );
  108. renderer.setPixelRatio( window.devicePixelRatio );
  109. renderer.setSize( window.innerWidth, window.innerHeight );
  110. renderer.setAnimationLoop( animate );
  111. renderer.setScissorTest( true );
  112. container.appendChild( renderer.domElement );
  113. if ( isP3Context && window.matchMedia( '( color-gamut: p3 )' ).matches ) {
  114. renderer.outputColorSpace = DisplayP3ColorSpace;
  115. }
  116. window.addEventListener( 'resize', onWindowResize );
  117. window.matchMedia( '( color-gamut: p3 )' ).addEventListener( 'change', onGamutChange );
  118. }
  119. async function initTextures() {
  120. const path = 'textures/wide_gamut/logo_{colorSpace}.png';
  121. textureL = await loader.loadAsync( path.replace( '{colorSpace}', 'srgb' ) );
  122. textureR = await loader.loadAsync( path.replace( '{colorSpace}', 'p3' ) );
  123. textureL.colorSpace = THREE.SRGBColorSpace;
  124. textureR.colorSpace = DisplayP3ColorSpace;
  125. sceneL.background = THREE.TextureUtils.contain( textureL, window.innerWidth / window.innerHeight );
  126. sceneR.background = THREE.TextureUtils.contain( textureR, window.innerWidth / window.innerHeight );
  127. }
  128. function initSlider() {
  129. function onPointerDown() {
  130. if ( event.isPrimary === false ) return;
  131. window.addEventListener( 'pointermove', onPointerMove );
  132. window.addEventListener( 'pointerup', onPointerUp );
  133. }
  134. function onPointerUp() {
  135. window.removeEventListener( 'pointermove', onPointerMove );
  136. window.removeEventListener( 'pointerup', onPointerUp );
  137. }
  138. function onPointerMove( e ) {
  139. if ( event.isPrimary === false ) return;
  140. updateSlider( e.pageX );
  141. }
  142. updateSlider( sliderPos );
  143. slider.style.touchAction = 'none'; // disable touch scroll
  144. slider.addEventListener( 'pointerdown', onPointerDown );
  145. }
  146. function updateSlider( offset ) {
  147. sliderPos = Math.max( 10, Math.min( window.innerWidth - 10, offset ) );
  148. slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + 'px';
  149. }
  150. function onWindowResize() {
  151. camera.aspect = window.innerWidth / window.innerHeight;
  152. camera.updateProjectionMatrix();
  153. renderer.setSize( window.innerWidth, window.innerHeight );
  154. THREE.TextureUtils.contain( sceneL.background, window.innerWidth / window.innerHeight );
  155. THREE.TextureUtils.contain( sceneR.background, window.innerWidth / window.innerHeight );
  156. updateSlider( sliderPos );
  157. }
  158. function onGamutChange( { matches } ) {
  159. renderer.outputColorSpace = isP3Context && matches ? DisplayP3ColorSpace : THREE.SRGBColorSpace;
  160. textureL.needsUpdate = true;
  161. textureR.needsUpdate = true;
  162. }
  163. function animate() {
  164. renderer.setScissor( 0, 0, sliderPos, window.innerHeight );
  165. renderer.render( sceneL, camera );
  166. renderer.setScissor( sliderPos, 0, window.innerWidth, window.innerHeight );
  167. renderer.render( sceneR, camera );
  168. }
  169. </script>
  170. </body>
  171. </html>
粤ICP备19079148号