예제 #1
0
        protected void initialize(GroundMap groundMap, WeightsMap weights, ColorSurface normals)
        {
            GroundMap = groundMap;

            Debug.Assert((groundMap.Width%TerrainPlane.SquareSize) == 0 && (groundMap.Height%TerrainPlane.SquareSize) == 0);

            _position = World.TranslationVector;

            Textures[0] = Textures[0] ?? VContent.Load<Texture2D>("terraintextures/sand");
            Textures[1] = Textures[1] ?? VContent.Load<Texture2D>("terraintextures/sahara");
            Textures[2] = Textures[2] ?? VContent.Load<Texture2D>("terraintextures/grass");
            Textures[3] = Textures[3] ?? VContent.Load<Texture2D>("terraintextures/rock");
            Textures[4] = Textures[4] ?? VContent.Load<Texture2D>("terraintextures/snow");
            Textures[5] = Textures[5] ?? VContent.Load<Texture2D>("terraintextures/stones");
            Textures[6] = Textures[6] ?? VContent.Load<Texture2D>("terraintextures/dirtground");
            Textures[7] = Textures[7] ?? VContent.Load<Texture2D>("terraintextures/path");
            Textures[8] = Textures[8] ?? VContent.Load<Texture2D>("terraintextures/wheatfield");

            HeightsMap = groundMap.CreateHeightsTexture(Effect.GraphicsDevice);
            WeightsMap = weights.CreateTexture2D(Effect.GraphicsDevice);
            NormalsMap = normals.CreateTexture2D(Effect.GraphicsDevice);

            var slicesW = groundMap.Width/Side;
            var slicesH = groundMap.Height/Side;
            //TODO - this is wrong - I guess...
            var sliceFracX = 1f/slicesW;
            var sliceFracY = 1f/slicesH;
            _slices = new TerrainSlice[slicesW*slicesH];

            var gurka = Vector3.TransformNormal(new Vector3(HalfSide, 0, HalfSide), World);
            var radius = Math.Max(gurka.X, gurka.Z) * (float)Math.Sqrt(2);

            var i = 0;
            for (var y = 0; y < slicesH; y++)
                for (var x = 0; x < slicesW; x++)
                {
                    var world = Matrix.Translation(Side*x, 0, Side*y)*World;
                    _slices[i++] = new TerrainSlice
                    {
                        TexOffsetAndScale = new Vector4(x*sliceFracX, y*sliceFracY, sliceFracX, sliceFracY),
                        World = world,
                        BoundingSphere = new BoundingSphere(world.TranslationVector + gurka, radius)
                    };
                }
            BoundingSphere = new BoundingSphere(
                _position + new Vector3(groundMap.Width, 0, groundMap.Height)/2,
                (float) Math.Sqrt(groundMap.Width*groundMap.Width + groundMap.Height*groundMap.Height)/2);

            GroundExtentX = slicesW;
            GroundExtentZ = slicesH;
        }