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 override void Initialize() { mesh = Scene.CreateMeshBuilder(); switch (Helpers.GetFileFormat(FileName)) { case Helpers.FileFormat.TVM: mesh.LoadTVM(FileName, true, false); break; case Helpers.FileFormat.X: mesh.LoadXFile(FileName, true, false); break; } if (Visible) { mesh.EnableFrustumCulling(true, true); mesh.ComputeNormals(); mesh.ComputeBoundings(); mesh.ComputeOctree(); mesh.SetAlphaTest(true); mesh.SetCullMode(CONST_TV_CULLING.TV_BACK_CULL); mesh.SetBlendingMode(CONST_TV_BLENDINGMODE.TV_BLEND_ALPHA); mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED); mesh.SetShadowCast(true, true); mesh.SetPosition(Position.x, Position.y, Position.z); mesh.SetRotation(Rotation.x, Rotation.y, Rotation.z); mesh.SetScale(Scale.x, Scale.y, Scale.z); // Physics SetPhysics(); } else { // Hide mesh. for (var i = 0; i < mesh.GetGroupCount(); i++) { mesh.SetGroupEnable(i, false); } } // Register object in Lua. ScriptManager.SetGlobal(Name, this); }
private void LoadTextures() { //mesh.SetShadowCast(true, true); var lightMode = LightMode.Managed; for (var i = 0; i < mesh.GetGroupCount(); i++) { var textureInfo = TextureFactory.GetTextureInfo(mesh.GetTexture(i)); // Skip if there is no texture. if (textureInfo.Filename.Equals(textureInfo.Name)) { break; } var normalTexture = string.Empty; var normalTextureName = string.Empty; var specularTexture = string.Empty; var specularTextureName = string.Empty; var heightTexture = string.Empty; var heightTextureName = string.Empty; var texture = textureInfo.Filename; var fileInfo = new FileInfo(textureInfo.Filename); normalTextureName = fileInfo.Name.Replace(fileInfo.Extension, Constants.TextureNormalSuffix + fileInfo.Extension); normalTexture = Directory.GetParent(textureInfo.Filename) + @"\" + normalTextureName; heightTextureName = fileInfo.Name.Replace(fileInfo.Extension, Constants.TextureHeightSuffix + fileInfo.Extension); heightTexture = Directory.GetParent(textureInfo.Filename) + @"\" + heightTextureName; specularTextureName = fileInfo.Name.Replace(fileInfo.Extension, Constants.TextureSpecularSuffix + fileInfo.Extension); specularTexture = Directory.GetParent(textureInfo.Filename) + @"\" + specularTextureName; //mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_BASETEXTURE, Globals.GetTex(textureInfo.Name), i); if (File.Exists(normalTexture)) { var normalId = TextureFactory.LoadTexture(normalTexture, normalTextureName); mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_NORMALMAP, normalId, i); if (lightMode != LightMode.Offset) { lightMode = LightMode.Normal; } if (File.Exists(specularTexture)) { var alphaId = TextureFactory.LoadTexture(specularTexture); var specularId = TextureFactory.AddAlphaChannel(normalId, alphaId, specularTexture); mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_SPECULARMAP, specularId, i); } } if (File.Exists(heightTexture)) { var heightId = TextureFactory.LoadTexture(heightTexture); mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_HEIGHTMAP, heightId, i); lightMode = LightMode.Offset; } } switch (lightMode) { case LightMode.Normal: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE); break; case LightMode.Offset: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_OFFSETBUMPMAPPING_TANGENTSPACE); break; case LightMode.Managed: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED); break; default: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_NONE); break; } // Setup material TV_COLOR ambient = new TV_COLOR(0.25f, 0.25f, 0.25f, 1f); TV_COLOR diffuse = new TV_COLOR(1f, 1f, 1f, 1f); TV_COLOR specular = new TV_COLOR(0.35f, 0.35f, 0.35f, 1f); TV_COLOR emissive = new TV_COLOR(1f, 1f, 1f, 1f); float power = 100; materialIdx = MaterialFactory.CreateMaterial(); MaterialFactory.SetAmbient(materialIdx, ambient.r, ambient.g, ambient.b, ambient.a); MaterialFactory.SetDiffuse(materialIdx, diffuse.r, diffuse.g, diffuse.b, diffuse.a); MaterialFactory.SetSpecular(materialIdx, specular.r, specular.g, specular.b, specular.a); MaterialFactory.SetEmissive(materialIdx, emissive.r, emissive.g, emissive.b, emissive.a); MaterialFactory.SetPower(materialIdx, power); mesh.SetMaterial(materialIdx); }
private void LoadTextures() { if (!settings.UseAdvanceTexturing) { // Set default texture for mesh with no textures. for (int i = 0; i < mesh.GetGroupCount(); i++) { TV_TEXTURE textureInfo = Core.TextureFactory.GetTextureInfo(mesh.GetTexture(i)); if (textureInfo.Name.Equals(Helpers.TEXTURE_BLANK)) { SetDefaultTexture(mesh, i); } } } else if (!enableLightning) { mesh.SetMaterial(0); mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_LIGHTMAP, lightmapIdx); mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED); mesh.SetShadowCast(false, false); } else { mesh.SetShadowCast(true, true); var lightMode = LightMode.None; int defaultNormalTexId = -1; int defaultSpecularTexId = -1; for (var i = 0; i < mesh.GetGroupCount(); i++) { var textureInfo = Core.TextureFactory.GetTextureInfo(mesh.GetTexture(i)); var normalTexture = string.Empty; var normalTextureName = string.Empty; var specularTexture = string.Empty; var specularTextureName = string.Empty; var heightTexture = string.Empty; var heightTextureName = string.Empty; if (textureInfo.Name.Equals(Helpers.TEXTURE_BLANK)) { Helpers.SetTextureFromResource(core, mesh, Resources.defaultTexture, i); } else if (Helpers.IsTextureFromMemory(textureInfo.Filename)) { mesh.SetTexture(Core.TextureFactory.LoadTexture(textureInfo.Name), i); } else { var texture = textureInfo.Filename; var fileInfo = new FileInfo(textureInfo.Filename); normalTextureName = fileInfo.Name.Replace(fileInfo.Extension, settings.TextureNormalSuffix + fileInfo.Extension); normalTexture = Directory.GetParent(textureInfo.Filename) + @"\" + normalTextureName; heightTextureName = fileInfo.Name.Replace(fileInfo.Extension, settings.TextureHeightSuffix + fileInfo.Extension); heightTexture = Directory.GetParent(textureInfo.Filename) + @"\" + heightTextureName; specularTextureName = fileInfo.Name.Replace(fileInfo.Extension, settings.TextureSpecularSuffix + fileInfo.Extension); specularTexture = Directory.GetParent(textureInfo.Filename) + @"\" + specularTextureName; //mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_BASETEXTURE, core.Globals.GetTex(textureInfo.Name), i); } var normalId = -1; if (File.Exists(normalTexture)) { normalId = core.TextureFactory.LoadTexture(normalTexture, normalTextureName); } else { if (defaultNormalTexId == -1) { normalId = Helpers.LoadTextureFromResourceToMemory(core, Resources.defaultNormal); defaultNormalTexId = normalId; } else { normalId = defaultNormalTexId; } } mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_NORMALMAP, normalId, i); if (lightMode != LightMode.Offset) { lightMode = LightMode.Normal; } #region Set specular texture // http://www.truevision3d.com/forums/empty-t19109.0.html;msg131232#msg131232 var specularId = -1; if (File.Exists(specularTexture)) { specularId = Core.TextureFactory.AddAlphaChannel(normalId, Core.TextureFactory.LoadTexture(specularTexture), specularTexture); } else { if (defaultSpecularTexId == -1) { defaultSpecularTexId = Helpers.LoadTextureFromResourceToMemory(core, Resources.defaultSpecular); } specularId = Core.TextureFactory.AddAlphaChannel(normalId, defaultSpecularTexId, "defaultSpecular"); } mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_SPECULARMAP, specularId, i); #endregion if (File.Exists(heightTexture)) { mesh.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_HEIGHTMAP, Core.TextureFactory.LoadTexture(heightTexture), i); lightMode = LightMode.Offset; } if (core.MaterialIdx == -1) { TV_COLOR ambient = new TV_COLOR(0.25f, 0.25f, 0.25f, 1f); TV_COLOR diffuse = new TV_COLOR(1f, 1f, 1f, 1f); TV_COLOR specular = new TV_COLOR(0.35f, 0.35f, 0.35f, 1f); TV_COLOR emissive = new TV_COLOR(1f, 1f, 1f, 1f); float power = 100; core.MaterialIdx = Core.MaterialFactory.CreateMaterial(Guid.NewGuid().ToString()); core.MaterialFactory.SetAmbient(core.MaterialIdx, ambient.r, ambient.g, ambient.b, ambient.a); core.MaterialFactory.SetDiffuse(core.MaterialIdx, diffuse.r, diffuse.g, diffuse.b, diffuse.a); core.MaterialFactory.SetSpecular(core.MaterialIdx, specular.r, specular.g, specular.b, specular.a); core.MaterialFactory.SetEmissive(core.MaterialIdx, emissive.r, emissive.g, emissive.b, emissive.a); core.MaterialFactory.SetPower(core.MaterialIdx, power); } mesh.SetMaterial(core.MaterialIdx, i); } switch (lightMode) { case LightMode.Normal: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE); break; case LightMode.Offset: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_OFFSETBUMPMAPPING_TANGENTSPACE); break; case LightMode.Managed: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED); break; default: mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_NONE); break; } } }
public override void Initialize() { mesh = Scene.CreateMeshBuilder(); switch (Helpers.GetFileFormat(FileName)) { case Helpers.FileFormat.TVM: mesh.LoadTVM(FileName, true, false); break; case Helpers.FileFormat.X: mesh.LoadXFile(FileName, true, false); break; } if (Visible) { mesh.EnableFrustumCulling(true, true); mesh.ComputeNormals(); mesh.ComputeBoundings(); mesh.ComputeOctree(); mesh.SetAlphaTest(true); mesh.SetCullMode(CONST_TV_CULLING.TV_BACK_CULL); mesh.SetBlendingMode(CONST_TV_BLENDINGMODE.TV_BLEND_ALPHA); mesh.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_MANAGED); mesh.SetShadowCast(true, true); mesh.SetPosition(Position.x, Position.y, Position.z); mesh.SetRotation(Rotation.x, Rotation.y, Rotation.z); mesh.SetScale(Scale.x, Scale.y, Scale.z); // Physics SetPhysics(); } else { // Hide mesh. for (var i = 0; i < mesh.GetGroupCount(); i++) mesh.SetGroupEnable(i, false); } // Register object in Lua. ScriptManager.SetGlobal(Name, this); }