Beispiel #1
0
        /*
         * Unity events
         */

        void Awake()
        {
            var sim = monoSim.sim;

            _simRenderer = GetComponent <SimRenderer>();
            _translation = new TranslationInteraction(sim.GetParticleAt(0), new Vector2(0f, 0f));
            // sim.AddInteraction(_translation);
        }
Beispiel #2
0
 public void SetRenderingGroupToSim(SimRenderer renderer)
 {
     for (var i = 0; i < renderingGroups.Count; ++i)
     {
         var group = renderingGroups[i];
         renderer.AddRenderingGroup(group.groupID, group.indices);
     }
 }
Beispiel #3
0
    void Start()
    {
        // populate our array of particles from the samples given, set their initial state
        List <float2> temp_positions = new List <float2>();
        const float   spacing = 0.5f;
        const int     box_x = 32, box_y = 32;
        const float   sx = grid_res / 2.0f, sy = grid_res / 2.0f;

        for (float i = sx - box_x / 2; i < sx + box_x / 2; i += spacing)
        {
            for (float j = sy - box_y / 2; j < sy + box_y / 2; j += spacing)
            {
                var pos = math.float2(i, j);

                temp_positions.Add(pos);
            }
        }

        // round number of particles down to the nearest power of 2. taken from
        int po2_amnt = 1; while (po2_amnt <= temp_positions.Count)

        {
            po2_amnt <<= 1;
        }

        num_particles = po2_amnt >> 1;

        ps = new NativeArray <Particle>(num_particles, Allocator.Persistent);

        for (int i = 0; i < num_particles; ++i)
        {
            Particle p = new Particle();
            p.x    = temp_positions[i];
            p.v    = 0;
            p.C    = 0;
            p.mass = 1.0f;
            ps[i]  = p;
        }

        grid = new NativeArray <Cell>(num_cells, Allocator.Persistent);

        for (int i = 0; i < num_cells; ++i)
        {
            var cell = new Cell();
            cell.v  = 0;
            grid[i] = cell;
        }

        sim_renderer = GameObject.FindObjectOfType <SimRenderer>();
        sim_renderer.Initialise(num_particles, Marshal.SizeOf(new Particle()));
    }
Beispiel #4
0
 void Start()
 {
     particle_mass *= (spacing * spacing / 0.25f);//(spacing < 0.5f)?(spacing * spacing / 0.25f) : (spacing / 0.5f);
     prefab_list    = usePrefab ? new List <Transform>() : null;
     Initialize();
     if (usePrefab)
     {
         particle_array = new TransformAccessArray(prefab_list.ToArray());
     }
     else
     {
         sim_renderer = GameObject.FindObjectOfType <SimRenderer>();
         sim_renderer.Initialise(num_particle, Marshal.SizeOf(new Particle()));
     }
 }
    void Start()
    {
        // initialising a bunch of points in a square
        List <float2> temp_positions = new List <float2>();
        const float   spacing = 1.0f;
        const int     box_x = 16, box_y = 16;
        const float   sx = grid_res / 2.0f, sy = grid_res / 2.0f;

        for (float i = sx - box_x / 2; i < sx + box_x / 2; i += spacing)
        {
            for (float j = sy - box_y / 2; j < sy + box_y / 2; j += spacing)
            {
                var pos = math.float2(i, j);
                temp_positions.Add(pos);
            }
        }
        num_particles = temp_positions.Count;

        ps = new NativeArray <Particle>(num_particles, Allocator.Persistent);

        // populate our array of particles, set their initial state
        for (int i = 0; i < num_particles; ++i)
        {
            Particle p = new Particle();
            p.x = temp_positions[i];
            // random initial velocity
            p.v    = math.float2(Random.value - 0.5f, Random.value - 0.5f + 2.75f) * 0.5f;
            p.C    = 0;
            p.mass = 1.0f;
            ps[i]  = p;
        }

        grid = new NativeArray <Cell>(num_cells, Allocator.Persistent);

        for (int i = 0; i < num_cells; ++i)
        {
            grid[i] = new Cell();
        }

        sim_renderer = GameObject.FindObjectOfType <SimRenderer>();
        sim_renderer.Initialise(num_particles, Marshal.SizeOf(new Particle()));
    }
