/// <summary> /// Called when a basic effect is encountered and potentially replaced by /// BasicPaletteEffect (if not overridden). This is called afer effects have been processed. /// </summary> /// <param name="skinningType">The the skinning type of the meshpart.</param> /// <param name="meshPart">The MeshPart that contains the BasicMaterialContent.</param> protected virtual void ReplaceBasicEffect(SkinningType skinningType, ModelMeshPartContent meshPart) { BasicMaterialContent basic = meshPart.Material as BasicMaterialContent; if (basic != null) { // Create a new PaletteSourceCode object and set its palette size // based on the platform since xbox has fewer registers. PaletteSourceCode source; if (context.TargetPlatform != TargetPlatform.Xbox360) { source = new PaletteSourceCode(56); } else { source = new PaletteSourceCode(40); } // Process the material and set the meshPart material to the new // material. PaletteInfoProcessor processor = new PaletteInfoProcessor(); meshPart.Material = processor.Process( new PaletteInfo(source.SourceCode4BonesPerVertex, source.PALETTE_SIZE, basic), context); } }
private void Proccess(ModelMeshPartContent part) { var vertexData = part.VertexBuffer.VertexData; object indexData; int indexSize = part.IndexBuffer.Count; if (part.VertexBuffer.VertexData.Length < short.MaxValue) { short[] indexData16 = new short[indexSize]; for (int i = 0; i < indexSize; i++) { indexData16[i] = (short)part.IndexBuffer[i]; } indexData = indexData16; } else { int[] indexData32 = new int[indexSize]; part.IndexBuffer.CopyTo(indexData32, 0); indexData = indexData32; } object[] tag; tag = new object[] { vertexData, indexData }; part.Tag = tag; }
public DynamicModelMeshPartContent(ModelMeshPartContent source) { this.Source = source; this.VertexOffset = source.VertexOffset; this.NumVertices = source.NumVertices; this.StartIndex = source.StartIndex; this.PrimitiveCount = source.PrimitiveCount; this.VertexBuffer = source.VertexBuffer; this.IndexBuffer = source.IndexBuffer; this.Material = source.Material; this.Tag = Tag; }
public void AddMesh(ModelMeshPartContent mmpc) { // build a look-up table for channel in to channel out VertexElement[] ves = mmpc.GetVertexDeclaration(); int[] dstOffset = new int[ves.Length]; ReadConversion[] converter = new ReadConversion[ves.Length]; int stride = 0; for (int i = 0, n = ves.Length; i != n; ++i) { dstOffset[i] = MatchComponent(ves[i], out converter[i], ref stride); } object colorValue; if (!mmpc.Material.OpaqueData.TryGetValue("Diffuse", out colorValue) || colorValue == null || !(colorValue is Color)) { materialColor_ = Color.White; } else { materialColor_ = (Color)colorValue; } // walk the primitives and build triangle data for (int i = 0, n = mmpc.PrimitiveCount; i != n; ++i) { // walk vertex declaration for (int q = 0, m = ves.Length; q != m; ++q) { if (dstOffset[q] >= 0) { int ix, bytePos; ix = indexBufferData_[0 + i * 3 + mmpc.StartIndex]; bytePos = ix * stride + mmpc.BaseVertex + ves[q].Offset; v0[dstOffset[q]] = converter[q](vertexBufferData_, bytePos); ix = indexBufferData_[1 + i * 3 + mmpc.StartIndex]; bytePos = ix * stride + mmpc.BaseVertex + ves[q].Offset; v1[dstOffset[q]] = converter[q](vertexBufferData_, bytePos); ix = indexBufferData_[2 + i * 3 + mmpc.StartIndex]; bytePos = ix * stride + mmpc.BaseVertex + ves[q].Offset; v2[dstOffset[q]] = converter[q](vertexBufferData_, bytePos); } } // add triangle AddTriangle(); } }
public DynamicModelMeshPartContent(ModelMeshPartContent source) { Source = source; VertexBuffer = new DynamicVertexBufferContent(source.VertexBuffer); IndexBuffer = new DynamicIndexBufferContent(source.IndexBuffer); }
public override ModelContent Process(SceneContent input, ContentProcessorContext context) { List <ModelMeshContent> meshes = new List <ModelMeshContent>(); VertexBufferContent vertexBuffer = new VertexBufferContent(new VertexAttribute[] { new PositionAttribute(), new NormalAttribute(), new TextureCoordinateAttribute(), }); if (Skin) { vertexBuffer.AddAttribute(new BlendWeightsAttribute()); vertexBuffer.AddAttribute(new BlendIndicesAttribute()); } BoneContent rootBone = BuildSkeleton(input.Scene.RootNode, null); MaterialContent[] materials = new MaterialContent[input.Scene.MaterialCount]; for (int i = 0; i < input.Scene.MaterialCount; i++) { Dictionary <string, object> parameters = new Dictionary <string, object>(); GetMaterialParameters(input.Scene.Materials[i], parameters, context); string materialFactory; if (!MaterialTypeFactoryReplacements.TryGetValue(i, out materialFactory)) { materialFactory = DefaultMaterialTypeFactory; } MaterialContent material = context.ContentManager.MaterialFactoryManager.GetMaterialFactory(materialFactory)(context, parameters); material.Name = input.Scene.Materials[i].HasName ? input.Scene.Materials[i].Name : string.Format("{0}_Material_{1}", input.Name, i); materials[i] = material; } uint numVertices = 0; uint numIndices = 0; for (int i = 0; i < input.Scene.MeshCount; i++) { Mesh mesh = input.Scene.Meshes[i]; uint primitiveCount = (uint)mesh.FaceCount; uint baseVertex = numVertices; uint baseIndex = numIndices; uint partNumIndices = (uint)mesh.FaceCount * 3; uint partNumVertices = (uint)mesh.VertexCount; numVertices += (uint)mesh.VertexCount; numIndices += (uint)mesh.FaceCount * 3; Dictionary <uint, List <VertexWeight> > boneVertWeights = new Dictionary <uint, List <VertexWeight> >(); if (Skin) { ExtractBoneWeights(mesh, boneVertWeights); } for (int j = 0; j < mesh.VertexCount; j++) { vertexBuffer[Minotaur.Graphics.VertexUsage.Position].AddValues(mesh, j); vertexBuffer[Minotaur.Graphics.VertexUsage.Normal].AddValues(mesh, j); vertexBuffer[Minotaur.Graphics.VertexUsage.TextureCoordinate].AddValues(mesh, j); if (Skin) { float[] weights = new float[4]; byte[] indices = new byte[4]; GetNormalizedBoneWeights(boneVertWeights[(uint)j], weights, indices); ((BlendWeightsAttribute)vertexBuffer[Minotaur.Graphics.VertexUsage.BlendWeight]).AddValues(weights); ((BlendIndicesAttribute)vertexBuffer[Minotaur.Graphics.VertexUsage.BlendIndices]).AddValues(indices); } } IndexCollection indecies = new IndexCollection(); indecies.AddRange(mesh.GetIndices().Select(k => k + (uint)baseVertex)); ModelMeshPartContent part = new ModelMeshPartContent(vertexBuffer, indecies, baseVertex, partNumVertices, baseIndex, partNumIndices, primitiveCount); Minotaur.Core.BoundingSphere boundingSphere = Minotaur.Core.BoundingSphere.CreateFromPoints( ((PositionAttribute)vertexBuffer[Minotaur.Graphics.VertexUsage.Position]).Values.Select( p => new OpenTK.Vector3(p.X, p.Y, p.Z))); ModelMeshContent modelmesh = new ModelMeshContent(mesh.Name, GetMeshParentBone(input.Scene.RootNode, i), new[] { part }, boundingSphere); meshes.Add(modelmesh); part.Material = materials[mesh.MaterialIndex]; } List <ExternalReferenceContent <BoneAnimationsContent> > animations = new List <ExternalReferenceContent <BoneAnimationsContent> >(); foreach (Animation anim in input.Scene.Animations) { if (ExportAnimations.Count == 0 || ExportAnimations.Contains(anim.Name)) { animations.Add(context.ContentManager.BuildContent <BoneAnimationsContent>(input.Name, "ModelImporter", processorName: "BoneAnimProcessor", processorData: new Dictionary <string, object>() { { "AnimationName", anim.Name }, { "NamePre", string.Format("{0}_", Path.GetFileNameWithoutExtension(input.Name)) }, }, ignoreBuildItem: true )); } } return(new ModelContent(rootBone, _bones, meshes, animations)); }