interactive_voxelpainter.html 6.3 KB

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