eee5
Community Member

Posts: 4
|
 |
« on: August 07, 2011, 09:05:50 AM » |
|
Hi guys, I have a problem with minimesh shaders. I want to use shader for minimesh per-pixel lighting which I found here: http://www.truevision3d.com/forums/tv3d_sdk_65/minimesh_lighting-t19807.0.html;msg135808#msg135808but if I put it in my code, minimeshes are not visible. I have same problem with CustomMinimesh.fx and any shader. I dont know what I am doing wrong, here is my code: Shader2 = Scene.CreateShader(); Shader2.CreateFromEffectFile("D:\\C#_TV3D engine_dead forrest\\data\\minimeshLight.fx");
Cube = Scene.CreateMeshBuilder("Cube"); Cube.CreateBox(40, 40, 40, false); Cube.SetTexture(IDDiffuse2, -1); Cube.SetTextureEx((int)CONST_TV_LAYER.TV_LAYER_NORMALMAP, IDNormal2, -1); Cube.SetMaterial(IDMat2, -1); Cube.SetLightingMode(CONST_TV_LIGHTINGMODE.TV_LIGHTING_BUMPMAPPING_TANGENTSPACE); Cube.SetShadowCast(true, false); Cube.SetShader(Shader2);
Env_CubeInstancer = Scene.CreateMiniMesh(1000, "cubeInstances"); Env_CubeInstancer.CreateFromMesh(Cube, false); Env_CubeInstancer.EnableFrustumCulling(true, true); Env_CubeInstancer.SetColorMode(true); Env_CubeInstancer.SetShader(Shader2);
for (int i = 0; i < 1000; i++) { int X = rnd.Next(0, 1024); int Z = rnd.Next(0, 1024); float Y = Land.GetHeight((float)X, (float)Z);
int rY = rnd.Next(0, 360);
Env_CubeInstancer.SetPosition(-1000, 0, -1000, i); Env_CubeInstancer.SetScale(0.2f, 0.2f, 0.2f, i); Env_CubeInstancer.EnableMiniMesh(i, false); Env_CubeInstancer.SetRotation(0, (float)rY, 0, i); Env_CubeInstancer.SetColor(new TV_COLOR(1f, 1f, 1f, 1f).GetIntColor(), i); } they are normaly visible when I remove shader from instancer "Env_CubeInstancer.SetShader(Shader2);" Please guys help me I need spot light on minimesh and all i need to know how put any shader on it. THANKS A LOT!!!
|
|
|
|
|
Logged
|
|
|
|
Dimple
Community Member

Posts: 580
|
 |
« Reply #1 on: August 07, 2011, 07:15:38 PM » |
|
 If you are using the original minimesh shader code check out this thread. http://www.truevision3d.com/forums/shader_development/mini_mesh_shaders-t19178.0.html there's been updates to it. Found it, finally here's an updated version. // // Minimesh custom shader example_v2 // //
// we need an array of matrices // max : 52 matrices for vS2.0 // 16 matrices for VS1.1 // this array must have the semantic : MINIMESH_WORLD // it can be 4x3 or 4x4 float4x3 minimeshes[52] : MINIMESH_WORLD;
// we can have the colors too float4 minimeshes_color[52] : MINIMESH_COLOR; // only used when MiniMesh.SetColorMode is enabled.
float4 minimeshes_fade : MINIMESH_FADE; // fading options from the engine. // VECTOR4(fFarDistance, fStartFading, fMaxAlpha, fMaxAlpha/(fStartFading - fFarDistance)); // the last parameter can obviously be used to optimize the fade computation as seen in the example below.
float3 CameraPos : CAMERAPOSITION; float3 CameraDir : CAMERADIRECTION;
// the usual view projection matrix float4x4 viewProj : VIEWPROJECTION;
// our texture used on the minimesh texture tex : TEXTURE0; sampler samp1 : register(s0);
struct VS_STANDARD_VERTEX { float4 position : POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD0; float2 index : TEXCOORD3; };
struct VS_OUTPUT { float4 position : POSITION; float2 tex : TEXCOORD0; float4 color : COLOR0; };
VS_OUTPUT VS_MiniMesh(VS_STANDARD_VERTEX vertex) { VS_OUTPUT o; // the index of the minimesh matrix to use // is in index.x, let's use it ! float4x3 ourMatrix = minimeshes[vertex.index.x];
// now we can just multiply the vertex by this matrix // and by the view projection float3 worldPos = mul(vertex.position, ourMatrix); // here of course we can do some vertex modifications // with wind etc. o.position = mul(float4(worldPos,1.0f), viewProj);
// we transmit the texture coordinates to the pixel shader o.tex = vertex.tex;
// now we choose the good color // be sure to have called SetColorMode(true) and set good colors with non-null alpha // for the minimeshes. o.color = minimeshes_color[vertex.index.x];
// if you don't want to deal with colors, just use that: //o.color = float4(1,1,1,1);
// handle the fading directly in vertex shader. // float dist = dot(p-CameraPos,CameraDir); // o.color.alpha = saturate( (dist - mMiniMesh.x) * mMiniMesh.w);
return o; }
// a very simple pixel shader to go with this vertex shader. float4 PS_MiniMesh(VS_OUTPUT input) : COLOR0 { return input.color * tex2D(samp1,input.tex); }
technique minimesh { pass pass0 { VertexShader = compile vs_2_0 VS_MiniMesh(); PixelShader = compile ps_1_1 PS_MiniMesh(); } }
|
|
|
|
« Last Edit: August 07, 2011, 08:52:05 PM by Dimple »
|
Logged
|
Using VB.NET, TV3D 6.5, VISTA ~~~~~~~~~~~~~~~~~~~~~~~
"Know how to ask. There is nothing more difficult for some people, nor for others, easier."
- Baltasar Gracian
|
|
|
|
|
eee5
Community Member

