179 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
			
		
		
	
	
			179 lines
		
	
	
		
			6.2 KiB
		
	
	
	
		
			Plaintext
		
	
	
	
| // Source Shader Example
 | |
| //
 | |
| // Check back here on new plugin versions for new features.
 | |
| //
 | |
| // This example explains the necessary basics to get started with custom shaders,
 | |
| // however it does not teach you HLSL. You will have to learn that yourself, and
 | |
| // no support is given for HLSL issues - either pay an artist, or learn it your-
 | |
| // self.
 | |
| //
 | |
| // Some uniform variables are automatically provided by StreamFX, these are:
 | |
| // * float4x4 ViewProj: View Projection Matrix
 | |
| // * float4 Time:
 | |
| //   [0] is current time,
 | |
| //   [1] is frame time,
 | |
| //   [2] is 24 hour system time,
 | |
| //   [3] is a random value that changes each frame.
 | |
| // * matrix4x4 Random: 16 random values that change each frame.
 | |
| // * float4 ViewSize:
 | |
| //   [0..1] View Size (1 / VS = 1 pixel move)
 | |
| //   [2..3] Inverse View Size (1 * IVS = 1 pixel move)
 | |
| // * texture2d ImageA: (First) input image for filters and transitions.
 | |
| // * texture2D ImageB: Second input image for transitions.
 | |
| //
 | |
| // Custom Shaders make heavy use of annotations, which can be used to essentially
 | |
| // describe a complete shader with just its own file, requiring no outside input.
 | |
| // There are various annotations that StreamFX reads from, and some are situational,
 | |
| // so it is best if I show some examples.
 | |
| //
 | |
| // ---------- Basic Uniform Example ----------
 | |
| //    uniform bool UniqueParameterName<
 | |
| // This defines a parameter of type float with the unique key "UniqueParameterName",
 | |
| // which is used to access it in a script as "Shader.Parameters.UniqueParameterName".
 | |
| //
 | |
| //        bool visible = false;
 | |
| // [OPTIONAL] The boolean annotation 'visible' allows hiding a parameter from the user,
 | |
| // but still allowing scripts to change it. Limits are ignored, but scale is not.
 | |
| //
 | |
| //        bool automatic = true;
 | |
| // [OPTIONAL] Prevents parameters from being shown as well as being modified by scripts,
 | |
| // and overrides visible if set to true. For seeded or given-by-default parameters this
 | |
| // must be set to true or their state is not guaranteed and may be nonsense.
 | |
| //
 | |
| //        string name = "Hello World";
 | |
| // [OPTIONAL] Overrides the visible name of a parameter with your own better string.
 | |
| // The maximum length is 256 characters, but you really should keep it shorter than 40.
 | |
| //
 | |
| //        string description = "This is an example";
 | |
| // [OPTIONAL] Sets the description of a parameter that shows up when hovering over it.
 | |
| //
 | |
| //        string type = "float";
 | |
| // [OPTIONAL] Overrides the detected type with a specified one, which is useful for some
 | |
| // functionality trickery and even array types. Must not differ in byte-size from the
 | |
| // intended type. Best left out unless you absolutely need it. Valid types are:
 | |
| //   "bool", "float", "int", "string", "texture", "sampler"
 | |
| //
 | |
| //        int size = 1;
 | |
| // [OPTIONAL/REQUIRED] Optional on detected types, required on arrays or unknown types.
 | |
| // Overrides the detected size with your own, which may at most be 32 elements (4x8 matrix).
 | |
| //    >;
 | |
| //
 | |
| // ---------- Float/Int Uniform Example ----------
 | |
| //    uniform float UniqueParameterName<
 | |
| // In addition to the basic parameters, Floats and Ints have additional annotations that are being read.
 | |
| //
 | |
| //        string field_type = "input";
 | |
| // [OPTIONAL] Changes the visible type of the field to be something else.
 | |
| // Possible values:
 | |
| // * "input": The default input method, a simple field with limits and stepping.
 | |
| // * "slider": A slider, also with limits and stepping.
 | |
| // * "enum": Enumeration that holds only selected data.
 | |
| //
 | |
| //        string suffix = "";
 | |
| // [OPTIONAL] Suffix to attach to the value in the UI, for example " px" for
 | |
| // pixels. Does not work with enumerations.
 | |
| //
 | |
| //        float minimum = 0.0;
 | |
