如何计算像素着色器的深度渲染绘制一个点精灵作为一个球体,将与其他物体相交的圆?球体、作为一个、将与、物体

2023-09-07 22:34:31 作者:越是在乎的人越猜不透

我写一个着色器渲染的点精灵球,通过绘制阴影的圆圈,并且需要以使彼此靠近的领域将正确相交写一个深度分量和颜色。

I am writing a shader to render spheres on point sprites, by drawing shaded circles, and need to write a depth component as well as colour in order that spheres near each other will intersect correctly.

我使用code类似于写的 Johna Holwerda

I am using code similar to that written by Johna Holwerda:

void PS_ShowDepth(VS_OUTPUT input, out float4 color: COLOR0,out float depth : DEPTH)
{
   float dist = length (input.uv - float2 (0.5f, 0.5f)); //get the distance form the center of the point-sprite
   float alpha = saturate(sign (0.5f - dist));
   sphereDepth = cos (dist * 3.14159) * sphereThickness * particleSize; //calculate how thick the sphere should be; sphereThickness is a variable.

   depth = saturate (sphereDepth + input.color.w); //input.color.w represents the depth value of the pixel on the point-sprite
   color = float4 (depth.xxx ,alpha ); //or anything else you might need in future passes
}

在该链接的视频给人的效果,我以后是个好主意:这些画上点精灵球相交正确。我已经添加了下面的图片说明了。

The video at that link gives a good idea of the effect I'm after: those spheres drawn on point sprites intersect correctly. I've added images below to illustrate too.

我可以计算出点精灵本身优良的深度。但是,我不知道展会上的计算出球的厚度在像素,以便将它添加到精灵的深度,使最终的深度值。 (以上code使用一个变量,而不是计算的。)

I can calculate the depth of the point sprite itself fine. However, I am not sure show to calculate the thickness of the sphere at a pixel in order to add it to the sprite's depth, to give a final depth value. (The above code uses a variable rather than calculating it.)

我一直对这个和关闭了好几个星期,但都没有意识到这一点 - 我敢肯定,这很简单,但它的东西,我的大脑还没有twigged

I've been working on this on and off for several weeks but haven't figured it out - I'm sure it's simple, but it's something my brain hasn't twigged.

的Direct3D 9的点精灵大小的计算以像素为单位,而我的精灵有几个大小 - 由于距离都是由衰减(我实现了的