Beispiel #1
0
        protected internal override TextureCube Read(ContentReader reader, TextureCube existingInstance)
        {
            TextureCube textureCube = null;

			SurfaceFormat surfaceFormat = (SurfaceFormat)reader.ReadInt32();
			int size = reader.ReadInt32();
			int levels = reader.ReadInt32();

            if (existingInstance == null)
                textureCube = new TextureCube(reader.GetGraphicsDevice(), size, levels > 1, surfaceFormat);
            else
                textureCube = existingInstance;

#if OPENGL
            Threading.BlockOnUIThread(() =>
            {
#endif
                for (int face = 0; face < 6; face++)
                {
                    for (int i = 0; i < levels; i++)
                    {
                        int faceSize = reader.ReadInt32();
                        byte[] faceData = ContentManager.ScratchBufferPool.Get(faceSize);
                        reader.Read(faceData, 0, faceSize);
                        textureCube.SetData<byte>((CubeMapFace)face, i, null, faceData, 0, faceSize);
                        ContentManager.ScratchBufferPool.Return(faceData);
                    }
                }
#if OPENGL
            });
#endif

             return textureCube;
        }
        protected internal override DualTextureEffect Read(ContentReader input, DualTextureEffect existingInstance)
        {
            DualTextureEffect effect = new DualTextureEffect(input.GetGraphicsDevice());

            effect.Texture            = input.ReadExternalReference <Texture>() as Texture2D;
            effect.Texture2           = input.ReadExternalReference <Texture>() as Texture2D;
            effect.DiffuseColor       = input.ReadVector3();
            effect.Alpha              = input.ReadSingle();
            effect.VertexColorEnabled = input.ReadBoolean();
            return(effect);
        }
        protected internal override AlphaTestEffect Read(ContentReader input, AlphaTestEffect existingInstance)
        {
            var effect = new AlphaTestEffect(input.GetGraphicsDevice());

            effect.Texture            = input.ReadExternalReference <Texture>() as Texture2D;
            effect.AlphaFunction      = (CompareFunction)input.ReadInt32();
            effect.ReferenceAlpha     = (int)input.ReadUInt32();
            effect.DiffuseColor       = input.ReadVector3();
            effect.Alpha              = input.ReadSingle();
            effect.VertexColorEnabled = input.ReadBoolean();
            return(effect);
        }
Beispiel #4
0
        protected internal override Effect Read(ContentReader input, Effect existingInstance)
        {
            int dataSize = input.ReadInt32();

            byte[] data = ContentManager.ScratchBufferPool.Get(dataSize);
            input.Read(data, 0, dataSize);
            var effect = new Effect(input.GetGraphicsDevice(), data, 0, dataSize);

            ContentManager.ScratchBufferPool.Return(data);
            effect.Name = input.AssetName;
            return(effect);
        }
Beispiel #5
0
        protected internal override SkinnedEffect Read(ContentReader input, SkinnedEffect existingInstance)
        {
            var effect = new SkinnedEffect(input.GetGraphicsDevice());

            effect.Texture          = input.ReadExternalReference <Texture> () as Texture2D;
            effect.WeightsPerVertex = input.ReadInt32();
            effect.DiffuseColor     = input.ReadVector3();
            effect.EmissiveColor    = input.ReadVector3();
            effect.SpecularColor    = input.ReadVector3();
            effect.SpecularPower    = input.ReadSingle();
            effect.Alpha            = input.ReadSingle();
            return(effect);
        }
Beispiel #6
0
        protected internal override EnvironmentMapEffect Read(ContentReader input, EnvironmentMapEffect existingInstance)
        {
            var effect = new EnvironmentMapEffect(input.GetGraphicsDevice());

            effect.Texture                = input.ReadExternalReference <Texture>() as Texture2D;
            effect.EnvironmentMap         = input.ReadExternalReference <TextureCube>() as TextureCube;
            effect.EnvironmentMapAmount   = input.ReadSingle();
            effect.EnvironmentMapSpecular = input.ReadVector3();
            effect.FresnelFactor          = input.ReadSingle();
            effect.DiffuseColor           = input.ReadVector3();
            effect.EmissiveColor          = input.ReadVector3();
            effect.Alpha = input.ReadSingle();
            return(effect);
        }
Beispiel #7
0
        protected internal override VertexBuffer Read(ContentReader input, VertexBuffer existingInstance)
        {
            var declaration = input.ReadRawObject <VertexDeclaration>();
            var vertexCount = (int)input.ReadUInt32();
            int dataSize    = vertexCount * declaration.VertexStride;

            byte[] data = ContentManager.ScratchBufferPool.Get(dataSize);
            input.Read(data, 0, dataSize);

            var buffer = new VertexBuffer(input.GetGraphicsDevice(), declaration, vertexCount, BufferUsage.None);

            buffer.SetData(data, 0, dataSize);
            ContentManager.ScratchBufferPool.Return(data);
            return(buffer);
        }
