canvas_interactive_cubes.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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.js"></script>
  18. <script src="js/RequestAnimationFrame.js"></script>
  19. <script src="js/Stats.js"></script>
  20. <script>
  21. var container, stats;
  22. var camera, scene, projector, renderer;
  23. var particleMaterial;
  24. var objects = [];
  25. init();
  26. animate();
  27. function init() {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. var info = document.createElement( 'div' );
  31. info.style.position = 'absolute';
  32. info.style.top = '10px';
  33. info.style.width = '100%';
  34. info.style.textAlign = 'center';
  35. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - clickable objects';
  36. container.appendChild( info );
  37. camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  38. camera.useTarget = true;
  39. camera.position.y = 300;
  40. camera.position.z = 500;
  41. scene = new THREE.Scene();
  42. var geometry = new THREE.CubeGeometry( 100, 100, 100 );
  43. for ( var i = 0; i < 10; i ++ ) {
  44. 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 } ) ] );
  45. object.position.x = Math.random() * 800 - 400;
  46. object.position.y = Math.random() * 800 - 400;
  47. object.position.z = Math.random() * 800 - 400;
  48. object.scale.x = Math.random() * 2 + 1;
  49. object.scale.y = Math.random() * 2 + 1;
  50. object.scale.z = Math.random() * 2 + 1;
  51. object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
  52. object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
  53. object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
  54. scene.add( object );
  55. objects.push( object );
  56. }
  57. var PI2 = Math.PI * 2;
  58. particleMaterial = new THREE.ParticleCanvasMaterial( {
  59. color: 0x000000,
  60. program: function ( context ) {
  61. context.beginPath();
  62. context.arc( 0, 0, 1, 0, PI2, true );
  63. context.closePath();
  64. context.fill();
  65. }
  66. } );
  67. projector = new THREE.Projector();
  68. renderer = new THREE.CanvasRenderer();
  69. renderer.setSize( window.innerWidth, window.innerHeight );
  70. container.appendChild( renderer.domElement );
  71. stats = new Stats();
  72. stats.domElement.style.position = 'absolute';
  73. stats.domElement.style.top = '0px';
  74. container.appendChild( stats.domElement );
  75. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  76. }
  77. function onDocumentMouseDown( event ) {
  78. event.preventDefault();
  79. var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
  80. projector.unprojectVector( vector, camera );
  81. var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
  82. var intersects = ray.intersectObjects( objects );
  83. if ( intersects.length > 0 ) {
  84. intersects[ 0 ].object.materials[ 0 ].color.setHex( Math.random() * 0xffffff );
  85. var particle = new THREE.Particle( particleMaterial );
  86. particle.position = intersects[ 0 ].point;
  87. particle.scale.x = particle.scale.y = 8;
  88. scene.add( particle );
  89. }
  90. /*
  91. // Parse all the faces
  92. for ( var i in intersects ) {
  93. intersects[ i ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
  94. }
  95. */
  96. }
  97. //
  98. function animate() {
  99. requestAnimationFrame( animate );
  100. render();
  101. stats.update();
  102. }
  103. var radius = 600;
  104. var theta = 0;
  105. function render() {
  106. theta += 0.2;
  107. camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
  108. camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
  109. camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
  110. renderer.render( scene, camera );
  111. }
  112. </script>
  113. </body>
  114. </html>