Пример #1
0
    public System.Collections.IEnumerator doCapture(Camera inCamera, System.Action <Texture2D> inResultCallback)
    {
        Texture2D theResultTexture = null;

        Camera.CameraCallback captureLambda = (Camera inRenderedCamera) => {
            if (inCamera != inRenderedCamera)
            {
                return;
            }

            Rect theCapturingRect = worldRectToScreenRect(captureZoneWorldRect, inCamera);

            Vector2Int theCapturingSizeInt = Vector2Int.CeilToInt(theCapturingRect.size);
            theResultTexture = new Texture2D(
                theCapturingSizeInt.x, theCapturingSizeInt.y, TextureFormat.RGB24, false);
            theResultTexture.ReadPixels(theCapturingRect, 0, 0, false);
            theResultTexture.Apply();
        };

        Camera.onPostRender += captureLambda;
        while (!theResultTexture)
        {
            yield return(null);
        }
        Camera.onPostRender -= captureLambda;

        inResultCallback.Invoke(theResultTexture);
    }
        public void QueueWrite(byte[] Bytes, bool Copy = false, Camera AfterCamera = null)
        {
            //	todo: make copy of bytes here, but it'll be slow
            //			we can pin/get GC handle for byte[] but intptr won't work that way
            if (Copy)
            {
                throw new System.Exception("Currently not copying, assuming caller won't delete bytes over the next frame");
            }

            if (!QueueWritePixels(CacheIndex.Value, Bytes, Bytes.Length))
            {
                throw new System.Exception("SetCacheBytes returned error");
            }

            //	queue a write
            if (AfterCamera != null)
            {
                if (IssueEventCallback == null)
                {
                    IssueEventCallback = (c) => {
                        if (c == AfterCamera)
                        {
                            GL.IssuePluginEvent(PluginFunction, CacheIndex.Value);
                        }
                    };
                    Camera.onPostRender += IssueEventCallback;
                }
            }
            else
            {
                GL.IssuePluginEvent(PluginFunction, CacheIndex.Value);
            }
        }
        public void Release()
        {
            //	release unity's texture before we destroy the texture/shader view
            if (NewTexture)
            {
                //	update to zero, will crash on dx11
                //	metal it seems from googling needs an explicit release.
                //t.UpdateExternalTexture(System.IntPtr.Zero);
                //	delete unity's texture and we're safe to free resources (texture will go black)
                Texture2D.Destroy(NewTexture);
                NewTexture = null;
            }

            //	gr: check we don't release whilst still using data
            if (IssueEventCallback != null)
            {
                Camera.onPostRender -= IssueEventCallback;
                IssueEventCallback   = null;
            }

            if (CacheIndex.HasValue)
            {
                //	if we release here with a texture still using the plugin's texture, we may crash!
                ReleaseCache(CacheIndex.Value);
                CacheIndex = null;
            }
        }
Пример #4
0
        public void     Release()
        {
            if (CacheIndex.HasValue)
            {
                ReleaseCache(CacheIndex.Value);
            }

            if (PixelFloatsAllocHandle != null)
            {
                PixelFloatsAllocHandle.Value.Free();
                PixelFloatsAllocHandle = null;
                PixelFloats            = null;
            }

            if (PixelBytesAllocHandle != null)
            {
                PixelBytesAllocHandle.Value.Free();
                PixelBytesAllocHandle = null;
                PixelBytes            = null;
            }

            if (IssueEventCallback != null)
            {
                Camera.onPostRender -= IssueEventCallback;
                IssueEventCallback   = null;
            }
        }
