@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
2 lines (1 loc) • 5 kB
JavaScript
import{Color as t}from"../../../core/math/color.js";import{Vec3 as e}from"../../../core/math/vec3.js";import{MeshInstance as o}from"../../../scene/mesh-instance.js";import{Entity as s}from"../../../framework/entity.js";import{CULLFACE_BACK as i,SEMANTIC_POSITION as r,SEMANTIC_COLOR as n}from"../../../platform/graphics/constants.js";import{BLEND_NORMAL as l}from"../../../scene/constants.js";import{COLOR_GRAY as a}from"../color.js";import{Mesh as h}from"../../../scene/mesh.js";import{Geometry as m}from"../../../scene/geometry/geometry.js";import{BoxGeometry as c}from"../../../scene/geometry/box-geometry.js";import{CylinderGeometry as d}from"../../../scene/geometry/cylinder-geometry.js";import{ConeGeometry as _}from"../../../scene/geometry/cone-geometry.js";import{PlaneGeometry as p}from"../../../scene/geometry/plane-geometry.js";import{SphereGeometry as g}from"../../../scene/geometry/sphere-geometry.js";import{TorusGeometry as u}from"../../../scene/geometry/torus-geometry.js";import{Mat4 as v}from"../../../core/math/mat4.js";import{createShaderFromCode as f}from"../../../scene/shader-lib/utils.js";import{Material as y}from"../../../scene/materials/material.js";const C=new e(1,2,3),b={box:c,cone:_,cylinder:d,plane:p,sphere:g,torus:u},x={uniqueName:"axis-shape",attributes:{vertex_position:r,vertex_color:n},vertexCode:"\n\t\tattribute vec3 vertex_position;\n\t\tattribute vec4 vertex_color;\n\t\tvarying vec4 vColor;\n\t\tuniform mat4 matrix_model;\n\t\tuniform mat4 matrix_viewProjection;\n\t\tvoid main(void) {\n\t\t\tgl_Position = matrix_viewProjection * matrix_model * vec4(vertex_position, 1.0);\n\t\t\tgl_Position.z = clamp(gl_Position.z, -abs(gl_Position.w), abs(gl_Position.w));\n\t\t\tvColor = vertex_color;\n\t\t}\n\t",fragmentCode:"\n\t\tprecision highp float;\n\t\tvarying vec4 vColor;\n\t\tvoid main(void) {\n\t\t\tgl_FragColor = vColor;\n\t\t}\n\t"},w=new Map,j=new e,I=new e,P=new v,L=new m;L.positions=[],L.normals=[];const M=(t,e,o)=>{if(!t.normals||!t.positions)return[];let s;o&&(s=P.copy(o).invert().transformVector(j.copy(C),j).normalize()),t.colors=[];const i=[],r=t.positions.length/3;for(let o=0;o<r;o++){let r=1;if(s){const e=t.normals[3*o],i=t.normals[3*o+1],n=t.normals[3*o+2],l=I.set(e,i,n);r=.25*s.dot(l)+.75}i.push(r),t.colors.push(r*e.r*255,r*e.g*255,r*e.b*255,255*e.a)}return i},R=(t,e)=>{const o=w.get(t),s=[];for(let t=0;t<o.length;t++)s.push(o[t]*e.r*255,o[t]*e.g*255,o[t]*e.b*255,255*e.a);t.setColors32(s),t.update()};class T{constructor(o,s){var r,n,l,h,m,c,d;this._position=void 0,this._rotation=void 0,this._scale=void 0,this._layers=[],this._shading=!0,this._disabled=void 0,this._defaultColor=t.WHITE,this._hoverColor=t.BLACK,this._disabledColor=a,this._cull=i,this.device=void 0,this.axis=void 0,this.entity=void 0,this.triData=[],this.meshInstances=[],this.device=o,this.axis=null!=(r=s.axis)?r:"x",this._position=null!=(n=s.position)?n:new e,this._rotation=null!=(l=s.rotation)?l:new e,this._scale=null!=(h=s.scale)?h:new e(1,1,1),this._disabled=null!=(m=s.disabled)&&m,this._layers=null!=(c=s.layers)?c:this._layers,this._shading=null!=(d=s.shading)?d:this._shading,s.defaultColor instanceof t&&(this._defaultColor=s.defaultColor),s.hoverColor instanceof t&&(this._hoverColor=s.hoverColor),s.disabledColor instanceof t&&(this._disabledColor=s.disabledColor)}set disabled(t){for(let e=0;e<this.meshInstances.length;e++)R(this.meshInstances[e].mesh,t?this._disabledColor:this._defaultColor);this._disabled=null!=t&&t}get disabled(){return this._disabled}set shading(t){this._shading=null==t||t;const e=this._disabled?this._disabledColor:this._defaultColor;for(let t=0;t<this.meshInstances.length;t++){const o=this.meshInstances[t].mesh;o.getPositions(L.positions),o.getNormals(L.normals);const s=M(L,e,this._shading?this.entity.getWorldTransform():void 0);w.set(o,s),R(o,e)}}get shading(){return this._shading}_createRoot(t){this.entity=new s(`${t}:${this.axis}`),this.entity.setLocalPosition(this._position),this.entity.setLocalEulerAngles(this._rotation),this.entity.setLocalScale(this._scale)}_createMesh(t,e=!0){const o=this._disabled?this._disabledColor:this._defaultColor,s=M(t,o,e?this.entity.getWorldTransform():void 0),i=h.fromGeometry(this.device,t);return w.set(i,s),i}_createRenderComponent(t,e){const s=f(this.device,x.vertexCode,x.fragmentCode,x.uniqueName,{vertex_position:r,vertex_color:n}),i=new y;i.shader=s,i.cull=this._cull,i.blendType=l,i.chunks&&(i.chunks.debugOutputPS=""),i.update();const a=[];for(let t=0;t<e.length;t++){const s=new o(e[t],i);s.cull=!1,a.push(s),this.meshInstances.push(s)}t.addComponent("render",{meshInstances:a,layers:this._layers,castShadows:!1})}_addRenderMesh(t,e,o){const s=b[e];if(!s)throw new Error("Invalid primitive type.");this._createRenderComponent(t,[this._createMesh(new s,o)])}hover(t){if(!this._disabled)for(let e=0;e<this.meshInstances.length;e++){const o=t?this._hoverColor:this._defaultColor,s=this.meshInstances[e].mesh;R(s,o)}}destroy(){this.entity.destroy()}}export{T as Shape};