vpenades / SharpGLTF

glTF reader and writer for .NET Standard
MIT License
457 stars 72 forks source link

.NET framework support on SharphGLTF.Toolkit? #119

Closed DanielAbalde closed 2 years ago

DanielAbalde commented 2 years ago

Hi!

It won't let me use SharphGLTF.Toolkit within a .NET framework 4.7.1 project,

code:


  var material1 = new MaterialBuilder()
      .WithDoubleSide(true)
      .WithMetallicRoughnessShader()
      .WithChannelParam("BaseColor", new Vector4(1, 0, 0, 1));

  var material2 = new MaterialBuilder()
      .WithDoubleSide(true)
      .WithMetallicRoughnessShader()
      .WithChannelParam("BaseColor", new Vector4(1, 0, 1, 1));

  // create a mesh with two primitives, one for each material

  var mesh = new MeshBuilder<VERTEX>("mesh");

  var prim = mesh.UsePrimitive(material1);
  prim.AddTriangle(new VERTEX(-10, 0, 0), new VERTEX(10, 0, 0), new VERTEX(0, 10, 0));
  prim.AddTriangle(new VERTEX(10, 0, 0), new VERTEX(-10, 0, 0), new VERTEX(0, -10, 0));

  prim = mesh.UsePrimitive(material2);
  prim.AddQuadrangle(new VERTEX(-5, 0, 3), new VERTEX(0, -5, 3), new VERTEX(5, 0, 3), new VERTEX(0, 5, 3));

  // create a scene

  var scene = new SharpGLTF.Scenes.SceneBuilder();

  scene.AddRigidMesh(mesh, Matrix4x4.Identity);

  // save the model in different formats

  var model = scene.ToGltf2();
  model.SaveAsWavefront("mesh.obj");
  model.SaveGLB("mesh.glb");
  model.SaveGLTF("mesh.gltf");

error:

CS1705 Assembly 'SharpGLTF.Toolkit' with identity 'SharpGLTF.Toolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Runtime' with identity 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Some help please?

Thanks!

vpenades commented 2 years ago

SharpGLTF toolkit nuget package is NetStandard2.0 so it is guaranteed to be compatible with net471, in fact, the unit tests of sharpgltf also run with net471

So this might be a problem in the configuration of your project. Usual things to check:

In general, the solution is to explicitly reference system.runtime 5.0.0.0, but by all means this should not be necessary if the project is correctly set.

DanielAbalde commented 2 years ago

Which Visual studio version are you using: recomended 2022, minimum 2019

v 2019.

your csproj is new sdk style? or old verbose style?

I don't know, what should I look for to find out? I created it as a .NET framework 4.8 project.

do you have explicit dependencies to system.runtime in your csproj?

No.

do you have dependencies that require system.runtime 4.1.2.0?

I don't think so, but I haven't found how to see it directly either. I tried to remove extra libraries and the error persisted.

DanielAbalde commented 2 years ago

I also tried installing this https://www.nuget.org/packages/System.Runtime.WindowsRuntime/5.0.0-preview.3.20214.6 and I got the same error.

vpenades commented 2 years ago

system.runtime.windowsruntime has nothing to do with the issue and it will only mess your project even more.

Your project csproj should be created with the new sdk-style, because it has better support of automatic transitive dependencies.

The old csproj format, which is long and verbose, requires explicit references for everything you use, which indirectly cause a lot of conflicts if not carefully defined. I suspect your issues happen because your csproj is old style.

DanielAbalde commented 2 years ago

I create a new project like that but it doesn't work either.

I am referencing two libraries (Rhinocommon and Grasshopper) which just have a dependency .NET 4.8. Here you can see the .csproj file, hopefully you see something wrong:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Numerics.Vectors" Version="4.5.0" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="Grasshopper">
      <HintPath>..\..\..\..\..\Program Files\Rhino 7\Plug-ins\Grasshopper\Grasshopper.dll</HintPath>
      <Private>false</Private>
    </Reference>
    <Reference Include="RhinoCommon">
      <HintPath>..\..\..\..\..\Program Files\Rhino 7\System\RhinoCommon.dll</HintPath>
      <Private>false</Private>
    </Reference>
    <Reference Include="SharpGLTF.Core">
      <HintPath>..\..\..\source\repos\SharpGLTF-master\SharpGLTF-master\src\SharpGLTF.Core\bin\Debug\net5.0\SharpGLTF.Core.dll</HintPath>
    </Reference>
    <Reference Include="SharpGLTF.Toolkit">
      <HintPath>..\..\..\source\repos\SharpGLTF-master\SharpGLTF-master\src\SharpGLTF.Toolkit\bin\Debug\net5.0\SharpGLTF.Toolkit.dll</HintPath>
    </Reference>
  </ItemGroup>

  <Target Name="PostBuild" AfterTargets="PostBuildEvent">
    <Exec Command="copy  /Y &quot;$(TargetDir)$(ProjectName).dll&quot; &quot;$(TargetDir)$(ProjectName).gha&quot;&#xD;&#xA;copy  /Y &quot;$(TargetDir)$(ProjectName).dll&quot; &quot;$(USERPROFILE)\AppData\Roaming\Grasshopper\Libraries\$(ProjectName).gha&quot;" />
  </Target>

</Project>

I got:

Error CS1705 Assembly 'SharpGLTF.Toolkit' with identity 'SharpGLTF.Toolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Runtime' with identity 'System.Runtime, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

vpenades commented 2 years ago

To begin with, you're referencing a net5.0 library from a net48 project. No wonder it's not working.

And you should include SharpGLTF as a packageReference, not as a direct DLL reference

DanielAbalde commented 2 years ago

And you should include SharpGLTF as a packageReference, not as a direct DLL reference

It was the first thing I tried and it did not appear. I tried again and I had the pre-release disabled. :/ It works now. Thank you very much!