9 Replies Latest reply on Apr 5, 2007 12:45 PM by DAVID NAJAR

    Alpha transparency borders on dynamically loaded images for use with textures

      Hi there folks. Well, I have been beating my head over this and am hoping one of you can help me out.
      Essentially I have 16bit images that are loaded into my shockwave application at run-time. These images are user defined which means that I will not know their size ahead of time. Once the images are loaded I then create textures and apply them to various shaders. Ok, all of this works just fine. What I need to do though is create an alpha transparent border around the images, so when I apply them to their surfaces I only see the image and nothing around it. I am doing this because these images will only appear at specific parts on a surface, textureRepeat will be false, and many models will be stacked in the same exact location using the same model resource. I have everything working, but I cannot figure out the alpha part for the life of me. Also, when I test the alpha based image by itself it works just fine. It is not until I try and combine the 16bit (converted to 32bit at run-time) image that I run into problems.
      I have tried:
      - Creating a 32bit alpha image in Fireworks with a transparent border and a black rect for the inside. I then copy the dynamic image over the alpha image (only after going from 16bit to 32bit in imaging Lingo) and leave a little room at the edges for the transparency. Well, I get a crazy amount of streaks here even when I try to up the trans border by around 24 pixels on each side.
      - Using another similiar alpha based image as a mask with copyPixels and extractAlpha/setAlpha like this... (code is a little rough here)
      newImage = new(#bitmap)
      newImage.name = "place_Test"
      newImage.image = member("place").image

      newAlpha = new(#bitmap)
      newAlpha.name = "AHH"
      newAlpha.image = image(newImage.image.width + 24, newImage.image.height + 24, 32)
      newAlpha.image.useAlpha = true
      newAlpha.image.copyPixels(member("vase").image, newAlpha.image.rect, member("vase").image.rect)
      newAlphaInfo = newAlpha.image.extractAlpha()
      newAlpha.image.useAlpha = false

      --reverse dark to light
      iAlphaReverse = image(newAlpha.image.width, newAlpha.image.height, 8, #grayscale)

      iAlphaReverse.fill(iAlphaReverse.rect, rgb(0,0,0))
      iAlphaReverse.copyPixels(newAlphaInfo, iAlphaReverse.rect, newAlphaInfo.rect, [#ink : #reverse])

      --newAlphaInfo.copyPixels(iAlphaReverse, newAlphaInfo.rect, iAlphaReverse.rect, [#ink:#subtractpin])
      newAlphaMask = iAlphaReverse.createMask()

      rescaleAbs(newImage, newImage.image.width, newImage.image.height, "place_Test", 32)

      destImage = member("place_Test").image.duplicate()
      destImage.fill(destImage.rect, rgb(0,0,0))

      newAlpha.image.useAlpha = false

      destImage.copyPixels(newImage.image, newImage.image.rect, newImage.image.rect, [#maskImage:newAlphaMask, #ink:#add])

      destImage.useAlpha = true
      member("place_Test").image = destImage

      I apologize for the messy code. I have cut and pasted from all over the place and am getting confused. In any case, I think I am making this harder then it needs to be and hope someone can help.

      Thank you in advance,
        • 2. Re: Alpha transparency borders on dynamically loaded images for use with textures
          Sonic2004 Level 1
          Hey there Nickname, thank you for the reply. I have, in fact, read that very article a couple of times to ensure that I did not miss anything. While it did help me with creating alpha textures within tools like Fireworks or Photoshop it does not explain how to create the alpha transparency at run-time using only Lingo. Better yet, how to combine a 16bit image with a 32bit alpha. The interesting thing is that I already do this in another part of my code with different image objects, but for some reason the same methodology does not work at all in this particular application. I did, however, test one of the images that I am referring to and as a sprite it works just fine. As a texture I get streaking.

          Is there something interesting that needs to be done to ensure that one does not get streaking when working with alpha transparency in textures as compared to sprites???

          Thank you,
          • 3. Re: Alpha transparency borders on dynamically loaded images for use with textures
            duckets Level 1
            Have a look at: .renderFormat (a property of textures), and getRenderServices().textureRenderFormat (which sets the default texture render format.

            You may find that setting your texture's renderformat property to #rgba8888 gets rid of the streaking.

            - Ben

            • 4. Re: Alpha transparency borders on dynamically loaded images for use with textures
              Sonic2004 Level 1
              Hey there duckets thank you for the reply as well. I guess I should do a better job of providing more upfront information in the description of my problem. I have tried using #rgba8888, #rgba4444, and #rgba5551 all with the same result. I have also used both OpenGL and Direct3D in my hunt to get rid of the streaking to no avail. Here is what is different when I do not get streaking. I can create an image in Fireworks with the correct masking technique and just load that image as a texture with no streaking. I, however, cannot seem to do the same only using imaging Lingo which is what I need to do. So, the issue I am having seems to be using the correct technique in imaging Lingo to convert a 16bit image to a 32bit one with alpha trans at the borders. Thank you again for your assistance.
              • 5. Re: Alpha transparency borders on dynamically loaded images for use with textures
                Sonic2004 Level 1
                Hi again folks. Ok, has anyone any knowledge of an xTra called GLEX? I was able to find a beta online and it literally solved my problems right out of the box. The issue with alpha transparency and Z ordering as I have described immediately disappeard after about 5 mins of configuration. I would really like to purchase this xTra if it is possible and I am working on a time crunch. Any information would be very helpful.

                Thank you,
                • 6. Re: Alpha transparency borders on dynamically loaded   images for   use with textures
                  Level 7
                  It was created by Lucas Meijer and Noisecrime. I'm not sure if they ever
                  got it to the point of a commercial release.
                  If you search the dir3d-l and/or dirgames-l list archives you'll find
                  contact addresses and threads relating to this xtra.
                  • 7. Re: Alpha transparency borders on dynamically loaded images for use with textures
                    duckets Level 1
                    So, were you having an alpha transparency z ordering problem? From you initial post it sounded like your problem was to do with the visual appearance of the textures ('streaking', maybe bit-depth related) rather than z-sorting. Did you solve the streaking?

                    - Ben
                    • 8. Alpha transparency borders on dynamically loaded images for use with textures
                      _eyesonly Level 1
                      Is your texture width and height a power of two?(16,32,64,128,256,512,1024,etc) If not then your texture will look lousy and streaking may still occur due to the renderer forcing the image to scale to a power of two. I have been able to work around this problem on occasion by using the #rgba5551 textureformat under DX_7. Also, if I remember correctly, when you are creating the alpha in an image object the alpha channel image has to be 8bit and of the same dimensions as the 32 bit host image (the mask function in imaging lingo does not add alpha transparency to the host image/base layer, but merely composites transparent layers onto the 32 bit host image, i.e. the 32 bit host image should contain the alpha transparency before compositing). In addition, trying to up-sample from 16 bit to 32 bit may alias your image boundaries significantly and could be an issue. On the other hand, the problem could lie with your UV mapping, but we can't tell for sure unless you post the actual code used or at least a screen capture of the problem.
                      • 9. Re: Alpha transparency borders on dynamically loaded images for use with textures
                        DAVID NAJAR Level 1
                        Hi, you can try using other texture layer as mask on the same shader. As usually you create the texture from a dynamic loaded image, then apply this texture to the shader on the texture list index 1 (textureList[1]). Next part does the job, create other texture from a 32 bits image with the alpha information and fill all pixels with white color, this is very important because the second texture layer will be multiply with the first texture layer. This texture set its render format to rgba8888. Apply the mask texture to the same shader at texture list index 2, verify that the blendFunctionList index 2 is #multiply.

                        I include the code of a project that use this masking approach:

                        property pMember
                        property pEarthSphere
                        property pNightSphere
                        property pLastTransform

                        on beginSprite me
                        pMember = sprite(me.spriteNum).member

                        on setupWorld(me)

                        repeat with i = pMember.light.count down to 1
                        end repeat

                        vEarthModelResource = pMember.newModelResource("EARTH MODEL RESOURCE", #sphere)
                        vEarthModelResource.radius = 50.000
                        vEarthModelResource.resolution = 20

                        vEarthTexture = pMember.newTexture("EARTH TEXTURE", #fromCastMember, member(3,1))

                        vEarthShader = pMember.newShader("EARTH SHADER", #standard)
                        vEarthShader.emissive = color(255,255,255)
                        vEarthShader.flat = TRUE
                        vEarthShader.transparent = FALSE
                        vEarthShader.textureList[1] = vEarthTexture

                        pEarthSphere = pMember.newModel("EARTH MODEL", vEarthModelResource)
                        pEarthSphere.shaderList = vEarthShader

                        vNightModelResource = pMember.newModelResource("NIGHT MODEL RESOURCE", #sphere)
                        vNightModelResource.radius = 50.2000
                        vNightModelResource.resolution = 20

                        vNightTexture = pMember.newTexture("NIGHT TEXTURE", #fromCastMember, member(4,1))
                        vNightTexture.quality = #lowFiltered
                        vNightTexture.nearFiltering = FALSE
                        vNightTexture.renderFormat = #rgba8880

                        vNightShader = pMember.newShader("NIGHT SHADER", #standard)
                        vNightShader.emissive = color(255,255,255)
                        vNightShader.flat = TRUE
                        vNightShader.transparent = TRUE
                        vNightShader.textureList[1] = vNightTexture

                        vMaskNightTexture = pMember.newTexture("MASK NIGHT TEXTURE", #fromCastMember, member(6,1))
                        vMaskNightTexture.renderFormat = #rgba8888

                        vNightShader.textureList[2] = vMaskNightTexture
                        vNightShader.textureModeList[2] = #wrapPlanar

                        pNightSphere = pMember.newModel("NIGHT MODEL", vNightModelResource)
                        pNightSphere.shaderList[1] = vNightShader

                        pNightSphere.parent = pEarthSphere


                        on exitFrame(me)

                        on moveMaskNightTexture(me)
                        vRotationVector = - pEarthSphere.transform.rotation
                        pNightSphere.shaderList[1].wrapTransformList[2].rotation = vRotationVector