// Start is called before the first frame update. void Start() { myRb = GetComponent <PhysXRigidBody>(); meshstateTracker = FindObjectOfType <MeshstateTracker>(); deformableMeshes = new List <DeformableMesh>(GetComponentsInChildren <DeformableMesh>()); DeformableMesh.Subdivide(deformableMeshes[0].maxEdgeLength, deformableMeshes[0].GetMeshFilter().mesh); vertices = new List <Vector3>(deformableMeshes[0].GetMeshFilter().mesh.vertices); // Group similar vertices. meshGraph = meshstateTracker.GetMyMeshGraph(meshType); originalMesh = Instantiate(deformableMeshes[0].GetMeshFilter().mesh); collisionResolver = Instantiate(collisionResolver); resolverBody = collisionResolver.GetComponent <PhysXBody>(); resolverBody.position = new Vector3(0, 10000, 0); interfaceCar = GetComponent <InterfaceCarDrive4W>(); if (interfaceCar != null) { frWheel = interfaceCar.frontRightW; frWheelVertexGroup = NearestVertexTo(frWheel.transform.position); flWheel = interfaceCar.frontLeftW; flWheelVertexGroup = NearestVertexTo(flWheel.transform.position); rrWheel = interfaceCar.rearRightW; rrWheelVertexGroup = NearestVertexTo(rrWheel.transform.position); rlWheel = interfaceCar.rearLeftW; rlWheelVertexGroup = NearestVertexTo(rlWheel.transform.position); } teamId = GetComponent <NetworkPlayerVehicle>().teamId; }
public int Add(VertexGroup entity) { lock (this) { return(scene.Add(entity, 2)); } }
private static bool ReadVertexGroup(out VertexGroup vertexGroup) { float valuesImportantInEditorButNotHere = 0; Vector3 s = new Vector3(s_reader.ReadSingle(), s_reader.ReadSingle(), s_reader.ReadSingle()); valuesImportantInEditorButNotHere = s_reader.ReadSingle(); Vector3 t = new Vector3(s_reader.ReadSingle(), s_reader.ReadSingle(), s_reader.ReadSingle()); valuesImportantInEditorButNotHere = s_reader.ReadSingle(); Vector3 r = new Vector3(s_reader.ReadSingle(), s_reader.ReadSingle(), s_reader.ReadSingle()); valuesImportantInEditorButNotHere = s_reader.ReadSingle(); Vector3 p = new Vector3(s_reader.ReadSingle(), s_reader.ReadSingle(), s_reader.ReadSingle()); valuesImportantInEditorButNotHere = s_reader.ReadSingle(); vertexGroup = new VertexGroup(s, r, t, p); VariationController vc; if (!ReadVariationData(out vc, vertexGroup.SetScaleRotationTranslation)) { return(false); } vertexGroup.SetVariation(vc); List <int> indices; if (!ReadVertexGroupIndices(out indices)) { return(false); } vertexGroup.SetIndices(indices); return(true); }
public void DisplaceVertex(VertexGroup _group, Vector3 _displacement) { for (int vertexIter = 0; vertexIter < _group.m_vertices.Count; vertexIter++) { m_vertices[_group.m_vertices[vertexIter]] += _displacement; } }
private void FillTiles() { Random random = new Random(); //Loads the tiles into the background array int height = 7, width = 7; int minHeight = -7, minWidth = -7; //string path = System.IO.Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, "../../../../Content/"); Texture2D texture = Content.Load <Texture2D>("grid"); int tileSize = 64; Vector3 posUL = new Vector3(minWidth * tileSize, height * tileSize, 0); Vector3 posLL = new Vector3(minWidth * tileSize, minHeight * tileSize, 0); Vector3 posLR = new Vector3(width * tileSize, minHeight * tileSize, 0); Vector3 posUR = new Vector3(width * tileSize, height * tileSize, 0); VertexPositionNormalTexture UL = new VertexPositionNormalTexture(posUL, Vector3.UnitZ, Vector2.Zero); VertexPositionNormalTexture LL = new VertexPositionNormalTexture(posLL, Vector3.UnitZ, Vector2.UnitY * (height - minHeight)); VertexPositionNormalTexture UR = new VertexPositionNormalTexture(posUR, Vector3.UnitZ, Vector2.UnitX * (width - minWidth)); VertexPositionNormalTexture LR = new VertexPositionNormalTexture(posLR, Vector3.UnitZ, new Vector2((height - minHeight), (width - minWidth))); List <VertexPositionNormalTexture> list = new List <VertexPositionNormalTexture>(); list.Add(UL); list.Add(LR); list.Add(LL); list.Add(UR); list.Add(LR); list.Add(UL); VertexGroup group = new VertexGroup(texture, list); group.Technique = Renderer.RendererAssetPool.UniversalEffect.Techniques.TexturedWrap; group.Ready(); renderer.ActiveScene.Add(group, 0); }
public void RebuildGeometry() { int capacity = Enumerable.Sum <TrixelSurface>((IEnumerable <TrixelSurface>) this.surfaces, (Func <TrixelSurface, int>)(x => x.RectangularParts.Count)); VertexGroup <VertexPositionNormalTextureInstance> vertexGroup = new VertexGroup <VertexPositionNormalTextureInstance>(capacity * 4); Dictionary <RectangularTrixelSurfacePart, FaceMaterialization <VertexPositionNormalTextureInstance> > dictionary = new Dictionary <RectangularTrixelSurfacePart, FaceMaterialization <VertexPositionNormalTextureInstance> >(capacity); Vector3 vector3_1 = new Vector3(0.5f); foreach (RectangularTrixelSurfacePart key in Enumerable.SelectMany <TrixelSurface, RectangularTrixelSurfacePart>((IEnumerable <TrixelSurface>) this.surfaces, (Func <TrixelSurface, IEnumerable <RectangularTrixelSurfacePart> >)(x => (IEnumerable <RectangularTrixelSurfacePart>)x.RectangularParts))) { Vector3 normal = FezMath.AsVector(key.Orientation); Vector3 vector3_2 = FezMath.AsVector(FezMath.GetTangent(key.Orientation)) * (float)key.TangentSize / 16f; Vector3 vector3_3 = FezMath.AsVector(FezMath.GetBitangent(key.Orientation)) * (float)key.BitangentSize / 16f; Vector3 position = key.Start.Position / 16f + (key.Orientation >= FaceOrientation.Right ? 1f : 0.0f) * normal / 16f - vector3_1; if (!dictionary.ContainsKey(key)) { FaceMaterialization <VertexPositionNormalTextureInstance> faceMaterialization = new FaceMaterialization <VertexPositionNormalTextureInstance>() { V0 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position, normal)), V1 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position + vector3_2, normal)), V2 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position + vector3_2 + vector3_3, normal)), V3 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position + vector3_3, normal)) }; faceMaterialization.SetupIndices(key.Orientation); dictionary.Add(key, faceMaterialization); } } VertexPositionNormalTextureInstance[] normalTextureInstanceArray = new VertexPositionNormalTextureInstance[vertexGroup.Vertices.Count]; int index = 0; foreach (SharedVertex <VertexPositionNormalTextureInstance> sharedVertex in (IEnumerable <SharedVertex <VertexPositionNormalTextureInstance> >)vertexGroup.Vertices) { normalTextureInstanceArray[index] = sharedVertex.Vertex; normalTextureInstanceArray[index].TextureCoordinate = FezMath.ComputeTexCoord <VertexPositionNormalTextureInstance>(normalTextureInstanceArray[index]) * (this.EngineState == null || !this.EngineState.InEditor ? Vector2.One : new Vector2(1.333333f, 1f)); sharedVertex.Index = index++; } int[] numArray = new int[dictionary.Count * 6]; int num = 0; foreach (FaceMaterialization <VertexPositionNormalTextureInstance> faceMaterialization in dictionary.Values) { for (ushort relativeIndex = (ushort)0; (int)relativeIndex < 6; ++relativeIndex) { numArray[num++] = faceMaterialization.GetIndex(relativeIndex); } } if (this.geometry == null) { this.geometry = new ShaderInstancedIndexedPrimitives <VertexPositionNormalTextureInstance, Vector4>(PrimitiveType.TriangleList, 220); this.geometry.NeedsEffectCommit = true; if (this.group != null) { this.group.Geometry = (IIndexedPrimitiveCollection)this.geometry; } } this.geometry.Vertices = normalTextureInstanceArray; this.geometry.Indices = numArray; this.DetermineFlags(); }
public Face(Vector3[] inPoints, int Index, MeshEditor editor) { points = inPoints; TriIndex = Index; Editor = editor; center = (points[0] + points[1] + points[2]) / 3; distances = new Vector3[] { points[0] - center, points[1] - center, points[2] - center }; Groups = new VertexGroup[] { Editor.GetVertexGroup(points[0]), Editor.GetVertexGroup(points[1]), Editor.GetVertexGroup(points[2]) }; }
private static void PostprocessModel(GameObject model, Mesh mesh) { var meshCopy = new Mesh(); meshCopy.vertices = mesh.vertices; meshCopy.triangles = mesh.triangles; meshCopy.RecalculateNormals(); var vertices = meshCopy.vertices; var normals = meshCopy.normals; var uvs = new List <Vector3>(new Vector3[vertices.Length]); for (var submesh = 0; submesh < mesh.subMeshCount; submesh++) { var verticesOfTheSubmesh = new HashSet <int>(); var triangles = mesh.GetTriangles(submesh); foreach (var index in triangles) { verticesOfTheSubmesh.Add(index); } var similarVertices = new List <VertexGroup>(); foreach (var vertex in verticesOfTheSubmesh) { var vertexPosition = vertices[vertex]; var similar = similarVertices.Find(x => Vector3.Distance(x.Position, vertexPosition) < MinVertexDistance); if (similar == null) { similar = new VertexGroup() { Position = vertexPosition }; similarVertices.Add(similar); } similar.Normal += normals[vertex]; similar.Others.Add(vertex); } foreach (var group in similarVertices) { var normal = (group.Normal / group.Others.Count).normalized; foreach (var other in group.Others) { uvs[other] = normal; } } } mesh.SetUVs(UVIndex, uvs); mesh.UploadMeshData(false); }
/// <summary> /// Adds a new RendererVertexGroup entity to the scene /// </summary> /// <param name="_entity">The entity to add.</param> /// <param name="_layer">The layer to add it to, lower layers are drawn first.</param> /// <returns>The entity's unique ID.</returns> public int Add(VertexGroup _entity, int _layer) { int x = entityID.GetID(); entityList.Add(x, _entity); AddLayers(_layer); layers[_layer].Add(x, _entity); return(x); }
/// <summary> /// Generates group objects to represent groups of unshared verticies. The mesh editing system dosen't /// interact with verticies by themselves, but rather with these wrapper objects, so they are nesesarry for all mesh editing operations. /// The proccess is relativelty slow (probably could be sped up) so this method is Async so this can happen in the background. If you need the vertex groups /// immediately for some reason, just use the GenerateVertexGroupsNow method /// </summary> /// <returns></returns> public IEnumerator GenerateVertexGroups() { if (!Filter) { Start(); } if (VertexGroups == null) { VertexGroups = new Dictionary <Vector3, VertexGroup>(); } VertexGroups.Clear(); for (int i = 0; i < Filter.mesh.vertexCount; i++) { var v = Filter.mesh.vertices[i]; Vertex newVertex = new Vertex(); newVertex.Index = i; newVertex.Body = transform; newVertex.LocalPosition = v; newVertex.Editor = this; foreach (var k in VertexGroups.Keys) { if (Vector3.Distance(k, v) < minVertDistance) { v = k; } } var g = GetVertexGroup(v); if (g != null) { g.Verts.Add(newVertex); } else { g = new VertexGroup(); g.Verts = new List <Vertex>(); g.Verts.Add(newVertex); g.Editor = this; VertexGroups.Add(v, g); } if (i % 10 == 0) { yield return(null); } } HasGroups = true; if (!IsShared) { ConvertToSharedVerts(false); } else { UpdateFaces(); } }
public void GenerateVertexGroupsNow() { if (!Filter) { Start(); } if (VertexGroups == null) { VertexGroups = new Dictionary <Vector3, VertexGroup>(); } VertexGroups.Clear(); for (int i = 0; i < Filter.mesh.vertexCount; i++) { var v = Filter.mesh.vertices[i]; Vertex newVertex = new Vertex(); newVertex.Index = i; newVertex.Body = transform; newVertex.LocalPosition = v; newVertex.Editor = this; VertexGroup g = null; if (VertexGroups.ContainsKey(v)) { g = VertexGroups[v]; } else { g = GetVertexGroup(v); } if (g != null) { g.Verts.Add(newVertex); } else { g = new VertexGroup(); g.Verts = new List <Vertex>(); g.Editor = this; g.Verts.Add(newVertex); VertexGroups.Add(v, g); } } HasGroups = true; if (!IsShared) { ConvertToSharedVerts(false); } else { UpdateFaces(); } }
public VertexGroup(VertexGroup src) { MatrixDataIndex = src.MatrixDataIndex; MatrixIndex = src.MatrixIndex; Tex0MatrixIndex = src.Tex0MatrixIndex; Tex1MatrixIndex = src.Tex1MatrixIndex; PositionIndex = src.PositionIndex; NormalIndex = src.NormalIndex; TangentIndex = src.TangentIndex; BinormalIndex = src.BinormalIndex; ColorIndex = src.ColorIndex; TexCoordIndex = src.TexCoordIndex; }
public Mass(SoftBodyMesh _mesh, Vector3 _postion, Vector3 _normal, int _index, bool _fixed) { mesh = _mesh; vertexGroup = null; m_postion = _postion; m_normal = _normal; index = _index; springs = new List <int>(); neighbours = new List <int>(); structuralNeighbours = new List <int>(); force = Vector3.zero; velocity = Vector3.zero; m_fixed = _fixed; }
private static IList <CrossCounterPair> FindVirtualEdgePairs( [NotNull, ItemNotNull] AlternatingLayer topLayer, [NotNull, ItemNotNull] AlternatingLayer bottomLayer) { var virtualEdgePairs = new List <CrossCounterPair>(); Queue <VertexGroup> firstLayerQueue = GetContainerLikeItems(topLayer, VertexTypes.PVertex); Queue <VertexGroup> secondLayerQueue = GetContainerLikeItems(bottomLayer, VertexTypes.QVertex); var group1 = new VertexGroup(); var group2 = new VertexGroup(); while (firstLayerQueue.Count > 0 || secondLayerQueue.Count > 0) { if (group1.Size == 0) { group1 = firstLayerQueue.Dequeue(); } if (group2.Size == 0) { group2 = secondLayerQueue.Dequeue(); } if (group1.Size <= group2.Size) { virtualEdgePairs.Add( new CrossCounterPair { First = group1.Position, Second = group2.Position, Weight = group1.Size }); group2.Size -= group1.Size; group1.Size = 0; } else { virtualEdgePairs.Add( new CrossCounterPair { First = group1.Position, Second = group2.Position, Weight = group2.Size }); group1.Size -= group2.Size; group2.Size = 0; } } return(virtualEdgePairs); }
// Return the closest vertex group to the given postion VertexGroup GetClosestVertexGroup(Vector3 pos) { VertexGroup closest = meshGraph.groups[0]; float closestDist = (vertices[closest.vertexIndices[0]] - pos).sqrMagnitude; for (int i = 1; i < meshGraph.groups.Length; i++) { float newDist = (vertices[meshGraph.groups[i].vertexIndices[0]] - pos).sqrMagnitude; if (newDist < closestDist) { closestDist = newDist; closest = meshGraph.groups[i]; } } return(closest); }
public VertexGroup(VertexGroup other) { Verts = new List <Vertex>(); foreach (var v in other.Verts) { var nv = new Vertex(); nv.Body = v.Body; nv.Editor = v.Editor; nv.WorldPosition = v.WorldPosition; Verts.Add(nv); } WorldPosition = other.WorldPosition; center = WorldPosition; Editor = other.Editor; }
public void AddWall(Vector3 LL, Vector3 normal, int tilesWide, int tilesHigh, int textureID) { VertexPositionNormalTexture lowerLeft, lowerRight, upperLeft, upperRight; Vector3 right = new Vector3(-normal.Y, normal.X, normal.Z); right.Normalize(); lowerLeft = new VertexPositionNormalTexture(LL, normal, TCLL * tilesHigh); lowerRight = new VertexPositionNormalTexture(LL + right * (tilesWide * Constants.TILE_SIZE), normal, TCLL * tilesHigh + TCUR * tilesWide); upperLeft = new VertexPositionNormalTexture(LL + Vector3.UnitZ * (tilesHigh * Constants.TILE_SIZE), normal, TCUL); upperRight = new VertexPositionNormalTexture(LL + Vector3.UnitZ * (tilesHigh * Constants.TILE_SIZE) + right * (tilesWide * Constants.TILE_SIZE), normal, TCUR * tilesWide); if (tilesHigh <= 1) { if (!NonTransWallTextureGroups.ContainsKey(textureID)) { VertexGroup gp = new VertexGroup(TileList.GetTile(textureID)); gp.Technique = RendererAssetPool.UniversalEffect.Techniques.TexturedWrap; NonTransWallTextureGroups.Add(textureID, gp); } NonTransWallTextureGroups[textureID].Add(lowerLeft); NonTransWallTextureGroups[textureID].Add(upperLeft); NonTransWallTextureGroups[textureID].Add(lowerRight); NonTransWallTextureGroups[textureID].Add(lowerRight); NonTransWallTextureGroups[textureID].Add(upperLeft); NonTransWallTextureGroups[textureID].Add(upperRight); } else { if (!WallTextureGroups.ContainsKey(textureID)) { VertexGroup gp = new VertexGroup(TileList.GetTile(textureID)); gp.Technique = RendererAssetPool.UniversalEffect.Techniques.TexturedWrap; gp.TransparencyEnabled = true; WallTextureGroups.Add(textureID, gp); } WallTextureGroups[textureID].Add(lowerLeft); WallTextureGroups[textureID].Add(upperLeft); WallTextureGroups[textureID].Add(lowerRight); WallTextureGroups[textureID].Add(lowerRight); WallTextureGroups[textureID].Add(upperLeft); WallTextureGroups[textureID].Add(upperRight); } }
private IList <CrossCounterPair> FindVirtualEdgePairs(AlternatingLayer topLayer, AlternatingLayer bottomLayer) { var virtualEdgePairs = new List <CrossCounterPair>(); Queue <VertexGroup> firstLayerQueue = GetContainerLikeItems(topLayer, VertexTypes.PVertex); Queue <VertexGroup> secondLayerQueue = GetContainerLikeItems(bottomLayer, VertexTypes.QVertex); VertexGroup vg1, vg2; vg1 = new VertexGroup(); vg2 = new VertexGroup(); while (firstLayerQueue.Count > 0 || secondLayerQueue.Count > 0) { if (vg1.Size == 0) { vg1 = firstLayerQueue.Dequeue(); } if (vg2.Size == 0) { vg2 = secondLayerQueue.Dequeue(); } if (vg1.Size <= vg2.Size) { virtualEdgePairs.Add( new CrossCounterPair { First = vg1.Position, Second = vg2.Position, Weight = vg1.Size }); vg2.Size -= vg1.Size; vg1.Size = 0; } else { virtualEdgePairs.Add( new CrossCounterPair { First = vg1.Position, Second = vg2.Position, Weight = vg2.Size }); vg1.Size -= vg2.Size; vg2.Size = 0; } } return(virtualEdgePairs); }
/// <summary> /// Gets the vertex group closest to the specified (Local Space) position. /// </summary> /// <param name="inVector">The point to start looking from (Local Position)</param> /// <returns></returns> public VertexGroup GetVertexGroup(Vector3 inVector) { VertexGroup g = null; float dist = float.MaxValue; foreach (var group in VertexGroups.Values) { if (dist > Vector3.Distance(inVector, group.LocalPosition)) { g = group; dist = Vector3.Distance(inVector, group.LocalPosition); } } if (dist < minVertDistance) { return(g); } return(null); }
public void AddFloor(Vector3 LL, int height, int textureID) { VertexPositionNormalTexture lowerLeft, lowerRight, upperLeft, upperRight; LL = LL + Vector3.UnitZ * height * Constants.TILE_SIZE; lowerLeft = new VertexPositionNormalTexture(LL, Vector3.UnitZ, TCLL); lowerRight = new VertexPositionNormalTexture(LL + Vector3.UnitX * Constants.TILE_SIZE, Vector3.UnitZ, TCLR); upperLeft = new VertexPositionNormalTexture(LL + Vector3.UnitY * Constants.TILE_SIZE, Vector3.UnitZ, TCUL); upperRight = new VertexPositionNormalTexture(LL + (Vector3.UnitY + Vector3.UnitX) * Constants.TILE_SIZE , Vector3.UnitZ, TCUR); if (height <= 1) { if (!FloorTextureGroups.ContainsKey(textureID)) { VertexGroup gp = new VertexGroup(TileList.GetTile(textureID)); gp.Technique = RendererAssetPool.UniversalEffect.Techniques.TexturedClamp; FloorTextureGroups.Add(textureID, gp); } FloorTextureGroups[textureID].Add(lowerLeft); FloorTextureGroups[textureID].Add(upperLeft); FloorTextureGroups[textureID].Add(lowerRight); FloorTextureGroups[textureID].Add(lowerRight); FloorTextureGroups[textureID].Add(upperLeft); FloorTextureGroups[textureID].Add(upperRight); } else { if (!WallTextureGroups.ContainsKey(textureID)) { VertexGroup gp = new VertexGroup(TileList.GetTile(textureID)); gp.TransparencyEnabled = true; gp.Technique = RendererAssetPool.UniversalEffect.Techniques.TexturedWrap; WallTextureGroups.Add(textureID, gp); } WallTextureGroups[textureID].Add(lowerLeft); WallTextureGroups[textureID].Add(upperLeft); WallTextureGroups[textureID].Add(lowerRight); WallTextureGroups[textureID].Add(lowerRight); WallTextureGroups[textureID].Add(upperLeft); WallTextureGroups[textureID].Add(upperRight); } }
/// <summary> /// Gets the vertex group closest to the specified (World Space) position. /// </summary> /// <param name="inVector">The point to start looking from (World Position)</param> /// <returns></returns> public VertexGroup GetClosestVertex(Vector3 worldPosition) { if (VertexGroups.Count == 0) { GenerateVertexGroupsNow(); } float distance = float.MaxValue; VertexGroup vert = null; Vector3 localPosition = transform.InverseTransformPoint(worldPosition); foreach (var g in VertexGroups) { if (Vector3.Distance(localPosition, g.Value.LocalPosition) < distance) { vert = g.Value; distance = Vector3.Distance(localPosition, g.Value.LocalPosition); } } return(vert); }
public VertexGroup NearestVertexTo(Vector3 point) { // convert point to local space point = transform.InverseTransformPoint(point); float minDistanceSqr = Mathf.Infinity; VertexGroup nearestVertex = meshGraph.groups[0]; // scan all vertices to find nearest foreach (VertexGroup vertex in meshGraph.groups) { Vector3 diff = point - vertex.pos; float distSqr = diff.sqrMagnitude; if (distSqr < minDistanceSqr) { minDistanceSqr = distSqr; nearestVertex = vertex; } } return(nearestVertex); }
/// <summary> /// Gets the closest edge. /// </summary> /// <param name="position">The position.</param> /// <returns></returns> public Edge GetClosestEdge(Vector3 position) { if (!HasGroups) { Debug.LogError("Groups haven't generated yet!"); } UpdateFaces(); var Filter = GetComponent <MeshFilter>(); VertexGroup firstHandle = GetClosestVertex(position); float dist = float.MaxValue; Vector3 v1, v2; Edge closestEdge = Edges[0]; var mesh = Filter.mesh; foreach (var e in Edges) { if (e.Contains(firstHandle.LocalPosition) && Vector3.Distance(transform.TransformPoint(e.center), position) < dist) { dist = Vector3.Distance(transform.TransformPoint(e.center), position); closestEdge = e; } } return(closestEdge); }
/// <summary> /// Gets the closest face. /// </summary> /// <param name="position">The position.</param> /// <returns></returns> public Face GetClosestFace(Vector3 position) { if (!HasGroups) { Debug.LogError("Groups haven't generated yet!"); } UpdateFaces(); var Filter = GetComponent <MeshFilter>(); VertexGroup firstVert = GetClosestVertex(position); float dist = float.MaxValue; Vector3 v1, v2, v3; Face closestFace = Faces[0]; var mesh = Filter.mesh; foreach (var f in Faces) { if (f.Contains(firstVert.LocalPosition) && Vector3.Distance(transform.TransformPoint(f.center), position) < dist) { dist = Vector3.Distance(transform.TransformPoint(f.center), position); closestFace = f; } } return(closestFace); }
/// <summary> /// Calculates parameters for tiles with height and creates DrawableTiles from them /// </summary> private void FillTiles() { Random random = new Random(); Dictionary <uint, int> borderTileCounter = new Dictionary <uint, int>(); for (uint y = 0; y < map.Height; y++) { if (borderTileCounter.ContainsKey(map.GetTile(0, y).ID) && map.GetTile(0, y).Height == 0) { borderTileCounter[map.GetTile(0, y).ID]++; } else if (map.GetTile(0, y).Height == 0) { borderTileCounter.Add(map.GetTile(0, y).ID, 1); } if (borderTileCounter.ContainsKey(map.GetTile(map.Width - 1, y).ID) && map.GetTile(map.Width - 1, y).Height == 0) { borderTileCounter[map.GetTile(map.Width - 1, y).ID]++; } else if (map.GetTile(map.Width - 1, y).Height == 0) { borderTileCounter.Add(map.GetTile(map.Width - 1, y).ID, 1); } } for (uint x = 0; x < map.Width - 1; x++) { if (borderTileCounter.ContainsKey(map.GetTile(x, 0).ID) && map.GetTile(x, 0).Height == 0) { borderTileCounter[map.GetTile(x, 0).ID]++; } else if (map.GetTile(x, 0).Height == 0) { borderTileCounter.Add(map.GetTile(x, 0).ID, 1); } if (borderTileCounter.ContainsKey(map.GetTile(x, map.Height - 1).ID) && map.GetTile(x, map.Height - 1).Height == 0) { borderTileCounter[map.GetTile(x, map.Height - 1).ID]++; } else if (map.GetTile(x, map.Height - 1).Height == 0) { borderTileCounter.Add(map.GetTile(x, map.Height - 1).ID, 1); } } int maxValue = 0; uint maxValueTile = 0; foreach (KeyValuePair <uint, int> element in borderTileCounter) { if (element.Value > maxValue) { maxValue = element.Value; maxValueTile = element.Key; } } //Load floor float minX = -10000f; float maxX = 100000f; float minY = -100000f; float maxY = 10000f; float Xspread = (maxX - minX) / Constants.TILE_SIZE; float Yspread = (maxY - minY) / Constants.TILE_SIZE; List <VertexPositionNormalTexture> verts = new List <VertexPositionNormalTexture>(); verts.Add(new VertexPositionNormalTexture(new Vector3(minX, minY, -0.75f), Vector3.UnitZ, Vector2.UnitY * Yspread)); verts.Add(new VertexPositionNormalTexture(new Vector3(minX, maxY, -0.75f), Vector3.UnitZ, Vector2.Zero)); verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, minY, -0.75f), Vector3.UnitZ, new Vector2(Xspread, Yspread))); verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, maxY, -0.75f), Vector3.UnitZ, Vector2.UnitX * Xspread)); verts.Add(new VertexPositionNormalTexture(new Vector3(maxX, minY, -0.75f), Vector3.UnitZ, new Vector2(Xspread, Yspread))); verts.Add(new VertexPositionNormalTexture(new Vector3(minX, maxY, -0.75f), Vector3.UnitZ, Vector2.Zero)); VertexGroup floor = new VertexGroup(TileList.GetTile((int)maxValueTile), verts); floor.Technique = RendererAssetPool.UniversalEffect.Techniques.TexturedWrap; floor.CastsShadow = false; floor.TransparencyEnabled = false; floor.Ready(); renderer.ActiveScene.Add(floor, 0); //Loads the tiles into the background array for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); // Figure out the height of each tile in each direction relative to this one. int positionNorth = (int)(y - 1); int positionSouth = (int)(y + 1); int positionWest = (int)(x - 1); int positionEast = (int)(x + 1); if (positionNorth >= 0) { tempTile.heightN = map.GetTile(x, y - 1).Height; } if (positionSouth < map.Height) { tempTile.heightS = map.GetTile(x, y + 1).Height; } if (positionWest >= 0) { tempTile.heightW = map.GetTile(x - 1, y).Height; } if (positionEast < map.Width) { tempTile.heightE = map.GetTile(x + 1, y).Height; } map.SetTile(x, y, tempTile); Texture2D texture = TileList.GetTile((int)tempTile.ID); DrawableTile tile = new DrawableTile( texture, tempTile, (int)x, (int)y, tempTile.heightN, tempTile.heightW, tempTile.heightE, tempTile.heightS); Vector3 pos = new Vector3( x * Constants.TILE_SIZE + (Constants.TILE_SIZE / 2), -(y * Constants.TILE_SIZE + (Constants.TILE_SIZE / 2)), 0); if (tempTile.ObjectID != 0) { Model objectModel = TileList.GetObject(tempTile.ObjectID); Object3 newTileObj = new Object3(objectModel, pos + (Vector3.UnitZ * tempTile.Height * Constants.TILE_SIZE)); newTileObj.TransparencyEnabled = true; Scene.Add(newTileObj, 1); } switch (tempTile.EventID) { case 4: case 5: GameSession.Alliance team = tempTile.EventID == 4 ? GameSession.Alliance.RED : GameSession.Alliance.BLUE; if (currentGameMode == VTankObject.GameMode.CAPTURETHEFLAG) { Flags.AddFlag(team, pos); } break; case 8: case 9: case 10: if (currentGameMode == VTankObject.GameMode.CAPTURETHEBASE) { Bases.AddBase(GameSession.Alliance.BLUE, tempTile.EventID, pos); } break; case 11: case 12: case 13: if (currentGameMode == VTankObject.GameMode.CAPTURETHEBASE) { Bases.AddBase(GameSession.Alliance.RED, tempTile.EventID, pos); } break; default: break; } visibleTiles[y * map.Width + x] = tile; } } #region Make Flat Tiles for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-(y + 1) * Constants.TILE_SIZE), 0); Tiles.AddFloor(pos, tempTile.Height, (int)tempTile.ID); } } #endregion #region Make North Walls int height = 0; int tileID = 0; int width = 0; int Hdir = 0; //Make north facing walls for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightN)) && (x + 1) < map.Width) { height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightN; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-y * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, Vector3.UnitY, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightN; } } width = 0; } #endregion #region Make South Walls height = 0; tileID = 0; width = 0; Hdir = 0; uint startX = 0; //Make south facing walls for (uint y = 0; y < map.Height; y++) { for (uint x = 0; x < map.Width; x++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightS)) && (x + 1) < map.Width) { if (width == 0) { startX = x; } height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightS; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3(startX * Constants.TILE_SIZE, (-(y + 1) * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, -Vector3.UnitY, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightS; startX = x; } } width = 0; } #endregion #region Make East Walls height = 0; tileID = 0; width = 0; Hdir = 0; //Make east facing walls for (uint x = 0; x < map.Width; x++) { for (uint y = 0; y < map.Height; y++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightE)) && (y + 1) < map.Height) { height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightE; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3((x + 1) * Constants.TILE_SIZE, (-y * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, Vector3.UnitX, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightE; } } width = 0; } #endregion #region Make West Walls height = 0; tileID = 0; width = 0; Hdir = 0; uint startY = 0; //Make south facing walls for (uint x = 0; x < map.Width; x++) { for (uint y = 0; y < map.Height; y++) { Tile tempTile = map.GetTile(x, y); if ((width == 0 || (tempTile.Height == height && tempTile.ID == tileID && Hdir == tempTile.heightW)) && (y + 1) < map.Height) { if (width == 0) { startY = y; } height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightW; width++; } else { if (height > Hdir) { Vector3 pos = new Vector3(x * Constants.TILE_SIZE, (-startY * Constants.TILE_SIZE), 0); Tiles.AddWall(pos, -Vector3.UnitX, width, height, tileID); } width = 1; height = tempTile.Height; tileID = (int)tempTile.ID; Hdir = tempTile.heightW; startY = y; } } width = 0; } #endregion Tiles.AllReady(); //////////////////////////////////////////// ///TODO:::: FOR EACH TILE GOING LEFT TO RIGHT // // IF height = height and tileID = tileID // if Hnorth = Hnorth // NorthWidth ++; // else // TexturedTileGroupManager.AddWall(lowerLeftNorth, Vector3.UnitY, NorthWidth, height, tileID) // NorthWidth = 0; // height = -1 // // if Hsouth = Hsouth // SouthWidth ++; // else // TexturedTileGroupManager.AddWall(lowerLeftSouth, -Vector3.UnitY, SouthhWidth, height, tileID) //etc }
private void CreateMass(VertexGroup _group) { m_masses.Add(new Mass(this, _group, m_masses.Count, CheckMassFixed((_group.m_useAveragePosition) ? _group.m_averagePosition : m_vertices[_group.m_vertices[0]]))); }
public void RebuildGeometry() { if (this.surfaces == null) return; if (this.Geometry == null) this.Geometry = new ShaderInstancedIndexedPrimitives<VertexPositionNormalTextureInstance, Matrix>(PrimitiveType.TriangleList, 60); int capacity = Enumerable.Sum<TrixelSurface>((IEnumerable<TrixelSurface>) this.surfaces, (Func<TrixelSurface, int>) (x => x.RectangularParts.Count)); Dictionary<RectangularTrixelSurfacePart, FaceMaterialization<VertexPositionNormalTextureInstance>> dictionary = new Dictionary<RectangularTrixelSurfacePart, FaceMaterialization<VertexPositionNormalTextureInstance>>(capacity * 4); VertexGroup<VertexPositionNormalTextureInstance> vertexGroup = new VertexGroup<VertexPositionNormalTextureInstance>(capacity); Vector3 vector3_1 = this.size / 2f; foreach (RectangularTrixelSurfacePart key in Enumerable.SelectMany<TrixelSurface, RectangularTrixelSurfacePart>((IEnumerable<TrixelSurface>) this.surfaces, (Func<TrixelSurface, IEnumerable<RectangularTrixelSurfacePart>>) (x => (IEnumerable<RectangularTrixelSurfacePart>) x.RectangularParts))) { if (!dictionary.ContainsKey(key)) { Vector3 normal = FezMath.AsVector(key.Orientation); Vector3 vector3_2 = FezMath.AsVector(FezMath.GetTangent(key.Orientation)) * (float) key.TangentSize / 16f; Vector3 vector3_3 = FezMath.AsVector(FezMath.GetBitangent(key.Orientation)) * (float) key.BitangentSize / 16f; Vector3 v = key.Start.Position / 16f + (FezMath.IsPositive(key.Orientation) ? 1f : 0.0f) * normal / 16f - vector3_1; FaceMaterialization<VertexPositionNormalTextureInstance> faceMaterialization = new FaceMaterialization<VertexPositionNormalTextureInstance>() { V0 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v, 4), normal)), V1 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v + vector3_2, 4), normal)), V2 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v + vector3_2 + vector3_3, 4), normal)), V3 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v + vector3_3, 4), normal)) }; faceMaterialization.SetupIndices(key.Orientation); dictionary.Add(key, faceMaterialization); } } VertexPositionNormalTextureInstance[] normalTextureInstanceArray = new VertexPositionNormalTextureInstance[vertexGroup.Vertices.Count]; int index = 0; foreach (SharedVertex<VertexPositionNormalTextureInstance> sharedVertex in (IEnumerable<SharedVertex<VertexPositionNormalTextureInstance>>) vertexGroup.Vertices) { normalTextureInstanceArray[index] = sharedVertex.Vertex; normalTextureInstanceArray[index].TextureCoordinate = FezMath.ComputeTexCoord<VertexPositionNormalTextureInstance>(normalTextureInstanceArray[index], this.size) * new Vector2(1.333333f, 1f); sharedVertex.Index = index++; } int[] numArray = new int[dictionary.Count * 6]; int num = 0; foreach (FaceMaterialization<VertexPositionNormalTextureInstance> faceMaterialization in dictionary.Values) { for (ushort relativeIndex = (ushort) 0; (int) relativeIndex < 6; ++relativeIndex) numArray[num++] = faceMaterialization.GetIndex(relativeIndex); } this.Geometry.Vertices = normalTextureInstanceArray; this.Geometry.Indices = numArray; if (this.artObject == null) return; this.PostInitialize(); }
public virtual void AddDefault(IMarkGeometry geometryIn, double[] color) { if (geometryIn == null || color == null || color.Length < 3) { AddDefault(geometryIn); return; } // Update extents MaxX = Math.Max(geometryIn.Extents.MaxX, MaxX); MaxY = Math.Max(geometryIn.Extents.MaxY, MaxY); MinX = Math.Min(geometryIn.Extents.MinX, MinX); MinY = Math.Min(geometryIn.Extents.MinY, MinY); // Update Counter Count += 1; if (geometryIn is MarkGeometryPoint point) { _points.Add(new VertexGroup() { Color = color, Vertices = new List <double>() { point.X, point.Y } }); } else if (geometryIn is MarkGeometryLine line) { // add to default _lines.Add(new VertexGroup() { Color = color, Vertices = new List <double>() { line.StartPoint.X, line.StartPoint.Y, line.EndPoint.X, line.EndPoint.Y } }); } else if (geometryIn is MarkGeometryCircle circle) { var vtx = new VertexGroup(color); for (int i = 0; i <= circle.VertexCount; i++) { vtx.Add( (circle.CentrePoint.X + (circle.Radius * Math.Cos(i * Math.PI * 2 / circle.VertexCount))), (circle.CentrePoint.Y + (circle.Radius * Math.Sin(i * Math.PI * 2 / circle.VertexCount))) ); } _closedPolylines.Add(vtx); } else if (geometryIn is MarkGeometryArc arc) { var arcPath = new MarkGeometryPath(arc); var vtx = new VertexGroup(color); // add points for (int i = 0; i < arcPath.Points.Count; i++) { vtx.Add(arcPath.Points[i].X, arcPath.Points[i].Y); } if (arcPath.IsClosed) { _closedPolylines.Add(vtx); } else { _openPolylines.Add(vtx); } } else if (geometryIn is MarkGeometryPath path) { var vtx = new VertexGroup(color); // add points for (int i = 0; i < path.Points.Count; i++) { vtx.Add(path.Points[i].X, path.Points[i].Y); } if (path.IsClosed) { _closedPolylines.Add(vtx); } else { _openPolylines.Add(vtx); } } else if (geometryIn is IMarkGeometryWrapper wrapper) { wrapper.BeginGetAll((geometry) => { AddDefault(geometry, color); return(true); }); } Update(); }
public virtual void AddDefault(IList <IMarkGeometry> geometriesIn, double[] color) { if (geometriesIn == null || color == null || color.Length < 3) { AddDefault(geometriesIn); return; } if (geometriesIn.All(x => x is MarkGeometryPoint)) { var currentExtents = GeometryExtents <double> .Combine( new GeometryExtents <double>() { MinX = MinX, MaxX = MaxX, MinY = MinY, MaxY = MaxY }, GeometricArithmeticModule.CalculateExtents( geometriesIn ) ); // Update extents MinX = currentExtents.MinX; MaxX = currentExtents.MaxX; MinY = currentExtents.MinY; MaxY = currentExtents.MaxY; // Update Counter Count += geometriesIn.Count; var vtx = new VertexGroup() { Color = color }; for (int i = 0; i < geometriesIn.Count; i++) { vtx.Vertices.AddRange(ToDouble(geometriesIn[i] as MarkGeometryPoint)); } _points.Add(vtx); Update(); } else if (geometriesIn.All(x => x is MarkGeometryLine)) { var currentExtents = GeometryExtents <double> .Combine( new GeometryExtents <double>() { MinX = MinX, MaxX = MaxX, MinY = MinY, MaxY = MaxY }, GeometricArithmeticModule.CalculateExtents( geometriesIn ) ); // Update extents MinX = currentExtents.MinX; MaxX = currentExtents.MaxX; MinY = currentExtents.MinY; MaxY = currentExtents.MaxY; // Update Counter Count += geometriesIn.Count; var vtx = new VertexGroup() { Color = color }; for (int i = 0; i < geometriesIn.Count; i++) { vtx.Vertices.AddRange(ToDouble(geometriesIn[i] as MarkGeometryLine)); } _lines.Add(vtx); Update(); } else { for (int i = 0; i < geometriesIn.Count; i++) { AddDefault(geometriesIn[i], color); } } }
public void RebuildGeometry() { if (this.surfaces == null) { return; } if (this.Geometry == null) { this.Geometry = new ShaderInstancedIndexedPrimitives <VertexPositionNormalTextureInstance, Matrix>(PrimitiveType.TriangleList, 60); } int capacity = Enumerable.Sum <TrixelSurface>((IEnumerable <TrixelSurface>) this.surfaces, (Func <TrixelSurface, int>)(x => x.RectangularParts.Count)); Dictionary <RectangularTrixelSurfacePart, FaceMaterialization <VertexPositionNormalTextureInstance> > dictionary = new Dictionary <RectangularTrixelSurfacePart, FaceMaterialization <VertexPositionNormalTextureInstance> >(capacity * 4); VertexGroup <VertexPositionNormalTextureInstance> vertexGroup = new VertexGroup <VertexPositionNormalTextureInstance>(capacity); Vector3 vector3_1 = this.size / 2f; foreach (RectangularTrixelSurfacePart key in Enumerable.SelectMany <TrixelSurface, RectangularTrixelSurfacePart>((IEnumerable <TrixelSurface>) this.surfaces, (Func <TrixelSurface, IEnumerable <RectangularTrixelSurfacePart> >)(x => (IEnumerable <RectangularTrixelSurfacePart>)x.RectangularParts))) { if (!dictionary.ContainsKey(key)) { Vector3 normal = FezMath.AsVector(key.Orientation); Vector3 vector3_2 = FezMath.AsVector(FezMath.GetTangent(key.Orientation)) * (float)key.TangentSize / 16f; Vector3 vector3_3 = FezMath.AsVector(FezMath.GetBitangent(key.Orientation)) * (float)key.BitangentSize / 16f; Vector3 v = key.Start.Position / 16f + (FezMath.IsPositive(key.Orientation) ? 1f : 0.0f) * normal / 16f - vector3_1; FaceMaterialization <VertexPositionNormalTextureInstance> faceMaterialization = new FaceMaterialization <VertexPositionNormalTextureInstance>() { V0 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v, 4), normal)), V1 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v + vector3_2, 4), normal)), V2 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v + vector3_2 + vector3_3, 4), normal)), V3 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(FezMath.Round(v + vector3_3, 4), normal)) }; faceMaterialization.SetupIndices(key.Orientation); dictionary.Add(key, faceMaterialization); } } VertexPositionNormalTextureInstance[] normalTextureInstanceArray = new VertexPositionNormalTextureInstance[vertexGroup.Vertices.Count]; int index = 0; foreach (SharedVertex <VertexPositionNormalTextureInstance> sharedVertex in (IEnumerable <SharedVertex <VertexPositionNormalTextureInstance> >)vertexGroup.Vertices) { normalTextureInstanceArray[index] = sharedVertex.Vertex; normalTextureInstanceArray[index].TextureCoordinate = FezMath.ComputeTexCoord <VertexPositionNormalTextureInstance>(normalTextureInstanceArray[index], this.size) * new Vector2(1.333333f, 1f); sharedVertex.Index = index++; } int[] numArray = new int[dictionary.Count * 6]; int num = 0; foreach (FaceMaterialization <VertexPositionNormalTextureInstance> faceMaterialization in dictionary.Values) { for (ushort relativeIndex = (ushort)0; (int)relativeIndex < 6; ++relativeIndex) { numArray[num++] = faceMaterialization.GetIndex(relativeIndex); } } this.Geometry.Vertices = normalTextureInstanceArray; this.Geometry.Indices = numArray; if (this.artObject == null) { return; } this.PostInitialize(); }
public void RebuildGeometry() { int capacity = Enumerable.Sum<TrixelSurface>((IEnumerable<TrixelSurface>) this.surfaces, (Func<TrixelSurface, int>) (x => x.RectangularParts.Count)); VertexGroup<VertexPositionNormalTextureInstance> vertexGroup = new VertexGroup<VertexPositionNormalTextureInstance>(capacity * 4); Dictionary<RectangularTrixelSurfacePart, FaceMaterialization<VertexPositionNormalTextureInstance>> dictionary = new Dictionary<RectangularTrixelSurfacePart, FaceMaterialization<VertexPositionNormalTextureInstance>>(capacity); Vector3 vector3_1 = new Vector3(0.5f); foreach (RectangularTrixelSurfacePart key in Enumerable.SelectMany<TrixelSurface, RectangularTrixelSurfacePart>((IEnumerable<TrixelSurface>) this.surfaces, (Func<TrixelSurface, IEnumerable<RectangularTrixelSurfacePart>>) (x => (IEnumerable<RectangularTrixelSurfacePart>) x.RectangularParts))) { Vector3 normal = FezMath.AsVector(key.Orientation); Vector3 vector3_2 = FezMath.AsVector(FezMath.GetTangent(key.Orientation)) * (float) key.TangentSize / 16f; Vector3 vector3_3 = FezMath.AsVector(FezMath.GetBitangent(key.Orientation)) * (float) key.BitangentSize / 16f; Vector3 position = key.Start.Position / 16f + (key.Orientation >= FaceOrientation.Right ? 1f : 0.0f) * normal / 16f - vector3_1; if (!dictionary.ContainsKey(key)) { FaceMaterialization<VertexPositionNormalTextureInstance> faceMaterialization = new FaceMaterialization<VertexPositionNormalTextureInstance>() { V0 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position, normal)), V1 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position + vector3_2, normal)), V2 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position + vector3_2 + vector3_3, normal)), V3 = vertexGroup.Reference(new VertexPositionNormalTextureInstance(position + vector3_3, normal)) }; faceMaterialization.SetupIndices(key.Orientation); dictionary.Add(key, faceMaterialization); } } VertexPositionNormalTextureInstance[] normalTextureInstanceArray = new VertexPositionNormalTextureInstance[vertexGroup.Vertices.Count]; int index = 0; foreach (SharedVertex<VertexPositionNormalTextureInstance> sharedVertex in (IEnumerable<SharedVertex<VertexPositionNormalTextureInstance>>) vertexGroup.Vertices) { normalTextureInstanceArray[index] = sharedVertex.Vertex; normalTextureInstanceArray[index].TextureCoordinate = FezMath.ComputeTexCoord<VertexPositionNormalTextureInstance>(normalTextureInstanceArray[index]) * (this.EngineState == null || !this.EngineState.InEditor ? Vector2.One : new Vector2(1.333333f, 1f)); sharedVertex.Index = index++; } int[] numArray = new int[dictionary.Count * 6]; int num = 0; foreach (FaceMaterialization<VertexPositionNormalTextureInstance> faceMaterialization in dictionary.Values) { for (ushort relativeIndex = (ushort) 0; (int) relativeIndex < 6; ++relativeIndex) numArray[num++] = faceMaterialization.GetIndex(relativeIndex); } if (this.geometry == null) { this.geometry = new ShaderInstancedIndexedPrimitives<VertexPositionNormalTextureInstance, Vector4>(PrimitiveType.TriangleList, 220); this.geometry.NeedsEffectCommit = true; if (this.group != null) this.group.Geometry = (IIndexedPrimitiveCollection) this.geometry; } this.geometry.Vertices = normalTextureInstanceArray; this.geometry.Indices = numArray; this.DetermineFlags(); }
private void ParsePacketData(FileReader reader, BIN_Parser header) { long pos = reader.Position; while (reader.Position < pos + (DisplayListSize * 0x20)) { byte opCode = reader.ReadByte(); if (opCode == 0) { continue; } PacketData packet = new PacketData(); packet.OpCode = opCode; Packets.Add(packet); ushort vertexCount = reader.ReadUInt16(); packet.Vertices = new VertexGroup[vertexCount]; for (int i = 0; i < vertexCount; i++) { VertexGroup group = new VertexGroup(); group.PositionIndex = reader.ReadInt16(); if (AttributeFlags.HasFlag(GXAttributes.Normal)) { group.NormalIndex = reader.ReadInt16(); if (NbtFlag != 0) { group.BinormalIndex = reader.ReadInt16(); group.TangentIndex = reader.ReadInt16(); } } if (AttributeFlags.HasFlag(GXAttributes.Color0)) { group.Color0Index = reader.ReadInt16(); } if (AttributeFlags.HasFlag(GXAttributes.Color1)) { group.Color1Index = reader.ReadInt16(); } for (int t = 0; t < TexCoordFlag; t++) { if (AttributeFlags.HasFlag((GXAttributes)(1 << (13 + t)))) { group.TexCoordIndex[t] = reader.ReadInt16(); } } //Parse data using (reader.TemporarySeek(header.PositionOffset + (group.PositionIndex * 6), System.IO.SeekOrigin.Begin)) { group.Position = new Vector3( (float)reader.ReadInt16(), (float)reader.ReadInt16(), (float)reader.ReadInt16()); } if (group.NormalIndex != -1) { using (reader.TemporarySeek(header.NormalOffset + (group.NormalIndex * 12), System.IO.SeekOrigin.Begin)) { group.Normal = new Vector3( (float)reader.ReadSingle(), (float)reader.ReadSingle(), (float)reader.ReadSingle()); } } if (group.TangentIndex != -1) { using (reader.TemporarySeek(header.NormalOffset + (group.TangentIndex * 12), System.IO.SeekOrigin.Begin)) { group.Tangent = new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } } if (group.BinormalIndex != -1) { using (reader.TemporarySeek(header.NormalOffset + (group.BinormalIndex * 12), System.IO.SeekOrigin.Begin)) { group.Binormal = new Vector3( reader.ReadSingle(), reader.ReadSingle(), reader.ReadSingle()); } } if (group.TexCoordIndex[0] != -1) { using (reader.TemporarySeek(header.TexCoordOffset + (group.TexCoordIndex[0] * 8), System.IO.SeekOrigin.Begin)) { //Flip UVs group.Texcoord = new Vector2( (float)reader.ReadSingle(), (float)reader.ReadSingle()); } } packet.Vertices[i] = group; } } }
//void Awake () //--------- // Use this for initialization public void InitBox(bool fromCopy=false) { MeshFilter meshFilter = GetComponent<MeshFilter> (); if (meshFilter == null) { throw new ArgumentNullException ("MeshFilter"); } _mesh = meshFilter.mesh; _mesh.RecalculateBounds (); Bounds meshBounds = _mesh.bounds; //Bounds meshBoundsTransform = transform.renderer.bounds; if (!fromCopy) _initialBounds = meshBounds; _rightSide = new VertexGroup (_mesh, Vector3.right); _leftSide = new VertexGroup (_mesh, Vector3.left); _forwardSide = new VertexGroup (_mesh, Vector3.forward); _backSide = new VertexGroup (_mesh, Vector3.back); _upSide = new VertexGroup (_mesh, Vector3.up); _downSide = new VertexGroup (_mesh, Vector3.down); float rightPoint = _initialBounds.center.x + _initialBounds.extents.x; float leftPoint = _initialBounds.center.x - _initialBounds.extents.x; float forwardPoint = _initialBounds.center.z + _initialBounds.extents.z; float backPoint = _initialBounds.center.z - _initialBounds.extents.z; float upPoint = _initialBounds.center.y /*+ meshBoundsTransform.center.y*/ + _initialBounds.extents.y; float downPoint = _initialBounds.center.y /*+ meshBoundsTransform.center.y*/ - _initialBounds.extents.y; //Set des offset min en fonction de la box collider de la piscine //et du décalage de la plage par rappport a celle-ci // Debug.Log("OBJECT = "+transform.name); //Bounds colliderBounds = transform.parent.collider.bounds; Transform margelle = transform.parent.Find(_objectBoxName); //Bounds colliderBounds = margelle.renderer.bounds; MeshFilter meshFilterMargelle = margelle.GetComponent<MeshFilter> (); if (meshFilterMargelle == null) { throw new ArgumentNullException ("MeshFilter"); } Mesh _meshMargelle = meshFilterMargelle.mesh; _meshMargelle.RecalculateBounds (); Bounds colliderBounds = _meshMargelle.bounds; float rightColliderPoint = colliderBounds.center.x + colliderBounds.extents.x; float leftColliderPoint = colliderBounds.center.x - colliderBounds.extents.x; float forwardColliderPoint = colliderBounds.center.z + colliderBounds.extents.z; float backColliderPoint = colliderBounds.center.z - colliderBounds.extents.z; float centersDiffX = transform.localPosition.x/*- meshBoundsTransform.center.x*/; float centersDiffZ = transform.localPosition.z/*- meshBoundsTransform.center.z*/; m_fwdMinOff7 = -Mathf.Abs(forwardPoint - forwardColliderPoint + centersDiffZ); m_rgtMinOff7 = -Mathf.Abs(rightPoint - rightColliderPoint + centersDiffX); m_lftMinOff7 = -Mathf.Abs(leftPoint - leftColliderPoint + centersDiffX); m_bckMinOff7 = -Mathf.Abs(backPoint - backColliderPoint + centersDiffZ); //--------- if (!fromCopy) { for (int i = 0; i < _mesh.vertexCount; ++i) { Vector3 vertex = _mesh.vertices[i]; //Vertices selection if (vertex.x <= rightPoint + TOLERANCE && vertex.x >= rightPoint - TOLERANCE) { _rightSide.Add (i); } if (vertex.x <= leftPoint + TOLERANCE && vertex.x >= leftPoint - TOLERANCE) { _leftSide.Add (i); } if (vertex.z <= forwardPoint + TOLERANCE && vertex.z >= forwardPoint - TOLERANCE) { _forwardSide.Add (i); } if (vertex.z <= backPoint + TOLERANCE && vertex.z >= backPoint - TOLERANCE) { _backSide.Add (i); } if (vertex.y <= upPoint + TOLERANCE && vertex.y >= upPoint - TOLERANCE) { _upSide.Add (i); } if (vertex.y <= downPoint + TOLERANCE && vertex.y >= downPoint - TOLERANCE) { _downSide.Add (i); } } _forwardIndices = _forwardSide.ToArray (); _rightIndices = _rightSide.ToArray (); _leftIndices = _leftSide.ToArray (); _backIndices = _backSide.ToArray (); _upIndices = _upSide.ToArray (); _downIndices = _downSide.ToArray (); } else { _forwardSide.AddRange (_forwardIndices); _rightSide.AddRange (_rightIndices); _leftSide.AddRange (_leftIndices); _backSide.AddRange (_backIndices); _upSide.AddRange (_upIndices); _downSide.AddRange (_downIndices); _forwardSide.SetValueOnly (_forwardOffset); _rightSide.SetValueOnly (_rightOffset); _leftSide.SetValueOnly (_leftOffset); _backSide.SetValueOnly (_backOffset); _upSide.SetValueOnly (_upOffset); _downSide.SetValueOnly (_downOffset); } _isInit=true; }