Posted: 14th Jul 2019 3:46
Been playing with Character Controllers and I figured basic car physics can be achieved
Basic Car Physics Its nowhere as sophisticated as blinks bullet car physics but it
may be usefull for a retro remake, the demo also shows that jumps are achievable
+ Code Snippet
SetErrorMode(2)
#constant acceleration#=.05 //used to calclate the carSpeed#
#constant maxCarSpeed#=20.0 //The Maximum speed the carBlock will move 
#constant minCarSpeed#=0.0   //The Minimum speed the carBlock will move 
#constant AccelerationFactor#=.25 //the speed your car accelerates at
#constant DecelerationFactor#=.05 //the braking factor
#constant KEY_LEFT         37
#constant KEY_UP           38
#constant KEY_RIGHT        39
#constant KEY_DOWN         40
screenWidth As Float
screenHeight As Float
screenAspect As Float

screenWidth = GetDeviceWidth()
screenHeight = GetDeviceHeight()

// set window properties
SetWindowTitle("Vehicle physics with playercontrollers")
SetOrientationAllowed(0, 0, 1, 1) // allow both portrait and landscape on mobile devices
SetWindowSize(screenWidth, screenHeight, 0, 0)
SetWindowAllowResize(1) // allow the user to resize the window

// set display properties
SetVirtualResolution(screenWidth, screenHeight) // doesn't have to match the window
SetSyncRate(25, 0) // 30fps instead of 60 to save battery
SetScissor(0, 0, 0, 0) // use the maximum available screen space, no black borders
UseNewDefaultFonts(1)

create3DPhysicsWorld()
box=CreateObjectSphere(2, 16, 16)
SetObjectColor(box,200,200,200,255)
SetObjectPosition(box,0,2,0)
//RotateObjectLocalz(box,90)
//SetCameraPosition(1, 0, 0, -3)
///SetCameraLookAt(1, 0, 0, 0, 0)
createCharacterController(box)

ground=CreateObjectPlane(200,200)
SetObjectImage(ground,CreateGrid(screenWidth,screenHeight), 0)
RotateObjectLocalX(ground,90)
Create3DPhysicsStaticBody(ground)

ramp=CreateObjectPlane(10,40)
SetObjectColor(ramp,200,0,0,255)
SetObjectPosition(ramp,0,-.025,10)
RotateObjectLocalX(ramp,65)
Create3DPhysicsStaticBody(ramp)


global velocity as float

do
		print (velocity)
		updatePlayerMovement(box)
		SetCameraPosition(1,GetObjectWorldX(box),GetObjectWorldY(box)+5,GetObjectWorldZ(box)-5)
        Sync()
loop

function createCharacterController(objID as integer)
	//This vector is the offset of the model center from origin.
    //For example a model 72 units tall standing at origin is 36 units offset on the y axis.
    //This allows for models of different sizes.
     characterOffsetVec = CreateVector3( 0.0, 0.0, 0.50 )
    //This vector indicates the axis your model faces forward.
    //For example if your model faces down the - Z axis use this vector.
    objectOrientationVec = CreateVector3( 0.0, 0.0, 0.0 )
    //The crouchScale parameter is 0.0 to 1.0, for examle 0.75 is 75% of the models standing height.
    Create3DPhysicsCharacterController( objID, 1, characterOffsetVec, objectOrientationVec, 0.90 )
    DeleteVector3( characterOffsetVec )
    DeleteVector3( objectOrientationVec )
endfunction	

