欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#实现3D效果完整实例

程序员文章站 2022-05-03 16:23:07
本文实例讲述了c#实现3d效果的方法。分享给大家供大家参考,具体如下: 一、新建一类文件 private static double[] addvector(d...

本文实例讲述了c#实现3d效果的方法。分享给大家供大家参考,具体如下:

一、新建一类文件

private static double[] addvector(double[] a, double[] b)
{
    return new double[] { a[0] + b[0], a[1] + b[1], a[2] + b[2] };
}
private static double[] scalarproduct(double[] vector, double scalar)
{
    return new double[] { vector[0] * scalar, vector[1] * scalar, vector[2] * scalar };
}
private static double dotproduct(double[] a, double[] b)
{
    return a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
}
private static double norm(double[] vector)
{
    return math.sqrt(dotproduct(vector, vector));
}
private static double[] normalize(double[] vector)
{
    return scalarproduct(vector, 1.0 / norm(vector));
}
private static double[] crossproduct(double[] a, double[] b)
{
    return new double[] 
        { 
          (a[1] * b[2] - a[2] * b[1]), 
          (a[2] * b[0] - a[0] * b[2]), 
          (a[0] * b[1] - a[1] * b[0]) 
        };
}
private static double[] vectorproductindexed(double[] v, double[] m, int i)
{
    return new double[]
        {
          v[i + 0] * m[0] + v[i + 1] * m[4] + v[i + 2] * m[8] + v[i + 3] * m[12],
          v[i + 0] * m[1] + v[i + 1] * m[5] + v[i + 2] * m[9] + v[i + 3] * m[13],
          v[i + 0] * m[2] + v[i + 1] * m[6] + v[i + 2] * m[10]+ v[i + 3] * m[14],
          v[i + 0] * m[3] + v[i + 1] * m[7] + v[i + 2] * m[11]+ v[i + 3] * m[15]
        };
}
private static double[] vectorproduct(double[] v, double[] m)
{
    return vectorproductindexed(v, m, 0);
}
private static double[] matrixproduct(double[] a, double[] b)
{
    double[] o1 = vectorproductindexed(a, b, 0);
    double[] o2 = vectorproductindexed(a, b, 4);
    double[] o3 = vectorproductindexed(a, b, 8);
    double[] o4 = vectorproductindexed(a, b, 12);
    return new double[]
        {
          o1[0], o1[1], o1[2], o1[3],
          o2[0], o2[1], o2[2], o2[3],
          o3[0], o3[1], o3[2], o3[3],
          o4[0], o4[1], o4[2], o4[3]
        };
}
private static double[] cameratransform(double[] c, double[] a)
{
    double[] w = normalize(addvector(c, scalarproduct(a, -1)));
    double[] y = new double[] { 0, 1, 0 };
    double[] u = normalize(crossproduct(y, w));
    double[] v = crossproduct(w, u);
    double[] t = scalarproduct(c, -1);
    return new double[]
        {
          u[0], v[0], w[0], 0,
          u[1], v[1], w[1], 0,
          u[2], v[2], w[2], 0,
          dotproduct(u, t), dotproduct(v, t), dotproduct(w, t), 1
        };
}
private static double[] viewingtransform(double fov, double n, double f)
{
    fov *= (math.pi / 180);
    double cot = 1.0 / math.tan(fov / 2);
    return new double[] { cot, 0, 0, 0, 0, cot, 0, 0, 0, 0, (f + n) / (f - n), -1, 0, 0, 2 * f * n / (f - n), 0 };
}
public static image generate(string captchatext)
{
    int fontsize = 24;
    font font = new font("arial", fontsize);
    sizef sizef;
    using (graphics g = graphics.fromimage(new bitmap(1, 1)))
    {
      sizef = g.measurestring(captchatext, font, 0, stringformat.genericdefault);
    }
    int image2d_x = (int)sizef.width;
    int image2d_y = (int)(fontsize * 1.3);
    bitmap image2d = new bitmap(image2d_x, image2d_y);
    color black = color.black;
    color white = color.white;
    using (graphics g = graphics.fromimage(image2d))
    {
      g.clear(black);
      g.drawstring(captchatext, font, brushes.white, 0, 0);
    }
    random rnd = new random();
    double[] t = cameratransform(new double[] { rnd.next(-90, 90), -200, rnd.next(150, 250) }, new double[] { 0, 0, 0 });
    t = matrixproduct(t, viewingtransform(60, 300, 3000));
    double[][] coord = new double[image2d_x * image2d_y][];
    int count = 0;
    for (int y = 0; y < image2d_y; y += 2)
    {
      for (int x = 0; x < image2d_x; x++)
      {
        int xc = x - image2d_x / 2;
        int zc = y - image2d_y / 2;
        double yc = -(double)(image2d.getpixel(x, y).toargb() & 0xff) / 256 * 4;
        double[] xyz = new double[] { xc, yc, zc, 1 };
        xyz = vectorproduct(xyz, t);
        coord[count] = xyz;
        count++;
      }
    }
    int image3d_x = 256;
    int image3d_y = image3d_x * 9 / 16;
    bitmap image3d = new bitmap(image3d_x, image3d_y);
    color fgcolor = color.white;
    color bgcolor = color.black;
    using (graphics g = graphics.fromimage(image3d))
    {
      g.clear(bgcolor);
      count = 0;
      double scale = 1.75 - (double)image2d_x / 400;
      for (int y = 0; y < image2d_y; y += 2)
      {
        for (int x = 0; x < image2d_x; x++)
        {
          if (x > 0)
          {
            double x0 = coord[count - 1][0] * scale + image3d_x / 2;
            double y0 = coord[count - 1][1] * scale + image3d_y / 2;
            double x1 = coord[count][0] * scale + image3d_x / 2;
            double y1 = coord[count][1] * scale + image3d_y / 2;
            g.drawline(new pen(fgcolor), (float)x0, (float)y0, (float)x1, (float)y1);
          }
          count++;
        }
      }
    }
    return image3d;
}

注意引用命名空间:

using system.drawing;

二、页面调用

response.contenttype = "image/pjpeg";
captcha.generate("我就是3d内容").save(response.outputstream, system.drawing.imaging.imageformat.jpeg);

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。