Skip to content

Commit

Permalink
fix Skeleton's opacity can not work normally (cocos#17497)
Browse files Browse the repository at this point in the history
* fix Skeleton's opacity can not work normally
  • Loading branch information
bofeng-song authored Aug 6, 2024
1 parent a9e4d0d commit 18db6ca
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 6 deletions.
7 changes: 4 additions & 3 deletions cocos/spine/assembler/simple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { legacyCC } from '../../core/global-exports';
import { RenderData } from '../../2d/renderer/render-data';
import { director } from '../../game';
import spine from '../lib/spine-core.js';
import { Color, Vec3 } from '../../core';
import { Color, EPSILON, Vec3 } from '../../core';
import { MaterialInstance } from '../../render-scene';

const _slotColor = new Color(0, 0, 255, 255);
Expand Down Expand Up @@ -279,11 +279,12 @@ function cacheTraverse (comp: Skeleton): void {
vUint8Buf.set(model.vData as TypedArray);

const nodeColor = comp.color;
if (Color.toUint32(nodeColor) !== 0xffffffff || _premultipliedAlpha) {
const opacity = comp.node._uiProps.opacity;
if ((1 - opacity) > EPSILON || Color.toUint32(nodeColor) !== 0xffffffff || _premultipliedAlpha) {
_nodeR = nodeColor.r / 255;
_nodeG = nodeColor.g / 255;
_nodeB = nodeColor.b / 255;
_nodeA = nodeColor.a / 255;
_nodeA = nodeColor.a * opacity / 255;
for (let i = 0; i < vc; i++) {
const index = i * _byteStrideTwoColor + 5 * Float32Array.BYTES_PER_ELEMENT;
const R = vUint8Buf[index];
Expand Down
2 changes: 1 addition & 1 deletion cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1665,7 +1665,7 @@ export class Skeleton extends UIRenderer {
const r = this._color.r / 255.0;
const g = this._color.g / 255.0;
const b = this._color.b / 255.0;
this._instance!.setColor(r, g, b, a);
this._instance!.setColor(r, g, b, a * this._color.a / 255.0);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ void SkeletonCacheAnimation::render(float /*dt*/) {
}

auto handleColor = [&](SkeletonCache::ColorData *colorData) {
tempA = colorData->finalColor.a * _nodeColor.a;
tempA = colorData->finalColor.a * _nodeColor.a * _entity->getOpacity();
multiplier = _premultipliedAlpha ? tempA / 255 : 1;
tempR = _nodeColor.r * multiplier;
tempG = _nodeColor.g * multiplier;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ void SkeletonRenderer::render(float /*deltaTime*/) {
continue;
}

color.a = _skeleton->getColor().a * slot->getColor().a * color.a * _nodeColor.a * 255;
color.a = _skeleton->getColor().a * slot->getColor().a * color.a * _nodeColor.a * _entity->getOpacity() * 255;
// skip rendering if the color of this attachment is 0
if (color.a == 0) {
_clipper->clipEnd(*slot);
Expand Down

0 comments on commit 18db6ca

Please sign in to comment.