1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
//! # An Everquest .wld file loader
//! This is a work in progress but already loads enough data from wld files to be able to do some
//! basic rendering of models. The interface has been heavily influenced by the
//! [glTF](https://github.com/gltf-rs/gltf) crate. Parts of the wld file format are still not well
//! understood and future understanding may influence the api of this crate.
//!
//! # Examples
//! ```rust
//! let archive = eq_archive::read("gfaydark.s3d").unwrap();
//! let wld_data = archive.get("gfaydark.wld").unwrap();
//!
//! let wld = eq_wld::load(&wld_data).unwrap();
//!
//! // Iterate over meshes
//! for mesh in wld.meshes() {
//!     let name = mesh.name();
//!     let positions = mesh.positions();
//!     let normals = mesh.normals();
//!     let texture_coordinates = mesh.texture_coordinates();
//!     let indices = mesh.indices();
//!     let center = mesh.center();
//!     ...
//! }
//!
//! // Iterate over materials
//! for material in wld.materials() {
//!     let name = material.name();
//!     let texture = material.base_color_texture();
//!     let texture_source = texture.source();
//!     ...
//! }
//! ```
//!
//! # Acknowledgements
//! This project wouldn't have been possible without Windcatcher's [WLD File Reference](https://eqemu.gitbook.io/server/categories/zones/customizing-zones/wld-file-reference).
//! Some documentation has been reproduced as comments within the parser module. Names of file
//! fragments have been changed when another term from the [glTF reference](https://www.khronos.org/files/gltf20-reference-guide.pdf)
//! seemed like a better fit. The goal is that this will be usable in more modern engines and
//! hopefully the names used are more familiar in that context.
//!

mod parser;

pub use parser::Error;
use parser::{MaterialFragment, MeshFragment, MeshFragmentPolygonEntry, TextureFragment, WldDoc};

pub struct Wld<'a>(WldDoc<'a>);

/// Load and parse a wld file from a slice.
pub fn load(data: &[u8]) -> Result<Wld, Error> {
    Wld::load(data)
}

impl<'a> Wld<'a> {
    fn load(data: &[u8]) -> Result<Wld, Error> {
        WldDoc::parse(&data[..]).map(|(_, wld_doc)| Ok(Wld(wld_doc)))?
    }

    /// Iterate over all meshes in the wld file.
    pub fn meshes(&self) -> impl Iterator<Item = Mesh> + '_ {
        self.0.meshes().map(move |(name, fragment)| Mesh {
            doc: &self.0,
            name,
            fragment,
        })
    }

    /// Iterate over all materials in the wld file.
    pub fn materials(&self) -> impl Iterator<Item = Material> + '_ {
        self.0.materials().map(move |(name, fragment)| Material {
            doc: &self.0,
            name,
            fragment,
        })
    }
}

#[derive(Debug)]
pub struct Mesh<'a> {
    doc: &'a WldDoc<'a>,
    fragment: MeshFragment,
    name: Option<&'a str>,
}

impl<'a> Mesh<'a> {
    /// The name of the mesh
    pub fn name(&self) -> Option<&str> {
        self.name
    }

    pub fn center(&self) -> (f32, f32, f32) {
        let (x, z, y) = self.fragment.center;
        (x, y, z)
    }

    /// The positions of the vertices that make up this mesh.
    pub fn positions(&self) -> Vec<[f32; 3]> {
        let scale = 1.0 / (1 << self.fragment.scale) as f32;
        self.fragment
            .positions
            .iter()
            .map(|v| [v.0 as f32 * scale, v.2 as f32 * scale, v.1 as f32 * scale])
            .collect()
    }

    /// The vertex normals of the mesh.
    pub fn normals(&self) -> Vec<[f32; 3]> {
        self.fragment
            .vertex_normals
            .iter()
            .map(|v| {
                [
                    (v.0 as f32) / 127.0,
                    (v.2 as f32) / 127.0,
                    (v.1 as f32) / 127.0,
                ]
            })
            .collect()
    }

    /// The coordinates used to map textures to this mesh.
    pub fn texture_coordinates(&self) -> Vec<[f32; 2]> {
        self.fragment
            .texture_coordinates
            .iter()
            .map(|v| [(v.0 as f32) / 256.0, (v.1 as f32) / 256.0])
            .collect()
    }

