2
0

webgl_interactive_voxelpainter.html 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. #oldie { background-color: #ddd !important }
  15. </style>
  16. </head>
  17. <body>
  18. <script src="../build/Three.js"></script>
  19. <script src="js/Detector.js"></script>
  20. <script src="js/Stats.js"></script>
  21. <script src='js/DAT.GUI.min.js'></script>
  22. <script>
  23. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  24. var container, stats;
  25. var camera, cameraTarget, scene, renderer;
  26. var projector, plane, cube;
  27. var mouse2D, mouse3D, ray,
  28. rollOveredFace, isShiftDown = false,
  29. theta = 45, isCtrlDown = false;
  30. var rollOverMesh, rollOverMaterial, voxelPosition = new THREE.Vector3(), tmpVec = new THREE.Vector3();
  31. var cubeGeo, cubeMaterial;
  32. var i, intersector;
  33. var gui, voxelConfig = {
  34. orthographicProjection: false
  35. };
  36. init();
  37. animate();
  38. function init() {
  39. container = document.createElement( 'div' );
  40. document.body.appendChild( container );
  41. var info = document.createElement( 'div' );
  42. info.style.position = 'absolute';
  43. info.style.top = '10px';
  44. info.style.width = '100%';
  45. info.style.textAlign = 'center';
  46. info.innerHTML = '<a href="http://github.com/mrdoob/three.js" target="_blank">three.js</a> - voxel painter - webgl<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>';
  47. container.appendChild( info );
  48. scene = new THREE.Scene();
  49. camera = new THREE.CombinedCamera( window.innerWidth, window.innerHeight, 45, 1, 10000, -2000, 10000 );
  50. camera.position.y = 800;
  51. scene.add( camera );
  52. cameraTarget = new THREE.Vector3( 0, 0, 0 );
  53. // roll-over helpers
  54. rollOverGeo = new THREE.CubeGeometry( 50, 50, 50 );
  55. rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5, transparent: true } );
  56. rollOverMesh = new THREE.Mesh( rollOverGeo, rollOverMaterial );
  57. scene.add( rollOverMesh );
  58. // cubes
  59. cubeGeo = new THREE.CubeGeometry( 50, 50, 50 );
  60. cubeMaterial = new THREE.MeshLambertMaterial( { color: 0x00ff80, ambient: 0x00ff80, shading: THREE.FlatShading, map: THREE.ImageUtils.loadTexture( "textures/square-outline-textured.png" ) } );
  61. cubeMaterial.color.setHSV( 0.1, 0.7, 1.0 );
  62. cubeMaterial.ambient = cubeMaterial.color;
  63. // picking
  64. projector = new THREE.Projector();
  65. // grid
  66. plane = new THREE.Mesh( new THREE.PlaneGeometry( 1000, 1000, 20, 20 ), new THREE.MeshBasicMaterial( { color: 0x555555, wireframe: true } ) );
  67. plane.rotation.x = - Math.PI / 2;
  68. scene.add( plane );
  69. mouse2D = new THREE.Vector3( 0, 10000, 0.5 );
  70. // Lights
  71. var ambientLight = new THREE.AmbientLight( 0x606060 );
  72. scene.add( ambientLight );
  73. var directionalLight = new THREE.DirectionalLight( 0xffffff );
  74. directionalLight.position.set( 1, 0.75, 0.5 ).normalize();
  75. scene.add( directionalLight );
  76. renderer = new THREE.WebGLRenderer( { antialias: true, preserveDrawingBuffer: true } );
  77. renderer.setSize( window.innerWidth, window.innerHeight );
  78. container.appendChild( renderer.domElement );
  79. stats = new Stats();
  80. stats.domElement.style.position = 'absolute';
  81. stats.domElement.style.top = '0px';
  82. container.appendChild( stats.domElement );
  83. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  84. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  85. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  86. document.addEventListener( 'keyup', onDocumentKeyUp, false );
  87. //
  88. window.addEventListener( 'resize', onWindowResize, false );
  89. // add GUI
  90. gui = new DAT.GUI();
  91. gui.add(voxelConfig, 'orthographicProjection').onChange(function(){
  92. if ( voxelConfig.orthographicProjection ) {
  93. camera.toOrthographic();
  94. camera.position.x = 1000;
  95. camera.position.y = 707.106;
  96. camera.position.z = 1000;
  97. theta = 90;
  98. } else {
  99. camera.toPerspective();
  100. camera.position.y = 800;
  101. }
  102. });
  103. gui.close();
  104. }
  105. function onWindowResize() {
  106. camera.setSize( window.innerWidth, window.innerHeight );
  107. camera.updateProjectionMatrix();
  108. renderer.setSize( window.innerWidth, window.innerHeight );
  109. }
  110. function getRealIntersector( intersects ) {
  111. for( i = 0; i < intersects.length; i++ ) {
  112. intersector = intersects[ i ];
  113. if ( intersector.object != rollOverMesh ) {
  114. return intersector;
  115. }
  116. }
  117. return null;
  118. }
  119. function setVoxelPosition( intersector ) {
  120. tmpVec.copy( intersector.face.normal );
  121. voxelPosition.add( intersector.point, intersector.object.matrixRotationWorld.multiplyVector3( tmpVec ) );
  122. voxelPosition.x = Math.floor( voxelPosition.x / 50 ) * 50 + 25;
  123. voxelPosition.y = Math.floor( voxelPosition.y / 50 ) * 50 + 25;
  124. voxelPosition.z = Math.floor( voxelPosition.z / 50 ) * 50 + 25;
  125. }
  126. function onDocumentMouseMove( event ) {
  127. event.preventDefault();
  128. mouse2D.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  129. mouse2D.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  130. }
  131. function onDocumentMouseDown( event ) {
  132. event.preventDefault();
  133. var intersects = ray.intersectObjects( scene.children );
  134. if ( intersects.length > 0 ) {
  135. intersector = getRealIntersector( intersects );
  136. // delete cube
  137. if ( isCtrlDown ) {
  138. if ( intersector.object != plane ) {
  139. scene.remove( intersector.object );
  140. }
  141. // create cube
  142. } else {
  143. intersector = getRealIntersector( intersects );
  144. setVoxelPosition( intersector );
  145. var voxel = new THREE.Mesh( cubeGeo, cubeMaterial );
  146. voxel.position.copy( voxelPosition );
  147. voxel.matrixAutoUpdate = false;
  148. voxel.updateMatrix();
  149. scene.add( voxel );
  150. }
  151. }
  152. }
  153. function onDocumentKeyDown( event ) {
  154. switch( event.keyCode ) {
  155. case 16: isShiftDown = true; break;
  156. case 17: isCtrlDown = true; break;
  157. }
  158. }
  159. function onDocumentKeyUp( event ) {
  160. switch( event.keyCode ) {
  161. case 16: isShiftDown = false; break;
  162. case 17: isCtrlDown = false; break;
  163. }
  164. }
  165. function save() {
  166. window.open( renderer.domElement.toDataURL('image/png'), 'mywindow' );
  167. }
  168. //
  169. function animate() {
  170. requestAnimationFrame( animate );
  171. render();
  172. stats.update();
  173. }
  174. function render() {
  175. if ( isShiftDown ) {
  176. theta += mouse2D.x * 3;
  177. }
  178. ray = projector.pickingRay( mouse2D.clone(), camera );
  179. var intersects = ray.intersectObjects( scene.children );
  180. if ( intersects.length > 0 ) {
  181. intersector = getRealIntersector( intersects );
  182. if ( intersector ) {
  183. setVoxelPosition( intersector );
  184. rollOverMesh.position = voxelPosition;
  185. }
  186. }
  187. camera.position.x = 1400 * Math.sin( theta * Math.PI / 360 );
  188. camera.position.z = 1400 * Math.cos( theta * Math.PI / 360 );
  189. camera.lookAt( cameraTarget );
  190. renderer.render( scene, camera );
  191. }
  192. </script>
  193. </body>
  194. </html>