Free Support Forum -

3D cube

How to generate a 3D cube with the image on each face of the cube and convert it into a gif in C#?


Thank you for contacting support.

We are looking into your requirements and will get back to you with our findings soon.


Thank you for being patient.

We have worked over your requirements and below is a code snippet for your kind reference.

public static void Main(string[] args) 
        License lic = new License(); 

        //Prepare scene with a box and a camera that looks at it 
        var scene = new Scene(); 

        //create the parent node 
        var boxNode = scene.RootNode.CreateChildNode("box"); 
        //create materials for each face 
        var diffuseColors = new[] 
            {Color.Blue, Color.ForestGreen, Color.Fuchsia, Color.Cyan, Color.Navy, Color.Peru}; 
        foreach (var color in diffuseColors) 
            var mat = new LambertMaterial() {DiffuseColor = new Vector3(color)}; 
        var textureFiles = new[] 
            {"texture-1.png", "texture-2.png", "texture-3.png", "texture-4.png", "texture-5.png", "texture-6.png"}; 
        foreach (var fileName in textureFiles) 
            var mat = new LambertMaterial(); 
            //create a texture for diffuse mapping 
            mat.SetTexture(Material.MapDiffuse, new Texture() {FileName = fileName}); 

        //create a box with size(1,1,1) and attach it to the node 
        var box = (new Box(1, 1, 1)).ToMesh(); 
        //bind materials that attached to its parent node to each face of the box(referenced by the index of material) 
        var materials = (VertexElementMaterial) box.CreateElement(VertexElementType.Material, MappingMode.Polygon, ReferenceMode.Index); 
        materials.SetIndices(new int[] { 0,1,2,3,4,5 }); 

        var camera = new Camera() {FarPlane = 40, NearPlane = 0.01}; 
        scene.RootNode.CreateChildNode(camera).Transform.Translation = new Vector3(3, 0, 3); 
        var light = new Light() {LightType = LightType.Point, Color = new Vector3(Color.WhiteSmoke)}; 
        scene.RootNode.CreateChildNode(light).Transform.Translation = new Vector3(5, 1, 0); 

        List<Bitmap> frames = new List<Bitmap>(); 

        //define a simple time-based rotation function to demonstrate the rotation animation 
        Func<float, Quaternion> rot = t => Quaternion.FromEulerAngle(t + 0.3, -0.5 * t + 0.8, t); 
        using (Renderer r = Renderer.CreateRenderer()) 
            using (var target = r.RenderFactory.CreateRenderTexture(new RenderParameters(false), 512, 512)) 
                var vp = target.CreateViewport(camera); 
                vp.BackgroundColor = Color.Aqua; 

                for (int i = 0; i < 40; i++) 
                    float t = i * 0.1f; 
                    boxNode.Transform.Rotation = rot(t); 
                    //save the result 
                    var frame = ((ITexture2D) target.Targets[0]).ToBitmap(); 

        //Save the frames into different files, or you can use 3rd party libraries to encode them as a single animated GIF file. 
        for (int i = 0; i < frames.Count; i++) 
            frames[i].Save(string.Format("d:\\gif\\frame-{0:00}.bmp", i), ImageFormat.Bmp); 

This code snippet renders a sequence of frames, and saves them into separated BMP files, you can use 3rd party GIF encoding library to make the frames into one GIF animation file. Moreover, please ensure using Aspose.3D for .NET 18.8 in your environment and feel free to contact us if you need any further assistance.