Exemple #1
0
        /// <summary>
        /// Loads the texture.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="path">The path.</param>
        /// <param name="loadedCallback">The loaded callback.</param>
        /// <param name="loadingMode">The loading mode.</param>
        /// <param name="flags">The flags.</param>
        /// <returns></returns>
        private static T LoadTexture <T>(string path, string contentDir, TextureLoadedHandler loadedCallback, LoadingMode loadingMode, TextureFlags flags) where T : MyTexture
        {
            MyTexture texture;

            if (TexturesWithIgnoredQuality.Contains(Path.Combine(contentDir, path)))
            {
                flags |= TextureFlags.IgnoreQuality;
            }

            if (typeof(T) == typeof(MyTexture2D))
            {
                texture = new MyTexture2D(contentDir, path, GetLoadMethod(loadingMode), flags);
            }
            else if (typeof(T) == typeof(MyTextureCube))
            {
                texture = new MyTextureCube(contentDir, path, GetLoadMethod(loadingMode), flags);
            }
            else
            {
                throw new ArgumentException("Unsupported texture type", "T");
            }

            if (loadedCallback != null)
            {
                texture.TextureLoaded += loadedCallback;
            }

            switch (loadingMode)
            {
            case LoadingMode.Immediate:
            {
                if (!texture.Load(MyRenderConstants.RenderQualityProfile.TextureQuality, (flags & TextureFlags.CanBeMissing) > 0))
                {
                    return(null);
                }

                break;
            }

            case LoadingMode.Background:
            {
                LoadTextureInBackground(texture);
            }
            break;
            }

            lock (m_textures)
            {
                m_textures[Path.Combine(contentDir, path)] = texture;

                DbgWatchLoadedTextures();
            }

            return((T)texture);
        }
        public MyRenderMeshMaterial(string name, string contentDir, string materialName, MyTexture2D diff, MyTexture2D norm)
        {
            if (name != null)
            {
                m_diffuseName = name + MyRenderMesh.C_POSTFIX_DIFFUSE_EMISSIVE + ".dds";
                m_normalName = name + MyRenderMesh.C_POSTFIX_NORMAL_SPECULAR + ".dds";
            }

            m_contentDir = contentDir;
            m_materialName = materialName;
            m_drawTechnique = MyMeshDrawTechnique.MESH;
            HashCode = 0;
            m_diffuseTex = diff;
            m_normalTex = norm;
            m_hasNormalTexture = m_normalTex != null;

            ComputeHashCode();
        }
        public void Draw(MyVertexFormatDecal[] vertices, MyEffectDecals effect, MyTexture2D[] texturesDiffuse, MyTexture2D[] texturesNormalMap)
        {
            CheckIfBufferIsFull();

            //  SortForSAP buffers by texture
            m_sortTriangleBuffersByTexture.Clear();
            foreach (MyDecalsForRenderObjectsTriangleBuffer buffer in m_usedTriangleBuffers)
            {
                if (buffer.RenderObject.Visible == true)
                {
                    /*  todo drawdecals flag
                    if ((buffer.Entity == MyGuiScreenGamePlay.Static.ControlledEntity
                        || buffer.Entity.Parent == MyGuiScreenGamePlay.Static.ControlledEntity) && 
                        MyGuiScreenGamePlay.Static.IsFirstPersonView)
                    {
                        //  Don't draw decals if they are on an entity in which the camera is
                        continue;
                    } */

                    // Decal with "ExplosionSmut" texture is much larger, so it must be drawed to larger distance.
                    float fadeoutDistance = MyDecals.GetMaxDistanceForDrawingDecals();
                    //if (buffer.DecalTexture == MyDecalTexturesEnum.ExplosionSmut)
                      //  fadeoutDistance *= MyDecalsConstants.DISTANCE_MULTIPLIER_FOR_LARGE_DECALS;

                    //if (Vector3.Distance(MyCamera.m_initialSunWindPosition, buffer.PhysObject.GetPosition()) >= (MyDecals.GetMaxDistanceForDrawingDecals()))
                    //if (buffer.PhysObject.GetDistanceBetweenCameraAndBoundingSphere() >= MyDecals.GetMaxDistanceForDrawingDecals())
                    
                    /*if (buffer.RenderObject.GetDistanceBetweenCameraAndBoundingSphere() >= fadeoutDistance)
                    {
                        continue;
                    } */

                    m_sortTriangleBuffersByTexture.Add(buffer);
                }
            }            
            m_sortTriangleBuffersByTexture.Sort();
            
            //  Draw decals - sorted by texture
            MyDecalTexturesEnum? lastDecalTexture = null;
            for (int i = 0; i < m_sortTriangleBuffersByTexture.Count; i++)
            {
                MyDecalsForRenderObjectsTriangleBuffer buffer = m_sortTriangleBuffersByTexture[i];

                int trianglesCount = buffer.CopyDecalsToVertices(vertices);

                if (trianglesCount <= 0) continue;

                //  Switch texture only if different than previous one
                if ((lastDecalTexture == null) || (lastDecalTexture != buffer.DecalTexture))
                {
                    int textureIndex = (int)buffer.DecalTexture;
                    effect.SetDecalDiffuseTexture(texturesDiffuse[textureIndex]);
                    effect.SetDecalNormalMapTexture(texturesNormalMap[textureIndex]);
                    lastDecalTexture = buffer.DecalTexture;
                }

                //effect.SetWorldMatrix(buffer.Entity.WorldMatrix * Matrix.CreateTranslation(-MyCamera.Position));
                if (buffer.RenderObject is MyRenderTransformObject)
                    effect.SetWorldMatrix((Matrix)((MyRenderTransformObject)buffer.RenderObject).GetWorldMatrixForDraw());
                else
                    effect.SetWorldMatrix((Matrix)((MyManualCullableRenderObject)buffer.RenderObject).GetWorldMatrixForDraw());

                effect.SetViewProjectionMatrix(MyRenderCamera.ViewProjectionMatrixAtZero);

                // set FadeoutDistance
                float fadeoutDistance = MyDecals.GetMaxDistanceForDrawingDecals();
                //if (buffer.DecalTexture == MyDecalTexturesEnum.ExplosionSmut)
                  //  fadeoutDistance *= MyDecalsConstants.DISTANCE_MULTIPLIER_FOR_LARGE_DECALS;

                effect.SetFadeoutDistance(fadeoutDistance);

                effect.SetTechnique(MyEffectDecals.Technique.Model);

                MyRender.GraphicsDevice.VertexDeclaration = MyVertexFormatDecal.VertexDeclaration;

                effect.Begin();
                MyRender.GraphicsDevice.DrawUserPrimitives(PrimitiveType.TriangleList, 0, trianglesCount, vertices);
                effect.End();

                MyPerformanceCounter.PerCameraDrawWrite.DecalsForEntitiesInFrustum += trianglesCount;
            }
        }
        public void UpdateParameters(
            LightTypeEnum type,
            Vector3D position,
            int parentID,
            float offset,
            Color color,
            Color specularColor,
            float falloff,
            float range,
            float intensity,
            bool lightOn, 
            bool useInForwardRender,
            float reflectorIntensity,
            bool reflectorOn,
            Vector3 reflectorDirection,
            Vector3 reflectorUp,
            float reflectorConeMaxAngleCos,
            Color reflectorColor,
            float reflectorRange,
            float reflectorFalloff,
            string reflectorTexture,
            float shadowDistance,
            bool castShadows,
            bool glareOn,
            MyGlareTypeEnum glareType,
            float glareSize,
            float glareQuerySize,
            float glareIntensity,
            string glareMaterial,
            float glareMaxDistance
            )
        {
            m_lightType = type;
            GlareOn = glareOn;

            Position = position;
            m_parentID = parentID;

            m_color = color;
            m_specularColor = specularColor;
            m_falloff = falloff;
            m_range = range;
            m_intensity = intensity;
            m_lightOn = lightOn;
            UseInForwardRender = useInForwardRender;
            ReflectorIntensity = reflectorIntensity;
            ReflectorOn = reflectorOn;
            ReflectorDirection = reflectorDirection;
            ReflectorUp = reflectorUp;
            ReflectorConeMaxAngleCos = reflectorConeMaxAngleCos;
            ReflectorColor = reflectorColor;
            ReflectorRange = reflectorRange;
            ReflectorFalloff = reflectorFalloff;
            ShadowDistance = shadowDistance;
            CastShadows = castShadows;

            if (Glare != null)
            {
                Glare.GlareMaterial = glareMaterial;
                Glare.Type = glareType;
                Glare.Size = glareSize;
                System.Diagnostics.Debug.Assert(!GlareOn || (GlareOn && glareQuerySize > 0));
                Glare.QuerySize = glareQuerySize;
                Glare.Intensity = glareIntensity == -1 ? (float?)null : glareIntensity;
                Glare.MaxDistance = glareMaxDistance;
            }

            m_pointBoundingSphere.Radius = range;

            m_reflectorTexture = string.IsNullOrEmpty(reflectorTexture) ? null : MyTextureManager.GetTexture<MyTexture2D>(reflectorTexture);
            PointLightOffset = offset;

            UpdatePositionWithOffset();

            UpdateSpotParams();

        }
 public static void AssertTexture(MyTexture2D texture)
 {
     //AssertTextureDxtCompress(texture);
     //AssertTextureMipMapped(texture);
 }
 public static bool IsTextureMipMapped(MyTexture2D texture)
 {
     return texture.LevelCount > 1;
 }
 public static bool IsTextureDxtCompressed(MyTexture2D texture)
 {
     return texture.Format == Format.Dxt1 || texture.Format == Format.Dxt3 || texture.Format == Format.Dxt5;
 }
        public static void LoadTextureAtlas(string[] enumsToStrings, string textureDir, string atlasFile, out MyTexture2D texture, out MyAtlasTextureCoordinate[] textureCoords)
        {
            //MyTextureAtlas atlas = contentManager.Load<MyTextureAtlas>(atlasFile);

            MyTextureAtlas atlas = LoadTextureAtlas(textureDir, atlasFile);

            //  Here we define particle texture coordinates inside of texture atlas
            textureCoords = new MyAtlasTextureCoordinate[enumsToStrings.Length];

            texture = null;

            for (int i = 0; i < enumsToStrings.Length; i++)
            {
                MyTextureAtlasItem textureAtlasItem = atlas[enumsToStrings[i]];

                textureCoords[i] = new MyAtlasTextureCoordinate(new Vector2(textureAtlasItem.UVOffsets.X, textureAtlasItem.UVOffsets.Y), new Vector2(textureAtlasItem.UVOffsets.Z, textureAtlasItem.UVOffsets.W));

                //  Texture atlas content processor support having more DDS files for one atlas, but we don't want it (because we want to have all particles in one texture, so we can draw fast).
                //  So here we just take first and only texture.
                if (texture == null)
                {
                    texture = textureAtlasItem.AtlasTexture;
                }
            }
        }
 static void AssertTextureMipMapped(MyTexture2D texture)
 {
     //MyCommonDebugUtils.AssertRelease(IsTextureMipMapped(texture));
 }
 static void AssertTextureDxtCompress(MyTexture2D texture)
 {
     MyDebug.AssertRelease(IsTextureDxtCompressed(texture));
 }
 public static double GetTextureSizeInMb(MyTexture2D texture)
 {
     return CalculateTextureSizeInMb(texture.Format, texture.Width, texture.Height, texture.LevelCount);
 }
        //  IMPORTANT: This load content can be called only once in application - at the application start. Not for every game-play screen start.
        //  Reason is that OcclusionQuery will stop work if two or more instances are created, even if previous was Disposed. I don't know if 
        //  it's error or feature... I will just follow above rule.
        public override void LoadContent()
        {
            MyRender.Log.WriteLine("MySunGlare.LoadContent() - START");
            MyRender.Log.IncreaseIndent();
                          
            // Load the glow, ray and flare textures.
            m_glareSprite = MyTextureManager.GetTexture<MyTexture2D>("Textures\\SunGlare\\SunGlare.dds");
            m_glowSprite = MyTextureManager.GetTexture<MyTexture2D>("Textures\\SunGlare\\sun_glow_main.dds");
            m_glowSprite2 = MyTextureManager.GetTexture<MyTexture2D>("Textures\\SunGlare\\sun_glow_ray.dds");

            m_flares = new[] {
                new MyFlare(-0.5f, 0.7f, new Color( 30,  40,  50), "Textures\\SunGlare\\flare1.dds"),
                new MyFlare(0.3f, 0.4f, new Color(155, 165, 180), "Textures\\SunGlare\\flare1.dds"),
                new MyFlare(1.2f, 1.0f, new Color(40,  50,  50), "Textures\\SunGlare\\flare1.dds"),
                new MyFlare(1.5f, 1.5f, new Color( 80, 90,  100), "Textures\\SunGlare\\flare1.dds"),
                new MyFlare(-0.3f, 0.7f, new Color(140,  150,  160), "Textures\\SunGlare\\flare2.dds"),
                new MyFlare(0.6f, 0.9f, new Color( 85, 95,  100), "Textures\\SunGlare\\flare2.dds"),
                new MyFlare(0.7f, 0.4f, new Color( 130, 150, 170), "Textures\\SunGlare\\flare2.dds"),
                new MyFlare(-0.7f, 0.7f, new Color( 60, 60,  80), "Textures\\SunGlare\\flare3.dds"),
                new MyFlare(0.0f, 0.6f, new Color( 20,  25,  30), "Textures\\SunGlare\\flare3.dds"),
                new MyFlare(2.0f, 1.4f, new Color( 70,  85, 110), "Textures\\SunGlare\\flare3.dds"),
            };

            // Create a SpriteBatch for drawing the glow and flare sprites.
            m_spriteBatch = new SpriteBatch(MyRender.GraphicsDevice, "SunGlare");

            // Create the occlusion query object. But only first time! Then just reuse it.
            for (int i = 0; i < m_occlusionQueries.Length; i++)
            {
                m_occlusionQueries[i] = MyOcclusionQueries.Get();
                m_occlusionStates[i] = QueryState.IssueMeasure;
            }
            for (int i = 0; i < m_measurementOcclusionQueries.Length; i++)
            {
                m_measurementOcclusionQueries[i] = MyOcclusionQueries.Get();
            }

            MyRender.Log.DecreaseIndent();
            MyRender.Log.WriteLine("MySunGlare.LoadContent() - END");
        }