Пример #1
0
 public void Reset()
 {
     Shape     = null;
     FatBounds = Aabb.Empty;
     Cost      = float.MinValue;
     ChildA    = null;
     ChildB    = null;
 }
Пример #2
0
    public Node CreateNode(RayMarchedShape Shape)
    {
        var node = Pool <Node> .Take();

        node.Reset();

        node.Shape     = Shape;
        node.Bounds    = Shape.Bounds;
        node.FatBounds = node.Bounds;
        node.FatBounds.Expand(FatBoundsRadius);
        node.Cost = node.Bounds.Cost;

        return(node);
    }
Пример #3
0
    private void DebugDrawNode(int index, bool drawBounds, bool drawLabel, bool fullTreeMode)
    {
        if (index == Null)
        {
            return;
        }

        Aabb bounds = m_nodes[index].Bounds;

        if (drawBounds)
        {
            if (m_nodes[index].IsLeaf || !fullTreeMode)
            {
                Handles.DrawWireCube(bounds.Center, bounds.Extents);

                RayMarchedShape shape = m_nodes[index].UserData as RayMarchedShape;
                if (shape != null)
                {
                    Aabb tightBounds = shape.Bounds;

                    Color prevColor = Handles.color;
                    Handles.color = new Color(prevColor.r, prevColor.g, prevColor.b, 0.3f);

                    Handles.DrawWireCube(tightBounds.Center, tightBounds.Extents);

                    Handles.color = prevColor;
                }
            }
        }

        Handles.SphereHandleCap(0, bounds.Center, Quaternion.identity, 0.05f, EventType.Repaint);

        if (drawLabel)
        {
            RayMarchedShape shape = m_nodes[index].UserData as RayMarchedShape;
            Handles.Label
            (
                bounds.Center,
                "Node  : " + index + (index == m_root ? " (root)" : "") + (m_nodes[index].IsLeaf ? " (Leaf)" : "")
                + "\nParent: " + m_nodes[index].Parent
                + "\nChildA: " + m_nodes[index].ChildA
                + "\nChildB: " + m_nodes[index].ChildB
                + "\nHeight: " + m_nodes[index].Height
                + (shape != null ? "\nShape : " + shape.ShapeIndex : "")
            );
        }
    }
Пример #4
0
    protected override void OnPreRenderImage(ComputeShader compute, RenderTexture src, RenderTexture dst)
    {
        var sdfShapes = RayMarchedShape.GetShapes();
        int numShapes = sdfShapes.Count;

        if (m_shapes == null || m_shapes.count != numShapes)
        {
            if (m_shapes != null)
            {
                m_shapes.Dispose();
            }

            m_shapes = new ComputeBuffer(Mathf.Max(1, numShapes), SdfShape.Stride);
        }

        m_shapes.SetData(sdfShapes);

        var camera = GetComponent <Camera>();

        compute.SetTexture(m_const.Kernel, m_const.Src, src);
        compute.SetTexture(m_const.Kernel, m_const.Dst, dst);

        compute.SetMatrix(m_const.CameraInverseProjection, camera.projectionMatrix.inverse);
        compute.SetMatrix(m_const.CameraToWorld, camera.cameraToWorldMatrix);
        compute.SetVector(m_const.CameraPosition, camera.transform.position);
        compute.SetInts(m_const.ScreenSize, new int[] { camera.pixelWidth, camera.pixelHeight });

        compute.SetBuffer(m_const.Kernel, m_const.SdfShapes, m_shapes);
        compute.SetInt(m_const.NumSdfShapes, numShapes);

        compute.SetVector(m_const.RayMarchParams, new Vector4(MaxRaySteps, RayHitThreshold, MaxRayDistance, Time.time));
        compute.SetFloat(m_const.BlendDistance, BlendDistance);

        compute.SetVector(m_const.BackgroundColor, new Vector4(BackgroundColor.r, BackgroundColor.g, BackgroundColor.b, BackgroundColor.a));
        compute.SetVector(m_const.MissColor, new Vector4(MissColor.r, MissColor.g, MissColor.b, MissColor.a));
    }
