1
0
Fork 0
mirror of https://github.com/DanielnetoDotCom/YouPHPTube synced 2025-10-06 03:50:04 +02:00
Daniel Neto 2023-12-11 11:59:56 -03:00
parent f0f62670c5
commit 7e26256cac
4563 changed files with 1246712 additions and 17558 deletions

View file

@ -0,0 +1,146 @@
const ShaderLib = {
meshBasic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
NODE_HEADER_ATTRIBUTES
NODE_HEADER_UNIFORMS
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
mat3 normalMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
NODE_BODY_ATTRIBUTES
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
NODE_HEADER_ATTRIBUTES
NODE_HEADER_UNIFORMS
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4( 1.0, 1.0, 1.0, 1.0 );
#ifdef NODE_COLOR
outColor = NODE_COLOR;
#endif
#ifdef NODE_OPACITY
outColor.a *= NODE_OPACITY;
#endif
}`
},
pointsBasic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
NODE_HEADER_ATTRIBUTES
NODE_HEADER_UNIFORMS
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
NODE_BODY_ATTRIBUTES
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
NODE_HEADER_ATTRIBUTES
NODE_HEADER_UNIFORMS
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4( 1.0, 1.0, 1.0, 1.0 );
#ifdef NODE_COLOR
outColor = NODE_COLOR;
#endif
#ifdef NODE_OPACITY
outColor.a = NODE_OPACITY;
#endif
}`
},
lineBasic: {
vertexShader: `#version 450
layout(location = 0) in vec3 position;
NODE_HEADER_ATTRIBUTES
NODE_HEADER_UNIFORMS
layout(set = 0, binding = 0) uniform ModelUniforms {
mat4 modelMatrix;
mat4 modelViewMatrix;
} modelUniforms;
layout(set = 0, binding = 1) uniform CameraUniforms {
mat4 projectionMatrix;
mat4 viewMatrix;
} cameraUniforms;
void main(){
NODE_BODY_ATTRIBUTES
gl_Position = cameraUniforms.projectionMatrix * modelUniforms.modelViewMatrix * vec4( position, 1.0 );
}`,
fragmentShader: `#version 450
NODE_HEADER_ATTRIBUTES
NODE_HEADER_UNIFORMS
layout(location = 0) out vec4 outColor;
void main() {
outColor = vec4( 1.0, 1.0, 1.0, 1.0 );
#ifdef NODE_COLOR
outColor = NODE_COLOR;
#endif
#ifdef NODE_OPACITY
outColor.a = NODE_OPACITY;
#endif
}`
}
};
export default ShaderLib;

View file

