Пример #1
0
    void Update()
    {
        if (!EffectMaterial || Application.isPlaying && !UpdateField)
        {
            return;
        }

        if (Tree == null)
        {
            return;
        }
        mFigures      = Tree.GetComponentsInChildren <Figure>();
        mFiguresCount = mFigures.Length;

        EffectMaterial.SetInt("_Size", mFiguresCount);

        var numbers    = new float[64];
        var parameters = new Matrix4x4[64];
        var transforms = new Matrix4x4[64];

        for (int i = 0; i < mFiguresCount; i++)
        {
            numbers[i]    = (float)mFigures[i].Type;
            parameters[i] = mFigures[i].Params;
            transforms[i] = mFigures[i].transform.worldToLocalMatrix;
        }

        EffectMaterial.SetFloatArray("_Numbers", numbers);
        EffectMaterial.SetMatrixArray("_Transforms", transforms);
        EffectMaterial.SetMatrixArray("_Params", parameters);
    }
Пример #2
0
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (!EffectMaterial)
        {
            Graphics.Blit(source, destination); // do nothing
            return;
        }

        EffectMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        EffectMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        EffectMaterial.SetVector("_CameraWS", CurrentCamera.transform.position);
        EffectMaterial.SetVector("_LightDir", mSunLight ? mSunLight.forward : Vector3.down);
        EffectMaterial.SetInt("_MaxStep", Application.isPlaying ? MaxSteps : 256);
        EffectMaterial.SetFloat("_MaxDist", Application.isPlaying ? MaxDistance : 10);

        Extensions.CustomGraphicsBlit(source, destination, EffectMaterial, 0); // use given effect shader as image effect
    }
Пример #3
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
    }
Пример #4
0
    void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        if (!EffectMaterial)
        {
            Graphics.Blit(source, destination);
            return;
        }

        var shapesData0Arr = new Vector4[_shapes.Count];
        var shapesData1Arr = new Vector4[_shapes.Count];

        for (int i = 0; i < _shapes.Count; i++)
        {
            var shape    = _shapes[i];
            var shapePos = shape.transform.position;
            shapesData0Arr[i] = new Vector4((float)shape.Type, (float)shape.Operator);
            if (shape.Type == RayMarchingShapeType.Sphere)
            {
                shapesData1Arr[i] = new Vector4(shapePos.x, shapePos.y, shapePos.z, shape.transform.localScale.x);
            }
            else if (shape.Type == RayMarchingShapeType.Box)
            {
                shapesData1Arr[i] = new Vector4(shapePos.x, shapePos.y, shapePos.z, shape.transform.localScale.x);
            }
        }

        EffectMaterial.SetMatrix("_FrustumCornersES", GetFrustumCorners(CurrentCamera));
        EffectMaterial.SetMatrix("_CameraInvViewMatrix", CurrentCamera.cameraToWorldMatrix);
        EffectMaterial.SetVector("_CameraWS", CurrentCamera.transform.position);
        EffectMaterial.SetVector("_LightDir", SunLight ? SunLight.forward : Vector3.down);
        if (_shapes.Count > 0)
        {
            EffectMaterial.SetInt("_ShapesCount", _shapes.Count);
            EffectMaterial.SetVectorArray("_ShapesData0Arr", shapesData0Arr);
            EffectMaterial.SetVectorArray("_ShapesData1Arr", shapesData1Arr);
        }

        CustomGraphicsBlit(source, destination, EffectMaterial, 0);
    }
Пример #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);
    }