|
1 | 1 | /* |
2 | | - * Copyright (c) 2009-2012 jMonkeyEngine |
| 2 | + * Copyright (c) 2009-2017 jMonkeyEngine |
3 | 3 | * All rights reserved. |
4 | 4 | * |
5 | 5 | * Redistribution and use in source and binary forms, with or without |
|
33 | 33 |
|
34 | 34 | import com.jme3.math.*; |
35 | 35 | 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.*; |
39 | 39 |
|
40 | 40 | public class Uniform extends ShaderVariable { |
41 | 41 |
|
@@ -348,22 +348,37 @@ public void setValue(VarType type, Object value){ |
348 | 348 | if (value.equals(this.value)) { |
349 | 349 | return; |
350 | 350 | } |
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()); |
354 | 360 | } |
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); |
356 | 366 | } 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); |
361 | 368 | } 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); |
366 | 380 | } |
| 381 | + vars.release(); |
367 | 382 | break; |
368 | 383 | // Only use check if equals optimization for primitive values |
369 | 384 | case Int: |
|
0 commit comments