您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

213 行
6.7 KiB

using TMPro;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using System;
using UnityEngine.UIElements;
[UnitCategory("Custom")]
public class CubeRotateNode : Unit
{
[SerializeField]
public ControlInput enter { get; private set; }
[SerializeField]
public ControlInput show { get; private set; }
[SerializeField]
public ControlInput rotate { get; private set; }
[DoNotSerialize]
public ControlOutput exit { get; private set; }
[DoNotSerialize]
public ControlOutput showed { get; private set; }
[DoNotSerialize]
public ControlOutput rotated { get; private set; }
[DoNotSerialize]
public ValueInput CubeframeInput { get; private set; }
[DoNotSerialize]
public ValueInput SwitchSpeedInput { get; private set; }
[DoNotSerialize]
public ValueInput lengthInput { get; private set; }
[DoNotSerialize]
public ValueInput EulersInput { get; private set; }
[DoNotSerialize]
public ValueInput targetInput { get; private set; }
[DoNotSerialize]
public ValueInput scaleInput { get; private set; }
[DoNotSerialize]
public ValueInput otherInput { get; private set; }
[DoNotSerialize]
public ValueInput stateInput { get; private set; }
[DoNotSerialize]
public ValueInput setEulerInput { get; private set; }
[DoNotSerialize]
public ValueOutput stateOutput { get; private set; }
[DoNotSerialize]
public ValueOutput otherOutput { get; private set; }
private GameObject Cubeframe;
private float state;
private float SwitchSpeed;
private float length;
private bool setEuler;
private float SitaY;
private Vector3 local ;
private Vector3 Eulers;
private Quaternion rotation;
private Quaternion localrotate;
private Collider target;
private GameObject other;
protected override void Definition()
{
enter = ControlInput("enter", (flow) =>
{
Cubeframe = flow.GetValue<GameObject>(CubeframeInput);
length = flow.GetValue<float>(lengthInput);
other = new GameObject();
other.transform.rotation = Cubeframe.transform.rotation;
other.transform.position = Cubeframe.transform.position;
flow.SetValue(otherOutput, other);
return exit;
});
rotate = ControlInput("rotate", (flow) =>
{
Cubeframe = flow.GetValue<GameObject>(CubeframeInput);
SwitchSpeed = flow.GetValue<float>(SwitchSpeedInput);
target = flow.GetValue<Collider>(targetInput);
Eulers = flow.GetValue<Vector3>(EulersInput);
local = flow.GetValue<Vector3>(scaleInput);
state = flow.GetValue<float>(stateInput);
setEuler = flow.GetValue<bool>(setEulerInput);
/*if (!setEuler)
{
setEuler = true;
//flow.SetValue(setEulerOutput, setEuler);
}*/
if (state >= 0)
{
state -= Time.deltaTime * SwitchSpeed;
var z = Mathf.Clamp(1 - state, 0.01f, 1);
var x = 1+ (1.01f - z) * (length - 1);
Vector3 Scale = new Vector3 (x * local.x, local.y, z * local.z);
Cubeframe.transform.localScale = Scale;
}
Quaternion localrotation = Quaternion.Euler(Eulers * Time.deltaTime);
Cubeframe.transform.localRotation *= localrotation;
//flow.SetValue(otherOutput, other);
flow.SetValue(stateOutput, state);
return rotated;
});
show = ControlInput("show", (flow) =>
{
Cubeframe = flow.GetValue<GameObject>(CubeframeInput);
SwitchSpeed = flow.GetValue<float>(SwitchSpeedInput);
target = flow.GetValue<Collider>(targetInput);
Eulers = flow.GetValue<Vector3>(EulersInput);
other = flow.GetValue<GameObject>(otherInput);
state = flow.GetValue<float>(stateInput);
local = flow.GetValue<Vector3>(scaleInput);
rotation = other.transform.localRotation;
setEuler = flow.GetValue<bool>(setEulerInput);
if (setEuler)
{
//setEuler = false;
localrotate = Cubeframe.transform.rotation;
}
setFace(target.transform.position);
if (state <= 1)
{
state += Time.deltaTime * SwitchSpeed;
var z = Mathf.Clamp(1 - state, 0.01f, 1);
var x = 1 + (1 - z) * (length - 1);
Vector3 Scale = new Vector3(x * local.x, local.y, z * local.z);
Cubeframe.transform.localScale = Scale;
Cubeframe.transform.rotation = Quaternion.Lerp(localrotate, rotation, state);
}
else
{
Cubeframe.transform.rotation = rotation;
}
flow.SetValue(otherOutput, other);
flow.SetValue(stateOutput, state);
return showed;
});
CubeframeInput = ValueInput<GameObject>("CubeframeInput");
SwitchSpeedInput = ValueInput<float>("SwitchSpeedInput",1f);
lengthInput = ValueInput<float>("lengthInput", 1.5f);
EulersInput = ValueInput<Vector3>("EulersInput");
targetInput = ValueInput<Collider>("targetInput");
otherInput = ValueInput<GameObject>("otherInput");
scaleInput = ValueInput<Vector3>("scaleInput");
stateInput = ValueInput<float>("stateInput");
setEulerInput = ValueInput<bool>("setEulerInput");
stateOutput = ValueOutput<float>("stateOutput");
otherOutput = ValueOutput<GameObject>("otherOutput");
exit = ControlOutput("exit");
showed = ControlOutput("showed");
rotated = ControlOutput("rotated");
Succession(enter, exit);
Succession(show, showed);
Succession(rotate, rotated);
}
private void setFace(Vector3 pos)
{
var forward = -other.transform.forward;
pos -= other.transform.position;
pos.y = 0;
pos = pos.normalized;
var dot = Vector3.Dot(forward, pos);
var cross = Vector3.Cross(forward, pos);
var sita = Mathf.Acos(dot) * Mathf.Rad2Deg;
if (cross.y > 0) { sita *= -1; }
if (Mathf.Abs(sita) > 0.02f)
{
rotation = rotation * Quaternion.Euler(0f, -sita, 0f);
other.transform.rotation = rotation;
}
}
}