@ -0,0 +1,294 @@
import WebGPUNodeUniformsGroup from './WebGPUNodeUniformsGroup.js';
import { FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform, ColorNodeUniform } from './WebGPUNodeUniform.js';
import WebGPUSampler from '../WebGPUSampler.js';
import { WebGPUSampledTexture } from '../WebGPUSampledTexture.js';
import NodeSlot from '../../nodes/core/NodeSlot.js';
import NodeBuilder from '../../nodes/core/NodeBuilder.js';
import ShaderLib from './ShaderLib.js';
class WebGPUNodeBuilder extends NodeBuilder {
constructor( material, renderer ) {
super( material, renderer );
this.bindingIndex = 2;
this.bindings = { vertex: [], fragment: [] };
this.attributeIndex = 1;
this.varyIndex = 0;
this.uniformsGroup = {};
this.nativeShader = null;
this._parseMaterial();
}
_parseMaterial() {
const material = this.material;
// get shader
if ( material.isMeshBasicMaterial ) {
this.nativeShader = ShaderLib.meshBasic;
} else if ( material.isPointsMaterial ) {
this.nativeShader = ShaderLib.pointsBasic;
} else if ( material.isLineBasicMaterial ) {
this.nativeShader = ShaderLib.lineBasic;
} else {
console.error( 'THREE.WebGPURenderer: Unknwon shader type.' );
}
// parse inputs
if ( material.isMeshBasicMaterial || material.isPointsMaterial || material.isLineBasicMaterial ) {
if ( material.colorNode !== undefined ) {
this.addSlot( 'fragment', new NodeSlot( material.colorNode, 'COLOR', 'vec4' ) );
}
if ( material.opacityNode !== undefined ) {
this.addSlot( 'fragment', new NodeSlot( material.opacityNode, 'OPACITY', 'float' ) );
}
}
}
getTexture( textureProperty, uvSnippet ) {
return `texture( sampler2D( ${textureProperty}, ${textureProperty}_sampler ), ${uvSnippet} )`;
}
getPropertyName( nodeUniform ) {
if ( nodeUniform.type === 'texture' ) {
return nodeUniform.name;
} else {
return `nodeUniforms.${nodeUniform.name}`;
}
}
getBindings( shaderStage ) {
return this.bindings[ shaderStage ];
}
getUniformFromNode( node, shaderStage, type ) {
const uniformNode = super.getUniformFromNode( node, shaderStage, type );
const nodeData = this.getDataFromNode( node, shaderStage );
if ( nodeData.uniformGPU === undefined ) {
let uniformGPU;
const bindings = this.bindings[ shaderStage ];
if ( type === 'texture' ) {
const sampler = new WebGPUSampler( `${uniformNode.name}_sampler`, uniformNode.value );
const texture = new WebGPUSampledTexture( uniformNode.name, uniformNode.value );
// add first textures in sequence and group for last
const lastBinding = bindings[ bindings.length - 1 ];
const index = lastBinding && lastBinding.isUniformsGroup ? bindings.length - 1 : bindings.length;
bindings.splice( index, 0, sampler, texture );
uniformGPU = { sampler, texture };
} else {
let uniformsGroup = this.uniformsGroup[ shaderStage ];
if ( uniformsGroup === undefined ) {
uniformsGroup = new WebGPUNodeUniformsGroup( shaderStage );
this.uniformsGroup[ shaderStage ] = uniformsGroup;
bindings.push( uniformsGroup );
}
if ( type === 'float' ) {
uniformGPU = new FloatNodeUniform( uniformNode );
} else if ( type === 'vec2' ) {
uniformGPU = new Vector2NodeUniform( uniformNode );
} else if ( type === 'vec3' ) {
uniformGPU = new Vector3NodeUniform( uniformNode );
} else if ( type === 'vec4' ) {
uniformGPU = new Vector4NodeUniform( uniformNode );
} else if ( type === 'color' ) {
uniformGPU = new ColorNodeUniform( uniformNode );
} else {
throw new Error( `Uniform "${type}" not declared.` );
}
uniformsGroup.addUniform( uniformGPU );
}
nodeData.uniformGPU = uniformGPU;
}
return uniformNode;
}
getAttributesHeaderSnippet( shaderStage ) {
let snippet = '';
const attributes = this.attributes;
let attributeIndex = this.attributeIndex;
let varyIndex = this.varyIndex;
for ( const name in attributes ) {
const attribute = attributes[ name ];
const type = attribute.type;
const property = attribute.property;
if ( shaderStage === 'vertex' ) {
snippet += `layout(location = ${attributeIndex ++}) in ${type} ${name};`;
snippet += `layout(location = ${varyIndex ++}) out ${type} ${property};`;
} else if ( shaderStage === 'fragment' ) {
snippet += `layout(location = ${varyIndex ++}) in ${type} ${property};`;
}
}
return snippet;
}
getAttributesBodySnippet( /* shaderStage */ ) {
let snippet = '';
const attributes = this.attributes;
for ( const name in attributes ) {
const attribute = attributes[ name ];
const property = attribute.property;
snippet += `${property} = ${name};`;
}
return snippet;
}
getUniformsHeaderSnippet( shaderStage ) {
const uniforms = this.uniforms[ shaderStage ];
let snippet = '';
let groupSnippet = '';
let bindingIndex = this.bindingIndex;
for ( const uniform of uniforms ) {
if ( uniform.type === 'texture' ) {
snippet += `layout(set = 0, binding = ${bindingIndex ++}) uniform sampler ${uniform.name}_sampler;`;
snippet += `layout(set = 0, binding = ${bindingIndex ++}) uniform texture2D ${uniform.name};`;
} else {
const vectorType = this.getVectorType( uniform.type );
groupSnippet += `uniform ${vectorType} ${uniform.name};`;
}
}
if ( groupSnippet ) {
snippet += `layout(set = 0, binding = ${bindingIndex ++}) uniform NodeUniforms { ${groupSnippet} } nodeUniforms;`;
}
return snippet;
}
composeShaderCode( code, snippet ) {
// use regex maybe for security?
const versionStrIndex = code.indexOf( '\n' );
let finalCode = code.substr( 0, versionStrIndex ) + '\n\n';
finalCode += snippet;
finalCode += code.substr( versionStrIndex );
return finalCode;
}
build() {
super.build();
this.vertexShader = this.composeShaderCode( this.nativeShader.vertexShader, this.vertexShader );
this.fragmentShader = this.composeShaderCode( this.nativeShader.fragmentShader, this.fragmentShader );
return this;
}
}
export default WebGPUNodeBuilder;

View file

@ -0,0 +1,135 @@
import {
FloatUniform, Vector2Uniform, Vector3Uniform, Vector4Uniform,
ColorUniform, Matrix3Uniform, Matrix4Uniform
} from '../WebGPUUniform.js';
class FloatNodeUniform extends FloatUniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Vector2NodeUniform extends Vector2Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Vector3NodeUniform extends Vector3Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Vector4NodeUniform extends Vector4Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class ColorNodeUniform extends ColorUniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Matrix3NodeUniform extends Matrix3Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
class Matrix4NodeUniform extends Matrix4Uniform {
constructor( nodeUniform ) {
super( nodeUniform.name, nodeUniform.value );
this.nodeUniform = nodeUniform;
}
getValue() {
return this.nodeUniform.value;
}
}
export {
FloatNodeUniform, Vector2NodeUniform, Vector3NodeUniform, Vector4NodeUniform,
ColorNodeUniform, Matrix3NodeUniform, Matrix4NodeUniform
};

View file

@ -0,0 +1,29 @@
import WebGPUUniformsGroup from '../WebGPUUniformsGroup.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;
}
*/
}
export default WebGPUNodeUniformsGroup;

View file

@ -0,0 +1,68 @@
import WebGPUNodeBuilder from './WebGPUNodeBuilder.js';
import NodeFrame from '../../nodes/core/NodeFrame.js';
class WebGPUNodes {
constructor( renderer ) {
this.renderer = renderer;
this.nodeFrame = new NodeFrame();
this.builders = new WeakMap();
}
get( material ) {
let nodeBuilder = this.builders.get( material );
if ( nodeBuilder === undefined ) {
nodeBuilder = new WebGPUNodeBuilder( material, this.renderer ).build();
this.builders.set( material, nodeBuilder );
}
return nodeBuilder;
}
remove( object ) {
this.builders.delete( object );
}
updateFrame() {
this.nodeFrame.update();
}
update( object/*, camera*/ ) {
const material = object.material;
const nodeBuilder = this.get( material );
this.nodeFrame.material = object.material;
for ( const node of nodeBuilder.updateNodes ) {
this.nodeFrame.updateNode( node );
}
}
dispose() {
this.builders = new WeakMap();
}
}
export default WebGPUNodes;