コード例 #1
0
        /// <summary>
        /// Create VGO_PS_ShapeModule from ShapeModule.
        /// </summary>
        /// <param name="module"></param>
        /// <param name="gltf"></param>
        /// <returns></returns>
        /// <remarks>
        /// @notice TextureIO.ExportTexture() may export the same texture multiple times.
        /// </remarks>
        protected virtual VGO_PS_ShapeModule CreateVgoModule(ShapeModule module, glTF gltf)
        {
            var vgoShapeModule = new VGO_PS_ShapeModule()
            {
                enabled               = module.enabled,
                shapeType             = module.shapeType,
                angle                 = module.angle,
                radius                = module.radius,
                donutRadius           = module.donutRadius,
                radiusMode            = module.radiusMode,
                radiusSpread          = module.radiusSpread,
                radiusSpeed           = VgoParticleSystemMinMaxCurveConverter.CreateFrom(module.radiusSpeed),
                radiusSpeedMultiplier = module.radiusSpeedMultiplier,
                radiusThickness       = module.radiusThickness,
                boxThickness          = module.boxThickness.ReverseZ().ToArray(),
                arc                      = module.arc,
                arcMode                  = module.arcMode,
                arcSpread                = module.arcSpread,
                arcSpeed                 = VgoParticleSystemMinMaxCurveConverter.CreateFrom(module.arcSpeed),
                arcSpeedMultiplier       = module.arcSpeedMultiplier,
                length                   = module.length,
                meshShapeType            = module.meshShapeType,
                meshSpawnMode            = module.meshSpawnMode,
                meshSpawnSpread          = module.meshSpawnSpread,
                meshSpawnSpeed           = VgoParticleSystemMinMaxCurveConverter.CreateFrom(module.meshSpawnSpread),
                meshSpawnSpeedMultiplier = module.meshSpawnSpeedMultiplier,
                //mesh
                //meshRenderer
                //skinnedMeshRenderer
                useMeshMaterialIndex = module.useMeshMaterialIndex,
                meshMaterialIndex    = module.meshMaterialIndex,
                useMeshColors        = module.useMeshColors,
                //sprite
                //spriteRenderer
                normalOffset                 = module.normalOffset,
                textureIndex                 = -1,
                textureClipChannel           = module.textureClipChannel,
                textureClipThreshold         = module.textureClipThreshold,
                textureColorAffectsParticles = module.textureColorAffectsParticles,
                textureAlphaAffectsParticles = module.textureAlphaAffectsParticles,
                textureBilinearFiltering     = module.textureBilinearFiltering,
                textureUVChannel             = module.textureUVChannel,
                position                 = module.position.ReverseZ().ToArray(),
                rotation                 = module.rotation.ReverseZ().ToArray(),
                scale                    = module.scale.ToArray(),
                alignToDirection         = module.alignToDirection,
                randomPositionAmount     = module.randomPositionAmount,
                sphericalDirectionAmount = module.sphericalDirectionAmount,
                randomDirectionAmount    = module.randomDirectionAmount,
            };

            if (module.texture != null)
            {
                vgoShapeModule.textureIndex = TextureIO.ExportTexture(gltf, gltf.buffers.Count - 1, module.texture, glTFTextureTypes.Unknown);
            }

            return(vgoShapeModule);
        }
