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.
Related
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.
I am trying to implement fur in Unity with the Shells technique. The Fins technique is purposely left out because I want this to run on low end mobiles (mostly Android devices) and that requires OpenGL ES 3.0 and above while Shells technique only requires OpenGL ES 2.0.
There is an example on the Shell technique based on XNA and I made an attempt to port that into Unity but it failed to work. Here is the article with the XNA project.
The XNA shader:
float4x4 World;
float4x4 View;
float4x4 Projection;
float CurrentLayer; //value between 0 and 1
float MaxHairLength; //maximum hair length
texture FurTexture;
sampler FurSampler = sampler_state
{
Texture = (FurTexture);
MinFilter = Point;
MagFilter = Point;
MipFilter = Point;
AddressU = Wrap;
AddressV = Wrap;
};
struct VertexShaderInput
{
float3 Position : POSITION0;
float3 Normal : NORMAL0;
float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
VertexShaderOutput FurVertexShader(VertexShaderInput input)
{
VertexShaderOutput output;
float3 pos;
pos = input.Position + input.Normal * MaxHairLength * CurrentLayer;
float4 worldPosition = mul(float4(pos,1), World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.TexCoord = input.TexCoord;
return output;
}
float4 FurPixelShader(VertexShaderOutput input) : COLOR0
{
return tex2D(FurSampler, input.TexCoord);
}
technique Fur
{
pass Pass1
{
AlphaBlendEnable = true;
SrcBlend = SRCALPHA;
DestBlend = INVSRCALPHA;
CullMode = None;
VertexShader = compile vs_2_0 FurVertexShader();
PixelShader = compile ps_2_0 FurPixelShader();
}
}
The XNA C# script that controls the shader:
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
//simple camera for use in the game
Camera camera;
//texture containing fur data
Texture2D furTexture;
//effect for fur shaders
Effect furEffect;
//number of layers of fur
int nrOfLayers = 60;
//total length of the hair
float maxHairLength = 2.0f;
//density of hair
float density = 0.2f;
Texture2D furColorTexture;
//movement vectors
Vector3 gravity = new Vector3(0, -1.0f, 0);
Vector3 forceDirection = Vector3.Zero;
//final displacement for hair
Vector3 displacement;
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
// TODO: Add your initialization logic here
camera = new Camera(this);
Components.Add(camera);
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//generate the geometry
GenerateGeometry();
//load the effect
furEffect = Content.Load<Effect>("FurEffect");
//create the texture
furTexture = new Texture2D(GraphicsDevice,
256, 256, 1,
TextureUsage.None,
SurfaceFormat.Color);
//fill the texture
FillFurTexture(furTexture, density);
furColorTexture = Content.Load<Texture2D>("bigtiger");
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
// TODO: Add your update logic here
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
forceDirection.X = (float)Math.Sin(gameTime.TotalGameTime.TotalSeconds) * 0.5f;
displacement = gravity + forceDirection;
furEffect.Parameters["Displacement"].SetValue(displacement);
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
furEffect.Parameters["World"].SetValue(Matrix.CreateTranslation(0, -10, 0));
furEffect.Parameters["View"].SetValue(camera.View);
furEffect.Parameters["Projection"].SetValue(camera.Projection);
furEffect.Parameters["MaxHairLength"].SetValue(maxHairLength);
furEffect.Parameters["FurTexture"].SetValue(furTexture);
furEffect.Parameters["Texture"].SetValue(furColorTexture);
furEffect.Begin();
for (int i = 0; i < nrOfLayers; i++)
{
furEffect.Parameters["CurrentLayer"].SetValue((float)i / nrOfLayers);
furEffect.CommitChanges();
furEffect.CurrentTechnique.Passes[0].Begin();
DrawGeometry();
furEffect.CurrentTechnique.Passes[0].End();
}
furEffect.End();
base.Draw(gameTime);
}
/// <summary>
/// This functions prepares a texture to be used for fur rendering
/// </summary>
/// <param name="furTexture">This will contain the final texture</param>
/// <param name="density">Hair density in [0..1] range </param>
private void FillFurTexture(Texture2D furTexture, float density)
{
//read the width and height of the texture
int width = furTexture.Width;
int height = furTexture.Height;
int totalPixels = width * height;
//an array to hold our pixels
Color[] colors;
colors = new Color[totalPixels];
//random number generator
Random rand = new Random();
//initialize all pixels to transparent black
for (int i = 0; i < totalPixels; i++)
colors[i] = Color.TransparentBlack;
//compute the number of opaque pixels = nr of hair strands
int nrStrands = (int)(density * totalPixels);
//compute the number of strands that stop at each layer
int strandsPerLayer = nrStrands / nrOfLayers;
//fill texture with opaque pixels
for (int i = 0; i < nrStrands; i++)
{
int x, y;
//random position on the texture
x = rand.Next(height);
y = rand.Next(width);
//compute max layer
int max_layer = i / strandsPerLayer;
//normalize into [0..1] range
float max_layer_n = (float)max_layer / (float)nrOfLayers;
//put color (which has an alpha value of 255, i.e. opaque)
//max_layer_n needs to be multiplied by 255 to achieve a color in [0..255] range
colors[x * width + y] = new Color((byte)(max_layer_n * 255), 0, 0, 255);
}
//set the pixels on the texture.
furTexture.SetData<Color>(colors);
}
VertexPositionNormalTexture[] vertices;
private void GenerateGeometry()
{
vertices = new VertexPositionNormalTexture[6];
vertices[0] = new VertexPositionNormalTexture(
new Vector3(-10, 0, 0),
-Vector3.UnitZ,
new Vector2(0, 0));
vertices[1] = new VertexPositionNormalTexture(
new Vector3(10, 20, 0),
-Vector3.UnitZ,
new Vector2(1, 1));
vertices[2] = new VertexPositionNormalTexture(
new Vector3(-10, 20, 0),
-Vector3.UnitZ,
new Vector2(0, 1));
vertices[3] = vertices[0];
vertices[4] = new VertexPositionNormalTexture(
new Vector3(10, 0, 0),
-Vector3.UnitZ,
new Vector2(1, 0));
vertices[5] = vertices[1];
}
private void DrawGeometry()
{
using (VertexDeclaration vdecl = new VertexDeclaration(
GraphicsDevice,
VertexPositionNormalTexture.VertexElements))
{
GraphicsDevice.VertexDeclaration = vdecl;
GraphicsDevice.DrawUserPrimitives<VertexPositionNormalTexture>(PrimitiveType.TriangleList, vertices, 0, 2);
}
}
}
I carefully ported the both the shader and the control script line by line to Unity.
The Ported Unity shader:
Shader "Programmer/Fur Shader"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
//_TintColor("Tint Color", Color) = (1,1,1,1)
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
Blend SrcAlpha One
Blend DstAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
//#pragma multi_compile_fog
#include "UnityCG.cginc"
//In
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
//Out
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
struct VertexShaderInput
{
float3 Position : POSITION0;
float3 Normal : NORMAL0;
float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
//Test variable/delete after
float4 _TintColor;
//The variables
float4x4 World;
float4x4 View;
float4x4 Projection;
float CurrentLayer; //value between 0 and 1
float MaxHairLength; //maximum hair length
VertexShaderOutput vert(VertexShaderInput input)
{
VertexShaderOutput output;
float3 pos;
pos = input.Position + input.Normal * MaxHairLength * CurrentLayer;
float4 worldPosition = mul(float4(pos, 1), World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.TexCoord = input.TexCoord;
return output;
}
float4 frag(VertexShaderOutput i) : COLOR0
{
return tex2D(_MainTex, i.TexCoord);
}
ENDCG
}
}
}
The ported Unity C# script that controls the shader:
public class Game1 : MonoBehaviour
{
public Material material;
public Vector3 pos = new Vector3(0f, 0.98f, -9.54f);
//simple camera for use in the game
private new Camera camera;
//texture containing fur data
public Texture2D furTexture;
//effect for fur shaders
//Effect furEffect;
//number of layers of fur
public int nrOfLayers = 40;
//total length of the hair
public float maxHairLength = 2.0f;
//density of hair
public float density = 0.2f;
//[Space(20)]
//public Vector3 dirWorldVal = new Vector3(0, -10, 0);
void Start()
{
Initialize();
GenerateGeometry();
}
public void Update()
{
Draw();
}
void Initialize()
{
//Initialize the camera
camera = Camera.main;
//create the texture
furTexture = new Texture2D(256, 256, TextureFormat.ARGB32, false);
furTexture.wrapModeU = TextureWrapMode.Repeat;
furTexture.wrapModeV = TextureWrapMode.Repeat;
furTexture.filterMode = FilterMode.Point;
//fill the texture
FillFurTexture(furTexture, density);
/*XNA's SurfaceFormat.Color is ARGB.
//https://gamedev.stackexchange.com/a/6442/98839*/
if (material.mainTexture != null)
{
material.mainTexture.wrapModeU = TextureWrapMode.Repeat;
material.mainTexture.wrapModeV = TextureWrapMode.Repeat;
material.mainTexture.filterMode = FilterMode.Point;
}
}
bool firstDraw = true;
protected void Draw()
{
camera.backgroundColor = CornflowerBlue();
Matrix4x4 worldValue = Matrix4x4.Translate(pos);
Matrix4x4 viewValue = camera.projectionMatrix;
// viewValue = camera.worldToCameraMatrix;
Matrix4x4 projectionValue = camera.projectionMatrix;
material.SetMatrix("World", worldValue);
material.SetMatrix("View", viewValue);
material.SetMatrix("Projection", projectionValue); //Causes object to disappear
material.SetFloat("MaxHairLength", maxHairLength);
if (firstDraw)
material.SetTexture("_MainTex", furTexture);
//furEffect.Begin();
for (int i = 0; i < nrOfLayers; i++)
{
material.SetFloat("CurrentLayer", (float)i / nrOfLayers);
DrawGeometry();
}
if (firstDraw)
{
material.mainTexture.wrapModeU = TextureWrapMode.Repeat;
material.mainTexture.wrapModeV = TextureWrapMode.Repeat;
material.mainTexture.filterMode = FilterMode.Point;
}
if (firstDraw)
firstDraw = false;
}
void DrawGeometry()
{
Quaternion rotation = Quaternion.Euler(0, 180, 0);
Graphics.DrawMesh(verticesMesh, pos, rotation, material, 0, camera);
}
private VertexPositionNormalTexture[] verticesPText;
public Mesh verticesMesh;
private void GenerateGeometry()
{
verticesPText = new VertexPositionNormalTexture[6];
verticesPText[0] = new VertexPositionNormalTexture(new Vector3(-10, 0, 0),
-UnitZ(),
new Vector2(0, 0));
verticesPText[1] = new VertexPositionNormalTexture(new Vector3(10, 20, 0),
-UnitZ(),
new Vector2(1, 1));
verticesPText[2] = new VertexPositionNormalTexture(new Vector3(-10, 20, 0),
-UnitZ(),
new Vector2(0, 1));
verticesPText[3] = verticesPText[0];
verticesPText[4] = new VertexPositionNormalTexture(new Vector3(10, 0, 0),
-UnitZ(),
new Vector2(1, 0));
verticesPText[5] = verticesPText[1];
verticesMesh = VertexPositionNormalTextureToUnityMesh(verticesPText);
}
Mesh VertexPositionNormalTextureToUnityMesh(VertexPositionNormalTexture[] vpnt)
{
Vector3[] vertices = new Vector3[vpnt.Length];
Vector3[] normals = new Vector3[vpnt.Length];
Vector2[] uvs = new Vector2[vpnt.Length];
int[] triangles = new int[vpnt.Length];
//Copy variables to create a mesh
for (int i = 0; i < vpnt.Length; i++)
{
vertices[i] = vpnt[i].Position;
normals[i] = vpnt[i].Normal;
uvs[i] = vpnt[i].TextureCoordinate;
triangles[i] = i;
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uvs;
mesh.triangles = triangles;
return mesh;
}
private void FillFurTexture(Texture2D furTexture, float density)
{
//read the width and height of the texture
int width = furTexture.width;
int height = furTexture.height;
int totalPixels = width * height;
//an array to hold our pixels
Color32[] colors = new Color32[totalPixels];
//random number generator
System.Random rand = new System.Random();
//initialize all pixels to transparent black
for (int i = 0; i < totalPixels; i++)
colors[i] = TransparentBlack();
//compute the number of opaque pixels = nr of hair strands
int nrStrands = (int)(density * totalPixels);
//fill texture with opaque pixels
for (int i = 0; i < nrStrands; i++)
{
int x, y;
//random position on the texture
x = rand.Next(height);
y = rand.Next(width);
//put color (which has an alpha value of 255, i.e. opaque)
colors[x * width + y] = Gold();
}
//set the pixels on the texture.
furTexture.SetPixels32(colors);
// actually apply all SetPixels, don't recalculate mip levels
furTexture.Apply();
}
Color32 TransparentBlack()
{
//http://www.monogame.net/documentation/?page=P_Microsoft_Xna_Framework_Color_TransparentBlack
Color32 color = new Color32(0, 0, 0, 0);
return color;
}
Color32 Gold()
{
//http://www.monogame.net/documentation/?page=P_Microsoft_Xna_Framework_Color_Gold
Color32 color = new Color32(255, 215, 0, 255);
return color;
}
Color32 CornflowerBlue()
{
//http://www.monogame.net/documentation/?page=P_Microsoft_Xna_Framework_Color_CornflowerBlue
Color32 color = new Color32(100, 149, 237, 255);
return color;
}
public static Vector3 UnitZ()
{
return new Vector3(0f, 0f, 1f);
}
}
The ported VertexPositionNormalTexture struct for Unity
public struct VertexPositionNormalTexture
{
public Vector3 Position;
public Vector3 Normal;
public Vector2 TextureCoordinate;
//public static readonly VertexDeclaration VertexDeclaration;
public VertexPositionNormalTexture(Vector3 position, Vector3 normal, Vector2 textureCoordinate)
{
this.Position = position;
this.Normal = normal;
this.TextureCoordinate = textureCoordinate;
}
public override int GetHashCode()
{
// TODO: FIc gethashcode
return 0;
}
public override string ToString()
{
return string.Format("{{Position:{0} Normal:{1} TextureCoordinate:{2}}}", new object[] { this.Position, this.Normal, this.TextureCoordinate });
}
public static bool operator ==(VertexPositionNormalTexture left, VertexPositionNormalTexture right)
{
return (((left.Position == right.Position) && (left.Normal == right.Normal)) && (left.TextureCoordinate == right.TextureCoordinate));
}
public static bool operator !=(VertexPositionNormalTexture left, VertexPositionNormalTexture right)
{
return !(left == right);
}
public override bool Equals(object obj)
{
if (obj == null)
{
return false;
}
if (obj.GetType() != base.GetType())
{
return false;
}
return (this == ((VertexPositionNormalTexture)obj));
}
}
The ported Unity work is not working properly. No shells and the output image is flat.
This is the expected result in XNA (Works fine):
But this is what I see in Unity (no shells):
The final image supposed to look like the image below but I can't go on with the porting work since the basic implementation is not working properly in Unity.
My script public variable settings:
Why is the the ported Unity result flat? Did I miss anything?
EDIT:
Leo mentioned about possible backface problem because Unity uses the left-handed coordinate system while XNA uses the right-handed coordinate system.
I flipped the UnitZ() value and also tried reversing the mesh vertices but there was nothing on the screen. This is not likely the issue.
Unity is doing a batch optimization on the material. You can see this in the frame debugger. Each DrawGeometry call is using the same value for CurrentLayer. You need to use a propertyblock for each call to DrawMesh.
Setting a new material causes some flickering.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
namespace foo {
public class FurBehavior : MonoBehaviour
{
public Material material;
public Vector3 pos = new Vector3(0f, 0.98f, -9.54f);
//simple camera for use in the game
private new Camera camera;
//texture containing fur data
public Texture2D furTexture;
//effect for fur shaders
//Effect furEffect;
//number of layers of fur
public int nrOfLayers = 40;
//total length of the hair
public float maxHairLength = 2.0f;
//density of hair
public float density = 0.2f;
//[Space(20)]
//public Vector3 dirWorldVal = new Vector3(0, -10, 0);
void Start()
{
this.transform.position = new Vector3(0f, 0.98f, -9.54f);
this.transform.rotation = Quaternion.Euler(0, 180, 0);
Initialize();
GenerateGeometry();
}
public void Update()
{
Draw();
}
void Initialize()
{
//Initialize the camera
camera = Camera.main;
//create the texture
furTexture = new Texture2D(256, 256, TextureFormat.ARGB32, false);
furTexture.wrapModeU = TextureWrapMode.Repeat;
furTexture.wrapModeV = TextureWrapMode.Repeat;
//furTexture.filterMode = FilterMode.Point;
//fill the texture
FillFurTexture(furTexture, density);
/*XNA's SurfaceFormat.Color is ARGB.
//https://gamedev.stackexchange.com/a/6442/98839*/
if (material.mainTexture != null)
{
material.mainTexture.wrapModeU = TextureWrapMode.Repeat;
material.mainTexture.wrapModeV = TextureWrapMode.Repeat;
// material.mainTexture.filterMode = FilterMode.Point;
}
}
bool firstDraw = true;
protected void Draw()
{
var pos = this.transform.position;
camera.backgroundColor = CornflowerBlue();
Matrix4x4 worldValue = Matrix4x4.Translate(pos);
Matrix4x4 viewValue = camera.projectionMatrix;
// viewValue = camera.worldToCameraMatrix;
Matrix4x4 projectionValue = camera.projectionMatrix;
material.SetMatrix("World", worldValue);
material.SetMatrix("View", viewValue);
material.SetMatrix("Projection", projectionValue); //Causes object to disappear
material.SetFloat("MaxHairLength", maxHairLength);
//if (firstDraw)
material.SetTexture("_MainTex", furTexture);
//furEffect.Begin();
for (int i = 0; i < nrOfLayers; i++)
{
var propertyBlock = new MaterialPropertyBlock();
var layer = (float)i / (float)nrOfLayers;
propertyBlock.SetFloat("CurrentLayer", layer);
propertyBlock.SetFloat("MaxHairLength", maxHairLength);
propertyBlock.SetColor("_TintColor", new Color(layer, layer, layer, layer));
DrawGeometry(propertyBlock);
}
if (firstDraw)
{
material.mainTexture.wrapModeU = TextureWrapMode.Repeat;
material.mainTexture.wrapModeV = TextureWrapMode.Repeat;
material.mainTexture.filterMode = FilterMode.Point;
}
if (firstDraw)
firstDraw = false;
}
void DrawGeometry(MaterialPropertyBlock props)
{
var rot = Quaternion.Euler(0, 180, 0);
Graphics.DrawMesh(verticesMesh, pos, rot, material, 0, camera, 0, props);
}
private VertexPositionNormalTexture[] verticesPText;
public Mesh verticesMesh;
private void GenerateGeometry()
{
var UnitZ = new Vector3(0, 0, 1);
var verticesPText = new VertexPositionNormalTexture[6];
verticesPText[5] = new VertexPositionNormalTexture(new Vector3(-10, 0, 0),
-UnitZ,
new Vector2(0, 0));
verticesPText[4] = new VertexPositionNormalTexture(new Vector3(10, 20, 0),
-UnitZ,
new Vector2(1, 1));
verticesPText[3] = new VertexPositionNormalTexture(new Vector3(-10, 20, 0),
-UnitZ,
new Vector2(0, 1));
verticesPText[2] = verticesPText[5];
verticesPText[1] = new VertexPositionNormalTexture(new Vector3(10, 0, 0),
-UnitZ,
new Vector2(1, 0));
verticesPText[0] = verticesPText[4];
}
Mesh VertexPositionNormalTextureToUnityMesh(VertexPositionNormalTexture[] vpnt)
{
Vector3[] vertices = new Vector3[vpnt.Length];
Vector3[] normals = new Vector3[vpnt.Length];
Vector2[] uvs = new Vector2[vpnt.Length];
int[] triangles = new int[vpnt.Length];
//Copy variables to create a mesh
for (int i = 0; i < vpnt.Length; i++)
{
vertices[i] = vpnt[i].Position;
normals[i] = vpnt[i].Normal;
uvs[i] = vpnt[i].TextureCoordinate;
triangles[i] = i;
}
Mesh mesh = new Mesh();
mesh.vertices = vertices;
mesh.normals = normals;
mesh.uv = uvs;
mesh.MarkDynamic();
mesh.triangles = triangles;
mesh.UploadMeshData(false);
return mesh;
}
private void FillFurTexture(Texture2D furTexture, float density)
{
//read the width and height of the texture
int width = furTexture.width;
int height = furTexture.height;
int totalPixels = width * height;
//an array to hold our pixels
Color32[] colors = new Color32[totalPixels];
//random number generator
System.Random rand = new System.Random();
//initialize all pixels to transparent black
for (int i = 0; i < totalPixels; i++)
colors[i] = TransparentBlack();
//compute the number of opaque pixels = nr of hair strands
int nrStrands = (int)(density * totalPixels);
//fill texture with opaque pixels
for (int i = 0; i < nrStrands; i++)
{
int x, y;
//random position on the texture
x = rand.Next(height);
y = rand.Next(width);
//put color (which has an alpha value of 255, i.e. opaque)
// colors[x * width + y] = new Color32((byte)255, (byte)x, (byte)y, (byte)255);
colors[x * width + y] = Gold();
}
//set the pixels on the texture.
furTexture.SetPixels32(colors);
// actually apply all SetPixels, don't recalculate mip levels
furTexture.Apply();
}
Color32 TransparentBlack()
{
//http://www.monogame.net/documentation/?page=P_Microsoft_Xna_Framework_Color_TransparentBlack
Color32 color = new Color32(0, 0, 0, 0);
return color;
}
Color32 Gold()
{
//http://www.monogame.net/documentation/?page=P_Microsoft_Xna_Framework_Color_Gold
Color32 color = new Color32(255, 215, 0, 255);
return color;
}
Color32 CornflowerBlue()
{
//http://www.monogame.net/documentation/?page=P_Microsoft_Xna_Framework_Color_CornflowerBlue
Color32 color = new Color32(100, 149, 237, 255);
return color;
}
public static Vector3 UnitZ()
{
return new Vector3(0f, 0f, 1f);
}
}
}
I also modified the shader to visualize the shells.
// Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with
'UnityObjectToClipPos(*)'
Shader "Programmer/Fur Shader"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_TintColor("Tint Color", Color) = (1,1,1,1)
}
SubShader
{
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }
LOD 100
//Blend SrcAlpha One
//Blend DstAlpha OneMinusSrcAlpha
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
// make fog work
//#pragma multi_compile_fog
#include "UnityCG.cginc"
//In
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
//Out
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
struct VertexShaderInput
{
float3 Position : POSITION0;
float3 Normal : NORMAL0;
float2 TexCoord : TEXCOORD0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
float4 Tint: COLOR1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
//Test variable/delete after
float4 _TintColor;
//The variables
float4x4 World;
float4x4 View;
float4x4 Projection;
float CurrentLayer; //value between 0 and 1
float MaxHairLength; //maximum hair length
VertexShaderOutput vert(VertexShaderInput input)
{
VertexShaderOutput output;
float3 pos;
pos = input.Position + input.Normal * MaxHairLength * CurrentLayer;
//float4 worldPosition = mul(float4(pos, 1), World);
//float4 viewPosition = mul(worldPosition, View);
output.Position = UnityObjectToClipPos(pos);
output.TexCoord = input.TexCoord;
output.Tint = float4(CurrentLayer, CurrentLayer, 0, 1);
return output;
}
float4 frag(VertexShaderOutput i) : COLOR0
{
float4 t = tex2D(_MainTex, i.TexCoord) * i.Tint;
return t;//float4(t, i.x, i.y, 1);
}
ENDCG
}
}
}
Here's what it looks like after messing with the parameters and moving the camera a bit.
I'm using unity version 5.6.1f1 Personal.
I'm trying to use this shader code and csharp script from wiki page:
Shield
In the editor i did: Assets > Create > Shader > Standrad Surface Shader
The double click on the created shader opened it in visual studio and added this code:
Shader "Custom/NewSurfaceShader" {
Properties
{
_Color("_Color", Color) = (0.0,1.0,0.0,1.0)
_Inside("_Inside", Range(0.0,2.0)) = 0.0
_Rim("_Rim", Range(0.0,1.0)) = 1.2
_Texture("_Texture", 2D) = "white" {}
_Speed("_Speed", Range(0.5,5.0)) = 0.5
_Tile("_Tile", Range(1.0,10.0)) = 5.0
_Strength("_Strength", Range(0.0,5.0)) = 1.5
}
SubShader
{
Tags
{
"Queue" = "Transparent"
"IgnoreProjector" = "True"
"RenderType" = "Transparent"
}
Cull Back
ZWrite On
ZTest LEqual
CGPROGRAM
#pragma surface surf BlinnPhongEditor alpha vertex:vert
//#pragma target 3.0
fixed4 _Color;
sampler2D _CameraDepthTexture;
fixed _Inside;
fixed _Rim;
sampler2D _Texture;
fixed _Speed;
fixed _Tile;
fixed _Strength;
struct EditorSurfaceOutput
{
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
};
inline half4 LightingBlinnPhongEditor_PrePass(EditorSurfaceOutput s, half4 light)
{
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha + Luminance(spec);
return c;
}
inline half4 LightingBlinnPhongEditor(EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
viewDir = normalize(viewDir);
half3 h = normalize(lightDir + viewDir);
half diff = max(0, dot(s.Normal, lightDir));
float nh = max(0, dot(s.Normal, h));
float3 spec = pow(nh, s.Specular*128.0) * s.Gloss;
half4 res;
res.rgb = _LightColor0.rgb * (diff * atten * 2.0);
res.w = spec * Luminance(_LightColor0.rgb);
return LightingBlinnPhongEditor_PrePass(s, res);
}
struct Input
{
float4 screenPos;
float3 viewDir;
float2 uv_Texture;
};
void vert(inout appdata_full v, out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
}
void surf(Input IN, inout EditorSurfaceOutput o)
{
o.Albedo = fixed3(0.0,0.0,0.0);
o.Normal = fixed3(0.0,0.0,1.0);
o.Emission = 0.0;
o.Gloss = 0.0;
o.Specular = 0.0;
o.Alpha = 1.0;
float4 ScreenDepthDiff0 = LinearEyeDepth(tex2Dproj(_CameraDepthTexture, UNITY_PROJ_COORD(IN.screenPos)).r) - IN.screenPos.z;
float4 Saturate0 = fixed4(0.3,0.3,0.3,1.0);//
float4 Fresnel0_1_NoInput = fixed4(0,0,1,1);
float dNorm = 1.0 - dot(normalize(float4(IN.viewDir, 1.0).xyz), normalize(Fresnel0_1_NoInput.xyz));
float4 Fresnel0 = float4(dNorm,dNorm,dNorm,dNorm);
float4 Step0 = step(Fresnel0,float4(1.0, 1.0, 1.0, 1.0));
float4 Clamp0 = clamp(Step0,_Inside.xxxx,float4(1.0, 1.0, 1.0, 1.0));
float4 Pow0 = pow(Fresnel0,(_Rim).xxxx);
float4 Multiply5 = _Time * _Speed.xxxx;
float4 UV_Pan0 = float4((IN.uv_Texture.xyxy).x,(IN.uv_Texture.xyxy).y + Multiply5.x,(IN.uv_Texture.xyxy).z,(IN.uv_Texture.xyxy).w);
float4 Multiply1 = UV_Pan0 * _Tile.xxxx;
float4 Tex2D0 = tex2D(_Texture,Multiply1.xy);
float4 Multiply2 = Tex2D0 * _Strength.xxxx;
float4 Multiply0 = Pow0 * Multiply2;
float4 Multiply3 = Clamp0 * Multiply0;
float4 Multiply4 = Saturate0 * Multiply3;
o.Emission = Multiply3.xyz * _Color.rgb;
o.Alpha = Multiply3.w * _Color.a;
}
ENDCG
}
Fallback "Diffuse"
}
Then created a csharp script file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ShieldUvAnimation : MonoBehaviour {
public GameObject iShield;
public float iSpeed;
private Material mMaterial;
private float mTime;
// Use this for initialization
void Start()
{
mMaterial = iShield.GetComponent<Renderer>().material;
mTime = 0.0f;
}
// Update is called once per frame
void Update()
{
mTime += Time.deltaTime * iSpeed;
mMaterial.SetFloat("_Offset", Mathf.Repeat(mTime, 1.0f));
}
}
Then i created a material new material and dragged the shader to the material.
Then created a new empty GameObject.
Then attached the csharp script to the new empty GameObject.
Now the first problem i had is that i could not drag the new material to the empty GameObject. So i changed the csharp script i changed the line:
private Material mMaterial;
To be public
public Material mMaterial;
And removed the line in the Start:
mMaterial = iShield.GetComponent<Renderer>().material;
Then i dragged the new material to the public mMaterial.
Last i dragged my spaceship object to the iShield.
I changed the iSpeed to 1.
Running the game i'm not getting any errors or exceptions but i don't see any shield.
Your shader doesn't have property "_Offset".
Script "ShieldUvAnimation" will work with "ShaderLab - ForceField.shader".
Your link shield
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.
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();
}
}