Exemple #1
0
        public void Dispose()
        {
            for (int i = 0; i < meshFrames.Count; i++)
            {
                MeshContainer mesh = meshFrames[i].MeshContainer;
                while (mesh != null)
                {
                    if ((mesh.MeshData != null) && (mesh.MeshData.Mesh != null))
                    {
                        mesh.MeshData.Mesh.Dispose();
                    }
                    if (mesh is MorphMeshContainer)
                    {
                        MorphMeshContainer morphMesh = (MorphMeshContainer)mesh;
                        if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                        {
                            morphMesh.StartBuffer.Dispose();
                        }
                        if (morphMesh.EndBuffer != null)
                        {
                            morphMesh.EndBuffer.Dispose();
                        }
                        if (morphMesh.CommonBuffer != null)
                        {
                            morphMesh.CommonBuffer.Dispose();
                        }
                        if (morphMesh.IndexBuffer != null)
                        {
                            morphMesh.IndexBuffer.Dispose();
                        }
                    }

                    for (int j = 0; j < Textures.Length; j++)
                    {
                        Texture tex = Textures[j];
                        if ((tex != null) && !tex.Disposed)
                        {
                            tex.Dispose();
                        }
                    }

                    mesh = mesh.NextMeshContainer;
                }
            }

            rootFrame.Dispose();
            AnimationController.Dispose();

            tweeningVertDec.Dispose();

            IsDisposed = true;
        }
Exemple #2
0
        private void DrawMeshFrame(AnimationFrame frame)
        {
            if (frame.MeshContainer is AnimationMeshContainer)
            {
                AnimationMeshContainer animMeshContainer = (AnimationMeshContainer)frame.MeshContainer;
                if (animMeshContainer.BoneNames.Length > 0)
                {
                    device.SetRenderState(RenderState.VertexBlend, VertexBlend.Weights3);
                    device.SetRenderState(RenderState.IndexedVertexBlendEnable, true);
                    // uncomment to emphazise bone weights and darken everything else
                    // device.SetRenderState(RenderState.DiffuseMaterialSource, ColorSource.Color1);
                    device.SetRenderState(RenderState.AmbientMaterialSource, ColorSource.Color1);

                    for (int i = 0; i < animMeshContainer.BoneNames.Length; i++)
                    {
                        if (animMeshContainer.BoneFrames[i] != null)
                        {
                            device.SetTransform(i, animMeshContainer.BoneOffsets[i] * animMeshContainer.BoneFrames[i].CombinedTransform);
                        }
                    }
                }
                else
                {
                    device.SetRenderState(RenderState.VertexBlend, VertexBlend.Disable);
                    device.SetRenderState(RenderState.AmbientMaterialSource, ColorSource.Material);
                    device.SetTransform(TransformState.World, frame.CombinedTransform);
                }

                submeshNum = 0;
                while (animMeshContainer != null)
                {
                    DrawAnimationMeshContainer(animMeshContainer);
                    animMeshContainer = (AnimationMeshContainer)animMeshContainer.NextMeshContainer;
                    submeshNum++;
                }
            }
            else if (frame.MeshContainer is MorphMeshContainer)
            {
                MorphMeshContainer morphMeshContainer = (MorphMeshContainer)frame.MeshContainer;
                device.SetRenderState(RenderState.AmbientMaterialSource, ColorSource.Material);
                device.SetTransform(TransformState.World, frame.CombinedTransform);

                submeshNum = 0;
                DrawMorphMeshContainer(morphMeshContainer);
            }
        }
