import { Guid } from "../utils" ; import { Designer , NodeRenderContext } from "../designer" ; import { Property , FloatProperty , IntProperty , BoolProperty , EnumProperty , ColorProperty , StringProperty , GradientProperty , IPropertyHolder , ImageProperty , PropertyType , PropertyGroup } from "./properties" ; import { buildShaderProgram } from "./gl" ; import { Color } from "./color" ; import { Gradient } from "./gradient" ; import { Image } from "./image" ; export class NodeInput { public node : DesignerNode ; public name : string ; } export class DesignerNode implements IPropertyHolder { public id : string = Guid . newGuid ( ) ; public title : string ; public typeName : string ; public gl : WebGLRenderingContext ; tex : WebGLTexture ; public designer : Designer ; exportName : string ; randomSeed : number = 0 ; inputs : string [ ] = [ ] ; properties : Property [ ] = [ ] ; propertyGroups : PropertyGroup [ ] = [ ] ; needsUpdate = true ; onthumbnailgenerated : ( DesignerNode , HTMLImageElement ) => void ; private requestUpdate ( ) { this . designer . requestUpdate ( this ) ; } getPixelData ( ) { return null ; } getTextureData ( ) { return null ; } setRandomSeed ( seed ) { this . randomSeed = seed ; this . requestUpdate ( ) ; } public render ( context ) { } public isCpu ( ) { return true ; } public getInputs ( ) { return this . inputs ; } protected addInput ( name ) { this . inputs . push ( name ) ; } public setProperty ( name , value ) { const prop = this . properties . find ( x => { return x . name == name ; } ) ; if ( prop ) { if ( prop . type == PropertyType . Image ) { prop . setValue ( value , ( ) => { this . requestUpdate ( ) ; } ) ; } else { prop . setValue ( value ) ; this . requestUpdate ( ) ; } } } public _init ( ) { this . createTexture ( ) ; this . init ( ) ; } protected init ( ) { } createTexture ( ) { const gl = this . gl as WebGL2RenderingContext ; if ( this . tex ) { gl . deleteTexture ( this . tex ) ; this . tex = null ; } const tex = gl . createTexture ( ) ; gl . bindTexture ( gl . TEXTURE_2D , tex ) ; const level = 0 ; const internalFormat = gl . RGBA16F ; const border = 0 ; const format = gl . RGBA ; const type = gl . FLOAT ; const data = null ; gl . texImage2D ( gl . TEXTURE_2D , level , internalFormat , this . designer . width , this . designer . height , border , format , type , data ) ; gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MIN_FILTER , gl . LINEAR ) ; gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_MAG_FILTER , gl . LINEAR ) ; gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_S , gl . REPEAT ) ; gl . texParameteri ( gl . TEXTURE_2D , gl . TEXTURE_WRAP_T , gl . REPEAT ) ; gl . bindTexture ( gl . TEXTURE_2D , null ) ; this . tex = tex ; } createGroup ( name ) { const group = new PropertyGroup ( ) ; group . name = name ; this . propertyGroups . push ( group ) ; return group ; } addIntProperty ( id , displayName , defaultVal = 1 , minVal = 1 , maxVal = 100 , increment = 1 ) { const prop = new IntProperty ( id , displayName , defaultVal ) ; prop . minValue = minVal ; prop . maxValue = maxVal ; prop . step = increment ; this . properties . push ( prop ) ; return prop ; } addFloatProperty ( id , displayName , defaultVal = 1 , minVal = 1 , maxVal = 100 , increment = 1 ) { const prop = new FloatProperty ( id , displayName , defaultVal ) ; prop . minValue = minVal ; prop . maxValue = maxVal ; prop . step = increment ; this . properties . push ( prop ) ; return prop ; } addBoolProperty ( id , displayName , defaultVal = false ) { const prop = new BoolProperty ( id , displayName , defaultVal ) ; this . properties . push ( prop ) ; return prop ; } addEnumProperty ( id , displayName , defaultVal = [ ] ) { const prop = new EnumProperty ( id , displayName , defaultVal ) ; this . properties . push ( prop ) ; return prop ; } addColorProperty ( id , displayName , defaultVal ) { const prop = new ColorProperty ( id , displayName , defaultVal ) ; this . properties . push ( prop ) ; return prop ; } addStringProperty ( id , displayName , defaultVal = "" ) { const prop = new StringProperty ( id , displayName , defaultVal ) ; this . properties . push ( prop ) ; return prop ; } addGradientProperty ( id , displayName , defaultVal ) { const prop = new GradientProperty ( id , displayName , defaultVal ) ; this . properties . push ( prop ) ; return prop ; } addImageProperty ( id , displayName , defaultVal ) { const prop = new ImageProperty ( id , displayName , defaultVal ) ; this . properties . push ( prop ) ; return prop ; } }