Skip to content

Commit

Permalink
Sphere vertex shader sample (#87)
Browse files Browse the repository at this point in the history
* 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
AVinitzca authored Sep 27, 2024
1 parent 49bfa86 commit 0bf9a9a
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 1 deletion.
6 changes: 6 additions & 0 deletions TGC.MonoGame.Samples/Content/Content.mgcb
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@
/processorParam:DebugMode=Auto
/build:Effects/SkyBox.fx

#begin Effects/SphereVertexShader.fx
/importer:EffectImporter
/processor:EffectProcessor
/processorParam:DebugMode=Auto
/build:Effects/SphereVertexShader.fx

#begin Effects/Terrain.fx
/importer:EffectImporter
/processor:EffectProcessor
Expand Down
65 changes: 65 additions & 0 deletions TGC.MonoGame.Samples/Content/Effects/SphereVertexShader.fx
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 TGC.MonoGame.Samples/Samples/Shaders/SphereVertexShader.cs
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);
}
}
2 changes: 1 addition & 1 deletion TGC.MonoGame.Samples/Viewer/TGCViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,8 @@ protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Background);
base.Draw(gameTime);
Model.DrawSampleExplorer(gameTime);
Gizmos.Draw();
Model.DrawSampleExplorer(gameTime);
}

/// <summary>
Expand Down

0 comments on commit 0bf9a9a

Please sign in to comment.