2010-12-28 10:23:14 +08:00
|
|
|
/****************************************************************************
|
2011-03-19 10:07:16 +08:00
|
|
|
Copyright (c) 2008-2010 Ricardo Quesada
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2011-07-04 19:20:16 +08:00
|
|
|
Copyright (c) 2011 Zynga Inc.
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-07-12 22:57:45 +08:00
|
|
|
Copyright (c) 2021-2022 Bytedance Inc.
|
2010-12-28 10:23:14 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2010-12-28 10:23:14 +08:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
2011-03-19 10:07:16 +08:00
|
|
|
|
|
|
|
// ideas taken from:
|
2012-04-19 14:35:52 +08:00
|
|
|
// . The ocean spray in your face [Jeff Lander]
|
|
|
|
// http://www.double.co.nz/dust/col0798.pdf
|
|
|
|
// . Building an Advanced Particle System [John van der Burg]
|
|
|
|
// http://www.gamasutra.com/features/20000623/vanderburg_01.htm
|
2011-03-19 10:07:16 +08:00
|
|
|
// . LOVE game engine
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://love2d.org/
|
2011-03-19 10:07:16 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Radius mode support, from 71 squared
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://particledesigner.71squared.com/
|
2011-03-19 10:07:16 +08:00
|
|
|
//
|
|
|
|
// IMPORTANT: Particle Designer is supported by cocos2d, but
|
2012-09-17 15:02:24 +08:00
|
|
|
// 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
|
2021-12-25 10:04:45 +08:00
|
|
|
// cocos2d uses a another approach, but the results are almost identical.
|
2011-03-19 10:07:16 +08:00
|
|
|
//
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "2d/CCParticleSystem.h"
|
2013-07-25 13:54:32 +08:00
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "2d/CCParticleBatchNode.h"
|
|
|
|
#include "renderer/CCTextureAtlas.h"
|
2014-04-30 08:37:36 +08:00
|
|
|
#include "base/ZipUtils.h"
|
|
|
|
#include "base/CCDirector.h"
|
2016-06-06 16:25:00 +08:00
|
|
|
#include "base/CCProfiling.h"
|
2016-06-15 15:01:26 +08:00
|
|
|
#include "base/ccUTF8.h"
|
metal support for cocos2d-x (#19305)
* remove deprecated files
* remove some deprecated codes
* remove more deprecated codes
* remove ui deprecated codes
* remove more deprecated codes
* remove deprecated codes in ccmenuitem
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes in ui
* remove more deprecated codes
* remove more deprecated codes
* remove more deprecated codes
* remove vr related codes and ignore some modules
* remove allocator
* remove some config
* 【Feature】add back-end project file
* [Feature] add back-end file
* add pipeline descriptor and shader cache
* [Feature] support sprite for backend
* [Feature] remove unneeded code
* [Feature] according to es2.0 spec, you must use clamp-to-edge as texture wrap mode, and no mipmapping for non-power-of-two texture
* [Feature] set texture wrap mode to clamp-to-edge, and no mipmapping for non-power-of-two texture
* [Feature] remove macro define to .cpp file
* [Feature] add log info
* [Feature] add PipelineDescriptor for TriangleCommand
* [Feature] add PipelineDescriptor object as member of TriangleCommand
* [Feature] add getPipelineDescriptor method
* add renderbackend
* complete pipeline descriptor
* [Feature] add viewport in RenderCommand
* set viewport when rendrering
* [Feature] occur error when using RendererBackend, to be fixed.
* a workaround to fix black screen on macOS 10.14 (#19090)
* add rendererbackend init function
* fix typo
* [Feature] modify testFile
* [BugFix] modify shader path
* [Feature] set default viewport
* fix projection
* [Feature] modify log info
* [BugFix] change viewport data type to int
* [BugFix] add BindGroup to PipelienDescriptor
* [BugFix] change a_position to vec3 in sprite.vert
* [BugFix] set vertexLayout according to V3F_C4B_T2F structure
* [Feature] revert a_position to vec4
* [Feature] renderer should not use gl codes directly
* [Feature] it's better not use default value parameter
* fix depth test setting
* rendererbackend -> renderer
* clear color and depth at begin
* add metal backend
* metal support normalized attribute
* simplify codes
* update external
* add render pass desctriptor in pipeline descriptor
* fix warnings
* fix crash and memeory leak
* refactor Texture2D
* put pipeline descriptor into render command
* simplify codes
* [Feature] update Sprite
* fix crash when closing app
* [Feature] update SpriteBatchNode and TextureAtlas
* support render texture(not finish)
* [Feature] remove unused code
* make tests work on mac
* fix download-deps path error
* make tests work on iOS
* [Feature] support ttf under normal label effect
* refactor triangle command processing
* let renderer handle more common commands
* refactor backend
* make render texture work
* [Feature] refactor backend for GL
* [Feature]Renaming to make it easy to understand
* [Feature] change warp mode to CLAMP_TO_EDGE
* fix ghost
* simplify visit render queue logic
* support progress timer without rial mode
* support partcile system
* Feature/update label (#149)
* [BugFix] fix compile error
* [Feature] support outline effect in ios
* [Feature] add shader file
* [BugFix] fix begin and end RenderPass
* [Feature] update CustomCommand
* [Feature] revert project.pbxproj
* [Feature] simplify codes
* [BugFix] pack AI88 to RGBA8888 only when outline enable
* [Feature] support shadow effect in Label
* [Feature] support BMFont
* [Feature] support glow effect
* [Feature] simplify shader files
* LabelAtlas work
* handle blend function correctly
* support tile map
* don't share buffer in metal
* alloc buffer size as needed
* support more tilemap
* Merge branch 'minggo/metal-support' into feature/updateLabel
* minggo/metal-support:
support tile map
handle blend function correctly
LabelAtlas work
Feature/update label (#149)
support partcile system
# Conflicts:
# cocos/2d/CCLabel.cpp
# cocos/2d/CCSprite.cpp
# cocos/2d/CCSpriteBatchNode.cpp
# cocos/renderer/CCQuadCommand.cpp
# cocos/renderer/CCQuadCommand.h
* render texture work without saving file
* use global viewport
* grid3d works
* remove grabber
* tiled3d works
* [BugFix] fix label bug
* [Feature] add updateSubData for buffer
* [Feature] remove setVertexCount
* support depth test
* add callback command
* [Feature] add UITest
* [Feature] update UITest
* [Feature] remove unneeded codes
* fix custom command issue
* fix layer color blend issue
* [BugFix] fix iOS compile error
* [Feature] remove unneeded codes
* [Feature] fix updateVertexBuffer
* layerradial works
* add draw test back
* fix batch issue
* fix compiling error
* [BugFix] support ETC1
* [BugFix] get the correct pipelineDescriptor
* [BugFix] skip draw when backendTexture nullptr
* clipping node support
* [Feature] add shader files
* fix stencil issue in metal
* [Feature] update UILayoutTest
* [BugFix] skip drawing when vertexCount is zero
* refactor renderer
* add set global z order for stencil manager commands
* fix warnings caused by type
* remove viewport in render command
* [Feature] fix warnings caused by type
* [BugFix] clear vertexCount and indexCount for CustomComand when needed
* [Feature] update clear for CustomCommand
* ios use metal
* fix viewport issue
* fix LayerColorGradient crash
* [cmake] transport to android and windows (#160)
* save point 1
* compile on windows
* run on android
* revert useless change
* android set CC_ENABLE_CACHE_TEXTURE_DATA to 1
* add initGlew
* fix android crash
* add TODO new-renderer
* review update
* revert onGLFWWindowPosCallback
* fix android compiling error
* Impl progress radial (#162)
* progresstimer add radial impl
* default drawType to element
* dec invoke times of createVertexBuffer (#163)
* support depth/stencil format for gl backend
* simplify progress timer codes
* support motionstreak, effect is wrong
* fix motionstreak issue
* [Feature] update Scissor Test (#161)
* [Feature] update Scissor Test
* [Feature] update ScissorTest
* [Feature] rename function
* [Feature] get constant reference if needed
* [Feature] show render status (#164)
* improve performance
* fix depth state
* fill error that triangle vertex/index number bigger than buffer
* fix compiline error in release mode
* fix buffer conflict between CPU and GPU on iOS/macOS
* Renderer refactor (#165)
* use one vertes/index buffer with opengl
* fix error on windows
* custom command support index format config
* CCLayer: compact vertex data structure
* update comment
* fix doc
* support fast tilemap
* pass index format instead
* fix some wrong effect
* fix render texture error
* fix texture per-element size
* fix texture format error
* BlendFunc type refactor, GLenum -> backend::BlendFactor (#167)
* BlendFunc use backend::BlendFactor as inner field
* update comments
* use int to replace GLenum
* update xcode project fiel
* rename to GLBlendConst
* add ccConstants.h
* update xcode project file
* update copyright
* remove primitive command
* remove CCPrimitive.cpp/.h
* remove deprecated files
* remove unneeded files
* remove multiple view support
* remove multiple view support
* remove the usage of frame buffer in camera
* director don't use frame buffer
* remove FrameBuffer
* remove BatchCommand
* add some api reference
* add physics2d back
* fix crash when close app on mac
* improve render texture
* fix rendertexture issue
* fix rendertexture issue
* simplify codes
* CMake support for mac & ios (#169)
* update cmake
* fix compile error
* update 3rd libs version
* remove CCThread.h/.cpp
* remove ccthread
* use audio engine to implement simple audio engine
* remove unneeded codes
* remove deprecated codes
* remove winrt macro
* remove CC_USE_WIC
* set partcile blend function in more elegant way
* remove unneeded codes
* remove unneeded codes
* cmake works on windows
* update project setting
* improve performance
* GLFloat -> float
* sync v3 cmake improvements into metal-support (#172)
* pick: modern cmake, compile definitions improvement (#19139)
* modern cmake, use target_compile_definitions partly
* simplify macro define, remove USE_*
* modern cmake, macro define
* add physics 2d macro define into ccConfig.h
* remove USE_CHIPMUNK macro in build.gradle
* remove CocosSelectModule.cmake
* shrink useless define
* simplify compile options config, re-add if necessary
* update external for tmp CI test
* un-quote target_compile_options value
* add "-g" parameter only when debug mode
* keep single build type when generator Xcode & VS projecy
* update external for tmp CI tes
* add static_cast<char>(-1), fix -Wc++11-narrowing
* simplify win32 compile define
* not modify code, only improve compile options
# Conflicts:
# .gitignore
# cmake/Modules/CocosConfigDepend.cmake
# cocos/CMakeLists.txt
# external/config.json
# tests/cpp-tests/CMakeLists.txt
* modern cmake, improve cmake_compiler_flags (#19145)
* cmake_compiler_flags
* Fix typo
* Fix typo2
* Remove chanages from Android.mk
* correct lua template cmake build (#19149)
* don't add -Wno-deprecated into jsb target
* correct lua template cmake build
* fix win32 lua template compile error
* prevent cmake in-source-build friendly (#19151)
* pick: Copy resources to "Resources/" on win32 like in linux configuration
* add "/Z7" for cpp-tests on windows
* [cmake] fix iOS xcode property setting failed (#19208)
* fix iOS xcode property setting failed
* use search_depend_libs_recursive at dlls collect
* fix typo
* [cmake] add find_host_library into iOS toolchain file (#19230)
* pick: [lua android] use luajit & template cmake update (#19239)
* increase cmake stability , remove tests/CMakeLists.txt (#19261)
* cmake win32 Precompiled header (#19273)
* Precompiled header
* Fix
* Precompiled header for cocos
* Precompiled header jscocos2d
* Fix for COCOS2D_DEBUG is always 1 on Android (#19291)
Related #19289
* little build fix, tests cpp-tests works on mac
* sync v3 build related codes into metal-support (#173)
* strict initialization for std::array
* remove proj.win32 project configs
* modern cmake, cmake_cleanup_remove_unused_variables (#19146)
* Switch travis CI to xenial (#19207)
* Switch travis CI to xenial
* Remove language: android
* Set language: cpp
* Fix java problem
* Update sdkmanager
* Fix sdkmanger
* next sdkmanager fix
* Remove xenial from android
* revert to sdk-tools-{system}-3859397
* Remove linux cmake install
* Update before-install.sh
* Update .travis.yml
* Simplify install-deps-linux.sh, tested on Ubuntu 16.04 (#19212)
* Simplify install-deps-linux.sh
* Cleanup
* pick: install ninja
* update cocos2d-console submodule
* for metal-support alpha release, we only test cpp
* add HelloCpp into project(Cocos2d-x) for tmp test
* update extenal metal-support-4
* update uniform setting
* [Feature] update BindGroup
* [Feature] empty-test
* [Feature] cpp-test
* [Feature] fix GL compiler error
* [Feature] fix GL crash
* [Feature] empty-test
* [Feature] cpp-tests
* [feature] improve frameRate
* [feature] fix opengl compile error
* [feature] fix opengl compile error
* [BugFix] fix compute maxLocation error
* [Feature] update setting unifrom
* [Feature] fix namespace
* [Feature] remove unneeded code
* [Bugfix] fix project file
* [Feature] update review
* [texture2d] impl texture format support (#175)
* texture update
* update
* update texture
* commit
* compile on windows
* ddd
* rename
* rename methods
* no crash
* save gl
* save
* save
* rename
* move out pixel format convert functions
* metal crash
* update
* update android
* support gles compressed texture format
* support more compress format
* add more conversion methods
* ss
* save
* update conversion methods
* add PVRTC format support
* reformat
* add marco linux
* fix GL marcro
* pvrtc supported only by ios 8.0+
* remove unused cmake
* revert change
* refactor Texture2D::initWithData
* fix conversion log
* refactor Texture2D::initWithData
* remove some OpenGL constants for PVRTC
* add todo
* fix typo
* AutoTest works on mac/iOS by disable part cases, sync v3 bug fix (#174)
* review cpp-tests, and fix part issues on start auto test
* sync png format fix: Node:Particle3D abnormal texture effects #19204
* fix cpp-tests SpritePolygon crash, wrong png format (#19170)
* fix wrong png convert format from sRGB to Gray
* erase plist index if all frames was erased
* test_A8.png have I8 format, fix it
* [CCSpriteCache] allow re-add plist & add testcase (#19175)
* allow re-add plist & add testcase
* remove comments/rename method/update testcase
* fix isSpriteFramesWithFileLoaded & add testcase
* remove used variable
* remove unused variable
* fix double free issues when js/lua-tests exit on iOS (#19236)
* disable part cases, AutoTest works without crash on mac
* update cocos2dx files json, to test cocos new next
* fix spritecache plist parsing issue (#19269)
* [linux] Fix FileUtils::getContents with folder (#19157)
* fix FileUtils::getContents on linux/mac
* use stat.st_mode
* simplify
* [CCFileUtils] win32 getFileSize (#19176)
* win32 getFileSize
* fix stat
* [cpp test-Android]20:FileUtils/2 change title (#19197)
* sync #19200
* sync #19231
* [android lua] improve performance of lua loader (#19234)
* [lua] improve performance of lua loader
* remove cache fix
* Revert "fix spritecache plist parsing issue (#19269)"
This reverts commit f3a85ece4307a7b90816c34489d1ed2c8fd11baf.
* remove win32 project files ref in template.json
* add metal framework lnk ref into cpp template
* test on iOS, and disable part cases
* alBufferData instead of alBufferDataStatic for small audio file on Apple (#19227)
* changes AudioCache to use alBufferData instead of alBufferDataStatic
(also makes test 19 faster to trigger openal bugs faster)
The original problem: CrashIfClientProvidedBogusAudioBufferList
https://github.com/cocos2d/cocos2d-x/issues/18948
is not happening anymore, but there's still a not very frequent issue
that makes OpenAL crash with a call stack like this.
AudioCache::readDataTask > alBufferData > CleanUpDeadBufferList
It happes more frequently when the device is "cold", which means after
half an hour of not using the device (locked).
I could not find the actual source code for iOS OpenAL, so I used the
macOS versions:
https://opensource.apple.com/source/OpenAL/OpenAL-48.7/Source/OpenAL/oalImp.cpp.auto.html
They seem to use CAGuard.h to make sure the dead buffer list
has no threading issues. I'm worried because the CAGuard code I found
has macos and win32 define but no iOS, so I'm not sure. I guess the
iOS version is different and has the guard.
I could not find a place in the code that's unprotected by the locks
except the InitializeBufferMap() which should not be called more than
once from cocos, and there's a workaround in AudioEngine-impl for it.
I reduced the occurence of the CleanUpDeadBufferList crash by moving
the guard in ~AudioCache to cover the alDeleteBuffers call.
* remove hack method "setTimeout" on audio
* AutoTest works on iOS
* support set ios deployment target for root project
* enable all texture2d cases, since Jiang have fixed
* add CCTextureUtils to xcode project file (#176)
* add leak cases for SpriteFrameCache (#177)
* re-add SpriteFrameCache cases
* update template file json
* Update SpriteFrameCacheTest.cpp
* fix compiling error
2019-01-18 15:08:25 +08:00
|
|
|
#include "base/ccUtils.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTextureCache.h"
|
2014-08-29 16:44:20 +08:00
|
|
|
#include "platform/CCFileUtils.h"
|
2011-03-19 10:07:16 +08:00
|
|
|
|
2012-11-15 17:16:51 +08:00
|
|
|
using namespace std;
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2010-12-27 17:39:15 +08:00
|
|
|
// ideas taken from:
|
2012-04-19 14:35:52 +08:00
|
|
|
// . The ocean spray in your face [Jeff Lander]
|
|
|
|
// http://www.double.co.nz/dust/col0798.pdf
|
|
|
|
// . Building an Advanced Particle System [John van der Burg]
|
|
|
|
// http://www.gamasutra.com/features/20000623/vanderburg_01.htm
|
2010-12-27 17:39:15 +08:00
|
|
|
// . LOVE game engine
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://love2d.org/
|
2010-12-27 17:39:15 +08:00
|
|
|
//
|
|
|
|
//
|
|
|
|
// Radius mode support, from 71 squared
|
2012-04-19 14:35:52 +08:00
|
|
|
// http://particledesigner.71squared.com/
|
2010-12-27 17:39:15 +08:00
|
|
|
//
|
|
|
|
// IMPORTANT: Particle Designer is supported by cocos2d, but
|
2012-09-17 15:02:24 +08:00
|
|
|
// 'Radius Mode' in Particle Designer uses a fixed emit rate of 30 hz. Since that can't be guaranteed in cocos2d,
|
2021-12-25 10:04:45 +08:00
|
|
|
// cocos2d uses a another approach, but the results are almost identical.
|
2010-12-27 17:39:15 +08:00
|
|
|
//
|
|
|
|
|
2017-02-06 15:13:20 +08:00
|
|
|
inline void normalize_point(float x, float y, particle_point* out)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
float n = x * x + y * y;
|
|
|
|
// Already normalized.
|
|
|
|
if (n == 1.0f)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
n = sqrt(n);
|
|
|
|
// Too close to zero.
|
|
|
|
if (n < MATH_TOLERANCE)
|
|
|
|
return;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
n = 1.0f / n;
|
2015-10-14 13:24:38 +08:00
|
|
|
out->x = x * n;
|
|
|
|
out->y = y * n;
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
ParticleData::ParticleData()
|
|
|
|
{
|
|
|
|
memset(this, 0, sizeof(ParticleData));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ParticleData::init(int count)
|
|
|
|
{
|
|
|
|
maxCount = count;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-06-07 00:23:11 +08:00
|
|
|
posx = (float*)malloc(count * sizeof(float));
|
|
|
|
posy = (float*)malloc(count * sizeof(float));
|
|
|
|
startPosX = (float*)malloc(count * sizeof(float));
|
|
|
|
startPosY = (float*)malloc(count * sizeof(float));
|
|
|
|
colorR = (float*)malloc(count * sizeof(float));
|
|
|
|
colorG = (float*)malloc(count * sizeof(float));
|
|
|
|
colorB = (float*)malloc(count * sizeof(float));
|
|
|
|
colorA = (float*)malloc(count * sizeof(float));
|
|
|
|
deltaColorR = (float*)malloc(count * sizeof(float));
|
|
|
|
deltaColorG = (float*)malloc(count * sizeof(float));
|
|
|
|
deltaColorB = (float*)malloc(count * sizeof(float));
|
|
|
|
deltaColorA = (float*)malloc(count * sizeof(float));
|
|
|
|
|
|
|
|
size = (float*)malloc(count * sizeof(float));
|
|
|
|
deltaSize = (float*)malloc(count * sizeof(float));
|
|
|
|
rotation = (float*)malloc(count * sizeof(float));
|
|
|
|
staticRotation = (float*)malloc(count * sizeof(float));
|
|
|
|
deltaRotation = (float*)malloc(count * sizeof(float));
|
|
|
|
totalTimeToLive = (float*)malloc(count * sizeof(float));
|
|
|
|
timeToLive = (float*)malloc(count * sizeof(float));
|
|
|
|
atlasIndex = (unsigned int*)malloc(count * sizeof(unsigned int));
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
modeA.dirX = (float*)malloc(count * sizeof(float));
|
|
|
|
modeA.dirY = (float*)malloc(count * sizeof(float));
|
|
|
|
modeA.radialAccel = (float*)malloc(count * sizeof(float));
|
|
|
|
modeA.tangentialAccel = (float*)malloc(count * sizeof(float));
|
|
|
|
|
|
|
|
modeB.angle = (float*)malloc(count * sizeof(float));
|
|
|
|
modeB.degreesPerSecond = (float*)malloc(count * sizeof(float));
|
|
|
|
modeB.deltaRadius = (float*)malloc(count * sizeof(float));
|
|
|
|
modeB.radius = (float*)malloc(count * sizeof(float));
|
|
|
|
|
2022-05-23 01:51:56 +08:00
|
|
|
return posx && posy && startPosX && startPosY && colorR && colorG && colorB && colorA && deltaColorR &&
|
2022-05-26 20:56:56 +08:00
|
|
|
deltaColorG && deltaColorB && deltaColorA && size && deltaSize && rotation && staticRotation &&
|
|
|
|
deltaRotation && totalTimeToLive && timeToLive && atlasIndex && modeA.dirX && modeA.dirY &&
|
|
|
|
modeA.radialAccel && modeA.tangentialAccel && modeB.angle && modeB.degreesPerSecond && modeB.deltaRadius &&
|
|
|
|
modeB.radius;
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleData::release()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_FREE(posx);
|
|
|
|
AX_SAFE_FREE(posy);
|
|
|
|
AX_SAFE_FREE(startPosX);
|
|
|
|
AX_SAFE_FREE(startPosY);
|
|
|
|
AX_SAFE_FREE(colorR);
|
|
|
|
AX_SAFE_FREE(colorG);
|
|
|
|
AX_SAFE_FREE(colorB);
|
|
|
|
AX_SAFE_FREE(colorA);
|
|
|
|
AX_SAFE_FREE(deltaColorR);
|
|
|
|
AX_SAFE_FREE(deltaColorG);
|
|
|
|
AX_SAFE_FREE(deltaColorB);
|
|
|
|
AX_SAFE_FREE(deltaColorA);
|
|
|
|
AX_SAFE_FREE(hue);
|
|
|
|
AX_SAFE_FREE(sat);
|
|
|
|
AX_SAFE_FREE(val);
|
|
|
|
AX_SAFE_FREE(opacityFadeInDelta);
|
|
|
|
AX_SAFE_FREE(opacityFadeInLength);
|
|
|
|
AX_SAFE_FREE(scaleInDelta);
|
|
|
|
AX_SAFE_FREE(scaleInLength);
|
|
|
|
AX_SAFE_FREE(size);
|
|
|
|
AX_SAFE_FREE(deltaSize);
|
|
|
|
AX_SAFE_FREE(rotation);
|
|
|
|
AX_SAFE_FREE(staticRotation);
|
|
|
|
AX_SAFE_FREE(deltaRotation);
|
|
|
|
AX_SAFE_FREE(totalTimeToLive);
|
|
|
|
AX_SAFE_FREE(timeToLive);
|
|
|
|
AX_SAFE_FREE(animTimeLength);
|
|
|
|
AX_SAFE_FREE(animTimeDelta);
|
|
|
|
AX_SAFE_FREE(animIndex);
|
|
|
|
AX_SAFE_FREE(animCellIndex);
|
|
|
|
AX_SAFE_FREE(atlasIndex);
|
|
|
|
|
|
|
|
AX_SAFE_FREE(modeA.dirX);
|
|
|
|
AX_SAFE_FREE(modeA.dirY);
|
|
|
|
AX_SAFE_FREE(modeA.radialAccel);
|
|
|
|
AX_SAFE_FREE(modeA.tangentialAccel);
|
|
|
|
|
|
|
|
AX_SAFE_FREE(modeB.angle);
|
|
|
|
AX_SAFE_FREE(modeB.degreesPerSecond);
|
|
|
|
AX_SAFE_FREE(modeB.deltaRadius);
|
|
|
|
AX_SAFE_FREE(modeB.radius);
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 13:37:50 +08:00
|
|
|
Vector<ParticleSystem*> ParticleSystem::__allInstances;
|
|
|
|
float ParticleSystem::__totalParticleCountFactor = 1.0f;
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystem::ParticleSystem()
|
2021-12-25 10:04:45 +08:00
|
|
|
: _isBlendAdditive(false)
|
|
|
|
, _isAutoRemoveOnFinish(false)
|
|
|
|
, _plistFile("")
|
|
|
|
, _elapsed(0)
|
|
|
|
, _configName("")
|
|
|
|
, _emitCounter(0)
|
|
|
|
, _batchNode(nullptr)
|
|
|
|
, _atlasIndex(0)
|
|
|
|
, _transformSystemDirty(false)
|
|
|
|
, _allocatedParticles(0)
|
2022-05-26 20:56:56 +08:00
|
|
|
, _isAnimAllocated(false)
|
|
|
|
, _isHSVAllocated(false)
|
2022-05-27 19:08:25 +08:00
|
|
|
, _isOpacityFadeInAllocated(false)
|
|
|
|
, _isScaleInAllocated(false)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _isActive(true)
|
|
|
|
, _particleCount(0)
|
|
|
|
, _duration(0)
|
|
|
|
, _life(0)
|
|
|
|
, _lifeVar(0)
|
|
|
|
, _angle(0)
|
|
|
|
, _angleVar(0)
|
|
|
|
, _emitterMode(Mode::GRAVITY)
|
|
|
|
, _startSize(0)
|
|
|
|
, _startSizeVar(0)
|
|
|
|
, _endSize(0)
|
|
|
|
, _endSizeVar(0)
|
|
|
|
, _startSpin(0)
|
|
|
|
, _startSpinVar(0)
|
|
|
|
, _endSpin(0)
|
|
|
|
, _endSpinVar(0)
|
2022-05-24 05:46:38 +08:00
|
|
|
, _spawnAngle(0)
|
|
|
|
, _spawnAngleVar(0)
|
2022-05-26 19:00:33 +08:00
|
|
|
, _hsv(0, 1, 1)
|
|
|
|
, _hsvVar(0, 0, 0)
|
2022-05-27 00:59:48 +08:00
|
|
|
, _spawnFadeIn(0)
|
|
|
|
, _spawnFadeInVar(0)
|
2022-05-27 03:53:19 +08:00
|
|
|
, _spawnScaleIn(0)
|
|
|
|
, _spawnScaleInVar(0)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _emissionRate(0)
|
|
|
|
, _totalParticles(0)
|
|
|
|
, _texture(nullptr)
|
|
|
|
, _blendFunc(BlendFunc::ALPHA_PREMULTIPLIED)
|
|
|
|
, _opacityModifyRGB(false)
|
2022-05-20 06:15:39 +08:00
|
|
|
, _isLifeAnimated(false)
|
|
|
|
, _isEmitterAnimated(false)
|
2022-05-21 21:48:46 +08:00
|
|
|
, _isLoopAnimated(false)
|
|
|
|
, _animIndexCount(0)
|
2022-05-22 20:58:36 +08:00
|
|
|
, _isAnimationReversed(false)
|
2022-06-07 00:23:11 +08:00
|
|
|
, _undefinedIndexRect({0, 0, 0, 0})
|
2022-05-26 19:00:33 +08:00
|
|
|
, _animationTimescaleInd(false)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _yCoordFlipped(1)
|
2022-06-11 23:06:43 +08:00
|
|
|
, _isEmissionShapes(false)
|
|
|
|
, _emissionShapeIndex(0)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _positionType(PositionType::FREE)
|
|
|
|
, _paused(false)
|
2022-05-23 21:04:09 +08:00
|
|
|
, _timeScale(1)
|
2022-05-24 05:46:38 +08:00
|
|
|
, _fixedFPS(0)
|
|
|
|
, _fixedFPSDelta(0)
|
2021-12-25 10:04:45 +08:00
|
|
|
, _sourcePositionCompatible(true) // In the furture this member's default value maybe false or be removed.
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-04-20 01:40:52 +08:00
|
|
|
modeA.gravity.setZero();
|
2021-12-25 10:04:45 +08:00
|
|
|
modeA.speed = 0;
|
|
|
|
modeA.speedVar = 0;
|
|
|
|
modeA.tangentialAccel = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.tangentialAccelVar = 0;
|
2021-12-25 10:04:45 +08:00
|
|
|
modeA.radialAccel = 0;
|
|
|
|
modeA.radialAccelVar = 0;
|
|
|
|
modeA.rotationIsDir = false;
|
|
|
|
modeB.startRadius = 0;
|
|
|
|
modeB.startRadiusVar = 0;
|
|
|
|
modeB.endRadius = 0;
|
|
|
|
modeB.endRadiusVar = 0;
|
|
|
|
modeB.rotatePerSecond = 0;
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.rotatePerSecondVar = 0;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2013-06-20 14:13:12 +08:00
|
|
|
// implementation ParticleSystem
|
2012-06-14 15:13:16 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
ParticleSystem* ParticleSystem::create(std::string_view plistFile)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ParticleSystem* ret = new ParticleSystem();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->initWithFile(plistFile))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-03-16 13:42:53 +08:00
|
|
|
|
2013-11-15 08:21:49 +08:00
|
|
|
ParticleSystem* ParticleSystem::createWithTotalParticles(int numberOfParticles)
|
2012-08-29 14:53:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
ParticleSystem* ret = new ParticleSystem();
|
2021-12-08 00:11:53 +08:00
|
|
|
if (ret->initWithTotalParticles(numberOfParticles))
|
2012-08-29 14:53:01 +08:00
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret->autorelease();
|
|
|
|
return ret;
|
2012-08-29 14:53:01 +08:00
|
|
|
}
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_DELETE(ret);
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2012-08-29 14:53:01 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 13:37:50 +08:00
|
|
|
// static
|
|
|
|
Vector<ParticleSystem*>& ParticleSystem::getAllParticleSystems()
|
|
|
|
{
|
|
|
|
return __allInstances;
|
|
|
|
}
|
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
bool ParticleSystem::allocAnimationMem()
|
|
|
|
{
|
|
|
|
if (!_isAnimAllocated)
|
|
|
|
{
|
|
|
|
_particleData.animTimeLength = (float*)malloc(_totalParticles * sizeof(float));
|
2022-06-07 00:23:11 +08:00
|
|
|
_particleData.animTimeDelta = (float*)malloc(_totalParticles * sizeof(float));
|
|
|
|
_particleData.animIndex = (unsigned short*)malloc(_totalParticles * sizeof(unsigned short));
|
|
|
|
_particleData.animCellIndex = (unsigned short*)malloc(_totalParticles * sizeof(unsigned short));
|
|
|
|
if (_particleData.animTimeLength && _particleData.animTimeDelta && _particleData.animIndex &&
|
|
|
|
_particleData.animCellIndex)
|
2022-05-27 18:36:38 +08:00
|
|
|
return _isAnimAllocated = true;
|
|
|
|
else
|
|
|
|
// If any of the above allocations fail, then we safely deallocate the ones that succeeded.
|
|
|
|
deallocAnimationMem();
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
2022-05-27 18:36:38 +08:00
|
|
|
return false;
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::deallocAnimationMem()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_FREE(_particleData.animTimeLength);
|
|
|
|
AX_SAFE_FREE(_particleData.animTimeDelta);
|
|
|
|
AX_SAFE_FREE(_particleData.animIndex);
|
|
|
|
AX_SAFE_FREE(_particleData.animCellIndex);
|
2022-05-27 18:36:38 +08:00
|
|
|
_isAnimAllocated = false;
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ParticleSystem::allocHSVMem()
|
|
|
|
{
|
|
|
|
if (!_isHSVAllocated)
|
|
|
|
{
|
|
|
|
_particleData.hue = (float*)malloc(_totalParticles * sizeof(float));
|
|
|
|
_particleData.sat = (float*)malloc(_totalParticles * sizeof(float));
|
|
|
|
_particleData.val = (float*)malloc(_totalParticles * sizeof(float));
|
2022-05-27 18:36:38 +08:00
|
|
|
if (_particleData.hue && _particleData.sat && _particleData.val)
|
|
|
|
return _isHSVAllocated = true;
|
|
|
|
else
|
|
|
|
// If any of the above allocations fail, then we safely deallocate the ones that succeeded.
|
|
|
|
deallocHSVMem();
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
2022-05-27 18:36:38 +08:00
|
|
|
return false;
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::deallocHSVMem()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_FREE(_particleData.hue);
|
|
|
|
AX_SAFE_FREE(_particleData.sat);
|
|
|
|
AX_SAFE_FREE(_particleData.val);
|
2022-05-27 18:36:38 +08:00
|
|
|
_isHSVAllocated = false;
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
|
|
|
|
2022-05-27 00:59:48 +08:00
|
|
|
bool ParticleSystem::allocOpacityFadeInMem()
|
|
|
|
{
|
|
|
|
if (!_isOpacityFadeInAllocated)
|
|
|
|
{
|
2022-05-27 18:36:38 +08:00
|
|
|
_particleData.opacityFadeInDelta = (float*)malloc(_totalParticles * sizeof(float));
|
2022-05-27 00:59:48 +08:00
|
|
|
_particleData.opacityFadeInLength = (float*)malloc(_totalParticles * sizeof(float));
|
2022-05-27 18:36:38 +08:00
|
|
|
if (_particleData.opacityFadeInDelta && _particleData.opacityFadeInLength)
|
|
|
|
return _isOpacityFadeInAllocated = true;
|
|
|
|
else
|
|
|
|
// If any of the above allocations fail, then we safely deallocate the ones that succeeded.
|
|
|
|
deallocOpacityFadeInMem();
|
2022-05-27 00:59:48 +08:00
|
|
|
}
|
2022-05-27 18:36:38 +08:00
|
|
|
return false;
|
2022-05-27 00:59:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::deallocOpacityFadeInMem()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_FREE(_particleData.opacityFadeInDelta);
|
|
|
|
AX_SAFE_FREE(_particleData.opacityFadeInLength);
|
2022-05-27 18:36:38 +08:00
|
|
|
_isOpacityFadeInAllocated = false;
|
2022-05-27 00:59:48 +08:00
|
|
|
}
|
|
|
|
|
2022-05-27 03:53:19 +08:00
|
|
|
bool ParticleSystem::allocScaleInMem()
|
|
|
|
{
|
|
|
|
if (!_isScaleInAllocated)
|
|
|
|
{
|
|
|
|
_particleData.scaleInDelta = (float*)malloc(_totalParticles * sizeof(float));
|
|
|
|
_particleData.scaleInLength = (float*)malloc(_totalParticles * sizeof(float));
|
2022-05-27 18:36:38 +08:00
|
|
|
if (_particleData.scaleInDelta && _particleData.scaleInLength)
|
|
|
|
return _isScaleInAllocated = true;
|
|
|
|
else
|
|
|
|
// If any of the above allocations fail, then we safely deallocate the ones that succeeded.
|
|
|
|
deallocScaleInMem();
|
2022-05-27 03:53:19 +08:00
|
|
|
}
|
2022-05-27 18:36:38 +08:00
|
|
|
return false;
|
2022-05-27 03:53:19 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::deallocScaleInMem()
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_FREE(_particleData.scaleInDelta);
|
|
|
|
AX_SAFE_FREE(_particleData.scaleInLength);
|
2022-05-27 18:36:38 +08:00
|
|
|
_isScaleInAllocated = false;
|
2022-05-27 03:53:19 +08:00
|
|
|
}
|
|
|
|
|
2017-06-05 13:37:50 +08:00
|
|
|
void ParticleSystem::setTotalParticleCountFactor(float factor)
|
|
|
|
{
|
|
|
|
__totalParticleCountFactor = factor;
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ParticleSystem::init()
|
2012-03-16 13:42:53 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
return initWithTotalParticles(150);
|
2012-03-16 13:42:53 +08:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool ParticleSystem::initWithFile(std::string_view plistFile)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ret = false;
|
|
|
|
_plistFile = FileUtils::getInstance()->fullPathForFilename(plistFile);
|
2015-05-12 12:31:33 +08:00
|
|
|
ValueMap dict = FileUtils::getInstance()->getValueMapFromFile(_plistFile);
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!dict.empty(), "Particles: file not found");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME: compute path from a path, should define a function somewhere to do it
|
2021-12-26 23:26:34 +08:00
|
|
|
auto listFilePath = plistFile;
|
2012-11-15 17:16:51 +08:00
|
|
|
if (listFilePath.find('/') != string::npos)
|
|
|
|
{
|
|
|
|
listFilePath = listFilePath.substr(0, listFilePath.rfind('/') + 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
ret = this->initWithDictionary(dict, listFilePath);
|
2012-11-15 17:16:51 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = this->initWithDictionary(dict, "");
|
2012-11-15 17:16:51 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool ParticleSystem::initWithDictionary(const ValueMap& dictionary)
|
|
|
|
{
|
2012-11-15 17:16:51 +08:00
|
|
|
return initWithDictionary(dictionary, "");
|
|
|
|
}
|
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool ParticleSystem::initWithDictionary(const ValueMap& dictionary, std::string_view dirname)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
unsigned char* buffer = nullptr;
|
|
|
|
Image* image = nullptr;
|
|
|
|
do
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
int maxParticles = optValue(dictionary, "maxParticles").asInt();
|
2012-04-19 14:35:52 +08:00
|
|
|
// self, not super
|
2021-12-25 10:04:45 +08:00
|
|
|
if (this->initWithTotalParticles(maxParticles))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-23 10:12:54 +08:00
|
|
|
// Emitter name in particle designer 2.0
|
2021-07-15 12:23:50 +08:00
|
|
|
_configName = optValue(dictionary, "configName").asString();
|
2013-09-23 10:12:54 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// angle
|
2021-12-25 10:04:45 +08:00
|
|
|
_angle = optValue(dictionary, "angle").asFloat();
|
2021-07-15 12:23:50 +08:00
|
|
|
_angleVar = optValue(dictionary, "angleVariance").asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// duration
|
2021-07-15 12:23:50 +08:00
|
|
|
_duration = optValue(dictionary, "duration").asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// blend function
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!_configName.empty())
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
_blendFunc.src = utils::toBackendBlendFactor((int)optValue(dictionary, "blendFuncSource").asFloat());
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
_blendFunc.src = utils::toBackendBlendFactor(optValue(dictionary, "blendFuncSource").asInt());
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2021-07-15 12:23:50 +08:00
|
|
|
_blendFunc.dst = utils::toBackendBlendFactor(optValue(dictionary, "blendFuncDestination").asInt());
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// color
|
2021-07-15 12:23:50 +08:00
|
|
|
_startColor.r = optValue(dictionary, "startColorRed").asFloat();
|
|
|
|
_startColor.g = optValue(dictionary, "startColorGreen").asFloat();
|
|
|
|
_startColor.b = optValue(dictionary, "startColorBlue").asFloat();
|
|
|
|
_startColor.a = optValue(dictionary, "startColorAlpha").asFloat();
|
|
|
|
|
|
|
|
_startColorVar.r = optValue(dictionary, "startColorVarianceRed").asFloat();
|
|
|
|
_startColorVar.g = optValue(dictionary, "startColorVarianceGreen").asFloat();
|
|
|
|
_startColorVar.b = optValue(dictionary, "startColorVarianceBlue").asFloat();
|
|
|
|
_startColorVar.a = optValue(dictionary, "startColorVarianceAlpha").asFloat();
|
|
|
|
|
|
|
|
_endColor.r = optValue(dictionary, "finishColorRed").asFloat();
|
|
|
|
_endColor.g = optValue(dictionary, "finishColorGreen").asFloat();
|
|
|
|
_endColor.b = optValue(dictionary, "finishColorBlue").asFloat();
|
|
|
|
_endColor.a = optValue(dictionary, "finishColorAlpha").asFloat();
|
|
|
|
|
|
|
|
_endColorVar.r = optValue(dictionary, "finishColorVarianceRed").asFloat();
|
|
|
|
_endColorVar.g = optValue(dictionary, "finishColorVarianceGreen").asFloat();
|
|
|
|
_endColorVar.b = optValue(dictionary, "finishColorVarianceBlue").asFloat();
|
|
|
|
_endColorVar.a = optValue(dictionary, "finishColorVarianceAlpha").asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// particle size
|
2021-12-25 10:04:45 +08:00
|
|
|
_startSize = optValue(dictionary, "startParticleSize").asFloat();
|
2021-07-15 12:23:50 +08:00
|
|
|
_startSizeVar = optValue(dictionary, "startParticleSizeVariance").asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
_endSize = optValue(dictionary, "finishParticleSize").asFloat();
|
|
|
|
_endSizeVar = optValue(dictionary, "finishParticleSizeVariance").asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// position
|
2021-07-15 12:23:50 +08:00
|
|
|
float x = optValue(dictionary, "sourcePositionx").asFloat();
|
|
|
|
float y = optValue(dictionary, "sourcePositiony").asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_sourcePositionCompatible)
|
|
|
|
{
|
2017-09-07 17:16:46 +08:00
|
|
|
this->setSourcePosition(Vec2(x, y));
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
this->setPosition(Vec2(x, y));
|
|
|
|
}
|
|
|
|
_posVar.x = optValue(dictionary, "sourcePositionVariancex").asFloat();
|
|
|
|
_posVar.y = optValue(dictionary, "sourcePositionVariancey").asFloat();
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Spinning
|
2021-12-25 10:04:45 +08:00
|
|
|
_startSpin = optValue(dictionary, "rotationStart").asFloat();
|
2021-07-15 12:23:50 +08:00
|
|
|
_startSpinVar = optValue(dictionary, "rotationStartVariance").asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
_endSpin = optValue(dictionary, "rotationEnd").asFloat();
|
|
|
|
_endSpinVar = optValue(dictionary, "rotationEndVariance").asFloat();
|
2011-07-04 19:20:16 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_emitterMode = (Mode)optValue(dictionary, "emitterType").asInt();
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Mode A: Gravity + tangential accel + radial accel
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_emitterMode == Mode::GRAVITY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
// gravity
|
2021-07-15 12:23:50 +08:00
|
|
|
modeA.gravity.x = optValue(dictionary, "gravityx").asFloat();
|
|
|
|
modeA.gravity.y = optValue(dictionary, "gravityy").asFloat();
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// speed
|
2021-12-25 10:04:45 +08:00
|
|
|
modeA.speed = optValue(dictionary, "speed").asFloat();
|
2021-07-15 12:23:50 +08:00
|
|
|
modeA.speedVar = optValue(dictionary, "speedVariance").asFloat();
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// radial acceleration
|
2021-12-25 10:04:45 +08:00
|
|
|
modeA.radialAccel = optValue(dictionary, "radialAcceleration").asFloat();
|
2021-07-15 12:23:50 +08:00
|
|
|
modeA.radialAccelVar = optValue(dictionary, "radialAccelVariance").asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// tangential acceleration
|
2021-12-25 10:04:45 +08:00
|
|
|
modeA.tangentialAccel = optValue(dictionary, "tangentialAcceleration").asFloat();
|
2021-07-15 12:23:50 +08:00
|
|
|
modeA.tangentialAccelVar = optValue(dictionary, "tangentialAccelVariance").asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-01-21 17:39:48 +08:00
|
|
|
// rotation is dir
|
2021-07-15 12:23:50 +08:00
|
|
|
modeA.rotationIsDir = optValue(dictionary, "rotationIsDir").asBool();
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// or Mode B: radius movement
|
2013-07-26 13:48:32 +08:00
|
|
|
else if (_emitterMode == Mode::RADIUS)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!_configName.empty())
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.startRadius = optValue(dictionary, "maxRadius").asInt();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.startRadius = optValue(dictionary, "maxRadius").asFloat();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.startRadiusVar = optValue(dictionary, "maxRadiusVariance").asFloat();
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!_configName.empty())
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.endRadius = optValue(dictionary, "minRadius").asInt();
|
2014-03-21 16:25:22 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.endRadius = optValue(dictionary, "minRadius").asFloat();
|
2014-03-21 16:25:22 +08:00
|
|
|
}
|
2021-07-15 12:23:50 +08:00
|
|
|
|
|
|
|
modeB.endRadiusVar = optValue(dictionary, "minRadiusVariance").asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-09-01 11:26:09 +08:00
|
|
|
if (!_configName.empty())
|
2013-09-18 16:21:48 +08:00
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.rotatePerSecond = optValue(dictionary, "rotatePerSecond").asInt();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.rotatePerSecond = optValue(dictionary, "rotatePerSecond").asFloat();
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2021-07-15 12:23:50 +08:00
|
|
|
modeB.rotatePerSecondVar = optValue(dictionary, "rotatePerSecondVariance").asFloat();
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Invalid emitterType in config file");
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(true);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// life span
|
2021-12-25 10:04:45 +08:00
|
|
|
_life = optValue(dictionary, "particleLifespan").asFloat();
|
2021-07-15 12:23:50 +08:00
|
|
|
_lifeVar = optValue(dictionary, "particleLifespanVariance").asFloat();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// emission Rate
|
2013-06-15 14:03:30 +08:00
|
|
|
_emissionRate = _totalParticles / _life;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// don't get the internal texture if a batchNode is used
|
2013-06-15 14:03:30 +08:00
|
|
|
if (!_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-06-12 01:43:07 +08:00
|
|
|
// Set a compatible default for the alpha transfer
|
2013-06-15 14:03:30 +08:00
|
|
|
_opacityModifyRGB = false;
|
2012-06-08 14:11:48 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// texture
|
2012-04-24 15:02:18 +08:00
|
|
|
// Try to get the texture from the cache
|
2021-07-15 12:23:50 +08:00
|
|
|
std::string textureName = optValue(dictionary, "textureFileName").asString();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-11-21 17:22:26 +08:00
|
|
|
size_t rPos = textureName.rfind('/');
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-11-21 17:22:26 +08:00
|
|
|
if (rPos != string::npos)
|
2012-11-15 17:16:51 +08:00
|
|
|
{
|
2012-11-21 17:22:26 +08:00
|
|
|
string textureDir = textureName.substr(0, rPos + 1);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-01-08 10:47:27 +08:00
|
|
|
if (!dirname.empty() && textureDir != dirname)
|
2012-11-21 17:22:26 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
textureName = textureName.substr(rPos + 1);
|
2021-12-26 23:26:34 +08:00
|
|
|
textureName.insert(0, dirname); // textureName = dirname + textureName;
|
2012-11-21 17:22:26 +08:00
|
|
|
}
|
2012-11-15 17:16:51 +08:00
|
|
|
}
|
2014-01-08 10:47:27 +08:00
|
|
|
else if (!dirname.empty() && !textureName.empty())
|
2013-01-17 11:42:41 +08:00
|
|
|
{
|
2021-12-26 23:26:34 +08:00
|
|
|
textureName.insert(0, dirname); // textureName = dirname + textureName;
|
2013-01-17 11:42:41 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
Texture2D* tex = nullptr;
|
|
|
|
|
2015-06-12 11:03:32 +08:00
|
|
|
if (!textureName.empty())
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2012-04-24 15:02:18 +08:00
|
|
|
// set not pop-up message box when load image failed
|
2013-12-18 17:47:20 +08:00
|
|
|
bool notify = FileUtils::getInstance()->isPopupNotify();
|
2013-07-12 06:24:23 +08:00
|
|
|
FileUtils::getInstance()->setPopupNotify(false);
|
2021-02-05 23:09:14 +08:00
|
|
|
tex = _director->getTextureCache()->addImage(textureName);
|
2012-04-24 15:02:18 +08:00
|
|
|
// reset the value of UIImage notify
|
2013-12-18 17:47:20 +08:00
|
|
|
FileUtils::getInstance()->setPopupNotify(notify);
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-24 15:02:18 +08:00
|
|
|
if (tex)
|
|
|
|
{
|
|
|
|
setTexture(tex);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else if (dictionary.find("textureImageData") != dictionary.end())
|
|
|
|
{
|
2014-05-06 06:50:08 +08:00
|
|
|
std::string textureData = dictionary.at("textureImageData").asString();
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!textureData.empty(), "textureData can't be empty!");
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-12-05 17:19:01 +08:00
|
|
|
auto dataLen = textureData.size();
|
2013-12-03 15:41:01 +08:00
|
|
|
if (dataLen != 0)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// if it fails, try to get it from the base64-gzipped data
|
|
|
|
int decodeLen =
|
2023-01-01 18:07:18 +08:00
|
|
|
utils::base64Decode((unsigned char*)textureData.c_str(), (unsigned int)dataLen, &buffer);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(buffer != nullptr, "CCParticleSystem: error decoding textureImageData");
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(!buffer);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-08-31 19:24:23 +08:00
|
|
|
unsigned char* deflated = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t deflatedLen = ZipUtils::inflateMemory(buffer, decodeLen, &deflated);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(deflated != nullptr, "CCParticleSystem: error ungzipping textureImageData");
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(!deflated);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// For android, we should retain it in VolatileTexture::addImage which invoked in
|
|
|
|
// Director::getInstance()->getTextureCache()->addUIImage()
|
|
|
|
image = new Image();
|
2020-08-31 19:24:23 +08:00
|
|
|
bool isOK = image->initWithImageData(deflated, deflatedLen, true);
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(isOK, "CCParticleSystem: error init image with Data");
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_BREAK_IF(!isOK);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-02-05 23:09:14 +08:00
|
|
|
setTexture(_director->getTextureCache()->addImage(image, _plistFile + textureName));
|
2012-05-23 11:18:04 +08:00
|
|
|
|
|
|
|
image->release();
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2013-09-18 16:21:48 +08:00
|
|
|
}
|
2021-07-15 12:23:50 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_yCoordFlipped = optValue(dictionary, "yCoordFlipped").asInt(1);
|
2014-05-06 06:50:08 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!this->_texture)
|
2022-10-01 16:24:52 +08:00
|
|
|
AXLOGWARN("axmol: Warning: ParticleSystemQuad system without a texture");
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-12-18 17:47:20 +08:00
|
|
|
ret = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
} while (0);
|
2013-11-12 10:09:47 +08:00
|
|
|
free(buffer);
|
2013-12-18 17:47:20 +08:00
|
|
|
return ret;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-11-15 08:21:49 +08:00
|
|
|
bool ParticleSystem::initWithTotalParticles(int numberOfParticles)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalParticles = numberOfParticles;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.release();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (!_particleData.init(_totalParticles))
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Particle system: not enough memory");
|
2012-04-19 14:35:52 +08:00
|
|
|
this->release();
|
|
|
|
return false;
|
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
_allocatedParticles = numberOfParticles;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-09-08 11:26:38 +08:00
|
|
|
for (int i = 0; i < _totalParticles; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.atlasIndex[i] = i;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// default, active
|
2013-06-15 14:03:30 +08:00
|
|
|
_isActive = true;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// default blend function
|
2013-07-26 08:47:42 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// default movement type;
|
2013-07-26 13:48:32 +08:00
|
|
|
_positionType = PositionType::FREE;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// by default be in mode A:
|
2013-07-26 13:48:32 +08:00
|
|
|
_emitterMode = Mode::GRAVITY;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
// default: modulate
|
2014-08-30 03:54:24 +08:00
|
|
|
// FIXME:: not used
|
2012-04-19 14:35:52 +08:00
|
|
|
// colorModulate = YES;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_isAutoRemoveOnFinish = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-09-17 15:02:24 +08:00
|
|
|
// Optimization: compile updateParticle method
|
2021-12-25 10:04:45 +08:00
|
|
|
// updateParticleSel = @selector(updateQuadWithParticle:newPosition:);
|
2022-07-15 19:17:01 +08:00
|
|
|
// updateParticleImp = (AX_UPDATE_PARTICLE_IMP) [self methodForSelector:updateParticleSel];
|
2021-12-25 10:04:45 +08:00
|
|
|
// for batchNode
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
return true;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
ParticleSystem::~ParticleSystem()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-02-27 16:26:42 +08:00
|
|
|
// Since the scheduler retains the "target (in this case the ParticleSystem)
|
2021-12-25 10:04:45 +08:00
|
|
|
// it is not needed to call "unscheduleUpdate" here. In fact, it will be called in "cleanup"
|
|
|
|
// unscheduleUpdate();
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.release();
|
2022-05-21 21:48:46 +08:00
|
|
|
_animations.clear();
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RELEASE(_texture);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
void ParticleSystem::addParticles(int count, int animationIndex, int animationCellIndex)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2016-06-15 18:31:10 +08:00
|
|
|
if (_paused)
|
|
|
|
return;
|
2022-05-22 20:58:36 +08:00
|
|
|
|
2022-05-27 00:59:48 +08:00
|
|
|
// Try to add as many particles as possible without overflowing.
|
2022-05-23 23:35:36 +08:00
|
|
|
count = MIN(int(_totalParticles * __totalParticleCountFactor) - _particleCount, count);
|
|
|
|
|
2022-05-22 20:58:36 +08:00
|
|
|
animationCellIndex = MIN(animationCellIndex, _animIndexCount - 1);
|
|
|
|
animationIndex = MIN(animationIndex, _animIndexCount - 1);
|
2022-05-20 06:15:39 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
int start = _particleCount;
|
|
|
|
_particleCount += count;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// life
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
float particleLife = _life + _lifeVar * _rng.rangef();
|
2022-05-20 06:15:39 +08:00
|
|
|
_particleData.totalTimeToLive[i] = MAX(0, particleLife);
|
|
|
|
_particleData.timeToLive[i] = MAX(0, particleLife);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-06-03 00:26:02 +08:00
|
|
|
if (_isEmissionShapes)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
2022-06-03 00:26:02 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
|
|
|
if (_emissionShapes.empty())
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.posx[i] = _sourcePosition.x + _posVar.x * _rng.rangef();
|
|
|
|
_particleData.posy[i] = _sourcePosition.y + _posVar.y * _rng.rangef();
|
2022-06-03 00:26:02 +08:00
|
|
|
continue;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-06-16 04:04:08 +08:00
|
|
|
auto randElem = _rng.float01();
|
2022-06-07 00:23:11 +08:00
|
|
|
auto& shape = _emissionShapes[MIN(randElem * _emissionShapes.size(), _emissionShapes.size() - 1)];
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
switch (shape.type)
|
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
case EmissionShapeType::POINT:
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
|
|
|
_particleData.posx[i] = _sourcePosition.x + shape.x;
|
|
|
|
_particleData.posy[i] = _sourcePosition.y + shape.y;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-06-17 00:42:37 +08:00
|
|
|
case EmissionShapeType::RECT:
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.posx[i] = _sourcePosition.x + shape.x + shape.innerWidth / 2 * _rng.rangef();
|
|
|
|
_particleData.posy[i] = _sourcePosition.y + shape.y + shape.innerHeight / 2 * _rng.rangef();
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-06-17 00:42:37 +08:00
|
|
|
case EmissionShapeType::RECTTORUS:
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
float width = (shape.outerWidth - shape.innerWidth) * _rng.float01() + shape.innerWidth;
|
|
|
|
float height = (shape.outerHeight - shape.innerHeight) * _rng.float01() + shape.innerHeight;
|
|
|
|
width = _rng.rangef() < 0.0F ? width * -1 : width;
|
|
|
|
height = _rng.rangef() < 0.0F ? height * -1 : height;
|
|
|
|
float prob = _rng.rangef();
|
|
|
|
_particleData.posx[i] = _sourcePosition.x + shape.x + width / 2 * (prob >= 0.0F ? 1.0F : _rng.rangef());
|
|
|
|
_particleData.posy[i] = _sourcePosition.y + shape.y + height / 2 * (prob < 0.0F ? 1.0F : _rng.rangef());
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-06-17 00:42:37 +08:00
|
|
|
case EmissionShapeType::CIRCLE:
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
auto val = _rng.float01() * shape.innerRadius / shape.innerRadius;
|
2022-06-16 20:25:43 +08:00
|
|
|
val = powf(val, 1 / shape.edgeBias);
|
2022-06-11 23:06:43 +08:00
|
|
|
auto point = Vec2(0.0F, val * shape.innerRadius);
|
2022-07-15 19:17:01 +08:00
|
|
|
point = point.rotateByAngle(Vec2::ZERO, -AX_DEGREES_TO_RADIANS(shape.coneOffset + shape.coneAngle / 2 * _rng.rangef()));
|
2022-06-03 00:26:02 +08:00
|
|
|
_particleData.posx[i] = _sourcePosition.x + shape.x + point.x / 2;
|
|
|
|
_particleData.posy[i] = _sourcePosition.y + shape.y + point.y / 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-06-17 00:42:37 +08:00
|
|
|
case EmissionShapeType::TORUS:
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
auto val = _rng.float01() * shape.outerRadius / shape.outerRadius;
|
2022-06-16 20:25:43 +08:00
|
|
|
val = powf(val, 1 / shape.edgeBias);
|
2022-06-11 23:06:43 +08:00
|
|
|
auto point = Vec2(0.0F, ((val * (shape.outerRadius - shape.innerRadius) + shape.outerRadius) - (shape.outerRadius - shape.innerRadius)));
|
2022-07-15 19:17:01 +08:00
|
|
|
point = point.rotateByAngle(Vec2::ZERO, -AX_DEGREES_TO_RADIANS(shape.coneOffset + shape.coneAngle / 2 * _rng.rangef()));
|
2022-06-03 00:26:02 +08:00
|
|
|
_particleData.posx[i] = _sourcePosition.x + shape.x + point.x / 2;
|
|
|
|
_particleData.posy[i] = _sourcePosition.y + shape.y + point.y / 2;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-06-17 00:42:37 +08:00
|
|
|
case EmissionShapeType::TEXTURE_ALPHA_MASK:
|
2022-06-12 05:58:01 +08:00
|
|
|
{
|
2022-06-16 20:25:43 +08:00
|
|
|
auto& mask = ParticleEmissionMaskCache::getInstance()->getEmissionMask(shape.fourccId);
|
2022-06-12 07:44:27 +08:00
|
|
|
|
2022-06-12 05:58:01 +08:00
|
|
|
Vec2 pos = {shape.x, shape.y};
|
2022-06-12 07:44:27 +08:00
|
|
|
Vec2 size = mask.size;
|
2022-06-12 05:58:01 +08:00
|
|
|
Vec2 overrideSize = {shape.innerWidth, shape.innerHeight};
|
|
|
|
Vec2 scale = {shape.outerWidth, shape.outerHeight};
|
|
|
|
float angle = shape.coneOffset;
|
|
|
|
|
|
|
|
if (overrideSize.isZero())
|
2022-06-12 07:44:27 +08:00
|
|
|
overrideSize = mask.size;
|
2022-06-12 05:58:01 +08:00
|
|
|
|
|
|
|
Vec2 point = {0, 0};
|
|
|
|
|
2022-06-16 04:04:08 +08:00
|
|
|
int rand0 = _rng.float01() * mask.points.size();
|
2022-11-20 08:58:34 +08:00
|
|
|
auto index = MIN(rand0, mask.points.size() - 1);
|
2022-06-12 08:10:49 +08:00
|
|
|
point = mask.points[index];
|
2022-06-12 05:58:01 +08:00
|
|
|
|
2022-06-12 07:16:15 +08:00
|
|
|
point -= size / 2;
|
|
|
|
|
|
|
|
point.x = point.x / size.x * overrideSize.x * scale.x;
|
|
|
|
point.y = point.y / size.y * overrideSize.y * scale.y;
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
point = point.rotateByAngle(Vec2::ZERO, -AX_DEGREES_TO_RADIANS(angle));
|
2022-06-12 07:16:15 +08:00
|
|
|
|
2022-06-12 05:58:01 +08:00
|
|
|
_particleData.posx[i] = _sourcePosition.x + shape.x + point.x;
|
|
|
|
_particleData.posy[i] = _sourcePosition.y + shape.y + point.y;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
2022-06-03 00:26:02 +08:00
|
|
|
// position
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.posx[i] = _sourcePosition.x + _posVar.x * _rng.rangef();
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.posy[i] = _sourcePosition.y + _posVar.y * _rng.rangef();
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
if (animationCellIndex != -1 || animationIndex != -1)
|
|
|
|
allocAnimationMem();
|
|
|
|
|
|
|
|
if (_isAnimAllocated)
|
2022-05-20 06:15:39 +08:00
|
|
|
{
|
2022-05-26 20:56:56 +08:00
|
|
|
if (animationCellIndex != -1)
|
|
|
|
std::fill_n(_particleData.animCellIndex + start, _particleCount - start, animationCellIndex);
|
|
|
|
else
|
|
|
|
std::fill_n(_particleData.animCellIndex + start, _particleCount - start, 0xFFFF);
|
|
|
|
|
|
|
|
if (animationIndex != -1)
|
2022-05-20 06:15:39 +08:00
|
|
|
{
|
2022-05-26 20:56:56 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
|
|
|
_particleData.animIndex[i] = animationIndex;
|
|
|
|
auto& descriptor = _animations.at(animationIndex);
|
|
|
|
_particleData.animTimeLength[i] =
|
2022-06-16 04:04:08 +08:00
|
|
|
descriptor.animationSpeed + descriptor.animationSpeedVariance * _rng.rangef();
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
2022-05-20 06:15:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
if (_isLifeAnimated || _isEmitterAnimated || _isLoopAnimated)
|
2022-05-21 21:48:46 +08:00
|
|
|
{
|
2022-05-26 20:56:56 +08:00
|
|
|
if (animationCellIndex == -1 && _isEmitterAnimated)
|
2022-05-21 21:48:46 +08:00
|
|
|
{
|
2022-05-26 20:56:56 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
int rand0 = _rng.float01() * _animIndexCount;
|
2022-05-26 20:56:56 +08:00
|
|
|
_particleData.animCellIndex[i] = MIN(rand0, _animIndexCount - 1);
|
|
|
|
}
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
2022-05-22 20:58:36 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
if (animationIndex == -1 && !_animations.empty())
|
2022-05-21 21:48:46 +08:00
|
|
|
{
|
2022-05-26 20:56:56 +08:00
|
|
|
if (_randomAnimations.empty())
|
|
|
|
setMultiAnimationRandom();
|
|
|
|
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
int rand0 = _rng.float01() * _randomAnimations.size();
|
2022-11-20 08:58:34 +08:00
|
|
|
auto index = MIN(rand0, _randomAnimations.size() - 1);
|
2022-05-26 20:56:56 +08:00
|
|
|
_particleData.animIndex[i] = _randomAnimations[index];
|
|
|
|
auto& descriptor = _animations.at(_particleData.animIndex[i]);
|
|
|
|
_particleData.animTimeLength[i] =
|
2022-06-16 04:04:08 +08:00
|
|
|
descriptor.animationSpeed + descriptor.animationSpeedVariance * _rng.rangef();
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
2022-05-26 20:56:56 +08:00
|
|
|
|
|
|
|
if (_isEmitterAnimated || _isLoopAnimated)
|
|
|
|
std::fill_n(_particleData.animTimeDelta + start, _particleCount - start, 0);
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// color
|
2022-06-07 00:23:11 +08:00
|
|
|
#define SET_COLOR(c, b, v) \
|
|
|
|
for (int i = start; i < _particleCount; ++i) \
|
|
|
|
{ \
|
2022-06-16 04:04:08 +08:00
|
|
|
c[i] = clampf(b + v * _rng.rangef(), 0, 1); \
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
SET_COLOR(_particleData.colorR, _startColor.r, _startColorVar.r);
|
|
|
|
SET_COLOR(_particleData.colorG, _startColor.g, _startColorVar.g);
|
|
|
|
SET_COLOR(_particleData.colorB, _startColor.b, _startColorVar.b);
|
|
|
|
SET_COLOR(_particleData.colorA, _startColor.a, _startColorVar.a);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
SET_COLOR(_particleData.deltaColorR, _endColor.r, _endColorVar.r);
|
|
|
|
SET_COLOR(_particleData.deltaColorG, _endColor.g, _endColorVar.g);
|
|
|
|
SET_COLOR(_particleData.deltaColorB, _endColor.b, _endColorVar.b);
|
|
|
|
SET_COLOR(_particleData.deltaColorA, _endColor.a, _endColorVar.a);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
#define SET_DELTA_COLOR(c, dc) \
|
|
|
|
for (int i = start; i < _particleCount; ++i) \
|
|
|
|
{ \
|
|
|
|
dc[i] = (dc[i] - c[i]) / _particleData.timeToLive[i]; \
|
|
|
|
}
|
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
SET_DELTA_COLOR(_particleData.colorR, _particleData.deltaColorR);
|
|
|
|
SET_DELTA_COLOR(_particleData.colorG, _particleData.deltaColorG);
|
|
|
|
SET_DELTA_COLOR(_particleData.colorB, _particleData.deltaColorB);
|
|
|
|
SET_DELTA_COLOR(_particleData.colorA, _particleData.deltaColorA);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-05-27 00:59:48 +08:00
|
|
|
// opacity fade in
|
|
|
|
if (_isOpacityFadeInAllocated)
|
|
|
|
{
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.opacityFadeInLength[i] = _spawnFadeIn + _spawnFadeInVar * _rng.rangef();
|
2022-05-27 00:59:48 +08:00
|
|
|
}
|
|
|
|
std::fill_n(_particleData.opacityFadeInDelta + start, _particleCount - start, 0.0F);
|
|
|
|
}
|
|
|
|
|
2022-05-27 03:53:19 +08:00
|
|
|
// scale fade in
|
|
|
|
if (_isScaleInAllocated)
|
|
|
|
{
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.scaleInLength[i] = _spawnScaleIn + _spawnScaleInVar * _rng.rangef();
|
2022-05-27 03:53:19 +08:00
|
|
|
}
|
|
|
|
std::fill_n(_particleData.scaleInDelta + start, _particleCount - start, 0.0F);
|
|
|
|
}
|
|
|
|
|
2022-05-26 19:00:33 +08:00
|
|
|
// hue saturation value color
|
2022-05-26 20:56:56 +08:00
|
|
|
if (_isHSVAllocated)
|
2022-05-26 19:00:33 +08:00
|
|
|
{
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.hue[i] = _hsv.h + _hsvVar.h * _rng.rangef();
|
2022-05-26 19:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.sat[i] = _hsv.s + _hsvVar.s * _rng.rangef();
|
2022-05-26 19:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.val[i] = _hsv.v + _hsvVar.v * _rng.rangef();
|
2022-05-26 19:00:33 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// size
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.size[i] = _startSize + _startSizeVar * _rng.rangef();
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.size[i] = MAX(0, _particleData.size[i]);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
if (_endSize != START_SIZE_EQUAL_TO_END_SIZE)
|
|
|
|
{
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
float endSize = _endSize + _endSizeVar * _rng.rangef();
|
2021-12-25 10:04:45 +08:00
|
|
|
endSize = MAX(0, endSize);
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.deltaSize[i] = (endSize - _particleData.size[i]) / _particleData.timeToLive[i];
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
2022-05-23 16:11:55 +08:00
|
|
|
std::fill_n(_particleData.deltaSize + start, _particleCount - start, 0.0F);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// rotation
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.rotation[i] = _startSpin + _startSpinVar * _rng.rangef();
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
float endA = _endSpin + _endSpinVar * _rng.rangef();
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.deltaRotation[i] = (endA - _particleData.rotation[i]) / _particleData.timeToLive[i];
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-05-21 21:48:46 +08:00
|
|
|
// static rotation
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.staticRotation[i] = _spawnAngle + _spawnAngleVar * _rng.rangef();
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// position
|
2015-08-31 17:25:34 +08:00
|
|
|
Vec2 pos;
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_positionType == PositionType::FREE)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
pos = this->convertToWorldSpace(Vec2::ZERO);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-07-26 13:48:32 +08:00
|
|
|
else if (_positionType == PositionType::RELATIVE)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
pos = _position;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2022-05-23 16:11:55 +08:00
|
|
|
std::fill_n(_particleData.startPosX + start, _particleCount - start, pos.x);
|
|
|
|
std::fill_n(_particleData.startPosY + start, _particleCount - start, pos.y);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Mode Gravity: A
|
2013-07-26 13:48:32 +08:00
|
|
|
if (_emitterMode == Mode::GRAVITY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// radial accel
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.modeA.radialAccel[i] = modeA.radialAccel + modeA.radialAccelVar * _rng.rangef();
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// tangential accel
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.modeA.tangentialAccel[i] = modeA.tangentialAccel + modeA.tangentialAccelVar * _rng.rangef();
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-01-21 17:39:48 +08:00
|
|
|
// rotation is dir
|
2021-12-25 10:04:45 +08:00
|
|
|
if (modeA.rotationIsDir)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
float a = AX_DEGREES_TO_RADIANS(_angle + _angleVar * _rng.rangef());
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 v(cosf(a), sinf(a));
|
2022-06-16 04:04:08 +08:00
|
|
|
float s = modeA.speed + modeA.speedVar * _rng.rangef();
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 dir = v * s;
|
|
|
|
_particleData.modeA.dirX[i] = dir.x; // v * s ;
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.modeA.dirY[i] = dir.y;
|
2022-07-15 19:17:01 +08:00
|
|
|
_particleData.rotation[i] = -AX_RADIANS_TO_DEGREES(dir.getAngle());
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
float a = AX_DEGREES_TO_RADIANS(_angle + _angleVar * _rng.rangef());
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 v(cosf(a), sinf(a));
|
2022-06-16 04:04:08 +08:00
|
|
|
float s = modeA.speed + modeA.speedVar * _rng.rangef();
|
2021-12-25 10:04:45 +08:00
|
|
|
Vec2 dir = v * s;
|
|
|
|
_particleData.modeA.dirX[i] = dir.x; // v * s ;
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.modeA.dirY[i] = dir.y;
|
|
|
|
}
|
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// Mode Radius: B
|
2015-08-31 17:25:34 +08:00
|
|
|
else
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// Need to check by Jacky
|
|
|
|
// Set the default diameter of the particle from the source position
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
_particleData.modeB.radius[i] = modeB.startRadius + modeB.startRadiusVar * _rng.rangef();
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
_particleData.modeB.angle[i] = AX_DEGREES_TO_RADIANS(_angle + _angleVar * _rng.rangef());
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_particleData.modeB.degreesPerSecond[i] =
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_DEGREES_TO_RADIANS(modeB.rotatePerSecond + modeB.rotatePerSecondVar * _rng.rangef());
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
if (modeB.endRadius == START_RADIUS_EQUAL_TO_END_RADIUS)
|
2022-05-23 16:11:55 +08:00
|
|
|
std::fill_n(_particleData.modeB.deltaRadius + start, _particleCount - start, 0.0F);
|
2012-04-19 14:35:52 +08:00
|
|
|
else
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = start; i < _particleCount; ++i)
|
|
|
|
{
|
2022-06-16 04:04:08 +08:00
|
|
|
float endRadius = modeB.endRadius + modeB.endRadiusVar * _rng.rangef();
|
2021-12-25 10:04:45 +08:00
|
|
|
_particleData.modeB.deltaRadius[i] =
|
|
|
|
(endRadius - _particleData.modeB.radius[i]) / _particleData.timeToLive[i];
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2022-05-21 21:48:46 +08:00
|
|
|
void ParticleSystem::setAnimationDescriptor(unsigned short indexOfDescriptor,
|
|
|
|
float time,
|
|
|
|
float timeVariance,
|
2022-06-07 00:23:11 +08:00
|
|
|
const std::vector<unsigned short>& indices,
|
2022-05-21 21:48:46 +08:00
|
|
|
bool reverse)
|
|
|
|
{
|
2022-05-22 15:09:13 +08:00
|
|
|
auto iter = _animations.find(indexOfDescriptor);
|
|
|
|
if (iter == _animations.end())
|
|
|
|
iter = _animations.emplace(indexOfDescriptor, ParticleAnimationDescriptor{}).first;
|
2022-05-21 21:48:46 +08:00
|
|
|
|
2022-05-22 15:09:13 +08:00
|
|
|
auto& desc = iter->second;
|
2022-05-21 21:48:46 +08:00
|
|
|
desc.animationSpeed = time;
|
|
|
|
desc.animationSpeedVariance = timeVariance;
|
2022-05-22 15:09:13 +08:00
|
|
|
desc.animationIndices = std::move(indices);
|
2022-05-21 21:48:46 +08:00
|
|
|
desc.reverseIndices = reverse;
|
|
|
|
}
|
|
|
|
|
2022-06-03 00:29:19 +08:00
|
|
|
void ParticleSystem::resetEmissionShapes()
|
|
|
|
{
|
2022-06-11 23:06:43 +08:00
|
|
|
_emissionShapeIndex = 0;
|
2022-06-03 00:29:19 +08:00
|
|
|
_emissionShapes.clear();
|
|
|
|
}
|
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
void ParticleSystem::addEmissionShape(EmissionShape shape)
|
|
|
|
{
|
|
|
|
setEmissionShape(_emissionShapeIndex, shape);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::setEmissionShape(unsigned short index, EmissionShape shape)
|
|
|
|
{
|
|
|
|
auto iter = _emissionShapes.find(index);
|
|
|
|
if (iter == _emissionShapes.end())
|
|
|
|
{
|
|
|
|
iter = _emissionShapes.emplace(index, EmissionShape{}).first;
|
|
|
|
_emissionShapeIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
iter->second = shape;
|
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createMaskShape(std::string_view maskId,
|
2022-06-12 05:58:01 +08:00
|
|
|
Vec2 pos,
|
|
|
|
Vec2 overrideSize,
|
|
|
|
Vec2 scale,
|
|
|
|
float angle)
|
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-12 05:58:01 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::TEXTURE_ALPHA_MASK;
|
2022-06-12 05:58:01 +08:00
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
shape.fourccId = utils::fourccValue(maskId);
|
2022-06-12 05:58:01 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
|
|
|
shape.innerWidth = overrideSize.x;
|
|
|
|
shape.innerHeight = overrideSize.y;
|
|
|
|
|
|
|
|
shape.outerWidth = scale.x;
|
|
|
|
shape.outerHeight = scale.y;
|
|
|
|
|
|
|
|
shape.coneOffset = angle;
|
|
|
|
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createPointShape(Vec2 pos)
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::POINT;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
return shape;
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createRectShape(Vec2 pos, Size size)
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::RECT;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
|
|
|
shape.innerWidth = size.x;
|
|
|
|
shape.innerHeight = size.y;
|
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
return shape;
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createRectTorusShape(Vec2 pos, Size innerSize, Size outerSize)
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::RECTTORUS;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
|
|
|
shape.innerWidth = innerSize.x;
|
|
|
|
shape.innerHeight = innerSize.y;
|
|
|
|
|
|
|
|
shape.outerWidth = outerSize.x;
|
|
|
|
shape.outerHeight = outerSize.y;
|
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
return shape;
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createCircleShape(Vec2 pos, float radius, float edgeBias)
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::CIRCLE;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
|
|
|
shape.innerRadius = radius;
|
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
shape.coneOffset = 0;
|
|
|
|
shape.coneAngle = 360;
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
shape.edgeBias = edgeBias;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
return shape;
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createConeShape(Vec2 pos,
|
2022-06-17 00:39:12 +08:00
|
|
|
float radius,
|
|
|
|
float offset,
|
|
|
|
float angle,
|
|
|
|
float edgeBias)
|
2022-06-11 23:06:43 +08:00
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-11 23:06:43 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::CIRCLE;
|
2022-06-11 23:06:43 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
|
|
|
shape.innerRadius = radius;
|
|
|
|
|
|
|
|
shape.coneOffset = offset;
|
|
|
|
shape.coneAngle = angle;
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
shape.edgeBias = edgeBias;
|
2022-06-11 23:06:43 +08:00
|
|
|
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createTorusShape(Vec2 pos,
|
2022-06-17 00:39:12 +08:00
|
|
|
float innerRadius,
|
|
|
|
float outerRadius,
|
|
|
|
float edgeBias)
|
2022-06-03 00:26:02 +08:00
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::TORUS;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
|
|
|
shape.innerRadius = innerRadius;
|
2022-06-11 23:06:43 +08:00
|
|
|
shape.outerRadius = outerRadius;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
shape.coneOffset = 0;
|
|
|
|
shape.coneAngle = 360;
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
shape.edgeBias = edgeBias;
|
2022-06-11 23:06:43 +08:00
|
|
|
|
|
|
|
return shape;
|
|
|
|
}
|
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape ParticleSystem::createConeTorusShape(Vec2 pos,
|
2022-06-17 00:39:12 +08:00
|
|
|
float innerRadius,
|
|
|
|
float outerRadius,
|
|
|
|
float offset,
|
|
|
|
float angle,
|
|
|
|
float edgeBias)
|
2022-06-11 23:06:43 +08:00
|
|
|
{
|
2022-06-17 00:42:37 +08:00
|
|
|
EmissionShape shape{};
|
2022-06-11 23:06:43 +08:00
|
|
|
|
2022-06-17 00:42:37 +08:00
|
|
|
shape.type = EmissionShapeType::TORUS;
|
2022-06-11 23:06:43 +08:00
|
|
|
|
|
|
|
shape.x = pos.x;
|
|
|
|
shape.y = pos.y;
|
|
|
|
|
|
|
|
shape.innerRadius = innerRadius;
|
2022-06-03 00:26:02 +08:00
|
|
|
shape.outerRadius = outerRadius;
|
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
shape.coneOffset = offset;
|
|
|
|
shape.coneAngle = angle;
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
shape.edgeBias = edgeBias;
|
2022-06-03 00:26:02 +08:00
|
|
|
|
2022-06-11 23:06:43 +08:00
|
|
|
return shape;
|
2022-06-03 00:26:02 +08:00
|
|
|
}
|
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
void ParticleSystem::setLifeAnimation(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled && !allocAnimationMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
deallocAnimationMem();
|
|
|
|
|
|
|
|
_isLifeAnimated = enabled;
|
|
|
|
_isEmitterAnimated = false;
|
|
|
|
_isLoopAnimated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::setEmitterAnimation(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled && !allocAnimationMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
deallocAnimationMem();
|
|
|
|
|
|
|
|
_isEmitterAnimated = enabled;
|
|
|
|
_isLifeAnimated = false;
|
|
|
|
_isLoopAnimated = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::setLoopAnimation(bool enabled)
|
|
|
|
{
|
|
|
|
if (enabled && !allocAnimationMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!enabled)
|
|
|
|
deallocAnimationMem();
|
|
|
|
|
|
|
|
_isLoopAnimated = enabled;
|
|
|
|
_isEmitterAnimated = false;
|
|
|
|
_isLifeAnimated = false;
|
|
|
|
}
|
|
|
|
|
2022-05-21 21:48:46 +08:00
|
|
|
void ParticleSystem::resetAnimationIndices()
|
|
|
|
{
|
|
|
|
_animIndexCount = 0;
|
|
|
|
_animationIndices.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::resetAnimationDescriptors()
|
|
|
|
{
|
|
|
|
_animations.clear();
|
|
|
|
_randomAnimations.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::setMultiAnimationRandom()
|
|
|
|
{
|
|
|
|
_randomAnimations.clear();
|
2022-07-21 19:19:08 +08:00
|
|
|
for (auto&& a : _animations)
|
2022-08-08 13:18:33 +08:00
|
|
|
_randomAnimations.emplace_back(a.first);
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-22 20:58:36 +08:00
|
|
|
void ParticleSystem::setAnimationIndicesAtlas()
|
|
|
|
{
|
|
|
|
// VERTICAL
|
|
|
|
if (_texture->getPixelsHigh() > _texture->getPixelsWide())
|
|
|
|
{
|
2022-06-07 00:23:11 +08:00
|
|
|
setAnimationIndicesAtlas(_texture->getPixelsWide(), ParticleSystem::TexAnimDir::VERTICAL);
|
2022-05-22 20:58:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HORIZONTAL
|
|
|
|
if (_texture->getPixelsWide() > _texture->getPixelsHigh())
|
|
|
|
{
|
2022-06-07 00:23:11 +08:00
|
|
|
setAnimationIndicesAtlas(_texture->getPixelsHigh(), ParticleSystem::TexAnimDir::HORIZONTAL);
|
2022-05-22 20:58:36 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(false, "Couldn't figure out the atlas size and direction.");
|
2022-05-22 20:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::setAnimationIndicesAtlas(unsigned int unifiedCellSize, TexAnimDir direction)
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(unifiedCellSize > 0, "A cell cannot have a size of zero.");
|
2022-05-22 20:58:36 +08:00
|
|
|
|
|
|
|
resetAnimationIndices();
|
2022-06-07 00:23:11 +08:00
|
|
|
|
2022-05-22 20:58:36 +08:00
|
|
|
auto texWidth = _texture->getPixelsWide();
|
|
|
|
auto texHeight = _texture->getPixelsHigh();
|
|
|
|
|
|
|
|
switch (direction)
|
|
|
|
{
|
|
|
|
case TexAnimDir::VERTICAL:
|
|
|
|
{
|
|
|
|
for (short i = 0; i < short(texHeight / unifiedCellSize); i++)
|
|
|
|
{
|
|
|
|
Rect frame{};
|
|
|
|
|
|
|
|
frame.origin.x = 0;
|
|
|
|
frame.origin.y = unifiedCellSize * i;
|
|
|
|
|
|
|
|
frame.size.x = texWidth;
|
|
|
|
frame.size.y = unifiedCellSize;
|
|
|
|
|
2022-05-26 21:53:26 +08:00
|
|
|
addAnimationIndex(_animIndexCount, frame);
|
2022-05-22 20:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
case TexAnimDir::HORIZONTAL:
|
|
|
|
{
|
|
|
|
for (short i = 0; i < short(texWidth / unifiedCellSize); i++)
|
|
|
|
{
|
|
|
|
Rect frame{};
|
|
|
|
|
|
|
|
frame.origin.x = unifiedCellSize * i;
|
|
|
|
frame.origin.y = 0;
|
|
|
|
|
2022-06-07 00:23:11 +08:00
|
|
|
frame.size.x = unifiedCellSize;
|
|
|
|
frame.size.y = texHeight;
|
2022-05-22 20:58:36 +08:00
|
|
|
|
2022-05-26 21:53:26 +08:00
|
|
|
addAnimationIndex(_animIndexCount, frame);
|
2022-05-22 20:58:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-23 01:51:56 +08:00
|
|
|
bool ParticleSystem::addAnimationIndex(std::string_view frameName)
|
2022-05-21 21:48:46 +08:00
|
|
|
{
|
2022-05-23 01:51:56 +08:00
|
|
|
return addAnimationIndex(_animIndexCount, frameName);
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-23 01:51:56 +08:00
|
|
|
bool ParticleSystem::addAnimationIndex(unsigned short index, std::string_view frameName)
|
2022-05-21 21:48:46 +08:00
|
|
|
{
|
|
|
|
auto frame = SpriteFrameCache::getInstance()->getSpriteFrameByName(frameName);
|
|
|
|
|
|
|
|
if (frame)
|
2022-05-23 01:51:56 +08:00
|
|
|
return addAnimationIndex(index, frame);
|
|
|
|
return false;
|
2022-06-07 00:23:11 +08:00
|
|
|
}
|
2022-05-21 21:48:46 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
bool ParticleSystem::addAnimationIndex(ax::SpriteFrame* frame)
|
2022-05-21 21:48:46 +08:00
|
|
|
{
|
2022-05-23 01:51:56 +08:00
|
|
|
return addAnimationIndex(_animIndexCount, frame);
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
bool ParticleSystem::addAnimationIndex(unsigned short index, ax::SpriteFrame* frame)
|
2022-05-21 21:48:46 +08:00
|
|
|
{
|
2022-05-23 01:51:56 +08:00
|
|
|
if (frame)
|
2022-06-16 04:04:08 +08:00
|
|
|
{
|
|
|
|
auto rect = frame->getRectInPixels();
|
|
|
|
rect.size.x = frame->getOriginalSizeInPixels().x;
|
|
|
|
rect.size.y = frame->getOriginalSizeInPixels().y;
|
|
|
|
return addAnimationIndex(index, rect, frame->isRotated());
|
|
|
|
}
|
2022-05-23 01:51:56 +08:00
|
|
|
return false;
|
2022-05-22 20:58:36 +08:00
|
|
|
}
|
2022-05-21 21:48:46 +08:00
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
bool ParticleSystem::addAnimationIndex(unsigned short index, ax::Rect rect, bool rotated)
|
2022-05-22 20:58:36 +08:00
|
|
|
{
|
|
|
|
auto iter = _animationIndices.find(index);
|
|
|
|
if (iter == _animationIndices.end())
|
2022-06-11 23:06:43 +08:00
|
|
|
{
|
2022-05-22 20:58:36 +08:00
|
|
|
iter = _animationIndices.emplace(index, ParticleFrameDescriptor{}).first;
|
2022-06-11 23:06:43 +08:00
|
|
|
_animIndexCount++;
|
|
|
|
}
|
2022-05-21 21:48:46 +08:00
|
|
|
|
2022-05-22 20:58:36 +08:00
|
|
|
auto& desc = iter->second;
|
|
|
|
desc.rect = rect;
|
|
|
|
desc.isRotated = rotated;
|
2022-05-23 01:51:56 +08:00
|
|
|
|
|
|
|
return true;
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 14:42:27 +08:00
|
|
|
void ParticleSystem::simulate(float seconds, float frameRate)
|
2022-05-24 05:46:38 +08:00
|
|
|
{
|
2022-06-07 00:23:11 +08:00
|
|
|
seconds = seconds == SIMULATION_USE_PARTICLE_LIFETIME ? getLife() + getLifeVar() : seconds;
|
|
|
|
frameRate = frameRate == SIMULATION_USE_GAME_ANIMATION_INTERVAL
|
|
|
|
? 1.0F / Director::getInstance()->getAnimationInterval()
|
|
|
|
: frameRate;
|
|
|
|
auto delta = 1.0F / frameRate;
|
2022-05-24 05:46:38 +08:00
|
|
|
if (seconds > delta)
|
|
|
|
{
|
|
|
|
while (seconds > 0.0F)
|
|
|
|
{
|
|
|
|
this->update(delta);
|
|
|
|
seconds -= delta;
|
|
|
|
}
|
|
|
|
this->update(seconds);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
this->update(seconds);
|
|
|
|
}
|
|
|
|
|
2022-05-24 14:42:27 +08:00
|
|
|
void ParticleSystem::resimulate(float seconds, float frameRate)
|
2022-05-24 05:46:38 +08:00
|
|
|
{
|
|
|
|
this->resetSystem();
|
2022-05-24 14:42:27 +08:00
|
|
|
this->simulate(seconds, frameRate);
|
2022-05-24 05:46:38 +08:00
|
|
|
}
|
|
|
|
|
2014-03-24 18:08:55 +08:00
|
|
|
void ParticleSystem::onEnter()
|
|
|
|
{
|
|
|
|
Node::onEnter();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2014-03-24 18:08:55 +08:00
|
|
|
// update after action in run!
|
|
|
|
this->scheduleUpdateWithPriority(1);
|
2017-06-05 13:37:50 +08:00
|
|
|
|
|
|
|
__allInstances.pushBack(this);
|
2014-03-24 18:08:55 +08:00
|
|
|
}
|
|
|
|
|
2014-03-28 14:05:38 +08:00
|
|
|
void ParticleSystem::onExit()
|
|
|
|
{
|
|
|
|
this->unscheduleUpdate();
|
|
|
|
Node::onExit();
|
2017-06-05 13:37:50 +08:00
|
|
|
|
|
|
|
auto iter = std::find(std::begin(__allInstances), std::end(__allInstances), this);
|
|
|
|
if (iter != std::end(__allInstances))
|
|
|
|
{
|
|
|
|
__allInstances.erase(iter);
|
|
|
|
}
|
2014-03-28 14:05:38 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::stopSystem()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_isActive = false;
|
|
|
|
_elapsed = _duration;
|
2013-06-15 14:03:30 +08:00
|
|
|
_emitCounter = 0;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::resetSystem()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isActive = true;
|
2021-12-25 10:04:45 +08:00
|
|
|
_elapsed = 0;
|
2022-05-24 05:46:38 +08:00
|
|
|
std::fill_n(_particleData.timeToLive, _particleCount, 0.0F);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2015-08-31 17:25:34 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
bool ParticleSystem::isFull()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return (_particleCount == _totalParticles);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleSystem - MainLoop
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::update(float dt)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-05-23 21:04:09 +08:00
|
|
|
// don't process particles nor update gl buffer when this node is invisible.
|
2022-06-16 04:04:08 +08:00
|
|
|
if (!_visible)
|
2022-05-23 21:04:09 +08:00
|
|
|
return;
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_PROFILER_START_CATEGORY(kProfilerCategoryParticles, "CCParticleSystem - update");
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-05-22 15:09:13 +08:00
|
|
|
if (_componentContainer && !_componentContainer->isEmpty())
|
|
|
|
{
|
|
|
|
_componentContainer->visit(dt);
|
|
|
|
}
|
|
|
|
|
2022-05-24 05:46:38 +08:00
|
|
|
if (_fixedFPS != 0)
|
|
|
|
{
|
|
|
|
_fixedFPSDelta += dt;
|
|
|
|
if (_fixedFPSDelta < 1.0F / _fixedFPS)
|
2022-06-07 00:23:11 +08:00
|
|
|
{
|
2022-06-16 08:55:46 +08:00
|
|
|
updateParticleQuads();
|
|
|
|
_transformSystemDirty = false;
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_PROFILER_STOP_CATEGORY(kProfilerCategoryParticles, "CCParticleSystem - update");
|
2022-05-24 05:46:38 +08:00
|
|
|
return;
|
2022-06-07 00:23:11 +08:00
|
|
|
}
|
|
|
|
dt = _fixedFPSDelta;
|
2022-05-24 05:46:38 +08:00
|
|
|
_fixedFPSDelta = 0.0F;
|
|
|
|
}
|
|
|
|
|
2022-05-24 16:26:53 +08:00
|
|
|
float pureDt = dt;
|
2022-05-23 21:04:09 +08:00
|
|
|
dt *= _timeScale;
|
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_isActive && _emissionRate)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
float rate = 1.0f / _emissionRate;
|
2017-06-05 13:37:50 +08:00
|
|
|
int totalParticles = static_cast<int>(_totalParticles * __totalParticleCountFactor);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// issue #1201, prevent bursts of particles, due to too high emitCounter
|
2017-06-05 13:37:50 +08:00
|
|
|
if (_particleCount < totalParticles)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_emitCounter += dt;
|
2022-05-23 21:04:09 +08:00
|
|
|
_emitCounter = MAX(0.0F, _emitCounter);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2017-06-05 13:37:50 +08:00
|
|
|
int emitCount = MIN(totalParticles - _particleCount, _emitCounter / rate);
|
2015-08-31 17:25:34 +08:00
|
|
|
addParticles(emitCount);
|
|
|
|
_emitCounter -= rate * emitCount;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_elapsed += dt;
|
2015-09-23 18:37:12 +08:00
|
|
|
if (_elapsed < 0.f)
|
|
|
|
_elapsed = 0.f;
|
2015-08-31 17:25:34 +08:00
|
|
|
if (_duration != DURATION_INFINITY && _duration < _elapsed)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->stopSystem();
|
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-05-23 16:32:54 +08:00
|
|
|
// The reason for using for-loops separately for every property is because
|
|
|
|
// When the processor needs to read from or write to a location in memory,
|
|
|
|
// it first checks whether a copy of that data is in the cpu's cache.
|
|
|
|
// And wether if every property's memory of the particle system is continuous,
|
|
|
|
// for the purpose of improving cache hit rate, we should process only one property in one for-loop.
|
|
|
|
// It was proved to be effective especially for low-end devices.
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
|
|
|
{
|
|
|
|
_particleData.timeToLive[i] -= dt;
|
2022-05-26 20:56:56 +08:00
|
|
|
}
|
|
|
|
|
2022-05-27 00:59:48 +08:00
|
|
|
if (_isOpacityFadeInAllocated)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
|
|
|
{
|
|
|
|
_particleData.opacityFadeInDelta[i] += dt;
|
|
|
|
_particleData.opacityFadeInDelta[i] =
|
|
|
|
MIN(_particleData.opacityFadeInDelta[i], _particleData.opacityFadeInLength[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-27 03:53:19 +08:00
|
|
|
if (_isScaleInAllocated)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
|
|
|
{
|
|
|
|
_particleData.scaleInDelta[i] += dt;
|
|
|
|
_particleData.scaleInDelta[i] = MIN(_particleData.scaleInDelta[i], _particleData.scaleInLength[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
if (_isLifeAnimated || _isEmitterAnimated || _isLoopAnimated)
|
|
|
|
{
|
2022-05-27 02:34:15 +08:00
|
|
|
if (_isEmitterAnimated && !_animations.empty())
|
2022-05-22 17:13:17 +08:00
|
|
|
{
|
2022-05-27 02:34:15 +08:00
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2022-05-22 17:13:17 +08:00
|
|
|
{
|
2022-05-26 20:56:56 +08:00
|
|
|
_particleData.animTimeDelta[i] += (_animationTimescaleInd ? pureDt : dt);
|
|
|
|
if (_particleData.animTimeDelta[i] > _particleData.animTimeLength[i])
|
|
|
|
{
|
|
|
|
auto& anim = _animations.at(_particleData.animIndex[i]);
|
2022-06-16 04:04:08 +08:00
|
|
|
float percent = _rng.float01();
|
2022-05-26 20:56:56 +08:00
|
|
|
percent = anim.reverseIndices ? 1.0F - percent : percent;
|
2022-05-22 17:13:17 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
_particleData.animCellIndex[i] = anim.animationIndices[MIN(
|
|
|
|
percent * anim.animationIndices.size(), anim.animationIndices.size() - 1)];
|
|
|
|
_particleData.animTimeDelta[i] = 0;
|
|
|
|
}
|
2022-05-22 17:13:17 +08:00
|
|
|
}
|
2022-05-27 02:34:15 +08:00
|
|
|
}
|
|
|
|
if (_isLifeAnimated && _animations.empty())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2022-05-26 20:56:56 +08:00
|
|
|
{
|
|
|
|
float percent = (_particleData.totalTimeToLive[i] - _particleData.timeToLive[i]) /
|
|
|
|
_particleData.totalTimeToLive[i];
|
|
|
|
percent = _isAnimationReversed ? 1.0F - percent : percent;
|
|
|
|
_particleData.animCellIndex[i] =
|
|
|
|
(unsigned short)MIN(percent * _animIndexCount, _animIndexCount - 1);
|
|
|
|
}
|
2022-05-27 02:34:15 +08:00
|
|
|
}
|
|
|
|
if (_isLifeAnimated && !_animations.empty())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2022-05-26 20:56:56 +08:00
|
|
|
{
|
|
|
|
auto& anim = _animations.at(_particleData.animIndex[i]);
|
2022-05-21 21:48:46 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
float percent = (_particleData.totalTimeToLive[i] - _particleData.timeToLive[i]) /
|
|
|
|
_particleData.totalTimeToLive[i];
|
|
|
|
percent = (!!_isAnimationReversed != !!anim.reverseIndices) ? 1.0F - percent : percent;
|
|
|
|
percent = MAX(0.0F, percent);
|
2022-05-22 01:23:19 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
_particleData.animCellIndex[i] = anim.animationIndices[MIN(percent * anim.animationIndices.size(),
|
|
|
|
anim.animationIndices.size() - 1)];
|
|
|
|
}
|
2022-05-27 02:34:15 +08:00
|
|
|
}
|
|
|
|
if (_isLoopAnimated && !_animations.empty())
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2022-05-26 20:56:56 +08:00
|
|
|
{
|
|
|
|
auto& anim = _animations.at(_particleData.animIndex[i]);
|
2022-05-21 21:48:46 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
_particleData.animTimeDelta[i] += (_animationTimescaleInd ? pureDt : dt);
|
|
|
|
if (_particleData.animTimeDelta[i] >= _particleData.animTimeLength[i])
|
|
|
|
_particleData.animTimeDelta[i] = 0;
|
2022-05-21 21:48:46 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
float percent = _particleData.animTimeDelta[i] / _particleData.animTimeLength[i];
|
|
|
|
percent = anim.reverseIndices ? 1.0F - percent : percent;
|
|
|
|
percent = MAX(0.0F, percent);
|
2022-05-22 01:23:19 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
_particleData.animCellIndex[i] = anim.animationIndices[MIN(percent * anim.animationIndices.size(),
|
|
|
|
anim.animationIndices.size() - 1)];
|
|
|
|
}
|
2022-05-21 21:48:46 +08:00
|
|
|
}
|
2022-05-27 02:34:15 +08:00
|
|
|
if (_isLoopAnimated && _animations.empty())
|
|
|
|
std::fill_n(_particleData.animTimeDelta, _particleCount, 0);
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
if (_particleData.timeToLive[i] <= 0.0f)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
int j = _particleCount - 1;
|
|
|
|
while (j > 0 && _particleData.timeToLive[j] <= 0)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleCount--;
|
|
|
|
j--;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.copyParticle(i, _particleCount - 1);
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// disable the switched particle
|
2015-08-31 17:25:34 +08:00
|
|
|
int currentIndex = _particleData.atlasIndex[i];
|
|
|
|
_batchNode->disableParticle(_atlasIndex + currentIndex);
|
2021-12-25 10:04:45 +08:00
|
|
|
// switch indexes
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.atlasIndex[_particleCount - 1] = currentIndex;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
--_particleCount;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_particleCount == 0 && _isAutoRemoveOnFinish)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
this->unscheduleUpdate();
|
2013-06-15 14:03:30 +08:00
|
|
|
_parent->removeChild(this, true);
|
2012-04-19 14:35:52 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
if (_emitterMode == Mode::GRAVITY)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
particle_point tmp, radial = {0.0f, 0.0f}, tangential;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
// radial acceleration
|
|
|
|
if (_particleData.posx[i] || _particleData.posy[i])
|
|
|
|
{
|
2017-02-06 15:13:20 +08:00
|
|
|
normalize_point(_particleData.posx[i], _particleData.posy[i], &radial);
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
tangential = radial;
|
|
|
|
radial.x *= _particleData.modeA.radialAccel[i];
|
|
|
|
radial.y *= _particleData.modeA.radialAccel[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
// tangential acceleration
|
|
|
|
std::swap(tangential.x, tangential.y);
|
2021-12-25 10:04:45 +08:00
|
|
|
tangential.x *= -_particleData.modeA.tangentialAccel[i];
|
2015-08-31 17:25:34 +08:00
|
|
|
tangential.y *= _particleData.modeA.tangentialAccel[i];
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
// (gravity + radial + tangential) * dt
|
|
|
|
tmp.x = radial.x + tangential.x + modeA.gravity.x;
|
|
|
|
tmp.y = radial.y + tangential.y + modeA.gravity.y;
|
|
|
|
tmp.x *= dt;
|
|
|
|
tmp.y *= dt;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.modeA.dirX[i] += tmp.x;
|
|
|
|
_particleData.modeA.dirY[i] += tmp.y;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
// this is cocos2d-x v3.0
|
|
|
|
// if (_configName.length()>0 && _yCoordFlipped != -1)
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
// this is cocos2d-x v3.0
|
|
|
|
tmp.x = _particleData.modeA.dirX[i] * dt * _yCoordFlipped;
|
|
|
|
tmp.y = _particleData.modeA.dirY[i] * dt * _yCoordFlipped;
|
|
|
|
_particleData.posx[i] += tmp.x;
|
|
|
|
_particleData.posy[i] += tmp.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
|
|
|
{
|
|
|
|
_particleData.modeB.angle[i] += _particleData.modeB.degreesPerSecond[i] * dt;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
|
|
|
{
|
|
|
|
_particleData.modeB.radius[i] += _particleData.modeB.deltaRadius[i] * dt;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_particleData.posx[i] = -cosf(_particleData.modeB.angle[i]) * _particleData.modeB.radius[i];
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_particleData.posy[i] =
|
|
|
|
-sinf(_particleData.modeB.angle[i]) * _particleData.modeB.radius[i] * _yCoordFlipped;
|
2015-08-31 17:25:34 +08:00
|
|
|
}
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
// color r,g,b,a
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
_particleData.colorR[i] += _particleData.deltaColorR[i] * dt;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
_particleData.colorG[i] += _particleData.deltaColorG[i] * dt;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
_particleData.colorB[i] += _particleData.deltaColorB[i] * dt;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
_particleData.colorA[i] += _particleData.deltaColorA[i] * dt;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// size
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
_particleData.size[i] += (_particleData.deltaSize[i] * dt);
|
|
|
|
_particleData.size[i] = MAX(0, _particleData.size[i]);
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
// angle
|
|
|
|
for (int i = 0; i < _particleCount; ++i)
|
2015-08-31 17:25:34 +08:00
|
|
|
{
|
|
|
|
_particleData.rotation[i] += _particleData.deltaRotation[i] * dt;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
updateParticleQuads();
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = false;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2015-08-31 17:25:34 +08:00
|
|
|
|
2022-05-23 21:04:09 +08:00
|
|
|
// update and send gl buffer only when this node is visible.
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_visible && !_batchNode)
|
2012-04-24 15:02:18 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
postStep();
|
2012-04-24 15:02:18 +08:00
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_PROFILER_STOP_CATEGORY(kProfilerCategoryParticles, "CCParticleSystem - update");
|
2012-03-14 14:55:17 +08:00
|
|
|
}
|
2010-08-23 14:57:37 +08:00
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
void ParticleSystem::updateWithNoTime()
|
2012-03-14 14:55:17 +08:00
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
this->update(0.0f);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2015-08-31 17:25:34 +08:00
|
|
|
void ParticleSystem::updateParticleQuads()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
// should be overridden
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::postStep()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2012-09-17 15:02:24 +08:00
|
|
|
// should be overridden
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
// ParticleSystem - Texture protocol
|
|
|
|
void ParticleSystem::setTexture(Texture2D* var)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if (_texture != var)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2022-07-15 19:17:01 +08:00
|
|
|
AX_SAFE_RETAIN(var);
|
|
|
|
AX_SAFE_RELEASE(_texture);
|
2013-06-15 14:03:30 +08:00
|
|
|
_texture = var;
|
2012-06-08 14:11:48 +08:00
|
|
|
updateBlendFunc();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::updateBlendFunc()
|
2012-06-08 14:11:48 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(!_batchNode, "Can't change blending functions when the particle is being batched");
|
2010-12-27 17:39:15 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_texture)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
bool premultiplied = _texture->hasPremultipliedAlpha();
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
_opacityModifyRGB = false;
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
if (_texture && (_blendFunc.src == AX_BLEND_SRC && _blendFunc.dst == AX_BLEND_DST))
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (premultiplied)
|
2012-06-12 01:43:07 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_opacityModifyRGB = true;
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2012-06-12 01:43:07 +08:00
|
|
|
}
|
|
|
|
}
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Texture2D* ParticleSystem::getTexture() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _texture;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleSystem - Additive Blending
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setBlendAdditive(bool additive)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (additive)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ADDITIVE;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_texture && !_texture->hasPremultipliedAlpha())
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2013-07-26 04:36:19 +08:00
|
|
|
_blendFunc = BlendFunc::ALPHA_PREMULTIPLIED;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::isBlendAdditive() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return (_blendFunc.src == backend::BlendFactor::SRC_ALPHA && _blendFunc.dst == backend::BlendFactor::ONE);
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// ParticleSystem - Properties of Gravity Mode
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setTangentialAccel(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.tangentialAccel = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getTangentialAccel() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.tangentialAccel;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setTangentialAccelVar(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.tangentialAccelVar = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getTangentialAccelVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.tangentialAccelVar;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRadialAccel(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.radialAccel = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRadialAccel() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.radialAccel;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRadialAccelVar(float t)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.radialAccelVar = t;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRadialAccelVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.radialAccelVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotationIsDir(bool t)
|
2013-01-21 17:39:48 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2013-01-21 17:39:48 +08:00
|
|
|
modeA.rotationIsDir = t;
|
|
|
|
}
|
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::getRotationIsDir() const
|
2013-01-21 17:39:48 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2013-01-21 17:39:48 +08:00
|
|
|
return modeA.rotationIsDir;
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
void ParticleSystem::setGravity(const Vec2& g)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.gravity = g;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2014-05-15 01:07:09 +08:00
|
|
|
const Vec2& ParticleSystem::getGravity()
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.gravity;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setSpeed(float speed)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.speed = speed;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getSpeed() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.speed;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setSpeedVar(float speedVar)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeA.speedVar = speedVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getSpeedVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::GRAVITY, "Particle Mode should be Gravity");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeA.speedVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParticleSystem - Properties of Radius Mode
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setStartRadius(float startRadius)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.startRadius = startRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getStartRadius() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.startRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setStartRadiusVar(float startRadiusVar)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.startRadiusVar = startRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getStartRadiusVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.startRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setEndRadius(float endRadius)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.endRadius = endRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getEndRadius() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.endRadius;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setEndRadiusVar(float endRadiusVar)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.endRadiusVar = endRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getEndRadiusVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.endRadiusVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotatePerSecond(float degrees)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.rotatePerSecond = degrees;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRotatePerSecond() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.rotatePerSecond;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotatePerSecondVar(float degrees)
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
modeB.rotatePerSecondVar = degrees;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
float ParticleSystem::getRotatePerSecondVar() const
|
2010-12-27 17:39:15 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(_emitterMode == Mode::RADIUS, "Particle Mode should be Radius");
|
2012-04-19 14:35:52 +08:00
|
|
|
return modeB.rotatePerSecondVar;
|
2010-12-27 17:39:15 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::isActive() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _isActive;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2022-05-26 20:56:56 +08:00
|
|
|
void ParticleSystem::useHSV(bool hsv)
|
|
|
|
{
|
|
|
|
if (hsv && !allocHSVMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!hsv)
|
|
|
|
deallocHSVMem();
|
|
|
|
};
|
|
|
|
|
2022-05-27 00:59:48 +08:00
|
|
|
void ParticleSystem::setSpawnFadeIn(float time)
|
|
|
|
{
|
|
|
|
if (time != 0.0F && !allocOpacityFadeInMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_spawnFadeIn = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::setSpawnFadeInVar(float time)
|
|
|
|
{
|
|
|
|
if (time != 0.0F && !allocOpacityFadeInMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_spawnFadeInVar = time;
|
|
|
|
}
|
|
|
|
|
2022-05-27 03:53:19 +08:00
|
|
|
void ParticleSystem::setSpawnScaleIn(float time)
|
|
|
|
{
|
|
|
|
if (time != 0.0F && !allocScaleInMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_spawnScaleIn = time;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::setSpawnScaleInVar(float time)
|
|
|
|
{
|
|
|
|
if (time != 0.0F && !allocScaleInMem())
|
|
|
|
return;
|
|
|
|
|
|
|
|
_spawnScaleInVar = time;
|
|
|
|
}
|
|
|
|
|
2013-09-08 11:26:38 +08:00
|
|
|
int ParticleSystem::getTotalParticles() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _totalParticles;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-09-08 11:26:38 +08:00
|
|
|
void ParticleSystem::setTotalParticles(int var)
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(var <= _allocatedParticles, "Particle: resizing particle array only supported for quads");
|
2013-06-15 14:03:30 +08:00
|
|
|
_totalParticles = var;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-05 16:49:22 +08:00
|
|
|
const BlendFunc& ParticleSystem::getBlendFunc() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _blendFunc;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void ParticleSystem::setBlendFunc(const BlendFunc& blendFunc)
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_blendFunc.src != blendFunc.src || _blendFunc.dst != blendFunc.dst)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_blendFunc = blendFunc;
|
2012-06-12 01:43:07 +08:00
|
|
|
this->updateBlendFunc();
|
2012-06-08 14:11:48 +08:00
|
|
|
}
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-07-07 13:01:21 +08:00
|
|
|
bool ParticleSystem::isAutoRemoveOnFinish() const
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _isAutoRemoveOnFinish;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setAutoRemoveOnFinish(bool var)
|
2011-03-19 10:07:16 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_isAutoRemoveOnFinish = var;
|
2011-03-19 10:07:16 +08:00
|
|
|
}
|
2012-04-18 18:43:45 +08:00
|
|
|
|
2012-04-19 14:35:52 +08:00
|
|
|
// ParticleSystem - methods for batchNode rendering
|
|
|
|
|
2019-07-22 09:38:46 +08:00
|
|
|
ParticleBatchNode* ParticleSystem::getBatchNode() const
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
return _batchNode;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setBatchNode(ParticleBatchNode* batchNode)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
if (_batchNode != batchNode)
|
|
|
|
{
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_batchNode = batchNode; // weak reference
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
if (batchNode)
|
|
|
|
{
|
|
|
|
// each particle needs a unique index
|
2013-09-08 11:26:38 +08:00
|
|
|
for (int i = 0; i < _totalParticles; i++)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2015-08-31 17:25:34 +08:00
|
|
|
_particleData.atlasIndex[i] = i;
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
// don't use a transform matrix, this is faster
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setScale(float s)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setScale(s);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setRotation(float newRotation)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setRotation(newRotation);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setScaleX(float newScaleX)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setScaleX(newScaleX);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
|
|
|
|
2013-06-20 14:13:12 +08:00
|
|
|
void ParticleSystem::setScaleY(float newScaleY)
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_transformSystemDirty = true;
|
2013-06-20 14:13:12 +08:00
|
|
|
Node::setScaleY(newScaleY);
|
2012-04-19 14:35:52 +08:00
|
|
|
}
|
2012-03-14 14:55:17 +08:00
|
|
|
|
2016-01-12 17:21:01 +08:00
|
|
|
void ParticleSystem::start()
|
|
|
|
{
|
|
|
|
resetSystem();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::stop()
|
|
|
|
{
|
|
|
|
stopSystem();
|
|
|
|
}
|
2016-06-15 18:31:10 +08:00
|
|
|
|
|
|
|
bool ParticleSystem::isPaused() const
|
|
|
|
{
|
|
|
|
return _paused;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::pauseEmissions()
|
|
|
|
{
|
|
|
|
_paused = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ParticleSystem::resumeEmissions()
|
|
|
|
{
|
|
|
|
_paused = false;
|
|
|
|
}
|
|
|
|
|
2022-05-24 05:49:17 +08:00
|
|
|
float ParticleSystem::getFixedFPS()
|
2022-05-23 21:04:09 +08:00
|
|
|
{
|
2022-05-24 05:49:17 +08:00
|
|
|
return _fixedFPS;
|
2022-05-23 21:04:09 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 05:49:17 +08:00
|
|
|
void ParticleSystem::setFixedFPS(float frameRate)
|
2022-05-23 21:04:09 +08:00
|
|
|
{
|
2022-05-24 05:49:17 +08:00
|
|
|
_fixedFPS = frameRate;
|
2022-05-23 21:04:09 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 05:49:17 +08:00
|
|
|
float ParticleSystem::getTimeScale()
|
2022-05-24 05:46:38 +08:00
|
|
|
{
|
2022-05-24 05:49:17 +08:00
|
|
|
return _timeScale;
|
2022-05-24 05:46:38 +08:00
|
|
|
}
|
|
|
|
|
2022-05-24 05:49:17 +08:00
|
|
|
void ParticleSystem::setTimeScale(float scale)
|
2022-05-24 05:46:38 +08:00
|
|
|
{
|
2022-05-24 05:49:17 +08:00
|
|
|
_timeScale = scale;
|
2022-05-24 05:46:38 +08:00
|
|
|
}
|
|
|
|
|
2022-06-12 05:58:01 +08:00
|
|
|
static ParticleEmissionMaskCache* emissionMaskCache;
|
|
|
|
|
|
|
|
ParticleEmissionMaskCache* ParticleEmissionMaskCache::getInstance()
|
|
|
|
{
|
|
|
|
if (emissionMaskCache == nullptr)
|
|
|
|
{
|
|
|
|
emissionMaskCache = new ParticleEmissionMaskCache();
|
|
|
|
return emissionMaskCache;
|
|
|
|
}
|
|
|
|
return emissionMaskCache;
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
void ParticleEmissionMaskCache::bakeEmissionMask(std::string_view maskId,
|
2022-06-12 05:58:01 +08:00
|
|
|
std::string_view texturePath,
|
|
|
|
float alphaThreshold,
|
2022-06-12 20:57:11 +08:00
|
|
|
bool inverted,
|
|
|
|
int inbetweenSamples)
|
2022-06-12 05:58:01 +08:00
|
|
|
{
|
|
|
|
auto img = new Image();
|
|
|
|
img->Image::initWithImageFile(texturePath);
|
|
|
|
img->autorelease();
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(img, "image texture was nullptr.");
|
2022-06-16 20:25:43 +08:00
|
|
|
bakeEmissionMask(maskId, img, alphaThreshold, inverted, inbetweenSamples);
|
2022-06-12 05:58:01 +08:00
|
|
|
}
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
void ParticleEmissionMaskCache::bakeEmissionMask(std::string_view maskId,
|
2022-06-12 05:58:01 +08:00
|
|
|
Image* imageTexture,
|
|
|
|
float alphaThreshold,
|
2022-06-12 20:57:11 +08:00
|
|
|
bool inverted,
|
|
|
|
int inbetweenSamples)
|
2022-06-12 05:58:01 +08:00
|
|
|
{
|
|
|
|
auto img = imageTexture;
|
2022-07-16 10:43:05 +08:00
|
|
|
AXASSERT(img, "image texture was nullptr.");
|
|
|
|
AXASSERT(img->hasAlpha(), "image data should contain an alpha channel.");
|
2022-06-12 05:58:01 +08:00
|
|
|
|
|
|
|
vector<Vec2> points;
|
|
|
|
|
|
|
|
auto data = img->getData();
|
|
|
|
auto w = img->getWidth();
|
|
|
|
auto h = img->getHeight();
|
|
|
|
|
2022-06-12 20:57:11 +08:00
|
|
|
for (int y = 0; y < h; y++)
|
|
|
|
for (int x = 0; x < w; x++)
|
|
|
|
{
|
|
|
|
if (inbetweenSamples > 1)
|
|
|
|
{
|
|
|
|
float a = data[(y * w + x) * 4 + 3] / 255.0F;
|
|
|
|
if (a >= alphaThreshold && !inverted)
|
|
|
|
for (float i = 0; i < 1.0F; i += 1.0F / inbetweenSamples)
|
2022-08-09 11:51:07 +08:00
|
|
|
points.emplace_back(Vec2{float(x + i), float(h - y + i)});
|
2022-06-12 20:57:11 +08:00
|
|
|
if (a < alphaThreshold && inverted)
|
|
|
|
for (float i = 0; i < 1.0F; i += 1.0F / inbetweenSamples)
|
2022-08-09 11:51:07 +08:00
|
|
|
points.emplace_back(Vec2{float(x + i), float(h - y + i)});
|
2022-06-12 20:57:11 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
float a = data[(y * w + x) * 4 + 3] / 255.0F;
|
|
|
|
if (a >= alphaThreshold && !inverted)
|
2022-08-09 11:51:07 +08:00
|
|
|
points.emplace_back(Vec2{float(x), float(h - y)});
|
2022-06-12 20:57:11 +08:00
|
|
|
if (a < alphaThreshold && inverted)
|
2022-08-09 11:51:07 +08:00
|
|
|
points.emplace_back(Vec2{float(x), float(h - y)});
|
2022-06-12 20:57:11 +08:00
|
|
|
}
|
|
|
|
}
|
2022-06-12 05:58:01 +08:00
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
auto fourccId = utils::fourccValue(maskId);
|
|
|
|
|
|
|
|
auto iter = this->masks.find(fourccId);
|
2022-06-12 05:58:01 +08:00
|
|
|
if (iter == this->masks.end())
|
2022-06-16 20:25:43 +08:00
|
|
|
iter = this->masks.emplace(fourccId, ParticleEmissionMaskDescriptor{}).first;
|
2022-06-12 05:58:01 +08:00
|
|
|
|
|
|
|
ParticleEmissionMaskDescriptor desc;
|
|
|
|
desc.size = {float(w), float(h)};
|
|
|
|
desc.points = std::move(points);
|
|
|
|
|
|
|
|
iter->second = desc;
|
2022-06-12 08:40:13 +08:00
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Particle emission mask '%u' baked (%dx%d), %zu samples generated taking %.2fmb of memory.",
|
2022-07-12 22:57:45 +08:00
|
|
|
(unsigned int)htonl(fourccId), w, h, desc.points.size(), desc.points.size() * 8 / 1e+6);
|
2022-06-16 20:25:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const ParticleEmissionMaskDescriptor& ParticleEmissionMaskCache::getEmissionMask(uint32_t fourccId)
|
|
|
|
{
|
|
|
|
auto iter = this->masks.find(fourccId);
|
|
|
|
if (iter == this->masks.end())
|
|
|
|
{
|
|
|
|
iter = this->masks.emplace(fourccId, ParticleEmissionMaskDescriptor{}).first;
|
|
|
|
iter->second.size = {float(1), float(1)};
|
|
|
|
iter->second.points = {{0, 0}};
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
return iter->second;
|
2022-06-12 05:58:01 +08:00
|
|
|
}
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
const ParticleEmissionMaskDescriptor& ParticleEmissionMaskCache::getEmissionMask(std::string_view maskId)
|
2022-06-12 05:58:01 +08:00
|
|
|
{
|
2022-06-16 20:25:43 +08:00
|
|
|
auto fourccId = utils::fourccValue(maskId);
|
|
|
|
|
|
|
|
auto iter = this->masks.find(fourccId);
|
2022-06-12 05:58:01 +08:00
|
|
|
if (iter == this->masks.end())
|
|
|
|
{
|
2022-06-16 20:25:43 +08:00
|
|
|
iter = this->masks.emplace(fourccId, ParticleEmissionMaskDescriptor{}).first;
|
2022-06-12 20:15:51 +08:00
|
|
|
iter->second.size = {float(1), float(1)};
|
|
|
|
iter->second.points = {{0, 0}};
|
|
|
|
return iter->second;
|
2022-06-12 05:58:01 +08:00
|
|
|
}
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
2022-06-16 20:25:43 +08:00
|
|
|
void ParticleEmissionMaskCache::removeMask(std::string_view maskId)
|
2022-06-12 05:58:01 +08:00
|
|
|
{
|
2022-06-16 20:25:43 +08:00
|
|
|
this->masks.erase(utils::fourccValue(maskId));
|
2022-06-12 05:58:01 +08:00
|
|
|
}
|
|
|
|
|
2022-06-12 20:57:11 +08:00
|
|
|
void ParticleEmissionMaskCache::removeAllMasks()
|
2022-06-12 05:58:01 +08:00
|
|
|
{
|
|
|
|
this->masks.clear();
|
|
|
|
}
|
|
|
|
|
2022-07-12 22:57:45 +08:00
|
|
|
NS_AX_END
|