webgl_multiple_views.html 7.4 KB

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