Jump to content

give visual, priority to object over the player


Krokara

Recommended Posts

  • Moderators
2 hours ago, Krokara said:

If the object doesn't need collision, you can create it with a lowLOD appearance.

https://wiki.multitheftauto.com/wiki/CreateObject

Syntax createObject function:

object createObject ( int modelId, float x, float y, float z, [ float rx, float ry, float rz, bool isLowLOD = false ] )

Set lowLOD to true.

That will increase draw distance.

 

Link to comment
Just now, IIYAMA said:

If the object doesn't need collision, you can create it with a lowLOD appearance.

https://wiki.multitheftauto.com/wiki/CreateObject

Syntax createObject function:

object createObject ( int modelId, float x, float y, float z, [ float rx, float ry, float rz, bool isLowLOD = false ] )

Set lowLOD to true.

That will increase draw distance.

 

You misunderstood I want the object to be visible even with the player in front of the object

 

Link to comment

Hello Krokara,

nice to see that you got that spherical 3D object into the game! You have made a nice looking effect with it, too. If you want to have a 3D object be always visible to the camera then I recommend you to set the depth value of each pixel inside of an assigned HLSL pixel shader to a very small value (close to 0 like 0.001). Also disable depth testing for all those pixels. Then your object is guaranteed to be in front of any other 3D object because it will be treated as closest 3D data to the game camera.

Hope this helps!

Edited by The_GTA
Link to comment
Just now, The_GTA said:

Hello Krokara,

nice to see that you got that spherical 3D object into the game! You have made a nice looking effect with it, too. If you want to have a 3D object be always visible to the camera then I recommend you to set the depth value of each pixel inside of an assigned HLSL pixel shader to a very small value (close to 0 like 0.001). Also disable depth testing for all those pixels. Then your object is guaranteed to be in front of any other 3D object because it will be treated as closest 3D data to the game camera.

Hope this helps!

https://wiki.multitheftauto.com/wiki/DepthBuffer

float4 PS_Example( PSInput In ) : COLOR
{
    float BufferValue = FetchDepthBufferValue( In.TexCoord.xy );
    float Depth = Linearize( BufferValue );
    
    Depth = 0.0f;
    return Depth;
}

so far is everything alright?

now i don't know how i can disable depth testing

Link to comment
1 hour ago, Krokara said:

now i don't know how i can disable depth testing

Take a look at https://docs.microsoft.com/de-de/windows/win32/direct3d9/effect-states

There you can find the RenderState property "ZEnable". Try to set ZEnable = FALSE; for your pass inside of your technique to disable depth testing.

If you want to know how to write to the depth in a pixel shader take a look at this:

c# - HLSL modify depth in pixel shader - Stack Overflow

Edited by The_GTA
Link to comment
1 hour ago, The_GTA said:

Take a look at https://docs.microsoft.com/de-de/windows/win32/direct3d9/effect-states

There you can find the RenderState property "ZEnable". Try to set ZEnable = FALSE; for your pass inside of your technique to disable depth testing.

If you want to know how to write to the depth in a pixel shader take a look at this:

c# - HLSL modify depth in pixel shader - Stack Overflow

spacer.png

I, couldn't change the depth

texture gDepthBuffer : DEPTHBUFFER;
Texture2D m_TextureColor;

sampler SamplerDepth = sampler_state
{
    Texture     = (gDepthBuffer);
    AddressU    = Clamp;
    AddressV    = Clamp;
};

SamplerState g_samPoint
{
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Wrap;
    AddressV = Wrap;
};



struct VS_IN
{
    float4 position : POSITION;
    float2 texcoord : TEXCOORD;
};

struct VS_OUT
{
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
};

struct PS_OUT
{
    float4 color : COLOR0;
    float depth : DEPTH0;
};

VS_OUT VS(VS_IN input)
{
    VS_OUT output = (VS_OUT)0;

    output.position = input.position;
    output.texcoord = input.texcoord;

    return output;
}

PS_OUT PS(VS_OUT input)
{
    PS_OUT output = (PS_OUT)0;

    // Now that output.depth is defined with SV_Depth, and you have depth-write enabled,
    // this should write to the depth buffer.
    output.depth = m_TextureColor.SampleLevel(g_samPoint, input.texcoord, 0);

    return output;
}
technique depthh
{
    pass P0
    {
        VertexShader = compile vs_3_0 VS();
        PixelShader  = compile ps_3_0 PS();
    }
}

ZEnable, it worked on the image correctly, but I think that the object did not work due to the depth that I could not modfy and it keeps disappearing object at times

Edited by Krokara
Link to comment

Why didn't you start out with a clean HLSL pixel shader file? I don't even understand what weird ... things you wrote in that HLSL file. Did you have a HLSL file prior to this mess or did you just take this from the MTA wiki? You really should not have taken anything without me telling you to.

