import { ShaderSemantics , ShaderSemanticsInfo , } from '../../definitions/ShaderSemantics' ; import AbstractMaterialNode from '../core/AbstractMaterialNode' ; import Material from '../core/Material' ; import { ShaderityObject } from 'shaderity' ; import ShaderityUtility from '../core/ShaderityUtility' ; import { ShaderType } from '../../definitions/ShaderType' ; import ComponentRepository from '../../core/ComponentRepository' ; import CameraComponent from '../../components/CameraComponent' ; import GlobalDataRepository from '../../core/GlobalDataRepository' ; import MeshComponent from '../../components/MeshComponent' ; import BlendShapeComponent from '../../components/BlendShapeComponent' ; export default class ShaderitySingleMaterialNode extends AbstractMaterialNode { constructor ( { name , vertexShaderityObj , pixelShaderityObj , } ) { super ( null , name , { } ) ; const vertexShaderData = ShaderityUtility . getShaderDataRefection ( vertexShaderityObj , AbstractMaterialNode . __semanticsMap . get ( this . shaderFunctionName ) ) ; const pixelShaderData = ShaderityUtility . getShaderDataRefection ( pixelShaderityObj , AbstractMaterialNode . __semanticsMap . get ( this . shaderFunctionName ) ) ; this . __vertexShaderityObject = vertexShaderityObj ; this . __pixelShaderityObject = pixelShaderityObj ; const shaderSemanticsInfoArray = vertexShaderData . shaderSemanticsInfoArray . concat ( ) ; for ( const pixelShaderSemanticsInfo of pixelShaderData . shaderSemanticsInfoArray ) { const foundShaderSemanticsInfo = shaderSemanticsInfoArray . find ( ( info ) => { return info . semantic . str === pixelShaderSemanticsInfo . semantic . str ; } ) ; if ( foundShaderSemanticsInfo ) { foundShaderSemanticsInfo . stage = ShaderType . VertexAndPixelShader ; } else { shaderSemanticsInfoArray . push ( pixelShaderSemanticsInfo ) ; } } ShaderitySingleMaterialNode . __removeUselessShaderSemantics ( shaderSemanticsInfoArray ) ; this . setShaderSemanticsInfoArray ( shaderSemanticsInfoArray ) ; } setParametersForGPU ( { material , shaderProgram , firstTime , args , } ) { if ( args . setUniform ) { this . setWorldMatrix ( shaderProgram , args . worldMatrix ) ; this . setNormalMatrix ( shaderProgram , args . normalMatrix ) ; if ( firstTime || args . isVr ) { let cameraComponent = args . renderPass . cameraComponent ; if ( cameraComponent == null ) { cameraComponent = ComponentRepository . getInstance ( ) . getComponent ( CameraComponent , CameraComponent . main ) as CameraComponent ; } this . setViewInfo ( shaderProgram , cameraComponent , args . isVr , args . displayIdx ) ; this . setProjection ( shaderProgram , cameraComponent , args . isVr , args . displayIdx ) ; } if ( firstTime ) { this . setLightsInfo ( shaderProgram , args . lightComponents , material , args . setUniform ) ; } const skeletalComponent = args . entity . getSkeletal ( ) ; this . setSkinning ( shaderProgram , skeletalComponent , args . setUniform ) ; } this . setMorphInfo ( shaderProgram , args . entity . getComponent ( MeshComponent ) , args . entity . getComponent ( BlendShapeComponent ) , args . primitive ) ; } private static __removeUselessShaderSemantics ( shaderSemanticsInfoArray ) { const globalPropertyStructs = GlobalDataRepository . getInstance ( ) . getGlobalProperties ( ) ; for ( const globalPropertyStruct of globalPropertyStructs ) { const globalShaderSemanticsInfo = globalPropertyStruct . shaderSemanticsInfo ; const duplicateElemId = shaderSemanticsInfoArray . findIndex ( shaderSemanticsInfo => shaderSemanticsInfo . semantic . str === globalShaderSemanticsInfo . semantic . str ) ; if ( duplicateElemId !== - 1 ) { shaderSemanticsInfoArray . splice ( duplicateElemId , 1 ) ; } } const defaultShaderSemantics = [ ShaderSemantics . VertexAttributesExistenceArray , ShaderSemantics . WorldMatrix , ShaderSemantics . NormalMatrix , ShaderSemantics . PointSize , ShaderSemantics . PointDistanceAttenuation , ] ; for ( const shaderSemantic of defaultShaderSemantics ) { const duplicateElemId = shaderSemanticsInfoArray . findIndex ( shaderSemanticsInfo => shaderSemanticsInfo . semantic . str === shaderSemantic . str ) ; if ( duplicateElemId !== - 1 ) { shaderSemanticsInfoArray . splice ( duplicateElemId , 1 ) ; } } } }