Beispiel #8
0
        protected internal override BasicEffect Read(ContentReader input, BasicEffect existingInstance)
        {
            var effect  = new BasicEffect(input.GetGraphicsDevice());
            var texture = input.ReadExternalReference <Texture>() as Texture2D;

            if (texture != null)
            {
                effect.Texture        = texture;
                effect.TextureEnabled = true;
            }

            effect.DiffuseColor       = input.ReadVector3();
            effect.EmissiveColor      = input.ReadVector3();
            effect.SpecularColor      = input.ReadVector3();
            effect.SpecularPower      = input.ReadSingle();
            effect.Alpha              = input.ReadSingle();
            effect.VertexColorEnabled = input.ReadBoolean();
            return(effect);
        }
Beispiel #9
0
        protected internal override IndexBuffer Read(ContentReader input, IndexBuffer existingInstance)
        {
            IndexBuffer indexBuffer = existingInstance;

            bool sixteenBits = input.ReadBoolean();
            int  dataSize    = input.ReadInt32();

            if (indexBuffer == null)
            {
                indexBuffer = new IndexBuffer(input.GetGraphicsDevice(),
                                              sixteenBits ? IndexElementSize.SixteenBits : IndexElementSize.ThirtyTwoBits,
                                              dataSize / (sixteenBits ? 2 : 4), BufferUsage.None);
            }

            byte[] data = ContentManager.ScratchBufferPool.Get(dataSize);
            input.Read(data, 0, dataSize);
            indexBuffer.SetData(data, 0, dataSize);
            ContentManager.ScratchBufferPool.Return(data);

            return(indexBuffer);
        }
Beispiel #10
0
        protected internal override Texture3D Read(ContentReader reader, Texture3D existingInstance)
        {
            Texture3D texture = null;

            SurfaceFormat format = (SurfaceFormat)reader.ReadInt32();
            int width = reader.ReadInt32();
            int height = reader.ReadInt32();
            int depth = reader.ReadInt32();
            int levelCount = reader.ReadInt32();

            if (existingInstance == null)
                texture = new Texture3D(reader.GetGraphicsDevice(), width, height, depth, levelCount > 1, format);
            else
                texture = existingInstance;

#if OPENGL
            Threading.BlockOnUIThread(() =>
            {
#endif
                for (int i = 0; i < levelCount; i++)
                {
                    int dataSize = reader.ReadInt32();
                    byte[] data = ContentManager.ScratchBufferPool.Get(dataSize);
                    reader.Read(data, 0, dataSize);
                    texture.SetData(i, 0, 0, width, height, 0, depth, data, 0, dataSize);

                    // Calculate dimensions of next mip level.
                    width = Math.Max(width >> 1, 1);
                    height = Math.Max(height >> 1, 1);
                    depth = Math.Max(depth >> 1, 1);

                    ContentManager.ScratchBufferPool.Return(data);
                }
#if OPENGL
            });
#endif

            return texture;
        }
Beispiel #11
0
        protected internal override Texture2D Read(ContentReader reader, Texture2D existingInstance)
		{
			Texture2D texture = null;

            var surfaceFormat = (SurfaceFormat)reader.ReadInt32();
            int width = reader.ReadInt32();
            int height = reader.ReadInt32();
            int levelCount = reader.ReadInt32();
            int levelCountOutput = levelCount;

            // If the system does not fully support Power of Two textures,
            // skip any mip maps supplied with any non PoT textures.
            if (levelCount > 1 && !reader.GetGraphicsDevice().GraphicsCapabilities.SupportsNonPowerOfTwo &&
                (!MathHelper.IsPowerOfTwo(width) || !MathHelper.IsPowerOfTwo(height)))
            {
                levelCountOutput = 1;
                System.Diagnostics.Debug.WriteLine(
                    "Device does not support non Power of Two textures. Skipping mipmaps.");
            }

			SurfaceFormat convertedFormat = surfaceFormat;
			switch (surfaceFormat)
			{
				case SurfaceFormat.Dxt1:
				case SurfaceFormat.Dxt1a:
					if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsDxt1)
						convertedFormat = SurfaceFormat.Color;
					break;
				case SurfaceFormat.Dxt1SRgb:
					if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsDxt1)
						convertedFormat = SurfaceFormat.ColorSRgb;
					break;
				case SurfaceFormat.Dxt3:
				case SurfaceFormat.Dxt5:
					if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsS3tc)
						convertedFormat = SurfaceFormat.Color;
					break;
				case SurfaceFormat.Dxt3SRgb:
				case SurfaceFormat.Dxt5SRgb:
					if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsS3tc)
						convertedFormat = SurfaceFormat.ColorSRgb;
					break;
				case SurfaceFormat.NormalizedByte4:
					convertedFormat = SurfaceFormat.Color;
					break;
			}
			
            texture = existingInstance ?? new Texture2D(reader.GetGraphicsDevice(), width, height, levelCountOutput > 1, convertedFormat);