function updatePlayerMovement(player as integer)
     
    if GetRawKeyState(KEY_LEFT)  //strafe left
   		Rotate3DPhysicsCharacterController(player,GetObjectAngleY(player)-2) 		
    endif
    if GetRawKeyState(KEY_RIGHT)  //strafe right
   		Rotate3DPhysicsCharacterController(player,GetObjectAngleY(player)+2) 		
    endif
     
    if GetRawKeyState(KEY_UP) //move forward
		//velocity=velocity+1.0
		velocity=velocity+((maxCarSpeed#-velocity)*AccelerationFactor#) 
        if velocity>maxCarSpeed# then velocity=maxCarSpeed#
	 else
		velocity=velocity-((maxCarSpeed#-velocity)*DecelerationFactor#+0.1) 
        if velocity<minCarSpeed# then velocity=minCarSpeed#
	 endif
 
     Move3DPhysicsCharacterController(player,1,velocity)
    Step3DPhysicsWorld() //needs to be called prior to below and any camera adjustments 
endfunction

function	CreateGrid(screenWidth,screenHeight)

	SetClearColor(0x70, 0x70, 0x70)
	ClearScreen()
	size = screenWidth/ 32
    green=MakeColor(0,200,0)
	DrawBox(0, 0, screenWidth, screenHeight, green,green,green,green, 1)
		
	for i=0 to screenWidth step size
		DrawLine(i, 0, i, screenHeight, 200,0,0)	
	next
	
	for i=screenHeight to 0 step -size
		DrawLine(0, i, screenWidth, i, 200,0,0)
	next
	
	render()
	
	img = GetImage(0, 0, screenWidth, screenHeight)
	ClearScreen()	
endfunction img


PS: the camera angles are bad but easily fixable
the main thing I find is the steering isn't correct but im sure there is a formula out there that calculates a steering
amount based on present velocity and strafe movements could be used for a sliding algorithm
Posted: 14th Jul 2019 4:41
Nice work
Posted: 26th Jul 2019 20:37
A Sprite Shader effect needs a texture assigned to the sprite

+ Code Snippet
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
 
LoadImage(1,"mytexture.png") 

createShader()
CreateSprite(1,1)
SetSpriteSize(1,1024,768)
LoadSpriteShader(1, "shader.ps")
SetSpriteShader( 1,1 )

SetShaderConstantByName(1,"resolution",1024,768,0.0,0.0) 

resetTimer()
do
	time#=Timer()
	SetShaderConstantByName(1,"time",time#,0.0,0.0,0.0) 
	sync()	

loop
 
function createShader()

file = OpenToWrite("shader.ps")
WriteLine(file,"#ifdef GL_ES")
WriteLine(file,"precision mediump float;")
WriteLine(file,"#endif")
WriteLine(file,"#extension GL_OES_standard_derivatives : enable")
WriteLine(file,"uniform sampler2D texture0;")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"uniform float time;")
WriteLine(file,"uniform vec2 resolution;")
WriteLine(file,"// function to return a random number")
WriteLine(file,"float random (vec2 p) {")
WriteLine(file,"		p = fract(p*vec2(123.45, 678.91));")
WriteLine(file,"    p += dot(p, p+23.45);")
WriteLine(file,"    return fract (p.x * p.y);")
WriteLine(file,"}")
WriteLine(file,"void main( void )")
WriteLine(file,"{")
WriteLine(file,"		vec2 uv = (gl_FragCoord.xy-.5*resolution.xy)/resolution.y;")
WriteLine(file,"    vec3 pattern = vec3(0);")
WriteLine(file,"    float units = 30.;")
WriteLine(file,"    vec2 gv = fract(uv * units) - .5;")
WriteLine(file,"    vec2 id = floor(uv * units) + .5; // add .5 here to center")
WriteLine(file,"    float d = length(gv);")
WriteLine(file,"    float minRadius = .2;")
WriteLine(file,"    float maxRadius = .5;")
WriteLine(file,"    float speed = 10.2;")
WriteLine(file,"    float pulseAmount = 1.;")
WriteLine(file,"    float radius = mix(")
WriteLine(file,"        minRadius, ")
WriteLine(file,"        maxRadius,")
WriteLine(file,"        //sin(random(id) * time * speed) * .5 + .5);")
WriteLine(file,"        sin(length(pulseAmount*gv - id) - time * speed)*.5+.5); // how to offset sine based on id")
WriteLine(file,"    float m = smoothstep(radius, radius*.9,d);")
WriteLine(file,"    pattern += m;")
WriteLine(file,"    vec3 colorA = vec3(random(id));")
WriteLine(file,"		vec3 colorB = texture2D(texture0, uvVarying).rgb;")
WriteLine(file,"		vec3 finalColor = colorB*colorA;")
WriteLine(file," 	finalColor=clamp(finalColor,0.0,1.0);")
WriteLine(file,"    gl_FragColor = vec4(finalColor * pattern,1.);")
WriteLine(file,"}")
CloseFile(file)
endfunction
Posted: 27th Jul 2019 11:22
Posted: 3rd Aug 2019 23:12
I find the Basic Car Physics thingy rather interesting. Is their a specific reason you didn't use box=CreateObjectBox ( 2, 2, 4)? For it appears to be working just as well.
Posted: 4th Aug 2019 9:36
Thanks Rick I was playing with the angle of the ramp hoping I could make it work at a greater angle
i found Get3DPhysicsCharacterControllerMaxSlope( objID ) helps but changing it to much really
created some unusual behaviour
Posted: 18th Aug 2019 7:55
I have here a shader that I am using in macroaction which basically turns any object above a set point transparent
and any point below a set point transparent
fade.vs
+ Code Snippet
attribute highp vec3 position;
attribute mediump vec3 normal;
attribute mediump vec2 uv;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec2 uvVarying;
varying mediump vec3 lightVarying;
uniform highp mat3 agk_WorldNormal;
uniform highp mat4 agk_World;
uniform highp mat4 agk_ViewProj;
uniform mediump vec4 uvBounds0;
mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );
void main()
{
    uvVarying = uv * uvBounds0.xy + uvBounds0.zw;
    highp vec4 pos = agk_World * vec4(position,1.0);
    gl_Position = agk_ViewProj * pos;
    mediump vec3 norm = normalize(agk_WorldNormal * normal);
    posVarying = pos.xyz;
    normalVarying = norm;
    lightVarying = GetVSLighting( norm, posVarying );
}


fade.ps
+ Code Snippet
uniform sampler2D texture0;
varying highp vec3 posVarying;
varying mediump vec3 normalVarying;
varying mediump vec2 uvVarying;
varying mediump vec3 lightVarying;
mediump vec3 GetPSLighting( mediump vec3 normal, highp vec3 pos );
mediump vec3 ApplyFog( mediump vec3 color, highp vec3 pointPos );
uniform mediump vec3 agk_MeshDiffuse;
uniform mediump vec3 myCoord;
uniform vec3 lowp alpha;
varying vec4 verpos;
void main()
{
mediump vec3 norm = normalize(normalVarying);
mediump vec3 light = lightVarying + GetPSLighting( norm, posVarying );
vec3 colorA = texture2D(texture0, uvVarying).rgb*light *agk_MeshDiffuse;
if (posVarying.y>myCoord.y)
{
    colorA = clamp(colorA,0.0,1.0);
	mediump vec3 color = ApplyFog( colorA, posVarying );
	gl_FragColor = vec4(color,alpha.r);
}else{
	if (posVarying.y<myCoord.x)
	{
		mediump vec3 color = ApplyFog( colorA, posVarying );
		gl_FragColor = vec4(color,alpha.r);
	}else{
		mediump vec3 color = ApplyFog( colorA, posVarying );
		gl_FragColor = vec4(color,1.0);
	}
}
}

to use the shader I set all objects I want to transparency ie in part etc like the following
+ Code Snippet
SetObjectColor(models[num].ID,255,255,255,255)
SetObjectTransparency(models[num].ID,1)
SetObjectDepthWrite(models[num].ID,1)  
SetObjectShader(models[num].ID, fadeShader)


i set it up to its alpha value like the following and set the players position for the shader as follows
+ Code Snippet
SetShaderConstantByName(fadeShader,"alpha",0.2,0,0,0)
do 
	SetShaderConstantByName(fadeShader,"myCoord",getObjectY(player.ID)-5,(getObjectY(player.ID)+12),1,1)
        rem sets 5 below or less of the players position transparent and 12 above the players position 
	sync()
loop
Posted: 6th Sep 2019 1:29
Image 3D file loader
Im not sure how useful people will find this, but it basically loads an image and depending on the colour of the pixels of the image
loads a 3D object into the world at that pixel location in the 3D world. It requires a text file like the following that maps the colour to
an object and texture for that object
test.map
+ Code Snippet
#object red green blue image
tree.3ds 0 200 0 tree.png
rockwall.3ds 100 100 100 rockwall.png
grass1.3ds 0 100 0 grass1.png

main.agc
+ Code Snippet
// Project: imgFileLoader 
// Created: 2019-09-04

// show all errors
SetErrorMode(2)
 
#constant screenwidth=1024
#constant screenheight=768
#constant fullscreen=0
#constant screenrate=0
 
// set window properties
SetWindowTitle( "image loader" )
SetWindowSize( screenwidth, screenheight, fullscreen )
SetWindowAllowResize( 1 ) // allow the user to resize the window
 
// set display properties
SetVirtualResolution( screenwidth, screenheight ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( screenrate, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
 
#constant tileWidth 16
#constant tileDepth 16
#constant tileHeight 16

type _object
	objID as integer
	imgID as integer
endtype	
 
global objects as _object[]

type _map
	objName as String
	r as integer
	g as integer
	b as integer
	imgName as String
	imgID as integer
endtype

maps as _map[]	
loadmap("test.map",maps)		
img1=loadImage("layer1.png")
img2=loadImage("layer2.png") 
grabinfo(img1,0,maps,objects)
grabinfo(img2,1,maps,objects)
 
camerax#=100
cameray#=100
cameraz#=-190
 
do
    if GetRawKeyState(38) then inc cameraz#
    if GetRawKeyState(40) then dec cameraz#
    if GetRawKeyState(37) then inc camerax#
    if GetRawKeyState(39) then dec camerax#
     
     
    if GetRawKeyState(87) then inc cameray#
    if GetRawKeyState(83) then dec cameray#
     
     
     
    print(GetRawLastKey())
     
     
    SetCameraPosition(1,camerax#,cameray#,cameraz#)
 
    Print( ScreenFPS() )
    Sync()
loop
 
function grabinfo(img,layer as integer,maps as _map[],objects ref as _object[])
    imgmemblock = CreateMemblockFromImage(img)
     
    width = GetMemblockInt(imgmemblock,0)
    height = GetMemblockInt(imgmemblock,4)
    objnumber=0
    myObject as _object
    for x=width-1 to 0 step -1
		for y=height-1 to 0 step -1
            offset = (12+((y * width) + x) * 4) - 4
            r=GetMemblockByte(imgmemblock,offset)
            g=GetMemblockByte(imgmemblock,offset+1)
            b=GetMemblockByte(imgmemblock,offset+2)
            a=GetMemblockByte(imgmemblock,offset+3)
			color=r+g+b+a
			for check=maps.length to 0 step-1
				if r=maps[check].r and g=maps[check].g and b=maps[check].b
					myObject.objID=LoadObject(maps[check].objName)
					myObject.imgID=maps[check].imgID
					objects.insert(myObject)
					SetObjectPosition(objects[objnumber].objID,x*tileWidth,layer*tileHeight, -y*tileDepth  )
					SetObjectImage(objects[objnumber].objID,objects[objnumber].imgID,0)
					inc objnumber
				endif
            next check
        next
    next
endfunction

function loadMap(filename as string,maps ref as _map[])	
file = OpenToRead(filename)
myMap as _map
line		as string
do
	line = ReadLine(file)		
	if FileEOF(file) = 1
		exit
	endif
	if mid(line, 1, 1) = "#"
		continue
	endif
		
	myMap.objName = GetStringToken2( line, " ", 1 )
	myMap.r = val(GetStringToken2( line, " ", 2 ))
	myMap.g = val(GetStringToken2( line, " ", 3 ))
	myMap.b = val(GetStringToken2( line, " ", 4 ))
	myMap.imgName = GetStringToken2( line, " ", 5 )
	a=imageExists(myMap.imgName,maps)
	if a>0 
		myMap.imgID=a
	else
		myMap.imgID=LoadImage(myMap.imgName)
	endif			
	maps.insert(myMap)
loop
CloseFile(file)
endfunction

function imageExists(imgName as string,maps as _map[])
	val=0
	for check=maps.length to 0 step -1
		if imgName=maps[check].imgName
			exitfunction maps[check].imgID
		endif
	next Check 
endfunction val	

It was originally designed to convert 2D games to 3D and uses constants that can be changed for the tile width,height depth etc
it requires two png images in the above example to get it to work but the code is just an example of how it could be used and allows
for several different layers to be saved.

PSAs the original idea was to convert 2D games to 3D for best results it is assumed that all 3D objects are the same size as
each other.
Posted: 23rd Sep 2019 5:16
Kinda work in progress but its meant to give the effect of melting the object
Note works best if pivot is at the bottom

+ Code Snippet
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
#constant Shader =1


createShader()
LoadShader(Shader,"melt.vs", "melt.ps")
b=CreateObjectBox(5,5,5)
setObjectImage(b,CreateTexture(),0)
//b=LoadObjectWithChildren("zombie.fbx")

//SetObjectImage(b,loadImage("zombie.png"),0)
//RotateObjectLocaly(b,180)
//FixObjectPivot(b)
//SetObjectAnimationSpeed(b, 30)
//PlayObjectAnimation(b, GetObjectAnimationName(b, 1), 1, -1, 1, 0)
SetObjectShader(b,Shader)


x#=.1:y#=.1:z#=.1
	
do
    
    SetShaderConstantByName(Shader,"scalevalue",x#,y#,z#,1)
	dec y#,.0001
	if GetRawKeyState(32) then RotateObjectLocalY(b,1)
	print(ScreenFPS())
	sync()	

loop


function createShader()
file = OpenToWrite("melt.vs")
 
WriteLine(file,"attribute highp vec3 position;")
WriteLine(file,"attribute mediump vec3 normal;")
WriteLine(file,"attribute mediump vec2 uv;")
  
WriteLine(file,"varying highp vec3 posVarying;")
WriteLine(file,"varying mediump vec3 normalVarying;")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"varying mediump vec3 lightVarying;")
  
WriteLine(file,"uniform highp mat3 agk_WorldNormal;")
WriteLine(file,"uniform highp mat4 agk_World;")
WriteLine(file,"uniform highp mat4 agk_ViewProj;")
 
//WriteLine(file,"uniform highp mat4 agk_View;")
//WriteLine(file,"uniform highp mat4 agk_Proj;")
//WriteLine(file,"uniform highp mat4 modelMat;")
 
 
WriteLine(file,"uniform mediump vec4 uvBounds0;")
  
WriteLine(file,"mediump vec3 GetVSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(file,"uniform vec4 pointPos;") 
WriteLine(file,"uniform vec3 scalevalue;") 
writeline(file,"vec4 a = gl_Vertex;")
WriteLine(file,"void main()")
WriteLine(file,"{ ")
WriteLine(file,"    a.x+= scalevalue.x;")
WriteLine(file,"    a.y+= scalevalue.y;")
WriteLine(file,"    a.z+= scalevalue.z;")
 
WriteLine(file,"    a=clamp(a,0.0,1.0);//stops negative values")
WriteLine(file,"    uvVarying = uv * uvBounds0.xy + uvBounds0.zw;")
WriteLine(file,"    highp vec4 pos = agk_World * vec4(position,1.0)*a;")
WriteLine(file,"    mediump vec3 norm = normalize(agk_WorldNormal * normal);")
WriteLine(file,"    posVarying = pos.xyz;")
WriteLine(file,"    gl_Position = pos;")
WriteLine(file,"    normalVarying = norm;")
WriteLine(file,"    lightVarying = GetVSLighting( norm, posVarying );")
WriteLine(file,"}")
CloseFile(file)
 
//pixel shader
file = OpenToWrite("melt.ps")
WriteLine(file,"uniform sampler2D texture0;")
WriteLine(file,"varying highp vec3 posVarying;")
WriteLine(file,"varying mediump vec3 normalVarying;")
WriteLine(file,"varying mediump vec2 uvVarying;")
WriteLine(file,"varying mediump vec3 lightVarying;")
WriteLine(file,"mediump vec3 GetPSLighting( mediump vec3 normal, highp vec3 pos );")
WriteLine(file,"mediump vec3 ApplyFog( mediump vec3 color, highp vec3 pointPos );")
WriteLine(file,"uniform vec4 myColor;")
WriteLine(file,"uniform mediump vec3 agk_MeshDiffuse;")
WriteLine(file,"uniform vec3 pointPos;") 
 
WriteLine(file,"void main()")
WriteLine(file,"{")
WriteLine(file,"mediump vec3 norm = normalize(normalVarying);")
WriteLine(file,"mediump vec3 light = lightVarying + GetPSLighting( norm, posVarying );")
WriteLine(file,"mediump vec3 color = texture2D(texture0, uvVarying).rgb * light;")
WriteLine(file,"color = ApplyFog( color, posVarying );")
 
WriteLine(file,"gl_FragColor = vec4(color,1.0);")
WriteLine(file,"}")

CloseFile(file)
endfunction

function CreateTexture()
	blue=MakeColor(0,0,200)
    SetClearColor(0,0,0)
    ClearScreen()
    //draw craft
    DrawBox(0,0,18,16,blue,blue,blue,blue,1)
    render()
    img = getImage(0,0,18,16)
    SetImageTransparentColor(img,0,0,0)
endfunction img

PS if you uncomment this //a=clamp(a,0.0,1.0);
it wont go below the bottom. The scaling is kinda wrong and currently wont play animations


why not use setobjectscale you may ask because then you would have to set the object position
each time for the same effect
Posted: 28th Oct 2019 0:42
Another 2D snow example this time using a shader.
you will see that the house sprite appears to have snow going behind and infront of it at same time
this can be changed by setting the house sprites depth
no media required

+ Code Snippet
// set window properties
SetWindowTitle( "snow" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts

createShaderFile()


houseSpr=createSprite(createHouse())
SetSpriteScale(houseSpr,25,25)
SetSpriteColor(houseSpr,200,0,0,255)
SetSpritePosition(houseSpr,200,500)

snowShader=1
snowSpr=CreateSprite(0)
SetSpriteSize(snowSpr,1024,768)
LoadSpriteShader(snowShader,"snow.ps")
SetSpriteShader( snowSpr,snowShader )
SetSpriteTransparency(snowSpr,2)
SetShaderConstantByName(snowShader,"resolution",1024,768 ,0,0)
do 
	SetShaderConstantByName(snowShader,"time",timer(),0,0,0)
	
    sync()   
loop
function createShaderFile()
fw=OpenToWrite("snow.ps")

WriteLine(fw,"#ifdef GL_ES")
WriteLine(fw,"precision mediump float;")
WriteLine(fw,"#endif")

WriteLine(fw,"#extension GL_OES_standard_derivatives : enable")

WriteLine(fw,"uniform float time;")
WriteLine(fw,"uniform vec2 resolution;")
//WriteLine(fw,"uniform sampler2D texture0;")

WriteLine(fw,"float snow(vec2 uv,float scale)")
WriteLine(fw,"{")
WriteLine(fw,"	float w=smoothstep(1.,0.,-uv.y*(scale/100.));if(w<.1)return 0.;")
WriteLine(fw,"	uv+=time/scale;uv.y+=time*2./scale;uv.x+=sin(uv.y+time*.5)/scale;")
WriteLine(fw,"	uv*=scale;vec2 s=floor(uv),f=fract(uv),p;float k=1.,d;")
WriteLine(fw,"	p=.5+.7*sin(11.*fract(sin((s+p+scale)*mat2(7,3,6,5))*5.))-f;d=length(p);k=min(d,k);")
WriteLine(fw,"	k=smoothstep(0.,k,sin(f.x+f.y)*0.01);")
WriteLine(fw,"    	return k*w;")
WriteLine(fw,"}")

WriteLine(fw,"void main(void){")
WriteLine(fw,"	vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y); ")
WriteLine(fw,"	vec3 finalColor=vec3(0);")
WriteLine(fw,"	float c=smoothstep(1.0,0.3,clamp(uv.y*.0+.8,0.0,.75));")
WriteLine(fw,"	c+=snow(uv,20.)*.3;")
WriteLine(fw,"	c+=snow(uv,10.)*.5;")
WriteLine(fw,"	c+=snow(uv,7.5)*.8;")
WriteLine(fw,"	c+=snow(uv,5.);")
WriteLine(fw,"	c+=snow(uv,4.);")
WriteLine(fw,"	c+=snow(uv,3.);")
WriteLine(fw,"	c+=snow(uv,2.5);")
WriteLine(fw,"	if (c>=.5) { finalColor=(vec3(c));}")
WriteLine(fw,"	gl_FragColor = vec4(finalColor,1);")
WriteLine(fw,"}")
CloseFile(fw)
endfunction

function createHouse()
    local data as integer [215] = [
           0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,1,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,1,0,0,1,1,0,1,0,0,0,0,0,0,0,0,0,
           0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,0,0,0,0,0,0,0,0,0]
            
    dataCount=0
     ClearScreen()
     for y = 0 to 8
        for x = 0 to 23
            if data[datacount]=1 then DrawLine(x,y,x,y,255,255,255)
            inc dataCount,1
        next x
    next y  
     
    render()
    img = getImage(0,0,24,10)
    SetImageTransparentColor(img,0,0,0)
endfunction img
Posted: 28th Oct 2019 1:42
@fubarpk
Thanks for all the help full examples

Unfortunate, I don't see any snow with the latest 2d Snow example. Only the red house

Thanks Mate!

EDITED: Works 100% in Classic, but not in Studio...
Posted: 30th Nov 2019 2:34
Some functions that should make accessing mesh memblocks easier (Does not support quads unfortunately)

integer GetPolysFromMemBlock(id as integer)
id = memblock id
integer = The number of polys in this mesh

tPoly = GetPolyFromMemblock(id as integer, n as integer)
id = memblock id
n = poly (from 0 through (GetPolysFromMemBlock() - 1))
tPoly = tPoly.vertex is a table containing information for the 3 vertex that make up the poly
tPoly.vertex.pos - Position of vertex x, y, z
tPoly.vertex.normal - Vertex normal x, y, z
tPoly.vertex.uv - Vertex uv u, v
tPoly.vertex.color - Color of vertex (if supplied)

I'm thinking it would be entirely possible to make a SaveObject() function using this code

NOTE : The GetPolyFromMemblock() will SetErrorMode(2) on exit. It needs to SetErrorMode(0) because if some of the vertex data is missing it will crash

To demonstrate this code will draw an objects wire mesh

+ Code Snippet
SetErrorMode(2)

// set window properties
SetWindowTitle( "GetPolyFromMemblock()" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )

cam = CreateObjectBox(1,1,1) : SetObjectVisible(cam, 0)
dolly = CreateObjectBox(1,1,1) : SetObjectVisible(dolly, 0)

SetObjectPosition(cam, 0, 0, -50)
FixObjectToObject(cam, dolly)

start as tPoint
move as tPoint

model = CreateObjectBox(10, 10, 10)
SetObjectColor(model, 0, 0xff, 0, 0xff)

do	
	print("B-Box, S-Sphere, P-Capsule, C-Cylinder, N-Cone")
	
	if GetRawKeyPressed(asc("S"))
		DeleteObjectMemblock(model)
		model = CreateObjectSphere(16, 8, 8)
		SetObjectColor(model, 0, 0xff, 0, 0xff)
	endif
	
	if GetRawKeyPressed(asc("P"))
		DeleteObjectMemblock(model)
		model = CreateObjectCapsule(8, 16, 1)
		SetObjectColor(model, 0, 0xff, 0, 0xff)
	endif

	if GetRawKeyPressed(asc("B"))
		DeleteObjectMemblock(model)
		model = CreateObjectBox(10, 10, 10)
		SetObjectColor(model, 0, 0xff, 0, 0xff)		
	endif
	
	if GetRawKeyPressed(asc("C"))
		DeleteObjectMemblock(model)
		model = CreateObjectCylinder(16, 8, 8)
		SetObjectColor(model, 0, 0xff, 0, 0xff)		
	endif

	if GetRawKeyPressed(asc("N"))
		DeleteObjectMemblock(model)
		model = CreateObjectCone(16, 8, 8)
		SetObjectColor(model, 0, 0xff, 0, 0xff)		
	endif

	if GetPointerPressed()
		start.x = GetPointerX()
		start.y = GetPointerY()
	endif	
	
	if GetPointerState()
		move.x = GetPointerX() - start.x
		move.y = GetPointerY() - start.y
		RotateObjectLocalX(dolly, move.y)		
		RotateObjectLocalY(dolly, move.x)		
		start.x = GetPointerX()
		start.y = GetPointerY()		
	endif
	
	SetCameraPosition(1, GetObjectWorldX(cam), GetObjectWorldY(cam), GetObjectWorldZ(cam))
	SetCameraLookAt(1,  GetObjectWorldX(dolly), GetObjectWorldY(dolly), GetObjectWorldZ(dolly), 0)

	DrawPolys(model)
			
    Sync()
loop


function	DrawPolys(id as integer)
	poly 		as tPoly
	polys		as integer
	i			as integer
	
	if GetMemblockExists(id) = 0
		CreateMemblockFromObjectMesh(id, id, 1)
	endif

	polys = GetPolysFromMemBlock(id)
	
	for i=0 to polys - 1
		poly = GetPolyFromMemblock(id, i)
		DrawWireLine(poly.vertex[0], poly.vertex[1])
		DrawWireLine(poly.vertex[1], poly.vertex[2])
		DrawWireLine(poly.vertex[2], poly.vertex[0])
	next
	
endfunction
	
function	DrawWireLine(f as tVertex, t as tVertex)	
	sf	as tPoint
	st	as tPoint
	
	sf.x = GetScreenXFrom3D(f.pos.x, f.pos.y, f.pos.z)			
	sf.y = GetScreenYFrom3D(f.pos.x, f.pos.y, f.pos.z)					
	
	st.x = GetScreenXFrom3D(t.pos.x, t.pos.y, t.pos.z)			
	st.y = GetScreenYFrom3D(t.pos.x, t.pos.y, t.pos.z)		
	
	DrawLine(sf.x, sf.y, st.x, st.y, 0xff, 0xff, 0xff)			
	
endfunction


/**************************************************************************************************
	3D memblock functions
	
	integer GetPolysFromMemBlock(id as integer)
	id = memblock id
	integer = The number of polys in this mesh
	
	tPoly = GetPolyFromMemblock(id as integer, n as integer)
	id = memblock id
	n = poly (from 0 through (GetPolysFromMemBlock() - 1))
	tPoly = tPoly.vertex is table containing information for the 3 vertex that make up the poly
		    pos - Position of vertex x, y, z
		    normal - Vertex normal x, y, z
		    uv - Vertex uv u, v
		    color - Color of vertex (if supplied)
		    
 **************************************************************************************************/
 
type tUV
	u as float
	v as float
endtype

type tPoint
	x as float
	y as float
	z as float
endtype

type tVertex
	pos 	as tPoint
	normal 	as tPoint
	uv		as tUV
	color	as integer
endtype

type tPoly
	vertex	as tVertex[]
endtype

function	GetPolysFromMemBlock(id as integer)
	polys 	as integer
	indicies as integer
	
	indicies = GetMemblockInt(id, 4)
	
	if indicies = 0
		polys = GetMemblockInt(id, 0) / 3
	else
		polys = indicies / 3		
	endif
	
endfunction polys

function	GetPolyFromMemblock(id as integer, n as integer)
	poly 		as tPoly
	point		as tPoint
	vertex		as tVertex
	verts		as integer
	indicies	as integer
	i			as integer
	v			as integer
	ioffset		as integer
	
	verts = GetMemblockInt(id, 0)	 
	indicies = GetMemblockInt(id, 4)	 	
	
	SetErrorMode(0)	
	
	if indicies = 0
		for i=(n * 3) to (n * 3)+2	
			vertex.pos.x = GetMeshMemblockVertexX(id, i)
			vertex.pos.y = GetMeshMemblockVertexY(id, i)
			vertex.pos.z = GetMeshMemblockVertexZ(id, i)
			vertex.normal.x = GetMeshMemblockVertexNormalX(id, i)
			vertex.normal.y = GetMeshMemblockVertexNormalY(id, i)
			vertex.normal.z = GetMeshMemblockVertexNormalZ(id, i)			
			vertex.uv.u = GetMeshMemblockVertexU(id, i)
			vertex.uv.v = GetMeshMemblockVertexV(id, i)
			vertex.color = MakeColor(GetMeshMemblockVertexRed(id, i), GetMeshMemblockVertexGreen(id, i), GetMeshMemblockVertexBlue(id, i), GetMeshMemblockVertexAlpha(id, i))
			poly.vertex.insert(vertex)
		next
	else
		ioffset = GetMemblockInt(id, 20) + (n * 12)
		for i=0 to 8 step 4
			v = GetMemblockInt(id, ioffset + i)
			vertex.pos.x = GetMeshMemblockVertexX(id, v)
			vertex.pos.y = GetMeshMemblockVertexY(id, v)
			vertex.pos.z = GetMeshMemblockVertexZ(id, v)
			vertex.normal.x = GetMeshMemblockVertexNormalX(id, v)
			vertex.normal.y = GetMeshMemblockVertexNormalY(id, v)
			vertex.normal.z = GetMeshMemblockVertexNormalZ(id, v)			
			vertex.uv.u = GetMeshMemblockVertexU(id, v)
			vertex.uv.v = GetMeshMemblockVertexV(id, v)
			vertex.color = MakeColor(GetMeshMemblockVertexRed(id, v), GetMeshMemblockVertexGreen(id, v), GetMeshMemblockVertexBlue(id, v), GetMeshMemblockVertexAlpha(id, v))			
			poly.vertex.insert(vertex)			
		next
	endif
	
	SetErrorMode(2)
	
endfunction poly


function DeleteObjectMemblock(id as integer)
	if GetMemblockExists(id)
		DeleteMemblock(id)
	endif
	DeleteObject(id)
endfunction	



Haven't given given it a super dooper test but it seems to work for primitives and i tried it on one of my models
Posted: 30th Nov 2019 2:39
Thanks Blink that could come in very handy
Posted: 30th Nov 2019 2:47
No worries matey.
Just remember it draws the wire mesh using the vertex information so if you move or rotate the object the wire frame stays at 0,0,0
If you want the wireframe to accurately reflect the objects state then you would need to CloneObject(), FixObjectPivot(clone) and then use the clone to create your Mesh MemBlock
Posted: 12th Dec 2019 6:53
not sure if anyone will find this useful but a timer countdown shader
+ Code Snippet
SetWindowSize( 1024, 768, 0 )
SetVirtualResolution( 1024, 768 )
  
 
createShader()
loadimage(1,"timer.png")
CreateSprite(1,1)
SetSpriteSize(1,766,766)
SetSpritePosition(1,0,1)
LoadSpriteShader(1, "timer.ps")
SetSpriteShader( 1,1 )
//resetTimer()
SetShaderConstantByName(1,"iresolution",GetSpriteWidth(1),GetSpriteHeight(1),0,0)
do
    time#=Timer()
    SetShaderConstantByName(1,"itime",time#,0,0,0) 
    print (time#)
    sync()  
 
loop
  
function createShader()
 
file = OpenToWrite("timer.ps")

WriteLine(file,"#ifdef GL_ES")
WriteLine(file,"precision mediump float;")
WriteLine(file,"#endif")

WriteLine(file,"#extension GL_OES_standard_derivatives : enable")

WriteLine(file,"uniform float itime;")
WriteLine(file,"uniform vec2 iresolution;")
WriteLine(file,"uniform sampler2D texture0;")
WriteLine(file,"varying mediump vec2 uvVarying;")

WriteLine(file,"float angle(vec2 p0,vec2 p1) {")
WriteLine(file,"	if (p1.x < p0.x )")
WriteLine(file,"		return radians(180.0) + acos(dot(normalize(p0-p1),vec2(0.0,1.0)));")
WriteLine(file,"	return acos(dot(normalize(p1-p0),vec2(0.0,1.0)));")
WriteLine(file,"}")

WriteLine(file,"void main( void ) {")
WriteLine(file,"	vec2 mid = iresolution.xy / 2.0;")
WriteLine(file,"	float scale = mod(itime / 20.0 ,1.0);")
WriteLine(file,"	float thickness = mid.x;") //try uncommenting and commenting line below 
//WriteLine(file,"	float thickness = 100.0;") 

	
WriteLine(file,"	if (distance(mid,gl_FragCoord.xy)>min(iresolution.x,iresolution.y)/2.0 - thickness && distance(mid,gl_FragCoord.xy)<min(iresolution.x,iresolution.y)/2.0) {")
WriteLine(file,"		if (angle(mid,gl_FragCoord.xy)<=radians(360.0 * scale) )")
WriteLine(file,"			gl_FragColor = vec4(1.0,0.0,0.0,1.0)+texture2D(texture0, uvVarying).rgba;	// Circle Fill Color")
WriteLine(file,"		else")
//WriteLine(file,"			gl_FragColor = vec4(0.0,1.0,0.0,1.0);	// Cirlce Background Color")
WriteLine(file,"			gl_FragColor = vec4(0.0,1.0,0.0,1.0)+texture2D(texture0, uvVarying).rgba;")
WriteLine(file,"	}")
WriteLine(file,"	else")
//WriteLine(file,"		gl_FragColor = vec4(0.0,0.0,0.0,1.0);	// Background Color")
WriteLine(file,"		gl_FragColor = texture2D(texture0, uvVarying).rgba;")
WriteLine(file,"}")
CloseFile(file)
endfunction	

if your after healthbar shaders there is much better ones on the shader thread I just thought ide have a play
ps you need the attached png and your good to go
HAHAHAHA think I need one of those gui shader creators to really grasp them
Posted: 1st Mar 2020 1:47
here is kinda a matrix shader
I don't really like the way it turned out but thought ide share
+ Code Snippet
SetWindowSize(800, 800,0)
SetVirtualResolution(800, 800)
spr=createSprite(0)
SetSpriteSize(spr,800,800)
SetSpriteColor(spr,0,0,0,255)
SetSpriteDepth(spr,0)
SetSpriteTransparency( spr,1 ) 

text=CreateText("Matrix")
SetTextPosition(text,200,300)
SetTextSize(text,100)
SetTextDepth(text,10)
createShader()
shader=LoadSpriteShader("matrix.ps")
SetSpriteShader(spr,shader)
SetShaderConstantByName(shader,"resolution",800,800,0.0,0.0)
do
	time#=Timer()
	SetShaderConstantByName(shader,"time",time#,time#,time#,0.2)
	sync()
loop

function createShader()
file = OpenToWrite("matrix.ps")
WriteLine(file,"#ifdef GL_ES")
WriteLine(file,"precision mediump float;")
WriteLine(file,"#endif")

WriteLine(file,"uniform float time;")
//WriteLine(file,"uniform vec2 mouse;")
WriteLine(file,"uniform vec2 resolution;")


WriteLine(file,"void main( void ) ")
WriteLine(file,"{")

WriteLine(file,"	vec2 uv = ( gl_FragCoord.xy / resolution.xy ) * 2.0 - 1.0;")
WriteLine(file,"	uv.x *= resolution.x/resolution.y;")
	
WriteLine(file,"	vec2 px = vec2(gl_FragCoord.x, gl_FragCoord.y);")
WriteLine(file,"	px.x+=sin(time+px.y*0.01)*0.07;")
	///px *= 4.;
	
	
WriteLine(file,"	vec3 finalColor = vec3( 0.1, 0.2, 0.3 );")

WriteLine(file,"	float a = 0.;//atan( uv.y / uv.x );")
WriteLine(file,"	float r = -1.5 + length( uv );")
	

WriteLine(file,"	float timeT = .1;//sin(time) * 0.5 + 0.5;")
WriteLine(file,"	float move = .6 + time;")
	
WriteLine(file,"     	float t = .7 + .1 * sin(move * 1.);")
    
WriteLine(file,"     	finalColor += vec3( 8.0 * t, 4.0 * t, 2.0 * t );")
    
WriteLine(file,"     	finalColor *= .5 * (1.-r);")
	
WriteLine(file,"	float g = -mod( px.y + time, cos( px.x ) + 0.004 ) * .5;")
WriteLine(file,"	finalColor *= vec3( (g/2.)*(resolution.y-gl_FragCoord.y)/resolution.y, g/2., g );")
WriteLine(file,"	gl_FragColor = vec4( 0.0,0.0,0.0,0.0);")
WriteLine(file,"	if (finalColor.x > .1 || finalColor.y > .1 || finalColor.z > .1 ){")	
WriteLine(file,"		gl_FragColor = vec4( finalColor, 1.0 );")

WriteLine(file,"	}")
WriteLine(file,"}")
CloseFile(file)
endfunction


you could change this
WriteLine(file," finalColor *= vec3( (g/2.)*(resolution.y-gl_FragCoord.y)/resolution.y, g/2., g );")
to this if you just want green
WriteLine(file," finalColor *= vec3( 0, g/2., 0);")
Posted: 1st Mar 2020 3:12
Nice!
Posted: 19th Mar 2020 21:04
I think it might be a good idea to pin this thread. I've been in here for help quite a few times
Posted: 19th Mar 2020 22:02
Thanks blink not so sure I want to pin my own threads tho hahahahahah
hope you are well
Posted: 19th Mar 2020 23:28
Here, use mine . lol