/// <summary> /// /// </summary> /// <param name="param">the edges of the viewport to which a SimpleUIRect is bound and determines how it is resized with its parent. /// <para>something like AnchorStyles.Left | AnchorStyles.Bottom.</para></param> /// <param name="content"></param> public DummyLabel(IUILayoutParam param, string content) { this.renderer = new StringRenderer(content.GetModel()); IUILayout layout = this; layout.Param = param; }
public SimpleUIPointSpriteStringElement(IUILayoutParam param, string content, vec3 position, GLColor color = null, int fontSize = 32, int maxRowWidth = 256, FontResource fontResource = null) { IUILayout layout = this; layout.Param = param; this.element = new PointSpriteStringElement(content, position, color, fontSize, maxRowWidth, fontResource); }
//标记为需要重新构建的布局 public static void UF_MarkLayoutForRebuild(IUILayout layout) { if (object.ReferenceEquals(null, layout)) { return; } //LayoutRebuilder.MarkLayoutForRebuild(this.rectTransform); s_LayoutBuilder.AddToRebuild(layout); }
/// <summary> /// 获取为UI元素布局所需的参数对象。 /// </summary> /// <param name="uiElement"></param> /// <returns></returns> public static IUILayoutArgs GetArgs(this IUILayout uiElement) { var args = new IUILayoutArgs(); CalculateViewport(args); CalculateCoords(uiElement, args.viewportWidth, args.viewportHeight, args); return(args); }
/// <summary> /// /// </summary> /// <param name="anchor">the edges of the viewport to which a SimpleUIRect is bound and determines how it is resized with its parent. /// <para>something like AnchorStyles.Left | AnchorStyles.Bottom.</para></param> /// <param name="margin">the space between viewport and SimpleRect.</param> /// <param name="size">Stores width when <see cref="OpenGLUIRect.Anchor"/>.Left & <see cref="OpenGLUIRect.Anchor"/>.Right is <see cref="OpenGLUIRect.Anchor"/>.None. /// <para> and height when <see cref="OpenGLUIRect.Anchor"/>.Top & <see cref="OpenGLUIRect.Anchor"/>.Bottom is <see cref="OpenGLUIRect.Anchor"/>.None.</para></param> /// <param name="zNear"></param> /// <param name="zFar"></param> /// <param name="rectColor">default color is red.</param> public SimpleUIColorIndicatorBar(IUILayoutParam param, ColorPalette colorPalette, float min, float max, float step) { IUILayout layout = this; layout.Param = param; this.ColorPalette = colorPalette; this.Min = min; this.Max = max; this.Step = step; }
/// <summary> /// /// </summary> /// <param name="anchor">the edges of the viewport to which a SimpleUIRect is bound and determines how it is resized with its parent. /// <para>something like AnchorStyles.Left | AnchorStyles.Bottom.</para></param> /// <param name="margin">the space between viewport and SimpleRect.</param> /// <param name="size">Stores width when <see cref="OpenGLUIRect.Anchor"/>.Left & <see cref="OpenGLUIRect.Anchor"/>.Right is <see cref="OpenGLUIRect.Anchor"/>.None. /// <para> and height when <see cref="OpenGLUIRect.Anchor"/>.Top & <see cref="OpenGLUIRect.Anchor"/>.Bottom is <see cref="OpenGLUIRect.Anchor"/>.None.</para></param> /// <param name="zNear"></param> /// <param name="zFar"></param> /// <param name="rectColor">default color is red.</param> public SimpleUIAxis(IUILayoutParam param, GLColor rectColor = null, float radius = 0.3f, float axisLength = 10, int faceCount = 10) { // 把AxiesElement缩放到恰好放进此UI radius = radius / axisLength / 2; axisLength = 0.5f; this.axisElement = new AxisElement(radius, axisLength, faceCount); IUILayout layout = this; layout.Param = param; }
protected override void DoRender(RenderEventArgs e) { mat4 projectionMatrix, viewMatrix, modelMatrix; { IUILayout element = this as IUILayout; element.GetMatrix(out projectionMatrix, out viewMatrix, out modelMatrix, e.Camera); } this.axisElement.mvp = projectionMatrix * viewMatrix * modelMatrix; this.axisElement.Render(e); }
static void SimpleUI_BeforeRendering(object sender, RenderEventArgs e) { mat4 projectionMatrix, viewMatrix, modelMatrix; { IUILayout element = sender as IUILayout; element.GetMatrix(out projectionMatrix, out viewMatrix, out modelMatrix, e.Camera); } { IMVP element = sender as IMVP; element.SetShaderProgram(projectionMatrix * viewMatrix * modelMatrix); } }
public static void UF_RebuildParentLayout(IUILayout layout) { var parent = (layout as MonoBehaviour).transform.parent; if (parent != null) { var parentLayout = parent.GetComponentInParent <IUILayout>(); //忽略标签布局 if (parentLayout != null) { parentLayout.UF_RebuildLoyout(); } } }
public static void UF_RebuildSiblingLayout(IUILayout layout) { List <IUILayout> list = ListCache <IUILayout> .Acquire(); (layout as MonoBehaviour).gameObject.GetComponents <IUILayout>(list); foreach (var item in list) { //忽略标签布局 if (item != layout) { item.UF_RebuildLoyout(); } } ListCache <IUILayout> .Release(list); }
/// <summary> /// /// </summary> /// <param name="anchor">the edges of the viewport to which a SimpleUIRect is bound and determines how it is resized with its parent. /// <para>something like AnchorStyles.Left | AnchorStyles.Bottom.</para></param> /// <param name="margin">the space between viewport and SimpleRect.</param> /// <param name="size">Stores width when <see cref="OpenGLUIRect.Anchor"/>.Left & <see cref="OpenGLUIRect.Anchor"/>.Right is <see cref="OpenGLUIRect.Anchor"/>.None. /// <para> and height when <see cref="OpenGLUIRect.Anchor"/>.Top & <see cref="OpenGLUIRect.Anchor"/>.Bottom is <see cref="OpenGLUIRect.Anchor"/>.None.</para></param> /// <param name="zNear"></param> /// <param name="zFar"></param> /// <param name="rectColor">default color is red.</param> public SimpleUIRect(IUILayoutParam param, GLColor rectColor = null) { IUILayout layout = this; layout.Param = param; if (rectColor == null) { this.rectColor = new vec3(0, 0, 1); } else { this.rectColor = new vec3(rectColor.R, rectColor.G, rectColor.B); } }
public bool AddToRebuild(IUILayout layout) { int hashcode = layout.GetHashCode(); if (!m_MapLayouts.Contains(hashcode)) { m_ListLayouts.Add(layout); m_MapLayouts.Add(hashcode); //for register ui canvas rebuild system CanvasUpdateRegistry.TryRegisterCanvasElementForLayoutRebuild(this); return(true); } else { return(false); } }
protected override void DoRender(RenderEventArgs e) { mat4 projectionMatrix, viewMatrix, modelMatrix; { IUILayout element = this as IUILayout; element.GetMatrix(out projectionMatrix, out viewMatrix, out modelMatrix, e.Camera); } this.shaderProgram.Bind(); this.shaderProgram.SetUniformMatrix4("MVP", (projectionMatrix * viewMatrix * modelMatrix).to_array()); GL.BindVertexArray(vao[0]); GL.DrawArrays(this.axisPrimitiveMode, 0, this.axisVertexCount); GL.BindVertexArray(0); this.shaderProgram.Unbind(); }
protected override void DoRender(RenderEventArgs e) { mat4 projectionMatrix, viewMatrix, modelMatrix; { IUILayout element = this as IUILayout; element.GetMatrix(out projectionMatrix, out viewMatrix, out modelMatrix, e.Camera); } this.shaderProgram.Bind(); this.shaderProgram.SetUniformMatrix4(strMVP, (projectionMatrix * viewMatrix * modelMatrix).to_array()); // 记录当前多边形状态 int[] polygonMode = new int[2]; GL.GetInteger(GetTarget.PolygonMode, polygonMode); GL.BindVertexArray(vao[0]); // 画面 GL.PolygonMode(PolygonModeFaces.FrontAndBack, PolygonModes.Filled); GL.DrawArrays(this.axisPrimitiveMode, 0, this.vertexCount); // 启用静态顶点属性 GL.DisableVertexAttribArray(in_ColorLocation); GL.VertexAttrib3(in_ColorLocation, 1.0f, 1.0f, 1.0f); // 画线 GL.PolygonMode(PolygonModeFaces.FrontAndBack, PolygonModes.Lines); GL.DrawArrays(this.axisPrimitiveMode, 0, this.vertexCount); // 恢复顶点属性数组 GL.EnableVertexAttribArray(in_ColorLocation); GL.BindVertexArray(0); // 恢复多边形状态 GL.PolygonMode(PolygonModeFaces.Front, (PolygonModes)polygonMode[0]); GL.PolygonMode(PolygonModeFaces.Back, (PolygonModes)polygonMode[1]); this.shaderProgram.Unbind(); }
/// <summary> /// 获取此UI元素的投影矩阵、视图矩阵和模型矩阵 /// </summary> /// <param name="uiElement"></param> /// <param name="projectionMatrix"></param> /// <param name="viewMatrix"></param> /// <param name="modelMatrix"></param> /// <param name="camera">如果为null,会以glm.lookAt(new vec3(0, 0, 1), new vec3(0, 0, 0), new vec3(0, 1, 0))计算默认值。</param> /// <param name="maxDepth">UI元素的外接球半径的倍数。</param> public static void GetMatrix(this IUILayout uiElement, out mat4 projectionMatrix, out mat4 viewMatrix, out mat4 modelMatrix, IViewCamera camera = null, float maxDepth = 2.0f) { IUILayoutArgs args = uiElement.GetArgs(); float max = (float)Math.Max(args.UIWidth, args.UIHeight); { //projectionMatrix = glm.ortho((float)args.left, (float)args.right, (float)args.bottom, (float)args.top, // TODO: / 2后与legacy opengl的UI元素显示就完全一致了。为什么??? projectionMatrix = glm.ortho((float)args.left / 2, (float)args.right / 2, (float)args.bottom / 2, (float)args.top / 2, uiElement.Param.zNear, uiElement.Param.zFar); // 下面注释掉的代码是用来测试legacy OpenGL的matrix与GLM库计算的matrix是否相同用的。已经证明了两者完全相同,此处仅作留念+以防万一。 //{ // float[] matrix = new float[16]; // GL.MatrixMode(GL.GL_PROJECTION); // GL.PushMatrix(); // GL.GetFloat(GetTarget.ProjectionMatrix, matrix); // GL.LoadIdentity(); // GL.GetFloat(GetTarget.ProjectionMatrix, matrix); // GL.Ortho(args.left / 2, args.right / 2, args.bottom / 2, args.top / 2, uiElement.Param.zNear, uiElement.Param.zFar); // GL.GetFloat(GetTarget.ProjectionMatrix, matrix);// this equals projectionMatrix // GL.PopMatrix(); //} // 把UI元素移到ortho长方体的最靠近camera的地方,这样就可以把UI元素放到OpenGL最前方。 projectionMatrix = glm.translate(projectionMatrix, new vec3(0, 0, uiElement.Param.zFar - max / 2 * maxDepth)); } { // UI元素不在三维场景中,所以其Camera可以是null。 if (camera == null) { //viewMatrix = glm.lookAt(new vec3(0, 0, 1), new vec3(0, 0, 0), new vec3(0, 1, 0)); viewMatrix = glm.lookAt( Camera.defaultPosition, Camera.defaultTarget, Camera.defaultUpVector); } else { vec3 position = camera.Position - camera.Target; position.Normalize(); viewMatrix = glm.lookAt(position, new vec3(0, 0, 0), camera.UpVector); } // 下面注释掉的代码是用来测试legacy OpenGL的matrix与GLM库计算的matrix是否相同用的。已经证明了两者完全相同,此处仅作留念+以防万一。 //{ // float[] matrix = new float[16]; // GL.MatrixMode(GL.GL_MODELVIEW); // GL.PushMatrix(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // GL.LoadIdentity(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // if(camera==null) // { // GL.gluLookAt(0, 0, 1, 0, 0, 0, 0, 1, 0); // } // else // { // vec3 position = camera.Position - camera.Target; // position.Normalize(); // GL.gluLookAt(position.x, position.y, position.z, 0, 0, 0, camera.UpVector.x, camera.UpVector.y, camera.UpVector.z); // } // GL.GetFloat(GetTarget.ModelviewMatix, matrix);// this equals viewMatrix // GL.PopMatrix(); //} } { modelMatrix = glm.scale(mat4.identity(), new vec3(args.UIWidth / 2, args.UIHeight / 2, max / 2)); // 下面注释掉的代码是用来测试legacy OpenGL的matrix与GLM库计算的matrix是否相同用的。已经证明了两者完全相同,此处仅作留念+以防万一。 //{ // float[] matrix = new float[16]; // GL.MatrixMode(GL.GL_MODELVIEW); // GL.PushMatrix(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // GL.LoadIdentity(); // GL.GetFloat(GetTarget.ModelviewMatix, matrix); // GL.Scale(args.UIWidth / 2, args.UIHeight / 2, max / 2); // GL.GetFloat(GetTarget.ModelviewMatix, matrix);// this equals modelMatrix // GL.PopMatrix(); //} } }
/// <summary> /// 根据UI元素的布局设定,计算其应有的宽高及其在ortho()中应有的参数。 /// </summary> /// <param name="uiElement"></param> /// <param name="viewportWidth"></param> /// <param name="viewportHeight"></param> /// <param name="args"></param> static void CalculateCoords(IUILayout uiElement, int viewportWidth, int viewportHeight, IUILayoutArgs args) { IUILayoutParam param = uiElement.Param; if ((param.Anchor & leftRightAnchor) == leftRightAnchor) { args.UIWidth = viewportWidth - param.Margin.Left - param.Margin.Right; if (args.UIWidth < 0) { args.UIWidth = 0; } } else { args.UIWidth = param.Size.Width; } if ((param.Anchor & topBottomAnchor) == topBottomAnchor) { args.UIHeight = viewportHeight - param.Margin.Top - param.Margin.Bottom; if (args.UIHeight < 0) { args.UIHeight = 0; } } else { args.UIHeight = param.Size.Height; } if ((param.Anchor & leftRightAnchor) == AnchorStyles.None) { args.left = -(args.UIWidth / 2 + (viewportWidth - args.UIWidth) * ((double)param.Margin.Left / (double)(param.Margin.Left + param.Margin.Right))); } else if ((param.Anchor & leftRightAnchor) == AnchorStyles.Left) { args.left = -(args.UIWidth / 2 + param.Margin.Left); } else if ((param.Anchor & leftRightAnchor) == AnchorStyles.Right) { args.left = -(viewportWidth - args.UIWidth / 2 - param.Margin.Right); } else // if ((Anchor & leftRightAnchor) == leftRightAnchor) { args.left = -(args.UIWidth / 2 + param.Margin.Left); } if ((param.Anchor & topBottomAnchor) == AnchorStyles.None) { args.bottom = -viewportHeight / 2; args.bottom = -(args.UIHeight / 2 + (viewportHeight - args.UIHeight) * ((double)param.Margin.Bottom / (double)(param.Margin.Bottom + param.Margin.Top))); } else if ((param.Anchor & topBottomAnchor) == AnchorStyles.Bottom) { args.bottom = -(args.UIHeight / 2 + param.Margin.Bottom); } else if ((param.Anchor & topBottomAnchor) == AnchorStyles.Top) { args.bottom = -(viewportHeight - args.UIHeight / 2 - param.Margin.Top); } else // if ((Anchor & topBottomAnchor) == topBottomAnchor) { args.bottom = -(args.UIHeight / 2 + param.Margin.Bottom); } }
/// <summary> /// /// </summary> /// <param name="anchor">the edges of the viewport to which a SimpleUIRect is bound and determines how it is resized with its parent. /// <para>something like AnchorStyles.Left | AnchorStyles.Bottom.</para></param> /// <param name="margin">the space between viewport and SimpleRect.</param> /// <param name="size">Stores width when <see cref="OpenGLUIRect.Anchor"/>.Left & <see cref="OpenGLUIRect.Anchor"/>.Right is <see cref="OpenGLUIRect.Anchor"/>.None. /// <para> and height when <see cref="OpenGLUIRect.Anchor"/>.Top & <see cref="OpenGLUIRect.Anchor"/>.Bottom is <see cref="OpenGLUIRect.Anchor"/>.None.</para></param> /// <param name="zNear"></param> /// <param name="zFar"></param> /// <param name="rectColor">default color is red.</param> public SimpleUICube(IUILayoutParam param) { IUILayout layout = this; layout.Param = param; }