Crear presentaciones 3D en .NET
Visión general
¿Cómo suele crear una presentación de PowerPoint en 3D?
Microsoft PowerPoint permite crear presentaciones 3D al agregar modelos 3D, aplicar efectos 3D a formas, crear texto 3D, cargar gráficos 3D en la presentación y crear animaciones 3D en PowerPoint.
Crear efectos 3D tiene un gran impacto en la mejora de su presentación, convirtiéndola en una presentación 3D, y puede ser la forma más sencilla de implementar una presentación 3D.
Desde la versión 20.9 de Aspose.Slides, se ha añadido un motor 3D multiplataforma. El nuevo motor 3D permite exportar y rasterizar formas y texto con efectos 3D. En versiones anteriores, las formas de Slides con efectos 3D aplicados se renderizaban de forma plana. Ahora es posible renderizar formas con un 3D completo.
Además, ahora es posible crear formas con efectos 3D mediante la API pública de Slides.
En la API de Aspose.Slides, para convertir una forma en una forma 3D de PowerPoint use la propiedad IShape.ThreeDFormat, que hereda las características de la interfaz IThreeDFormat:
- BevelBottom y BevelTop: establezca el bisel de la forma, defina el tipo de bisel (p. ej., Angle, Circle, SoftRound), la altura y el ancho del bisel.
- Camera: se usa para imitar movimientos de cámara alrededor del objeto. En otras palabras, al establecer rotación, zoom y otras propiedades puede “entretener” sus formas como con el modelo 3D en PowerPoint.
- ContourColor y ContourWidth: establezca propiedades de contorno para que la forma parezca una forma 3D de PowerPoint.
- Depth, ExtrusionColor y ExtrusionHeight: se usan para dar a la forma una tercera dimensión, lo que significa convertir una forma 2D en una forma 3D, estableciendo su profundidad o extrusión.
- LightRig: puede crear un efecto de luz en una forma 3D. La lógica de esta propiedad es similar a la de Camera; puede establecer la rotación de la luz en relación con la forma 3D y elegir el tipo de luz.
- Material: establecer el tipo de material de la forma 3D aporta un efecto más realista. La propiedad ofrece un conjunto de materiales predefinidos, como Metal, Plastic, Powder, Matte, etc.
Todas las funciones 3D pueden aplicarse tanto a formas como a texto. Veamos cómo acceder a las propiedades mencionadas y, a continuación, analicémoslas paso a paso:
const float imageScale = 2;
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IAutoShape shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);
shape.TextFrame.Text = "3D";
shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 64;
shape.ThreeDFormat.Camera.CameraType = CameraPresetType.OrthographicFront;
shape.ThreeDFormat.Camera.SetRotation(20, 30, 40);
shape.ThreeDFormat.LightRig.LightType = LightRigPresetType.Flat;
shape.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
shape.ThreeDFormat.Material = MaterialPresetType.Flat;
shape.ThreeDFormat.ExtrusionHeight = 100;
shape.ThreeDFormat.ExtrusionColor.Color = Color.Blue;
using (IImage thumbnail = slide.GetImage(imageScale, imageScale))
{
thumbnail.Save("sample_3d.png");
}
presentation.Save("sandbox_3d.pptx", SaveFormat.Pptx);
}
La miniatura renderizada se ve así:

Rotación 3D
Es posible rotar formas 3D de PowerPoint en un plano 3D, lo que brinda mayor interactividad. Para rotar una forma 3D en PowerPoint, normalmente se utiliza el siguiente menú:

En la API de Aspose.Slides, la rotación de formas 3D puede gestionarse mediante la propiedad IThreeDFormat.Camera:
IAutoShape shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);
shape.ThreeDFormat.Camera.SetRotation(20, 30, 40);
// ... establecer otros parámetros de la escena 3D
using (IImage thumbnail = slide.GetImage(imageScale, imageScale))
{
thumbnail.Save("sample_3d.png");
}
Profundidad 3D y Extrusión
Para añadir la tercera dimensión a su forma y convertirla en una forma 3D, use las propiedades IThreeDFormat.ExtrusionHeight y IThreeDFormat.ExtrusionColor.Color:
IAutoShape shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 200, 200);
shape.ThreeDFormat.Camera.SetRotation(20, 30, 40);
shape.ThreeDFormat.ExtrusionHeight = 100;
shape.ThreeDFormat.ExtrusionColor.Color = Color.Purple;
// ... establecer otros parámetros de la escena 3D
using (IImage thumbnail = slide.GetImage(imageScale, imageScale))
{
thumbnail.Save("sample_3d.png");
}
Normalmente, se usa el menú Depth en PowerPoint para establecer la profundidad de una forma 3D de PowerPoint:

