private void ApplyMapping(Texture texture, bool requiresYFlip, int plane, int materialIndex = -1) { if (_renderer != null) { _isDirty = false; Material[] meshMaterials = _renderer.materials; if (meshMaterials != null) { for (int i = 0; i < meshMaterials.Length; i++) { if (_materialIndex < 0 || i == _materialIndex) { Material mat = meshMaterials[i]; if (mat != null) { if (plane == 0) { VideoRender.SetupMaterialForMedia(mat, _media, _propTexture.Id, texture); _lastTextureApplied = texture; if(texture == _defaultTexture) { mat.EnableKeyword("USING_DEFAULT_TEXTURE"); } else { mat.DisableKeyword("USING_DEFAULT_TEXTURE"); } if (texture != null) { if (requiresYFlip) { mat.SetTextureScale(_propTexture.Id, new Vector2(_scale.x, -_scale.y)); mat.SetTextureOffset(_propTexture.Id, Vector2.up + _offset); } else { mat.SetTextureScale(_propTexture.Id, _scale); mat.SetTextureOffset(_propTexture.Id, _offset); } } } else if (plane == 1) { if (texture != null) { if (requiresYFlip) { mat.SetTextureScale(VideoRender.PropChromaTex.Id, new Vector2(_scale.x, -_scale.y)); mat.SetTextureOffset(VideoRender.PropChromaTex.Id, Vector2.up + _offset); } else { mat.SetTextureScale(VideoRender.PropChromaTex.Id, _scale); mat.SetTextureOffset(VideoRender.PropChromaTex.Id, _offset); } } } } } } } } }
private void ApplyMapping(Texture texture, bool requiresYFlip, int plane = 0) { if (_material != null) { _isDirty = false; if (plane == 0) { VideoRender.SetupMaterialForMedia(_material, _media, _propTexture.Id, texture); _lastTextureApplied = texture; if (texture == _defaultTexture) { _material.EnableKeyword("USING_DEFAULT_TEXTURE"); } else { _material.DisableKeyword("USING_DEFAULT_TEXTURE"); } if (texture != null) { if (requiresYFlip) { _material.SetTextureScale(_propTexture.Id, new Vector2(_scale.x, -_scale.y)); _material.SetTextureOffset(_propTexture.Id, Vector2.up + _offset); } else { _material.SetTextureScale(_propTexture.Id, _scale); _material.SetTextureOffset(_propTexture.Id, _offset); } } } else if (plane == 1) { if (texture != null) { if (requiresYFlip) { _material.SetTextureScale(VideoRender.PropChromaTex.Id, new Vector2(_scale.x, -_scale.y)); _material.SetTextureOffset(VideoRender.PropChromaTex.Id, Vector2.up + _offset); } else { _material.SetTextureScale(VideoRender.PropChromaTex.Id, _scale); _material.SetTextureOffset(VideoRender.PropChromaTex.Id, _offset); } } } } }
internal static void SetupMaterial(Material material, bool flipVertically, bool playerSupportsLinear, Matrix4x4 ycbcrTransform, Texture ycbcrTexture = null, float[] textureTransform = null, VideoMapping mapping = VideoMapping.Normal, StereoPacking stereoPacking = StereoPacking.None, AlphaPacking alphaPacking = AlphaPacking.None) { SetupVerticalFlipMaterial(material, flipVertically); // Apply changes for layout if (material.HasProperty(VideoRender.PropLayout.Id)) { VideoRender.SetupLayoutMaterial(material, mapping); } // Apply changes for stereo videos if (material.HasProperty(VideoRender.PropStereo.Id)) { VideoRender.SetupStereoMaterial(material, stereoPacking); } // Apply changes for alpha videos if (material.HasProperty(VideoRender.PropAlphaPack.Id)) { VideoRender.SetupAlphaPackedMaterial(material, alphaPacking); } // Apply gamma correction #if UNITY_PLATFORM_SUPPORTS_LINEAR if (material.HasProperty(VideoRender.PropApplyGamma.Id)) { VideoRender.SetupGammaMaterial(material, playerSupportsLinear); } #endif // Adjust for cropping (when the decoder decodes in blocks that overrun the video frame size, it pads), OES only as we apply this lower down for none-OES #if (!UNITY_EDITOR && UNITY_ANDROID) // STE: HasProperty doesn't work on Matrix' // if (material.HasProperty(VideoRender.PropTextureMatrix.Id)) { VideoRender.SetupTextureMatrix(material, textureTransform); } #endif #if UNITY_PLATFORM_SUPPORTS_YPCBCR VideoRender.SetupYpCbCrMaterial(material, ycbcrTexture != null, ycbcrTransform, ycbcrTexture); #endif }
// We do a LateUpdate() to allow for any changes in the texture that may have happened in Update() void LateUpdate() { if (_setNativeSize) { SetNativeSize(); } if (_lastTexture != mainTexture) { _lastTexture = mainTexture; SetVerticesDirty(); SetMaterialDirty(); } if (HasValidTexture()) { if (mainTexture != null) { Orientation orientation = Helper.GetOrientation(_mediaPlayer.Info.GetTextureTransform()); if (mainTexture.width != _lastWidth || mainTexture.height != _lastHeight || orientation != _lastOrientation) { _lastWidth = mainTexture.width; _lastHeight = mainTexture.height; _lastOrientation = orientation; SetVerticesDirty(); SetMaterialDirty(); } } } if (Application.isPlaying) { if (!_isUserMaterial) { UpdateInternalMaterial(); } } if (material != null && _mediaPlayer != null) { // TODO: only run when dirty VideoRender.SetupMaterialForMedia(materialForRendering, _mediaPlayer); } }
public static RenderTexture ResolveVideoToRenderTexture(Material resolveMaterial, RenderTexture targetTexture, ITextureProducer texture, ResolveFlags flags, ScaleMode scaleMode = ScaleMode.StretchToFill) { int targetWidth = texture.GetTexture(0).width; int targetHeight = texture.GetTexture(0).height; GetResolveTextureSize(texture.GetTextureAlphaPacking(), texture.GetTextureStereoPacking(), StereoEye.Left, ref targetWidth, ref targetHeight); if (targetTexture) { bool sizeChanged = (targetTexture.width != targetWidth || targetTexture.height != targetHeight); if (sizeChanged) { RenderTexture.ReleaseTemporary(targetTexture); targetTexture = null; } } if (!targetTexture) { RenderTextureReadWrite readWrite = ((flags & ResolveFlags.ColorspaceSRGB) == ResolveFlags.ColorspaceSRGB) ? RenderTextureReadWrite.sRGB : RenderTextureReadWrite.Linear; targetTexture = RenderTexture.GetTemporary(targetWidth, targetHeight, 0, RenderTextureFormat.ARGB32, readWrite); } // Set target mipmap generation support { bool requiresMipmap = (flags & ResolveFlags.Mipmaps) == ResolveFlags.Mipmaps; bool requiresRecreate = (targetTexture.IsCreated() && targetTexture.useMipMap != requiresMipmap); if (requiresRecreate) { targetTexture.Release(); } if (!targetTexture.IsCreated()) { targetTexture.useMipMap = targetTexture.autoGenerateMips = requiresMipmap; targetTexture.Create(); } } // Render resolve blit // TODO: combine these two paths into a single material blit { bool prevSRGB = GL.sRGBWrite; GL.sRGBWrite = targetTexture.sRGB; RenderTexture prev = RenderTexture.active; if (scaleMode == ScaleMode.StretchToFill) { Graphics.Blit(texture.GetTexture(0), targetTexture, resolveMaterial); } else { RenderTexture.active = targetTexture; bool partialAreaRender = (scaleMode == ScaleMode.ScaleToFit); if (partialAreaRender) { GL.Clear(false, true, Color.black); } VideoRender.DrawTexture(new Rect(0f, 0f, targetTexture.width, targetTexture.height), texture.GetTexture(0), scaleMode, texture.GetTextureAlphaPacking(), resolveMaterial); } RenderTexture.active = prev; GL.sRGBWrite = prevSRGB; } return(targetTexture); }
void OnGUI() { #if UNITY_EDITOR if (_showAreaInEditor && !Application.isPlaying) { DrawArea(); return; } #endif if (_mediaPlayer == null) { return; } Texture texture = null; if (_showAreaInEditor) { #if UNITY_EDITOR texture = Texture2D.whiteTexture; #endif } texture = VideoRender.GetTexture(_mediaPlayer, 0); if (_mediaPlayer.Info != null && !_mediaPlayer.Info.HasVideo()) { texture = null; } if (texture != null) { bool isTextureVisible = (_color.a > 0f || !_allowTransparency); if (isTextureVisible) { GUI.depth = _depth; GUI.color = _color; Rect rect = GetAreaRect(); // TODO: change this to a material-only path so we only have a single drawing path if (_material != null) { // TODO: Only setup material when needed VideoRender.SetupMaterialForMedia(_material, _mediaPlayer); // NOTE: It seems that Graphics.DrawTexture() behaves differently than GUI.DrawTexture() when it comes to sRGB writing // on newer versions of Unity (at least 2018.2.19 and above), so now we have to force the conversion to sRGB on writing bool restoreSRGBWrite = false; #if UNITY_EDITOR_WIN || (!UNITY_EDITOR && UNITY_STANDALONE_WIN) if (QualitySettings.activeColorSpace == ColorSpace.Linear && !GL.sRGBWrite) { restoreSRGBWrite = true; } #endif if (restoreSRGBWrite) { GL.sRGBWrite = true; } VideoRender.DrawTexture(rect, texture, _scaleMode, _mediaPlayer.TextureProducer.GetTextureAlphaPacking(), _material); if (restoreSRGBWrite) { GL.sRGBWrite = false; } } else { bool requiresVerticalFlip = false; if (_mediaPlayer.TextureProducer != null) { requiresVerticalFlip = _mediaPlayer.TextureProducer.RequiresVerticalFlip(); } if (requiresVerticalFlip) { GUIUtility.ScaleAroundPivot(new Vector2(1f, -1f), new Vector2(0f, rect.y + (rect.height / 2f))); } GUI.DrawTexture(rect, texture, _scaleMode, _allowTransparency); } } } }