Logic for a single Model with different Bitmap?

All topics about ZGameEditor goes here.

Moderator: Moderators

Post Reply
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Logic for a single Model with different Bitmap?

Post by Ats »

Hello.
I have a texture that is randomly created with a BitmapExpression at the start of a level, and I apply a @RefreshContent on it and everything is working fine... Until I used that texture on the same model spawned several times on the scene. I expect each models to have different textures, but the last content refresh is the only texture existing. So I was thinking I had to put that Bitmap in the Definitions of the model, just like variables and meshes, but it just crashes.

So I'm stuck against the logic here.
Is this possible to do? And I think I'll have the same problem afterwards with Named RanderTransformGroup inside the OnRender of the Model...

I made a very simple non-working (crashing) example:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[for (int i=0; i<16; i++)
{
  model m = createModel(ModelCube);
  m.CubeColor = vector3(rnd(),rnd(),rnd());
  m.Scale = 0.4;
  m.Position = vector3(random(0,8), random(0,6), -random(10,6));

  TempColor = CubeColor;
  @RefreshContent(Component: BitmapColor);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="ModelCube">
      <Definitions>
        <Variable Name="CubeColor" Type="7"/>
        <Material Name="Material1">
          <Textures>
            <MaterialTexture Texture="BitmapColor" TexCoords="1"/>
          </Textures>
        </Material>
        <Bitmap Name="BitmapColor" Width="16" Height="16">
          <Producers>
            <BitmapExpression>
              <Expression>
<![CDATA[Pixel.R = CurrentModel.CubeColor.X;
Pixel.G = CurrentModel.CubeColor.Y;
Pixel.B = CurrentModel.CubeColor.Z;
/*
Pixel.R = TempColor.X;
Pixel.G = TempColor.Y;
Pixel.B = TempColor.Z;
*/]]>
              </Expression>
            </BitmapExpression>
          </Producers>
        </Bitmap>
      </Definitions>
      <OnRender>
        <UseMaterial Material="Material1"/>
        <RenderMesh Mesh="MeshCube"/>
      </OnRender>
    </Model>
    <Mesh Name="MeshCube">
      <Producers>
        <MeshBox/>
      </Producers>
    </Mesh>
    <Variable Name="TempColor" Type="7"/>
  </Content>
</ZApplication>
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Logic for a single Model with different Bitmap?

Post by Kjell »

Hi Ats,
Ats wrote: Fri Feb 11, 2022 1:52 pmIs this possible to do?
Yup, i recommend doing it like this:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0.7529 0.7529 0.7529 1" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[//

for(int i=0; i<8; i++)
{
  Dummy.Position.X = random(0, 8);
  Dummy.Position.Y = random(0, 4);

  createModel(Dummy);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="Dummy" Position="2.945 3.2998 0">
      <Definitions>
        <Bitmap Name="DummyBitmap" Width="32" Height="32">
          <Producers>
            <BitmapExpression>
              <Expression>
<![CDATA[//

float u, v, c;

u = DummyColor[0]-X;
v = DummyColor[1]-Y;

c = clamp(DummyColor[2]*8-sqrt(u*u+v*v)*16, 0, 1);

Pixel.R = c*DummyColor[0];
Pixel.G = c*DummyColor[1];
Pixel.B = c*DummyColor[2];]]>
              </Expression>
            </BitmapExpression>
          </Producers>
        </Bitmap>
        <Material Name="DummyMaterial" Shading="1" Light="0">
          <Textures>
            <MaterialTexture Texture="DummyBitmap" TextureWrapMode="2" TexCoords="1"/>
          </Textures>
        </Material>
      </Definitions>
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[//

DummyColor[0] = rnd();
DummyColor[1] = rnd();
DummyColor[2] = rnd();]]>
          </Expression>
        </ZExpression>
        <RefreshContent Component="DummyBitmap"/>
      </OnSpawn>
      <OnRender>
        <UseMaterial Material="DummyMaterial"/>
        <RenderMesh Mesh="DummyMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="DummyMesh">
      <Producers>
        <MeshBox Grid2DOnly="255"/>
      </Producers>
    </Mesh>
    <Array Name="DummyColor" SizeDim1="3"/>
  </Content>
</ZApplication>
You could also put the DummyColor array inside Dummy.Definitions, but that's a lot slower for some reason.

K
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Logic for a single Model with different Bitmap?

Post by Ats »

Oh, thanks. You're right, putting the DummyColor inside inside Dummy.Definitions is so slow... That's weird.

One last thing, you are using an Array for DummyColor instead of a Vector3. Is it better, or faster?
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Logic for a single Model with different Bitmap?

Post by Kjell »

Hi Ats,
Ats wrote: Fri Feb 11, 2022 4:15 pmOne last thing, you are using an Array for DummyColor instead of a Vector3. Is it better, or faster?
I don't think it makes a big difference, here's the same thing using a vec3 instead of a array:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" ClearColor="0.7529 0.7529 0.7529 1" FileVersion="2">
  <OnLoaded>
    <ZExpression>
      <Expression>
<![CDATA[//

for(int i=0; i<8; i++)
{
  Dummy.Position.X = random(0, 8);
  Dummy.Position.Y = random(0, 4);

  createModel(Dummy);
}]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="Dummy" Position="5.409 0.8699 0">
      <Definitions>
        <Bitmap Name="DummyBitmap" Width="32" Height="32">
          <Producers>
            <BitmapExpression>
              <Expression>
<![CDATA[//

float u, v, c;

u = DummyColor.R-X;
v = DummyColor.G-Y;

c = clamp(DummyColor.B*8-sqrt(u*u+v*v)*16, 0, 1);

Pixel.R = c*DummyColor.R;
Pixel.G = c*DummyColor.G;
Pixel.B = c*DummyColor.B;]]>
              </Expression>
            </BitmapExpression>
          </Producers>
        </Bitmap>
        <Material Name="DummyMaterial" Shading="1" Light="0">
          <Textures>
            <MaterialTexture Texture="DummyBitmap" TextureWrapMode="2" TexCoords="1"/>
          </Textures>
        </Material>
      </Definitions>
      <OnSpawn>
        <ZExpression>
          <Expression>
<![CDATA[//

DummyColor = vector3(rnd(), rnd(), rnd());]]>
          </Expression>
        </ZExpression>
        <RefreshContent Component="DummyBitmap"/>
      </OnSpawn>
      <OnRender>
        <UseMaterial Material="DummyMaterial"/>
        <RenderMesh Mesh="DummyMesh"/>
      </OnRender>
    </Model>
    <Mesh Name="DummyMesh">
      <Producers>
        <MeshBox Grid2DOnly="255"/>
      </Producers>
    </Mesh>
    <Variable Name="DummyColor" Type="7"/>
  </Content>
</ZApplication>
K
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Logic for a single Model with different Bitmap?

Post by Ats »

All right, I just discovered that one can use RGB instead of XYZ on vectors :oops:
•X, Y, Z, W (or x, y, z, w) - for position, or
•R, G, B, A (or r, g, b, a) - for colors.
Thanks again for the great examples, Kjell, I'll put them to good use in Omeganaut!
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: Logic for a single Model with different Bitmap?

Post by Ats »

By the way, thanks to your example, I managed to solve a few remnant bugs in my game :lol:

I was spawning all my models like this:

Code: Select all

model m = createModel(Dummy);
m.Position = vector3(0,0,10);
instead of:

Code: Select all

Dummy.Position = vector3(0,0,10);
createModel(Dummy);
I didn't understand that creating the temporary model would spawn it right away, all while creating its physics and collisions before setting its position, resulting in weird bugs from time to time.
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: Logic for a single Model with different Bitmap?

Post by Kjell »

Hi Ats,
Ats wrote: Sat Feb 12, 2022 9:17 amI didn't understand that creating the temporary model would spawn it right away, all while creating its physics and collisions before setting its position, resulting in weird bugs from time to time.
It's important to be aware of the order in which things get executed yes. Here's a simple example that demonstrates that the OnSpawn event of a model is executed directly after calling createModel ( same thing with the equivalent SpawnModel component ).

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <ZExpression Comment="Set X to 4">
      <Expression>
<![CDATA[// Create clone

model m = createModel(Dummy);

// Set position to 4

m.Position.x = 4;]]>
      </Expression>
    </ZExpression>
  </OnLoaded>
  <Content>
    <Model Name="Dummy">
      <OnSpawn>
        <ZExpression Comment="Set X to 0">
          <Expression>
<![CDATA[// Set position to 0

Dummy.Position.X = 0;]]>
          </Expression>
        </ZExpression>
      </OnSpawn>
      <OnRender>
        <RenderSprite/>
      </OnRender>
    </Model>
  </Content>
</ZApplication>
K
Post Reply