texture m_TextureColor;

SamplerState g_samPoint
{
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Wrap;
    AddressV = Wrap;
};



struct VS_IN
{
    float4 position : POSITION;
    float2 texcoord : TEXCOORD;
};

struct VS_OUT
{
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
};

struct PS_OUT
{
    float4 color : COLOR0;
    float depth : DEPTH0;
};

VS_OUT VS(VS_IN input)
{
    VS_OUT output = (VS_OUT)0;

    output.position = input.position;
    output.texcoord = input.texcoord;

    return output;
}

PS_OUT PS(VS_OUT input)
{
    PS_OUT output = (PS_OUT)0;

    // Now that output.depth is defined with SV_Depth, and you have depth-write enabled,
    // this should write to the depth buffer.
    output.depth = 0;

    return output;
}
technique depthh
{
    pass P0
    {
        VertexShader = compile vs_3_0 VS();
        PixelShader  = compile ps_3_0 PS();
    }
}

You still have to add things like the color output as well as the render-state adjustment to the pass.

Edited by The_GTA
Link to comment
Just now, The_GTA said:

Why didn't you start out with a clean HLSL pixel shader file? I don't even understand what weird ... things you wrote in that HLSL file. Did you have a HLSL file prior to this mess or did you just take this from the MTA wiki? You really should not have taken anything without me telling you to.

Texture2D m_TextureColor;

SamplerState g_samPoint
{
    Filter = MIN_MAG_MIP_POINT;
    AddressU = Wrap;
    AddressV = Wrap;
};



struct VS_IN
{
    float4 position : POSITION;
    float2 texcoord : TEXCOORD;
};

struct VS_OUT
{
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
};

struct PS_OUT
{
    float4 color : COLOR0;
    float depth : DEPTH0;
};

VS_OUT VS(VS_IN input)
{
    VS_OUT output = (VS_OUT)0;

    output.position = input.position;
    output.texcoord = input.texcoord;

    return output;
}

PS_OUT PS(VS_OUT input)
{
    PS_OUT output = (PS_OUT)0;

    // Now that output.depth is defined with SV_Depth, and you have depth-write enabled,
    // this should write to the depth buffer.
    output.depth = 0;

    return output;
}
technique depthh
{
    pass P0
    {
        VertexShader = compile vs_3_0 VS();
        PixelShader  = compile ps_3_0 PS();
    }
}

You still have to add things like the color output as well as the render-state adjustment to the pass.

that Russian lightning effect I couldn't make it work so for now I'm not touching it this is another shader
 

spacer.png
object is not working like the image

I made a video
https://drive.google.com/file/d/1XV588GBdFpLUXZksPVjZSoPfpI5ss4Z1/view?usp=sharing

Edited by Krokara
I made a video
Link to comment
4 hours ago, The_GTA said:

Have you tried assigning the shader that I adjusted for you to the object? What is the effect?

//
// energyField.fx
// By: Brandon Fogerty 
// http://glslsandbox.com/e#25448.3
// glsl to hlsl translation by Ren712

// bfogerty at gmail dot com 
// xdpixel.com
// Special thanks to Inigo Quilez for noise!


//------------------------------------------------------------------------------------------
// //-- These two are set by MTA
//------------------------------------------------------------------------------------------//
matrix gProjectionMainScene : PROJECTION_MAIN_SCENE;


//------------------------------------------------------------------------------------------
// Shader settings
//------------------------------------------------------------------------------------------
float2 sTexSize = float2(800,600);
float2 sScale = float2(0.5,0.5);
float2 sCenter = float2(0.5,0.5);

//------------------------------------------------------------------------------------------
// These parameters are set by MTA whenever a shader is drawn
//------------------------------------------------------------------------------------------
float4x4 gWorld : WORLD;
float4x4 gView : VIEW;
float4x4 gProjection : PROJECTION;
float gTime : TIME;
#define PI 3.1415926535897

//------------------------------------------------------------------------------------------
// Structure of data sent to the vertex shader
//------------------------------------------------------------------------------------------//

struct VSInput
{
    float3 Position : POSITION0;
    float4 Diffuse : COLOR0;
    float2 TexCoord : TEXCOORD0;
};

//------------------------------------------------------------------------------------------
// Structure of data sent to the pixel shader ( from the vertex shader )
//------------------------------------------------------------------------------------------
struct PSInput
{
    float4 Position : POSITION0;
    float4 Diffuse : COLOR0;
    float2 TexCoord: TEXCOORD0;
};

struct VS_IN
{
    float4 position : POSITION;
    float2 texcoord : TEXCOORD;
};

struct VS_OUT
{
    float4 position : SV_POSITION;
    float2 texcoord : TEXCOORD0;
};

struct PS_OUT
{
    float4 color : COLOR0;
    float depth : DEPTH0;
};