Gradiente 3D
El gradiente puede usarse para rellenar el color de una forma 3D de PowerPoint. Creemos una forma con relleno de gradiente y apliquemos un efecto 3D:
const float imageScale = 2;
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IAutoShape shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 250, 250);
shape.TextFrame.Text = "3D Gradient";
shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 64;
shape.FillFormat.FillType = FillType.Gradient;
shape.FillFormat.GradientFormat.GradientStops.Add(0, Color.Blue);
shape.FillFormat.GradientFormat.GradientStops.Add(100, Color.Orange);
shape.ThreeDFormat.Camera.CameraType = CameraPresetType.OrthographicFront;
shape.ThreeDFormat.Camera.SetRotation(10, 20, 30);
shape.ThreeDFormat.LightRig.LightType = LightRigPresetType.Flat;
shape.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
shape.ThreeDFormat.ExtrusionHeight = 150;
shape.ThreeDFormat.ExtrusionColor.Color = Color.DarkOrange;
using (IImage thumbnail = slide.GetImage(imageScale, imageScale))
{
thumbnail.Save("sample_3d.png");
}
}
Y aquí está el resultado:

Además de un relleno de gradiente, es posible rellenar formas con una imagen:
byte[] imageData = File.ReadAllBytes("image.jpg");
IPPImage image = presentation.Images.AddImage(imageData);
shape.FillFormat.FillType = FillType.Picture;
shape.FillFormat.PictureFillFormat.Picture.Image = image;
shape.FillFormat.PictureFillFormat.PictureFillMode = PictureFillMode.Stretch;
// ... configurar 3D: shape.ThreeDFormat.Camera, shape.ThreeDFormat.LightRig, shape.ThreeDFormat.Extrusion* propiedades
using (IImage thumbnail = slide.GetImage(imageScale, imageScale))
{
thumbnail.Save("sample_3d.png");
}
Así es como se ve:

Texto 3D (WordArt)
Aspose.Slides permite aplicar 3D al texto también. Para crear texto 3D es posible usar el efecto de transformación WordArt:
const float imageScale = 2;
using (Presentation presentation = new Presentation())
{
ISlide slide = presentation.Slides[0];
IAutoShape shape = slide.Shapes.AddAutoShape(ShapeType.Rectangle, 200, 150, 250, 250);
shape.FillFormat.FillType = FillType.NoFill;
shape.LineFormat.FillFormat.FillType = FillType.NoFill;
shape.TextFrame.Text = "3D Text";
Portion portion = (Portion)shape.TextFrame.Paragraphs[0].Portions[0];
portion.PortionFormat.FillFormat.FillType = FillType.Pattern;
portion.PortionFormat.FillFormat.PatternFormat.ForeColor.Color = Color.DarkOrange;
portion.PortionFormat.FillFormat.PatternFormat.BackColor.Color = Color.White;
portion.PortionFormat.FillFormat.PatternFormat.PatternStyle = PatternStyle.LargeGrid;
shape.TextFrame.Paragraphs[0].ParagraphFormat.DefaultPortionFormat.FontHeight = 128;
ITextFrameFormat textFrameFormat = shape.TextFrame.TextFrameFormat;
// establecer el efecto de transformación WordArt "Arch Up"
textFrameFormat.Transform = TextShapeType.ArchUp;
textFrameFormat.ThreeDFormat.ExtrusionHeight = 3.5f;
textFrameFormat.ThreeDFormat.Depth = 3;
textFrameFormat.ThreeDFormat.Material = MaterialPresetType.Plastic;
textFrameFormat.ThreeDFormat.LightRig.Direction = LightingDirection.Top;
textFrameFormat.ThreeDFormat.LightRig.LightType = LightRigPresetType.Balanced;
textFrameFormat.ThreeDFormat.LightRig.SetRotation(0, 0, 40);
textFrameFormat.ThreeDFormat.Camera.CameraType = CameraPresetType.PerspectiveContrastingRightFacing;
using (IImage thumbnail = slide.GetImage(imageScale, imageScale))
{
thumbnail.Save("text3d.png");
}
presentation.Save("text3d.pptx", SaveFormat.Pptx);
}
Este es el resultado:

Preguntas frecuentes
¿Se conservarán los efectos 3D al exportar una presentación a imágenes/PDF/HTML?
Sí. El motor 3D de Slides renderiza los efectos 3D al exportar a los formatos compatibles (images, PDF, HTML, etc.).
¿Puedo obtener los valores “efectivos” (finales) de los parámetros 3D que tienen en cuenta temas, herencia, etc.?
Sí. Slides proporciona API para read effective values (incluyendo 3D—iluminación, biseles, etc.) de modo que pueda ver la configuración final aplicada.
¿Los efectos 3D funcionan al convertir una presentación a video?
Sí. Al generar fotogramas para el video, los efectos 3D se renderizan igual que en las imágenes exportadas.