public override void OnImportAsset(AssetImportContext ctx)
        {
            if (ctx == null)
            {
                return;
            }

            var path = ctx.assetPath;

            AlembicStream.DisconnectStreamsWithPath(path);

            var fileName = Path.GetFileNameWithoutExtension(path);
            var go       = new GameObject(fileName);

            var streamDescriptor = ScriptableObject.CreateInstance <AlembicStreamDescriptor>();

            streamDescriptor.name      = go.name + "_ABCDesc";
            streamDescriptor.PathToAbc = path;
            streamDescriptor.Settings  = StreamSettings;

            using (var abcStream = new AlembicStream(go, streamDescriptor))
            {
                abcStream.AbcLoad(true, true);

                abcStream.GetTimeRange(ref abcStartTime, ref abcEndTime);
                if (firstImport)
                {
                    startTime = abcStartTime;
                    endTime   = abcEndTime;
                }
                streamDescriptor.abcStartTime = abcStartTime;
                streamDescriptor.abcEndTime   = abcEndTime;

                var streamPlayer = go.AddComponent <AlembicStreamPlayer>();
                streamPlayer.StreamDescriptor = streamDescriptor;
                streamPlayer.StartTime        = StartTime;
                streamPlayer.EndTime          = EndTime;

                var subassets = new Subassets(ctx);
                subassets.Add(streamDescriptor.name, streamDescriptor);
                GenerateSubAssets(subassets, abcStream.abcTreeRoot, streamDescriptor);

                AlembicStream.ReconnectStreamsWithPath(path);

                if (string.IsNullOrEmpty(rootGameObjectId))
                {
                    rootGameObjectId = fileName;
                    EditorUtility.SetDirty(this);
                }

#if UNITY_2017_3_OR_NEWER
                ctx.AddObjectToAsset(rootGameObjectId, go);
                ctx.SetMainObject(go);
#else
                ctx.SetMainAsset(rootGameObjectId, go);
#endif
            }

            firstImport = false;
        }
