-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added sample and inverted the order of drawing for Gizmos and UI * Added files * Removed unnecessary code in shader * Fixed indentation in file and comments in spanish
- Loading branch information
Showing
4 changed files
with
154 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
TGC.MonoGame.Samples/Content/Effects/SphereVertexShader.fx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#if OPENGL | ||
#define SV_POSITION POSITION | ||
#define VS_SHADERMODEL vs_3_0 | ||
#define PS_SHADERMODEL ps_3_0 | ||
#else | ||
#define VS_SHADERMODEL vs_4_0_level_9_1 | ||
#define PS_SHADERMODEL ps_4_0_level_9_1 | ||
#endif | ||
|
||
// Custom Effects - https://docs.monogame.net/articles/content/custom_effects.html | ||
// High-level shader language (HLSL) - https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl | ||
// Programming guide for HLSL - https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-pguide | ||
// Reference for HLSL - https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-reference | ||
// HLSL Semantics - https://docs.microsoft.com/en-us/windows/win32/direct3dhlsl/dx-graphics-hlsl-semantics | ||
|
||
uniform float4x4 World; | ||
uniform float4x4 View; | ||
uniform float4x4 Projection; | ||
uniform float Time = 0; | ||
|
||
struct VertexShaderInput | ||
{ | ||
// Local Space Position | ||
float4 Position : POSITION0; | ||
float3 Normal : NORMAL0; | ||
}; | ||
|
||
struct VertexShaderOutput | ||
{ | ||
// Projection Space Position | ||
float4 Position : SV_POSITION; | ||
}; | ||
|
||
VertexShaderOutput MainVS(in VertexShaderInput input) | ||
{ | ||
VertexShaderOutput output = (VertexShaderOutput)0; | ||
|
||
input.Position.x += 0.05 * sin(input.Position.y * 10.0 + Time * 10.0); | ||
|
||
// Local to World | ||
float4 worldPosition = mul(input.Position, World); | ||
|
||
// World to View | ||
float4 viewPosition = mul(worldPosition, View); | ||
|
||
// View to Projection | ||
output.Position = mul(viewPosition, Projection); | ||
|
||
return output; | ||
} | ||
|
||
float4 MainPS(VertexShaderOutput input) : COLOR | ||
{ | ||
return float4(0.0, 0.0, 0.0, 1.0); | ||
} | ||
|
||
technique BaseTechnique | ||
{ | ||
pass P0 | ||
{ | ||
VertexShader = compile VS_SHADERMODEL MainVS(); | ||
PixelShader = compile PS_SHADERMODEL MainPS(); | ||
} | ||
}; | ||
|
82 changes: 82 additions & 0 deletions
82
TGC.MonoGame.Samples/Samples/Shaders/SphereVertexShader.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
using System; | ||
using Microsoft.Xna.Framework; | ||
using Microsoft.Xna.Framework.Graphics; | ||
using TGC.MonoGame.Samples.Cameras; | ||
using TGC.MonoGame.Samples.Geometries; | ||
using TGC.MonoGame.Samples.Viewer; | ||
|
||
namespace TGC.MonoGame.Samples.Samples.Shaders; | ||
|
||
/// <summary> | ||
/// Sphere Basic Shader: | ||
/// Units Involved: | ||
/// # Unit 8 - Video Adapters - Shaders. | ||
/// A basic vertex shader on a sphere. | ||
/// Author: Ronán Vinitzca | ||
/// </summary> | ||
public class SphereVertexShader : TGCSample | ||
{ | ||
private Camera _camera; | ||
|
||
private Effect _effect; | ||
|
||
private SpherePrimitive _sphere; | ||
|
||
public SphereVertexShader(TGCViewer game) : base(game) | ||
{ | ||
Category = TGCSampleCategory.Shaders; | ||
Name = "SphereVertexShader"; | ||
Description = "Basic Sample for demonstrating Vertex Shaders."; | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
_camera = new StaticCamera(GraphicsDevice.Viewport.AspectRatio, Vector3.Forward * 4f, Vector3.Backward, Vector3.Up); | ||
base.Initialize(); | ||
} | ||
|
||
protected override void LoadContent() | ||
{ | ||
_sphere = new SpherePrimitive(GraphicsDevice, 2f, 32); | ||
|
||
// Load a shader using Content pipeline. | ||
_effect = Game.Content.Load<Effect>(ContentFolderEffects + "SphereVertexShader"); | ||
|
||
base.LoadContent(); | ||
} | ||
|
||
public override void Update(GameTime gameTime) | ||
{ | ||
_camera.Update(gameTime); | ||
|
||
Game.Gizmos.UpdateViewProjection(_camera.View, _camera.Projection); | ||
|
||
base.Update(gameTime); | ||
} | ||
|
||
public override void Draw(GameTime gameTime) | ||
{ | ||
Game.Background = Color.CornflowerBlue; | ||
|
||
// Draw using wireframe and store old rasterizer state | ||
var oldRasterizerState = GraphicsDevice.RasterizerState; | ||
var rasterizerState = new RasterizerState(); | ||
rasterizerState.CullMode = CullMode.None; | ||
rasterizerState.FillMode = FillMode.WireFrame; | ||
GraphicsDevice.RasterizerState = rasterizerState; | ||
|
||
var time = Convert.ToSingle(gameTime.TotalGameTime.TotalSeconds); | ||
|
||
_effect.Parameters["Time"]?.SetValue(time); | ||
_effect.Parameters["World"].SetValue(Matrix.Identity); | ||
_effect.Parameters["View"].SetValue(_camera.View); | ||
_effect.Parameters["Projection"].SetValue(_camera.Projection); | ||
|
||
_sphere.Draw(_effect); | ||
|
||
// Restore old rasterizer state | ||
GraphicsDevice.RasterizerState = oldRasterizerState; | ||
|
||
base.Draw(gameTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters