forked from mrdoob/three.js
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
293 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
79 changes: 79 additions & 0 deletions
79
examples/jsm/renderers/nodes/accessors/SkinningPositionNode.js
This file contains 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,79 @@ | ||
import Node from '../core/Node.js'; | ||
import AttributeNode from '../core/AttributeNode.js'; | ||
import FloatNode from '../inputs/FloatNode.js'; | ||
import Matrix4Node from '../inputs/Matrix4Node.js'; | ||
import TextureNode from '../inputs/TextureNode.js'; | ||
import PositionNode from './PositionNode.js'; | ||
|
||
import { NodeUpdateType } from '../core/constants.js'; | ||
import { SkinningPosition } from '../functions/Common.js'; | ||
|
||
class SkinningPositionNode extends Node { | ||
|
||
constructor( skinnedMesh ) { | ||
|
||
super( 'vec3' ); | ||
|
||
this.skinnedMesh = skinnedMesh; | ||
|
||
this.updateType = NodeUpdateType.Object; | ||
|
||
// | ||
|
||
this.bindMatrixNode = new Matrix4Node( skinnedMesh.bindMatrix ); | ||
this.bindMatrixInverseNode = new Matrix4Node( skinnedMesh.bindMatrixInverse ); | ||
this.boneTextureSizeNode = new FloatNode(); | ||
this.boneTextureNode = new TextureNode(); | ||
this.position = new PositionNode(); | ||
|
||
} | ||
|
||
generate( builder, output ) { | ||
|
||
const type = this.getType( builder ); | ||
const nodeData = builder.getDataFromNode( this, builder.shaderStage ); | ||
|
||
let getSkinningCallNode = nodeData.getSkinningCallNode; | ||
|
||
if ( getSkinningCallNode === undefined ) { | ||
|
||
const skinIndexNode = new AttributeNode( 'skinIndex', 'uvec4' ); | ||
const skinWeightNode = new AttributeNode( 'skinWeight', 'vec4' ); | ||
|
||
getSkinningCallNode = SkinningPosition.call( { | ||
index: skinIndexNode, | ||
weight: skinWeightNode, | ||
position: this.position, | ||
bindMatrix: this.bindMatrixNode, | ||
bindMatrixInverse: this.bindMatrixInverseNode, | ||
boneTexture: this.boneTextureNode, | ||
boneSampler: this.boneTextureNode, | ||
boneTextureSize: this.boneTextureSizeNode | ||
} ); | ||
|
||
nodeData.getSkinningCallNode = getSkinningCallNode; | ||
|
||
} | ||
|
||
const skinningSnipped = getSkinningCallNode.build( builder, type ); | ||
|
||
return builder.format( skinningSnipped, type, output ); | ||
|
||
} | ||
|
||
update() { | ||
|
||
const skeleton = this.skinnedMesh.skeleton; | ||
|
||
skeleton.update(); | ||
|
||
if ( skeleton.boneTexture === null ) skeleton.computeBoneTexture(); | ||
|
||
this.boneTextureSizeNode.value = skeleton.boneTextureSize; | ||
this.boneTextureNode.value = skeleton.boneTexture; | ||
|
||
} | ||
|
||
} | ||
|
||
export default SkinningPositionNode; |
This file contains 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 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,34 @@ | ||
import FunctionNode from '../core/FunctionNode.js'; | ||
|
||
export const BoneMatrix = new FunctionNode( ` | ||
mat4 getBoneMatrix( const in float i, const in texture2D boneTexture, const in sampler boneSampler, const in float boneTextureSize ) { | ||
float j = i * 4.0; | ||
float x = mod( j, float( boneTextureSize ) ); | ||
float y = floor( j / float( boneTextureSize ) ); | ||
float dx = 1.0 / float( boneTextureSize ); | ||
float dy = 1.0 / float( boneTextureSize ); | ||
y = dy * ( y + 0.5 ); | ||
vec4 v1 = texture( sampler2D( boneTexture, boneSampler ), vec2( dx * ( x + 0.5 ), y ) ); | ||
vec4 v2 = texture( sampler2D( boneTexture, boneSampler ), vec2( dx * ( x + 1.5 ), y ) ); | ||
vec4 v3 = texture( sampler2D( boneTexture, boneSampler ), vec2( dx * ( x + 2.5 ), y ) ); | ||
vec4 v4 = texture( sampler2D( boneTexture, boneSampler ), vec2( dx * ( x + 3.5 ), y ) ); | ||
mat4 bone = mat4( v1, v2, v3, v4 ); | ||
return bone; | ||
}` | ||
); | ||
|
||
export const SkinningPosition = new FunctionNode( ` | ||
vec3 getSkinningPosition( const in vec4 index, const in vec4 weight, const in vec3 position, const in mat4 bindMatrix, const in mat4 bindMatrixInverse, const in texture2D boneTexture, const in sampler boneSampler, const in float boneTextureSize ) { | ||
mat4 boneMatX = getBoneMatrix( index.x, boneTexture, boneSampler, boneTextureSize ); | ||
mat4 boneMatY = getBoneMatrix( index.y, boneTexture, boneSampler, boneTextureSize ); | ||
mat4 boneMatZ = getBoneMatrix( index.z, boneTexture, boneSampler, boneTextureSize ); | ||
mat4 boneMatW = getBoneMatrix( index.w, boneTexture, boneSampler, boneTextureSize ); | ||
vec4 skinVertex = bindMatrix * vec4( position, 1.0 ); | ||
vec4 skinned = vec4( 0.0 ); | ||
skinned += boneMatX * skinVertex * weight.x; | ||
skinned += boneMatY * skinVertex * weight.y; | ||
skinned += boneMatZ * skinVertex * weight.z; | ||
skinned += boneMatW * skinVertex * weight.w; | ||
return ( bindMatrixInverse * skinned ).xyz; | ||
}` | ||
).setIncludes( [ BoneMatrix ] ); |
This file contains 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 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 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 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 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 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.