Untitled

 avatar
unknown
javascript
a year ago
3.1 kB
6
Indexable
Trail.prototype.initialize = function () {
    this.MAX_VERTICES = 1800;
    this.VERTEX_SIZE = 4;
    this.timer = 0;
    this.node = null;
    this.vertices = [];
    this.vertexData = new Float32Array(this.MAX_VERTICES * this.VERTEX_SIZE);
    this.model = null;
    var e = this.color.r + ", " + this.color.g + ", " + this.color.b;

    var shaderDefinition = {
        attributes: {
            poslist: pc.SEMANTIC_POSITION
        },
        vshader: ["attribute vec4 poslist;",
            "",
            "uniform mat4 matrix_viewProjection;",
            "uniform float trail_time;",
            "",
            "varying float vAge;",
            "",
            "void main(void)",
            "{",
            "   vAge = trail_time - poslist.w;",
            "   gl_Position = matrix_viewProjection * vec4(poslist.xyz, 1.0);",
            "}"].join("\n"),

        fshader: ["precision mediump float;",
            "",
            "varying float vAge;",
            "",
            "uniform float trail_lifetime;",
            "uniform bool visible;",
            "void main(void)",
            "{",
            "   vec4 color = vec4(0);",
            "   if(visible) color = vec4(" + e + ", (1.0 - (vAge / trail_lifetime)) *" + this.opacityFactor + ");",
            "   gl_FragColor = color;",
            "}"].join("\n")
    };

    var gl = new pc.Shader(this.app.graphicsDevice, shaderDefinition);
    var material = this.material = new pc.scene.Material();
    material.shader = gl;
    material.setParameter("trail_time", 0);
    material.setParameter("trail_lifetime", this.lifetime);
    material.setParameter("visible", true);
    material.cull = pc.CULLFACE_NONE;
    material.blendType = pc.BLEND_ADDITIVE;
    material.blendSrc = pc.BLENDMODE_SRC_ALPHA;
    material.blendDst = pc.BLENDMODE_ONE_MINUS_SRC_ALPHA;
    material.blendEquation = pc.BLENDEQUATION_ADD;
    material.depthWrite = false;

    var vertexFormat = new pc.VertexFormat(this.app.graphicsDevice, [{
        semantic: pc.SEMANTIC_POSITION,
        components: 4,
        type: pc.TYPE_FLOAT32
    }]);

    // Vertex Buffer for the mesh
    var vertexBuffer = new pc.VertexBuffer(this.app.graphicsDevice, vertexFormat, this.MAX_VERTICES, pc.BUFFER_DYNAMIC);
    var mesh = new pc.scene.Mesh(this.app.graphicsDevice); // Pass the GraphicsDevice
    mesh.vertexBuffer = vertexBuffer;
    mesh.indexBuffer[0] = null;
    mesh.primitive[0].type = pc.PRIMITIVE_TRISTRIP;
    mesh.primitive[0].base = 0;
    mesh.primitive[0].count = 0;
    mesh.primitive[0].indexed = false;

    var node = new pc.GraphNode();
    var instance = new pc.MeshInstance(node, mesh, material);
    instance.layer = pc.LAYER_WORLD;
    instance.cull = false;
    instance.updateKey();
    this.model = new pc.Model();
    this.model.graph = node;
    this.model.meshInstances.push(instance);
    this.setNode(this.entity);
    this.vb = vertexBuffer;
    this.isNoneState = false;
    this.cachedVec = new pc.Vec3(0, 0, 0);
    this.camera = this.app.root.findByName('Camera');
};
Editor is loading...
Leave a Comment