Example #1
0
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (!EffectMaterial)
        {
            Graphics.Blit(source, destination);
            return;
        }

        EffectMaterial.SetColor("_Color1", color1);
        EffectMaterial.SetColor("_Color2", color2);
        EffectMaterial.SetColor("_Color3", color3);
        EffectMaterial.SetColor("_Color4", color4);
        EffectMaterial.SetColor("_Color5", color5);
        EffectMaterial.SetVector("_LightPos", SunLight.position);
        EffectMaterial.SetFloat("_DrawDistance", _RaymarchDrawDistance);
        EffectMaterial.SetVector("_CameraWP", CurrentCamera.transform.position);
        EffectMaterial.SetFloat("_FovX", CurrentCamera.fieldOfView * Mathf.Deg2Rad);
        EffectMaterial.SetVector("_CamForward", CurrentCamera.transform.forward.normalized);
        EffectMaterial.SetVector("_CamUp", CurrentCamera.transform.up.normalized);
        EffectMaterial.SetVector("_CamRight", CurrentCamera.transform.right.normalized);
        EffectMaterial.SetFloat("_AspectRatio", CurrentCamera.aspect);
        EffectMaterial.SetFloat("_LightStrength", lightStrength);


        Graphics.Blit(source, destination, EffectMaterial);
    }
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (!EffectMaterial)
        {
            Graphics.Blit(source, destination); // do nothing
            return;
        }

        // Set any custom shader variables here.  For example, you could do:
        // EffectMaterial.SetFloat("_MyVariable", 13.37f);
        // This would set the shader uniform _MyVariable to value 13.37

        EffectMaterial.SetVector("_LightDir", lightTransform ? lightTransform.forward : Vector3.down);

        //// Construct a Model Matrix for the Torus
        //Matrix4x4 MatTorus = Matrix4x4.TRS(
        //    Vector3.right * Mathf.Sin(Time.time) * 5,
        //    Quaternion.identity,
        //    Vector3.one);
        //MatTorus *= Matrix4x4.TRS(
        //    Vector3.zero,
        //    Quaternion.Euler(new Vector3(0, 0, (Time.time * 200) % 360)),
        //    Vector3.one);
        //// Send the torus matrix to our shader
        //EffectMaterial.SetMatrix("_MatTorus_InvModel", MatTorus.inverse);

        EffectMaterial.SetColor("_Color", mainColor);
        if (matColorRamp)
        {
            EffectMaterial.SetTexture("_ColorRamp_Material", matColorRamp);
        }
        if (perfColorRamp)
        {
            EffectMaterial.SetTexture("_ColorRamp_PerfMap", perfColorRamp);
        }
        if (densColorRamp)
        {
            EffectMaterial.SetTexture("_ColorRamp_Density", densColorRamp);
        }

        EffectMaterial.SetFloat("_DrawDistance", rayMarchMaxDist);

        if (EffectMaterial.IsKeywordEnabled("DEBUG_PERFORMANCE") != debugPerformance)
        {
            if (debugPerformance)
            {
                EffectMaterial.EnableKeyword("DEBUG_PERFORMANCE");
            }
            else
            {
                EffectMaterial.DisableKeyword("DEBUG_PERFORMANCE");
            }
        }

        EffectMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        EffectMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        EffectMaterial.SetVector("_CameraWS", CurrentCamera.transform.position);

        CustomGraphicsBlit(source, destination, EffectMaterial, 0);
    }
Example #3
0
    private void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (!EffectMaterial)
        {
            Graphics.Blit(source, destination);
            return;
        }

        EffectMaterial.SetColor("_ringColor", ringColor);
        EffectMaterial.SetFloat("radius", radius);
        EffectMaterial.SetFloat("thickness", thickness);
        EffectMaterial.SetVector("ringCenter", sourceTransform.position);
        EffectMaterial.SetVector("cameraPositionWS", MainCamera.transform.position);
        EffectMaterial.SetMatrix("cameraToWorldMatrix", MainCamera.cameraToWorldMatrix);
        EffectMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(MainCamera));

        CustomGraphicsBlit(source, destination, EffectMaterial, 0);
    }
