void OnGenerateVisualContent(MeshGenerationContext mgc)
            {
                Rect r = contentRect;

                if (r.width < 0.01f || r.height < 0.01f)
                {
                    return; // Skip rendering when too small.
                }
                Color color = resolvedStyle.color;

                k_Vertices[0].tint = Color.black;
                k_Vertices[1].tint = Color.black;
                k_Vertices[2].tint = color;
                k_Vertices[3].tint = color;

                float left   = 0;
                float right  = r.width;
                float top    = 0;
                float bottom = r.height;

                k_Vertices[0].position = new Vector3(left, bottom, Vertex.nearZ);
                k_Vertices[1].position = new Vector3(left, top, Vertex.nearZ);
                k_Vertices[2].position = new Vector3(right, top, Vertex.nearZ);
                k_Vertices[3].position = new Vector3(right, bottom, Vertex.nearZ);

                MeshWriteData mwd = mgc.Allocate(k_Vertices.Length, k_Indices.Length);

                mwd.SetAllVertices(k_Vertices);
                mwd.SetAllIndices(k_Indices);
            }
        public void GenerateTickHeadContent(MeshGenerationContext context)
        {
            const float tipY   = 20f;
            const float height = 6f;
            const float width  = 11f;
            const float middle = 6f;

            Color color;

            if (EditorGUIUtility.isProSkin)
            {
                color = m_DebugPlayhead ? ColorUtility.FromHtmlString("#234A6C") : Color.white;
            }
            else
            {
                color = m_DebugPlayhead ? ColorUtility.FromHtmlString("#2E5B8D") : Color.white;
            }

            MeshWriteData mesh = context.Allocate(3, 3);

            Vertex[] vertices = new Vertex[3];
            vertices[0].position = new Vector3(middle, tipY, Vertex.nearZ);
            vertices[1].position = new Vector3(width, tipY - height, Vertex.nearZ);
            vertices[2].position = new Vector3(0, tipY - height, Vertex.nearZ);

            vertices[0].tint = color;
            vertices[1].tint = color;
            vertices[2].tint = color;

            mesh.SetAllVertices(vertices);
            mesh.SetAllIndices(new ushort[] { 0, 2, 1 });
        }
            void OnGenerateVisualContent(MeshGenerationContext mgc)
            {
                Rect r = contentRect;

                if (r.width < 0.01f || r.height < 0.01f)
                {
                    return; // Skip rendering when too small.
                }
                float left   = 0;
                float right  = r.width;
                float top    = 0;
                float bottom = r.height;

                k_Vertices[0].position = new Vector3(left, bottom, Vertex.nearZ);
                k_Vertices[1].position = new Vector3(left, top, Vertex.nearZ);
                k_Vertices[2].position = new Vector3(right, top, Vertex.nearZ);
                k_Vertices[3].position = new Vector3(right, bottom, Vertex.nearZ);

                MeshWriteData mwd = mgc.Allocate(k_Vertices.Length, k_Indices.Length, m_Texture);

                // Remap 0..1 to the uv region.
                Rect uvs = mwd.uvRegion;

                k_Vertices[0].uv = new Vector2(uvs.xMin, uvs.yMin);
                k_Vertices[1].uv = new Vector2(uvs.xMin, uvs.yMax);
                k_Vertices[2].uv = new Vector2(uvs.xMax, uvs.yMax);
                k_Vertices[3].uv = new Vector2(uvs.xMax, uvs.yMin);

                mwd.SetAllVertices(k_Vertices);
                mwd.SetAllIndices(k_Indices);
            }
            void OnGenerateVisualContent(MeshGenerationContext mgc)
            {
                Rect r = contentRect;

                if (r.width < 0.01f || r.height < 0.01f)
                {
                    return; // Skip rendering when too small.
                }
                float radiusX = r.width / 2;
                float radiusY = r.height / 2;

                k_Vertices[0].position = new Vector3(radiusX, radiusY, Vertex.nearZ);

                float angle = 0;

                for (int i = 1; i < 7; ++i)
                {
                    k_Vertices[i].position = new Vector3(
                        radiusX + radiusX * Mathf.Cos(angle),
                        radiusY - radiusY * Mathf.Sin(angle),
                        Vertex.nearZ);
                    angle += 2f * Mathf.PI / 6;
                }

                MeshWriteData mwd = mgc.Allocate(k_Vertices.Length, k_Indices.Length);

                mwd.SetAllVertices(k_Vertices);
                mwd.SetAllIndices(k_Indices);
            }
        private static void MakeVectorGraphics9SliceBackground(Vertex[] svgVertices, ushort[] svgIndices, float svgWidth, float svgHeight, Rect targetRect, Vector4 sliceLTRB, bool stretch, Color tint, int settingIndexOffset, MeshBuilder.AllocMeshData meshAlloc)
        {
            MeshWriteData meshWriteData = meshAlloc.alloc((uint)svgVertices.Length, (uint)svgIndices.Length, ref meshAlloc);

            meshWriteData.SetAllIndices(svgIndices);
            bool flag = !stretch;

            if (flag)
            {
                throw new NotImplementedException("Support for repeating 9-slices is not done yet");
            }
            MeshBuilder.s_VectorGraphics9Slice.Begin();
            Rect    uvRegion = meshWriteData.uvRegion;
            int     num      = svgVertices.Length;
            Vector2 vector   = new Vector2(1f / (svgWidth - sliceLTRB.z - sliceLTRB.x), 1f / (svgHeight - sliceLTRB.w - sliceLTRB.y));
            Vector2 vector2  = new Vector2(targetRect.width - svgWidth, targetRect.height - svgHeight);

            for (int i = 0; i < num; i++)
            {
                Vertex  vertex = svgVertices[i];
                Vector2 vector3;
                vector3.x         = Mathf.Clamp01((vertex.position.x - sliceLTRB.x) * vector.x);
                vector3.y         = Mathf.Clamp01((vertex.position.y - sliceLTRB.y) * vector.y);
                vertex.position.x = vertex.position.x + vector3.x * vector2.x;
                vertex.position.y = vertex.position.y + vector3.y * vector2.y;
                vertex.uv.x       = vertex.uv.x * uvRegion.width + uvRegion.xMin;
                vertex.uv.y       = vertex.uv.y * uvRegion.height + uvRegion.yMin;
                vertex.tint      *= tint;
                uint num2 = (uint)(((int)vertex.opacityPageSVGSettingIndex.b << 8 | (int)vertex.opacityPageSVGSettingIndex.a) + settingIndexOffset);
                vertex.opacityPageSVGSettingIndex.b = (byte)(num2 >> 8);
                vertex.opacityPageSVGSettingIndex.a = (byte)num2;
                meshWriteData.SetNextVertex(vertex);
            }
            MeshBuilder.s_VectorGraphics9Slice.End();
        }
