Modelowanie_Wirtualnych_Swi.../Assets/Scripts/Turtle3D.cs

67 lines
3.5 KiB
C#
Raw Permalink Normal View History

2021-04-16 23:44:21 +02:00
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
using ConsoleLSystem;
using System;
public class Turtle3D : TurtleLSystem {
public GameObject bud;
public GameObject flower_young;
public GameObject flower_old;
public GameObject petal;
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 (1f, 1f, 1f));
//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])));
turtleInterpretation.Add("S", (float[] args) => new Tuple<GameObject, Matrix4x4>(null, Matrix4x4.identity));
turtleInterpretation.Add("B", (float[] args) => new Tuple<GameObject, Matrix4x4>(bud, transformation));
turtleInterpretation.Add("Y", (float[] args) => new Tuple<GameObject, Matrix4x4>(flower_young, transformation));
turtleInterpretation.Add("O", (float[] args) => new Tuple<GameObject, Matrix4x4>(flower_old, transformation));
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>(null, transformation));
}
}