webgl2_sandbox.html 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - webgl2 sandbox</div>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  14. import { WEBGL } from './jsm/WebGL.js';
  15. if ( WEBGL.isWebGL2Available() === false ) {
  16. document.body.appendChild( WEBGL.getWebGL2ErrorMessage() );
  17. }
  18. //
  19. var camera, scene, renderer;
  20. var controls;
  21. init();
  22. render();
  23. function init() {
  24. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 100 );
  25. camera.position.z = 3;
  26. scene = new THREE.Scene();
  27. scene.background = new THREE.Color( 0, 0, 0.5 );
  28. scene.fog = new THREE.Fog( 0x000000, 0.1, 3 );
  29. var light = new THREE.PointLight( 0xffffff );
  30. scene.add( light );
  31. var geometry = new THREE.SphereBufferGeometry( 0.05, 32, 16 );
  32. var material = new THREE.MeshNormalMaterial();
  33. for ( var i = 0; i < 5000; i ++ ) {
  34. var mesh = new THREE.Mesh( geometry, material );
  35. mesh.position.x = Math.random() * 10 - 5;
  36. mesh.position.y = Math.random() * 10 - 5;
  37. mesh.position.z = Math.random() * 10 - 5;
  38. mesh.rotation.y = Math.random() * 2 * Math.PI;
  39. mesh.scale.setScalar( Math.random() * 4 + 1 );
  40. scene.add( mesh );
  41. }
  42. var canvas = document.createElement( 'canvas' );
  43. var context = canvas.getContext( 'webgl2', { alpha: false, antialias: true } );
  44. renderer = new THREE.WebGLRenderer( { canvas: canvas, context: context } );
  45. renderer.setPixelRatio( window.devicePixelRatio );
  46. renderer.setSize( window.innerWidth, window.innerHeight );
  47. document.body.appendChild( renderer.domElement );
  48. window.addEventListener( 'resize', onWindowResize, false );
  49. //
  50. controls = new OrbitControls( camera, renderer.domElement );
  51. controls.addEventListener( 'change', render );
  52. }
  53. function onWindowResize() {
  54. camera.aspect = window.innerWidth / window.innerHeight;
  55. camera.updateProjectionMatrix();
  56. renderer.setSize( window.innerWidth, window.innerHeight );
  57. }
  58. //
  59. function render() {
  60. renderer.render( scene, camera );
  61. }
  62. </script>
  63. </body>
  64. </html>