webgl_test_wide_gamut.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. <script type="importmap">
  51. {
  52. "imports": {
  53. "three": "../build/three.module.js",
  54. "three/addons/": "./jsm/"
  55. }
  56. }
  57. </script>
  58. <script type="module">
  59. import * as THREE from 'three';
  60. import WebGL from 'three/addons/capabilities/WebGL.js';
  61. let container, camera, renderer, loader;
  62. let sceneL, sceneR, textureL, textureR;
  63. let sliderPos = window.innerWidth / 2;
  64. const isP3Context = WebGL.isColorSpaceAvailable( THREE.DisplayP3ColorSpace );
  65. if ( isP3Context ) {
  66. THREE.ColorManagement.workingColorSpace = THREE.LinearDisplayP3ColorSpace;
  67. }
  68. init();
  69. function init() {
  70. container = document.querySelector( '.container' );
  71. sceneL = new THREE.Scene();
  72. sceneR = new THREE.Scene();
  73. camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 0.1, 100 );
  74. camera.position.z = 6;
  75. loader = new THREE.TextureLoader();
  76. initTextures();
  77. initSlider();
  78. renderer = new THREE.WebGLRenderer( { antialias: true } );
  79. renderer.setPixelRatio( window.devicePixelRatio );
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. renderer.setAnimationLoop( animate );
  82. renderer.setScissorTest( true );
  83. container.appendChild( renderer.domElement );
  84. if ( isP3Context && window.matchMedia( '( color-gamut: p3 )' ).matches ) {
  85. renderer.outputColorSpace = THREE.DisplayP3ColorSpace;
  86. }
  87. window.addEventListener( 'resize', onWindowResize );
  88. window.matchMedia( '( color-gamut: p3 )' ).addEventListener( 'change', onGamutChange );
  89. }
  90. async function initTextures() {
  91. const path = 'textures/wide_gamut/logo_{colorSpace}.png';
  92. textureL = await loader.loadAsync( path.replace( '{colorSpace}', 'srgb' ) );
  93. textureR = await loader.loadAsync( path.replace( '{colorSpace}', 'p3' ) );
  94. textureL.colorSpace = THREE.SRGBColorSpace;
  95. textureR.colorSpace = THREE.DisplayP3ColorSpace;
  96. sceneL.background = THREE.TextureUtils.contain( textureL, window.innerWidth / window.innerHeight );
  97. sceneR.background = THREE.TextureUtils.contain( textureR, window.innerWidth / window.innerHeight );
  98. }
  99. function initSlider() {
  100. const slider = document.querySelector( '.slider' );
  101. function onPointerDown() {
  102. if ( event.isPrimary === false ) return;
  103. window.addEventListener( 'pointermove', onPointerMove );
  104. window.addEventListener( 'pointerup', onPointerUp );
  105. }
  106. function onPointerUp() {
  107. window.removeEventListener( 'pointermove', onPointerMove );
  108. window.removeEventListener( 'pointerup', onPointerUp );
  109. }
  110. function onPointerMove( e ) {
  111. if ( event.isPrimary === false ) return;
  112. sliderPos = Math.max( 0, Math.min( window.innerWidth, e.pageX ) );
  113. slider.style.left = sliderPos - ( slider.offsetWidth / 2 ) + 'px';
  114. }
  115. slider.style.touchAction = 'none'; // disable touch scroll
  116. slider.addEventListener( 'pointerdown', onPointerDown );
  117. }
  118. function onWindowResize() {
  119. camera.aspect = window.innerWidth / window.innerHeight;
  120. camera.updateProjectionMatrix();
  121. renderer.setSize( window.innerWidth, window.innerHeight );
  122. THREE.TextureUtils.contain( sceneL.background, window.innerWidth / window.innerHeight );
  123. THREE.TextureUtils.contain( sceneR.background, window.innerWidth / window.innerHeight );
  124. }
  125. function onGamutChange( { matches } ) {
  126. renderer.outputColorSpace = isP3Context && matches ? THREE.DisplayP3ColorSpace : THREE.SRGBColorSpace;
  127. textureL.needsUpdate = true;
  128. textureR.needsUpdate = true;
  129. }
  130. function animate() {
  131. renderer.setScissor( 0, 0, sliderPos, window.innerHeight );
  132. renderer.render( sceneL, camera );
  133. renderer.setScissor( sliderPos, 0, window.innerWidth, window.innerHeight );
  134. renderer.render( sceneR, camera );
  135. }
  136. </script>
  137. </body>
  138. </html>