I have developed a companion program for the game Minecraft and have decided to provide a 3D display of the imported data.
The data comes from a Minecraft schematic file giving length,width and height of the schematic. It also provides an array that gives the blocks material. It’s position in the array can be used find its location within the schematic.
When i put it to code I get a 3D display with the blocks in the correct x and z position but the y position is wrong.
I am using Flash CC version 13.1.0.226, This is an AIR for desktop version 3.8 and I am using Away3D version 4.1.6
Here is the code:
package ui {
import away3d.containers.*;
import away3d.entities.*;
import away3d.materials.*;
import away3d.primitives.*;
import away3d.utils.*;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Vector3D;
import flash.utils.ByteArray;
public class Schematic3D extends Sprite {
private var fileData:FileToData;
private var size:int;
private var l:int;
private var w:int;
private var h:int;
private var blocks:ByteArray;
private var blockData:ByteArray;
private var view:View3D;
private var cube:Mesh;
public function Schematic3D(fileToData:FileToData) {
addEventListener(Event.ADDED_TO_STAGE, onAdded);
addEventListener(Event.REMOVED_FROM_STAGE, onRemoved);
fileData = fileToData;
}
private function onAdded(e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, onAdded);
size = 30;
h = fileData.schematicHeight;
l = fileData.schematicLength;
w = fileData.schematicWidth;
blocks = fileData.blocks;
blockData = fileData.blockData;
show();
}
private function show():void {
var bpl = l * w;
//setup the view
view = new View3D();
addChild(view);
//setup the camera
view.camera.x = 300;
view.camera.z = -300;
view.camera.y = 300;
view.camera.lookAt(new Vector3D());
//setup the scene
for(var i:int = 0; i < blocks.length; i++) {
cube = new Mesh(new CubeGeometry(size, size, size), new TextureMaterial(Cast.bitmapTexture(Stone)));
cube.x = (i % w) * size;
cube.y = int(i / bpl) * size;
cube.z = -((int(i / w) - (int(i / bpl) * bpl)) * size);
view.scene.addChild(cube);
}
//setup the render loop
addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
private function onEnterFrame(e:Event):void {
view.render();
}
private function onRemoved(e:Event):void {
removeEventListener(Event.ENTER_FRAME, onEnterFrame);
view.dispose();
}
}
}
Here is what the display looks like:
Output image