软粒子

1649 篇文章 11 订阅
1623 篇文章 22 订阅

SoftParticles Sample

软粒子实例

Path

路径

Source:

SDK root/Samples/C++/Direct3D10/SoftParticles

Executable:

SDK root/Samples/C++/Direct3D10/Bin/platform/SoftParticles.exe

Sample Overview

实例总览

Particle systems are a common method for creating volumetric effects in games. They can be used to simulate such things as fire, cloudes, smoke, dust, glowing projectiles, magic spells, and so on. A common technique is to use 2D camera-aligned quads centered at each particle to represent the volume that the particle represents. Because the quads follow the camera, they give the illusion of a substance filling a 3D volume. However, this illusion often breaks down when the 2D sprites used to visualize the particles intersect with the world geometry. The intersection of the 2D quad with the 3D world geometry creates a hard, straight line — on one side of the line is particle, and on the other is world geometry. Figure 1 shows an example of this.

粒子系统是在游戏中建立体积效果的一般方法。他们可以被用来模拟像火,云,烟雾,尘土,流体,魔法等等。一般的方法是使用2D矩形用粒子的方式显示体积效果。这些2D矩形是位于每个粒子的中心。由于这些矩形是跟随摄像机的,所以利用它们就像幻觉一样就描绘出了3D体积效果。尽管如此,当使用2D精灵显示出的粒子和某些几何体相交的时候,这种幻觉仍然可能被破坏。这种2D矩形和3D几何体的交叉产生一种生硬,奇怪的线——线的一边是粒子,另一边是几何体。图例1显示这种情况。

Figure 1.  Hard, Flat Particles

图例1,生硬,平坦的粒子

2D Particles with Depth

The first approach to dealing with this is to perturb the depth being output from the pixel shader by a depth stored in the particle texture. This makes the intersections with the world geometry follow the contour of the particle. Figure 2 shows an example of this.

解决这个问题的第一个办法是是改变深度。这个深度由像素着色器根据储存在粒子纹理中的深度生成。这种方法可以使粒子与几何体的交叉是在粒子轮廓上产生的。图例2显示了这样的一个例子。

Figure 2.  Depth Particles

图例2 深度粒子

2D Soft Particles

2D软粒子

In the basic flat particle system, the line where the particle quad intersects the geometry is a giveaway that the particles are not actually 3D. To avoid this, the sample can read back the depth buffer as a texture. In the shader, this depth value is sampled and tested against the depth value being rendered for the current pixel in the particle. The alpha value increases as the difference between the depth value in the buffer, and then depth being written out from the pixel shader decreases. Therefore, the particle becomes more transparent as it approaches intersection with the scene geometry.

在基本的平坦粒子系统中,由于矩形粒子与几何体交叉产生的线使人感觉到它不是真正的3D。为了避免这个问题,这个实例把深度缓存作为一个纹理。在着色器中,这个深度值和当前正要被渲染的粒子像素的深度值进行比较和采样。ALPHA值作为区别增加 缓存中深度值,然后像素着色器中再输出减少的深度。这样粒子越靠近场景中的几何体,粒子就越变的透明。

Figure 3.  2D Soft Particles

图例3 2D软粒子

Depth Soft Particles

深度软粒子

The same approach can be used with particles that output depth. The only difference is that the depth compared with the depth buffer is not the depth from the quad, but the depth from the quad augmented by the depth stored in the particle texture.

同样的方法也可以用来输出深度的粒子上。唯一的区别是与深度缓存中深度值比较的不是来自矩形的深度,而是来自矩形的深度加上粒子纹理中储存的深度。

Figure 4.  Depth Soft Particles, example

图例4 深度软粒子,实例

Figure 5.  Depth Soft Particles, explanation

图例5,深度软粒子,解释

 

Animating the 2D Particles

2D粒子动画

To animate the 2D particles, the sample uses a series of animated particle texture frames stored in a volume texture. The first slice of the volume texture stores the first frame of the animation; the last slice stores the last. As the particle ages, the shader uses the age (in this case a float in the range of [0..1]) to look up a color in a 1D texture. This color modulates the color of the entire particle and makes the particle appear dark near its birth and lighter just before death.

