//读取指定文件中的 Json数据 反序列化 public T LoadData<T>(string fileName, JsonType type = JsonType.LitJson) where T : new() { //确定从哪个路径读取 //首先先判断 默认数据文件夹中是否有我们想要的数据 如果有 就从中获取 string path = Application.streamingAssetsPath + "/" + fileName + ".json"; //先判断 是否存在这个文件 //如果不存在默认文件 就从 读写文件夹中去寻找 if(!File.Exists(path)) path = Application.persistentDataPath + "/" + fileName + ".json"; //如果读写文件夹中都还没有 那就返回一个默认对象 if (!File.Exists(path)) return new T();
//进行反序列化 string jsonStr = File.ReadAllText(path); //数据对象 T data = default(T); switch (type) { case JsonType.JsonUtlity: data = JsonUtility.FromJson<T>(jsonStr); break; case JsonType.LitJson: data = JsonMapper.ToObject<T>(jsonStr); break; }
classcamera { public: camera( // 相机位置。 point3 lookfrom, // 相机看向的目标点。 point3 lookat, // 相机正上方方向向量(通常为(0,1,0))。 vec3 vup, // 视场大小。 double vfov, // 长宽比。 double aspect_ratio ) { auto theta = degrees_to_radians(vfov); auto h = tan(theta/2); auto viewport_height = 2.0 * h; auto viewport_width = aspect_ratio * viewport_height;
// w向量是从lookat指向lookfrom的向量。 auto w = unit_vector(lookfrom - lookat); // u向量与vup和w都垂直,我们可以直接叉乘得到它。 // 叉乘注意两个变量的前后顺序,注意叉乘结果向量的方向满足右手定则。 auto u = unit_vector(cross(vup, w)); // v向量与w及u向量都垂直,叉乘得到。 auto v = cross(w, u);
voidwrite_color(std::ostream &out, color pixel_color, int samples_per_pixel){ auto r = pixel_color.x(); auto g = pixel_color.y(); auto b = pixel_color.z();
//Gamma矫正(Gamma = 2.0)。 auto scale = 1.0 / samples_per_pixel; r = sqrt(scale * r); g = sqrt(scale * g); b = sqrt(scale * b);
voidwrite_color(std::ostream &out, color pixel_color, int samples_per_pixel){ auto r = pixel_color.x(); auto g = pixel_color.y(); auto b = pixel_color.z();
// 除以采样次数 auto scale = 1.0 / samples_per_pixel; r *= scale; g *= scale; b *= scale;