HLSL Shader throws error when I try to use value - c#

So I'm trying to learn myself some HLSL, but I am stumped. I'm writing a custom shader that has ambient lighting and a simple point-light color thing.
Here is the shader code:
`float4x4 World;
float4x4 View;
float4x4 Projection;
// TODO: add effect parameters here.
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.5;
float4 DiffuseColor = float4(1, 1, 1, 1);
float3 LightPosition = float3(32, 32, 64);
float4 LightDiffuseColor = float4(0.3, 0.05, 0, 1); // intensity multiplier
float4 LightSpecularColor = float4(1, 1, 1, 1); // intensity multiplier
float LightDistance = 50;
texture Texture;
sampler2D textureSampler = sampler_state {
Texture = (Texture);
MinFilter = Point;
MagFilter = Point;
AddressU = Wrap;
AddressV = Wrap;
};
struct VertexShaderInput
{
float4 Pos: POSITION;
float2 TexCoords : TEXCOORD0;
float4 Normal : NORMAL0;
};
struct VertexShaderOutput
{
float4 PosOut : POSITION;
float2 TextureCoordinate : TEXCOORD0;
float3 Normal : TEXCOORD1;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Pos, World);
float4 viewPosition = mul(worldPosition, View);
output.PosOut= mul(viewPosition, Projection);
output.TextureCoordinate = input.TexCoords;
output.Normal = mul(input.Normal, World);
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float attenuation = saturate(1.0f - (length(input.PosOut - LightPosition) / LightDistance));
float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
textureColor.a = 1;
float4 lightCol =
//LightDiffuseColor;
mul(LightDiffuseColor, attenuation);
float4 ambient = (AmbientColor * AmbientIntensity);
ambient.a = 1;
return saturate(textureColor * ambient + lightCol);
}
technique Textured
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_2_0 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}
`
The problem has been narrowed down to this section in the pixel shader:
float attenuation = saturate(1.0f - (length(input.PosOut - LightPosition) / LightDistance));
...
float4 lightCol =
//LightDiffuseColor;
mul(LightDiffuseColor, attenuation);
return saturate(textureColor * ambient + lightCol);
It will work fine if i use just the LightDiffuseColor, but as soon as I try to multiply it, it throws this error:
GameContentShadersSimpleTexture.fx(35,21) error X4502 invalid ps_2_0 input semantic 'POSITION'
I'm using XNA for the engine. I'm kinda stumped here. Can anyone help me?
Thanks,
Doodles.
Edit:
Mkay, so I've narrowed it down to a pretty precise spot. It's when the length function is called for (input.PosOut - LightPosition)

All right, so I have a solution for this. Instead of using per-pixel lighting for point lights, I should be using vertex lighting instead. The position semantic is not supported for pixel shader inputs, as seen here. But position still needs to be an ouput for the vertex shader, so it's included in the pixel shader input, but it cannot be used. My updated code is shown below:
float4x4 World;
float4x4 View;
float4x4 Projection;
// TODO: add effect parameters here.
float4 AmbientColor = float4(1, 1, 1, 1);
float AmbientIntensity = 0.5;
float4 DiffuseColor = float4(1, 1, 1, 1);
float3 LightPosition = float3(32, 32, 48);
float4 LightDiffuseColor = float4(0.3, 0.05, 0, 1);
float4 LightSpecularColor = float4(1, 1, 1, 1); // intensity multiplier
float LightDistance = 100;
texture Texture;
sampler2D textureSampler = sampler_state {
Texture = (Texture);
MinFilter = Point;
MagFilter = Point;
AddressU = Wrap;
AddressV = Wrap;
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float4 Normal : NORMAL0;
float2 TextureCoordinate : TEXCOORD0;
};
struct PixelShaderInput
{
float4 Position : POSITION0;
float4 Color : COLOR1;
float3 Normal : TEXCOORD0;
float2 TextureCoordinate : TEXCOORD1;
};
PixelShaderInput VertexShaderFunction(VertexShaderInput input)
{
PixelShaderInput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position= mul(viewPosition, Projection);
output.Color = mul(LightDiffuseColor,saturate(1.0f - (length(mul(input.Position, World) - LightPosition) / LightDistance)));
output.TextureCoordinate = input.TextureCoordinate;
output.Normal = mul(input.Normal, World);
return output;
}
float4 PixelShaderFunction(PixelShaderInput input) : COLOR0
{
float4 textureColor = tex2D(textureSampler, input.TextureCoordinate);
textureColor.a = 1;
float4 ambient = (AmbientColor * AmbientIntensity);
ambient.a = 1;
return saturate(textureColor * ambient + input.Color);
}
technique Textured
{
pass Pass1
{
// TODO: set renderstates here.
VertexShader = compile vs_1_1 VertexShaderFunction();
PixelShader = compile ps_2_0 PixelShaderFunction();
}
}

Related

Changing color of unity concentric circle shader

Trying to change the colors of this shader from black and white to a multitude of colors, is there any way one would be able to do this? I'm pretty new to unity and trying my best with what I've got. If possible I'd like to be able to do at least 3 different colors, but I understand if that's not really feasible with what's made here. If so, could someone potentially point me towards something similar that I can try using instead?
http://www.shaderslab.com/demo-01---concentric-circles.html
Here's the code from the shader itself.
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/ConcentricCircles" {
Properties {
_OrigineX ("PosX Origine", Range(0,1)) = 0.5
_OrigineY ("PosY Origine", Range(0,1)) = 0.5
_Speed ("Speed", Range(-100,100)) = 60.0
_CircleNbr ("Circle quantity", Range(10,1000)) = 60.0
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
float _OrigineX;
float _OrigineY;
float _Speed;
float _CircleNbr;
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord0 : TEXCOORD0;
};
struct fragmentInput{
float4 position : SV_POSITION;
float4 texcoord0 : TEXCOORD0;
};
fragmentInput vert(vertexInput i){
fragmentInput o;
o.position = UnityObjectToClipPos (i.vertex);
o.texcoord0 = i.texcoord0;
return o;
}
fixed4 frag(fragmentInput i) : SV_Target {
fixed4 color;
float distanceToCenter;
float time = _Time.x * _Speed;
float xdist = _OrigineX - i.texcoord0.x;
float ydist = _OrigineY - i.texcoord0.y;
distanceToCenter = (xdist * xdist + ydist * ydist) * _CircleNbr;
color = sin(distanceToCenter + time);
return color;
}
ENDCG
}
}
}
I am not great at shaders, but I can help out a bit. You would want to add a _Color property. The value can be changed in code, or in the inspector. There are different ways to apply the color either by summation or multiplication. Both options produce different results. It will only replace the white portion, or add highlights around the white. The reason for the black portions is due to how the sin function works.
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
Shader "Custom/Test" {
Properties {
_OrigineX ("PosX Origine", Range(0,1)) = 0.5
_OrigineY ("PosY Origine", Range(0,1)) = 0.5
_Speed ("Speed", Range(-100,100)) = 60.0
_CircleNbr ("Circle quantity", Range(10,1000)) = 60.0
_Color ("Main Color", Color) = (1,1,1,1)
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
float _OrigineX;
float _OrigineY;
float _Speed;
float _CircleNbr;
fixed4 _Color;
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord0 : TEXCOORD0;
};
struct fragmentInput{
float4 position : SV_POSITION;
float4 texcoord0 : TEXCOORD0;
};
fragmentInput vert(vertexInput i){
fragmentInput o;
o.position = UnityObjectToClipPos (i.vertex);
o.texcoord0 = i.texcoord0;
return o;
}
fixed4 frag(fragmentInput i) : SV_Target {
fixed4 color;
float distanceToCenter;
float time = _Time.x * _Speed;
float xdist = _OrigineX - i.texcoord0.x;
float ydist = _OrigineY - i.texcoord0.y;
distanceToCenter = (xdist * xdist + ydist * ydist) * _CircleNbr;
color = sin(distanceToCenter + time) + _Color;
return color;
}
ENDCG
}
}
}
Replace the line color = sin(distanceToCenter + time) + _Color; with color = sin(distanceToCenter + time) * _Color; to see the other option. The left image is with additive while the right is multiplicative. I set the color in the editor to green. You can also try changing the sin function to another trig function which produces different results as well.
Edit: This line is pretty cool color = sin(distanceToCenter + time) + cos(distanceToCenter + time) * _Color;
Edit 2: As per request, here is one way to get multiple different colors. As the sin function is being used, the values are between [-1, 1], so at times this would equal 0, or (0,0,0,1) which is black. You can make if conditionals to catch ranges of these values then directly set the color. Here is how I approach it.
Shader "Custom/Test" {
Properties {
_OrigineX ("PosX Origine", Range(0,1)) = 0.5
_OrigineY ("PosY Origine", Range(0,1)) = 0.5
_Speed ("Speed", Range(-100,100)) = 60.0
_CircleNbr ("Circle quantity", Range(10,1000)) = 60.0
_Color1 ("Color 1", Color) = (1,1,1,1)
_Color2 ("Color 2", Color) = (1,1,1,1)
_Color3 ("Color 3", Color) = (1,1,1,1)
}
SubShader {
Pass {
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma target 3.0
float _OrigineX;
float _OrigineY;
float _Speed;
float _CircleNbr;
fixed4 _Color1;
fixed4 _Color2;
fixed4 _Color3;
struct vertexInput {
float4 vertex : POSITION;
float4 texcoord0 : TEXCOORD0;
};
struct fragmentInput{
float4 position : SV_POSITION;
float4 texcoord0 : TEXCOORD0;
};
fragmentInput vert(vertexInput i){
fragmentInput o;
o.position = UnityObjectToClipPos (i.vertex);
o.texcoord0 = i.texcoord0;
return o;
}
fixed4 frag(fragmentInput i) : SV_Target {
fixed4 color;
float distanceToCenter;
float time = _Time.x * _Speed;
float xdist = _OrigineX - i.texcoord0.x;
float ydist = _OrigineY - i.texcoord0.y;
distanceToCenter = (xdist * xdist + ydist * ydist) * _CircleNbr;
float preColor = sin(distanceToCenter + time);
if(preColor < -0.8)
{
color = _Color1;
}
else if(preColor < 0.2)
{
color = _Color2;
}
else
{
color = _Color3;
}
return color;
}
ENDCG
}
}
}
And my result
Just assign the three colors you would like in the inspector. You can edit the code to change how many thresholds there are, or what the thresholds are.

