@@ -793,61 +793,79 @@ <h3>Gra zatrzymana</h3>
793793 } ) ;
794794 }
795795
796+ //World
797+ {
798+ $ . get ( "assets/textureMapping.json" , function ( textureMapping ) {
799+ textureAtlasMapping = textureMapping
800+ } )
801+ var texture = new THREE . TextureLoader ( ) . load ( 'assets/textures/textureAtlas.png' ) ;
802+ texture . magFilter = THREE . NearestFilter ;
796803
797- $ . get ( "assets/textureMapping.json" , function ( textureMapping ) {
798- textureAtlasMapping = textureMapping
799- } )
800- var texture = new THREE . TextureLoader ( ) . load ( 'assets/textures/textureAtlas.png' ) ;
801- texture . magFilter = THREE . NearestFilter ;
802-
803- var worldMaterial = new THREE . MeshLambertMaterial ( {
804- map : texture ,
805- side : 0
806- } )
807-
808- world = new Terrain ( {
809- textureMaterial : worldMaterial ,
810- textureRows : 27 ,
811- textureCols : 27 ,
812- cellSize : 16 ,
813- } )
814- $ . get ( `assets/blocks.json?${ uuidv4 ( ) } ` , function ( blocks ) {
815- world . blocks = blocks
816- loadBlocksModels ( )
817- } )
818-
804+ var worldMaterial = new THREE . MeshLambertMaterial ( {
805+ map : texture ,
806+ side : 0
807+ } )
819808
809+ world = new Terrain ( {
810+ textureMaterial : worldMaterial ,
811+ textureRows : 27 ,
812+ textureCols : 27 ,
813+ cellSize : 16 ,
814+ } )
815+ $ . get ( `assets/blocks.json?${ uuidv4 ( ) } ` , function ( blocks ) {
816+ world . blocks = blocks
817+ loadBlocksModels ( )
818+ } )
819+ }
820820
821821
822822 //animated Texture Atlas
823823 {
824824 var textureAtlasX = new Image ;
825+
825826 textureAtlasX . onload = function ( ) {
826827 $ . get ( "assets/textureMappingX.json" , function ( textureMappingX ) {
827828 var atlasCreator = new TextureAtlasCreator ( {
828829 textureX :textureAtlasX ,
829830 textureMapping :textureMappingX
830831 } )
831-
832+ var savedTextures = [ ]
833+ for ( var i = 0 ; i < 500 ; i ++ ) {
834+ var t = atlasCreator . gen ( i ) . toDataURL ( ) ;
835+ var tekstura = new THREE . TextureLoader ( ) . load ( t , function ( textu ) {
836+
837+
838+ // worldMaterial.map=tekstura
839+
840+ // worldMaterial.map.needsUpdate=true;
841+ } ) ;
842+ tekstura . magFilter = THREE . NearestFilter ;
843+ savedTextures . push ( tekstura )
844+ }
832845 var q ;
833846 var tickq = 0 ;
847+
834848 setInterval ( function ( ) {
835849 try {
836850 q . remove ( )
837851 } catch ( e ) { }
838- q = atlasCreator . gen ( tickq )
852+ // q=atlasCreator.gen(tickq)
839853 tickq ++ ;
840- var tekstura = new THREE . TextureLoader ( ) . load ( q . toDataURL ( ) , function ( text ) {
841- tekstura . magFilter = THREE . NearestFilter ;
842- worldMaterial . map = tekstura
854+ var tekst = savedTextures [ tickq % 500 ] ;
855+ worldMaterial . map = tekst
843856
844- // worldMaterial.map.needsUpdate=true;
845- } ) ;
857+ worldMaterial . map . needsUpdate = true ;
858+ // var tekstura=new THREE.TextureLoader().load(q.toDataURL(),function (text){
859+ // // tekstura.magFilter = THREE.NearestFilter;
860+ // // worldMaterial.map=tekstura
861+
862+ // // worldMaterial.map.needsUpdate=true;
863+ // });
846864
847865 // world.textureAtlas=new THREE.TextureLoader().load(q.toDataURL());
848866 // world.textureAtlas.magFilter = THREE.NearestFilter;
849867 // $("body").append(q)
850- } , 150 )
868+ } , 100 )
851869
852870 } )
853871 }
@@ -896,6 +914,7 @@ <h3>Gra zatrzymana</h3>
896914 } )
897915 }
898916
917+
899918 //Inventory Bar
900919 {
901920 var inv_bar = new InventoryBar ( {
@@ -908,7 +927,7 @@ <h3>Gra zatrzymana</h3>
908927 inv_bar . setBox ( 1 , "assets/images/grass_block.png" )
909928 inv_bar . setBox ( 2 , "assets/images/stone.png" )
910929 inv_bar . setBox ( 3 , "assets/images/oak_planks.png" )
911- inv_bar . setBox ( 4 , "https://vignette.wikia.nocookie.net/minecraftpocketedition/ images/a/a5/Fire .gif/revision/latest?cb=20160210064250 " )
930+ inv_bar . setBox ( 4 , "assets/ images/smoker .gif" )
912931 inv_bar . setBox ( 5 , "assets/images/anvil.png" )
913932 inv_bar . setBox ( 6 , "assets/images/brick.png" )
914933 inv_bar . setBox ( 7 , "assets/images/furnace.png" )
@@ -924,6 +943,7 @@ <h3>Gra zatrzymana</h3>
924943 } )
925944 }
926945
946+
927947 //First Person Controls
928948 {
929949 FPC = new FirstPersonControls ( {
@@ -967,21 +987,21 @@ <h3>Gra zatrzymana</h3>
967987 }
968988
969989
970-
971-
972-
973- var geometry = new THREE . BoxGeometry ( 1 , 1 , 1 ) ;
974- var material = new THREE . MeshBasicMaterial ( {
975- color : 0x00ff00
976- } ) ;
977- var edges = new THREE . EdgesGeometry ( geometry ) ;
978- cube = new THREE . LineSegments ( edges , new THREE . LineBasicMaterial ( {
979- color : 0x000000 ,
980- linewidth : 0.5
981- } ) ) ;
982- scene . add ( cube ) ;
983-
990+ //Raycast cube
991+ {
992+ var geometry = new THREE . BoxGeometry ( 1 , 1 , 1 ) ;
993+ var material = new THREE . MeshBasicMaterial ( {
994+ color : 0x00ff00
995+ } ) ;
996+ var edges = new THREE . EdgesGeometry ( geometry ) ;
997+ cube = new THREE . LineSegments ( edges , new THREE . LineBasicMaterial ( {
998+ color : 0x000000 ,
999+ linewidth : 0.5
1000+ } ) ) ;
1001+ scene . add ( cube ) ;
1002+ }
9841003
1004+
9851005 $ ( document ) . mousedown ( function ( e ) {
9861006 if ( gameState == "game" ) {
9871007 const start = new THREE . Vector3 ( ) . setFromMatrixPosition ( camera . matrixWorld ) ;
@@ -1023,7 +1043,18 @@ <h3>Gra zatrzymana</h3>
10231043 var block = modelsToLoad [ i ] ;
10241044 loader . load ( `assets/models/${ block . model } ` , function ( object ) {
10251045 var geometry = object . children [ 0 ] . geometry ;
1026- world . saveModel ( prepareModel ( geometry , block . name ) , block . name )
1046+ if ( block . name == "anvil" ) {
1047+ geometry . rotateX ( - Math . PI / 2 )
1048+ geometry . translate ( 0 , 0.17 , 0 )
1049+ geometry . translate ( 0 , - 0.25 , 0 )
1050+ }
1051+ if ( block . name == "fire" ) {
1052+ geometry . rotateX ( - Math . PI / 2 )
1053+ geometry . scale ( 0.5 , 0.5 , 0.5 )
1054+ // geometry.translate(0,0.25,0)
1055+ // geometry.translate(0,-0.25,0)
1056+ }
1057+ world . saveModel ( geometry , block . name )
10271058 modelsLoaded ++ ;
10281059 if ( modelsLoaded == modelsNumber ) {
10291060 // console.log("DONE! Models loaded!")
@@ -1033,26 +1064,10 @@ <h3>Gra zatrzymana</h3>
10331064 }
10341065 } ) ;
10351066 } ) ( ) ;
1036-
1037- }
1038- }
1039- function prepareModel ( geometry , name ) {
1040- if ( name == "anvil" ) {
1041- geometry . rotateX ( - Math . PI / 2 )
1042- geometry . translate ( 0 , 0.17 , 0 )
1043- geometry . translate ( 0 , - 0.25 , 0 )
1044- }
1045- if ( name == "fire" ) {
1046- geometry . rotateX ( - Math . PI / 2 )
1047- geometry . scale ( 0.5 , 0.5 , 0.5 )
1048- // geometry.translate(0,0.25,0)
1049- // geometry.translate(0,-0.25,0)
10501067 }
1051- return geometry
10521068}
10531069
10541070
1055-
10561071function uuidv4 ( ) {
10571072 return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
10581073 var r = Math . random ( ) * 16 | 0 , v = c == 'x' ? r : ( r & 0x3 | 0x8 ) ;
@@ -1061,42 +1076,13 @@ <h3>Gra zatrzymana</h3>
10611076}
10621077
10631078
1064- function updateCursor ( ) {
1065- const start = new THREE . Vector3 ( ) . setFromMatrixPosition ( camera . matrixWorld ) ;
1066- const end = new THREE . Vector3 ( ) . set ( 0 , 0 , 1 ) . unproject ( camera ) ;
1067- const intersection = world . intersectsRay ( start , end ) ;
1068- if ( intersection ) {
1069- const pos = intersection . position . map ( ( v , ndx ) => {
1070- return v + intersection . normal [ ndx ] * - 0.5 ;
1071- } ) ;
1072- pos [ 0 ] = Math . floor ( pos [ 0 ] )
1073- pos [ 1 ] = Math . floor ( pos [ 1 ] )
1074- pos [ 2 ] = Math . floor ( pos [ 2 ] )
1075- cube . position . set ( ...pos )
1076- // console.log(pos)
1077- cube . visible = true ;
1078- } else {
1079- cube . visible = false ;
1080- }
1081- }
1082-
10831079function animate ( ) {
10841080 stats . begin ( ) ;
10851081 render ( )
10861082 stats . end ( ) ;
10871083 requestAnimationFrame ( animate )
10881084}
10891085
1090- function resizeRendererToDisplaySize ( renderer ) {
1091- const canvas = renderer . domElement ;
1092- const width = canvas . clientWidth ;
1093- const height = canvas . clientHeight ;
1094- const needResize = canvas . width !== width || canvas . height !== height ;
1095- if ( needResize ) {
1096- renderer . setSize ( width , height , false ) ;
1097- }
1098- return needResize ;
1099- }
11001086
11011087function render ( ) {
11021088 var time = Date . now ( ) * 0.00005 ;
@@ -1113,11 +1099,17 @@ <h3>Gra zatrzymana</h3>
11131099 }
11141100
11151101
1116- if ( resizeRendererToDisplaySize ( renderer ) ) {
1102+ const canvas = renderer . domElement ;
1103+ const width = canvas . clientWidth ;
1104+ const height = canvas . clientHeight ;
1105+ const needResize = canvas . width !== width || canvas . height !== height ;
1106+ if ( needResize ) {
1107+ renderer . setSize ( width , height , false ) ;
11171108 const canvas = renderer . domElement ;
11181109 camera . aspect = canvas . clientWidth / canvas . clientHeight ;
11191110 camera . updateProjectionMatrix ( ) ;
11201111 }
1112+
11211113 if ( gameState == "game" ) {
11221114 socket . emit ( "playerUpdate" , {
11231115 x :camera . position . x ,
@@ -1131,7 +1123,24 @@ <h3>Gra zatrzymana</h3>
11311123 }
11321124 renderer . render ( scene , camera ) ;
11331125 world . updateCells ( )
1134- updateCursor ( )
1126+
1127+
1128+ const start = new THREE . Vector3 ( ) . setFromMatrixPosition ( camera . matrixWorld ) ;
1129+ const end = new THREE . Vector3 ( ) . set ( 0 , 0 , 1 ) . unproject ( camera ) ;
1130+ const intersection = world . intersectsRay ( start , end ) ;
1131+ if ( intersection ) {
1132+ const pos = intersection . position . map ( ( v , ndx ) => {
1133+ return v + intersection . normal [ ndx ] * - 0.5 ;
1134+ } ) ;
1135+ pos [ 0 ] = Math . floor ( pos [ 0 ] )
1136+ pos [ 1 ] = Math . floor ( pos [ 1 ] )
1137+ pos [ 2 ] = Math . floor ( pos [ 2 ] )
1138+ cube . position . set ( ...pos )
1139+ // console.log(pos)
1140+ cube . visible = true ;
1141+ } else {
1142+ cube . visible = false ;
1143+ }
11351144
11361145
11371146}
0 commit comments