mirror of
https://github.com/cocos/cocos-engine.git
synced 2026-09-07 08:09:13 +08:00
102 lines
4.0 KiB
TypeScript
102 lines
4.0 KiB
TypeScript
/*
|
|
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
|
|
|
|
https://www.cocos.com/
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
*/
|
|
|
|
import { Color, Mat4, Vec3 } from '../../core/math';
|
|
import { RenderData } from '../renderer/render-data';
|
|
import { IBatcher } from '../renderer/i-batcher';
|
|
import { Node } from '../../core/scene-graph/node';
|
|
import { FormatInfos } from '../../core/gfx';
|
|
import { clamp } from '../../core';
|
|
|
|
const vec3_temp = new Vec3();
|
|
const _worldMatrix = new Mat4();
|
|
|
|
export function fillMeshVertices3D (node: Node, renderer: IBatcher, renderData: RenderData, color: Color) {
|
|
const chunk = renderData.chunk;
|
|
const dataList = renderData.data;
|
|
const vData = chunk.vb;
|
|
const vertexCount = renderData.vertexCount;
|
|
|
|
node.getWorldMatrix(_worldMatrix);
|
|
|
|
let vertexOffset = 0;
|
|
for (let i = 0; i < vertexCount; i++) {
|
|
const vert = dataList[i];
|
|
Vec3.set(vec3_temp, vert.x, vert.y, 0);
|
|
Vec3.transformMat4(vec3_temp, vec3_temp, _worldMatrix);
|
|
vData[vertexOffset++] = vec3_temp.x;
|
|
vData[vertexOffset++] = vec3_temp.y;
|
|
vData[vertexOffset++] = vec3_temp.z;
|
|
Color.toArray(vData, color, vertexOffset + 2);
|
|
vertexOffset += 6;
|
|
}
|
|
|
|
// fill index data
|
|
const bid = chunk.bufferId;
|
|
const vid = chunk.vertexOffset;
|
|
const meshBuffer = chunk.vertexAccessor.getMeshBuffer(chunk.bufferId);
|
|
const ib = chunk.vertexAccessor.getIndexBuffer(bid);
|
|
let indexOffset = meshBuffer.indexOffset;
|
|
for (let i = 0, count = vertexCount / 4; i < count; i++) {
|
|
const start = vid + i * 4;
|
|
ib[indexOffset++] = start;
|
|
ib[indexOffset++] = start + 1;
|
|
ib[indexOffset++] = start + 2;
|
|
ib[indexOffset++] = start + 1;
|
|
ib[indexOffset++] = start + 3;
|
|
ib[indexOffset++] = start + 2;
|
|
}
|
|
meshBuffer.indexOffset += renderData.indexCount;
|
|
meshBuffer.setDirty();
|
|
}
|
|
|
|
export function updateOpacity (renderData: RenderData, opacity: number) {
|
|
const vfmt = renderData.vertexFormat;
|
|
const vb = renderData.chunk.vb;
|
|
let attr; let format; let stride;
|
|
// Color component offset
|
|
let offset = 0;
|
|
for (let i = 0; i < vfmt.length; ++i) {
|
|
attr = vfmt[i];
|
|
format = FormatInfos[attr.format];
|
|
if (format.hasAlpha) {
|
|
stride = renderData.floatStride;
|
|
if (format.size / format.count === 1) {
|
|
const alpha = ~~clamp(Math.round(opacity * 255), 0, 255);
|
|
// Uint color RGBA8
|
|
for (let color = offset; color < vb.length; color += stride) {
|
|
vb[color] = ((vb[color] & 0xffffff00) | alpha) >>> 0;
|
|
}
|
|
} else if (format.size / format.count === 4) {
|
|
// RGBA32 color, alpha at position 3
|
|
for (let alpha = offset + 3; alpha < vb.length; alpha += stride) {
|
|
vb[alpha] = opacity;
|
|
}
|
|
}
|
|
}
|
|
offset += format.size >> 2;
|
|
}
|
|
}
|