Ive been trying to construct a Cube Primitive using Shaders, but havent really got anything to appear on the screen. Im using Unity Pro 4.3.1f1 at Windows 7 and using a GTX 260 as graphic card. The “Use Direct 3D 11” option at Player Settings is enable.
The reason I need to construct a Cube Primitive using Shader is that I need to render a Point Cloud (10k+ points), where every point has got to be rendered using cubes.
This is my try so far:
Shader "Custom/CubeColor"
{
Properties
{
_MainTex ("TileTexture", 2D) = "white" {}
_PointSize("PointSize", Float) = 10
_MyColor ("Some Color", Color) = (0,1,0,1)
}
SubShader
{
LOD 200
Pass
{
CGPROGRAM
#pragma only_renderers d3d11
#pragma target 4.0
#include "UnityCG.cginc"
#pragma vertex myVertexShader
#pragma geometry myGeometryShader
#pragma fragment myFragmentShader
struct vIn // Into the vertex shader
{
float4 vertex : POSITION;
float4 color : COLOR0;
};
struct gIn // OUT vertex shader, IN geometry shader
{
float4 pos : SV_POSITION;
float4 col : COLOR0;
float size : PSIZE;
};
struct v2f // OUT geometry shader, IN fragment shader
{
float4 pos : SV_POSITION;
float2 uv_MainTex : TEXCOORD0;
float4 col : COLOR0;
};
float4 _MainTex_ST;
sampler2D _MainTex;
float _PointSize;
fixed4 _MyColor;
// ----------------------------------------------------
gIn myVertexShader(vIn v)
{
gIn o; // Out here, into geometry shader
UNITY_INITIALIZE_OUTPUT(gIn,o);
// Passing on color to next shader (using .r/.g there as tile coordinate)
o.col = _MyColor;//v.color;
o.size = _PointSize;
// Passing on center vertex (tile to be built by geometry shader from it later)
o.pos = v.vertex;
return o;
}
[maxvertexcount(10)] // 8 vertex
// ----------------------------------------------------
// Using "point" type as input, not "triangle"
void myGeometryShader(point gIn vert[1], inout TriangleStream<v2f> triStream)
{
const float f = _PointSize/2; //half size
The 8 vertex positions of a CUBE tile
const float4 vc[8] = { float4( -f, -f, -f, 0.0f ),
float4( -f, -f, +f, 0.0f ),
float4( -f, +f, -f, 0.0f ),
float4( -f, +f, +f, 0.0f ),
float4( +f, -f, -f, 0.0f ),
float4( +f, -f, +f, 0.0f ),
float4( +f, +f, -f, 0.0f ),
float4( +f, +f, +f, 0.0f ) };
const int TRI_STRIP_CUBE[9] = { 0, 2, 1, 3, 5, 7, 4, 6, 0 };
v2f v[12]; //for CUBE
// Assign new vertices positions (12 new tile vertices, forming CUBE)
for (int i=0;i<12;i++) { v_.pos = vert[0].pos + vc*; }*_
* // Position in view space*
for (int i=0;i<6;i++) { v.pos = mul(UNITY_MATRIX_MVP, v*.pos); }*
* // Build the CUBE tile by submitting triangle strip vertices*
for (int i=0;i<9;i++) triStream.Append(v[TRI_STRIP_CUBE*]);
_ triStream.RestartStrip();
}*_
* // ----------------------------------------------------*
* half4 myFragmentShader(v2f IN) : COLOR*
* {*
* // Not considering normals/light here. Just texture*
* half4 c = tex2D (MainTex, IN.uv_MainTex);*
* return c;
}*_
* ENDCG*
* }*
* }*
* FallBack “Diffuse”*
}
And the test Mesh is created with the following method:
void CreateMesh( int startRange, int endRange, Mesh currMesh ) {
* Vector3[] points = new Vector3[maxPointsPerMesh];*
* int[] indexes = new int[maxPointsPerMesh];*
* Color[] colors = new Color[maxPointsPerMesh];*
* for(int i=0;i<points.Length;++i) {*
_ points = new Vector3(Random.Range(startRange,endRange), Random.Range (startRange,endRange), Random.Range (startRange,endRange));
indexes = i;
colors = new Color(Random.Range(0.0f,1.0f),Random.Range (0.0f,1.0f),Random.Range(0.0f,1.0f),1.0f);
* }*_
* currMesh.vertices = points;*
* currMesh.colors = colors;*
* currMesh.SetIndices(indexes, MeshTopology.Points,0);*
* }*
My Game Object has a MeshFilter and a MeshRenderer.
What am I doing wrong?
Thank you all!