- Home /
Question by
noahg1 · Mar 04, 2015 at 07:29 PM ·
materialoptimizationalpha
Optimize code changing transparency of large number of Objects
I have a wall made up of a about two thousand cubes. Each cube has a material that changes transparency with time. I change the alpha value of each material based on a randomized sine function. The problem is that when the player gets far away from the wall, the frame rate tanks. How can I optimize the code below to improve my FPS?
// Use this for initialization
void Start () {
rend = GetComponent<Renderer>();
if (type == WallType.blue)
rend.material = blue;
else if (type == WallType.red)
rend.material = red;
color = rend.material.color;
height = Random.Range(.1f, 1f);
stretch = Random.Range(1f, 3f);
offset = Random.Range(.1f, 6.28f);
origZ = transform.localPosition.z;
}
// Update is called once per frame
void Update () {
alpha = height * Mathf.Sin(stretch * (Time.time + offset));
if (alpha > .1)
color.a = alpha;
rend.material.color = color;
}
Comment
Your answer
Follow this Question
Related Questions
Sharing materials in unity 1 Answer
Sprite Alpha Performance 0 Answers
Is it possible to calculate batch count of each material ? 0 Answers
shader FX/water and diffuse texture alpha blend 1 Answer
Set alpha cutoff value Unity3d 1 Answer