Example #2
0
        private void GenerateSubAssets(Subassets subassets, AlembicTreeNode root, AlembicStreamDescriptor streamDescr)
        {
            if (streamDescr.duration > 0)
            {
                var frames = new Keyframe[2];
                frames[0].value      = 0.0f;
                frames[0].time       = 0.0f;
                frames[0].outTangent = 1.0f;
                frames[1].value      = (float)streamDescr.duration;
                frames[1].time       = (float)streamDescr.duration;
                frames[1].inTangent  = 1.0f;

                var curve = new AnimationCurve(frames);
                AnimationUtility.SetKeyLeftTangentMode(curve, 0, AnimationUtility.TangentMode.Linear);
                AnimationUtility.SetKeyRightTangentMode(curve, 1, AnimationUtility.TangentMode.Linear);

                var animationClip = new AnimationClip();
                animationClip.SetCurve("", typeof(AlembicStreamPlayer), "currentTime", curve);
                animationClip.name      = root.gameObject.name + "_Clip";
                animationClip.hideFlags = HideFlags.NotEditable;

                subassets.Add("Default Animation", animationClip);
            }
            varyingTopologyMeshNames = new List <string>();
            splittingMeshNames       = new List <string>();

            CollectSubAssets(subassets, root);

            streamDescr.hasVaryingTopology = varyingTopologyMeshNames.Count > 0;
        }
        void CollectSubAssets(Subassets subassets, AlembicTreeNode node)
        {
            var mesh = node.GetAlembicObj <AlembicMesh>();

            if (mesh != null)
            {
                var sum = mesh.summary;
                if (mesh.summary.topologyVariance == aiTopologyVariance.Heterogeneous)
                {
                    varyingTopologyMeshNames.Add(node.gameObject.name);
                }
                else if (mesh.sampleSummary.splitCount > 1)
                {
                    splittingMeshNames.Add(node.gameObject.name);
                }
            }

            int submeshCount = 0;
            var meshFilter   = node.gameObject.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                var m = meshFilter.sharedMesh;
                submeshCount = m.subMeshCount;
                m.name       = node.gameObject.name;
                subassets.Add(node.abcObject.abcObject.fullname, m);
            }

            var renderer = node.gameObject.GetComponent <MeshRenderer>();

            if (renderer != null)
            {
                var mats = new Material[submeshCount];
                for (int i = 0; i < submeshCount; ++i)
                {
                    mats[i] = subassets.defaultMaterial;
                }
                renderer.sharedMaterials = mats;
            }

            var apr = node.gameObject.GetComponent <AlembicPointsRenderer>();

            if (apr != null)
            {
                var cubeGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
                apr.sharedMesh = cubeGO.GetComponent <MeshFilter>().sharedMesh;
                DestroyImmediate(cubeGO);

                apr.sharedMaterials      = new Material[] { subassets.pointsMaterial };
                apr.motionVectorMaterial = subassets.pointsMotionVectorMaterial;
            }

            foreach (var child in node.children)
            {
                CollectSubAssets(subassets, child);
            }
        }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var shortAssetPath = MakeShortAssetPath(ctx.assetPath);

            AlembicStream.DisconnectStreamsWithPath(shortAssetPath);
            var sourcePath    = Application.dataPath + shortAssetPath;
            var destPath      = Application.streamingAssetsPath + shortAssetPath;
            var directoryPath = Path.GetDirectoryName(destPath);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
            if (File.Exists(destPath))
            {
                File.SetAttributes(destPath, FileAttributes.Normal);
            }
            File.Copy(sourcePath, destPath, true);

            var fileName = Path.GetFileNameWithoutExtension(destPath);
            var go       = new GameObject(fileName);

            var streamDescriptor = ScriptableObject.CreateInstance <AlembicStreamDescriptor>();

            streamDescriptor.name      = go.name + "_ABCDesc";
            streamDescriptor.pathToAbc = shortAssetPath;
            streamDescriptor.settings  = streamSettings;

            using (var abcStream = new AlembicStream(go, streamDescriptor))
            {
                abcStream.AbcLoad(true);

                abcStream.GetTimeRange(ref startTime, ref endTime);
                streamDescriptor.abcStartTime = abcStartTime = startTime;
                streamDescriptor.abcEndTime   = abcEndTime = endTime;

                var streamPlayer = go.AddComponent <AlembicStreamPlayer>();
                streamPlayer.streamDescriptor = streamDescriptor;
                streamPlayer.startTime        = startTime;
                streamPlayer.endTime          = endTime;

                var subassets = new Subassets(ctx);
                subassets.Add(streamDescriptor.name, streamDescriptor);
                GenerateSubAssets(subassets, abcStream.abcTreeRoot, streamDescriptor);

                AlembicStream.ReconnectStreamsWithPath(shortAssetPath);

#if UNITY_2017_3_OR_NEWER
                ctx.AddObjectToAsset(go.name, go);
                ctx.SetMainObject(go);
#else
                ctx.SetMainAsset(go.name, go);
#endif
            }
        }
        private void CollectSubAssets(Subassets subassets, OpenVDBStream stream, OpenVDBStreamDescriptor descriptor)
        {
            var     go      = stream.gameObject;
            Texture texture = null;

            if (descriptor.settings.extractTextures)
            {
                texture = descriptor.settings.textures.First();
                AddRemap(new SourceAssetIdentifier(typeof(Texture), texture.name), texture);
            }
            else
            {
                if (stream.texture3D != null)
                {
                    texture = stream.texture3D;
                    subassets.Add(stream.texture3D.name, stream.texture3D);
                }
            }

            var meshFilter = go.GetOrAddComponent <MeshFilter>();

            if (meshFilter != null)
            {
                meshFilter.sharedMesh      = stream.mesh;
                meshFilter.sharedMesh.name = go.name;
                subassets.Add(meshFilter.sharedMesh.name, meshFilter.sharedMesh);
            }
            var renderer = go.GetOrAddComponent <MeshRenderer>();

            if (renderer == null)
            {
                return;
            }
            if (!descriptor.settings.importMaterials)
            {
                return;
            }
            if (descriptor.settings.extractMaterials)
            {
                var material = descriptor.settings.materials.First();
                AddRemap(new SourceAssetIdentifier(typeof(Material), material.name), material);
                renderer.sharedMaterial = material;
            }
            else
            {
                renderer.sharedMaterial = subassets.defaultMaterial;
            }

            if (texture == null)
            {
                return;
            }
            renderer.sharedMaterial.SetTexture("_Volume", texture);
            renderer.sharedMaterial.name = texture.name;
        }
        void GenerateSubAssets(Subassets subassets, AlembicTreeNode root, AlembicStreamDescriptor streamDescr)
        {
            if (streamDescr.duration > 0)
            {
                // AnimationClip for time
                {
                    var frames = new Keyframe[2];
                    frames[0].value      = 0.0f;
                    frames[0].time       = 0.0f;
                    frames[0].outTangent = 1.0f;
                    frames[1].value      = (float)streamDescr.duration;
                    frames[1].time       = (float)streamDescr.duration;
                    frames[1].inTangent  = 1.0f;

                    var curve = new AnimationCurve(frames);
                    AnimationUtility.SetKeyLeftTangentMode(curve, 0, AnimationUtility.TangentMode.Linear);
                    AnimationUtility.SetKeyRightTangentMode(curve, 1, AnimationUtility.TangentMode.Linear);

                    var clip = new AnimationClip();
                    clip.SetCurve("", typeof(AlembicStreamPlayer), "currentTime", curve);
                    clip.name      = root.gameObject.name + "_Time";
                    clip.hideFlags = HideFlags.NotEditable;

                    subassets.Add("Default Animation", clip);
                }

                // AnimationClip for frame events
                {
                    var abc = root.stream.abcContext;
                    var n   = abc.timeSamplingCount;
                    for (int i = 1; i < n; ++i)
                    {
                        var clip = new AnimationClip();
                        if (AddFrameEvents(clip, abc.GetTimeSampling(i)))
                        {
                            var name = root.gameObject.name + "_Frames";
                            if (n > 2)
                            {
                                name += i.ToString();
                            }
                            clip.name = name;
                            subassets.Add(clip.name, clip);
                        }
                    }
                }
            }
            varyingTopologyMeshNames = new List <string>();
            splittingMeshNames       = new List <string>();

            CollectSubAssets(subassets, root);

            streamDescr.HasVaryingTopology = VaryingTopologyMeshNames.Count > 0;
        }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            var shortAssetPath = MakeShortAssetPath(ctx.assetPath);
            var sourcePath     = SourcePath(shortAssetPath);
            var destPath       = Path.Combine(Application.streamingAssetsPath, shortAssetPath);
            var directoryPath  = Path.GetDirectoryName(destPath);

            if (!Directory.Exists(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
            if (File.Exists(destPath))
            {
                File.SetAttributes(destPath, FileAttributes.Normal);
            }
            File.Copy(sourcePath, destPath, true);

            var fileName = Path.GetFileNameWithoutExtension(destPath);
            var go       = new GameObject(fileName);

            var streamDescriptor = ScriptableObject.CreateInstance <OpenVDBStreamDescriptor>();

            streamDescriptor.name      = go.name + "_VDBDesc";
            streamDescriptor.pathToVDB = shortAssetPath;
            streamDescriptor.settings  = streamSettings;

            using (var vdbStream = new OpenVDBStream(go, streamDescriptor))
            {
                if (!vdbStream.Load())
                {
                    return;
                }

                var subassets = new Subassets(ctx);
                subassets.Add(streamDescriptor.name, streamDescriptor);
                GenerateSubAssets(subassets, vdbStream, streamDescriptor);

#if UNITY_2017_3_OR_NEWER
                ctx.AddObjectToAsset(go.name, go);
                ctx.SetMainObject(go);
#else
                ctx.SetMainAsset(go.name, go);
#endif
            }
        }
        void CollectSubAssets(Subassets subassets, AlembicTreeNode node)
        {
            int submeshCount = 0;
            var meshFilter   = node.gameObject.GetComponent <MeshFilter>();

            if (meshFilter != null)
            {
                var m = meshFilter.sharedMesh;
                submeshCount = m.subMeshCount;
                m.name       = node.gameObject.name;
                subassets.Add(node.abcObject.abcObject.fullname, m);
            }

            var renderer = node.gameObject.GetComponent <MeshRenderer>();

            if (renderer != null)
            {
                var mats = new Material[submeshCount];
                for (int i = 0; i < submeshCount; ++i)
                {
                    mats[i] = subassets.defaultMaterial;
                }
                renderer.sharedMaterials = mats;
            }

            var apr = node.gameObject.GetComponent <AlembicPointsRenderer>();

            if (apr != null)
            {
                var cubeGO = GameObject.CreatePrimitive(PrimitiveType.Cube);
                apr.InstancedMesh = cubeGO.GetComponent <MeshFilter>().sharedMesh;
                DestroyImmediate(cubeGO);

                apr.Materials = new List <Material> {
                    subassets.pointsMaterial
                };
                apr.MotionVectorMaterial = subassets.pointsMotionVectorMaterial;
            }

            foreach (var child in node.Children)
            {
                CollectSubAssets(subassets, child);
            }
        }
        public override void OnImportAsset(AssetImportContext ctx)
        {
            if (ctx == null)
            {
                return;
            }

            var path = ctx.assetPath;

            AlembicStream.DisconnectStreamsWithPath(path);

            var fileName       = Path.GetFileNameWithoutExtension(path);
            var previousGoName = fileName;

            if (!string.IsNullOrEmpty(rootGameObjectName))
            {
                previousGoName = rootGameObjectName;
            }
            var go = new GameObject(previousGoName);

            var streamDescriptor = ScriptableObject.CreateInstance <AlembicStreamDescriptor>();

            streamDescriptor.name      = go.name + "_ABCDesc";
            streamDescriptor.PathToAbc = path;
            streamDescriptor.Settings  = StreamSettings;

            using (var abcStream = new AlembicStream(go, streamDescriptor))
            {
                abcStream.AbcLoad(true, true);
                abcStream.GetTimeRange(out abcStartTime, out abcEndTime);
                if (firstImport)
                {
                    startTime = abcStartTime;
                    endTime   = abcEndTime;
                }
                streamDescriptor.mediaStartTime = (float)abcStartTime;
                streamDescriptor.mediaEndTime   = (float)abcEndTime;

                var streamPlayer = go.AddComponent <AlembicStreamPlayer>();
                streamPlayer.StreamDescriptor = streamDescriptor;
                streamPlayer.StartTime        = (float)StartTime;
                streamPlayer.EndTime          = (float)EndTime;

                var subassets = new Subassets(ctx);
                subassets.Add(streamDescriptor.name, streamDescriptor);
                GenerateSubAssets(subassets, abcStream.abcTreeRoot, streamDescriptor);

                AlembicStream.ReconnectStreamsWithPath(path);

                var prevIdName = fileName;
                if (!string.IsNullOrEmpty(rootGameObjectId))
                {
                    prevIdName = rootGameObjectId;
                }

                ctx.AddObjectToAsset(prevIdName, go);
                ctx.SetMainObject(go);
                isHDF5 = abcStream.IsHDF5();
                if (IsHDF5)
                {
                    Debug.LogWarning(path + ": Deprecated HDF5 file format. Consider converting to Ogawa.");
                }
            }

            firstImport = false;
        }
 private void GenerateSubAssets(Subassets subassets, OpenVDBStream stream, OpenVDBStreamDescriptor descriptor)
 {
     CollectSubAssets(subassets, stream, descriptor);
 }