Exemple #1
0
            /// <summary>
            /// Configure FbxCameras from GameCamera
            /// </summary>
            private static void ConfigureGameCamera(FbxCamera fbxCamera, Camera unityCamera)
            {
                // Configure FilmBack settings as a 35mm TV Projection (0.816 x 0.612)
                float aspectRatio = unityCamera.aspect;

                float apertureHeightInInches = 0.612f;
                float apertureWidthInInches  = aspectRatio * apertureHeightInInches;

                FbxCamera.EProjectionType projectionType =
                    unityCamera.orthographic ? FbxCamera.EProjectionType.eOrthogonal : FbxCamera.EProjectionType.ePerspective;

                fbxCamera.ProjectionType.Set(projectionType);
                fbxCamera.FilmAspectRatio.Set(aspectRatio);
                fbxCamera.SetApertureWidth(apertureWidthInInches);
                fbxCamera.SetApertureHeight(apertureHeightInInches);
                fbxCamera.SetApertureMode(FbxCamera.EApertureMode.eVertical);

                // Focal Length
                double focalLength = fbxCamera.ComputeFocalLength(unityCamera.fieldOfView);

                fbxCamera.FocalLength.Set(focalLength);

                // Field of View
                fbxCamera.FieldOfView.Set(unityCamera.fieldOfView);

                // NearPlane
                fbxCamera.SetNearPlane(unityCamera.nearClipPlane.Meters().ToCentimeters());

                // FarPlane
                fbxCamera.SetFarPlane(unityCamera.farClipPlane.Meters().ToCentimeters());

                return;
            }
