Skip to content

Commit

Permalink
Adding basic diffuse and ambient lighting (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
gmriggs authored Nov 29, 2018
1 parent b6744d5 commit 6b38dd9
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 3 deletions.
81 changes: 79 additions & 2 deletions DatExplorer/Content/texture.fx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ float xPointSpriteSizeX;
float xPointSpriteSizeY;
float xOpacity;
float3x3 xAlphaRot;
float xAmbient;
float3 xLightDirection;

Texture2DArray xOverlays;
Texture2DArray xAlphas;
Expand Down Expand Up @@ -54,6 +56,7 @@ struct VertexShaderOutput
float4 Position : SV_POSITION;
//float3 Normal : TEXCOORD0;
float4 Color : COLOR0;
float LightingFactor : COLOR1;
float2 TextureCoord : TEXCOORD0;
float2 AlphaCoord : TEXCOORD1;
};
Expand All @@ -80,6 +83,7 @@ struct PSCombined
BaseData Base;
TerrainData Terrains;
RoadData Roads;
float LightingFactor : COLOR1;
};

//------- Technique: ColoredNoShading --------
Expand Down Expand Up @@ -114,11 +118,79 @@ technique ColoredNoShading
}
}

//------- Technique: Textured ----------

VertexShaderOutput TexturedVS(float4 iPos : SV_POSITION, float3 iNormal : NORMAL, float2 iTexCoord: TEXCOORD0)
{
VertexShaderOutput output = (VertexShaderOutput) 0;
float4 worldPos = mul(iPos, xWorld);
float4 viewPos = mul(worldPos, xView);
output.Position = mul(viewPos, xProjection);

output.TextureCoord = iTexCoord;

float3 normal = normalize(mul(iNormal, xWorld));
output.LightingFactor = dot(normal, -xLightDirection);

return output;
}

PixelShaderOutput TexturedPS(VertexShaderOutput input)
{
PixelShaderOutput output = (PixelShaderOutput) 0;

output.Color = xTexture.Sample(TextureSampler, input.TextureCoord);

// only output completely opaque pixels
clip(output.Color.a < 1.0f ? -1 : 1);

output.Color.rgb *= saturate(input.LightingFactor) + xAmbient;

return output;
}

PixelShaderOutput TexturedTransPS(VertexShaderOutput input)
{
PixelShaderOutput output = (PixelShaderOutput) 0;

output.Color = xTexture.Sample(TextureSampler, input.TextureCoord);

// only output semi-transparent pixels
clip(output.Color.a < 1.0f && output.Color.a >= 0.03125f ? 1 : -1);
//clip(output.Color.a < 1.0f ? 1 : -1);

output.Color.rgb *= saturate(input.LightingFactor) + xAmbient;

return output;
}

technique Textured
{
pass Pass0
{
ZWriteEnable = true;

VertexShader = compile vs_4_0 TexturedVS();
PixelShader = compile ps_4_0 TexturedPS();
}
pass Pass1
{
ZWriteEnable = false;

AlphaBlendEnable = true;
DestBlend = InvSrcAlpha;
SrcBlend = SrcAlpha;

VertexShader = compile vs_4_0 TexturedVS();
PixelShader = compile ps_4_0 TexturedTransPS();
}
}

//------- Technique: TexturedNoShading --------

VertexShaderOutput TexturedNoShadingVS(float4 iPos : SV_POSITION, float2 iTexCoord : TEXCOORD0)
{
VertexShaderOutput output = (VertexShaderOutput)0;
VertexShaderOutput output = (VertexShaderOutput) 0;

float4 worldPos = mul(iPos, xWorld);
float4 viewPos = mul(worldPos, xView);
Expand Down Expand Up @@ -389,6 +461,9 @@ PSCombined LandscapeSinglePassVS(VertexShaderInput input)

output.Roads = roads;

float3 normal = normalize(mul(input.Normal, xWorld));
output.LightingFactor = dot(normal, -xLightDirection);

return output;
}

Expand Down Expand Up @@ -420,7 +495,9 @@ float4 LandscapeSinglePassPS(PSCombined input) : COLOR
float3 r1 = saturate(((1 - a2) * a1) * c1.rgb);
float3 r2 = a2 * c2.rgb;

return float4(r0 + r1 + r2, 1);
float4 color = float4(r0 + r1 + r2, 1);
color.rgb *= saturate(input.LightingFactor) + xAmbient;
return color;
//return c1;

//return maskBlend3(b0, c1, c2, 1, ht, hr);
Expand Down
Binary file modified DatExplorer/Content/texture.mgfxo
Binary file not shown.
14 changes: 14 additions & 0 deletions DatExplorer/Physics/Common/LandblockStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,17 @@ public void ConstructPolygons(uint landblockID)
}
}

public void ConstructNormals()
{
foreach (var polygon in Polygons)
foreach (var vertex in polygon.Vertices)
vertex.Normal += polygon.Plane.Normal;

foreach (var polygon in Polygons)
foreach (var vertex in polygon.Vertices)
vertex.Normal = Vector3.Normalize(vertex.Normal);
}

public void ConstructUVs(uint landblockID)
{
for (uint x = 0; x < SidePolyCount; x++)
Expand Down Expand Up @@ -399,7 +410,10 @@ public bool Generate(uint landblockID, int cellScale, LandDefs.Direction transAd
ConstructPolygons(landblockID);

if (!PhysicsEngine.Instance.Server)
{
ConstructNormals();
ConstructUVs(landblockID); // client mode only
}
}

CalcWater();
Expand Down
5 changes: 4 additions & 1 deletion DatExplorer/Render/Buffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ public static void SetRasterizerState(CullMode cullMode = CullMode.CullClockwise
public void Draw()
{
Effect.Parameters["xWorld"].SetValue(Matrix.Identity);
Effect.Parameters["xLightDirection"].SetValue(-Vector3.UnitZ);
Effect.Parameters["xAmbient"].SetValue(0.5f);

DrawTerrain();

Expand All @@ -243,7 +245,8 @@ public void DrawBuffer(Dictionary<uint, RenderBatch> batches)
var cullMode = WorldViewer.Instance.DungeonMode ? CullMode.CullClockwiseFace : CullMode.None;

SetRasterizerState(cullMode); // todo: neg uv indices
Effect.CurrentTechnique = Effect.Techniques["TexturedNoShading"];
//Effect.CurrentTechnique = Effect.Techniques["TexturedNoShading"];
Effect.CurrentTechnique = Effect.Techniques["Textured"];

foreach (var batch in batches.Values)
batch.Draw();
Expand Down
1 change: 1 addition & 0 deletions build/init.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
git submodule update --init --recursive

0 comments on commit 6b38dd9

Please sign in to comment.