How to color an imported 3ds mesh?

All topics about ZGameEditor goes here.

Moderator: Moderators

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

How to color an imported 3ds mesh?

Post by Ats »

Hi everyone, it's been a long time since I played ZGE. So I'm back to basics...
I'm trying to color an imported 3DS mesh directly in ZGE.
This mesh is already colored using vertex colors.

I tried UseMaterial and RenderSetColor without success. In the example below, how could I color my cube while pressing the spacebar?
Am I obliged to use textures for that?

Thanks for your help :)

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <SpawnModel Model="CubeModel" SpawnStyle="1"/>
  </OnLoaded>
  <OnUpdate>
    <ZExpression Expression="CubeHit = 0;"/>
    <KeyPress CharCode="32">
      <OnPressed>
        <ZExpression Expression="CubeHit = 1;"/>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <Content>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshImport HasVertexColors="1">
          <MeshData>
<![CDATA[789C9D8CC10D80300C0353103FFEECD10598A6733047A76113E8086C600CA14ADA278E4E8E1C2B8B88CCA460DB0FF2F809AC072904AF581075C56735B74CA9F2B9DDAD613FFC777FAF5B908100414699C89FFD42EA06312AF9F34E29B5B0D4928D1B1414A9E7]]>
          </MeshData>
        </MeshImport>
      </Producers>
    </Mesh>
    <Model Name="CubeModel" Rotation="1.897 2.5127 2.2678" RotationVelocity="0.0605 0.0289 0.047">
      <OnSpawn>
        <ZExpression Expression="CurrentModel.RotationVelocity = rnd() * 0.1;"/>
      </OnSpawn>
      <OnRender>
        <Condition Comment="Is Cube Hit?" Expression="return CubeHit == 1;">
          <OnTrue>
            <UseMaterial Material="RedMaterial"/>
            <RenderSetColor Color="1 0 0 1"/>
            <RenderTransform Scale="1.1 1.1 1.1"/>
          </OnTrue>
          <OnFalse>
            <UseMaterial Material="FlatMaterial"/>
          </OnFalse>
        </Condition>
        <RenderMesh Mesh="BoxMesh"/>
      </OnRender>
    </Model>
    <Material Name="RedMaterial" Shading="1" Color="1 0 0 1"/>
    <Material Name="FlatMaterial" Shading="1"/>
    <Variable Name="CubeHit" Type="1"/>
  </Content>
</ZApplication>
User avatar
Ats
Posts: 603
Joined: Fri Sep 28, 2012 10:05 am
Contact:

Re: How to color an imported 3ds mesh?

Post by Ats »

Ok... Getting into it didn't take me long after all, here's my solution:

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" FileVersion="2">
  <OnLoaded>
    <SpawnModel Model="CubeModel" SpawnStyle="1"/>
  </OnLoaded>
  <OnUpdate>
    <ZExpression Expression="CubeHit = 0;"/>
    <KeyPress CharCode="32">
      <OnPressed>
        <ZExpression Expression="CubeHit = 1;"/>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <Content>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshImport HasVertexColors="1">
          <MeshData>
<![CDATA[789C9D8CC10D80300C0353103FFEECD10598A6733047A76113E8086C600CA14ADA278E4E8E1C2B8B88CCA460DB0FF2F809AC072904AF581075C56735B74CA9F2B9DDAD613FFC777FAF5B908100414699C89FFD42EA06312AF9F34E29B5B0D4928D1B1414A9E7]]>
          </MeshData>
        </MeshImport>
      </Producers>
    </Mesh>
    <Model Name="CubeModel" Rotation="11.8458 7.0448 8.8451" RotationVelocity="0.0342 0.0458 0.0495">
      <OnSpawn>
        <ZExpression Expression="CurrentModel.RotationVelocity = rnd() * 0.1;"/>
      </OnSpawn>
      <OnRender>
        <Condition Comment="Is Cube Hit?" Expression="return CubeHit == 1;">
          <OnTrue>
            <UseMaterial Material="RedMaterial"/>
            <RenderTransform Scale="1.1 1.1 1.1"/>
          </OnTrue>
          <OnFalse>
            <UseMaterial Material="FlatMaterial"/>
          </OnFalse>
        </Condition>
        <RenderMesh Mesh="BoxMesh"/>
      </OnRender>
    </Model>
    <Material Name="RedMaterial" Shading="1" Color="1 0 0 1">
      <Textures>
        <MaterialTexture Texture="RedBitmap"/>
      </Textures>
    </Material>
    <Material Name="FlatMaterial" Shading="1"/>
    <Variable Name="CubeHit" Type="1"/>
    <Bitmap Name="RedBitmap">
      <Producers>
        <BitmapExpression>
          <Expression>
