Skip to content

Instantly share code, notes, and snippets.

@bnolan
Created August 2, 2017 05:19
Show Gist options
  • Save bnolan/04fe5aec95029da540eb6e0843f2fe4b to your computer and use it in GitHub Desktop.
Save bnolan/04fe5aec95029da540eb6e0843f2fe4b to your computer and use it in GitHub Desktop.
A-Minus core renderer
/* globals fetch, DOMParser */
import * as THREE from 'three';
import parseVector from './parse-vector';
import parseEuler from './parse-euler';
import createObjModel from './create-obj-model';
import createGltfModel from './create-gltf-model';
import createBillboard from './create-billboard';
function createCube (node) {
const geometry = new THREE.CubeGeometry(1, 1, 1);
const material = new THREE.MeshLambertMaterial({
color: node.getAttribute('color')
});
return new THREE.Mesh(geometry, material);
}
function parse (source) {
var parser = new DOMParser();
return parser.parseFromString(source, 'application/xml').documentElement;
}
function addChildren (node, parent) {
Array.from(node.childNodes).forEach((childNode) => {
var child;
if (childNode.nodeName === 'a-cube') {
child = createCube(childNode);
} else if (childNode.nodeName === 'a-billboard') {
child = createBillboard(childNode);
} else if (childNode.nodeName === 'a-obj-model') {
child = createObjModel(childNode);
} else if (childNode.nodeName === 'a-gltf-model') {
child = createGltfModel(childNode);
} else {
return;
}
console.log({childNode});
child.castShadow = true;
child.receiveShadow = true;
// traverse((o) => {
// o.castShadow = true;
// o.receiveShadow = true;
// });
// Set position and other attributes
child.position.copy(parseVector(childNode.getAttribute('position'), 0));
child.scale.copy(parseVector(childNode.getAttribute('scale'), 1));
child.rotation.copy(parseEuler(childNode.getAttribute('rotation')));
parent.add(child);
});
}
export default function loadParcel (scene, url) {
var doc;
const parent = new THREE.Object3D();
fetch(url)
.then((r) => r.text())
.then((body) => {
doc = parse(body);
addChildren(doc, parent);
});
scene.add(parent);
return parent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment