3 registered members (AndrewAMD, wandaluciaia, 1 invisible),
798
guests, and 6
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
Re: Ways to send variables/locations to entity shaders
[Re: jumpman]
#473704
08/07/18 08:25
08/07/18 08:25
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, materials point to their directX LPD3DXEFFECT struct. It has many methods to modify the static table of the effect.
LPD3DXEFFECT _fx = _mtl->d3deffect;
if (_fx == NULL)
error("unable to retrieve the effect of a material");
float _f[4] = ...;
if(_fx->SetVector("customName", _f) != NULL) // see: SetFloat, SetVectorArray, SetMatrix, SetMatrixArray...
error("unable to modify the static table of an effect");
https://docs.microsoft.com/en-us/windows/desktop/direct3d9/id3dxbaseeffectSalud!
|
|
|
Re: Ways to send variables/locations to entity shaders
[Re: jumpman]
#473710
08/07/18 14:48
08/07/18 14:48
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
Hi, you need to include windows.h, the definition is into d3d9.h ENABLE_RENDER material flag lets you modify the render of each visible entity which has the material assigned. (untested code)
#define skFloatArray skill20
...
function evnMtl00 () {
if (mtl->d3deffect == NULL) // no effect loaded?
return 1; // do not render
if (me == NULL) // not an actual entity?
return 0; // render as it is
LPD3DXEFFECT _fx = mtl->d3deffect;
// fill a float array, or retrieve a pointer to an array from the entity skill or whatever
float *_fArray = (float*)my->skFloatArray;
_fx->SetVector("myFloats", _fArray);
return 0;
}
MATERIAL *mtl00 = {
event = evnMtl00;
flags = ENABLE_RENDER;
}
This is only needed when each entity has its own values for the same variables.
|
|
|
Re: Ways to send variables/locations to entity shaders
[Re: txesmi]
#473713
08/07/18 15:56
08/07/18 15:56
|
Joined: Apr 2002
Posts: 1,246 ny
jumpman
OP
Serious User
|
OP
Serious User
Joined: Apr 2002
Posts: 1,246
ny
|
Hey txesmi thanks for helping me, I think thats what im going for, but i get a script crash in the event, I think its this line?:
float *_fArray = (float*)my->skFloatArray;
Last edited by jumpman; 08/07/18 16:04. Reason: script crashes when line is added to .fx file, see post below
|
|
|
Re: Ways to send variables/locations to entity shaders
[Re: jumpman]
#473714
08/07/18 16:03
08/07/18 16:03
|
Joined: Apr 2002
Posts: 1,246 ny
jumpman
OP
Serious User
|
OP
Serious User
Joined: Apr 2002
Posts: 1,246
ny
|
If I go into my .FX file and I place this: The script will crash when the engine runs. i can see this in the d3d9.h
STDMETHOD(SetVector)(THIS_ D3DXHANDLE hParameter, CONST D3DXVECTOR4* pVector) PURE;
in the .fx file ive done this as well: and the script still crashes in the material event
Last edited by jumpman; 08/07/18 16:08.
|
|
|
Re: Ways to send variables/locations to entity shaders
[Re: txesmi]
#473718
08/07/18 16:58
08/07/18 16:58
|
Joined: Apr 2002
Posts: 1,246 ny
jumpman
OP
Serious User
|
OP
Serious User
Joined: Apr 2002
Posts: 1,246
ny
|
That was unrelated to your work, Im sorry, I had a wait(1); by mistake in the material event, which caused the functions to increase when i paused the game, forgive me. Back on track, heres what Im doing at the top of my entity action:
Action tester_ent()
{
float4 *myFloatsArray = sys_malloc(sizeof(float) * 4); // only once!
my->skFloatArray = (var)myFloatsArray;
...
my.material = mtl_Hero; //---------Assign the Hero shader
while(1)
// changing the variable
myFloatsArray[0] = (float)my.x+random(200)-random(200);
myFloatsArray[1] = (float)my.y+random(200)-random(200);
myFloatsArray[2] = (float)my.z+random(200)-random(200);
myFloatsArray[3] = (float)0;
wait(1);
}
Here is the material event:
hero_event()
{
...
if (mtl->d3deffect == NULL) // no effect loaded?
return 1; // do not render
if (me == NULL) // not an actual entity?
return 0; // render as it is
LPD3DXEFFECT _fx = mtl->d3deffect;
float *_fArray = (float*)my->skFloatArray; // fill a float array, or retrieve a pointer to an array from the entity skill or whatever
_fx->SetVector("myFloats", _fArray);
//
return 0;
}
Is that correct? This all doesnt crash until i add myFloats to the .fx file
|
|
|
Re: Ways to send variables/locations to entity shaders
[Re: jumpman]
#473719
08/07/18 17:20
08/07/18 17:20
|
Joined: Jun 2007
Posts: 1,337 Hiporope and its pain
txesmi
Serious User
|
Serious User
Joined: Jun 2007
Posts: 1,337
Hiporope and its pain
|
The effect compiler errors are always described in a prompt window. It does never crash. I don't know what could be happening Here goes a complete and tested example of the very same
#include <acknex.h>
#include <windows.h>
#include <d3d9.h>
#define skFloatArray skill20
function evnCamMtl () {
if (mtl->d3deffect == NULL)
return 1;
if (me == NULL)
return 0;
if (my->skFloatArray == 0)
return 0;
LPD3DXEFFECT _fx = mtl->d3deffect;
_fx->SetVector("myFloats", (float*)my->skFloatArray);
return 0;
}
MATERIAL *mtlCam = {
event = evnCamMtl;
flags = ENABLE_RENDER;
effect = "
const float4x4 matWorldViewProj;
float4 myFloats;
void VS (
in float4 inPos : POSITION,
out float4 outPos : POSITION) {
outPos = mul ( inPos, matWorldViewProj );
}
float4 PS () : COLOR0 {
return myFloats;
}
technique tech {
pass p0 {
ZWriteEnable = True;
AlphaBlendEnable = False;
VertexShader = compile vs_3_0 VS();
PixelShader = compile ps_3_0 PS();
}
}
";
}
action actSphere () {
float *_fV = sys_malloc(sizeof(float) * 4);
memset(_fV, 0, sizeof(float) * 4);
my->skFloatArray = (var)_fV;
my->material = mtlCam;
while (!key_esc) {
_fV[0] = random(1);
_fV[1] = random(1);
_fV[2] = random(1);
_fV[3] = random(1);
wait(1);
}
sys_free(_fV);
ent_remove(me);
}
void main () {
wait(1);
level_load ("");
camera->x = -200;
int _count = 0;
for (; _count<5; _count+=1)
ENTITY *_ent = ent_create(SPHERE_MDL, vector(0, _count*50, 0), actSphere);
while (!key_esc) {
camera->pan = ang(camera->pan - mickey.x * 0.2);
camera->tilt = clamp(camera->tilt - mickey.y * 0.2, -90, 90);
wait(1);
}
wait(1);
sys_exit(NULL);
}
|
|
|
|