@@ -795,6 +795,54 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
795795 hook . set = function ( result ) {
796796 hook . _result = result ;
797797 } ;
798+ hook . _active = false ;
799+
800+ const numStructArgs = hookType . parameters . filter (
801+ param => param . type && param . type . properties
802+ ) . length ;
803+ let argIdx = - 1 ;
804+ if ( numStructArgs === 1 ) {
805+ argIdx = hookType . parameters . findIndex (
806+ param => param . type && param . type . properties
807+ ) ;
808+ }
809+ if ( argIdx >= 0 ) {
810+ const structParam = hookType . parameters [ argIdx ] ;
811+ if ( structParam . type . properties ) {
812+ const nameMatch = / ^ g e t ( [ A - Z 0 - 9 ] \w * ) $ / . exec ( hookType . name ) ;
813+ const displayName = nameMatch
814+ ? nameMatch [ 1 ] [ 0 ] . toLowerCase ( ) + nameMatch [ 1 ] . slice ( 1 )
815+ : hookType . name ;
816+ for ( const prop of structParam . type . properties ) {
817+ const key = prop . name ;
818+ Object . defineProperty ( hook , key , {
819+ get ( ) {
820+ if ( ! this . _active ) {
821+ FES . userError (
822+ 'scope error' ,
823+ `It looks like you're trying to access "${ displayName } .${ key } " outside of its begin()/end() block.\n\n` +
824+ `Properties of ${ displayName } are only available between ` +
825+ `${ displayName } .begin() and ${ displayName } .end().\n\n` +
826+ `To share data between hooks, use sharedVec3() or sharedFloat() ` +
827+ `to pass values between them.`
828+ ) ;
829+ }
830+ return this . _args [ this . _argIdx ] [ key ] ;
831+ } ,
832+ set ( val ) {
833+ if ( ! this . _active ) {
834+ FES . userError (
835+ 'scope error' ,
836+ `It looks like you're trying to set "${ displayName } .${ key } " outside of its begin()/end() block.`
837+ ) ;
838+ }
839+ this . _args [ this . _argIdx ] [ key ] = val ;
840+ } ,
841+ enumerable : true ,
842+ } ) ;
843+ }
844+ }
845+ }
798846
799847 let entryBlockID ;
800848 function setupHook ( ) {
@@ -803,25 +851,14 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
803851 CFG . addEdge ( cfg , cfg . currentBlock , entryBlockID ) ;
804852 CFG . pushBlock ( cfg , entryBlockID ) ;
805853 const args = createHookArguments ( strandsContext , hookType . parameters ) ;
806- const numStructArgs = hookType . parameters . filter ( param => param . type . properties ) . length ;
807- let argIdx = - 1 ;
808- if ( numStructArgs === 1 ) {
809- argIdx = hookType . parameters . findIndex ( param => param . type . properties ) ;
810- }
854+ hook . _active = true ;
855+ hook . _args = args ;
856+ hook . _argIdx = argIdx ;
811857 hook . _properties = [ ] ;
812858 for ( let i = 0 ; i < args . length ; i ++ ) {
813859 if ( i === argIdx ) {
814860 for ( const key of args [ argIdx ] . structProperties || [ ] ) {
815861 hook . _properties . push ( key ) ;
816- Object . defineProperty ( hook , key , {
817- get ( ) {
818- return args [ argIdx ] [ key ] ;
819- } ,
820- set ( val ) {
821- args [ argIdx ] [ key ] = val ;
822- } ,
823- enumerable : true ,
824- } ) ;
825862 }
826863 if ( hookType . returnType ?. typeName === hookType . parameters [ argIdx ] . type . typeName ) {
827864 hook . set ( args [ argIdx ] ) ;
@@ -835,6 +872,7 @@ export function createShaderHooksFunctions(strandsContext, fn, shader) {
835872 } ;
836873
837874 function finishHook ( ) {
875+ hook . _active = false ;
838876 const userReturned = hook . _result ;
839877 strandsContext . activeHook = undefined ;
840878
0 commit comments