void PerformTexcoordCoordinateConversion(BrushDescriptor desc, Mesh mesh, int uvSet) { var semantic = GetUvsetSemantic(desc, uvSet); if (semantic == BrushDescriptor.Semantic.Vector || semantic == BrushDescriptor.Semantic.Position) { var size = GetUvsetSize(desc, uvSet); if (size == 3) { var data = new List <Vector3>(); mesh.GetUVs(uvSet, data); InPlaceUnityFromFbx(data); mesh.SetUVs(uvSet, data); } else if (size == 4) { var data = new List <Vector4>(); mesh.GetUVs(uvSet, data); InPlaceUnityFromFbx(data); mesh.SetUVs(uvSet, data); } else { LogWarningWithContext(string.Format( "Unexpected: Semantic {0} on texcoord of size {1}, guid {2}", semantic, size, desc.m_Guid)); } } }
// Try to find a Tilt Brush material using the imported models's material name Material OnAssignMaterialModel(Material material, Renderer renderer) { // Ignore models that aren't Tilt Brush - generated FBXs if (!IsTiltBrush) { return(null); } // For now, don't be strict about versions, because we can do an okay job on TB7 fbx files BrushDescriptor desc = GetDescriptorForStroke(material.name); if (desc != null) { // This is a stroke mesh and needs postprocessing. if (renderer.GetComponent <MeshFilter>() != null) { var mesh = renderer.GetComponent <MeshFilter>().sharedMesh; CollapseUvs(mesh); if (m_Info.tiltBrushVersion < new Version { major = 10 }) { // Old versions of TB use linear lighting ConvertSrgbToLinear(mesh); } if (m_Info.tiltBrushVersion.Value.major >= 12) { // Pre-TB12, Tilt Brush did unit conversion but not coord system conversion // when exporting texcoords with Position semantics. TB12 fixes this, so now // texcoord Position data are in fbx coordinates rather than Unity coordinates. // However, this means we need to convert from fbx -> Unity coords on import, // since Unity only takes care of mesh.vertices, tangents, normals, binormals. FixDataWithPositionSemantic(desc, mesh); } if (desc.m_IsParticle) { // Would like to do this in OnPreprocessModel, but we don't yet // know whether it's a particle mesh. var importer = assetImporter as ModelImporter; if (importer != null && importer.optimizeMesh) { // Should never get here any more. LogWarningWithContext("Disabling optimizeMesh and reimporting. Tilt Brush particle meshes must have optimizeMesh=false."); importer.optimizeMesh = false; importer.SaveAndReimport(); } ParticleMesh.FilterMesh(mesh, s => LogWarningWithContext(s, renderer)); } } return(desc.m_Material); } else { return(null); } }
void FixDataWithPositionSemantic(BrushDescriptor desc, Mesh mesh) { // We don't have full VertexLayout data in BrushDescriptor; currently, particles // are the only brushes that use Semantic.Position in texcoords if (desc.m_IsParticle) { List <Vector3> uv1 = new List <Vector3>(); mesh.GetUVs(1, uv1); for (int i = 0; i < uv1.Count; ++i) { uv1[i] = UnityFromFbx(uv1[i]); } mesh.SetUVs(1, uv1); } }
// Try to find a Tilt Brush material using the imported models's material name Material OnAssignMaterialModel(Material material, Renderer renderer) { // Ignore models that aren't Tilt Brush - generated FBXs if (!IsTiltBrush) { return(null); } // For now, don't be strict about versions, because we can do an okay job on TB7 fbx files BrushDescriptor desc = GetDescriptorForStroke(material.name); if (desc != null) { // This is a stroke mesh and needs postprocessing. if (renderer.GetComponent <MeshFilter>() != null) { var mesh = renderer.GetComponent <MeshFilter>().sharedMesh; CollapseUvs(mesh); if (m_Info.tiltBrushVersion < new Version { major = 10 }) { // Old versions of TB use linear lighting ConvertSrgbToLinear(mesh); } if (desc.m_IsParticle) { // Would like to do this in OnPreprocessModel, but we don't yet // know whether it's a particle mesh. var importer = assetImporter as ModelImporter; if (importer != null && importer.optimizeMesh) { // Should never get here any more. LogWarningWithContext("Disabling optimizeMesh and reimporting. Tilt Brush particle meshes must have optimizeMesh=false."); importer.optimizeMesh = false; importer.SaveAndReimport(); } ParticleMesh.FilterMesh(mesh, s => LogWarningWithContext(s, renderer)); } } return(desc.m_Material); } else { return(null); } }
void FixupHypercolorTexcoord(BrushDescriptor desc, Mesh mesh) { int uvSet = 0; var semantic = GetUvsetSemantic(desc, uvSet); if (semantic != BrushDescriptor.Semantic.XyIsUvZIsDistance || GetUvsetSize(desc, uvSet) != 3) { LogWarningWithContext("Not hypercolor?"); } var data = new List <Vector3>(); mesh.GetUVs(uvSet, data); for (int i = 0; i < data.Count; ++i) { Vector3 tmp = data[i]; tmp.z *= .1f; data[i] = tmp; } mesh.SetUVs(uvSet, data); }
static int GetUvsetSize(BrushDescriptor desc, int uvSet) { switch (uvSet) { case 0: return(desc.m_uv0Size); case 1: // Unity's mesh importer destroys non-unit data in fbx.normals, so Tilt Brush // sometimes routes mesh.normals through fbx.texcoord1 instead. if (desc.m_bFbxExportNormalAsTexcoord1) { return(3); } else { return(desc.m_uv1Size); } default: throw new ArgumentException("uvSet"); } }
// Try to find a Tilt Brush material using the imported models's material name Material OnAssignMaterialModel(Material material, Renderer renderer) { // Ignore models that aren't Tilt Brush - generated FBXs if (!IsSupportedTiltBrushFbx(assetPath)) { return(null); } BrushDescriptor desc = GetDescriptorForStroke(material.name); if (desc != null) { // This is a stroke mesh and needs postprocessing. if (renderer.GetComponent <MeshFilter>() != null) { var mesh = renderer.GetComponent <MeshFilter>().sharedMesh; CollapseUvs(mesh); if (desc.m_IsParticle) { // Would like to do this in OnPreprocessModel, but we don't yet // know whether it's a particle mesh. var importer = assetImporter as ModelImporter; if (importer != null && importer.optimizeMesh) { LogWarningWithContext("Disabling optimizeMesh and reimporting. Tilt Brush particle meshes must have optimizeMesh=false."); importer.optimizeMesh = false; importer.SaveAndReimport(); } ParticleMesh.FilterMesh(mesh, s => LogWarningWithContext(s, renderer)); } } return(desc.m_Material); } else { return(null); } }
// Try to find a Tilt Brush material using the imported models's material name. Material OnAssignMaterialModel(Material material, Renderer renderer) { // This gets called once for each (Renderer, Material) pair. // However, Unity passes a fresh Material every time, even if two FbxNodes use the // same FbxMaterial. Therefore we can't distinguish between "two unique materials with // the same name" and "one material being used multiple times". // Therefore we have to rely on the exporter using distinct names. // Ignore models that aren't Tilt Brush - generated FBXs if (!IsTiltBrush) { return(null); } // For now, don't be strict about versions, because we can do an okay job on TB7 fbx files BrushDescriptor desc = GetDescriptorForStroke(material.name); if (desc != null) { if (IsTemplateDescriptor(desc)) { // Replace shader with our own so we get culling and so on. // First 32 characters are the guid and underscore material.name = material.name.Substring(33); material.shader = desc.m_Material.shader; // Our shaders don't use "_MainTex", so we need to find the correct property name. SetFirstShaderTexture(material, material.mainTexture); // If we return null, Unity will ignore our material mutations. // If we return the material, Unity complains it's not an asset instead of making it one // and embedding it in the fbx. // So create one explicitly. return(GetOrCreateAsset(material, this.assetPath)); } // This is a stroke mesh and needs postprocessing. if (renderer.GetComponent <MeshFilter>() != null) { var mesh = renderer.GetComponent <MeshFilter>().sharedMesh; CollapseUvs(mesh); if (m_Info.tiltBrushVersion < new Version { major = 10 }) { // Old versions of TB use linear lighting ConvertSrgbToLinear(mesh); } if (m_Info.tiltBrushVersion.Value.major >= 12) { // Pre-TB12, Tilt Brush did unit conversion but not coord system conversion // when exporting texcoords with Position semantics. TB12 fixes this, so now // texcoord Position data are in fbx coordinates rather than Unity coordinates. // However, this means we need to convert from fbx -> Unity coords on import, // since Unity only takes care of mesh.vertices, tangents, normals, binormals. PerformTexcoordCoordinateConversion(desc, mesh, 0); PerformTexcoordCoordinateConversion(desc, mesh, 1); } if (m_Info.tiltBrushVersion.Value.major < 14 && desc.m_Guid == new Guid("e8ef32b1-baa8-460a-9c2c-9cf8506794f5")) { // Pre-TB14, Tilt Brush hadn't set the "Z is distance" semantic and texcoord0.z // ended up in decimeters FixupHypercolorTexcoord(desc, mesh); } if (desc.m_IsParticle) { // Would like to do this in OnPreprocessModel, but we don't yet // know whether it's a particle mesh. var importer = assetImporter as ModelImporter; #if UNITY_2019_1_OR_NEWER if (importer != null && importer.optimizeMeshVertices) { // Should never get here any more. LogWarningWithContext("Disabling optimizeMesh and reimporting. Tilt Brush particle meshes must have importer.optimizeMeshVertices = false."); importer.optimizeMeshVertices = false; importer.SaveAndReimport(); } #else if (importer != null && importer.optimizeMesh) { // Should never get here any more. LogWarningWithContext("Disabling optimizeMesh and reimporting. Tilt Brush particle meshes must have optimizeMesh=false."); importer.optimizeMesh = false; importer.SaveAndReimport(); } #endif ParticleMesh.FilterMesh(mesh, s => LogWarningWithContext(s, renderer)); } } return(desc.m_Material); } else { return(null); } }
// Returns true if this is a BrushDescriptor meant to be cloned and customized rather // than used directly, to support things like gltf PbrMetallicRoughness or // double-sided/non-culling materials in fbx bool IsTemplateDescriptor(BrushDescriptor desc) { return(desc.name.StartsWith("Pbr")); // hacky but works for now }
/// <returns>null if not found</returns> public bool TryGetBrush(Guid guid, out BrushDescriptor desc) { return(m_BrushManifest.BrushesByGuid.TryGetValue(guid, out desc)); }