Пример #5
0
    private void OnDrawGizmos()
    {
    #if UNITY_EDITOR
        var camera = GetComponent <Camera>();

        if (DrawBoundingVolumes)
        {
            RayMarchedShape.DrawBoundingVolumeHierarchyGizmos(IsolateBoundingVolumeDepth);
        }

        if (TestBvhBoundsQuery)
        {
            Color prevColor = Handles.color;

            Aabb queryBounds =
                new Aabb
                (
                    camera.transform.position - 0.5f * Vector3.one,
                    camera.transform.position + 0.5f * Vector3.one
                );

            Handles.color = Color.yellow;
            Handles.DrawWireCube(queryBounds.Center, queryBounds.Extents);

            Handles.color = new Color(1.0f, 1.0f, 0.0f, 0.5f);
            RayMarchedShape.Query
            (
                queryBounds,
                (RayMarchedShape shape) =>
            {
                Handles.DrawWireCube(shape.Bounds.Center, shape.Bounds.Extents);
                return(true);
            }
            );

            Handles.color = prevColor;
        }

        if (TestBvhRayCast)
        {
            Color prevColor = Handles.color;

            Vector3 cameraFrom = camera.transform.position;
            Vector3 cameraTo   = cameraFrom + 10.0f * camera.transform.forward;

            Handles.color = Color.yellow;
            Handles.DrawLine(cameraFrom, cameraTo);

            Handles.color = new Color(1.0f, 1.0f, 0.0f, 0.5f);
            RayMarchedShape.RayCast
            (
                cameraFrom,
                cameraTo,
                (Vector3 from, Vector3 to, RayMarchedShape shape) =>
            {
                Handles.DrawWireCube(shape.Bounds.Center, shape.Bounds.Extents);
                return(1.0f);
            }
            );

            Handles.color = prevColor;
        }
    #endif
    }
