예제 #1
0
    protected void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            Dispose();
            InitParticles();
            SetColliders();
        }
        ComputeDensityPressureJob computeDensityPressure = new ComputeDensityPressureJob()
        {
            poly6          = settings.POLY6(),
            gazConst       = settings.gasConstant,
            mass           = settings.mass,
            radiusSq       = settings.particleRadius * settings.particleRadius,
            restDensity    = settings.restDensity,
            waterParticles = waterParticles
        };

        computeDensityPressure.Run(waterParticles.Length);

        ComputeForceJob computeForce = new ComputeForceJob()
        {
            spikyGrad       = settings.CalculateSpikyGrad(),
            viscosityKernel = settings.CalculateViscosityKernel(),
            gravity         = settings.gravity,
            mass            = settings.mass,
            radius          = settings.particleRadius,
            radiusSq        = settings.particleRadius * settings.particleRadius,
            viscosity       = settings.viscosity,
            waterParticles  = waterParticles
        };

        computeForce.Run(waterParticles.Length);

        IntegrateJob integrate = new IntegrateJob()
        {
            deltaTime           = Time.deltaTime,
            boxes               = boxes,
            spheres             = spheres,
            innerBoxes          = innerBoxes,
            collisionElasticity = settings.collisionElasticity,
            waterParticles      = waterParticles,
            particleSize        = settings.particleRadius,
            boxSize             = boxSize
        };

        integrate.Run(waterParticles.Length);

        waterRenderer.RenderWater(waterParticles);
        //CalculateStats();
    }
예제 #2
0
    private void CalculateFluidBehavior(float2 gravity)
    {
        NativeMultiHashMap <int, int> hashMap           = new NativeMultiHashMap <int, int>(particles.Length, Allocator.TempJob);
        NativeArray <int2>            cellOffset        = new NativeArray <int2>(GridHashUtilities.cell2DOffsets, Allocator.TempJob);
        NativeArray <WaterParticle2D> readOnlyParticles = new NativeArray <WaterParticle2D>(particles, Allocator.TempJob);

        CalculateConst();

        var handle = new HashPositionJob()
        {
            hashMap   = hashMap.AsParallelWriter(),
            particles = particles,
            radius    = particleRadius
        }.Schedule(particles.Length, 1);

        handle = new ComputeDensityPressureJob()
        {
            particles         = particles,
            readOnlyParticles = readOnlyParticles,

            particleRadius        = particleRadius,
            particleRadiusSquared = particleRadius * particleRadius,
            hashMap    = hashMap,
            cellOffset = cellOffset,

            //Consts
            GAS_CONST = GAS_CONST,
            MASS      = MASS,
            POLY6     = POLY6,
            REST_DENS = REST_DENS
        }.Schedule(particles.Length, 1, handle);

        handle = new CopyReadOnlyParticleJob()
        {
            particles         = particles,
            readOnlyParticles = readOnlyParticles,
        }.Schedule(particles.Length, 1, handle);

        handle = new ComputeForceJob()
        {
            hashMap           = hashMap,
            cellOffset        = cellOffset,
            particles         = particles,
            readOnlyParticles = readOnlyParticles,

            particleRadius        = particleRadius,
            particleRadiusSquared = particleRadius * particleRadius,

            //consts
            GRAVITY    = gravity,
            MASS       = MASS,
            SPIKY_GRAD = SPIKY_GRAD,
            VISC       = VISC,
            VISC_LAP   = VISC_LAP
        }.Schedule(particles.Length, 1, handle);

        handle = new IntegrateJob()
        {
            particles      = particles,
            particleRadius = particleRadius,
            deltaTime      = Time.fixedDeltaTime / iterationPerFrame,

            collisionScale = collisionScale,
            colArray       = collisionArray,

            //Consts
            BOUND_DAMPING = BOUND_DAMPING,
            VIEW_HEIGHT   = VIEW_HEIGHT,
            VIEW_WIDTH    = VIEW_WIDTH,
        }.Schedule(particles.Length, 1, handle);

        handle.Complete();

        hashMap.Dispose();
        cellOffset.Dispose();
        readOnlyParticles.Dispose();
    }