| // [OPTIONAL] Minimum possible value, must always be the same type as the uniform.
 | |
| //
 | |
| //        float maximum = 1.0;
 | |
| // [OPTIONAL] Maximum possible value, must always be the same type as the uniform.
 | |
| //
 | |
| //        float step = 0.01;
 | |
| // [OPTIONAL] Step value for each increase/decrease. Values lower than 0.01 may
 | |
| // not be supported.
 | |
| //
 | |
| //        float scale = 1.0;
 | |
| // [OPTIONAL] Defines the scaling used when assigning the value to the shader.
 | |
| // For example a UI value of 100.0 can be scaled to 1.0 this way.
 | |
| //
 | |
| //        int values<
 | |
| // [REQUIRED if "enum"] Contains the values for an enumeration and holds the
 | |
| // number of enumeration entries actually available.
 | |
| //
 | |
| //            string _0<
 | |
| //                float value = 1.0;
 | |
| //            > = "Entry 1";
 | |
| // Defines the first enumeration entry with the visible name "Entry 1" and the value 1.0.
 | |
| //
 | |
| //            string _1<
 | |
| //                float value = 0.0;
 | |
| //            > = "Entry 2";
 | |
| // Defines the second enumeration entry with the visible name "Entry 2" and the value 0.0.
 | |
| //
 | |
| //        > = 2;
 | |
| // [REQUIRED if "enum"] The value of this annotation must be the number of enumeration entries.
 | |
| //
 | |
| //    >;
 | |
| //
 | |
| // ---------- Float4/Int4 Uniform Example ----------
 | |
| //    uniform float4 UniqueParameterName<
 | |
| // There aren't many difference to the standard float/int parameter, but you can
 | |
| // specify limits, stepping and scale per element. Enumerations also are per
 | |
| // element so their type must be the base type (float for float#, int for int#).
 | |
| //
 | |
| //        float4 minimum = {0.0, 1.0, 0.5, -0.5};
 | |
| // Each element now has different minimum values which ...
 | |
| //
 | |
| //        float4 maximum = {1.0, 0.0, 0.6, 0.0};
 | |
| // ... also works for maximum, ...
 | |
| //
 | |
| //        float4 step = {0.01, 0.01, 0.1, 0.5};
 | |
| // ... stepping and ...
 | |
| //
 | |
| //        float4 scale = {100.0, 0.01, 3.14, 1283828.0};
 | |
| // ... scaling values. 
 | |
| //
 | |
| //    >;
 | |
| 
 | |
| // Always provided by OBS
 | |
| uniform float4x4 ViewProj<
 | |
| 	bool automatic = true;
 | |
| >;
 | |
| 
 | |
| // Provided by StreamFX
 | |
| uniform float4 Time<
 | |
| 	bool automatic = true;
 | |
| >;
 | |
| 
 | |
| // ---------- Shader Code
 | |
| sampler_state def_sampler {
 | |
| 	AddressU  = Wrap;
 | |
| 	AddressV  = Wrap;
 | |
| 	Filter    = Linear;
 | |
| };
 | |
| 
 | |
| struct VertData {
 | |
| 	float4 pos : POSITION;
 | |
| 	float2 uv  : TEXCOORD0;
 | |
| };
 | |
| 
 | |
| struct FragData {
 | |
| 	float4 pos : POSITION;
 | |
| 	float2 uv  : TEXCOORD0;
 | |
| };
 | |
| 
 | |
| FragData VSDefault(VertData v_in) {
 | |
| 	FragData vert_out;
 | |
| 	vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);
 | |
| 	vert_out.uv  = v_in.uv;
 | |
| 	return vert_out;
 | |
| }
 | |
| 
 | |
| float4 PSTime(FragData v_in) : TARGET {
 | |
| 	return float4(
 | |
| 		cos(Time[0] * 5.) * 0.5 + 0.5,
 | |
| 		cos(Time[0] * 0.) * 0.5 + 0.5,
 | |
| 		cos(Time[0] * 0.) * 0.5 + 0.5,
 | |
| 		1.0);
 | |
| }
 | |
| 
 | |
| technique Draw
 | |
| {
 | |
| 	pass
 | |
| 	{
 | |
| 		vertex_shader = VSDefault(v_in);
 | |
| 		pixel_shader  = PSTime(v_in); 
 | |
| 	}
 | |
| }
 |