This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
forked from MaxRoetzler/IMP
-
Notifications
You must be signed in to change notification settings - Fork 7
/
ImposterBaker.cs
586 lines (496 loc) · 23.8 KB
/
ImposterBaker.cs
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System;
using System.IO;
using System.Collections.Generic;
[CanEditMultipleObjects]
public class ImposterBaker : MonoBehaviour
{
[System.Serializable]
private struct Snapshot
{
public Vector3 _position;
public Vector3 _direction;
}
private enum ImposterBakerPass : int
{
MinMax = 0,
AlphaCopy,
DepthCopy,
MergeNormalsDepth,
Dilate,
}
private enum UnityShaderPass
{
ForwardLit,
ShadowCaster,
GBuffer,
DepthOnly,
DepthNormals,
Meta,
}
[Header("Settings")]
[SerializeField] private int _atlasResolution = 2048;
[SerializeField] private bool _useHalfSphere = true;
[SerializeField] private int _frames = 12;
[SerializeField] [Range(0, 256)] private float _framePadding = 0;
[Header("Baked")]
[SerializeField] private Vector3 _boundsOffset;
[SerializeField] private float _boundsRadius;
[Header("System")]
// materials + shaders
[SerializeField] private Shader _imposterBakerShader;
[SerializeField] private Material _imposterBakerMaterial;
// rendering data
[SerializeField] private UnityEngine.Renderer[] _renderers;
[SerializeField] private Mesh[] _meshes;
[SerializeField] private Material[][] _materials;
[SerializeField] private Snapshot[] _snapshots;
[SerializeField] private Bounds _bounds;
bool CheckData()
{
// get materials/shaders
if (_imposterBakerShader == null)
{
_imposterBakerShader = Shader.Find("IMP/ImposterBaker");
_imposterBakerMaterial = new Material(_imposterBakerShader);
}
// find stuff to bake
List<MeshRenderer> renderers = new List<MeshRenderer>(transform.GetComponentsInChildren<MeshRenderer>(true));
renderers.Remove(gameObject.GetComponent<MeshRenderer>());
_renderers = renderers.ToArray();
_meshes = new Mesh[_renderers.Length];
_materials = new Material[_renderers.Length][];
for (int i = 0; i < _renderers.Length; i++)
{
_renderers[i].gameObject.SetActive(false);
_meshes[i] = _renderers[i].GetComponent<MeshFilter>().sharedMesh;
_materials[i] = _renderers[i].sharedMaterials;
}
// make sure frames are even
if (_frames % 2 != 0)
_frames -= 1;
// make sure min is 2 x 2
_frames = Mathf.Max(2, _frames);
//grow bounds, first centered on root transform
if (_renderers == null || _renderers.Length == 0)
return false;
_bounds = new Bounds(_renderers[0].transform.position, Vector3.zero);
for (int i = 0; i < _renderers.Length; i++)
{
Vector3[] verts = _meshes[i].vertices;
for (int v = 0; v < verts.Length; v++)
{
Vector3 meshWorldVert = _renderers[i].localToWorldMatrix.MultiplyPoint3x4(verts[v]);
Vector3 meshLocalToRoot = transform.worldToLocalMatrix.MultiplyPoint3x4(meshWorldVert);
Vector3 worldVert = transform.localToWorldMatrix.MultiplyPoint3x4(meshLocalToRoot);
_bounds.Encapsulate(worldVert);
}
}
_boundsRadius = Vector3.Distance(_bounds.min, _bounds.max) * 0.5f;
_boundsOffset = _bounds.center;
_snapshots = BuildSnapshots(_frames, _boundsRadius, _boundsOffset, _useHalfSphere);
return true;
}
[ContextMenu("Bake")]
void Bake()
{
// fix rotation bug ?
Vector3 oldPosition = transform.position;
Quaternion oldRotation = transform.rotation;
Vector3 oldScale = transform.localScale;
transform.position = Vector3.zero;
transform.rotation = Quaternion.identity;
transform.localScale = Vector3.one;
if (CheckData())
{
RenderTexture albedoPackRT = RenderTexture.GetTemporary(_atlasResolution, _atlasResolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
RenderTexture normalPackRT = RenderTexture.GetTemporary(_atlasResolution, _atlasResolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
CaptureViews(albedoPackRT, normalPackRT);
transform.position = oldPosition;
transform.rotation = oldRotation;
transform.localScale = oldScale;
CreateImposterAssets(albedoPackRT, normalPackRT);
RenderTexture.ReleaseTemporary(albedoPackRT);
RenderTexture.ReleaseTemporary(normalPackRT);
}
transform.position = oldPosition;
transform.rotation = oldRotation;
transform.localScale = oldScale;
}
void DrawMeshesToTarget(ImposterBakerPass pass)
{
for (int i = 0; i < _renderers.Length; i++)
{
for (int j = 0; j < _meshes[i].subMeshCount; j++)
{
_imposterBakerMaterial.SetPass((int)pass);
Graphics.DrawMeshNow(_meshes[i], _renderers[i].localToWorldMatrix, j);
}
}
}
void DrawMeshesToTarget(UnityShaderPass pass)
{
for (int i = 0; i < _renderers.Length; i++)
{
for (int j = 0; j < _meshes[i].subMeshCount; j++)
{
int passIndex = _materials[i][j].FindPass(Enum.GetName(typeof(UnityShaderPass), pass));
if (passIndex == -1)
continue;
_materials[i][j].SetPass(passIndex);
Graphics.DrawMeshNow(_meshes[i], _renderers[i].localToWorldMatrix, j);
}
}
}
void CaptureViews(RenderTexture albedoPackRT, RenderTexture normalPackRT)
{
int frameResolution = _atlasResolution / _frames;
// find better min max frame/boundsRadius size
{
RenderTexture minMaxTileRT = RenderTexture.GetTemporary(_atlasResolution, _atlasResolution, 0, RenderTextureFormat.R8, RenderTextureReadWrite.Linear);
GL.PushMatrix();
Graphics.SetRenderTarget(minMaxTileRT);
GL.Clear(true, true, Color.clear);
for (var i = 0; i < _snapshots.Length; i++)
{
Matrix4x4 ortho = Matrix4x4.Ortho(-_boundsRadius, _boundsRadius, -_boundsRadius, _boundsRadius, -_boundsRadius * 2, _boundsRadius * 2);
GL.LoadProjectionMatrix(ortho);
Matrix4x4 cameraMatrix = Matrix4x4.TRS(_snapshots[i]._position, Quaternion.LookRotation(_snapshots[i]._direction, Vector3.up), Vector3.one).inverse;
GL.modelview = cameraMatrix;
DrawMeshesToTarget(ImposterBakerPass.MinMax);
}
GL.PopMatrix();
//now read render texture
Texture2D tempMinMaxTex = new Texture2D(_atlasResolution, _atlasResolution, TextureFormat.R8, false);
RenderTexture.active = minMaxTileRT;
tempMinMaxTex.ReadPixels(new Rect(0f, 0f, _atlasResolution, _atlasResolution), 0, 0);
tempMinMaxTex.Apply();
Color32[] tempTexC = tempMinMaxTex.GetPixels32();
// start with max min
Vector2 min = Vector2.one * _atlasResolution;
Vector2 max = Vector2.zero;
//loop pixels get min max
for (int c = 0; c < tempTexC.Length; c++)
{
if (tempTexC[c].r != 0x00)
{
Vector2 texPos = Get2DIndex(c, _atlasResolution);
min.x = Mathf.Min(min.x, texPos.x);
min.y = Mathf.Min(min.y, texPos.y);
max.x = Mathf.Max(max.x, texPos.x);
max.y = Mathf.Max(max.y, texPos.y);
}
}
// padding
min -= Vector2.one * _framePadding;
max += Vector2.one * _framePadding;
//rescale radius
Vector2 len = new Vector2(max.x - min.x, max.y - min.y);
float maxR = Mathf.Max(len.x, len.y);
float ratio = maxR / _atlasResolution; //assume square
_boundsRadius = _boundsRadius * ratio;
//recalculate snapshots
_snapshots = BuildSnapshots(_frames, _boundsRadius, _boundsOffset, _useHalfSphere);
RenderTexture.ReleaseTemporary(minMaxTileRT);
}
// Render actual tiles
{
Shader.EnableKeyword("_RENDER_PASS_ENABLED");
GL.PushMatrix();
for (int frameIndex = 0; frameIndex < _snapshots.Length; frameIndex++)
{
// current snap
Snapshot currentSnapshot = _snapshots[frameIndex];
// setup camera pos to currentSnapshot
Matrix4x4 ortho = Matrix4x4.Ortho(-_boundsRadius, _boundsRadius, -_boundsRadius, _boundsRadius, 0, _boundsRadius * 2);
GL.LoadProjectionMatrix(ortho);
Matrix4x4 cameraMatrix = Matrix4x4.Inverse(Matrix4x4.TRS(currentSnapshot._position, Quaternion.LookRotation(currentSnapshot._direction, Vector3.up), new Vector3(1, 1, -1)));
GL.modelview = cameraMatrix;
// set buffers
RenderTexture[] gBuffer = new RenderTexture[5];
gBuffer[0] = RenderTexture.GetTemporary(frameResolution, frameResolution, 32, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
gBuffer[1] = RenderTexture.GetTemporary(frameResolution, frameResolution, 0, RenderTextureFormat.R8, RenderTextureReadWrite.Linear);
gBuffer[2] = RenderTexture.GetTemporary(frameResolution, frameResolution, 32, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
gBuffer[3] = RenderTexture.GetTemporary(frameResolution, frameResolution, 32, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
gBuffer[4] = RenderTexture.GetTemporary(frameResolution, frameResolution, 32, RenderTextureFormat.R16, RenderTextureReadWrite.Linear);
// get frame rts
RenderTexture depthFrameRT = RenderTexture.GetTemporary(frameResolution, frameResolution, 32, RenderTextureFormat.R16, RenderTextureReadWrite.Linear);
RenderTexture albedoFrameRT = RenderTexture.GetTemporary(frameResolution, frameResolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
RenderTexture normalFrameRT = RenderTexture.GetTemporary(frameResolution, frameResolution, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
RenderTexture dilateFrameRT = RenderTexture.GetTemporary(frameResolution, frameResolution, 0, RenderTextureFormat.R8, RenderTextureReadWrite.Linear);
// clear frame targets
Graphics.SetRenderTarget(depthFrameRT);
GL.Clear(true, true, Color.clear);
Graphics.SetRenderTarget(albedoFrameRT);
GL.Clear(true, true, Color.clear);
Graphics.SetRenderTarget(normalFrameRT);
GL.Clear(true, true, Color.clear);
Graphics.SetRenderTarget(dilateFrameRT);
GL.Clear(true, true, Color.clear);
for (int i = 0; i < gBuffer.Length; i++)
gBuffer[i].Create();
RenderBuffer[] buffers = new RenderBuffer[5];
for (int i = 0; i < gBuffer.Length; i++)
buffers[i] = gBuffer[i].colorBuffer;
// render gBuffers
Graphics.SetRenderTarget(buffers, gBuffer[0].depthBuffer);
GL.Clear(true, true, Color.clear);
DrawMeshesToTarget(UnityShaderPass.GBuffer);
// render dilateMask (alpha mask)
{
// send alpha to _dilateMaskRT
_imposterBakerMaterial.SetTexture("_AlphaMap", gBuffer[3]);
_imposterBakerMaterial.SetVector("_Channels", new Vector4(1, 0, 0, 0));
_imposterBakerMaterial.SetPass((int)ImposterBakerPass.AlphaCopy);
Graphics.Blit(gBuffer[3], dilateFrameRT, _imposterBakerMaterial, (int)ImposterBakerPass.AlphaCopy);
// send alpha to _albedoFrameRT
_imposterBakerMaterial.SetTexture("_AlphaMap", gBuffer[3]);
_imposterBakerMaterial.SetVector("_Channels", new Vector4(0, 0, 0, 1));
_imposterBakerMaterial.SetPass((int)ImposterBakerPass.AlphaCopy);
Graphics.Blit(gBuffer[3], albedoFrameRT, _imposterBakerMaterial, (int)ImposterBakerPass.AlphaCopy);
}
// render depth
{
_imposterBakerMaterial.SetTexture("_DepthMap", gBuffer[4]);
_imposterBakerMaterial.SetVector("_Channels", new Vector4(1, 0, 0, 0));
_imposterBakerMaterial.SetPass((int)ImposterBakerPass.DepthCopy);
Graphics.Blit(gBuffer[4], depthFrameRT, _imposterBakerMaterial, (int)ImposterBakerPass.DepthCopy);
}
// render albedo/color + normals + merge depth
{
// albedo
{
// dilate albedo
_imposterBakerMaterial.SetTexture("_DilateMask", dilateFrameRT);
_imposterBakerMaterial.SetTexture("_MainTex", gBuffer[0]);
_imposterBakerMaterial.SetVector("_Channels", new Vector4(1, 1, 1, 0));
_imposterBakerMaterial.SetPass((int)ImposterBakerPass.Dilate);
Graphics.Blit(gBuffer[0], albedoFrameRT, _imposterBakerMaterial, (int)ImposterBakerPass.Dilate);
}
// normals + depth
{
// merge normals + depth
RenderTexture tempNormalsDepthRT = RenderTexture.GetTemporary(frameResolution, frameResolution, 0, RenderTextureFormat.ARGBFloat, RenderTextureReadWrite.Linear);
_imposterBakerMaterial.SetTexture("_NormalMap", gBuffer[2]);
_imposterBakerMaterial.SetTexture("_DepthMap", depthFrameRT);
_imposterBakerMaterial.SetPass((int)ImposterBakerPass.MergeNormalsDepth);
Graphics.Blit(null, tempNormalsDepthRT, _imposterBakerMaterial, (int)ImposterBakerPass.MergeNormalsDepth);
// dilate normals & depth
_imposterBakerMaterial.SetTexture("_DilateMask", dilateFrameRT);
_imposterBakerMaterial.SetTexture("_MainTex", tempNormalsDepthRT);
_imposterBakerMaterial.SetVector("_Channels", new Vector4(1, 1, 1, 1));
_imposterBakerMaterial.SetPass((int)ImposterBakerPass.Dilate);
Graphics.Blit(tempNormalsDepthRT, normalFrameRT, _imposterBakerMaterial, (int)ImposterBakerPass.Dilate);
RenderTexture.ReleaseTemporary(tempNormalsDepthRT);
}
}
// blit to pack rts
{
//convert 1D index to flattened octahedra coordinate
int x;
int y;
//this is 0-(frames-1) ex, 0-(12-1) 0-11 (for 12 x 12 frames)
XYFromIndex(frameIndex, _frames, out x, out y);
//X Y position to write frame into atlas
//this would be frame index * frame width, ex 2048/12 = 170.6 = 170
//so 12 * 170 = 2040, loses 8 pixels on the right side of atlas and top of atlas
x *= albedoFrameRT.width;
y *= albedoFrameRT.height;
//copy base frame into base render target
Graphics.CopyTexture(albedoFrameRT, 0, 0, 0, 0, albedoFrameRT.width, albedoFrameRT.height, albedoPackRT, 0, 0, x, y);
//copy normals frame into normals render target
Graphics.CopyTexture(normalFrameRT, 0, 0, 0, 0, normalFrameRT.width, normalFrameRT.height, normalPackRT, 0, 0, x, y);
}
// dispose
RenderTexture.ReleaseTemporary(depthFrameRT);
RenderTexture.ReleaseTemporary(albedoFrameRT);
RenderTexture.ReleaseTemporary(normalFrameRT);
RenderTexture.ReleaseTemporary(dilateFrameRT);
// dispose
for (int i = 0; i < gBuffer.Length; i++)
RenderTexture.ReleaseTemporary(gBuffer[i]);
}
Shader.DisableKeyword("_RENDER_PASS_ENABLED");
GL.PopMatrix();
}
}
Snapshot[] BuildSnapshots(int frames, float radius, Vector3 origin, bool isHalf = true)
{
Snapshot[] snapshots = new Snapshot[frames * frames];
float framesMinusOne = frames - 1;
int i = 0;
for (int y = 0; y < frames; y++)
{
for (int x = 0; x < frames; x++)
{
Vector2 vec = new Vector2(
x / framesMinusOne * 2f - 1f,
y / framesMinusOne * 2f - 1f
);
Vector3 ray = isHalf ? OctahedralHemisphereCoordToVector(vec) : OctahedralSphereCoordToVector(vec);
ray = ray.normalized;
snapshots[i]._position = origin + ray * radius;
snapshots[i]._direction = -ray;
i++;
}
}
return snapshots;
}
Vector3 OctahedralHemisphereCoordToVector(Vector2 coord)
{
coord = new Vector2(coord.x + coord.y, coord.x - coord.y) * 0.5f;
Vector3 vec = new Vector3(
coord.x,
1.0f - Vector2.Dot(Vector2.one,
new Vector2(Mathf.Abs(coord.x), Mathf.Abs(coord.y))
),
coord.y
);
return Vector3.Normalize(vec);
}
Vector3 OctahedralSphereCoordToVector(Vector2 f)
{
Vector3 n = new Vector3(f.x, 1f - Mathf.Abs(f.x) - Mathf.Abs(f.y), f.y);
float t = Mathf.Clamp01(-n.y);
n.x += n.x >= 0f ? -t : t;
n.z += n.z >= 0f ? -t : t;
return n;
}
Vector2 Get2DIndex(int i, int res)
{
float x = i % res;
float y = (i - x) / res;
return new Vector2(x, y);
}
void XYFromIndex(int index, int dims, out int x, out int y)
{
x = index % dims;
y = (index - x) / dims;
}
string WriteTexture(Texture2D tex, string path)
{
byte[] bytes = tex.EncodeToPNG();
string fullPath = path + "/" + tex.name + ".png";
File.WriteAllBytes(fullPath, bytes);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
return fullPath;
}
Mesh CreateImposterMesh()
{
var vertices = new[]
{
new Vector3(0f, 0.0f, 0f),
new Vector3(-0.5f, 0.0f, -0.5f),
new Vector3(0.5f, 0.0f, -0.5f),
new Vector3(0.5f, 0.0f, 0.5f),
new Vector3(-0.5f, 0.0f, 0.5f)
};
var triangles = new[]
{
2, 1, 0,
3, 2, 0,
4, 3, 0,
1, 4, 0
};
var uv = new[]
{
//UV matched to verts
new Vector2(0.5f, 0.5f),
new Vector2(0.0f, 0.0f),
new Vector2(1.0f, 0.0f),
new Vector2(1.0f, 1.0f),
new Vector2(0.0f, 1.0f)
};
var normals = new[]
{
new Vector3(0f, 1f, 0f),
new Vector3(0f, 1f, 0f),
new Vector3(0f, 1f, 0f),
new Vector3(0f, 1f, 0f),
new Vector3(0f, 1f, 0f)
};
var mesh = new Mesh
{
vertices = vertices,
uv = uv,
normals = normals,
tangents = new Vector4[5]
};
mesh.SetTriangles(triangles, 0);
mesh.bounds = new Bounds(Vector3.zero + _boundsOffset, Vector3.one * _boundsRadius * 2f);
mesh.RecalculateTangents();
return mesh;
}
void CreateImposterAssets(RenderTexture albedoPackRT, RenderTexture normalPackRT)
{
// get path
string assetPath = EditorUtility.SaveFilePanelInProject("Save Imposter Textures", gameObject.name, "", "Select textures save location");
string directoryPath = Path.GetDirectoryName(assetPath);
// mesh
MeshRenderer meshRenderer;
if (!gameObject.TryGetComponent<MeshRenderer>(out meshRenderer))
meshRenderer = gameObject.AddComponent<MeshRenderer>();
MeshFilter meshFilter;
if (!gameObject.TryGetComponent<MeshFilter>(out meshFilter))
meshFilter = gameObject.AddComponent<MeshFilter>();
meshFilter.sharedMesh = CreateImposterMesh();
// textures
Texture2D albedoMapTexture = new Texture2D(albedoPackRT.width, albedoPackRT.height, TextureFormat.ARGB32, true, true);
albedoMapTexture.name = $"{gameObject.name}_ImposterAlbedoMap";
Graphics.SetRenderTarget(albedoPackRT);
albedoMapTexture.ReadPixels(new Rect(0f, 0f, albedoPackRT.width, albedoPackRT.height), 0, 0);
string albedoTexPath = WriteTexture(albedoMapTexture, directoryPath);
Texture2D normalMapTexture = new Texture2D(normalPackRT.width, normalPackRT.height, TextureFormat.ARGB32, true, true);
normalMapTexture.name = $"{gameObject.name}_ImposterNormalMap";
Graphics.SetRenderTarget(normalPackRT);
normalMapTexture.ReadPixels(new Rect(0f, 0f, normalPackRT.width, normalPackRT.height), 0, 0);
string normalTexPath = WriteTexture(normalMapTexture, directoryPath);
TextureImporter albedoTexImporter = AssetImporter.GetAtPath(albedoTexPath) as TextureImporter;
if (albedoTexImporter != null)
{
albedoTexImporter.textureType = TextureImporterType.Default;
albedoTexImporter.maxTextureSize = _atlasResolution;
albedoTexImporter.alphaSource = TextureImporterAlphaSource.FromInput;
albedoTexImporter.alphaIsTransparency = false;
albedoTexImporter.sRGBTexture = false;
albedoTexImporter.SaveAndReimport();
}
albedoMapTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(albedoTexPath);
TextureImporter normalTexImporter = AssetImporter.GetAtPath(normalTexPath) as TextureImporter;
if (normalTexImporter != null)
{
normalTexImporter.textureType = TextureImporterType.Default;
normalTexImporter.maxTextureSize = _atlasResolution;
normalTexImporter.alphaSource = TextureImporterAlphaSource.FromInput;
normalTexImporter.alphaIsTransparency = false;
normalTexImporter.sRGBTexture = false;
normalTexImporter.SaveAndReimport();
}
normalMapTexture = AssetDatabase.LoadAssetAtPath<Texture2D>(normalTexPath);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();
// create material
Shader imposterShader = Shader.Find("Shader Graphs/Imposter");
string materialName = $"{gameObject.name}_ImposterMaterial";
Material imposterMaterial = AssetDatabase.LoadAssetAtPath<Material>(directoryPath + "/" + materialName + ".asset");
if (imposterMaterial == null)
{
imposterMaterial = new Material(imposterShader);
imposterMaterial.name = materialName;
AssetDatabase.CreateAsset(imposterMaterial, directoryPath + "/" + imposterMaterial.name + ".asset");
}
imposterMaterial.SetTexture("_ImposterAlbedoMap", AssetDatabase.LoadAssetAtPath<Texture2D>(albedoTexPath));
imposterMaterial.SetTexture("_ImposterNormalMap", AssetDatabase.LoadAssetAtPath<Texture2D>(normalTexPath));
imposterMaterial.SetFloat("_ImposterIsHalfSphere", _useHalfSphere ? 1 : 0);
imposterMaterial.SetFloat("_ImposterFrames", _frames);
imposterMaterial.SetFloat("_ImposterSize", _boundsRadius);
imposterMaterial.SetVector("_ImposterOffset", _boundsOffset);
meshRenderer.sharedMaterial = imposterMaterial;
EditorUtility.SetDirty(this);
AssetDatabase.SaveAssets();
}
}
#endif