How to change Texture coordinates in MGFX Shaders in Monogame - xna

I'm trying to change texture coordinates from Effect class in Monogame, how can I do that?
this is code in C#:
lighting.CurrentTechnique = lighting.Techniques["LightDrawing"]
lighting.Parameters["RenderTargetTexture"].SetValue(_melttown[0].Texture);
lighting.Parameters["MaskTexture"].SetValue(_lightMask[0].Texture);
this is MGFX code:
float4 MainPS(float2 textureCoords : TEXCOORD0) : COLOR
{
float4 pixelColor = tex2D(RenderTargetSampler, textureCoords);
float4 lightColor = tex2D(MaskSampler, textureCoords);
return pixelColor * lightColor;
}
technique LightDrawing
{
pass P0
{
PixelShader = compile PS_SHADERMODEL MainPS();
}
};
I tried to change Texture Coordinates and Light will be moving on the screen

Related

what is the 'alpha' value of pixel shader?

hi there day i am in process to make 2d game using directx11 api.
and it come to point that i need to use transparent effect.
so i have a green background and one footprint on middle.
and simply without setting anything but alpha value of returning color in pixel shader, i made a bit of success, but the problem is that it doesn't work for white color.
this is Pixel Shader code
cbuffer CB_TRANSPARENCY : register(b0)
{
float tp;
};
Texture2D footprint : register(t0);
SamplerState samplerState : register(s0);
struct PS_INPUT
{
float4 pos : SV_POSITION;
float2 tex : TEXCOORD;
};
float4 main(PS_INPUT input) : SV_Target
{
float3 texColor = footprint.Sample(samplerState, input.tex).xyz;
return float4(texColor, tp);
}
it there something that i miss?
or should i use some blendingstate thing?
any help would be appreciated
[edit] here's something to edit. actually alpha value doesn't do anything without blending setting. just one variable to be used for any custom calculation.
In my project, i was using spritebathch,spritefont class for rendering font on screen,
So i guess in spritebatch class, there might be blendingState under the hood that blend black color, so that i have got this effect without setting my blendingState.
Yes, you need to create a blend state with appropriate alpha processing mode and then make sure that created blend state is attached to output merging stage of the rendering pipeline prior to drawing:
D3D11_BLEND_DESC blendStateDesc{};
blendStateDesc.AlphaToCoverageEnable = FALSE;
blendStateDesc.IndependentBlendEnable = FALSE;
blendStateDesc.RenderTarget[0].BlendEnable = TRUE;
blendStateDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
blendStateDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_SRC_ALPHA;
blendStateDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_DEST_ALPHA;
blendStateDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
blendStateDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
if(not SUCCEEDED(p_device->CreateBlendState(&blendStateDesc, &blendState)))
{
std::abort();
}
p_device_context->OMSetBlendState(blendState, nullptr, 0xFFFFFFFF);
//draw calls...

HLSL Variable Returning Zero To MonoGame

To improve performance, I offloaded some CPU tasks onto the GPU with Effects. This is what my HLSL code looks like:
float angle;
extern float2 direction;
float4 PixelShaderFunction(float4 pos : SV_POSITION, float4 color : COLOR0, float2 coords : TEXCOORD0) : COLOR
{
angle = atan2(direction.x, -direction.y);
return float4(1, 1, 1, 1);
}
technique DefaultTechnique
{
pass Pass1
{
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
In the Emitter class, I set the direction, apply the technique, and then retrieve the "angle" variable:
gpu.SetValue("direction", particles[i].Velocity);
for (int i = 0; i < effect.CurrentTechnique.Passes.Count; i++)
effect.CurrentTechnique.Passes[i].Apply();
particles[i].Angle = gpu.RetrieveFloat("angle");
This runs fine with no crashing. However, the "angle" value is always 0. My HLSL skills aren't great, but the code looks like it should work as expected.
Any suggestions are appreciated.

Adding bloom (glow) effect to shader (Unity game engine)

I am creating iOS app in Unity game engine.
I am trying to rewrite my shader so that the material, witch using it, gave Bloom (Glow) effect (like a Halo component).
An example of how it should look:
I really searched answer in the internet but did not find anything for the worker or of suitable solution to my problem.
My shader's code:
Shader "Unlit"
{
Properties
{
_MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
_Color("Main Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
LOD 100
Cull off
ZWrite on
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
Lighting Off
SetTexture[_MainTex]
{
constantColor[_Color]
Combine texture * constant, texture * constant
}
}
}
}
A shader program is just code used to draw triangles on the screen. Bloom effects are a completely different beast, and are screen-space calculations that are done after the geometry is drawn. You won't get a bloom effect just by modifying the object's shader.
Simply speaking, with a shader you can never draw "outside the lines", and here you need to alter pixels that are outside the object's reach. I'm sorry, but it's just not within the shader's capabilities.
Still, you can make it work by implementing Image Effect scripts, like unity's built-in bloom effect. Once the script is added to the camera, and the camera's HDR setting is activated, you can use a special shader that will result in a glow, but not before all that.
Once you have set up the effect correctly (and enable the HDR option on the camera), you can now use any shader that returns values greater than 1 in the pixel shader to generate a glow effect around the object. The shader you posted is a legacy shader program. Here's the updated code, with a Glow multiplier included:
Shader "Glow" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_Glow ("Intensity", Range(0, 3)) = 1
}
SubShader {
Tags { "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
LOD 100
Cull Off
ZWrite On
Blend SrcAlpha OneMinusSrcAlpha
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
sampler2D _MainTex;
half4 _MainTex_ST;
fixed4 _Color;
half _Glow;
struct vertIn {
float4 pos : POSITION;
half2 tex : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
half2 tex : TEXCOORD0;
};
v2f vert (vertIn v) {
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.pos);
o.tex = v.tex * _MainTex_ST.xy + _MainTex_ST.zw;
return o;
}
fixed4 frag (v2f f) : SV_Target {
fixed4 col = tex2D(_MainTex, f.tex);
col *= _Color;
col *= _Glow;
return col;
}
ENDCG
}
}
}

