Beispiel #1
0
    RenderTexture Render(RenderTexture target)
    {
        if (target == null)
        {
            return(null);
        }

        if (bloom_enabled)
        {
            ApplyBloom(density_dfbo.GetReadFBO(), bloom_fbo);
        }
        blendOption = FluidBlendOption.BlendOption.Enable_One_OMSA;

        int width  = target.width;
        int height = target.height;

        if (transparent_enable)
        {
            backgroundProgram.SetFloat("_aspectRatio", (float)width / (float)height);
            backgroundProgram.Blit(target, target, blendOption);
        }
        else
        {
            colorProgram.SetColor("_color", BACK_COLOR);
            colorProgram.Blit(target, target, blendOption);
        }
        if (shading_enable)
        {
            FluidProgram program = bloom_enabled ? displayBloomShadingProgram : displayShadingProgram;
            program.SetFloat("_texelSize_x", 1.0f / simWidth);
            program.SetFloat("_texelSize_y", 1.0f / simHeight);
            program.SetTexture("_uTexture", density_dfbo.GetReadFBO());
            if (bloom_enabled)
            {
                program.SetTexture("_Bloom", bloom_fbo);
                program.SetTexture("_Dithering", ditheringTexture);
                Vector2 scale = GetTextureScale(ditheringTexture, width, height);
                program.SetFloat("_ditherScale_x", scale.x);
                program.SetFloat("_ditherScale_y", scale.y);
            }
            program.Blit(target, target, blendOption);
        }
        else
        {
            FluidProgram program = bloom_enabled ? displayBloomProgram : displayProgram;
            program.SetTexture("_uTexture", density_dfbo.GetReadFBO());
            if (bloom_enabled)
            {
                program.SetTexture("_Bloom", bloom_fbo);
                program.SetTexture("_Dithering", ditheringTexture);
                Vector2 scale = GetTextureScale(ditheringTexture, width, height);
                program.SetFloat("_ditherScale_x", scale.x);
                program.SetFloat("_ditherScale_y", scale.y);
            }
            program.Blit(target, target, blendOption);
        }
        return(target);
    }
    void Step(float dt)
    {
        blendOption = FluidBlendOption.BlendOption.Disable;

        curlProgram.SetFloat("_texelSize_x", 1.0f / simWidth);
        curlProgram.SetFloat("_texelSize_y", 1.0f / simHeight);
        curlProgram.SetTexture("_Velocity", velocity_dfbo.GetReadFBO());
        curlProgram.Blit(curl_fbo, curl_fbo, blendOption);

        vorticityProgram.SetFloat("_texelSize_x", 1.0f / simWidth);
        vorticityProgram.SetFloat("_texelSize_y", 1.0f / simHeight);
        vorticityProgram.SetTexture("_Velocity", velocity_dfbo.GetReadFBO());
        vorticityProgram.SetTexture("_Curl", curl_fbo);
        vorticityProgram.SetFloat("_curl", CURL);
        vorticityProgram.SetFloat("_dt", dt);
        vorticityProgram.Blit(velocity_dfbo.GetWriteFBO(), velocity_dfbo.GetWriteFBO(), blendOption);
        velocity_dfbo.Swap();

        divergenceProgram.SetFloat("_texelSize_x", 1.0f / simWidth);
        divergenceProgram.SetFloat("_texelSize_y", 1.0f / simHeight);
        divergenceProgram.SetTexture("_Velocity", velocity_dfbo.GetReadFBO());
        divergenceProgram.Blit(divergence_fbo, divergence_fbo, blendOption);

        clearProgram.SetTexture("_uTexture", pressure_dfbo.GetReadFBO());
        clearProgram.SetFloat("_value", PRESSURE_DISSIPATION);
        clearProgram.Blit(pressure_dfbo.GetWriteFBO(), pressure_dfbo.GetWriteFBO(), blendOption);
        pressure_dfbo.Swap();

        pressureProgram.SetFloat("_texelSize_x", 1.0f / simWidth);
        pressureProgram.SetFloat("_texelSize_y", 1.0f / simHeight);
        pressureProgram.SetTexture("_Divergence", divergence_fbo);
        for (int i = 0; i < PRESSURE_ITERATIONS; i++)
        {
            pressureProgram.SetTexture("_Pressure", pressure_dfbo.GetReadFBO());
            pressureProgram.Blit(pressure_dfbo.GetWriteFBO(), pressure_dfbo.GetWriteFBO(), blendOption);
            pressure_dfbo.Swap();
        }

        gradienSubtractProgram.SetFloat("_texelSize_x", 1.0f / simWidth);
        gradienSubtractProgram.SetFloat("_texelSize_y", 1.0f / simHeight);
        gradienSubtractProgram.SetTexture("_Pressure", pressure_dfbo.GetReadFBO());
        gradienSubtractProgram.SetTexture("_Velocity", velocity_dfbo.GetReadFBO());
        gradienSubtractProgram.Blit(velocity_dfbo.GetWriteFBO(), velocity_dfbo.GetWriteFBO(), blendOption);
        velocity_dfbo.Swap();

        advectionProgram.SetFloat("_texelSize_x", 1.0f / simWidth);
        advectionProgram.SetFloat("_texelSize_y", 1.0f / simHeight);
        if (!supportLinearFiltering)
        {
            advectionProgram.SetFloat("_dyeTexelSize_x", 1.0f / simWidth);
            advectionProgram.SetFloat("_dyeTexelSize_y", 1.0f / simHeight);
        }
        advectionProgram.SetTexture("_Velocity", velocity_dfbo.GetReadFBO());
        advectionProgram.SetTexture("_Source", velocity_dfbo.GetReadFBO());
        advectionProgram.SetFloat("_dt", dt);
        advectionProgram.SetFloat("_dissipation", VELOCITY_DISSIPATION);
        advectionProgram.Blit(velocity_dfbo.GetWriteFBO(), velocity_dfbo.GetWriteFBO(), blendOption);
        velocity_dfbo.Swap();

        if (!supportLinearFiltering)
        {
            advectionProgram.SetFloat("_dyeTexelSize_x", 1.0f / dyeWidth);
            advectionProgram.SetFloat("_dyeTexelSize_y", 1.0f / dyeHeight);
        }
        advectionProgram.SetTexture("_Velocity", velocity_dfbo.GetReadFBO());
        advectionProgram.SetTexture("_Source", density_dfbo.GetReadFBO());
        advectionProgram.SetFloat("_dissipation", DENSITY_DISSIPATION);
        advectionProgram.Blit(density_dfbo.GetWriteFBO(), density_dfbo.GetWriteFBO(), blendOption);
        density_dfbo.Swap();
    }