canvas_interactive_voxelpainter.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE HTML>
  2. <html lang="en">
  3. <head>
  4. <title>three.js canvas - interactive - voxel painter</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="../src/extras/primitives/Cube.js"></script>
  18. <script type="text/javascript" src="../src/extras/primitives/Plane.js"></script>
  19. <script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
  20. <script type="text/javascript" src="js/Stats.js"></script>
  21. <script type="text/javascript">
  22. var container, stats;
  23. var camera, scene, renderer;
  24. var projector, plane, cube;
  25. var mouse2D, mouse3D, ray,
  26. rollOveredFace, isShiftDown = false,
  27. theta = 45, isCtrlDown = false;
  28. init();
  29. animate();
  30. function init() {
  31. container = document.createElement( 'div' );
  32. document.body.appendChild( container );
  33. var info = document.createElement( 'div' );
  34. info.style.position = 'absolute';
  35. info.style.top = '10px';
  36. info.style.width = '100%';
  37. info.style.textAlign = 'center';
  38. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - voxel painter<br /><strong>click</strong>: add voxel, <strong>control + click</strong>: remove voxel, <strong>shift + click</strong>: rotate, <a href="javascript:save();return false;">save .png</a>';
  39. container.appendChild( info );
  40. camera = new THREE.Camera( 40, window.innerWidth / window.innerHeight, 1, 10000 );
  41. camera.position.y = 800;
  42. camera.target.position.y = 200;
  43. scene = new THREE.Scene();
  44. // Grid
  45. var geometry = new THREE.Geometry();
  46. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( - 500, 0, 0 ) ) );
  47. geometry.vertices.push( new THREE.Vertex( new THREE.Vector3( 500, 0, 0 ) ) );
  48. var material = new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } );
  49. for ( var i = 0; i <= 20; i ++ ) {
  50. var line = new THREE.Line( geometry, material );
  51. line.position.z = ( i * 50 ) - 500;
  52. scene.addObject( line );
  53. var line = new THREE.Line( geometry, material );
  54. line.position.x = ( i * 50 ) - 500;
  55. line.rotation.y = 90 * Math.PI / 180;
  56. scene.addObject( line );
  57. }
  58. projector = new THREE.Projector();
  59. plane = new THREE.Mesh( new Plane( 1000, 1000, 20, 20 ), new THREE.MeshFaceMaterial() );
  60. plane.rotation.x = - 90 * Math.PI / 180;
  61. scene.addObject( plane );
  62. mouse2D = new THREE.Vector3( 0, 10000, 0.5 );
  63. ray = new THREE.Ray( camera.position, null );
  64. // Lights
  65. var ambientLight = new THREE.AmbientLight( 0x606060 );
  66. scene.addLight( ambientLight );
  67. var directionalLight = new THREE.DirectionalLight( 0xffffff );
  68. directionalLight.position.x = Math.random() - 0.5;
  69. directionalLight.position.y = Math.random() - 0.5;
  70. directionalLight.position.z = Math.random() - 0.5;
  71. directionalLight.position.normalize();
  72. scene.addLight( directionalLight );
  73. var directionalLight = new THREE.DirectionalLight( 0x808080 );
  74. directionalLight.position.x = Math.random() - 0.5;
  75. directionalLight.position.y = Math.random() - 0.5;
  76. directionalLight.position.z = Math.random() - 0.5;
  77. directionalLight.position.normalize();
  78. scene.addLight( directionalLight );
  79. renderer = new THREE.CanvasRenderer();
  80. renderer.setSize( window.innerWidth, window.innerHeight );
  81. container.appendChild(renderer.domElement);
  82. stats = new Stats();
  83. stats.domElement.style.position = 'absolute';
  84. stats.domElement.style.top = '0px';
  85. container.appendChild( stats.domElement );
  86. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  87. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  88. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  89. document.addEventListener( 'keyup', onDocumentKeyUp, false );
  90. }
  91. function onDocumentMouseMove( event ) {
  92. event.preventDefault();
  93. mouse2D.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  94. mouse2D.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  95. }
  96. function onDocumentMouseDown( event ) {
  97. event.preventDefault();
  98. var intersects = ray.intersectScene( scene );
  99. if ( intersects.length > 0 ) {
  100. if ( isCtrlDown ) {
  101. if ( intersects[ 0 ].object != plane ) {
  102. scene.removeObject( intersects[ 0 ].object );
  103. }
  104. } else {
  105. var position = new THREE.Vector3().add( intersects[ 0 ].point, intersects[ 0 ].object.matrixRotation.multiplyVector3( intersects[ 0 ].face.normal.clone() ) );
  106. var voxel = new THREE.Mesh( new Cube( 50, 50, 50 ), [ new THREE.MeshLambertMaterial( { color: 0x00ff80, opacity: 1, shading: THREE.FlatShading } ), new THREE.MeshFaceMaterial() ] );
  107. voxel.position.x = Math.floor( position.x / 50 ) * 50 + 25;
  108. voxel.position.y = Math.floor( position.y / 50 ) * 50 + 25;
  109. voxel.position.z = Math.floor( position.z / 50 ) * 50 + 25;
  110. voxel.overdraw = true;
  111. scene.addObject( voxel );
  112. }
  113. }
  114. }
  115. function onDocumentKeyDown( event ) {
  116. switch( event.keyCode ) {
  117. case 16: isShiftDown = true; break;
  118. case 17: isCtrlDown = true; break;
  119. }
  120. }
  121. function onDocumentKeyUp( event ) {
  122. switch( event.keyCode ) {
  123. case 16: isShiftDown = false; break;
  124. case 17: isCtrlDown = false; break;
  125. }
  126. }
  127. function save() {
  128. window.open( renderer.domElement.toDataURL('image/png'), 'mywindow' );
  129. }
  130. //
  131. function animate() {
  132. requestAnimationFrame( animate );
  133. render();
  134. stats.update();
  135. }
  136. function render() {
  137. if ( isShiftDown ) {
  138. theta += mouse2D.x * 3;
  139. }
  140. mouse3D = projector.unprojectVector( mouse2D.clone(), camera );
  141. ray.direction = mouse3D.subSelf( camera.position ).normalize();
  142. var intersects = ray.intersectScene( scene );
  143. // console.log( intersects );
  144. if ( intersects.length > 0 ) {
  145. if ( intersects[ 0 ].face != rollOveredFace ) {
  146. if ( rollOveredFace ) rollOveredFace.materials = [];
  147. rollOveredFace = intersects[ 0 ].face;
  148. rollOveredFace.materials = [ new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5 } ) ];
  149. }
  150. } else if ( rollOveredFace ) {
  151. rollOveredFace.materials = [];
  152. rollOveredFace = null;
  153. }
  154. camera.position.x = 1400 * Math.sin( theta * Math.PI / 360 );
  155. camera.position.z = 1400 * Math.cos( theta * Math.PI / 360 );
  156. renderer.render( scene, camera );
  157. }
  158. </script>
  159. </body>
  160. </html>