コード例 #2
0
        public static void _Export(glTF gltf, VRMExporter exporter, GameObject go)
        {
            exporter.Prepare(go);
            exporter.Export();

            // avatar
            var animator = go.GetComponent <Animator>();

            if (animator != null)
            {
                var humanoid = go.GetComponent <VRMHumanoidDescription>();
                UniHumanoid.AvatarDescription description = null;
                var nodes = go.transform.Traverse().Skip(1).ToList();
                {
                    var isCreated = false;
                    if (humanoid != null)
                    {
                        description = humanoid.GetDescription(out isCreated);
                    }

                    if (description != null)
                    {
                        // use description
                        gltf.extensions.VRM.humanoid.Apply(description, nodes);
                    }
                    if (isCreated)
                    {
                        GameObject.DestroyImmediate(description);
                    }
                }

                {
                    // set humanoid bone mapping
                    var avatar = animator.avatar;
                    foreach (HumanBodyBones key in Enum.GetValues(typeof(HumanBodyBones)))
                    {
                        if (key == HumanBodyBones.LastBone)
                        {
                            break;
                        }

                        var transform = animator.GetBoneTransform(key);
                        if (transform != null)
                        {
                            gltf.extensions.VRM.humanoid.SetNodeIndex(key, nodes.IndexOf(transform));
                        }
                    }
                }
            }

            // morph
            var master = go.GetComponent <VRMBlendShapeProxy>();

            if (master != null)
            {
                var avatar = master.BlendShapeAvatar;
                if (avatar != null)
                {
                    var meshes = exporter.Meshes;
                    foreach (var x in avatar.Clips)
                    {
                        gltf.extensions.VRM.blendShapeMaster.Add(x, exporter.Copy.transform, meshes);
                    }
                }
            }

            // secondary
            VRMSpringUtility.ExportSecondary(exporter.Copy.transform, exporter.Nodes,
                                             x => gltf.extensions.VRM.secondaryAnimation.colliderGroups.Add(x),
                                             x => gltf.extensions.VRM.secondaryAnimation.boneGroups.Add(x)
                                             );

            // meta(obsolete)
            {
                var meta = exporter.Copy.GetComponent <VRMMetaInformation>();
                if (meta != null)
                {
                    gltf.extensions.VRM.meta.author             = meta.Author;
                    gltf.extensions.VRM.meta.contactInformation = meta.ContactInformation;
                    gltf.extensions.VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        gltf.extensions.VRM.meta.texture = TextureIO.ExportTexture(gltf, gltf.buffers.Count - 1, meta.Thumbnail, false);
                    }
                    gltf.extensions.VRM.meta.licenseType     = meta.LicenseType;
                    gltf.extensions.VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    gltf.extensions.VRM.meta.reference       = meta.Reference;
                }
            }
            // meta
            {
                var _meta = exporter.Copy.GetComponent <VRMMeta>();
                if (_meta != null && _meta.Meta != null)
                {
                    var meta = _meta.Meta;

                    // info
                    gltf.extensions.VRM.meta.version            = meta.Version;
                    gltf.extensions.VRM.meta.author             = meta.Author;
                    gltf.extensions.VRM.meta.contactInformation = meta.ContactInformation;
                    gltf.extensions.VRM.meta.reference          = meta.Reference;
                    gltf.extensions.VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        gltf.extensions.VRM.meta.texture = TextureIO.ExportTexture(gltf, gltf.buffers.Count - 1, meta.Thumbnail, false);
                    }

                    // ussage pemission
                    gltf.extensions.VRM.meta.allowedUser        = meta.AllowedUser;
                    gltf.extensions.VRM.meta.violentUssage      = meta.ViolentUssage;
                    gltf.extensions.VRM.meta.sexualUssage       = meta.SexualUssage;
                    gltf.extensions.VRM.meta.commercialUssage   = meta.CommercialUssage;
                    gltf.extensions.VRM.meta.otherPermissionUrl = meta.OtherPermissionUrl;

                    // distribution license
                    gltf.extensions.VRM.meta.licenseType = meta.LicenseType;
                    if (meta.LicenseType == LicenseType.Other)
                    {
                        gltf.extensions.VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    }
                }
            }

            // firstPerson
            var firstPerson = exporter.Copy.GetComponent <VRMFirstPerson>();

            if (firstPerson != null)
            {
                if (firstPerson.FirstPersonBone != null)
                {
                    gltf.extensions.VRM.firstPerson.firstPersonBone       = exporter.Nodes.IndexOf(firstPerson.FirstPersonBone);
                    gltf.extensions.VRM.firstPerson.firstPersonBoneOffset = firstPerson.FirstPersonOffset;
                    gltf.extensions.VRM.firstPerson.meshAnnotations       = firstPerson.Renderers.Select(x => new glTF_VRM_MeshAnnotation
                    {
                        mesh            = exporter.Meshes.IndexOf(x.SharedMesh),
                        firstPersonFlag = x.FirstPersonFlag.ToString(),
                    }).ToList();
                }

                // lookAt
                {
                    var lookAtHead = exporter.Copy.GetComponent <VRMLookAtHead>();
                    var lookAt     = exporter.Copy.GetComponent <VRMLookAt>();
                    if (lookAtHead != null)
                    {
                        var boneApplyer       = exporter.Copy.GetComponent <VRMLookAtBoneApplyer>();
                        var blendShapeApplyer = exporter.Copy.GetComponent <VRMLookAtBlendShapeApplyer>();
                        if (boneApplyer != null)
                        {
                            gltf.extensions.VRM.firstPerson.lookAtType = LookAtType.Bone;
                            gltf.extensions.VRM.firstPerson.lookAtHorizontalInner.Apply(boneApplyer.HorizontalInner);
                            gltf.extensions.VRM.firstPerson.lookAtHorizontalOuter.Apply(boneApplyer.HorizontalOuter);
                            gltf.extensions.VRM.firstPerson.lookAtVerticalDown.Apply(boneApplyer.VerticalDown);
                            gltf.extensions.VRM.firstPerson.lookAtVerticalUp.Apply(boneApplyer.VerticalUp);
                        }
                        else if (blendShapeApplyer != null)
                        {
                            gltf.extensions.VRM.firstPerson.lookAtType = LookAtType.BlendShape;
                            gltf.extensions.VRM.firstPerson.lookAtHorizontalOuter.Apply(blendShapeApplyer.Horizontal);
                            gltf.extensions.VRM.firstPerson.lookAtVerticalDown.Apply(blendShapeApplyer.VerticalDown);
                            gltf.extensions.VRM.firstPerson.lookAtVerticalUp.Apply(blendShapeApplyer.VerticalUp);
                        }
                    }
                    else if (lookAt != null)
                    {
                        gltf.extensions.VRM.firstPerson.lookAtHorizontalInner.Apply(lookAt.HorizontalInner);
                        gltf.extensions.VRM.firstPerson.lookAtHorizontalOuter.Apply(lookAt.HorizontalOuter);
                        gltf.extensions.VRM.firstPerson.lookAtVerticalDown.Apply(lookAt.VerticalDown);
                        gltf.extensions.VRM.firstPerson.lookAtVerticalUp.Apply(lookAt.VerticalUp);
                    }
                }
            }

            // materials
            foreach (var m in exporter.Materials)
            {
                gltf.extensions.VRM.materialProperties.Add(glTF_VRM_Material.CreateFromMaterial(m, exporter.Textures));
            }
        }
