Modelowanie_Wirtualnych_Swi.../Assets/Scripts/TurtleEnvironment.cs
2021-07-09 23:35:24 +02:00

86 lines
4.6 KiB
C#
Raw Blame History

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TurtleEnvironment : TurtleLSystemEnvironment {
public GameObject obj;
public GameObject flower;
public GameObject leaf;
public GameObject appleRed;
public GameObject appleGreen;
public float angle;
private Func<float[], Matrix4x4> _roation(Vector3 axis) {
Matrix4x4 f(float[] args) {
if (args.Length == 0) {
return Matrix4x4.Rotate(Quaternion.AngleAxis(angle, axis));
}
else {
return Matrix4x4.Rotate(Quaternion.AngleAxis(args[0], axis));
}
}
return f;
}
GameObject setColor(GameObject prefab, int index) {
var result = Instantiate(prefab);
switch (index) {
case 1:
result.GetComponent<Renderer>().material.SetColor("_Color", Color.red);
break;
case 2:
result.GetComponent<Renderer>().material.SetColor("_Color", Color.blue);
break;
case 3:
result.GetComponent<Renderer>().material.SetColor("_Color", Color.white);
break;
}
return result;
}
protected override void initLiteralInterpretation() {
turtleInterpretation = new Dictionary<string, Func<float[], Tuple<GameObject, Matrix4x4>>>();
//turtleInterpretation
var transformation = Matrix4x4.Translate(new Vector3(0.0f, 0.1f, 0)) * Matrix4x4.Scale(new Vector3(0.05f, 0.1f, 0.05f));
var transformationM = Matrix4x4.Translate(new Vector3(0.0f, 0.2f, 0)) * Matrix4x4.Scale(new Vector3(0.08f, 0.2f, 0.08f));
turtleInterpretation.Add("+", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.back)(args)));
turtleInterpretation.Add("-", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.forward)(args)));
turtleInterpretation.Add("\\", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.down)(args)));
turtleInterpretation.Add("/", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.up)(args)));
turtleInterpretation.Add("^", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.left)(args)));
turtleInterpretation.Add("&", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, _roation(Vector3.right)(args)));
turtleInterpretation.Add("f", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Translate(Vector3.up * args[0])));
turtleInterpretation.Add("W", (float[] args) => new Tuple<GameObject, Matrix4x4>(flower, Matrix4x4.Scale(new Vector3(args[0]*0.95f, args[0]*0.95f, args[0]*0.95f))*Matrix4x4.Rotate(Quaternion.Euler(90.0f, 0, 0))));
turtleInterpretation.Add("B", (float[] args) => new Tuple<GameObject, Matrix4x4>(leaf, Matrix4x4.Scale(new Vector3(args[0]*0.35f, args[0]*0.35f, args[0]*0.35f))));
turtleInterpretation.Add("S", (float[] args) => new Tuple<GameObject, Matrix4x4>(appleRed, Matrix4x4.Scale(new Vector3(args[0]*0.1f, args[0]*0.1f, args[0]*0.1f))*Matrix4x4.Rotate(Quaternion.Euler(180.0f, 0, 0))*Matrix4x4.Translate(new Vector3(0.0f, -0.75f, 0.0f))));
turtleInterpretation.Add("Z", (float[] args) => new Tuple<GameObject, Matrix4x4>(appleGreen, Matrix4x4.Scale(new Vector3(args[0]*0.1f, args[0]*0.1f, args[0]*0.1f))*Matrix4x4.Rotate(Quaternion.Euler(180.0f, 0, 0))*Matrix4x4.Translate(new Vector3(0.0f, -0.75f, 0.0f))));
// z wyswietlaniem sygna<6E><61>w wzrostu
//turtleInterpretation.Add("F", (float[] args) => new Tuple<GameObject, Matrix4x4>(setColor(obj,Mathf.FloorToInt(args[0])), transformation*Matrix4x4.Scale(new Vector3(args[1], 1.0f, args[1]))));
// bez wyswietlania sygna<6E><61>w wzrostu
turtleInterpretation.Add("F", (float[] args) => new Tuple<GameObject, Matrix4x4>(obj, transformation*Matrix4x4.Scale(new Vector3(args[1]*1.0f, 1.0f, args[1]*1.0f))));
turtleInterpretation.Add("G", (float[] args) => new Tuple<GameObject, Matrix4x4>(obj, transformation));
turtleInterpretation.Add("C", (float[] args) => new Tuple<GameObject, Matrix4x4>(obj, transformation*Matrix4x4.Translate(new Vector3(0.0f, -1.0f, 0.0f))*Matrix4x4.Scale(new Vector3(1.0f, 0.1f, 1.0f))));
turtleInterpretation.Add("", (float[] args) => new Tuple<GameObject, Matrix4x4>(obj, transformation));
//Wildcard how to represent any other symbol
turtleInterpretation.Add("*.*", (float[] args) => new Tuple<GameObject, Matrix4x4>(obj, transformationM));
}
}