public void Dispose() { if (ColorConverter.IsValueCreated) { ColorConverter.Value.Dispose(); } _solidColorBrush?.Dispose(); _solidColorBrush = null; RenderTarget.Dispose(); RenderTarget = null; _factory.Dispose(); _factory = null; _writeFactory?.Dispose(); _writeFactory = null; _imagingFactory?.Dispose(); _imagingFactory = null; PreviewTexture.Dispose(); PreviewTexture = null; DesktopTexture.Dispose(); DesktopTexture = null; StagingTexture.Dispose(); StagingTexture = null; Device.Dispose(); Device = null; }
public void StartPreview(DeviceCamera genericCamera, Action startCallback, Action frameCallback) { this.startCallback = startCallback; this.frameCallback = frameCallback; var camera = genericCamera as DeviceCameraLegacy; // Create preview if (PreviewTexture && this.camera != camera) { WebCamTexture.Destroy(PreviewTexture); PreviewTexture = null; } if (!PreviewTexture) { if (camera.PreviewResolution.x == 0) { PreviewTexture = new WebCamTexture(camera.Device.name); } else { PreviewTexture = new WebCamTexture( camera.Device.name, camera.PreviewResolution.x, camera.PreviewResolution.y, (int)Mathf.Max(camera.Framerate, 30) ); } this.camera = camera as DeviceCameraLegacy; } // Start preview firstFrame = true; PreviewTexture.Play(); DispatchUtility.onFrame += Update; }
private void Update() { // Check that we are playing if ( !PreviewTexture.didUpdateThisFrame || PreviewTexture.width == 16 || PreviewTexture.height == 16 ) { return; } // Update preview buffer if (previewBuffer == null) { previewBuffer = PreviewTexture.GetPixels32(); } else { PreviewTexture.GetPixels32(previewBuffer); } // Invoke events if (firstFrame) { startCallback(); firstFrame = false; } if (frameCallback != null) { frameCallback(); } }
public void CapturePhoto(PhotoCallback callback) { var photo = new Texture2D(PreviewTexture.width, PreviewTexture.height, TextureFormat.RGB24, false, false); photo.SetPixels32(PreviewTexture.GetPixels32()); photo.Apply(); callback(photo); }
public void StopPreview() { DispatchUtility.onFrame -= Update; PreviewTexture.Stop(); WebCamTexture.Destroy(PreviewTexture); PreviewTexture = null; previewBuffer = null; camera = null; startCallback = frameCallback = null; }
public void Release() { if (!PreviewTexture) { return; } OnStart = OnFrame = null; PreviewTexture.Stop(); WebCamTexture.Destroy(PreviewTexture); PreviewTexture = null; previewBuffer = null; dispatch.Dispose(); dispatch = null; camera = -1; }
private void Update() { // Check that we are playing if ( !PreviewTexture || !PreviewTexture.isPlaying || !PreviewTexture.didUpdateThisFrame || PreviewTexture.width == 16 || PreviewTexture.height == 16 ) { if (dispatch != null) { dispatch.Dispatch(Update); } return; } // Update preview buffer if (previewBuffer == null) { previewBuffer = PreviewTexture.GetPixels32(); } else { PreviewTexture.GetPixels32(previewBuffer); } // Invoke events if (firstFrame) { if (OnStart != null) { OnStart(); } firstFrame = false; } if (OnFrame != null) { OnFrame(); } // Re-invoke if (dispatch != null) { dispatch.Dispatch(Update); } }
private void OnIMGUIRendered() { if (EditorApplication.isPlaying && !EditorApplication.isPaused) { EditorGUIUtility.keyboardControl = 0; } var type = Event.current.type; if (type == EventType.Repaint) { if (PreviewTexture != null && PreviewTexture.IsCreated()) { LoadResources(); RenderPreviewImage(); RenderDeviceImage(); RenderSafeArea(); } } if (type != EventType.Repaint && type != EventType.Layout && type != EventType.Used) { var useEvent = true; if (!Event.current.isKey || (!EditorApplication.isPlaying || EditorApplication.isPaused)) { return; } EditorGUIUtility.QueueGameViewInputEvent(Event.current); // Don't use command events, or they won't be sent to other views. if (type == EventType.ExecuteCommand || type == EventType.ValidateCommand) { useEvent = false; } if (useEvent) { Event.current.Use(); } } }
// save the render texture to png public void SavePNG(string path) { if (path.Length != 0) { _lastPNGPathName = path; byte[] pngData = PreviewTexture.EncodeToPNG(); if (pngData != null) { try { File.WriteAllBytes(path, pngData); } catch (Exception ex) { Debug.Log(ex); } } } }
public void Play() { // Create dispatch if (dispatch == null) { dispatch = new MainDispatch(); dispatch.Dispatch(Update); } // Create preview if (!PreviewTexture) { string name = WebCamTexture.devices[camera].name; var resolution = Device.GetPreviewResolution(camera); var rate = Mathf.Max(30, (int)Device.GetFramerate(camera)); PreviewTexture = resolution.width == 0 ? new WebCamTexture(name) : new WebCamTexture(name, resolution.width, resolution.height, rate); } // Start preview firstFrame = true; PreviewTexture.Play(); }
public void Pause() { PreviewTexture.Stop(); }
void Update() { previewObjectAlpha = managerObject.previewCameraAlpha; if (outsideObject.Count != 0 && previewAvailable) { foreach (string key in outsideObject.Keys) { if (!previewObject.ContainsKey(key)) { RenderTexture previewRtt = new RenderTexture(Screen.width / 4, Screen.height / 4, 16, RenderTextureFormat.Default, RenderTextureReadWrite.Default); previewRtt.filterMode = FilterMode.Point; previewRtt.anisoLevel = 0; previewRtt.antiAliasing = 1; previewRtt.Create(); GameObject previewCam = new GameObject(); previewCam.AddComponent <Camera>(); previewCam.GetComponent <Camera>().targetTexture = previewRtt; previewCam.GetComponent <Camera>().cullingMask = previewCameraLayer; previewCam.transform.LookAt(outsideObject[key].transform.position); GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane); plane.transform.parent = previewPlane.transform; plane.GetComponent <Renderer>().shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; Material planeMat = plane.GetComponent <Renderer>().material; planeMat.mainTexture = previewRtt; Color planeColor = planeMat.color; planeColor.a = previewObjectAlpha; planeMat.color = planeColor; planeMat.SetFloat("_Mode", 3); planeMat.SetInt("_SrcBlend", (int)UnityEngine.Rendering.BlendMode.One); planeMat.SetInt("_DstBlend", (int)UnityEngine.Rendering.BlendMode.OneMinusSrcAlpha); planeMat.SetInt("_ZWrite", 0); planeMat.DisableKeyword("_ALPHATEST_ON"); planeMat.DisableKeyword("_ALPHABLEND_ON"); planeMat.EnableKeyword("_ALPHAPREMULTIPLY_ON"); planeMat.renderQueue = 3000; plane.GetComponent <Renderer>().material = planeMat; plane.transform.localScale = new Vector3(.2f, .2f, .2f); plane.transform.localEulerAngles = new Vector3(0.0f, 180.0f, 0.0f); plane.layer = LayerMask.NameToLayer("PreviewCamera"); previewObject[key] = new PreviewTexture(plane, previewCam, previewRtt); } else { Vector3 v3Pos = Camera.main.WorldToViewportPoint(outsideObject[key].transform.position); if (v3Pos.x >= 0.0f && v3Pos.x <= 1.0f && v3Pos.y >= 0.0f && v3Pos.y <= 1.0f && v3Pos.z > 0) { previewHideFlag = false; previewObject[key].previewObject.transform.GetComponent <Renderer>().enabled = false; } else if (!previewHideFlag && previewDelay) { StartCoroutine("DelayPreview", previewObject[key].previewObject); } else if (!previewDelay) { previewObject[key].previewObject.transform.GetComponent <Renderer>().enabled = true; } previewObject[key].previewCamera.transform.LookAt(outsideObject[key].transform.position); } } RemovePreview(); ArrangePreview(); } }