-
Notifications
You must be signed in to change notification settings - Fork 877
Expand file tree
/
Copy pathVFXDataMesh.cs
More file actions
213 lines (181 loc) · 7.36 KB
/
Copy pathVFXDataMesh.cs
File metadata and controls
213 lines (181 loc) · 7.36 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Serialization;
using UnityEngine.VFX;
using System.Text;
namespace UnityEditor.VFX
{
class VFXDataMesh : VFXData
{
[SerializeField, FormerlySerializedAs("shader")]
private Shader m_Shader;
[SerializeField]
private string m_ShaderName;
public Shader shader
{
get
{
//This is needed for standard shaders ( for instance Unlit/Color ) that are not deserialized correctly during first import.
if (m_Shader == null && !object.ReferenceEquals(m_Shader, null) && !string.IsNullOrEmpty(m_ShaderName))
{
Shader newShader = Shader.Find(m_ShaderName);
if (newShader != null)
m_Shader = newShader;
}
return m_Shader;
}
set
{
if (m_Shader != value)
{
m_Shader = value;
DestroyCachedMaterial();
m_ShaderName = m_Shader != null ? m_Shader.name : null;
}
}
}
private Material m_CachedMaterial = null; // Transient material used to retrieve key words and properties
public override VFXDataType type { get { return VFXDataType.Mesh; } }
public override void OnEnable()
{
base.OnEnable();
if (object.ReferenceEquals(shader, null)) shader = VFXResources.defaultResources.shader;
if (m_Shader != null)
{
if (m_ShaderName != m_Shader.name)
{
m_ShaderName = m_Shader.name;
EditorUtility.SetDirty(this);
}
}
}
public virtual void OnDisable()
{
DestroyCachedMaterial();
}
public override void OnSRPChanged()
{
DestroyCachedMaterial();
}
public void RefreshShader()
{
DestroyCachedMaterial();
Invalidate(InvalidationCause.kSettingChanged);
}
private void DestroyCachedMaterial()
{
Material.DestroyImmediate(m_CachedMaterial);
m_CachedMaterial = null;
}
public override void CopySettings<T>(T dst)
{
VFXDataMesh other = dst as VFXDataMesh;
if (other != null)
other.shader = shader;
}
public override VFXDeviceTarget GetCompilationTarget(VFXContext context)
{
return VFXDeviceTarget.GPU;
}
public override bool CanBeCompiled()
{
return shader != null && m_Owners.Count == 1;
}
public Material GetOrCreateMaterial()
{
if (shader != null && (m_CachedMaterial == null || m_CachedMaterial.shader != shader))
{
m_CachedMaterial = new Material(shader);
m_CachedMaterial.hideFlags = HideFlags.HideAndDontSave;
VFXLibrary.currentSRPBinder?.SetupMaterial(m_CachedMaterial);
}
return m_CachedMaterial;
}
public override void FillDescs(
IVFXErrorReporter reporter,
VFXCompilationMode compilationMode,
List<VFXGPUBufferDesc> outBufferDescs,
List<VFXTemporaryGPUBufferDesc> outTemporaryBufferDescs,
List<VFXEditorSystemDesc> outSystemDescs,
VFXExpressionGraph expressionGraph,
VFXCompiledData compiledData,
IEnumerable<VFXContext> compilableContexts,
Dictionary<VFXContext, int> contextSpawnToBufferIndex,
VFXDependentBuffersData dependentBuffers,
Dictionary<VFXContext, List<VFXContextLink>[]> effectiveFlowInputLinks,
Dictionary<VFXData, uint> dataToSystemIndex,
VFXSystemNames systemNames = null)
{
var context = m_Owners[0];
foreach (var task in compiledData.contextToCompiledData[context].tasks)
{
var contextData = compiledData.taskToCompiledData[task];
var mappings = new List<VFXMapping>();
var uniforms = contextData.uniformMapper.uniforms;
uniforms = uniforms.Concat(contextData.uniformMapper.textures);
uniforms = uniforms.Concat(contextData.uniformMapper.buffers);
foreach (var uniform in uniforms)
{
int exprIndex = expressionGraph.GetFlattenedIndex(uniform);
foreach (var name in contextData.uniformMapper.GetNames(uniform))
mappings.Add(new VFXMapping(name, exprIndex));
}
var taskDesc = new VFXEditorTaskDesc()
{
externalProcessor = shader,
values = mappings.ToArray(),
type = (UnityEngine.VFX.VFXTaskType)VFXTaskType.Output,
model = context
};
mappings.Clear();
var mapper = contextData.cpuMapper;
// TODO Factorize that
var meshExp = mapper.FromNameAndId("mesh", -1);
var transformExp = mapper.FromNameAndId("transform", -1);
var subMaskExp = mapper.FromNameAndId("subMeshMask", -1);
int meshIndex = meshExp != null ? expressionGraph.GetFlattenedIndex(meshExp) : -1;
int transformIndex = transformExp != null ? expressionGraph.GetFlattenedIndex(transformExp) : -1;
int subMaskIndex = subMaskExp != null ? expressionGraph.GetFlattenedIndex(subMaskExp) : -1;
if (meshIndex != -1)
mappings.Add(new VFXMapping("mesh", meshIndex));
if (transformIndex != -1)
mappings.Add(new VFXMapping("transform", transformIndex));
if (subMaskIndex != -1)
mappings.Add(new VFXMapping("subMeshMask", subMaskIndex));
outSystemDescs.Add(new VFXEditorSystemDesc()
{
tasks = new VFXEditorTaskDesc[1] { taskDesc },
values = mappings.ToArray(),
type = VFXSystemType.Mesh,
layer = uint.MaxValue,
});
}
}
public override void Sanitize(int version)
{
if (shader == null && m_ShaderName == "Hidden/Default StaticMeshOutput")
{
shader = VFXResources.defaultResources.shader;
owners.OfType<VFXStaticMeshOutput>().First().Invalidate(InvalidationCause.kSettingChanged);
}
base.Sanitize(version);
}
public override void GenerateAttributeLayout(Dictionary<VFXContext, List<VFXContextLink>[]> effectiveFlowInputLinks)
{
}
public override string GetAttributeDataDeclaration(VFXAttributeMode mode)
{
throw new NotImplementedException();
}
public override string GetLoadAttributeCode(VFXAttribute attrib, VFXAttributeLocation location)
{
throw new NotImplementedException();
}
public override string GetStoreAttributeCode(VFXAttribute attrib, string value)
{
throw new NotImplementedException();
}
}
}