Beispiel #6
0
        public UISim()
        {
            SimRender    = new SimRenderer();
            SimRender.ID = "SimRender";

            SimScene        = new ThreeDScene();
            SimScene.ID     = "SimScene";
            SimScene.Camera = new Camera(Vector3.Backward * ViewScale, Vector3.Zero, Vector3.Right);
            SimScene.Add(SimRender);
            //GameFacade.Scenes.AddScene(SimScene);


            /** Default settings **/
            SimRender.Scale     = new Vector3(0.6f);
            SimRender.RotationY = (float)MathUtils.DegreeToRadian(25);
            SimRender.RotationX = (float)MathUtils.DegreeToRadian(RotationStartAngle);
            //
            //var scene = new TSOClient.ThreeD.ThreeDScene();
            //scene.Add(a);
            GameFacade.Scenes.AddExternalScene(SimScene);
        }
Beispiel #7
0
 void Awake()
 {
     _monoSim     = GetComponent <MonoSimulator>();
     _simRenderer = GetComponent <SimRenderer>();
 }
Beispiel #8
0
 void Start()
 {
     Initialize();
     sim = GameObject.FindObjectOfType <SimRenderer>();
     sim.Initialise(num_particle, Marshal.SizeOf(new Particle()));
 }
Beispiel #9
0
    void Start()
    {
        // populate our array of particles
        temp_positions = new List <float2>();
        spawn_box(grid_res / 2, grid_res / 2, 32, 32);
        num_particles = temp_positions.Count;

        ps = new NativeArray <Particle>(num_particles, Allocator.Persistent);
        Fs = new NativeArray <float2x2>(num_particles, Allocator.Persistent);

        // initialise particles
        for (int i = 0; i < num_particles; ++i)
        {
            Particle p = new Particle();
            p.x    = temp_positions[i];
            p.v    = 0;
            p.C    = 0;
            p.mass = 1.0f;
            ps[i]  = p;

            // deformation gradient initialised to the identity
            Fs[i] = math.float2x2(
                1, 0,
                0, 1
                );
        }

        grid = new NativeArray <Cell>(num_cells, Allocator.Persistent);

        for (int i = 0; i < num_cells; ++i)
        {
            var cell = new Cell();
            cell.v  = 0;
            grid[i] = cell;
        }

        // ---- begin precomputation of particle volumes
        // MPM course, equation 152

        // launch a P2G job to scatter particle mass to the grid
        new Job_P2G()
        {
            ps            = ps,
            Fs            = Fs,
            grid          = grid,
            num_particles = num_particles
        }.Schedule().Complete();

        for (int i = 0; i < num_particles; ++i)
        {
            var p = ps[i];

            // quadratic interpolation weights
            float2 cell_idx  = math.floor(p.x);
            float2 cell_diff = (p.x - cell_idx) - 0.5f;
            weights[0] = 0.5f * math.pow(0.5f - cell_diff, 2);
            weights[1] = 0.75f - math.pow(cell_diff, 2);
            weights[2] = 0.5f * math.pow(0.5f + cell_diff, 2);

            float density = 0.0f;
            // iterate over neighbouring 3x3 cells
            for (int gx = 0; gx < 3; ++gx)
            {
                for (int gy = 0; gy < 3; ++gy)
                {
                    float weight = weights[gx].x * weights[gy].y;

                    // map 2D to 1D index in grid
                    int cell_index = ((int)cell_idx.x + (gx - 1)) * grid_res + ((int)cell_idx.y + gy - 1);
                    density += grid[cell_index].mass * weight;
                }
            }

            // per-particle volume estimate has now been computed
            float volume = p.mass / density;
            p.volume_0 = volume;

            ps[i] = p;
        }

        // ---- end precomputation of particle volumes

        // boilerplate rendering code handled elsewhere
        sim_renderer = GameObject.FindObjectOfType <SimRenderer>();
        sim_renderer.Initialise(num_particles, Marshal.SizeOf(new Particle()));
    }