예제 #1
0
        private static TextureData LoadWhiteTextureData()
        {
            var texture = new RawTextureDataArray <RgbaFloat>(1, 1, RgbaFloat.SizeInBytes, PixelFormat.R32_G32_B32_A32_Float);

            texture.PixelData[0] = RgbaFloat.White;
            return(texture);
        }
예제 #2
0
파일: ParticleSystem.cs 프로젝트: zhuowp/ge
        private void InitializeContextObjects(RenderContext rc, MaterialCache materialCache, BufferCache bufferCache)
        {
            ResourceFactory factory = rc.ResourceFactory;

            _instanceDataVB = factory.CreateVertexBuffer(InstanceData.SizeInBytes * 10, true);
            _ib             = factory.CreateIndexBuffer(new[] { 0 }, false);

            if (_texture == null)
            {
                _texture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Pink);
            }

            _deviceTexture  = _texture.CreateDeviceTexture(factory);
            _textureBinding = factory.CreateShaderTextureBinding(_deviceTexture);

            _material = materialCache.GetMaterial(rc,
                                                  "passthrough-vertex", "billboard-geometry", "particle-fragment",
                                                  s_vertexInputs,
                                                  s_globalInputs,
                                                  s_perObjectInputs,
                                                  s_textureInputs);
            _depthStencilState = factory.CreateDepthStencilState(true, DepthComparison.LessEqual, true);

#if DEBUG_PARTICLE_BOUNDS
            var briwr = new BoundsRenderItemWireframeRenderer(this, rc);
            _gs.AddRenderItem(briwr, Transform);
#endif

            _initialized = true;
        }
예제 #3
0
        private void DropRandomObject()
        {
            _totalObjects++;
            _numBoxes++;

            var color = new RawTextureDataArray <RgbaFloat>(
                new RgbaFloat[] { new RgbaFloat((float)_random.NextDouble(), (float)_random.NextDouble(), (float)_random.NextDouble(), 1.0f) },
                1, 1, RgbaFloat.SizeInBytes, PixelFormat.R32_G32_B32_A32_Float);

            bool isBox = _random.NextDouble() <= 0.8;
            var  newGo = new GameObject((isBox ? "Cube" : "Sphere") + (_totalObjects));

            newGo.Transform.Position = new Vector3((float)_random.NextDouble() * 29f - 14f, (float)_random.NextDouble() * 10f, (float)_random.NextDouble() * 29f - 14f);
            var mr = isBox
                ? new MeshRenderer(new SimpleMeshDataProvider(CubeModel.Vertices, CubeModel.Indices), color)
                : new MeshRenderer(EngineEmbeddedAssets.SphereModelID, color);

            mr.Wireframe = _random.NextDouble() > 0.9;
            newGo.AddComponent(mr);
            float radius = 0.3f + (float)_random.NextDouble() * .75f;

            if (!isBox)
            {
                newGo.Transform.Scale = new Vector3(radius);
            }
            Collider collider = isBox ? (Collider) new BoxCollider(1f, 1f, 1f) : new SphereCollider(1.0f);

            newGo.AddComponent(collider);
            newGo.AddComponent(new TimedDeath(30.0f));
            newGo.Destroyed         += (go) => _numBoxes--;
            newGo.Transform.Rotation = Quaternion.CreateFromYawPitchRoll((float)_random.NextDouble() * 10f, 0f, 0f);
        }
예제 #4
0
        public WireframeShapeRenderer(RenderContext rc, RgbaFloat color)
        {
            _textureData = new RawTextureDataArray <RgbaFloat>(new RgbaFloat[] { color }, 1, 1, RgbaFloat.SizeInBytes, PixelFormat.R32_G32_B32_A32_Float);

            _worldProvider = new DynamicDataProvider <Matrix4x4>(Matrix4x4.Identity);
            _inverseTransposeWorldProvider = new DependantDataProvider <Matrix4x4>(_worldProvider, Utilities.CalculateInverseTranspose);
            _perObjectProviders            = new ConstantBufferDataProvider[] { _worldProvider, _inverseTransposeWorldProvider };

            InitializeContextObjects(rc);
        }
