MWS_2021/unity_artifitial_world-st/Assets/Scripts/Turtle3D.cs
2021-04-11 20:45:16 +02:00

99 lines
5.4 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using ConsoleLSystem;
using System;
public class Turtle3D : TurtleLSystem {
public GameObject obj;
public GameObject sphere;
public GameObject flower;
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;
}
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));
// import modeli z gry do rysowania - AnabaenaTurtle.cs ma import z glownej sciezki
var path = "Assets/Models/sunflower/{0}.fbx"; // nazwa.fbx
// srodek najmlodszy kwiatek - domyslnmie tutaj C i sfera
var bud = (GameObject)AssetDatabase.LoadAssetAtPath(String.Format(path, "bud"), typeof(GameObject));
// drugi najmblodszy kwiatek
var flower_young = (GameObject)AssetDatabase.LoadAssetAtPath(String.Format(path, "flower_young"), typeof(GameObject));
// dorosły kwiat - na końcu
var flower_old = (GameObject)AssetDatabase.LoadAssetAtPath(String.Format(path, "flower_old"), typeof(GameObject));
// petal - płatek
var petal = (GameObject)AssetDatabase.LoadAssetAtPath(String.Format(path, "petal"), typeof(GameObject));
//turtleInterpretation.Add("+", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Rotate(Quaternion.Euler(0, 0, -angle))));
//turtleInterpretation.Add("-", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Rotate(Quaternion.Euler(0, 0, angle))));
//turtleInterpretation.Add("\\", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Rotate(Quaternion.Euler(0, -angle, 0))));
//turtleInterpretation.Add("/", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Rotate(Quaternion.Euler(0, angle, 0))));
//turtleInterpretation.Add("^", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Rotate(Quaternion.Euler(-angle, 0, 0))));
//turtleInterpretation.Add("&", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.Rotate(Quaternion.Euler(angle, 0, 0))));
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])));
// odpowiada za rysowanie kulek ze słonecznika - po pierwszej ewoluacji wychodzi sfera/kulka mała ze słonecznika
// jakoś podpiete są pod symbole obiekty/modele z Unity
// turtleInterpretation.Add("S", (float[] args) => new Tuple<GameObject, Matrix4x4>(sphere,Matrix4x4.identity));
turtleInterpretation.Add("S", (float[] args) => new Tuple<GameObject, Matrix4x4>(bud,Matrix4x4.identity));
// słonecznik modele do symboli
//Y - młode kwiatki drugie od śrdoka słonecznika
// turtleInterpretation.Add("Y", (float[] args) => new Tuple<GameObject, Matrix4x4>(Flower, Matrix4x4.identity));
// negowanie symbolu C, żeby w słoneczniku Vogel.txt nie dostawać pierwszego cylidra jako obiektu unity
// Vogel.txt parastychy - te spirale w srodku paratychy (l.poij) kilka parastychów
// parastychów jest tyle bo liczba ich jest taka jak kolejne liczby ciągu fibonacciego
// http://algorithmicbotany.org/papers/abop/abop-ch4.pdf
turtleInterpretation.Add("C", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, transformation));
// Y - bardziej rozwiniete - mlode kwiatki
turtleInterpretation.Add("Y", (float[] args) => new Tuple<GameObject, Matrix4x4>(flower_young, transformation));
//litera O albo sybol A- dorole kwiatki
turtleInterpretation.Add("O", (float[] args) => new Tuple<GameObject, Matrix4x4>(flower_old, transformation));
turtleInterpretation.Add("A", (float[] args) => new Tuple<GameObject, Matrix4x4>(flower_old, transformation));
// P - petla płatki
turtleInterpretation.Add("P", (float[] args) => new Tuple<GameObject, Matrix4x4>(petal, transformation));
//Wildcard how to represent any other symbol
turtleInterpretation.Add("*.*", (float[] args) => new Tuple<GameObject, Matrix4x4>(obj, transformation));
}
}