Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - Magicorange

Pages: [1]
1
Support / Animated shader
« on: April 19, 2014, 06:09:57 pm »
Hello,

I'm trying to animate a texture on my objects using a time variable, the problem is that it doesn't move.
This is where I update the time variable (in the onDrawFrame method ) :
Code: [Select]

...
if (System.currentTimeMillis() - time >= 1000) {
Logger.log(fps + "fps");
fps = 0;
time = System.currentTimeMillis();
currentTime = System.nanoTime() - startTime;//startTime is initialized in the class' constructor with : startTime = System.nanoTime();
animateTextures();
}
fps++;
...


Here we set the uniforms in the method that creates the object :
Code: [Select]

...
String uniformTime = mContext.getResources().getString(R.string.global_uniform_time);

ArrayList<ShaderUniform> uniformsTop = new ArrayList<ShaderUniform>();
uniformsTop.add(new ShaderUniform("colorMap", 1));
uniformsTop.add(new ShaderUniform("noiseMap", 1));
uniformsTop.add(new ShaderUniform(uniformTime, currentTime));
uniformsTop.add(new ShaderUniform("alpha", 1.0));
uniformsTop.add(new ShaderUniform("baseSpeed", 0.005));
uniformsTop.add(new ShaderUniform("noiseScale", 0.1337));
uniformsTop.add(new ShaderUniform("invRadius", 0.0005f));


ShaderUtils.setShader(mContext, towerTop, shadersDir+"towers/"+"vertexShader.glsl", shadersDir+"towers/"+"fragmentShader.glsl", uniformsTop);

...


ShaderUniform is a class I've made that has a type parameter, and ShaderUtils.setShader is a method that will iterate through the arraylist and choose the right setStaticUniform method according to this type parameter.

And in the animateTextures method, this is how the shader should update the currentTime uniform:
Code: [Select]
  t.getTop().getShader().setUniform(uniformTime, currentTime); //uniformTime is the name of the variable



If needed, the fragment shader :
Code: [Select]

precision mediump float;

varying vec3 lightVec[2];
varying vec3 eyeVec;
varying vec2 texCoord;

uniform sampler2D colorMap;
uniform sampler2D noiseMap;
uniform float time;
uniform float baseSpeed;
uniform float noiseScale;
uniform float invRadius;
uniform float alpha;

uniform vec3 diffuseColors[8];
uniform vec3 specularColors[8];
uniform vec4 ambientColor;


float coef = 10.0;

void main ()
{
//Speed & direction of the texture
vec2 uvTimeShift = texCoord + vec2( -0.4, 0.5 ) * time * baseSpeed;
vec4 noise = texture2D( noiseMap, uvTimeShift );
vec2 uvNoisyTimeShift = texCoord + noiseScale * vec2( noise.r, noise.g ); //noise quantity
vec4 baseColor = texture2D( colorMap, uvNoisyTimeShift );
baseColor.a = alpha;

//...
gl_FragColor = baseColor;
}

Note that I got this working on another project that used Three.JS and WebGL, I have simply taken this code again and slightly modified it, but the time variable thingy has not changed (in the javascript version, the time uniform was updated in the renderLoop using a delta provided by a ThreeJS method).
It's meant to animate water textures using a noisemap,  I use it for a kind of lava effect.

Is it possible to update a uniform in a shader (then the way I do it is probably wrong), or does the problem comes from my time variable ?
I can't figure out yet why this is not working (and I have already searched if someone had the same problem, but it didn't help me that much), any help would be appreciated.

Thanks in advance for your answer.

2
Support / Moving the camera (on scrolling)
« on: February 27, 2014, 12:36:54 pm »
Hello,

I would like to move the camera on the x/y axis using the onScroll listener, but I'm having difficults using the camera.moveCamera method :

Code: [Select]
SimpleVector touchOrigin = Interact2D.reproject2D3DWS(map.getCam(), fb, (int)xStart, (int)yStart).normalize();
SimpleVector touchEnd = Interact2D.reproject2D3DWS(map.getCam(), fb, (int)xEnd, (int)yEnd).normalize();
SimpleVector moveDist = touchEnd.calcSub(touchOrigin);
cameraMovement = moveDist;
cameraChanged = true;

The idea is to project the touch origin (xStart yStart) and the current touch position (xEnd yEnd) in the world space so I can calculate the distance between both and use it to move the camera.


Code: [Select]
if(cameraChanged)
{
map.getCam().moveCamera(cameraMovement, 2);
cameraChanged = false;
}


The thing is that I'm getting incoherent results, like the camera going upwards while I'm scrolling to the left etc.
It's probably an easy math problem but I really can't figure what exactly it is, I thought that substracting the two vectors would work but maybe I'm wrong.
I've tried to use camera.moveCamera using the already defined directions (Camera.CAMERA_MOVERIGHT, CAMERA_MOVELEFT ...) when we have xStart < or > xEnd, yStart < > yEnd but I'm getting more or less the same problem.

I'm still trying to find a solution, and I've already searched for this in other topics but I couldn't find a real solution at the moment.

Any idea on how I could do this please ?

Thanks in advance ! :)

Edit : I just noticed I've posted in the wrong section (I'm using JPCT AE !), sorry !

Pages: [1]