Пример #5
0
    void Awake()
    {
        var mf = GetComponent <MeshFilter>();

        if (!mf || !mf.sharedMesh)
        {
            if (!mf)
            {
                mf = gameObject.AddComponent <MeshFilter>();
            }
            mf.sharedMesh      = new Mesh();
            mf.sharedMesh.name = "Callbackproxy";
#if WAITING_FOR_ENGINE_REGRESSION_FIX
            mf.sharedMesh.bounds = new Bounds(Vector3.zero, Vector3.one * 10000f);
            mf.sharedMesh.SetTriangles((int[])null, 0);
#else
            mf.sharedMesh.vertices = new[] { new Vector3(0f, -100f, 0f) };
            mf.sharedMesh.SetIndices(new[] { 0 }, MeshTopology.Points, 0);
            mf.sharedMesh.bounds = new Bounds(Vector3.zero, Vector3.one * 10000f);
#endif
        }

        if (!GetComponent <MeshRenderer>())
        {
            var mr = gameObject.AddComponent <MeshRenderer>();
            mr.receiveShadows       = false;
            mr.lightProbeUsage      = UnityEngine.Rendering.LightProbeUsage.Off;
            mr.shadowCastingMode    = UnityEngine.Rendering.ShadowCastingMode.Off;
            mr.reflectionProbeUsage = UnityEngine.Rendering.ReflectionProbeUsage.Off;
        }

        if (occlusionShader == null)
        {
            occlusionShader = Shader.Find("Hidden/AtmosphericScattering_Occlusion");
        }

        m_occlusionMaterial           = new Material(occlusionShader);
        m_occlusionMaterial.hideFlags = HideFlags.HideAndDontSave;

        if (worldRayleighColorRamp == null)
        {
            worldRayleighColorRamp = new Gradient();
            worldRayleighColorRamp.SetKeys(
                new[] { new GradientColorKey(new Color(0.3f, 0.4f, 0.6f), 0f), new GradientColorKey(new Color(0.5f, 0.6f, 0.8f), 1f) },
                new[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) }
                );
        }
        if (worldMieColorRamp == null)
        {
            worldMieColorRamp = new Gradient();
            worldMieColorRamp.SetKeys(
                new[] { new GradientColorKey(new Color(0.95f, 0.75f, 0.5f), 0f), new GradientColorKey(new Color(1f, 0.9f, 8.0f), 1f) },
                new[] { new GradientAlphaKey(1f, 0f), new GradientAlphaKey(1f, 1f) }
                );
        }

        m_precullCallback = new Camera.CameraCallback(CBOnPreCull);

        m_isAwake = true;
    }
Пример #6
0
 public static void TriggerCameraCallback(Camera camera, string message, Camera.CameraCallback callback)
 {
     camera.SendMessage(message, SendMessageOptions.DontRequireReceiver);
     if (callback != null)
     {
         callback(camera);
     }
 }
 public static Delegate Camera_CameraCallback(LuaFunction func)
 {
     Camera.CameraCallback d = (param0) =>
     {
         int    top = func.BeginPCall();
         IntPtr L   = func.GetLuaState();
         LuaScriptMgr.Push(L, param0);
         func.PCall(top, 1);
         func.EndPCall(top);
     };
     return(d);
 }
Пример #8
0
 public void ReadAsync(Camera AfterCamera = null)
 {
     if (AfterCamera != null)
     {
         if (IssueEventCallback == null)
         {
             IssueEventCallback = (c) => {
                 if (c == AfterCamera)
                 {
                     GL.IssuePluginEvent(PluginFunction, CacheIndex.Value);
                 }
             };
             Camera.onPostRender += IssueEventCallback;
         }
     }
     else
     {
         GL.IssuePluginEvent(PluginFunction, CacheIndex.Value);
     }
 }
Пример #9
0
 public static Delegate UnityEngine_Camera_CameraCallback(LuaFunction func, LuaTable self, bool flag)
 {
     if (func == null)
     {
         return(new Camera.CameraCallback(delegate(Camera param0)
         {
         }));
     }
     if (!flag)
     {
         DelegateFactory.UnityEngine_Camera_CameraCallback_Event unityEngine_Camera_CameraCallback_Event = new DelegateFactory.UnityEngine_Camera_CameraCallback_Event(func);
         Camera.CameraCallback cameraCallback = new Camera.CameraCallback(unityEngine_Camera_CameraCallback_Event.Call);
         unityEngine_Camera_CameraCallback_Event.method = cameraCallback.Method;
         return(cameraCallback);
     }
     DelegateFactory.UnityEngine_Camera_CameraCallback_Event unityEngine_Camera_CameraCallback_Event2 = new DelegateFactory.UnityEngine_Camera_CameraCallback_Event(func, self);
     Camera.CameraCallback cameraCallback2 = new Camera.CameraCallback(unityEngine_Camera_CameraCallback_Event2.CallWithSelf);
     unityEngine_Camera_CameraCallback_Event2.method = cameraCallback2.Method;
     return(cameraCallback2);
 }
 //	queue a write update
 public void QueueUpdate(Camera AfterCamera = null)
 {
     //	queue a write
     if (AfterCamera != null)
     {
         if (IssueEventCallback == null)
         {
             IssueEventCallback = (c) => {
                 if (c == AfterCamera)
                 {
                     GL.IssuePluginEvent(PluginFunction, CacheIndex.Value);
                 }
             };
             Camera.onPostRender += IssueEventCallback;
         }
     }
     else
     {
         GL.IssuePluginEvent(PluginFunction, CacheIndex.Value);
     }
 }
