public override void OnWorldIntitialize(World world) { for (int i = 1; i < 16; ++i) { TerrainCorner corners = ((i >> 0) % 2 == 1 ? TerrainCorner.FrontLeft : TerrainCorner.None) | ((i >> 1) % 2 == 1 ? TerrainCorner.FrontRight : TerrainCorner.None) | ((i >> 2) % 2 == 1 ? TerrainCorner.BackLeft : TerrainCorner.None) | ((i >> 3) % 2 == 1 ? TerrainCorner.BackRight : TerrainCorner.None); Face solidFaces = Face.Bottom | ((corners & TerrainCorner.All) == TerrainCorner.All ? Face.Top : Face.None) | ((corners & TerrainCorner.Left) == TerrainCorner.Left ? Face.Left : Face.None) | ((corners & TerrainCorner.Front) == TerrainCorner.Front ? Face.Front : Face.None) | ((corners & TerrainCorner.Right) == TerrainCorner.Right ? Face.Right : Face.None) | ((corners & TerrainCorner.Back) == TerrainCorner.Back ? Face.Back : Face.None); BlockType sand = BlockManager.Get("MarsMiner_Sand", i - 1); sand.SetComponant(new VisibilityBComponant(true, solidFaces)); sand.SetComponant(new ModelBComponant(GeometryModel.Terrain(corners, "images_blocks_sand", "images_blocks_sandtri", "images_blocks_sand", "images_blocks_sandtri", "images_blocks_sand"))); BlockType rock = BlockManager.Get("MarsMiner_Rock", i - 1); rock.SetComponant(new VisibilityBComponant(true, solidFaces)); rock.SetComponant(new ModelBComponant(GeometryModel.Terrain(corners, "images_blocks_rock", "images_blocks_rocktri", "images_blocks_rock", "images_blocks_rocktri", "images_blocks_rock"))); } BlockType boulder = BlockManager.Get("MarsMiner_Boulder"); boulder.SetComponant(new VisibilityBComponant(true, Face.All)); boulder.SetComponant(new ModelBComponant(GeometryModel.Cube("images_blocks_boulder"))); }
public static GeometryModel Terrain( TerrainCorner highCorners, string texTop, string texTopTri, string texWall, string texWallTri, string texBottom) { Vector2 pfl = new Vector2( 0.0f, 0.0f ); Vector2 pfr = new Vector2( 1.0f, 0.0f ); Vector2 pbl = new Vector2( 0.0f, 1.0f ); Vector2 pbr = new Vector2( 1.0f, 1.0f ); bool bfl = ( highCorners & TerrainCorner.FrontLeft ) != 0; bool bfr = ( highCorners & TerrainCorner.FrontRight ) != 0; bool bbl = ( highCorners & TerrainCorner.BackLeft ) != 0; bool bbr = ( highCorners & TerrainCorner.BackRight ) != 0; float hfl = bfl ? 1.0f : 0.0f; float hfr = bfr ? 1.0f : 0.0f; float hbl = bbl ? 1.0f : 0.0f; float hbr = bbr ? 1.0f : 0.0f; Tuple<Face,Vector2,Vector2,float,float>[] faces = new Tuple<Face,Vector2,Vector2,float,float>[] { new Tuple<Face,Vector2,Vector2,float,float> ( Face.Left, pbl, pfl, hbl, hfl ), new Tuple<Face,Vector2,Vector2,float,float> ( Face.Front, pfl, pfr, hfl, hfr ), new Tuple<Face,Vector2,Vector2,float,float> ( Face.Right, pfr, pbr, hfr, hbr ), new Tuple<Face,Vector2,Vector2,float,float> ( Face.Back, pbr, pbl, hbr, hbl ) }; GeometryModel mdl = new GeometryModel(); foreach( Tuple<Face,Vector2,Vector2,float,float> face in faces ) { if( face.Item4 == 0.0f && face.Item5 == 0.0f ) continue; ModelFace mf; if( face.Item4 == 0.0f || face.Item5 == 0.0f ) { Vector2 corner = face.Item4 == 0.0f ? face.Item3 : face.Item2; mf = new ModelFace( texWallTri, new float[] { face.Item2.X, face.Item4, face.Item2.Y, 0.0f, face.Item4, face.Item3.X, face.Item5, face.Item3.Y, 1.0f, face.Item5, corner.X, 0.0f, corner.Y, face.Item4 == 0.0f ? 1.0f : 0.0f, 0.0f } ); } else { mf = new ModelFace( texWall, new float[] { face.Item2.X, face.Item4, face.Item2.Y, 0.0f, face.Item4, face.Item3.X, face.Item5, face.Item3.Y, 1.0f, face.Item5, face.Item3.X, 0.0f, face.Item3.Y, 1.0f, 0.0f, face.Item2.X, 0.0f, face.Item2.Y, 0.0f, 0.0f } ); } mdl.AddFace( mf, face.Item1 ); } bool tri = (int) highCorners % 3 > 0; if ( !( !bfr && !bbl && ( bfl ^ bbr ) ) && !( bfr && bbl && ( !bfl || !bbr ) ) ) { mdl.AddFace( new ModelFace( tri ? texTopTri : texTop, new float[] { pbl.X, hbl, pbl.Y, 0.0f, 0.0f, pbr.X, hbr, pbr.Y, 1.0f, 0.0f, pfr.X, hfr, pfr.Y, 1.0f, 1.0f, } ), bbl && bbr && bfr ? Face.Top : Face.All ); mdl.AddFace( new ModelFace( tri ? texTopTri : texTop, new float[] { pbl.X, hbl, pbl.Y, 0.0f, 0.0f, pfr.X, hfr, pfr.Y, 1.0f, 1.0f, pfl.X, hfl, pfl.Y, 0.0f, 1.0f } ), bbl && bfl && bfr ? Face.Top : Face.All ); } else { mdl.AddFace( new ModelFace( tri ? texTopTri : texTop, new float[] { pbr.X, hbr, pbr.Y, 0.0f, 0.0f, pfr.X, hfr, pfr.Y, 1.0f, 0.0f, pfl.X, hfl, pfl.Y, 1.0f, 1.0f, } ), bbr && bfr && bfl ? Face.Top : Face.All ); mdl.AddFace( new ModelFace( tri ? texTopTri : texTop, new float[] { pbr.X, hbr, pbr.Y, 0.0f, 0.0f, pfl.X, hfl, pfl.Y, 1.0f, 1.0f, pbl.X, hbl, pbl.Y, 0.0f, 1.0f } ), bbr && bfl && bbl ? Face.Top : Face.All ); } return mdl; }
public static GeometryModel Terrain(TerrainCorner highCorners, string texTop, string texTopTri, string texWall, string texWallTri, string texBottom) { Vector2 pfl = new Vector2(0.0f, 0.0f); Vector2 pfr = new Vector2(1.0f, 0.0f); Vector2 pbl = new Vector2(0.0f, 1.0f); Vector2 pbr = new Vector2(1.0f, 1.0f); bool bfl = (highCorners & TerrainCorner.FrontLeft) != 0; bool bfr = (highCorners & TerrainCorner.FrontRight) != 0; bool bbl = (highCorners & TerrainCorner.BackLeft) != 0; bool bbr = (highCorners & TerrainCorner.BackRight) != 0; float hfl = bfl ? 1.0f : 0.0f; float hfr = bfr ? 1.0f : 0.0f; float hbl = bbl ? 1.0f : 0.0f; float hbr = bbr ? 1.0f : 0.0f; Tuple <Face, Vector2, Vector2, float, float>[] faces = new Tuple <Face, Vector2, Vector2, float, float>[] { new Tuple <Face, Vector2, Vector2, float, float> (Face.Left, pbl, pfl, hbl, hfl), new Tuple <Face, Vector2, Vector2, float, float> (Face.Front, pfl, pfr, hfl, hfr), new Tuple <Face, Vector2, Vector2, float, float> (Face.Right, pfr, pbr, hfr, hbr), new Tuple <Face, Vector2, Vector2, float, float> (Face.Back, pbr, pbl, hbr, hbl) }; GeometryModel mdl = new GeometryModel(); foreach (Tuple <Face, Vector2, Vector2, float, float> face in faces) { if (face.Item4 == 0.0f && face.Item5 == 0.0f) { continue; } ModelFace mf; if (face.Item4 == 0.0f || face.Item5 == 0.0f) { Vector2 corner = face.Item4 == 0.0f ? face.Item3 : face.Item2; mf = new ModelFace(texWallTri, new float[] { face.Item2.X, face.Item4, face.Item2.Y, 0.0f, face.Item4, face.Item3.X, face.Item5, face.Item3.Y, 1.0f, face.Item5, corner.X, 0.0f, corner.Y, face.Item4 == 0.0f ? 1.0f : 0.0f, 0.0f }); } else { mf = new ModelFace(texWall, new float[] { face.Item2.X, face.Item4, face.Item2.Y, 0.0f, face.Item4, face.Item3.X, face.Item5, face.Item3.Y, 1.0f, face.Item5, face.Item3.X, 0.0f, face.Item3.Y, 1.0f, 0.0f, face.Item2.X, 0.0f, face.Item2.Y, 0.0f, 0.0f }); } mdl.AddFace(mf, face.Item1); } bool tri = (int)highCorners % 3 > 0; if (!(!bfr && !bbl && (bfl ^ bbr)) && !(bfr && bbl && (!bfl || !bbr))) { mdl.AddFace(new ModelFace(tri ? texTopTri : texTop, new float[] { pbl.X, hbl, pbl.Y, 0.0f, 0.0f, pbr.X, hbr, pbr.Y, 1.0f, 0.0f, pfr.X, hfr, pfr.Y, 1.0f, 1.0f, }), bbl && bbr && bfr ? Face.Top : Face.All); mdl.AddFace(new ModelFace(tri ? texTopTri : texTop, new float[] { pbl.X, hbl, pbl.Y, 0.0f, 0.0f, pfr.X, hfr, pfr.Y, 1.0f, 1.0f, pfl.X, hfl, pfl.Y, 0.0f, 1.0f }), bbl && bfl && bfr ? Face.Top : Face.All); } else { mdl.AddFace(new ModelFace(tri ? texTopTri : texTop, new float[] { pbr.X, hbr, pbr.Y, 0.0f, 0.0f, pfr.X, hfr, pfr.Y, 1.0f, 0.0f, pfl.X, hfl, pfl.Y, 1.0f, 1.0f, }), bbr && bfr && bfl ? Face.Top : Face.All); mdl.AddFace(new ModelFace(tri ? texTopTri : texTop, new float[] { pbr.X, hbr, pbr.Y, 0.0f, 0.0f, pfl.X, hfl, pfl.Y, 1.0f, 1.0f, pbl.X, hbl, pbl.Y, 0.0f, 1.0f }), bbr && bfl && bbl ? Face.Top : Face.All); } return(mdl); }