ApplyPostProcessing after Scene.ToUnmanagedScene() returns IntPtr.Zero
- Dominant language
- C#
- Stars
- 218
- Forks
- 83
- PR merge metrics
- No merged PRs in 30d
Description
Using AssimpNet 4.1.0 I want to load a model from file, clear it's normals and then apply post processing to regenerate smooth normals. For this to work I have to jump through hoops converting the scene from managed to unmanaged memory. The IntPtr to the unmanaged scene is getting corrupted and returned as Zero from `AssimpLibrary.Instance.AppyPostProcessing()` when it's called on the IntPtr that comes from `Scene.ToUnmanagedScene()`.
Using AssimpNet 5.0.0-beta1 it doesn't return a Ptr.Zero but crashes instead.
For the sake of keeping the examples smaller cleaning up of unmanaged memory is not included, but I have tested if that makes any difference and it doesn't from what I've seen.
Any help or even a different approach to force smooth normals would be greatly appreciated!
## Expected Behavior
Variable `ptr2` should have a non-zero value.
```
IntPtr ptr = Scene.ToUnmanagedScene(scene)
IntPtr ptr2 = AssimpLibrary.Instance.ApplyPostProcessing(ptr , PostProcessSteps.Whatever)
// Result: ptr2 != Ptr.Zero
```
## Current Behavior
Variable `ptr2` is always zero.
```
IntPtr ptr = Scene.ToUnmanagedScene(scene)
IntPtr ptr2 = AssimpLibrary.Instance.ApplyPostProcessing(ptr , PostProcessSteps.Whatever)
// Result: ptr2 == Ptr.Zero
```
## Steps to Reproduce
Importing as managed scene
```
// 1. Import the model without any additional processing
Scene scene = importer.ImportFile(file, PostProcessSteps.Triangulate);
// 2. Clear normals (uncommented for debugging)
//for(int i = 0; i < scene.Meshes.Count; i++)
//{
// scene.Meshes[i].Normals.Clear();
//}
// 3. Get pointer to unmanaged scene
IntPtr ptrBeforePP = Scene.ToUnmanagedScene(scene);
// 4. Apply post processing after manually manipulated the meshes
IntPtr ptrAfterPP = AssimpLibrary.Instance.ApplyPostProcessing(ptrBeforePP, PostProcessSteps.GenerateSmoothNormals);
```

Importing as unmanaged scene
```
// 1. Import the model as unmanaged scene
IntPtr ptrBeforeFirstPP = AssimpLibrary.Instance.ImportFile(file, PostProcessSteps.None, IntPtr.Zero, IntPtr.Zero);
// 2. Apply first postprocessing, returning a valid IntPtr
IntPtr ptrAfterFirstPP = AssimpLibrary.Instance.ApplyPostProcessing(ptrBeforeFirstPP, PostProcessSteps.Triangulate);
// 3. Convert unmanaged scene to managed scene and clear normals (uncommented for debugging)
Scene scene = Scene.FromUnmanagedScene(ptrAfterFirstPP);
//for(int i = 0; i < scene.Meshes.Count; i++)
//{
// scene.Meshes[i].Normals.Clear();
//}
// 4. Convert managed scene to unmanaged scene
IntPtr ptrBeforeSecondPP = Scene.ToUnmanagedScene(scene);
// 5. Apply second postprocessing, returning IntPtr.Zero
IntPtr ptrAfterSecondPP = AssimpLibrary.Instance.ApplyPostProcessing(ptrBeforeSecondPP, PostProcessSteps.GenerateSmoothNormals);
```

## Context (Environment)
- .NET Core 3.1
- Issue is independent from the model or file format being used
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by tracing Scene.ToUnmanagedScene() and AssimpLibrary.Instance.ApplyPostProcessing() using the two reproduction paths in the issue. Compare the managed-to-unmanaged scene conversion with the directly imported unmanaged scene, and verify that GenerateSmoothNormals receives a valid scene pointer without returning IntPtr.Zero or crashing.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- computer-graphics
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100