GLSLBackground.cs 4.64 KB
Newer Older
pcy's avatar
pcy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Terraria;
using Terraria.ModLoader;
using Terraria.ModLoader.Core;
using PoroCYon.FNAGLSL;

using VertexType = Microsoft.Xna.Framework.Graphics.VertexPositionColorTexture;

namespace glsltest {
	public partial class GLSLBackground : ModSurfaceBgStyle {
		internal static BasicEffect hlsl;
		internal static GLSLEffect glsl;
		internal static TimeSpan totalElapsed = TimeSpan.Zero;

		// called from our Mod instance
		internal static void Load(GraphicsDevice gd) {
			totalElapsed = TimeSpan.Zero;

			hlsl = new BasicEffect(gd);

			hlsl.LightingEnabled = false;
			hlsl.PreferPerPixelLighting = false;
			hlsl.FogEnabled = false;
			hlsl.TextureEnabled = false;
			hlsl.VertexColorEnabled = true;

			hlsl.World = hlsl.View = hlsl.Projection = Matrix.Identity;

			glsl = new GLSLEffect(gd, new Dictionary<GLSLPurpose, string>() {
				{ GLSLPurpose.VertexShader  , vsh       },
				{ GLSLPurpose.FragmentShader, fsh_bonzo }
			});
		}
		internal static void Unload() {
			totalElapsed = TimeSpan.Zero;

			if (hlsl != null) {
				hlsl.Dispose();
				hlsl = null;
			}
			if (glsl != null) {
				glsl.Dispose();
				glsl = null;
			}
		}

		public override bool ChooseBgStyle() => true;

		public override void ModifyFarFades(float[] fades, float transitionSpeed) {
			for (int i = 0; i < fades.Length; ++i) {
				if (i == Slot) fades[i] = 1;
				else fades[i] = 0;
			}
		}

		public override bool PreDrawCloseBackground(SpriteBatch sb) {
			var gd = Main.instance.GraphicsDevice;

			// you can mix these with HLSL shaders!
			/*var tech = hlsl.CurrentTechnique = hlsl.Techniques[0];
			for (int i = 0; i < tech.Passes.Count; ++i) {
				tech.Passes[i].Apply();

				gd.DrawUserPrimitives(PrimitiveType.TriangleStrip, vertices_l, 0, 2);
			}*/

			var vp = gd.Viewport;
			glsl.Parameters["fGlobalTime" ].SetValue((float)totalElapsed.TotalSeconds);
			glsl.Parameters["v2Resolution"].SetValue(new Vector2(vp.Width, vp.Height));

			using (var bound = glsl.Bind()) {
				gd.DrawUserPrimitives(PrimitiveType.TriangleStrip, vertices_fs/*r*/, 0, 2);
			}

			return false;
		}

		readonly static VertexType[] vertices_fs = new[] {
			new VertexType(new Vector3(-1, -1, 0), Color.Red   , new Vector2(0, 0))
		,	new VertexType(new Vector3(-1,  1, 0), Color.Blue  , new Vector2(0, 1))
		,	new VertexType(new Vector3( 1, -1, 0), Color.Green , new Vector2(1, 0))
		,	new VertexType(new Vector3( 1,  1, 0), Color.Yellow, new Vector2(1, 1))
		};

		readonly static VertexType[] vertices_r = new[] {
			new VertexType(new Vector3( 0, -1, 0), Color.Red   , new Vector2(0, 0))
		,	new VertexType(new Vector3( 0,  1, 0), Color.Blue  , new Vector2(0, 1))
		,	new VertexType(new Vector3( 1, -1, 0), Color.Green , new Vector2(1, 0))
		,	new VertexType(new Vector3( 1,  1, 0), Color.Yellow, new Vector2(1, 1))
		};
		readonly static VertexType[] vertices_l = new[] {
			new VertexType(new Vector3(-1, -1, 0), Color.Red   , new Vector2(0, 0))
		,	new VertexType(new Vector3(-1,  1, 0), Color.Blue  , new Vector2(0, 1))
		,	new VertexType(new Vector3( 0, -1, 0), Color.Green , new Vector2(1, 0))
		,	new VertexType(new Vector3( 0,  1, 0), Color.Yellow, new Vector2(1, 1))
		};

		readonly static VertexType[] vertices_tr = new[] {
			new VertexType(new Vector3( 0,  0, 0), Color.Red   , new Vector2(0, 0))
		,	new VertexType(new Vector3( 0,  1, 0), Color.Blue  , new Vector2(0, 1))
		,	new VertexType(new Vector3( 1,  0, 0), Color.Green , new Vector2(1, 0))
		,	new VertexType(new Vector3( 1,  1, 0), Color.Yellow, new Vector2(1, 1))
		};
		readonly static VertexType[] vertices_br = new[] {
			new VertexType(new Vector3( 0, -1, 0), Color.Red   , new Vector2(0, 0))
		,	new VertexType(new Vector3( 0,  0, 0), Color.Blue  , new Vector2(0, 1))
		,	new VertexType(new Vector3( 1, -1, 0), Color.Green , new Vector2(1, 0))
		,	new VertexType(new Vector3( 1,  0, 0), Color.Yellow, new Vector2(1, 1))
		};
		readonly static VertexType[] vertices_tl = new[] {
			new VertexType(new Vector3(-1,  0, 0), Color.Red   , new Vector2(0, 0))
		,	new VertexType(new Vector3(-1,  1, 0), Color.Blue  , new Vector2(0, 1))
		,	new VertexType(new Vector3( 0,  0, 0), Color.Green , new Vector2(1, 0))
		,	new VertexType(new Vector3( 0,  1, 0), Color.Yellow, new Vector2(1, 1))
		};
		readonly static VertexType[] vertices_bl = new[] {
			new VertexType(new Vector3(-1, -1, 0), Color.Red   , new Vector2(0, 0))
		,	new VertexType(new Vector3(-1,  0, 0), Color.Blue  , new Vector2(0, 1))
		,	new VertexType(new Vector3( 0, -1, 0), Color.Green , new Vector2(1, 0))
		,	new VertexType(new Vector3( 0,  0, 0), Color.Yellow, new Vector2(1, 1))
		};
	}
}