Skip to content

Instantly share code, notes, and snippets.

@davideicardi
Created April 19, 2015 09:18
Show Gist options
  • Save davideicardi/4f2260cc5f2fb5e85fe5 to your computer and use it in GitHub Desktop.
Save davideicardi/4f2260cc5f2fb5e85fe5 to your computer and use it in GitHub Desktop.
Angular Js directive that display a three.js model
// usage:
// <di-three-js-viewer three-js-object="device3DModel"></di-three-js-viewer>
app.directive('diThreeJsViewer', [function() {
function createThreeJsCanvas(parentElement, object) {
var width = 400;
var height = 400;
var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera(35, width / height, 2.109, 213.014);
var renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(width, height);
renderer.setClearColor( 0xffffff, 1);
parentElement.append(renderer.domElement);
camera.position.x = 0;
camera.position.y = 0;
camera.position.z = 20;
//camera.up = new THREE.Vector3(0, 0, 1);
camera.lookAt(new THREE.Vector3(0.000, 0.000, 0.000)); // look at the center
var light = new THREE.PointLight(0x404040, 2);
light.position.set(20, 20, 0);
scene.add(light);
light = new THREE.PointLight(0x404040, 2);
light.position.set(20, 0, 20);
scene.add(light);
light = new THREE.PointLight(0x404040, 2);
light.position.set(0, 20, 20);
scene.add(light);
light = new THREE.AmbientLight(0x404040);
scene.add(light);
// add object
scene.add(object);
var render = function() {
requestAnimationFrame(render);
//model.rotation.y += 0.01;
renderer.render(scene, camera);
};
render();
}
function link(scope, element, attrs) {
scope.$watch("threeJsObject", function(){
if (scope.threeJsObject) {
createThreeJsCanvas(element, scope.threeJsObject);
}
}, false);
}
return {
restrict: 'E',
scope: {
threeJsObject: '='
},
link: link
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment