Browse Source

Fixed mojo3d sprites not affected by entity color/alpha.

Mark Sibly 6 years ago
parent
commit
ae18ebcb9b

+ 2 - 0
VERSIONS.TXT

@@ -1,4 +1,6 @@
 
 
+Fixed entity.Color and Entity.Alpha not affecting sprites.
+
 Experimental fix for Mojave vsync not working. Removed support for Window.SwapAsync as it breaks on macos.
 Experimental fix for Mojave vsync not working. Removed support for Window.SwapAsync as it breaks on macos.
 
 
 Experimental fix for Mojave not rendering until window moved.
 Experimental fix for Mojave not rendering until window moved.

+ 2 - 0
modules/mojo3d/assets/shaders/imports/std.glsl

@@ -180,6 +180,8 @@ void transformSpriteVertex(){
 	v_Position=(i_ModelViewMatrix * a_Position).xyz;
 	v_Position=(i_ModelViewMatrix * a_Position).xyz;
 
 
 	v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
 	v_TexCoord0=(m_TextureMatrix * vec3(a_TexCoord0,1.0)).st;
+
+	v_Color=a_Color;
 	
 	
 	gl_Position=i_ModelViewProjectionMatrix * a_Position;
 	gl_Position=i_ModelViewProjectionMatrix * a_Position;
 }
 }

+ 1 - 1
modules/mojo3d/assets/shaders/materials/sprite.glsl