    /// Indices into the positions vector of this mesh. Expects that the mesh will be drawn as
    /// a triangle list.
    pub fn indices(&self) -> Vec<u32> {
        self.fragment
            .polygons
            .iter()
            .flat_map(|v| {
                vec![
                    v.vertex_indexes.0 as u32,
                    v.vertex_indexes.1 as u32,
                    v.vertex_indexes.2 as u32,
                ]
                .into_iter()
            })
            .collect()
    }

    /// A list of materials used by this mesh.
    pub fn materials(&self) -> Vec<Material> {
        let (_, material_list) = self
            .doc
            .get(&self.fragment.material_list_ref)
            .expect("Invalid material list reference");
        material_list
            .fragments
            .iter()
            .map(|fragment_ref| {
                self.doc
                    .get(&fragment_ref)
                    .expect("Invalid material reference")
            })
            .map(|(name, fragment)| Material {
                doc: &self.doc,
                name,
                fragment,
            })
            .collect()
    }

    /// Primitives belonging to this mesh.
    pub fn primitives(&self) -> Vec<Primitive> {
        let mut pos = 0;
        self.fragment
            .polygon_materials
            .iter()
            .enumerate()
            .map(|(index, (poly_count, ref material_idx))| {
                let count = *poly_count as usize;
                let next_pos = pos + count;
                let batch = pos..next_pos;
                pos = next_pos;
                Primitive {
                    mesh: self,
                    index,
                    fragments: &self
                        .fragment
                        .polygons
                        .get(batch)
                        .expect("Primitive fragments out of range"),
                    material_idx: *material_idx as usize,
                }
            })
            .collect()
    }
}

#[derive(Debug)]
pub struct Primitive<'a> {
    mesh: &'a Mesh<'a>,
    index: usize,
    fragments: &'a [MeshFragmentPolygonEntry],
    material_idx: usize,
}

impl<'a> Primitive<'a> {
    /// Indices of the positions making up this primitive. These refer to positions on the parent
    /// mesh.
    pub fn indices(&self) -> Vec<u32> {
        self.fragments
            .iter()
            .flat_map(|v| {
                vec![
                    v.vertex_indexes.0 as u32,
                    v.vertex_indexes.1 as u32,
                    v.vertex_indexes.2 as u32,
                ]
            })
            .collect()
    }

    pub fn positions(&self) -> Vec<[f32; 3]> {
        self.mesh.positions()
    }

    pub fn normals(&self) -> Vec<[f32; 3]> {
        self.mesh.normals()
    }

    pub fn texture_coordinates(&self) -> Vec<[f32; 2]> {
        self.mesh.texture_coordinates()
    }

    /// The material that this primitive uses.
    pub fn material(&self) -> Material {
        self.mesh.materials().remove(self.material_idx)
    }

    /// The index of this primitive in its parent mesh.
    pub fn index(&self) -> usize {
        self.index
    }
}

#[derive(Debug)]
pub struct Material<'a> {
    doc: &'a WldDoc<'a>,
    fragment: MaterialFragment,
    name: Option<&'a str>,
}

impl<'a> Material<'a> {
    /// The name of the material
    pub fn name(&self) -> Option<&str> {
        self.name
    }

    /// The color texture for this material.
    pub fn base_color_texture(&self) -> Option<Texture> {
        self.doc
            .get(&self.fragment.reference)
            .and_then(|(_, texture_ref)| self.doc.get(&texture_ref.reference))
            .map(|(name, fragment)| Texture {
                doc: self.doc,
                fragment,
                name,
            })
    }
}

#[derive(Debug)]
pub struct Texture<'a> {
    doc: &'a WldDoc<'a>,
    fragment: TextureFragment,
    name: Option<&'a str>,
}

impl<'a> Texture<'a> {
    /// The name of the texture
    pub fn name(&self) -> Option<&str> {
        self.name
    }

    /// The name of the source image used by this texture. Wld files in theory support multiple
    /// source images per texture but in practice only ever seem to have one.
    pub fn source(&self) -> Option<String> {
        self.fragment
            .references
            .iter()
            // [TextureFragment]s reference a [TextureImagesFragment]
            .map(|r| self.doc.get(&r))
            .flat_map(|image| match image {
                // The [TextureImagesFragment] itself contains a collection of filenames. In
                // practice this seems to always be just a single filename.
                Some((_, i)) => i
                    .entries
                    .iter()
                    // These also seem to be stored in all caps. The s3d files however store
                    // filenames in lowercase. This accounts for that.
                    .map(|e| e.file_name.to_lowercase())
                    .collect::<Vec<_>>(),
                None => vec![],
            })
            .nth(0)
    }
}