webgl2_sandbox.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. overflow:hidden;
  14. }
  15. #info {
  16. position: absolute;
  17. top: 0px; width: 100%;
  18. color: #ffffff;
  19. padding: 5px;
  20. font-family:Monospace;
  21. font-size:13px;
  22. text-align:center;
  23. z-index:1000;
  24. }
  25. a {
  26. color: #ffffff;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - webgl2 sandbox.</div>
  32. <script src="../build/three.js"></script>
  33. <script src="js/libs/stats.min.js"></script>
  34. <script>
  35. var statsEnabled = true;
  36. var container, stats;
  37. var camera, scene, renderer;
  38. var mesh, zmesh, lightMesh, geometry;
  39. var mouseX = 0, mouseY = 0;
  40. var windowHalfX = window.innerWidth / 2;
  41. var windowHalfY = window.innerHeight / 2;
  42. init();
  43. animate();
  44. function init() {
  45. container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  48. camera.position.z = 3200;
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0, 0, 0.5 );
  51. scene.fog = new THREE.Fog( 0x000000, 1, 20000 );
  52. var light = new THREE.PointLight( 0xffffff );
  53. scene.add( light );
  54. var texture1 = new THREE.CanvasTexture( generateTexture( 0, 0.5, 1 ), THREE.UVMapping );
  55. var materials = [
  56. new THREE.MeshNormalMaterial(),
  57. new THREE.MeshDepthMaterial(),
  58. new THREE.MeshBasicMaterial( { color: 0x0066ff, blending: THREE.AdditiveBlending, transparent: true, depthWrite: false } ),
  59. new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ),
  60. new THREE.MeshBasicMaterial( { map: texture1, fog: false } ),
  61. new THREE.MeshLambertMaterial( { color: 0xdddddd } ),
  62. new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.FlatShading } ),
  63. new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, shading: THREE.SmoothShading } )
  64. ];
  65. var geometry = new THREE.SphereGeometry( 50, 32, 16 );
  66. for ( var i = 0; i < 5000; i ++ ) {
  67. // random order
  68. //var index = Math.floor( Math.random() * materials.length );
  69. // sort by material / geometry
  70. var index = Math.floor( ( i / 5000 ) * materials.length );
  71. var material = materials[ index ];
  72. var mesh = new THREE.Mesh( geometry, material );
  73. mesh.position.x = Math.random() * 10000 - 5000;
  74. mesh.position.y = Math.random() * 10000 - 5000;
  75. mesh.position.z = Math.random() * 10000 - 5000;
  76. //mesh.rotation.x = Math.random() * 360 * ( Math.PI / 180 );
  77. mesh.rotation.y = Math.random() * 2 * Math.PI;
  78. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 4 + 1;
  79. scene.add( mesh );
  80. }
  81. renderer = new THREE.WebGL2Renderer();
  82. renderer.setPixelRatio( window.devicePixelRatio );
  83. renderer.setSize( window.innerWidth, window.innerHeight );
  84. container.appendChild( renderer.domElement );
  85. if ( statsEnabled ) {
  86. stats = new Stats();
  87. container.appendChild( stats.dom );
  88. }
  89. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  90. //
  91. window.addEventListener( 'resize', onWindowResize, false );
  92. }
  93. function onWindowResize() {
  94. windowHalfX = window.innerWidth / 2;
  95. windowHalfY = window.innerHeight / 2;
  96. camera.aspect = window.innerWidth / window.innerHeight;
  97. camera.updateProjectionMatrix();
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. }
  100. function generateTexture( r, g, b ) {
  101. var canvas = document.createElement( 'canvas' );
  102. canvas.width = 256;
  103. canvas.height = 256;
  104. var context = canvas.getContext( '2d' );
  105. var image = context.getImageData( 0, 0, 256, 256 );
  106. var x = 0, y = 0, p;
  107. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  108. x = j % 256;
  109. y = x == 0 ? y + 1 : y;
  110. p = Math.floor( x ^ y );
  111. image.data[ i ] = ~~ p * r;
  112. image.data[ i + 1 ] = ~~ p * g;
  113. image.data[ i + 2 ] = ~~ p * b;
  114. image.data[ i + 3 ] = 255;
  115. }
  116. context.putImageData( image, 0, 0 );
  117. return canvas;
  118. }
  119. function onDocumentMouseMove(event) {
  120. mouseX = ( event.clientX - windowHalfX ) * 10;
  121. mouseY = ( event.clientY - windowHalfY ) * 10;
  122. }
  123. //
  124. function animate() {
  125. requestAnimationFrame( animate );
  126. render();
  127. if ( statsEnabled ) stats.update();
  128. }
  129. function render() {
  130. camera.position.x += ( mouseX - camera.position.x ) * .05;
  131. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  132. camera.lookAt( scene.position );
  133. renderer.render( scene, camera );
  134. }
  135. </script>
  136. </body>
  137. </html>