Exemple #3
0
        private void DrawMorphMeshContainer(MorphMeshContainer meshContainer)
        {
            device.SetRenderState(RenderState.ZEnable, ZBufferType.UseZBuffer);
            device.SetRenderState(RenderState.Lighting, true);

            Cull culling = (Gui.Renderer.Culling) ? Cull.Counterclockwise : Cull.None;

            device.SetRenderState(RenderState.CullMode, culling);

            FillMode fill = (Gui.Renderer.Wireframe) ? FillMode.Wireframe : FillMode.Solid;

            device.SetRenderState(RenderState.FillMode, fill);

            int matIdx = meshContainer.MaterialIndex;

            device.Material = ((matIdx >= 0) && (matIdx < Materials.Length)) ? Materials[matIdx] : nullMaterial;

            int     texIdx = meshContainer.TextureIndex;
            Texture tex    = ((texIdx >= 0) && (texIdx < Textures.Length)) ? Textures[texIdx] : null;

            device.SetTexture(0, tex);

            device.SetRenderState(RenderState.VertexBlend, VertexBlend.Tweening);
            device.SetRenderState(RenderState.TweenFactor, meshContainer.TweenFactor);

            device.VertexDeclaration = tweeningVertDec;
            device.Indices           = meshContainer.IndexBuffer;
            device.SetStreamSource(0, meshContainer.StartBuffer, 0, Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream0)));
            device.SetStreamSource(1, meshContainer.EndBuffer, 0, Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream1)));
            device.SetStreamSource(2, meshContainer.CommonBuffer, 0, Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream2)));
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, meshContainer.VertexCount, 0, meshContainer.FaceCount);

            if (HighlightSubmesh.Contains(submeshNum))
            {
                device.SetRenderState(RenderState.ZEnable, ZBufferType.DontUseZBuffer);
                device.SetRenderState(RenderState.FillMode, FillMode.Wireframe);
                device.Material = highlightMaterial;
                device.SetTexture(0, null);
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, meshContainer.VertexCount, 0, meshContainer.FaceCount);
            }
        }
