PlotlyToolkit.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Numerics;
  5. using System.Text;
  6. using SharpGLTF.Geometry;
  7. using SharpGLTF.Materials;
  8. using SharpGLTF.Schema2;
  9. using TRACES = Plotly.Box<Plotly.Types.ITracesProperty>;
  10. namespace SharpGLTF
  11. {
  12. public static class PlotlyToolkit
  13. {
  14. public static PlotlyScene ToPlotly(this Schema2.Scene srcScene, Animation animation = null, float time = 0)
  15. {
  16. // create an instantiable scene.
  17. var sceneInstance = Runtime.SceneTemplate
  18. .Create(srcScene, false)
  19. .CreateInstance();
  20. // set the node animations for our scene instance-
  21. if (animation == null) { sceneInstance.SetPoseTransforms(); }
  22. else { sceneInstance.SetAnimationFrame(animation.Name, time); }
  23. // keep source meshes.
  24. var meshes = srcScene.LogicalParent.LogicalMeshes;
  25. // get the drawable instances.
  26. var instances = sceneInstance
  27. .DrawableInstances
  28. .Where(item => item.Transform.Visible);
  29. // prepare the PlotlyScene.
  30. var dstScene = new PlotlyScene();
  31. // enumerate and "render" the instances.
  32. foreach (var instance in instances)
  33. {
  34. var mesh = meshes[instance.Template.LogicalMeshIndex];
  35. var tris = mesh.EvaluateTriangles(instance.Transform);
  36. dstScene.AppendTriangles(tris, Matrix4x4.Identity, GetMaterialColor);
  37. }
  38. return dstScene;
  39. }
  40. public static PlotlyScene ToPlotly(this Schema2.Mesh mesh)
  41. {
  42. var dstScene = new PlotlyScene();
  43. dstScene.AppendTriangles(mesh.EvaluateTriangles(), Matrix4x4.Identity, GetMaterialColor);
  44. return dstScene;
  45. }
  46. public static PlotlyScene ToPlotly(this IMeshBuilder<MaterialBuilder> mesh)
  47. {
  48. IEnumerable<(IVertexBuilder, IVertexBuilder, IVertexBuilder, MaterialBuilder)> _enumTris()
  49. {
  50. foreach (var p in mesh.Primitives)
  51. {
  52. foreach (var (A, B, C) in p.Triangles)
  53. {
  54. var va = p.Vertices[A];
  55. var vb = p.Vertices[B];
  56. var vc = p.Vertices[C];
  57. yield return (va, vb, vc, p.Material);
  58. }
  59. }
  60. }
  61. var scene = new PlotlyScene();
  62. scene.AppendTriangles(_enumTris(), Matrix4x4.Identity, GetMaterialColor);
  63. return scene;
  64. }
  65. private static int GetMaterialColor(Schema2.Material material)
  66. {
  67. var mb = new MaterialBuilder();
  68. material.CopyTo(mb);
  69. return GetMaterialColor(mb);
  70. }
  71. private static int GetMaterialColor(MaterialBuilder material)
  72. {
  73. var color = (material.GetChannel(KnownChannel.BaseColor) ?? material.GetChannel(KnownChannel.Diffuse))?.Parameter ?? Vector4.One * 0.8f;
  74. color *= 255;
  75. var ccc = color.X * 65536 + color.Y * 256 + color.Z;
  76. return (int)ccc;
  77. }
  78. }
  79. }