예제 #5
0
        public WireframeShapeRenderer(AssetDatabase ad, RenderContext rc, RgbaFloat color)
        {
            _materialAsset = ad.LoadAsset <MaterialAsset>("MaterialAsset/ShadowCaster_MtlTemplate.json");
            _textureData   = new RawTextureDataArray <RgbaFloat>(new RgbaFloat[] { color }, 1, 1, RgbaFloat.SizeInBytes, PixelFormat.R32_G32_B32_A32_Float);

            _worldProvider = new DynamicDataProvider <Matrix4x4>(Matrix4x4.Identity);
            _inverseTransposeWorldProvider = new DependantDataProvider <Matrix4x4>(_worldProvider, Utilities.CalculateInverseTranspose);
            var materialProvider = new DynamicDataProvider <ForwardRendering.MtlMaterialProperties>(new ForwardRendering.MtlMaterialProperties(new Vector3(4, 5, 6), 7));

            _perObjectProviders = new ConstantBufferDataProvider[] { _worldProvider, _inverseTransposeWorldProvider, materialProvider };

            InitializeContextObjects(ad, rc);
        }
예제 #6
0
파일: AxesRenderer.cs 프로젝트: zhuowp/ge
        public AxesRenderer(RenderContext rc, GraphicsSystem gs)
        {
            _gs = gs;
            _vb = rc.ResourceFactory.CreateVertexBuffer(6 * VertexPositionColor.SizeInBytes, false);

            const float opacity = 0.56f;
            RgbaFloat   red     = new RgbaFloat(1, 0, 0, opacity);
            RgbaFloat   green   = new RgbaFloat(0, 1, 0, opacity);
            RgbaFloat   blue    = new RgbaFloat(0, 0, 1, opacity);

            SetPlaneVertices(
                new Vector3(PlaneLength, 0, PlaneLength),
                new Vector3(PlaneLength, PlaneLength, 0),
                new Vector3(0, PlaneLength, PlaneLength));
            _ib = rc.ResourceFactory.CreateIndexBuffer(6 * 4, false);
            _ib.SetIndices(
                new int[]
            {
                0, 1, 2, 3, 4, 5,     // Lines
                // Planes
                6, 7, 8, 6, 8, 9,
                10, 11, 12, 10, 12, 13,
                14, 15, 16, 14, 16, 17,

                // Solid plane borders
                18, 19, 19, 20, 20, 21, 21, 18,
                22, 23, 23, 24, 24, 25, 25, 22,
                26, 27, 27, 28, 28, 29, 29, 26
            },
                0,
                0);
            _lineIndicesCount = 6;
            _material         = CreateMaterial(rc);

            _pointerVB       = ArrowPointerModel.MeshData.CreateVertexBuffer(rc.ResourceFactory);
            _pointerIB       = ArrowPointerModel.MeshData.CreateIndexBuffer(rc.ResourceFactory, out _pointerIndexCount);
            _pointerMaterial = CreatePointerMaterial(rc);
            _redTexture      = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Red).CreateDeviceTexture(rc.ResourceFactory);

            _greenTexture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Green).CreateDeviceTexture(rc.ResourceFactory);

            _blueTexture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Blue).CreateDeviceTexture(rc.ResourceFactory);

            _redBinding   = rc.ResourceFactory.CreateShaderTextureBinding(_redTexture);
            _greenBinding = rc.ResourceFactory.CreateShaderTextureBinding(_greenTexture);
            _blueBinding  = rc.ResourceFactory.CreateShaderTextureBinding(_blueTexture);

            _dss = rc.ResourceFactory.CreateDepthStencilState(false, DepthComparison.Always);
            _rs  = rc.ResourceFactory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true);
        }
예제 #7
0
        private void SaveDepthTextureToFile()
        {
            int width           = DepthMapWidth;
            int height          = DepthMapHeight;
            var cpuDepthTexture = new RawTextureDataArray <ushort>(width, height, sizeof(ushort), PixelFormat.Alpha_UInt16);

            _depthTexture.CopyTo(cpuDepthTexture);

            ImageSharp.Image image = new ImageSharp.Image(width, height);
            PixelFormatConversion.ConvertPixelsUInt16DepthToRgbaFloat(width * height, cpuDepthTexture.PixelData, image.Pixels);
            ImageSharpTexture rgbaDepthTexture = new ImageSharpTexture(image);

            Console.WriteLine($"Saving file: {width} x {height}, ratio:{(double)width / height}");
            rgbaDepthTexture.SaveToFile(Environment.TickCount + ".png");
        }