Exemple #4
0
        public void SetTweenFactor(xxFrame meshFrame, xaMorphIndexSet idxSet, float tweenFactor)
        {
            foreach (AnimationFrame frame in meshFrames)
            {
                if (frame.Name == meshFrame.Name)
                {
                    xxMesh xxMesh     = meshFrame.Mesh;
                    int    meshObjIdx = xa.MorphMeshObjIdx(idxSet.MeshIndices, xxMesh);
                    if (meshObjIdx < 0)
                    {
                        Report.ReportLog("no valid mesh object was found for the morph");
                        return;
                    }
                    MeshContainer animMesh = frame.MeshContainer;
                    for (int i = 1; i < meshObjIdx; i++)
                    {
                        animMesh = animMesh.NextMeshContainer;
                        if (animMesh == null)
                        {
                            break;
                        }
                    }
                    if (animMesh == null)
                    {
                        Report.ReportLog("Bad submesh specified.");
                        return;
                    }
                    MorphMeshContainer morphMesh = (MorphMeshContainer)animMesh;

                    morphMesh.TweenFactor = tweenFactor;
                    return;
                }
            }
            Report.ReportLog("Mesh frame " + meshFrame + " not displayed.");
            return;
        }
        private void DrawMorphMeshContainer(MorphMeshContainer meshContainer)
        {
            device.SetRenderState(RenderState.ZEnable, ZBufferType.UseZBuffer);
            device.SetRenderState(RenderState.Lighting, true);

            Cull culling = (Gui.Renderer.Culling) ? Cull.Counterclockwise : Cull.None;
            device.SetRenderState(RenderState.CullMode, culling);

            FillMode fill = (Gui.Renderer.Wireframe) ? FillMode.Wireframe : FillMode.Solid;
            device.SetRenderState(RenderState.FillMode, fill);

            int matIdx = meshContainer.MaterialIndex;
            device.Material = ((matIdx >= 0) && (matIdx < Materials.Length)) ? Materials[matIdx] : nullMaterial;

            int texIdx = meshContainer.TextureIndex;
            Texture tex = ((texIdx >= 0) && (texIdx < Textures.Length)) ? Textures[texIdx] : null;
            device.SetTexture(0, tex);

            device.SetRenderState(RenderState.VertexBlend, VertexBlend.Tweening);
            device.SetRenderState(RenderState.TweenFactor, meshContainer.TweenFactor);

            device.VertexDeclaration = tweeningVertDec;
            device.Indices = meshContainer.IndexBuffer;
            device.SetStreamSource(0, meshContainer.StartBuffer, 0, Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream0)));
            device.SetStreamSource(1, meshContainer.EndBuffer, 0, Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream1)));
            device.SetStreamSource(2, meshContainer.CommonBuffer, 0, Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream2)));
            device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, meshContainer.VertexCount, 0, meshContainer.FaceCount);

            if (HighlightSubmesh.Contains(submeshNum))
            {
                device.SetRenderState(RenderState.ZEnable, ZBufferType.DontUseZBuffer);
                device.SetRenderState(RenderState.FillMode, FillMode.Wireframe);
                device.Material = highlightMaterial;
                device.SetTexture(0, null);
                device.DrawIndexedPrimitives(PrimitiveType.TriangleList, 0, 0, meshContainer.VertexCount, 0, meshContainer.FaceCount);
            }
        }
        public float SetMorphKeyframe(xxFrame meshFrame, xaMorphIndexSet idxSet, xaMorphKeyframe keyframe, bool asStart)
        {
            foreach (AnimationFrame frame in meshFrames)
            {
                if (frame.Name == meshFrame.Name)
                {
                    xxMesh xxMesh = meshFrame.Mesh;
                    int meshObjIdx = xa.MorphMeshObjIdx(idxSet.MeshIndices, xxMesh);
                    if (meshObjIdx < 0)
                    {
                        Report.ReportLog("no valid mesh object was found for the morph");
                        return -1f;
                    }
                    MorphMeshContainer morphMesh = null;
                    AnimationMeshContainer animMesh = frame.MeshContainer as AnimationMeshContainer;
                    if (animMesh != null)
                    {
                        for (int i = 1; i < meshObjIdx; i++)
                        {
                            animMesh = (AnimationMeshContainer)animMesh.NextMeshContainer;
                            if (animMesh == null)
                                break;
                        }
                        if (animMesh == null)
                        {
                            Report.ReportLog("Bad submesh specified.");
                            return -1f;
                        }

                        morphMesh = new MorphMeshContainer();
                        morphMesh.FaceCount = xxMesh.SubmeshList[meshObjIdx].FaceList.Count;
                        morphMesh.IndexBuffer = animMesh.MeshData.Mesh.IndexBuffer;

                        morphMesh.VertexCount = xxMesh.SubmeshList[meshObjIdx].VertexList.Count;
                        List<xxVertex> vertexList = xxMesh.SubmeshList[meshObjIdx].VertexList;
                        VertexBuffer vertBuffer = CreateMorphVertexBuffer(idxSet, keyframe, vertexList);
                        morphMesh.StartBuffer = morphMesh.EndBuffer = vertBuffer;

                        int vertBufferSize = morphMesh.VertexCount * Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream2));
                        vertBuffer = new VertexBuffer(device, vertBufferSize, Usage.WriteOnly, VertexFormat.Texture1, Pool.Managed);
                        using (DataStream vertexStream = vertBuffer.Lock(0, vertBufferSize, LockFlags.None))
                        {
                            for (int i = 0; i < vertexList.Count; i++)
                            {
                                xxVertex vertex = vertexList[i];
                                vertexStream.Write(vertex.UV[0]);
                                vertexStream.Write(vertex.UV[1]);
                            }
                            vertBuffer.Unlock();
                        }
                        morphMesh.CommonBuffer = vertBuffer;

                        morphMesh.MaterialIndex = animMesh.MaterialIndex;
                        morphMesh.TextureIndex = animMesh.TextureIndex;

                        morphMesh.NextMeshContainer = animMesh;
                        frame.MeshContainer = morphMesh;

                        morphMesh.TweenFactor = 0.0f;
                    }
                    else
                    {
                        morphMesh = frame.MeshContainer as MorphMeshContainer;
                        List<xxVertex> vertexList = xxMesh.SubmeshList[meshObjIdx].VertexList;
                        VertexBuffer vertBuffer = CreateMorphVertexBuffer(idxSet, keyframe, vertexList);
                        if (asStart)
                        {
                            if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                            {
                                morphMesh.StartBuffer.Dispose();
                            }
                            morphMesh.StartBuffer = vertBuffer;
                            morphMesh.TweenFactor = 0.0f;
                        }
                        else
                        {
                            if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                            {
                                morphMesh.EndBuffer.Dispose();
                            }
                            morphMesh.EndBuffer = vertBuffer;
                            morphMesh.TweenFactor = 1.0f;
                        }
                    }
                    return morphMesh.TweenFactor;
                }
            }
            Report.ReportLog("Mesh frame " + meshFrame + " not displayed.");
            return -1f;
        }
