Increase FPS in a block based game
I'm building a game that, at its core, works a bit like Minecraft. Blocks are generated in a terrain that can then be broken to get materials. The similarities pretty much end there, but that's beside the point. For some reason, the FPS on the game is terrible! I added a system that disables the Mesh Renderers after they are a certain distance from the player. With a render distance of 5 units (5 in-game blocks), I was only getting about 20 FPS on my MacBook (Radeon Pro 460 4 GB, 2.7 GHz Intel Core i7) I've changed all shadows to low resolution, and all materials to use the Mobile Diffuse shader (I was told that mobile shaders are less intensive). All of the textures are 1024 X 1024, but after switching them to 1/8 that size, the FPS didn't change. Someone also recommended that I use occlusion culling, but this won't work since the blocks in-game will be broken and placed by the player. After seeing how Minecraft can render millions of blocks without lag, I just don't understand why I can't even render a 5X5X5 area without lag.
Side notes: 1. Almost all scripts on the blocks are removed after generation, so I doubt that is a problem. 2. The "Mesh Render Disable" script uses the Invoke command to run the 'distance to player' check at random intervals. This helps it so the game doesn't have to calculate the distance to the player from all the blocks all at once. (See code below) 3. I tried running the game at lower resolutions, which helped, but not by much. 4. All blocks have a RigidBody, but it is marked as being kinematic. 5. I am aware that Minecraft renders terrain with a chunk based system, but even that is 16X16X16 blocks, which my game struggles with. 6. The max terrain size is 25X25X25 blocks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeRenderer : MonoBehaviour {
private GameObject Player;
public int RenderDistanceVariable;
// Use this for initialization
void Start () {
Player = GameObject.Find("FPSController");
Invoke("RenderDistanceValueUpdate", 0F);
Invoke("RenderUpdate", 0F);
}
// Update is called once per frame
void RenderUpdate () {
if (Vector3.Distance(Player.transform.position, gameObject.transform.position) >= RenderDistanceVariable) {
gameObject.GetComponent<Renderer>().enabled = false;
} else {
gameObject.GetComponent<Renderer>().enabled = true;
}
Invoke("RenderUpdate", Random.Range(0F, 3.0F));
}
void RenderDistanceValueUpdate () {
Player = GameObject.Find("FPSController");
RenderDistanceVariable = PlayerPrefs.GetInt("RenderDistance");
Invoke("RenderDistanceValueUpdate", 1.0F);
}
}