Exemple #6
0
        protected override void OnGenerateVisualContent(MeshGenerationContext cxt)
        {
            var         mp     = new MeshParts();
            List <int3> curTri = new List <int3>();

            Color[] clrByDepth = new Color[_subDiv + 1];
            for (int iClr = 0; iClr < clrByDepth.Length; iClr++)
            {
                float t = iClr / (float)(clrByDepth.Length);
                clrByDepth[iClr] = Color.HSVToRGB(t, 1, 1);
            }
            for (int iTriVtx = 0; iTriVtx < 3; iTriVtx++)
            {
                Vertex vtx = new Vertex();
                vtx.position = (Vector2)T_LsFromNs(TriangleMat[iTriVtx]);
                vtx.tint     = clrByDepth[_subDiv]; // it circular
                mp.vertices.Add(vtx);
            }
            curTri.Add(new int3(2, 1, 0));

            for (int iDiv = 0; iDiv < _subDiv; iDiv++)
            {
                List <int3> nxtTri = new List <int3>();
                foreach (var outerTri in curTri)
                {
                    int vtxOffset = mp.vertices.Count;
                    for (int iTriVtx = 0; iTriVtx < 3; iTriVtx++)
                    {
                        var pntLS = Vector3.zero;
                        for (int iEdge = 0; iEdge < 3; iEdge++)
                        {
                            pntLS += (iEdge == iTriVtx) ? Vector3.zero: mp.vertices[outerTri[iEdge]].position;
                        }
                        pntLS /= 2;
                        Vertex vtx = new Vertex();
                        vtx.position = pntLS;
                        vtx.tint     = clrByDepth[iDiv];
                        mp.vertices.Add(vtx);
                    }

                    for (int iTriVtx = 0; iTriVtx < 3; iTriVtx++)
                    {
                        // Broad cast it to xyz;
                        int3 newTri = outerTri[iTriVtx];
                        if (iTriVtx == 0)
                        {
                            newTri.yz = vtxOffset + new int2(2, 1);
                        }
                        else if (iTriVtx == 1)
                        {
                            newTri.xz = vtxOffset + new int2(2, 0);
                        }
                        else if (iTriVtx == 2)
                        {
                            newTri.xy = vtxOffset + new int2(1, 0);
                        }
                        nxtTri.Add(newTri);
                    }
                    nxtTri.Add(vtxOffset + new int3(0, 1, 2));
                }
                curTri = nxtTri;
            }
            foreach (var tri in curTri)
            {
                mp.triangles.Add(tri);
                //indices.AddRange(new ushort[] { (ushort)tri.z, (ushort)tri.y, (ushort)tri.x });
            }

            if (GeneBankManager.Inst && GeneBankManager.Inst.GenomeCount > 0)
            {
                ApplyGenomeColors(mp.vertices);
            }

            AddPoint(mp, T_NsFromBs(_value_bs), math.cmin(layout.size / 30), Color.white);

            MeshWriteData meshData = cxt.Allocate(mp.vertices.Count, mp.triangles.Count * 3);

            meshData.SetAllVertices(mp.vertices.ToArray());
            meshData.SetAllIndices(mp.GetIndices());
        }
        private void OnGenerateVisualContent(MeshGenerationContext cxt)
        {
            MeshParts            mp  = new MeshParts();
            MultiLayerPerception mlp = _TestMLP;

            AddRect(mp, 0, 1, Color.gray);
            if (mlp != null)
            {
                using (IWorker oneshotSyncWorker =
                           WorkerFactory.CreateWorker(_testMLP.model, _extraLayers, WorkerFactory.Device.GPU)) {
                    using (Tensor obsTensor = new Tensor(new TensorShape(1, mlp._shape.inputSize))) {
                        if (_observe.Length < mlp._shape.inputSize)
                        {
                            _observe = new float[mlp._shape.inputSize];
                        }
                        for (int iINode = 0; iINode < mlp._shape.inputSize; iINode++)
                        {
                            obsTensor[iINode] = _observe[iINode];
                        }

                        oneshotSyncWorker.Execute(obsTensor).FlushSchedule();
                    }

                    for (int iINode = 0; iINode < mlp._shape.inputSize; iINode++)
                    {
                        AddRect(mp, GetNodePos(0, iINode), NodeSize, ActNodeColor(_observe[iINode]));
                    }


                    using (Tensor hvr = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.Hidden)) {
                        using (Tensor hva = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.HiddenActive)) {
                            for (int iHNode = 0; iHNode < mlp._shape.hiddenSize; iHNode++)
                            {
                                AddRect(mp, GetNodePos(1, iHNode), NodeSize, RawNodeColor(hvr[iHNode]));
                                AddRect(mp, GetNodePos(1, iHNode) + new float2(0.5f, 0) * NodeSize, new float2(0.5f, 1) * NodeSize,
                                        ActNodeColor(hva[iHNode]));
                            }
                        }
                    }

                    using (Tensor ovr = oneshotSyncWorker.PeekOutput(MultiLayerPerception.LayerNames.Output)) {
                        using (Tensor ova = oneshotSyncWorker.PeekOutput()) {
                            for (int iONode = 0; iONode < mlp._shape.outputSize; iONode++)
                            {
                                AddRect(mp, GetNodePos(2, iONode), NodeSize, RawNodeColor(ovr[iONode]));
                                AddRect(mp, GetNodePos(2, iONode) + new float2(0.5f, 0) * NodeSize, new float2(0.5f, 1) * NodeSize,
                                        ActNodeColor(ova[iONode]));
                            }
                        }
                    }
                }

                string[] layerNames = new string[]
                { MultiLayerPerception.LayerNames.Hidden, MultiLayerPerception.LayerNames.Output };
                float2 xBuf = NodeSize / 2;
                xBuf.y = 0;
                int prvLayer = 0;
                int curLayer = 1;
                foreach (string layerName in layerNames)
                {
                    TensorShape tShape = _testMLP.GetLayerShape(layerName);
                    for (int iPNode = 0; iPNode < tShape.flatHeight; iPNode++)
                    {
                        for (int iCNode = 0; iCNode < tShape.flatWidth; iCNode++)
                        {
                            float2 posI = GetNodePos(prvLayer, iPNode) + NodeSize / 2;
                            float2 posW = GetNodePos(curLayer, iCNode) + NodeSize / 2;

                            float t = 0.5f + mlp.GetWeight(layerName, iPNode, iCNode);
                            DrawLine(mp, posI + xBuf, posW - xBuf, 0.025f, TurboColorMap.Map(t));
                        }
                    }

                    prvLayer = curLayer;
                    curLayer++;
                }
            }
            MeshWriteData meshData = cxt.Allocate(mp.vertices.Count, mp.IndicesCount);

            if (meshData.vertexCount > 0)
            {
                meshData.SetAllVertices(mp.vertices.ToArray());
                meshData.SetAllIndices(mp.GetIndices());
            }
        }
        internal static void MakeVectorGraphicsStretchBackground(Vertex[] svgVertices, ushort[] svgIndices, float svgWidth, float svgHeight, Rect targetRect, Rect sourceUV, ScaleMode scaleMode, Color tint, int settingIndexOffset, MeshBuilder.AllocMeshData meshAlloc, out int finalVertexCount, out int finalIndexCount)
        {
            Vector2 vector  = new Vector2(svgWidth * sourceUV.width, svgHeight * sourceUV.height);
            Vector2 vector2 = new Vector2(sourceUV.xMin * svgWidth, sourceUV.yMin * svgHeight);
            Rect    rect    = new Rect(vector2, vector);
            bool    flag    = sourceUV.xMin != 0f || sourceUV.yMin != 0f || sourceUV.width != 1f || sourceUV.height != 1f;
            float   num     = vector.x / vector.y;
            float   num2    = targetRect.width / targetRect.height;
            Vector2 vector3;
            Vector2 vector4;

            switch (scaleMode)
            {
            case ScaleMode.StretchToFill:
                vector3   = new Vector2(0f, 0f);
                vector4.x = targetRect.width / vector.x;
                vector4.y = targetRect.height / vector.y;
                break;

            case ScaleMode.ScaleAndCrop:
            {
                vector3 = new Vector2(0f, 0f);
                bool flag2 = num2 > num;
                if (flag2)
                {
                    vector4.x = (vector4.y = targetRect.width / vector.x);
                    float num3 = targetRect.height / vector4.y;
                    float num4 = rect.height / 2f - num3 / 2f;
                    vector3.y  -= num4 * vector4.y;
                    rect.y     += num4;
                    rect.height = num3;
                    flag        = true;
                }
                else
                {
                    bool flag3 = num2 < num;
                    if (flag3)
                    {
                        vector4.x = (vector4.y = targetRect.height / vector.y);
                        float num5 = targetRect.width / vector4.x;
                        float num6 = rect.width / 2f - num5 / 2f;
                        vector3.x -= num6 * vector4.x;
                        rect.x    += num6;
                        rect.width = num5;
                        flag       = true;
                    }
                    else
                    {
                        vector4.x = (vector4.y = targetRect.width / vector.x);
                    }
                }
                break;
            }

            case ScaleMode.ScaleToFit:
            {
                bool flag4 = num2 > num;
                if (flag4)
                {
                    vector4.x = (vector4.y = targetRect.height / vector.y);
                    vector3.x = (targetRect.width - vector.x * vector4.x) * 0.5f;
                    vector3.y = 0f;
                }
                else
                {
                    vector4.x = (vector4.y = targetRect.width / vector.x);
                    vector3.x = 0f;
                    vector3.y = (targetRect.height - vector.y * vector4.y) * 0.5f;
                }
                break;
            }

            default:
                throw new NotImplementedException();
            }
            MeshBuilder.s_VectorGraphicsStretch.Begin();
            vector3 -= vector2 * vector4;
            int num7 = svgVertices.Length;
            int num8 = svgIndices.Length;

            MeshBuilder.ClipCounts clipCounts = default(MeshBuilder.ClipCounts);
            Vector4 zero  = Vector4.zero;
            bool    flag5 = flag;

            if (flag5)
            {
                bool flag6 = rect.width <= 0f || rect.height <= 0f;
                if (flag6)
                {
                    finalVertexCount = (finalIndexCount = 0);
                    MeshBuilder.s_VectorGraphicsStretch.End();
                    return;
                }
                zero       = new Vector4(rect.xMin, rect.yMin, rect.xMax, rect.yMax);
                clipCounts = MeshBuilder.UpperBoundApproximateRectClippingResults(svgVertices, svgIndices, zero);
                num7      += clipCounts.clippedTriangles * 6;
                num8      += clipCounts.addedTriangles * 3;
                num8      -= clipCounts.degenerateTriangles * 3;
            }
            MeshWriteData meshWriteData = meshAlloc.alloc((uint)num7, (uint)num8, ref meshAlloc);
            bool          flag7         = flag;

            if (flag7)
            {
                MeshBuilder.RectClip(svgVertices, svgIndices, zero, meshWriteData, clipCounts, ref num7);
            }
            else
            {
                meshWriteData.SetAllIndices(svgIndices);
            }
            Debug.Assert(meshWriteData.currentVertex == 0);
            Rect uvRegion = meshWriteData.uvRegion;
            int  num9     = svgVertices.Length;

            for (int i = 0; i < num9; i++)
            {
                Vertex vertex = svgVertices[i];
                vertex.position.x = vertex.position.x * vector4.x + vector3.x;
                vertex.position.y = vertex.position.y * vector4.y + vector3.y;
                vertex.uv.x       = vertex.uv.x * uvRegion.width + uvRegion.xMin;
                vertex.uv.y       = vertex.uv.y * uvRegion.height + uvRegion.yMin;
                vertex.tint      *= tint;
                uint num10 = (uint)(((int)vertex.opacityPageSVGSettingIndex.b << 8 | (int)vertex.opacityPageSVGSettingIndex.a) + settingIndexOffset);
                vertex.opacityPageSVGSettingIndex.b = (byte)(num10 >> 8);
                vertex.opacityPageSVGSettingIndex.a = (byte)num10;
                meshWriteData.SetNextVertex(vertex);
            }
            for (int j = num9; j < num7; j++)
            {
                Vertex vertex2 = meshWriteData.m_Vertices[j];
                vertex2.position.x = vertex2.position.x * vector4.x + vector3.x;
                vertex2.position.y = vertex2.position.y * vector4.y + vector3.y;
                vertex2.uv.x       = vertex2.uv.x * uvRegion.width + uvRegion.xMin;
                vertex2.uv.y       = vertex2.uv.y * uvRegion.height + uvRegion.yMin;
                vertex2.tint      *= tint;
                uint num11 = (uint)(((int)vertex2.opacityPageSVGSettingIndex.b << 8 | (int)vertex2.opacityPageSVGSettingIndex.a) + settingIndexOffset);
                vertex2.opacityPageSVGSettingIndex.b = (byte)(num11 >> 8);
                vertex2.opacityPageSVGSettingIndex.a = (byte)num11;
                meshWriteData.SetNextVertex(vertex2);
            }
            finalVertexCount = meshWriteData.vertexCount;
            finalIndexCount  = meshWriteData.indexCount;
            MeshBuilder.s_VectorGraphicsStretch.End();
        }
        internal static void MakeSlicedQuad(ref MeshGenerationContextUtils.RectangleParams rectParams, float posZ, MeshBuilder.AllocMeshData meshAlloc)
        {
            MeshWriteData meshWriteData = meshAlloc.Allocate(16u, 54u);
            float         num           = 1f;
            Texture2D     texture2D     = rectParams.texture as Texture2D;
            bool          flag          = texture2D != null;

            if (flag)
            {
                num = texture2D.pixelsPerPoint;
            }
            float num2  = (float)rectParams.texture.width;
            float num3  = (float)rectParams.texture.height;
            float num4  = num / num2;
            float num5  = num / num3;
            float num6  = Mathf.Max(0f, (float)rectParams.leftSlice);
            float num7  = Mathf.Max(0f, (float)rectParams.rightSlice);
            float num8  = Mathf.Max(0f, (float)rectParams.bottomSlice);
            float num9  = Mathf.Max(0f, (float)rectParams.topSlice);
            float num10 = Mathf.Clamp(num6 * num4, 0f, 1f);
            float num11 = Mathf.Clamp(num7 * num4, 0f, 1f);
            float num12 = Mathf.Clamp(num8 * num5, 0f, 1f);
            float num13 = Mathf.Clamp(num9 * num5, 0f, 1f);

            MeshBuilder.k_TexCoordSlicesX[0] = rectParams.uv.min.x;
            MeshBuilder.k_TexCoordSlicesX[1] = rectParams.uv.min.x + num10;
            MeshBuilder.k_TexCoordSlicesX[2] = rectParams.uv.max.x - num11;
            MeshBuilder.k_TexCoordSlicesX[3] = rectParams.uv.max.x;
            MeshBuilder.k_TexCoordSlicesY[0] = rectParams.uv.max.y;
            MeshBuilder.k_TexCoordSlicesY[1] = rectParams.uv.max.y - num12;
            MeshBuilder.k_TexCoordSlicesY[2] = rectParams.uv.min.y + num13;
            MeshBuilder.k_TexCoordSlicesY[3] = rectParams.uv.min.y;
            Rect uvRegion = meshWriteData.uvRegion;

            for (int i = 0; i < 4; i++)
            {
                MeshBuilder.k_TexCoordSlicesX[i] = MeshBuilder.k_TexCoordSlicesX[i] * uvRegion.width + uvRegion.xMin;
                MeshBuilder.k_TexCoordSlicesY[i] = (rectParams.uv.min.y + rectParams.uv.max.y - MeshBuilder.k_TexCoordSlicesY[i]) * uvRegion.height + uvRegion.yMin;
            }
            float num14 = num6 + num7;
            bool  flag2 = num14 > rectParams.rect.width;

            if (flag2)
            {
                float num15 = rectParams.rect.width / num14;
                num6 *= num15;
                num7 *= num15;
            }
            float num16 = num8 + num9;
            bool  flag3 = num16 > rectParams.rect.height;

            if (flag3)
            {
                float num17 = rectParams.rect.height / num16;
                num8 *= num17;
                num9 *= num17;
            }
            MeshBuilder.k_PositionSlicesX[0] = rectParams.rect.x;
            MeshBuilder.k_PositionSlicesX[1] = rectParams.rect.x + num6;
            MeshBuilder.k_PositionSlicesX[2] = rectParams.rect.xMax - num7;
            MeshBuilder.k_PositionSlicesX[3] = rectParams.rect.xMax;
            MeshBuilder.k_PositionSlicesY[0] = rectParams.rect.yMax;
            MeshBuilder.k_PositionSlicesY[1] = rectParams.rect.yMax - num8;
            MeshBuilder.k_PositionSlicesY[2] = rectParams.rect.y + num9;
            MeshBuilder.k_PositionSlicesY[3] = rectParams.rect.y;
            for (int j = 0; j < 16; j++)
            {
                int num18 = j % 4;
                int num19 = j / 4;
                meshWriteData.SetNextVertex(new Vertex
                {
                    position = new Vector3(MeshBuilder.k_PositionSlicesX[num18], MeshBuilder.k_PositionSlicesY[num19], posZ),
                    uv       = new Vector2(MeshBuilder.k_TexCoordSlicesX[num18], MeshBuilder.k_TexCoordSlicesY[num19]),
                    tint     = rectParams.color
                });
            }
            meshWriteData.SetAllIndices(MeshBuilder.slicedQuadIndices);
        }