private void SaveX(TVMesh tvm, string fileName) { ExtendedMaterial[] exMaterials = new ExtendedMaterial[tvm.GetGroupCount()]; Material dxMaterial; int idx = 0; for (int group = 0; group < tvm.GetGroupCount(); group++) { idx = tvm.GetMaterial(group); dxMaterial = new Material(); dxMaterial.AmbientColor = ToDx(core.MaterialFactory.GetAmbient(idx)); dxMaterial.DiffuseColor = ToDx(core.MaterialFactory.GetDiffuse(idx)); dxMaterial.EmissiveColor = ToDx(core.MaterialFactory.GetEmissive(idx)); dxMaterial.SpecularColor = ToDx(core.MaterialFactory.GetSpecular(idx)); dxMaterial.SpecularSharpness = core.MaterialFactory.GetPower(idx); exMaterials[group].Material3D = dxMaterial; // Get the Texture Filename. idx = tvm.GetTexture(group); // Add em to the array. exMaterials[group].TextureFilename = core.TextureFactory.GetTextureInfo(idx).Name; } // Save the Mesh. Microsoft.DirectX.Direct3D.Mesh dxMesh = new Microsoft.DirectX.Direct3D.Mesh(core.InternalObjects.GetD3DMesh(tvm.GetIndex())); int[] adjacency = new int[dxMesh.NumberFaces * 3]; dxMesh.GenerateAdjacency(0f, adjacency); dxMesh.Save(fileName, adjacency, exMaterials, XFileFormat.Text); }
public MeshObject(D3D.Device dev, D3D.Mesh mesh, D3D.Material[] materials, D3D.Texture[] textures) { //this._owner = dev; this._mesh = mesh; this._materials = materials; this._textures = textures; }
public static void GetBoundingBox(D3D.Mesh mesh, float scale, out DX.Vector3 min, out DX.Vector3 max) { D3D.VertexBuffer verts = mesh.VertexBuffer; DX.GraphicsStream stream = verts.Lock(0, 0, D3D.LockFlags.None); D3D.Geometry.ComputeBoundingBox(stream, mesh.NumberVertices, mesh.VertexFormat, out min, out max); verts.Unlock(); stream = null; verts = null; }
public static float GetBoundingRadius(D3D.Mesh mesh, float scale) { D3D.VertexBuffer verts = mesh.VertexBuffer; DX.GraphicsStream stream = verts.Lock(0, 0, D3D.LockFlags.None); DX.Vector3 meshCenter; float radius = D3D.Geometry.ComputeBoundingSphere(stream, mesh.NumberVertices, mesh.VertexFormat, out meshCenter) * scale; verts.Unlock(); stream = null; verts = null; return(radius); }
public GooAccess(Direct3D.Device NewGameDevice, GameEngine.ShaderLevel NewCardShaderLevel, DirectX.Matrix NewViewMatrix, DirectX.Matrix NewProjectionMatrix) { GameDevice = NewGameDevice; CardShaderLevel = NewCardShaderLevel; ViewMatrix = NewViewMatrix; ProjectionMatrix = NewProjectionMatrix; // Load Shader Effect From File GooEffect = Direct3D.Effect.FromFile(GameDevice, GameConfig.Files.GooFx, null, null, Direct3D.ShaderFlags.None, null); // Choose shader technique based on shader level. if (CardShaderLevel == GameEngine.ShaderLevel.Pixel_3_0) { GooEffect.Technique = "Goo_Parallax_3_0"; } else if (CardShaderLevel == GameEngine.ShaderLevel.Pixel_2_b) { GooEffect.Technique = "Goo_Parallax_2_b"; } else if (CardShaderLevel == GameEngine.ShaderLevel.Pixel_2_0) { GooEffect.Technique = "Goo_Parallax_2"; } else if (CardShaderLevel == GameEngine.ShaderLevel.Pixel_1_4) { GooEffect.Technique = "Goo_Bump_1_4"; } // Load Mesh From File GooMesh = Direct3D.Mesh.FromFile(GameConfig.Files.GooMesh, Direct3D.MeshFlags.Managed, GameDevice); // Load Textures From File ColorTexture = Direct3D.TextureLoader.FromFile(GameDevice, GameConfig.Files.GooColor); NormalTexture = Direct3D.TextureLoader.FromFile(GameDevice, GameConfig.Files.GooNormal); HeightTexture = Direct3D.TextureLoader.FromFile(GameDevice, GameConfig.Files.GooHeight); // Load Textures into Effect GooEffect.SetValue("ColorTexture", ColorTexture); GooEffect.SetValue("NormalsTexture", NormalTexture); GooEffect.SetValue("HeightTexture", HeightTexture); // Set Parallax and Bump Intensity GooEffect.SetValue("ParallaxAmount", .5f); GooEffect.SetValue("BumpAmount", 1.5f); }
public static void LoadMesh(Stream stream, D3D.Device dev, ref D3D.Mesh mesh, ref D3D.Material[] meshMaterials, ref D3D.Texture[] meshTextures) { D3D.ExtendedMaterial[] materialArray; mesh = D3D.Mesh.FromStream(stream, D3D.MeshFlags.Managed, dev, out materialArray); if ((materialArray != null) && (materialArray.Length > 0)) { meshMaterials = new D3D.Material[materialArray.Length]; meshTextures = new D3D.Texture[materialArray.Length]; for (int i = 0; i < materialArray.Length; i++) { meshMaterials[i] = materialArray[i].Material3D; if (!string.IsNullOrEmpty(materialArray[i].TextureFilename)) { meshTextures[i] = D3D.TextureLoader.FromFile(dev, materialArray[i].TextureFilename); } } } }
private void OnDeviceLost(object sender, EventArgs e ) { if(m_d3dMesh != null) { m_d3dMesh.Dispose(); m_d3dMesh = null; } }
protected virtual void DrawRigidBody(AnimatTools.Forms.BodyPlan.Command.enumCommandMode cmCommand) { if(this.NeedToCreateBody) //if the mesh is null create it with the loaded information CreateBody(); //If a mesh was created then draw it. if(m_d3dMesh != null && this.Device != null) { this.Device.Transform.World = CombinedTransformationMatrix * Device.Transform.World1; //if we are in SelectJoint mode set the transparent material and set selected to false if(cmCommand == AnimatTools.Forms.BodyPlan.Command.enumCommandMode.SelectJoints || cmCommand == AnimatTools.Forms.BodyPlan.Command.enumCommandMode.SelectReceptiveFields) { this.Device.Material = m_d3dTransparentMaterial; m_bSelected = false; if(m_d3dTexture != null) m_d3dDevice.SetTexture(0, m_d3dAlphaTexture); } //otherwise set the material to default material else { this.Device.Material = m_d3dMaterial; if(m_d3dTexture != null) m_d3dDevice.SetTexture(0, m_d3dTexture); } //try to draw the mesh try { m_d3dMesh.DrawSubset(0); //draws the object m_d3dDevice.SetTexture(0, null); } //if we fail..see if the mesh has been disposed..if it has...dispose of the mesh and recreate it catch(Exception ex) { try { if(m_d3dMesh.Disposed) { m_d3dMesh.Dispose(); m_d3dMesh = null; this.DrawRigidBody(cmCommand); } } catch(Exception ex2) {} } } }
/// <summary> /// Creates all the bodies associated with this body part /// </summary> protected override void CreateBody() { //dispose of any previous bodies this.CleanUp(); //used to maked the selection box a little bigger than the joint float fltScale = (this.MaxDimension * 3.0f); if(fltScale > 0.3f) fltScale = 0.3f; //create the min and max movement cylinders //m_mshMin = Util_DX.CreateCylinder(this.Device, m_fRadius, m_fRadius, this.MinMovement, 16); //m_mshMax = Util_DX.CreateCylinder(this.Device, m_fRadius, m_fRadius, this.MaxMovement, 16); m_mshMin = Mesh.Cylinder(this.Device, m_fRadius, m_fRadius, Math.Abs(this.MinMovement), 16, 16); m_mshMax = Mesh.Cylinder(this.Device, m_fRadius, m_fRadius, this.MaxMovement, 16, 16); //create the box used as "stoppers" m_mshConstraint = Util_DX.CreateBox(this.Device,3 * m_fRadius, 3 * m_fRadius, 3 * m_fRadius); //Create the selected bounding box m_mshSelectedBoundingBox = Mesh.Box(Device, (float)((m_fRadius) + fltScale),(float)((m_fRadius) + fltScale), (float)((-1 * MinMovement) + MaxMovement + fltScale)); RefreshSelectionBoxSizes(); base.CreateBody(); }
public WallAccess(Direct3D.Device NewGameDevice, GameEngine.ShaderLevel NewCardShaderLevel, DirectX.Matrix NewViewMatrix, DirectX.Matrix NewProjectionMatrix) { GameDevice = NewGameDevice; CardShaderLevel = NewCardShaderLevel; ViewMatrix = NewViewMatrix; ProjectionMatrix = NewProjectionMatrix; // Load Shader Effects From Files WallEffect = Direct3D.Effect.FromFile(GameDevice, GameConfig.Files.WallFx, null, null, Direct3D.ShaderFlags.None, null); WallEffect.Technique = "WallLight_1_1"; PipeEffect = Direct3D.Effect.FromFile(GameDevice, GameConfig.Files.PipeFx, null, null, Direct3D.ShaderFlags.None, null); PipeEffect.Technique = "PipeLight_1_1"; // Load Mesh From File WallMesh = Direct3D.Mesh.FromFile(GameConfig.Files.WallMesh, Direct3D.MeshFlags.Managed, GameDevice, out WallMtrl); PipeMesh = Direct3D.Mesh.FromFile(GameConfig.Files.PipeMesh, Direct3D.MeshFlags.Managed, GameDevice, out PipeMtrl); // Load Wall Textures if ((WallMtrl != null) && (WallMtrl.Length > 0)) { WallMaterials = new Direct3D.Material[WallMtrl.Length]; WallTextures = new Direct3D.Texture[WallMtrl.Length]; for (int i = 0; i < WallMtrl.Length; i++) { WallMaterials[i] = WallMtrl[i].Material3D; if ((WallMtrl[i].TextureFilename != null) && (WallMtrl[i].TextureFilename != string.Empty)) { WallTextures[i] = Direct3D.TextureLoader.FromFile(GameDevice, @"..\..\Resources\" + WallMtrl[i].TextureFilename); } } } // Load Pipe Textures if ((PipeMtrl != null) && (PipeMtrl.Length > 0)) { PipeMaterials = new Direct3D.Material[PipeMtrl.Length]; PipeTextures = new Direct3D.Texture[PipeMtrl.Length]; for (int i = 0; i < PipeMtrl.Length; i++) { PipeMaterials[i] = PipeMtrl[i].Material3D; if ((PipeMtrl[i].TextureFilename != null) && (PipeMtrl[i].TextureFilename != string.Empty)) { PipeTextures[i] = Direct3D.TextureLoader.FromFile(GameDevice, @"..\..\Resources\" + PipeMtrl[i].TextureFilename); } } } // Set wall mesh location WallWorldMatrix = DirectX.Matrix.RotationYawPitchRoll(3.12f, 0.0f, 0.0f) * DirectX.Matrix.Translation(15, -75, -425); PipeWorldMatrix = DirectX.Matrix.RotationYawPitchRoll(3.20f, -0.1f, 0.0f) * DirectX.Matrix.Translation(-145, 15, -375); // Set Wall Shader Parameters DirectX.Matrix WorldViewProjMatrix = WallWorldMatrix * ViewMatrix * ProjectionMatrix; WallEffect.SetValue("WorldViewProj", WorldViewProjMatrix); WallEffect.SetValue("WorldMatrix", WallWorldMatrix); WallEffect.SetValue("DiffuseDirection", new DirectX.Vector4(1.0f, 1.0f, 1.0f, 0.0f)); // Set Pipe Shader Parameters WorldViewProjMatrix = PipeWorldMatrix * ViewMatrix * ProjectionMatrix; PipeEffect.SetValue("WorldViewProj", WorldViewProjMatrix); PipeEffect.SetValue("WorldMatrix", PipeWorldMatrix); PipeEffect.SetValue("DiffuseDirection", new DirectX.Vector4(1.0f, 1.0f, 1.0f, 0.0f)); }
public static void GetBoundingBox(D3D.Mesh mesh, out DX.Vector3 min, out DX.Vector3 max) { MeshObject.GetBoundingBox(mesh, 1.0f, out min, out max); }
public static float GetBoundingRadius(D3D.Mesh mesh) { return(MeshObject.GetBoundingRadius(mesh, 1.0f)); }
//*************************************************************************** // Static Methods // public static void LoadMesh(string filename, D3D.Device dev, ref D3D.Mesh mesh, ref D3D.Material[] meshMaterials, ref D3D.Texture[] meshTextures) { using (FileStream stream = new FileStream(filename, FileMode.Open, FileAccess.Read)) MeshObject.LoadMesh(stream, dev, ref mesh, ref meshMaterials, ref meshTextures); }
public void ImportWavefrontObject(WavefrontObject Wavefront, CompressionBounds boundingBox) { List<D3DVertex> temp = new List<D3DVertex>(Wavefront.FaceCount * 3); for (int Material = 0; Material < Wavefront.MaterialCount; Material++) { int[] FaceIndices = Wavefront.GetFaceIndicesUsingMaterialID(Material); for (int Face = 0; Face < FaceIndices.Length; Face++) { for (int Component = 0; Component < 3; Component++) { D3DVertex d3DVertex = new D3DVertex(); d3DVertex.Position = Wavefront.Vertices[Wavefront.Faces[FaceIndices[Face]].VertexIndices[Component]]; d3DVertex.Texture = Wavefront.Texcoords[Wavefront.Faces[FaceIndices[Face]].TexcoordIndices[Component]]; d3DVertex.Normal = Wavefront.Normals[Wavefront.Faces[FaceIndices[Face]].NormalIndices[Component]]; temp.Add(d3DVertex); } } } //Hashtable D3DVertexHashtable = new Hashtable(Wavefront.FaceCount * 3); List<short> tempIndices = new List<short>(Wavefront.FaceCount * 3); List<D3DVertex> D3DVertexList = new List<D3DVertex>(Wavefront.FaceCount * 3); short IndiceIndex = 0; for (int Index = 0; Index < temp.Count; Index++) { D3DVertex d3DVertex = temp[Index]; if (!D3DVertexList.Contains(d3DVertex)) { //D3DVertexHashtable.Add(d3DVertex, d3DVertex); D3DVertexList.Add(d3DVertex); tempIndices.Add(IndiceIndex); IndiceIndex++; } else { tempIndices.Add((short)D3DVertexList.IndexOf(d3DVertex)); } } this.Indices = tempIndices.ToArray(); D3DVertex[] D3DVertices = D3DVertexList.ToArray(); RenderDevice Device = new RenderDevice(); Device.InitializeDevice(); Microsoft.DirectX.Direct3D.Mesh mesh = new Microsoft.DirectX.Direct3D.Mesh(Wavefront.FaceCount, D3DVertices.Length, MeshFlags.SystemMemory, D3DVertex.Format, Device.Device); List<int> newAttributes = new List<int>(Wavefront.FaceCount); foreach (Face f in Wavefront.Faces) { newAttributes.Add(f.MaterialID); } mesh.LockAttributeBuffer(LockFlags.None); mesh.UnlockAttributeBuffer(newAttributes.ToArray()); mesh.SetIndexBufferData(Indices.ToArray(), LockFlags.None); mesh.SetVertexBufferData(D3DVertices.ToArray(), LockFlags.None); int[] adj = new int[Wavefront.FaceCount * 3]; mesh.GenerateAdjacency(0.005F, adj); mesh.OptimizeInPlace(MeshFlags.OptimizeAttributeSort, adj); IndexBuffer iBuffer = mesh.IndexBuffer; short[] D3DIndices; int IndiceCount; short[][] MaterialFaceIndices = new short[Wavefront.MaterialCount][]; for (int Material = 0; Material < Wavefront.MaterialCount; Material++) { iBuffer = Microsoft.DirectX.Direct3D.Mesh.ConvertMeshSubsetToSingleStrip(mesh, Material, MeshFlags.SystemMemory, out IndiceCount); GraphicsStream graphics = iBuffer.Lock(0, 0, LockFlags.None); unsafe { short* IndiceArray = (short*)graphics.InternalData.ToPointer(); D3DIndices = new short[IndiceCount]; for (int Index = 0; Index < IndiceCount; Index++) { D3DIndices[Index] = IndiceArray[Index]; } } MaterialFaceIndices[Material] = D3DIndices; } List<short> newIndices = new List<short>(); Groups = new Group[MaterialFaceIndices.Length]; for (int i = 0; i < MaterialFaceIndices.Length; i++) { Groups[i] = new Group(); Groups[i].IndiceStart = (short)newIndices.Count; Groups[i].IndiceCount = (short)MaterialFaceIndices[i].Length; Groups[i].ShaderIndex = (short)i; newIndices.AddRange(MaterialFaceIndices[i]); } this.Indices = newIndices.ToArray(); this._Vertices = new Vector3[D3DVertices.Length]; this.Texcoords = new Vector2[D3DVertices.Length]; this.Normals = new Vector3[D3DVertices.Length]; for (int i = 0; i < D3DVertices.Length; i++) { _Vertices[i] = D3DVertices[i].Position; Texcoords[i] = D3DVertices[i].Texture; Normals[i] = D3DVertices[i].Normal; } CalculateTangentArray(_Vertices.Length, _Vertices, Normals, Texcoords, mesh.NumberFaces, Wavefront.Faces.ToArray(), out Bitangents, out Tangents); mesh.Dispose(); }
protected override void CreateBody() { this.CleanUp(); this.m_d3dMesh = Mesh.Sphere(this.Device,m_fRadius,32,32); this.m_mshPole = Mesh.Cylinder(this.Device,(0.5f * m_fRadius),(0.5f * m_fRadius), 4 * m_fRadius, 32, 32); this.m_mshBox = Mesh.Box(this.Device,2 * m_fRadius,(0.5f * m_fRadius), 4 * m_fRadius); this.m_mshConstraint = Mesh.Box(this.Device,2 * m_fRadius,(0.125f * m_fRadius), 4 * m_fRadius); this.m_mshSelectedBoundingBox = Mesh.Box(this.Device,5 * m_fRadius, 5 * m_fRadius, 5 * m_fRadius); RefreshSelectionBoxSizes(); base.CreateBody(); m_vrConstraint = new DxHingeConstraintRef(this); }
public MeshObject(D3D.Device dev, D3D.Mesh mesh) : this(dev, mesh, new D3D.Material[0], new D3D.Texture[0]) { }