Volume Ray Marching rendered always on top of the other objects - c#

I have a really big problem that has been bothering me for so long and I can't seem to find the solution. I have downloaded this project https://github.com/brianasu/unity-ray-marching/tree/volumetric-textures (Unitypackage with my project here https://dl.dropboxusercontent.com/u/27758186/ApplicationVolume.unitypackage ) , that is about volume rendering.
The problem is that, as you can see, the volume renders ALWAYS in front of everything else (try placing a cube in front of the cube with the volume). I have tried a lot of things but none seems to work.
I think it might be an issue of the shaders used. In the main Camera, a RayMarching script is attached, which contains an OnRenderImage method, which creates a new Camera (although disabled), and renders the volume.
I don't know if it is a shader issue, then, or more like a camera issue (is rendering with replaced shaders). I attach my current project for testing so you don't have to download from git, and I just want the volume (head), to appear behind the rectangles (not seen) when it's actually behind, and in front when it is, pretty much like an standard geometry, but it appears always on top...
Any help or suggestion would be GREATLY appreciated, I'm kind of desperate as anything works and I'm pretty sure it is a fairly easy issue.
The code of the shader of the Ray Marching is as follows. Should it do some kind of ZTesting to not show the fragments that are covered by any other object?
Shader "Hidden/Ray Marching/Ray Marching"
{
CGINCLUDE
#include "UnityCG.cginc"
#pragma target 3.0
#pragma profileoption MaxLocalParams=1024
#pragma profileoption NumInstructionSlots=4096
#pragma profileoption NumMathInstructionSlots=4096
struct v2f {
float4 pos : POSITION;
float2 uv[2] : TEXCOORD0;
};
sampler3D _VolumeTex;
float4 _VolumeTex_TexelSize;
sampler2D _FrontTex;
sampler2D _BackTex;
float4 _LightDir;
float4 _LightPos;
float _Dimensions;
float _Opacity;
float4 _ClipDims;
float4 _ClipPlane;
v2f vert( appdata_img v )
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv[0] = v.texcoord.xy;
o.uv[1] = v.texcoord.xy;
#if SHADER_API_D3D9
if (_MainTex_TexelSize.y < 0)
o.uv[0].y = 1-o.uv[0].y;
#endif
return o;
}
#define TOTAL_STEPS 128.0
#define STEP_CNT 128
#define STEP_SIZE 1 / 128.0
half4 raymarch(v2f i, float offset)
{
float3 frontPos = tex2D(_FrontTex, i.uv[1]).xyz;
float3 backPos = tex2D(_BackTex, i.uv[1]).xyz;
float3 dir = backPos - frontPos;
float3 pos = frontPos;
float4 dst = 0;
float3 stepDist = dir * STEP_SIZE;
for(int k = 0; k < STEP_CNT; k++)
{
float4 src = tex3D(_VolumeTex, pos);
// clipping
float border = step(1 - _ClipDims.x, pos.x);
border *= step(pos.y, _ClipDims.y);
border *= step(pos.z, _ClipDims.z);
border *= step(0, dot(_ClipPlane, float4(pos - 0.5, 1)) + _ClipPlane.w);
// Standard blending
src.a *= saturate(_Opacity * border);
src.rgb *= src.a;
dst = (1.0f - dst.a) * src + dst;
pos += stepDist;
}
return 3.0F*dst;// + dst;
}
ENDCG
Subshader {
Fog { Mode off }
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
half4 frag(v2f i) : COLOR {
return raymarch(i, 0);
}
ENDCG
}
}
Fallback off
} // shader

Related

How can I improve/change the shader to be like the image in my message and not as it is in my scene?

