CanvasViewportMaterialView.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**
  2. * Copyright (c) 2021 Vuplex Inc. All rights reserved.
  3. *
  4. * Licensed under the Vuplex Commercial Software Library License, you may
  5. * not use this file except in compliance with the License. You may obtain
  6. * a copy of the License at
  7. *
  8. * https://vuplex.com/commercial-library-license
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. using UnityEngine;
  17. using UnityEngine.UI;
  18. namespace Vuplex.WebView {
  19. public class CanvasViewportMaterialView : ViewportMaterialView {
  20. public override Material Material {
  21. get {
  22. return GetComponent<RawImage>().material;
  23. }
  24. set {
  25. GetComponent<RawImage>().material = value;
  26. }
  27. }
  28. public override Texture2D Texture {
  29. get {
  30. return (Texture2D) GetComponent<RawImage>().material.mainTexture;
  31. }
  32. set {
  33. GetComponent<RawImage>().material.mainTexture = value;
  34. }
  35. }
  36. public override bool Visible {
  37. get {
  38. return GetComponent<RawImage>().enabled;
  39. }
  40. set {
  41. GetComponent<RawImage>().enabled = value;
  42. }
  43. }
  44. public override void SetCropRect(Rect rect) {
  45. GetComponent<RawImage>().material.SetVector("_CropRect", _rectToVector(rect));
  46. }
  47. public override void SetCutoutRect(Rect rect) {
  48. var rectVector = _rectToVector(rect);
  49. if (rect != new Rect(0, 0, 1, 1)) {
  50. // Make the actual cutout slightly smaller (2% shorter and 2% skinnier) so that
  51. // the gap between the video layer and the viewport isn't visible.
  52. // This is only done if the rect doesn't cover the entire view, because
  53. // the Keyboard component uses a rect cutout of the entire view for Android Gecko.
  54. var onePercentOfWidth = rect.width * 0.01f;
  55. var onePercentOfHeight = rect.height * 0.01f;
  56. rectVector = new Vector4(
  57. rectVector.x + onePercentOfWidth,
  58. rectVector.y + onePercentOfHeight,
  59. rectVector.z - 2 * onePercentOfWidth,
  60. rectVector.w - 2 * onePercentOfHeight
  61. );
  62. }
  63. GetComponent<RawImage>().material.SetVector("_VideoCutoutRect", rectVector);
  64. }
  65. Vector4 _rectToVector(Rect rect) {
  66. return new Vector4(rect.x, rect.y, rect.width, rect.height);
  67. }
  68. }
  69. }