#if OPENGL
            Threading.BlockOnUIThread(() =>
            {
#endif
                for (int level = 0; level < levelCount; level++)
			    {
				    var levelDataSizeInBytes = reader.ReadInt32();
                    var levelData = ContentManager.ScratchBufferPool.Get(levelDataSizeInBytes);
                    reader.Read(levelData, 0, levelDataSizeInBytes);
                    int levelWidth = Math.Max(width >> level, 1);
                    int levelHeight = Math.Max(height >> level, 1);

                    if (level >= levelCountOutput)
                        continue;

				    //Convert the image data if required
				    switch (surfaceFormat)
				    {
					    case SurfaceFormat.Dxt1:
                        case SurfaceFormat.Dxt1SRgb:
                        case SurfaceFormat.Dxt1a:
				            if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsDxt1 && convertedFormat == SurfaceFormat.Color)
				            {
				                levelData = DxtUtil.DecompressDxt1(levelData, levelWidth, levelHeight);
				                levelDataSizeInBytes = levelData.Length;
				            }
				            break;
					    case SurfaceFormat.Dxt3:
					    case SurfaceFormat.Dxt3SRgb:
                            if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsS3tc)
				                if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsS3tc &&
				                    convertedFormat == SurfaceFormat.Color)
				                {
				                    levelData = DxtUtil.DecompressDxt3(levelData, levelWidth, levelHeight);
                                    levelDataSizeInBytes = levelData.Length;
                                }
				            break;
					    case SurfaceFormat.Dxt5:
					    case SurfaceFormat.Dxt5SRgb:
                            if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsS3tc)
				                if (!reader.GetGraphicsDevice().GraphicsCapabilities.SupportsS3tc &&
				                    convertedFormat == SurfaceFormat.Color)
				                {
				                    levelData = DxtUtil.DecompressDxt5(levelData, levelWidth, levelHeight);
                                    levelDataSizeInBytes = levelData.Length;
                                }
				            break;
                        case SurfaceFormat.Bgra5551:
                            {
#if OPENGL
                                // Shift the channels to suit OpenGL
                                int offset = 0;
                                for (int y = 0; y < levelHeight; y++)
                                {
                                    for (int x = 0; x < levelWidth; x++)
                                    {
                                        ushort pixel = BitConverter.ToUInt16(levelData, offset);
                                        pixel = (ushort)(((pixel & 0x7FFF) << 1) | ((pixel & 0x8000) >> 15));
                                        levelData[offset] = (byte)(pixel);
                                        levelData[offset + 1] = (byte)(pixel >> 8);
                                        offset += 2;
                                    }
                                }
#endif
                            }
                            break;
					    case SurfaceFormat.Bgra4444:
						    {
#if OPENGL
                                // Shift the channels to suit OpenGL
							    int offset = 0;
							    for (int y = 0; y < levelHeight; y++)
							    {
								    for (int x = 0; x < levelWidth; x++)
								    {
									    ushort pixel = BitConverter.ToUInt16(levelData, offset);
									    pixel = (ushort)(((pixel & 0x0FFF) << 4) | ((pixel & 0xF000) >> 12));
									    levelData[offset] = (byte)(pixel);
									    levelData[offset + 1] = (byte)(pixel >> 8);
									    offset += 2;
								    }
							    }
#endif
						    }
						    break;
					    case SurfaceFormat.NormalizedByte4:
						    {
							    int bytesPerPixel = surfaceFormat.GetSize();
							    int pitch = levelWidth * bytesPerPixel;
							    for (int y = 0; y < levelHeight; y++)
							    {
								    for (int x = 0; x < levelWidth; x++)
								    {
									    int color = BitConverter.ToInt32(levelData, y * pitch + x * bytesPerPixel);
									    levelData[y * pitch + x * 4] = (byte)(((color >> 16) & 0xff)); //R:=W
									    levelData[y * pitch + x * 4 + 1] = (byte)(((color >> 8) & 0xff)); //G:=V
									    levelData[y * pitch + x * 4 + 2] = (byte)(((color) & 0xff)); //B:=U
									    levelData[y * pitch + x * 4 + 3] = (byte)(((color >> 24) & 0xff)); //A:=Q
								    }
							    }
						    }
						    break;
				    }
				
                    texture.SetData(level, null, levelData, 0, levelDataSizeInBytes);
                    ContentManager.ScratchBufferPool.Return(levelData);
			    }
