private void exportNodeRec(BabylonNode babylonNode, GLTF gltf, BabylonScene babylonScene, GLTFNode gltfParentNode = null) { GLTFNode gltfNode = ExportNode(babylonNode, gltf, babylonScene, gltfParentNode); if (gltfNode != null) { var type = babylonNode.GetType(); if (type == typeof(BabylonAbstractMesh) || type.IsSubclassOf(typeof(BabylonAbstractMesh))) { gltfNode = ExportAbstractMesh(ref gltfNode, babylonNode as BabylonAbstractMesh, gltf, gltfParentNode, babylonScene); } else if (type == typeof(BabylonCamera)) { GLTFCamera gltfCamera = ExportCamera(ref gltfNode, babylonNode as BabylonCamera, gltf, gltfParentNode); } else { RaiseError($"Node named {babylonNode.name} as no exporter", 1); } CheckCancelled(); // ...export its children List <BabylonNode> babylonDescendants = getDescendants(babylonNode); babylonDescendants.ForEach(descendant => exportNodeRec(descendant, gltf, babylonScene, gltfNode)); } }
public object ExportGLTFExtension <T1, T2>(T1 babylonObject, ref T2 gltfObject, ref GLTF gltf, GLTFExporter exporter, ExtensionInfo info) { var babylonCamera = babylonObject as BabylonCamera; var gltfAnimation = gltfObject as GLTFAnimation; AnimationExtensionInfo animationExtensionInfo = info as AnimationExtensionInfo; if (babylonCamera == null) { return(null); } Guid guid = Guid.Empty; Guid.TryParse(babylonCamera.id, out guid); IINode camNode = Tools.GetINodeByGuid(guid); var maxCamera = CameraUtilities.GetGenCameraFromNode(camNode, exporter.logger); if (maxCamera == null) { exporter.logger.RaiseError($"Impossible to export {FlightSimAsoboPropertyAnimationExtension.SerializedName} for {camNode.Name}, camera node must be a TargetCamera type"); return(null); } if (babylonCamera != null && gltfAnimation != null && FlightSimCameraUtilities.class_ID.Equals(new MaterialUtilities.ClassIDWrapper(maxCamera.ClassID))) { GLTFNode gltfNode = null; GLTFCamera gltfCamera = null; if (!exporter.nodeToGltfNodeMap.TryGetValue(babylonCamera, out gltfNode)) { return(gltfObject); } if (gltfNode != null && gltfNode.camera >= 0) { gltfCamera = gltf.CamerasList[gltfNode.camera.GetValueOrDefault()]; } int samplerIndex = AddParameterSamplerAnimation(babylonCamera, gltfAnimation, exporter, gltf, animationExtensionInfo.startFrame, animationExtensionInfo.endFrame); if (samplerIndex > -1) { GLTFExtensionAsoboPropertyAnimation gltfExtension = new GLTFExtensionAsoboPropertyAnimation(); List <GLTFAnimationPropertyChannel> animationPropertyChannels = new List <GLTFAnimationPropertyChannel>(); //TODO: for each animated property in the camera //TODO: this should be generated based on the property itself GLTFAnimationPropertyChannel propertyChannel = new GLTFAnimationPropertyChannel(); propertyChannel.sampler = samplerIndex; propertyChannel.target = $"cameras/{gltfCamera.index}/perspective/yfov"; animationPropertyChannels.Add(propertyChannel); gltfExtension.channels = animationPropertyChannels.ToArray(); return(gltfExtension); } } return(null); }
private void exportNodeRec(BabylonNode babylonNode, GLTF gltf, BabylonScene babylonScene, GLTFNode gltfParentNode = null) { var type = babylonNode.GetType(); // Ambiant light are attached to the scene if (type == typeof(BabylonLight) && ((BabylonLight)babylonNode).type == 3) { RaiseMessage($"GLTFExporter.Light | Export light named: {babylonNode.name}", 1); // new light in the scene extensions GLTFLight light = new GLTFLight { light = AddLightExtension(ref gltf, babylonNode as BabylonLight) }; int sceneIndex = (int)gltf.scene; if (gltf.scenes[sceneIndex].extensions.ContainsKey(KHR_lights)) { RaiseWarning($"Only 1 ambient light can be referenced per scene. {babylonNode.name} has overwritten the previous one.", 2); } gltf.scenes[sceneIndex].extensions[KHR_lights] = light; return; } GLTFNode gltfNode = ExportNode(babylonNode, gltf, babylonScene, gltfParentNode); if (gltfNode != null) { if (type == typeof(BabylonAbstractMesh) || type.IsSubclassOf(typeof(BabylonAbstractMesh))) { gltfNode = ExportAbstractMesh(ref gltfNode, babylonNode as BabylonAbstractMesh, gltf, gltfParentNode, babylonScene); } else if (type == typeof(BabylonCamera)) { GLTFCamera gltfCamera = ExportCamera(ref gltfNode, babylonNode as BabylonCamera, gltf, gltfParentNode); } else if (type == typeof(BabylonLight) || type.IsSubclassOf(typeof(BabylonLight))) { ExportLight(ref gltfNode, babylonNode as BabylonLight, gltf, gltfParentNode, babylonScene); } else { RaiseError($"Node named {babylonNode.name} as no exporter", 1); } CheckCancelled(); // ...export its children List <BabylonNode> babylonDescendants = getDescendants(babylonNode); babylonDescendants.ForEach(descendant => exportNodeRec(descendant, gltf, babylonScene, gltfNode)); } }
private void exportNodeRec(BabylonNode babylonNode, GLTF gltf, BabylonScene babylonScene, GLTFNode gltfParentNode = null) { var type = babylonNode.GetType(); GLTFNode gltfNode = ExportNode(babylonNode, gltf, babylonScene, gltfParentNode); if (gltfNode != null) { if (type == typeof(BabylonAbstractMesh) || type.IsSubclassOf(typeof(BabylonAbstractMesh))) { gltfNode = ExportAbstractMesh(ref gltfNode, babylonNode as BabylonAbstractMesh, gltf, gltfParentNode, babylonScene); } else if (type == typeof(BabylonCamera)) { GLTFCamera gltfCamera = ExportCamera(ref gltfNode, babylonNode as BabylonCamera, gltf, gltfParentNode); } else if (type == typeof(BabylonLight) || type.IsSubclassOf(typeof(BabylonLight))) { if (((BabylonLight)babylonNode).type != 3) { ExportLight(ref gltfNode, babylonNode as BabylonLight, gltf, gltfParentNode, babylonScene); } } else { RaiseError($"Node named {babylonNode.name} as no exporter", 1); } CheckCancelled(); // export its tag if (babylonNode.tags != null && babylonNode.tags != "") { if (gltfNode.extras == null) { gltfNode.extras = new Dictionary <string, object>(); } gltfNode.extras["tags"] = babylonNode.tags; } // ...export its children List <BabylonNode> babylonDescendants = getDescendants(babylonNode); babylonDescendants.ForEach(descendant => exportNodeRec(descendant, gltf, babylonScene, gltfNode)); } }
private void exportNodeRec(BabylonNode babylonNode, GLTF gltf, BabylonScene babylonScene, GLTFNode gltfParentNode = null) { GLTFNode gltfNode = null; var type = babylonNode.GetType(); if (type == typeof(BabylonAbstractMesh) || type.IsSubclassOf(typeof(BabylonAbstractMesh))) { gltfNode = ExportAbstractMesh(babylonNode as BabylonAbstractMesh, gltf, gltfParentNode, babylonScene); } else if (type == typeof(BabylonCamera)) { GLTFCamera gltfCamera = ExportCamera(babylonNode as BabylonCamera, gltf, gltfParentNode); gltfNode = gltfCamera.gltfNode; } else if (type == typeof(BabylonLight)) { if (isNodeRelevantToExport(babylonNode)) { // Export light nodes as empty nodes (no lights in glTF 2.0 core) RaiseWarning($"GLTFExporter | Light named {babylonNode.name} has children but lights are not exported with glTF 2.0 core version. An empty node is used instead.", 1); gltfNode = ExportLight(babylonNode as BabylonLight, gltf, gltfParentNode); } else { RaiseMessage($"GLTFExporter | Light named {babylonNode.name} is not relevant to export", 1); } } else { RaiseError($"Node named {babylonNode.name} as no exporter", 1); } CheckCancelled(); // If node is exported successfully... if (gltfNode != null) { // ...export its children List <BabylonNode> babylonDescendants = getDescendants(babylonNode); babylonDescendants.ForEach(descendant => exportNodeRec(descendant, gltf, babylonScene, gltfNode)); } }
private void exportNodeTypeRec(BabylonNode babylonNode, GLTF gltf, BabylonScene babylonScene, GLTFNode gltfParentNode = null) { var type = babylonNode.GetType(); logger.RaiseMessage($"GLTFExporter | ExportNode {babylonNode.name} of Type {type.ToString()}", 1); var nodeNodePair = nodeToGltfNodeMap.FirstOrDefault(pair => pair.Key.id.Equals(babylonNode.id)); GLTFNode gltfNode = nodeNodePair.Value; if (gltfNode != null) { if (type == typeof(BabylonAbstractMesh) || type.IsSubclassOf(typeof(BabylonAbstractMesh))) { gltfNode = ExportAbstractMesh(ref gltfNode, babylonNode as BabylonAbstractMesh, gltf, gltfParentNode, babylonScene); } else if (type == typeof(BabylonCamera)) { GLTFCamera gltfCamera = ExportCamera(ref gltfNode, babylonNode as BabylonCamera, gltf, gltfParentNode); } else if (type == typeof(BabylonLight) || type.IsSubclassOf(typeof(BabylonLight))) { if (((BabylonLight)babylonNode).type != 3) { ExportLight(ref gltfNode, babylonNode as BabylonLight, gltf, gltfParentNode, babylonScene); } } else if (type == typeof(BabylonNode)) { logger.RaiseVerbose($"Node named {babylonNode.name} already exported as gltfNode", 1); } else { logger.RaiseError($"Node named {babylonNode.name} has no exporter", 1); } logger.CheckCancelled(); // ...export its children List <BabylonNode> babylonDescendants = getDescendants(babylonNode); babylonDescendants.ForEach(descendant => exportNodeTypeRec(descendant, gltf, babylonScene, gltfNode)); } }
private GLTFCamera ExportCamera(ref GLTFNode gltfNode, BabylonCamera babylonCamera, GLTF gltf, GLTFNode gltfParentNode) { logger.RaiseMessage("GLTFExporter.Camera | Export camera named: " + babylonCamera.name, 2); // --- prints --- #region prints logger.RaiseVerbose("GLTFExporter.Camera | babylonCamera data", 3); logger.RaiseVerbose("GLTFExporter.Camera | babylonCamera.type=" + babylonCamera.type, 4); logger.RaiseVerbose("GLTFExporter.Camera | babylonCamera.fov=" + babylonCamera.fov, 4); logger.RaiseVerbose("GLTFExporter.Camera | babylonCamera.maxZ=" + babylonCamera.maxZ, 4); logger.RaiseVerbose("GLTFExporter.Camera | babylonCamera.minZ=" + babylonCamera.minZ, 4); #endregion // -------------------------- // ------- gltfCamera ------- // -------------------------- logger.RaiseMessage("GLTFExporter.Camera | create gltfCamera", 3); // Camera var gltfCamera = new GLTFCamera { name = babylonCamera.name }; gltfCamera.index = gltf.CamerasList.Count; gltf.CamerasList.Add(gltfCamera); gltfNode.camera = gltfCamera.index; gltfCamera.gltfNode = gltfNode; // Camera type switch (babylonCamera.mode) { case (BabylonCamera.CameraMode.ORTHOGRAPHIC_CAMERA): var gltfCameraOrthographic = new GLTFCameraOrthographic(); gltfCameraOrthographic.xmag = 1; // Do not bother about it - still mandatory gltfCameraOrthographic.ymag = 1; // Do not bother about it - still mandatory gltfCameraOrthographic.zfar = babylonCamera.maxZ; gltfCameraOrthographic.znear = babylonCamera.minZ; gltfCamera.type = GLTFCamera.CameraType.orthographic.ToString(); gltfCamera.orthographic = gltfCameraOrthographic; break; case (BabylonCamera.CameraMode.PERSPECTIVE_CAMERA): var gltfCameraPerspective = new GLTFCameraPerspective(); gltfCameraPerspective.aspectRatio = null; // Do not bother about it - use default glTF value gltfCameraPerspective.yfov = babylonCamera.fov; // Babylon camera fov mode is assumed to be vertical (FOVMODE_VERTICAL_FIXED) gltfCameraPerspective.zfar = babylonCamera.maxZ; gltfCameraPerspective.znear = babylonCamera.minZ; gltfCamera.type = GLTFCamera.CameraType.perspective.ToString(); gltfCamera.perspective = gltfCameraPerspective; break; default: logger.RaiseError("GLTFExporter.Camera | camera mode not found"); break; } ExportGLTFExtension(babylonCamera, ref gltfCamera, gltf); return(gltfCamera); }
private GLTFCamera ExportCamera(BabylonCamera babylonCamera, GLTF gltf, GLTFNode gltfParentNode) { RaiseMessage("GLTFExporter.Camera | Export camera named: " + babylonCamera.name, 1); // -------------------------- // ---------- Node ---------- // -------------------------- RaiseMessage("GLTFExporter.Camera | Node", 2); // Node var gltfNode = new GLTFNode(); gltfNode.name = GetUniqueNodeName(babylonCamera.name); gltfNode.index = gltf.NodesList.Count; gltf.NodesList.Add(gltfNode); // Hierarchy if (gltfParentNode != null) { RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as child to " + gltfParentNode.name, 3); gltfParentNode.ChildrenList.Add(gltfNode.index); gltfNode.parent = gltfParentNode; } else { // It's a root node // Only root nodes are listed in a gltf scene RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as root node to scene", 3); gltf.scenes[0].NodesList.Add(gltfNode.index); } // Transform gltfNode.translation = babylonCamera.position; if (babylonCamera.rotationQuaternion != null) { gltfNode.rotation = babylonCamera.rotationQuaternion; } else { // Convert rotation vector to quaternion BabylonVector3 rotationVector3 = new BabylonVector3 { X = babylonCamera.rotation[0], Y = babylonCamera.rotation[1], Z = babylonCamera.rotation[2] }; gltfNode.rotation = rotationVector3.toQuaternion().ToArray(); } // No scaling defined for babylon camera. Use identity instead. gltfNode.scale = new float[3] { 1, 1, 1 }; // Switch coordinate system at object level gltfNode.translation[2] *= -1; gltfNode.rotation[0] *= -1; gltfNode.rotation[1] *= -1; // Animations ExportNodeAnimation(babylonCamera, gltf, gltfNode); // --- prints --- #region prints RaiseVerbose("GLTFExporter.Camera | babylonCamera data", 2); RaiseVerbose("GLTFExporter.Camera | babylonCamera.type=" + babylonCamera.type, 3); RaiseVerbose("GLTFExporter.Camera | babylonCamera.fov=" + babylonCamera.fov, 3); RaiseVerbose("GLTFExporter.Camera | babylonCamera.maxZ=" + babylonCamera.maxZ, 3); RaiseVerbose("GLTFExporter.Camera | babylonCamera.minZ=" + babylonCamera.minZ, 3); #endregion // -------------------------- // ------- gltfCamera ------- // -------------------------- RaiseMessage("GLTFExporter.Camera | create gltfCamera", 2); // Camera var gltfCamera = new GLTFCamera { name = babylonCamera.name }; gltfCamera.index = gltf.CamerasList.Count; gltf.CamerasList.Add(gltfCamera); gltfNode.camera = gltfCamera.index; gltfCamera.gltfNode = gltfNode; // Camera type switch (babylonCamera.mode) { case (BabylonCamera.CameraMode.ORTHOGRAPHIC_CAMERA): var gltfCameraOrthographic = new GLTFCameraOrthographic(); gltfCameraOrthographic.xmag = 1; // Do not bother about it - still mandatory gltfCameraOrthographic.ymag = 1; // Do not bother about it - still mandatory gltfCameraOrthographic.zfar = babylonCamera.maxZ; gltfCameraOrthographic.znear = babylonCamera.minZ; gltfCamera.type = GLTFCamera.CameraType.orthographic.ToString(); gltfCamera.orthographic = gltfCameraOrthographic; break; case (BabylonCamera.CameraMode.PERSPECTIVE_CAMERA): var gltfCameraPerspective = new GLTFCameraPerspective(); gltfCameraPerspective.aspectRatio = null; // Do not bother about it - use default glTF value gltfCameraPerspective.yfov = babylonCamera.fov; // Babylon camera fov mode is assumed to be vertical (FOVMODE_VERTICAL_FIXED) gltfCameraPerspective.zfar = babylonCamera.maxZ; gltfCameraPerspective.znear = babylonCamera.minZ; gltfCamera.type = GLTFCamera.CameraType.perspective.ToString(); gltfCamera.perspective = gltfCameraPerspective; break; default: RaiseError("GLTFExporter.Camera | camera mode not found"); break; } return(gltfCamera); }
// TODO - Test if ok with a gltf viewer working with custom camera (babylon loader/sandbox doesn't load them) private GLTFCamera ExportCamera(BabylonCamera babylonCamera, GLTF gltf, GLTFNode gltfParentNode) { RaiseMessage("GLTFExporter.Camera | Export camera named: " + babylonCamera.name, 1); // -------------------------- // ---------- Node ---------- // -------------------------- RaiseMessage("GLTFExporter.Camera | Node", 2); // Node var gltfNode = new GLTFNode(); gltfNode.name = babylonCamera.name; gltfNode.index = gltf.NodesList.Count; gltf.NodesList.Add(gltfNode); // Hierarchy if (gltfParentNode != null) { RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as child to " + gltfParentNode.name, 3); gltfParentNode.ChildrenList.Add(gltfNode.index); } else { // It's a root node // Only root nodes are listed in a gltf scene RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as root node to scene", 3); gltf.scenes[0].NodesList.Add(gltfNode.index); } // Transform gltfNode.translation = babylonCamera.position; // Switch from left to right handed coordinate system //gltfNode.translation[0] *= -1; if (babylonCamera.rotationQuaternion != null) { gltfNode.rotation = babylonCamera.rotationQuaternion; } else { // Convert rotation vector to quaternion BabylonVector3 rotationVector3 = new BabylonVector3 { X = babylonCamera.rotation[0], Y = babylonCamera.rotation[1], Z = babylonCamera.rotation[2] }; gltfNode.rotation = rotationVector3.toQuaternionGltf().ToArray(); } // No scaling defined for babylon camera. Use identity instead. gltfNode.scale = new float[3] { 1, 1, 1 }; // --- prints --- RaiseMessage("GLTFExporter.Camera | babylonCamera data", 2); RaiseMessage("GLTFExporter.Camera | babylonCamera.type=" + babylonCamera.type, 3); RaiseMessage("GLTFExporter.Camera | babylonCamera.fov=" + babylonCamera.fov, 3); RaiseMessage("GLTFExporter.Camera | babylonCamera.maxZ=" + babylonCamera.maxZ, 3); RaiseMessage("GLTFExporter.Camera | babylonCamera.minZ=" + babylonCamera.minZ, 3); // -------------------------- // ------- gltfCamera ------- // -------------------------- RaiseMessage("GLTFExporter.Camera | create gltfCamera", 2); // Camera var gltfCamera = new GLTFCamera { name = babylonCamera.name }; gltfCamera.index = gltf.CamerasList.Count; gltf.CamerasList.Add(gltfCamera); gltfNode.camera = gltfCamera.index; gltfCamera.gltfNode = gltfNode; // Camera type switch (babylonCamera.mode) { case (BabylonCamera.CameraMode.ORTHOGRAPHIC_CAMERA): var gltfCameraOrthographic = new GLTFCameraOrthographic(); gltfCameraOrthographic.xmag = 1; // TODO - How to retreive value from babylon? xmag:The floating-point horizontal magnification of the view gltfCameraOrthographic.ymag = 1; // TODO - How to retreive value from babylon? ymag:The floating-point vertical magnification of the view gltfCameraOrthographic.zfar = babylonCamera.maxZ; gltfCameraOrthographic.znear = babylonCamera.minZ; gltfCamera.type = GLTFCamera.CameraType.orthographic.ToString(); gltfCamera.orthographic = gltfCameraOrthographic; break; case (BabylonCamera.CameraMode.PERSPECTIVE_CAMERA): var gltfCameraPerspective = new GLTFCameraPerspective(); gltfCameraPerspective.aspectRatio = null; // 0.8f; // TODO - How to retreive value from babylon? The aspect ratio in babylon is computed based on the engine rather than set on a camera (aspectRatio = _gl.drawingBufferWidth / _gl.drawingBufferHeight) gltfCameraPerspective.yfov = babylonCamera.fov; // WARNING - Babylon camera fov mode is assumed to be vertical (FOVMODE_VERTICAL_FIXED) gltfCameraPerspective.zfar = babylonCamera.maxZ; gltfCameraPerspective.znear = babylonCamera.minZ; gltfCamera.type = GLTFCamera.CameraType.perspective.ToString(); gltfCamera.perspective = gltfCameraPerspective; break; default: RaiseError("GLTFExporter.Camera | camera mode not found"); break; } return(gltfCamera); }