使螺旋星系的臂末端密度降低的公式

Formula to make the ends of the arms of a spiral galaxy less dense

我想使用 C# 在 Unity 3D 中制作螺旋星系(我使用派生的对数螺旋)。我想把手臂的末端设置得比中间的密度低,但我的整个密度都是一样的。

我想要这个:

我目前的结果是:


(来源:noelshack.com

我完全不知道该怎么做,但用于分散的值是 randomOffsetXY


public int numArms = 5;
public float armOffsetMax = 0.5f;
public float rotationFactor = 5;
public float randomOffsetXY = 0.02f;
public float percentStarInCentre = 5;



for (int i = 0; i < PixelMath.instanceStarCount; i++)
        {
            if(type == GalaxyTypes.Spiral)
            {
                float distance = Random.value;
                distance = Mathf.Pow (distance, PixelMath.percentStarInCentre / 100) ;
                
                      
                // Choose an angle between 0 and 2 * PI.
                float angle = Random.value * 2 * Mathf.PI;
                float armOffset = Random.value * PixelMath.armsOffetSetMax;

                armOffset = armOffset - PixelMath.armsOffetSetMax / 2;
                armOffset = armOffset * (1 / distance);

                float squaredArmOffset = Mathf.Pow (armOffset, 2);

                if (armOffset < 0)
                    squaredArmOffset = squaredArmOffset * -1;
                armOffset = squaredArmOffset;

                float rotation = distance * PixelMath.rotationFactor;

                // Compute the angle of the arms.
                angle = (int) (angle / armSeparationDistance ) * armSeparationDistance + armOffset + rotation;



                // Convert polar coordinates to 2D cartesian coordinates.
                float starX =  Mathf.Sin (angle) * distance;
                float starZ =  Mathf.Cos (angle) * distance;
                float starY = 0;
                


更改行
distance = Mathf.Pow (distance, PixelMath.percentStarInCentre / 100) ;

第二个参数大于 1 的东西。所以像

distance = Mathf.Pow (distance, 2f);

或者取决于你想要的效果的夸张程度,

distance = Mathf.Pow (distance, 5f);

如果您希望中心的簇较少,则需要线性和抛物线的混合:

float pow
float distance = Random.value;
float slopeMod = 0.8f; // between 0 and 1, higher is more linear
distance = Mathf.Pow(distance, 2f) *(1-slopeMod) + distance*slopeMod;

您发布的 link 说的是距离的平方:

Now it’s starting to come together. We can see clearly defined arms in our galaxy, which have clearly defined spines running down the middle. At this point, you might notice that due to our squaring, the futher areas of the arms appear more dense than they were before. Let’s resolve this by pulling all the stars closer to the center with a distance squaring operation, the same way we just pulled stars closer to their arms:

说这样做会从看起来像您当前拥有的东西中获取输出:

像你想要的那样手臂外侧的相对密度较低的东西:

谢谢,经过一番搜索和帮助,我需要的函数称为 "falloff" 函数,但它们是不同的衰减函数,如标准函数(二次函数、线性函数等)

我是这个论坛的新手,我不知道它是否是一个很好的答案用法,但我的衰减函数实际上是:distance = (Mathf.Pow(distance, 1f / 3f) - 1f) / (1-slopeMod);