webgl_interactive_voxelpainter.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - voxel painter - webgl<br>
  21. <strong>click</strong>: add voxel, <strong>shift + click</strong>: remove voxel
  22. </div>
  23. <script type="module">
  24. import * as THREE from '../build/three.module.js';
  25. var camera, scene, renderer;
  26. var plane;
  27. var mouse, raycaster, isShiftDown = false;
  28. var rollOverMesh, rollOverMaterial;
  29. var cubeGeo, cubeMaterial;
  30. var objects = [];
  31. init();
  32. render();
  33. function init() {
  34. camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 10000 );
  35. camera.position.set( 500, 800, 1300 );
  36. camera.lookAt( 0, 0, 0 );
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0xf0f0f0 );
  39. // roll-over helpers
  40. var rollOverGeo = new THREE.BoxBufferGeometry( 50, 50, 50 );
  41. rollOverMaterial = new THREE.MeshBasicMaterial( { color: 0xff0000, opacity: 0.5, transparent: true } );
  42. rollOverMesh = new THREE.Mesh( rollOverGeo, rollOverMaterial );
  43. scene.add( rollOverMesh );
  44. // cubes
  45. cubeGeo = new THREE.BoxBufferGeometry( 50, 50, 50 );
  46. cubeMaterial = new THREE.MeshLambertMaterial( { color: 0xfeb74c, map: new THREE.TextureLoader().load( 'textures/square-outline-textured.png' ) } );
  47. // grid
  48. var gridHelper = new THREE.GridHelper( 1000, 20 );
  49. scene.add( gridHelper );
  50. //
  51. raycaster = new THREE.Raycaster();
  52. mouse = new THREE.Vector2();
  53. var geometry = new THREE.PlaneBufferGeometry( 1000, 1000 );
  54. geometry.rotateX( - Math.PI / 2 );
  55. plane = new THREE.Mesh( geometry, new THREE.MeshBasicMaterial( { visible: false } ) );
  56. scene.add( plane );
  57. objects.push( plane );
  58. // lights
  59. var ambientLight = new THREE.AmbientLight( 0x606060 );
  60. scene.add( ambientLight );
  61. var directionalLight = new THREE.DirectionalLight( 0xffffff );
  62. directionalLight.position.set( 1, 0.75, 0.5 ).normalize();
  63. scene.add( directionalLight );
  64. renderer = new THREE.WebGLRenderer( { antialias: true } );
  65. renderer.setPixelRatio( window.devicePixelRatio );
  66. renderer.setSize( window.innerWidth, window.innerHeight );
  67. document.body.appendChild( renderer.domElement );
  68. document.addEventListener( 'mousemove', onDocumentMouseMove, false );
  69. document.addEventListener( 'mousedown', onDocumentMouseDown, false );
  70. document.addEventListener( 'keydown', onDocumentKeyDown, false );
  71. document.addEventListener( 'keyup', onDocumentKeyUp, false );
  72. //
  73. window.addEventListener( 'resize', onWindowResize, false );
  74. }
  75. function onWindowResize() {
  76. camera.aspect = window.innerWidth / window.innerHeight;
  77. camera.updateProjectionMatrix();
  78. renderer.setSize( window.innerWidth, window.innerHeight );
  79. }
  80. function onDocumentMouseMove( event ) {
  81. event.preventDefault();
  82. mouse.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
  83. raycaster.setFromCamera( mouse, camera );
  84. var intersects = raycaster.intersectObjects( objects );
  85. if ( intersects.length > 0 ) {
  86. var intersect = intersects[ 0 ];
  87. rollOverMesh.position.copy( intersect.point ).add( intersect.face.normal );
  88. rollOverMesh.position.divideScalar( 50 ).floor().multiplyScalar( 50 ).addScalar( 25 );
  89. }
  90. render();
  91. }
  92. function onDocumentMouseDown( event ) {
  93. event.preventDefault();
  94. mouse.set( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1 );
  95. raycaster.setFromCamera( mouse, camera );
  96. var intersects = raycaster.intersectObjects( objects );
  97. if ( intersects.length > 0 ) {
  98. var intersect = intersects[ 0 ];
  99. // delete cube
  100. if ( isShiftDown ) {
  101. if ( intersect.object !== plane ) {
  102. scene.remove( intersect.object );
  103. objects.splice( objects.indexOf( intersect.object ), 1 );
  104. }
  105. // create cube
  106. } else {
  107. var voxel = new THREE.Mesh( cubeGeo, cubeMaterial );
  108. voxel.position.copy( intersect.point ).add( intersect.face.normal );
  109. voxel.position.divideScalar( 50 ).floor().multiplyScalar( 50 ).addScalar( 25 );
  110. scene.add( voxel );
  111. objects.push( voxel );
  112. }
  113. render();
  114. }
  115. }
  116. function onDocumentKeyDown( event ) {
  117. switch ( event.keyCode ) {
  118. case 16: isShiftDown = true; break;
  119. }
  120. }
  121. function onDocumentKeyUp( event ) {
  122. switch ( event.keyCode ) {
  123. case 16: isShiftDown = false; break;
  124. }
  125. }
  126. function render() {
  127. renderer.render( scene, camera );
  128. }
  129. </script>
  130. </body>
  131. </html>