Exemple #7
0
        public float UnsetMorphKeyframe(xxFrame meshFrame, xaMorphIndexSet idxSet, bool asStart)
        {
            foreach (AnimationFrame frame in meshFrames)
            {
                if (frame.Name == meshFrame.Name)
                {
                    xxMesh xxMesh     = meshFrame.Mesh;
                    int    meshObjIdx = xa.MorphMeshObjIdx(idxSet.MeshIndices, xxMesh);
                    if (meshObjIdx < 0)
                    {
                        Report.ReportLog("no valid mesh object was found for the morph");
                        return(-1f);
                    }
                    MeshContainer animMesh = frame.MeshContainer;
                    for (int i = 1; i < meshObjIdx; i++)
                    {
                        animMesh = animMesh.NextMeshContainer;
                        if (animMesh == null)
                        {
                            break;
                        }
                    }
                    if (animMesh == null)
                    {
                        Report.ReportLog("Bad submesh specified.");
                        return(-1f);
                    }
                    MorphMeshContainer morphMesh = (MorphMeshContainer)animMesh;

                    if (asStart)
                    {
                        if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                        {
                            morphMesh.StartBuffer.Dispose();
                            morphMesh.StartBuffer = morphMesh.EndBuffer;
                        }
                        else
                        {
                            frame.MeshContainer = morphMesh.NextMeshContainer;
                        }
                        morphMesh.TweenFactor = 1.0f;
                    }
                    else
                    {
                        if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                        {
                            morphMesh.EndBuffer.Dispose();
                            morphMesh.EndBuffer = morphMesh.StartBuffer;
                        }
                        else
                        {
                            frame.MeshContainer = morphMesh.NextMeshContainer;
                        }
                        morphMesh.TweenFactor = 0.0f;
                    }
                    return(morphMesh.TweenFactor);
                }
            }
            Report.ReportLog("Mesh frame " + meshFrame + " not displayed.");
            return(-1f);
        }
