Ejemplo n.º 1
0
        private void DrawTMX(int x, int y)
        {
            var demo = (DemoReel)RB.Game;

            if (mMap != null)
            {
                RB.Offscreen(spriteSheet3);

                RB.DrawRectFill(new Rect2i(0, 0, RB.DisplaySize.width, RB.DisplaySize.height), DemoUtil.IndexToRGB(22));

                RB.DrawMapLayer(0);
                RB.DrawMapLayer(1);

                RB.Offscreen(spriteSheet4);
                RB.Clear(new Color32(0, 0, 0, 0));
                RB.SpriteSheetSet(spriteSheet2);
                RB.DrawSprite(0, new Vector2i((int)mBouncePos.x, (int)mBouncePos.y), mVelocity.x > 0 ? RB.FLIP_H : 0);

                RB.Onscreen();

                RB.ShaderSet(shader1);
                shader1.SpriteSheetTextureSet("Mask", spriteSheet4);
                shader1.FloatSet("Wave", RB.Ticks / 10.0f);
                shader1.SpriteSheetFilterSet(spriteSheet4, RB.Filter.Linear);

                RB.SpriteSheetSet(spriteSheet3);
                RB.DrawCopy(new Rect2i(0, 0, RB.DisplaySize.width, RB.DisplaySize.height), Vector2i.zero);

                RB.ShaderReset();

                RB.SpriteSheetSet(spriteSheet1);
            }
            else
            {
                RB.Print(new Vector2i(x, y + 250), DemoUtil.IndexToRGB(14), "Failed to load TMX map.\nPlease try re-importing the map Demos/DemoReel/Tilemap.tmx in Unity");
            }

            string shaderName = "WavyMaskShader";

            mFormatStr.Set("@C// Custom shaders can be used for many things, like masking!\n");
            mFormatStr.Append("@NmyShader.Load(@S\"Demos/DemoReel/").Append(shaderName).Append("\"@N);\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("@C// Draw a tilemap to one offscreen surface\n");
            mFormatStr.Append("@[email protected](myOffscreenSurface1);\n");
            mFormatStr.Append("@[email protected](@Knew @MRect2i@N(@L0@N, @L0@N,\n");
            mFormatStr.Append("   @[email protected], @[email protected]),\n");
            mFormatStr.Append("   @I22@N);\n");
            mFormatStr.Append("@[email protected](@L0@N);\n");
            mFormatStr.Append("@[email protected](@L1@N);\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("@C// Draw a mask to the other offscreen surface\n");
            mFormatStr.Append("@[email protected](myOffscreenSurface2);\n");
            mFormatStr.Append("@[email protected](@Knew @MColor32@N(@L0@N, @L0@N, @L0@N, @L0@N));\n");
            mFormatStr.Append("@[email protected](@NmyGhostMaskSpriteSheet);\n");
            mFormatStr.Append("@[email protected](@L0@N, @Knew @MVector2i@N(@L").Append((int)mBouncePos.x).Append("@N, @L").Append((int)mBouncePos.y).Append("@N)").Append(mVelocity.x > 0 ? ", RB.FLIP_H" : string.Empty).Append(");\n");

            mFormatStr.Append("\n");
            mFormatStr.Append("@C// Use a custom shader to combine the two!\n");
            mFormatStr.Append("@[email protected]();\n");
            mFormatStr.Append("@[email protected](@NmyShader);\n");
            mFormatStr.Append("@NmyShader.SpriteSheetTextureSet(@S\"Mask\"@N, myOffscreenSurface2);\n");
            mFormatStr.Append("@NmyShader.FloatSet(@S\"Wave\"@N, @L").Append(RB.Ticks / 10.0f, 2).Append("f@N);\n");
            mFormatStr.Append("@NmyShader.SpriteSheetFilterSet(myOffscreenSurface2, @MRB@N.@[email protected]);\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("@[email protected](@NmyOffscreenSurface1);\n");
            mFormatStr.Append("@[email protected](@Knew @MRect2i@N(@L0@N, @L0@N,\n   @[email protected], @[email protected]),\n   @[email protected]);\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("@[email protected]();\n");
            DemoUtil.HighlightCode(mFormatStr, mCodeStr);

            mFormatStr.Set("@C// This shader multiplies in a mask and applies a wavy effect!\n");
            mFormatStr.Append("@KShader@N \"Unlit/").Append(shaderName).Append("\" {\n");
            mFormatStr.Append("  @KSubShader@N {\n");
            mFormatStr.Append("    @C...\n");
            mFormatStr.Append("    @KPass@N {\n");
            mFormatStr.Append("      @C...\n");
            mFormatStr.Append("      @C/*** Insert custom shader variables here ***/\n");
            mFormatStr.Append("      @Ksampler2D@N Mask;\n");
            mFormatStr.Append("      @Kfloat@N Wave;\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("      @Nfrag_in vert(vert_in v, @Kout float4@N screen_pos : @MSV_POSITION@N) {\n");
            mFormatStr.Append("        @C...@N\n");
            mFormatStr.Append("      }\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("      @Kfloat4@N frag(frag_in i, @MUNITY_VPOS_TYPE@N screen_pos : @MVPOS@N) : @MSV_Target@N {\n");
            mFormatStr.Append("        @C...\n");
            mFormatStr.Append("        @C/*** Insert custom fragment shader code here ***/@N\n");
            mFormatStr.Append("        @C// Sample the mask texture@N\n");
            mFormatStr.Append("        i.uv.x += sin(Wave + i.uv.y * @L8@N) * @L0.025@N;\n");
            mFormatStr.Append("        i.uv.y += cos(Wave - i.uv.x * @L8@N) * @L0.015@N;\n");
            mFormatStr.Append("        @Kfloat4@N mask_color = @Mtex2D@N(Mask, i.uv).rgba;\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("        @C// Multiply the sprite pixel by mask color@N\n");
            mFormatStr.Append("        @Kreturn@N sprite_pixel_color * mask_color;\n");
            mFormatStr.Append("      }\n");
            mFormatStr.Append("    }\n");
            mFormatStr.Append("  }\n");
            mFormatStr.Append("}\n");
            DemoUtil.HighlightCode(mFormatStr, mShaderStr);

            RB.Print(new Vector2i(x, y), DemoUtil.IndexToRGB(5), mCodeStr);
            RB.Print(new Vector2i(x + 300, y), DemoUtil.IndexToRGB(5), mShaderStr);
        }
        /// <summary>
        /// Render
        /// </summary>
        public override void Render()
        {
            var demo = (DemoReel)RB.Game;

            if (mMap != null)
            {
                RB.Clear(DemoUtil.IndexToRGB(22));
                RB.DrawMapLayer(0);
                RB.DrawMapLayer(1);
            }
            else
            {
                RB.Print(new Vector2i(2, 210), DemoUtil.IndexToRGB(14), "Failed to load TMX map.\nPlease try re-importing the map Demos/DemoReel/Tilemap.tmx in Unity");
            }

            RB.EffectShader(mShader1);
            mShader1.FloatSet("Wave", RB.Ticks / 25.0f);

            RB.DrawRectFill(new Rect2i(0, 0, RB.DisplaySize.width, 200), DemoUtil.IndexToRGB(1));

            string shaderName = "PresentRippleShader";

            mFormatStr.Set("@C// Custom post-processing shader\n");
            mFormatStr.Append("@NmyShader.Load(@S\"Demos/DemoReel/").Append(shaderName).Append("\"@N);\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("@[email protected](@L0@N);\n");
            mFormatStr.Append("@[email protected](@L1@N);\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("@[email protected](myShader);\n");
            mFormatStr.Append("@[email protected](myShader, @S\"Wave\"@N, @L").Append(RB.Ticks / 25.0f, 2).Append("f@N);\n");
            mFormatStr.Append("@[email protected](@MRB@N.@[email protected]);\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("@[email protected]();\n");
            mFormatStr.Append("@[email protected]();\n");
            DemoUtil.HighlightCode(mFormatStr, mCodeStr);

            mFormatStr.Set("@C// This creates a wavy effect!\n");
            mFormatStr.Append("@KShader@N \"Unlit/").Append(shaderName).Append("\" {\n");
            mFormatStr.Append("  @KSubShader@N {\n");
            mFormatStr.Append("    @C...\n");
            mFormatStr.Append("    @KPass@N {\n");
            mFormatStr.Append("      @C...\n");
            mFormatStr.Append("      @C/*** Insert custom shader variables here ***/\n");
            mFormatStr.Append("      @Kfloat@N Wave;\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("      @Nfrag_in vert(appdata v) {\n");
            mFormatStr.Append("        @C...@N\n");
            mFormatStr.Append("      }\n");
            mFormatStr.Append("\n");
            mFormatStr.Append("      @Kfloat4@N frag(v2f i) : @MSV_Target@N {\n");
            mFormatStr.Append("        @C/*** Insert custom fragment shader code here ***/@N\n");
            mFormatStr.Append("        @Kfloat2@N centerOffset = @L-1.0@N + @L2.0@N * i.uv.xy;\n");
            mFormatStr.Append("        @Kfloat@N len = @Klength@N(centerOffset);\n");
            mFormatStr.Append("        i.uv.xy += (centerOffset / len) * cos(len * @L10.0@N - Wave) * @L0.005@N;\n");
            mFormatStr.Append("        @C...@N\n");
            mFormatStr.Append("        @Kreturn@N color;\n");
            mFormatStr.Append("      }\n");
            mFormatStr.Append("    }\n");
            mFormatStr.Append("  }\n");
            mFormatStr.Append("}\n");
            DemoUtil.HighlightCode(mFormatStr, mShaderStr);

            RB.Print(new Vector2i(4, 4), DemoUtil.IndexToRGB(0), mCodeStr);
            RB.Print(new Vector2i(304, 4), DemoUtil.IndexToRGB(0), mShaderStr);
        }