コード例 #3
0
ファイル: VRMExporter.cs プロジェクト: YukiSamavu/GAT185
        public override void Export(MeshExportSettings configuration)
        {
            base.Export(configuration);

            // avatar
            var animator = Copy.GetComponent <Animator>();

            if (animator != null)
            {
                var humanoid = Copy.GetComponent <VRMHumanoidDescription>();
                UniHumanoid.AvatarDescription description = null;
                var nodes = Copy.transform.Traverse().Skip(1).ToList();
                {
                    var isCreated = false;
                    if (humanoid != null)
                    {
                        description = humanoid.GetDescription(out isCreated);
                    }

                    if (description != null)
                    {
                        // use description
                        VRM.humanoid.Apply(description, nodes);
                    }

                    if (isCreated)
                    {
                        GameObject.DestroyImmediate(description);
                    }
                }

                {
                    // set humanoid bone mapping
                    var avatar = animator.avatar;
                    foreach (HumanBodyBones key in Enum.GetValues(typeof(HumanBodyBones)))
                    {
                        if (key == HumanBodyBones.LastBone)
                        {
                            break;
                        }

                        var transform = animator.GetBoneTransform(key);
                        if (transform != null)
                        {
                            VRM.humanoid.SetNodeIndex(key, nodes.IndexOf(transform));
                        }
                    }
                }
            }

            // morph
            var master = Copy.GetComponent <VRMBlendShapeProxy>();

            if (master != null)
            {
                var avatar = master.BlendShapeAvatar;
                if (avatar != null)
                {
                    foreach (var x in avatar.Clips)
                    {
                        VRM.blendShapeMaster.Add(x, this);
                    }
                }
            }

            // secondary
            VRMSpringUtility.ExportSecondary(Copy.transform, Nodes,
                                             x => VRM.secondaryAnimation.colliderGroups.Add(x),
                                             x => VRM.secondaryAnimation.boneGroups.Add(x)
                                             );

#pragma warning disable 0618
            // meta(obsolete)
            {
                var meta = Copy.GetComponent <VRMMetaInformation>();
                if (meta != null)
                {
                    VRM.meta.author             = meta.Author;
                    VRM.meta.contactInformation = meta.ContactInformation;
                    VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
                    }

                    VRM.meta.licenseType     = meta.LicenseType;
                    VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    VRM.meta.reference       = meta.Reference;
                }
            }
