Mesh with 1D Texture

The following code shows how to apply a 1D Texture to a Mesh entity.

 

 

        class MyPoint : Point3D
        {
            public MyPoint(double x, double y, double z, double t) : base(x, y, z)
            {
                Temperature = t;
            }

            public double Temperature;

        }

        private class MyMeshTex1D : Mesh
        {
            public MyMeshTex1D()
                : base(natureType.RichPlain)
            {
            }

            private TextureBase tex;

            private void CreateTexture1D(RenderContextBase renderContext)
            {
                Color[] colors = new Color[] { Color.Green, Color.White, Color.Red, Color.Yellow };

                tex = renderContext.CreateTexture1D(colors, textureFilteringFunctionType.Nearest, textureFilteringFunctionType.Nearest, false);
            }

            public override void Dispose()
            {
                tex.Dispose();
                base.Dispose();
            }

            public override void Compile(CompileParams data)
            {
                CreateTexture1D(data.RenderContext);

                base.Compile(data);
            }

            private bool prevTex1D;
            protected override void SetShader(DrawParams data)
            {
                // Tell the shader to set a Texture1D.
                if (data.ShaderParams != null && !data.Selected)
                {
                    prevTex1D = data.ShaderParams.Texture1D;
                    data.ShaderParams.Texture1D = true;
                }

                base.SetShader(data);
            }

            protected override void Render(RenderParams data)
            {
                if (data.ShaderParams != null && !data.Selected)
                    data.RenderContext.SetTexture(tex);

                base.Render(data);

                if (data.ShaderParams != null && !data.Selected)
                {
                    data.RenderContext.CloseTexture();
                    data.ShaderParams.Texture1D = prevTex1D;
                }

            }
        }

 

 Then you can use it line this:

            Mesh m = new MyMeshTex1D();

            Point3D[] pts = new Point3D[] { new MyPoint(0, 0, 0, 0), new MyPoint(20, 0, 0, 0), new MyPoint(20, 15, 0, 1), new MyPoint(0, 15, 0, 0) };
            IndexTriangle[] triangles = new IndexTriangle[] { new RichTriangle(0, 1, 2, 0, 1, 2), new RichTriangle(0, 2, 3, 0, 2, 3) };

            m.Vertices = pts;
            m.Triangles = triangles;

            // the second coordinate is unused because we are using Texture1D

            m.TextureCoords = new PointF[m.Vertices.Length];

            for (int i = 0; i < m.Vertices.Length; i++)
            {
                // The second coordinate is not used
                // Multiply by 0.75 to use just the first 3 colors
                m.TextureCoords[i] = new PointF((float)((MyPoint)m.Vertices[i]).Temperature, 0);
            }

            m.Color = Color.White;
            m.ColorMethod = colorMethodType.byEntity;

            viewportLayout1.Entities.Add(m);

            // sets rendered only display mode
            viewportLayout1.DisplayMode = displayType.Rendered;

            // sets trimetric view
            viewportLayout1.SetView(viewType.Trimetric);

            // fits the model in the viewport
            viewportLayout1.ZoomFit();

 

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Please sign in to leave a comment.