Start of 3d renderer integration

This commit is contained in:
Simon Lübeß
2022-05-04 18:38:59 +02:00
parent 412d0ea965
commit 2817c5b1a1
16 changed files with 733 additions and 14 deletions
Binary file not shown.
@@ -0,0 +1,73 @@
cbuffer SceneConstants
{
float4x4 ViewProjection = float4x4(1, 0, 0, 0,
0, 1, 0, 0,
0, 0, 1, 0,
0, 0, 0, 1);
}
cbuffer ObjectConstants
{
float4x4 Transform;
}
cbuffer Constants
{
float4 BaseColor = float4(1, 0, 1, 1);
//float3 LightDir = float3(0, 1, 0);
}
struct VS_IN
{
float3 Position : POSITION;
float3 Normal : NORMAL;
};
struct PS_IN
{
float4 Position : SV_POSITION;
float3 WorldPosition : TEXCOORD0;
float3 Normal : NORMAL;
};
PS_IN VS(VS_IN input)
{
PS_IN output;
float4 worldPosition = mul(Transform, float4(input.Position, 1));
output.Position = mul(ViewProjection, worldPosition);
output.WorldPosition = worldPosition.xyz / worldPosition.w;
output.Normal = mul((float3x3)Transform, input.Normal);
return output;
}
struct PS_OUT
{
float4 Color : SV_TARGET0;
float4 Normal : SV_TARGET1;
float4 Position : SV_TARGET2;
};
PS_OUT PS(PS_IN input)
{
input.Normal = normalize(input.Normal);
float f = distance(input.Normal, input.Normal);
//float shading = dot(LightDir, input.Normal);
//float shading = dot(LightDir, LightDir) + 1;
//shading = clamp(shading, 0.0f, 1.0f);
//return float4(BaseColor.rgb * shading, 1.0f);
PS_OUT output = (PS_OUT)0;
output.Color = BaseColor;
output.Normal = float4(input.Normal, 1);
output.Position = float4(input.WorldPosition, 1);
return output;
}
#effect[VS=VS,PS=PS]
@@ -0,0 +1,46 @@
Texture2D Colors : register(t0);
Texture2D<float3> Normals : register(t1);
Texture2D Positions : register(t2);
SamplerState Sampler : register(s0);
cbuffer Constants
{
float3 LightDir = float3(0, 1, 0);
}
struct VS_IN
{
float2 Position : POSITION;
float2 TexCoord : TEXCOORD0;
};
struct PS_IN
{
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD;
};
PS_IN VS(VS_IN input)
{
PS_IN output;
output.Position = float4(input.Position, 0, 1);
output.TexCoord = input.TexCoord;
return output;
}
float4 PS(PS_IN input) : SV_TARGET
{
float4 color = Colors.Sample(Sampler, input.TexCoord);
float3 normal = Normals.Sample(Sampler, input.TexCoord);
//float shading = dot(LightDir, normal) + 100;
float shading = dot(float3(0, 1, 0), normal);
shading = clamp(shading, 0.0f, 1.0f);
return float4(color.rgb * shading, 1);
}
#effect[VS=VS,PS=PS]
@@ -0,0 +1,52 @@
Texture2D Texture : register(t0);
SamplerState Sampler : register(s0);
cbuffer Constants
{
float4x4 ViewProjection;
float ColorOffset = 0.5f;
float AlphaOffset = 0.0f;
float ColorScale = 0.5f;
float AlphaScale = 1.0f;
float2 TextureSizeInPixels;
}
struct VS_Input
{
float2 Position : POSITION;
float2 Texcoord : TEXCOORD0;
float4x4 Transform : TRANSFORM;
float4 Color : COLOR;
float4 UVTransform : TEXCOORD1;
};
struct PS_Input
{
float4 Position : SV_Position;
float2 Texcoord : TEXCOORD0;
float4 Color : COLOR;
};
PS_Input VS(VS_Input input)
{
PS_Input output;
output.Position = mul(ViewProjection, mul(input.Transform, float4(input.Position, 0.0f, 1.0f)));
output.Texcoord = input.UVTransform.xy + input.UVTransform.zw * input.Texcoord;
output.Color = input.Color;
return output;
}
float4 PS(PS_Input input) : SV_Target0
{
float4 color = Texture.Sample(Sampler, input.Texcoord);
float4 final = float4(ColorOffset.xxx, AlphaOffset) + color * float4(ColorScale.xxx, AlphaScale);
return final;
}
#effect[VS=VS, PS=PS]
+34
View File
@@ -7,6 +7,9 @@ using GlitchyEngine.ImGui;
using GlitchyEngine.Math;
using GlitchyEngine.Renderer;
using GlitchyEngine.World;
using GlitchyEngine.Content;
using System.Collections;
using GlitchyEngine.Renderer.Animation;
namespace GlitchyEditor
{
@@ -83,6 +86,7 @@ namespace GlitchyEditor
camera.Camera.SetPerspective(MathHelper.ToRadians(75), 0.1f, 10000.0f);
camera.Primary = true;
camera.FixedAspectRatio = false;
camera.RenderTarget = _viewportTarget;
let transform = _cameraEntity.GetComponent<TransformComponent>();
transform.Position = .(0, 0, -5);
@@ -96,6 +100,7 @@ namespace GlitchyEditor
camera.Camera.SetPerspective(MathHelper.ToRadians(45), 0.1f, 1000.0f);
camera.Primary = false;
camera.FixedAspectRatio = false;
camera.RenderTarget = _viewportTarget;
let transform = _otherCameraEntity.GetComponent<TransformComponent>();
transform.Position = .(0, 0, -5);
@@ -104,6 +109,31 @@ namespace GlitchyEditor
}
InitEditor();
TestEntitiesWithModels();
}
private void TestEntitiesWithModels()
{
Effect myEffect = new Effect("content/Shaders/myEffect.hlsl")..ReleaseRefNoDelete();
Material mat = new Material(myEffect)..ReleaseRefNoDelete();
mat.SetVariable("BaseColor", Vector4(1, 0, 1, 1));
//mat.SetVariable("LightDir", Vector3(1, 1, 0).Normalized());
List<AnimationClip> clips = scope .();
ModelLoader.LoadModel("content/Models/plane.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips);
mat = new Material(myEffect)..ReleaseRefNoDelete();
mat.SetVariable("BaseColor", Vector4(1, 1, 0, 1));
//mat.SetVariable("LightDir", Vector3(0, 1, 0).Normalized());
clips = scope .();
ModelLoader.LoadModel("content/Models/plane.glb", myEffect, mat, _scene.[Friend]_ecsWorld, clips);
ClearAndReleaseItems!(clips);
}
private void InitGraphics()
@@ -184,9 +214,13 @@ namespace GlitchyEditor
}
ImGui.ID _mainDockspaceId;
TextureViewer viewer = new TextureViewer() ~ delete _;
private bool OnImGuiRender(ImGuiRenderEvent event)
{
viewer.ViewTexture(Renderer.[Friend]_gBuffer.Normal);
ImGui.Begin("Test");
static bool cameraA = true;
+246
View File
@@ -0,0 +1,246 @@
using GlitchyEngine.Renderer;
using GlitchyEngine;
using ImGui;
using GlitchyEngine.Math;
using System;
namespace GlitchyEditor
{
class TextureViewer
{
enum BackgroundMode : int32
{
White,
Black,
Checkerboard
}
enum SampleMode : int32
{
Point,
Linear
}
GraphicsContext _context ~ _.ReleaseRef();
Effect _effect ~ _.ReleaseRef();
float _zoom = 1.0f;
BackgroundMode _backgroundMode = .Checkerboard;
SampleMode _sampleMode = .Linear;
RenderTarget2D _target ~ _?.ReleaseRef();
// TODO: we don't need depth!
DepthStencilTarget _depth ~ _?.ReleaseRef();
SamplerState _samplerPoint ~ _.ReleaseRef();
SamplerState _samplerLinear ~ _.ReleaseRef();
public this()
{
_context = Renderer.[Friend]_context..AddRef();
InitEffect();
InitState();
// TODO: rasterizerstate and depthstencilstate
}
private void InitEffect()
{
_effect = new Effect("content\\Shaders\\textureViewerShader.hlsl");
}
private void InitState()
{
SamplerStateDescription desc = .();
desc.MagFilter = .Linear;
desc.MinFilter = .Linear;
_samplerLinear = SamplerStateManager.GetSampler(desc);
desc.MagFilter = .Point;
desc.MinFilter = .Point;
_samplerPoint = SamplerStateManager.GetSampler(desc);
}
Vector2 _position;
bool _moving;
float _colorOffset = 0;
float _colorScale = 1;
float _alphaOffset = 0;
float _alphaScale = 1;
public void ViewTexture(Texture viewedTexture)
{
ImGui.Begin("Texture Viewer");
ImGui.SliderFloat("Zoom", &_zoom, 0.01f, 100.0f);
char8*[] items = scope .("White", "Black", "Checkerboard");
ImGui.Combo("Background", (.)&_backgroundMode, items.Ptr, (.)items.Count);
items = scope .("Point", "Linear");
ImGui.Combo("Sampler", (.)&_sampleMode, items.Ptr, (.)items.Count);
ImGui.SliderFloat2("Color offset and scale", *(float[2]*)&_colorOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Alpha offset and scale", *(float[2]*)&_alphaOffset, -1.0f, 1.0f);
ImGui.SliderFloat2("Position", *(float[2]*)&_position, 2 * -Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom, 2 * Math.Max(viewedTexture.Width, viewedTexture.Height) * _zoom);
ImGui.BeginChild("imageChild");
UpdateInput();
var viewportSize = ImGui.GetContentRegionAvail();
viewportSize.x = Math.Max(viewportSize.x, 1);
viewportSize.y = Math.Max(viewportSize.y, 1);
if(_target == null || viewportSize.x != _target.Width || viewportSize.y != _target.Height)
{
_target?.ReleaseRef();
_target = new RenderTarget2D(.(.R8G8B8A8_UNorm, (.)viewportSize.x, (.)viewportSize.y));
_depth?.ReleaseRef();
_depth = new DepthStencilTarget((.)viewportSize.x, (.)viewportSize.y, .D16_UNorm);
}
RenderTexture(viewedTexture);
ImGui.Image(_target, viewportSize);
ImGui.EndChild();
ImGui.End();
}
float lastWheel;
private void UpdateInput()
{
var windowPos = ImGui.GetWindowPos();
var mousePos = ImGui.GetIO().MousePos;
Vector2 mouseInWindow = .(mousePos.x - windowPos.x, mousePos.y - windowPos.y);
bool windowHovered = ImGui.IsWindowHovered();
if(windowHovered && Input.IsMouseButtonPressing(.MiddleButton))
{
_moving = true;
}
else if(Input.IsMouseButtonReleased(.MiddleButton))
{
_moving = false;
}
if(windowHovered || _moving)
{
float mouseWheel = ImGui.GetIO().MouseWheel;
float delta = mouseWheel - lastWheel;
if(delta != 0)
{
_position -= mouseInWindow;
_position /= _zoom;
_zoom *= Math.Pow(1.1f, delta);
_position *= _zoom;
_position += mouseInWindow;
}
}
if(_moving)
{
Point movement = Input.GetMouseMovement();
_position.X += movement.X;
_position.Y += movement.Y;
}
}
OrthographicCamera _camera = new OrthographicCamera() ~ delete _;
private void RenderTexture(Texture viewedTexture)
{
Viewport vp = .(0, 0, _target.Width, _target.Height);
RenderCommand.SetViewport(vp);
// TODO: don't clear pink!
RenderCommand.Clear(_target, .Pink);
RenderCommand.Clear(_depth, .Depth, 1.0f, 0);
//_target.Bind();
_context.SetRenderTarget(_target);
_context.SetDepthStencilTarget(_depth);
_context.BindRenderTargets();
Vector2 textureSize = Vector2(viewedTexture.Width, viewedTexture.Height);
Vector2 zoomedTextureSize = textureSize * _zoom;
Vector2 targetSize = Vector2(_target.Width, _target.Height);
_effect.Variables["ColorOffset"].SetData(_colorOffset);
_effect.Variables["ColorScale"].SetData(_colorScale);
_effect.Variables["AlphaOffset"].SetData(_alphaOffset);
_effect.Variables["AlphaScale"].SetData(_alphaScale);
_camera.Left = 0;
_camera.Top = 0;
_camera.Right = targetSize.X;
_camera.Bottom = -targetSize.Y;
_camera.NearPlane = -5;
_camera.FarPlane = 5;
_camera.Update();
Renderer2D.BeginScene(_camera);
switch(_backgroundMode)
{
case .Black:
Renderer2D.DrawQuadPivotCorner(Vector3(0, 0, 1), targetSize, 0, .Black);
case .White:
Renderer2D.DrawQuadPivotCorner(Vector3(0, 0, 1), targetSize, 0, .White);
case .Checkerboard:
float quadSize = 50.0f;
Vector2 numQuads = (targetSize / 500f) * 10f;
for(float x = 0; x < numQuads.X; x++)
{
for(float y = 0; y < numQuads.Y; y++)
{
Renderer2D.DrawQuadPivotCorner(Vector3(x * quadSize, -y * quadSize, 1), quadSize.XX, 0, ((x + y) % 2 == 0) ? .White : .Gray);
}
}
break;
}
Renderer2D.EndScene();
var sampler = viewedTexture.SamplerState;
switch(_sampleMode)
{
case .Point:
viewedTexture.SamplerState = _samplerPoint;
case .Linear:
viewedTexture.SamplerState = _samplerLinear;
}
Renderer2D.BeginScene(_camera, .SortByTexture, _effect);
Renderer2D.DrawQuad(Vector3(_position * .(1, -1), 0), zoomedTextureSize, 0, viewedTexture);
Renderer2D.EndScene();
viewedTexture.SamplerState = sampler;
}
}
}