Partage
  • Partager sur Facebook
  • Partager sur Twitter

[C#][WPF][3D] Cylindre creux et transparent

Sujet résolu
    8 mars 2012 à 17:43:44

    Bonjour,

    Je suis actuellement sur le développement d'une application qui à terme proposera des UserControls sur des objets 3D tout en restant interactif :D
    Pour ce faire j'utilise la librairie 3DTools :)
    Seulement je rencontre un petit problèmes :
    J'ai un cylindre sur lequel je colle une texture qui en réalité correspond à mon UserControl ou du moins à une partie :

    <SlideShow:InteractiveCylinder.Visual>
      <ListBox Name="ListTest">
        <ListBox.Template>
          <ControlTemplate TargetType="ListBox">
            <StackPanel Orientation="Horizontal" Background="Red" Height="40"  IsItemsHost="True" HorizontalAlignment="Stretch"/>
          </ControlTemplate>
        </ListBox.Template>
        <ListBox.Items>
          <Image Source="/SlideShow3DProject;component/jeu_addik.jpg" Margin="5"  Height="30" Stretch="UniformToFill"/>
          <Image Source="/SlideShow3DProject;component/jeu_addik.jpg" Margin="5"  Height="30" Stretch="UniformToFill"/>
          <Image Source="/SlideShow3DProject;component/jeu_addik.jpg" Margin="5"  Height="30" Stretch="UniformToFill"/>
          <Image Source="/SlideShow3DProject;component/jeu_addik.jpg" Margin="5"  Height="30" Stretch="UniformToFill"/>
          <Image Source="/SlideShow3DProject;component/jeu_addik.jpg" Margin="5"  Height="30" Stretch="UniformToFill"/>
        </ListBox.Items>
      </ListBox>
    </SlideShow:InteractiveCylinder.Visual>
    


    SlideShow:InteractiveCylinder est un control que j'ai trouvé sur un des exemples de 3DTools qui construit un cylindre.

    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Media3D;
    using _3DTools;
    
    namespace SlideShow3DProject
    {
        public class InteractiveCylinder : InteractiveVisual3D
        {
            public InteractiveCylinder()
            {
                Geometry = Tessellate(32, 32);
            }
    
            internal static Point3D GetPosition(double t, double y)
            {
                double x = Math.Cos(t);
                double z = Math.Sin(t);
    
                return new Point3D(x, y, z);
            }
    
            private static Vector3D GetNormal(double t, double y)
            {
                double x = Math.Cos(t);
                double z = Math.Sin(t);
    
                return new Vector3D(x, 0, z);
            }
    
            internal static double DegToRad(double degrees)
            {
                return (degrees / 180.0) * Math.PI;
            }
    
            private static Point GetTextureCoordinate(double t, double y)
            {
                return new Point(1.0 - t * 1 / (2 * Math.PI), y * -0.5 + 0.5);
            }
    
            internal static MeshGeometry3D Tessellate(int tDiv, int yDiv)
            {
                double maxTheta = DegToRad(360.0);
                double minY = -1.0;
                double maxY = 1.0;
    
                double dt = maxTheta / tDiv;
                double dy = (maxY - minY) / yDiv;
    
                MeshGeometry3D mesh = new MeshGeometry3D();
    
                for (int yi = 0; yi <= yDiv; yi++)
                {
                    double y = minY + yi * dy;
    
                    for (int ti = 0; ti <= tDiv; ti++)
                    {
                        double t = ti * dt;
    
                        mesh.Positions.Add(GetPosition(t, y));
                        mesh.Normals.Add(GetNormal(t, y));
                        mesh.TextureCoordinates.Add(GetTextureCoordinate(t, y));
                    }
                }
    
                for (int yi = 0; yi < yDiv; yi++)
                {
                    for (int ti = 0; ti < tDiv; ti++)
                    {
                        int x0 = ti;
                        int x1 = (ti + 1);
                        int y0 = yi * (tDiv + 1);
                        int y1 = (yi + 1) * (tDiv + 1);
    
                        mesh.TriangleIndices.Add(x0 + y0);
                        mesh.TriangleIndices.Add(x0 + y1);
                        mesh.TriangleIndices.Add(x1 + y0);
    
                        mesh.TriangleIndices.Add(x1 + y0);
                        mesh.TriangleIndices.Add(x0 + y1);
                        mesh.TriangleIndices.Add(x1 + y1);
                    }
                }
                mesh.Freeze();
                return mesh;
            }
    
    
            public int Cobble
            {
                get { return (int)GetValue(CobbleProperty); }
                set { SetValue(CobbleProperty, value); }
            }
    
            // Using a DependencyProperty as the backing store for Cobble.  This enables animation, styling, binding, etc...
            public static readonly DependencyProperty CobbleProperty =
                DependencyProperty.Register("Cobble", typeof(int), typeof(InteractiveCylinder), new UIPropertyMetadata(32, OnCobbleChange));
    
            static void OnCobbleChange(DependencyObject d, DependencyPropertyChangedEventArgs e)
            {
                InteractiveCylinder cyl = d as InteractiveCylinder;
    
                if (cyl != null)
                {
                    int cobble = cyl.Cobble;
                    cyl.Geometry = InteractiveCylinder.Tessellate(cobble, cobble);
                }
            }
        }
    }
    


    La construction du cylindre se fait dans la methode Tessellate de InteractiveCylinder

    Pour l'instant, le résultat est là :

    Image utilisateur
    Je précise que le fond rouge est dû à la texture qui est en réalité une ListBox contenant plusieurs images.


    Ce que je souhaite, c'est pouvoir voir ma texture a travers mon cylindre :-°
    Ce qui n'est pas le cas ci dessus :(
    M'y connaissant pas extrêmement en 3D et surtout en 3D WPF, je sais pas trop où chercher, quitte à recommencer et suivre une autre méthode.

    Merci :)

    EDIT :

    Je cherchais pas au bon endroit :p
    Mon cylindre possède une propriété IsBackVisible :)
    • Partager sur Facebook
    • Partager sur Twitter

    [C#][WPF][3D] Cylindre creux et transparent

    × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
    × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
    • Editeur
    • Markdown