Fractal_Test
本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Fractal_Test.html
参考:http://catlikecoding.com/unity/tutorials/constructing-a-fractal/

using UnityEngine;
using System.Collections; public class Fractal : MonoBehaviour {
public Mesh[] meshes;
public Material material;
public int maxDepth;
public float childScale;
private int depth;
public float spawnProbability;
public float maxRotationSpeed;
private float rotationSpeed; private static Vector3[] childDirections = {
Vector3.up,
Vector3.right,
Vector3.left,
Vector3.forward,
Vector3.back,
Vector3.down
}; private static Quaternion[] childOrientations = {
Quaternion.identity,
Quaternion.Euler(0f, 0f, -90f),
Quaternion.Euler(0f, 0f, 90f),
Quaternion.Euler(90f, 0f, 0f),
Quaternion.Euler(-90f, 0f, 0f),
Quaternion.Euler(0f, 0f, 180f)
}; private Material[,] materials; private void InitializeMaterials() {
materials = new Material[maxDepth + , ];
for (int i = ; i <= maxDepth; i++) {
float t = i / (maxDepth - 1f);
t *= t;
materials[i, ] = new Material(material);
materials[i, ].color = Color.Lerp(Color.white, Color.yellow, t);
materials[i, ] = new Material(material);
materials[i, ].color = Color.Lerp(Color.white, Color.cyan, t);
}
materials[maxDepth, ].color = Color.magenta;
materials[maxDepth, ].color = Color.red;
} // Use this for initialization
void Start() {
if(materials == null){
InitializeMaterials();
}
gameObject.AddComponent<MeshFilter>().mesh = meshes[Random.Range(, meshes.Length)];
gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(, )];
rotationSpeed = Random.Range(-maxRotationSpeed, maxRotationSpeed);
if(depth < maxDepth) {
StartCoroutine(CreateChildren());
} } private IEnumerator CreateChildren() {
for (int i = ; i < childDirections.Length; ++i) {
if (Random.value < spawnProbability) {
yield return new WaitForSeconds(Random.Range(0.1f, 0.5f));
new GameObject("Fractal Child").AddComponent<Fractal>().Initialize(this, i);
} }
} private void Initialize(Fractal parent, int childIndex) {
meshes = parent.meshes;
materials = parent.materials;
maxDepth = parent.maxDepth;
depth = parent.depth + ;
transform.parent = parent.transform;
childScale = parent.childScale;
transform.localScale = Vector3.one * childScale;
transform.localPosition = childDirections[childIndex] * (0.5f + 0.5f * childScale);
transform.localRotation = childOrientations[childIndex];
spawnProbability = parent.spawnProbability;
maxRotationSpeed = parent.maxRotationSpeed;
} // Update is called once per frame
void Update() {
transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
}
}
PS:失败的地方是:没有DynamicBatch,为何原文说会合并呢?
unitypackage:http://files.cnblogs.com/files/YinaPan/Fractal_Test.rar
Fractal_Test的更多相关文章
随机推荐
- Request.url用法
原文:Request.url用法 我們在開發網頁應用程式,時常需要去解析網址(Request.Url)的每個片段,進行一些判斷.例如說 "http://localhost:1897/News ...
- swiper 多个循环的实现
swiper 最好要一一对应,最好与id关联. new Swiper('#guess .swiper-container', { pagination: '#guess .swiper-paginat ...
- weblogic启动报错之Unrecognized option: -jrockit
报错如下: $ ./startWebLogic.sh . . JAVA Memory arguments: -Xms512m -Xmx512m . WLS Start Mode=Production ...
- 字符串(多串后缀自动机):HDU 4436 str2int
str2int Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total S ...
- [OSX] 取消开机启动
以Pulse Secure为例 参考:https://kb.pulsesecure.net/articles/Pulse_Secure_Article/KB26679 输入指令: launchctl ...
- Java Spring的 JavaConfig 注解
序 传统spring一般都是基于xml配置的,不过后来新增了许多JavaConfig的注解.特别是springboot,基本都是清一色的java config,不了解一下,还真是不适应.这里备注一下. ...
- 3rd day
今天学习创建了几个简单的表:
- bzoj3669: [Noi2014]魔法森林 lct
记得去年模拟赛的时候好像YY出二分答案枚举a,b的暴力,过了55欸 然后看正解,为了将两维变成一维,将a排序,模拟Kruskal的加边过程,同时维护1到n的最大值,加入一条边e(u,v,a,b)时有以 ...
- [PWA] 2. Service worker life cycle
Once serive worker is registered, the first time we go to the app, we cannot see the logs from servc ...
- Android中自定义View的MeasureSpec使用
有时,Android系统控件无法满足我们的需求,因此有必要自定义View.具体方法参见官方开发文档:http://developer.android.com/guide/topics/ui/custo ...