public void ExtractAudio(UnityPath prefabPath) { #if UNITY_EDITOR if (GLTF.extensions.VCAST_vci_audios == null || GLTF.extensions.VCAST_vci_audios.audios == null || GLTF.extensions.VCAST_vci_audios.audios.Count == 0) { return; } var prefabParentDir = prefabPath.Parent; // glb buffer var folder = prefabPath.GetAssetFolder(".Audios"); var created = 0; for (var i = 0; i < GLTF.extensions.VCAST_vci_audios.audios.Count; ++i) { folder.EnsureFolder(); var audio = GLTF.extensions.VCAST_vci_audios.audios[i]; var bytes = GLTF.GetViewBytes(audio.bufferView); var audioBuffer = new byte[bytes.Count]; Buffer.BlockCopy(bytes.Array, bytes.Offset, audioBuffer, 0, audioBuffer.Length); System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex( @"(?<type>.*)/(?<ext>.*)", System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Singleline); var match = r.Match(audio.mimeType); var ext = match.Groups["ext"].Value; if (!string.IsNullOrEmpty(ext)) { var path = string.Format("{0}/{1}.{2}", folder.Value, audio.name, ext); File.WriteAllBytes(path, audioBuffer); AudioAssetPathList.Add(audio.name, path); } created++; } if (created > 0) { AssetDatabase.Refresh(); } #endif }
public void ExtractAudio(UnityPath prefabPath) { #if UNITY_EDITOR if (GLTF.extensions.VCAST_vci_audios == null || GLTF.extensions.VCAST_vci_audios.audios == null || GLTF.extensions.VCAST_vci_audios.audios.Count == 0) { return; } var prefabParentDir = prefabPath.Parent; // glb buffer var folder = prefabPath.GetAssetFolder(".Audios"); var created = 0; for (var i = 0; i < GLTF.extensions.VCAST_vci_audios.audios.Count; ++i) { folder.EnsureFolder(); var audio = GLTF.extensions.VCAST_vci_audios.audios[i]; var bytes = GLTF.GetViewBytes(audio.bufferView); var audioBuffer = new byte[bytes.Count]; Buffer.BlockCopy(bytes.Array, bytes.Offset, audioBuffer, 0, audioBuffer.Length); var path = string.Format("{0}/{1}.wav", folder.Value, audio.name); File.WriteAllBytes(path, audioBuffer); AudioAssetPathList.Add(audio.name, path); created++; } if (created > 0) { AssetDatabase.Refresh(); } #endif }
public void ExtractEffekseer(UnityPath prefabPath) { #if UNITY_EDITOR if (GLTF.extensions.Effekseer == null || GLTF.extensions.Effekseer.effects == null || GLTF.extensions.Effekseer.effects.Count == 0) { return; } var prefabParentDir = prefabPath.Parent; var folder = prefabPath.GetAssetFolder(".Effekseers"); for (var i = 0; i < GLTF.extensions.Effekseer.effects.Count; ++i) { folder.EnsureFolder(); var effect = GLTF.extensions.Effekseer.effects[i]; var effectDir = string.Format("{0}/{1}", folder.Value, effect.effectName); SafeCreateDirectory(effectDir); var textureDir = string.Format("{0}/{1}", effectDir, "Textures"); var modelDir = string.Format("{0}/{1}", effectDir, "Models"); var body = GLTF.GetViewBytes(effect.body.bufferView).ToArray(); var resourcePath = new Effekseer.EffekseerResourcePath(); if (!Effekseer.EffekseerEffectAsset.ReadResourcePath(body, ref resourcePath)) { continue; } // Texture var writeTexturePathList = new List <string>(); if (effect.images != null && effect.images.Any()) { for (int t = 0; t < effect.images.Count; t++) { var image = effect.images[t]; var path = resourcePath.TexturePathList[t]; var buffer = GLTF.GetViewBytes(image.bufferView); var texturePath = string.Format("{0}/{1}", textureDir, Path.GetFileName(path)); SafeCreateDirectory(Path.GetDirectoryName(texturePath)); if (image.mimeType == glTF_Effekseer_image.MimeTypeString.Png) { File.WriteAllBytes(texturePath, buffer.ToArray()); writeTexturePathList.Add(texturePath); } else { Debug.LogError(string.Format("image format {0} is not suppported.", image.mimeType)); } } } // Models if (effect.models != null && effect.models.Any()) { for (int t = 0; t < effect.models.Count; t++) { var model = effect.models[t]; var path = resourcePath.ModelPathList[t]; var buffer = GLTF.GetViewBytes(model.bufferView); var modelPath = string.Format("{0}/{1}", modelDir, Path.GetFileName(path)); SafeCreateDirectory(Path.GetDirectoryName(modelPath)); File.WriteAllBytes(modelPath, buffer.ToArray()); AssetDatabase.ImportAsset(modelPath); } } EditorApplication.delayCall += () => { // fix texture setting foreach (var texturePath in writeTexturePathList) { AssetDatabase.ImportAsset(texturePath); var textureImporter = (TextureImporter)TextureImporter.GetAtPath(texturePath); if (textureImporter != null) { textureImporter.isReadable = true; textureImporter.textureCompression = TextureImporterCompression.Uncompressed; textureImporter.SaveAndReimport(); } } // efk var assetPath = string.Format("{0}/{1}.efk", effectDir, effect.effectName); Effekseer.EffekseerEffectAsset.CreateAsset(assetPath, body); var effectAsset = AssetDatabase.LoadAssetAtPath <Effekseer.EffekseerEffectAsset>(System.IO.Path.ChangeExtension(assetPath, "asset")); EffekseerEffectAssets.Add(effectAsset); // find assets // textures for (int t = 0; t < effectAsset.textureResources.Count(); t++) { var path = effectAsset.textureResources[t].path; if (string.IsNullOrEmpty(path)) { continue; } var fileName = Path.GetFileName(path); if (string.IsNullOrEmpty(fileName)) { continue; } Texture2D texture = UnityEditor.AssetDatabase.LoadAssetAtPath( string.Format("{0}/{1}", textureDir, fileName), typeof(Texture2D)) as Texture2D; if (texture != null) { effectAsset.textureResources[t].texture = texture; } } // models for (int t = 0; t < effectAsset.modelResources.Count(); t++) { var path = effectAsset.modelResources[t].path; if (string.IsNullOrEmpty(path)) { continue; } var fileName = Path.GetFileName(path); if (string.IsNullOrEmpty(fileName)) { continue; } Effekseer.EffekseerModelAsset model = UnityEditor.AssetDatabase.LoadAssetAtPath( string.Format("{0}/{1}", modelDir, fileName), typeof(Effekseer.EffekseerModelAsset)) as Effekseer.EffekseerModelAsset; if (model != null) { effectAsset.modelResources[t].asset = model; } } }; } #endif }
public virtual IEnumerator SetupEffekseer() { // Effekseer if (GLTF.extensions != null && GLTF.extensions.Effekseer != null) { for (var i = 0; i < GLTF.nodes.Count; i++) { var node = GLTF.nodes[i]; var go = Nodes[i].gameObject; if (node.extensions != null && node.extensions.Effekseer_emitters != null && node.extensions.Effekseer_emitters.emitters != null) { foreach (var emitter in node.extensions.Effekseer_emitters.emitters) { var effectIndex = emitter.effectIndex; if (Application.isPlaying) { var effect = GLTF.extensions.Effekseer.effects[effectIndex]; var bodySegment = GLTF.GetViewBytes(effect.body.bufferView); byte[] body = new byte[bodySegment.Count]; Buffer.BlockCopy(bodySegment.Array, bodySegment.Offset, body, 0, body.Count()); var resourcePath = new Effekseer.EffekseerResourcePath(); if (!Effekseer.EffekseerEffectAsset.ReadResourcePath(body, ref resourcePath)) { continue; } yield return(null); // Images var effekseerTextures = new List <Effekseer.Internal.EffekseerTextureResource>(); if (effect.images != null && effect.images.Any()) { for (int t = 0; t < effect.images.Count; t++) { var image = effect.images[t]; var path = resourcePath.TexturePathList[t]; var buffer = GLTF.GetViewBytes(image.bufferView); if (image.mimeType == glTF_Effekseer_image.MimeTypeString.Png) { Texture2D texture = new Texture2D(2, 2); byte[] copyBuffer = new byte[buffer.Count]; Buffer.BlockCopy(buffer.Array, buffer.Offset, copyBuffer, 0, copyBuffer.Count()); texture.LoadImage(copyBuffer); effekseerTextures.Add(new Effekseer.Internal.EffekseerTextureResource() { path = path, texture = texture }); } else { Debug.LogError(string.Format("image format {0} is not suppported.", image.mimeType)); } yield return(null); } } // Models var effekseerModels = new List <Effekseer.Internal.EffekseerModelResource>(); if (effect.models != null && effect.models.Any()) { for (int t = 0; t < effect.models.Count; t++) { var model = effect.models[t]; var path = resourcePath.ModelPathList[t]; path = Path.ChangeExtension(path, "asset"); var modelSegment = GLTF.GetViewBytes(model.bufferView); byte[] modelBuffer = new byte[modelSegment.Count]; Buffer.BlockCopy(modelSegment.Array, modelSegment.Offset, modelBuffer, 0, modelBuffer.Count()); effekseerModels.Add(new Effekseer.Internal.EffekseerModelResource() { path = path, asset = new Effekseer.EffekseerModelAsset() { bytes = modelBuffer } }); yield return(null); } } Effekseer.EffekseerEffectAsset effectAsset = ScriptableObject.CreateInstance <Effekseer.EffekseerEffectAsset>(); effectAsset.name = effect.effectName; effectAsset.efkBytes = body; effectAsset.Scale = effect.scale; effectAsset.textureResources = effekseerTextures.ToArray(); effectAsset.modelResources = effekseerModels.ToArray(); effectAsset.soundResources = new Effekseer.Internal.EffekseerSoundResource[0]; yield return(null); var emitterComponent = go.AddComponent <Effekseer.EffekseerEmitter>(); emitterComponent.effectAsset = effectAsset; emitterComponent.playOnStart = emitter.isPlayOnStart; emitterComponent.isLooping = emitter.isLoop; emitterComponent.effectAsset.LoadEffect(); EffekseerEmitterComponents.Add(emitterComponent); } else { #if UNITY_EDITOR var emitterComponent = go.AddComponent <Effekseer.EffekseerEmitter>(); emitterComponent.effectAsset = EffekseerEffectAssets[effectIndex]; emitterComponent.playOnStart = emitter.isPlayOnStart; emitterComponent.isLooping = emitter.isLoop; EffekseerEmitterComponents.Add(emitterComponent); #endif } } } } } yield break; }
public IEnumerator SetupCoroutine() { VCIObject = Root.AddComponent <VCIObject>(); yield return(ToUnity(meta => VCIObject.Meta = meta)); // Script if (GLTF.extensions.VCAST_vci_embedded_script != null) { VCIObject.Scripts.AddRange(GLTF.extensions.VCAST_vci_embedded_script.scripts.Select(x => { var source = ""; try { var bytes = GLTF.GetViewBytes(x.source); source = Utf8String.Encoding.GetString(bytes.Array, bytes.Offset, bytes.Count); } catch (Exception) { // 握りつぶし } return(new VCIObject.Script { name = x.name, mimeType = x.mimeType, targetEngine = x.targetEngine, source = source }); })); } // Audio if (GLTF.extensions.VCAST_vci_audios != null && GLTF.extensions.VCAST_vci_audios.audios != null) { var root = Root; foreach (var audio in GLTF.extensions.VCAST_vci_audios.audios) { #if ((NET_4_6 || NET_STANDARD_2_0) && UNITY_2017_1_OR_NEWER) if (Application.isPlaying) { var bytes = GLTF.GetViewBytes(audio.bufferView); yield return(AudioImporter.Import(audio, bytes, root)); } else #endif { #if UNITY_EDITOR var audioClip = AssetDatabase.LoadAssetAtPath(AudioAssetPathList[audio.name], typeof(AudioClip)) as AudioClip; if (audioClip != null) { var source = root.AddComponent <AudioSource>(); source.clip = audioClip; } else { Debug.LogWarning("AudioFile NotFound: " + audio.name); } #endif } } } // Animation var rootAnimation = Root.GetComponent <Animation>(); if (rootAnimation != null) { rootAnimation.playAutomatically = false; } // SubItem for (var i = 0; i < GLTF.nodes.Count; i++) { var node = GLTF.nodes[i]; if (node.extensions != null) { if (node.extensions.VCAST_vci_item != null) { var go = Nodes[i].gameObject; var item = go.AddComponent <VCISubItem>(); item.Grabbable = node.extensions.VCAST_vci_item.grabbable; item.Scalable = node.extensions.VCAST_vci_item.scalable; item.UniformScaling = node.extensions.VCAST_vci_item.uniformScaling; item.GroupId = node.extensions.VCAST_vci_item.groupId; } } } }
public IEnumerator SetupCorutine() { VCIObject = Root.AddComponent <VCIObject>(); yield return(ToUnity(meta => VCIObject.Meta = meta)); // Script if (GLTF.extensions.VCAST_vci_embedded_script != null) { VCIObject.Scripts.AddRange(GLTF.extensions.VCAST_vci_embedded_script.scripts.Select(x => { var source = ""; try { var bytes = GLTF.GetViewBytes(x.source); source = Utf8String.Encoding.GetString(bytes.Array, bytes.Offset, bytes.Count); } catch (Exception) { // 握りつぶし } return(new VCIObject.Script { name = x.name, mimeType = x.mimeType, targetEngine = x.targetEngine, source = source }); })); } // Audio if (GLTF.extensions.VCAST_vci_audios != null && GLTF.extensions.VCAST_vci_audios.audios != null) { var root = Root; foreach (var audio in GLTF.extensions.VCAST_vci_audios.audios) { #if ((NET_4_6 || NET_STANDARD_2_0) && UNITY_2017_1_OR_NEWER) if (Application.isPlaying) { var bytes = GLTF.GetViewBytes(audio.bufferView); WaveUtil.WaveHeaderData waveHeader; var audioBuffer = new byte[bytes.Count]; Buffer.BlockCopy(bytes.Array, bytes.Offset, audioBuffer, 0, audioBuffer.Length); if (WaveUtil.TryReadHeader(audioBuffer, out waveHeader)) { AudioClip audioClip = null; yield return(AudioClipMaker.Create( audio.name, audioBuffer, 44, waveHeader.BitPerSample, waveHeader.DataChunkSize / waveHeader.BlockSize, waveHeader.Channel, waveHeader.SampleRate, false, (clip) => { audioClip = clip; })); if (audioClip != null) { var source = root.AddComponent <AudioSource>(); source.clip = audioClip; source.playOnAwake = false; source.loop = false; } } } else #endif { #if UNITY_EDITOR var audioClip = AssetDatabase.LoadAssetAtPath(AudioAssetPathList[audio.name], typeof(AudioClip)) as AudioClip; if (audioClip != null) { var source = root.AddComponent <AudioSource>(); source.clip = audioClip; } else { Debug.LogWarning("AudioFile NotFound: " + audio.name); } #endif } } } // Animation var animation = Root.GetComponent <Animation>(); if (animation != null) { animation.playAutomatically = false; } for (var i = 0; i < GLTF.nodes.Count; i++) { var node = GLTF.nodes[i]; var go = Nodes[i].gameObject; if (node.extensions != null) { if (node.extensions.VCAST_vci_item != null) { var item = go.AddComponent <VCISubItem>(); item.Grabbable = node.extensions.VCAST_vci_item.grabbable; item.Scalable = node.extensions.VCAST_vci_item.scalable; item.UniformScaling = node.extensions.VCAST_vci_item.uniformScaling; item.GroupId = node.extensions.VCAST_vci_item.groupId; } } } }