Fractal_Test

Posted YinaPan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Fractal_Test相关的知识,希望对你有一定的参考价值。

本文由博主(YinaPan)原创,转载请注明出处:http://www.cnblogs.com/YinaPan/p/Fractal_Test.html 

参考:http://catlikecoding.com/unity/tutorials/constructing-a-fractal/

 1 using UnityEngine;
 2 using System.Collections;
 3 
 4 public class Fractal : MonoBehaviour {
 5     public Mesh[] meshes;
 6     public Material material;
 7     public int maxDepth;
 8     public float childScale;
 9     private int depth;
10     public float spawnProbability;
11     public float maxRotationSpeed;
12     private float rotationSpeed;
13 
14     private static Vector3[] childDirections = {
15                                                    Vector3.up,
16                                                    Vector3.right,
17                                                    Vector3.left,
18                                                    Vector3.forward,
19                                                    Vector3.back,
20                                                    Vector3.down
21                                                };
22 
23     private static Quaternion[] childOrientations = {
24                                                         Quaternion.identity,
25                                                         Quaternion.Euler(0f, 0f, -90f),
26                                                         Quaternion.Euler(0f, 0f, 90f),
27                                                         Quaternion.Euler(90f, 0f, 0f),
28                                                         Quaternion.Euler(-90f, 0f, 0f),
29                                                         Quaternion.Euler(0f, 0f, 180f)
30                                                     };
31 
32     private Material[,] materials;
33 
34     private void InitializeMaterials() {
35         materials = new Material[maxDepth + 1, 2];
36         for (int i = 0; i <= maxDepth; i++) {
37             float t = i / (maxDepth - 1f);
38             t *= t;
39             materials[i, 0] = new Material(material);
40             materials[i, 0].color = Color.Lerp(Color.white, Color.yellow, t);
41             materials[i, 1] = new Material(material);
42             materials[i, 1].color = Color.Lerp(Color.white, Color.cyan, t);
43         }
44         materials[maxDepth, 0].color = Color.magenta;
45         materials[maxDepth, 1].color = Color.red;
46     }
47 
48     // Use this for initialization
49     void Start() {
50         if(materials == null){
51             InitializeMaterials();
52         }
53         gameObject.AddComponent<MeshFilter>().mesh = meshes[Random.Range(0, meshes.Length)];
54         gameObject.AddComponent<MeshRenderer>().material = materials[depth, Random.Range(0, 2)];
55         rotationSpeed = Random.Range(-maxRotationSpeed, maxRotationSpeed);
56         if(depth < maxDepth) {
57             StartCoroutine(CreateChildren());
58         }
59 
60     }
61 
62     private IEnumerator CreateChildren() {
63         for (int i = 0; i < childDirections.Length; ++i) {
64             if (Random.value < spawnProbability) {
65                 yield return new WaitForSeconds(Random.Range(0.1f, 0.5f));
66                 new GameObject("Fractal Child").AddComponent<Fractal>().Initialize(this, i);
67             }
68             
69         }
70     }
71 
72     private void Initialize(Fractal parent, int childIndex) {
73         meshes = parent.meshes;
74         materials = parent.materials;
75         maxDepth = parent.maxDepth;
76         depth = parent.depth + 1;
77         transform.parent = parent.transform;
78         childScale = parent.childScale;
79         transform.localScale = Vector3.one * childScale;
80         transform.localPosition = childDirections[childIndex] * (0.5f + 0.5f * childScale);
81         transform.localRotation = childOrientations[childIndex];
82         spawnProbability = parent.spawnProbability;
83         maxRotationSpeed = parent.maxRotationSpeed;
84     }
85 
86     // Update is called once per frame
87     void Update() {
88         transform.Rotate(0f, rotationSpeed * Time.deltaTime, 0f);
89     }
90 }
View Code

PS:失败的地方是:没有DynamicBatch,为何原文说会合并呢?

unitypackage:http://files.cnblogs.com/files/YinaPan/Fractal_Test.rar

以上是关于Fractal_Test的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段——CSS选择器

谷歌浏览器调试jsp 引入代码片段,如何调试代码片段中的js

片段和活动之间的核心区别是啥?哪些代码可以写成片段?

VSCode自定义代码片段——.vue文件的模板

VSCode自定义代码片段6——CSS选择器

VSCode自定义代码片段——声明函数