I have a custom template, and I want to somehow take the output of ContentPresenter (imagine it as a bitmap), now strip RGB from that bitmap (so only alpha channel remains), and then set RGB on every pixel to white (preserve alpha channel). So how would you do that?
I'd use a PixelShader + Effect on the ContentPresenter if it needs to be a 'live' effect.
See the Shazzam tool to easily create the sources for the effect and the PixelShader.
sampler2D Texture1Sampler : register(S0);
//-----------------
// Pixel Shader
//-----------------
float4 main(float2 uv : TEXCOORD) : COLOR
{
float4 color = tex2D( Texture1Sampler, uv );
float4 alphaMaskColor = float4(color.a,color.a,color.a,color.a); //Pre-multiplied Alpha in WPF
return alphaMaskColor;
}
Related
I'm creating simple google cardboard game in unity and i want to add score count. I know how to do it normally, but in VR it doesn't work for me. I tried to just put Text and display score in it, and it looked good(not exactly because it was displayed only for one eye) but after I tested it on my phone text was in completely different place. Do you know what's the proper way to do it?
Create a World Space canvas and have that canvas attached to the camera. Thats how I get my UI in VR games.
You're going to need a custom shader for your World Space UI objects that has it's Render Order to Overlay and has ZTest turned off.
This is a copy of the Default UI shader with the necessary changes. Should do the trick. Just make a material with this shader, and apply it to everything you want drawn over the top of geometry in your WorldSpace UI.
Shader "UI/Default_OverlayNoZTest"
{
Properties
{
[PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
_Color ("Tint", Color) = (1,1,1,1)
_StencilComp ("Stencil Comparison", Float) = 8
_Stencil ("Stencil ID", Float) = 0
_StencilOp ("Stencil Operation", Float) = 0
_StencilWriteMask ("Stencil Write Mask", Float) = 255
_StencilReadMask ("Stencil Read Mask", Float) = 255
_ColorMask ("Color Mask", Float) = 15
}
SubShader
{
Tags
{
"Queue"="Overlay"
"IgnoreProjector"="True"
"RenderType"="Transparent"
"PreviewType"="Plane"
"CanUseSpriteAtlas"="True"
}
Stencil
{
Ref [_Stencil]
Comp [_StencilComp]
Pass [_StencilOp]
ReadMask [_StencilReadMask]
WriteMask [_StencilWriteMask]
}
Cull Off
Lighting Off
ZWrite Off
ZTest Off
Blend SrcAlpha OneMinusSrcAlpha
ColorMask [_ColorMask]
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata_t
{
float4 vertex : POSITION;
float4 color : COLOR;
float2 texcoord : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
fixed4 color : COLOR;
half2 texcoord : TEXCOORD0;
};
fixed4 _Color;
v2f vert(appdata_t IN)
{
v2f OUT;
OUT.vertex = mul(UNITY_MATRIX_MVP, IN.vertex);
OUT.texcoord = IN.texcoord;
#ifdef UNITY_HALF_TEXEL_OFFSET
OUT.vertex.xy += (_ScreenParams.zw-1.0)*float2(-1,1);
#endif
OUT.color = IN.color * _Color;
return OUT;
}
sampler2D _MainTex;
fixed4 frag(v2f IN) : SV_Target
{
half4 color = tex2D(_MainTex, IN.texcoord) * IN.color;
clip (color.a - 0.01);
return color;
}
ENDCG
}
}
}
credit goes to http://answers.unity3d.com/questions/878667/world-space-canvas-on-top-of-everything.html
I'd say; setup a RenderTexture and make your/the UI the only thing drawn onto it, then stick the RenderTexture onto something else in your world
I am very new to HLSL to please bear with me...
This is the effect file:
sampler s0;
texture tex;
sampler tex_sampler = sampler_state{Texture = tex;};
float4 PixelShaderFunction(float2 coords: TEXCOORD0) : COLOR0
{
float4 color = tex2D(s0, coords);
float4 tex_color = tex2D(tex_sampler, coords);
if (tex_color.a)
{
// SOMETHING GOES RIGHT HERE BUT I DON'T KNOW WHAT
return color;
}
return tex_color;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
The "tex" field is the mask.
The "tex_sampler" is the sampler for the mask.
The result contains either pixels with 1 or 0 alpha, but the mask is actually blurred and contains different alpha values. What should I add or change to make this do what I want? This works completely well otherwise.
I belive you need to set renderstates in your technique.
AlphaBlendEnable = true;
SrcBlend = SrcAlpha;
DestBlend = InvSrcAlpha;
I'm creating an image editor for Windows Phone 8 / Windows 8 using SharpDX api and trying to apply a custom effect (SharpDX.Toolkit.Graphics.Effect) to a WriteableBitmap.
The effect itself is applied, but the result looks strange. Seems to me that the texture was reduced and inverted.
The code to apply the effect is written below.
private void ApplyEffect()
{
GraphicsDevice graphicsDevice = GraphicsDevice.New();
RenderTarget2D renderTarget = RenderTarget2D.New( graphicsDevice, _originalBitmap.PixelWidth, _originalBitmap.PixelHeight, PixelFormat.B8G8R8A8.UNorm );
Texture2D texture = Texture2D.New( graphicsDevice, _originalBitmap.PixelWidth, _originalBitmap.PixelHeight, PixelFormat.B8G8R8A8.UNorm );
texture.SetData<int>( _originalBitmap.Pixels );
Effect effect = new Effect( graphicsDevice, SimpleEffectTest.Effects.Invert.EffectBytecode );
graphicsDevice.SetRenderTargets( renderTarget );
SpriteBatch spriteBatch = new SpriteBatch( graphicsDevice );
spriteBatch.Begin( SpriteSortMode.Deferred, effect );
spriteBatch.Draw( texture, Vector2.Zero, Color.White );
spriteBatch.End();
renderTarget.GetData<int>( _resultBitmap.Pixels );
}
Do you know what I'm doing wrong here?
SimpleEffectTest project
Best regards,
Pieter Voloshyn
---- UPDATE ----
After many tests I finally found what I did wrong. I changed the main pixel shader function parameters.
From
float4 PS(
float4 pos : SV_POSITION,
float4 posScene : SCENE_POSITION,
float2 texCoord : TEXCOORD0
) : SV_TARGET0
To
float4 PS(
float4 color : COLOR0,
float2 texCoord : TEXCOORD0
) : SV_Target
Thanks!
I have created two RenderTarget2Ds:
currentWorldState = new RenderTarget2D(graphicsDevice, width, height, false, SurfaceFormat.Vector4, DepthFormat.None);
nextWorldState = new RenderTarget2D(graphicsDevice, width, height, false, SurfaceFormat.Vector4, DepthFormat.None);
I am trying to write simulation code on the GPU. My goal is to be able to pass vector4 information between these rendertargets. I have created a shader that reads information from the first target, and outputs it to the next target. My problem is that the float4 gets converted into color information and a lot of precision is lost.
This is the header to my pixel shader:
float4 CreateActions(in float2 uv : TEXCOORD0) : COLOR
Can I change this in any way to ouput the float4 data, changing COLOR to anything else doesn't compile.
You have to change the render target format, this is what determines precision :
Shawn Heargrave wrote about it and here is the SurfaceFormat documentation
i got 2 dynamic texture ,and want add second texture color to first texture color But just where first texture color alpha is not 0
something like inverse transparncey
i add two pic link to show what is my mean:
TO
just collisions part must add two texture pixel color
ty for your help
Maybe using BlendState.Additive wil be enough for you.
or maybe it can be achieved with a custom BlendState.. but I have not experience with this...
or you can make a shader, you should note that you have to quads:
Quad with a rag doll. (Qrd)
Quad with a circle. (Qc)
you draw Qc over Qrd...
so you have to traduce the texture coordinates that you get in the pixel shader that owns to Qc to texture cordinates at Qrd space...
then you sample the color from Qrd texture,
and if alpha is near zero you clip the pixel...
else you return the sample from Qrc texture
just did it , works great
sampler circleSampler : register(s1);
sampler playerSampler : register(s0);
float4 main(float4 color : COLOR0 ,float2 texCoord : TEXCOORD0):COLOR0
{
float4 output = float4(1,1,1,1);
float4 CircColor = tex2D(circleSampler,texCoord);
float4 playerColor = tex2D(playerSampler,texCoord);
if (CircColor.a ==0)
{
output = playerColor;
}
else
{
output = CircColor* playerColor;
}
output.a = playerColor.a;
return output;
}
technique Technique1
{
pass Pass1
{
PixelShader = compile ps_2_0 main();
}
}
anyway ty for ur time