This is what I want to get as blur background image :
And this is what I'm getting now and it's not the same blur effect :
It looks like my blue background image is more strecth and blur and not like the image in my first screenshot :
This is my shader code :
Shader "Tutorial/023_Postprocessing_Blur"{
//show values to edit in inspector
Properties{
[HideInInspector] _MainTex("Texture", 2D) = "white" {}
_BlurSize("Blur Size", Range(0,0.5)) = 0
[KeywordEnum(Low, Medium, High)] _Samples("Sample amount", Float) = 0
[Toggle(GAUSS)] _Gauss("Gaussian Blur", float) = 0
[PowerSlider(3)]_StandardDeviation("Standard Deviation (Gauss only)", Range(0.00, 0.3)) = 0.02
}
SubShader{
// markers that specify that we don't need culling
// or reading/writing to the depth buffer
Cull Off
ZWrite Off
ZTest Always
//Vertical Blur
Pass{
CGPROGRAM
//include useful shader functions
#include "UnityCG.cginc"
//define vertex and fragment shader
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile _SAMPLES_LOW _SAMPLES_MEDIUM _SAMPLES_HIGH
#pragma shader_feature GAUSS
//texture and transforms of the texture
sampler2D _MainTex;
float _BlurSize;
float _StandardDeviation;
#define PI 3.14159265359
#define E 2.71828182846
#if _SAMPLES_LOW
#define SAMPLES 10
#elif _SAMPLES_MEDIUM
#define SAMPLES 30
#else
#define SAMPLES 100
#endif
//the object data that's put into the vertex shader
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
//the data that's used to generate fragments and can be read by the fragment shader
struct v2f {
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
};
//the vertex shader
v2f vert(appdata v) {
v2f o;
//convert the vertex positions from object space to clip space so they can be rendered
o.position = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
//the fragment shader
fixed4 frag(v2f i) : SV_TARGET{
#if GAUSS
//failsafe so we can use turn off the blur by setting the deviation to 0
if (_StandardDeviation == 0)
return tex2D(_MainTex, i.uv);
#endif
//init color variable
float4 col = 0;
#if GAUSS
float sum = 0;
#else
float sum = SAMPLES;
#endif
//iterate over blur samples
for (float index = 0; index < SAMPLES; index++) {
//get the offset of the sample
float offset = (index / (SAMPLES - 1) - 0.5) * _BlurSize;
//get uv coordinate of sample
float2 uv = i.uv + float2(0, offset);
#if !GAUSS
//simply add the color if we don't have a gaussian blur (box)
col += tex2D(_MainTex, uv);
#else
//calculate the result of the gaussian function
float stDevSquared = _StandardDeviation * _StandardDeviation;
float gauss = (1 / sqrt(2 * PI * stDevSquared)) * pow(E, -((offset * offset) / (2 * stDevSquared)));
//add result to sum
sum += gauss;
//multiply color with influence from gaussian function and add it to sum color
col += tex2D(_MainTex, uv) * gauss;
#endif
}
//divide the sum of values by the amount of samples
col = col / sum;
return col;
}
ENDCG
}
//Horizontal Blur
Pass{
CGPROGRAM
//include useful shader functions
#include "UnityCG.cginc"
#pragma multi_compile _SAMPLES_LOW _SAMPLES_MEDIUM _SAMPLES_HIGH
#pragma shader_feature GAUSS
//define vertex and fragment shader
#pragma vertex vert
#pragma fragment frag
//texture and transforms of the texture
sampler2D _MainTex;
float _BlurSize;
float _StandardDeviation;
#define PI 3.14159265359
#define E 2.71828182846
#if _SAMPLES_LOW
#define SAMPLES 10
#elif _SAMPLES_MEDIUM
#define SAMPLES 30
#else
#define SAMPLES 100
#endif
//the object data that's put into the vertex shader
struct appdata {
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
//the data that's used to generate fragments and can be read by the fragment shader
struct v2f {
float4 position : SV_POSITION;
float2 uv : TEXCOORD0;
};
//the vertex shader
v2f vert(appdata v) {
v2f o;
//convert the vertex positions from object space to clip space so they can be rendered
o.position = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
//the fragment shader
fixed4 frag(v2f i) : SV_TARGET{
#if GAUSS
//failsafe so we can use turn off the blur by setting the deviation to 0
if (_StandardDeviation == 0)
return tex2D(_MainTex, i.uv);
#endif
//calculate aspect ratio
float invAspect = _ScreenParams.y / _ScreenParams.x;
//init color variable
float4 col = 0;
#if GAUSS
float sum = 0;
#else
float sum = SAMPLES;
#endif
//iterate over blur samples
for (float index = 0; index < SAMPLES; index++) {
//get the offset of the sample
float offset = (index / (SAMPLES - 1) - 0.5) * _BlurSize * invAspect;
//get uv coordinate of sample
float2 uv = i.uv + float2(offset, 0);
#if !GAUSS
//simply add the color if we don't have a gaussian blur (box)
col += tex2D(_MainTex, uv);
#else
//calculate the result of the gaussian function
float stDevSquared = _StandardDeviation * _StandardDeviation;
float gauss = (1 / sqrt(2 * PI * stDevSquared)) * pow(E, -((offset * offset) / (2 * stDevSquared)));
//add result to sum
sum += gauss;
//multiply color with influence from gaussian function and add it to sum color
col += tex2D(_MainTex, uv) * gauss;
#endif
}
//divide the sum of values by the amount of samples
col = col / sum;
return col;
}
ENDCG
}
}
}
This is the link with the blur image I'm trying to get but he didn't provided the shader code he is using: So I thought maybe there is something I can change/add to my shader code to get the same result as in the link :
https://sharpcoderblog.com/blog/unity-3d-create-main-menu-with-ui-canvas

Where does unity discard vertices before shader call?

I'm trying to make a game with periodic boundary conditions (so basically if you are on the right edge of the map, and you look right, you should see the whole map again).
I made a geometry shader which takes one triangle and outputs the 27 triangles i need (3*3*3 periodic box), and it works perfectly while the base object is on the screen. As soon as the base object leaves the screen all the copies disappear too.
So i think unity does not even call my shaders on the vertices which is behind my camera (which is totally fine for optimization), but with my current solution i need a call on every object of the game.
Is it possible to force unity not to discard any objects before rendering, or should i look for a different solution? If i have to do something else, do you have any ideas?
Here's my current code. (I'm new with shaders, so it might be stupid...)
Shader "Unlit/Geom_shader"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma geometry geom
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
};
struct v2g {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
struct g2f {
float4 vertex : SV_POSITION;
float2 uv : TEXCOORD0;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2g vert (appdata v)
{
v2g o;
o.vertex = v.vertex;
o.uv = v.uv;
return o;
}
[maxvertexcount(3*3*3*3)]
void geom(triangle v2g input[3], inout TriangleStream<g2f> tristream) {
g2f o = (g2f)0;
for (int x = 0; x < 3; x++)
{
float x_shift = (x - 1) * 2*_SinTime[2];
for (int y = 0; y < 3; y++)
{
float y_shift = (y - 1) * 2 * _SinTime[2];
for (int z = 0; z < 3; z++)
{
float z_shift = (z - 1) * 2 * _CosTime[2];
for (int i = 0; i < 3; i++)
{
o.vertex = UnityObjectToClipPos(input[i].vertex + float4(x_shift, y_shift, z_shift, 0));
o.uv = TRANSFORM_TEX(input[i].uv, _MainTex);
tristream.Append(o);
}
tristream.RestartStrip();
}
}
}
}
fixed4 frag (g2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv);
return col;
}
ENDCG
}
}
}
I'm not trying to use it for lights but for whole objects and i'm sure they are handled differently. So the reason why it is happening is the same, but the solution must be different. (Why it's not a duplicate of: Unity3D - Light deactivated when facing opposite direction )
It's not the vertices being discarded, it's the entire mesh being culled as a whole.
Unity implements View Frustum Culling based on the bounding box of your mesh. You can change the bounding box of a mesh manually by assigning a new value to Mesh.bounds.

Black hole distortion shader in Unity

I found shader code that has the effect of warping a space around a certain point. It's a cool effect, but it's missing some animation, so I've added something to it:
Shader "Marek/BlackHoleDistortion"
{
Properties {
_DistortionStrength ("Distortion Strength", Range(0, 10)) = 0
_Timer("Timer", Range(0, 10)) = 0
_HoleSize ("Hole Size", Range(0, 1)) = 0.1736101
_HoleEdgeSmoothness ("Hole Edge Smoothness", Range(1, 4)) = 4
_ObjectEdgeArtifactFix ("Object Edge Artifact Fix", Range(1, 10)) = 1
}
SubShader {
Tags {
"IgnoreProjector"="True"
"Queue"="Transparent"
"RenderType"="Transparent"
}
GrabPass{ }
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
ZWrite Off
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _GrabTexture;
uniform float _DistortionStrength;
uniform float _HoleSize;
uniform float _HoleEdgeSmoothness;
uniform float _ObjectEdgeArtifactFix;
uniform float _Timer;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD0;
float3 normalDir : TEXCOORD1;
float4 projPos : TEXCOORD2;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.pos = UnityObjectToClipPos(v.vertex);
o.projPos = ComputeScreenPos(o.pos);
COMPUTE_EYEDEPTH(o.projPos.z);
return o;
}
float4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz);
float3 normalDirection = i.normalDir;
float2 sceneUVs = (i.projPos.xy / i.projPos.w);
float node_9892 = (_HoleSize * -1.0 + 1.0);
float node_3969 = (1.0 - pow(1.0 - max(0, dot(normalDirection, viewDirection)), clamp(_DistortionStrength - _Timer, 0, _DistortionStrength)));
float node_9136 = (length(float2(ddx(node_3969), ddy(node_3969))) * _HoleEdgeSmoothness);
float node_4918 = pow(node_3969, 6.0);
float node_1920 = (1.0 - smoothstep((node_9892 - node_9136), (node_9892 + node_9136), node_4918));
float3 finalColor = (
lerp(
float4(node_1920, node_1920, node_1920, node_1920),
float4(1, 1, 1, 1),
pow(
pow(1.0 - max(0, dot(normalDirection, viewDirection)), 1.0),
_ObjectEdgeArtifactFix
)
) * tex2D(_GrabTexture, ((node_4918 * (sceneUVs.rg * _Time * -2.0 + 1.0)) + sceneUVs.rg)).rgb).rgb;
return fixed4(finalColor, 1);
}
ENDCG
}
}
FallBack "Diffuse"
}
Now, the problem is that in order to make the distortion disappear after certain time, I need to include some variable into the equation - here I'm calling it _Timer. I'm not using the _Time built in because of obvious reasons - it's an ever growing value and I need something that starts from 0 each time the object using this shader is made active. C# code handling passing that parameter looks as follows:
public void Update() {
_timeElapsed += Time.deltaTime;
_renderer.material.SetFloat("_Timer", _timeElapsed);
}
The question is - can I do it better? I would like this shader's code to be more of a self-contained thing - without the need to pass parameters from cs script to it.
Can I do it better?
In-short, yes and no. If you want the shader to behave differently per material you simply cannot avoid passing a property from C#. You can however avoid doing this in Update by passing a start time and computing the elapse time in the shader.
C#
void OnEnable ()
{
_renderer.material.SetFloat("_StartTime", Time.timeSinceLevelLoad);
}
Shader
uniform float _StartTime;
float4 frag(VertexOutput i) : COLOR
{
float elapse = _Time.y - _StartTime;
}
Now, although this will tie directly into the setup you are currently using, it should be noted that accessing the .material property will clone the material (which can break batching, among other things).
This can be avoided with the more recent introduction of MaterialPropertyBlocks.
Unity provides a handful of built-in values for your shaders: things like current object’s transformation matrices, time etc.
You just use them in ShaderLab like you’d use any other property, the only difference is that you don’t have to declare it somewhere - they are “built in”.
https://docs.unity3d.com/455/Documentation/Manual/SL-BuiltinValues.html
there is a clever way of giving you 4 variations of the value, potentially saving you a multiply operation by re-using the pre-multiplied value for every pixel being rendered. There are 4 values available.
_Time.x = time / 20
_Time.y = time
_Time.z = time * 2
_Time.w = time * 3
this is a simple example that show you how it works:
Shader "Example/Circle"
{
Properties
{
}
SubShader
{
Cull Off
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float circle(in float2 _st, in float _radius){
float2 dist = distance(_st,float2(0.5,0.5));
float result = step(dist,_radius);
return result;
}
fixed4 frag (v2f i) : SV_Target
{
float WaveTime = sin(_Time.z);
float3 color = float3(1,1,1)*circle(i.uv,WaveTime);
return float4( color, 1.0 );
}
ENDCG
}
}
}
In comments you mentioned that you want reset time value when you enable it , so here you need to Initialize Time value with script.
so you should use your own Time In shader:
Properties
{
_Timer("Timer",Float) = 0
}
float WaveTime = sin(_Timer);
using System.Collections;
using UnityEngine;
public class Circle : MonoBehaviour {
public float _timeElapsed;
void OnEnable(){
_timeElapsed = 0;
}
public void Update() {
_timeElapsed += Time.deltaTime;
var _renderer = GetComponent<MeshRenderer>();
_renderer.material.SetFloat("_Timer", _timeElapsed);
}
}

