canvas_interactive_voxelpainter.html 6.5 KB

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