123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- import WebGPUUniformsGroup from './WebGPUUniformsGroup.js';
- import { FloatUniform, Vector3Uniform } from './WebGPUUniform.js';
- import NodeSlot from '../nodes/core/NodeSlot.js';
- import NodeBuilder from '../nodes/core/NodeBuilder.js';
- class WebGPUNodeUniformsGroup extends WebGPUUniformsGroup {
-
- constructor( shaderStage ) {
- super( 'nodeUniforms' );
-
- let shaderStageVisibility;
-
- if ( shaderStage === 'vertex' ) shaderStageVisibility = GPUShaderStage.VERTEX;
- else if ( shaderStage === 'fragment' ) shaderStageVisibility = GPUShaderStage.FRAGMENT;
-
- this.setVisibility( shaderStageVisibility );
-
- this.setOnBeforeUpdate( this._onBeforeUpdate );
-
- }
-
- _onBeforeUpdate( object, camera ) {
-
- const material = object.material;
-
- console.log( 1 );
-
- }
-
- }
- class WebGPUNodeBuilder extends NodeBuilder {
- constructor( material, renderer ) {
- super( material, renderer );
-
- this.uniformsGroup = {};
-
- this._parseMaterial();
-
- }
-
- _parseMaterial() {
-
- const material = this.material;
-
- if ( material.isMeshBasicMaterial ) {
-
- if ( material.color.isNode ) {
-
- this.addSlot( 'fragment', new NodeSlot( material.color, 'COLOR', 'vec3' ) );
-
- }
-
- }
-
- }
-
- getUniformNSName( nodeUniform ) {
-
- return `nodeUniforms.${nodeUniform.name}`;
-
- }
-
- getBindings() {
-
- const bindings = [];
-
- const uniformsVertexGroup = this.uniformsGroup[ 'vertex' ];
- const uniformsFragmentGroup = this.uniformsGroup[ 'fragment' ];
-
- if ( uniformsVertexGroup ) bindings.push( uniformsVertexGroup );
- if ( uniformsFragmentGroup ) bindings.push( uniformsFragmentGroup );
-
- return bindings;
-
- }
-
- createUniformFromNode( node, shaderStage, type ) {
-
- const uniformNode = super.createUniformFromNode( node, shaderStage, type )
- const nodeData = this.createDataFromNode( node, shaderStage );
-
- if ( nodeData.uniformGroup === undefined ) {
-
- let uniformsGroup = this.uniformsGroup[shaderStage];
-
- if (uniformsGroup === undefined) {
-
- uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
-
- this.uniformsGroup[shaderStage] = uniformsGroup;
-
- }
-
- let uniformGroup;
-
- if ( type === 'float' ) {
-
- uniformGroup = new FloatUniform( uniformNode.name, uniformNode.value );
-
- } else if ( type === 'vec3' ) {
-
- uniformGroup = new Vector3Uniform( uniformNode.name, uniformNode.value );
-
- } else {
-
- console.error( `Uniform "${type}" not declared.` );
-
- }
-
- uniformsGroup.addUniform( uniformGroup );
-
- nodeData.uniformGroup = uniformGroup;
-
- }
-
- return uniformNode;
-
- }
-
- buildShader( shaderStage, code ) {
-
- // use regex maybe for security?
- const versionStrIndex = code.indexOf("\n");
-
- let finalCode = code.substr( 0, versionStrIndex ) + "\n\n";
- const shaderCodes = this.build( shaderStage );
-
- finalCode += shaderCodes.defines;
- finalCode += code.substr( versionStrIndex );
- console.log( finalCode );
- return finalCode;
-
- }
-
- parse( vertexShader, fragmentShader ) {
-
- vertexShader = this.buildShader( 'vertex', vertexShader );
- fragmentShader = this.buildShader( 'fragment', fragmentShader );
- return {
- vertexShader,
- fragmentShader
- };
-
- }
- }
- export default WebGPUNodeBuilder;
|