How to Render an Image of 3D Model from the Camera inside .NET Application


/ Published in: C#
Save to your folder(s)

This Technical tip explains how .NET developers can render an image of 3D model from the camera inside their .NET applications. Using Aspose.3D for .NET, developers can render an image to view a realistic image of 3D model, with or without the enhanced background, textures, shadows and also adjust the image size. The Render method exposed by the Scene class can be used to take a picture from the active camera. Developers may also use the several different ways to navigate and position the camera in the scene. In this code example, we create a camera at position (10,10,10) in an existing 3D scene and look at the origin point for rendering.


Copy this code and paste it in your HTML
  1. // this code example creates a camera in a 3D scene, sets its target and render an image.
  2.  
  3. // [C# Code Sample]
  4.  
  5. // For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET
  6. // The path to the documents directory.
  7. string MyDir = RunExamples.GetDataDir();
  8.  
  9. // Load scene from file
  10. Scene scene = new Scene(MyDir + "camera.3ds");
  11. // Create a camera at (10,10,10) and look at the origin point for rendering,
  12. // it must be attached to the scene before render
  13. Camera camera = new Camera();
  14. scene.RootNode.CreateChildNode("camera", camera);
  15. camera.ParentNode.Transform.Translation = new Vector3(10, 10, 10);
  16. camera.LookAt = Vector3.Origin;
  17.  
  18. // Specify the image render option
  19. ImageRenderOptions opt = new ImageRenderOptions();
  20. // Set the background color
  21. opt.BackgroundColor = Color.AliceBlue;
  22. // Tells renderer where the it can find textures
  23. opt.AssetDirectories.Add(MyDir + "textures");
  24. // Turn on shadow
  25. opt.EnableShadows = true;
  26. // Render the scene in given camera's perspective into specified png file with size 1024x1024
  27. scene.Render(camera, MyDir + "Render3DModelImageFromCamera_out_.png", new Size(1024, 1024), ImageFormat.Png, opt);
  28.  
  29. //[VB.NET Code Sample]
  30.  
  31. ' For complete examples and data files, please go to https://github.com/aspose-3d/Aspose.3D-for-.NET
  32. ' The path to the documents directory.
  33. Dim MyDir As String = RunExamples.GetDataDir()
  34.  
  35. ' Load scene from file
  36. Dim scene As New Scene(MyDir & Convert.ToString("camera.3ds"))
  37. ' Create a camera at (10,10,10) and look at the origin point for rendering,
  38. ' it must be attached to the scene before render
  39. Dim camera As New Camera()
  40. scene.RootNode.CreateChildNode("camera", camera)
  41. camera.ParentNode.Transform.Translation = New Vector3(10, 10, 10)
  42. camera.LookAt = Vector3.Origin
  43.  
  44. ' Specify the image render option
  45. Dim opt As New ImageRenderOptions()
  46. ' Set the background color
  47. opt.BackgroundColor = Color.AliceBlue
  48. ' Tells renderer where the it can find textures
  49. opt.AssetDirectories.Add(MyDir & Convert.ToString("textures"))
  50. ' Turn on shadow
  51. opt.EnableShadows = True
  52. ' Render the scene in given camera's perspective into specified png file with size 1024x1024
  53. scene.Render(camera, MyDir & Convert.ToString("Render3DModelImageFromCamera_out_.png"), New Size(1024, 1024), ImageFormat.Png, opt)

URL: http://www.aspose.com/.net/3d-component.aspx

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.