The script does have mouse event callbacks. Maybe that’s whats keeping the reference? How would I completely destroy everything?
Script…PS. The findByName(“Plane”); is at the bottom of the script
var Hotspot = pc.createScript('hotspot');
//Hotspot.attributes.add("cameraEntity", {type: "entity", title: "Camera Entity"});//OLD from demo
Hotspot.attributes.add("id", {type: "number", title: "ID"});
Hotspot.attributes.add("radius", {type: "number", title: "Radius"});
Hotspot.attributes.add("fadeDropOff", {
type: "number",
default: 0.4,
title: "Fade Drop Off",
description: "When to start fading out hotspot relative to the camera direction. 1 for when hotspot is directly inline with the camera. 0 for never."
});
// initialize code called once per entity
Hotspot.prototype.initialize = function() {
//
hotspot_highlightMaterial = this.app.assets.find("Hotspot_Highlight");
this.cameraEntity=this.app.root.findByName("Camera_MV");
//get the app manager script
this.appManagerScript= this.app.root.findByName("AppManager").script.appManager;
if( this.cameraEntity===null)
{console.log("HOTSPOT CANT FIND CAMERA");}
else
{console.log("HOTSPOT FOUND CAMERA: "+this.cameraEntity.name);} //Returns Camera correctly
// Create a hit area using a bounding sphere
this.hitArea = new pc.BoundingSphere(this.entity.getPosition(), this.radius);
// More information about pc.ray: http://developer.playcanvas.com/en/api/pc.Ray.html
this.ray = new pc.Ray();
this.defaultForwardDirection = this.entity.forward.clone();
this.directionToCamera = new pc.Vec3();
this.sprite = this.entity.children[0];
//set the text on this hotspot to match the id
this.hotspotText=this.entity.findByName("Text").element;//you can only reference objects
this.hotspotText.text=this.id+1;
// Register the mouse down and touch start event so we know when the user has clicked
this.app.mouse.on(pc.EVENT_MOUSEDOWN, this.onMouseDown, this);
if (this.app.touch) {
this.app.touch.on(pc.EVENT_TOUCHSTART, this.onTouchStart, this);
}
//reference to all other cutaway hotspots
this.cutaway_hotspots=this.app.root.findByTag("cutaway_hotspots");
// this.cutaway_hotspot_models=this.app.root.findByTag("cutaway_hotspots").model;
console.log("INIT HOTSPOT");
// this.hotspot_model = this.entity.findByName("Plane").model;
this.hotspot_plane_mesh = this.entity.findByName("Plane").model;
};
Hotspot.prototype.postInitialize = function() {
};
// update code called every frame
Hotspot.prototype.update = function(dt) {
var cameraPosition = this.cameraEntity.getPosition();
// Always face the camera
this.entity.lookAt(cameraPosition);
// Get the current direction to the camera
this.directionToCamera.sub2(cameraPosition, this.entity.getPosition());
this.directionToCamera.normalize();
// Get the dot product of the direction to the camera and the original direction of the hotspot
// which is relative to the angle between the two vectors
// Start fading out the hotspot if the dot product is below fadeDropOff
var dot = this.directionToCamera.dot(this.defaultForwardDirection);
if (dot < 0) {
if (this.sprite.enabled) {
this.sprite.enabled = false;
}
} else {
if (!this.sprite.enabled) {
this.sprite.enabled = true;
}
var meshInstances = this.sprite.model.meshInstances;
var alpha = pc.math.clamp(dot / this.fadeDropOff, 0, 1);
for(var i = 0; i < meshInstances.length; ++i) {
meshInstances[i].setParameter("material_opacity", alpha);
}
}
};
Hotspot.prototype.doRayCast = function (screenPosition) {
// Only do the raycast if the sprite is showing
if (this.sprite.enabled) {
// if( this.cameraEntity===null)
// {console.log("HOTSPOT CANT FIND CAMERA");}
// else
// {console.log("HOTSPOT FOUND CAMERA: "+this.cameraEntity.name);}//Returns fine
// if( this.cameraEntity.camera===null)
// {console.log("NO COMPONENT ON CAMERA ENTITY");}
// else
// {console.log("COMPONENT ON CAMERA FOUND : "+this.cameraEntity.camera);}//Returns but its called Undefined
this.cameraEntity=this.app.root.findByName("Camera_MV");//This works if a I put it here. But maybe its slow. Its probably finding the cam from previous scene so maybe quick fix is to rename camera
if(this.cameraEntity!==null)
{
// Initialise the ray and work out the direction of the ray from the a screen position
this.cameraEntity.camera.screenToWorld(screenPosition.x, screenPosition.y, this.cameraEntity.camera.farClip, this.ray.direction);
this.ray.origin.copy(this.cameraEntity.getPosition());
this.ray.direction.sub(this.ray.origin).normalize();
// If the hotspot is clicked on, then send a event to start the 'pulse' effect
if (this.hitArea.intersectsRay(this.ray))
{
this.entity.fire("pulse:start");
this.show_hotspot_information();
}
}
}
};
Hotspot.prototype.onMouseDown = function(event) {
if (event.button == pc.MOUSEBUTTON_LEFT) {
this.doRayCast(event);
}
};
Hotspot.prototype.onTouchStart = function (event) {
// On perform the raycast logic if the user has one finger on the screen
if (event.touches.length == 1) {
this.doRayCast(event.touches[0]);
// Prevent the default mouse down event from triggering
// https://www.w3.org/TR/touch-events/#h3_list-of-touchevent-types
event.event.preventDefault();
}
};
Hotspot.prototype.show_hotspot_information = function ()
{
console.log("Showing Hotspot Info");
//populate left hand window with info from the JSON data file
//hide some text
this.modelNameTextTitle=this.app.root.findByName("Text_Cutaway_Title").element;//you can only reference objects
this.modelNameTextTitle.text="";
//Hotspot Name
this.modelNameText=this.app.root.findByName("Text_Cutaway_SubTitle").element;//you can only reference objects
this.modelNameText.text=this.appManagerScript.mattressData.brands[this.appManagerScript.currentBrand].products[this.appManagerScript.currentMattress].hotspots.cutaway[this.id].info_1;
this.modelNameText2=this.app.root.findByName("Text_Cutaway_Body").element;//you can only reference objects
this.modelNameText2.text=this.appManagerScript.mattressData.brands[this.appManagerScript.currentBrand].products[this.appManagerScript.currentMattress].hotspots.cutaway[this.id].info_2;
//change the hotspot image..
this.hotspot_image_1=this.app.root.findByName("Mattress_Main_Image").element;//you can only reference objects
this.hotspot_image_1_name = this.appManagerScript.mattressData.brands[this.appManagerScript.currentBrand].products[this.appManagerScript.currentMattress].hotspots.cutaway[this.id].image;
var hotspot_imageAsset = this.app.assets.find(this.hotspot_image_1_name);
console.log ("Trying to load "+this.hotspot_image_1_name);
if( hotspot_imageAsset && hotspot_imageAsset.loaded)
{
this.hotspot_image_1.texture= hotspot_imageAsset.resource;
}
//highlight this hotspot and unhighlight others
var hotspot_normalMaterial= this.app.assets.find("Hotspot_Normal");
//var hotspot_highlightMaterial = this.app.assets.find("Hotspot_Highlight");
console.log("This hospot id is:"+this.id);
console.log("this.cutaway_hotspots.length is :"+this.cutaway_hotspots.length );
if(hotspot_highlightMaterial.loaded)
{
for(x=0;x<this.cutaway_hotspots.length > 0;x++)
{
//set texture to normal
this.hotspot_plane_mesh = this.cutaway_hotspots[x].findByName("Plane");
this.hotspot_plane_mesh = this.cutaway_hotspots[x].findByName("Plane").model;// when the scene gets reloaded it cant find this mesh..why not???
this.hotspot_plane_mesh.material = hotspot_normalMaterial.resource;
console.log("SETTING DEFAULT MAT ON id "+x);
}
for(x=0;x<this.cutaway_hotspots.length > 0;x++)
{
//set texture to normal
//but set this hotspot the highlighted
if(x===this.id)
{
console.log("CHANGING MATERIAL on "+this.id);
this.hotspot_plane_mesh = this.cutaway_hotspots[x].findByName("Plane").model;
this.hotspot_plane_mesh.material= hotspot_highlightMaterial.resource;
//this.hotspot_model.material= hotspot_highlightMaterial.resource;
}
}
}
};