public void TestEnterFrameEvent() { _eventCount = 0; MeshStyle style = new MeshStyle(); TestQuad quad0 = new TestQuad(100, 100); TestQuad quad1 = new TestQuad(100, 100); style.EnterFrame += StyleEnterFrame; quad0.DispatchEnterFrame(); Assert.AreEqual(0, _eventCount); quad0.Style = style; quad0.DispatchEnterFrame(); Assert.AreEqual(1, _eventCount); quad0.DispatchEnterFrame(); Assert.AreEqual(2, _eventCount); quad1.Style = style; quad0.DispatchEnterFrame(); Assert.AreEqual(2, _eventCount); quad0.Style = style; quad0.DispatchEnterFrame(); Assert.AreEqual(3, _eventCount); style.EnterFrame -= StyleEnterFrame; quad0.DispatchEnterFrame(); Assert.AreEqual(3, _eventCount); }
/// <summary> /// Adds a mesh to the batch by copying its vertices and indices to the given positions. /// Beware that you need to check for yourself if those positions make sense; for example, /// you need to make sure that they are aligned within the 3-indices groups making up /// the mesh's triangles. /// /// <para>It's easiest to only add objects with an identical setup, e.g. only quads. /// For the latter, indices are aligned in groups of 6 (one quad requires six indices), /// and the vertices in groups of 4 (one vertex for every corner).</para> /// </summary> public void AddMeshAt(Mesh mesh, int indexId, int vertexId) { int numIndices = mesh.NumIndices; int numVertices = mesh.NumVertices; Matrix2D matrix = mesh.TransformationMatrix; MeshStyle meshStyle = mesh._style; if (_vertexData.NumVertices == 0) { SetupFor(mesh); } meshStyle.BatchVertexData(_style, vertexId, matrix, 0, numVertices); meshStyle.BatchIndexData(_style, indexId, vertexId, 0, numIndices); if (Alpha != 1.0f) { _vertexData.ScaleAlphas(Alpha, vertexId, numVertices); } if (_parent != null) { SetRequiresRedraw(); } _indexSyncRequired = _vertexSyncRequired = true; }
/// <summary> /// Sets the style that is used to render the mesh. Styles (which are always subclasses of /// <code>MeshStyle</code>) provide a means to completely modify the way a mesh is rendered. /// For example, they may add support for color transformations or normal mapping. /// /// <para>When assigning a new style, the vertex format will be changed to fit it. /// Do not use the same style instance on multiple objects! Instead, make use of /// <code>Style.Clone()</code> to assign an identical style to multiple meshes.</para> /// </summary> /// <param name="meshStyle">the style to assign. If <code>null</code>, the default /// style will be created.</param> /// <param name="mergeWithPredecessor">if enabled, all attributes of the previous style will be /// be copied to the new one, if possible.</param> /// <see cref="DefaultStyle"/> /// <see cref="DefaultStyleFactory"/> public virtual void SetStyle(MeshStyle meshStyle = null, bool mergeWithPredecessor = true) { if (meshStyle == null) { meshStyle = CreateDefaultMeshStyle(); } else if (meshStyle == _style) { return; } else if (meshStyle.Target != null) { meshStyle.Target.SetStyle(); } if (_style != null) { if (mergeWithPredecessor) { meshStyle.CopyFrom(_style); } _style.SetTarget(null); } _style = meshStyle; _style.SetTarget(this, _vertexData, _indexData); }
//Add a triangle (oriented clock-wise) to the mesh public void AddTriangle(MyMeshVertex v1, MyMeshVertex v2, MyMeshVertex v3, MeshStyle meshStyle) { int index1 = AddVertexAndReturnIndex(v1, meshStyle); int index2 = AddVertexAndReturnIndex(v2, meshStyle); int index3 = AddVertexAndReturnIndex(v3, meshStyle); AddTrianglePositions(index1, index2, index3); }
public override void SetStyle(MeshStyle meshStyle = null, bool mergeWithPredecessor = true) { base.SetStyle(meshStyle, mergeWithPredecessor); _effect?.Dispose(); _effect = Style.CreateEffect(); _effect.OnRestore = SetVertexAndIndexDataChanged; }
private void SetupFor(Mesh mesh) { MeshStyle meshStyle = mesh._style; Type meshStyleType = meshStyle.Type; if (_style.Type != meshStyleType) { SetStyle((MeshStyle)Activator.CreateInstance(meshStyleType), false); } _style.CopyFrom(meshStyle); }
private MeshStyle CreateDefaultMeshStyle() { MeshStyle meshStyle = null; if (_sDefaultStyleFactory != null) { meshStyle = _sDefaultStyleFactory(); } if (meshStyle == null) { meshStyle = (MeshStyle)Activator.CreateInstance(_sDefaultStyle); } return(meshStyle); }
/// <summary> Creates a new mesh with the given vertices and indices. /// If you don't pass a style, an instance of <code>MeshStyle</code> will be created /// for you. Note that the format of the vertex data will be matched to the /// given style right away. /// </summary> public Mesh(VertexData vertexData, IndexData indexData, MeshStyle style = null) { if (vertexData == null) { throw new ArgumentException("VertexData must not be null"); } if (indexData == null) { throw new ArgumentException("IndexData must not be null"); } _vertexData = vertexData; _indexData = indexData; SetStyle(style, false); }
//Add a vertex to the mesh and return its position in the array //If we want only hard edges, set shareVertices to false. Otherwise we will get a smooth surface //If we want combination of smooth surface and hard edges, set shareVertices and hasHardEdges to true public int AddVertexAndReturnIndex(MyMeshVertex v, MeshStyle meshStyle) { int vertexPosInList = -1; if (meshStyle == MeshStyle.SoftEdges || meshStyle == MeshStyle.HardAndSoftEdges) { for (int i = 0; i < vertices.Count; i++) { MyVector3 thisPos = vertices[i]; if (thisPos.Equals(v.position)) { //Here we have to compare both position and normal or we can't get hard edges in combination with soft edges MyVector3 thisNormal = normals[i]; if (meshStyle == MeshStyle.HardAndSoftEdges && thisNormal.Equals(v.normal)) { vertexPosInList = i; return(vertexPosInList); } //Sometimes we dont have a normal to compare if (meshStyle == MeshStyle.SoftEdges) { vertexPosInList = i; return(vertexPosInList); } } } } //If we got here it means the vertex is not in the list, so add it as the last vertex vertices.Add(v.position); normals.Add(v.normal); vertexPosInList = vertices.Count - 1; return(vertexPosInList); }
public void TestAssignment() { Quad quad0 = new Quad(100, 100); Quad quad1 = new Quad(100, 100); MeshStyle style = new MeshStyle(); Type meshStyleType = typeof(MeshStyle); quad0.Style = style; Assert.AreEqual(style, quad0.Style); Assert.AreEqual(style.Target, quad0); quad1.Style = style; Assert.AreEqual(style, quad1.Style); Assert.AreEqual(style.Target, quad1); Assert.IsFalse(quad0.Style == style); Assert.AreEqual(quad0.Style.Type, meshStyleType); quad1.Style = null; Assert.AreEqual(quad1.Style.Type, meshStyleType); Assert.IsNull(style.Target); }
/// <summary> /// Adds a mesh to the batch by appending its vertices and indices. /// </summary> /// <param name="mesh">The mesh to add to the batch.</param> /// <param name="matrix">Transform all vertex positions with a certain matrix. If this /// parameter is omitted, <code>mesh.transformationMatrix</code> /// will be used instead (except if the last parameter is enabled).</param> /// <param name="alpha">Will be multiplied with each vertex' alpha value.</param> /// <param name="subset">The subset of the mesh you want to add, or <code>null</code> for /// the complete mesh.</param> /// <param name="ignoreTransformations">When enabled, the mesh's vertices will be added /// without transforming them in any way (no matter the value of the /// <code>matrix</code>)</param> public void AddMesh(Mesh mesh, Matrix2D matrix = null, float alpha = 1.0f, MeshSubset subset = null, bool ignoreTransformations = false) { if (ignoreTransformations) { matrix = null; } else if (matrix == null) { matrix = mesh.TransformationMatrix; } if (subset == null) { subset = SFullMeshSubset; } int targetVertexId = _vertexData.NumVertices; int targetIndexId = _indexData.NumIndices; MeshStyle meshStyle = mesh._style; if (targetVertexId == 0) { SetupFor(mesh); } meshStyle.BatchVertexData(_style, targetVertexId, matrix, subset.VertexId, subset.NumVertices); meshStyle.BatchIndexData(_style, targetIndexId, targetVertexId - subset.VertexId, subset.IndexId, subset.NumIndices); if (alpha != 1.0f) { _vertexData.ScaleAlphas(alpha, targetVertexId, subset.NumVertices); } if (_parent != null) { SetRequiresRedraw(); } _indexSyncRequired = _vertexSyncRequired = true; }
public override bool CanBatchWith(MeshStyle meshStyle) { return(base.CanBatchWith(meshStyle)); }
public override void CopyFrom(MeshStyle meshStyle) { base.CopyFrom(meshStyle); }