-
-
Notifications
You must be signed in to change notification settings - Fork 36k
TSL: Introduce RaymarchingBox
and raymarchingTexture3D
#30495
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2153496
raymarching box
sunag 9c9039c
revisions
sunag 12befb7
Update Raymarching.js
sunag 75075a3
add `raymarchingTexture3D`
sunag f29e497
cleanup
sunag f65f7cf
Update Raymarching.js
sunag e11a494
cleanup
sunag 0132ae7
Update Raymarching.js
sunag 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { varying, vec4, modelWorldMatrixInverse, cameraPosition, positionGeometry, float, Fn, Loop, max, min, vec2, vec3, smoothstep, If, Break } from 'three/tsl'; | ||
|
||
const hitBox = /*@__PURE__*/ Fn( ( { orig, dir } ) => { | ||
|
||
const box_min = vec3( - 0.5 ); | ||
const box_max = vec3( 0.5 ); | ||
|
||
const inv_dir = dir.reciprocal(); | ||
|
||
const tmin_tmp = box_min.sub( orig ).mul( inv_dir ); | ||
const tmax_tmp = box_max.sub( orig ).mul( inv_dir ); | ||
|
||
const tmin = min( tmin_tmp, tmax_tmp ); | ||
const tmax = max( tmin_tmp, tmax_tmp ); | ||
|
||
const t0 = max( tmin.x, max( tmin.y, tmin.z ) ); | ||
const t1 = min( tmax.x, min( tmax.y, tmax.z ) ); | ||
|
||
return vec2( t0, t1 ); | ||
|
||
} ); | ||
|
||
/** | ||
* Performs raymarching box-area using the specified number of steps and a callback function. | ||
* | ||
* ```js | ||
* RaymarchingBox( count, ( { positionRay } ) => { | ||
* | ||
* } ); | ||
* ``` | ||
* | ||
* @tsl | ||
* @function | ||
* @param {Number|Node} steps - The number of steps for raymarching. | ||
* @param {Function|FunctionNode} callback - The callback function to execute at each step. | ||
* @returns {void} | ||
*/ | ||
export const RaymarchingBox = ( steps, callback ) => { | ||
|
||
const vOrigin = varying( vec3( modelWorldMatrixInverse.mul( vec4( cameraPosition, 1.0 ) ) ) ); | ||
const vDirection = varying( positionGeometry.sub( vOrigin ) ); | ||
|
||
const rayDir = vDirection.normalize(); | ||
const bounds = vec2( hitBox( { orig: vOrigin, dir: rayDir } ) ).toVar(); | ||
|
||
bounds.x.greaterThan( bounds.y ).discard(); | ||
|
||
bounds.assign( vec2( max( bounds.x, 0.0 ), bounds.y ) ); | ||
|
||
const inc = vec3( rayDir.abs().reciprocal() ).toVar(); | ||
const delta = float( min( inc.x, min( inc.y, inc.z ) ) ).toVar( 'rayDelta' ); // used 'rayDelta' name in loop | ||
|
||
delta.divAssign( float( steps ) ); | ||
|
||
const positionRay = vec3( vOrigin.add( bounds.x.mul( rayDir ) ) ).toVar(); | ||
|
||
Loop( { type: 'float', start: bounds.x, end: bounds.y, update: '+= rayDelta' }, () => { | ||
|
||
callback( { positionRay } ); | ||
|
||
positionRay.addAssign( rayDir.mul( delta ) ); | ||
|
||
} ); | ||
|
||
}; | ||
|
||
/** | ||
* Performs raymarching on the specified 3D texture. | ||
* | ||
* @tsl | ||
* @function | ||
* @param {Object} params - The parameters for the function. | ||
* @param {Texture|Node} params.texture - The 3D texture to sample. | ||
* @param {Number|Node} [params.range=0.1] - The range for the smoothstep function. | ||
* @param {Number|Node} [params.threshold=0.25] - The threshold for the smoothstep function. | ||
* @param {Number|Node} [params.opacity=0.25] - The opacity value for the final color. | ||
* @param {Number|Node} [params.steps=100] - The number of steps for raymarching. | ||
* @returns {Function} The generated function that performs raymarching on the 3D texture. | ||
*/ | ||
export const raymarchingTexture3D = Fn( ( { | ||
texture, | ||
range = float( 0.1 ), | ||
threshold = float( 0.25 ), | ||
opacity = float( 0.25 ), | ||
steps = float( 100 ) | ||
} ) => { | ||
|
||
const finalColor = vec4( 0 ).toVar(); | ||
|
||
RaymarchingBox( steps, ( { positionRay } ) => { | ||
|
||
const mapValue = float( texture.sample( positionRay.add( 0.5 ) ).r ).toVar(); | ||
|
||
mapValue.assign( smoothstep( threshold.sub( range ), threshold.add( range ), mapValue ).mul( opacity ) ); | ||
|
||
const shading = texture.sample( positionRay.add( vec3( - 0.01 ) ) ).r.sub( texture.sample( positionRay.add( vec3( 0.01 ) ) ).r ); | ||
|
||
const col = shading.mul( 3.0 ).add( positionRay.x.add( positionRay.y ).mul( 0.25 ) ).add( 0.2 ); | ||
|
||
finalColor.rgb.addAssign( finalColor.a.oneMinus().mul( mapValue ).mul( col ) ); | ||
|
||
finalColor.a.addAssign( finalColor.a.oneMinus().mul( mapValue ) ); | ||
|
||
If( finalColor.a.greaterThanEqual( 0.95 ), () => { | ||
|
||
Break(); | ||
|
||
} ); | ||
|
||
} ); | ||
|
||
return finalColor; | ||
|
||
} ); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.