mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-21 08:02:39 +08:00
modify builtin texture generation + particle unit test (#9841)
* modify builtin texture generation + particle unit test + particle system capacity issue 10417 * change texture format to RGBA8 * modify builtin texture generation * modify format and remove particle parts out to another pr * fix array buffer view generation
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
|
||||
import { UI_GPU_DRIVEN, TEST } from 'internal:constants';
|
||||
import { Asset } from '../assets/asset';
|
||||
import { ImageAsset } from '../assets/image-asset';
|
||||
import { ImageAsset, ImageSource } from '../assets/image-asset';
|
||||
import { SpriteFrame } from '../../2d/assets/sprite-frame';
|
||||
import { Texture2D } from '../assets/texture-2d';
|
||||
import { TextureCube } from '../assets/texture-cube';
|
||||
@@ -34,6 +34,7 @@ import { effects } from './effects';
|
||||
import { legacyCC } from '../global-exports';
|
||||
import { getDeviceShaderVersion } from '../renderer/core/program-lib';
|
||||
import shaderSourceAssembly from './shader-source-assembly';
|
||||
import { Color } from '../math';
|
||||
|
||||
class BuiltinResMgr {
|
||||
protected _device: Device | null = null;
|
||||
@@ -41,32 +42,150 @@ class BuiltinResMgr {
|
||||
|
||||
// this should be called after renderer initialized
|
||||
public initBuiltinRes (device: Device): Promise<void> {
|
||||
if (TEST) return Promise.resolve();
|
||||
this._device = device;
|
||||
const resources = this._resources;
|
||||
const canvas = document.createElement('canvas');
|
||||
const context = canvas.getContext('2d')!;
|
||||
const imgAsset = new ImageAsset(canvas);
|
||||
const l = canvas.width = canvas.height = 2;
|
||||
|
||||
const len = 2;
|
||||
const numChannels = 4;
|
||||
|
||||
const blackValueView = new Uint8Array(len * len * numChannels);
|
||||
const emptyValueView = new Uint8Array(len * len * numChannels);
|
||||
const greyValueView = new Uint8Array(len * len * numChannels);
|
||||
const whiteValueView = new Uint8Array(len * len * numChannels);
|
||||
const normalValueView = new Uint8Array(len * len * numChannels);
|
||||
const defaultValueView = new Uint8Array(len * len * numChannels);
|
||||
|
||||
const defaultSize = 16;
|
||||
const halfDefaultSize = defaultSize / 2;
|
||||
|
||||
let offset = 0;
|
||||
for (let i = 0; i < len * len; i++) {
|
||||
blackValueView[offset] = 0;
|
||||
blackValueView[offset + 1] = 0;
|
||||
blackValueView[offset + 2] = 0;
|
||||
blackValueView[offset + 3] = 255;
|
||||
|
||||
emptyValueView[offset] = 0;
|
||||
emptyValueView[offset + 1] = 0;
|
||||
emptyValueView[offset + 2] = 0;
|
||||
emptyValueView[offset + 3] = 0;
|
||||
|
||||
greyValueView[offset] = 119;
|
||||
greyValueView[offset + 1] = 119;
|
||||
greyValueView[offset + 2] = 119;
|
||||
greyValueView[offset + 3] = 255;
|
||||
|
||||
whiteValueView[offset] = 255;
|
||||
whiteValueView[offset + 1] = 255;
|
||||
whiteValueView[offset + 2] = 255;
|
||||
whiteValueView[offset + 3] = 255;
|
||||
|
||||
normalValueView[offset] = 127;
|
||||
normalValueView[offset + 1] = 127;
|
||||
normalValueView[offset + 2] = 255;
|
||||
normalValueView[offset + 3] = 255;
|
||||
|
||||
offset += numChannels;
|
||||
}
|
||||
|
||||
offset = 0;
|
||||
for (let i = 0; i < defaultSize * defaultSize; i++) {
|
||||
defaultValueView[offset] = 221;
|
||||
defaultValueView[offset + 1] = 221;
|
||||
defaultValueView[offset + 2] = 221;
|
||||
defaultValueView[offset + 3] = 255;
|
||||
|
||||
offset += numChannels;
|
||||
}
|
||||
offset = 0;
|
||||
for (let i = 0; i < halfDefaultSize; i++) {
|
||||
for (let j = 0; j < halfDefaultSize; j++) {
|
||||
defaultValueView[offset] = 85;
|
||||
defaultValueView[offset + 1] = 85;
|
||||
defaultValueView[offset + 2] = 85;
|
||||
defaultValueView[offset + 3] = 255;
|
||||
|
||||
offset += numChannels;
|
||||
}
|
||||
offset += halfDefaultSize * numChannels;
|
||||
}
|
||||
offset += halfDefaultSize * numChannels;
|
||||
for (let i = 0; i < halfDefaultSize; i++) {
|
||||
for (let j = 0; j < halfDefaultSize; j++) {
|
||||
defaultValueView[offset] = 85;
|
||||
defaultValueView[offset + 1] = 85;
|
||||
defaultValueView[offset + 2] = 85;
|
||||
defaultValueView[offset + 3] = 255;
|
||||
|
||||
offset += numChannels;
|
||||
}
|
||||
offset += halfDefaultSize * numChannels;
|
||||
}
|
||||
|
||||
const blackMemImageSource: ImageSource = {
|
||||
width: len,
|
||||
height: len,
|
||||
_data: blackValueView,
|
||||
_compressed: false,
|
||||
format: Texture2D.PixelFormat.RGBA8888,
|
||||
};
|
||||
|
||||
const emptyMemImageSource: ImageSource = {
|
||||
width: len,
|
||||
height: len,
|
||||
_data: emptyValueView,
|
||||
_compressed: false,
|
||||
format: Texture2D.PixelFormat.RGBA8888,
|
||||
};
|
||||
|
||||
const greyMemImageSource: ImageSource = {
|
||||
width: len,
|
||||
height: len,
|
||||
_data: greyValueView,
|
||||
_compressed: false,
|
||||
format: Texture2D.PixelFormat.RGBA8888,
|
||||
};
|
||||
|
||||
const whiteMemImageSource: ImageSource = {
|
||||
width: len,
|
||||
height: len,
|
||||
_data: whiteValueView,
|
||||
_compressed: false,
|
||||
format: Texture2D.PixelFormat.RGBA8888,
|
||||
};
|
||||
|
||||
const normalMemImageSource: ImageSource = {
|
||||
width: len,
|
||||
height: len,
|
||||
_data: normalValueView,
|
||||
_compressed: false,
|
||||
format: Texture2D.PixelFormat.RGBA8888,
|
||||
};
|
||||
|
||||
const defaultMemImageSource: ImageSource = {
|
||||
width: defaultSize,
|
||||
height: defaultSize,
|
||||
_data: defaultValueView,
|
||||
_compressed: false,
|
||||
format: Texture2D.PixelFormat.RGBA8888,
|
||||
};
|
||||
|
||||
// ============================
|
||||
// builtin textures
|
||||
// ============================
|
||||
|
||||
// black texture
|
||||
context.fillStyle = '#000';
|
||||
context.fillRect(0, 0, l, l);
|
||||
const imgAsset = new ImageAsset(blackMemImageSource);
|
||||
const blackTexture = new Texture2D();
|
||||
blackTexture._uuid = 'black-texture';
|
||||
blackTexture.image = imgAsset;
|
||||
resources[blackTexture._uuid] = blackTexture;
|
||||
|
||||
// empty texture
|
||||
context.fillStyle = 'rgba(0,0,0,0)';
|
||||
context.fillRect(0, 0, l, l);
|
||||
const emptyImgAsset = new ImageAsset(emptyMemImageSource);
|
||||
const emptyTexture = new Texture2D();
|
||||
emptyTexture._uuid = 'empty-texture';
|
||||
emptyTexture.image = imgAsset;
|
||||
emptyTexture.image = emptyImgAsset;
|
||||
resources[emptyTexture._uuid] = emptyTexture;
|
||||
|
||||
// black texture
|
||||
@@ -74,29 +193,27 @@ class BuiltinResMgr {
|
||||
blackCubeTexture._uuid = 'black-cube-texture';
|
||||
blackCubeTexture.setMipFilter(TextureCube.Filter.NEAREST);
|
||||
blackCubeTexture.image = {
|
||||
front: new ImageAsset(canvas),
|
||||
back: new ImageAsset(canvas),
|
||||
left: new ImageAsset(canvas),
|
||||
right: new ImageAsset(canvas),
|
||||
top: new ImageAsset(canvas),
|
||||
bottom: new ImageAsset(canvas),
|
||||
front: new ImageAsset(blackMemImageSource),
|
||||
back: new ImageAsset(blackMemImageSource),
|
||||
left: new ImageAsset(blackMemImageSource),
|
||||
right: new ImageAsset(blackMemImageSource),
|
||||
top: new ImageAsset(blackMemImageSource),
|
||||
bottom: new ImageAsset(blackMemImageSource),
|
||||
};
|
||||
resources[blackCubeTexture._uuid] = blackCubeTexture;
|
||||
|
||||
// grey texture
|
||||
context.fillStyle = '#777';
|
||||
context.fillRect(0, 0, l, l);
|
||||
const greyImgAsset = new ImageAsset(greyMemImageSource);
|
||||
const greyTexture = new Texture2D();
|
||||
greyTexture._uuid = 'grey-texture';
|
||||
greyTexture.image = imgAsset;
|
||||
greyTexture.image = greyImgAsset;
|
||||
resources[greyTexture._uuid] = greyTexture;
|
||||
|
||||
// white texture
|
||||
context.fillStyle = '#fff';
|
||||
context.fillRect(0, 0, l, l);
|
||||
const whiteImgAsset = new ImageAsset(whiteMemImageSource);
|
||||
const whiteTexture = new Texture2D();
|
||||
whiteTexture._uuid = 'white-texture';
|
||||
whiteTexture.image = imgAsset;
|
||||
whiteTexture.image = whiteImgAsset;
|
||||
resources[whiteTexture._uuid] = whiteTexture;
|
||||
|
||||
// white cube texture
|
||||
@@ -104,34 +221,27 @@ class BuiltinResMgr {
|
||||
whiteCubeTexture._uuid = 'white-cube-texture';
|
||||
whiteCubeTexture.setMipFilter(TextureCube.Filter.NEAREST);
|
||||
whiteCubeTexture.image = {
|
||||
front: new ImageAsset(canvas),
|
||||
back: new ImageAsset(canvas),
|
||||
left: new ImageAsset(canvas),
|
||||
right: new ImageAsset(canvas),
|
||||
top: new ImageAsset(canvas),
|
||||
bottom: new ImageAsset(canvas),
|
||||
front: new ImageAsset(whiteMemImageSource),
|
||||
back: new ImageAsset(whiteMemImageSource),
|
||||
left: new ImageAsset(whiteMemImageSource),
|
||||
right: new ImageAsset(whiteMemImageSource),
|
||||
top: new ImageAsset(whiteMemImageSource),
|
||||
bottom: new ImageAsset(whiteMemImageSource),
|
||||
};
|
||||
resources[whiteCubeTexture._uuid] = whiteCubeTexture;
|
||||
|
||||
// normal texture
|
||||
context.fillStyle = '#7f7fff';
|
||||
context.fillRect(0, 0, l, l);
|
||||
const normalImgAsset = new ImageAsset(normalMemImageSource);
|
||||
const normalTexture = new Texture2D();
|
||||
normalTexture._uuid = 'normal-texture';
|
||||
normalTexture.image = imgAsset;
|
||||
normalTexture.image = normalImgAsset;
|
||||
resources[normalTexture._uuid] = normalTexture;
|
||||
|
||||
// default texture
|
||||
canvas.width = canvas.height = 16;
|
||||
context.fillStyle = '#ddd';
|
||||
context.fillRect(0, 0, 16, 16);
|
||||
context.fillStyle = '#555';
|
||||
context.fillRect(0, 0, 8, 8);
|
||||
context.fillStyle = '#555';
|
||||
context.fillRect(8, 8, 8, 8);
|
||||
const defaultImgAsset = new ImageAsset(defaultMemImageSource);
|
||||
const defaultTexture = new Texture2D();
|
||||
defaultTexture._uuid = 'default-texture';
|
||||
defaultTexture.image = imgAsset;
|
||||
defaultTexture.image = defaultImgAsset;
|
||||
resources[defaultTexture._uuid] = defaultTexture;
|
||||
|
||||
// default cube texture
|
||||
@@ -139,12 +249,12 @@ class BuiltinResMgr {
|
||||
defaultCubeTexture.setMipFilter(TextureCube.Filter.NEAREST);
|
||||
defaultCubeTexture._uuid = 'default-cube-texture';
|
||||
defaultCubeTexture.image = {
|
||||
front: new ImageAsset(canvas),
|
||||
back: new ImageAsset(canvas),
|
||||
left: new ImageAsset(canvas),
|
||||
right: new ImageAsset(canvas),
|
||||
top: new ImageAsset(canvas),
|
||||
bottom: new ImageAsset(canvas),
|
||||
front: new ImageAsset(defaultMemImageSource),
|
||||
back: new ImageAsset(defaultMemImageSource),
|
||||
left: new ImageAsset(defaultMemImageSource),
|
||||
right: new ImageAsset(defaultMemImageSource),
|
||||
top: new ImageAsset(defaultMemImageSource),
|
||||
bottom: new ImageAsset(defaultMemImageSource),
|
||||
};
|
||||
resources[defaultCubeTexture._uuid] = defaultCubeTexture;
|
||||
|
||||
@@ -359,11 +469,13 @@ class BuiltinResMgr {
|
||||
materialsToBeCompiled.push(defaultParticleMtl);
|
||||
|
||||
// default particle gpu material
|
||||
const defaultParticleGPUMtl = new legacyCC.Material();
|
||||
defaultParticleGPUMtl._uuid = 'default-particle-gpu-material';
|
||||
defaultParticleGPUMtl.initialize({ effectName: 'particle-gpu' });
|
||||
resources[defaultParticleGPUMtl._uuid] = defaultParticleGPUMtl;
|
||||
materialsToBeCompiled.push(defaultParticleGPUMtl);
|
||||
if (!TEST) {
|
||||
const defaultParticleGPUMtl = new legacyCC.Material();
|
||||
defaultParticleGPUMtl._uuid = 'default-particle-gpu-material';
|
||||
defaultParticleGPUMtl.initialize({ effectName: 'particle-gpu' });
|
||||
resources[defaultParticleGPUMtl._uuid] = defaultParticleGPUMtl;
|
||||
materialsToBeCompiled.push(defaultParticleGPUMtl);
|
||||
}
|
||||
|
||||
// default particle material
|
||||
const defaultTrailMtl = new legacyCC.Material();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { HTML5, MINIGAME, RUNTIME_BASED, WECHAT } from 'internal:constants';
|
||||
import { HTML5, MINIGAME, RUNTIME_BASED, WECHAT, TEST } from 'internal:constants';
|
||||
import { glsl1 } from './shader-sources/glsl1';
|
||||
import { glsl3 } from './shader-sources/glsl3';
|
||||
import { glsl4 } from './shader-sources/glsl4';
|
||||
@@ -11,7 +11,13 @@ type ShaderSource = Record<string, string>[][];
|
||||
* The shader sources assembled in this build.
|
||||
*/
|
||||
const assembly: Partial<Record<ShaderVersion, ShaderSource>> = (() => {
|
||||
if (HTML5 || WECHAT || RUNTIME_BASED) {
|
||||
if (TEST) {
|
||||
return {
|
||||
glsl1,
|
||||
glsl3,
|
||||
glsl4,
|
||||
};
|
||||
} else if (HTML5 || WECHAT || RUNTIME_BASED) {
|
||||
return {
|
||||
glsl1,
|
||||
glsl3,
|
||||
|
||||
@@ -937,20 +937,6 @@ export class Game extends EventTarget {
|
||||
}
|
||||
|
||||
private _setRenderPipelineNShowSplash () {
|
||||
// The test environment does not currently support the renderer
|
||||
if (TEST) {
|
||||
return Promise.resolve((() => {
|
||||
this._rendererInitialized = true;
|
||||
this._safeEmit(Game.EVENT_RENDERER_INITED);
|
||||
this._inited = true;
|
||||
this._setAnimFrame();
|
||||
this._runMainLoop();
|
||||
this._safeEmit(Game.EVENT_GAME_INITED);
|
||||
if (this.onStart) {
|
||||
this.onStart();
|
||||
}
|
||||
})());
|
||||
}
|
||||
return Promise.resolve(this._setupRenderPipeline()).then(
|
||||
() => Promise.resolve(this._showSplashScreen()).then(
|
||||
() => {
|
||||
|
||||
@@ -115,6 +115,7 @@ export default class ParticleBatchModel extends scene.Model {
|
||||
this._vertCount = this._mesh.struct.vertexBundles[this._mesh.struct.primitives[0].vertexBundelIndices[0]].view.count;
|
||||
this._indexCount = this._mesh.struct.primitives[0].indexView!.count;
|
||||
}
|
||||
|
||||
const vertexBuffer = this._device.createBuffer(new BufferInfo(
|
||||
BufferUsageBit.VERTEX | BufferUsageBit.TRANSFER_DST,
|
||||
MemoryUsageBit.HOST | MemoryUsageBit.DEVICE,
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import { director, Node, Scene } from "../../cocos/core";
|
||||
import { legacyCC } from "../../cocos/core/global-exports";
|
||||
import { ParticleSystem } from "../../exports/particle";
|
||||
|
||||
test('particle system capacity test', () => {
|
||||
});
|
||||
|
||||
// Will be updated later with the fix on particle capacity
|
||||
// test('particle system capacity test', function () {
|
||||
// const scene = new Scene('test');
|
||||
// director.runSceneImmediate(scene);
|
||||
|
||||
// const temp0 = new Node();
|
||||
// scene.addChild(temp0);
|
||||
|
||||
// const particle = temp0.addComponent(ParticleSystem) as ParticleSystem;
|
||||
|
||||
// particle.capacity = 0;
|
||||
// particle.renderer.useGPU = false;
|
||||
|
||||
// legacyCC.game.step();
|
||||
|
||||
// // @ts-expect-error
|
||||
// expect(!!particle.processor.getModel()._vBuffer).toBe(true);
|
||||
// });
|
||||
Reference in New Issue
Block a user