Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        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;
                }
            }
        }