#pragma warning restore 0618

            // meta
            {
                var _meta = Copy.GetComponent <VRMMeta>();
                if (_meta != null && _meta.Meta != null)
                {
                    var meta = _meta.Meta;

                    // info
                    VRM.meta.version            = meta.Version;
                    VRM.meta.author             = meta.Author;
                    VRM.meta.contactInformation = meta.ContactInformation;
                    VRM.meta.reference          = meta.Reference;
                    VRM.meta.title = meta.Title;
                    if (meta.Thumbnail != null)
                    {
                        VRM.meta.texture = TextureIO.ExportTexture(glTF, glTF.buffers.Count - 1, meta.Thumbnail, glTFTextureTypes.Unknown);
                    }

                    // ussage permission
                    VRM.meta.allowedUser        = meta.AllowedUser;
                    VRM.meta.violentUssage      = meta.ViolentUssage;
                    VRM.meta.sexualUssage       = meta.SexualUssage;
                    VRM.meta.commercialUssage   = meta.CommercialUssage;
                    VRM.meta.otherPermissionUrl = meta.OtherPermissionUrl;

                    // distribution license
                    VRM.meta.licenseType = meta.LicenseType;
                    if (meta.LicenseType == LicenseType.Other)
                    {
                        VRM.meta.otherLicenseUrl = meta.OtherLicenseUrl;
                    }
                }
            }

            // firstPerson
            var firstPerson = Copy.GetComponent <VRMFirstPerson>();
            if (firstPerson != null)
            {
                if (firstPerson.FirstPersonBone != null)
                {
                    VRM.firstPerson.firstPersonBone       = Nodes.IndexOf(firstPerson.FirstPersonBone);
                    VRM.firstPerson.firstPersonBoneOffset = firstPerson.FirstPersonOffset;
                    VRM.firstPerson.meshAnnotations       = firstPerson.Renderers.Select(x => new glTF_VRM_MeshAnnotation
                    {
                        mesh            = Meshes.IndexOf(x.SharedMesh),
                        firstPersonFlag = x.FirstPersonFlag.ToString(),
                    }).ToList();
                }

                // lookAt
                {
                    var lookAtHead = Copy.GetComponent <VRMLookAtHead>();
                    if (lookAtHead != null)
                    {
                        var boneApplyer       = Copy.GetComponent <VRMLookAtBoneApplyer>();
                        var blendShapeApplyer = Copy.GetComponent <VRMLookAtBlendShapeApplyer>();
                        if (boneApplyer != null)
                        {
                            VRM.firstPerson.lookAtType = LookAtType.Bone;
                            VRM.firstPerson.lookAtHorizontalInner.Apply(boneApplyer.HorizontalInner);
                            VRM.firstPerson.lookAtHorizontalOuter.Apply(boneApplyer.HorizontalOuter);
                            VRM.firstPerson.lookAtVerticalDown.Apply(boneApplyer.VerticalDown);
                            VRM.firstPerson.lookAtVerticalUp.Apply(boneApplyer.VerticalUp);
                        }
                        else if (blendShapeApplyer != null)
                        {
                            VRM.firstPerson.lookAtType = LookAtType.BlendShape;
                            VRM.firstPerson.lookAtHorizontalOuter.Apply(blendShapeApplyer.Horizontal);
                            VRM.firstPerson.lookAtVerticalDown.Apply(blendShapeApplyer.VerticalDown);
                            VRM.firstPerson.lookAtVerticalUp.Apply(blendShapeApplyer.VerticalUp);
                        }
                    }
                }
            }

            // materials
            foreach (var m in Materials)
            {
                VRM.materialProperties.Add(VRMMaterialExporter.CreateFromMaterial(m, TextureManager.Textures));
            }

            // Serialize VRM
            var f = new JsonFormatter();
            VRMSerializer.Serialize(f, VRM);
            var bytes = f.GetStoreBytes();
            glTFExtensionExport.GetOrCreate(ref glTF.extensions).Add("VRM", bytes);
        }
