- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
摘要和目标:
我正在尝试制作一个着色器来为游戏编辑器执行简单的窗口效果。该效果将绘制一个具有低值边框颜色和高值突出显示的框架。我尝试了很多方法,但总的来说,我只提出了一种可能的解决方案来使用 GPU 实现这一目标。
首先,我创建了一个自定义顶点类型,用于在屏幕空间中存储向量的 XY 坐标。
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace WindowsGame1 {
public struct Vertex2D : IVertexType {
public Vector2 Position;
public static readonly VertexDeclaration VertexDeclaration = new VertexDeclaration(new VertexElement(0, VertexElementFormat.Vector2, VertexElementUsage.Position, 0));
public Vertex2D(float x, float y) {
Position = new Vector2(x, y);
}
VertexDeclaration IVertexType.VertexDeclaration {
get {
return VertexDeclaration;
}
}
}
}
接下来我创建一个自定义窗口类的实例。构造函数设置顶点缓冲区并设置效果中的 View 、投影和颜色参数。
这是效果文件。
float4x4 view;
float4x4 projection;
float4 color;
float shadowPercent = 0.36893203883495145631067961165049;
float highlightPercent = 1.262135922330097087378640776699;
Texture2D targetTexture;
struct FillVertexShaderInput {
float4 position : POSITION0;
};
struct FillPixelShaderInput {
float4 position : POSITION0;
};
struct BorderPixelShaderInput {
float4 position : SV_Position;
};
// Transforms color component range from 0-255 to 0-1.
float4 ClampColor(float4 color) {
return float4(color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255);
}
// Shifts the value of a color by a percent to get border color and highlight color from a fill color.
float4 ShiftValue(float4 color, float percent) {
return float4(clamp(color[0] * percent, 0, 1), clamp(color[1] * percent, 0, 1), clamp(color[2] * percent, 0, 1), clamp(color[3] * percent, 0, 1));
}
FillPixelShaderInput FillVertexShader(FillVertexShaderInput input) {
FillPixelShaderInput output;
output.position = mul(mul(input.position, view), projection);
return output;
}
float4 FillPixelShader(FillPixelShaderInput input) : COLOR0 {
return color;
}
float4 BorderPixelShader(BorderPixelShaderInput input) : COLOR0 {
// Get color of pixel above?
// float4 tempColor = texture.Sample(sampler, (input.position[0], input.position[1] - width));
return color;
}
technique Frame {
// Store Texture2D, sampler2D, and others to be stored between passes?
/*Texture2D texture;
sampler2D sampler;
float width;
float height;
texture.GetDimensions(width, height);
color = ClampColor(color);
float4 shadowColor = ShiftValue(color, shadowPercent);
float4 highlightColor = ShiftValue(color, highlightPercent);*/
pass Fill {
VertexShader = compile vs_2_0 FillVertexShader();
PixelShader = compile ps_2_0 FillPixelShader();
}
pass Border {
PixelShader = compile ps_4_0 BorderPixelShader();
}
}
我希望能够在传递之间存储数据,但我不知道这是否可行,所以我尝试在 XNA 中存储渲染目标并将其用作下一次传递的参数。
这里是Window的Draw代码。
public void Draw(Game1 game) {
// rectangle is a simple window for this test.
RenderTarget2D target = new RenderTarget2D(game.GraphicsDevice, rectangle.Width, rectangle.Height);
game.GraphicsDevice.SetRenderTarget(target);
game.GraphicsDevice.BlendState = BlendState.AlphaBlend;
game.GraphicsDevice.Clear(Color.Transparent);
game.GraphicsDevice.SetVertexBuffer(vertexbuffer);
effect.Techniques["Frame"].Passes["Fill"].Apply();
game.GraphicsDevice.DrawPrimitives(PrimitiveType.TriangleStrip, 0, 2);
game.GraphicsDevice.SetRenderTarget(null);
effect.Parameters["targetTexture"].SetValue(target);
effect.Techniques["Frame"].Passes["Border"].Apply();
}
如果我能在像素着色器中获取当前像素周围像素的位置和颜色,我就能确定在当前位置绘制什么颜色的像素。填充工作正常。我只是不知道绘制边框阴影和高光的最佳方法。我也遇到了 alpha 混合问题。除了窗口之外的所有内容都是默认的深紫色,即使我设置了 alpha 混合并将渲染目标缓冲区清除为透明。
如果您决定提供帮助,在此先感谢您。
最佳答案
我相信它是这样工作的:
PixelShaderOutput PixelShaderFunction(VertexShaderOutput input, float2 vPos : VPOS)
{
// shader code here
}
然后您可以只使用vPos.x
和vPos.y
来访问当前正在处理的像素的坐标。
关于c# - HLSL 和 XNA : How do I get the position and color of surrounding pixels of the current pixel shader iteration?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13945582/
我刚开始使用 html5 和 css,我对使用 css position 有点困惑。 我可以从堆栈溢出中找到这些链接, Difference between relative and absolute
我想知道是否有一种方法可以获得相对定位的元素的“绝对位置”。我想不出任何方法,但也许有人想出了一个方法...... 最佳答案 我不确定“绝对位置”是什么意思,但您可以使用 el.getBounding
基本上我的 HTML(使用 Bootstrap 类)中有一个页面,其中包含使用 thumbnail 和 caption 类展示的产品。在产品图片上方有一个 ON SELL(圆形红色标签)。我给这个红色
当我使用 BaseAdapter 的 ListView 离开屏幕时,每一行不再保持连续的位置。除了这个我不知道还能怎么解释。 如果我的 BA/LV 在屏幕上显示 4 个项目,并且我添加了一个显示每一行
此 HTML 无法在 IE8 中正确呈现。它适用于 Firefox、Opera、IE7、IE9 和 IE10。 (当然,它在 IE6 中也不起作用,因为它涉及固定位置的元素。) 实际上它在 Chrom
我正在使用Position Absolute的表单验证引擎。 我有一个特定的验证案例,希望可以为我解决。我需要能够根据另一个字段的值来使一个字段成为必需。 例如: If country.dropdow
我看过很多帖子,人们希望将相机位置调整到屏幕位置。我的问题是如何做相反的事情。 我目前想要实现的是将“门”位置设置为屏幕的百分比,此计算已准备就绪,并且我确实有最终屏幕的 X、Y (px) 位置。相机
谁能解释一下使用 position:relative、position:absolute 和 float 对正常文档流及其子项的影响的区别? 例如当我有三个元素 A、B、C 时,其中 A 是包含 B
我有以下代码: .tabs{ position: fixed; top:110px; left:0px; width:40px; z-inde
我不知道如何表达这个问题。 所以这是一个例子。 图层上的动画滑入 View ,然后稍微弹回。 十分简单。 位置关键帧看起来像这样: 关键帧 0:(-100,200) 关键帧 10:(100,200)
我是 XSLT 的新手,我正在使用 w3schools 的“自己尝试”页面进行一些测试。我正在使用以下演示: http://www.w3schools.com/xsl/tryxslt.asp?xmlf
我试图让一个对象 1 (SKSpriteNode) 在某个高度(y 坐标)处创建另一个对象 (object2)。我已经得到了我需要的所有代码,除了我不知道如何正确编写: object1.positio
.special p { display:none; } .special:hover p { display:block; } Things
当将一个绝对定位元素放置在一个位置固定包装器中时,在一个位置相对包装器中,绝对元素位置不应该相对于相对 parent 而不是固定 parent ? Content
我有一个 iframe,它在应用 css 后显示谷歌地图 position: relative; top: 48px; left: -233px; 但是当我向下滚动时,它刚好出现在显示 khaleej
我在一个网站上工作,有一个带有 position:relative 的父元素,它有一个带有 position:absolute 的子元素。令我感到奇怪的是,对于我提到的位置,子元素似乎仍然可以识别它们
我有一个 main 元素,我已将其设置为 position: relative。这包含两个 div,然后我在其上应用 position: absolute。然后,这会导致夹在 main 元素中间的页眉
HTML 代码由一个 div 组成,它具有两种类型的类:“隐藏”和“保留”。 Lorem ipsum dolor sit amet, consectetur adipis
我必须开发一个生成表格(有点……)的软件,并且必须提供对元素位置的完全控制。 到目前为止,我们有这样的构造: 还有一些javascript代码来放置#a , #b和 #c #co
我尝试将 z-index 设置为 body 下的某些位置为 fixed 的元素 示例如下: HTML menu content ....
我是一名优秀的程序员,十分优秀!