Skip to content

Commit

Permalink
Add rotation of blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Fechner authored and Florian Fechner committed Feb 24, 2018
1 parent 2b2b532 commit b61149b
Show file tree
Hide file tree
Showing 289 changed files with 300 additions and 40 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"animation": {
"frametime": 3,
"frames": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
{
"animation": {
"frametime": 2,
"frames": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19,
18,
17,
16,
15,
14,
13,
12,
11,
10,
9,
8,
7,
6,
5,
4,
3,
2,
1
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"animation": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"animation": {
"frametime": 300,
"interpolate": true,
"frames": [
0,
1,
0,
2,
0,
3,
0,
1,
2,
1,
3,
1,
0,
2,
1,
2,
3,
2,
0,
3,
1,
3
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"animation": {
"frametime": 5
}
}
6 changes: 6 additions & 0 deletions demo/faithful32pack/pack.mcmeta
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"pack": {
"pack_format": 1,
"description": "Vattic\u0027s Faithful 32x32 pack"
}
}
Binary file added demo/faithful32pack/pack.png
120 changes: 120 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes">

<title>voxel-visualization demo</title>

<script src="../../webcomponentsjs/webcomponents-lite.js"></script>

<link rel="import" href="../../iron-demo-helpers/demo-pages-shared-styles.html">
<link rel="import" href="../../iron-demo-helpers/demo-snippet.html">
<link rel="import" href="../../iron-icons/iron-icons.html">
<link rel="import" href="../../iron-icon/iron-icon.html">
<link rel="import" href="../../paper-slider/paper-slider.html">
<link rel="import" href="../../paper-card/paper-card.html">
<link rel="import" href="../../voxel-visualization/voxel-visualization.html">

<custom-style>
<style is="custom-style" include="demo-pages-shared-styles"></style>
</custom-style>

<custom-style>
<style>

table
{
width: 100%;
}

td
{
width: 80%;
text-align: center;
}

td.small
{
width: 10%;
text-align: center;
}

paper-slider
{
width: 100%;
}

paper-card
{
padding: 15px;
text-align: center;
}

</style>
</custom-style>
</head>
<body>
<div class="vertical-section-container centered">

<h3>Schematic</h3>
<demo-snippet>
<template>
<voxel-visualization
schematic-path="schematics/house.schematic"
></voxel-visualization>
</template>
</demo-snippet>

<h3>Textures</h3>
<demo-snippet>
<template>
<voxel-visualization
schematic-path="schematics/house.schematic"
texture-pack-path="faithful32pack/"
></voxel-visualization>
</template>
</demo-snippet>



<br><br>

<h3>Zoom</h3>
<demo-snippet>
<template>
<dom-bind id="scope">
<template>
<voxel-visualization
schematic-path="schematics/tower.schematic"
texture-pack-path="faithful32pack/"
zoom="[[zoom]]"
></voxel-visualization>

<br>

<table>
<tbody>
<tr>
<td class="small"><iron-icon icon="zoom-in"></iron-icon></td>
<td class="small">[[zoom]]</td>
<td><paper-slider pin value="{{zoom}}" min="0.25" max="4.0" step="0.125"></paper-slider></td>
</tr>
</tbody>
</table>
</template>
</dom-bind>

<script>
scope.zoom = 1;
</script>
</template>
</demo-snippet>

<paper-card>
🗒 Note: The used texture pack "Faithful 32x32 Pack" (<a href="https://www.minecraftforum.net/forums/mapping-and-modding-java-edition/resource-packs/1223254-faithful-32x32-pack-update-red-cat-clay-1-8">Link</a>) was made by "Vattic".
</paper-card>

</div>
</body>
</html>
Binary file added demo/schematics/house.schematic
Binary file not shown.
Binary file added demo/schematics/tower.schematic
Binary file not shown.
108 changes: 68 additions & 40 deletions voxel-visualization.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,13 @@ class VoxelVisualization extends Polymer.mixinBehaviors([Polymer.IronResizableBe

// THREE.JS stuff

let vertices = new Float32Array(this.calculateVertices(faces, expandedWidth, expandedHeight, expandedDepth));
let normals = new Float32Array(this.calculateNormals(faces, expandedWidth, expandedHeight, expandedDepth));
let uvs = new Float32Array(this.calculateUVs(faces));

let faceMap = this.calculateFaceMap(faces);
let keys = Object.getOwnPropertyNames(faceMap);

let vertices = new Float32Array(this.calculateVertices(faces, expandedWidth, expandedHeight, expandedDepth));
let normals = new Float32Array(this.calculateNormals(faces, expandedWidth, expandedHeight, expandedDepth));
let uvs = new Float32Array(this.calculateUVs(faces, blockIdList));

this.group = new THREE.Group();

for(let i=0; i<keys.length; i++)
Expand Down Expand Up @@ -302,6 +302,39 @@ class VoxelVisualization extends Polymer.mixinBehaviors([Polymer.IronResizableBe
return [this.baseURI, this.texturePackPath, "/assets/minecraft/textures/blocks/", textureName, ".png"].join("");
}

calculateTextureRotation(key, blockIdList)
{
let splittedKey = key.split(".");
let type = splittedKey[0];
let metaType = splittedKey[1];
let normal = JSON.parse(splittedKey[2]);

let textureObject = (blockIdList[type][metaType]) ? blockIdList[type][metaType] : blockIdList[type]["*"];
let texture;

if(typeof textureObject === 'object')
{
let orientation = "none";

if(normal.z == -1) orientation = "north";
if(normal.x == 1) orientation = "east";
if(normal.z == 1) orientation = "south";
if(normal.x == -1) orientation = "west";
if(normal.y == 1) orientation = "top";
if(normal.y == -1) orientation = "bottom";

texture = (textureObject[orientation]) ? textureObject[orientation] : textureObject["*"];
}
else
{
texture = textureObject;
}

let textureRotation = parseInt(texture.split(":")[1]);

return (textureRotation) ? textureRotation : 0;
}

calculateVertices(faces, width, height, depth)
{
let vertices = [];
Expand Down Expand Up @@ -415,61 +448,56 @@ class VoxelVisualization extends Polymer.mixinBehaviors([Polymer.IronResizableBe
return normals;
}

calculateUVs(faces)
calculateUVs(faces, blockIdList)
{
let uvs = [];

for(let i=0; i<faces.length; i++)
{
let face = faces[i];
let id = face.block.id;
let metaData = face.block.metaData;
let normal = face.normal;
let normalJson = JSON.stringify(normal);
let key = [id, metaData, normalJson].join(".");

let normalX = face.normal.x;
let normalY = face.normal.y;
let normalZ = face.normal.z;
let textureRotation = this.calculateTextureRotation(key, blockIdList);

if(normalX != 0)
{
uvs.push(1.0);
uvs.push(0.0);
console.log(textureRotation);

uvs.push(1.0);
uvs.push(1.0);
let normalX = normal.x;
let normalY = normal.y;
let normalZ = normal.z;

uvs.push(0.0);
uvs.push(1.0);
let uvRotation;

uvs.push(0.0);
uvs.push(0.0);
if(normalX != 0)
{
uvRotation = textureRotation + 270;
}

if(normalY != 0)
else
{
uvs.push(1.0);
uvs.push(1.0);
uvRotation = textureRotation;
}

uvs.push(0.0);
uvs.push(1.0);
console.log(uvRotation)

uvs.push(0.0);
uvs.push(0.0);
let coordinates = [
1.0, 1.0,
0.0, 1.0,
0.0, 0.0,
1.0, 0.0
];

uvs.push(1.0);
uvs.push(0.0);
}
let numberOfPoints = coordinates.length / 2;
let offset = uvRotation / 90;

if(normalZ != 0)
for(let i=0; i<numberOfPoints; i++)
{
uvs.push(1.0);
uvs.push(1.0);

uvs.push(0.0);
uvs.push(1.0);

uvs.push(0.0);
uvs.push(0.0);
let index = (offset + i) % numberOfPoints;

uvs.push(1.0);
uvs.push(0.0);
uvs.push(coordinates[index * 2]);
uvs.push(coordinates[index * 2 + 1]);
}
}

Expand Down

0 comments on commit b61149b

Please sign in to comment.