コード例 #4
0
        public override void Export()
        {
            base.Export();

            var gltf     = GLTF;
            var exporter = this;
            var go       = Copy;

            //exporter.Prepare(go);
            //exporter.Export();

            gltf.extensions.VCAST_vci_material_unity = new glTF_VCAST_vci_material_unity
            {
                materials = exporter.Materials
                            .Select(m => glTF_VCI_Material.CreateFromMaterial(m, exporter.TextureManager.Textures))
                            .ToList()
            };

            if (Copy == null)
            {
                return;
            }

            // vci interaction
            var vciObject = Copy.GetComponent <VCIObject>();

            if (vciObject != null)
            {
                // script
                if (vciObject.Scripts.Any())
                {
                    gltf.extensions.VCAST_vci_embedded_script = new glTF_VCAST_vci_embedded_script
                    {
                        scripts = vciObject.Scripts.Select(x =>
                        {
                            int viewIndex = -1;
#if UNITY_EDITOR
                            if (!string.IsNullOrEmpty(x.filePath))
                            {
                                if (File.Exists(x.filePath))
                                {
                                    using (var resader = new StreamReader(x.filePath))
                                    {
                                        string scriptStr = resader.ReadToEnd();
                                        viewIndex        = gltf.ExtendBufferAndGetViewIndex <byte>(0, Utf8String.Encoding.GetBytes(scriptStr));
                                    }
                                }
                                else
                                {
                                    Debug.LogError("script file not found. スクリプトファイルが見つかりませんでした: " + x.filePath);
                                    throw new FileNotFoundException(x.filePath);
                                }
                            }
                            else
#endif
                            {
                                viewIndex = gltf.ExtendBufferAndGetViewIndex <byte>(0, Utf8String.Encoding.GetBytes(x.source));
                            }

                            return(new glTF_VCAST_vci_embedded_script_source
                            {
                                name = x.name,
                                mimeType = x.mimeType,
                                targetEngine = x.targetEngine,
                                source = viewIndex,
                            });
                        })
                                  .ToList()
                    }
                }
                ;

                var springBones = Copy.GetComponents <VCISpringBone>();
                if (springBones.Length > 0)
                {
                    var sbg = new glTF_VCAST_vci_spring_bone();
                    sbg.springBones = new List <glTF_VCAST_vci_SpringBone>();
                    gltf.extensions.VCAST_vci_spring_bone = sbg;
                    foreach (var sb in springBones)
                    {
                        sbg.springBones.Add(new glTF_VCAST_vci_SpringBone()
                        {
                            center       = Nodes.IndexOf(sb.m_center),
                            dragForce    = sb.m_dragForce,
                            gravityDir   = sb.m_gravityDir,
                            gravityPower = sb.m_gravityPower,
                            stiffiness   = sb.m_stiffnessForce,
                            hitRadius    = sb.m_hitRadius,
                            colliderIds  = sb.m_colliderObjects
                                           .Where(x => x != null)
                                           .Select(x => Nodes.IndexOf(x))
                                           .ToArray(),
                            bones = sb.RootBones.Where(x => x != null).Select(x => Nodes.IndexOf(x.transform)).ToArray()
                        });
                    }
                }

                // meta
                var meta = vciObject.Meta;
                gltf.extensions.VCAST_vci_meta = new glTF_VCAST_vci_meta
                {
                    exporterVCIVersion = VCIVersion.VCI_VERSION,
                    specVersion        = VCISpecVersion.Version,

                    title = meta.title,

                    version            = meta.version,
                    author             = meta.author,
                    contactInformation = meta.contactInformation,
                    reference          = meta.reference,
                    description        = meta.description,

                    modelDataLicenseType     = meta.modelDataLicenseType,
                    modelDataOtherLicenseUrl = meta.modelDataOtherLicenseUrl,
                    scriptLicenseType        = meta.scriptLicenseType,
                    scriptOtherLicenseUrl    = meta.scriptOtherLicenseUrl,

                    scriptWriteProtected  = meta.scriptWriteProtected,
                    scriptEnableDebugging = meta.scriptEnableDebugging,
                    scriptFormat          = meta.scriptFormat
                };
                if (meta.thumbnail != null)
                {
                    gltf.extensions.VCAST_vci_meta.thumbnail = TextureIO.ExportTexture(
                        gltf, gltf.buffers.Count - 1, meta.thumbnail, glTFTextureTypes.Unknown);
                }
            }

            // collider & rigidbody & joint & item
            for (var i = 0; i < exporter.Nodes.Count; i++)
            {
                var node     = exporter.Nodes[i];
                var gltfNode = gltf.nodes[i];

                // 各ノードに複数のコライダーがあり得る
                var colliders = node.GetComponents <Collider>();
                if (colliders.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_collider           = new glTF_VCAST_vci_colliders();
                    gltfNode.extensions.VCAST_vci_collider.colliders = new List <glTF_VCAST_vci_Collider>();

                    foreach (var collider in colliders)
                    {
                        var gltfCollider = glTF_VCAST_vci_Collider.GetglTfColliderFromUnityCollider(collider);
                        if (gltfCollider == null)
                        {
                            Debug.LogWarningFormat("collider is not supported: {0}", collider.GetType().Name);
                            continue;
                        }

                        gltfNode.extensions.VCAST_vci_collider.colliders.Add(gltfCollider);
                    }
                }

                var rigidbodies = node.GetComponents <Rigidbody>();
                if (rigidbodies.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_rigidbody             = new glTF_VCAST_vci_rigidbody();
                    gltfNode.extensions.VCAST_vci_rigidbody.rigidbodies = new List <glTF_VCAST_vci_Rigidbody>();

                    foreach (var rigidbody in rigidbodies)
                    {
                        gltfNode.extensions.VCAST_vci_rigidbody.rigidbodies.Add(
                            glTF_VCAST_vci_Rigidbody.GetglTfRigidbodyFromUnityRigidbody(rigidbody));
                    }
                }

                var joints = node.GetComponents <Joint>();
                if (joints.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_joints        = new glTF_VCAST_vci_joints();
                    gltfNode.extensions.VCAST_vci_joints.joints = new List <glTF_VCAST_vci_joint>();

                    foreach (var joint in joints)
                    {
                        gltfNode.extensions.VCAST_vci_joints.joints.Add(
                            glTF_VCAST_vci_joint.GetglTFJointFromUnityJoint(joint, exporter.Nodes));
                    }
                }

                var item = node.GetComponent <VCISubItem>();
                if (item != null)
                {
                    var warning = item.ExportWarning;
                    if (!string.IsNullOrEmpty(warning))
                    {
                        throw new System.Exception(warning);
                    }

                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_item = new glTF_VCAST_vci_item
                    {
                        grabbable      = item.Grabbable,
                        scalable       = item.Scalable,
                        uniformScaling = item.UniformScaling,
                        groupId        = item.GroupId,
                    };
                }

                // Attachable
                var vciAttachable = node.GetComponent <VCIAttachable>();
                if (vciAttachable != null &&
                    vciAttachable.AttachableHumanBodyBones != null &&
                    vciAttachable.AttachableHumanBodyBones.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_attachable = new glTF_VCAST_vci_attachable
                    {
                        attachableHumanBodyBones = vciAttachable.AttachableHumanBodyBones.Select(x => x.ToString()).ToList(),
                        attachableDistance       = vciAttachable.AttachableDistance,
                    };
                }

                // Text
                var tmp = node.GetComponent <TextMeshPro>();
                var rt  = node.GetComponent <RectTransform>();
                if (tmp != null && rt != null)
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_rectTransform = new glTF_VCAST_vci_rectTransform()
                    {
                        rectTransform = glTF_VCAST_vci_RectTransform.CreateFromRectTransform(rt)
                    };

                    gltfNode.extensions.VCAST_vci_text = new glTF_VCAST_vci_text
                    {
                        text = glTF_VCAST_vci_Text.Create(tmp)
                    };
                }
            }

            // Audio
            var clips = exporter.Copy.GetComponentsInChildren <AudioSource>()
                        .Select(x => x.clip)
                        .Where(x => x != null)
                        .ToArray();
            if (clips.Any())
            {
                var audios = clips.Select(x => FromAudioClip(gltf, x)).Where(x => x != null).ToList();
                gltf.extensions.VCAST_vci_audios = new glTF_VCAST_vci_audios
                {
                    audios = audios
                };
            }

