tex2D(s,t,ddx,ddy) not working in fullscreen in unity - unity3d

i'm trying to make full screen custom post effect.
with script added to main cam, with one line: blit(src,dst,mat) in void OnRenderImage(src,dst). I wrote the shader with _MainTex so it'll work in "Blit". I done it(many times) all correctlly like in tuts. if i assign this shader to any object (with texture) it works well, but the problem is when i assign that shader(in material) to script (blit(,,mat)) it ignores the ddx,ddy part,it works like tex2D(tex,uv). WHY? how can I fix it?
here is the fragment code.
fixed3 frag(vout v):sv_target
{
fixed3 c;
float2 cddx=float2(
pow(abs(0.5-v.uv.x),1.6)*_xy,
-pow(abs(0.5-v.uv.y),1.6)*_xy);
float2 cddy=float2(
-pow(abs(0.5-v.uv.x),1.6)*_xy,
pow(abs(0.5-v.uv.y),1.6)*_xy);
c=tex2D(_MainTex,v.uv,cddx,cddy);
return c*0.5;
}

I found an answer. tex2D(sampler2D,uv,ddx,ddy) and tex2Dgrad(...) methods use mipmaps. when i tried to pass "screen" texture to the shader in blit(src,dst,mat) I noticed that src has no mipmaps, only the original texture. so, I thought that I'll simply add mipmaps with src.GenerateMips() and it'll work. but there is a catch: if i try to do that the editor throws me exceptions that the rendertexture is created, that generating mips is unsuported in created rendertexture and other stuff... finally i found the recipy:
one time
create another temp renderTexture
turn off auto generate mipmaps
useMips=true
every frame (OnRenderImage(...))
blit src to temp
generate mipmaps in temp
blit(temp,dst,mat)
it generates mipmaps on new render texture and passes it to the shader without any errors.
one last thing, if someone knows how to specify mipmap count that i want to generate, tell me please.
I hope it'll be helpful to newbies in shaders like me.

Related

Unity scaling an object when duplicating (not wanted)

I'm making a mining game, where you have a rest area, and a mine area (all of this stuff works well) but when I "hire a new worker" the main miner clones, and is put in the starting area. When I clone the main miner, the cloned miner's size goes to 6k something something.
public void moreMiners(){
if (gm.Diamonds >= gm.mmcost){
gm.Diamonds -= gm.mmcost;
GameObject newplr = GameObject.Instantiate(plr);
newplr.gameObject.transform.localPosition = plr.transform.position;
}
}
duplicating code ^
help is appreciated
Make sure that "plr" reference is the original Prefab from your assets folder and not from your scene, then what you can do is to check your original prefab inside your assets folder and see if the scale is the one that you want to have.
Also when you Instantiate an object you can parse the position too as a parameter like this:
GameObject.Instantiate(plr,position);

Unity A* graph won't scale with canvas

