localScale 如何在此 Fractal Unity 代码示例中减少
How is localScale reducing in this Fractal code sample for Unity
我正在关注这个 Unity Fractal 示例:
http://catlikecoding.com/unity/tutorials/constructing-a-fractal/
using UnityEngine;
using System.Collections;
public class Fractal : MonoBehaviour {
public Mesh mesh;
public Material material;
public int maxDepth = 20;
public int depth = 0;
public float childScale = 0.5f;
// Use this for initialization
void Start () {
gameObject.AddComponent<MeshFilter>().mesh = mesh;
gameObject.AddComponent<MeshRenderer>().material = material;
if(depth < maxDepth)
{
new GameObject("Fractal Child").AddComponent<Fractal>().Initialise(this);
}
}
// Update is called once per frame
void Update () {
}
private void Initialise(Fractal parent)
{
Debug.Log(gameObject.transform.localScale);
mesh = parent.mesh;
material = parent.material;
maxDepth = parent.maxDepth;
childScale = parent.childScale;
Debug.Log("childScale = " + childScale);
depth = parent.depth + 1;
transform.parent = parent.transform;
transform.localScale = Vector3.one * childScale;
Debug.Log("localScale = " + transform.localScale);
transform.localPosition = Vector3.up * (0.5f + 0.5f * childScale);
}
}
到目前为止,我确实有一个垂直堆叠的盒子,每个盒子的大小都是它下面盒子的一半(childScale 设置为 0.5f)。
我不太明白为什么每个游戏对象(Fractal 实例)的大小都是其父对象的一半。 childScale 始终为 0.5f,我看不出它如何在此代码中减少到 0.25f、0.125f 等以使递归创建的每个实例 - 其父级大小的一半。
感谢任何建议。
下图是结果(请注意,这是使用 0.9f 而不是 0.5f 的 childScale)。
编辑器中显示的比例为局部比例。局部比例是相对于变换父级的。分形示例是嵌套的。因此,如果您的顶级父级是 0.5 本地比例,那么它也是 0.5 世界比例,因为它没有父级。您的下一个对象是 .5 本地比例,但 (.5 * .5) 世界比例。下一个对象是 .5 本地比例,(.5 * .5 * .5) 世界比例。看到这个兔子洞要去哪里了吗? :)
我正在关注这个 Unity Fractal 示例:
http://catlikecoding.com/unity/tutorials/constructing-a-fractal/
using UnityEngine;
using System.Collections;
public class Fractal : MonoBehaviour {
public Mesh mesh;
public Material material;
public int maxDepth = 20;
public int depth = 0;
public float childScale = 0.5f;
// Use this for initialization
void Start () {
gameObject.AddComponent<MeshFilter>().mesh = mesh;
gameObject.AddComponent<MeshRenderer>().material = material;
if(depth < maxDepth)
{
new GameObject("Fractal Child").AddComponent<Fractal>().Initialise(this);
}
}
// Update is called once per frame
void Update () {
}
private void Initialise(Fractal parent)
{
Debug.Log(gameObject.transform.localScale);
mesh = parent.mesh;
material = parent.material;
maxDepth = parent.maxDepth;
childScale = parent.childScale;
Debug.Log("childScale = " + childScale);
depth = parent.depth + 1;
transform.parent = parent.transform;
transform.localScale = Vector3.one * childScale;
Debug.Log("localScale = " + transform.localScale);
transform.localPosition = Vector3.up * (0.5f + 0.5f * childScale);
}
}
到目前为止,我确实有一个垂直堆叠的盒子,每个盒子的大小都是它下面盒子的一半(childScale 设置为 0.5f)。
我不太明白为什么每个游戏对象(Fractal 实例)的大小都是其父对象的一半。 childScale 始终为 0.5f,我看不出它如何在此代码中减少到 0.25f、0.125f 等以使递归创建的每个实例 - 其父级大小的一半。
感谢任何建议。
下图是结果(请注意,这是使用 0.9f 而不是 0.5f 的 childScale)。
编辑器中显示的比例为局部比例。局部比例是相对于变换父级的。分形示例是嵌套的。因此,如果您的顶级父级是 0.5 本地比例,那么它也是 0.5 世界比例,因为它没有父级。您的下一个对象是 .5 本地比例,但 (.5 * .5) 世界比例。下一个对象是 .5 本地比例,(.5 * .5 * .5) 世界比例。看到这个兔子洞要去哪里了吗? :)