- Reverted previous uniform change, it was causing changes in constants, when user was assigning a constant to a uniform (like mat.setColor("Color",ColorRGBA.White)).

- Changed Material's renderMultipassLighting method to not reuse Shader's values instance, and to use temporary allocated variables in the material instance instead

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7901 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
rem..om 14 years ago
parent 1bbbc3b0bb
commit c172b2e455
  1. 56
      engine/src/core/com/jme3/material/Material.java
  2. 74
      engine/src/core/com/jme3/shader/Uniform.java

@ -698,6 +698,9 @@ public class Material implements Cloneable, Savable, Comparable<Material> {
lightIndex++; lightIndex++;
} }
} }
Quaternion tmpLightDirection = new Quaternion();
Quaternion tmpLightPosition = new Quaternion();
ColorRGBA tmpLightColor = new ColorRGBA();
protected void renderMultipassLighting(Shader shader, Geometry g, Renderer r) { protected void renderMultipassLighting(Shader shader, Geometry g, Renderer r) {
LightList lightList = g.getWorldLightList(); LightList lightList = g.getWorldLightList();
@ -727,41 +730,25 @@ public class Material implements Cloneable, Savable, Comparable<Material> {
} }
ColorRGBA color = l.getColor(); ColorRGBA color = l.getColor();
ColorRGBA color2; tmpLightColor.set(color);
if (lightColor.getValue() != null) { tmpLightColor.a = l.getType().getId();
color2 = (ColorRGBA) lightColor.getValue(); lightColor.setValue(VarType.Vector4, tmpLightColor);
} else {
color2 = new ColorRGBA();
}
color2.set(color);
color2.a = l.getType().getId();
lightColor.setValue(VarType.Vector4, color2);
switch (l.getType()) { switch (l.getType()) {
case Directional: case Directional:
DirectionalLight dl = (DirectionalLight) l; DirectionalLight dl = (DirectionalLight) l;
Vector3f dir = dl.getDirection(); Vector3f dir = dl.getDirection();
Quaternion q1;
if (lightPos.getValue() != null) { tmpLightPosition.set(dir.getX(), dir.getY(), dir.getZ(), -1);
q1 = (Quaternion) lightPos.getValue(); lightPos.setValue(VarType.Vector4, tmpLightPosition);
} else {
q1 = new Quaternion();
}
q1.set(dir.getX(), dir.getY(), dir.getZ(), -1);
lightPos.setValue(VarType.Vector4, q1);
break; break;
case Point: case Point:
PointLight pl = (PointLight) l; PointLight pl = (PointLight) l;
Vector3f pos = pl.getPosition(); Vector3f pos = pl.getPosition();
float invRadius = pl.getInvRadius(); float invRadius = pl.getInvRadius();
Quaternion q2;
if (lightPos.getValue() != null) { tmpLightPosition.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
q2 = (Quaternion) lightPos.getValue(); lightPos.setValue(VarType.Vector4, tmpLightPosition);
} else {
q2 = new Quaternion();
}
q2.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
lightPos.setValue(VarType.Vector4, q2);
break; break;
case Spot: case Spot:
SpotLight sl = (SpotLight) l; SpotLight sl = (SpotLight) l;
@ -770,22 +757,11 @@ public class Material implements Cloneable, Savable, Comparable<Material> {
float invRange = sl.getInvSpotRange(); float invRange = sl.getInvSpotRange();
float spotAngleCos = sl.getPackedAngleCos(); float spotAngleCos = sl.getPackedAngleCos();
Quaternion q3,q4; tmpLightPosition.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
if (lightPos.getValue() != null) { lightPos.setValue(VarType.Vector4, tmpLightPosition);
q3 = (Quaternion) lightPos.getValue();
} else {
q3 = new Quaternion();
}
q3.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
lightPos.setValue(VarType.Vector4, q3);
if (lightDir.getValue() != null) { tmpLightDirection.set(dir2.getX(), dir2.getY(), dir2.getZ(), spotAngleCos);
q4 = (Quaternion) lightDir.getValue(); lightDir.setValue(VarType.Vector4, tmpLightDirection);
} else {
q4 = new Quaternion();
}
q4.set(dir2.getX(), dir2.getY(), dir2.getZ(), spotAngleCos);
lightDir.setValue(VarType.Vector4, q4);
break; break;
default: default:

@ -29,6 +29,7 @@
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package com.jme3.shader; package com.jme3.shader;
import com.jme3.export.JmeExporter; import com.jme3.export.JmeExporter;
@ -41,7 +42,6 @@ import com.jme3.math.Matrix4f;
import com.jme3.math.Quaternion; import com.jme3.math.Quaternion;
import com.jme3.math.Vector2f; import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f; import com.jme3.math.Vector3f;
import com.jme3.math.Vector4f;
import com.jme3.util.BufferUtils; import com.jme3.util.BufferUtils;
import java.io.IOException; import java.io.IOException;
import java.nio.FloatBuffer; import java.nio.FloatBuffer;
@ -51,19 +51,23 @@ public class Uniform extends ShaderVariable {
private static final Integer ZERO_INT = Integer.valueOf(0); private static final Integer ZERO_INT = Integer.valueOf(0);
private static final Float ZERO_FLT = Float.valueOf(0); private static final Float ZERO_FLT = Float.valueOf(0);
private static final FloatBuffer ZERO_BUF = BufferUtils.createFloatBuffer(4*4); private static final FloatBuffer ZERO_BUF = BufferUtils.createFloatBuffer(4*4);
/** /**
* Currently set value of the uniform. * Currently set value of the uniform.
*/ */
protected Object value = null; protected Object value = null;
protected FloatBuffer multiData = null; protected FloatBuffer multiData = null;
/** /**
* Type of uniform * Type of uniform
*/ */
protected VarType varType; protected VarType varType;
/** /**
* Binding to a renderer value, or null if user-defined uniform * Binding to a renderer value, or null if user-defined uniform
*/ */
protected UniformBinding binding; protected UniformBinding binding;
protected boolean setByCurrentMaterial = false; protected boolean setByCurrentMaterial = false;
// protected Object lastChanger = null; // protected Object lastChanger = null;
@ -209,6 +213,7 @@ public class Uniform extends ShaderVariable {
// public Object getLastChanger(){ // public Object getLastChanger(){
// return lastChanger; // return lastChanger;
// } // }
public void clearValue(){ public void clearValue(){
updateNeeded = true; updateNeeded = true;
@ -226,9 +231,8 @@ public class Uniform extends ShaderVariable {
return; return;
} }
if (varType == null) { if (varType == null)
return; return;
}
switch (varType){ switch (varType){
case Int: case Int:
@ -241,18 +245,16 @@ public class Uniform extends ShaderVariable {
this.value = ZERO_FLT; this.value = ZERO_FLT;
break; break;
case Vector2: case Vector2:
((Vector2f) this.value).set(Vector2f.ZERO); this.value = Vector2f.ZERO;
break; break;
case Vector3: case Vector3:
((Vector3f) this.value).set(Vector3f.ZERO); this.value = Vector3f.ZERO;
break; break;
case Vector4: case Vector4:
if (this.value instanceof ColorRGBA){ if (this.value instanceof ColorRGBA){
((ColorRGBA) this.value).set(ColorRGBA.BlackNoAlpha); this.value = ColorRGBA.BlackNoAlpha;
} else if (this.value instanceof Quaternion) {
((Quaternion) this.value).set(Quaternion.ZERO);
}else{ }else{
((Vector4f) this.value).set(Vector4f.ZERO); this.value = Quaternion.ZERO;
} }
break; break;
default: default:
@ -262,35 +264,30 @@ public class Uniform extends ShaderVariable {
} }
public void setValue(VarType type, Object value){ public void setValue(VarType type, Object value){
if (location == -1) { if (location == -1)
return; return;
}
if (varType != null && varType != type) { if (varType != null && varType != type)
throw new IllegalArgumentException("Expected a "+varType.name()+" value!"); throw new IllegalArgumentException("Expected a "+varType.name()+" value!");
}
if (value == null) { if (value == null)
throw new NullPointerException(); throw new NullPointerException();
}
setByCurrentMaterial = true; setByCurrentMaterial = true;
switch (type){ switch (type){
case Matrix3: case Matrix3:
Matrix3f m3 = (Matrix3f) value; Matrix3f m3 = (Matrix3f) value;
if (multiData == null) { if (multiData == null)
multiData = BufferUtils.createFloatBuffer(9); multiData = BufferUtils.createFloatBuffer(9);
}
m3.fillFloatBuffer(multiData, true); m3.fillFloatBuffer(multiData, true);
multiData.clear(); multiData.clear();
break; break;
case Matrix4: case Matrix4:
Matrix4f m4 = (Matrix4f) value; Matrix4f m4 = (Matrix4f) value;
if (multiData == null) { if (multiData == null)
multiData = BufferUtils.createFloatBuffer(16); multiData = BufferUtils.createFloatBuffer(16);
}
m4.fillFloatBuffer(multiData, true); m4.fillFloatBuffer(multiData, true);
multiData.clear(); multiData.clear();
@ -314,9 +311,8 @@ public class Uniform extends ShaderVariable {
multiData = BufferUtils.ensureLargeEnough(multiData, v2a.length * 2); multiData = BufferUtils.ensureLargeEnough(multiData, v2a.length * 2);
} }
for (int i = 0; i < v2a.length; i++) { for (int i = 0; i < v2a.length; i++)
BufferUtils.setInBuffer(v2a[i], multiData, i); BufferUtils.setInBuffer(v2a[i], multiData, i);
}
multiData.clear(); multiData.clear();
break; break;
@ -328,9 +324,8 @@ public class Uniform extends ShaderVariable {
multiData = BufferUtils.ensureLargeEnough(multiData, v3a.length * 3); multiData = BufferUtils.ensureLargeEnough(multiData, v3a.length * 3);
} }
for (int i = 0; i < v3a.length; i++) { for (int i = 0; i < v3a.length; i++)
BufferUtils.setInBuffer(v3a[i], multiData, i); BufferUtils.setInBuffer(v3a[i], multiData, i);
}
multiData.clear(); multiData.clear();
break; break;
@ -342,39 +337,36 @@ public class Uniform extends ShaderVariable {
multiData = BufferUtils.ensureLargeEnough(multiData, v4a.length * 4); multiData = BufferUtils.ensureLargeEnough(multiData, v4a.length * 4);
} }
for (int i = 0; i < v4a.length; i++) { for (int i = 0; i < v4a.length; i++)
BufferUtils.setInBuffer(v4a[i], multiData, i); BufferUtils.setInBuffer(v4a[i], multiData, i);
}
multiData.clear(); multiData.clear();
break; break;
case Matrix3Array: case Matrix3Array:
Matrix3f[] m3a = (Matrix3f[]) value; Matrix3f[] m3a = (Matrix3f[]) value;
if (multiData == null) { if (multiData == null)
multiData = BufferUtils.createFloatBuffer(m3a.length * 9); multiData = BufferUtils.createFloatBuffer(m3a.length * 9);
} else { else{
multiData = BufferUtils.ensureLargeEnough(multiData, m3a.length * 9); multiData = BufferUtils.ensureLargeEnough(multiData, m3a.length * 9);
} }
for (int i = 0; i < m3a.length; i++) { for (int i = 0; i < m3a.length; i++)
m3a[i].fillFloatBuffer(multiData, true); m3a[i].fillFloatBuffer(multiData, true);
}
multiData.clear(); multiData.clear();
break; break;
case Matrix4Array: case Matrix4Array:
Matrix4f[] m4a = (Matrix4f[]) value; Matrix4f[] m4a = (Matrix4f[]) value;
if (multiData == null) { if (multiData == null)
multiData = BufferUtils.createFloatBuffer(m4a.length * 16); multiData = BufferUtils.createFloatBuffer(m4a.length * 16);
} else { else{
multiData = BufferUtils.ensureLargeEnough(multiData, m4a.length * 16); multiData = BufferUtils.ensureLargeEnough(multiData, m4a.length * 16);
} }
for (int i = 0; i < m4a.length; i++) { for (int i = 0; i < m4a.length; i++)
m4a[i].fillFloatBuffer(multiData, true); m4a[i].fillFloatBuffer(multiData, true);
}
multiData.clear(); multiData.clear();
break; break;
@ -382,9 +374,8 @@ public class Uniform extends ShaderVariable {
case Int: case Int:
case Float: case Float:
case Boolean: case Boolean:
if (this.value != null && this.value.equals(value)) { if (this.value != null && this.value.equals(value))
return; return;
}
this.value = value; this.value = value;
break; break;
@ -393,18 +384,16 @@ public class Uniform extends ShaderVariable {
break; break;
} }
if (multiData != null) { if (multiData != null)
this.value = multiData; this.value = multiData;
}
varType = type; varType = type;
updateNeeded = true; updateNeeded = true;
} }
public void setVector4Length(int length){ public void setVector4Length(int length){
if (location == -1) { if (location == -1)
return; return;
}
FloatBuffer fb = (FloatBuffer) value; FloatBuffer fb = (FloatBuffer) value;
if (fb == null || fb.capacity() < length){ if (fb == null || fb.capacity() < length){
@ -417,13 +406,11 @@ public class Uniform extends ShaderVariable {
} }
public void setVector4InArray(float x, float y, float z, float w, int index){ public void setVector4InArray(float x, float y, float z, float w, int index){
if (location == -1) { if (location == -1)
return; return;
}
if (varType != null && varType != VarType.Vector4Array) { if (varType != null && varType != VarType.Vector4Array)
throw new IllegalArgumentException("Expected a "+varType.name()+" value!"); throw new IllegalArgumentException("Expected a "+varType.name()+" value!");
}
FloatBuffer fb = (FloatBuffer) value; FloatBuffer fb = (FloatBuffer) value;
fb.position(index * 4); fb.position(index * 4);
@ -446,4 +433,5 @@ public class Uniform extends ShaderVariable {
location = -2; location = -2;
updateNeeded = true; updateNeeded = true;
} }
} }

Loading…
Cancel
Save