webgl_multiple_views.html 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - multiple views</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="container"></div>
  11. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - multiple views - webgl</div>
  12. <script type="importmap">
  13. {
  14. "imports": {
  15. "three": "../build/three.module.js",
  16. "three/addons/": "./jsm/"
  17. }
  18. }
  19. </script>
  20. <script type="module">
  21. import * as THREE from 'three';
  22. import Stats from 'three/addons/libs/stats.module.js';
  23. let stats;
  24. let scene, renderer;
  25. let mouseX = 0, mouseY = 0;
  26. let windowWidth, windowHeight;
  27. const views = [
  28. {
  29. left: 0,
  30. bottom: 0,
  31. width: 0.5,
  32. height: 1.0,
  33. background: new THREE.Color().setRGB( 0.5, 0.5, 0.7, THREE.SRGBColorSpace ),
  34. eye: [ 0, 300, 1800 ],
  35. up: [ 0, 1, 0 ],
  36. fov: 30,
  37. updateCamera: function ( camera, scene, mouseX ) {
  38. camera.position.x += mouseX * 0.05;
  39. camera.position.x = Math.max( Math.min( camera.position.x, 2000 ), - 2000 );
  40. camera.lookAt( scene.position );
  41. }
  42. },
  43. {
  44. left: 0.5,
  45. bottom: 0,
  46. width: 0.5,
  47. height: 0.5,
  48. background: new THREE.Color().setRGB( 0.7, 0.5, 0.5, THREE.SRGBColorSpace ),
  49. eye: [ 0, 1800, 0 ],
  50. up: [ 0, 0, 1 ],
  51. fov: 45,
  52. updateCamera: function ( camera, scene, mouseX ) {
  53. camera.position.x -= mouseX * 0.05;
  54. camera.position.x = Math.max( Math.min( camera.position.x, 2000 ), - 2000 );
  55. camera.lookAt( camera.position.clone().setY( 0 ) );
  56. }
  57. },
  58. {
  59. left: 0.5,
  60. bottom: 0.5,
  61. width: 0.5,
  62. height: 0.5,
  63. background: new THREE.Color().setRGB( 0.5, 0.7, 0.7, THREE.SRGBColorSpace ),
  64. eye: [ 1400, 800, 1400 ],
  65. up: [ 0, 1, 0 ],
  66. fov: 60,
  67. updateCamera: function ( camera, scene, mouseX ) {
  68. camera.position.y -= mouseX * 0.05;
  69. camera.position.y = Math.max( Math.min( camera.position.y, 1600 ), - 1600 );
  70. camera.lookAt( scene.position );
  71. }
  72. }
  73. ];
  74. init();
  75. function init() {
  76. const container = document.getElementById( 'container' );
  77. for ( let ii = 0; ii < views.length; ++ ii ) {
  78. const view = views[ ii ];
  79. const camera = new THREE.PerspectiveCamera( view.fov, window.innerWidth / window.innerHeight, 1, 10000 );
  80. camera.position.fromArray( view.eye );
  81. camera.up.fromArray( view.up );
  82. view.camera = camera;
  83. }
  84. scene = new THREE.Scene();
  85. const light = new THREE.DirectionalLight( 0xffffff, 3 );
  86. light.position.set( 0, 0, 1 );
  87. scene.add( light );
  88. // shadow
  89. const canvas = document.createElement( 'canvas' );
  90. canvas.width = 128;
  91. canvas.height = 128;
  92. const context = canvas.getContext( '2d' );
  93. const gradient = context.createRadialGradient( canvas.width / 2, canvas.height / 2, 0, canvas.width / 2, canvas.height / 2, canvas.width / 2 );
  94. gradient.addColorStop( 0.1, 'rgba(0,0,0,0.15)' );
  95. gradient.addColorStop( 1, 'rgba(0,0,0,0)' );
  96. context.fillStyle = gradient;
  97. context.fillRect( 0, 0, canvas.width, canvas.height );
  98. const shadowTexture = new THREE.CanvasTexture( canvas );
  99. const shadowMaterial = new THREE.MeshBasicMaterial( { map: shadowTexture, transparent: true } );
  100. const shadowGeo = new THREE.PlaneGeometry( 300, 300, 1, 1 );
  101. let shadowMesh;
  102. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  103. shadowMesh.position.y = - 250;
  104. shadowMesh.rotation.x = - Math.PI / 2;
  105. scene.add( shadowMesh );
  106. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  107. shadowMesh.position.x = - 400;
  108. shadowMesh.position.y = - 250;
  109. shadowMesh.rotation.x = - Math.PI / 2;
  110. scene.add( shadowMesh );
  111. shadowMesh = new THREE.Mesh( shadowGeo, shadowMaterial );
  112. shadowMesh.position.x = 400;
  113. shadowMesh.position.y = - 250;
  114. shadowMesh.rotation.x = - Math.PI / 2;
  115. scene.add( shadowMesh );
  116. const radius = 200;
  117. const geometry1 = new THREE.IcosahedronGeometry( radius, 1 );
  118. const count = geometry1.attributes.position.count;
  119. geometry1.setAttribute( 'color', new THREE.BufferAttribute( new Float32Array( count * 3 ), 3 ) );
  120. const geometry2 = geometry1.clone();
  121. const geometry3 = geometry1.clone();
  122. const color = new THREE.Color();
  123. const positions1 = geometry1.attributes.position;
  124. const positions2 = geometry2.attributes.position;
  125. const positions3 = geometry3.attributes.position;
  126. const colors1 = geometry1.attributes.color;
  127. const colors2 = geometry2.attributes.color;
  128. const colors3 = geometry3.attributes.color;
  129. for ( let i = 0; i < count; i ++ ) {
  130. color.setHSL( ( positions1.getY( i ) / radius + 1 ) / 2, 1.0, 0.5, THREE.SRGBColorSpace );
  131. colors1.setXYZ( i, color.r, color.g, color.b );
  132. color.setHSL( 0, ( positions2.getY( i ) / radius + 1 ) / 2, 0.5, THREE.SRGBColorSpace );
  133. colors2.setXYZ( i, color.r, color.g, color.b );
  134. color.setRGB( 1, 0.8 - ( positions3.getY( i ) / radius + 1 ) / 2, 0, THREE.SRGBColorSpace );
  135. colors3.setXYZ( i, color.r, color.g, color.b );
  136. }
  137. const material = new THREE.MeshPhongMaterial( {
  138. color: 0xffffff,
  139. flatShading: true,
  140. vertexColors: true,
  141. shininess: 0
  142. } );
  143. const wireframeMaterial = new THREE.MeshBasicMaterial( { color: 0x000000, wireframe: true, transparent: true } );
  144. let mesh = new THREE.Mesh( geometry1, material );
  145. let wireframe = new THREE.Mesh( geometry1, wireframeMaterial );
  146. mesh.add( wireframe );
  147. mesh.position.x = - 400;
  148. mesh.rotation.x = - 1.87;
  149. scene.add( mesh );
  150. mesh = new THREE.Mesh( geometry2, material );
  151. wireframe = new THREE.Mesh( geometry2, wireframeMaterial );
  152. mesh.add( wireframe );
  153. mesh.position.x = 400;
  154. scene.add( mesh );
  155. mesh = new THREE.Mesh( geometry3, material );
  156. wireframe = new THREE.Mesh( geometry3, wireframeMaterial );
  157. mesh.add( wireframe );
  158. scene.add( mesh );
  159. renderer = new THREE.WebGLRenderer( { antialias: true } );
  160. renderer.setPixelRatio( window.devicePixelRatio );
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. renderer.setAnimationLoop( animate );
  163. container.appendChild( renderer.domElement );
  164. stats = new Stats();
  165. container.appendChild( stats.dom );
  166. document.addEventListener( 'mousemove', onDocumentMouseMove );
  167. }
  168. function onDocumentMouseMove( event ) {
  169. mouseX = ( event.clientX - windowWidth / 2 );
  170. mouseY = ( event.clientY - windowHeight / 2 );
  171. }
  172. function updateSize() {
  173. if ( windowWidth != window.innerWidth || windowHeight != window.innerHeight ) {
  174. windowWidth = window.innerWidth;
  175. windowHeight = window.innerHeight;
  176. renderer.setSize( windowWidth, windowHeight );
  177. }
  178. }
  179. function animate() {
  180. render();
  181. stats.update();
  182. }
  183. function render() {
  184. updateSize();
  185. for ( let ii = 0; ii < views.length; ++ ii ) {
  186. const view = views[ ii ];
  187. const camera = view.camera;
  188. view.updateCamera( camera, scene, mouseX, mouseY );
  189. const left = Math.floor( windowWidth * view.left );
  190. const bottom = Math.floor( windowHeight * view.bottom );
  191. const width = Math.floor( windowWidth * view.width );
  192. const height = Math.floor( windowHeight * view.height );
  193. renderer.setViewport( left, bottom, width, height );
  194. renderer.setScissor( left, bottom, width, height );
  195. renderer.setScissorTest( true );
  196. renderer.setClearColor( view.background );
  197. camera.aspect = width / height;
  198. camera.updateProjectionMatrix();
  199. renderer.render( scene, camera );
  200. }
  201. }
  202. </script>
  203. </body>
  204. </html>