# STL written by vtkSTLWriter is not properly visible in Unity3D!

**URL:** <https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386>\
**Category:** Development\
**Created:** [July 19, 2019, 7:21am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386 "2019-07-19T07:21:47Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [July 19, 2019, 7:21am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/1 "2019-07-19T07:21:47Z")

</div>

Hello,

I am new to VTK and interested to make a CAD software. Recently I have been working with decimation. I have tried to write the reduced STL using vtkSTLWriter. But the exported STL is not correctly visible due to disturbed vertices.

Weird thing is if I import the same STL in blender, the export seems fine. And If I export an STL of the same vtkSTLWritten imported STL from blender, then the blender exported STL is visible fine in Unity3D.

Please help what I am missing here?

---

<div class="post-metadata">

**Author:** ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)\
**Post date:** [July 19, 2019, 1:44pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/2 "2019-07-19T13:44:57Z")

</div>

Welcome to VTK, @rai-sam. Could you add a screenshot that show what is going wrong?

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [July 20, 2019, 7:13am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/3 "2019-07-20T07:13:27Z")

</div>

![vtkIssue](https://discourse.vtk.org/uploads/default/original/1X/d742826d58526e74b01fc386fa026e92041cc78e.gif)

@cory.quammen Let me know if anything else needed at my end. Please help. 🙂

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [July 23, 2019, 6:53pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/4 "2019-07-23T18:53:13Z")

</div>

It looks like faces are not oriented correctly. You can use [vtkPolyDataNormals](https://vtk.org/doc/nightly/html/classvtkPolyDataNormals.html) to fix it (make sure “Consistency” is enabled).

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [July 24, 2019, 9:18am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/5 "2019-07-24T09:18:42Z")

</div>

@lassoan If I SetFlipNormals(true) using [vtkPolyDataNormals](https://vtk.org/doc/nightly/html/classvtkPolyDataNormals.html) then problem seemed solved. Is this fine practice? What could be the issue?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [July 24, 2019, 6:33pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/6 "2019-07-24T18:33:06Z")

</div>

Some filters may not preserve consistency of cell orientations, so I would consider it to be normal that you need to reorient (unless you analysed all input data sets and processing filters and based on that you would not expect it to be necessary).

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [July 25, 2019, 7:28am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/7 "2019-07-25T07:28:33Z")

</div>

@lassoan So what I am going to assume in this case is vtkSTLReader is somehow reading STL with flipped faces? So that before exporting, I need to perform setFlipNormals(true) everytime. Right?

---

<div class="post-metadata">

**Author:** ![lassoan](https://discourse.vtk.org/user_avatar/discourse.vtk.org/lassoan/32/50_2.png) [@lassoan](https://discourse.vtk.org/u/lassoan)\
**Post date:** [July 25, 2019, 6:13pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/8 "2019-07-25T18:13:32Z")

</div>

If triangle points may be ordered incorrectly in your input STL files then you can use vtkPolyDataNormals filter to fix them.

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [July 26, 2019, 10:49am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/9 "2019-07-26T10:49:24Z")

</div>

@lassoan I wrote a code to Flip normals in Unity3D after importing STL mesh.  
I have verified by test case of original input STL and vtk written STL files that vtk is causing this issue to output STL files. I discarded the use of vtkPolyDataNormals at vtkSTLWriter time.

Test Files:  
original.stl (input)  
original\_test.stl (export of original.stl by vtk)  
original\_test2.stl (export of original\_test.stl by vtk)  
original\_test3.stl (export of original\_test2.stl by vtk)

After importing all 4 STL files in Unity3D, I see original input STL all fine and all 3 vtk exported STL files faces incorrectly oriented. So I place the code to Flip normals in Unity3D for all, and this time original input STL faces were incorrectly oriented due to code and all 3 vtk exported STL files were fine as fixed by code.

My point is if I missing something at write time? Maybe stlWriter-\>Update()?

---

<div class="post-metadata">

**Author:** ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)\
**Post date:** [July 31, 2019, 11:15pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/10 "2019-07-31T23:15:36Z")

</div>

What is your full VTK processing pipeline? You can post code if you’d like or just list the reader/filters/writers are you have them connected.

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [August 1, 2019, 7:27am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/11 "2019-08-01T07:27:27Z")

</div>

I am using Tools:  
VTK v8.2.0  
Cmake 3.14.5  
Visual Studio 2015

Code:

```
#include <vtkPolyData.h>
#include <vtkSTLWriter.h>
#include <vtkSTLReader.h>
#include <vtkSmartPointer.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkRenderWindow.h>
#include <vtkRenderer.h>
#include <vtkRenderWindowInteractor.h>

using namespace std;

int main(int argc, char *argv[])
{
  if(argc != 3)
  {
    cout << "Required parameters: input Filename(.stl) OR output filename.stl" << endl;
    return EXIT_FAILURE;
  }

  string inputFileName = argv[1];
  string outputFileName = argv[2];

  // read input stl
  vtkSmartPointer<vtkSTLReader> inputReader = vtkSmartPointer<vtkSTLReader>::New();
  inputReader->SetFileName(inputFileName.c_str());
  inputReader->Update();

  // write output file
  vtkSmartPointer<vtkSTLWriter> stlWriter = vtkSmartPointer<vtkSTLWriter>::New();
  stlWriter->SetFileName(outputFileName.c_str());
  stlWriter->SetInputConnection(inputReader->GetOutputPort());
  stlWriter->Write();

  // Read and display for verification
  vtkSmartPointer<vtkSTLReader> outputReader = vtkSmartPointer<vtkSTLReader>::New();
  outputReader->SetFileName(outputFileName.c_str());
  outputReader->Update();

  vtkSmartPointer<vtkPolyDataMapper> mapper = vtkSmartPointer<vtkPolyDataMapper>::New();
  mapper->SetInputConnection(outputReader->GetOutputPort());

  vtkSmartPointer<vtkActor> actor = vtkSmartPointer<vtkActor>::New();
  actor->SetMapper(mapper);

  vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
  vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->AddRenderer(renderer);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(renderWindow);

  renderer->AddActor(actor);
  renderer->SetBackground(.3, .6, .3); // Background color green

  renderWindow->Render();
  renderWindowInteractor->Start();

  return EXIT_SUCCESS;
}
```

---

<div class="post-metadata">

**Author:** ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)\
**Post date:** [August 1, 2019, 11:17pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/12 "2019-08-01T23:17:34Z")

</div>

The code looks fine and straightforward. Saving and loading and STL file with VTK, I didn’t see any reorientation of the geometry faces taking place.

Could you share a small STL file that shows the odd behavior with this program?

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [August 2, 2019, 7:13am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/13 "2019-08-02T07:13:23Z")

</div>

I have sent you the STL file exported by running this same code. Check this file in Unity3D any version if you can as well.

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [August 27, 2019, 12:36pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/14 "2019-08-27T12:36:06Z")

</div>

@cory.quammen NO luck? ☹

---

<div class="post-metadata">

**Author:** ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)\
**Post date:** [September 9, 2019, 1:30pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/15 "2019-09-09T13:30:12Z")

</div>

Sorry, I’ve been completely swamped. Are you blocked by this issue?

---

<div class="post-metadata">

**Author:** ![rai-sam](https://discourse.vtk.org/user_avatar/discourse.vtk.org/rai-sam/32/667_2.png) [@rai-sam](https://discourse.vtk.org/u/rai-sam)\
**Post date:** [September 11, 2019, 7:17am UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/16 "2019-09-11T07:17:19Z")

</div>

@cory.quammen Yes! These STL exports should work for Unity3D as well. At-least for now.

I have a work around importing it to blender and then exporting. But that is 1 more step.

---

<div class="post-metadata">

**Author:** ![cory.quammen](https://discourse.vtk.org/user_avatar/discourse.vtk.org/cory.quammen/32/6751_2.png) [@cory.quammen](https://discourse.vtk.org/u/cory.quammen)\
**Post date:** [September 12, 2019, 1:57pm UTC](https://discourse.vtk.org/t/stl-written-by-vtkstlwriter-is-not-properly-visible-in-unity3d/1386/17 "2019-09-12T13:57:54Z")

</div>

I have looked at both your original STL files and the files converted by VTK. The only difference I see between the original files and VTK-produced files is that the original is a binary STL file and those saved out from the VTK STL writer are binary. Content-wise, they are exactly the same in terms of polygon winding, points, and normals, aside from some minor precision differences in the floating point numbers. Hence, there is no problem with VTK.

Perhaps the difference is in how Unity is importing binary vs. STL files. You can check whether this is the case by calling

`vtkSTLWriter::SetFileTypeToBinary()`

and saving out the VTK files that way. Hopefully Unity will treat normals in those files the same way as your original binary STL.