#if UNITY_EDITOR
            // Animation
            // None RootAnimation
            var animators  = exporter.Copy.GetComponentsInChildren <Animator>().Where(x => exporter.Copy != x.gameObject);
            var animations = exporter.Copy.GetComponentsInChildren <Animation>().Where(x => exporter.Copy != x.gameObject);
            // NodeIndex to AnimationClips
            Dictionary <int, AnimationClip[]> animationNodeList = new Dictionary <int, AnimationClip[]>();

            foreach (var animator in animators)
            {
                var animationClips = AnimationExporter.GetAnimationClips(animator);
                var nodeIndex      = exporter.Nodes.FindIndex(0, exporter.Nodes.Count, x => x == animator.transform);
                if (animationClips.Any() && nodeIndex != -1)
                {
                    animationNodeList.Add(nodeIndex, animationClips.ToArray());
                }
            }

            foreach (var animation in animations)
            {
                var animationClips = AnimationExporter.GetAnimationClips(animation);
                var nodeIndex      = exporter.Nodes.FindIndex(0, exporter.Nodes.Count, x => x == animation.transform);
                if (animationClips.Any() && nodeIndex != -1)
                {
                    animationNodeList.Add(nodeIndex, animationClips.ToArray());
                }
            }

            int bufferIndex = 0;
            foreach (var animationNode in animationNodeList)
            {
                List <int> clipIndices = new List <int>();
                // write animationClips
                foreach (var clip in animationNode.Value)
                {
                    var animationWithCurve = AnimationExporter.Export(clip, Nodes[animationNode.Key], Nodes);
                    AnimationExporter.WriteAnimationWithSampleCurves(gltf, animationWithCurve, clip.name, bufferIndex);
                    clipIndices.Add(gltf.animations.IndexOf(animationWithCurve.Animation));
                }

                // write node
                if (clipIndices.Any())
                {
                    var node = gltf.nodes[animationNode.Key];
                    if (node.extensions == null)
                    {
                        node.extensions = new glTFNode_extensions();
                    }

                    node.extensions.VCAST_vci_animation = new glTF_VCAST_vci_animation()
                    {
                        animationReferences = new List <glTF_VCAST_vci_animationReference>()
                    };

                    foreach (var index in clipIndices)
                    {
                        node.extensions.VCAST_vci_animation.animationReferences.Add(new glTF_VCAST_vci_animationReference()
                        {
                            animation = index
                        });
                    }
                }
            }
