threejs-tips-preservedrawingbuffer.html 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - PreserveDrawingBuffer</title>
  8. <style>
  9. body {
  10. margin: 0;
  11. }
  12. #c {
  13. width: 100vw;
  14. height: 100vh;
  15. display: block;
  16. }
  17. </style>
  18. </head>
  19. <body>
  20. <canvas id="c"></canvas>
  21. </body>
  22. <script src="resources/threejs/r108/three.min.js"></script>
  23. <script>
  24. 'use strict';
  25. /* global THREE */
  26. function main() {
  27. const canvas = document.querySelector('#c');
  28. const renderer = new THREE.WebGLRenderer({
  29. canvas,
  30. preserveDrawingBuffer: true,
  31. alpha: true,
  32. });
  33. renderer.autoClearColor = false;
  34. const camera = new THREE.OrthographicCamera(-2, 2, 1, -1, -1, 1);
  35. const scene = new THREE.Scene();
  36. {
  37. const color = 0xFFFFFF;
  38. const intensity = 1;
  39. const light = new THREE.DirectionalLight(color, intensity);
  40. light.position.set(-1, 2, 4);
  41. scene.add(light);
  42. }
  43. const boxWidth = 1;
  44. const boxHeight = 1;
  45. const boxDepth = 1;
  46. const geometry = new THREE.BoxGeometry(boxWidth, boxHeight, boxDepth);
  47. const base = new THREE.Object3D();
  48. scene.add(base);
  49. base.scale.set(0.1, 0.1, 0.1);
  50. function makeInstance(geometry, color, x, y, z) {
  51. const material = new THREE.MeshPhongMaterial({color});
  52. const cube = new THREE.Mesh(geometry, material);
  53. base.add(cube);
  54. cube.position.set(x, y, z);
  55. return cube;
  56. }
  57. makeInstance(geometry, '#F00', -2, 0, 0);
  58. makeInstance(geometry, '#FF0', 2, 0, 0);
  59. makeInstance(geometry, '#0F0', 0, -2, 0);
  60. makeInstance(geometry, '#0FF', 0, 2, 0);
  61. makeInstance(geometry, '#00F', 0, 0, -2);
  62. makeInstance(geometry, '#F0F', 0, 0, 2);
  63. function resizeRendererToDisplaySize(renderer) {
  64. const canvas = renderer.domElement;
  65. const width = canvas.clientWidth;
  66. const height = canvas.clientHeight;
  67. const needResize = canvas.width !== width || canvas.height !== height;
  68. if (needResize) {
  69. renderer.setSize(width, height, false);
  70. }
  71. return needResize;
  72. }
  73. const state = { x: 0, y: 0 };
  74. function render(time) {
  75. time *= 0.001; // convert to seconds
  76. if (resizeRendererToDisplaySize(renderer)) {
  77. const canvas = renderer.domElement;
  78. camera.right = canvas.clientWidth / canvas.clientHeight;
  79. camera.left = -camera.right;
  80. camera.updateProjectionMatrix();
  81. }
  82. base.position.set(state.x, state.y, 0);
  83. base.rotation.x = time;
  84. base.rotation.y = time * 1.11;
  85. renderer.render(scene, camera);
  86. requestAnimationFrame(render);
  87. }
  88. requestAnimationFrame(render);
  89. function getCanvasRelativePosition(event) {
  90. const rect = canvas.getBoundingClientRect();
  91. return {
  92. x: event.clientX - rect.left,
  93. y: event.clientY - rect.top,
  94. };
  95. }
  96. const temp = new THREE.Vector3();
  97. function setPosition(e) {
  98. const pos = getCanvasRelativePosition(e);
  99. const x = pos.x / canvas.clientWidth * 2 - 1;
  100. const y = pos.y / canvas.clientHeight * -2 + 1;
  101. temp.set(x, y, 0).unproject(camera);
  102. state.x = temp.x;
  103. state.y = temp.y;
  104. }
  105. canvas.addEventListener('mousemove', setPosition);
  106. canvas.addEventListener('touchmove', (e) => {
  107. e.preventDefault();
  108. setPosition(e.touches[0]);
  109. }, {passive: false});
  110. }
  111. main();
  112. </script>
  113. </html>