-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[NPE] Particles noise #17500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
VicenteCartas
wants to merge
7
commits into
BabylonJS:master
Choose a base branch
from
VicenteCartas:particles-noise
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
[NPE] Particles noise #17500
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
aa25d01
WIP
VicenteCartas 11f2e4e
Merge branch 'master' into particles-noise
VicenteCartas 77b8ac1
Merge branch 'master' into particles-noise
VicenteCartas a65ce50
Merge branch 'master' into particles-noise
VicenteCartas f9db462
Adding noise support
VicenteCartas dd39672
nit
VicenteCartas c5e80e7
Fix ES6 build
VicenteCartas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
packages/dev/core/src/Particles/Node/Blocks/Update/updateNoiseBlock.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,137 @@ | ||
| import type { Nullable } from "core/types"; | ||
| import type { Particle } from "core/Particles/particle"; | ||
| import type { ThinParticleSystem } from "core/Particles/thinParticleSystem"; | ||
| import type { NodeParticleConnectionPoint } from "core/Particles/Node/nodeParticleBlockConnectionPoint"; | ||
| import type { NodeParticleBuildState } from "core/Particles/Node/nodeParticleBuildState"; | ||
| import type { INodeParticleTextureData, ParticleTextureSourceBlock } from "core/Particles/Node/Blocks/particleSourceTextureBlock"; | ||
|
|
||
| import { TmpVectors, Vector3 } from "core/Maths/math.vector"; | ||
| import { RegisterClass } from "core/Misc/typeStore"; | ||
| import { NodeParticleBlock } from "core/Particles/Node/nodeParticleBlock"; | ||
| import { NodeParticleBlockConnectionPointTypes } from "core/Particles/Node/Enums/nodeParticleBlockConnectionPointTypes"; | ||
| import { _ConnectAtTheEnd } from "core/Particles/Queue/executionQueue"; | ||
|
|
||
| /** | ||
| * Block used to update particle position based on a noise texture | ||
| */ | ||
| export class UpdateNoiseBlock extends NodeParticleBlock { | ||
| /** | ||
| * Create a new UpdateNoiseBlock | ||
| * @param name defines the block name | ||
| */ | ||
| public constructor(name: string) { | ||
| super(name); | ||
|
|
||
| this.registerInput("particle", NodeParticleBlockConnectionPointTypes.Particle); | ||
| this.registerInput("noiseTexture", NodeParticleBlockConnectionPointTypes.Texture); | ||
| this.registerInput("strength", NodeParticleBlockConnectionPointTypes.Vector3, true, new Vector3(100, 100, 100)); | ||
| this.registerOutput("output", NodeParticleBlockConnectionPointTypes.Particle); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the particle component | ||
| */ | ||
| public get particle(): NodeParticleConnectionPoint { | ||
| return this._inputs[0]; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the noiseTexture input component | ||
| */ | ||
| public get noiseTexture(): NodeParticleConnectionPoint { | ||
| return this._inputs[1]; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the strength input component | ||
| */ | ||
| public get strength(): NodeParticleConnectionPoint { | ||
| return this._inputs[2]; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the output component | ||
| */ | ||
| public get output(): NodeParticleConnectionPoint { | ||
| return this._outputs[0]; | ||
| } | ||
|
|
||
| /** | ||
| * Gets the current class name | ||
| * @returns the class name | ||
| */ | ||
| public override getClassName() { | ||
| return "UpdateNoiseBlock"; | ||
| } | ||
|
|
||
| /** | ||
| * Builds the block | ||
| * @param state defines the current build state | ||
| */ | ||
| public override _build(state: NodeParticleBuildState) { | ||
| const system = this.particle.getConnectedValue(state) as ThinParticleSystem; | ||
|
|
||
| const strength = this.strength.getConnectedValue(state) as Vector3; | ||
| if (!strength) { | ||
| return; | ||
| } | ||
|
|
||
| const noiseTexture = this.noiseTexture.connectedPoint?.ownerBlock as ParticleTextureSourceBlock; | ||
| if (!noiseTexture) { | ||
| return; | ||
| } | ||
|
|
||
| let noiseData: INodeParticleTextureData; | ||
|
|
||
| // eslint-disable-next-line github/no-then | ||
| void noiseTexture.extractTextureContentAsync().then((textureContent: Nullable<INodeParticleTextureData>) => { | ||
| if (!textureContent) { | ||
| return; | ||
| } | ||
| noiseData = textureContent; | ||
| }); | ||
|
|
||
| const processNoise = (particle: Particle) => { | ||
| if (!noiseData) { | ||
| // If the noise data is not ready, we skip processing | ||
| return; | ||
| } | ||
|
|
||
| if (!particle._randomNoiseCoordinates1) { | ||
| particle._randomNoiseCoordinates1 = new Vector3(Math.random(), Math.random(), Math.random()); | ||
| } | ||
|
|
||
| if (!particle._randomNoiseCoordinates2) { | ||
| particle._randomNoiseCoordinates2 = new Vector3(Math.random(), Math.random(), Math.random()); | ||
| } | ||
|
|
||
| const fetchedColorR = system._fetchR(particle._randomNoiseCoordinates1.x, particle._randomNoiseCoordinates1.y, noiseData.width, noiseData.height, noiseData.data); | ||
| const fetchedColorG = system._fetchR(particle._randomNoiseCoordinates1.z, particle._randomNoiseCoordinates2.x, noiseData.width, noiseData.height, noiseData.data); | ||
| const fetchedColorB = system._fetchR(particle._randomNoiseCoordinates2.y, particle._randomNoiseCoordinates2.z, noiseData.width, noiseData.height, noiseData.data); | ||
|
|
||
| const force = TmpVectors.Vector3[0]; | ||
| const scaledForce = TmpVectors.Vector3[1]; | ||
|
|
||
| force.copyFromFloats((2 * fetchedColorR - 1) * strength.x, (2 * fetchedColorG - 1) * strength.y, (2 * fetchedColorB - 1) * strength.z); | ||
|
|
||
| force.scaleToRef(system._tempScaledUpdateSpeed, scaledForce); | ||
| particle.direction.addInPlace(scaledForce); | ||
| }; | ||
|
|
||
| const noiseProcessing = { | ||
| process: processNoise, | ||
| previousItem: null, | ||
| nextItem: null, | ||
| }; | ||
|
|
||
| if (system._updateQueueStart) { | ||
| _ConnectAtTheEnd(noiseProcessing, system._updateQueueStart); | ||
| } else { | ||
| system._updateQueueStart = noiseProcessing; | ||
| } | ||
|
|
||
| this.output._storedValue = system; | ||
| } | ||
| } | ||
|
|
||
| RegisterClass("BABYLON.UpdateNoiseBlock", UpdateNoiseBlock); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be call on every frame right? Unless I'm wrong (which is totally possible). This will be called just once and never change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this needs to go inside the process noise function