HLSL/XNA Ambient light texture mixed up with multi pass lighting

I've been having some troubles lately with lighting. I have found a source on google which is working pretty good on the example. However, when I try to implement it to my current project, I am getting some very weird bugs. The main one is that my textures are "mixed up" when I only activate the ambient light, which means that a model gets the texture of another one .
I am using the same effect for every meshes of my models. I guess this could be the problem, but I don't really know how to "reset" an effect for a new model. Is it possible?
Here is my shader:
float4x4 WVP;
float4x4 WVP;
float3x3 World;
float3 Ke;
float3 Ka;
float3 Kd;
float3 Ks;
float specularPower;
float3 globalAmbient;
float3 lightColor;
float3 eyePosition;
float3 lightDirection;
float3 lightPosition;
float spotPower;
texture2D Texture;
sampler2D texSampler = sampler_state
{
Texture = <Texture>;
MinFilter = anisotropic;
MagFilter = anisotropic;
MipFilter = linear;
MaxAnisotropy = 16;
};
struct VertexShaderInput
{
float4 Position : POSITION0;
float2 Texture : TEXCOORD0;
float3 Normal : NORMAL0;
};
struct VertexShaderOutput
{
float4 Position : POSITION0;
float2 Texture : TEXCOORD0;
float3 PositionO: TEXCOORD1;
float3 Normal : NORMAL0;
};
VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
VertexShaderOutput output;
output.Position = mul(input.Position, WVP);
output.Normal = input.Normal;
output.PositionO = input.Position.xyz;
output.Texture = input.Texture;
return output;
}
float4 PSAmbient(VertexShaderOutput input) : COLOR0
{
return float4(Ka*globalAmbient + Ke,1) * tex2D(texSampler,input.Texture);
}
float4 PSDirectionalLight(VertexShaderOutput input) : COLOR0
{
//Difuze
float3 L = normalize(-lightDirection);
float diffuseLight = max(dot(input.Normal,L), 0);
float3 diffuse = Kd*lightColor*diffuseLight;
//Specular
float3 V = normalize(eyePosition - input.PositionO);
float3 H = normalize(L + V);
float specularLight = pow(max(dot(input.Normal,H),0),specularPower);
if(diffuseLight<=0) specularLight=0;
float3 specular = Ks * lightColor * specularLight;
//sum all light components
float3 light = diffuse + specular;
return float4(light,1) * tex2D(texSampler,input.Texture);
}
technique MultiPassLight
{
pass Ambient
{
VertexShader = compile vs_3_0 VertexShaderFunction();
PixelShader = compile ps_3_0 PSAmbient();
}
pass Directional
{
PixelShader = compile ps_3_0 PSDirectionalLight();
}
}
And here is how I actually apply my effects:
public void ApplyLights(ModelMesh mesh, Matrix world,
Texture2D modelTexture, Camera camera, Effect effect,
GraphicsDevice graphicsDevice)
{
graphicsDevice.BlendState = BlendState.Opaque;
effect.CurrentTechnique.Passes["Ambient"].Apply();
foreach (ModelMeshPart part in mesh.MeshParts)
{
graphicsDevice.SetVertexBuffer(part.VertexBuffer);
graphicsDevice.Indices = part.IndexBuffer;
// Texturing
graphicsDevice.BlendState = BlendState.AlphaBlend;
if (modelTexture != null)
{
effect.Parameters["Texture"].SetValue(
modelTexture
);
}
graphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
part.VertexOffset,
0,
part.NumVertices,
part.StartIndex,
part.PrimitiveCount
);
// Applying our shader to all the mesh parts
effect.Parameters["WVP"].SetValue(
world *
camera.View *
camera.Projection
);
effect.Parameters["World"].SetValue(world);
effect.Parameters["eyePosition"].SetValue(
camera.Position
);
graphicsDevice.BlendState = BlendState.Additive;
// Drawing lights
foreach (DirectionalLight light in DirectionalLights)
{
effect.Parameters["lightColor"].SetValue(light.Color.ToVector3());
effect.Parameters["lightDirection"].SetValue(light.Direction);
// Applying changes and drawing them
effect.CurrentTechnique.Passes["Directional"].Apply();
graphicsDevice.DrawIndexedPrimitives(
PrimitiveType.TriangleList,
part.VertexOffset,
0,
part.NumVertices,
part.StartIndex,
part.PrimitiveCount
);
}
}
I am also applying this when loading the effect:
effect.Parameters["lightColor"].SetValue(Color.White.ToVector3());
effect.Parameters["globalAmbient"].SetValue(Color.White.ToVector3());
effect.Parameters["Ke"].SetValue(0.0f);
effect.Parameters["Ka"].SetValue(0.01f);
effect.Parameters["Kd"].SetValue(1.0f);
effect.Parameters["Ks"].SetValue(0.3f);
effect.Parameters["specularPower"].SetValue(100);
Thank you very much
UPDATE:
I tried to load an effect for each model when drawing, but it doesn't seem to have changed anything. I suppose it is because XNA detects that the effect has already been loaded before and doesn't want to load a new one. Any idea why?
Alright so the problem was pretty stupid actually. I was applying the same effect to every meshes. I simply added a Effect.Clone() when loading the model and it worked!

