Example #1
0
        public List <TriangleGroup> TriangleGroups = new List <TriangleGroup>();         // 面

        public float Intersect(Ray ray, ref TriangleGroup interTG, ref Vector3 normal, ref Color4 color)
        {
            float   interT  = float.PositiveInfinity;
            Vector3 nNormal = Vector3.zero;
            Color4  nColor  = Color4.zero;

            foreach (var tg in TriangleGroups)
            {
                float t = tg.Intersect(ray, ref nNormal, ref nColor);
                if (t > 0.0001f && t < interT)
                {
                    interTG = tg;
                    interT  = t;
                    normal  = nNormal;
                    color   = nColor;
                }
            }
            if (interT == float.PositiveInfinity)
            {
                return(-1);
            }
            return(interT);
        }
Example #2
0
 public static Mesh LoadFromObj(string fileName)
 {
     using (var sr = new StreamReader(new FileStream(fileName, FileMode.Open))) {
         Mesh mesh = new Mesh();
         Dictionary <string, Material> ms = null;
         List <Vector3> vs            = new List <Vector3>();      // 顶点
         List <Vector3> ns            = new List <Vector3>();      // 法线
         List <Vector2> ts            = new List <Vector2>();      // 纹理坐标
         TriangleGroup  triangleGroup = null;
         int            i             = 0;
         while (!sr.EndOfStream)
         {
             string line = sr.ReadLine();
             i++;
             try {
                 if (line.StartsWith("#"))
                 {
                     continue;
                 }
                 if (line.StartsWith("vn"))
                 {
                     ReadLine(ns, line);
                 }
                 else if (line.StartsWith("vt"))
                 {
                     ReadLine(ts, line);
                 }
                 else if (line.StartsWith("v"))
                 {
                     ReadLine(vs, line);
                 }
                 else if (line.StartsWith("o"))
                 {
                     string[] data = line.Split(' ');
                     if (data.Length != 2)
                     {
                         throw new Exception($"第{i}行,OBJ格式错误");
                     }
                     if (triangleGroup != null)
                     {
                         triangleGroup.GenerateAABB();
                         mesh.TriangleGroups.Add(triangleGroup);
                     }
                     triangleGroup = new TriangleGroup(data[1]);
                 }
                 else if (line.StartsWith("f"))
                 {
                     Triangle triangle = ReadLine(vs, ns, ts, line);
                     triangleGroup.Triangles.Add(triangle);
                 }
                 else if (line.StartsWith("mtllib"))
                 {
                     string[] data = line.Split(' ');
                     if (data.Length != 2)
                     {
                         throw new Exception($"第{i}行,OBJ格式错误");
                     }
                     ms = Material.LoadFromMtl(Path.Combine(Path.GetDirectoryName(fileName), data[1]));
                 }
                 else if (line.StartsWith("usemtl"))
                 {
                     string[] data = line.Split(' ');
                     if (data.Length != 2)
                     {
                         throw new Exception($"第{i}行,OBJ格式错误");
                     }
                     if (ms == null)
                     {
                         throw new Exception($"第{i}行,材质未加载");
                     }
                     triangleGroup.Material = ms[data[1]];
                 }
             } catch (Exception e) {
                 throw new Exception($"第{i}行", e);
             }
         }
         if (triangleGroup == null)
         {
             throw new Exception("OBJ格式错误,没有物体");
         }
         triangleGroup.GenerateAABB();
         mesh.TriangleGroups.Add(triangleGroup);
         return(mesh);
     }
 }