canvas_interactive_cubes.html 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - interactive - cubes</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. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.min.js"></script>
  18. <script src="js/libs/stats.min.js"></script>
  19. <script>
  20. var container, stats;
  21. var camera, scene, projector, renderer;
  22. var particleMaterial;
  23. var objects = [];
  24. init();
  25. animate();
  26. function init() {
  27. container = document.createElement( 'div' );
  28. document.body.appendChild( container );
  29. var info = document.createElement( 'div' );
  30. info.style.position = 'absolute';
  31. info.style.top = '10px';
  32. info.style.width = '100%';
  33. info.style.textAlign = 'center';
  34. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> - clickable objects';
  35. container.appendChild( info );
  36. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.set( 0, 300, 500 );
  38. scene = new THREE.Scene();
  39. var geometry = new THREE.BoxGeometry( 100, 100, 100 );
  40. for ( var i = 0; i < 10; i ++ ) {
  41. var object = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5 } ) );
  42. object.position.x = Math.random() * 800 - 400;
  43. object.position.y = Math.random() * 800 - 400;
  44. object.position.z = Math.random() * 800 - 400;
  45. object.scale.x = Math.random() * 2 + 1;
  46. object.scale.y = Math.random() * 2 + 1;
  47. object.scale.z = Math.random() * 2 + 1;
  48. object.rotation.x = Math.random() * 2 * Math.PI;
  49. object.rotation.y = Math.random() * 2 * Math.PI;
  50. object.rotation.z = Math.random() * 2 * Math.PI;
  51. scene.add( object );
  52. objects.push( object );
  53. }
  54. var PI2 = Math.PI * 2;
  55. particleMaterial = new THREE.SpriteCanvasMaterial( {
  56. color: 0x000000,
  57. program: function ( context ) {
  58. context.beginPath();
  59. context.arc( 0, 0, 0.5, 0, PI2, true );
  60. context.fill();
  61. }
  62. } );
  63. projector = new THREE.Projector();
  64. renderer = new THREE.CanvasRenderer();
  65. renderer.setClearColor( 0xf0f0f0 );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. container.appendChild( renderer.domElement );
  68. stats = new Stats();
  69. stats.domElement.style.position = 'absolute';
  70. stats.domElement.style.top = '0px';
  71. container.appendChild( stats.domElement );
  72. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  73. //
  74. window.addEventListener( 'resize', onWindowResize, false );
  75. }
  76. function onWindowResize() {
  77. camera.aspect = window.innerWidth / window.innerHeight;
  78. camera.updateProjectionMatrix();
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. }
  81. function onDocumentMouseDown( event ) {
  82. event.preventDefault();
  83. var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
  84. projector.unprojectVector( vector, camera );
  85. var raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
  86. var intersects = raycaster.intersectObjects( objects );
  87. if ( intersects.length > 0 ) {
  88. intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );
  89. var particle = new THREE.Sprite( particleMaterial );
  90. particle.position.copy( intersects[ 0 ].point );
  91. particle.scale.x = particle.scale.y = 16;
  92. scene.add( particle );
  93. }
  94. /*
  95. // Parse all the faces
  96. for ( var i in intersects ) {
  97. intersects[ i ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
  98. }
  99. */
  100. }
  101. //
  102. function animate() {
  103. requestAnimationFrame( animate );
  104. render();
  105. stats.update();
  106. }
  107. var radius = 600;
  108. var theta = 0;
  109. function render() {
  110. theta += 0.1;
  111. camera.position.x = radius * Math.sin( THREE.Math.degToRad( theta ) );
  112. camera.position.y = radius * Math.sin( THREE.Math.degToRad( theta ) );
  113. camera.position.z = radius * Math.cos( THREE.Math.degToRad( theta ) );
  114. camera.lookAt( scene.position );
  115. renderer.render( scene, camera );
  116. }
  117. </script>
  118. </body>
  119. </html>