#endif

            // Effekseer
            var effekseerEmitters = exporter.Copy.GetComponentsInChildren <Effekseer.EffekseerEmitter>()
                                    .Where(x => x.effectAsset != null)
                                    .ToArray();

            if (effekseerEmitters.Any())
            {
                gltf.extensions.Effekseer = new glTF_Effekseer()
                {
                    effects = new List <glTF_Effekseer_effect>()
                };

                foreach (var emitter in effekseerEmitters)
                {
                    var index = exporter.Nodes.FindIndex(x => x == emitter.transform);
                    if (index < 0)
                    {
                        continue;
                    }

                    var effectIndex = AddEffekseerEffect(gltf, emitter);
                    var gltfNode    = gltf.nodes[index];
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }
                    if (gltfNode.extensions.Effekseer_emitters == null)
                    {
                        gltfNode.extensions.Effekseer_emitters = new glTF_Effekseer_emitters()
                        {
                            emitters = new List <glTF_Effekseer_emitter>()
                        };
                    }

                    gltfNode.extensions.Effekseer_emitters.emitters.Add(new glTF_Effekseer_emitter()
                    {
                        effectIndex   = effectIndex,
                        isLoop        = emitter.isLooping,
                        isPlayOnStart = emitter.playOnStart
                    });
                }
            }
        }
