webgl_test_wide_gamut.html 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. .slider {
  16. position: absolute;
  17. cursor: ew-resize;
  18. width: 40px;
  19. height: 40px;
  20. background-color: #F32196;
  21. opacity: 0.7;
  22. border-radius: 50%;
  23. top: calc(50% - 20px);
  24. left: calc(50% - 20px);
  25. }
  26. .label {
  27. position: fixed;
  28. top: calc(50% - 1em);
  29. height: 2em;
  30. line-height: 2em;
  31. background: rgba(0, 0, 0, 0.5);
  32. margin: 0;
  33. padding: 0.2em 0.5em;
  34. border-radius: 4px;
  35. font-size: 14px;
  36. user-select: none;
  37. -webkit-user-select: none;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div id="info">
  43. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - wide gamut test<br />
  44. </div>
  45. <div class="container">
  46. <div class="slider"></div>
  47. <p class="label" style="left: 1em;">sRGB</p>
  48. <p class="label" style="right: 1em;">Display P3</p>
  49. </div>
  50. <!-- Import maps polyfill -->
  51. <!-- Remove this when import maps will be widely supported -->
  52. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  53. <script type="importmap">
  54. {
  55. "imports": {
  56. "three": "../build/three.module.js",
  57. "three/addons/": "./jsm/"
  58. }
  59. }
  60. </script>
  61. <script type="module">
  62. import * as THREE from 'three';
  63. import WebGL from 'three/addons/capabilities/WebGL.js';
  64. let container, camera, renderer, loader;
  65. let sceneL, sceneR, textureL, textureR;
  66. let sliderPos = window.innerWidth / 2;
  67. const isP3Context = WebGL.isColorSpaceAvailable( THREE.DisplayP3ColorSpace );
  68. if ( isP3Context ) {
  69. THREE.ColorManagement.workingColorSpace = THREE.LinearDisplayP3ColorSpace;
  70. }
  71. init();
  72. function init() {
  73. container = document.querySelector( '.container' );
  74. sceneL = new THREE.Scene();
  75. sceneR = new THREE.Scene();
  76. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
  77. camera.position.z = 6;
  78. loader = new THREE.TextureLoader();
  79. initTextures();
  80. initSlider();
  81. renderer = new THREE.WebGLRenderer( { antialias: true } );
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. renderer.setScissorTest( true );
  85. renderer.setAnimationLoop( render );
  86. container.appendChild( renderer.domElement );
  87. if ( isP3Context && window.matchMedia( '( color-gamut: p3 )' ).matches ) {
  88. renderer.outputColorSpace = THREE.DisplayP3ColorSpace;
  89. }
  90. window.addEventListener( 'resize', onWindowResize );
  91. window.matchMedia( '( color-gamut: p3 )' ).addEventListener( 'change', onGamutChange );
  92. }
  93. async function initTextures() {
  94. const path = 'textures/wide_gamut/logo_{colorSpace}.png';
  95. textureL = await loader.loadAsync( path.replace( '{colorSpace}', 'srgb' ) );
  96. textureR = await loader.loadAsync( path.replace( '{colorSpace}', 'p3' ) );
  97. textureL.colorSpace = THREE.SRGBColorSpace;
  98. textureR.colorSpace = THREE.DisplayP3ColorSpace;
  99. sceneL.background = containTexture( window.innerWidth / window.innerHeight, textureL );
  100. sceneR.background = containTexture( window.innerWidth / window.innerHeight, textureR );
  101. }
  102. function initSlider() {
  103. const slider = document.querySelector( '.slider' );
  104. function onPointerDown() {
  105. if ( event.isPrimary === false ) return;
  106. window.addEventListener( 'pointermove', onPointerMove );
  107. window.addEventListener( 'pointerup', onPointerUp );
  108. }
  109. function onPointerUp() {
  110. window.removeEventListener( 'pointermove', onPointerMove );
  111. window.removeEventListener( 'pointerup', onPointerUp );
  112. }
  113. function onPointerMove( e ) {
  114. if ( event.isPrimary === false ) return;
  115. sliderPos = Math.max( 0, Math.min( window.innerWidth, e.pageX ) );
  116. slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + 'px';
  117. }
  118. slider.style.touchAction = 'none'; // disable touch scroll
  119. slider.addEventListener( 'pointerdown', onPointerDown );
  120. }
  121. function onWindowResize() {
  122. camera.aspect = window.innerWidth / window.innerHeight;
  123. camera.updateProjectionMatrix();
  124. renderer.setSize( window.innerWidth, window.innerHeight );
  125. containTexture( window.innerWidth / window.innerHeight, sceneL.background );
  126. containTexture( window.innerWidth / window.innerHeight, sceneR.background );
  127. }
  128. function onGamutChange( { matches } ) {
  129. renderer.outputColorSpace = isP3Context && matches ? THREE.DisplayP3ColorSpace : THREE.SRGBColorSpace;
  130. textureL.needsUpdate = true;
  131. textureR.needsUpdate = true;
  132. }
  133. function containTexture ( aspect, target ) {
  134. // Sets the matrix uv transform so the texture image is contained in a region having the specified aspect ratio,
  135. // and does so without distortion. Akin to CSS object-fit: contain.
  136. // Source: https://github.com/mrdoob/three.js/pull/17199
  137. var imageAspect = ( target.image && target.image.width ) ? target.image.width / target.image.height : 1;
  138. if ( aspect > imageAspect ) {
  139. target.matrix.setUvTransform( 0, 0, aspect / imageAspect, 1, 0, 0.5, 0.5 );
  140. } else {
  141. target.matrix.setUvTransform( 0, 0, 1, imageAspect / aspect, 0, 0.5, 0.5 );
  142. }
  143. target.matrixAutoUpdate = false;
  144. return target;
  145. }
  146. function render() {
  147. renderer.setScissor( 0, 0, sliderPos, window.innerHeight );
  148. renderer.render( sceneL, camera );
  149. renderer.setScissor( sliderPos, 0, window.innerWidth, window.innerHeight );
  150. renderer.render( sceneR, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>