Exemple #8
0
        public float SetMorphKeyframe(xxFrame meshFrame, xaMorphIndexSet idxSet, xaMorphKeyframe keyframe, bool asStart)
        {
            foreach (AnimationFrame frame in meshFrames)
            {
                if (frame.Name == meshFrame.Name)
                {
                    xxMesh xxMesh     = meshFrame.Mesh;
                    int    meshObjIdx = xa.MorphMeshObjIdx(idxSet.MeshIndices, xxMesh);
                    if (meshObjIdx < 0)
                    {
                        Report.ReportLog("no valid mesh object was found for the morph");
                        return(-1f);
                    }
                    MorphMeshContainer     morphMesh = null;
                    AnimationMeshContainer animMesh  = frame.MeshContainer as AnimationMeshContainer;
                    if (animMesh != null)
                    {
                        for (int i = 1; i < meshObjIdx; i++)
                        {
                            animMesh = (AnimationMeshContainer)animMesh.NextMeshContainer;
                            if (animMesh == null)
                            {
                                break;
                            }
                        }
                        if (animMesh == null)
                        {
                            Report.ReportLog("Bad submesh specified.");
                            return(-1f);
                        }

                        morphMesh             = new MorphMeshContainer();
                        morphMesh.FaceCount   = xxMesh.SubmeshList[meshObjIdx].FaceList.Count;
                        morphMesh.IndexBuffer = animMesh.MeshData.Mesh.IndexBuffer;

                        morphMesh.VertexCount = xxMesh.SubmeshList[meshObjIdx].VertexList.Count;
                        List <xxVertex> vertexList = xxMesh.SubmeshList[meshObjIdx].VertexList;
                        VertexBuffer    vertBuffer = CreateMorphVertexBuffer(idxSet, keyframe, vertexList);
                        morphMesh.StartBuffer = morphMesh.EndBuffer = vertBuffer;

                        int vertBufferSize = morphMesh.VertexCount * Marshal.SizeOf(typeof(TweeningMeshesVertexBufferFormat.Stream2));
                        vertBuffer = new VertexBuffer(device, vertBufferSize, Usage.WriteOnly, VertexFormat.Texture1, Pool.Managed);
                        using (DataStream vertexStream = vertBuffer.Lock(0, vertBufferSize, LockFlags.None))
                        {
                            for (int i = 0; i < vertexList.Count; i++)
                            {
                                xxVertex vertex = vertexList[i];
                                vertexStream.Write(vertex.UV[0]);
                                vertexStream.Write(vertex.UV[1]);
                            }
                            vertBuffer.Unlock();
                        }
                        morphMesh.CommonBuffer = vertBuffer;

                        morphMesh.MaterialIndex = animMesh.MaterialIndex;
                        morphMesh.TextureIndex  = animMesh.TextureIndex;

                        morphMesh.NextMeshContainer = animMesh;
                        frame.MeshContainer         = morphMesh;

                        morphMesh.TweenFactor = 0.0f;
                    }
                    else
                    {
                        morphMesh = frame.MeshContainer as MorphMeshContainer;
                        List <xxVertex> vertexList = xxMesh.SubmeshList[meshObjIdx].VertexList;
                        VertexBuffer    vertBuffer = CreateMorphVertexBuffer(idxSet, keyframe, vertexList);
                        if (asStart)
                        {
                            if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                            {
                                morphMesh.StartBuffer.Dispose();
                            }
                            morphMesh.StartBuffer = vertBuffer;
                            morphMesh.TweenFactor = 0.0f;
                        }
                        else
                        {
                            if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                            {
                                morphMesh.EndBuffer.Dispose();
                            }
                            morphMesh.EndBuffer   = vertBuffer;
                            morphMesh.TweenFactor = 1.0f;
                        }
                    }
                    return(morphMesh.TweenFactor);
                }
            }
            Report.ReportLog("Mesh frame " + meshFrame + " not displayed.");
            return(-1f);
        }
        public float SetMorphKeyframe(SkinnedMeshRenderer sMesh, int keyframeIdx, bool asStart)
        {
            foreach (AnimationFrame frame in meshFrames)
            {
                Transform meshTransform = sMesh.m_GameObject.instance.FindLinkedComponent(UnityClassID.Transform);
                if (frame.Name == meshTransform.GetTransformPath())
                {
                    Mesh mesh = Operations.GetMesh(sMesh);
                    AnimationMeshContainer animMesh = frame.MeshContainer as AnimationMeshContainer;
                    if (animMesh != null)
                    {
                        MorphMeshContainer[] morphMeshes = new MorphMeshContainer[mesh.m_SubMeshes.Count];
                        int startVertexIdx = 0;
                        for (int meshObjIdx = 0; meshObjIdx < mesh.m_SubMeshes.Count; meshObjIdx++)
                        {
                            MorphMeshContainer morphMesh = new MorphMeshContainer();
                            morphMeshes[meshObjIdx] = morphMesh;
                            morphMesh.FaceCount = (int)mesh.m_SubMeshes[meshObjIdx].indexCount / 3;
                            morphMesh.IndexBuffer = animMesh.MeshData.Mesh.IndexBuffer;

                            morphMesh.VertexCount = (int)mesh.m_SubMeshes[meshObjIdx].vertexCount;
                            Operations.vMesh vMesh = new Operations.vMesh(sMesh, false, false);
                            List<Operations.vVertex> vertexList = vMesh.submeshes[meshObjIdx].vertexList;
                            VertexBuffer vertBuffer = CreateMorphVertexBuffer(mesh.m_Shapes, keyframeIdx, vertexList, startVertexIdx);
                            morphMesh.StartBuffer = morphMesh.EndBuffer = vertBuffer;

                            int vertBufferSize = morphMesh.VertexCount * Marshal.SizeOf(typeof(TweeningWithoutNormalsVertexBufferFormat.Stream2));
                            vertBuffer = new VertexBuffer(device, vertBufferSize, Usage.WriteOnly, VertexFormat.Texture1, Pool.Managed);
                            using (DataStream vertexStream = vertBuffer.Lock(0, vertBufferSize, LockFlags.None))
                            {
                                for (int i = 0; i < vertexList.Count; i++)
                                {
                                    Operations.vVertex vertex = vertexList[i];
                                    vertexStream.Write(vertex.uv[0]);
                                    vertexStream.Write(vertex.uv[1]);
                                }
                                vertBuffer.Unlock();
                            }
                            morphMesh.CommonBuffer = vertBuffer;

                            morphMesh.MaterialIndex = animMesh.MaterialIndex;
                            morphMesh.TextureIndex = animMesh.TextureIndex;

                            morphMesh.TweenFactor = 0.0f;

                            startVertexIdx += morphMesh.VertexCount;
                            animMesh = (AnimationMeshContainer)animMesh.NextMeshContainer;
                        }

                        for (int meshObjIdx = 0; meshObjIdx < mesh.m_SubMeshes.Count; meshObjIdx++)
                        {
                            morphMeshes[meshObjIdx].NextMeshContainer = meshObjIdx < mesh.m_SubMeshes.Count - 1
                                ? (MeshContainer)morphMeshes[meshObjIdx + 1] : frame.MeshContainer;
                        }
                        frame.MeshContainer = morphMeshes[0];
                        return 0;
                    }
                    else
                    {
                        MorphMeshContainer morphMesh = frame.MeshContainer as MorphMeshContainer;
                        Operations.vMesh vMesh = new Operations.vMesh(sMesh, false, false);
                        int startVertexIdx = 0;
                        for (int meshObjIdx = 0; meshObjIdx < mesh.m_SubMeshes.Count; meshObjIdx++)
                        {
                            List<Operations.vVertex> vertexList = vMesh.submeshes[meshObjIdx].vertexList;
                            VertexBuffer vertBuffer = CreateMorphVertexBuffer(mesh.m_Shapes, keyframeIdx, vertexList, startVertexIdx);
                            if (asStart)
                            {
                                if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                                {
                                    morphMesh.StartBuffer.Dispose();
                                }
                                morphMesh.StartBuffer = vertBuffer;
                                morphMesh.TweenFactor = 0.0f;
                            }
                            else
                            {
                                if (morphMesh.StartBuffer != morphMesh.EndBuffer)
                                {
                                    morphMesh.EndBuffer.Dispose();
                                }
                                morphMesh.EndBuffer = vertBuffer;
                                morphMesh.TweenFactor = 1.0f;
                            }

                            startVertexIdx += morphMesh.VertexCount;
                            morphMesh = morphMesh.NextMeshContainer as MorphMeshContainer;
                        }
                        return asStart ? 0 : 1;
                    }
                }
            }
            Report.ReportLog("Mesh frame " + sMesh.m_GameObject.instance.m_Name + " not displayed.");
            return -1f;
        }