armory/blender/arm/material/make_particle.py

100 lines
3.7 KiB
Python
Raw Normal View History

import arm.utils
2017-10-04 18:24:13 +02:00
import arm.material.mat_state as mat_state
if arm.is_reload(__name__):
2021-08-04 22:49:38 +02:00
arm.utils = arm.reload_module(arm.utils)
mat_state = arm.reload_module(mat_state)
else:
arm.enable_reload(__name__)
2021-08-04 22:49:38 +02:00
2018-08-03 08:56:26 +02:00
def write(vert, particle_info=None, shadowmap=False):
2017-10-04 18:24:13 +02:00
# Outs
out_index = True if particle_info != None and particle_info['index'] else False
out_age = True if particle_info != None and particle_info['age'] else False
out_lifetime = True if particle_info != None and particle_info['lifetime'] else False
out_location = True if particle_info != None and particle_info['location'] else False
out_size = True if particle_info != None and particle_info['size'] else False
out_velocity = True if particle_info != None and particle_info['velocity'] else False
out_angular_velocity = True if particle_info != None and particle_info['angular_velocity'] else False
2017-09-29 01:18:57 +02:00
2017-09-27 00:04:47 +02:00
vert.add_uniform('mat4 pd', '_particleData')
2017-09-28 00:48:57 +02:00
2018-08-03 08:56:26 +02:00
str_tex_hash = "float fhash(float n) { return fract(sin(n) * 43758.5453); }\n"
2017-09-28 00:48:57 +02:00
vert.add_function(str_tex_hash)
2021-08-04 22:49:38 +02:00
2017-10-04 18:24:13 +02:00
prep = 'float '
if out_age:
prep = ''
vert.add_out('float p_age')
2017-10-15 18:16:55 +02:00
# var p_age = lapTime - p.i * spawnRate
vert.write(prep + 'p_age = pd[3][3] - gl_InstanceID * pd[0][1];')
2017-10-04 18:24:13 +02:00
# p_age -= p_age * fhash(i) * r.lifetime_random;
vert.write('p_age -= p_age * fhash(gl_InstanceID) * pd[2][3];')
2017-09-28 00:48:57 +02:00
2017-10-15 18:16:55 +02:00
# Loop
# pd[0][0] - animtime, loop stored in sign
# vert.write('while (p_age < 0) p_age += pd[0][0];')
vert.write('if (pd[0][0] > 0 && p_age < 0) p_age += (int(-p_age / pd[0][0]) + 1) * pd[0][0];')
2017-09-27 00:04:47 +02:00
# lifetime
2017-10-04 18:24:13 +02:00
prep = 'float '
if out_lifetime:
prep = ''
vert.add_out('float p_lifetime')
vert.write(prep + 'p_lifetime = pd[0][2];')
2019-03-19 08:31:13 +01:00
# clip with nan
2018-08-03 08:56:26 +02:00
vert.write('if (p_age < 0 || p_age > p_lifetime) {')
2019-03-19 08:31:13 +01:00
vert.write(' gl_Position /= 0.0;')
2018-08-03 08:56:26 +02:00
vert.write(' return;')
vert.write('}')
2017-09-28 00:48:57 +02:00
2018-08-03 08:56:26 +02:00
# vert.write('p_age /= 2;') # Match
2021-08-04 22:49:38 +02:00
2017-09-28 00:48:57 +02:00
# object_align_factor / 2 + gxyz
2017-10-04 18:24:13 +02:00
prep = 'vec3 '
if out_velocity:
prep = ''
vert.add_out('vec3 p_velocity')
vert.write(prep + 'p_velocity = vec3(pd[1][0], pd[1][1], pd[1][2]);')
vert.write('p_velocity.x += fhash(gl_InstanceID) * pd[1][3] - pd[1][3] / 2;')
vert.write('p_velocity.y += fhash(gl_InstanceID + pd[0][3]) * pd[1][3] - pd[1][3] / 2;')
vert.write('p_velocity.z += fhash(gl_InstanceID + 2 * pd[0][3]) * pd[1][3] - pd[1][3] / 2;')
2017-09-28 00:48:57 +02:00
# factor_random = pd[1][3]
2017-09-29 01:18:57 +02:00
# p.i = gl_InstanceID
2017-09-28 00:48:57 +02:00
# particles.length = pd[0][3]
2017-09-27 00:04:47 +02:00
# gxyz
2017-10-04 18:24:13 +02:00
vert.write('p_velocity.x += (pd[2][0] * p_age) / 5;')
vert.write('p_velocity.y += (pd[2][1] * p_age) / 5;')
vert.write('p_velocity.z += (pd[2][2] * p_age) / 5;')
prep = 'vec3 '
if out_location:
prep = ''
vert.add_out('vec3 p_location')
vert.write(prep + 'p_location = p_velocity * p_age;')
2017-09-28 00:48:57 +02:00
2017-10-04 18:24:13 +02:00
vert.write('spos.xyz += p_location;')
2017-09-28 00:48:57 +02:00
2017-10-04 18:24:13 +02:00
# Particle fade
2019-04-06 14:30:11 +02:00
if mat_state.material.arm_particle_flag and arm.utils.get_rp().arm_particles == 'On' and mat_state.material.arm_particle_fade:
2017-10-04 18:24:13 +02:00
vert.add_out('float p_fade')
vert.write('p_fade = sin(min((p_age / 2) * 3.141592, 3.141592));')
2017-09-29 01:18:57 +02:00
2017-10-04 18:24:13 +02:00
if out_index:
vert.add_out('float p_index');
vert.write('p_index = gl_InstanceID;')
2017-09-29 17:00:21 +02:00
2017-09-29 01:18:57 +02:00
def write_tilesheet(vert):
# tilesx, tilesy, framerate - pd[3][0], pd[3][1], pd[3][2]
2017-10-15 18:16:55 +02:00
vert.write('int frame = int((p_age) / pd[3][2]);')
2017-09-29 01:18:57 +02:00
vert.write('int tx = frame % int(pd[3][0]);')
vert.write('int ty = int(frame / pd[3][0]);')
vert.write('vec2 tilesheetOffset = vec2(tx * (1 / pd[3][0]), ty * (1 / pd[3][1]));')
2018-12-14 15:27:43 +01:00
vert.write('texCoord = tex * texUnpack + tilesheetOffset;')
2017-09-29 01:18:57 +02:00
# vert.write('texCoord = tex;')