예제 #8
0
        private void SaveDepthTextureToFile()
        {
            int width           = DepthMapWidth;
            int height          = DepthMapHeight;
            var cpuDepthTexture = new RawTextureDataArray <ushort>(width, height, sizeof(ushort), PixelFormat.R16_UInt);

            _depthTexture.GetTextureData(0, cpuDepthTexture.PixelData);

            ImageSharp.Image <ImageSharp.Rgba32> image = new ImageSharp.Image <ImageSharp.Rgba32>(width, height);
            unsafe
            {
                fixed(ImageSharp.Rgba32 *pixelsPtr = &image.Pixels.DangerousGetPinnableReference())
                {
                    PixelFormatConversion.ConvertPixelsUInt16DepthToRgbaFloat(width * height, cpuDepthTexture.PixelData, pixelsPtr);
                }
            }
            ImageSharpTexture rgbaDepthTexture = new ImageSharpTexture(image);

            Console.WriteLine($"Saving file: {width} x {height}, ratio:{(double)width / height}");
            rgbaDepthTexture.SaveToFile(Environment.TickCount + ".png");
        }
예제 #9
0
파일: BallLauncher.cs 프로젝트: zhuowp/ge
        private void FireBoxForward()
        {
            Task.Run(() =>
            {
                var ball = new GameObject("Ball");
                var sc   = new SphereCollider(1.0f, .01f);
                ball.AddComponent(sc);
                sc.Entity.PositionUpdateMode = PositionUpdateMode.Continuous;
                var color = new RawTextureDataArray <RgbaFloat>(
                    new RgbaFloat[] { new RgbaFloat((float)_random.NextDouble(), (float)_random.NextDouble(), (float)_random.NextDouble(), 1.0f) },
                    1, 1, RgbaFloat.SizeInBytes, PixelFormat.R32_G32_B32_A32_Float);
                ball.AddComponent(new MeshRenderer(new SimpleMeshDataProvider(SphereModel.Vertices, SphereModel.Indices), color)
                {
                    Wireframe = _random.NextDouble() > .99
                });
                ball.Transform.Position  = Transform.Position + Transform.Forward * 1.0f;
                ball.Transform.Scale     = new Vector3(0.1f);
                sc.Entity.LinearVelocity = Transform.Forward * _launchSpeed;

                ball.AddComponent(new TimedDeath(5.0f));
            });
        }
예제 #10
0
        public unsafe void RecreateFontDeviceTexture(RenderContext rc)
        {
            var io = ImGui.GetIO();

            // Build
            _textureData = io.FontAtlas.GetTexDataAsRGBA32();
            int[] pixels = new int[_textureData.Width * _textureData.Height];
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = ((int *)_textureData.Pixels)[i];
            }

            _fontTexture = new RawTextureDataArray <int>(pixels, _textureData.Width, _textureData.Height, _textureData.BytesPerPixel, PixelFormat.R8_G8_B8_A8);

            // Store our identifier
            io.FontAtlas.SetTexID(_fontAtlasID);

            var deviceTexture = rc.ResourceFactory.CreateTexture(_fontTexture.PixelData, _textureData.Width, _textureData.Height, _textureData.BytesPerPixel, PixelFormat.R8_G8_B8_A8);

            _fontTextureBinding = rc.ResourceFactory.CreateShaderTextureBinding(deviceTexture);

            io.FontAtlas.ClearTexData();
        }
예제 #11
0
        private unsafe void CreateFontsTexture(RenderContext rc)
        {
            IO io = ImGui.GetIO();

            if (!string.IsNullOrEmpty(Preferences.Instance.MenuFont))
            {
                if (File.Exists(Preferences.Instance.MenuFont))
                {
                    float fontSize = Math.Max(4, Math.Min(48, Preferences.Instance.FontSize));
                    var   font     = ImGuiNative.ImFontAtlas_AddFontFromFileTTF(io.GetNativePointer()->FontAtlas, Preferences.Instance.MenuFont, fontSize, IntPtr.Zero, null);
                }
                else
                {
                    Console.WriteLine("Font listed in preferences doesn't exist: " + Preferences.Instance.MenuFont);
                }
            }
            else
            {
                ImGui.LoadDefaultFont();
            }

            // Build
            _textureData = io.FontAtlas.GetTexDataAsRGBA32();
            int[] pixels = new int[_textureData.Width * _textureData.Height];
            for (int i = 0; i < pixels.Length; i++)
            {
                pixels[i] = ((int *)_textureData.Pixels)[i];
            }

            _fontTexture = new RawTextureDataArray <int>(pixels, _textureData.Width, _textureData.Height, _textureData.BytesPerPixel, PixelFormat.R8_G8_B8_A8);

            // Store our identifier
            io.FontAtlas.SetTexID(_fontAtlasID);

            // Cleanup (don't clear the input data if you want to append new fonts later)
            io.FontAtlas.ClearTexData();
        }
