1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
using Pathfinding.Graphs.Grid.Rules;
using UnityEditor;
using UnityEngine;
namespace Pathfinding {
/// <summary>Editor for the <see cref="RuleTexture"/> rule</summary>
[CustomGridGraphRuleEditor(typeof(RuleTexture), "Texture")]
public class RuleTextureEditor : IGridGraphRuleEditor {
protected static readonly string[] ChannelUseNames = { "None", "Penalty", "Height", "Walkability and Penalty", "Walkability" };
public void OnInspectorGUI (GridGraph graph, GridGraphRule rule) {
var target = rule as RuleTexture;
target.texture = GraphEditor.ObjectField(new GUIContent("Texture"), target.texture, typeof(Texture2D), false, true) as Texture2D;
GUILayout.BeginHorizontal();
GUILayout.FlexibleSpace();
if (GUILayout.Button("Generate Reference")) {
SaveReferenceTexture(graph);
EditorUtility.DisplayDialog("Reference texture saved", "A texture has been saved in which every pixel corresponds to one node. The red channel represents if a node is walkable or not. The green channel represents the (normalized) Y coordinate of the nodes.", "Ok");
}
GUILayout.EndHorizontal();
if (target.texture != null) {
string path = AssetDatabase.GetAssetPath(target.texture);
if (path != "") {
var importer = AssetImporter.GetAtPath(path) as TextureImporter;
if (importer != null && !importer.isReadable) {
if (GraphEditor.FixLabel("Texture is not readable")) {
importer.isReadable = true;
EditorUtility.SetDirty(importer);
AssetDatabase.ImportAsset(path);
}
}
}
}
target.scalingMode = (RuleTexture.ScalingMode)EditorGUILayout.EnumPopup("Scaling Mode", target.scalingMode);
if (target.scalingMode == RuleTexture.ScalingMode.FixedScale) {
EditorGUI.indentLevel++;
target.nodesPerPixel = EditorGUILayout.FloatField("Nodes Per Pixel", target.nodesPerPixel);
EditorGUI.indentLevel--;
}
for (int i = 0; i < 4; i++) {
char channelName = "RGBA"[i];
target.channels[i] = (RuleTexture.ChannelUse)EditorGUILayout.Popup("" + channelName, (int)target.channels[i], ChannelUseNames);
if (target.channels[i] != RuleTexture.ChannelUse.None) {
EditorGUI.indentLevel++;
if (target.channels[i] != RuleTexture.ChannelUse.Walkable) {
target.channelScales[i] = EditorGUILayout.FloatField("Scale", target.channelScales[i]);
}
string help = "";
switch (target.channels[i]) {
case RuleTexture.ChannelUse.Penalty:
help = "Penalty goes from 0 to " + target.channelScales[i].ToString("0") + " depending on the " + channelName + " channel value";
break;
case RuleTexture.ChannelUse.Position:
help = "Nodes will have their Y coordinate set to a value between 0 and " + target.channelScales[i].ToString("0") + " depending on the "+channelName+" channel";
if (graph.collision.heightCheck) {
EditorGUILayout.HelpBox("Height testing is enabled but the node positions will be overwritten by the texture data. You should disable either height testing or this feature.", MessageType.Error);
}
break;
case RuleTexture.ChannelUse.WalkablePenalty:
help = "If the "+channelName+" channel is 0, the node is made unwalkable. Otherwise the penalty goes from 0 to " + target.channelScales[i].ToString("0") + " depending on the " + channelName + " channel value";
break;
case RuleTexture.ChannelUse.Walkable:
help = "If the "+channelName+" channel is 0, the node is made unwalkable.";
break;
}
EditorGUILayout.HelpBox(help, MessageType.None);
if ((target.channels[i] == RuleTexture.ChannelUse.Penalty || target.channels[i] == RuleTexture.ChannelUse.WalkablePenalty) && target.channelScales[i] < 0) {
EditorGUILayout.HelpBox("Negative penalties are not supported. You can instead raise the penalty of other nodes.", MessageType.Error);
}
EditorGUI.indentLevel--;
}
}
}
static void SaveReferenceTexture (GridGraph graph) {
if (graph.nodes == null || graph.nodes.Length != graph.width * graph.depth * graph.LayerCount) {
AstarPath.active.Scan();
}
if (graph.nodes.Length < graph.width * graph.depth) {
Debug.LogError("Couldn't create reference image since nodes.Length < width*depth");
return;
}
if (graph.nodes.Length == 0) {
Debug.LogError("Couldn't create reference image since the graph is too small (0*0)");
return;
}
if (graph.LayerCount > 1) {
Debug.LogWarning("Creating reference image for a layered grid graph. Only the first layer will be included in the image.");
}
var tex = new Texture2D(graph.width, graph.depth);
float maxY = float.NegativeInfinity;
for (int i = 0; i < graph.nodes.Length; i++) {
Vector3 p = graph.nodes[i] != null? graph.transform.InverseTransform((Vector3)graph.nodes[i].position) : Vector3.zero;
maxY = p.y > maxY ? p.y : maxY;
}
var cols = new Color[graph.width*graph.depth];
for (int z = 0; z < graph.depth; z++) {
for (int x = 0; x < graph.width; x++) {
GraphNode node = graph.nodes[z*graph.width+x];
if (node != null) {
float v = node.Walkable ? 1F : 0.0F;
Vector3 p = graph.transform.InverseTransform((Vector3)node.position);
float q = p.y / maxY;
cols[z*graph.width+x] = new Color(v, q, 0);
} else {
cols[z*graph.width+x] = new Color(0, 0, 0);
}
}
}
tex.SetPixels(cols);
tex.Apply();
string path = AssetDatabase.GenerateUniqueAssetPath("Assets/gridReference.png");
System.IO.File.WriteAllBytes(path, tex.EncodeToPNG());
AssetDatabase.Refresh();
Object obj = AssetDatabase.LoadAssetAtPath(path, typeof(Texture));
EditorGUIUtility.PingObject(obj);
}
public void OnSceneGUI (GridGraph graph, GridGraphRule rule) { }
}
}
|