Inheritance: UnityEngine.MonoBehaviour
Exemplo n.º 1
0
        private void SubscribeToBodyEvents(GravitationalBody body)
        {
            UnsubscribeFromBodyEvents(body);

            body.OnDestroyed += RemoveBody;
            body.OnAltered   += OnBodyAltered;
        }
Exemplo n.º 2
0
        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);
                }
            }
        }
Exemplo n.º 3
0
        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);
                }
            }
        }
Exemplo n.º 4
0
 private void UnsubscribeFromBodyEvents(GravitationalBody body)
 {
     body.OnAltered   -= OnBodyAltered;
     body.OnDestroyed -= RemoveBody;
 }
Exemplo n.º 5
0
 private void OnBodyAltered(GravitationalBody body)
 {
     UpdateBuffer(true);
 }
Exemplo n.º 6
0
 public void RemoveBody(GravitationalBody body)
 {
     UnsubscribeFromBodyEvents(body);
     bodies.Remove(body);
     updateBuffer = true;
 }