Black Screen due to shaders ps_3

I'm currently using shaders in my game, it's working fine with a nVidia GeForceGT330m but with an ATI 4670 (which supports ps_4.1) I encounter a black screen.
Here is the source of the HLSL effect:
struct Explo
{
float3 position;
float4 color;
float power;
int time;
};
float2 DisplacementScroll;
texture colortexture;
int nb;
Explo explos[5];
float ambient;
float4 ambientColor;
float screenWidth;
float screenHeight;
sampler ColorMap = sampler_state
{
Texture = <colortexture>;
};
float4 CalculateLight(Explo ex, float4 base, float3 pixelPosition)
{
float3 direction = ex.position - pixelPosition;
float distance = 1 / length(ex.position - pixelPosition) * ex.power;
float amount = max(dot(base, normalize(distance)), 0);
return base * distance * amount * ex.color * ambient;
}
float4 Explosion(float2 texCoords : TEXCOORD0) : COLOR
{
//texCoords = tex2D(NormalMap, DisplacementScroll + texCoords / 3)*0.2 - 0.15;
float4 base = tex2D(ColorMap, texCoords);
float3 pixelPosition = float3(screenWidth * (texCoords.x),
screenHeight * (texCoords.y),0);
float4 finalColor = (base * ambientColor * ambient);
for (int i=0; i<nb; i++)
{
finalColor += CalculateLight(explos[i], base, pixelPosition);
}
return finalColor;
}
technique KaBoom
{
pass Pass1
{
PixelShader = compile ps_3_0 Explosion();
}
}
I remember once I had a similar problem. A shader just didn't work on ATI. The problem was that the vertex and pixel shaders were compiled to different shader models (vs_3_0 and ps_2_0). It worked for NVIDIA, but not for ATI. In your case you're only binding a pixel shader for the pass and who knows what the last vertex shader was.
Granted, this is relevant only if you're dead sure the problem is with the shader and not something else, e.g. your DIPs.
Good luck

Categories