Exemple #2
0
            /// <summary>
            /// Configure FbxCameras from a Physical Camera
            /// </summary>
            private static void ConfigurePhysicalCamera(FbxCamera fbxCamera, Camera unityCamera)
            {
                Debug.Assert(unityCamera.usePhysicalProperties);

                // Configure FilmBack settings
                float apertureHeightInInches = unityCamera.sensorSize.y.Millimeters().ToInches();
                float apertureWidthInInches  = unityCamera.sensorSize.x.Millimeters().ToInches();
                float aspectRatio            = apertureWidthInInches / apertureHeightInInches;

                FbxCamera.EProjectionType projectionType = unityCamera.orthographic
                    ? FbxCamera.EProjectionType.eOrthogonal
                    : FbxCamera.EProjectionType.ePerspective;

                // NOTE: it is possible to match some of the sensor sizes to the
                // predefined EApertureFormats : e16mmTheatrical, eSuper16mm,
                // e35mmFullAperture, eIMAX. However the round in the sizes is not
                // consistent between Unity and FBX so we choose
                // to leave the values as a eCustomAperture setting.

                fbxCamera.ProjectionType.Set(projectionType);
                fbxCamera.FilmAspectRatio.Set(aspectRatio);

                Vector2 gameViewSize = GetSizeOfMainGameView();

                fbxCamera.SetAspect(FbxCamera.EAspectRatioMode.eFixedRatio, gameViewSize.x / gameViewSize.y, 1.0);
                fbxCamera.SetApertureWidth(apertureWidthInInches);
                fbxCamera.SetApertureHeight(apertureHeightInInches);

                // Fit the resolution gate horizontally within the film gate.
                fbxCamera.GateFit.Set(s_mapGateFit[unityCamera.gateFit]);

                // Lens Shift ( Film Offset ) as a percentage 0..1
                // FBX FilmOffset is in inches
                fbxCamera.FilmOffsetX.Set(apertureWidthInInches * Mathf.Clamp(Mathf.Abs(unityCamera.lensShift.x), 0f, 1f) * Mathf.Sign(unityCamera.lensShift.x));
                fbxCamera.FilmOffsetY.Set(apertureHeightInInches * Mathf.Clamp(Mathf.Abs(unityCamera.lensShift.y), 0f, 1f) * Mathf.Sign(unityCamera.lensShift.y));

                // Focal Length
                fbxCamera.SetApertureMode(FbxCamera.EApertureMode.eFocalLength);

                double focalLength = (double)unityCamera.focalLength;

                fbxCamera.FocalLength.Set(focalLength); /* in millimeters */

                // NearPlane
                fbxCamera.SetNearPlane((double)unityCamera.nearClipPlane.Meters().ToCentimeters());

                // FarPlane
                fbxCamera.SetFarPlane((float)unityCamera.farClipPlane.Meters().ToCentimeters());
                return;
            }
            /// <summary>
            /// Exports camera component
            /// </summary>
            protected FbxCamera ExportCamera(Camera unityCamera, FbxScene fbxScene, FbxNode fbxNode)
            {
                FbxCamera fbxCamera = FbxCamera.Create(fbxScene.GetFbxManager(), unityCamera.name);

                bool  perspective = unityCamera.orthographic != true;
                float aspectRatio = unityCamera.aspect;

                // Configure FilmBack settings: 35mm TV Projection (0.816 x 0.612)
                float apertureHeightInInches = 0.612f;
                float apertureWidthInInches  = aspectRatio * apertureHeightInInches;

                FbxCamera.EProjectionType projectionType =
                    perspective ? FbxCamera.EProjectionType.ePerspective : FbxCamera.EProjectionType.eOrthogonal;

                fbxCamera.ProjectionType.Set(projectionType);
                fbxCamera.SetAspect(FbxCamera.EAspectRatioMode.eFixedRatio, aspectRatio, 1.0f);
                fbxCamera.FilmAspectRatio.Set(aspectRatio);
                fbxCamera.SetApertureWidth(apertureWidthInInches);
                fbxCamera.SetApertureHeight(apertureHeightInInches);
                fbxCamera.SetApertureMode(FbxCamera.EApertureMode.eFocalLength);

                // FOV / Focal Length
                fbxCamera.FocalLength.Set(fbxCamera.ComputeFocalLength(unityCamera.fieldOfView));

                // NearPlane
                fbxCamera.SetNearPlane(unityCamera.nearClipPlane);

                // FarPlane
                fbxCamera.SetFarPlane(unityCamera.farClipPlane);

                // Export backgroundColor as a custom property
                // NOTE: export on fbxNode so that it will show up in Maya
                ExportColorProperty(fbxNode, unityCamera.backgroundColor,
                                    MakeName("backgroundColor"),
                                    "The color with which the screen will be cleared.");

                // Export clearFlags as a custom property
                // NOTE: export on fbxNode so that it will show up in Maya
                ExportIntProperty(fbxNode, (int)unityCamera.clearFlags,
                                  MakeName("clearFlags"),
                                  "How the camera clears the background.");


                return(fbxCamera);
            }
        protected override FbxScene CreateScene(FbxManager manager)
        {
            FbxScene  scene      = base.CreateScene(manager);
            FbxNode   cameraNode = scene.GetRootNode().GetChild(0);
            FbxCamera camera     = FbxCamera.Create(scene, "camera");

            camera.ProjectionType.Set(FbxCamera.EProjectionType.ePerspective);
            camera.SetAspect(FbxCamera.EAspectRatioMode.eFixedRatio, 300, 400);
            camera.FilmAspectRatio.Set(240);
            camera.SetApertureWidth(4);
            camera.SetApertureHeight(2);
            camera.SetApertureMode(FbxCamera.EApertureMode.eFocalLength);
            camera.FocalLength.Set(32);
            camera.SetNearPlane(1);
            camera.SetFarPlane(100);

            // create custom property (background color)
            var bgColorProperty = FbxProperty.Create(cameraNode, Globals.FbxColor4DT, "backgroundColor");

            Assert.IsTrue(bgColorProperty.IsValid());

            bgColorProperty.Set(new FbxColor(0.5, 0.4, 0.1, 1));

            // Must be marked user-defined or it won't be shown in most DCCs
            bgColorProperty.ModifyFlag(FbxPropertyFlags.EFlags.eUserDefined, true);
            bgColorProperty.ModifyFlag(FbxPropertyFlags.EFlags.eAnimatable, true);

            Assert.IsTrue(bgColorProperty.GetFlag(FbxPropertyFlags.EFlags.eUserDefined));
            Assert.IsTrue(bgColorProperty.GetFlag(FbxPropertyFlags.EFlags.eAnimatable));

            // create custom property (clear flags)
            var clearFlagsProperty = FbxProperty.Create(cameraNode, Globals.FbxIntDT, "clearFlags");

            Assert.IsTrue(clearFlagsProperty.IsValid());

            clearFlagsProperty.Set(4);

            // Must be marked user-defined or it won't be shown in most DCCs
            clearFlagsProperty.ModifyFlag(FbxPropertyFlags.EFlags.eUserDefined, true);
            clearFlagsProperty.ModifyFlag(FbxPropertyFlags.EFlags.eAnimatable, true);

            Assert.IsTrue(clearFlagsProperty.GetFlag(FbxPropertyFlags.EFlags.eUserDefined));
            Assert.IsTrue(clearFlagsProperty.GetFlag(FbxPropertyFlags.EFlags.eAnimatable));

            // Add camera properties to animation clip
            FbxAnimStack animStack = scene.GetCurrentAnimationStack();
            FbxAnimLayer animLayer = animStack.GetAnimLayerMember();

            // TODO: (UNI-19438) Figure out why trying to do GetCurve for NearPlane always returns null
            CreateAnimCurves(cameraNode, animLayer, new List <PropertyComponentPair> ()
            {
                new PropertyComponentPair("backgroundColor", new string[] {
                    Globals.FBXSDK_CURVENODE_COLOR_RED,
                    Globals.FBXSDK_CURVENODE_COLOR_GREEN,
                    Globals.FBXSDK_CURVENODE_COLOR_BLUE, "W"
                }),
                new PropertyComponentPair("FocalLength", new string[] { null }),
                new PropertyComponentPair("clearFlags", new string[] { null })
            }, (index) => { return(index); }, (index) => { return(index / 5.0f); }, camera);

            cameraNode.SetNodeAttribute(camera);

            // set the default camera
            scene.GetGlobalSettings().SetDefaultCamera(cameraNode.GetName());

            return(scene);
        }