|
| 1 | +/** |
| 2 | + * @author Richard Davey <rich@photonstorm.com> |
| 3 | + * @copyright 2020 Photon Storm Ltd. |
| 4 | + * @license {@link https://opensource.org/licenses/MIT|MIT License} |
| 5 | + */ |
| 6 | + |
| 7 | +var Box3 = require('../math/Box3'); |
| 8 | +var BufferAttribute = require('./BufferAttribute'); |
| 9 | +var Class = require('../../utils/Class'); |
| 10 | +var Sphere = require('../math/Sphere'); |
| 11 | + |
| 12 | +var geometryId = 1; |
| 13 | + |
| 14 | +var Geometry = new Class({ |
| 15 | + |
| 16 | + initialize: |
| 17 | + |
| 18 | + function Geometry () |
| 19 | + { |
| 20 | + this.id = geometryId++; |
| 21 | + |
| 22 | + /** |
| 23 | + * This hashmap has an id the name of the attribute to be set and as value the buffer to set it to. |
| 24 | + * Rather than accessing this property directly, use `addAttribute` and `getAttribute` to access attributes of this geometry. |
| 25 | + */ |
| 26 | + this.attributes = {}; |
| 27 | + |
| 28 | + /** |
| 29 | + * Hashmap of Attributes Array for morph targets. |
| 30 | + */ |
| 31 | + this.morphAttributes = {}; |
| 32 | + |
| 33 | + /** |
| 34 | + * Allows for vertices to be re-used across multiple triangles; this is called using "indexed triangles" and each triangle is associated with the indices of three vertices. |
| 35 | + * This attribute therefore stores the index of each vertex for each triangular face. |
| 36 | + * If this attribute is not set, the renderer assumes that each three contiguous positions represent a single triangle. |
| 37 | + * @type {BufferAttribute} |
| 38 | + */ |
| 39 | + this.index = null; |
| 40 | + |
| 41 | + /** |
| 42 | + * Bounding box for the bufferGeometry, which can be calculated with `computeBoundingBox`. |
| 43 | + * @type {Box3} |
| 44 | + */ |
| 45 | + this.boundingBox = new Box3(); |
| 46 | + |
| 47 | + /** |
| 48 | + * Bounding sphere for the bufferGeometry, which can be calculated with `computeBoundingSphere`. |
| 49 | + * @type {Sphere} |
| 50 | + */ |
| 51 | + this.boundingSphere = new Sphere(); |
| 52 | + |
| 53 | + /** |
| 54 | + * Split the geometry into groups, each of which will be rendered in a separate WebGL draw call. This allows an array of materials to be used with the geometry. |
| 55 | + * Each group is an object of the form: |
| 56 | + * { start: Integer, count: Integer, materialIndex: Integer } |
| 57 | + */ |
| 58 | + this.groups = []; |
| 59 | + |
| 60 | + /** |
| 61 | + * A version number, incremented every time the attribute object or index object changes to mark VAO drity. |
| 62 | + */ |
| 63 | + this.version = 0; |
| 64 | + }, |
| 65 | + |
| 66 | + /** |
| 67 | + * Adds an attribute to this geometry. |
| 68 | + * Use this rather than the attributes property. |
| 69 | + * @param {string} name |
| 70 | + * @param {BufferAttribute|InterleavedBufferAttribute} attribute |
| 71 | + */ |
| 72 | + addAttribute: function (name, attribute) |
| 73 | + { |
| 74 | + this.attributes[name] = attribute; |
| 75 | + }, |
| 76 | + |
| 77 | + /** |
| 78 | + * Returns the attribute with the specified name. |
| 79 | + * @return {BufferAttribute|InterleavedBufferAttribute} |
| 80 | + */ |
| 81 | + getAttribute: function (name) |
| 82 | + { |
| 83 | + return this.attributes[name]; |
| 84 | + }, |
| 85 | + |
| 86 | + /** |
| 87 | + * Removes the attribute with the specified name. |
| 88 | + */ |
| 89 | + removeAttribute: function (name) |
| 90 | + { |
| 91 | + delete this.attributes[name]; |
| 92 | + }, |
| 93 | + |
| 94 | + /** |
| 95 | + * Set the index buffer. |
| 96 | + * @param {Array|BufferAttribute} index |
| 97 | + */ |
| 98 | + setIndex: function (index) |
| 99 | + { |
| 100 | + if (Array.isArray(index)) |
| 101 | + { |
| 102 | + this.index = new BufferAttribute(new Uint16Array(index), 1); |
| 103 | + } |
| 104 | + else |
| 105 | + { |
| 106 | + this.index = index; |
| 107 | + } |
| 108 | + }, |
| 109 | + |
| 110 | + /** |
| 111 | + * Adds a group to this geometry; see the groups for details. |
| 112 | + * @param {number} start |
| 113 | + * @param {number} count |
| 114 | + * @param {number} materialIndex |
| 115 | + */ |
| 116 | + addGroup: function (start, count, materialIndex) |
| 117 | + { |
| 118 | + this.groups.push({ |
| 119 | + start: start, |
| 120 | + count: count, |
| 121 | + materialIndex: materialIndex !== undefined ? materialIndex : 0 |
| 122 | + }); |
| 123 | + }, |
| 124 | + |
| 125 | + /** |
| 126 | + * Clears all groups. |
| 127 | + */ |
| 128 | + clearGroups: function () |
| 129 | + { |
| 130 | + this.groups = []; |
| 131 | + }, |
| 132 | + |
| 133 | + /** |
| 134 | + * Computes bounding box of the geometry, updating `boundingBox`. |
| 135 | + * Bounding boxes aren't computed by default. They need to be explicitly computed. |
| 136 | + */ |
| 137 | + computeBoundingBox: function () |
| 138 | + { |
| 139 | + var position = this.attributes['a_Position'] || this.attributes['position']; |
| 140 | + |
| 141 | + if (position.isInterleavedBufferAttribute) |
| 142 | + { |
| 143 | + var data = position.data; |
| 144 | + |
| 145 | + this.boundingBox.setFromArray(data.array, data.stride); |
| 146 | + } |
| 147 | + else |
| 148 | + { |
| 149 | + this.boundingBox.setFromArray(position.array, position.size); |
| 150 | + } |
| 151 | + }, |
| 152 | + |
| 153 | + /** |
| 154 | + * Computes bounding sphere of the geometry, updating `boundingSphere`. |
| 155 | + * |
| 156 | + * Bounding spheres aren't computed by default. They need to be explicitly computed. |
| 157 | + */ |
| 158 | + computeBoundingSphere: function () |
| 159 | + { |
| 160 | + var position = this.attributes['a_Position'] || this.attributes['position']; |
| 161 | + |
| 162 | + if (position.isInterleavedBufferAttribute) |
| 163 | + { |
| 164 | + var data = position.data; |
| 165 | + |
| 166 | + this.boundingSphere.setFromArray(data.array, data.stride); |
| 167 | + } |
| 168 | + else |
| 169 | + { |
| 170 | + this.boundingSphere.setFromArray(position.array, position.size); |
| 171 | + } |
| 172 | + }, |
| 173 | + |
| 174 | + /** |
| 175 | + * Disposes the object from memory. |
| 176 | + * You need to call this when you want the BufferGeometry removed while the application is running. |
| 177 | + */ |
| 178 | + dispose: function () |
| 179 | + { |
| 180 | + // TODO |
| 181 | + } |
| 182 | + |
| 183 | +}); |
| 184 | + |
| 185 | +module.exports = Geometry; |
0 commit comments