コード例 #5
0
ファイル: VCIExporter.cs プロジェクト: Santarh/VCI
        public override void Export()
        {
            base.Export();

            var gltf     = GLTF;
            var exporter = this;
            var go       = Copy;

            //exporter.Prepare(go);
            //exporter.Export();

            gltf.extensions.VCAST_vci_material_unity = new glTF_VCAST_vci_material_unity
            {
                materials = exporter.Materials
                            .Select(m => glTF_VCI_Material.CreateFromMaterial(m, exporter.TextureManager.Textures))
                            .ToList()
            };

            if (Copy == null)
            {
                return;
            }

            // vci interaction
            var vciObject = Copy.GetComponent <VCIObject>();

            if (vciObject != null)
            {
                // script
                if (vciObject.Scripts.Any())
                {
                    gltf.extensions.VCAST_vci_embedded_script = new glTF_VCAST_vci_embedded_script
                    {
                        scripts = vciObject.Scripts.Select(x =>
                        {
                            int viewIndex = -1;
#if UNITY_EDITOR
                            if (!string.IsNullOrEmpty(x.filePath))
                            {
                                if (File.Exists(x.filePath))
                                {
                                    using (var resader = new StreamReader(x.filePath))
                                    {
                                        string scriptStr = resader.ReadToEnd();
                                        viewIndex        = gltf.ExtendBufferAndGetViewIndex <byte>(0, Utf8String.Encoding.GetBytes(scriptStr));
                                    }
                                }
                                else
                                {
                                    Debug.LogError("script file not found. スクリプトファイルが見つかりませんでした: " + x.filePath);
                                    throw new FileNotFoundException(x.filePath);
                                }
                            }
                            else
#endif
                            {
                                viewIndex = gltf.ExtendBufferAndGetViewIndex <byte>(0, Utf8String.Encoding.GetBytes(x.source));
                            }

                            return(new glTF_VCAST_vci_embedded_script_source
                            {
                                name = x.name,
                                mimeType = x.mimeType,
                                targetEngine = x.targetEngine,
                                source = viewIndex,
                            });
                        })
                                  .ToList()
                    }
                }
                ;

                // meta
                var meta = vciObject.Meta;
                gltf.extensions.VCAST_vci_meta = new glTF_VCAST_vci_meta
                {
                    exporterVCIVersion = VCIVersion.VCI_VERSION,
                    specVersion        = VCISpecVersion.Version,

                    title = meta.title,

                    version            = meta.version,
                    author             = meta.author,
                    contactInformation = meta.contactInformation,
                    reference          = meta.reference,
                    description        = meta.description,

                    modelDataLicenseType     = meta.modelDataLicenseType,
                    modelDataOtherLicenseUrl = meta.modelDataOtherLicenseUrl,
                    scriptLicenseType        = meta.scriptLicenseType,
                    scriptOtherLicenseUrl    = meta.scriptOtherLicenseUrl,

                    scriptWriteProtected  = meta.scriptWriteProtected,
                    scriptEnableDebugging = meta.scriptEnableDebugging,
                    scriptFormat          = meta.scriptFormat
                };
                if (meta.thumbnail != null)
                {
                    gltf.extensions.VCAST_vci_meta.thumbnail = TextureIO.ExportTexture(
                        gltf, gltf.buffers.Count - 1, meta.thumbnail, glTFTextureTypes.Unknown);
                }
            }

            // collider & rigidbody & joint & item
            for (var i = 0; i < exporter.Nodes.Count; i++)
            {
                var node     = exporter.Nodes[i];
                var gltfNode = gltf.nodes[i];

                // 各ノードに複数のコライダーがあり得る
                var colliders = node.GetComponents <Collider>();
                if (colliders.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_collider           = new glTF_VCAST_vci_colliders();
                    gltfNode.extensions.VCAST_vci_collider.colliders = new List <glTF_VCAST_vci_Collider>();

                    foreach (var collider in colliders)
                    {
                        var gltfCollider = glTF_VCAST_vci_Collider.GetglTfColliderFromUnityCollider(collider);
                        if (gltfCollider == null)
                        {
                            Debug.LogWarningFormat("collider is not supported: {0}", collider.GetType().Name);
                            continue;
                        }

                        gltfNode.extensions.VCAST_vci_collider.colliders.Add(gltfCollider);
                    }
                }

                var rigidbodies = node.GetComponents <Rigidbody>();
                if (rigidbodies.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_rigidbody             = new glTF_VCAST_vci_rigidbody();
                    gltfNode.extensions.VCAST_vci_rigidbody.rigidbodies = new List <glTF_VCAST_vci_Rigidbody>();

                    foreach (var rigidbody in rigidbodies)
                    {
                        gltfNode.extensions.VCAST_vci_rigidbody.rigidbodies.Add(
                            glTF_VCAST_vci_Rigidbody.GetglTfRigidbodyFromUnityRigidbody(rigidbody));
                    }
                }

                var joints = node.GetComponents <Joint>();
                if (joints.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_joints        = new glTF_VCAST_vci_joints();
                    gltfNode.extensions.VCAST_vci_joints.joints = new List <glTF_VCAST_vci_joint>();

                    foreach (var joint in joints)
                    {
                        gltfNode.extensions.VCAST_vci_joints.joints.Add(
                            glTF_VCAST_vci_joint.GetglTFJointFromUnityJoint(joint, exporter.Nodes));
                    }
                }

                var item = node.GetComponent <VCISubItem>();
                if (item != null)
                {
                    var warning = item.ExportWarning;
                    if (!string.IsNullOrEmpty(warning))
                    {
                        throw new System.Exception(warning);
                    }

                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_item = new glTF_VCAST_vci_item
                    {
                        grabbable      = item.Grabbable,
                        scalable       = item.Scalable,
                        uniformScaling = item.UniformScaling,
                        groupId        = item.GroupId,
                    };
                }

                // Attachable
                var vciAttachable = node.GetComponent <VCIAttachable>();
                if (vciAttachable != null &&
                    vciAttachable.AttachableHumanBodyBones != null &&
                    vciAttachable.AttachableHumanBodyBones.Any())
                {
                    if (gltfNode.extensions == null)
                    {
                        gltfNode.extensions = new glTFNode_extensions();
                    }

                    gltfNode.extensions.VCAST_vci_attachable = new glTF_VCAST_vci_attachable
                    {
                        attachableHumanBodyBones = vciAttachable.AttachableHumanBodyBones.Select(x => x.ToString()).ToList(),
                        attachableDistance       = vciAttachable.AttachableDistance,
                    };
                }
            }

            var clips = exporter.Copy.GetComponentsInChildren <AudioSource>()
                        .Select(x => x.clip)
                        .Where(x => x != null)
                        .ToArray();
            if (clips.Any())
            {
                var audios = clips.Select(x => FromAudioClip(gltf, x)).Where(x => x != null).ToList();
                gltf.extensions.VCAST_vci_audios = new glTF_VCAST_vci_audios
                {
                    audios = audios
                };
            }
        }