I am using the A* graph package that I found here.
https://arongranberg.com/astar/download
it all works well in scene view and I was able to set the graph to treat walls like obstacles.
However once I start the game the canvas scales and the graph's nodes no longer align with the walls.
this is really messing up my path finding. If anyone has any ideas how to fix this that would be much appreciated. I tried parenting the graph to the canvas but it still doesn't scale.
Kind regards
For anyone struggling with this what we did is edited the script of the A* code, in the update function we just got it to rescan once. This means that once the game started and all the scaling had taken place the graph re adjusted its bounds. This is probably not the most proper way but it only took four lines and worked for us.
private bool scanAgain = true;
private void Update () {
// This class uses the [ExecuteInEditMode] attribute
// So Update is called even when not playing
// Don't do anything when not in play mode
if (!Application.isPlaying) return;
navmeshUpdates.Update();
// Execute blocking actions such as graph updates
// when not scanning
if (!isScanning) {
PerformBlockingActions();
}
// Calculates paths when not using multithreading
pathProcessor.TickNonMultithreaded();
// Return calculated paths
pathReturnQueue.ReturnPaths(true);
if (scanAgain)
{
Scan();
scanAgain = false;
}

Material doesn't have a texture property '_RendererColor'

I want to scroll ball texture to show its moving so I have written this kind of code:
// ball texture rolling
textureOffset.x -= myRigidBody.velocity.normalized.z * (speed / 500f);
textureOffset.y = myRigidBody.velocity.normalized.x * (speed / 40);
myMaterial.mainTextureOffset = Vector2.Lerp (myMaterial.GetTextureOffset (1), textureOffset, speed * Time.fixedDeltaTime);
I was getting this kind of error during game play in Unity Editor.
This kind of material assigned to ball object:
I have just upgraded Unity version 2017.3.1p4 and error started coming, I don't know what to do now. Give me suggestions to solve this.
There is a couple of reasons for this bug, so here is a few suggestions :
1-make sure the shader you are using has the texture "_RendererColor", look for something like this
Properties {
_RendererColor ("Base (RGB)", 2D) = "white" {}
}
maybe they might have changed it's name in the update
2-make sure your shader has the appropriate include file like for example
include "UnityCG.cginc"
maybe some functionality requires certain includes after the update
3-this could just be a random bug after unity crashes or after a update, I have had some friends have their unity crash for them then they get bugs like this, maybe updating unity might do something similar, maybe try to remake the shader/create a new one and replace this one.
This is all I can think of

Sharpdx Direct3D10 How to edit pixels in texture?

I need to change certain pixels in a texture (Texture2D) using Sharpdx with dx10
Right now I got
Texture.Map(0, MapMode.Write, MapFlags.None, out stream);
but I get a "The parameter is incorrect." exception.
All solutions I find are for either dx9 or dx11 or something other then Sharpdx and the used methods don't exist in Sharpdx.Direct3D10, hopefully someone here knows how to do this simple task of editing a texture...

Adding videos to Unity3d

We are developing a game about driving awareness.
The problem is we need to show videos to the user if he makes any mistakes after completing driving. For example, if he makes two mistakes we need to show two videos at the end of the game.
Can you help with this. I don't have any idea.
#solus already gave you an answer, regarding "how to play a (pre-registered) video from your application". However, from what I've understood, you are asking about saving (and visualize) a kind of replay for the "wrong" actions, performed by the player. This is not an easy task, and I don't think that you can receive an exaustive answer, but only some advices. I will try to give you my own ones.
First of all, you should "capture" the position of the player's car, in various time periods.
As an example, you could read player's car position every 0.2 seconds, and save it into a structure (example: a List).
Then, you would implement some logic to detect the "wrong" actions (crashes, speeding...They obviously depend on your game) and save a reference to the pair ["mistake", "relevant portion of the list containg car's positions for that event"].
Now, you have all what you need to recreate a replay of the action: that is, making the car "driving alone", by reading the previously saved positions (that will act as waypoints for generating the route).
Obviously, you also have to deal with the camera's position and rotation: just leave it attached to the car (as the normal "in-game" action), or modify it during time to catch the more interesting angulations, as the AAA racing games do (this will make the overall task more difficult, of course).
Unity will import a video as a MovieTexture. It will be converted to the native Theora/Vorbis (Ogg) format. (Use ffmpeg2theora if import fails.)
Simply apply it as you would any texture. You could use a plane or a flat cube. You should adjust its localScale to the aspect ratio of your video (movie.width/(float)movie.height).
Put the attached audioclip in an AudioSource. Then call movie.Play() and audio.Play().
You could also load the video from a local file path or the web (in the correct format).
var movie = new WWW(#"file://C:\videos\myvideo.ogv").movie;
...
if(movie.isReadyToPlay)
{
renderer.material.mainTexture = movie;
audio.clip = movie.audioClip;
movie.Play();
audio.clip.Play();
}
Use MovieTexture, but do not forget to install QuickTime, you need it to import movie clip (.mov file for example).