[ts][threejs] Prefill normals at construction; they are always constant and perpendicular. See #2952.

This commit is contained in:
Davide Tantillo 2025-10-20 17:39:42 +02:00
parent 2a73be09b7
commit 6e7727c836

View File

@ -27,11 +27,10 @@
* THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*****************************************************************************/
import * as THREE from "three"
import { ThreeJsTexture, ThreeBlendOptions } from "./ThreeJsTexture.js";
import { BlendMode } from "@esotericsoftware/spine-core";
import * as THREE from "three"
import { SkeletonMesh } from "./SkeletonMesh.js";
import { ThreeBlendOptions, ThreeJsTexture } from "./ThreeJsTexture.js";
export type MaterialWithMap = THREE.Material & { map: THREE.Texture | null };
export class MeshBatcher extends THREE.Mesh {
@ -59,19 +58,35 @@ export class MeshBatcher extends THREE.Mesh {
this.vertexSize += 3;
}
let vertices = this.vertices = new Float32Array(maxVertices * this.vertexSize);
let indices = this.indices = new Uint16Array(maxVertices * 3);
let geo = new THREE.BufferGeometry();
let vertexBuffer = this.vertexBuffer = new THREE.InterleavedBuffer(vertices, this.vertexSize);
vertexBuffer.usage = WebGLRenderingContext.DYNAMIC_DRAW;
this.vertices = new Float32Array(maxVertices * this.vertexSize);
this.indices = new Uint16Array(maxVertices * 3);
const normals = new Float32Array(maxVertices * 3);
for (let i = 0; i < maxVertices * 3; i += 3) {
normals[i] = 0;
normals[i + 1] = 0;
normals[i + 2] = -1;
}
const vertexBuffer = new THREE.InterleavedBuffer(this.vertices, this.vertexSize)
this.vertexBuffer = vertexBuffer;
this.vertexBuffer.usage = WebGLRenderingContext.DYNAMIC_DRAW;
const geo = new THREE.BufferGeometry();
geo.setAttribute("position", new THREE.InterleavedBufferAttribute(vertexBuffer, 3, 0, false));
geo.setAttribute("color", new THREE.InterleavedBufferAttribute(vertexBuffer, 4, 3, false));
geo.setAttribute("uv", new THREE.InterleavedBufferAttribute(vertexBuffer, 2, 7, false));
if (twoColorTint) {
if (twoColorTint)
geo.setAttribute("darkcolor", new THREE.InterleavedBufferAttribute(vertexBuffer, 3, 9, false));
}
geo.setIndex(new THREE.BufferAttribute(indices, 1));
geo.getIndex()!.usage = WebGLRenderingContext.DYNAMIC_DRAW;
const normalBuffer = new THREE.BufferAttribute(normals, 3);
normalBuffer.usage = WebGLRenderingContext.STATIC_DRAW;
geo.setAttribute("normal", normalBuffer);
const indexBuffer = new THREE.BufferAttribute(this.indices, 1);
indexBuffer.usage = WebGLRenderingContext.DYNAMIC_DRAW;
geo.setIndex(indexBuffer);
geo.drawRange.start = 0;
geo.drawRange.count = 0;
this.geometry = geo;
@ -180,7 +195,6 @@ export class MeshBatcher extends THREE.Mesh {
index.addUpdateRange(0, this.indicesLength);
geo.drawRange.start = 0;
geo.drawRange.count = this.indicesLength;
geo.computeVertexNormals();
}
addMaterialGroup (indicesLength: number, materialGroup: number) {