Advertisement
Guest User

DrawUserPrimitives culling parts of mesh

a guest
Mar 23rd, 2020
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.18 KB | None | 0 0
  1. public static RasterizerState MyRasterizerStateCullNoneSolid = new RasterizerState() { FillMode = FillMode.Solid, CullMode = CullMode.None };
  2.  
  3. public Chunk(int chunkX, int chunkY, int width, int height, int mapWidth, int mapHeight, float[] noiseMap, GraphicsDeviceManager graphics, PlanetTypes planetType) {
  4.  Width = width;
  5.  Height = height;
  6.  
  7.  ChunkY = chunkY;
  8.  
  9.  List < Vector3 > positions = new List < Vector3 > ();
  10.  List < Color > colors = new List < Color > ();
  11.  VertexPositionColor[] vertices;
  12.  
  13.  float pX = chunkX * width * innerRadius * 2;
  14.  float pY = chunkY * height * outerRadius * 3 / 2;
  15.  
  16.  for (int y = 0; y < height; y++) {
  17.   for (int x = 0; x < width; x++) {
  18.    float tx = pX + (x + y * 0.5 f - y / 2) * (innerRadius * 2 f);
  19.    float ty = pY + y * (outerRadius * 1.5 f);
  20.  
  21.    float value = noiseMap[(y + chunkY * height) * width * mapWidth + x + chunkX * width];
  22.  
  23.    int color = (int)(MathHelper.Clamp(value, 0.1 f, 0.9 f) * 255);
  24.  
  25.    Vector3 pos = new Vector3(tx, ty, value * 100);
  26.    for (int i = 0; i < 6; i++) {
  27.     Vector3 c1 = pos + corners[i];
  28.     Vector3 c2 = pos + corners[i + 1];
  29.  
  30.     if (i == 0 || i == 4 || i == 5) {
  31.      int ax = y % 2 == 0 ? AdjentsEven[i].Item1 : AdjentsOdd[i].Item1;
  32.      int ay = y % 2 == 0 ? AdjentsEven[i].Item2 : AdjentsOdd[i].Item2;
  33.      int nx = x + ax;
  34.      int ny = y + ay;
  35.  
  36.      float tx2 = pX + (nx + ny * 0.5 f - ny / 2) * (innerRadius * 2 f);
  37.      float ty2 = pY + ny * (outerRadius * 1.5 f);
  38.  
  39.      int noiseMapIndex = (ny + chunkY * height) * width * mapWidth + nx + chunkX * width;
  40.      if (noiseMapIndex >= 0 && noiseMapIndex < noiseMap.Length) {
  41.       float value2 = noiseMap[noiseMapIndex];
  42.       int color2 = (int)(MathHelper.Clamp(value2, 0.1 f, 0.9 f) * 255);
  43.  
  44.       Vector3 pos2 = new Vector3(tx2, ty2, value2 * 100);
  45.  
  46.       Vector3 c3 = pos2 + corners[(i + 4) % 6];
  47.       Vector3 c4 = pos2 + corners[(i + 3) % 6];
  48.  
  49.       positions.Add(c1);
  50.       positions.Add(c2);
  51.       positions.Add(c3);
  52.  
  53.       colors.Add(new Color(color, 125, 125));
  54.       colors.Add(new Color(color, 125, 125));
  55.       colors.Add(new Color(color2, 125, 125));
  56.  
  57.  
  58.       positions.Add(c4);
  59.       positions.Add(c3);
  60.       positions.Add(c2);
  61.  
  62.       colors.Add(new Color(color2, 125, 125));
  63.       colors.Add(new Color(color2, 125, 125));
  64.       colors.Add(new Color(color, 125, 125));
  65.      }
  66.  
  67.     }
  68.  
  69.     //if(y >= CHUNK_WIDTH/ 2)
  70.     //{
  71.     //    positions.Add(pos);
  72.     //    positions.Add(c2);
  73.     //    positions.Add(c1);
  74.  
  75.     //    colors.Add(new Color(color, 125, 125));
  76.     //    colors.Add(new Color(color, 125, 125));
  77.     //    colors.Add(new Color(color, 125, 125));
  78.     //}
  79.     //else
  80.     //{
  81.     positions.Add(c2);
  82.  
  83.     positions.Add(pos);
  84.  
  85.     positions.Add(c1);
  86.  
  87.     colors.Add(new Color(color, 225, 225));
  88.     colors.Add(new Color(color, 225, 225));
  89.     colors.Add(new Color(color, 225, 225));
  90.     //}
  91.    }
  92.   }
  93.  }
  94.  
  95.  //Vertices = new VertexBuffer(graphics.GraphicsDevice, typeof(VertexPositionColor), positions.Count, BufferUsage.None);
  96.  
  97.  //positions.Reverse();
  98.  //colors.Reverse();
  99.  Vertices = new VertexPositionColor[positions.Count];
  100.  
  101.  for (int i = 0; i < positions.Count; i++)
  102.   Vertices[i] = new VertexPositionColor(positions[i], colors[i]);
  103.  
  104.  BoundingBox = BoundingBox.CreateFromPoints(positions);
  105.  
  106.  testEffect = new BasicEffect(graphics.GraphicsDevice);
  107. }
  108.  
  109. public static void LoadContent(ContentManager content) {
  110.  effect = content.Load < Effect > ("Shaders/FogOfWar");
  111. }
  112.  
  113. public void Draw(SpriteBatch spriteBatch, GraphicsDeviceManager graphics, Camera camera) {
  114.  //if (camera.Frustum.Contains(BoundingBox) == ContainmentType.Disjoint)
  115.  //    return;
  116.  
  117.  Space.ChunksDrawn++;
  118.  
  119.  effect.Parameters["WorldViewProjection"].SetValue(camera.ViewMatrix * camera.ProjectionMatrix * Matrix.Identity);
  120.  
  121.  
  122.  graphics.GraphicsDevice.RasterizerState = MyRasterizerStateCullNoneSolid;
  123.  graphics.GraphicsDevice.DepthStencilState = DepthStencilState.Default;
  124.  
  125.  foreach(var pass in effect.CurrentTechnique.Passes) {
  126.  
  127.   pass.Apply();
  128.   graphics.GraphicsDevice.DrawUserPrimitives < VertexPositionColor > (
  129.    PrimitiveType.TriangleList,
  130.    Vertices,
  131.    0,
  132.    Width * Height * 6, VertexPositionColor.VertexDeclaration);
  133.  }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement