initial work to split into dart_filament and flutter_filament
This commit is contained in:
7
dart_filament/lib/dart_filament.dart
Normal file
7
dart_filament/lib/dart_filament.dart
Normal file
@@ -0,0 +1,7 @@
|
||||
library filament_dart;
|
||||
|
||||
export 'dart_filament/abstract_filament_viewer.dart';
|
||||
export 'dart_filament/filament_viewer_impl.dart';
|
||||
export 'dart_filament/dart_filament.g.dart';
|
||||
|
||||
export 'dart_filament/entities/entity_transform_controller.dart';
|
||||
649
dart_filament/lib/dart_filament/abstract_filament_viewer.dart
Normal file
649
dart_filament/lib/dart_filament/abstract_filament_viewer.dart
Normal file
@@ -0,0 +1,649 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ffi';
|
||||
import 'package:animation_tools_dart/animation_tools_dart.dart';
|
||||
import 'package:dart_filament/dart_filament/entities/filament_entity.dart';
|
||||
import 'package:dart_filament/dart_filament/entities/gizmo.dart';
|
||||
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
typedef RenderCallback = Pointer<NativeFunction<Void Function(Pointer<Void>)>>;
|
||||
|
||||
// "picking" means clicking/tapping on the viewport, and unprojecting the X/Y coordinate to determine whether any renderable entities were present at those coordinates.
|
||||
typedef FilamentPickResult = ({FilamentEntity entity, double x, double y});
|
||||
|
||||
// copied from filament/backened/DriverEnums.h
|
||||
enum PrimitiveType {
|
||||
// don't change the enums values (made to match GL)
|
||||
POINTS, //!< points
|
||||
LINES, //!< lines
|
||||
UNUSED1,
|
||||
LINE_STRIP, //!< line strip
|
||||
TRIANGLES, //!< triangles
|
||||
TRIANGLE_STRIP, //!< triangle strip
|
||||
}
|
||||
|
||||
enum ToneMapper { ACES, FILMIC, LINEAR }
|
||||
|
||||
// see filament Manipulator.h for more details
|
||||
enum ManipulatorMode { ORBIT, MAP, FREE_FLIGHT }
|
||||
|
||||
class TextureDetails {
|
||||
final int textureId;
|
||||
|
||||
// both width and height are in physical, not logical pixels
|
||||
final int width;
|
||||
final int height;
|
||||
|
||||
TextureDetails(
|
||||
{required this.textureId, required this.width, required this.height});
|
||||
}
|
||||
|
||||
abstract class AbstractFilamentViewer {
|
||||
///
|
||||
/// The result(s) of calling [pick] (see below).
|
||||
/// This may be a broadcast stream, so you should ensure you have subscribed to this stream before calling [pick].
|
||||
/// If [pick] is called without an active subscription to this stream, the results will be silently discarded.
|
||||
///
|
||||
Stream<FilamentPickResult> get pickResult;
|
||||
|
||||
///
|
||||
/// Whether the controller is currently rendering at [framerate].
|
||||
///
|
||||
bool get rendering;
|
||||
|
||||
///
|
||||
/// Set to true to continuously render the scene at the framerate specified by [setFrameRate] (60 fps by default).
|
||||
///
|
||||
Future setRendering(bool render);
|
||||
|
||||
///
|
||||
/// Render a single frame.
|
||||
///
|
||||
Future render();
|
||||
|
||||
///
|
||||
/// Sets the framerate for continuous rendering when [setRendering] is enabled.
|
||||
///
|
||||
Future setFrameRate(int framerate);
|
||||
|
||||
///
|
||||
/// Destroys/disposes the viewer (including the entire scene). You cannot use the viewer after calling this method.
|
||||
///
|
||||
Future dispose();
|
||||
|
||||
///
|
||||
/// Set the background image to [path] (which should have a file extension .png, .jpg, or .ktx).
|
||||
/// This will be rendered at the maximum depth (i.e. behind all other objects including the skybox).
|
||||
/// If [fillHeight] is false, the image will be rendered at its original size. Note this may cause issues with pixel density so be sure to specify the correct resolution
|
||||
/// If [fillHeight] is true, the image will be stretched/compressed to fit the height of the viewport.
|
||||
///
|
||||
Future setBackgroundImage(String path, {bool fillHeight = false});
|
||||
|
||||
///
|
||||
/// Moves the background image to the relative offset from the origin (bottom-left) specified by [x] and [y].
|
||||
/// If [clamp] is true, the image cannot be positioned outside the bounds of the viewport.
|
||||
///
|
||||
Future setBackgroundImagePosition(double x, double y, {bool clamp = false});
|
||||
|
||||
///
|
||||
/// Removes the background image.
|
||||
///
|
||||
Future clearBackgroundImage();
|
||||
|
||||
///
|
||||
/// Sets the color for the background plane (positioned at the maximum depth, i.e. behind all other objects including the skybox).
|
||||
///
|
||||
Future setBackgroundColor(double r, double g, double b, double alpha);
|
||||
|
||||
///
|
||||
/// Load a skybox from [skyboxPath] (which must be a .ktx file)
|
||||
///
|
||||
Future loadSkybox(String skyboxPath);
|
||||
|
||||
///
|
||||
/// Removes the skybox from the scene.
|
||||
///
|
||||
Future removeSkybox();
|
||||
|
||||
///
|
||||
/// Loads an image-based light from the specified path at the given intensity.
|
||||
/// Only one IBL can be active at any given time; if an IBL has already been loaded, it will be replaced.
|
||||
///
|
||||
Future loadIbl(String lightingPath, {double intensity = 30000});
|
||||
|
||||
///
|
||||
/// Rotates the IBL & skybox.
|
||||
///
|
||||
Future rotateIbl(Matrix3 rotation);
|
||||
|
||||
///
|
||||
/// Removes the image-based light from the scene.
|
||||
///
|
||||
Future removeIbl();
|
||||
|
||||
///
|
||||
/// Adds a dynamic light to the scene.
|
||||
/// copied from filament LightManager.h
|
||||
/// enum class Type : uint8_t {
|
||||
/// SUN, //!< Directional light that also draws a sun's disk in the sky.
|
||||
/// DIRECTIONAL, //!< Directional light, emits light in a given direction.
|
||||
/// POINT, //!< Point light, emits light from a position, in all directions.
|
||||
/// FOCUSED_SPOT, //!< Physically correct spot light.
|
||||
/// SPOT, //!< Spot light with coupling of outer cone and illumination disabled.
|
||||
/// };
|
||||
Future<FilamentEntity> addLight(
|
||||
int type,
|
||||
double colour,
|
||||
double intensity,
|
||||
double posX,
|
||||
double posY,
|
||||
double posZ,
|
||||
double dirX,
|
||||
double dirY,
|
||||
double dirZ,
|
||||
bool castShadows);
|
||||
|
||||
Future removeLight(FilamentEntity light);
|
||||
|
||||
///
|
||||
/// Remove all lights (excluding IBL) from the scene.
|
||||
///
|
||||
Future clearLights();
|
||||
|
||||
///
|
||||
/// Load the .glb asset at the given path and insert into the scene.
|
||||
///
|
||||
Future<FilamentEntity> loadGlb(String path, {int numInstances = 1});
|
||||
|
||||
///
|
||||
/// Create a new instance of [entity].
|
||||
///
|
||||
Future<FilamentEntity> createInstance(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Returns the number of instances of the asset associated with [entity].
|
||||
///
|
||||
Future<int> getInstanceCount(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Returns all instances of [entity].
|
||||
///
|
||||
Future<List<FilamentEntity>> getInstances(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Load the .gltf asset at the given path and insert into the scene.
|
||||
/// [relativeResourcePath] is the folder path where the glTF resources are stored;
|
||||
/// this is usually the parent directory of the .gltf file itself.
|
||||
///
|
||||
Future<FilamentEntity> loadGltf(String path, String relativeResourcePath,
|
||||
{bool force = false});
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future panStart(double x, double y);
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future panUpdate(double x, double y);
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future panEnd();
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future rotateStart(double x, double y);
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future rotateUpdate(double x, double y);
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future rotateEnd();
|
||||
|
||||
///
|
||||
/// Set the weights for all morph targets in [entity] to [weights].
|
||||
/// Note that [weights] must contain values for ALL morph targets, but no exception will be thrown if you don't do so (you'll just get incorrect results).
|
||||
/// If you only want to set one value, set all others to zero (check [getMorphTargetNames] if you need the get a list of all morph targets).
|
||||
/// IMPORTANT - this accepts the actual FilamentEntity with the relevant morph targets (unlike [getMorphTargetNames], which uses the parent entity and the child mesh name).
|
||||
/// Use [getChildEntityByName] if you are setting the weights for a child mesh.
|
||||
///
|
||||
Future setMorphTargetWeights(FilamentEntity entity, List<double> weights);
|
||||
|
||||
///
|
||||
/// Gets the names of all morph targets for the mesh [meshName] under [entity].
|
||||
///
|
||||
Future<List<String>> getMorphTargetNames(
|
||||
FilamentEntity entity, String meshName);
|
||||
|
||||
Future<List<String>> getAnimationNames(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Returns the length (in seconds) of the animation at the given index.
|
||||
///
|
||||
Future<double> getAnimationDuration(
|
||||
FilamentEntity entity, int animationIndex);
|
||||
|
||||
///
|
||||
/// Animate the morph targets in [entity]. See [MorphTargetAnimation] for an explanation as to how to construct the animation frame data.
|
||||
/// This method will check the morph target names specified in [animation] against the morph target names that actually exist exist under [meshName] in [entity],
|
||||
/// throwing an exception if any cannot be found.
|
||||
/// It is permissible for [animation] to omit any targets that do exist under [meshName]; these simply won't be animated.
|
||||
///
|
||||
Future setMorphAnimationData(
|
||||
FilamentEntity entity, MorphAnimationData animation,
|
||||
{List<String>? targetMeshNames});
|
||||
|
||||
///
|
||||
/// Resets all bones in the given entity to their rest pose.
|
||||
/// This should be done before every call to addBoneAnimation.
|
||||
///
|
||||
Future resetBones(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Transforms the bone(s)/joint(s) according [animation].
|
||||
/// To set the instantaneous transform, just use a single frame.
|
||||
///
|
||||
Future addBoneAnimation(FilamentEntity entity, BoneAnimationData animation);
|
||||
|
||||
///
|
||||
/// Removes/destroys the specified entity from the scene.
|
||||
/// [entity] will no longer be a valid handle after this method is called; ensure you immediately discard all references once this method is complete.
|
||||
///
|
||||
Future removeEntity(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Removes/destroys all renderable entities from the scene (including cameras).
|
||||
/// All [FilamentEntity] handles will no longer be valid after this method is called; ensure you immediately discard all references to all entities once this method is complete.
|
||||
///
|
||||
Future clearEntities();
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future zoomBegin();
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future zoomUpdate(double x, double y, double z);
|
||||
|
||||
///
|
||||
/// Called by `FilamentGestureDetector`. You probably don't want to call this yourself.
|
||||
///
|
||||
Future zoomEnd();
|
||||
|
||||
///
|
||||
/// Schedules the glTF animation at [index] in [entity] to start playing on the next frame.
|
||||
///
|
||||
Future playAnimation(FilamentEntity entity, int index,
|
||||
{bool loop = false,
|
||||
bool reverse = false,
|
||||
bool replaceActive = true,
|
||||
double crossfade = 0.0});
|
||||
|
||||
///
|
||||
/// Schedules the glTF animation at [index] in [entity] to start playing on the next frame.
|
||||
///
|
||||
Future playAnimationByName(FilamentEntity entity, String name,
|
||||
{bool loop = false,
|
||||
bool reverse = false,
|
||||
bool replaceActive = true,
|
||||
double crossfade = 0.0});
|
||||
|
||||
Future setAnimationFrame(
|
||||
FilamentEntity entity, int index, int animationFrame);
|
||||
|
||||
Future stopAnimation(FilamentEntity entity, int animationIndex);
|
||||
Future stopAnimationByName(FilamentEntity entity, String name);
|
||||
|
||||
///
|
||||
/// Sets the current scene camera to the glTF camera under [name] in [entity].
|
||||
///
|
||||
Future setCamera(FilamentEntity entity, String? name);
|
||||
|
||||
///
|
||||
/// Sets the current scene camera to the main camera (which is always available and added to every scene by default).
|
||||
///
|
||||
Future setMainCamera();
|
||||
|
||||
///
|
||||
/// Returns the entity associated with the main camera.
|
||||
///
|
||||
Future<FilamentEntity> getMainCamera();
|
||||
|
||||
///
|
||||
/// Sets the current scene camera to the glTF camera under [name] in [entity].
|
||||
///
|
||||
Future setCameraFov(double degrees, double width, double height);
|
||||
|
||||
///
|
||||
/// Sets the tone mapping (requires postprocessing).
|
||||
///
|
||||
Future setToneMapping(ToneMapper mapper);
|
||||
|
||||
///
|
||||
/// Sets the strength of the bloom.
|
||||
///
|
||||
Future setBloom(double bloom);
|
||||
|
||||
///
|
||||
/// Sets the focal length of the camera. Default value is 28.0.
|
||||
///
|
||||
Future setCameraFocalLength(double focalLength);
|
||||
|
||||
///
|
||||
/// Sets the distance (in world units) to the near/far planes for the active camera. Default values are 0.05/1000.0. See Camera.h for details.
|
||||
///
|
||||
Future setCameraCulling(double near, double far);
|
||||
|
||||
///
|
||||
/// Get the distance (in world units) to the near culling plane for the active camera.
|
||||
///
|
||||
Future<double> getCameraCullingNear();
|
||||
|
||||
///
|
||||
/// Get the distance (in world units) to the far culling plane for the active camera.
|
||||
///
|
||||
Future<double> getCameraCullingFar();
|
||||
|
||||
///
|
||||
/// Sets the focus distance for the camera.
|
||||
///
|
||||
Future setCameraFocusDistance(double focusDistance);
|
||||
|
||||
///
|
||||
/// Get the camera position in world space.
|
||||
///
|
||||
Future<Vector3> getCameraPosition();
|
||||
|
||||
///
|
||||
/// Get the camera's model matrix.
|
||||
///
|
||||
Future<Matrix4> getCameraModelMatrix();
|
||||
|
||||
///
|
||||
/// Get the camera's view matrix. See Camera.h for more details.
|
||||
///
|
||||
Future<Matrix4> getCameraViewMatrix();
|
||||
|
||||
///
|
||||
/// Get the camera's projection matrix. See Camera.h for more details.
|
||||
///
|
||||
Future<Matrix4> getCameraProjectionMatrix();
|
||||
|
||||
///
|
||||
/// Get the camera's culling projection matrix. See Camera.h for more details.
|
||||
///
|
||||
Future<Matrix4> getCameraCullingProjectionMatrix();
|
||||
|
||||
///
|
||||
/// Get the camera's culling frustum in world space. Returns a (vector_math) [Frustum] instance where plane0-plane6 define the left, right, bottom, top, far and near planes respectively.
|
||||
/// See Camera.h and (filament) Frustum.h for more details.
|
||||
///
|
||||
Future<Frustum> getCameraFrustum();
|
||||
|
||||
///
|
||||
/// Set the camera position in world space. Note this is not persistent - any viewport navigation will reset the camera transform.
|
||||
///
|
||||
Future setCameraPosition(double x, double y, double z);
|
||||
|
||||
///
|
||||
/// Get the camera rotation matrix.
|
||||
///
|
||||
Future<Matrix3> getCameraRotation();
|
||||
|
||||
///
|
||||
/// Repositions the camera to the last vertex of the bounding box of [entity], looking at the penultimate vertex.
|
||||
///
|
||||
Future moveCameraToAsset(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Enables/disables frustum culling. Currently we don't expose a method for manipulating the camera projection/culling matrices so this is your only option to deal with unwanted near/far clipping.
|
||||
///
|
||||
Future setViewFrustumCulling(bool enabled);
|
||||
|
||||
///
|
||||
/// Sets the camera exposure.
|
||||
///
|
||||
Future setCameraExposure(
|
||||
double aperture, double shutterSpeed, double sensitivity);
|
||||
|
||||
///
|
||||
/// Rotate the camera by [rads] around the given axis. Note this is not persistent - any viewport navigation will reset the camera transform.
|
||||
///
|
||||
Future setCameraRotation(Quaternion quaternion);
|
||||
|
||||
///
|
||||
/// Sets the camera model matrix.
|
||||
///
|
||||
Future setCameraModelMatrix(List<double> matrix);
|
||||
|
||||
///
|
||||
/// Sets the `baseColorFactor` property for the material at index [materialIndex] in [entity] under node [meshName] to [color].
|
||||
///
|
||||
Future setMaterialColor(FilamentEntity entity, String meshName,
|
||||
int materialIndex, double r, double g, double b, double a);
|
||||
|
||||
///
|
||||
/// Scale [entity] to fit within the unit cube.
|
||||
///
|
||||
Future transformToUnitCube(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Directly sets the world space position for [entity] to the given coordinates, skipping all collision detection.
|
||||
///
|
||||
Future setPosition(FilamentEntity entity, double x, double y, double z);
|
||||
|
||||
///
|
||||
/// Directly sets the scale for [entity], skipping all collision detection.
|
||||
///
|
||||
Future setScale(FilamentEntity entity, double scale);
|
||||
|
||||
///
|
||||
/// Directly sets the rotation for [entity] to [rads] around the axis {x,y,z}, skipping all collision detection.
|
||||
///
|
||||
Future setRotation(
|
||||
FilamentEntity entity, double rads, double x, double y, double z);
|
||||
|
||||
///
|
||||
/// Queues an update to the worldspace position for [entity] to {x,y,z}.
|
||||
/// The actual update will occur on the next frame, and will be subject to collision detection.
|
||||
///
|
||||
Future queuePositionUpdate(
|
||||
FilamentEntity entity, double x, double y, double z,
|
||||
{bool relative = false});
|
||||
|
||||
///
|
||||
/// Queues an update to the worldspace rotation for [entity].
|
||||
/// The actual update will occur on the next frame, and will be subject to collision detection.
|
||||
///
|
||||
Future queueRotationUpdate(
|
||||
FilamentEntity entity, double rads, double x, double y, double z,
|
||||
{bool relative = false});
|
||||
|
||||
///
|
||||
/// Same as [queueRotationUpdate].
|
||||
///
|
||||
Future queueRotationUpdateQuat(FilamentEntity entity, Quaternion quat,
|
||||
{bool relative = false});
|
||||
|
||||
///
|
||||
/// Enable/disable postprocessing.
|
||||
///
|
||||
Future setPostProcessing(bool enabled);
|
||||
|
||||
///
|
||||
/// Set antialiasing options.
|
||||
///
|
||||
Future setAntiAliasing(bool msaa, bool fxaa, bool taa);
|
||||
|
||||
///
|
||||
/// Sets the rotation for [entity] to the specified quaternion.
|
||||
///
|
||||
Future setRotationQuat(FilamentEntity entity, Quaternion rotation);
|
||||
|
||||
///
|
||||
/// Reveal the node [meshName] under [entity]. Only applicable if [hide] had previously been called; this is a no-op otherwise.
|
||||
///
|
||||
Future reveal(FilamentEntity entity, String? meshName);
|
||||
|
||||
///
|
||||
/// If [meshName] is provided, hide the node [meshName] under [entity], otherwise hide the root node for [entity].
|
||||
/// The entity still exists in memory, but is no longer being rendered into the scene. Call [reveal] to re-commence rendering.
|
||||
///
|
||||
Future hide(FilamentEntity entity, String? meshName);
|
||||
|
||||
///
|
||||
/// Used to select the entity in the scene at the given viewport coordinates.
|
||||
/// Called by `FilamentGestureDetector` on a mouse/finger down event. You probably don't want to call this yourself.
|
||||
/// This is asynchronous and will require 2-3 frames to complete - subscribe to the [pickResult] stream to receive the results of this method.
|
||||
/// [x] and [y] must be in local logical coordinates (i.e. where 0,0 is at top-left of the FilamentWidget).
|
||||
///
|
||||
void pick(int x, int y);
|
||||
|
||||
///
|
||||
/// Retrieves the name assigned to the given FilamentEntity (usually corresponds to the glTF mesh name).
|
||||
///
|
||||
String? getNameForEntity(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Sets the options for manipulating the camera via the viewport.
|
||||
/// ManipulatorMode.FREE_FLIGHT and ManipulatorMode.MAP are currently unsupported and will throw an exception.
|
||||
///
|
||||
Future setCameraManipulatorOptions(
|
||||
{ManipulatorMode mode = ManipulatorMode.ORBIT,
|
||||
double orbitSpeedX = 0.01,
|
||||
double orbitSpeedY = 0.01,
|
||||
double zoomSpeed = 0.01});
|
||||
|
||||
///
|
||||
/// Returns all child entities under [parent].
|
||||
///
|
||||
Future<List<FilamentEntity>> getChildEntities(
|
||||
FilamentEntity parent, bool renderableOnly);
|
||||
|
||||
///
|
||||
/// Finds the child entity named [childName] associated with the given parent.
|
||||
/// Usually, [parent] will be the return value from [loadGlb]/[loadGltf] and [childName] will be the name of a node/mesh.
|
||||
///
|
||||
Future<FilamentEntity> getChildEntity(
|
||||
FilamentEntity parent, String childName);
|
||||
|
||||
///
|
||||
/// List the name of all child entities under the given entity.
|
||||
///
|
||||
Future<List<String>> getChildEntityNames(FilamentEntity entity,
|
||||
{bool renderableOnly = true});
|
||||
|
||||
///
|
||||
/// If [recording] is set to true, each frame the framebuffer/texture will be written to /tmp/output_*.png.
|
||||
/// This will impact performance; handle with care.
|
||||
///
|
||||
Future setRecording(bool recording);
|
||||
|
||||
///
|
||||
/// Sets the output directory where recorded PNGs will be placed.
|
||||
///
|
||||
Future setRecordingOutputDirectory(String outputDirectory);
|
||||
|
||||
///
|
||||
/// An [entity] will only be animatable after an animation component is attached.
|
||||
/// Any calls to [playAnimation]/[setBoneAnimation]/[setMorphAnimation] will have no visual effect until [addAnimationComponent] has been called on the instance.
|
||||
///
|
||||
Future addAnimationComponent(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Makes [entity] collidable.
|
||||
/// This allows you to call [testCollisions] with any other entity ("entity B") to see if [entity] has collided with entity B. The callback will be invoked if so.
|
||||
/// Alternatively, if [affectsTransform] is true and this entity collides with another entity, any queued position updates to the latter entity will be ignored.
|
||||
///
|
||||
Future addCollisionComponent(FilamentEntity entity,
|
||||
{void Function(int entityId1, int entityId2)? callback,
|
||||
bool affectsTransform = false});
|
||||
|
||||
///
|
||||
/// Removes the collision component from [entity], meaning this will no longer be tested when [testCollisions] or [queuePositionUpdate] is called with another entity.
|
||||
///
|
||||
Future removeCollisionComponent(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Creates a (renderable) entity with the specified geometry and adds to the scene.
|
||||
///
|
||||
Future createGeometry(List<double> vertices, List<int> indices,
|
||||
{String? materialPath,
|
||||
PrimitiveType primitiveType = PrimitiveType.TRIANGLES});
|
||||
|
||||
///
|
||||
/// Sets the parent transform of [child] to the transform of [parent].
|
||||
///
|
||||
Future setParent(FilamentEntity child, FilamentEntity parent);
|
||||
|
||||
///
|
||||
/// Test all collidable entities against this entity to see if any have collided.
|
||||
/// This method returns void; the relevant callback passed to [addCollisionComponent] will be fired if a collision is detected.
|
||||
///
|
||||
Future testCollisions(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// Sets the draw priority for the given entity. See RenderableManager.h for more details.
|
||||
///
|
||||
Future setPriority(FilamentEntity entityId, int priority);
|
||||
|
||||
///
|
||||
/// The Scene holds the transform gizmo and all loaded entities/lights.
|
||||
///
|
||||
Scene get scene;
|
||||
}
|
||||
|
||||
///
|
||||
/// For now, this class just holds the entities that have been loaded (though not necessarily visible in the Filament Scene).
|
||||
///
|
||||
abstract class Scene {
|
||||
///
|
||||
/// The last entity clicked/tapped in the viewport (internally, the result of calling pick);
|
||||
FilamentEntity? selected;
|
||||
|
||||
///
|
||||
/// A Stream updated whenever an entity is added/removed from the scene.
|
||||
///
|
||||
Stream<bool> get onUpdated;
|
||||
|
||||
///
|
||||
/// A Stream containing every FilamentEntity added to the scene (i.e. via [loadGlb], [loadGltf] or [addLight]).
|
||||
/// This is provided for convenience so you can set listeners in front-end widgets that can respond to entity loads without manually passing around the FilamentEntity returned from those methods.
|
||||
///
|
||||
Stream<FilamentEntity> get onLoad;
|
||||
|
||||
///
|
||||
/// A Stream containing every FilamentEntity removed from the scene (i.e. via [removeEntity], [clearEntities], [removeLight] or [clearLights]).
|
||||
|
||||
Stream<FilamentEntity> get onUnload;
|
||||
|
||||
///
|
||||
/// Lists all light entities currently loaded (not necessarily active in the scene). Does not account for instances.
|
||||
///
|
||||
Iterable<FilamentEntity> listLights();
|
||||
|
||||
///
|
||||
/// Lists all entities currently loaded (not necessarily active in the scene). Does not account for instances.
|
||||
///
|
||||
Iterable<FilamentEntity> listEntities();
|
||||
|
||||
///
|
||||
/// Attach the gizmo to the specified entity.
|
||||
///
|
||||
void select(FilamentEntity entity);
|
||||
|
||||
///
|
||||
/// The transform gizmo.
|
||||
///
|
||||
Future<Gizmo> get gizmo;
|
||||
}
|
||||
1705
dart_filament/lib/dart_filament/dart_filament.g.dart
Normal file
1705
dart_filament/lib/dart_filament/dart_filament.g.dart
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,182 @@
|
||||
import 'dart:async';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:dart_filament/dart_filament/entities/filament_entity.dart';
|
||||
import 'package:dart_filament/dart_filament/abstract_filament_viewer.dart';
|
||||
import 'package:dart_filament/dart_filament/filament_viewer_impl.dart';
|
||||
import 'package:vector_math/vector_math_64.dart' as v;
|
||||
|
||||
class EntityTransformController {
|
||||
final FilamentViewer controller;
|
||||
final FilamentEntity _entity;
|
||||
|
||||
late Timer _ticker;
|
||||
|
||||
double translationSpeed;
|
||||
double rotationRadsPerSecond;
|
||||
|
||||
bool _forward = false;
|
||||
bool _strafeLeft = false;
|
||||
bool _strafeRight = false;
|
||||
bool _back = false;
|
||||
bool _rotateLeft = false;
|
||||
bool _rotateRight = false;
|
||||
double _rotY = 0;
|
||||
|
||||
int? forwardAnimationIndex;
|
||||
int? backwardAnimationIndex;
|
||||
int? strafeLeftAnimationIndex;
|
||||
int? strafeRightAnimationIndex;
|
||||
|
||||
EntityTransformController(this.controller, this._entity,
|
||||
{this.translationSpeed = 1,
|
||||
this.rotationRadsPerSecond = pi / 2,
|
||||
this.forwardAnimationIndex,
|
||||
this.backwardAnimationIndex,
|
||||
this.strafeLeftAnimationIndex,
|
||||
this.strafeRightAnimationIndex}) {
|
||||
var translationSpeedPerTick = translationSpeed / (1000 / 16.667);
|
||||
var rotationRadsPerTick = rotationRadsPerSecond / (1000 / 16.667);
|
||||
_ticker = Timer.periodic(const Duration(milliseconds: 16), (timer) {
|
||||
_update(translationSpeedPerTick, rotationRadsPerTick);
|
||||
});
|
||||
}
|
||||
|
||||
bool _enabled = true;
|
||||
void enable() {
|
||||
_enabled = true;
|
||||
}
|
||||
|
||||
void disable() {
|
||||
_enabled = false;
|
||||
}
|
||||
|
||||
void _update(
|
||||
double translationSpeedPerTick, double rotationRadsPerTick) async {
|
||||
if (!_enabled) {
|
||||
return;
|
||||
}
|
||||
var _position = v.Vector3.zero();
|
||||
bool updateTranslation = false;
|
||||
if (_forward) {
|
||||
_position.add(v.Vector3(0, 0, -translationSpeedPerTick));
|
||||
updateTranslation = true;
|
||||
}
|
||||
if (_back) {
|
||||
_position.add(v.Vector3(0, 0, translationSpeedPerTick));
|
||||
updateTranslation = true;
|
||||
}
|
||||
if (_strafeLeft) {
|
||||
_position.add(v.Vector3(-translationSpeedPerTick, 0, 0));
|
||||
updateTranslation = true;
|
||||
}
|
||||
if (_strafeRight) {
|
||||
_position.add(v.Vector3(translationSpeedPerTick, 0, 0));
|
||||
updateTranslation = true;
|
||||
}
|
||||
|
||||
// TODO - use pitch/yaw/roll
|
||||
bool updateRotation = false;
|
||||
var _rotation = v.Quaternion.identity();
|
||||
|
||||
double rads = 0.0;
|
||||
if (_rotY != 0) {
|
||||
rads = _rotY * pi / 1000;
|
||||
var rotY = v.Quaternion.axisAngle(v.Vector3(0, 1, 0), rads).normalized();
|
||||
_rotation = rotY;
|
||||
updateRotation = true;
|
||||
_rotY = 0;
|
||||
}
|
||||
|
||||
if (updateTranslation) {
|
||||
await controller.queuePositionUpdate(
|
||||
_entity, _position.x, _position.y, _position.z,
|
||||
relative: true);
|
||||
}
|
||||
if (updateRotation) {
|
||||
await controller.queueRotationUpdateQuat(_entity, _rotation,
|
||||
relative: true);
|
||||
}
|
||||
}
|
||||
|
||||
void look(double deltaX) async {
|
||||
_rotY -= deltaX;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_ticker.cancel();
|
||||
}
|
||||
|
||||
bool _playingForwardAnimation = false;
|
||||
bool _playingBackwardAnimation = false;
|
||||
|
||||
void forwardPressed() async {
|
||||
_forward = true;
|
||||
if (forwardAnimationIndex != null && !_playingForwardAnimation) {
|
||||
await controller.playAnimation(_entity, forwardAnimationIndex!,
|
||||
loop: true, replaceActive: false);
|
||||
_playingForwardAnimation = true;
|
||||
}
|
||||
}
|
||||
|
||||
void forwardReleased() async {
|
||||
_forward = false;
|
||||
await Future.delayed(Duration(milliseconds: 50));
|
||||
if (!_forward) {
|
||||
_playingForwardAnimation = false;
|
||||
if (forwardAnimationIndex != null) {
|
||||
await controller.stopAnimation(_entity, forwardAnimationIndex!);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void backPressed() async {
|
||||
_back = true;
|
||||
if (forwardAnimationIndex != null) {
|
||||
if (!_playingBackwardAnimation) {
|
||||
await controller.playAnimation(_entity, forwardAnimationIndex!,
|
||||
loop: true, replaceActive: false, reverse: true);
|
||||
_playingBackwardAnimation = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void backReleased() async {
|
||||
_back = false;
|
||||
if (forwardAnimationIndex != null) {
|
||||
await controller.stopAnimation(_entity, forwardAnimationIndex!);
|
||||
}
|
||||
_playingBackwardAnimation = false;
|
||||
}
|
||||
|
||||
void strafeLeftPressed() {
|
||||
_strafeLeft = true;
|
||||
}
|
||||
|
||||
void strafeLeftReleased() async {
|
||||
_strafeLeft = false;
|
||||
}
|
||||
|
||||
void strafeRightPressed() {
|
||||
_strafeRight = true;
|
||||
}
|
||||
|
||||
void strafeRightReleased() async {
|
||||
_strafeRight = false;
|
||||
}
|
||||
|
||||
void Function()? _mouse1DownCallback;
|
||||
void onMouse1Down(void Function() callback) {
|
||||
_mouse1DownCallback = callback;
|
||||
}
|
||||
|
||||
void mouse1Down() async {
|
||||
_mouse1DownCallback?.call();
|
||||
}
|
||||
|
||||
void mouse1Up() async {}
|
||||
|
||||
void mouse2Up() async {}
|
||||
|
||||
void mouse2Down() async {}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// a handle that can be safely passed back to the rendering layer to manipulate an Entity
|
||||
typedef FilamentEntity = int;
|
||||
74
dart_filament/lib/dart_filament/entities/gizmo.dart
Normal file
74
dart_filament/lib/dart_filament/entities/gizmo.dart
Normal file
@@ -0,0 +1,74 @@
|
||||
import 'package:dart_filament/dart_filament/entities/filament_entity.dart';
|
||||
import 'package:dart_filament/dart_filament/filament_viewer_impl.dart';
|
||||
import 'package:vector_math/vector_math_64.dart';
|
||||
|
||||
import '../abstract_filament_viewer.dart';
|
||||
|
||||
class Gizmo {
|
||||
final FilamentEntity x;
|
||||
Vector3 _x = Vector3(0.1, 0, 0);
|
||||
final FilamentEntity y;
|
||||
Vector3 _y = Vector3(0.0, 0.1, 0);
|
||||
final FilamentEntity z;
|
||||
Vector3 _z = Vector3(0.0, 0.0, 0.1);
|
||||
|
||||
final FilamentViewer controller;
|
||||
|
||||
FilamentEntity? _activeAxis;
|
||||
FilamentEntity? _activeEntity;
|
||||
bool get isActive => _activeAxis != null;
|
||||
|
||||
Gizmo(this.x, this.y, this.z, this.controller) {
|
||||
controller.pickResult.listen(_onPickResult);
|
||||
}
|
||||
|
||||
Future _reveal() async {
|
||||
await controller.reveal(x, null);
|
||||
await controller.reveal(y, null);
|
||||
await controller.reveal(z, null);
|
||||
}
|
||||
|
||||
void translate(double x, double y) async {
|
||||
late Vector3 vec;
|
||||
if (_activeAxis == x) {
|
||||
vec = _x;
|
||||
} else if (_activeAxis == y) {
|
||||
vec = _y;
|
||||
} else if (_activeAxis == z) {
|
||||
vec = _z;
|
||||
}
|
||||
await controller.queuePositionUpdate(
|
||||
_activeEntity!, x * vec.x, -y * vec.y, -x * vec.z,
|
||||
relative: true);
|
||||
}
|
||||
|
||||
void reset() {
|
||||
_activeAxis = null;
|
||||
}
|
||||
|
||||
void _onPickResult(FilamentPickResult result) async {
|
||||
if (result.entity == x || result.entity == y || result.entity == z) {
|
||||
_activeAxis = result.entity;
|
||||
} else {
|
||||
attach(result.entity);
|
||||
}
|
||||
}
|
||||
|
||||
void attach(FilamentEntity entity) async {
|
||||
_activeAxis = null;
|
||||
_activeEntity = entity;
|
||||
await _reveal();
|
||||
await controller.setParent(x, entity);
|
||||
await controller.setParent(y, entity);
|
||||
await controller.setParent(z, entity);
|
||||
}
|
||||
|
||||
void detach() async {
|
||||
await controller.setParent(x, 0);
|
||||
await controller.setParent(y, 0);
|
||||
await controller.setParent(z, 0);
|
||||
await controller.hide(x, null);
|
||||
await controller.hide(y, null);
|
||||
await controller.hide(z, null);
|
||||
}
|
||||
}
|
||||
1195
dart_filament/lib/dart_filament/filament_viewer_impl.dart
Normal file
1195
dart_filament/lib/dart_filament/filament_viewer_impl.dart
Normal file
File diff suppressed because it is too large
Load Diff
138
dart_filament/lib/dart_filament/scene.dart
Normal file
138
dart_filament/lib/dart_filament/scene.dart
Normal file
@@ -0,0 +1,138 @@
|
||||
import 'dart:async';
|
||||
import 'dart:ffi';
|
||||
import 'package:dart_filament/dart_filament/dart_filament.g.dart';
|
||||
import 'package:dart_filament/dart_filament/entities/filament_entity.dart';
|
||||
import 'package:dart_filament/dart_filament/filament_viewer_impl.dart';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
import 'entities/gizmo.dart';
|
||||
import 'abstract_filament_viewer.dart';
|
||||
|
||||
///
|
||||
/// For now, this class just holds the entities that have been loaded (though not necessarily visible in the Filament Scene).
|
||||
///
|
||||
class SceneImpl extends Scene {
|
||||
Gizmo? _gizmo;
|
||||
Future<Gizmo> get gizmo async {
|
||||
if (_gizmo == null) {
|
||||
final out = calloc<Int32>(3);
|
||||
get_gizmo(_sceneManager!, out);
|
||||
_gizmo = Gizmo(out[0], out[1], out[2], controller);
|
||||
calloc.free(out);
|
||||
}
|
||||
return _gizmo!;
|
||||
}
|
||||
|
||||
FilamentViewer controller;
|
||||
|
||||
final Pointer<Void> _sceneManager;
|
||||
|
||||
SceneImpl(this._gizmo, this.controller, this._sceneManager);
|
||||
|
||||
@override
|
||||
FilamentEntity? selected;
|
||||
|
||||
final _onUpdatedController = StreamController<bool>.broadcast();
|
||||
@override
|
||||
Stream<bool> get onUpdated => _onUpdatedController.stream;
|
||||
|
||||
final _onLoadController = StreamController<FilamentEntity>.broadcast();
|
||||
@override
|
||||
Stream<FilamentEntity> get onLoad => _onLoadController.stream;
|
||||
|
||||
final _onUnloadController = StreamController<FilamentEntity>.broadcast();
|
||||
@override
|
||||
Stream<FilamentEntity> get onUnload => _onUnloadController.stream;
|
||||
|
||||
final _lights = <FilamentEntity>{};
|
||||
final _entities = <FilamentEntity>{};
|
||||
|
||||
void registerLight(FilamentEntity entity) {
|
||||
_lights.add(entity);
|
||||
_onLoadController.sink.add(entity);
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
void unregisterLight(FilamentEntity entity) async {
|
||||
var children = await controller.getChildEntities(entity, true);
|
||||
if (selected == entity || children.contains(selected)) {
|
||||
selected = null;
|
||||
_gizmo?.detach();
|
||||
}
|
||||
_lights.remove(entity);
|
||||
_onUnloadController.add(entity);
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
void unregisterEntity(FilamentEntity entity) async {
|
||||
var children = await controller.getChildEntities(entity, true);
|
||||
if (selected == entity || children.contains(selected)) {
|
||||
selected = null;
|
||||
_gizmo?.detach();
|
||||
}
|
||||
_entities.remove(entity);
|
||||
_onUnloadController.add(entity);
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
void registerEntity(FilamentEntity entity) {
|
||||
_entities.add(entity);
|
||||
_entities.add(entity);
|
||||
_onLoadController.sink.add(entity);
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
void clearLights() {
|
||||
for (final light in _lights) {
|
||||
if (selected == light) {
|
||||
selected = null;
|
||||
_gizmo?.detach();
|
||||
}
|
||||
_onUnloadController.add(light);
|
||||
}
|
||||
|
||||
_lights.clear();
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
void clearEntities() {
|
||||
for (final entity in _entities) {
|
||||
if (selected == entity) {
|
||||
selected = null;
|
||||
_gizmo?.detach();
|
||||
}
|
||||
_onUnloadController.add(entity);
|
||||
}
|
||||
_entities.clear();
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
///
|
||||
/// Lists all entities currently loaded (not necessarily active in the scene).
|
||||
///
|
||||
Iterable<FilamentEntity> listLights() {
|
||||
return _lights;
|
||||
}
|
||||
|
||||
@override
|
||||
Iterable<FilamentEntity> listEntities() {
|
||||
return _entities;
|
||||
}
|
||||
|
||||
void registerSelected(FilamentEntity entity) {
|
||||
selected = entity;
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
void unregisterSelected() {
|
||||
selected = null;
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
|
||||
@override
|
||||
void select(FilamentEntity entity) {
|
||||
selected = entity;
|
||||
_gizmo?.attach(entity);
|
||||
_onUpdatedController.add(true);
|
||||
}
|
||||
}
|
||||
73832
dart_filament/lib/dart_filament/swift/swift_bindings.g.dart
Normal file
73832
dart_filament/lib/dart_filament/swift/swift_bindings.g.dart
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,15 @@
|
||||
import 'package:vector_math/vector_math_64.dart' as v;
|
||||
|
||||
class CameraOrientation {
|
||||
v.Vector3 position = v.Vector3(0, 0, 0);
|
||||
|
||||
var rotationX = 0.0;
|
||||
var rotationY = 0.0;
|
||||
var rotationZ = 0.0;
|
||||
|
||||
v.Quaternion compose() {
|
||||
return v.Quaternion.axisAngle(v.Vector3(0, 0, 1), rotationZ) *
|
||||
v.Quaternion.axisAngle(v.Vector3(0, 1, 0), rotationY) *
|
||||
v.Quaternion.axisAngle(v.Vector3(1, 0, 0), rotationX);
|
||||
}
|
||||
}
|
||||
29
dart_filament/lib/dart_filament/utils/dart_resources.dart
Normal file
29
dart_filament/lib/dart_filament/utils/dart_resources.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dart_filament/dart_filament/dart_filament.g.dart';
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
class DartResourceLoader {
|
||||
static final _assets = <int, Pointer>{};
|
||||
static void loadResource(Pointer<Char> uri, Pointer<ResourceBuffer> out) {
|
||||
try {
|
||||
var data = File(uri.cast<Utf8>().toDartString().replaceAll("file://", ""))
|
||||
.readAsBytesSync();
|
||||
var ptr = calloc<Uint8>(data.lengthInBytes);
|
||||
ptr.asTypedList(data.lengthInBytes).setRange(0, data.lengthInBytes, data);
|
||||
|
||||
out.ref.data = ptr.cast<Void>();
|
||||
out.ref.size = data.lengthInBytes;
|
||||
out.ref.id = _assets.length;
|
||||
_assets[out.ref.id] = ptr;
|
||||
} catch (err) {
|
||||
print(err);
|
||||
out.ref.size = -1;
|
||||
}
|
||||
}
|
||||
|
||||
static void freeResource(ResourceBuffer rb) {
|
||||
calloc.free(_assets[rb.id]!);
|
||||
}
|
||||
}
|
||||
29
dart_filament/lib/dart_filament/utils/light_options.dart
Normal file
29
dart_filament/lib/dart_filament/utils/light_options.dart
Normal file
@@ -0,0 +1,29 @@
|
||||
import 'package:vector_math/vector_math_64.dart' as v;
|
||||
|
||||
class LightOptions {
|
||||
String? iblPath;
|
||||
double iblIntensity;
|
||||
int directionalType;
|
||||
double directionalColor;
|
||||
double directionalIntensity;
|
||||
bool directionalCastShadows;
|
||||
late v.Vector3 directionalPosition;
|
||||
late v.Vector3 directionalDirection;
|
||||
|
||||
LightOptions(
|
||||
{required this.iblPath,
|
||||
required this.iblIntensity,
|
||||
required this.directionalType,
|
||||
required this.directionalColor,
|
||||
required this.directionalIntensity,
|
||||
required this.directionalCastShadows,
|
||||
v.Vector3? directionalDirection,
|
||||
v.Vector3? directionalPosition}) {
|
||||
this.directionalDirection = directionalDirection == null
|
||||
? v.Vector3(0, -1, 0)
|
||||
: directionalDirection;
|
||||
this.directionalPosition = directionalPosition == null
|
||||
? v.Vector3(0, 100, 0)
|
||||
: directionalPosition;
|
||||
}
|
||||
}
|
||||
10
dart_filament/lib/dart_filament/utils/using_pointer.dart
Normal file
10
dart_filament/lib/dart_filament/utils/using_pointer.dart
Normal file
@@ -0,0 +1,10 @@
|
||||
import 'dart:ffi';
|
||||
|
||||
import 'package:ffi/ffi.dart';
|
||||
|
||||
final allocator = calloc;
|
||||
|
||||
void using(Pointer ptr, Future Function(Pointer ptr) function) async {
|
||||
await function.call(ptr);
|
||||
allocator.free(ptr);
|
||||
}
|
||||
Reference in New Issue
Block a user