Spaces:
Running
Running
File size: 3,458 Bytes
2b7aae2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 | import { Uint16BufferAttribute, Uint32BufferAttribute } from '../../core/BufferAttribute.js';
import { arrayMax } from '../../utils.js';
function WebGLGeometries(gl, attributes, info, bindingStates) {
const geometries = {};
const wireframeAttributes = new WeakMap();
function onGeometryDispose(event) {
const geometry = event.target;
if (geometry.index !== null) {
attributes.remove(geometry.index);
}
for (const name in geometry.attributes) {
attributes.remove(geometry.attributes[name]);
}
geometry.removeEventListener('dispose', onGeometryDispose);
delete geometries[geometry.id];
const attribute = wireframeAttributes.get(geometry);
if (attribute) {
attributes.remove(attribute);
wireframeAttributes.delete(geometry);
}
bindingStates.releaseStatesOfGeometry(geometry);
if (geometry.isInstancedBufferGeometry === true) {
delete geometry._maxInstanceCount;
}
//
info.memory.geometries--;
}
function get(object, geometry) {
if (geometries[geometry.id] === true) return geometry;
geometry.addEventListener('dispose', onGeometryDispose);
geometries[geometry.id] = true;
info.memory.geometries++;
return geometry;
}
function update(geometry) {
const geometryAttributes = geometry.attributes;
// Updating index buffer in VAO now. See WebGLBindingStates.
for (const name in geometryAttributes) {
attributes.update(geometryAttributes[name], gl.ARRAY_BUFFER);
}
// morph targets
const morphAttributes = geometry.morphAttributes;
for (const name in morphAttributes) {
const array = morphAttributes[name];
for (let i = 0, l = array.length; i < l; i++) {
attributes.update(array[i], gl.ARRAY_BUFFER);
}
}
}
function updateWireframeAttribute(geometry) {
const indices = [];
const geometryIndex = geometry.index;
const geometryPosition = geometry.attributes.position;
let version = 0;
if (geometryIndex !== null) {
const array = geometryIndex.array;
version = geometryIndex.version;
for (let i = 0, l = array.length; i < l; i += 3) {
const a = array[i + 0];
const b = array[i + 1];
const c = array[i + 2];
indices.push(a, b, b, c, c, a);
}
} else {
const array = geometryPosition.array;
version = geometryPosition.version;
for (let i = 0, l = array.length / 3 - 1; i < l; i += 3) {
const a = i + 0;
const b = i + 1;
const c = i + 2;
indices.push(a, b, b, c, c, a);
}
}
const attribute = new (arrayMax(indices) > 65535 ? Uint32BufferAttribute : Uint16BufferAttribute)(indices, 1);
attribute.version = version;
// Updating index buffer in VAO now. See WebGLBindingStates
//
const previousAttribute = wireframeAttributes.get(geometry);
if (previousAttribute) attributes.remove(previousAttribute);
//
wireframeAttributes.set(geometry, attribute);
}
function getWireframeAttribute(geometry) {
const currentAttribute = wireframeAttributes.get(geometry);
if (currentAttribute) {
const geometryIndex = geometry.index;
if (geometryIndex !== null) {
// if the attribute is obsolete, create a new one
if (currentAttribute.version < geometryIndex.version) {
updateWireframeAttribute(geometry);
}
}
} else {
updateWireframeAttribute(geometry);
}
return wireframeAttributes.get(geometry);
}
return {
get: get,
update: update,
getWireframeAttribute: getWireframeAttribute,
};
}
export { WebGLGeometries };
|