ViewportMaterialView.cs 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. namespace Vuplex.WebView {
  18. /// <summary>
  19. /// Script that helps with setting the video-related shader properties on mobile.
  20. /// </summary>
  21. public class ViewportMaterialView : MonoBehaviour {
  22. public virtual Material Material {
  23. get {
  24. return GetComponent<Renderer>().sharedMaterial;
  25. }
  26. set {
  27. // Use sharedMaterial instead of material, because the latter creates copies
  28. // that are hard to destroy properly.
  29. GetComponent<Renderer>().sharedMaterial = value;
  30. }
  31. }
  32. /// <summary>
  33. /// The view's texture, which is `null` until the material has been set.
  34. /// </summary>
  35. public virtual Texture2D Texture {
  36. get {
  37. return (Texture2D) GetComponent<Renderer>().sharedMaterial.mainTexture;
  38. }
  39. set {
  40. GetComponent<Renderer>().sharedMaterial.mainTexture = value;
  41. }
  42. }
  43. public virtual bool Visible {
  44. get {
  45. return GetComponent<Renderer>().enabled;
  46. }
  47. set {
  48. GetComponent<Renderer>().enabled = value;
  49. }
  50. }
  51. public virtual void SetCropRect(Rect rect) {
  52. GetComponent<Renderer>().sharedMaterial.SetVector("_CropRect", _rectToVector(rect));
  53. }
  54. public virtual void SetCutoutRect(Rect rect) {
  55. var rectVector = _rectToVector(rect);
  56. if (rect != new Rect(0, 0, 1, 1)) {
  57. // Make the actual cutout slightly smaller (2% shorter and 2% skinnier) so that
  58. // the gap between the video layer and the viewport isn't visible.
  59. // This is only done if the rect doesn't cover the entire view, because
  60. // the Keyboard component uses a rect cutout of the entire view for Android Gecko.
  61. var onePercentOfWidth = rect.width * 0.01f;
  62. var onePercentOfHeight = rect.height * 0.01f;
  63. rectVector = new Vector4(
  64. rectVector.x + onePercentOfWidth,
  65. rectVector.y + onePercentOfHeight,
  66. rectVector.z - 2 * onePercentOfWidth,
  67. rectVector.w - 2 * onePercentOfHeight
  68. );
  69. }
  70. GetComponent<Renderer>().sharedMaterial.SetVector("_VideoCutoutRect", rectVector);
  71. }
  72. Vector4 _rectToVector(Rect rect) {
  73. return new Vector4(rect.x, rect.y, rect.width, rect.height);
  74. }
  75. }
  76. }