예제 #12
0
파일: MeshRenderer.cs 프로젝트: nureyev/ge
        private async void InitializeContextObjects(RenderContext context, MaterialCache materialCache, BufferCache bufferCache)
        {
            ResourceFactory factory = context.ResourceFactory;

            Debug.Assert(_vb == null);
            Debug.Assert(_ib == null);
            Debug.Assert(_deviceTexture == null);
            Debug.Assert(_textureBinding == null);

            _vb = bufferCache.GetVertexBuffer(_mesh);
            CreateIndexBuffer(wasTransparent: false);

            if (s_regularGlobalInputs == null)
            {
                s_regularGlobalInputs = new MaterialInputs <MaterialGlobalInputElement>(
                    new MaterialGlobalInputElement[]
                {
                    new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "ProjectionMatrix"),
                    new MaterialGlobalInputElement("ViewMatrixBuffer", MaterialInputType.Matrix4x4, "ViewMatrix"),
                    new MaterialGlobalInputElement("LightProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "LightProjMatrix"),
                    new MaterialGlobalInputElement("LightViewMatrixBuffer", MaterialInputType.Matrix4x4, "LightViewMatrix"),
                    new MaterialGlobalInputElement("LightInfoBuffer", MaterialInputType.Custom, "LightBuffer"),
                    new MaterialGlobalInputElement("CameraInfoBuffer", MaterialInputType.Custom, "CameraInfo"),
                    new MaterialGlobalInputElement("PointLightsBuffer", MaterialInputType.Custom, "PointLights")
                });
            }

            _regularPassMaterial = materialCache.GetMaterial(
                context,
                RegularPassVertexShaderSource,
                RegularPassFragmentShaderSource,
                s_vertexInputs,
                s_regularGlobalInputs,
                s_perObjectInputs,
                s_textureInputs);

            _regularPassTransparentMaterial = materialCache.GetMaterial(
                context,
                RegularPassTransparentVertexShaderSource,
                RegularPassTransparentFragmentShaderSource,
                s_vertexInputs,
                s_regularGlobalInputs,
                s_transparentPerObjectInputs,
                s_transparentTextureInputs);

            if (_texture == null)
            {
                _texture = RawTextureDataArray <RgbaFloat> .FromSingleColor(RgbaFloat.Pink);
            }

            _deviceTexture = await _gs.ExecuteOnMainThread(() => _texture.CreateDeviceTexture(factory));

            _textureBinding = await _gs.ExecuteOnMainThread(() => factory.CreateShaderTextureBinding(_deviceTexture));

            if (s_shadowmapGlobalInputs == null)
            {
                s_shadowmapGlobalInputs = new MaterialInputs <MaterialGlobalInputElement>(
                    new MaterialGlobalInputElement[]
                {
                    new MaterialGlobalInputElement("ProjectionMatrixBuffer", MaterialInputType.Matrix4x4, "LightProjMatrix"),
                    new MaterialGlobalInputElement("ViewMatrixBuffer", MaterialInputType.Matrix4x4, "LightViewMatrix")
                });
            }

            _shadowPassMaterial = materialCache.GetMaterial(
                context,
                ShadowMapPassVertexShaderSource,
                ShadowMapPassFragmentShaderSource,
                s_vertexInputs,
                s_shadowmapGlobalInputs,
                s_shadowmapPerObjectInputs,
                MaterialTextureInputs.Empty);

            if (s_wireframeRS == null)
            {
                s_wireframeRS = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Wireframe, true, true);
            }
            if (s_noCullRS == null)
            {
                s_noCullRS = factory.CreateRasterizerState(FaceCullingMode.None, TriangleFillMode.Solid, true, true);
            }

            _initialized = true;
        }
