Page 1 of 1

Call component from BaseModel

Posted: Sat Jan 06, 2018 8:26 am
by rrTea
I'm making some buttons with a common BaseModel. I'm trying to change their color based on certain parameters, and that color should also later be used for some other parts of the buttons, something like this:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <SpawnModel Model="Model2"/>
  </OnLoaded>
  <Content>
    <Material Name="Material1"/>
    <Model Name="Model1">
      <Definitions>
        <RenderSetColor Name="setCol_ButtonLabel" Color="1 1 1 1"/>
      </Definitions>
      <OnUpdate>
        <ZExpression Expression="setCol_ButtonLabel.Color = 0.5+sin(App.Time*10)*0.5;"/>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="Material1"/>
        <CallComponent Component="setCol_ButtonLabel"/>
        <RenderNet/>
      </OnRender>
    </Model>
    <Model Name="Model2" BaseModel="Model1">
      <OnRender>
        <UseMaterial Material="Material1"/>
        <CallComponent Component="setCol_ButtonLabel"/>
        <RenderNet>
          <RenderVertexExpression>
<![CDATA[//Update each vertex.
//Vertex : current vertex
//TexCoord : current texture coordinate
//Color : current vertex color

Vertex.X += 2;]]>
          </RenderVertexExpression>
        </RenderNet>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
How come the square on the Right doesn't blink? I'm confused :)

Re: Call component from BaseModel

Posted: Sat Jan 06, 2018 8:50 pm
by VilleK
There are some limitations about basemodel inheritance. Try this variant that uses a Vec3 variable to hold the color and a library to set it.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <SpawnModel Model="Model2"/>
  </OnLoaded>
  <Content>
    <Material Name="Material1"/>
    <Model Name="Model1">
      <Definitions>
        <RenderSetColor Name="setCol_ButtonLabel" Color="1 1 1 1"/>
        <Variable Name="Variable1" Type="7"/>
        <ZLibrary>
          <Source>
<![CDATA[void setColButton() {
  setCol_ButtonLabel.Color=Variable1;
  @CallComponent(Component : setCol_ButtonLabel);
}]]>
          </Source>
        </ZLibrary>
      </Definitions>
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[setCol_ButtonLabel.Color = 0.5+sin(App.Time*10)*0.5;
Variable1 = 0.5+sin(App.Time*10)*0.5;]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="Material1"/>
        <ZExpression Expression="setColButton();"/>
        <RenderNet/>
      </OnRender>
    </Model>
    <Model Name="Model2" BaseModel="Model1">
      <OnRender>
        <UseMaterial Material="Material1"/>
        <ZExpression Expression="setColButton();"/>
        <RenderNet>
          <RenderVertexExpression>
<![CDATA[//Update each vertex.
//Vertex : current vertex
//TexCoord : current texture coordinate
//Color : current vertex color

Vertex.X += 2;]]>
          </RenderVertexExpression>
        </RenderNet>
      </OnRender>
    </Model>
  </Content>
</ZApplication>

Re: Call component from BaseModel

Posted: Sun Jan 07, 2018 3:55 am
by rrTea
Ah right I actually asked about the same thing on one earlier occasion :oops: thanks Ville!!!... :oops: :oops:

Re: Call component from BaseModel

Posted: Sun Jan 07, 2018 4:03 am
by rrTea
Here's an even simpler solution based on the old discussion in case someone bumps into the same problem!... just put the @CallComponent as a ZExpression, this eliminates the need for the ZLibrary etc.

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <SpawnModel Model="Model2"/>
  </OnLoaded>
  <Content>
    <Material Name="Material1"/>
    <Model Name="Model1">
      <Definitions>
        <RenderSetColor Name="setCol_ButtonLabel" Color="1 1 1 1"/>
        <Variable Name="Variable1" Type="7"/>
      </Definitions>
      <OnUpdate>
        <ZExpression>
          <Expression>
<![CDATA[setCol_ButtonLabel.Color = 0.5+sin(App.Time*10)*0.5;
Variable1 = 0.5+sin(App.Time*10)*0.5;]]>
          </Expression>
        </ZExpression>
      </OnUpdate>
      <OnRender>
        <UseMaterial Material="Material1"/>
        <ZExpression Expression="@CallComponent(Component : setCol_ButtonLabel);"/>
        <RenderNet/>
      </OnRender>
    </Model>
    <Model Name="Model2" BaseModel="Model1">
      <OnRender>
        <UseMaterial Material="Material1"/>
        <ZExpression Expression="@CallComponent(Component : setCol_ButtonLabel);"/>
        <RenderNet>
          <RenderVertexExpression>
<![CDATA[//Update each vertex.
//Vertex : current vertex
//TexCoord : current texture coordinate
//Color : current vertex color

Vertex.X += 2;]]>
          </RenderVertexExpression>
        </RenderNet>
      </OnRender>
    </Model>
  </Content>
</ZApplication>