webgl_test_wide_gamut.html 5.8 KB

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