How to improve the performance of this code?

This is my code:

v3.zip (4.2 MB)

When i try to increase the chunk size or the render distance or the depth, the scenary bugs or goes slowly. Any solution? Thanks anyway.

I ran your code through the profiler:

Almost ALL your frametime is in raycasting. I think if you optimize that it will run much faster.

Also you might consider pull all your code out of the html into a .js file..
Not for optimization but just organization.

2 Likes

Disabling raycast works a little, it gives the double of fps with performancing.
Im now trying to make the *.js file.
More suggerences?

Edit: I tried to do 2 files: main.js which is the main script and 3dminecraft.html, which is the main file. You have them here:

v4.zip (4.2 MB)

And didnt gave to me much performance.

1 Like

I went from 20 to 60fps with:

		var renderDistance = 8;
		var chunkSize = 10;

by disabling all the raycasts…

Yeah putting it all in main.js is good.

1 Like

image

There are my FPS with that you mentioned:

		var renderDistance = 8;
		var chunkSize = 10;

var renderDistance = 13;
var chunkSize = 10;|

(gf1080gtx)

@60fps

(i had to fix a bug in your Math.min/max.apply )
**


**

Wich bug? Im seeing this:

		function lowestXBlock(){
			var xPosArray = [];
			for(var i = 0; i < chunks.length; i++){
				for(var j = 0; j < chunks[i].length; j++){
					xPosArray.push(chunks[i][j].x);
				}
			}
			return Math.min.apply(null, xPosArray);
		}

		function highestXBlock(){
			var xPosArray = [];
			for(var i = 0; i < chunks.length; i++){
				for(var j = 0; j < chunks[i].length; j++){
					xPosArray.push(chunks[i][j].x);
				}
			}
			return Math.max.apply(null, xPosArray);
		}

		function lowestZBlock(){
			var zPosArray = [];
			for(var i = 0; i < chunks.length; i++){
				for(var j = 0; j < chunks[i].length; j++){
					zPosArray.push(chunks[i][j].z);
				}
			}
			return Math.min.apply(null, zPosArray);
		}

		function highestZBlock(){
			var zPosArray = [];
			for(var i = 0; i < chunks.length; i++){
				for(var j = 0; j < chunks[i].length; j++){
					zPosArray.push(chunks[i][j].z);
				}
			}
			return Math.max.apply(null, zPosArray);
		}

If i increased the chunkSize i was getting a stack overflow in those methods…

I fixed tho.. i’ll send u a zip.

As far as the raycasting goes.. I think you’re gonna need something more clever that searches your block structure directly… because raycasting against the cube instanceMeshes is kinda slow.

But chunkSize isnt defined on that methods…

How could i change it?

It is. there is a constant for it i think?
idk.. here’s the fixed up version .. (but raycast is still disabled)

v3-thrax.zip (4.2 MB)

image

Edit: With your version i cant place or destroy blocks.

Yeah because I disabled the raycasting. There is a flag at the top of main to turn it back on again. set disableRaycast = false