scene_tree.h - エンジンの C API リファレンス

scene_tree.h
  1. #pragma once
  2. #include "matrix4x4.h"
  3. #include "allocator.h"
  4. #include "hash_function.h"
  5. #include "hash_function_string.h"
  6. #include "vector.h"
  7. #include "hash_map.h"
  8. #include "id_string.h"
  9. namespace stingray_plugin_foundation {
  10. enum ChannelType {
  11. CT_FLOAT1 = 0,
  12. CT_FLOAT2,
  13. CT_FLOAT3,
  14. CT_FLOAT4,
  15. CT_MATRIX4x4,
  16. CT_QUATERNION,
  17. CT_FLOAT3_CMP_11_11_10,
  18. CT_HALF1,
  19. CT_HALF2,
  20. CT_HALF3,
  21. CT_HALF4,
  22. CT_UBYTE4,
  23. CT_SHORT1,
  24. CT_SHORT2,
  25. CT_SHORT3,
  26. CT_SHORT4,
  27. CT_COUNT
  28. };
  29. uint32_t channel_size(ChannelType type);
  30. const char *Parameter_LocalTransform();
  31. const char *Channel_LocalTransform();
  32. const char *Channel_BlendShapeWeight();
  33. // General container for storing data
  34. struct Stream {
  35. ALLOCATOR_AWARE;
  36. Stream(Allocator &a) : data(a), channels(a), size(0), stride(0) { }
  37. // Structure describing a single data channel in the stream
  38. struct Channel {
  39. ALLOCATOR_AWARE;
  40. Channel(Allocator &a) : name(a) { }
  41. // Semantic name of channel
  42. DynamicString name;
  43. // Semantic index of channel
  44. unsigned index;
  45. // Data contained within channel
  46. ChannelType type;
  47. };
  48. // List of channels describing the contents of this stream
  49. Vector<Channel> channels;
  50. // Number of elements in stream
  51. unsigned size;
  52. // Stride in bytes between two elements
  53. unsigned stride;
  54. // Stream data
  55. Array<char> data;
  56. };
  57. bool operator==(const Stream::Channel &lhs, const Stream::Channel &rhs);
  58. bool operator!=(const Stream::Channel &lhs, const Stream::Channel &rhs);
  59. typedef Vector<Stream> Streams;
  60. // Structure that represents a scene node
  61. struct Node {
  62. ALLOCATOR_AWARE;
  63. Node(Allocator &a) : parent(a), children(a), geometries(a), viewport_visible(true), has_local_mirroring(false) { }
  64. // Name of parent (can be empty)
  65. DynamicString parent;
  66. // List with names of this nodes children
  67. Vector<DynamicString> children;
  68. // Local transformation matrix
  69. Matrix4x4 local;
  70. // List with names of geometries attached to this node
  71. Vector<DynamicString> geometries;
  72. // Initial visibility preference (overridden by renderables section of .unit)
  73. bool viewport_visible;
  74. // We keep this information here to avoid computing the local transform determinant
  75. // every time we want to know whether the transform is mirrored or not.
  76. // NOTE: we neither serialize this information but compute it from local once read.
  77. bool has_local_mirroring;
  78. void swap(Node &o) {
  79. parent.swap(o.parent);
  80. children.swap(o.children);
  81. std::swap(local, o.local);
  82. geometries.swap(o.geometries);
  83. std::swap(viewport_visible, o.viewport_visible);
  84. std::swap(has_local_mirroring, o.has_local_mirroring);
  85. }
  86. };
  87. // Structure that represents mesh topology
  88. struct Indices {
  89. ALLOCATOR_AWARE;
  90. Indices(Allocator &a) : streams(a), n_indices(0), type(TRIANGLE_LIST) { }
  91. // Number of indices
  92. unsigned n_indices;
  93. // Primitive type - currently we are only handling TRIANGLE_LIST
  94. enum PrimitiveType { TRIANGLE_LIST };
  95. PrimitiveType type;
  96. struct Stream {
  97. ALLOCATOR_AWARE;
  98. Stream(Allocator &a) : indices(a) { }
  99. Array<unsigned> indices;
  100. };
  101. // List of indices for each stream in a mesh
  102. Vector<Stream> streams;
  103. };
  104. // Structure describing a Skin modifier
  105. struct Skin {
  106. ALLOCATOR_AWARE;
  107. Skin(Allocator &a) : joints(a), max_affecting_bones(0) { }
  108. // Describes a skin joint
  109. struct Joint {
  110. ALLOCATOR_AWARE;
  111. Joint(Allocator &a) : name(a) { }
  112. // Name of joint
  113. DynamicString name;
  114. // Inverse bind matrix for joint
  115. Matrix4x4 inv_bind_matrix;
  116. };
  117. // Formerly "Bind shape matrix for skin".
  118. // This information is actually stored by cluster in FBX and there is no such
  119. // global bind matrix anymore there. Nevertheless, it is used in BSI exporters
  120. // so we keep it for backward-compatibility there and remove it from anywhere
  121. // else, namely the engine.
  122. #ifdef BSI_PLUGIN
  123. Matrix4x4 bind_matrix;
  124. #endif
  125. // Maximum number of influences
  126. unsigned max_affecting_bones;
  127. // Skin joints
  128. Vector<Joint> joints;
  129. };
  130. // Structure describing a mesh instance
  131. struct GeometryInstance {
  132. ALLOCATOR_AWARE;
  133. GeometryInstance(Allocator &a) : geometry(a), materials(a) { }
  134. DynamicString geometry;
  135. Vector<DynamicString> materials;
  136. };
  137. // Structure describing a mesh
  138. struct Geometry {
  139. ALLOCATOR_AWARE;
  140. Geometry(Allocator &a) : name(a), streams(a), indices(a), materials(a), skin(a), primitive_smoothing(a), blend_shape_targets(a), shadow_caster(true), vertex_position_remapping(a), vertex_normal_remapping(a) { }
  141. DynamicString name;
  142. // List of vertex data streams
  143. Streams streams;
  144. // Indices of mesh
  145. Indices indices;
  146. // Name of skin if mesh is skinned
  147. DynamicString skin;
  148. // Structure describing a mesh material
  149. struct Material {
  150. ALLOCATOR_AWARE;
  151. Material(Allocator &a) : name(a), primitives(a) { }
  152. // Material name
  153. DynamicString name;
  154. // List of primitive indices that the material is assigned to
  155. Array<unsigned> primitives;
  156. };
  157. // List of materials assigned to this mesh
  158. Vector<Material> materials;
  159. // Per primitive smoothing group information
  160. Array<unsigned> primitive_smoothing;
  161. // Structure describing a blend shape target
  162. struct BlendShapeTarget {
  163. ALLOCATOR_AWARE;
  164. BlendShapeTarget(Allocator &a) : name(a), streams(a) { }
  165. // Name of blend_shape target
  166. DynamicString name;
  167. // Blend Shape Target vertex data streams
  168. Streams streams;
  169. };
  170. // BlendShape targets
  171. Vector<BlendShapeTarget> blend_shape_targets;
  172. // Initial shadow preference (overridden by renderables section of .unit)
  173. bool shadow_caster;
  174. Array<unsigned> vertex_position_remapping;
  175. Array<unsigned> vertex_normal_remapping;
  176. };
  177. // Structure describing a light
  178. struct Light {
  179. enum DefinitionType { // same enum values as found in: source\engine\engine\scene\light.h
  180. OMNI = 0,
  181. SPOT = 1,
  182. BOX = 2,
  183. DIRECTIONAL = 3
  184. };
  185. DefinitionType type;
  186. float color[4];
  187. float falloff_start; // units are meters
  188. float falloff_end; // units are meters
  189. float spot_angle_start; // radians
  190. float spot_angle_end; // radians
  191. bool cast_shadow;
  192. };
  193. // Structure describing a camera
  194. struct Camera {
  195. enum DefinitionType { // same enum values as found in source\engine\engine\scene\camera.h
  196. ORTHOGRAPHIC,
  197. PERSPECTIVE
  198. };
  199. DefinitionType type;
  200. float near_plane; // units are meters
  201. float far_plane; // units are meters
  202. float vertical_fov; // units are radians
  203. };
  204. // Structure describing a animation.
  205. // there are 4 types of animation curve :
  206. // NODE_LOCAL_TRANSFORM : in this case, parameter == "local_tm". The animation has 1 channel of CT_MATRIX4x4 of name "matrix".
  207. // NODE_PROPERTY : in this case, parameter is the name of the animated property. The animation has up to 4 channels of CT_FLOAT1 named "0","1","2","3"
  208. // NODE_BLENDSHAPECHANNEL : in this case, parameter is the name of the blend shape channel. The animation has 1 channel of CT_FLOAT1 named "weight"
  209. // MATERIAL_PROPERTY : in this case, parameter is the name of the animated property. The animation has up to 4 channels of CT_FLOAT1 named "0","1","2","3"
  210. struct Animation {
  211. ALLOCATOR_AWARE;
  212. Animation(Allocator &a) : node(a), parameter(a), times(a), data(a) { }
  213. bool is_local_transform_anim() const { return data.channels.size() == 1 && data.channels[0].type == CT_MATRIX4x4 && parameter == Parameter_LocalTransform() && data.channels[0].name == Channel_LocalTransform(); }
  214. // Name of node this animation affects, or name of the material
  215. DynamicString node;
  216. // Name of affected parameter
  217. DynamicString parameter;
  218. // List of time keys
  219. Array<float> times;
  220. // Data stream containing the animation data (1:1 mapping with 'times' indices)
  221. Stream data;
  222. };
  223. // Structure describing an animation take
  224. struct AnimationTake {
  225. ALLOCATOR_AWARE;
  226. AnimationTake(Allocator &a) : animations(a), start_time(0), end_time(0), nb_samples(0) { }
  227. // infos
  228. float start_time;
  229. float end_time;
  230. unsigned nb_samples;
  231. // animations for each node
  232. Vector<Animation> animations;
  233. };
  234. // Structure describing a surface material
  235. struct SurfaceMaterial {
  236. ALLOCATOR_AWARE;
  237. SurfaceMaterial(Allocator& a) : type(LAMBERT), properties(a), nb_influences(0) { }
  238. // Type of definition
  239. enum DefinitionType {
  240. LAMBERT = 0,
  241. PHONG = 1,
  242. SHADER = 2
  243. };
  244. DefinitionType type;
  245. // Next version, we should consider adding a Datatype attribute so we know what kind of data is stored
  246. // by the property. This will help representing shaders parameters since they can be: bool, ints, enums,
  247. // floats, etc...
  248. struct Property {
  249. ALLOCATOR_AWARE;
  250. Property(Allocator& a) : n_integers(0), n_floats(0), name(a), value_string(a), textures(a) { }
  251. unsigned n_integers;
  252. int integer;
  253. unsigned n_floats;
  254. float floats[4];
  255. DynamicString name;
  256. DynamicString value_string;
  257. // reference to textures attached to this property
  258. Vector<DynamicString> textures;
  259. };
  260. typedef Vector<Property> Properties;
  261. Properties properties;
  262. // number of bones influences [0..4]
  263. unsigned nb_influences;
  264. };
  265. // Structure describing a texture
  266. struct Texture {
  267. ALLOCATOR_AWARE;
  268. Texture(Allocator& a) : file_path(a), relative_path(a), embedded(false), uv_rotation(0)
  269. {
  270. uv_offset.x = uv_offset.y = 0;
  271. uv_scale.x = uv_scale.y = 1;
  272. }
  273. DynamicString file_path;
  274. DynamicString relative_path;
  275. // was embedded in source .fbx file
  276. bool embedded;
  277. Vector2 uv_offset;
  278. Vector2 uv_scale;
  279. float uv_rotation;
  280. };
  281. // Structure describing a Level of Details
  282. struct LevelOfDetail {
  283. ALLOCATOR_AWARE;
  284. LevelOfDetail(Allocator& a) : orientation_node(a), bounding_volume(a), requires_predefined_pct(0), steps(a) { }
  285. DynamicString orientation_node;
  286. DynamicString bounding_volume;
  287. bool requires_predefined_pct;
  288. struct Step {
  289. ALLOCATOR_AWARE;
  290. Step(Allocator& a) : nodes(a), min_pct(0.0f), max_pct(1.0f) {}
  291. Vector<DynamicString> nodes;
  292. float min_pct;
  293. float max_pct;
  294. };
  295. Vector<Step> steps;
  296. };
  297. // Structure describing how a scene is interpreted when importing
  298. struct SceneImportOptions {
  299. SceneImportOptions() :
  300. combine_meshes(false),
  301. combine_meshes_by_material(false),
  302. reverse_forward_axis(false),
  303. skip_create_extra_root(false),
  304. skip_textures(false),
  305. skip_lights(false),
  306. skip_cameras(false),
  307. create_missing_uvs(false),
  308. tangents(TANGENTS_IMPORT)
  309. { }
  310. enum TangentsOption {TANGENTS_IMPORT, TANGENTS_MIKKTSPACE};
  311. bool combine_meshes;
  312. bool combine_meshes_by_material;
  313. bool reverse_forward_axis;
  314. bool skip_create_extra_root;
  315. bool skip_textures;
  316. bool skip_lights;
  317. bool skip_cameras;
  318. bool create_missing_uvs;
  319. TangentsOption tangents;
  320. };
  321. // Structure describing a scene
  322. struct SceneDatabase {
  323. SceneDatabase(Allocator &a) : nodes(a), roots(a), geometry_instances(a), geometries(a), lights(a), cameras(a), skins(a), materials(a), textures(a), lods(a), anim_takes(a), source_path(a), properties(a), geometry_entries(a) { }
  324. Allocator &allocator() const {return nodes.allocator();}
  325. typedef HashMap<DynamicString, Node, string_hash> Nodes;
  326. Nodes nodes;
  327. Vector<DynamicString> roots;
  328. typedef HashMap<DynamicString, GeometryInstance, string_hash> GeometryInstances;
  329. GeometryInstances geometry_instances;
  330. typedef Vector<Geometry> Geometries;
  331. Geometries geometries;
  332. typedef HashMap<DynamicString, Light, string_hash> Lights;
  333. Lights lights;
  334. typedef HashMap<DynamicString, Camera, string_hash> Cameras;
  335. Cameras cameras;
  336. typedef HashMap<DynamicString, Skin, string_hash> Skins;
  337. Skins skins;
  338. typedef HashMap<DynamicString, SurfaceMaterial, string_hash> Materials;
  339. Materials materials;
  340. typedef HashMap<DynamicString, Texture, string_hash> Textures;
  341. Textures textures;
  342. typedef HashMap<DynamicString, LevelOfDetail, string_hash> Lods;
  343. Lods lods;
  344. typedef HashMap<DynamicString, AnimationTake, string_hash> AnimTakes;
  345. AnimTakes anim_takes;
  346. DynamicString source_path;
  347. SceneImportOptions import_options;
  348. typedef HashMap<DynamicString, DynamicString, string_hash> Properties;
  349. Properties properties;
  350. typedef HashMap<IdString32, unsigned, idstring_hash> GeometryEntries;
  351. GeometryEntries geometry_entries;
  352. Geometry *find_geometry(const char *name) {
  353. const IdString32 id(name);
  354. auto it = geometry_entries.find(id);
  355. return it != geometry_entries.end() ? &geometries[it->second] : nullptr;
  356. }
  357. Geometry &add_geometry(const char *name) {
  358. const IdString32 id(name);
  359. geometry_entries.insert(id, geometries.size());
  360. Geometry &geom = geometries.extend();
  361. geom.name = name;
  362. return geom;
  363. }
  364. };
  365. }
  366. #include "scene_tree.inl"