VS_OUT VS(VS_IN input)
{
    VS_OUT output = (VS_OUT)0;

    output.position = input.position;
    output.texcoord = input.texcoord;

    return output;
}

PS_OUT PS(VS_OUT input)
{
    PS_OUT output = (PS_OUT)0;

    // Now that output.depth is defined with SV_Depth, and you have depth-write enabled,
    // this should write to the depth buffer.
    output.depth = 0;

    return output;
}

//-----------------------------------------------------------------------------
//-- Get value from the depth buffer
//-- Uses define set at compile time to handle RAWZ special case (which will use up a few more slots)
//-----------------------------------------------------------------------------
float FetchDepthBufferValue( float2 uv )
{
    float4 texel = tex2D(SamplerDepth, uv);
#if IS_DEPTHBUFFER_RAWZ
    float3 rawval = floor(255.0 * texel.arg + 0.5);
    float3 valueScaler = float3(0.996093809371817670572857294849, 0.0038909914428586627756752238080039, 1.5199185323666651467481343000015e-5);
    return dot(rawval, valueScaler / 255.0);
#else
    return texel.r;
#endif
}

//-----------------------------------------------------------------------------
//-- Use the last scene projecion matrix to linearize the depth value a bit more
//-----------------------------------------------------------------------------
float Linearize(float posZ)
{
    return gProjectionMainScene[3][2] / (posZ - gProjectionMainScene[2][2]);
}



PSInput VertexShaderFunction(VSInput VS)
{
    PSInput PS = (PSInput)0;

    // Calculate screen pos of vertex
    float4 posWorld = mul(float4(VS.Position.xyz,1), gWorld);
    float4 posWorldView = mul(posWorld, gView);
    PS.Position = mul(posWorldView, gProjection);
	
    // Pass through color and tex coord
    PS.Diffuse = VS.Diffuse;

    // Translate TexCoord
    float2 position = float2(VS.TexCoord.x,1 - VS.TexCoord.y);
    float2 center = 5 + (sCenter + 0.6);
    position += float2(1 - center.x, center.y) - 0.5;
    position = (position - 0.5) * float2(sTexSize.x/sTexSize.y,1) / sScale + 0.5;
    position -= 0.5;
    PS.TexCoord = position;

    return PS;
}

//------------------------------------------------------------------------------------------
// PixelShaderFunction
//------------------------------------------------------------------------------------------
float hash( float n ) { return frac(sin(n)*753.5453123); }

// Slight modification of iq's noise function.
float noise( float2 x )
{
    float2 p = floor(x);
    float2 f = frac(x);
    f = f*f*(5.0-2.0*f);
    
    float n = p.x + p.y*157.0;
    return lerp(
                    lerp( hash(n+  0.0), hash(n+  1.0),f.x),
                    lerp( hash(n+157.0), hash(n+158.0),f.x),
            f.y);
}

float fbm(float2 p, float3 a)
{
     float v = 0.0;
     v += noise(p*a.x)*.5;
     v += noise(p*a.y)*.25;
     v += noise(p*a.z)*.125;
     return v;
}

float3 drawLines( float2 uv, float3 fbmOffset, float3 color1, float3 color2, float timer )
{
    float timeVal = timer * 0.1;
    float3 finalColor = float3( 0.0,0.0,0.0 );
    for( int i=0; i < 3; ++i )
    {
        float indexAsFloat = float(i);
        float amp = 40.0 + (indexAsFloat*5.0);
        float period = 2.0 + (indexAsFloat+2.0);
        float thickness = lerp( 0.9, 1.0, noise(uv*10.0) );
        float t = abs( 0.9 / (sin(uv.x + fbm( uv + timeVal * period, fbmOffset )) * amp) * thickness );
        
        finalColor +=  t * color1;
    }
    
    for( int j=0; j < 5; ++j )
    {
        float indexAsFloat = float(j);
        float amp = 40.0 + (indexAsFloat*7.0);
        float period = 2.0 + (indexAsFloat+8.0);
        float thickness = lerp( 0.7, 1.0, noise(uv*10.0) );
        float t = abs( 0.8 / (sin(uv.x + fbm( uv + timeVal * period, fbmOffset )) * amp) * thickness );
        
        finalColor +=  t * color2 * 0.6;
    }
    
    return finalColor;
}

