private void SubscribeToBodyEvents(GravitationalBody body) { UnsubscribeFromBodyEvents(body); body.OnDestroyed += RemoveBody; body.OnAltered += OnBodyAltered; }
private void OnRenderObject() { for (int i = 0; i < bodies.Count; ++i) { if (bodies[i].Render) { GravitationalBody body = bodies[i]; material.SetPass(0); material.SetInt("index", i); material.SetFloat("scale", body.transform.localScale.y); material.SetColor("color", body.Color); material.SetBuffer("body_buffer", buffer); Graphics.DrawMeshNow(mesh, Matrix4x4.identity); } } }
private void UpdateBuffer(bool forceUpdate = false) { if (updateBuffer || forceUpdate) { updateBuffer = false; if (bodies.Count > 0) { ValidateComputeBuffer(bodies.Count, GravitationalBody.Data.Size, ref buffer); if (data == null || data.Length != bodies.Count) { data = new GravitationalBody.Data[bodies.Count]; } for (int i = 0; i < bodies.Count; ++i) { GravitationalBody body = bodies[i]; Vector3 position = body.transform.position; if (position.x % 1 == 0 && position.y % 1 == 0 && position.z % 1 == 0) { position[Random.Range(0, 3)] += 0.0001f; } data[i].position = position; data[i].position.w = 1; data[i].velocity = body.transform.forward * body.InitialSpeed; data[i].mass = body.Mass; } buffer.SetData(data); } } }
private void UnsubscribeFromBodyEvents(GravitationalBody body) { body.OnAltered -= OnBodyAltered; body.OnDestroyed -= RemoveBody; }
private void OnBodyAltered(GravitationalBody body) { UpdateBuffer(true); }
public void RemoveBody(GravitationalBody body) { UnsubscribeFromBodyEvents(body); bodies.Remove(body); updateBuffer = true; }