<![CDATA[//X,Y : current coordinate (0..1)
//Pixel : current color (rgb)
//Sample expression: Pixel.R=abs(sin(X*16));
Pixel.R=1;
Pixel.G=0;
Pixel.B=0;
Pixel.A=1;]]>
          </Expression>
        </BitmapExpression>
      </Producers>
    </Bitmap>
  </Content>
</ZApplication>
Is it possible to use transparency on the red color so we can still see the vertex colors under, or is it impossible?
User avatar
Kjell
Posts: 1876
Joined: Sat Feb 23, 2008 11:15 pm

Re: How to color an imported 3ds mesh?

Post by Kjell »

Hi Ats,
Ats wrote: Fri Feb 07, 2020 6:52 pmI'm trying to color an imported 3DS mesh directly in ZGE. This mesh is already colored using vertex colors. I tried UseMaterial and RenderSetColor without success.
Vertex colors, material color and RenderSetColor all manipulate the same data in the fixed-function pipeline, so you can't "combine" colors that way.
Ats wrote: Fri Feb 07, 2020 7:11 pmIs it possible to use transparency on the red color so we can still see the vertex colors under, or is it impossible?
The easiest way to do that using the fixed-function pipeline would be to use lights. By default ZGE enables a single directional light and a ambient component .. so here's a example with the ambient set to black and the direction light set to red when the spacebar is being pressed ( for the rotating mesh only ).

Code: Select all

<?xml version="1.0" encoding="iso-8859-1" ?>
<ZApplication Name="App" Caption="ZGameEditor application" AmbientLightColor="0 0 0 1" FileVersion="2">
  <OnLoaded>
    <ZExternalLibrary ModuleName="opengl32">
      <Source>
<![CDATA[//

void glLightfv(int light, int pname, xptr params){}]]>
      </Source>
    </ZExternalLibrary>
    <SpawnModel Model="Box" Position="2 0 0"/>
  </OnLoaded>
  <OnUpdate>
    <ZExpression>
      <Expression>
<![CDATA[//

Spacebar = 0;]]>
      </Expression>
    </ZExpression>
    <KeyPress Keys=" ">
      <OnPressed>
        <ZExpression>
          <Expression>
<![CDATA[//

Spacebar = 1;]]>
          </Expression>
        </ZExpression>
      </OnPressed>
    </KeyPress>
  </OnUpdate>
  <OnRender>
    <RenderTransformGroup Translate="-2 0 0">
      <Children>
        <RenderMesh Mesh="BoxMesh"/>
      </Children>
    </RenderTransformGroup>
  </OnRender>
  <Content>
    <Model Name="Box" RotationVelocity="0.1 0.1 0">
      <OnRender>
        <Condition>
          <Expression>
<![CDATA[//

return Spacebar;]]>
          </Expression>
          <OnTrue>
            <ZExpression>
              <Expression>
<![CDATA[//

float[4] v;

v[0] = 1;
v[1] = 0;
v[2] = 0;
v[3] = 0;

glLightfv(0x4000, 0x1201, v);]]>
              </Expression>
            </ZExpression>
          </OnTrue>
        </Condition>
        <RenderMesh Mesh="BoxMesh"/>
        <Condition>
          <Expression>
<![CDATA[//

return Spacebar;]]>
          </Expression>
          <OnTrue>
            <ZExpression>
              <Expression>
<![CDATA[//

float[4] v;

v[0] = 1;
v[1] = 1;
v[2] = 1;
v[3] = 0;

glLightfv(0x4000, 0x1201, v);]]>
              </Expression>
            </ZExpression>
          </OnTrue>
        </Condition>
      </OnRender>
    </Model>
    <Mesh Name="BoxMesh">
      <Producers>
        <MeshBox/>
        <MeshExpression AutoNormals="0" VertexColors="255">
          <Expression>
<![CDATA[//

C.R = V.X;
C.G = V.Y;
C.B = V.Z;]]>
          </Expression>
        </MeshExpression>
      </Producers>
    </Mesh>
    <Variable Name="Spacebar" Type="4"/>
  </Content>
</ZApplication>
Or you can simply use shaders :wink:

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

Re: How to color an imported 3ds mesh?

Post by Ats »

Thanks Kjell. Clever example, as always :wink:
Post Reply