@@ -18,7 +18,7 @@ uniform float m_AlphaDiscard;
 
 
 void main(){
 void main(){
 
 
-	vec4 color=texture2D( m_ColorTexture,v_TexCoord0 ) * m_ColorFactor;
+	vec4 color=texture2D( m_ColorTexture,v_TexCoord0 ) * m_ColorFactor * v_Color;
 	
 	
 	if( color.a<m_AlphaDiscard ) discard;
 	if( color.a<m_AlphaDiscard ) discard;
 		
 		

+ 6 - 4
modules/mojo3d/render/spritebuffer.monkey2

@@ -92,6 +92,8 @@ Class SpriteBuffer
 
 
 			Local sprite:=spriteOps[spritei].sprite
 			Local sprite:=spriteOps[spritei].sprite
 			
 			
+			Local rgba:=(sprite.Color*sprite.Alpha).ToRGBA()
+			
 			Local material:=sprite.Material
 			Local material:=sprite.Material
 			
 			
 			If material<>_material
 			If material<>_material
@@ -120,19 +122,19 @@ Class SpriteBuffer
 			
 			
 			vp[0].position=matrix * New Vec3f( -handle.x,1-handle.y,0 )
 			vp[0].position=matrix * New Vec3f( -handle.x,1-handle.y,0 )
 			vp[0].texCoord0=New Vec2f( texrect.min.x,texrect.min.y )
 			vp[0].texCoord0=New Vec2f( texrect.min.x,texrect.min.y )
-			vp[0].color=~0
+			vp[0].color=rgba
 			
 			
 			vp[1].position=matrix * New Vec3f( 1-handle.x,1-handle.y,0 )
 			vp[1].position=matrix * New Vec3f( 1-handle.x,1-handle.y,0 )
 			vp[1].texCoord0=New Vec2f( texrect.max.x,texrect.min.y )
 			vp[1].texCoord0=New Vec2f( texrect.max.x,texrect.min.y )
-			vp[1].color=~0
+			vp[1].color=rgba
 
 
 			vp[2].position=matrix * New Vec3f( 1-handle.x,-handle.y,0 )
 			vp[2].position=matrix * New Vec3f( 1-handle.x,-handle.y,0 )
 			vp[2].texCoord0=New Vec2f( texrect.max.x,texrect.max.y )
 			vp[2].texCoord0=New Vec2f( texrect.max.x,texrect.max.y )
-			vp[2].color=~0
+			vp[2].color=rgba
 			
 			
 			vp[3].position=matrix * New Vec3f( -handle.x,-handle.y,0 )
 			vp[3].position=matrix * New Vec3f( -handle.x,-handle.y,0 )
 			vp[3].texCoord0=New Vec2f( texrect.min.x,texrect.max.y )
 			vp[3].texCoord0=New Vec2f( texrect.min.x,texrect.max.y )
-			vp[3].color=~0
+			vp[3].color=rgba
 			
 			
 			'bump vertex/index
 			'bump vertex/index
 			vp+=4
 			vp+=4

+ 2 - 2
modules/mojo3d/scene/materials/spritematerial.monkey2

@@ -10,13 +10,13 @@ Class SpriteMaterial Extends Material
 	Method New()
 	Method New()
 		
 		
 		ShaderName="materials/sprite"
 		ShaderName="materials/sprite"
-		AttribMask=1|8
+		AttribMask=1|4|8
 		BlendMode=BlendMode.Alpha
 		BlendMode=BlendMode.Alpha
 		CullMode=CullMode.None
 		CullMode=CullMode.None
 		
 		
 		ColorTexture=Texture.ColorTexture( Color.White )
 		ColorTexture=Texture.ColorTexture( Color.White )
 		ColorFactor=Color.White
 		ColorFactor=Color.White
-		AlphaDiscard=.5
+		AlphaDiscard=0
 		
 		
 		AddInstance()
 		AddInstance()
 	End
 	End

+ 18 - 2
modules/std/graphics/color.monkey2

@@ -196,7 +196,7 @@ Struct Color
 	Operator To:geom.Vec4f()
 	Operator To:geom.Vec4f()
 		Return New Vec4f( r,g,b,a )
 		Return New Vec4f( r,g,b,a )
 	End
 	End
-	
+
 	#rem monkeydoc The Red color component.
 	#rem monkeydoc The Red color component.
 	#end
 	#end
 	Property R:Float()
 	Property R:Float()
@@ -268,7 +268,7 @@ Struct Color
 	Operator-:Color( offset:Float )
 	Operator-:Color( offset:Float )
 		Return New Color( r-offset,g-offset,b-offset,a-offset )
 		Return New Color( r-offset,g-offset,b-offset,a-offset )
 	End
 	End
-
+	
 	#rem monkeydoc Blends the color with another color and returns the result.
 	#rem monkeydoc Blends the color with another color and returns the result.
 	#end	
 	#end	
 	Method Blend:Color( color:Color,delta:Float )
 	Method Blend:Color( color:Color,delta:Float )
@@ -282,6 +282,12 @@ Struct Color
 		Return UInt(a*255) Shl 24 | UInt(r*255) Shl 16 | UInt(g*255) Shl 8 | UInt(b*255)
 		Return UInt(a*255) Shl 24 | UInt(r*255) Shl 16 | UInt(g*255) Shl 8 | UInt(b*255)
 	End
 	End
 
 
+	#rem monkeydoc Converts the color to 32 bit RGBA format.
+	#end	
+	Method ToRGBA:UInt()
+		Return Int(r*255)Shl 24 | Int(g*255)Shl 16 |Int(b*255)Shl 8|Int(a*255)
+	End
+
 	#rem monkeydoc Converts the color to printable string.
 	#rem monkeydoc Converts the color to printable string.
 	#end
 	#end
 	Method ToString:String()
 	Method ToString:String()
@@ -324,6 +330,16 @@ Struct Color
 		Return New Color( r,g,b,a )
 		Return New Color( r,g,b,a )
 	End
 	End
 	
 	
+	#rem monkeydoc Creates a color from a 32 bit RGBA color.
+	#end
+	Function FromRGBA:Color( argb:UInt )
+		Local r:=(argb Shr 24 & $ff)/255.0
+		Local g:=(argb Shr 16 & $ff)/255.0
+		Local b:=(argb Shr 8 & $ff)/255.0
+		Local a:=(argb & $ff)/255.0
+		Return New Color( r,g,b,a )
+	End
+	
 	#rem monkeydoc Creates a random color.
 	#rem monkeydoc Creates a random color.
 	#end
 	#end
 	Function Rnd:Color()
 	Function Rnd:Color()