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的更多相关文章
随机推荐
- windows 10家庭版升级到专业版
因为要搭建一个服务器,需要用到Docker,根据Docker的文档,Docker必须要安装在windows 10 企业版,专业版,或者教育版上.不然不能使用.一直以为要重新下载专业版的镜像重新安装wi ...
- 五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O
五种I/O 模式——阻塞(默认IO模式),非阻塞(常用语管道),I/O多路复用(IO多路复用的应用场景),信号I/O,异步I/O 五种I/O 模式:[1] 阻塞 I/O ...
- Javascript面向对象编程(二):构造函数的继承 by 阮一峰
今天要介绍的是,对象之间的"继承"的五种方法. 比如,现在有一个"动物"对象的构造函数. function Animal(){ this.species = & ...
- Codeforces 715B & 716D Complete The Graph 【最短路】 (Codeforces Round #372 (Div. 2))
B. Complete The Graph time limit per test 4 seconds memory limit per test 256 megabytes input standa ...
- CodeForces 588A
题目链接: http://codeforces.com/problemset/problem/588/A 解题思路: 这个题目很简单, 就是前一天肉的价格比后面几天低还是高,如果是高的话,只要买当天份 ...
- SQL 第二章 作业
/*第二章 作业*/ create table S ( sno char(2) NOT NULL UNIQUE, sname char(3), city char(2) ); alter table ...
- [Locked] Paint House I & II
Paint House There are a row of n houses, each house can be painted with one of the three colors: red ...
- 《University Calculus》-chaper8-无穷序列和无穷级数-p级数
Q:定义p级数有如下形式,讨论p级数的敛散性.(p>o) 我们以p = 1作为分界点,因为实践表明这个分界点是最优区分度的.那么下面我们进行分情况讨论. 在这之前,我们有必要先引入一个检验敛散性 ...
- SRM 400(1-250pt, 1-500pt)
DIV1 250pt 题意:给定一个正整数n(n <= 10^18),如果n = p^q,其中p为质数,q > 1,则返回vector<int> ans = {p, q},否则 ...
- Java 流的概述及操作(转)
一.什么是流? 流就是字节序列的抽象概念,能被连续读取数据的数据源和能被连续写入数据的接收端就是流,流机制是Java及C++中的一个重要机制,通过流我们可以自由地控制文件.内存.IO设备等数据的流向. ...