public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();

        GUILayout.Space(10);

        LightBuilder spawningArea = (LightBuilder)target;

        if (GUILayout.Button("BuildLight"))
        {
            EditorUtility.SetDirty(target);
            spawningArea.BuildLight();
        }

        if (GUILayout.Button("ClearLight"))
        {
            EditorUtility.SetDirty(target);
            spawningArea.ClearLight();
        }
        if (GUILayout.Button("ActivateLight"))
        {
            EditorUtility.SetDirty(target);
            spawningArea.ActivateLight();
        }
        if (GUILayout.Button("DeactivateLight"))
        {
            EditorUtility.SetDirty(target);
            spawningArea.DeactivateLight();
        }
    }
Beispiel #2
0
        private void glControl_ContextCreated(object sender, GlControlEventArgs e)
        {
            Control senderControl = (Control)sender;

            _previousMousPosition = new Point(senderControl.ClientSize.Width / 2, senderControl.ClientSize.Height / 2);

            Gl.Enable(EnableCap.DepthTest);

            string vertexShader =
                File.ReadAllText(
                    $"{GlobalConfig.CurrentDirectory}/{GlobalConfig.ShadersDirectory}/{GlobalConfig.FigureVertexShader}");
            string fragmentShader =
                File.ReadAllText(
                    $"{GlobalConfig.CurrentDirectory}/{GlobalConfig.ShadersDirectory}/{GlobalConfig.FigureFragmentShader}");

            _figureShaderProgram = new FigureShaderProgram(new[] { vertexShader }, new[] { fragmentShader });

            _light      = LightBuilder.CreateWhiteLight(LightType.Ambient);
            _light.Type = _defaultLightType;

            _standardFigure          = ModelBuilder.CreateCube(new Vector3(0f, 0f, 0f), _figureShaderProgram.BindAttributes);
            _standardFigure.Material = _defaultMaterial;

            _singleObjectScene = new SingleObjectScene(new Vector3(0f, 0f, 3f));
            _singleObjectScene.AddFigure(_standardFigure);

            _multipleObjectsSceneStartInCenter = new MultipleRandomObjectsScene(new MultipleRandomObjectsScene.Args
            {
                CameraPosition    = new Vector3(0f, 0f, 0f),
                MinObjectPostion  = new Vector3(-50f, -50f, -50f),
                MaxObjectPosition = new Vector3(50f, 50f, 50f),
                ObjectsCount      = 10000
            });
            _multipleObjectsSceneStartInCenter.AddFigure(_standardFigure);

            _multipleObjectsSceneStartOutside = new MultipleRandomObjectsScene(new MultipleRandomObjectsScene.Args
            {
                CameraPosition    = new Vector3(60f, 60f, 60f),
                MinObjectPostion  = new Vector3(-50f, -50f, -50f),
                MaxObjectPosition = new Vector3(50f, 50f, 50f),
                ObjectsCount      = 10000
            });
            _multipleObjectsSceneStartOutside.AddFigure(_standardFigure);

            _currentScene  = _singleObjectScene;
            _currentFigure = _standardFigure;
        }
Beispiel #3
0
        private void glControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (_isCameraMoveEnabled)
            {
                switch (e.KeyCode)
                {
                case Keys.A:
                    _currentScene.MoveCamera(CameraMove.Left, _cameraMoveStep);
                    break;

                case Keys.D:
                    _currentScene.MoveCamera(CameraMove.Right, _cameraMoveStep);
                    break;

                case Keys.W:
                    _currentScene.MoveCamera(CameraMove.Forward, _cameraMoveStep);
                    break;

                case Keys.S:
                    _currentScene.MoveCamera(CameraMove.Backward, _cameraMoveStep);
                    break;
                }
            }

            if (e.Modifiers == Keys.Control)
            {
                switch (e.KeyCode)
                {
                case Keys.D1:
                    _light = LightBuilder.CreateWhiteLight(_light.Type);
                    break;

                case Keys.D2:
                    _light = LightBuilder.CreateDimWhiteLight(_light.Type);
                    break;

                case Keys.F1:
                    _currentScene = _singleObjectScene;
                    break;

                case Keys.F2:
                    _currentScene = _multipleObjectsSceneStartInCenter;
                    break;

                case Keys.F3:
                    _currentScene = _multipleObjectsSceneStartOutside;
                    break;

                case Keys.O:
                    ImportFigure();
                    break;

                case Keys.P:
                    SetStandardFigure();
                    break;
                }
            }
            else
            {
                switch (e.KeyCode)
                {
                case Keys.NumPad4:
                    _currentScene.RotateFigures(FigureRotation.OY, -_figureRotationAngle);
                    break;

                case Keys.NumPad6:
                    _currentScene.RotateFigures(FigureRotation.OY, _figureRotationAngle);
                    break;

                case Keys.NumPad8:
                    _currentScene.RotateFigures(FigureRotation.OX, -_figureRotationAngle);
                    break;

                case Keys.NumPad2:
                    _currentScene.RotateFigures(FigureRotation.OX, _figureRotationAngle);
                    break;

                case Keys.NumPad7:
                    _currentScene.RotateFigures(FigureRotation.OZ, _figureRotationAngle);
                    break;

                case Keys.NumPad9:
                    _currentScene.RotateFigures(FigureRotation.OZ, -_figureRotationAngle);
                    break;

                case Keys.Add:
                    _currentFigure.Transform(_scaleIncreaseSize);
                    break;

                case Keys.Subtract:
                    _currentFigure.Transform(_scaleDecreaseSize);
                    break;

                case Keys.Space:
                    _isCameraMoveEnabled = !_isCameraMoveEnabled;
                    break;

                case Keys.D1:
                    _currentFigure.Material = MaterialType.Gold;
                    break;

                case Keys.D2:
                    _currentFigure.Material = MaterialType.Silver;
                    break;

                case Keys.D3:
                    _currentFigure.Material = MaterialType.Bronze;
                    break;

                case Keys.D4:
                    _currentFigure.Material = MaterialType.GreenRubber;
                    break;

                case Keys.D5:
                    _currentFigure.Material = MaterialType.RedPlastic;
                    break;

                case Keys.D6:
                    _currentFigure.Material = MaterialType.Obsidian;
                    break;

                case Keys.D7:
                    _currentFigure.Material = MaterialType.Emerald;
                    break;

                case Keys.D8:
                    _currentFigure.Material = MaterialType.Ruby;
                    break;

                case Keys.D9:
                    _currentFigure.Material = MaterialType.Brass;
                    break;

                case Keys.D0:
                    _currentFigure.Material = MaterialType.Chrome;
                    break;

                case Keys.OemMinus:
                    _currentFigure.Material = MaterialType.Copper;
                    break;

                case Keys.Oemplus:
                    _currentFigure.Material = MaterialType.Pearl;
                    break;

                case Keys.F1:
                    _light.Type = LightType.Ambient;
                    break;

                case Keys.F2:
                    _light.Type = LightType.Directional;
                    break;

                case Keys.F3:
                    _light.Type = LightType.Point;
                    break;

                case Keys.F4:
                    _light.Type = LightType.Spotlight;
                    break;
                }
            }

            glControl.Refresh();
        }
        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);
            };