Exemple #1
0
    void OnPostRender()
    {
        if (!CheckResources())
        {
            return;
        }
        Camera cam = Camera.main;

        if (cam == null)
        {
            return;
        }

        //NOTE kind of overkill for just 2 volumes... but keeps it together
        _volumesData[0].WorldToLocal = volumeATransform.worldToLocalMatrix;
        _volumesData[0].Extents      = volumeA.bounds.extents;

        _volumesData[1].WorldToLocal = volumeBTransform.worldToLocalMatrix;
        _volumesData[1].Extents      = volumeB.bounds.extents;

        // Note: Assuming sphere and box are children!
        // If using single game object then scale is applied to bounds and localmatrix meaning its applied twice in shader!
        // Sphere
        _volumesData[2].WorldToLocal = sphere.worldToLocalMatrix;
        _volumesData[2].Extents      = sphere.GetChild(0).GetComponent <MeshRenderer>().bounds.extents;
        // Box
        _volumesData[3].WorldToLocal = box.worldToLocalMatrix;
        _volumesData[3].Extents      = box.GetChild(0).GetComponent <MeshRenderer>().bounds.extents;

        _volumes.SetData(_volumesData);


        AVolumeUtils.SetupRaymarchingMatrix(cam.fieldOfView, cam.worldToCameraMatrix, new Vector2(cam.pixelWidth, cam.pixelHeight));

        _cmd.Clear();

        _cmd.SetGlobalVector("_BlitScaleBiasRt", new Vector4(1f, 1f, 0f, 0f));
        _cmd.SetGlobalVector("_BlitScaleBias", new Vector4(1f, 1f, 0f, 0f));
        _cmd.SetGlobalBuffer("_VolumeBuffer", _volumes);

        _cmd.SetGlobalTexture("_VolumeATex", volumeA.sdfTexture);
        _cmd.SetGlobalTexture("_VolumeBTex", volumeB.sdfTexture);

        Vector4 sphereData = sphere.transform.position;

        sphereData.w = sphereRadius;
        _cmd.SetGlobalVector("_Sphere", sphereData);

        Vector4 boxData = box.transform.position;

        boxData.w = boxSize;
        _cmd.SetGlobalVector("_Box", boxData);

        _cmd.DrawProcedural(Matrix4x4.identity, _material, 0, MeshTopology.Quads, 4, 1);
        Graphics.ExecuteCommandBuffer(_cmd);
    }
Exemple #2
0
    private void OnRenderObject()
    {
        if (data == null || data.sdfTexture == null)
        {
            return;
        }

        //render preview - only for testing!
        //try to get active camera...
        Camera cam = Camera.main;

#if UNITY_EDITOR
        if (UnityEditor.SceneView.lastActiveSceneView != null)
        {
            cam = UnityEditor.SceneView.lastActiveSceneView.camera;
        }
#endif

        if (_material == null)
        {
            Shader shader = Shader.Find("XRA/SDFr");
            if (shader != null)
            {
                _material = new Material(shader);
            }
        }

        if (_material == null)
        {
            return;
        }

        CommandBuffer _cmd = new CommandBuffer();

        _material.SetTexture("_SDFVolumeTex", data.sdfTexture);
        _material.SetVector("_SDFVolumeExtents", Vector3.one * 0.5f);

        //_material.SetMatrix(_SDFVolumeLocalToWorld, _volume.LocalToWorldNoScale);
        _material.SetMatrix("_SDFVolumeWorldToLocal", volMatrixLocalToWorld.inverse);
        //_material.SetFloat(_SDFVolumeFlip, flip ? -1f : 1f);
        _material.SetVector("_BlitScaleBiasRt", new Vector4(1f, 1f, 0f, 0f));
        _material.SetVector("_BlitScaleBias", new Vector4(1f, 1f, 0f, 0f));
        _material.SetFloat("_SDFPreviewEpsilon", epsilon);
        _material.SetFloat("_SDFPreviewNormalDelta", normalDelta);

        AVolumeUtils.SetupRaymarchingMatrix(
            cam.fieldOfView,
            cam.worldToCameraMatrix,
            new Vector2(cam.pixelWidth, cam.pixelHeight));

        _cmd.DrawProcedural(Matrix4x4.identity, _material, 0, MeshTopology.Quads, 4, 1);
        Graphics.ExecuteCommandBuffer(_cmd);

        _cmd.Release();
    }
Exemple #3
0
    void OnPostRender()
    {
        if (!CheckResources())
        {
            return;
        }
        Camera cam = Camera.main;

        if (cam == null)
        {
            return;
        }

        //NOTE kind of overkill for just 2 volumes... but keeps it together
        _volumesData[0].WorldToLocal = volumeATransform.worldToLocalMatrix;
        _volumesData[0].Extents      = volumeA.bounds.extents;

        _volumesData[1].WorldToLocal = volumeBTransform.worldToLocalMatrix;
        _volumesData[1].Extents      = volumeB.bounds.extents;

        _volumes.SetData(_volumesData);


        AVolumeUtils.SetupRaymarchingMatrix(cam.fieldOfView, cam.worldToCameraMatrix, new Vector2(cam.pixelWidth, cam.pixelHeight));

        _cmd.Clear();

        _cmd.SetGlobalVector("_BlitScaleBiasRt", new Vector4(1f, 1f, 0f, 0f));
        _cmd.SetGlobalVector("_BlitScaleBias", new Vector4(1f, 1f, 0f, 0f));
        _cmd.SetGlobalBuffer("_VolumeBuffer", _volumes);

        _cmd.SetGlobalTexture("_VolumeATex", volumeA.sdfTexture);
        _cmd.SetGlobalTexture("_VolumeBTex", volumeB.sdfTexture);

        Vector4 sphereData = sphere.transform.position;

        sphereData.w = sphereRadius;
        _cmd.SetGlobalVector("_Sphere", sphereData);

        Vector4 boxData = box.transform.position;

        boxData.w = boxSize;
        _cmd.SetGlobalVector("_Box", boxData);

        _cmd.DrawProcedural(Matrix4x4.identity, _material, 0, MeshTopology.Quads, 4, 1);
        Graphics.ExecuteCommandBuffer(_cmd);
    }