webgl_geometry_minecraft_oculusrift.html 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - geometry - minecraft - Oculus Rift</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. color: rgb(168, 148, 0);
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #bfd1e5;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #a06851;
  24. }
  25. #oldie {
  26. background:rgb(100,0,0) !important;
  27. color:#fff !important;
  28. margin-top:10em !important;
  29. }
  30. #oldie a { color:#fff }
  31. </style>
  32. </head>
  33. <body>
  34. <div id="container"><br /><br /><br /><br /><br />Generating world...</div>
  35. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> - <a href="http://www.minecraft.net/" target="_blank">minecraft</a> demo [oculus rift]. featuring <a href="http://painterlypack.net/" target="_blank">painterly pack</a><br />(left click: forward, right click: backward, o,p: distortion [<span id='distVal'>0</span>], k,l; eye separation [<span id='sepVal'>0</span>], n,m; fov [<span id='fovVal'>0</span>])</div>
  36. <script src="../build/three.min.js"></script>
  37. <script src="js/controls/FirstPersonControls.js"></script>
  38. <script src="js/ImprovedNoise.js"></script>
  39. <script src="js/Detector.js"></script>
  40. <script src="js/effects/OculusRiftEffect.js"></script>
  41. <script src="js/libs/stats.min.js"></script>
  42. <script>
  43. if ( ! Detector.webgl ) {
  44. Detector.addGetWebGLMessage();
  45. document.getElementById( 'container' ).innerHTML = "";
  46. }
  47. var container, stats;
  48. var camera, controls, scene, renderer;
  49. var mesh;
  50. var worldWidth = 128, worldDepth = 128,
  51. worldHalfWidth = worldWidth / 2, worldHalfDepth = worldDepth / 2,
  52. data = generateHeight( worldWidth, worldDepth );
  53. var clock = new THREE.Clock();
  54. init();
  55. animate();
  56. function init() {
  57. container = document.getElementById( 'container' );
  58. camera = new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 1, 20000 );
  59. camera.position.y = getY( worldHalfWidth, worldHalfDepth ) * 100 + 100;
  60. controls = new THREE.FirstPersonControls( camera );
  61. controls.movementSpeed = 1000;
  62. controls.lookSpeed = 0.125;
  63. controls.lookVertical = true;
  64. scene = new THREE.Scene();
  65. // sides
  66. var matrix = new THREE.Matrix4();
  67. var pxGeometry = new THREE.PlaneGeometry( 100, 100 );
  68. pxGeometry.faces[ 0 ].materialIndex = 1;
  69. pxGeometry.applyMatrix( matrix.makeRotationY( Math.PI / 2 ) );
  70. pxGeometry.applyMatrix( matrix.makeTranslation( 50, 0, 0 ) );
  71. var nxGeometry = new THREE.PlaneGeometry( 100, 100 );
  72. nxGeometry.faces[ 0 ].materialIndex = 1;
  73. nxGeometry.applyMatrix( matrix.makeRotationY( - Math.PI / 2 ) );
  74. nxGeometry.applyMatrix( matrix.makeTranslation( - 50, 0, 0 ) );
  75. var pyGeometry = new THREE.PlaneGeometry( 100, 100 );
  76. pyGeometry.faces[ 0 ].materialIndex = 0;
  77. pyGeometry.applyMatrix( matrix.makeRotationX( - Math.PI / 2 ) );
  78. pyGeometry.applyMatrix( matrix.makeTranslation( 0, 50, 0 ) );
  79. var pzGeometry = new THREE.PlaneGeometry( 100, 100 );
  80. pzGeometry.faces[ 0 ].materialIndex = 1;
  81. pzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, 50 ) );
  82. var nzGeometry = new THREE.PlaneGeometry( 100, 100 );
  83. nzGeometry.faces[ 0 ].materialIndex = 1;
  84. nzGeometry.applyMatrix( matrix.makeRotationY( Math.PI ) );
  85. nzGeometry.applyMatrix( matrix.makeTranslation( 0, 0, -50 ) );
  86. //
  87. var geometry = new THREE.Geometry();
  88. var dummy = new THREE.Mesh();
  89. for ( var z = 0; z < worldDepth; z ++ ) {
  90. for ( var x = 0; x < worldWidth; x ++ ) {
  91. var h = getY( x, z );
  92. dummy.position.x = x * 100 - worldHalfWidth * 100;
  93. dummy.position.y = h * 100;
  94. dummy.position.z = z * 100 - worldHalfDepth * 100;
  95. var px = getY( x + 1, z );
  96. var nx = getY( x - 1, z );
  97. var pz = getY( x, z + 1 );
  98. var nz = getY( x, z - 1 );
  99. dummy.geometry = pyGeometry;
  100. THREE.GeometryUtils.merge( geometry, dummy );
  101. if ( ( px != h && px != h + 1 ) || x == 0 ) {
  102. dummy.geometry = pxGeometry;
  103. THREE.GeometryUtils.merge( geometry, dummy );
  104. }
  105. if ( ( nx != h && nx != h + 1 ) || x == worldWidth - 1 ) {
  106. dummy.geometry = nxGeometry;
  107. THREE.GeometryUtils.merge( geometry, dummy );
  108. }
  109. if ( ( pz != h && pz != h + 1 ) || z == worldDepth - 1 ) {
  110. dummy.geometry = pzGeometry;
  111. THREE.GeometryUtils.merge( geometry, dummy );
  112. }
  113. if ( ( nz != h && nz != h + 1 ) || z == 0 ) {
  114. dummy.geometry = nzGeometry;
  115. THREE.GeometryUtils.merge( geometry, dummy );
  116. }
  117. }
  118. }
  119. var textureGrass = THREE.ImageUtils.loadTexture( 'textures/minecraft/grass.png' );
  120. textureGrass.magFilter = THREE.NearestFilter;
  121. textureGrass.minFilter = THREE.LinearMipMapLinearFilter;
  122. var textureGrassDirt = THREE.ImageUtils.loadTexture( 'textures/minecraft/grass_dirt.png' );
  123. textureGrassDirt.magFilter = THREE.NearestFilter;
  124. textureGrassDirt.minFilter = THREE.LinearMipMapLinearFilter;
  125. var material1 = new THREE.MeshLambertMaterial( { map: textureGrass, ambient: 0xbbbbbb } );
  126. var material2 = new THREE.MeshLambertMaterial( { map: textureGrassDirt, ambient: 0xbbbbbb } );
  127. var mesh = new THREE.Mesh( geometry, new THREE.MeshFaceMaterial( [ material1, material2 ] ) );
  128. scene.add( mesh );
  129. var ambientLight = new THREE.AmbientLight( 0xcccccc );
  130. scene.add( ambientLight );
  131. var directionalLight = new THREE.DirectionalLight( 0xffffff, 2 );
  132. directionalLight.position.set( 1, 1, 0.5 ).normalize();
  133. scene.add( directionalLight );
  134. renderer = new THREE.WebGLRenderer();
  135. renderer.setSize( window.innerWidth, window.innerHeight );
  136. effect = new THREE.OculusRiftEffect( renderer );
  137. effect.setSize( window.innerWidth, window.innerHeight );
  138. // Right Oculus Parameters are yet to be determined
  139. effect.separation = 20;
  140. effect.distortion = 0.1;
  141. effect.fov = 110;
  142. document.getElementById( 'distVal' ).innerHTML = effect.distortion.toFixed(2);
  143. document.getElementById( 'sepVal' ).innerHTML = effect.separation;
  144. document.getElementById( 'fovVal' ).innerHTML = effect.fov;
  145. container.innerHTML = "";
  146. container.appendChild( renderer.domElement );
  147. stats = new Stats();
  148. stats.domElement.style.position = 'absolute';
  149. stats.domElement.style.top = '0px';
  150. container.appendChild( stats.domElement );
  151. // GUI
  152. window.addEventListener( 'resize', onWindowResize, false );
  153. document.addEventListener( 'keydown', keyPressed, false );
  154. }
  155. function onWindowResize() {
  156. camera.aspect = window.innerWidth / window.innerHeight;
  157. camera.updateProjectionMatrix();
  158. effect.setSize( window.innerWidth, window.innerHeight );
  159. controls.handleResize();
  160. }
  161. function keyPressed (event) {
  162. switch ( event.keyCode ) {
  163. case 79: /*O*/ effect.distortion -= 0.01; document.getElementById( 'distVal' ).innerHTML = effect.distortion.toFixed(2); break;
  164. case 80: /*P*/ effect.distortion += 0.01; document.getElementById( 'distVal' ).innerHTML = effect.distortion.toFixed(2); break;
  165. case 75: /*K*/ effect.separation -= 1; document.getElementById( 'sepVal' ).innerHTML = effect.separation; break;
  166. case 76: /*L*/ effect.separation += 1; document.getElementById( 'sepVal' ).innerHTML = effect.separation; break;
  167. case 78: /*N*/ effect.fov -= 1; document.getElementById( 'fovVal' ).innerHTML = effect.fov; break;
  168. case 77: /*M*/ effect.fov += 1; document.getElementById( 'fovVal' ).innerHTML = effect.fov; break;
  169. }
  170. }
  171. function generateHeight( width, height ) {
  172. var data = [], perlin = new ImprovedNoise(),
  173. size = width * height, quality = 2, z = Math.random() * 100;
  174. for ( var j = 0; j < 4; j ++ ) {
  175. if ( j == 0 ) for ( var i = 0; i < size; i ++ ) data[ i ] = 0;
  176. for ( var i = 0; i < size; i ++ ) {
  177. var x = i % width, y = ( i / width ) | 0;
  178. data[ i ] += perlin.noise( x / quality, y / quality, z ) * quality;
  179. }
  180. quality *= 4
  181. }
  182. return data;
  183. }
  184. function getY( x, z ) {
  185. return ( data[ x + z * worldWidth ] * 0.2 ) | 0;
  186. }
  187. //
  188. function animate() {
  189. requestAnimationFrame( animate );
  190. render();
  191. stats.update();
  192. }
  193. function render() {
  194. controls.update( clock.getDelta() );
  195. effect.render( scene, camera );
  196. }
  197. </script>
  198. </body>
  199. </html>