Created
January 12, 2017 11:42
-
-
Save dfkeenan/0f0eb9ce26824cde3b318fe9c92df06b to your computer and use it in GitHub Desktop.
Xenko: Transitions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Threading.Tasks; | |
| using PhysicsSample.Transitions.Rendering.Images; | |
| using SiliconStudio.Core.Extensions; | |
| using SiliconStudio.Core.Mathematics; | |
| using SiliconStudio.Xenko.Engine; | |
| using SiliconStudio.Xenko.Graphics; | |
| using SiliconStudio.Xenko.Input; | |
| using SiliconStudio.Xenko.Rendering; | |
| using SiliconStudio.Xenko.Rendering.Composers; | |
| namespace PhysicsSample | |
| { | |
| public class NextSceneScript : AsyncScript | |
| { | |
| public List<string> Scenes = new List<string>(); | |
| public List<Texture> Textures = new List<Texture>(); | |
| public ChildSceneComponent ChildScene { get; set; } | |
| private int currentScene = 0; | |
| private int currentTexture = 0; | |
| private Transition transitionEffect; | |
| public override async Task Execute() | |
| { | |
| var scene = SceneSystem.SceneInstance.Scene; | |
| var compositor = ((SceneGraphicsCompositorLayers)scene.Settings.GraphicsCompositor); | |
| transitionEffect = compositor.Master.Renderers.OfType<SceneEffectRenderer>() | |
| .Select(r => r.Effect).OfType<Transitions.Rendering.Images.Transition>() | |
| .FirstOrDefault(); | |
| await FadeIn(); | |
| while (Game.IsRunning) | |
| { | |
| await Script.NextFrame(); | |
| if (Scenes.Count == 0 || Scenes[currentScene].IsNullOrEmpty()) | |
| continue; | |
| if (ChildScene != null && Input.IsKeyPressed(Keys.PageDown)) | |
| { | |
| currentScene++; | |
| currentScene %= Scenes.Count; | |
| await FadeOut(); | |
| ChildScene.Scene = await Content.LoadAsync<Scene>(Scenes[currentScene]); | |
| await FadeIn(); | |
| } | |
| } | |
| } | |
| private async Task FadeIn() | |
| { | |
| if (transitionEffect == null) | |
| return; | |
| float cutOff = 1; | |
| transitionEffect.Texture = GetNextTexture(); | |
| transitionEffect.Fade = 1; | |
| transitionEffect.CutOff = cutOff; | |
| while (Game.IsRunning && cutOff > 0) | |
| { | |
| await Script.NextFrame(); | |
| cutOff -= (float)(1.2 * Game.UpdateTime.Elapsed.TotalSeconds); | |
| if(cutOff < 0) cutOff = 0; | |
| transitionEffect.CutOff = cutOff; | |
| } | |
| } | |
| private async Task FadeOut() | |
| { | |
| if (transitionEffect == null) | |
| return; | |
| float cutOff = 0; | |
| transitionEffect.Texture = GetNextTexture(); | |
| transitionEffect.Fade = 1; | |
| transitionEffect.CutOff = cutOff; | |
| while (Game.IsRunning && cutOff < 1) | |
| { | |
| await Script.NextFrame(); | |
| cutOff += (float)(1.2 * Game.UpdateTime.Elapsed.TotalSeconds); | |
| if (cutOff > 1) cutOff = 1; | |
| transitionEffect.CutOff = cutOff; | |
| } | |
| } | |
| private Texture GetNextTexture() | |
| { | |
| currentTexture++; | |
| currentTexture %= Textures.Count; | |
| return Textures[currentTexture]; | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.Text; | |
| using System.Threading.Tasks; | |
| using SiliconStudio.Core; | |
| using SiliconStudio.Core.Annotations; | |
| using SiliconStudio.Core.Mathematics; | |
| using SiliconStudio.Xenko.Graphics; | |
| using SiliconStudio.Xenko.Rendering; | |
| using SiliconStudio.Xenko.Rendering.Images; | |
| namespace PhysicsSample.Transitions.Rendering.Images | |
| { | |
| [DataContract("Transition")] | |
| [Display("Transition")] | |
| public sealed class Transition: ImageEffect, IImageEffectRenderer | |
| { | |
| private ImageEffectShader transitionShader; | |
| public Transition() | |
| { | |
| Color = Color4.White; | |
| EnableDistort = false; | |
| Texture = null; | |
| CutOff = 1; | |
| } | |
| [DataMember(10)] | |
| public Color4 Color { get; set; } | |
| [DataMember(20)] | |
| public bool EnableDistort { get; set; } | |
| [DataMember(30)] | |
| public Texture Texture { get; set; } | |
| [DataMember(40)] | |
| [DataMemberRange(0,1,0.01,0.1, AllowNaN =false)] | |
| public float CutOff { get; set; } | |
| [DataMember(50)] | |
| [DataMemberRange(0, 1, 0.01, 0.1, AllowNaN = false)] | |
| public float Fade { get; set; } | |
| protected override void InitializeCore() | |
| { | |
| base.InitializeCore(); | |
| transitionShader = ToLoadAndUnload(new ImageEffectShader("TransitionEffect")); | |
| } | |
| protected override void DrawCore(RenderDrawContext context) | |
| { | |
| // Input texture | |
| var input = GetInput(0); | |
| var output = GetOutput(0); | |
| if (input == null || output == null) | |
| { | |
| return; | |
| } | |
| if (input == output) | |
| { | |
| var newInput = NewScopedRenderTarget2D(input.Width, input.Height, input.Format); | |
| context.CommandList.Copy(input, newInput); | |
| input = newInput; | |
| } | |
| //transitionShader.EffectInstance.UpdateEffect(context.GraphicsDevice); | |
| // Update parameters | |
| transitionShader.Parameters.Set(TransitionShaderKeys.Color, Color.ToColorSpace(GraphicsDevice.ColorSpace)); | |
| transitionShader.Parameters.Set(TransitionShaderKeys.EnableDistort, EnableDistort); | |
| transitionShader.Parameters.Set(TransitionShaderKeys.CutOff, CutOff); | |
| transitionShader.Parameters.Set(TransitionShaderKeys.Fade, Fade); | |
| transitionShader.SetInput(0, input); | |
| transitionShader.SetInput(1, Texture); | |
| transitionShader.SetOutput(output); | |
| transitionShader.Draw(context); | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| namespace PhysicsSample.Transitions.Rendering.Images | |
| { | |
| effect TransitionEffect | |
| { | |
| mixin TransitionShader; | |
| }; | |
| } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using SiliconStudio.Xenko.Rendering.Images; | |
| namespace PhysicsSample.Transitions.Rendering.Images | |
| { | |
| internal shader TransitionShader: ImageEffectShader | |
| { | |
| [Color] | |
| stage float4 Color; | |
| stage bool EnableDistort; | |
| stage float CutOff; | |
| stage float Fade; | |
| stage override float4 Shading() | |
| { | |
| float3 trans = Texture1.Sample(PointSampler, streams.TexCoord).rgb; | |
| float2 direction = float2(0,0); | |
| if(EnableDistort) | |
| { | |
| direction = normalize(float2((trans.r - 0.5) * 2, (trans.g - 0.5) * 2)); | |
| } | |
| float4 sourceColor = Texture0.Sample(PointSampler, streams.TexCoord + direction * CutOff); | |
| if(trans.b < CutOff) | |
| return lerp(sourceColor, Color, Fade); | |
| return sourceColor; | |
| } | |
| }; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment