Uniform.java: avoid ClassCastException when overriding Vector4 params

empirephoenix-patch-1
Stephen Gold 7 years ago committed by Rémy Bouquet
parent edba4b9844
commit 950721f926
  1. 47
      jme3-core/src/main/java/com/jme3/shader/Uniform.java

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2009-2012 jMonkeyEngine * Copyright (c) 2009-2017 jMonkeyEngine
* All rights reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
@ -33,9 +33,9 @@ package com.jme3.shader;
import com.jme3.math.*; import com.jme3.math.*;
import com.jme3.util.BufferUtils; import com.jme3.util.BufferUtils;
import java.nio.Buffer; import com.jme3.util.TempVars;
import java.nio.FloatBuffer;
import java.nio.IntBuffer; import java.nio.*;
public class Uniform extends ShaderVariable { public class Uniform extends ShaderVariable {
@ -348,22 +348,37 @@ public class Uniform extends ShaderVariable {
if (value.equals(this.value)) { if (value.equals(this.value)) {
return; return;
} }
if (value instanceof ColorRGBA) {
if (this.value == null) { TempVars vars = TempVars.get();
this.value = new ColorRGBA(); Vector4f vec4 = vars.vect4f1;
//handle the null case
if (this.value == null) {
try {
this.value = value.getClass().newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException("Cannot instanciate param of class " + value.getClass().getCanonicalName());
} }
((ColorRGBA) this.value).set((ColorRGBA) value); }
//feed the pivot vec 4 with the correct value
if (value instanceof ColorRGBA) {
ColorRGBA c = (ColorRGBA) value;
vec4.set(c.r, c.g, c.b, c.a);
} else if (value instanceof Vector4f) { } else if (value instanceof Vector4f) {
if (this.value == null) { vec4.set((Vector4f) value);
this.value = new Vector4f();
}
((Vector4f) this.value).set((Vector4f) value);
} else { } else {
if (this.value == null) { Quaternion q = (Quaternion) value;
this.value = new Quaternion(); vec4.set(q.getX(), q.getY(), q.getZ(), q.getW());
} }
((Quaternion) this.value).set((Quaternion) value);
//feed this.value with the collected values.
if (this.value instanceof ColorRGBA) {
((ColorRGBA) this.value).set(vec4.x, vec4.y, vec4.z, vec4.w);
} else if (value instanceof Vector4f) {
((Vector4f) this.value).set(vec4);
} else {
((Quaternion) this.value).set(vec4.x, vec4.y, vec4.z, vec4.w);
} }
vars.release();
break; break;
// Only use check if equals optimization for primitive values // Only use check if equals optimization for primitive values
case Int: case Int:

Loading…
Cancel
Save