本文由博主(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的更多相关文章

随机推荐

  1. 我的VSTO之路(五):Outlook初步开发之联系人扩展

    原文:我的VSTO之路(五):Outlook初步开发之联系人扩展 上一讲我们完成对Word的介绍,文本开始,我将着重介绍Outlook.Outlook是微软Office中一个非常实用的工具,尤其在一个 ...

  2. 【Fiddler】手机抓包

    Fiddler (四) 实现手机的抓包 手机配置了后,Fiddler不开起来就什么也连接不了

  3. arcgis数据文件使用

    dem数据打开,保存,使用 打开

  4. bzoj1227 [SDOI2009]虔诚的墓主人(组合公式+离散化+线段树)

    1227: [SDOI2009]虔诚的墓主人 Time Limit: 5 Sec  Memory Limit: 259 MBSubmit: 803  Solved: 372[Submit][Statu ...

  5. 用数组实现栈demo

    package cn.aust.zyw.demo; import java.util.Iterator; /** * Created by zyw on 2016/2/19. * 用数组实现栈 */ ...

  6. media screen 响应式布局(知识点)

    一.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端--而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网 ...

  7. JAVA大数类

    JAVA大数类api http://man.ddvip.com/program/java_api_zh/java/math/BigInteger.html#method_summary 不仅仅只能查J ...

  8. 账户管理命令 useradd、groupadd

    内容提要: 1. 掌握用户的 增/删/改 命令 2. 掌握组的 增/删/改 命令 组管理 1)groupadd groupadd 用于添加组账号.格式如下: groupadd [-g GID] GRO ...

  9. TCP具体解释(3):重传、流量控制、拥塞控制……

    传输数据 在TCP的数据传送状态.非常多重要的机制保证了TCP的可靠性和强壮性.它们包括:使用序号.对收到的TCP报文段进行排序以及检測反复的数据:使用校验和来检測报文段的错误.使用确认和计时器来检測 ...

  10. [ES6] Array.findIndex()

    In es5, you can use indexOf to get the index of one item in an array. In es6, you can use findIndex( ...