canvas_interactive_cubes.html 4.2 KB

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