#if OPENGL
            });
#endif
        			
			return texture;
		}
Beispiel #12
0
        protected internal override Model Read(ContentReader reader, Model existingInstance)
        {
            // Read the bone names and transforms.
            uint boneCount = reader.ReadUInt32();
            //Debug.WriteLine("Bone count: {0}", boneCount);

            List <ModelBone> bones = new List <ModelBone>((int)boneCount);

            for (uint i = 0; i < boneCount; i++)
            {
                string name   = reader.ReadObject <string>();
                var    matrix = reader.ReadMatrix();
                var    bone   = new ModelBone {
                    Transform = matrix, Index = (int)i, Name = name
                };
                bones.Add(bone);
            }

            // Read the bone hierarchy.
            for (int i = 0; i < boneCount; i++)
            {
                var bone = bones[i];

                //Debug.WriteLine("Bone {0} hierarchy:", i);

                // Read the parent bone reference.
                //Debug.WriteLine("Parent: ");
                var parentIndex = ReadBoneReference(reader, boneCount);

                if (parentIndex != -1)
                {
                    bone.Parent = bones[parentIndex];
                }

                // Read the child bone references.
                uint childCount = reader.ReadUInt32();

                if (childCount != 0)
                {
                    //Debug.WriteLine("Children:");

                    for (uint j = 0; j < childCount; j++)
                    {
                        var childIndex = ReadBoneReference(reader, boneCount);
                        if (childIndex != -1)
                        {
                            bone.AddChild(bones[childIndex]);
                        }
                    }
                }
            }

            List <ModelMesh> meshes = new List <ModelMesh>();

            //// Read the mesh data.
            int meshCount = reader.ReadInt32();

            //Debug.WriteLine("Mesh count: {0}", meshCount);

            for (int i = 0; i < meshCount; i++)
            {
                //Debug.WriteLine("Mesh {0}", i);
                string name            = reader.ReadObject <string>();
                var    parentBoneIndex = ReadBoneReference(reader, boneCount);
                var    boundingSphere  = reader.ReadBoundingSphere();

                // Tag
                var meshTag = reader.ReadObject <object>();

                // Read the mesh part data.
                int partCount = reader.ReadInt32();
                //Debug.WriteLine("Mesh part count: {0}", partCount);

                List <ModelMeshPart> parts = new List <ModelMeshPart>(partCount);

                for (uint j = 0; j < partCount; j++)
                {
                    ModelMeshPart part;
                    if (existingInstance != null)
                    {
                        part = existingInstance.Meshes[i].MeshParts[(int)j];
                    }
                    else
                    {
                        part = new ModelMeshPart();
                    }

                    part.VertexOffset   = reader.ReadInt32();
                    part.NumVertices    = reader.ReadInt32();
                    part.StartIndex     = reader.ReadInt32();
                    part.PrimitiveCount = reader.ReadInt32();

                    // tag
                    part.Tag = reader.ReadObject <object>();

                    parts.Add(part);

                    int jj = (int)j;
                    reader.ReadSharedResource <VertexBuffer>(delegate(VertexBuffer v)
                    {
                        parts[jj].VertexBuffer = v;
                    });
                    reader.ReadSharedResource <IndexBuffer>(delegate(IndexBuffer v)
                    {
                        parts[jj].IndexBuffer = v;
                    });
                    reader.ReadSharedResource <Effect>(delegate(Effect v)
                    {
                        parts[jj].Effect = v;
                    });
                }

                if (existingInstance != null)
                {
                    continue;
                }

                ModelMesh mesh = new ModelMesh(reader.GetGraphicsDevice(), parts);

                // Tag reassignment
                mesh.Tag = meshTag;

                mesh.Name       = name;
                mesh.ParentBone = bones[parentBoneIndex];
                mesh.ParentBone.AddMesh(mesh);
                mesh.BoundingSphere = boundingSphere;
                meshes.Add(mesh);
            }

            if (existingInstance != null)
            {
                // Read past remaining data and return existing instance
                ReadBoneReference(reader, boneCount);
                reader.ReadObject <object>();
                return(existingInstance);
            }

            // Read the final pieces of model data.
            var rootBoneIndex = ReadBoneReference(reader, boneCount);

            Model model = new Model(reader.GetGraphicsDevice(), bones, meshes);

            model.Root = bones[rootBoneIndex];

            model.BuildHierarchy();

            // Tag?
            model.Tag = reader.ReadObject <object>();

            return(model);
        }