Skip to content

Commit

Permalink
performance enhancements
Browse files Browse the repository at this point in the history
use shared materials for every mesh
only bundle one shader: "Legacy Shaders/Diffuse"
  • Loading branch information
MichaelBuhler committed Jun 18, 2020
1 parent d9de03f commit 7f68f72
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
12 changes: 9 additions & 3 deletions Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,23 @@ private void InitGround()
ground.transform.localScale *= 5;
ground.transform.localPosition = new Vector3(24.5f, -1, 24.5f);
GameObject.Destroy(ground.GetComponent<Collider>());
ground.GetComponent<Renderer>().receiveShadows = false;
Renderer renderer = ground.GetComponent<Renderer>();
renderer.receiveShadows = false;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.sharedMaterial = Materials.sugarscape;
}

private void InitCapsules()
{
for (int i = 0; i < 100; i++) {
for (int i = 0; i < 400; i++) {
GameObject agent = GameObject.CreatePrimitive(PrimitiveType.Capsule);
agent.name = "Agent" + i;
agent.transform.localPosition = new Vector3(Utils.RandomInt(50), 0, Utils.RandomInt(50));
GameObject.Destroy(agent.GetComponent<Collider>());
agent.GetComponent<Renderer>().material.color = Random.value < 0.5 ? Color.red : Color.blue;
Renderer renderer = agent.GetComponent<Renderer>();
renderer.receiveShadows = false;
renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off;
renderer.sharedMaterial = Random.value < 0.5 ? Materials.male : Materials.female;
}
}
}
24 changes: 24 additions & 0 deletions Materials.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

using UnityEngine;

public class Materials {

public static readonly Material sugarscape;

public static readonly Material male;
public static readonly Material female;

static Materials () {
sugarscape = new Material(Shaders.Legacy.diffuse) {
color = Color.white
};

male = new Material(Shaders.Legacy.diffuse) {
color = Color.red
};
female = new Material(Shaders.Legacy.diffuse) {
color = Color.blue
};
}

}
16 changes: 16 additions & 0 deletions Shaders.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

using UnityEngine;

public class Shaders {

public static class Legacy {

public static readonly Shader diffuse;

static Legacy () {
diffuse = Shader.Find("Legacy Shaders/Diffuse");
}

}

}
2 changes: 1 addition & 1 deletion scene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ Camera:
m_Enabled: 1
serializedVersion: 2
m_ClearFlags: 1
m_BackGroundColor: {r: 0.19215687, g: 0.3019608, b: 0.4745098, a: 0}
m_BackGroundColor: {r: 0, g: 0, b: 0, a: 0}
m_projectionMatrixMode: 1
m_GateFitMode: 2
m_FOVAxisMode: 0
Expand Down

0 comments on commit 7f68f72

Please sign in to comment.