webgl2_sandbox.html 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgl2 sandbox</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. <style>
  8. body {
  9. background: #000;
  10. padding: 0;
  11. margin: 0;
  12. font-weight: bold;
  13. }
  14. canvas {
  15. display: block;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. color: #ffffff;
  21. padding: 5px;
  22. font-family: Monospace;
  23. font-size: 13px;
  24. text-align: center;
  25. }
  26. a {
  27. color: #ffffff;
  28. }
  29. </style>
  30. </head>
  31. <body>
  32. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl2 sandbox.</div>
  33. <script type="module">
  34. import { PerspectiveCamera } from '../src/cameras/PerspectiveCamera.js';
  35. import { SphereBufferGeometry } from '../src/geometries/SphereGeometry.js';
  36. import { MeshNormalMaterial } from '../src/materials/MeshNormalMaterial.js';
  37. import { PointLight } from '../src/lights/PointLight.js';
  38. import { Color } from '../src/math/Color.js';
  39. import { Mesh } from '../src/objects/Mesh.js';
  40. import { Fog } from '../src/scenes/Fog.js';
  41. import { Scene } from '../src/scenes/Scene.js';
  42. import { WebGLRenderer } from '../src/renderers/WebGLRenderer.js';
  43. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  44. //
  45. var camera, scene, renderer;
  46. var controls;
  47. init();
  48. render();
  49. function init() {
  50. camera = new PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  51. camera.position.z = 3;
  52. scene = new Scene();
  53. scene.background = new Color( 0, 0, 0.5 );
  54. scene.fog = new Fog( 0x000000, 0.1, 3 );
  55. var light = new PointLight( 0xffffff );
  56. scene.add( light );
  57. var geometry = new SphereBufferGeometry( 0.05, 32, 16 );
  58. var material = new MeshNormalMaterial();
  59. for ( var i = 0; i < 5000; i ++ ) {
  60. var mesh = new Mesh( geometry, material );
  61. mesh.position.x = Math.random() * 10 - 5;
  62. mesh.position.y = Math.random() * 10 - 5;
  63. mesh.position.z = Math.random() * 10 - 5;
  64. mesh.rotation.y = Math.random() * 2 * Math.PI;
  65. mesh.scale.setScalar( Math.random() * 4 + 1 );
  66. scene.add( mesh );
  67. }
  68. var canvas = document.createElement( 'canvas' );
  69. var context = canvas.getContext( 'webgl2' );
  70. renderer = new WebGLRenderer( { canvas: canvas, context: context } );
  71. renderer.setPixelRatio( window.devicePixelRatio );
  72. renderer.setSize( window.innerWidth, window.innerHeight );
  73. document.body.appendChild( renderer.domElement );
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. //
  76. controls = new OrbitControls( camera, renderer.domElement );
  77. controls.addEventListener( 'change', render );
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. //
  85. function render() {
  86. renderer.render( scene, camera );
  87. }
  88. </script>
  89. </body>
  90. </html>