Example #4
0
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (!EffectMaterial)
        {
            Graphics.Blit(source, destination); // do nothing
            return;
        }

        // pass frustum rays to shader
        EffectMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        EffectMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        EffectMaterial.SetVector("_CameraWS", CurrentCamera.transform.position);
        EffectMaterial.SetFloat("_Interpolator", t);
        EffectMaterial.SetMatrixArray("_Points", points.Select(x => Matrix4x4.TRS(x.position, x.rotation, x.localScale).inverse).ToArray());
        EffectMaterial.SetInt("_Points_size", points.Count);
        EffectMaterial.SetColor("_LightColor", lightColor);
        EffectMaterial.SetColor("_ShadowColor", shadowColor);
        CustomGraphicsBlit(source, destination, EffectMaterial, 0); // use given effect shader as image effect
    }
Example #5
0
    private void OnRenderImage(RenderTexture src, RenderTexture dest)
    {
        // if for some reason there is no material or if there are no Objects, do nothing
        if (!EffectMaterial || _objs == null || _objs.Count == 0)
        {
            Graphics.Blit(src, dest);
            return;
        }

        // sort the Objects by depth
        _objs.Sort((x, y) => (int)Mathf.Clamp(CurrentCamera.transform.InverseTransformPoint(y.transform.position).z - CurrentCamera.transform.InverseTransformPoint(x.transform.position).z, -1, 1));

        // set general shader values for camera
        EffectMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        EffectMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        EffectMaterial.SetVector("_CameraWS", CurrentCamera.transform.position);

        // set general shader values for raymarching
        EffectMaterial.SetVector("_RaymarchingParams", new Vector4(StepCount, StepCountInsideVolume, StepSize, DrawDist));

        // create to temporary Render textures as buffers
        RenderTexture temp0 = RenderTexture.GetTemporary(src.width, src.height, 0, src.format);
        RenderTexture temp1 = RenderTexture.GetTemporary(src.width, src.height, 0, src.format);

        // this is a variable for storing which buffer we last used
        int lastUsed = 0;

        // render all objects
        for (int i = 0; i < _objs.Count; ++i)
        {
            VolumetricObject obj = _objs[i];
            // set shader values that are type dependend
            VolumetricObject.Types t = obj.Type;
            switch (t)
            {
            case VolumetricObject.Types.BOX:
                EffectMaterial.SetInt("_Type", 0);
                EffectMaterial.SetVector("_BoxDimensions", obj.Dimensions * 0.5f);
                break;

            case VolumetricObject.Types.SPHERE:
                EffectMaterial.SetInt("_Type", 1);
                EffectMaterial.SetFloat("_SphereRad", obj.Radius);
                break;

            case VolumetricObject.Types.CAPSULE:
                EffectMaterial.SetInt("_Type", 2);
                EffectMaterial.SetMatrix("_CapsuleBounds", obj.CapsuleParams);
                break;
            }

            // set non type dependent per Object values
            EffectMaterial.SetColor("_Color", obj.Color);

            Color dc = obj.DenseColor;
            dc.a = obj.UseDenseColor ? 1 : 0;
            EffectMaterial.SetColor("_DenseColor", dc);

            EffectMaterial.SetVector("_FogOptions", new Vector4(obj.Density, (float)obj.Falloff, (float)obj.BlendMode, 0));
            EffectMaterial.SetMatrix("_Noise_STO", obj.NoiseSTO);
            EffectMaterial.SetMatrix("_InvModel", obj.transform.localToWorldMatrix.inverse);

            // render the Object by alternating between buffers
            if (i == 0)
            {
                CustomGraphicsBlit(src, temp0, EffectMaterial, 0);
                lastUsed = 0;
            }
            else if (i % 2 == 0)
            {
                CustomGraphicsBlit(temp1, temp0, EffectMaterial, 0);
                lastUsed = 0;
            }
            else
            {
                CustomGraphicsBlit(temp0, temp1, EffectMaterial, 0);
                lastUsed = 1;
            }
        }

        // render final render texture
        Graphics.Blit(lastUsed == 1 ? temp1 : temp0, dest);

        // clean up buffers
        RenderTexture.ReleaseTemporary(temp0);
        RenderTexture.ReleaseTemporary(temp1);
    }