Edit: I made some changes to this post after fixing the model and making some changes to my specular shader (using exactly what I found in MonoGameEffects/Content/WindowsGL/Shaders at master · kgiannakakis/MonoGameEffects · GitHub)
I’m learning about HLSL with RB Whitaker’s tutorials and applying them to my current MG project. Things were going okay, but I’m having trouble getting Specular Lighting to work correctly.
It’s supposed to look like this:
However, mine is just totally dark.
And yet, if I comment out setting the viewVector of the effect and leave it at it’s default value float3(1, 0, 0); then I at least get this:
So there’s two problems. The helicopter is rotated slightly and it looks like parts of are semi-transparent…? The other problem is the aforementioned problem regarding how the viewVector makes it not show up at all (this probably is just an issue related to the view and not the shader)
Now I have reviewed this MonoGame Effects and changed my Specular.fx copied and pasted the Specular.fx hlsl from the WindowsGL project here MonoGameEffects/Content/WindowsGL/Shaders at master · kgiannakakis/MonoGameEffects · GitHub which has some slight differences.
One thing though. On my MonoGame Pipeline Tool I have the platform set to Windows and my profile set to HiDef. I don’t know if this makes a difference. I tried other settings, like DesktopGL and Reach. But this didn’t make a difference. Do I need to set it to WindowsGL? Because I don’t have that option and I’m not sure how to add it. This pipeline tool is 3.6
Shader code: float4x4 World;float4x4 View;float4x4 Projection;float4 AmbientColor = - Pastebin.com
Source code: using System;using System.Collections.Generic;using System.Linq;using Micr - Pastebin.com
For additional reference, here is what the model looks like using BasicEffect and default lighting
Maybe this will help.
Below are the classic phong and blinn phong as functions called from the pixel shader.
float SpecularBlinnPhong(float3 pos, float3 lightDir, float shininess, float3 normal)
float3 viewDir = normalize(-pos);
float3 halfDir = normalize(lightDir + viewDir);
float specAngle = max(dot(halfDir, normal), 0.0f);
return pow(specAngle, shininess);
float SpecularPhong(float3 pos, float3 lightDir, float shininess, float3 normal)
float3 viewDir = normalize(-pos);
float3 reflectDir = reflect(-lightDir, normal);
float specAngle = max(dot(reflectDir, viewDir), 0.0f);
return pow(specAngle, shininess / 4.0f); // note that the exponent is different here
It might also be that the normal is twisted around depending on the platform you are using and the one that was written on, you can try sticking a negative sign on the light or the normal.
// typically i just pass in the camera position to calculate the View vector for lighting.
float3 V = normalize(CameraPosition - P); // the pixel pos to view normal
// Were N is the input surface normal passed in.
float NdotV = max(0.0, dot(N, V)); // view respective backface determination.
float3 R = 2.0 * NdotV * N - V; // approximated reflection vector.
float3 L = normalize(Lights[i] - P); // to light vector, where lighs is positional lights.
float3 H = normalize(L + V); // half vector
float NdotL = max(0.0001f, dot(N, L)); // n dot l is basically the diffuse multiplier for the texel.
// So then specular is basically the H half vector to the surface normal N as shown in the blinnphong formula.
Thanks I think I got it now.
The camPosition value was indeed incorrect I should have sent something else.
Also, MG was rendering it as slightly transparent somehow. I needed to include
device.BlendState = BlendState.Opaque;