Пример #6
0
    protected override void OnPreRenderImage(ComputeShader compute, RenderTexture src, RenderTexture dst)
    {
        // validate SDF shapes buffer
        var sdfShapes = RayMarchedShape.GetShapes();
        int numShapes = sdfShapes.Count;

        if (m_shapes == null ||
            m_shapes.count != numShapes)
        {
            if (m_shapes != null)
            {
                m_shapes.Dispose();
                m_shapes = null;
            }

            m_shapes = new ComputeBuffer(Mathf.Max(1, numShapes), SdfShape.Stride);
        }

        // validate SDF temp buffer
        if (m_tempBuffer == null ||
            m_tempBuffer.width != src.width ||
            m_tempBuffer.height != src.height)
        {
            if (m_tempBuffer != null)
            {
                DestroyImmediate(m_tempBuffer);
                m_tempBuffer = null;
            }

            m_tempBuffer = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.ARGBFloat);
            m_tempBuffer.enableRandomWrite = true;
            m_tempBuffer.Create();
        }

        // validate AABB tree buffer
        if (m_aabbTree == null ||
            m_aabbTree.count != RayMarchedShape.AabbTreeCapacity)
        {
            if (m_aabbTree != null)
            {
                m_aabbTree.Dispose();
                m_aabbTree = null;
            }

            m_aabbTree = new ComputeBuffer(RayMarchedShape.AabbTreeCapacity, AabbTree <RayMarchedShape> .NodePod.Stride);
        }

        // validate heat map buffer
        if (m_heatMap == null ||
            m_heatMap.width != src.width ||
            m_heatMap.height != src.height)
        {
            if (m_heatMap != null)
            {
                DestroyImmediate(m_heatMap);
                m_heatMap = null;
            }

            m_heatMap = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.RFloat);
            m_heatMap.enableRandomWrite = true;
            m_heatMap.Create();
        }

        // fill buffers
        m_shapes.SetData(sdfShapes);
        RayMarchedShape.FillAabbTree(m_aabbTree, AabbTree <RayMarchedShape> .FatBoundsRadius - BlendDistance);

        if (m_const.Kernels == null)
        {
            InitShaderConstants();
        }

        foreach (int kernel in m_const.Kernels)
        {
            compute.SetTexture(kernel, m_const.Src, src);
            compute.SetTexture(kernel, m_const.Dst, dst);

            compute.SetBuffer(kernel, m_const.SdfShapes, m_shapes);
            compute.SetTexture(kernel, m_const.TempBuffer, m_tempBuffer);

            compute.SetBuffer(kernel, m_const.AabbTree, m_aabbTree);

            compute.SetTexture(kernel, m_const.HeatMap, m_heatMap);
        }

        var camera = GetComponent <Camera>();

        compute.SetMatrix(m_const.CameraInverseProjection, camera.projectionMatrix.inverse);
        compute.SetMatrix(m_const.CameraToWorld, camera.cameraToWorldMatrix);
        compute.SetVector(m_const.CameraPosition, camera.transform.position);
        compute.SetInts(m_const.ScreenSize, new int[] { camera.pixelWidth, camera.pixelHeight });

        compute.SetInt(m_const.NumSdfShapes, numShapes);

        compute.SetVector(m_const.RayMarchParams, new Vector4(MaxRaySteps, RayHitThreshold, MaxRayDistance, Time.time));
        compute.SetFloat(m_const.BlendDistance, BlendDistance);

        compute.SetVector(m_const.BackgroundColor, new Vector4(BackgroundColor.r, BackgroundColor.g, BackgroundColor.b, BackgroundColor.a));
        compute.SetVector(m_const.MissColor, new Vector4(MissColor.r, MissColor.g, MissColor.b, MissColor.a));

        compute.SetBool(m_const.UseAabbTree, UseBoundingVolumes);
        compute.SetInt(m_const.AabbTreeRoot, RayMarchedShape.AabbTreeRoot);

        compute.SetVector(m_const.HeatColorCool, new Vector4(HeatColorCool.r, HeatColorCool.g, HeatColorCool.b, HeatColorCool.a));
        compute.SetVector(m_const.HeatColorMedium, new Vector4(HeatColorMedium.r, HeatColorMedium.g, HeatColorMedium.b, HeatColorMedium.a));
        compute.SetVector(m_const.HeatColorHot, new Vector4(HeatColorHot.r, HeatColorHot.g, HeatColorHot.b, HeatColorHot.a));
        compute.SetFloat(m_const.HeatAlpha, HeatAlpha);
    }
    protected override void OnPreRenderImage(ComputeShader compute, RenderTexture src, RenderTexture dst)
    {
        var sdfShapes = RayMarchedShape.GetShapes();
        int numShapes = sdfShapes.Count;

        if (m_shapes == null ||
            m_shapes.count != numShapes)
        {
            if (m_shapes != null)
            {
                m_shapes.Dispose();
            }

            m_shapes = new ComputeBuffer(Mathf.Max(1, numShapes), SdfShape.Stride);
        }

        if (m_heatMap == null ||
            m_heatMap.width != src.width ||
            m_heatMap.height != src.height)
        {
            if (m_heatMap != null)
            {
                DestroyImmediate(m_heatMap);
                m_heatMap = null;
            }

            m_heatMap = new RenderTexture(src.width, src.height, 0, RenderTextureFormat.RFloat);
            m_heatMap.enableRandomWrite = true;
            m_heatMap.Create();
        }

        m_shapes.SetData(sdfShapes);

        var camera = GetComponent <Camera>();

        if (m_const.Kernels == null)
        {
            InitShaderConstants();
        }

        foreach (int kernel in m_const.Kernels)
        {
            compute.SetTexture(kernel, m_const.Src, src);
            compute.SetTexture(kernel, m_const.Dst, dst);
            compute.SetTexture(kernel, m_const.HeatMap, m_heatMap);

            compute.SetBuffer(kernel, m_const.SdfShapes, m_shapes);
        }

        compute.SetMatrix(m_const.CameraInverseProjection, camera.projectionMatrix.inverse);
        compute.SetMatrix(m_const.CameraToWorld, camera.cameraToWorldMatrix);
        compute.SetVector(m_const.CameraPosition, camera.transform.position);
        compute.SetInts(m_const.ScreenSize, new int[] { camera.pixelWidth, camera.pixelHeight });

        compute.SetInt(m_const.NumSdfShapes, numShapes);

        compute.SetVector(m_const.RayMarchParams, new Vector4(MaxRaySteps, RayHitThreshold, MaxRayDistance, Time.time));
        compute.SetFloat(m_const.BlendDistance, BlendDistance);

        compute.SetVector(m_const.BackgroundColor, new Vector4(BackgroundColor.r, BackgroundColor.g, BackgroundColor.b, BackgroundColor.a));
        compute.SetVector(m_const.MissColor, new Vector4(MissColor.r, MissColor.g, MissColor.b, MissColor.a));

        compute.SetVector(m_const.HeatColorCool, new Vector4(HeatColorCool.r, HeatColorCool.g, HeatColorCool.b, HeatColorCool.a));
        compute.SetVector(m_const.HeatColorMedium, new Vector4(HeatColorMedium.r, HeatColorMedium.g, HeatColorMedium.b, HeatColorMedium.a));
        compute.SetVector(m_const.HeatColorHot, new Vector4(HeatColorHot.r, HeatColorHot.g, HeatColorHot.b, HeatColorHot.a));
        compute.SetFloat(m_const.HeatAlpha, HeatAlpha);
    }