7

I was wondering...

Is it possible to add Three.js elements to a A-frame scene? Assuming A-frame is built upon Three.js, and

three Version: ^0.74.0

is logged into your console it shouldn't be a weird thing right?

I tried this code onto my A-frame scene element:

let scene = document.getElementsByTagName('a-scene');
console.log(scene);

var sphereMaterial = new THREE.MeshLambertMaterial( {  } );
var sphere = new THREE.Mesh( new THREE.SphereGeometry( 15, 10, 10 ), sphereMaterial );
    sphere.position.set(150, 20, -170);
    scene.add( sphere );

But it doesnt work because the scene object doesnt have a add function.. Maybe because the A-frame scene is not an instance of a normal WebGLRenderer?

Does anybody have experience with this? It would be very awesome!

Fabian Tjoe A On
  • 1,383
  • 5
  • 18
  • 41

1 Answers1

14

A-Frame is built on top of three.js and exposes all underlying functionality.

<a-scene>.object3D gives you access to the THREE.Scene.

Each <a-entity> has an object3D that is a group. You use getObject3D(name) and getOrCreateObject3D(name, constructor to add things to the group.

To add three.js elements within the A-Frame framework, use the Entity-Component system that A-Frame provides.

AFRAME.registerComponent('mythreejsthing', {
  schema: {
    // ... Define schema to pass properties from DOM to this component
  },

  init: function () {
    var el = this.el;  // Entity.
    var mythreejsobject = // ... Create three.js object.
    el.setObject3D('mesh', mythreejsobject);
  }
});

https://aframe.io/docs/0.2.0/core/entity.html#object3d https://aframe.io/docs/0.2.0/core/component.html

ngokevin
  • 12,980
  • 2
  • 38
  • 84
  • Can you give me an example of what goes inside the schema brackets? I tried this and I don't see my Three.JS object – Jonathan Allen Grant Nov 18 '16 at 21:50
  • 3
    This would be a minimal example, then, based on Kevins answer. http://codepen.io/dirkk0/pen/YpNrQR – dirkk0 Nov 19 '16 at 11:24
  • 1
    I'm working on a project on [8th Wall](https://www.8thwall.com/) using A-Frame and Three.js. I'm using a component called ["text-geometry"](https://github.com/supermedium/superframe/tree/master/components/text-geometry) which generates extruded text. It keeps giving me an error `getOrCreateObject3D has been deprecated. Use setObject3D() and object3dset event instead.` for the line `mesh = el.getOrCreateObject3D('mesh', THREE.Mesh)` ... how would I do that? – Dan Jan 18 '23 at 18:31