XNA Shader Matrix Parameter Broken - c#

Im working with model instancing in XNA 4.0 and I sending my model instance transformation in a parallel stream. Im following this tutorial. However when I want a matrix as input to my shader I get what looks like a damaged matrix, because I get strange projection results.
Does anyone know the source to the problem and why I can't pass the matrix when others suggest so?
Problem:
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float3 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Tangent : TANGENT0;
float3 Binormal : BINORMAL0;
float4x4 World : TEXCOORD3; //Problem
};
Changing the vertex shader function to the following does not help either:
VertexShaderOutput VertexShaderFunction(VertexShaderInput input, float4x4 World : TEXCOORD3)
{
}
This works if I build the matrix with the vectors alone, I dont know why. Im I losing data?
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float3 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Tangent : TANGENT0;
float3 Binormal : BINORMAL0;
float4 World1 : TEXCOORD3;
float4 World2 : TEXCOORD4;
float4 World3 : TEXCOORD5;
float4 World4 : TEXCOORD6;
};
Vertex format:
internal struct InstanceDataVertex
{
public Matrix World;
public InstanceDataVertex(Matrix World)
{
this.World = World;
}
public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(0, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 3),
new VertexElement(sizeof(float) * 4, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 4),
new VertexElement(sizeof(float) * 8, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 5),
new VertexElement(sizeof(float) * 12, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 6)
);
}

The input registers on the GPU are limited in size. The size of TEXCOORDn is float4 (as listed here). There are no float4x4 inputs.
Splitting your matrix across several input registers and then reconstructing it should work fine. It's just a matter of making sure the right values in your C# Matrix end up in the right places in your HLSL float4x4. I suspect the mapping is trivial, but I'm not sure.

here is code I use in my project
public Instance(Instancer instancer, float scale, Vector3 translate, Vector3 information)
{
ID++;
id = ID;
this.Scale(scale);
this.Translate(translate);
this.Update(); //update the model matrix modelMatrix=scale*rotate*translate ma!
Instancer = instancer;
modelMatrix.M12 = information.X; //additional info unique for each instance
modelMatrix.M23 = information.Y;
modelMatrix.M34 = information.Z;
Instancer.instanceTransformMatrices.Add(this, ModelMatrix);
}
Model Matrix for each instance
protected static VertexDeclaration instanceVertexDeclaration = new VertexDeclaration
(
new VertexElement(0, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 0),
new VertexElement(16, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 1),
new VertexElement(32, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 2),
new VertexElement(48, VertexElementFormat.Vector4, VertexElementUsage.BlendWeight, 3)
);
List of Matrices to vbo where modelVertexBuffer is a quad or any other geometry
instanceVertexBuffer = new DynamicVertexBuffer(BaseClass.Device, instanceVertexDeclaration, instanceTransformMatrices.Count, BufferUsage.WriteOnly);
instanceVertexBuffer.SetData(instanceTransformMatrices.Values.ToArray(), 0, instanceTransformMatrices.Count, SetDataOptions.Discard);
Draw Function
BaseClass.Device.SetVertexBuffers(
new VertexBufferBinding(modelVertexBuffer, 0, 0),
new VertexBufferBinding(instanceVertexBuffer, 0, 1)
);
BaseClass.Device.Indices = indexBuffer;
BaseClass.Device.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0,modelVertexBuffer.VertexCount, 0,2,instanceTransformMatrices.Count);
Sample Vertex Shader
VertexOut VS(VertexIn input, float4x4 instanceTransform : BLENDWEIGHT)
{
VertexOut Out = (VertexOut)0;
float4x4 world = transpose(instanceTransform);
input.Position.xyz = float3(world._41,world._42,world._43);

Related

Hardware instancing retrieve color from Model

Hello I'm using c# with monogame and rendering models with hardware instancing I extract a models vertices, normal, textureCoordinate and color. When I render the model using its Texture and textureCoordinate with HLSL the model looks fine. But when I render the model only by its Color it comes up all broken and wrong.
The left model is correct this is the same 3d model but is loaded as a single 3d model without hardware instancing. This is how the colors should look I used blender to manually set these colors.
The right model is using hardware instancing, the shape looks correct but the colors are wrong.
This is how I initialize the custom vertex decleration:
public struct VertexPositionNormalTextureColor
{
public Vector3 Position;
public Vector3 Normal;
public Vector2 TextureCoordinate;
public Vector4 Color;
public VertexPositionNormalTextureColor(Vector3 _Position, Vector3 _Normal, Vector2 _TextureCoordinate, Vector4 _Color)
{
Position = _Position;
Normal = _Normal;
TextureCoordinate = _TextureCoordinate;
Color = _Color;
}
static public VertexDeclaration VertexDeclaration { get { return MyVertexDeclaration; } }
static readonly VertexDeclaration MyVertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement( 0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0 ),
new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0 ),
new VertexElement(sizeof(float) * 6, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0 ),
new VertexElement(sizeof(float) * 8, VertexElementFormat.Vector4, VertexElementUsage.Color, 0 )
});
}
This is how I gather the models vertices, normal, textureCoordinate and color:
List<Vector3> vertices = new List<Vector3>();
List<Vector3> normal = new List<Vector3>();
List<Vector2> texCoord = new List<Vector2>();
List<Vector4> color = new List<Vector4>();
bones = new Matrix[myModel.Bones.Count];
myModel.CopyAbsoluteBoneTransformsTo(bones);
foreach (ModelMesh mm in myModel.Meshes)
{
foreach (ModelMeshPart mmp in mm.MeshParts)
{
VertexPositionNormalTextureColor[] vertexData = new
VertexPositionNormalTextureColor[mmp.NumVertices];
mmp.VertexBuffer.GetData(mmp.VertexOffset * mmp.VertexBuffer.VertexDeclaration.VertexStride,
vertexData, 0, mmp.NumVertices, mmp.VertexBuffer.VertexDeclaration.VertexStride);
for (int i = 0; i != vertexData.Length; i++)
{
vertices.Add(vertexData[i].Position);
normal.Add(vertexData[i].Normal);
texCoord.Add(vertexData[i].TextureCoordinate);
color.Add(vertexData[i].Color);
}
}
}
This is how I set the models vertices, normal, textureCoordinate, color to the vertex buffer:
jvertices = new List<VertexPositionNormalTextureColor>(vertices.Count);
for(int i = 0; i < vertices.Count(); i++)
{
jvertices.Add(new VertexPositionNormalTextureColor(vertices[i], normal[i], texCoord[i], color[i]));
}
geometryBuffer = new VertexBuffer(device, VertexPositionNormalTextureColor.VertexDeclaration, vertices.Count(), BufferUsage.WriteOnly);
geometryBuffer.SetData(jvertices.ToArray());
The code for the HLSL is as below:
struct VertexShaderInput
{
float3 inPositionOS : SV_Position;
float3 NormalOS : NORMAL0;
float2 inTexCoords : TEXCOORD0;
float4 inColor : COLOR0;
};
struct VertexShaderOutput
{
float4 PositionCS : SV_Position; //clip space
float4 PositionWS : POSITIONWS; //world space
float3 NormalWS : NORMAL0;
float2 inTexCoords : TEXCOORD0;
float4 inColor : COLOR0;
};
VertexShaderOutput InstancingVS(VertexShaderInput input, float4x4 instanceTransform : TEXCOORD2)
{
VertexShaderOutput output;
float4x4 instance = transpose(instanceTransform);
output.PositionWS = mul(float4(input.inPositionOS.xyz, 1.0f), instance);
output.PositionCS = mul(output.PositionWS, ViewProjection);
output.NormalWS = normalize(mul(input.NormalOS, (float3x3)instance));
output.inTexCoords = input.inTexCoords;
output.inColor = input.inColor;
return output;
}
float4 InstancingPS(VertexShaderOutput input) : COLOR0
{
float4 color = input.inColor;
if (color.a < 0.75f) { clip(-1); return color; }
else color.a = 1;
return color;
}
If anyone notices any issue that might be the reason why Color wont work on HLSL but a texture works perfectly fine.
Edit:
I changed the order of the variables in the struct setting the vertex declaration. It turns out it changed the result I looked into what mmp.VertexBuffer.GetData() actually had stored from the model and for some reason it doesn't have Color.
static readonly VertexDeclaration MyVertexDeclaration = new VertexDeclaration(new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0 ),
new VertexElement(12, VertexElementFormat.Vector3, VertexElementUsage.Normal, 0 ),
new VertexElement(24, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0 ),
new VertexElement(32, VertexElementFormat.Vector3, VertexElementUsage.Tangent, 0 ),
new VertexElement(44, VertexElementFormat.Vector3, VertexElementUsage.Binormal, 0 )
});
I followed the offset and order for these variables in the vertex declaration struct and it seems to be working. How do I make mmp.VertexBuffer.GetData() also gather the Color from the model, Thankyou.

Getting unexpected colors in SharpDX app

I'm delving into directx via the SharpDX wrapper for .NET, but I'm getting some unexpected results.
This is my expected result:
and here is the result I'm getting:
Here is my shader:
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut vsMain(float4 position : POSITION, float4 color : COLOR)
{
VOut output;
output.position = position;
output.color = color;
return output;
}
float4 psMain(VOut pInput) : SV_TARGET
{
return pInput.color;
}
along with the Input Layout:
private D3D11.InputElement[] inputElements = new D3D11.InputElement[]
{
new D3D11.InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, D3D11.InputClassification.PerVertexData, 0),
new D3D11.InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
};
I'm passing the following set of vertices through the vertexBuffer
mesh = new NonIndexMesh() {
vertices = new List<Vertex>() {
new Vertex(new Vector3(-0.5f, 0.5f, 0.0f), Color.Red),
new Vertex(new Vector3(0.5f, 0.5f, 0.0f), Color.Red),
new Vertex(new Vector3(0.0f, -0.5f, 0.0f), Color.Red)
}
}
my Vertex type looks like this:
public struct Vertex {
public Vector3 position;
public Color color;
public Vertex(Vector3 pos, Color col) {
position = pos;
color = col;
}
}
The data being passed through is correct even at runtime according to what's printed out to the Console, and the positions of each vertex being rendered seem to be correct.
What am I missing here that's causing these weird colors in my rendered triangle?
The color class you are using represents the colors as bytes. So values for RGBA range from 0 - 255. Try using the class Color4 which represents the colors as floats in the range 0.0 - 1.0. This is indeed what the shader expects.

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

HLSL Shader throws error when I try to use value

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();
}
}

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;
}
}

Categories

Resources