├── aframe instancedmesh Example.jpg ├── LICENSE ├── instancedmesh.js └── README.md /aframe instancedmesh Example.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/EX3D/aframe-InstancedMesh/HEAD/aframe instancedmesh Example.jpg -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 EX3D 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /instancedmesh.js: -------------------------------------------------------------------------------- 1 | AFRAME.registerComponent('instancedmesh', { 2 | schema: { 3 | retainParent: {default: false}, 4 | retainChildren: {default: false},//Not yet implemented 5 | inheritMat: {default: true}, 6 | mergeInstances: {default: false},//Not yet implemented 7 | frustumCulled: {default: true} 8 | }, 9 | 10 | init: function () { 11 | }, 12 | update: function () { 13 | var self = this; 14 | var el = this.el; 15 | var list = this.el.children; 16 | var quantity = 0; 17 | 18 | var applyMatrix = function () { 19 | var position = new THREE.Vector3(); 20 | var rotation = new THREE.Euler(); 21 | var scale = new THREE.Vector3(); 22 | var quaternion = new THREE.Quaternion(); 23 | return function ( i, matrix ) { 24 | position.x = el.children[i].object3D.position.x; 25 | position.y = el.children[i].object3D.position.y; 26 | position.z = el.children[i].object3D.position.z; 27 | rotation.x = el.children[i].object3D.rotation.x; 28 | rotation.y = el.children[i].object3D.rotation.y; 29 | rotation.z = el.children[i].object3D.rotation.z; 30 | quaternion.setFromEuler( rotation ); 31 | scale.x = el.children[i].object3D.scale.x; 32 | scale.y = el.children[i].object3D.scale.y; 33 | scale.z = el.children[i].object3D.scale.z; 34 | matrix.compose( position, quaternion, scale ); 35 | } //High verbosity because imma N00b don´t know how to access matrix on an uninitialized object 36 | }(); 37 | for (var item of list) { 38 | quantity = quantity + 1; 39 | } 40 | var mesh = this.el.getObject3D('mesh') 41 | if (!mesh) { 42 | this.el.addEventListener('model-loaded', e => { 43 | this.update.call(this, this.data) 44 | }) 45 | return; 46 | } 47 | var material = mesh.material.clone(); 48 | 49 | mesh.traverse(function(node) { 50 | if(node.type != "Mesh") return; 51 | geometry = node.geometry; 52 | }) 53 | 54 | var amesh = new THREE.InstancedMesh(geometry,material,quantity); 55 | 56 | for ( i = 0; i < quantity; i ++ ) { 57 | matrix = new THREE.Matrix4(); 58 | child = this.el.children[i]; 59 | applyMatrix (i,matrix); 60 | amesh.setMatrixAt( i, matrix ); 61 | } 62 | //frustumCulled 63 | amesh.frustumCulled = this.data.frustumCulled; 64 | this.el.object3D.add(amesh); 65 | // retainParent 66 | if (!self.data.retainParent) { this.el.object3D.remove(mesh); } 67 | // inheritMat (Set material attribute to cloned material) 68 | if (self.data.inheritMat) { 69 | this.el.components.material.material = material; 70 | } 71 | }, 72 | }); 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aframe-instanced-mesh 2 | 3 | W.I.P Component for A-Frame entities to leverage threejs InstancedMesh. 4 | 5 | ![Example Image](https://github.com/EX3D/aframe-InstancedMesh/blob/master/aframe%20instancedmesh%20Example.jpg) 6 | 7 | Component A-Frame to take advantage of threejs instanced rendering. The objective behind this component is reducing the overall number of calls for groups of entities sharing geometry and material while using traditional a-frame notation. Using a higher level of abstaction with InstancedMesh this component will support native geometry as well as imported geometry such as .obj parsing for threejs BufferGeometry. 8 | 9 | This leverages type of threejs Mesh (InstancedMesh) and will use (mergeBufferGeometries) 10 | 11 | ## WIP 12 | 13 | - Implement schema components: Retain Children & mergeinstances 14 | 15 | - As of now threejs does not cull individual geometries so the resulting instancedmesh will inherit the original bounding sphere of the parent object. I will add a tick component that recreates mesh instancing depending if child enitites intersect with camera frustum. 16 | 17 | - Implement mergeInstances using three js BufferGeometryUtils.mergeBufferGeometries 18 | 19 | - Allow some kind of randomization for materials and transformations (This is last priority because this has no use for our main project) 20 | 21 | ### A-Frame entity notation 22 | 23 | ```html 24 | 25 | 26 | 27 | 28 | 29 | ``` 30 | *Child Objects can have placeholder geometry (Ex. Child 1 has sphere geometry) and can be erased by toggling retainChildren. 31 | 32 | ### How it works and Why 33 | 34 | Designed to be used on an HTMLCollection consistent of a parent object and many child objects. This is used for arrays of objects that require precise transformations such as groups of buildings, vehicles and trees (See:Example Image) 35 | 36 | - Takes geometry and material of parent entity Oject3D. 37 | - Populates parent Oject3d with number of child entities and corresponding transformations using InstancedMesh of parent geometry and material**. 38 | - Erases parent entityobject3d original geometry. 39 | - Erases children from DOM leaving only parent entity. 40 | 41 | **Right now this component creates a .clone() of parent material because of a known threejs limitation. 42 | 43 | ### Component Schema 44 | 45 | | Property | Description | Default Value | Implemented | 46 | | -------- | ----------- | ------------- | ------------| 47 | | `retainParent` | Retain Parent Geometry | `false` | Yes | 48 | | `retainChildren` | Retain Children in DOM | `false` | No | 49 | | `inheritMat` | A-frame entity will now use cloned Material | `true` | Yes | 50 | | `mergeInstances` | Merge all instances into one single mesh (This would be used to reduce reuqest animation calls)| `false` | No | 51 | | `frustumCulled` | Culls non visible meshInstances | `true` | Yes | 52 | 53 | ## TESTED ON 54 | A-Frame v1.0.4 55 | --------------------------------------------------------------------------------