We are about to switch to a new forum software. Until then we have removed the registration on this forum.
https://www.dropbox.com/sh/2riljmpqfywibaa/AADseXkl0ms0WOYcjPSjKow7a?dl=0
^ that's a link to my code
Someone else was apparently able to save/load cameras in proscene from a file: https://forum.processing.org/one/topic/proscene-saving-loading-camera-settings-to-from-file-what-are-the-key-settings-of-a-camera.html
But I'm having trouble implementing their code. I have a feeling it's a basic problem that I just don't understand. When I run this:
import processing.data.JSONObject;
//this is how you save the camera, theoretically;
//save a Proscene scene current camera settings in as a JSON string in a file
void saveCamera(Scene scene, String fileName ) {
processing.data.JSONObject json = new JSONObject();
json.setFloat("fov", scene.camera().fieldOfView() );
setVector(json, "position", scene.camera().position() );
setVector(json, "viewdirection", scene.camera().viewDirection() );
setVector(json, "upvector", scene.camera().upVector() );
saveJSONObject(json, fileName);
}
//Add a PVector as a string representation of a float array to a JSON object
void setVector(JSONObject json, String attributeName, Vec v) {
json.setString( attributeName, Arrays.toString( new float[]{ v.x, v.y, v.z} ) );
}
//#####this is how you load the camera, theoretically;
void loadCamera(Scene scene, String fileName) {
JSONObject json = loadJSONObject(fileName);
scene.camera().setFieldOfView( json.getFloat("fov") );
scene.camera().setUpVector( getVector(json, "upvector") );
scene.camera().setViewDirection( getVector(json, "viewdirection") );
scene.camera().setPosition( getVector(json, "position") );
}
//Parse a PVector from its string representation as an array of float in a JSON object
PVector getVector(JSONObject json, String attributeName) {
String o = json.getString(attributeName);
String[] arr = o.substring(1, o.length()-1).split(", ");
float[] f = new float[arr.length];
int i = 0;
for (String s : arr) {
f[i] = Float.parseFloat(s);
i++;
}
return new PVector(f[0], f[1], f[2]);
}
"Arrays" isn't pointing to anything, and I'm not sure what to do about it?
Answers
You can remove the redundant
import processing.data.JSONObject;
. PDE already gets it.On the other hand, in order to use Arrays.toString()
static
method, you're better off importing it 1st:import java.util.Arrays;
http://docs.Oracle.com/javase/8/docs/api/java/util/Arrays.html#toString-float:A-
Also, this whole code:
Can be replaced by Processing's float() conversion function: *-:)
https://Processing.org/reference/floatconvert_.html
https://Processing.org/reference/PVector_set_.html
Awesome; thank you; got everything working!
I liked the idea ;)
I implemented it as
saveConfig
/loadConfig
in the scene. It deals with camera attributes, keyframes and visual hints and it should be available in the next release