Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Texture offscreenColorTexture = null;
            Texture offscreenDepthTexture = null;

            Filament.RenderTarget offscreenRenderTarget = null;
            View   offscreenView   = null;
            Scene  offscreenScene  = null;
            Camera offscreenCamera = null;

            int              lightEntity     = -1;
            Material         meshMaterial    = null;
            MaterialInstance meshMatInstance = null;
            Mesh             monkeyMesh      = null;
            int              reflectedMonkey = -1;
            Matrix4x4        transform       = Matrix4x4.Identity;

            int              quadEntity      = -1;
            Vector3          quadCenter      = Vector3.Zero;
            Vector3          quadNormal      = Vector3.Zero;
            VertexBuffer     quadVb          = null;
            IndexBuffer      quadIb          = null;
            Material         quadMaterial    = null;
            MaterialInstance quadMatInstance = null;
            ReflectionMode   mode            = ReflectionMode.Camera;

            var app = new Application(
                new WindowConfig()
            {
                Title = "rendertarget",
            },
                new ApplicationConfig()
                );

            app.Setup = (engine, view, scene) => {
                var tcm = engine.TransformManager;
                var rcm = engine.RenderableManager;
                var vp  = view.Viewport;

                var resourceData = new SampleDataLoader();
                var monkeyData   = new MonkeyDataLoader();

                // Instantiate offscreen render target.
                offscreenScene = engine.CreateScene();

                offscreenView       = engine.CreateView();
                offscreenView.Scene = offscreenScene;
                offscreenView.PostProcessingEnabled = false;

                offscreenColorTexture = TextureBuilder.Create()
                                        .WithWidth(vp.Width)
                                        .WithHeight(vp.Height)
                                        .WithLevels(1)
                                        .WithUsage(TextureUsage.ColorAttachment | TextureUsage.Sampleable)
                                        .WithFormat(TextureFormat.Rgba8)
                                        .Build(engine);

                offscreenDepthTexture = TextureBuilder.Create()
                                        .WithWidth(vp.Width)
                                        .WithHeight(vp.Height)
                                        .WithLevels(1)
                                        .WithUsage(TextureUsage.DepthAttachment)
                                        .WithFormat(TextureFormat.Depth24)
                                        .Build(engine);

                offscreenRenderTarget = RenderTargetBuilder.Create()
                                        .WithTexture(AttachmentPoint.Color, offscreenColorTexture)
                                        .WithTexture(AttachmentPoint.Depth, offscreenDepthTexture)
                                        .Build(engine);

                offscreenView.RenderTarget = offscreenRenderTarget;
                offscreenView.Viewport     = new Viewport(0, 0, vp.Width, vp.Height);

                offscreenCamera      = engine.CreateCamera(EntityManager.Create());
                offscreenView.Camera = offscreenCamera;

                app.AddOffscreenView(offscreenView);

                // Position and orient the mirror in an interesting way.
                var c = quadCenter = new Vector3(-2, 0, -5);
                var n = quadNormal = Vector3.Normalize(new Vector3(1, 0, 2));
                var u = Vector3.Normalize(Vector3.Cross(quadNormal, new Vector3(0, 1, 0)));
                var v = Vector3.Cross(n, u);
                u = 1.5f * u;
                v = 1.5f * v;

                Vertex[] kQuadVertices =
                {
                    new() { Position = c - u - v, Uv = new Vector2(1, 0) },
                    new() { Position = c + u - v, Uv = new Vector2(0, 0) },
                    new() { Position = c - u + v, Uv = new Vector2(1, 1) },
                    new() { Position = c + u + v, Uv = new Vector2(0, 1) },
                };

                var vbo = new byte[20 * 4];

                MemoryStream vboStream = new MemoryStream(vbo);
                BinaryWriter vboWriter = new BinaryWriter(vboStream);
                vboWriter.Write(kQuadVertices[0].Position.X);
                vboWriter.Write(kQuadVertices[0].Position.Y);
                vboWriter.Write(kQuadVertices[0].Position.Z);
                vboWriter.Write(kQuadVertices[0].Uv.X);
                vboWriter.Write(kQuadVertices[0].Uv.Y);
                vboWriter.Write(kQuadVertices[1].Position.X);
                vboWriter.Write(kQuadVertices[1].Position.Y);
                vboWriter.Write(kQuadVertices[1].Position.Z);
                vboWriter.Write(kQuadVertices[1].Uv.X);
                vboWriter.Write(kQuadVertices[1].Uv.Y);
                vboWriter.Write(kQuadVertices[2].Position.X);
                vboWriter.Write(kQuadVertices[2].Position.Y);
                vboWriter.Write(kQuadVertices[2].Position.Z);
                vboWriter.Write(kQuadVertices[2].Uv.X);
                vboWriter.Write(kQuadVertices[2].Uv.Y);
                vboWriter.Write(kQuadVertices[3].Position.X);
                vboWriter.Write(kQuadVertices[3].Position.Y);
                vboWriter.Write(kQuadVertices[3].Position.Z);
                vboWriter.Write(kQuadVertices[3].Uv.X);
                vboWriter.Write(kQuadVertices[3].Uv.Y);

                // Create quad vertex buffer.
                quadVb = VertexBufferBuilder.Create()
                         .WithVertexCount(4)
                         .WithBufferCount(1)
                         .WithAttribute(VertexAttribute.Position, 0, ElementType.Float3, 0, 20)
                         .WithAttribute(VertexAttribute.Uv0, 0, ElementType.Float2, 12, 20)
                         .Build(engine);
                quadVb.SetBufferAt(engine, 0, vbo);

                // Create quad index buffer.
                var kQuadIndices = new ushort[] { 0, 1, 2, 3, 2, 1 };

                quadIb = IndexBufferBuilder.Create()
                         .WithIndexCount(6)
                         .WithBufferType(IndexType.UShort)
                         .Build(engine);
                quadIb.SetBuffer(engine, kQuadIndices);

                // Create quad material and renderable.
                quadMaterial = MaterialBuilder.Create()
                               .WithPackage(resourceData.LoadMirror())
                               .Build(engine);
                quadMatInstance = quadMaterial.CreateInstance();

                var sampler = new TextureSampler(SamplerMinFilter.Linear, SamplerMagFilter.Linear);

                quadMatInstance.SetParameter("albedo", offscreenColorTexture, sampler);
                quadEntity = EntityManager.Create();

                RenderableBuilder.Create()
                .WithBoundingBox(
                    new Box(
                        new Vector3(-1, -1, -1),
                        new Vector3(1, 1, 1)
                        )
                    )
                .WithMaterial(0, quadMatInstance)
                .WithGeometry(0, PrimitiveType.Triangles, quadVb, quadIb, 0, 6)
                .WithCulling(false)
                .WithReceiveShadows(false)
                .WithCastShadows(false)
                .Build(engine, quadEntity);

                scene.AddEntity(quadEntity);

                // Instantiate mesh material.
                meshMaterial = MaterialBuilder.Create()
                               .WithPackage(resourceData.LoadAiDefaultMat())
                               .Build(engine);

                var mi = meshMatInstance = meshMaterial.CreateInstance();
                mi.SetParameter("baseColor", RgbType.Linear, new Color(0.8f, 1.0f, 1.0f));
                mi.SetParameter("metallic", 0.0f);
                mi.SetParameter("roughness", 0.4f);
                mi.SetParameter("reflectance", 0.5f);

                // Add monkey into the scene.
                monkeyMesh = MeshReader.LoadFromBuffer(engine, monkeyData.LoadSuzanne(), mi);

                var ti = tcm.GetInstance(monkeyMesh.Renderable);

                transform = Matrix4x4.CreateTranslation(0, 0, -4) * tcm.GetWorldTransform(ti);
                rcm.SetCastShadows(rcm.GetInstance(monkeyMesh.Renderable), false);
                scene.AddEntity(monkeyMesh.Renderable);

                // Create a reflected monkey, which is used only for ReflectionMode::RENDERABLES.
                reflectedMonkey = EntityManager.Create();

                RenderableBuilder.Create()
                .WithBoundingBox(
                    new Box(
                        new Vector3(-2, -2, -2),
                        new Vector3(2, 2, 2)
                        )
                    )
                .WithMaterial(0, mi)
                .WithGeometry(0, PrimitiveType.Triangles, monkeyMesh.VertexBuffer, monkeyMesh.IndexBuffer)
                .WithReceiveShadows(true)
                .WithCastShadows(false)
                .Build(engine, reflectedMonkey);
                mode = SetReflectionMode(offscreenScene, offscreenView, reflectedMonkey, monkeyMesh, ReflectionMode.Camera);

                // Add light source to both scenes.
                // NOTE: this is slightly wrong when the reflection mode is RENDERABLES.
                lightEntity = EntityManager.Create();

                LightBuilder.Create(LightType.Sun)
                .WithColor(Color.ToLinearAccurate(new sRGBColor(0.98f, 0.92f, 0.89f)))
                .WithIntensity(110000)
                .WithDirection(new Vector3(0.7f, -1, -0.8f))
                .WithSunAngularRadius(1.9f)
                .WithCastShadows(false)
                .Build(engine, lightEntity);

                scene.AddEntity(lightEntity);
                offscreenScene.AddEntity(lightEntity);
            };
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Material         material         = null;
            MaterialInstance materialInstance = null;
            Texture          normal           = null;
            Texture          albedo           = null;
            Texture          ao        = null;
            Texture          metallic  = null;
            Texture          roughness = null;
            Mesh             mesh      = null;
            Matrix4x4        transform;

            var app = new Application(
                new WindowConfig()
            {
                Title = "suzanne",
            },
                new ApplicationConfig()
            {
                IblDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), IBL_FOLDER)
            }
                );

            app.Setup = (engine, view, scene) => {
                var tcm = engine.TransformManager;
                var rcm = engine.RenderableManager;

                var monkeyData = new MonkeyDataLoader();

                // Create textures. The KTX bundles are freed by KtxUtility.
                var albedoBundle    = new KtxBundle(monkeyData.LoadAlbedoS3tc());
                var aoBundle        = new KtxBundle(monkeyData.LoadAo());
                var metallicBundle  = new KtxBundle(monkeyData.LoadMetallic());
                var roughnessBundle = new KtxBundle(monkeyData.LoadRougness());
                albedo    = KtxUtility.CreateTexture(engine, albedoBundle, true);
                ao        = KtxUtility.CreateTexture(engine, aoBundle, false);
                metallic  = KtxUtility.CreateTexture(engine, metallicBundle, false);
                roughness = KtxUtility.CreateTexture(engine, roughnessBundle, false);
                normal    = LoadNormalMap(engine, monkeyData.LoadNormal());

                var resourceData = new SampleDataLoader();
                var sampler      = new TextureSampler(SamplerMinFilter.LinearMipmapLinear, SamplerMagFilter.Linear);

                // Instantiate material.
                material = MaterialBuilder.Create()
                           .WithPackage(resourceData.LoadTexturedLit())
                           .Build(engine);

                materialInstance = material.CreateInstance();
                materialInstance.SetParameter("albedo", albedo, sampler);
                materialInstance.SetParameter("ao", ao, sampler);
                materialInstance.SetParameter("metallic", metallic, sampler);
                materialInstance.SetParameter("normal", normal, sampler);
                materialInstance.SetParameter("roughness", roughness, sampler);

                var indirectLight = app.Ibl.IndirectLight;
                indirectLight.Intensity = 100000;
                indirectLight.Rotation  = Matrix4x4.CreateFromAxisAngle(Vector3.UnitY, 0.5f);

                // Add geometry into the scene.
                mesh = MeshReader.LoadFromBuffer(engine, monkeyData.LoadSuzanne(), materialInstance);
                var ti = tcm.GetInstance(mesh.Renderable);
                transform = tcm.GetWorldTransform(ti) * Matrix4x4.CreateTranslation(0, 0, -4);

                rcm.SetCastShadows(rcm.GetInstance(mesh.Renderable), false);
                scene.AddEntity(mesh.Renderable);
                tcm.SetTransform(ti, transform);
            };

            app.Cleanup = (engine, view, scene) => {
                engine.Destroy(materialInstance);
                engine.Destroy(mesh.Renderable);
                engine.Destroy(material);
                engine.Destroy(albedo);
                engine.Destroy(normal);
                engine.Destroy(roughness);
                engine.Destroy(metallic);
                engine.Destroy(ao);
            };

            app.Run();
        }