canvas_interactive_cubes.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - interactive - cubes</title>
  5. <meta charset="utf-8">
  6. <style type="text/css">
  7. body {
  8. font-family: Monospace;
  9. background-color: #f0f0f0;
  10. margin: 0px;
  11. overflow: hidden;
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <script type="text/javascript" src="../build/Three.js"></script>
  17. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  18. <script type="text/javascript" src="js/Stats.js"></script>
  19. <script type="text/javascript">
  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://github.com/mrdoob/three.js" target="_blank">three.js</a> - clickable objects';
  35. container.appendChild( info );
  36. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  37. camera.position.y = 300;
  38. camera.position.z = 500;
  39. scene = new THREE.Scene();
  40. var geometry = new THREE.CubeGeometry( 100, 100, 100 );
  41. for ( var i = 0; i < 10; i ++ ) {
  42. var object = new THREE.Mesh( geometry, [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5 } ), new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, wireframe: true } ) ] );
  43. object.position.x = Math.random() * 800 - 400;
  44. object.position.y = Math.random() * 800 - 400;
  45. object.position.z = Math.random() * 800 - 400;
  46. object.scale.x = Math.random() * 2 + 1;
  47. object.scale.y = Math.random() * 2 + 1;
  48. object.scale.z = Math.random() * 2 + 1;
  49. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  50. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  51. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  52. scene.add( object );
  53. objects.push( object );
  54. }
  55. var PI2 = Math.PI * 2;
  56. particleMaterial = new THREE.ParticleCanvasMaterial( {
  57. color: 0x000000,
  58. program: function ( context ) {
  59. context.beginPath();
  60. context.arc( 0, 0, 1, 0, PI2, true );
  61. context.closePath();
  62. context.fill();
  63. }
  64. } );
  65. projector = new THREE.Projector();
  66. renderer = new THREE.CanvasRenderer();
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. container.appendChild( renderer.domElement );
  69. stats = new Stats();
  70. stats.domElement.style.position = 'absolute';
  71. stats.domElement.style.top = '0px';
  72. container.appendChild( stats.domElement );
  73. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  74. }
  75. function onDocumentMouseDown( event ) {
  76. event.preventDefault();
  77. var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
  78. projector.unprojectVector( vector, camera );
  79. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  80. var intersects = ray.intersectObjects( objects );
  81. if ( intersects.length > 0 ) {
  82. intersects[ 0 ].object.materials[ 0 ].color.setHex( Math.random() * 0xffffff );
  83. var particle = new THREE.Particle( particleMaterial );
  84. particle.position = intersects[ 0 ].point;
  85. particle.scale.x = particle.scale.y = 8;
  86. scene.add( particle );
  87. }
  88. /*
  89. // Parse all the faces
  90. for ( var i in intersects ) {
  91. intersects[ i ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
  92. }
  93. */
  94. }
  95. //
  96. function animate() {
  97. requestAnimationFrame( animate );
  98. render();
  99. stats.update();
  100. }
  101. var radius = 600;
  102. var theta = 0;
  103. function render() {
  104. theta += 0.2;
  105. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  106. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  107. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  108. renderer.render( scene, camera );
  109. }
  110. </script>
  111. </body>
  112. </html>