为了使2D粒子产生动画,这个实例使用了一系列储存在体积纹理中的动画粒子纹理桢。体积纹理中第一片纹理就是动画的第一个桢,最后一片纹理就是最后一桢。像粒子周期一样,着色起使用寿命(在这里是0到1的浮点数)来查找在1D纹理中的颜色。这个颜色与整个粒子的颜色进行混合,来使粒子刚出现的时候显得“黑”而在在消失的时候更“亮”。

Volumetric Particles

体积化粒子

The volumetric techniques in the sample give the particles the appearance of true 3D volume instead of the flat look of 2D particles. To achieve this, the sample traces rays through an imaginary 3D primitive circumscribed by the camera-aligned particle quad. In this case, the sample uses a sphere as the imaginary primitive. There are three main reasons for choosing a sphere. First, it is always possible to circumscribe a sphere in a camera-aligned quad. Second, a sphere is a very symmetrical shape. It looks the same no matter how you orient it. Third, the intersection between a ray and a sphere is easy to compute.

在这个例子中体积化技术使粒子像真正的3D体积而不是平坦的2D粒子。为了达到这个目标,实例追踪穿越与摄像机对齐的粒子矩形外切的虚拟的3D几何体的光线。这种情况吓,实例把一个球体作为虚拟几何体。这里有三个主要的原因来解释为什么选择一个球体。首先,它总能保证和一个与摄像机对齐的矩形相切。其次,它是一个非常简单的图形,无论你怎样放置它看起来都一样。最后,一条射线与球体的交点容易计算。

The main process is as follows. The particles are handled in the same manner as in the 2D technique until they reach the pixel shader. The pixel shader computes a ray from the eye to the pixel being drawn on the quad.

主要的过程如下所示,粒子像2D技术那样被处理直到他们用上像素着色器。像素着色起计算从眼睛到被画矩形的光线。

Figure 6.  Volumetric Particles, Main Process

图例6 体积化粒子,主过程

It then calculates the intersections of this ray and the imaginary sphere circumscribed by the particle quad. If no intersections occur, the ray is discarded.

接下来计算这个光线和与粒子矩形相切虚拟球体的饿交点。如果没有交点,那么这条光线将被忽略。

Figure 7.  Volumetric Particles, Calculate the Intersection

图例7 体积化粒子,计算交点

Using a fixed step-size, the shader determines the number of steps needed to march from one intersection point to the other. Along the way, several octaves of noise are sampled. These are stored in a 4-channel volume texture. The RGB channels contain the inverse density gradient. The alpha channel contains the density. Lighting is computed at the point using a combination of the normal from the center of the sphere at the point and the density gradient. Each step is summed with the previous step, and the final result is the opacity and color value for the ray traced through the volume. This result is modified slightly by some overall opacity values to get a specific look.

使用固定的步长,着色器可以决定需要多少数量的步长,这种步长是用来跟踪一个交点到另一个交点。使用这种方法,扰动可以通过量化表示出来。他们被存储在4通道的体积纹理中。RGB通道包括反射率。Alpha通道包括密度,通过球心的法向量和斜率可以用来计算出光线位置。每一个步骤都是是通过前一个步骤计算出来的,最后的结果是不透明的而且就是被追踪的射线穿越体积的颜色。通过更改不透明值去稍微修改结果可以获得特别的视觉效果!!

Figure 8.  March Between the Two Intersection Points

图例8 通过两个交点

If the ray intersects the depth buffer before it exits the sphere, the shader changes the rear exit point to be the depth buffer intersection. Optionally, to achieve the soft intersection with world geometry, the alpha can be attenuated based upon the distance between the sample and the depth buffer.

如果光线在它到达球体前与深度缓存相交,那么着色器就要把后出口点变成与深度缓存的交点。

Figure 9.  Ray Intersecting the Depth Buffer

图例9 光线与深度缓存相交

Figure 10.  Hard Volume Particles

图例10 硬体积粒子

Figure 11.  Soft Volume Particles

图例11 软体积粒子

Animating the Volume Particles

体积粒子动画

Although the particles are animating (moving away from the source), added detail is gained by animating the texture coordinates used to calculate the lookup in the the noise volume texture. By moving the texture coordinates in one direction, the smoke is made to look like it is moving in the opposite direction.

尽管粒子是动态的(从原点移开),但额外的细节是由动态计算纹理坐标来表现体积纹理的不规则表现而产生的。通过向一个方向移动纹理坐标,烟雾就看起来向相反的方向移动。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值