alpha value captured by camera in ios

i am trying to make a fake shadow by using RenderTexture
i dynamically create a camera, and assign a RenderTexture to the camera as render target.
the format of the RenderTexture is set to ARGB32
then i use the RenderTexture as texture on a plane with a custom shader to change the color to black and adjust the alpha value so the shadow can be properly shown
below is the shader im using:
Shader "Fake Shadow" {
Properties {
_ShadowTex ("Fake Shadow", 2D) = "white" { TexGen ObjectLinear }
_SAlpha ("Shadow Intensity", float) = 0.35
}
Category {
Tags { "Queue" = "Transparent-1" }
Lighting Off
ZWrite On
Cull Back
Blend SrcAlpha OneMinusSrcAlpha, one one
Subshader {
LOD 200
Pass {
SetTexture[_ShadowTex] {
ConstantColor(0,0,0,[_SAlpha])
matrix [_ProjMatrix]
Combine texture * constant, texture * constant
}
}
}
}
}
things are normal when i test play in Unity3D editor and on Android
however, the result is not correct in ios
the result image is in the link below (sorry i cant post image yet)
shadow in editor
shadow in ios
it seems ios put alpha as 1 in the RenderTexture even on pixels which the camera captured nothing
i have found a solution to solve this problem which is to check 32-bit display in the player settings
but im worried that checking 32-bit display will affect the performance
is there any other solution without checking 32-bit display?
I'm doing the same thing (camera and projector for emulating shadows), and I use a shader based on the one on this page: http://en.wikibooks.org/wiki/Cg_Programming/Unity/Projectors
The shader follows:
Shader "Cg projector shader for drop shadows" {
Properties {
_ShadowTex ("Projected Image", 2D) = "white" {}
_ShadowStrength ("Shadow Strength", Float) = 0.65
}
SubShader {
Pass {
// Blend Off
Blend Zero OneMinusSrcAlpha // attenuate color in framebuffer
// by 1 minus alpha of _ShadowTex
// fb_color = black + fb_color * OneMinusShadowAlpha;
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// User-specified properties
uniform sampler2D _ShadowTex;
uniform float _ShadowStrength;
// Projector-specific uniforms
uniform float4x4 _Projector; // transformation matrix
// from object space to projector space
struct vertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct vertexOutput {
float4 pos : SV_POSITION;
float4 posProj : TEXCOORD0;
// position in projector space
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.posProj = mul(_Projector, input.vertex);
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
return output;
}
float4 frag(vertexOutput input) : COLOR
{
if (input.posProj.w > 0.0) // in front of projector?
{
return tex2D(_ShadowTex ,
float2(input.posProj) / input.posProj.w) * _ShadowStrength;
}
else // behind projector
{
return float4(0.0);
}
}
ENDCG
}
}
// The definition of a fallback shader should be commented out
// during development:
// Fallback "Projector/Light"
}

Shader including another shader?

Is it possible, using XNA 4, to include a Shader within another shader? I know you could do this within 3.1, but I seem to be having trouble getting this to work? If you can, any pointers would be great.
EDIT
//---------------------------------------------------------------------------//
// Name : Rain.fx
// Desc : Rain particle effect using cylindrical billboards
// Author : Justin Stoecker. Copyright (C) 2008-2009.
//---------------------------------------------------------------------------//
#include "common.inc" // It's this line that causes me a problem
float4x4 matWorld;
float3 vVelocity;
float3 vOrigin; // min point of the cube area
float fWidth; // width of the weather region (x-axis)
float fHeight; // height of the weather region (y-axis)
float fLength; // length of the weather region (z-axis)
... Rest of file ...
The "common.inc" file has variables in there, but I was wondering if you could put methods in there as well?
Yes it's possible, from memory I think the basic effect example shader example from the MS App Hub does it.
In any case, see code below!
In FractalBase.fxh
float4x4 MatrixTransform : register(vs, c0);
float2 Pan;
float Zoom;
float Aspect;
float ZPower = 2;
float3 Colour = 0;
float3 ColourScale = 0;
float ComAbs(float2 Arg)
{
}
float2 ComSquare(float2 Arg)
{
}
int GreaterThan(float x, float y)
{
}
float4 GetColour(int DoneIterations, float MaxIterations, float BailoutTest, float OldBailoutTest, float BailoutFigure)
{
}
void SpriteVertexShader(inout float4 Colour : COLOR0,
inout float2 texCoord : TEXCOORD0,
inout float4 position : SV_Position)
{
position = mul(position, MatrixTransform);
// Convert the position into from screen space into complex coordinates
texCoord = (position) * Zoom * float2(1, Aspect) - float2(Pan.x, -Pan.y);
}
In FractalMandelbrot.fx
#include "FractalBase.fxh"
float4 FractalPixelShader(float2 texCoord : TEXCOORD0, uniform float Iterations) : COLOR0
{
}
technique Technique1
{
pass
{
VertexShader = compile vs_3_0 SpriteVertexShader();
PixelShader = compile ps_3_0 FractalPixelShader(128);
}
}
#includes work like this:
The preprocessor loads your main .fx file, and parses it, looking for anything that starts with a #. #includes cause the preprocessor to load the referenced file and insert its contents into the source buffer. Effectively, your #include directive is replaced by the entire contents of the included file.
So, yes, you can define anything in your #includes that you can define in a regular .fx file. I use this for keeping lighting functions, vertex type declarations, etc in common files that are used by several shaders.

Resources