Skip to content

Commit 950721f

Browse files
stephengoldNehon
authored andcommitted
Uniform.java: avoid ClassCastException when overriding Vector4 params
1 parent edba4b9 commit 950721f

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

jme3-core/src/main/java/com/jme3/shader/Uniform.java

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2012 jMonkeyEngine
2+
* Copyright (c) 2009-2017 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -33,9 +33,9 @@
3333

3434
import com.jme3.math.*;
3535
import com.jme3.util.BufferUtils;
36-
import java.nio.Buffer;
37-
import java.nio.FloatBuffer;
38-
import java.nio.IntBuffer;
36+
import com.jme3.util.TempVars;
37+
38+
import java.nio.*;
3939

4040
public class Uniform extends ShaderVariable {
4141

@@ -348,22 +348,37 @@ public void setValue(VarType type, Object value){
348348
if (value.equals(this.value)) {
349349
return;
350350
}
351-
if (value instanceof ColorRGBA) {
352-
if (this.value == null) {
353-
this.value = new ColorRGBA();
351+
352+
TempVars vars = TempVars.get();
353+
Vector4f vec4 = vars.vect4f1;
354+
//handle the null case
355+
if (this.value == null) {
356+
try {
357+
this.value = value.getClass().newInstance();
358+
} catch (InstantiationException | IllegalAccessException e) {
359+
throw new IllegalArgumentException("Cannot instanciate param of class " + value.getClass().getCanonicalName());
354360
}
355-
((ColorRGBA) this.value).set((ColorRGBA) value);
361+
}
362+
//feed the pivot vec 4 with the correct value
363+
if (value instanceof ColorRGBA) {
364+
ColorRGBA c = (ColorRGBA) value;
365+
vec4.set(c.r, c.g, c.b, c.a);
356366
} else if (value instanceof Vector4f) {
357-
if (this.value == null) {
358-
this.value = new Vector4f();
359-
}
360-
((Vector4f) this.value).set((Vector4f) value);
367+
vec4.set((Vector4f) value);
361368
} else {
362-
if (this.value == null) {
363-
this.value = new Quaternion();
364-
}
365-
((Quaternion) this.value).set((Quaternion) value);
369+
Quaternion q = (Quaternion) value;
370+
vec4.set(q.getX(), q.getY(), q.getZ(), q.getW());
371+
}
372+
373+
//feed this.value with the collected values.
374+
if (this.value instanceof ColorRGBA) {
375+
((ColorRGBA) this.value).set(vec4.x, vec4.y, vec4.z, vec4.w);
376+
} else if (value instanceof Vector4f) {
377+
((Vector4f) this.value).set(vec4);
378+
} else {
379+
((Quaternion) this.value).set(vec4.x, vec4.y, vec4.z, vec4.w);
366380
}
381+
vars.release();
367382
break;
368383
// Only use check if equals optimization for primitive values
369384
case Int:

0 commit comments

Comments
 (0)