/// <summary>
        /// Reads a shape from a stream.
        /// </summary>
        /// <param name="stream">The stream to read from.</param>
        /// <returns>The new shape.</returns>
        public Shape ReadShape(Stream stream)
        {
            _shape = new Shape();

            BinaryReader bin = new BinaryReader(stream);
            int readVersion = bin.ReadInt32();
            int exporterVersion = readVersion >> 16;
            readVersion &= 0xFF;
            if (readVersion > Shape.WriteVersion || readVersion < 24)
            {
                Assert.Fatal(false, "TSShapeReader.ReadShape - Old shape formats are not supported.");
                return null;
            }

            Shape.ReadVersion = readVersion;

            int sizeMemBuffer, startushort, startU8;
            sizeMemBuffer = 4 * bin.ReadInt32();
            startushort = 4 * bin.ReadInt32();
            startU8 = 4 * bin.ReadInt32();

            byte[] buffer = bin.ReadBytes(sizeMemBuffer);

            ShapeStream shapeStream = new ShapeStream(buffer, 0, startushort, startU8, startushort, startU8 - startushort, sizeMemBuffer - startU8);

            // Read sequences
            int numSequences = bin.ReadInt32();
            _shape.Sequences = new Sequence[numSequences];
            for (int i = 0; i < numSequences; i++)
            {
                _shape.Sequences[i] = new Sequence();
                _ReadSequence(bin, ref _shape.Sequences[i], true);
            }

            // Read material list
            _ReadMaterialList(bin);

            _AssembleShape(shapeStream);

            _shape.Initialize();

            return _shape;
        }
 /// <summary>
 /// Initializes the shape reader for reading sequences (dsq files).
 /// </summary>
 /// <param name="shape">The shape to import the sequence into.</param>
 public ShapeReader(Shape shape)
 {
     _shape = shape;
 }
        /// <summary>
        /// Creates a shape instance for a specified shape.
        /// </summary>
        /// <param name="inShape">The shape to wrap in this instance.</param>
        /// <param name="loadMaterials"></param>
        public ShapeInstance(Shape inShape, bool loadMaterials)
        {
            _shape = inShape;

            _currentDetailLevel = 0;

            // Set up subtree data
            int ss = _shape.SubShapeFirstNode.Length;
            _dirtyFlags = new DirtyFlags[ss];

            // Set up node data
            int numNodes = _shape.Nodes.Length;
            _nodeTransforms = new Matrix[numNodes];

            // add objects to trees
            int numObjects = _shape.Objects.Length;
            _meshObjects = new ObjectInstance[numObjects];
            for (int i = 0; i < numObjects; i++)
            {
                _meshObjects[i].Object = _shape.Objects[i];
                _meshObjects[i].Visibility = 1.0f;
            }

            // initialize bitvectors
            _transitionRotationNodes = new BitVector();
            _transitionRotationNodes.SetSize(numNodes);
            _transitionTranslationNodes = new BitVector();
            _transitionTranslationNodes.SetSize(numNodes);
            _transitionScaleNodes = new BitVector();
            _transitionScaleNodes.SetSize(numNodes);
            _disableBlendNodes = new BitVector();
            _disableBlendNodes.SetSize(numNodes);
            _handsOffNodes = new BitVector();
            _handsOffNodes.SetSize(numNodes);

            // make sure we have a thread list
            _threadList = new List<Thread>();
            _transitionThreads = new List<Thread>();

            // construct ifl material objects
            _iflMaterialInstances = new IflMaterialInstance[_shape.IflMaterials.Length];
            for (int i = 0; i < _shape.IflMaterials.Length; i++)
            {
                _iflMaterialInstances[i].IflMaterial = _shape.IflMaterials[i];
                _iflMaterialInstances[i].Frame = -1;
            }

            if (loadMaterials)
                _SetMaterialList(_shape.MaterialList);

            _AnimateSubtrees(true);
        }