|
4 | 4 | <title>Minecraft Renderer</title> |
5 | 5 | <style> |
6 | 6 | body { margin: 0; } |
7 | | -canvas { display: block; width:100%;height:100%;} |
| 7 | +canvas { |
| 8 | + display: block; |
| 9 | + width:100%; |
| 10 | + height:100%; |
| 11 | + position:fixed; |
| 12 | + top:0px; |
| 13 | + left:0px |
| 14 | +} |
| 15 | +.inventoryBar{ |
| 16 | + position:fixed; |
| 17 | + bottom:0px; |
| 18 | + left:50%; |
| 19 | + width:378px; |
| 20 | + background:black; |
| 21 | + margin-left:-189px; |
| 22 | + height:40px; |
| 23 | +} |
| 24 | +.icell{ |
| 25 | + margin:1px; |
| 26 | + padding:0px; |
| 27 | + border:0px; |
| 28 | + width:40px; |
| 29 | + height:40px; |
| 30 | +} |
| 31 | +.cross{ |
| 32 | + z-index:999999999; |
| 33 | + position:fixed; |
| 34 | + top:50%; |
| 35 | + left:50%; |
| 36 | + margin-top:-15px; |
| 37 | + margin-left:-15px; |
| 38 | +} |
8 | 39 |
|
9 | 40 | </style> |
10 | 41 | </head> |
11 | 42 | <body> |
12 | 43 | <canvas id="c"></canvas> |
13 | | - <script src="js/jq2.js"></script> |
14 | | - <script src="js/firstPersonControls.js?xd"></script> |
| 44 | + <div class="gui"> |
| 45 | + <div class="inventoryBar"> |
| 46 | + <img class="icell"><img class="icell"><img class="icell"><img class="icell"><img class="icell"><img class="icell"><img class="icell"><img class="icell"><img class="icell"> |
| 47 | + </div> |
| 48 | + <div class="cross"> |
| 49 | + <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"> |
| 50 | + <line x1="15" y1="0" x2="15" y2="30" stroke="black" stroke-width="1" /> |
| 51 | + <line x1="0" y1="15" x2="30" y2="15" stroke="black" stroke-width="1" /> |
| 52 | + </svg> |
| 53 | + </div> |
| 54 | + </div> |
| 55 | + <script src="js/jq.js?10210281028012"></script> |
| 56 | + <script src="js/firstPersonControls.js?912912"></script> |
15 | 57 | <script type="module"> |
16 | | - |
17 | 58 | import * as THREE from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/build/three.module.js'; |
18 | 59 | import { BufferGeometryUtils } from 'https://threejsfundamentals.org/threejs/resources/threejs/r115/examples/jsm/utils/BufferGeometryUtils.js'; |
19 | 60 |
|
|
78 | 119 | if(this.cells[cellId]!=undefined){ |
79 | 120 | this.cells[cellId][this.parseVec(...voxel)]=v; |
80 | 121 | }else{ |
81 | | - this.cells[cellId]={}; |
82 | | - this.cells[cellId][this.parseVec(...voxel)]=v; |
| 122 | + this.cells[cellId]={ |
| 123 | + [this.parseVec(...voxel)]:v |
| 124 | + }; |
83 | 125 | } |
84 | 126 | this.cells[cellId].needsUpdate=true; |
85 | 127 | // console.log(this.neighbours) |
|
115 | 157 | }) |
116 | 158 | } |
117 | 159 | updateCellGeometry(x,y,z){ |
118 | | - console.log(`updating Chunk: ${x}:${y}:${z}`) |
| 160 | + console.warn(`updating Chunk: ${x}:${y}:${z}`) |
119 | 161 | const {textureAtlas}=this; |
120 | | - if(this.cells[this.parseVec(x,y,z)]==undefined || this.cells[this.parseVec(x,y,z)].needsUpdate){ |
| 162 | + if( this.cells[this.parseVec(x,y,z)].needsUpdate){ |
121 | 163 | var mesh=this.cells_meshes[this.parseVec(x,y,z)]; |
122 | 164 | var geometry=this.generateCellGeometry(x,y,z); |
123 | 165 | if(geometry!=null){ |
|
134 | 176 | }else{ |
135 | 177 | scene.remove(this.cells_meshes[this.parseVec(x,y,z)]) |
136 | 178 | delete this.cells_meshes[this.parseVec(x,y,z)] |
137 | | - delete this.cells[this.parseVec(x,y,z)] |
| 179 | + // delete this.cells[this.parseVec(x,y,z)] |
138 | 180 | } |
139 | 181 | try{ |
140 | 182 | this.cells[this.parseVec(x,y,z)].needsUpdate=false; |
|
264 | 306 | } |
265 | 307 |
|
266 | 308 | } |
| 309 | +class Inventory{ |
| 310 | + constructor(options){ |
| 311 | + this.cellSize=options.cellSize; |
| 312 | + this.windows=options.windows; |
| 313 | + } |
| 314 | +} |
267 | 315 | var canvas,renderer,scene,camera,raycaster; |
268 | 316 |
|
269 | 317 |
|
|
279 | 327 | camera = new THREE.PerspectiveCamera(75, 2, 0.1, 1000); |
280 | 328 | camera.rotation.order="YXZ" |
281 | 329 | camera.position.set(25,25,25) |
282 | | -FirstPersonControls(canvas,camera,0.07) |
| 330 | +FirstPersonControls(document.getElementById("cross"),camera,0.07) |
283 | 331 |
|
284 | 332 |
|
285 | 333 | var ambientLight = new THREE.AmbientLight( 0xcccccc ); |
|
295 | 343 | textureCols:16, |
296 | 344 | cellSize:16, |
297 | 345 | }) |
298 | | -var vari=50 |
| 346 | +var vari=100 |
| 347 | +// for(var i=0;i<vari;i++){ |
| 348 | +// for(var j=0;j<vari;j++){ |
| 349 | +// for(var k=0;k<vari;k++){ |
| 350 | +// world.setVoxel(i,j,k,2) |
| 351 | +// } |
| 352 | +// } |
| 353 | +// } |
| 354 | +// console.log(world.cells["1:1:1"]) |
| 355 | +// setTimeout(function (){ |
| 356 | +// console.log(world.cells["1:1:1"]) |
| 357 | +// },2000) |
299 | 358 | for (let y = 0; y < vari; ++y) { |
300 | 359 | for (let z = 0; z < vari; ++z) { |
301 | 360 | for (let x = 0; x < vari; ++x) { |
|
322 | 381 | var geometry = new THREE.BoxGeometry( 1, 1, 1 ); |
323 | 382 | // var material = new THREE.MeshBasicMaterial( {color: 0x00ff00} ); |
324 | 383 | var edges = new THREE.EdgesGeometry( geometry ); |
325 | | -var cube = new THREE.LineSegments( edges, new THREE.LineBasicMaterial( { color: 0x000000,linewidth:1 } ) ); |
| 384 | +var cube = new THREE.LineSegments( edges, new THREE.LineBasicMaterial( { color: 0x000000,linewidth:0.5 } ) ); |
| 385 | +// var cube=new THREE.Mesh() |
| 386 | + |
326 | 387 | var positionQ=[] |
327 | 388 | // cube.material.visible=false; |
| 389 | +var selected=false; |
328 | 390 | scene.add( cube ); |
329 | 391 | function raycast(){ |
330 | 392 | raycaster.setFromCamera( mouse, camera ); |
| 393 | + raycaster.far=10 |
331 | 394 | // calculate objects intersecting the picking ray |
332 | 395 | var intersects = raycaster.intersectObjects( scene.children ); |
333 | 396 | var miniDist=-1 |
|
346 | 409 |
|
347 | 410 | } |
348 | 411 | if(mini!=null){ |
| 412 | + selected=true; |
349 | 413 | cube.visible=true; |
350 | 414 | var xd=Math.round(mini.point.x) |
351 | 415 | var yd=Math.round(mini.point.y) |
|
394 | 458 | } |
395 | 459 | // console.log(mini.point) |
396 | 460 | }else{ |
| 461 | + selected=false; |
397 | 462 | cube.visible=false; |
398 | 463 | } |
399 | 464 |
|
|
405 | 470 | $(document).click(function (e){ |
406 | 471 | // console.log(positionQ) |
407 | 472 | // console.log(cube.position) |
408 | | - if(e.which==1){ |
409 | | - world.setVoxel(cube.position.x,cube.position.y,cube.position.z,0) |
| 473 | + if(selected){ |
| 474 | + if(e.which==1){ |
| 475 | + world.setVoxel(cube.position.x,cube.position.y,cube.position.z,0) |
| 476 | + }else{ |
| 477 | + world.setVoxel(positionQ[0],positionQ[1],positionQ[2],1) |
| 478 | + // console.log(world.cells[world.computeCellId(...positionQ)]) |
| 479 | + } |
410 | 480 | }else{ |
411 | | - |
412 | | - world.setVoxel(positionQ[0],positionQ[1],positionQ[2],1) |
| 481 | + console.error("Jesteś za daleko!") |
413 | 482 | } |
414 | 483 |
|
| 484 | + |
415 | 485 | }) |
416 | 486 |
|
417 | 487 | animate() |
|
0 commit comments