Exemple #1
0
        private void WriteHeightMap(TerrainData terrain, PrefabContext prefabContext)
        {
            var w       = terrain.heightmapResolution;
            var h       = terrain.heightmapResolution;
            var max     = float.MinValue;
            var min     = float.MaxValue;
            var heights = terrain.GetHeights(0, 0, w, h);

            foreach (var height in heights)
            {
                if (height > max)
                {
                    max = height;
                }
                if (height < min)
                {
                    min = height;
                }
            }

            if (max < min)
            {
                max = 1;
                min = 0;
            }
            else if (max == min)
            {
                max = min + 0.1f;
            }

            using (var imageFile = _engine.TryCreate(terrain.GetKey(), EvaluateHeightMap(terrain), DateTime.MaxValue))
            {
                if (imageFile != null)
                {
                    using (var binaryWriter = new BinaryWriter(imageFile))
                    {
                        WriteTgaHeader(binaryWriter, 32, w, h);
                        for (var y = h - 1; y >= 0; --y)
                        {
                            for (var x = 0; x < w; ++x)
                            {
                                var height = (heights[h - y - 1, x] - min) / (max - min) * 255.0f;
                                var msb    = (byte)height;
                                var lsb    = (byte)((height - msb) * 255.0f);
                                binaryWriter.Write((byte)0);   //B - none
                                binaryWriter.Write(lsb);       //G - LSB
                                binaryWriter.Write(msb);       //R - MSB
                                binaryWriter.Write((byte)255); //A - none
                            }
                        }
                    }
                }
            }
        }
Exemple #2
0
        public void ExportAnimation(AnimationClip clip, PrefabContext prefabContext)
        {
            if (!_engine.Options.ExportAnimations)
            {
                return;
            }

            var aniFilePath = EvaluateAnimationName(clip, prefabContext);

            ExportMetadata(ExportUtils.ReplaceExtension(aniFilePath, ".xml"), clip, prefabContext);

            using (var file = _engine.TryCreate(clip.GetKey(), aniFilePath,
                                                ExportUtils.GetLastWriteTimeUtc(clip)))
            {
                if (file == null)
                {
                    return;
                }
                using (var writer = new BinaryWriter(file))
                {
                    writer.Write(new byte[] { 0x55, 0x41, 0x4e, 0x49 });
                    WriteStringSZ(writer, _engine.DecorateName(clip.name));
                    writer.Write(clip.length);

                    // Legacy animation
                    if (clip.legacy)
                    {
                        WriteTracksAsIs(clip, writer);
                    }
                    else if (clip.isHumanMotion)
                    {
                        WriteHumanoidAnimation(clip, writer);
                    }
                    else
                    {
                        WriteGenericAnimation(clip, writer);
                    }
                }
            }
        }
        public void ExportAnimation(AnimationClip clipAnimation, PrefabContext prefabContext)
        {
            if (!_engine.Options.ExportAnimations)
            {
                return;
            }

            var name = GetSafeFileName(_engine.DecorateName(clipAnimation.name));

            //_assetCollection.AddAnimationPath(clipAnimation, fileName);

            var aniFilePath = EvaluateAnimationName(clipAnimation, prefabContext);

            using (var file = _engine.TryCreate(clipAnimation.GetKey(), aniFilePath,
                                                ExportUtils.GetLastWriteTimeUtc(clipAnimation)))
            {
                if (file == null)
                {
                    return;
                }
                using (var writer = new BinaryWriter(file))
                {
                    writer.Write(new byte[] { 0x55, 0x41, 0x4e, 0x49 });
                    WriteStringSZ(writer, _engine.DecorateName(clipAnimation.name));
                    writer.Write(clipAnimation.length);

                    if (clipAnimation.legacy)
                    {
                        WriteTracksAsIs(clipAnimation, writer);
                    }
                    else
                    {
                        var allBindings = AnimationUtility.GetCurveBindings(clipAnimation);
                        var rootBones   =
                            new HashSet <string>(allBindings.Select(_ => GetRootBoneName(_)).Where(_ => _ != null));
                        if (rootBones.Count != 1)
                        {
                            Debug.LogWarning(aniFilePath + ": Multiple root bones found (" +
                                             string.Join(", ", rootBones.ToArray()) +
                                             "), falling back to curve export");
                            WriteTracksAsIs(clipAnimation, writer);
                        }
                        else
                        {
                            var rootBoneName = rootBones.First();
                            var rootGOs      = _skeletons
                                               .Select(_ => _.name == rootBoneName ? _.transform : _.transform.Find(rootBoneName))
                                               .Where(_ => _ != null).ToList();
                            if (rootGOs.Count == 1)
                            {
                                WriteSkelAnimation(clipAnimation, rootGOs.First().gameObject, writer);
                            }
                            else
                            {
                                Debug.LogWarning(aniFilePath +
                                                 ": Multiple game objects found that match root bone name, falling back to curve export");
                                WriteTracksAsIs(clipAnimation, writer);
                            }
                        }
                    }
                }
            }
        }