예제 #1
0
        /*
        void InitializeMaterial()
        {

            terrainMaterial.SetTexture(0, climate.BaseMapAtlas);
            terrainMaterial.SetTexture(1, climate.NormalMapAtlas);
            TextureResource blendTex = new TextureResource();
            blendTex.SetTexture(TextureResourceType.Texture3D, blendTexture);
            TextureResource blendIDTex = new TextureResource();
            blendIDTex.SetTexture(TextureResourceType.Texture3D, blendIDTexture);
            terrainMaterial.SetTexture(2, blendTex);
            terrainMaterial.SetTexture(3, blendIDTex);
            terrainMaterial.kAmbient = climate.GetInverseResolution();
            terrainMaterial.kDiffuse = Vector3.One / new Vector3(blendMapWidth, blendMapHeight, blendMapDepth);

        }
        */
        void InitializeClimateMap()
        {
            Texture3D densityFieldTexture = new Texture3D(GFX.Device, DensityFieldWidth, DensityFieldHeight, DensityFieldDepth, 1, TextureUsage.None, GFX.Inst.ByteSurfaceFormat);
            CopyDensityTextureData(ref DensityField, densityFieldTexture);

            GFX.Device.Textures[0] = densityFieldTexture;

            RenderTarget2D rtClimateMap = new RenderTarget2D(GFX.Device, blendMapWidth, blendMapHeight, 1, SurfaceFormat.Color);
            RenderTarget2D rtIDMap = new RenderTarget2D(GFX.Device, blendMapWidth, blendMapHeight, 1, SurfaceFormat.Color);

            int stride = blendMapWidth * blendMapHeight;
            Color[] colorData = new Color[stride * blendMapDepth];
            Color[] blendIDData = new Color[stride * blendMapDepth];

            Vector4[] climateParams = new Vector4[4];
            Vector4[] climateParams2 = new Vector4[4];

            for (int i = 0; i < climate.heightCoeffs.Length; i++)
            {
                climateParams[i] = new Vector4(climate.heightCoeffs[i], climate.gradientCoeffs[i], climate.curvatureCoeffs[i], climate.baseScores[i]);
                climateParams2[i] = Vector4.One * climate.blendZones[i];
            }

            GFX.Device.SetPixelShaderConstant(0, Vector3.One / new Vector3(DensityFieldWidth, DensityFieldHeight, DensityFieldDepth));

            RenderTarget2D rtGradientMap = new RenderTarget2D(GFX.Device, DensityFieldWidth, DensityFieldHeight, 1, SurfaceFormat.Single);
            int gradStride = DensityFieldWidth * DensityFieldHeight;
            float[] gradientValues = new float[gradStride * DensityFieldDepth];
            Texture3D gradientTexture = new Texture3D(GFX.Device, DensityFieldWidth, DensityFieldHeight, DensityFieldDepth, 1, TextureUsage.None, SurfaceFormat.Single);
            Shader gradientShader = ResourceManager.Inst.GetShader("GradientShader");
            gradientShader.SetupShader();

            for (int z = 0; z < DensityFieldDepth; z++)
            {
                Vector4 depth = Vector4.One * (float)z / (float)(DensityFieldDepth - 1);
                GFX.Device.SetVertexShaderConstant(0, depth); //Set our current depth

                GFX.Device.SetRenderTarget(0, rtGradientMap);

                GFXPrimitives.Quad.Render();

                GFX.Device.SetRenderTarget(0, null);

                rtGradientMap.GetTexture().GetData<float>(gradientValues, z * gradStride, gradStride);
            }
            gradientTexture.SetData<float>(gradientValues);

            GFX.Device.Textures[1] = gradientTexture;
            GFX.Device.SetPixelShaderConstant(1, climateParams);
            GFX.Device.SetPixelShaderConstant(5, climateParams2);

            Shader climateShader = ResourceManager.Inst.GetShader("ClimateShader");
            climateShader.SetupShader();

            for (int z = 0; z < blendMapDepth; z++)
            {
                Vector4 depth = Vector4.One * (float)z / (float)(blendMapDepth - 1);
                GFX.Device.SetVertexShaderConstant(0, depth); //Set our current depth

                GFX.Device.SetRenderTarget(0, rtClimateMap);
                GFX.Device.SetRenderTarget(1, rtIDMap);

                GFXPrimitives.Quad.Render();

                GFX.Device.SetRenderTarget(0, null);
                GFX.Device.SetRenderTarget(1, null);

                rtClimateMap.GetTexture().GetData<Color>(colorData, z * stride, stride);
                rtIDMap.GetTexture().GetData<Color>(blendIDData, z * stride, stride);
            }
            GFX.Device.Textures[0] = null;
            GFX.Device.Textures[1] = null;

            gradientTexture.Dispose();

            blendTexture = new Texture3D(GFX.Device, blendMapWidth, blendMapHeight, blendMapDepth, 1, TextureUsage.None, SurfaceFormat.Color);
            blendTexture.SetData<Color>(colorData);

            blendIDTexture = new Texture3D(GFX.Device, blendMapWidth, blendMapHeight, blendMapDepth, 1, TextureUsage.None, SurfaceFormat.Color);
            blendIDTexture.SetData<Color>(blendIDData);

            blendTexture.Save("TestClimateMap.dds", ImageFileFormat.Dds);
            blendIDTexture.Save("TestClimateMapID.dds", ImageFileFormat.Dds);
        }
        void Create3DTextures(TextureResource[] baseMaps, TextureResource[] normalMaps)
        {
            Texture2D simpleWhite = new Texture2D(GFX.Device, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
            Texture2D simpleNormal = new Texture2D(GFX.Device, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
            Color[] whiteColor = new Color[1] { Color.White };
            Color[] normalColor = new Color[1] { Color.Blue };
            simpleWhite.SetData<Color>(whiteColor);
            simpleNormal.SetData<Color>(normalColor);

            List<Texture2D> baseTextures = new List<Texture2D>(TerrainClimateVoxels.MAX_BLEND_ZONES);
            List<Texture2D> normalTextures = new List<Texture2D>(TerrainClimateVoxels.MAX_BLEND_ZONES);
            int maxBaseSize = 256;
            int maxNormalSize = 256;

            for (int i = 0; i < TerrainClimate.MAX_BLEND_ZONES; i++)
            {
                if(baseMaps[i] != null)
                {
                    Texture2D baseMap = (Texture2D)baseMaps[i].GetTexture();
                    maxBaseSize = Math.Max(maxBaseSize, baseMap.Width);
                    baseTextures.Add(baseMap);
                }
                else
                    baseTextures.Add(simpleWhite);
                if(normalMaps[i] != null)
                {
                    Texture2D normalMap = (Texture2D)normalMaps[i].GetTexture();
                    maxNormalSize = Math.Max(maxNormalSize, normalMap.Width);
                    normalTextures.Add(normalMap);
                }
                else
                    normalTextures.Add(simpleNormal);
            }

            Texture3D baseAtlas = new Texture3D(GFX.Device, maxBaseSize, maxBaseSize, TerrainClimateVoxels.MAX_BLEND_ZONES, 1, TextureUsage.None, SurfaceFormat.Color);
            Texture3D normalAtlas = new Texture3D(GFX.Device, maxNormalSize, maxNormalSize, TerrainClimateVoxels.MAX_BLEND_ZONES, 1, TextureUsage.None, SurfaceFormat.Color);

            width = maxBaseSize;
            height = maxBaseSize;
            depth = TerrainClimate.MAX_BLEND_ZONES;

            PopulateAtlas(baseTextures.ToArray(), baseAtlas);

            PopulateAtlas(normalTextures.ToArray(), normalAtlas);

            BaseMapAtlas = new TextureResource();
            BaseMapAtlas.SetTexture(TextureResourceType.Texture3D, baseAtlas);

            NormalMapAtlas = new TextureResource();
            NormalMapAtlas.SetTexture(TextureResourceType.Texture3D, normalAtlas);

            baseAtlas.Save("BaseMapAtlas.dds", ImageFileFormat.Dds);

            normalAtlas.Save("NormalMapAtlas.dds", ImageFileFormat.Dds);
        }