Having trouble projecting a ShadowMap on to a surface

So I am having trouble rendering a shadow map in a shader I'm kind of new to shader coding(roughly 3 months), I'll talk you through what I am doing(well what I think I am doing).
So to start off by creating a script(ShadowCopy.cs) which copies the shadow map then passes it to a global variable(_MyShadowMap). Now the shader(ShadowMapping.shader), then I create getShadowCoord which calculates the shadowMap then applies the getCascadeWeights() to the shadow Coordinates(for Optimal reasons).
Then I create computeCameraSpacePosFromDepthAndInvProjMat() which computes the camera space position based from depth and inverse projection matrix. Now the frag function which renders the pixels
making it visible in the Application, but overall its just Screen-Space-Shadows.
So the problem I am facing are the Projection Values which I honestly don't know how to fix. You can see the Problem in the picture.
enter image description here
The Material Settings for ShadowMapping.shader.
enter image description here
ShadowCopy.cs
using UnityEngine;
using UnityEngine.Rendering;
public class ShadowCopy : MonoBehaviour
{
public Light m_Light;
RenderTexture m_ShadowmapCopy;
public int TextureSize = 512;
void Start()
{
RenderTargetIdentifier shadowmap = BuiltinRenderTextureType.CurrentActive;
m_ShadowmapCopy = new RenderTexture(TextureSize, TextureSize, 16, RenderTextureFormat.ARGB32);
m_ShadowmapCopy.filterMode = FilterMode.Point;
CommandBuffer cb = new CommandBuffer();
cb.SetShadowSamplingMode(shadowmap, ShadowSamplingMode.RawDepth);
cb.Blit(shadowmap, new RenderTargetIdentifier(m_ShadowmapCopy));
cb.SetGlobalTexture("_MyShadowMap", shadowmap);
m_Light.AddCommandBuffer(LightEvent.AfterShadowMap, cb);
}
void OnGUI()
{
if (m_ShadowmapCopy != null)
{
GUI.DrawTextureWithTexCoords(new Rect(0, 20, 150, 150), m_ShadowmapCopy, new Rect(0, 0, 1, 1), false);
}
}
ShadowMapping.shader
Shader "Custom/ShadowMapping" {
Properties {
_Color("Main Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGBA)", 2D) = "white" {}
}
SubShader {
Pass {
Tags { "RenderType"="Opaque" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
#include "AutoLight.cginc"
uniform float4 _Color;
uniform sampler2D _MyShadowMap;
//UNITY_DECLARE_SHADOWMAP(_MyShadowMap);
uniform sampler2D _MainTex;
float4 LowResDepth_TexelSize;
float4 _ShadowMapTexture_TexelSize;
#define SHADOWMAPSAMPLER_AND_TEXELSIZE_DEFINED
#define UNITY_USE_CASCADE_BLENDING 0
#define UNITY_CASCADE_BLEND_DISTANCE 0.1
struct appdata {
float4 vertex : POSITION;
float2 texcoord : TEXCOORD0;
#ifdef UNITY_STEREO_INSTANCING_ENABLED
float3 ray0 : TEXCOORD1;
float3 ray1 : TEXCOORD2;
#else
float3 ray : TEXCOORD1;
#endif
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct v2f {
float4 pos : SV_POSITION;
// xy uv / zw screenpos
float4 uv : TEXCOORD0;
// View space ray, for perspective case
float3 ray : TEXCOORD1;
// Orthographic view space positions (need xy as well for oblique matrices)
float3 orthoPosNear : TEXCOORD2;
float3 orthoPosFar : TEXCOORD3;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
v2f vert (appdata v)
{
v2f o;
UNITY_SETUP_INSTANCE_ID(v);
UNITY_TRANSFER_INSTANCE_ID(v, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
float4 clipPos = UnityObjectToClipPos(v.vertex);
o.pos = clipPos;
o.uv.xy = v.texcoord;
o.uv.zw = ComputeNonStereoScreenPos(clipPos);
clipPos.y *= _ProjectionParams.x;
float3 orthoPosNear = mul(unity_CameraInvProjection, float4(clipPos.x,clipPos.y,-1,1)).xyz;
float3 orthoPosFar = mul(unity_CameraInvProjection, float4(clipPos.x,clipPos.y, 1,1)).xyz;
orthoPosNear.z *= -1;
orthoPosFar.z *= -1;
o.orthoPosNear = orthoPosNear;
o.orthoPosFar = orthoPosFar;
return o;
}
UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture);
inline fixed4 getCascadeWeights(float3 wpos, float z)
{
fixed4 zNear = float4( z >= _LightSplitsNear );
fixed4 zFar = float4( z < _LightSplitsFar );
fixed4 weights = zNear * zFar;
return weights;
}
inline float4 getShadowCoord( float4 wpos, fixed4 cascadeWeights )
{
float3 sc0 = mul (unity_WorldToShadow[0], wpos).xyz;
float3 sc1 = mul (unity_WorldToShadow[1], wpos).xyz;
float3 sc2 = mul (unity_WorldToShadow[2], wpos).xyz;
float3 sc3 = mul (unity_WorldToShadow[3], wpos).xyz;
float4 shadowMapCoordinate = float4(sc0 * cascadeWeights[0] + sc1 * cascadeWeights[1] + sc2 * cascadeWeights[2] + sc3 * cascadeWeights[3], 1);
#if defined(UNITY_REVERSED_Z)
float noCascadeWeights = 1 - dot(cascadeWeights, float4(1, 1, 1, 1));
shadowMapCoordinate.z += noCascadeWeights;
#endif
return shadowMapCoordinate;
}
inline float3 computeCameraSpacePosFromDepthAndInvProjMat(v2f i)
{
float zdepth = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, i.uv.xy);
#if defined(UNITY_REVERSED_Z)
zdepth = 1 - zdepth;
#endif
float4 clipPos = float4(i.uv.zw, zdepth, 1.0);
clipPos.xyz = 2.0f * clipPos.xyz - 1.0f;
float4 camPos = mul(unity_CameraInvProjection, clipPos);
camPos.xyz /= camPos.w;
camPos.z *= -1;
return camPos.xyz;
}
half4 frag(v2f i) : COLOR
{
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
float3 vpos = computeCameraSpacePosFromDepthAndInvProjMat(i);
float4 wpos = mul (unity_CameraToWorld, float4(vpos,1));
fixed4 cascadeWeights = getCascadeWeights (wpos, vpos.z);
float4 shadowCoord = getShadowCoord(wpos, cascadeWeights);
float shadow = tex2D(_MyShadowMap, shadowCoord);
shadow = lerp(_LightShadowData.r, 1.0, shadow);
fixed4 res = shadow;
return shadow * tex2D(_MainTex, i.uv) * _Color;
}
ENDCG
}
}
}
A few items that jump out at me:
Change: cb.SetGlobalTexture("_MyShadowMap", shadowmap); to cb.SetGlobalTexture("_MyShadowMap", m_ShadowmapCopy);
Change: uniform sampler2D _MyShadowMap; to UNITY_DECLARE_SHADOWMAP(_MyShadowMap);
Change float shadow = tex2D(_MyShadowMap, shadowCoord); to float shadow = UNITY_SAMPLE_SHADOW(_MyShadowMap, shadowCoord);

Problems with my unity shader

I have began to create some shaders for my university group project however I have ran into a bit of a snag with my water shader. I am trying to create one which utilises two overlapping normal maps.
Whilst everything looks okay in the editor, when I then publish to the webplayer, the scene looks like its unlit.
Heres my code for the shader:
//
// Filename : WaterShader.shader
// Version : 2.0
// Date : 1st March 2014
//
Shader "Flight/WaterShader/2.0"
{
// Set up variables so we can access them in inspector mode
Properties
{
// Variable to control the colour tint
_Color ("Base Color", Color) = (1, 1, 1, 1)
// Variables for specular
_SpecularColor ("Specular Color", Color) = (1, 1, 1, 1)
_SpecularAmount ("Shininess", Float) = 10
// Variable for setting the base texture
_MainTex ("Water Texture", 2D) = "white" { }
// Variables to set the normal map 1
_BumpMapA ("Normal Map", 2D) = "bump" { }
_BumpDepthA ("Depth", Range(0.25, 10.0)) = 1
// Variables to set the normal map 2
_BumpMapB ("Normal Map", 2D) = "bump" { }
_BumpDepthB ("Depth", Range(0.25, 10.0)) = 1
}
SubShader
{
pass
{
Tags { "RenderType" = "Opaque" }
Lighting On
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma exclude_renderers flash
// Variables
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMapA;
float4 _BumpMapA_ST;
float _BumpDepthA;
sampler2D _BumpMapB;
float4 _BumpMapB_ST;
float _BumpDepthB;
float4 _Color;
float4 _SpecularColor;
float _SpecularAmount;
float4 _LightColor0;
struct vertexInput
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
float4 tangent : TANGENT;
};
struct vertexOutput
{
float4 pos : SV_POSITION;
float4 tex : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalWorld : TEXCOORD2;
float3 tangentWorld : TEXCOORD3;
float3 binormalWorld : TEXCOORD4;
};
vertexOutput vert(vertexInput input)
{
vertexOutput output;
output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
output.tex = input.texcoord;
output.posWorld = mul(_Object2World, input.vertex);
output.normalWorld = normalize( mul( float4( input.normal, 0.0f ), _World2Object ).xyz );
output.tangentWorld = normalize( mul( _Object2World, input.tangent ).xyz );
output.binormalWorld = normalize( cross( output.normalWorld, output.tangentWorld) * input.tangent.w );
return output;
}
float4 frag(vertexOutput input) : COLOR
{
// Set up variables
float3 viewDirection;
float3 lightDirection;
float3 normalDirection;
float lightIntensity;
float4 normalColorA;
float4 normalColorB;
float4 normalColor;
float3 normalLocalA;
float3 normalLocalB;
float3 normalLocal;
float3x3 normalWorld;
float4 textureColor;
float3 diffuseColor;
float3 specularColor;
float3 lightColor;
float4 finalColor;
// Begin calculations
// Calculate the angle we are looking at the pixel
viewDirection = normalize(_WorldSpaceCameraPos.xyz - input.posWorld.xyz );
if(_WorldSpaceLightPos0.w == 0.0)
{
lightIntensity = 1.0;
lightDirection = normalize(_WorldSpaceLightPos0.xyz);
}
else
{
float3 fragmentToLightSource = _WorldSpaceLightPos0.xyz - input.posWorld.xyz;
float distance = length(fragmentToLightSource);
lightIntensity = 1.0 / distance;
lightDirection = normalize(fragmentToLightSource);
}
// Sample the textures
textureColor = tex2D(_MainTex, input.tex.xy * _MainTex_ST.xy + _MainTex_ST.zw);
normalColorA = tex2D(_BumpMapA, input.tex.xy * _BumpMapA_ST.xy + _BumpMapA_ST.zw);
normalColorB = tex2D(_BumpMapB, input.tex.xy * _BumpMapB_ST.xy + _BumpMapB_ST.zw);
// Expand the normals and set the intensity of the normal map
normalLocalA = float3(2.0 * normalColorA.ag - float2(1.0, 1.0), 0.0);
normalLocalA.z = _BumpDepthA;
normalLocalB = float3(2.0 * normalColorB.ag - float2(1.0, 1.0), 0.0);
normalLocalB.z = _BumpDepthB;
// Combine the two normals
normalLocal = normalize(normalLocalA + normalLocalB);
// Calculate the normal in the world
normalWorld = float3x3( input.tangentWorld, input.binormalWorld, input.normalWorld );
normalDirection = normalize( mul( normalLocal, normalWorld ) );
// Calculate lighting
diffuseColor = lightIntensity * _LightColor0.xyz * saturate( dot(normalDirection, lightDirection) );
specularColor = diffuseColor * _SpecularColor.xyz * pow( saturate( dot( reflect(-lightDirection, normalDirection), viewDirection) ), _SpecularAmount );
// Combine lighting
lightColor = UNITY_LIGHTMODEL_AMBIENT.xyz + diffuseColor + specularColor;
// Apply lighting to the texture color
textureColor = float4( textureColor.xyz * lightColor * _Color.xyz, 1.0);
return textureColor;
}
ENDCG
}
}
FallBack "Specular"
}
In the editor it looks like this:
Where as in the webplayer it looks like this:
Anyone able to help me see where I have gone wrong? :)
You need a separate pass of pixel light in the scene for the web player. Replace your current SubShader Parameters with
SubShader
{
Tags {"RenderType" = "Opaque"}
Pass
{
Tags {"LightMode" = "ForwardAdd"}
}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma exclude_renderers flash

How to draw a circle on 3D terrain in XNA?

So I've been looking across the web to find an answer to this question, but I seem to be missing something.
I've got a little project going that dynamically creates 3D terrain in XNA 4.0, but I want be able to draw a circle (or any other shape for that matter, but let's first go with a circle) on the terrain.
Now I've read some things about 'projective texturing' but I'll admit I'm at a complete loss when it comes to shader-language.
The idea is that I can (dynamically) create a basic shape, say a circle with a 2 'units' diameter, then draw that on the terrain to function as an indicator where the cursor is. (I am able to get the cursor position on the 3D terrain.)
Would anyone know how to do this, is it required to use shaders for this? Any help on the matter is apreciated!
Thanks in advance!
You can use a shader ...
you pass as parameter to the terrain shader, the 3D world position of your cursor, and a radius... to define a sphere,
the trick is pass the vertex world position from the vertex shader to the pixel shader, and
in the pixel shader you only have to tint the output color if the pixel is being drawed, is inside the sphere.
EDIT: I have found an old shader done by myself... with two types of selection circle and box, here you are:
uniform float4x4 xWorldViewProjection;
uniform float3 xCursorPos;
uniform float xCursorRadio;
uniform float4 xLightColor = float4(0.8, 0.8, 0.8,1);
uniform float4 xAmbientFactor = 0.4f;
uniform float3 xCamPos;
uniform int xCursorType=0; // 0: Circle 1: Box
void VS_Basico(
in float4 inPos : POSITION,
in float3 inNormal : NORMAL0,
in float4 inColor : COLOR0,
out float4 outPos: POSITION,
out float3 outNormal:TEXCOORD1,
out float3 outPos2 : TEXCOORD0,
out float4 outColor: COLOR0
)
{
outPos = mul (inPos, xWorldViewProjection);
outNormal = inNormal;
outPos2 = inPos.xyz;
outColor = inColor;
}
float4 PS_CursorPerPixelCircular ( in float4 inColor : COLOR, in float3 inPos:TEXCOORD0 ) : COLOR
{
float f = distance(inPos, xCursorPos);
float4 outColor = inColor;
if (f<xCursorRadio) {
outColor=lerp(float4(0,1,1,1), inColor, 0.4) ;
}
return outColor;
}
float4 PS_CursorPerPixelCuadrado ( in float4 inColor : COLOR, in float3 inPos:TEXCOORD0 ) : COLOR
{
float3 size = float3(xCursorRadio,xCursorRadio,xCursorRadio);
float3 minSize = xCursorPos - size;
float3 maxSize = xCursorPos + size;
float4 outColor = inColor;
if (inPos.x>=minSize.x && inPos.x<=maxSize.x && inPos.y>=minSize.y && inPos.y<=maxSize.y && inPos.z>=minSize.z && inPos.z<=maxSize.z )
{
outColor=lerp(float4(0,1,1,1), inColor, 0.4) ;
}
return outColor;
}
void PS_Basico(
in float4 inColor : COLOR0,
in float3 inPos:TEXCOORD0,
in float3 inNormal:TEXCOORD1,
out float4 outColor: COLOR0
)
{
float3 xLightPos = float3(40, 40, 0);
float3 LightDir = normalize(inPos - xLightPos);
float3 reflectionVector = reflect(LightDir, inNormal);
float3 eyeVector = inPos - xCamPos;
float specular = dot(normalize(reflectionVector), normalize(eyeVector));
specular = pow(specular, 256);
float difusse_factor = -dot(normalize(inNormal), LightDir);
if (difusse_factor<0) difusse_factor = 0;
float4 col = inColor * xAmbientFactor + inColor * difusse_factor * xLightColor;
if (xCursorType ==0)
{
col = PS_CursorPerPixelCircular(col, inPos);
} else {
col = PS_CursorPerPixelCuadrado(col, inPos);
}
col.a = 1;
col.rgb += specular;
/* col.xyz = col.xyz * (inPos.y+1) / 2;
col.y = 2*col.x;
col.z = 2*col.x;
*/
outColor = col;
//outColor = float4(inNormal, 1);
}
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
//--- TECNIQUES -----------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------------------------------
technique ColoredWired
{
pass Pass0
{
VertexShader = compile vs_2_0 VS_Basico();
PixelShader = compile ps_2_0 PS_Basico();
FILLMODE = WIREFRAME;
}
}
technique ColoredSolid
{
pass Pass0
{
VertexShader = compile vs_2_0 VS_Basico();
PixelShader = compile ps_2_0 PS_Basico();
FILLMODE = SOLID;
}
}

Problem with shader/code

I have this basic 3d application and trying to write my own toon shader, but even if I remove the tooning part it still still just stays plain darkblue when I give a red color to it.
shader code :
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float4 Color : COLOR0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float LightAmount : TEXCOORD1;
float4 Color : COLOR0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
float4 worldPosition = mul(input.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.Color = input.Color;
float3 worldNormal = mul(input.Normal, World);
output.LightAmount = dot(worldNormal, LightDirection);
// TODO: add your vertex shader code here.
return output;
}
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float4 color = input.Color;
float light;
if (input.LightAmount > ToonThresholds[0])
light = ToonBrightnessLevels[0];
else if (input.LightAmount > ToonThresholds[1])
light = ToonBrightnessLevels[1];
else
light = ToonBrightnessLevels[2];
color.rgb *= light;
return color;
}
Custom vertex format :
public struct VertexPositionNormalColored
{
public Vector3 Position;
public Color Color;
public Vector3 Normal;
public static int SizeInBytes = 7 * 4;
public static VertexElement[] VertexElements = new VertexElement[]
{
new VertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
new VertexElement(12,VertexElementFormat.Vector3,VertexElementUsage.Normal,0),
new VertexElement(24,VertexElementFormat.Color,VertexElementUsage.Color,0)
};
}
Init of the triangle I am trying to draw :
testVetices = new VertexPositionNormalColored[3];
testVetices[0].Position = new Vector3(-0.5f, -0.5f, 0f);
testVetices[0].Color = Color.Red;
testVetices[0].Normal = new Vector3(0, 0, 1);
testVetices[1].Position = new Vector3(0, 0.5f, 0f);
testVetices[1].Color = Color.Red;
testVetices[1].Normal = new Vector3(0, 0, 1);
testVetices[2].Position = new Vector3(0.5f, -0.5f, 0f);
testVetices[2].Color = Color.Red;
testVetices[2].Normal = new Vector3(0, 0, 1);
In C#, struct fields are ordered sequentially in memory. But the order of fields in your structure does not match what you have set in VertexElements.
It should be:
public struct VertexPositionNormalColored
{
public Vector3 Position;
public Vector3 Normal;
public Color Color; // oh look I've moved!
public static int SizeInBytes = 7 * 4;
public static VertexElement[] VertexElements = new VertexElement[]
{
new VertexElement(0,VertexElementFormat.Vector3,VertexElementUsage.Position,0),
new VertexElement(12,VertexElementFormat.Vector3,VertexElementUsage.Normal,0),
new VertexElement(24,VertexElementFormat.Color,VertexElementUsage.Color,0)
};
}
(Not sure if that's the only problem in your code, but that's what stuck out.)
If you are using XNA 4.0, you might also want to have a read of this blog post.

Categories

Resources