Example #1
0
#pragma warning disable CC0061 // Asynchronous methods should return a Task instead of void
        static async void Main()
#pragma warning restore CC0061 // Asynchronous methods should return a Task instead of void
        {
            if (!WebGL2RenderingContextBase.IsSupported)
            {
                HtmlHelper.AddParagraph("We are sorry, but your browser does not seem to support WebGL2.");
                HtmlHelper.AddParagraph("See the <a href=\"https://github.com/WaveEngine/WebGL.NET\">GitHub repo</a>.");
                return;
            }

            HtmlHelper.AddHeader1("WebGL.NET Samples Gallery");

            HtmlHelper.AddParagraph(
                "A collection of WebGL samples translated from .NET/C# into WebAssembly. " +
                "See the <a href=\"https://github.com/WaveEngine/WebGL.NET\">GitHub repo</a>.");

            samples = new ISample[]
            {
                new Triangle(),
                new RotatingCube(),
                new Texture2D(),
                new TexturedCubeFromHTMLImage(),
                new TexturedCubeFromAssets(),
                // TODO: Report issue with monolinker (remove Linker workaround project)
                new LoadGLTF(),
                new TransformFeedback(),
                new PointerLock(),
            };

            foreach (var sample in samples)
            {
                var sampleName = sample.GetType().Name;

                HtmlHelper.AddHeader2(sampleName);
                HtmlHelper.AddParagraph(sample.Description);

                var divCanvasName = $"div_canvas_{sampleName}";
                var canvasName    = $"canvas_{sampleName}";
                using (var canvas = HtmlHelper.AddCanvas(divCanvasName, canvasName, CanvasWidth, CanvasHeight))
                {
                    await sample.InitAsync(canvas, CanvasColor);

                    sample.Run();

                    if (sample.EnableFullScreen)
                    {
                        var fullscreenButtonName = $"fullscreen_{sampleName}";
                        HtmlHelper.AddButton(fullscreenButtonName, "Enter fullscreen");
                        AddFullScreenHandler(sample, fullscreenButtonName, divCanvasName, canvasName);
                    }
                }
            }

            AddEnterFullScreenHandler();

            AddGenerationStamp();

            RequestAnimationFrame();
        }
        public Task InitAsync(JSObject canvas, Vector4 clearColor)
        {
            this.clearColor = clearColor;
            this.canvas     = canvas;

            canvasWidth  = (int)canvas.GetObjectProperty("width");
            canvasHeight = (int)canvas.GetObjectProperty("height");

            HtmlHelper.AddButton(ButtonId, "Next");
            HtmlHelper.AttachButtonOnClickEvent(ButtonId, new Action <JSObject>(clickEvent =>
            {
                shouldDraw = true;
                clickEvent.Dispose();
            }));

            return(Task.CompletedTask);
        }