Web3D Preloader

A preloader is a small scene that can download another scene package and simultaneously show progress and some content. For example, you can show advertisement while downloading a big scene package.

Show Content

You can show any content while downloading. Here, we just show a simple text. Create a Tex2D and edit it's text to 'Preloader Demo', change it's size at your pleasure. Select Options > Viewport... from main menu, enable Standard Viewport; then right-click in viewport, select Show Viewport Frame from the context menu. You can see a red frame appears in viewport, move the text to the center of the red frame.

 

Edit User Script File
First save the scene to a file, then select View > Edit User Script File...  from main menu. Then edit the file:

1. Add below codes in the beginning:

require 'scripts\\websession'
require 'scripts\\webfile'

local szURL='http://www.awingsoft.com/zips/Paramount.exe' -- replace this string by your own scene package URL

local bOpenFailed=false

local ses=websession.new()
local file=ses.openURL(szURL)

local destfilename=GetTempFileName()

local filesize=-1
local bytescount=0
local readbytes=0

if (file) then 
   filesize=file.seek(0,FILE_END)
   file.seek(0,FILE_BEGIN)
   file.setDestFile(destfilename)
   readbytes=file.read()
end


if (filesize<0) then
   bOpenFailed=true
   if (file) then file.release() end
   ses.release()
end

2.  Edit the OnSize function as below:

local wcx,wcy = 0,0
function OnSize( type, cx, cy )
   wcx=cx
   wcy=cy
end

3.  Edit the FrameMove function as below:

local per=0

function FrameMove()

   if (bOpenFailed) then return end

   if (readbytes>0) then
      bytescount=bytescount + readbytes
      per=bytescount/filesize

      readbytes=file.read()
   else
      file.release()
      ses.release()

      Unpack(destfilename)

      local path, scefn, ver = GetPackageInfo(szURL,destfilename)

      SetBasePath(path)
      wnd.SetCurrentDirectory(path)
      LoadScene(path..'\\'..scefn)
   end

end

4.  Edit the Render2D function as below:

function Render2D(draw)

   local r=rect.new(50,wcy-80,wcx-50,wcy-50)

   draw.setcolor(COLOR_YELLOW)

   if (bOpenFailed) then
      draw.textout(r.left,r.top-24,'Open URL failed!') 
      return
   else
      draw.textout(r.left,r.top-24,'Loading...')
   end

   draw.setcolor(COLOR_RED)
   draw.rect(r)
   r.right=r.left+r.width()*per

   draw.setbkcolor(COLOR_YELLOW)
   draw.fillrect(r)

end

Ok, we have finished it.