|
21 | 21 |
|
22 | 22 | namespace UnityEditor.SpeedTree.Importer
|
23 | 23 | {
|
24 |
| - [ScriptedImporter(1, "st9", AllowCaching = true)] |
| 24 | + // [2024-08-07] version: 2 |
| 25 | + // Fixed mesh's UV2 & UV3 data usage strategy to 'always allocate' from 'conditionally allocate' |
| 26 | + // to fix unwanted application of leaf-facing effect to geometries without leaf-facing data. |
| 27 | + |
| 28 | + [ScriptedImporter(version: 2, ext: "st9", AllowCaching = true)] |
25 | 29 | public class SpeedTree9Importer : ScriptedImporter
|
26 | 30 | {
|
27 | 31 | const int SPEEDTREE_9_WIND_VERSION = 1;
|
@@ -212,10 +216,10 @@ public override void OnImportAsset(AssetImportContext ctx)
|
212 | 216 |
|
213 | 217 | AddDependencyOnExtractedMaterials();
|
214 | 218 |
|
215 |
| - TriggerAllCabback(); |
| 219 | + TriggerAllCallbacks(); |
216 | 220 | }
|
217 | 221 |
|
218 |
| - private void TriggerAllCabback() |
| 222 | + private void TriggerAllCallbacks() |
219 | 223 | {
|
220 | 224 | var allMethods = AttributeHelper.GetMethodsWithAttribute<MaterialSettingsCallbackAttribute>().methodsWithAttributes;
|
221 | 225 | foreach (var method in allMethods)
|
@@ -259,7 +263,7 @@ internal void RegenerateMaterials()
|
259 | 263 |
|
260 | 264 | RegenerateAndPopulateExternalMaterials(this.assetPath);
|
261 | 265 |
|
262 |
| - TriggerAllCabback(); |
| 266 | + TriggerAllCallbacks(); |
263 | 267 | }
|
264 | 268 | finally
|
265 | 269 | {
|
@@ -346,17 +350,13 @@ private Mesh CreateMeshAndGeometry(Lod lod, int lodIndex)
|
346 | 350 |
|
347 | 351 | mesh.SetUVs(0, sTMeshGeometry.uvs[0]);
|
348 | 352 | mesh.SetUVs(1, sTMeshGeometry.uvs[1]);
|
349 |
| - |
350 | 353 | if (!isBillboard)
|
351 | 354 | {
|
352 |
| - if (m_HasBranch2Data || m_HasFacingData) // If Branch2 is available: |
353 |
| - { |
354 |
| - mesh.SetUVs(2, sTMeshGeometry.uvs[2]); // Branch2Pos, Branch2Dir, Branch2Weight, <Unused> |
355 |
| - } |
356 |
| - if (m_HasBranch2Data && m_HasFacingData) // If camera-facing geom is available: |
357 |
| - { |
358 |
| - mesh.SetUVs(3, sTMeshGeometry.uvs[3]); // 2/3 Anchor XYZ, FacingFlag |
359 |
| - } |
| 355 | + // SpeedTree shader expects certain UV2 & UV3 values for leaf facing & wind. |
| 356 | + // if we don't claim them here now, tree rendering may break when Unity |
| 357 | + // uses UV2 & UV3 and the shader finds unexpected values. |
| 358 | + mesh.SetUVs(2, sTMeshGeometry.uvs[2]); // Branch2Pos, Branch2Dir, Branch2Weight, <Unused> |
| 359 | + mesh.SetUVs(3, sTMeshGeometry.uvs[3]); // 2/3 Anchor XYZ, FacingFlag |
360 | 360 | }
|
361 | 361 |
|
362 | 362 | return mesh;
|
@@ -453,23 +453,23 @@ private void CalculateMeshGeometry(STMeshGeometry sTMeshGeometry, Lod lod, bool
|
453 | 453 |
|
454 | 454 | if (!isBillboard)
|
455 | 455 | {
|
456 |
| - if (m_HasBranch2Data) |
457 |
| - { |
458 |
| - sTMeshGeometry.uvs[currentUV++][i].Set( |
459 |
| - vertex.BranchWind2.X, |
460 |
| - vertex.BranchWind2.Y, |
461 |
| - vertex.BranchWind2.Z, |
462 |
| - 0.0f); |
463 |
| - } |
464 |
| - |
465 |
| - if (m_HasFacingData) |
466 |
| - { |
467 |
| - sTMeshGeometry.uvs[currentUV++][i].Set( |
468 |
| - vertex.Anchor.X * m_MeshSettings.scaleFactor, |
469 |
| - vertex.Anchor.Y * m_MeshSettings.scaleFactor, |
470 |
| - vertex.Anchor.Z * m_MeshSettings.scaleFactor, |
471 |
| - vertex.CameraFacing ? 1.0f : 0.0f); |
472 |
| - } |
| 456 | + float anchorX = m_HasFacingData ? vertex.Anchor.X * m_MeshSettings.scaleFactor : 0.0f; |
| 457 | + float anchorY = m_HasFacingData ? vertex.Anchor.Y * m_MeshSettings.scaleFactor : 0.0f; |
| 458 | + float anchorZ = m_HasFacingData ? vertex.Anchor.Z * m_MeshSettings.scaleFactor : 0.0f; |
| 459 | + float leafFacingFlag = vertex.CameraFacing ? 1.0f : 0.0f; |
| 460 | + |
| 461 | + sTMeshGeometry.uvs[currentUV++][i].Set( |
| 462 | + m_HasBranch2Data ? vertex.BranchWind2.X : anchorX, |
| 463 | + m_HasBranch2Data ? vertex.BranchWind2.Y : anchorY, |
| 464 | + m_HasBranch2Data ? vertex.BranchWind2.Z : anchorZ, |
| 465 | + m_HasBranch2Data ? 0.0f /*UNUSED*/ : leafFacingFlag); |
| 466 | + |
| 467 | + bool useUV3 = m_HasBranch2Data && m_HasFacingData; |
| 468 | + sTMeshGeometry.uvs[currentUV++][i].Set( |
| 469 | + useUV3 ? anchorX : 0.0f, |
| 470 | + useUV3 ? anchorY : 0.0f, |
| 471 | + useUV3 ? anchorZ : 0.0f, |
| 472 | + useUV3 ? leafFacingFlag : 0.0f); |
473 | 473 | }
|
474 | 474 | }
|
475 | 475 | }
|
@@ -502,15 +502,7 @@ private int CalculateNumUVs(bool isBillboard)
|
502 | 502 | int numUVs = 2;
|
503 | 503 | if (!isBillboard)
|
504 | 504 | {
|
505 |
| - if (m_HasBranch2Data) |
506 |
| - { |
507 |
| - numUVs += 1; |
508 |
| - } |
509 |
| - |
510 |
| - if (m_HasFacingData) |
511 |
| - { |
512 |
| - numUVs += 1; |
513 |
| - } |
| 505 | + numUVs += 2; // reserve UV2 & UV3 for 3D-geometry to detect leaf facing (VS effect) correctly |
514 | 506 | }
|
515 | 507 | return numUVs;
|
516 | 508 | }
|
|
0 commit comments