예제 #13
0
        private static void AddObjectRainScene()
        {
            GameObject camera = new GameObject("Camera");

            camera.Transform.Position = new Vector3(0, 0, -5f);
            camera.Transform.Rotation = Quaternion.CreateFromYawPitchRoll(0.3f, 0.3f, 0f);
            camera.AddComponent(new Camera());

            GameObject light          = new GameObject("Light");
            var        lightComponent = new DirectionalLight(RgbaFloat.White, new Vector3(0.3f, -.3f, -1f));

            light.AddComponent(lightComponent);
            float timeFactor = 0.0f;

            light.AddComponent(new DelegateBehavior(
                                   dt =>
            {
                timeFactor  += dt;
                var position = new Vector3(
                    (float)(Math.Cos(timeFactor) * 5),
                    6 + (float)Math.Sin(timeFactor) * 2,
                    -(float)(Math.Sin(timeFactor) * 5));
                lightComponent.Direction = -position;
            }));

            GameObject sphere       = new GameObject("Sphere1");
            var        stoneTexture = Path.Combine("Textures", "Stone.png");

            sphere.AddComponent(new MeshRenderer(new SimpleMeshDataProvider(SphereModel.Vertices, SphereModel.Indices), stoneTexture));
            sphere.Transform.Position = new Vector3(0, 2f, 0f);
            sphere.Transform.Scale    = new Vector3(1f);
            sphere.AddComponent(new SphereCollider(1.0f, 1f));

            GameObject sphere2 = new GameObject("Sphere2");

            sphere2.AddComponent(new MeshRenderer(new SimpleMeshDataProvider(SphereModel.Vertices, SphereModel.Indices), stoneTexture));
            sphere2.Transform.Scale         = new Vector3(1f);
            sphere2.Transform.Parent        = sphere.Transform;
            sphere2.Transform.LocalPosition = new Vector3(-1f, -1f, 0f);
            sphere2.AddComponent(new SphereCollider(1.0f, 1f));

            GameObject sphere3 = new GameObject("Cube3");

            sphere3.AddComponent(new MeshRenderer(new SimpleMeshDataProvider(CubeModel.Vertices, CubeModel.Indices), stoneTexture));
            sphere3.Transform.Scale         = new Vector3(1f);
            sphere3.Transform.Parent        = sphere.Transform;
            sphere3.Transform.LocalPosition = new Vector3(1f, -1f, 0f);
            sphere3.AddComponent(new BoxCollider(1.0f, 1.0f, 1.0f, 1.0f));

            var solidBlue = new RawTextureDataArray <RgbaFloat>(
                new RgbaFloat[] { RgbaFloat.Blue },
                1, 1, RgbaFloat.SizeInBytes, PixelFormat.R32_G32_B32_A32_Float);

            GameObject upperTrigger = new GameObject("UpperTrigger");
            var        mr           = new MeshRenderer(new SimpleMeshDataProvider(CubeModel.Vertices, CubeModel.Indices), solidBlue)
            {
                Wireframe = true
            };

            upperTrigger.AddComponent(mr);
            upperTrigger.Transform.Parent        = sphere.Transform;
            upperTrigger.Transform.LocalPosition = new Vector3(0f, 2f, 0f);
            upperTrigger.Transform.Scale         = new Vector3(2.0f);
            var triggerBox = new BoxCollider(1.0f, 1.0f, 1.0f, .01f)
            {
                IsTrigger = true
            };

            upperTrigger.AddComponent(triggerBox);
            triggerBox.TriggerEntered += (other) => Console.WriteLine("Upper box triggered with " + other.GameObject.Name);

            GameObject plane = new GameObject("Plane");

            plane.Transform.Position = new Vector3(0, -3.5f, 0f);
            plane.Transform.Scale    = new Vector3(30f);
            plane.Transform.Rotation = Quaternion.CreateFromAxisAngle(Vector3.UnitZ, 0.05f);
            var woodTexture = Path.Combine("Textures", "Wood.png");

            plane.AddComponent(new MeshRenderer(new SimpleMeshDataProvider(PlaneModel.Vertices, PlaneModel.Indices), woodTexture));
            plane.AddComponent(new BoxCollider(1f, 0.1f / 30f, 1f, -1.0f));

            camera.AddComponent(new ObjectRain());
            camera.AddComponent(new FreeFlyMovement());
        }