public static AssetLoadHelper Get(string assetName) { if (assetName == null) { throw new ArgumentNullException("assetName"); } if (assetName.Length == 0) { throw new ArgumentException("Asset name must not be empty.", "assetName"); } lock (AssetLoadHelpers) { AssetLoadHelper assetLoadHelper; if (!AssetLoadHelpers.TryGetValue(assetName, out assetLoadHelper)) { // Create and register new helper. assetLoadHelper = new AssetLoadHelper(assetName); AssetLoadHelpers[assetName] = assetLoadHelper; } assetLoadHelper.AddReference(); return(assetLoadHelper); } }
public FixupWrapper(AssetLoadHelper helper, Action <T> fixup) { Debug.Assert(helper != null, "The AssetLoadHelper must not be null."); Debug.Assert(fixup != null, "The fixup action must not be null."); _helper = helper; _fixup = fixup; }
protected override Mesh Read(ContentReader input, Mesh existingInstance) { if (existingInstance == null) existingInstance = new Mesh(); existingInstance.BoundingShape = input.ReadObject<Shape>(); int numberOfSubmeshes = input.ReadInt32(); for (int i = 0; i < numberOfSubmeshes; i++) { // Add submesh to Mesh.Submeshes before loading it. This is necessary // because the submesh tries to store its material in Mesh.Materials. Submesh submesh = new Submesh(); existingInstance.Submeshes.Add(submesh); input.ReadObject(submesh); } existingInstance.Name = input.ReadString(); bool hasOccluder = input.ReadBoolean(); if (hasOccluder) { using (var helper = AssetLoadHelper.Get(input.AssetName)) { input.ReadSharedResource(helper.Fixup<Occluder>(o => existingInstance.Occluder = o)); } } bool hasSkeleton = input.ReadBoolean(); if (hasSkeleton) { using (var helper = AssetLoadHelper.Get(input.AssetName)) { input.ReadSharedResource(helper.Fixup<Skeleton>(s => existingInstance.Skeleton = s)); } #else throw new ContentLoadException("Mesh contains a skeleton, but this build of DigitalRune Graphics does not support animations."); } var hasAnimations = input.ReadBoolean(); if (hasAnimations) { using (var helper = AssetLoadHelper.Get(input.AssetName)) { input.ReadSharedResource(helper.Fixup<Dictionary<string, SkeletonKeyFrameAnimation>>(a => existingInstance.Animations = a)); } #else throw new ContentLoadException("Mesh contains animations, but this build of DigitalRune Graphics does not support animations."); } input.ReadSharedResource<object>(userData => existingInstance.UserData = userData); return existingInstance; }
protected override Submesh Read(ContentReader input, Submesh existingInstance) { // The submesh is deserialized into an existing instance, where Submesh.Mesh is // already set! if (existingInstance == null) { throw new ArgumentNullException("existingInstance", "A submesh must be deserialized into an existing instance."); } using (var helper = AssetLoadHelper.Get(input.AssetName)) { input.ReadSharedResource(helper.Fixup <VertexBuffer>(vb => existingInstance.VertexBuffer = vb)); existingInstance.StartVertex = input.ReadInt32(); existingInstance.VertexCount = input.ReadInt32(); input.ReadSharedResource(helper.Fixup <IndexBuffer>(ib => existingInstance.IndexBuffer = ib)); existingInstance.StartIndex = input.ReadInt32(); existingInstance.PrimitiveCount = input.ReadInt32(); int numberOfMorphs = input.ReadInt32(); if (numberOfMorphs > 0) { var morphs = new MorphTargetCollection(); for (int i = 0; i < numberOfMorphs; i++) { morphs.Add(input.ReadObject <MorphTarget>()); } existingInstance.MorphTargets = morphs; } bool hasSharedMaterial = input.ReadBoolean(); if (hasSharedMaterial) { // Load external material, which is shared between models. existingInstance.SetMaterial(input.ReadExternalReference <Material>()); } else { // Load local material, which is only shared within the model. input.ReadSharedResource(helper.Fixup <Material>(existingInstance.SetMaterial)); } // No fixup helper for the UserData because the UserData can be null (see AssetLoadHelper)! input.ReadSharedResource <object>(userData => existingInstance.UserData = userData); } return(existingInstance); }
protected override MorphTarget Read(ContentReader input, MorphTarget existingInstance) { if (existingInstance == null) { existingInstance = new MorphTarget(); } using (var helper = AssetLoadHelper.Get(input.AssetName)) { existingInstance.Name = input.ReadString(); input.ReadSharedResource(helper.Fixup <VertexBuffer>(vb => existingInstance.VertexBuffer = vb)); existingInstance.StartVertex = input.ReadInt32(); } return(existingInstance); }
protected override ModelNode Read(ContentReader input, ModelNode existingInstance) { if (existingInstance == null) { existingInstance = new ModelNode(); } // Use AssetLoadHelper to receive an event when the asset (including all // shared resources) is loaded. using (var helper = AssetLoadHelper.Get(input.AssetName)) { input.ReadRawObject <SceneNode>(existingInstance); helper.AssetLoaded += existingInstance.OnAssetLoaded; } return(existingInstance); }
protected override OccluderNode Read(ContentReader input, OccluderNode existingInstance) { if (existingInstance == null) { existingInstance = new OccluderNode(); } // ----- SceneNode properties (base class). input.ReadRawObject <SceneNode>(existingInstance); // ----- OccluderNode properties using (var helper = AssetLoadHelper.Get(input.AssetName)) { input.ReadSharedResource(helper.Fixup <Occluder>(o => existingInstance.Occluder = o)); } return(existingInstance); }
protected override MeshNode Read(ContentReader input, MeshNode existingInstance) { if (existingInstance == null) { existingInstance = new MeshNode(); } // Use AssetLoadHelper to receive an event when the asset (including all // shared resources) is loaded. using (var helper = AssetLoadHelper.Get(input.AssetName)) { // ----- SceneNode properties (base class). input.ReadRawObject <SceneNode>(existingInstance); // ----- MeshNode properties input.ReadSharedResource(helper.Fixup <Mesh>(m => existingInstance.Mesh = m)); helper.AssetLoaded += existingInstance.OnAssetLoaded; } return(existingInstance); }