webgl_sandbox.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js - webgl 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> webgl - sandbox</div>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import Stats from './jsm/libs/stats.module.js';
  14. import { BasicShader } from './jsm/shaders/BasicShader.js';
  15. var statsEnabled = true;
  16. var container, stats;
  17. var camera, scene, renderer;
  18. var mouseX = 0, mouseY = 0;
  19. var windowHalfX = window.innerWidth / 2;
  20. var windowHalfY = window.innerHeight / 2;
  21. init();
  22. animate();
  23. function init() {
  24. container = document.createElement( 'div' );
  25. document.body.appendChild( container );
  26. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  27. camera.position.z = 3200;
  28. scene = new THREE.Scene();
  29. scene.fog = new THREE.Fog( 0x000000, 1, 20000 );
  30. var light = new THREE.PointLight( 0xffffff );
  31. scene.add( light );
  32. var shader = BasicShader;
  33. var uniforms = shader.uniforms;
  34. var vertexShader = shader.vertexShader;
  35. var fragmentShader = shader.fragmentShader;
  36. var texture1 = new THREE.CanvasTexture( generateTexture( 0, 0.5, 1 ), THREE.UVMapping );
  37. var texture2 = new THREE.TextureLoader().load( 'textures/land_ocean_ice_cloud_2048.jpg' );
  38. var materials = [
  39. new THREE.MeshNormalMaterial(),
  40. new THREE.MeshDepthMaterial(),
  41. new THREE.MeshBasicMaterial( { color: 0x0066ff, blending: THREE.AdditiveBlending, transparent: true, depthWrite: false } ),
  42. new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } ),
  43. new THREE.MeshBasicMaterial( { map: texture1, fog: false } ),
  44. new THREE.MeshBasicMaterial( { map: texture2 } ),
  45. new THREE.ShaderMaterial( { uniforms: uniforms, vertexShader: vertexShader, fragmentShader: fragmentShader, transparent: true } ),
  46. new THREE.MeshLambertMaterial( { map: texture2 } ),
  47. new THREE.MeshLambertMaterial( { color: 0xdddddd } ),
  48. new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30, flatShading: true } ),
  49. new THREE.MeshPhongMaterial( { color: 0xdddddd, specular: 0x009900, shininess: 30 } )
  50. ];
  51. var geometry = new THREE.SphereBufferGeometry( 50, 32, 16 );
  52. for ( var i = 0; i < 5000; i ++ ) {
  53. // random order
  54. //var index = Math.floor( Math.random() * materials.length );
  55. // sort by material / geometry
  56. var index = Math.floor( ( i / 5000 ) * materials.length );
  57. var material = materials[ index ];
  58. var mesh = new THREE.Mesh( geometry, material );
  59. mesh.position.x = Math.random() * 10000 - 5000;
  60. mesh.position.y = Math.random() * 10000 - 5000;
  61. mesh.position.z = Math.random() * 10000 - 5000;
  62. //mesh.rotation.x = Math.random() * 360 * ( Math.PI / 180 );
  63. mesh.rotation.y = Math.random() * 2 * Math.PI;
  64. mesh.scale.x = mesh.scale.y = mesh.scale.z = Math.random() * 4 + 1;
  65. scene.add( mesh );
  66. }
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setPixelRatio( window.devicePixelRatio );
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. container.appendChild( renderer.domElement );
  71. if ( statsEnabled ) {
  72. stats = new Stats();
  73. container.appendChild( stats.dom );
  74. }
  75. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  76. //
  77. window.addEventListener( 'resize', onWindowResize, false );
  78. }
  79. function onWindowResize() {
  80. windowHalfX = window.innerWidth / 2;
  81. windowHalfY = window.innerHeight / 2;
  82. camera.aspect = window.innerWidth / window.innerHeight;
  83. camera.updateProjectionMatrix();
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. }
  86. function generateTexture( r, g, b ) {
  87. var canvas = document.createElement( 'canvas' );
  88. canvas.width = 256;
  89. canvas.height = 256;
  90. var context = canvas.getContext( '2d' );
  91. var image = context.getImageData( 0, 0, 256, 256 );
  92. var x = 0, y = 0, p;
  93. for ( var i = 0, j = 0, l = image.data.length; i < l; i += 4, j ++ ) {
  94. x = j % 256;
  95. y = x == 0 ? y + 1 : y;
  96. p = Math.floor( x ^ y );
  97. image.data[ i ] = ~ ~ p * r;
  98. image.data[ i + 1 ] = ~ ~ p * g;
  99. image.data[ i + 2 ] = ~ ~ p * b;
  100. image.data[ i + 3 ] = 255;
  101. }
  102. context.putImageData( image, 0, 0 );
  103. return canvas;
  104. }
  105. function onDocumentMouseMove( event ) {
  106. mouseX = ( event.clientX - windowHalfX ) * 10;
  107. mouseY = ( event.clientY - windowHalfY ) * 10;
  108. }
  109. //
  110. function animate() {
  111. requestAnimationFrame( animate );
  112. render();
  113. if ( statsEnabled ) stats.update();
  114. }
  115. function render() {
  116. camera.position.x += ( mouseX - camera.position.x ) * .05;
  117. camera.position.y += ( - mouseY - camera.position.y ) * .05;
  118. camera.lookAt( scene.position );
  119. renderer.render( scene, camera );
  120. }
  121. </script>
  122. </body>
  123. </html>