Пример #11
0
 public static void UnregisterEndRender(Camera.CameraCallback callback)
 {
     Camera.onPostRender -= callback;
 }
Пример #12
0
 public static void UnregisterBeginRender(Camera.CameraCallback callback)
 {
     Camera.onPreCull -= callback;
 }
        private void SetupCameraUpdateCallback(EyeType type)
        {
            var eyeCamera          = _eyeCamera[type];
            var eyeTransform       = _eyeTransform[type];
            var homographyMaterial = _eyeCamMaterial[type];

            Action <Camera> updateState = (camera) =>
            {
                _faceTracker.GetCurrentFacePose(out _currentFacePose);
                var eyePose = _currentFacePose.GetEyePose(type);
                eyeTransform.SetPositionAndRotation(eyePose.position, eyePose.rotation);

                _faceTracker.GetCurrentProjMatrices(eyeCamera.nearClipPlane, eyeCamera.farClipPlane,
                                                    out _currentProjMatrices);
                var projMat = _currentProjMatrices.GetProjectionMatrix(type);

                if (!SRDHelper.HasNanOrInf(projMat))
                {
                    eyeCamera.ResetProjectionMatrix();
                    eyeCamera.fieldOfView      = CalcVerticalFoVFromProjectionMatrix(projMat);
                    eyeCamera.aspect           = CalcAspectWperHFromProjectionMatrix(projMat);
                    eyeCamera.projectionMatrix = projMat;

                    if (_isBoxFrontClippingCache)
                    {
                        var nearClipPlaneTF = _srdManager.DisplayEdges.LeftBottom;
                        eyeCamera.projectionMatrix = CalcObliquedNearClipProjectionMatrix(eyeCamera, nearClipPlaneTF.forward,
                                                                                          nearClipPlaneTF.position + nearClipPlaneTF.forward * ObliquedNearClipOffset * nearClipPlaneTF.lossyScale.x);
                    }
                }

                var homographyMat = SRDHelper.CalcHomographyMatrix(_srdManager.DisplayEdges.LeftUp.position, _srdManager.DisplayEdges.LeftBottom.position,
                                                                   _srdManager.DisplayEdges.RightBottom.position, _srdManager.DisplayEdges.RightUp.position,
                                                                   eyeCamera);
                var invHomographyMat = SRDHelper.CalcInverseMatrix3x3(homographyMat);
                homographyMaterial.SetFloatArray("_Homography", invHomographyMat);
                homographyMaterial.SetFloatArray("_InvHomography", homographyMat);
            };

            if (_isSRPUsed)
            {
#if SRP_AVAILABLE
                SRPCallbackFunc srpCallback = (context, camera) =>
                {
                    if (camera.name != eyeCamera.name)
                    {
                        return;
                    }
                    updateState(camera);
                };
                _eyeCamSRPPreCallback[type] = srpCallback;
                RenderPipelineManager.beginCameraRendering += _eyeCamSRPPreCallback[type];
#endif
            }
            else
            {
                Camera.CameraCallback cameraStateUpdate = (camera) =>
                {
                    if (camera.name != eyeCamera.name)
                    {
                        return;
                    }
                    updateState(camera);
                };
                _eyeCamStateUpdateCallback[type] = cameraStateUpdate;
                // This Should be onPreCull for correct frustum culling, however onPreCull is fired before vblank sometimes.
                // That's why onPreRender is used to make the latency shorter as possible
                Camera.onPreRender += _eyeCamStateUpdateCallback[type];
            }
        }
Пример #14
0
 protected IDispatch()
 {
     updateLoop = cam => Update();
     pending    = new Queue();
     executing  = new Queue();
 }