diff --git a/Bonsai.Shaders/BindTexture.cs b/Bonsai.Shaders/BindTexture.cs index 7892a9d6c..b95bb1d2e 100644 --- a/Bonsai.Shaders/BindTexture.cs +++ b/Bonsai.Shaders/BindTexture.cs @@ -58,35 +58,25 @@ public bool IndexSpecified get { return Index.HasValue; } } - IObservable Process(IObservable source, Action update) + /// The source sequence to be processed + /// Function taking (int? index, Shader shader, TSource input) and producing a texture ID (or null to skip binding) + IObservable Process(IObservable source, Func getTextureId) { return Observable.Defer(() => { - var texture = default(Texture); - var textureName = default(string); return source.CombineEither( ShaderManager.ReserveShader(ShaderName), (input, shader) => { - if (textureName != TextureName) + if (getTextureId(Index, shader, input) is int textureId) { - textureName = TextureName; - texture = !string.IsNullOrEmpty(textureName) - ? shader.Window.ResourceManager.Load(textureName) - : null; - } - - if (texture != null) - { - var index = Index; - var textureId = index.HasValue ? ((TextureSequence)texture).Textures[index.Value] : texture.Id; shader.Update(() => { GL.ActiveTexture(TextureSlot); GL.BindTexture(TextureTarget, textureId); }); } - else if (update != null) shader.Update(() => update(input)); + return input; }); }); @@ -110,7 +100,28 @@ IObservable Process(IObservable source, Action public IObservable Process(IObservable source) { - return Process(source, update: null); + var texture = default(Texture); + var textureName = default(string); + + return Process(source, (maybeIndex, shader, _) => + { + if (textureName != TextureName) + { + textureName = TextureName; + texture = !string.IsNullOrEmpty(textureName) + ? shader.Window.ResourceManager.Load(textureName) + : null; + } + + if (texture != null) + { + return Index is int index + ? ((TextureSequence)texture).Textures[index] + : texture.Id; + } + + return null; + }); } /// @@ -133,10 +144,11 @@ public IObservable Process(IObservable source) /// public IObservable Process(IObservable source) { - return Process(source, input => + return Process(source, (maybeIndex, _, input) => { - GL.ActiveTexture(TextureSlot); - GL.BindTexture(TextureTarget, input != null ? input.Id : 0); + return maybeIndex is int index + ? ((TextureSequence)input).Textures[index] + : input.Id; }); } }