Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions examples/webgpu_compute_geometry.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<script type="module">

import * as THREE from 'three';
import { vec3, vec4, storage, Fn, If, uniform, instanceIndex, objectWorldMatrix, color, screenUV, attribute } from 'three/tsl';
import { vec4, storage, Fn, If, uniform, instanceIndex, objectWorldMatrix, color, screenUV, attribute } from 'three/tsl';

import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

Expand All @@ -51,25 +51,24 @@

const count = geometry.attributes.position.count;

const speedBufferAttribute = new THREE.StorageBufferAttribute( count, 4 );

// replace geometry attributes for storage buffer attributes

const positionBaseAttribute = geometry.attributes.position;
const positionStorageBufferAttribute = new THREE.StorageBufferAttribute( count, 4 );
const positionStorageBufferAttribute = new THREE.StorageBufferAttribute( count, 3 );
const speedBufferAttribute = new THREE.StorageBufferAttribute( count, 3 );

geometry.setAttribute( 'storagePosition', positionStorageBufferAttribute );

// compute ( jelly )
// attributes

const positionAttribute = storage( positionBaseAttribute, 'vec3', count ).toReadOnly();
const positionStorageAttribute = storage( positionStorageBufferAttribute, 'vec4', count );
const positionStorageAttribute = storage( positionStorageBufferAttribute, 'vec3', count );

const speedAttribute = storage( speedBufferAttribute, 'vec4', count );
const speedAttribute = storage( speedBufferAttribute, 'vec3', count );

// vectors

const basePosition = vec3( positionAttribute.element( instanceIndex ) );
const basePosition = positionAttribute.element( instanceIndex );
const currentPosition = positionStorageAttribute.element( instanceIndex );
const currentSpeed = speedAttribute.element( instanceIndex );

Expand All @@ -91,18 +90,18 @@

If( pointerPosition.w.equal( 1 ), () => {

const worldPosition = objectWorldMatrix( object ).mul( currentPosition.xyz );
const worldPosition = objectWorldMatrix( object ).mul( currentPosition );

const dist = worldPosition.distance( pointerPosition.xyz );
const direction = pointerPosition.xyz.sub( worldPosition ).normalize();

const power = brushSize.sub( dist ).max( 0 ).mul( brushStrength );

currentPosition.xyz.addAssign( direction.mul( power ) );
currentPosition.addAssign( direction.mul( power ) );

} );

// update
// compute ( jelly )

const distance = basePosition.distance( currentPosition );
const force = elasticity.mul( distance ).mul( basePosition.sub( currentPosition ) );
Expand Down
38 changes: 30 additions & 8 deletions src/nodes/accessors/StorageBufferNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,27 +112,49 @@ class StorageBufferNode extends BufferNode {

}

generate( builder ) {
getAttributeData() {

if ( this._attribute === null ) {

this._attribute = bufferAttribute( this.value );
this._varying = varying( this._attribute );

}

return {
attribute: this._attribute,
varying: this._varying
};

}

getNodeType( builder ) {

if ( builder.isAvailable( 'storageBuffer' ) ) {

return super.generate( builder );
return super.getNodeType( builder );

}

const nodeType = this.getNodeType( builder );
const { attribute } = this.getAttributeData();

if ( this._attribute === null ) {
return attribute.getNodeType( builder );

this._attribute = bufferAttribute( this.value );
this._varying = varying( this._attribute );
}

generate( builder ) {

if ( builder.isAvailable( 'storageBuffer' ) ) {

return super.generate( builder );

}

const { attribute, varying } = this.getAttributeData();

const output = this._varying.build( builder, nodeType );
const output = varying.build( builder );

builder.registerTransform( output, this._attribute );
builder.registerTransform( output, attribute );

return output;

Expand Down