Posts: 4
|
 |
« Reply #3 on: August 08, 2011, 05:04:45 AM » |
|
Thanks Dimple, but I have that custom shader updated and its still the same. I cant run shaders with minimeshes and i dont know why, please help i need that lighting, thanks.
|
|
|
|
|
Logged
|
|
|
|
frank7723
Community Member

Posts: 5
|
 |
« Reply #4 on: August 10, 2011, 03:23:08 PM » |
|
I have not been able to run shaders with minimeshes either. I have searched this forum and others but have not found an answer. Is there anyone who can help? I wonder if you guys have thought about building some sort of mobile app platform for support.
|
|
|
|
« Last Edit: August 16, 2011, 02:07:32 PM by frank7723 »
|
Logged
|
|
|
|
jviper
Community Member

Posts: 2130
Discipline in training
|
 |
« Reply #5 on: August 10, 2011, 06:03:41 PM » |
|
Seems to work fine for me. I have the following shader code: float4x3 minimeshes[52] : MINIMESH_WORLD;
// we can have the colors too float4 minimeshes_color[52] : MINIMESH_COLOR; // only used when MiniMesh.SetColorMode is enabled.
float4 minimeshes_fade : MINIMESH_FADE; // fading options from the engine. // VECTOR4(fFarDistance, fStartFading, fMaxAlpha, fMaxAlpha/(fStartFading - fFarDistance)); // the last parameter can obviously be used to optimize the fade computation as seen in the example below.
float3 CameraPos : CAMERAPOSITION; float3 CameraDir : CAMERADIRECTION;
// the usual view projection matrix float4x4 matWorld : WORLDMATRIX; float4x4 viewProj : VIEWPROJECTION; float4x4 Proj : PROJECTIONMATRIX; float4x4 View : VIEW;
float4 ambientSys: AMBIENT; float4 diffuseSys: DIFFUSE; float4 specularSys: SPECULAR; float4 emissiveSys: EMISSIVE; float specularpowerSys: SPECULARPOWER; // our texture used on the minimesh texture SysTex : TEXTURE0; sampler2D Sampl1 = sampler_state { Texture = (SysTex); };
struct VS_STANDARD_VERTEX { float4 position : POSITION; float3 normal : NORMAL; float2 tex : TEXCOORD0; int index : TEXCOORD3; };
struct VS_OUTPUT { float4 position : POSITION; float4 color : TEXCOORD0; float2 tex : TEXCOORD1; float3 normal : TEXCOORD2; float3 pos : TEXCOORD3; float3 viewdir : TEXCOORD4; };
VS_OUTPUT VS_MiniMesh(VS_STANDARD_VERTEX vertex) { VS_OUTPUT o; // the index of the minimesh matrix to use // is in index.x, let's use it ! int idx = vertex.index; float4x3 ourMatrix = minimeshes[idx];
// now we can just multiply the vertex by this matrix // and by the view projection float3 worldPos = mul(vertex.position, ourMatrix); //float3 worldPos = mul(vertex.position, (float4x3)matWorld); // here of course we can do some vertex modifications // with wind etc. o.position = mul(float4(worldPos, 1.0f), viewProj); //o.normal = normalize(mul(vertex.normal,ourMatrix)); o.normal = mul(normalize(vertex.normal),(float3x3)ourMatrix); o.pos = worldPos;
// we transmit the texture coordinates to the pixel shader o.tex = vertex.tex;
// now we choose the good color // be sure to have called SetColorMode(true) and set good colors with non-null alpha // for the minimeshes. o.color = minimeshes_color[idx];
// if you don't want to deal with colors, just use that: //o.color = float4(1,1,1,1); //o.color = diffuseSys;
// handle the fading directly in vertex shader. // float dist = dot(p-CameraPos,CameraDir); // o.color.alpha = saturate( (dist - mMiniMesh.x) * mMiniMesh.w);
o.viewdir = normalize(CameraPos - worldPos); //o.viewdir = normalize(CameraPos);
return o; }
// a very simple pixel shader to go with this vertex shader. float4 PS_MiniMesh(VS_OUTPUT input) : COLOR0 { float4 Col = input.color; float NdotL = dot(input.viewdir, normalize(input.normal)); float spec = pow(NdotL,96); Col = float4((Col.rgb * NdotL) + (float4(1,1,1,1)*spec), Col.a) ;//* tex2D(Sampl1,input.tex); //Col = float4(float3(1.0f,1.0f,1.0f) * dot(input.viewdir, normalize(input.normal)), 1.0f) * tex2D(Sampl1,input.tex); //Col = float4(float3(1.0f,1.0f,1.0f), 1.0f) ; //* tex2D(Sampl1,input.tex); //Col = float4(1.0f, 1.0f, 1.0f, 1.0f); return Col; }
technique minimesh { pass pass0 { VertexShader = compile vs_2_0 VS_MiniMesh(); PixelShader = compile ps_2_0 PS_MiniMesh(); } }
Btw, set the shader on the tvminimesh system, not the tvmesh.
|
|
|
|
« Last Edit: August 10, 2011, 06:07:07 PM by jviper »
|
Logged
|
JAbstract.....Don't just imagine, make it happen!
|
|
|
eee5
Community Member

