def test_assign1(self): code = """ v2[1] = v1[0] p1.position[2] = p1.size[0] """ p1 = Point(Vector3(2.2, 4.4, 7.7), Vector2(3.3, 5.5), Vector4(1.1, 2.2, 3.3, 4.4)) props = { 'v1': Vector2(8.3, 6.0), 'v2': Vector3(5.6, 3.3, 2.2), 'v3': Vector4(4.4, 6.6, 2.2, 9.9), 'ret': 0.0, 'p1': p1 } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val1 = bs.shader.get_value('v2') val2 = bs.shader.get_value('v1') self.assertAlmostEqual(val1.y, val2.x, places=5) val1 = bs.shader.get_value('p1.position') val2 = bs.shader.get_value('p1.size') self.assertAlmostEqual(val1.z, val2.x, places=5)
def test_arith1(self): code = """ r1 = a1 + a2 r2 = a1 * a2 r3 = a4 * (0.5, 0.5) r4 = a5 / (0.5, 0.5, 0.5) r5 = (7,7,7,7) + a6 r6 = a3 / 3 r7 = a7 % 18 """ props = { 'a1': 1.1, 'a2': 2.2, 'r1': 1.1, 'r2': 3.3, 'a3': 55, 'a4': Vector2(2.2, 4), 'a5': Vector3(5, 6, 7), 'a6': Vector4(3, 4, 5, 6), 'r3': Vector2(3, 4), 'r4': Vector3(1, 1, 1), 'r5': Vector4(11, 1, 1, 1), 'r6': 88, 'a7': 789, 'r7': 1 } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('r1') self.assertAlmostEqual(val, 2.2 + 1.1, places=5) val = bs.shader.get_value('r2') self.assertAlmostEqual(val, 1.1 * 2.2, places=5) val = bs.shader.get_value('r3') self.assertAlmostEqual(val.x, 2.2 * 0.5, places=5) self.assertAlmostEqual(val.y, 4.0 * 0.5, places=5) val = bs.shader.get_value('r4') self.assertAlmostEqual(val.x, 5 * 2.0, places=5) self.assertAlmostEqual(val.y, 6 * 2.0, places=5) self.assertAlmostEqual(val.z, 7 * 2.0, places=5) val = bs.shader.get_value('r5') self.assertAlmostEqual(val.x, 7 + 3.0, places=5) self.assertAlmostEqual(val.y, 7 + 4.0, places=5) self.assertAlmostEqual(val.z, 7 + 5.0, places=5) self.assertAlmostEqual(val.w, 7 + 6.0, places=5) val = bs.shader.get_value('r6') self.assertEqual(val, 55 // 3) val = bs.shader.get_value('r7') self.assertEqual(val, 789 % 18)
def math_fun_test(self, fun, py_fun): num = random() nums = (random(), random()) line1 = "ret = %s(%f)\n" % (fun, num) line2 = "ret2 = %s((%f, %f))\n" % (fun, nums[0], nums[1]) code = line1 + line2 props = { 'ret': 1.1, 'ret2': Vector2(2.2, 4), 'ret3': Vector3(5, 6, 7), 'ret4': Vector4(11, 1, 1, 1) } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('ret') self.assertAlmostEqual(val, py_fun(num), places=3) val = bs.shader.get_value('ret2') self.assertAlmostEqual(val.x, py_fun(nums[0]), places=3) self.assertAlmostEqual(val.y, py_fun(nums[1]), places=3)
def test_pow(self): code = """ ret = pow(1.4, 4.4) ret2 = pow((1.1, 1.25), (2.1, 3.1)) ret3 = pow((1.3, 1.7, 1.8), (1.11, 2.11, 1.45)) ret4 = pow((1.9, 1.15, 2.11, 2.22), (1.77, 2.21, 2.5, 2.71)) """ props = { 'ret': 1.1, 'ret2': Vector2(2.2, 4), 'ret3': Vector3(5, 6, 7), 'ret4': Vector4(11, 1, 1, 1) } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('ret') self.assertAlmostEqual(val, pow(1.4, 4.4), places=3) val = bs.shader.get_value('ret2') self.assertAlmostEqual(val.x, pow(1.1, 2.1), places=3) self.assertAlmostEqual(val.y, pow(1.25, 3.1), places=3) val = bs.shader.get_value('ret3') self.assertAlmostEqual(val.x, pow(1.3, 1.11), places=3) self.assertAlmostEqual(val.y, pow(1.7, 2.11), places=3) self.assertAlmostEqual(val.z, pow(1.8, 1.45), places=3) val = bs.shader.get_value('ret4') self.assertAlmostEqual(val.x, pow(1.9, 1.77), places=3) self.assertAlmostEqual(val.y, pow(1.15, 2.21), places=3) self.assertAlmostEqual(val.z, pow(2.11, 2.5), places=3) self.assertAlmostEqual(val.w, pow(2.22, 2.71), places=3)
def test_arith2(self): code = """ r1 = a1 + 5 r2 = 4 * a2 r3 = 8 * a4 r4 = a5 * 1.1 r5 = a6 * 0.5 """ props = { 'a1': 1.1, 'a2': 2.2, 'r1': 1.1, 'r2': 3.3, 'a3': 55, 'a4': Vector2(2.2, 4), 'a5': Vector3(5, 6, 7), 'a6': Vector4(3, 4, 5, 6), 'r3': Vector2(3, 4), 'r4': Vector3(1, 1, 1), 'r5': Vector4(11, 1, 1, 1) } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('r1') self.assertAlmostEqual(val, 1.1 + 5, places=5) val = bs.shader.get_value('r2') self.assertAlmostEqual(val, 4 * 2.2, places=5) val = bs.shader.get_value('r3') self.assertAlmostEqual(val.x, 8 * 2.2, places=5) self.assertAlmostEqual(val.y, 8 * 4.0, places=5) val = bs.shader.get_value('r4') self.assertAlmostEqual(val.x, 1.1 * 5, places=5) self.assertAlmostEqual(val.y, 1.1 * 6, places=5) self.assertAlmostEqual(val.z, 1.1 * 7, places=5) val = bs.shader.get_value('r5') self.assertAlmostEqual(val.x, 0.5 * 3, places=5) self.assertAlmostEqual(val.y, 0.5 * 4, places=5) self.assertAlmostEqual(val.z, 0.5 * 5, places=5) self.assertAlmostEqual(val.w, 0.5 * 6, places=5)
def test_assign1(self): code = """ m1 = 77 a = m1 m1 = -45 b = m1 m1 = 3.4 c = m1 m1 = -8.8 d = m1 m1 = (-8, 5.1) e = m1 m1 = (-1, 2.25, 7) f = m1 m1 = (8, -2.33, -7, 1) k = m1 """ props = { 'aa': 333, 'a': 111, 'b': 250, 'c': 4.4, 'd': 1.1, 'e': Vector2(2.3, 6.0), 'f': Vector3(5.6, 3.3, 2.2), 'k': Vector4(4.4, 6.6, 2.2, 9.9) } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('a') self.assertEqual(77, val) val = bs.shader.get_value('b') self.assertEqual(-45, val) val = bs.shader.get_value('c') self.assertAlmostEqual(val, 3.4, places=5) val = bs.shader.get_value('d') self.assertAlmostEqual(val, -8.8, places=5) val = bs.shader.get_value('e') self.assertAlmostEqual(val.x, -8.0, places=5) self.assertAlmostEqual(val.y, 5.1, places=5) val = bs.shader.get_value('f') self.assertAlmostEqual(val.x, -1.0, places=5) self.assertAlmostEqual(val.y, 2.25, places=5) self.assertAlmostEqual(val.z, 7.0, places=5) val = bs.shader.get_value('k') self.assertAlmostEqual(val.x, 8.0, places=5) self.assertAlmostEqual(val.y, -2.33, places=5) self.assertAlmostEqual(val.z, -7.0, places=5) self.assertAlmostEqual(val.w, 1.0, places=5)
def test_const1(self): pass code = """ a = 555 b = -36 c = 2.3 d = -9.8 e = (2.3, 5) f = (2.2, 4.77, -2.66) k = (-1.23, 1.44, 9, 7.7) """ props = { 'a': 111, 'b': 250, 'c': 4.4, 'd': 1.1, 'e': Vector2(2.3, 6.0), 'f': Vector3(5.6, 3.3, 2.2), 'k': Vector4(4.4, 6.6, 2.2, 9.9) } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('a') val2 = bs.shader.get_value('b') val3 = bs.shader.get_value('c') val4 = bs.shader.get_value('d') self.assertEqual(555, val) self.assertEqual(-36, val2) self.assertAlmostEqual(val3, 2.3, places=5) self.assertAlmostEqual(val4, -9.8, places=5) val5 = bs.shader.get_value('e') self.assertAlmostEqual(val5.x, 2.3, places=5) self.assertAlmostEqual(val5.y, 5.0, places=5) val6 = bs.shader.get_value('f') self.assertAlmostEqual(val6.x, 2.2, places=5) self.assertAlmostEqual(val6.y, 4.77, places=5) self.assertAlmostEqual(val6.z, -2.66, places=5) val7 = bs.shader.get_value('k') self.assertAlmostEqual(val7.x, -1.23, places=5) self.assertAlmostEqual(val7.y, 1.44, places=5) self.assertAlmostEqual(val7.z, 9.0, places=5) self.assertAlmostEqual(val7.w, 7.7, places=5)
def test_arith1(self): code = """ r1 = a1 + a3 * 8.0 r4 = a5 - 0.2 * (8, 8, 8) """ props = {'a1':1.1, 'a2':2.2, 'r1':1.1, 'r2':3.3, 'a3':55, 'a4':Vector2(2.2, 4), 'a5':Vector3(5,6,7), 'a6':Vector4(3,4,5,6), 'r3':Vector2(3,4), 'r4':Vector3(1,1,1), 'r5':Vector4(11,1,1,1), 'r6':88, 'a7':789, 'r7':1} bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('r1') self.assertAlmostEqual(val, 1.1 + 55 * 8.0, places=4) val = bs.shader.get_value('r4') self.assertAlmostEqual(val.x, 5 - 0.2 * 8, places=4) self.assertAlmostEqual(val.y, 6 - 0.2 * 8, places=4) self.assertAlmostEqual(val.z, 7 - 0.2 * 8, places=4)
def test_assign2(self): register_user_type(Point) p1 = Point(33, 77, 99, 3.5, Vector3(2.2, 4.4, 7.7), Vector2(3.3, 5.5), Vector4(1.1, 2.2, 3.3, 4.4)) code = """ idx = p1.idx p1.x = 55 pp = 555 p1.y = pp g = 4.8 p1.radius = g g = (4,5,6) p1.position = g p1.size = (6,7) p1.pp = (8,1,3,4) """ props = {'p1': p1, 'idx': 44} bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('idx') self.assertEqual(33, val) val = bs.shader.get_value('p1.x') self.assertEqual(55, val) val = bs.shader.get_value('p1.y') self.assertEqual(555, val) val = bs.shader.get_value('p1.radius') self.assertAlmostEqual(val, 4.8, places=5) val = bs.shader.get_value('p1.position') self.assertAlmostEqual(val.x, 4.0, places=5) self.assertAlmostEqual(val.y, 5.0, places=5) self.assertAlmostEqual(val.z, 6.0, places=5) val = bs.shader.get_value('p1.size') self.assertAlmostEqual(val.x, 6.0, places=5) self.assertAlmostEqual(val.y, 7.0, places=5) val = bs.shader.get_value('p1.pp') self.assertAlmostEqual(val.x, 8.0, places=5) self.assertAlmostEqual(val.y, 1.0, places=5) self.assertAlmostEqual(val.z, 3.0, places=5) self.assertAlmostEqual(val.w, 4.0, places=5)
def test_float1(self): v = Vector2(1, 1) v2 = Vector2(1, 1) v3 = Vector3(1, 1, 1) v4 = Vector4(1, 1, 1, 1) code = """ ret = float2(5,6) ret2 = float2(aa, bb) ret3 = float3(8,9.5,3) temp = (8,9,1,2) ret4 = float4(temp[1],5.3,6, aa) """ props = { 'aa': 3.4, 'bb': 77, 'ret': v, 'ret2': v2, 'ret3': v3, 'ret4': v4 } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('ret') self.assertAlmostEqual(val.x, 5.0, places=5) self.assertAlmostEqual(val.y, 6.0, places=5) val = bs.shader.get_value('ret2') self.assertAlmostEqual(val.x, 3.4, places=5) self.assertAlmostEqual(val.y, 77.0, places=5) val = bs.shader.get_value('ret3') self.assertAlmostEqual(val.x, 8.0, places=5) self.assertAlmostEqual(val.y, 9.5, places=5) self.assertAlmostEqual(val.z, 3.0, places=5) val = bs.shader.get_value('ret4') self.assertAlmostEqual(val.x, 9.0, places=5) self.assertAlmostEqual(val.y, 5.3, places=5) self.assertAlmostEqual(val.z, 6.0, places=5) self.assertAlmostEqual(val.w, 3.4, places=5)
def test_get_rgba(self): img1 = ImagePRGBA(200, 200) img1.set_pixel(25, 25, 0.2, 0.3, 0.4, 0.5) v1 = Vector4(0, 0, 0, 0) code = """ v1 = get_rgba(img, 25, 25) """ props = {'img': img1, 'v1': v1} bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('v1') self.assertAlmostEqual(val.x, 0.2, places=5) self.assertAlmostEqual(val.y, 0.3, places=5) self.assertAlmostEqual(val.z, 0.4, places=5) self.assertAlmostEqual(val.w, 0.5, places=5)
def test_lum1(self): code = """ ret1 = luminance(v1) ret2 = luminance(v2) """ v1 = Vector3(0.2, 0.3, 0.4) v2 = Vector4(0.4, 0.1, 0.2, 0.5) props = {'v1': v1, 'v2': v2, 'ret1': 0.0, 'ret2': 0.0} bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() lum1 = v1.x * 0.2126 + v1.y * 0.7152 + v1.z * 0.0722 lum2 = bs.shader.get_value('ret1') lum3 = v2.x * 0.2126 + v2.y * 0.7152 + v2.z * 0.0722 lum4 = bs.shader.get_value('ret2') self.assertAlmostEqual(lum1, lum2, places=5) self.assertAlmostEqual(lum3, lum4, places=5)
def test_atanr2(self): code = """ ret = atanr2(1.4, 4.4) ret2 = atanr2((1.1, 1.25), (2.1, 3.1)) """ props = { 'ret': 1.1, 'ret2': Vector2(2.2, 4), 'ret3': Vector3(5, 6, 7), 'ret4': Vector4(11, 1, 1, 1) } bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) #print (bs.shader._code) bs.execute() val = bs.shader.get_value('ret') self.assertAlmostEqual(val, math.atan2(1.4, 1.0 / 4.4), places=3) val = bs.shader.get_value('ret2') self.assertAlmostEqual(val.x, math.atan2(1.1, 1.0 / 2.1), places=3) self.assertAlmostEqual(val.y, math.atan2(1.25, 1.0 / 3.1), places=3)
import math import unittest from random import random from tdasm import Runtime from renmas3.base import BasicShader, Integer, Float, Vec3, Vec2, Vec4 from renmas3.base import Vector2, Vector3, Vector4, Struct from renmas3.base import register_user_type, create_user_type code = """ ret = random() ret2 = random2() ret3 = random3() ret4 = random4() """ props = {'ret':1.1, 'ret2':Vector2(2.2, 4), 'ret3':Vector3(5,6,7), 'ret4':Vector4(11,1,1,1)} bs = BasicShader(code, props) runtime = Runtime() bs.prepare([runtime]) print (bs.shader._code) bs.execute() val = bs.shader.get_value('ret') print(val) val = bs.shader.get_value('ret2') print(val) val = bs.shader.get_value('ret3') print(val) val = bs.shader.get_value('ret4') print(val)