float4 PixelShaderFunction(PSInput PS) : COLOR0
{
    // Get TexCoord
    float2 position = PS.TexCoord;
    float timer = gTime;
	
	// correction
    position.xy = position.yx;
   
    float3 lineColor1 = float3( 2.3, 0.5, .5 );
    float3 lineColor2 = float3( 0.3, 0.5, 2.5 );

    // main effect	
    float3 finalColor = float3(0,0,0);
	
    float t = sin( timer ) * 0.5 + 0.5;
    float pulse = lerp( 0.10, 0.20, t);
    
    finalColor += drawLines( position, float3( 1.0, 20.0, 30.0), lineColor1, lineColor2, timer ) * pulse;
    finalColor += drawLines( position, float3( 1.0, 2.0, 4.0), lineColor1, lineColor2, timer );
	
    return float4(finalColor.rgb,1);

}
//------------------------------------------------------------------------------------------
// Techniques
//------------------------------------------------------------------------------------------
technique energyField
{
    pass P0
    {
		zEnable = false;
        VertexShader = compile vs_3_0 VS();
        PixelShader  = compile ps_3_0 PS();
    }
    pass P1
    {
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader  = compile ps_3_0 PixelShaderFunction();
    }
}

// Fallback
technique fallback
{
    pass P0
    {
        // Just draw normally
    }
}

I don't understand why it is not modifying depth

Link to comment
33 minutes ago, Krokara said:

I don't understand why it is not modifying depth

You are rendering two passes, one without depth and one with depth. By disabling depth in the first pass you should be drawing nothing at the position of your sphere. Try merging both of the passes like this:

...

PS_OUT PixelShaderFunction(PSInput PS)
{
    PS_OUT psout;
    
    // Get TexCoord
    float2 position = PS.TexCoord;
    float timer = gTime;
	
	// correction
    position.xy = position.yx;
   
    float3 lineColor1 = float3( 2.3, 0.5, .5 );
    float3 lineColor2 = float3( 0.3, 0.5, 2.5 );

    // main effect	
    float3 finalColor = float3(0,0,0);
	
    float t = sin( timer ) * 0.5 + 0.5;
    float pulse = lerp( 0.10, 0.20, t);
    
    finalColor += drawLines( position, float3( 1.0, 20.0, 30.0), lineColor1, lineColor2, timer ) * pulse;
    finalColor += drawLines( position, float3( 1.0, 2.0, 4.0), lineColor1, lineColor2, timer );
	
    psout.color = float4(finalColor.rgb,1);
    psout.depth = 0;
    
    return psout;
}
//------------------------------------------------------------------------------------------
// Techniques
//------------------------------------------------------------------------------------------
technique energyField
{
    pass P0
    {
        ZEnable = false;
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader  = compile ps_3_0 PixelShaderFunction();
    }
}

...

 

  • Thanks 1
Link to comment
Just now, The_GTA said:

You are rendering two passes, one without depth and one with depth. By disabling depth in the first pass you should be drawing nothing at the position of your sphere. Try merging both of the passes like this:

...

PS_OUT PixelShaderFunction(PSInput PS)
{
    PS_OUT psout;
    
    // Get TexCoord
    float2 position = PS.TexCoord;
    float timer = gTime;
	
	// correction
    position.xy = position.yx;
   
    float3 lineColor1 = float3( 2.3, 0.5, .5 );
    float3 lineColor2 = float3( 0.3, 0.5, 2.5 );

    // main effect	
    float3 finalColor = float3(0,0,0);
	
    float t = sin( timer ) * 0.5 + 0.5;
    float pulse = lerp( 0.10, 0.20, t);
    
    finalColor += drawLines( position, float3( 1.0, 20.0, 30.0), lineColor1, lineColor2, timer ) * pulse;
    finalColor += drawLines( position, float3( 1.0, 2.0, 4.0), lineColor1, lineColor2, timer );
	
    psout.color = float4(finalColor.rgb,1);
    psout.depth = 0;
    
    return psout;
}
//------------------------------------------------------------------------------------------
// Techniques
//------------------------------------------------------------------------------------------
technique energyField
{
    pass P0
    {
        ZEnable = false;
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader  = compile ps_3_0 PixelShaderFunction();
    }
}

...

 

object keeps disappearing

Link to comment
7 minutes ago, Krokara said:

object keeps disappearing

Hmm. Maybe the combination of render-states is wrong after all. How about this?

technique energyField
{
    pass P0
    {
        ZEnable = true;
        ZWriteEnable = true;
        ZFunc = NEVER;
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader  = compile ps_3_0 PixelShaderFunction();
    }
}

 

  • Thanks 1
Link to comment
Just now, The_GTA said:

Hmm. Maybe the combination of render-states is wrong after all. How about this?

technique energyField
{
    pass P0
    {
        ZEnable = true;
        ZWriteEnable = true;
        ZFunc = NEVER;
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader  = compile ps_3_0 PixelShaderFunction();
    }
}

 

It looks like ZEnable false only works with image and ZEnable true for objects, technique 
 

technique energyField
{
    pass P0
    {
        ZEnable = true;
        //ZWriteEnable = true;
        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader  = compile ps_3_0 PixelShaderFunction();
    }

}

Result
spacer.png
It's working a lot, thank you The_GTA

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...