Posts: 4
|
 |
« Reply #6 on: August 11, 2011, 05:33:15 AM » |
|
Thanks jviper, first shader which working!!! But its not what I expect, with this code line in PS_MiniMesh(VS_OUTPUT input): Col = float4((Col.rgb * NdotL) + (float4(1,1,1,1)*spec), Col.a) * tex2D(Sampl1,input.tex); i get this: http://imageshack.us/photo/my-images/19/img1sj.jpg/"flashlight" repeats every 90 degrees, light does not rotate with camera. if i use this code line Col = float4(float3(1.0f,1.0f,1.0f) * dot(input.viewdir, normalize(input.normal)), 1.0f) * tex2D(Sampl1,input.tex); i get this: http://imageshack.us/photo/my-images/837/img2ly.jpg/http://imageshack.us/photo/my-images/64/img3gk.jpg/light illuminates grass front of me, but if i rotate with camera in 90 degrees it goes to dark, its like the light has complete bad position or view projection not working. is it the same in your programe? or i must set some parametres from my game with SetEffectParam? I simply need effect like when light illuminate in circle around player. Thank again for help.
|
|
|
|
|
Logged
|
|
|
|
jviper
Community Member

Posts: 2130
Discipline in training
|
 |
« Reply #7 on: August 13, 2011, 08:53:57 AM » |
|
Not sure what's going on here. The lighting in my program seems to be ok so I don't know why the lighting is not working in your program. It looks like the texture coordinates are not coming through the minimesh system correctly. It seems any texture coordinates over 1.0f, it clamps. If you were trying to implement normal mapping or bump mapping, you would run into problems. EDIT: I found the problem with the texture coordinates. I had to add the ADDRESSU = WRAP; ADDRESSV = WRAP; to the texture in the shader. texture SysTex : TEXTURE0; sampler2D Sampl1 = sampler_state { Texture = (SysTex); ADDRESSU = WRAP; ADDRESSV = WRAP; }; Other than that, I have not been able to find in the code I have where the issues you are having are.
|
|
|
|
« Last Edit: August 13, 2011, 10:20:32 AM by jviper »
|
Logged
|
JAbstract.....Don't just imagine, make it happen!
|
|
|
|
|
|